wcc/cgen: #8 def str-array element load — emit + pre-intern def-twin + ww load (both stages)
def C:[N]str; C[i] was loud (undefined main.C) both stages. Three folded fixes, one commit (splitting would ship a bisect point where wwstage silently returns an element address instead of .len): P0: the str-array static-init emitter dropped its vestigial directive =="DATAW" gate so a def table rides the same DATAW-header + DATAR-reloc path as let. A def str/slice table lives in DATAW by w6a's A_DATAR-holder constraint -- placement only; def immutability stays checker-enforced. P1: let_pre_intern / letpreintern walked N_LET only, so a def str-array's element string-literals were never interned (dangling _S_n). Extracted a pre_intern_strarray SSoT helper, called for a def str-array arm too, both stages. Scoped to str fixed arrays; def []T / def [N][]T stay loud (#270). P2: wwstage cgenexpr lacked a defvartnode fallback in the indexed-element classify, so a def str-array element load returned the element address instead of the slice header -- a silent miscompile. One line, aligning wwstage up to cstage (which was correct). C[1].len now = 3 both stages, byte-identical. byte-id 990-997 8/8; w6c/w6c_ww move. test/wcc/819 table-driven. The def-global scalar str index sibling (def S:str; S[0]) stays task #14.
This commit is contained in:
274
test/wcc/819_def_str_table.c
Normal file
274
test/wcc/819_def_str_table.c
Normal file
@@ -0,0 +1,274 @@
|
||||
/*
|
||||
* 819_def_str_table — `def C: [N]str = [...]` module-level str-array static
|
||||
* init + element load (#8 / GAP-B, #270 family). A `let [N]str` global static-
|
||||
* inits correctly (#18); the def-twin did NOT — both stages emitted the index
|
||||
* ref `LEAQ main.C(SB)` but NEVER emitted the backing DATA block, so w6l failed
|
||||
* with `undefined reference to 'main.C'` (LOUD, symmetric, both stages — not
|
||||
* silent, not cs≠ww).
|
||||
*
|
||||
* ROOT: emit_strarray_data / emitstrarraydata (the str-array DATAW header +
|
||||
* per-element A_DATAR ptr-reloc emitter) was gated to the "DATAW" directive
|
||||
* (let only). A def array routes through emit_array_data(..,"DATA",..) so the
|
||||
* gate skipped it → no DATA block. The str backing must live in DATAW anyway
|
||||
* (w6a requires a DATAR reloc-holder be a DATAW slot, asm.c:362); def
|
||||
* immutability is checker-enforced, independent of the section bit. The fix
|
||||
* drops the directive gate in BOTH stages so a def str-array rides the same
|
||||
* DATAW+DATAR emitter as let. int-defs are plain DATA (no reloc) — unaffected.
|
||||
*
|
||||
* row | shape | want
|
||||
* -----------------+---------------------------------------------+------
|
||||
* def_str_len | def C:[2]str; C.len | 2
|
||||
* def_str_elem0 | def C:[2]str=["ab","cde"]; C[0].len | 2
|
||||
* def_str_elem1 | def C:[2]str=["ab","cde"]; C[1].len | 3
|
||||
* def_str_content | def C:[2]str=["ab","cde"]; C[1][0] ('c') | 99 (DATAR reloc
|
||||
* | | resolves to the
|
||||
* | | right _S_ rodata)
|
||||
* def_str_multi | def C:[3]str=["a","bb","ccc"]; C[2].len | 3
|
||||
* def_str_varbind | def C:[3]str; let s=C[2]; s.len | 3
|
||||
* let_str_elem1 | let C:[2]str=["ab","cde"]; C[1].len (CTRL) | 3 (#18, already
|
||||
* | | works; no-regr)
|
||||
*
|
||||
* Each def row fails-to-LINK pre-fix (mutation-sane: delete the gate-drop and
|
||||
* the def rows go back to w6l undefined). let_str_elem1 is the #18 regression
|
||||
* guard. A byte-id pass pins cstage==wwstage `.s` for every row (the both-
|
||||
* wrong-identical convergence: pre-fix both omit the block, post-fix both emit
|
||||
* the same DATAW+DATAR).
|
||||
*/
|
||||
#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; };
|
||||
|
||||
static const struct row rows[] = {
|
||||
/* def_str_len — `.len` on a def str-array (static element count; works
|
||||
* via the def-twin .len arm even pre-#8, but pinned here for the set). */
|
||||
{ "def_str_len",
|
||||
"package main;\n"
|
||||
"def C: [2]str = [\"ab\", \"cde\"];\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\treturn C.len: i32;\n"
|
||||
"};\n",
|
||||
2 },
|
||||
|
||||
/* def_str_elem0 — THE regression: load element 0, read its .len.
|
||||
* Pre-#8 the def DATA block is never emitted → w6l undefined main.C. */
|
||||
{ "def_str_elem0",
|
||||
"package main;\n"
|
||||
"def C: [2]str = [\"ab\", \"cde\"];\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\treturn C[0].len: i32;\n"
|
||||
"};\n",
|
||||
2 },
|
||||
|
||||
/* def_str_elem1 — element 1 .len (ken oracle row). */
|
||||
{ "def_str_elem1",
|
||||
"package main;\n"
|
||||
"def C: [2]str = [\"ab\", \"cde\"];\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\treturn C[1].len: i32;\n"
|
||||
"};\n",
|
||||
3 },
|
||||
|
||||
/* def_str_content — C[1][0] is 'c' (99). Proves the per-element A_DATAR
|
||||
* ptr-reloc resolves to the right _S_ rodata row, not just that a block
|
||||
* exists. */
|
||||
{ "def_str_content",
|
||||
"package main;\n"
|
||||
"def C: [2]str = [\"ab\", \"cde\"];\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\treturn C[1][0]: i32;\n"
|
||||
"};\n",
|
||||
99 },
|
||||
|
||||
/* def_str_multi — 3-element def array, element 2 .len. */
|
||||
{ "def_str_multi",
|
||||
"package main;\n"
|
||||
"def C: [3]str = [\"a\", \"bb\", \"ccc\"];\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\treturn C[2].len: i32;\n"
|
||||
"};\n",
|
||||
3 },
|
||||
|
||||
/* def_str_varbind — bind an element to a let, then read .len (ken
|
||||
* matrix var-bind form: a full 24B str-header element copy). */
|
||||
{ "def_str_varbind",
|
||||
"package main;\n"
|
||||
"def C: [3]str = [\"x\", \"yy\", \"zzz\"];\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet s: str = C[2];\n"
|
||||
"\treturn s.len: i32;\n"
|
||||
"};\n",
|
||||
3 },
|
||||
|
||||
/* let_str_elem1 — CONTROL: the #18 let-global str-array (already works).
|
||||
* Guards against the gate-drop regressing the let path. */
|
||||
{ "let_str_elem1",
|
||||
"package main;\n"
|
||||
"let C: [2]str = [\"ab\", \"cde\"];\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\treturn C[1].len: i32;\n"
|
||||
"};\n",
|
||||
3 },
|
||||
};
|
||||
|
||||
static int
|
||||
run_driver(const char *driver, const struct row *r, int i)
|
||||
{
|
||||
char src[64], tmpdir[64], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/dst_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/dst_%d_d_%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 2>/dev/null",
|
||||
tmpdir, driver, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: build via %s failed\n",
|
||||
r->label, driver);
|
||||
unlink(src); rmdir(tmpdir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *base = strrchr(src, '/');
|
||||
base = base ? base + 1 : src;
|
||||
char outbin[128];
|
||||
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); rmdir(tmpdir);
|
||||
return got;
|
||||
}
|
||||
|
||||
/* asm_byte_identical — w6c vs w6c_ww .s for the same source must match. */
|
||||
static int
|
||||
asm_byte_identical(const char *bin, const struct row *r, int i)
|
||||
{
|
||||
char src[64], cs[64], ws[64], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/dst_asm_%d_%d.ww", getpid(), i);
|
||||
snprintf(cs, sizeof cs, "/tmp/dst_asm_%d_%d_c.s", getpid(), i);
|
||||
snprintf(ws, sizeof ws, "/tmp/dst_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;
|
||||
}
|
||||
|
||||
FILE *fc = fopen(cs, "rb");
|
||||
FILE *fw = fopen(ws, "rb");
|
||||
int rc = 0;
|
||||
if (!fc || !fw) {
|
||||
rc = -1;
|
||||
} else {
|
||||
for (;;) {
|
||||
int a = fgetc(fc);
|
||||
int b = fgetc(fw);
|
||||
if (a != b) { rc = -1; break; }
|
||||
if (a == EOF) break;
|
||||
}
|
||||
}
|
||||
if (fc) fclose(fc);
|
||||
if (fw) fclose(fw);
|
||||
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[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 cdrv[1024];
|
||||
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
||||
char wdrv[1024];
|
||||
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
||||
|
||||
struct { const char *name; const char *path; int gated_on_existence; }
|
||||
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_on_existence
|
||||
&& access(drivers[d].path, X_OK) != 0) {
|
||||
fprintf(stderr, "def_str_table: skip %s (no %s)\n",
|
||||
drivers[d].name, drivers[d].path);
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
int got = run_driver(drivers[d].path, &rows[i], i);
|
||||
total++;
|
||||
if (got != rows[i].want) {
|
||||
fprintf(stderr,
|
||||
"def_str_table[%s][%s]: exit=%d want=%d\n",
|
||||
drivers[d].name, rows[i].label,
|
||||
got, rows[i].want);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (access(wdrv, X_OK) == 0) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
total++;
|
||||
if (asm_byte_identical(bin, &rows[i], i) != 0)
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr,
|
||||
"def_str_table: %d/%d fixtures failed\n", fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("def_str_table: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user