selfhost/cmd/wcc: index-base esz from n.type_.size + delete indexbaseesz (#72, A.6.3k)
The N_INDEX node carries the checker-stamped element tinfo (#60 arc); cgindex, cgun TK_AMP, and cgassign now read the element stride off that .type_.size instead of indexbaseesz's manual N_DOT-pseudo-field + structlookup walk, retiring the helper (0 callers, ~96 LOC). Aligns down to cstage's idx_eff(base->type)->sub->size (cmd/w6c/cgen.c:3517-18, natural element size) -- strictly more cstage-faithful than indexbaseesz's totsize/slotsize derivation (byte-id held only because firing shapes have totsize==natural; cstage reads natural and ww-old==cstage, so the flip is structural). Each site keeps its nil->default-8 fallback; cgindex stays esz-only (signed_elem unset for the N_DOT base, as before). Closes the A.6.3 cgenutil-collapse arc: every type/size/offset query in cgen now reads the checker-stamped tinfo, and the AST-walker / structinfo-walk helpers it replaced (dotfieldtnode, indexvaluetnode, rhstargetname, dotinnerstructptr, indexbaseesz) are retired. make test 134/134, byte-id 990-997 hold. Coverage: 713/741/755 + self-rebuild.
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
// - pushargsrev: per-call arg pushing
|
||||
// - type predicates: isstr*/isslice*/istagged*/nodeis* families
|
||||
// - field ops: fieldloadop, fieldstoreop
|
||||
// - index helpers: indexbaseesz, elemsizeof
|
||||
// - index helpers: elemsizeof, elemsizeofc
|
||||
// - slot sizing: structlookup, primsize, slotsize, fieldsize,
|
||||
// registerstruct, collectstructs
|
||||
// - rhs helpers: taggedvariantindex
|
||||
@@ -659,8 +659,8 @@ fn nodeisstr(c: *cgen, n: *node) bool = {
|
||||
};
|
||||
// N_INDEX through a struct field: e.g. cmd.argsptr[i]
|
||||
// where argsptr: *str. cgindex correctly loads the
|
||||
// (ptr, len) pair via indexbaseesz; without this arm
|
||||
// pushargsrev would only push AX and lose the .len.
|
||||
// (ptr, len) pair off the stamped element size; without
|
||||
// this arm pushargsrev would only push AX and lose .len.
|
||||
if (base.kind == nkind.N_DOT) {
|
||||
let fld: str = base.str;
|
||||
if (streq(fld, "ptr")) { return false; };
|
||||
@@ -851,103 +851,6 @@ export fn localloadop(c: *cgen, tnode: *node) str = {
|
||||
return loadopsz(sigd, sz);
|
||||
};
|
||||
|
||||
// indexbaseesz — element size for `arr[i]` where the base is a
|
||||
// chained-dot pseudo-field `s.ptr` (s being str/*str/slice/*slice).
|
||||
// For str the element is one byte; for `[]T` / `*[]T` we drill into
|
||||
// the slice element type.
|
||||
fn indexbaseesz(c: *cgen, base: *node) i32 = {
|
||||
if (base == nil) { return 8; };
|
||||
if (base.kind != nkind.N_DOT) { return 8; };
|
||||
let fld: str = base.str;
|
||||
let inner: *node = base.lhs;
|
||||
if (inner == nil) { return 8; };
|
||||
if (inner.kind != nkind.N_IDENT) { return 8; };
|
||||
let nm: str = inner.str;
|
||||
let lc: *local = localfindnode(c, nm);
|
||||
if (lc == nil) { return 8; };
|
||||
let tn: *node = lc.tnode;
|
||||
if (tn == nil) { return 8; };
|
||||
|
||||
// `.ptr` pseudo-field on str/slice → element of the str/slice.
|
||||
// Gated on inner kind, NOT on the field name alone: a struct with
|
||||
// a literal `ptr: *T` field (lib/memio.state, lib/bufio.state) must
|
||||
// route through the generic struct-field arm below so the stride
|
||||
// comes from primsize/structlookup, not the str/slice default. The
|
||||
// over-broad pre-#21 shortcut hard-coded esz=8 and silently
|
||||
// miscompiled `m.ptr[i]` for `*u8` callers (also widened the load
|
||||
// op MOVZBQ → MOVQ in cgindex). Mirrors cstage which routes every
|
||||
// base through `base->type->sub->size` (cmd/w6c/cgen.c idx_eff).
|
||||
if (streq(fld, "ptr")) {
|
||||
let innert: *node = tn;
|
||||
if (tn.kind == nkind.N_TPTR) { innert = tn.lhs; };
|
||||
if (innert != nil) {
|
||||
if (innert.kind == nkind.N_TNAME) {
|
||||
if (streq(innert.str, "str")) { return 1; };
|
||||
};
|
||||
// Slice element: resolve through elemsizeofc so a
|
||||
// slice of a named struct (e.g. *[]option) returns
|
||||
// the struct stride instead of falling through to
|
||||
// elemsizeof's default 8.
|
||||
if (innert.kind == nkind.N_TSLICE) {
|
||||
return elemsizeofc(c, innert);
|
||||
};
|
||||
};
|
||||
// Inner is a struct N_TNAME (or unresolved) — fall through
|
||||
// to the generic struct-field arm below.
|
||||
};
|
||||
|
||||
// Generic struct field: if it's *T, element size is T's size.
|
||||
let lkind: nkind = tn.kind;
|
||||
let sname: str;
|
||||
sname.ptr = nil; sname.len = 0;
|
||||
if (lkind == nkind.N_TNAME) { sname = tn.str; };
|
||||
if (lkind == nkind.N_TPTR) {
|
||||
let pinner: *node = tn.lhs;
|
||||
if (pinner != nil) {
|
||||
if (pinner.kind == nkind.N_TNAME) { sname = pinner.str; };
|
||||
};
|
||||
};
|
||||
if (sname.len == 0) { return 8; };
|
||||
let si: *structinfo = structlookup(c, sname);
|
||||
if (si == nil) { return 8; };
|
||||
let fi: *fieldinfo = si.fields;
|
||||
for (fi != nil) {
|
||||
let fn_: str = fi.fname;
|
||||
if (streq(fn_, fld)) {
|
||||
let ft: *node = fi.tnode;
|
||||
if (ft == nil) { return 8; };
|
||||
if (ft.kind == nkind.N_TPTR) {
|
||||
let elem: *node = ft.lhs;
|
||||
if (elem != nil) {
|
||||
if (elem.kind == nkind.N_TNAME) {
|
||||
if (streq(elem.str, "str")) { return primtypesize("str"): i32; };
|
||||
let ps: i32 = primsize(elem.str);
|
||||
if (ps > 0) { return ps; };
|
||||
// Pointer to named struct: indexing
|
||||
// stride is the struct slot size.
|
||||
// Without this, &p.ptr[i] for p.ptr:
|
||||
// *S falls through to 8 and reads
|
||||
// the wrong element.
|
||||
let si: *structinfo = structlookup(c, elem.str);
|
||||
if (si != nil) { return si.totsize; };
|
||||
};
|
||||
};
|
||||
return 8;
|
||||
};
|
||||
if (ft.kind == nkind.N_TSLICE) { return elemsizeof(ft); };
|
||||
// str-typed field: indexing yields one byte
|
||||
// (`n.s[i]` where .s is str — matches C cgen's
|
||||
// MOVZBQ for byte indexing).
|
||||
if (ft.kind == nkind.N_TNAME) {
|
||||
if (streq(ft.str, "str")) { return 1; };
|
||||
};
|
||||
return 8;
|
||||
};
|
||||
fi = fi.finext;
|
||||
};
|
||||
return 8;
|
||||
};
|
||||
|
||||
// elemsizeof — given the type node of an indexable (`*T`, `[]T`,
|
||||
// `[N]T`, `str`), return the byte size of one element (1 for u8/i8/
|
||||
// bool/str-byte, 8 otherwise — same shape as C cgen's esz fallback).
|
||||
|
||||
Reference in New Issue
Block a user