From 2611a64b2e3387627e9199456c768dee754e447d Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sun, 21 Jul 2019 22:40:42 +0200 Subject: [PATCH] mescc: Add 70-struct-post.c test with fix. * lib/tests/scaffold/70-struct-post.c: New file. * build-aux/check-mescc.sh (TESTS): Add it. * module/mescc/compile.scm (expr->register): Use type size for rank 0 (WAS: 1). --- build-aux/check-mescc.sh | 1 + lib/tests/scaffold/70-struct-post.c | 40 +++++++++++++++++++++++++++++ module/mescc/compile.scm | 8 ++++-- 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 lib/tests/scaffold/70-struct-post.c diff --git a/build-aux/check-mescc.sh b/build-aux/check-mescc.sh index cdd5f060..0a3cb882 100755 --- a/build-aux/check-mescc.sh +++ b/build-aux/check-mescc.sh @@ -182,6 +182,7 @@ lib/tests/scaffold/7u-struct-size10.c lib/tests/scaffold/7u-vstack.c lib/tests/scaffold/70-array-in-struct-init.c lib/tests/scaffold/70-struct-short-enum-init.c +lib/tests/scaffold/70-struct-post.c lib/tests/setjmp/80-setjmp.c lib/tests/stdio/80-sscanf.c lib/tests/stdlib/80-qsort.c diff --git a/lib/tests/scaffold/70-struct-post.c b/lib/tests/scaffold/70-struct-post.c new file mode 100644 index 00000000..262c003c --- /dev/null +++ b/lib/tests/scaffold/70-struct-post.c @@ -0,0 +1,40 @@ +/* -*-comment-start: "//";comment-end:""-*- + * GNU Mes --- Maxwell Equations of Software + * Copyright © 2019 Jan (janneke) Nieuwenhuizen + * + * This file is part of GNU Mes. + * + * GNU 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. + * + * GNU 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 GNU Mes. If not, see . + */ + +#include + +struct foo +{ + int bar; +}; + +int +main () +{ + struct foo f; + struct foo *p = &f; + //eputs ("p-f="); eputs (itoa (p-&f)); eputs ("\n"); + int i = 22; + *p++ = f; + //eputs ("p-f="); eputs (itoa (p-&f)); eputs ("\n"); + if (p - &f != 1) + return 1; + return 0; +} diff --git a/module/mescc/compile.scm b/module/mescc/compile.scm index 4a5907ea..11e3d94c 100644 --- a/module/mescc/compile.scm +++ b/module/mescc/compile.scm @@ -1314,7 +1314,9 @@ (type (ident->type info name)) (rank (ident->rank info name)) (reg-size (->size "*" info)) - (size (if (> rank 1) reg-size 1))) + (size (cond ((= rank 1) (ast-type->size info `(p-expr (ident ,name)))) + ((> rank 1) reg-size) + (else 1)))) (append-text info ((ident-add info) name size)))) ((assn-expr (de-ref (post-dec (p-expr (ident ,name)))) (op ,op) ,b) @@ -1322,7 +1324,9 @@ (type (ident->type info name)) (rank (ident->rank info name)) (reg-size (->size "*" info)) - (size (if (> rank 1) reg-size 1))) + (size (cond ((= rank 1) (ast-type->size info `(p-expr (ident ,name)))) + ((> rank 1) reg-size) + (else 1)))) (append-text info ((ident-add info) name (- size))))) ((assn-expr ,a (op ,op) ,b)