The wcc test drivers ran `ww build <bare-/tmp src>` with no -o, so the compiler's <stem>.sepwork scratch landed beside the source and was never cleaned: unbounded /tmp growth (2195 stale dirs observed) that fills tmpfs and fabricates phantom test failures + silent harness aborts, and for in-repo fixture builds leaked .sepwork into the tracked tree. Each leaking build now writes its source + output inside a per-invocation tmpdir, passes -o <tmpdir>/<stem> so the .sepwork lands inside it, and rm -rf's the tmpdir on every exit path -- including fopen-fail and the expected-fail reject builds (scratch is mkdir'd before the build can fail). `ww run` and explicit-`-o`/byte-id helpers are left as-is; the 990/993 byte-id comparison logic is byte-for-byte unchanged. Two items filed separately (this commit holds the no-Makefile / no-main.c rail): - #13: a stale <src>.s byte-id readback (749) silently no-ops since separate-compile emits .s to <ostem>.sepwork/__root.s; documented inline. - #14: build-system Makefile recipes build selfhost/cmd/*/main.ww with no -o and leak main.sepwork in-tree (bounded, gitignored; own commit). One concern -- sepwork leak hygiene -- across 228 drivers; uniform transform applied per-file and two-round reviewed. make test: all 402 passed, zero net-new /tmp scratch, zero test-driven in-repo .sepwork.
337 lines
11 KiB
C
337 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 <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 tmpdir[64], src[128], outbin[128], rmcmd[160], cmd[1024];
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/wcds_%d_d_%d", getpid(), i);
|
|
mkdir(tmpdir, 0755);
|
|
snprintf(src, sizeof src, "%s/wcds_%d_%d.ww", tmpdir, getpid(), i);
|
|
snprintf(outbin, sizeof outbin, "%s/wcds_%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. 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;
|
|
}
|