selfhost: graduate N_* defs to nkind enum
This commit is contained in:
@@ -30,16 +30,16 @@ use strconv;
|
||||
fn pushargsrev(c: *cgen, arg: *node) i32 = {
|
||||
if (arg == nil) { return 0; };
|
||||
let rest: i32 = pushargsrev(c, arg.next);
|
||||
// N_SLICE expression as arg: `buf[lo:hi]` builds a slice header
|
||||
// nkind.N_SLICE expression as arg: `buf[lo:hi]` builds a slice header
|
||||
// on the stack matching C cgen's sequence — push base, push hi,
|
||||
// compute lo, pop into BX/CX, derive len/ptr, push (cap, len, ptr).
|
||||
if (arg.kind == N_SLICE) {
|
||||
if (arg.kind == nkind.N_SLICE) {
|
||||
let base: *node = arg.lhs;
|
||||
let lo: *node = arg.rhs;
|
||||
let hi: *node = arg.cond;
|
||||
let baselocal: *local = nil;
|
||||
if (base != nil) {
|
||||
if (base.kind == N_IDENT) {
|
||||
if (base.kind == nkind.N_IDENT) {
|
||||
let bn: str = base.str;
|
||||
baselocal = localfindnode(c, bn);
|
||||
};
|
||||
@@ -48,7 +48,7 @@ fn pushargsrev(c: *cgen, arg: *node) i32 = {
|
||||
if (baselocal != nil) {
|
||||
let tn: *node = baselocal.tnode;
|
||||
if (tn != nil) {
|
||||
if (tn.kind == N_TARRAY) {
|
||||
if (tn.kind == nkind.N_TARRAY) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitoff(baselocal.off: i64);
|
||||
emitline("(BP), AX\n");
|
||||
@@ -72,20 +72,20 @@ fn pushargsrev(c: *cgen, arg: *node) i32 = {
|
||||
} else { if (baselocal != nil) {
|
||||
let tn: *node = baselocal.tnode;
|
||||
if (tn != nil) {
|
||||
if (tn.kind == N_TARRAY) {
|
||||
if (tn.kind == nkind.N_TARRAY) {
|
||||
let lenn: *node = tn.rhs;
|
||||
if (lenn != nil) {
|
||||
if (lenn.kind == N_INTLIT) {
|
||||
if (lenn.kind == nkind.N_INTLIT) {
|
||||
emitline("\tMOVQ\t$");
|
||||
emituint(lenn.uval);
|
||||
emitline(", AX\n");
|
||||
};
|
||||
};
|
||||
} else { if (tn.kind == N_TSLICE) {
|
||||
} else { if (tn.kind == nkind.N_TSLICE) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff((baselocal.off + 8): i64);
|
||||
emitline("(BP), AX\n");
|
||||
} else { if (tn.kind == N_TNAME) {
|
||||
} else { if (tn.kind == nkind.N_TNAME) {
|
||||
if (streq(tn.str, "str")) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff((baselocal.off + 8): i64);
|
||||
@@ -113,7 +113,7 @@ fn pushargsrev(c: *cgen, arg: *node) i32 = {
|
||||
// Slice/tagged ident args: emit per-register MOVQ+PUSHQ pairs in
|
||||
// reverse order (cap/v1, len/v0, ptr/tag) so a left-to-right pop
|
||||
// into argregs lands the canonical (ptr/tag, len/v0, cap/v1).
|
||||
if (arg.kind == N_IDENT) {
|
||||
if (arg.kind == nkind.N_IDENT) {
|
||||
let nm: str = arg.str;
|
||||
let lc: *local = localfindnode(c, nm);
|
||||
if (lc != nil) {
|
||||
@@ -154,13 +154,13 @@ fn pushargsrev(c: *cgen, arg: *node) i32 = {
|
||||
fn nodeisslice(c: *cgen, n: *node) bool = {
|
||||
if (n == nil) { return false; };
|
||||
let k: i32 = n.kind;
|
||||
if (k == N_IDENT) {
|
||||
if (k == nkind.N_IDENT) {
|
||||
let nm: str = n.str;
|
||||
let lc: *local = localfindnode(c, nm);
|
||||
if (lc != nil) { return isslicetype(c, lc.tnode); };
|
||||
return false;
|
||||
};
|
||||
if (k == N_SLICE) { return true; };
|
||||
if (k == nkind.N_SLICE) { return true; };
|
||||
return false;
|
||||
};
|
||||
|
||||
@@ -170,14 +170,14 @@ fn nodeisslice(c: *cgen, n: *node) bool = {
|
||||
fn nodeisstr(c: *cgen, n: *node) bool = {
|
||||
if (n == nil) { return false; };
|
||||
let k: i32 = n.kind;
|
||||
if (k == N_STRLIT) { return true; };
|
||||
if (k == N_IDENT) {
|
||||
if (k == nkind.N_STRLIT) { return true; };
|
||||
if (k == nkind.N_IDENT) {
|
||||
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 == N_TNAME) {
|
||||
if (tn.kind == nkind.N_TNAME) {
|
||||
let tnm: str = tn.str;
|
||||
if (streq(tnm, "str")) { return true; };
|
||||
};
|
||||
@@ -185,10 +185,10 @@ fn nodeisstr(c: *cgen, n: *node) bool = {
|
||||
};
|
||||
return false;
|
||||
};
|
||||
if (k == N_CALL) {
|
||||
if (k == nkind.N_CALL) {
|
||||
let callee: *node = n.lhs;
|
||||
if (callee != nil) {
|
||||
if (callee.kind == N_IDENT) {
|
||||
if (callee.kind == nkind.N_IDENT) {
|
||||
let cnm: str = callee.str;
|
||||
let rt: *node = fnretlookup(c, cnm);
|
||||
return isstrtype(c, rt);
|
||||
@@ -196,7 +196,7 @@ fn nodeisstr(c: *cgen, n: *node) bool = {
|
||||
};
|
||||
return false;
|
||||
};
|
||||
if (k == N_DOT) {
|
||||
if (k == nkind.N_DOT) {
|
||||
let base: *node = n.lhs;
|
||||
let fld: str = n.str;
|
||||
// `<expr>.ptr` is *u8 not str; `<expr>.len` is i32 not str.
|
||||
@@ -206,17 +206,17 @@ fn nodeisstr(c: *cgen, n: *node) bool = {
|
||||
if (base != nil) {
|
||||
let sname: str;
|
||||
sname.ptr = nil; sname.len = 0;
|
||||
if (base.kind == N_IDENT) {
|
||||
if (base.kind == nkind.N_IDENT) {
|
||||
let lc: *local = localfindnode(c, base.str);
|
||||
if (lc != nil) {
|
||||
let tn: *node = lc.tnode;
|
||||
let lkind: i32 = -1;
|
||||
if (tn != nil) { lkind = tn.kind; };
|
||||
if (lkind == N_TNAME) { sname = tn.str; };
|
||||
if (lkind == N_TPTR) {
|
||||
if (lkind == nkind.N_TNAME) { sname = tn.str; };
|
||||
if (lkind == nkind.N_TPTR) {
|
||||
let inner: *node = tn.lhs;
|
||||
if (inner != nil) {
|
||||
if (inner.kind == N_TNAME) { sname = inner.str; };
|
||||
if (inner.kind == nkind.N_TNAME) { sname = inner.str; };
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -224,10 +224,10 @@ fn nodeisstr(c: *cgen, n: *node) bool = {
|
||||
// Chained dot (`p.foo.bar`): use dotinnerstructptr
|
||||
// to resolve the inner chain to the *struct it lands
|
||||
// on, then look up `fld` in that struct.
|
||||
if (base.kind == N_DOT) {
|
||||
if (base.kind == nkind.N_DOT) {
|
||||
let innert: *node = dotinnerstructptr(c, base);
|
||||
if (innert != nil) {
|
||||
if (innert.kind == N_TNAME) { sname = innert.str; };
|
||||
if (innert.kind == nkind.N_TNAME) { sname = innert.str; };
|
||||
};
|
||||
};
|
||||
if (sname.len > 0) {
|
||||
@@ -246,7 +246,7 @@ fn nodeisstr(c: *cgen, n: *node) bool = {
|
||||
};
|
||||
return false;
|
||||
};
|
||||
if (k == N_CAST) {
|
||||
if (k == nkind.N_CAST) {
|
||||
return isstrtype(c, n.rhs);
|
||||
};
|
||||
return false;
|
||||
@@ -266,13 +266,13 @@ fn typenameisunsigned(nm: str) bool = {
|
||||
// typenodeisunsigned — recurse through TNAME / TPTR / TSLICE etc.
|
||||
fn typenodeisunsigned(t: *node) bool = {
|
||||
if (t == nil) { return false; };
|
||||
if (t.kind == N_TNAME) { return typenameisunsigned(t.str); };
|
||||
if (t.kind == nkind.N_TNAME) { return typenameisunsigned(t.str); };
|
||||
return false;
|
||||
};
|
||||
|
||||
// typeis8byteprimitive — does this type take exactly one 8-byte
|
||||
// slot (pointer / fn-ptr / 64-bit int / chan / scalar primitive
|
||||
// padded up to 8) rather than a wider aggregate? Used by N_LET
|
||||
// padded up to 8) rather than a wider aggregate? Used by nkind.N_LET
|
||||
// zero-init to mirror C cgen's "only zero if sz == 8 at the type
|
||||
// level" rule. Strings (16), slices (24), tagged unions (>=16),
|
||||
// tuples (16), structs (varies), arrays — all fall through to
|
||||
@@ -280,14 +280,14 @@ fn typenodeisunsigned(t: *node) bool = {
|
||||
fn typeis8byteprimitive(c: *cgen, t: *node) bool = {
|
||||
if (t == nil) { return false; };
|
||||
let k: i32 = t.kind;
|
||||
if (k == N_TPTR) { return true; };
|
||||
if (k == N_TFN) { return true; };
|
||||
if (k == N_TCHAN) { return true; };
|
||||
if (k == N_TSLICE) { return false; };
|
||||
if (k == N_TARRAY) { return false; };
|
||||
if (k == N_TTUPLE) { return false; };
|
||||
if (k == N_TTAGGED){ return false; };
|
||||
if (k == N_TNAME) {
|
||||
if (k == nkind.N_TPTR) { return true; };
|
||||
if (k == nkind.N_TFN) { return true; };
|
||||
if (k == nkind.N_TCHAN) { return true; };
|
||||
if (k == nkind.N_TSLICE) { return false; };
|
||||
if (k == nkind.N_TARRAY) { return false; };
|
||||
if (k == nkind.N_TTUPLE) { return false; };
|
||||
if (k == nkind.N_TTAGGED){ return false; };
|
||||
if (k == nkind.N_TNAME) {
|
||||
let nm: str = t.str;
|
||||
if (streq(nm, "str")) { return false; };
|
||||
// Struct alias: not a primitive even if the slot is 8B.
|
||||
@@ -322,7 +322,7 @@ fn fieldloadop(f: *fieldinfo) str = {
|
||||
if (sz == 4) {
|
||||
let t: *node = f.tnode;
|
||||
if (t != nil) {
|
||||
if (t.kind == N_TNAME) {
|
||||
if (t.kind == nkind.N_TNAME) {
|
||||
if (typenameissigned(t.str)) { return "MOVSXD"; };
|
||||
};
|
||||
};
|
||||
@@ -347,11 +347,11 @@ fn fieldstoreop(f: *fieldinfo) str = {
|
||||
// the slice element type.
|
||||
fn indexbaseesz(c: *cgen, base: *node) i32 = {
|
||||
if (base == nil) { return 8; };
|
||||
if (base.kind != N_DOT) { return 8; };
|
||||
if (base.kind != nkind.N_DOT) { return 8; };
|
||||
let fld: str = base.str;
|
||||
let inner: *node = base.lhs;
|
||||
if (inner == nil) { return 8; };
|
||||
if (inner.kind != N_IDENT) { return 8; };
|
||||
if (inner.kind != nkind.N_IDENT) { return 8; };
|
||||
let nm: str = inner.str;
|
||||
let lc: *local = localfindnode(c, nm);
|
||||
if (lc == nil) { return 8; };
|
||||
@@ -361,12 +361,12 @@ fn indexbaseesz(c: *cgen, base: *node) i32 = {
|
||||
// `.ptr` pseudo-field on str/slice → element of the str/slice.
|
||||
if (streq(fld, "ptr")) {
|
||||
let innert: *node = tn;
|
||||
if (tn.kind == N_TPTR) { innert = tn.lhs; };
|
||||
if (tn.kind == nkind.N_TPTR) { innert = tn.lhs; };
|
||||
if (innert == nil) { return 8; };
|
||||
if (innert.kind == N_TNAME) {
|
||||
if (innert.kind == nkind.N_TNAME) {
|
||||
if (streq(innert.str, "str")) { return 1; };
|
||||
};
|
||||
if (innert.kind == N_TSLICE) { return elemsizeof(innert); };
|
||||
if (innert.kind == nkind.N_TSLICE) { return elemsizeof(innert); };
|
||||
return 8;
|
||||
};
|
||||
|
||||
@@ -374,11 +374,11 @@ fn indexbaseesz(c: *cgen, base: *node) i32 = {
|
||||
let lkind: i32 = tn.kind;
|
||||
let sname: str;
|
||||
sname.ptr = nil; sname.len = 0;
|
||||
if (lkind == N_TNAME) { sname = tn.str; };
|
||||
if (lkind == N_TPTR) {
|
||||
if (lkind == nkind.N_TNAME) { sname = tn.str; };
|
||||
if (lkind == nkind.N_TPTR) {
|
||||
let pinner: *node = tn.lhs;
|
||||
if (pinner != nil) {
|
||||
if (pinner.kind == N_TNAME) { sname = pinner.str; };
|
||||
if (pinner.kind == nkind.N_TNAME) { sname = pinner.str; };
|
||||
};
|
||||
};
|
||||
if (sname.len == 0) { return 8; };
|
||||
@@ -390,10 +390,10 @@ fn indexbaseesz(c: *cgen, base: *node) i32 = {
|
||||
if (streq(fn_, fld)) {
|
||||
let ft: *node = fi.tnode;
|
||||
if (ft == nil) { return 8; };
|
||||
if (ft.kind == N_TPTR) {
|
||||
if (ft.kind == nkind.N_TPTR) {
|
||||
let elem: *node = ft.lhs;
|
||||
if (elem != nil) {
|
||||
if (elem.kind == N_TNAME) {
|
||||
if (elem.kind == nkind.N_TNAME) {
|
||||
if (streq(elem.str, "str")) { return 16; };
|
||||
let ps: i32 = primsize(elem.str);
|
||||
if (ps > 0) { return ps; };
|
||||
@@ -401,11 +401,11 @@ fn indexbaseesz(c: *cgen, base: *node) i32 = {
|
||||
};
|
||||
return 8;
|
||||
};
|
||||
if (ft.kind == N_TSLICE) { return elemsizeof(ft); };
|
||||
if (ft.kind == nkind.N_TSLICE) { return elemsizeof(ft); };
|
||||
// str-typed field: indexing yields one byte
|
||||
// (`n.s[i]` where .s is str — matches C cgen's
|
||||
// MOVZBQ for byte indexing).
|
||||
if (ft.kind == N_TNAME) {
|
||||
if (ft.kind == nkind.N_TNAME) {
|
||||
if (streq(ft.str, "str")) { return 1; };
|
||||
};
|
||||
return 8;
|
||||
@@ -415,36 +415,36 @@ fn indexbaseesz(c: *cgen, base: *node) i32 = {
|
||||
return 8;
|
||||
};
|
||||
|
||||
// dotinnerstructptr — for an N_DOT whose lhs is a chain of dots
|
||||
// or an N_IDENT, walk the chain and return the N_TNAME tnode of the
|
||||
// dotinnerstructptr — for an nkind.N_DOT whose lhs is a chain of dots
|
||||
// or an nkind.N_IDENT, walk the chain and return the nkind.N_TNAME tnode of the
|
||||
// struct that the chain dereferences to (i.e., for `r.sym` where
|
||||
// .sym is *lsym, return N_TNAME("lsym")). Returns nil if the chain
|
||||
// .sym is *lsym, return nkind.N_TNAME("lsym")). Returns nil if the chain
|
||||
// doesn't resolve to a *struct.
|
||||
//
|
||||
// Used by the chained-DOT cgen path so `r.sym.val` knows the outer
|
||||
// is a field of `lsym`.
|
||||
fn dotinnerstructptr(c: *cgen, n: *node) *node = {
|
||||
if (n == nil) { return nil; };
|
||||
if (n.kind != N_DOT) { return nil; };
|
||||
if (n.kind != nkind.N_DOT) { return nil; };
|
||||
let base: *node = n.lhs;
|
||||
let fld: str = n.str;
|
||||
if (base == nil) { return nil; };
|
||||
|
||||
// Resolve base's struct tnode.
|
||||
let baset: *node = nil;
|
||||
if (base.kind == N_IDENT) {
|
||||
if (base.kind == nkind.N_IDENT) {
|
||||
let lc: *local = localfindnode(c, base.str);
|
||||
if (lc == nil) { return nil; };
|
||||
let tn: *node = lc.tnode;
|
||||
if (tn == nil) { return nil; };
|
||||
// base could be either struct-by-value (N_TNAME) or *struct (N_TPTR).
|
||||
if (tn.kind == N_TNAME) { baset = tn; };
|
||||
if (tn.kind == N_TPTR) { baset = tn.lhs; };
|
||||
} else { if (base.kind == N_DOT) {
|
||||
// base could be either struct-by-value (nkind.N_TNAME) or *struct (nkind.N_TPTR).
|
||||
if (tn.kind == nkind.N_TNAME) { baset = tn; };
|
||||
if (tn.kind == nkind.N_TPTR) { baset = tn.lhs; };
|
||||
} else { if (base.kind == nkind.N_DOT) {
|
||||
baset = dotinnerstructptr(c, base);
|
||||
};};
|
||||
if (baset == nil) { return nil; };
|
||||
if (baset.kind != N_TNAME) { return nil; };
|
||||
if (baset.kind != nkind.N_TNAME) { return nil; };
|
||||
|
||||
// Look up the struct, find the field, return the field's *struct.
|
||||
let si: *structinfo = structlookup(c, baset.str);
|
||||
@@ -454,10 +454,10 @@ fn dotinnerstructptr(c: *cgen, n: *node) *node = {
|
||||
if (streq(fi.fname, fld)) {
|
||||
let ft: *node = fi.tnode;
|
||||
if (ft == nil) { return nil; };
|
||||
if (ft.kind != N_TPTR) { return nil; };
|
||||
if (ft.kind != nkind.N_TPTR) { return nil; };
|
||||
let inner: *node = ft.lhs;
|
||||
if (inner == nil) { return nil; };
|
||||
if (inner.kind != N_TNAME) { return nil; };
|
||||
if (inner.kind != nkind.N_TNAME) { return nil; };
|
||||
return inner;
|
||||
};
|
||||
fi = fi.finext;
|
||||
@@ -472,10 +472,10 @@ fn elemsizeof(t: *node) i32 = {
|
||||
if (t == nil) { return 1; };
|
||||
let k: i32 = t.kind;
|
||||
let elem: *node = nil;
|
||||
if (k == N_TPTR) { elem = t.lhs; };
|
||||
if (k == N_TSLICE) { elem = t.lhs; };
|
||||
if (k == N_TARRAY) { elem = t.lhs; };
|
||||
if (k == N_TNAME) {
|
||||
if (k == nkind.N_TPTR) { elem = t.lhs; };
|
||||
if (k == nkind.N_TSLICE) { elem = t.lhs; };
|
||||
if (k == nkind.N_TARRAY) { elem = t.lhs; };
|
||||
if (k == nkind.N_TNAME) {
|
||||
let nm: str = t.str;
|
||||
if (streq(nm, "str")) { return 1; };
|
||||
// Indexing a primitive name (rare): element size = the prim.
|
||||
@@ -484,7 +484,7 @@ fn elemsizeof(t: *node) i32 = {
|
||||
return 1;
|
||||
};
|
||||
if (elem == nil) { return 1; };
|
||||
if (elem.kind == N_TNAME) {
|
||||
if (elem.kind == nkind.N_TNAME) {
|
||||
let nm: str = elem.str;
|
||||
// str element is 16B (ptr+len). primsize returns 0 for it.
|
||||
if (streq(nm, "str")) { return 16; };
|
||||
@@ -496,28 +496,28 @@ fn elemsizeof(t: *node) i32 = {
|
||||
|
||||
// nodeisunsigned — best-effort cgen-time inference from the AST. We
|
||||
// don't have a typed AST yet, so we walk surface nodes:
|
||||
// N_INTLIT — never marked unsigned (no tsuffix plumbing yet)
|
||||
// N_IDENT — look up the local's declared type
|
||||
// N_DOT — look up the field's declared type via struct reg
|
||||
// N_BIN / N_UN — recurse: unsigned if either operand is unsigned
|
||||
// N_CAST — use the cast target type
|
||||
// nkind.N_INTLIT — never marked unsigned (no tsuffix plumbing yet)
|
||||
// nkind.N_IDENT — look up the local's declared type
|
||||
// nkind.N_DOT — look up the field's declared type via struct reg
|
||||
// nkind.N_BIN / nkind.N_UN — recurse: unsigned if either operand is unsigned
|
||||
// nkind.N_CAST — use the cast target type
|
||||
//
|
||||
// Conservative: if we can't tell, return false (signed). The cost of
|
||||
// being wrong here is byte-different asm vs C, not bad runtime.
|
||||
fn nodeisunsigned(c: *cgen, n: *node) bool = {
|
||||
if (n == nil) { return false; };
|
||||
let k: i32 = n.kind;
|
||||
if (k == N_IDENT) {
|
||||
if (k == nkind.N_IDENT) {
|
||||
let nm: str = n.str;
|
||||
let lc: *local = localfindnode(c, nm);
|
||||
if (lc != nil) { return typenodeisunsigned(lc.tnode); };
|
||||
return false;
|
||||
};
|
||||
if (k == N_DOT) {
|
||||
if (k == nkind.N_DOT) {
|
||||
let base: *node = n.lhs;
|
||||
let fld: str = n.str;
|
||||
if (base != nil) {
|
||||
if (base.kind == N_IDENT) {
|
||||
if (base.kind == nkind.N_IDENT) {
|
||||
let bn: str = base.str;
|
||||
let lc: *local = localfindnode(c, bn);
|
||||
if (lc != nil) {
|
||||
@@ -526,13 +526,13 @@ fn nodeisunsigned(c: *cgen, n: *node) bool = {
|
||||
if (tn != nil) { lkind = tn.kind; };
|
||||
let sname: str;
|
||||
sname.ptr = nil; sname.len = 0;
|
||||
if (lkind == N_TPTR) {
|
||||
if (lkind == nkind.N_TPTR) {
|
||||
let inner: *node = tn.lhs;
|
||||
if (inner != nil) {
|
||||
if (inner.kind == N_TNAME) { sname = inner.str; };
|
||||
if (inner.kind == nkind.N_TNAME) { sname = inner.str; };
|
||||
};
|
||||
};
|
||||
if (lkind == N_TNAME) { sname = tn.str; };
|
||||
if (lkind == nkind.N_TNAME) { sname = tn.str; };
|
||||
if (sname.len > 0) {
|
||||
let si: *structinfo = structlookup(c, sname);
|
||||
if (si != nil) {
|
||||
@@ -551,29 +551,29 @@ fn nodeisunsigned(c: *cgen, n: *node) bool = {
|
||||
};
|
||||
return false;
|
||||
};
|
||||
if (k == N_CAST) { return typenodeisunsigned(n.rhs); };
|
||||
if (k == N_BIN) {
|
||||
if (k == nkind.N_CAST) { return typenodeisunsigned(n.rhs); };
|
||||
if (k == nkind.N_BIN) {
|
||||
if (nodeisunsigned(c, n.lhs)) { return true; };
|
||||
return nodeisunsigned(c, n.rhs);
|
||||
};
|
||||
if (k == N_UN) { return nodeisunsigned(c, n.lhs); };
|
||||
// N_INDEX: `p[i]` is unsigned iff p's element type is unsigned.
|
||||
if (k == nkind.N_UN) { return nodeisunsigned(c, n.lhs); };
|
||||
// nkind.N_INDEX: `p[i]` is unsigned iff p's element type is unsigned.
|
||||
// Walks the base local's declared type and pulls the element
|
||||
// out — *u8 → u8, [N]u32 → u32, []u64 → u64. Without this the
|
||||
// compare-codegen for `p[i] >= 48u8` falls back to signed JGE
|
||||
// instead of JAE, diverging from C w6c on byte indexing.
|
||||
if (k == N_INDEX) {
|
||||
if (k == nkind.N_INDEX) {
|
||||
let base: *node = n.lhs;
|
||||
if (base != nil) {
|
||||
if (base.kind == N_IDENT) {
|
||||
if (base.kind == nkind.N_IDENT) {
|
||||
let lc: *local = localfindnode(c, base.str);
|
||||
if (lc != nil) {
|
||||
let tn: *node = lc.tnode;
|
||||
if (tn != nil) {
|
||||
let elem: *node = nil;
|
||||
if (tn.kind == N_TPTR) { elem = tn.lhs; };
|
||||
if (tn.kind == N_TARRAY) { elem = tn.lhs; };
|
||||
if (tn.kind == N_TSLICE) { elem = tn.lhs; };
|
||||
if (tn.kind == nkind.N_TPTR) { elem = tn.lhs; };
|
||||
if (tn.kind == nkind.N_TARRAY) { elem = tn.lhs; };
|
||||
if (tn.kind == nkind.N_TSLICE) { elem = tn.lhs; };
|
||||
if (elem != nil) {
|
||||
return typenodeisunsigned(elem);
|
||||
};
|
||||
@@ -601,7 +601,7 @@ fn structlookup(c: *cgen, name: str) *structinfo = {
|
||||
// recognised as a primitive — the caller falls back to other paths).
|
||||
// fldnumidx — parse a tuple field name like "0" / "1" / "12" into an
|
||||
// index, or -1 if not all-digits. Used by cgdot to dispatch
|
||||
// `t.0` / `t.1` against an N_TTUPLE local without pulling in strconv.
|
||||
// `t.0` / `t.1` against an nkind.N_TTUPLE local without pulling in strconv.
|
||||
fn fldnumidx(s: str) i32 = {
|
||||
if (s.len == 0) { return -1; };
|
||||
let r: i32 = 0;
|
||||
@@ -646,14 +646,14 @@ export fn letslotsize(c: *cgen, n: *node) i32 = {
|
||||
// return elem_size * 1 (treating missing length as 1); intercept
|
||||
// and compute the real count first.
|
||||
if (n.lhs != nil) {
|
||||
if (n.lhs.kind == N_TARRAY) {
|
||||
if (n.lhs.kind == nkind.N_TARRAY) {
|
||||
if (n.lhs.rhs == nil) {
|
||||
if (n.rhs != nil) {
|
||||
if (n.rhs.kind == N_ARRLIT) {
|
||||
if (n.rhs.kind == nkind.N_ARRLIT) {
|
||||
let elemn: *node = n.lhs.lhs;
|
||||
let esz: i32 = 8;
|
||||
if (elemn != nil) {
|
||||
if (elemn.kind == N_TNAME) {
|
||||
if (elemn.kind == nkind.N_TNAME) {
|
||||
let ps: i32 = primsize(elemn.str);
|
||||
if (ps > 0) { esz = ps; };
|
||||
};
|
||||
@@ -662,7 +662,7 @@ export fn letslotsize(c: *cgen, n: *node) i32 = {
|
||||
let e: *node = n.rhs.list;
|
||||
for (e != nil) {
|
||||
let adv: bool = true;
|
||||
if (e.kind == N_FIELD) {
|
||||
if (e.kind == nkind.N_FIELD) {
|
||||
if (streq(e.str, "...")) {
|
||||
e = nil;
|
||||
adv = false;
|
||||
@@ -685,11 +685,11 @@ export fn letslotsize(c: *cgen, n: *node) i32 = {
|
||||
fn slotsize(c: *cgen, typn: *node) i32 = {
|
||||
if (typn == nil) { return 8; };
|
||||
let k: i32 = typn.kind;
|
||||
if (k == N_TPTR) { return 8; };
|
||||
if (k == N_TFN) { return 8; };
|
||||
if (k == N_TCHAN) { return 8; };
|
||||
if (k == N_TSLICE) { return 24; };
|
||||
if (k == N_TTUPLE) {
|
||||
if (k == nkind.N_TPTR) { return 8; };
|
||||
if (k == nkind.N_TFN) { return 8; };
|
||||
if (k == nkind.N_TCHAN) { return 8; };
|
||||
if (k == nkind.N_TSLICE) { return 24; };
|
||||
if (k == nkind.N_TTUPLE) {
|
||||
// Sum element sizes. Mirrors C cgen which uses raw type
|
||||
// sizes; padding to 8 happens inside slotsize for primitives,
|
||||
// so a `(i64, str)` resolves to 8 + 16 = 24 (matches the C
|
||||
@@ -702,13 +702,13 @@ fn slotsize(c: *cgen, typn: *node) i32 = {
|
||||
};
|
||||
return total;
|
||||
};
|
||||
if (k == N_TTAGGED){
|
||||
if (k == nkind.N_TTAGGED){
|
||||
// Nullable `(*T | void)` collapses to a single 8B pointer.
|
||||
if (isnullabletype(typn)) { return 8; };
|
||||
// Slot = 8 (tag) + max(variant payload sizes), rounded up
|
||||
// to an 8-byte multiple so the reg-passing ABI (size/8
|
||||
// words) doesn't drop the last value register. Mirrors C
|
||||
// cgen's resolve_type for N_TTAGGED.
|
||||
// cgen's resolve_type for nkind.N_TTAGGED.
|
||||
let v: *node = typn.list;
|
||||
let maxsz: i32 = 0;
|
||||
for (v != nil) {
|
||||
@@ -719,7 +719,7 @@ fn slotsize(c: *cgen, typn: *node) i32 = {
|
||||
let pad: i32 = (maxsz + 7) & ~7;
|
||||
return 8 + pad;
|
||||
};
|
||||
if (k == N_TNAME) {
|
||||
if (k == nkind.N_TNAME) {
|
||||
let nm: str = typn.str;
|
||||
if (streq(nm, "str")) { return 16; };
|
||||
let ps: i32 = primsize(nm);
|
||||
@@ -733,16 +733,16 @@ fn slotsize(c: *cgen, typn: *node) i32 = {
|
||||
if (si != nil) { return si.totsize; };
|
||||
return 8;
|
||||
};
|
||||
if (k == N_TARRAY) {
|
||||
if (k == nkind.N_TARRAY) {
|
||||
let lenn: *node = typn.rhs;
|
||||
let elemn: *node = typn.lhs;
|
||||
let elen: i64 = 1i64;
|
||||
if (lenn != nil) {
|
||||
if (lenn.kind == N_INTLIT) { elen = lenn.uval: i64; };
|
||||
if (lenn.kind == nkind.N_INTLIT) { elen = lenn.uval: i64; };
|
||||
};
|
||||
let esz: i32 = 8;
|
||||
if (elemn != nil) {
|
||||
if (elemn.kind == N_TNAME) {
|
||||
if (elemn.kind == nkind.N_TNAME) {
|
||||
let en: str = elemn.str;
|
||||
let ps: i32 = primsize(en);
|
||||
if (ps > 0) { esz = ps; };
|
||||
@@ -750,12 +750,12 @@ fn slotsize(c: *cgen, typn: *node) i32 = {
|
||||
};
|
||||
return (esz: i64 * elen): i32;
|
||||
};
|
||||
if (k == N_TSTRUCT) {
|
||||
if (k == nkind.N_TSTRUCT) {
|
||||
// Inline anonymous struct — sum of field sizes.
|
||||
let f: *node = typn.list;
|
||||
let total: i32 = 0;
|
||||
for (f != nil) {
|
||||
if (f.kind == N_TFIELD) {
|
||||
if (f.kind == nkind.N_TFIELD) {
|
||||
total += slotsize(c, f.lhs);
|
||||
};
|
||||
f = f.next;
|
||||
@@ -772,7 +772,7 @@ fn slotsize(c: *cgen, typn: *node) i32 = {
|
||||
fn fieldsize(c: *cgen, tnode: *node) i32 = {
|
||||
if (tnode == nil) { return 8; };
|
||||
let k: i32 = tnode.kind;
|
||||
if (k == N_TNAME) {
|
||||
if (k == nkind.N_TNAME) {
|
||||
let nm: str = tnode.str;
|
||||
if (streq(nm, "str")) { return 16; };
|
||||
let ps: i32 = primsize(nm);
|
||||
@@ -781,15 +781,15 @@ fn fieldsize(c: *cgen, tnode: *node) i32 = {
|
||||
if (si != nil) { return si.totsize; };
|
||||
return 8;
|
||||
};
|
||||
if (k == N_TPTR) { return 8; };
|
||||
if (k == N_TSLICE) { return 24; };
|
||||
if (k == N_TARRAY) {
|
||||
if (k == nkind.N_TPTR) { return 8; };
|
||||
if (k == nkind.N_TSLICE) { return 24; };
|
||||
if (k == nkind.N_TARRAY) {
|
||||
// Same shape as slotsize's TARRAY branch.
|
||||
let lenn: *node = tnode.rhs;
|
||||
let elemn: *node = tnode.lhs;
|
||||
let elen: i64 = 1i64;
|
||||
if (lenn != nil) {
|
||||
if (lenn.kind == N_INTLIT) { elen = lenn.uval: i64; };
|
||||
if (lenn.kind == nkind.N_INTLIT) { elen = lenn.uval: i64; };
|
||||
};
|
||||
let esz: i32 = fieldsize(c, elemn);
|
||||
return (esz: i64 * elen): i32;
|
||||
@@ -807,7 +807,7 @@ fn registerstruct(c: *cgen, name: str, tstruct: *node) void = {
|
||||
let off: i32 = 0;
|
||||
let f: *node = tstruct.list;
|
||||
for (f != nil) {
|
||||
if (f.kind == N_TFIELD) {
|
||||
if (f.kind == nkind.N_TFIELD) {
|
||||
let sz: i32 = fieldsize(c, f.lhs);
|
||||
// Align to 8 for any field >= 4 bytes (matches our other
|
||||
// cgen choices). i8/u8/bool may sit on odd byte offsets;
|
||||
@@ -843,10 +843,10 @@ fn collectstructs(c: *cgen, file: *node) void = {
|
||||
if (file == nil) { return; };
|
||||
let d: *node = file.list;
|
||||
for (d != nil) {
|
||||
if (d.kind == N_TYPEDECL) {
|
||||
if (d.kind == nkind.N_TYPEDECL) {
|
||||
let body: *node = d.lhs;
|
||||
if (body != nil) {
|
||||
if (body.kind == N_TSTRUCT) {
|
||||
if (body.kind == nkind.N_TSTRUCT) {
|
||||
registerstruct(c, d.str, body);
|
||||
};
|
||||
};
|
||||
@@ -859,7 +859,7 @@ fn collectstructs(c: *cgen, file: *node) void = {
|
||||
// alias chain registered at file load.
|
||||
fn isstrtyperaw(t: *node) bool = {
|
||||
if (t == nil) { return false; };
|
||||
if (t.kind == N_TNAME) {
|
||||
if (t.kind == nkind.N_TNAME) {
|
||||
let nm: str = t.str;
|
||||
if (streq(nm, "str")) { return true; };
|
||||
};
|
||||
@@ -875,7 +875,7 @@ fn isstrtype(c: *cgen, t: *node) bool = {
|
||||
|
||||
fn isslicetyperaw(t: *node) bool = {
|
||||
if (t == nil) { return false; };
|
||||
if (t.kind == N_TSLICE) { return true; };
|
||||
if (t.kind == nkind.N_TSLICE) { return true; };
|
||||
return false;
|
||||
};
|
||||
|
||||
@@ -888,26 +888,26 @@ fn isslicetype(c: *cgen, t: *node) bool = {
|
||||
|
||||
fn istaggedtype(t: *node) bool = {
|
||||
if (t == nil) { return false; };
|
||||
if (t.kind == N_TTAGGED) { return true; };
|
||||
if (t.kind == nkind.N_TTAGGED) { return true; };
|
||||
return false;
|
||||
};
|
||||
|
||||
// isnullabletype — N_TTAGGED with exactly two children, one *T and
|
||||
// isnullabletype — nkind.N_TTAGGED with exactly two children, one *T and
|
||||
// one `void`. Folds to a single 8-byte pointer slot per Hare's
|
||||
// `(*T | null)` semantics. Mirrors check.c's resolve_type detection.
|
||||
export fn isnullabletype(t: *node) bool = {
|
||||
if (t == nil) { return false; };
|
||||
if (t.kind != N_TTAGGED) { return false; };
|
||||
if (t.kind != nkind.N_TTAGGED) { return false; };
|
||||
let a: *node = t.list;
|
||||
if (a == nil) { return false; };
|
||||
let b: *node = a.next;
|
||||
if (b == nil) { return false; };
|
||||
if (b.next != nil) { return false; };
|
||||
let aptr: bool = (a.kind == N_TPTR);
|
||||
let bptr: bool = (b.kind == N_TPTR);
|
||||
let avoid: bool = (a.kind == N_TNAME);
|
||||
let aptr: bool = (a.kind == nkind.N_TPTR);
|
||||
let bptr: bool = (b.kind == nkind.N_TPTR);
|
||||
let avoid: bool = (a.kind == nkind.N_TNAME);
|
||||
if (avoid) { avoid = streq(a.str, "void"); };
|
||||
let bvoid: bool = (b.kind == N_TNAME);
|
||||
let bvoid: bool = (b.kind == nkind.N_TNAME);
|
||||
if (bvoid) { bvoid = streq(b.str, "void"); };
|
||||
if (aptr) { if (bvoid) { return true; }; };
|
||||
if (avoid) { if (bptr) { return true; }; };
|
||||
@@ -918,9 +918,9 @@ export fn isnullabletype(t: *node) bool = {
|
||||
// union. The void variant takes the other slot (0 or 1).
|
||||
export fn nullableptrtag(t: *node) i32 = {
|
||||
if (t == nil) { return 0; };
|
||||
if (t.kind != N_TTAGGED) { return 0; };
|
||||
if (t.kind != nkind.N_TTAGGED) { return 0; };
|
||||
let a: *node = t.list;
|
||||
if (a != nil) { if (a.kind == N_TPTR) { return 0; }; };
|
||||
if (a != nil) { if (a.kind == nkind.N_TPTR) { return 0; }; };
|
||||
return 1;
|
||||
};
|
||||
|
||||
@@ -929,11 +929,11 @@ export fn nullableptrtag(t: *node) i32 = {
|
||||
// `return;` in a tagged-union-returning fn to the void variant's tag.
|
||||
fn voidvariantindex(tagged: *node) i32 = {
|
||||
if (tagged == nil) { return -1; };
|
||||
if (tagged.kind != N_TTAGGED) { return -1; };
|
||||
if (tagged.kind != nkind.N_TTAGGED) { return -1; };
|
||||
let v: *node = tagged.list;
|
||||
let idx: i32 = 0;
|
||||
for (v != nil) {
|
||||
if (v.kind == N_TNAME) {
|
||||
if (v.kind == nkind.N_TNAME) {
|
||||
if (streq(v.str, "void")) { return idx; };
|
||||
};
|
||||
v = v.next;
|
||||
@@ -949,20 +949,20 @@ fn rhstargetname(c: *cgen, rhs: *node) str = {
|
||||
let nm: str;
|
||||
nm.ptr = nil; nm.len = 0;
|
||||
if (rhs == nil) { return nm; };
|
||||
if (rhs.kind == N_CAST) {
|
||||
if (rhs.kind == nkind.N_CAST) {
|
||||
let t: *node = rhs.rhs;
|
||||
if (t != nil) {
|
||||
if (t.kind == N_TNAME) { return t.str; };
|
||||
if (t.kind == nkind.N_TNAME) { return t.str; };
|
||||
};
|
||||
return nm;
|
||||
};
|
||||
if (rhs.kind == N_STRLIT) { return "str"; };
|
||||
if (rhs.kind == N_IDENT) {
|
||||
if (rhs.kind == nkind.N_STRLIT) { return "str"; };
|
||||
if (rhs.kind == nkind.N_IDENT) {
|
||||
let lc: *local = localfindnode(c, rhs.str);
|
||||
if (lc != nil) {
|
||||
let tn: *node = lc.tnode;
|
||||
if (tn != nil) {
|
||||
if (tn.kind == N_TNAME) { return tn.str; };
|
||||
if (tn.kind == nkind.N_TNAME) { return tn.str; };
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -981,7 +981,7 @@ fn taggedvariantindex(c: *cgen, tagged: *node, rhs: *node) i32 = {
|
||||
let v: *node = tagged.list;
|
||||
let idx: i32 = 0;
|
||||
for (v != nil) {
|
||||
if (v.kind == N_TNAME) {
|
||||
if (v.kind == nkind.N_TNAME) {
|
||||
if (streq(v.str, wantname)) { return idx; };
|
||||
};
|
||||
v = v.next;
|
||||
@@ -994,7 +994,7 @@ fn taggedvariantindex(c: *cgen, tagged: *node, rhs: *node) i32 = {
|
||||
let idx: i32 = 0;
|
||||
for (v != nil) {
|
||||
let visstr: bool = false;
|
||||
if (v.kind == N_TNAME) {
|
||||
if (v.kind == nkind.N_TNAME) {
|
||||
if (isstrtype(c, v)) { visstr = true; };
|
||||
};
|
||||
if (visstr == wantstr) { return idx; };
|
||||
|
||||
Reference in New Issue
Block a user