From 75c1b278e6bea102ecc7b94cda6d9b7693b98a61 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Sat, 13 Jun 2026 00:21:21 +0900 Subject: [PATCH] wcc/ww: def-dim array slice takes len and cap from the type table MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Slicing an array whose dimension is a def constant gave len 0 — the default-hi and cgbasecap arms only read an N_INTLIT dimension. Route the dimension through the type table (one root, four arms: default-hi and cgbasecap, local and global each), byte-identical for the def-dim SLICE shape. The def-dim array .len/.ptr FIELD-read keeps the N_INTLIT-only limitation — filed as task #56 (cgdot sibling). Review item #21. --- Makefile | 11 ++ selfhost/cmd/w6c/main.combined.ww | 89 ++++++++++---- selfhost/cmd/wcc/cgenexpr.ww | 89 ++++++++++---- selfhost/cmd/wwdump/main.combined.ww | 89 ++++++++++---- test/wcc/989_defdim_slice_run.c | 175 +++++++++++++++++++++++++++ 5 files changed, 387 insertions(+), 66 deletions(-) create mode 100644 test/wcc/989_defdim_slice_run.c diff --git a/Makefile b/Makefile index 8dbbf0ea..c19ec793 100644 --- a/Makefile +++ b/Makefile @@ -254,6 +254,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_tupfieldsize_run \ $(BIN)/test_tagtupfieldsize_run \ $(BIN)/test_arrlit_tail_zero_run \ + $(BIN)/test_defdim_slice_run \ $(BIN)/test_gunsigned_run \ $(BIN)/test_taggedidx_run \ $(BIN)/test_fnptrcollide_run \ @@ -723,6 +724,16 @@ $(BIN)/test_arrlit_tail_zero_run: test/wcc/989_arrlit_tail_zero_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +# 989_defdim_slice_run (#21): slicing a `def`-dimensioned array (`[MAX]T`) +# resolves length + capacity from the type table, not the AST dim node. +# Builds+runs on BOTH driver twins (rule-10), pinning the absolute value. +$(BIN)/test_defdim_slice_run: test/wcc/989_defdim_slice_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 $@ $< + # 989_gunsigned_run (F7-c5, #25): a module-global unsigned ident on the # divide/shift/relational path must pick the unsigned opcode. Builds+runs on # BOTH driver twins (rule-10). CLASS-M — see the test header. diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 006e939e..edb7a4a5 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -25683,14 +25683,28 @@ fn cgbasecap(c: *cgen, base: *node, dst: str) bool = { if (tn == nil) { return false; }; if (tn.kind == nkind.N_TARRAY) { let lenn: *node = tn.rhs; - if (lenn == nil) { return false; }; - if (lenn.kind != nkind.N_INTLIT) { return false; }; - emitline("\tMOVQ\t$"); - emituint(lenn.uval); - emitline(", "); - emitline(dst); - emitline("\n"); - return true; + if (lenn != nil && lenn.kind == nkind.N_INTLIT) { + emitline("\tMOVQ\t$"); + emituint(lenn.uval); + emitline(", "); + emitline(dst); + emitline("\n"); + return true; + }; + // #21: a def/const array dim — non-N_INTLIT — reads its cap + // from the stamped array tinfo (rule-13), the cap twin of the + // default-hi fix; pre-fix cgbasecap returned false here and the + // caller fell to cap=len (cs computes base_cap-lo from bu->alen). + let abt: *tinfo = tichase(base.type_: *tinfo); + if (abt != nil && abt.kind == tykind.TY_ARRAY) { + emitline("\tMOVQ\t$"); + emitint(abt.alen: i64); + emitline(", "); + emitline(dst); + emitline("\n"); + return true; + }; + return false; }; if (tn.kind == nkind.N_TSLICE) { emitline("\tMOVQ\t"); @@ -25741,14 +25755,25 @@ fn cgbasecap(c: *cgen, base: *node, dst: str) bool = { if (gt == nil) { return false; }; if (gt.kind == nkind.N_TARRAY) { let lenn: *node = gt.rhs; - if (lenn == nil) { return false; }; - if (lenn.kind != nkind.N_INTLIT) { return false; }; - emitline("\tMOVQ\t$"); - emituint(lenn.uval); - emitline(", "); - emitline(dst); - emitline("\n"); - return true; + if (lenn != nil && lenn.kind == nkind.N_INTLIT) { + emitline("\tMOVQ\t$"); + emituint(lenn.uval); + emitline(", "); + emitline(dst); + emitline("\n"); + return true; + }; + // #21: def/const global array dim cap — twin of the local arm. + let abt: *tinfo = tichase(base.type_: *tinfo); + if (abt != nil && abt.kind == tykind.TY_ARRAY) { + emitline("\tMOVQ\t$"); + emitint(abt.alen: i64); + emitline(", "); + emitline(dst); + emitline("\n"); + return true; + }; + return false; }; if (gt.kind == nkind.N_TSLICE) { emitline("\tLEAQ\t"); @@ -25990,10 +26015,21 @@ fn cgslice(c: *cgen, n: *node) void = { if (tn != nil) { if (tn.kind == nkind.N_TARRAY) { let lenn: *node = tn.rhs; - if (lenn != nil) { - if (lenn.kind == nkind.N_INTLIT) { + if (lenn != nil && lenn.kind == nkind.N_INTLIT) { + emitline("\tMOVQ\t$"); + emituint(lenn.uval); + emitline(", AX\n"); + handled = true; + } else { + // #21: a def/const array dim (`[MAX]u8`) is not an + // N_INTLIT node, so the literal read above misses it + // (MOVQ $0 default-hi -> len 0 / underflow, exit 255). + // Read the resolved length from the stamped array + // tinfo (rule-13), mirroring cstage's bu->alen. + let abt: *tinfo = tichase(base.type_: *tinfo); + if (abt != nil && abt.kind == tykind.TY_ARRAY) { emitline("\tMOVQ\t$"); - emituint(lenn.uval); + emitint(abt.alen: i64); emitline(", AX\n"); handled = true; }; @@ -26034,10 +26070,19 @@ fn cgslice(c: *cgen, n: *node) void = { let handled: bool = false; if (globaltn.kind == nkind.N_TARRAY) { let lenn: *node = globaltn.rhs; - if (lenn != nil) { - if (lenn.kind == nkind.N_INTLIT) { + if (lenn != nil && lenn.kind == nkind.N_INTLIT) { + emitline("\tMOVQ\t$"); + emituint(lenn.uval); + emitline(", AX\n"); + handled = true; + } else { + // #21: a def/const global array dim — non-N_INTLIT, read the + // resolved length off the stamped array tinfo (rule-13), the + // twin of the local arm above (cstage's bu->alen). + let abt: *tinfo = tichase(base.type_: *tinfo); + if (abt != nil && abt.kind == tykind.TY_ARRAY) { emitline("\tMOVQ\t$"); - emituint(lenn.uval); + emitint(abt.alen: i64); emitline(", AX\n"); handled = true; }; diff --git a/selfhost/cmd/wcc/cgenexpr.ww b/selfhost/cmd/wcc/cgenexpr.ww index 607c9135..0d59129f 100644 --- a/selfhost/cmd/wcc/cgenexpr.ww +++ b/selfhost/cmd/wcc/cgenexpr.ww @@ -2463,14 +2463,28 @@ fn cgbasecap(c: *cgen, base: *node, dst: str) bool = { if (tn == nil) { return false; }; if (tn.kind == nkind.N_TARRAY) { let lenn: *node = tn.rhs; - if (lenn == nil) { return false; }; - if (lenn.kind != nkind.N_INTLIT) { return false; }; - emitline("\tMOVQ\t$"); - emituint(lenn.uval); - emitline(", "); - emitline(dst); - emitline("\n"); - return true; + if (lenn != nil && lenn.kind == nkind.N_INTLIT) { + emitline("\tMOVQ\t$"); + emituint(lenn.uval); + emitline(", "); + emitline(dst); + emitline("\n"); + return true; + }; + // #21: a def/const array dim — non-N_INTLIT — reads its cap + // from the stamped array tinfo (rule-13), the cap twin of the + // default-hi fix; pre-fix cgbasecap returned false here and the + // caller fell to cap=len (cs computes base_cap-lo from bu->alen). + let abt: *tinfo = tichase(base.type_: *tinfo); + if (abt != nil && abt.kind == tykind.TY_ARRAY) { + emitline("\tMOVQ\t$"); + emitint(abt.alen: i64); + emitline(", "); + emitline(dst); + emitline("\n"); + return true; + }; + return false; }; if (tn.kind == nkind.N_TSLICE) { emitline("\tMOVQ\t"); @@ -2521,14 +2535,25 @@ fn cgbasecap(c: *cgen, base: *node, dst: str) bool = { if (gt == nil) { return false; }; if (gt.kind == nkind.N_TARRAY) { let lenn: *node = gt.rhs; - if (lenn == nil) { return false; }; - if (lenn.kind != nkind.N_INTLIT) { return false; }; - emitline("\tMOVQ\t$"); - emituint(lenn.uval); - emitline(", "); - emitline(dst); - emitline("\n"); - return true; + if (lenn != nil && lenn.kind == nkind.N_INTLIT) { + emitline("\tMOVQ\t$"); + emituint(lenn.uval); + emitline(", "); + emitline(dst); + emitline("\n"); + return true; + }; + // #21: def/const global array dim cap — twin of the local arm. + let abt: *tinfo = tichase(base.type_: *tinfo); + if (abt != nil && abt.kind == tykind.TY_ARRAY) { + emitline("\tMOVQ\t$"); + emitint(abt.alen: i64); + emitline(", "); + emitline(dst); + emitline("\n"); + return true; + }; + return false; }; if (gt.kind == nkind.N_TSLICE) { emitline("\tLEAQ\t"); @@ -2770,10 +2795,21 @@ fn cgslice(c: *cgen, n: *node) void = { if (tn != nil) { if (tn.kind == nkind.N_TARRAY) { let lenn: *node = tn.rhs; - if (lenn != nil) { - if (lenn.kind == nkind.N_INTLIT) { + if (lenn != nil && lenn.kind == nkind.N_INTLIT) { + emitline("\tMOVQ\t$"); + emituint(lenn.uval); + emitline(", AX\n"); + handled = true; + } else { + // #21: a def/const array dim (`[MAX]u8`) is not an + // N_INTLIT node, so the literal read above misses it + // (MOVQ $0 default-hi -> len 0 / underflow, exit 255). + // Read the resolved length from the stamped array + // tinfo (rule-13), mirroring cstage's bu->alen. + let abt: *tinfo = tichase(base.type_: *tinfo); + if (abt != nil && abt.kind == tykind.TY_ARRAY) { emitline("\tMOVQ\t$"); - emituint(lenn.uval); + emitint(abt.alen: i64); emitline(", AX\n"); handled = true; }; @@ -2814,10 +2850,19 @@ fn cgslice(c: *cgen, n: *node) void = { let handled: bool = false; if (globaltn.kind == nkind.N_TARRAY) { let lenn: *node = globaltn.rhs; - if (lenn != nil) { - if (lenn.kind == nkind.N_INTLIT) { + if (lenn != nil && lenn.kind == nkind.N_INTLIT) { + emitline("\tMOVQ\t$"); + emituint(lenn.uval); + emitline(", AX\n"); + handled = true; + } else { + // #21: a def/const global array dim — non-N_INTLIT, read the + // resolved length off the stamped array tinfo (rule-13), the + // twin of the local arm above (cstage's bu->alen). + let abt: *tinfo = tichase(base.type_: *tinfo); + if (abt != nil && abt.kind == tykind.TY_ARRAY) { emitline("\tMOVQ\t$"); - emituint(lenn.uval); + emitint(abt.alen: i64); emitline(", AX\n"); handled = true; }; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index b9ef040e..3ec59838 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -25683,14 +25683,28 @@ fn cgbasecap(c: *cgen, base: *node, dst: str) bool = { if (tn == nil) { return false; }; if (tn.kind == nkind.N_TARRAY) { let lenn: *node = tn.rhs; - if (lenn == nil) { return false; }; - if (lenn.kind != nkind.N_INTLIT) { return false; }; - emitline("\tMOVQ\t$"); - emituint(lenn.uval); - emitline(", "); - emitline(dst); - emitline("\n"); - return true; + if (lenn != nil && lenn.kind == nkind.N_INTLIT) { + emitline("\tMOVQ\t$"); + emituint(lenn.uval); + emitline(", "); + emitline(dst); + emitline("\n"); + return true; + }; + // #21: a def/const array dim — non-N_INTLIT — reads its cap + // from the stamped array tinfo (rule-13), the cap twin of the + // default-hi fix; pre-fix cgbasecap returned false here and the + // caller fell to cap=len (cs computes base_cap-lo from bu->alen). + let abt: *tinfo = tichase(base.type_: *tinfo); + if (abt != nil && abt.kind == tykind.TY_ARRAY) { + emitline("\tMOVQ\t$"); + emitint(abt.alen: i64); + emitline(", "); + emitline(dst); + emitline("\n"); + return true; + }; + return false; }; if (tn.kind == nkind.N_TSLICE) { emitline("\tMOVQ\t"); @@ -25741,14 +25755,25 @@ fn cgbasecap(c: *cgen, base: *node, dst: str) bool = { if (gt == nil) { return false; }; if (gt.kind == nkind.N_TARRAY) { let lenn: *node = gt.rhs; - if (lenn == nil) { return false; }; - if (lenn.kind != nkind.N_INTLIT) { return false; }; - emitline("\tMOVQ\t$"); - emituint(lenn.uval); - emitline(", "); - emitline(dst); - emitline("\n"); - return true; + if (lenn != nil && lenn.kind == nkind.N_INTLIT) { + emitline("\tMOVQ\t$"); + emituint(lenn.uval); + emitline(", "); + emitline(dst); + emitline("\n"); + return true; + }; + // #21: def/const global array dim cap — twin of the local arm. + let abt: *tinfo = tichase(base.type_: *tinfo); + if (abt != nil && abt.kind == tykind.TY_ARRAY) { + emitline("\tMOVQ\t$"); + emitint(abt.alen: i64); + emitline(", "); + emitline(dst); + emitline("\n"); + return true; + }; + return false; }; if (gt.kind == nkind.N_TSLICE) { emitline("\tLEAQ\t"); @@ -25990,10 +26015,21 @@ fn cgslice(c: *cgen, n: *node) void = { if (tn != nil) { if (tn.kind == nkind.N_TARRAY) { let lenn: *node = tn.rhs; - if (lenn != nil) { - if (lenn.kind == nkind.N_INTLIT) { + if (lenn != nil && lenn.kind == nkind.N_INTLIT) { + emitline("\tMOVQ\t$"); + emituint(lenn.uval); + emitline(", AX\n"); + handled = true; + } else { + // #21: a def/const array dim (`[MAX]u8`) is not an + // N_INTLIT node, so the literal read above misses it + // (MOVQ $0 default-hi -> len 0 / underflow, exit 255). + // Read the resolved length from the stamped array + // tinfo (rule-13), mirroring cstage's bu->alen. + let abt: *tinfo = tichase(base.type_: *tinfo); + if (abt != nil && abt.kind == tykind.TY_ARRAY) { emitline("\tMOVQ\t$"); - emituint(lenn.uval); + emitint(abt.alen: i64); emitline(", AX\n"); handled = true; }; @@ -26034,10 +26070,19 @@ fn cgslice(c: *cgen, n: *node) void = { let handled: bool = false; if (globaltn.kind == nkind.N_TARRAY) { let lenn: *node = globaltn.rhs; - if (lenn != nil) { - if (lenn.kind == nkind.N_INTLIT) { + if (lenn != nil && lenn.kind == nkind.N_INTLIT) { + emitline("\tMOVQ\t$"); + emituint(lenn.uval); + emitline(", AX\n"); + handled = true; + } else { + // #21: a def/const global array dim — non-N_INTLIT, read the + // resolved length off the stamped array tinfo (rule-13), the + // twin of the local arm above (cstage's bu->alen). + let abt: *tinfo = tichase(base.type_: *tinfo); + if (abt != nil && abt.kind == tykind.TY_ARRAY) { emitline("\tMOVQ\t$"); - emituint(lenn.uval); + emitint(abt.alen: i64); emitline(", AX\n"); handled = true; }; diff --git a/test/wcc/989_defdim_slice_run.c b/test/wcc/989_defdim_slice_run.c new file mode 100644 index 00000000..20effbcf --- /dev/null +++ b/test/wcc/989_defdim_slice_run.c @@ -0,0 +1,175 @@ +/* + * 989_defdim_slice_run (#21) — slicing an array whose dimension is a `def` + * constant (`[MAX]T`) must resolve the length AND the capacity from the type + * table, not the AST dimension node. + * + * THE BUG (wwstage only): cgslice's default-hi and cgbasecap both read the + * array dimension straight off the AST tnode and only handled an N_INTLIT + * dim. A `def MAX` dim arrives as a non-literal node, so default-hi fell to + * `MOVQ $0` (len 0 → underflow, exit 255) and cgbasecap returned false (cap + * fell back to len). cstage reads the resolved bu->alen. THE FIX: when the + * dim is not an N_INTLIT node, read the length from the stamped array tinfo + * (rule-13), in both the local and global arms of both helpers. + * + * row | shape | exit (cs==ww) + * --------------+------------------------------------+-------------- + * deflen_local | def MAX=4; buf[1:].len (local) | 3 (4-1) + * deflen_global | def MAX=4; g[1:].len (global) | 3 (4-1) + * defcap | def MAX=6; s=buf[2:4]; len*100+cap | 204 (len2,cap4) + * defcap_global | def MAX=6; g[2:4]; len*100+cap | 204 (cgbasecap global) + * Pre-fix: deflen_* ww=255 (len 0/underflow); defcap cap diverged from cs. + * All four cgen arms (default-hi local+global, cgbasecap local+global) are + * pinned: defcap teeth the cap≠len word the global arm could drop. + */ +#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[] = { + { "deflen_local", + "package main;\n" + "def MAX: i32 = 4;\n" + "export fn main() i32 = {\n" + " let buf: [MAX]u8 = [10, 20, 30, 40];\n" + " let s: []u8 = buf[1:];\n" + " return s.len: i32;\n" + "};\n", + 3 }, + + { "deflen_global", + "package main;\n" + "def MAX: i32 = 4;\n" + "let g: [MAX]u8 = [10, 20, 30, 40];\n" + "export fn main() i32 = {\n" + " let s: []u8 = g[1:];\n" + " return s.len: i32;\n" + "};\n", + 3 }, + + { "defcap", + "package main;\n" + "def MAX: i32 = 6;\n" + "export fn main() i32 = {\n" + " let buf: [MAX]u8 = [1, 2, 3, 4, 5, 6];\n" + " let s: []u8 = buf[2:4];\n" + " return (s.len * 100 + s.cap): i32;\n" + "};\n", + 204 }, + + { "defcap_global", + "package main;\n" + "def MAX: i32 = 6;\n" + "let g: [MAX]u8 = [1, 2, 3, 4, 5, 6];\n" + "export fn main() i32 = {\n" + " let s: []u8 = g[2:4];\n" + " return (s.len * 100 + s.cap): i32;\n" + "};\n", + 204 }, +}; + +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/dds_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/dds_%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); + int have_ww = (access(wdrv, X_OK) == 0); + + int n = (int)(sizeof rows / sizeof rows[0]); + int total = 0, fail = 0; + + for (int i = 0; i < n; i++) { + total++; + int gc = run_build(cdrv, &rows[i], i); + if (gc < 0) { + fprintf(stderr, "defdim_slice[cstage][%s]: build/run failed " + "(got %d)\n", rows[i].label, gc); + fail++; + continue; + } + if (gc != rows[i].want_exit) { + fprintf(stderr, "defdim_slice[cstage][%s]: exit=%d want=%d\n", + rows[i].label, gc, rows[i].want_exit); + fail++; + } + if (!have_ww) { + fprintf(stderr, "defdim_slice: skip wwstage (no %s)\n", wdrv); + continue; + } + int gw = run_build(wdrv, &rows[i], i); + if (gw != gc) { + fprintf(stderr, "defdim_slice[%s]: cs=%d != ww=%d " + "(def-dim length/cap divergence — #21)\n", + rows[i].label, gc, gw); + fail++; + } + if (gw != rows[i].want_exit) { + fprintf(stderr, "defdim_slice[wwstage][%s]: exit=%d want=%d\n", + rows[i].label, gw, rows[i].want_exit); + fail++; + } + } + + if (fail) { + fprintf(stderr, "defdim_slice_run: %d/%d checks failed\n", + fail, total); + return 1; + } + printf("defdim_slice_run: %d/%d ok\n", total, total); + return 0; +}