diff --git a/Makefile b/Makefile index 81747c34..8edf338d 100644 --- a/Makefile +++ b/Makefile @@ -321,6 +321,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_peellint_gate \ $(BIN)/test_tuple_nary_destructure_run \ $(BIN)/test_rvalue_tuple_destructure_run \ + $(BIN)/test_tuple_lit_declblind_run \ $(BIN)/test_overcap_tuple_field_store_run \ $(BIN)/test_mixed_scalar_tuple_sret_run \ $(BIN)/test_tuple_in_union_run \ @@ -1590,6 +1591,18 @@ $(BIN)/test_rvalue_tuple_destructure_run: test/wcc/945_rvalue_tuple_destructure_ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +# #64 + #68: tuple LITERAL fills the register cursor DECL-BLIND — the #57 +# decl wire (a declared-tagged element widens into its box) stopped at +# N_LET/N_RETURN; destructure-REASSIGN (#64) and CALL-ARG send (#68) still +# rode the decl-less route (box stored/sent word0-only, cursor skewed). +# Both stages, #263 gate-blind (byte-id cs==ww, both ran wrong). Run + +# cs==ww byte-id. +$(BIN)/test_tuple_lit_declblind_run: test/wcc/945_tuple_lit_declblind_run.c \ + $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + # #234: over-cap tuple sret store into a local struct field / indexed local # (run + cs==ww byte-id), deferred forms loud-stop (builderr both drivers). $(BIN)/test_overcap_tuple_field_store_run: test/wcc/940_overcap_tuple_field_store_run.c \ diff --git a/cmd/w6c/cgen.c b/cmd/w6c/cgen.c index 1e156962..84e638ef 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -250,6 +250,23 @@ node_tuplearg(Node *n) return (u && u->kind == TY_TUPLE) ? u : NULL; } +/* node_tuplearg_decl — #68: like node_tuplearg, but an N_TUPLE LITERAL arg + * with a tuple PARAM type reports the DECLARED param tuple (element widths + * keyed on the param, so a declared-tagged element's box words drive the + * send/drain). The literal's own type is element-constructed (a concrete + * rvalue under a tagged slot counts ONE word, not the box) — see the cgcall + * paramtup send. Every other source falls back to node_tuplearg. */ +static Type * +node_tuplearg_decl(Node *n, Type *param) +{ + if (n && n->kind == N_TUPLE && param) { + Type *pu = type_chase_named(param); + if (pu && pu->kind == TY_TUPLE) + return pu; + } + return node_tuplearg(n); +} + /* #83: positional tuple register-return ABI. Tuple elements ride * consecutive eightbytes over tuple_rseq[]; a slice/str rides its 3-word * {ptr,len,cap} header (ref/hare/rt/ensure.ha:4-8, ty_str->size SSoT), a @@ -8842,10 +8859,12 @@ cgexpr(Cg *c, Node *n, Local *locals) int widen_sz[64] = {0}; Type *widen_param[64] = {0}; int memarg[64] = {0}; + Type *argparam[64] = {0}; /* #68: declared param type per arg */ { Tparam *p = callee_params; for (int i = 0; i < argcount; i++) { if (p == NULL) break; + argparam[i] = p->type; Type *at = args[i] ? args[i]->type : NULL; int psz = tagged_arg_size(p->type); if (psz > 0) { @@ -9207,8 +9226,26 @@ cgexpr(Cg *c, Node *n, Local *locals) args[i], widen_sz[i]); continue; } - cgexpr(c, args[i], locals); - Type *tuparg_push = node_tuplearg(args[i]); + /* #68: a tuple-LITERAL arg derives its DECLARED tuple + * type from the callee PARAM (the #57 decl wire, + * extended to call-arg send), so a declared-tagged + * element's concrete rvalue widens into the box cursor; + * the restage + push then key on the param tuple type, + * not the literal's element-constructed type. */ + Type *paramtup = NULL; + if (args[i] && args[i]->kind == N_TUPLE) { + Type *pcu = argparam[i] + ? type_chase_named(argparam[i]) : NULL; + if (pcu && pcu->kind == TY_TUPLE) + paramtup = pcu; + } + if (paramtup) + cg_tuple_lit_to_cursor(c, &locals, args[i], + paramtup); + else + cgexpr(c, args[i], locals); + Type *tuparg_push = paramtup + ? paramtup : node_tuplearg(args[i]); /* #32 (C-t2, rule 7): a tuple-typed arg from a source * shape whose cgexpr does NOT fill the return cursor * (chain reads, match exprs, ...) must die loud here — @@ -9307,11 +9344,22 @@ cgexpr(Cg *c, Node *n, Local *locals) * cannot carry it; pre-guard it ran * WRONG (inner words skewed). Loud * until a consumer motivates wiring. */ + /* #68: a declared-tagged element graduates + * to a real widen — the decl-aware send + * (cg_tuple_lit_to_cursor over the param + * tuple type) already left the box words in + * the cursor, so the count + tuple_store + * below carry them. Only the param-decl + * literal path is wired; nested tuple/ + * struct/array stay rule-7 loud, as does a + * tagged element with no param decl (the + * cursor was filled stamped-keyed). */ Type *cu = type_chase_named(p->type); if (cu && (cu->kind == TY_TUPLE || cu->kind == TY_STRUCT || cu->kind == TY_ARRAY - || cu->kind == TY_TAGGED)) + || (cu->kind == TY_TAGGED + && !paramtup))) fatal("#32: tuple arg element " "kind unsupported (nested " "tuple/struct/array/tagged; " @@ -9549,7 +9597,8 @@ cgexpr(Cg *c, Node *n, Local *locals) else stackslots++; } - } else if ((tu = node_tuplearg(args[i])) != NULL) { + } else if ((tu = node_tuplearg_decl(args[i], + argparam[i])) != NULL) { /* #163: drain the tuple's staged words (pushed * slot+0 first) into the SysV arg cursor by SysV * class — a float MOVSD/MOVSS off (SP) into the @@ -9557,7 +9606,14 @@ cgexpr(Cg *c, Node *n, Local *locals) * next INTEGER arg reg (DI/SI/..); a slice/str its * 3-word {ptr,len,cap}. Reg overflow loud-stops * (rule 7): the partial-spill stitch is out of - * scope (twin of #164's cap). */ + * scope (twin of #164's cap). + * + * #68: a tuple-LITERAL arg drains over the PARAM + * tuple element widths (node_tuplearg_decl), so a + * declared-tagged element's box words pop into the + * arg cursor together with the i64 that follows — + * the literal's element-constructed types undercount + * a wide box. Mirrors the param-aware send. */ int ef32; for (Tparam *p = tu->params; p; p = p->next) { if (fld_isfloat(p->type, &ef32)) { @@ -14084,15 +14140,42 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) * SAME producer source the SEND walks. */ int sret_recv = (n->rhs && n->rhs->kind == N_CALL) ? cg_sret_retsize(n->rhs->type) : 0; - /* #64 (filed, rule 7): an N_TUPLE literal rhs rides this - * decl-less cgexpr route, so a declared-TAGGED element's - * concrete rvalue still fills the cursor stamped-keyed - * (silent skew) — the #57 decl wire stops at return/let. */ - cgexpr(c, n->rhs, *locals); Type *rt = n->rhs ? n->rhs->type : NULL; Type *ru = type_chase_named(rt); Tparam *tp0 = (ru && ru->kind == TY_TUPLE) ? ru->params : NULL; int mf32; + /* #64: a tuple-LITERAL rhs carries a DECLARED tuple type (built + * from the lvalue binding types) into the cursor fill, so a + * declared-tagged element's concrete rvalue widens into the box + * instead of riding the decl-less stamped-keyed route — the #57 + * decl wire extended past N_LET/N_RETURN to destructure-reassign. + * A `_` lvalue is never type-stamped (the checker skips it) — + * fall back to the rhs literal element type for its cursor + * stride so the next element stays aligned (harec `_` advance; + * pinned by the R3 control row). */ + int litrhs = (n->rhs && n->rhs->kind == N_TUPLE); + Tparam *declp = NULL; + if (litrhs) { + Tparam **dpp = &declp; + Tparam *tpw = tp0; + for (Node *l = n->list; l; l = l->next) { + Tparam *dpn = amalloc(c->a, sizeof *dpn); + dpn->name = NULL; + dpn->type = l->type ? l->type + : (tpw ? tpw->type : NULL); + dpn->next = NULL; + dpn->variadic = 0; + *dpp = dpn; + dpp = &dpn->next; + if (tpw) tpw = tpw->next; + } + Type *decltt = newtype(c->a, TY_TUPLE); + decltt->params = declp; + cg_tuple_lit_to_cursor(c, locals, n->rhs, decltt); + } else { + cgexpr(c, n->rhs, *locals); + } + Tparam *recvp = litrhs ? declp : tp0; if (sret_recv > 0) { int scr = cg_sretscr_off; int foff = 0; @@ -14137,7 +14220,7 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) int gpcap = TUPLE_GPCAP; int ssecap = TUPLE_SSECAP; int gptotal = 0, ssetotal = 0; - for (Tparam *tp = tp0; tp; tp = tp->next) { + for (Tparam *tp = recvp; tp; tp = tp->next) { if (fld_isfloat(tp->type, &mf32)) ssetotal++; else @@ -14152,7 +14235,7 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) "capacity (%d eightbytes: X0,X1); see return-ABI #10", ssecap); int gpcur = 0, ssecur = 0; - Tparam *tp = tp0; + Tparam *tp = recvp; for (Node *l = n->list; l; l = l->next) { Type *et = tp ? tp->type : NULL; int isflt = fld_isfloat(et, &mf32); diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 342e94e1..c282f1be 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -17065,22 +17065,40 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { emitline("\tX0, (SP)\n"); return rest + 1; }; - cgexpr(c, arg); - // #163/#32 (C-t2): tuple ARG (param twin of #164's return). cgexpr - // left the tuple in the return-ABI cursor (AX/DX/CX/R8 + X0/X1) for - // every nodetuplearg producer — call (return ABI), ident - // (cgtupleslottocursor), literal (cgtuplelittocursor), `?`/`!` + // #68: a tuple-LITERAL arg derives its DECLARED tuple type from the + // callee PARAM type node (the #57 decl wire, extended to call-arg + // send), so a declared-tagged element's concrete rvalue widens into + // the box cursor; the restage + push then key on the param tuple type + // node (declared element widths), not the literal's element- + // constructed types. Mirrors cstage cgcall paramtup. + let paramtt: *node = nil; + if (arg.kind == nkind.N_TUPLE) { if (param != nil) { + if (param.kind == nkind.N_PARAM && param.lhs != nil) { + let ptn: *node = param.lhs; + for (ptn != nil && ptn.kind == nkind.N_TNAME) { + ptn = aliaslookup(c, ptn.str); + }; + if (ptn != nil) { if (ptn.kind == nkind.N_TTUPLE) { paramtt = ptn; }; }; + }; + }; }; + if (paramtt != nil) { cgtuplelittocursor(c, arg, paramtt); } + else { cgexpr(c, arg); }; + // #163/#32 (C-t2): tuple ARG (param twin of #164's return). cgexpr / + // the decl-aware fill left the tuple in the return-ABI cursor (AX/DX/ + // CX/R8 + X0/X1) for every nodetuplearg producer — call (return ABI), + // ident (cgtupleslottocursor), literal (cgtuplelittocursor), `?`/`!` // unwrap (payload shift); restage it into @tupargscr by SysV class // (tupstore, the #164 helper) and push the slot words high->low so // the pop drains slot+0 first into the SysV ARG cursor. The frame // slot decouples the return-class regs from the overlapping - // arg-class regs. An N_TUPLE literal's elements are VALUE exprs — - // classify them the way cgtuplelittocursor does (nodeisstr/ - // nodeisslice), kind-discriminated; the stride is the slot formula - // (wide ? header : 8), the same number slotsize() gives a type node. - let tuparg: *node = nodetuplearg(c, arg); + // arg-class regs. When the PARAM tuple type is known (#68), walk its + // declared element type nodes (p.lhs); else an N_TUPLE literal's + // elements are VALUE exprs classified the way cgtuplelittocursor does. + let tuparg: *node = paramtt; + if (tuparg == nil) { tuparg = nodetuplearg(c, arg); }; if (tuparg != nil) { - let tuplit: bool = tuparg.kind == nkind.N_TUPLE; + let tuplit: bool = false; + if (paramtt == nil) { if (tuparg.kind == nkind.N_TUPLE) { tuplit = true; }; }; let gptot: i32 = 0; let sstot: i32 = 0; let tsz: i32 = 0; @@ -17099,23 +17117,34 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { let eti: *tinfo = et.type_: *tinfo; eti = tichase(eti); if (eti != nil) { - if (eti.kind == tykind.TY_TUPLE + let bad: bool = eti.kind == tykind.TY_TUPLE || eti.kind == tykind.TY_STRUCT - || eti.kind == tykind.TY_ARRAY - || eti.kind == tykind.TY_TAGGED) { + || eti.kind == tykind.TY_ARRAY; + // #68: a declared-tagged element graduates to a real + // widen — the decl-aware send (cgtuplelittocursor over + // the param tuple type) left the box words in the + // cursor, so tupstore below carries them. A tagged + // element with no param decl stays rule-7 loud (the + // cursor was filled stamped-keyed). + if (eti.kind == tykind.TY_TAGGED && paramtt == nil) { bad = true; }; + if (bad) { let mne: str = "#32: tuple arg element kind unsupported (nested tuple/struct/array/tagged; rule 7)\n"; os.write(2, mne.ptr, mne.len: u64); os.exit(1); }; }; - // tagged is guarded loud above, so wide-vs-scalar is - // the full slot split here; eslot keeps the stride - // arithmetic on the accessor scale (#22). - let wide: bool = false; - if (tuplit) { wide = nodeisstr(c, et) || nodeisslice(c, et); } - else { wide = isstrtype(c, et) || isslicetype(c, et); }; + // eslot — the full slot stride (str/slice header, tagged + // box, else 8); on the declared (non-tuplit) path tupeslotn + // reads the element TYPE node directly (#68 box-aware, + // mirrors cstage tuple_eslot). A literal element rides the + // wide-vs-scalar split off its VALUE node. let eslot: i32 = 8; - if (wide) { eslot = tyslicesize(): i32; }; + if (tuplit) { + let wide: bool = nodeisstr(c, et) || nodeisslice(c, et); + if (wide) { eslot = tyslicesize(): i32; }; + } else { + eslot = tupeslotn(et); + }; if (isfloattype(c, et)) { sstot += 1; } else { gptot += eslot / 8; }; tsz += eslot; @@ -17136,11 +17165,13 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { for (p != nil) { let et: *node = p.lhs; if (tuplit) { et = p; }; - let wide: bool = false; - if (tuplit) { wide = nodeisstr(c, et) || nodeisslice(c, et); } - else { wide = isstrtype(c, et) || isslicetype(c, et); }; let eslot: i32 = 8; - if (wide) { eslot = tyslicesize(): i32; }; + if (tuplit) { + let wide: bool = nodeisstr(c, et) || nodeisslice(c, et); + if (wide) { eslot = tyslicesize(): i32; }; + } else { + eslot = tupeslotn(et); + }; tupstore(c, gpcur, ssecur, scr + eoff, eslot, et); if (isfloattype(c, et)) { ssecur += 1; } else { gpcur += eslot / 8; }; @@ -28798,19 +28829,38 @@ fn cgcall(c: *cgen, n: *node) void = { stackslots += 1; }; popped += 1; - } else { let tuparg: *node = nodetuplearg(c, a); + } else { + // #68: drain over the PARAM tuple element widths for a + // tuple LITERAL arg (the declared-tagged box words pop + // together with the i64 that follows), mirroring the + // param-aware send; else the source tuple type. + let dptt: *node = nil; + if (a.kind == nkind.N_TUPLE) { if (dparam != nil) { + if (dparam.kind == nkind.N_PARAM && dparam.lhs != nil) { + let dtn2: *node = dparam.lhs; + for (dtn2 != nil && dtn2.kind == nkind.N_TNAME) { + dtn2 = aliaslookup(c, dtn2.str); + }; + if (dtn2 != nil) { if (dtn2.kind == nkind.N_TTUPLE) { dptt = dtn2; }; }; + }; + }; }; + let tuparg: *node = dptt; + if (tuparg == nil) { tuparg = nodetuplearg(c, a); }; if (tuparg != nil) { // #163/#32: drain the tuple's staged words (slot+0 // pushed first) into the SysV arg cursor by SysV class // — a float MOVSD/MOVSS off (SP) into the next XMM, // else POPQ into the next INTEGER arg reg; a slice/str - // its 3 words. Reg overflow loud-stops (rule 7); the + // its 3 words, a declared-tagged box its eslot words + // (#68). Reg overflow loud-stops (rule 7); the // partial-spill stitch is out of scope (twin of #164). // C-t2: nodetuplearg admits ident/literal/unwrap // sources; a literal's elements are VALUE exprs, // classified the way the @tupargscr restage classified - // them (kind-discriminated twin walk). - let tuplit: bool = tuparg.kind == nkind.N_TUPLE; + // them (kind-discriminated twin walk). The param-aware + // path (dptt) walks declared element TYPE nodes. + let tuplit: bool = false; + if (dptt == nil) { if (tuparg.kind == nkind.N_TUPLE) { tuplit = true; }; }; let p: *node = tuparg.list; for (p != nil) { let et: *node = p.lhs; @@ -28832,14 +28882,17 @@ fn cgcall(c: *cgen, n: *node) void = { fpidx += 1; popped += 1; } else { - // tagged is guarded loud at the restage, so - // wide-vs-scalar is the full slot split here - // (#22 accessor scale). - let wide: bool = false; - if (tuplit) { wide = nodeisstr(c, et) || nodeisslice(c, et); } - else { wide = isstrtype(c, et) || isslicetype(c, et); }; + // eslot — full slot split; on the declared + // path tupeslotn reads the element TYPE node + // directly (#68 box-aware), else wide-vs-scalar + // off the literal VALUE node (#22 accessor scale). let eb: i32 = 1; - if (wide) { eb = (tyslicesize() / 8i64): i32; }; + if (tuplit) { + let wide: bool = nodeisstr(c, et) || nodeisslice(c, et); + if (wide) { eb = (tyslicesize() / 8i64): i32; }; + } else { + eb = tupeslotn(et) / 8; + }; if (intidx + eb > 6) { let msg: str = "tuple arg element overflows integer arg regs (DI/SI/DX/CX/R8/R9); stitch out of scope, see #163\n"; os.write(2, msg.ptr, msg.len: u64); @@ -36355,11 +36408,37 @@ fn cgmassign(c: *cgen, n: *node) void = { }; }; - // #64 (filed, rule 7): an N_TUPLE literal rhs rides this decl-less - // cgexpr route, so a declared-TAGGED element's concrete rvalue - // still fills the cursor stamped-keyed (silent skew) — the #57 - // decl wire stops at return/let. - if (n.rhs != nil) { cgexpr(c, n.rhs); }; + // #64: a tuple-LITERAL rhs carries a DECLARED tuple type (built from + // the lvalue binding types) into the cursor fill, so a declared-tagged + // element's concrete rvalue widens into the box instead of riding the + // decl-less stamped-keyed route — the #57 decl wire extended past + // cgmlet/cgreturn to destructure-reassign. A `_` lvalue has no local + // (no declared type node); its decl element stays nil and the + // fill/receive fall back to the rhs literal element's own stamped type + // for the cursor stride (harec `_` advance; pinned by the R3 control). + let litrhs: bool = false; + if (n.rhs != nil) { if (n.rhs.kind == nkind.N_TUPLE) { litrhs = true; }; }; + let synthdecl: *node = nil; + if (litrhs) { + synthdecl = newnode(nkind.N_TTUPLE, n.rhs.file, n.rhs.line, n.rhs.col); + let dtail: *node = nil; + let lb0: *node = n.list; + for (lb0 != nil) { + let w: *node = newnode(nkind.N_TUPLE, n.rhs.file, n.rhs.line, n.rhs.col); + w.lhs = nil; + if (lb0.kind == nkind.N_IDENT) { + let lc0: *local = localfindnode(c, lb0.str); + if (lc0 != nil) { w.lhs = lc0.tnode; }; + }; + w.next = nil; + if (dtail == nil) { synthdecl.list = w; } else { dtail.next = w; }; + dtail = w; + lb0 = lb0.next; + }; + cgtuplelittocursor(c, n.rhs, synthdecl); + } else { + if (n.rhs != nil) { cgexpr(c, n.rhs); }; + }; if (sretrecv > 0) { let scr: i32 = localfind(c, "@sretscr"); @@ -36438,9 +36517,20 @@ fn cgmassign(c: *cgen, n: *node) void = { let l: *node = n.list; let pt: *node = nil; if (rettuple != nil) { pt = rettuple.list; }; + // #64: a tuple-LITERAL rhs keys element WIDTH on the DECLARED lvalue + // type (synthdecl), not the rettuple (nil for a literal); a `_` slot + // (declared type nil) falls back to the rhs literal element's own + // stamped type for the cursor stride. + let dp: *node = nil; + let re: *node = nil; + if (litrhs) { dp = synthdecl.list; re = n.rhs.list; }; for (l != nil) { let tn: *node = nil; - if (pt != nil) { tn = pt.lhs; }; + if (litrhs) { + if (dp != nil && dp.lhs != nil) { tn = dp.lhs; } else { tn = re; }; + } else { + if (pt != nil) { tn = pt.lhs; }; + }; if (isfloattype(c, tn)) { ssetotal = ssetotal + 1; } else { @@ -36448,6 +36538,8 @@ fn cgmassign(c: *cgen, n: *node) void = { }; l = l.next; if (pt != nil) { pt = pt.next; }; + if (dp != nil) { dp = dp.next; }; + if (re != nil) { re = re.next; }; }; if (gptotal > TUPLE_GPCAP) { // AX,DX,CX,R8 capacity // pinned loud-stop, inline like cgen.ww:604 (cstage uses @@ -36467,9 +36559,16 @@ fn cgmassign(c: *cgen, n: *node) void = { l = n.list; pt = nil; if (rettuple != nil) { pt = rettuple.list; }; + dp = nil; + re = nil; + if (litrhs) { dp = synthdecl.list; re = n.rhs.list; }; for (l != nil) { let tn: *node = nil; - if (pt != nil) { tn = pt.lhs; }; + if (litrhs) { + if (dp != nil && dp.lhs != nil) { tn = dp.lhs; } else { tn = re; }; + } else { + if (pt != nil) { tn = pt.lhs; }; + }; let isflt: bool = isfloattype(c, tn); let eslot: i32 = tupeslotn(tn); let off: i32 = 0; @@ -36486,6 +36585,8 @@ fn cgmassign(c: *cgen, n: *node) void = { }; l = l.next; if (pt != nil) { pt = pt.next; }; + if (dp != nil) { dp = dp.next; }; + if (re != nil) { re = re.next; }; }; c.lastwasreturn = 0; return; diff --git a/selfhost/cmd/wcc/cgenexpr.ww b/selfhost/cmd/wcc/cgenexpr.ww index 884f75a3..2be69b80 100644 --- a/selfhost/cmd/wcc/cgenexpr.ww +++ b/selfhost/cmd/wcc/cgenexpr.ww @@ -7363,19 +7363,38 @@ fn cgcall(c: *cgen, n: *node) void = { stackslots += 1; }; popped += 1; - } else { let tuparg: *node = nodetuplearg(c, a); + } else { + // #68: drain over the PARAM tuple element widths for a + // tuple LITERAL arg (the declared-tagged box words pop + // together with the i64 that follows), mirroring the + // param-aware send; else the source tuple type. + let dptt: *node = nil; + if (a.kind == nkind.N_TUPLE) { if (dparam != nil) { + if (dparam.kind == nkind.N_PARAM && dparam.lhs != nil) { + let dtn2: *node = dparam.lhs; + for (dtn2 != nil && dtn2.kind == nkind.N_TNAME) { + dtn2 = aliaslookup(c, dtn2.str); + }; + if (dtn2 != nil) { if (dtn2.kind == nkind.N_TTUPLE) { dptt = dtn2; }; }; + }; + }; }; + let tuparg: *node = dptt; + if (tuparg == nil) { tuparg = nodetuplearg(c, a); }; if (tuparg != nil) { // #163/#32: drain the tuple's staged words (slot+0 // pushed first) into the SysV arg cursor by SysV class // — a float MOVSD/MOVSS off (SP) into the next XMM, // else POPQ into the next INTEGER arg reg; a slice/str - // its 3 words. Reg overflow loud-stops (rule 7); the + // its 3 words, a declared-tagged box its eslot words + // (#68). Reg overflow loud-stops (rule 7); the // partial-spill stitch is out of scope (twin of #164). // C-t2: nodetuplearg admits ident/literal/unwrap // sources; a literal's elements are VALUE exprs, // classified the way the @tupargscr restage classified - // them (kind-discriminated twin walk). - let tuplit: bool = tuparg.kind == nkind.N_TUPLE; + // them (kind-discriminated twin walk). The param-aware + // path (dptt) walks declared element TYPE nodes. + let tuplit: bool = false; + if (dptt == nil) { if (tuparg.kind == nkind.N_TUPLE) { tuplit = true; }; }; let p: *node = tuparg.list; for (p != nil) { let et: *node = p.lhs; @@ -7397,14 +7416,17 @@ fn cgcall(c: *cgen, n: *node) void = { fpidx += 1; popped += 1; } else { - // tagged is guarded loud at the restage, so - // wide-vs-scalar is the full slot split here - // (#22 accessor scale). - let wide: bool = false; - if (tuplit) { wide = nodeisstr(c, et) || nodeisslice(c, et); } - else { wide = isstrtype(c, et) || isslicetype(c, et); }; + // eslot — full slot split; on the declared + // path tupeslotn reads the element TYPE node + // directly (#68 box-aware), else wide-vs-scalar + // off the literal VALUE node (#22 accessor scale). let eb: i32 = 1; - if (wide) { eb = (tyslicesize() / 8i64): i32; }; + if (tuplit) { + let wide: bool = nodeisstr(c, et) || nodeisslice(c, et); + if (wide) { eb = (tyslicesize() / 8i64): i32; }; + } else { + eb = tupeslotn(et) / 8; + }; if (intidx + eb > 6) { let msg: str = "tuple arg element overflows integer arg regs (DI/SI/DX/CX/R8/R9); stitch out of scope, see #163\n"; os.write(2, msg.ptr, msg.len: u64); diff --git a/selfhost/cmd/wcc/cgenstmt.ww b/selfhost/cmd/wcc/cgenstmt.ww index c36aa1f8..aeb7cd80 100644 --- a/selfhost/cmd/wcc/cgenstmt.ww +++ b/selfhost/cmd/wcc/cgenstmt.ww @@ -3171,11 +3171,37 @@ fn cgmassign(c: *cgen, n: *node) void = { }; }; - // #64 (filed, rule 7): an N_TUPLE literal rhs rides this decl-less - // cgexpr route, so a declared-TAGGED element's concrete rvalue - // still fills the cursor stamped-keyed (silent skew) — the #57 - // decl wire stops at return/let. - if (n.rhs != nil) { cgexpr(c, n.rhs); }; + // #64: a tuple-LITERAL rhs carries a DECLARED tuple type (built from + // the lvalue binding types) into the cursor fill, so a declared-tagged + // element's concrete rvalue widens into the box instead of riding the + // decl-less stamped-keyed route — the #57 decl wire extended past + // cgmlet/cgreturn to destructure-reassign. A `_` lvalue has no local + // (no declared type node); its decl element stays nil and the + // fill/receive fall back to the rhs literal element's own stamped type + // for the cursor stride (harec `_` advance; pinned by the R3 control). + let litrhs: bool = false; + if (n.rhs != nil) { if (n.rhs.kind == nkind.N_TUPLE) { litrhs = true; }; }; + let synthdecl: *node = nil; + if (litrhs) { + synthdecl = newnode(nkind.N_TTUPLE, n.rhs.file, n.rhs.line, n.rhs.col); + let dtail: *node = nil; + let lb0: *node = n.list; + for (lb0 != nil) { + let w: *node = newnode(nkind.N_TUPLE, n.rhs.file, n.rhs.line, n.rhs.col); + w.lhs = nil; + if (lb0.kind == nkind.N_IDENT) { + let lc0: *local = localfindnode(c, lb0.str); + if (lc0 != nil) { w.lhs = lc0.tnode; }; + }; + w.next = nil; + if (dtail == nil) { synthdecl.list = w; } else { dtail.next = w; }; + dtail = w; + lb0 = lb0.next; + }; + cgtuplelittocursor(c, n.rhs, synthdecl); + } else { + if (n.rhs != nil) { cgexpr(c, n.rhs); }; + }; if (sretrecv > 0) { let scr: i32 = localfind(c, "@sretscr"); @@ -3254,9 +3280,20 @@ fn cgmassign(c: *cgen, n: *node) void = { let l: *node = n.list; let pt: *node = nil; if (rettuple != nil) { pt = rettuple.list; }; + // #64: a tuple-LITERAL rhs keys element WIDTH on the DECLARED lvalue + // type (synthdecl), not the rettuple (nil for a literal); a `_` slot + // (declared type nil) falls back to the rhs literal element's own + // stamped type for the cursor stride. + let dp: *node = nil; + let re: *node = nil; + if (litrhs) { dp = synthdecl.list; re = n.rhs.list; }; for (l != nil) { let tn: *node = nil; - if (pt != nil) { tn = pt.lhs; }; + if (litrhs) { + if (dp != nil && dp.lhs != nil) { tn = dp.lhs; } else { tn = re; }; + } else { + if (pt != nil) { tn = pt.lhs; }; + }; if (isfloattype(c, tn)) { ssetotal = ssetotal + 1; } else { @@ -3264,6 +3301,8 @@ fn cgmassign(c: *cgen, n: *node) void = { }; l = l.next; if (pt != nil) { pt = pt.next; }; + if (dp != nil) { dp = dp.next; }; + if (re != nil) { re = re.next; }; }; if (gptotal > TUPLE_GPCAP) { // AX,DX,CX,R8 capacity // pinned loud-stop, inline like cgen.ww:604 (cstage uses @@ -3283,9 +3322,16 @@ fn cgmassign(c: *cgen, n: *node) void = { l = n.list; pt = nil; if (rettuple != nil) { pt = rettuple.list; }; + dp = nil; + re = nil; + if (litrhs) { dp = synthdecl.list; re = n.rhs.list; }; for (l != nil) { let tn: *node = nil; - if (pt != nil) { tn = pt.lhs; }; + if (litrhs) { + if (dp != nil && dp.lhs != nil) { tn = dp.lhs; } else { tn = re; }; + } else { + if (pt != nil) { tn = pt.lhs; }; + }; let isflt: bool = isfloattype(c, tn); let eslot: i32 = tupeslotn(tn); let off: i32 = 0; @@ -3302,6 +3348,8 @@ fn cgmassign(c: *cgen, n: *node) void = { }; l = l.next; if (pt != nil) { pt = pt.next; }; + if (dp != nil) { dp = dp.next; }; + if (re != nil) { re = re.next; }; }; c.lastwasreturn = 0; return; diff --git a/selfhost/cmd/wcc/cgenutil.ww b/selfhost/cmd/wcc/cgenutil.ww index 4fee7111..c31e448a 100644 --- a/selfhost/cmd/wcc/cgenutil.ww +++ b/selfhost/cmd/wcc/cgenutil.ww @@ -829,22 +829,40 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { emitline("\tX0, (SP)\n"); return rest + 1; }; - cgexpr(c, arg); - // #163/#32 (C-t2): tuple ARG (param twin of #164's return). cgexpr - // left the tuple in the return-ABI cursor (AX/DX/CX/R8 + X0/X1) for - // every nodetuplearg producer — call (return ABI), ident - // (cgtupleslottocursor), literal (cgtuplelittocursor), `?`/`!` + // #68: a tuple-LITERAL arg derives its DECLARED tuple type from the + // callee PARAM type node (the #57 decl wire, extended to call-arg + // send), so a declared-tagged element's concrete rvalue widens into + // the box cursor; the restage + push then key on the param tuple type + // node (declared element widths), not the literal's element- + // constructed types. Mirrors cstage cgcall paramtup. + let paramtt: *node = nil; + if (arg.kind == nkind.N_TUPLE) { if (param != nil) { + if (param.kind == nkind.N_PARAM && param.lhs != nil) { + let ptn: *node = param.lhs; + for (ptn != nil && ptn.kind == nkind.N_TNAME) { + ptn = aliaslookup(c, ptn.str); + }; + if (ptn != nil) { if (ptn.kind == nkind.N_TTUPLE) { paramtt = ptn; }; }; + }; + }; }; + if (paramtt != nil) { cgtuplelittocursor(c, arg, paramtt); } + else { cgexpr(c, arg); }; + // #163/#32 (C-t2): tuple ARG (param twin of #164's return). cgexpr / + // the decl-aware fill left the tuple in the return-ABI cursor (AX/DX/ + // CX/R8 + X0/X1) for every nodetuplearg producer — call (return ABI), + // ident (cgtupleslottocursor), literal (cgtuplelittocursor), `?`/`!` // unwrap (payload shift); restage it into @tupargscr by SysV class // (tupstore, the #164 helper) and push the slot words high->low so // the pop drains slot+0 first into the SysV ARG cursor. The frame // slot decouples the return-class regs from the overlapping - // arg-class regs. An N_TUPLE literal's elements are VALUE exprs — - // classify them the way cgtuplelittocursor does (nodeisstr/ - // nodeisslice), kind-discriminated; the stride is the slot formula - // (wide ? header : 8), the same number slotsize() gives a type node. - let tuparg: *node = nodetuplearg(c, arg); + // arg-class regs. When the PARAM tuple type is known (#68), walk its + // declared element type nodes (p.lhs); else an N_TUPLE literal's + // elements are VALUE exprs classified the way cgtuplelittocursor does. + let tuparg: *node = paramtt; + if (tuparg == nil) { tuparg = nodetuplearg(c, arg); }; if (tuparg != nil) { - let tuplit: bool = tuparg.kind == nkind.N_TUPLE; + let tuplit: bool = false; + if (paramtt == nil) { if (tuparg.kind == nkind.N_TUPLE) { tuplit = true; }; }; let gptot: i32 = 0; let sstot: i32 = 0; let tsz: i32 = 0; @@ -863,23 +881,34 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { let eti: *tinfo = et.type_: *tinfo; eti = tichase(eti); if (eti != nil) { - if (eti.kind == tykind.TY_TUPLE + let bad: bool = eti.kind == tykind.TY_TUPLE || eti.kind == tykind.TY_STRUCT - || eti.kind == tykind.TY_ARRAY - || eti.kind == tykind.TY_TAGGED) { + || eti.kind == tykind.TY_ARRAY; + // #68: a declared-tagged element graduates to a real + // widen — the decl-aware send (cgtuplelittocursor over + // the param tuple type) left the box words in the + // cursor, so tupstore below carries them. A tagged + // element with no param decl stays rule-7 loud (the + // cursor was filled stamped-keyed). + if (eti.kind == tykind.TY_TAGGED && paramtt == nil) { bad = true; }; + if (bad) { let mne: str = "#32: tuple arg element kind unsupported (nested tuple/struct/array/tagged; rule 7)\n"; os.write(2, mne.ptr, mne.len: u64); os.exit(1); }; }; - // tagged is guarded loud above, so wide-vs-scalar is - // the full slot split here; eslot keeps the stride - // arithmetic on the accessor scale (#22). - let wide: bool = false; - if (tuplit) { wide = nodeisstr(c, et) || nodeisslice(c, et); } - else { wide = isstrtype(c, et) || isslicetype(c, et); }; + // eslot — the full slot stride (str/slice header, tagged + // box, else 8); on the declared (non-tuplit) path tupeslotn + // reads the element TYPE node directly (#68 box-aware, + // mirrors cstage tuple_eslot). A literal element rides the + // wide-vs-scalar split off its VALUE node. let eslot: i32 = 8; - if (wide) { eslot = tyslicesize(): i32; }; + if (tuplit) { + let wide: bool = nodeisstr(c, et) || nodeisslice(c, et); + if (wide) { eslot = tyslicesize(): i32; }; + } else { + eslot = tupeslotn(et); + }; if (isfloattype(c, et)) { sstot += 1; } else { gptot += eslot / 8; }; tsz += eslot; @@ -900,11 +929,13 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { for (p != nil) { let et: *node = p.lhs; if (tuplit) { et = p; }; - let wide: bool = false; - if (tuplit) { wide = nodeisstr(c, et) || nodeisslice(c, et); } - else { wide = isstrtype(c, et) || isslicetype(c, et); }; let eslot: i32 = 8; - if (wide) { eslot = tyslicesize(): i32; }; + if (tuplit) { + let wide: bool = nodeisstr(c, et) || nodeisslice(c, et); + if (wide) { eslot = tyslicesize(): i32; }; + } else { + eslot = tupeslotn(et); + }; tupstore(c, gpcur, ssecur, scr + eoff, eslot, et); if (isfloattype(c, et)) { ssecur += 1; } else { gpcur += eslot / 8; }; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 0f3fd57c..0df74c0e 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -17065,22 +17065,40 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { emitline("\tX0, (SP)\n"); return rest + 1; }; - cgexpr(c, arg); - // #163/#32 (C-t2): tuple ARG (param twin of #164's return). cgexpr - // left the tuple in the return-ABI cursor (AX/DX/CX/R8 + X0/X1) for - // every nodetuplearg producer — call (return ABI), ident - // (cgtupleslottocursor), literal (cgtuplelittocursor), `?`/`!` + // #68: a tuple-LITERAL arg derives its DECLARED tuple type from the + // callee PARAM type node (the #57 decl wire, extended to call-arg + // send), so a declared-tagged element's concrete rvalue widens into + // the box cursor; the restage + push then key on the param tuple type + // node (declared element widths), not the literal's element- + // constructed types. Mirrors cstage cgcall paramtup. + let paramtt: *node = nil; + if (arg.kind == nkind.N_TUPLE) { if (param != nil) { + if (param.kind == nkind.N_PARAM && param.lhs != nil) { + let ptn: *node = param.lhs; + for (ptn != nil && ptn.kind == nkind.N_TNAME) { + ptn = aliaslookup(c, ptn.str); + }; + if (ptn != nil) { if (ptn.kind == nkind.N_TTUPLE) { paramtt = ptn; }; }; + }; + }; }; + if (paramtt != nil) { cgtuplelittocursor(c, arg, paramtt); } + else { cgexpr(c, arg); }; + // #163/#32 (C-t2): tuple ARG (param twin of #164's return). cgexpr / + // the decl-aware fill left the tuple in the return-ABI cursor (AX/DX/ + // CX/R8 + X0/X1) for every nodetuplearg producer — call (return ABI), + // ident (cgtupleslottocursor), literal (cgtuplelittocursor), `?`/`!` // unwrap (payload shift); restage it into @tupargscr by SysV class // (tupstore, the #164 helper) and push the slot words high->low so // the pop drains slot+0 first into the SysV ARG cursor. The frame // slot decouples the return-class regs from the overlapping - // arg-class regs. An N_TUPLE literal's elements are VALUE exprs — - // classify them the way cgtuplelittocursor does (nodeisstr/ - // nodeisslice), kind-discriminated; the stride is the slot formula - // (wide ? header : 8), the same number slotsize() gives a type node. - let tuparg: *node = nodetuplearg(c, arg); + // arg-class regs. When the PARAM tuple type is known (#68), walk its + // declared element type nodes (p.lhs); else an N_TUPLE literal's + // elements are VALUE exprs classified the way cgtuplelittocursor does. + let tuparg: *node = paramtt; + if (tuparg == nil) { tuparg = nodetuplearg(c, arg); }; if (tuparg != nil) { - let tuplit: bool = tuparg.kind == nkind.N_TUPLE; + let tuplit: bool = false; + if (paramtt == nil) { if (tuparg.kind == nkind.N_TUPLE) { tuplit = true; }; }; let gptot: i32 = 0; let sstot: i32 = 0; let tsz: i32 = 0; @@ -17099,23 +17117,34 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { let eti: *tinfo = et.type_: *tinfo; eti = tichase(eti); if (eti != nil) { - if (eti.kind == tykind.TY_TUPLE + let bad: bool = eti.kind == tykind.TY_TUPLE || eti.kind == tykind.TY_STRUCT - || eti.kind == tykind.TY_ARRAY - || eti.kind == tykind.TY_TAGGED) { + || eti.kind == tykind.TY_ARRAY; + // #68: a declared-tagged element graduates to a real + // widen — the decl-aware send (cgtuplelittocursor over + // the param tuple type) left the box words in the + // cursor, so tupstore below carries them. A tagged + // element with no param decl stays rule-7 loud (the + // cursor was filled stamped-keyed). + if (eti.kind == tykind.TY_TAGGED && paramtt == nil) { bad = true; }; + if (bad) { let mne: str = "#32: tuple arg element kind unsupported (nested tuple/struct/array/tagged; rule 7)\n"; os.write(2, mne.ptr, mne.len: u64); os.exit(1); }; }; - // tagged is guarded loud above, so wide-vs-scalar is - // the full slot split here; eslot keeps the stride - // arithmetic on the accessor scale (#22). - let wide: bool = false; - if (tuplit) { wide = nodeisstr(c, et) || nodeisslice(c, et); } - else { wide = isstrtype(c, et) || isslicetype(c, et); }; + // eslot — the full slot stride (str/slice header, tagged + // box, else 8); on the declared (non-tuplit) path tupeslotn + // reads the element TYPE node directly (#68 box-aware, + // mirrors cstage tuple_eslot). A literal element rides the + // wide-vs-scalar split off its VALUE node. let eslot: i32 = 8; - if (wide) { eslot = tyslicesize(): i32; }; + if (tuplit) { + let wide: bool = nodeisstr(c, et) || nodeisslice(c, et); + if (wide) { eslot = tyslicesize(): i32; }; + } else { + eslot = tupeslotn(et); + }; if (isfloattype(c, et)) { sstot += 1; } else { gptot += eslot / 8; }; tsz += eslot; @@ -17136,11 +17165,13 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { for (p != nil) { let et: *node = p.lhs; if (tuplit) { et = p; }; - let wide: bool = false; - if (tuplit) { wide = nodeisstr(c, et) || nodeisslice(c, et); } - else { wide = isstrtype(c, et) || isslicetype(c, et); }; let eslot: i32 = 8; - if (wide) { eslot = tyslicesize(): i32; }; + if (tuplit) { + let wide: bool = nodeisstr(c, et) || nodeisslice(c, et); + if (wide) { eslot = tyslicesize(): i32; }; + } else { + eslot = tupeslotn(et); + }; tupstore(c, gpcur, ssecur, scr + eoff, eslot, et); if (isfloattype(c, et)) { ssecur += 1; } else { gpcur += eslot / 8; }; @@ -28798,19 +28829,38 @@ fn cgcall(c: *cgen, n: *node) void = { stackslots += 1; }; popped += 1; - } else { let tuparg: *node = nodetuplearg(c, a); + } else { + // #68: drain over the PARAM tuple element widths for a + // tuple LITERAL arg (the declared-tagged box words pop + // together with the i64 that follows), mirroring the + // param-aware send; else the source tuple type. + let dptt: *node = nil; + if (a.kind == nkind.N_TUPLE) { if (dparam != nil) { + if (dparam.kind == nkind.N_PARAM && dparam.lhs != nil) { + let dtn2: *node = dparam.lhs; + for (dtn2 != nil && dtn2.kind == nkind.N_TNAME) { + dtn2 = aliaslookup(c, dtn2.str); + }; + if (dtn2 != nil) { if (dtn2.kind == nkind.N_TTUPLE) { dptt = dtn2; }; }; + }; + }; }; + let tuparg: *node = dptt; + if (tuparg == nil) { tuparg = nodetuplearg(c, a); }; if (tuparg != nil) { // #163/#32: drain the tuple's staged words (slot+0 // pushed first) into the SysV arg cursor by SysV class // — a float MOVSD/MOVSS off (SP) into the next XMM, // else POPQ into the next INTEGER arg reg; a slice/str - // its 3 words. Reg overflow loud-stops (rule 7); the + // its 3 words, a declared-tagged box its eslot words + // (#68). Reg overflow loud-stops (rule 7); the // partial-spill stitch is out of scope (twin of #164). // C-t2: nodetuplearg admits ident/literal/unwrap // sources; a literal's elements are VALUE exprs, // classified the way the @tupargscr restage classified - // them (kind-discriminated twin walk). - let tuplit: bool = tuparg.kind == nkind.N_TUPLE; + // them (kind-discriminated twin walk). The param-aware + // path (dptt) walks declared element TYPE nodes. + let tuplit: bool = false; + if (dptt == nil) { if (tuparg.kind == nkind.N_TUPLE) { tuplit = true; }; }; let p: *node = tuparg.list; for (p != nil) { let et: *node = p.lhs; @@ -28832,14 +28882,17 @@ fn cgcall(c: *cgen, n: *node) void = { fpidx += 1; popped += 1; } else { - // tagged is guarded loud at the restage, so - // wide-vs-scalar is the full slot split here - // (#22 accessor scale). - let wide: bool = false; - if (tuplit) { wide = nodeisstr(c, et) || nodeisslice(c, et); } - else { wide = isstrtype(c, et) || isslicetype(c, et); }; + // eslot — full slot split; on the declared + // path tupeslotn reads the element TYPE node + // directly (#68 box-aware), else wide-vs-scalar + // off the literal VALUE node (#22 accessor scale). let eb: i32 = 1; - if (wide) { eb = (tyslicesize() / 8i64): i32; }; + if (tuplit) { + let wide: bool = nodeisstr(c, et) || nodeisslice(c, et); + if (wide) { eb = (tyslicesize() / 8i64): i32; }; + } else { + eb = tupeslotn(et) / 8; + }; if (intidx + eb > 6) { let msg: str = "tuple arg element overflows integer arg regs (DI/SI/DX/CX/R8/R9); stitch out of scope, see #163\n"; os.write(2, msg.ptr, msg.len: u64); @@ -36355,11 +36408,37 @@ fn cgmassign(c: *cgen, n: *node) void = { }; }; - // #64 (filed, rule 7): an N_TUPLE literal rhs rides this decl-less - // cgexpr route, so a declared-TAGGED element's concrete rvalue - // still fills the cursor stamped-keyed (silent skew) — the #57 - // decl wire stops at return/let. - if (n.rhs != nil) { cgexpr(c, n.rhs); }; + // #64: a tuple-LITERAL rhs carries a DECLARED tuple type (built from + // the lvalue binding types) into the cursor fill, so a declared-tagged + // element's concrete rvalue widens into the box instead of riding the + // decl-less stamped-keyed route — the #57 decl wire extended past + // cgmlet/cgreturn to destructure-reassign. A `_` lvalue has no local + // (no declared type node); its decl element stays nil and the + // fill/receive fall back to the rhs literal element's own stamped type + // for the cursor stride (harec `_` advance; pinned by the R3 control). + let litrhs: bool = false; + if (n.rhs != nil) { if (n.rhs.kind == nkind.N_TUPLE) { litrhs = true; }; }; + let synthdecl: *node = nil; + if (litrhs) { + synthdecl = newnode(nkind.N_TTUPLE, n.rhs.file, n.rhs.line, n.rhs.col); + let dtail: *node = nil; + let lb0: *node = n.list; + for (lb0 != nil) { + let w: *node = newnode(nkind.N_TUPLE, n.rhs.file, n.rhs.line, n.rhs.col); + w.lhs = nil; + if (lb0.kind == nkind.N_IDENT) { + let lc0: *local = localfindnode(c, lb0.str); + if (lc0 != nil) { w.lhs = lc0.tnode; }; + }; + w.next = nil; + if (dtail == nil) { synthdecl.list = w; } else { dtail.next = w; }; + dtail = w; + lb0 = lb0.next; + }; + cgtuplelittocursor(c, n.rhs, synthdecl); + } else { + if (n.rhs != nil) { cgexpr(c, n.rhs); }; + }; if (sretrecv > 0) { let scr: i32 = localfind(c, "@sretscr"); @@ -36438,9 +36517,20 @@ fn cgmassign(c: *cgen, n: *node) void = { let l: *node = n.list; let pt: *node = nil; if (rettuple != nil) { pt = rettuple.list; }; + // #64: a tuple-LITERAL rhs keys element WIDTH on the DECLARED lvalue + // type (synthdecl), not the rettuple (nil for a literal); a `_` slot + // (declared type nil) falls back to the rhs literal element's own + // stamped type for the cursor stride. + let dp: *node = nil; + let re: *node = nil; + if (litrhs) { dp = synthdecl.list; re = n.rhs.list; }; for (l != nil) { let tn: *node = nil; - if (pt != nil) { tn = pt.lhs; }; + if (litrhs) { + if (dp != nil && dp.lhs != nil) { tn = dp.lhs; } else { tn = re; }; + } else { + if (pt != nil) { tn = pt.lhs; }; + }; if (isfloattype(c, tn)) { ssetotal = ssetotal + 1; } else { @@ -36448,6 +36538,8 @@ fn cgmassign(c: *cgen, n: *node) void = { }; l = l.next; if (pt != nil) { pt = pt.next; }; + if (dp != nil) { dp = dp.next; }; + if (re != nil) { re = re.next; }; }; if (gptotal > TUPLE_GPCAP) { // AX,DX,CX,R8 capacity // pinned loud-stop, inline like cgen.ww:604 (cstage uses @@ -36467,9 +36559,16 @@ fn cgmassign(c: *cgen, n: *node) void = { l = n.list; pt = nil; if (rettuple != nil) { pt = rettuple.list; }; + dp = nil; + re = nil; + if (litrhs) { dp = synthdecl.list; re = n.rhs.list; }; for (l != nil) { let tn: *node = nil; - if (pt != nil) { tn = pt.lhs; }; + if (litrhs) { + if (dp != nil && dp.lhs != nil) { tn = dp.lhs; } else { tn = re; }; + } else { + if (pt != nil) { tn = pt.lhs; }; + }; let isflt: bool = isfloattype(c, tn); let eslot: i32 = tupeslotn(tn); let off: i32 = 0; @@ -36486,6 +36585,8 @@ fn cgmassign(c: *cgen, n: *node) void = { }; l = l.next; if (pt != nil) { pt = pt.next; }; + if (dp != nil) { dp = dp.next; }; + if (re != nil) { re = re.next; }; }; c.lastwasreturn = 0; return; diff --git a/test/wcc/945_tuple_lit_declblind_run.c b/test/wcc/945_tuple_lit_declblind_run.c new file mode 100644 index 00000000..e7d68357 --- /dev/null +++ b/test/wcc/945_tuple_lit_declblind_run.c @@ -0,0 +1,285 @@ +/* + * 945_tuple_lit_declblind_run — #64 + #68: a tuple LITERAL fills the + * register cursor DECL-BLIND. The #57 decl wire (a declared-tagged element's + * concrete rvalue widens into its box) stopped at N_LET / N_RETURN; the + * other two tuple-literal consumers — destructure-REASSIGN (N_MASSIGN, #64) + * and CALL-ARG send (#68) — still rode the decl-less route, so a declared- + * tagged element was stored/sent WORD-0 ONLY (the box tag never set) and the + * cursor skewed every later element. Both stages, #263 gate-blind: the asm + * was byte-identical cs==ww and both ran wrong, so only RUNTIME catches it. + * + * Fix (both stages, byte-identical per rule 10): N_MASSIGN builds a DECLARED + * tuple type from the lvalue binding types and threads it into the cursor + * fill + receive (a `_` lvalue, never type-stamped, falls back to the rhs + * literal element type for its stride); the call-arg send/restage/drain key + * on the PARAM tuple type (the literal's element-constructed type undercounts + * a wide tagged box). The single residual generic cgexpr(N_TUPLE) arm is + * provably non-widening (the constructed type IS the governing type there). + * + * Every RUN row reads the box PAYLOAD back (binds n and asserts its VALUE), + * not merely which match arm fired — a skew that sets the tag right but + * corrupts the payload still fails. Rows (RUN unless marked ERR): + * massign_tagged `a,b=(7i64,99)` into a:(i32|i64); n==7 ⇒ 1, else 3; + * wrong arm ⇒ 0; garbage-tag fall-through ⇒ 2 (base). + * callarg_tagged `f((7i64,99))` param (ev,i64); match t.0, n==7 ⇒ 1. + * base: garbage tag ⇒ fall-through ⇒ 2. + * massign_blank `_,a=(99,7i64)` — `_` stride + named box widen; the box + * is element 1 (off the `_`-aligned cursor). n==7 ⇒ 1. + * base: word-0-only store corrupts a's tag ⇒ fall-through ⇒ + * 2 (the box's tag sits at word0; the lone scalar word + * overwrites it — verified on pristine abd97e6). + * callarg_drain `g((7i64, 35))` param (ev,i64); g returns (box payload) + * + t.1. This is the #68 DRAIN teeth (ken's verified probe + * shape): the param-aware drain must pop the box's 2 words + * AND the trailing i64 together. base: box is word-0 only + * so the i64 arm never fires (payload 0) ⇒ 0 + 35 = 35; + * post: 7 + 35 = 42. base 35 ≠ 42, and the +35 simultaneously + * proves the trailing arg still lands in its reg. + * nested_tuple_arg [ERR] `h(((1,2),3))` param ((i32,i32),i64): a NESTED- + * TUPLE tuple-arg element has no transport and stays rule-7 + * LOUD (the fix graduates ONLY a declared-tagged element; + * the TY_TUPLE/STRUCT/ARRAY arms are preserved). Asserts the + * build FAILS (nonzero) on BOTH drivers — locks the + * preserved loudness so a future widen-everything regresses. + * + * RUN rows: build+run on BOTH drivers AND cs==ww .s byte-identical (rule 10). + * NNN<950, self-contained (/tmp, no imports), so rule-14's selfhost-sibling + * race does not apply (903/940/945 precedent). + */ +#include +#include +#include +#include +#include +#include + +static int +runwait(const char *cmd) +{ + int rc = system(cmd); + if (rc == -1) return -1; + if (WIFEXITED(rc)) return WEXITSTATUS(rc); + return -1; +} + +static int +slurp_eq(const char *a, const char *b) +{ + FILE *fa = fopen(a, "rb"); + FILE *fb = fopen(b, "rb"); + if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; } + int rc = 0; + for (;;) { + int ca = fgetc(fa), cb = fgetc(fb); + if (ca != cb) { rc = -1; break; } + if (ca == EOF) break; + } + fclose(fa); fclose(fb); + return rc; +} + +/* builderr: the build must FAIL (rule-7 loud); RUN rows leave it 0. */ +struct row { const char *label; const char *src; int want; int builderr; }; + +static const struct row rows[] = { + { "massign_tagged", + "package main;\n" + "type ev = (i32 | i64);\n" + "export fn main() i32 = {\n" + " let a: ev = 5i32;\n" + " let b: i64 = 0;\n" + " a, b = (7i64, 99);\n" + " match (a) {\n" + " case let n: i64 => { if (n == 7) { return 1; }; return 3; };\n" + " case let m: i32 => { return 0; };\n" + " };\n" + " return 2;\n" + "};\n", 1, 0 }, + { "callarg_tagged", + "package main;\n" + "type ev = (i32 | i64);\n" + "fn f(t: (ev, i64)) i32 = {\n" + " match (t.0) {\n" + " case let n: i64 => { if (n == 7) { return 1; }; return 3; };\n" + " case let m: i32 => { return 0; };\n" + " };\n" + " return 2;\n" + "};\n" + "export fn main() i32 = {\n" + " return f((7i64, 99));\n" + "};\n", 1, 0 }, + { "massign_blank", + "package main;\n" + "type ev = (i32 | i64);\n" + "export fn main() i32 = {\n" + " let a: ev = 5i32;\n" + " _, a = (99, 7i64);\n" + " match (a) {\n" + " case let n: i64 => { if (n == 7) { return 1; }; return 3; };\n" + " case let m: i32 => { return 0; };\n" + " };\n" + " return 2;\n" + "};\n", 1, 0 }, + { "callarg_drain", + "package main;\n" + "type ev = (i32 | i64);\n" + "fn g(t: (ev, i64)) i32 = {\n" + " let bp: i64 = 0;\n" + " match (t.0) {\n" + " case let n: i64 => { bp = n; };\n" + " case let m: i32 => { bp = 0; };\n" + " };\n" + " return (bp + t.1): i32;\n" + "};\n" + "export fn main() i32 = {\n" + " return g((7i64, 35));\n" + "};\n", 42, 0 }, + { "nested_tuple_arg", + "package main;\n" + "fn h(t: ((i32, i32), i64)) i32 = {\n" + " return 0;\n" + "};\n" + "export fn main() i32 = {\n" + " return h(((1, 2), 3));\n" + "};\n", 0, 1 }, +}; + +/* build+run via a driver (ww / ww_ww); returns 0 pass, nonzero fail. */ +static int +run_driver(const char *driver, const struct row *r, int i) +{ + char src[96], tmpdir[96], errf[96], cmd[1024]; + snprintf(src, sizeof src, "/tmp/tldb_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/tldb_%d_d_%d", getpid(), i); + snprintf(errf, sizeof errf, "/tmp/tldb_%d_e_%d", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -1; + fputs(r->src, f); + fclose(f); + + mkdir(tmpdir, 0755); + snprintf(cmd, sizeof cmd, "cd %s && %s build %s >/dev/null 2>%s", + tmpdir, driver, src, errf); + int brc = runwait(cmd); + /* ERR row: the rule-7 loud arm must fire — build FAILS (nonzero). A + * clean build means the preserved loudness regressed. */ + if (r->builderr) { + int ok = (brc != 0); + if (!ok) + fprintf(stderr, "row[%s]: %s built clean, expected " + "rule-7 loud build-fail\n", r->label, driver); + unlink(src); unlink(errf); rmdir(tmpdir); + return ok ? 0 : 1; + } + if (brc != 0) { + fprintf(stderr, "row[%s]: build via %s failed\n", + r->label, driver); + unlink(src); unlink(errf); rmdir(tmpdir); + return -1; + } + + const char *base = strrchr(src, '/'); + base = base ? base + 1 : src; + char outbin[256]; + snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); + char *dot = strrchr(outbin, '.'); + if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; + int got = runwait(outbin); + + unlink(src); unlink(outbin); unlink(errf); rmdir(tmpdir); + if (got != r->want) { + fprintf(stderr, "row[%s]: %s exit %d, want %d\n", + r->label, driver, got, r->want); + return 1; + } + return 0; +} + +/* cs==ww .s byte-id (rule 10). */ +static int +byteid(const char *w6c, const char *w6c_ww, const struct row *r, int i) +{ + char src[96], cs_s[96], ws_s[96], cmd[1024]; + snprintf(src, sizeof src, "/tmp/tldb_bi_%d_%d.ww", getpid(), i); + snprintf(cs_s, sizeof cs_s, "/tmp/tldb_bi_%d_%d_cs.s", getpid(), i); + snprintf(ws_s, sizeof ws_s, "/tmp/tldb_bi_%d_%d_ww.s", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -1; + fputs(r->src, f); + fclose(f); + + int rc = 0; + snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, cs_s, src); + if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: w6c failed\n", r->label); rc = 1; } + else { + snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c_ww, ws_s, src); + if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: w6c_ww failed\n", r->label); rc = 1; } + else if (slurp_eq(cs_s, ws_s) != 0) { + fprintf(stderr, "row[%s]: cstage/wwstage .s DIFFER " + "(rule-10 byte-id)\n", r->label); + rc = 1; + } + } + unlink(src); unlink(cs_s); unlink(ws_s); + return rc; +} + +int +main(void) +{ + const char *bin = getenv("BIN"); + if (!bin) bin = "out/bin"; + char absbin[512]; + if (bin[0] != '/') { + char cwd[256]; + if (getcwd(cwd, sizeof cwd) == NULL) return 1; + snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); + bin = absbin; + } + + char cdrv[640], wdrv[640], w6c[640], w6c_ww[640]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); + snprintf(w6c, sizeof w6c, "%s/w6c", bin); + snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin); + + struct { const char *name; const char *path; int gated; } + drivers[] = { + { "cstage", cdrv, 0 }, + { "wwstage", wdrv, 1 }, + { NULL, NULL, 0 }, + }; + + int n = (int)(sizeof rows / sizeof rows[0]); + int total = 0, fail = 0; + + for (int d = 0; drivers[d].name; d++) { + if (drivers[d].gated && access(drivers[d].path, X_OK) != 0) { + fprintf(stderr, "tuple_lit_declblind: skip %s (no %s)\n", + drivers[d].name, drivers[d].path); + continue; + } + for (int i = 0; i < n; i++) { + total++; + if (run_driver(drivers[d].path, &rows[i], i) != 0) fail++; + } + } + + if (access(w6c_ww, X_OK) == 0) { + for (int i = 0; i < n; i++) { + if (rows[i].builderr) continue; /* no .s to compare */ + total++; + if (byteid(w6c, w6c_ww, &rows[i], i) != 0) fail++; + } + } + + if (fail) { + fprintf(stderr, "tuple_lit_declblind: %d/%d checks failed\n", + fail, total); + return 1; + } + printf("tuple_lit_declblind: %d/%d ok\n", total, total); + return 0; +}