Files
ww/test/wcc/916_arr_float_call_index_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

236 lines
7.3 KiB
C

/*
* 916_arr_float_call_index_run — runtime + byte-id net for #125: in
* the `arr[i] = v` ASSIGN path, when the element type is float and the
* INDEX sub-expression clobbers X0 (e.g. a fn-call index), the value
* was LOST pre-fix because both stages PUSHed AX (junk for floats) +
* never spilled X0 across the idx/base eval, then re-emitted MOVSS/
* MOVSD X0, (BX) using the already-clobbered X0.
*
* The bug was load-bearing only after #122 routed the value through
* X0 (pre-#122 it sat in AX and the existing PUSH AX accidentally
* protected it). Broken byte-identically between cstage and wwstage,
* so pre-existing + non-regression but exposed by #122.
*
* Fix mirrors the *p = v float deref-store at cmd/w6c/cgen.c:4187
* (the only other arr[i]= /-style float store): for float elements,
* substitute the PUSHQ AX / POPQ AX pair around the idx/base eval
* with SUBQ $8,SP + MOVSS/MOVSD X0,(SP) ... MOVSS/MOVSD (SP),X0 +
* ADDQ $8,SP. Non-float keeps PUSHQ AX (the original) so the str/
* slice 3-word pop order at the end of the branch is undisturbed.
*
* The fix changes asm shape for ALL float arr[i]= stores (its own
* scoped fold, byte-identical between stages); covered by the 990-
* 997 byte-id gates + this test's standalone w6c vs w6c_ww cmp.
*
* Rows cover:
* - f64_call_index: a[geti(1.0)] = 1.5f64 — the canonical repro,
* geti is a fn that adds 1 and returns i32 (its body clobbers X0).
* Pre-fix exit=2 (a[2] held geti's last X0=2.0 → cast i32=2);
* post-fix exit=1 (a[2] = 1.5 → cast i32 = 1).
* - f32_call_index: same shape, f32 element — pre-fix even worse
* because CVTSD2SS at #104 only touched X0; AX store would be
* garbage. Post-fix exit=1 same as f64.
* - f64_lit_index: a[2] = 1.5f64 with a LITERAL index — pre-fix
* this already worked (cgexpr of an int literal doesn't clobber
* X0). Regression guard.
* - f64_localvar_index: a[k] = 1.5f64 with k = 2 (local var) —
* pre-fix also worked (MOVQ off(BP), AX doesn't clobber X0).
* Regression guard.
* - f64_arith_index: a[k + 1] = 1.5f64 — integer arith on locals
* doesn't clobber X0 either; pre-fix worked. Regression guard.
*
* Each row carries (a) cstage `ww build` + run asserting exit code
* and (b) w6c vs w6c_ww `.s` cmp (rule-10 byte-id).
*/
#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[] = {
{ "f64_call_index",
"package main;\n"
"export fn geti(x: f64) i32 = {\n"
" let y: f64 = x + 1.0;\n"
" return (y: i32);\n"
"};\n"
"export fn main() i32 = {\n"
" let a: [4]f64 = [99.0, 99.0, 99.0, 99.0];\n"
" a[geti(1.0)] = 1.5f64;\n"
" let v: i32 = (a[2]: i32);\n"
" return v;\n"
"};\n", 1 },
/* Body of getj uses f64 arith → clobbers X0 via the materialise-
* literal-in-X0 sequence and the ADDSD. The call's argument is
* int (not float), so the call-arg-push goes through the integer
* convention and avoids a pre-existing cs/ww f32-arg-push MOVSD-
* vs-MOVSS divergence in pre-call arg materialisation (sibling
* bug, filed separately). Pre-fix the post-call X0 (set to the
* residue of 1.0+1.0 = 2.0) would overwrite the 1.5f32 in X0,
* yielding garbage. */
{ "f32_call_index",
"package main;\n"
"export fn getj(seed: i32) i32 = {\n"
" let y: f64 = (seed: f64) + 1.0;\n"
" return (y: i32);\n"
"};\n"
"export fn main() i32 = {\n"
" let a: [4]f32 = [99.0f32, 99.0f32, 99.0f32, 99.0f32];\n"
" a[getj(1)] = 1.5f32;\n"
" let v: i32 = (a[2]: i32);\n"
" return v;\n"
"};\n", 1 },
{ "f64_lit_index",
"package main;\n"
"export fn main() i32 = {\n"
" let a: [4]f64 = [99.0, 99.0, 99.0, 99.0];\n"
" a[2] = 1.5f64;\n"
" let v: i32 = (a[2]: i32);\n"
" return v;\n"
"};\n", 1 },
{ "f64_localvar_index",
"package main;\n"
"export fn main() i32 = {\n"
" let a: [4]f64 = [99.0, 99.0, 99.0, 99.0];\n"
" let k: i32 = 2;\n"
" a[k] = 1.5f64;\n"
" let v: i32 = (a[2]: i32);\n"
" return v;\n"
"};\n", 1 },
{ "f64_arith_index",
"package main;\n"
"export fn main() i32 = {\n"
" let a: [4]f64 = [99.0, 99.0, 99.0, 99.0];\n"
" let k: i32 = 1;\n"
" a[k + 1] = 1.5f64;\n"
" let v: i32 = (a[2]: i32);\n"
" return v;\n"
"};\n", 1 },
{ 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, "arrfcidx: 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/wwafc_%d_d_%d",
getpid(), i);
mkdir(tmpdir, 0755);
char rmcmd[160];
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
char src[128], outbin[128], cs_s[128], ws_s[128];
snprintf(src, sizeof src, "%s/wwafc_%d_%d.ww",
tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/wwafc_%d_%d",
tmpdir, getpid(), i);
snprintf(cs_s, sizeof cs_s, "%s/wwafc_%d_%d_cs.s",
tmpdir, getpid(), i);
snprintf(ws_s, sizeof ws_s, "%s/wwafc_%d_%d_ww.s",
tmpdir, getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { runwait(rmcmd); fail++; continue; }
fputs(rows[i].src, f);
fclose(f);
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++;
}
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 arr-float-call-idx tests failed\n", fail, n);
return 1;
}
printf("arrfcidx: %d/%d ok (cstage run + cs==ww byte-id)\n",
n, n);
return 0;
}