wcc/cgen: #116 non-literal tuple source into a tagged box (both-stage)

cg_widen_tagged_store only handled a tuple LITERAL (N_TUPLE / cast-of-
N_TUPLE) widened into a tagged box; any addressable non-literal tuple
source -- IDENT var, INDEX tbl[i], DEREF *p -- hit the `else fatal`
("tuple-typed source shape unwired"). Both stages loud-identical
(honest, no silent miscompile). This blocked indexing a const tuple
table into a union (regex charclass_map[i] -> charset union).

Add an addressable-tuple-source arm, both stages (cgen.c +
cgenutil.ww twin). It resolves the source address via the cgplaceaddr
place-spine (covering ident/index/deref -- one mechanism, so the trio
is family-closed) and block-copies the tuple's type-table ->size bytes
into the box payload (after the 8B tag), then stamps the variant tag.
No re-slotting: a tuple's in-memory layout uses the same eslot strides
(str=24B header, *fn=8B, ...) as the box payload the literal loop
fills, so source-layout == dest-layout. The existing narrow-pack and
tag-unresolved guards stay as the honest boundary; CALL/sret tuple
sources (different receive, #68-kin) stay loud.

align-BOTH: both stages were loud (no runtime reference), and byte-id
is structurally blind to an identical-wrong emission -- so correctness
is proven by a RUNTIME read-back pin (944_nonlit_tuple_widen_run, per
shape: match-extract + assert str header + call the fn-ptr elem with
distinct fns so a stale pointer is caught). 936's old reject row
graduates to a run row. Both stages byte-identical (990-997 green).
This commit is contained in:
2026-06-06 19:27:28 +09:00
parent da30f10f70
commit f8be2ae8dd
7 changed files with 638 additions and 60 deletions

View File

@@ -324,6 +324,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_overcap_tuple_field_store_run \ $(BIN)/test_overcap_tuple_field_store_run \
$(BIN)/test_mixed_scalar_tuple_sret_run \ $(BIN)/test_mixed_scalar_tuple_sret_run \
$(BIN)/test_tuple_in_union_run \ $(BIN)/test_tuple_in_union_run \
$(BIN)/test_nonlit_tuple_widen_run \
$(BIN)/test_tuple_slot_layout_run \ $(BIN)/test_tuple_slot_layout_run \
$(BIN)/test_tagged_tuple_widen_run \ $(BIN)/test_tagged_tuple_widen_run \
$(BIN)/test_tagged_structlit_payload_run \ $(BIN)/test_tagged_structlit_payload_run \
@@ -1616,6 +1617,12 @@ $(BIN)/test_tuple_in_union_run: test/wcc/940_tuple_in_union_run.c \
$(LIB)/libwwrt.a | $(BIN) $(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $< $(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_nonlit_tuple_widen_run: test/wcc/944_nonlit_tuple_widen_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_tuple_slot_layout_run: test/wcc/941_tuple_slot_layout_run.c \ $(BIN)/test_tuple_slot_layout_run: test/wcc/941_tuple_slot_layout_run.c \
$(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \

View File

@@ -2737,14 +2737,9 @@ cg_widen_tagged_store(Cg *c, Local **locals_p, Type *dst, Node *src,
else if (src->kind == N_CAST && src->lhs else if (src->kind == N_CAST && src->lhs
&& src->lhs->kind == N_TUPLE) && src->lhs->kind == N_TUPLE)
tupsrc = src->lhs; tupsrc = src->lhs;
/* #72: any OTHER tuple-typed source (ident, call result, /* #116: a NON-LITERAL tuple-typed source (ident, index,
* match binding) would fall to the scalar arm below and * deref) is no longer loud here — it routes to the
* silently drop payload slot 1+ — loud-stop (rule 7) until * addressable block-copy arm just below the literal arm. */
* the word-copy / cursor-receive arms are wired. */
if (tupsrc == NULL)
fatal("cg_widen_tagged_store: tuple-typed source "
"shape unwired (only the bare/cast tuple literal "
"carries a full payload; see #72)");
} }
if (tupsrc != NULL) { if (tupsrc != NULL) {
int tag = cg_tag_for_variant(du, st); int tag = cg_tag_for_variant(du, st);
@@ -2833,6 +2828,62 @@ cg_widen_tagged_store(Cg *c, Local **locals_p, Type *dst, Node *src,
if (via_outer) goto copy_out; if (via_outer) goto copy_out;
return; return;
} }
/* #116: a NON-LITERAL but ADDRESSABLE tuple source — a tuple IDENT
* var, a slice/array INDEX (tbl[i]), or a DEREF (*p). The tuple in
* memory uses the SAME tuple_eslot strides as the box payload the
* literal loop above fills (a scalar 8B, a slice/str its 24B header,
* a tagged element its boxed tag+payload), so the source-in-memory
* layout already equals the box payload layout — the fill is a flat
* block-copy of sum(tuple_eslot) bytes from the source address into
* write_off+8, no re-slotting (a tagged element rides over as its
* already-built box, so no recursion is needed). Kept loud (out of
* scope): a CALL/sret result (the tuple sits at the sret address,
* #40-kin) and a struct-field / array-literal-element source (loud
* EARLIER at construction, #49 / #270-1c). A cast wrapping a
* concrete-variant tuple (`(tbl[i]: ci)`) survived the widen-cast
* peel above; its operand is the addressable expr. */
if (su && su->kind == TY_TUPLE) {
Node *addrsrc = src;
if (addrsrc->kind == N_CAST && addrsrc->lhs)
addrsrc = addrsrc->lhs;
if (addrsrc->kind != N_IDENT && addrsrc->kind != N_INDEX
&& !(addrsrc->kind == N_UN && addrsrc->op == TK_STAR))
fatal("cg_widen_tagged_store: tuple-typed source shape "
"unwired (only the bare/cast tuple literal and the "
"addressable ident/index/deref trio carry a full "
"payload; see #116)");
int tag = cg_tag_for_variant(du, st);
if (tag < 0)
fatal("cg_widen_tagged_store: tuple-in-union variant "
"tag unresolved (untyped/literal tuple element; "
"see #242 / #241)");
/* The tuple's type-table size IS sum(tuple_eslot) under the
* 8B-slot tuple layout (every walk takes its stride from
* tuple_eslot; the type's size is their sum), so the payload
* byte-count routes through the type table (rule 13) without
* re-walking the elements — and matches the wwstage twin, whose
* tuple tinfo carries no per-element params list. */
int total = (int)su->size;
if (8 + total > sz)
fatal("cg_widen_tagged_store: tuple-in-union payload "
"needs SysV eightbyte packing (narrow elements "
"share an eightbyte; see #242 follow-up)");
if (!cgplaceaddr(c, addrsrc, D_SI, *locals_p))
fatal("cg_widen_tagged_store: addressable tuple source "
"address unresolved (see #116)");
ins2(c, A_XORQ, areg(D_AX), areg(D_AX));
for (int k = 0; k < sz; k += 8)
ins2(c, A_MOVQ, areg(D_AX),
amem(D_BP, write_off + k));
for (int k = 0; k < total; k += 8) {
ins2(c, A_MOVQ, amem(D_SI, k), areg(D_AX));
ins2(c, A_MOVQ, areg(D_AX),
amem(D_BP, write_off + 8 + k));
}
ins2(c, A_MOVQ, aimm(tag), amem(D_BP, write_off + 0));
if (via_outer) goto copy_out;
return;
}
/* Struct payload: zero the whole slot, then write fields/words /* Struct payload: zero the whole slot, then write fields/words
* at slot+8+ — keeping the tag word at slot+0 from the zero-fill, * at slot+8+ — keeping the tag word at slot+0 from the zero-fill,
* then patch it with the variant tag. */ * then patch it with the variant tag. */

View File

@@ -20170,20 +20170,10 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s
}; };
}; };
}; };
// #72: any OTHER tuple-typed source (ident, call result, // #116: a NON-LITERAL tuple-typed source (ident, index,
// match binding) would fall to the scalar arm below and // deref) is no longer loud here — it routes to the
// silently drop payload slot 1+ — loud-stop (rule 7) until // addressable block-copy arm just below the literal arm.
// the word-copy / cursor-receive arms are wired. Mirrors // Mirrors cstage cg_widen_tagged_store.
// cstage cg_widen_tagged_store.
if (tupsrc == nil) {
if (su != nil) {
if (su.kind == tykind.TY_TUPLE) {
let m72: str = "cgwidentaggedstore: tuple-typed source shape unwired (only the bare/cast tuple literal carries a full payload; see #72)\n";
os.write(2, m72.ptr, m72.len: u64);
os.exit(1);
};
};
};
}; };
if (tupsrc != nil) { if (tupsrc != nil) {
if (su != nil) { if (su.kind == tykind.TY_TUPLE) { if (su != nil) { if (su.kind == tykind.TY_TUPLE) {
@@ -20335,6 +20325,83 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s
return; return;
}; }; }; };
}; };
// #116: a NON-LITERAL but ADDRESSABLE tuple source — a tuple
// IDENT var, a slice/array INDEX (tbl[i]), or a DEREF (*p). The
// tuple in memory uses the SAME tupeslot strides as the box
// payload the literal loop above fills, so the source-in-memory
// layout already equals the box payload layout — the fill is a
// flat block-copy of sum(tupeslot) bytes from the source address
// into slot_off+8, no re-slotting (a tagged element rides over
// as its already-built box). Kept loud (out of scope): a
// CALL/sret result (#40-kin) and struct-field / array-literal-
// element sources (loud EARLIER at construction, #49 / #270-1c).
// A cast wrapping a concrete-variant tuple (`(tbl[i]: ci)`)
// survived the widen-cast peel above; its operand is the
// addressable expr. Mirror of cstage cg_widen_tagged_store.
if (su != nil) { if (su.kind == tykind.TY_TUPLE) {
let addrsrc: *node = src;
if (addrsrc.kind == nkind.N_CAST) {
if (addrsrc.lhs != nil) { addrsrc = addrsrc.lhs; };
};
let okkind: bool = false;
if (addrsrc.kind == nkind.N_IDENT) { okkind = true; };
if (addrsrc.kind == nkind.N_INDEX) { okkind = true; };
if (addrsrc.kind == nkind.N_UN) {
if (addrsrc.op == tkind.TK_STAR) { okkind = true; };
};
if (!okkind) {
let mk: str = "cgwidentaggedstore: tuple-typed source shape unwired (only the bare/cast tuple literal and the addressable ident/index/deref trio carry a full payload; see #116)\n";
os.write(2, mk.ptr, mk.len: u64);
os.exit(1);
};
let ntag: i32 = flatvariantidxt(dt, src.type_: *tinfo, false);
if (ntag < 0) {
let mt: str = "cgwidentaggedstore: tuple-in-union variant tag unresolved (untyped/literal tuple element; see #242 / #241)\n";
os.write(2, mt.ptr, mt.len: u64);
os.exit(1);
};
// The tuple's type-table size IS sum(tupeslot) under the 8B-slot
// tuple layout (every walk takes its stride from tupeslot; the
// type's size is their sum), so the payload byte-count routes
// through the type table (rule 13) without re-walking the
// elements — and a wwstage tuple tinfo carries no per-element
// params list anyway. Mirror of cstage cg_widen_tagged_store.
let ntotal: i32 = su.size: i32;
if (8 + ntotal > slot_sz) {
let mp: str = "cgwidentaggedstore: tuple-in-union payload needs SysV eightbyte packing (narrow elements share an eightbyte; see #242 follow-up)\n";
os.write(2, mp.ptr, mp.len: u64);
os.exit(1);
};
if (!cgplaceaddr(c, addrsrc, "SI")) {
let ma: str = "cgwidentaggedstore: addressable tuple source address unresolved (see #116)\n";
os.write(2, ma.ptr, ma.len: u64);
os.exit(1);
};
emitline("\tXORQ\tAX, AX\n");
let nzk: i32 = 0;
for (nzk < slot_sz) {
emitline("\tMOVQ\tAX, ");
emitoff((slot_off + nzk): i64);
emitline("(BP)\n");
nzk += 8;
};
let nck: i32 = 0;
for (nck < ntotal) {
emitline("\tMOVQ\t");
emitoff(nck: i64);
emitline("(SI), AX\n");
emitline("\tMOVQ\tAX, ");
emitoff((slot_off + 8 + nck): i64);
emitline("(BP)\n");
nck += 8;
};
emitline("\tMOVQ\t$");
emitint(ntag: i64);
emitline(", ");
emitoff(slot_off: i64);
emitline("(BP)\n");
return;
}; };
// Struct payload (literal or ident). // Struct payload (literal or ident).
let sname: str = rhsstructpayload(c, src); let sname: str = rhsstructpayload(c, src);
if (sname.len > 0) { if (sname.len > 0) {

View File

@@ -3998,20 +3998,10 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s
}; };
}; };
}; };
// #72: any OTHER tuple-typed source (ident, call result, // #116: a NON-LITERAL tuple-typed source (ident, index,
// match binding) would fall to the scalar arm below and // deref) is no longer loud here — it routes to the
// silently drop payload slot 1+ — loud-stop (rule 7) until // addressable block-copy arm just below the literal arm.
// the word-copy / cursor-receive arms are wired. Mirrors // Mirrors cstage cg_widen_tagged_store.
// cstage cg_widen_tagged_store.
if (tupsrc == nil) {
if (su != nil) {
if (su.kind == tykind.TY_TUPLE) {
let m72: str = "cgwidentaggedstore: tuple-typed source shape unwired (only the bare/cast tuple literal carries a full payload; see #72)\n";
os.write(2, m72.ptr, m72.len: u64);
os.exit(1);
};
};
};
}; };
if (tupsrc != nil) { if (tupsrc != nil) {
if (su != nil) { if (su.kind == tykind.TY_TUPLE) { if (su != nil) { if (su.kind == tykind.TY_TUPLE) {
@@ -4163,6 +4153,83 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s
return; return;
}; }; }; };
}; };
// #116: a NON-LITERAL but ADDRESSABLE tuple source — a tuple
// IDENT var, a slice/array INDEX (tbl[i]), or a DEREF (*p). The
// tuple in memory uses the SAME tupeslot strides as the box
// payload the literal loop above fills, so the source-in-memory
// layout already equals the box payload layout — the fill is a
// flat block-copy of sum(tupeslot) bytes from the source address
// into slot_off+8, no re-slotting (a tagged element rides over
// as its already-built box). Kept loud (out of scope): a
// CALL/sret result (#40-kin) and struct-field / array-literal-
// element sources (loud EARLIER at construction, #49 / #270-1c).
// A cast wrapping a concrete-variant tuple (`(tbl[i]: ci)`)
// survived the widen-cast peel above; its operand is the
// addressable expr. Mirror of cstage cg_widen_tagged_store.
if (su != nil) { if (su.kind == tykind.TY_TUPLE) {
let addrsrc: *node = src;
if (addrsrc.kind == nkind.N_CAST) {
if (addrsrc.lhs != nil) { addrsrc = addrsrc.lhs; };
};
let okkind: bool = false;
if (addrsrc.kind == nkind.N_IDENT) { okkind = true; };
if (addrsrc.kind == nkind.N_INDEX) { okkind = true; };
if (addrsrc.kind == nkind.N_UN) {
if (addrsrc.op == tkind.TK_STAR) { okkind = true; };
};
if (!okkind) {
let mk: str = "cgwidentaggedstore: tuple-typed source shape unwired (only the bare/cast tuple literal and the addressable ident/index/deref trio carry a full payload; see #116)\n";
os.write(2, mk.ptr, mk.len: u64);
os.exit(1);
};
let ntag: i32 = flatvariantidxt(dt, src.type_: *tinfo, false);
if (ntag < 0) {
let mt: str = "cgwidentaggedstore: tuple-in-union variant tag unresolved (untyped/literal tuple element; see #242 / #241)\n";
os.write(2, mt.ptr, mt.len: u64);
os.exit(1);
};
// The tuple's type-table size IS sum(tupeslot) under the 8B-slot
// tuple layout (every walk takes its stride from tupeslot; the
// type's size is their sum), so the payload byte-count routes
// through the type table (rule 13) without re-walking the
// elements — and a wwstage tuple tinfo carries no per-element
// params list anyway. Mirror of cstage cg_widen_tagged_store.
let ntotal: i32 = su.size: i32;
if (8 + ntotal > slot_sz) {
let mp: str = "cgwidentaggedstore: tuple-in-union payload needs SysV eightbyte packing (narrow elements share an eightbyte; see #242 follow-up)\n";
os.write(2, mp.ptr, mp.len: u64);
os.exit(1);
};
if (!cgplaceaddr(c, addrsrc, "SI")) {
let ma: str = "cgwidentaggedstore: addressable tuple source address unresolved (see #116)\n";
os.write(2, ma.ptr, ma.len: u64);
os.exit(1);
};
emitline("\tXORQ\tAX, AX\n");
let nzk: i32 = 0;
for (nzk < slot_sz) {
emitline("\tMOVQ\tAX, ");
emitoff((slot_off + nzk): i64);
emitline("(BP)\n");
nzk += 8;
};
let nck: i32 = 0;
for (nck < ntotal) {
emitline("\tMOVQ\t");
emitoff(nck: i64);
emitline("(SI), AX\n");
emitline("\tMOVQ\tAX, ");
emitoff((slot_off + 8 + nck): i64);
emitline("(BP)\n");
nck += 8;
};
emitline("\tMOVQ\t$");
emitint(ntag: i64);
emitline(", ");
emitoff(slot_off: i64);
emitline("(BP)\n");
return;
}; };
// Struct payload (literal or ident). // Struct payload (literal or ident).
let sname: str = rhsstructpayload(c, src); let sname: str = rhsstructpayload(c, src);
if (sname.len > 0) { if (sname.len > 0) {

View File

@@ -20170,20 +20170,10 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s
}; };
}; };
}; };
// #72: any OTHER tuple-typed source (ident, call result, // #116: a NON-LITERAL tuple-typed source (ident, index,
// match binding) would fall to the scalar arm below and // deref) is no longer loud here — it routes to the
// silently drop payload slot 1+ — loud-stop (rule 7) until // addressable block-copy arm just below the literal arm.
// the word-copy / cursor-receive arms are wired. Mirrors // Mirrors cstage cg_widen_tagged_store.
// cstage cg_widen_tagged_store.
if (tupsrc == nil) {
if (su != nil) {
if (su.kind == tykind.TY_TUPLE) {
let m72: str = "cgwidentaggedstore: tuple-typed source shape unwired (only the bare/cast tuple literal carries a full payload; see #72)\n";
os.write(2, m72.ptr, m72.len: u64);
os.exit(1);
};
};
};
}; };
if (tupsrc != nil) { if (tupsrc != nil) {
if (su != nil) { if (su.kind == tykind.TY_TUPLE) { if (su != nil) { if (su.kind == tykind.TY_TUPLE) {
@@ -20335,6 +20325,83 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s
return; return;
}; }; }; };
}; };
// #116: a NON-LITERAL but ADDRESSABLE tuple source — a tuple
// IDENT var, a slice/array INDEX (tbl[i]), or a DEREF (*p). The
// tuple in memory uses the SAME tupeslot strides as the box
// payload the literal loop above fills, so the source-in-memory
// layout already equals the box payload layout — the fill is a
// flat block-copy of sum(tupeslot) bytes from the source address
// into slot_off+8, no re-slotting (a tagged element rides over
// as its already-built box). Kept loud (out of scope): a
// CALL/sret result (#40-kin) and struct-field / array-literal-
// element sources (loud EARLIER at construction, #49 / #270-1c).
// A cast wrapping a concrete-variant tuple (`(tbl[i]: ci)`)
// survived the widen-cast peel above; its operand is the
// addressable expr. Mirror of cstage cg_widen_tagged_store.
if (su != nil) { if (su.kind == tykind.TY_TUPLE) {
let addrsrc: *node = src;
if (addrsrc.kind == nkind.N_CAST) {
if (addrsrc.lhs != nil) { addrsrc = addrsrc.lhs; };
};
let okkind: bool = false;
if (addrsrc.kind == nkind.N_IDENT) { okkind = true; };
if (addrsrc.kind == nkind.N_INDEX) { okkind = true; };
if (addrsrc.kind == nkind.N_UN) {
if (addrsrc.op == tkind.TK_STAR) { okkind = true; };
};
if (!okkind) {
let mk: str = "cgwidentaggedstore: tuple-typed source shape unwired (only the bare/cast tuple literal and the addressable ident/index/deref trio carry a full payload; see #116)\n";
os.write(2, mk.ptr, mk.len: u64);
os.exit(1);
};
let ntag: i32 = flatvariantidxt(dt, src.type_: *tinfo, false);
if (ntag < 0) {
let mt: str = "cgwidentaggedstore: tuple-in-union variant tag unresolved (untyped/literal tuple element; see #242 / #241)\n";
os.write(2, mt.ptr, mt.len: u64);
os.exit(1);
};
// The tuple's type-table size IS sum(tupeslot) under the 8B-slot
// tuple layout (every walk takes its stride from tupeslot; the
// type's size is their sum), so the payload byte-count routes
// through the type table (rule 13) without re-walking the
// elements — and a wwstage tuple tinfo carries no per-element
// params list anyway. Mirror of cstage cg_widen_tagged_store.
let ntotal: i32 = su.size: i32;
if (8 + ntotal > slot_sz) {
let mp: str = "cgwidentaggedstore: tuple-in-union payload needs SysV eightbyte packing (narrow elements share an eightbyte; see #242 follow-up)\n";
os.write(2, mp.ptr, mp.len: u64);
os.exit(1);
};
if (!cgplaceaddr(c, addrsrc, "SI")) {
let ma: str = "cgwidentaggedstore: addressable tuple source address unresolved (see #116)\n";
os.write(2, ma.ptr, ma.len: u64);
os.exit(1);
};
emitline("\tXORQ\tAX, AX\n");
let nzk: i32 = 0;
for (nzk < slot_sz) {
emitline("\tMOVQ\tAX, ");
emitoff((slot_off + nzk): i64);
emitline("(BP)\n");
nzk += 8;
};
let nck: i32 = 0;
for (nck < ntotal) {
emitline("\tMOVQ\t");
emitoff(nck: i64);
emitline("(SI), AX\n");
emitline("\tMOVQ\tAX, ");
emitoff((slot_off + 8 + nck): i64);
emitline("(BP)\n");
nck += 8;
};
emitline("\tMOVQ\t$");
emitint(ntag: i64);
emitline(", ");
emitoff(slot_off: i64);
emitline("(BP)\n");
return;
}; };
// Struct payload (literal or ident). // Struct payload (literal or ident).
let sname: str = rhsstructpayload(c, src); let sname: str = rhsstructpayload(c, src);
if (sname.len > 0) { if (sname.len > 0) {

View File

@@ -38,9 +38,9 @@
* | into tagged stays the #242/#241 loud | * | into tagged stays the #242/#241 loud |
* | stop (the cast form lifts it; the | * | stop (the cast form lifts it; the |
* | bare form must NOT silently change) | err * | bare form must NOT silently change) | err
* tuple_ident_reject | tuple-typed IDENT into tagged stays | * tuple_ident_widen | tuple-typed IDENT into tagged — #116 |
* | LOUD (#72) — was a silent slot-1+ | * | addressable block-copy arm (was the |
* | drop through the scalar arm | err * | #72 loud-stop); both words round-trip| 0
* *
* Every K_RUN row also asserts cstage/wwstage asm byte-id. NNN<950, * Every K_RUN row also asserts cstage/wwstage asm byte-id. NNN<950,
* self-contained (/tmp, no imports) — rule-14's selfhost-sibling race * self-contained (/tmp, no imports) — rule-14's selfhost-sibling race
@@ -370,11 +370,13 @@ static const struct row rows[] = {
" return 0;\n" " return 0;\n"
"};\n", 0, "};\n", 0,
K_BUILDERR, "tuple-in-union variant tag unresolved" }, K_BUILDERR, "tuple-in-union variant tag unresolved" },
/* tuple-typed IDENT source into a tagged slot: the widen /* #116 GRADUATION: a tuple-typed IDENT source widened into a tagged
* choke-point has no word-copy arm for it — pre-#72 it fell to * slot. Pre-#116 the widen choke-point had no arm for it and loud-
* the scalar arm and SILENTLY dropped payload slot 1+; now loud * stopped (the row pinned that loud); #116 added the addressable
* (rule 7) until #72 wires ident/call/match-binding sources. */ * block-copy arm, so the IDENT source now boxes correctly — both
{ "tuple_ident_reject", * payload words round-trip (r.0 AND r.1). Companion shapes (index,
* deref) + the runtime read-back live in 944_nonlit_tuple_widen_run. */
{ "tuple_ident_widen",
"package main;\n" "package main;\n"
"type lit = rune;\n" "type lit = rune;\n"
"type rng = (u32, u32);\n" "type rng = (u32, u32);\n"
@@ -383,12 +385,15 @@ static const struct row rows[] = {
" let t: rng = ((65, 90): rng);\n" " let t: rng = ((65, 90): rng);\n"
" let v: item = t;\n" " let v: item = t;\n"
" match (v) {\n" " match (v) {\n"
" case let r: rng => { if (r.0 != 65) { return 1; }; };\n" " case let r: rng => {\n"
" if (r.0 != 65) { return 1; };\n"
" if (r.1 != 90) { return 3; };\n"
" };\n"
" case => return 2;\n" " case => return 2;\n"
" };\n" " };\n"
" return 0;\n" " return 0;\n"
"};\n", 0, "};\n", 0,
K_BUILDERR, "tuple-typed source shape unwired" }, K_RUN, NULL },
}; };
/* build+run via a driver (ww / ww_ww); returns 0 pass, nonzero fail. */ /* build+run via a driver (ww / ww_ww); returns 0 pass, nonzero fail. */

View File

@@ -0,0 +1,314 @@
/*
* 944_nonlit_tuple_widen_run — project #116: a NON-LITERAL tuple source
* widened into a tagged box (cg_widen_tagged_store / cgwidentaggedstore).
*
* Pre-#116 only a bare/cast tuple LITERAL (N_TUPLE / N_CAST-of-N_TUPLE)
* carried a full payload into the union box; every ADDRESSABLE non-literal
* tuple source — a tuple IDENT var, a slice/array INDEX `tbl[i]`, a DEREF
* `*p` — hit the `else fatal` ("tuple-typed source shape unwired"), LOUD and
* IDENTICAL on both stages. This blocked fold-6's
* `append(charsets[...], charclass_map[cc_idx])` (the INDEX shape).
*
* THE FIX (align-BOTH; both stages were loud, so there is NO runtime
* reference and byte-id is BLIND — #263). A new arm resolves the source
* ADDRESS through the existing place-address spine (cgplaceaddr — the same
* one &base[i] / *p / ident use) into SI, then BLOCK-COPIES sum(tuple_eslot)
* bytes (== the tuple type's table size under the 8B-slot layout) from the
* source address into the box payload (write_off+8), and stamps the variant
* tag. The source-tuple-in-memory layout already EQUALS the box-payload
* layout (same tuple_eslot strides the literal loop fills), so it is a flat
* address block-copy — no re-slotting, and a tagged element rides over as
* its already-built box. One mechanism closes the addressable trio.
*
* SCOPE (kept loud, out of #116): a CALL/sret tuple result (the tuple sits
* at the sret address, #40-kin) and struct-field / array-literal-element
* tuple sources (loud EARLIER at their construction, #49 / #270-1c).
*
* Because byte-id is blind here, the RUNTIME read-back is the correctness
* net (rob's obligation): EACH covered shape (ident / index / deref) builds
* the box from its non-literal tuple source, match-extracts the variant, and
* ASSERTS the payload survived — `len(x.0)` proves the str header rode over,
* `(*x.1)(arg)` indirect-calls the fn-ptr element and checks its result
* (drew's P3/P4 read-back). The tuple-LITERAL row is the already-green
* regression guard. All four rows confirmed to LOUD on base da30f10.
*
* The read-back deliberately avoids `str !=` (a pre-existing cs!=ww
* comparison-codegen divergence, rt_streq vs inline CMPQ — unrelated to
* #116) so the K_RUN byte-id check isolates the widen arm.
*
* NNN<950, self-contained (/tmp, no imports), so rule-14's selfhost-sibling
* race does not apply (903/940/945 precedent).
*/
#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 */
struct row { const char *label; const char *src; int kind; int want; };
static const struct row rows[] = {
/* control (already green pre-#116): a CAST tuple LITERAL. Regression
* guard — the literal arm must keep emitting the same fill. */
{ "literal",
"package main;\n"
"fn fa(c: rune) bool = { return c == 'a'; };\n"
"type ci = (str, *fn(c: rune) bool);\n"
"type u = (rune | ci);\n"
"export fn main() i32 = {\n"
" let nm: str = \"ww\";\n"
" let cf: *fn(c: rune) bool = &fa;\n"
" let s: []u = [];\n"
" append(s, ((nm, cf): ci));\n"
" match (s[0]) {\n"
" case let r: rune => { return 90; };\n"
" case let x: ci => {\n"
" if ((len(x.0): i32) != 2) { return 1; };\n"
" if (!(*x.1)('a')) { return 2; };\n"
" return 0;\n"
" };\n"
" };\n"
" return 4;\n"
"};\n", K_RUN, 0 },
/* IDENT: a tuple-typed local var as the widen source. */
{ "ident",
"package main;\n"
"fn fb(c: rune) bool = { return c == 'b'; };\n"
"type ci = (str, *fn(c: rune) bool);\n"
"type u = (rune | ci);\n"
"export fn main() i32 = {\n"
" let nm: str = \"zz\";\n"
" let cf: *fn(c: rune) bool = &fb;\n"
" let t: ci = (nm, cf);\n"
" let s: []u = [];\n"
" append(s, t);\n"
" match (s[0]) {\n"
" case let r: rune => { return 90; };\n"
" case let x: ci => {\n"
" if ((len(x.0): i32) != 2) { return 1; };\n"
" if (!(*x.1)('b')) { return 2; };\n"
" if ((*x.1)('a')) { return 3; };\n"
" return 0;\n"
" };\n"
" };\n"
" return 4;\n"
"};\n", K_RUN, 0 },
/* INDEX: `tbl[i]` — the fold-6 shape. The array is filled by tuple-
* ident element stores (the foldable construction at HEAD), then the
* indexed element is the widen source. */
{ "index",
"package main;\n"
"fn fb(c: rune) bool = { return c == 'b'; };\n"
"fn fa(c: rune) bool = { return c == 'a'; };\n"
"type ci = (str, *fn(c: rune) bool);\n"
"type u = (rune | ci);\n"
"export fn main() i32 = {\n"
" let n0: str = \"aa\"; let c0: *fn(c: rune) bool = &fa;\n"
" let n1: str = \"zzz\"; let c1: *fn(c: rune) bool = &fb;\n"
" let t0: ci = (n0, c0);\n"
" let t1: ci = (n1, c1);\n"
" let tbl: [2]ci = [];\n"
" tbl[0] = t0;\n"
" tbl[1] = t1;\n"
" let s: []u = [];\n"
" append(s, tbl[1]);\n"
" match (s[0]) {\n"
" case let r: rune => { return 90; };\n"
" case let x: ci => {\n"
" if ((len(x.0): i32) != 3) { return 1; };\n"
" if (!(*x.1)('b')) { return 2; };\n"
" if ((*x.1)('a')) { return 3; };\n"
" return 0;\n"
" };\n"
" };\n"
" return 4;\n"
"};\n", K_RUN, 0 },
/* DEREF: `*p` — a pointer to a tuple var as the widen source. */
{ "deref",
"package main;\n"
"fn fa(c: rune) bool = { return c == 'a'; };\n"
"type ci = (str, *fn(c: rune) bool);\n"
"type u = (rune | ci);\n"
"export fn main() i32 = {\n"
" let nm: str = \"qqq\";\n"
" let cf: *fn(c: rune) bool = &fa;\n"
" let t: ci = (nm, cf);\n"
" let p: *ci = &t;\n"
" let s: []u = [];\n"
" append(s, *p);\n"
" match (s[0]) {\n"
" case let r: rune => { return 90; };\n"
" case let x: ci => {\n"
" if ((len(x.0): i32) != 3) { return 1; };\n"
" if (!(*x.1)('a')) { return 2; };\n"
" if ((*x.1)('b')) { return 3; };\n"
" return 0;\n"
" };\n"
" };\n"
" return 4;\n"
"};\n", K_RUN, 0 },
};
/* 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], cmd[1024];
snprintf(src, sizeof src, "/tmp/ntw_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/ntw_%d_d_%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>&1",
tmpdir, driver, src);
int brc = runwait(cmd);
if (brc != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
unlink(src); 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); 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
byteid(const char *w6c, const char *w6c_ww, const struct row *r, int i)
{
char src[96], cs_s[96], ws_s[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/ntw_bi_%d_%d.ww", getpid(), i);
snprintf(cs_s, sizeof cs_s, "/tmp/ntw_bi_%d_%d_cs.s", getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/ntw_bi_%d_%d_ww.s", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
int rc = 0;
/* Single-file package — w6c compiles the .ww source directly (no
* imports, no combined.ww amalgam needed); diff the two emissions. */
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", r->label);
rc = 1;
} else {
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", r->label);
rc = 1;
} else if (slurp_eq(cs_s, ws_s) != 0) {
fprintf(stderr, "row[%s]: cstage/wwstage .s DIFFER "
"(#116 non-literal tuple widen regression)\n",
r->label);
rc = 1;
}
}
unlink(src); unlink(cs_s); unlink(ws_s);
return rc;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
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;
}
char cdrv[640], wdrv[640], w6c[640], w6c_ww[640];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
struct { const char *name; const char *path; int gated; }
drivers[] = {
{ "cstage", cdrv, 0 },
{ "wwstage", wdrv, 1 },
{ NULL, NULL, 0 },
};
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int d = 0; drivers[d].name; d++) {
if (drivers[d].gated && access(drivers[d].path, X_OK) != 0) {
fprintf(stderr, "nonlit_tuple_widen: skip %s (no %s)\n",
drivers[d].name, drivers[d].path);
continue;
}
for (int i = 0; i < n; i++) {
total++;
if (run_driver(drivers[d].path, &rows[i], i) != 0) fail++;
}
}
if (access(w6c_ww, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
if (byteid(w6c, w6c_ww, &rows[i], i) != 0) fail++;
}
}
if (fail) {
fprintf(stderr, "nonlit_tuple_widen: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("nonlit_tuple_widen: %d/%d ok\n", total, total);
return 0;
}