From 1cc663f494e255c571d4ddc11783b32420582faf Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Sat, 6 Jun 2026 02:15:14 +0900 Subject: [PATCH] =?UTF-8?q?cgen:=20B5-c1=20helper+funnel=20single=20peels?= =?UTF-8?q?=20fold=20into=20type=5Fchase=5Fnamed=20=E2=80=94=2019=20sites,?= =?UTF-8?q?=20cs-only?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The exact F2b c1 set (rob next-arc spec + B5 re-rule): node_tuplearg:249, fld_issigned:409, castsrcprim:501/:531, struct_float_class:598, tagged_arg_size:640, tagged_memarg_size:661, type_isnullable:740, nullable_ptr_tag:750, cg_tagged_success_tag:860, cg_variant_is_error:876, cg_tag_for_variant:899, type_istagged:953, type_unwrap:1269 + the widen/ fill funnel entries cg_widen_tagged_store:2456/:2480/:2483, cg_widen_tagged_push:2905, cg_structlit_fill:3195. Raw `->under` in cgen.c 88→69. Riding per re-rule R1: peel-ok-#218 annotations at cg_variant_match/cg_variant_struct_match (citing ken's b5 oracle §4 — chasing those four peels graduates zero v2_struct rows; the real fix is a both-stage NAMED-source arm, task #95) and the :755 peel-ok annotation mirroring ww cgenutil.ww:2758 (probe-cleared, 018ef66). :447 untouched (c2's grant). TRAIN INVARIANT: cs-only — zero selfhost/ or lib/ bytes move; w6c_ww/ ww_ww/w6a_ww/w6l_ww bit-identical to the bcd948d baselines (md5 28ad889042bad8006f1997cbcec94805 / 4e9ca8741f19e1f68219ff799a5e5a14). cs movers bounded to exactly: kb5_targ, kb5_tmem, kb5_wpush, kb5_null, kb5_f32p, kb5_fill2, kb5_tuparg_c; five selfhost mains + the rest of the kb2/kb3/kb4/kb5/kna corpus byte-NEUTRAL both stages. LIVE graduations: kb5_targ (tagged_arg_size sized a 2-level alias union param 0 → wrong arg path, cs silent exit 1) and kb5_tmem (>48B memarg twin) → 0/0 byte-id. Divergence flips to byte-id: wpush/null/f32p. #85 CLOSES as SITE-CLOSURE with ZERO live graduations: type_unwrap's two consumers (:14716/:14907, both tuple-global layout walks that want the chased view) are checker-DEAD on cs for alias tuples (#86 upstream) — correctness there is by-construction, pinned by tupglobal_bound86. DESIGNED DIVERGENCE (re-rule R4, task #100): the :3195 chase flips kb5_fill2 from both-wrong-IDENTICAL-silent (gate-blind, both stages accepted and ran wrong byte-identically) to cs-LOUD / ww-silent-wrong. A loud, disclosed, pinned divergence over a silent miscompile; rejected programs emit no asm so the byte-id gates hold. Dual-cell pin (fill2_bound100): cs experr + ww run-exit-1 both asserted; fill0 both-loud control holds. #100 (the ww twin gate) fires immediately after B5 so the window is one train wide. Oracle corrections at the c1 boundary (ken c1-BOUNDARY ADDENDUM, verified on his independent scratch build; rob ack'd, scope unchanged): C1-CORR-1: kb5_succ does NOT flip here — its residual divergence is exactly three paired return-position tag syntheses, the cgreturn return-route family (:12278/:12318/:12322). Joins c3's graduation set; pinned succ_bound_c3 K_RUN_NOID until then. C1-CORR-2 (corrects re-rule R2): kb5_wstore_a does NOT flip — the ident-lhs N_ASSIGN tagged store gates in the cgexpr INLINE set (B6), never reaching the :2456 funnel; cs byte-neutral here. Pinned wstore_a_bound_b6 K_RUN_NOID; byte-id rides B6. C1-CORR-3 (corrects re-rule R3 + ken FLAG-2): the :249 chase is NOT purely latent — the CAST spelling (kb5_tuparg_c) earned a LIVE cs graduation (cs ok/1 → ok/0, correct tuple-arg classify); ww still runs wrong (task #99). Two-key pin tuparg_cast_bound99: cs-0 earned + ww-1 pinned observed-wrong; byte-id re-pins to full 0/0 when #99's ww fix lands. New 944_alias_cgen_b5_run row table (16 rows, 40 checks): controls signed/wstore/wstore1 byte-NEUTRAL as predicted (kind-keyed tests are the only behavior-visible peels — type.c classifiers already recurse); literal tuparg spellings stay dual-cell bounds (#99/#86). All 944-family suites green; sizelint 0. --- Makefile | 7 + cmd/w6c/cgen.c | 60 ++-- test/wcc/944_alias_cgen_b5_run.c | 522 +++++++++++++++++++++++++++++++ 3 files changed, 564 insertions(+), 25 deletions(-) create mode 100644 test/wcc/944_alias_cgen_b5_run.c diff --git a/Makefile b/Makefile index c8e7a15a..4df5bac4 100644 --- a/Makefile +++ b/Makefile @@ -308,6 +308,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_alias_amp_idx_run \ $(BIN)/test_alias_def_addr_run \ $(BIN)/test_alias_global_decl_run \ + $(BIN)/test_alias_cgen_b5_run \ $(BIN)/test_tuple_nary_destructure_run \ $(BIN)/test_rvalue_tuple_destructure_run \ $(BIN)/test_overcap_tuple_field_store_run \ @@ -1488,6 +1489,12 @@ $(BIN)/test_alias_global_decl_run: test/wcc/944_alias_global_decl_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_alias_cgen_b5_run: test/wcc/944_alias_cgen_b5_run.c \ + $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_tuple_nary_destructure_run: test/wcc/945_tuple_nary_destructure_run.c \ $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ diff --git a/cmd/w6c/cgen.c b/cmd/w6c/cgen.c index c81f312d..3ab1a3a4 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -246,7 +246,7 @@ node_tuplearg(Node *n) && n->kind != N_TRYUNW && n->kind != N_TRYPROP) return NULL; Type *t = n->type; - Type *u = (t && t->kind == TY_NAMED) ? t->under : t; + Type *u = type_chase_named(t); return (u && u->kind == TY_TUPLE) ? u : NULL; } @@ -406,7 +406,7 @@ cg_sret_retsize(Type *rt) static int fld_issigned(Type *t) { - Type *u = (t && t->kind == TY_NAMED) ? t->under : t; + Type *u = type_chase_named(t); if (u == NULL) return 0; if (u->kind == TY_BOOL) return 0; if (type_isunsigned(u)) return 0; @@ -497,8 +497,7 @@ castsrcprim(Node *n, int *sz, int *unsignd) * TY_UNTYPED_INT — we conservatively skip those (wwstage * matches: no tsuffix → sz=0). */ if (n->tsuffix && n->type) { - Type *u = (n->type->kind == TY_NAMED) - ? n->type->under : n->type; + Type *u = type_chase_named(n->type); if (u && u->kind != TY_UNTYPED_INT && u->kind != TY_UNTYPED_RUNE && type_isint(u)) { @@ -528,7 +527,7 @@ castsrcprim(Node *n, int *sz, int *unsignd) default: break; } - Type *u = (t && t->kind == TY_NAMED) ? t->under : t; + Type *u = type_chase_named(t); if (u && type_isint(u)) { *sz = (int)u->size; *unsignd = type_isunsigned(u); @@ -594,8 +593,7 @@ struct_float_class(Type *t, int *cls) int nflt[2], nint[2]; nflt[0] = nflt[1] = nint[0] = nint[1] = 0; for (Tfield *f = t->fields; f; f = f->next) { - Type *fu = (f->type && f->type->kind == TY_NAMED) - ? f->type->under : f->type; + Type *fu = type_chase_named(f->type); if (fu == NULL) return 0; int foff = (int)f->offset; int fsz = (int)fu->size; @@ -637,7 +635,7 @@ static int tagged_arg_size(Type *t) { if (t == NULL) return 0; - if (t->kind == TY_NAMED) t = t->under; + t = type_chase_named(t); if (t == NULL || t->kind != TY_TAGGED) return 0; /* Param/let/struct contexts have 6 int regs (DI..R9) so a 48B * tagged union (6 words) still fits in registers. Return values @@ -658,7 +656,7 @@ static int tagged_memarg_size(Type *t) { if (t == NULL) return 0; - if (t->kind == TY_NAMED) t = t->under; + t = type_chase_named(t); if (t == NULL || t->kind != TY_TAGGED || t->nullable) return 0; if (t->size <= 48) return 0; return (int)t->size; @@ -737,7 +735,7 @@ static int type_isnullable(Type *t) { if (t == NULL) return 0; - if (t->kind == TY_NAMED) t = t->under; + t = type_chase_named(t); return t && t->kind == TY_TAGGED && t->nullable; } @@ -747,10 +745,14 @@ static int nullable_ptr_tag(Type *t) { if (t == NULL) return 0; - if (t->kind == TY_NAMED) t = t->under; + t = type_chase_named(t); if (t == NULL || t->kind != TY_TAGGED) return 0; int i = 0; for (Tparam *p = t->params; p; p = p->next, i++) { + /* peel-ok: single peel PROBE-CLEARED (batch-2 c3-B2, + * 018ef66) — constructible variant params never carry + * 2+-level NAMED at this scan; ww twin cgenutil.ww:2758 + * carries the identical annotated peel. */ Type *pu = (p->type && p->type->kind == TY_NAMED) ? p->type->under : p->type; if (pu && pu->kind == TY_PTR) return i; @@ -825,7 +827,15 @@ cg_variant_match(Type *vt, Type *src) * unions so the outer widen tag (cg_tag_for_variant) computes. * Sound only while the model is nominal-lossy; the collision * guard at the widen site (cg_widen_tagged_store) enforces the - * invariant for when #199b/B-full lands true nominal layout. */ + * invariant for when #199b/B-full lands true nominal layout. + * peel-ok (#218, B5-c1): these single peels serve ONLY the + * both-TAGGED structural fallback — a NAMED struct source vs + * a NAMED variant falls through every arm at ANY depth + * (.ai/ken-b5-oracle.md §4: kb5_v2s1i both-wrong-identical + * at depth ONE, gate-blind; kb5_v2sE2 pointer-id exact-match + * works). Chasing here graduates zero rows; the real fix is + * a NEW NAMED-source structural arm under the >=2-candidate + * guard, BOTH stages — task #95. */ Type *vu = (vt->kind == TY_NAMED) ? vt->under : vt; Type *su = (src->kind == TY_NAMED) ? src->under : src; if (vu && su && vu->kind == TY_TAGGED && su->kind == TY_TAGGED) @@ -844,6 +854,9 @@ cg_variant_match(Type *vt, Type *src) static int cg_variant_struct_match(Type *vt, Type *src) { + /* peel-ok (#218, B5-c1): shape-only collision count for the + * both-TAGGED fallback above — same probe record, task #95 + * (.ai/ken-b5-oracle.md §4). */ Type *vu = (vt && vt->kind == TY_NAMED) ? vt->under : vt; Type *su = (src && src->kind == TY_NAMED) ? src->under : src; if (vu == NULL || su == NULL) return 0; @@ -857,7 +870,7 @@ static int cg_tagged_success_tag(Type *t) { if (t == NULL) return 0; - if (t->kind == TY_NAMED) t = t->under; + t = type_chase_named(t); if (t == NULL || t->kind != TY_TAGGED) return 0; int has_err = 0; for (Tparam *p = t->params; p; p = p->next) @@ -873,7 +886,7 @@ static int cg_variant_is_error(Type *t, int idx) { if (t == NULL) return 0; - if (t->kind == TY_NAMED) t = t->under; + t = type_chase_named(t); if (t == NULL || t->kind != TY_TAGGED) return 0; int has_err = 0; for (Tparam *p = t->params; p; p = p->next) @@ -896,7 +909,7 @@ static int cg_tag_for_variant(Type *t, Type *vt) { if (t == NULL || vt == NULL) return -1; - if (t->kind == TY_NAMED) t = t->under; + t = type_chase_named(t); if (t == NULL || t->kind != TY_TAGGED) return -1; /* Pass 1: exact match (NAMED-vs-NAMED pointer-id, tagged-vs-tagged, * bare type_eq). Exact matches take precedence and need no guard — @@ -950,7 +963,7 @@ static int type_istagged(Type *t) { if (t == NULL) return 0; - if (t->kind == TY_NAMED) t = t->under; + t = type_chase_named(t); return t && t->kind == TY_TAGGED; } @@ -1266,7 +1279,7 @@ static Type * type_unwrap(Type *t) { if (t == NULL) return NULL; - return (t->kind == TY_NAMED) ? t->under : t; + return type_chase_named(t); } /* Element-effective type for indexing. For `*[N]T` we drill through @@ -2453,7 +2466,7 @@ cg_widen_tagged_store(Cg *c, Local **locals_p, Type *dst, Node *src, ins2(c, A_MOVQ, areg(D_AX), amem(D_BP, write_off + k)); } - Type *du = (dst && dst->kind == TY_NAMED) ? dst->under : dst; + Type *du = type_chase_named(dst); if (du == NULL || du->kind != TY_TAGGED) return; if (du->nullable) { cgexpr(c, src, *locals_p); @@ -2476,11 +2489,9 @@ cg_widen_tagged_store(Cg *c, Local **locals_p, Type *dst, Node *src, * lookup and fall through to the matching branch. */ if (src && src->kind == N_CAST && src->lhs) { Type *castt = src->type; - Type *castu = (castt && castt->kind == TY_NAMED) - ? castt->under : castt; + Type *castu = type_chase_named(castt); Type *innert = src->lhs->type; - Type *innu = (innert && innert->kind == TY_NAMED) - ? innert->under : innert; + Type *innu = type_chase_named(innert); int cast_is_widen = (castu == du) || (castu && castu->kind == TY_TAGGED && type_eq(castt, dst)); int inner_is_tagged = innu && innu->kind == TY_TAGGED; @@ -2902,7 +2913,7 @@ copy_out: static void cg_widen_tagged_push(Cg *c, Local **locals_p, Type *dst, Node *src, int sz) { - Type *du = (dst && dst->kind == TY_NAMED) ? dst->under : dst; + Type *du = type_chase_named(dst); if (du && du->nullable) { /* Single 8B slot: just push the pointer/null. */ cgexpr(c, src, *locals_p); @@ -3191,8 +3202,7 @@ cg_structlit_fill(Cg *c, Local **locals_p, Type *lu, Node *lit, if (fu && fu->kind == TY_ARRAY && f->lhs && f->lhs->kind == N_ARRLIT) { Type *esub = fu->sub; - Type *esubu = (esub && esub->kind == TY_NAMED) - ? esub->under : esub; + Type *esubu = type_chase_named(esub); int esz = esub ? (int)esub->size : 1; int al_isf32 = 0; int is_float_el = fld_isfloat(esub, &al_isf32); diff --git a/test/wcc/944_alias_cgen_b5_run.c b/test/wcc/944_alias_cgen_b5_run.c new file mode 100644 index 00000000..98910725 --- /dev/null +++ b/test/wcc/944_alias_cgen_b5_run.c @@ -0,0 +1,522 @@ +/* + * 944_alias_cgen_b5_run — #5 alias arc F2b (B5 train): cstage cgen + * helper + funnel internalization through type_chase_named (c1), the + * #73 tripwire graduation (c2), the #89 cgreturn return-position widen + * (c3) and the #93 deref-index pointee chase (c4). All cs-only: every + * graduation is a DIVERGE→byte-id flip (cs aligns UP to ww's already- + * chasing asm); zero selfhost/lib bytes move across the train, so the + * _ww binaries stay bit-identical (the crossing detector). + * + * Row sources are ken's B5 oracle probes (/tmp/ken_b5/src, + * .ai/ken-b5-oracle.md + its c1-BOUNDARY ADDENDUM) and the c3/c4 + * seeds (/tmp/ken_na, kb4). + * + * row | shape (oracle row) | cs/ww + * --------------------+-------------------------------------+------ + * signed_ctl | kb5_signed: 2-lvl alias i8 field | + * | load (fld_issigned) — classifiers | + * | recurse, NO-mover control | 0/0 + * targ_2lvl | kb5_targ: 2-lvl alias (void|i64) | + * | param ≤48B (tagged_arg_size) — was | + * | cs LIVE exit-1, c1 graduation | 0/0 + * tmem_2lvl | kb5_tmem: >48B memarg twin | + * | (tagged_memarg_size) — graduation | 0/0 + * wpush_2lvl | kb5_wpush: call-site widen, literal | + * | arg (:2905 du) — divergence flip | 0/0 + * wstore_2lvl/_1lvl | kb5_wstore/wstore1: let-route | + * | controls (du arrives chased | + * | upstream — ken FLAG-1), NEUTRAL | 0/0 + * wstore_a_bound_b6 | kb5_wstore_a: ASSIGN-route re-store | + * | — both-correct divergent; the | + * | ident-lhs N_ASSIGN gates in the | + * | cgexpr INLINE set (B6), NOT the | + * | :2456 funnel (C1-CORR-2, corrects | + * | rob R2) — byte-id waived until B6 | 0/0 + * succ_bound_c3 | kb5_succ: `?`-success remap over | + * | 2-lvl alias result — both-correct | + * | divergent at RETURN positions only | + * | (:12278/:12318/:12322 family, | + * | C1-CORR-1) — joins c3's graduation | + * | set; byte-id waived until c3 | 0/0 + * null_2lvl | kb5_null: nullable fold + deref | + * | (type_isnullable/nullable_ptr_tag) | + * | — divergence flip | 0/0 + * f32p_2lvl | kb5_f32p: nested 2-lvl-alias f64 | + * | field struct param | + * | (struct_float_class) — flip | 0/0 + * fill0_ctl | kb5_fill0: named-struct array elem | + * | in struct lit — LOUD both stages | + * | (twin texts share the experr) | err/err + * fill2_bound100 | kb5_fill2: 1-alias-lvl elem — was | + * | both-wrong-IDENTICAL gate-blind; c1 | + * | :3195 chase flips cs LOUD, ww stays | + * | silent-wrong exit 1. DESIGNED | + * | DIVERGENCE until the ww twin gate | + * | (task #100) lands — dual-cell pin | err/1 + * tuparg_bound99 / | kb5_tuparg(1): literal named-tuple | + * tuparg1_bound99 | arg — cs checker-rejects (#86-kin), | + * | WW RUNS WRONG exit 1 (task #99, | + * | metric-1 ww side). Dual-cell pin | err/1 + * tuparg_cast_bound99 | kb5_tuparg_c: cast spelling dodges | + * | the cs checker; the :249 chase made | + * | cs run CORRECT (LIVE c1 graduation, | + * | C1-CORR-3 — corrects rob R3 + ken | + * | FLAG-2); ww still WRONG exit 1 | + * | (#99). Two-key pin: cs-0 earned + | + * | ww-1 pinned-wrong; byte-id waived | + * | until #99 re-pins to full byte-id | 0/1 + * tupglobal_bound86 | kb5_tupglobal: alias-tuple global — | + * | cs checker-dead (#86), ww runs 0. | + * | #85 type_unwrap close is SITE- | + * | closure with zero live graduations | + * | — this row pins the bound | err/0 + * + * K_RUN rows build+run BOTH drivers (cs exit==cswant, ww exit==wwwant) + * and assert cstage/wwstage asm byte-id. K_RUN_NOID asserts both run + * cells but waives byte-id — cite the blocking task/commit at the row. + * K_CSERR_WWRUN is the dual-cell bound kind (the inverse of + * 944_alias_accept_run's K_RUN_CS_WWERR): the cstage build must FAIL + * with experr AND the wwstage build+run must exit wwwant — both cells + * asserted so neither the cs loudness nor the ww behavior can drift + * silently while the cited task is parked. NNN<950, self-contained + * /tmp sources, no imports (944 precedent). + */ +#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), cb = fgetc(fb); + if (ca != cb) { rc = -1; break; } + if (ca == EOF) break; + } + fclose(fa); fclose(fb); + return rc; +} + +#define K_RUN 0 /* build+run BOTH, exits==wants, + byte-id */ +#define K_BUILDERR 1 /* build FAILS with experr on BOTH drivers */ +#define K_RUN_NOID 2 /* build+run BOTH, exits==wants, byte-id + * SKIPPED — cite the blocking task */ +#define K_CSERR_WWRUN 3 /* dual-cell bound row: cstage build must + * FAIL with experr; wwstage build+run, + * exit==wwwant — cite the blocking task */ + +struct row { const char *label; const char *src; + int cswant; int wwwant; int kind; const char *experr; }; + +/* errlog_has — a builderr cell must fail WITH its diagnostic; any other + * failure (parse error, crash, hang-kill) is a vacuous reject (940 + * precedent). */ +static int +errlog_has(const char *path, const char *needle) +{ + FILE *f = fopen(path, "rb"); + if (!f) return 0; + char buf[8192]; + size_t got = fread(buf, 1, sizeof buf - 1, f); + fclose(f); + buf[got] = '\0'; + return strstr(buf, needle) != NULL; +} + +static const struct row rows[] = { + /* ---- c1: helper + funnel internalization (the F2c-cs fuse) */ + { "signed_ctl", + "package main;\n" + "type s0 = i8;\n" + "type s1 = s0;\n" + "type st = struct { f: s1, g: s1 };\n" + "export fn main() i32 = {\n" + " let x: st;\n" + " x.f = -5;\n" + " x.g = 3;\n" + " if (x.f >= 0) { return 1; };\n" + " let w: i64 = x.f: i64;\n" + " if (w != -5) { return 2; };\n" + " return 0;\n" + "};\n", 0, 0, K_RUN, NULL }, + /* LIVE c1 graduation: tagged_arg_size single-peeled the 2-level + * alias union param to 0 → wrong arg path (cs exit 1 SILENT, ww + * correct). */ + { "targ_2lvl", + "package main;\n" + "type u0 = (void | i64);\n" + "type u = u0;\n" + "fn h(v: u) i32 = {\n" + " match (v) {\n" + " case let n: i64 => { if (n != 42) { return 1; }; return 0; };\n" + " case void => { return 2; };\n" + " };\n" + "};\n" + "export fn main() i32 = {\n" + " let x: u = 42i64;\n" + " return h(x);\n" + "};\n", 0, 0, K_RUN, NULL }, + /* LIVE c1 graduation: the >48B MEMORY-class twin + * (tagged_memarg_size). LAST payload word checked. */ + { "tmem_2lvl", + "package main;\n" + "type big = struct { a: i64, b: i64, c: i64, d: i64, e: i64, f: i64, g: i64 };\n" + "type m0 = (void | big);\n" + "type m = m0;\n" + "fn h(v: m) i32 = {\n" + " match (v) {\n" + " case let s: big => { if (s.g != 77) { return 1; }; return 0; };\n" + " case void => { return 2; };\n" + " };\n" + "};\n" + "export fn main() i32 = {\n" + " let s: big;\n" + " s.a = 11; s.b = 22; s.c = 33; s.d = 44; s.e = 55; s.f = 66; s.g = 77;\n" + " let x: m = s;\n" + " return h(x);\n" + "};\n", 0, 0, K_RUN, NULL }, + { "wpush_2lvl", + "package main;\n" + "type u0 = (void | i64);\n" + "type u = u0;\n" + "fn h(v: u) i32 = {\n" + " match (v) {\n" + " case let n: i64 => { if (n != 42) { return 1; }; return 0; };\n" + " case void => { return 2; };\n" + " };\n" + "};\n" + "export fn main() i32 = {\n" + " return h(42i64);\n" + "};\n", 0, 0, K_RUN, NULL }, + { "wstore_2lvl", + "package main;\n" + "type u0 = (void | i64);\n" + "type u = u0;\n" + "export fn main() i32 = {\n" + " let v: u = 42i64;\n" + " match (v) {\n" + " case let n: i64 => { if (n != 42) { return 1; }; };\n" + " case void => { return 2; };\n" + " };\n" + " return 0;\n" + "};\n", 0, 0, K_RUN, NULL }, + { "wstore_1lvl", + "package main;\n" + "type u = (void | i64);\n" + "export fn main() i32 = {\n" + " let v: u = 42i64;\n" + " match (v) {\n" + " case let n: i64 => { if (n != 42) { return 1; }; };\n" + " case void => { return 2; };\n" + " };\n" + " return 0;\n" + "};\n", 0, 0, K_RUN, NULL }, + /* C1-CORR-2 (ken c1-boundary addendum): the assign-route + * divergence does NOT close in this train — the ident-lhs + * N_ASSIGN tagged store gates in the cgexpr INLINE set (B6), + * never reaching the :2456 funnel. Both stages runtime-correct; + * byte-id rides B6. */ + { "wstore_a_bound_b6", + "package main;\n" + "type u0 = (void | i64);\n" + "type u = u0;\n" + "export fn main() i32 = {\n" + " let v: u = void;\n" + " v = 42i64;\n" + " match (v) {\n" + " case let n: i64 => { if (n != 42) { return 1; }; };\n" + " case void => { return 2; };\n" + " };\n" + " return 0;\n" + "};\n", 0, 0, K_RUN_NOID, NULL }, /* byte-id: B6 */ + /* C1-CORR-1: the residual divergence is exactly three paired + * return-position tag syntheses — the cgreturn return-route + * family (:12278/:12318/:12322). Joins c3's graduation set + * (pins-follow-the-layer); flips to K_RUN there. */ + { "succ_bound_c3", + "package main;\n" + "type e0 = !i64;\n" + "type r0 = (i64 | e0);\n" + "type r = r0;\n" + "fn w(bad: bool) r = {\n" + " if (bad) { return 7: e0; };\n" + " return 42;\n" + "};\n" + "fn outer() r = {\n" + " let v = w(false)?;\n" + " return v + 1;\n" + "};\n" + "export fn main() i32 = {\n" + " match (outer()) {\n" + " case let n: i64 => { if (n != 43) { return 1; }; };\n" + " case e0 => { return 2; };\n" + " };\n" + " return 0;\n" + "};\n", 0, 0, K_RUN_NOID, NULL }, /* byte-id: c3 */ + { "null_2lvl", + "package main;\n" + "type np0 = (*i64 | void);\n" + "type np = np0;\n" + "export fn main() i32 = {\n" + " let n: i64 = 31;\n" + " let p: np = &n;\n" + " match (p) {\n" + " case let q: *i64 => { if (*q != 31) { return 1; }; };\n" + " case void => { return 2; };\n" + " };\n" + " return 0;\n" + "};\n", 0, 0, K_RUN, NULL }, + { "f32p_2lvl", + "package main;\n" + "type in0 = struct { a: f64 };\n" + "type in1 = in0;\n" + "type pt = struct { x: in1, y: f64 };\n" + "fn g(p: pt) f64 = { return p.x.a + p.y; };\n" + "export fn main() i32 = {\n" + " let p: pt;\n" + " p.x.a = 2.5;\n" + " p.y = 1.5;\n" + " if (g(p) != 4.0) { return 1; };\n" + " return 0;\n" + "};\n", 0, 0, K_RUN, NULL }, + /* fill loudness control: named-struct array elem louds on BOTH + * stages with twin texts — the experr substring is shared. */ + { "fill0_ctl", + "package main;\n" + "type el0 = struct { a: i64 };\n" + "type S = struct { arr: [2]el0, n: i64 };\n" + "export fn main() i32 = {\n" + " let s = S { arr = [el0 { a = 5 }, el0 { a = 6 }], n = 3 };\n" + " if (s.arr[0].a + s.arr[1].a + s.n != 14) { return 1; };\n" + " return 0;\n" + "};\n", 0, 0, K_BUILDERR, + "has a str/slice/struct/tagged element" }, + /* DESIGNED IN-TRAIN ACCEPTANCE DIVERGENCE (B5 re-rule R4, task + * #100): pre-c1 this was both-wrong-IDENTICAL-silent (gate-blind + * #263 class — both stages accepted and ran exit 1 byte-id). The + * cs-only :3195 chase flips cs to the fill0 fatal; ww's own + * alias-blind twin gate (checkarrlitfits kin) stays silent-wrong + * until #100 lands — then this row graduates to a both-loud + * fill0-class pair. Both cells asserted. */ + { "fill2_bound100", + "package main;\n" + "type el0 = struct { a: i64 };\n" + "type el = el0;\n" + "type S = struct { arr: [2]el, n: i64 };\n" + "export fn main() i32 = {\n" + " let s = S { arr = [el0 { a = 5 }, el0 { a = 6 }], n = 3 };\n" + " if (s.arr[0].a + s.arr[1].a + s.n != 14) { return 1; };\n" + " return 0;\n" + "};\n", 0, 1, K_CSERR_WWRUN, + "has a str/slice/struct/tagged element" }, /* ww: task #100 */ + /* node_tuplearg:249 pin (ken FLAG-2): the LITERAL spelling has + * NO GREEN TARGET — cs checker-rejects even at 1 level (#86-kin) + * while WW ACCEPTS AND RUNS WRONG (exit 1, task #99 metric-1 + * ww-side). Cells pinned as observed. */ + { "tuparg_bound99", + "package main;\n" + "type tp0 = (i64, i64);\n" + "type tp = tp0;\n" + "fn f(t: tp) i64 = { return t.0 + t.1; };\n" + "export fn main() i32 = {\n" + " let t: tp = (4, 9);\n" + " if (f(t) != 13) { return 1; };\n" + " return 0;\n" + "};\n", 0, 1, K_CSERR_WWRUN, + "not assignable" }, /* ww: task #99 */ + { "tuparg1_bound99", /* 1-LEVEL identical — not alias-depth */ + "package main;\n" + "type tp = (i64, i64);\n" + "fn f(t: tp) i64 = { return t.0 + t.1; };\n" + "export fn main() i32 = {\n" + " let t: tp = (4, 9);\n" + " if (f(t) != 13) { return 1; };\n" + " return 0;\n" + "};\n", 0, 1, K_CSERR_WWRUN, + "not assignable" }, /* ww: task #99 */ + /* C1-CORR-3 (corrects rob R3 + ken FLAG-2): the CAST spelling + * dodges the cs checker, and the :249 chase gave it a LIVE cs + * graduation — cs now classifies the alias-tuple arg and runs + * CORRECT. ww still runs WRONG (#99). Two-key pin: cs-0 earned + * + ww-1 pinned OBSERVED-WRONG; byte-id waived — when #99's ww + * fix lands, the ww cell reds and forces the re-pin to full 0/0 + * byte-id (the designed graduation path). */ + { "tuparg_cast_bound99", + "package main;\n" + "type tp0 = (i64, i64);\n" + "type tp = tp0;\n" + "fn f(t: tp) i64 = { return t.0 + t.1; };\n" + "export fn main() i32 = {\n" + " let t: tp = (4, 9): tp;\n" + " if (f(t) != 13) { return 1; };\n" + " return 0;\n" + "};\n", 0, 1, K_RUN_NOID, NULL }, /* ww+byte-id: task #99 */ + /* #85 SITE-closure bound: type_unwrap's consumers (:14716/:14907 + * tuple-global layout walks) are checker-DEAD on cs for alias + * tuples (#86 upstream) — the c1 chase graduates NOTHING live + * here; ww runs the global correctly. */ + { "tupglobal_bound86", + "package main;\n" + "type tp0 = (i64, i64);\n" + "type tp = tp0;\n" + "let G: tp = (4, 9);\n" + "export fn main() i32 = {\n" + " if (G.0 + G.1 != 13) { return 1; };\n" + " return 0;\n" + "};\n", 0, 0, K_CSERR_WWRUN, + "not assignable" }, /* cs: task #86 */ +}; + +static int +run_driver(const char *driver, const struct row *r, int i, int expect_err, + int want) +{ + char src[96], tmpdir[96], errf[96], cmd[1024]; + snprintf(src, sizeof src, "/tmp/ab5_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/ab5_%d_d_%d", getpid(), i); + snprintf(errf, sizeof errf, "/tmp/ab5_%d_e_%d", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -1; + fputs(r->src, f); + fclose(f); + + mkdir(tmpdir, 0755); + snprintf(cmd, sizeof cmd, + "cd %s && timeout 20 %s build %s >/dev/null 2>%s", + tmpdir, driver, src, errf); + int brc = runwait(cmd); + if (expect_err) { + int ok = (brc != 0) + && (r->experr == NULL || errlog_has(errf, r->experr)); + if (!ok) + fprintf(stderr, "row[%s]: %s expected loud builderr " + "\"%s\" (brc=%d)\n", r->label, driver, + r->experr ? r->experr : "", brc); + unlink(src); unlink(errf); rmdir(tmpdir); + return ok ? 0 : 1; + } + if (brc != 0) { + fprintf(stderr, "row[%s]: build via %s failed\n", + r->label, driver); + unlink(src); unlink(errf); rmdir(tmpdir); + return -1; + } + + const char *base = strrchr(src, '/'); + base = base ? base + 1 : src; + char outbin[256]; + snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); + char *dot = strrchr(outbin, '.'); + if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; + int got = runwait(outbin); + + unlink(src); unlink(outbin); unlink(errf); rmdir(tmpdir); + if (got != want) { + fprintf(stderr, "row[%s]: %s exit %d, want %d\n", + r->label, driver, got, want); + return 1; + } + return 0; +} + +static int +asm_byte_identical(const char *bin, const struct row *r, int i) +{ + char src[96], cs[96], ws[96], cmd[1024]; + snprintf(src, sizeof src, "/tmp/ab5_asm_%d_%d.ww", getpid(), i); + snprintf(cs, sizeof cs, "/tmp/ab5_asm_%d_%d_c.s", getpid(), i); + snprintf(ws, sizeof ws, "/tmp/ab5_asm_%d_%d_w.s", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -1; + fputs(r->src, f); + fclose(f); + + snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: w6c errored\n", r->label); + unlink(src); + return -1; + } + snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null", + bin, ws, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label); + unlink(src); unlink(cs); + return -1; + } + int rc = slurp_eq(cs, ws); + if (rc != 0) + fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n", + r->label); + unlink(src); unlink(cs); unlink(ws); + return rc; +} + +int +main(void) +{ + const char *bin = getenv("BIN"); + if (!bin) bin = "out/bin"; + char absbin[2080]; + 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[2120], wdrv[2120]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); + + int n = (int)(sizeof rows / sizeof rows[0]); + int total = 0, fail = 0; + + for (int i = 0; i < n; i++) { + total++; + int cs_err = rows[i].kind == K_BUILDERR + || rows[i].kind == K_CSERR_WWRUN; + if (run_driver(cdrv, &rows[i], i, cs_err, + rows[i].cswant) != 0) fail++; + } + if (access(wdrv, X_OK) == 0) { + for (int i = 0; i < n; i++) { + total++; + int ww_err = rows[i].kind == K_BUILDERR; + if (run_driver(wdrv, &rows[i], i, ww_err, + rows[i].wwwant) != 0) fail++; + } + for (int i = 0; i < n; i++) { + if (rows[i].kind != K_RUN) + continue; + total++; + if (asm_byte_identical(bin, &rows[i], i) != 0) fail++; + } + } + + if (fail) { + fprintf(stderr, "alias_cgen_b5: %d/%d checks failed\n", + fail, total); + return 1; + } + printf("alias_cgen_b5: %d/%d ok\n", total, total); + return 0; +}