w6c+wwstage: construct + bind tuple-in-union payload (#242)
A mixed-scalar tuple WRAPPED IN A TAGGED UNION (the (neg, n) shape Hare's
strconv parseint returns, ((bool,u64)|invalid|overflow)) miscompiled three
ways, all gate-blind (no bootstrap tuple-in-union):
(a) cstage CONSTRUCTION: a tuple variant fell through the N_RETURN scalar
shuffle, which ZEROED tag + payload — the operands were never packed.
Route the tuple variant through the scratch-slot widen path; add a
TY_TUPLE arm to cg_widen_tagged_store that packs each element into the
union payload at the register-ABI 8B stride + sets the variant tag.
(b) wwstage CHECKER: `let (a,b)=t` over a plain tuple ident (the match-
bound union payload) left the un-annotated binders UNTYPED, so the bin
node reading them was untyped -> asserttyped abort. The element-type
distribution only fired for an N_CALL rhs. Consume the rhs tuple type
for ANY rhs (mirror cstage check.c:2017).
(c) BOTH stages DESTRUCTURE: the register-cursor receive assumes the rhs
left every element in AX/DX/CX (a call's tuple-return ABI). For a tuple
IDENT cgexpr loads only word0->AX, so the 2nd binder read a STALE DX.
Copy each element from the ident's slot at the 8B stride.
Construction is correct at ANY variant position (the resolved tag, not a
default 0); wwstage resolves it via the typeeq core (flatvariantidxt), not
taggedvariantindext whose str/slice shape-fallback would mask a mismatch.
Two rule-7 loud-stops cover shapes this slotted packing can't yet handle,
on BOTH stages, so neither silently miscompiles:
- a tuple with a SysV-eightbyte-sharing narrow pair (e.g. (i32,i32,u64)),
caught by the 8+payload > slot-size guard (the eightbyte tuple
classification is #243);
- a tuple built from a BARE LITERAL element (`true`/`false`, suffix-less
`7`). cstage's cg_tag_for_variant can't type the literal (#241), returns
-1, and loud-stops. wwstage types `true` as bool and `7` as untyped_int,
so flatvariantidxt WOULD resolve the variant — a program cstage rejects
but wwstage accepts is the cs!=ww divergence rule 10 forbids. wwstage
mirrors cstage's CONDITION (a bare-literal element), not its -1
mechanism, with an explicit guard that aligns the richer side DOWN. Lift
BOTH guards together when #241 lands cstage literal typing -> symmetric
accept.
Test 940_tuple_in_union: 4 K_RUN rows (variant 0, void arm, tuple at
variant 1 two ways) x cstage-run + wwstage-run + cs==ww byte-id, plus 2
K_BUILDERR rows (eightbyte-share, bare-literal) asserting a loud stop with
the #242 diagnostic on BOTH drivers = 16 ok.
This commit is contained in:
@@ -265,7 +265,13 @@ fn cgreturn(c: *cgen, n: *node) void = {
|
||||
// INTEGER cursor (ref/qbe/amd64/sysv.c retr L95-108). Both rows
|
||||
// loud-stop at their cap (rule-7): INTEGER 4, SSE 2. The SAME
|
||||
// class split drives the receive sites.
|
||||
if (rhs.kind == nkind.N_TUPLE) {
|
||||
// #242: a bare tuple return packs into the register cursor; a
|
||||
// tuple WRAPPED IN A TAGGED UNION must instead pack into the
|
||||
// union payload (tag + words) — fall through to the tagged path
|
||||
// below, which routes it via cgwidentaggedstore. Without this
|
||||
// guard the bare-tuple arm fired first and dropped the tag,
|
||||
// returning (AX=word0, DX=word1) with no tag word.
|
||||
if (rhs.kind == nkind.N_TUPLE && !istaggedtype(c, c.fnret)) {
|
||||
let ssecap: i32 = TUPLE_SSECAP; // X0,X1 per SysV
|
||||
let gptotal: i32 = 0;
|
||||
let ssecount: i32 = 0;
|
||||
@@ -469,6 +475,11 @@ fn cgreturn(c: *cgen, n: *node) void = {
|
||||
if (rhstaggedident(c, rhs) != nil) {
|
||||
needswiden = true;
|
||||
};
|
||||
// #242: a tuple variant packs into the union
|
||||
// payload via cgwidentaggedstore's TY_TUPLE arm.
|
||||
if (rhs.kind == nkind.N_TUPLE) {
|
||||
needswiden = true;
|
||||
};
|
||||
};
|
||||
};
|
||||
if (needswiden) {
|
||||
@@ -1883,6 +1894,71 @@ fn cgmlet(c: *cgen, n: *node) void = {
|
||||
let sretrecv: i32 = 0;
|
||||
if (rhs.kind == nkind.N_CALL) { sretrecv = callsretsize(c, rhs); };
|
||||
|
||||
// #242: rhs is a tuple already materialised in a local slot (a match-
|
||||
// bound union payload, `let (a,b)=t`), NOT a register-returning call.
|
||||
// cgexpr(tuple ident) loads only word0->AX, so the register cursor
|
||||
// path below reads DX/CX stale. Copy each element from the ident's
|
||||
// slot at the register-ABI 8B stride (24B for a slice/str header) —
|
||||
// the SAME layout the tagged construct + match payload-bind write.
|
||||
// Mirror of cstage cgen.c N_MLET tuple-ident arm. The binding element
|
||||
// types ride l.lhs (stamped by the checker's stamptuplebinds).
|
||||
if (rhs.kind == nkind.N_IDENT) {
|
||||
let rl: *local = localfindnode(c, rhs.str);
|
||||
if (rl != nil) {
|
||||
let rti: *tinfo = rl.tnode.type_: *tinfo;
|
||||
for (rti != nil && rti.kind == tykind.TY_NAMED) { rti = rti.under; };
|
||||
if (rti != nil) { if (rti.kind == tykind.TY_TUPLE) {
|
||||
let srcoff: i32 = rl.off;
|
||||
let foff: i32 = 0;
|
||||
let lb: *node = n.list;
|
||||
for (lb != nil) {
|
||||
let tn: *node = lb.lhs;
|
||||
let isflt: bool = isfloattype(c, tn);
|
||||
let wide: bool = isstrtype(c, tn) || isslicetype(c, tn);
|
||||
let esz: i32 = 8;
|
||||
let eti: *tinfo = nil;
|
||||
if (tn != nil) { eti = tn.type_: *tinfo; };
|
||||
if (eti != nil) { esz = eti.size: i32; };
|
||||
let bsz: i32 = 8;
|
||||
if (wide) { bsz = tyslicesize(): i32; };
|
||||
let off: i32 = localadd(c, lb.str, bsz, tn);
|
||||
if (isflt) {
|
||||
let mov: str = "MOVSD";
|
||||
if (isf32type(c, tn)) { mov = "MOVSS"; };
|
||||
emitline("\t"); emitline(mov); emitline("\t");
|
||||
emitoff((srcoff + foff): i64);
|
||||
emitline("(BP), X0\n");
|
||||
emitline("\t"); emitline(mov); emitline("\tX0, ");
|
||||
emitoff(off: i64); emitline("(BP)\n");
|
||||
} else { if (wide) {
|
||||
let k: i32 = 0;
|
||||
for (k < esz) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff((srcoff + foff + k): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff((off + k): i64);
|
||||
emitline("(BP)\n");
|
||||
k += 8;
|
||||
};
|
||||
} else {
|
||||
let lop: str = tnodeloadop(c, tn, esz);
|
||||
let sop: str = tnodestoreop(c, tn, esz);
|
||||
emitline("\t"); emitline(lop); emitline("\t");
|
||||
emitoff((srcoff + foff): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\t"); emitline(sop); emitline("\tAX, ");
|
||||
emitoff(off: i64); emitline("(BP)\n");
|
||||
}; };
|
||||
if (wide) { foff += 24; } else { foff += 8; };
|
||||
lb = lb.next;
|
||||
};
|
||||
c.lastwasreturn = 0;
|
||||
return;
|
||||
}; };
|
||||
};
|
||||
};
|
||||
|
||||
cgexpr(c, rhs);
|
||||
|
||||
if (sretrecv > 0) {
|
||||
|
||||
Reference in New Issue
Block a user