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.
295 lines
8.8 KiB
C
295 lines
8.8 KiB
C
/*
|
|
* 831_match_field_inplace — match on a tagged-union struct-FIELD
|
|
* scrutinee. wwstage cgmatch reads an addressable BP-relative field
|
|
* (`match (b.t)`, b a local VALUE struct) IN PLACE at base.off +
|
|
* field.offset, dispatching off the field's own slot — no @match_spill
|
|
* copy. Verbatim mirror of cstage N_MATCH's in-place arm
|
|
* (cmd/w6c/cgen.c:10241-10296). Task #25 (M1).
|
|
*
|
|
* Pre-fix (wwstage): the non-ident cgmatch branch unconditionally
|
|
* spilled ANY non-ident scrutinee — including an addressable field —
|
|
* into @match_spill (+16B scratch → frame TEXT $48). cstage read the
|
|
* field where it lives (TEXT $32). Both stages were runtime-correct
|
|
* (ken's battery), so this was a latent rule-10 leanness gap, not a
|
|
* miscompile; the fix aligns wwstage DOWN to cstage so both emit
|
|
* byte-identical asm.
|
|
*
|
|
* Predicate (copied from cstage): in-place iff the scrutinee is an
|
|
* N_DOT whose lhs is a BARE N_IDENT with a stamped type, that type
|
|
* chases to TY_STRUCT, and the field resolves by name. Everything else
|
|
* — a *ptr-field base (`match (h.t)`, h:*struct, chases to TY_PTR so
|
|
* the by-name scan misses), a plain-local tagged ident, a call-result
|
|
* scrutinee — keeps its existing (in-place-ident / spill) path,
|
|
* unchanged.
|
|
*
|
|
* What this table pins, per row, across BOTH driver stages plus a
|
|
* cstage-vs-wwstage asm byte-identity check:
|
|
* (a) local VALUE-struct tagged field [NEW in-place arm]
|
|
* (b) *ptr-struct tagged field [stays on spill — TY_PTR]
|
|
* (c) plain-local tagged ident [unchanged in-place-ident]
|
|
* (d) call-result tagged scrutinee [spill]
|
|
* (e) bool->int payload remap on a field [width: 1-word variants]
|
|
* (f) str-payload (24B box) on a field [width: 3-word variant]
|
|
*
|
|
* The boundary, not just the new arm, is the subject: a regression
|
|
* that mis-routed (b)/(c)/(d) into the in-place arm, or failed to route
|
|
* (a)/(e)/(f) there, would break either the runtime exit code or the
|
|
* byte-identity. The $48->$32 frame convergence itself is ken's
|
|
* authoritative byte-id bind; a runtime test cannot observe frame size.
|
|
*/
|
|
#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[] = {
|
|
/* (a) NEW in-place arm: local VALUE struct, field reassigned to
|
|
* the int variant, matched in place. Returns 42. */
|
|
{ "local_field_int_inplace",
|
|
"package main;\n"
|
|
"type box = struct { t: (int | bool), n: int };\n"
|
|
"fn main() i32 = {\n"
|
|
" let b: box = box { t = 0, n = 5 };\n"
|
|
" b.t = 42;\n"
|
|
" match (b.t) {\n"
|
|
" case let n: int => return n: i32;\n"
|
|
" case bool => return 1;\n"
|
|
" };\n"
|
|
"};\n",
|
|
42 },
|
|
/* (b) *ptr-struct field: base chases to TY_PTR, the by-name scan
|
|
* misses, scrutinee falls to the spill path — exactly as cstage,
|
|
* no separate deref guard. Returns 9. */
|
|
{ "ptr_field_stays_spill",
|
|
"package main;\n"
|
|
"type box = struct { t: (int | bool), n: int };\n"
|
|
"fn pick(h: *box) i32 = {\n"
|
|
" match (h.t) {\n"
|
|
" case let n: int => return n: i32;\n"
|
|
" case bool => return 1;\n"
|
|
" };\n"
|
|
"};\n"
|
|
"fn main() i32 = {\n"
|
|
" let b: box = box { t = 9, n = 5 };\n"
|
|
" return pick(&b);\n"
|
|
"};\n",
|
|
9 },
|
|
/* (c) plain-local tagged ident: unchanged in-place-ident path
|
|
* (not N_DOT). Returns 5. */
|
|
{ "plain_local_ident",
|
|
"package main;\n"
|
|
"fn main() i32 = {\n"
|
|
" let t: (int | bool) = 5;\n"
|
|
" match (t) {\n"
|
|
" case let n: int => return n: i32;\n"
|
|
" case bool => return 1;\n"
|
|
" };\n"
|
|
"};\n",
|
|
5 },
|
|
/* (d) call-result tagged scrutinee: not N_DOT, keeps the spill
|
|
* path. Returns 7. */
|
|
{ "call_result_spill",
|
|
"package main;\n"
|
|
"fn mk(b: bool) (int | bool) = {\n"
|
|
" if (b) { return 7; };\n"
|
|
" return false;\n"
|
|
"};\n"
|
|
"fn main() i32 = {\n"
|
|
" match (mk(true)) {\n"
|
|
" case let n: int => return n: i32;\n"
|
|
" case bool => return 1;\n"
|
|
" };\n"
|
|
"};\n",
|
|
7 },
|
|
/* (e) width: a field whose variant remaps from bool to int. The
|
|
* in-place arm reads tag + one payload word at the field slot;
|
|
* the int arm also reads a sibling field (b.n) to prove the
|
|
* surrounding struct frame is undisturbed. Returns 7 + 5 = 12. */
|
|
{ "field_bool_to_int_remap",
|
|
"package main;\n"
|
|
"type box = struct { t: (int | bool), n: int };\n"
|
|
"fn main() i32 = {\n"
|
|
" let b: box = box { t = true, n = 5 };\n"
|
|
" b.t = 7;\n"
|
|
" match (b.t) {\n"
|
|
" case let n: int => return (n + b.n): i32;\n"
|
|
" case bool => return 1;\n"
|
|
" };\n"
|
|
"};\n",
|
|
12 },
|
|
/* (f) width: a 24B str-payload variant read in place at the
|
|
* field slot (tag@+0, .ptr@+8, .len@+16). Returns len("hello")
|
|
* = 5. */
|
|
{ "field_str_payload_24B",
|
|
"package main;\n"
|
|
"type box = struct { t: (str | int), n: int };\n"
|
|
"fn main() i32 = {\n"
|
|
" let b: box = box { t = 0, n = 5 };\n"
|
|
" b.t = \"hello\";\n"
|
|
" match (b.t) {\n"
|
|
" case let s: str => return len(s): i32;\n"
|
|
" case int => return 99;\n"
|
|
" };\n"
|
|
"};\n",
|
|
5 },
|
|
};
|
|
|
|
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/wcmfi_%d_d_%d", getpid(), i);
|
|
mkdir(tmpdir, 0755);
|
|
snprintf(src, sizeof src, "%s/wcmfi_%d_%d.ww", tmpdir, getpid(), i);
|
|
snprintf(outbin, sizeof outbin, "%s/wcmfi_%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 — the whole point of M1 is that wwstage's frame
|
|
* stops bloating for an addressable field scrutinee, so the cstage vs
|
|
* wwstage text output must match byte-for-byte on every row. */
|
|
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/wcmfi_asm_%d_%d.ww", getpid(), i);
|
|
snprintf(cs, sizeof cs, "/tmp/wcmfi_asm_%d_%d_c.s", getpid(), i);
|
|
snprintf(ws, sizeof ws, "/tmp/wcmfi_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[512];
|
|
if (bin[0] != '/') {
|
|
char cwd[256];
|
|
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
|
|
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
|
|
bin = absbin;
|
|
}
|
|
|
|
char cdrv[640];
|
|
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
|
char wdrv[640];
|
|
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_field_inplace: 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_field_inplace[%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,
|
|
"match_field_inplace: %d/%d fixtures failed\n",
|
|
fail, total);
|
|
return 1;
|
|
}
|
|
printf("match_field_inplace: %d/%d ok\n", total, total);
|
|
return 0;
|
|
}
|