From fdfc2ce318b2aa78807f9aa07ee4a929d09c67ec Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Thu, 4 Jun 2026 18:20:07 +0900 Subject: [PATCH] =?UTF-8?q?wcc+w6c+w6c=5Fww:=20tuple=20slot=20layout=20SSo?= =?UTF-8?q?T=20=E2=80=94=20checker=20size=20=3D=20cgen=20slot=20stride=20(?= =?UTF-8?q?C-t0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The checker computed TY_TUPLE size as the packed element-size sum ((u32,u32) = 8B) while every cgen cursor-transport site strode 8B slots (16B). 16B tuples were blind to the split (slot == packed); packed tuples hit it everywhere: cstage let-receive keyed on sz 16/32 missed sz 8 and dropped word 1, the cgfn param receive spilled 8B/element into a packed-sized local (saved-BP clobber, SIGSEGV), and mixed (u32,f64)/(u32,str) shapes missed the receive arms entirely. Slot layout is now the SSoT (user-ratified): the flip lives in the two checkers' N_TTUPLE size computation only (check.c, check.ww tupleelemslot + stamp); cgen's packed-keyed walks (t.N read, #235 len arm, over-cap sret send/receive pair) align onto the slot stride, and the wwstage t.N read gains the natural-width load (tnodeloadop) to byte-id with cstage's fldloadop. ttupleelem.offset re-stamped slot-cumulative (no consumers yet). The #242/#243 eightbyte-share loud-stop dissolves by construction (no two narrows ever share an eightbyte) — 940's eightbyte_share row graduates to a runtime round-trip. Hare-layout divergence documented at both checker sites; re-alignment is task #60. #32 send skew and #33 wwstage literal-let receive are separate commits on this base. 941_tuple_slot_layout_run pins the matrix: 4 packed rows fail at the parent (8/21 checks), 3 neutral anchors prove 16B/32B emission untouched. --- Makefile | 7 + cmd/w6c/cgen.c | 41 +++- cmd/wcc/check.c | 25 ++- selfhost/cmd/w6c/main.combined.ww | 69 ++++--- selfhost/cmd/wcc/cgenexpr.ww | 11 +- selfhost/cmd/wcc/check.ww | 58 +++--- selfhost/cmd/wwdump/main.combined.ww | 69 ++++--- test/wcc/940_tuple_in_union_run.c | 39 ++-- test/wcc/941_tuple_slot_layout_run.c | 287 +++++++++++++++++++++++++++ 9 files changed, 500 insertions(+), 106 deletions(-) create mode 100644 test/wcc/941_tuple_slot_layout_run.c diff --git a/Makefile b/Makefile index 723d3d6a..54c5a186 100644 --- a/Makefile +++ b/Makefile @@ -307,6 +307,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_overcap_tuple_field_store_run \ $(BIN)/test_mixed_scalar_tuple_sret_run \ $(BIN)/test_tuple_in_union_run \ + $(BIN)/test_tuple_slot_layout_run \ $(BIN)/test_errtype_compare \ $(BIN)/test_tuple_elem_slice_len_run \ $(BIN)/test_str_forrange_loopvar_run \ @@ -1414,6 +1415,12 @@ $(BIN)/test_tuple_in_union_run: test/wcc/940_tuple_in_union_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_tuple_slot_layout_run: test/wcc/941_tuple_slot_layout_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_errtype_compare: test/wcc/949_errtype_compare.c \ $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ diff --git a/cmd/w6c/cgen.c b/cmd/w6c/cgen.c index 1adb9a8c..e3904927 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -6660,9 +6660,17 @@ cgexpr(Cg *c, Node *n, Local *locals) idx = idx * 10 + (*q - '0'); Tparam *tp = bu->params; int foff = 0; + /* C-t0: slot stride, twin of the N_DOT + * TY_TUPLE walk. */ while (idx > 0 && tp) { - if (tp->type) - foff += (int)tp->type->size; + Type *su = (tp->type + && tp->type->kind == TY_NAMED) + ? tp->type->under : tp->type; + if (su && (su->kind == TY_STR + || su->kind == TY_SLICE)) + foff += (int)su->size; + else + foff += 8; tp = tp->next; idx--; } @@ -9504,14 +9512,25 @@ cgexpr(Cg *c, Node *n, Local *locals) break; } } - /* tuple positional field access: t.0, t.1, ... */ + /* tuple positional field access: t.0, t.1, ... + * C-t0: slot stride (a str/slice its header, everything else + * one 8B eightbyte) — the layout every cursor transport site + * writes and the checker's TY_TUPLE size now counts. The load + * below keeps the element's NATURAL width (fldloadop). */ if (u && u->kind == TY_TUPLE && n->lhs->kind == N_IDENT && n->str) { int idx = 0; for (const char *q = n->str; *q; q++) idx = idx * 10 + (*q - '0'); Tparam *tp = u->params; int foff = 0; while (idx > 0 && tp) { - if (tp->type) foff += (int)tp->type->size; + Type *su = (tp->type + && tp->type->kind == TY_NAMED) + ? tp->type->under : tp->type; + if (su && (su->kind == TY_STR + || su->kind == TY_SLICE)) + foff += (int)su->size; + else + foff += 8; tp = tp->next; idx--; } @@ -11765,7 +11784,11 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) } else ins2(c, fldstoreop(e->type, esz), areg(D_AX), amem(D_DX, foff)); - foff += esz; + /* C-t0: the sret buffer is slot-laid + * like every tuple home (checker size, + * t.N reader, mlet receive agree); esz + * keeps the store WIDTH natural. */ + foff += wide ? esz : 8; if (pp) pp = pp->next; } ins2(c, A_MOVQ, amem(D_BP, cg_sret_arg_off), @@ -12132,7 +12155,9 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) ins2(c, fldstoreop(t, esz), areg(D_AX), amem(D_BP, off)); } - foff += esz; + /* C-t0: slot stride — must mirror the + * N_RETURN over-cap SEND's buffer layout. */ + foff += wide ? esz : 8; } break; } @@ -12239,7 +12264,9 @@ cgstmt(Cg *c, Node *n, Local **locals, int *frame) areg(D_AX), amem(D_BP, off)); } } - foff += esz; + /* C-t0: slot stride — must mirror the + * N_RETURN over-cap SEND's buffer layout. */ + foff += wide ? esz : 8; if (tp) tp = tp->next; } break; diff --git a/cmd/wcc/check.c b/cmd/wcc/check.c index 531b1cde..6fc3f413 100644 --- a/cmd/wcc/check.c +++ b/cmd/wcc/check.c @@ -629,7 +629,30 @@ resolve_type(Checker *c, Node *n) if (!require_sized(c, tp->type, e->pos, "a tuple member")) continue; if (tp->type && tp->type->align > al) al = tp->type->align; - if (tp->type) sz += tp->type->size; + /* Slot layout is the tuple SSoT (tuple arc C-t0, + * user-ratified): every element occupies the stride + * cgen's cursor transport actually writes — a + * str/slice its header, everything else (narrow + * scalars included) one 8B eightbyte. ww-internal ABI + * only (tuples never cross extern); size((u32,u32))=16 + * is observable via size() and diverges from Hare + * (harec type_store.c:533-580 anonymous-struct rule) + * AND from ww's own structs (which pack narrow fields + * post-fldloadop) — that internal inconsistency is + * what task #60 eventually fixes; re-open before any + * serialization/FFI/density use. Pre-C-t0 this summed + * packed element sizes while cgen strode 8B slots — + * the checker-says-8/cgen-does-16 split behind the + * packed-tuple miscompile family (#32/#33/#48). */ + if (tp->type) { + Type *eu = tp->type->kind == TY_NAMED + ? tp->type->under : tp->type; + if (eu && (eu->kind == TY_STR + || eu->kind == TY_SLICE)) + sz += eu->size; + else if (eu == NULL || eu->kind != TY_VOID) + sz += 8; /* sizelint-ok: the slot IS the 8B eightbyte */ + } if (head == NULL) head = tp; else tail->next = tp; tail = tp; diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 214842ae..0b11b874 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -11926,8 +11926,15 @@ fn tupleelemslot(pt: *tinfo) u64 = { pk == tykind.TY_I32 || pk == tykind.TY_U8 || pk == tykind.TY_U16 || pk == tykind.TY_U32 || pk == tykind.TY_F32 || pk == tykind.TY_ENUM) { return 8u64; }; - // Composite — struct/tuple/array/tagged carry their own slot total. - return t.slotsize; + // Composite — one 8B eightbyte, NOT t.slotsize: cgen's cursor + // transport strides `wide ? size : 8` at every tuple site (both + // stages), so a composite element rides one register word today. + // The checker mirrors what cgen emits (tuple arc C-t0; cstage + // check.c N_TTUPLE twin) — a slotsize answer here would re-open the + // checker-vs-cgen layout split the slot-SSoT ruling closed. No + // composite-element tuple exists in the corpus; transport for >8B + // composites is its own unwired gap. + return 8u64; }; // #61 A.5 helper: per-field slot size mirroring cgenutil.ww @@ -12128,16 +12135,20 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { r.align = storage.align; r.slotsize = storage.size; case nkind.N_TTUPLE: - // Cstage cmd/wcc/check.c:329-345: sum of element sizes with - // per-element alignment NOT padded — cstage uses raw sums for - // tuples and 8B-rounding lives at the call/return ABI layer. + // Slot layout is the tuple SSoT (tuple arc C-t0, user-ratified): + // ti.size = ti.slotsize = per-element slot sum (tupleelemslot — + // a str/slice its header, everything else one 8B eightbyte), + // the stride cgen's cursor transport actually writes. ww- + // internal ABI only (tuples never cross extern); + // size((u32,u32))=16 is observable via size() and diverges from + // Hare (harec type_store.c:533-580 anonymous-struct rule) AND + // from ww's own structs (which pack narrow fields post- + // fldloadop) — that internal inconsistency is what task #60 + // eventually fixes; re-open before any serialization/FFI/ + // density use. Pre-C-t0 ti.size was the packed raw sum while + // cgen strode 8B slots — the checker-says-8/cgen-does-16 split + // behind the packed-tuple miscompile family (#32/#33/#48). // Pre-bind for cycle protection (recursive tuple shapes). - // - // #61 A.5: ti.size = natural sum (cstage parity); ti.slotsize - // = per-element slot sum mirroring cgenutil.ww:2018-2029 - // slotsize TTUPLE — narrow scalars pad to 8 (cgen spills each - // tuple element into its own register / stack-slot eightbyte), - // composites contribute their own ti.slotsize. r = newtype(tykind.TY_TUPLE); tinfocachebind(c.tc, n, r); // #57 A.6.3i-phase-1: populate r.tupleelems as a ttupleelem @@ -12145,37 +12156,32 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { // size/align accumulator. Harec analog ref/harec/src/type_ // store.c:532-589 tuple_init_from_atype — {type, offset, next} // per member onto type->tuple.next chain. Diverges from cstage - // cmd/wcc/check.c:329-345 which stores tuple positionals on - // t->params (Tparam, no offset, consumer recomputes by walking - // at cgen.c:5723-5750); the offset-stored shape lets Phase - // 2/J consumers (dotchainresolve) read offsets directly per - // the A.6 stamp-once-read-many arc. Direct analog - // 26724fe (#50 phase 1, A.6.3f-a) for the head/tail append - // pattern. Offset matches cstage's raw-sum layout (no per- - // element padding) — rule 10 aligns wwstage tuple layout down - // to cstage, distinct from harec's add_padding(&offset, - // memb.align) at type_store.c:561. + // cmd/wcc/check.c N_TTUPLE which stores tuple positionals on + // t->params (Tparam, no offset, consumer recomputes by + // walking); the offset-stored shape lets consumers + // (dotchainresolve) read offsets directly per the A.6 + // stamp-once-read-many arc. Direct analog 26724fe (#50 phase 1, + // A.6.3f-a) for the head/tail append pattern. Offsets are + // slot-cumulative (C-t0), distinct from harec's + // add_padding(&offset, memb.align) at type_store.c:561. let teh: *ttupleelem = nil; let tet: *ttupleelem = nil; - let total: u64 = 0u64; let slottotal: u64 = 0u64; let maxal: u64 = 1u64; let p: *node = n.list; for (p != nil) { let pt: *tinfo = tinfofornode(c, p.lhs); - let elemoff: u64 = total; - let te: *ttupleelem = alloc(ttupleelem{type_=pt, offset=elemoff, tnext=nil})!; + let te: *ttupleelem = alloc(ttupleelem{type_=pt, offset=slottotal, tnext=nil})!; if (teh == nil) { teh = te; } else { tet.tnext = te; }; tet = te; if (pt != nil) { if (pt.align > maxal) { maxal = pt.align; }; - total += pt.size; slottotal += tupleelemslot(pt); }; p = p.next; }; r.tupleelems = teh; - r.size = total; + r.size = slottotal; r.align = maxal; r.slotsize = slottotal; case nkind.N_TSTRUCT: @@ -22876,8 +22882,15 @@ fn cgdot(c: *cgen, n: *node) void = { emitline("(BP), X0\n"); return; }; - let sz: i32 = slotsize(c, tpt); - let op: str = tnodeloadop(c, tpt, sz); + // C-t0: load at the element's NATURAL + // width (narrow MOVL/MOVSXD/... at the + // slot base), not the 8B slot width — + // byte-id twin of cstage's fldloadop + // in the N_DOT TY_TUPLE arm. + let nsz: i32 = 8; + let tpti: *tinfo = tpt.type_: *tinfo; + if (tpti != nil) { nsz = tpti.size: i32; }; + let op: str = tnodeloadop(c, tpt, nsz); emitline("\t"); emitline(op); emitline("\t"); diff --git a/selfhost/cmd/wcc/cgenexpr.ww b/selfhost/cmd/wcc/cgenexpr.ww index c0ab77ee..7f42be37 100644 --- a/selfhost/cmd/wcc/cgenexpr.ww +++ b/selfhost/cmd/wcc/cgenexpr.ww @@ -2658,8 +2658,15 @@ fn cgdot(c: *cgen, n: *node) void = { emitline("(BP), X0\n"); return; }; - let sz: i32 = slotsize(c, tpt); - let op: str = tnodeloadop(c, tpt, sz); + // C-t0: load at the element's NATURAL + // width (narrow MOVL/MOVSXD/... at the + // slot base), not the 8B slot width — + // byte-id twin of cstage's fldloadop + // in the N_DOT TY_TUPLE arm. + let nsz: i32 = 8; + let tpti: *tinfo = tpt.type_: *tinfo; + if (tpti != nil) { nsz = tpti.size: i32; }; + let op: str = tnodeloadop(c, tpt, nsz); emitline("\t"); emitline(op); emitline("\t"); diff --git a/selfhost/cmd/wcc/check.ww b/selfhost/cmd/wcc/check.ww index f7c8fe59..1aeb9ee4 100644 --- a/selfhost/cmd/wcc/check.ww +++ b/selfhost/cmd/wcc/check.ww @@ -1599,8 +1599,15 @@ fn tupleelemslot(pt: *tinfo) u64 = { pk == tykind.TY_I32 || pk == tykind.TY_U8 || pk == tykind.TY_U16 || pk == tykind.TY_U32 || pk == tykind.TY_F32 || pk == tykind.TY_ENUM) { return 8u64; }; - // Composite — struct/tuple/array/tagged carry their own slot total. - return t.slotsize; + // Composite — one 8B eightbyte, NOT t.slotsize: cgen's cursor + // transport strides `wide ? size : 8` at every tuple site (both + // stages), so a composite element rides one register word today. + // The checker mirrors what cgen emits (tuple arc C-t0; cstage + // check.c N_TTUPLE twin) — a slotsize answer here would re-open the + // checker-vs-cgen layout split the slot-SSoT ruling closed. No + // composite-element tuple exists in the corpus; transport for >8B + // composites is its own unwired gap. + return 8u64; }; // #61 A.5 helper: per-field slot size mirroring cgenutil.ww @@ -1801,16 +1808,20 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { r.align = storage.align; r.slotsize = storage.size; case nkind.N_TTUPLE: - // Cstage cmd/wcc/check.c:329-345: sum of element sizes with - // per-element alignment NOT padded — cstage uses raw sums for - // tuples and 8B-rounding lives at the call/return ABI layer. + // Slot layout is the tuple SSoT (tuple arc C-t0, user-ratified): + // ti.size = ti.slotsize = per-element slot sum (tupleelemslot — + // a str/slice its header, everything else one 8B eightbyte), + // the stride cgen's cursor transport actually writes. ww- + // internal ABI only (tuples never cross extern); + // size((u32,u32))=16 is observable via size() and diverges from + // Hare (harec type_store.c:533-580 anonymous-struct rule) AND + // from ww's own structs (which pack narrow fields post- + // fldloadop) — that internal inconsistency is what task #60 + // eventually fixes; re-open before any serialization/FFI/ + // density use. Pre-C-t0 ti.size was the packed raw sum while + // cgen strode 8B slots — the checker-says-8/cgen-does-16 split + // behind the packed-tuple miscompile family (#32/#33/#48). // Pre-bind for cycle protection (recursive tuple shapes). - // - // #61 A.5: ti.size = natural sum (cstage parity); ti.slotsize - // = per-element slot sum mirroring cgenutil.ww:2018-2029 - // slotsize TTUPLE — narrow scalars pad to 8 (cgen spills each - // tuple element into its own register / stack-slot eightbyte), - // composites contribute their own ti.slotsize. r = newtype(tykind.TY_TUPLE); tinfocachebind(c.tc, n, r); // #57 A.6.3i-phase-1: populate r.tupleelems as a ttupleelem @@ -1818,37 +1829,32 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { // size/align accumulator. Harec analog ref/harec/src/type_ // store.c:532-589 tuple_init_from_atype — {type, offset, next} // per member onto type->tuple.next chain. Diverges from cstage - // cmd/wcc/check.c:329-345 which stores tuple positionals on - // t->params (Tparam, no offset, consumer recomputes by walking - // at cgen.c:5723-5750); the offset-stored shape lets Phase - // 2/J consumers (dotchainresolve) read offsets directly per - // the A.6 stamp-once-read-many arc. Direct analog - // 26724fe (#50 phase 1, A.6.3f-a) for the head/tail append - // pattern. Offset matches cstage's raw-sum layout (no per- - // element padding) — rule 10 aligns wwstage tuple layout down - // to cstage, distinct from harec's add_padding(&offset, - // memb.align) at type_store.c:561. + // cmd/wcc/check.c N_TTUPLE which stores tuple positionals on + // t->params (Tparam, no offset, consumer recomputes by + // walking); the offset-stored shape lets consumers + // (dotchainresolve) read offsets directly per the A.6 + // stamp-once-read-many arc. Direct analog 26724fe (#50 phase 1, + // A.6.3f-a) for the head/tail append pattern. Offsets are + // slot-cumulative (C-t0), distinct from harec's + // add_padding(&offset, memb.align) at type_store.c:561. let teh: *ttupleelem = nil; let tet: *ttupleelem = nil; - let total: u64 = 0u64; let slottotal: u64 = 0u64; let maxal: u64 = 1u64; let p: *node = n.list; for (p != nil) { let pt: *tinfo = tinfofornode(c, p.lhs); - let elemoff: u64 = total; - let te: *ttupleelem = alloc(ttupleelem{type_=pt, offset=elemoff, tnext=nil})!; + let te: *ttupleelem = alloc(ttupleelem{type_=pt, offset=slottotal, tnext=nil})!; if (teh == nil) { teh = te; } else { tet.tnext = te; }; tet = te; if (pt != nil) { if (pt.align > maxal) { maxal = pt.align; }; - total += pt.size; slottotal += tupleelemslot(pt); }; p = p.next; }; r.tupleelems = teh; - r.size = total; + r.size = slottotal; r.align = maxal; r.slotsize = slottotal; case nkind.N_TSTRUCT: diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 3e364a6f..4dae206c 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -11926,8 +11926,15 @@ fn tupleelemslot(pt: *tinfo) u64 = { pk == tykind.TY_I32 || pk == tykind.TY_U8 || pk == tykind.TY_U16 || pk == tykind.TY_U32 || pk == tykind.TY_F32 || pk == tykind.TY_ENUM) { return 8u64; }; - // Composite — struct/tuple/array/tagged carry their own slot total. - return t.slotsize; + // Composite — one 8B eightbyte, NOT t.slotsize: cgen's cursor + // transport strides `wide ? size : 8` at every tuple site (both + // stages), so a composite element rides one register word today. + // The checker mirrors what cgen emits (tuple arc C-t0; cstage + // check.c N_TTUPLE twin) — a slotsize answer here would re-open the + // checker-vs-cgen layout split the slot-SSoT ruling closed. No + // composite-element tuple exists in the corpus; transport for >8B + // composites is its own unwired gap. + return 8u64; }; // #61 A.5 helper: per-field slot size mirroring cgenutil.ww @@ -12128,16 +12135,20 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { r.align = storage.align; r.slotsize = storage.size; case nkind.N_TTUPLE: - // Cstage cmd/wcc/check.c:329-345: sum of element sizes with - // per-element alignment NOT padded — cstage uses raw sums for - // tuples and 8B-rounding lives at the call/return ABI layer. + // Slot layout is the tuple SSoT (tuple arc C-t0, user-ratified): + // ti.size = ti.slotsize = per-element slot sum (tupleelemslot — + // a str/slice its header, everything else one 8B eightbyte), + // the stride cgen's cursor transport actually writes. ww- + // internal ABI only (tuples never cross extern); + // size((u32,u32))=16 is observable via size() and diverges from + // Hare (harec type_store.c:533-580 anonymous-struct rule) AND + // from ww's own structs (which pack narrow fields post- + // fldloadop) — that internal inconsistency is what task #60 + // eventually fixes; re-open before any serialization/FFI/ + // density use. Pre-C-t0 ti.size was the packed raw sum while + // cgen strode 8B slots — the checker-says-8/cgen-does-16 split + // behind the packed-tuple miscompile family (#32/#33/#48). // Pre-bind for cycle protection (recursive tuple shapes). - // - // #61 A.5: ti.size = natural sum (cstage parity); ti.slotsize - // = per-element slot sum mirroring cgenutil.ww:2018-2029 - // slotsize TTUPLE — narrow scalars pad to 8 (cgen spills each - // tuple element into its own register / stack-slot eightbyte), - // composites contribute their own ti.slotsize. r = newtype(tykind.TY_TUPLE); tinfocachebind(c.tc, n, r); // #57 A.6.3i-phase-1: populate r.tupleelems as a ttupleelem @@ -12145,37 +12156,32 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { // size/align accumulator. Harec analog ref/harec/src/type_ // store.c:532-589 tuple_init_from_atype — {type, offset, next} // per member onto type->tuple.next chain. Diverges from cstage - // cmd/wcc/check.c:329-345 which stores tuple positionals on - // t->params (Tparam, no offset, consumer recomputes by walking - // at cgen.c:5723-5750); the offset-stored shape lets Phase - // 2/J consumers (dotchainresolve) read offsets directly per - // the A.6 stamp-once-read-many arc. Direct analog - // 26724fe (#50 phase 1, A.6.3f-a) for the head/tail append - // pattern. Offset matches cstage's raw-sum layout (no per- - // element padding) — rule 10 aligns wwstage tuple layout down - // to cstage, distinct from harec's add_padding(&offset, - // memb.align) at type_store.c:561. + // cmd/wcc/check.c N_TTUPLE which stores tuple positionals on + // t->params (Tparam, no offset, consumer recomputes by + // walking); the offset-stored shape lets consumers + // (dotchainresolve) read offsets directly per the A.6 + // stamp-once-read-many arc. Direct analog 26724fe (#50 phase 1, + // A.6.3f-a) for the head/tail append pattern. Offsets are + // slot-cumulative (C-t0), distinct from harec's + // add_padding(&offset, memb.align) at type_store.c:561. let teh: *ttupleelem = nil; let tet: *ttupleelem = nil; - let total: u64 = 0u64; let slottotal: u64 = 0u64; let maxal: u64 = 1u64; let p: *node = n.list; for (p != nil) { let pt: *tinfo = tinfofornode(c, p.lhs); - let elemoff: u64 = total; - let te: *ttupleelem = alloc(ttupleelem{type_=pt, offset=elemoff, tnext=nil})!; + let te: *ttupleelem = alloc(ttupleelem{type_=pt, offset=slottotal, tnext=nil})!; if (teh == nil) { teh = te; } else { tet.tnext = te; }; tet = te; if (pt != nil) { if (pt.align > maxal) { maxal = pt.align; }; - total += pt.size; slottotal += tupleelemslot(pt); }; p = p.next; }; r.tupleelems = teh; - r.size = total; + r.size = slottotal; r.align = maxal; r.slotsize = slottotal; case nkind.N_TSTRUCT: @@ -22876,8 +22882,15 @@ fn cgdot(c: *cgen, n: *node) void = { emitline("(BP), X0\n"); return; }; - let sz: i32 = slotsize(c, tpt); - let op: str = tnodeloadop(c, tpt, sz); + // C-t0: load at the element's NATURAL + // width (narrow MOVL/MOVSXD/... at the + // slot base), not the 8B slot width — + // byte-id twin of cstage's fldloadop + // in the N_DOT TY_TUPLE arm. + let nsz: i32 = 8; + let tpti: *tinfo = tpt.type_: *tinfo; + if (tpti != nil) { nsz = tpti.size: i32; }; + let op: str = tnodeloadop(c, tpt, nsz); emitline("\t"); emitline(op); emitline("\t"); diff --git a/test/wcc/940_tuple_in_union_run.c b/test/wcc/940_tuple_in_union_run.c index 12a193ac..02e27077 100644 --- a/test/wcc/940_tuple_in_union_run.c +++ b/test/wcc/940_tuple_in_union_run.c @@ -29,14 +29,14 @@ * Construction is correct at ANY variant position (the variant tag, not a * default-0) — `tuple_tag1` and `tuple_after_int` place the tuple at index 1. * - * SCOPE (rule 7 loud-stops, PINNED by the K_BUILDERR rows): a tuple built + * SCOPE (rule 7 loud-stop, PINNED by the K_BUILDERR row): a tuple built * from a BARE LITERAL element (cstage mis-types `true`/`false`/untyped `7` → * tag unresolved, #241 literal-init family; wwstage types them but mirrors - * cstage's CONDITION down per rule 10) and a tuple whose narrow elements - * SHARE a SysV eightbyte (e.g. (i32,i32,u64), needs eightbyte classification, - * #243) both loud-stop in cgen on BOTH stages rather than silently - * miscompile. The K_RUN rows build their tuple from TYPED expressions — the - * supported, byte-identical shape. + * cstage's CONDITION down per rule 10) loud-stops in cgen on BOTH stages + * rather than silently miscompile. The old eightbyte-share loud-stop + * ((i32,i32,u64), #243) DISSOLVED with the slot-SSoT tuple layout (tuple + * arc C-t0) — that row graduated to K_RUN below. The K_RUN rows build + * their tuple from TYPED expressions — the supported, byte-identical shape. * * K_RUN rows: build+run exit 0 on BOTH drivers AND cs==ww byte-identical. * K_BUILDERR rows: build FAILS with the #242 diagnostic on BOTH drivers. @@ -165,19 +165,30 @@ static const struct row rows[] = { " };\n" " return 4;\n" "};\n", K_RUN, 0, NULL }, - /* rule-7 loud-stop (i): a tuple whose narrow elements SHARE a SysV - * eightbyte ((i32,i32,u64) — i32@0,i32@4,u64@8 packs to 16B, but the - * 8B-slotted write needs 24B) overflows the union payload. Both stages - * MUST loud-stop (eightbyte classification is the #243 follow-up), not - * silently miscompile. Typed params -> the tag resolves; the SIZE guard - * fires. */ + /* C-t0 graduation (was the #242/#243 loud-stop): under the slot-SSoT + * tuple layout narrow elements never share an eightbyte — + * (i32,i32,u64) is 24B (3 slots), the slotted union write fits by + * construction, and the whole tuple round-trips through the union. + * cg_widen_tagged_store's size guard stays as a safety net but can + * no longer fire for an in-cap tuple. */ { "eightbyte_share", "package main;\n" "fn f(a: i32, b: i32, c: u64) ((i32, i32, u64) | void) = {\n" " return (a, b, c);\n" "};\n" - "export fn main() i32 = { return 0; };\n", - K_BUILDERR, 0, "needs SysV eightbyte packing" }, + "export fn main() i32 = {\n" + " match (f(-3, 4, 9u64)) {\n" + " case let t: (i32, i32, u64) => {\n" + " let (x, y, z) = t;\n" + " if (x != -3) { return 1; };\n" + " if (y != 4) { return 2; };\n" + " if (z != 9u64) { return 3; };\n" + " return 0;\n" + " };\n" + " case void => { return 4; };\n" + " };\n" + " return 5;\n" + "};\n", K_RUN, 0, NULL }, /* rule-7 loud-stop (ii): a tuple built from a BARE LITERAL element * (`true`). cstage's cg_tag_for_variant can't type the literal (#241) * so it returns -1 and loud-stops; wwstage types `true` as bool and diff --git a/test/wcc/941_tuple_slot_layout_run.c b/test/wcc/941_tuple_slot_layout_run.c new file mode 100644 index 00000000..ce0cde1c --- /dev/null +++ b/test/wcc/941_tuple_slot_layout_run.c @@ -0,0 +1,287 @@ +/* + * 941_tuple_slot_layout_run — tuple arc C-t0: SLOT layout is the tuple + * SSoT (user-ratified). The checker's TY_TUPLE size counts the stride + * cgen's cursor transport actually writes — a str/slice its 24B header, + * everything else (narrow scalars included) one 8B eightbyte — and the + * t.N read walks stride the same slots. + * + * Pre-C-t0 there were TWO answers: the checker summed PACKED element + * sizes ((u32,u32) = 8B) while every cursor transport site strode 8B + * slots (16B). The 16B shapes were blind to the split (slot == packed); + * the packed shapes hit it everywhere: + * - `let t: (u32,u32) = f()` — cstage's receive keys on the checker + * size (16/32), so sz=8 fell to the generic single-word store + * (word 1 DROPPED) and the packed read walk then read the wrong + * offsets. wwstage (slot-keyed throughout) was runtime-CORRECT + * here — inverted polarity vs the 16B #33 map, gate-blind. + * - a (u32,u32) by-value param: cgfn's receive walk spilled + * 8B/element into a local sized from the PACKED checker size (8B) + * — word 1 landed on the saved-BP slot, frame corruption, SIGSEGV. + * - a mixed (u32,f64)/(u32,str) tuple: packed sz (12/28) missed the + * 16/32 receive arms entirely — silent drop on both stages. + * + * The send-side ABI skew (#32, C-t2) and the wwstage literal-let + * receive (#33, C-t1) are SEPARATE bugs pinned by their own commits; + * this test pins the layout unification only: + * + * row | shape | want + * ---------------------+--------------------------------------+----- + * letcall_packed | let t:(u32,u32)=f(); t.0/t.1 | 0 + * letcall_float_packed | (u32,f64) from call, X0 element | 0 + * letcall_signed_packed| (i32,i32) negatives — MOVSXD reads | 0 + * letcall_mixed_packed | (u32,str): 28B packed missed the 32B | + * | arm; len(t.1) strides a full slot | 0 + * len_elem_packed | len(t.1) of (u32,str) — #235 arm's | + * | slot stride (was BP+4+8) | (in mixed row) + * letcall_16_neutral | (i64,i64) — slot==packed, anchor | 0 + * letcall_32_neutral | (i64,str) — 32B arm, anchor | 0 + * mlet_packed_neutral | let (a,b) = f() packed — per-element | + * | locals, layout-independent anchor | 0 + * + * Every row also asserts cstage/wwstage asm byte-id. + * NNN<950, self-contained (/tmp, no imports) — rule-14's + * selfhost-sibling race does not apply (903/940/945 precedent). + */ +#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; +} + +struct row { const char *label; const char *src; int want; }; + +static const struct row rows[] = { + { "letcall_packed", + "package main;\n" + "fn f() (u32, u32) = {\n" + " return (3, 4);\n" + "};\n" + "export fn main() i32 = {\n" + " let t: (u32, u32) = f();\n" + " if (t.0 != 3) { return 1; };\n" + " if (t.1 != 4) { return 2; };\n" + " return 0;\n" + "};\n", 0 }, + { "letcall_float_packed", + "package main;\n" + "fn f() (u32, f64) = {\n" + " let a: u32 = 9;\n" + " let x: f64 = 2.5;\n" + " return (a, x);\n" + "};\n" + "export fn main() i32 = {\n" + " let t: (u32, f64) = f();\n" + " if (t.0 != 9) { return 1; };\n" + " if (t.1 != 2.5) { return 2; };\n" + " return 0;\n" + "};\n", 0 }, + { "letcall_signed_packed", + "package main;\n" + "fn f() (i32, i32) = {\n" + " let a: i32 = -5;\n" + " let b: i32 = -6;\n" + " return (a, b);\n" + "};\n" + "export fn main() i32 = {\n" + " let t: (i32, i32) = f();\n" + " if (t.0 != -5) { return 1; };\n" + " if (t.1 != -6) { return 2; };\n" + " if (t.0 + t.1 != -11) { return 3; };\n" + " return 0;\n" + "};\n", 0 }, + { "letcall_mixed_packed", + "package main;\n" + "fn f() (u32, str) = {\n" + " let a: u32 = 7;\n" + " let s: str = \"hello\";\n" + " return (a, s);\n" + "};\n" + "export fn main() i32 = {\n" + " let t: (u32, str) = f();\n" + " if (t.0 != 7) { return 1; };\n" + " if (len(t.1) != 5) { return 2; };\n" + " return 0;\n" + "};\n", 0 }, + { "letcall_16_neutral", + "package main;\n" + "fn f() (i64, i64) = {\n" + " return (41, 17);\n" + "};\n" + "export fn main() i32 = {\n" + " let t: (i64, i64) = f();\n" + " if (t.0 != 41) { return 1; };\n" + " if (t.1 != 17) { return 2; };\n" + " return 0;\n" + "};\n", 0 }, + { "letcall_32_neutral", + "package main;\n" + "fn f() (i64, str) = {\n" + " let a: i64 = 12;\n" + " let s: str = \"wxyz\";\n" + " return (a, s);\n" + "};\n" + "export fn main() i32 = {\n" + " let t: (i64, str) = f();\n" + " if (t.0 != 12) { return 1; };\n" + " if (len(t.1) != 4) { return 2; };\n" + " return 0;\n" + "};\n", 0 }, + { "mlet_packed_neutral", + "package main;\n" + "fn f() (u32, u32) = {\n" + " return (3, 4);\n" + "};\n" + "export fn main() i32 = {\n" + " let (a, b) = f();\n" + " if (a != 3) { return 1; };\n" + " if (b != 4) { return 2; };\n" + " return 0;\n" + "};\n", 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], errf[96], cmd[1024]; + snprintf(src, sizeof src, "/tmp/tsl_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/tsl_%d_d_%d", getpid(), i); + snprintf(errf, sizeof errf, "/tmp/tsl_%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; +} + +/* cs==ww .s byte-id (rule 10). */ +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/tsl_asm_%d_%d.ww", getpid(), i); + snprintf(cs, sizeof cs, "/tmp/tsl_asm_%d_%d_c.s", getpid(), i); + snprintf(ws, sizeof ws, "/tmp/tsl_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++) { + total++; + if (asm_byte_identical(bin, &rows[i], i) != 0) fail++; + } + } + + if (fail) { + fprintf(stderr, "tuple_slot_layout: %d/%d checks failed\n", + fail, total); + return 1; + } + printf("tuple_slot_layout: %d/%d ok\n", total, total); + return 0; +}