wcc: tuple-param ABI via SSE/GP arg cursors (#163)

Tuples were unhandled as parameters — no tuple arm in arg-push, arg-pop, or
callee-recv in either stage — so a tuple param fell to the 1-GP-word else and
dropped all but its first element (integer tuple params too; floats doubly
lost). Add tuple-param arms (SEND push+pop, callee RECV) across both stages,
reusing #164's per-element SysV classify with the 6-GP (DI,SI,DX,CX,R8,R9) +
8-SSE (X0-X7) arg cursors. A frame slot @tupargscr decouples the producing
call's return cursor from the overlapping arg cursor (capture-before-clobber).
Overflow (>6 GP / >8 SSE) fails loud (rule 7). Scoped to the N_CALL producer;
first-class tuple values (ident/literal) remain a separate unimplemented gap.

Gate-blind (the bootstrap passes no tuple params) — covered by table-driven
probe 905, which proves pre-fix element-drop and the loud-stop.
This commit is contained in:
2026-05-27 22:13:46 +09:00
parent 153c7b3b46
commit 0465c423c1
8 changed files with 986 additions and 3 deletions

View File

@@ -353,6 +353,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_f32stamp_run \
$(BIN)/test_f32arg_run \
$(BIN)/test_tuprecv_f64_run \
$(BIN)/test_tupparam_run \
$(BIN)/test_floats_run \
$(BIN)/test_size_type_run \
$(BIN)/test_types_sizelim_run \
@@ -1266,6 +1267,11 @@ $(BIN)/test_tuprecv_f64_run: test/wcc/956_tuprecv_f64_run.c $(BIN)/ww \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_tupparam_run: test/wcc/905_tupparam_run.c $(BIN)/ww \
$(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
sizelint:
@sh tools/sizelint

View File

@@ -46,6 +46,17 @@ static int cg_retscr;
* @retscr single-slot convention + wwstage's `@tupfscr` '@'-prefix dedup;
* 0 means "not yet allocated". */
static int cg_tupfscr;
/* Per-fn @tupargscr staging slot (#163, single-slot SSoT). A tuple
* PASSED AS AN ARGUMENT (the param twin of #164's tuple return) is left
* by its producing call in the return-ABI cursor (AX/DX/CX/R8 + X0/X1);
* the SEND restages it into this slot positionally (tuple_store), then
* pushes the slot words onto the stack so the pop drains them into the
* SysV ARG cursor (DI/SI/.. + X0..X7). A frame slot decouples the
* return-class regs (which overlap the arg-class regs) from the arg
* placement. Reused per tuple arg (drained to the stack before the next
* arg); 0 means "not yet allocated", cg_tupargscr_sz the cached width. */
static int cg_tupargscr;
static int cg_tupargscr_sz;
/* Per-fn @-prefix scratch SSoT (task #26, follow-up to #15-cstage's
* @retscr). Pre-#26 each site allocated a labelseq-stamped fresh slot
* per call (mklabel "tagbase" / "tagscr" / "argscr" / "idxscr"); the
@@ -191,6 +202,22 @@ node_isslice(Node *n)
return n && type_isslice(n->type);
}
/* node_tuplearg — the underlying TY_TUPLE Type of a tuple-typed argument
* VALUE, else NULL. #163: scoped to an N_CALL producer — the only form
* that leaves a tuple in the return-ABI cursor (AX/DX/CX/R8 + X0/X1, per
* #164). A tuple ident / literal as a first-class value is a separate
* unimplemented gap (`let t = (1,2)` does not materialise a slot today),
* so the SEND restricts to the call form and loud-stops the rest rather
* than push stale registers (rule 7, never a silent drop). */
static Type *
node_tuplearg(Node *n)
{
if (n == NULL || n->kind != N_CALL) return NULL;
Type *t = n->type;
Type *u = (t && t->kind == TY_NAMED) ? t->under : t;
return (u && u->kind == TY_TUPLE) ? u : NULL;
}
/* #83: positional tuple register-return ABI. Tuple elements ride
* consecutive eightbytes over tuple_rseq[]; a slice/str rides its 3-word
* {ptr,len,cap} header (ref/hare/rt/ensure.ha:4-8, ty_str->size SSoT), a
@@ -5411,6 +5438,7 @@ cgexpr(Cg *c, Node *n, Local *locals)
continue;
}
cgexpr(c, args[i], locals);
Type *tuparg_push = node_tuplearg(args[i]);
if (node_isfloat(args[i])) {
/* f32 spills 4B (MOVSS), f64 8B (MOVSD): the SysV
* float class drives the width per ref/qbe
@@ -5449,6 +5477,72 @@ cgexpr(Cg *c, Node *n, Local *locals)
if (sz > 8)
ins1(c, A_PUSHQ, areg(D_DX));
ins1(c, A_PUSHQ, areg(D_AX));
} else if (tuparg_push) {
/* #163: tuple ARG (param twin of #164's return).
* cgexpr above left the tuple in the return-ABI
* cursor; restage it into @tupargscr by SysV class
* (tuple_store, the #164 helper), then push the slot
* words high→low so the pop drains slot+0 first into
* the ARG cursor. The frame slot decouples the
* return-class regs (AX/DX/CX/R8 + X0/X1) from the
* overlapping arg-class regs (DI/SI/.. + X0..X7). */
int gpcur = 0, ssecur = 0, eoff = 0, ef32;
int gptot = 0, sstot = 0, tsz = 0;
for (Tparam *p = tuparg_push->params; p; p = p->next) {
Type *pu = (p->type
&& p->type->kind == TY_NAMED)
? p->type->under : p->type;
int wide = pu && (pu->kind == TY_SLICE
|| pu->kind == TY_STR);
if (fld_isfloat(p->type, &ef32))
sstot++;
else
gptot += tuple_ebytes(wide);
/* slot stride per element (sum == tuple slot
* size); matches the wwstage slotsize() walk so
* the @tupargscr width + reverse-push count agree
* byte-for-byte. */
tsz += wide ? (int)pu->size : 8;
}
/* The producing call already satisfied #164's
* return caps; guard anyway (tuple_store indexes
* tuple_rseq[4] / tuple_sse_seq[2]). */
if (gptot > (int)(sizeof tuple_rseq
/ sizeof tuple_rseq[0])
|| sstot > (int)(sizeof tuple_sse_seq
/ sizeof tuple_sse_seq[0]))
fatal("tuple arg exceeds return-cursor ABI "
"capacity; see #163/#164");
if (cg_tupargscr == 0) {
cg_tupargscr = local_alloc(c, &locals,
"@tupargscr", tsz, cg_frame);
cg_tupargscr_sz = tsz;
} else if (tsz > cg_tupargscr_sz) {
fatal("cgcall: @tupargscr cached sz %d, "
"need %d (pinned offset can't grow; "
"#163)", cg_tupargscr_sz, tsz);
}
for (Tparam *p = tuparg_push->params; p; p = p->next) {
Type *pu = (p->type
&& p->type->kind == TY_NAMED)
? p->type->under : p->type;
int wide = pu && (pu->kind == TY_SLICE
|| pu->kind == TY_STR);
int isflt = fld_isfloat(p->type, &ef32);
tuple_store(c, p->type, wide, gpcur, ssecur,
cg_tupargscr + eoff);
if (isflt)
ssecur++;
else
gpcur += tuple_ebytes(wide);
eoff += wide ? (int)pu->size : 8;
}
for (int w = tsz - 8; w >= 0; w -= 8) {
ins2(c, A_MOVQ,
amem(D_BP, cg_tupargscr + w),
areg(D_AX));
ins1(c, A_PUSHQ, areg(D_AX));
}
} else {
ins1(c, A_PUSHQ, areg(D_AX));
}
@@ -5505,6 +5599,7 @@ cgexpr(Cg *c, Node *n, Local *locals)
* the callee via positive offsets from BP. The caller is
* responsible for cleaning them up after CALL. */
int ii = (sret_call_sz > 0) ? 1 : 0, fi = 0, stackslots = 0;
Type *tu;
for (int i = 0; i < argcount; i++) {
if (widen[i]) {
/* Pop widened tagged slot into arg-register
@@ -5567,6 +5662,47 @@ cgexpr(Cg *c, Node *n, Local *locals)
else
stackslots++;
}
} else if ((tu = node_tuplearg(args[i])) != NULL) {
/* #163: drain the tuple's staged words (pushed
* slot+0 first) into the SysV arg cursor by SysV
* class — a float MOVSD/MOVSS off (SP) into the
* next XMM (X0..X7), everything else POPQ into the
* next INTEGER arg reg (DI/SI/..); a slice/str its
* 3-word {ptr,len,cap}. Reg overflow loud-stops
* (rule 7): the partial-spill stitch is out of
* scope (twin of #164's cap). */
int ef32;
for (Tparam *p = tu->params; p; p = p->next) {
Type *pu = (p->type
&& p->type->kind == TY_NAMED)
? p->type->under : p->type;
int wide = pu && (pu->kind == TY_SLICE
|| pu->kind == TY_STR);
if (fld_isfloat(p->type, &ef32)) {
if (fi >= 8)
fatal("tuple arg float "
"element overflows SSE "
"arg regs (X0..X7); "
"stitch out of scope, "
"see #163");
ins2(c, ef32 ? A_MOVSS : A_MOVSD,
amem(D_SP, 0),
areg(sysv_fargregs[fi]));
ins2(c, A_ADDQ, aimm(8),
areg(D_SP));
fi++;
continue;
}
int eb = tuple_ebytes(wide);
if (ii + eb > 6)
fatal("tuple arg element "
"overflows integer arg regs "
"(DI/SI/DX/CX/R8/R9); stitch "
"out of scope, see #163");
for (int k = 0; k < eb; k++)
ins1(c, A_POPQ,
areg(sysv_argregs[ii++]));
}
} else {
if (ii < 6) {
ins1(c, A_POPQ, areg(sysv_argregs[ii]));
@@ -8513,6 +8649,8 @@ cgfn(Cg *c, FILE *out, Node *fn)
cg_ret_type = fn->type ? fn->type->ret : NULL;
cg_retscr = 0;
cg_tupfscr = 0;
cg_tupargscr = 0;
cg_tupargscr_sz = 0;
cg_tagbase = 0;
cg_tagbase_sz = 0;
cg_tagscr = 0;
@@ -8575,6 +8713,55 @@ cgfn(Cg *c, FILE *out, Node *fn)
int is_tagged = tagged_sz > 0;
int isf = cg_isfloat(pt);
/* #163: tuple PARAM receive (param twin of #164's return).
* Walk the tuple's elements over the SysV arg cursor — a float
* reads its XMM (X0..X7), everything else an INTEGER arg reg
* (DI/SI/..); a slice/str its 3-word {ptr,len,cap} header — and
* store each into the param's frame slot positionally (eoff
* steps by the element's slot width: a slice/str 24B, else 8B,
* matching the tuple-field-access offset walk + the SEND). Reg
* overflow loud-stops (rule 7), the partial-spill stitch out of
* scope (twin of #164's cap). Placed before the single-class
* eightbytes logic below, which can't model a mixed GP/SSE
* aggregate. */
if (pu && pu->kind == TY_TUPLE) {
int sz = (int)pu->size;
int off = localoff(c, &locals, p->str, sz, &frame);
int eoff = 0, ef32;
for (Tparam *te = pu->params; te; te = te->next) {
Type *teu = (te->type
&& te->type->kind == TY_NAMED)
? te->type->under : te->type;
int wide = teu && (teu->kind == TY_SLICE
|| teu->kind == TY_STR);
if (fld_isfloat(te->type, &ef32)) {
if (fargi >= 8)
fatal("tuple param float element "
"overflows SSE arg regs "
"(X0..X7); stitch out of "
"scope, see #163");
ins2(c, ef32 ? A_MOVSS : A_MOVSD,
areg(sysv_fargregs[fargi]),
amem(D_BP, off + eoff));
fargi++;
eoff += 8;
continue;
}
int eb = tuple_ebytes(wide);
if (argi + eb > 6)
fatal("tuple param element overflows "
"integer arg regs (DI/SI/DX/CX/R8/"
"R9); stitch out of scope, see #163");
for (int k = 0; k < eb; k++, argi++)
ins2(c, A_MOVQ,
areg(sysv_argregs[argi]),
amem(D_BP, off + eoff + k * 8));
eoff += wide ? (int)teu->size : 8;
}
if (tp) tp = tp->next;
continue;
}
/* Args overflowing register classes live at positive offsets
* from BP (16 + i*8). We register them as Locals at those
* offsets, no spill needed. */

View File

@@ -14312,6 +14312,59 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
return rest + 1;
};
cgexpr(c, arg);
// #163: tuple ARG (param twin of #164's return). cgexpr left the
// tuple in the return-ABI cursor (AX/DX/CX/R8 + X0/X1); restage it
// into @tupargscr by SysV class (tupstore, the #164 helper) and push
// the slot words high->low so the pop drains slot+0 first into the
// SysV ARG cursor. The frame slot decouples the return-class regs
// from the overlapping arg-class regs. rettupleof scopes to an
// N_CALL producer (tuple idents/literals as values are a separate
// unimplemented gap; the SEND never pushes stale regs, rule 7).
let tuparg: *node = rettupleof(c, arg);
if (tuparg != nil) {
let gptot: i32 = 0;
let sstot: i32 = 0;
let tsz: i32 = 0;
let p: *node = tuparg.list;
for (p != nil) {
let et: *node = p.lhs;
let wide: bool = isstrtype(c, et) || isslicetype(c, et);
if (isfloattype(c, et)) { sstot += 1; }
else { gptot += tupebytes(wide); };
tsz += slotsize(c, et);
p = p.next;
};
// The producing call already satisfied #164's return caps;
// guard anyway (tupstore indexes [AX,DX,CX,R8] / [X0,X1]).
if (gptot > 4 || sstot > 2) {
let msg: str = "tuple arg exceeds return-cursor ABI capacity; see #163/#164\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let scr: i32 = localadd(c, "@tupargscr", tsz, nil);
let gpcur: i32 = 0;
let ssecur: i32 = 0;
let eoff: i32 = 0;
p = tuparg.list;
for (p != nil) {
let et: *node = p.lhs;
let wide: bool = isstrtype(c, et) || isslicetype(c, et);
tupstore(c, gpcur, ssecur, scr + eoff, wide, et);
if (isfloattype(c, et)) { ssecur += 1; }
else { gpcur += tupebytes(wide); };
eoff += slotsize(c, et);
p = p.next;
};
let w: i32 = tsz - 8;
for (w >= 0) {
emitline("\tMOVQ\t");
emitoff((scr + w): i64);
emitline("(BP), AX\n");
emitline("\tPUSHQ\tAX\n");
w -= 8;
};
return rest + tsz / 8;
};
if (nodeisslice(c, arg)) {
emitline("\tPUSHQ\tCX\n");
emitline("\tPUSHQ\tBX\n");
@@ -21091,7 +21144,54 @@ fn cgcall(c: *cgen, n: *node) void = {
stackslots += 1;
};
popped += 1;
} else {
} else { let tuparg: *node = rettupleof(c, a);
if (tuparg != nil) {
// #163: drain the tuple's staged words (slot+0 pushed
// first) into the SysV arg cursor by SysV class — a
// float MOVSD/MOVSS off (SP) into the next XMM, else
// POPQ into the next INTEGER arg reg; a slice/str its
// 3 words. Reg overflow loud-stops (rule 7); the
// partial-spill stitch is out of scope (twin of #164).
let p: *node = tuparg.list;
for (p != nil) {
let et: *node = p.lhs;
if (isfloattype(c, et)) {
if (fpidx >= 8) {
let msg: str = "tuple arg float element overflows SSE arg regs (X0..X7); stitch out of scope, see #163\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let mov: str = "MOVSD";
if (isf32type(c, et)) { mov = "MOVSS"; };
emitline("\t");
emitline(mov);
emitline("\t(SP), ");
emitline(fargregname(fpidx));
emitline("\n");
emitline("\tADDQ\t$8, SP\n");
fpidx += 1;
popped += 1;
} else {
let wide: bool = isstrtype(c, et) || isslicetype(c, et);
let eb: i32 = tupebytes(wide);
if (intidx + eb > 6) {
let msg: str = "tuple arg element overflows integer arg regs (DI/SI/DX/CX/R8/R9); stitch out of scope, see #163\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let k: i32 = 0;
for (k < eb) {
emitline("\tPOPQ\t");
emitline(argregname(intidx));
emitline("\n");
intidx += 1;
popped += 1;
k += 1;
};
};
p = p.next;
};
} else {
let extra: i32 = 0;
// str IS []u8: 3-word arg, same as slice (#1/Phase 3).
if (nodeisstr(c, a)) { extra = 2; };
@@ -21116,6 +21216,7 @@ fn cgcall(c: *cgen, n: *node) void = {
popped += 1;
w += 1;
};
};
};
a = a.next;
};
@@ -26180,6 +26281,62 @@ fn cgfnparams(c: *cgen, params: *node) void = {
p = p.next;
continue;
};
if (p.lhs != nil) { if (p.lhs.kind == nkind.N_TTUPLE) {
// #163: tuple PARAM receive (param twin of #164's
// return). Walk the tuple's elements over the SysV
// arg cursor — a float reads its XMM (X0..X7),
// everything else an INTEGER arg reg (DI/SI/..); a
// slice/str its 3-word {ptr,len,cap} — storing each
// into the param slot positionally (eoff steps by
// slotsize, matching the t.0/t.1 field-access walk +
// the SEND). Reg overflow loud-stops (rule 7); the
// partial-spill stitch is out of scope (twin of #164).
let off: i32 = localadd(c, nm, slotsize(c, p.lhs), p.lhs);
let eoff: i32 = 0;
let te: *node = p.lhs.list;
for (te != nil) {
let et: *node = te.lhs;
if (isfloattype(c, et)) {
if (fidx >= 8) {
let msg: str = "tuple param float element overflows SSE arg regs (X0..X7); stitch out of scope, see #163\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let mov: str = "MOVSD";
if (isf32type(c, et)) { mov = "MOVSS"; };
emitline("\t");
emitline(mov);
emitline("\t");
emitline(fargregname(fidx));
emitline(", ");
emitoff((off + eoff): i64);
emitline("(BP)\n");
fidx += 1;
} else {
let wide: bool = isstrtype(c, et) || isslicetype(c, et);
let eb: i32 = tupebytes(wide);
if (idx + eb > 6) {
let msg: str = "tuple param element overflows integer arg regs (DI/SI/DX/CX/R8/R9); stitch out of scope, see #163\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let k: i32 = 0;
for (k < eb) {
emitline("\tMOVQ\t");
emitline(argregname(idx));
emitline(", ");
emitoff((off + eoff + k*8): i64);
emitline("(BP)\n");
idx += 1;
k += 1;
};
};
eoff += slotsize(c, et);
te = te.next;
};
p = p.next;
continue;
}; };
if (isfloattype(c, p.lhs)) {
// Float param: SysV uses the XMM stream
// (X0..X7). 8B (f64) or 4B (f32) slot.

View File

@@ -100,6 +100,62 @@ fn cgfnparams(c: *cgen, params: *node) void = {
p = p.next;
continue;
};
if (p.lhs != nil) { if (p.lhs.kind == nkind.N_TTUPLE) {
// #163: tuple PARAM receive (param twin of #164's
// return). Walk the tuple's elements over the SysV
// arg cursor — a float reads its XMM (X0..X7),
// everything else an INTEGER arg reg (DI/SI/..); a
// slice/str its 3-word {ptr,len,cap} — storing each
// into the param slot positionally (eoff steps by
// slotsize, matching the t.0/t.1 field-access walk +
// the SEND). Reg overflow loud-stops (rule 7); the
// partial-spill stitch is out of scope (twin of #164).
let off: i32 = localadd(c, nm, slotsize(c, p.lhs), p.lhs);
let eoff: i32 = 0;
let te: *node = p.lhs.list;
for (te != nil) {
let et: *node = te.lhs;
if (isfloattype(c, et)) {
if (fidx >= 8) {
let msg: str = "tuple param float element overflows SSE arg regs (X0..X7); stitch out of scope, see #163\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let mov: str = "MOVSD";
if (isf32type(c, et)) { mov = "MOVSS"; };
emitline("\t");
emitline(mov);
emitline("\t");
emitline(fargregname(fidx));
emitline(", ");
emitoff((off + eoff): i64);
emitline("(BP)\n");
fidx += 1;
} else {
let wide: bool = isstrtype(c, et) || isslicetype(c, et);
let eb: i32 = tupebytes(wide);
if (idx + eb > 6) {
let msg: str = "tuple param element overflows integer arg regs (DI/SI/DX/CX/R8/R9); stitch out of scope, see #163\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let k: i32 = 0;
for (k < eb) {
emitline("\tMOVQ\t");
emitline(argregname(idx));
emitline(", ");
emitoff((off + eoff + k*8): i64);
emitline("(BP)\n");
idx += 1;
k += 1;
};
};
eoff += slotsize(c, et);
te = te.next;
};
p = p.next;
continue;
}; };
if (isfloattype(c, p.lhs)) {
// Float param: SysV uses the XMM stream
// (X0..X7). 8B (f64) or 4B (f32) slot.

View File

@@ -3932,7 +3932,54 @@ fn cgcall(c: *cgen, n: *node) void = {
stackslots += 1;
};
popped += 1;
} else {
} else { let tuparg: *node = rettupleof(c, a);
if (tuparg != nil) {
// #163: drain the tuple's staged words (slot+0 pushed
// first) into the SysV arg cursor by SysV class — a
// float MOVSD/MOVSS off (SP) into the next XMM, else
// POPQ into the next INTEGER arg reg; a slice/str its
// 3 words. Reg overflow loud-stops (rule 7); the
// partial-spill stitch is out of scope (twin of #164).
let p: *node = tuparg.list;
for (p != nil) {
let et: *node = p.lhs;
if (isfloattype(c, et)) {
if (fpidx >= 8) {
let msg: str = "tuple arg float element overflows SSE arg regs (X0..X7); stitch out of scope, see #163\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let mov: str = "MOVSD";
if (isf32type(c, et)) { mov = "MOVSS"; };
emitline("\t");
emitline(mov);
emitline("\t(SP), ");
emitline(fargregname(fpidx));
emitline("\n");
emitline("\tADDQ\t$8, SP\n");
fpidx += 1;
popped += 1;
} else {
let wide: bool = isstrtype(c, et) || isslicetype(c, et);
let eb: i32 = tupebytes(wide);
if (intidx + eb > 6) {
let msg: str = "tuple arg element overflows integer arg regs (DI/SI/DX/CX/R8/R9); stitch out of scope, see #163\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let k: i32 = 0;
for (k < eb) {
emitline("\tPOPQ\t");
emitline(argregname(intidx));
emitline("\n");
intidx += 1;
popped += 1;
k += 1;
};
};
p = p.next;
};
} else {
let extra: i32 = 0;
// str IS []u8: 3-word arg, same as slice (#1/Phase 3).
if (nodeisstr(c, a)) { extra = 2; };
@@ -3957,6 +4004,7 @@ fn cgcall(c: *cgen, n: *node) void = {
popped += 1;
w += 1;
};
};
};
a = a.next;
};

View File

@@ -490,6 +490,59 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
return rest + 1;
};
cgexpr(c, arg);
// #163: tuple ARG (param twin of #164's return). cgexpr left the
// tuple in the return-ABI cursor (AX/DX/CX/R8 + X0/X1); restage it
// into @tupargscr by SysV class (tupstore, the #164 helper) and push
// the slot words high->low so the pop drains slot+0 first into the
// SysV ARG cursor. The frame slot decouples the return-class regs
// from the overlapping arg-class regs. rettupleof scopes to an
// N_CALL producer (tuple idents/literals as values are a separate
// unimplemented gap; the SEND never pushes stale regs, rule 7).
let tuparg: *node = rettupleof(c, arg);
if (tuparg != nil) {
let gptot: i32 = 0;
let sstot: i32 = 0;
let tsz: i32 = 0;
let p: *node = tuparg.list;
for (p != nil) {
let et: *node = p.lhs;
let wide: bool = isstrtype(c, et) || isslicetype(c, et);
if (isfloattype(c, et)) { sstot += 1; }
else { gptot += tupebytes(wide); };
tsz += slotsize(c, et);
p = p.next;
};
// The producing call already satisfied #164's return caps;
// guard anyway (tupstore indexes [AX,DX,CX,R8] / [X0,X1]).
if (gptot > 4 || sstot > 2) {
let msg: str = "tuple arg exceeds return-cursor ABI capacity; see #163/#164\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let scr: i32 = localadd(c, "@tupargscr", tsz, nil);
let gpcur: i32 = 0;
let ssecur: i32 = 0;
let eoff: i32 = 0;
p = tuparg.list;
for (p != nil) {
let et: *node = p.lhs;
let wide: bool = isstrtype(c, et) || isslicetype(c, et);
tupstore(c, gpcur, ssecur, scr + eoff, wide, et);
if (isfloattype(c, et)) { ssecur += 1; }
else { gpcur += tupebytes(wide); };
eoff += slotsize(c, et);
p = p.next;
};
let w: i32 = tsz - 8;
for (w >= 0) {
emitline("\tMOVQ\t");
emitoff((scr + w): i64);
emitline("(BP), AX\n");
emitline("\tPUSHQ\tAX\n");
w -= 8;
};
return rest + tsz / 8;
};
if (nodeisslice(c, arg)) {
emitline("\tPUSHQ\tCX\n");
emitline("\tPUSHQ\tBX\n");

View File

@@ -14312,6 +14312,59 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
return rest + 1;
};
cgexpr(c, arg);
// #163: tuple ARG (param twin of #164's return). cgexpr left the
// tuple in the return-ABI cursor (AX/DX/CX/R8 + X0/X1); restage it
// into @tupargscr by SysV class (tupstore, the #164 helper) and push
// the slot words high->low so the pop drains slot+0 first into the
// SysV ARG cursor. The frame slot decouples the return-class regs
// from the overlapping arg-class regs. rettupleof scopes to an
// N_CALL producer (tuple idents/literals as values are a separate
// unimplemented gap; the SEND never pushes stale regs, rule 7).
let tuparg: *node = rettupleof(c, arg);
if (tuparg != nil) {
let gptot: i32 = 0;
let sstot: i32 = 0;
let tsz: i32 = 0;
let p: *node = tuparg.list;
for (p != nil) {
let et: *node = p.lhs;
let wide: bool = isstrtype(c, et) || isslicetype(c, et);
if (isfloattype(c, et)) { sstot += 1; }
else { gptot += tupebytes(wide); };
tsz += slotsize(c, et);
p = p.next;
};
// The producing call already satisfied #164's return caps;
// guard anyway (tupstore indexes [AX,DX,CX,R8] / [X0,X1]).
if (gptot > 4 || sstot > 2) {
let msg: str = "tuple arg exceeds return-cursor ABI capacity; see #163/#164\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let scr: i32 = localadd(c, "@tupargscr", tsz, nil);
let gpcur: i32 = 0;
let ssecur: i32 = 0;
let eoff: i32 = 0;
p = tuparg.list;
for (p != nil) {
let et: *node = p.lhs;
let wide: bool = isstrtype(c, et) || isslicetype(c, et);
tupstore(c, gpcur, ssecur, scr + eoff, wide, et);
if (isfloattype(c, et)) { ssecur += 1; }
else { gpcur += tupebytes(wide); };
eoff += slotsize(c, et);
p = p.next;
};
let w: i32 = tsz - 8;
for (w >= 0) {
emitline("\tMOVQ\t");
emitoff((scr + w): i64);
emitline("(BP), AX\n");
emitline("\tPUSHQ\tAX\n");
w -= 8;
};
return rest + tsz / 8;
};
if (nodeisslice(c, arg)) {
emitline("\tPUSHQ\tCX\n");
emitline("\tPUSHQ\tBX\n");
@@ -21091,7 +21144,54 @@ fn cgcall(c: *cgen, n: *node) void = {
stackslots += 1;
};
popped += 1;
} else {
} else { let tuparg: *node = rettupleof(c, a);
if (tuparg != nil) {
// #163: drain the tuple's staged words (slot+0 pushed
// first) into the SysV arg cursor by SysV class — a
// float MOVSD/MOVSS off (SP) into the next XMM, else
// POPQ into the next INTEGER arg reg; a slice/str its
// 3 words. Reg overflow loud-stops (rule 7); the
// partial-spill stitch is out of scope (twin of #164).
let p: *node = tuparg.list;
for (p != nil) {
let et: *node = p.lhs;
if (isfloattype(c, et)) {
if (fpidx >= 8) {
let msg: str = "tuple arg float element overflows SSE arg regs (X0..X7); stitch out of scope, see #163\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let mov: str = "MOVSD";
if (isf32type(c, et)) { mov = "MOVSS"; };
emitline("\t");
emitline(mov);
emitline("\t(SP), ");
emitline(fargregname(fpidx));
emitline("\n");
emitline("\tADDQ\t$8, SP\n");
fpidx += 1;
popped += 1;
} else {
let wide: bool = isstrtype(c, et) || isslicetype(c, et);
let eb: i32 = tupebytes(wide);
if (intidx + eb > 6) {
let msg: str = "tuple arg element overflows integer arg regs (DI/SI/DX/CX/R8/R9); stitch out of scope, see #163\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let k: i32 = 0;
for (k < eb) {
emitline("\tPOPQ\t");
emitline(argregname(intidx));
emitline("\n");
intidx += 1;
popped += 1;
k += 1;
};
};
p = p.next;
};
} else {
let extra: i32 = 0;
// str IS []u8: 3-word arg, same as slice (#1/Phase 3).
if (nodeisstr(c, a)) { extra = 2; };
@@ -21116,6 +21216,7 @@ fn cgcall(c: *cgen, n: *node) void = {
popped += 1;
w += 1;
};
};
};
a = a.next;
};
@@ -26180,6 +26281,62 @@ fn cgfnparams(c: *cgen, params: *node) void = {
p = p.next;
continue;
};
if (p.lhs != nil) { if (p.lhs.kind == nkind.N_TTUPLE) {
// #163: tuple PARAM receive (param twin of #164's
// return). Walk the tuple's elements over the SysV
// arg cursor — a float reads its XMM (X0..X7),
// everything else an INTEGER arg reg (DI/SI/..); a
// slice/str its 3-word {ptr,len,cap} — storing each
// into the param slot positionally (eoff steps by
// slotsize, matching the t.0/t.1 field-access walk +
// the SEND). Reg overflow loud-stops (rule 7); the
// partial-spill stitch is out of scope (twin of #164).
let off: i32 = localadd(c, nm, slotsize(c, p.lhs), p.lhs);
let eoff: i32 = 0;
let te: *node = p.lhs.list;
for (te != nil) {
let et: *node = te.lhs;
if (isfloattype(c, et)) {
if (fidx >= 8) {
let msg: str = "tuple param float element overflows SSE arg regs (X0..X7); stitch out of scope, see #163\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let mov: str = "MOVSD";
if (isf32type(c, et)) { mov = "MOVSS"; };
emitline("\t");
emitline(mov);
emitline("\t");
emitline(fargregname(fidx));
emitline(", ");
emitoff((off + eoff): i64);
emitline("(BP)\n");
fidx += 1;
} else {
let wide: bool = isstrtype(c, et) || isslicetype(c, et);
let eb: i32 = tupebytes(wide);
if (idx + eb > 6) {
let msg: str = "tuple param element overflows integer arg regs (DI/SI/DX/CX/R8/R9); stitch out of scope, see #163\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
let k: i32 = 0;
for (k < eb) {
emitline("\tMOVQ\t");
emitline(argregname(idx));
emitline(", ");
emitoff((off + eoff + k*8): i64);
emitline("(BP)\n");
idx += 1;
k += 1;
};
};
eoff += slotsize(c, et);
te = te.next;
};
p = p.next;
continue;
}; };
if (isfloattype(c, p.lhs)) {
// Float param: SysV uses the XMM stream
// (X0..X7). 8B (f64) or 4B (f32) slot.

319
test/wcc/905_tupparam_run.c Normal file
View File

@@ -0,0 +1,319 @@
/*
* 905_tupparam_run — runtime + byte-id net for #163, the tuple-PARAM ABI
* (the param twin of #164's tuple RETURN).
*
* THE BUG (#163, LIVE drop on master): a tuple passed AS AN ARGUMENT was
* unhandled in BOTH stages — no tuple arm in the cgcall arg push, the
* cgcall arg pop, OR the callee cgfnparams receive. A tuple-typed call
* result (`f(g())` where g returns a tuple) left its elements in the
* return-ABI cursor (AX/DX/CX/R8 + X0/X1, per #164); the SEND fell to the
* 1-GP-word `else` (PUSHQ AX / POPQ DI) so ALL BUT THE FIRST ELEMENT was
* dropped, and the callee read its tuple param as a single GP word. This
* broke INTEGER tuple params too; float elements were doubly lost (they
* ride X0/X1, never AX).
*
* THE FIX: per-element SysV class placement reusing #164's helper. SEND —
* cgexpr leaves the tuple in the return cursor; restage it into a frame
* slot (@tupargscr) by class via tuple_store/tupstore, then push the slot
* words high->low so the pop drains slot+0 first into the SysV ARG cursor
* (DI/SI/.. + X0..X7). The frame slot decouples the return-class regs
* (which OVERLAP the arg-class regs) from the arg placement. RECV — the
* callee walks the tuple's elements over the arg cursor, storing each into
* its frame slot positionally. Symmetric across cstage (cmd/w6c/cgen.c)
* and wwstage (cgenutil.ww pushargsrev + cgenexpr.ww cgcall pop +
* cgendecl.ww cgfnparams).
*
* SCOPE: register-class tuple ARGS produced by a CALL (the only form that
* materialises a tuple value today — `let t = (1,2)` as a first-class
* value is a separate unimplemented gap, so the SEND scopes to the N_CALL
* producer and never pushes stale regs, rule 7). Arg-register overflow
* loud-stops (the partial-spill stitch is out of scope, twin of #164's
* cap); the loudstop row asserts BOTH stages ERROR.
*
* GATE-BLIND TO BYTE-ID ALONE: the bootstrap passes no tuple params, and
* pre-fix both stages were symmetric-WRONG (both PUSHQ AX), so the cs==ww
* .s gate HOLDS on master for the value rows — they diverge only at
* RUNTIME. Each value row carries BOTH dimensions (modelled on 956):
* (a) cstage `ww build` + run, asserting the exit code (catches #163:
* master returns the wrong exit / dropped element).
* (b) w6c vs w6c_ww `.s` cmp (rule-10: both stages fixed identically).
*/
#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;
int want_compile_fail; /* loud-stop rows must NOT compile */
};
static const struct row rows[] = {
/* HEADLINE — (f64, f64) arg. Pre-fix the SEND pushes only AX (the
* two floats stay stranded in X0/X1) and the callee reads one GP
* word; t.0+t.1 != 8.0 -> return 1. Post-fix each float rides the
* SSE arg cursor (X0,X1). */
{ "f64f64_arg",
"package main;\n"
"fn pair(a: f64, b: f64) (f64, f64) = { return (a, b); };\n"
"fn add(t: (f64, f64)) f64 = { return t.0 + t.1; };\n"
"export fn main() i32 = {\n"
"\tif (add(pair(3.0, 5.0)) != 8.0) { return 1; };\n"
"\treturn 0;\n"
"};\n", 0 },
/* (i64, i64) arg — proves the broader INTEGER-tuple-param drop is
* fixed (master dropped the second i64 too). e0->DI, e1->SI. */
{ "i64i64_arg",
"package main;\n"
"fn pair(a: i64, b: i64) (i64, i64) = { return (a, b); };\n"
"fn add(t: (i64, i64)) i64 = { return t.0 + t.1; };\n"
"export fn main() i32 = {\n"
"\tif (add(pair(3, 5)) != 8) { return 1; };\n"
"\treturn 0;\n"
"};\n", 0 },
/* (f64, i64) — class independent of position: f64@X0 (SSE cursor),
* i64@DI (INTEGER cursor), independent counters. */
{ "f64i64_arg",
"package main;\n"
"fn mk(a: f64, b: i64) (f64, i64) = { return (a, b); };\n"
"fn add(t: (f64, i64)) i64 = { return (t.0: i64) + t.1; };\n"
"export fn main() i32 = {\n"
"\tif (add(mk(3.0, 5)) != 8) { return 1; };\n"
"\treturn 0;\n"
"};\n", 0 },
/* (i64, f64) — order-swap: i64@DI, f64@X0. Confirms the float lands
* in the next XMM regardless of its positional slot. */
{ "i64f64_arg",
"package main;\n"
"fn mk(a: i64, b: f64) (i64, f64) = { return (a, b); };\n"
"fn add(t: (i64, f64)) i64 = { return t.0 + (t.1: i64); };\n"
"export fn main() i32 = {\n"
"\tif (add(mk(3, 5.0)) != 8) { return 1; };\n"
"\treturn 0;\n"
"};\n", 0 },
/* (f64, str) — SSE + wide (24B {ptr,len,cap}) coexist. The f64
* rides X0 (SSE, consuming no GP slot); the str rides DI/SI/DX
* (INTEGER cursor). f=4.0, s.len=5 -> 4+5 = 9. */
{ "f64str_arg",
"package main;\n"
"fn mk(a: f64) (f64, str) = { return (a, \"hello\"); };\n"
"fn add(t: (f64, str)) i64 = {\n"
"\treturn (t.0: i64) + (t.1.len: i64);\n"
"};\n"
"export fn main() i32 = {\n"
"\tif (add(mk(4.0)) != 9) { return 1; };\n"
"\treturn 0;\n"
"};\n", 0 },
/* MULTI-TUPLE-ARG, ONE CALL — f(g(), h()) where BOTH args are
* tuple-producing calls. Proves @tupargscr (single-slot-per-fn) is
* REUSED per arg, not COLLIDED: pushargsrev evals right-to-left, so
* h() restages into the slot + drains it to the stack BEFORE g()
* restages into the SAME slot (h's words already pushed, safe to
* overwrite). Drain forward: s->DI,SI; t->DX,CX. 1+2+3+4 = 10. A
* collision (both restaged before either pushed) would corrupt the
* first-pushed tuple's words. */
{ "two_tuple_args",
"package main;\n"
"fn pair(a: i64, b: i64) (i64, i64) = { return (a, b); };\n"
"fn add4(s: (i64, i64), t: (i64, i64)) i64 = {\n"
"\treturn s.0 + s.1 + t.0 + t.1;\n"
"};\n"
"export fn main() i32 = {\n"
"\tif (add4(pair(1, 2), pair(3, 4)) != 10) { return 1; };\n"
"\treturn 0;\n"
"};\n", 0 },
/* CONTROL — a tuple arg threaded through a chain of two calls,
* proving the SEND/RECV round-trips through the slot intact. */
{ "f64f64_chain",
"package main;\n"
"fn pair(a: f64, b: f64) (f64, f64) = { return (a, b); };\n"
"fn id(t: (f64, f64)) f64 = { return t.0 * 10.0 + t.1; };\n"
"export fn main() i32 = {\n"
"\tif (id(pair(3.0, 5.0)) != 35.0) { return 1; };\n"
"\treturn 0;\n"
"};\n", 0 },
/* LOUD-STOP — 5 i64 scalars + an (i64,i64) tuple = 7 INTEGER arg
* eightbytes, overflowing the 6 GP arg regs (DI/SI/DX/CX/R8/R9).
* The partial-spill stitch is out of scope (twin of #164's cap), so
* BOTH stages must FAIL TO COMPILE (rule 7: surface, never silently
* drop). Master has no tuple-arg arm (pushes the tuple as 1 word ->
* 6 GP, no overflow) and builds the miscompile, so want_compile_fail
* discriminates. */
{ "gp_overflow_loudstop",
"package main;\n"
"fn pair(a: i64, b: i64) (i64, i64) = { return (a, b); };\n"
"fn f(a: i64, b: i64, c: i64, d: i64, e: i64, t: (i64, i64)) i64 = {\n"
"\treturn a + b + c + d + e + t.0 + t.1;\n"
"};\n"
"export fn main() i32 = {\n"
"\treturn (f(1, 2, 3, 4, 5, pair(6, 7)): i32);\n"
"};\n", 0, 1 },
{ NULL, NULL, 0, 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, "tupparam: 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/wwtupp_%d_%d.ww", getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; continue; }
fputs(rows[i].src, f);
fclose(f);
char cmd[2048];
/* LOUD-STOP rows: the arg-reg overflow must FAIL TO COMPILE in
* BOTH stages (rule 7). Assert (a) cstage `ww build` errors and
* (b) w6c AND w6c_ww each return non-zero. No .s is produced, so
* the byte-id cmp is skipped. */
if (rows[i].want_compile_fail) {
char ldir[64];
snprintf(ldir, sizeof ldir, "/tmp/wwtupp_%d_l_%d",
getpid(), i);
mkdir(ldir, 0755);
snprintf(cmd, sizeof cmd,
"cd %s && %s/ww build %s >/dev/null 2>&1",
ldir, bin, src);
if (runwait(cmd) == 0) {
fprintf(stderr, "row[%s]: cstage build SUCCEEDED, "
"want loud-stop (arg-reg overflow)\n",
rows[i].label);
fail++;
}
snprintf(cmd, sizeof cmd, "%s -o /dev/null %s 2>/dev/null",
w6c, src);
if (runwait(cmd) == 0) {
fprintf(stderr, "row[%s]: w6c emitted .s, want "
"loud-stop\n", rows[i].label);
fail++;
}
snprintf(cmd, sizeof cmd, "%s -o /dev/null %s 2>/dev/null",
w6c_ww, src);
if (runwait(cmd) == 0) {
fprintf(stderr, "row[%s]: w6c_ww emitted .s, want "
"loud-stop\n", rows[i].label);
fail++;
}
unlink(src); rmdir(ldir);
continue;
}
/* (a) cstage build + run in a scratch dir. */
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwtupp_%d_d_%d",
getpid(), i);
mkdir(tmpdir, 0755);
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: emit .s from both stages, cmp. */
char cs_s[64], ws_s[64];
snprintf(cs_s, sizeof cs_s, "/tmp/wwtupp_%d_%d_cs.s",
getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/wwtupp_%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-param tests failed\n", fail, n);
return 1;
}
printf("tupparam: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n);
return 0;
}