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

@@ -5101,18 +5101,24 @@ fn cgcall(c: *cgen, n: *node) void = {
};
};
};
// `len(x)` Hare builtin — mirror cmd/w6c/cgen.c:4283-4297.
// Required for byte-id when compiler-imported lib code uses
// len(fixedarray) (e.g. lib/strconv/decimal.ha's `len(d.digits)`
// over the [800]u8 field). Without this intercept wwstage falls
// through to a regular CALL len(SB) while cstage folds to
// `MOVQ $alen, AX` — rule-10 byte-id break (#131).
// `len(x)` Hare builtin — mirror cmd/w6c/cgen.c N_CALL "len"
// arm. Required for byte-id when compiler-imported lib code
// uses len(fixedarray) (e.g. lib/strconv/decimal.ha's
// `len(d.digits)` over the [800]u8 field). Without this
// intercept wwstage falls through to a regular CALL len(SB)
// while cstage folds to `MOVQ $alen, AX` — rule-10 byte-id
// break (#131).
//
// Argument-type-driven branches:
// TY_SLICE / TY_STR (+ N_IDENT operand) → load .len at BP+off+8.
// TY_ARRAY → fold `MOVQ $alen, AX`.
// else → evaluate operand (cstage's pseudo-.len fallback —
// unlikely to fire on Hare-shaped sources).
// Dispatch (#10/#41): enumerated fast-paths keep their
// pre-fix asm (ident local/global, #235 tuple element, #19
// indexed element, TY_ARRAY const fold), then ONE uniform
// header-place route via cgplaceaddr (.len at place+8) for
// every other slice/str place — the arm enumeration leaked
// four siblings (#235 → #19 → F2 → FA2/FB1), each new operand
// shape falling to a cgexpr fallback that returned the slice
// DATA POINTER as the length. Non-place operands (call
// result, slicing expr, string literal — previously the same
// silent ptr-garbage) die LOUD per rule 7.
if (streq(callee.str, "len")) {
if (n.list != nil) {
let a: *node = n.list;
@@ -5121,94 +5127,102 @@ fn cgcall(c: *cgen, n: *node) void = {
for (u != nil && u.kind == tykind.TY_NAMED) {
u = u.under;
};
let hdrish: bool = false;
if (u != nil) {
if ((u.kind == tykind.TY_SLICE
|| u.kind == tykind.TY_STR)
&& a.kind == nkind.N_IDENT) {
let lc: *local = localfindnode(c, a.str);
if (lc != nil) {
emitline("\tMOVQ\t");
emitoff((lc.off + 8): i64);
emitline("(BP), AX\n");
return;
};
// #231: str/slice GLOBAL — the
// local-only path above lacked it,
// so cgexpr fell through and left
// AX=.ptr (not .len). The .len word
// lives at the global's address+8;
// route the LEAQ through the post-#1
// value mangle (c.curmod) so a
// private same-leaf global isn't
// mis-resolved.
if (isletvar(c, a.str)) {
emitline("\tLEAQ\t");
emitsymnamehint(c, a.str, c.curmod);
emitline("(SB), CX\n");
emitline("\tMOVQ\t8(CX), AX\n");
return;
};
if (u.kind == tykind.TY_SLICE
|| u.kind == tykind.TY_STR) {
hdrish = true;
};
// #235: len() of a tuple-element slice/str
// (`len(t.N)`). The tuple-element read leaves
// only AX=.ptr — no slice-header sibling (that
// gap is #238) — so the 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 (cgenexpr.ww N_TTUPLE).
if ((u.kind == tykind.TY_SLICE
|| u.kind == tykind.TY_STR)
&& a.kind == nkind.N_DOT
&& a.lhs != nil
&& a.lhs.kind == nkind.N_IDENT) {
let lc: *local = localfindnode(c, a.lhs.str);
if (lc != nil) {
let tn: *node = lc.tnode;
for (tn != nil && tn.kind == nkind.N_TNAME) {
tn = aliaslookup(c, tn.str);
};
if (tn != nil) {
if (tn.kind == nkind.N_TTUPLE) {
let idx: i32 = fldnumidx(a.str);
if (idx >= 0) {
let tp: *node = tn.list;
let foff: i32 = 0;
let i: i32 = 0;
for (i < idx) {
if (tp == nil) { i = idx; }
else {
foff += slotsize(c, tp.lhs);
tp = tp.next;
i += 1;
};
};
if (tp != nil) {
emitline("\tMOVQ\t");
emitoff((lc.off + foff + 8): i64);
emitline("(BP), AX\n");
return;
};
if (hdrish && a.kind == nkind.N_IDENT) {
let lc: *local = localfindnode(c, a.str);
if (lc != nil) {
emitline("\tMOVQ\t");
emitoff((lc.off + 8): i64);
emitline("(BP), AX\n");
return;
};
// #231: str/slice GLOBAL — the
// local-only path above lacked it,
// so cgexpr fell through and left
// AX=.ptr (not .len). The .len word
// lives at the global's address+8;
// route the LEAQ through the post-#1
// value mangle (c.curmod) so a
// private same-leaf global isn't
// mis-resolved.
if (isletvar(c, a.str)) {
emitline("\tLEAQ\t");
emitsymnamehint(c, a.str, c.curmod);
emitline("(SB), CX\n");
emitline("\tMOVQ\t8(CX), AX\n");
return;
};
// non-local non-let ident (DATA-backed
// def): the old arm fell to the silent
// cgexpr fallback. Falls to the
// resolver route below.
};
// #235: len() of a tuple-element slice/str
// (`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 (cgenexpr.ww N_TTUPLE).
if (hdrish && a.kind == nkind.N_DOT
&& a.lhs != nil
&& a.lhs.kind == nkind.N_IDENT) {
let lc: *local = localfindnode(c, a.lhs.str);
if (lc != nil) {
let tn: *node = lc.tnode;
for (tn != nil && tn.kind == nkind.N_TNAME) {
tn = aliaslookup(c, tn.str);
};
if (tn != nil) {
if (tn.kind == nkind.N_TTUPLE) {
let idx: i32 = fldnumidx(a.str);
if (idx >= 0) {
let tp: *node = tn.list;
let foff: i32 = 0;
let i: i32 = 0;
for (i < idx) {
if (tp == nil) { i = idx; }
else {
foff += slotsize(c, tp.lhs);
tp = tp.next;
i += 1;
};
};
if (tp != nil) {
emitline("\tMOVQ\t");
emitoff((lc.off + foff + 8): i64);
emitline("(BP), AX\n");
return;
};
};
};
};
};
// #19: len() of an INDEXED str/slice element
// (`len(xs[i])`). The N_INDEX str/slice load
// leaves AX=.ptr, BX=.len, CX=.cap — the bare
// cgexpr fallback below 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 rule-10).
if ((u.kind == tykind.TY_SLICE
|| u.kind == tykind.TY_STR)
&& a.kind == nkind.N_INDEX) {
cgexpr(c, a);
emitline("\tMOVQ\tBX, AX\n");
return;
};
// struct-field N_DOT (`len(s.field)`): the
// old fallback returned .ptr as the length.
// Falls to the resolver route below.
};
// #19: len() of an INDEXED str/slice element
// (`len(xs[i])`). The N_INDEX str/slice load
// leaves AX=.ptr, BX=.len, CX=.cap — the bare
// 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 rule-10).
if (hdrish && a.kind == nkind.N_INDEX) {
cgexpr(c, a);
emitline("\tMOVQ\tBX, AX\n");
return;
};
if (u != nil) {
if (u.kind == tykind.TY_ARRAY) {
emitline("\tMOVQ\t$");
emitint(u.alen: i64);
@@ -5216,11 +5230,20 @@ fn cgcall(c: *cgen, n: *node) void = {
return;
};
};
// Fallback: evaluate the argument and let AX carry
// whatever the value-load shape yields. Mirrors
// cstage's `cgexpr(c, a, locals)` fallthrough.
cgexpr(c, a);
return;
// #10 (F2) + #41 (FA2/FB1): ONE uniform
// header-place route for every other slice/str
// place — resolve the operand's header address
// (cgplaceaddr: deref / index / dot spines) and
// read the .len word at +8.
if (hdrish) {
if (cgplaceaddr(c, a, "BX")) {
emitline("\tMOVQ\t8(BX), AX\n");
return;
};
};
let mlen: str = "#10/#41: len() operand shape not place-resolvable (rule-7)\n";
os.write(2, mlen.ptr, mlen.len: u64);
os.exit(1);
};
};
// free(x) — documented NO-OP, mirror of cstage's cgexpr