/* * 695_match_bind_struct — `case let v: T => ...` where T is a TY_STRUCT * variant of a tagged-union scrutinee. wwstage's cgmatch (and the * matching scanlocals frame pre-scan) hardcoded the bind-slot size to * * str -> 16 * []T -> 24 * else -> 8 * * so a struct variant collapsed to a single 8B word: only the first * quadword reached the per-arm bind slot, and (worse) the prologue's * SUBQ underbooked the frame, so localalloc at emit time then ran the * SP cursor off the bottom of the function's frame and the bind's * subsequent stores smashed whatever was past it. * * cstage cgen.c is fine: the else-branch falls through to * `bsz = (int)bu->size` and the SP cursor / cg_frame counter both * ride the same `bu->size`. Worker-28 noted this while landing #28, * and a reread of cstage's cgmatch (cgen.c ~3998-4017) and stack- * frame setup confirms — no cstage mirror needed. * * Closed in task #31 by: * - selfhost/cmd/wcc/cgenexpr.ww: cgmatch bsz table replaced with * `slotsize(c, pat)`. slotsize already covers N_TNAME named struct * (returns si.totsize), str (16), []T (24), aliases, tuples, and * primitives (8). Defensive nwords = (bsz + 7) / 8 mirrors cstage. * - selfhost/cmd/wcc/cgendecl.ww: scanlocals N_MCASE pre-scan * mirrored — must agree with cgmatch's bind-slot size so the * prologue SUBQ reserves enough frame. * - selfhost/cmd/{w6c,wwdump}/main.combined.ww: combined-file mirrors * of both sites kept in lockstep. * * Coverage — seven rows: four structurally distinct TY_STRUCT shapes * plus three negative controls. The 3xi64 headline repro, a 4-i64 * struct via let-init scrutinee (exercise > 24B payload, so the * historical 24-cap doesn't quietly pass), a mixed-quadword struct * with i32 alignment + i64 tail (size still pads to a multiple of 8), * str/slice/i32 negative controls (the existing sized paths must not * regress), and a direct `let x: tag = (pair{...}: tag);` scrutinee * (different from `match (h.e)` — exercises cglet's tagged-init * route to the same bind site). */ #include #include #include #include #include #include "wwtestpkg.h" 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 *label; const char *src; int want; }; static const struct row rows[] = { /* Headline repro. 3xi64 = 24B struct as a tagged-union variant. * Bind copies all three quadwords; sum to 31. Pre-fix only v.x * reached the bind and v.y/v.z were uninitialized stack — exit * was nondeterministic but never 0. */ { "pair_3xi64", "type pair = struct { x: i64, y: i64, z: i64 };\n" "type tag = (pair | i32);\n" "type holder = struct { e: tag, mark: i32 };\n" "fn main() i32 = {\n" " let h: holder;\n" " h.mark = 99;\n" " let p: pair = pair { x = 7i64, y = 11i64, z = 13i64 };\n" " h.e = (p: tag);\n" " match (h.e) {\n" " case let v: pair => {\n" " let s: i64 = v.x + v.y + v.z;\n" " if (s != 31i64) { return 1; };\n" " return 0;\n" " };\n" " case let z: i32 => { return 2; };\n" " };\n" "};\n", 0 }, /* 4xi64 = 32B struct payload, exercising a >24B bind copy via the * direct-let-init scrutinee shape (`match (x)` over an ident, * bypassing the N_DOT spill path). Hardcoding a 24B cap (an * obvious fix-sketch hack — "if struct then 24") would drop * v.d and the sum would miss 24, returning 1 not 0. * * The N_DOT route through `match (h.t)` for a 32B-payload variant * is a separate, unrelated gap: cgmatch's scrut spill only writes * AX/DX/CX/R8 (4 registers, one of which is the tag), so the 4th * payload quadword is dropped at spill time — not at bind time. * Cstage sidesteps this with a direct-field addressing special- * case in cgmatch (cgen.c ~3858-3876) that skips the spill * entirely; wwstage lacks that path. Filing that as a follow-up; * the let-init scrutinee already exercises the bind size for * payloads > 24B without entangling the two bugs. */ { "wide_4xi64_letinit", "type wide = struct { a: i64, b: i64, c: i64, d: i64 };\n" "type tag = (wide | i32);\n" "fn main() i32 = {\n" " let x: tag = (wide { a = 1i64, b = 2i64, c = 4i64,\n" " d = 24i64 }: tag);\n" " match (x) {\n" " case let v: wide => {\n" " let s: i64 = v.a + v.b + v.c + v.d;\n" " if (s != 31i64) { return 1; };\n" " return 0;\n" " };\n" " case let z: i32 => { return 2; };\n" " };\n" "};\n", 0 }, /* Mixed-quadword: i32+i32+i64+i64. registerstruct packs the two * i32s into one quadword (x@0, y@4, z@8, w@16), totsize = 24 * (after the trailing pad-to-8). Bind must copy all three * quadwords; reading w must see the right value. */ { "mixed_i32_i32_i64_i64", "type mix = struct { x: i32, y: i32, z: i64, w: i64 };\n" "type tag = (mix | i32);\n" "type holder = struct { t: tag, mark: i32 };\n" "fn main() i32 = {\n" " let h: holder;\n" " h.mark = 99;\n" " let m: mix = mix { x = 3, y = 5, z = 11i64, w = 12i64 };\n" " h.t = (m: tag);\n" " match (h.t) {\n" " case let v: mix => {\n" " if (v.x != 3) { return 10; };\n" " if (v.y != 5) { return 11; };\n" " if (v.z != 11i64) { return 12; };\n" " if (v.w != 12i64) { return 13; };\n" " return 0;\n" " };\n" " case let z: i32 => { return 2; };\n" " };\n" "};\n", 0 }, /* Negative control: str variant in (str | pair). Pins that the * fix didn't shadow the existing 16B str bind. */ { "str_neg_control", "type pair = struct { x: i64, y: i64, z: i64 };\n" "type tag = (str | pair);\n" "type holder = struct { e: tag, mark: i32 };\n" "fn main() i32 = {\n" " let h: holder;\n" " h.mark = 99;\n" " h.e = (\"hello\": tag);\n" " match (h.e) {\n" " case let s: str => { return s.len: i32; };\n" " case let v: pair => { return -1; };\n" " };\n" "};\n", 5 }, /* Negative control: []u8 variant in ([]u8 | pair). Pins 24B slice * bind. */ { "slice_neg_control", "type pair = struct { x: i64, y: i64, z: i64 };\n" "type tag = ([]u8 | pair);\n" "type holder = struct { e: tag, mark: i32 };\n" "fn main() i32 = {\n" " let raw: [8]u8;\n" " raw[0] = 1u8; raw[1] = 2u8; raw[2] = 3u8;\n" " let h: holder;\n" " h.mark = 99;\n" " h.e = (raw[0:3]: tag);\n" " match (h.e) {\n" " case let v: []u8 => {\n" " if (v.cap != 8) { return 20; };\n" " return v.len: i32;\n" " };\n" " case let p: pair => { return -1; };\n" " };\n" "};\n", 3 }, /* Negative control: primitive i32 variant — pins the 8B fall- * through. */ { "i32_neg_control", "type pair = struct { x: i64, y: i64, z: i64 };\n" "type tag = (pair | i32);\n" "type holder = struct { e: tag, mark: i32 };\n" "fn main() i32 = {\n" " let h: holder;\n" " h.mark = 99;\n" " h.e = (42i32: tag);\n" " match (h.e) {\n" " case let v: pair => { return -1; };\n" " case let z: i32 => { return z; };\n" " };\n" "};\n", 42 }, /* Direct `let x: tag = (struct: tag);` scrutinee — different * write site than the holder-field assign route, but the bind * read-side hits the same cgmatch table. Pins the let-init * shape. */ { "letinit_direct_scrutinee", "type pair = struct { x: i64, y: i64, z: i64 };\n" "type tag = (pair | i32);\n" "fn main() i32 = {\n" " let x: tag = (pair { x = 7i64, y = 11i64, z = 13i64 }: tag);\n" " match (x) {\n" " case let v: pair => {\n" " let s: i64 = v.x + v.y + v.z;\n" " if (s != 31i64) { return 1; };\n" " return 0;\n" " };\n" " case let z: i32 => { return 2; };\n" " };\n" "};\n", 0 }, }; static int run_driver(const char *driver, const struct row *r, int i) { char src[128], tmpdir[64], outbin[128], rmcmd[160], cmd[1024]; snprintf(tmpdir, sizeof tmpdir, "/tmp/wmbs_%d_d_%d", getpid(), i); mkdir(tmpdir, 0755); snprintf(src, sizeof src, "%s/wmbs_%d_%d.ww", tmpdir, getpid(), i); snprintf(outbin, sizeof outbin, "%s/wmbs_%d_%d", tmpdir, getpid(), i); snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir); FILE *f = fopen(src, "wb"); if (!f) { runwait(rmcmd); return -1; } wwtest_fputs(r->src, f); fclose(f); snprintf(cmd, sizeof cmd, "%s build -o %s %s", driver, outbin, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: build via %s failed\n", r->label, driver); runwait(rmcmd); return -1; } int got = runwait(outbin); runwait(rmcmd); return got; } 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 cdrv[1024]; snprintf(cdrv, sizeof cdrv, "%s/ww", bin); char wdrv[1024]; snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); struct { const char *name; const char *path; int gated_on_existence; } drivers[] = { { "cstage", cdrv, 0 }, { "wwstage", wdrv, 1 }, { NULL, NULL, 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_on_existence && access(drivers[d].path, X_OK) != 0) { fprintf(stderr, "match_bind_struct: skip %s (no %s)\n", drivers[d].name, drivers[d].path); continue; } for (int i = 0; i < n; i++) { int got = run_driver(drivers[d].path, &rows[i], i); total++; if (got != rows[i].want) { fprintf(stderr, "match_bind_struct[%s][%s]: exit=%d want=%d\n", drivers[d].name, rows[i].label, got, rows[i].want); fail++; } } } if (fail) { fprintf(stderr, "match_bind_struct: %d/%d fixtures failed\n", fail, total); return 1; } printf("match_bind_struct: %d/%d ok\n", total, total); return 0; }