Files
ww/test/wcc/917_def_float_lit_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

232 lines
7.0 KiB
C

/*
* 917_def_float_lit_run — runtime + byte-id net for #129 Phase A.1:
* module-level `def NAME: f64 = literal;` (and `def: f32 = …`)
* silently fell through emit_defs's int-only `fold_int_literal` gate,
* so no DATAW row landed in the .data section — `w6l` failed link
* with `undefined reference to 'main.NAME'`. Symmetric latent bug in
* emit_lets's float arm: it only handled bare N_FLOATLIT, never
* peeled N_UN(MINUS/PLUS, N_FLOATLIT), so `let g: f64 = -1.5;`
* silently zero-emitted into the same undef-ref shape.
*
* Phase A.1 fix (rule-12 sea-of-stars consolidation):
* - cstage: extract `emit_floatlit_data(out, c, dir, name, T, rhs)`
* helper, called by emit_lets's float arm (now collapsed) AND
* emit_defs (new float arm). N_CAST + N_UN(MINUS/PLUS,
* N_FLOATLIT) peeled inside the helper.
* - wwstage: parallel `emitfloatlitdata` helper. Negation via
* IEEE-754 sign-bit XOR (avoids dragging the math bitcast helpers
* into cgen).
* - LOAD side: cstage's N_IDENT float-let arm and wwstage's cgident
* def-branch both gated on let_islet/deflookup-but-not-float; the
* gate widens to also catch float defs (they emit through the
* same mod_mangle / emitsymname symbol).
*
* Bootstrap NEUTRAL: zero current `def: f{32,64} = lit` consumers in
* lib/ or selfhost/. The N_UN-peel collateral fix for lets has no
* shipped consumers either (audit: no `let g: f64 = -lit;` anywhere).
*
* Rows cover both fix sides (DATA emit + LOAD) and the N_UN collateral:
* - f64_def_pos: `def K: f64 = 1.5;` — was undef-ref pre-fix, exit
* 1 (1.5 → i32 truncates to 1).
* - f64_def_neg: `def K: f64 = -1.5;` — was undef-ref pre-fix; exit
* 255 (sign-bit flip via XOR; -1.5 → i32 truncates to -1 → 255
* in unsigned exit byte).
* - f32_def_pos: `def K: f32 = 1.5f32; …` — same shape, f32 width.
* - f64_let_neg: `let g: f64 = -1.5;` — pre-existing latent bug
* (N_UN peel never added to emit_lets); now fixed by the same
* helper.
* - f64_let_pos: `let g: f64 = 1.5;` — regression guard (existing
* bare-N_FLOATLIT path).
* - f32_let_pos: `let g: f32 = 1.5f32;` — regression guard, f32 path.
*
* 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_def_pos",
"package main;\n"
"def K: f64 = 1.5;\n"
"export fn main() i32 = {\n"
" let x: f64 = K;\n"
" return (x: i32);\n"
"};\n", 1 },
{ "f64_def_neg",
"package main;\n"
"def K: f64 = -1.5;\n"
"export fn main() i32 = {\n"
" let x: f64 = K;\n"
" return (x: i32);\n"
"};\n", 255 /* -1 as unsigned exit byte */ },
{ "f32_def_pos",
"package main;\n"
"def K: f32 = 1.5f32;\n"
"export fn main() i32 = {\n"
" let x: f32 = K;\n"
" return (x: i32);\n"
"};\n", 1 },
/* Matrix-closure: f32 + neg combined directly. Exercises the
* f32-narrow path PLUS the top-byte-XOR negation in one row.
* The other rows cover each leg individually (f64_def_neg for
* negation, f32_def_pos for f32 narrow); this row pins they
* compose correctly. */
{ "f32_def_neg",
"package main;\n"
"def K: f32 = -1.5f32;\n"
"export fn main() i32 = {\n"
" let x: f32 = K;\n"
" return (x: i32);\n"
"};\n", 255 /* -1 as exit byte */ },
/* Pre-existing latent: emit_lets's float arm never peeled
* N_UN(MINUS,N_FLOATLIT). The class-closure consolidation
* fix in emit_floatlit_data heals this too. */
{ "f64_let_neg",
"package main;\n"
"let g: f64 = -1.5;\n"
"export fn main() i32 = {\n"
" return (g: i32);\n"
"};\n", 255 },
{ "f64_let_pos",
"package main;\n"
"let g: f64 = 1.5;\n"
"export fn main() i32 = {\n"
" return (g: i32);\n"
"};\n", 1 },
{ "f32_let_pos",
"package main;\n"
"let g: f32 = 1.5f32;\n"
"export fn main() i32 = {\n"
" return (g: i32);\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, "deflitf: 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/wwdflf_%d_d_%d",
getpid(), i);
mkdir(tmpdir, 0755);
char rmcmd[128];
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
char src[128], outbin[128], cs_s[128], ws_s[128];
snprintf(src, sizeof src, "%s/wwdflf_%d_%d.ww",
tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/wwdflf_%d_%d",
tmpdir, getpid(), i);
snprintf(cs_s, sizeof cs_s, "%s/wwdflf_%d_%d_cs.s",
tmpdir, getpid(), i);
snprintf(ws_s, sizeof ws_s, "%s/wwdflf_%d_%d_ww.s",
tmpdir, getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; runwait(rmcmd); 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 def-float-lit tests failed\n", fail, n);
return 1;
}
printf("deflitf: %d/%d ok (cstage run + cs==ww byte-id)\n",
n, n);
return 0;
}