w6c: f32 arg-push/pop uses MOVSS not MOVSD (#143)

cstage spilled f32 args via MOVSD (8-byte); ABI-correct is MOVSS (4-byte,
single class) per SysV (ref/qbe amd64/emit.c:524 — slot-copy-through-XMM
follows the float class). wwstage already emitted MOVSS; align cstage up
via op_for(node_isf32) at the arg PUSH (cgen.c:5366) + POP (cgen.c:5469).
Byte-id-only divergence (callee reads the f32 param low-32 regardless),
but it blocked cs==ww — closes the f32-arg-push half of the float-register
family (#119/#122/#125/#157). Bootstrap-NEUTRAL (no f32-arg caller in the
990-997 gated path). Test 907_f32arg_run (f32-arg push single/multi/mixed/
stack, cs==ww byte-id). Make test 187/187 incl 990-997.

Unblocks fold-5b (strconv f32tos passes f32 to f32bits).
This commit is contained in:
2026-05-27 16:17:09 +09:00
parent 0e66073e32
commit aff7725f8f
3 changed files with 248 additions and 2 deletions

229
test/wcc/907_f32arg_run.c Normal file
View File

@@ -0,0 +1,229 @@
/*
* 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 <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[] = {
/* 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 src[64];
snprintf(src, sizeof src, "/tmp/wwf32a_%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/wwf32a_%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/wwf32a_%d_%d_cs.s",
getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/wwf32a_%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 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;
}