/* * 701_cgassign_struct — whole-struct receive ABI for sizes <= 24B * (receive side of #4's cgreturn, task #5 #27). * * #4 wired the producer side: `return s;` materialises bytes into * AX (bytes 0..7), DX (8..15), CX (16..23), zero-padded to 24B. The * #4 test (698_cgreturn_struct.c) only pins that cgreturn doesn't * crash; the result is discarded by the caller. #5 wires the * receive side end-to-end: * * - `let s: foo = bar()` — N_LET call-result rhs lands AX/DX/CX * into the slot with sized stores (MOVQ for full 8B chunks, * MOVL/MOVB/MOVW tail by *declared* struct size — the receiver * must NOT mirror the sender's three uniform MOVQs, else * trailing 1..7 bytes overrun the next slot). * - `let s: foo = foo{...}` — already wired by #4's predecessor; * this test pins the value path. * - `s = bar()` reassign — symmetric N_ASSIGN N_IDENT-lhs. * - `s = foo{...}` reassign — symmetric N_STRUCTLIT shape. * * What this test pins: * - cstage and wwstage emit byte-identical asm for every fixture * (catches any drift in the receive-side store sequence). * - END-TO-END VALUE CORRECTNESS: each fixture's main returns a * deterministic exit code derived from the received struct's * fields. A regression in the receive ABI (e.g., DX→+0 instead * of +8, or an over-wide tail MOVQ overrunning the slot) shows * up as a `got != want` exit-code mismatch, not a no-crash * pass. * * Coverage: * - 8B (one_i64): smallest struct, only AX is meaningful. * - 16B (pair_i64): two-word AX/DX. * - 20B (five_i32): three-word AX/DX/CX with MOVL tail — the * headline ASYMMETRY case. A naïve MOVQ tail here would stomp * 4 bytes past the slot. * - 24B (trip_i64): three-word, no tail. * - 24B mix (i32+i32+i64+i64): registerstruct-packed shape. * * Each shape covers two rhs forms (call-result, struct-literal) * across two lvalue forms (let-init, reassign). * * Sizes >24B are OUT OF SCOPE — sret is deferred (same constraint * as #4). Tail chunks in {3,5,6,7} are unreachable under WW * struct alignment rules (field aligns force size%align == 0); the * receive branches guard those out and fall through. */ #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; } struct row { const char *label; const char *src; int want; }; static const struct row rows[] = { /* 8B, call-result let-init. exit = s.v (= 7). */ { "one_i64_let_call", "type one = struct { v: i64 };\n" "fn mk() one = { return one { v = 7i64 }; };\n" "fn main() i32 = {\n" " let s: one = mk();\n" " return s.v: i32;\n" "};\n", 7 }, /* 8B, struct-literal let-init. exit = s.v (= 11). */ { "one_i64_let_lit", "type one = struct { v: i64 };\n" "fn main() i32 = {\n" " let s: one = one { v = 11i64 };\n" " return s.v: i32;\n" "};\n", 11 }, /* 8B, call-result reassign. exit = s.v (= 13). */ { "one_i64_reassign_call", "type one = struct { v: i64 };\n" "fn mk() one = { return one { v = 13i64 }; };\n" "fn main() i32 = {\n" " let s: one = one { v = 0i64 };\n" " s = mk();\n" " return s.v: i32;\n" "};\n", 13 }, /* 8B, struct-literal reassign. exit = s.v (= 17). */ { "one_i64_reassign_lit", "type one = struct { v: i64 };\n" "fn main() i32 = {\n" " let s: one = one { v = 0i64 };\n" " s = one { v = 17i64 };\n" " return s.v: i32;\n" "};\n", 17 }, /* 16B, call-result let-init. exit = a + b (= 3 + 5 = 8). * Pins that DX lands at +8 and AX at +0. */ { "pair_i64_let_call", "type pair = struct { a: i64, b: i64 };\n" "fn mk() pair = { return pair { a = 3i64, b = 5i64 }; };\n" "fn main() i32 = {\n" " let s: pair = mk();\n" " return (s.a + s.b): i32;\n" "};\n", 8 }, /* 16B, struct-literal reassign. exit = a + b (= 4 + 6 = 10). */ { "pair_i64_reassign_lit", "type pair = struct { a: i64, b: i64 };\n" "fn main() i32 = {\n" " let s: pair = pair { a = 0i64, b = 0i64 };\n" " s = pair { a = 4i64, b = 6i64 };\n" " return (s.a + s.b): i32;\n" "};\n", 10 }, /* 20B (five-i32), call-result let-init — the ASYMMETRY * headline case. AX bytes 0..7 hold a,b. DX bytes 8..15 hold * c,d. CX low 4 bytes hold e; CX upper 4 bytes are pad zero. * The receiver MUST store CX as MOVL (4B), not MOVQ (8B); * a MOVQ would overrun into the next local slot. Sum check: * 1+2+3+4+5 = 15. */ { "five_i32_let_call", "type five = struct { a: i32, b: i32, c: i32, d: i32, e: i32 };\n" "fn mk() five = {\n" " return five { a = 1, b = 2, c = 3, d = 4, e = 5 };\n" "};\n" "fn main() i32 = {\n" " let s: five = mk();\n" " return s.a + s.b + s.c + s.d + s.e;\n" "};\n", 15 }, /* 20B (five-i32), struct-literal reassign. Same tail-MOVL * pin as above but exercises the structlit-field-walk path. */ { "five_i32_reassign_lit", "type five = struct { a: i32, b: i32, c: i32, d: i32, e: i32 };\n" "fn main() i32 = {\n" " let s: five = five { a = 0, b = 0, c = 0, d = 0, e = 0 };\n" " s = five { a = 2, b = 4, c = 6, d = 8, e = 10 };\n" " return s.a + s.b + s.c + s.d + s.e;\n" "};\n", 30 }, /* 24B, call-result let-init — the headline three-word ABI. * AX/DX/CX each carry one full word, no tail. Sum check: * 11+22+33 = 66. */ { "trip_i64_let_call", "type trip = struct { x: i64, y: i64, z: i64 };\n" "fn mk() trip = { return trip { x = 11i64, y = 22i64, z = 33i64 }; };\n" "fn main() i32 = {\n" " let s: trip = mk();\n" " return (s.x + s.y + s.z): i32;\n" "};\n", 66 }, /* 24B, struct-literal reassign. Same word-shape as above. */ { "trip_i64_reassign_lit", "type trip = struct { x: i64, y: i64, z: i64 };\n" "fn main() i32 = {\n" " let s: trip = trip { x = 0i64, y = 0i64, z = 0i64 };\n" " s = trip { x = 14i64, y = 28i64, z = 42i64 };\n" " return (s.x + s.y + s.z): i32;\n" "};\n", 84 }, /* 24B mix (registerstruct-packed): {i32, i32, i64, i64}. * Layout: a@+0, b@+4, c@+8, d@+16 (totsize 24). AX carries * a+b packed, DX = c, CX = d. Sum check: 1+2+3+4 = 10. */ { "mix_let_call", "type mix = struct { a: i32, b: i32, c: i64, d: i64 };\n" "fn mk() mix = { return mix { a = 1, b = 2, c = 3i64, d = 4i64 }; };\n" "fn main() i32 = {\n" " let s: mix = mk();\n" " return (s.a + s.b) + (s.c + s.d): i32;\n" "};\n", 10 }, /* dst.field = call() — single-dot struct-typed field via * local struct base. Exercises the cgen.c:1996+ N_DOT.N_IDENT * struct-field branch (cstage) and cgenexpr.ww:~4046 single- * dot local-base site (wwstage). Sum check: 3+4+0 = 7. */ { "field_local_call", "type inner = struct { a: i64, b: i64 };\n" "type outer = struct { i: inner, t: i64 };\n" "fn mki() inner = { return inner { a = 3i64, b = 4i64 }; };\n" "fn main() i32 = {\n" " let o: outer = outer { i = inner { a = 0i64, b = 0i64 }, t = 0i64 };\n" " o.i = mki();\n" " return (o.i.a + o.i.b + o.t): i32;\n" "};\n", 7 }, /* dst.field = T{...} — same single-dot site, struct-literal * rhs. Sum check: 11+22+5 = 38. */ { "field_local_lit", "type inner = struct { a: i64, b: i64 };\n" "type outer = struct { i: inner, t: i64 };\n" "fn main() i32 = {\n" " let o: outer = outer { i = inner { a = 0i64, b = 0i64 }, t = 5i64 };\n" " o.i = inner { a = 11i64, b = 22i64 };\n" " return (o.i.a + o.i.b + o.t): i32;\n" "};\n", 38 }, /* dst.field = call() via *struct base (auto-deref through * pointer). Exercises cgenexpr.ww:~3752 single-dot ptr-base * site (wwstage). Sum check: 30+40+100 = 170. */ { "field_ptr_call", "type inner = struct { a: i64, b: i64 };\n" "type outer = struct { i: inner, t: i64 };\n" "fn mki() inner = { return inner { a = 30i64, b = 40i64 }; };\n" "fn main() i32 = {\n" " let o: outer = outer { i = inner { a = 0i64, b = 0i64 }, t = 100i64 };\n" " let p: *outer = &o;\n" " p.i = mki();\n" " return (o.i.a + o.i.b + o.t): i32;\n" "};\n", 170 }, /* dst.field = T{...} via *struct base, struct-literal rhs. * Sum check: 7+8+50 = 65. */ { "field_ptr_lit", "type inner = struct { a: i64, b: i64 };\n" "type outer = struct { i: inner, t: i64 };\n" "fn main() i32 = {\n" " let o: outer = outer { i = inner { a = 0i64, b = 0i64 }, t = 50i64 };\n" " let p: *outer = &o;\n" " p.i = inner { a = 7i64, b = 8i64 };\n" " return (o.i.a + o.i.b + o.t): i32;\n" "};\n", 65 }, /* dst.field = call() via top-level let-global base. * Exercises cgenexpr.ww:~4352 single-dot global-base site * (wwstage) and the corresponding cstage is_global path. * `let g: T;` (no init) avoids the module-name-mangle path * that initialised globals hit (cf. task #17). Sum check: * 70+80+100 = 250 (≤ 255, fits in process exit code). */ { "field_global_call", "type inner = struct { a: i64, b: i64 };\n" "type outer = struct { i: inner, t: i64 };\n" "let g: outer;\n" "fn mki() inner = { return inner { a = 70i64, b = 80i64 }; };\n" "fn main() i32 = {\n" " g.t = 100i64;\n" " g.i = mki();\n" " return (g.i.a + g.i.b + g.t): i32;\n" "};\n", 250 }, /* Chained `s.f.g = call()` — depth-2 dot lhs. Exercises the * cstage chained walker (cgen.c:~2626) and wwstage walker * (cgenexpr.ww:~4381). The initialiser uses explicit field * writes (not a nested struct-literal), since cgen's nested- * STRUCTLIT-as-field-value path is a separate gap. Bare * `let o: outer;` zero-fills the slot. Sums sized to fit a * process exit code (8 bits): 5+6+10+20 = 41. */ { "field_chain_call", "type inner = struct { a: i64, b: i64 };\n" "type middle = struct { in: inner, t: i64 };\n" "type outer = struct { m: middle, x: i64 };\n" "fn mki() inner = { return inner { a = 5i64, b = 6i64 }; };\n" "fn main() i32 = {\n" " let o: outer;\n" " o.m.t = 10i64;\n" " o.x = 20i64;\n" " o.m.in = mki();\n" " return (o.m.in.a + o.m.in.b + o.m.t + o.x): i32;\n" "};\n", 41 }, /* Chained `s.f.g = T{...}` — depth-2 struct-literal rhs. * Sum check: 11+22+10+20 = 63. */ { "field_chain_lit", "type inner = struct { a: i64, b: i64 };\n" "type middle = struct { in: inner, t: i64 };\n" "type outer = struct { m: middle, x: i64 };\n" "fn main() i32 = {\n" " let o: outer;\n" " o.m.t = 10i64;\n" " o.x = 20i64;\n" " o.m.in = inner { a = 11i64, b = 22i64 };\n" " return (o.m.in.a + o.m.in.b + o.m.t + o.x): i32;\n" "};\n", 63 }, }; static int run_driver(const char *driver, const struct row *r, int i) { char tmpdir[64], src[128], outbin[128], rmcmd[160], cmd[1024]; snprintf(tmpdir, sizeof tmpdir, "/tmp/wcas_%d_d_%d", getpid(), i); mkdir(tmpdir, 0755); snprintf(src, sizeof src, "%s/wcas_%d_%d.ww", tmpdir, getpid(), i); snprintf(outbin, sizeof outbin, "%s/wcas_%d_%d", tmpdir, getpid(), i); snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir); FILE *f = fopen(src, "wb"); if (!f) { runwait(rmcmd); return -1; } 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; } /* asm_byte_identical — generate .s via cstage's w6c and wwstage's * w6c_ww and diff. Catches receive-side codegen drift between the * stages, which 995_self_rebuild covers globally but doesn't surface * as a focused-fixture failure. */ static int asm_byte_identical(const char *bin, const struct row *r, int i) { char src[64], cs[64], ws[64], cmd[1024]; snprintf(src, sizeof src, "/tmp/wcas_asm_%d_%d.ww", getpid(), i); snprintf(cs, sizeof cs, "/tmp/wcas_asm_%d_%d_c.s", getpid(), i); snprintf(ws, sizeof ws, "/tmp/wcas_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; } FILE *fc = fopen(cs, "rb"); FILE *fw = fopen(ws, "rb"); int rc = 0; if (!fc || !fw) { rc = -1; } else { for (;;) { int a = fgetc(fc); int b = fgetc(fw); if (a != b) { rc = -1; break; } if (a == EOF) break; } } if (fc) fclose(fc); if (fw) fclose(fw); 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[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; /* Compile+run for each (driver, row). */ for (int d = 0; drivers[d].name; d++) { if (drivers[d].gated_on_existence && access(drivers[d].path, X_OK) != 0) { fprintf(stderr, "cgassign_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, "cgassign_struct[%s][%s]: exit=%d want=%d\n", drivers[d].name, rows[i].label, got, rows[i].want); fail++; } } } /* Asm byte-identity diff, only when both stages exist. */ if (access(wdrv, X_OK) == 0) { for (int i = 0; i < n; i++) { total++; if (asm_byte_identical(bin, &rows[i], i) != 0) fail++; } } if (fail) { fprintf(stderr, "cgassign_struct: %d/%d fixtures failed\n", fail, total); return 1; } printf("cgassign_struct: %d/%d ok\n", total, total); return 0; }