cstage+selfhost+test: System V AMD64 sret discipline for >24B struct return (#23)

Class B shared miscompile pre-fix: cstage skipped the CALL emit at the
receive site (frame collapsed, exit 11); wwstage emitted CALL but
truncated 32B return to AX only (slice payload garbage, segfault on
g.b[0]). Both stages now lower plain TY_STRUCT > 24B through the SysV
sret discipline: caller pre-allocates dest, passes &dest in RDI as a
hidden first-arg (user args shift to SI/DX/CX/R8/R9/+stack), callee
saves RDI to @sretarg at the prologue and writes through it, returns
RDI in RAX. Surfaced by lib/encoding/utf8 pre-flight when the
Hoehrmann decoder (32B) hit 698_cgreturn_struct.c's OUT-OF-SCOPE
marker.

Scope: plain TY_STRUCT > 24B only — tagged unions, tuples, str, slice
keep their existing register-return ABIs. `return f()` forwarding
from a sret callee is fail-loud-not-wired (compile-time error in
both stages, follow-up filed); the workaround `let r = f(); return
r;` is wired and byte-identical. Discard-context calls (`f();` of an
sret-returning function) share a per-fn single-slot @sretscr;
consecutive discards reuse the same slot.

698_cgreturn_struct.c's OUT-OF-SCOPE marker retired in the same
commit; three positive rows (32B quad, 32B decoder, 40B five) now
assert the sret discipline across both stages via byte-id diff.

Tests:
  - 721_sret_struct_return pins three asm-presence sentinels per
    row: (a) LEAQ -K(BP), DI immediately before CALL at the receive
    site, (b) MOVQ -K(BP), AX before RET in the callee (sret return-
    the-pointer), (c) negative-assert no MOVQ AX, -K(BP) capture for
    return type >8B. Three rows × both stages × cmp -s byte-id.
  - 925_sret_struct_return_run runtime-pins 7 rows × 2 stages
    including the collision row (25B+ struct BOTH returned AND passed
    by-value as arg — catches arg-shift, sister site to #11), nested
    struct payload, slice payload, reassign-receive, N_IDENT return
    rhs.

89/89 ok. 995_self_rebuild stays green (ww2==ww3==ww4 byte-id).
This commit is contained in:
2026-05-17 23:17:03 +09:00
parent 6ab865d933
commit 7e0c280691
12 changed files with 1723 additions and 42 deletions

View File

@@ -7738,6 +7738,49 @@ fn structnaturalsize(si: *structinfo) i32 = {
return n;
};
// sretretsize — if `t` ultimately denotes a plain TY_STRUCT > 24B,
// return its natural size; else 0. Tagged unions, tuples, str,
// slices, scalars route through their existing register-return ABIs
// (AX/DX/CX/[R8]) regardless of size. Task #23 mirrors cstage's
// cg_sret_retsize predicate. Resolves N_TNAME → struct via structlookup
// and unwraps one leading N_TBANG so `type box = !big;` still
// triggers sret on the underlying big.
export fn sretretsize(c: *cgen, t: *node) i32 = {
if (t == nil) { return 0; };
let r: *node = t;
if (r.kind == nkind.N_TBANG) {
r = r.lhs;
if (r == nil) { return 0; };
};
if (r.kind != nkind.N_TNAME) { return 0; };
// Primitives / aliased-to-primitives are never sret.
if (primsize(r.str) > 0) { return 0; };
if (streq(r.str, "str")) { return 0; };
let si: *structinfo = structlookup(c, r.str);
if (si == nil) { return 0; };
let n: i32 = structnaturalsize(si);
if (n <= 24) { return 0; };
return n;
};
// callsretsize — if N_CALL `n`'s callee returns a plain TY_STRUCT
// > 24B, return its natural size; else 0. Wraps sretretsize over the
// callee's resolved return type, used by cglet / cgassign receive
// sites and cgcall to detect sret at the receive / emit boundaries.
export fn callsretsize(c: *cgen, n: *node) i32 = {
if (n == nil) { return 0; };
if (n.kind != nkind.N_CALL) { return 0; };
let callee: *node = n.lhs;
if (callee == nil) { return 0; };
let cn: str;
cn.ptr = nil; cn.len = 0;
if (callee.kind == nkind.N_IDENT) { cn = callee.str; };
if (callee.kind == nkind.N_DOT) { cn = callee.str; };
if (cn.len == 0) { return 0; };
let rt: *node = fnretlookup(c, cn);
return sretretsize(c, rt);
};
fn structlookup(c: *cgen, name: str) *structinfo = {
// Exact match first: bare-from-source struct names and already-
// leafed lookups hit here directly.
@@ -12942,6 +12985,24 @@ fn cgcall(c: *cgen, n: *node) void = {
};
};
let nargs: i32 = pushargsrev(c, n.list, calleeparams);
// sret call (#23): callee returns plain TY_STRUCT > 24B. The
// dest pointer lands in RDI; start intidx at 1 to skip RDI in
// the user-arg pop loop and emit `LEAQ off(BP), DI` AFTER all
// pops have finished (so they don't clobber RDI). The dest off
// is either the receive site's slot (c.sretdestoff, propagated
// from cglet / cgassign ident) or the per-fn @sretscr discard
// slot reserved by scanlocals.
let sretcs: i32 = callsretsize(c, n);
let sretcalloff: i32 = 0;
if (sretcs > 0) {
if (c.sretdestoff != 0) {
sretcalloff = c.sretdestoff;
c.sretdestoff = 0;
} else {
sretcalloff = localadd(c, "@sretscr",
c.sretscrsz, nil);
};
};
// Pop forward. Float args were pushed as 8 bytes from X0 via
// SUBQ+MOVSD; pop into the XMM stream (X0..X7). Everything else
// pops into the int stream (DI..R9) per the SysV ABI. Walk the
@@ -12950,6 +13011,7 @@ fn cgcall(c: *cgen, n: *node) void = {
// the remaining slots stay on the stack and the callee reads them
// via 16+8*k(BP). Caller-cleanup is emitted after the CALL.
let intidx: i32 = 0;
if (sretcs > 0) { intidx = 1; };
let fpidx: i32 = 0;
let a: *node = n.list;
let popped: i32 = 0;
@@ -13080,6 +13142,14 @@ fn cgcall(c: *cgen, n: *node) void = {
};
};
};
// sret hidden first-arg (#23): load &dest into RDI AFTER all
// user-arg pops have finished — intidx started at 1 so RDI was
// never written. The CALL emit follows immediately.
if (sretcs > 0) {
emitline("\tLEAQ\t");
emitoff(sretcalloff: i64);
emitline("(BP), DI\n");
};
if (isfnptrcall) {
// Load fn-ptr field value into AX; CALL AX. We emit the
// load AFTER the args have been popped (so AX/BX/etc
@@ -15255,6 +15325,21 @@ fn cgassign(c: *cgen, n: *node) void = {
};
if (n.rhs != nil
&& n.rhs.kind == nkind.N_CALL) {
// sret receive (#23): plain
// TY_STRUCT > 24B from a CALL.
// `s` is the prealloc dest; the
// callee writes through hidden RDI
// directly into off(BP). Mirror of
// cglet's sret branch.
if (lcnsz > 24) {
let rscs: i32 = callsretsize(c, n.rhs);
if (rscs > 0) {
c.sretdestoff = off;
cgexpr(c, n.rhs);
c.sretdestoff = 0;
return;
};
};
let lcsz: i32 = lcnsz;
if (lcsz <= 24) {
let tlm: i32 = lcsz - (lcsz / 8) * 8;
@@ -15744,15 +15829,106 @@ fn cgreturn(c: *cgen, n: *node) void = {
c.lastwasreturn = 1;
return;
};
// sret return (#23): plain TY_STRUCT > 24B. Callee writes
// through *(@sretarg) (the caller-prealloc dest saved at
// the prologue), then loads @sretarg into RAX and rets —
// the SysV "return the pointer" discipline. Two rhs shapes
// are wired: N_IDENT (word-copy from rhs slot to *(dest))
// and N_STRUCTLIT (cgstructlitfill with mode=1 PTR_LOCAL).
if (c.sretargoff != 0) {
let scs: i32 = sretretsize(c, c.fnret);
if (scs > 0) {
// `return f();` from a sret callee would silent-
// miscompile: cgexpr writes inner's result to
// @sretscr but outer never copies into *@sretarg
// and never sets RAX. Fail loud per CLAUDE.md
// rule 7; the `let r = f(); return r;` workaround
// is already wired and byte-id with cstage.
if (rhs.kind == nkind.N_CALL) {
let m: str = "ww: cgreturn: sret return-forwarding for >24B struct not wired (task #23)\n";
os.write(2, m.ptr, m.len: u64);
os.exit(1);
};
let okrhs: bool = false;
if (rhs.kind == nkind.N_IDENT) { okrhs = true; };
if (rhs.kind == nkind.N_STRUCTLIT) { okrhs = true; };
if (okrhs) {
if (rhs.kind == nkind.N_STRUCTLIT) {
let trefn: *node = rhs.lhs;
let sname: str;
sname.ptr = nil; sname.len = 0;
if (trefn != nil) {
if (trefn.kind == nkind.N_IDENT) { sname = trefn.str; }
else { if (trefn.kind == nkind.N_TNAME) { sname = trefn.str; }; };
};
let sret_si: *structinfo = structlookup(c, sname);
if (sret_si != nil) {
let emptys: str;
emptys.ptr = nil; emptys.len = 0;
// mode=1 (PTR_LOCAL): base reg = BX,
// reloaded from @sretarg(BP) before
// each field store. disp = 0 because
// the dest pointer IS the struct base.
cgstructlitfill(c, sret_si, rhs,
1, c.sretargoff, emptys,
0, scs);
};
} else {
let rl: *local = localfindnode(c, rhs.str);
if (rl != nil) {
emitline("\tMOVQ\t");
emitoff(c.sretargoff: i64);
emitline("(BP), BX\n");
let k: i32 = 0;
for (k + 8 <= scs) {
emitline("\tMOVQ\t");
emitoff((rl.off + k): i64);
emitline("(BP), AX\n");
emitline("\tMOVQ\tAX, ");
emitoff(k: i64);
emitline("(BX)\n");
k += 8;
};
for (k + 4 <= scs) {
emitline("\tMOVL\t");
emitoff((rl.off + k): i64);
emitline("(BP), AX\n");
emitline("\tMOVL\tAX, ");
emitoff(k: i64);
emitline("(BX)\n");
k += 4;
};
for (k < scs) {
emitline("\tMOVB\t");
emitoff((rl.off + k): i64);
emitline("(BP), AX\n");
emitline("\tMOVB\tAX, ");
emitoff(k: i64);
emitline("(BX)\n");
k += 1;
};
};
};
// sret return: RAX = dest pointer.
emitline("\tMOVQ\t");
emitoff(c.sretargoff: i64);
emitline("(BP), AX\n");
emitline("\tMOVQ\tBP, SP\n");
emitline("\tPOPQ\tBP\n");
emitline("\tRET\n");
c.lastwasreturn = 1;
return;
};
};
};
// Whole-struct return for sizes <= 24B. ABI: AX=bytes[0..7],
// DX=bytes[8..15], CX=bytes[16..23]. Mirrors cstage cgen.c
// N_RETURN TY_STRUCT branch. Two rhs shapes are wired:
// N_IDENT (word-copy from rhs local slot) and N_STRUCTLIT
// (field-by-field store at scratch+foff, with tagged fields
// delegated to cgwidentaggedstore). Call-result chain return
// is deferred to #5's receive side. Sizes > 24B fall through
// to the scalar path below (only AX gets the first qword),
// pending sret.
// is deferred to #5's receive side. Sizes > 24B route through
// the sret arm above.
let rname: str;
rname.ptr = nil; rname.len = 0;
if (c.fnret != nil) {
@@ -16076,6 +16252,22 @@ fn cglet(c: *cgen, n: *node) void = {
return;
};
};
// sret receive (#23): plain TY_STRUCT > 24B from a call.
// The let's own slot IS the caller-prealloc dest; the
// nested cgexpr → cgcall path emits `LEAQ off(BP), DI`
// before the CALL and the callee writes through it. No
// AX/DX/CX shuffle; AX returns the dest pointer per SysV
// sret discipline (irrelevant here).
if (rhs.kind == nkind.N_CALL) {
let scs: i32 = callsretsize(c, rhs);
if (scs > 0) {
c.sretdestoff = off;
cgexpr(c, rhs);
c.sretdestoff = 0;
c.lastwasreturn = 0;
return;
};
};
// Whole-struct receive for sizes <=24B (call-result rhs).
// Counterpart of #4's cgreturn ABI: cgexpr leaves
// AX=bytes[0..7], DX=bytes[8..15], CX=bytes[16..23],
@@ -16804,6 +16996,20 @@ fn tagscrbump(c: *cgen, need: i32) i32 = {
return delta;
};
// sretscrbump — sister of tagscrbump for the sret discard slot
// (#23). Tracks max sret return type used as a CALL discard / nested
// receiver. Returns frame-byte delta vs the previous high water mark
// (rounded up to 8B).
fn sretscrbump(c: *cgen, need: i32) i32 = {
let n: i32 = need;
if (n < 8) { n = 8; };
if ((n & 7) != 0) { n = (n + 7) & ~7; };
if (n <= c.sretscrsz) { return 0; };
let delta: i32 = n - c.sretscrsz;
c.sretscrsz = n;
return delta;
};
//
// Recursively walks the body to count every local `let`. Each gets a
// slot sized by slotsize(typ); 8-byte default. Match-bindings + for-
@@ -17140,6 +17346,17 @@ fn scanlocals(c: *cgen, n: *node) i32 = {
};
};
};
// sret CALL (#23): callee returns plain TY_STRUCT > 24B. The
// receive site (cglet / cgassign ident) overrides at emit time
// with the dest local's own slot; discards / nested calls fall
// back to @sretscr. Single-slot per fn sized to the max sret
// return — sretscrbump tracks the high-water mark so a later
// larger call grows the frame without re-counting the prior
// reservation. Mirrors @tagscr's cumulative tagscrbump.
if (n.kind == nkind.N_CALL) {
let scs: i32 = callsretsize(c, n);
if (scs > 0) { total += sretscrbump(c, scs); };
};
// Call-site struct-payload widening uses @tagscr — when the
// arg is a struct literal/ident and the callee's param is
// tagged, pushargsrev materialises in scratch and pushes.
@@ -17254,7 +17471,11 @@ fn scanlocals(c: *cgen, n: *node) i32 = {
fn cgfnparams(c: *cgen, params: *node) void = {
let p: *node = params;
// sret (#23): RDI is consumed by the hidden dest pointer
// (already spilled to @sretarg by cgfn); the first user param
// lands in SI.
let idx: i32 = 0;
if (c.sretargoff != 0) { idx = 1; };
let fidx: i32 = 0;
// Cursor for args that overflow the SysV reg windows. Each
// stack-passed arg lives at 16+8*k(BP) — no spill, the local
@@ -17564,6 +17785,14 @@ fn cgfn(c: *cgen, fn_: *node) void = {
c.curmod = fn_.module;
c.fnret = fn_.lhs;
// sret callee (#23): return type is plain TY_STRUCT > 24B.
// Reserve 8B for @sretarg (holds the saved hidden RDI dest
// pointer); cgfnparams skips DI for user args, cgreturn writes
// through *(@sretarg) and returns @sretarg in RAX. Decision
// made here so the frame pre-scan and cgfnparams see the same
// view of the int-arg cursor.
let sret_callee: bool = sretretsize(c, c.fnret) > 0;
// Emit the TEXT label via emitfnname so the def site picks up the
// same skip rule (FFI / `main` / empty-module) and the same module
// hint (this fn's own module) that the call sites use. Drops the
@@ -17586,6 +17815,12 @@ fn cgfn(c: *cgen, fn_: *node) void = {
let frame: i32 = 0;
let argi: i32 = 0;
let fargi: i32 = 0;
// Reserve @sretarg (8B) BEFORE the param-induced frame, and
// start argi at 1 so the param walker sees RDI as consumed.
if (sret_callee) {
frame += 8;
argi = 1;
};
for (scanp != nil) {
if (scanp.kind == nkind.N_PARAM) {
let isvar: bool = scanp.op == tkind.TK_ELLIPSIS;
@@ -17678,6 +17913,13 @@ fn cgfn(c: *cgen, fn_: *node) void = {
emitint(frame: i64);
emitline(", SP\n");
if (sret_callee) {
let saoff: i32 = localadd(c, "@sretarg", 8, nil);
emitline("\tMOVQ\tDI, ");
emitoff(saoff: i64);
emitline("(BP)\n");
};
cgfnparams(c, fn_.list);
c.lastwasreturn = 0;
// Iterate the fn body's statements directly rather than dispatching
@@ -18179,6 +18421,29 @@ type cgen = struct {
// later emit reuses. Mirrors c.tagscrsz pattern (#38) but tracks
// offset, not size (per-fn return type is fixed, so size is too).
retscroff: i32,
// System V AMD64 sret discipline (#23). Plain TY_STRUCT returns
// with size > 24B are passed via a hidden first-arg pointer
// (RDI) to a caller-prealloc dest; the callee writes through
// that pointer and returns it in RAX.
//
// sretargoff — callee-side @sretarg slot (8B, holds saved RDI).
// Set in cgfn prologue when the fn's return type
// triggers sret. 0 means N/A.
// sretdestoff — caller-side dest BP offset, propagated from a
// receive site (cglet / cgassign ident) to the
// nested cgexpr → cgcall so the call emits
// `LEAQ off(BP), DI` instead of allocating a
// scratch. 0 means no receiver wired.
// sretscroff — per-fn @sretscr discard slot, used by sret CALLs
// whose result has no named receiver. Single-slot
// SSoT mirroring c.retscroff; the scanlocals walk
// sums c.sretscrsz to pre-reserve.
// sretscrsz — max sret discard size in this fn (sums during
// scanlocals, consumed by localadd("@sretscr", ...)).
sretargoff: i32,
sretdestoff: i32,
sretscroff: i32,
sretscrsz: i32,
};
// Top-level mutable `let` registry. Mirrors cmd/w6c/cgen.c LetVar.
@@ -18202,6 +18467,10 @@ fn cgeninit(c: *cgen, a: *arena) void = {
c.varargseq = 0;
c.tagscrsz = 0;
c.retscroff = 0;
c.sretargoff = 0;
c.sretdestoff = 0;
c.sretscroff = 0;
c.sretscrsz = 0;
// Note: strlit_seq, strlits, ffis are *not* reset here; they
// persist across cgfn calls within one file. cgfile resets them
// at the start of each compilation unit.
@@ -18285,6 +18554,23 @@ fn localadd(c: *cgen, name: str, sz: i32, tnode: *node) i32 = {
c.retscroff = off;
return off;
};
// @sretarg / @sretscr (#23): same single-slot SSoT
// pattern as @retscr. @sretarg holds the saved hidden
// RDI for sret callees (8B, set once per fn at the
// prologue); @sretscr is the caller-side discard slot
// for sret CALLs whose result is dropped.
if (streq(name, "@sretarg")) {
if (c.sretargoff != 0) { return c.sretargoff; };
let off: i32 = localalloc(c, name, sz, tnode);
c.sretargoff = off;
return off;
};
if (streq(name, "@sretscr")) {
if (c.sretscroff != 0) { return c.sretscroff; };
let off: i32 = localalloc(c, name, sz, tnode);
c.sretscroff = off;
return off;
};
let cur: *local = c.locals;
for (cur != nil) {
let cn: str = cur.name;

View File

@@ -431,6 +431,29 @@ type cgen = struct {
// later emit reuses. Mirrors c.tagscrsz pattern (#38) but tracks
// offset, not size (per-fn return type is fixed, so size is too).
retscroff: i32,
// System V AMD64 sret discipline (#23). Plain TY_STRUCT returns
// with size > 24B are passed via a hidden first-arg pointer
// (RDI) to a caller-prealloc dest; the callee writes through
// that pointer and returns it in RAX.
//
// sretargoff — callee-side @sretarg slot (8B, holds saved RDI).
// Set in cgfn prologue when the fn's return type
// triggers sret. 0 means N/A.
// sretdestoff — caller-side dest BP offset, propagated from a
// receive site (cglet / cgassign ident) to the
// nested cgexpr → cgcall so the call emits
// `LEAQ off(BP), DI` instead of allocating a
// scratch. 0 means no receiver wired.
// sretscroff — per-fn @sretscr discard slot, used by sret CALLs
// whose result has no named receiver. Single-slot
// SSoT mirroring c.retscroff; the scanlocals walk
// sums c.sretscrsz to pre-reserve.
// sretscrsz — max sret discard size in this fn (sums during
// scanlocals, consumed by localadd("@sretscr", ...)).
sretargoff: i32,
sretdestoff: i32,
sretscroff: i32,
sretscrsz: i32,
};
// Top-level mutable `let` registry. Mirrors cmd/w6c/cgen.c LetVar.
@@ -454,6 +477,10 @@ fn cgeninit(c: *cgen, a: *arena) void = {
c.varargseq = 0;
c.tagscrsz = 0;
c.retscroff = 0;
c.sretargoff = 0;
c.sretdestoff = 0;
c.sretscroff = 0;
c.sretscrsz = 0;
// Note: strlit_seq, strlits, ffis are *not* reset here; they
// persist across cgfn calls within one file. cgfile resets them
// at the start of each compilation unit.
@@ -537,6 +564,23 @@ fn localadd(c: *cgen, name: str, sz: i32, tnode: *node) i32 = {
c.retscroff = off;
return off;
};
// @sretarg / @sretscr (#23): same single-slot SSoT
// pattern as @retscr. @sretarg holds the saved hidden
// RDI for sret callees (8B, set once per fn at the
// prologue); @sretscr is the caller-side discard slot
// for sret CALLs whose result is dropped.
if (streq(name, "@sretarg")) {
if (c.sretargoff != 0) { return c.sretargoff; };
let off: i32 = localalloc(c, name, sz, tnode);
c.sretargoff = off;
return off;
};
if (streq(name, "@sretscr")) {
if (c.sretscroff != 0) { return c.sretscroff; };
let off: i32 = localalloc(c, name, sz, tnode);
c.sretscroff = off;
return off;
};
let cur: *local = c.locals;
for (cur != nil) {
let cn: str = cur.name;

View File

@@ -39,6 +39,20 @@ fn tagscrbump(c: *cgen, need: i32) i32 = {
return delta;
};
// sretscrbump — sister of tagscrbump for the sret discard slot
// (#23). Tracks max sret return type used as a CALL discard / nested
// receiver. Returns frame-byte delta vs the previous high water mark
// (rounded up to 8B).
fn sretscrbump(c: *cgen, need: i32) i32 = {
let n: i32 = need;
if (n < 8) { n = 8; };
if ((n & 7) != 0) { n = (n + 7) & ~7; };
if (n <= c.sretscrsz) { return 0; };
let delta: i32 = n - c.sretscrsz;
c.sretscrsz = n;
return delta;
};
//
// Recursively walks the body to count every local `let`. Each gets a
// slot sized by slotsize(typ); 8-byte default. Match-bindings + for-
@@ -375,6 +389,17 @@ fn scanlocals(c: *cgen, n: *node) i32 = {
};
};
};
// sret CALL (#23): callee returns plain TY_STRUCT > 24B. The
// receive site (cglet / cgassign ident) overrides at emit time
// with the dest local's own slot; discards / nested calls fall
// back to @sretscr. Single-slot per fn sized to the max sret
// return — sretscrbump tracks the high-water mark so a later
// larger call grows the frame without re-counting the prior
// reservation. Mirrors @tagscr's cumulative tagscrbump.
if (n.kind == nkind.N_CALL) {
let scs: i32 = callsretsize(c, n);
if (scs > 0) { total += sretscrbump(c, scs); };
};
// Call-site struct-payload widening uses @tagscr — when the
// arg is a struct literal/ident and the callee's param is
// tagged, pushargsrev materialises in scratch and pushes.
@@ -489,7 +514,11 @@ fn scanlocals(c: *cgen, n: *node) i32 = {
fn cgfnparams(c: *cgen, params: *node) void = {
let p: *node = params;
// sret (#23): RDI is consumed by the hidden dest pointer
// (already spilled to @sretarg by cgfn); the first user param
// lands in SI.
let idx: i32 = 0;
if (c.sretargoff != 0) { idx = 1; };
let fidx: i32 = 0;
// Cursor for args that overflow the SysV reg windows. Each
// stack-passed arg lives at 16+8*k(BP) — no spill, the local
@@ -799,6 +828,14 @@ fn cgfn(c: *cgen, fn_: *node) void = {
c.curmod = fn_.module;
c.fnret = fn_.lhs;
// sret callee (#23): return type is plain TY_STRUCT > 24B.
// Reserve 8B for @sretarg (holds the saved hidden RDI dest
// pointer); cgfnparams skips DI for user args, cgreturn writes
// through *(@sretarg) and returns @sretarg in RAX. Decision
// made here so the frame pre-scan and cgfnparams see the same
// view of the int-arg cursor.
let sret_callee: bool = sretretsize(c, c.fnret) > 0;
// Emit the TEXT label via emitfnname so the def site picks up the
// same skip rule (FFI / `main` / empty-module) and the same module
// hint (this fn's own module) that the call sites use. Drops the
@@ -821,6 +858,12 @@ fn cgfn(c: *cgen, fn_: *node) void = {
let frame: i32 = 0;
let argi: i32 = 0;
let fargi: i32 = 0;
// Reserve @sretarg (8B) BEFORE the param-induced frame, and
// start argi at 1 so the param walker sees RDI as consumed.
if (sret_callee) {
frame += 8;
argi = 1;
};
for (scanp != nil) {
if (scanp.kind == nkind.N_PARAM) {
let isvar: bool = scanp.op == tkind.TK_ELLIPSIS;
@@ -913,6 +956,13 @@ fn cgfn(c: *cgen, fn_: *node) void = {
emitint(frame: i64);
emitline(", SP\n");
if (sret_callee) {
let saoff: i32 = localadd(c, "@sretarg", 8, nil);
emitline("\tMOVQ\tDI, ");
emitoff(saoff: i64);
emitline("(BP)\n");
};
cgfnparams(c, fn_.list);
c.lastwasreturn = 0;
// Iterate the fn body's statements directly rather than dispatching

View File

@@ -2991,6 +2991,24 @@ fn cgcall(c: *cgen, n: *node) void = {
};
};
let nargs: i32 = pushargsrev(c, n.list, calleeparams);
// sret call (#23): callee returns plain TY_STRUCT > 24B. The
// dest pointer lands in RDI; start intidx at 1 to skip RDI in
// the user-arg pop loop and emit `LEAQ off(BP), DI` AFTER all
// pops have finished (so they don't clobber RDI). The dest off
// is either the receive site's slot (c.sretdestoff, propagated
// from cglet / cgassign ident) or the per-fn @sretscr discard
// slot reserved by scanlocals.
let sretcs: i32 = callsretsize(c, n);
let sretcalloff: i32 = 0;
if (sretcs > 0) {
if (c.sretdestoff != 0) {
sretcalloff = c.sretdestoff;
c.sretdestoff = 0;
} else {
sretcalloff = localadd(c, "@sretscr",
c.sretscrsz, nil);
};
};
// Pop forward. Float args were pushed as 8 bytes from X0 via
// SUBQ+MOVSD; pop into the XMM stream (X0..X7). Everything else
// pops into the int stream (DI..R9) per the SysV ABI. Walk the
@@ -2999,6 +3017,7 @@ fn cgcall(c: *cgen, n: *node) void = {
// the remaining slots stay on the stack and the callee reads them
// via 16+8*k(BP). Caller-cleanup is emitted after the CALL.
let intidx: i32 = 0;
if (sretcs > 0) { intidx = 1; };
let fpidx: i32 = 0;
let a: *node = n.list;
let popped: i32 = 0;
@@ -3129,6 +3148,14 @@ fn cgcall(c: *cgen, n: *node) void = {
};
};
};
// sret hidden first-arg (#23): load &dest into RDI AFTER all
// user-arg pops have finished — intidx started at 1 so RDI was
// never written. The CALL emit follows immediately.
if (sretcs > 0) {
emitline("\tLEAQ\t");
emitoff(sretcalloff: i64);
emitline("(BP), DI\n");
};
if (isfnptrcall) {
// Load fn-ptr field value into AX; CALL AX. We emit the
// load AFTER the args have been popped (so AX/BX/etc
@@ -5304,6 +5331,21 @@ fn cgassign(c: *cgen, n: *node) void = {
};
if (n.rhs != nil
&& n.rhs.kind == nkind.N_CALL) {
// sret receive (#23): plain
// TY_STRUCT > 24B from a CALL.
// `s` is the prealloc dest; the
// callee writes through hidden RDI
// directly into off(BP). Mirror of
// cglet's sret branch.
if (lcnsz > 24) {
let rscs: i32 = callsretsize(c, n.rhs);
if (rscs > 0) {
c.sretdestoff = off;
cgexpr(c, n.rhs);
c.sretdestoff = 0;
return;
};
};
let lcsz: i32 = lcnsz;
if (lcsz <= 24) {
let tlm: i32 = lcsz - (lcsz / 8) * 8;

View File

@@ -291,15 +291,106 @@ fn cgreturn(c: *cgen, n: *node) void = {
c.lastwasreturn = 1;
return;
};
// sret return (#23): plain TY_STRUCT > 24B. Callee writes
// through *(@sretarg) (the caller-prealloc dest saved at
// the prologue), then loads @sretarg into RAX and rets —
// the SysV "return the pointer" discipline. Two rhs shapes
// are wired: N_IDENT (word-copy from rhs slot to *(dest))
// and N_STRUCTLIT (cgstructlitfill with mode=1 PTR_LOCAL).
if (c.sretargoff != 0) {
let scs: i32 = sretretsize(c, c.fnret);
if (scs > 0) {
// `return f();` from a sret callee would silent-
// miscompile: cgexpr writes inner's result to
// @sretscr but outer never copies into *@sretarg
// and never sets RAX. Fail loud per CLAUDE.md
// rule 7; the `let r = f(); return r;` workaround
// is already wired and byte-id with cstage.
if (rhs.kind == nkind.N_CALL) {
let m: str = "ww: cgreturn: sret return-forwarding for >24B struct not wired (task #23)\n";
os.write(2, m.ptr, m.len: u64);
os.exit(1);
};
let okrhs: bool = false;
if (rhs.kind == nkind.N_IDENT) { okrhs = true; };
if (rhs.kind == nkind.N_STRUCTLIT) { okrhs = true; };
if (okrhs) {
if (rhs.kind == nkind.N_STRUCTLIT) {
let trefn: *node = rhs.lhs;
let sname: str;
sname.ptr = nil; sname.len = 0;
if (trefn != nil) {
if (trefn.kind == nkind.N_IDENT) { sname = trefn.str; }
else { if (trefn.kind == nkind.N_TNAME) { sname = trefn.str; }; };
};
let sret_si: *structinfo = structlookup(c, sname);
if (sret_si != nil) {
let emptys: str;
emptys.ptr = nil; emptys.len = 0;
// mode=1 (PTR_LOCAL): base reg = BX,
// reloaded from @sretarg(BP) before
// each field store. disp = 0 because
// the dest pointer IS the struct base.
cgstructlitfill(c, sret_si, rhs,
1, c.sretargoff, emptys,
0, scs);
};
} else {
let rl: *local = localfindnode(c, rhs.str);
if (rl != nil) {
emitline("\tMOVQ\t");
emitoff(c.sretargoff: i64);
emitline("(BP), BX\n");
let k: i32 = 0;
for (k + 8 <= scs) {
emitline("\tMOVQ\t");
emitoff((rl.off + k): i64);
emitline("(BP), AX\n");
emitline("\tMOVQ\tAX, ");
emitoff(k: i64);
emitline("(BX)\n");
k += 8;
};
for (k + 4 <= scs) {
emitline("\tMOVL\t");
emitoff((rl.off + k): i64);
emitline("(BP), AX\n");
emitline("\tMOVL\tAX, ");
emitoff(k: i64);
emitline("(BX)\n");
k += 4;
};
for (k < scs) {
emitline("\tMOVB\t");
emitoff((rl.off + k): i64);
emitline("(BP), AX\n");
emitline("\tMOVB\tAX, ");
emitoff(k: i64);
emitline("(BX)\n");
k += 1;
};
};
};
// sret return: RAX = dest pointer.
emitline("\tMOVQ\t");
emitoff(c.sretargoff: i64);
emitline("(BP), AX\n");
emitline("\tMOVQ\tBP, SP\n");
emitline("\tPOPQ\tBP\n");
emitline("\tRET\n");
c.lastwasreturn = 1;
return;
};
};
};
// Whole-struct return for sizes <= 24B. ABI: AX=bytes[0..7],
// DX=bytes[8..15], CX=bytes[16..23]. Mirrors cstage cgen.c
// N_RETURN TY_STRUCT branch. Two rhs shapes are wired:
// N_IDENT (word-copy from rhs local slot) and N_STRUCTLIT
// (field-by-field store at scratch+foff, with tagged fields
// delegated to cgwidentaggedstore). Call-result chain return
// is deferred to #5's receive side. Sizes > 24B fall through
// to the scalar path below (only AX gets the first qword),
// pending sret.
// is deferred to #5's receive side. Sizes > 24B route through
// the sret arm above.
let rname: str;
rname.ptr = nil; rname.len = 0;
if (c.fnret != nil) {
@@ -623,6 +714,22 @@ fn cglet(c: *cgen, n: *node) void = {
return;
};
};
// sret receive (#23): plain TY_STRUCT > 24B from a call.
// The let's own slot IS the caller-prealloc dest; the
// nested cgexpr → cgcall path emits `LEAQ off(BP), DI`
// before the CALL and the callee writes through it. No
// AX/DX/CX shuffle; AX returns the dest pointer per SysV
// sret discipline (irrelevant here).
if (rhs.kind == nkind.N_CALL) {
let scs: i32 = callsretsize(c, rhs);
if (scs > 0) {
c.sretdestoff = off;
cgexpr(c, rhs);
c.sretdestoff = 0;
c.lastwasreturn = 0;
return;
};
};
// Whole-struct receive for sizes <=24B (call-result rhs).
// Counterpart of #4's cgreturn ABI: cgexpr leaves
// AX=bytes[0..7], DX=bytes[8..15], CX=bytes[16..23],

View File

@@ -1348,6 +1348,49 @@ fn structnaturalsize(si: *structinfo) i32 = {
return n;
};
// sretretsize — if `t` ultimately denotes a plain TY_STRUCT > 24B,
// return its natural size; else 0. Tagged unions, tuples, str,
// slices, scalars route through their existing register-return ABIs
// (AX/DX/CX/[R8]) regardless of size. Task #23 mirrors cstage's
// cg_sret_retsize predicate. Resolves N_TNAME → struct via structlookup
// and unwraps one leading N_TBANG so `type box = !big;` still
// triggers sret on the underlying big.
export fn sretretsize(c: *cgen, t: *node) i32 = {
if (t == nil) { return 0; };
let r: *node = t;
if (r.kind == nkind.N_TBANG) {
r = r.lhs;
if (r == nil) { return 0; };
};
if (r.kind != nkind.N_TNAME) { return 0; };
// Primitives / aliased-to-primitives are never sret.
if (primsize(r.str) > 0) { return 0; };
if (streq(r.str, "str")) { return 0; };
let si: *structinfo = structlookup(c, r.str);
if (si == nil) { return 0; };
let n: i32 = structnaturalsize(si);
if (n <= 24) { return 0; };
return n;
};
// callsretsize — if N_CALL `n`'s callee returns a plain TY_STRUCT
// > 24B, return its natural size; else 0. Wraps sretretsize over the
// callee's resolved return type, used by cglet / cgassign receive
// sites and cgcall to detect sret at the receive / emit boundaries.
export fn callsretsize(c: *cgen, n: *node) i32 = {
if (n == nil) { return 0; };
if (n.kind != nkind.N_CALL) { return 0; };
let callee: *node = n.lhs;
if (callee == nil) { return 0; };
let cn: str;
cn.ptr = nil; cn.len = 0;
if (callee.kind == nkind.N_IDENT) { cn = callee.str; };
if (callee.kind == nkind.N_DOT) { cn = callee.str; };
if (cn.len == 0) { return 0; };
let rt: *node = fnretlookup(c, cn);
return sretretsize(c, rt);
};
fn structlookup(c: *cgen, name: str) *structinfo = {
// Exact match first: bare-from-source struct names and already-
// leafed lookups hit here directly.

View File

@@ -7738,6 +7738,49 @@ fn structnaturalsize(si: *structinfo) i32 = {
return n;
};
// sretretsize — if `t` ultimately denotes a plain TY_STRUCT > 24B,
// return its natural size; else 0. Tagged unions, tuples, str,
// slices, scalars route through their existing register-return ABIs
// (AX/DX/CX/[R8]) regardless of size. Task #23 mirrors cstage's
// cg_sret_retsize predicate. Resolves N_TNAME → struct via structlookup
// and unwraps one leading N_TBANG so `type box = !big;` still
// triggers sret on the underlying big.
export fn sretretsize(c: *cgen, t: *node) i32 = {
if (t == nil) { return 0; };
let r: *node = t;
if (r.kind == nkind.N_TBANG) {
r = r.lhs;
if (r == nil) { return 0; };
};
if (r.kind != nkind.N_TNAME) { return 0; };
// Primitives / aliased-to-primitives are never sret.
if (primsize(r.str) > 0) { return 0; };
if (streq(r.str, "str")) { return 0; };
let si: *structinfo = structlookup(c, r.str);
if (si == nil) { return 0; };
let n: i32 = structnaturalsize(si);
if (n <= 24) { return 0; };
return n;
};
// callsretsize — if N_CALL `n`'s callee returns a plain TY_STRUCT
// > 24B, return its natural size; else 0. Wraps sretretsize over the
// callee's resolved return type, used by cglet / cgassign receive
// sites and cgcall to detect sret at the receive / emit boundaries.
export fn callsretsize(c: *cgen, n: *node) i32 = {
if (n == nil) { return 0; };
if (n.kind != nkind.N_CALL) { return 0; };
let callee: *node = n.lhs;
if (callee == nil) { return 0; };
let cn: str;
cn.ptr = nil; cn.len = 0;
if (callee.kind == nkind.N_IDENT) { cn = callee.str; };
if (callee.kind == nkind.N_DOT) { cn = callee.str; };
if (cn.len == 0) { return 0; };
let rt: *node = fnretlookup(c, cn);
return sretretsize(c, rt);
};
fn structlookup(c: *cgen, name: str) *structinfo = {
// Exact match first: bare-from-source struct names and already-
// leafed lookups hit here directly.
@@ -12942,6 +12985,24 @@ fn cgcall(c: *cgen, n: *node) void = {
};
};
let nargs: i32 = pushargsrev(c, n.list, calleeparams);
// sret call (#23): callee returns plain TY_STRUCT > 24B. The
// dest pointer lands in RDI; start intidx at 1 to skip RDI in
// the user-arg pop loop and emit `LEAQ off(BP), DI` AFTER all
// pops have finished (so they don't clobber RDI). The dest off
// is either the receive site's slot (c.sretdestoff, propagated
// from cglet / cgassign ident) or the per-fn @sretscr discard
// slot reserved by scanlocals.
let sretcs: i32 = callsretsize(c, n);
let sretcalloff: i32 = 0;
if (sretcs > 0) {
if (c.sretdestoff != 0) {
sretcalloff = c.sretdestoff;
c.sretdestoff = 0;
} else {
sretcalloff = localadd(c, "@sretscr",
c.sretscrsz, nil);
};
};
// Pop forward. Float args were pushed as 8 bytes from X0 via
// SUBQ+MOVSD; pop into the XMM stream (X0..X7). Everything else
// pops into the int stream (DI..R9) per the SysV ABI. Walk the
@@ -12950,6 +13011,7 @@ fn cgcall(c: *cgen, n: *node) void = {
// the remaining slots stay on the stack and the callee reads them
// via 16+8*k(BP). Caller-cleanup is emitted after the CALL.
let intidx: i32 = 0;
if (sretcs > 0) { intidx = 1; };
let fpidx: i32 = 0;
let a: *node = n.list;
let popped: i32 = 0;
@@ -13080,6 +13142,14 @@ fn cgcall(c: *cgen, n: *node) void = {
};
};
};
// sret hidden first-arg (#23): load &dest into RDI AFTER all
// user-arg pops have finished — intidx started at 1 so RDI was
// never written. The CALL emit follows immediately.
if (sretcs > 0) {
emitline("\tLEAQ\t");
emitoff(sretcalloff: i64);
emitline("(BP), DI\n");
};
if (isfnptrcall) {
// Load fn-ptr field value into AX; CALL AX. We emit the
// load AFTER the args have been popped (so AX/BX/etc
@@ -15255,6 +15325,21 @@ fn cgassign(c: *cgen, n: *node) void = {
};
if (n.rhs != nil
&& n.rhs.kind == nkind.N_CALL) {
// sret receive (#23): plain
// TY_STRUCT > 24B from a CALL.
// `s` is the prealloc dest; the
// callee writes through hidden RDI
// directly into off(BP). Mirror of
// cglet's sret branch.
if (lcnsz > 24) {
let rscs: i32 = callsretsize(c, n.rhs);
if (rscs > 0) {
c.sretdestoff = off;
cgexpr(c, n.rhs);
c.sretdestoff = 0;
return;
};
};
let lcsz: i32 = lcnsz;
if (lcsz <= 24) {
let tlm: i32 = lcsz - (lcsz / 8) * 8;
@@ -15744,15 +15829,106 @@ fn cgreturn(c: *cgen, n: *node) void = {
c.lastwasreturn = 1;
return;
};
// sret return (#23): plain TY_STRUCT > 24B. Callee writes
// through *(@sretarg) (the caller-prealloc dest saved at
// the prologue), then loads @sretarg into RAX and rets —
// the SysV "return the pointer" discipline. Two rhs shapes
// are wired: N_IDENT (word-copy from rhs slot to *(dest))
// and N_STRUCTLIT (cgstructlitfill with mode=1 PTR_LOCAL).
if (c.sretargoff != 0) {
let scs: i32 = sretretsize(c, c.fnret);
if (scs > 0) {
// `return f();` from a sret callee would silent-
// miscompile: cgexpr writes inner's result to
// @sretscr but outer never copies into *@sretarg
// and never sets RAX. Fail loud per CLAUDE.md
// rule 7; the `let r = f(); return r;` workaround
// is already wired and byte-id with cstage.
if (rhs.kind == nkind.N_CALL) {
let m: str = "ww: cgreturn: sret return-forwarding for >24B struct not wired (task #23)\n";
os.write(2, m.ptr, m.len: u64);
os.exit(1);
};
let okrhs: bool = false;
if (rhs.kind == nkind.N_IDENT) { okrhs = true; };
if (rhs.kind == nkind.N_STRUCTLIT) { okrhs = true; };
if (okrhs) {
if (rhs.kind == nkind.N_STRUCTLIT) {
let trefn: *node = rhs.lhs;
let sname: str;
sname.ptr = nil; sname.len = 0;
if (trefn != nil) {
if (trefn.kind == nkind.N_IDENT) { sname = trefn.str; }
else { if (trefn.kind == nkind.N_TNAME) { sname = trefn.str; }; };
};
let sret_si: *structinfo = structlookup(c, sname);
if (sret_si != nil) {
let emptys: str;
emptys.ptr = nil; emptys.len = 0;
// mode=1 (PTR_LOCAL): base reg = BX,
// reloaded from @sretarg(BP) before
// each field store. disp = 0 because
// the dest pointer IS the struct base.
cgstructlitfill(c, sret_si, rhs,
1, c.sretargoff, emptys,
0, scs);
};
} else {
let rl: *local = localfindnode(c, rhs.str);
if (rl != nil) {
emitline("\tMOVQ\t");
emitoff(c.sretargoff: i64);
emitline("(BP), BX\n");
let k: i32 = 0;
for (k + 8 <= scs) {
emitline("\tMOVQ\t");
emitoff((rl.off + k): i64);
emitline("(BP), AX\n");
emitline("\tMOVQ\tAX, ");
emitoff(k: i64);
emitline("(BX)\n");
k += 8;
};
for (k + 4 <= scs) {
emitline("\tMOVL\t");
emitoff((rl.off + k): i64);
emitline("(BP), AX\n");
emitline("\tMOVL\tAX, ");
emitoff(k: i64);
emitline("(BX)\n");
k += 4;
};
for (k < scs) {
emitline("\tMOVB\t");
emitoff((rl.off + k): i64);
emitline("(BP), AX\n");
emitline("\tMOVB\tAX, ");
emitoff(k: i64);
emitline("(BX)\n");
k += 1;
};
};
};
// sret return: RAX = dest pointer.
emitline("\tMOVQ\t");
emitoff(c.sretargoff: i64);
emitline("(BP), AX\n");
emitline("\tMOVQ\tBP, SP\n");
emitline("\tPOPQ\tBP\n");
emitline("\tRET\n");
c.lastwasreturn = 1;
return;
};
};
};
// Whole-struct return for sizes <= 24B. ABI: AX=bytes[0..7],
// DX=bytes[8..15], CX=bytes[16..23]. Mirrors cstage cgen.c
// N_RETURN TY_STRUCT branch. Two rhs shapes are wired:
// N_IDENT (word-copy from rhs local slot) and N_STRUCTLIT
// (field-by-field store at scratch+foff, with tagged fields
// delegated to cgwidentaggedstore). Call-result chain return
// is deferred to #5's receive side. Sizes > 24B fall through
// to the scalar path below (only AX gets the first qword),
// pending sret.
// is deferred to #5's receive side. Sizes > 24B route through
// the sret arm above.
let rname: str;
rname.ptr = nil; rname.len = 0;
if (c.fnret != nil) {
@@ -16076,6 +16252,22 @@ fn cglet(c: *cgen, n: *node) void = {
return;
};
};
// sret receive (#23): plain TY_STRUCT > 24B from a call.
// The let's own slot IS the caller-prealloc dest; the
// nested cgexpr → cgcall path emits `LEAQ off(BP), DI`
// before the CALL and the callee writes through it. No
// AX/DX/CX shuffle; AX returns the dest pointer per SysV
// sret discipline (irrelevant here).
if (rhs.kind == nkind.N_CALL) {
let scs: i32 = callsretsize(c, rhs);
if (scs > 0) {
c.sretdestoff = off;
cgexpr(c, rhs);
c.sretdestoff = 0;
c.lastwasreturn = 0;
return;
};
};
// Whole-struct receive for sizes <=24B (call-result rhs).
// Counterpart of #4's cgreturn ABI: cgexpr leaves
// AX=bytes[0..7], DX=bytes[8..15], CX=bytes[16..23],
@@ -16804,6 +16996,20 @@ fn tagscrbump(c: *cgen, need: i32) i32 = {
return delta;
};
// sretscrbump — sister of tagscrbump for the sret discard slot
// (#23). Tracks max sret return type used as a CALL discard / nested
// receiver. Returns frame-byte delta vs the previous high water mark
// (rounded up to 8B).
fn sretscrbump(c: *cgen, need: i32) i32 = {
let n: i32 = need;
if (n < 8) { n = 8; };
if ((n & 7) != 0) { n = (n + 7) & ~7; };
if (n <= c.sretscrsz) { return 0; };
let delta: i32 = n - c.sretscrsz;
c.sretscrsz = n;
return delta;
};
//
// Recursively walks the body to count every local `let`. Each gets a
// slot sized by slotsize(typ); 8-byte default. Match-bindings + for-
@@ -17140,6 +17346,17 @@ fn scanlocals(c: *cgen, n: *node) i32 = {
};
};
};
// sret CALL (#23): callee returns plain TY_STRUCT > 24B. The
// receive site (cglet / cgassign ident) overrides at emit time
// with the dest local's own slot; discards / nested calls fall
// back to @sretscr. Single-slot per fn sized to the max sret
// return — sretscrbump tracks the high-water mark so a later
// larger call grows the frame without re-counting the prior
// reservation. Mirrors @tagscr's cumulative tagscrbump.
if (n.kind == nkind.N_CALL) {
let scs: i32 = callsretsize(c, n);
if (scs > 0) { total += sretscrbump(c, scs); };
};
// Call-site struct-payload widening uses @tagscr — when the
// arg is a struct literal/ident and the callee's param is
// tagged, pushargsrev materialises in scratch and pushes.
@@ -17254,7 +17471,11 @@ fn scanlocals(c: *cgen, n: *node) i32 = {
fn cgfnparams(c: *cgen, params: *node) void = {
let p: *node = params;
// sret (#23): RDI is consumed by the hidden dest pointer
// (already spilled to @sretarg by cgfn); the first user param
// lands in SI.
let idx: i32 = 0;
if (c.sretargoff != 0) { idx = 1; };
let fidx: i32 = 0;
// Cursor for args that overflow the SysV reg windows. Each
// stack-passed arg lives at 16+8*k(BP) — no spill, the local
@@ -17564,6 +17785,14 @@ fn cgfn(c: *cgen, fn_: *node) void = {
c.curmod = fn_.module;
c.fnret = fn_.lhs;
// sret callee (#23): return type is plain TY_STRUCT > 24B.
// Reserve 8B for @sretarg (holds the saved hidden RDI dest
// pointer); cgfnparams skips DI for user args, cgreturn writes
// through *(@sretarg) and returns @sretarg in RAX. Decision
// made here so the frame pre-scan and cgfnparams see the same
// view of the int-arg cursor.
let sret_callee: bool = sretretsize(c, c.fnret) > 0;
// Emit the TEXT label via emitfnname so the def site picks up the
// same skip rule (FFI / `main` / empty-module) and the same module
// hint (this fn's own module) that the call sites use. Drops the
@@ -17586,6 +17815,12 @@ fn cgfn(c: *cgen, fn_: *node) void = {
let frame: i32 = 0;
let argi: i32 = 0;
let fargi: i32 = 0;
// Reserve @sretarg (8B) BEFORE the param-induced frame, and
// start argi at 1 so the param walker sees RDI as consumed.
if (sret_callee) {
frame += 8;
argi = 1;
};
for (scanp != nil) {
if (scanp.kind == nkind.N_PARAM) {
let isvar: bool = scanp.op == tkind.TK_ELLIPSIS;
@@ -17678,6 +17913,13 @@ fn cgfn(c: *cgen, fn_: *node) void = {
emitint(frame: i64);
emitline(", SP\n");
if (sret_callee) {
let saoff: i32 = localadd(c, "@sretarg", 8, nil);
emitline("\tMOVQ\tDI, ");
emitoff(saoff: i64);
emitline("(BP)\n");
};
cgfnparams(c, fn_.list);
c.lastwasreturn = 0;
// Iterate the fn body's statements directly rather than dispatching
@@ -18179,6 +18421,29 @@ type cgen = struct {
// later emit reuses. Mirrors c.tagscrsz pattern (#38) but tracks
// offset, not size (per-fn return type is fixed, so size is too).
retscroff: i32,
// System V AMD64 sret discipline (#23). Plain TY_STRUCT returns
// with size > 24B are passed via a hidden first-arg pointer
// (RDI) to a caller-prealloc dest; the callee writes through
// that pointer and returns it in RAX.
//
// sretargoff — callee-side @sretarg slot (8B, holds saved RDI).
// Set in cgfn prologue when the fn's return type
// triggers sret. 0 means N/A.
// sretdestoff — caller-side dest BP offset, propagated from a
// receive site (cglet / cgassign ident) to the
// nested cgexpr → cgcall so the call emits
// `LEAQ off(BP), DI` instead of allocating a
// scratch. 0 means no receiver wired.
// sretscroff — per-fn @sretscr discard slot, used by sret CALLs
// whose result has no named receiver. Single-slot
// SSoT mirroring c.retscroff; the scanlocals walk
// sums c.sretscrsz to pre-reserve.
// sretscrsz — max sret discard size in this fn (sums during
// scanlocals, consumed by localadd("@sretscr", ...)).
sretargoff: i32,
sretdestoff: i32,
sretscroff: i32,
sretscrsz: i32,
};
// Top-level mutable `let` registry. Mirrors cmd/w6c/cgen.c LetVar.
@@ -18202,6 +18467,10 @@ fn cgeninit(c: *cgen, a: *arena) void = {
c.varargseq = 0;
c.tagscrsz = 0;
c.retscroff = 0;
c.sretargoff = 0;
c.sretdestoff = 0;
c.sretscroff = 0;
c.sretscrsz = 0;
// Note: strlit_seq, strlits, ffis are *not* reset here; they
// persist across cgfn calls within one file. cgfile resets them
// at the start of each compilation unit.
@@ -18285,6 +18554,23 @@ fn localadd(c: *cgen, name: str, sz: i32, tnode: *node) i32 = {
c.retscroff = off;
return off;
};
// @sretarg / @sretscr (#23): same single-slot SSoT
// pattern as @retscr. @sretarg holds the saved hidden
// RDI for sret callees (8B, set once per fn at the
// prologue); @sretscr is the caller-side discard slot
// for sret CALLs whose result is dropped.
if (streq(name, "@sretarg")) {
if (c.sretargoff != 0) { return c.sretargoff; };
let off: i32 = localalloc(c, name, sz, tnode);
c.sretargoff = off;
return off;
};
if (streq(name, "@sretscr")) {
if (c.sretscroff != 0) { return c.sretscroff; };
let off: i32 = localalloc(c, name, sz, tnode);
c.sretscroff = off;
return off;
};
let cur: *local = c.locals;
for (cur != nil) {
let cn: str = cur.name;