scc

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

commit 7921f9b553bc19ed3dc6db7394d1bdea20502b0e
parent 30e0a933714d65ec7702aa10e11b16bec4ce69f2
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon, 28 Mar 2022 14:42:30 +0200

cc1: Use alignment in the offset calculation

The function typesize() was updating the base offset of
every field, but it was not considering the alignment
for the calculation of the offset. The alignment was
still considered for the actual size of the type, and
since code.c was emitting padding bytes when the alignment
was not correct the emited layout was correct, but the
definition of the tyoe was not correct. Instructions
using the offset of the field were not taking in account
the offset added due to the padding bytes. This error
was detected by the test 0050-inits.c.

Diffstat:
Msrc/cmd/cc/cc1/types.c | 7++++---
1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/cmd/cc/cc1/types.c b/src/cmd/cc/cc1/types.c @@ -206,17 +206,18 @@ typesize(Type *tp) offset = align = size = 0; n = tp->n.elem; for (sp = tp->p.fields; n--; ++sp) { - (*sp)->u.i = offset; type = (*sp)->type; a = type->align; if (a > align) align = a; if (tp->op == STRUCT) { if (--a != 0) - size = (size + a) & ~a; - size += type->size; + offset = (offset + a) & ~a; + (*sp)->u.i = offset; + size = offset + type->size; offset = size; } else { + (*sp)->u.i = 0; if (type->size > size) size = type->size; }