mescc: Tinycc support: Fix sizeof interesting expressions.

* module/language/c99/compiler.mes (ast->type): Return type for
  sizeof-expr, sizeof-type.
* scaffold/tests/85-sizeof.c: Test it.
This commit is contained in:
Jan Nieuwenhuizen 2018-05-14 20:14:42 +02:00
parent 0f87473105
commit 010ab2032f
No known key found for this signature in database
GPG key ID: F3C1A0D9C1D65273
2 changed files with 37 additions and 14 deletions

View file

@ -189,16 +189,8 @@
((char ,value) (get-type "char" info)) ((char ,value) (get-type "char" info))
((enum-ref . _) (get-type "int" info)) ((enum-ref . _) (get-type "int" info))
((fixed ,value) (get-type "int" info)) ((fixed ,value) (get-type "int" info))
((sizeof-expr . _) (get-type "int" info))
((sizeof-type . _) (get-type "int" info))
((string ,string) (make-c-array (get-type "char" info) (1+ (string-length string))))
((void) (get-type "void" info)) ((void) (get-type "void" info))
((type-name ,type) (ast->type type info))
((type-name ,type (abs-declr ,pointer))
(let ((rank (pointer->rank pointer)))
(rank+= (ast->type type info) rank)))
((ident ,name) (ident->type info name)) ((ident ,name) (ident->type info name))
((tag ,name) (or (get-type o info) ((tag ,name) (or (get-type o info)
o)) o))
@ -207,6 +199,21 @@
(let ((type (get-type name info))) (let ((type (get-type name info)))
(ast->type type info))) (ast->type type info)))
((type-name (decl-spec-list ,type) (abs-declr (pointer . ,pointer)))
(let ((rank (pointer->rank `(pointer ,@pointer)))
(type (ast->type type info)))
(rank+= type rank)))
((type-name ,type) (ast->type type info))
((type-spec ,type) (ast->type type info))
((sizeof-expr ,expr) (ast->type expr info))
((sizeof-type ,type) (ast->type type info))
((string ,string) (make-c-array (get-type "char" info) (1+ (string-length string))))
((decl-spec-list (type-spec ,type)) (ast->type type info))
((fctn-call (p-expr (ident ,name)) . _) ((fctn-call (p-expr (ident ,name)) . _)
(or (and=> (assoc-ref (.functions info) name) function:type) (or (and=> (assoc-ref (.functions info) name) function:type)
(get-type "int" info))) (get-type "int" info)))
@ -1586,9 +1593,7 @@
(_ (error "struct-field: not supported: " o))))) (_ (error "struct-field: not supported: " o)))))
(define (->size o) (define (->size o)
(cond ((and (type? o) (eq? (type:type o) 'struct)) (cond ((and (type? o) (eq? (type:type o) 'union))
(apply + (map (compose ->size cdr) (struct->fields o))))
((and (type? o) (eq? (type:type o) 'union))
(apply max (map (compose ->size cdr) (struct->fields o)))) (apply max (map (compose ->size cdr) (struct->fields o))))
((type? o) (type:size o)) ((type? o) (type:size o))
((pointer? o) %pointer-size) ((pointer? o) %pointer-size)

View file

@ -24,13 +24,31 @@ struct foo
char buf[16]; char buf[16];
}; };
struct bar
{
struct
{
int x;
int y;
int z;
};
};
int int
main () main ()
{ {
char **p; char **p;
if (sizeof (*p) != 4) return 1; if (sizeof (*p) != 4)
if (sizeof (**p) != 1) return 2; return 1;
if (sizeof (**p) != 1)
return 2;
puts ("size: "); puts (itoa (sizeof (struct foo))); puts ("\n"); puts ("size: "); puts (itoa (sizeof (struct foo))); puts ("\n");
if (sizeof (struct foo) != 20) return 3; if (sizeof (struct foo) != 20)
return 3;
struct foo f;
if (sizeof f != 20)
return 4;
if (sizeof (struct bar) != 12)
return 5;
return 0; return 0;
} }