diff --git a/Makefile b/Makefile index 733f1d68..30f2bbee 100644 --- a/Makefile +++ b/Makefile @@ -262,6 +262,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_str_arrfield_store_cap_run \ $(BIN)/test_str_chainfield_store_cap_run \ $(BIN)/test_str_massign_store_cap_run \ + $(BIN)/test_str_forrange_loopvar_run \ $(BIN)/test_composite_call_arg \ $(BIN)/test_composite_call_arg_run \ $(BIN)/test_letdecl_zeroinit \ @@ -676,6 +677,12 @@ $(BIN)/test_str_massign_store_cap_run: test/wcc/939_str_massign_store_cap_run.c $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_str_forrange_loopvar_run: test/wcc/940_str_forrange_loopvar_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/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index e5d074c8..6c628d1f 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -21356,6 +21356,27 @@ fn cgforrange(c: *cgen, n: *node) void = { let sk: nkind = slctn.kind; if (sk == nkind.N_TSLICE) { elemt = slctn.lhs; }; if (sk == nkind.N_TARRAY) { elemt = slctn.lhs; }; + // str IS []u8 (F1: tystr.sub = tyu8). []u8 hands cgen a real + // u8 element node (slctn.lhs); a str scrutinee has none, so the + // loop var would register tnode=nil and read back as a wide + // MOVQ. Synthesise the u8 element off str.sub so the loop-var + // registration carries a u8 tnode and localloadop narrows the + // read-back to MOVZBQ on its own — aligning wwstage up to + // cstage, whose checker stamps the binding u8. Kind-gated so + // str's own type stays nominal. + if (sk == nkind.N_TNAME) { + if (streq(slctn.str, "str")) { + let sti: *tinfo = slctn.type_: *tinfo; + if (sti != nil) { + if (sti.sub != nil) { + let u8n: *node = newnode(nkind.N_TNAME, slctn.file, slctn.line, slctn.col); + u8n.str = "u8"; + u8n.type_ = sti.sub: *void; + elemt = u8n; + }; + }; + }; + }; }; // esz: raw elem byte size. For tuple-element slices `[](T0, T1)`, // C cgen reads the resolved tuple's size (sum of raw param sizes, diff --git a/selfhost/cmd/wcc/cgenstmt.ww b/selfhost/cmd/wcc/cgenstmt.ww index 473d1cd0..24ba9d01 100644 --- a/selfhost/cmd/wcc/cgenstmt.ww +++ b/selfhost/cmd/wcc/cgenstmt.ww @@ -1504,6 +1504,27 @@ fn cgforrange(c: *cgen, n: *node) void = { let sk: nkind = slctn.kind; if (sk == nkind.N_TSLICE) { elemt = slctn.lhs; }; if (sk == nkind.N_TARRAY) { elemt = slctn.lhs; }; + // str IS []u8 (F1: tystr.sub = tyu8). []u8 hands cgen a real + // u8 element node (slctn.lhs); a str scrutinee has none, so the + // loop var would register tnode=nil and read back as a wide + // MOVQ. Synthesise the u8 element off str.sub so the loop-var + // registration carries a u8 tnode and localloadop narrows the + // read-back to MOVZBQ on its own — aligning wwstage up to + // cstage, whose checker stamps the binding u8. Kind-gated so + // str's own type stays nominal. + if (sk == nkind.N_TNAME) { + if (streq(slctn.str, "str")) { + let sti: *tinfo = slctn.type_: *tinfo; + if (sti != nil) { + if (sti.sub != nil) { + let u8n: *node = newnode(nkind.N_TNAME, slctn.file, slctn.line, slctn.col); + u8n.str = "u8"; + u8n.type_ = sti.sub: *void; + elemt = u8n; + }; + }; + }; + }; }; // esz: raw elem byte size. For tuple-element slices `[](T0, T1)`, // C cgen reads the resolved tuple's size (sum of raw param sizes, diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 0d8d629a..df74e2c0 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -21356,6 +21356,27 @@ fn cgforrange(c: *cgen, n: *node) void = { let sk: nkind = slctn.kind; if (sk == nkind.N_TSLICE) { elemt = slctn.lhs; }; if (sk == nkind.N_TARRAY) { elemt = slctn.lhs; }; + // str IS []u8 (F1: tystr.sub = tyu8). []u8 hands cgen a real + // u8 element node (slctn.lhs); a str scrutinee has none, so the + // loop var would register tnode=nil and read back as a wide + // MOVQ. Synthesise the u8 element off str.sub so the loop-var + // registration carries a u8 tnode and localloadop narrows the + // read-back to MOVZBQ on its own — aligning wwstage up to + // cstage, whose checker stamps the binding u8. Kind-gated so + // str's own type stays nominal. + if (sk == nkind.N_TNAME) { + if (streq(slctn.str, "str")) { + let sti: *tinfo = slctn.type_: *tinfo; + if (sti != nil) { + if (sti.sub != nil) { + let u8n: *node = newnode(nkind.N_TNAME, slctn.file, slctn.line, slctn.col); + u8n.str = "u8"; + u8n.type_ = sti.sub: *void; + elemt = u8n; + }; + }; + }; + }; }; // esz: raw elem byte size. For tuple-element slices `[](T0, T1)`, // C cgen reads the resolved tuple's size (sum of raw param sizes, diff --git a/test/wcc/940_str_forrange_loopvar_run.c b/test/wcc/940_str_forrange_loopvar_run.c new file mode 100644 index 00000000..3e28df1a --- /dev/null +++ b/test/wcc/940_str_forrange_loopvar_run.c @@ -0,0 +1,169 @@ +/* + * 940_str_forrange_loopvar_run — corpus coverage for the step-3 Fold 1 + * fold: ranging a `str` value-form (`for (let b .. s)`) and reading the + * loop var `b` back must narrow to MOVZBQ (u8 zero-extend), matching + * cstage. Pre-fold the wwstage cgen registered the loop var with a nil + * tnode (str had no element node the way []u8 does), so the read-back + * fell through localloadop to a wide MOVQ; cstage's checker stamps the + * binding u8, so it narrowed. The fold synthesises the u8 element off + * str.sub (Phase 2 F1) so the loop var carries a u8 tnode and the + * existing narrow-load logic fires on its own. + * + * REAL GATE IS SHAPE BYTE-ID, NOT RUNTIME. The MOVQ-vs-MOVZBQ + * divergence is runtime-benign: the slot is always written by a + * MOVZBQ-into-AX then MOVQ-AX-into-slot, so the upper bytes are already + * zero — a MOVQ read-back yields the same value a MOVZBQ would. A + * runtime probe therefore passes on BOTH the buggy and the fixed code + * and CANNOT distinguish them. The load-bearing gate is diffing the + * cstage (w6c) and wwstage (w6c_ww) .s at the loop-var read: + * pre-fold: cstage MOVZBQ vs wwstage MOVQ (1-line diff) + * post-fold: byte-identical + * This fixture exists for corpus presence — it confirms the path lowers + * and runs correctly through both drivers; it does not by itself prove + * the narrow. + * + * Loop-var read-back is exercised two ways the fold touches: + * - Site A: pass `b` to a fn (`use1(b)` → cgident read into an arg). + * - Site B: use `b` in an arithmetic expression (`sum += b: i32`). + */ +#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[] = { + /* Site A — read-back into a fn arg. "AB" is 0x41,0x42; the callee + * checks each byte zero-extends to its exact value, so a botched + * wide read (had the slot held garbage) would mismatch. */ + { "sitea_arg_readback", + "fn check(x: u8) i32 = {\n" + " if (x: i32 < 65) { return 1; };\n" + " if (x: i32 > 66) { return 1; };\n" + " return 0;\n" + "};\n" + "export fn main() i32 = {\n" + " let s: str = \"AB\";\n" + " for (let b .. s) {\n" + " if (check(b) != 0) { return 1; };\n" + " };\n" + " return 0;\n" + "};\n", + 0 }, + /* Site B — read-back in an arithmetic expression. Sum the bytes of + * "AB" (0x41 + 0x42 = 131) and confirm. */ + { "siteb_expr_readback", + "export fn main() i32 = {\n" + " let s: str = \"AB\";\n" + " let sum: i32 = 0;\n" + " for (let b .. s) {\n" + " sum += b: i32;\n" + " };\n" + " if (sum != 131) { return 1; };\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/strforrange_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/strforrange_%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_forrange_loopvar_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_forrange_loopvar_run[%s][%s]: exit=%d want=%d\n", + drivers[d].name, rows[i].label, + got, rows[i].want); + fail++; + } + } + } + + if (fail) { + fprintf(stderr, "str_forrange_loopvar_run: %d/%d fixtures failed\n", + fail, total); + return 1; + } + printf("str_forrange_loopvar_run: %d/%d ok\n", total, total); + return 0; +}