Files
ww/test/wcc/913_def_mangle_run.c
Hojun-Cho bdfb5cda58 wcc: route DATA-emit through emitsymname mangler (#127)
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.
2026-05-27 01:51:12 +09:00

214 lines
7.1 KiB
C

/*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
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;
}