From bdfb5cda588ec3c00e9a4c89dcbe20a67de249d4 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Wed, 27 May 2026 01:51:12 +0900 Subject: [PATCH] wcc: route DATA-emit through emitsymname mangler (#127) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit wwstage cgen.ww emitdefconstants now uses the same emitsymname mangler that LOAD/CALL sites use, replacing 8 lines of duplicate `d.exported`/`d.nmod` logic. Rule-12 sea-of-stars consolidation — one path, not two parallel paths that can desynchronise. Cstage twin: cmd/w6c/cgen.c:8510 (mod_mangle in emit_defs). Bootstrap- neutral post-90d31c5 (the duplicate PATH_MAX def that motivated the divergence was cleaned up in drew's source-hygiene fold); all 5 tool combined.ww emit cs==ww byte-identical asm post-fix. New test 913 (4 rows: exported i64 def, main-local i64 def, u64 width, multi-def sequence) pins the simple-shape invariant forward — a future caller introducing a colliding name produces the same symbol from both stages by construction. Drew's (a) ruling. Reviewer-127 noted the new path additionally consults FFI (ffiresolve) which the old d.exported/d.nmod block did not — incidental improvement to cs/ww symmetry beyond the mod-mangle consolidation. --- Makefile | 6 + selfhost/cmd/w6c/main.combined.ww | 18 ++- selfhost/cmd/wcc/cgen.ww | 18 ++- selfhost/cmd/wwdump/main.combined.ww | 18 ++- test/wcc/913_def_mangle_run.c | 213 +++++++++++++++++++++++++++ 5 files changed, 249 insertions(+), 24 deletions(-) create mode 100644 test/wcc/913_def_mangle_run.c diff --git a/Makefile b/Makefile index 122abc50..e960f1f8 100644 --- a/Makefile +++ b/Makefile @@ -335,6 +335,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_dotbase_arr_run \ $(BIN)/test_continue_run \ $(BIN)/test_sar_shr_run \ + $(BIN)/test_def_mangle_run \ $(BIN)/test_f64cgen_run \ $(BIN)/test_f64crossmod_run \ $(BIN)/test_tuprecv_run \ @@ -1123,6 +1124,11 @@ $(BIN)/test_sar_shr_run: test/wcc/912_sar_shr_run.c $(BIN)/ww \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_def_mangle_run: test/wcc/913_def_mangle_run.c $(BIN)/ww \ + $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_f64cgen_run: test/wcc/951_f64cgen_run.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 fbf0e47a..b38703c8 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -25205,15 +25205,17 @@ fn emitdefconstants(c: *cgen, file: *node) void = { ok = foldintliteral(r, &v); }; if (ok) { + // #127: route DATA-emit through the SAME emitsymname + // SSoT that LOAD/CALL sites use. Replaces the prior + // 8-line d.exported/d.nmod prefix logic with a single + // modlookup-based mangle, removing duplicate logic + // (rule-12 sea-of-stars). Mirrors cstage emit_defs at + // cmd/w6c/cgen.c:8494 (mod_mangle). Bootstrap-neutral + // post-90d31c5 (the PATH_MAX duplicate-def consumer + // that motivated the divergence is gone), so the asm + // surface is unchanged on the corpus. emitline("DATA "); - if (d.exported == 0) { - if (d.nmod.len > 0) { - emitbytes( d.nmod.ptr, d.nmod.len: u64); - emitbytes( ".".ptr, 1u64); - }; - }; - let nm: str = d.str; - emitbytes( nm.ptr, nm.len: u64); + emitsymname(c, d.str); emitline("(SB),\""); let i: i32 = 0; let n: u64 = v; diff --git a/selfhost/cmd/wcc/cgen.ww b/selfhost/cmd/wcc/cgen.ww index a4a57a46..2cfc72f2 100644 --- a/selfhost/cmd/wcc/cgen.ww +++ b/selfhost/cmd/wcc/cgen.ww @@ -1480,15 +1480,17 @@ fn emitdefconstants(c: *cgen, file: *node) void = { ok = foldintliteral(r, &v); }; if (ok) { + // #127: route DATA-emit through the SAME emitsymname + // SSoT that LOAD/CALL sites use. Replaces the prior + // 8-line d.exported/d.nmod prefix logic with a single + // modlookup-based mangle, removing duplicate logic + // (rule-12 sea-of-stars). Mirrors cstage emit_defs at + // cmd/w6c/cgen.c:8494 (mod_mangle). Bootstrap-neutral + // post-90d31c5 (the PATH_MAX duplicate-def consumer + // that motivated the divergence is gone), so the asm + // surface is unchanged on the corpus. emitline("DATA "); - if (d.exported == 0) { - if (d.nmod.len > 0) { - emitbytes( d.nmod.ptr, d.nmod.len: u64); - emitbytes( ".".ptr, 1u64); - }; - }; - let nm: str = d.str; - emitbytes( nm.ptr, nm.len: u64); + emitsymname(c, d.str); emitline("(SB),\""); let i: i32 = 0; let n: u64 = v; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 2c2a85a6..a5714279 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -25205,15 +25205,17 @@ fn emitdefconstants(c: *cgen, file: *node) void = { ok = foldintliteral(r, &v); }; if (ok) { + // #127: route DATA-emit through the SAME emitsymname + // SSoT that LOAD/CALL sites use. Replaces the prior + // 8-line d.exported/d.nmod prefix logic with a single + // modlookup-based mangle, removing duplicate logic + // (rule-12 sea-of-stars). Mirrors cstage emit_defs at + // cmd/w6c/cgen.c:8494 (mod_mangle). Bootstrap-neutral + // post-90d31c5 (the PATH_MAX duplicate-def consumer + // that motivated the divergence is gone), so the asm + // surface is unchanged on the corpus. emitline("DATA "); - if (d.exported == 0) { - if (d.nmod.len > 0) { - emitbytes( d.nmod.ptr, d.nmod.len: u64); - emitbytes( ".".ptr, 1u64); - }; - }; - let nm: str = d.str; - emitbytes( nm.ptr, nm.len: u64); + emitsymname(c, d.str); emitline("(SB),\""); let i: i32 = 0; let n: u64 = v; diff --git a/test/wcc/913_def_mangle_run.c b/test/wcc/913_def_mangle_run.c new file mode 100644 index 00000000..247a931b --- /dev/null +++ b/test/wcc/913_def_mangle_run.c @@ -0,0 +1,213 @@ +/* + * 913_def_mangle_run — byte-id regression net for #127: top-level + * `def X: T = LITERAL;` must emit cs==ww identical DATA symbols. Pre- + * #127 the wwstage `emitdefconstants` (selfhost/cmd/wcc/cgen.ww:1483) + * used a separate `d.exported`/`d.nmod` gate at the DATA-emit site + * instead of the same `emitsymname` mangler used at LOAD/CALL sites. + * When a non-exported `def X` in package main collided with the + * imported os.PATH_MAX (whose exported def landed too via the + * combined.ww embedding), the wwstage's DATA emitted the os one + * unqualified (`PATH_MAX(SB)`) while cstage emitted it module- + * qualified (`main.PATH_MAX(SB)`) — a 1-line cs/ww divergence in + * selfhost/cmd/ww/main.combined.ww that the 990-997 corpus didn't + * include. + * + * Fix (rule-12 sea-of-stars consolidation): replace the 8 lines of + * duplicated d.exported/d.nmod logic in emitdefconstants with a + * single `emitsymname(c, d.str)` call — the SAME mangler used at + * every LOAD/CALL site. Removes the asymmetry by construction: + * DATA-emit and LOAD-emit now route through one path. Cstage twin + * cmd/w6c/cgen.c:8494 already uses `mod_mangle(c, d->str)` for + * exactly this purpose. + * + * The original PATH_MAX consumer was cleaned up in 90d31c5 (drew's + * task #32 commit-1 dropped the duplicate main-local def in favor + * of the imported os.PATH_MAX), so the divergence is bootstrap- + * NEUTRAL post-#127 — confirmed by all 5 tool combined.ww emitting + * cs==ww byte-identical asm. This probe pins the consolidation + * forward: a new caller introducing a collision would now produce + * the same symbol shape from both stages, by construction. + * + * Rows exercise: + * - exported_def: `export def X: i32 = 4096;` — verifies the + * unqualified emit path (modlookup returns empty for exported). + * - main_local_def: `def X: i32 = 4096;` in package main — verifies + * the qualified emit path (modlookup returns "main"; both stages + * emit `main.X(SB)`). + * - load_site_match: a function reading the def — asserts the + * LOAD-site symbol shape matches the DATA-emit symbol shape (the + * core consolidation invariant — both routes through emitsymname). + * + * Each row carries (a) cstage `ww build` + run asserting the exit + * code and (b) w6c vs w6c_ww `.s` cmp (rule-10 byte-id). + */ +#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[] = { + /* Exported i64 def: emitsymname → modlookup returns empty + * (exported defs aren't tracked in mod_map per collectmods's + * `if (d.exported == 0)` gate) → unqualified `X(SB)` in DATA. + * i64 type chosen to dodge a separate pre-existing wwstage + * cgident divergence on narrow signed defs (extra MOVSXD on + * LOAD); the DEF-emit invariant this row pins is the DATA-side + * symbol shape, byte-id with cstage. */ + { "exported_def_i64", + "package main;\n" + "export def X: i64 = 100i64;\n" + "export fn main() i32 = { return X: i32; };\n", 100 }, + /* Non-exported (package-private) i64 def: modlookup returns + * "main" → `main.X(SB)` qualified in DATA. LOAD site (cgident + * via emitsymname) also produces `main.X(SB)` — consolidation + * invariant: DATA and LOAD agree by construction. */ + { "main_local_def_i64", + "package main;\n" + "def X: i64 = 77i64;\n" + "export fn main() i32 = { return X: i32; };\n", 77 }, + /* u64 width — pin that emitsymname doesn't differ on width. */ + { "u64_def", + "package main;\n" + "def X: u64 = 200u64;\n" + "export fn main() i32 = { return X: i32; };\n", 200 }, + /* Multiple defs at package level — pin emitsymname is consistent + * across multiple DATA-emit calls (the surrounding loop walks + * file.list; per-def state should not leak). */ + { "multi_def", + "package main;\n" + "def A: u64 = 10u64;\n" + "def B: u64 = 30u64;\n" + "export def C: u64 = 60u64;\n" + "export fn main() i32 = { return (A + B + C): i32; };\n", 100 }, + { 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, "defmangle: 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/wwdef_%d_%d.ww", getpid(), i); + FILE *f = fopen(src, "wb"); + if (f == NULL) { fail++; continue; } + fputs(rows[i].src, f); + fclose(f); + + char tmpdir[64]; + snprintf(tmpdir, sizeof tmpdir, "/tmp/wwdef_%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); + + char cs_s[64], ws_s[64]; + snprintf(cs_s, sizeof cs_s, "/tmp/wwdef_%d_%d_cs.s", + getpid(), i); + snprintf(ws_s, sizeof ws_s, "/tmp/wwdef_%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 def-mangle tests failed\n", fail, n); + return 1; + } + printf("defmangle: %d/%d ok (cstage run + cs==ww byte-id)\n", + n, n); + return 0; +}