w6c+wwstage: len() of tuple-element slice reads .len not .ptr (#235)

len() special-cased only a plain N_IDENT slice operand (load .len at
BP+off+8) and an array operand (fold $alen); every other shape fell back
to a bare cgexpr(operand), which for a slice leaves AX=.ptr. A tuple-
element read (t.N) loads only AX=.ptr, so len(t.N) on a slice/str tuple
element returned the slice's .ptr word AS its length — a silent
miscompile, gate-blind because the bootstrap never does len() on a
slice-typed tuple element (sibling of the #234/#237 tuple-sret cluster).

Both stages: detect a slice/str tuple-element len() operand and load the
element's .len word directly at BP + element_off + 8, mirroring the
N_IDENT slice arm and the tuple-field-offset walk (element_off sums
preceding element sizes through the type table). Byte-identical asm
(rule 10). The separate tuple-element-read full-header gap is #238; a
leading-scalar mixed-tuple has its own pre-existing sret-layout cs/ww
divergence, filed apart from #235.

Test 903_tuple_elem_slice_len_run: 4 slice/str-only tuple rows (two/
three slices, str+slice, slice+str; distinct lengths), build+run both
drivers + cs==ww byte-id. 12/12 ok.
This commit is contained in:
2026-06-01 18:17:07 +09:00
parent 20fe5419d2
commit 6acddc3a82
6 changed files with 428 additions and 0 deletions

View File

@@ -290,6 +290,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_deref_slice_store_run \
$(BIN)/test_tuple_nary_destructure_run \
$(BIN)/test_overcap_tuple_field_store_run \
$(BIN)/test_tuple_elem_slice_len_run \
$(BIN)/test_str_forrange_loopvar_run \
$(BIN)/test_composite_call_arg \
$(BIN)/test_composite_call_arg_run \
@@ -1169,6 +1170,13 @@ $(BIN)/test_overcap_tuple_field_store_run: test/wcc/940_overcap_tuple_field_stor
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
# #235: len() of a tuple-element slice/str reads .len (+8) not .ptr.
$(BIN)/test_tuple_elem_slice_len_run: test/wcc/903_tuple_elem_slice_len_run.c \
$(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_composite_call_arg: test/wcc/723_composite_call_arg.c \
$(BIN)/w6c $(BIN)/w6c_ww | $(BIN)
$(CC) $(CFLAGS) -o $@ $<

View File

@@ -5328,6 +5328,39 @@ cgexpr(Cg *c, Node *n, Local *locals)
ins2(c, A_MOVQ, amem(D_BP, off + 8),
areg(D_AX));
}
} else if (u && (u->kind == TY_SLICE || u->kind == TY_STR)
&& 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). */
Type *bt = a->lhs->type;
Type *bu = (bt && bt->kind == TY_NAMED)
? bt->under : bt;
if (bu && bu->kind == TY_TUPLE) {
int idx = 0;
for (const char *q = a->str; *q; q++)
idx = idx * 10 + (*q - '0');
Tparam *tp = bu->params;
int foff = 0;
while (idx > 0 && tp) {
if (tp->type)
foff += (int)tp->type->size;
tp = tp->next;
idx--;
}
int off = localfind(locals, a->lhs->str);
ins2(c, A_MOVQ,
amem(D_BP, off + foff + 8),
areg(D_AX));
} else {
cgexpr(c, a, locals);
}
} else if (u && u->kind == TY_ARRAY) {
ins2(c, A_MOVQ, aimm((long long)u->alen), areg(D_AX));
} else {

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,252 @@
/*
* 903_tuple_elem_slice_len_run — project #235: `len(t.N)` where t is a tuple
* and t.N is a slice/str-typed element.
*
* The len() builtin special-cased only a PLAIN N_IDENT slice operand (load
* .len at BP+off+8) and an array operand (fold $alen); every other shape fell
* back to a bare `cgexpr(operand)`, which for a slice leaves AX=.ptr. The
* tuple-element read (`t.N`) loads only AX=.ptr (it has no slice-header
* sibling — that gap is the separate #238), so `len(t.N)` returned the slice's
* .ptr word AS the length — a SILENT miscompile, gate-blind because the
* bootstrap never does len() on a slice-typed tuple element.
*
* Fix (both stages, byte-identical per rule 10): for a slice/str tuple-element
* len() operand, load the element's .len word directly at
* BP + tuple_off + element_off + 8 — mirroring the N_IDENT slice arm and the
* tuple-field-offset walk. The +8 is the {ptr,len,cap}-header .len offset, the
* same constant the N_IDENT arm and the .len pseudo-field use; element_off sums
* preceding element sizes through the type table.
*
* Rows (every element a DISTINCT length so a dropped/wrong field is caught;
* each ww program self-asserts and returns 0 only when every len(t.N) is
* correct). Tuples are slice/str-ONLY — a leading scalar element exercises a
* SEPARATE pre-existing mixed-tuple sret-layout cs/ww divergence (filed apart
* from #235), out of scope here.
* two_slice ([]u8,[]u8) lens 3,5
* three_slice ([]u8,[]u8,[]u8) lens 3,5,11
* str_slice (str,[]u8) len("abcd")=4, slice 6
* slice_str ([]u8,str) slice 7, len("hi")=2
*
* All K_RUN: build+run exit 0 on BOTH drivers AND cs==ww byte-identical.
* NNN<950, self-contained (/tmp, no imports), so rule-14's selfhost-sibling
* race does not apply (940/945/799 precedent).
*/
#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;
}
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), cb = fgetc(fb);
if (ca != cb) { rc = -1; break; }
if (ca == EOF) break;
}
fclose(fa); fclose(fb);
return rc;
}
struct row { const char *label; const char *src; int want; };
static const struct row rows[] = {
{ "two_slice",
"package main;\n"
"fn mk() ([]u8, []u8) = {\n"
" let a: []u8; a.len = 3; a.cap = 7;\n"
" let b: []u8; b.len = 5; b.cap = 9;\n"
" return (a, b);\n"
"};\n"
"export fn main() i32 = {\n"
" let t: ([]u8, []u8) = mk();\n"
" if (len(t.0) != 3) { return 1; };\n"
" if (len(t.1) != 5) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "three_slice",
"package main;\n"
"fn mk() ([]u8, []u8, []u8) = {\n"
" let a: []u8; a.len = 3; a.cap = 7;\n"
" let b: []u8; b.len = 5; b.cap = 9;\n"
" let c: []u8; c.len = 11; c.cap = 13;\n"
" return (a, b, c);\n"
"};\n"
"export fn main() i32 = {\n"
" let t: ([]u8, []u8, []u8) = mk();\n"
" if (len(t.0) != 3) { return 1; };\n"
" if (len(t.1) != 5) { return 2; };\n"
" if (len(t.2) != 11) { return 3; };\n"
" return 0;\n"
"};\n", 0 },
{ "str_slice",
"package main;\n"
"fn mk() (str, []u8) = {\n"
" let s: str = \"abcd\";\n"
" let b: []u8; b.len = 6; b.cap = 8;\n"
" return (s, b);\n"
"};\n"
"export fn main() i32 = {\n"
" let t: (str, []u8) = mk();\n"
" if (len(t.0) != 4) { return 1; };\n"
" if (len(t.1) != 6) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "slice_str",
"package main;\n"
"fn mk() ([]u8, str) = {\n"
" let b: []u8; b.len = 7; b.cap = 9;\n"
" let s: str = \"hi\";\n"
" return (b, s);\n"
"};\n"
"export fn main() i32 = {\n"
" let t: ([]u8, str) = mk();\n"
" if (len(t.0) != 7) { return 1; };\n"
" if (len(t.1) != 2) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
};
/* build+run via a driver (ww / ww_ww); returns 0 pass, nonzero fail. */
static int
run_driver(const char *driver, const struct row *r, int i)
{
char src[96], tmpdir[96], errf[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/telen_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/telen_%d_d_%d", getpid(), i);
snprintf(errf, sizeof errf, "/tmp/telen_%d_e_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s >/dev/null 2>%s",
tmpdir, driver, src, errf);
int brc = runwait(cmd);
if (brc != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
unlink(src); unlink(errf); rmdir(tmpdir);
return -1;
}
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[256];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = runwait(outbin);
unlink(src); unlink(outbin); unlink(errf); rmdir(tmpdir);
if (got != r->want) {
fprintf(stderr, "row[%s]: %s exit %d, want %d\n",
r->label, driver, got, r->want);
return 1;
}
return 0;
}
/* cs==ww .s byte-id (rule 10). */
static int
byteid(const char *w6c, const char *w6c_ww, const struct row *r, int i)
{
char src[96], cs_s[96], ws_s[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/telen_bi_%d_%d.ww", getpid(), i);
snprintf(cs_s, sizeof cs_s, "/tmp/telen_bi_%d_%d_cs.s", getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/telen_bi_%d_%d_ww.s", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
int rc = 0;
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", r->label); rc = 1; }
else {
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", r->label); rc = 1; }
else if (slurp_eq(cs_s, ws_s) != 0) {
fprintf(stderr, "row[%s]: cstage/wwstage .s DIFFER "
"(rule-10 byte-id)\n", r->label);
rc = 1;
}
}
unlink(src); unlink(cs_s); unlink(ws_s);
return rc;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[512];
if (bin[0] != '/') {
char cwd[256];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char cdrv[640], wdrv[640], w6c[640], w6c_ww[640];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
struct { const char *name; const char *path; int gated; }
drivers[] = {
{ "cstage", cdrv, 0 },
{ "wwstage", wdrv, 1 },
{ NULL, NULL, 0 },
};
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int d = 0; drivers[d].name; d++) {
if (drivers[d].gated && access(drivers[d].path, X_OK) != 0) {
fprintf(stderr, "tuple_elem_slice_len: skip %s (no %s)\n",
drivers[d].name, drivers[d].path);
continue;
}
for (int i = 0; i < n; i++) {
total++;
if (run_driver(drivers[d].path, &rows[i], i) != 0) fail++;
}
}
if (access(w6c_ww, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
if (byteid(w6c, w6c_ww, &rows[i], i) != 0) fail++;
}
}
if (fail) {
fprintf(stderr, "tuple_elem_slice_len: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("tuple_elem_slice_len: %d/%d ok\n", total, total);
return 0;
}