diff --git a/Makefile b/Makefile index c7d55658..b7fb99d0 100644 --- a/Makefile +++ b/Makefile @@ -310,6 +310,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_def_amp_idx_run \ + $(BIN)/test_tagged_widen_arg_run \ $(BIN)/test_alias_global_decl_run \ $(BIN)/test_alias_cgen_b5_run \ $(BIN)/test_alias_cgen_b6_run \ @@ -1518,6 +1519,12 @@ $(BIN)/test_def_amp_idx_run: test/wcc/944_def_amp_idx_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_tagged_widen_arg_run: test/wcc/944_tagged_widen_arg_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/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 840a66e2..303960ee 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -16402,7 +16402,23 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { if (arg.kind == nkind.N_IDENT) { let lc: *local = localfindnode(c, arg.str); if (lc != nil) { - aistagged = istaggedtype(c, lc.tnode); + // #55: only treat a tagged ident as + // "already tagged" (natural push, no remap) + // when its slot MATCHES the param. On slot- + // DIFFER the source is a NARROWER union widened + // into a wider one — fall to the widen scratch + // + tag-remap below (the slot-gated INDEX/DOT/ + // STAR arms' twin). Pre-#55 the ungated TRUE + // natural-pushed the narrower box's words with + // no remap (aligned-tag LUCK, misaligned-tag + // wrong). cstage routes every tagged source + // through cg_widen_tagged_store (cmd/w6c/ + // cgen.c:2982 src_is_tagged). + if (istaggedtype(c, lc.tnode)) { + if (slotsize(c, lc.tnode) == slotsize(c, ptype)) { + aistagged = true; + }; + }; }; }; // #21: a CALL returning a tagged-union must @@ -16496,13 +16512,26 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { let argtup: *tinfo = arg.type_: *tinfo; argtup = tichase(argtup); let argistuple: bool = false; + // #55: a tagged SOURCE widened into a wider/reordered tagged param + // (slot-DIFFER — the ident/deref/index/dot legs that fell past the + // slot-gated aistagged arms above) has no direct-push shape: the + // scalar fast arm below would box word0 with a tag clamped to 0, + // dropping the real tag + high words and skipping the variant + // remap. Route through the @tagscr store, whose ident/memread/ + // cursor arms copy the box + tag-remap. Mirrors cstage + // cg_widen_tagged_push's unconditional src_is_tagged scratch + // routing (cmd/w6c/cgen.c:2982). + let argistagged: bool = false; if (argtup != nil) { if (argtup.kind == tykind.TY_TUPLE) { argistuple = true; }; + if (argtup.kind == tykind.TY_TAGGED) { + argistagged = true; + }; }; let pname: str = rhsstructpayload(c, arg); - if (pname.len > 0 || argistuple) { + if (pname.len > 0 || argistuple || argistagged) { let ptype: *node = param.lhs; let scroff: i32 = tagscradd(c, widensz); emitline("\tXORQ\tAX, AX\n"); @@ -20017,25 +20046,48 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s cgwidentagremap(c, dt, src.type_: *tinfo, slot_off); return; }; + // #55: spill the AX/DX/CX/R8 cursor by the SOURCE box width + // (su.size), zero-pad to the dst slot, then tag-remap — the + // cursor twin of the ident / memread arms above. Pre-#55 it + // spilled by slot_sz (reading STALE high regs when the source is + // narrower) and skipped the pad + remap, so an INDEX/DOT tagged + // element widened into a wider/reordered union pushed a stale word + // and the un-remapped source tag (silent cs!=ww). Mirrors cstage + // cg_widen_tagged_store's cursor arm + shared pad/remap tail + // (cmd/w6c/cgen.c:2695-2714). Same-slot source (ssz==slot_sz) + // leaves the pad empty + an identity remap, so the emission is + // byte-identical to the prior arm for the exact-slot callers. cgexpr(c, src); + let ssz: i32 = su.size: i32; emitline("\tMOVQ\tAX, "); emitoff(slot_off: i64); emitline("(BP)\n"); - if (slot_sz > 8) { + if (ssz > 8) { emitline("\tMOVQ\tDX, "); emitoff((slot_off + 8): i64); emitline("(BP)\n"); }; - if (slot_sz > 16) { + if (ssz > 16) { emitline("\tMOVQ\tCX, "); emitoff((slot_off + 16): i64); emitline("(BP)\n"); }; - if (slot_sz > 24) { + if (ssz > 24) { emitline("\tMOVQ\tR8, "); emitoff((slot_off + 24): i64); emitline("(BP)\n"); }; + if (ssz < slot_sz) { + emitline("\tXORQ\tAX, AX\n"); + let pp: i32 = ssz; + for (pp < slot_sz) { + emitline("\tMOVQ\tAX, "); + emitoff((slot_off + pp): i64); + emitline("(BP)\n"); + pp += 8; + }; + }; + cgwidentagremap(c, dt, src.type_: *tinfo, slot_off); return; }; // #37 (rule 7): a >32B TAGGED source of a kind the resolver arms diff --git a/selfhost/cmd/wcc/cgenutil.ww b/selfhost/cmd/wcc/cgenutil.ww index 3cf4b3be..b8fbe8c3 100644 --- a/selfhost/cmd/wcc/cgenutil.ww +++ b/selfhost/cmd/wcc/cgenutil.ww @@ -261,7 +261,23 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { if (arg.kind == nkind.N_IDENT) { let lc: *local = localfindnode(c, arg.str); if (lc != nil) { - aistagged = istaggedtype(c, lc.tnode); + // #55: only treat a tagged ident as + // "already tagged" (natural push, no remap) + // when its slot MATCHES the param. On slot- + // DIFFER the source is a NARROWER union widened + // into a wider one — fall to the widen scratch + // + tag-remap below (the slot-gated INDEX/DOT/ + // STAR arms' twin). Pre-#55 the ungated TRUE + // natural-pushed the narrower box's words with + // no remap (aligned-tag LUCK, misaligned-tag + // wrong). cstage routes every tagged source + // through cg_widen_tagged_store (cmd/w6c/ + // cgen.c:2982 src_is_tagged). + if (istaggedtype(c, lc.tnode)) { + if (slotsize(c, lc.tnode) == slotsize(c, ptype)) { + aistagged = true; + }; + }; }; }; // #21: a CALL returning a tagged-union must @@ -355,13 +371,26 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { let argtup: *tinfo = arg.type_: *tinfo; argtup = tichase(argtup); let argistuple: bool = false; + // #55: a tagged SOURCE widened into a wider/reordered tagged param + // (slot-DIFFER — the ident/deref/index/dot legs that fell past the + // slot-gated aistagged arms above) has no direct-push shape: the + // scalar fast arm below would box word0 with a tag clamped to 0, + // dropping the real tag + high words and skipping the variant + // remap. Route through the @tagscr store, whose ident/memread/ + // cursor arms copy the box + tag-remap. Mirrors cstage + // cg_widen_tagged_push's unconditional src_is_tagged scratch + // routing (cmd/w6c/cgen.c:2982). + let argistagged: bool = false; if (argtup != nil) { if (argtup.kind == tykind.TY_TUPLE) { argistuple = true; }; + if (argtup.kind == tykind.TY_TAGGED) { + argistagged = true; + }; }; let pname: str = rhsstructpayload(c, arg); - if (pname.len > 0 || argistuple) { + if (pname.len > 0 || argistuple || argistagged) { let ptype: *node = param.lhs; let scroff: i32 = tagscradd(c, widensz); emitline("\tXORQ\tAX, AX\n"); @@ -3876,25 +3905,48 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s cgwidentagremap(c, dt, src.type_: *tinfo, slot_off); return; }; + // #55: spill the AX/DX/CX/R8 cursor by the SOURCE box width + // (su.size), zero-pad to the dst slot, then tag-remap — the + // cursor twin of the ident / memread arms above. Pre-#55 it + // spilled by slot_sz (reading STALE high regs when the source is + // narrower) and skipped the pad + remap, so an INDEX/DOT tagged + // element widened into a wider/reordered union pushed a stale word + // and the un-remapped source tag (silent cs!=ww). Mirrors cstage + // cg_widen_tagged_store's cursor arm + shared pad/remap tail + // (cmd/w6c/cgen.c:2695-2714). Same-slot source (ssz==slot_sz) + // leaves the pad empty + an identity remap, so the emission is + // byte-identical to the prior arm for the exact-slot callers. cgexpr(c, src); + let ssz: i32 = su.size: i32; emitline("\tMOVQ\tAX, "); emitoff(slot_off: i64); emitline("(BP)\n"); - if (slot_sz > 8) { + if (ssz > 8) { emitline("\tMOVQ\tDX, "); emitoff((slot_off + 8): i64); emitline("(BP)\n"); }; - if (slot_sz > 16) { + if (ssz > 16) { emitline("\tMOVQ\tCX, "); emitoff((slot_off + 16): i64); emitline("(BP)\n"); }; - if (slot_sz > 24) { + if (ssz > 24) { emitline("\tMOVQ\tR8, "); emitoff((slot_off + 24): i64); emitline("(BP)\n"); }; + if (ssz < slot_sz) { + emitline("\tXORQ\tAX, AX\n"); + let pp: i32 = ssz; + for (pp < slot_sz) { + emitline("\tMOVQ\tAX, "); + emitoff((slot_off + pp): i64); + emitline("(BP)\n"); + pp += 8; + }; + }; + cgwidentagremap(c, dt, src.type_: *tinfo, slot_off); return; }; // #37 (rule 7): a >32B TAGGED source of a kind the resolver arms diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index b0424502..919c6dd5 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -16402,7 +16402,23 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { if (arg.kind == nkind.N_IDENT) { let lc: *local = localfindnode(c, arg.str); if (lc != nil) { - aistagged = istaggedtype(c, lc.tnode); + // #55: only treat a tagged ident as + // "already tagged" (natural push, no remap) + // when its slot MATCHES the param. On slot- + // DIFFER the source is a NARROWER union widened + // into a wider one — fall to the widen scratch + // + tag-remap below (the slot-gated INDEX/DOT/ + // STAR arms' twin). Pre-#55 the ungated TRUE + // natural-pushed the narrower box's words with + // no remap (aligned-tag LUCK, misaligned-tag + // wrong). cstage routes every tagged source + // through cg_widen_tagged_store (cmd/w6c/ + // cgen.c:2982 src_is_tagged). + if (istaggedtype(c, lc.tnode)) { + if (slotsize(c, lc.tnode) == slotsize(c, ptype)) { + aistagged = true; + }; + }; }; }; // #21: a CALL returning a tagged-union must @@ -16496,13 +16512,26 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node, memphase: bool) i32 = { let argtup: *tinfo = arg.type_: *tinfo; argtup = tichase(argtup); let argistuple: bool = false; + // #55: a tagged SOURCE widened into a wider/reordered tagged param + // (slot-DIFFER — the ident/deref/index/dot legs that fell past the + // slot-gated aistagged arms above) has no direct-push shape: the + // scalar fast arm below would box word0 with a tag clamped to 0, + // dropping the real tag + high words and skipping the variant + // remap. Route through the @tagscr store, whose ident/memread/ + // cursor arms copy the box + tag-remap. Mirrors cstage + // cg_widen_tagged_push's unconditional src_is_tagged scratch + // routing (cmd/w6c/cgen.c:2982). + let argistagged: bool = false; if (argtup != nil) { if (argtup.kind == tykind.TY_TUPLE) { argistuple = true; }; + if (argtup.kind == tykind.TY_TAGGED) { + argistagged = true; + }; }; let pname: str = rhsstructpayload(c, arg); - if (pname.len > 0 || argistuple) { + if (pname.len > 0 || argistuple || argistagged) { let ptype: *node = param.lhs; let scroff: i32 = tagscradd(c, widensz); emitline("\tXORQ\tAX, AX\n"); @@ -20017,25 +20046,48 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s cgwidentagremap(c, dt, src.type_: *tinfo, slot_off); return; }; + // #55: spill the AX/DX/CX/R8 cursor by the SOURCE box width + // (su.size), zero-pad to the dst slot, then tag-remap — the + // cursor twin of the ident / memread arms above. Pre-#55 it + // spilled by slot_sz (reading STALE high regs when the source is + // narrower) and skipped the pad + remap, so an INDEX/DOT tagged + // element widened into a wider/reordered union pushed a stale word + // and the un-remapped source tag (silent cs!=ww). Mirrors cstage + // cg_widen_tagged_store's cursor arm + shared pad/remap tail + // (cmd/w6c/cgen.c:2695-2714). Same-slot source (ssz==slot_sz) + // leaves the pad empty + an identity remap, so the emission is + // byte-identical to the prior arm for the exact-slot callers. cgexpr(c, src); + let ssz: i32 = su.size: i32; emitline("\tMOVQ\tAX, "); emitoff(slot_off: i64); emitline("(BP)\n"); - if (slot_sz > 8) { + if (ssz > 8) { emitline("\tMOVQ\tDX, "); emitoff((slot_off + 8): i64); emitline("(BP)\n"); }; - if (slot_sz > 16) { + if (ssz > 16) { emitline("\tMOVQ\tCX, "); emitoff((slot_off + 16): i64); emitline("(BP)\n"); }; - if (slot_sz > 24) { + if (ssz > 24) { emitline("\tMOVQ\tR8, "); emitoff((slot_off + 24): i64); emitline("(BP)\n"); }; + if (ssz < slot_sz) { + emitline("\tXORQ\tAX, AX\n"); + let pp: i32 = ssz; + for (pp < slot_sz) { + emitline("\tMOVQ\tAX, "); + emitoff((slot_off + pp): i64); + emitline("(BP)\n"); + pp += 8; + }; + }; + cgwidentagremap(c, dt, src.type_: *tinfo, slot_off); return; }; // #37 (rule 7): a >32B TAGGED source of a kind the resolver arms diff --git a/test/wcc/944_tagged_widen_arg_run.c b/test/wcc/944_tagged_widen_arg_run.c new file mode 100644 index 00000000..9d1164b4 --- /dev/null +++ b/test/wcc/944_tagged_widen_arg_run.c @@ -0,0 +1,287 @@ +/* + * 944_tagged_widen_arg_run — #55 tagged-SOURCE arg widened into a wider + * tagged-union PARAM slot (slot-DIFFER). + * + * A call arg whose own type is a NARROWER tagged union (un16=16B) passed + * where the param is a WIDER tagged union (un3=24B). wwstage pushargsrev + * mis-pushed every leg but the lucky aligned-ident: the slot-DIFFER arg + * fell past the slot-gated aistagged arms to the concrete-variant scalar + * boxing (taggedvariantindex<0 → tag clamped to 0 → word0 boxed, real tag + * + high words dropped, NO variant remap). cstage routes EVERY tagged + * source through cg_widen_tagged_store (cgen.c:2982 src_is_tagged) → + * scratch + tag-remap, correct on all 8. + * + * The fix (ww align-UP, 3 coordinated arms in cgenutil.ww): + * 1. the IDENT aistagged gate is now slot-gated (was ungated TRUE) so a + * slot-DIFFER ident falls to the widen scratch instead of the no- + * remap natural push (the prefix-luck); + * 2. pushargsrev's widensz>0 arm routes a tagged source (argistagged) + * through @tagscr + cgwidentaggedstore + push high→low (the + * pname/argistuple twin); + * 3. cgwidentaggedstore's cursor arm (INDEX/DOT ≤32B) spills by the + * SOURCE width, zero-pads, and tag-REMAPS (mirrors cstage's + * cg_widen_tagged_store cursor + shared pad/remap tail, cgen.c:2695- + * 2714) — pre-#55 it spilled by the dst slot (stale high regs) and + * skipped the remap. + * + * row | leg | tag | want + * ----------------+-------+-----------+----- + * ident_aligned | e | i64=tag0 | 42 + * deref_aligned | *p | i64=tag0 | 42 + * index_aligned | xs[0] | i64=tag0 | 42 + * dot_aligned | w.f | i64=tag0 | 42 + * ident_remap | e | i64=tag1 | 42 (un16b reorders i64→tag1) + * deref_remap | *p | i64=tag1 | 42 + * index_remap | xs[0] | i64=tag1 | 42 + * dot_remap | w.f | i64=tag1 | 42 + * + * The remap rows carry the MISALIGNED tag: un16b=(bool|i64) puts i64 at + * tag1, widened into un3b=(i64|bool|s16) where i64 is tag0 — so the source + * tag MUST be rewritten or the callee reads the wrong arm. An aligned-only + * suite false-greens a remap-less fix (the exact trap here, ken #55). + * + * Pre-fix: deref/index/dot legs cs42/ww!=42 (silent wrong); aligned-ident + * ww42 by LUCK but asm cs!=ww. Post-fix: all 8 cs42/ww42, all 8 byte-id. + */ +#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; +} + +struct row { const char *label; const char *src; int want; }; + +#define TAKE_ALIGNED \ + "type s16 = struct { a: i64, b: i64 };\n" \ + "type un16 = (i64 | bool);\n" \ + "type un3 = (i64 | bool | s16);\n" \ + "fn take(e: un3) i64 = {\n" \ + " match (e) {\n" \ + " case let v: i64 => { return v; };\n" \ + " case let b: bool => { return -1; };\n" \ + " case let s: s16 => { return s.a + s.b; };\n" \ + " };\n" \ + "};\n" + +#define TAKE_REMAP \ + "type s16 = struct { a: i64, b: i64 };\n" \ + "type un16b = (bool | i64);\n" \ + "type un3b = (i64 | bool | s16);\n" \ + "fn take(e: un3b) i64 = {\n" \ + " match (e) {\n" \ + " case let v: i64 => { return v; };\n" \ + " case let b: bool => { return -1; };\n" \ + " case let s: s16 => { return s.a + s.b; };\n" \ + " };\n" \ + "};\n" + +static const struct row rows[] = { + { "ident_aligned", + "package main;\n" TAKE_ALIGNED + "export fn main() i32 = {\n" + " let v: un16 = (42: i64);\n" + " if (take(v) != 42) { return 1; };\n" + " return 0;\n" + "};\n", 0 }, + { "deref_aligned", + "package main;\n" TAKE_ALIGNED + "export fn main() i32 = {\n" + " let v: un16 = (42: i64);\n" + " let p: *un16 = &v;\n" + " if (take(*p) != 42) { return 1; };\n" + " return 0;\n" + "};\n", 0 }, + { "index_aligned", + "package main;\n" TAKE_ALIGNED + "export fn main() i32 = {\n" + " let xs: [2]un16 = [(42: i64), (7: i64)];\n" + " if (take(xs[0]) != 42) { return 1; };\n" + " return 0;\n" + "};\n", 0 }, + { "dot_aligned", + "package main;\n" TAKE_ALIGNED + "type wrap = struct { f: un16 };\n" + "export fn main() i32 = {\n" + " let w: wrap = wrap { f = (42: i64) };\n" + " if (take(w.f) != 42) { return 1; };\n" + " return 0;\n" + "};\n", 0 }, + /* misaligned-tag rows — i64 is tag1 in un16b, tag0 in un3b: the + * remap MUST fire or the callee reads the wrong match arm. */ + { "ident_remap", + "package main;\n" TAKE_REMAP + "export fn main() i32 = {\n" + " let v: un16b = (42: i64);\n" + " if (take(v) != 42) { return 1; };\n" + " return 0;\n" + "};\n", 0 }, + { "deref_remap", + "package main;\n" TAKE_REMAP + "export fn main() i32 = {\n" + " let v: un16b = (42: i64);\n" + " let p: *un16b = &v;\n" + " if (take(*p) != 42) { return 1; };\n" + " return 0;\n" + "};\n", 0 }, + { "index_remap", + "package main;\n" TAKE_REMAP + "export fn main() i32 = {\n" + " let xs: [2]un16b = [(42: i64), (7: i64)];\n" + " if (take(xs[0]) != 42) { return 1; };\n" + " return 0;\n" + "};\n", 0 }, + { "dot_remap", + "package main;\n" TAKE_REMAP + "type wrap = struct { f: un16b };\n" + "export fn main() i32 = {\n" + " let w: wrap = wrap { f = (42: i64) };\n" + " if (take(w.f) != 42) { return 1; };\n" + " return 0;\n" + "};\n", 0 }, +}; + +static int +run_driver(const char *driver, const struct row *r, int i) +{ + char src[96], tmpdir[96], errf[96], cmd[1024]; + snprintf(src, sizeof src, "/tmp/twa_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/twa_%d_d_%d", getpid(), i); + snprintf(errf, sizeof errf, "/tmp/twa_%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 (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; +} + +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/twa_asm_%d_%d.ww", getpid(), i); + snprintf(cs, sizeof cs, "/tmp/twa_asm_%d_%d_c.s", getpid(), i); + snprintf(ws, sizeof ws, "/tmp/twa_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++; + if (run_driver(cdrv, &rows[i], i) != 0) fail++; + } + if (access(wdrv, X_OK) == 0) { + for (int i = 0; i < n; i++) { + total++; + if (run_driver(wdrv, &rows[i], i) != 0) fail++; + } + for (int i = 0; i < n; i++) { + total++; + if (asm_byte_identical(bin, &rows[i], i) != 0) fail++; + } + } + + if (fail) { + fprintf(stderr, "tagged_widen_arg: %d/%d checks failed\n", + fail, total); + return 1; + } + printf("tagged_widen_arg: %d/%d ok\n", total, total); + return 0; +}