w6c+wwstage: cgexpr materializes tuple rvalues + unwrap-shift for tuple-payload destructure (#241)

cgexpr could not produce a tuple VALUE, so a destructure / let bind of an
RVALUE tuple read garbage past the first element (cstage) or left an untyped
binder aborting wwstage's asserttyped gate — a DANGEROUS gate-blind cs!=ww,
and the strconv-int blocker (Hare's stoi64/stou64 require
`let (sign, u) = parseint(s, base)?`). Three feeders, all routed at the same
SysV register-return cursor the cgmlet/cgmassign consumers already read:

  - an N_TUPLE literal fell to the `cgexpr_int(0)` / `MOVQ $0, AX` default;
  - a tuple-typed IDENT loaded only word0 into AX (`yield t`, `return t`,
    `let q = t`), leaving DX/CX stale;
  - the `?`/`!` unwrap of a tuple-in-union payload lifted only word0->AX,
    stranding word1 in CX (the scalar/str success ABI).

Fix (both stages, byte-identical per rule 10):

  - cgexpr packs an N_TUPLE literal into the cursor (cg_tuple_lit_to_cursor /
    cgtuplelittocursor — a byte-identical reuse of cgreturn's in-register
    N_TUPLE arm) and a tuple IDENT from its slot at the register-ABI stride
    (cg_tuple_slot_to_cursor / cgtupleslottocursor);
  - the ?/! unwrap shifts a tuple success payload down one integer reg past
    the tag (cg_tagged_tuple_payload_shift / cgtaggedtuplepayloadshift),
    loud-stopping a float/slice/str payload element (the SysV per-eightbyte
    tagged-tuple-payload classification is #243);
  - wwstage's checker recovers the popped match-arm binder type for a
    `yield <binder>` operand (matchyieldtype's scope-free fallback to the
    arm's declared type), so the destructured binders stamp — cstage reads
    the operand's already-stamped ->type, wwstage caches only a tinfo.

Over-cap rvalue-tuple materialisation (no slot to sret a bare expression
value into) loud-stops both stages — the #10 follow-up.

NOT closed (distinct root, deferred to #238/task #6): single-var
`let q = (true, 9u64)` then `q.N` — the N_LET tuple-init sz==16||32 gate
drops a narrow-first mixed tuple, and the N_DOT tuple-field PACKED-offset
reader disagrees with tuple_store's 8B stride. Not the rvalue-into-cursor
fix and not a strconv blocker (strconv destructures); documented at the test
header.

Test 945_rvalue_tuple_destructure_run: literal destructure, match-yield
destructure, and the ?-call strconv shape, each run + cs==ww byte-id on both
drivers (9 checks). Embedded w6c/wwdump combined.ww regenerated.
This commit is contained in:
2026-06-01 21:27:49 +09:00
parent 6fc85f9aaf
commit 5d023c0ef0
8 changed files with 1229 additions and 22 deletions

View File

@@ -246,6 +246,197 @@ fn tupstore(c: *cgen, gpcur: i32, ssecur: i32, off: i32, wide: bool, tn: *node)
emitline("(BP)\n");
};
// cgtuplelittocursor — #241: materialise an N_TUPLE literal's elements into
// the SysV register-return cursor (integer words L->R over tupreg AX/DX/CX/
// R8, floats over tupsse X0/X1, a slice/str's {ptr,len,cap} over three
// consecutive INTEGER regs) — the SAME ABI a tuple-returning call leaves,
// which every tuple consumer (tupstore at cgmlet/cgmassign) reads. cgexpr
// otherwise falls to its `MOVQ $0, AX` default for a tuple, so a literal
// rvalue tuple bound or destructured read garbage past word0. Byte-identical
// extraction of cgreturn's in-register N_TUPLE arm (cgenstmt.ww), now shared
// with cgexpr. Over-cap loud-stops (rule 7); a bare expression value can't
// sret, so the >cap rvalue-tuple materialisation is the #10 follow-up.
fn cgtuplelittocursor(c: *cgen, tuple: *node) void = {
let ssecap: i32 = TUPLE_SSECAP;
let gptotal: i32 = 0;
let ssecount: i32 = 0;
let e: *node = tuple.list;
for (e != nil) {
if (isfloattype(c, e)) {
ssecount = ssecount + 1;
} else {
let wide: bool = nodeisstr(c, e) || nodeisslice(c, e);
gptotal = gptotal + tupebytes(wide);
};
e = e.next;
};
if (gptotal > TUPLE_GPCAP || ssecount > ssecap) {
let msg: str = "tuple literal exceeds register-return ABI capacity (integer AX,DX,CX,R8 / SSE X0,X1); over-cap rvalue-tuple materialisation is the #10 sret follow-up\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let fscr: i32 = 0;
if (ssecount > 0) {
fscr = localadd(c, "@tupfscr", ssecap * 8, nil);
};
let sseidx: i32 = 0;
e = tuple.list;
for (e != nil) {
let isflt: bool = isfloattype(c, e);
cgexpr(c, e);
if (isflt) {
let mov: str = "MOVSD";
if (isf32type(c, e)) { mov = "MOVSS"; };
emitline("\t"); emitline(mov); emitline("\tX0, ");
emitoff((fscr + sseidx * 8): i64);
emitline("(BP)\n");
sseidx = sseidx + 1;
} else {
emitline("\tPUSHQ\tAX\n");
if (nodeisstr(c, e) || nodeisslice(c, e)) {
emitline("\tPUSHQ\tBX\n");
emitline("\tPUSHQ\tCX\n");
};
};
e = e.next;
};
let i: i32 = gptotal - 1;
for (i >= 0) {
emitline("\tPOPQ\t");
emitline(tupreg(i));
emitline("\n");
i = i - 1;
};
let j: i32 = 0;
e = tuple.list;
for (e != nil) {
if (isfloattype(c, e)) {
let mov: str = "MOVSD";
if (isf32type(c, e)) { mov = "MOVSS"; };
emitline("\t"); emitline(mov); emitline("\t");
emitoff((fscr + j * 8): i64);
emitline("(BP), ");
emitline(tupsse(j));
emitline("\n");
j = j + 1;
};
e = e.next;
};
};
// cgtupleslottocursor — #241: load a tuple already materialised in a BP-
// relative slot (a tuple-typed IDENT: a let-bound tuple, a match-bound union
// payload) into the SAME register cursor. The slot uses the register-ABI
// stride the tuple-init / #242 destructure write (a scalar 8B, a slice/str
// its 3-word header), NOT the packed t.N field layout (#238). All sources
// are memory, so each word loads straight into its cursor reg. So `yield t`
// / `return t` / `let q = t` over a tuple ident leave the whole tuple in the
// cursor, not just word0 in AX. Over-cap loud-stops (rule 7; #10). Mirror of
// cstage cg_tuple_slot_to_cursor.
fn cgtupleslottocursor(c: *cgen, srcoff: i32, tu: *tinfo) void = {
let gptotal: i32 = 0;
let ssecount: i32 = 0;
let el: *ttupleelem = tu.tupleelems;
for (el != nil) {
let et: *tinfo = el.type_;
for (et != nil && et.kind == tykind.TY_NAMED) { et = et.under; };
if (et != nil && (et.kind == tykind.TY_F32 || et.kind == tykind.TY_F64)) {
ssecount = ssecount + 1;
} else {
let wide: bool = et != nil && (et.kind == tykind.TY_SLICE || et.kind == tykind.TY_STR);
gptotal = gptotal + tupebytes(wide);
};
el = el.tnext;
};
if (gptotal > TUPLE_GPCAP || ssecount > TUPLE_SSECAP) {
let msg: str = "tuple ident exceeds register-return ABI capacity (integer AX,DX,CX,R8 / SSE X0,X1); over-cap rvalue-tuple materialisation is the #10 sret follow-up\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let gp: i32 = 0;
let sse: i32 = 0;
let foff: i32 = 0;
el = tu.tupleelems;
for (el != nil) {
let et: *tinfo = el.type_;
for (et != nil && et.kind == tykind.TY_NAMED) { et = et.under; };
let isflt: bool = et != nil && (et.kind == tykind.TY_F32 || et.kind == tykind.TY_F64);
let wide: bool = et != nil && (et.kind == tykind.TY_SLICE || et.kind == tykind.TY_STR);
if (isflt) {
let mov: str = "MOVSD";
if (et.kind == tykind.TY_F32) { mov = "MOVSS"; };
emitline("\t"); emitline(mov); emitline("\t");
emitoff((srcoff + foff): i64);
emitline("(BP), ");
emitline(tupsse(sse));
emitline("\n");
sse = sse + 1;
foff += 8;
} else { if (wide) {
let k: i32 = 0;
for (k < 3) {
emitline("\tMOVQ\t");
emitoff((srcoff + foff + k * 8): i64);
emitline("(BP), ");
emitline(tupreg(gp + k));
emitline("\n");
k += 1;
};
gp += 3;
foff += et.size: i32;
} else {
emitline("\tMOVQ\t");
emitoff((srcoff + foff): i64);
emitline("(BP), ");
emitline(tupreg(gp));
emitline("\n");
gp += 1;
foff += 8;
}; };
el = el.tnext;
};
};
// cgtaggedtuplepayloadshift — #241: a `?`-unwrapped tuple payload is an
// rvalue tuple that must fill the register cursor. The tagged return leaves
// AX=tag, DX=word0, CX=word1, R8=word2; the scalar/str unwrap lifts only
// word0->AX, stranding word1+ in CX/R8. Shift the whole payload DOWN one
// INTEGER reg so element i lands in tupreg(i). Float/slice/str payload
// elements ride a different SysV class — loud-stop (rule 7; the per-
// eightbyte tagged-tuple-payload classification is the #243 follow-up).
// Mirror of cstage cg_tagged_tuple_payload_shift.
fn cgtaggedtuplepayloadshift(c: *cgen, tup: *tinfo) void = {
let words: i32 = 0;
let el: *ttupleelem = tup.tupleelems;
for (el != nil) {
let et: *tinfo = el.type_;
for (et != nil && et.kind == tykind.TY_NAMED) { et = et.under; };
let isflt: bool = et != nil && (et.kind == tykind.TY_F32 || et.kind == tykind.TY_F64);
let wide: bool = et != nil && (et.kind == tykind.TY_SLICE || et.kind == tykind.TY_STR);
if (isflt || wide) {
let msg: str = "tuple-in-union ? unwrap: float/slice/str payload element needs SysV per-eightbyte classification (see #243); only integer tuple payloads supported\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
words = words + 1;
el = el.tnext;
};
if (words > 3) {
let msg: str = "tuple-in-union ? unwrap payload exceeds the 3 integer return regs past the tag; see #10/#243\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let i: i32 = 0;
for (i < words) {
emitline("\tMOVQ\t");
emitline(tupreg(i + 1));
emitline(", ");
emitline(tupreg(i));
emitline("\n");
i = i + 1;
};
};
fn cgreturn(c: *cgen, n: *node) void = {
rundefers(c);
let rhs: *node = n.lhs;