Files
ww/test/wcc/946_floatarr_run.c
Hojun-Cho 0917fee48d wcc: float array-element loads to X0 + indexed-float consumer (#119)
cgindex's element-load sites ended in the integer loadopsz (MOVQ/MOVL
into AX), with no float branch — so an f32/f64 array element landed in
a GPR while the consumer's ADDSD/MOVSD read a stale X0. Add a float-
element branch (MOVSS f32 / MOVSD f64 into X0) at all three wwstage
cgindex sites (global, baselocal, fallback) and both cstage N_INDEX
element-load sites, deriving float-ness from the SAME stamped element
tinfo the esz already reads: new elemisfloatc/elemisf32c helpers
(mirroring elemissignedc) for ident bases, typeisfloat/typeisf32(n.type_)
for N_DOT/N_INDEX bases — never a fresh node-stamp that could hit an
unstamped base (#121).

The load fix cannot land alone: the wwstage consumer (cgbin/cgcast)
classified an indexed float operand as INTEGER (no exprfloatkind N_INDEX
arm) and fell to PUSHQ/ADDQ/MOVSXD, while the cstage read the stamped
operand type and used ADDSD/CVTTSD2SI. That divergence is pre-existing
on master (proven: master cs vs ww already differ on `a[0]+a[1]`),
contradicting the original "consumer already expects X0, cs==ww"
premise; load-only would leave the wwstage incoherent (value in X0,
consumed from AX) and still cs!=ww. So this also adds the exprfloatkind
N_INDEX arm — safe because the index-result type_ IS checker-stamped
(cgindex reads it for esz), unlike the unstamped-N_MLET case deferred
under #121. With both, f64 arrays are runtime-correct and both stages
emit byte-identical asm.

946_floatarr_run: f64 element add / trunc / non-adjacent index assert
the value + cs==ww; the f32 row asserts cs==ww only — its runtime value
is blocked by a SEPARATE store-side bug (f32 array-element store writes
AX raw double low-bits instead of CVTSD2SS-narrowed X0), filed as
#119-store. Regen w6c/wwdump combined.ww (cgenexpr.ww + cgenutil.ww
embedded).
2026-05-26 13:10:46 +09:00

212 lines
6.7 KiB
C

/*
* 946_floatarr_run — runtime + byte-id net for #119: a float ARRAY
* ELEMENT load must land in X0 (MOVSS/MOVSD), not the integer register
* file (MOVQ → AX). cgindex's element-load sites ended in the integer
* loadopsz, so `let a: [3]f64 = [...]; a[0] + a[1]` loaded the element
* into AX while the consumer's ADDSD read a stale X0 → wrong sum. #119
* adds a float-element branch (MOVSS f32 / MOVSD f64 into X0) at all
* three wwstage cgindex sites (global, baselocal, fallback) and both
* cstage N_INDEX element-load sites, deriving float-ness from the same
* stamped element tinfo the esz already reads (elemisfloatc/elemisf32c
* for ident bases, typeisfloat/typeisf32(n.type_) for N_DOT/N_INDEX
* bases — never an unstamped node-stamp, dodging the #121 trap).
*
* #119 also required a wwstage exprfloatkind N_INDEX arm: the consumer
* (cgbin / cgcast) classified an indexed float operand as INTEGER and
* fell to PUSHQ/ADDQ/MOVSXD, while the cstage read the stamped operand
* type and used ADDSD/CVTTSD2SI — a rule-10 divergence the load fix
* exposed. The N_INDEX result type_ is checker-stamped (cgindex reads
* it for esz), so this is not the unstamped-N_MLET case deferred under
* #121.
*
* Each row carries (a) a cstage `ww build` + run asserting the exit
* code, and (b) a w6c vs w6c_ww `.s` cmp (rule-10 byte-id). A row whose
* want_exit is RUN_SKIP runs only the byte-id leg: the f32 row exercises
* the f32 LOAD + cs==ww but its runtime VALUE is blocked by a SEPARATE,
* pre-existing bug — the f32 array-element STORE writes AX (the raw
* double low-bits) instead of the CVTSD2SS-narrowed X0 single, so every
* f32 array slot reads back 0.0f. That store-side twin is filed
* separately (#119-store); this probe still proves the f32 LOAD shape +
* cs==ww byte-identity.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#define RUN_SKIP (-1)
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 element load + float add: 1.5 + 2.5 == 4.0. On the bug the
* elements loaded into AX and the ADDSD read a stale X0 -> != 4.0. */
{ "f64_arith",
"package main;\n"
"export fn main() i32 = {\n"
" let a: [3]f64 = [1.5, 2.5, 9.0];\n"
" if (a[0] + a[1] != 4.0) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
/* f64 element -> i32 cast: 1.5 truncates to 1. On the bug the cast
* operand classified integer (MOVSXD on AX) not CVTTSD2SI on X0. */
{ "f64_trunc",
"package main;\n"
"export fn main() i32 = {\n"
" let a: [3]f64 = [1.5, 2.5, 9.0];\n"
" return a[0]: i32;\n"
"};\n", 1 },
/* third element, non-adjacent index: a[2] == 9.0. */
{ "f64_elem2",
"package main;\n"
"export fn main() i32 = {\n"
" let a: [3]f64 = [1.5, 2.5, 9.0];\n"
" if (a[2] != 9.0) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
/* f32 element load (MOVSS into X0) + add, suffixed literals so
* fold-1 narrows them. BYTE-ID ONLY: the runtime value is blocked by
* the f32 array-element STORE bug (#119-store), so we assert only
* that both stages emit the same (correct-load) asm. */
{ "f32_arith_byteid",
"package main;\n"
"export fn main() i32 = {\n"
" let b: [2]f32 = [1.5f32, 2.5f32];\n"
" if ((b[0] + b[1]): f64 != 4.0) { return 1; };\n"
" return 0;\n"
"};\n", RUN_SKIP },
{ 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, "floatarr: 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/wwfarr_%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 (skipped for byte-id-only rows). */
if (rows[i].want_exit != RUN_SKIP) {
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwfarr_%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/wwfarr_%d_%d_cs.s",
getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/wwfarr_%d_%d_ww.s",
getpid(), i);
char cmd[2048];
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 float-array element tests failed\n",
fail, n);
return 1;
}
printf("floatarr: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n);
return 0;
}