From e416885d96d22b19eadb5053e9a39073e273f883 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Fri, 12 Jun 2026 09:21:16 +0900 Subject: [PATCH] wcc/ww: widen of a module-global tagged ident copies the whole box Widening a global tagged union into a wider tagged slot copied nothing of the box; treat the global ident as a tagged source and copy the full box from g(SB) through the nested arm. Both-wrong pair: cstage spills frame garbage as the box (filed task #44, residual non-deterministic so the rows assert divergence only). The non-nested SUBSET-widen shape remains open as task #49 (site comment at the fall-through). Review item #51. --- Makefile | 12 ++ selfhost/cmd/w6c/main.combined.ww | 52 ++++++-- selfhost/cmd/wcc/cgenutil.ww | 52 ++++++-- selfhost/cmd/wwdump/main.combined.ww | 52 ++++++-- test/wcc/989_globtagwiden_run.c | 190 +++++++++++++++++++++++++++ 5 files changed, 328 insertions(+), 30 deletions(-) create mode 100644 test/wcc/989_globtagwiden_run.c diff --git a/Makefile b/Makefile index 77958717..85469f9b 100644 --- a/Makefile +++ b/Makefile @@ -262,6 +262,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_globtagreassign_run \ $(BIN)/test_globstructret_run \ $(BIN)/test_globstructwiden_run \ + $(BIN)/test_globtagwiden_run \ $(BIN)/test_arr_ptr_global \ $(BIN)/test_def_arr_infer_len \ $(BIN)/test_def_arr_len \ @@ -792,6 +793,17 @@ $(BIN)/test_globstructwiden_run: test/wcc/989_globstructwiden_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +# 989_globtagwiden_run (F8-c8, #51, #263): widening a module-global tagged +# ident into a wider union must copy the whole box as the nested payload. +# ww-runtime-correct; cstage copies frame garbage — non-deterministic, so the +# cstage rows assert only divergence (cs!=ww, build-ok), cstage half = task #44. +$(BIN)/test_globtagwiden_run: test/wcc/989_globtagwiden_run.c \ + $(BIN)/ww $(BIN)/ww_ww \ + $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_let_global: test/wcc/630_let_global.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 455e9d91..0801f1d4 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -21303,6 +21303,14 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s // cg_widen_tagged_store's nested arm (cmd/w6c/cgen.c). let srctagged: bool = (rhstaggedident(c, src) != nil) || rhstaggedabicall(c, src); + // #51 (#263): a module-global tagged ident is also a tagged source + // (no local slot — handled by the global branch in the nested copy). + if (!srctagged) { if (src.kind == nkind.N_IDENT) { + if (localfindnode(c, src.str) == nil) { + let gtn51: *node = letvartnode(c, src.str); + if (gtn51 != nil) { if (istaggedtype(c, gtn51)) { srctagged = true; }; }; + }; + }; }; if (srctagged) { let nested: i32 = flatvariantidxt(dt, src.type_: *tinfo, false); if (nested >= 0) { @@ -21337,18 +21345,42 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s }; if (src.kind == nkind.N_IDENT) { let lc: *local = localfindnode(c, src.str); - let soff: i32 = lc.off; - let ck: i32 = 0; - for (ck < ssz) { - emitline("\tMOVQ\t"); - emitoff((soff + ck): i64); - emitline("(BP), AX\n"); - emitline("\tMOVQ\tAX, "); - emitoff((slot_off + 8 + ck): i64); - emitline("(BP)\n"); - ck += 8; + if (lc != nil) { + let soff: i32 = lc.off; + let ck: i32 = 0; + for (ck < ssz) { + emitline("\tMOVQ\t"); + emitoff((soff + ck): i64); + emitline("(BP), AX\n"); + emitline("\tMOVQ\tAX, "); + emitoff((slot_off + 8 + ck): i64); + emitline("(BP)\n"); + ck += 8; + }; + } else { + // #51 (#263 ww-runtime-correct): a module-global tagged + // ident source — no BP slot. Land g(SB) in SI + // (aggargsrcaddr) and copy the inner box to slot+8. + // Pre-fix rhstaggedident returned nil for a global, so + // srctagged was false and the value fell to the scalar + // word0 arm — gi's TAG landed as the payload. cstage + // copies frame garbage (cstage half #44). + if (aggargsrcaddr(c, src, "SI")) { + let ck2: i32 = 0; + for (ck2 < ssz) { + emitline("\tMOVQ\t"); + emitoff(ck2: i64); + emitline("(SI), AX\n"); + emitline("\tMOVQ\tAX, "); + emitoff((slot_off + 8 + ck2): i64); + emitline("(BP)\n"); + ck2 += 8; + }; + }; }; } else { + // non-nested SUBSET-widen of a GLOBAL tagged + // source still mis-copies (both stages) — task #49. // #38b: an sret-classified call result is in // memory (AX = dest pointer), not the cursor — // the spill below would store the pointer as diff --git a/selfhost/cmd/wcc/cgenutil.ww b/selfhost/cmd/wcc/cgenutil.ww index bb772b46..60be7e6e 100644 --- a/selfhost/cmd/wcc/cgenutil.ww +++ b/selfhost/cmd/wcc/cgenutil.ww @@ -3883,6 +3883,14 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s // cg_widen_tagged_store's nested arm (cmd/w6c/cgen.c). let srctagged: bool = (rhstaggedident(c, src) != nil) || rhstaggedabicall(c, src); + // #51 (#263): a module-global tagged ident is also a tagged source + // (no local slot — handled by the global branch in the nested copy). + if (!srctagged) { if (src.kind == nkind.N_IDENT) { + if (localfindnode(c, src.str) == nil) { + let gtn51: *node = letvartnode(c, src.str); + if (gtn51 != nil) { if (istaggedtype(c, gtn51)) { srctagged = true; }; }; + }; + }; }; if (srctagged) { let nested: i32 = flatvariantidxt(dt, src.type_: *tinfo, false); if (nested >= 0) { @@ -3917,18 +3925,42 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s }; if (src.kind == nkind.N_IDENT) { let lc: *local = localfindnode(c, src.str); - let soff: i32 = lc.off; - let ck: i32 = 0; - for (ck < ssz) { - emitline("\tMOVQ\t"); - emitoff((soff + ck): i64); - emitline("(BP), AX\n"); - emitline("\tMOVQ\tAX, "); - emitoff((slot_off + 8 + ck): i64); - emitline("(BP)\n"); - ck += 8; + if (lc != nil) { + let soff: i32 = lc.off; + let ck: i32 = 0; + for (ck < ssz) { + emitline("\tMOVQ\t"); + emitoff((soff + ck): i64); + emitline("(BP), AX\n"); + emitline("\tMOVQ\tAX, "); + emitoff((slot_off + 8 + ck): i64); + emitline("(BP)\n"); + ck += 8; + }; + } else { + // #51 (#263 ww-runtime-correct): a module-global tagged + // ident source — no BP slot. Land g(SB) in SI + // (aggargsrcaddr) and copy the inner box to slot+8. + // Pre-fix rhstaggedident returned nil for a global, so + // srctagged was false and the value fell to the scalar + // word0 arm — gi's TAG landed as the payload. cstage + // copies frame garbage (cstage half #44). + if (aggargsrcaddr(c, src, "SI")) { + let ck2: i32 = 0; + for (ck2 < ssz) { + emitline("\tMOVQ\t"); + emitoff(ck2: i64); + emitline("(SI), AX\n"); + emitline("\tMOVQ\tAX, "); + emitoff((slot_off + 8 + ck2): i64); + emitline("(BP)\n"); + ck2 += 8; + }; + }; }; } else { + // non-nested SUBSET-widen of a GLOBAL tagged + // source still mis-copies (both stages) — task #49. // #38b: an sret-classified call result is in // memory (AX = dest pointer), not the cursor — // the spill below would store the pointer as diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 583962f4..2a0a16d4 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -21303,6 +21303,14 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s // cg_widen_tagged_store's nested arm (cmd/w6c/cgen.c). let srctagged: bool = (rhstaggedident(c, src) != nil) || rhstaggedabicall(c, src); + // #51 (#263): a module-global tagged ident is also a tagged source + // (no local slot — handled by the global branch in the nested copy). + if (!srctagged) { if (src.kind == nkind.N_IDENT) { + if (localfindnode(c, src.str) == nil) { + let gtn51: *node = letvartnode(c, src.str); + if (gtn51 != nil) { if (istaggedtype(c, gtn51)) { srctagged = true; }; }; + }; + }; }; if (srctagged) { let nested: i32 = flatvariantidxt(dt, src.type_: *tinfo, false); if (nested >= 0) { @@ -21337,18 +21345,42 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s }; if (src.kind == nkind.N_IDENT) { let lc: *local = localfindnode(c, src.str); - let soff: i32 = lc.off; - let ck: i32 = 0; - for (ck < ssz) { - emitline("\tMOVQ\t"); - emitoff((soff + ck): i64); - emitline("(BP), AX\n"); - emitline("\tMOVQ\tAX, "); - emitoff((slot_off + 8 + ck): i64); - emitline("(BP)\n"); - ck += 8; + if (lc != nil) { + let soff: i32 = lc.off; + let ck: i32 = 0; + for (ck < ssz) { + emitline("\tMOVQ\t"); + emitoff((soff + ck): i64); + emitline("(BP), AX\n"); + emitline("\tMOVQ\tAX, "); + emitoff((slot_off + 8 + ck): i64); + emitline("(BP)\n"); + ck += 8; + }; + } else { + // #51 (#263 ww-runtime-correct): a module-global tagged + // ident source — no BP slot. Land g(SB) in SI + // (aggargsrcaddr) and copy the inner box to slot+8. + // Pre-fix rhstaggedident returned nil for a global, so + // srctagged was false and the value fell to the scalar + // word0 arm — gi's TAG landed as the payload. cstage + // copies frame garbage (cstage half #44). + if (aggargsrcaddr(c, src, "SI")) { + let ck2: i32 = 0; + for (ck2 < ssz) { + emitline("\tMOVQ\t"); + emitoff(ck2: i64); + emitline("(SI), AX\n"); + emitline("\tMOVQ\tAX, "); + emitoff((slot_off + 8 + ck2): i64); + emitline("(BP)\n"); + ck2 += 8; + }; + }; }; } else { + // non-nested SUBSET-widen of a GLOBAL tagged + // source still mis-copies (both stages) — task #49. // #38b: an sret-classified call result is in // memory (AX = dest pointer), not the cursor — // the spill below would store the pointer as diff --git a/test/wcc/989_globtagwiden_run.c b/test/wcc/989_globtagwiden_run.c new file mode 100644 index 00000000..6e279510 --- /dev/null +++ b/test/wcc/989_globtagwiden_run.c @@ -0,0 +1,190 @@ +/* + * 989_globtagwiden_run — F8-c8 (report-item #51, #263): widening a module- + * GLOBAL tagged-union ident into a wider union (`let x: (inner | str) = gi;`) + * must copy gi's whole box as the nested payload, not store its tag as the + * payload. + * + * THE BUG (cat-A silent miscompile, #263 BOTH-WRONG): cgwidentaggedstorebp's + * #218 nested-tagged-source arm is gated on rhstaggedident, which returns nil + * for a module-global tagged ident (local-only). So srctagged was false and a + * global tagged source fell to the scalar word0 widen arm — gi's TAG word + * landed as the box payload, the real value lost. cstage is ALSO wrong: it + * copies frame garbage (saved-BP / return-address), never gi(SB). Both wrong, + * divergent (#263); the cstage half is filed as task #44. + * + * THE WWSTAGE FIX (ww-runtime-correct): srctagged also recognises a global + * tagged ident, and the nested arm's N_IDENT copy grows a global branch — + * LEAQ gi(SB),SI via aggargsrcaddr, then copy the inner box (ssz bytes) into + * slot+8 with the outer tag at slot+0. Local sources keep the BP copy + * (byte-id). cstage stays wrong → cs≠ww residual. + * + * cstage's residual is FRAME GARBAGE (non-deterministic across stack + * layouts), so — unlike the deterministic #263 members — the cstage rows + * assert only that cstage DIVERGES (got != the runtime-correct value and the + * build succeeded), not a pinned value. The wwstage rows assert the exact + * runtime-correct value. (#44 closes the cs side → cstage will then match.) + * row | shape | ww | cs + * ----------+-----------------------------------------+----+-------- + * nest_47 | gi:inner=47; (inner|str)=gi; match→v | 47 | != 47 (garbage) + * nest_99 | gi:inner=99; (inner|str)=gi; match→v | 99 | != 99 (garbage) + */ +#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_ww; +}; + +static const struct row rows[] = { + { "nest_47", + "package main;\n" + "type inner = (i64 | bool);\n" + "let gi: inner = 47;\n" + "export fn main() int = {\n" + " let x: (inner | str) = gi;\n" + " match (x) {\n" + " case let n: inner => {\n" + " match (n) {\n" + " case let v: i64 => return v: int;\n" + " case bool => return 88;\n" + " };\n" + " };\n" + " case str => return 3;\n" + " };\n" + "};\n", + 47 }, + + { "nest_99", + "package main;\n" + "type inner = (i64 | bool);\n" + "let gi: inner = 99;\n" + "export fn main() int = {\n" + " let x: (inner | str) = gi;\n" + " match (x) {\n" + " case let n: inner => {\n" + " match (n) {\n" + " case let v: i64 => return v: int;\n" + " case bool => return 88;\n" + " };\n" + " };\n" + " case str => return 3;\n" + " };\n" + "};\n", + 99 }, +}; + +/* run_build — build+run `src` via `driver`; returns the binary's exit + * code, or -1 on a build failure. */ +static int +run_build(const char *driver, const struct row *r, int i) +{ + char src[64], tmpdir[64], cmd[1024]; + snprintf(src, sizeof src, "/tmp/gtw_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/gtw_%d_d_%d", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -2; + fputs(r->src, f); + fclose(f); + + mkdir(tmpdir, 0755); + snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null", + tmpdir, driver, src); + int brc = runwait(cmd); + + const char *base = strrchr(src, '/'); + base = base ? base + 1 : src; + char outbin[128]; + snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); + char *dot = strrchr(outbin, '.'); + if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; + + int got = -1; + if (brc == 0) got = runwait(outbin); + + unlink(src); unlink(outbin); rmdir(tmpdir); + return brc == 0 ? got : -1; +} + +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 cdrv[1024], wdrv[1024]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); + + struct { const char *name; const char *drv; int gated; } + drivers[] = { + { "cstage", cdrv, 0 }, + { "wwstage", wdrv, 1 }, + { NULL, NULL, 0 }, + }; + + int n = (int)(sizeof rows / sizeof rows[0]); + int total = 0, fail = 0; + + for (int d = 0; drivers[d].name; d++) { + if (drivers[d].gated && access(drivers[d].drv, X_OK) != 0) { + fprintf(stderr, "globtagwiden_run: skip %s (no %s)\n", + drivers[d].name, drivers[d].drv); + continue; + } + int is_ww = (d == 1); + for (int i = 0; i < n; i++) { + total++; + int got = run_build(drivers[d].drv, &rows[i], i); + if (is_ww) { + /* align-to-runtime-correct: exact value. */ + if (got != rows[i].want_ww) { + fprintf(stderr, "globtagwiden_run[wwstage][%s]:" + " exit=%d want=%d\n", rows[i].label, + got, rows[i].want_ww); + fail++; + } + } else { + /* #263 residual: cstage built but diverges from the + * runtime-correct value (frame garbage, #44). */ + if (got == -1 || got == rows[i].want_ww) { + fprintf(stderr, "globtagwiden_run[cstage][%s]:" + " exit=%d expected build-ok and !=%d " + "(#263 residual, task #44)\n", + rows[i].label, got, rows[i].want_ww); + fail++; + } + } + } + } + + if (fail) { + fprintf(stderr, "globtagwiden_run: %d/%d fixtures failed\n", + fail, total); + return 1; + } + printf("globtagwiden_run: %d/%d ok\n", total, total); + return 0; +}