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:
11
Makefile
11
Makefile
@@ -345,6 +345,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
|
||||
$(BIN)/test_xmod_valglobal_dot_run \
|
||||
$(BIN)/test_len_strglobal_run \
|
||||
$(BIN)/test_tuple_sret_callee \
|
||||
$(BIN)/test_tuple_sret_receive_run \
|
||||
$(BIN)/test_widen_pad_zero_run \
|
||||
$(BIN)/test_named_ptr_alias_variant_widen \
|
||||
$(BIN)/test_single_field_struct_zeroinit \
|
||||
@@ -850,6 +851,16 @@ $(BIN)/test_tuple_sret_callee: test/wcc/798_tuple_sret_callee.c \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
# #10 Fold B (wide tuple-return / sret, RECEIVE side): the call/receive
|
||||
# end of the over-cap tuple ABI — single-var-let, destructure, reassign,
|
||||
# return-forward. Runtime round-trip (the net Fold A deferred — byte-id is
|
||||
# blind to a SEND/RECEIVE layout mismatch) + cs==ww byte-id. Self-contained
|
||||
# single-file probes (953 model), no selfhost traversal.
|
||||
$(BIN)/test_tuple_sret_receive_run: test/wcc/799_tuple_sret_receive_run.c \
|
||||
$(BIN)/ww $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
# #15: widening a bare *vtable into a NAMED-alias variant (`stream` =
|
||||
# *vtable) of `(file | stream)` must compute the right tag, not default
|
||||
# to tag 0. Both-stage byte-id + runtime, plus a degenerate-ambiguity
|
||||
|
||||
150
cmd/w6c/cgen.c
150
cmd/w6c/cgen.c
@@ -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;
|
||||
|
||||
@@ -25659,6 +25659,26 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
};
|
||||
return;
|
||||
};
|
||||
// #10 Fold B: over-cap tuple reassign `t = f();`. t's
|
||||
// slot (off) IS the caller-prealloc dest; the callee
|
||||
// writes the whole tuple through hidden RDI. Keys on
|
||||
// callsretsize (the shared sret SSoT) for a tuple-
|
||||
// returning call — rettupleof distinguishes it from a
|
||||
// >24B struct, which keeps its own size-aware recv
|
||||
// below. Mirrors the cstage N_ASSIGN-ident over-cap arm.
|
||||
if (n.op == tkind.TK_ASSIGN && n.rhs != nil
|
||||
&& n.rhs.kind == nkind.N_CALL) {
|
||||
let rtup: *node = rettupleof(c, n.rhs);
|
||||
if (rtup != nil) {
|
||||
let rscs: i32 = callsretsize(c, n.rhs);
|
||||
if (rscs > 0) {
|
||||
c.sretdestoff = off;
|
||||
cgexpr(c, n.rhs);
|
||||
c.sretdestoff = 0;
|
||||
return;
|
||||
};
|
||||
};
|
||||
};
|
||||
// Detect str/slice-typed local — assignment must store
|
||||
// both halves (AX=ptr at +0, BX=len at +8) for str,
|
||||
// plus the cap (CX at +16) for slice.
|
||||
@@ -27668,8 +27688,81 @@ fn cgmassign(c: *cgen, n: *node) void = {
|
||||
// multi-assign idiom — rule-9 carve-out. Over-capacity loud-stops.
|
||||
let rettuple: *node = rettupleof(c, n.rhs);
|
||||
|
||||
// #10 Fold B: over-cap tuple destructure REASSIGN. Same sret copy-out
|
||||
// as cgmlet 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 `_`). Byte-identical to the
|
||||
// cstage N_MASSIGN over-cap arm.
|
||||
let sretrecv: i32 = 0;
|
||||
if (n.rhs != nil) {
|
||||
if (n.rhs.kind == nkind.N_CALL) {
|
||||
sretrecv = callsretsize(c, n.rhs);
|
||||
};
|
||||
};
|
||||
|
||||
if (n.rhs != nil) { cgexpr(c, n.rhs); };
|
||||
|
||||
if (sretrecv > 0) {
|
||||
let scr: i32 = localfind(c, "@sretscr");
|
||||
let pt2: *node = nil;
|
||||
if (rettuple != nil) { pt2 = rettuple.list; };
|
||||
let foff: i32 = 0;
|
||||
let lb: *node = n.list;
|
||||
for (lb != nil) {
|
||||
let tn: *node = nil;
|
||||
if (pt2 != nil) { tn = pt2.lhs; };
|
||||
let isflt: bool = isfloattype(c, tn);
|
||||
let wide: bool = isstrtype(c, tn) || isslicetype(c, tn);
|
||||
let esz: i32 = 8;
|
||||
if (pt2 != nil) {
|
||||
let eti: *tinfo = pt2.lhs.type_: *tinfo;
|
||||
if (eti != nil) { esz = eti.size: i32; };
|
||||
};
|
||||
let off: i32 = 0;
|
||||
if (lb.kind == nkind.N_IDENT) { off = localfind(c, lb.str); };
|
||||
if (off != 0) {
|
||||
if (isflt) {
|
||||
let mov: str = "MOVSD";
|
||||
if (isf32type(c, tn)) { mov = "MOVSS"; };
|
||||
emitline("\t"); emitline(mov); emitline("\t");
|
||||
emitoff((scr + foff): i64);
|
||||
emitline("(BP), X0\n");
|
||||
emitline("\t"); emitline(mov);
|
||||
emitline("\tX0, ");
|
||||
emitoff(off: i64); emitline("(BP)\n");
|
||||
} else {
|
||||
if (wide) {
|
||||
let k: i32 = 0;
|
||||
for (k < esz) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff((scr + foff + k): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff((off + k): i64);
|
||||
emitline("(BP)\n");
|
||||
k += 8;
|
||||
};
|
||||
} else {
|
||||
let lop: str = tnodeloadop(c, tn, esz);
|
||||
let sop: str = tnodestoreop(c, tn, esz);
|
||||
emitline("\t"); emitline(lop);
|
||||
emitline("\t");
|
||||
emitoff((scr + foff): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\t"); emitline(sop);
|
||||
emitline("\tAX, ");
|
||||
emitoff(off: i64); emitline("(BP)\n");
|
||||
};
|
||||
};
|
||||
};
|
||||
foff += esz;
|
||||
lb = lb.next;
|
||||
if (pt2 != nil) { pt2 = pt2.next; };
|
||||
};
|
||||
c.lastwasreturn = 0;
|
||||
return;
|
||||
};
|
||||
|
||||
let ssecap: i32 = TUPLE_SSECAP; // X0,X1 per SysV
|
||||
let gptotal: i32 = 0;
|
||||
let ssetotal: i32 = 0;
|
||||
@@ -27755,8 +27848,75 @@ fn cgmlet(c: *cgen, n: *node) void = {
|
||||
// scalar rides 1 word into an 8B slot. Over-capacity loud-stops.
|
||||
let rettuple: *node = rettupleof(c, rhs);
|
||||
|
||||
// #10 Fold B: over-cap tuple destructure RECEIVE. The callee sret'd
|
||||
// the whole tuple into the @sretscr discard slot (cgcall sees
|
||||
// callsretsize > 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). Byte-identical to the cstage N_MLET over-cap arm.
|
||||
let sretrecv: i32 = 0;
|
||||
if (rhs.kind == nkind.N_CALL) { sretrecv = callsretsize(c, rhs); };
|
||||
|
||||
cgexpr(c, rhs);
|
||||
|
||||
if (sretrecv > 0) {
|
||||
let scr: i32 = localfind(c, "@sretscr");
|
||||
let pt2: *node = nil;
|
||||
if (rettuple != nil) { pt2 = rettuple.list; };
|
||||
let foff: i32 = 0;
|
||||
let lb: *node = n.list;
|
||||
for (lb != nil) {
|
||||
let tn: *node = nil;
|
||||
if (pt2 != nil) { tn = pt2.lhs; };
|
||||
let isflt: bool = isfloattype(c, tn);
|
||||
let wide: bool = isstrtype(c, tn) || isslicetype(c, tn);
|
||||
let esz: i32 = 8;
|
||||
if (pt2 != nil) {
|
||||
let eti: *tinfo = pt2.lhs.type_: *tinfo;
|
||||
if (eti != nil) { esz = eti.size: i32; };
|
||||
};
|
||||
let bsz: i32 = 8;
|
||||
if (wide) { bsz = tyslicesize(): i32; };
|
||||
let off: i32 = localadd(c, lb.str, bsz, tn);
|
||||
if (isflt) {
|
||||
let mov: str = "MOVSD";
|
||||
if (isf32type(c, tn)) { mov = "MOVSS"; };
|
||||
emitline("\t"); emitline(mov); emitline("\t");
|
||||
emitoff((scr + foff): i64);
|
||||
emitline("(BP), X0\n");
|
||||
emitline("\t"); emitline(mov); emitline("\tX0, ");
|
||||
emitoff(off: i64); emitline("(BP)\n");
|
||||
} else {
|
||||
if (wide) {
|
||||
let k: i32 = 0;
|
||||
for (k < esz) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff((scr + foff + k): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff((off + k): i64);
|
||||
emitline("(BP)\n");
|
||||
k += 8;
|
||||
};
|
||||
} else {
|
||||
let lop: str = tnodeloadop(c, tn, esz);
|
||||
let sop: str = tnodestoreop(c, tn, esz);
|
||||
emitline("\t"); emitline(lop); emitline("\t");
|
||||
emitoff((scr + foff): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\t"); emitline(sop);
|
||||
emitline("\tAX, ");
|
||||
emitoff(off: i64); emitline("(BP)\n");
|
||||
};
|
||||
};
|
||||
foff += esz;
|
||||
lb = lb.next;
|
||||
if (pt2 != nil) { pt2 = pt2.next; };
|
||||
};
|
||||
c.lastwasreturn = 0;
|
||||
return;
|
||||
};
|
||||
|
||||
let ssecap: i32 = TUPLE_SSECAP; // X0,X1 per SysV
|
||||
let gptotal: i32 = 0;
|
||||
let ssetotal: i32 = 0;
|
||||
|
||||
@@ -6971,6 +6971,26 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
};
|
||||
return;
|
||||
};
|
||||
// #10 Fold B: over-cap tuple reassign `t = f();`. t's
|
||||
// slot (off) IS the caller-prealloc dest; the callee
|
||||
// writes the whole tuple through hidden RDI. Keys on
|
||||
// callsretsize (the shared sret SSoT) for a tuple-
|
||||
// returning call — rettupleof distinguishes it from a
|
||||
// >24B struct, which keeps its own size-aware recv
|
||||
// below. Mirrors the cstage N_ASSIGN-ident over-cap arm.
|
||||
if (n.op == tkind.TK_ASSIGN && n.rhs != nil
|
||||
&& n.rhs.kind == nkind.N_CALL) {
|
||||
let rtup: *node = rettupleof(c, n.rhs);
|
||||
if (rtup != nil) {
|
||||
let rscs: i32 = callsretsize(c, n.rhs);
|
||||
if (rscs > 0) {
|
||||
c.sretdestoff = off;
|
||||
cgexpr(c, n.rhs);
|
||||
c.sretdestoff = 0;
|
||||
return;
|
||||
};
|
||||
};
|
||||
};
|
||||
// Detect str/slice-typed local — assignment must store
|
||||
// both halves (AX=ptr at +0, BX=len at +8) for str,
|
||||
// plus the cap (CX at +16) for slice.
|
||||
|
||||
@@ -1706,8 +1706,81 @@ fn cgmassign(c: *cgen, n: *node) void = {
|
||||
// multi-assign idiom — rule-9 carve-out. Over-capacity loud-stops.
|
||||
let rettuple: *node = rettupleof(c, n.rhs);
|
||||
|
||||
// #10 Fold B: over-cap tuple destructure REASSIGN. Same sret copy-out
|
||||
// as cgmlet 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 `_`). Byte-identical to the
|
||||
// cstage N_MASSIGN over-cap arm.
|
||||
let sretrecv: i32 = 0;
|
||||
if (n.rhs != nil) {
|
||||
if (n.rhs.kind == nkind.N_CALL) {
|
||||
sretrecv = callsretsize(c, n.rhs);
|
||||
};
|
||||
};
|
||||
|
||||
if (n.rhs != nil) { cgexpr(c, n.rhs); };
|
||||
|
||||
if (sretrecv > 0) {
|
||||
let scr: i32 = localfind(c, "@sretscr");
|
||||
let pt2: *node = nil;
|
||||
if (rettuple != nil) { pt2 = rettuple.list; };
|
||||
let foff: i32 = 0;
|
||||
let lb: *node = n.list;
|
||||
for (lb != nil) {
|
||||
let tn: *node = nil;
|
||||
if (pt2 != nil) { tn = pt2.lhs; };
|
||||
let isflt: bool = isfloattype(c, tn);
|
||||
let wide: bool = isstrtype(c, tn) || isslicetype(c, tn);
|
||||
let esz: i32 = 8;
|
||||
if (pt2 != nil) {
|
||||
let eti: *tinfo = pt2.lhs.type_: *tinfo;
|
||||
if (eti != nil) { esz = eti.size: i32; };
|
||||
};
|
||||
let off: i32 = 0;
|
||||
if (lb.kind == nkind.N_IDENT) { off = localfind(c, lb.str); };
|
||||
if (off != 0) {
|
||||
if (isflt) {
|
||||
let mov: str = "MOVSD";
|
||||
if (isf32type(c, tn)) { mov = "MOVSS"; };
|
||||
emitline("\t"); emitline(mov); emitline("\t");
|
||||
emitoff((scr + foff): i64);
|
||||
emitline("(BP), X0\n");
|
||||
emitline("\t"); emitline(mov);
|
||||
emitline("\tX0, ");
|
||||
emitoff(off: i64); emitline("(BP)\n");
|
||||
} else {
|
||||
if (wide) {
|
||||
let k: i32 = 0;
|
||||
for (k < esz) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff((scr + foff + k): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff((off + k): i64);
|
||||
emitline("(BP)\n");
|
||||
k += 8;
|
||||
};
|
||||
} else {
|
||||
let lop: str = tnodeloadop(c, tn, esz);
|
||||
let sop: str = tnodestoreop(c, tn, esz);
|
||||
emitline("\t"); emitline(lop);
|
||||
emitline("\t");
|
||||
emitoff((scr + foff): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\t"); emitline(sop);
|
||||
emitline("\tAX, ");
|
||||
emitoff(off: i64); emitline("(BP)\n");
|
||||
};
|
||||
};
|
||||
};
|
||||
foff += esz;
|
||||
lb = lb.next;
|
||||
if (pt2 != nil) { pt2 = pt2.next; };
|
||||
};
|
||||
c.lastwasreturn = 0;
|
||||
return;
|
||||
};
|
||||
|
||||
let ssecap: i32 = TUPLE_SSECAP; // X0,X1 per SysV
|
||||
let gptotal: i32 = 0;
|
||||
let ssetotal: i32 = 0;
|
||||
@@ -1793,8 +1866,75 @@ fn cgmlet(c: *cgen, n: *node) void = {
|
||||
// scalar rides 1 word into an 8B slot. Over-capacity loud-stops.
|
||||
let rettuple: *node = rettupleof(c, rhs);
|
||||
|
||||
// #10 Fold B: over-cap tuple destructure RECEIVE. The callee sret'd
|
||||
// the whole tuple into the @sretscr discard slot (cgcall sees
|
||||
// callsretsize > 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). Byte-identical to the cstage N_MLET over-cap arm.
|
||||
let sretrecv: i32 = 0;
|
||||
if (rhs.kind == nkind.N_CALL) { sretrecv = callsretsize(c, rhs); };
|
||||
|
||||
cgexpr(c, rhs);
|
||||
|
||||
if (sretrecv > 0) {
|
||||
let scr: i32 = localfind(c, "@sretscr");
|
||||
let pt2: *node = nil;
|
||||
if (rettuple != nil) { pt2 = rettuple.list; };
|
||||
let foff: i32 = 0;
|
||||
let lb: *node = n.list;
|
||||
for (lb != nil) {
|
||||
let tn: *node = nil;
|
||||
if (pt2 != nil) { tn = pt2.lhs; };
|
||||
let isflt: bool = isfloattype(c, tn);
|
||||
let wide: bool = isstrtype(c, tn) || isslicetype(c, tn);
|
||||
let esz: i32 = 8;
|
||||
if (pt2 != nil) {
|
||||
let eti: *tinfo = pt2.lhs.type_: *tinfo;
|
||||
if (eti != nil) { esz = eti.size: i32; };
|
||||
};
|
||||
let bsz: i32 = 8;
|
||||
if (wide) { bsz = tyslicesize(): i32; };
|
||||
let off: i32 = localadd(c, lb.str, bsz, tn);
|
||||
if (isflt) {
|
||||
let mov: str = "MOVSD";
|
||||
if (isf32type(c, tn)) { mov = "MOVSS"; };
|
||||
emitline("\t"); emitline(mov); emitline("\t");
|
||||
emitoff((scr + foff): i64);
|
||||
emitline("(BP), X0\n");
|
||||
emitline("\t"); emitline(mov); emitline("\tX0, ");
|
||||
emitoff(off: i64); emitline("(BP)\n");
|
||||
} else {
|
||||
if (wide) {
|
||||
let k: i32 = 0;
|
||||
for (k < esz) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff((scr + foff + k): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff((off + k): i64);
|
||||
emitline("(BP)\n");
|
||||
k += 8;
|
||||
};
|
||||
} else {
|
||||
let lop: str = tnodeloadop(c, tn, esz);
|
||||
let sop: str = tnodestoreop(c, tn, esz);
|
||||
emitline("\t"); emitline(lop); emitline("\t");
|
||||
emitoff((scr + foff): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\t"); emitline(sop);
|
||||
emitline("\tAX, ");
|
||||
emitoff(off: i64); emitline("(BP)\n");
|
||||
};
|
||||
};
|
||||
foff += esz;
|
||||
lb = lb.next;
|
||||
if (pt2 != nil) { pt2 = pt2.next; };
|
||||
};
|
||||
c.lastwasreturn = 0;
|
||||
return;
|
||||
};
|
||||
|
||||
let ssecap: i32 = TUPLE_SSECAP; // X0,X1 per SysV
|
||||
let gptotal: i32 = 0;
|
||||
let ssetotal: i32 = 0;
|
||||
|
||||
@@ -25659,6 +25659,26 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
};
|
||||
return;
|
||||
};
|
||||
// #10 Fold B: over-cap tuple reassign `t = f();`. t's
|
||||
// slot (off) IS the caller-prealloc dest; the callee
|
||||
// writes the whole tuple through hidden RDI. Keys on
|
||||
// callsretsize (the shared sret SSoT) for a tuple-
|
||||
// returning call — rettupleof distinguishes it from a
|
||||
// >24B struct, which keeps its own size-aware recv
|
||||
// below. Mirrors the cstage N_ASSIGN-ident over-cap arm.
|
||||
if (n.op == tkind.TK_ASSIGN && n.rhs != nil
|
||||
&& n.rhs.kind == nkind.N_CALL) {
|
||||
let rtup: *node = rettupleof(c, n.rhs);
|
||||
if (rtup != nil) {
|
||||
let rscs: i32 = callsretsize(c, n.rhs);
|
||||
if (rscs > 0) {
|
||||
c.sretdestoff = off;
|
||||
cgexpr(c, n.rhs);
|
||||
c.sretdestoff = 0;
|
||||
return;
|
||||
};
|
||||
};
|
||||
};
|
||||
// Detect str/slice-typed local — assignment must store
|
||||
// both halves (AX=ptr at +0, BX=len at +8) for str,
|
||||
// plus the cap (CX at +16) for slice.
|
||||
@@ -27668,8 +27688,81 @@ fn cgmassign(c: *cgen, n: *node) void = {
|
||||
// multi-assign idiom — rule-9 carve-out. Over-capacity loud-stops.
|
||||
let rettuple: *node = rettupleof(c, n.rhs);
|
||||
|
||||
// #10 Fold B: over-cap tuple destructure REASSIGN. Same sret copy-out
|
||||
// as cgmlet 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 `_`). Byte-identical to the
|
||||
// cstage N_MASSIGN over-cap arm.
|
||||
let sretrecv: i32 = 0;
|
||||
if (n.rhs != nil) {
|
||||
if (n.rhs.kind == nkind.N_CALL) {
|
||||
sretrecv = callsretsize(c, n.rhs);
|
||||
};
|
||||
};
|
||||
|
||||
if (n.rhs != nil) { cgexpr(c, n.rhs); };
|
||||
|
||||
if (sretrecv > 0) {
|
||||
let scr: i32 = localfind(c, "@sretscr");
|
||||
let pt2: *node = nil;
|
||||
if (rettuple != nil) { pt2 = rettuple.list; };
|
||||
let foff: i32 = 0;
|
||||
let lb: *node = n.list;
|
||||
for (lb != nil) {
|
||||
let tn: *node = nil;
|
||||
if (pt2 != nil) { tn = pt2.lhs; };
|
||||
let isflt: bool = isfloattype(c, tn);
|
||||
let wide: bool = isstrtype(c, tn) || isslicetype(c, tn);
|
||||
let esz: i32 = 8;
|
||||
if (pt2 != nil) {
|
||||
let eti: *tinfo = pt2.lhs.type_: *tinfo;
|
||||
if (eti != nil) { esz = eti.size: i32; };
|
||||
};
|
||||
let off: i32 = 0;
|
||||
if (lb.kind == nkind.N_IDENT) { off = localfind(c, lb.str); };
|
||||
if (off != 0) {
|
||||
if (isflt) {
|
||||
let mov: str = "MOVSD";
|
||||
if (isf32type(c, tn)) { mov = "MOVSS"; };
|
||||
emitline("\t"); emitline(mov); emitline("\t");
|
||||
emitoff((scr + foff): i64);
|
||||
emitline("(BP), X0\n");
|
||||
emitline("\t"); emitline(mov);
|
||||
emitline("\tX0, ");
|
||||
emitoff(off: i64); emitline("(BP)\n");
|
||||
} else {
|
||||
if (wide) {
|
||||
let k: i32 = 0;
|
||||
for (k < esz) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff((scr + foff + k): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff((off + k): i64);
|
||||
emitline("(BP)\n");
|
||||
k += 8;
|
||||
};
|
||||
} else {
|
||||
let lop: str = tnodeloadop(c, tn, esz);
|
||||
let sop: str = tnodestoreop(c, tn, esz);
|
||||
emitline("\t"); emitline(lop);
|
||||
emitline("\t");
|
||||
emitoff((scr + foff): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\t"); emitline(sop);
|
||||
emitline("\tAX, ");
|
||||
emitoff(off: i64); emitline("(BP)\n");
|
||||
};
|
||||
};
|
||||
};
|
||||
foff += esz;
|
||||
lb = lb.next;
|
||||
if (pt2 != nil) { pt2 = pt2.next; };
|
||||
};
|
||||
c.lastwasreturn = 0;
|
||||
return;
|
||||
};
|
||||
|
||||
let ssecap: i32 = TUPLE_SSECAP; // X0,X1 per SysV
|
||||
let gptotal: i32 = 0;
|
||||
let ssetotal: i32 = 0;
|
||||
@@ -27755,8 +27848,75 @@ fn cgmlet(c: *cgen, n: *node) void = {
|
||||
// scalar rides 1 word into an 8B slot. Over-capacity loud-stops.
|
||||
let rettuple: *node = rettupleof(c, rhs);
|
||||
|
||||
// #10 Fold B: over-cap tuple destructure RECEIVE. The callee sret'd
|
||||
// the whole tuple into the @sretscr discard slot (cgcall sees
|
||||
// callsretsize > 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). Byte-identical to the cstage N_MLET over-cap arm.
|
||||
let sretrecv: i32 = 0;
|
||||
if (rhs.kind == nkind.N_CALL) { sretrecv = callsretsize(c, rhs); };
|
||||
|
||||
cgexpr(c, rhs);
|
||||
|
||||
if (sretrecv > 0) {
|
||||
let scr: i32 = localfind(c, "@sretscr");
|
||||
let pt2: *node = nil;
|
||||
if (rettuple != nil) { pt2 = rettuple.list; };
|
||||
let foff: i32 = 0;
|
||||
let lb: *node = n.list;
|
||||
for (lb != nil) {
|
||||
let tn: *node = nil;
|
||||
if (pt2 != nil) { tn = pt2.lhs; };
|
||||
let isflt: bool = isfloattype(c, tn);
|
||||
let wide: bool = isstrtype(c, tn) || isslicetype(c, tn);
|
||||
let esz: i32 = 8;
|
||||
if (pt2 != nil) {
|
||||
let eti: *tinfo = pt2.lhs.type_: *tinfo;
|
||||
if (eti != nil) { esz = eti.size: i32; };
|
||||
};
|
||||
let bsz: i32 = 8;
|
||||
if (wide) { bsz = tyslicesize(): i32; };
|
||||
let off: i32 = localadd(c, lb.str, bsz, tn);
|
||||
if (isflt) {
|
||||
let mov: str = "MOVSD";
|
||||
if (isf32type(c, tn)) { mov = "MOVSS"; };
|
||||
emitline("\t"); emitline(mov); emitline("\t");
|
||||
emitoff((scr + foff): i64);
|
||||
emitline("(BP), X0\n");
|
||||
emitline("\t"); emitline(mov); emitline("\tX0, ");
|
||||
emitoff(off: i64); emitline("(BP)\n");
|
||||
} else {
|
||||
if (wide) {
|
||||
let k: i32 = 0;
|
||||
for (k < esz) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff((scr + foff + k): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff((off + k): i64);
|
||||
emitline("(BP)\n");
|
||||
k += 8;
|
||||
};
|
||||
} else {
|
||||
let lop: str = tnodeloadop(c, tn, esz);
|
||||
let sop: str = tnodestoreop(c, tn, esz);
|
||||
emitline("\t"); emitline(lop); emitline("\t");
|
||||
emitoff((scr + foff): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\t"); emitline(sop);
|
||||
emitline("\tAX, ");
|
||||
emitoff(off: i64); emitline("(BP)\n");
|
||||
};
|
||||
};
|
||||
foff += esz;
|
||||
lb = lb.next;
|
||||
if (pt2 != nil) { pt2 = pt2.next; };
|
||||
};
|
||||
c.lastwasreturn = 0;
|
||||
return;
|
||||
};
|
||||
|
||||
let ssecap: i32 = TUPLE_SSECAP; // X0,X1 per SysV
|
||||
let gptotal: i32 = 0;
|
||||
let ssetotal: i32 = 0;
|
||||
|
||||
231
test/wcc/799_tuple_sret_receive_run.c
Normal file
231
test/wcc/799_tuple_sret_receive_run.c
Normal 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;
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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. */
|
||||
|
||||
Reference in New Issue
Block a user