From 80527f386830c99a9d6f36816dc9ead7654b69fd Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Sun, 24 May 2026 16:54:14 +0900 Subject: [PATCH] cgen: str a,s=call() N_MASSIGN tuple-elem store -> 3-word -- Phase 2 G3 (both stages) Reassign-destructuring a (scalar,str) tuple (a, s = call(), N_MASSIGN) stored only the str's ptr (DX->slot+0), dropping len/cap -- the last STORE-cluster gap. Reachable (valid ww; checker accepts str tuple elements) but unexercised in bootstrap (all N_MASSIGN sites returned <=8B tuples). Mirror the N_MLET destructure-store oracle (cgen.c:7475): on the one-str XOR, route the str's 3 words DX/CX/R8 -> slot+0/+8/+16; the slot pre-exists (localfind, not localadd). wwstage has no checker, so it derives str-ness from the callee return-type tuple via fnretlookupmod (structurally identical to cgmlet). Both XOR positions (str at l0 and l1). Kind-gated, never size==24. cstage==wwstage byte-identical. Scope = one-str only, matching N_MLET exactly; str+str-both is unhandled by N_MLET too and is filed as a shared gap (task #22), with WHY-comments at both destructure sites. N_MLET emission unchanged (its edit is comment-only, verified byte-identical). test/wcc/939: table-driven write-then-read-cap over both XOR positions (a,s=mk() and s,a=mk2()); cap!=len via mutation (not a sub-slice, #20); pre-poisoned via a non-G3 let-init; full triple+scalar asserted; fail-before/pass-after on both drivers. Completes the str-cap STORE cluster -- the read/write round-trip is now whole. main.combined.ww regenerated via the canonical make path. --- Makefile | 7 + cmd/w6c/cgen.c | 37 +++- selfhost/cmd/w6c/main.combined.ww | 104 ++++++++++- selfhost/cmd/wcc/cgenstmt.ww | 104 ++++++++++- selfhost/cmd/wwdump/main.combined.ww | 104 ++++++++++- test/wcc/939_str_massign_store_cap_run.c | 211 +++++++++++++++++++++++ 6 files changed, 559 insertions(+), 8 deletions(-) create mode 100644 test/wcc/939_str_massign_store_cap_run.c diff --git a/Makefile b/Makefile index d3026803..733f1d68 100644 --- a/Makefile +++ b/Makefile @@ -261,6 +261,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_str_arrfield_cap_run \ $(BIN)/test_str_arrfield_store_cap_run \ $(BIN)/test_str_chainfield_store_cap_run \ + $(BIN)/test_str_massign_store_cap_run \ $(BIN)/test_composite_call_arg \ $(BIN)/test_composite_call_arg_run \ $(BIN)/test_letdecl_zeroinit \ @@ -669,6 +670,12 @@ $(BIN)/test_str_chainfield_store_cap_run: test/wcc/938_str_chainfield_store_cap_ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_str_massign_store_cap_run: test/wcc/939_str_massign_store_cap_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 6c010708..6cba67ff 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -7479,7 +7479,8 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) * 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). */ + * str IS []u8 (24B): the cap rides R8 (#1/Phase 3, task #5). + * one-str only; two-str destructure is gap (task #22). */ cgexpr(c, n->rhs, *locals); Node *l0 = n->list; Node *l1 = l0 ? l0->next : NULL; @@ -7525,9 +7526,41 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) } case N_MASSIGN: { cgexpr(c, n->rhs, *locals); - ins1(c, A_PUSHQ, areg(D_DX)); 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)); + } 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)); + } + break; + } + } + ins1(c, A_PUSHQ, areg(D_DX)); if (l0 && l0->kind == N_IDENT) { int off = localfind(*locals, l0->str); if (off != 0) diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 52def505..e5d074c8 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -21049,11 +21049,111 @@ 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 = { - if (n.rhs != nil) { cgexpr(c, n.rhs); }; - emitline("\tPUSHQ\tDX\n"); 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); + + 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; + }; + }; + }; + }; + }; + }; + }; + + emitline("\tPUSHQ\tDX\n"); if (l0 != nil) { if (l0.kind == nkind.N_IDENT) { let off: i32 = localfind(c, l0.str); diff --git a/selfhost/cmd/wcc/cgenstmt.ww b/selfhost/cmd/wcc/cgenstmt.ww index 07bd06af..473d1cd0 100644 --- a/selfhost/cmd/wcc/cgenstmt.ww +++ b/selfhost/cmd/wcc/cgenstmt.ww @@ -1197,11 +1197,111 @@ 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 = { - if (n.rhs != nil) { cgexpr(c, n.rhs); }; - emitline("\tPUSHQ\tDX\n"); 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); + + 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; + }; + }; + }; + }; + }; + }; + }; + + emitline("\tPUSHQ\tDX\n"); if (l0 != nil) { if (l0.kind == nkind.N_IDENT) { let off: i32 = localfind(c, l0.str); diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index ce25fe57..0d8d629a 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -21049,11 +21049,111 @@ 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 = { - if (n.rhs != nil) { cgexpr(c, n.rhs); }; - emitline("\tPUSHQ\tDX\n"); 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); + + 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; + }; + }; + }; + }; + }; + }; + }; + + emitline("\tPUSHQ\tDX\n"); if (l0 != nil) { if (l0.kind == nkind.N_IDENT) { let off: i32 = localfind(c, l0.str); diff --git a/test/wcc/939_str_massign_store_cap_run.c b/test/wcc/939_str_massign_store_cap_run.c new file mode 100644 index 00000000..6f6a12fc --- /dev/null +++ b/test/wcc/939_str_massign_store_cap_run.c @@ -0,0 +1,211 @@ +/* + * 939_str_massign_store_cap_run — runtime coverage for the G3 fold: a + * tuple-destructure REASSIGNMENT `a, s = call()` (N_MASSIGN) whose str element + * must write the full 24B {ptr,len,cap} header into the (already 24B) str slot, + * not just {ptr}. str is 24B since Phase 2 (#1); the N_MASSIGN arm previously + * stored the str element with the bare scalar path (PUSHQ DX; ...; MOVQ DX, + * slot) — one word, dropping len/cap. This is the third and final STORE-cluster + * fold (after arrfield 937 and chainfield 938), closing the store cluster on + * the tuple-destructure REASSIGN shape. + * + * N_MASSIGN vs N_MLET: `let a, s = call()` is N_MLET (fresh bindings, slots + * sized from each binding's type) and ALREADY destructures 3-word. `a, s = + * call()` with a, s PRE-DECLARED is N_MASSIGN (reassignment, slots already + * exist). The fix mirrors N_MLET's (DX,CX,R8)->(.ptr,.len,.cap) routing at the + * N_MASSIGN arm but localfinds the existing slot instead of allocating it. + * The fixture MUST pre-declare a and s then reassign WITHOUT `let`, or the + * parser emits N_MLET and this arm is never reached. + * + * Sites: cgen.c's N_MASSIGN case (str-XOR branch, l0/l1 N_IDENT, str-ness from + * the checker-stamped l->type) and the cgenstmt.ww cgmassign twin (wwstage has + * no checker, so str-ness comes from the called fn's return-type tuple element, + * exactly as cgmlet derives it). ONE-STR ONLY, matching N_MLET's coverage; the + * two-str destructure is a shared N_MLET/N_MASSIGN gap (task #22). + * + * Rows exercise BOTH XOR positions (the routing is position-agnostic, as in + * N_MLET): + * A `a, s = mk()` mk() returns (i64, str) — str is the 2nd element (l1). + * B `s, a = mk2()` mk2() returns (str, i64) — str is the 1st element (l0). + * + * RHS cap!=len: the returned str is a literal whose .cap is MUTATED to a value + * DISTINCT from its len (NOT a bare literal — literals carry cap==len, which + * would hide a dropped cap; a `buf[lo:hi]` sub-slice was rejected too: ww + * yields cap==len for it, task #20, equally hiding the drop). cap=8 and len=2 + * are both nonzero and unequal so a 1-word store that drops cap is detectable. + * + * PRE-POISON (cap=5 != 8, len=4 != 2, both nonzero): before the G3 store under + * test, all three slot words of `s` are seeded with a DIFFERENT str (ptr='q', + * len=4, cap=5) via `let s: str = q` — a PROVEN already-3-word let-init copy + * (the same path 935's spoil() relies on), NOT the G3 store itself (if G3 is + * broken its own poison write would also drop cap, leaving uninit words rather + * than a controlled poison). The let-init both DECLARES s (so the subsequent + * comma-assign is a reassignment = N_MASSIGN) and poisons its slot. A broken + * 1-word G3 store writes only s.ptr and never touches +8/+16, so the 3-word + * read-back observes the poison len 4 / cap 5, never 2 / 8 — deterministic + * discrimination with no reliance on a stale register. + * + * FULL-TRIPLE ASSERT: a register-reallocation slip in the 3-word store could + * clobber ptr while wiring len/cap, so each row reads s back (landed 3-word + * tuple/let reads) and checks ptr (first byte through it — 'h'=104), len (2), + * cap (8), plus the scalar side a (5) — confirming the XOR branch wires the + * scalar slot too. The broken 1-word store writes ptr correctly (it IS the one + * word it keeps), so len AND cap are the fail-before discriminators; ptr and a + * guard the fix. + * + * Verified fail-before (reverted the store edit on BOTH stages → all rows exit + * 1, cap reads the poison 5) / pass-after (exit 0), both the cstage `ww` and + * wwstage `ww_ww` drivers. Confirmed via emitted asm the fixture hits the + * N_MASSIGN arm (CALL ...; MOVQ {AX,DX,CX,R8} -> slots), not N_MLET. + */ +#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; +} + +struct row { const char *label; const char *src; int want; }; + +static const struct row rows[] = { + /* A — `a, s = mk()`, str the 2nd tuple element (l1). a and s are + * pre-declared (so the comma-assign is N_MASSIGN, not N_MLET); s is + * poisoned (cap=5,len=4,'q') by its let-init copy. The G3 store then + * lands the test str (cap=8,len=2,'h') via (DX,CX,R8)->s, AX->a. */ + { "massign_store_str_pos1", + "fn mk() (i64, str) = {\n" + " let p: str = \"hi\"; p.cap = 8i32;\n" + " return (5i64, p);\n" + "};\n" + "export fn main() i32 = {\n" + " let q: str = \"qqqq\"; q.cap = 5i32;\n" + " let a: i64 = 7i64;\n" + " let s: str = q;\n" + " a, s = mk();\n" + " if (s.cap: i32 != 8) { return 1; };\n" + " if (s.len: i32 != 2) { return 2; };\n" + " if (s[0] != 104u8) { return 3; };\n" + " if (a != 5) { return 4; };\n" + " return 0;\n" + "};\n", + 0 }, + /* B — `s, a = mk2()`, str the 1st tuple element (l0). Same poison and + * test values; exercises the s0_is_str routing (DX,CX,R8)->s, AX->a + * from the other XOR side. */ + { "massign_store_str_pos0", + "fn mk2() (str, i64) = {\n" + " let p: str = \"hi\"; p.cap = 8i32;\n" + " return (p, 5i64);\n" + "};\n" + "export fn main() i32 = {\n" + " let q: str = \"qqqq\"; q.cap = 5i32;\n" + " let s: str = q;\n" + " let a: i64 = 7i64;\n" + " s, a = mk2();\n" + " if (s.cap: i32 != 8) { return 1; };\n" + " if (s.len: i32 != 2) { return 2; };\n" + " if (s[0] != 104u8) { return 3; };\n" + " if (a != 5) { return 4; };\n" + " return 0;\n" + "};\n", + 0 }, +}; + +static int +run_driver(const char *driver, const struct row *r, int i) +{ + char src[96], tmpdir[96], cmd[1024]; + snprintf(src, sizeof src, "/tmp/strmassignstore_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/strmassignstore_%d_d_%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", + tmpdir, driver, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: build via %s failed\n", + r->label, driver); + unlink(src); 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); rmdir(tmpdir); + return got; +} + +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, + "str_massign_store_cap_run: skip %s (no %s)\n", + drivers[d].name, drivers[d].path); + continue; + } + for (int i = 0; i < n; i++) { + int got = run_driver(drivers[d].path, &rows[i], i); + total++; + if (got != rows[i].want) { + fprintf(stderr, + "str_massign_store_cap_run[%s][%s]: exit=%d want=%d\n", + drivers[d].name, rows[i].label, + got, rows[i].want); + fail++; + } + } + } + + if (fail) { + fprintf(stderr, "str_massign_store_cap_run: %d/%d fixtures failed\n", + fail, total); + return 1; + } + printf("str_massign_store_cap_run: %d/%d ok\n", total, total); + return 0; +}