cgen: size a struct-array local frame slot by round8(natural), not the slot-padded total (#9)

This commit is contained in:
2026-06-28 12:20:28 +09:00
parent d0ce55cab8
commit 0bf69140a2
2 changed files with 84 additions and 11 deletions

View File

@@ -2647,19 +2647,27 @@ fn slotsize(c: *cgen, typn: *syntax.node) i32 = {
kk == syntax.tykind.TY_STR || kk == syntax.tykind.TY_TAGGED) {
return ti.size: i32;
};
// #75: a struct LOCAL's stack slot is the struct's NATURAL size
// rounded up to the 8B slot grain — NOT ti.slotsize, which sums the
// slot-padded field widths and over-reserves on sub-8 nested
// composites (e.g. outer{a:u8, p:inner{x:u8,y:u8}, z:i64} → ww $32 vs
// cstage $16). cstage reserves the local at f->type->size (cmd/w6c/
// cgen.c), i.e. the checker's natural r.size (check.ww:2256). Same
// dual-SSoT leak as #44 (field-offset) / #55, one notion over. The
// TUPLE arm (8B/elem slot, user ruling #60) and ARRAY arm (element
// stride, #48 [N]Alias 24B) keep ti.slotsize — deliberate divergences.
if (kk == syntax.tykind.TY_STRUCT) {
// #75/#9: a struct OR array LOCAL's stack slot is the type's NATURAL
// size rounded to the 8B slot grain — NOT ti.slotsize, which sums the
// slot-PADDED element/field widths and over-reserves when a nested
// element is a sub-8-tail composite (e.g. [2]outer with
// outer{a:u8, p:inner{x:u8,y:u8}, z:i64}: outer.slotsize 24 != size 16
// → ww frame $48 vs cstage natural $32). cstage's localslot reserves at
// round8(f->type->size) (cmd/w6c/cgen.c, frame=(frame+size+7)&~7 over
// the checker's natural size); ww's localreserve mirrors that round.
// ti.size and ti.slotsize round to the SAME 8-multiple for every array
// of prims/arrays (element slotsize==size) and for an 8-multiple tagged
// element (#48 [N]Alias), so this arm MOVES only the nested-sub-8-struct
// case — the #9 divergence. #75 fixed the TY_STRUCT arm; #9 carries the
// identical transform to its TY_ARRAY sibling (same dual-SSoT slotsize
// leak as #44/#55, one notion over). The per-element STRIDE is
// unaffected: it reads slotsize on the ELEMENT node (struct arm) /
// elemsizeofc's natural sub.size, never this array-total arm. TUPLE
// keeps ti.slotsize (8B/elem slot, USER ruling #60 — untouched).
if (kk == syntax.tykind.TY_STRUCT || kk == syntax.tykind.TY_ARRAY) {
return ((ti.size + 7u64) & ~7u64): i32;
};
if (kk == syntax.tykind.TY_TUPLE || kk == syntax.tykind.TY_ARRAY) {
if (kk == syntax.tykind.TY_TUPLE) {
return ti.slotsize: i32;
};
return 8;