Files
ww/test/wcc/919_strarray_static_run.c
Hojun-Cho 58c8f4be02 w6c+cgen: emit module-level [N]str static-init data + relocations (fix #18)
A module-level `let xs: [N]str = ["a","b",...];` static init emitted no
.data: a str element carries a ptr->rodata relocation, not just bytes, so
it fell through the byte-only array-emit path and left the table symbol
undefined (w6l: undefined reference). Shared gap on both stages, not
rule-10.

emit_strarray_data / emitstrarraydata apply the scalar-str-global pattern
per element at offset idx*esz: a DATAW row of {0-ptr placeholder, LE len,
cap} plus a per-element DATAR sym+idx*esz,_S_n reloc. let_pre_intern /
letpreintern pre-intern each element strlit so the _S_ rodata rows precede
the DATAR references. Stride routes through etype->size (rule 13). Scoped
to the DATAW (`let`) directive: A_DATAR requires a DATAW holder, so
`def [N]str` and str-in-aggregate stay a filed follow-up.

919_strarray_static_run pins runtime (len-sum, element .ptr deref, var
index, empty slot, repeat suffix) + cs==ww byte-id. w6c + wwdump
combined.ww regenerated.
2026-06-03 11:35:03 +09:00

212 lines
7.1 KiB
C

/*
* 919_strarray_static_run — BUG #18. Runtime + cs==ww byte-id net for a
* module-level `let xs: [N]str = ["a","b",…];` static table.
*
* THE BUG (BOTH stages dropped it — shared-logic gap, not rule-10):
* A module-level [N]str static init emitted NO .data at all. The
* per-element str header carries a ptr→rodata relocation (not just
* bytes), so it can't ride emit_array_lit_bytes (byte-only): the str-
* element case fell through to fold_int_literal, returned 0, and
* emit_lets zero-init'd / skipped — leaving `main.<tab>` undefined.
* `w6l: undefined reference` at link. Function-LOCAL [N]str worked
* (runtime element stores); only the module-level STATIC DATA form was
* broken.
*
* THE FIX (#18): emit_strarray_data / emitstrarraydata apply the scalar-
* str-global pattern (DATAW header with a zero ptr placeholder + inline
* LE len, then a per-element `DATAR sym+idx*esz(SB),_S_n(SB)`) at each
* element offset. let_pre_intern / letpreintern pre-intern each element
* strlit so the _S_ rodata rows precede the DATAR references. Scoped to
* the DATAW (`let`) directive — A_DATAR requires a DATAW holder.
*
* EACH ROW CARRIES BOTH DIMENSIONS (801 model):
* (a) cstage `ww build` + run, asserting the exit — proves the table's
* .len bytes are correct AND each element's .ptr relocation
* resolves to the right rodata label (the rows deref a NON-zero
* element's first byte, so a dropped/wrong reloc gives a wrong
* char — built-in negative control).
* (b) w6c vs w6c_ww `.s` cmp — FAILS if the stages diverge.
*
* GATE POLARITY: must stay GREEN. A wrong exit means the static table
* relocations regressed; a byte-id FAIL means the stages diverged.
*
* NB: the rows read elements via the `.ptr`/`.len` pseudo-fields, NOT
* the `len()` builtin — `len(xs[i])` on an indexed str element is a
* separate pre-existing read-side miscompile (returns the ptr; filed
* alongside #18), orthogonal to the emission fix under test here.
*/
#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[] = {
/* .len bytes: 2+3+1 == 6. A dropped table would fail to link. */
{ "strtab_len",
"package main;\n"
"let t: [3]str = [\"ab\",\"cde\",\"f\"];\n"
"export fn main() i32 = { return (t[0].len + t[1].len + t[2].len): i32; };\n",
6 },
/* element-2 .ptr reloc: *(t[2].ptr) == 'C' (67), NOT 'A' — proves
* the +2*esz relocation resolves to "C", not element 0. */
{ "strtab_ptr_elem2",
"package main;\n"
"let t: [3]str = [\"A\",\"B\",\"C\"];\n"
"export fn main() i32 = { let p: *u8 = t[2].ptr; return (*p): i32; };\n",
67 },
/* runtime-indexed .ptr: i=1, *(t[i].ptr) == 'y' (121) — the
* LEAQ tab(SB)+i*esz addressing reaches the right element's reloc. */
{ "strtab_ptr_varindex",
"package main;\n"
"let t: [3]str = [\"xx\",\"yyy\",\"z\"];\n"
"export fn main() i32 = { let i: i32 = 1; let p: *u8 = t[i].ptr; "
"return (*p): i32; };\n",
121 },
/* empty-element (no reloc, len 0) followed by a real element: t[0]
* len==0, *(t[1].ptr) == 'Z' (90) — the empty slot must not shift
* the following element's reloc offset. */
{ "strtab_empty_then_ptr",
"package main;\n"
"let t: [2]str = [\"\",\"Z\"];\n"
"export fn main() i32 = { if (t[0].len != 0) { return 88; }; "
"let p: *u8 = t[1].ptr; return (*p): i32; };\n",
90 },
/* repeat suffix `[X, Y...]` — the trailing `...` fills the rest of
* [3]str with the last explicit element ("Y"). t[2] is a repeat-
* filled slot: *(t[2].ptr) == 'Y' (89), NOT 'X' — proves the +48
* fill row's DATAR resolves to last_ev ("Y"), exercising the
* repeat branch (its own len-fill + DATAR loop) the other rows skip. */
{ "strtab_repeat_suffix",
"package main;\n"
"let t: [3]str = [\"X\",\"Y\"...];\n"
"export fn main() i32 = { let p: *u8 = t[2].ptr; return (*p): i32; };\n",
89 },
{ 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, "strarray_static: 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/wwsas_%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/wwsas_%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/wwsas_%d_%d_cs.s", getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/wwsas_%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 strarray static-init tests failed\n",
fail, n);
return 1;
}
printf("strarray_static: %d/%d ok (cstage run + cs==ww byte-id)\n",
n, n);
return 0;
}