Files
ww/test/wcc/701_cgassign_struct.c
Hojun-Cho f2643a846a cstage+selfhost+test: cgen N_ASSIGN whole-STRUCT (call+structlit, 5 sites)
Receive side of #4's cgreturn ABI (aee8149) for TY_STRUCT lvalues of
size <=24B. Producer materialises rhs into AX=bytes[0..7], DX=[8..15],
CX=[16..23], zero-padded to 24B; receive sites here read the regs and
write only `declared sz` bytes — MOVQ for full 8B chunks plus a sized
tail (MOVL/MOVW/MOVB) by the *declared* struct size. ASYMMETRY: do NOT
mirror the sender's three uniform MOVQs, else trailing 1..7B chunks
overrun the next local slot. Tail chunks in {3,5,6,7} are unreachable
under WW struct align rules (size%align==0) and fall through.

Five sites wired in each stage (cstage cgen.c, wwstage cgenexpr.ww +
cgenstmt.ww), call-result + structlit rhs at each:
  - N_LET   `let s: T = bar()` / `= T{...}`           cgenstmt cglet
  - N_ASSIGN N_IDENT-lhs   `s = bar()` / `= T{...}`   cgenexpr cgassign
  - N_ASSIGN single-DOT local-base   `o.f = ...`
  - N_ASSIGN single-DOT ptr-base auto-deref   `p.f = ...`
  - N_ASSIGN single-DOT global-base   `g.f = ...`
  - N_ASSIGN chained-DOT depth>=2   `o.m.in = ...`
(The four dot-flavors share one shape pattern, hence "5 sites".) Where
the dst addr needs scratch (ptr-base/global-base/via_cx), it is loaded
into BX after the call so CX stays as the third value word; for
structlit field-walks BX is reloaded before each store since cgexpr
clobbers AX/BX between fields.

wwstage needed a new `structnaturalsize(si)` helper (cgenutil.ww):
si.totsize is mis-named — it's slot-padded to 8 by registerstruct for
stack-slot use, while the receive ABI wants the type's natural size
(max(foff+fsz)). Splitting si.totsize into naturalsize + slotsize is
tracked as the wwstage struct sizing follow-up (task #15); until that
lands, the helper recovers the natural size at receive sites.

Test 701_cgassign_struct.c (18 rows, 3 checks each — cstage value,
wwstage value, asm byte-identity), wired in Makefile after 698. The
headline ASYMMETRY case is the 20B `{i32×5}` row: sender pads to 24B
via three MOVQs, receiver writes MOVQ AX +0, MOVQ DX +8, MOVL CX +16.
A regression to a MOVQ tail there overruns 4B past the slot and
flips the exit-code check.

smoke.combined.ww is the auto-regen ride-along of strings.freeall
landing in 714d089 (worker-shlex).

Pre-existing gaps surfaced and tracked separately (not fixed here,
out of scope):
  - task #16: silent drop of `(*p).f = ...` explicit-deref dot lhs.
  - task #17: silent zero of nested STRUCTLIT field in N_LET / N_ASSIGN
    initializer — the field_chain and field_global test rows use
    explicit field writes (`o.m.t = 10i64;`) rather than nested
    literals as a fixture-level workaround.
  - task #9: module-name-mangle for fn labels avoided in the
    field_global_call fixture by `let g: outer;` (no init).

make test: 59/59. 994_w6c_ww + 995_self_rebuild PASS — bootstrap
byte-identity is the load-bearing proof for this commit's scope.
2026-05-15 16:43:07 +09:00

436 lines
14 KiB
C

/*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.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[] = {
/* 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 src[64], tmpdir[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/wcas_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/wcas_%d_d_%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",
tmpdir, driver, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
unlink(src); rmdir(tmpdir);
return -1;
}
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[128];
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); rmdir(tmpdir);
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;
}