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

@@ -1919,6 +1919,25 @@ fn cgindex(c: *cgen, n: *node) void = {
};
};
};
// #121 leg (b): whole TUPLE element `let e = tbl[i]`. Classify off
// the chased index-result tinfo (n.type_, the same source the N_DOT/
// N_INDEX arms read). gptotal = sum(tupeslot/8); the per-base arms
// below fill that many cursor words (tupreg) from the element
// address. Mirrors cstage cgen.c N_INDEX TY_TUPLE arms.
let elem_tuple: bool = false;
let tuple_nwords: i32 = 0;
{
let eti: *tinfo = tichase(n.type_: *tinfo);
if (eti != nil) { if (eti.kind == tykind.TY_TUPLE) {
elem_tuple = true;
// ww tuples store elements in .tupleelems, not .params.
let tpw: *ttupleelem = eti.tupleelems;
for (tpw != nil) {
tuple_nwords += tupeslot(tpw.type_) / 8;
tpw = tpw.tnext;
};
};};
};
cgexpr(c, idx);
if (esz > 1) {
emitline("\tMOVQ\t$");
@@ -1963,6 +1982,25 @@ fn cgindex(c: *cgen, n: *node) void = {
emitline("\tMOVQ\t(BX), AX\n");
return;
};
// #121 leg (b): whole TUPLE element — fill gptotal cursor words
// from the element address (BX); descending so AX loads last,
// mirroring the tagged arm. Over-cap leaves the ADDRESS in AX.
if (elem_tuple) {
if (tuple_nwords > TUPLE_GPCAP) {
emitline("\tMOVQ\tBX, AX\n");
return;
};
let kw: i32 = tuple_nwords - 1;
for (kw >= 0) {
emitline("\tMOVQ\t");
emitdispreg((kw * 8): i64, "BX");
emitline(", ");
emitline(tupreg(kw));
emitline("\n");
kw -= 1;
};
return;
};
// str/slice element: load the full (ptr, len, cap) header into
// (AX, BX, CX) — both are 24B since #1, so cap must survive.
// Kind-gate on isstrtype||isslicetype, never size==24 (a >16B
@@ -2035,6 +2073,24 @@ fn cgindex(c: *cgen, n: *node) void = {
emitline("\tMOVQ\t(BX), AX\n");
return;
};
// #121 leg (b): whole TUPLE element — twin of the global arm
// above (base in BX). Over-cap leaves the ADDRESS in AX.
if (elem_tuple) {
if (tuple_nwords > TUPLE_GPCAP) {
emitline("\tMOVQ\tBX, AX\n");
return;
};
let kw: i32 = tuple_nwords - 1;
for (kw >= 0) {
emitline("\tMOVQ\t");
emitdispreg((kw * 8): i64, "BX");
emitline(", ");
emitline(tupreg(kw));
emitline("\n");
kw -= 1;
};
return;
};
// str/slice element: full (ptr, len, cap) header into (AX, BX, CX);
// cap must survive (#1). Kind-gate, never size==24. Base BX.
if (elemisstr || elemisslice) {
@@ -2096,6 +2152,25 @@ fn cgindex(c: *cgen, n: *node) void = {
emitline("\tMOVQ\t(BX), AX\n");
return;
};
// #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 AX as the addr.
if (elem_tuple) {
if (tuple_nwords > TUPLE_GPCAP) {
return;
};
emitline("\tMOVQ\tAX, BX\n");
let kw: i32 = tuple_nwords - 1;
for (kw >= 0) {
emitline("\tMOVQ\t");
emitdispreg((kw * 8): i64, "BX");
emitline(", ");
emitline(tupreg(kw));
emitline("\n");
kw -= 1;
};
return;
};
// str/slice element via fallback base: full (ptr, len, cap) header
// into (AX, BX, CX); cap must survive (#1). Kind-gate, never
// size==24. Base AX.
@@ -3718,6 +3793,90 @@ fn cgdot(c: *cgen, n: *node) void = {
};};
};
};
// #121 leg (a): `tbl[i].N` — a positional FIELD of an indexed
// TUPLE element. The struct N_INDEX-lhs block above handles
// struct / ptr-to-struct elements; a tuple element fell through
// to the read-resolver and died LOUD. 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 local N_TTUPLE block wires: str-triple / float-X0 / fn-or-
// scalar loadopsz. Narrow (rob Q3): tagged / nested-aggregate
// fields stay LOUD. Mirrors cstage cgen.c leg-(a) arm 1:1.
if (lhs != nil) {
if (lhs.kind == nkind.N_INDEX) {
let eu: *tinfo = tichase(lhs.type_: *tinfo);
if (eu != nil) { if (eu.kind == tykind.TY_TUPLE) {
let idx: i32 = fldnumidx(fld);
if (idx >= 0) {
// ww tuples store elements in .tupleelems
// (ttupleelem chain), NOT .params (that is
// fn-only). foff via tupeslot accumulation =
// cstage tuple_eslot, byte-id.
let tp: *ttupleelem = eu.tupleelems;
let foff: i32 = 0;
let i: i32 = 0;
for (i < idx) {
if (tp == nil) { i = idx; }
else {
foff += tupeslot(tp.type_);
tp = tp.tnext;
i += 1;
};
};
if (tp != nil) {
let ft: *tinfo = tp.type_;
let fu: *tinfo = tichase(ft);
if (fu != nil && (fu.kind == tykind.TY_TAGGED
|| fu.kind == tykind.TY_STRUCT
|| fu.kind == tykind.TY_TUPLE
|| fu.kind == tykind.TY_ARRAY)) {
let mt: str = "#121: aggregate/tagged tuple-element field read off an indexed base unwired\n";
os.write(2, mt.ptr, mt.len: u64);
os.exit(1);
};
if (!cgplaceaddr(c, lhs, "BX")) {
let mp: str = "#121: indexed tuple base not place-resolvable\n";
os.write(2, mp.ptr, mp.len: u64);
os.exit(1);
};
emitline("\tMOVQ\tBX, AX\n");
if (typeisfloat(ft)) {
let mov: str = "MOVSD";
if (typeisf32(ft)) { mov = "MOVSS"; };
emitline("\t");
emitline(mov);
emitline("\t");
emitdispreg(foff: i64, "AX");
emitline(", X0\n");
return;
};
if (fu != nil && (fu.kind == tykind.TY_STR
|| fu.kind == tykind.TY_SLICE)) {
emitline("\tMOVQ\t");
emitdispreg((foff + 8): i64, "AX");
emitline(", BX\n");
emitline("\tMOVQ\t");
emitdispreg((foff + 16): i64, "AX");
emitline(", CX\n");
emitline("\tMOVQ\t");
emitdispreg(foff: i64, "AX");
emitline(", AX\n");
return;
};
let fsz: i32 = 8;
if (ft != nil) { fsz = ft.size: i32; };
let lop: str = loadopsz(typeissigned(ft), fsz);
emitline("\t");
emitline(lop);
emitline("\t");
emitdispreg(foff: i64, "AX");
emitline(", AX\n");
return;
};
};
};};
};
};
// Module-qualified value reference: `mod.name` where `mod`
// is nkind.N_IDENT bound as skind.SK_USE and the leaf isn't a local.
// Treat as a SB symbol — `MOVQ leaf(SB), AX` for the 8B case;
@@ -8157,6 +8316,91 @@ fn cgassign(c: *cgen, n: *node) void = {
c.sretdestoff = 0;
return;
};
// #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). Materialise the literal into a frame
// scratch via the cglet in-cap path (cgtuplelittocursor +
// tupstore), then word-copy scratch → &a[i]. NARROW:
// N_TTUPLE-literal rhs, N_IDENT base (idxelemtn gives the
// element N_TTUPLE), in-cap. Mirror of cstage cgen.c #121
// store arm; the materialise + base-resolve are the
// cglet / #270-1b byte-id twins.
if (n.rhs.kind == nkind.N_TUPLE && esz > 8
&& base != nil && base.kind == nkind.N_IDENT
&& elemtn != nil && elemtn.kind == nkind.N_TTUPLE
&& sretretsize(c, elemtn) == 0) {
let scr121: i32 = tagscradd(c, esz);
cgtuplelittocursor(c, n.rhs, elemtn);
let gpc: i32 = 0;
let ssc: i32 = 0;
let eo: i32 = 0;
let q121: *node = elemtn.list;
for (q121 != nil) {
let qt: *node = q121.lhs;
let isflt: bool = isfloattype(c, qt);
let es: i32 = tupeslotn(qt);
tupstore(c, gpc, ssc, scr121 + eo, es, qt);
if (isflt) { ssc += 1; }
else { gpc += es / 8; };
eo += es;
q121 = q121.next;
};
// dest &a[i] → BX (#270-1b base resolve)
cgexpr(c, idx);
if (esz > 1) {
emitline("\tMOVQ\t$");
emitint(esz: i64);
emitline(", CX\n");
emitline("\tIMULQ\tCX, AX\n");
};
emitline("\tPUSHQ\tAX\n");
if (isglobalarr) {
emitline("\tLEAQ\t");
emitsymname(c, globalname);
emitline("(SB), BX\n");
} else { if (isglobalptr) {
emitline("\tMOVQ\t");
emitsymname(c, globalname);
emitline("(SB), BX\n");
} else { if (baselocal != nil) {
let tn2: *node = baselocal.tnode;
let isarr2: bool = false;
if (tn2 != nil) { if (tn2.kind == nkind.N_TARRAY) { isarr2 = true; }; };
if (basealias) {
let bu60: *tinfo = tichase(base.type_: *tinfo);
if (bu60 != nil) { isarr2 = bu60.kind == tykind.TY_ARRAY; };
};
if (isarr2) {
emitline("\tLEAQ\t");
emitoff(baselocal.off: i64);
emitline("(BP), BX\n");
} else {
emitline("\tMOVQ\t");
emitoff(baselocal.off: i64);
emitline("(BP), BX\n");
};
} else { if (dotbaseaddr(c, base, "BX")) {
} else {
cgexpr(c, base);
emitline("\tMOVQ\tAX, BX\n");
};};};};
emitline("\tPOPQ\tAX\n");
emitline("\tADDQ\tAX, BX\n");
let kk2: i32 = 0;
for (kk2 < esz) {
emitline("\tMOVQ\t");
emitoff((scr121 + kk2): i64);
emitline("(BP), AX\n");
emitline("\tMOVQ\tAX, ");
emitoff(kk2: i64);
emitline("(BX)\n");
kk2 += 8;
};
return;
};
// #270-1b: aggregate (struct/array/tuple >8B) element
// STORE `a[i] = val`. The scalar store path below copies
// only the first 8 bytes (tnodestoreop MOVQ) — a silent

View File

@@ -3727,6 +3727,32 @@ fn cgforrange(c: *cgen, n: *node) void = {
baseoff = localalloc(c, bname, 8, nil);
};
};
// #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 (localfindnode), so a global let/def base
// reads saved-BP as the .ptr/.len. LOUD-STOP symmetric with cstage
// cgen.c (byte-id-neutral; segfault→compile-error is pure
// improvement). The fix (the N_INDEX isglobal base resolution ported
// into the for-range spine) is a DISTINCT mechanism — filed as a #121
// sibling, off fold-6's path.
if (slc != nil) {
if (slc.kind == nkind.N_IDENT) {
if (localfindnode(c, slc.str) == nil) {
let isglob: bool = isletvar(c, slc.str);
if (!isglob) {
let gdtn: *node = defvartnode(c, slc.str);
if (gdtn != nil) {
if (gdtn.kind == nkind.N_TARRAY) { isglob = true; };
};
};
if (isglob) {
let mc: str = "#121: for-range over a module-global slice/array base unwired (global-base resolution gap)\n";
os.write(2, mc.ptr, mc.len: u64);
os.exit(1);
};
};
};
};
// Per-binding (up to 8 — matches the C array). Parallel arrays so
// we don't depend on local-struct cgen.