diff --git a/Makefile b/Makefile index 1e4eac99..f38d3010 100644 --- a/Makefile +++ b/Makefile @@ -333,6 +333,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_f64xmm_run \ $(BIN)/test_tuprecv_f64_run \ $(BIN)/test_floats_run \ + $(BIN)/test_size_type_run \ $(BIN)/test_bufio_run $(BIN)/test_random_run $(BIN)/test_smoke: test/wcc/000_smoke.c $(LIB)/libwcc.a | $(BIN) @@ -1074,6 +1075,10 @@ $(BIN)/test_floats_run: test/wcc/952_floats_run.c $(BIN)/ww $(BIN)/w6c \ $(BIN)/w6a $(BIN)/w6l $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(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_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/cmd/wcc/check.c b/cmd/wcc/check.c index 19c4d34e..40ebef79 100644 --- a/cmd/wcc/check.c +++ b/cmd/wcc/check.c @@ -49,6 +49,7 @@ lookup_builtin(const char *name) if (strcmp(name, "int") == 0) return ty_int; if (strcmp(name, "uint") == 0) return ty_uint; if (strcmp(name, "uintptr") == 0) return ty_uintptr; + if (strcmp(name, "size") == 0) return ty_size; /* #85 fold-2 */ if (strcmp(name, "f32") == 0) return ty_f32; if (strcmp(name, "f64") == 0) return ty_f64; if (strcmp(name, "str") == 0) return ty_str; diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index e4a3c687..5a812483 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -7836,7 +7836,7 @@ fn primtypesize(nm: str) i64 = { if (streq(nm, "i16") || streq(nm, "u16")) { return 2i64; }; if (streq(nm, "i32") || streq(nm, "u32") || streq(nm, "f32") || streq(nm, "rune")) { return 4i64; }; if (streq(nm, "i64") || streq(nm, "u64") || streq(nm, "f64")) { return 8i64; }; - if (streq(nm, "int") || streq(nm, "uint") || streq(nm, "uintptr")) { return 8i64; }; + if (streq(nm, "int") || streq(nm, "uint") || streq(nm, "uintptr") || streq(nm, "size")) { return 8i64; }; // str IS []u8: 24B, sourced from the slice header SSoT so str and // []u8 can never drift; no second hardcoded 24 (#1/Phase 3). if (streq(nm, "str")) { return tyslicesize(); }; @@ -7895,7 +7895,7 @@ fn astalign(c: *checker, t: *node) i64 = { if (streq(nm, "void") || streq(nm, "bool") || streq(nm, "i8") || streq(nm, "u8")) { return 1i64; }; if (streq(nm, "i16") || streq(nm, "u16")) { return 2i64; }; if (streq(nm, "i32") || streq(nm, "u32") || streq(nm, "f32") || streq(nm, "rune")) { return 4i64; }; - if (streq(nm, "i64") || streq(nm, "u64") || streq(nm, "f64") || streq(nm, "int") || streq(nm, "uint") || streq(nm, "uintptr") || streq(nm, "str")) { return 8i64; }; + if (streq(nm, "i64") || streq(nm, "u64") || streq(nm, "f64") || streq(nm, "int") || streq(nm, "uint") || streq(nm, "uintptr") || streq(nm, "size") || streq(nm, "str")) { return 8i64; }; let resolved: *node = resolvealias(c, t); if (resolved != nil && resolved != t) { return astalign(c, resolved); @@ -8421,6 +8421,7 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { if (streq(nm, "int")) { r = c.tc.tyint; }; if (streq(nm, "uint")) { r = c.tc.tyuint; }; if (streq(nm, "uintptr")) { r = c.tc.tyuintptr; }; + if (streq(nm, "size")) { r = c.tc.tysize; }; // #85 fold-2 if (streq(nm, "f32")) { r = c.tc.tyf32; }; if (streq(nm, "f64")) { r = c.tc.tyf64; }; if (streq(nm, "str")) { r = c.tc.tystr; }; @@ -9713,6 +9714,7 @@ fn isinttypeast(t: *node) bool = { if (streq(s, "int")) { return true; }; if (streq(s, "uint")) { return true; }; if (streq(s, "uintptr")) { return true; }; + if (streq(s, "size")) { return true; }; if (streq(s, "rune")) { return true; }; return false; }; @@ -9732,6 +9734,7 @@ fn isnumerictname(t: *node) bool = { if (streq(s, "int")) { return true; }; if (streq(s, "uint")) { return true; }; if (streq(s, "uintptr")) { return true; }; + if (streq(s, "size")) { return true; }; if (streq(s, "rune")) { return true; }; if (streq(s, "f32")) { return true; }; if (streq(s, "f64")) { return true; }; @@ -11986,6 +11989,7 @@ fn primsize(name: str) i32 = { if (streq(name, "uint")) { return 8; }; if (streq(name, "int")) { return 8; }; if (streq(name, "uintptr")) { return 8; }; + if (streq(name, "size")) { return 8; }; if (streq(name, "f64")) { return 8; }; if (streq(name, "rune")) { return 4; }; if (streq(name, "void")) { return 0; }; @@ -23454,6 +23458,7 @@ fn letscalarprim(nm: str) bool = { if (streq(nm, "int")) { return true; }; if (streq(nm, "uint")) { return true; }; if (streq(nm, "uintptr")) { return true; }; + if (streq(nm, "size")) { return true; }; return false; }; diff --git a/selfhost/cmd/wcc/cgen.ww b/selfhost/cmd/wcc/cgen.ww index d275445a..a4a57a46 100644 --- a/selfhost/cmd/wcc/cgen.ww +++ b/selfhost/cmd/wcc/cgen.ww @@ -904,6 +904,7 @@ fn letscalarprim(nm: str) bool = { if (streq(nm, "int")) { return true; }; if (streq(nm, "uint")) { return true; }; if (streq(nm, "uintptr")) { return true; }; + if (streq(nm, "size")) { return true; }; return false; }; diff --git a/selfhost/cmd/wcc/cgenutil.ww b/selfhost/cmd/wcc/cgenutil.ww index c5f51d21..404302c0 100644 --- a/selfhost/cmd/wcc/cgenutil.ww +++ b/selfhost/cmd/wcc/cgenutil.ww @@ -1248,6 +1248,7 @@ fn primsize(name: str) i32 = { if (streq(name, "uint")) { return 8; }; if (streq(name, "int")) { return 8; }; if (streq(name, "uintptr")) { return 8; }; + if (streq(name, "size")) { return 8; }; if (streq(name, "f64")) { return 8; }; if (streq(name, "rune")) { return 4; }; if (streq(name, "void")) { return 0; }; diff --git a/selfhost/cmd/wcc/check.ww b/selfhost/cmd/wcc/check.ww index aa6e3e28..24109230 100644 --- a/selfhost/cmd/wcc/check.ww +++ b/selfhost/cmd/wcc/check.ww @@ -713,7 +713,7 @@ fn primtypesize(nm: str) i64 = { if (streq(nm, "i16") || streq(nm, "u16")) { return 2i64; }; if (streq(nm, "i32") || streq(nm, "u32") || streq(nm, "f32") || streq(nm, "rune")) { return 4i64; }; if (streq(nm, "i64") || streq(nm, "u64") || streq(nm, "f64")) { return 8i64; }; - if (streq(nm, "int") || streq(nm, "uint") || streq(nm, "uintptr")) { return 8i64; }; + if (streq(nm, "int") || streq(nm, "uint") || streq(nm, "uintptr") || streq(nm, "size")) { return 8i64; }; // str IS []u8: 24B, sourced from the slice header SSoT so str and // []u8 can never drift; no second hardcoded 24 (#1/Phase 3). if (streq(nm, "str")) { return tyslicesize(); }; @@ -772,7 +772,7 @@ fn astalign(c: *checker, t: *node) i64 = { if (streq(nm, "void") || streq(nm, "bool") || streq(nm, "i8") || streq(nm, "u8")) { return 1i64; }; if (streq(nm, "i16") || streq(nm, "u16")) { return 2i64; }; if (streq(nm, "i32") || streq(nm, "u32") || streq(nm, "f32") || streq(nm, "rune")) { return 4i64; }; - if (streq(nm, "i64") || streq(nm, "u64") || streq(nm, "f64") || streq(nm, "int") || streq(nm, "uint") || streq(nm, "uintptr") || streq(nm, "str")) { return 8i64; }; + if (streq(nm, "i64") || streq(nm, "u64") || streq(nm, "f64") || streq(nm, "int") || streq(nm, "uint") || streq(nm, "uintptr") || streq(nm, "size") || streq(nm, "str")) { return 8i64; }; let resolved: *node = resolvealias(c, t); if (resolved != nil && resolved != t) { return astalign(c, resolved); @@ -1298,6 +1298,7 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { if (streq(nm, "int")) { r = c.tc.tyint; }; if (streq(nm, "uint")) { r = c.tc.tyuint; }; if (streq(nm, "uintptr")) { r = c.tc.tyuintptr; }; + if (streq(nm, "size")) { r = c.tc.tysize; }; // #85 fold-2 if (streq(nm, "f32")) { r = c.tc.tyf32; }; if (streq(nm, "f64")) { r = c.tc.tyf64; }; if (streq(nm, "str")) { r = c.tc.tystr; }; @@ -2590,6 +2591,7 @@ fn isinttypeast(t: *node) bool = { if (streq(s, "int")) { return true; }; if (streq(s, "uint")) { return true; }; if (streq(s, "uintptr")) { return true; }; + if (streq(s, "size")) { return true; }; if (streq(s, "rune")) { return true; }; return false; }; @@ -2609,6 +2611,7 @@ fn isnumerictname(t: *node) bool = { if (streq(s, "int")) { return true; }; if (streq(s, "uint")) { return true; }; if (streq(s, "uintptr")) { return true; }; + if (streq(s, "size")) { return true; }; if (streq(s, "rune")) { return true; }; if (streq(s, "f32")) { return true; }; if (streq(s, "f64")) { return true; }; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 75f2ac1e..68ab61df 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -7836,7 +7836,7 @@ fn primtypesize(nm: str) i64 = { if (streq(nm, "i16") || streq(nm, "u16")) { return 2i64; }; if (streq(nm, "i32") || streq(nm, "u32") || streq(nm, "f32") || streq(nm, "rune")) { return 4i64; }; if (streq(nm, "i64") || streq(nm, "u64") || streq(nm, "f64")) { return 8i64; }; - if (streq(nm, "int") || streq(nm, "uint") || streq(nm, "uintptr")) { return 8i64; }; + if (streq(nm, "int") || streq(nm, "uint") || streq(nm, "uintptr") || streq(nm, "size")) { return 8i64; }; // str IS []u8: 24B, sourced from the slice header SSoT so str and // []u8 can never drift; no second hardcoded 24 (#1/Phase 3). if (streq(nm, "str")) { return tyslicesize(); }; @@ -7895,7 +7895,7 @@ fn astalign(c: *checker, t: *node) i64 = { if (streq(nm, "void") || streq(nm, "bool") || streq(nm, "i8") || streq(nm, "u8")) { return 1i64; }; if (streq(nm, "i16") || streq(nm, "u16")) { return 2i64; }; if (streq(nm, "i32") || streq(nm, "u32") || streq(nm, "f32") || streq(nm, "rune")) { return 4i64; }; - if (streq(nm, "i64") || streq(nm, "u64") || streq(nm, "f64") || streq(nm, "int") || streq(nm, "uint") || streq(nm, "uintptr") || streq(nm, "str")) { return 8i64; }; + if (streq(nm, "i64") || streq(nm, "u64") || streq(nm, "f64") || streq(nm, "int") || streq(nm, "uint") || streq(nm, "uintptr") || streq(nm, "size") || streq(nm, "str")) { return 8i64; }; let resolved: *node = resolvealias(c, t); if (resolved != nil && resolved != t) { return astalign(c, resolved); @@ -8421,6 +8421,7 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { if (streq(nm, "int")) { r = c.tc.tyint; }; if (streq(nm, "uint")) { r = c.tc.tyuint; }; if (streq(nm, "uintptr")) { r = c.tc.tyuintptr; }; + if (streq(nm, "size")) { r = c.tc.tysize; }; // #85 fold-2 if (streq(nm, "f32")) { r = c.tc.tyf32; }; if (streq(nm, "f64")) { r = c.tc.tyf64; }; if (streq(nm, "str")) { r = c.tc.tystr; }; @@ -9713,6 +9714,7 @@ fn isinttypeast(t: *node) bool = { if (streq(s, "int")) { return true; }; if (streq(s, "uint")) { return true; }; if (streq(s, "uintptr")) { return true; }; + if (streq(s, "size")) { return true; }; if (streq(s, "rune")) { return true; }; return false; }; @@ -9732,6 +9734,7 @@ fn isnumerictname(t: *node) bool = { if (streq(s, "int")) { return true; }; if (streq(s, "uint")) { return true; }; if (streq(s, "uintptr")) { return true; }; + if (streq(s, "size")) { return true; }; if (streq(s, "rune")) { return true; }; if (streq(s, "f32")) { return true; }; if (streq(s, "f64")) { return true; }; @@ -11986,6 +11989,7 @@ fn primsize(name: str) i32 = { if (streq(name, "uint")) { return 8; }; if (streq(name, "int")) { return 8; }; if (streq(name, "uintptr")) { return 8; }; + if (streq(name, "size")) { return 8; }; if (streq(name, "f64")) { return 8; }; if (streq(name, "rune")) { return 4; }; if (streq(name, "void")) { return 0; }; @@ -23454,6 +23458,7 @@ fn letscalarprim(nm: str) bool = { if (streq(nm, "int")) { return true; }; if (streq(nm, "uint")) { return true; }; if (streq(nm, "uintptr")) { return true; }; + if (streq(nm, "size")) { return true; }; return false; }; diff --git a/test/wcc/957_size_type_run.c b/test/wcc/957_size_type_run.c new file mode 100644 index 00000000..05d643e6 --- /dev/null +++ b/test/wcc/957_size_type_run.c @@ -0,0 +1,137 @@ +/* + * 957_size_type_run — runtime proof that the `size` type name is usable + * in TYPE position (#85 fold-2) and that it coexists with the `size(T)` + * size-of OPERATOR. The two are different paths — TYPE-position `size` + * resolves via the type-name resolver (lookup_builtin / tinfofornode's + * N_TNAME chain), while `size(T)` is a c.top SK_FN seed folded to an + * N_INTLIT — so a program may use BOTH in the same scope with no clash. + * Each row below exercises that coexistence; without the fold-2 resolver + * arm the `let _: size` rows fail to compile ("unknown type 'size'"), so + * a green row IS the proof the name binds. + * + * `size` mirrors uintptr: platform-width (8B amd64) unsigned int. The + * rows assert it is usable in arithmetic, comparison, and an i32 cast. + * + * Table-driven like 952_floats_run: each row is a self-contained ww + * program; the C-side cstage `ww build -I lib` compiles it, we run the + * binary and assert the exit code. cstage-only by design (mirrors 951/ + * 952/969): `ww_ww run` is broken (#95) and per-program wwstage byte-id + * is the 990-997 gates' job; the wwstage resolver arm is twinned for + * rule-10 symmetry and stays dead on the size-free selfhost corpus. + */ +#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[] = { + /* type-position `size` + the size(T) operator in one scope. x is a + * size-typed local; size(int)/size(i32) are folded operator results. + * Both must resolve and agree (8/4 on amd64). */ + { "package main;\n" + "export fn main() i32 = {\n" + " let x: size = 0;\n" + " if (x != 0) { return 1; };\n" + " let y: size = 42;\n" + " if (y != 42) { return 2; };\n" + " if (size(int) != 8) { return 3; };\n" + " if (size(i32) != 4) { return 4; };\n" + " return 0;\n" + "};\n", 0 }, + /* size arithmetic + i32 cast value propagation: a size-typed sum + * truncates to i32 and propagates (40 + 2 == 42). */ + { "package main;\n" + "export fn main() i32 = {\n" + " let x: size = 40;\n" + " let y: size = 2;\n" + " return (x + y): i32;\n" + "};\n", 42 }, + /* size as a struct field type: 8-byte slot, written and read back. */ + { "package main;\n" + "type box = struct { n: size };\n" + "export fn main() i32 = {\n" + " let b: box = box{ n = 7 };\n" + " return b.n: i32;\n" + "};\n", 7 }, + /* the size(T) operator value-propagated through an i32 cast (operator + * path, must keep working alongside the new type-name arm). */ + { "package main;\n" + "export fn main() i32 = {\n" + " return size(int): i32;\n" + "};\n", 8 }, + { 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/wwsz_%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/wwsz_%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 size_type tests failed\n", fail, n); + return 1; + } + printf("size_type: %d/%d ok\n", n, n); + return 0; +}