diff --git a/Makefile b/Makefile index ac088a59..797b5f36 100644 --- a/Makefile +++ b/Makefile @@ -244,6 +244,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_arr_tagged_elem \ $(BIN)/test_arr_infer_len \ $(BIN)/test_arr_cap_reject \ + $(BIN)/test_arr_ptr_global \ $(BIN)/test_def_arr_infer_len \ $(BIN)/test_def_arr_len \ $(BIN)/test_slice_str_global_zero \ @@ -642,6 +643,12 @@ $(BIN)/test_arr_cap_reject: test/wcc/817_arr_cap_reject.c $(BIN)/ww \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_arr_ptr_global: test/wcc/818_arr_ptr_global.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_def_arr_infer_len: test/wcc/814_def_arr_infer_len.c $(BIN)/ww \ $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ diff --git a/cmd/w6c/cgen.c b/cmd/w6c/cgen.c index 7561f60f..aff534df 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -11123,7 +11123,21 @@ cgexpr(Cg *c, Node *n, Local *locals) break; } if (ptrfld) { - ins2(c, A_LEAQ, amem(D_BP, off), areg(D_AX)); + /* The array's backing pointer = address of its + * element 0 (array.ptr ≡ &A[0], drew #13). A LOCAL + * array IS its frame slot — LEAQ off(BP). A module + * GLOBAL (let or def array) lives at name(SB): off==0 + * here would LEAQ the frame's first slot = garbage + * (*p→0), the GAP-A.ptr silent miscompile. Same + * off==0/global base-selection as the def-array index + * base (cgen.c:4367) and #231/#48. */ + if (off == 0 && (let_islet(n->lhs->str) + || def_isarraydef(n->lhs->str))) + ins2(c, A_LEAQ, masym(c, n->lhs->str), + areg(D_AX)); + else + ins2(c, A_LEAQ, amem(D_BP, off), + areg(D_AX)); break; } } diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 6f0febd7..f25dfd32 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -24981,11 +24981,24 @@ fn cgdot(c: *cgen, n: *node) void = { // SB fallback → MOVQ len(SB) (w6l: undefined 'len'). cstage cgen.c // emits MOVQ $alen here. defvartnode is the def-side mirror of // letvartnode (returns dtnode = the N_TARRAY whose .rhs length child - // is #11-stamped). `.ptr`/`.cap` are NOT mirrored: cstage's def .ptr - // is itself buggy (LEAQ (BP), task GAP-A.ptr) and arrays have no - // .cap (cstage rejects, task GAP-A.cap). + // is #11-stamped). `.ptr` mirrors the let-global arm above: the + // array's backing pointer ≡ &A[0] = LEAQ name(SB) (drew #13, + // .ai/drew-gapa-ptr-ruling.md; GAP-A.ptr now fixed cstage-side too, + // so both stages byte-id). `.cap` stays unmirrored — arrays have no + // .cap (both checkers reject, task GAP-A.cap). if (lhs != nil) { if (lhs.kind == nkind.N_IDENT) { + if (streq(fld, "ptr")) { + let dtn: *node = defvartnode(c, lhs.str); + if (dtn != nil) { + if (dtn.kind == nkind.N_TARRAY) { + emitline("\tLEAQ\t"); + emitsymname(c, lhs.str); + emitline("(SB), AX\n"); + return; + }; + }; + }; if (streq(fld, "len")) { let dtn: *node = defvartnode(c, lhs.str); if (dtn != nil) { diff --git a/selfhost/cmd/wcc/cgenexpr.ww b/selfhost/cmd/wcc/cgenexpr.ww index 8aef37ac..db4e76e3 100644 --- a/selfhost/cmd/wcc/cgenexpr.ww +++ b/selfhost/cmd/wcc/cgenexpr.ww @@ -3461,11 +3461,24 @@ fn cgdot(c: *cgen, n: *node) void = { // SB fallback → MOVQ len(SB) (w6l: undefined 'len'). cstage cgen.c // emits MOVQ $alen here. defvartnode is the def-side mirror of // letvartnode (returns dtnode = the N_TARRAY whose .rhs length child - // is #11-stamped). `.ptr`/`.cap` are NOT mirrored: cstage's def .ptr - // is itself buggy (LEAQ (BP), task GAP-A.ptr) and arrays have no - // .cap (cstage rejects, task GAP-A.cap). + // is #11-stamped). `.ptr` mirrors the let-global arm above: the + // array's backing pointer ≡ &A[0] = LEAQ name(SB) (drew #13, + // .ai/drew-gapa-ptr-ruling.md; GAP-A.ptr now fixed cstage-side too, + // so both stages byte-id). `.cap` stays unmirrored — arrays have no + // .cap (both checkers reject, task GAP-A.cap). if (lhs != nil) { if (lhs.kind == nkind.N_IDENT) { + if (streq(fld, "ptr")) { + let dtn: *node = defvartnode(c, lhs.str); + if (dtn != nil) { + if (dtn.kind == nkind.N_TARRAY) { + emitline("\tLEAQ\t"); + emitsymname(c, lhs.str); + emitline("(SB), AX\n"); + return; + }; + }; + }; if (streq(fld, "len")) { let dtn: *node = defvartnode(c, lhs.str); if (dtn != nil) { diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index b4561eb8..d4679c0e 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -24981,11 +24981,24 @@ fn cgdot(c: *cgen, n: *node) void = { // SB fallback → MOVQ len(SB) (w6l: undefined 'len'). cstage cgen.c // emits MOVQ $alen here. defvartnode is the def-side mirror of // letvartnode (returns dtnode = the N_TARRAY whose .rhs length child - // is #11-stamped). `.ptr`/`.cap` are NOT mirrored: cstage's def .ptr - // is itself buggy (LEAQ (BP), task GAP-A.ptr) and arrays have no - // .cap (cstage rejects, task GAP-A.cap). + // is #11-stamped). `.ptr` mirrors the let-global arm above: the + // array's backing pointer ≡ &A[0] = LEAQ name(SB) (drew #13, + // .ai/drew-gapa-ptr-ruling.md; GAP-A.ptr now fixed cstage-side too, + // so both stages byte-id). `.cap` stays unmirrored — arrays have no + // .cap (both checkers reject, task GAP-A.cap). if (lhs != nil) { if (lhs.kind == nkind.N_IDENT) { + if (streq(fld, "ptr")) { + let dtn: *node = defvartnode(c, lhs.str); + if (dtn != nil) { + if (dtn.kind == nkind.N_TARRAY) { + emitline("\tLEAQ\t"); + emitsymname(c, lhs.str); + emitline("(SB), AX\n"); + return; + }; + }; + }; if (streq(fld, "len")) { let dtn: *node = defvartnode(c, lhs.str); if (dtn != nil) { diff --git a/test/wcc/818_arr_ptr_global.c b/test/wcc/818_arr_ptr_global.c new file mode 100644 index 00000000..7df44914 --- /dev/null +++ b/test/wcc/818_arr_ptr_global.c @@ -0,0 +1,230 @@ +/* + * 818_arr_ptr_global — `.ptr` on a fixed-size array is the sanctioned ww + * spelling for &A[0] (task #13 divergence-record, drew ruling + * .ai/drew-gapa-ptr-ruling.md). For a LOCAL array the backing pointer is the + * frame slot (LEAQ off(BP)); for a module GLOBAL array (let or def) it is the + * symbol address (LEAQ name(SB)). cstage cgen emitted LEAQ off(BP) for BOTH + * flavours — and for a global, off==0, so `*A.ptr` read the frame's first + * slot = stack garbage (the GAP-A.ptr silent miscompile, task #11; the + * off==0/global base-selection class of #231/#48). + * + * The fix (cmd/w6c/cgen.c N_DOT array .ptr arm + selfhost cgenexpr.ww def + * arm): when off==0 and the operand is a module global (let_islet || + * def_isarraydef), emit LEAQ name(SB) instead of LEAQ (BP), mirroring the + * def-array index base at cgen.c:4367. wwstage's LET-global `.ptr` was + * already correct (LEAQ name(SB)); only the DEF-global arm was missing. + * After this commit local / let-global / def-global `.ptr` are byte- + * IDENTICAL across both stages. + * + * row | shape | want + * -----------------+-----------------------------------------+------ + * def_glob_ptr | def A:[3]int, *A.ptr (THE regression) | 10 + * let_glob_ptr | let G:[3]int, *G.ptr (BP→SB converge) | 5 + * local_ptr | local [3]int, (a.ptr)[2] (unchanged) | 3 + * + * def_glob_ptr / let_glob_ptr are the mutation-sane rows: pre-fix they read + * stack garbage. local_ptr guards the 14 live local-`.ptr` consumers (cgen + * unchanged for locals). A byte-id row pins cstage==wwstage `.s` for the + * def-global `.ptr` source (the convergence the fix delivers). + */ +#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; }; + +static const struct row rows[] = { + /* def_glob_ptr — THE regression pin: pre-fix `*A.ptr` reads stack + * garbage because cstage LEAQ (BP)'d the def-global base. */ + { "def_glob_ptr", + "package main;\n" + "def A: [3]int = [10, 20, 30];\n" + "export fn main() i32 = {\n" + "\tlet p: *int = A.ptr;\n" + "\treturn (*p): i32;\n" + "};\n", + 10 }, + + /* let_glob_ptr — pins the cstage BP→SB convergence (wwstage was + * already right for let-global, so this closes the latent cs≠ww). */ + { "let_glob_ptr", + "package main;\n" + "let G: [3]int = [5, 6, 7];\n" + "export fn main() i32 = {\n" + "\tlet p: *int = G.ptr;\n" + "\treturn (*p): i32;\n" + "};\n", + 5 }, + + /* local_ptr — the 14-site regression guard; cgen unchanged for a + * LOCAL array (off!=0 → LEAQ off(BP)). p[2] strides element 2. */ + { "local_ptr", + "package main;\n" + "export fn main() i32 = {\n" + "\tlet a: [3]int = [1, 2, 3];\n" + "\tlet p: *int = a.ptr;\n" + "\treturn p[2]: i32;\n" + "};\n", + 3 }, +}; + +static int +run_driver(const char *driver, const struct row *r, int i) +{ + char src[64], tmpdir[64], cmd[1024]; + snprintf(src, sizeof src, "/tmp/apg_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/apg_%d_d_%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 && %s build %s 2>/dev/null", + tmpdir, driver, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: build via %s failed\n", + r->label, driver); + unlink(src); rmdir(tmpdir); + return -1; + } + + 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 = runwait(outbin); + + unlink(src); unlink(outbin); rmdir(tmpdir); + return got; +} + +/* asm_byte_identical — w6c vs w6c_ww .s for the same source must match. */ +static int +asm_byte_identical(const char *bin, const struct row *r, int i) +{ + char src[64], cs[64], ws[64], cmd[1024]; + snprintf(src, sizeof src, "/tmp/apg_asm_%d_%d.ww", getpid(), i); + snprintf(cs, sizeof cs, "/tmp/apg_asm_%d_%d_c.s", getpid(), i); + snprintf(ws, sizeof ws, "/tmp/apg_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; + } + + FILE *fc = fopen(cs, "rb"); + FILE *fw = fopen(ws, "rb"); + int rc = 0; + if (!fc || !fw) { + rc = -1; + } else { + for (;;) { + int a = fgetc(fc); + int b = fgetc(fw); + if (a != b) { rc = -1; break; } + if (a == EOF) break; + } + } + if (fc) fclose(fc); + if (fw) fclose(fw); + 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[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]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + char wdrv[1024]; + snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); + + struct { const char *name; const char *path; int gated_on_existence; } + 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_on_existence + && access(drivers[d].path, X_OK) != 0) { + fprintf(stderr, "arr_ptr_global: skip %s (no %s)\n", + drivers[d].name, drivers[d].path); + continue; + } + for (int i = 0; i < n; i++) { + int got = run_driver(drivers[d].path, &rows[i], i); + total++; + if (got != rows[i].want) { + fprintf(stderr, + "arr_ptr_global[%s][%s]: exit=%d want=%d\n", + drivers[d].name, rows[i].label, + got, rows[i].want); + fail++; + } + } + } + + if (access(wdrv, X_OK) == 0) { + for (int i = 0; i < n; i++) { + total++; + if (asm_byte_identical(bin, &rows[i], i) != 0) + fail++; + } + } + + if (fail) { + fprintf(stderr, + "arr_ptr_global: %d/%d fixtures failed\n", fail, total); + return 1; + } + printf("arr_ptr_global: %d/%d ok\n", total, total); + return 0; +}