w6c+selfhost: cgen chained N_DOT/N_ASSIGN spine walk
Loop-shaped spine walker for value-struct chains (o.i.a) and slice/str pseudo-fields (s.buf.len), read+write, both stages. SB-fallback at the catch-all preserved for unresolved module-qualified idents. Follow-ups filed: tasks #7-#10 (wwstage >6-arg frame over-alloc, chained array-elem field BX loss, & through chained DOT, signed sub-word field loads zero-extend).
This commit is contained in:
8
Makefile
8
Makefile
@@ -216,7 +216,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
|
||||
$(BIN)/test_arch \
|
||||
$(BIN)/test_e2e $(BIN)/test_ffi $(BIN)/test_dyn $(BIN)/test_stdlib \
|
||||
$(BIN)/test_at_test $(BIN)/test_let_global \
|
||||
$(BIN)/test_int_cast_signed \
|
||||
$(BIN)/test_int_cast_signed $(BIN)/test_dot_chain \
|
||||
$(BIN)/test_selfhost $(BIN)/test_w6a_ww $(BIN)/test_w6l_ww \
|
||||
$(BIN)/test_w6c_ww $(BIN)/test_ww_ww $(BIN)/test_self_rebuild \
|
||||
$(BIN)/test_dyn_ww $(BIN)/test_selfcheck $(BIN)/test_at_test_ww
|
||||
@@ -283,6 +283,12 @@ $(BIN)/test_int_cast_signed: test/wcc/640_int_cast_signed.c $(BIN)/ww \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BIN)/test_dot_chain: test/wcc/650_dot_chain.c $(BIN)/ww \
|
||||
$(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
|
||||
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BIN)/test_selfhost: test/wcc/990_selfhost.c $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
|
||||
$(BIN)/ww $(BIN)/wwdump $(BIN)/wwdump_ww $(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
279
cmd/w6c/cgen.c
279
cmd/w6c/cgen.c
@@ -1943,6 +1943,153 @@ cgexpr(Cg *c, Node *n, Local *locals)
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Chained `<chain>.field = v` where <chain> spans value-struct
|
||||
* dots ending at a root ident — `o.i.a = 10`, `v.a.b.c = …`.
|
||||
* Also handles a slice/str pseudo-field leaf (`b.buf.len = 5`):
|
||||
* spine walks down to the slice/str header, then the +0/+8/+16
|
||||
* delta selects ptr/len/cap. Sibling of the chained-pointer-
|
||||
* field branch above; without this the LHS is silently dropped
|
||||
* (the existing 1-deep branch only fires for `ident.field = …`).
|
||||
* Only plain `=` is wired — compound on a chained value-struct
|
||||
* field is rare and stays unhandled. */
|
||||
if (n->lhs && n->lhs->kind == N_DOT && n->lhs->lhs
|
||||
&& n->lhs->lhs->kind == N_DOT && n->op == TK_ASSIGN) {
|
||||
struct { Type *pu; const char *name; } steps[16];
|
||||
int nsteps = 0;
|
||||
Node *cur = n->lhs;
|
||||
int abort = 0;
|
||||
while (cur && cur->kind == N_DOT && cur->lhs) {
|
||||
Type *pt = cur->lhs->type;
|
||||
Type *pu = (pt && pt->kind == TY_NAMED)
|
||||
? pt->under : pt;
|
||||
if (!pu) { abort = 1; break; }
|
||||
if (cur == n->lhs && (pu->kind == TY_SLICE
|
||||
|| pu->kind == TY_STR)) {
|
||||
/* leaf pseudo-field on slice/str header */
|
||||
} else if (pu->kind != TY_STRUCT) {
|
||||
abort = 1;
|
||||
break;
|
||||
}
|
||||
if (nsteps >= 16) { abort = 1; break; }
|
||||
steps[nsteps].pu = pu;
|
||||
steps[nsteps].name = cur->str;
|
||||
nsteps++;
|
||||
cur = cur->lhs;
|
||||
}
|
||||
if (!abort && cur && cur->kind == N_IDENT
|
||||
&& nsteps > 0) {
|
||||
int total_off = 0;
|
||||
Type *leaf_type = NULL;
|
||||
int slice_delta = -1;
|
||||
int ok = 1;
|
||||
for (int i = nsteps - 1; i >= 0; i--) {
|
||||
Type *pu = steps[i].pu;
|
||||
if (pu->kind == TY_SLICE
|
||||
|| pu->kind == TY_STR) {
|
||||
if (strcmp(steps[i].name, "ptr") == 0)
|
||||
slice_delta = 0;
|
||||
else if (strcmp(steps[i].name, "len") == 0)
|
||||
slice_delta = 8;
|
||||
else if (strcmp(steps[i].name, "cap") == 0)
|
||||
slice_delta = 16;
|
||||
else { ok = 0; break; }
|
||||
} else {
|
||||
Tfield *f = NULL;
|
||||
for (Tfield *fl = pu->fields; fl; fl = fl->next)
|
||||
if (strcmp(fl->name, steps[i].name) == 0)
|
||||
{ f = fl; break; }
|
||||
if (!f) { ok = 0; break; }
|
||||
total_off += (int)f->offset;
|
||||
leaf_type = f->type;
|
||||
}
|
||||
}
|
||||
if (ok) {
|
||||
int root_off = localfind(locals, cur->str);
|
||||
int base_reg = D_BP;
|
||||
int base_disp = root_off;
|
||||
int is_global = 0;
|
||||
int root_resolved = (root_off != 0);
|
||||
if (!root_resolved && let_islet(cur->str)) {
|
||||
root_resolved = 1;
|
||||
is_global = 1;
|
||||
}
|
||||
if (root_resolved) {
|
||||
if (slice_delta >= 0) {
|
||||
/* slice/str pseudo-field store. .ptr writes
|
||||
* 8 bytes; .len / .cap write 8 bytes each
|
||||
* (matches the existing N_IDENT pseudo-
|
||||
* field branch). */
|
||||
cgexpr(c, n->rhs, locals);
|
||||
if (is_global) {
|
||||
ins2(c, A_LEAQ,
|
||||
masym(c, cur->str),
|
||||
areg(D_CX));
|
||||
ins2(c, A_MOVQ, areg(D_AX),
|
||||
amem(D_CX, total_off + slice_delta));
|
||||
} else {
|
||||
ins2(c, A_MOVQ, areg(D_AX),
|
||||
amem(D_BP, base_disp + total_off + slice_delta));
|
||||
}
|
||||
break;
|
||||
}
|
||||
Type *fu = (leaf_type
|
||||
&& leaf_type->kind == TY_NAMED)
|
||||
? leaf_type->under : leaf_type;
|
||||
int fsz = (int)(leaf_type
|
||||
? leaf_type->size : 8);
|
||||
int store_op = A_MOVQ;
|
||||
if (fsz == 1) store_op = A_MOVB;
|
||||
else if (fsz == 4) store_op = A_MOVL;
|
||||
if (fu && fu->kind == TY_STR) {
|
||||
cgexpr(c, n->rhs, locals);
|
||||
if (is_global) {
|
||||
ins2(c, A_LEAQ,
|
||||
masym(c, cur->str),
|
||||
areg(D_CX));
|
||||
ins2(c, A_MOVQ, areg(D_AX),
|
||||
amem(D_CX, total_off + 0));
|
||||
ins2(c, A_MOVQ, areg(D_BX),
|
||||
amem(D_CX, total_off + 8));
|
||||
} else {
|
||||
ins2(c, A_MOVQ, areg(D_AX),
|
||||
amem(D_BP, base_disp + total_off + 0));
|
||||
ins2(c, A_MOVQ, areg(D_BX),
|
||||
amem(D_BP, base_disp + total_off + 8));
|
||||
}
|
||||
break;
|
||||
}
|
||||
int sf32 = 0;
|
||||
if (fld_isfloat(leaf_type, &sf32)) {
|
||||
int mov = sf32 ? A_MOVSS : A_MOVSD;
|
||||
cgexpr(c, n->rhs, locals);
|
||||
if (is_global) {
|
||||
ins2(c, A_LEAQ,
|
||||
masym(c, cur->str),
|
||||
areg(D_CX));
|
||||
ins2(c, mov, areg(D_X0),
|
||||
amem(D_CX, total_off));
|
||||
} else {
|
||||
ins2(c, mov, areg(D_X0),
|
||||
amem(D_BP, base_disp + total_off));
|
||||
}
|
||||
break;
|
||||
}
|
||||
cgexpr(c, n->rhs, locals);
|
||||
if (is_global) {
|
||||
ins2(c, A_LEAQ,
|
||||
masym(c, cur->str),
|
||||
areg(D_CX));
|
||||
ins2(c, store_op, areg(D_AX),
|
||||
amem(D_CX, total_off));
|
||||
} else {
|
||||
ins2(c, store_op, areg(D_AX),
|
||||
amem(D_BP, base_disp + total_off));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/* float assignment to a local or top-level global. Globals
|
||||
* route through LEAQ+indirect (no D_EXTERN SSE in w6a).
|
||||
* Compound (`acc += d` etc.) loads slot into X1, combines
|
||||
@@ -3561,6 +3708,138 @@ cgexpr(Cg *c, Node *n, Local *locals)
|
||||
ins2(c, A_MOVQ, masym(c, n->str), areg(D_AX));
|
||||
goto dot_done;
|
||||
}
|
||||
/* Chained N_DOT spine through value-struct fields. Handles any
|
||||
* depth `root.f0.f1.…leaf` where every intermediate field is a
|
||||
* value struct, plus the slice/str pseudo-field tail (`s.buf.len`)
|
||||
* where the innermost field is a slice/str header. Walks inward
|
||||
* collecting (parent_struct, field_name); reverses to sum field
|
||||
* offsets; emits one load at (base + total_off). Placed BEFORE
|
||||
* the slice/str pseudo-field branch so its else-arm (cgexpr lhs
|
||||
* + shuffle BX→AX) doesn't mis-handle `b.buf.len` — cgexpr on a
|
||||
* value-struct→slice chain only loads .ptr into AX, leaving BX
|
||||
* stale. Sibling of the pointer-chain branch further down. */
|
||||
if (n->lhs && n->lhs->kind == N_DOT) {
|
||||
Type *lt0 = n->lhs->type;
|
||||
Type *lu0 = (lt0 && lt0->kind == TY_NAMED) ? lt0->under : lt0;
|
||||
int leaf_is_pseudo = lu0 && n->str
|
||||
&& (lu0->kind == TY_SLICE || lu0->kind == TY_STR)
|
||||
&& (strcmp(n->str, "ptr") == 0
|
||||
|| strcmp(n->str, "len") == 0
|
||||
|| strcmp(n->str, "cap") == 0);
|
||||
int leaf_in_struct = lu0 && lu0->kind == TY_STRUCT;
|
||||
if (leaf_is_pseudo || leaf_in_struct) {
|
||||
struct { Type *pu; const char *name; } steps[16];
|
||||
int nsteps = 0;
|
||||
Node *cur = n;
|
||||
int abort = 0;
|
||||
while (cur && cur->kind == N_DOT && cur->lhs) {
|
||||
Type *pt = cur->lhs->type;
|
||||
Type *pu = (pt && pt->kind == TY_NAMED)
|
||||
? pt->under : pt;
|
||||
if (!pu) { abort = 1; break; }
|
||||
if (cur == n && (pu->kind == TY_SLICE
|
||||
|| pu->kind == TY_STR)) {
|
||||
/* leaf pseudo on slice/str header */
|
||||
} else if (pu->kind != TY_STRUCT) {
|
||||
abort = 1;
|
||||
break;
|
||||
}
|
||||
if (nsteps >= 16) { abort = 1; break; }
|
||||
steps[nsteps].pu = pu;
|
||||
steps[nsteps].name = cur->str;
|
||||
nsteps++;
|
||||
cur = cur->lhs;
|
||||
}
|
||||
if (!abort && cur && cur->kind == N_IDENT
|
||||
&& nsteps > 0) {
|
||||
int total_off = 0;
|
||||
Type *leaf_type = NULL;
|
||||
int slice_delta = -1;
|
||||
int ok = 1;
|
||||
for (int i = nsteps - 1; i >= 0; i--) {
|
||||
Type *pu = steps[i].pu;
|
||||
if (pu->kind == TY_SLICE
|
||||
|| pu->kind == TY_STR) {
|
||||
if (strcmp(steps[i].name, "ptr") == 0)
|
||||
slice_delta = 0;
|
||||
else if (strcmp(steps[i].name, "len") == 0)
|
||||
slice_delta = 8;
|
||||
else if (strcmp(steps[i].name, "cap") == 0)
|
||||
slice_delta = 16;
|
||||
else { ok = 0; break; }
|
||||
} else {
|
||||
Tfield *f = NULL;
|
||||
for (Tfield *fl = pu->fields; fl; fl = fl->next)
|
||||
if (strcmp(fl->name, steps[i].name) == 0)
|
||||
{ f = fl; break; }
|
||||
if (!f) { ok = 0; break; }
|
||||
total_off += (int)f->offset;
|
||||
leaf_type = f->type;
|
||||
}
|
||||
}
|
||||
if (ok) {
|
||||
int root_off = localfind(locals, cur->str);
|
||||
int base_reg = D_BP;
|
||||
int base_disp = root_off;
|
||||
int root_resolved = (root_off != 0);
|
||||
if (!root_resolved && let_islet(cur->str)) {
|
||||
ins2(c, A_LEAQ,
|
||||
masym(c, cur->str), areg(D_CX));
|
||||
base_reg = D_CX;
|
||||
base_disp = 0;
|
||||
root_resolved = 1;
|
||||
}
|
||||
if (root_resolved) {
|
||||
if (slice_delta >= 0) {
|
||||
ins2(c, A_MOVQ,
|
||||
amem(base_reg,
|
||||
base_disp + total_off + slice_delta),
|
||||
areg(D_AX));
|
||||
goto dot_done;
|
||||
}
|
||||
Type *fu = (leaf_type
|
||||
&& leaf_type->kind == TY_NAMED)
|
||||
? leaf_type->under : leaf_type;
|
||||
if (fu && fu->kind == TY_STR) {
|
||||
ins2(c, A_MOVQ,
|
||||
amem(base_reg,
|
||||
base_disp + total_off + 0),
|
||||
areg(D_AX));
|
||||
ins2(c, A_MOVQ,
|
||||
amem(base_reg,
|
||||
base_disp + total_off + 8),
|
||||
areg(D_BX));
|
||||
goto dot_done;
|
||||
}
|
||||
int g_isf32 = 0;
|
||||
if (fld_isfloat(leaf_type, &g_isf32)) {
|
||||
int mov = g_isf32 ? A_MOVSS : A_MOVSD;
|
||||
ins2(c, mov,
|
||||
amem(base_reg,
|
||||
base_disp + total_off),
|
||||
areg(D_X0));
|
||||
goto dot_done;
|
||||
}
|
||||
int fsz = (int)(leaf_type
|
||||
? leaf_type->size : 8);
|
||||
int signed_field = leaf_type && (
|
||||
leaf_type->kind == TY_I8
|
||||
|| leaf_type->kind == TY_I16
|
||||
|| leaf_type->kind == TY_I32);
|
||||
int op = A_MOVQ;
|
||||
if (fsz == 1) op = A_MOVZBQ;
|
||||
else if (fsz == 4)
|
||||
op = signed_field ? A_MOVSXD : A_MOVL;
|
||||
ins2(c, op,
|
||||
amem(base_reg,
|
||||
base_disp + total_off),
|
||||
areg(D_AX));
|
||||
goto dot_done;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
int lenfld = (n->str && strcmp(n->str, "len") == 0);
|
||||
int capfld = (n->str && strcmp(n->str, "cap") == 0);
|
||||
int ptrfld = (n->str && strcmp(n->str, "ptr") == 0);
|
||||
|
||||
@@ -7778,6 +7778,175 @@ fn cgwidentaggedstore(c: *cgen, dst: *node, src: *node, slot_off: i32, slot_sz:
|
||||
return;
|
||||
};
|
||||
|
||||
// dotchain — packed result struct for dotchainresolve. Out-params are
|
||||
// bundled to keep the helper at <= 6 register-passed args; wwstage's
|
||||
// per-fn arg-frame computation over-allocates by 16 bytes for any
|
||||
// function with > 6 args (task #7, a pre-existing quirk independent
|
||||
// of this fix), which would silently break the bootstrap fixed-point
|
||||
// gate (993 / 994 / 995).
|
||||
//
|
||||
// Numeric fields are all i64, not i32. wwstage zero-inits an i32
|
||||
// local with MOVQ (8-byte store) but subsequent `out.totaloff = …`
|
||||
// updates would emit MOVL (4-byte store), leaving the upper 4 bytes
|
||||
// stale from the wider init. Keeping the out-params at i64 makes the
|
||||
// init width and the update width agree, so the field reads back
|
||||
// what was written across both stages.
|
||||
type dotchain = struct {
|
||||
rootname: str,
|
||||
rootoff: i64,
|
||||
totaloff: i64,
|
||||
leaffi: *fieldinfo,
|
||||
slicedelta: i64,
|
||||
isglobal: bool,
|
||||
};
|
||||
|
||||
// Spine-walk a chained N_DOT (n) inward to a root ident, summing field
|
||||
// offsets through value-struct intermediates. Optional slice/str leaf
|
||||
// pseudo-field (.ptr / .len / .cap) on the last segment is folded into
|
||||
// `out.slicedelta` (0/8/16); otherwise out.leaffi is the leaf fieldinfo
|
||||
// and slicedelta stays -1. Returns true on success; on false the caller
|
||||
// falls through to other branches.
|
||||
//
|
||||
// Mirrors cmd/w6c/cgen.c's N_DOT chained walker; both stages must agree
|
||||
// on the same shapes so the bootstrap fixed-point holds. The chain
|
||||
// depth is capped at 16 — deeper chains are vanishingly rare and fall
|
||||
// through.
|
||||
//
|
||||
// On success the caller emits one load/store at root_base + out.totaloff
|
||||
// (+ slicedelta for pseudo leaf). Root resolves as: local frame slot
|
||||
// (out.rootoff != 0, isglobal false) or top-level let (isglobal true,
|
||||
// root accessed via LEAQ name(SB), CX).
|
||||
export fn dotchainresolve(c: *cgen, n: *node, out: *dotchain) bool = {
|
||||
out.rootname = "";
|
||||
out.rootoff = 0i64;
|
||||
out.isglobal = false;
|
||||
out.totaloff = 0i64;
|
||||
out.leaffi = nil;
|
||||
out.slicedelta = -1i64;
|
||||
if (n == nil) { return false; };
|
||||
if (n.kind != nkind.N_DOT) { return false; };
|
||||
// Walk inward, recording the N_DOT node at each step (leaf first).
|
||||
// We hold *node pointers (8B each, slotsize-stable across stages)
|
||||
// and read .str on demand — a [16]str array would mis-slot at
|
||||
// wwstage where slotsize("str") returns 8, breaking the bootstrap
|
||||
// fixed-point.
|
||||
let stk: [16]*node;
|
||||
let nsteps: i32 = 0;
|
||||
let cur: *node = n;
|
||||
for (cur != nil) {
|
||||
if (cur.kind != nkind.N_DOT) { break; };
|
||||
if (nsteps >= 16) { return false; };
|
||||
stk[nsteps] = cur;
|
||||
nsteps += 1;
|
||||
cur = cur.lhs;
|
||||
};
|
||||
if (nsteps < 2) { return false; };
|
||||
if (cur == nil) { return false; };
|
||||
if (cur.kind != nkind.N_IDENT) { return false; };
|
||||
out.rootname = cur.str;
|
||||
// Resolve the root's struct type and base.
|
||||
let rootstruct: str = "";
|
||||
let lc: *local = localfindnode(c, cur.str);
|
||||
let gsi: *structinfo = nil;
|
||||
if (lc != nil) {
|
||||
if (lc.tnode != nil) {
|
||||
if (lc.tnode.kind == nkind.N_TNAME) {
|
||||
rootstruct = lc.tnode.str;
|
||||
out.rootoff = lc.off: i64;
|
||||
};
|
||||
};
|
||||
};
|
||||
if (rootstruct.len == 0) {
|
||||
gsi = letvarstructinfo(c, cur.str);
|
||||
if (gsi != nil) {
|
||||
rootstruct = gsi.sname;
|
||||
out.isglobal = true;
|
||||
};
|
||||
};
|
||||
if (rootstruct.len == 0) { return false; };
|
||||
// Walk outward, resolving each field. stk is leaf-first; iterate
|
||||
// from i = nsteps - 1 (the root-most field) down to i = 0 (leaf).
|
||||
let curstruct: str = rootstruct;
|
||||
// Pre-declare per-iteration spills here so cstage / wwstage agree
|
||||
// on the frame layout. Both must emit byte-identical asm for the
|
||||
// bootstrap fixed-point (tests 993/995) — letting these locals get
|
||||
// declared inside the branch bodies trips a per-stage divergence in
|
||||
// slot counting.
|
||||
let stepnd: *node = nil;
|
||||
let stepnm: str = "";
|
||||
let fi: *fieldinfo = nil;
|
||||
let found: *fieldinfo = nil;
|
||||
let ft: *node = nil;
|
||||
let s0nd: *node = nil;
|
||||
let pseudo: str = "";
|
||||
let delta: i64 = 0i64;
|
||||
let i: i32 = nsteps - 1;
|
||||
for (i >= 0) {
|
||||
let csi: *structinfo = structlookup(c, curstruct);
|
||||
if (csi == nil) { return false; };
|
||||
// Materialise the *node first; wwstage's cgen mis-emits the
|
||||
// chained shape `stk[i].str` directly (task #8 — loses BX
|
||||
// between the index load and the field deref), so always
|
||||
// spill to an intermediate local before reading the str
|
||||
// field. The cstage emits the same pattern for byte-identity.
|
||||
stepnd = stk[i];
|
||||
if (stepnd == nil) { return false; };
|
||||
stepnm = stepnd.str;
|
||||
fi = csi.fields;
|
||||
found = nil;
|
||||
for (fi != nil) {
|
||||
if (streq(fi.fname, stepnm)) { found = fi; break; };
|
||||
fi = fi.finext;
|
||||
};
|
||||
if (found == nil) { return false; };
|
||||
if (i == 0) {
|
||||
out.totaloff = out.totaloff + (found.foff: i64);
|
||||
out.leaffi = found;
|
||||
return true;
|
||||
};
|
||||
// Intermediate step. Must be a nested value-struct, OR a slice/
|
||||
// str field with the leaf (i == 1, stk[0]) as a pseudo-field.
|
||||
ft = found.tnode;
|
||||
if (ft == nil) { return false; };
|
||||
if (ft.kind == nkind.N_TNAME) {
|
||||
if (streq(ft.str, "str")) {
|
||||
if (i != 1) { return false; };
|
||||
s0nd = stk[0];
|
||||
if (s0nd == nil) { return false; };
|
||||
pseudo = s0nd.str;
|
||||
delta = -1i64;
|
||||
if (streq(pseudo, "ptr")) { delta = 0i64; }
|
||||
else { if (streq(pseudo, "len")) { delta = 8i64; }; };
|
||||
if (delta < 0i64) { return false; };
|
||||
out.totaloff = out.totaloff + (found.foff: i64);
|
||||
out.slicedelta = delta;
|
||||
return true;
|
||||
};
|
||||
if (primsize(ft.str) != 0) { return false; };
|
||||
// Nested value-struct (named).
|
||||
out.totaloff = out.totaloff + (found.foff: i64);
|
||||
curstruct = ft.str;
|
||||
i -= 1;
|
||||
} else { if (ft.kind == nkind.N_TSLICE) {
|
||||
if (i != 1) { return false; };
|
||||
s0nd = stk[0];
|
||||
if (s0nd == nil) { return false; };
|
||||
pseudo = s0nd.str;
|
||||
delta = -1i64;
|
||||
if (streq(pseudo, "ptr")) { delta = 0i64; }
|
||||
else { if (streq(pseudo, "len")) { delta = 8i64; }
|
||||
else { if (streq(pseudo, "cap")) { delta = 16i64; }; }; };
|
||||
if (delta < 0i64) { return false; };
|
||||
out.totaloff = out.totaloff + (found.foff: i64);
|
||||
out.slicedelta = delta;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}; };
|
||||
};
|
||||
return false;
|
||||
};
|
||||
|
||||
// MODULE: wcc
|
||||
// selfhost/cmd/wcc/cgenexpr.ww — split out of cgen.ww.
|
||||
//
|
||||
@@ -9324,6 +9493,99 @@ fn cgdot(c: *cgen, n: *node) void = {
|
||||
return;
|
||||
};
|
||||
};
|
||||
// Chained N_DOT spine through value-struct fields (any depth).
|
||||
// Walks the spine to a root ident, summing field offsets, then
|
||||
// emits ONE load at base + total_off. Also handles a slice/str
|
||||
// pseudo-field leaf (`b.buf.len`): the walk lands on the slice/
|
||||
// str header and slicedelta picks ptr/len/cap. Mirror of cstage
|
||||
// cgen.c's chained-DOT read branch. Without this, depth ≥ 3
|
||||
// shapes (`v.a.a.a`) and `b.buf.len` fall through to the non-
|
||||
// ident-base pseudo branch below — which would cgexpr the inner
|
||||
// (loading only .ptr into AX) and shuffle stale BX into AX.
|
||||
// Placed BEFORE the .ptr/.len fast paths so the chain wins.
|
||||
if (lhs != nil) {
|
||||
if (lhs.kind == nkind.N_DOT) {
|
||||
let r: dotchain;
|
||||
let pok: bool = dotchainresolve(c, n, &r);
|
||||
if (pok) {
|
||||
if (r.slicedelta >= 0i64) {
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\tMOVQ\t");
|
||||
emitdispreg(r.totaloff + r.slicedelta, "CX");
|
||||
emitline(", AX\n");
|
||||
} else {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(r.rootoff + r.totaloff + r.slicedelta);
|
||||
emitline("(BP), AX\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
if (isstrtype(c, r.leaffi.tnode)) {
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\tMOVQ\t");
|
||||
emitdispreg(r.totaloff + 0i64, "CX");
|
||||
emitline(", AX\n");
|
||||
emitline("\tMOVQ\t");
|
||||
emitdispreg(r.totaloff + 8i64, "CX");
|
||||
emitline(", BX\n");
|
||||
} else {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(r.rootoff + r.totaloff + 8i64);
|
||||
emitline("(BP), BX\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
if (isfloattype(c, r.leaffi.tnode)) {
|
||||
let mov: str = "MOVSD";
|
||||
if (isf32type(c, r.leaffi.tnode)) { mov = "MOVSS"; };
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\t");
|
||||
emitdispreg(r.totaloff, "CX");
|
||||
emitline(", X0\n");
|
||||
} else {
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\t");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP), X0\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
let lop: str = fieldloadop(r.leaffi);
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\t");
|
||||
emitline(lop);
|
||||
emitline("\t");
|
||||
emitdispreg(r.totaloff, "CX");
|
||||
emitline(", AX\n");
|
||||
} else {
|
||||
emitline("\t");
|
||||
emitline(lop);
|
||||
emitline("\t");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP), AX\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
};
|
||||
};
|
||||
// Non-ident base pseudo-field: e.g. `"abc".ptr` / `"abc".len`.
|
||||
// Evaluate the str-producing expression — that leaves
|
||||
// (AX=ptr, BX=len). Then `.ptr` returns AX as is; `.len`
|
||||
@@ -9393,6 +9655,9 @@ fn cgdot(c: *cgen, n: *node) void = {
|
||||
// field. Mirror of the cgassign branch added for the same shape.
|
||||
// Without this, `L.cur.kind` (cur a by-value struct of *L)
|
||||
// falls into the SB-fallback and emits `MOVQ kind(SB), AX`.
|
||||
// Kept as a fallback below the generalized walker above (placed
|
||||
// earlier in cgdot) to preserve byte-identical output on shapes
|
||||
// it already handles.
|
||||
if (lhs != nil) {
|
||||
if (lhs.kind == nkind.N_DOT) {
|
||||
let inner: *node = lhs.lhs;
|
||||
@@ -11158,6 +11423,101 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
};
|
||||
};
|
||||
};
|
||||
// Chained N_DOT spine write through value-struct fields (any
|
||||
// depth) — `o.i.a = 10`, `v.a.b.c = …`. Also handles a slice/str
|
||||
// pseudo-field leaf (`b.buf.len = 5`). Mirror of cstage cgen.c's
|
||||
// chained-DOT write branch. Without this, depth ≥ 3 writes and
|
||||
// the slice/str pseudo-field write through a value-struct chain
|
||||
// silently emit no store. Only plain `=` is wired.
|
||||
if (lhs != nil) {
|
||||
if (lhs.kind == nkind.N_DOT && lhs.lhs != nil
|
||||
&& lhs.lhs.kind == nkind.N_DOT
|
||||
&& n.op == tkind.TK_ASSIGN) {
|
||||
let r: dotchain;
|
||||
let yok: bool = dotchainresolve(c, lhs, &r);
|
||||
if (yok) {
|
||||
if (r.slicedelta >= 0i64) {
|
||||
cgexpr(c, n.rhs);
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitdispreg(r.totaloff + r.slicedelta, "CX");
|
||||
emitline("\n");
|
||||
} else {
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff(r.rootoff + r.totaloff + r.slicedelta);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
if (isstrtype(c, r.leaffi.tnode)) {
|
||||
cgexpr(c, n.rhs);
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitdispreg(r.totaloff + 0i64, "CX");
|
||||
emitline("\n");
|
||||
emitline("\tMOVQ\tBX, ");
|
||||
emitdispreg(r.totaloff + 8i64, "CX");
|
||||
emitline("\n");
|
||||
} else {
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP)\n");
|
||||
emitline("\tMOVQ\tBX, ");
|
||||
emitoff(r.rootoff + r.totaloff + 8i64);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
if (isfloattype(c, r.leaffi.tnode)) {
|
||||
let mov: str = "MOVSD";
|
||||
if (isf32type(c, r.leaffi.tnode)) { mov = "MOVSS"; };
|
||||
cgexpr(c, n.rhs);
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\tX0, ");
|
||||
emitdispreg(r.totaloff, "CX");
|
||||
emitline("\n");
|
||||
} else {
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\tX0, ");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
let sop: str = fieldstoreop(r.leaffi);
|
||||
cgexpr(c, n.rhs);
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\t");
|
||||
emitline(sop);
|
||||
emitline("\tAX, ");
|
||||
emitdispreg(r.totaloff, "CX");
|
||||
emitline("\n");
|
||||
} else {
|
||||
emitline("\t");
|
||||
emitline(sop);
|
||||
emitline("\tAX, ");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
};
|
||||
};
|
||||
// Chained `(ident).f1.f2 = v` where f1 is a struct-by-value
|
||||
// field. The earlier chained-DOT branch handles f1: *T (deref
|
||||
// then store). This handles f1: T (in-place sub-struct), which
|
||||
@@ -11165,6 +11525,8 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
// to flatten `cur.kind`/`cur.ival`/... into top-level fields to
|
||||
// work around it. Only plain `=` is wired; compound on a by-
|
||||
// value sub-field hasn't surfaced.
|
||||
// Kept as fallback below the generalized walker for any shape
|
||||
// the walker doesn't recognize.
|
||||
if (lhs != nil) {
|
||||
if (lhs.kind == nkind.N_DOT) {
|
||||
let base: *node = lhs.lhs;
|
||||
|
||||
@@ -1543,6 +1543,99 @@ fn cgdot(c: *cgen, n: *node) void = {
|
||||
return;
|
||||
};
|
||||
};
|
||||
// Chained N_DOT spine through value-struct fields (any depth).
|
||||
// Walks the spine to a root ident, summing field offsets, then
|
||||
// emits ONE load at base + total_off. Also handles a slice/str
|
||||
// pseudo-field leaf (`b.buf.len`): the walk lands on the slice/
|
||||
// str header and slicedelta picks ptr/len/cap. Mirror of cstage
|
||||
// cgen.c's chained-DOT read branch. Without this, depth ≥ 3
|
||||
// shapes (`v.a.a.a`) and `b.buf.len` fall through to the non-
|
||||
// ident-base pseudo branch below — which would cgexpr the inner
|
||||
// (loading only .ptr into AX) and shuffle stale BX into AX.
|
||||
// Placed BEFORE the .ptr/.len fast paths so the chain wins.
|
||||
if (lhs != nil) {
|
||||
if (lhs.kind == nkind.N_DOT) {
|
||||
let r: dotchain;
|
||||
let pok: bool = dotchainresolve(c, n, &r);
|
||||
if (pok) {
|
||||
if (r.slicedelta >= 0i64) {
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\tMOVQ\t");
|
||||
emitdispreg(r.totaloff + r.slicedelta, "CX");
|
||||
emitline(", AX\n");
|
||||
} else {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(r.rootoff + r.totaloff + r.slicedelta);
|
||||
emitline("(BP), AX\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
if (isstrtype(c, r.leaffi.tnode)) {
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\tMOVQ\t");
|
||||
emitdispreg(r.totaloff + 0i64, "CX");
|
||||
emitline(", AX\n");
|
||||
emitline("\tMOVQ\t");
|
||||
emitdispreg(r.totaloff + 8i64, "CX");
|
||||
emitline(", BX\n");
|
||||
} else {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(r.rootoff + r.totaloff + 8i64);
|
||||
emitline("(BP), BX\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
if (isfloattype(c, r.leaffi.tnode)) {
|
||||
let mov: str = "MOVSD";
|
||||
if (isf32type(c, r.leaffi.tnode)) { mov = "MOVSS"; };
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\t");
|
||||
emitdispreg(r.totaloff, "CX");
|
||||
emitline(", X0\n");
|
||||
} else {
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\t");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP), X0\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
let lop: str = fieldloadop(r.leaffi);
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\t");
|
||||
emitline(lop);
|
||||
emitline("\t");
|
||||
emitdispreg(r.totaloff, "CX");
|
||||
emitline(", AX\n");
|
||||
} else {
|
||||
emitline("\t");
|
||||
emitline(lop);
|
||||
emitline("\t");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP), AX\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
};
|
||||
};
|
||||
// Non-ident base pseudo-field: e.g. `"abc".ptr` / `"abc".len`.
|
||||
// Evaluate the str-producing expression — that leaves
|
||||
// (AX=ptr, BX=len). Then `.ptr` returns AX as is; `.len`
|
||||
@@ -1612,6 +1705,9 @@ fn cgdot(c: *cgen, n: *node) void = {
|
||||
// field. Mirror of the cgassign branch added for the same shape.
|
||||
// Without this, `L.cur.kind` (cur a by-value struct of *L)
|
||||
// falls into the SB-fallback and emits `MOVQ kind(SB), AX`.
|
||||
// Kept as a fallback below the generalized walker above (placed
|
||||
// earlier in cgdot) to preserve byte-identical output on shapes
|
||||
// it already handles.
|
||||
if (lhs != nil) {
|
||||
if (lhs.kind == nkind.N_DOT) {
|
||||
let inner: *node = lhs.lhs;
|
||||
@@ -3377,6 +3473,101 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
};
|
||||
};
|
||||
};
|
||||
// Chained N_DOT spine write through value-struct fields (any
|
||||
// depth) — `o.i.a = 10`, `v.a.b.c = …`. Also handles a slice/str
|
||||
// pseudo-field leaf (`b.buf.len = 5`). Mirror of cstage cgen.c's
|
||||
// chained-DOT write branch. Without this, depth ≥ 3 writes and
|
||||
// the slice/str pseudo-field write through a value-struct chain
|
||||
// silently emit no store. Only plain `=` is wired.
|
||||
if (lhs != nil) {
|
||||
if (lhs.kind == nkind.N_DOT && lhs.lhs != nil
|
||||
&& lhs.lhs.kind == nkind.N_DOT
|
||||
&& n.op == tkind.TK_ASSIGN) {
|
||||
let r: dotchain;
|
||||
let yok: bool = dotchainresolve(c, lhs, &r);
|
||||
if (yok) {
|
||||
if (r.slicedelta >= 0i64) {
|
||||
cgexpr(c, n.rhs);
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitdispreg(r.totaloff + r.slicedelta, "CX");
|
||||
emitline("\n");
|
||||
} else {
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff(r.rootoff + r.totaloff + r.slicedelta);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
if (isstrtype(c, r.leaffi.tnode)) {
|
||||
cgexpr(c, n.rhs);
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitdispreg(r.totaloff + 0i64, "CX");
|
||||
emitline("\n");
|
||||
emitline("\tMOVQ\tBX, ");
|
||||
emitdispreg(r.totaloff + 8i64, "CX");
|
||||
emitline("\n");
|
||||
} else {
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP)\n");
|
||||
emitline("\tMOVQ\tBX, ");
|
||||
emitoff(r.rootoff + r.totaloff + 8i64);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
if (isfloattype(c, r.leaffi.tnode)) {
|
||||
let mov: str = "MOVSD";
|
||||
if (isf32type(c, r.leaffi.tnode)) { mov = "MOVSS"; };
|
||||
cgexpr(c, n.rhs);
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\tX0, ");
|
||||
emitdispreg(r.totaloff, "CX");
|
||||
emitline("\n");
|
||||
} else {
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\tX0, ");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
let sop: str = fieldstoreop(r.leaffi);
|
||||
cgexpr(c, n.rhs);
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\t");
|
||||
emitline(sop);
|
||||
emitline("\tAX, ");
|
||||
emitdispreg(r.totaloff, "CX");
|
||||
emitline("\n");
|
||||
} else {
|
||||
emitline("\t");
|
||||
emitline(sop);
|
||||
emitline("\tAX, ");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
};
|
||||
};
|
||||
// Chained `(ident).f1.f2 = v` where f1 is a struct-by-value
|
||||
// field. The earlier chained-DOT branch handles f1: *T (deref
|
||||
// then store). This handles f1: T (in-place sub-struct), which
|
||||
@@ -3384,6 +3575,8 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
// to flatten `cur.kind`/`cur.ival`/... into top-level fields to
|
||||
// work around it. Only plain `=` is wired; compound on a by-
|
||||
// value sub-field hasn't surfaced.
|
||||
// Kept as fallback below the generalized walker for any shape
|
||||
// the walker doesn't recognize.
|
||||
if (lhs != nil) {
|
||||
if (lhs.kind == nkind.N_DOT) {
|
||||
let base: *node = lhs.lhs;
|
||||
|
||||
@@ -2207,3 +2207,172 @@ fn cgwidentaggedstore(c: *cgen, dst: *node, src: *node, slot_off: i32, slot_sz:
|
||||
emitline("(BP)\n");
|
||||
return;
|
||||
};
|
||||
|
||||
// dotchain — packed result struct for dotchainresolve. Out-params are
|
||||
// bundled to keep the helper at <= 6 register-passed args; wwstage's
|
||||
// per-fn arg-frame computation over-allocates by 16 bytes for any
|
||||
// function with > 6 args (task #7, a pre-existing quirk independent
|
||||
// of this fix), which would silently break the bootstrap fixed-point
|
||||
// gate (993 / 994 / 995).
|
||||
//
|
||||
// Numeric fields are all i64, not i32. wwstage zero-inits an i32
|
||||
// local with MOVQ (8-byte store) but subsequent `out.totaloff = …`
|
||||
// updates would emit MOVL (4-byte store), leaving the upper 4 bytes
|
||||
// stale from the wider init. Keeping the out-params at i64 makes the
|
||||
// init width and the update width agree, so the field reads back
|
||||
// what was written across both stages.
|
||||
type dotchain = struct {
|
||||
rootname: str,
|
||||
rootoff: i64,
|
||||
totaloff: i64,
|
||||
leaffi: *fieldinfo,
|
||||
slicedelta: i64,
|
||||
isglobal: bool,
|
||||
};
|
||||
|
||||
// Spine-walk a chained N_DOT (n) inward to a root ident, summing field
|
||||
// offsets through value-struct intermediates. Optional slice/str leaf
|
||||
// pseudo-field (.ptr / .len / .cap) on the last segment is folded into
|
||||
// `out.slicedelta` (0/8/16); otherwise out.leaffi is the leaf fieldinfo
|
||||
// and slicedelta stays -1. Returns true on success; on false the caller
|
||||
// falls through to other branches.
|
||||
//
|
||||
// Mirrors cmd/w6c/cgen.c's N_DOT chained walker; both stages must agree
|
||||
// on the same shapes so the bootstrap fixed-point holds. The chain
|
||||
// depth is capped at 16 — deeper chains are vanishingly rare and fall
|
||||
// through.
|
||||
//
|
||||
// On success the caller emits one load/store at root_base + out.totaloff
|
||||
// (+ slicedelta for pseudo leaf). Root resolves as: local frame slot
|
||||
// (out.rootoff != 0, isglobal false) or top-level let (isglobal true,
|
||||
// root accessed via LEAQ name(SB), CX).
|
||||
export fn dotchainresolve(c: *cgen, n: *node, out: *dotchain) bool = {
|
||||
out.rootname = "";
|
||||
out.rootoff = 0i64;
|
||||
out.isglobal = false;
|
||||
out.totaloff = 0i64;
|
||||
out.leaffi = nil;
|
||||
out.slicedelta = -1i64;
|
||||
if (n == nil) { return false; };
|
||||
if (n.kind != nkind.N_DOT) { return false; };
|
||||
// Walk inward, recording the N_DOT node at each step (leaf first).
|
||||
// We hold *node pointers (8B each, slotsize-stable across stages)
|
||||
// and read .str on demand — a [16]str array would mis-slot at
|
||||
// wwstage where slotsize("str") returns 8, breaking the bootstrap
|
||||
// fixed-point.
|
||||
let stk: [16]*node;
|
||||
let nsteps: i32 = 0;
|
||||
let cur: *node = n;
|
||||
for (cur != nil) {
|
||||
if (cur.kind != nkind.N_DOT) { break; };
|
||||
if (nsteps >= 16) { return false; };
|
||||
stk[nsteps] = cur;
|
||||
nsteps += 1;
|
||||
cur = cur.lhs;
|
||||
};
|
||||
if (nsteps < 2) { return false; };
|
||||
if (cur == nil) { return false; };
|
||||
if (cur.kind != nkind.N_IDENT) { return false; };
|
||||
out.rootname = cur.str;
|
||||
// Resolve the root's struct type and base.
|
||||
let rootstruct: str = "";
|
||||
let lc: *local = localfindnode(c, cur.str);
|
||||
let gsi: *structinfo = nil;
|
||||
if (lc != nil) {
|
||||
if (lc.tnode != nil) {
|
||||
if (lc.tnode.kind == nkind.N_TNAME) {
|
||||
rootstruct = lc.tnode.str;
|
||||
out.rootoff = lc.off: i64;
|
||||
};
|
||||
};
|
||||
};
|
||||
if (rootstruct.len == 0) {
|
||||
gsi = letvarstructinfo(c, cur.str);
|
||||
if (gsi != nil) {
|
||||
rootstruct = gsi.sname;
|
||||
out.isglobal = true;
|
||||
};
|
||||
};
|
||||
if (rootstruct.len == 0) { return false; };
|
||||
// Walk outward, resolving each field. stk is leaf-first; iterate
|
||||
// from i = nsteps - 1 (the root-most field) down to i = 0 (leaf).
|
||||
let curstruct: str = rootstruct;
|
||||
// Pre-declare per-iteration spills here so cstage / wwstage agree
|
||||
// on the frame layout. Both must emit byte-identical asm for the
|
||||
// bootstrap fixed-point (tests 993/995) — letting these locals get
|
||||
// declared inside the branch bodies trips a per-stage divergence in
|
||||
// slot counting.
|
||||
let stepnd: *node = nil;
|
||||
let stepnm: str = "";
|
||||
let fi: *fieldinfo = nil;
|
||||
let found: *fieldinfo = nil;
|
||||
let ft: *node = nil;
|
||||
let s0nd: *node = nil;
|
||||
let pseudo: str = "";
|
||||
let delta: i64 = 0i64;
|
||||
let i: i32 = nsteps - 1;
|
||||
for (i >= 0) {
|
||||
let csi: *structinfo = structlookup(c, curstruct);
|
||||
if (csi == nil) { return false; };
|
||||
// Materialise the *node first; wwstage's cgen mis-emits the
|
||||
// chained shape `stk[i].str` directly (task #8 — loses BX
|
||||
// between the index load and the field deref), so always
|
||||
// spill to an intermediate local before reading the str
|
||||
// field. The cstage emits the same pattern for byte-identity.
|
||||
stepnd = stk[i];
|
||||
if (stepnd == nil) { return false; };
|
||||
stepnm = stepnd.str;
|
||||
fi = csi.fields;
|
||||
found = nil;
|
||||
for (fi != nil) {
|
||||
if (streq(fi.fname, stepnm)) { found = fi; break; };
|
||||
fi = fi.finext;
|
||||
};
|
||||
if (found == nil) { return false; };
|
||||
if (i == 0) {
|
||||
out.totaloff = out.totaloff + (found.foff: i64);
|
||||
out.leaffi = found;
|
||||
return true;
|
||||
};
|
||||
// Intermediate step. Must be a nested value-struct, OR a slice/
|
||||
// str field with the leaf (i == 1, stk[0]) as a pseudo-field.
|
||||
ft = found.tnode;
|
||||
if (ft == nil) { return false; };
|
||||
if (ft.kind == nkind.N_TNAME) {
|
||||
if (streq(ft.str, "str")) {
|
||||
if (i != 1) { return false; };
|
||||
s0nd = stk[0];
|
||||
if (s0nd == nil) { return false; };
|
||||
pseudo = s0nd.str;
|
||||
delta = -1i64;
|
||||
if (streq(pseudo, "ptr")) { delta = 0i64; }
|
||||
else { if (streq(pseudo, "len")) { delta = 8i64; }; };
|
||||
if (delta < 0i64) { return false; };
|
||||
out.totaloff = out.totaloff + (found.foff: i64);
|
||||
out.slicedelta = delta;
|
||||
return true;
|
||||
};
|
||||
if (primsize(ft.str) != 0) { return false; };
|
||||
// Nested value-struct (named).
|
||||
out.totaloff = out.totaloff + (found.foff: i64);
|
||||
curstruct = ft.str;
|
||||
i -= 1;
|
||||
} else { if (ft.kind == nkind.N_TSLICE) {
|
||||
if (i != 1) { return false; };
|
||||
s0nd = stk[0];
|
||||
if (s0nd == nil) { return false; };
|
||||
pseudo = s0nd.str;
|
||||
delta = -1i64;
|
||||
if (streq(pseudo, "ptr")) { delta = 0i64; }
|
||||
else { if (streq(pseudo, "len")) { delta = 8i64; }
|
||||
else { if (streq(pseudo, "cap")) { delta = 16i64; }; }; };
|
||||
if (delta < 0i64) { return false; };
|
||||
out.totaloff = out.totaloff + (found.foff: i64);
|
||||
out.slicedelta = delta;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}; };
|
||||
};
|
||||
return false;
|
||||
};
|
||||
|
||||
@@ -7778,6 +7778,175 @@ fn cgwidentaggedstore(c: *cgen, dst: *node, src: *node, slot_off: i32, slot_sz:
|
||||
return;
|
||||
};
|
||||
|
||||
// dotchain — packed result struct for dotchainresolve. Out-params are
|
||||
// bundled to keep the helper at <= 6 register-passed args; wwstage's
|
||||
// per-fn arg-frame computation over-allocates by 16 bytes for any
|
||||
// function with > 6 args (task #7, a pre-existing quirk independent
|
||||
// of this fix), which would silently break the bootstrap fixed-point
|
||||
// gate (993 / 994 / 995).
|
||||
//
|
||||
// Numeric fields are all i64, not i32. wwstage zero-inits an i32
|
||||
// local with MOVQ (8-byte store) but subsequent `out.totaloff = …`
|
||||
// updates would emit MOVL (4-byte store), leaving the upper 4 bytes
|
||||
// stale from the wider init. Keeping the out-params at i64 makes the
|
||||
// init width and the update width agree, so the field reads back
|
||||
// what was written across both stages.
|
||||
type dotchain = struct {
|
||||
rootname: str,
|
||||
rootoff: i64,
|
||||
totaloff: i64,
|
||||
leaffi: *fieldinfo,
|
||||
slicedelta: i64,
|
||||
isglobal: bool,
|
||||
};
|
||||
|
||||
// Spine-walk a chained N_DOT (n) inward to a root ident, summing field
|
||||
// offsets through value-struct intermediates. Optional slice/str leaf
|
||||
// pseudo-field (.ptr / .len / .cap) on the last segment is folded into
|
||||
// `out.slicedelta` (0/8/16); otherwise out.leaffi is the leaf fieldinfo
|
||||
// and slicedelta stays -1. Returns true on success; on false the caller
|
||||
// falls through to other branches.
|
||||
//
|
||||
// Mirrors cmd/w6c/cgen.c's N_DOT chained walker; both stages must agree
|
||||
// on the same shapes so the bootstrap fixed-point holds. The chain
|
||||
// depth is capped at 16 — deeper chains are vanishingly rare and fall
|
||||
// through.
|
||||
//
|
||||
// On success the caller emits one load/store at root_base + out.totaloff
|
||||
// (+ slicedelta for pseudo leaf). Root resolves as: local frame slot
|
||||
// (out.rootoff != 0, isglobal false) or top-level let (isglobal true,
|
||||
// root accessed via LEAQ name(SB), CX).
|
||||
export fn dotchainresolve(c: *cgen, n: *node, out: *dotchain) bool = {
|
||||
out.rootname = "";
|
||||
out.rootoff = 0i64;
|
||||
out.isglobal = false;
|
||||
out.totaloff = 0i64;
|
||||
out.leaffi = nil;
|
||||
out.slicedelta = -1i64;
|
||||
if (n == nil) { return false; };
|
||||
if (n.kind != nkind.N_DOT) { return false; };
|
||||
// Walk inward, recording the N_DOT node at each step (leaf first).
|
||||
// We hold *node pointers (8B each, slotsize-stable across stages)
|
||||
// and read .str on demand — a [16]str array would mis-slot at
|
||||
// wwstage where slotsize("str") returns 8, breaking the bootstrap
|
||||
// fixed-point.
|
||||
let stk: [16]*node;
|
||||
let nsteps: i32 = 0;
|
||||
let cur: *node = n;
|
||||
for (cur != nil) {
|
||||
if (cur.kind != nkind.N_DOT) { break; };
|
||||
if (nsteps >= 16) { return false; };
|
||||
stk[nsteps] = cur;
|
||||
nsteps += 1;
|
||||
cur = cur.lhs;
|
||||
};
|
||||
if (nsteps < 2) { return false; };
|
||||
if (cur == nil) { return false; };
|
||||
if (cur.kind != nkind.N_IDENT) { return false; };
|
||||
out.rootname = cur.str;
|
||||
// Resolve the root's struct type and base.
|
||||
let rootstruct: str = "";
|
||||
let lc: *local = localfindnode(c, cur.str);
|
||||
let gsi: *structinfo = nil;
|
||||
if (lc != nil) {
|
||||
if (lc.tnode != nil) {
|
||||
if (lc.tnode.kind == nkind.N_TNAME) {
|
||||
rootstruct = lc.tnode.str;
|
||||
out.rootoff = lc.off: i64;
|
||||
};
|
||||
};
|
||||
};
|
||||
if (rootstruct.len == 0) {
|
||||
gsi = letvarstructinfo(c, cur.str);
|
||||
if (gsi != nil) {
|
||||
rootstruct = gsi.sname;
|
||||
out.isglobal = true;
|
||||
};
|
||||
};
|
||||
if (rootstruct.len == 0) { return false; };
|
||||
// Walk outward, resolving each field. stk is leaf-first; iterate
|
||||
// from i = nsteps - 1 (the root-most field) down to i = 0 (leaf).
|
||||
let curstruct: str = rootstruct;
|
||||
// Pre-declare per-iteration spills here so cstage / wwstage agree
|
||||
// on the frame layout. Both must emit byte-identical asm for the
|
||||
// bootstrap fixed-point (tests 993/995) — letting these locals get
|
||||
// declared inside the branch bodies trips a per-stage divergence in
|
||||
// slot counting.
|
||||
let stepnd: *node = nil;
|
||||
let stepnm: str = "";
|
||||
let fi: *fieldinfo = nil;
|
||||
let found: *fieldinfo = nil;
|
||||
let ft: *node = nil;
|
||||
let s0nd: *node = nil;
|
||||
let pseudo: str = "";
|
||||
let delta: i64 = 0i64;
|
||||
let i: i32 = nsteps - 1;
|
||||
for (i >= 0) {
|
||||
let csi: *structinfo = structlookup(c, curstruct);
|
||||
if (csi == nil) { return false; };
|
||||
// Materialise the *node first; wwstage's cgen mis-emits the
|
||||
// chained shape `stk[i].str` directly (task #8 — loses BX
|
||||
// between the index load and the field deref), so always
|
||||
// spill to an intermediate local before reading the str
|
||||
// field. The cstage emits the same pattern for byte-identity.
|
||||
stepnd = stk[i];
|
||||
if (stepnd == nil) { return false; };
|
||||
stepnm = stepnd.str;
|
||||
fi = csi.fields;
|
||||
found = nil;
|
||||
for (fi != nil) {
|
||||
if (streq(fi.fname, stepnm)) { found = fi; break; };
|
||||
fi = fi.finext;
|
||||
};
|
||||
if (found == nil) { return false; };
|
||||
if (i == 0) {
|
||||
out.totaloff = out.totaloff + (found.foff: i64);
|
||||
out.leaffi = found;
|
||||
return true;
|
||||
};
|
||||
// Intermediate step. Must be a nested value-struct, OR a slice/
|
||||
// str field with the leaf (i == 1, stk[0]) as a pseudo-field.
|
||||
ft = found.tnode;
|
||||
if (ft == nil) { return false; };
|
||||
if (ft.kind == nkind.N_TNAME) {
|
||||
if (streq(ft.str, "str")) {
|
||||
if (i != 1) { return false; };
|
||||
s0nd = stk[0];
|
||||
if (s0nd == nil) { return false; };
|
||||
pseudo = s0nd.str;
|
||||
delta = -1i64;
|
||||
if (streq(pseudo, "ptr")) { delta = 0i64; }
|
||||
else { if (streq(pseudo, "len")) { delta = 8i64; }; };
|
||||
if (delta < 0i64) { return false; };
|
||||
out.totaloff = out.totaloff + (found.foff: i64);
|
||||
out.slicedelta = delta;
|
||||
return true;
|
||||
};
|
||||
if (primsize(ft.str) != 0) { return false; };
|
||||
// Nested value-struct (named).
|
||||
out.totaloff = out.totaloff + (found.foff: i64);
|
||||
curstruct = ft.str;
|
||||
i -= 1;
|
||||
} else { if (ft.kind == nkind.N_TSLICE) {
|
||||
if (i != 1) { return false; };
|
||||
s0nd = stk[0];
|
||||
if (s0nd == nil) { return false; };
|
||||
pseudo = s0nd.str;
|
||||
delta = -1i64;
|
||||
if (streq(pseudo, "ptr")) { delta = 0i64; }
|
||||
else { if (streq(pseudo, "len")) { delta = 8i64; }
|
||||
else { if (streq(pseudo, "cap")) { delta = 16i64; }; }; };
|
||||
if (delta < 0i64) { return false; };
|
||||
out.totaloff = out.totaloff + (found.foff: i64);
|
||||
out.slicedelta = delta;
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}; };
|
||||
};
|
||||
return false;
|
||||
};
|
||||
|
||||
// MODULE: wcc
|
||||
// selfhost/cmd/wcc/cgenexpr.ww — split out of cgen.ww.
|
||||
//
|
||||
@@ -9324,6 +9493,99 @@ fn cgdot(c: *cgen, n: *node) void = {
|
||||
return;
|
||||
};
|
||||
};
|
||||
// Chained N_DOT spine through value-struct fields (any depth).
|
||||
// Walks the spine to a root ident, summing field offsets, then
|
||||
// emits ONE load at base + total_off. Also handles a slice/str
|
||||
// pseudo-field leaf (`b.buf.len`): the walk lands on the slice/
|
||||
// str header and slicedelta picks ptr/len/cap. Mirror of cstage
|
||||
// cgen.c's chained-DOT read branch. Without this, depth ≥ 3
|
||||
// shapes (`v.a.a.a`) and `b.buf.len` fall through to the non-
|
||||
// ident-base pseudo branch below — which would cgexpr the inner
|
||||
// (loading only .ptr into AX) and shuffle stale BX into AX.
|
||||
// Placed BEFORE the .ptr/.len fast paths so the chain wins.
|
||||
if (lhs != nil) {
|
||||
if (lhs.kind == nkind.N_DOT) {
|
||||
let r: dotchain;
|
||||
let pok: bool = dotchainresolve(c, n, &r);
|
||||
if (pok) {
|
||||
if (r.slicedelta >= 0i64) {
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\tMOVQ\t");
|
||||
emitdispreg(r.totaloff + r.slicedelta, "CX");
|
||||
emitline(", AX\n");
|
||||
} else {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(r.rootoff + r.totaloff + r.slicedelta);
|
||||
emitline("(BP), AX\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
if (isstrtype(c, r.leaffi.tnode)) {
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\tMOVQ\t");
|
||||
emitdispreg(r.totaloff + 0i64, "CX");
|
||||
emitline(", AX\n");
|
||||
emitline("\tMOVQ\t");
|
||||
emitdispreg(r.totaloff + 8i64, "CX");
|
||||
emitline(", BX\n");
|
||||
} else {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff(r.rootoff + r.totaloff + 8i64);
|
||||
emitline("(BP), BX\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
if (isfloattype(c, r.leaffi.tnode)) {
|
||||
let mov: str = "MOVSD";
|
||||
if (isf32type(c, r.leaffi.tnode)) { mov = "MOVSS"; };
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\t");
|
||||
emitdispreg(r.totaloff, "CX");
|
||||
emitline(", X0\n");
|
||||
} else {
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\t");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP), X0\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
let lop: str = fieldloadop(r.leaffi);
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\t");
|
||||
emitline(lop);
|
||||
emitline("\t");
|
||||
emitdispreg(r.totaloff, "CX");
|
||||
emitline(", AX\n");
|
||||
} else {
|
||||
emitline("\t");
|
||||
emitline(lop);
|
||||
emitline("\t");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP), AX\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
};
|
||||
};
|
||||
// Non-ident base pseudo-field: e.g. `"abc".ptr` / `"abc".len`.
|
||||
// Evaluate the str-producing expression — that leaves
|
||||
// (AX=ptr, BX=len). Then `.ptr` returns AX as is; `.len`
|
||||
@@ -9393,6 +9655,9 @@ fn cgdot(c: *cgen, n: *node) void = {
|
||||
// field. Mirror of the cgassign branch added for the same shape.
|
||||
// Without this, `L.cur.kind` (cur a by-value struct of *L)
|
||||
// falls into the SB-fallback and emits `MOVQ kind(SB), AX`.
|
||||
// Kept as a fallback below the generalized walker above (placed
|
||||
// earlier in cgdot) to preserve byte-identical output on shapes
|
||||
// it already handles.
|
||||
if (lhs != nil) {
|
||||
if (lhs.kind == nkind.N_DOT) {
|
||||
let inner: *node = lhs.lhs;
|
||||
@@ -11158,6 +11423,101 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
};
|
||||
};
|
||||
};
|
||||
// Chained N_DOT spine write through value-struct fields (any
|
||||
// depth) — `o.i.a = 10`, `v.a.b.c = …`. Also handles a slice/str
|
||||
// pseudo-field leaf (`b.buf.len = 5`). Mirror of cstage cgen.c's
|
||||
// chained-DOT write branch. Without this, depth ≥ 3 writes and
|
||||
// the slice/str pseudo-field write through a value-struct chain
|
||||
// silently emit no store. Only plain `=` is wired.
|
||||
if (lhs != nil) {
|
||||
if (lhs.kind == nkind.N_DOT && lhs.lhs != nil
|
||||
&& lhs.lhs.kind == nkind.N_DOT
|
||||
&& n.op == tkind.TK_ASSIGN) {
|
||||
let r: dotchain;
|
||||
let yok: bool = dotchainresolve(c, lhs, &r);
|
||||
if (yok) {
|
||||
if (r.slicedelta >= 0i64) {
|
||||
cgexpr(c, n.rhs);
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitdispreg(r.totaloff + r.slicedelta, "CX");
|
||||
emitline("\n");
|
||||
} else {
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff(r.rootoff + r.totaloff + r.slicedelta);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
if (isstrtype(c, r.leaffi.tnode)) {
|
||||
cgexpr(c, n.rhs);
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitdispreg(r.totaloff + 0i64, "CX");
|
||||
emitline("\n");
|
||||
emitline("\tMOVQ\tBX, ");
|
||||
emitdispreg(r.totaloff + 8i64, "CX");
|
||||
emitline("\n");
|
||||
} else {
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP)\n");
|
||||
emitline("\tMOVQ\tBX, ");
|
||||
emitoff(r.rootoff + r.totaloff + 8i64);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
if (isfloattype(c, r.leaffi.tnode)) {
|
||||
let mov: str = "MOVSD";
|
||||
if (isf32type(c, r.leaffi.tnode)) { mov = "MOVSS"; };
|
||||
cgexpr(c, n.rhs);
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\tX0, ");
|
||||
emitdispreg(r.totaloff, "CX");
|
||||
emitline("\n");
|
||||
} else {
|
||||
emitline("\t");
|
||||
emitline(mov);
|
||||
emitline("\tX0, ");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
let sop: str = fieldstoreop(r.leaffi);
|
||||
cgexpr(c, n.rhs);
|
||||
if (r.isglobal) {
|
||||
emitline("\tLEAQ\t");
|
||||
emitsymname(c, r.rootname);
|
||||
emitline("(SB), CX\n");
|
||||
emitline("\t");
|
||||
emitline(sop);
|
||||
emitline("\tAX, ");
|
||||
emitdispreg(r.totaloff, "CX");
|
||||
emitline("\n");
|
||||
} else {
|
||||
emitline("\t");
|
||||
emitline(sop);
|
||||
emitline("\tAX, ");
|
||||
emitoff(r.rootoff + r.totaloff);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
return;
|
||||
};
|
||||
};
|
||||
};
|
||||
// Chained `(ident).f1.f2 = v` where f1 is a struct-by-value
|
||||
// field. The earlier chained-DOT branch handles f1: *T (deref
|
||||
// then store). This handles f1: T (in-place sub-struct), which
|
||||
@@ -11165,6 +11525,8 @@ fn cgassign(c: *cgen, n: *node) void = {
|
||||
// to flatten `cur.kind`/`cur.ival`/... into top-level fields to
|
||||
// work around it. Only plain `=` is wired; compound on a by-
|
||||
// value sub-field hasn't surfaced.
|
||||
// Kept as fallback below the generalized walker for any shape
|
||||
// the walker doesn't recognize.
|
||||
if (lhs != nil) {
|
||||
if (lhs.kind == nkind.N_DOT) {
|
||||
let base: *node = lhs.lhs;
|
||||
|
||||
221
test/wcc/650_dot_chain.c
Normal file
221
test/wcc/650_dot_chain.c
Normal file
@@ -0,0 +1,221 @@
|
||||
/*
|
||||
* 650_dot_chain — chained dotted access through value-struct fields
|
||||
* must compile to a single load/store at (base + sum-of-offsets),
|
||||
* not silently lower to an undefined global symbol or shuffle stale
|
||||
* registers. Drew filed this from worker-bufio when the Hare-shaped
|
||||
* `scanner` embed in lib/bufio tripped the cgen.
|
||||
*
|
||||
* Each fixture exercises a different chain shape. The repro from
|
||||
* drew's bug report is row[0]; the rest pin: 3-deep chains, slice
|
||||
* pseudo-field tails (`s.buf.len`), global-rooted chains, and
|
||||
* mixed leaf widths (u8 / i32 / u32). `&o.i.a` address-of and i8
|
||||
* sign-extend leaves are deferred (tasks #9 / #10).
|
||||
*
|
||||
* Exercises both stages via `ww` (cstage) and `ww_ww` (wwstage)
|
||||
* when present, so a regression on either side is caught here.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
static int
|
||||
runwait(const char *cmd)
|
||||
{
|
||||
int rc = system(cmd);
|
||||
if (rc == -1) return -1;
|
||||
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct row { const char *label; const char *src; int want; };
|
||||
|
||||
static const struct row rows[] = {
|
||||
/* Drew's repro: 2-deep value-struct read AND write. Returns
|
||||
* o.i.a (=10) + o.x (=5) = 15. */
|
||||
{ "drew_2deep_read_write",
|
||||
"type inner = struct { a: i32, b: i32, c: i32 };\n"
|
||||
"type outer = struct { i: inner, x: i32 };\n"
|
||||
"fn main() i32 = {\n"
|
||||
" let o: outer; o.i.a = 10; o.x = 5;\n"
|
||||
" return o.i.a + o.x;\n"
|
||||
"};\n",
|
||||
15 },
|
||||
/* 3-deep value-struct chain. Proves the spine walker is loop-
|
||||
* shaped, not hardcoded to depth 2. */
|
||||
{ "value_struct_3deep",
|
||||
"type a3 = struct { a: i32 };\n"
|
||||
"type a2 = struct { a: a3 };\n"
|
||||
"type a1 = struct { a: a2 };\n"
|
||||
"fn main() i32 = {\n"
|
||||
" let v: a1;\n"
|
||||
" v.a.a.a = 7;\n"
|
||||
" return v.a.a.a;\n"
|
||||
"};\n",
|
||||
7 },
|
||||
/* Slice pseudo-field tail through a value-struct field: write
|
||||
* .ptr/.len/.cap on s.buf where buf: []u8 is a struct field.
|
||||
* Read back .len through the same chain. */
|
||||
{ "slice_field_pseudo",
|
||||
"type bag = struct { buf: []u8, x: i32 };\n"
|
||||
"fn main() i32 = {\n"
|
||||
" let arr: [8]u8;\n"
|
||||
" let i: i32 = 0;\n"
|
||||
" for (i < 8) { arr[i] = i: u8; i = i + 1; };\n"
|
||||
" let b: bag;\n"
|
||||
" b.buf.ptr = &arr[0];\n"
|
||||
" b.buf.len = 5;\n"
|
||||
" b.buf.cap = 8;\n"
|
||||
" b.x = 0;\n"
|
||||
" return b.buf.len;\n"
|
||||
"};\n",
|
||||
5 },
|
||||
/* u8 leaf through a chained struct: round-trips a single byte
|
||||
* unchanged (no narrow cast in play — direct MOVB write +
|
||||
* MOVZBQ read). Pins the leaf-width dispatch in the walker. */
|
||||
{ "u8_leaf_through_chain",
|
||||
"type holder = struct { val: u8, pad: u8 };\n"
|
||||
"type box = struct { h: holder, tag: i32 };\n"
|
||||
"fn main() i32 = {\n"
|
||||
" let b: box;\n"
|
||||
" b.h.val = 99u8;\n"
|
||||
" b.h.pad = 0u8;\n"
|
||||
" b.tag = 0;\n"
|
||||
" return b.h.val: i32;\n"
|
||||
"};\n",
|
||||
99 },
|
||||
/* i32 leaf through a chained struct, value > 255 to confirm the
|
||||
* MOVL store width (not silently narrowed to MOVB). Returns 42
|
||||
* iff the full 4-byte value round-trips. */
|
||||
{ "i32_leaf_through_chain",
|
||||
"type holder = struct { val: i32, pad: i32 };\n"
|
||||
"type box = struct { h: holder, tag: i32 };\n"
|
||||
"fn main() i32 = {\n"
|
||||
" let b: box;\n"
|
||||
" b.h.val = 0x12345;\n"
|
||||
" b.h.pad = 0;\n"
|
||||
" b.tag = 0;\n"
|
||||
" if (b.h.val == 0x12345) { return 42; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
42 },
|
||||
/* u32 leaf through a chained struct. Distinct write + read at
|
||||
* the chained leaf, value chosen so the MOVL store + load
|
||||
* round-trip preserves all four bytes. */
|
||||
{ "u32_leaf_through_chain",
|
||||
"type holder = struct { val: u32, pad: u32 };\n"
|
||||
"type box = struct { h: holder, tag: i32 };\n"
|
||||
"fn main() i32 = {\n"
|
||||
" let b: box;\n"
|
||||
" b.h.val = 4321u32;\n"
|
||||
" b.h.pad = 0u32;\n"
|
||||
" b.tag = 0;\n"
|
||||
" if (b.h.val == 4321u32) { return 42; };\n"
|
||||
" return 0;\n"
|
||||
"};\n",
|
||||
42 },
|
||||
/* Global-rooted chain: write+read through a top-level `let g`.
|
||||
* Pins the LEAQ name(SB), CX → MOVQ disp(CX) path on both
|
||||
* stages (the local-rooted rows above pin the BP-relative
|
||||
* path). 11 + 3 = 14. */
|
||||
{ "global_root_chain",
|
||||
"type inner = struct { a: i32, b: i32 };\n"
|
||||
"type outer = struct { i: inner, x: i32 };\n"
|
||||
"let g: outer;\n"
|
||||
"fn main() i32 = {\n"
|
||||
" g.i.a = 11; g.x = 3;\n"
|
||||
" return g.i.a + g.x;\n"
|
||||
"};\n",
|
||||
14 },
|
||||
};
|
||||
|
||||
static int
|
||||
run_driver(const char *driver, const struct row *r, int i)
|
||||
{
|
||||
char src[64], tmpdir[64], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/wdc_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/wdc_%d_d_%d", getpid(), i);
|
||||
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(r->src, f);
|
||||
fclose(f);
|
||||
|
||||
mkdir(tmpdir, 0755);
|
||||
snprintf(cmd, sizeof cmd, "cd %s && %s build %s",
|
||||
tmpdir, driver, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: build via %s failed\n",
|
||||
r->label, driver);
|
||||
unlink(src); rmdir(tmpdir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *base = strrchr(src, '/');
|
||||
base = base ? base + 1 : src;
|
||||
char outbin[128];
|
||||
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
|
||||
char *dot = strrchr(outbin, '.');
|
||||
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
||||
int got = runwait(outbin);
|
||||
|
||||
unlink(src); unlink(outbin); rmdir(tmpdir);
|
||||
return got;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const char *bin = getenv("BIN");
|
||||
if (!bin) bin = "out/bin";
|
||||
char absbin[1024];
|
||||
if (bin[0] != '/') {
|
||||
char cwd[1024];
|
||||
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
|
||||
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
|
||||
bin = absbin;
|
||||
}
|
||||
|
||||
char cdrv[1024];
|
||||
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
||||
char wdrv[1024];
|
||||
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
||||
|
||||
struct { const char *name; const char *path; int gated_on_existence; }
|
||||
drivers[] = {
|
||||
{ "cstage", cdrv, 0 },
|
||||
{ "wwstage", wdrv, 1 },
|
||||
{ NULL, NULL, 0 },
|
||||
};
|
||||
|
||||
int n = (int)(sizeof rows / sizeof rows[0]);
|
||||
int total = 0, fail = 0;
|
||||
for (int d = 0; drivers[d].name; d++) {
|
||||
if (drivers[d].gated_on_existence
|
||||
&& access(drivers[d].path, X_OK) != 0) {
|
||||
fprintf(stderr, "dot_chain: skip %s (no %s)\n",
|
||||
drivers[d].name, drivers[d].path);
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
int got = run_driver(drivers[d].path, &rows[i], i);
|
||||
total++;
|
||||
if (got != rows[i].want) {
|
||||
fprintf(stderr,
|
||||
"dot_chain[%s][%s]: exit=%d want=%d\n",
|
||||
drivers[d].name, rows[i].label,
|
||||
got, rows[i].want);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (fail) {
|
||||
fprintf(stderr,
|
||||
"dot_chain: %d/%d fixtures failed\n", fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("dot_chain: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user