#43 (8e93b31+087c85c) routed many sizeof(str) / sizeof(slice) sites through primtypesize / tyslicesize / ty_*->size, but missed the cgparam regs-fit, cgparam stack-stitch, cgmlet mixed scalar+str receive, and vararg slice gather paths in both stages. A bare #1 bump (str→24B) on top of #43 reds ~60 tests because those paths still hardcoded 16/24. Cstage: - cgen.c:7360-7361 cgmlet: sz0/sz1 → (int)u0->size / (int)u1->size. - cgen.c:7557 cgparam regs-fit: slice|is_str → (int)pu->size. - cgen.c:7586 cgparam stack-stitch: same. - cgen.c:4368 cgcall vararg gather: localoff slice descriptor → (int)vsu->size (the cstage twin of cgenexpr.ww:3084). Wwstage: - cgendecl.ww:225, :243 cgfnparams: 16 → primtypesize("str"): i32. - cgenexpr.ww:3084 cgcall vararg gather: 24 → tyslicesize(): i32. Plus a latent-bug fix at cgenstmt.ww cglet :1031 / :1040: the str-init and slice-init arms dispatched on size only. Under #1's str→24, both arms would have fired on a str let (duplicate MOVQ BX,off+8 + bogus MOVQ CX,off+8). Added isstrtype / isslicetype kind gates mirroring cstage cgen.c:6439's `type_isstr(lt) && sz == ty_str->size`. Zero asm change today because the size constants implicitly disambiguate at 16 vs 24. Probe with temporary #1 bump (str.size=24) confirms 990_selfhost + 994_w6c_ww go green — the cgen-routing slice for #1 is now closed. Remaining red under bump is lib/ww/typ.ww's parallel SSoT seed + stringstest cap*16u64 strides + w6l_ww runtime SIGSEGV; all tracked separately. EIGHTBYTES register-count sites (cgen.c:7553-7554, cgendecl.ww:224 /:260) intentionally NOT touched — those are str ABI in-flight 3-reg work (task #34), not slot-width SSoT.
458 lines
13 KiB
Plaintext
458 lines
13 KiB
Plaintext
// selfhost/cmd/wcc/cgendecl.ww — split out of cgen.ww.
|
|
//
|
|
// Houses the top-level emission glue:
|
|
// - cgfnparams: parameter spilling per SysV
|
|
// - cgfn: fn body emit (TEXT/SUBQ patched after body), prologue
|
|
// deferred via cgen.ww's cgoutbuf so the frame size
|
|
// reflects every emit-time localadd (#15/#26c)
|
|
// - cgfile: file-level entry (the exported driver)
|
|
//
|
|
// Bundler pulls this in transitively via cgen.ww; consumers don't
|
|
// need to `use cgendecl;` directly.
|
|
|
|
package wcc;
|
|
|
|
import os;
|
|
import mem;
|
|
import ast;
|
|
import tok;
|
|
import typ;
|
|
import sym;
|
|
import strconv;
|
|
|
|
|
|
// ---- function-level cgen ---------------------------------------------
|
|
|
|
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 (localfind(c, "@sretarg") != 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
|
|
// is registered with a *positive* offset pointing into the
|
|
// caller's frame. Mirrors C cgen's cg_stack_arg_cursor.
|
|
let stkcursor: i32 = 0;
|
|
for (p != nil) {
|
|
if (p.kind == nkind.N_PARAM) {
|
|
let nm: str = p.str;
|
|
// Hare-style variadic `T...`: callee receives a []T
|
|
// slice (3 register words / 24B). Mirror the slice-
|
|
// param spill below but use a synthesised TSLICE
|
|
// tnode so body references see the slot as a slice.
|
|
if (p.op == tkind.TK_ELLIPSIS) {
|
|
let tn: *node = slicewrap(c, p.lhs);
|
|
if (idx + 3 <= 6) {
|
|
let off: i32 = localadd(c, nm, tyslicesize(): i32, tn);
|
|
emitline("\tMOVQ\t");
|
|
emitline(argregname(idx));
|
|
emitline(", ");
|
|
emitoff(off: i64);
|
|
emitline("(BP)\n");
|
|
idx += 1;
|
|
emitline("\tMOVQ\t");
|
|
emitline(argregname(idx));
|
|
emitline(", ");
|
|
emitoff((off + 8): i64);
|
|
emitline("(BP)\n");
|
|
idx += 1;
|
|
emitline("\tMOVQ\t");
|
|
emitline(argregname(idx));
|
|
emitline(", ");
|
|
emitoff((off + 16): i64);
|
|
emitline("(BP)\n");
|
|
idx += 1;
|
|
} else { if (idx < 6) {
|
|
// Partial-fit stitch — variadic `T...` is a slice
|
|
// at the ABI boundary (the call site synthesises a
|
|
// 24B descriptor and pushes ptr/len/cap), so this
|
|
// mirrors the slice branch at cgendecl.ww:518.
|
|
let off: i32 = localadd(c, nm, tyslicesize(): i32, tn);
|
|
let regs_left: i32 = 6 - idx;
|
|
let w: i32 = 0;
|
|
for (w < regs_left) {
|
|
emitline("\tMOVQ\t");
|
|
emitline(argregname(idx));
|
|
emitline(", ");
|
|
emitoff((off + w*8): i64);
|
|
emitline("(BP)\n");
|
|
idx += 1;
|
|
w += 1;
|
|
};
|
|
for (w < 3) {
|
|
emitline("\tMOVQ\t");
|
|
emitoff((16 + stkcursor*8): i64);
|
|
emitline("(BP), AX\n");
|
|
emitline("\tMOVQ\tAX, ");
|
|
emitoff((off + w*8): i64);
|
|
emitline("(BP)\n");
|
|
stkcursor += 1;
|
|
w += 1;
|
|
};
|
|
} else {
|
|
localaddstack(c, nm, tn, 16 + stkcursor*8);
|
|
stkcursor += 3;
|
|
};};
|
|
p = p.next;
|
|
continue;
|
|
};
|
|
if (isfloattype(c, p.lhs)) {
|
|
// Float param: SysV uses the XMM stream
|
|
// (X0..X7). 8B (f64) or 4B (f32) slot.
|
|
let fsz: i32 = 8;
|
|
if (isf32type(c, p.lhs)) { fsz = 4; };
|
|
if (fidx < 8) {
|
|
let off: i32 = localadd(c, nm, fsz, p.lhs);
|
|
let mov: str = "MOVSD";
|
|
if (fsz == 4) { mov = "MOVSS"; };
|
|
emitline("\t");
|
|
emitline(mov);
|
|
emitline("\t");
|
|
emitline(fargregname(fidx));
|
|
emitline(", ");
|
|
emitoff(off: i64);
|
|
emitline("(BP)\n");
|
|
fidx += 1;
|
|
} else {
|
|
localaddstack(c, nm, p.lhs, 16 + stkcursor*8);
|
|
stkcursor += 1;
|
|
};
|
|
p = p.next;
|
|
continue;
|
|
};
|
|
if (istaggedtype(c, p.lhs)) {
|
|
let slot: i32 = slotsize(c, p.lhs);
|
|
let nw: i32 = slot / 8;
|
|
if (idx + nw <= 6) {
|
|
let off: i32 = localadd(c, nm, slot, p.lhs);
|
|
let w: i32 = 0;
|
|
for (w < nw) {
|
|
emitline("\tMOVQ\t");
|
|
emitline(argregname(idx));
|
|
emitline(", ");
|
|
emitoff((off + w*8): i64);
|
|
emitline("(BP)\n");
|
|
idx += 1;
|
|
w += 1;
|
|
};
|
|
} else { if (idx < 6 && nw > 1) {
|
|
// Partial fit: fill remaining regs, then read
|
|
// the tail from positive BP offsets. Mirrors
|
|
// the caller's greedy reg fill in pushargsrev.
|
|
let off: i32 = localadd(c, nm, slot, p.lhs);
|
|
let regs_left: i32 = 6 - idx;
|
|
let w: i32 = 0;
|
|
for (w < regs_left) {
|
|
emitline("\tMOVQ\t");
|
|
emitline(argregname(idx));
|
|
emitline(", ");
|
|
emitoff((off + w*8): i64);
|
|
emitline("(BP)\n");
|
|
idx += 1;
|
|
w += 1;
|
|
};
|
|
for (w < nw) {
|
|
emitline("\tMOVQ\t");
|
|
emitoff((16 + stkcursor*8): i64);
|
|
emitline("(BP), AX\n");
|
|
emitline("\tMOVQ\tAX, ");
|
|
emitoff((off + w*8): i64);
|
|
emitline("(BP)\n");
|
|
stkcursor += 1;
|
|
w += 1;
|
|
};
|
|
} else {
|
|
localaddstack(c, nm, p.lhs, 16 + stkcursor*8);
|
|
stkcursor += nw;
|
|
};};
|
|
} else { if (isslicetype(c, p.lhs)) {
|
|
if (idx + 3 <= 6) {
|
|
let off: i32 = localadd(c, nm, tyslicesize(): i32, p.lhs);
|
|
emitline("\tMOVQ\t");
|
|
emitline(argregname(idx));
|
|
emitline(", ");
|
|
emitoff(off: i64);
|
|
emitline("(BP)\n");
|
|
idx += 1;
|
|
emitline("\tMOVQ\t");
|
|
emitline(argregname(idx));
|
|
emitline(", ");
|
|
emitoff((off + 8): i64);
|
|
emitline("(BP)\n");
|
|
idx += 1;
|
|
emitline("\tMOVQ\t");
|
|
emitline(argregname(idx));
|
|
emitline(", ");
|
|
emitoff((off + 16): i64);
|
|
emitline("(BP)\n");
|
|
idx += 1;
|
|
} else { if (idx < 6) {
|
|
// Partial-fit stitch — mirrors tagged at lines
|
|
// 440-469. Caller's pushargsrev greedy-fills the
|
|
// remaining argregs (ptr,len,cap order), the tail
|
|
// spills to +16+stkcursor*8(BP).
|
|
let off: i32 = localadd(c, nm, tyslicesize(): i32, p.lhs);
|
|
let regs_left: i32 = 6 - idx;
|
|
let w: i32 = 0;
|
|
for (w < regs_left) {
|
|
emitline("\tMOVQ\t");
|
|
emitline(argregname(idx));
|
|
emitline(", ");
|
|
emitoff((off + w*8): i64);
|
|
emitline("(BP)\n");
|
|
idx += 1;
|
|
w += 1;
|
|
};
|
|
for (w < 3) {
|
|
emitline("\tMOVQ\t");
|
|
emitoff((16 + stkcursor*8): i64);
|
|
emitline("(BP), AX\n");
|
|
emitline("\tMOVQ\tAX, ");
|
|
emitoff((off + w*8): i64);
|
|
emitline("(BP)\n");
|
|
stkcursor += 1;
|
|
w += 1;
|
|
};
|
|
} else {
|
|
localaddstack(c, nm, p.lhs, 16 + stkcursor*8);
|
|
stkcursor += 3;
|
|
};};
|
|
} else { if (isstrtype(c, p.lhs)) {
|
|
if (idx + 2 <= 6) {
|
|
// #60: route str-param slot width through the
|
|
// primtypesize SSoT so #1's ty_str bump propagates
|
|
// here (parent #43 covered the reg-fill site only
|
|
// inside cgexpr).
|
|
let off: i32 = localadd(c, nm, primtypesize("str"): i32, p.lhs);
|
|
emitline("\tMOVQ\t");
|
|
emitline(argregname(idx));
|
|
emitline(", ");
|
|
emitoff(off: i64);
|
|
emitline("(BP)\n");
|
|
idx += 1;
|
|
emitline("\tMOVQ\t");
|
|
emitline(argregname(idx));
|
|
emitline(", ");
|
|
emitoff((off + 8): i64);
|
|
emitline("(BP)\n");
|
|
idx += 1;
|
|
} else { if (idx < 6) {
|
|
// Partial-fit stitch — mirrors tagged at lines
|
|
// 440-469. Only idx=5 hits this (nw=2,
|
|
// regs_left=1): ptr lands in R9, len at
|
|
// +16+stkcursor*8(BP).
|
|
// #60: same SSoT routing as the regs-fit arm above.
|
|
let off: i32 = localadd(c, nm, primtypesize("str"): i32, p.lhs);
|
|
let regs_left: i32 = 6 - idx;
|
|
let w: i32 = 0;
|
|
for (w < regs_left) {
|
|
emitline("\tMOVQ\t");
|
|
emitline(argregname(idx));
|
|
emitline(", ");
|
|
emitoff((off + w*8): i64);
|
|
emitline("(BP)\n");
|
|
idx += 1;
|
|
w += 1;
|
|
};
|
|
for (w < 2) {
|
|
emitline("\tMOVQ\t");
|
|
emitoff((16 + stkcursor*8): i64);
|
|
emitline("(BP), AX\n");
|
|
emitline("\tMOVQ\tAX, ");
|
|
emitoff((off + w*8): i64);
|
|
emitline("(BP)\n");
|
|
stkcursor += 1;
|
|
w += 1;
|
|
};
|
|
} else {
|
|
localaddstack(c, nm, p.lhs, 16 + stkcursor*8);
|
|
stkcursor += 2;
|
|
};};
|
|
} else { let stsz: i32 = structparamsize(c, p.lhs);
|
|
if (stsz > 0) {
|
|
// User-defined by-value struct ≤ 16B: 1 or 2
|
|
// integer eightbytes. Mirrors cstage's
|
|
// `struct_eb = (pu->size > 8) ? 2 : 1` and the
|
|
// matching reg/stack/stitch arms in cgen.c cgfn.
|
|
let nw: i32 = 1;
|
|
if (stsz > 8) { nw = 2; };
|
|
if (idx + nw <= 6) {
|
|
let off: i32 = localadd(c, nm, stsz, p.lhs);
|
|
let w: i32 = 0;
|
|
for (w < nw) {
|
|
emitline("\tMOVQ\t");
|
|
emitline(argregname(idx));
|
|
emitline(", ");
|
|
emitoff((off + w*8): i64);
|
|
emitline("(BP)\n");
|
|
idx += 1;
|
|
w += 1;
|
|
};
|
|
} else { if (idx < 6 && nw > 1) {
|
|
let off: i32 = localadd(c, nm, stsz, p.lhs);
|
|
let regs_left: i32 = 6 - idx;
|
|
let w: i32 = 0;
|
|
for (w < regs_left) {
|
|
emitline("\tMOVQ\t");
|
|
emitline(argregname(idx));
|
|
emitline(", ");
|
|
emitoff((off + w*8): i64);
|
|
emitline("(BP)\n");
|
|
idx += 1;
|
|
w += 1;
|
|
};
|
|
for (w < nw) {
|
|
emitline("\tMOVQ\t");
|
|
emitoff((16 + stkcursor*8): i64);
|
|
emitline("(BP), AX\n");
|
|
emitline("\tMOVQ\tAX, ");
|
|
emitoff((off + w*8): i64);
|
|
emitline("(BP)\n");
|
|
stkcursor += 1;
|
|
w += 1;
|
|
};
|
|
} else {
|
|
localaddstack(c, nm, p.lhs, 16 + stkcursor*8);
|
|
stkcursor += nw;
|
|
};};
|
|
} else {
|
|
if (idx < 6) {
|
|
let off: i32 = localadd(c, nm, 8, p.lhs);
|
|
emitline("\tMOVQ\t");
|
|
emitline(argregname(idx));
|
|
emitline(", ");
|
|
emitoff(off: i64);
|
|
emitline("(BP)\n");
|
|
idx += 1;
|
|
} else {
|
|
localaddstack(c, nm, p.lhs, 16 + stkcursor*8);
|
|
stkcursor += 1;
|
|
};
|
|
};
|
|
};};};
|
|
};
|
|
p = p.next;
|
|
};
|
|
};
|
|
|
|
fn cgfn(c: *cgen, fn_: *node) void = {
|
|
cgeninit(c, c.a);
|
|
c.fnname = fn_.str;
|
|
c.curmod = fn_.nmod;
|
|
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.
|
|
let sret_callee: bool = sretretsize(c, c.fnret) > 0;
|
|
|
|
// Capture the body into cgoutbuf while c.frame grows under
|
|
// emit-time localadd calls (#15/#26c — wwstage dropped its
|
|
// scanlocals pre-pass to align DOWN with cstage's first-use
|
|
// pattern). The prologue (TEXT label, PUSHQ/MOVQ/SUBQ) emits
|
|
// after the body finishes so the frame size reflects every
|
|
// localadd. Mirrors cstage cmd/w6c/cgen.c cgfn which builds
|
|
// `subsp`/`text` Progs up front and patches their `from.offset`
|
|
// at the end via txt_emit.
|
|
cgout_enable(c.a);
|
|
|
|
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
|
|
// the outermost N_BLOCK through cgstmt — cgblock now save/restores
|
|
// c.locals to scope inner shadows (post-#27), but the function body
|
|
// is not "an inner block": defers (queued during the body) and the
|
|
// implicit-return epilogue both call cgexpr after this loop and
|
|
// resolve identifiers via localfind, so the body's locals must
|
|
// still be in c.locals when we get there.
|
|
if (fn_.body != nil) {
|
|
if (fn_.body.kind == nkind.N_BLOCK) {
|
|
let s: *node = fn_.body.list;
|
|
for (s != nil) {
|
|
cgstmt(c, s);
|
|
s = s.next;
|
|
};
|
|
} else {
|
|
cgstmt(c, fn_.body);
|
|
};
|
|
};
|
|
|
|
if (c.lastwasreturn == 0) {
|
|
// Run any registered defers in LIFO order before the
|
|
// implicit return.
|
|
rundefers(c);
|
|
// Zero AX before the fall-through return — matches cstage,
|
|
// which always emits this so void-returning fns don't leak
|
|
// a stale callee value to their caller.
|
|
emitline("\tMOVQ\t$0, AX\n");
|
|
emitline("\tMOVQ\tBP, SP\n");
|
|
emitline("\tPOPQ\tBP\n");
|
|
emitline("\tRET\n");
|
|
};
|
|
|
|
cgout_disable();
|
|
|
|
let frame: i32 = c.frame;
|
|
if ((frame & 15) != 0) { frame = (frame + 15) & ~15; };
|
|
|
|
// 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.
|
|
emitline("TEXT ");
|
|
emitfnname(c, fn_.str, fn_.nmod);
|
|
emitline(",$");
|
|
emitint(frame: i64);
|
|
emitline("\n");
|
|
|
|
emitline("\tPUSHQ\tBP\n");
|
|
emitline("\tMOVQ\tSP, BP\n");
|
|
emitline("\tSUBQ\t$");
|
|
emitint(frame: i64);
|
|
emitline(", SP\n");
|
|
|
|
cgout_flush();
|
|
};
|
|
|
|
// ---- file-level entry ------------------------------------------------
|
|
|
|
export fn cgfile(c: *cgen, file: *node) void = {
|
|
if (file == nil) { return; };
|
|
c.strlits = nil;
|
|
c.strlitseq = 0;
|
|
collectaliases(c, file);
|
|
// Enums must register before structs — fieldsize on a tkind-typed
|
|
// field needs the enum's storage size, otherwise it falls back to
|
|
// 8 (wrong load width).
|
|
collectenums(c, file);
|
|
collectstructs(c, file);
|
|
collectdefs(c, file);
|
|
collectfnrets(c, file);
|
|
fficollect(c, file);
|
|
collectmods(c, file);
|
|
collectlets(c, file);
|
|
let d: *node = file.list;
|
|
for (d != nil) {
|
|
if (d.kind == nkind.N_FNDECL) {
|
|
if (d.body != nil) {
|
|
cgfn(c, d);
|
|
};
|
|
};
|
|
d = d.next;
|
|
};
|
|
letpreintern(c, file);
|
|
emitdatasection(c);
|
|
emitdefconstants(c, file);
|
|
emitletdataw(c, file);
|
|
};
|