selfhost: fix several wwstage cgen miscompilations
Surfaced via examples/lisp, which had to work around the following in
source. Each lowering now matches cstage on the same shape.
- cgassign / cgdot: two-level field through a non-pointer sub-struct.
`(*L).cur.kind = k` (cur a struct-by-value field of L) silently
dropped the store; the corresponding read fell into the SB-symbol
fallback and the linker reported `undefined reference to kind`. The
two new branches resolve outer-field offset + inner-field offset
and emit a single direct store/load at the combined slot, both for
T-by-value and *T-base shapes.
- cgdot: `xs[i].field` chains the trailing field load through the
N_INDEX result for [N]T / []T / *T element-of-struct-ptr. The
cgforrange loop variable now carries the elem tnode so the same
fast path covers `for (let x .. xs) { x.field }`.
- cgindex / cgassign: top-level `[N]T` array and `*T` pointer used
as an index base. cgindex now emits LEAQ name(SB) (array) or
MOVQ name(SB) (pointer) with the correct element scaling; without
this the fallback emitted neither base and walked off the saved
BP slot. Adds letvartnode() helper, an N_TARRAY branch to
letemitsize so the array shows up in c.lets, and an N_TARRAY
initialiser path in emitletdataw that lays the literal bytes into
DATAW.
- cglet / scanlocals: infer the local's tnode for an unannotated
`let x = f()` / `let x = f()?`. inferletcalltype() reads the
callee's declared return; `?` and `!` strip to the success variant
so a tagged-union let allocates the full 24B slot and the
struct-field dispatch in cgdot/cgassign sees the right type.
letslotsize now defers to slotsize on the inferred type.
- slotsize: follow type aliases for tagged-union variants. With
`type parserr = !str;`, the variant slot was 8B instead of the
required 16B; the tagged let stomped on the next slot at the
AX/DX/CX spill.
- cgreturn: tagged-union return forwarding. `return f();` where f
also returns a tagged union now passes the (tag, payload1,
payload2) triple through unchanged instead of re-wrapping it.
- cgreturn / cglet / taggedvariantindex: dispatch by variant name
with module-qualified-vs-bare matching, and recognise N_STRUCTLIT
as the variant tag for `return eof{};`. cgexpr default emits
`MOVQ $0, AX` so the surrounding return shuffle isn't left with
a stale AX.
- isstrtype / nodeisstr: resolve through `!T` aliases. `parserr =
!str` was not propagating the str-shape to the rhs check and the
MOVQ BX,CX shuffle was being dropped from str-typed local
returns.
- exprfloatkind: recognise `p.field` as f64/f32 when the struct
field is so declared, so `v.fval: i64` lowers to CVTTSD2SI on X0.
- cgassign: str field on a direct struct local writes both halves.
`L.src = s;` previously dropped s.len.
- cgcall: pop into the int reg window only up to 6 (DI..R9); rest
stays on the stack and the caller emits ADDQ to clean up.
cgfnparams accepts >6-arg signatures by registering the overflow
params at positive BP offsets (16+8*k(BP)), no spill instruction
emitted.
All 26 harness tests pass; bootstrap reaches a byte-stable fixed
point at ww3 == ww4.
This commit is contained in:
@@ -190,13 +190,11 @@ fn nodeisstr(c: *cgen, n: *node) bool = {
|
||||
let nm: str = n.str;
|
||||
let lc: *local = localfindnode(c, nm);
|
||||
if (lc != nil) {
|
||||
let tn: *node = lc.tnode;
|
||||
if (tn != nil) {
|
||||
if (tn.kind == nkind.N_TNAME) {
|
||||
let tnm: str = tn.str;
|
||||
if (streq(tnm, "str")) { return true; };
|
||||
};
|
||||
};
|
||||
// Use isstrtype so `!str` aliases (parserr = !str) and
|
||||
// `type foo = str;` chains resolve through. The bare
|
||||
// `streq("str", ...)` test missed them and dropped the
|
||||
// MOVQ BX,CX shuffle on returns of str-aliased locals.
|
||||
if (isstrtype(c, lc.tnode)) { return true; };
|
||||
};
|
||||
return false;
|
||||
};
|
||||
@@ -684,11 +682,88 @@ fn primsize(name: str) i32 = {
|
||||
return 0;
|
||||
};
|
||||
|
||||
// variantnamematch — tagged-union variant names are compared as if
|
||||
// they'd been alias-resolved. Pattern names can be module-qualified
|
||||
// (`strconv.invalid` from a `case let e: strconv.invalid =>`),
|
||||
// while the variant's declared name inside its own module is bare
|
||||
// (`invalid`). With no checker the cgen can't follow imports, so we
|
||||
// accept exact match plus suffix-after-`.` on either side. Mirrors
|
||||
// the C cgen's type_eq, which goes through resolved Type pointers.
|
||||
fn variantnamematch(vname: str, pname: str) bool = {
|
||||
if (streq(vname, pname)) { return true; };
|
||||
// `pname` is qualified, `vname` is bare: drop module prefix.
|
||||
let i: i32 = 0;
|
||||
for (i < pname.len) {
|
||||
if (pname[i] == '.': u8) {
|
||||
let tail: str;
|
||||
tail.ptr = pname.ptr + i + 1;
|
||||
tail.len = pname.len - i - 1;
|
||||
if (streq(tail, vname)) { return true; };
|
||||
};
|
||||
i += 1;
|
||||
};
|
||||
// `vname` is qualified, `pname` is bare: same trick in reverse.
|
||||
let j: i32 = 0;
|
||||
for (j < vname.len) {
|
||||
if (vname[j] == '.': u8) {
|
||||
let tail: str;
|
||||
tail.ptr = vname.ptr + j + 1;
|
||||
tail.len = vname.len - j - 1;
|
||||
if (streq(tail, pname)) { return true; };
|
||||
};
|
||||
j += 1;
|
||||
};
|
||||
return false;
|
||||
};
|
||||
|
||||
// inferletcalltype — for an annotation-less `let x = expr;`, return
|
||||
// a usable tnode for cgen's struct-aware paths. Today: `let x =
|
||||
// f()?` infers x's type from the success variant of f's tagged
|
||||
// return; without this, x has tnode = nil and `x.field` falls into
|
||||
// the SB-symbol fallback (linker reports `undefined reference to
|
||||
// <fieldname>`). We don't infer for plain `let x = f()` yet —
|
||||
// non-tagged returns don't carry their type back the same way.
|
||||
fn inferletcalltype(c: *cgen, rhs: *node) *node = {
|
||||
if (rhs == nil) { return nil; };
|
||||
// `?` (N_TRYPROP) and `!` (N_TRYUNW) both unwrap a tagged
|
||||
// return to its success variant; the rhs we want the type of
|
||||
// is the inner call expression.
|
||||
let unwrap: bool = false;
|
||||
let call: *node = rhs;
|
||||
if (rhs.kind == nkind.N_TRYPROP) { call = rhs.lhs; unwrap = true; };
|
||||
if (rhs.kind == nkind.N_TRYUNW) { call = rhs.lhs; unwrap = true; };
|
||||
if (call == nil) { return nil; };
|
||||
if (call.kind != nkind.N_CALL) { return nil; };
|
||||
let callee: *node = call.lhs;
|
||||
if (callee == nil) { return nil; };
|
||||
let cname: str;
|
||||
cname.ptr = nil; cname.len = 0;
|
||||
if (callee.kind == nkind.N_IDENT) { cname = callee.str; };
|
||||
if (callee.kind == nkind.N_DOT) { cname = callee.str; };
|
||||
if (cname.len == 0) { return nil; };
|
||||
let rt: *node = fnretlookup(c, cname);
|
||||
if (rt == nil) { return nil; };
|
||||
if (unwrap) {
|
||||
// Strip error variants — success type is the first
|
||||
// variant of the tagged return.
|
||||
if (rt.kind != nkind.N_TTAGGED) { return nil; };
|
||||
return rt.list;
|
||||
};
|
||||
// Plain call: declared return type is the local's type.
|
||||
return rt;
|
||||
};
|
||||
|
||||
// letslotsize — slot size for a `let` binding. Like slotsize, but
|
||||
// detects `[_]T = arrlit;` (the type-AST has rhs == nil as the
|
||||
// length-inferred sentinel) and computes count × element-size from
|
||||
// the initialiser. Used by both scanlocals (prologue sizing) and
|
||||
// cglet (slot alloc) so they agree on the frame layout.
|
||||
//
|
||||
// `let x = f();` (no annotation): infer from `f`'s declared return
|
||||
// type so a 24B tagged-union return reserves all three spill slots,
|
||||
// not the default 8B. Without this, the AX:DX:CX spill in cglet's
|
||||
// tagged-init branch writes past the local and tramples the next
|
||||
// slot.
|
||||
export fn letslotsize(c: *cgen, n: *node) i32 = {
|
||||
// `[_]T = arrlit;` — inferred-length array. slotsize would
|
||||
// return elem_size * 1 (treating missing length as 1); intercept
|
||||
@@ -727,7 +802,13 @@ export fn letslotsize(c: *cgen, n: *node) i32 = {
|
||||
};
|
||||
};
|
||||
};
|
||||
return slotsize(c, n.lhs);
|
||||
if (n.lhs != nil) { return slotsize(c, n.lhs); };
|
||||
// Annotation-less init: defer to the call's return type if we
|
||||
// can infer it. Tagged-union returns need 24B; everything else
|
||||
// matches slotsize on the inferred type.
|
||||
let inferred: *node = inferletcalltype(c, n.rhs);
|
||||
if (inferred != nil) { return slotsize(c, inferred); };
|
||||
return 8;
|
||||
};
|
||||
|
||||
fn slotsize(c: *cgen, typn: *node) i32 = {
|
||||
@@ -779,6 +860,19 @@ fn slotsize(c: *cgen, typn: *node) i32 = {
|
||||
// Named struct lookup.
|
||||
let si: *structinfo = structlookup(c, nm);
|
||||
if (si != nil) { return si.totsize; };
|
||||
// Type alias (`type foo = !str;` / `type foo = bar;`):
|
||||
// follow it so a tagged-union variant of a !str-aliased
|
||||
// error type contributes 16 bytes to the max payload
|
||||
// rather than 8 (the default).
|
||||
if (c != nil) {
|
||||
let aliased: *node = aliaslookup(c, nm);
|
||||
if (aliased != nil) {
|
||||
if (aliased.kind == nkind.N_TBANG) {
|
||||
return slotsize(c, aliased.lhs);
|
||||
};
|
||||
return slotsize(c, aliased);
|
||||
};
|
||||
};
|
||||
return 8;
|
||||
};
|
||||
if (k == nkind.N_TARRAY) {
|
||||
@@ -930,7 +1024,20 @@ fn isstrtype(c: *cgen, t: *node) bool = {
|
||||
if (isstrtyperaw(t)) { return true; };
|
||||
if (c == nil) { return false; };
|
||||
let r: *node = resolvetype(c, t);
|
||||
return isstrtyperaw(r);
|
||||
if (isstrtyperaw(r)) { return true; };
|
||||
// `parserr = !str` — `!T` aliases shouldn't hide their
|
||||
// underlying type from str-routing. Unwrap and re-check.
|
||||
if (r != nil) {
|
||||
if (r.kind == nkind.N_TBANG) {
|
||||
let inner: *node = r.lhs;
|
||||
if (isstrtyperaw(inner)) { return true; };
|
||||
if (inner != nil) {
|
||||
let r2: *node = resolvetype(c, inner);
|
||||
if (isstrtyperaw(r2)) { return true; };
|
||||
};
|
||||
};
|
||||
};
|
||||
return false;
|
||||
};
|
||||
|
||||
fn isslicetyperaw(t: *node) bool = {
|
||||
@@ -1061,6 +1168,48 @@ export fn exprfloatkind(c: *cgen, n: *node) i32 = {
|
||||
};
|
||||
return 0;
|
||||
};
|
||||
if (k == nkind.N_DOT) {
|
||||
// `p.field` where the struct field is f64/f32. Without this,
|
||||
// `v.fval: i64` lowers to CVTSI on an integer-load value
|
||||
// instead of CVTTSD2SI on the X0 the cgdot path actually
|
||||
// emits for an f64 field.
|
||||
let base: *node = n.lhs;
|
||||
let fld: str = n.str;
|
||||
if (base != nil) {
|
||||
let sname: str;
|
||||
sname.ptr = nil; sname.len = 0;
|
||||
if (base.kind == nkind.N_IDENT) {
|
||||
let lc: *local = localfindnode(c, base.str);
|
||||
if (lc != nil) {
|
||||
let tn: *node = lc.tnode;
|
||||
if (tn != nil) {
|
||||
if (tn.kind == nkind.N_TNAME) { sname = tn.str; };
|
||||
if (tn.kind == nkind.N_TPTR) {
|
||||
let pe: *node = tn.lhs;
|
||||
if (pe != nil) {
|
||||
if (pe.kind == nkind.N_TNAME) { sname = pe.str; };
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
if (sname.len > 0) {
|
||||
let si: *structinfo = structlookup(c, sname);
|
||||
if (si != nil) {
|
||||
let fi: *fieldinfo = si.fields;
|
||||
for (fi != nil) {
|
||||
if (streq(fi.fname, fld)) {
|
||||
if (isf32type(c, fi.tnode)) { return 1; };
|
||||
if (isfloattype(c, fi.tnode)) { return 2; };
|
||||
return 0;
|
||||
};
|
||||
fi = fi.finext;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
return 0;
|
||||
};
|
||||
return 0;
|
||||
};
|
||||
|
||||
@@ -1129,6 +1278,19 @@ fn rhstargetname(c: *cgen, rhs: *node) str = {
|
||||
return nm;
|
||||
};
|
||||
if (rhs.kind == nkind.N_STRLIT) { return "str"; };
|
||||
// `T{}` carries its type name on the lhs N_IDENT — the parser
|
||||
// builds `N_STRUCTLIT{ lhs = N_IDENT("T"), list = fields }`.
|
||||
// Needed so `return eof{};` (variant of a tagged union) resolves
|
||||
// to the `eof` variant index rather than falling through to the
|
||||
// "first non-str variant" fallback in taggedvariantindex.
|
||||
if (rhs.kind == nkind.N_STRUCTLIT) {
|
||||
let tref: *node = rhs.lhs;
|
||||
if (tref != nil) {
|
||||
if (tref.kind == nkind.N_IDENT) { return tref.str; };
|
||||
if (tref.kind == nkind.N_TNAME) { return tref.str; };
|
||||
};
|
||||
return nm;
|
||||
};
|
||||
if (rhs.kind == nkind.N_IDENT) {
|
||||
let lc: *local = localfindnode(c, rhs.str);
|
||||
if (lc != nil) {
|
||||
@@ -1154,7 +1316,7 @@ fn taggedvariantindex(c: *cgen, tagged: *node, rhs: *node) i32 = {
|
||||
let idx: i32 = 0;
|
||||
for (v != nil) {
|
||||
if (v.kind == nkind.N_TNAME) {
|
||||
if (streq(v.str, wantname)) { return idx; };
|
||||
if (variantnamematch(v.str, wantname)) { return idx; };
|
||||
};
|
||||
v = v.next;
|
||||
idx += 1;
|
||||
|
||||
Reference in New Issue
Block a user