diff --git a/Makefile b/Makefile index cf551678..23b56280 100644 --- a/Makefile +++ b/Makefile @@ -335,6 +335,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_tuprecv_f64_run \ $(BIN)/test_floats_run \ $(BIN)/test_size_type_run \ + $(BIN)/test_types_sizelim_run \ $(BIN)/test_bufio_run $(BIN)/test_random_run $(BIN)/test_smoke: test/wcc/000_smoke.c $(LIB)/libwcc.a | $(BIN) @@ -1086,6 +1087,10 @@ $(BIN)/test_size_type_run: test/wcc/957_size_type_run.c $(BIN)/ww $(BIN)/w6c \ $(BIN)/w6a $(BIN)/w6l $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_types_sizelim_run: test/wcc/958_types_sizelim_run.c $(BIN)/ww \ + $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_f64crossmod_run: test/wcc/953_f64crossmod_run.c $(BIN)/ww \ $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \ $(LIB)/libwwrt.a | $(BIN) diff --git a/lib/types/types.ww b/lib/types/types.ww index daa16f01..b38caf87 100644 --- a/lib/types/types.ww +++ b/lib/types/types.ww @@ -24,4 +24,12 @@ def U16_MIN: u16 = 0; def U32_MIN: u32 = 0; def U64_MIN: u64 = 0; +// size is 8B on amd64; no cast needed (size ∈ unsigned class per #113). +def SIZE_MIN: size = U64_MIN; +def SIZE_MAX: size = U64_MAX; + +// uintptr not in the unsigned class, so the cast is required (Hare's form). +def UINTPTR_MIN: uintptr = U64_MIN: uintptr; +def UINTPTR_MAX: uintptr = U64_MAX: uintptr; + def RUNE_MIN: rune = '\0'; diff --git a/selfhost/cmd/w6a/main.combined.ww b/selfhost/cmd/w6a/main.combined.ww index b899ee9b..e340f5f0 100644 --- a/selfhost/cmd/w6a/main.combined.ww +++ b/selfhost/cmd/w6a/main.combined.ww @@ -776,6 +776,14 @@ def U16_MIN: u16 = 0; def U32_MIN: u32 = 0; def U64_MIN: u64 = 0; +// size is 8B on amd64; no cast needed (size ∈ unsigned class per #113). +def SIZE_MIN: size = U64_MIN; +def SIZE_MAX: size = U64_MAX; + +// uintptr not in the unsigned class, so the cast is required (Hare's form). +def UINTPTR_MIN: uintptr = U64_MIN: uintptr; +def UINTPTR_MAX: uintptr = U64_MAX: uintptr; + def RUNE_MIN: rune = '\0'; // bytes — slice operations over []u8. Mirrors Hare's bytes module diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 5a812483..e82cebad 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -776,6 +776,14 @@ def U16_MIN: u16 = 0; def U32_MIN: u32 = 0; def U64_MIN: u64 = 0; +// size is 8B on amd64; no cast needed (size ∈ unsigned class per #113). +def SIZE_MIN: size = U64_MIN; +def SIZE_MAX: size = U64_MAX; + +// uintptr not in the unsigned class, so the cast is required (Hare's form). +def UINTPTR_MIN: uintptr = U64_MIN: uintptr; +def UINTPTR_MAX: uintptr = U64_MAX: uintptr; + def RUNE_MIN: rune = '\0'; // bytes — slice operations over []u8. Mirrors Hare's bytes module diff --git a/selfhost/cmd/w6l/main.combined.ww b/selfhost/cmd/w6l/main.combined.ww index 90daa61d..ec497b7d 100644 --- a/selfhost/cmd/w6l/main.combined.ww +++ b/selfhost/cmd/w6l/main.combined.ww @@ -885,6 +885,14 @@ def U16_MIN: u16 = 0; def U32_MIN: u32 = 0; def U64_MIN: u64 = 0; +// size is 8B on amd64; no cast needed (size ∈ unsigned class per #113). +def SIZE_MIN: size = U64_MIN; +def SIZE_MAX: size = U64_MAX; + +// uintptr not in the unsigned class, so the cast is required (Hare's form). +def UINTPTR_MIN: uintptr = U64_MIN: uintptr; +def UINTPTR_MAX: uintptr = U64_MAX: uintptr; + def RUNE_MIN: rune = '\0'; // bytes — slice operations over []u8. Mirrors Hare's bytes module diff --git a/selfhost/cmd/ww/main.combined.ww b/selfhost/cmd/ww/main.combined.ww index a43bb127..259f141e 100644 --- a/selfhost/cmd/ww/main.combined.ww +++ b/selfhost/cmd/ww/main.combined.ww @@ -776,6 +776,14 @@ def U16_MIN: u16 = 0; def U32_MIN: u32 = 0; def U64_MIN: u64 = 0; +// size is 8B on amd64; no cast needed (size ∈ unsigned class per #113). +def SIZE_MIN: size = U64_MIN; +def SIZE_MAX: size = U64_MAX; + +// uintptr not in the unsigned class, so the cast is required (Hare's form). +def UINTPTR_MIN: uintptr = U64_MIN: uintptr; +def UINTPTR_MAX: uintptr = U64_MAX: uintptr; + def RUNE_MIN: rune = '\0'; // bytes — slice operations over []u8. Mirrors Hare's bytes module diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 68ab61df..7e78d5f2 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -753,6 +753,14 @@ def U16_MIN: u16 = 0; def U32_MIN: u32 = 0; def U64_MIN: u64 = 0; +// size is 8B on amd64; no cast needed (size ∈ unsigned class per #113). +def SIZE_MIN: size = U64_MIN; +def SIZE_MAX: size = U64_MAX; + +// uintptr not in the unsigned class, so the cast is required (Hare's form). +def UINTPTR_MIN: uintptr = U64_MIN: uintptr; +def UINTPTR_MAX: uintptr = U64_MAX: uintptr; + def RUNE_MIN: rune = '\0'; // bytes — slice operations over []u8. Mirrors Hare's bytes module diff --git a/selfhost/test/smoke.combined.ww b/selfhost/test/smoke.combined.ww index 2054f01b..731d0df6 100644 --- a/selfhost/test/smoke.combined.ww +++ b/selfhost/test/smoke.combined.ww @@ -753,6 +753,14 @@ def U16_MIN: u16 = 0; def U32_MIN: u32 = 0; def U64_MIN: u64 = 0; +// size is 8B on amd64; no cast needed (size ∈ unsigned class per #113). +def SIZE_MIN: size = U64_MIN; +def SIZE_MAX: size = U64_MAX; + +// uintptr not in the unsigned class, so the cast is required (Hare's form). +def UINTPTR_MIN: uintptr = U64_MIN: uintptr; +def UINTPTR_MAX: uintptr = U64_MAX: uintptr; + def RUNE_MIN: rune = '\0'; // bytes — slice operations over []u8. Mirrors Hare's bytes module diff --git a/test/wcc/958_types_sizelim_run.c b/test/wcc/958_types_sizelim_run.c new file mode 100644 index 00000000..7b1593c2 --- /dev/null +++ b/test/wcc/958_types_sizelim_run.c @@ -0,0 +1,146 @@ +/* + * 958_types_sizelim_run — runtime proof of the lib/types SIZE_* and + * UINTPTR_* limit constants (faithful port of ref/hare/types/arch+ + * x86_64.ha:16-26). Asserts both the values (MIN==0, MAX==U64_MAX) and + * that the consts are usable as their declared type in arithmetic and an + * i32 cast. + * + * SIZE_MAX is the no-cast def `def SIZE_MAX: size = U64_MAX;` — size is in + * the unsigned class and 8B-wide on amd64, so the u64→size init coerces + * without a cast (#113). UINTPTR_MAX keeps Hare's explicit cast + * (`U64_MAX: uintptr`) because uintptr is outside the unsigned class. A + * green row IS the proof both forms compiled and carry the right value. + * + * INT_MIN/MAX + UINT_MIN/MAX are deferred to #114 (ww int=8B vs Hare's 4B + * on amd64 makes the value question open); RUNE_MAX is deferred to #112 + * (no \U rune-escape lexer yet). Only the 4 SIZE/UINTPTR consts land here. + * + * Table-driven like 957_size_type_run / 952_floats_run: each row is a + * self-contained ww program; the cstage `ww build -I lib` compiles it, we + * run the binary and assert the exit code. cstage-only by design (mirrors + * 951/952/957): per-program wwstage byte-id is the 990-997 gates' job. + */ +#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 *src; int want_exit; }; + +static const struct row rows[] = { + /* size limits: SIZE_MIN is zero; SIZE_MAX equals U64_MAX both by + * direct compare and by the all-ones wrap (MAX + 1 == 0 on 8B). */ + { "package main;\n" + "import types;\n" + "export fn main() i32 = {\n" + " let lo: size = types.SIZE_MIN;\n" + " if (lo != 0) { return 1; };\n" + " let hi: size = types.SIZE_MAX;\n" + " if (hi != (types.U64_MAX): size) { return 2; };\n" + " if (hi + 1 != 0) { return 3; };\n" + " return 0;\n" + "};\n", 0 }, + /* SIZE_MAX usable in size arithmetic, propagated through an i32 + * cast: (2^64-1) + 43 wraps to 42. */ + { "package main;\n" + "import types;\n" + "export fn main() i32 = {\n" + " let hi: size = types.SIZE_MAX;\n" + " let v: size = hi + 43;\n" + " return v: i32;\n" + "};\n", 42 }, + /* uintptr limits: UINTPTR_MIN is zero; UINTPTR_MAX equals U64_MAX + * (Hare's explicit-cast def) both by compare and by the wrap. */ + { "package main;\n" + "import types;\n" + "export fn main() i32 = {\n" + " let lo: uintptr = types.UINTPTR_MIN;\n" + " if (lo != 0) { return 1; };\n" + " let hi: uintptr = types.UINTPTR_MAX;\n" + " if (hi != (types.U64_MAX): uintptr) { return 2; };\n" + " if (hi + 1 != 0) { return 3; };\n" + " return 0;\n" + "};\n", 0 }, + /* UINTPTR_MAX usable in uintptr arithmetic, propagated through an + * i32 cast: (2^64-1) + 43 wraps to 42. */ + { "package main;\n" + "import types;\n" + "export fn main() i32 = {\n" + " let hi: uintptr = types.UINTPTR_MAX;\n" + " let v: uintptr = hi + 43;\n" + " return v: i32;\n" + "};\n", 42 }, + { NULL, 0 } +}; + +int +main(void) +{ + const char *bin = getenv("BIN"); + if (!bin) bin = "out/bin"; + char cwd[1024]; + if (getcwd(cwd, sizeof cwd) == NULL) return 1; + char absbin[1024]; + if (bin[0] != '/') { + snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); + bin = absbin; + } + + int n = 0, fail = 0; + for (int i = 0; rows[i].src; i++, n++) { + char src[64]; + snprintf(src, sizeof src, "/tmp/wwslim_%d_%d.ww", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (f == NULL) { fail++; continue; } + fputs(rows[i].src, f); + fclose(f); + + char tmpdir[64]; + snprintf(tmpdir, sizeof tmpdir, "/tmp/wwslim_%d_d_%d", getpid(), i); + mkdir(tmpdir, 0755); + + char cmd[2048]; + snprintf(cmd, sizeof cmd, "cd %s && %s/ww build -I %s/lib %s", + tmpdir, bin, cwd, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row %d: build failed\n src: %s\n", + i, rows[i].src); + fail++; + unlink(src); rmdir(tmpdir); + continue; + } + + char outbin[128]; + const char *base = strrchr(src, '/'); + base = base ? base + 1 : src; + snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); + char *dot = strrchr(outbin, '.'); + if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; + + int got = runwait(outbin); + if (got != rows[i].want_exit) { + fprintf(stderr, "row %d: exit %d, want %d\n src: %s\n", + i, got, rows[i].want_exit, rows[i].src); + fail++; + } + unlink(src); unlink(outbin); rmdir(tmpdir); + } + if (fail) { + fprintf(stderr, "%d/%d types_sizelim tests failed\n", fail, n); + return 1; + } + printf("types_sizelim: %d/%d ok\n", n, n); + return 0; +}