wcc: tuple-param ABI via SSE/GP arg cursors (#163)
Tuples were unhandled as parameters — no tuple arm in arg-push, arg-pop, or callee-recv in either stage — so a tuple param fell to the 1-GP-word else and dropped all but its first element (integer tuple params too; floats doubly lost). Add tuple-param arms (SEND push+pop, callee RECV) across both stages, reusing #164's per-element SysV classify with the 6-GP (DI,SI,DX,CX,R8,R9) + 8-SSE (X0-X7) arg cursors. A frame slot @tupargscr decouples the producing call's return cursor from the overlapping arg cursor (capture-before-clobber). Overflow (>6 GP / >8 SSE) fails loud (rule 7). Scoped to the N_CALL producer; first-class tuple values (ident/literal) remain a separate unimplemented gap. Gate-blind (the bootstrap passes no tuple params) — covered by table-driven probe 905, which proves pre-fix element-drop and the loud-stop.
This commit is contained in:
319
test/wcc/905_tupparam_run.c
Normal file
319
test/wcc/905_tupparam_run.c
Normal file
@@ -0,0 +1,319 @@
|
||||
/*
|
||||
* 905_tupparam_run — runtime + byte-id net for #163, the tuple-PARAM ABI
|
||||
* (the param twin of #164's tuple RETURN).
|
||||
*
|
||||
* THE BUG (#163, LIVE drop on master): a tuple passed AS AN ARGUMENT was
|
||||
* unhandled in BOTH stages — no tuple arm in the cgcall arg push, the
|
||||
* cgcall arg pop, OR the callee cgfnparams receive. A tuple-typed call
|
||||
* result (`f(g())` where g returns a tuple) left its elements in the
|
||||
* return-ABI cursor (AX/DX/CX/R8 + X0/X1, per #164); the SEND fell to the
|
||||
* 1-GP-word `else` (PUSHQ AX / POPQ DI) so ALL BUT THE FIRST ELEMENT was
|
||||
* dropped, and the callee read its tuple param as a single GP word. This
|
||||
* broke INTEGER tuple params too; float elements were doubly lost (they
|
||||
* ride X0/X1, never AX).
|
||||
*
|
||||
* THE FIX: per-element SysV class placement reusing #164's helper. SEND —
|
||||
* cgexpr leaves the tuple in the return cursor; restage it into a frame
|
||||
* slot (@tupargscr) by class via tuple_store/tupstore, then push the slot
|
||||
* words high->low so the pop drains slot+0 first into the SysV ARG cursor
|
||||
* (DI/SI/.. + X0..X7). The frame slot decouples the return-class regs
|
||||
* (which OVERLAP the arg-class regs) from the arg placement. RECV — the
|
||||
* callee walks the tuple's elements over the arg cursor, storing each into
|
||||
* its frame slot positionally. Symmetric across cstage (cmd/w6c/cgen.c)
|
||||
* and wwstage (cgenutil.ww pushargsrev + cgenexpr.ww cgcall pop +
|
||||
* cgendecl.ww cgfnparams).
|
||||
*
|
||||
* SCOPE: register-class tuple ARGS produced by a CALL (the only form that
|
||||
* materialises a tuple value today — `let t = (1,2)` as a first-class
|
||||
* value is a separate unimplemented gap, so the SEND scopes to the N_CALL
|
||||
* producer and never pushes stale regs, rule 7). Arg-register overflow
|
||||
* loud-stops (the partial-spill stitch is out of scope, twin of #164's
|
||||
* cap); the loudstop row asserts BOTH stages ERROR.
|
||||
*
|
||||
* GATE-BLIND TO BYTE-ID ALONE: the bootstrap passes no tuple params, and
|
||||
* pre-fix both stages were symmetric-WRONG (both PUSHQ AX), so the cs==ww
|
||||
* .s gate HOLDS on master for the value rows — they diverge only at
|
||||
* RUNTIME. Each value row carries BOTH dimensions (modelled on 956):
|
||||
* (a) cstage `ww build` + run, asserting the exit code (catches #163:
|
||||
* master returns the wrong exit / dropped element).
|
||||
* (b) w6c vs w6c_ww `.s` cmp (rule-10: both stages fixed identically).
|
||||
*/
|
||||
#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;
|
||||
int want_compile_fail; /* loud-stop rows must NOT compile */
|
||||
};
|
||||
|
||||
static const struct row rows[] = {
|
||||
/* HEADLINE — (f64, f64) arg. Pre-fix the SEND pushes only AX (the
|
||||
* two floats stay stranded in X0/X1) and the callee reads one GP
|
||||
* word; t.0+t.1 != 8.0 -> return 1. Post-fix each float rides the
|
||||
* SSE arg cursor (X0,X1). */
|
||||
{ "f64f64_arg",
|
||||
"package main;\n"
|
||||
"fn pair(a: f64, b: f64) (f64, f64) = { return (a, b); };\n"
|
||||
"fn add(t: (f64, f64)) f64 = { return t.0 + t.1; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tif (add(pair(3.0, 5.0)) != 8.0) { return 1; };\n"
|
||||
"\treturn 0;\n"
|
||||
"};\n", 0 },
|
||||
/* (i64, i64) arg — proves the broader INTEGER-tuple-param drop is
|
||||
* fixed (master dropped the second i64 too). e0->DI, e1->SI. */
|
||||
{ "i64i64_arg",
|
||||
"package main;\n"
|
||||
"fn pair(a: i64, b: i64) (i64, i64) = { return (a, b); };\n"
|
||||
"fn add(t: (i64, i64)) i64 = { return t.0 + t.1; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tif (add(pair(3, 5)) != 8) { return 1; };\n"
|
||||
"\treturn 0;\n"
|
||||
"};\n", 0 },
|
||||
/* (f64, i64) — class independent of position: f64@X0 (SSE cursor),
|
||||
* i64@DI (INTEGER cursor), independent counters. */
|
||||
{ "f64i64_arg",
|
||||
"package main;\n"
|
||||
"fn mk(a: f64, b: i64) (f64, i64) = { return (a, b); };\n"
|
||||
"fn add(t: (f64, i64)) i64 = { return (t.0: i64) + t.1; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tif (add(mk(3.0, 5)) != 8) { return 1; };\n"
|
||||
"\treturn 0;\n"
|
||||
"};\n", 0 },
|
||||
/* (i64, f64) — order-swap: i64@DI, f64@X0. Confirms the float lands
|
||||
* in the next XMM regardless of its positional slot. */
|
||||
{ "i64f64_arg",
|
||||
"package main;\n"
|
||||
"fn mk(a: i64, b: f64) (i64, f64) = { return (a, b); };\n"
|
||||
"fn add(t: (i64, f64)) i64 = { return t.0 + (t.1: i64); };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tif (add(mk(3, 5.0)) != 8) { return 1; };\n"
|
||||
"\treturn 0;\n"
|
||||
"};\n", 0 },
|
||||
/* (f64, str) — SSE + wide (24B {ptr,len,cap}) coexist. The f64
|
||||
* rides X0 (SSE, consuming no GP slot); the str rides DI/SI/DX
|
||||
* (INTEGER cursor). f=4.0, s.len=5 -> 4+5 = 9. */
|
||||
{ "f64str_arg",
|
||||
"package main;\n"
|
||||
"fn mk(a: f64) (f64, str) = { return (a, \"hello\"); };\n"
|
||||
"fn add(t: (f64, str)) i64 = {\n"
|
||||
"\treturn (t.0: i64) + (t.1.len: i64);\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tif (add(mk(4.0)) != 9) { return 1; };\n"
|
||||
"\treturn 0;\n"
|
||||
"};\n", 0 },
|
||||
/* MULTI-TUPLE-ARG, ONE CALL — f(g(), h()) where BOTH args are
|
||||
* tuple-producing calls. Proves @tupargscr (single-slot-per-fn) is
|
||||
* REUSED per arg, not COLLIDED: pushargsrev evals right-to-left, so
|
||||
* h() restages into the slot + drains it to the stack BEFORE g()
|
||||
* restages into the SAME slot (h's words already pushed, safe to
|
||||
* overwrite). Drain forward: s->DI,SI; t->DX,CX. 1+2+3+4 = 10. A
|
||||
* collision (both restaged before either pushed) would corrupt the
|
||||
* first-pushed tuple's words. */
|
||||
{ "two_tuple_args",
|
||||
"package main;\n"
|
||||
"fn pair(a: i64, b: i64) (i64, i64) = { return (a, b); };\n"
|
||||
"fn add4(s: (i64, i64), t: (i64, i64)) i64 = {\n"
|
||||
"\treturn s.0 + s.1 + t.0 + t.1;\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tif (add4(pair(1, 2), pair(3, 4)) != 10) { return 1; };\n"
|
||||
"\treturn 0;\n"
|
||||
"};\n", 0 },
|
||||
/* CONTROL — a tuple arg threaded through a chain of two calls,
|
||||
* proving the SEND/RECV round-trips through the slot intact. */
|
||||
{ "f64f64_chain",
|
||||
"package main;\n"
|
||||
"fn pair(a: f64, b: f64) (f64, f64) = { return (a, b); };\n"
|
||||
"fn id(t: (f64, f64)) f64 = { return t.0 * 10.0 + t.1; };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tif (id(pair(3.0, 5.0)) != 35.0) { return 1; };\n"
|
||||
"\treturn 0;\n"
|
||||
"};\n", 0 },
|
||||
/* LOUD-STOP — 5 i64 scalars + an (i64,i64) tuple = 7 INTEGER arg
|
||||
* eightbytes, overflowing the 6 GP arg regs (DI/SI/DX/CX/R8/R9).
|
||||
* The partial-spill stitch is out of scope (twin of #164's cap), so
|
||||
* BOTH stages must FAIL TO COMPILE (rule 7: surface, never silently
|
||||
* drop). Master has no tuple-arg arm (pushes the tuple as 1 word ->
|
||||
* 6 GP, no overflow) and builds the miscompile, so want_compile_fail
|
||||
* discriminates. */
|
||||
{ "gp_overflow_loudstop",
|
||||
"package main;\n"
|
||||
"fn pair(a: i64, b: i64) (i64, i64) = { return (a, b); };\n"
|
||||
"fn f(a: i64, b: i64, c: i64, d: i64, e: i64, t: (i64, i64)) i64 = {\n"
|
||||
"\treturn a + b + c + d + e + t.0 + t.1;\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\treturn (f(1, 2, 3, 4, 5, pair(6, 7)): i32);\n"
|
||||
"};\n", 0, 1 },
|
||||
{ NULL, NULL, 0, 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, "tupparam: w6c_ww missing — cannot run the "
|
||||
"cs==ww byte-id gate\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/wwtupp_%d_%d.ww", getpid(), i);
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (f == NULL) { fail++; continue; }
|
||||
fputs(rows[i].src, f);
|
||||
fclose(f);
|
||||
|
||||
char cmd[2048];
|
||||
|
||||
/* LOUD-STOP rows: the arg-reg 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. No .s is produced, so
|
||||
* the byte-id cmp is skipped. */
|
||||
if (rows[i].want_compile_fail) {
|
||||
char ldir[64];
|
||||
snprintf(ldir, sizeof ldir, "/tmp/wwtupp_%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 (arg-reg overflow)\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/wwtupp_%d_d_%d",
|
||||
getpid(), i);
|
||||
mkdir(tmpdir, 0755);
|
||||
|
||||
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/wwtupp_%d_%d_cs.s",
|
||||
getpid(), i);
|
||||
snprintf(ws_s, sizeof ws_s, "/tmp/wwtupp_%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 tuple-param tests failed\n", fail, n);
|
||||
return 1;
|
||||
}
|
||||
printf("tupparam: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user