selfhost+cstage+test: graduate *[]T indexing to slice-element type (#20)

Cstage and wwstage share the latent: check.c's N_INDEX bespoke
TY_PTR-over-TY_SLICE clause peeled the slice in `*[]T[i]` and
returned the element of the element, while wwstage's elemsizeof
had no N_TSLICE arm for the post-N_TPTR-peel elem and fell to
the 8B catch-all. Splitting leaves one stage broken on the
exact `*[]T[i]` shape the new 754 sentinel asserts byte-identical
between stages (rule 11). The companion 24B per-element copy
emit is a separate codegen wedge already pinned inline at
cmd/w6c/cgen.c:6518; out-of-scope here and noted in the fixture
header.
This commit is contained in:
2026-05-19 12:30:36 +09:00
parent 02ada29624
commit f0b8c25b29
6 changed files with 261 additions and 1 deletions

View File

@@ -288,6 +288,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_vararg_seq_percall \
$(BIN)/test_modparam_callee \
$(BIN)/test_convwrap_audit \
$(BIN)/test_slice_of_slice_index \
$(BIN)/test_param_shadow_mod \
$(BIN)/test_localoff_scope \
$(BIN)/test_cast_enum_movl \
@@ -741,6 +742,12 @@ $(BIN)/test_convwrap_audit: test/wcc/753_convwrap_audit.c \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_slice_of_slice_index: test/wcc/754_slice_of_slice_index.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_match_4arm_cross_module_run: test/wcc/929_match_4arm_cross_module_run.c \
$(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \

View File

@@ -874,8 +874,14 @@ cexpr(Checker *c, Node *n)
return n->type = u->sub;
if (u && u->kind == TY_STR)
return n->type = ty_u8;
/* `*[N]T` auto-decays to `[N]T` indexing — drill into the
* inner T so callers see the element type, matching C's
* pointer-to-array semantics. `*[]T` does NOT auto-decay:
* `p[i]` for `p: *[]T` yields `[]T` via the default `*U → U`
* fall-through below (here U is `[]T`). Hare-faithful: a
* pointer-to-slice is a 1D array of slices, not of T. */
if (u && u->kind == TY_PTR && u->sub &&
(u->sub->kind == TY_ARRAY || u->sub->kind == TY_SLICE))
u->sub->kind == TY_ARRAY)
return n->type = u->sub->sub;
/* C-style pointer indexing: p[i] → *(p+i) */
if (u && u->kind == TY_PTR && u->sub)

View File

@@ -8787,6 +8787,10 @@ fn elemsizeof(t: *node) i32 = {
if (elem.kind == nkind.N_TARRAY) {
if (elem.lhs != nil) { elem = elem.lhs; };
};
// `*[]T`: stride is the slice header (24B). Hare-faithful — a
// pointer-to-slice is a 1D array of slices, not of T. Mirrors the
// cstage check.c default `*U → U` path for U=[]T (slice element).
if (elem.kind == nkind.N_TSLICE) { return 24; };
if (elem.kind == nkind.N_TNAME) {
let nm: str = elem.str;
// str element is 16B (ptr+len). primsize returns 0 for it.

View File

@@ -1288,6 +1288,10 @@ fn elemsizeof(t: *node) i32 = {
if (elem.kind == nkind.N_TARRAY) {
if (elem.lhs != nil) { elem = elem.lhs; };
};
// `*[]T`: stride is the slice header (24B). Hare-faithful — a
// pointer-to-slice is a 1D array of slices, not of T. Mirrors the
// cstage check.c default `*U → U` path for U=[]T (slice element).
if (elem.kind == nkind.N_TSLICE) { return 24; };
if (elem.kind == nkind.N_TNAME) {
let nm: str = elem.str;
// str element is 16B (ptr+len). primsize returns 0 for it.

View File

@@ -8787,6 +8787,10 @@ fn elemsizeof(t: *node) i32 = {
if (elem.kind == nkind.N_TARRAY) {
if (elem.lhs != nil) { elem = elem.lhs; };
};
// `*[]T`: stride is the slice header (24B). Hare-faithful — a
// pointer-to-slice is a 1D array of slices, not of T. Mirrors the
// cstage check.c default `*U → U` path for U=[]T (slice element).
if (elem.kind == nkind.N_TSLICE) { return 24; };
if (elem.kind == nkind.N_TNAME) {
let nm: str = elem.str;
// str element is 16B (ptr+len). primsize returns 0 for it.

View File

@@ -0,0 +1,235 @@
/*
* 754_slice_of_slice_index — sentinel for latent #20. Pre-fix, cstage's
* check.c typed `p[i]` for `p: *[]T` as `T` (element-of-element) via a
* bespoke TY_PTR-over-TY_SLICE clause that auto-decayed the pointer
* twice: first peeling the `*`, then peeling the `[]`. The Hare-faithful
* shape is `[]T` — a pointer-to-slice is a 1D array of slice headers,
* not a 2D array of T. The `*[N]T → T` auto-decay (TY_PTR over TY_ARRAY)
* IS the C-style array-to-element semantics callers expect; only the
* TY_SLICE arm of the same clause was wrong.
*
* Selfhost mirror lived in cgenutil.ww elemsizeof: for `*[]T`, the
* elem walk falls into `elem.kind == nkind.N_TSLICE` which had no
* recognition — fell through to the catch-all `return 8`. Post-fix
* the N_TSLICE arm returns 24 (slice header stride). Both stages now
* agree on stride; cstage already computed 24 via Type.size on the
* idx_eff(TY_PTR) sub.
*
* Surface impact (the wedge that flushed this latent): a `[][]u8`
* indexed through a `*[][]u8` (e.g. `tokens.ptr[0] = ...` for tokens:
* [][]u8) was rejected by cstage's `init u8 not assignable to declared
* []u8` and silently 8B-stored by wwstage. Symmetric fix:
* - cstage: drop the TY_SLICE branch in check.c N_INDEX so `*[]T[i]`
* falls through to the default `*U → U` path with U=[]T.
* - wwstage: extend cgenutil.ww elemsizeof to return 24 when the
* elem (post-N_TPTR peel) is N_TSLICE.
*
* Probes:
* row 0 — `let r: []u8 = p[0]` for `p: *[]u8`. Pre-fix cstage errors
* ("init u8 not assignable to declared []u8"); wwstage
* silently emits an 8B load. Post-fix both stages compile
* and emit the same MOVQ load with stride 24 (slice element
* span — read width is still 8B today; the full 24B slice
* element copy is a separately-tracked downstream gap noted
* at cmd/w6c/cgen.c:6518 ("Slice (24B) and struct/tuple/
* tagged element arrays land in the same multi-word-store
* gap")). Asserts byte-identical asm between stages.
*
* row 1 — `let v: u8 = p[0]` for `p: *u8`. Default `*T → T` path:
* unchanged by the fix. Confirms shlex / getopt's
* `slice.ptr[i]` (where `slice.ptr` is `*T` for some
* non-slice T) still routes through the default and emits
* a narrow MOVZBQ. Byte-id between stages.
*
* No 24B-element-emit assertion: the 24B slice / 16B struct / etc.
* multi-word-element copy is a parallel codegen wedge already noted
* inline at cgen.c:6515-6523. Splitting keeps this commit bisect-clean
* (rule 11).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.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;
const char *stride_imm; /* expected `MOVQ\t$<stride>, CX` */
};
static const struct row rows[] = {
/* `p: *[]u8` indexed: result type is `[]u8`, stride = 24
* (slice header). Pre-fix cstage rejected the assignment;
* wwstage silently used stride 8. Post-fix both agree on 24. */
{ "ptr_to_slice",
"fn probe(p: *[]u8) i32 = {\n"
"\tlet r: []u8 = p[0];\n"
"\treturn r.len;\n"
"};\n"
"export fn main() i32 = { return 0; };\n",
"MOVQ\t$24, CX" },
/* `p: *u8` default path: stride = 1, no MOVQ-$-CX scaling at
* all. Confirms the TY_SLICE-clause removal doesn't re-route
* non-slice pointer indexing. */
{ "ptr_to_byte",
"fn probe(p: *u8) i32 = {\n"
"\tlet x: u8 = p[0];\n"
"\treturn x: i32;\n"
"};\n"
"export fn main() i32 = { return 0; };\n",
NULL },
};
static int
slurp(const char *path, char *buf, size_t cap)
{
FILE *f = fopen(path, "rb");
if (!f) return -1;
size_t n = fread(buf, 1, cap - 1, f);
fclose(f);
buf[n] = '\0';
return (int)n;
}
static int
emit_s(const char *w6c, const struct row *r, int i, char *out_s, size_t cap)
{
char src[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/sosi_%d_%d.ww", getpid(), i);
snprintf(out_s, cap, "/tmp/sosi_%d_%d_%s.s",
getpid(), i, w6c[strlen(w6c) - 1] == 'w' ? "ww" : "c");
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, out_s, src);
int rc = runwait(cmd);
unlink(src);
return rc;
}
static int
check_stride(const char *spath, const struct row *r, const char *stage)
{
char buf[1 << 14];
if (slurp(spath, buf, sizeof buf) < 0) {
fprintf(stderr, "row[%s][%s]: cannot read %s\n",
r->label, stage, spath);
return -1;
}
const char *fn = strstr(buf, "TEXT probe");
if (!fn) {
fprintf(stderr,
"row[%s][%s]: no TEXT probe in %s\n",
r->label, stage, spath);
return -1;
}
const char *ret = strstr(fn, "\tRET\n");
if (!ret) ret = fn + strlen(fn);
if (r->stride_imm) {
const char *m = strstr(fn, r->stride_imm);
if (!m || m >= ret) {
fprintf(stderr,
"row[%s][%s]: expected `%s` inside TEXT probe\n",
r->label, stage, r->stride_imm);
return -1;
}
} else {
const char *m = strstr(fn, "\tMOVQ\t$");
while (m && m < ret) {
const char *eol = strchr(m, '\n');
if (eol && eol < ret &&
strstr(m, ", CX\n") && (strstr(m, ", CX\n") < eol)) {
fprintf(stderr,
"row[%s][%s]: unexpected MOVQ $imm, CX "
"(scale) inside TEXT probe — `*u8` path "
"should index with stride 1\n",
r->label, stage);
return -1;
}
m = eol ? strstr(eol, "\tMOVQ\t$") : NULL;
}
}
return 0;
}
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 w6c[640], w6c_ww[640];
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
int have_ww = (access(w6c_ww, X_OK) == 0);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
char cs_path[128], ws_path[128];
if (emit_s(w6c, &rows[i], i, cs_path, sizeof cs_path) != 0) {
fprintf(stderr,
"slice_of_slice_index[cstage][%s]: w6c failed\n",
rows[i].label);
fail++; total++; continue;
}
total++;
if (check_stride(cs_path, &rows[i], "cstage") != 0) fail++;
if (!have_ww) { unlink(cs_path); continue; }
if (emit_s(w6c_ww, &rows[i], i, ws_path, sizeof ws_path) != 0) {
fprintf(stderr,
"slice_of_slice_index[wwstage][%s]: w6c_ww failed\n",
rows[i].label);
fail++; total++;
unlink(cs_path); continue;
}
total++;
if (check_stride(ws_path, &rows[i], "wwstage") != 0) fail++;
total++;
char cmd[512];
snprintf(cmd, sizeof cmd, "cmp -s %s %s", cs_path, ws_path);
if (runwait(cmd) != 0) {
fprintf(stderr,
"slice_of_slice_index[%s]: cstage vs wwstage asm "
"differs\n",
rows[i].label);
fail++;
}
unlink(cs_path); unlink(ws_path);
}
if (fail) {
fprintf(stderr,
"slice_of_slice_index: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("slice_of_slice_index: %d/%d ok\n", total, total);
return 0;
}