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

@@ -127,6 +127,14 @@ fn cgexpr(c: *cgen, n: *node) void = {
if (k == nkind.N_TRYUNW) { cgtryunw(c, n); return; };
if (k == nkind.N_TYPETEST) { cgtypetest(c, n); return; };
if (k == nkind.N_TYPEASSERT) { cgtypeassert(c, n); return; };
if (k == nkind.N_TUPLE) {
// #241: a literal tuple rvalue `(a, b)` is a value — pack its
// elements into the register cursor (mirror cgreturn's N_TUPLE
// arm) so a let-bind / destructure consumer reads every element,
// not just AX = 0 from the default arm below.
cgtuplelittocursor(c, n);
return;
};
// Default fallback: produce a deterministic AX = 0. Mirrors
// the C cgen's `default: cgexpr_int(c, 0)` branch, which is
// what `return eof{};` (N_STRUCTLIT with an empty !void
@@ -155,6 +163,27 @@ fn cgtagvariantidx(c: *cgen, tagged: *node, vt: *node) i32 = {
return flatvariantidx(c, tagged, vt);
};
// cgtrytupleshift — #241: if the `?`/`!` operand's success variant (tag 0)
// is a tuple, the unwrapped payload is an rvalue tuple that must fill the
// register cursor (shift past the tag), and the scalar/str MOVQ DX,AX tail
// is skipped. Returns true when it emitted the shift. Reads the operand's
// stamped tagged result tinfo (n.lhs.type_) — the success variant is the
// first param, matching the `CMPQ $0` success-tag convention.
fn cgtrytupleshift(c: *cgen, n: *node) bool = {
if (n.lhs == nil) { return false; };
let ou: *tinfo = n.lhs.type_: *tinfo;
for (ou != nil && ou.kind == tykind.TY_NAMED) { ou = ou.under; };
if (ou == nil) { return false; };
if (ou.kind != tykind.TY_TAGGED) { return false; };
if (ou.params == nil) { return false; };
let sv: *tinfo = ou.params.type_;
for (sv != nil && sv.kind == tykind.TY_NAMED) { sv = sv.under; };
if (sv == nil) { return false; };
if (sv.kind != tykind.TY_TUPLE) { return false; };
cgtaggedtuplepayloadshift(c, sv);
return true;
};
// cgtryprop — `e?` propagates the error variant up the stack.
// Success tag = 0 (#216 tracks the legacy/flag-aware success-tag
// divergence — out of scope here, success check stays `CMPQ $0`).
@@ -218,6 +247,10 @@ fn cgtryprop(c: *cgen, n: *node) void = {
};
emitline("\tMOVQ\tBP, SP\n\tPOPQ\tBP\n\tRET\n");
emitlabel(cl);
// #241: a tuple success payload is an rvalue tuple — fill the cursor
// (shift past the tag) so the destructure / let consumer reads every
// element, not just word0. Success variant = tag 0 (first param).
if (cgtrytupleshift(c, n)) { return; };
// Success: unwrap value. Tag-only result was AX; the rest of
// the codegen expects the success value in AX (and BX for str).
// AX=tag, DX=val0, CX=val1 from the call ABI. For str success,
@@ -281,6 +314,9 @@ fn cgtryunw(c: *cgen, n: *node) void = {
emitline("\n");
emitline("\tMOVQ\t$1, DI\n\tMOVQ\t$60, AX\n\tSYSCALL\n");
emitlabel(cl);
// #241: tuple success payload fills the cursor (shift past the tag) —
// same rvalue-tuple-into-cursor story as cgtryprop.
if (cgtrytupleshift(c, n)) { return; };
// Unwrap success value. (Same shuffle pattern as cgtryprop.)
let succisstr: bool = false;
if (n.lhs != nil) {
@@ -659,6 +695,16 @@ fn cgident(c: *cgen, n: *node) void = {
let lc: *local = localfindnode(c, nm);
if (lc != nil) {
let off: i32 = lc.off;
// #241: a tuple ident is a value — leave the whole tuple in the
// register cursor (`yield t` / `return t` / `let q = t`), not
// just word0 in AX. Mirror of cstage cgexpr N_IDENT tuple arm.
let itu: *tinfo = nil;
if (lc.tnode != nil) { itu = lc.tnode.type_: *tinfo; };
for (itu != nil && itu.kind == tykind.TY_NAMED) { itu = itu.under; };
if (itu != nil) { if (itu.kind == tykind.TY_TUPLE) {
cgtupleslottocursor(c, off, itu);
return;
}; };
// Float local: MOVSS / MOVSD into X0. Skips the AX shuffle
// so consumers (cgbin, cgcast, return) pick up the SSE value
// directly.