/* * 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 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 tmpdir[96], src[160], outbin[160], rmcmd[160], cmd[1024]; snprintf(tmpdir, sizeof tmpdir, "/tmp/strforrange_%d_d_%d", getpid(), i); mkdir(tmpdir, 0755); snprintf(src, sizeof src, "%s/strforrange_%d_%d.ww", tmpdir, getpid(), i); snprintf(outbin, sizeof outbin, "%s/strforrange_%d_%d", tmpdir, getpid(), i); snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir); FILE *f = fopen(src, "wb"); if (!f) { runwait(rmcmd); return -1; } fputs(r->src, f); fclose(f); snprintf(cmd, sizeof cmd, "%s build -o %s %s", driver, outbin, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: build via %s failed\n", r->label, driver); runwait(rmcmd); return -1; } int got = runwait(outbin); runwait(rmcmd); 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; }