From cbbfd1a4a612669f25055d63919f6a60346ec15b Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Fri, 12 Jun 2026 00:04:22 +0900 Subject: [PATCH] wcc/ww: nodeisslice/nodeisstr gain the N_INDEX arm (stamp-keyed) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A slice/str ELEMENT of an indexed expression passed as a call-arg pushed one word instead of the 24B/16B header — the predicates had no N_INDEX arm, so element-typed args fell to the scalar path (review findings #45/#46). Read the element-type stamp; dual-stage rows in 989_idxarg_run pin cs==ww (red 2/8 on pre-fix binaries). --- Makefile | 12 ++ selfhost/cmd/w6c/main.combined.ww | 112 ++++----------- selfhost/cmd/wcc/cgenutil.ww | 112 ++++----------- selfhost/cmd/wwdump/main.combined.ww | 112 ++++----------- test/wcc/989_idxarg_run.c | 204 +++++++++++++++++++++++++++ 5 files changed, 303 insertions(+), 249 deletions(-) create mode 100644 test/wcc/989_idxarg_run.c diff --git a/Makefile b/Makefile index 1e438b3d..13da284d 100644 --- a/Makefile +++ b/Makefile @@ -248,6 +248,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_arr_cap_reject \ $(BIN)/test_tagged_subset_reject \ $(BIN)/test_callarg_typecheck \ + $(BIN)/test_idxarg_run \ $(BIN)/test_arr_ptr_global \ $(BIN)/test_def_arr_infer_len \ $(BIN)/test_def_arr_len \ @@ -632,6 +633,17 @@ $(BIN)/test_callarg_typecheck: test/wcc/989_callarg_typecheck.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +# 989_idxarg_run (F7-c2, #45/#46): an indexed slice/str element passed as +# a call arg must push its full multi-word header. Builds+runs each fixture +# on BOTH the cstage `ww` and wwstage `ww_ww` drivers (rule-10), so it needs +# the full cstage + wwstage tool sets plus libwwrt for the link. +$(BIN)/test_idxarg_run: test/wcc/989_idxarg_run.c \ + $(BIN)/ww $(BIN)/ww_ww \ + $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_let_global: test/wcc/630_let_global.c $(BIN)/ww $(BIN)/w6c \ $(BIN)/w6a $(BIN)/w6l $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 46bcafa7..5232d761 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -18587,6 +18587,17 @@ fn nodeisslice(c: *cgen, n: *node) bool = { if (k == nkind.N_DOT) { return typeisslice(n.type_: *tinfo); }; + // #45 (F7-c2): `arr[i]` whose element is a slice. cgindex leaves + // (AX=ptr, BX=len, CX=cap) for a 24B element, but with no N_INDEX + // arm here pushargsrev fell to the scalar default — one PUSHQ AX — + // and the cgcall pop under-drained by 2 words (take(rows[1]) pushed + // 1 word, callee read garbage len). Read the checker-stamped element + // type (exprtype N_INDEX stamps n.type_, check.ww:2777), mirroring + // cstage node_isslice = type_isslice(n->type) and the N_INDEX arms of + // nodeisstr (below) + nodeisunsigned (:1798). + if (k == nkind.N_INDEX) { + return typeisslice(n.type_: *tinfo); + }; return false; }; @@ -18594,14 +18605,14 @@ fn nodeisslice(c: *cgen, n: *node) bool = { // evaluate to a str value? Used to drive the call-arg push convention // (str args take two slots: ptr + len). // -// TODO(#11): every consumer of "is-str" here reconstructs the answer -// from raw N_kind because wwstage has no typed AST. Each new expression -// shape needs an explicit arm or it silently falls through to false, -// which downstream drops the second slot (BX/len) at the call site. -// A typed AST check (cstage reads n->type) would replace this whole -// function. Covered arms below: N_STRLIT, N_IDENT (local/let-typed), -// N_CALL (return type), N_INDEX (element type of [N]T / []T / *T base), -// N_DOT (reads checker-stamped n.type_ — #56 A.6.3h), N_CAST. +// The value-bearing arms (N_IDENT local, N_INDEX, N_DOT) read the +// checker-stamped n.type_ — the typed-AST check the prior TODO(#11) +// wanted, mirroring cstage node_isstr = type_isstr(n->type). The +// remaining N_kind arms (N_STRLIT, N_CALL, N_CAST) carry their own +// recognizer because the stamp is on a sub-node (callee return / cast +// target), not on `n` itself. Covered: N_STRLIT, N_IDENT (local stamp / +// def stamp), N_CALL (return type), N_INDEX (element stamp — #46 F7-c2), +// N_DOT (n.type_ — #56 A.6.3h), N_CAST. // Not covered (separate bugs / out of scope): // - N_UN(TK_STAR) of `*str` — cgun itself emits only `MOVQ (AX), AX` // and never loads .len into BX; fixing the recognizer alone won't @@ -18663,82 +18674,17 @@ fn nodeisstr(c: *cgen, n: *node) bool = { }; return false; }; - // N_INDEX: `arr[i]` whose base is an indexable type carrying a - // str element. cgindex correctly loads (AX=ptr, BX=len) for a - // 16B element; without this arm pushargsrev only pushes AX and - // the call-arg pop reads .len from stack residue. Mirror of - // cstage's node_isstr → type_isstr(n->type), where n->type is - // the resolved element type after check. + // #46 (F7-c2): `arr[i]` whose element is a str. The prior structural + // walk only recognised N_IDENT and N_DOT bases (idxelemtn off the + // base's tnode), so a chained / call / slice base (take(m[1][1])) + // fell through to `return false` → 1-word push, callee read garbage + // .len. Read the checker-stamped element type instead (exprtype + // N_INDEX stamps n.type_, check.ww:2777), mirroring cstage node_isstr + // = type_isstr(n->type) and nodeisunsigned's N_INDEX arm (:1798). The + // base-kind whitelist is gone — every base shape routes through the + // one stamp read. if (k == nkind.N_INDEX) { - let base: *node = n.lhs; - if (base != nil) { - if (base.kind == nkind.N_IDENT) { - let bt: *node = nil; - let lc: *local = localfindnode(c, base.str); - if (lc != nil) { bt = lc.tnode; } - else { bt = letvartnode(c, base.str); }; - if (bt != nil) { - // idxelemtn: `*[N]T` drills to the pointee - // array's element (#61). - let elem: *node = idxelemtn(bt); - if (elem != nil) { - return isstrtype(c, elem); - }; - }; - }; - // N_INDEX through a struct field: e.g. cmd.argsptr[i] - // where argsptr: *str. cgindex correctly loads the - // (ptr, len) pair off the stamped element size; without - // this arm pushargsrev would only push AX and lose .len. - if (base.kind == nkind.N_DOT) { - let fld: str = base.str; - if (streq(fld, "ptr")) { return false; }; - if (streq(fld, "len")) { return false; }; - if (streq(fld, "cap")) { return false; }; - let inner: *node = base.lhs; - if (inner != nil) { - if (inner.kind == nkind.N_IDENT) { - let lc: *local = localfindnode(c, inner.str); - if (lc != nil) { - let tn: *node = lc.tnode; - let sname: str; - sname.ptr = nil; sname.len = 0; - if (tn != nil) { - if (tn.kind == nkind.N_TNAME) { sname = tn.str; }; - if (tn.kind == nkind.N_TPTR) { - let pinner: *node = tn.lhs; - if (pinner != nil) { - if (pinner.kind == nkind.N_TNAME) { - sname = pinner.str; - }; - }; - }; - }; - if (sname.len > 0) { - let si: *structinfo = structlookup(c, sname); - if (si != nil) { - let fi: *fieldinfo = si.fields; - for (fi != nil) { - if (streq(fi.fname, fld)) { - let ft: *node = fi.tnode; - if (ft != nil) { - // idxelemtn: `*[N]T` drill (#61). - let elem: *node = idxelemtn(ft); - if (elem != nil) { - return isstrtype(c, elem); - }; - }; - }; - fi = fi.finext; - }; - }; - }; - }; - }; - }; - }; - }; - return false; + return typeisstr(n.type_: *tinfo); }; // N_DOT: read the checker-stamped n.type_. Struct field, nested // dot, value-struct hops, and pseudo-fields (.ptr/.len/.cap) all diff --git a/selfhost/cmd/wcc/cgenutil.ww b/selfhost/cmd/wcc/cgenutil.ww index 86fb8026..ab34d290 100644 --- a/selfhost/cmd/wcc/cgenutil.ww +++ b/selfhost/cmd/wcc/cgenutil.ww @@ -1214,6 +1214,17 @@ fn nodeisslice(c: *cgen, n: *node) bool = { if (k == nkind.N_DOT) { return typeisslice(n.type_: *tinfo); }; + // #45 (F7-c2): `arr[i]` whose element is a slice. cgindex leaves + // (AX=ptr, BX=len, CX=cap) for a 24B element, but with no N_INDEX + // arm here pushargsrev fell to the scalar default — one PUSHQ AX — + // and the cgcall pop under-drained by 2 words (take(rows[1]) pushed + // 1 word, callee read garbage len). Read the checker-stamped element + // type (exprtype N_INDEX stamps n.type_, check.ww:2777), mirroring + // cstage node_isslice = type_isslice(n->type) and the N_INDEX arms of + // nodeisstr (below) + nodeisunsigned (:1798). + if (k == nkind.N_INDEX) { + return typeisslice(n.type_: *tinfo); + }; return false; }; @@ -1221,14 +1232,14 @@ fn nodeisslice(c: *cgen, n: *node) bool = { // evaluate to a str value? Used to drive the call-arg push convention // (str args take two slots: ptr + len). // -// TODO(#11): every consumer of "is-str" here reconstructs the answer -// from raw N_kind because wwstage has no typed AST. Each new expression -// shape needs an explicit arm or it silently falls through to false, -// which downstream drops the second slot (BX/len) at the call site. -// A typed AST check (cstage reads n->type) would replace this whole -// function. Covered arms below: N_STRLIT, N_IDENT (local/let-typed), -// N_CALL (return type), N_INDEX (element type of [N]T / []T / *T base), -// N_DOT (reads checker-stamped n.type_ — #56 A.6.3h), N_CAST. +// The value-bearing arms (N_IDENT local, N_INDEX, N_DOT) read the +// checker-stamped n.type_ — the typed-AST check the prior TODO(#11) +// wanted, mirroring cstage node_isstr = type_isstr(n->type). The +// remaining N_kind arms (N_STRLIT, N_CALL, N_CAST) carry their own +// recognizer because the stamp is on a sub-node (callee return / cast +// target), not on `n` itself. Covered: N_STRLIT, N_IDENT (local stamp / +// def stamp), N_CALL (return type), N_INDEX (element stamp — #46 F7-c2), +// N_DOT (n.type_ — #56 A.6.3h), N_CAST. // Not covered (separate bugs / out of scope): // - N_UN(TK_STAR) of `*str` — cgun itself emits only `MOVQ (AX), AX` // and never loads .len into BX; fixing the recognizer alone won't @@ -1290,82 +1301,17 @@ fn nodeisstr(c: *cgen, n: *node) bool = { }; return false; }; - // N_INDEX: `arr[i]` whose base is an indexable type carrying a - // str element. cgindex correctly loads (AX=ptr, BX=len) for a - // 16B element; without this arm pushargsrev only pushes AX and - // the call-arg pop reads .len from stack residue. Mirror of - // cstage's node_isstr → type_isstr(n->type), where n->type is - // the resolved element type after check. + // #46 (F7-c2): `arr[i]` whose element is a str. The prior structural + // walk only recognised N_IDENT and N_DOT bases (idxelemtn off the + // base's tnode), so a chained / call / slice base (take(m[1][1])) + // fell through to `return false` → 1-word push, callee read garbage + // .len. Read the checker-stamped element type instead (exprtype + // N_INDEX stamps n.type_, check.ww:2777), mirroring cstage node_isstr + // = type_isstr(n->type) and nodeisunsigned's N_INDEX arm (:1798). The + // base-kind whitelist is gone — every base shape routes through the + // one stamp read. if (k == nkind.N_INDEX) { - let base: *node = n.lhs; - if (base != nil) { - if (base.kind == nkind.N_IDENT) { - let bt: *node = nil; - let lc: *local = localfindnode(c, base.str); - if (lc != nil) { bt = lc.tnode; } - else { bt = letvartnode(c, base.str); }; - if (bt != nil) { - // idxelemtn: `*[N]T` drills to the pointee - // array's element (#61). - let elem: *node = idxelemtn(bt); - if (elem != nil) { - return isstrtype(c, elem); - }; - }; - }; - // N_INDEX through a struct field: e.g. cmd.argsptr[i] - // where argsptr: *str. cgindex correctly loads the - // (ptr, len) pair off the stamped element size; without - // this arm pushargsrev would only push AX and lose .len. - if (base.kind == nkind.N_DOT) { - let fld: str = base.str; - if (streq(fld, "ptr")) { return false; }; - if (streq(fld, "len")) { return false; }; - if (streq(fld, "cap")) { return false; }; - let inner: *node = base.lhs; - if (inner != nil) { - if (inner.kind == nkind.N_IDENT) { - let lc: *local = localfindnode(c, inner.str); - if (lc != nil) { - let tn: *node = lc.tnode; - let sname: str; - sname.ptr = nil; sname.len = 0; - if (tn != nil) { - if (tn.kind == nkind.N_TNAME) { sname = tn.str; }; - if (tn.kind == nkind.N_TPTR) { - let pinner: *node = tn.lhs; - if (pinner != nil) { - if (pinner.kind == nkind.N_TNAME) { - sname = pinner.str; - }; - }; - }; - }; - if (sname.len > 0) { - let si: *structinfo = structlookup(c, sname); - if (si != nil) { - let fi: *fieldinfo = si.fields; - for (fi != nil) { - if (streq(fi.fname, fld)) { - let ft: *node = fi.tnode; - if (ft != nil) { - // idxelemtn: `*[N]T` drill (#61). - let elem: *node = idxelemtn(ft); - if (elem != nil) { - return isstrtype(c, elem); - }; - }; - }; - fi = fi.finext; - }; - }; - }; - }; - }; - }; - }; - }; - return false; + return typeisstr(n.type_: *tinfo); }; // N_DOT: read the checker-stamped n.type_. Struct field, nested // dot, value-struct hops, and pseudo-fields (.ptr/.len/.cap) all diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 01798111..80db6e15 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -18587,6 +18587,17 @@ fn nodeisslice(c: *cgen, n: *node) bool = { if (k == nkind.N_DOT) { return typeisslice(n.type_: *tinfo); }; + // #45 (F7-c2): `arr[i]` whose element is a slice. cgindex leaves + // (AX=ptr, BX=len, CX=cap) for a 24B element, but with no N_INDEX + // arm here pushargsrev fell to the scalar default — one PUSHQ AX — + // and the cgcall pop under-drained by 2 words (take(rows[1]) pushed + // 1 word, callee read garbage len). Read the checker-stamped element + // type (exprtype N_INDEX stamps n.type_, check.ww:2777), mirroring + // cstage node_isslice = type_isslice(n->type) and the N_INDEX arms of + // nodeisstr (below) + nodeisunsigned (:1798). + if (k == nkind.N_INDEX) { + return typeisslice(n.type_: *tinfo); + }; return false; }; @@ -18594,14 +18605,14 @@ fn nodeisslice(c: *cgen, n: *node) bool = { // evaluate to a str value? Used to drive the call-arg push convention // (str args take two slots: ptr + len). // -// TODO(#11): every consumer of "is-str" here reconstructs the answer -// from raw N_kind because wwstage has no typed AST. Each new expression -// shape needs an explicit arm or it silently falls through to false, -// which downstream drops the second slot (BX/len) at the call site. -// A typed AST check (cstage reads n->type) would replace this whole -// function. Covered arms below: N_STRLIT, N_IDENT (local/let-typed), -// N_CALL (return type), N_INDEX (element type of [N]T / []T / *T base), -// N_DOT (reads checker-stamped n.type_ — #56 A.6.3h), N_CAST. +// The value-bearing arms (N_IDENT local, N_INDEX, N_DOT) read the +// checker-stamped n.type_ — the typed-AST check the prior TODO(#11) +// wanted, mirroring cstage node_isstr = type_isstr(n->type). The +// remaining N_kind arms (N_STRLIT, N_CALL, N_CAST) carry their own +// recognizer because the stamp is on a sub-node (callee return / cast +// target), not on `n` itself. Covered: N_STRLIT, N_IDENT (local stamp / +// def stamp), N_CALL (return type), N_INDEX (element stamp — #46 F7-c2), +// N_DOT (n.type_ — #56 A.6.3h), N_CAST. // Not covered (separate bugs / out of scope): // - N_UN(TK_STAR) of `*str` — cgun itself emits only `MOVQ (AX), AX` // and never loads .len into BX; fixing the recognizer alone won't @@ -18663,82 +18674,17 @@ fn nodeisstr(c: *cgen, n: *node) bool = { }; return false; }; - // N_INDEX: `arr[i]` whose base is an indexable type carrying a - // str element. cgindex correctly loads (AX=ptr, BX=len) for a - // 16B element; without this arm pushargsrev only pushes AX and - // the call-arg pop reads .len from stack residue. Mirror of - // cstage's node_isstr → type_isstr(n->type), where n->type is - // the resolved element type after check. + // #46 (F7-c2): `arr[i]` whose element is a str. The prior structural + // walk only recognised N_IDENT and N_DOT bases (idxelemtn off the + // base's tnode), so a chained / call / slice base (take(m[1][1])) + // fell through to `return false` → 1-word push, callee read garbage + // .len. Read the checker-stamped element type instead (exprtype + // N_INDEX stamps n.type_, check.ww:2777), mirroring cstage node_isstr + // = type_isstr(n->type) and nodeisunsigned's N_INDEX arm (:1798). The + // base-kind whitelist is gone — every base shape routes through the + // one stamp read. if (k == nkind.N_INDEX) { - let base: *node = n.lhs; - if (base != nil) { - if (base.kind == nkind.N_IDENT) { - let bt: *node = nil; - let lc: *local = localfindnode(c, base.str); - if (lc != nil) { bt = lc.tnode; } - else { bt = letvartnode(c, base.str); }; - if (bt != nil) { - // idxelemtn: `*[N]T` drills to the pointee - // array's element (#61). - let elem: *node = idxelemtn(bt); - if (elem != nil) { - return isstrtype(c, elem); - }; - }; - }; - // N_INDEX through a struct field: e.g. cmd.argsptr[i] - // where argsptr: *str. cgindex correctly loads the - // (ptr, len) pair off the stamped element size; without - // this arm pushargsrev would only push AX and lose .len. - if (base.kind == nkind.N_DOT) { - let fld: str = base.str; - if (streq(fld, "ptr")) { return false; }; - if (streq(fld, "len")) { return false; }; - if (streq(fld, "cap")) { return false; }; - let inner: *node = base.lhs; - if (inner != nil) { - if (inner.kind == nkind.N_IDENT) { - let lc: *local = localfindnode(c, inner.str); - if (lc != nil) { - let tn: *node = lc.tnode; - let sname: str; - sname.ptr = nil; sname.len = 0; - if (tn != nil) { - if (tn.kind == nkind.N_TNAME) { sname = tn.str; }; - if (tn.kind == nkind.N_TPTR) { - let pinner: *node = tn.lhs; - if (pinner != nil) { - if (pinner.kind == nkind.N_TNAME) { - sname = pinner.str; - }; - }; - }; - }; - if (sname.len > 0) { - let si: *structinfo = structlookup(c, sname); - if (si != nil) { - let fi: *fieldinfo = si.fields; - for (fi != nil) { - if (streq(fi.fname, fld)) { - let ft: *node = fi.tnode; - if (ft != nil) { - // idxelemtn: `*[N]T` drill (#61). - let elem: *node = idxelemtn(ft); - if (elem != nil) { - return isstrtype(c, elem); - }; - }; - }; - fi = fi.finext; - }; - }; - }; - }; - }; - }; - }; - }; - return false; + return typeisstr(n.type_: *tinfo); }; // N_DOT: read the checker-stamped n.type_. Struct field, nested // dot, value-struct hops, and pseudo-fields (.ptr/.len/.cap) all diff --git a/test/wcc/989_idxarg_run.c b/test/wcc/989_idxarg_run.c new file mode 100644 index 00000000..45d5784e --- /dev/null +++ b/test/wcc/989_idxarg_run.c @@ -0,0 +1,204 @@ +/* + * 989_idxarg_run — F7-c2 (#45/#46): an INDEXED slice/str element passed as + * a call argument must push its full multi-word header, both stages. + * + * THE BUG (cat-A silent miscompile, gate-blind): pushargsrev sizes a + * call arg via nodeisslice/nodeisstr (selfhost/cmd/wcc/cgenutil.ww). A + * str arg takes 2 slots (ptr+len), a slice 3 (ptr+len+cap); a scalar + * takes 1. Pre-fix: + * - nodeisslice had NO N_INDEX arm (#45) → `take(rows[i])` where the + * element is a slice fell to the scalar default: one PUSHQ AX, the + * callee read .len/.cap from stack residue (garbage len). + * - nodeisstr's N_INDEX arm was a base-kind whitelist that only knew + * N_IDENT and N_DOT bases (#46) → a chained index `take(m[1][1])` + * (the outer index's base is itself an N_INDEX) fell through to + * `return false` → 1-word push, garbage .len. + * cstage is type-keyed (cmd/w6c/cgen.c node_isslice/node_isstr = + * type_isXXX(n->type)), so it pushed the full header and ran correct — + * the divergence is the cat-A signature. 990-997 stay green because the + * bootstrap corpus never feeds an indexed slice/str element as a call + * arg, so this value miscompile is invisible to byte-id; only a runtime + * row catches it. THE FIX: both N_INDEX arms read the checker-stamped + * element type n.type_ (exprtype N_INDEX, check.ww), aligning wwstage UP. + * + * Rows (every row builds+runs on cstage `ww` and, when present, wwstage + * `ww_ww`; rule-10 — both stages must agree AND hit want_exit): + * row | shape | want + * -------------------+------------------------------------+------ + * slice_elem_arg | take(rows[1]) rows:[2][]int | 2 [#45 bug] + * call_str_elem | take(getarr()[1]) getarr()->[]str | 5 [#46 bug: + * | the index's base is an N_CALL — non-ident/non-dot, + * | the shape the old base-kind whitelist missed. The + * | slice-base READ already loads the header, so this + * | isolates the PUSH-side fix (c2) cleanly.] + * ident_str_elem | take(arr[1]) arr:[3]str | 5 (#46 control: + * | the N_IDENT-base case the old whitelist DID handle — + * | must still work through the stamp read) + * slice_local_arg | take(xs) xs:[]int | 3 (control: the + * | N_IDENT-local arm, unchanged since c1) + * + * The chained-index `take(m[1][1])` shape (an N_INDEX base) is covered by + * the c3 sibling test (989_chainidx_run.c): its push side is fixed here, + * but it ALSO needs the cgindex read-side header load (#22/c3), so it is + * pinned where both halves are present. + */ +#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_exit; +}; + +static const struct row rows[] = { + /* (1) #45 — slice element `rows[1]` (a []int) as a call arg. Pre-fix + * nodeisslice had no N_INDEX arm → 1-word push, callee read garbage + * len. len(rows[1]) == len(b) == 2. */ + { "slice_elem_arg", + "package main;\n" + "fn take(xs: []int) int = { return len(xs): int; };\n" + "export fn main() int = {\n" + " let a: []int = [10, 20, 30, 40];\n" + " let b: []int = [1, 2];\n" + " let rows: [2][]int = [a, b];\n" + " return take(rows[1]);\n" + "};\n", + 2 }, + + /* (2) #46 — index whose base is an N_CALL (`getarr()[1]`): a non-ident/ + * non-dot base the old base-kind whitelist missed. The slice-base READ + * already loads the (ptr,len) header, so this isolates the PUSH-side + * fix (nodeisstr N_INDEX arm). len("ddddd") == 5. */ + { "call_str_elem", + "package main;\n" + "fn getarr() []str = {\n" + " let a: []str = [\"x\", \"ddddd\", \"zz\"];\n" + " return a;\n" + "};\n" + "fn take(s: str) int = { return len(s): int; };\n" + "export fn main() int = { return take(getarr()[1]); };\n", + 5 }, + + /* (3) #46 control — `arr[1]` with an N_IDENT base (the case the old + * whitelist DID handle): the stamp read must not regress it. */ + { "ident_str_elem", + "package main;\n" + "fn take(s: str) int = { return len(s): int; };\n" + "export fn main() int = {\n" + " let arr: [3]str = [\"x\", \"ddddd\", \"zz\"];\n" + " return take(arr[1]);\n" + "};\n", + 5 }, + + /* (4) control — a plain str/slice local arg (the c1 N_IDENT-local + * arm): pins that c2 leaves the non-indexed path alone. */ + { "slice_local_arg", + "package main;\n" + "fn take(xs: []int) int = { return len(xs): int; };\n" + "export fn main() int = {\n" + " let xs: []int = [7, 8, 9];\n" + " return take(xs);\n" + "};\n", + 3 }, +}; + +/* run_build — build+run `src` via `driver`; returns the binary's exit + * code, or -1 on a build failure. */ +static int +run_build(const char *driver, const struct row *r, int i) +{ + char src[64], tmpdir[64], cmd[1024]; + snprintf(src, sizeof src, "/tmp/idxarg_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/idxarg_%d_d_%d", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -2; + fputs(r->src, f); + fclose(f); + + mkdir(tmpdir, 0755); + snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null", + tmpdir, driver, src); + int brc = runwait(cmd); + + const char *base = strrchr(src, '/'); + base = base ? base + 1 : src; + char outbin[128]; + snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); + char *dot = strrchr(outbin, '.'); + if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; + + int got = -1; + if (brc == 0) got = runwait(outbin); + + unlink(src); unlink(outbin); rmdir(tmpdir); + return brc == 0 ? got : -1; +} + +int +main(void) +{ + const char *bin = getenv("BIN"); + if (!bin) bin = "out/bin"; + char absbin[1024]; + if (bin[0] != '/') { + char cwd[1024]; + if (getcwd(cwd, sizeof cwd) == NULL) return 1; + snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); + bin = absbin; + } + + char cdrv[1024], wdrv[1024]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); + + struct { const char *name; const char *drv; int gated; } + drivers[] = { + { "cstage", cdrv, 0 }, + { "wwstage", wdrv, 1 }, + { NULL, NULL, 0 }, + }; + + int n = (int)(sizeof rows / sizeof rows[0]); + int total = 0, fail = 0; + + for (int d = 0; drivers[d].name; d++) { + if (drivers[d].gated && access(drivers[d].drv, X_OK) != 0) { + fprintf(stderr, "idxarg_run: skip %s (no %s)\n", + drivers[d].name, drivers[d].drv); + continue; + } + for (int i = 0; i < n; i++) { + total++; + int got = run_build(drivers[d].drv, &rows[i], i); + if (got != rows[i].want_exit) { + fprintf(stderr, "idxarg_run[%s][%s]: exit=%d " + "want=%d\n", drivers[d].name, rows[i].label, + got, rows[i].want_exit); + fail++; + } + } + } + + if (fail) { + fprintf(stderr, "idxarg_run: %d/%d fixtures failed\n", + fail, total); + return 1; + } + printf("idxarg_run: %d/%d ok\n", total, total); + return 0; +}