diff --git a/Makefile b/Makefile index 5bdb971e..bb8f5e23 100644 --- a/Makefile +++ b/Makefile @@ -268,6 +268,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_module_decl \ $(BIN)/test_chained_index \ $(BIN)/test_chained_write \ + $(BIN)/test_dotbase_chained \ $(BIN)/test_fnparams_bare_leaf_shadow \ $(BIN)/test_fnret_bare_leaf_shadow \ $(BIN)/test_param_shadow_mod \ @@ -649,6 +650,10 @@ $(BIN)/test_chained_write: test/wcc/740_chained_write.c \ $(BIN)/w6c $(BIN)/w6c_ww | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_dotbase_chained: test/wcc/741_dotbase_chained.c \ + $(BIN)/w6c $(BIN)/w6c_ww | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_fnparams_bare_leaf_shadow: test/wcc/732_fnparams_bare_leaf_shadow.c \ $(BIN)/w6c $(BIN)/w6c_ww | $(BIN) $(CC) $(CFLAGS) -o $@ $< diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index d9845189..3b9229f5 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -8208,6 +8208,9 @@ fn elemsizeofc(c: *cgen, t: *node) i32 = { // the outer base type to *u8 (the post-inner-index value type), so // cgindex can compute the outer element size honestly. Mirrors // cstage's `n->lhs->type` via typed-AST (cmd/w6c/cgen.c idx_eff). +// N_DOT base graduated (tasks #28/#30) so `obj.mat[i][k]` reads +// and `obj.arr[i] = v` tagged-element writes route through the +// same helper as the N_IDENT/N_INDEX bases #24/#27 graduated. fn indexvaluetnode(c: *cgen, n: *node) *node = { if (n == nil) { return nil; }; if (n.kind != nkind.N_INDEX) { return nil; }; @@ -8220,6 +8223,7 @@ fn indexvaluetnode(c: *cgen, n: *node) *node = { else { bt = letvartnode(c, base.str); }; }; if (base.kind == nkind.N_INDEX) { bt = indexvaluetnode(c, base); }; + if (base.kind == nkind.N_DOT) { bt = dotfieldtnode(c, base); }; if (bt == nil) { return nil; }; let k: nkind = bt.kind; if (k == nkind.N_TPTR) { return bt.lhs; }; @@ -14242,6 +14246,16 @@ fn cgassign(c: *cgen, n: *node) void = { }; } else { if (base.kind == nkind.N_DOT) { esz = indexbaseesz(c, base); + // Without this, the tagged-element gate + // below (keyed on elemtn) misses for + // `obj.arr[i] = v` over an [N]Tagged field + // and the store falls through to scalar — + // task #30, sister of the cgindex N_INDEX- + // base fix #24. indexvaluetnode now handles + // N_DOT base, so the element type drops out + // of the same helper. + let bt: *node = indexvaluetnode(c, lhs); + if (bt != nil) { elemtn = bt; }; } else { if (base.kind == nkind.N_INDEX) { // Chained-write write-side parallel of the // cgindex N_INDEX-base arm graduated in #24: @@ -18010,6 +18024,12 @@ fn scanlocals(c: *cgen, n: *node) i32 = { // cgwidentaggedstore to materialise the source in before copying // to the element address. Slot is shared per function via // c.tagscrsz (raised to the largest element slot_sz seen). + // N_DOT-base graduated (task #30) so `obj.arr[i] = v` for an + // [N]Tagged struct field pre-reserves the slot — parallels the + // cgassign N_DOT-base elemtn fix in cgenexpr.ww. Without this + // arm the runtime localadd allocates past the frame boundary + // and clobbers live locals; cstage handles the shape naturally + // via first-use+fail-loud (rule-10 convergence filed as #15). if (n.kind == nkind.N_ASSIGN) { let alhs: *node = n.lhs; if (alhs != nil) { @@ -18034,6 +18054,21 @@ fn scanlocals(c: *cgen, n: *node) i32 = { }; }; }; + if (abase.kind == nkind.N_DOT) { + let ft: *node = dotfieldtnode(c, abase); + if (ft != nil) { + let bk: nkind = ft.kind; + let etn: *node = nil; + if (bk == nkind.N_TARRAY) { etn = ft.lhs; }; + if (bk == nkind.N_TSLICE) { etn = ft.lhs; }; + if (bk == nkind.N_TPTR) { etn = ft.lhs; }; + if (etn != nil) { + if (istaggedtype(c, etn)) { + total += tagscrbump(c, slotsize(c, etn)); + }; + }; + }; + }; }; }; }; diff --git a/selfhost/cmd/wcc/cgendecl.ww b/selfhost/cmd/wcc/cgendecl.ww index cc1512d8..afdc411a 100644 --- a/selfhost/cmd/wcc/cgendecl.ww +++ b/selfhost/cmd/wcc/cgendecl.ww @@ -241,6 +241,12 @@ fn scanlocals(c: *cgen, n: *node) i32 = { // cgwidentaggedstore to materialise the source in before copying // to the element address. Slot is shared per function via // c.tagscrsz (raised to the largest element slot_sz seen). + // N_DOT-base graduated (task #30) so `obj.arr[i] = v` for an + // [N]Tagged struct field pre-reserves the slot — parallels the + // cgassign N_DOT-base elemtn fix in cgenexpr.ww. Without this + // arm the runtime localadd allocates past the frame boundary + // and clobbers live locals; cstage handles the shape naturally + // via first-use+fail-loud (rule-10 convergence filed as #15). if (n.kind == nkind.N_ASSIGN) { let alhs: *node = n.lhs; if (alhs != nil) { @@ -265,6 +271,21 @@ fn scanlocals(c: *cgen, n: *node) i32 = { }; }; }; + if (abase.kind == nkind.N_DOT) { + let ft: *node = dotfieldtnode(c, abase); + if (ft != nil) { + let bk: nkind = ft.kind; + let etn: *node = nil; + if (bk == nkind.N_TARRAY) { etn = ft.lhs; }; + if (bk == nkind.N_TSLICE) { etn = ft.lhs; }; + if (bk == nkind.N_TPTR) { etn = ft.lhs; }; + if (etn != nil) { + if (istaggedtype(c, etn)) { + total += tagscrbump(c, slotsize(c, etn)); + }; + }; + }; + }; }; }; }; diff --git a/selfhost/cmd/wcc/cgenexpr.ww b/selfhost/cmd/wcc/cgenexpr.ww index 72a240ba..e164f781 100644 --- a/selfhost/cmd/wcc/cgenexpr.ww +++ b/selfhost/cmd/wcc/cgenexpr.ww @@ -3518,6 +3518,16 @@ fn cgassign(c: *cgen, n: *node) void = { }; } else { if (base.kind == nkind.N_DOT) { esz = indexbaseesz(c, base); + // Without this, the tagged-element gate + // below (keyed on elemtn) misses for + // `obj.arr[i] = v` over an [N]Tagged field + // and the store falls through to scalar — + // task #30, sister of the cgindex N_INDEX- + // base fix #24. indexvaluetnode now handles + // N_DOT base, so the element type drops out + // of the same helper. + let bt: *node = indexvaluetnode(c, lhs); + if (bt != nil) { elemtn = bt; }; } else { if (base.kind == nkind.N_INDEX) { // Chained-write write-side parallel of the // cgindex N_INDEX-base arm graduated in #24: diff --git a/selfhost/cmd/wcc/cgenutil.ww b/selfhost/cmd/wcc/cgenutil.ww index fdaadb11..930ae210 100644 --- a/selfhost/cmd/wcc/cgenutil.ww +++ b/selfhost/cmd/wcc/cgenutil.ww @@ -1247,6 +1247,9 @@ fn elemsizeofc(c: *cgen, t: *node) i32 = { // the outer base type to *u8 (the post-inner-index value type), so // cgindex can compute the outer element size honestly. Mirrors // cstage's `n->lhs->type` via typed-AST (cmd/w6c/cgen.c idx_eff). +// N_DOT base graduated (tasks #28/#30) so `obj.mat[i][k]` reads +// and `obj.arr[i] = v` tagged-element writes route through the +// same helper as the N_IDENT/N_INDEX bases #24/#27 graduated. fn indexvaluetnode(c: *cgen, n: *node) *node = { if (n == nil) { return nil; }; if (n.kind != nkind.N_INDEX) { return nil; }; @@ -1259,6 +1262,7 @@ fn indexvaluetnode(c: *cgen, n: *node) *node = { else { bt = letvartnode(c, base.str); }; }; if (base.kind == nkind.N_INDEX) { bt = indexvaluetnode(c, base); }; + if (base.kind == nkind.N_DOT) { bt = dotfieldtnode(c, base); }; if (bt == nil) { return nil; }; let k: nkind = bt.kind; if (k == nkind.N_TPTR) { return bt.lhs; }; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index a25abee0..4932c433 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -8208,6 +8208,9 @@ fn elemsizeofc(c: *cgen, t: *node) i32 = { // the outer base type to *u8 (the post-inner-index value type), so // cgindex can compute the outer element size honestly. Mirrors // cstage's `n->lhs->type` via typed-AST (cmd/w6c/cgen.c idx_eff). +// N_DOT base graduated (tasks #28/#30) so `obj.mat[i][k]` reads +// and `obj.arr[i] = v` tagged-element writes route through the +// same helper as the N_IDENT/N_INDEX bases #24/#27 graduated. fn indexvaluetnode(c: *cgen, n: *node) *node = { if (n == nil) { return nil; }; if (n.kind != nkind.N_INDEX) { return nil; }; @@ -8220,6 +8223,7 @@ fn indexvaluetnode(c: *cgen, n: *node) *node = { else { bt = letvartnode(c, base.str); }; }; if (base.kind == nkind.N_INDEX) { bt = indexvaluetnode(c, base); }; + if (base.kind == nkind.N_DOT) { bt = dotfieldtnode(c, base); }; if (bt == nil) { return nil; }; let k: nkind = bt.kind; if (k == nkind.N_TPTR) { return bt.lhs; }; @@ -14242,6 +14246,16 @@ fn cgassign(c: *cgen, n: *node) void = { }; } else { if (base.kind == nkind.N_DOT) { esz = indexbaseesz(c, base); + // Without this, the tagged-element gate + // below (keyed on elemtn) misses for + // `obj.arr[i] = v` over an [N]Tagged field + // and the store falls through to scalar — + // task #30, sister of the cgindex N_INDEX- + // base fix #24. indexvaluetnode now handles + // N_DOT base, so the element type drops out + // of the same helper. + let bt: *node = indexvaluetnode(c, lhs); + if (bt != nil) { elemtn = bt; }; } else { if (base.kind == nkind.N_INDEX) { // Chained-write write-side parallel of the // cgindex N_INDEX-base arm graduated in #24: @@ -18010,6 +18024,12 @@ fn scanlocals(c: *cgen, n: *node) i32 = { // cgwidentaggedstore to materialise the source in before copying // to the element address. Slot is shared per function via // c.tagscrsz (raised to the largest element slot_sz seen). + // N_DOT-base graduated (task #30) so `obj.arr[i] = v` for an + // [N]Tagged struct field pre-reserves the slot — parallels the + // cgassign N_DOT-base elemtn fix in cgenexpr.ww. Without this + // arm the runtime localadd allocates past the frame boundary + // and clobbers live locals; cstage handles the shape naturally + // via first-use+fail-loud (rule-10 convergence filed as #15). if (n.kind == nkind.N_ASSIGN) { let alhs: *node = n.lhs; if (alhs != nil) { @@ -18034,6 +18054,21 @@ fn scanlocals(c: *cgen, n: *node) i32 = { }; }; }; + if (abase.kind == nkind.N_DOT) { + let ft: *node = dotfieldtnode(c, abase); + if (ft != nil) { + let bk: nkind = ft.kind; + let etn: *node = nil; + if (bk == nkind.N_TARRAY) { etn = ft.lhs; }; + if (bk == nkind.N_TSLICE) { etn = ft.lhs; }; + if (bk == nkind.N_TPTR) { etn = ft.lhs; }; + if (etn != nil) { + if (istaggedtype(c, etn)) { + total += tagscrbump(c, slotsize(c, etn)); + }; + }; + }; + }; }; }; }; diff --git a/test/wcc/741_dotbase_chained.c b/test/wcc/741_dotbase_chained.c new file mode 100644 index 00000000..a1b6f0af --- /dev/null +++ b/test/wcc/741_dotbase_chained.c @@ -0,0 +1,239 @@ +/* + * 741_dotbase_chained — sentinel for #28 + #30. Pins wwstage's + * `indexvaluetnode` to handle N_DOT base (graduates the helper #24 + * introduced for chained N_INDEX so the N_DOT base shape rides the + * same path). + * + * Two sister latents, one helper graduation: + * + * Read (#28): `obj.mat[i][k]` where obj is a struct with field + * `mat: **u8`. The outer N_INDEX's base is the inner N_INDEX, + * whose base is N_DOT. cgindex routes through `indexvaluetnode` + * for the inner base type; pre-fix `indexvaluetnode` only handled + * N_IDENT + N_INDEX bases, so the recursion bottomed out at the + * N_DOT base with bt=nil. esz fell through to 8 and signed_elem + * to false — wwstage emitted a stray outer `MOVQ $8, CX; IMULQ + * CX, AX` plus `MOVQ (AX), AX` (8-byte read over a 1-byte u8) + * instead of cstage's bare `MOVZBQ (AX), AX`. + * + * Write (#30): `obj.arr[i] = v` where obj is a struct with field + * `arr: [N]Tagged` (e.g. (i64|str)). cgassign's N_DOT-base arm + * computed esz via `indexbaseesz` but never set `elemtn`, so the + * tagged-element store gate (keyed on elemtn) missed and the + * 24-byte tagged slot was overwritten by a single MOVQ scalar + * store — wrong-width store + tag/payload junk in the upper + * 16 bytes. The matching scanlocals pre-pass arm (cgendecl.ww) + * was also missing N_DOT base, so post-elemtn-fix the @tagscr + * slot allocated past the frame boundary and clobbered live + * locals (s, i). + * + * Cstage walks `n->lhs->type` directly via the typed AST (cmd/w6c/ + * cgen.c idx_eff + the N_INDEX-lhs N_ASSIGN branch). Wwstage + * mirrors via `indexvaluetnode`, which #24 introduced for the + * N_INDEX-base case and #28/#30 now graduate for N_DOT base via + * `dotfieldtnode` lookup. + * + * Class A wwstage cgen UNDER. No in-tree consumer; sister latents + * filed during #24 + #27 reviews (commits aa8ca47, 3ba1922). This + * sentinel is the sole exerciser of the shape. + * + * Per row: in the `probe` body, assert the dispatch-defining MOV + * mnemonic + scaling is present, plus cstage vs wwstage asm is + * byte-identical. + */ +#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; +} + +/* `needle` = a substring that must appear in the probe body and + * uniquely identifies the post-fix dispatch (narrow MOV mnemonic + * for read, 24B element scale for tagged write). + * `antineedle` = a substring that MUST NOT appear — pre-fix wwstage + * regression marker. Empty string skips the anti-check (write row + * relies on cmp -s byte-id to catch divergence, since the tagged- + * store byte-copy legitimately contains MOVQ-store fragments). */ +struct row { + const char *label; + const char *src; + const char *needle; + const char *antineedle; +}; + +static const struct row rows[] = { + /* Read: chained `obj.mat[i][k]` where mat: **u8. Post-fix wwstage + * routes the inner N_INDEX's N_DOT base through indexvaluetnode → + * dotfieldtnode, esz collapses to 1, outer load becomes MOVZBQ. + * Pre-fix had stray `MOVQ (AX), AX` (8-byte read over u8). */ + { "dotbase_chained_read", + "type S = struct{ pad: i64, mat: **u8 };\n" + "fn probe(s: *S) u8 = {\n" + " let i: i32 = 0;\n" + " let k: u64 = 0u64;\n" + " return s.mat[i][k];\n" + "};\n" + "export fn main() i32 = { return 0; };\n", + "\tMOVZBQ\t(AX), AX\n", + "" }, + /* Write: `obj.arr[i] = v` where arr: [N](i64|str) — 24B tagged. + * Post-fix wwstage: cgassign N_DOT arm sets elemtn → tagged-store + * path → IMULQ $24 + byte-copy from scratch. Scanlocals N_DOT arm + * pre-reserves @tagscr in the frame. Pre-fix: scalar `MOVQ AX, + * (BX)` over the 24B slot. */ + { "dotbase_array_tagged_write", + "type T = (i64 | str);\n" + "type S = struct{ pad: i64, arr: [4]T };\n" + "fn probe(s: *S) void = {\n" + " let i: i32 = 0;\n" + " s.arr[i] = 42i64;\n" + "};\n" + "export fn main() i32 = { return 0; };\n", + "\tMOVQ\t$24, CX\n", + "" }, +}; + +static int +slurp(const char *path, char *buf, size_t cap) +{ + FILE *f = fopen(path, "rb"); + if (!f) return -1; + size_t n = fread(buf, 1, cap - 1, f); + fclose(f); + buf[n] = '\0'; + return (int)n; +} + +static int +emit_s(const char *w6c, const struct row *r, int i, char *out_s, size_t cap) +{ + char src[96], cmd[1024]; + snprintf(src, sizeof src, "/tmp/dotbase_%d_%d.ww", getpid(), i); + snprintf(out_s, cap, "/tmp/dotbase_%d_%d_%s.s", + getpid(), i, w6c[strlen(w6c) - 1] == 'w' ? "ww" : "c"); + + FILE *f = fopen(src, "wb"); + if (!f) return -1; + fputs(r->src, f); + fclose(f); + + snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, out_s, src); + int rc = runwait(cmd); + unlink(src); + return rc; +} + +/* Inside `TEXT probe`, the needle must appear and the antineedle + * must NOT appear (anti-regression on pre-fix wwstage emit). */ +static int +check_probe(const char *spath, const struct row *r, const char *stage) +{ + char buf[1 << 14]; + if (slurp(spath, buf, sizeof buf) < 0) { + fprintf(stderr, "row[%s][%s]: cannot read %s\n", + r->label, stage, spath); + return -1; + } + const char *fn = strstr(buf, "TEXT probe"); + if (!fn) { + fprintf(stderr, + "row[%s][%s]: no TEXT probe in %s\n", + r->label, stage, spath); + return -1; + } + const char *ret = strstr(fn, "\tRET\n"); + const char *m = strstr(fn, r->needle); + if (!m || (ret && m > ret)) { + fprintf(stderr, + "row[%s][%s]: expected `%s` in probe body\n", + r->label, stage, r->needle); + return -1; + } + if (r->antineedle[0] != '\0') { + const char *bad = strstr(fn, r->antineedle); + if (bad && (!ret || bad < ret)) { + fprintf(stderr, + "row[%s][%s]: stray `%s` in probe body — " + "pre-fix wwstage regression\n", + r->label, stage, r->antineedle); + return -1; + }; + } + return 0; +} + +int +main(void) +{ + const char *bin = getenv("BIN"); + if (!bin) bin = "out/bin"; + char absbin[512]; + if (bin[0] != '/') { + char cwd[256]; + if (getcwd(cwd, sizeof cwd) == NULL) return 1; + snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); + bin = absbin; + } + + char w6c[640], w6c_ww[640]; + snprintf(w6c, sizeof w6c, "%s/w6c", bin); + snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin); + + int have_ww = (access(w6c_ww, X_OK) == 0); + int n = (int)(sizeof rows / sizeof rows[0]); + int total = 0, fail = 0; + + for (int i = 0; i < n; i++) { + char cs_path[128], ws_path[128]; + + if (emit_s(w6c, &rows[i], i, cs_path, sizeof cs_path) != 0) { + fprintf(stderr, + "dotbase_chained[cstage][%s]: w6c failed\n", + rows[i].label); + fail++; total++; continue; + } + total++; + if (check_probe(cs_path, &rows[i], "cstage") != 0) fail++; + + if (!have_ww) { unlink(cs_path); continue; } + + if (emit_s(w6c_ww, &rows[i], i, ws_path, sizeof ws_path) != 0) { + fprintf(stderr, + "dotbase_chained[wwstage][%s]: w6c_ww failed\n", + rows[i].label); + fail++; total++; + unlink(cs_path); continue; + } + total++; + if (check_probe(ws_path, &rows[i], "wwstage") != 0) fail++; + + total++; + char cmd[512]; + snprintf(cmd, sizeof cmd, "cmp -s %s %s", cs_path, ws_path); + if (runwait(cmd) != 0) { + fprintf(stderr, + "dotbase_chained[%s]: cstage vs wwstage asm differs\n", + rows[i].label); + fail++; + } + + unlink(cs_path); unlink(ws_path); + } + + if (fail) { + fprintf(stderr, + "dotbase_chained: %d/%d fixtures failed\n", fail, total); + return 1; + } + printf("dotbase_chained: %d/%d ok\n", total, total); + return 0; +}