/* * 949_f6_header_run — F6 24B str/slice header partial-load/store cluster. * * Three members of the cat-A drain F6 sub-train, each a 24B-header * (ptr,len,cap) materialize choke-point that dropped one or more words: * * #19 (align-UP, wwstage-only): a str→[]u8 cast from a NON-LOCAL * source (global ident / struct field / call result) missed the * cap=len synth (`MOVQ BX, CX`) — wwstage gated the synth on a * local-ident source only, leaving CX = the str's stale word-16 * garbage cap. cstage was already type-keyed (TY_STR→TY_SLICE) and * correct, so this aligns wwstage UP; the rows pin cs==ww byte-id. * * #28 (#263 both-stages): a tuple slice-element read `t.0` over a * `([]u8, i64)` loaded only the ptr word — the str arm handled str * but a SLICE element fell to the scalar tail, so len/cap took * stale registers (a clobber() call between build and read leaves * junk in BX/CX). Both stages were byte-id-WRONG; fixed together. * * #29 (#263 both-stages): a chained-dot str leaf `o.i.s` (depth-2) * emitted only (ptr,len) — the cap word was dropped, so a junk * strlit before the chain left CX stale. Both stages byte-id-WRONG. * * POLARITY: #19 cstage is the byte-id ref (align wwstage UP). #28/#29 are * #263 (both byte-id-WRONG) — the fix lands BOTH halves together, so the * rows assert both-stages-CORRECT post-fix (no residual rows); the * runtime read-back with DISTINCT values (cap=3/len=5/cap=5) is the net * a byte-id gate alone was blind to. * * NNN<950, self-contained (/tmp, no imports) — rule-14's selfhost-sibling * race does not apply (941/944/945/947 precedent). Every K_RUN row also * pins cstage/wwstage asm byte-id. */ #include #include #include #include #include #include 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 const struct row rows[] = { /* #19: str→[]u8 cast from a GLOBAL ident source, threaded through a * call return. cap must == len == 3. Pre-fix wwstage left CX = the * str's stale word-16 (rc=120). */ { "global_str_cap", "package main;\n" "let G: str = \"abc\";\n" "fn f() []u8 = { return G: []u8; };\n" "export fn main() i32 = {\n" " let s: []u8 = f();\n" " return s.cap: i32;\n" "};\n", 3, K_RUN, NULL }, /* #19 sibling: str→[]u8 cast from a struct FIELD source. Same * type-keyed synth path; distinct len (5) catches a dropped cap. */ { "field_str_cap", "package main;\n" "type box = struct { s: str, x: i64 };\n" "export fn main() i32 = {\n" " let b: box = box { s = \"hello\", x = 0 };\n" " let v: []u8 = b.s: []u8;\n" " return v.cap: i32;\n" "};\n", 5, K_RUN, NULL }, /* #19 control: LOCAL str source (the pre-fix-covered shape) still * emits the synth — guards against a regression of the common case. */ { "local_str_cap", "package main;\n" "export fn main() i32 = {\n" " let s: str = \"abcd\";\n" " let v: []u8 = s: []u8;\n" " return v.cap: i32;\n" "};\n", 4, K_RUN, NULL }, /* #28 (#263 both-stages): tuple slice-element read `t.0` over a * ([]u8, i64). A clobber() call between the tuple build and the * read leaves junk in BX/CX; pre-fix the str-only arm fell to the * scalar tail (ptr word only), so ys.len read stale BX. Reads len * == 5 (distinct from any stale value) → 0. */ { "tuple_slice_elem", "package main;\n" "fn clobber() i64 = {\n" " let a: i64 = 111;\n" " let b: i64 = 222;\n" " return a + b;\n" "};\n" "export fn main() i32 = {\n" " let xs: []u8 = [1u8, 2u8, 3u8, 4u8, 5u8];\n" " let t: ([]u8, i64) = (xs, 7);\n" " let junk: i64 = clobber();\n" " let ys: []u8 = t.0;\n" " if (ys.len == 5) { return 0; };\n" " return 1;\n" "};\n", 0, K_RUN, NULL }, /* #28 sibling: the slice element sits at tuple position 1 (foff != 0: * an i64 occupies slot 0, the []u8 header starts at +8). Exercises the * widened arm's offset arithmetic, which the position-0 row leaves * untested. Same clobber()-between-build-and-read trigger; len == 6. */ { "tuple_slice_elem_pos1", "package main;\n" "fn clobber() i64 = {\n" " let a: i64 = 111;\n" " let b: i64 = 222;\n" " return a + b;\n" "};\n" "export fn main() i32 = {\n" " let xs: []u8 = [1u8, 2u8, 3u8, 4u8, 5u8, 6u8];\n" " let t: (i64, []u8) = (9, xs);\n" " let junk: i64 = clobber();\n" " let ys: []u8 = t.1;\n" " if (ys.len == 6) { return 0; };\n" " return 1;\n" "};\n", 0, K_RUN, NULL }, /* #29 (#263 both-stages): chained-dot str leaf `o.i.s` (depth-2) * dropped the cap word — a junk strlit before the chain left CX * stale, so t.cap read 2 (the junk's cap) instead of 5. Now the * str leaf loads all three header words like the slice arm. */ { "chained_str_cap", "package main;\n" "type inner = struct { s: str, x: i64 };\n" "type outer = struct { i: inner, y: i64 };\n" "export fn main() i32 = {\n" " let iv: inner = inner { s = \"hello\", x = 0 };\n" " let o: outer = outer { i = iv, y = 0 };\n" " let junk: str = \"ab\";\n" " let t: str = o.i.s;\n" " return t.cap: i32;\n" "};\n", 5, K_RUN, NULL }, /* #29 sibling: the str leaf sits at a NON-ZERO field offset within the * inner struct (a leading i64 pad pushes `s` to +8, so totaloff != 0). * Exercises the chained-leaf offset arithmetic the offset-0 row leaves * untested. Junk strlit before the chain; cap == len == 7. */ { "chained_str_cap_offset", "package main;\n" "type inner = struct { pad: i64, s: str };\n" "type outer = struct { i: inner, y: i64 };\n" "export fn main() i32 = {\n" " let iv: inner = inner { pad = 0, s = \"worldly\" };\n" " let o: outer = outer { i = iv, y = 0 };\n" " let junk: str = \"ab\";\n" " let t: str = o.i.s;\n" " return t.cap: i32;\n" "};\n", 7, K_RUN, NULL }, }; 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/f6h_%d_%d.ww", getpid(), i); snprintf(tmpdir, sizeof tmpdir, "/tmp/f6h_%d_d_%d", getpid(), i); snprintf(errf, sizeof errf, "/tmp/f6h_%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 (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/f6h_asm_%d_%d.ww", getpid(), i); snprintf(cs, sizeof cs, "/tmp/f6h_asm_%d_%d_c.s", getpid(), i); snprintf(ws, sizeof ws, "/tmp/f6h_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, "f6_header: %d/%d checks failed\n", fail, total); return 1; } printf("f6_header: %d/%d ok\n", total, total); return 0; }