From 6a5bb3efc958f09b903c85ec57d754f332c7ec08 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Sat, 6 Jun 2026 16:57:42 +0900 Subject: [PATCH] wcc/cgen: #47 gap-A tuple-in-union tagged-element store (both-stage) A tuple containing a tagged-union element, used as a union member (e.g. ((void|size),(void|size),size) | error), loud-stopped in the cgen return-store: the tuple-in-union store walk had scalar/float/ str/slice element arms but no TY_TAGGED-element arm. A PLAIN tuple-in-union already worked -- the blocker was the tagged element. Add the recursive two-level widen arm at both stages (cg_widen_tagged_store / cgwidentaggedstorebp): for each tagged element, re-enter the tagged-box store (inner tag@slot+0, payload@slot+8) at the element's tuple-payload offset, then stamp the outer tuple tag. Slot strides come from the type table (roundup8(eu->size)) -- the checker already sizes the shape correctly (tuple->size measured 40, union box 48; check.c:715-720). The recursion descends a finite type tree (a tagged element is never a tuple literal, so it can't re-enter the tuple arm); unsupported deeper nesting still louds via the existing size/tag guards. Both stages get the same arm -> byte-id (990-997 green; additive, bootstrap-neutral). cstage runs the full b1c shape (construct+return+match-extract) as the runtime reference; wwstage's store rides on byte-id until gap-B. gap-B (wwstage checker match-acceptance of the tuple-with-tagged case pattern) is a separate commit -- wwstage still louds the match honestly at the checker. Pin 944_tuple_tagged_union_run. --- Makefile | 7 + cmd/w6c/cgen.c | 32 ++- selfhost/cmd/w6c/main.combined.ww | 38 ++- selfhost/cmd/wcc/cgenutil.ww | 38 ++- selfhost/cmd/wwdump/main.combined.ww | 38 ++- test/wcc/944_tuple_tagged_union_run.c | 317 ++++++++++++++++++++++++++ 6 files changed, 429 insertions(+), 41 deletions(-) create mode 100644 test/wcc/944_tuple_tagged_union_run.c diff --git a/Makefile b/Makefile index 8d17fd24..32f3ca96 100644 --- a/Makefile +++ b/Makefile @@ -312,6 +312,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_def_amp_idx_run \ $(BIN)/test_idx_tagged_field_run \ $(BIN)/test_tagged_widen_arg_run \ + $(BIN)/test_tuple_tagged_union_run \ $(BIN)/test_alias_global_decl_run \ $(BIN)/test_alias_cgen_b5_run \ $(BIN)/test_alias_cgen_b6_run \ @@ -1532,6 +1533,12 @@ $(BIN)/test_tagged_widen_arg_run: test/wcc/944_tagged_widen_arg_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_tuple_tagged_union_run: test/wcc/944_tuple_tagged_union_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_alias_global_decl_run: test/wcc/944_alias_global_decl_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 c74c9cab..2c82e0ac 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -2768,16 +2768,18 @@ cg_widen_tagged_store(Cg *c, Local **locals_p, Type *dst, Node *src, * SysV eightbyte tuple classification is a deferred follow-up. */ int total = 0; for (Node *e = tupsrc->list; e; e = e->next) { - /* #22a (rule 7): a tagged element's box can't ride - * the scalar/wide store arms below — pre-guard it - * silently stored word0. Nested tagged-in-tuple-in- - * union packing is the #242/#22b family. */ + /* #47 gap-A: a tagged element rides its OWN box + * (tag + payload, roundup8) per tuple slot — NOT one + * 8B word. Mirror the checker's N_TTUPLE accumulation + * (check.c:715-720): aggregate/tagged elem += its + * box size rounded to 8; the store loop below boxes it + * recursively (two-level widen). */ Type *eu = type_chase_named(e->type); if (eu && eu->kind == TY_TAGGED) - fatal("cg_widen_tagged_store: tagged element " - "in a tuple-in-union payload unwired " - "(see #242/#22b)"); - total += (node_isstr(e) || node_isslice(e)) ? 24 : 8; + total += ((int)eu->size + 7) & ~7; + else + total += (node_isstr(e) || node_isslice(e)) ? + 24 : 8; } if (8 + total > sz) fatal("cg_widen_tagged_store: tuple-in-union payload needs " @@ -2789,6 +2791,20 @@ cg_widen_tagged_store(Cg *c, Local **locals_p, Type *dst, Node *src, amem(D_BP, write_off + k)); int foff = 0; for (Node *e = tupsrc->list; e; e = e->next) { + /* #47 gap-A: a tagged element is its own tag+payload + * box. Recurse so the inner box (tag@slot+0, + * payload@slot+8) is built at the element's tuple- + * payload offset, exactly as a top-level tagged-store + * does — two-level widen (inner boxes here, outer tuple + * tag stamped below). slot stride = roundup8(box). */ + Type *eu = type_chase_named(e->type); + if (eu && eu->kind == TY_TAGGED) { + int ebox = ((int)eu->size + 7) & ~7; + cg_widen_tagged_store(c, locals_p, e->type, e, + D_BP, write_off + 8 + foff, ebox); + foff += ebox; + continue; + } int e_isf32 = 0; int isflt = fld_isfloat(e->type, &e_isf32); int wide = node_isstr(e) || node_isslice(e); diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 0390219b..89977179 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -20212,21 +20212,19 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s let ttotal: i32 = 0; let ce: *node = tupsrc.list; for (ce != nil) { - // #22a (rule 7): a tagged element's box can't - // ride the scalar/wide store arms below — - // pre-guard it silently stored word0. Nested - // tagged-in-tuple-in-union packing is the - // #242/#22b family. Mirrors cstage. + // #47 gap-A: a tagged element rides its OWN + // box (tag + payload, roundup8) per tuple slot + // — NOT one 8B word. Mirror the checker's + // N_TTUPLE accumulation (check.ww:1640-1646); + // the store loop below boxes it recursively + // (two-level widen). Symmetric with cstage. let ceti: *tinfo = ce.type_: *tinfo; ceti = tichase(ceti); if (ceti != nil && ceti.kind == tykind.TY_TAGGED) { - let m22: str = "cgwidentaggedstore: tagged element in a tuple-in-union payload unwired (see #242/#22b)\n"; - os.write(2, m22.ptr, m22.len: u64); - os.exit(1); - }; - if (nodeisstr(c, ce) || nodeisslice(c, ce)) { + ttotal += (ceti.size: i32 + 7) & ~7; + } else { if (nodeisstr(c, ce) || nodeisslice(c, ce)) { ttotal += 24; - } else { ttotal += 8; }; + } else { ttotal += 8; }; }; ce = ce.next; }; if (8 + ttotal > slot_sz) { @@ -20245,6 +20243,24 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s let tfoff: i32 = 0; let te: *node = tupsrc.list; for (te != nil) { + // #47 gap-A: a tagged element is its own + // tag+payload box. Recurse so the inner box + // (tag@slot+0, payload@slot+8) is built at the + // element's tuple-payload offset, exactly as a + // top-level tagged-store does — two-level widen + // (inner boxes here, outer tuple tag stamped + // below). slot stride = roundup8(box). Symmetric + // with cstage cg_widen_tagged_store. + let teti: *tinfo = te.type_: *tinfo; + teti = tichase(teti); + if (teti != nil && teti.kind == tykind.TY_TAGGED) { + let ebox: i32 = (teti.size: i32 + 7) & ~7; + cgwidentaggedstore(c, te.type_: *tinfo, te, + "BP", slot_off + 8 + tfoff, ebox); + tfoff += ebox; + te = te.next; + continue; + }; let isflt: bool = isfloattype(c, te); let wide: bool = nodeisstr(c, te) || nodeisslice(c, te); let esz: i32 = 8; diff --git a/selfhost/cmd/wcc/cgenutil.ww b/selfhost/cmd/wcc/cgenutil.ww index b8fbe8c3..c1fa1dba 100644 --- a/selfhost/cmd/wcc/cgenutil.ww +++ b/selfhost/cmd/wcc/cgenutil.ww @@ -4071,21 +4071,19 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s let ttotal: i32 = 0; let ce: *node = tupsrc.list; for (ce != nil) { - // #22a (rule 7): a tagged element's box can't - // ride the scalar/wide store arms below — - // pre-guard it silently stored word0. Nested - // tagged-in-tuple-in-union packing is the - // #242/#22b family. Mirrors cstage. + // #47 gap-A: a tagged element rides its OWN + // box (tag + payload, roundup8) per tuple slot + // — NOT one 8B word. Mirror the checker's + // N_TTUPLE accumulation (check.ww:1640-1646); + // the store loop below boxes it recursively + // (two-level widen). Symmetric with cstage. let ceti: *tinfo = ce.type_: *tinfo; ceti = tichase(ceti); if (ceti != nil && ceti.kind == tykind.TY_TAGGED) { - let m22: str = "cgwidentaggedstore: tagged element in a tuple-in-union payload unwired (see #242/#22b)\n"; - os.write(2, m22.ptr, m22.len: u64); - os.exit(1); - }; - if (nodeisstr(c, ce) || nodeisslice(c, ce)) { + ttotal += (ceti.size: i32 + 7) & ~7; + } else { if (nodeisstr(c, ce) || nodeisslice(c, ce)) { ttotal += 24; - } else { ttotal += 8; }; + } else { ttotal += 8; }; }; ce = ce.next; }; if (8 + ttotal > slot_sz) { @@ -4104,6 +4102,24 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s let tfoff: i32 = 0; let te: *node = tupsrc.list; for (te != nil) { + // #47 gap-A: a tagged element is its own + // tag+payload box. Recurse so the inner box + // (tag@slot+0, payload@slot+8) is built at the + // element's tuple-payload offset, exactly as a + // top-level tagged-store does — two-level widen + // (inner boxes here, outer tuple tag stamped + // below). slot stride = roundup8(box). Symmetric + // with cstage cg_widen_tagged_store. + let teti: *tinfo = te.type_: *tinfo; + teti = tichase(teti); + if (teti != nil && teti.kind == tykind.TY_TAGGED) { + let ebox: i32 = (teti.size: i32 + 7) & ~7; + cgwidentaggedstore(c, te.type_: *tinfo, te, + "BP", slot_off + 8 + tfoff, ebox); + tfoff += ebox; + te = te.next; + continue; + }; let isflt: bool = isfloattype(c, te); let wide: bool = nodeisstr(c, te) || nodeisslice(c, te); let esz: i32 = 8; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index e3d05eb9..121a77de 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -20212,21 +20212,19 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s let ttotal: i32 = 0; let ce: *node = tupsrc.list; for (ce != nil) { - // #22a (rule 7): a tagged element's box can't - // ride the scalar/wide store arms below — - // pre-guard it silently stored word0. Nested - // tagged-in-tuple-in-union packing is the - // #242/#22b family. Mirrors cstage. + // #47 gap-A: a tagged element rides its OWN + // box (tag + payload, roundup8) per tuple slot + // — NOT one 8B word. Mirror the checker's + // N_TTUPLE accumulation (check.ww:1640-1646); + // the store loop below boxes it recursively + // (two-level widen). Symmetric with cstage. let ceti: *tinfo = ce.type_: *tinfo; ceti = tichase(ceti); if (ceti != nil && ceti.kind == tykind.TY_TAGGED) { - let m22: str = "cgwidentaggedstore: tagged element in a tuple-in-union payload unwired (see #242/#22b)\n"; - os.write(2, m22.ptr, m22.len: u64); - os.exit(1); - }; - if (nodeisstr(c, ce) || nodeisslice(c, ce)) { + ttotal += (ceti.size: i32 + 7) & ~7; + } else { if (nodeisstr(c, ce) || nodeisslice(c, ce)) { ttotal += 24; - } else { ttotal += 8; }; + } else { ttotal += 8; }; }; ce = ce.next; }; if (8 + ttotal > slot_sz) { @@ -20245,6 +20243,24 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s let tfoff: i32 = 0; let te: *node = tupsrc.list; for (te != nil) { + // #47 gap-A: a tagged element is its own + // tag+payload box. Recurse so the inner box + // (tag@slot+0, payload@slot+8) is built at the + // element's tuple-payload offset, exactly as a + // top-level tagged-store does — two-level widen + // (inner boxes here, outer tuple tag stamped + // below). slot stride = roundup8(box). Symmetric + // with cstage cg_widen_tagged_store. + let teti: *tinfo = te.type_: *tinfo; + teti = tichase(teti); + if (teti != nil && teti.kind == tykind.TY_TAGGED) { + let ebox: i32 = (teti.size: i32 + 7) & ~7; + cgwidentaggedstore(c, te.type_: *tinfo, te, + "BP", slot_off + 8 + tfoff, ebox); + tfoff += ebox; + te = te.next; + continue; + }; let isflt: bool = isfloattype(c, te); let wide: bool = nodeisstr(c, te) || nodeisslice(c, te); let esz: i32 = 8; diff --git a/test/wcc/944_tuple_tagged_union_run.c b/test/wcc/944_tuple_tagged_union_run.c new file mode 100644 index 00000000..1766d2fd --- /dev/null +++ b/test/wcc/944_tuple_tagged_union_run.c @@ -0,0 +1,317 @@ +/* + * 944_tuple_tagged_union_run — project #47 gap-A: a tuple whose elements + * include a TAGGED union, boxed as a member of a tagged union, must + * CONSTRUCT + RETURN-STORE identically in both stages. + * + * A PLAIN tuple-in-union already worked (#242, 940_tuple_in_union); the + * blocker was specifically the TAGGED ELEMENT. cg_widen_tagged_store's + * tuple-in-union store walk had scalar/float/str/slice element arms but + * NO TY_TAGGED arm — it loud-stopped ("tagged element in a tuple-in-union + * payload unwired"). gap-A adds a RECURSIVE two-level widen: each tagged + * element is boxed into its tuple slot (inner tag@slot+0, payload@slot+8) + * by re-entering cg_widen_tagged_store per element, THEN the whole tuple is + * boxed (outer tag + 40B payload copy). The element's tuple-slot stride is + * its OWN box size (roundup8) — a (void|size) elem is 16B (2 slots), not + * 8B — mirroring the checker's N_TTUPLE accumulation (check.c:715-720 / + * check.ww:1640-1646). MEASURED at HEAD: tuple->size for + * ((void|size),(void|size),size) = 40 (16+16+8); union box = 48. + * + * Both stages get the SAME arm → byte-identical asm (rule 10). + * + * K_RUN rows (ret-shape: construct + return + `is error` consume, NO match + * case-arm): build+run exit==want on BOTH drivers AND cs==ww byte-id. These + * are the gap-A pin — wwstage's store correctness rides on byte-id with the + * cstage runtime reference until gap-B unlocks wwstage's match-extract. + * + * K_CSRUN row (full b1c: construct + return + MATCH case-arm + is/as elem + * reads): build+run exit==want on CSTAGE ONLY. cstage's checker accepts the + * tuple-with-tagged case pattern; wwstage's does NOT (#47 gap-B, a separate + * later commit — casevariantin/typeeqast align-up). So the wwstage match- + * extract RUNTIME row lands with gap-B; here it is cstage-only and proves + * the recursive store reads back every tuple-elem value correctly. + * + * NNN<950, self-contained (/tmp, no imports), so rule-14's selfhost-sibling + * race does not apply (903/940/945 precedent). Cites #47 gap-A. + */ +#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 drivers, exit==want, + cs==ww byte-id */ +#define K_CSRUN 1 /* build+run CSTAGE ONLY (wwstage match-extract = gap-B) */ + +struct row { const char *label; const char *src; int kind; int want; }; + +static const struct row rows[] = { + /* ret-shape 3-tuple ((void|size),(void|size),size) | error: the + * parse_repetition shape. pr(0) builds the tuple (min=void tag0, + * max=5:size tag1, rep=7), pr(2) the error variant. Consumed via + * `is error` only — no match arm — so both stages build+run. The + * recursive inner box exercises BOTH inner tags (void + size). */ + { "ret_3tuple", + "package main;\n" + "type error = !str;\n" + "fn pr(sel: i32) (((void | size), (void | size), size) | error) = {\n" + " if (sel == 0) {\n" + " let min: (void | size) = void;\n" + " let max: (void | size) = 5: size;\n" + " return (min, max, 7: size);\n" + " };\n" + " return \"neg\": error;\n" + "};\n" + "export fn main() i32 = {\n" + " let a: (((void | size), (void | size), size) | error) = pr(0);\n" + " if (a is error) { return 1; };\n" + " let b: (((void | size), (void | size), size) | error) = pr(2);\n" + " if (b is error) { return 235; };\n" + " return 2;\n" + "};\n", K_RUN, 235 }, + /* ret-shape 2-tuple ((void|size), size) | error: min=5:size (tag1). + * Smaller box (8+16+8 = wait: 16+8 = 24B tuple, 32B union) exercises + * the same recursive arm at a different slot layout. */ + { "ret_2tuple", + "package main;\n" + "type error = !str;\n" + "fn pr(sel: i32) (((void | size), size) | error) = {\n" + " let min: (void | size) = 5: size;\n" + " return (min, 7: size);\n" + "};\n" + "export fn main() i32 = {\n" + " let a: (((void | size), size) | error) = pr(0);\n" + " if (a is error) { return 1; };\n" + " return 235;\n" + "};\n", K_RUN, 235 }, + /* scalar-elem control (regression guard): a PLAIN tuple-in-union with + * no tagged element still takes the scalar arm and stays byte-id. */ + { "scalar_ctrl", + "package main;\n" + "type error = !str;\n" + "fn pr(sel: i32) ((size, size, size) | error) = {\n" + " return (3: size, 4: size, 5: size);\n" + "};\n" + "export fn main() i32 = {\n" + " let a: ((size, size, size) | error) = pr(0);\n" + " if (a is error) { return 1; };\n" + " return 235;\n" + "};\n", K_RUN, 235 }, + /* full b1c (cstage-only): construct + return + MATCH case-arm + is/as + * elem reads on the boxed tuple. pr(0): t.0 is void (+1), t.1 is size + * = 5 (+5), t.2*10 (+70) = 76. pr(1): t.0 is size==3 (+100), t.1 is + * void (+50), t.2 = 9 (+9) = 235. pr(2): error arm, strings.compare + * matches. Reads back EVERY tuple-elem value the recursive store wrote. + * wwstage cannot match the tuple-with-tagged case pattern yet (gap-B). */ + { "b1c_full", + "package main;\n" + "type error = !str;\n" + "fn pr(sel: i32) (((void | size), (void | size), size) | error) = {\n" + " if (sel == 0) {\n" + " let min: (void | size) = void;\n" + " let max: (void | size) = 5: size;\n" + " return (min, max, 7: size);\n" + " };\n" + " if (sel == 1) {\n" + " let min: (void | size) = 3: size;\n" + " let max: (void | size) = void;\n" + " return (min, max, 9: size);\n" + " };\n" + " return \"Negative repetition count\": error;\n" + "};\n" + "import strings;\n" + "export fn main() i32 = {\n" + " let acc: size = 0;\n" + " let a: (((void | size), (void | size), size) | error) = pr(0);\n" + " match (a) {\n" + " case let t: ((void | size), (void | size), size) => {\n" + " if (t.0 is void) { acc += 1; };\n" + " if (t.1 is size) { acc += t.1 as size; };\n" + " acc += t.2 * 10;\n" + " };\n" + " case let e: error => { return 250; };\n" + " };\n" + " let b: (((void | size), (void | size), size) | error) = pr(1);\n" + " match (b) {\n" + " case let t: ((void | size), (void | size), size) => {\n" + " if (t.0 is size && t.0 as size == 3) { acc += 100; };\n" + " if (t.1 is void) { acc += 50; };\n" + " acc += t.2;\n" + " };\n" + " case let e: error => { return 251; };\n" + " };\n" + " let c: (((void | size), (void | size), size) | error) = pr(2);\n" + " match (c) {\n" + " case let t: ((void | size), (void | size), size) => { return 252; };\n" + " case let e: error => {\n" + " if (strings.compare((e: str),\n" + " \"Negative repetition count\") != 0) { return 253; };\n" + " };\n" + " };\n" + " return (acc: i32);\n" + "};\n", K_CSRUN, 235 }, +}; + +/* build+run via a driver; returns 0 pass, nonzero fail. is_ww gates the + * cstage-only K_CSRUN row. */ +static int +run_driver(const char *driver, int is_ww, const struct row *r, int i) +{ + char src[96], tmpdir[96], errf[96], cmd[1024]; + + if (r->kind == K_CSRUN && is_ww) + return 0; /* wwstage match-extract is gap-B */ + + snprintf(src, sizeof src, "/tmp/ttu_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/ttu_%d_d_%d", getpid(), i); + snprintf(errf, sizeof errf, "/tmp/ttu_%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 && %s build %s >/dev/null 2>%s", + tmpdir, driver, src, errf); + int brc = runwait(cmd); + 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 != r->want) { + fprintf(stderr, "row[%s]: %s exit %d, want %d\n", + r->label, driver, got, r->want); + return 1; + } + return 0; +} + +/* cs==ww .s byte-id (rule 10) — K_RUN rows only. */ +static int +byteid(const char *w6c, const char *w6c_ww, const struct row *r, int i) +{ + char src[96], cs_s[96], ws_s[96], cmd[1024]; + snprintf(src, sizeof src, "/tmp/ttu_bi_%d_%d.ww", getpid(), i); + snprintf(cs_s, sizeof cs_s, "/tmp/ttu_bi_%d_%d_cs.s", getpid(), i); + snprintf(ws_s, sizeof ws_s, "/tmp/ttu_bi_%d_%d_ww.s", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -1; + fputs(r->src, f); + fclose(f); + + int rc = 0; + 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", r->label); rc = 1; } + else { + 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", r->label); rc = 1; } + else if (slurp_eq(cs_s, ws_s) != 0) { + fprintf(stderr, "row[%s]: cstage/wwstage .s DIFFER " + "(#47 gap-A tuple-w/-tagged-elem store regression)\n", + r->label); + rc = 1; + } + } + unlink(src); unlink(cs_s); unlink(ws_s); + return rc; +} + +int +main(void) +{ + const char *bin = getenv("BIN"); + if (!bin) bin = "out/bin"; + char absbin[512]; + if (bin[0] != '/') { + char cwd[256]; + if (getcwd(cwd, sizeof cwd) == NULL) return 1; + snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); + bin = absbin; + } + + char cdrv[640], wdrv[640], w6c[640], w6c_ww[640]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); + snprintf(w6c, sizeof w6c, "%s/w6c", bin); + snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin); + + struct { const char *name; const char *path; int gated; int is_ww; } + drivers[] = { + { "cstage", cdrv, 0, 0 }, + { "wwstage", wdrv, 1, 1 }, + { NULL, NULL, 0, 0 }, + }; + + int n = (int)(sizeof rows / sizeof rows[0]); + int total = 0, fail = 0; + + for (int d = 0; drivers[d].name; d++) { + if (drivers[d].gated && access(drivers[d].path, X_OK) != 0) { + fprintf(stderr, "tuple_tagged_union: skip %s (no %s)\n", + drivers[d].name, drivers[d].path); + continue; + } + for (int i = 0; i < n; i++) { + total++; + if (run_driver(drivers[d].path, drivers[d].is_ww, + &rows[i], i) != 0) fail++; + } + } + + /* cs==ww byte-id for K_RUN rows only (K_CSRUN's match arm has no + * wwstage .s until gap-B). */ + if (access(w6c_ww, X_OK) == 0) { + for (int i = 0; i < n; i++) { + if (rows[i].kind != K_RUN) continue; + total++; + if (byteid(w6c, w6c_ww, &rows[i], i) != 0) fail++; + } + } + + if (fail) { + fprintf(stderr, "tuple_tagged_union: %d/%d checks failed\n", + fail, total); + return 1; + } + printf("tuple_tagged_union: %d/%d ok\n", total, total); + return 0; +}