diff --git a/Makefile b/Makefile index 05b9f956..b9e8f642 100644 --- a/Makefile +++ b/Makefile @@ -275,10 +275,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_structlocal_frame \ $(BIN)/test_structcopytail_run \ $(BIN)/test_arrlit_tail_zero_run \ - $(BIN)/test_defdim_slice_run \ $(BIN)/test_allocalias_run \ - $(BIN)/test_defdim_field_run \ - $(BIN)/test_defdim_argslice_run \ $(BIN)/test_slttypepref_run \ $(BIN)/test_defercap_run \ $(BIN)/test_loopcap_run \ @@ -961,16 +958,6 @@ $(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_allocalias_run (#26): alloc(T{...}) for an alias T sizes + field-fills # from the alias-chased struct layout, not a name-keyed lookup of the literal # head. Builds+runs on BOTH driver twins (rule-10). @@ -981,28 +968,6 @@ $(BIN)/test_allocalias_run: test/wcc/989_allocalias_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< -# 989_defdim_field_run (#56): `.len` field-read on a `def`-dimensioned array -# (`[MAX]T`) resolves the length from the type table in the local/global/def -# cgdot arms + letemitsize (cgdot sibling of #21). Builds+runs on BOTH driver -# twins (rule-10). -$(BIN)/test_defdim_field_run: test/wcc/989_defdim_field_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_defdim_argslice_run (#56 c4): passing a slice of a `def`-dimensioned -# array as a call arg resolves the default-hi length from the type table in -# the N_SLICE arg-push arms (cgenutil pushargsrev local+global). Builds+runs -# on BOTH driver twins (rule-10). -$(BIN)/test_defdim_argslice_run: test/wcc/989_defdim_argslice_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_slttypepref_run (#58/#50, c1): scopelookuptype prefers the current # module's SK_TYPE when a value binding shadows a type leaf two modules both # export. Builds+runs on BOTH driver twins (rule-10). See the test header. @@ -3464,7 +3429,7 @@ test-lang: all LANGBYTEID_DIR = $(OUT)/langbyteid LANGBYTEID_FILES = $(wildcard test/lang/*_test.ww) LANGBYTEID_VERB = test -c -LANGBYTEID_EXPECTED_MIN = 31 +LANGBYTEID_EXPECTED_MIN = 34 $(if $(LANGBYTEID_FILES),,$(error test-lang-byteid: empty corpus)) test-lang-byteid: all @set -e; \ diff --git a/test/lang/defdim_argslice_test.ww b/test/lang/defdim_argslice_test.ww new file mode 100644 index 00000000..7e80eb2b --- /dev/null +++ b/test/lang/defdim_argslice_test.ww @@ -0,0 +1,35 @@ +// defdim_argslice_test — passing a slice of a `def`-dimensioned array +// (`take(buf[1:])` on `[MAX]T`) as a call argument must resolve the default-hi +// length from the type table, the N_SLICE arg-push member of the def-dim +// family, migrated from test/wcc/989_defdim_argslice_run.c (#56 c4). pushargsrev's +// N_SLICE default-hi arms (local / global) only handled an N_INTLIT dim, so a +// plain `[MAX]u8` base emitted nothing and the pushed slice header's len word +// was left stale -- the callee read garbage (ww 135 local / 255 global). The +// fix reads alen from the stamped array tinfo (rule-13) in both arg-push arms. +// `take` returns s.len, so the asserted 4 IS the pushed-header length contract; +// the litctrl row pins the N_INTLIT path so the new else-arm can't perturb it. +// T2 (test-lang-byteid) keeps the cs==ww net the .c twin's run-compare gave. + +package defdim_argslice_test; + +def MAX: i32 = 5; +let g: [MAX]u8 = [1, 2, 3, 4, 5]; + +fn take(s: []u8) i32 = { return s.len: i32; }; + +@test fn local_argslice() void = { + // local def-dim base: take(buf[1:]) sees len 5-1 = 4. + let buf: [MAX]u8 = [1, 2, 3, 4, 5]; + assert(take(buf[1:]) == 4); +}; + +@test fn global_argslice() void = { + // global def-dim base: take(g[1:]) sees len 4. + assert(take(g[1:]) == 4); +}; + +@test fn litctrl_argslice() void = { + // literal-dim control: the N_INTLIT arm stays correct (no regress). + let buf: [5]u8 = [1, 2, 3, 4, 5]; + assert(take(buf[1:]) == 4); +}; diff --git a/test/lang/defdim_field_test.ww b/test/lang/defdim_field_test.ww new file mode 100644 index 00000000..78c033de --- /dev/null +++ b/test/lang/defdim_field_test.ww @@ -0,0 +1,37 @@ +// defdim_field_test — `.len` field-read on an array whose dimension is a +// `def` constant (`[MAX]T`) must resolve the length from the type table, the +// cgdot sibling of the #21 cgslice fix, migrated from +// test/wcc/989_defdim_field_run.c (#56). A `def MAX` dim arrives as a +// non-literal node; wwstage's cgdot `.len` arms (local / let-global / def) + +// letemitsize only handled an N_INTLIT dim, so `.len` folded to MOVQ $0 (ww +// returned 0 where cstage reads the resolved bu->alen). The fix reads alen from +// the stamped array tinfo (rule-13) in each arm. cstage was always correct; T2 +// (test-lang-byteid) keeps the cs==ww net the .c twin's run-compare provided. + +package defdim_field_test; + +def MAX: i32 = 5; +let g: [MAX]u8 = [1, 2, 3, 4, 5]; +def A: [MAX]u8 = [1, 2, 3, 4, 5]; + +@test fn local_len() void = { + // local [MAX]u8 — the cgdot local arm; pre-fix ww folded to 0. + let buf: [MAX]u8 = [1, 2, 3, 4, 5]; + assert(buf.len: i32 == 5); +}; + +@test fn global_len() void = { + // let-global [MAX]u8 — the cgdot let-global arm. + assert(g.len: i32 == 5); +}; + +@test fn defarr_len() void = { + // def [MAX]u8 constant — the cgdot def arm. + assert(A.len: i32 == 5); +}; + +@test fn sum_len() void = { + // all three arms in one fn (the .c `sum` row): 5 + 5 + 5 = 15. + let buf: [MAX]u8 = [1, 2, 3, 4, 5]; + assert((buf.len + g.len + A.len): i32 == 15); +}; diff --git a/test/lang/defdim_slice_test.ww b/test/lang/defdim_slice_test.ww new file mode 100644 index 00000000..a7458eeb --- /dev/null +++ b/test/lang/defdim_slice_test.ww @@ -0,0 +1,47 @@ +// defdim_slice_test — 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, migrated from test/wcc/989_defdim_slice_run.c (#21). +// A `def MAX` dim arrives as a non-literal node; wwstage's cgslice default-hi +// fell to MOVQ $0 (len 0 -> underflow, exit 255) and cgbasecap returned false +// (cap fell back to len). The fix reads alen from the stamped array tinfo +// (rule-13) in both the local and global arms of both helpers. cstage reads the +// resolved bu->alen and was always correct; the defcap rows poison cap != len +// so a stage that drops the cap word FAILS. T2 keeps the cs==ww net. +// +// Two def constants (MAX4/MAX6) and two globals (g4/g6) since one package +// cannot redeclare `def MAX`; the def-dim path is exercised regardless of name. + +package defdim_slice_test; + +def MAX4: i32 = 4; +def MAX6: i32 = 6; +let g4: [MAX4]u8 = [10, 20, 30, 40]; +let g6: [MAX6]u8 = [1, 2, 3, 4, 5, 6]; + +@test fn deflen_local() void = { + // local default-hi: cgslice reads N=4 from tinfo; buf[1:].len = 4-1 = 3. + let buf: [MAX4]u8 = [10, 20, 30, 40]; + let s: []u8 = buf[1:]; + assert(s.len: i32 == 3); +}; + +@test fn deflen_global() void = { + // global default-hi: g4[1:].len = 4-1 = 3. + let s: []u8 = g4[1:]; + assert(s.len: i32 == 3); +}; + +@test fn defcap_local() void = { + // cgbasecap local: buf[2:4] over [6]u8 -> len 2, cap = 6-2 = 4 (cap != len). + let buf: [MAX6]u8 = [1, 2, 3, 4, 5, 6]; + let s: []u8 = buf[2:4]; + assert(s.len: i32 == 2); + assert(s.cap: i32 == 4); +}; + +@test fn defcap_global() void = { + // cgbasecap global: g6[2:4] -> len 2, cap = 6-2 = 4. + let s: []u8 = g6[2:4]; + assert(s.len: i32 == 2); + assert(s.cap: i32 == 4); +}; diff --git a/test/wcc/989_defdim_argslice_run.c b/test/wcc/989_defdim_argslice_run.c deleted file mode 100644 index a6e4379b..00000000 --- a/test/wcc/989_defdim_argslice_run.c +++ /dev/null @@ -1,165 +0,0 @@ -/* - * 989_defdim_argslice_run (#56, c4) — passing a slice of a `def`-dimensioned - * array (`take(buf[1:])` on `[MAX]T`) as a call argument must resolve the - * default-hi length from the type table, the N_SLICE arg-push member of the - * def-dim family. - * - * THE BUG (wwstage only): pushargsrev's N_SLICE default-hi arms (cgenutil.ww - * local ~643 / global ~690) read the array dimension straight off the AST - * tnode and only handled an N_INTLIT dim. The #60 bu60 fallback fires ONLY for - * NAMED-alias bases (bt60.kind == TY_NAMED), so a PLAIN `[MAX]u8` base reached - * the N_INTLIT arm, emitted nothing, and the slice header's len word was left - * stale — the callee read garbage. cstage reads the resolved bu->alen. THE - * FIX: when the dim is not N_INTLIT, read alen from the stamped array tinfo - * (rule-13), in both the local and global arg-push arms — the same shape as - * the #21 cgslice and #56 cgdot fixes. - * - * row | shape | exit (cs==ww) - * --------+--------------------------------------------+-------------- - * local | def MAX=5; let buf:[MAX]u8; take(buf[1:]) | 4 (was ww 135) - * global | def MAX=5; let g:[MAX]u8; take(g[1:]) | 4 (was ww 255) - * litctrl | let buf:[5]u8; take(buf[1:]) | 4 (no regress) - * The litctrl row pins the literal-dim path so the new else-arm can't double- - * emit or perturb it. - */ -#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[] = { - { "local", - "package main;\n" - "def MAX: i32 = 5;\n" - "fn take(s: []u8) i32 = { return s.len: i32; };\n" - "export fn main() i32 = {\n" - " let buf: [MAX]u8 = [1, 2, 3, 4, 5];\n" - " return take(buf[1:]);\n" - "};\n", - 4 }, - - { "global", - "package main;\n" - "def MAX: i32 = 5;\n" - "let g: [MAX]u8 = [1, 2, 3, 4, 5];\n" - "fn take(s: []u8) i32 = { return s.len: i32; };\n" - "export fn main() i32 = {\n" - " return take(g[1:]);\n" - "};\n", - 4 }, - - { "litctrl", - "package main;\n" - "fn take(s: []u8) i32 = { return s.len: i32; };\n" - "export fn main() i32 = {\n" - " let buf: [5]u8 = [1, 2, 3, 4, 5];\n" - " return take(buf[1:]);\n" - "};\n", - 4 }, -}; - -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/dda_%d_%d.ww", getpid(), i); - snprintf(tmpdir, sizeof tmpdir, "/tmp/dda_%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_argslice[cstage][%s]: build/run failed " - "(got %d)\n", rows[i].label, gc); - fail++; - continue; - } - if (gc != rows[i].want_exit) { - fprintf(stderr, "defdim_argslice[cstage][%s]: exit=%d want=%d\n", - rows[i].label, gc, rows[i].want_exit); - fail++; - } - if (!have_ww) { - fprintf(stderr, "defdim_argslice: skip wwstage (no %s)\n", wdrv); - continue; - } - int gw = run_build(wdrv, &rows[i], i); - if (gw != gc) { - fprintf(stderr, "defdim_argslice[%s]: cs=%d != ww=%d " - "(def-dim arg-push default-hi divergence — #56 c4)\n", - rows[i].label, gc, gw); - fail++; - } - if (gw != rows[i].want_exit) { - fprintf(stderr, "defdim_argslice[wwstage][%s]: exit=%d want=%d\n", - rows[i].label, gw, rows[i].want_exit); - fail++; - } - } - - if (fail) { - fprintf(stderr, "defdim_argslice_run: %d/%d checks failed\n", - fail, total); - return 1; - } - printf("defdim_argslice_run: %d/%d ok\n", total, total); - return 0; -} diff --git a/test/wcc/989_defdim_field_run.c b/test/wcc/989_defdim_field_run.c deleted file mode 100644 index 656eb304..00000000 --- a/test/wcc/989_defdim_field_run.c +++ /dev/null @@ -1,170 +0,0 @@ -/* - * 989_defdim_field_run (#56) — `.len` field-read on an array whose dimension - * is a `def` constant (`[MAX]T`) must resolve the length from the type table, - * the cgdot sibling of the #21 cgslice fix. - * - * THE BUG (wwstage only): the cgdot `.len` arms (local / let-global / def) and - * letemitsize 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 - * `.len` folded to MOVQ $0 (ww returned 0 where cstage returns the resolved - * length). 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 - * each of the local/global/def cgdot arms + letemitsize. - * - * row | shape | exit (cs==ww) - * --------+----------------------------------------+-------------- - * local | def MAX=5; let buf:[MAX]u8; buf.len | 5 (was ww 0) - * global | def MAX=5; let g:[MAX]u8; g.len | 5 (was ww 0) - * defarr | def MAX=5; def A:[MAX]u8; A.len | 5 (was ww 0) - * sum | local + global + def .len (one fn) | 15 - */ -#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[] = { - { "local", - "package main;\n" - "def MAX: i32 = 5;\n" - "export fn main() i32 = {\n" - " let buf: [MAX]u8 = [1, 2, 3, 4, 5];\n" - " return buf.len: i32;\n" - "};\n", - 5 }, - - { "global", - "package main;\n" - "def MAX: i32 = 5;\n" - "let g: [MAX]u8 = [1, 2, 3, 4, 5];\n" - "export fn main() i32 = {\n" - " return g.len: i32;\n" - "};\n", - 5 }, - - { "defarr", - "package main;\n" - "def MAX: i32 = 5;\n" - "def A: [MAX]u8 = [1, 2, 3, 4, 5];\n" - "export fn main() i32 = {\n" - " return A.len: i32;\n" - "};\n", - 5 }, - - { "sum", - "package main;\n" - "def MAX: i32 = 5;\n" - "let g: [MAX]u8 = [1, 2, 3, 4, 5];\n" - "def A: [MAX]u8 = [1, 2, 3, 4, 5];\n" - "export fn main() i32 = {\n" - " let buf: [MAX]u8 = [1, 2, 3, 4, 5];\n" - " return (buf.len + g.len + A.len): i32;\n" - "};\n", - 15 }, -}; - -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/ddf_%d_%d.ww", getpid(), i); - snprintf(tmpdir, sizeof tmpdir, "/tmp/ddf_%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_field[cstage][%s]: build/run failed " - "(got %d)\n", rows[i].label, gc); - fail++; - continue; - } - if (gc != rows[i].want_exit) { - fprintf(stderr, "defdim_field[cstage][%s]: exit=%d want=%d\n", - rows[i].label, gc, rows[i].want_exit); - fail++; - } - if (!have_ww) { - fprintf(stderr, "defdim_field: skip wwstage (no %s)\n", wdrv); - continue; - } - int gw = run_build(wdrv, &rows[i], i); - if (gw != gc) { - fprintf(stderr, "defdim_field[%s]: cs=%d != ww=%d " - "(def-dim .len field-read divergence — #56)\n", - rows[i].label, gc, gw); - fail++; - } - if (gw != rows[i].want_exit) { - fprintf(stderr, "defdim_field[wwstage][%s]: exit=%d want=%d\n", - rows[i].label, gw, rows[i].want_exit); - fail++; - } - } - - if (fail) { - fprintf(stderr, "defdim_field_run: %d/%d checks failed\n", - fail, total); - return 1; - } - printf("defdim_field_run: %d/%d ok\n", total, total); - return 0; -} diff --git a/test/wcc/989_defdim_slice_run.c b/test/wcc/989_defdim_slice_run.c deleted file mode 100644 index 20effbcf..00000000 --- a/test/wcc/989_defdim_slice_run.c +++ /dev/null @@ -1,175 +0,0 @@ -/* - * 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; -}