w6c+w6c_ww: size-keyed @tagscr — one tagged scratch per slot size (fix #44)

A fn mixing two tagged slot sizes smaller-first (regex compile(): 56B
append-element widen then 64B sret return) hit the #15/#26c rule-7
grow-fatal — the single shared per-fn @tagscr is first-use-sized and
its pinned offset can't grow. Key the scratch by slot size instead:
@tagscr<sz>, one first-use-allocated slot per distinct size, all three
sites (widen-store via_outer, widen-push, N_INDEX tagged-element
assign) funnelled through cg_tagscr_slot / tagscradd in both stages.
Single-size fns emit byte-identical asm to pre-fix (control row pinned
+ hand-cmp'd vs master w6c). 736's tagscr_size_grow_fatal fixture
pinned the now-unreachable fatal; converted to a byte-id succ row.
Runtime rows live in 926_tagscr_sizes_run.
This commit is contained in:
2026-06-04 04:34:54 +09:00
parent 8999b59ab0
commit c2308a11c7
9 changed files with 483 additions and 176 deletions

View File

@@ -20,18 +20,15 @@
* cstage UP to a scanlocals pre-pass) is filed as #26c — separate
* concern, not in #26's scope.
*
* Two row families:
* succ_rows — pin cstage vs wwstage asm `cmp -s` byte-id on the
* canonical pointer-rooted two-tagged-store shape.
* Pre-#26 cstage's frame was 16B larger (extra @tagbase
* + @tagscr per call); post-#26 they match.
* fail_rows — pre-locate the Class A landmine: a fn that needs the
* @tagscr slot to grow within one cgfn. Cstage must
* fatal() with the expected pattern (rule 7: surface,
* don't silently corrupt the frame). Tests that the
* next stdlib commit landing into this shape gets a
* loud failure rather than a silent miscompile.
*/
* succ_rows pin cstage vs wwstage asm `cmp -s` byte-id on the
* canonical pointer-rooted two-tagged-store shapes. Pre-#26 cstage's
* frame was 16B larger (extra @tagbase + @tagscr per call); post-#26
* they match. The old fail_rows family pre-located the Class A
* landmine (a fn needing the single @tagscr slot to GROW within one
* cgfn → rule-7 fatal); #44 size-keys the scratch (@tagscr<sz>, one
* slot per distinct slot size) so that shape now compiles — it moved
* into succ_rows, and the dedicated runtime rows live in
* 926_tagscr_sizes_run. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -69,27 +66,13 @@ static const struct row succ_rows[] = {
" c.v = (q: bag);\n"
" return 0i64;\n"
"};\n" },
};
struct failrow {
const char *label;
const char *src;
const char *want_pattern; /* substring expected in cstage stderr */
};
static const struct failrow fail_rows[] = {
/* Pre-located landmine: two pointer-rooted tagged stores into
* differently-sized union slots within ONE fn. First store pins
* cg_tagscr_sz to the smaller slot; second store needs a larger
* slot than cached → fatal() with the size-grow pattern. Wwstage
* handles this via scanlocals pre-pass (c.tagscrsz = max). Cstage
* has no pre-pass — fail-loud per rule 7 keeps the next stdlib
* commit hitting this shape from silently corrupting the frame.
*
* Slot sizes: (i64 | i32) → 16B (tag + max(8,4) → 8+8). (i64 | str)
* → 24B (tag + max(8, str=16) → 8+16). First write 16B, second
* 24B; cstage must fatal on the second call. */
{ "tagscr_size_grow_fatal",
/* Two pointer-rooted tagged stores into DIFFERENTLY-sized union
* slots within one fn. Pre-#44 this was the fail_rows landmine:
* the single first-use-sized @tagscr couldn't grow → cstage
* fatal "@tagscr cached sz". #44 keys the scratch by slot size
* (@tagscr<sz>), so each store pins its own slot and the shape
* compiles byte-id on both stages. */
{ "tagscr_two_sizes",
"type small = (i64 | i32);\n"
"type big = (i64 | str);\n"
"type cell_a = struct { v: small };\n"
@@ -98,8 +81,7 @@ static const struct failrow fail_rows[] = {
" a.v = (x: small);\n"
" b.v = (s: big);\n"
" return 0i64;\n"
"};\n",
"@tagscr cached sz" },
"};\n" },
};
static int
@@ -121,39 +103,6 @@ emit_s(const char *w6c, const char *src_text, int i, char *out_s, size_t cap)
return rc;
}
/* Run w6c with stderr captured to a tmp file. Returns rc and writes
* stderr path into err_out. Caller unlinks. */
static int
emit_capture_stderr(const char *w6c, const char *src_text, int i,
char *err_out, size_t cap)
{
char src[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/lbl_ssot_fail_%d_%d.ww", getpid(), i);
snprintf(err_out, cap, "/tmp/lbl_ssot_fail_%d_%d.err", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(src_text, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s -o /dev/null %s 2>%s", w6c, src, err_out);
int rc = runwait(cmd);
unlink(src);
return rc;
}
static int
stderr_contains(const char *path, const char *needle)
{
FILE *f = fopen(path, "rb");
if (!f) return 0;
char buf[4096];
size_t n = fread(buf, 1, sizeof buf - 1, f);
fclose(f);
buf[n] = '\0';
return strstr(buf, needle) != NULL;
}
int
main(void)
{
@@ -215,31 +164,6 @@ main(void)
unlink(cs_path); unlink(ws_path);
}
int nfail = (int)(sizeof fail_rows / sizeof fail_rows[0]);
for (int i = 0; i < nfail; i++) {
char err_path[128];
int rc = emit_capture_stderr(w6c, fail_rows[i].src, i,
err_path, sizeof err_path);
total++;
if (rc == 0) {
fprintf(stderr,
"cstage_label_ssot[fail][%s]: expected cstage "
"fatal, got success exit\n", fail_rows[i].label);
fail++;
unlink(err_path);
continue;
}
total++;
if (!stderr_contains(err_path, fail_rows[i].want_pattern)) {
fprintf(stderr,
"cstage_label_ssot[fail][%s]: stderr missing "
"pattern \"%s\"\n",
fail_rows[i].label, fail_rows[i].want_pattern);
fail++;
}
unlink(err_path);
}
if (fail) {
fprintf(stderr,
"cstage_label_ssot: %d/%d fixtures failed\n", fail, total);

View File

@@ -0,0 +1,291 @@
/*
* 926_tagscr_sizes_run — size-keyed per-fn tagged scratch (#44): a fn
* that mixes two tagged slot sizes gets one @tagscr<sz> slot per
* distinct size instead of one shared first-use-sized slot.
*
* Pre-#44 the single shared slot was sized at first use and a later
* LARGER ask hit the rule-7 fatal ("@tagscr cached sz %d, need %d" /
* wwstage "localadd: @-prefix slot grew within fn") — so any fn that
* first widened a smaller tagged (56B append-element fill, #34) and
* then needed a larger one (64B sret return, #38) was uncompilable.
* That is exactly regex compile()'s shape (append(insts, v) then
* `return regex{...}`), the fold-2a blocker.
*
* Rows pin: smaller-then-larger (the blocker), larger-then-smaller
* (pre-#44 this silently REUSED the bigger slot; now each size pins
* its own), same-size-twice (one slot, reused), the single-size
* control (asm must stay byte-identical — also hand-checked against
* the pre-#44 compiler when the fix landed), and the N_INDEX
* tagged-element store site mixing with an sret return.
*
* Per row: w6c vs w6c_ww byte-id (rule 10) + runtime via both the ww
* and ww_ww drivers. Payload checks stay within the 32B register
* cursor — wide tagged ELEMENT reads past 32B truncate (the filed #43
* residual, pre-existing, out of #44's scope).
*/
#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;
};
/* 48B payload -> 56B tagged slot (the regex inst-union shape); 56B
* payload -> 64B slot (the compile()-return shape). */
#define SIZE_TYPES \
"type oops = !str;\n" \
"type p48 = struct { a: i64, b: i64, c: i64, d: i64, e: i64, f: i64 };\n" \
"type small = (p48 | bool);\n" \
"type p56 = struct { a: i64, b: i64, c: i64, d: i64, e: i64, f: i64, g: i64 };\n"
static const struct row rows[] = {
/* The fold-2a blocker: 56B append-element widen, then a 64B
* tagged sret return in the SAME fn. Pre-#44: loud fatal. */
{ "small_then_large",
SIZE_TYPES
"fn f() (p56 | oops | nomem) = {\n"
" let xs: []small;\n"
" let b: bool = true;\n"
" let v: small = b;\n"
" append(xs, v);\n"
" return p56 { a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7 };\n"
"};\n"
"export fn main() i32 = {\n"
" match (f()) {\n"
" case let w: p56 => { if (w.g != 7) { return 1; }; };\n"
" case => return 2;\n"
" };\n"
" return 0;\n"
"};\n",
0 },
/* Reverse order via an early conditional return: 64B sret
* first, 56B append second. Pre-#44 this passed by silently
* REUSING the 64B slot for the 56B widen; now the 56B widen
* pins its own slot — runtime must stay correct. */
{ "large_then_small",
SIZE_TYPES
"fn f(flag: bool) (p56 | oops | nomem) = {\n"
" if (flag) {\n"
" return p56 { a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 9 };\n"
" };\n"
" let xs: []small;\n"
" let b: bool = true;\n"
" let v: small = b;\n"
" append(xs, v);\n"
" let e: small = xs[0];\n"
" if (!(e is bool)) {\n"
" return \"bad\": oops;\n"
" };\n"
" return p56 { a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7 };\n"
"};\n"
"export fn main() i32 = {\n"
" match (f(false)) {\n"
" case let w: p56 => { if (w.g != 7) { return 1; }; };\n"
" case => return 2;\n"
" };\n"
" match (f(true)) {\n"
" case let w: p56 => { if (w.g != 9) { return 3; }; };\n"
" case => return 4;\n"
" };\n"
" return 0;\n"
"};\n",
0 },
/* Two 56B-slot widens in one fn share ONE slot; frame canaries
* around the appends pin against slot/neighbour overlap. */
{ "same_size_twice",
SIZE_TYPES
"type other = (p48 | i64);\n"
"export fn main() i32 = {\n"
" let canary1: i64 = 111;\n"
" let xs: []small;\n"
" let ys: []other;\n"
" let b: bool = true;\n"
" let v: small = b;\n"
" append(xs, v);\n"
" let canary2: i64 = 222;\n"
" let n: i64 = 7;\n"
" let w: other = n;\n"
" append(ys, w);\n"
" if (canary1 != 111) { return 11; };\n"
" if (canary2 != 222) { return 12; };\n"
" let e: small = xs[0];\n"
" if (!(e is bool)) { return 1; };\n"
" let e2: other = ys[0];\n"
" match (e2) {\n"
" case let q: i64 => { if (q != 7) { return 2; }; };\n"
" case => return 3;\n"
" };\n"
" return 0;\n"
"};\n",
0 },
/* Single-size control: one widen, one slot — the common case
* whose asm the size-keying must not perturb. */
{ "single_size_control",
SIZE_TYPES
"export fn main() i32 = {\n"
" let xs: []small;\n"
" let b: bool = true;\n"
" let v: small = b;\n"
" append(xs, v);\n"
" let e: small = xs[0];\n"
" if (!(e is bool)) { return 1; };\n"
" return 0;\n"
"};\n",
0 },
/* The third @tagscr site: N_INDEX tagged-element STORE (56B)
* mixed with the 64B sret return. */
{ "idx_store_then_sret",
SIZE_TYPES
"fn f() (p56 | oops | nomem) = {\n"
" let arr: [2]small;\n"
" let b: bool = true;\n"
" let v: small = b;\n"
" arr[1] = v;\n"
" let e: small = arr[1];\n"
" if (!(e is bool)) {\n"
" return \"bad\": oops;\n"
" };\n"
" return p56 { a = 1, b = 2, c = 3, d = 4, e = 5, f = 6, g = 7 };\n"
"};\n"
"export fn main() i32 = {\n"
" match (f()) {\n"
" case let w: p56 => { if (w.g != 7) { return 1; }; };\n"
" case => return 2;\n"
" };\n"
" return 0;\n"
"};\n",
0 },
};
static const char *g_bin;
static int
compile_s(const char *tool, const char *src, const char *outpath)
{
char cmd[1024];
snprintf(cmd, sizeof cmd, "%s/%s %s > %s 2>&1",
g_bin, tool, src, outpath);
return runwait(cmd);
}
static int
file_eq(const char *a, const char *b)
{
char cmd[1024];
snprintf(cmd, sizeof cmd, "cmp -s %s %s", a, b);
return runwait(cmd) == 0;
}
static int
run_driver(const char *driver, const char *src, const char *label)
{
char tmpdir[128], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/tagscr_%d_d", getpid());
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s/%s build %s >/dev/null 2>&1",
tmpdir, g_bin, driver, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
label, driver);
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(outbin);
rmdir(tmpdir);
return got;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
static char absbin[512];
if (bin[0] != '/') {
char cwd[256];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
g_bin = bin;
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
const struct row *r = &rows[i];
char src[128], cs_s[128], ww_s[128];
snprintf(src, sizeof src, "/tmp/tagscr_%d_%d.ww",
getpid(), i);
snprintf(cs_s, sizeof cs_s, "/tmp/tagscr_%d_%d_cs.s",
getpid(), i);
snprintf(ww_s, sizeof ww_s, "/tmp/tagscr_%d_%d_ww.s",
getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return 1;
fputs("package main;\n\n", f);
fputs(r->src, f);
fclose(f);
total++;
int cs_rc = compile_s("w6c", src, cs_s);
int ww_rc = compile_s("w6c_ww", src, ww_s);
if (cs_rc != 0 || ww_rc != 0) {
fprintf(stderr, "FAIL row[%s]: compile rc cs=%d "
"ww=%d\n", r->label, cs_rc, ww_rc);
fail++;
unlink(src); unlink(cs_s); unlink(ww_s);
continue;
}
if (!file_eq(cs_s, ww_s)) {
fprintf(stderr, "FAIL row[%s]: cs != ww .s\n",
r->label);
fail++;
}
int got_cs = run_driver("ww", src, r->label);
if (got_cs != r->want) {
fprintf(stderr, "FAIL row[%s] cstage: want %d "
"got %d\n", r->label, r->want, got_cs);
fail++;
}
char wwdrv[600];
snprintf(wwdrv, sizeof wwdrv, "%s/ww_ww", g_bin);
if (access(wwdrv, X_OK) == 0) {
int got_ww = run_driver("ww_ww", src, r->label);
if (got_ww != r->want) {
fprintf(stderr, "FAIL row[%s] wwstage: "
"want %d got %d\n",
r->label, r->want, got_ww);
fail++;
}
}
unlink(src); unlink(cs_s); unlink(ww_s);
}
if (fail) {
fprintf(stderr, "tagscr_sizes_run: %d/%d rows failed\n",
fail, total);
return 1;
}
printf("tagscr_sizes_run: %d rows ok\n", total);
return 0;
}