selfhost+cstage+test: graduate alias-chain unwrap to transitive (#22)

Single-peel TY_NAMED.under bottoms out at the inner alias when
chain length is 2+, surfaces in two stages with different
mechanisms: cstage's gates inline `if (t->kind == TY_NAMED)
t = t->under` at every callsite (cgreturn, cglet sizing, cgexpr
N_DOT, cgassign N_DOT, cg_sret_retsize) — graduated to a
while-loop via new type_chase_named helper across 11 sites.
wwstage routes all field-walks through structlookup, which
registers only direct struct definitions (not aliases) — missing
the alias-recurse fallback. New structlookupchain helper mirrors
slotsize's N_TARRAY arm precedent; sretretsize + 4 cgenexpr.ww
sites route through it. Splitting would either land cstage
without unblocking wwstage's strings.tokenize wrapper shape
(rule 10 byte-id regression) or land wwstage without cstage
gate parity (breaking 995 self-rebuild). 756 sentinel exercises
4 rows × cstage RC + wwstage RC + byte-id = 12 fixtures; pre-fix
rows 2 + 4 (slice-fields single alias, i32 double alias) fail
on both RC and byte-id. The ~67 cstage / ~26 wwstage candidate
sibling sites are #17-style structural-close follow-up; this
commit fixes the immediate strings.tokenize-wrapper blockers.
This commit is contained in:
2026-05-19 15:09:57 +09:00
parent 7e1b681701
commit a1d9f36d11
7 changed files with 513 additions and 40 deletions

View File

@@ -123,6 +123,19 @@ cg_isfloat(Type *t)
|| t->kind == TY_UNTYPED_FLOAT;
}
/* type_chase_named — walk the TY_NAMED.under chain to the deepest non-
* named type. Chain-of-aliases (#22): `type b = a; type a = struct;`
* stacks two TY_NAMED layers — a single peel leaves `t` pointing at
* the inner alias (still TY_NAMED), so kind-gated arms (TY_STRUCT,
* TY_SLICE, TY_TAGGED, TY_PTR) miss and the codegen silently falls
* through to a scalar shape. Mirror of wwstage's structlookupchain. */
static Type *
type_chase_named(Type *t)
{
while (t && t->kind == TY_NAMED) t = t->under;
return t;
}
/* cg_sret_retsize — if `rt` is a plain TY_STRUCT > 24B, return its
* natural size (the sret threshold); else 0. Tagged unions, tuples,
* str, and slices route through their existing register-return ABIs
@@ -130,8 +143,7 @@ cg_isfloat(Type *t)
static int
cg_sret_retsize(Type *rt)
{
if (rt == NULL) return 0;
if (rt->kind == TY_NAMED) rt = rt->under;
rt = type_chase_named(rt);
if (rt == NULL || rt->kind != TY_STRUCT) return 0;
if ((int)rt->size <= 24) return 0;
return (int)rt->size;
@@ -2408,12 +2420,16 @@ cgexpr(Cg *c, Node *n, Local *locals)
Node *base = n->lhs->lhs;
if (base->kind == N_UN) base = base->lhs;
Type *bt = base->type;
Type *u = (bt && bt->kind == TY_NAMED) ? bt->under : bt;
/* type_chase_named (#22): a chain `type b = a; a = struct`
* left u at TY_NAMED a after a single peel, missing the
* TY_STRUCT field-walk gate below — the assignment
* silently dropped (the `break` at the bottom of the
* N_DOT-lhs arm). */
Type *u = type_chase_named(bt);
int via_ptr = 0;
if (u && u->kind == TY_PTR) {
via_ptr = 1;
u = u->sub;
if (u && u->kind == TY_NAMED) u = u->under;
u = type_chase_named(u->sub);
}
/* slice/str pseudo-field write (.ptr/.len/.cap) */
if (u && (u->kind == TY_SLICE || u->kind == TY_STR)) {
@@ -3013,10 +3029,11 @@ cgexpr(Cg *c, Node *n, Local *locals)
if (n->lhs && n->lhs->kind == N_DOT && n->lhs->lhs
&& n->lhs->lhs->kind != N_IDENT) {
Type *bt = n->lhs->lhs->type;
Type *bu = (bt && bt->kind == TY_NAMED) ? bt->under : bt;
/* type_chase_named (#22); same rationale as the cgexpr-
* side pointer-to-struct field branch. */
Type *bu = type_chase_named(bt);
if (bu && bu->kind == TY_PTR && bu->sub) {
Type *inner = bu->sub;
if (inner->kind == TY_NAMED) inner = inner->under;
Type *inner = type_chase_named(bu->sub);
if (inner && inner->kind == TY_STRUCT) {
Tfield *f = NULL;
for (Tfield *fl = inner->fields; fl; fl = fl->next)
@@ -5325,7 +5342,12 @@ cgexpr(Cg *c, Node *n, Local *locals)
&& dot_lhs->lhs && dot_lhs->lhs->kind == N_IDENT)
dot_lhs = dot_lhs->lhs;
Type *bt = dot_lhs ? dot_lhs->type : NULL;
Type *u = (bt && bt->kind == TY_NAMED) ? bt->under : bt;
/* type_chase_named (#22): `type b = a; type a = struct;` stacks
* two TY_NAMED layers; single peel left `u` still TY_NAMED,
* missing the TY_STRUCT field-walk gate below and collapsing
* `s.field` to a base-only MOVQ read (offset 0 instead of
* the field's declared offset). */
Type *u = type_chase_named(bt);
/* Module-qualified value reference: `mod.name`. The checker
* leaves SK_USE idents untyped (NULL/ty_err); detect that and
* look up the leaf in the flat (driver-concatenated) sym/def
@@ -5797,8 +5819,7 @@ cgexpr(Cg *c, Node *n, Local *locals)
* IDENT)) emits the same load as `p.len` after the case-top
* retarget. */
if (u && u->kind == TY_PTR && u->sub) {
Type *inner = u->sub;
if (inner->kind == TY_NAMED) inner = inner->under;
Type *inner = type_chase_named(u->sub);
if (inner && (inner->kind == TY_SLICE || inner->kind == TY_STR)
&& (lenfld || capfld || ptrfld)
&& dot_lhs && dot_lhs->kind == N_IDENT) {
@@ -5813,10 +5834,15 @@ cgexpr(Cg *c, Node *n, Local *locals)
* fn move(p: *point) ... { p.x += dx; ... }
* dot_lhs gates this branch so both `p.f` (n->lhs is IDENT)
* and `(*p).f` (n->lhs is N_UN(STAR, IDENT), retargeted to
* inner IDENT at case-top) emit the same load sequence. */
* inner IDENT at case-top) emit the same load sequence.
*
* type_chase_named (#22): `type b = a;` inside the pointer
* (`*b`) leaves a single peel still at TY_NAMED. Bites the
* strings.tokenize wrapper shape — caller signature
* `next_token(s: *strings.tokenizer)` where strings.tokenizer
* aliases bytes.tokenizer. */
if (u && u->kind == TY_PTR && u->sub) {
Type *inner = u->sub;
if (inner->kind == TY_NAMED) inner = inner->under;
Type *inner = type_chase_named(u->sub);
if (inner && inner->kind == TY_STRUCT
&& dot_lhs && dot_lhs->kind == N_IDENT) {
int off = localfind(locals, dot_lhs->str);
@@ -6290,7 +6316,12 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
break;
case N_LET: {
Type *lt = n->type;
Type *lu = (lt && lt->kind == TY_NAMED) ? lt->under : lt;
/* type_chase_named (#22): a chain `type a = struct{...};
* type b = a;` stacks two TY_NAMED layers. A single peel
* left `lu` pointing at the inner alias (still TY_NAMED),
* collapsed the struct/slice/tagged sizing arms to the 8B
* fallback, and the slot under-allocated the local. */
Type *lu = type_chase_named(lt);
int sz = 8;
if (lu && lu->kind == TY_ARRAY) sz = (int)lu->size;
else if (lu && lu->kind == TY_SLICE) sz = 24;
@@ -6829,8 +6860,14 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
* SysV sret discipline of "return the pointer". No
* AX/DX/CX shuffle, no scratch slot beyond @sretarg. */
if (n->lhs && cg_ret_type && cg_sret_arg_off != 0) {
Type *rt = cg_ret_type;
if (rt->kind == TY_NAMED) rt = rt->under;
/* type_chase_named (#22). A single peel left `rt` still
* TY_NAMED when the declared return type is `type b
* = a;` where a is itself a NAMED alias of a struct,
* so the TY_STRUCT gate below missed and the sret
* return arm fell through to the scalar-AX default —
* corrupting the caller's receive slot even though
* the prologue wired @sretarg. */
Type *rt = type_chase_named(cg_ret_type);
/* sret return-forwarding (task #9 follow-up to #23):
* `return f();` where outer + inner both return the
* same >24B struct shape. Outer's @sretarg already
@@ -6939,8 +6976,10 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame)
* N_STRUCTLIT (field-by-field store at scratch+foff). Call-
* result chain return is deferred to #5's receive side. */
if (n->lhs && cg_ret_type) {
Type *rt = cg_ret_type;
if (rt->kind == TY_NAMED) rt = rt->under;
/* type_chase_named (#22); see the >24B sret arm above
* for the same rationale. The ≤24B register-return
* ABI uses the same TY_STRUCT gate. */
Type *rt = type_chase_named(cg_ret_type);
if (rt && rt->kind == TY_STRUCT && rt->size <= 24
&& (n->lhs->kind == N_IDENT
|| n->lhs->kind == N_STRUCTLIT)) {