w6c+w6c_ww: for-range over a non-ident slice base — bound from len, base ptr spilled (#70)
The N_FORRANGE header's non-ident arm stored cgexpr's AX into the
single bound temp — but a slice-valued cgexpr leaves AX=ptr, BX=len,
CX=cap, so the loop compared i against the DATA POINTER; and the
per-iteration element address had no non-ident base arm at all, so
the bound reload doubled as the base. One slot, two roles, holding
the wrong word. An empty slice coincidentally exited (ptr==0), which
is how regex.finish's `for (let charset .. re.charsets)` — planted
verbatim in fold 1 — stayed latent until fold 4 produced the first
non-empty charsets and SEGV'd. Byte-id both stages (the 989 M_ID
entry held on both-wrong-identical); first-consumer surfacing, the
kwtab/#8 pattern.
Fix mirrors the correct local-base arm: bound = BX (len), base ptr
spilled to a dedicated .rgb slot and reloaded per iteration. Covers
field-chain, indexed-element (the task-#57 shape) and call-result
bases. Two shapes whose cgexpr does NOT deliver the header convention
stay LOUD instead of silently wrong (rule 7): deref bases (*p — the
#11 deref-spine family) and non-ident ARRAY bases.
test/937: field (value+ptr roots), 24B-str-header field (the finish
shape), indexed, call, empty-header, eval-once (header captured at
loop entry, not re-read per iteration) rows + the two reject pins,
per-row cs==ww byte-id; verified failing 14/22 at the #66 parent
bb8a44a.
This commit is contained in:
345
test/wcc/937_forrange_fieldbase_run.c
Normal file
345
test/wcc/937_forrange_fieldbase_run.c
Normal file
@@ -0,0 +1,345 @@
|
||||
/*
|
||||
* 937_forrange_fieldbase_run — #70: by-value for-range over a NON-IDENT
|
||||
* slice/str base (field chain, indexed element, call result). Pre-#70
|
||||
* the range header stored cgexpr's AX — the DATA POINTER (slice cgexpr
|
||||
* leaves AX=ptr, BX=len, CX=cap) — into the single bound temp, and the
|
||||
* per-iteration code had no non-ident base arm, so the bound reload
|
||||
* doubled as the base: i was compared against the POINTER and the loop
|
||||
* walked off the end (regex.finish, SEGV on the first non-empty
|
||||
* charsets; empty slices coincidentally exited on ptr==0 — latent since
|
||||
* fold 1, BYTE-ID both stages, so the 989 M_ID gate held on
|
||||
* both-wrong-identical). Now: bound = len, base ptr spilled to its own
|
||||
* .rgb slot. Deref bases (*p — the #11 family, cgexpr does NOT deliver
|
||||
* the header) and non-ident ARRAY bases stay LOUD.
|
||||
*
|
||||
* row | shape | want
|
||||
* -------------------+----------------------------------------+-----
|
||||
* field_base | h.xs ([]i64) value + ptr root | 0
|
||||
* field_base_24B | p.names ([]str) — the finish() shape | 0
|
||||
* indexed_base | m[0] ([][]i64 element) | 0
|
||||
* call_base | mk() ([]i64 call result) | 0
|
||||
* empty_field | zero header field exits cleanly | 0
|
||||
* eval_once | base reassigned INSIDE the loop — |
|
||||
* | header captured once (a per-iteration |
|
||||
* | re-read of the field would mis-sum) | 0
|
||||
* reject_deref | *q base stays loud (#11) | err
|
||||
* reject_arr_field | h.arr ([3]i64 field) stays loud | err
|
||||
*
|
||||
* Every K_RUN row also asserts cstage/wwstage asm byte-id. NNN<950,
|
||||
* self-contained (/tmp, no imports).
|
||||
*/
|
||||
#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; };
|
||||
|
||||
/* errlog_has — a BUILDERR row must fail WITH its diagnostic; any other
|
||||
* failure (parse error, crash) is a vacuous reject (940 precedent). */
|
||||
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[] = {
|
||||
{ "field_base",
|
||||
"package main;\n"
|
||||
"type holder = struct { xs: []i64, name: str };\n"
|
||||
"fn mk() []i64 = {\n"
|
||||
" let xs: []i64;\n"
|
||||
" append(xs, 7);\n"
|
||||
" append(xs, 8);\n"
|
||||
" return xs;\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let h: holder = holder { xs = mk(), name = \"h\" };\n"
|
||||
" let s1: i64 = 0;\n"
|
||||
" for (let v .. h.xs) { s1 += v; };\n"
|
||||
" if (s1 != 15) { return 1; };\n"
|
||||
" let p: *holder = &h;\n"
|
||||
" let s2: i64 = 0;\n"
|
||||
" for (let v .. p.xs) { s2 += v; };\n"
|
||||
" if (s2 != 15) { return 2; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0, K_RUN, NULL },
|
||||
/* 24B str-header elements through a ptr field — regex.finish's
|
||||
* exact shape (the live SEGV repro). */
|
||||
{ "field_base_24B",
|
||||
"package main;\n"
|
||||
"type bag = struct { names: []str, n: i64 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let names: []str;\n"
|
||||
" append(names, \"ab\");\n"
|
||||
" append(names, \"cde\");\n"
|
||||
" let b: bag = bag { names = names, n = 2 };\n"
|
||||
" let p: *bag = &b;\n"
|
||||
" let total: i64 = 0;\n"
|
||||
" for (let nm .. p.names) {\n"
|
||||
" total += (len(nm): i64);\n"
|
||||
" };\n"
|
||||
" if (total != 5) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0, K_RUN, NULL },
|
||||
/* indexed element base — the task-#57 shape, graduated by #70 */
|
||||
{ "indexed_base",
|
||||
"package main;\n"
|
||||
"fn mk() []i64 = {\n"
|
||||
" let xs: []i64;\n"
|
||||
" append(xs, 7);\n"
|
||||
" append(xs, 8);\n"
|
||||
" return xs;\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let m: [][]i64;\n"
|
||||
" append(m, mk());\n"
|
||||
" let s: i64 = 0;\n"
|
||||
" for (let v .. m[0]) { s += v; };\n"
|
||||
" if (s != 15) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0, K_RUN, NULL },
|
||||
{ "call_base",
|
||||
"package main;\n"
|
||||
"fn mk() []i64 = {\n"
|
||||
" let xs: []i64;\n"
|
||||
" append(xs, 7);\n"
|
||||
" append(xs, 8);\n"
|
||||
" return xs;\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let s: i64 = 0;\n"
|
||||
" for (let v .. mk()) { s += v; };\n"
|
||||
" if (s != 15) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0, K_RUN, NULL },
|
||||
/* zero header: the pre-#70 code exited only because ptr==0; the
|
||||
* fixed code must exit because len==0. */
|
||||
{ "empty_field",
|
||||
"package main;\n"
|
||||
"type holder = struct { xs: []i64, name: str };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let h: holder = holder { name = \"e\", ... };\n"
|
||||
" for (let v .. h.xs) { return 1; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0, K_RUN, NULL },
|
||||
/* the range header (base ptr + len) is captured ONCE at loop
|
||||
* entry — Hare evaluates the range operand once. A fix that
|
||||
* re-read the field per iteration would walk b (sum 7+100) and
|
||||
* never see the reassign-then-original discipline. */
|
||||
{ "eval_once",
|
||||
"package main;\n"
|
||||
"type holder = struct { xs: []i64, n: i64 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let a: []i64;\n"
|
||||
" append(a, 7);\n"
|
||||
" append(a, 8);\n"
|
||||
" let b: []i64;\n"
|
||||
" append(b, 100);\n"
|
||||
" let h: holder = holder { xs = a, n = 0 };\n"
|
||||
" let s: i64 = 0;\n"
|
||||
" for (let v .. h.xs) {\n"
|
||||
" s += v;\n"
|
||||
" h.xs = b;\n"
|
||||
" };\n"
|
||||
" if (s != 15) { return 1; };\n"
|
||||
" if (len(h.xs) != 1) { return 2; };\n"
|
||||
" return 0;\n"
|
||||
"};\n", 0, K_RUN, NULL },
|
||||
/* deref base: cgexpr on *p does not deliver the AX/BX/CX header
|
||||
* (the #11 deref-spine family) — pre-#70 it crashed or mis-summed
|
||||
* SILENTLY; now loud both stages. */
|
||||
{ "reject_deref",
|
||||
"package main;\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let xs: []i64;\n"
|
||||
" append(xs, 1);\n"
|
||||
" let q: *[]i64 = &xs;\n"
|
||||
" let s: i64 = 0;\n"
|
||||
" for (let v .. *q) { s += v; };\n"
|
||||
" return (s: i32);\n"
|
||||
"};\n", 0,
|
||||
K_BUILDERR, "for-range over a deref base unwired (#11)" },
|
||||
/* non-ident ARRAY base: no base spill and a different cgexpr
|
||||
* register shape — loud (rule 7) until a consumer wires it. */
|
||||
{ "reject_arr_field",
|
||||
"package main;\n"
|
||||
"type holder = struct { arr: [3]i64, n: i64 };\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let h: holder = holder { arr = [1, 2, 3], n = 0 };\n"
|
||||
" let s: i64 = 0;\n"
|
||||
" for (let v .. h.arr) { s += v; };\n"
|
||||
" return (s: i32);\n"
|
||||
"};\n", 0,
|
||||
K_BUILDERR, "for-range over a non-ident array base unwired (#70)" },
|
||||
};
|
||||
|
||||
/* 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/tfr_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/tfr_%d_d_%d", getpid(), i);
|
||||
snprintf(errf, sizeof errf, "/tmp/tfr_%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;
|
||||
}
|
||||
|
||||
/* cs==ww .s byte-id (rule 10). */
|
||||
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/tfr_asm_%d_%d.ww", getpid(), i);
|
||||
snprintf(cs, sizeof cs, "/tmp/tfr_asm_%d_%d_c.s", getpid(), i);
|
||||
snprintf(ws, sizeof ws, "/tmp/tfr_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, "forrange_fieldbase: %d/%d checks failed\n",
|
||||
fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("forrange_fieldbase: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user