diff --git a/Makefile b/Makefile index 23983a11..aa499028 100644 --- a/Makefile +++ b/Makefile @@ -329,6 +329,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_checked_run \ $(BIN)/test_f64cgen_run \ $(BIN)/test_f64crossmod_run \ + $(BIN)/test_tuprecv_run \ $(BIN)/test_floats_run \ $(BIN)/test_bufio_run $(BIN)/test_random_run @@ -1076,6 +1077,11 @@ $(BIN)/test_f64crossmod_run: test/wcc/953_f64crossmod_run.c $(BIN)/ww \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_tuprecv_run: test/wcc/954_tuprecv_run.c $(BIN)/ww \ + $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + sizelint: @sh tools/sizelint diff --git a/test/wcc/954_tuprecv_run.c b/test/wcc/954_tuprecv_run.c new file mode 100644 index 00000000..1f064bf0 --- /dev/null +++ b/test/wcc/954_tuprecv_run.c @@ -0,0 +1,234 @@ +/* + * 954_tuprecv_run — runtime + byte-id regression net for #102: the 16B + * whole-tuple-from-call receive (`let t = mk()`, then field reads t.0 / + * t.1). + * + * Root: wwstage's cglet had NO 16B tuple-from-call receive branch, so a + * `let t = call()` whose callee returns a 2-eightbyte (16B) tuple fell + * through to the generic single-word store (`MOVQ AX, off(BP)`) and + * never spilled word1 (the DX eightbyte) — silent loss of t.1. cstage + * has the branch (cmd/w6c/cgen.c:6652: spill AX -> off+0 AND DX -> + * off+8). The fix mirrors it in cglet. + * + * NOTE on the diagnosis: this is NOT a tupstore cursor off-by-one, and + * it is NOT f64-specific. The destructure form `let (a,b) = mk()` + * (cgmlet + tupstore cursor) was already byte-id; only the whole-tuple + * N_LET receive dropped word1. An f64 element only made it visible + * first, but the all-integer (i64,i64) receive dropped DX too — so the + * "all-integer control" row below is ALSO a fix row (it diverges on + * master 60c3e51, byte-id after the fix), not a pure no-op control. + * + * THIS TEST MUST CATCH A WWSTAGE-ONLY DIVERGENCE. cstage is correct + * before and after, so a cstage-only probe is gate-blind. Each row + * carries BOTH dimensions (modelled on 953_f64crossmod_run): + * (a) cstage `ww build` + run, asserting the exit code — pins the asm + * both stages converge on as runtime-correct. + * (b) w6c vs w6c_ww `.s` cmp — FAILS if the stages diverge. On master + * 60c3e51 (pre-fix) the three scalar-pair single-var rows each + * diverge by exactly one dropped `MOVQ DX, -8(BP)`; post-fix all + * are byte-identical. + * + * Two CONTROL rows pin that the fix touches nothing else: the + * destructure form `let (a,b) = mk()` (cgmlet + tupstore cursor) and a + * 32B wide/str-element single-var tuple stay byte-id both pre- and + * post-fix (the fix is in cglet/N_LET, gated on sz==16). + */ +#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[] = { + /* mixed (f64, i64): f=2.5 -> i32 2, i=7 -> i32 7, 2+7 = 9. The + * f64 element rides its eightbyte in AX at receive and is re-read + * from X0 at field-read; the i64 (t.1) rides DX — the dropped + * word. */ + { "f64_i64", + "package main;\n" + "fn mk() (f64, i64) = { return (2.5, 7); };\n" + "export fn main() i32 = {\n" + "\tlet t = mk();\n" + "\tlet f: f64 = t.0;\n" + "\tlet i: i64 = t.1;\n" + "\treturn (f: i32) + (i: i32);\n" + "};\n", 9 }, + /* order-swap (i64, f64): f64 in the 2nd slot. i=7, f=2.5 -> 2, + * 7+2 = 9. Confirms the spill is position-agnostic (both words go + * over the integer cursor regardless of which holds the f64). */ + { "i64_f64", + "package main;\n" + "fn mk() (i64, f64) = { return (7, 2.5); };\n" + "export fn main() i32 = {\n" + "\tlet t = mk();\n" + "\tlet i: i64 = t.0;\n" + "\tlet f: f64 = t.1;\n" + "\treturn (i: i32) + (f: i32);\n" + "};\n", 9 }, + /* all-integer (i64, i64): a=5, b=7, 5+7 = 12. NOT a no-op control + * — pre-fix wwstage dropped DX here too (proves the fix is not + * f64-gated). */ + { "i64_i64", + "package main;\n" + "fn mk() (i64, i64) = { return (5, 7); };\n" + "export fn main() i32 = {\n" + "\tlet t = mk();\n" + "\tlet a: i64 = t.0;\n" + "\tlet b: i64 = t.1;\n" + "\treturn (a: i32) + (b: i32);\n" + "};\n", 12 }, + /* CONTROL — destructure form `let (f,i) = mk()` (cgmlet + tupstore + * cursor, a different and already-correct path; bootstrap relies on + * it, which is why 990-997 pass). The fix is in cglet/N_LET only, so + * this stays byte-id pre- and post-fix. f=2.5->2, i=7, 2+7 = 9. */ + { "ctl_destr", + "package main;\n" + "fn mk() (f64, i64) = { return (2.5, 7); };\n" + "export fn main() i32 = {\n" + "\tlet (f, i) = mk();\n" + "\treturn (f: i32) + (i: i32);\n" + "};\n", 9 }, + /* CONTROL — wide/str element single-var (i64, str) is a 32B tuple + * handled by the separate 32B receive branch; the fix's sz==16 gate + * excludes it, so it stays byte-id pre- and post-fix. t.0 = 7. */ + { "ctl_str", + "package main;\n" + "fn mk() (i64, str) = { return (7, \"hi\"); };\n" + "export fn main() i32 = {\n" + "\tlet t: (i64, str) = mk();\n" + "\treturn t.0: i32;\n" + "};\n", 7 }, + { NULL, NULL, 0 } +}; + +static int +slurp_eq(const char *a, const char *b) +{ + FILE *fa = fopen(a, "rb"); + FILE *fb = fopen(b, "rb"); + if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; } + int rc = 0; + for (;;) { + int ca = fgetc(fa); + int cb = fgetc(fb); + if (ca != cb) { rc = -1; break; } + if (ca == EOF) break; + } + fclose(fa); fclose(fb); + return rc; +} + +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 w6c[1100], w6c_ww[1100]; + snprintf(w6c, sizeof w6c, "%s/w6c", bin); + snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin); + if (access(w6c_ww, X_OK) != 0) { + fprintf(stderr, "tuprecv: w6c_ww missing — cannot run the " + "cs==ww byte-id gate (the whole point of this test)\n"); + return 1; + } + + int n = 0, fail = 0; + for (int i = 0; rows[i].src; i++, n++) { + char src[64]; + snprintf(src, sizeof src, "/tmp/wwtup_%d_%d.ww", getpid(), i); + FILE *f = fopen(src, "wb"); + if (f == NULL) { fail++; continue; } + fputs(rows[i].src, f); + fclose(f); + + /* (a) cstage build + run in a scratch dir so the output binary + * (and any intermediates) land there. */ + char tmpdir[64]; + snprintf(tmpdir, sizeof tmpdir, "/tmp/wwtup_%d_d_%d", + getpid(), i); + mkdir(tmpdir, 0755); + + char cmd[2048]; + snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s", + tmpdir, bin, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: cstage build failed\n", + rows[i].label); + fail++; + unlink(src); rmdir(tmpdir); + continue; + } + + char outbin[128]; + const char *base = strrchr(src, '/'); + base = base ? base + 1 : src; + snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); + char *dot = strrchr(outbin, '.'); + if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; + + int got = runwait(outbin); + if (got != rows[i].want_exit) { + fprintf(stderr, "row[%s]: cstage exit %d, want %d\n", + rows[i].label, got, rows[i].want_exit); + fail++; + } + unlink(outbin); rmdir(tmpdir); + + /* (b) cs==ww byte-id gate: emit .s from both stages, cmp. + * FAILS on the pre-fix wwstage divergence (dropped MOVQ DX). */ + char cs_s[64], ws_s[64]; + snprintf(cs_s, sizeof cs_s, "/tmp/wwtup_%d_%d_cs.s", + getpid(), i); + snprintf(ws_s, sizeof ws_s, "/tmp/wwtup_%d_%d_ww.s", + getpid(), i); + + snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", + w6c, cs_s, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label); + fail++; unlink(src); continue; + } + snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", + w6c_ww, ws_s, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: w6c_ww failed\n", + rows[i].label); + fail++; unlink(src); unlink(cs_s); continue; + } + if (slurp_eq(cs_s, ws_s) != 0) { + fprintf(stderr, + "row[%s]: cstage/wwstage .s DIFFER (rule-10 " + "byte-id violation)\n", rows[i].label); + fail++; + } + unlink(src); unlink(cs_s); unlink(ws_s); + } + + if (fail) { + fprintf(stderr, "%d/%d tuple-receive tests failed\n", + fail, n); + return 1; + } + printf("tuprecv: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n); + return 0; +}