diff --git a/Makefile b/Makefile index 7494ec00..5c87ab9f 100644 --- a/Makefile +++ b/Makefile @@ -305,6 +305,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_alias_decl_order_size_run \ $(BIN)/test_alias_accept_run \ $(BIN)/test_alias_idx_family_run \ + $(BIN)/test_alias_amp_idx_run \ $(BIN)/test_alias_global_decl_run \ $(BIN)/test_tuple_nary_destructure_run \ $(BIN)/test_rvalue_tuple_destructure_run \ @@ -1468,6 +1469,12 @@ $(BIN)/test_alias_idx_family_run: test/wcc/944_alias_idx_family_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_alias_amp_idx_run: test/wcc/944_alias_amp_idx_run.c \ + $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_alias_global_decl_run: test/wcc/944_alias_global_decl_run.c \ $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index ace64eda..6bbfdb02 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -25639,6 +25639,32 @@ fn cgun(c: *cgen, n: *node) void = { }; }; }; + // #82: the tnode-kind classify above misses an + // alias-typed base (N_TNAME) — base materialized + // as MOVQ (element-0 VALUE) instead of LEAQ, a + // wild pointer. Re-key arrayness off the chased + // checker-stamped base type, the cgindex #60 + // idiom (cgenexpr.ww:1800-1820); cstage already + // classifies off type_chase_named uniformly + // (cmd/w6c/cgen.c:4172-4188). TY_NAMED gate + // keeps non-alias rows byte-id by construction. + let bt82: *tinfo = base.type_: *tinfo; + let basealias82: bool = false; + if (bt82 != nil) { + if (bt82.kind == tykind.TY_NAMED) { basealias82 = true; }; + }; + if (basealias82) { + let bu82: *tinfo = tichase(bt82); + if (bu82 != nil) { + if (baselocal != nil) { + isarr = bu82.kind == tykind.TY_ARRAY; + }; + if (isglobalarr || isglobalptr) { + isglobalarr = bu82.kind == tykind.TY_ARRAY; + isglobalptr = !isglobalarr; + }; + }; + }; } else { if (base.kind == nkind.N_DOT || (base.kind == nkind.N_UN && base.op == tkind.TK_STAR)) { diff --git a/selfhost/cmd/wcc/cgenexpr.ww b/selfhost/cmd/wcc/cgenexpr.ww index 99a19026..ce6c13a9 100644 --- a/selfhost/cmd/wcc/cgenexpr.ww +++ b/selfhost/cmd/wcc/cgenexpr.ww @@ -4664,6 +4664,32 @@ fn cgun(c: *cgen, n: *node) void = { }; }; }; + // #82: the tnode-kind classify above misses an + // alias-typed base (N_TNAME) — base materialized + // as MOVQ (element-0 VALUE) instead of LEAQ, a + // wild pointer. Re-key arrayness off the chased + // checker-stamped base type, the cgindex #60 + // idiom (cgenexpr.ww:1800-1820); cstage already + // classifies off type_chase_named uniformly + // (cmd/w6c/cgen.c:4172-4188). TY_NAMED gate + // keeps non-alias rows byte-id by construction. + let bt82: *tinfo = base.type_: *tinfo; + let basealias82: bool = false; + if (bt82 != nil) { + if (bt82.kind == tykind.TY_NAMED) { basealias82 = true; }; + }; + if (basealias82) { + let bu82: *tinfo = tichase(bt82); + if (bu82 != nil) { + if (baselocal != nil) { + isarr = bu82.kind == tykind.TY_ARRAY; + }; + if (isglobalarr || isglobalptr) { + isglobalarr = bu82.kind == tykind.TY_ARRAY; + isglobalptr = !isglobalarr; + }; + }; + }; } else { if (base.kind == nkind.N_DOT || (base.kind == nkind.N_UN && base.op == tkind.TK_STAR)) { diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index c6d78359..570da5b3 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -25639,6 +25639,32 @@ fn cgun(c: *cgen, n: *node) void = { }; }; }; + // #82: the tnode-kind classify above misses an + // alias-typed base (N_TNAME) — base materialized + // as MOVQ (element-0 VALUE) instead of LEAQ, a + // wild pointer. Re-key arrayness off the chased + // checker-stamped base type, the cgindex #60 + // idiom (cgenexpr.ww:1800-1820); cstage already + // classifies off type_chase_named uniformly + // (cmd/w6c/cgen.c:4172-4188). TY_NAMED gate + // keeps non-alias rows byte-id by construction. + let bt82: *tinfo = base.type_: *tinfo; + let basealias82: bool = false; + if (bt82 != nil) { + if (bt82.kind == tykind.TY_NAMED) { basealias82 = true; }; + }; + if (basealias82) { + let bu82: *tinfo = tichase(bt82); + if (bu82 != nil) { + if (baselocal != nil) { + isarr = bu82.kind == tykind.TY_ARRAY; + }; + if (isglobalarr || isglobalptr) { + isglobalarr = bu82.kind == tykind.TY_ARRAY; + isglobalptr = !isglobalarr; + }; + }; + }; } else { if (base.kind == nkind.N_DOT || (base.kind == nkind.N_UN && base.op == tkind.TK_STAR)) { diff --git a/test/wcc/944_alias_amp_idx_run.c b/test/wcc/944_alias_amp_idx_run.c new file mode 100644 index 00000000..8c3c15ab --- /dev/null +++ b/test/wcc/944_alias_amp_idx_run.c @@ -0,0 +1,295 @@ +/* + * 944_alias_amp_idx_run — #5 alias arc F2a batch 3 c1 (task #82): + * cgun's TK_AMP N_INDEX classify keyed arrayness off the SYNTACTIC + * tnode — `&a[i]` over an alias-typed base (tnode N_TNAME) missed the + * N_TARRAY gate, so the base materialized as MOVQ (element-0 VALUE) + * instead of LEAQ (storage address): wild pointer, ww SEGV 139 on the + * deref (silent-wrong class; esz was already alias-correct via the + * batch-2 elemsizeofc chase, so ONLY the base classify moved). The + * global leg (isglobalarr/isglobalptr) graduated from latent to live + * when g-fold #77/#78 landed alias-global DATA emit. cstage already + * classifies off the chased type (type_chase_named, cgen.c:4172-4188) + * and is the runtime-correct reference; the fix re-keys both legs off + * tichase(base.type_) gated on TY_NAMED — the landed cgindex #60 idiom + * (cgenexpr.ww:1800-1820) — so non-alias rows are byte-id-neutral by + * construction. + * + * row | shape | want + * -----------+-----------------------------------------------+----- + * amp_ctrl | plain [4]int local; p=&a[2]; *p=..; readback | 0 + * amp_l1 | 1-level alias local array, &a[3] w+r via ptr | 0 + * amp_l2 | 2-level alias local | 0 + * amp_order | type decl AFTER the let (fwd-ref) | 0 + * amp_narrow | alias [4]u32, &a[3] (last elem, narrow elem: | + * | stride was already right, base was wrong) | 0 + * amp_g1 | alias-typed GLOBAL array, &g[3] | 0 + * amp_g2 | 2-level alias global | 0 + * amp_gctrl | plain global array &g[3] + global str &s[3] | + * | (the #11 legs hold) | 0 + * + * All reads/writes go THROUGH THE TAKEN POINTER (direct `a[i]` reads + * are 944_alias_idx_family's landed rows — different layer). Values + * exceed 255 to break little-endian prefix-luck; the LAST element is + * checked. Pre-fix: controls 0/0 byte-id; all six alias rows cs 0 / + * ww SEGV 139, byte-id NO. Post-fix: every row 0/0 byte-id. + * + * Deliberately NOT pinned (filed, predicted by the batch-3 spec §1 + * NOTE-2): `&D[i]` with a DEF-array base — broken at a DIFFERENT site + * both stages (cs XORQ BX,BX zero-base cgen.c:4209-4212; ww + * complex-base fallback), a missing-leg defect, not this tnode-kind + * read. + */ +#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; +} + +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), cb = fgetc(fb); + if (ca != cb) { rc = -1; break; } + if (ca == EOF) break; + } + fclose(fa); fclose(fb); + return rc; +} + +struct row { const char *label; const char *src; int want; }; + +static const struct row rows[] = { + { "amp_ctrl", + "package main;\n" + "export fn main() i32 = {\n" + " let a: [4]int = [1000: int, 2000: int, 3000: int, 4000: int];\n" + " let p: *int = &a[2];\n" + " if (*p != 3000) { return 1; };\n" + " *p = 7777;\n" + " if (a[2] != 7777) { return 2; };\n" + " let q: *int = &a[3];\n" + " if (*q != 4000) { return 3; };\n" + " return 0;\n" + "};\n", 0 }, + { "amp_l1", + "package main;\n" + "type arr = [4]int;\n" + "export fn main() i32 = {\n" + " let a: arr = [1000: int, 2000: int, 3000: int, 4000: int];\n" + " let p: *int = &a[3];\n" + " if (*p != 4000) { return 1; };\n" + " *p = 8888;\n" + " if (a[3] != 8888) { return 2; };\n" + " return 0;\n" + "};\n", 0 }, + { "amp_l2", + "package main;\n" + "type arr = [4]int;\n" + "type arr2 = arr;\n" + "export fn main() i32 = {\n" + " let a: arr2 = [1000: int, 2000: int, 3000: int, 4000: int];\n" + " let p: *int = &a[3];\n" + " if (*p != 4000) { return 1; };\n" + " *p = 9999;\n" + " if (a[3] != 9999) { return 2; };\n" + " return 0;\n" + "};\n", 0 }, + { "amp_order", + "package main;\n" + "export fn main() i32 = {\n" + " let a: arr = [1000: int, 2000: int, 3000: int, 4000: int];\n" + " let p: *int = &a[3];\n" + " if (*p != 4000) { return 1; };\n" + " *p = 8888;\n" + " if (a[3] != 8888) { return 2; };\n" + " return 0;\n" + "};\n" + "type arr = [4]int;\n", 0 }, + /* Narrow element: the batch-2 elemsizeofc chase already strides + * u32 right (PREMISE-2) — this row pins that the BASE was the + * only wrong half. */ + { "amp_narrow", + "package main;\n" + "type A = [4]u32;\n" + "export fn main() i32 = {\n" + " let a: A = [1000: u32, 2000: u32, 3000: u32, 4000: u32];\n" + " let p: *u32 = &a[3];\n" + " if (*p != 4000) { return 1; };\n" + " *p = 5555;\n" + " if (a[3] != 5555) { return 2; };\n" + " return 0;\n" + "};\n", 0 }, + { "amp_g1", + "package main;\n" + "type arr = [4]int;\n" + "let g: arr = [1000: int, 2000: int, 3000: int, 4000: int];\n" + "export fn main() i32 = {\n" + " let p: *int = &g[3];\n" + " if (*p != 4000) { return 1; };\n" + " *p = 8888;\n" + " if (g[3] != 8888) { return 2; };\n" + " return 0;\n" + "};\n", 0 }, + { "amp_g2", + "package main;\n" + "type arr = [4]int;\n" + "type arr2 = arr;\n" + "let g: arr2 = [1000: int, 2000: int, 3000: int, 4000: int];\n" + "export fn main() i32 = {\n" + " let p: *int = &g[3];\n" + " if (*p != 4000) { return 1; };\n" + " *p = 9999;\n" + " if (g[3] != 9999) { return 2; };\n" + " return 0;\n" + "};\n", 0 }, + /* Plain-global + global-str controls: the #11 legs (isglobalarr + * LEAQ / str isglobalptr MOVQ) must hold byte-id. */ + { "amp_gctrl", + "package main;\n" + "let g: [4]int = [1000: int, 2000: int, 3000: int, 4000: int];\n" + "let s: str = \"wxyz\";\n" + "export fn main() i32 = {\n" + " let p: *int = &g[3];\n" + " if (*p != 4000) { return 1; };\n" + " *p = 7777;\n" + " if (g[3] != 7777) { return 2; };\n" + " let q: *u8 = &s[3];\n" + " if (*q != 'z') { return 3; };\n" + " return 0;\n" + "};\n", 0 }, +}; + +static int +run_driver(const char *driver, const struct row *r, int i) +{ + char src[96], tmpdir[96], errf[96], cmd[1024]; + snprintf(src, sizeof src, "/tmp/aai_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/aai_%d_d_%d", getpid(), i); + snprintf(errf, sizeof errf, "/tmp/aai_%d_e_%d", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -1; + fputs(r->src, f); + fclose(f); + + mkdir(tmpdir, 0755); + snprintf(cmd, sizeof cmd, + "cd %s && timeout 20 %s build %s >/dev/null 2>%s", + tmpdir, driver, src, errf); + int brc = runwait(cmd); + if (brc != 0) { + fprintf(stderr, "row[%s]: build via %s failed\n", + r->label, driver); + unlink(src); unlink(errf); rmdir(tmpdir); + return -1; + } + + const char *base = strrchr(src, '/'); + base = base ? base + 1 : src; + char outbin[256]; + snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); + char *dot = strrchr(outbin, '.'); + if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; + int got = runwait(outbin); + + unlink(src); unlink(outbin); unlink(errf); rmdir(tmpdir); + if (got != r->want) { + fprintf(stderr, "row[%s]: %s exit %d, want %d\n", + r->label, driver, got, r->want); + return 1; + } + return 0; +} + +static int +asm_byte_identical(const char *bin, const struct row *r, int i) +{ + char src[96], cs[96], ws[96], cmd[1024]; + snprintf(src, sizeof src, "/tmp/aai_asm_%d_%d.ww", getpid(), i); + snprintf(cs, sizeof cs, "/tmp/aai_asm_%d_%d_c.s", getpid(), i); + snprintf(ws, sizeof ws, "/tmp/aai_asm_%d_%d_w.s", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -1; + fputs(r->src, f); + fclose(f); + + snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: w6c errored\n", r->label); + unlink(src); + return -1; + } + snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null", + bin, ws, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label); + unlink(src); unlink(cs); + return -1; + } + int rc = slurp_eq(cs, ws); + if (rc != 0) + fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n", + r->label); + unlink(src); unlink(cs); unlink(ws); + return rc; +} + +int +main(void) +{ + const char *bin = getenv("BIN"); + if (!bin) bin = "out/bin"; + char absbin[2080]; + 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[2120], wdrv[2120]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); + + int n = (int)(sizeof rows / sizeof rows[0]); + int total = 0, fail = 0; + + for (int i = 0; i < n; i++) { + total++; + if (run_driver(cdrv, &rows[i], i) != 0) fail++; + } + if (access(wdrv, X_OK) == 0) { + for (int i = 0; i < n; i++) { + total++; + if (run_driver(wdrv, &rows[i], i) != 0) fail++; + } + for (int i = 0; i < n; i++) { + total++; + if (asm_byte_identical(bin, &rows[i], i) != 0) fail++; + } + } + + if (fail) { + fprintf(stderr, "alias_amp_idx: %d/%d checks failed\n", + fail, total); + return 1; + } + printf("alias_amp_idx: %d/%d ok\n", total, total); + return 0; +}