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:
@@ -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;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user