From 8ec7e1225b783f66f6c233918a64d598fa392611 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Mon, 17 Jul 2017 07:54:48 +0200 Subject: [PATCH] mescc: Tinycc support: sizeof struct field. * module/language/c99/compiler.mes (expr->accu): Sizeof struct field. * scaffold/tests/72-typedef-struct-def.c (test): Test it. --- module/language/c99/compiler.mes | 12 ++++++++++++ scaffold/tests/72-typedef-struct-def.c | 2 ++ 2 files changed, 14 insertions(+) diff --git a/module/language/c99/compiler.mes b/module/language/c99/compiler.mes index cf0c56ea..b74be83a 100644 --- a/module/language/c99/compiler.mes +++ b/module/language/c99/compiler.mes @@ -511,6 +511,11 @@ (size (ast-type->size info type))) (append-text info (wrap-as (i386:value->accu size))))) + ((sizeof-expr (i-sel (ident ,field) (p-expr (ident ,array)))) + (let* ((type (ident->type info array)) + (size (field-size info type field))) + (append-text info (wrap-as (i386:value->accu size))))) + ((sizeof-type (type-name (decl-spec-list (type-spec (fixed-type ,name))))) (let* ((type name) (size (ast-type->size info type))) @@ -1040,6 +1045,13 @@ (prefix (and=> (member field (reverse fields) (lambda (a b) (equal? a (car b)))) cdr))) (apply + (map field:size prefix)))))) +(define (field-size info struct field) + (let ((xtype (ast-type->type info struct))) + (if (eq? (type:type xtype) 'union) 0 + (let* ((fields (type:description xtype)) + (field (and=> (member field fields (lambda (a b) (equal? a (car b)))) car))) + (field:size field))))) + (define (ast->type o) (pmatch o ((fixed-type ,type) diff --git a/scaffold/tests/72-typedef-struct-def.c b/scaffold/tests/72-typedef-struct-def.c index 73ff7356..b6565cc7 100644 --- a/scaffold/tests/72-typedef-struct-def.c +++ b/scaffold/tests/72-typedef-struct-def.c @@ -54,5 +54,7 @@ test () (*pp)->i = 3; printf ("(*pp)->i=%d\n", b.i); + printf ("sizeof i:%d\n", sizeof (p->i)); + return 0; }