cstage+selfhost+test: fix nested STRUCTLIT silent zero in BP-relative fills

Pre-existing landmine surfaced by #5. For a struct literal whose
field value is itself an N_STRUCTLIT of a struct-typed field, the
inline field-walk did `cgexpr(field.lhs); store-AX-sized`. cgexpr
has no whole-struct-in-register convention, so the nested literal
landed AX = first qword and the trailing bytes silently stayed zero
(or stack garbage). Three BP-relative sites in each stage hit it:
N_LET, N_ASSIGN N_IDENT-lhs, and N_RETURN N_STRUCTLIT.

Fix: shared cg_structlit_fill_bp (cstage) / cgstructlitfillbp
(wwstage) helper handles TK_ELLIPSIS autofill, tagged-field
widening, float vs scalar store dispatch, AND recurses on
struct-typed N_STRUCTLIT field values at bp_off + field_off. All 3
sites in each stage now call the helper instead of the inline walk.

Scalar store dispatch is the explicit {1->MOVB, 4->MOVL, else MOVQ}
shape (not fieldstoreop, which would emit MOVW for fsz==2) to stay
byte-identical with cstage pending task #13. Sister N_ASSIGN N_DOT
structlit walks (via_ptr / global / BP-relative-through-N_DOT) keep
their inline walk and still drop nested-STRUCTLIT silently — tracked
as task #18.

703 covers 6 rows: let_nested_i64, let_nested_3deep, let_nested_i32,
let_nested_middle (i64; switch to i32 once #15 lands),
assign_ident_nested, return_nested. 995_self_rebuild byte-identity
preserved.
This commit is contained in:
2026-05-15 18:14:33 +09:00
parent c4347499ee
commit 9d03e02881
8 changed files with 932 additions and 689 deletions

View File

@@ -2783,3 +2783,149 @@ export fn dotchainresolve(c: *cgen, n: *node,
};
return false;
};
// cgstructlitfillbp — fill a struct-typed slot from an N_STRUCTLIT
// value into BP-relative memory at `bpoff`. Mirror of cstage cgen.c's
// cg_structlit_fill_bp. Used by cglet, cgreturn N_STRUCTLIT, and
// cgassign N_IDENT-lhs N_STRUCTLIT sites where the dst is BP-
// relative.
//
// The inline field-walk previously did `cgexpr(field.lhs); store AX
// sized`. For struct-typed fields whose value is itself a nested
// N_STRUCTLIT, cgexpr has no whole-struct-in-register convention —
// it lands AX = first qword and the trailing bytes silently stay
// zero. Selfhost source used write-by-field as a workaround; this
// helper recurses cleanly so the workaround is no longer required.
//
// Sister branches (cgassign single-dot / via_ptr / global structlit
// walks) keep their inline field-walk in v1 since their addressing
// has additional BX-reload concerns; nested-STRUCTLIT silent-zero
// still drops on those paths — separate follow-up task.
//
// Output is byte-identical to the prior inline code for any input
// that has no nested-STRUCTLIT field (the only shape selfhost source
// actually compiles today), preserving 995_self_rebuild byte-
// identity.
//
// Graduation note (task #13): the scalar store currently uses the
// explicit {1→MOVB, 4→MOVL, else MOVQ} dispatch to match cstage
// byte-identically — cstage hasn't yet learned MOVW for fsz==2. Once
// #13 aligns both stages, the dispatch can switch to fieldstoreop
// which already returns MOVW where appropriate.
fn cgstructlitfillbp(c: *cgen, si: *structinfo, lit: *node, bpoff: i32) void = {
if (si == nil) { return; };
if (lit.op == tkind.TK_ELLIPSIS) {
// `..., ...` autofill — zero the entire slot first so
// unmentioned fields read as 0. Uses si.totsize (slot-padded)
// to match the inline pre-#17 pattern.
let total: i32 = si.totsize;
emitline("\tXORQ\tAX, AX\n");
let zi: i32 = 0;
for (zi + 8 <= total) {
emitline("\tMOVQ\tAX, ");
emitoff((bpoff + zi): i64);
emitline("(BP)\n");
zi += 8;
};
for (zi + 4 <= total) {
emitline("\tMOVL\tAX, ");
emitoff((bpoff + zi): i64);
emitline("(BP)\n");
zi += 4;
};
for (zi < total) {
emitline("\tMOVB\tAX, ");
emitoff((bpoff + zi): i64);
emitline("(BP)\n");
zi += 1;
};
};
let fieldnode: *node = lit.list;
for (fieldnode != nil) {
if (fieldnode.kind == nkind.N_FIELD) {
let fname: str = fieldnode.str;
let fi: *fieldinfo = si.fields;
for (fi != nil) {
let fn_: str = fi.fname;
if (streq(fn_, fname)) {
// Tagged-union field: delegate to the shared
// widening writer (handles str/scalar/struct
// literal/ident payload + tagged-subset tag
// remap). Mirrors cstage cg_structlit_fill_bp
// and the pre-existing wwstage cgreturn
// structlit dispatch; cglet's pre-#17 inline
// lacked this branch but selfhost never
// tripped it (no tagged-fields-in-let-
// structlit in tree).
if (istaggedtype(c, fi.tnode)) {
cgwidentaggedstore(c, fi.tnode,
fieldnode.lhs, "BP",
bpoff + fi.foff, fi.fsz);
fi = nil;
} else {
// Nested struct-typed structlit value: look up
// the inner struct's metadata and recurse at the
// field's offset. Pre-#17 the cgexpr-then-store
// below would land AX = first qword and the rest
// silently stayed zero.
let nested: bool = false;
if (fieldnode.lhs != nil) {
if (fieldnode.lhs.kind == nkind.N_STRUCTLIT) {
if (fi.tnode != nil) {
if (fi.tnode.kind == nkind.N_TNAME) {
if (primsize(fi.tnode.str) == 0) {
let isi: *structinfo = structlookup(c, fi.tnode.str);
if (isi != nil) {
cgstructlitfillbp(c, isi,
fieldnode.lhs,
bpoff + fi.foff);
nested = true;
};
};
};
};
};
};
if (nested) {
fi = nil;
} else {
cgexpr(c, fieldnode.lhs);
if (isfloattype(c, fi.tnode)) {
let mov: str = "MOVSD";
if (isf32type(c, fi.tnode)) { mov = "MOVSS"; };
emitline("\t");
emitline(mov);
emitline("\tX0, ");
emitoff((bpoff + fi.foff): i64);
emitline("(BP)\n");
fi = nil;
} else {
// Explicit {1→MOVB, 4→MOVL, else MOVQ}
// dispatch (not fieldstoreop) to match
// cstage cg_structlit_fill_bp byte-
// identically. wwstage's fieldstoreop
// would return MOVW for fsz==2 which
// cstage doesn't emit — tracked as task
// #13. Until that lands, the helper
// emits the cstage shape.
let fsz: i32 = fi.fsz;
let op: str = "MOVQ";
if (fsz == 1) { op = "MOVB"; };
if (fsz == 4) { op = "MOVL"; };
emitline("\t");
emitline(op);
emitline("\tAX, ");
emitoff((bpoff + fi.foff): i64);
emitline("(BP)\n");
fi = nil;
};
};
};
} else {
fi = fi.finext;
};
};
};
fieldnode = fieldnode.next;
};
};