Files
ww/test/wcc/955_f64xmm_run.c
Hojun-Cho 4c4006d854 test: 955 add negative-f64-lit compare rows (#103 FACE X)
-8f64 is N_UN(TK_MINUS) wrapping a float-typed N_INTLIT; the wwstage
exprfloatkind must recurse through the unary into the N_INTLIT-float
arm. Two rows: a true-case catcher (g(-8.0) == -8f64 -> 1; pre-fix the
literal never reaches X0 so the compare is always false and g(-8.0)
wrongly returns 0, plus a cs!=ww .s divergence) and a false-case
control (g(8.0) -> 0). Both pass byte-identically on the fix.
2026-05-25 15:38:47 +09:00

225 lines
7.5 KiB
C

/*
* 955_f64xmm_run — runtime + byte-id regression net for #103: an f64
* value failing to materialise in XMM (X0) before an SSE op. Two faces,
* same class, both GATE-BLIND (cstage cgen.c and wwstage cgenexpr.ww
* emitted byte-identical-but-wrong asm, so the 990-997 byte-id gates
* could never catch a reintroduction — only an executed-and-checked
* runtime probe can).
*
* FACE X — a no-decimal float-typed integer literal (`0f64`, `8f64`)
* is an N_INTLIT carrying float TYPE; the integer-immediate path
* stranded it in AX, so `n == 0f64` compared a stale X0 (true for
* all n) and `(8f64 * 10.0): i32` read garbage. Fixed by routing
* the float-typed N_INTLIT through the float-constant-in-X0 emit
* (cgen.c cgexpr_float / cgenexpr.ww cgfloatbits), plus the wwstage
* exprfloatkind N_INTLIT arm so the downstream f64->i32 cast emits
* CVTTSD2SI not MOVSXD (the #101 structural-vs-stamped asymmetry).
* FACE Z — a tuple positional f64 field read (`r.0`, r:(f64,i64))
* loaded via the integer op into AX, so `r.0 == 0.0` was wrongly
* true. Fixed by a fld_isfloat branch -> MOVSD/MOVSS into X0
* (cgen.c:5910 / cgenexpr.ww tuple arm), mirroring the struct-field
* float load at cgen.c:1462,1838 (the #96 pattern).
*
* Each row carries BOTH dimensions (like 953_f64crossmod_run):
* (a) cstage `ww build` + run, asserting the exit code.
* (b) w6c vs w6c_ww `.s` cmp — FAILS if the stages diverge. Both
* stages are fixed identically, so this stays byte-identical
* before and after; it catches one stage being fixed without the
* other.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.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[] = {
/* FACE X false-case: g(8.0) must be 0. On the bug `n == 0f64`
* compares against a stale X0 (true for all n) -> 1. */
{ "x_cmp_false",
"package main;\n"
"fn g(n: f64) i32 = { if (n == 0f64) { return 1; }; return 0; };\n"
"export fn main() i32 = { return g(8.0); };\n", 0 },
/* FACE X true-case: g(0.0) must still be 1 — the genuine equal
* case must survive the fix. */
{ "x_cmp_true",
"package main;\n"
"fn g(n: f64) i32 = { if (n == 0f64) { return 1; }; return 0; };\n"
"export fn main() i32 = { return g(0.0); };\n", 1 },
/* FACE X arith: (8f64 * 10.0): i32 == 80. On the bug 8f64 never
* reaches X0, the MULSD reads stale X0 -> garbage. */
{ "x_arith",
"package main;\n"
"export fn main() i32 = { return (8f64 * 10.0): i32; };\n", 80 },
/* FACE X negative-lit: `-8f64` is N_UN(TK_MINUS) wrapping a float-
* typed N_INTLIT, so the wwstage exprfloatkind must recurse through
* the unary into the new N_INTLIT-float arm. g(-8.0) == -8f64 must be
* true -> 1; on the bug the literal never reaches X0, the compare is
* always false, and g(-8.0) wrongly returns 0. */
{ "x_neg_cmp_true",
"package main;\n"
"fn g(n: f64) i32 = { if (n == -8f64) { return 1; }; return 0; };\n"
"export fn main() i32 = { return g(-8.0); };\n", 1 },
/* FACE X negative-lit control: g(8.0) == -8f64 must stay false -> 0.
* Guards the negative-literal path against over-reach. */
{ "x_neg_cmp_false",
"package main;\n"
"fn g(n: f64) i32 = { if (n == -8f64) { return 1; }; return 0; };\n"
"export fn main() i32 = { return g(8.0); };\n", 0 },
/* FACE Z: tuple positional f64 field compare. r.0 == 0.0 with
* r = (8.0, 0) must be false -> 9. On the bug r.0 loads into AX
* (integer op), `== 0.0` reads stale X0 -> wrongly true -> 5. */
{ "z_tuple_field",
"package main;\n"
"fn norm(n: f64) (f64, i64) = { return (n, 0); };\n"
"export fn main() i32 = {\n"
" const r = norm(8.0);\n"
" if (r.0 == 0.0) { return 5; };\n"
" return 9;\n"
"};\n", 9 },
/* CONTROL: the let-bound spelling (`const m = r.0; m == 0.0`) was
* already correct (the let-init is float-aware) and must STAY
* correct -> 9. Guards against the FACE-Z fix over- or
* under-reaching. */
{ "z_letbound_control",
"package main;\n"
"fn norm(n: f64) (f64, i64) = { return (n, 0); };\n"
"export fn main() i32 = {\n"
" const r = norm(8.0);\n"
" const m = r.0;\n"
" if (m == 0.0) { return 5; };\n"
" return 9;\n"
"};\n", 9 },
{ 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, "f64xmm: 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 src[64];
snprintf(src, sizeof src, "/tmp/wwf64m_%d_%d.ww", getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; continue; }
fputs(rows[i].src, f);
fclose(f);
/* (a) cstage build + run. */
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwf64m_%d_d_%d",
getpid(), i);
mkdir(tmpdir, 0755);
char cmd[2048];
snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s",
tmpdir, bin, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: cstage build failed\n",
rows[i].label);
fail++;
unlink(src); rmdir(tmpdir);
continue;
}
char outbin[128];
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
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++;
}
unlink(outbin); rmdir(tmpdir);
/* (b) cs==ww byte-id gate: emit .s from both stages, cmp. */
char cs_s[64], ws_s[64];
snprintf(cs_s, sizeof cs_s, "/tmp/wwf64m_%d_%d_cs.s",
getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/wwf64m_%d_%d_ww.s",
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++; unlink(src); 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++; unlink(src); unlink(cs_s); 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++;
}
unlink(src); unlink(cs_s); unlink(ws_s);
}
if (fail) {
fprintf(stderr, "%d/%d f64 xmm-materialise tests failed\n",
fail, n);
return 1;
}
printf("f64xmm: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n);
return 0;
}