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

@@ -4950,11 +4950,13 @@ cgexpr(Cg *c, Node *n, Local *locals)
}
break;
}
/* sret receive (#23): `s = f();` where s is a struct
* local >24B. s's slot IS the caller-prealloc dest;
* the callee writes through hidden RDI. Mirrors the
* cglet branch above. */
if (lu && lu->kind == TY_STRUCT && (int)lu->size > 24
/* sret receive (#23 / #10 Fold B): `s = f();` where s's
* own slot IS the caller-prealloc dest; the callee writes
* through hidden RDI. Mirrors the cglet branch above and
* keys on cg_sret_retsize (the shared sret SSoT), NOT a
* kind — so an over-cap tuple reassign materialises its
* whole slot exactly like a >24B struct. */
if (cg_sret_retsize(lt) > 0
&& n->rhs && n->rhs->kind == N_CALL
&& n->op == TK_ASSIGN) {
int off = localfind(locals, n->lhs->str);
@@ -4969,8 +4971,13 @@ cgexpr(Cg *c, Node *n, Local *locals)
* to g's symbol address. Mirrors the str/slice
* global arm above (let_islet + LEAQ masym). The
* scalar fall-through below would emit a truncated
* 8-byte `MOVQ AX, g(SB)` and drop the struct body. */
if (let_islet(n->lhs->str)) {
* 8-byte `MOVQ AX, g(SB)` and drop the struct body.
* Kept TY_STRUCT-only: a tuple-typed global reassign
* has no sret-to-symbol path in wwstage either, so
* leaving it to fall through keeps the stages aligned
* (rule-10). */
if (lu && lu->kind == TY_STRUCT
&& let_islet(n->lhs->str)) {
cg_sret_dest_sym = n->lhs->str;
cgexpr(c, n->rhs, locals);
cg_sret_dest_sym = NULL;
@@ -8012,13 +8019,17 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
cg_structlit_fill_bp(c, locals, lu, n->rhs, off);
break;
}
/* sret receive (#23): plain TY_STRUCT >24B. The let's own
* slot IS the caller-prealloc dest; the call writes
* through hidden RDI directly into our slot, no AX/DX/CX
* shuffle. Set cg_sret_dest_off so the nested cgexpr →
* N_CALL path emits `LEAQ off(BP), RDI` before CALL. */
if (n->rhs && n->rhs->kind == N_CALL && lu
&& lu->kind == TY_STRUCT && sz > 24) {
/* sret receive (#23 / #10 Fold B): the let's own slot IS the
* caller-prealloc dest; the call writes through hidden RDI
* directly into our slot, no AX/DX/CX shuffle. Set
* cg_sret_dest_off so the nested cgexpr → N_CALL path emits
* `LEAQ off(BP), RDI` before CALL. Keys on cg_sret_retsize
* (the shared sret SSoT), NOT a kind — so an over-cap tuple
* return (Fold A made the callee sret it) materialises its
* WHOLE slot here exactly like a >24B struct, and t.0/t.1
* read by offset afterward. */
if (n->rhs && n->rhs->kind == N_CALL
&& cg_sret_retsize(lt) > 0) {
cg_sret_dest_off = off;
cgexpr(c, n->rhs, *locals);
cg_sret_dest_off = 0;
@@ -8471,9 +8482,11 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
* corrupting the caller's receive slot even though
* the prologue wired @sretarg. */
Type *rt = type_chase_named(cg_ret_type);
/* sret return-forwarding (task #9 follow-up to #23):
* `return f();` where outer + inner both return the
* same >24B struct shape. Outer's @sretarg already
/* sret return-forwarding (task #9 follow-up to #23,
* generalised for #10 Fold B): `return f();` where outer
* + inner both return the same sret shape (>24B struct OR
* over-cap tuple — gate keys cg_sret_retsize, not a kind).
* Outer's @sretarg already
* holds its caller's prealloc dest; pass it to inner
* in RDI (set by cgcall via cg_sret_forward), inner
* writes directly there, inner's RAX (dest pointer)
@@ -8481,8 +8494,7 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
* MOVQ @sretarg(BP), AX is redundant after inner's
* RET but kept for byte-id symmetry with the
* N_IDENT / N_STRUCTLIT arms below. */
if (rt && rt->kind == TY_STRUCT
&& (int)rt->size > 24
if (cg_sret_retsize(rt) > 0
&& n->lhs->kind == N_CALL) {
cg_sret_forward = 1;
cgexpr(c, n->lhs, *locals);
@@ -9001,10 +9013,56 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
* float from X0/X1 (SSE cursor), a scalar's 1 word from the
* INTEGER cursor into an 8B slot. Both rows loud-stop at their
* cap. */
/* #10 Fold B: over-cap tuple destructure RECEIVE. The callee
* sret'd the whole tuple into the @sretscr discard slot (cgcall
* sees cg_sret_retsize > 0, no lvalue dest wired). Copy each
* element out to its binding slot at the SAME packed offset the
* SEND wrote (foff += element size — the t.0/t.1 layout), each
* at its NATURAL width (#169). The receive has no single lvalue
* dest, so it reuses the same per-fn @sretscr slot a discarded
* sret call would; the in-reg path below is unchanged. */
int sret_recv = (n->rhs && n->rhs->kind == N_CALL)
? cg_sret_retsize(n->rhs->type) : 0;
cgexpr(c, n->rhs, *locals);
int lf32;
if (sret_recv > 0) {
int scr = cg_sretscr_off;
int foff = 0;
for (Node *l = n->list; l; l = l->next) {
Type *t = l->type;
Type *u = type_chase_named(t);
int wide = u && (u->kind == TY_SLICE
|| u->kind == TY_STR);
int isflt = fld_isfloat(t, &lf32);
int esz = t ? (int)t->size : 8;
int bsz = wide ? esz : 8;
int off = localoff(c, locals, l->str, bsz, frame);
if (isflt) {
ins2(c, lf32 ? A_MOVSS : A_MOVSD,
amem(D_BP, scr + foff), areg(D_X0));
ins2(c, lf32 ? A_MOVSS : A_MOVSD,
areg(D_X0), amem(D_BP, off));
} else if (wide) {
for (int k = 0; k < esz; k += 8) {
ins2(c, A_MOVQ,
amem(D_BP, scr + foff + k),
areg(D_AX));
ins2(c, A_MOVQ, areg(D_AX),
amem(D_BP, off + k));
}
} else {
ins2(c, fldloadop(t, esz),
amem(D_BP, scr + foff), areg(D_AX));
ins2(c, fldstoreop(t, esz),
areg(D_AX), amem(D_BP, off));
}
foff += esz;
}
break;
}
int gpcap = TUPLE_GPCAP;
int ssecap = TUPLE_SSECAP;
int gptotal = 0, ssetotal = 0, lf32;
int gptotal = 0, ssetotal = 0;
for (Node *l = n->list; l; l = l->next) {
Type *t = l->type;
Type *u = (t && t->kind == TY_NAMED) ? t->under : t;
@@ -9056,13 +9114,63 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
* retained ww-EXTENSION beyond Hare (Hare tuple-unpack is binding-
* only); ww keeps the Go/rob-pike multi-assign idiom — rule-9
* carve-out. Over-capacity is a loud stop, not a silent drop. */
/* #10 Fold B: over-cap tuple destructure REASSIGN. Same sret
* copy-out as N_MLET but the slots already exist (localfind);
* a `_` / missing binding (off == 0) SKIPS its store yet still
* ADVANCES foff so the next element stays aligned (harec `_`).
* Element widths come from the rhs tuple's element types — the
* SAME producer source the SEND walks. */
int sret_recv = (n->rhs && n->rhs->kind == N_CALL)
? cg_sret_retsize(n->rhs->type) : 0;
cgexpr(c, n->rhs, *locals);
Type *rt = n->rhs ? n->rhs->type : NULL;
Type *ru = (rt && rt->kind == TY_NAMED) ? rt->under : rt;
Tparam *tp0 = (ru && ru->kind == TY_TUPLE) ? ru->params : NULL;
int mf32;
if (sret_recv > 0) {
int scr = cg_sretscr_off;
int foff = 0;
Tparam *tp = tp0;
for (Node *l = n->list; l; l = l->next) {
Type *et = tp ? tp->type : NULL;
Type *eu = type_chase_named(et);
int wide = eu && (eu->kind == TY_SLICE
|| eu->kind == TY_STR);
int isflt = fld_isfloat(et, &mf32);
int esz = et ? (int)et->size : 8;
int off = (l->kind == N_IDENT)
? localfind(*locals, l->str) : 0;
if (off != 0) {
if (isflt) {
ins2(c, mf32 ? A_MOVSS : A_MOVSD,
amem(D_BP, scr + foff),
areg(D_X0));
ins2(c, mf32 ? A_MOVSS : A_MOVSD,
areg(D_X0), amem(D_BP, off));
} else if (wide) {
for (int k = 0; k < esz; k += 8) {
ins2(c, A_MOVQ,
amem(D_BP, scr + foff + k),
areg(D_AX));
ins2(c, A_MOVQ, areg(D_AX),
amem(D_BP, off + k));
}
} else {
ins2(c, fldloadop(et, esz),
amem(D_BP, scr + foff),
areg(D_AX));
ins2(c, fldstoreop(et, esz),
areg(D_AX), amem(D_BP, off));
}
}
foff += esz;
if (tp) tp = tp->next;
}
break;
}
int gpcap = TUPLE_GPCAP;
int ssecap = TUPLE_SSECAP;
int gptotal = 0, ssetotal = 0, mf32;
int gptotal = 0, ssetotal = 0;
for (Tparam *tp = tp0; tp; tp = tp->next) {
Type *u = (tp->type && tp->type->kind == TY_NAMED)
? tp->type->under : tp->type;