shithub: scc

Download patch

ref: c5134537d2d2b0a7b7b2e9a08e5255c4b881cdbb
parent: c970c0f3b3510ff26976834a880977b5bdc1237d
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Mar 28 10:42:30 EDT 2022

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.

--- a/src/cmd/cc/cc1/types.c
+++ b/src/cmd/cc/cc1/types.c
@@ -206,7 +206,6 @@
 		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)
@@ -213,10 +212,12 @@
 				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;
 			}