/* * 795_xmod_valglobal_run — project #1 close, the cgen residual of #55. * Runtime + cs==ww byte-id net for the cross-module bare value-GLOBAL * load miscompile. Sibling of the CHECKER test 794_xmod_ident_prefer, * which deliberately omitted a byte-id assertion because THIS cgen bug * would diverge the asm independently (see 794's NOTE). With #1 fixed, * the byte-id now holds and is asserted here. * * THE BUG (cgen-only, both stages emit IDENTICAL wrong asm → byte-id * was BLIND to it): a bare cross-module value-global load mangled its * symbol via a NON-preferring leaf lookup (cstage masym->mod_mangle-> * mod_lookup first-match; wwstage emitsymname->modlookup). With * package aa; export let v: i32 = 7; fn getv() = { return v; } * package main; import aa; let v: i32 = 99; main = { return aa.getv(); } * the bare `v` inside aa.getv resolved to main.v, and aa.v's DATA slot * was ALSO labeled main.v (collision) — so aa.getv() returned 99, not 7. * Functions were already correct (they thread a cur_mod hint via mafn/ * emitfnname); value globals did not. * * THE FIX (#1): reference-site mangle uses the resolved module (curmod- * prefer for bare idents); definition-site mangle uses the decl's OWN * module — threaded per-site the way fns already do (cstage mahint with * c->cur_mod / d->module; wwstage emitsymnamehint with c.curmod / * d.nmod). The value lookup mangles ONLY on an exact (name, module) * match and otherwise leaves the name BARE — no first-leaf fallback — * because exported value globals are export-skipped from the module map * (they keep their bare-name data ABI), and a first-match fallback would * mis-mangle an exported `v` onto another module's private `v`. So the * exported aa.v stays `v`, the private main.v stays `main.v`, no * collision. * * EACH ROW CARRIES BOTH DIMENSIONS (953_f64crossmod_run model): * (a) cstage `ww build` (in a /tmp scratch dir) + run, asserting the * exit code — pins that the converged asm is runtime-correct. * (b) w6c vs w6c_ww `.s` cmp — FAILS if the stages diverge. Necessary * because the bug was byte-id-blind (both stages were wrong the * same way); a green byte-id alone never caught it, so the runtime * row (a) is the real net and (b) guards rule-10 going forward. * * Single-file multi-package form (like 953): `package aa; ... package * main; import aa; ...` in one source, so w6c/w6c_ww see the cross- * module reference without -I path plumbing, and the build is self- * contained (no selfhost traversal → no .combined.ww fixture race, so * this lives in the parallel phase, not the 990-997 skip-set). * * GATE POLARITY: must stay GREEN. A wrong exit means the value-global * mangle regressed (collision returned); a byte-id FAIL means the stages * diverged on the mangle (rule-10 violation). * * SCOPE NOTE: str/slice/array value-globals are intentionally NOT rowed * here — `len(str-global)` and indexed-global reads trip a SEPARATE, * still-open cs!=ww divergence in the load body (the N_DOT-base / index * sibling tracked as #229), orthogonal to this symbol-mangle fix. */ #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[] = { /* canonical #1: exported aa.v (7) vs private main.v (99); aa.getv() * must read aa's v, not collide onto main.v. Pre-fix: 99. */ { "i32_let_export_vs_private", "package aa;\n" "export let v: i32 = 7;\n" "export fn getv() i32 = { return v; };\n" "package main;\n" "import aa;\n" "let v: i32 = 99;\n" "export fn main() i32 = { return aa.getv(); };\n", 7 }, /* def-constant flavour: exported def vs private def, same leaf. */ { "def_export_vs_private", "package aa;\n" "export def K: i32 = 5;\n" "export fn getk() i32 = { return K; };\n" "package main;\n" "import aa;\n" "def K: i32 = 88;\n" "export fn main() i32 = { return aa.getk(); };\n", 5 }, /* float flavour: exercises the LEAQ+MOVSD load arm and the * emit_floatlit_data DATA label. aa.getf() == 2.5 -> i32 2. */ { "f64_let_export_vs_private", "package aa;\n" "export let f: f64 = 2.5;\n" "export fn getf() f64 = { return f; };\n" "package main;\n" "import aa;\n" "let f: f64 = 9.9;\n" "export fn main() i32 = { return aa.getf(): i32; };\n", 2 }, { 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, "xmod_valglobal: w6c_ww missing — cannot run the " "cs==ww byte-id gate\n"); return 1; } int n = 0, fail = 0; for (int i = 0; rows[i].src; i++, n++) { /* src + binary + both .s land under one tmpdir so the * compiler's .sepwork scratch stays inside it; a single * rm -rf at the end reclaims everything. */ char tmpdir[64]; snprintf(tmpdir, sizeof tmpdir, "/tmp/wwxmv_%d_d_%d", getpid(), i); mkdir(tmpdir, 0755); char src[128], outbin[128], rmcmd[160]; snprintf(src, sizeof src, "%s/wwxmv_%d_%d.ww", tmpdir, getpid(), i); snprintf(outbin, sizeof outbin, "%s/wwxmv_%d_%d", tmpdir, getpid(), i); snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir); FILE *f = fopen(src, "wb"); if (f == NULL) { fail++; runwait(rmcmd); continue; } fputs(rows[i].src, f); fclose(f); /* (a) cstage build + run. */ char cmd[2048]; snprintf(cmd, sizeof cmd, "%s/ww build -o %s %s", bin, outbin, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: cstage build failed\n", rows[i].label); fail++; runwait(rmcmd); continue; } 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++; } /* (b) cs==ww byte-id gate. */ char cs_s[128], ws_s[128]; snprintf(cs_s, sizeof cs_s, "%s/wwxmv_%d_%d_cs.s", tmpdir, getpid(), i); snprintf(ws_s, sizeof ws_s, "%s/wwxmv_%d_%d_ww.s", tmpdir, 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++; runwait(rmcmd); 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++; runwait(rmcmd); 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++; } runwait(rmcmd); } if (fail) { fprintf(stderr, "%d/%d xmod value-global tests failed\n", fail, n); return 1; } printf("xmod_valglobal: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n); return 0; }