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:
@@ -11340,30 +11340,46 @@ fn astunsized(c: *checker, t: *node) bool = {
|
||||
// descend into a nested N_MATCH — each match opens its own yield
|
||||
// scope. exprtype is idempotent on already-stamped nodes (tinfocache
|
||||
// path at L467) so re-entering it on the yield operand here is safe.
|
||||
fn matchyieldtype(c: *checker, body: *node) *node = {
|
||||
// bname/btype carry the enclosing arm's case-binding name + declared type
|
||||
// node. #241: when exprtype can't re-derive a `yield <binder>` operand —
|
||||
// the arm binder's scope is already popped by the time exprtype(N_MATCH)
|
||||
// runs post-order (resolvewalk's N_MCASE restores c.cur before this), so
|
||||
// scopelookup of the bare binder ident returns nil — the yielded type IS
|
||||
// the binding type (btype). cstage avoids this by reading the operand's
|
||||
// already-stamped ->type (check.c:122) rather than re-running cexpr; wwstage
|
||||
// caches only node.type_ (a tinfo, not a type NODE), so this binder-typed
|
||||
// fallback is the node-form recovery for the dominant match-bind-then-yield
|
||||
// idiom (Hare's parseint `case let t => yield t`).
|
||||
fn matchyieldtype(c: *checker, body: *node, bname: str, btype: *node) *node = {
|
||||
if (body == nil) { return nil; };
|
||||
let k: nkind = body.kind;
|
||||
if (k == nkind.N_YIELD) {
|
||||
if (body.lhs == nil) { return nil; };
|
||||
return exprtype(c, body.lhs, nil);
|
||||
let t: *node = exprtype(c, body.lhs, nil);
|
||||
if (t != nil) { return t; };
|
||||
if (body.lhs.kind == nkind.N_IDENT && bname.len > 0
|
||||
&& streq(body.lhs.str, bname)) {
|
||||
return btype;
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
if (k == nkind.N_MATCH) { return nil; };
|
||||
if (k == nkind.N_BLOCK) {
|
||||
let s: *node = body.list;
|
||||
for (s != nil) {
|
||||
let t: *node = matchyieldtype(c, s);
|
||||
let t: *node = matchyieldtype(c, s, bname, btype);
|
||||
if (t != nil) { return t; };
|
||||
s = s.next;
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
if (k == nkind.N_IF) {
|
||||
let t: *node = matchyieldtype(c, body.body);
|
||||
let t: *node = matchyieldtype(c, body.body, bname, btype);
|
||||
if (t != nil) { return t; };
|
||||
return matchyieldtype(c, body.els);
|
||||
return matchyieldtype(c, body.els, bname, btype);
|
||||
};
|
||||
if (k == nkind.N_FOR || k == nkind.N_FORRANGE) {
|
||||
return matchyieldtype(c, body.body);
|
||||
return matchyieldtype(c, body.body, bname, btype);
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
@@ -13154,7 +13170,10 @@ fn exprtype(c: *checker, e: *node, hint: *node) *node = {
|
||||
let yt: *node = nil;
|
||||
let cs: *node = e.list;
|
||||
for (cs != nil) {
|
||||
let t: *node = matchyieldtype(c, cs.body);
|
||||
// cs.str/cs.lhs = the arm's case-binding name + declared
|
||||
// type (the N_MCASE binder); fed to matchyieldtype's #241
|
||||
// scope-popped `yield <binder>` fallback.
|
||||
let t: *node = matchyieldtype(c, cs.body, cs.str, cs.lhs);
|
||||
if (t != nil) { yt = t; break; };
|
||||
cs = cs.next;
|
||||
};
|
||||
@@ -19045,6 +19064,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
|
||||
@@ -19073,6 +19100,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`).
|
||||
@@ -19136,6 +19184,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,
|
||||
@@ -19199,6 +19251,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) {
|
||||
@@ -19577,6 +19632,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.
|
||||
@@ -26592,6 +26657,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;
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1090,30 +1090,46 @@ fn astunsized(c: *checker, t: *node) bool = {
|
||||
// descend into a nested N_MATCH — each match opens its own yield
|
||||
// scope. exprtype is idempotent on already-stamped nodes (tinfocache
|
||||
// path at L467) so re-entering it on the yield operand here is safe.
|
||||
fn matchyieldtype(c: *checker, body: *node) *node = {
|
||||
// bname/btype carry the enclosing arm's case-binding name + declared type
|
||||
// node. #241: when exprtype can't re-derive a `yield <binder>` operand —
|
||||
// the arm binder's scope is already popped by the time exprtype(N_MATCH)
|
||||
// runs post-order (resolvewalk's N_MCASE restores c.cur before this), so
|
||||
// scopelookup of the bare binder ident returns nil — the yielded type IS
|
||||
// the binding type (btype). cstage avoids this by reading the operand's
|
||||
// already-stamped ->type (check.c:122) rather than re-running cexpr; wwstage
|
||||
// caches only node.type_ (a tinfo, not a type NODE), so this binder-typed
|
||||
// fallback is the node-form recovery for the dominant match-bind-then-yield
|
||||
// idiom (Hare's parseint `case let t => yield t`).
|
||||
fn matchyieldtype(c: *checker, body: *node, bname: str, btype: *node) *node = {
|
||||
if (body == nil) { return nil; };
|
||||
let k: nkind = body.kind;
|
||||
if (k == nkind.N_YIELD) {
|
||||
if (body.lhs == nil) { return nil; };
|
||||
return exprtype(c, body.lhs, nil);
|
||||
let t: *node = exprtype(c, body.lhs, nil);
|
||||
if (t != nil) { return t; };
|
||||
if (body.lhs.kind == nkind.N_IDENT && bname.len > 0
|
||||
&& streq(body.lhs.str, bname)) {
|
||||
return btype;
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
if (k == nkind.N_MATCH) { return nil; };
|
||||
if (k == nkind.N_BLOCK) {
|
||||
let s: *node = body.list;
|
||||
for (s != nil) {
|
||||
let t: *node = matchyieldtype(c, s);
|
||||
let t: *node = matchyieldtype(c, s, bname, btype);
|
||||
if (t != nil) { return t; };
|
||||
s = s.next;
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
if (k == nkind.N_IF) {
|
||||
let t: *node = matchyieldtype(c, body.body);
|
||||
let t: *node = matchyieldtype(c, body.body, bname, btype);
|
||||
if (t != nil) { return t; };
|
||||
return matchyieldtype(c, body.els);
|
||||
return matchyieldtype(c, body.els, bname, btype);
|
||||
};
|
||||
if (k == nkind.N_FOR || k == nkind.N_FORRANGE) {
|
||||
return matchyieldtype(c, body.body);
|
||||
return matchyieldtype(c, body.body, bname, btype);
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
@@ -2904,7 +2920,10 @@ fn exprtype(c: *checker, e: *node, hint: *node) *node = {
|
||||
let yt: *node = nil;
|
||||
let cs: *node = e.list;
|
||||
for (cs != nil) {
|
||||
let t: *node = matchyieldtype(c, cs.body);
|
||||
// cs.str/cs.lhs = the arm's case-binding name + declared
|
||||
// type (the N_MCASE binder); fed to matchyieldtype's #241
|
||||
// scope-popped `yield <binder>` fallback.
|
||||
let t: *node = matchyieldtype(c, cs.body, cs.str, cs.lhs);
|
||||
if (t != nil) { yt = t; break; };
|
||||
cs = cs.next;
|
||||
};
|
||||
|
||||
@@ -11340,30 +11340,46 @@ fn astunsized(c: *checker, t: *node) bool = {
|
||||
// descend into a nested N_MATCH — each match opens its own yield
|
||||
// scope. exprtype is idempotent on already-stamped nodes (tinfocache
|
||||
// path at L467) so re-entering it on the yield operand here is safe.
|
||||
fn matchyieldtype(c: *checker, body: *node) *node = {
|
||||
// bname/btype carry the enclosing arm's case-binding name + declared type
|
||||
// node. #241: when exprtype can't re-derive a `yield <binder>` operand —
|
||||
// the arm binder's scope is already popped by the time exprtype(N_MATCH)
|
||||
// runs post-order (resolvewalk's N_MCASE restores c.cur before this), so
|
||||
// scopelookup of the bare binder ident returns nil — the yielded type IS
|
||||
// the binding type (btype). cstage avoids this by reading the operand's
|
||||
// already-stamped ->type (check.c:122) rather than re-running cexpr; wwstage
|
||||
// caches only node.type_ (a tinfo, not a type NODE), so this binder-typed
|
||||
// fallback is the node-form recovery for the dominant match-bind-then-yield
|
||||
// idiom (Hare's parseint `case let t => yield t`).
|
||||
fn matchyieldtype(c: *checker, body: *node, bname: str, btype: *node) *node = {
|
||||
if (body == nil) { return nil; };
|
||||
let k: nkind = body.kind;
|
||||
if (k == nkind.N_YIELD) {
|
||||
if (body.lhs == nil) { return nil; };
|
||||
return exprtype(c, body.lhs, nil);
|
||||
let t: *node = exprtype(c, body.lhs, nil);
|
||||
if (t != nil) { return t; };
|
||||
if (body.lhs.kind == nkind.N_IDENT && bname.len > 0
|
||||
&& streq(body.lhs.str, bname)) {
|
||||
return btype;
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
if (k == nkind.N_MATCH) { return nil; };
|
||||
if (k == nkind.N_BLOCK) {
|
||||
let s: *node = body.list;
|
||||
for (s != nil) {
|
||||
let t: *node = matchyieldtype(c, s);
|
||||
let t: *node = matchyieldtype(c, s, bname, btype);
|
||||
if (t != nil) { return t; };
|
||||
s = s.next;
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
if (k == nkind.N_IF) {
|
||||
let t: *node = matchyieldtype(c, body.body);
|
||||
let t: *node = matchyieldtype(c, body.body, bname, btype);
|
||||
if (t != nil) { return t; };
|
||||
return matchyieldtype(c, body.els);
|
||||
return matchyieldtype(c, body.els, bname, btype);
|
||||
};
|
||||
if (k == nkind.N_FOR || k == nkind.N_FORRANGE) {
|
||||
return matchyieldtype(c, body.body);
|
||||
return matchyieldtype(c, body.body, bname, btype);
|
||||
};
|
||||
return nil;
|
||||
};
|
||||
@@ -13154,7 +13170,10 @@ fn exprtype(c: *checker, e: *node, hint: *node) *node = {
|
||||
let yt: *node = nil;
|
||||
let cs: *node = e.list;
|
||||
for (cs != nil) {
|
||||
let t: *node = matchyieldtype(c, cs.body);
|
||||
// cs.str/cs.lhs = the arm's case-binding name + declared
|
||||
// type (the N_MCASE binder); fed to matchyieldtype's #241
|
||||
// scope-popped `yield <binder>` fallback.
|
||||
let t: *node = matchyieldtype(c, cs.body, cs.str, cs.lhs);
|
||||
if (t != nil) { yt = t; break; };
|
||||
cs = cs.next;
|
||||
};
|
||||
@@ -19045,6 +19064,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
|
||||
@@ -19073,6 +19100,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`).
|
||||
@@ -19136,6 +19184,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,
|
||||
@@ -19199,6 +19251,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) {
|
||||
@@ -19577,6 +19632,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.
|
||||
@@ -26592,6 +26657,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;
|
||||
|
||||
Reference in New Issue
Block a user