w6c+wwstage: receive over-cap tuple sret returns at the call site (#10 Fold B)

Fold A made the CALLEE emit an over-capacity tuple return (> 4 GP or > 2
SSE eightbytes) via sret, but every receive site stayed loud-stopped, so
such a fn was not yet usefully callable. Fold B wires the call/receive end
by aligning every receive gate UP to the shared cg_sret_retsize() /
callsretsize() > 0 predicate (never a kind), per Rob's (B) ruling:

  - single-var-let  `let t = f();`      cstage gate generalised from
        TY_STRUCT&&>24 to cg_sret_retsize(lt)>0; the let's slot IS the
        sret dest, the callee writes the whole tuple there, t.0/t.1 read
        by offset. wwstage already keyed callsretsize (verified).
  - N_ASSIGN-ident  `t = f();`          same generalisation; global arm
        kept TY_STRUCT-only (a tuple-global has no sret-to-symbol path in
        either stage). wwstage grows a tuple-local arm (rettupleof gates
        it apart from the >24B-struct recv, which keeps its own path).
  - destructure     `let (a,b) = f();` and `a,b = f();` — the genuinely
        new wiring: the callee sret's into the @sretscr discard slot, then
        a copy-out loop moves each element to its binding at the SAME
        packed offset the SEND wrote (foff += element size), each at its
        natural width (#169); a `_` binding skips its store but advances
        foff. Both stages, byte-identical.
  - return-forward  `return f();`        cstage forward gate generalised
        to the predicate, reusing cg_sret_forward verbatim. wwstage
        already keyed sretretsize (verified).

The escape boundary stays loud: arg-pass `g(f())` fatals identically in
both stages (tuple arg exceeds return-cursor ABI capacity).

Test 799 is the runtime net Fold A deferred (byte-id is blind to a
SEND/RECEIVE layout mismatch): the bytes.cut-shaped ([]u8,[]u8) round-trip
over destructure / single-var-let / reassign / return-forward, each both
RUN under cstage and asserted cs==ww byte-identical. Tests 945 (row F)
and 956 (f64x3) flip from asserting the old over-cap loud-stop to
asserting the now-working sret round-trip. combined.ww amalgams (w6c +
wwdump embed the wcc cgen) regenerated. Unblocks #4 bytes.cut/rcut.
This commit is contained in:
2026-06-01 13:06:33 +09:00
parent 19e6b68d03
commit a937d67377
9 changed files with 885 additions and 45 deletions

View File

@@ -0,0 +1,231 @@
/*
* 799_tuple_sret_receive — project #10 Fold B (wide tuple-return / sret,
* RECEIVE side). Fold A (798) made the CALLEE emit an over-capacity tuple
* return (> 4 GP / > 2 SSE eightbytes) via sret, but every receive site
* stayed LOUD-STOPPED, so such a fn was not yet usefully callable. Fold B
* wires the call/receive end:
* - single-var-let `let t = f();` — t's slot IS the sret dest; the
* callee writes the whole tuple
* there, t.0/t.1 read by offset.
* - destructure `let (a, b) = f();` — the callee sret's into the
* @sretscr slot, then each element
* is copied out to its binding at
* the SAME packed offset the SEND
* wrote (foff += element size).
* - reassign `t = f();` — same as single-var, into t's slot.
* - return-forward `return f();` — outer @sretarg threads to inner.
*
* THIS IS THE RUNTIME NET Fold A deferred: byte-id alone is blind to a
* receive that lays the elements out at a different offset than the SEND
* (both stages would be wrong the same way). Each row both (a) RUNS the
* round-trip under cstage and asserts the data arrives intact, and (b)
* asserts w6c vs w6c_ww .s byte-identity (rule-10). The shape is the
* bytes.cut target — ([]u8, []u8) = 6 GP eightbytes, over the 4-GP cap.
*
* Data is read back by INDEXING the slice elements (t.0[i] / a[i]); the
* destructure row also asserts len() on its bindings. NOTE: len(t.N) on a
* tuple-element slice is a SEPARATE, pre-existing N_DOT-tuple+len
* composition bug (reads .ptr, not .len) orthogonal to the sret receive —
* it is deliberately NOT exercised here (filed for follow-up).
*
* GATE POLARITY: must stay GREEN. A wrong exit means the receive lays the
* tuple out inconsistently with the SEND (silent corruption); a byte-id
* FAIL means cstage and wwstage diverged on the receive (rule-10).
*/
#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[] = {
/* (a) destructure round-trip: bindings carry both halves; len() on a
* destructured binding works, and indexing reads the right bytes. */
{ "destructure",
"package main;\n"
"fn mk(a: []u8, b: []u8) ([]u8, []u8) = { return (a, b); };\n"
"export fn main() i32 = {\n"
" let buf: [8]u8 = [10u8, 11u8, 12u8, 13u8, 20u8, 21u8, 22u8, 23u8];\n"
" let x: []u8 = buf[0:4];\n"
" let y: []u8 = buf[4:8];\n"
" let (a, b) = mk(x, y);\n"
" if (len(a) != 4) { return 1; };\n"
" if (len(b) != 4) { return 2; };\n"
" if (a[0] != 10u8) { return 3; };\n"
" if (a[3] != 13u8) { return 4; };\n"
" if (b[0] != 20u8) { return 5; };\n"
" if (b[3] != 23u8) { return 6; };\n"
" return 0;\n"
"};\n", 0 },
/* (b) single-var-let + positional field read (t.0 / t.1 by offset,
* indexed). Locks that the whole tuple materialises in t's slot. */
{ "single_var_let",
"package main;\n"
"fn mk(a: []u8, b: []u8) ([]u8, []u8) = { return (a, b); };\n"
"export fn main() i32 = {\n"
" let buf: [8]u8 = [10u8, 11u8, 12u8, 13u8, 20u8, 21u8, 22u8, 23u8];\n"
" let x: []u8 = buf[0:4];\n"
" let y: []u8 = buf[4:8];\n"
" let t = mk(x, y);\n"
" if (t.0[0] != 10u8) { return 1; };\n"
" if (t.0[3] != 13u8) { return 2; };\n"
" if (t.1[0] != 20u8) { return 3; };\n"
" if (t.1[3] != 23u8) { return 4; };\n"
" return 0;\n"
"};\n", 0 },
/* (c) return-forward: outer fn forwards inner's over-cap tuple via the
* shared @sretarg (cg_sret_forward), no intermediate materialise. */
{ "return_forward",
"package main;\n"
"fn mk(a: []u8, b: []u8) ([]u8, []u8) = { return (a, b); };\n"
"fn fwd(a: []u8, b: []u8) ([]u8, []u8) = { return mk(a, b); };\n"
"export fn main() i32 = {\n"
" let buf: [8]u8 = [10u8, 11u8, 12u8, 13u8, 20u8, 21u8, 22u8, 23u8];\n"
" let x: []u8 = buf[0:4];\n"
" let y: []u8 = buf[4:8];\n"
" let (a, b) = fwd(x, y);\n"
" if (len(a) != 4) { return 1; };\n"
" if (a[0] != 10u8) { return 2; };\n"
" if (b[3] != 23u8) { return 3; };\n"
" return 0;\n"
"};\n", 0 },
/* (d) whole-tuple reassign `t = f();` into an existing slot. */
{ "reassign",
"package main;\n"
"fn mk(a: []u8, b: []u8) ([]u8, []u8) = { return (a, b); };\n"
"export fn main() i32 = {\n"
" let buf: [8]u8 = [10u8, 11u8, 12u8, 13u8, 20u8, 21u8, 22u8, 23u8];\n"
" let x: []u8 = buf[0:4];\n"
" let y: []u8 = buf[4:8];\n"
" let t = mk(x, y);\n"
" t = mk(y, x);\n"
" if (t.0[0] != 20u8) { return 1; };\n"
" if (t.1[0] != 10u8) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ 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, "tuple_sret_receive: 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/wwtsr_%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 in a scratch dir. */
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwtsr_%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. */
char cs_s[64], ws_s[64];
snprintf(cs_s, sizeof cs_s, "/tmp/wwtsr_%d_%d_cs.s", getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/wwtsr_%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-sret-receive tests failed\n", fail, n);
return 1;
}
printf("tuple_sret_receive: %d/%d ok (cstage run + cs==ww byte-id)\n",
n, n);
return 0;
}

View File

@@ -48,8 +48,11 @@
* G massign_blank_wide `_, a = f()`, f()->([]u8,i64). The blank `_`
* rides a 3-word slot so a lands on R8 (the i64), not DX
* (slice.len). len=2,cap=5,i64=7 distinct (any desync !=7).
* F slice_slice_builderr f()->([]u8,[]u8) returns (a,b) — 6 eightbytes,
* the build MUST FAIL (loud stop) on both stages.
* F slice_slice_recv f()->([]u8,[]u8) returns (a,b) — 6 eightbytes >
* cap; the SEND sret's it (#10 Fold A) and the
* destructure RECEIVE copies all 3 words/element out
* of @sretscr (#10 Fold B). len AND cap of both halves
* asserted (cap!=len) so a dropped word is caught.
*
* Fold discriminators: A/B (slice) — the old str-only XOR was slice-blind,
* so the slice fell to the scalar-pair fallback and dropped len/cap; B's
@@ -57,9 +60,11 @@
* receive width from the binding type, which is null for an unstamped `_`,
* so a wide `_` was mis-sized scalar and the cursor desynced (cstage read
* DX, wwstage R8); deriving width from the rhs tuple type fixes + aligns
* both stages. F (loud stop) — the old code register-returned ([]u8,[]u8)
* with only the two .ptr words (built + ran WRONG); the fix turns that into
* a BUILDERR. C/D/E are CONTROLS: the (i64,str) path was ALREADY 3-word
* both stages. F (over-cap recv) — the pre-#10 code register-returned
* ([]u8,[]u8) with only the two .ptr words (built + ran WRONG); #10 Fold A
* loud-stopped it at the SEND, and Fold B now sret's + copies it out into
* the bindings, so it is a working round-trip (was a BUILDERR at Fold A).
* C/D/E are CONTROLS: the (i64,str) path was ALREADY 3-word
* under the old XOR, and the single-str return is a separate untouched
* branch — they confirm no regression. All 14
* fixtures pass on both the cstage `ww` and wwstage `ww_ww` drivers with
@@ -201,22 +206,27 @@ static const struct row rows[] = {
" return 0;\n"
"};\n",
0, 0, NULL },
/* F — slice_slice_builderr: ([]u8,[]u8) = 6 eightbytes > 4 capacity.
* The send site MUST loud-stop (return-ABI #10); the build FAILS on
* both stages WITH the cited diagnostic (builderr=1: pass iff build
* returns nonzero AND stderr carries experr — rule 7, loud not silent). */
{ "slice_slice_builderr",
/* F — slice_slice_recv: ([]u8,[]u8) = 6 eightbytes > 4 capacity. The
* SEND sret's the over-cap tuple (#10 Fold A) and the destructure
* RECEIVE copies all 3 words per element out of the @sretscr slot
* (#10 Fold B) — formerly a loud-stop, now a working round-trip.
* Asserts BOTH len AND cap of each half (cap != len) so a dropped
* word — the original register-return bug — is caught at runtime. */
{ "slice_slice_recv",
"fn mk() ([]u8, []u8) = {\n"
" let a: []u8; a.len = 1; a.cap = 1;\n"
" let b: []u8; b.len = 2; b.cap = 2;\n"
" let a: []u8; a.len = 1; a.cap = 5;\n"
" let b: []u8; b.len = 2; b.cap = 6;\n"
" return (a, b);\n"
"};\n"
"export fn main() i32 = {\n"
" let (x, y) = mk();\n"
" if (x.len: i32 != 1) { return 1; };\n"
" if (x.len: i32 != 1) { return 1; };\n"
" if (x.cap: i32 != 5) { return 2; };\n"
" if (y.len: i32 != 2) { return 3; };\n"
" if (y.cap: i32 != 6) { return 4; };\n"
" return 0;\n"
"};\n",
0, 1, "register-return ABI capacity" },
0, 0, NULL },
};
static int

View File

@@ -11,8 +11,9 @@
* 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).
* SSE-cursor reg. SSE caps at 2 (X0,X1) — (f64,f64,f64) is over-cap, so
* it returns via sret (#10 Fold A SEND + Fold B destructure RECEIVE); the
* f64x3_recv row asserts that round-trip works in 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
@@ -355,13 +356,12 @@ static const struct row rows[] = {
"\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",
/* #10 OVER-CAP RECV — three f64 = 0 GP / 3 SSE exceeds the SSE return
* cap (X0,X1 only). #164 loud-stopped this at the SEND; #10 Fold A
* sret's it (callee stores X0/X1 → @sretarg at foff 0/8/16) and Fold B
* destructures it (each element MOVSD'd out of @sretscr into its
* binding), so it is now a working round-trip in BOTH stages. */
{ "f64x3_recv",
"package main;\n"
"fn tri(a: f64, b: f64, c: f64) (f64, f64, f64) = {\n"
"\treturn (a, b, c);\n"
@@ -372,7 +372,7 @@ static const struct row rows[] = {
"\tif (y != 2.0) { return 2; };\n"
"\tif (z != 3.0) { return 3; };\n"
"\treturn 0;\n"
"};\n", 0, 0, 1 },
"};\n", 0, 0, 0 },
/* 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. */