From 3637993bb212e3963f1649296efc4d1939e80c55 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Sun, 24 May 2026 18:01:32 +0900 Subject: [PATCH] cgen: collapse redundant TY_STR esz special-cases onto str.sub -- Phase 2 step-3 Fold 2 F1 set str.sub = u8; the str-element-size readers no longer need a TY_STR special-case. cstage: delete the two 'if (kind==TY_STR) esz = ty_u8->size' blocks -- the general 'esz = sub->size' path already yields 1 for str (str.sub=u8), as the third index site (which never had the special-case) proves. Provably byte-id-NEUTRAL for ALL inputs: ty_str is the sole TY_STR instance and str.sub==ty_u8, so sub->size==ty_u8->size==1 in every case. No kind-gate (type_isstr/isstrtype arm-selectors) touched. wwstage elemsizeof (cgenutil.ww) is COMMENT-ONLY: it names primtypesize("u8") directly because it operates on a raw type node with no stamped tinfo at the ident-base index path (str.sub lives on .type_.sub, unstamped here -- cf. cgforrange's 'if sti != nil'); that IS the str.sub-equivalent value, identical asm. Added the WHY + retargeted the citation to the surviving cstage path. The structural collapse there is blocked on tinfo-stamping, not intent -- filed (task #24); byte-id 990-997 guards the residual coupling. Zero asm change both stages (cstage/wwstage .s byte-identical pre/post and cross-stage). main.combined.ww regenerated via the canonical make path (comment propagation only). --- cmd/w6c/cgen.c | 7 ------- selfhost/cmd/w6c/main.combined.ww | 11 ++++++++--- selfhost/cmd/wcc/cgenutil.ww | 11 ++++++++--- selfhost/cmd/wwdump/main.combined.ww | 11 ++++++++--- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/cmd/w6c/cgen.c b/cmd/w6c/cgen.c index 6cba67ff..8cbcc59e 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -2118,10 +2118,6 @@ cgexpr(Cg *c, Node *n, Local *locals) ? bt->under : bt; int esz = (bu && bu->sub) ? (int)bu->sub->size : 1; - /* str element is u8 — size via the type table - * so #1's TY_STR collapse needs no second edit. */ - if (bu && bu->kind == TY_STR) - esz = (int)ty_u8->size; cgexpr(c, idx, locals); /* idx → AX */ if (esz > 1) { ins2(c, A_MOVQ, aimm(esz), @@ -6254,9 +6250,6 @@ cgexpr(Cg *c, Node *n, Local *locals) Type *eff = idx_eff(bt); int esz = 1; if (eff && eff->sub) esz = (int)eff->sub->size; - /* str element is u8 — size via the type table so #1's - * TY_STR collapse needs no second edit. */ - if (u && u->kind == TY_STR) esz = (int)ty_u8->size; Type *esub = eff ? eff->sub : NULL; Type *esubu = (esub && esub->kind == TY_NAMED) ? esub->under : esub; diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 6c628d1f..48a28919 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -11433,9 +11433,14 @@ fn elemsizeof(t: *node) i32 = { if (k == nkind.N_TARRAY) { elem = t.lhs; }; if (k == nkind.N_TNAME) { let nm: str = t.str; - // str element is u8 — size via the type table so #1's - // TY_STR collapse needs no second edit. Cite cstage cgen.c - // N_INDEX `esz = ty_u8->size`. + // str's element is u8 (F1: tystr.sub = tyu8), named directly + // rather than read off str.sub because elemsizeof gets a raw + // N_TNAME at the ident-base index path with no checker-stamped + // tinfo — str.sub lives on .type_.sub, unstamped at this site + // (cf. cgforrange's `if (sti != nil)` guard). This IS the + // str.sub-equivalent; the structural collapse onto str.sub is + // blocked on tinfo-stamping here, not intent (#24). cstage twin + // reads eff->sub->size (cmd/w6c/cgen.c N_INDEX). if (streq(nm, "str")) { return primtypesize("u8"): i32; }; // Indexing a primitive name (rare): element size = the prim. let ps: i32 = primsize(nm); diff --git a/selfhost/cmd/wcc/cgenutil.ww b/selfhost/cmd/wcc/cgenutil.ww index 5d69602d..1dd09dda 100644 --- a/selfhost/cmd/wcc/cgenutil.ww +++ b/selfhost/cmd/wcc/cgenutil.ww @@ -872,9 +872,14 @@ fn elemsizeof(t: *node) i32 = { if (k == nkind.N_TARRAY) { elem = t.lhs; }; if (k == nkind.N_TNAME) { let nm: str = t.str; - // str element is u8 — size via the type table so #1's - // TY_STR collapse needs no second edit. Cite cstage cgen.c - // N_INDEX `esz = ty_u8->size`. + // str's element is u8 (F1: tystr.sub = tyu8), named directly + // rather than read off str.sub because elemsizeof gets a raw + // N_TNAME at the ident-base index path with no checker-stamped + // tinfo — str.sub lives on .type_.sub, unstamped at this site + // (cf. cgforrange's `if (sti != nil)` guard). This IS the + // str.sub-equivalent; the structural collapse onto str.sub is + // blocked on tinfo-stamping here, not intent (#24). cstage twin + // reads eff->sub->size (cmd/w6c/cgen.c N_INDEX). if (streq(nm, "str")) { return primtypesize("u8"): i32; }; // Indexing a primitive name (rare): element size = the prim. let ps: i32 = primsize(nm); diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index df74e2c0..56741412 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -11433,9 +11433,14 @@ fn elemsizeof(t: *node) i32 = { if (k == nkind.N_TARRAY) { elem = t.lhs; }; if (k == nkind.N_TNAME) { let nm: str = t.str; - // str element is u8 — size via the type table so #1's - // TY_STR collapse needs no second edit. Cite cstage cgen.c - // N_INDEX `esz = ty_u8->size`. + // str's element is u8 (F1: tystr.sub = tyu8), named directly + // rather than read off str.sub because elemsizeof gets a raw + // N_TNAME at the ident-base index path with no checker-stamped + // tinfo — str.sub lives on .type_.sub, unstamped at this site + // (cf. cgforrange's `if (sti != nil)` guard). This IS the + // str.sub-equivalent; the structural collapse onto str.sub is + // blocked on tinfo-stamping here, not intent (#24). cstage twin + // reads eff->sub->size (cmd/w6c/cgen.c N_INDEX). if (streq(nm, "str")) { return primtypesize("u8"): i32; }; // Indexing a primitive name (rare): element size = the prim. let ps: i32 = primsize(nm);