scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit cdd599b1db41f2983c446c0fdefb799afbb511eb
parent 42441dfa818cce4c67092948f8113fd1afef41ab
Author: Roberto E. Vargas Caballero <k0ga@shike2.net>
Date:   Mon,  2 Feb 2026 12:37:24 +0100

cc1: Fix padding in initializers

The alignment calculation was wrong and it generated static struct and arrays
with data misplaced.

Diffstat:
Msrc/cmd/scc-cc/cc1/code.c | 17+++++++++--------
Atests/cc/execute/0238-align.c | 20++++++++++++++++++++
Mtests/cc/execute/scc-tests.lst | 3++-
3 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/src/cmd/scc-cc/cc1/code.c b/src/cmd/scc-cc/cc1/code.c @@ -353,18 +353,19 @@ zeronode(Type *tp) return simplify(convert(constnode(zero), tp, 0)); } -static int +static void emitpadding(Type *tp, unsigned long long *addr) { - int i; - unsigned long long n; + int i, align; + unsigned long long a, n; - n = *addr & tp->align-1; - for (i = 0; i < n; i++) - emitexp(OEXPR, zeronode(chartype)); - *addr += n; + align = tp->align - 1; + a = *addr; + n = a+align & ~align; - return n; + for ( ; a != n; ++a) + emitexp(OEXPR, zeronode(chartype)); + *addr = n; } static void diff --git a/tests/cc/execute/0238-align.c b/tests/cc/execute/0238-align.c @@ -0,0 +1,20 @@ +struct a { + char c; + int i; +}; + +int +main(void) +{ + struct a s[2] = { + {.c = 64, .i = 3}, + {.c = 64, .i = 6} + }; + + if (s[0].i != 3) + return 1; + if (s[1].i != 6) + return 2; + + return 0; +} diff --git a/tests/cc/execute/scc-tests.lst b/tests/cc/execute/scc-tests.lst @@ -208,7 +208,7 @@ 0215-ret_struct.c 0216-initialize.c [TODO] 0217-lchar.c -0218-initialize.c [TODO] +0218-initialize.c 0219-abbrev.c 0220-comma.c 0221-ifdef.c @@ -228,3 +228,4 @@ 0235-comment.c 0236-sizeof.c 0237-down.c +0238-align.c