wcc/cgen: #121 indexed tuple-element read + literal-store round-trip (both-stage)

Reading or storing a tuple element of an indexed array element was
broken across the board (the fold-6 read-path). One fused commit,
both stages, four faces of indexed tuple-element access:

 - FIELD read `tbl[i].N`: was loud ("unsupported field-read shape" --
   the field-read dispatch keyed on an N_IDENT base; an INDEX base fell
   to a fatal). Now resolves &tbl[i] via the place-spine and reads the
   field at addr+foff through the existing per-kind arms (str-triple /
   scalar / fn-ptr).
 - WHOLE read `let e = tbl[i]`: was a silent word0-only truncation
   (plain-tuple kin of #37/#58, which covered only tagged). Now a full
   cursor fill from &tbl[i].
 - STORE `a[i] = (3,4)` (N_TUPLE-literal rhs): was a silent word0-only
   store -- the write face of the read. The aggregate-store-into-index
   site handled ident/dot/deref tuple rhs but not the literal; now it
   materializes the literal and word-copies. Narrow: N_IDENT base only
   (N_DOT/chained stay deferred, #270).
 - for-range over a const-slice-of-tuple: was a divergent SEGV; now a
   symmetric loud-stop on both stages (filed #122).

The store and read were a round-trip that passed test 809 only by luck
(broken store XOR broken read canceled). Fixing the read alone exposed
the silent store; rule-7 obliges fixing both, so 809 is now genuinely
correct, not luck-correct. Both faces are byte-id-blind (#263) -- the
net is a runtime round-trip pin with distinct-per-word values and a
real call clobbering the cursor registers between store and read, so a
word0-only store or read is caught. Both stages byte-identical
(990-997 green). Pin 947_tuple_index_read_run.
This commit is contained in:
2026-06-06 22:23:43 +09:00
parent 761cfa4524
commit 754944a755
8 changed files with 1317 additions and 4 deletions

View File

@@ -6235,6 +6235,80 @@ cgexpr(Cg *c, Node *n, Local *locals)
cg_sret_dest_off = 0;
break;
}
/* #121 (write-face of leg-b): a tuple-LITERAL rhs into an
* indexed element `a[i] = (3,4)`. A literal has no source
* ADDRESS, so the ident/dot/deref copy arm below can't reach
* it — it fell to the 1-word scalar store tail (word0 only;
* the read-luck masked it until leg-b's correct read). The
* indexed-tuple-element STORE is the write-face of leg-b's
* read (one round-trip, #58/#135 precedent). Materialise the
* literal into a frame scratch via the cglet in-cap cursor
* fill (cg_tuple_lit_to_cursor + tuple_store), then word-copy
* scratch → &a[i]. NARROW: only the N_TUPLE-literal rhs (the
* rest of the #270/#31/#32 store family stays deferred). In-
* cap only (cg_sret_retsize==0); over-cap tuple stores keep
* the existing sret/loud paths. */
if ((is_arr || is_sl || is_ptr) && n->op == TK_ASSIGN
&& esubu && esubu->kind == TY_TUPLE && esz > 8
&& n->rhs->kind == N_TUPLE
&& base->kind == N_IDENT
&& cg_sret_retsize(esub) == 0) {
int scr = cg_tagscr_slot(c, &locals, esz);
cg_tuple_lit_to_cursor(c, &locals, n->rhs, esubu);
int gpcur = 0, ssecur = 0, eoff = 0, ef32;
for (Tparam *p = esubu->params; p; p = p->next) {
int isflt = fld_isfloat(p->type, &ef32);
tuple_store(c, p->type, gpcur, ssecur,
scr + eoff);
if (isflt)
ssecur++;
else
gpcur += tuple_eslot(p->type) / 8;
eoff += tuple_eslot(p->type);
}
/* dest &a[i] → BX (mirror the #270-1b resolve) */
cgexpr(c, n->lhs->rhs, locals); /* idx → AX */
if (esz > 1) {
ins2(c, A_MOVQ, aimm(esz), areg(D_CX));
ins2(c, A_IMULQ, areg(D_CX), areg(D_AX));
}
ins1(c, A_PUSHQ, areg(D_AX));
if (base->kind == N_IDENT) {
int off = localfind(locals, base->str);
int isglobal = (off == 0)
&& let_islet(base->str);
if (isglobal && is_arr)
ins2(c, A_LEAQ,
masym(c, base->str),
areg(D_BX));
else if (isglobal)
ins2(c, A_MOVQ,
masym(c, base->str),
areg(D_BX));
else if (is_arr)
ins2(c, A_LEAQ,
amem(D_BP, off), areg(D_BX));
else
ins2(c, A_MOVQ,
amem(D_BP, off), areg(D_BX));
} else if (cg_dotbase_addr(c, base, D_BX, locals)) {
/* N_DOT array-field base resolved inline. */
} else {
cgexpr(c, base, locals);
ins2(c, A_MOVQ, areg(D_AX), areg(D_BX));
}
ins1(c, A_POPQ, areg(D_AX));
ins2(c, A_ADDQ, areg(D_AX), areg(D_BX));
/* word-copy scratch → dest (tuple esz is 8-aligned,
* tuple_eslot 8B floor). */
for (int k = 0; k < esz; k += 8) {
ins2(c, A_MOVQ, amem(D_BP, scr + k),
areg(D_AX));
ins2(c, A_MOVQ, areg(D_AX),
amem(D_BX, k));
}
break;
}
/* #270-1b: aggregate (struct/array/tuple >8B) element
* STORE `a[i] = val`. The scalar store path below copies
* only the first 8 bytes (fldstoreop MOVQ) — a silent
@@ -11434,6 +11508,70 @@ cgexpr(Cg *c, Node *n, Local *locals)
}
}
}
/* #121 leg (a): `tbl[i].N` — a positional FIELD of an indexed
* TUPLE element. The struct branch above handles struct / ptr-to-
* struct elements; a tuple element fell through to the read-
* resolver and died LOUD ("unsupported field-read shape").
* Resolve &tbl[i] via the place-spine (cgplaceaddr, the #116
* mechanism) into BX→AX, then read the field at addr+foff reusing
* the per-element-kind arms the N_IDENT tuple-field block wires
* (cgen.c TY_TUPLE arm): str-triple / float-X0 / fn-or-scalar
* fldloadop. Narrow (rob Q3): tagged / nested-aggregate fields
* stay LOUD (no fold-6 consumer; the #58/#117 untested-arm
* hazard). Mirrors selfhost cgenexpr.ww's leg-(a) twin. */
if (n->lhs && n->lhs->kind == N_INDEX && n->str) {
Type *eu = type_chase_named(n->lhs->type);
if (eu && eu->kind == TY_TUPLE) {
int idx = 0;
for (const char *q = n->str; *q; q++)
idx = idx * 10 + (*q - '0');
Tparam *tp = eu->params;
int foff = 0;
while (idx > 0 && tp) {
foff += tuple_eslot(tp->type);
tp = tp->next;
idx--;
}
if (tp != NULL) {
Type *fu = type_chase_named(tp->type);
if (fu && (fu->kind == TY_TAGGED
|| fu->kind == TY_STRUCT
|| fu->kind == TY_TUPLE
|| fu->kind == TY_ARRAY))
fatal("#121: aggregate/tagged tuple-"
"element field read off an "
"indexed base unwired");
if (!cgplaceaddr(c, n->lhs, D_BX, locals))
fatal("#121: indexed tuple base "
"not place-resolvable");
ins2(c, A_MOVQ, areg(D_BX), areg(D_AX));
int tf32 = 0;
if (fld_isfloat(tp->type, &tf32)) {
ins2(c, tf32 ? A_MOVSS : A_MOVSD,
amem(D_AX, foff), areg(D_X0));
goto dot_done;
}
if (fu && (fu->kind == TY_STR
|| fu->kind == TY_SLICE)) {
ins2(c, A_MOVQ,
amem(D_AX, foff + 8),
areg(D_BX));
ins2(c, A_MOVQ,
amem(D_AX, foff + 16),
areg(D_CX));
ins2(c, A_MOVQ,
amem(D_AX, foff + 0),
areg(D_AX));
goto dot_done;
}
int fsz = (int)(tp->type
? tp->type->size : 8);
int op = fldloadop(tp->type, fsz);
ins2(c, op, amem(D_AX, foff), areg(D_AX));
goto dot_done;
}
}
}
/* Nested module-qualified field where the chain didn't fold to
* a known shape (typical when w6c runs on a single file with
* `use mod;` but no driver concatenation — the body's enum /
@@ -11632,6 +11770,30 @@ cgexpr(Cg *c, Node *n, Local *locals)
ins2(c, A_MOVQ, amem(D_BX, 0), areg(D_AX));
break;
}
/* #121 leg (b): whole TUPLE element `let e = tbl[i]`.
* The in-cap tuple receive (cglet) reads the SysV register
* cursor (tuple_rseq words L→R); without this arm the
* element fell to the scalar load below = word0 only
* (str.len→0, later words garbage — silent both-wrong-
* identical, #263). Fill gptotal cursor words from the
* element address (BX); descending so AX (= tuple_rseq[0])
* loads last, mirroring the tagged arm. Over-cap (> the GP
* cursor) leaves the ADDRESS in AX (the sret /
* cg_tagged_memread convention) — the cglet over-cap arm
* louds it (no live consumer). */
if (esubu && esubu->kind == TY_TUPLE) {
int nw = 0;
for (Tparam *p = esubu->params; p; p = p->next)
nw += tuple_eslot(p->type) / 8;
if (nw > TUPLE_GPCAP) {
ins2(c, A_MOVQ, areg(D_BX), areg(D_AX));
break;
}
for (int k = nw - 1; k >= 0; k--)
ins2(c, A_MOVQ, amem(D_BX, k * 8),
areg(tuple_rseq[k]));
break;
}
/* float element → MOVSS/MOVSD into X0: the consumer's
* ADDSD/MOVSD spill machinery already expects X0, but the
* integer fldloadop below would leave it in AX and the SSE
@@ -11702,6 +11864,23 @@ cgexpr(Cg *c, Node *n, Local *locals)
ins2(c, A_MOVQ, amem(D_BX, 0), areg(D_AX));
break;
}
/* #121 leg (b) via fallback base: whole TUPLE element. AX holds
* the element address — copy to BX (the cursor fill into AX
* clobbers it), then fill the gptotal cursor words. Over-cap
* leaves the ADDRESS in AX (cglet louds it). Twin of the
* N_IDENT-base arm above. */
if (esubu && esubu->kind == TY_TUPLE) {
int nw = 0;
for (Tparam *p = esubu->params; p; p = p->next)
nw += tuple_eslot(p->type) / 8;
if (nw > TUPLE_GPCAP)
break; /* AX already = element addr */
ins2(c, A_MOVQ, areg(D_AX), areg(D_BX));
for (int k = nw - 1; k >= 0; k--)
ins2(c, A_MOVQ, amem(D_BX, k * 8),
areg(tuple_rseq[k]));
break;
}
/* float element via fallback base → X0 (see Site A, #119). The
* base address is in AX; MOVSS/MOVSD reads the element into X0. */
if (type_isfloat(esub)) {
@@ -13524,6 +13703,24 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
if (slc && slc->kind == N_UN && slc->op == TK_STAR
&& u && (u->kind == TY_SLICE || u->kind == TY_STR))
fatal("for-range over a deref base unwired (#11)");
/* #121 leg (c): for-range over a module-GLOBAL slice/str/array
* base SEGV's today — the init + per-iteration base resolution
* below assume a frame-local slot (localfind), so a global
* let/def base reads saved-BP as the .ptr/.len (SEGV; divergent
* cs≠ww asm). LOUD-STOP symmetric both stages (byte-id-neutral —
* no asm; converges the divergence by rejecting; segfault→
* compile-error is pure improvement). The fix is the N_INDEX
* isglobal base resolution (LEAQ/MOVQ name(SB)) ported into the
* for-range spine — a DISTINCT mechanism from leg (a)/(b)'s
* element-address machinery, with its own scalar/str/array/
* destructure global-base test surface. Filed as a #121 sibling;
* off fold-6's path (fold-6 needs only leg (a)). */
if (slc && slc->kind == N_IDENT
&& localfind(*locals, slc->str) == 0
&& (let_islet(slc->str) || def_isarraydef(slc->str)))
fatal("#121: for-range over a module-global "
"slice/array base unwired (global-base "
"resolution gap)");
int baseoff = 0;
if (slc && slc->kind != N_IDENT
&& !(u && u->kind == TY_ARRAY)) {