Files
ww/test/wcc/704_dot_structlit.c
Hojun-Cho 99a68a6a57 cstage+selfhost+test: extend structlit-fill helper to N_ASSIGN N_DOT lhs (4 flavors)
Sister fix to #17. The BP-rel helper from #17 covered N_LET /
N_ASSIGN N_IDENT-lhs / N_RETURN; the four N_ASSIGN N_DOT-lhs
structlit walks still went through the inline `cgexpr(field.lhs);
store-AX-sized` shape and silently dropped trailing bytes when a
struct-typed field's value was itself an N_STRUCTLIT. Affected dot
flavors: single-dot via_ptr / global / BP-rel and the chained-dot
walker (depth >= 2, all three root flavors).

Extend `cg_structlit_fill_bp` / `cgstructlitfillbp` into
`cg_structlit_fill` / `cgstructlitfill` taking a destination mode
(DST_BP / DST_PTR_LOCAL / DST_GLOBAL = 0/1/2), srcoff (PTR_LOCAL),
srcname (GLOBAL), and disp accumulator. `disp` grows by foff on
descent; srcoff/srcname stay constant across the call tree. The
pre-#17 wrappers are preserved byte-identically by delegating with
mode=DST_BP — 995_self_rebuild byte-identity holds for the no-
nested-STRUCTLIT case that selfhost source actually uses.

The non-BP modes reload BX before the ELLIPSIS zero-fill loop AND
before every field store (tagged, scalar, and the cgexpr leaf).
This is correctness-by-construction — cgexpr clobbers BX between
fields, and the redundant reload only fires on shapes that didn't
compile before. The four dot-flavor sites in each stage now compute
their dst mode + disp and call the shared helper (reducing each
from ~80-130 inline lines to ~5-12 lines of dispatch).

Stage signature asymmetry: cstage threads Local** for cgexpr; ww-
stage takes explicit totsize because #15 (split totsize into
naturalsize + slotsize) is still pending and the dot sites need
structnaturalsize while the BP-rel sites need si.totsize. Both
asymmetries are documented in the helper docstrings.

704 covers 8 rows (24 checks: 8 cstage exits, 8 wwstage exits, 8
cstage-vs-wwstage .s byte-identity diffs): 6 dst-flavors (single-
dot local/ptr/global, chained-dot local/ptr/global) plus single-
local 3-deep and single-ptr 3-deep to pin disp threading through
the helper's recursion and through DST_PTR_LOCAL BX reloads.

The nested struct-typed CALL rhs in field-walks has the same shape
as the STRUCTLIT bug fixed here but the helper only handles
STRUCTLIT — tracked as task #20.
2026-05-15 18:45:21 +09:00

343 lines
11 KiB
C

/*
* 704_dot_structlit — silent zero of nested STRUCTLIT field in
* cgassign N_DOT-lhs (task #18).
*
* Sister bug to #17. #17 fixed the BP-relative sites (N_LET,
* N_ASSIGN N_IDENT-lhs, N_RETURN) via the cg_structlit_fill_bp /
* cgstructlitfillbp helper. The N_ASSIGN N_DOT-lhs structlit walks
* were left inline because their addressing has extra BX-reload
* concerns (the dst addr is a *struct local or a struct global,
* loaded into BX, and cgexpr clobbers BX between fields). All four
* dot-flavors still emitted `cgexpr(field_value); store-AX-sized`
* for the rhs structlit's fields — and for a struct-typed field
* whose value is itself an N_STRUCTLIT, that lands only the first
* qword while the trailing bytes silently stay zero.
*
* Affected dot-flavors (each with the same silent-zero shape):
* 1. single-dot local-base `o.f = outer { i = inner{...}, ... }`
* 2. single-dot ptr-base `p.f = outer { i = inner{...}, ... }`
* 3. single-dot global-base `g.f = outer { i = inner{...}, ... }`
* 4. chained-dot local `o.x.y = outer { i = inner{...}, ... }`
* 5. chained-dot ptrroot `p.x.y = outer { i = inner{...}, ... }`
* 6. chained-dot global `g.x.y = outer { i = inner{...}, ... }`
*
* Fix (#18): the helper is extended into cg_structlit_fill /
* cgstructlitfill that takes a destination context (mode, srcoff,
* name, disp). The old BP-rel wrapper is preserved byte-identically.
* The four dot-flavor sites in each stage now delegate to the
* helper, which recurses on nested struct-typed N_STRUCTLIT values
* at `disp + foff` and reloads BX before each store for the non-BP
* modes. 995_self_rebuild byte-identity is the load-bearing proof
* that the no-nested case is byte-identical to the pre-#18 inline
* walks.
*
* Each row pins:
* - cstage value correctness (process exit code).
* - wwstage value correctness (when ww_ww exists).
* - cstage vs wwstage byte-identical .s output (catches drift).
*/
#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[] = {
/* single-dot local-base: `h.f = outer { i = inner{...}, ... }`.
* Pre-fix: h.f.i.a landed (first qword via cgexpr) but h.f.i.b
* silently stayed 0. Want: 7+8+12 = 27. */
{ "dot_local_lit_nested",
"type inner = struct { a: i64, b: i64 };\n"
"type outer = struct { i: inner, t: i64 };\n"
"type holder = struct { f: outer };\n"
"fn main() i32 = {\n"
" let h: holder;\n"
" h.f = outer { i = inner { a = 7i64, b = 8i64 }, t = 12i64 };\n"
" return (h.f.i.a + h.f.i.b + h.f.t): i32;\n"
"};\n",
27 },
/* single-dot local-base, 3-deep recursion (outer.m.in). Pins
* that the disp accumulator threads `foff + middle_foff +
* leaf_foff` correctly. Want: 1+2+3+4+9 = 19. */
{ "dot_local_lit_3deep",
"type leaf = struct { a: i64, b: i64 };\n"
"type middle = struct { in: leaf, t: i64 };\n"
"type outer = struct { m: middle, x: i64 };\n"
"type holder = struct { f: outer, pad: i64 };\n"
"fn main() i32 = {\n"
" let h: holder;\n"
" h.f = outer {\n"
" m = middle {\n"
" in = leaf { a = 1i64, b = 2i64 },\n"
" t = 3i64\n"
" },\n"
" x = 4i64\n"
" };\n"
" h.pad = 9i64;\n"
" return (h.f.m.in.a + h.f.m.in.b + h.f.m.t + h.f.x + h.pad): i32;\n"
"};\n",
19 },
/* single-dot ptr-base (auto-deref): `p.f = outer { i = inner{...}, ... }`.
* Exercises DST_PTR_LOCAL mode — helper reloads BX from
* srcoff(BP) before zero-fill and before every store. Want:
* 9+11+30 = 50. */
{ "dot_ptr_lit_nested",
"type inner = struct { a: i64, b: i64 };\n"
"type outer = struct { i: inner, t: i64 };\n"
"type holder = struct { f: outer };\n"
"fn main() i32 = {\n"
" let h: holder;\n"
" let p: *holder = &h;\n"
" p.f = outer { i = inner { a = 9i64, b = 11i64 }, t = 30i64 };\n"
" return (p.f.i.a + p.f.i.b + p.f.t): i32;\n"
"};\n",
50 },
/* single-dot global-base: `g.f = outer { i = inner{...}, ... }`.
* Exercises DST_GLOBAL mode — helper reloads BX via LEAQ
* name(SB) before zero-fill and before every store. `let g: T;`
* (no init) per the task #17 module-name-mangle workaround
* pattern. Want: 13+17+25 = 55. */
{ "dot_global_lit_nested",
"type inner = struct { a: i64, b: i64 };\n"
"type outer = struct { i: inner, t: i64 };\n"
"type holder = struct { f: outer };\n"
"let g: holder;\n"
"fn main() i32 = {\n"
" g.f = outer { i = inner { a = 13i64, b = 17i64 }, t = 25i64 };\n"
" return (g.f.i.a + g.f.i.b + g.f.t): i32;\n"
"};\n",
55 },
/* chained-dot local (depth-2 lhs): `h.mid.f = outer { i =
* inner{...}, ... }`. Exercises the chained-DOT walker with the
* non-via_cx (local-through-chain) path — disp = base_disp +
* total_off, no BX reload. Want: 4+5+13 = 22. */
{ "dot_chain_lit_nested",
"type inner = struct { a: i64, b: i64 };\n"
"type outer = struct { i: inner, t: i64 };\n"
"type holder = struct { f: outer };\n"
"type h2 = struct { mid: holder, pad: i64 };\n"
"fn main() i32 = {\n"
" let h: h2;\n"
" h.mid.f = outer { i = inner { a = 4i64, b = 5i64 }, t = 13i64 };\n"
" return (h.mid.f.i.a + h.mid.f.i.b + h.mid.f.t): i32;\n"
"};\n",
22 },
/* chained-dot ptrroot (depth-2 lhs through *struct root):
* `p.mid.f = outer { i = inner{...}, ... }`. Exercises the
* chained-DOT walker with ptrroot=1 — helper mode=1, reloads BX
* from rootoff(BP). Want: 6+7+14 = 27. */
{ "dot_chain_ptr_lit_nested",
"type inner = struct { a: i64, b: i64 };\n"
"type outer = struct { i: inner, t: i64 };\n"
"type holder = struct { f: outer };\n"
"type h2 = struct { mid: holder, pad: i64 };\n"
"fn main() i32 = {\n"
" let h: h2;\n"
" let p: *h2 = &h;\n"
" p.mid.f = outer { i = inner { a = 6i64, b = 7i64 }, t = 14i64 };\n"
" return (p.mid.f.i.a + p.mid.f.i.b + p.mid.f.t): i32;\n"
"};\n",
27 },
/* chained-dot global (depth-2 lhs through struct global):
* `gg.mid.f = outer { i = inner{...}, ... }`. Exercises the
* chained-DOT walker with isglobal=1 — helper mode=2, reloads
* BX via LEAQ gg(SB). Want: 10+20+31 = 61. */
{ "dot_chain_global_lit_nested",
"type inner = struct { a: i64, b: i64 };\n"
"type outer = struct { i: inner, t: i64 };\n"
"type holder = struct { f: outer };\n"
"type h2 = struct { mid: holder, pad: i64 };\n"
"let gg: h2;\n"
"fn main() i32 = {\n"
" gg.mid.f = outer { i = inner { a = 10i64, b = 20i64 }, t = 31i64 };\n"
" return (gg.mid.f.i.a + gg.mid.f.i.b + gg.mid.f.t): i32;\n"
"};\n",
61 },
/* single-dot ptr-base 3-deep — pins disp threading through the
* helper's recursion AND through DST_PTR_LOCAL reloads. Want:
* 1+2+3+4 = 10. */
{ "dot_ptr_lit_3deep",
"type leaf = struct { a: i64, b: i64 };\n"
"type middle = struct { in: leaf, t: i64 };\n"
"type outer = struct { m: middle, x: i64 };\n"
"type holder = struct { f: outer };\n"
"fn main() i32 = {\n"
" let h: holder;\n"
" let p: *holder = &h;\n"
" p.f = outer {\n"
" m = middle {\n"
" in = leaf { a = 1i64, b = 2i64 },\n"
" t = 3i64\n"
" },\n"
" x = 4i64\n"
" };\n"
" return (p.f.m.in.a + p.f.m.in.b + p.f.m.t + p.f.x): i32;\n"
"};\n",
10 },
};
/* run_driver — compile r->src via the given driver and exec; return
* the process exit code. Mirror of 703. */
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/wcds_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/wcds_%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. Mirror of 703. */
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/wcds_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/wcds_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/wcds_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;
for (int d = 0; drivers[d].name; d++) {
if (drivers[d].gated_on_existence
&& access(drivers[d].path, X_OK) != 0) {
fprintf(stderr, "dot_structlit: 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,
"dot_structlit[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
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,
"dot_structlit: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("dot_structlit: %d/%d ok\n", total, total);
return 0;
}