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:
304
cmd/w6c/cgen.c
304
cmd/w6c/cgen.c
@@ -864,6 +864,7 @@ static void cgstmt(Cg*, Node*, Local**, int*);
|
||||
static void cg_widen_tagged_push(Cg*, Local**, Type*, Node*, int);
|
||||
static void cg_widen_tagged_store(Cg*, Local**, Type*, Node*, int, int, int);
|
||||
static void cg_widen_tag_remap(Cg*, Type*, Type*, int);
|
||||
static void cg_structlit_fill_bp(Cg*, Local**, Type*, Node*, int);
|
||||
|
||||
static void
|
||||
cgexpr_int(Cg *c, long long v)
|
||||
@@ -1225,6 +1226,100 @@ cg_widen_tagged_push(Cg *c, Local **locals_p, Type *dst, Node *src, int sz)
|
||||
}
|
||||
}
|
||||
|
||||
/* cg_structlit_fill_bp — fill a struct-typed slot from an N_STRUCTLIT
|
||||
* value into BP-relative memory at `bp_off`. Used by N_LET, N_ASSIGN
|
||||
* N_IDENT-lhs, and N_RETURN N_STRUCTLIT sites where the dst is BP-
|
||||
* relative (BP-rel locals, or a return-side scratch slot).
|
||||
*
|
||||
* Why a helper? The inline field-walk at each call site previously
|
||||
* did `cgexpr(f->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 (or stack garbage). Selfhost
|
||||
* source uses write-by-field as a fixture-level workaround; with
|
||||
* this helper, nested literals recurse cleanly and the workaround
|
||||
* is no longer load-bearing.
|
||||
*
|
||||
* Sister branches (N_ASSIGN N_DOT-lhs structlit — via_ptr / global /
|
||||
* BP-relative) keep their inline field-walk for v1 since their
|
||||
* addressing has additional BX-reload concerns; nested-STRUCTLIT
|
||||
* silent-zero still drops on those paths — separate follow-up.
|
||||
*
|
||||
* Output is byte-identical to the prior inline code for any input
|
||||
* that has no nested-STRUCTLIT field (the only shape selfhost
|
||||
* actually compiles today), preserving 995_self_rebuild byte-
|
||||
* identity. */
|
||||
static void
|
||||
cg_structlit_fill_bp(Cg *c, Local **locals_p, Type *lu, Node *lit, int bp_off)
|
||||
{
|
||||
int sz = (int)lu->size;
|
||||
if (lit->op == TK_ELLIPSIS) {
|
||||
/* `..., ...` autofill — zero the entire slot first so
|
||||
* unmentioned fields read as 0. Sized stores: 8/4/1
|
||||
* (matches the inline pre-#17 pattern at N_LET / N_ASSIGN /
|
||||
* N_RETURN). */
|
||||
ins2(c, A_XORQ, areg(D_AX), areg(D_AX));
|
||||
int zi = 0;
|
||||
while (zi + 8 <= sz) {
|
||||
ins2(c, A_MOVQ, areg(D_AX),
|
||||
amem(D_BP, bp_off + zi));
|
||||
zi += 8;
|
||||
}
|
||||
while (zi + 4 <= sz) {
|
||||
ins2(c, A_MOVL, areg(D_AX),
|
||||
amem(D_BP, bp_off + zi));
|
||||
zi += 4;
|
||||
}
|
||||
while (zi < sz) {
|
||||
ins2(c, A_MOVB, areg(D_AX),
|
||||
amem(D_BP, bp_off + zi));
|
||||
zi += 1;
|
||||
}
|
||||
}
|
||||
for (Node *f = lit->list; f; f = f->next) {
|
||||
u64 foff = 0;
|
||||
int fsz = 8;
|
||||
Type *ft = NULL;
|
||||
for (Tfield *fl = lu->fields; fl; fl = fl->next) {
|
||||
if (strcmp(fl->name, f->str) == 0) {
|
||||
foff = fl->offset;
|
||||
fsz = (int)(fl->type ? fl->type->size : 8);
|
||||
ft = fl->type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Type *fu = (ft && ft->kind == TY_NAMED) ? ft->under : ft;
|
||||
if (fu && fu->kind == TY_TAGGED) {
|
||||
cg_widen_tagged_store(c, locals_p, fu, f->lhs,
|
||||
D_BP, bp_off + (int)foff, (int)fu->size);
|
||||
continue;
|
||||
}
|
||||
/* Nested struct-typed structlit value: recurse at the
|
||||
* field's offset so all inner fields land. Pre-#17 the
|
||||
* cgexpr-then-store below would land AX = first qword and
|
||||
* the rest silently stayed zero. */
|
||||
if (fu && fu->kind == TY_STRUCT
|
||||
&& f->lhs && f->lhs->kind == N_STRUCTLIT) {
|
||||
cg_structlit_fill_bp(c, locals_p, fu, f->lhs,
|
||||
bp_off + (int)foff);
|
||||
continue;
|
||||
}
|
||||
cgexpr(c, f->lhs, *locals_p);
|
||||
int sl_isf32 = 0;
|
||||
if (fld_isfloat(ft, &sl_isf32)) {
|
||||
int mov = sl_isf32 ? A_MOVSS : A_MOVSD;
|
||||
ins2(c, mov, areg(D_X0),
|
||||
amem(D_BP, bp_off + (int)foff));
|
||||
continue;
|
||||
}
|
||||
int op = A_MOVQ;
|
||||
if (fsz == 1) op = A_MOVB;
|
||||
else if (fsz == 4) op = A_MOVL;
|
||||
ins2(c, op, areg(D_AX),
|
||||
amem(D_BP, bp_off + (int)foff));
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
cgexpr(Cg *c, Node *n, Local *locals)
|
||||
{
|
||||
@@ -3454,76 +3549,14 @@ cgexpr(Cg *c, Node *n, Local *locals)
|
||||
if (off != 0) {
|
||||
int sz = (int)lu->size;
|
||||
if (n->rhs && n->rhs->kind == N_STRUCTLIT) {
|
||||
/* TK_ELLIPSIS autofill: zero-fill the
|
||||
* slot first so unmentioned fields read
|
||||
* as 0 (mirrors cglet's structlit). */
|
||||
if (n->rhs->op == TK_ELLIPSIS) {
|
||||
ins2(c, A_XORQ, areg(D_AX),
|
||||
areg(D_AX));
|
||||
int zi = 0;
|
||||
while (zi + 8 <= sz) {
|
||||
ins2(c, A_MOVQ, areg(D_AX),
|
||||
amem(D_BP, off + zi));
|
||||
zi += 8;
|
||||
}
|
||||
while (zi + 4 <= sz) {
|
||||
ins2(c, A_MOVL, areg(D_AX),
|
||||
amem(D_BP, off + zi));
|
||||
zi += 4;
|
||||
}
|
||||
while (zi < sz) {
|
||||
ins2(c, A_MOVB, areg(D_AX),
|
||||
amem(D_BP, off + zi));
|
||||
zi += 1;
|
||||
}
|
||||
}
|
||||
for (Node *f = n->rhs->list; f;
|
||||
f = f->next) {
|
||||
u64 foff = 0;
|
||||
int fsz = 8;
|
||||
Type *ft = NULL;
|
||||
for (Tfield *fl = lu->fields; fl;
|
||||
fl = fl->next) {
|
||||
if (strcmp(fl->name, f->str) == 0) {
|
||||
foff = fl->offset;
|
||||
fsz = (int)(fl->type
|
||||
? fl->type->size : 8);
|
||||
ft = fl->type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Tagged-union field: delegate to the
|
||||
* shared widening writer. Handles
|
||||
* str, scalar, struct literal/ident
|
||||
* payload, and tagged-subset
|
||||
* forwarding (with tag remap). */
|
||||
Type *fu = (ft
|
||||
&& ft->kind == TY_NAMED)
|
||||
? ft->under : ft;
|
||||
if (fu && fu->kind == TY_TAGGED) {
|
||||
cg_widen_tagged_store(c,
|
||||
&locals, fu, f->lhs,
|
||||
D_BP, off + (int)foff,
|
||||
(int)fu->size);
|
||||
continue;
|
||||
}
|
||||
cgexpr(c, f->lhs, locals);
|
||||
int sl_isf32 = 0;
|
||||
if (fld_isfloat(ft, &sl_isf32)) {
|
||||
int mov = sl_isf32
|
||||
? A_MOVSS : A_MOVSD;
|
||||
ins2(c, mov, areg(D_X0),
|
||||
amem(D_BP,
|
||||
off + (int)foff));
|
||||
continue;
|
||||
}
|
||||
int op = A_MOVQ;
|
||||
if (fsz == 1) op = A_MOVB;
|
||||
else if (fsz == 4) op = A_MOVL;
|
||||
ins2(c, op, areg(D_AX),
|
||||
amem(D_BP,
|
||||
off + (int)foff));
|
||||
}
|
||||
/* Delegate to the shared BP-relative
|
||||
* structlit fill helper. Handles
|
||||
* TK_ELLIPSIS autofill, tagged fields,
|
||||
* float/scalar stores, AND nested
|
||||
* struct-typed structlit values via
|
||||
* recursion (#17 silent-zero fix). */
|
||||
cg_structlit_fill_bp(c, &locals, lu,
|
||||
n->rhs, off);
|
||||
break;
|
||||
}
|
||||
if (n->rhs && n->rhs->kind == N_CALL
|
||||
@@ -5888,74 +5921,16 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
|
||||
ins2(c, A_MOVQ, areg(D_CX), amem(D_BP, off + 16));
|
||||
break;
|
||||
}
|
||||
/* struct literal initialiser: field-by-field store. The
|
||||
* literal carries op == TK_ELLIPSIS when the source ends in
|
||||
* `..., ...` — in that case zero-fill the entire slot first,
|
||||
* so unmentioned fields read as 0. */
|
||||
/* struct literal initialiser: field-by-field store via the
|
||||
* shared cg_structlit_fill_bp helper. The literal carries
|
||||
* op == TK_ELLIPSIS when the source ends in `..., ...` —
|
||||
* helper zero-fills the slot first so unmentioned fields
|
||||
* read as 0. Nested struct-typed structlit field values
|
||||
* recurse into the helper at the correct offset instead of
|
||||
* landing AX = first-qword via cgexpr (#17 silent zero). */
|
||||
if (n->rhs && n->rhs->kind == N_STRUCTLIT && lu
|
||||
&& lu->kind == TY_STRUCT) {
|
||||
if (n->rhs->op == TK_ELLIPSIS) {
|
||||
u64 sz = lu->size;
|
||||
/* AX = 0 once, then store from AX. w6a doesn't
|
||||
* accept MOVB imm,mem — use register stores. */
|
||||
ins2(c, A_XORQ, areg(D_AX), areg(D_AX));
|
||||
u64 i = 0;
|
||||
while (i + 8 <= sz) {
|
||||
ins2(c, A_MOVQ, areg(D_AX),
|
||||
amem(D_BP, off + (int)i));
|
||||
i += 8;
|
||||
}
|
||||
while (i + 4 <= sz) {
|
||||
ins2(c, A_MOVL, areg(D_AX),
|
||||
amem(D_BP, off + (int)i));
|
||||
i += 4;
|
||||
}
|
||||
while (i < sz) {
|
||||
ins2(c, A_MOVB, areg(D_AX),
|
||||
amem(D_BP, off + (int)i));
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
for (Node *f = n->rhs->list; f; f = f->next) {
|
||||
/* find offset of this field */
|
||||
u64 foff = 0;
|
||||
int fsz = 8;
|
||||
Type *ft = NULL;
|
||||
for (Tfield *fl = lu->fields; fl; fl = fl->next) {
|
||||
if (strcmp(fl->name, f->str) == 0) {
|
||||
foff = fl->offset;
|
||||
fsz = (int)(fl->type ? fl->type->size : 8);
|
||||
ft = fl->type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* Tagged-union field: delegate to the shared
|
||||
* widening writer. Handles str, scalar, struct
|
||||
* literal/ident payload, and tagged-subset
|
||||
* forwarding (with tag remap). The field's slot
|
||||
* starts at off+foff inside the struct slot. */
|
||||
Type *fu = (ft && ft->kind == TY_NAMED)
|
||||
? ft->under : ft;
|
||||
if (fu && fu->kind == TY_TAGGED) {
|
||||
cg_widen_tagged_store(c, locals, fu,
|
||||
f->lhs, D_BP, off + (int)foff,
|
||||
(int)fu->size);
|
||||
continue;
|
||||
}
|
||||
cgexpr(c, f->lhs, *locals);
|
||||
int sl_isf32 = 0;
|
||||
if (fld_isfloat(ft, &sl_isf32)) {
|
||||
int mov = sl_isf32 ? A_MOVSS : A_MOVSD;
|
||||
ins2(c, mov, areg(D_X0),
|
||||
amem(D_BP, off + (int)foff));
|
||||
continue;
|
||||
}
|
||||
int op = A_MOVQ;
|
||||
if (fsz == 1) op = A_MOVB;
|
||||
else if (fsz == 4) op = A_MOVL;
|
||||
ins2(c, op, areg(D_AX),
|
||||
amem(D_BP, off + (int)foff));
|
||||
}
|
||||
cg_structlit_fill_bp(c, locals, lu, n->rhs, off);
|
||||
break;
|
||||
}
|
||||
/* Whole-struct receive for sizes <=24B (call-result rhs).
|
||||
@@ -6224,55 +6199,14 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
|
||||
ins2(c, A_MOVQ, areg(D_AX),
|
||||
amem(D_BP, scr + 16));
|
||||
if (n->lhs->kind == N_STRUCTLIT) {
|
||||
for (Node *f = n->lhs->list; f;
|
||||
f = f->next) {
|
||||
u64 foff = 0;
|
||||
int fsz = 8;
|
||||
Type *ft = NULL;
|
||||
for (Tfield *fl = rt->fields;
|
||||
fl; fl = fl->next) {
|
||||
if (strcmp(fl->name,
|
||||
f->str) == 0) {
|
||||
foff = fl->offset;
|
||||
fsz = (int)(fl->type
|
||||
? fl->type->size
|
||||
: 8);
|
||||
ft = fl->type;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Type *fu = (ft && ft->kind
|
||||
== TY_NAMED)
|
||||
? ft->under : ft;
|
||||
if (fu && fu->kind
|
||||
== TY_TAGGED) {
|
||||
cg_widen_tagged_store(c,
|
||||
locals, fu, f->lhs,
|
||||
D_BP,
|
||||
scr + (int)foff,
|
||||
(int)fu->size);
|
||||
continue;
|
||||
}
|
||||
cgexpr(c, f->lhs, *locals);
|
||||
int sl_isf32 = 0;
|
||||
if (fld_isfloat(ft,
|
||||
&sl_isf32)) {
|
||||
int mov = sl_isf32
|
||||
? A_MOVSS
|
||||
: A_MOVSD;
|
||||
ins2(c, mov,
|
||||
areg(D_X0),
|
||||
amem(D_BP,
|
||||
scr + (int)foff));
|
||||
continue;
|
||||
}
|
||||
int op = A_MOVQ;
|
||||
if (fsz == 1) op = A_MOVB;
|
||||
else if (fsz == 4) op = A_MOVL;
|
||||
ins2(c, op, areg(D_AX),
|
||||
amem(D_BP,
|
||||
scr + (int)foff));
|
||||
}
|
||||
/* Delegate to the shared BP-relative
|
||||
* fill helper. Same store sequence the
|
||||
* inline pre-#17 walk emitted, plus
|
||||
* nested struct-typed structlit values
|
||||
* recurse instead of dropping the
|
||||
* trailing bytes. */
|
||||
cg_structlit_fill_bp(c, locals, rt,
|
||||
n->lhs, scr);
|
||||
} else {
|
||||
/* N_IDENT: word-copy rhs slot into
|
||||
* scratch. Whole 8B words via MOVQ;
|
||||
|
||||
Reference in New Issue
Block a user