From 74e60e89f0d45e36077bbfa190f111966b638e99 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Sun, 7 Jun 2026 12:26:57 +0900 Subject: [PATCH] =?UTF-8?q?wcc/cgen:=20#66(b-i)=20wwstage=20inferred-liter?= =?UTF-8?q?al=20scalar=20global=20=E2=80=94=20default=20annotation=20to=20?= =?UTF-8?q?int,=20emit=20DATA+load=20(align=20up)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An inferred-literal scalar module-global (`let s = 42;`) was wwstage silent-wrong: the checker stamps the annotation N_TNAME("untyped_int"), which letscalarprim does not recognise, so letemitsize returns 0 — the global is dropped from collectlets (no DATA emitted) AND cgident falls to the silent module-leaf (no load), running garbage. cstage defaults untyped_int to an 8B int before emit (DATAW + MOVQ), which is correct. Fix (wwstage-only, align up to cstage): defaultinferredlets in cgen.ww, called from cgfile (cgendecl.ww) before collectlets, rewrites the annotation "untyped_int" -> "int" (8B machine word, NOT i32 — the #108 truncation trap is the opposite polarity) for a module-level N_LET whose rhs is N_INTLIT. All three consumers (letemitsize, emitletdataw, cgident global-read) then resolve a concrete int. cstage is untouched. Scope: N_INTLIT only. A const-expr inferred global (`let s = 7*6;`, N_BIN) stays on its existing path — that is a separate live cs!=ww silent miscompile tracked as #133, out of scope here. Pin: 947_inferred_scalar_global_run — inferred `let s=42` (42, base wwstage garbage) + typed control, cs==ww byte-id. --- Makefile | 6 + selfhost/cmd/w6c/main.combined.ww | 35 +++++ selfhost/cmd/wcc/cgen.ww | 34 ++++ selfhost/cmd/wcc/cgendecl.ww | 1 + selfhost/cmd/wwdump/main.combined.ww | 35 +++++ test/wcc/947_inferred_scalar_global_run.c | 181 ++++++++++++++++++++++ 6 files changed, 292 insertions(+) create mode 100644 test/wcc/947_inferred_scalar_global_run.c diff --git a/Makefile b/Makefile index 480fe229..41c56491 100644 --- a/Makefile +++ b/Makefile @@ -460,6 +460,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_checked_run \ $(BIN)/test_floatarr_run \ $(BIN)/test_deref_narrow_run \ + $(BIN)/test_inferred_scalar_global_run \ $(BIN)/test_idx_compound_run \ $(BIN)/test_ptrarr_index_run \ $(BIN)/test_dotbase_arr_run \ @@ -2095,6 +2096,11 @@ $(BIN)/test_deref_narrow_run: test/wcc/947_deref_narrow_run.c $(BIN)/ww \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_inferred_scalar_global_run: test/wcc/947_inferred_scalar_global_run.c $(BIN)/ww \ + $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_idx_compound_run: test/wcc/948_idx_compound_run.c $(BIN)/ww \ $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \ $(LIB)/libwwrt.a | $(BIN) diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index e23f7595..dd8dbba0 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -38056,6 +38056,7 @@ export fn cgfile(c: *cgen, file: *node) void = { collectfnrets(c, file); fficollect(c, file); collectmods(c, file); + defaultinferredlets(c, file); collectlets(c, file); let d: *node = file.list; for (d != nil) { @@ -39114,6 +39115,40 @@ fn letemitsize(c: *cgen, d: *node) i32 = { return 0; }; +// defaultinferredlets — #66(b-i): an inferred module-global int-literal +// `let s = 42;` is stamped by the checker with an N_TNAME("untyped_int") +// annotation (d.lhs). letemitsize / emitletdataw / the cgident global-read arm +// key on that annotation's name, which letscalarprim doesn't recognise → the +// global is dropped from collectlets (no DATAW) and the read falls to the +// silent module-leaf (no MOVQ, MOVSXD on stale AX → wrong). cstage instead +// type_default's the untyped int to the 8B machine word BEFORE emit. Mirror +// that here at the single global-decl pass: rewrite the annotation to the +// concrete machine word `int` so all three consumers resolve it as an 8B int +// global (DATAW + MOVQ, byte-identical to cstage). int (not i32) per +// [[project_int_machine_word_derived_limits]] — i32 is the #108 truncation +// trap, opposite polarity. +// Guarded to N_INTLIT ONLY: a const-EXPR rhs (`let s = 7*6`, N_BIN) is also +// stamped untyped_int but is #66(b-ii) — a SEPARATE loud both-stage gap (no +// DATA → link-fail) — and must stay on its loud route, not be defaulted to a +// silent value. Runs before collectlets in cgfile so the mutated d.lhs is +// visible to letpreintern + emitletdataw too. +fn defaultinferredlets(c: *cgen, file: *node) void = { + if (file == nil) { return; }; + let d: *node = file.list; + for (d != nil) { + if (d.kind == nkind.N_LET) { + if (d.lhs != nil && d.rhs != nil) { + if (d.lhs.kind == nkind.N_TNAME + && streq(d.lhs.str, "untyped_int") + && d.rhs.kind == nkind.N_INTLIT) { + d.lhs.str = "int"; + }; + }; + }; + d = d.next; + }; +}; + fn collectlets(c: *cgen, file: *node) void = { c.lets = nil; if (file == nil) { return; }; diff --git a/selfhost/cmd/wcc/cgen.ww b/selfhost/cmd/wcc/cgen.ww index c6663d2b..344e6dba 100644 --- a/selfhost/cmd/wcc/cgen.ww +++ b/selfhost/cmd/wcc/cgen.ww @@ -1040,6 +1040,40 @@ fn letemitsize(c: *cgen, d: *node) i32 = { return 0; }; +// defaultinferredlets — #66(b-i): an inferred module-global int-literal +// `let s = 42;` is stamped by the checker with an N_TNAME("untyped_int") +// annotation (d.lhs). letemitsize / emitletdataw / the cgident global-read arm +// key on that annotation's name, which letscalarprim doesn't recognise → the +// global is dropped from collectlets (no DATAW) and the read falls to the +// silent module-leaf (no MOVQ, MOVSXD on stale AX → wrong). cstage instead +// type_default's the untyped int to the 8B machine word BEFORE emit. Mirror +// that here at the single global-decl pass: rewrite the annotation to the +// concrete machine word `int` so all three consumers resolve it as an 8B int +// global (DATAW + MOVQ, byte-identical to cstage). int (not i32) per +// [[project_int_machine_word_derived_limits]] — i32 is the #108 truncation +// trap, opposite polarity. +// Guarded to N_INTLIT ONLY: a const-EXPR rhs (`let s = 7*6`, N_BIN) is also +// stamped untyped_int but is #66(b-ii) — a SEPARATE loud both-stage gap (no +// DATA → link-fail) — and must stay on its loud route, not be defaulted to a +// silent value. Runs before collectlets in cgfile so the mutated d.lhs is +// visible to letpreintern + emitletdataw too. +fn defaultinferredlets(c: *cgen, file: *node) void = { + if (file == nil) { return; }; + let d: *node = file.list; + for (d != nil) { + if (d.kind == nkind.N_LET) { + if (d.lhs != nil && d.rhs != nil) { + if (d.lhs.kind == nkind.N_TNAME + && streq(d.lhs.str, "untyped_int") + && d.rhs.kind == nkind.N_INTLIT) { + d.lhs.str = "int"; + }; + }; + }; + d = d.next; + }; +}; + fn collectlets(c: *cgen, file: *node) void = { c.lets = nil; if (file == nil) { return; }; diff --git a/selfhost/cmd/wcc/cgendecl.ww b/selfhost/cmd/wcc/cgendecl.ww index bc041d87..5f931d39 100644 --- a/selfhost/cmd/wcc/cgendecl.ww +++ b/selfhost/cmd/wcc/cgendecl.ww @@ -619,6 +619,7 @@ export fn cgfile(c: *cgen, file: *node) void = { collectfnrets(c, file); fficollect(c, file); collectmods(c, file); + defaultinferredlets(c, file); collectlets(c, file); let d: *node = file.list; for (d != nil) { diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index ee3a1a23..e9c21e71 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -38056,6 +38056,7 @@ export fn cgfile(c: *cgen, file: *node) void = { collectfnrets(c, file); fficollect(c, file); collectmods(c, file); + defaultinferredlets(c, file); collectlets(c, file); let d: *node = file.list; for (d != nil) { @@ -39114,6 +39115,40 @@ fn letemitsize(c: *cgen, d: *node) i32 = { return 0; }; +// defaultinferredlets — #66(b-i): an inferred module-global int-literal +// `let s = 42;` is stamped by the checker with an N_TNAME("untyped_int") +// annotation (d.lhs). letemitsize / emitletdataw / the cgident global-read arm +// key on that annotation's name, which letscalarprim doesn't recognise → the +// global is dropped from collectlets (no DATAW) and the read falls to the +// silent module-leaf (no MOVQ, MOVSXD on stale AX → wrong). cstage instead +// type_default's the untyped int to the 8B machine word BEFORE emit. Mirror +// that here at the single global-decl pass: rewrite the annotation to the +// concrete machine word `int` so all three consumers resolve it as an 8B int +// global (DATAW + MOVQ, byte-identical to cstage). int (not i32) per +// [[project_int_machine_word_derived_limits]] — i32 is the #108 truncation +// trap, opposite polarity. +// Guarded to N_INTLIT ONLY: a const-EXPR rhs (`let s = 7*6`, N_BIN) is also +// stamped untyped_int but is #66(b-ii) — a SEPARATE loud both-stage gap (no +// DATA → link-fail) — and must stay on its loud route, not be defaulted to a +// silent value. Runs before collectlets in cgfile so the mutated d.lhs is +// visible to letpreintern + emitletdataw too. +fn defaultinferredlets(c: *cgen, file: *node) void = { + if (file == nil) { return; }; + let d: *node = file.list; + for (d != nil) { + if (d.kind == nkind.N_LET) { + if (d.lhs != nil && d.rhs != nil) { + if (d.lhs.kind == nkind.N_TNAME + && streq(d.lhs.str, "untyped_int") + && d.rhs.kind == nkind.N_INTLIT) { + d.lhs.str = "int"; + }; + }; + }; + d = d.next; + }; +}; + fn collectlets(c: *cgen, file: *node) void = { c.lets = nil; if (file == nil) { return; }; diff --git a/test/wcc/947_inferred_scalar_global_run.c b/test/wcc/947_inferred_scalar_global_run.c new file mode 100644 index 00000000..e8a721cd --- /dev/null +++ b/test/wcc/947_inferred_scalar_global_run.c @@ -0,0 +1,181 @@ +/* + * 947_inferred_scalar_global_run — runtime + byte-id net for #66(b-i): an + * INFERRED int-literal module-global `let s = 42;` (no type annotation) was + * wwstage SILENT-WRONG. The checker stamps such a global with an + * N_TNAME("untyped_int") annotation; letemitsize / emitletdataw / the cgident + * global-read arm key on that annotation's name, which letscalarprim doesn't + * recognise — so the global was dropped from collectlets (no DATAW emitted) + * AND the read fell to the silent module-leaf (no `MOVQ main.s(SB)`), leaving + * `MOVSXD` to sign-extend a STALE AX. cstage instead type_default's the untyped + * int to the 8B machine word before emit, so it produced the correct + * `DATAW main.s` + `MOVQ main.s(SB), AX` (exit 42). cs≠ww, ww silent-wrong. + * + * Fix (wwstage only, cgen.ww defaultinferredlets, one choke-point before + * collectlets): rewrite the untyped_int annotation to the concrete machine + * word `int` — NOT i32 (that's the #108 truncation trap, opposite polarity) — + * so all three consumers resolve it as an 8B int global. The emitted DATAW + + * MOVQ then match cstage byte-for-byte (align ww UP). Guarded to N_INTLIT rhs + * ONLY: a const-EXPR inferred global (`let s = 7*6`) is #66(b-ii), a SEPARATE + * loud both-stage no-DATA gap, and must stay on its loud route. + * + * Each row carries (a) a cstage `ww build` + run asserting the exit code (R1 + * pre-fix: wwstage exit 136 / asm differs), and (b) a w6c vs w6c_ww `.s` cmp + * (rule-10 byte-id). R2 is the typed-annotation control (already cs==ww/42), + * locking no regression on the annotated path. + */ +#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[] = { + /* R1 — the #66(b-i) repro: inferred int-literal module-global, read + * back from main. Pre-fix wwstage emitted neither the DATAW nor the + * MOVQ → MOVSXD on stale AX → exit 136. Post-fix cs==ww, exit 42. */ + { "inferred", + "package main;\n" + "let s = 42;\n" + "export fn main() i32 = {\n" + " return s: i32;\n" + "};\n", 42 }, + /* R2 — typed-annotation control: `let s: i64 = 42;` already worked + * (cs==ww, DATA emitted). Regression guard for the annotated path. */ + { "typed_ctrl", + "package main;\n" + "let s: i64 = 42;\n" + "export fn main() i32 = {\n" + " return s: i32;\n" + "};\n", 42 }, + { 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, "infglobal: 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/wwisg_%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. */ + char tmpdir[64]; + snprintf(tmpdir, sizeof tmpdir, "/tmp/wwisg_%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. */ + char cs_s[64], ws_s[64]; + snprintf(cs_s, sizeof cs_s, "/tmp/wwisg_%d_%d_cs.s", + getpid(), i); + snprintf(ws_s, sizeof ws_s, "/tmp/wwisg_%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 inferred-scalar-global tests failed\n", + fail, n); + return 1; + } + printf("infglobal: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n); + return 0; +}