wcc/cgen: #121 indexed tuple-element read + literal-store round-trip (both-stage)

Reading or storing a tuple element of an indexed array element was
broken across the board (the fold-6 read-path). One fused commit,
both stages, four faces of indexed tuple-element access:

 - FIELD read `tbl[i].N`: was loud ("unsupported field-read shape" --
   the field-read dispatch keyed on an N_IDENT base; an INDEX base fell
   to a fatal). Now resolves &tbl[i] via the place-spine and reads the
   field at addr+foff through the existing per-kind arms (str-triple /
   scalar / fn-ptr).
 - WHOLE read `let e = tbl[i]`: was a silent word0-only truncation
   (plain-tuple kin of #37/#58, which covered only tagged). Now a full
   cursor fill from &tbl[i].
 - STORE `a[i] = (3,4)` (N_TUPLE-literal rhs): was a silent word0-only
   store -- the write face of the read. The aggregate-store-into-index
   site handled ident/dot/deref tuple rhs but not the literal; now it
   materializes the literal and word-copies. Narrow: N_IDENT base only
   (N_DOT/chained stay deferred, #270).
 - for-range over a const-slice-of-tuple: was a divergent SEGV; now a
   symmetric loud-stop on both stages (filed #122).

The store and read were a round-trip that passed test 809 only by luck
(broken store XOR broken read canceled). Fixing the read alone exposed
the silent store; rule-7 obliges fixing both, so 809 is now genuinely
correct, not luck-correct. Both faces are byte-id-blind (#263) -- the
net is a runtime round-trip pin with distinct-per-word values and a
real call clobbering the cursor registers between store and read, so a
word0-only store or read is caught. Both stages byte-identical
(990-997 green). Pin 947_tuple_index_read_run.
This commit is contained in:
2026-06-06 22:23:43 +09:00
parent 761cfa4524
commit 754944a755
8 changed files with 1317 additions and 4 deletions

View File

@@ -1382,9 +1382,11 @@ static const struct row rows[] = {
" return 0;\n"
"};\n", 0,
K_BUILDERR, "tuple arg element kind unsupported" },
/* variadic-of-tuples ((i64,i64)...) stays LOUD via the
* tuple-in-slice read surface — bounded, not silent (demand 2). */
{ "t2_reject_variadic_of_tuples",
/* variadic-of-tuples ((i64,i64)...): `ts[0].0` is an indexed-slice
* tuple-element FIELD read — #121 leg (a) now resolves it (was LOUD
* "unsupported field-read shape"; the dispatch fired only for an
* N_IDENT base). Runs correct both stages + byte-id. */
{ "t2_variadic_of_tuples",
"package main;\n"
"fn first(ts: (i64, i64)...) i64 = {\n"
" if (len(ts) == 0) { return -1; };\n"
@@ -1395,7 +1397,7 @@ static const struct row rows[] = {
" if (first(t) != 3) { return 1; };\n"
" return 0;\n"
"};\n", 0,
K_BUILDERR, "unsupported field-read shape" },
K_RUN, NULL },
/* rule-7: a tuple arg from a source whose cgexpr does NOT fill
* the cursor (here a struct-field chain) dies loud — pre-C-t2
* it fell to the scalar single-PUSHQ default silently.

View File

@@ -0,0 +1,297 @@
/*
* 947_tuple_index_read_run — #121: const-slice tuple-element READ.
*
* Three legs of indexed-tuple read off a `const [](str,*fn)` (fold-6's
* charclass_map shape), all sharing the &tbl[i] place-spine (#116):
*
* (a) FIELD `tbl[i].N` (bound) — was LOUD both stages ("unsupported
* field-read shape": the tuple-field dispatch fired only for an
* N_IDENT base; an N_INDEX base fell to the read-resolver fatal).
* Now resolved via cgplaceaddr + the per-element-kind arms
* (str-triple / fn-8B). align-loud→both-accept-correct.
*
* (b) WHOLE `let e = tbl[i]` then read e.N — was SILENT both-wrong-
* IDENTICAL (#263, word0-only: the in-cap tuple receive read a
* register cursor the N_INDEX load never filled past word0, so
* str.len→0 and the *fn word was garbage). Now fills all gptotal
* cursor words from &tbl[i]. align-both-correct.
*
* (c) for-range over a module-global slice — LOUD-STOP both stages
* (the for-range spine has no global-base resolution; pre-fix it
* read saved-BP → SEGV with divergent cs≠ww asm). Filed as a #121
* sibling; loud is byte-id-neutral + a pure improvement.
*
* POLARITY: leg (a) cstage is the byte-id ref (align-loud→accept); leg
* (b) byte-id is BLIND (#263) — both stages were identically truncated,
* so the runtime READ-BACK with DISTINCT-per-row values (str lengths
* differ; fns fa↔'a'/fz↔'z' differ) is the only net: a wrong/truncated
* word is CAUGHT, not masked.
*
* MUST use `[](str,*fn)`, NOT `[](str,int)` — the latter is #120
* wwstage-checker-rejected ("let: not assignable") BEFORE the read runs.
*
* NNN<950, self-contained (/tmp, no imports) — rule-14's selfhost-sibling
* race does not apply (941/944/945/946 precedent). Every K_RUN row also
* pins cstage/wwstage asm byte-id.
*/
#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;
}
#define K_RUN 0 /* build+run both drivers, exit==want, + cs==ww byte-id */
#define K_BUILDERR 1 /* build must FAIL with experr on BOTH drivers (rule 7) */
struct row { const char *label; const char *src; int want;
int kind; const char *experr; };
static int
errlog_has(const char *path, const char *needle)
{
FILE *f = fopen(path, "rb");
if (!f) return 0;
char buf[8192];
size_t got = fread(buf, 1, sizeof buf - 1, f);
fclose(f);
buf[got] = '\0';
return strstr(buf, needle) != NULL;
}
static const struct row rows[] = {
/* leg (a): direct `tbl[i].N` field read, bound then observed.
* str field (distinct lengths 2 vs 4) + fn field (distinct fns,
* each called back). Was LOUD both stages at base 761cfa4. */
{ "field_read",
"package main;\n"
"fn fa(c: rune) bool = { return c == 'a'; };\n"
"fn fz(c: rune) bool = { return c == 'z'; };\n"
"const tbl: [](str, *fn(c: rune) bool) = "
"[(\"ab\", &fa), (\"cdef\", &fz)];\n"
"export fn main() i32 = {\n"
" let s1 = tbl[1].0;\n"
" let s0 = tbl[0].0;\n"
" if (len(s1) != 4) { return 1; };\n"
" if (len(s0) != 2) { return 2; };\n"
" let f1 = tbl[1].1;\n"
" if (!(*f1)('z')) { return 3; };\n"
" if ((*f1)('a')) { return 4; };\n"
" let f0 = tbl[0].1;\n"
" if (!(*f0)('a')) { return 5; };\n"
" if ((*f0)('z')) { return 6; };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
/* leg (b): whole-element bind then read e.0 (str.len, catches the
* w1→0 truncation) + e.1 (the *fn word, catches w3 garbage).
* DISTINCT per row — a truncated/wrong word is caught not masked.
* Was SILENT both-wrong-identical (#263) at base 761cfa4. */
{ "whole_element",
"package main;\n"
"fn fa(c: rune) bool = { return c == 'a'; };\n"
"fn fz(c: rune) bool = { return c == 'z'; };\n"
"const tbl: [](str, *fn(c: rune) bool) = "
"[(\"ab\", &fa), (\"cdef\", &fz)];\n"
"export fn main() i32 = {\n"
" let e = tbl[1];\n"
" if (len(e.0) != 4) { return 1; };\n"
" if (!(*e.1)('z')) { return 2; };\n"
" if ((*e.1)('a')) { return 3; };\n"
" let e0 = tbl[0];\n"
" if (len(e0.0) != 2) { return 4; };\n"
" if (!(*e0.1)('a')) { return 5; };\n"
" if ((*e0.1)('z')) { return 6; };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
/* write-face of leg (b): a tuple-LITERAL store into an indexed
* element `a[i] = (3,4)`, read back whole. DISTINCT words per row
* (3≠4, 7≠9) so the pre-fix word0-only store (cgen.c:6249 fell to
* the scalar tail) is CAUGHT — t.1 would read 0. Both faces (this
* store + the leg-b read) must be correct or the round-trip fails;
* the read alone was green by stale-register luck (809 tuple_elem). */
{ "store_roundtrip",
"package main;\n"
"export fn main() i32 = {\n"
" let a: [2](u64, u64);\n"
" a[1] = (3u64, 4u64);\n"
" a[0] = (7u64, 9u64);\n"
" let t = a[1];\n"
" if (t.0 != 3) { return 1; };\n"
" if (t.1 != 4) { return 2; };\n"
" let s = a[0];\n"
" if (s.0 != 7) { return 3; };\n"
" if (s.1 != 9) { return 4; };\n"
" return 0;\n"
"};\n", 0, K_RUN, NULL },
/* leg (c): for-range over a module-global slice LOUD-STOPS,
* symmetric both stages (segfault→compile-error, byte-id-neutral).
* `n += 1` (not len()) sidesteps the #26 len-vs-int checker reject
* so the cgen loud is what fires, on BOTH stages. */
{ "forrange_global_loud",
"package main;\n"
"fn fa(c: rune) bool = { return c == 'a'; };\n"
"const tbl: [](str, *fn(c: rune) bool) = "
"[(\"ab\", &fa), (\"cdef\", &fa)];\n"
"export fn main() i32 = {\n"
" let n: int = 0;\n"
" for (let e .. tbl) { n += 1; };\n"
" return n: i32;\n"
"};\n", 0, K_BUILDERR,
"for-range over a module-global" },
};
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/tir_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/tir_%d_d_%d", getpid(), i);
snprintf(errf, sizeof errf, "/tmp/tir_%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 (r->kind == K_BUILDERR) {
int ok = (brc != 0)
&& (r->experr == NULL || errlog_has(errf, r->experr));
if (!ok)
fprintf(stderr, "row[%s]: %s expected loud builderr "
"\"%s\" (brc=%d)\n", r->label, driver,
r->experr ? r->experr : "", brc);
unlink(src); unlink(errf); rmdir(tmpdir);
return ok ? 0 : 1;
}
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;
}
static int
asm_byte_identical(const char *bin, const struct row *r, int i)
{
char src[96], cs[96], ws[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/tir_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/tir_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/tir_asm_%d_%d_w.s", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c errored\n", r->label);
unlink(src);
return -1;
}
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null",
bin, ws, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label);
unlink(src); unlink(cs);
return -1;
}
int rc = slurp_eq(cs, ws);
if (rc != 0)
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
r->label);
unlink(src); unlink(cs); unlink(ws);
return rc;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[2080];
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 cdrv[2120], wdrv[2120];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
total++;
if (run_driver(cdrv, &rows[i], i) != 0) fail++;
}
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
if (run_driver(wdrv, &rows[i], i) != 0) fail++;
}
for (int i = 0; i < n; i++) {
if (rows[i].kind == K_BUILDERR)
continue;
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0) fail++;
}
}
if (fail) {
fprintf(stderr, "tuple_index_read: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("tuple_index_read: %d/%d ok\n", total, total);
return 0;
}