w6c+selfhost+lib: cgen quality batch + lib Hare-shape graduation
Six fixes across the toolchain, surfaced by lib/lisp porting work.
1. f64 compound assigns (`acc += d`, `-=`, `*=`, `/=`). Both stages
load slot → X1, OP X0 into X1, store back (ADDSD/SUBSD/MULSD/
DIVSD are reg-reg only). Previous MOVSD-overwrite dropped the
OP. Locals and top-level lets.
2. Top-level `[N]u8` arrays + `&arr[i]`. let_emit_size grows a
TY_ARRAY branch so zero-init DATAW lands; cgindex / N_INDEX
store / `&base[i]` all detect a global array base and use
LEAQ name(SB) instead of LEAQ (BP). TK_AMP no longer pre-
evaluates the operand as a value-load — `&base[i]` computes
base + i*esz directly. Unblocks Hare's static-buffer pattern:
strconv.{u64,i64,f64}tos graduate to module-level `*_buf`
arrays and return owned views.
3. Cross-module `pkg.Enum.MEMBER`. Nested N_DOT chains that
don't fold to a known shape now emit `MOVQ <leaf>(SB), AX`
(mirrors the bare-IDENT unresolved fallback), so isolation
probes — and the test 990 cgen-match floor — stay consistent
across stages. strconv exposes `base` as a real `enum i32`;
callers updated. The `main` exemption (linker entry-point
keeps bare name even when not exported) mirrors C-side
collectmods into selfhost cgendecl.
4. Sum-typed parameter ABI. lib/bytes.{index,rindex} take
`(u8 | []u8)` needle; lib/strings.byteindex / rbyteindex take
`(str | rune)` needle (Hare-shaped; the byte-wise misnomer
`index` is dropped). tagged_arg_size cap bumps to 48 (6 int
regs), with a new partial-fit branch on the callee: when an
N-word tagged arg overflows remaining regs, fill what fits and
stitch the rest from positive BP offsets. scanlocals MCASE
handles slice binds (24B) and walks each arm with a saved /
restored seenmark set so two arms naming the same local each
get their own slot — matches cstage's per-arm scope reset.
5. 4-reg tagged-return ABI (AX=tag, DX=word0, CX=word1, R8=word2),
up from 3 regs. Slice-payload variants (`([]T | E)`, slot 32B)
round-trip ptr/len/cap end-to-end. Every receive site updates:
let-init via cgwidentaggedstore, match scrutinee spill, cgindex
tagged-element load (both N_IDENT and fallback bases),
pushargsrev tagged-ident arg (reads word count from slot size),
cgreturn slice variant in the shuffle path.
6. `expr: TaggedAlias` is a widening, not a re-interpret. C cgen +
selfhost cgwidentaggedstore peel an N_CAST whose destination IS
the union — so cgexpr's natural shape (str: AX=ptr, BX=len;
slice: AX=ptr, BX=len, CX=cap) is consumed by the matching
concrete-variant branch instead of being misread as a tagged
AX/DX/CX triple. Inner casts to a concrete variant (`7: i32`)
keep their type for proper tag lookup. `[N]Alias` arrays
resolve element size via slotsize + aliaslookup, and aliaslookup
strips a `pkg.` prefix so cross-module references work.
lib/fmt grows `formattable = (i64 | str | bool | rune)` plus
`printv` / `printlnv` taking an explicit `[]formattable` slice (the
receive side of Hare's `args: formattable...`). Call-site variadic
gather isn't wired — callers either hand-build the slice or compose
strconv.i64tos + strings.concat.
700_e2e: 114 → 123 rows (f64 compound, top-level u8 arrays + `&buf[i]`,
pkg.Enum.MEMBER, sum-typed (str|rune) and (u8|[]u8) params, 4-reg
slice-return ABI, formattable array). 26/26 tests, bootstrap stable
through ww4.
This commit is contained in:
@@ -534,7 +534,7 @@ fn cgindex(c: *cgen, n: *node) void = {
|
||||
let bn: str = base.str;
|
||||
baselocal = localfindnode(c, bn);
|
||||
if (baselocal != nil) {
|
||||
esz = elemsizeof(baselocal.tnode);
|
||||
esz = elemsizeofc(c, baselocal.tnode);
|
||||
signed_elem = elemissigned(baselocal.tnode);
|
||||
} else {
|
||||
let tn: *node = letvartnode(c, bn);
|
||||
@@ -542,13 +542,13 @@ fn cgindex(c: *cgen, n: *node) void = {
|
||||
if (tn.kind == nkind.N_TARRAY) {
|
||||
isglobalarr = true;
|
||||
globalname = bn;
|
||||
esz = elemsizeof(tn);
|
||||
esz = elemsizeofc(c, tn);
|
||||
signed_elem = elemissigned(tn);
|
||||
};
|
||||
if (tn.kind == nkind.N_TPTR) {
|
||||
isglobalptr = true;
|
||||
globalname = bn;
|
||||
esz = elemsizeof(tn);
|
||||
esz = elemsizeofc(c, tn);
|
||||
signed_elem = elemissigned(tn);
|
||||
};
|
||||
};
|
||||
@@ -613,6 +613,9 @@ fn cgindex(c: *cgen, n: *node) void = {
|
||||
};
|
||||
emitline("\tADDQ\tAX, BX\n");
|
||||
if (elem_tagged) {
|
||||
if (elem_slot_sz > 24) {
|
||||
emitline("\tMOVQ\t24(BX), R8\n");
|
||||
};
|
||||
if (elem_slot_sz > 16) {
|
||||
emitline("\tMOVQ\t16(BX), CX\n");
|
||||
};
|
||||
@@ -651,6 +654,9 @@ fn cgindex(c: *cgen, n: *node) void = {
|
||||
};
|
||||
emitline("\tADDQ\tAX, BX\n");
|
||||
if (elem_tagged) {
|
||||
if (elem_slot_sz > 24) {
|
||||
emitline("\tMOVQ\t24(BX), R8\n");
|
||||
};
|
||||
if (elem_slot_sz > 16) {
|
||||
emitline("\tMOVQ\t16(BX), CX\n");
|
||||
};
|
||||
@@ -809,7 +815,7 @@ fn cgmatch(c: *cgen, n: *node) void = {
|
||||
};
|
||||
} else {
|
||||
// Non-ident scrutinee (call result, arr[i], ?, etc.).
|
||||
// Spill into a 24B `@match_spill` scratch slot and
|
||||
// Spill into an `@match_spill` scratch slot and
|
||||
// dispatch off it. Tagged returns (N_CALL) follow the
|
||||
// AX:DX:CX convention; tagged-element loads (N_INDEX)
|
||||
// after the cgindex fix produce the same triple.
|
||||
@@ -864,6 +870,16 @@ fn cgmatch(c: *cgen, n: *node) void = {
|
||||
emitline("\tMOVQ\tCX, ");
|
||||
emitoff((scrutoff + 16): i64);
|
||||
emitline("(BP)\n");
|
||||
// R8 carries the 4th return word when the
|
||||
// scrutinee's tagged union has a slice-payload
|
||||
// variant (slot 32B). Harmless for narrower
|
||||
// returns — R8 is callee-clobbered either way.
|
||||
let ssz: i32 = slotsize(c, scrutt);
|
||||
if (ssz > 24) {
|
||||
emitline("\tMOVQ\tR8, ");
|
||||
emitoff((scrutoff + 24): i64);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -949,25 +965,22 @@ fn cgmatch(c: *cgen, n: *node) void = {
|
||||
};
|
||||
} else {
|
||||
let bsz: i32 = 8;
|
||||
if (isstrtype(c, pat)) { bsz = 16; };
|
||||
if (isstrtype(c, pat)) { bsz = 16; }
|
||||
else { if (isslicetype(c, pat)) { bsz = 24; }; };
|
||||
// localalloc (not localadd): match-arm
|
||||
// binds don't dedup with same-named binds
|
||||
// in *other* matches, since C's cgexpr
|
||||
// allocates a fresh slot per match expr.
|
||||
let voff: i32 = localalloc(c, bn, bsz, pat);
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff((scrutoff + 8): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff(voff: i64);
|
||||
emitline("(BP)\n");
|
||||
if (bsz == 16) {
|
||||
let bw: i32 = 0;
|
||||
for (bw < bsz) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff((scrutoff + 16): i64);
|
||||
emitoff((scrutoff + 8 + bw): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff((voff + 8): i64);
|
||||
emitoff((voff + bw): i64);
|
||||
emitline("(BP)\n");
|
||||
bw += 8;
|
||||
};
|
||||
};
|
||||
};
|
||||
@@ -1589,6 +1602,20 @@ fn cgdot(c: *cgen, n: *node) void = {
|
||||
};};
|
||||
};
|
||||
};
|
||||
// Nested module-qualified field where the chain didn't fold to a
|
||||
// known shape (raw w6c on a single file with `use mod;` but no
|
||||
// driver concatenation — the inner enum / struct hasn't been
|
||||
// seen). Emit `MOVQ <leaf>(SB), AX` so the linker surfaces a
|
||||
// clean undefined-symbol error on the leaf. Mirror of
|
||||
// cmd/w6c/cgen.c N_DOT nested fallback.
|
||||
if (lhs != nil) {
|
||||
if (lhs.kind == nkind.N_DOT) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitsymname(c, fld);
|
||||
emitline("(SB), AX\n");
|
||||
return;
|
||||
};
|
||||
};
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -1617,10 +1644,9 @@ fn cgun(c: *cgen, n: *node) void = {
|
||||
emitline("\t"); emitline(sub); emitline("\tX1, X0\n");
|
||||
return;
|
||||
};
|
||||
cgexpr(c, n.lhs);
|
||||
if (n.op == tkind.TK_MINUS) { emitline("\tNEGQ\tAX\n"); return; };
|
||||
if (n.op == tkind.TK_TILDE) { emitline("\tNOTQ\tAX\n"); return; };
|
||||
if (n.op == tkind.TK_STAR) { emitline("\tMOVQ\t(AX), AX\n"); return; };
|
||||
// Address-of has its own evaluation strategy — we want the address
|
||||
// of the operand, not its value. Special-case here so `&arr[i]`
|
||||
// doesn't compile the value load and then discard it.
|
||||
if (n.op == tkind.TK_AMP) {
|
||||
let opnd: *node = n.lhs;
|
||||
if (opnd != nil) {
|
||||
@@ -1633,17 +1659,94 @@ fn cgun(c: *cgen, n: *node) void = {
|
||||
emitline("(BP), AX\n");
|
||||
return;
|
||||
};
|
||||
// Top-level mutable let — RIP-relative LEAQ.
|
||||
if (isletvar(c, nm)) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, nm);
|
||||
emitline("(SB), AX\n");
|
||||
return;
|
||||
};
|
||||
return;
|
||||
};
|
||||
if (opnd.kind == nkind.N_INDEX) {
|
||||
// &base[i] = base + i*esz, no dereference.
|
||||
let base: *node = opnd.lhs;
|
||||
let idx: *node = opnd.rhs;
|
||||
let esz: i32 = 8;
|
||||
let isglobalarr: bool = false;
|
||||
let isglobalptr: bool = false;
|
||||
let globalname: str;
|
||||
globalname.ptr = nil; globalname.len = 0;
|
||||
let baselocal: *local = nil;
|
||||
let isarr: bool = false;
|
||||
if (base != nil) {
|
||||
if (base.kind == nkind.N_IDENT) {
|
||||
baselocal = localfindnode(c, base.str);
|
||||
if (baselocal != nil) {
|
||||
esz = elemsizeofc(c, baselocal.tnode);
|
||||
let tn: *node = baselocal.tnode;
|
||||
if (tn != nil) {
|
||||
if (tn.kind == nkind.N_TARRAY) { isarr = true; };
|
||||
};
|
||||
} else {
|
||||
let tn: *node = letvartnode(c, base.str);
|
||||
if (tn != nil) {
|
||||
if (tn.kind == nkind.N_TARRAY) {
|
||||
isglobalarr = true;
|
||||
globalname = base.str;
|
||||
esz = elemsizeofc(c, tn);
|
||||
};
|
||||
if (tn.kind == nkind.N_TPTR) {
|
||||
isglobalptr = true;
|
||||
globalname = base.str;
|
||||
esz = elemsizeofc(c, tn);
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
cgexpr(c, idx);
|
||||
if (esz > 1) {
|
||||
emitline("\tMOVQ\t$");
|
||||
emitint(esz: i64);
|
||||
emitline(", CX\n");
|
||||
emitline("\tIMULQ\tCX, AX\n");
|
||||
};
|
||||
if (isglobalarr) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, globalname);
|
||||
emitline("(SB), BX\n");
|
||||
} else { if (isglobalptr) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitsymname(c, globalname);
|
||||
emitline("(SB), BX\n");
|
||||
} else { if (baselocal != nil) {
|
||||
if (isarr) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitoff(baselocal.off: i64);
|
||||
emitline("(BP), BX\n");
|
||||
} else {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(baselocal.off: i64);
|
||||
emitline("(BP), BX\n");
|
||||
};
|
||||
} else {
|
||||
// Complex base: spill scaled idx, eval
|
||||
// base to AX, move to BX, restore idx.
|
||||
emitline("\tPUSHQ\tAX\n");
|
||||
cgexpr(c, base);
|
||||
emitline("\tMOVQ\tAX, BX\n");
|
||||
emitline("\tPOPQ\tAX\n");
|
||||
};};};
|
||||
emitline("\tADDQ\tBX, AX\n");
|
||||
return;
|
||||
};
|
||||
};
|
||||
return;
|
||||
};
|
||||
cgexpr(c, n.lhs);
|
||||
if (n.op == tkind.TK_MINUS) { emitline("\tNEGQ\tAX\n"); return; };
|
||||
if (n.op == tkind.TK_TILDE) { emitline("\tNOTQ\tAX\n"); return; };
|
||||
if (n.op == tkind.TK_STAR) { emitline("\tMOVQ\t(AX), AX\n"); return; };
|
||||
if (n.op == tkind.TK_NOT) {
|
||||
let t: str = mklabel(c, "tt");
|
||||
let e: str = mklabel(c, "te");
|
||||
@@ -2349,7 +2452,7 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
let bn: str = base.str;
|
||||
baselocal = localfindnode(c, bn);
|
||||
if (baselocal != nil) {
|
||||
esz = elemsizeof(baselocal.tnode);
|
||||
esz = elemsizeofc(c, baselocal.tnode);
|
||||
let btn: *node = baselocal.tnode;
|
||||
if (btn != nil) {
|
||||
let bk: nkind = btn.kind;
|
||||
@@ -2363,13 +2466,13 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
if (tn.kind == nkind.N_TARRAY) {
|
||||
isglobalarr = true;
|
||||
globalname = bn;
|
||||
esz = elemsizeof(tn);
|
||||
esz = elemsizeofc(c, tn);
|
||||
elemtn = tn.lhs;
|
||||
};
|
||||
if (tn.kind == nkind.N_TPTR) {
|
||||
isglobalptr = true;
|
||||
globalname = bn;
|
||||
esz = elemsizeof(tn);
|
||||
esz = elemsizeofc(c, tn);
|
||||
elemtn = tn.lhs;
|
||||
};
|
||||
};
|
||||
@@ -3063,16 +3166,55 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
lvf = lvf.lvnext;
|
||||
};
|
||||
};
|
||||
if (isfg && n.op == tkind.TK_ASSIGN) {
|
||||
if (isfg) {
|
||||
cgexpr(c, n.rhs);
|
||||
let mov: str = "MOVSD";
|
||||
if (isf32g) { mov = "MOVSS"; };
|
||||
let addf: str = "ADDSD";
|
||||
let subf: str = "SUBSD";
|
||||
let mulf: str = "MULSD";
|
||||
let divf: str = "DIVSD";
|
||||
if (isf32g) {
|
||||
mov = "MOVSS";
|
||||
addf = "ADDSS";
|
||||
subf = "SUBSS";
|
||||
mulf = "MULSS";
|
||||
divf = "DIVSS";
|
||||
};
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, nm);
|
||||
emitline("(SB), CX\n");
|
||||
if (n.op == tkind.TK_ASSIGN) {
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\tX0, (CX)\n");
|
||||
return;
|
||||
};
|
||||
// Compound: X1 = load; X1 OP= X0; store X1.
|
||||
// ADDSD/SUBSD/MULSD/DIVSD are register-register
|
||||
// only, so we can't combine direct to memory.
|
||||
let fop: str;
|
||||
fop.ptr = nil; fop.len = 0;
|
||||
if (n.op == tkind.TK_PLUSEQ) { fop = addf; };
|
||||
if (n.op == tkind.TK_MINUSEQ) { fop = subf; };
|
||||
if (n.op == tkind.TK_STAREQ) { fop = mulf; };
|
||||
if (n.op == tkind.TK_SLASHEQ) { fop = divf; };
|
||||
if (fop.len == 0) {
|
||||
// Unsupported (e.g., %= on float):
|
||||
// fall back to plain store of rhs.
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\tX0, (CX)\n");
|
||||
return;
|
||||
};
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\tX0, (CX)\n");
|
||||
emitline("\t(CX), X1\n");
|
||||
emitline("\t");
|
||||
emitline(fop);
|
||||
emitline("\tX0, X1\n");
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\tX1, (CX)\n");
|
||||
return;
|
||||
};
|
||||
cgexpr(c, n.rhs);
|
||||
@@ -3151,15 +3293,56 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
lcf32 = isf32type(c, lcn.tnode);
|
||||
};
|
||||
// Float-typed local: rhs lands in X0; store via MOVSD/
|
||||
// MOVSS, no AX shuffle. Only plain `=` is wired; compound
|
||||
// float-assign isn't.
|
||||
if (lcf && n.op == tkind.TK_ASSIGN) {
|
||||
// MOVSS, no AX shuffle. Compound (+= -= *= /=) loads
|
||||
// slot into X1, combines into X1, stores X1 back —
|
||||
// ADDSD/SUBSD/MULSD/DIVSD are register-register only.
|
||||
if (lcf) {
|
||||
cgexpr(c, n.rhs);
|
||||
let mov: str = "MOVSD";
|
||||
if (lcf32) { mov = "MOVSS"; };
|
||||
let addf: str = "ADDSD";
|
||||
let subf: str = "SUBSD";
|
||||
let mulf: str = "MULSD";
|
||||
let divf: str = "DIVSD";
|
||||
if (lcf32) {
|
||||
mov = "MOVSS";
|
||||
addf = "ADDSS";
|
||||
subf = "SUBSS";
|
||||
mulf = "MULSS";
|
||||
divf = "DIVSS";
|
||||
};
|
||||
if (n.op == tkind.TK_ASSIGN) {
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\tX0, ");
|
||||
emitoff(off: i64);
|
||||
emitline("(BP)\n");
|
||||
return;
|
||||
};
|
||||
let fop: str;
|
||||
fop.ptr = nil; fop.len = 0;
|
||||
if (n.op == tkind.TK_PLUSEQ) { fop = addf; };
|
||||
if (n.op == tkind.TK_MINUSEQ) { fop = subf; };
|
||||
if (n.op == tkind.TK_STAREQ) { fop = mulf; };
|
||||
if (n.op == tkind.TK_SLASHEQ) { fop = divf; };
|
||||
if (fop.len == 0) {
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\tX0, ");
|
||||
emitoff(off: i64);
|
||||
emitline("(BP)\n");
|
||||
return;
|
||||
};
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\tX0, ");
|
||||
emitline("\t");
|
||||
emitoff(off: i64);
|
||||
emitline("(BP), X1\n");
|
||||
emitline("\t");
|
||||
emitline(fop);
|
||||
emitline("\tX0, X1\n");
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\tX1, ");
|
||||
emitoff(off: i64);
|
||||
emitline("(BP)\n");
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user