From 5a0427ef32e6bb9370b862c548ee4180488defe8 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Mon, 25 May 2026 09:34:05 +0900 Subject: [PATCH] cgen: N-ary tuple destructure positional store + loud-stop (both stages, #83) Replace the str-only XOR (e0_is_str ^ e1_is_str) at the tuple send (N_RETURN) and receive (N_MLET/N_MASSIGN) sites with a positional per-element register cursor, mirroring harec create_unpack_bindings (ref/harec/src/check.c:1354-1416). Each element rides consecutive eightbytes over [AX,DX,CX,R8]; a slice/str rides its 3-word {ptr,len,cap} header (ref/hare/rt/ensure.ha:4-8), a scalar rides 1. Send and receive walk the SAME type-table widths so element->register agrees. This routes []u8 elements through the 3-word path (the XOR was slice-blind, dropping len+cap to the scalar fallback) and closes the pre-existing (scalar,slice) cs!=ww divergence by construction. cstage and wwstage emit byte-identical asm. Both receive sites derive each element's width from the rhs tuple's element types (n->rhs->type->params / the callee return type) -- the SAME producer view the send site walks -- NOT the binding type: a `_` lvalue is an N_IDENT with empty str the checker never type-stamps, so a binding-typed width mis-sized a wide `_` and desynced the cursor for the next element (cstage read DX, wwstage R8). harec `_` skips the store but CONSUMES its tuple offset; the cursor advance honours that. Loud-stop (rule 7): the register file holds 4 eightbytes; a tuple whose elements sum to >4 (([]u8,[]u8)/(str,str)=6) cannot be register-returned, so the send site aborts at compile time citing the return-ABI capacity (#10) rather than silently miscompiling. The receive loop guards the same predicate (defense-in-depth). Routed through each stage's EXISTING pinned-fatal idiom: cstage fatal() (cmd/wcc/err.c), wwstage the inline os.write(2,...)+os.exit(1) at cgen.ww:604 -- no new diagnostics path. N_MASSIGN (`a,b=f()`, bare comma, pre-declared) is a retained ww-EXTENSION beyond Hare's binding-only tuple-unpack (Go/rob-pike multi-assign, rule-9 carve-out); the loop covers it identically to N_MLET. Test 945_tuple_nary_destructure_run: (i64,[]u8)+(i64,str) store+read len/cap for both N_MLET and N_MASSIGN, a single-str control, a wide- first blank `_,a=f()` row (the cursor-desync discriminator), and a ([]u8,[]u8) row asserting the loud BUILDERR carries the cited diagnostic; dual ww/ww_ww drivers. --- Makefile | 7 + cmd/w6c/cgen.c | 255 ++++++------ selfhost/cmd/w6c/main.combined.ww | 470 ++++++++++------------ selfhost/cmd/wcc/cgenstmt.ww | 470 ++++++++++------------ selfhost/cmd/wwdump/main.combined.ww | 470 ++++++++++------------ test/wcc/945_tuple_nary_destructure_run.c | 340 ++++++++++++++++ 6 files changed, 1116 insertions(+), 896 deletions(-) create mode 100644 test/wcc/945_tuple_nary_destructure_run.c diff --git a/Makefile b/Makefile index d75762a3..7ff4e480 100644 --- a/Makefile +++ b/Makefile @@ -266,6 +266,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_subslice_cap_run \ $(BIN)/test_subslice_ptresz_run \ $(BIN)/test_deref_slice_store_run \ + $(BIN)/test_tuple_nary_destructure_run \ $(BIN)/test_str_forrange_loopvar_run \ $(BIN)/test_composite_call_arg \ $(BIN)/test_composite_call_arg_run \ @@ -711,6 +712,12 @@ $(BIN)/test_deref_slice_store_run: test/wcc/944_deref_slice_store_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_tuple_nary_destructure_run: test/wcc/945_tuple_nary_destructure_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 $@ $< + $(BIN)/test_composite_call_arg: test/wcc/723_composite_call_arg.c \ $(BIN)/w6c $(BIN)/w6c_ww | $(BIN) $(CC) $(CFLAGS) -o $@ $< diff --git a/cmd/w6c/cgen.c b/cmd/w6c/cgen.c index a7184f48..06980c05 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -184,6 +184,20 @@ node_isslice(Node *n) return n && type_isslice(n->type); } +/* #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 + * scalar rides 1. SEND (N_RETURN) and RECEIVE (N_MLET/N_MASSIGN) walk the + * SAME widths so element->register agrees — mirrors harec's + * create_unpack_bindings element walk (ref/harec/src/check.c:1354-1416). */ +static const int tuple_rseq[] = { D_AX, D_DX, D_CX, D_R8 }; + +static int +tuple_ebytes(int wide) +{ + return wide ? (int)(ty_str->size / 8) : 1; +} + static int type_isf32(Type *t) { @@ -7256,45 +7270,37 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) break; } if (n->lhs && n->lhs->kind == N_TUPLE) { - /* 2-tuple ABI, word-indexed AX→DX→CX→R8 (the SAME - * register sequence as the tagged-union return; the - * tuple just fills it positionally): - * (scalar, scalar) — AX = e0, DX = e1. (16B, fits SysV.) - * (scalar, str) — AX = scalar elem, DX = str.ptr, - * CX = str.len, R8 = str.cap. (32B.) - * (str, scalar) — same regs, type-keyed not position-keyed. - * - * str IS []u8 (24B), so a (scalar, str) tuple is 32B and - * rides AX:DX:CX:R8 — the cap is the 4th word, matching the - * tagged-union return that already uses R8 for slot+24 - * (#1/Phase 3, task #5). Receive sites destructure off the - * same regs. */ - Node *e0 = n->lhs->list; - Node *e1 = e0 ? e0->next : NULL; - if (e1 && e1->next == NULL) { - int e0_is_str = node_isstr(e0); - int e1_is_str = node_isstr(e1); - if (e0_is_str ^ e1_is_str) { - Node *strn = e0_is_str ? e0 : e1; - Node *scaln = e0_is_str ? e1 : e0; - cgexpr(c, scaln, *locals); /* AX = scalar */ - ins1(c, A_PUSHQ, areg(D_AX)); - cgexpr(c, strn, *locals); /* AX=ptr, BX=len, CX=cap */ - ins2(c, A_MOVQ, areg(D_CX), areg(D_R8)); - ins2(c, A_MOVQ, areg(D_BX), areg(D_CX)); - ins2(c, A_MOVQ, areg(D_AX), areg(D_DX)); - ins1(c, A_POPQ, areg(D_AX)); - } else { - cgexpr(c, e1, *locals); - ins1(c, A_PUSHQ, areg(D_AX)); - cgexpr(c, e0, *locals); - ins1(c, A_POPQ, areg(D_DX)); + /* #83: positional per-element register-return. Walk the + * tuple's elements (harec create_unpack_bindings, + * ref/harec/src/check.c:1354-1416); each rides consecutive + * eightbytes over tuple_rseq[]. A slice/str rides its 3-word + * {ptr,len,cap} header (ref/hare/rt/ensure.ha:4-8), cgexpr + * leaving it in (AX,BX,CX); a scalar rides 1 word in AX. + * Spill each element's word(s) L→R, then pop into the + * cursor's registers in reverse so positional slot i lands in + * tuple_rseq[i] — (scalar,str) keeps the historical AX + + * DX,CX,R8 layout, and the SAME cursor drives the receive + * sites. Over-capacity is a loud stop (return-ABI #10), never + * a silent drop. */ + int cap = (int)(sizeof tuple_rseq / sizeof tuple_rseq[0]); + int total = 0; + for (Node *e = n->lhs->list; e; e = e->next) + total += tuple_ebytes(node_isstr(e) || node_isslice(e)); + if (total > cap) + fatal("tuple return exceeds register-return ABI " + "capacity (%d eightbytes); see return-ABI #10", + cap); + for (Node *e = n->lhs->list; e; e = e->next) { + int wide = node_isstr(e) || node_isslice(e); + cgexpr(c, e, *locals); /* scalar=AX; slice/str=AX,BX,CX */ + ins1(c, A_PUSHQ, areg(D_AX)); /* scalar / .ptr */ + if (wide) { + ins1(c, A_PUSHQ, areg(D_BX)); /* .len */ + ins1(c, A_PUSHQ, areg(D_CX)); /* .cap */ } - } else { - /* >2-tuple not yet implemented; fall back to first elem */ - if (e0) cgexpr(c, e0, *locals); - else cgexpr_int(c, 0); } + for (int i = total - 1; i >= 0; i--) + ins1(c, A_POPQ, areg(tuple_rseq[i])); } else if (n->lhs) { cgexpr(c, n->lhs, *locals); } else { @@ -7458,105 +7464,104 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) break; } case N_MLET: { - /* eval rhs; consume the per-type return-ABI registers. - * (scalar, scalar) — AX → l0, DX → l1. - * (scalar, str) — AX → scalar slot, (DX, CX, R8) → str slot - * as (.ptr, .len, .cap). Position-agnostic. - * Local sizing comes from each l->type so the str slot gets - * the full 24B; without this, only DX would land and the - * len/cap halves (CX/R8) would have nowhere to go. - * str IS []u8 (24B): the cap rides R8 (#1/Phase 3, task #5). - * one-str only; two-str destructure is gap (task #22). */ + /* #83: positional per-element destructure store. The rhs left + * each tuple element in the register-return cursor (see N_RETURN + * / harec create_unpack_bindings, ref/harec/src/check.c:1354-1416); + * walk the bindings over the SAME cursor and store each at its + * own width — a slice/str's 3-word {ptr,len,cap} header + * (ref/hare/rt/ensure.ha:4-8) into a header-sized slot (sized + * from u->size so #1 propagates), a scalar's 1 word into an 8B + * slot. Over-capacity is a loud stop, not a silent drop. */ cgexpr(c, n->rhs, *locals); - Node *l0 = n->list; - Node *l1 = l0 ? l0->next : NULL; - Type *t0 = l0 ? l0->type : NULL; - Type *t1 = l1 ? l1->type : NULL; - Type *u0 = (t0 && t0->kind == TY_NAMED) ? t0->under : t0; - Type *u1 = (t1 && t1->kind == TY_NAMED) ? t1->under : t1; - int s0_is_str = u0 && u0->kind == TY_STR; - int s1_is_str = u1 && u1->kind == TY_STR; - if (l0 && l1 && (s0_is_str ^ s1_is_str)) { - /* #60: route str-slot width through ty_str->size so #1 - * propagates here. Scalar side keeps the 8B slot. */ - int sz0 = s0_is_str ? (int)u0->size : 8; - int sz1 = s1_is_str ? (int)u1->size : 8; - int off0 = localoff(c, locals, l0->str, sz0, frame); - int off1 = localoff(c, locals, l1->str, sz1, frame); - if (s0_is_str) { - /* l0 is str: ptr=DX, len=CX, cap=R8. l1 scalar = AX. */ - ins2(c, A_MOVQ, areg(D_DX), amem(D_BP, off0 + 0)); - ins2(c, A_MOVQ, areg(D_CX), amem(D_BP, off0 + 8)); - ins2(c, A_MOVQ, areg(D_R8), amem(D_BP, off0 + 16)); - ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, off1)); + int cap = (int)(sizeof tuple_rseq / sizeof tuple_rseq[0]); + int total = 0; + for (Node *l = n->list; l; l = l->next) { + Type *t = l->type; + Type *u = (t && t->kind == TY_NAMED) ? t->under : t; + int wide = u && (u->kind == TY_SLICE || u->kind == TY_STR); + total += tuple_ebytes(wide); + } + if (total > cap) + fatal("tuple destructure exceeds register-return ABI " + "capacity (%d eightbytes); see return-ABI #10", cap); + int cur = 0; + for (Node *l = n->list; l; l = l->next) { + Type *t = l->type; + Type *u = (t && t->kind == TY_NAMED) ? t->under : t; + int wide = u && (u->kind == TY_SLICE || u->kind == TY_STR); + int sz = wide ? (int)u->size : 8; + int off = localoff(c, locals, l->str, sz, frame); + if (wide) { + ins2(c, A_MOVQ, areg(tuple_rseq[cur + 0]), + amem(D_BP, off + 0)); /* .ptr */ + ins2(c, A_MOVQ, areg(tuple_rseq[cur + 1]), + amem(D_BP, off + 8)); /* .len */ + ins2(c, A_MOVQ, areg(tuple_rseq[cur + 2]), + amem(D_BP, off + 16)); /* .cap */ } else { - /* l0 is scalar; l1 is str. */ - ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, off0)); - ins2(c, A_MOVQ, areg(D_DX), amem(D_BP, off1 + 0)); - ins2(c, A_MOVQ, areg(D_CX), amem(D_BP, off1 + 8)); - ins2(c, A_MOVQ, areg(D_R8), amem(D_BP, off1 + 16)); + ins2(c, A_MOVQ, areg(tuple_rseq[cur]), + amem(D_BP, off)); } - break; - } - ins1(c, A_PUSHQ, areg(D_DX)); /* save 2nd while we store 1st */ - if (l0) { - int off = localoff(c, locals, l0->str, 8, frame); - ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, off)); - } - ins1(c, A_POPQ, areg(D_DX)); - if (l1) { - int off = localoff(c, locals, l1->str, 8, frame); - ins2(c, A_MOVQ, areg(D_DX), amem(D_BP, off)); + cur += tuple_ebytes(wide); } break; } case N_MASSIGN: { + /* #83: positional per-element destructure REASSIGN. Same cursor + * as N_MLET (and N_RETURN; harec create_unpack_bindings, + * ref/harec/src/check.c:1354-1416), but the slots already exist + * (reassignment) so localfind them. Element WIDTH comes from the + * rhs tuple's element types (n->rhs->type->params) — the SAME + * producer source the SEND site walks and wwstage reads via the + * callee return type — NOT the binding type: a `_` lvalue is an + * N_IDENT with empty str the checker never type-stamps (it skips + * cexpr on `_`, cmd/wcc/check.c N_MASSIGN), so a binding-typed + * width would mis-size a wide `_` and DESYNC the cursor for the + * next element. harec `_` skips the store but CONSUMES its tuple + * offset; the cursor advance below honours that. A wide element's + * 3-word {ptr,len,cap} header (ref/hare/rt/ensure.ha:4-8) is stored + * at its slot. This bare-comma `a, s = f()` multi-assign is a + * retained ww-EXTENSION beyond Hare (Hare tuple-unpack is binding- + * only); ww keeps the Go/rob-pike multi-assign idiom — rule-9 + * carve-out. Over-capacity is a loud stop, not a silent drop. */ cgexpr(c, n->rhs, *locals); - Node *l0 = n->list; - Node *l1 = l0 ? l0->next : NULL; - /* one-str only; two-str destructure is gap (task #22). The str - * tuple element's 24B slot already exists (reassignment), so - * localfind it and mirror N_MLET's (DX,CX,R8)→(.ptr,.len,.cap) - * routing; the bare scalar fallback below would store only DX - * and drop len/cap. Scalar side stays AX→slot. */ - Type *t0 = l0 ? l0->type : NULL; - Type *t1 = l1 ? l1->type : NULL; - Type *u0 = (t0 && t0->kind == TY_NAMED) ? t0->under : t0; - Type *u1 = (t1 && t1->kind == TY_NAMED) ? t1->under : t1; - int s0_is_str = u0 && u0->kind == TY_STR; - int s1_is_str = u1 && u1->kind == TY_STR; - if (l0 && l1 && l0->kind == N_IDENT && l1->kind == N_IDENT - && (s0_is_str ^ s1_is_str)) { - int off0 = localfind(*locals, l0->str); - int off1 = localfind(*locals, l1->str); - if (off0 != 0 && off1 != 0) { - if (s0_is_str) { - /* l0 is str: ptr=DX, len=CX, cap=R8. l1 scalar = AX. */ - ins2(c, A_MOVQ, areg(D_DX), amem(D_BP, off0 + 0)); - ins2(c, A_MOVQ, areg(D_CX), amem(D_BP, off0 + 8)); - ins2(c, A_MOVQ, areg(D_R8), amem(D_BP, off0 + 16)); - ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, off1)); + Type *rt = n->rhs ? n->rhs->type : NULL; + Type *ru = (rt && rt->kind == TY_NAMED) ? rt->under : rt; + Tparam *tp0 = (ru && ru->kind == TY_TUPLE) ? ru->params : NULL; + int cap = (int)(sizeof tuple_rseq / sizeof tuple_rseq[0]); + int total = 0; + for (Tparam *tp = tp0; tp; tp = tp->next) { + Type *u = (tp->type && tp->type->kind == TY_NAMED) + ? tp->type->under : tp->type; + total += tuple_ebytes(u && (u->kind == TY_SLICE + || u->kind == TY_STR)); + } + if (total > cap) + fatal("tuple destructure exceeds register-return ABI " + "capacity (%d eightbytes); see return-ABI #10", cap); + int cur = 0; + Tparam *tp = tp0; + for (Node *l = n->list; l; l = l->next) { + Type *et = tp ? tp->type : NULL; + Type *u = (et && et->kind == TY_NAMED) ? et->under : et; + int wide = u && (u->kind == TY_SLICE || u->kind == TY_STR); + int off = (l->kind == N_IDENT) + ? localfind(*locals, l->str) : 0; + if (off != 0) { + if (wide) { + ins2(c, A_MOVQ, areg(tuple_rseq[cur + 0]), + amem(D_BP, off + 0)); /* .ptr */ + ins2(c, A_MOVQ, areg(tuple_rseq[cur + 1]), + amem(D_BP, off + 8)); /* .len */ + ins2(c, A_MOVQ, areg(tuple_rseq[cur + 2]), + amem(D_BP, off + 16)); /* .cap */ } else { - /* l0 is scalar; l1 is str. */ - ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, off0)); - ins2(c, A_MOVQ, areg(D_DX), amem(D_BP, off1 + 0)); - ins2(c, A_MOVQ, areg(D_CX), amem(D_BP, off1 + 8)); - ins2(c, A_MOVQ, areg(D_R8), amem(D_BP, off1 + 16)); + ins2(c, A_MOVQ, areg(tuple_rseq[cur]), + amem(D_BP, off)); } - break; } - } - ins1(c, A_PUSHQ, areg(D_DX)); - if (l0 && l0->kind == N_IDENT) { - int off = localfind(*locals, l0->str); - if (off != 0) - ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, off)); - } - ins1(c, A_POPQ, areg(D_DX)); - if (l1 && l1->kind == N_IDENT) { - int off = localfind(*locals, l1->str); - if (off != 0) - ins2(c, A_MOVQ, areg(D_DX), amem(D_BP, off)); + cur += tuple_ebytes(wide); + if (tp) tp = tp->next; } break; } diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index e51f730d..1e736f7b 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -20062,47 +20062,134 @@ fn rundefers(c: *cgen) void = { return; }; +// #83: positional tuple register-return ABI. Tuple elements ride +// consecutive eightbytes over [AX,DX,CX,R8] (tupreg by index); a +// slice/str rides its 3-word {ptr,len,cap} header (tyslicesize SSoT, +// ref/hare/rt/ensure.ha:4-8), a scalar rides 1. SEND (cgreturn) and +// RECEIVE (cgmlet/cgmassign) walk the SAME widths so element->register +// agrees — mirrors harec create_unpack_bindings +// (ref/harec/src/check.c:1354-1416). Capacity is 4 (AX,DX,CX,R8). +fn tupreg(i: i32) str = { + if (i == 0) { return "AX"; }; + if (i == 1) { return "DX"; }; + if (i == 2) { return "CX"; }; + return "R8"; +}; + +fn tupebytes(wide: bool) i32 = { + if (wide) { return (tyslicesize() / 8i64): i32; }; + return 1; +}; + +// rettupleof — the N_TTUPLE return-type node of an N_CALL rhs (else nil). +// wwstage has no checker, so the receive sites read each tuple element's +// width from the called fn's declared return type. Mirrors the callee +// resolution shared by cgmlet/cgmassign. +fn rettupleof(c: *cgen, rhs: *node) *node = { + if (rhs == nil) { return nil; }; + if (rhs.kind != nkind.N_CALL) { return nil; }; + let callee: *node = rhs.lhs; + if (callee == nil) { return nil; }; + let cnm: str; + cnm.ptr = nil; cnm.len = 0; + let cmod: str; + cmod.ptr = nil; cmod.len = 0; + if (callee.kind == nkind.N_IDENT) { + cnm = callee.str; + cmod = c.curmod; + }; + if (callee.kind == nkind.N_DOT) { + cnm = callee.str; + if (callee.lhs != nil) { + if (callee.lhs.kind == nkind.N_IDENT) { + cmod = callee.lhs.str; + }; + }; + }; + if (cnm.len == 0) { return nil; }; + let rtyp: *node = fnretlookupmod(c, cnm, cmod); + if (rtyp == nil) { return nil; }; + if (rtyp.kind != nkind.N_TTUPLE) { return nil; }; + return rtyp; +}; + +// tupstore — store the tuple element at register-cursor `cur` into the +// BP-relative slot at `off`. A slice/str stores its 3-word {ptr,len,cap} +// header (ref/hare/rt/ensure.ha:4-8) at off/+8/+16 from consecutive +// cursor registers; a scalar stores 1 word. Byte-identical to the cstage +// N_MLET/N_MASSIGN store (cmd/w6c/cgen.c). +fn tupstore(cur: i32, off: i32, wide: bool) void = { + if (wide) { + emitline("\tMOVQ\t"); + emitline(tupreg(cur + 0)); + emitline(", "); + emitoff(off: i64); + emitline("(BP)\n"); + emitline("\tMOVQ\t"); + emitline(tupreg(cur + 1)); + emitline(", "); + emitoff((off + 8): i64); + emitline("(BP)\n"); + emitline("\tMOVQ\t"); + emitline(tupreg(cur + 2)); + emitline(", "); + emitoff((off + 16): i64); + emitline("(BP)\n"); + return; + }; + emitline("\tMOVQ\t"); + emitline(tupreg(cur)); + emitline(", "); + emitoff(off: i64); + emitline("(BP)\n"); +}; + fn cgreturn(c: *cgen, n: *node) void = { rundefers(c); let rhs: *node = n.lhs; if (rhs != nil) { - // Tuple return `return a, b;`, word-indexed AX→DX→CX→R8 (the - // SAME register sequence as the tagged-union return below; the - // tuple just fills it positionally): - // (scalar, scalar) — AX = v0, DX = v1. - // (scalar, str) / (str, scalar) — AX = scalar elem, - // DX = str.ptr, CX = str.len, R8 = str.cap. - // str IS []u8 (24B) → 32B tuple; cap rides R8, matching the - // tagged-union return that already uses R8 for slot+24 - // (#1/Phase 3, task #5). Receive sites destructure off the - // same regs regardless of position. + // #83: positional per-element register-return (harec + // create_unpack_bindings, ref/harec/src/check.c:1354-1416). Each + // element rides consecutive eightbytes over [AX,DX,CX,R8] + // (tupreg); a slice/str rides its 3-word {ptr,len,cap} header + // (ref/hare/rt/ensure.ha:4-8), cgexpr leaving it in (AX,BX,CX); a + // scalar rides 1 word in AX. Spill each element L->R, then pop + // into the cursor's registers in reverse so positional slot i + // lands in tupreg(i) — (scalar,str) keeps the historical AX + + // DX,CX,R8. The SAME cursor drives the receive sites. Over- + // capacity is a loud stop (return-ABI #10), never a silent drop. if (rhs.kind == nkind.N_TUPLE) { - let v: *node = rhs.list; - if (v != nil) { - let v2: *node = v.next; - if (v2 != nil) { - let v0_is_str: bool = nodeisstr(c, v); - let v1_is_str: bool = nodeisstr(c, v2); - if ((v0_is_str || v1_is_str) && !(v0_is_str && v1_is_str)) { - let strn: *node = v; - let scaln: *node = v2; - if (v1_is_str) { strn = v2; scaln = v; }; - cgexpr(c, scaln); - emitline("\tPUSHQ\tAX\n"); - cgexpr(c, strn); - emitline("\tMOVQ\tCX, R8\n"); - emitline("\tMOVQ\tBX, CX\n"); - emitline("\tMOVQ\tAX, DX\n"); - emitline("\tPOPQ\tAX\n"); - } else { - cgexpr(c, v2); - emitline("\tPUSHQ\tAX\n"); - cgexpr(c, v); - emitline("\tPOPQ\tDX\n"); - }; - } else { - cgexpr(c, v); + let total: i32 = 0; + let e: *node = rhs.list; + for (e != nil) { + let wide: bool = nodeisstr(c, e) || nodeisslice(c, e); + total = total + tupebytes(wide); + e = e.next; + }; + if (total > 4) { // AX,DX,CX,R8 capacity + // pinned loud-stop, inline like cgen.ww:604 (cstage + // uses fatal(), err.c) — surface, don't corrupt. + let msg: str = "tuple return exceeds register-return ABI capacity (4 eightbytes); see return-ABI #10\n"; + os.write(2, msg.ptr, msg.len: u64); + os.exit(1); + }; + e = rhs.list; + for (e != nil) { + let wide: bool = nodeisstr(c, e) || nodeisslice(c, e); + cgexpr(c, e); + emitline("\tPUSHQ\tAX\n"); // scalar / .ptr + if (wide) { + emitline("\tPUSHQ\tBX\n"); // .len + emitline("\tPUSHQ\tCX\n"); // .cap }; + e = e.next; + }; + let i: i32 = total - 1; + for (i >= 0) { + emitline("\tPOPQ\t"); + emitline(tupreg(i)); + emitline("\n"); + i = i - 1; }; emitline("\tMOVQ\tBP, SP\n"); emitline("\tPOPQ\tBP\n"); @@ -21152,131 +21239,58 @@ fn cgfor(c: *cgen, n: *node) void = { // cmd/w6c/cgen.c:2424-2440. Lvalues beyond two are dropped (same // as C — no fixture uses >2 today). fn cgmassign(c: *cgen, n: *node) void = { - let l0: *node = n.list; - let l1: *node = nil; - if (l0 != nil) { l1 = l0.next; }; - - // one-str only; two-str destructure is gap (task #22). The str - // tuple element's 24B slot already exists (reassignment), so - // localfind it and mirror cgmlet's (DX,CX,R8)->(.ptr,.len,.cap) - // routing; the bare scalar fallback below would store only DX and - // drop len/cap. wwstage has no checker, so str-ness comes from the - // called fn's return-type tuple element (as in cgmlet). - let p0t: *node = nil; - let p1t: *node = nil; - let rhs: *node = n.rhs; - if (rhs != nil) { - if (rhs.kind == nkind.N_CALL) { - let callee: *node = rhs.lhs; - if (callee != nil) { - let cnm: str; - cnm.ptr = nil; cnm.len = 0; - let cmod: str; - cmod.ptr = nil; cmod.len = 0; - if (callee.kind == nkind.N_IDENT) { - cnm = callee.str; - cmod = c.curmod; - }; - if (callee.kind == nkind.N_DOT) { - cnm = callee.str; - if (callee.lhs != nil) { - if (callee.lhs.kind == nkind.N_IDENT) { - cmod = callee.lhs.str; - }; - }; - }; - if (cnm.len > 0) { - let rtyp: *node = fnretlookupmod(c, cnm, cmod); - if (rtyp != nil) { - if (rtyp.kind == nkind.N_TTUPLE) { - let pp: *node = rtyp.list; - if (pp != nil) { - p0t = pp.lhs; - if (pp.next != nil) { - p1t = pp.next.lhs; - }; - }; - }; - }; - }; - }; - }; - }; - - let s0_is_str: bool = isstrtype(c, p0t); - let s1_is_str: bool = isstrtype(c, p1t); + // #83: positional per-element destructure REASSIGN. Same cursor as + // cgmlet (and cgreturn; harec create_unpack_bindings, + // ref/harec/src/check.c:1354-1416), but the slots already exist + // (reassignment) so localfind them. wwstage has no checker, so each + // element's width comes from the called fn's return-type tuple + // element (N_TTUPLE param) walked in lockstep with the bindings; a + // slice/str rides its 3-word {ptr,len,cap} header + // (ref/hare/rt/ensure.ha:4-8). A missing/non-ident binding consumes + // its register slot without storing (mirrors harec `_`). This bare- + // comma `a, s = f()` multi-assign is a retained ww-EXTENSION beyond + // Hare (Hare tuple-unpack is binding-only); ww keeps the Go/rob-pike + // multi-assign idiom — rule-9 carve-out. Over-capacity loud-stops. + let rettuple: *node = rettupleof(c, n.rhs); if (n.rhs != nil) { cgexpr(c, n.rhs); }; - if (l0 != nil) { - if (l1 != nil) { - if (l0.kind == nkind.N_IDENT) { - if (l1.kind == nkind.N_IDENT) { - if (s0_is_str != s1_is_str) { - let off0: i32 = localfind(c, l0.str); - let off1: i32 = localfind(c, l1.str); - if (off0 != 0) { - if (off1 != 0) { - if (s0_is_str) { - // l0 str: ptr=DX, len=CX, cap=R8. l1 scalar = AX. - emitline("\tMOVQ\tDX, "); - emitoff(off0: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tCX, "); - emitoff((off0 + 8): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tR8, "); - emitoff((off0 + 16): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tAX, "); - emitoff(off1: i64); - emitline("(BP)\n"); - } else { - // l0 scalar; l1 str: ptr=DX, len=CX, cap=R8. - emitline("\tMOVQ\tAX, "); - emitoff(off0: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tDX, "); - emitoff(off1: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tCX, "); - emitoff((off1 + 8): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tR8, "); - emitoff((off1 + 16): i64); - emitline("(BP)\n"); - }; - c.lastwasreturn = 0; - return; - }; - }; - }; - }; - }; - }; + let total: i32 = 0; + let l: *node = n.list; + let pt: *node = nil; + if (rettuple != nil) { pt = rettuple.list; }; + for (l != nil) { + let tn: *node = nil; + if (pt != nil) { tn = pt.lhs; }; + let wide: bool = isstrtype(c, tn) || isslicetype(c, tn); + total = total + tupebytes(wide); + l = l.next; + if (pt != nil) { pt = pt.next; }; + }; + if (total > 4) { // AX,DX,CX,R8 capacity + // pinned loud-stop, inline like cgen.ww:604 (cstage uses + // fatal(), err.c) — surface, don't corrupt. + let msg: str = "tuple destructure exceeds register-return ABI capacity (4 eightbytes); see return-ABI #10\n"; + os.write(2, msg.ptr, msg.len: u64); + os.exit(1); }; - emitline("\tPUSHQ\tDX\n"); - if (l0 != nil) { - if (l0.kind == nkind.N_IDENT) { - let off: i32 = localfind(c, l0.str); - if (off != 0) { - emitline("\tMOVQ\tAX, "); - emitoff(off: i64); - emitline("(BP)\n"); - }; - }; - }; - emitline("\tPOPQ\tDX\n"); - if (l1 != nil) { - if (l1.kind == nkind.N_IDENT) { - let off: i32 = localfind(c, l1.str); - if (off != 0) { - emitline("\tMOVQ\tDX, "); - emitoff(off: i64); - emitline("(BP)\n"); - }; + let cur: i32 = 0; + l = n.list; + pt = nil; + if (rettuple != nil) { pt = rettuple.list; }; + for (l != nil) { + let tn: *node = nil; + if (pt != nil) { tn = pt.lhs; }; + let wide: bool = isstrtype(c, tn) || isslicetype(c, tn); + let off: i32 = 0; + if (l.kind == nkind.N_IDENT) { off = localfind(c, l.str); }; + if (off != 0) { + tupstore(cur, off, wide); }; + cur = cur + tupebytes(wide); + l = l.next; + if (pt != nil) { pt = pt.next; }; }; c.lastwasreturn = 0; return; @@ -21297,115 +21311,57 @@ fn cgmlet(c: *cgen, n: *node) void = { let rhs: *node = n.rhs; if (rhs == nil) { return; }; - let p0t: *node = nil; - let p1t: *node = nil; - if (rhs.kind == nkind.N_CALL) { - let callee: *node = rhs.lhs; - if (callee != nil) { - let cnm: str; - cnm.ptr = nil; cnm.len = 0; - let cmod: str; - cmod.ptr = nil; cmod.len = 0; - if (callee.kind == nkind.N_IDENT) { - cnm = callee.str; - cmod = c.curmod; - }; - if (callee.kind == nkind.N_DOT) { - cnm = callee.str; - if (callee.lhs != nil) { - if (callee.lhs.kind == nkind.N_IDENT) { - cmod = callee.lhs.str; - }; - }; - }; - if (cnm.len > 0) { - let rtyp: *node = fnretlookupmod(c, cnm, cmod); - if (rtyp != nil) { - if (rtyp.kind == nkind.N_TTUPLE) { - let pp: *node = rtyp.list; - if (pp != nil) { - p0t = pp.lhs; - if (pp.next != nil) { - p1t = pp.next.lhs; - }; - }; - }; - }; - }; - }; - }; - - let l0: *node = n.list; - let l1: *node = nil; - if (l0 != nil) { l1 = l0.next; }; - - let t0: *node = nil; - let t1: *node = nil; - if (l0 != nil) { t0 = l0.lhs; }; - if (l1 != nil) { t1 = l1.lhs; }; - if (t0 == nil) { t0 = p0t; }; - if (t1 == nil) { t1 = p1t; }; - - let s0_is_str: bool = isstrtype(c, t0); - let s1_is_str: bool = isstrtype(c, t1); + // #83: positional per-element destructure let-binding. Same cursor + // as cgmassign (and cgreturn; harec create_unpack_bindings, + // ref/harec/src/check.c:1354-1416). wwstage has no checker, so each + // binding's type is its explicit annotation (l.lhs) when present, + // else the called fn's return-type tuple element (N_TTUPLE param) + // walked in lockstep. A slice/str rides its 3-word {ptr,len,cap} + // header (ref/hare/rt/ensure.ha:4-8) into a header-sized slot; a + // scalar rides 1 word into an 8B slot. Over-capacity loud-stops. + let rettuple: *node = rettupleof(c, rhs); cgexpr(c, rhs); - if (l0 != nil) { - if (l1 != nil) { - if (s0_is_str != s1_is_str) { - let sz0: i32 = 8; - let sz1: i32 = 8; - if (s0_is_str) { sz0 = primtypesize("str"): i32; }; - if (s1_is_str) { sz1 = primtypesize("str"): i32; }; - let off0: i32 = localadd(c, l0.str, sz0, t0); - let off1: i32 = localadd(c, l1.str, sz1, t1); - if (s0_is_str) { - // l0 str: ptr=DX, len=CX, cap=R8. l1 scalar = AX. - emitline("\tMOVQ\tDX, "); - emitoff(off0: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tCX, "); - emitoff((off0 + 8): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tR8, "); - emitoff((off0 + 16): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tAX, "); - emitoff(off1: i64); - emitline("(BP)\n"); - } else { - // l0 scalar; l1 str: ptr=DX, len=CX, cap=R8. - emitline("\tMOVQ\tAX, "); - emitoff(off0: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tDX, "); - emitoff(off1: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tCX, "); - emitoff((off1 + 8): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tR8, "); - emitoff((off1 + 16): i64); - emitline("(BP)\n"); - }; - c.lastwasreturn = 0; - return; - }; + let total: i32 = 0; + let l: *node = n.list; + let pt: *node = nil; + if (rettuple != nil) { pt = rettuple.list; }; + for (l != nil) { + let tn: *node = l.lhs; + if (tn == nil) { + if (pt != nil) { tn = pt.lhs; }; }; + let wide: bool = isstrtype(c, tn) || isslicetype(c, tn); + total = total + tupebytes(wide); + l = l.next; + if (pt != nil) { pt = pt.next; }; + }; + if (total > 4) { // AX,DX,CX,R8 capacity + // pinned loud-stop, inline like cgen.ww:604 (cstage uses + // fatal(), err.c) — surface, don't corrupt. + let msg: str = "tuple destructure exceeds register-return ABI capacity (4 eightbytes); see return-ABI #10\n"; + os.write(2, msg.ptr, msg.len: u64); + os.exit(1); }; - if (l0 != nil) { - let off: i32 = localadd(c, l0.str, 8, t0); - emitline("\tMOVQ\tAX, "); - emitoff(off: i64); - emitline("(BP)\n"); - }; - if (l1 != nil) { - let off: i32 = localadd(c, l1.str, 8, t1); - emitline("\tMOVQ\tDX, "); - emitoff(off: i64); - emitline("(BP)\n"); + let cur: i32 = 0; + l = n.list; + pt = nil; + if (rettuple != nil) { pt = rettuple.list; }; + for (l != nil) { + let tn: *node = l.lhs; + if (tn == nil) { + if (pt != nil) { tn = pt.lhs; }; + }; + let wide: bool = isstrtype(c, tn) || isslicetype(c, tn); + let sz: i32 = 8; + if (wide) { sz = tyslicesize(): i32; }; + let off: i32 = localadd(c, l.str, sz, tn); + tupstore(cur, off, wide); + cur = cur + tupebytes(wide); + l = l.next; + if (pt != nil) { pt = pt.next; }; }; c.lastwasreturn = 0; return; diff --git a/selfhost/cmd/wcc/cgenstmt.ww b/selfhost/cmd/wcc/cgenstmt.ww index 24ba9d01..6cac2f74 100644 --- a/selfhost/cmd/wcc/cgenstmt.ww +++ b/selfhost/cmd/wcc/cgenstmt.ww @@ -107,47 +107,134 @@ fn rundefers(c: *cgen) void = { return; }; +// #83: positional tuple register-return ABI. Tuple elements ride +// consecutive eightbytes over [AX,DX,CX,R8] (tupreg by index); a +// slice/str rides its 3-word {ptr,len,cap} header (tyslicesize SSoT, +// ref/hare/rt/ensure.ha:4-8), a scalar rides 1. SEND (cgreturn) and +// RECEIVE (cgmlet/cgmassign) walk the SAME widths so element->register +// agrees — mirrors harec create_unpack_bindings +// (ref/harec/src/check.c:1354-1416). Capacity is 4 (AX,DX,CX,R8). +fn tupreg(i: i32) str = { + if (i == 0) { return "AX"; }; + if (i == 1) { return "DX"; }; + if (i == 2) { return "CX"; }; + return "R8"; +}; + +fn tupebytes(wide: bool) i32 = { + if (wide) { return (tyslicesize() / 8i64): i32; }; + return 1; +}; + +// rettupleof — the N_TTUPLE return-type node of an N_CALL rhs (else nil). +// wwstage has no checker, so the receive sites read each tuple element's +// width from the called fn's declared return type. Mirrors the callee +// resolution shared by cgmlet/cgmassign. +fn rettupleof(c: *cgen, rhs: *node) *node = { + if (rhs == nil) { return nil; }; + if (rhs.kind != nkind.N_CALL) { return nil; }; + let callee: *node = rhs.lhs; + if (callee == nil) { return nil; }; + let cnm: str; + cnm.ptr = nil; cnm.len = 0; + let cmod: str; + cmod.ptr = nil; cmod.len = 0; + if (callee.kind == nkind.N_IDENT) { + cnm = callee.str; + cmod = c.curmod; + }; + if (callee.kind == nkind.N_DOT) { + cnm = callee.str; + if (callee.lhs != nil) { + if (callee.lhs.kind == nkind.N_IDENT) { + cmod = callee.lhs.str; + }; + }; + }; + if (cnm.len == 0) { return nil; }; + let rtyp: *node = fnretlookupmod(c, cnm, cmod); + if (rtyp == nil) { return nil; }; + if (rtyp.kind != nkind.N_TTUPLE) { return nil; }; + return rtyp; +}; + +// tupstore — store the tuple element at register-cursor `cur` into the +// BP-relative slot at `off`. A slice/str stores its 3-word {ptr,len,cap} +// header (ref/hare/rt/ensure.ha:4-8) at off/+8/+16 from consecutive +// cursor registers; a scalar stores 1 word. Byte-identical to the cstage +// N_MLET/N_MASSIGN store (cmd/w6c/cgen.c). +fn tupstore(cur: i32, off: i32, wide: bool) void = { + if (wide) { + emitline("\tMOVQ\t"); + emitline(tupreg(cur + 0)); + emitline(", "); + emitoff(off: i64); + emitline("(BP)\n"); + emitline("\tMOVQ\t"); + emitline(tupreg(cur + 1)); + emitline(", "); + emitoff((off + 8): i64); + emitline("(BP)\n"); + emitline("\tMOVQ\t"); + emitline(tupreg(cur + 2)); + emitline(", "); + emitoff((off + 16): i64); + emitline("(BP)\n"); + return; + }; + emitline("\tMOVQ\t"); + emitline(tupreg(cur)); + emitline(", "); + emitoff(off: i64); + emitline("(BP)\n"); +}; + fn cgreturn(c: *cgen, n: *node) void = { rundefers(c); let rhs: *node = n.lhs; if (rhs != nil) { - // Tuple return `return a, b;`, word-indexed AX→DX→CX→R8 (the - // SAME register sequence as the tagged-union return below; the - // tuple just fills it positionally): - // (scalar, scalar) — AX = v0, DX = v1. - // (scalar, str) / (str, scalar) — AX = scalar elem, - // DX = str.ptr, CX = str.len, R8 = str.cap. - // str IS []u8 (24B) → 32B tuple; cap rides R8, matching the - // tagged-union return that already uses R8 for slot+24 - // (#1/Phase 3, task #5). Receive sites destructure off the - // same regs regardless of position. + // #83: positional per-element register-return (harec + // create_unpack_bindings, ref/harec/src/check.c:1354-1416). Each + // element rides consecutive eightbytes over [AX,DX,CX,R8] + // (tupreg); a slice/str rides its 3-word {ptr,len,cap} header + // (ref/hare/rt/ensure.ha:4-8), cgexpr leaving it in (AX,BX,CX); a + // scalar rides 1 word in AX. Spill each element L->R, then pop + // into the cursor's registers in reverse so positional slot i + // lands in tupreg(i) — (scalar,str) keeps the historical AX + + // DX,CX,R8. The SAME cursor drives the receive sites. Over- + // capacity is a loud stop (return-ABI #10), never a silent drop. if (rhs.kind == nkind.N_TUPLE) { - let v: *node = rhs.list; - if (v != nil) { - let v2: *node = v.next; - if (v2 != nil) { - let v0_is_str: bool = nodeisstr(c, v); - let v1_is_str: bool = nodeisstr(c, v2); - if ((v0_is_str || v1_is_str) && !(v0_is_str && v1_is_str)) { - let strn: *node = v; - let scaln: *node = v2; - if (v1_is_str) { strn = v2; scaln = v; }; - cgexpr(c, scaln); - emitline("\tPUSHQ\tAX\n"); - cgexpr(c, strn); - emitline("\tMOVQ\tCX, R8\n"); - emitline("\tMOVQ\tBX, CX\n"); - emitline("\tMOVQ\tAX, DX\n"); - emitline("\tPOPQ\tAX\n"); - } else { - cgexpr(c, v2); - emitline("\tPUSHQ\tAX\n"); - cgexpr(c, v); - emitline("\tPOPQ\tDX\n"); - }; - } else { - cgexpr(c, v); + let total: i32 = 0; + let e: *node = rhs.list; + for (e != nil) { + let wide: bool = nodeisstr(c, e) || nodeisslice(c, e); + total = total + tupebytes(wide); + e = e.next; + }; + if (total > 4) { // AX,DX,CX,R8 capacity + // pinned loud-stop, inline like cgen.ww:604 (cstage + // uses fatal(), err.c) — surface, don't corrupt. + let msg: str = "tuple return exceeds register-return ABI capacity (4 eightbytes); see return-ABI #10\n"; + os.write(2, msg.ptr, msg.len: u64); + os.exit(1); + }; + e = rhs.list; + for (e != nil) { + let wide: bool = nodeisstr(c, e) || nodeisslice(c, e); + cgexpr(c, e); + emitline("\tPUSHQ\tAX\n"); // scalar / .ptr + if (wide) { + emitline("\tPUSHQ\tBX\n"); // .len + emitline("\tPUSHQ\tCX\n"); // .cap }; + e = e.next; + }; + let i: i32 = total - 1; + for (i >= 0) { + emitline("\tPOPQ\t"); + emitline(tupreg(i)); + emitline("\n"); + i = i - 1; }; emitline("\tMOVQ\tBP, SP\n"); emitline("\tPOPQ\tBP\n"); @@ -1197,131 +1284,58 @@ fn cgfor(c: *cgen, n: *node) void = { // cmd/w6c/cgen.c:2424-2440. Lvalues beyond two are dropped (same // as C — no fixture uses >2 today). fn cgmassign(c: *cgen, n: *node) void = { - let l0: *node = n.list; - let l1: *node = nil; - if (l0 != nil) { l1 = l0.next; }; - - // one-str only; two-str destructure is gap (task #22). The str - // tuple element's 24B slot already exists (reassignment), so - // localfind it and mirror cgmlet's (DX,CX,R8)->(.ptr,.len,.cap) - // routing; the bare scalar fallback below would store only DX and - // drop len/cap. wwstage has no checker, so str-ness comes from the - // called fn's return-type tuple element (as in cgmlet). - let p0t: *node = nil; - let p1t: *node = nil; - let rhs: *node = n.rhs; - if (rhs != nil) { - if (rhs.kind == nkind.N_CALL) { - let callee: *node = rhs.lhs; - if (callee != nil) { - let cnm: str; - cnm.ptr = nil; cnm.len = 0; - let cmod: str; - cmod.ptr = nil; cmod.len = 0; - if (callee.kind == nkind.N_IDENT) { - cnm = callee.str; - cmod = c.curmod; - }; - if (callee.kind == nkind.N_DOT) { - cnm = callee.str; - if (callee.lhs != nil) { - if (callee.lhs.kind == nkind.N_IDENT) { - cmod = callee.lhs.str; - }; - }; - }; - if (cnm.len > 0) { - let rtyp: *node = fnretlookupmod(c, cnm, cmod); - if (rtyp != nil) { - if (rtyp.kind == nkind.N_TTUPLE) { - let pp: *node = rtyp.list; - if (pp != nil) { - p0t = pp.lhs; - if (pp.next != nil) { - p1t = pp.next.lhs; - }; - }; - }; - }; - }; - }; - }; - }; - - let s0_is_str: bool = isstrtype(c, p0t); - let s1_is_str: bool = isstrtype(c, p1t); + // #83: positional per-element destructure REASSIGN. Same cursor as + // cgmlet (and cgreturn; harec create_unpack_bindings, + // ref/harec/src/check.c:1354-1416), but the slots already exist + // (reassignment) so localfind them. wwstage has no checker, so each + // element's width comes from the called fn's return-type tuple + // element (N_TTUPLE param) walked in lockstep with the bindings; a + // slice/str rides its 3-word {ptr,len,cap} header + // (ref/hare/rt/ensure.ha:4-8). A missing/non-ident binding consumes + // its register slot without storing (mirrors harec `_`). This bare- + // comma `a, s = f()` multi-assign is a retained ww-EXTENSION beyond + // Hare (Hare tuple-unpack is binding-only); ww keeps the Go/rob-pike + // multi-assign idiom — rule-9 carve-out. Over-capacity loud-stops. + let rettuple: *node = rettupleof(c, n.rhs); if (n.rhs != nil) { cgexpr(c, n.rhs); }; - if (l0 != nil) { - if (l1 != nil) { - if (l0.kind == nkind.N_IDENT) { - if (l1.kind == nkind.N_IDENT) { - if (s0_is_str != s1_is_str) { - let off0: i32 = localfind(c, l0.str); - let off1: i32 = localfind(c, l1.str); - if (off0 != 0) { - if (off1 != 0) { - if (s0_is_str) { - // l0 str: ptr=DX, len=CX, cap=R8. l1 scalar = AX. - emitline("\tMOVQ\tDX, "); - emitoff(off0: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tCX, "); - emitoff((off0 + 8): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tR8, "); - emitoff((off0 + 16): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tAX, "); - emitoff(off1: i64); - emitline("(BP)\n"); - } else { - // l0 scalar; l1 str: ptr=DX, len=CX, cap=R8. - emitline("\tMOVQ\tAX, "); - emitoff(off0: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tDX, "); - emitoff(off1: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tCX, "); - emitoff((off1 + 8): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tR8, "); - emitoff((off1 + 16): i64); - emitline("(BP)\n"); - }; - c.lastwasreturn = 0; - return; - }; - }; - }; - }; - }; - }; + let total: i32 = 0; + let l: *node = n.list; + let pt: *node = nil; + if (rettuple != nil) { pt = rettuple.list; }; + for (l != nil) { + let tn: *node = nil; + if (pt != nil) { tn = pt.lhs; }; + let wide: bool = isstrtype(c, tn) || isslicetype(c, tn); + total = total + tupebytes(wide); + l = l.next; + if (pt != nil) { pt = pt.next; }; + }; + if (total > 4) { // AX,DX,CX,R8 capacity + // pinned loud-stop, inline like cgen.ww:604 (cstage uses + // fatal(), err.c) — surface, don't corrupt. + let msg: str = "tuple destructure exceeds register-return ABI capacity (4 eightbytes); see return-ABI #10\n"; + os.write(2, msg.ptr, msg.len: u64); + os.exit(1); }; - emitline("\tPUSHQ\tDX\n"); - if (l0 != nil) { - if (l0.kind == nkind.N_IDENT) { - let off: i32 = localfind(c, l0.str); - if (off != 0) { - emitline("\tMOVQ\tAX, "); - emitoff(off: i64); - emitline("(BP)\n"); - }; - }; - }; - emitline("\tPOPQ\tDX\n"); - if (l1 != nil) { - if (l1.kind == nkind.N_IDENT) { - let off: i32 = localfind(c, l1.str); - if (off != 0) { - emitline("\tMOVQ\tDX, "); - emitoff(off: i64); - emitline("(BP)\n"); - }; + let cur: i32 = 0; + l = n.list; + pt = nil; + if (rettuple != nil) { pt = rettuple.list; }; + for (l != nil) { + let tn: *node = nil; + if (pt != nil) { tn = pt.lhs; }; + let wide: bool = isstrtype(c, tn) || isslicetype(c, tn); + let off: i32 = 0; + if (l.kind == nkind.N_IDENT) { off = localfind(c, l.str); }; + if (off != 0) { + tupstore(cur, off, wide); }; + cur = cur + tupebytes(wide); + l = l.next; + if (pt != nil) { pt = pt.next; }; }; c.lastwasreturn = 0; return; @@ -1342,115 +1356,57 @@ fn cgmlet(c: *cgen, n: *node) void = { let rhs: *node = n.rhs; if (rhs == nil) { return; }; - let p0t: *node = nil; - let p1t: *node = nil; - if (rhs.kind == nkind.N_CALL) { - let callee: *node = rhs.lhs; - if (callee != nil) { - let cnm: str; - cnm.ptr = nil; cnm.len = 0; - let cmod: str; - cmod.ptr = nil; cmod.len = 0; - if (callee.kind == nkind.N_IDENT) { - cnm = callee.str; - cmod = c.curmod; - }; - if (callee.kind == nkind.N_DOT) { - cnm = callee.str; - if (callee.lhs != nil) { - if (callee.lhs.kind == nkind.N_IDENT) { - cmod = callee.lhs.str; - }; - }; - }; - if (cnm.len > 0) { - let rtyp: *node = fnretlookupmod(c, cnm, cmod); - if (rtyp != nil) { - if (rtyp.kind == nkind.N_TTUPLE) { - let pp: *node = rtyp.list; - if (pp != nil) { - p0t = pp.lhs; - if (pp.next != nil) { - p1t = pp.next.lhs; - }; - }; - }; - }; - }; - }; - }; - - let l0: *node = n.list; - let l1: *node = nil; - if (l0 != nil) { l1 = l0.next; }; - - let t0: *node = nil; - let t1: *node = nil; - if (l0 != nil) { t0 = l0.lhs; }; - if (l1 != nil) { t1 = l1.lhs; }; - if (t0 == nil) { t0 = p0t; }; - if (t1 == nil) { t1 = p1t; }; - - let s0_is_str: bool = isstrtype(c, t0); - let s1_is_str: bool = isstrtype(c, t1); + // #83: positional per-element destructure let-binding. Same cursor + // as cgmassign (and cgreturn; harec create_unpack_bindings, + // ref/harec/src/check.c:1354-1416). wwstage has no checker, so each + // binding's type is its explicit annotation (l.lhs) when present, + // else the called fn's return-type tuple element (N_TTUPLE param) + // walked in lockstep. A slice/str rides its 3-word {ptr,len,cap} + // header (ref/hare/rt/ensure.ha:4-8) into a header-sized slot; a + // scalar rides 1 word into an 8B slot. Over-capacity loud-stops. + let rettuple: *node = rettupleof(c, rhs); cgexpr(c, rhs); - if (l0 != nil) { - if (l1 != nil) { - if (s0_is_str != s1_is_str) { - let sz0: i32 = 8; - let sz1: i32 = 8; - if (s0_is_str) { sz0 = primtypesize("str"): i32; }; - if (s1_is_str) { sz1 = primtypesize("str"): i32; }; - let off0: i32 = localadd(c, l0.str, sz0, t0); - let off1: i32 = localadd(c, l1.str, sz1, t1); - if (s0_is_str) { - // l0 str: ptr=DX, len=CX, cap=R8. l1 scalar = AX. - emitline("\tMOVQ\tDX, "); - emitoff(off0: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tCX, "); - emitoff((off0 + 8): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tR8, "); - emitoff((off0 + 16): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tAX, "); - emitoff(off1: i64); - emitline("(BP)\n"); - } else { - // l0 scalar; l1 str: ptr=DX, len=CX, cap=R8. - emitline("\tMOVQ\tAX, "); - emitoff(off0: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tDX, "); - emitoff(off1: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tCX, "); - emitoff((off1 + 8): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tR8, "); - emitoff((off1 + 16): i64); - emitline("(BP)\n"); - }; - c.lastwasreturn = 0; - return; - }; + let total: i32 = 0; + let l: *node = n.list; + let pt: *node = nil; + if (rettuple != nil) { pt = rettuple.list; }; + for (l != nil) { + let tn: *node = l.lhs; + if (tn == nil) { + if (pt != nil) { tn = pt.lhs; }; }; + let wide: bool = isstrtype(c, tn) || isslicetype(c, tn); + total = total + tupebytes(wide); + l = l.next; + if (pt != nil) { pt = pt.next; }; + }; + if (total > 4) { // AX,DX,CX,R8 capacity + // pinned loud-stop, inline like cgen.ww:604 (cstage uses + // fatal(), err.c) — surface, don't corrupt. + let msg: str = "tuple destructure exceeds register-return ABI capacity (4 eightbytes); see return-ABI #10\n"; + os.write(2, msg.ptr, msg.len: u64); + os.exit(1); }; - if (l0 != nil) { - let off: i32 = localadd(c, l0.str, 8, t0); - emitline("\tMOVQ\tAX, "); - emitoff(off: i64); - emitline("(BP)\n"); - }; - if (l1 != nil) { - let off: i32 = localadd(c, l1.str, 8, t1); - emitline("\tMOVQ\tDX, "); - emitoff(off: i64); - emitline("(BP)\n"); + let cur: i32 = 0; + l = n.list; + pt = nil; + if (rettuple != nil) { pt = rettuple.list; }; + for (l != nil) { + let tn: *node = l.lhs; + if (tn == nil) { + if (pt != nil) { tn = pt.lhs; }; + }; + let wide: bool = isstrtype(c, tn) || isslicetype(c, tn); + let sz: i32 = 8; + if (wide) { sz = tyslicesize(): i32; }; + let off: i32 = localadd(c, l.str, sz, tn); + tupstore(cur, off, wide); + cur = cur + tupebytes(wide); + l = l.next; + if (pt != nil) { pt = pt.next; }; }; c.lastwasreturn = 0; return; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 67c81b0e..6306327c 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -20062,47 +20062,134 @@ fn rundefers(c: *cgen) void = { return; }; +// #83: positional tuple register-return ABI. Tuple elements ride +// consecutive eightbytes over [AX,DX,CX,R8] (tupreg by index); a +// slice/str rides its 3-word {ptr,len,cap} header (tyslicesize SSoT, +// ref/hare/rt/ensure.ha:4-8), a scalar rides 1. SEND (cgreturn) and +// RECEIVE (cgmlet/cgmassign) walk the SAME widths so element->register +// agrees — mirrors harec create_unpack_bindings +// (ref/harec/src/check.c:1354-1416). Capacity is 4 (AX,DX,CX,R8). +fn tupreg(i: i32) str = { + if (i == 0) { return "AX"; }; + if (i == 1) { return "DX"; }; + if (i == 2) { return "CX"; }; + return "R8"; +}; + +fn tupebytes(wide: bool) i32 = { + if (wide) { return (tyslicesize() / 8i64): i32; }; + return 1; +}; + +// rettupleof — the N_TTUPLE return-type node of an N_CALL rhs (else nil). +// wwstage has no checker, so the receive sites read each tuple element's +// width from the called fn's declared return type. Mirrors the callee +// resolution shared by cgmlet/cgmassign. +fn rettupleof(c: *cgen, rhs: *node) *node = { + if (rhs == nil) { return nil; }; + if (rhs.kind != nkind.N_CALL) { return nil; }; + let callee: *node = rhs.lhs; + if (callee == nil) { return nil; }; + let cnm: str; + cnm.ptr = nil; cnm.len = 0; + let cmod: str; + cmod.ptr = nil; cmod.len = 0; + if (callee.kind == nkind.N_IDENT) { + cnm = callee.str; + cmod = c.curmod; + }; + if (callee.kind == nkind.N_DOT) { + cnm = callee.str; + if (callee.lhs != nil) { + if (callee.lhs.kind == nkind.N_IDENT) { + cmod = callee.lhs.str; + }; + }; + }; + if (cnm.len == 0) { return nil; }; + let rtyp: *node = fnretlookupmod(c, cnm, cmod); + if (rtyp == nil) { return nil; }; + if (rtyp.kind != nkind.N_TTUPLE) { return nil; }; + return rtyp; +}; + +// tupstore — store the tuple element at register-cursor `cur` into the +// BP-relative slot at `off`. A slice/str stores its 3-word {ptr,len,cap} +// header (ref/hare/rt/ensure.ha:4-8) at off/+8/+16 from consecutive +// cursor registers; a scalar stores 1 word. Byte-identical to the cstage +// N_MLET/N_MASSIGN store (cmd/w6c/cgen.c). +fn tupstore(cur: i32, off: i32, wide: bool) void = { + if (wide) { + emitline("\tMOVQ\t"); + emitline(tupreg(cur + 0)); + emitline(", "); + emitoff(off: i64); + emitline("(BP)\n"); + emitline("\tMOVQ\t"); + emitline(tupreg(cur + 1)); + emitline(", "); + emitoff((off + 8): i64); + emitline("(BP)\n"); + emitline("\tMOVQ\t"); + emitline(tupreg(cur + 2)); + emitline(", "); + emitoff((off + 16): i64); + emitline("(BP)\n"); + return; + }; + emitline("\tMOVQ\t"); + emitline(tupreg(cur)); + emitline(", "); + emitoff(off: i64); + emitline("(BP)\n"); +}; + fn cgreturn(c: *cgen, n: *node) void = { rundefers(c); let rhs: *node = n.lhs; if (rhs != nil) { - // Tuple return `return a, b;`, word-indexed AX→DX→CX→R8 (the - // SAME register sequence as the tagged-union return below; the - // tuple just fills it positionally): - // (scalar, scalar) — AX = v0, DX = v1. - // (scalar, str) / (str, scalar) — AX = scalar elem, - // DX = str.ptr, CX = str.len, R8 = str.cap. - // str IS []u8 (24B) → 32B tuple; cap rides R8, matching the - // tagged-union return that already uses R8 for slot+24 - // (#1/Phase 3, task #5). Receive sites destructure off the - // same regs regardless of position. + // #83: positional per-element register-return (harec + // create_unpack_bindings, ref/harec/src/check.c:1354-1416). Each + // element rides consecutive eightbytes over [AX,DX,CX,R8] + // (tupreg); a slice/str rides its 3-word {ptr,len,cap} header + // (ref/hare/rt/ensure.ha:4-8), cgexpr leaving it in (AX,BX,CX); a + // scalar rides 1 word in AX. Spill each element L->R, then pop + // into the cursor's registers in reverse so positional slot i + // lands in tupreg(i) — (scalar,str) keeps the historical AX + + // DX,CX,R8. The SAME cursor drives the receive sites. Over- + // capacity is a loud stop (return-ABI #10), never a silent drop. if (rhs.kind == nkind.N_TUPLE) { - let v: *node = rhs.list; - if (v != nil) { - let v2: *node = v.next; - if (v2 != nil) { - let v0_is_str: bool = nodeisstr(c, v); - let v1_is_str: bool = nodeisstr(c, v2); - if ((v0_is_str || v1_is_str) && !(v0_is_str && v1_is_str)) { - let strn: *node = v; - let scaln: *node = v2; - if (v1_is_str) { strn = v2; scaln = v; }; - cgexpr(c, scaln); - emitline("\tPUSHQ\tAX\n"); - cgexpr(c, strn); - emitline("\tMOVQ\tCX, R8\n"); - emitline("\tMOVQ\tBX, CX\n"); - emitline("\tMOVQ\tAX, DX\n"); - emitline("\tPOPQ\tAX\n"); - } else { - cgexpr(c, v2); - emitline("\tPUSHQ\tAX\n"); - cgexpr(c, v); - emitline("\tPOPQ\tDX\n"); - }; - } else { - cgexpr(c, v); + let total: i32 = 0; + let e: *node = rhs.list; + for (e != nil) { + let wide: bool = nodeisstr(c, e) || nodeisslice(c, e); + total = total + tupebytes(wide); + e = e.next; + }; + if (total > 4) { // AX,DX,CX,R8 capacity + // pinned loud-stop, inline like cgen.ww:604 (cstage + // uses fatal(), err.c) — surface, don't corrupt. + let msg: str = "tuple return exceeds register-return ABI capacity (4 eightbytes); see return-ABI #10\n"; + os.write(2, msg.ptr, msg.len: u64); + os.exit(1); + }; + e = rhs.list; + for (e != nil) { + let wide: bool = nodeisstr(c, e) || nodeisslice(c, e); + cgexpr(c, e); + emitline("\tPUSHQ\tAX\n"); // scalar / .ptr + if (wide) { + emitline("\tPUSHQ\tBX\n"); // .len + emitline("\tPUSHQ\tCX\n"); // .cap }; + e = e.next; + }; + let i: i32 = total - 1; + for (i >= 0) { + emitline("\tPOPQ\t"); + emitline(tupreg(i)); + emitline("\n"); + i = i - 1; }; emitline("\tMOVQ\tBP, SP\n"); emitline("\tPOPQ\tBP\n"); @@ -21152,131 +21239,58 @@ fn cgfor(c: *cgen, n: *node) void = { // cmd/w6c/cgen.c:2424-2440. Lvalues beyond two are dropped (same // as C — no fixture uses >2 today). fn cgmassign(c: *cgen, n: *node) void = { - let l0: *node = n.list; - let l1: *node = nil; - if (l0 != nil) { l1 = l0.next; }; - - // one-str only; two-str destructure is gap (task #22). The str - // tuple element's 24B slot already exists (reassignment), so - // localfind it and mirror cgmlet's (DX,CX,R8)->(.ptr,.len,.cap) - // routing; the bare scalar fallback below would store only DX and - // drop len/cap. wwstage has no checker, so str-ness comes from the - // called fn's return-type tuple element (as in cgmlet). - let p0t: *node = nil; - let p1t: *node = nil; - let rhs: *node = n.rhs; - if (rhs != nil) { - if (rhs.kind == nkind.N_CALL) { - let callee: *node = rhs.lhs; - if (callee != nil) { - let cnm: str; - cnm.ptr = nil; cnm.len = 0; - let cmod: str; - cmod.ptr = nil; cmod.len = 0; - if (callee.kind == nkind.N_IDENT) { - cnm = callee.str; - cmod = c.curmod; - }; - if (callee.kind == nkind.N_DOT) { - cnm = callee.str; - if (callee.lhs != nil) { - if (callee.lhs.kind == nkind.N_IDENT) { - cmod = callee.lhs.str; - }; - }; - }; - if (cnm.len > 0) { - let rtyp: *node = fnretlookupmod(c, cnm, cmod); - if (rtyp != nil) { - if (rtyp.kind == nkind.N_TTUPLE) { - let pp: *node = rtyp.list; - if (pp != nil) { - p0t = pp.lhs; - if (pp.next != nil) { - p1t = pp.next.lhs; - }; - }; - }; - }; - }; - }; - }; - }; - - let s0_is_str: bool = isstrtype(c, p0t); - let s1_is_str: bool = isstrtype(c, p1t); + // #83: positional per-element destructure REASSIGN. Same cursor as + // cgmlet (and cgreturn; harec create_unpack_bindings, + // ref/harec/src/check.c:1354-1416), but the slots already exist + // (reassignment) so localfind them. wwstage has no checker, so each + // element's width comes from the called fn's return-type tuple + // element (N_TTUPLE param) walked in lockstep with the bindings; a + // slice/str rides its 3-word {ptr,len,cap} header + // (ref/hare/rt/ensure.ha:4-8). A missing/non-ident binding consumes + // its register slot without storing (mirrors harec `_`). This bare- + // comma `a, s = f()` multi-assign is a retained ww-EXTENSION beyond + // Hare (Hare tuple-unpack is binding-only); ww keeps the Go/rob-pike + // multi-assign idiom — rule-9 carve-out. Over-capacity loud-stops. + let rettuple: *node = rettupleof(c, n.rhs); if (n.rhs != nil) { cgexpr(c, n.rhs); }; - if (l0 != nil) { - if (l1 != nil) { - if (l0.kind == nkind.N_IDENT) { - if (l1.kind == nkind.N_IDENT) { - if (s0_is_str != s1_is_str) { - let off0: i32 = localfind(c, l0.str); - let off1: i32 = localfind(c, l1.str); - if (off0 != 0) { - if (off1 != 0) { - if (s0_is_str) { - // l0 str: ptr=DX, len=CX, cap=R8. l1 scalar = AX. - emitline("\tMOVQ\tDX, "); - emitoff(off0: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tCX, "); - emitoff((off0 + 8): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tR8, "); - emitoff((off0 + 16): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tAX, "); - emitoff(off1: i64); - emitline("(BP)\n"); - } else { - // l0 scalar; l1 str: ptr=DX, len=CX, cap=R8. - emitline("\tMOVQ\tAX, "); - emitoff(off0: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tDX, "); - emitoff(off1: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tCX, "); - emitoff((off1 + 8): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tR8, "); - emitoff((off1 + 16): i64); - emitline("(BP)\n"); - }; - c.lastwasreturn = 0; - return; - }; - }; - }; - }; - }; - }; + let total: i32 = 0; + let l: *node = n.list; + let pt: *node = nil; + if (rettuple != nil) { pt = rettuple.list; }; + for (l != nil) { + let tn: *node = nil; + if (pt != nil) { tn = pt.lhs; }; + let wide: bool = isstrtype(c, tn) || isslicetype(c, tn); + total = total + tupebytes(wide); + l = l.next; + if (pt != nil) { pt = pt.next; }; + }; + if (total > 4) { // AX,DX,CX,R8 capacity + // pinned loud-stop, inline like cgen.ww:604 (cstage uses + // fatal(), err.c) — surface, don't corrupt. + let msg: str = "tuple destructure exceeds register-return ABI capacity (4 eightbytes); see return-ABI #10\n"; + os.write(2, msg.ptr, msg.len: u64); + os.exit(1); }; - emitline("\tPUSHQ\tDX\n"); - if (l0 != nil) { - if (l0.kind == nkind.N_IDENT) { - let off: i32 = localfind(c, l0.str); - if (off != 0) { - emitline("\tMOVQ\tAX, "); - emitoff(off: i64); - emitline("(BP)\n"); - }; - }; - }; - emitline("\tPOPQ\tDX\n"); - if (l1 != nil) { - if (l1.kind == nkind.N_IDENT) { - let off: i32 = localfind(c, l1.str); - if (off != 0) { - emitline("\tMOVQ\tDX, "); - emitoff(off: i64); - emitline("(BP)\n"); - }; + let cur: i32 = 0; + l = n.list; + pt = nil; + if (rettuple != nil) { pt = rettuple.list; }; + for (l != nil) { + let tn: *node = nil; + if (pt != nil) { tn = pt.lhs; }; + let wide: bool = isstrtype(c, tn) || isslicetype(c, tn); + let off: i32 = 0; + if (l.kind == nkind.N_IDENT) { off = localfind(c, l.str); }; + if (off != 0) { + tupstore(cur, off, wide); }; + cur = cur + tupebytes(wide); + l = l.next; + if (pt != nil) { pt = pt.next; }; }; c.lastwasreturn = 0; return; @@ -21297,115 +21311,57 @@ fn cgmlet(c: *cgen, n: *node) void = { let rhs: *node = n.rhs; if (rhs == nil) { return; }; - let p0t: *node = nil; - let p1t: *node = nil; - if (rhs.kind == nkind.N_CALL) { - let callee: *node = rhs.lhs; - if (callee != nil) { - let cnm: str; - cnm.ptr = nil; cnm.len = 0; - let cmod: str; - cmod.ptr = nil; cmod.len = 0; - if (callee.kind == nkind.N_IDENT) { - cnm = callee.str; - cmod = c.curmod; - }; - if (callee.kind == nkind.N_DOT) { - cnm = callee.str; - if (callee.lhs != nil) { - if (callee.lhs.kind == nkind.N_IDENT) { - cmod = callee.lhs.str; - }; - }; - }; - if (cnm.len > 0) { - let rtyp: *node = fnretlookupmod(c, cnm, cmod); - if (rtyp != nil) { - if (rtyp.kind == nkind.N_TTUPLE) { - let pp: *node = rtyp.list; - if (pp != nil) { - p0t = pp.lhs; - if (pp.next != nil) { - p1t = pp.next.lhs; - }; - }; - }; - }; - }; - }; - }; - - let l0: *node = n.list; - let l1: *node = nil; - if (l0 != nil) { l1 = l0.next; }; - - let t0: *node = nil; - let t1: *node = nil; - if (l0 != nil) { t0 = l0.lhs; }; - if (l1 != nil) { t1 = l1.lhs; }; - if (t0 == nil) { t0 = p0t; }; - if (t1 == nil) { t1 = p1t; }; - - let s0_is_str: bool = isstrtype(c, t0); - let s1_is_str: bool = isstrtype(c, t1); + // #83: positional per-element destructure let-binding. Same cursor + // as cgmassign (and cgreturn; harec create_unpack_bindings, + // ref/harec/src/check.c:1354-1416). wwstage has no checker, so each + // binding's type is its explicit annotation (l.lhs) when present, + // else the called fn's return-type tuple element (N_TTUPLE param) + // walked in lockstep. A slice/str rides its 3-word {ptr,len,cap} + // header (ref/hare/rt/ensure.ha:4-8) into a header-sized slot; a + // scalar rides 1 word into an 8B slot. Over-capacity loud-stops. + let rettuple: *node = rettupleof(c, rhs); cgexpr(c, rhs); - if (l0 != nil) { - if (l1 != nil) { - if (s0_is_str != s1_is_str) { - let sz0: i32 = 8; - let sz1: i32 = 8; - if (s0_is_str) { sz0 = primtypesize("str"): i32; }; - if (s1_is_str) { sz1 = primtypesize("str"): i32; }; - let off0: i32 = localadd(c, l0.str, sz0, t0); - let off1: i32 = localadd(c, l1.str, sz1, t1); - if (s0_is_str) { - // l0 str: ptr=DX, len=CX, cap=R8. l1 scalar = AX. - emitline("\tMOVQ\tDX, "); - emitoff(off0: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tCX, "); - emitoff((off0 + 8): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tR8, "); - emitoff((off0 + 16): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tAX, "); - emitoff(off1: i64); - emitline("(BP)\n"); - } else { - // l0 scalar; l1 str: ptr=DX, len=CX, cap=R8. - emitline("\tMOVQ\tAX, "); - emitoff(off0: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tDX, "); - emitoff(off1: i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tCX, "); - emitoff((off1 + 8): i64); - emitline("(BP)\n"); - emitline("\tMOVQ\tR8, "); - emitoff((off1 + 16): i64); - emitline("(BP)\n"); - }; - c.lastwasreturn = 0; - return; - }; + let total: i32 = 0; + let l: *node = n.list; + let pt: *node = nil; + if (rettuple != nil) { pt = rettuple.list; }; + for (l != nil) { + let tn: *node = l.lhs; + if (tn == nil) { + if (pt != nil) { tn = pt.lhs; }; }; + let wide: bool = isstrtype(c, tn) || isslicetype(c, tn); + total = total + tupebytes(wide); + l = l.next; + if (pt != nil) { pt = pt.next; }; + }; + if (total > 4) { // AX,DX,CX,R8 capacity + // pinned loud-stop, inline like cgen.ww:604 (cstage uses + // fatal(), err.c) — surface, don't corrupt. + let msg: str = "tuple destructure exceeds register-return ABI capacity (4 eightbytes); see return-ABI #10\n"; + os.write(2, msg.ptr, msg.len: u64); + os.exit(1); }; - if (l0 != nil) { - let off: i32 = localadd(c, l0.str, 8, t0); - emitline("\tMOVQ\tAX, "); - emitoff(off: i64); - emitline("(BP)\n"); - }; - if (l1 != nil) { - let off: i32 = localadd(c, l1.str, 8, t1); - emitline("\tMOVQ\tDX, "); - emitoff(off: i64); - emitline("(BP)\n"); + let cur: i32 = 0; + l = n.list; + pt = nil; + if (rettuple != nil) { pt = rettuple.list; }; + for (l != nil) { + let tn: *node = l.lhs; + if (tn == nil) { + if (pt != nil) { tn = pt.lhs; }; + }; + let wide: bool = isstrtype(c, tn) || isslicetype(c, tn); + let sz: i32 = 8; + if (wide) { sz = tyslicesize(): i32; }; + let off: i32 = localadd(c, l.str, sz, tn); + tupstore(cur, off, wide); + cur = cur + tupebytes(wide); + l = l.next; + if (pt != nil) { pt = pt.next; }; }; c.lastwasreturn = 0; return; diff --git a/test/wcc/945_tuple_nary_destructure_run.c b/test/wcc/945_tuple_nary_destructure_run.c new file mode 100644 index 00000000..785c1c6b --- /dev/null +++ b/test/wcc/945_tuple_nary_destructure_run.c @@ -0,0 +1,340 @@ +/* + * 945_tuple_nary_destructure_run — runtime coverage for project #83: the + * general N-ary tuple-destructure per-element register-return. The send + * (N_RETURN) and receive (N_MLET / N_MASSIGN) sites previously gated the + * 3-word {ptr,len,cap} store on a str-only XOR (`e0_is_str ^ e1_is_str`), + * which admitted EXACTLY ONE str element and was slice-BLIND: a tuple with + * a []u8 element fell to the scalar-pair fallback and silently DROPPED + * len+cap (only .ptr landed). The fix REPLACES the XOR with a positional + * per-element cursor (harec create_unpack_bindings, + * ref/harec/src/check.c:1354-1416): each element rides consecutive + * eightbytes over [AX,DX,CX,R8]; a slice/str rides its 3-word header + * (ref/hare/rt/ensure.ha:4-8), a scalar rides 1. SEND and RECEIVE walk the + * SAME cursor so element->register agrees. This closes the pre-existing + * (scalar,slice) cs!=ww divergence by construction (slice no longer rides + * the scalar fallback) and unifies both stages on the positional layout. + * + * LOUD-STOP (rule 7): the register file holds only 4 eightbytes. A tuple + * whose elements sum to > 4 (e.g. ([]u8,[]u8)/(str,str) = 3+3 = 6) CANNOT + * be register-returned; the send site emits a COMPILE-TIME abort citing + * the return-ABI capacity (project #10), NOT a silent miscompile. Row F + * asserts that loud stop fires on BOTH stages: the build must FAIL *and* + * stderr must carry the cited diagnostic (rule 7 — loud, not a silent + * crash that an exit-code-only check would mistake for the stop). + * + * N_MLET vs N_MASSIGN: `let (a, s) = f()` is N_MLET (fresh bindings, slots + * sized from each element type). `a, s = f()` with a, s PRE-DECLARED is + * N_MASSIGN (reassignment, existing slots) — a retained ww-EXTENSION + * beyond Hare (Hare tuple-unpack is binding-only; ww keeps the Go/rob-pike + * multi-assign idiom). The N_MASSIGN rows pre-poison the destination slot + * (distinct len/cap via a PROVEN 3-word let-init copy) so a dropped + * len/cap reads the poison, never the test value — deterministic + * fail-before. The N_MLET rows use cap!=len distinct values; a dropped + * cap reads fresh-slot garbage, ~never the asserted pair. + * + * Backing storage: each []u8 element borrows a str literal's .ptr (stable + * .rodata), so the header survives the producer's return without a + * use-after-return. str elements return a literal with a MUTATED .cap so + * cap!=len (a bare literal carries cap==len, hiding a dropped cap). + * + * Rows (cap!=len in every wide row so a dropped len OR cap is caught): + * A mlet_islice `let (a, s) = f()`, f()->(i64,[]u8). a=4,len=2,cap=5,'h'. + * B massign_islice`a, s = f()` predeclared+poisoned (len=9,cap=9). + * C mlet_istr `let (a, s) = f()`, f()->(i64,str). a=4,len=2,cap=7,'h'. + * D massign_istr `a, s = f()` predeclared+poisoned (len=4,cap=6). + * E str_control plain single-str return `let s = f()` f()->str — the + * non-tuple str path (cgen.c N_RETURN node_isstr branch) + * the fix leaves untouched; len=5,cap=9 regression guard. + * G massign_blank_wide `_, a = f()`, f()->([]u8,i64). The blank `_` + * rides a 3-word slot so a lands on R8 (the i64), not DX + * (slice.len). len=2,cap=5,i64=7 distinct (any desync !=7). + * F slice_slice_builderr f()->([]u8,[]u8) returns (a,b) — 6 eightbytes, + * the build MUST FAIL (loud stop) on both stages. + * + * Fold discriminators: A/B (slice) — the old str-only XOR was slice-blind, + * so the slice fell to the scalar-pair fallback and dropped len/cap; B's + * poison makes that deterministic. G (blank `_`) — cstage derived the + * receive width from the binding type, which is null for an unstamped `_`, + * so a wide `_` was mis-sized scalar and the cursor desynced (cstage read + * DX, wwstage R8); deriving width from the rhs tuple type fixes + aligns + * both stages. F (loud stop) — the old code register-returned ([]u8,[]u8) + * with only the two .ptr words (built + ran WRONG); the fix turns that into + * a BUILDERR. C/D/E are CONTROLS: the (i64,str) path was ALREADY 3-word + * under the old XOR, and the single-str return is a separate untouched + * branch — they confirm no regression. All 14 + * fixtures pass on both the cstage `ww` and wwstage `ww_ww` drivers with + * byte-identical asm (smoke-verified pre-commit; the formal fail-before + * revert-run is ken's tuple83 gate). NNN<950, self-contained (/tmp, no + * imports), so rule-14's selfhost-sibling race does not apply (mirrors the + * 941/942/943/944 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; +} + +/* experr (builderr rows only): a stable substring of the loud-stop + * diagnostic. rule 7 — the over-capacity stop must be LOUD: the build must + * fail BECAUSE of the cited return-ABI diagnostic (project #10), not an + * incidental/silent error. NULL skips the message check. */ +struct row { const char *label; const char *src; int want; int builderr; + const char *experr; }; + +static const struct row rows[] = { + /* A — mlet_islice: `let (a, s) = f()`, f()->(i64,[]u8). Slice borrows + * the "hi" literal's .ptr (stable). len=2, cap=5 (distinct). */ + { "mlet_islice", + "fn mk() (i64, []u8) = {\n" + " let b: str = \"hi\";\n" + " let p: []u8; p.ptr = b.ptr; p.len = 2; p.cap = 5;\n" + " return (4i64, p);\n" + "};\n" + "export fn main() i32 = {\n" + " let (a, s) = mk();\n" + " if (a != 4) { return 1; };\n" + " if (s.len: i32 != 2) { return 2; };\n" + " if (s.cap: i32 != 5) { return 3; };\n" + " if (s[0] != 104u8) { return 4; };\n" + " return 0;\n" + "};\n", + 0, 0, NULL }, + /* B — massign_islice: `a, s = f()` predeclared; s poisoned (len=9, + * cap=9) via the proven 3-word `s = q` let-init copy. A dropped + * len/cap leaves the poison 9; the fix lands 2 / 5. */ + { "massign_islice", + "fn mk() (i64, []u8) = {\n" + " let b: str = \"hi\";\n" + " let p: []u8; p.ptr = b.ptr; p.len = 2; p.cap = 5;\n" + " return (4i64, p);\n" + "};\n" + "export fn main() i32 = {\n" + " let qb: str = \"zzzz\";\n" + " let q: []u8; q.ptr = qb.ptr; q.len = 9; q.cap = 9;\n" + " let a: i64 = 0i64;\n" + " let s: []u8 = q;\n" + " a, s = mk();\n" + " if (a != 4) { return 1; };\n" + " if (s.len: i32 != 2) { return 2; };\n" + " if (s.cap: i32 != 5) { return 3; };\n" + " if (s[0] != 104u8) { return 4; };\n" + " return 0;\n" + "};\n", + 0, 0, NULL }, + /* C — mlet_istr: `let (a, s) = f()`, f()->(i64,str). cap mutated to + * 7 so cap!=len(2). */ + { "mlet_istr", + "fn mk() (i64, str) = {\n" + " let p: str = \"hi\"; p.cap = 7i32;\n" + " return (4i64, p);\n" + "};\n" + "export fn main() i32 = {\n" + " let (a, s) = mk();\n" + " if (a != 4) { return 1; };\n" + " if (s.len: i32 != 2) { return 2; };\n" + " if (s.cap: i32 != 7) { return 3; };\n" + " if (s[0] != 104u8) { return 4; };\n" + " return 0;\n" + "};\n", + 0, 0, NULL }, + /* D — massign_istr: `a, s = f()` predeclared; s poisoned (len=4, + * cap=6) via the proven 3-word `s = q` copy. */ + { "massign_istr", + "fn mk() (i64, str) = {\n" + " let p: str = \"hi\"; p.cap = 7i32;\n" + " return (4i64, p);\n" + "};\n" + "export fn main() i32 = {\n" + " let q: str = \"qqqq\"; q.cap = 6i32;\n" + " let a: i64 = 0i64;\n" + " let s: str = q;\n" + " a, s = mk();\n" + " if (a != 4) { return 1; };\n" + " if (s.len: i32 != 2) { return 2; };\n" + " if (s.cap: i32 != 7) { return 3; };\n" + " if (s[0] != 104u8) { return 4; };\n" + " return 0;\n" + "};\n", + 0, 0, NULL }, + /* E — str_control: plain single-str return, the non-tuple path the + * fix leaves untouched. cap mutated to 9 so cap!=len(5). */ + { "str_control", + "fn mk() str = {\n" + " let p: str = \"hello\"; p.cap = 9i32;\n" + " return p;\n" + "};\n" + "export fn main() i32 = {\n" + " let s: str = mk();\n" + " if (s.len: i32 != 5) { return 1; };\n" + " if (s.cap: i32 != 9) { return 2; };\n" + " if (s[0] != 104u8) { return 3; };\n" + " return 0;\n" + "};\n", + 0, 0, NULL }, + /* G — massign_blank_wide: `_, a = f()`, f()->([]u8,i64), wide-first + * blank. The `_` (N_IDENT, empty str) is never type-stamped by the + * checker, so cstage previously mis-sized it as a scalar (1 word) and + * the cursor desynced: a read DX (slice.len=2) instead of R8 (the + * i64). The fix reads element width from the rhs tuple type, so `_` + * consumes its 3-word slot and a lands on R8. len=2, cap=5, i64=7 all + * distinct so any desync misread (DX=2 / CX=5) is caught, never 7. */ + { "massign_blank_wide", + "fn mk() ([]u8, i64) = {\n" + " let b: str = \"hi\";\n" + " let p: []u8; p.ptr = b.ptr; p.len = 2; p.cap = 5;\n" + " return (p, 7i64);\n" + "};\n" + "export fn main() i32 = {\n" + " let a: i64 = 0i64;\n" + " _, a = mk();\n" + " if (a != 7) { return 1; };\n" + " return 0;\n" + "};\n", + 0, 0, NULL }, + /* F — slice_slice_builderr: ([]u8,[]u8) = 6 eightbytes > 4 capacity. + * The send site MUST loud-stop (return-ABI #10); the build FAILS on + * both stages WITH the cited diagnostic (builderr=1: pass iff build + * returns nonzero AND stderr carries experr — rule 7, loud not silent). */ + { "slice_slice_builderr", + "fn mk() ([]u8, []u8) = {\n" + " let a: []u8; a.len = 1; a.cap = 1;\n" + " let b: []u8; b.len = 2; b.cap = 2;\n" + " return (a, b);\n" + "};\n" + "export fn main() i32 = {\n" + " let (x, y) = mk();\n" + " if (x.len: i32 != 1) { return 1; };\n" + " return 0;\n" + "};\n", + 0, 1, "register-return ABI capacity" }, +}; + +static int +file_contains(const char *path, const char *needle) +{ + FILE *f = fopen(path, "rb"); + if (!f) return 0; + char buf[8192]; + size_t n = fread(buf, 1, sizeof buf - 1, f); + fclose(f); + buf[n] = '\0'; + return strstr(buf, needle) != NULL; +} + +/* Returns 0 on pass, nonzero on fail. For builderr rows the build must + * FAIL and (if experr is set) the diagnostic must carry the loud-stop + * message; for normal rows the build must succeed and the binary's exit + * must equal r->want. */ +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/tuplenary_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/tuplenary_%d_d_%d", getpid(), i); + snprintf(errf, sizeof errf, "/tmp/tuplenary_%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); + + if (r->builderr) { + int ok = (brc != 0) + && (r->experr == NULL || file_contains(errf, r->experr)); + if (!ok) + fprintf(stderr, + "row[%s]: %s expected loud-stop builderr (brc=%d)\n", + r->label, driver, brc); + 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[160]; + 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); + return (got == r->want) ? 0 : (got ? got : 1); +} + +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]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + char wdrv[640]; + snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); + + struct { const char *name; const char *path; int gated_on_existence; } + 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_on_existence + && access(drivers[d].path, X_OK) != 0) { + fprintf(stderr, + "tuple_nary_destructure_run: skip %s (no %s)\n", + drivers[d].name, drivers[d].path); + continue; + } + for (int i = 0; i < n; i++) { + int rc = run_driver(drivers[d].path, &rows[i], i); + total++; + if (rc != 0) { + fprintf(stderr, + "tuple_nary_destructure_run[%s][%s]: rc=%d\n", + drivers[d].name, rows[i].label, rc); + fail++; + } + } + } + + if (fail) { + fprintf(stderr, "tuple_nary_destructure_run: %d/%d fixtures failed\n", + fail, total); + return 1; + } + printf("tuple_nary_destructure_run: %d/%d ok\n", total, total); + return 0; +}