wcc: multi-float tuple return via SSE cursor (#164, #107)

A multi-float tuple return mis-routed: SEND pushed a stale AX leaving the
float stranded in X0, while RECV (#105) read every float from X0 — so a
(f64,f64) return collided both floats. Add an SSE cursor [X0,X1] parallel to
the GP cursor [AX,DX,CX,R8], placing each element by its SysV class +
within-class index (ref/qbe/amd64/sysv.c retr), symmetric send/recv across
both stages, via a generic tuple_store/tupstore+tupsse helper that #171 will
reuse for struct-return convergence. (f64,f64,f64) = 3 SSE eightbytes exceeds
the 2-register cap and now fails loud (rule 7) rather than colliding.

Unifying the 16B and 32B whole-tuple-single-var branches onto the dual cursor
was required for f64+str coexistence; it also fixes a latent str-first
single-var bug (the old 32B branch read .ptr from DX while the send placed it
in AX). No str-first or 32B tuple exists in-tree, so integer paths stay
byte-identical (990-997 green).
This commit is contained in:
2026-05-27 21:01:25 +09:00
parent 028109513e
commit 153c7b3b46
5 changed files with 1102 additions and 572 deletions

View File

@@ -1,8 +1,22 @@
/*
* 956_tuprecv_f64_run — runtime + byte-id regression net for #105: a
* tuple-from-call receive corrupts the f64 word when the callee is
* BRANCHED. Covers ALL THREE receive forms, which share the #83
* tuple_rseq cursor and all carried the same defect:
* 956_tuprecv_f64_run — runtime + byte-id regression net for #105 and
* the #164 (#107) multi-float extension.
*
* #164 (#107): a multi-float tuple return (e.g. (f64,f64)) mis-routes —
* the SEND emitted every float through X0 (cgexpr clobbers X0 per
* element), so two floats collided on X0 and the receive read both from
* X0. The fix gives the tuple return a SysV SSE cursor [X0,X1] parallel
* to the integer cursor [AX,DX,CX,R8]: a float rides the next XMM on an
* INDEPENDENT counter (ref/qbe/amd64/sysv.c retr). The SEND spills each
* float to @tupfscr as it walks (X0 is clobbered by later elements) and
* reloads X0/X1 by SSE index after the integer pops; every receive site
* (single-var 16B/32B, destructure, reassign) reads the float from its
* SSE-cursor reg. SSE caps at 2 (X0,X1) — (f64,f64,f64) loud-stops at
* compile (f64x3_loudstop row asserts the compiler ERRORS, both stages).
*
* #105 (original): a tuple-from-call receive corrupts the f64 word when
* the callee is BRANCHED. Covers ALL THREE receive forms, which share the
* #83 tuple_rseq cursor and all carried the same defect:
* 1. SINGLE-VAR `let r = norm(); ...r.0` (cglet 16B-tuple branch)
* 2. DESTRUCTURE `let (m,i) = norm()` (N_MLET / cgmlet+tupstore)
* 3. REASSIGN `m,i = norm()` (N_MASSIGN / cgmassign+tupstore)
@@ -60,7 +74,13 @@ runwait(const char *cmd)
return -1;
}
struct row { const char *label; const char *src; int want_exit; int chk_stamped; };
struct row {
const char *label;
const char *src;
int want_exit;
int chk_stamped;
int want_compile_fail; /* #164: loud-stop rows must NOT compile */
};
static const struct row rows[] = {
/* BUG — minimal repro. norm is BRANCHED (inner issub() CALL clobbers
@@ -184,6 +204,175 @@ static const struct row rows[] = {
"\tif (i != 0) { return 2; };\n"
"\treturn 0;\n"
"};\n", 0 },
/* #164 (#107) HEADLINE — multi-float (f64, f64), destructure. The
* callee is BRANCHED (issub CALL clobbers X0), so on master BOTH
* elements collide on X0: cgexpr(a) leaves a in X0, cgexpr(b)
* overwrites it, and every receive read spills from X0 -> x==y==b
* (5.0). Post-fix a rides the SSE cursor X0, b rides X1; the receive
* splits them. x=3.0, y=5.0. Pre-fix: x==5.0 -> return 1. */
{ "f64f64_destr_br",
"package main;\n"
"fn issub(n: f64) bool = { return false; };\n"
"fn pair(a: f64, b: f64) (f64, f64) = {\n"
"\tif (issub(a)) { return (a*2.0, b*2.0); };\n"
"\treturn (a, b);\n"
"};\n"
"export fn main() i32 = {\n"
"\tlet (x, y) = pair(3.0, 5.0);\n"
"\tif (x != 3.0) { return 1; };\n"
"\tif (y != 5.0) { return 2; };\n"
"\treturn 0;\n"
"};\n", 0, 1 },
/* #164 HEADLINE — multi-float (f64, f64), SINGLE-VAR whole-tuple
* receive (`let r = pair(); r.0 / r.1`, the 16B rt16 branch). Same
* X0-collision on master; post-fix r.0 from X0, r.1 from X1. */
{ "f64f64_single_br",
"package main;\n"
"fn issub(n: f64) bool = { return false; };\n"
"fn pair(a: f64, b: f64) (f64, f64) = {\n"
"\tif (issub(a)) { return (a*2.0, b*2.0); };\n"
"\treturn (a, b);\n"
"};\n"
"export fn main() i32 = {\n"
"\tconst r = pair(3.0, 5.0);\n"
"\tconst x: f64 = r.0;\n"
"\tconst y: f64 = r.1;\n"
"\tif (x != 3.0) { return 1; };\n"
"\tif (y != 5.0) { return 2; };\n"
"\treturn 0;\n"
"};\n", 0 },
/* #164 HEADLINE — multi-float (f64, f64), REASSIGN into pre-declared
* slots (N_MASSIGN / cgmassign+tupstore). x=3.0, y=5.0. */
{ "f64f64_massign_br",
"package main;\n"
"fn issub(n: f64) bool = { return false; };\n"
"fn pair(a: f64, b: f64) (f64, f64) = {\n"
"\tif (issub(a)) { return (a*2.0, b*2.0); };\n"
"\treturn (a, b);\n"
"};\n"
"export fn main() i32 = {\n"
"\tlet x: f64 = 0.0;\n"
"\tlet y: f64 = 0.0;\n"
"\tx, y = pair(3.0, 5.0);\n"
"\tif (x != 3.0) { return 1; };\n"
"\tif (y != 5.0) { return 2; };\n"
"\treturn 0;\n"
"};\n", 0 },
/* #164 — INTERLEAVED (i64, f64, i64): kills naive position->reg. The
* two i64s ride the INTEGER cursor (AX,DX), the f64 the SSE cursor
* (X0) on an independent counter — so x=AX, z=DX, y=X0. 3-element
* destructure. x=3, y=2.0, z=7. */
{ "i64_f64_i64_destr",
"package main;\n"
"fn issub(n: i64) bool = { return false; };\n"
"fn tri(a: i64, b: f64, c: i64) (i64, f64, i64) = {\n"
"\tif (issub(a)) { return (a*2, b*2.0, c*2); };\n"
"\treturn (a, b, c);\n"
"};\n"
"export fn main() i32 = {\n"
"\tlet (x, y, z) = tri(3, 2.0, 7);\n"
"\tif (x != 3) { return 1; };\n"
"\tif (y != 2.0) { return 2; };\n"
"\tif (z != 7) { return 3; };\n"
"\treturn 0;\n"
"};\n", 0, 1 },
/* #164 — (f64, str): SSE + wide (24B {ptr,len,cap} header) coexist.
* The f64 rides the SSE cursor (X0); the str rides the INTEGER
* cursor (AX,DX,CX) since the float consumes no GP slot. Destructure
* form. f=4.0, s.len=5 ("hello"). */
{ "f64_str_destr",
"package main;\n"
"fn issub(n: f64) bool = { return false; };\n"
"fn fs(n: f64) (f64, str) = {\n"
"\tif (issub(n)) { return (n*2.0, \"x\"); };\n"
"\treturn (n, \"hello\");\n"
"};\n"
"export fn main() i32 = {\n"
"\tlet (f, s) = fs(4.0);\n"
"\tif (f != 4.0) { return 1; };\n"
"\tif (s.len != 5) { return 2; };\n"
"\treturn 0;\n"
"};\n", 0, 1 },
/* #164 STR-FIRST destructure (str, f64): the unification's new-
* coverage shape with the wide header in slot 0. The str rides the
* INTEGER cursor (AX,DX,CX = ptr,len,cap), the f64 the SSE cursor (X0)
* — independent counters, the float consuming no GP slot. The
* destructure path's cursor handled str-first on master too, so this
* pins the dual-cursor restructure PRESERVED it (byte-id both ways)
* AND that the f64 coexists. s.len=5 ("hello"), f=4.0. */
{ "str_f64_destr",
"package main;\n"
"fn issub(n: f64) bool = { return false; };\n"
"fn sf(n: f64) (str, f64) = {\n"
"\tif (issub(n)) { return (\"x\", n*2.0); };\n"
"\treturn (\"hello\", n);\n"
"};\n"
"export fn main() i32 = {\n"
"\tlet (s, f) = sf(4.0);\n"
"\tif (s.len != 5) { return 1; };\n"
"\tif (f != 4.0) { return 2; };\n"
"\treturn 0;\n"
"};\n", 0, 1 },
/* #164 STR-FIRST destructure (str, i64): pure-integer str-first.
* str@AX,DX,CX then i64@R8. Correct on master too (single-cursor
* destructure already routed ptr=AX) — byte-id regression guard that
* the dual cursor left the integer str-first mapping intact. s.len=5,
* k=7. */
{ "str_i64_destr",
"package main;\n"
"fn issub(n: i64) bool = { return false; };\n"
"fn si(n: i64) (str, i64) = {\n"
"\tif (issub(n)) { return (\"x\", n*2); };\n"
"\treturn (\"hello\", n);\n"
"};\n"
"export fn main() i32 = {\n"
"\tlet (s, k) = si(7);\n"
"\tif (s.len != 5) { return 1; };\n"
"\tif (k != 7) { return 2; };\n"
"\treturn 0;\n"
"};\n", 0 },
/* #164 STR-FIRST SINGLE-VAR (str, i64), annotated `let t: (str,i64) =
* si(); t.0/t.1`: the shape the OLD 32B single-var branch got WRONG —
* it read .ptr from DX while the send placed .ptr in AX (self-
* inconsistent), scrambling the slot so t.1 read the str.ptr word.
* DISCRIMINATES: pre-fix t.1 = a large address != 7; post-fix the
* unified dual cursor lands str@AX,DX,CX + i64@R8 so t.1=7. Both
* stages were wrong IDENTICALLY pre-fix (byte-id held, runtime broke),
* right identically post-fix. Annotated (not bare `const r=`) because
* the wwstage 32B single-var branch keys on the N_TTUPLE type node —
* a bare 32B single-var is a pre-existing stage asymmetry out of #164
* scope (16B bare single-var rows above cover the inferred path). */
{ "str_i64_single",
"package main;\n"
"fn issub(n: i64) bool = { return false; };\n"
"fn si(n: i64) (str, i64) = {\n"
"\tif (issub(n)) { return (\"x\", n*2); };\n"
"\treturn (\"hello\", n);\n"
"};\n"
"export fn main() i32 = {\n"
"\tlet t: (str, i64) = si(7);\n"
"\tif (t.1 != 7) { return 1; };\n"
"\tif (t.0.len != 5) { return 2; };\n"
"\treturn 0;\n"
"};\n", 0 },
/* #164 LOUD-STOP — three f64 = 0 GP / 3 SSE exceeds the SSE return
* cap (X0,X1 only). Must FAIL TO COMPILE in BOTH stages (rule-7:
* surface, never silently collide). Master has no SSE cap and
* miscompiles (build succeeds), so want_compile_fail discriminates:
* pre-fix the build succeeds (test fails), post-fix both stages
* error (test passes). */
{ "f64x3_loudstop",
"package main;\n"
"fn tri(a: f64, b: f64, c: f64) (f64, f64, f64) = {\n"
"\treturn (a, b, c);\n"
"};\n"
"export fn main() i32 = {\n"
"\tlet (x, y, z) = tri(1.0, 2.0, 3.0);\n"
"\tif (x != 1.0) { return 1; };\n"
"\tif (y != 2.0) { return 2; };\n"
"\tif (z != 3.0) { return 3; };\n"
"\treturn 0;\n"
"};\n", 0, 0, 1 },
/* CONTROL — all-integer branched 2-tuple. The integer-cursor MOVQ
* path is untouched by the fix (e0/e1 not float), so this is correct
* pre- and post-fix and byte-id both ways. a=5, b=7 -> 12. */
@@ -289,13 +478,51 @@ main(void)
fputs(rows[i].src, f);
fclose(f);
char cmd[2048];
/* #164 LOUD-STOP rows: the SSE-cap overflow must FAIL TO COMPILE
* in BOTH stages (rule-7). Assert (a) cstage `ww build` errors,
* and (b) w6c AND w6c_ww each return non-zero — proving the
* loud-stop fires symmetrically. No .s is produced, so the
* byte-id cmp is skipped. Discriminates against master, which
* has no SSE cap and builds the (mis)compile. */
if (rows[i].want_compile_fail) {
char ldir[64];
snprintf(ldir, sizeof ldir, "/tmp/wwtupf_%d_l_%d",
getpid(), i);
mkdir(ldir, 0755);
snprintf(cmd, sizeof cmd,
"cd %s && %s/ww build %s >/dev/null 2>&1",
ldir, bin, src);
if (runwait(cmd) == 0) {
fprintf(stderr, "row[%s]: cstage build SUCCEEDED, "
"want loud-stop (SSE cap)\n", rows[i].label);
fail++;
}
snprintf(cmd, sizeof cmd, "%s -o /dev/null %s 2>/dev/null",
w6c, src);
if (runwait(cmd) == 0) {
fprintf(stderr, "row[%s]: w6c emitted .s, want "
"loud-stop\n", rows[i].label);
fail++;
}
snprintf(cmd, sizeof cmd, "%s -o /dev/null %s 2>/dev/null",
w6c_ww, src);
if (runwait(cmd) == 0) {
fprintf(stderr, "row[%s]: w6c_ww emitted .s, want "
"loud-stop\n", rows[i].label);
fail++;
}
unlink(src); rmdir(ldir);
continue;
}
/* (a) cstage build + run in a scratch dir. */
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwtupf_%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) {