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

@@ -280,6 +280,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_sret_struct_return \
$(BIN)/test_sret_struct_return_run \
$(BIN)/test_tagged_sret_run \
$(BIN)/test_tagscr_sizes_run \
$(BIN)/test_global_sret_run \
$(BIN)/test_sret_narrow_field \
$(BIN)/test_sret_narrow_field_run \
@@ -1171,6 +1172,12 @@ $(BIN)/test_tagged_sret_run: test/wcc/926_tagged_sret_run.c \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_tagscr_sizes_run: test/wcc/926_tagscr_sizes_run.c \
$(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_global_sret_run: test/wcc/940_global_sret_run.c \
$(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \

View File

@@ -71,22 +71,31 @@ static int cg_aggargscr_sz;
* Two cached slots match wwstage's `@`-prefix namespace exactly:
* cg_tagbase — 8B base-register spill for cg_widen_tagged_store
* via_outer (mirrors wwstage @tagbase, 1 site).
* cg_tagscr — sized scratch shared across THREE sites: cg_widen_
* @tagscr<sz> — sized scratch shared across THREE sites: cg_widen_
* tagged_store via_outer write target, cg_widen_tagged_
* push struct/tagged-source widen, N_INDEX tagged-element
* assign. Mirrors wwstage @tagscr — wwstage shares the
* slot via localadd `@`-prefix dedup against c.atlocals.
* assign. Mirrors wwstage @tagscr<sz> — wwstage shares
* the slot via localadd `@`-prefix dedup against
* c.atlocals.
*
* Both stages now size at first use and fatal() if a later site asks
* for more (rule 7: surface, don't silently corrupt the frame —
* pinned offset can't grow in place once neighbours are allocated).
* Both stages size at first use (per name). Pre-#44 the tagged scratch
* was a SINGLE slot and a later site asking for a larger size fatal'd
* (rule 7 — pinned offset can't grow in place once neighbours are
* allocated); a fn mixing two tagged slot sizes smaller-first (regex
* compile(): 56B append-element widen then 64B sret return) was
* uncompilable. #44 keys the scratch by slot size — one cached slot
* per distinct size, allocated in first-use order in BOTH stages, so
* the grow-fatal is unreachable for @tagscr by construction. All
* three sites funnel through cg_tagscr_slot (no other alloc path).
* Per-fn convergence completed by #15 (#26c follow-up): wwstage
* dropped its scanlocals pre-pass and aligned DOWN to cstage's
* first-use shape. _sz tracks cached allocation size. */
* first-use shape. */
static int cg_tagbase;
static int cg_tagbase_sz;
static int cg_tagscr;
static int cg_tagscr_sz;
enum { CG_NTAGSCR = 16 };
static int cg_tagscr_off[CG_NTAGSCR];
static int cg_tagscr_sz[CG_NTAGSCR];
static int cg_ntagscr;
/* #34: per-fn @appendscr — 8B dst-pointer spill for the append()
* struct-literal element fill (cg_structlit_fill DST_PTR_LOCAL needs
* a BP-rooted slot to reload BX from across its internal cgexprs).
@@ -1687,6 +1696,26 @@ localfind(Local *head, const char *name)
return 0; /* 0 = not found (caller must verify) */
}
/* cg_tagscr_slot — the ONLY alloc path for the per-fn tagged scratch
* (#44). One cached slot per distinct slot size, named "@tagscr<sz>"
* so wwstage's localadd name-dedup keys the same way; first-use
* allocation order is the source order in both stages (byte-id). */
static int
cg_tagscr_slot(Cg *c, Local **locals_p, int sz)
{
for (int i = 0; i < cg_ntagscr; i++)
if (cg_tagscr_sz[i] == sz)
return cg_tagscr_off[i];
if (cg_ntagscr >= CG_NTAGSCR)
fatal("cg_tagscr_slot: more than %d distinct tagged "
"scratch sizes in one fn", CG_NTAGSCR);
cg_tagscr_off[cg_ntagscr] = local_alloc(c, locals_p,
aprintf(c->a, "@tagscr%d", sz), sz, cg_frame);
cg_tagscr_sz[cg_ntagscr] = sz;
cg_ntagscr++;
return cg_tagscr_off[cg_ntagscr - 1];
}
/* cg_base_cap — load the capacity of a sub-slice's UNDERLYING storage
* into `dst` for the #20 cap = base_cap - lo formula (drew: harec
* eval.c:1017 slice cap-=start / eval.c:1024 array cap=length-start;
@@ -2080,19 +2109,7 @@ cg_widen_tagged_store(Cg *c, Local **locals_p, Type *dst, Node *src,
cg_tagbase_sz = 8;
}
ins2(c, A_MOVQ, areg(base_reg), amem(D_BP, base_spill));
if (cg_tagscr != 0) {
if (sz > cg_tagscr_sz)
fatal("cg_widen_tagged_store: @tagscr "
"cached sz %d, need %d (pinned offset "
"can't grow in place; rule 7 — #15/#26c)",
cg_tagscr_sz, sz);
write_off = cg_tagscr;
} else {
write_off = local_alloc(c, locals_p, "@tagscr", sz,
cg_frame);
cg_tagscr = write_off;
cg_tagscr_sz = sz;
}
write_off = cg_tagscr_slot(c, locals_p, sz);
/* Pre-zero so str/scalar branches (which leave high words
* untouched when sz exceeds the variant's footprint) still
* deliver a clean slot to the copy-out. */
@@ -2536,19 +2553,7 @@ cg_widen_tagged_push(Cg *c, Local **locals_p, Type *dst, Node *src, int sz)
ins1(c, A_PUSHQ, areg(D_AX)); /* tag at +0 */
return;
}
int scr;
if (cg_tagscr != 0) {
if (sz > cg_tagscr_sz)
fatal("cg_widen_tagged_push: @tagscr cached sz %d, "
"need %d (pinned offset can't grow in place; "
"rule 7 — #15/#26c)",
cg_tagscr_sz, sz);
scr = cg_tagscr;
} else {
scr = local_alloc(c, locals_p, "@tagscr", sz, cg_frame);
cg_tagscr = scr;
cg_tagscr_sz = sz;
}
int scr = cg_tagscr_slot(c, locals_p, sz);
/* Zero the scratch slot first so any pad word the store path
* leaves untouched (struct payload shorter than the slot's value
* area) reads as 0 on the callee. The store path then writes the
@@ -5238,22 +5243,7 @@ cgexpr(Cg *c, Node *n, Local *locals)
* the function frame; no cleanup needed. */
if ((is_arr || is_sl || is_ptr) && elem_tagged) {
int ssz = esz;
int scr;
if (cg_tagscr != 0) {
if (ssz > cg_tagscr_sz)
fatal("N_INDEX tagged: "
"@tagscr cached sz %d, "
"need %d (pinned offset "
"can't grow in place; "
"rule 7 — #15/#26c)",
cg_tagscr_sz, ssz);
scr = cg_tagscr;
} else {
scr = local_alloc(c, &locals,
"@tagscr", ssz, cg_frame);
cg_tagscr = scr;
cg_tagscr_sz = ssz;
}
int scr = cg_tagscr_slot(c, &locals, ssz);
ins2(c, A_XORQ, areg(D_AX), areg(D_AX));
for (int k = 0; k < ssz; k += 8)
ins2(c, A_MOVQ, areg(D_AX),
@@ -11242,8 +11232,7 @@ cgfn(Cg *c, FILE *out, Node *fn)
cg_aggargscr_sz = 0;
cg_tagbase = 0;
cg_tagbase_sz = 0;
cg_tagscr = 0;
cg_tagscr_sz = 0;
cg_ntagscr = 0;
cg_appendscr = 0;
cg_sret_arg_off = 0;
cg_sret_dest_off = 0;

View File

@@ -15898,7 +15898,7 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
let pname: str = rhsstructpayload(c, arg);
if (pname.len > 0) {
let ptype: *node = param.lhs;
let scroff: i32 = localadd(c, "@tagscr", widensz, nil);
let scroff: i32 = tagscradd(c, widensz);
emitline("\tXORQ\tAX, AX\n");
let zz: i32 = 0;
for (zz < widensz) {
@@ -18523,11 +18523,10 @@ fn cgwidentaggedstore(c: *cgen, dst: *tinfo, src: *node,
emitline(", ");
emitoff(bspill: i64);
emitline("(BP)\n");
// Shared scratch sized at first use per #15/#26c. A sibling
// site (cgreturn, pushargsrev, cgindex) hitting @tagscr later
// with a larger size fatals (rule 7) — pinned offset can't
// grow in place.
let scr: i32 = localadd(c, "@tagscr", slot_sz, nil);
// Shared per-size scratch sized at first use (#15/#26c, size-keyed
// by #44): sibling sites (cgreturn, pushargsrev, cgindex) hitting
// the same slot size reuse the slot; a different size pins its own.
let scr: i32 = tagscradd(c, slot_sz);
emitline("\tXORQ\tAX, AX\n");
let z: i32 = 0;
for (z < slot_sz) {
@@ -25539,13 +25538,13 @@ fn cgassign(c: *cgen, n: *node) void = {
// scratch slot via cgwidentaggedstore (handles struct /
// str / scalar / subset / nullable variants uniformly),
// then compute &arr[i] and byte-copy. The scratch
// (@tagscr) is reused across all tagged-arr stores in
// the function; first-use sizes the slot (#15/#26c).
// (@tagscr<sz>) is reused across all same-size tagged-arr
// stores in the function; first-use sizes the slot
// (#15/#26c, size-keyed by #44).
if (elemtn != nil) {
if (istaggedtype(c, elemtn)) {
let slot_sz: i32 = slotsize(c, elemtn);
let scroff: i32 = localadd(c, "@tagscr",
slot_sz, nil);
let scroff: i32 = tagscradd(c, slot_sz);
// Pre-zero scratch (matches push helper).
emitline("\tXORQ\tAX, AX\n");
let zz: i32 = 0;
@@ -33243,6 +33242,39 @@ fn localadd(c: *cgen, name: str, sz: i32, tnode: *node) i32 = {
return localalloc(c, name, sz, tnode);
};
// tagscradd — the ONLY alloc path for the per-fn tagged scratch (#44).
// "@tagscr<sz>" keys localadd's @-prefix name-dedup by slot size, so a
// fn mixing two tagged slot sizes smaller-first (regex compile(): 56B
// append-element widen then 64B sret return) no longer trips the
// #15/#26c grow-fatal — each distinct size pins its own first-use
// slot, in source order in BOTH stages (byte-id). Mirrors cstage
// cg_tagscr_slot (cmd/w6c/cgen.c).
fn tagscradd(c: *cgen, sz: i32) i32 = {
let buf: [32]u8;
let pre: str = "@tagscr";
let i: i32 = 0;
for (i < pre.len) {
buf[i] = pre[i];
i += 1;
};
let ns: str = strconv.i64tos(sz: i64, strconv.base.DEC);
let n: i32 = ns.len;
let k: i32 = 0;
for (k < n) { buf[i + k] = ns.ptr[k]; k += 1; };
let total: i32 = i + n;
let p: []u8 = alloc([], (total: u64) + 1u64)!;
let j: i32 = 0;
for (j < total) {
p[j] = buf[j];
j += 1;
};
p[total] = 0u8;
let name: str;
name.ptr = p.ptr;
name.len = total;
return localadd(c, name, sz, nil);
};
fn localfindnode(c: *cgen, name: str) *local = {
let l: *local = c.locals;
for (l != nil) {

View File

@@ -647,6 +647,39 @@ fn localadd(c: *cgen, name: str, sz: i32, tnode: *node) i32 = {
return localalloc(c, name, sz, tnode);
};
// tagscradd — the ONLY alloc path for the per-fn tagged scratch (#44).
// "@tagscr<sz>" keys localadd's @-prefix name-dedup by slot size, so a
// fn mixing two tagged slot sizes smaller-first (regex compile(): 56B
// append-element widen then 64B sret return) no longer trips the
// #15/#26c grow-fatal — each distinct size pins its own first-use
// slot, in source order in BOTH stages (byte-id). Mirrors cstage
// cg_tagscr_slot (cmd/w6c/cgen.c).
fn tagscradd(c: *cgen, sz: i32) i32 = {
let buf: [32]u8;
let pre: str = "@tagscr";
let i: i32 = 0;
for (i < pre.len) {
buf[i] = pre[i];
i += 1;
};
let ns: str = strconv.i64tos(sz: i64, strconv.base.DEC);
let n: i32 = ns.len;
let k: i32 = 0;
for (k < n) { buf[i + k] = ns.ptr[k]; k += 1; };
let total: i32 = i + n;
let p: []u8 = alloc([], (total: u64) + 1u64)!;
let j: i32 = 0;
for (j < total) {
p[j] = buf[j];
j += 1;
};
p[total] = 0u8;
let name: str;
name.ptr = p.ptr;
name.len = total;
return localadd(c, name, sz, nil);
};
fn localfindnode(c: *cgen, name: str) *local = {
let l: *local = c.locals;
for (l != nil) {

View File

@@ -5679,13 +5679,13 @@ fn cgassign(c: *cgen, n: *node) void = {
// scratch slot via cgwidentaggedstore (handles struct /
// str / scalar / subset / nullable variants uniformly),
// then compute &arr[i] and byte-copy. The scratch
// (@tagscr) is reused across all tagged-arr stores in
// the function; first-use sizes the slot (#15/#26c).
// (@tagscr<sz>) is reused across all same-size tagged-arr
// stores in the function; first-use sizes the slot
// (#15/#26c, size-keyed by #44).
if (elemtn != nil) {
if (istaggedtype(c, elemtn)) {
let slot_sz: i32 = slotsize(c, elemtn);
let scroff: i32 = localadd(c, "@tagscr",
slot_sz, nil);
let scroff: i32 = tagscradd(c, slot_sz);
// Pre-zero scratch (matches push helper).
emitline("\tXORQ\tAX, AX\n");
let zz: i32 = 0;

View File

@@ -206,7 +206,7 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
let pname: str = rhsstructpayload(c, arg);
if (pname.len > 0) {
let ptype: *node = param.lhs;
let scroff: i32 = localadd(c, "@tagscr", widensz, nil);
let scroff: i32 = tagscradd(c, widensz);
emitline("\tXORQ\tAX, AX\n");
let zz: i32 = 0;
for (zz < widensz) {
@@ -2831,11 +2831,10 @@ fn cgwidentaggedstore(c: *cgen, dst: *tinfo, src: *node,
emitline(", ");
emitoff(bspill: i64);
emitline("(BP)\n");
// Shared scratch sized at first use per #15/#26c. A sibling
// site (cgreturn, pushargsrev, cgindex) hitting @tagscr later
// with a larger size fatals (rule 7) — pinned offset can't
// grow in place.
let scr: i32 = localadd(c, "@tagscr", slot_sz, nil);
// Shared per-size scratch sized at first use (#15/#26c, size-keyed
// by #44): sibling sites (cgreturn, pushargsrev, cgindex) hitting
// the same slot size reuse the slot; a different size pins its own.
let scr: i32 = tagscradd(c, slot_sz);
emitline("\tXORQ\tAX, AX\n");
let z: i32 = 0;
for (z < slot_sz) {

View File

@@ -15898,7 +15898,7 @@ fn pushargsrev(c: *cgen, arg: *node, param: *node) i32 = {
let pname: str = rhsstructpayload(c, arg);
if (pname.len > 0) {
let ptype: *node = param.lhs;
let scroff: i32 = localadd(c, "@tagscr", widensz, nil);
let scroff: i32 = tagscradd(c, widensz);
emitline("\tXORQ\tAX, AX\n");
let zz: i32 = 0;
for (zz < widensz) {
@@ -18523,11 +18523,10 @@ fn cgwidentaggedstore(c: *cgen, dst: *tinfo, src: *node,
emitline(", ");
emitoff(bspill: i64);
emitline("(BP)\n");
// Shared scratch sized at first use per #15/#26c. A sibling
// site (cgreturn, pushargsrev, cgindex) hitting @tagscr later
// with a larger size fatals (rule 7) — pinned offset can't
// grow in place.
let scr: i32 = localadd(c, "@tagscr", slot_sz, nil);
// Shared per-size scratch sized at first use (#15/#26c, size-keyed
// by #44): sibling sites (cgreturn, pushargsrev, cgindex) hitting
// the same slot size reuse the slot; a different size pins its own.
let scr: i32 = tagscradd(c, slot_sz);
emitline("\tXORQ\tAX, AX\n");
let z: i32 = 0;
for (z < slot_sz) {
@@ -25539,13 +25538,13 @@ fn cgassign(c: *cgen, n: *node) void = {
// scratch slot via cgwidentaggedstore (handles struct /
// str / scalar / subset / nullable variants uniformly),
// then compute &arr[i] and byte-copy. The scratch
// (@tagscr) is reused across all tagged-arr stores in
// the function; first-use sizes the slot (#15/#26c).
// (@tagscr<sz>) is reused across all same-size tagged-arr
// stores in the function; first-use sizes the slot
// (#15/#26c, size-keyed by #44).
if (elemtn != nil) {
if (istaggedtype(c, elemtn)) {
let slot_sz: i32 = slotsize(c, elemtn);
let scroff: i32 = localadd(c, "@tagscr",
slot_sz, nil);
let scroff: i32 = tagscradd(c, slot_sz);
// Pre-zero scratch (matches push helper).
emitline("\tXORQ\tAX, AX\n");
let zz: i32 = 0;
@@ -33243,6 +33242,39 @@ fn localadd(c: *cgen, name: str, sz: i32, tnode: *node) i32 = {
return localalloc(c, name, sz, tnode);
};
// tagscradd — the ONLY alloc path for the per-fn tagged scratch (#44).
// "@tagscr<sz>" keys localadd's @-prefix name-dedup by slot size, so a
// fn mixing two tagged slot sizes smaller-first (regex compile(): 56B
// append-element widen then 64B sret return) no longer trips the
// #15/#26c grow-fatal — each distinct size pins its own first-use
// slot, in source order in BOTH stages (byte-id). Mirrors cstage
// cg_tagscr_slot (cmd/w6c/cgen.c).
fn tagscradd(c: *cgen, sz: i32) i32 = {
let buf: [32]u8;
let pre: str = "@tagscr";
let i: i32 = 0;
for (i < pre.len) {
buf[i] = pre[i];
i += 1;
};
let ns: str = strconv.i64tos(sz: i64, strconv.base.DEC);
let n: i32 = ns.len;
let k: i32 = 0;
for (k < n) { buf[i + k] = ns.ptr[k]; k += 1; };
let total: i32 = i + n;
let p: []u8 = alloc([], (total: u64) + 1u64)!;
let j: i32 = 0;
for (j < total) {
p[j] = buf[j];
j += 1;
};
p[total] = 0u8;
let name: str;
name.ptr = p.ptr;
name.len = total;
return localadd(c, name, sz, nil);
};
fn localfindnode(c: *cgen, name: str) *local = {
let l: *local = c.locals;
for (l != nil) {

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;
}