w6c+selfhost: cg_widen_tagged_store basereg + N_ASSIGN tagged field (closes #26)
Extended cg_widen_tagged_store (cstage) / cgwidentaggedstore (wwstage) to take a base_reg/basereg parameter so the primitive supports non-BP destinations. Cstage extends body in-place via via_outer gate + spill+scratch+copy-out; wwstage splits into wrapper (non-BP) + cgwidentaggedstorebp (BP-only) to dodge the no-goto constraint. New N_ASSIGN field TY_TAGGED branch routes through the primitive for all rhs shapes. Scope-adjacent: fieldsize recurses through N_TTAGGED via slotsize and TNAME-aliased-to-tagged via aliaslookup. Needed for the test fixtures. Wwstage read-side N_DOT-of-tagged-field source is filed as task #28; test rows use mark-canary verification until that lands.
This commit is contained in:
@@ -5744,7 +5744,7 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
|
||||
emitline("(BP)\n");
|
||||
zz += 8;
|
||||
};
|
||||
cgwidentaggedstore(c, ptype, arg, scroff, widensz);
|
||||
cgwidentaggedstore(c, ptype, arg, "BP", scroff, widensz);
|
||||
let pp: i32 = widensz - 8;
|
||||
for (pp >= 0) {
|
||||
emitline("\tMOVQ\t");
|
||||
@@ -6938,6 +6938,7 @@ fn slotsize(c: *cgen, typn: *node) i32 = {
|
||||
fn fieldsize(c: *cgen, tnode: *node) i32 = {
|
||||
if (tnode == nil) { return 8; };
|
||||
let k: nkind = tnode.kind;
|
||||
if (k == nkind.N_TTAGGED){ return slotsize(c, tnode); };
|
||||
if (k == nkind.N_TNAME) {
|
||||
let nm: str = tnode.str;
|
||||
if (streq(nm, "str")) { return 16; };
|
||||
@@ -6957,6 +6958,13 @@ fn fieldsize(c: *cgen, tnode: *node) i32 = {
|
||||
};
|
||||
return 4; // default storage is i32
|
||||
};
|
||||
// Type alias to a tagged-union — recurse through aliaslookup
|
||||
// so `e: ev` (where `ev = (i64 | i32)`) takes 16B in the
|
||||
// containing struct rather than the 8B default.
|
||||
if (c != nil) {
|
||||
let aliased: *node = aliaslookup(c, nm);
|
||||
if (aliased != nil) { return fieldsize(c, aliased); };
|
||||
};
|
||||
return 8;
|
||||
};
|
||||
if (k == nkind.N_TPTR) { return 8; };
|
||||
@@ -7633,9 +7641,20 @@ fn rhstaggedabicall(c: *cgen, src: *node) bool = {
|
||||
return false;
|
||||
};
|
||||
|
||||
// cgwidentaggedstore — write tagged-union slot bytes for `src` into the
|
||||
// slot at BP+slot_off, sized to slot_sz. Mirrors cg_widen_tagged_store
|
||||
// in cmd/w6c/cgen.c. Branches by source shape:
|
||||
// cgwidentaggedstore — write tagged-union slot bytes for `src` into
|
||||
// the slot at `basereg`+slot_off, sized to slot_sz. Mirrors
|
||||
// cg_widen_tagged_store in cmd/w6c/cgen.c.
|
||||
//
|
||||
// `basereg` selects the addressing root:
|
||||
// - "BP": function-frame slot (let / assign / return / structlit /
|
||||
// array-elem scratch). Body writes straight to slot_off(BP).
|
||||
// - else (e.g. "BX" for *struct field, top-level struct LEAQ
|
||||
// base): pointer-rooted dst. cgexpr inside trashes every GPR,
|
||||
// so we route through a fresh BP-rooted scratch slot, spill
|
||||
// basereg before the body, reload after, then word-copy
|
||||
// scratch → (basereg, slot_off).
|
||||
//
|
||||
// Branches by source shape:
|
||||
// - nullable dst (8B slot): cgexpr → AX → slot+0.
|
||||
// - tagged src ident: copy slot words, zero-pad, tag-remap.
|
||||
// - tagged src via AX/DX/CX ABI (call / tagged-arr index): cgexpr,
|
||||
@@ -7646,7 +7665,53 @@ fn rhstaggedabicall(c: *cgen, src: *node) bool = {
|
||||
// tag last.
|
||||
// - str src: tag@+0, ptr@+8, len@+16.
|
||||
// - scalar src: tag@+0, value@+8.
|
||||
fn cgwidentaggedstore(c: *cgen, dst: *node, src: *node, slot_off: i32, slot_sz: i32) void = {
|
||||
fn cgwidentaggedstore(c: *cgen, dst: *node, src: *node,
|
||||
basereg: str, slot_off: i32, slot_sz: i32) void = {
|
||||
if (streq(basereg, "BP")) {
|
||||
cgwidentaggedstorebp(c, dst, src, slot_off, slot_sz);
|
||||
return;
|
||||
};
|
||||
// Pointer-rooted dst: spill basereg (cgexpr will trash it),
|
||||
// materialise into a BP-rooted scratch via the BP path, then
|
||||
// reload basereg and word-copy scratch → caller's slot.
|
||||
let bspill: i32 = localadd(c, "@tagbase", 8, nil);
|
||||
emitline("\tMOVQ\t");
|
||||
emitline(basereg);
|
||||
emitline(", ");
|
||||
emitoff(bspill: i64);
|
||||
emitline("(BP)\n");
|
||||
let scr: i32 = localadd(c, "@tagscr", slot_sz, nil);
|
||||
emitline("\tXORQ\tAX, AX\n");
|
||||
let z: i32 = 0;
|
||||
for (z < slot_sz) {
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff((scr + z): i64);
|
||||
emitline("(BP)\n");
|
||||
z += 8;
|
||||
};
|
||||
cgwidentaggedstorebp(c, dst, src, scr, slot_sz);
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(bspill: i64);
|
||||
emitline("(BP), ");
|
||||
emitline(basereg);
|
||||
emitline("\n");
|
||||
let k: i32 = 0;
|
||||
for (k < slot_sz) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff((scr + k): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitdispreg((slot_off + k): i64, basereg);
|
||||
emitline("\n");
|
||||
k += 8;
|
||||
};
|
||||
};
|
||||
|
||||
// cgwidentaggedstorebp — BP-rooted body. Called via cgwidentaggedstore
|
||||
// for the natural "BP" case and via the wrapper's scratch path for
|
||||
// pointer-rooted dst. Direct callers exist only in case of future
|
||||
// inlined uses inside this file; new code should call the wrapper.
|
||||
fn cgwidentaggedstorebp(c: *cgen, dst: *node, src: *node, slot_off: i32, slot_sz: i32) void = {
|
||||
let dt: *node = resolvetagged(c, dst);
|
||||
if (dt == nil) { return; };
|
||||
// Nullable fold: one 8B word holding the pointer (or 0 for void).
|
||||
@@ -10793,7 +10858,7 @@ fn cgcall(c: *cgen, n: *node) void = {
|
||||
let slot: i32 = doff + j * esz;
|
||||
if (velemtagged) {
|
||||
cgwidentaggedstore(c, varp.lhs,
|
||||
aa2, slot, esz);
|
||||
aa2, "BP", slot, esz);
|
||||
} else { if (velemstr) {
|
||||
cgexpr(c, aa2);
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
@@ -11049,7 +11114,7 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
if (istaggedtype(c, lc.tnode)) {
|
||||
let lsz: i32 = slotsize(c, lc.tnode);
|
||||
cgwidentaggedstore(c, lc.tnode,
|
||||
n.rhs, lc.off, lsz);
|
||||
n.rhs, "BP", lc.off, lsz);
|
||||
return;
|
||||
};
|
||||
};
|
||||
@@ -11273,7 +11338,7 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
zz += 8;
|
||||
};
|
||||
cgwidentaggedstore(c, elemtn, n.rhs,
|
||||
scroff, slot_sz);
|
||||
"BP", scroff, slot_sz);
|
||||
cgexpr(c, idx);
|
||||
if (slot_sz > 1) {
|
||||
emitline("\tMOVQ\t$");
|
||||
@@ -11619,6 +11684,20 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
for (fi != nil) {
|
||||
let fn_: str = fi.fname;
|
||||
if (streq(fn_, fld)) {
|
||||
// Tagged-union field via *struct base — full slot
|
||||
// rewrite via cgwidentaggedstore basereg="BX". Pre-#26
|
||||
// fell through to the scalar store and dropped tag
|
||||
// + payload.
|
||||
if (n.op == tkind.TK_ASSIGN
|
||||
&& istaggedtype(c, fi.tnode)) {
|
||||
let fsz: i32 = slotsize(c, fi.tnode);
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(lc.off: i64);
|
||||
emitline("(BP), BX\n");
|
||||
cgwidentaggedstore(c, fi.tnode,
|
||||
n.rhs, "BX", fi.foff, fsz);
|
||||
return;
|
||||
};
|
||||
// struct-typed field, struct-ident rhs through
|
||||
// *struct base: cgexpr can't materialise a whole
|
||||
// struct value, so word-copy from the rhs slot
|
||||
@@ -11769,6 +11848,17 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
for (fi != nil) {
|
||||
let fn_: str = fi.fname;
|
||||
if (streq(fn_, fld)) {
|
||||
// Tagged-union field in a direct struct local —
|
||||
// full slot rewrite at (lc.off + fi.foff)(BP)
|
||||
// via cgwidentaggedstore basereg="BP". Pre-#26
|
||||
// fell through and dropped tag + payload.
|
||||
if (n.op == tkind.TK_ASSIGN
|
||||
&& istaggedtype(c, fi.tnode)) {
|
||||
let fsz: i32 = slotsize(c, fi.tnode);
|
||||
cgwidentaggedstore(c, fi.tnode,
|
||||
n.rhs, "BP", lc.off + fi.foff, fsz);
|
||||
return;
|
||||
};
|
||||
// struct-typed field, struct-ident rhs:
|
||||
// word-copy direct, skipping cgexpr (no
|
||||
// register convention for a whole struct
|
||||
@@ -12978,8 +13068,8 @@ fn cgreturn(c: *cgen, n: *node) void = {
|
||||
emitline("(BP)\n");
|
||||
zz += 8;
|
||||
};
|
||||
cgwidentaggedstore(c, c.fnret, rhs, scroff,
|
||||
rsz);
|
||||
cgwidentaggedstore(c, c.fnret, rhs, "BP",
|
||||
scroff, rsz);
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(scroff: i64);
|
||||
emitline("(BP), AX\n");
|
||||
@@ -13101,7 +13191,7 @@ fn cglet(c: *cgen, n: *node) void = {
|
||||
// ABI call), struct payload (literal/ident), str payload,
|
||||
// scalar payload — with tag remap for tagged-subset widening.
|
||||
if (istaggedtype(c, tn)) {
|
||||
cgwidentaggedstore(c, tn, rhs, off, sz);
|
||||
cgwidentaggedstore(c, tn, rhs, "BP", off, sz);
|
||||
c.lastwasreturn = 0;
|
||||
return;
|
||||
};
|
||||
@@ -14118,6 +14208,61 @@ fn scanlocals(c: *cgen, n: *node) i32 = {
|
||||
};
|
||||
};
|
||||
};
|
||||
// Tagged-union struct-field write: `s.f = v` or `(*p).f = v`
|
||||
// where f is a tagged-union field. cgassign delegates to
|
||||
// cgwidentaggedstore; for pointer-rooted dst the wrapper
|
||||
// allocates @tagbase (8B) and @tagscr (slot_sz). Both names
|
||||
// dedup with other tagged scratch users in the same function.
|
||||
if (n.kind == nkind.N_ASSIGN) {
|
||||
let alhs: *node = n.lhs;
|
||||
if (alhs != nil) {
|
||||
if (alhs.kind == nkind.N_DOT) {
|
||||
let abase: *node = alhs.lhs;
|
||||
if (abase != nil) {
|
||||
if (abase.kind == nkind.N_IDENT) {
|
||||
let lc: *local = localfindnode(c, abase.str);
|
||||
let btn: *node = nil;
|
||||
if (lc != nil) { btn = lc.tnode; }
|
||||
else { btn = letvartnode(c, abase.str); };
|
||||
let isptr: bool = false;
|
||||
let stype: *node = nil;
|
||||
if (btn != nil) {
|
||||
if (btn.kind == nkind.N_TPTR) {
|
||||
isptr = true;
|
||||
stype = btn.lhs;
|
||||
};
|
||||
if (btn.kind == nkind.N_TNAME) { stype = btn; };
|
||||
};
|
||||
if (stype != nil) {
|
||||
if (stype.kind == nkind.N_TNAME) {
|
||||
let si: *structinfo = structlookup(c, stype.str);
|
||||
if (si != nil) {
|
||||
let fi: *fieldinfo = si.fields;
|
||||
for (fi != nil) {
|
||||
if (streq(fi.fname, alhs.str)) {
|
||||
if (istaggedtype(c, fi.tnode)) {
|
||||
if (isptr) {
|
||||
if (!scanseenmark(c, "@tagbase")) {
|
||||
total += 8;
|
||||
};
|
||||
if (!scanseenmark(c, "@tagscr")) {
|
||||
total += 24;
|
||||
};
|
||||
};
|
||||
};
|
||||
fi = nil;
|
||||
} else {
|
||||
fi = fi.finext;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
// Tagged-union return with struct payload or tagged-subset
|
||||
// source — cgreturn materialises in @tagscr then loads
|
||||
// AX/DX/CX. Detect via the same rhsstructpayload predicate
|
||||
|
||||
@@ -204,6 +204,61 @@ fn scanlocals(c: *cgen, n: *node) i32 = {
|
||||
};
|
||||
};
|
||||
};
|
||||
// Tagged-union struct-field write: `s.f = v` or `(*p).f = v`
|
||||
// where f is a tagged-union field. cgassign delegates to
|
||||
// cgwidentaggedstore; for pointer-rooted dst the wrapper
|
||||
// allocates @tagbase (8B) and @tagscr (slot_sz). Both names
|
||||
// dedup with other tagged scratch users in the same function.
|
||||
if (n.kind == nkind.N_ASSIGN) {
|
||||
let alhs: *node = n.lhs;
|
||||
if (alhs != nil) {
|
||||
if (alhs.kind == nkind.N_DOT) {
|
||||
let abase: *node = alhs.lhs;
|
||||
if (abase != nil) {
|
||||
if (abase.kind == nkind.N_IDENT) {
|
||||
let lc: *local = localfindnode(c, abase.str);
|
||||
let btn: *node = nil;
|
||||
if (lc != nil) { btn = lc.tnode; }
|
||||
else { btn = letvartnode(c, abase.str); };
|
||||
let isptr: bool = false;
|
||||
let stype: *node = nil;
|
||||
if (btn != nil) {
|
||||
if (btn.kind == nkind.N_TPTR) {
|
||||
isptr = true;
|
||||
stype = btn.lhs;
|
||||
};
|
||||
if (btn.kind == nkind.N_TNAME) { stype = btn; };
|
||||
};
|
||||
if (stype != nil) {
|
||||
if (stype.kind == nkind.N_TNAME) {
|
||||
let si: *structinfo = structlookup(c, stype.str);
|
||||
if (si != nil) {
|
||||
let fi: *fieldinfo = si.fields;
|
||||
for (fi != nil) {
|
||||
if (streq(fi.fname, alhs.str)) {
|
||||
if (istaggedtype(c, fi.tnode)) {
|
||||
if (isptr) {
|
||||
if (!scanseenmark(c, "@tagbase")) {
|
||||
total += 8;
|
||||
};
|
||||
if (!scanseenmark(c, "@tagscr")) {
|
||||
total += 24;
|
||||
};
|
||||
};
|
||||
};
|
||||
fi = nil;
|
||||
} else {
|
||||
fi = fi.finext;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
// Tagged-union return with struct payload or tagged-subset
|
||||
// source — cgreturn materialises in @tagscr then loads
|
||||
// AX/DX/CX. Detect via the same rhsstructpayload predicate
|
||||
|
||||
@@ -2745,7 +2745,7 @@ fn cgcall(c: *cgen, n: *node) void = {
|
||||
let slot: i32 = doff + j * esz;
|
||||
if (velemtagged) {
|
||||
cgwidentaggedstore(c, varp.lhs,
|
||||
aa2, slot, esz);
|
||||
aa2, "BP", slot, esz);
|
||||
} else { if (velemstr) {
|
||||
cgexpr(c, aa2);
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
@@ -3001,7 +3001,7 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
if (istaggedtype(c, lc.tnode)) {
|
||||
let lsz: i32 = slotsize(c, lc.tnode);
|
||||
cgwidentaggedstore(c, lc.tnode,
|
||||
n.rhs, lc.off, lsz);
|
||||
n.rhs, "BP", lc.off, lsz);
|
||||
return;
|
||||
};
|
||||
};
|
||||
@@ -3225,7 +3225,7 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
zz += 8;
|
||||
};
|
||||
cgwidentaggedstore(c, elemtn, n.rhs,
|
||||
scroff, slot_sz);
|
||||
"BP", scroff, slot_sz);
|
||||
cgexpr(c, idx);
|
||||
if (slot_sz > 1) {
|
||||
emitline("\tMOVQ\t$");
|
||||
@@ -3571,6 +3571,20 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
for (fi != nil) {
|
||||
let fn_: str = fi.fname;
|
||||
if (streq(fn_, fld)) {
|
||||
// Tagged-union field via *struct base — full slot
|
||||
// rewrite via cgwidentaggedstore basereg="BX". Pre-#26
|
||||
// fell through to the scalar store and dropped tag
|
||||
// + payload.
|
||||
if (n.op == tkind.TK_ASSIGN
|
||||
&& istaggedtype(c, fi.tnode)) {
|
||||
let fsz: i32 = slotsize(c, fi.tnode);
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(lc.off: i64);
|
||||
emitline("(BP), BX\n");
|
||||
cgwidentaggedstore(c, fi.tnode,
|
||||
n.rhs, "BX", fi.foff, fsz);
|
||||
return;
|
||||
};
|
||||
// struct-typed field, struct-ident rhs through
|
||||
// *struct base: cgexpr can't materialise a whole
|
||||
// struct value, so word-copy from the rhs slot
|
||||
@@ -3721,6 +3735,17 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
for (fi != nil) {
|
||||
let fn_: str = fi.fname;
|
||||
if (streq(fn_, fld)) {
|
||||
// Tagged-union field in a direct struct local —
|
||||
// full slot rewrite at (lc.off + fi.foff)(BP)
|
||||
// via cgwidentaggedstore basereg="BP". Pre-#26
|
||||
// fell through and dropped tag + payload.
|
||||
if (n.op == tkind.TK_ASSIGN
|
||||
&& istaggedtype(c, fi.tnode)) {
|
||||
let fsz: i32 = slotsize(c, fi.tnode);
|
||||
cgwidentaggedstore(c, fi.tnode,
|
||||
n.rhs, "BP", lc.off + fi.foff, fsz);
|
||||
return;
|
||||
};
|
||||
// struct-typed field, struct-ident rhs:
|
||||
// word-copy direct, skipping cgexpr (no
|
||||
// register convention for a whole struct
|
||||
|
||||
@@ -188,8 +188,8 @@ fn cgreturn(c: *cgen, n: *node) void = {
|
||||
emitline("(BP)\n");
|
||||
zz += 8;
|
||||
};
|
||||
cgwidentaggedstore(c, c.fnret, rhs, scroff,
|
||||
rsz);
|
||||
cgwidentaggedstore(c, c.fnret, rhs, "BP",
|
||||
scroff, rsz);
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(scroff: i64);
|
||||
emitline("(BP), AX\n");
|
||||
@@ -311,7 +311,7 @@ fn cglet(c: *cgen, n: *node) void = {
|
||||
// ABI call), struct payload (literal/ident), str payload,
|
||||
// scalar payload — with tag remap for tagged-subset widening.
|
||||
if (istaggedtype(c, tn)) {
|
||||
cgwidentaggedstore(c, tn, rhs, off, sz);
|
||||
cgwidentaggedstore(c, tn, rhs, "BP", off, sz);
|
||||
c.lastwasreturn = 0;
|
||||
return;
|
||||
};
|
||||
|
||||
@@ -174,7 +174,7 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
|
||||
emitline("(BP)\n");
|
||||
zz += 8;
|
||||
};
|
||||
cgwidentaggedstore(c, ptype, arg, scroff, widensz);
|
||||
cgwidentaggedstore(c, ptype, arg, "BP", scroff, widensz);
|
||||
let pp: i32 = widensz - 8;
|
||||
for (pp >= 0) {
|
||||
emitline("\tMOVQ\t");
|
||||
@@ -1368,6 +1368,7 @@ fn slotsize(c: *cgen, typn: *node) i32 = {
|
||||
fn fieldsize(c: *cgen, tnode: *node) i32 = {
|
||||
if (tnode == nil) { return 8; };
|
||||
let k: nkind = tnode.kind;
|
||||
if (k == nkind.N_TTAGGED){ return slotsize(c, tnode); };
|
||||
if (k == nkind.N_TNAME) {
|
||||
let nm: str = tnode.str;
|
||||
if (streq(nm, "str")) { return 16; };
|
||||
@@ -1387,6 +1388,13 @@ fn fieldsize(c: *cgen, tnode: *node) i32 = {
|
||||
};
|
||||
return 4; // default storage is i32
|
||||
};
|
||||
// Type alias to a tagged-union — recurse through aliaslookup
|
||||
// so `e: ev` (where `ev = (i64 | i32)`) takes 16B in the
|
||||
// containing struct rather than the 8B default.
|
||||
if (c != nil) {
|
||||
let aliased: *node = aliaslookup(c, nm);
|
||||
if (aliased != nil) { return fieldsize(c, aliased); };
|
||||
};
|
||||
return 8;
|
||||
};
|
||||
if (k == nkind.N_TPTR) { return 8; };
|
||||
@@ -2063,9 +2071,20 @@ fn rhstaggedabicall(c: *cgen, src: *node) bool = {
|
||||
return false;
|
||||
};
|
||||
|
||||
// cgwidentaggedstore — write tagged-union slot bytes for `src` into the
|
||||
// slot at BP+slot_off, sized to slot_sz. Mirrors cg_widen_tagged_store
|
||||
// in cmd/w6c/cgen.c. Branches by source shape:
|
||||
// cgwidentaggedstore — write tagged-union slot bytes for `src` into
|
||||
// the slot at `basereg`+slot_off, sized to slot_sz. Mirrors
|
||||
// cg_widen_tagged_store in cmd/w6c/cgen.c.
|
||||
//
|
||||
// `basereg` selects the addressing root:
|
||||
// - "BP": function-frame slot (let / assign / return / structlit /
|
||||
// array-elem scratch). Body writes straight to slot_off(BP).
|
||||
// - else (e.g. "BX" for *struct field, top-level struct LEAQ
|
||||
// base): pointer-rooted dst. cgexpr inside trashes every GPR,
|
||||
// so we route through a fresh BP-rooted scratch slot, spill
|
||||
// basereg before the body, reload after, then word-copy
|
||||
// scratch → (basereg, slot_off).
|
||||
//
|
||||
// Branches by source shape:
|
||||
// - nullable dst (8B slot): cgexpr → AX → slot+0.
|
||||
// - tagged src ident: copy slot words, zero-pad, tag-remap.
|
||||
// - tagged src via AX/DX/CX ABI (call / tagged-arr index): cgexpr,
|
||||
@@ -2076,7 +2095,53 @@ fn rhstaggedabicall(c: *cgen, src: *node) bool = {
|
||||
// tag last.
|
||||
// - str src: tag@+0, ptr@+8, len@+16.
|
||||
// - scalar src: tag@+0, value@+8.
|
||||
fn cgwidentaggedstore(c: *cgen, dst: *node, src: *node, slot_off: i32, slot_sz: i32) void = {
|
||||
fn cgwidentaggedstore(c: *cgen, dst: *node, src: *node,
|
||||
basereg: str, slot_off: i32, slot_sz: i32) void = {
|
||||
if (streq(basereg, "BP")) {
|
||||
cgwidentaggedstorebp(c, dst, src, slot_off, slot_sz);
|
||||
return;
|
||||
};
|
||||
// Pointer-rooted dst: spill basereg (cgexpr will trash it),
|
||||
// materialise into a BP-rooted scratch via the BP path, then
|
||||
// reload basereg and word-copy scratch → caller's slot.
|
||||
let bspill: i32 = localadd(c, "@tagbase", 8, nil);
|
||||
emitline("\tMOVQ\t");
|
||||
emitline(basereg);
|
||||
emitline(", ");
|
||||
emitoff(bspill: i64);
|
||||
emitline("(BP)\n");
|
||||
let scr: i32 = localadd(c, "@tagscr", slot_sz, nil);
|
||||
emitline("\tXORQ\tAX, AX\n");
|
||||
let z: i32 = 0;
|
||||
for (z < slot_sz) {
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff((scr + z): i64);
|
||||
emitline("(BP)\n");
|
||||
z += 8;
|
||||
};
|
||||
cgwidentaggedstorebp(c, dst, src, scr, slot_sz);
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(bspill: i64);
|
||||
emitline("(BP), ");
|
||||
emitline(basereg);
|
||||
emitline("\n");
|
||||
let k: i32 = 0;
|
||||
for (k < slot_sz) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff((scr + k): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitdispreg((slot_off + k): i64, basereg);
|
||||
emitline("\n");
|
||||
k += 8;
|
||||
};
|
||||
};
|
||||
|
||||
// cgwidentaggedstorebp — BP-rooted body. Called via cgwidentaggedstore
|
||||
// for the natural "BP" case and via the wrapper's scratch path for
|
||||
// pointer-rooted dst. Direct callers exist only in case of future
|
||||
// inlined uses inside this file; new code should call the wrapper.
|
||||
fn cgwidentaggedstorebp(c: *cgen, dst: *node, src: *node, slot_off: i32, slot_sz: i32) void = {
|
||||
let dt: *node = resolvetagged(c, dst);
|
||||
if (dt == nil) { return; };
|
||||
// Nullable fold: one 8B word holding the pointer (or 0 for void).
|
||||
|
||||
@@ -5744,7 +5744,7 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
|
||||
emitline("(BP)\n");
|
||||
zz += 8;
|
||||
};
|
||||
cgwidentaggedstore(c, ptype, arg, scroff, widensz);
|
||||
cgwidentaggedstore(c, ptype, arg, "BP", scroff, widensz);
|
||||
let pp: i32 = widensz - 8;
|
||||
for (pp >= 0) {
|
||||
emitline("\tMOVQ\t");
|
||||
@@ -6938,6 +6938,7 @@ fn slotsize(c: *cgen, typn: *node) i32 = {
|
||||
fn fieldsize(c: *cgen, tnode: *node) i32 = {
|
||||
if (tnode == nil) { return 8; };
|
||||
let k: nkind = tnode.kind;
|
||||
if (k == nkind.N_TTAGGED){ return slotsize(c, tnode); };
|
||||
if (k == nkind.N_TNAME) {
|
||||
let nm: str = tnode.str;
|
||||
if (streq(nm, "str")) { return 16; };
|
||||
@@ -6957,6 +6958,13 @@ fn fieldsize(c: *cgen, tnode: *node) i32 = {
|
||||
};
|
||||
return 4; // default storage is i32
|
||||
};
|
||||
// Type alias to a tagged-union — recurse through aliaslookup
|
||||
// so `e: ev` (where `ev = (i64 | i32)`) takes 16B in the
|
||||
// containing struct rather than the 8B default.
|
||||
if (c != nil) {
|
||||
let aliased: *node = aliaslookup(c, nm);
|
||||
if (aliased != nil) { return fieldsize(c, aliased); };
|
||||
};
|
||||
return 8;
|
||||
};
|
||||
if (k == nkind.N_TPTR) { return 8; };
|
||||
@@ -7633,9 +7641,20 @@ fn rhstaggedabicall(c: *cgen, src: *node) bool = {
|
||||
return false;
|
||||
};
|
||||
|
||||
// cgwidentaggedstore — write tagged-union slot bytes for `src` into the
|
||||
// slot at BP+slot_off, sized to slot_sz. Mirrors cg_widen_tagged_store
|
||||
// in cmd/w6c/cgen.c. Branches by source shape:
|
||||
// cgwidentaggedstore — write tagged-union slot bytes for `src` into
|
||||
// the slot at `basereg`+slot_off, sized to slot_sz. Mirrors
|
||||
// cg_widen_tagged_store in cmd/w6c/cgen.c.
|
||||
//
|
||||
// `basereg` selects the addressing root:
|
||||
// - "BP": function-frame slot (let / assign / return / structlit /
|
||||
// array-elem scratch). Body writes straight to slot_off(BP).
|
||||
// - else (e.g. "BX" for *struct field, top-level struct LEAQ
|
||||
// base): pointer-rooted dst. cgexpr inside trashes every GPR,
|
||||
// so we route through a fresh BP-rooted scratch slot, spill
|
||||
// basereg before the body, reload after, then word-copy
|
||||
// scratch → (basereg, slot_off).
|
||||
//
|
||||
// Branches by source shape:
|
||||
// - nullable dst (8B slot): cgexpr → AX → slot+0.
|
||||
// - tagged src ident: copy slot words, zero-pad, tag-remap.
|
||||
// - tagged src via AX/DX/CX ABI (call / tagged-arr index): cgexpr,
|
||||
@@ -7646,7 +7665,53 @@ fn rhstaggedabicall(c: *cgen, src: *node) bool = {
|
||||
// tag last.
|
||||
// - str src: tag@+0, ptr@+8, len@+16.
|
||||
// - scalar src: tag@+0, value@+8.
|
||||
fn cgwidentaggedstore(c: *cgen, dst: *node, src: *node, slot_off: i32, slot_sz: i32) void = {
|
||||
fn cgwidentaggedstore(c: *cgen, dst: *node, src: *node,
|
||||
basereg: str, slot_off: i32, slot_sz: i32) void = {
|
||||
if (streq(basereg, "BP")) {
|
||||
cgwidentaggedstorebp(c, dst, src, slot_off, slot_sz);
|
||||
return;
|
||||
};
|
||||
// Pointer-rooted dst: spill basereg (cgexpr will trash it),
|
||||
// materialise into a BP-rooted scratch via the BP path, then
|
||||
// reload basereg and word-copy scratch → caller's slot.
|
||||
let bspill: i32 = localadd(c, "@tagbase", 8, nil);
|
||||
emitline("\tMOVQ\t");
|
||||
emitline(basereg);
|
||||
emitline(", ");
|
||||
emitoff(bspill: i64);
|
||||
emitline("(BP)\n");
|
||||
let scr: i32 = localadd(c, "@tagscr", slot_sz, nil);
|
||||
emitline("\tXORQ\tAX, AX\n");
|
||||
let z: i32 = 0;
|
||||
for (z < slot_sz) {
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff((scr + z): i64);
|
||||
emitline("(BP)\n");
|
||||
z += 8;
|
||||
};
|
||||
cgwidentaggedstorebp(c, dst, src, scr, slot_sz);
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(bspill: i64);
|
||||
emitline("(BP), ");
|
||||
emitline(basereg);
|
||||
emitline("\n");
|
||||
let k: i32 = 0;
|
||||
for (k < slot_sz) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff((scr + k): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitdispreg((slot_off + k): i64, basereg);
|
||||
emitline("\n");
|
||||
k += 8;
|
||||
};
|
||||
};
|
||||
|
||||
// cgwidentaggedstorebp — BP-rooted body. Called via cgwidentaggedstore
|
||||
// for the natural "BP" case and via the wrapper's scratch path for
|
||||
// pointer-rooted dst. Direct callers exist only in case of future
|
||||
// inlined uses inside this file; new code should call the wrapper.
|
||||
fn cgwidentaggedstorebp(c: *cgen, dst: *node, src: *node, slot_off: i32, slot_sz: i32) void = {
|
||||
let dt: *node = resolvetagged(c, dst);
|
||||
if (dt == nil) { return; };
|
||||
// Nullable fold: one 8B word holding the pointer (or 0 for void).
|
||||
@@ -10793,7 +10858,7 @@ fn cgcall(c: *cgen, n: *node) void = {
|
||||
let slot: i32 = doff + j * esz;
|
||||
if (velemtagged) {
|
||||
cgwidentaggedstore(c, varp.lhs,
|
||||
aa2, slot, esz);
|
||||
aa2, "BP", slot, esz);
|
||||
} else { if (velemstr) {
|
||||
cgexpr(c, aa2);
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
@@ -11049,7 +11114,7 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
if (istaggedtype(c, lc.tnode)) {
|
||||
let lsz: i32 = slotsize(c, lc.tnode);
|
||||
cgwidentaggedstore(c, lc.tnode,
|
||||
n.rhs, lc.off, lsz);
|
||||
n.rhs, "BP", lc.off, lsz);
|
||||
return;
|
||||
};
|
||||
};
|
||||
@@ -11273,7 +11338,7 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
zz += 8;
|
||||
};
|
||||
cgwidentaggedstore(c, elemtn, n.rhs,
|
||||
scroff, slot_sz);
|
||||
"BP", scroff, slot_sz);
|
||||
cgexpr(c, idx);
|
||||
if (slot_sz > 1) {
|
||||
emitline("\tMOVQ\t$");
|
||||
@@ -11619,6 +11684,20 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
for (fi != nil) {
|
||||
let fn_: str = fi.fname;
|
||||
if (streq(fn_, fld)) {
|
||||
// Tagged-union field via *struct base — full slot
|
||||
// rewrite via cgwidentaggedstore basereg="BX". Pre-#26
|
||||
// fell through to the scalar store and dropped tag
|
||||
// + payload.
|
||||
if (n.op == tkind.TK_ASSIGN
|
||||
&& istaggedtype(c, fi.tnode)) {
|
||||
let fsz: i32 = slotsize(c, fi.tnode);
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(lc.off: i64);
|
||||
emitline("(BP), BX\n");
|
||||
cgwidentaggedstore(c, fi.tnode,
|
||||
n.rhs, "BX", fi.foff, fsz);
|
||||
return;
|
||||
};
|
||||
// struct-typed field, struct-ident rhs through
|
||||
// *struct base: cgexpr can't materialise a whole
|
||||
// struct value, so word-copy from the rhs slot
|
||||
@@ -11769,6 +11848,17 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
for (fi != nil) {
|
||||
let fn_: str = fi.fname;
|
||||
if (streq(fn_, fld)) {
|
||||
// Tagged-union field in a direct struct local —
|
||||
// full slot rewrite at (lc.off + fi.foff)(BP)
|
||||
// via cgwidentaggedstore basereg="BP". Pre-#26
|
||||
// fell through and dropped tag + payload.
|
||||
if (n.op == tkind.TK_ASSIGN
|
||||
&& istaggedtype(c, fi.tnode)) {
|
||||
let fsz: i32 = slotsize(c, fi.tnode);
|
||||
cgwidentaggedstore(c, fi.tnode,
|
||||
n.rhs, "BP", lc.off + fi.foff, fsz);
|
||||
return;
|
||||
};
|
||||
// struct-typed field, struct-ident rhs:
|
||||
// word-copy direct, skipping cgexpr (no
|
||||
// register convention for a whole struct
|
||||
@@ -12978,8 +13068,8 @@ fn cgreturn(c: *cgen, n: *node) void = {
|
||||
emitline("(BP)\n");
|
||||
zz += 8;
|
||||
};
|
||||
cgwidentaggedstore(c, c.fnret, rhs, scroff,
|
||||
rsz);
|
||||
cgwidentaggedstore(c, c.fnret, rhs, "BP",
|
||||
scroff, rsz);
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(scroff: i64);
|
||||
emitline("(BP), AX\n");
|
||||
@@ -13101,7 +13191,7 @@ fn cglet(c: *cgen, n: *node) void = {
|
||||
// ABI call), struct payload (literal/ident), str payload,
|
||||
// scalar payload — with tag remap for tagged-subset widening.
|
||||
if (istaggedtype(c, tn)) {
|
||||
cgwidentaggedstore(c, tn, rhs, off, sz);
|
||||
cgwidentaggedstore(c, tn, rhs, "BP", off, sz);
|
||||
c.lastwasreturn = 0;
|
||||
return;
|
||||
};
|
||||
@@ -14118,6 +14208,61 @@ fn scanlocals(c: *cgen, n: *node) i32 = {
|
||||
};
|
||||
};
|
||||
};
|
||||
// Tagged-union struct-field write: `s.f = v` or `(*p).f = v`
|
||||
// where f is a tagged-union field. cgassign delegates to
|
||||
// cgwidentaggedstore; for pointer-rooted dst the wrapper
|
||||
// allocates @tagbase (8B) and @tagscr (slot_sz). Both names
|
||||
// dedup with other tagged scratch users in the same function.
|
||||
if (n.kind == nkind.N_ASSIGN) {
|
||||
let alhs: *node = n.lhs;
|
||||
if (alhs != nil) {
|
||||
if (alhs.kind == nkind.N_DOT) {
|
||||
let abase: *node = alhs.lhs;
|
||||
if (abase != nil) {
|
||||
if (abase.kind == nkind.N_IDENT) {
|
||||
let lc: *local = localfindnode(c, abase.str);
|
||||
let btn: *node = nil;
|
||||
if (lc != nil) { btn = lc.tnode; }
|
||||
else { btn = letvartnode(c, abase.str); };
|
||||
let isptr: bool = false;
|
||||
let stype: *node = nil;
|
||||
if (btn != nil) {
|
||||
if (btn.kind == nkind.N_TPTR) {
|
||||
isptr = true;
|
||||
stype = btn.lhs;
|
||||
};
|
||||
if (btn.kind == nkind.N_TNAME) { stype = btn; };
|
||||
};
|
||||
if (stype != nil) {
|
||||
if (stype.kind == nkind.N_TNAME) {
|
||||
let si: *structinfo = structlookup(c, stype.str);
|
||||
if (si != nil) {
|
||||
let fi: *fieldinfo = si.fields;
|
||||
for (fi != nil) {
|
||||
if (streq(fi.fname, alhs.str)) {
|
||||
if (istaggedtype(c, fi.tnode)) {
|
||||
if (isptr) {
|
||||
if (!scanseenmark(c, "@tagbase")) {
|
||||
total += 8;
|
||||
};
|
||||
if (!scanseenmark(c, "@tagscr")) {
|
||||
total += 24;
|
||||
};
|
||||
};
|
||||
};
|
||||
fi = nil;
|
||||
} else {
|
||||
fi = fi.finext;
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
// Tagged-union return with struct payload or tagged-subset
|
||||
// source — cgreturn materialises in @tagscr then loads
|
||||
// AX/DX/CX. Detect via the same rhsstructpayload predicate
|
||||
|
||||
Reference in New Issue
Block a user