mescc: Tinycc support: struct.
* scaffold/tests/72-typedef-struct-def.c: New file. * module/language/c99/compiler.mes (decl->info): Struct support for tinycc.
This commit is contained in:
parent
94b3c828d3
commit
07ee1fbf4f
3
make.scm
3
make.scm
|
@ -150,7 +150,8 @@ exec ${GUILE-guile} --no-auto-compile -L . -L guile -C . -C guile -s "$0" ${1+"$
|
|||
(for-each
|
||||
add-scaffold-test
|
||||
'("70-printf"
|
||||
"71-struct-array"))
|
||||
"71-struct-array"
|
||||
"72-typedef-struct-def"))
|
||||
|
||||
(add-target (group "check-scaffold-tests/7" #:dependencies (filter (target-prefix? "check-scaffold/tests/7") %targets)))
|
||||
|
||||
|
|
|
@ -498,6 +498,11 @@
|
|||
(size (type->size info type)))
|
||||
(append-text info (wrap-as (i386:value->accu size)))))
|
||||
|
||||
((sizeof-type (type-name (decl-spec-list (type-spec (struct-ref (ident (,name)))))))
|
||||
(let* ((type (list "struct" name))
|
||||
(size (ast-type->size info type)))
|
||||
(append-text info (wrap-as (i386:value->accu size)))))
|
||||
|
||||
((sizeof-type (type-name (decl-spec-list (type-spec (struct-ref (ident ,name))))))
|
||||
(let* ((type (list "struct" name))
|
||||
(size (type->size info type)))
|
||||
|
@ -911,7 +916,9 @@
|
|||
((decl-spec-list (type-spec (fixed-type ,type)))
|
||||
(type->size info type))
|
||||
((decl-spec-list (type-qual ,qual) (type-spec (fixed-type ,type)))
|
||||
(type->size info type))
|
||||
(type->type info type))
|
||||
((struct-ref (ident (,type)))
|
||||
(type->type info `("struct" ,type)))
|
||||
((struct-ref (ident ,type))
|
||||
(type->size info `("struct" ,type)))
|
||||
(void 4)
|
||||
|
@ -930,6 +937,10 @@
|
|||
(pmatch o
|
||||
((fixed-type ,type)
|
||||
type)
|
||||
((typename ,type)
|
||||
type)
|
||||
((struct-ref (ident (,type)))
|
||||
(list "struct" type))
|
||||
((struct-ref (ident ,type))
|
||||
(list "struct" type))
|
||||
(_ (stderr "SKIP: type=~s\n" o)
|
||||
|
@ -938,6 +949,7 @@
|
|||
(define (decl->type o)
|
||||
(pmatch o
|
||||
((fixed-type ,type) type)
|
||||
((struct-ref (ident (,name))) (list "struct" name))
|
||||
((struct-ref (ident ,name)) (list "struct" name))
|
||||
((struct-def (ident ,name) . ,fields) (list "struct" name))
|
||||
((decl (decl-spec-list (type-spec (struct-ref (ident ,name))))) ;; "scm"
|
||||
|
@ -1070,47 +1082,55 @@
|
|||
((string-prefix? "0" s) (string->number s 8))
|
||||
(else (string->number s))))
|
||||
|
||||
(define (struct-field o)
|
||||
(pmatch o
|
||||
((comp-decl (decl-spec-list (type-spec (enum-ref (ident ,type))))
|
||||
(comp-declr-list (comp-declr (ident ,name))))
|
||||
(list name type 4))
|
||||
((comp-decl (decl-spec-list (type-spec (fixed-type ,type))) (comp-declr-list (comp-declr (ident ,name))))
|
||||
(list name type 4))
|
||||
((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (ident ,name))))
|
||||
(list name type 4))
|
||||
((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (ptr-declr (pointer (pointer)) (ident ,name)))))
|
||||
(list name type 4)) ;; FIXME: **
|
||||
((comp-decl (decl-spec-list (type-spec (fixed-type ,type))) (comp-declr-list (comp-declr (ftn-declr (scope (ptr-declr (pointer) (ident ,name))) (param-list . ,param-list)))))
|
||||
(list name type 4)) ;; FIXME function / int
|
||||
((comp-decl (decl-spec-list (type-spec (fixed-type ,type))) (comp-declr-list (comp-declr (ptr-declr (pointer) (ident ,name)))))
|
||||
(list name type 4)) ;; FIXME: ptr/char
|
||||
((comp-decl (decl-spec-list (type-spec (fixed-type ,type))) (comp-declr-list (comp-declr (ptr-declr (pointer (pointer)) (ident ,name)))))
|
||||
(list name type 4)) ;; FIXME: **
|
||||
((comp-decl (decl-spec-list (type-spec (void))) (comp-declr-list (comp-declr (ptr-declr (pointer) (ident ,name)))))
|
||||
(list name '(void) 4)) ;; FIXME: *
|
||||
((comp-decl (decl-spec-list (type-spec (void))) (comp-declr-list (comp-declr (ftn-declr (scope (ptr-declr (pointer) (ident ,name))) (param-list . ,param-list)))))
|
||||
(list name '(void) 4))
|
||||
((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (ptr-declr (pointer) (ident ,name)))))
|
||||
(list name '(void) 4))
|
||||
((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (ptr-declr (pointer) (array-of (ident ,name) (p-expr (fixed ,count)))))))
|
||||
(let ((size 4)
|
||||
(count (cstring->number count)))
|
||||
(list name type (* count size) 0)))
|
||||
((comp-decl (decl-spec-list (type-spec (fixed-type ,type))) (comp-declr-list (comp-declr (array-of (ident ,name) (p-expr (fixed ,count))))))
|
||||
(let ((size 4)
|
||||
(count (cstring->number count)))
|
||||
(list name type (* count size) 0)))
|
||||
((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (array-of (ident ,name) (p-expr (fixed ,count))))))
|
||||
(let ((size 4)
|
||||
(count (cstring->number count)))
|
||||
(list name type (* count size) 0)))
|
||||
;; struct InlineFunc **inline_fns;
|
||||
((comp-decl (decl-spec-list (type-spec (struct-ref (ident ,type)))) (comp-declr-list (comp-declr (ptr-declr (pointer (pointer)) (ident ,name)))))
|
||||
(list name type 4))
|
||||
((comp-decl (decl-spec-list (type-spec (struct-ref (ident ,type)))) (comp-declr-list (comp-declr (ptr-declr (pointer) (ident ,name)))))
|
||||
(list name type 4))
|
||||
(_ (error "struct-field: unsupported: " o))))
|
||||
(define (struct-field info)
|
||||
(lambda (o)
|
||||
(pmatch o
|
||||
((comp-decl (decl-spec-list (type-spec (enum-ref (ident ,type))))
|
||||
(comp-declr-list (comp-declr (ident ,name))))
|
||||
(list name type 4))
|
||||
((comp-decl (decl-spec-list (type-spec (fixed-type ,type))) (comp-declr-list (comp-declr (ident ,name))))
|
||||
(list name type 4))
|
||||
((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (ident ,name))))
|
||||
(list name type 4))
|
||||
((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (ptr-declr (pointer (pointer)) (ident ,name)))))
|
||||
(list name type 4)) ;; FIXME: **
|
||||
((comp-decl (decl-spec-list (type-spec (fixed-type ,type))) (comp-declr-list (comp-declr (ftn-declr (scope (ptr-declr (pointer) (ident ,name))) (param-list . ,param-list)))))
|
||||
(list name type 4)) ;; FIXME function / int
|
||||
((comp-decl (decl-spec-list (type-spec (fixed-type ,type))) (comp-declr-list (comp-declr (ptr-declr (pointer) (ident ,name)))))
|
||||
(list name type 4)) ;; FIXME: ptr/char
|
||||
((comp-decl (decl-spec-list (type-spec (fixed-type ,type))) (comp-declr-list (comp-declr (ptr-declr (pointer (pointer)) (ident ,name)))))
|
||||
(list name type 4)) ;; FIXME: **
|
||||
((comp-decl (decl-spec-list (type-spec (void))) (comp-declr-list (comp-declr (ptr-declr (pointer) (ident ,name)))))
|
||||
(list name '(void) 4)) ;; FIXME: *
|
||||
((comp-decl (decl-spec-list (type-spec (void))) (comp-declr-list (comp-declr (ftn-declr (scope (ptr-declr (pointer) (ident ,name))) (param-list . ,param-list)))))
|
||||
(list name '(void) 4))
|
||||
((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (ptr-declr (pointer) (ident ,name)))))
|
||||
(list name type 4))
|
||||
((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (ptr-declr (pointer) (array-of (ident ,name) (p-expr (fixed ,count)))))))
|
||||
(let ((size 4)
|
||||
(count (cstring->number count)))
|
||||
(list name type (* count size) 0)))
|
||||
((comp-decl (decl-spec-list (type-spec (fixed-type ,type))) (comp-declr-list (comp-declr (array-of (ident ,name) (p-expr (fixed ,count))))))
|
||||
(let ((size 4)
|
||||
(count (cstring->number count)))
|
||||
(list name type (* count size) 0)))
|
||||
((comp-decl (decl-spec-list (type-spec (typename ,type))) (comp-declr-list (comp-declr (array-of (ident ,name) (p-expr (fixed ,count))))))
|
||||
(let ((size 4)
|
||||
(count (cstring->number count)))
|
||||
(list name type (* count size) 0)))
|
||||
;; struct InlineFunc **inline_fns;
|
||||
((comp-decl (decl-spec-list (type-spec (struct-ref (ident ,type)))) (comp-declr-list (comp-declr (ptr-declr (pointer (pointer)) (ident ,name)))))
|
||||
(list name type 4))
|
||||
((comp-decl (decl-spec-list (type-spec (struct-ref (ident ,type)))) (comp-declr-list (comp-declr (ptr-declr (pointer) (ident ,name)))))
|
||||
(list name type 4))
|
||||
|
||||
((comp-decl (decl-spec-list (type-spec (struct-ref (ident ,type)))) (comp-declr-list (comp-declr (ident ,name))))
|
||||
(let ((size (type->size info `("struct" ,type))))
|
||||
(list name type size 0)))
|
||||
|
||||
(_ (error "struct-field: unsupported: " o)))
|
||||
)
|
||||
)
|
||||
|
||||
(define (ident->decl info o)
|
||||
(or (assoc-ref (.locals info) o)
|
||||
|
@ -1239,6 +1259,17 @@
|
|||
(clone info #:functions (cons (cons name #f) functions))))
|
||||
(pmatch o
|
||||
|
||||
;; FIXME: Nyacc sometimes produces extra parens: (ident (<struct-name>))
|
||||
((decl (decl-spec-list (stor-spec ,spec) (type-spec (struct-ref (ident (,type))))) ,init)
|
||||
((decl->info info) `(decl (decl-spec-list (stor-spec ,spec) (type-spec (struct-ref (ident ,type)))) ,init)))
|
||||
((decl (decl-spec-list (type-spec (struct-ref (ident (,type))))) (init-declr-list (init-declr (ptr-declr (pointer) (ident ,name)))))
|
||||
((decl->info info) `(decl (decl-spec-list (type-spec (struct-ref (ident ,type)))) (init-declr-list (init-declr (ptr-declr (pointer) (ident ,name)))))))
|
||||
|
||||
((decl (decl-spec-list (type-spec (struct-def (ident (,type)) ,field-list))))
|
||||
((decl->info info) `(decl (decl-spec-list (type-spec (struct-def (ident ,type) ,field-list))))))
|
||||
|
||||
|
||||
|
||||
((decl (decl-spec-list (type-spec (typename ,type))) (init-declr-list (init-declr (ftn-declr (ident ,name) (param-list . ,param-list)))))
|
||||
(declare name))
|
||||
|
||||
|
@ -1318,9 +1349,6 @@
|
|||
((decl (decl-spec-list (stor-spec (extern)) (type-spec (fixed-type ,type))) (init-declr-list (init-declr (ptr-declr (pointer) (ident ,name)))))
|
||||
info)
|
||||
|
||||
((decl (decl-spec-list (stor-spec (typedef)) (type-spec (struct-ref (ident (,type))))) (init-declr-list (init-declr (ident ,name))))
|
||||
(clone info #:types (cons (cons name (or (get-type types type) `(typedef ("struct" ,type)))) types)))
|
||||
|
||||
((decl (decl-spec-list (stor-spec (typedef)) (type-spec (struct-ref (ident ,type)))) (init-declr-list (init-declr (ident ,name))))
|
||||
(clone info #:types (cons (cons name (or (get-type types type) `(typedef ("struct" ,type)))) types)))
|
||||
|
||||
|
@ -1331,8 +1359,13 @@
|
|||
(clone info #:types (cons (cons name (or (get-type types type) `(typedef ,type))) types)))
|
||||
|
||||
((decl (decl-spec-list (stor-spec (typedef)) (type-spec (struct-def ,field-list))) (init-declr-list (init-declr (ident ,name))))
|
||||
(let ((info ((ast->info info) `(decl (decl-spec-list (type-spec (struct-def (ident ,name) ,field-list))) (init-declr-list (init-declr (ident ,name)))))))
|
||||
(clone info #:types (cons (cons name (or (get-type types `("struct" ,name)) `(typedef ,name))) types))))
|
||||
((decl->info info) `(decl (decl-spec-list (stor-spec (typedef)) (type-spec (struct-def (ident ,name) ,field-list))) (init-declr-list (init-declr (ident ,name))))))
|
||||
|
||||
((decl (decl-spec-list (stor-spec (typedef)) (type-spec (struct-def (ident ,type) ,field-list))) (init-declr-list (init-declr (ident ,name))))
|
||||
(let* ((info ((decl->info info) `(decl (decl-spec-list (type-spec (struct-def (ident ,type) ,field-list))))))
|
||||
(types (.types info)))
|
||||
(clone info #:types (cons (cons name (or (get-type types `("struct" ,type)) `(typedef ,type))) types))))
|
||||
|
||||
|
||||
((decl (decl-spec-list (stor-spec (typedef)) (type-spec (fixed-type ,type))) (init-declr-list (init-declr (ptr-declr (pointer) (ident ,name)))))
|
||||
(let* ((type (get-type types type))
|
||||
|
@ -1345,7 +1378,7 @@
|
|||
|
||||
;; struct
|
||||
((decl (decl-spec-list (type-spec (struct-def (ident ,name) (field-list . ,fields)))))
|
||||
(let ((type-entry (struct->type-entry name (map struct-field fields))))
|
||||
(let ((type-entry (struct->type-entry name (map (struct-field info) fields))))
|
||||
(clone info #:types (cons type-entry types))))
|
||||
|
||||
;; enum e i;
|
||||
|
@ -1445,17 +1478,14 @@
|
|||
(clone info
|
||||
#:constants (append constants (.constants info)))))
|
||||
|
||||
;; FIXME TCC/Nyacc madness here: extra parentheses around struct name?!?
|
||||
;; struct (FOO) WTF?
|
||||
((decl (decl-spec-list (type-spec (struct-def (ident (,name)) (field-list . ,fields)))))
|
||||
(let ((type-entry (struct->type-entry name (map struct-field fields))))
|
||||
((decl (decl-spec-list (type-spec (struct-def (ident ,name) (field-list . ,fields)))))
|
||||
(let ((type-entry (struct->type-entry name (map (struct-field info) fields))))
|
||||
(clone info #:types (cons type-entry types))))
|
||||
|
||||
((decl (decl-spec-list (type-spec (struct-def (ident (,type)) (field-list . ,fields))))
|
||||
((decl (decl-spec-list (type-spec (struct-def (ident ,type) (field-list . ,fields))))
|
||||
(init-declr-list (init-declr (ident ,name))))
|
||||
(let ((info ((ast->info info) `(decl (decl-spec-list (type-spec (struct-def (ident ,type) (field-list . ,fields))))))))
|
||||
((ast->info info)
|
||||
`(decl (decl-spec-list (type-spec (struct-ref (ident ,type)))) (init-declr-list (init-declr (ident ,name)))))))
|
||||
(let ((info ((decl->info info) `(decl (decl-spec-list (type-spec (struct-def (ident ,type) (field-list . ,fields))))))))
|
||||
((decl->info info) `(decl (decl-spec-list (type-spec (struct-ref (ident ,type)))) (init-declr-list (init-declr (ident ,name)))))))
|
||||
|
||||
;; struct f = {...};
|
||||
;; LOCALS!
|
||||
|
@ -1547,10 +1577,9 @@
|
|||
(let loop ((inits `((init-declr (ident ,name) ,@initzer) ,@rest)) (info info))
|
||||
(if (null? inits) info
|
||||
(loop (cdr inits)
|
||||
((ast->info info)
|
||||
((decl->info info)
|
||||
`(decl (decl-spec-list (type-spec (fixed-type ,type))) (init-declr-list ,(car inits))))))))
|
||||
|
||||
|
||||
((decl (decl-spec-list (stor-spec (typedef)) ,type) ,name)
|
||||
(format (current-error-port) "SKIP: typedef=~s\n" o)
|
||||
info)
|
||||
|
@ -1559,7 +1588,7 @@
|
|||
(format (current-error-port) "SKIP: at=~s\n" o)
|
||||
info)
|
||||
|
||||
((decl . _) (error "ast->info: unsupported: " o))))))
|
||||
((decl . _) (error "decl->info: unsupported: " o))))))
|
||||
|
||||
(define (ast->info info)
|
||||
(lambda (o)
|
||||
|
@ -1830,7 +1859,7 @@
|
|||
(define (type->info info o)
|
||||
(pmatch o
|
||||
((struct-def (ident ,name) (field-list . ,fields))
|
||||
(let ((type-entry (struct->type-entry name (map struct-field fields))))
|
||||
(let ((type-entry (struct->type-entry name (map (struct-field info) fields))))
|
||||
(clone info #:types (cons type-entry (.types info)))))
|
||||
(_ info)))
|
||||
|
||||
|
|
44
scaffold/tests/72-typedef-struct-def.c
Normal file
44
scaffold/tests/72-typedef-struct-def.c
Normal file
|
@ -0,0 +1,44 @@
|
|||
/* -*-comment-start: "//";comment-end:""-*-
|
||||
* Mes --- Maxwell Equations of Software
|
||||
* Copyright © 2017 Jan Nieuwenhuizen <janneke@gnu.org>
|
||||
*
|
||||
* This file is part of Mes.
|
||||
*
|
||||
* Mes is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or (at
|
||||
* your option) any later version.
|
||||
*
|
||||
* Mes is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Mes. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "30-test.i"
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct foo
|
||||
{
|
||||
int i;
|
||||
} foo;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int i;
|
||||
} bar;
|
||||
|
||||
int
|
||||
test ()
|
||||
{
|
||||
foo b = {1};
|
||||
printf ("b.i=%d\n", b.i);
|
||||
|
||||
bar b = {2};
|
||||
printf ("b.i=%d\n", b.i);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in a new issue