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

@@ -8656,6 +8656,152 @@ 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;
};
};
// MODULE: wcc
// selfhost/cmd/wcc/cgenexpr.ww — split out of cgen.ww.
//
@@ -14149,67 +14295,18 @@ fn cgassign(c: *cgen, n: *node) void = {
if (n.op == tkind.TK_ASSIGN) {
if (n.rhs != nil
&& n.rhs.kind == nkind.N_STRUCTLIT) {
let lcsz: i32 = lcnsz;
if (n.rhs.op == tkind.TK_ELLIPSIS) {
emitline("\tXORQ\tAX, AX\n");
let zi: i32 = 0;
for (zi + 8 <= lcsz) {
emitline("\tMOVQ\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 8;
};
for (zi + 4 <= lcsz) {
emitline("\tMOVL\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 4;
};
for (zi < lcsz) {
emitline("\tMOVB\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 1;
};
};
let fldn: *node = n.rhs.list;
for (fldn != nil) {
if (fldn.kind == nkind.N_FIELD) {
let fnm: str = fldn.str;
let fi: *fieldinfo = lcsi.fields;
for (fi != nil) {
if (streq(fi.fname, fnm)) {
cgexpr(c, fldn.lhs);
if (isfloattype(c, fi.tnode)) {
let mov: str = "MOVSD";
if (isf32type(c, fi.tnode)) {
mov = "MOVSS";
};
emitline("\t");
emitline(mov);
emitline("\tX0, ");
emitoff((off + fi.foff): i64);
emitline("(BP)\n");
fi = nil;
} else {
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((off + fi.foff): i64);
emitline("(BP)\n");
fi = nil;
};
} else {
fi = fi.finext;
};
};
};
fldn = fldn.next;
};
// Delegate to the shared BP-relative
// structlit fill helper. Handles
// TK_ELLIPSIS autofill + per-field
// walk; nested struct-typed values
// recurse via the helper (#17 fix).
// Helper uses the explicit {1→MOVB,
// 4→MOVL, else MOVQ} sized-store
// dispatch (NOT fieldstoreop) to stay
// byte-identical with cstage pending
// #13 (fsz==2 MOVW divergence). See
// cgstructlitfillbp docstring.
cgstructlitfillbp(c, lcsi, n.rhs, off);
return;
};
if (n.rhs != nil
@@ -14677,48 +14774,14 @@ fn cgreturn(c: *cgen, n: *node) void = {
emitoff((scroff + 16): i64);
emitline("(BP)\n");
if (rhs.kind == nkind.N_STRUCTLIT) {
let fn_: *node = rhs.list;
for (fn_ != nil) {
if (fn_.kind == nkind.N_FIELD) {
let fi: *fieldinfo = rsi.fields;
for (fi != nil) {
if (streq(fi.fname, fn_.str)) {
if (istaggedtype(c, fi.tnode)) {
cgwidentaggedstore(c,
fi.tnode, fn_.lhs,
"BP",
scroff + fi.foff,
fi.fsz);
fi = nil;
} else {
cgexpr(c, fn_.lhs);
if (isfloattype(c, fi.tnode)) {
let mov: str = "MOVSD";
if (isf32type(c, fi.tnode)) {
mov = "MOVSS";
};
emitline("\t");
emitline(mov);
emitline("\tX0, ");
emitoff((scroff + fi.foff): i64);
emitline("(BP)\n");
} else {
let sop: str = fieldstoreop(c, fi);
emitline("\t");
emitline(sop);
emitline("\tAX, ");
emitoff((scroff + fi.foff): i64);
emitline("(BP)\n");
};
fi = nil;
};
} else {
fi = fi.finext;
};
};
};
fn_ = fn_.next;
};
// Delegate to the shared BP-relative
// structlit fill helper. Same store
// sequence the inline pre-#17 walk
// emitted (tagged + float + scalar),
// plus nested struct-typed structlit
// values recurse instead of dropping
// trailing bytes.
cgstructlitfillbp(c, rsi, rhs, scroff);
} else {
// N_IDENT: word-copy from rhs slot
// to scratch. Whole 8B words via
@@ -14951,12 +15014,11 @@ fn cglet(c: *cgen, n: *node) void = {
return;
};
// Struct literal init: `let p: point = point{x=..., y=...};`.
// For each field in the lit, evaluate its value and store at
// the field's offset within the slot. Field-name → offset
// from the struct registry. When the literal carries
// op == tkind.TK_ELLIPSIS (autofill marker from the parser), the
// entire slot is zero-filled first so unmentioned fields
// read as 0.
// Delegates to the shared cgstructlitfillbp helper: TK_ELLIPSIS
// autofill + per-field walk, with nested struct-typed structlit
// values recursing into the helper instead of landing only AX
// (the #17 silent-zero fix). Mirror of cstage cgen.c N_LET
// structlit branch.
if (rhs.kind == nkind.N_STRUCTLIT) {
let trefn: *node = rhs.lhs;
let sname: str;
@@ -14967,65 +15029,7 @@ fn cglet(c: *cgen, n: *node) void = {
};
let si: *structinfo = structlookup(c, sname);
if (si != nil) {
if (rhs.op == tkind.TK_ELLIPSIS) {
let total: i32 = si.totsize;
emitline("\tXORQ\tAX, AX\n");
let zi: i32 = 0;
for (zi + 8 <= total) {
emitline("\tMOVQ\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 8;
};
for (zi + 4 <= total) {
emitline("\tMOVL\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 4;
};
for (zi < total) {
emitline("\tMOVB\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 1;
};
};
let fieldnode: *node = rhs.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)) {
cgexpr(c, fieldnode.lhs);
// f64/f32 struct-literal field init: cgexpr left
// the value in X0, store via MOVSD/MOVSS.
if (isfloattype(c, fi.tnode)) {
let mov: str = "MOVSD";
if (isf32type(c, fi.tnode)) { mov = "MOVSS"; };
emitline("\t");
emitline(mov);
emitline("\tX0, ");
emitoff((off + fi.foff): i64);
emitline("(BP)\n");
fi = nil;
} else {
let sop: str = fieldstoreop(c, fi);
emitline("\t");
emitline(sop);
emitline("\tAX, ");
emitoff((off + fi.foff): i64);
emitline("(BP)\n");
fi = nil;
};
} else {
fi = fi.finext;
};
};
};
fieldnode = fieldnode.next;
};
cgstructlitfillbp(c, si, rhs, off);
c.lastwasreturn = 0;
return;
};

View File

@@ -5490,67 +5490,18 @@ fn cgassign(c: *cgen, n: *node) void = {
if (n.op == tkind.TK_ASSIGN) {
if (n.rhs != nil
&& n.rhs.kind == nkind.N_STRUCTLIT) {
let lcsz: i32 = lcnsz;
if (n.rhs.op == tkind.TK_ELLIPSIS) {
emitline("\tXORQ\tAX, AX\n");
let zi: i32 = 0;
for (zi + 8 <= lcsz) {
emitline("\tMOVQ\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 8;
};
for (zi + 4 <= lcsz) {
emitline("\tMOVL\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 4;
};
for (zi < lcsz) {
emitline("\tMOVB\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 1;
};
};
let fldn: *node = n.rhs.list;
for (fldn != nil) {
if (fldn.kind == nkind.N_FIELD) {
let fnm: str = fldn.str;
let fi: *fieldinfo = lcsi.fields;
for (fi != nil) {
if (streq(fi.fname, fnm)) {
cgexpr(c, fldn.lhs);
if (isfloattype(c, fi.tnode)) {
let mov: str = "MOVSD";
if (isf32type(c, fi.tnode)) {
mov = "MOVSS";
};
emitline("\t");
emitline(mov);
emitline("\tX0, ");
emitoff((off + fi.foff): i64);
emitline("(BP)\n");
fi = nil;
} else {
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((off + fi.foff): i64);
emitline("(BP)\n");
fi = nil;
};
} else {
fi = fi.finext;
};
};
};
fldn = fldn.next;
};
// Delegate to the shared BP-relative
// structlit fill helper. Handles
// TK_ELLIPSIS autofill + per-field
// walk; nested struct-typed values
// recurse via the helper (#17 fix).
// Helper uses the explicit {1→MOVB,
// 4→MOVL, else MOVQ} sized-store
// dispatch (NOT fieldstoreop) to stay
// byte-identical with cstage pending
// #13 (fsz==2 MOVW divergence). See
// cgstructlitfillbp docstring.
cgstructlitfillbp(c, lcsi, n.rhs, off);
return;
};
if (n.rhs != nil

View File

@@ -295,48 +295,14 @@ fn cgreturn(c: *cgen, n: *node) void = {
emitoff((scroff + 16): i64);
emitline("(BP)\n");
if (rhs.kind == nkind.N_STRUCTLIT) {
let fn_: *node = rhs.list;
for (fn_ != nil) {
if (fn_.kind == nkind.N_FIELD) {
let fi: *fieldinfo = rsi.fields;
for (fi != nil) {
if (streq(fi.fname, fn_.str)) {
if (istaggedtype(c, fi.tnode)) {
cgwidentaggedstore(c,
fi.tnode, fn_.lhs,
"BP",
scroff + fi.foff,
fi.fsz);
fi = nil;
} else {
cgexpr(c, fn_.lhs);
if (isfloattype(c, fi.tnode)) {
let mov: str = "MOVSD";
if (isf32type(c, fi.tnode)) {
mov = "MOVSS";
};
emitline("\t");
emitline(mov);
emitline("\tX0, ");
emitoff((scroff + fi.foff): i64);
emitline("(BP)\n");
} else {
let sop: str = fieldstoreop(c, fi);
emitline("\t");
emitline(sop);
emitline("\tAX, ");
emitoff((scroff + fi.foff): i64);
emitline("(BP)\n");
};
fi = nil;
};
} else {
fi = fi.finext;
};
};
};
fn_ = fn_.next;
};
// Delegate to the shared BP-relative
// structlit fill helper. Same store
// sequence the inline pre-#17 walk
// emitted (tagged + float + scalar),
// plus nested struct-typed structlit
// values recurse instead of dropping
// trailing bytes.
cgstructlitfillbp(c, rsi, rhs, scroff);
} else {
// N_IDENT: word-copy from rhs slot
// to scratch. Whole 8B words via
@@ -569,12 +535,11 @@ fn cglet(c: *cgen, n: *node) void = {
return;
};
// Struct literal init: `let p: point = point{x=..., y=...};`.
// For each field in the lit, evaluate its value and store at
// the field's offset within the slot. Field-name → offset
// from the struct registry. When the literal carries
// op == tkind.TK_ELLIPSIS (autofill marker from the parser), the
// entire slot is zero-filled first so unmentioned fields
// read as 0.
// Delegates to the shared cgstructlitfillbp helper: TK_ELLIPSIS
// autofill + per-field walk, with nested struct-typed structlit
// values recursing into the helper instead of landing only AX
// (the #17 silent-zero fix). Mirror of cstage cgen.c N_LET
// structlit branch.
if (rhs.kind == nkind.N_STRUCTLIT) {
let trefn: *node = rhs.lhs;
let sname: str;
@@ -585,65 +550,7 @@ fn cglet(c: *cgen, n: *node) void = {
};
let si: *structinfo = structlookup(c, sname);
if (si != nil) {
if (rhs.op == tkind.TK_ELLIPSIS) {
let total: i32 = si.totsize;
emitline("\tXORQ\tAX, AX\n");
let zi: i32 = 0;
for (zi + 8 <= total) {
emitline("\tMOVQ\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 8;
};
for (zi + 4 <= total) {
emitline("\tMOVL\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 4;
};
for (zi < total) {
emitline("\tMOVB\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 1;
};
};
let fieldnode: *node = rhs.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)) {
cgexpr(c, fieldnode.lhs);
// f64/f32 struct-literal field init: cgexpr left
// the value in X0, store via MOVSD/MOVSS.
if (isfloattype(c, fi.tnode)) {
let mov: str = "MOVSD";
if (isf32type(c, fi.tnode)) { mov = "MOVSS"; };
emitline("\t");
emitline(mov);
emitline("\tX0, ");
emitoff((off + fi.foff): i64);
emitline("(BP)\n");
fi = nil;
} else {
let sop: str = fieldstoreop(c, fi);
emitline("\t");
emitline(sop);
emitline("\tAX, ");
emitoff((off + fi.foff): i64);
emitline("(BP)\n");
fi = nil;
};
} else {
fi = fi.finext;
};
};
};
fieldnode = fieldnode.next;
};
cgstructlitfillbp(c, si, rhs, off);
c.lastwasreturn = 0;
return;
};

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;
};
};

View File

@@ -8656,6 +8656,152 @@ 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;
};
};
// MODULE: wcc
// selfhost/cmd/wcc/cgenexpr.ww — split out of cgen.ww.
//
@@ -14149,67 +14295,18 @@ fn cgassign(c: *cgen, n: *node) void = {
if (n.op == tkind.TK_ASSIGN) {
if (n.rhs != nil
&& n.rhs.kind == nkind.N_STRUCTLIT) {
let lcsz: i32 = lcnsz;
if (n.rhs.op == tkind.TK_ELLIPSIS) {
emitline("\tXORQ\tAX, AX\n");
let zi: i32 = 0;
for (zi + 8 <= lcsz) {
emitline("\tMOVQ\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 8;
};
for (zi + 4 <= lcsz) {
emitline("\tMOVL\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 4;
};
for (zi < lcsz) {
emitline("\tMOVB\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 1;
};
};
let fldn: *node = n.rhs.list;
for (fldn != nil) {
if (fldn.kind == nkind.N_FIELD) {
let fnm: str = fldn.str;
let fi: *fieldinfo = lcsi.fields;
for (fi != nil) {
if (streq(fi.fname, fnm)) {
cgexpr(c, fldn.lhs);
if (isfloattype(c, fi.tnode)) {
let mov: str = "MOVSD";
if (isf32type(c, fi.tnode)) {
mov = "MOVSS";
};
emitline("\t");
emitline(mov);
emitline("\tX0, ");
emitoff((off + fi.foff): i64);
emitline("(BP)\n");
fi = nil;
} else {
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((off + fi.foff): i64);
emitline("(BP)\n");
fi = nil;
};
} else {
fi = fi.finext;
};
};
};
fldn = fldn.next;
};
// Delegate to the shared BP-relative
// structlit fill helper. Handles
// TK_ELLIPSIS autofill + per-field
// walk; nested struct-typed values
// recurse via the helper (#17 fix).
// Helper uses the explicit {1→MOVB,
// 4→MOVL, else MOVQ} sized-store
// dispatch (NOT fieldstoreop) to stay
// byte-identical with cstage pending
// #13 (fsz==2 MOVW divergence). See
// cgstructlitfillbp docstring.
cgstructlitfillbp(c, lcsi, n.rhs, off);
return;
};
if (n.rhs != nil
@@ -14677,48 +14774,14 @@ fn cgreturn(c: *cgen, n: *node) void = {
emitoff((scroff + 16): i64);
emitline("(BP)\n");
if (rhs.kind == nkind.N_STRUCTLIT) {
let fn_: *node = rhs.list;
for (fn_ != nil) {
if (fn_.kind == nkind.N_FIELD) {
let fi: *fieldinfo = rsi.fields;
for (fi != nil) {
if (streq(fi.fname, fn_.str)) {
if (istaggedtype(c, fi.tnode)) {
cgwidentaggedstore(c,
fi.tnode, fn_.lhs,
"BP",
scroff + fi.foff,
fi.fsz);
fi = nil;
} else {
cgexpr(c, fn_.lhs);
if (isfloattype(c, fi.tnode)) {
let mov: str = "MOVSD";
if (isf32type(c, fi.tnode)) {
mov = "MOVSS";
};
emitline("\t");
emitline(mov);
emitline("\tX0, ");
emitoff((scroff + fi.foff): i64);
emitline("(BP)\n");
} else {
let sop: str = fieldstoreop(c, fi);
emitline("\t");
emitline(sop);
emitline("\tAX, ");
emitoff((scroff + fi.foff): i64);
emitline("(BP)\n");
};
fi = nil;
};
} else {
fi = fi.finext;
};
};
};
fn_ = fn_.next;
};
// Delegate to the shared BP-relative
// structlit fill helper. Same store
// sequence the inline pre-#17 walk
// emitted (tagged + float + scalar),
// plus nested struct-typed structlit
// values recurse instead of dropping
// trailing bytes.
cgstructlitfillbp(c, rsi, rhs, scroff);
} else {
// N_IDENT: word-copy from rhs slot
// to scratch. Whole 8B words via
@@ -14951,12 +15014,11 @@ fn cglet(c: *cgen, n: *node) void = {
return;
};
// Struct literal init: `let p: point = point{x=..., y=...};`.
// For each field in the lit, evaluate its value and store at
// the field's offset within the slot. Field-name → offset
// from the struct registry. When the literal carries
// op == tkind.TK_ELLIPSIS (autofill marker from the parser), the
// entire slot is zero-filled first so unmentioned fields
// read as 0.
// Delegates to the shared cgstructlitfillbp helper: TK_ELLIPSIS
// autofill + per-field walk, with nested struct-typed structlit
// values recursing into the helper instead of landing only AX
// (the #17 silent-zero fix). Mirror of cstage cgen.c N_LET
// structlit branch.
if (rhs.kind == nkind.N_STRUCTLIT) {
let trefn: *node = rhs.lhs;
let sname: str;
@@ -14967,65 +15029,7 @@ fn cglet(c: *cgen, n: *node) void = {
};
let si: *structinfo = structlookup(c, sname);
if (si != nil) {
if (rhs.op == tkind.TK_ELLIPSIS) {
let total: i32 = si.totsize;
emitline("\tXORQ\tAX, AX\n");
let zi: i32 = 0;
for (zi + 8 <= total) {
emitline("\tMOVQ\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 8;
};
for (zi + 4 <= total) {
emitline("\tMOVL\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 4;
};
for (zi < total) {
emitline("\tMOVB\tAX, ");
emitoff((off + zi): i64);
emitline("(BP)\n");
zi += 1;
};
};
let fieldnode: *node = rhs.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)) {
cgexpr(c, fieldnode.lhs);
// f64/f32 struct-literal field init: cgexpr left
// the value in X0, store via MOVSD/MOVSS.
if (isfloattype(c, fi.tnode)) {
let mov: str = "MOVSD";
if (isf32type(c, fi.tnode)) { mov = "MOVSS"; };
emitline("\t");
emitline(mov);
emitline("\tX0, ");
emitoff((off + fi.foff): i64);
emitline("(BP)\n");
fi = nil;
} else {
let sop: str = fieldstoreop(c, fi);
emitline("\t");
emitline(sop);
emitline("\tAX, ");
emitoff((off + fi.foff): i64);
emitline("(BP)\n");
fi = nil;
};
} else {
fi = fi.finext;
};
};
};
fieldnode = fieldnode.next;
};
cgstructlitfillbp(c, si, rhs, off);
c.lastwasreturn = 0;
return;
};