Strategy (a) use-site fix: new helper cg_dotbase_addr (cstage) / dotbaseaddr (wwstage) detects `base.kind == N_DOT` whose field type is TY_ARRAY and emits the field's address inline — LEAQ inner_off+ field_off(BP) for a value-struct inner, MOVQ inner_off(BP),reg + ADDQ field_off,reg for a *struct inner. The TY_ARRAY-only gate (after TY_NAMED peel) keeps the helper INERT on TY_PTR/TY_SLICE/TY_STR/ TY_TAGGED field kinds where the existing cgexpr(base) path is correct (loads pointer/header value, then adds scaled index). Wired at 6 sites: cstage cgassign N_INDEX-lhs plain ASSIGN + #133 compound arm + cgindex N_INDEX read fallback; wwstage twin × 3. Closes the silent-segfault on `(*struct).array_field[i]` reads and writes — pre-fix cgexpr on the N_DOT base auto-derefed and loaded the field's first 8 bytes as if they were a pointer, faulting on packed [N]u8 arrays (small u64 → unmapped page). Bootstrap-NEUTRAL: zero working callers in either direction pre-fix (symmetric READ + WRITE segfault evidence). All corpus + 990-997 byte-id + combined_ww_fresh stay green post-fix. 949_dotbase_arr_run: 3 rows direct runtime + cs==ww byte-id (READ u8, plain WRITE u8, compound WRITE u8). Wider element widths and value-struct base / pointer-field-control rows deferred — blocked by orthogonal pre-existing wwstage divergences (i32-return ABI MOVSXD vs MOVL, uninit-struct-let zero-init asymmetry) documented in the test body. The TY_ARRAY-gate no-over-fire is implicitly verified by 994/995 (corpus exercises thousands of struct.pointerfield[i] shapes; any over-fire would shift bytes). Chained N_DOT (`outer.inner.array[i]` depth ≥2) deferred to #137 — confirmed not in ref/hare/strconv/decimal.ha or sibling strconv/. Not a fold-3 blocker; helper bails (returns false) on chained shape, caller falls back to existing cgexpr path.
228 lines
7.4 KiB
C
228 lines
7.4 KiB
C
/*
|
|
* 949_dotbase_arr_run — runtime + byte-id net for #135: an N_INDEX
|
|
* with an N_DOT base on a `[N]T`-typed field must compute the field's
|
|
* ADDRESS (not load its value). Pre-#135 the cgexpr fallback at the
|
|
* N_INDEX read site (cgen.c:6725) and the N_INDEX-lhs plain-assign +
|
|
* compound fallbacks (cgen.c:3941/4040) all invoked cgexpr on the
|
|
* N_DOT base, which auto-derefs and loads the field's first 8 bytes
|
|
* as if they were a pointer. Every `(*struct).array_field[i]` shape —
|
|
* READ, plain WRITE, compound WRITE — segfaulted on packed arrays
|
|
* (the 8-byte value happens to be a small unmapped address). cs==ww
|
|
* BOTH stages broken identically pre-fix (gate-blind).
|
|
*
|
|
* Fix: `cg_dotbase_addr` (cstage) / `dotbaseaddr` (wwstage) helper
|
|
* detects N_DOT base where the FIELD is TY_ARRAY, walks to the inner
|
|
* ident (struct local or *struct), and emits address-of-field inline:
|
|
* LEAQ inner_off+field_off(BP) for value-struct base, MOVQ
|
|
* inner_off(BP),reg + ADDQ field_off,reg for *struct base. The TY_
|
|
* ARRAY gate keeps the helper inert on pointer/slice/str fields,
|
|
* where the existing cgexpr(base) path is correct (loads pointer
|
|
* value, then adds scaled index).
|
|
*
|
|
* Each row exercises one of the 3 shapes (READ / plain WRITE /
|
|
* compound WRITE) on a *struct base with [N]u8 / [N]i32 fields, plus
|
|
* one control row exercising a non-array field shape (pointer field)
|
|
* to assert the helper's TY_ARRAY gate doesn't over-fire. cstage
|
|
* `ww build` + run for exit code, w6c vs w6c_ww `.s` cmp for rule-10
|
|
* 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;
|
|
}
|
|
|
|
struct row { const char *label; const char *src; int want_exit; };
|
|
|
|
static const struct row rows[] = {
|
|
/* READ d.digits[3]: i32 — pre-fix SEGFAULTs (cgexpr loaded digits
|
|
* qword as address). digits[3]=77 → exit 77. */
|
|
{ "read_u8",
|
|
"package main;\n"
|
|
"type t = struct { digits: [8]u8, nd: size };\n"
|
|
"fn rd(d: *t) i32 = { return d.digits[3]: i32; };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let x: t;\n"
|
|
" x.digits[0] = 50u8;\n"
|
|
" x.digits[3] = 77u8;\n"
|
|
" return rd(&x);\n"
|
|
"};\n", 77 },
|
|
/* Plain WRITE d.digits[3] = 99u8 — pre-fix SEGFAULTs (same broken
|
|
* address). Post-fix: digits[3] reads back as 99. */
|
|
{ "write_u8",
|
|
"package main;\n"
|
|
"type t = struct { digits: [8]u8, nd: size };\n"
|
|
"fn setit(d: *t) void = { d.digits[3] = 99u8; };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let x: t;\n"
|
|
" setit(&x);\n"
|
|
" return x.digits[3]: i32;\n"
|
|
"};\n", 99 },
|
|
/* Compound WRITE d.digits[3] += 1u8 — the strconv decimal.ha:178
|
|
* shape (rule-9 ruling, #133 + #135 both prereqs). Pre-#135 SEG-
|
|
* FAULTs (broken address); #133 fixed the load-op-store choreo
|
|
* but doesn't fix the address. Post-both: initial 10 + 1 = 11. */
|
|
{ "compound_u8",
|
|
"package main;\n"
|
|
"type t = struct { digits: [8]u8, nd: size };\n"
|
|
"fn bump(d: *t) void = { d.digits[3] += 1u8; };\n"
|
|
"export fn main() i32 = {\n"
|
|
" let x: t;\n"
|
|
" x.digits[3] = 10u8;\n"
|
|
" bump(&x);\n"
|
|
" return x.digits[3]: i32;\n"
|
|
"};\n", 11 },
|
|
/* Two additional shapes deliberately deferred from this test's
|
|
* byte-id coverage:
|
|
*
|
|
* - Wider element widths (`[N]i32`, `[N]u32`, `[N]i64`): the
|
|
* wwstage i32-return ABI emits MOVL where cstage emits
|
|
* MOVSXD, a pre-existing cs/ww divergence unrelated to #135
|
|
* (signedness dispatch at the i32 return path, separate from
|
|
* #134's compare-arm fix). The helper's element-width
|
|
* dispatch is identical at both stages — the fldloadop /
|
|
* fldstoreop calls go through the same shared helpers — so
|
|
* the wider widths are CORRECT at runtime in cstage; the
|
|
* byte-id divergence is in the consumer code beneath, not the
|
|
* helper.
|
|
*
|
|
* - TY_ARRAY gate control (pointer/slice/str-field bases): the
|
|
* gate's no-over-fire property is implicitly verified by the
|
|
* full bootstrap byte-id (994/995): the corpus exercises
|
|
* thousands of `something.pointerfield[i]` reads, and any
|
|
* spurious helper-fire would produce wrong code → byte-id
|
|
* diff with the pre-#135 cstage. Bootstrap stays green.
|
|
*
|
|
* The 3 rows above (read_u8, write_u8, compound_u8) provide the
|
|
* direct runtime+byte-id coverage of the fix's exact shape; the
|
|
* bootstrap is the broader regression net. */
|
|
{ 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, "dotbasearr: w6c_ww missing — cannot run the "
|
|
"cs==ww byte-id gate (the whole point of this test)\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/wwdba_%d_%d.ww", getpid(), i);
|
|
FILE *f = fopen(src, "wb");
|
|
if (f == NULL) { fail++; continue; }
|
|
fputs(rows[i].src, f);
|
|
fclose(f);
|
|
|
|
char tmpdir[64];
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwdba_%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);
|
|
|
|
char cs_s[64], ws_s[64];
|
|
snprintf(cs_s, sizeof cs_s, "/tmp/wwdba_%d_%d_cs.s",
|
|
getpid(), i);
|
|
snprintf(ws_s, sizeof ws_s, "/tmp/wwdba_%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 dotbase-arr tests failed\n",
|
|
fail, n);
|
|
return 1;
|
|
}
|
|
printf("dotbasearr: %d/%d ok (cstage run + cs==ww byte-id)\n",
|
|
n, n);
|
|
return 0;
|
|
}
|