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.
232 lines
7.9 KiB
C
232 lines
7.9 KiB
C
/*
|
|
* 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;
|
|
}
|