cstage+selfhost+test: extend structlit-fill helper to N_ASSIGN N_DOT lhs (4 flavors)

Sister fix to #17. The BP-rel helper from #17 covered N_LET /
N_ASSIGN N_IDENT-lhs / N_RETURN; the four N_ASSIGN N_DOT-lhs
structlit walks still went through the inline `cgexpr(field.lhs);
store-AX-sized` shape and silently dropped trailing bytes when a
struct-typed field's value was itself an N_STRUCTLIT. Affected dot
flavors: single-dot via_ptr / global / BP-rel and the chained-dot
walker (depth >= 2, all three root flavors).

Extend `cg_structlit_fill_bp` / `cgstructlitfillbp` into
`cg_structlit_fill` / `cgstructlitfill` taking a destination mode
(DST_BP / DST_PTR_LOCAL / DST_GLOBAL = 0/1/2), srcoff (PTR_LOCAL),
srcname (GLOBAL), and disp accumulator. `disp` grows by foff on
descent; srcoff/srcname stay constant across the call tree. The
pre-#17 wrappers are preserved byte-identically by delegating with
mode=DST_BP — 995_self_rebuild byte-identity holds for the no-
nested-STRUCTLIT case that selfhost source actually uses.

The non-BP modes reload BX before the ELLIPSIS zero-fill loop AND
before every field store (tagged, scalar, and the cgexpr leaf).
This is correctness-by-construction — cgexpr clobbers BX between
fields, and the redundant reload only fires on shapes that didn't
compile before. The four dot-flavor sites in each stage now compute
their dst mode + disp and call the shared helper (reducing each
from ~80-130 inline lines to ~5-12 lines of dispatch).

Stage signature asymmetry: cstage threads Local** for cgexpr; ww-
stage takes explicit totsize because #15 (split totsize into
naturalsize + slotsize) is still pending and the dot sites need
structnaturalsize while the BP-rel sites need si.totsize. Both
asymmetries are documented in the helper docstrings.

704 covers 8 rows (24 checks: 8 cstage exits, 8 wwstage exits, 8
cstage-vs-wwstage .s byte-identity diffs): 6 dst-flavors (single-
dot local/ptr/global, chained-dot local/ptr/global) plus single-
local 3-deep and single-ptr 3-deep to pin disp threading through
the helper's recursion and through DST_PTR_LOCAL BX reloads.

The nested struct-typed CALL rhs in field-walks has the same shape
as the STRUCTLIT bug fixed here but the helper only handles
STRUCTLIT — tracked as task #20.
This commit is contained in:
2026-05-15 18:45:21 +09:00
parent 9d03e02881
commit 99a68a6a57
7 changed files with 1070 additions and 1450 deletions

View File

@@ -864,6 +864,13 @@ 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);
/* cg_structlit_fill modes — see helper docstring. */
enum {
DST_BP = 0,
DST_PTR_LOCAL = 1,
DST_GLOBAL = 2,
};
static void cg_structlit_fill(Cg*, Local**, Type*, Node*, int, int, const char*, int);
static void cg_structlit_fill_bp(Cg*, Local**, Type*, Node*, int);
static void
@@ -1226,53 +1233,81 @@ 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).
/* cg_structlit_fill — fill a struct-typed slot from an N_STRUCTLIT
* value into one of three destination flavors. Used by N_LET, N_ASSIGN
* N_IDENT-lhs, N_RETURN N_STRUCTLIT (BP-rel), and N_ASSIGN N_DOT-lhs
* (BP-rel / via *struct local / via struct global) at single-dot and
* chained-dot sites.
*
* Destination modes:
* DST_BP — base = BP, no reload. Stores at disp+i(BP).
* srcoff/name unused.
* DST_PTR_LOCAL — base = BX, reloaded from srcoff(BP) before the
* ELLIPSIS zero-fill loop and before EVERY field
* store (cgexpr clobbers BX between fields).
* Stores at disp+i(BX). name unused.
* DST_GLOBAL — base = BX, reloaded via `LEAQ name(SB), BX` with
* the same reload cadence as DST_PTR_LOCAL.
* srcoff unused.
*
* Param semantics (locked in here so the recursion contract is clear):
* - `disp` is the per-recursion accumulator — grows by `foff` as
* we descend into a nested struct-typed structlit field.
* - `srcoff` (DST_PTR_LOCAL) and `name` (DST_GLOBAL) are *constant*
* across the whole call tree — they identify the root dst, which
* doesn't change with depth. Recursion passes them through.
*
* 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.
* trailing bytes silently stay zero (or stack garbage). #17 fixed
* the BP-rel sites; #18 extends the same recursion to the four
* N_ASSIGN N_DOT-lhs structlit walks (single-dot via_ptr/global/
* local + chained depth>=2).
*
* 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.
* The non-BP modes emit a redundant BX reload at the start of each
* recursive nested zero-fill / each recursive scalar store — this is
* correctness-by-construction (BX is always freshly loaded right
* before use), and the redundancy only fires on the nested-STRUCTLIT
* shapes that didn't compile before. Byte-identity for the no-nested
* case (the only shape selfhost source uses today) is preserved
* because the existing inline code's reload-before-each-store pattern
* matches the helper's per-store reload exactly.
*
* 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. */
* The scalar store dispatch stays at the explicit {1->MOVB, 4->MOVL,
* else MOVQ} shape (not fieldstoreop, which emits MOVW for fsz==2) to
* stay byte-identical with cstage pending task #13. */
static void
cg_structlit_fill_bp(Cg *c, Local **locals_p, Type *lu, Node *lit, int bp_off)
cg_structlit_fill(Cg *c, Local **locals_p, Type *lu, Node *lit,
int mode, int srcoff, const char *name, int disp)
{
int sz = (int)lu->size;
int base_reg = (mode == DST_BP) ? D_BP : D_BX;
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). */
* unmentioned fields read as 0. Sized stores: 8/4/1. For
* non-BP modes, reload BX once before the loop (cgexpr-free
* region between iterations, so one reload is enough). */
ins2(c, A_XORQ, areg(D_AX), areg(D_AX));
if (mode == DST_PTR_LOCAL)
ins2(c, A_MOVQ, amem(D_BP, srcoff), areg(D_BX));
else if (mode == DST_GLOBAL)
ins2(c, A_LEAQ, masym(c, name), areg(D_BX));
int zi = 0;
while (zi + 8 <= sz) {
ins2(c, A_MOVQ, areg(D_AX),
amem(D_BP, bp_off + zi));
amem(base_reg, disp + zi));
zi += 8;
}
while (zi + 4 <= sz) {
ins2(c, A_MOVL, areg(D_AX),
amem(D_BP, bp_off + zi));
amem(base_reg, disp + zi));
zi += 4;
}
while (zi < sz) {
ins2(c, A_MOVB, areg(D_AX),
amem(D_BP, bp_off + zi));
amem(base_reg, disp + zi));
zi += 1;
}
}
@@ -1290,36 +1325,59 @@ cg_structlit_fill_bp(Cg *c, Local **locals_p, Type *lu, Node *lit, int bp_off)
}
Type *fu = (ft && ft->kind == TY_NAMED) ? ft->under : ft;
if (fu && fu->kind == TY_TAGGED) {
/* Tagged store: reload BX first (if non-BP) so the
* widener sees a valid base reg. The widener itself
* preserves base_reg through its internal cgexpr. */
if (mode == DST_PTR_LOCAL)
ins2(c, A_MOVQ, amem(D_BP, srcoff),
areg(D_BX));
else if (mode == DST_GLOBAL)
ins2(c, A_LEAQ, masym(c, name), areg(D_BX));
cg_widen_tagged_store(c, locals_p, fu, f->lhs,
D_BP, bp_off + (int)foff, (int)fu->size);
base_reg, disp + (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
* field's offset so all inner fields land. Pre-#17/#18 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);
cg_structlit_fill(c, locals_p, fu, f->lhs,
mode, srcoff, name, disp + (int)foff);
continue;
}
cgexpr(c, f->lhs, *locals_p);
/* For non-BP modes, cgexpr just clobbered BX; reload it
* before the store. */
if (mode == DST_PTR_LOCAL)
ins2(c, A_MOVQ, amem(D_BP, srcoff), areg(D_BX));
else if (mode == DST_GLOBAL)
ins2(c, A_LEAQ, masym(c, name), areg(D_BX));
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));
amem(base_reg, disp + (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));
amem(base_reg, disp + (int)foff));
}
}
/* Thin wrapper preserving the BP-rel call shape used by N_LET,
* N_ASSIGN N_IDENT-lhs, and N_RETURN. Byte-identical to the pre-#18
* helper. */
static void
cg_structlit_fill_bp(Cg *c, Local **locals_p, Type *lu, Node *lit, int bp_off)
{
cg_structlit_fill(c, locals_p, lu, lit, DST_BP, 0, NULL, bp_off);
}
static void
cgexpr(Cg *c, Node *n, Local *locals)
{
@@ -2176,137 +2234,21 @@ cgexpr(Cg *c, Node *n, Local *locals)
if (n->op == TK_ASSIGN && str_fu
&& str_fu->kind == TY_STRUCT
&& n->rhs && n->rhs->kind == N_STRUCTLIT) {
int ssz = (int)str_fu->size;
/* TK_ELLIPSIS autofill: zero-fill the field
* region first so unmentioned inner fields
* read as 0 (mirrors N_LET / N_IDENT-lhs
* structlit branches). */
if (n->rhs->op == TK_ELLIPSIS) {
ins2(c, A_XORQ, areg(D_AX),
areg(D_AX));
int zbase_reg, zbase_disp;
if (via_ptr || is_global) {
if (via_ptr)
ins2(c, A_MOVQ,
amem(D_BP, boff),
areg(D_BX));
else
ins2(c, A_LEAQ,
masym(c, base->str),
areg(D_BX));
zbase_reg = D_BX;
zbase_disp = foff;
} else {
zbase_reg = D_BP;
zbase_disp = boff + foff;
}
int zi = 0;
while (zi + 8 <= ssz) {
ins2(c, A_MOVQ, areg(D_AX),
amem(zbase_reg,
zbase_disp + zi));
zi += 8;
}
while (zi + 4 <= ssz) {
ins2(c, A_MOVL, areg(D_AX),
amem(zbase_reg,
zbase_disp + zi));
zi += 4;
}
while (zi < ssz) {
ins2(c, A_MOVB, areg(D_AX),
amem(zbase_reg,
zbase_disp + zi));
zi += 1;
}
}
for (Node *fn = n->rhs->list; fn;
fn = fn->next) {
u64 inner_foff = 0;
int fsz = 8;
Type *ft = NULL;
for (Tfield *fl = str_fu->fields;
fl; fl = fl->next) {
if (strcmp(fl->name, fn->str) == 0) {
inner_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) {
if (via_ptr || is_global) {
if (via_ptr)
ins2(c, A_MOVQ,
amem(D_BP, boff),
areg(D_BX));
else
ins2(c, A_LEAQ,
masym(c, base->str),
areg(D_BX));
cg_widen_tagged_store(c,
&locals, fu, fn->lhs,
D_BX,
foff + (int)inner_foff,
(int)fu->size);
} else {
cg_widen_tagged_store(c,
&locals, fu, fn->lhs,
D_BP,
boff + foff + (int)inner_foff,
(int)fu->size);
}
continue;
}
cgexpr(c, fn->lhs, locals);
int sl_isf32 = 0;
if (fld_isfloat(ft, &sl_isf32)) {
int mov = sl_isf32
? A_MOVSS : A_MOVSD;
if (via_ptr || is_global) {
if (via_ptr)
ins2(c, A_MOVQ,
amem(D_BP, boff),
areg(D_BX));
else
ins2(c, A_LEAQ,
masym(c, base->str),
areg(D_BX));
ins2(c, mov, areg(D_X0),
amem(D_BX,
foff + (int)inner_foff));
} else {
ins2(c, mov, areg(D_X0),
amem(D_BP,
boff + foff + (int)inner_foff));
}
continue;
}
int op = A_MOVQ;
if (fsz == 1) op = A_MOVB;
else if (fsz == 4) op = A_MOVL;
if (via_ptr || is_global) {
if (via_ptr)
ins2(c, A_MOVQ,
amem(D_BP, boff),
areg(D_BX));
else
ins2(c, A_LEAQ,
masym(c, base->str),
areg(D_BX));
ins2(c, op, areg(D_AX),
amem(D_BX,
foff + (int)inner_foff));
} else {
ins2(c, op, areg(D_AX),
amem(D_BP,
boff + foff + (int)inner_foff));
}
}
(void)ssz;
/* Delegate to the shared structlit fill
* helper. For via_ptr/is_global, helper
* reloads BX before zero-fill loop + each
* field store. For local BP-rel, helper
* stores direct off BP. AND nested struct-
* typed structlit values recurse instead
* of silently dropping trailing bytes
* (#18 fix). */
int mode = via_ptr ? DST_PTR_LOCAL
: is_global ? DST_GLOBAL : DST_BP;
int disp = (mode == DST_BP)
? (boff + foff) : foff;
cg_structlit_fill(c, &locals, str_fu,
n->rhs, mode, boff,
is_global ? base->str : NULL, disp);
break;
}
if (n->op == TK_ASSIGN && str_fu
@@ -2974,135 +2916,22 @@ cgexpr(Cg *c, Node *n, Local *locals)
}
if (fu && fu->kind == TY_STRUCT
&& n->rhs && n->rhs->kind == N_STRUCTLIT) {
int ssz = fsz;
if (n->rhs->op == TK_ELLIPSIS) {
ins2(c, A_XORQ, areg(D_AX),
areg(D_AX));
int zbase_reg, zbase_off;
if (via_cx) {
if (ptr_root)
ins2(c, A_MOVQ,
amem(D_BP, base_disp),
areg(D_BX));
else
ins2(c, A_LEAQ,
masym(c, cur->str),
areg(D_BX));
zbase_reg = D_BX;
zbase_off = total_off;
} else {
zbase_reg = D_BP;
zbase_off = base_disp + total_off;
}
int zi = 0;
while (zi + 8 <= ssz) {
ins2(c, A_MOVQ, areg(D_AX),
amem(zbase_reg,
zbase_off + zi));
zi += 8;
}
while (zi + 4 <= ssz) {
ins2(c, A_MOVL, areg(D_AX),
amem(zbase_reg,
zbase_off + zi));
zi += 4;
}
while (zi < ssz) {
ins2(c, A_MOVB, areg(D_AX),
amem(zbase_reg,
zbase_off + zi));
zi += 1;
}
}
for (Node *fn = n->rhs->list; fn;
fn = fn->next) {
u64 inner_foff = 0;
int ifsz = 8;
Type *ift = NULL;
for (Tfield *fl = fu->fields; fl;
fl = fl->next) {
if (strcmp(fl->name,
fn->str) == 0) {
inner_foff = fl->offset;
ifsz = (int)(fl->type
? fl->type->size : 8);
ift = fl->type;
break;
}
}
Type *ifu = (ift
&& ift->kind == TY_NAMED)
? ift->under : ift;
if (ifu && ifu->kind == TY_TAGGED) {
if (via_cx) {
if (ptr_root)
ins2(c, A_MOVQ,
amem(D_BP, base_disp),
areg(D_BX));
else
ins2(c, A_LEAQ,
masym(c, cur->str),
areg(D_BX));
cg_widen_tagged_store(c,
&locals, ifu, fn->lhs,
D_BX,
total_off + (int)inner_foff,
(int)ifu->size);
} else {
cg_widen_tagged_store(c,
&locals, ifu, fn->lhs,
D_BP,
base_disp + total_off + (int)inner_foff,
(int)ifu->size);
}
continue;
}
cgexpr(c, fn->lhs, locals);
int sf32 = 0;
if (fld_isfloat(ift, &sf32)) {
int mov = sf32
? A_MOVSS : A_MOVSD;
if (via_cx) {
if (ptr_root)
ins2(c, A_MOVQ,
amem(D_BP, base_disp),
areg(D_BX));
else
ins2(c, A_LEAQ,
masym(c, cur->str),
areg(D_BX));
ins2(c, mov, areg(D_X0),
amem(D_BX,
total_off + (int)inner_foff));
} else {
ins2(c, mov, areg(D_X0),
amem(D_BP,
base_disp + total_off + (int)inner_foff));
}
continue;
}
int op = A_MOVQ;
if (ifsz == 1) op = A_MOVB;
else if (ifsz == 4) op = A_MOVL;
if (via_cx) {
if (ptr_root)
ins2(c, A_MOVQ,
amem(D_BP, base_disp),
areg(D_BX));
else
ins2(c, A_LEAQ,
masym(c, cur->str),
areg(D_BX));
ins2(c, op, areg(D_AX),
amem(D_BX,
total_off + (int)inner_foff));
} else {
ins2(c, op, areg(D_AX),
amem(D_BP,
base_disp + total_off + (int)inner_foff));
}
}
(void)ssz;
/* Delegate to the shared structlit fill
* helper. For via_cx (ptr_root | is_global),
* helper reloads BX before zero-fill loop +
* each field store. For local through chain,
* helper stores direct off BP. AND nested
* struct-typed structlit values recurse
* instead of silently dropping trailing
* bytes (#18 fix). */
int dst_mode = ptr_root ? DST_PTR_LOCAL
: is_global ? DST_GLOBAL : DST_BP;
int dst_disp = (dst_mode == DST_BP)
? (base_disp + total_off) : total_off;
cg_structlit_fill(c, &locals, fu,
n->rhs, dst_mode, base_disp,
is_global ? cur->str : NULL,
dst_disp);
break;
}
if (fu && fu->kind == TY_STRUCT