w6c+selfhost: cgdot N_DOT tagged-field source ABI (closes #28)

cgdot of a tagged-union struct field previously dropped the AX/DX/CX/R8
payload-register convention used by tagged-union returns: cstage's
direct-struct branch stopped at CX (size > 16) and never loaded R8
(slice-payload variants, slot 32B); the via_ptr branch had no TY_TAGGED
handler at all, falling through to fldloadop and yielding only the tag
in AX. The N_DOT scrutinee fallback in N_MATCH similarly stored only AX
into the spill slot. Wwstage cgdot had no TY_TAGGED branch in any of
the direct, *struct, or top-level-global field-load paths, and cgmatch's
non-ident scrutinee branch didn't recognise N_DOT — dispatch always
computed want = 0 and the spill scratch was hardcoded 24B. The combined
effect: any code reading `s.taggedfield` and consuming more than one
quadword of the payload saw garbage in the upper halves.

Cstage: extended the direct-struct TY_TAGGED branch with an R8 load for
size > 24 (CX still loaded last so global LEAQ-into-CX rooting
survives), added a parallel TY_TAGGED handler to the via_ptr (TY_PTR
inner TY_STRUCT) field branch, and extended the N_DOT scrutinee spill
fallback in N_MATCH to write DX/CX/R8 alongside AX.

Wwstage: new cgloadtaggedfield helper emits the four-register load with
CX-last ordering, and dotfieldtnode resolves a field's declared type
node for a local-ident or *struct base. cgdot grew three TY_TAGGED
branches (direct local, *struct deref staging in BX, top-level global
through CX). cgmatch's non-ident-scrutinee branch grew an N_DOT type-
extraction path mirroring the N_CALL / N_INDEX shapes and now sizes the
@match_spill slot from slotsize(scrutt) so slice-payload variants don't
overflow the historical 24B alloc. rhstaggedabicall accepts N_DOT so
`let copy: ev = h.e;` and tagged-arg call sites pass through the
tagged-source spill branch of cgwidentaggedstore.

Out of scope for #28 and left as separate latents: wwstage's match-arm
bind for a TY_STRUCT-typed variant copies only 8B (cstage falls back
to bu->size; wwstage's bsz=8 default), and the variant-index lookup
for an i64 literal in (i32 | i64) picks the wrong tag on the write
side. Both surface in struct-payload tagged unions and merit their
own tasks; the new test rows steer clear so #28's fix verifies
end-to-end on scalar / str / slice payloads.

Test 693_dot_tagged_source — three variant shapes (16B i64, 24B str,
32B slice) read from direct local, *struct param, top-level global,
and let-init round-trip. The 32B-slice rows verify v.cap (R8 / +24)
so dropping the upper-word load isn't masked by len-only checks; the
top-level-global row routes the write through *p because the direct
global-LHS tagged store is a separate wwstage gap (followup). Three
negative controls (untagged i32 / str / slice fields) keep the new
TY_TAGGED guard from shadowing the existing field-load paths. Wired
into make test; 37 tests total. Bootstrap ww2 == ww3 == ww4
byte-identical.
This commit is contained in:
2026-05-15 00:45:14 +09:00
parent 49e39a4cf3
commit bacbf4b845
7 changed files with 948 additions and 47 deletions

View File

@@ -943,17 +943,20 @@ fn cgmatch(c: *cgen, n: *node) void = {
scrutt = resolvetagged(c, lc.tnode);
};
} else {
// Non-ident scrutinee (call result, arr[i], ?, etc.).
// 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.
// Nullable returns are single-word (AX = ptr); only +0
// is read, so the extra stores are harmless. We
// recover the scrutinee type from fnretlookup (N_CALL)
// or the base local's array element type (N_INDEX) so
// dispatch can compute variant indices.
scrutoff = localalloc(c, "@match_spill", 24, nil);
// Non-ident scrutinee (call result, arr[i], p.field,
// ?, etc.). 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) and tagged-field loads (N_DOT, fixed by
// #28) produce the same triple. Nullable returns are
// single-word (AX = ptr); only +0 is read, so the
// extra stores are harmless. We recover the scrutinee
// type from fnretlookup (N_CALL), the base local's
// array element type (N_INDEX), or the struct field
// type (N_DOT) so dispatch can compute variant
// indices. Slot size is then derived from the
// scrutinee type so slice-variant tagged-unions
// (32B slot) don't overflow a hardcoded 24B scratch.
if (scrut.kind == nkind.N_CALL) {
let callee: *node = scrut.lhs;
if (callee != nil) {
@@ -988,6 +991,21 @@ fn cgmatch(c: *cgen, n: *node) void = {
};
};
};
if (scrut.kind == nkind.N_DOT) {
let ft: *node = dotfieldtnode(c, scrut);
if (ft != nil) {
scrutt = resolvetagged(c, ft);
};
};
// Size the spill to the scrutinee slot. Default 24B
// preserves the historical alloc for non-tagged or
// unresolved cases (nullable, str-returning, etc.).
let spillsz: i32 = 24;
if (scrutt != nil) {
let resolved: i32 = slotsize(c, scrutt);
if (resolved > spillsz) { spillsz = resolved; };
};
scrutoff = localalloc(c, "@match_spill", spillsz, nil);
cgexpr(c, scrut);
emitline("\tMOVQ\tAX, ");
emitoff(scrutoff: i64);
@@ -1188,6 +1206,24 @@ fn cgdot(c: *cgen, n: *node) void = {
for (fi != nil) {
let fn_: str = fi.fname;
if (streq(fn_, fld)) {
// tagged-union field via *struct: stage
// the *struct in BX, then load the four
// payload regs via cgloadtaggedfield.
// BX isn't a target (AX/DX/CX/R8), so
// load order doesn't matter. Mirrors
// the direct-local branch above so the
// match / let-init / call-arg consumer
// shape is identical regardless of
// pointer rooting.
if (istaggedtype(c, fi.tnode)) {
let tsz: i32 = slotsize(c, fi.tnode);
emitline("\tMOVQ\t");
emitoff(lc.off: i64);
emitline("(BP), BX\n");
cgloadtaggedfield(c, "BX",
fi.foff, tsz);
return;
};
// str field via *struct: load len into a
// scratch first (so loading ptr into AX
// last leaves (AX=ptr, BX=len)).
@@ -1254,6 +1290,21 @@ fn cgdot(c: *cgen, n: *node) void = {
for (fi != nil) {
let fn_: str = fi.fname;
if (streq(fn_, fld)) {
// tagged-union field: emit the AX=tag,
// DX=word0, CX=word1[, R8=word2] load
// sequence so the match / let-init /
// call-arg consumers see the same shape
// as a tagged-returning fn. Pre-#28 fell
// through to the scalar fieldloadop and
// only AX (tag) was loaded — payload
// words came from whatever the caller
// left in DX/CX/R8.
if (istaggedtype(c, fi.tnode)) {
let tsz: i32 = slotsize(c, fi.tnode);
cgloadtaggedfield(c, "BP",
lc.off + fi.foff, tsz);
return;
};
// str field: load both halves so chained
// `.ptr` / `.len` see (AX=ptr, BX=len).
if (isstrtype(c, fi.tnode)) {
@@ -1470,6 +1521,18 @@ fn cgdot(c: *cgen, n: *node) void = {
emitline("\tLEAQ\t");
emitsymname(c, lhs.str);
emitline("(SB), CX\n");
// tagged-union field: load via the tagged-
// return ABI off CX. cgloadtaggedfield orders
// the loads so CX (word1 target) is written
// LAST — otherwise the base address would be
// trashed before the +24/R8 (slice variant)
// read could index off it. Pre-#28 fell
// through to fieldloadop and dropped payload.
if (istaggedtype(c, fi.tnode)) {
let tsz: i32 = slotsize(c, fi.tnode);
cgloadtaggedfield(c, "CX", fi.foff, tsz);
return;
};
if (isstrtype(c, fi.tnode)) {
emitline("\tMOVQ\t");
emitdispreg(fi.foff: i64, "CX");