w6c+cgen: len() over indexed str/slice element extracts .len (fix #19)

`len(xs[i])` over a [N]str/[]str (and []T slice) element returned the
element's .ptr, not its length, on BOTH stages (shared gap, not rule-10):
the len() builtin had no N_INDEX arm, so it fell to the bare-cgexpr
fallback, where the N_INDEX str/slice load (cgslicehdr) leaves AX=.ptr,
BX=.len, CX=.cap — and len() returned AX (the ptr) as the length.

Add an N_INDEX arm gated on a (TY_SLICE||TY_STR) element in both stages:
cgexpr the element, then MOVQ BX,AX to shuffle the len word into the
result reg — the same shape as the #14 .len pseudo-field fix. Byte-id
neutral (no bootstrap source uses len(indexed-element)); regenerated
w6c + wwdump combined.ww. New 802_lenidx_run pins runtime + cs==ww.
This commit is contained in:
2026-06-03 11:56:16 +09:00
parent 58c8f4be02
commit 6b67655eca
6 changed files with 280 additions and 0 deletions

View File

@@ -370,6 +370,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_xmod_valglobal_dot_run \
$(BIN)/test_len_strglobal_run \
$(BIN)/test_litstr_pseudo_run \
$(BIN)/test_lenidx_run \
$(BIN)/test_tuple_sret_callee \
$(BIN)/test_tuple_sret_receive_run \
$(BIN)/test_struct_tuple_field_slot \
@@ -886,6 +887,14 @@ $(BIN)/test_litstr_pseudo_run: test/wcc/801_litstr_pseudo_run.c \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
# #19: len() over an INDEXED str/slice element (`len(xs[i])`) returned the
# element's .ptr, not its length (cstage == wwstage, shared gap). Runtime
# (cstage build+run) + cs==ww byte-id, both dimensions per row.
$(BIN)/test_lenidx_run: test/wcc/802_lenidx_run.c \
$(BIN)/ww $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
# #10 Fold A (wide tuple-return / sret, CALLEE side): an over-cap tuple
# return (> 4 GP or > 2 SSE eightbytes) now compiles via sret instead of
# loud-stopping at the SEND. Compile + cs==ww byte-id only — the receive

View File

@@ -5998,6 +5998,18 @@ cgexpr(Cg *c, Node *n, Local *locals)
} else {
cgexpr(c, a, locals);
}
} else if (u && (u->kind == TY_SLICE || u->kind == TY_STR)
&& 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
* 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));
} else if (u && u->kind == TY_ARRAY) {
ins2(c, A_MOVQ, aimm((long long)u->alen), areg(D_AX));
} else {

View File

@@ -23887,6 +23887,21 @@ fn cgcall(c: *cgen, n: *node) void = {
};
};
};
// #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;
};
if (u.kind == tykind.TY_ARRAY) {
emitline("\tMOVQ\t$");
emitint(u.alen: i64);

View File

@@ -4180,6 +4180,21 @@ fn cgcall(c: *cgen, n: *node) void = {
};
};
};
// #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;
};
if (u.kind == tykind.TY_ARRAY) {
emitline("\tMOVQ\t$");
emitint(u.alen: i64);

View File

@@ -23887,6 +23887,21 @@ fn cgcall(c: *cgen, n: *node) void = {
};
};
};
// #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;
};
if (u.kind == tykind.TY_ARRAY) {
emitline("\tMOVQ\t$");
emitint(u.alen: i64);

214
test/wcc/802_lenidx_run.c Normal file
View File

@@ -0,0 +1,214 @@
/*
* 802_lenidx_run — BUG #19. Runtime + cs==ww byte-id net for the
* `len()` builtin applied to an INDEXED str/slice element (`len(xs[i])`).
*
* THE BUG (cstage == wwstage, BOTH wrong — shared gap, NOT rule-10):
* `len(t[i])` over a `[N]str` / `[]str` returned the element's .ptr,
* not its length. The len() builtin (cgen.c / cgenexpr.ww) had arms for
* an N_IDENT slice/str operand (.len at BP+off+8), a tuple-element N_DOT
* (#235), and a whole TY_ARRAY (→ alen) — but NOT an N_INDEX element.
* So `len(xs[i])` fell to the bare `cgexpr(arg)` fallback, which loads
* the str/slice element header via cgslicehdr leaving AX=.ptr, BX=.len,
* CX=.cap — and len() then returned AX (the ptr) AS the length. Same
* family as #14 (literal `.len` returned ptr) and #18 (shared static-init
* gap). byte-id was BLIND: no bootstrap source uses len(indexed-element)
* — the corpus indexes-then-reads .len explicitly or hardcodes lengths.
* CONTRAST (controls, all already correct): `t[i].len` pseudo-field,
* `t[i].ptr`, and scalar `len(s)` over a bare str variable.
*
* THE FIX (#19): add an N_INDEX arm to the len() builtin in BOTH stages —
* `cgexpr(arg)` to materialise the element, then `MOVQ BX, AX` to shuffle
* the len word (already in BX after cgslicehdr) into the result reg. The
* same MOVQ BX,AX shape as #14's `.len` pseudo-field fix. Covers str AND
* slice elements (both are 24B headers loaded by cgslicehdr).
*
* EACH ROW CARRIES BOTH DIMENSIONS (801 model):
* (a) cstage `ww build` + run, asserting the exit — pins that the
* converged asm is runtime-correct (real lengths, not ptr bytes).
* (b) w6c vs w6c_ww `.s` cmp — FAILS if the stages diverge (rule-10).
*
* GATE POLARITY: must stay GREEN. A wrong exit means len(indexed-element)
* regressed back to returning the ptr; a byte-id FAIL means the stages
* diverged.
*
* SCOPE NOTE: the []T (slice-of-slice) ELEMENT len shares the identical
* codegen path (cgslicehdr → BX=len → MOVQ BX,AX) and is byte-id-clean in
* isolation, but a clean runtime row for it is blocked by a SEPARATE
* pre-existing bug — the `[2][]u8` array-literal → slice coercion emits a
* divergent frame offset (cstage -24(BP) vs wwstage -40(BP)). That gap is
* unrelated to len() (it reproduces with the `.len` pseudo-field too) and
* is filed separately; these rows stay on str elements to avoid it.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
static int
runwait(const char *cmd)
{
int rc = system(cmd);
if (rc == -1) return -1;
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
return -1;
}
struct row { const char *label; const char *src; int want_exit; };
static const struct row rows[] = {
/* the exact #19 repro: a [3]str table, len(t[1]) == 3. Pre-fix this
* returned a ptr low-byte (58), not 3. */
{ "arrtab_mid",
"package main;\n"
"let t: [3]str = [\"a\", \"bcd\", \"\"];\n"
"export fn main() i32 = { return len(t[1]): i32; };\n", 3 },
/* empty-string element edge: len(t[2]) == 0 (a ptr would be nonzero). */
{ "arrtab_empty",
"package main;\n"
"let t: [3]str = [\"a\", \"bcd\", \"\"];\n"
"export fn main() i32 = { return len(t[2]): i32; };\n", 0 },
/* first element: len(t[0]) == 1 — guards an off-by-one in the index
* scale (a ptr or the wrong element would not be 1). */
{ "arrtab_first",
"package main;\n"
"let t: [3]str = [\"a\", \"bcd\", \"\"];\n"
"export fn main() i32 = { return len(t[0]): i32; };\n", 1 },
/* []str slice parameter — the callee-side shape: len(xs[i]) over a
* slice arg, summing all three lengths (1 + 3 + 0 == 4). */
{ "slice_param_sum",
"package main;\n"
"fn slen(xs: []str, i: i64) i32 = { return len(xs[i]): i32; };\n"
"export fn main() i32 = {\n"
" let arr: [3]str = [\"a\", \"bcd\", \"\"];\n"
" let xs: []str = arr;\n"
" return slen(xs, 0) + slen(xs, 1) + slen(xs, 2);\n"
"};\n", 4 },
/* NEGATIVE CONTROL: the `.len` pseudo-field on the same indexed
* element must agree with len() (both == 3), AND `.ptr` must still
* deref to the first byte ('b' == 98) — proving the fix left the
* already-correct .len/.ptr field paths untouched. Returns 3 only if
* len()==.len AND *.ptr=='b'. */
{ "neg_control_field_agree",
"package main;\n"
"let t: [3]str = [\"a\", \"bcd\", \"\"];\n"
"export fn main() i32 = {\n"
" if (len(t[1]) != t[1].len) { return 91; };\n"
" let p: *u8 = t[1].ptr;\n"
" if (*p != 'b') { return 92; };\n"
" return len(t[1]): i32;\n"
"};\n", 3 },
{ NULL, NULL, 0 }
};
static int
slurp_eq(const char *a, const char *b)
{
FILE *fa = fopen(a, "rb");
FILE *fb = fopen(b, "rb");
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
int rc = 0;
for (;;) {
int ca = fgetc(fa);
int cb = fgetc(fb);
if (ca != cb) { rc = -1; break; }
if (ca == EOF) break;
}
fclose(fa); fclose(fb);
return rc;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[1024];
if (bin[0] != '/') {
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char w6c[1100], w6c_ww[1100];
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
if (access(w6c_ww, X_OK) != 0) {
fprintf(stderr, "lenidx: w6c_ww missing — cannot run the "
"cs==ww byte-id gate\n");
return 1;
}
int n = 0, fail = 0;
for (int i = 0; rows[i].src; i++, n++) {
char src[64];
snprintf(src, sizeof src, "/tmp/wwli_%d_%d.ww", getpid(), i);
FILE *f = fopen(src, "wb");
if (f == NULL) { fail++; continue; }
fputs(rows[i].src, f);
fclose(f);
/* (a) cstage build + run in a scratch dir. */
char tmpdir[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwli_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
char cmd[2048];
snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s",
tmpdir, bin, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: cstage build failed\n",
rows[i].label);
fail++;
unlink(src); rmdir(tmpdir);
continue;
}
char outbin[128];
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = runwait(outbin);
if (got != rows[i].want_exit) {
fprintf(stderr, "row[%s]: cstage exit %d, want %d\n",
rows[i].label, got, rows[i].want_exit);
fail++;
}
unlink(outbin); rmdir(tmpdir);
/* (b) cs==ww byte-id gate. */
char cs_s[64], ws_s[64];
snprintf(cs_s, sizeof cs_s, "/tmp/wwli_%d_%d_cs.s", getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/wwli_%d_%d_ww.s", getpid(), i);
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, cs_s, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label);
fail++; unlink(src); continue;
}
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
w6c_ww, ws_s, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c_ww failed\n", rows[i].label);
fail++; unlink(src); unlink(cs_s); continue;
}
if (slurp_eq(cs_s, ws_s) != 0) {
fprintf(stderr, "row[%s]: cstage/wwstage .s DIFFER "
"(rule-10 byte-id violation)\n", rows[i].label);
fail++;
}
unlink(src); unlink(cs_s); unlink(ws_s);
}
if (fail) {
fprintf(stderr, "%d/%d lenidx tests failed\n", fail, n);
return 1;
}
printf("lenidx: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n);
return 0;
}