diff --git a/Makefile b/Makefile index 916c92dc..8a847f8f 100644 --- a/Makefile +++ b/Makefile @@ -339,6 +339,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_types_intlim_run \ $(BIN)/test_opaque_decl_run \ $(BIN)/test_opaque_guards \ + $(BIN)/test_opaque_assign_cast_run \ $(BIN)/test_bufio_run $(BIN)/test_random_run $(BIN)/test_smoke: test/wcc/000_smoke.c $(LIB)/libwcc.a | $(BIN) @@ -1107,6 +1108,11 @@ $(BIN)/test_opaque_guards: test/wcc/961_opaque_guards.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_opaque_assign_cast_run: test/wcc/962_opaque_assign_cast_run.c \ + $(BIN)/ww $(BIN)/w6c $(BIN)/w6c_ww $(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/type.c b/cmd/wcc/type.c index 855827b0..8ed4cf0e 100644 --- a/cmd/wcc/type.c +++ b/cmd/wcc/type.c @@ -356,6 +356,33 @@ type_assignable(Type *dst, Type *src) return pa == NULL && pb == NULL; } + /* #108(c): opaque is a type-erasure sink. Any pointer is assignable + * to *opaque, and any slice to []opaque — the universal void-pointer + * and erased slice. harec type_is_assignable: ptr→*opaque at ref/harec/src/ + * types.c:1053 (`case STORAGE_OPAQUE: break;` inside the pointer arm, + * i.e. the referent need not match), slice→[]opaque at :1094 (`if + * (to_secondary->storage == STORAGE_OPAQUE) return true;`). + * + * Array→[]opaque (harec's STORAGE_POINTER-to-array and array→slice + * decay, types.c:1080-1099) is deliberately EXCLUDED: ww has no + * implicit array→slice conversion for any element type (`let s: + * []i32 = a` is rejected too — a slice is built only via an explicit + * `a[0:n]` op), so there is no array→slice-header cgen. Accepting + * array→[]opaque alone would assign a fat array local into a 24-byte + * slot with no decay — a silent miscompile (rule 7). sort's caller + * passes a slice, so slice→[]opaque is the only shape it needs. + * + * Both rules fire only when the destination element is opaque, so + * they are inert on the opaque-free selfhost corpus. */ + { + Type *du = (dst->kind == TY_NAMED) ? dst->under : dst; + Type *su = (src->kind == TY_NAMED) ? src->under : src; + if (du && su && du->kind == su->kind && + (du->kind == TY_PTR || du->kind == TY_SLICE) && + du->sub && du->sub->kind == TY_OPAQUE) + return 1; + } + return 0; } diff --git a/test/wcc/962_opaque_assign_cast_run.c b/test/wcc/962_opaque_assign_cast_run.c new file mode 100644 index 00000000..27f71cfb --- /dev/null +++ b/test/wcc/962_opaque_assign_cast_run.c @@ -0,0 +1,282 @@ +/* + * 962_opaque_assign_cast_run — runtime + byte-id proof of #108 sub-fold + * (c): opaque as a type-erasure sink. Two assignability rules + the + * reinterpret casts sort's implementation relies on. + * + * rule 1 `*T -> *opaque` IMPLICIT (no cast). Any pointer is the + * universal void-pointer. harec type_is_assignable, pointer + * arm: ref/harec/src/types.c:1053 (`case STORAGE_OPAQUE: + * break;` — the referent need not match). + * rule 2 `[]T -> []opaque` IMPLICIT (no cast). Any slice is the + * type-erased slice; the {ptr,len,cap} header is normal, + * the byte stride is supplied at runtime (itemsz). harec + * slice arm: types.c:1094 (`if (to_secondary->storage == + * STORAGE_OPAQUE) return true;`). + * casts `[]opaque -> *u8` (slice -> byte ptr; cgexpr leaves the + * ptr in AX, so the cast naturally takes .ptr) and + * `*opaque -> *u8` / `*opaque -> *i32` (ptr->ptr reinterpret, + * a no-op). drew described the Hare idiom as `*[*]u8`; ww has + * no unbounded-array `[*]`, so the ww-faithful reinterpret + * target is `*u8` + uintptr stride arithmetic. + * + * Rule-10 placement (per-rule, empirical): + * rules 1 & 2 are CSTAGE-ONLY. cstage type_assignable (cmd/wcc/ + * type.c) gained the opaque sink; the wwstage check.ww isassignable + * is a resolve-only AST approximation that returns "can't tell, stay + * quiet" (confident=false) for a ptr/slice whose element it cannot + * match, so it already ACCEPTS every form here (let-init AND call- + * arg). Verified empirically: w6c_ww compiles each row's source with + * exit 0, byte-identically to w6c (the cs==ww gate below). cstage + * rejected these before the type.c change. No ww twin is needed + * (same align-down precedent as 960/961's cstage-only arms). + * The casts are validation-free in BOTH stages (N_CAST never checks + * legality) and the reinterpret cgen needed no change — proven by + * the cs==ww byte-id gate. + * + * Array->[]opaque (harec's array->slice decay, types.c:1080-1099) is + * deliberately EXCLUDED: ww has no implicit array->slice conversion for + * any element type (`let s: []i32 = a` is rejected too — a slice is + * built only via an explicit `a[0:n]`), so there is no array->slice- + * header cgen. Accepting array->[]opaque alone would assign a fat + * array local into a 24-byte slot with no decay: a silent miscompile + * (rule 7). sort's caller passes a slice, so slice->[]opaque suffices. + * + * opaque is unused by the bootstrap, so the new rules fire only on + * opaque-typed operands — INERT on the selfhost corpus, 990-997 stay + * byte-identical. But that same inertness means the 990-997 gates + * never exercise opaque cs==ww; this test carries its own w6c-vs-w6c_ww + * byte-id gate (dimension (b)) to cover the rule-10 symmetry directly. + * + * Each row carries BOTH dimensions, like 953_f64crossmod_run: + * (a) cstage `ww build` + run, asserting the exit code — pins that + * the converged asm is runtime-correct (the type erasure round- + * trips: a value written/read through the opaque path reads back + * intact). + * (b) w6c vs w6c_ww `.s` cmp — FAILS if the stages diverge. + * + * NOTE — call results are bound to locals before any comparison, never + * compared inline (`if (f(x) != k)`). That inline-call-result-in- + * comparison shape is mis-compiled by a PRE-EXISTING cgen bug (#116 + * family: reproduced with zero opaque — a fn-call result compared + * inline when its pointer arg was produced by a prior call doing + * uintptr arithmetic). Binding first is the same dodge 960 uses; it is + * NOT a workaround for the opaque feature, which is exercised in full. + */ +#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; +} + +static int +slurp_eq(const char *a, const char *b) +{ + FILE *fa = fopen(a, "rb"); + FILE *fb = fopen(b, "rb"); + if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; } + int rc = 0; + for (;;) { + int ca = fgetc(fa); + int cb = fgetc(fb); + if (ca != cb) { rc = -1; break; } + if (ca == EOF) break; + } + fclose(fa); fclose(fb); + return rc; +} + +struct row { const char *label; const char *src; int want_exit; }; + +static const struct row rows[] = { + /* rule 1: `*T -> *opaque` IMPLICIT (no cast) at a let-init AND a + * call-arg. Round-trip a real *i32 through *opaque and back, deref. */ + { "rule1_implicit_ptr", + "package main;\n" + "fn readi32(p: *opaque) i32 = { let pi: *i32 = p: *i32; return *pi; };\n" + "export fn main() i32 = {\n" + " let n: i32 = 42;\n" + " let po: *opaque = &n;\n" /* let-init *i32 -> *opaque */ + " let v: i32 = readi32(po);\n" + " let v2: i32 = readi32(&n);\n" /* call-arg *i32 -> *opaque */ + " if (v != 42) { return 1; };\n" + " if (v2 != 42) { return 2; };\n" + " return v;\n" + "};\n", 42 }, + /* rule 2: `[]T -> []opaque` IMPLICIT (no cast) at a let-init AND a + * call-arg. Read .len, round-trip .ptr (a *opaque) back to *i32. */ + { "rule2_implicit_slice", + "package main;\n" + "fn slen(o: []opaque) i32 = { return o.len: i32; };\n" + "fn first(o: []opaque) i32 = { let p: *opaque = o.ptr; let pi: *i32 = p: *i32; return *pi; };\n" + "export fn main() i32 = {\n" + " let a: [4]i32 = [11, 22, 33, 44];\n" + " let s: []i32 = a[0:4];\n" + " let o: []opaque = s;\n" /* let-init []i32 -> []opaque */ + " let n: i32 = slen(o);\n" + " let n2: i32 = slen(s);\n" /* call-arg []i32 -> []opaque */ + " if (n != 4) { return 1; };\n" + " if (n2 != 4) { return 2; };\n" + " let f: i32 = first(s);\n" + " if (f != 11) { return 3; };\n" + " return n;\n" + "};\n", 4 }, + /* sort's actual usage end-to-end: a `fn(items: []opaque, itemsz: + * size)` called with a []i32; inside, reinterpret the slice as a + * byte base (`items: *u8`), uintptr-arith two element addresses, + * byte-swap them by itemsz. Then read back through the *opaque + * element path and through the original []i32 view — the type + * erasure round-trips iff both agree. Also a *opaque arg straight + * from a *i32 (rule 1). exit 0 == every assertion held. */ + { "sort_pattern", + "package main;\n" + "fn elemptr(items: []opaque, i: size, itemsz: size) *opaque = {\n" + " let base: *u8 = items: *u8;\n" /* []opaque -> *u8 (takes .ptr) */ + " let off: uintptr = (i * itemsz): uintptr;\n" + " return ((base: uintptr) + off): *opaque;\n" + "};\n" + "fn swap(items: []opaque, x: size, y: size, itemsz: size) void = {\n" + " let pa: *u8 = elemptr(items, x, itemsz): *u8;\n" + " let pb: *u8 = elemptr(items, y, itemsz): *u8;\n" + " let k: size = 0;\n" + " for (k < itemsz) {\n" + " let qa: *u8 = ((pa: uintptr) + k: uintptr): *u8;\n" + " let qb: *u8 = ((pb: uintptr) + k: uintptr): *u8;\n" + " let t: u8 = *qa;\n" + " *qa = *qb;\n" + " *qb = t;\n" + " k = k + 1;\n" + " };\n" + "};\n" + "fn readi32(p: *opaque) i32 = { let pi: *i32 = p: *i32; return *pi; };\n" + "export fn main() i32 = {\n" + " let a: [4]i32 = [10, 20, 30, 40];\n" + " let s: []i32 = a[0:4];\n" + " swap(s, 0: size, 3: size, size(i32));\n" /* []i32 -> []opaque call-arg */ + " if (a[0] != 40) { return 1; };\n" /* erased swap round-trips */ + " if (a[3] != 10) { return 2; };\n" + " let p0: *opaque = elemptr(s, 0: size, size(i32));\n" + " let v0: i32 = readi32(p0);\n" + " if (v0 != 40) { return 3; };\n" + " let nn: i32 = 77;\n" + " let pn: *opaque = &nn;\n" /* *i32 -> *opaque let-init */ + " let vn: i32 = readi32(pn);\n" + " if (vn != 77) { return 4; };\n" + " let o: []opaque = s;\n" + " let ol: i32 = o.len: i32;\n" + " if (ol != 4) { return 5; };\n" + " return 0;\n" + "};\n", 0 }, + { NULL, NULL, 0 } +}; + +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 w6c[1100], w6c_ww[1100]; + snprintf(w6c, sizeof w6c, "%s/w6c", bin); + snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin); + if (access(w6c_ww, X_OK) != 0) { + fprintf(stderr, "opaque_assign_cast: w6c_ww missing — cannot " + "run the cs==ww byte-id gate (the rule-10 proof)\n"); + return 1; + } + + int n = 0, fail = 0; + for (int i = 0; rows[i].src; i++, n++) { + char src[64]; + snprintf(src, sizeof src, "/tmp/wwopqc_%d_%d.ww", getpid(), i); + FILE *f = fopen(src, "wb"); + if (f == NULL) { fail++; continue; } + fputs(rows[i].src, f); + fclose(f); + + /* (a) cstage build + run in a scratch dir. */ + char tmpdir[64]; + snprintf(tmpdir, sizeof tmpdir, "/tmp/wwopqc_%d_d_%d", + getpid(), i); + mkdir(tmpdir, 0755); + + char cmd[2048]; + snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s", + tmpdir, bin, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: cstage build failed\n", + rows[i].label); + 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[%s]: cstage exit %d, want %d\n", + rows[i].label, got, rows[i].want_exit); + fail++; + } + unlink(outbin); rmdir(tmpdir); + + /* (b) cs==ww byte-id gate: emit .s from both stages, cmp. */ + char cs_s[64], ws_s[64]; + snprintf(cs_s, sizeof cs_s, "/tmp/wwopqc_%d_%d_cs.s", + getpid(), i); + snprintf(ws_s, sizeof ws_s, "/tmp/wwopqc_%d_%d_ww.s", + getpid(), i); + + snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", + w6c, cs_s, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label); + fail++; unlink(src); continue; + } + snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", + w6c_ww, ws_s, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: w6c_ww failed\n", + rows[i].label); + fail++; unlink(src); unlink(cs_s); continue; + } + if (slurp_eq(cs_s, ws_s) != 0) { + fprintf(stderr, + "row[%s]: cstage/wwstage .s DIFFER (rule-10 " + "byte-id violation)\n", rows[i].label); + fail++; + } + unlink(src); unlink(cs_s); unlink(ws_s); + } + + if (fail) { + fprintf(stderr, "%d/%d opaque assign/cast tests failed\n", + fail, n); + return 1; + } + printf("opaque_assign_cast: %d/%d ok (cstage run + cs==ww byte-id)\n", + n, n); + return 0; +}