wcc+w6c_ww: len() over place-resolved operands (F2/FA2)

C5 (tasks #10 + #41): the len() builtin's operand handling was an arm
enumeration that leaked FOUR siblings over time (#235 tuple-elem →
#19 indexed-elem → F2 len(xs[i].field) → FA2/FB1 len(*p)) — every
unhandled slice/str operand shape fell to a bare cgexpr fallback that
returned the slice DATA POINTER as the length. Silent ptr-garbage,
byte-id both stages, gate-blind. Probing at d642017 surfaced the full
family: len(*p) (param 48 / local 64), len(xs[i].field) (147),
len((*p)[i].field) (10), len(s.field) (75), len(p.field) (87) — plus
the same garbage for non-place operands len("abc") (40),
len(xs[1:3]) (48), len(mk()) (0). Review widened it twice more:
len(**pp) (chained deref, garbage 208 at e481cb8) and the EMPTY-slice
deref (len 0 reported as .ptr — masked by exit-code truncation, hence
the branchy test row).

The enumeration is closed by construction (ken's verdict): enumerated
fast-paths keep their pre-fix asm byte-identically (ident local/global,
#235 tuple element — not resolver-addressable, cgplaceaddr has no
TY_TUPLE hop — #19 indexed element, TY_ARRAY const fold), then ONE
uniform header-place route via cgplaceaddr resolves every other
slice/str place and reads the .len word at place+8 (the same offset
math as the ident arm). Non-place operands (string literal, slicing
expr, call result) die LOUD per rule 7 — previously the same silent
ptr-garbage; Hare instead const-folds len of literals, that parity is
filed as #46. The ident arm's off==0 non-let residue (MOVQ 8(BP)
garbage) now also routes resolver-or-loud. cstage's dispatch peel is
aligned to wwstage's existing TY_NAMED loop-chase (single-peel +
loud tail would have surfaced as cs-rejects/ww-accepts on 2-level
aliases).

Asm-neutrality: all five embedded main.combined.ww corpora compile
byte-identically under pristine-parent w6c vs fixed w6c; per-shape
pins (ident local/global, tuple, index, array) NEUTRAL + cs==ww.
802_lenidx_run grows 14 rows: the nine garbage shapes (incl. computed
index through a deref spine, param-vs-local *p, chained **pp, empty
slice), two neutrality controls (global and tuple fast-paths have
dedicated runs: 797, 903), three reject rows pinning the exact rule-7
text in BOTH stages; all fix rows verified FAILING against a pristine
build of the parent e481cb8 (12/19 fail there, 19/19 green here).

Consumers unblocked: regex fold-2b tranche C ha:795/798
len(threads[i].captures); lib/regex add_thread's (*threads).len
dodge (regex.ww:238, WHY comment cites #41) reverts to len(*threads)
with the tranche-C port, not here.
This commit is contained in:
2026-06-04 13:22:41 +09:00
parent e481cb86bd
commit 796d41bb9f
5 changed files with 615 additions and 312 deletions

View File

@@ -6599,10 +6599,17 @@ cgexpr(Cg *c, Node *n, Local *locals)
if (n->lhs && n->lhs->kind == N_IDENT && n->lhs->str &&
strcmp(n->lhs->str, "len") == 0 && n->list) {
Node *a = n->list;
Type *at = a->type;
Type *u = (at && at->kind == TY_NAMED) ? at->under : at;
if (u && (u->kind == TY_SLICE || u->kind == TY_STR)
&& a->kind == N_IDENT) {
/* loop-peel: the wwstage mirror already chases
* multi-level TY_NAMED; cstage single-peeled, so a
* 2-level alias fell to the old silent fallback. With
* the loud tail below that asymmetry would surface as
* cs-rejects / ww-accepts — same predicate both
* stages. */
Type *u = type_chase_named(a->type);
int hdrish = u && (u->kind == TY_SLICE
|| u->kind == TY_STR);
int lendone = 0;
if (hdrish && a->kind == N_IDENT) {
int off = localfind(locals, a->str);
if (off == 0 && let_islet(a->str)) {
/* #231: str/slice GLOBAL — the .len word
@@ -6617,21 +6624,26 @@ cgexpr(Cg *c, Node *n, Local *locals)
areg(D_CX));
ins2(c, A_MOVQ, amem(D_CX, 8),
areg(D_AX));
} else {
lendone = 1;
} else if (off != 0) {
ins2(c, A_MOVQ, amem(D_BP, off + 8),
areg(D_AX));
lendone = 1;
}
} else if (u && (u->kind == TY_SLICE || u->kind == TY_STR)
/* off==0 non-let ident (e.g. a DATA-backed
* def): the old arm emitted MOVQ 8(BP) —
* garbage. Falls to the resolver route. */
} else if (hdrish
&& a->kind == N_DOT && a->lhs
&& a->lhs->kind == N_IDENT && a->str) {
/* #235: len() of a tuple-element slice/str
* (`len(t.N)`). The tuple-element read leaves only
* AX=.ptr — it has no slice-header sibling (that
* gap is #238) — so the bare cgexpr fallback below
* returned .ptr AS the length. Load the element's
* .len word directly at BP + element_off + 8,
* mirroring the N_IDENT slice arm above and the
* tuple-field-offset walk (cgen.c N_DOT TY_TUPLE). */
* (`len(t.N)`). Kept as an enumerated arm: tuples
* are not resolver-addressable (cgplaceaddr has no
* TY_TUPLE hop — that gap is #238). Load the
* element's .len word directly at
* BP + element_off + 8, mirroring the N_IDENT
* slice arm above and the tuple-field-offset walk
* (cgen.c N_DOT TY_TUPLE). */
Type *bt = a->lhs->type;
Type *bu = (bt && bt->kind == TY_NAMED)
? bt->under : bt;
@@ -6651,27 +6663,46 @@ cgexpr(Cg *c, Node *n, Local *locals)
ins2(c, A_MOVQ,
amem(D_BP, off + foff + 8),
areg(D_AX));
} else {
cgexpr(c, a, locals);
lendone = 1;
}
} else if (u && (u->kind == TY_SLICE || u->kind == TY_STR)
&& a->kind == N_INDEX) {
/* struct-field N_DOT (`len(s.field)`): the old
* inner fallback returned .ptr as the length.
* Falls to the resolver route. */
} else if (hdrish && a->kind == N_INDEX) {
/* #19: len() of an INDEXED str/slice element
* (`len(xs[i])`). The N_INDEX str/slice load leaves
* AX=.ptr, BX=.len, CX=.cap (cgslicehdr) — the bare
* cgexpr fallback below then returned AX (the ptr) AS
* the length. Shuffle BX (the len word) into AX, the
* cgexpr fallback returned AX (the ptr) AS the
* length. Shuffle BX (the len word) into AX, the
* same MOVQ BX,AX shape as the #14 .len pseudo-field
* fix. Same family as #18 (shared cstage==wwstage gap,
* not a rule-10 divergence). */
cgexpr(c, a, locals);
ins2(c, A_MOVQ, areg(D_BX), areg(D_AX));
lendone = 1;
} else if (u && u->kind == TY_ARRAY) {
ins2(c, A_MOVQ, aimm((long long)u->alen), areg(D_AX));
} else {
/* fall back: load via .len pseudo-field */
cgexpr(c, a, locals);
lendone = 1;
}
/* #10 (F2) + #41 (FA2/FB1): ONE uniform header-place
* route for every other slice/str place — the arm
* enumeration above leaked four siblings
* (#235 → #19 → F2 → FA2/FB1) because each new operand
* shape fell to a bare cgexpr fallback that returned
* the slice DATA POINTER as the length. Resolve the
* operand's header address (cgplaceaddr — deref /
* index / dot spines) and read the .len word at +8;
* non-place operands (call result, slicing expr,
* string literal — all previously the same silent
* ptr-garbage) die LOUD per rule 7. */
if (!lendone && hdrish
&& cgplaceaddr(c, a, D_BX, locals)) {
ins2(c, A_MOVQ, amem(D_BX, 8), areg(D_AX));
lendone = 1;
}
if (!lendone)
fatal("#10/#41: len() operand shape not "
"place-resolvable (rule-7)");
break;
}
if (n->lhs && n->lhs->kind == N_IDENT && n->lhs->str &&