Files
ww/test/wcc/907_f32arg_run.c
Hojun-Cho ce3a25a0b4 test: contain sepwork scratch per-driver tmpdir, fix /tmp+in-repo leak (#8)
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.
2026-06-22 23:29:39 +09:00

226 lines
7.4 KiB
C

/*
* 907_f32arg_run — runtime + byte-id regression net for #143: passing an
* f32 as a function argument must spill through the stack at the f32 class
* width (MOVSS, 4-byte), not MOVSD (8-byte). cstage lowered the arg-push
* (cgen.c N_CALL push) and arg-pop (the pop-into-XMM loop) with a hardcoded
* MOVSD; wwstage already split f32/f64 via exprfloatkind (MOVSS for f32).
* The result was a cs!=ww byte-id break on EVERY f32 argument — a pure
* divergence, not a runtime miscompile (the callee reads its f32 param as
* the low 32 bits via MOVSS regardless of how the caller spilled it), but
* it blocked the cs==ww gate and therefore fold-5b (strconv f32tos, whose
* core calls math.f32bits(n) — an f32 arg).
*
* ABI-correct form is MOVSS for single precision per SysV: ref/qbe
* amd64/emit.c:524 — the slot->slot copy-through-XMM path (exactly ww's
* X0 -> 8B stack slot -> XMMn arg-spill shape) narrows the class to Ks for
* non-wide floats and emits movss (clstoa[Ks] == "ss"); movsd is the
* double (Kd) form. The fix aligns cstage UP to MOVSS via the existing
* op_for(node_isf32) helper at both the push and pop sites. wwstage is
* unchanged. This is the f32-arg-push half of the float-register family
* (#119 f32-arr-load, #122 f32-arr-store, #125 float-arr-index, #157
* tagged-float-return) — "f32 must route through the right XMM width."
*
* Each row carries BOTH dimensions (like 955 / 964 / 965):
* (a) cstage `ww build` + run, asserting the exit code (end-to-end ABI:
* the callee must read the correct f32 value out of the XMM reg).
* (b) w6c vs w6c_ww `.s` cmp — FAILS if the stages diverge (rule-10).
*
* The 990-997 byte-id gates are BLIND to a reintroduction here only if the
* gated selfhost tools never pass an f32 arg (they don't — bootstrap-
* NEUTRAL), so this executed-and-byte-id-checked probe is the live net.
*/
#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; };
static const struct row rows[] = {
/* single f32 var arg — the minimal repro. takef32(2.5) -> 2.5,
* truncated to i32 == 2. On the bug the byte-id breaks (MOVSD push)
* but the value still rounds (MOVSD carries the low 4 bytes too). */
{ "single_var",
"package main;\n"
"fn takef32(x: f32) f32 = { return x; };\n"
"export fn main() i32 = {\n"
" let a: f32 = 2.5f32;\n"
" let r: f32 = takef32(a);\n"
" return r: i32;\n"
"};\n", 2 },
/* f32 LITERAL directly as the arg (ken's original getj(1.0f32)
* shape that surfaced #143). id(3.0f32) -> 3.0 -> 3. */
{ "literal_arg",
"package main;\n"
"fn id(x: f32) f32 = { return x; };\n"
"export fn main() i32 = {\n"
" let r: f32 = id(3.0f32);\n"
" return r: i32;\n"
"};\n", 3 },
/* multiple f32 args — each pushed/popped independently; all must
* use MOVSS on both stages. 1.5 + 2.5 + 4.0 == 8.0 (exact in f32). */
{ "multi_f32",
"package main;\n"
"fn add3(a: f32, b: f32, c: f32) f32 = { return a + b + c; };\n"
"export fn main() i32 = {\n"
" let x: f32 = 1.5f32;\n"
" let y: f32 = 2.5f32;\n"
" let z: f32 = 4.0f32;\n"
" let r: f32 = add3(x, y, z);\n"
" return r: i32;\n"
"};\n", 8 },
/* f32 + f64 MIX — the f64 arg must stay MOVSD (8-byte), the f32 args
* MOVSS (4-byte), and BOTH stages must agree per-arg. 1.5 + 10.0 +
* 0.5 == 12.0 (exact). Guards against the fix over-reaching to f64. */
{ "mixed_f32_f64",
"package main;\n"
"fn mix(a: f32, b: f64, c: f32) f64 = "
"{ return a: f64 + b + c: f64; };\n"
"export fn main() i32 = {\n"
" let p: f32 = 1.5f32;\n"
" let q: f64 = 10.0;\n"
" let s: f32 = 0.5f32;\n"
" let r: f64 = mix(p, q, s);\n"
" return r: i32;\n"
"};\n", 12 },
/* STACK-passed f32: 10 float args — 8 ride X0..X7, args 9+10 stay on
* the stack (fpidx >= 8). The push-spill still diverged on the bug;
* this exercises the fpidx>=8 leave-on-stack arm. 1.0 * 10 == 10.0. */
{ "stack_passed",
"package main;\n"
"fn ten(a: f32, b: f32, c: f32, d: f32, e: f32, f: f32, "
"g: f32, h: f32, i: f32, j: f32) f32 = {\n"
" return a + b + c + d + e + f + g + h + i + j;\n"
"};\n"
"export fn main() i32 = {\n"
" let v: f32 = 1.0f32;\n"
" let r: f32 = ten(v, v, v, v, v, v, v, v, v, v);\n"
" return r: i32;\n"
"};\n", 10 },
{ NULL, NULL, 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, "f32arg: w6c_ww missing — cannot run the "
"cs==ww byte-id gate (the whole point of this test)\n");
return 1;
}
int n = 0, fail = 0;
for (int i = 0; rows[i].src; i++, n++) {
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwf32a_%d_d_%d",
getpid(), i);
mkdir(tmpdir, 0755);
char src[128], outbin[128], rmcmd[160];
snprintf(src, sizeof src, "%s/wwf32a_%d_%d.ww",
tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/wwf32a_%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);
/* (a) cstage build + run. */
char cmd[2048];
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[128], ws_s[128];
snprintf(cs_s, sizeof cs_s, "%s/wwf32a_%d_%d_cs.s",
tmpdir, getpid(), i);
snprintf(ws_s, sizeof ws_s, "%s/wwf32a_%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 f32-arg-push tests failed\n", fail, n);
return 1;
}
printf("f32arg: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n);
return 0;
}