From e8969af4ca88d91ec210a87fd6714034584039d1 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sat, 12 May 2018 11:25:35 +0200 Subject: [PATCH] mescc: Tinycc support: sizeof: Bugfix for c-array. * module/language/c99/compiler.mes (->size): Bugfix for c-array. * scaffold/tests/85-sizeof.c: Test it. --- module/language/c99/compiler.mes | 5 +++-- scaffold/tests/85-sizeof.c | 13 +++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/module/language/c99/compiler.mes b/module/language/c99/compiler.mes index 82bb1b50..247fba12 100644 --- a/module/language/c99/compiler.mes +++ b/module/language/c99/compiler.mes @@ -1552,7 +1552,7 @@ (apply max (map (compose ->size cdr) (struct->fields o)))) ((type? o) (type:size o)) ((pointer? o) %pointer-size) - ((c-array? o) %pointer-size) + ((c-array? o) (* (c-array:count o) ((compose type:size c-array:type) o))) ((local? o) ((compose ->size local:type) o)) ((global? o) ((compose ->size global:type) o)) ;; FIXME @@ -2087,7 +2087,8 @@ (info (if (null? strings) info (clone info #:globals (append (.globals info) strings)))) (count (expr->number info count)) - (type (make-c-array (rank++ type) count))) + (count1 (expr->number info count1)) + (type (rank++ (make-c-array type (* %pointer-size count count1))))) (if (.function info) (local->info type name o init info) (global->info type name o init info)))) (_ (error "init-declr->info: not supported: " o)))) diff --git a/scaffold/tests/85-sizeof.c b/scaffold/tests/85-sizeof.c index 12d58e55..7cb552bb 100644 --- a/scaffold/tests/85-sizeof.c +++ b/scaffold/tests/85-sizeof.c @@ -18,10 +18,19 @@ * along with Mes. If not, see . */ +struct foo +{ + int length; + char buf[16]; +}; + int main () { char **p; - if (sizeof (*p) != 4) return 2; - return sizeof (**p) - 1; + if (sizeof (*p) != 4) return 1; + if (sizeof (**p) != 1) return 2; + puts ("size: "); puts (itoa (sizeof (struct foo))); puts ("\n"); + if (sizeof (struct foo) != 20) return 3; + return 0; }