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.
309 lines
11 KiB
C
309 lines
11 KiB
C
/*
|
|
* 905_tupparam_run — runtime + byte-id net for #163, the tuple-PARAM ABI
|
|
* (the param twin of #164's tuple RETURN).
|
|
*
|
|
* THE BUG (#163, LIVE drop on master): a tuple passed AS AN ARGUMENT was
|
|
* unhandled in BOTH stages — no tuple arm in the cgcall arg push, the
|
|
* cgcall arg pop, OR the callee cgfnparams receive. A tuple-typed call
|
|
* result (`f(g())` where g returns a tuple) left its elements in the
|
|
* return-ABI cursor (AX/DX/CX/R8 + X0/X1, per #164); the SEND fell to the
|
|
* 1-GP-word `else` (PUSHQ AX / POPQ DI) so ALL BUT THE FIRST ELEMENT was
|
|
* dropped, and the callee read its tuple param as a single GP word. This
|
|
* broke INTEGER tuple params too; float elements were doubly lost (they
|
|
* ride X0/X1, never AX).
|
|
*
|
|
* THE FIX: per-element SysV class placement reusing #164's helper. SEND —
|
|
* cgexpr leaves the tuple in the return cursor; restage it into a frame
|
|
* slot (@tupargscr) by class via tuple_store/tupstore, then push the slot
|
|
* words high->low so the pop drains slot+0 first into the SysV ARG cursor
|
|
* (DI/SI/.. + X0..X7). The frame slot decouples the return-class regs
|
|
* (which OVERLAP the arg-class regs) from the arg placement. RECV — the
|
|
* callee walks the tuple's elements over the arg cursor, storing each into
|
|
* its frame slot positionally. Symmetric across cstage (cmd/w6c/cgen.c)
|
|
* and wwstage (cgenutil.ww pushargsrev + cgenexpr.ww cgcall pop +
|
|
* cgendecl.ww cgfnparams).
|
|
*
|
|
* SCOPE: register-class tuple ARGS produced by a CALL (the only form that
|
|
* materialises a tuple value today — `let t = (1,2)` as a first-class
|
|
* value is a separate unimplemented gap, so the SEND scopes to the N_CALL
|
|
* producer and never pushes stale regs, rule 7). Arg-register overflow
|
|
* loud-stops (the partial-spill stitch is out of scope, twin of #164's
|
|
* cap); the loudstop row asserts BOTH stages ERROR.
|
|
*
|
|
* GATE-BLIND TO BYTE-ID ALONE: the bootstrap passes no tuple params, and
|
|
* pre-fix both stages were symmetric-WRONG (both PUSHQ AX), so the cs==ww
|
|
* .s gate HOLDS on master for the value rows — they diverge only at
|
|
* RUNTIME. Each value row carries BOTH dimensions (modelled on 956):
|
|
* (a) cstage `ww build` + run, asserting the exit code (catches #163:
|
|
* master returns the wrong exit / dropped element).
|
|
* (b) w6c vs w6c_ww `.s` cmp (rule-10: both stages fixed identically).
|
|
*/
|
|
#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_exit;
|
|
int want_compile_fail; /* loud-stop rows must NOT compile */
|
|
};
|
|
|
|
static const struct row rows[] = {
|
|
/* HEADLINE — (f64, f64) arg. Pre-fix the SEND pushes only AX (the
|
|
* two floats stay stranded in X0/X1) and the callee reads one GP
|
|
* word; t.0+t.1 != 8.0 -> return 1. Post-fix each float rides the
|
|
* SSE arg cursor (X0,X1). */
|
|
{ "f64f64_arg",
|
|
"package main;\n"
|
|
"fn pair(a: f64, b: f64) (f64, f64) = { return (a, b); };\n"
|
|
"fn add(t: (f64, f64)) f64 = { return t.0 + t.1; };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tif (add(pair(3.0, 5.0)) != 8.0) { return 1; };\n"
|
|
"\treturn 0;\n"
|
|
"};\n", 0 },
|
|
/* (i64, i64) arg — proves the broader INTEGER-tuple-param drop is
|
|
* fixed (master dropped the second i64 too). e0->DI, e1->SI. */
|
|
{ "i64i64_arg",
|
|
"package main;\n"
|
|
"fn pair(a: i64, b: i64) (i64, i64) = { return (a, b); };\n"
|
|
"fn add(t: (i64, i64)) i64 = { return t.0 + t.1; };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tif (add(pair(3, 5)) != 8) { return 1; };\n"
|
|
"\treturn 0;\n"
|
|
"};\n", 0 },
|
|
/* (f64, i64) — class independent of position: f64@X0 (SSE cursor),
|
|
* i64@DI (INTEGER cursor), independent counters. */
|
|
{ "f64i64_arg",
|
|
"package main;\n"
|
|
"fn mk(a: f64, b: i64) (f64, i64) = { return (a, b); };\n"
|
|
"fn add(t: (f64, i64)) i64 = { return (t.0: i64) + t.1; };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tif (add(mk(3.0, 5)) != 8) { return 1; };\n"
|
|
"\treturn 0;\n"
|
|
"};\n", 0 },
|
|
/* (i64, f64) — order-swap: i64@DI, f64@X0. Confirms the float lands
|
|
* in the next XMM regardless of its positional slot. */
|
|
{ "i64f64_arg",
|
|
"package main;\n"
|
|
"fn mk(a: i64, b: f64) (i64, f64) = { return (a, b); };\n"
|
|
"fn add(t: (i64, f64)) i64 = { return t.0 + (t.1: i64); };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tif (add(mk(3, 5.0)) != 8) { return 1; };\n"
|
|
"\treturn 0;\n"
|
|
"};\n", 0 },
|
|
/* (f64, str) — SSE + wide (24B {ptr,len,cap}) coexist. The f64
|
|
* rides X0 (SSE, consuming no GP slot); the str rides DI/SI/DX
|
|
* (INTEGER cursor). f=4.0, s.len=5 -> 4+5 = 9. */
|
|
{ "f64str_arg",
|
|
"package main;\n"
|
|
"fn mk(a: f64) (f64, str) = { return (a, \"hello\"); };\n"
|
|
"fn add(t: (f64, str)) i64 = {\n"
|
|
"\treturn (t.0: i64) + (t.1.len: i64);\n"
|
|
"};\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tif (add(mk(4.0)) != 9) { return 1; };\n"
|
|
"\treturn 0;\n"
|
|
"};\n", 0 },
|
|
/* MULTI-TUPLE-ARG, ONE CALL — f(g(), h()) where BOTH args are
|
|
* tuple-producing calls. Proves @tupargscr (single-slot-per-fn) is
|
|
* REUSED per arg, not COLLIDED: pushargsrev evals right-to-left, so
|
|
* h() restages into the slot + drains it to the stack BEFORE g()
|
|
* restages into the SAME slot (h's words already pushed, safe to
|
|
* overwrite). Drain forward: s->DI,SI; t->DX,CX. 1+2+3+4 = 10. A
|
|
* collision (both restaged before either pushed) would corrupt the
|
|
* first-pushed tuple's words. */
|
|
{ "two_tuple_args",
|
|
"package main;\n"
|
|
"fn pair(a: i64, b: i64) (i64, i64) = { return (a, b); };\n"
|
|
"fn add4(s: (i64, i64), t: (i64, i64)) i64 = {\n"
|
|
"\treturn s.0 + s.1 + t.0 + t.1;\n"
|
|
"};\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tif (add4(pair(1, 2), pair(3, 4)) != 10) { return 1; };\n"
|
|
"\treturn 0;\n"
|
|
"};\n", 0 },
|
|
/* CONTROL — a tuple arg threaded through a chain of two calls,
|
|
* proving the SEND/RECV round-trips through the slot intact. */
|
|
{ "f64f64_chain",
|
|
"package main;\n"
|
|
"fn pair(a: f64, b: f64) (f64, f64) = { return (a, b); };\n"
|
|
"fn id(t: (f64, f64)) f64 = { return t.0 * 10.0 + t.1; };\n"
|
|
"export fn main() i32 = {\n"
|
|
"\tif (id(pair(3.0, 5.0)) != 35.0) { return 1; };\n"
|
|
"\treturn 0;\n"
|
|
"};\n", 0 },
|
|
/* LOUD-STOP — 5 i64 scalars + an (i64,i64) tuple = 7 INTEGER arg
|
|
* eightbytes, overflowing the 6 GP arg regs (DI/SI/DX/CX/R8/R9).
|
|
* The partial-spill stitch is out of scope (twin of #164's cap), so
|
|
* BOTH stages must FAIL TO COMPILE (rule 7: surface, never silently
|
|
* drop). Master has no tuple-arg arm (pushes the tuple as 1 word ->
|
|
* 6 GP, no overflow) and builds the miscompile, so want_compile_fail
|
|
* discriminates. */
|
|
{ "gp_overflow_loudstop",
|
|
"package main;\n"
|
|
"fn pair(a: i64, b: i64) (i64, i64) = { return (a, b); };\n"
|
|
"fn f(a: i64, b: i64, c: i64, d: i64, e: i64, t: (i64, i64)) i64 = {\n"
|
|
"\treturn a + b + c + d + e + t.0 + t.1;\n"
|
|
"};\n"
|
|
"export fn main() i32 = {\n"
|
|
"\treturn (f(1, 2, 3, 4, 5, pair(6, 7)): i32);\n"
|
|
"};\n", 0, 1 },
|
|
{ NULL, NULL, 0, 0 }
|
|
};
|
|
|
|
static int
|
|
slurp_eq(const char *a, const char *b)
|
|
{
|
|
FILE *fa = fopen(a, "rb");
|
|
FILE *fb = fopen(b, "rb");
|
|
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
|
|
int rc = 0;
|
|
for (;;) {
|
|
int ca = fgetc(fa);
|
|
int cb = fgetc(fb);
|
|
if (ca != cb) { rc = -1; break; }
|
|
if (ca == EOF) break;
|
|
}
|
|
fclose(fa); fclose(fb);
|
|
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 w6c[1100], w6c_ww[1100];
|
|
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
|
|
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
|
|
if (access(w6c_ww, X_OK) != 0) {
|
|
fprintf(stderr, "tupparam: w6c_ww missing — cannot run the "
|
|
"cs==ww byte-id gate\n");
|
|
return 1;
|
|
}
|
|
|
|
int n = 0, fail = 0;
|
|
for (int i = 0; rows[i].src; i++, n++) {
|
|
char tmpdir[64], src[128], outbin[128], rmcmd[160];
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwtupp_%d_d_%d",
|
|
getpid(), i);
|
|
mkdir(tmpdir, 0755);
|
|
snprintf(src, sizeof src, "%s/wwtupp_%d_%d.ww",
|
|
tmpdir, getpid(), i);
|
|
snprintf(outbin, sizeof outbin, "%s/wwtupp_%d_%d",
|
|
tmpdir, getpid(), i);
|
|
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
|
|
FILE *f = fopen(src, "wb");
|
|
if (f == NULL) { fail++; runwait(rmcmd); continue; }
|
|
fputs(rows[i].src, f);
|
|
fclose(f);
|
|
|
|
char cmd[2048];
|
|
|
|
/* LOUD-STOP rows: the arg-reg overflow must FAIL TO COMPILE in
|
|
* BOTH stages (rule 7). Assert (a) cstage `ww build` errors and
|
|
* (b) w6c AND w6c_ww each return non-zero. No .s is produced, so
|
|
* the byte-id cmp is skipped. */
|
|
if (rows[i].want_compile_fail) {
|
|
snprintf(cmd, sizeof cmd,
|
|
"%s/ww build -o %s %s >/dev/null 2>&1",
|
|
bin, outbin, src);
|
|
if (runwait(cmd) == 0) {
|
|
fprintf(stderr, "row[%s]: cstage build SUCCEEDED, "
|
|
"want loud-stop (arg-reg overflow)\n",
|
|
rows[i].label);
|
|
fail++;
|
|
}
|
|
snprintf(cmd, sizeof cmd, "%s -o /dev/null %s 2>/dev/null",
|
|
w6c, src);
|
|
if (runwait(cmd) == 0) {
|
|
fprintf(stderr, "row[%s]: w6c emitted .s, want "
|
|
"loud-stop\n", rows[i].label);
|
|
fail++;
|
|
}
|
|
snprintf(cmd, sizeof cmd, "%s -o /dev/null %s 2>/dev/null",
|
|
w6c_ww, src);
|
|
if (runwait(cmd) == 0) {
|
|
fprintf(stderr, "row[%s]: w6c_ww emitted .s, want "
|
|
"loud-stop\n", rows[i].label);
|
|
fail++;
|
|
}
|
|
runwait(rmcmd);
|
|
continue;
|
|
}
|
|
|
|
/* (a) cstage build + run in a scratch dir. */
|
|
snprintf(cmd, sizeof cmd, "%s/ww build -o %s %s",
|
|
bin, outbin, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: cstage build failed\n",
|
|
rows[i].label);
|
|
fail++;
|
|
runwait(rmcmd);
|
|
continue;
|
|
}
|
|
|
|
int got = runwait(outbin);
|
|
if (got != rows[i].want_exit) {
|
|
fprintf(stderr, "row[%s]: cstage exit %d, want %d\n",
|
|
rows[i].label, got, rows[i].want_exit);
|
|
fail++;
|
|
}
|
|
|
|
/* (b) cs==ww byte-id gate: emit .s from both stages, cmp. */
|
|
char cs_s[160], ws_s[160];
|
|
snprintf(cs_s, sizeof cs_s, "%s/wwtupp_%d_%d_cs.s",
|
|
tmpdir, getpid(), i);
|
|
snprintf(ws_s, sizeof ws_s, "%s/wwtupp_%d_%d_ww.s",
|
|
tmpdir, getpid(), i);
|
|
|
|
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
|
|
w6c, cs_s, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label);
|
|
fail++; runwait(rmcmd); continue;
|
|
}
|
|
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
|
|
w6c_ww, ws_s, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "row[%s]: w6c_ww failed\n",
|
|
rows[i].label);
|
|
fail++; runwait(rmcmd); continue;
|
|
}
|
|
if (slurp_eq(cs_s, ws_s) != 0) {
|
|
fprintf(stderr,
|
|
"row[%s]: cstage/wwstage .s DIFFER (rule-10 "
|
|
"byte-id violation)\n", rows[i].label);
|
|
fail++;
|
|
}
|
|
runwait(rmcmd);
|
|
}
|
|
|
|
if (fail) {
|
|
fprintf(stderr, "%d/%d tuple-param tests failed\n", fail, n);
|
|
return 1;
|
|
}
|
|
printf("tupparam: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n);
|
|
return 0;
|
|
}
|