diff --git a/Makefile b/Makefile index 797b5f36..9bbb3274 100644 --- a/Makefile +++ b/Makefile @@ -247,6 +247,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_arr_ptr_global \ $(BIN)/test_def_arr_infer_len \ $(BIN)/test_def_arr_len \ + $(BIN)/test_def_str_table \ $(BIN)/test_slice_str_global_zero \ $(BIN)/test_slice_literal_global \ $(BIN)/test_global_arr_elem_field \ @@ -661,6 +662,12 @@ $(BIN)/test_def_arr_len: test/wcc/816_def_arr_len.c $(BIN)/ww \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_def_str_table: test/wcc/819_def_str_table.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_arr_tagged_elem: test/wcc/685_arr_tagged_elem.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 aff534df..86065cda 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -15326,11 +15326,12 @@ emit_array_lit_bytes(FILE *out, Cg *c, Type *t, Node *rhs, int emit_phase) * by let_pre_intern so its rodata _S_ row exists before this row's DATAR * references it. * - * Scoped to DATAW (writable `let`): A_DATAR requires its holder be a - * DATAW slot (w6a asm.c:362), so a read-only `def [N]str` can't carry - * the relocs — that generalisation is a #18 follow-up. Returns 0 if the - * element type isn't str, leaving the generic array path / zero-init to - * the caller. */ + * Always emits into DATAW (writable): A_DATAR requires its holder be a + * DATAW slot (w6a asm.c:362), so both `let` and a read-only `def [N]str` + * (#8/GAP-B) park their backing here — the section bit is the reloc-holder + * constraint, not a mutability grant (def immutability stays checker- + * enforced). Returns 0 if the element type isn't str, leaving the generic + * array path / zero-init to the caller. */ static int emit_strarray_data(FILE *out, Cg *c, const char *directive, const char *name, const char *module, Type *t, Node *rhs) @@ -15340,7 +15341,18 @@ emit_strarray_data(FILE *out, Cg *c, const char *directive, Type *etype = u->sub; Type *eu = type_chase_named(etype); if (eu == NULL || eu->kind != TY_STR) return 0; - if (strcmp(directive, "DATAW") != 0) return 0; + /* #8/GAP-B: a str-element array's backing ALWAYS lives in DATAW + * (writable section), regardless of the caller's let/def directive — + * each element carries an A_DATAR ptr-reloc to its _S_ rodata row, and + * w6a requires a DATAR holder be a DATAW slot (asm.c:362). The passed + * `directive` ("DATA" for a def, "DATAW" for a let) is therefore + * IGNORED here; the emit below hardcodes DATAW. A `def [N]str` stays + * immutable — the checker rejects writes to a def; DATAW is only the + * reloc-holder placement, not a mutability grant (rule-8 placement + * detail). Pre-fix this gate skipped the def path → no DATA block → + * w6l undefined 'main.C' (#270 lineage; int-def is plain DATA, no + * holder constraint, so it was unaffected). */ + (void)directive; int esz = (int)etype->size; int alen = (int)u->alen; @@ -16050,6 +16062,44 @@ sdef_collect(Cg *c, Node *file) } } +/* pre_intern_strarray — SSoT for the #18 [N]str element-strlit intern + * ORDER (element order, then `...` repeat-fill). Shared by let_pre_intern's + * let arm and the #8/GAP-B def arm so both emit labels in the SAME order + * emit_strarray_data references them by — a divergent order would mis-pair + * the DATAR rows with their _S_ rodata. `u` is the chased TY_ARRAY type, + * `r` the chased N_ARRLIT rhs; the caller has verified the element is str. */ +static void +pre_intern_strarray(Cg *c, Type *u, Node *r) +{ + int alen = (int)u->alen; + int cnt = 0; + Node *last_ev = NULL; + int repeat = 0; + for (Node *e = r->list; e && cnt < alen; e = e->next) { + if (e->kind == N_FIELD && e->str + && strcmp(e->str, "...") == 0) { + repeat = 1; + break; + } + Node *ev = e; + while (ev && ev->kind == N_CAST) + ev = ev->lhs; + if (ev == NULL || ev->kind != N_STRLIT) + break; + if (ev->strlen > 0) + (void)intern_strlit(c, ev->str, ev->strlen); + last_ev = ev; + cnt++; + } + if (repeat && last_ev != NULL && last_ev->strlen > 0) { + while (cnt < alen) { + (void)intern_strlit(c, last_ev->str, + last_ev->strlen); + cnt++; + } + } +} + /* Pre-intern strlits referenced from top-level `let` initialisers * (e.g. `let g: str = "hello";`). Interning has to happen before * emit_data walks the strlit list, but we don't want to reorder @@ -16062,6 +16112,25 @@ let_pre_intern(Cg *c, Node *file) { if (file == NULL) return; for (Node *d = file->list; d; d = d->next) { + /* #8/GAP-B: a `def [N]str` needs the SAME element-strlit + * pre-interning as the let [N]str arm below (the #18 ordering + * contract) so emit_strarray_data's DATAR rows find their _S_ + * rodata. let_pre_intern walked only N_LET; a def's labels were + * allocated too late (emit_defs pass) → dangling _S_. Str-array + * ONLY — def tuple/slice/tagged/scalar-str stay out of scope + * (#10/#270 / inline-Sdef). */ + if (d->kind == N_DEF) { + Type *du = type_chase_named(d->type); + Node *dr = d->rhs; + while (dr && dr->kind == N_CAST) dr = dr->lhs; + if (du && du->kind == TY_ARRAY && dr + && dr->kind == N_ARRLIT) { + Type *deu = type_chase_named(du->sub); + if (deu && deu->kind == TY_STR) + pre_intern_strarray(c, du, dr); + } + continue; /* defs ride only the str-array twin */ + } if (d->kind != N_LET) continue; Node *r = d->rhs; while (r != NULL && r->kind == N_CAST) r = r->lhs; @@ -16077,37 +16146,7 @@ let_pre_intern(Cg *c, Node *file) && r != NULL && r->kind == N_ARRLIT) { Type *eu = type_chase_named(u->sub); if (eu != NULL && eu->kind == TY_STR) { - int alen = (int)u->alen; - int cnt = 0; - Node *last_ev = NULL; - int repeat = 0; - for (Node *e = r->list; e && cnt < alen; - e = e->next) { - if (e->kind == N_FIELD && e->str - && strcmp(e->str, "...") == 0) { - repeat = 1; - break; - } - Node *ev = e; - while (ev && ev->kind == N_CAST) - ev = ev->lhs; - if (ev == NULL || ev->kind != N_STRLIT) - break; - if (ev->strlen > 0) - (void)intern_strlit(c, ev->str, - ev->strlen); - last_ev = ev; - cnt++; - } - if (repeat && last_ev != NULL - && last_ev->strlen > 0) { - while (cnt < alen) { - (void)intern_strlit(c, - last_ev->str, - last_ev->strlen); - cnt++; - } - } + pre_intern_strarray(c, u, r); continue; } } diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index f25dfd32..778c2bb9 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -23401,6 +23401,17 @@ fn cgindex(c: *cgen, n: *node) void = { etn = idxelemtn(bl.tnode); } else { etn = idxelemtn(letvartnode(c, base.str)); + // #8/GAP-B: a def-global str/slice-array base lives + // in c.defs, not c.lets — letvartnode misses it → + // etn nil → elem mis-classified scalar, dropping the + // 3-word slice-header load (returns element ADDR not + // .len; cstage's Type-based classify loads the full + // header, the proven let form). defvartnode is the + // def-side sister — same fallback as the base-address + // resolution at cgenexpr.ww:1762. + if (etn == nil) { + etn = idxelemtn(defvartnode(c, base.str)); + }; }; // #60: an alias-NAMED base has no element tnode // (idxelemtn sees the N_TNAME leaf, nil) — classify @@ -39618,6 +39629,48 @@ fn emitdatawbyte(b: u8) void = { emitbytes( bb.ptr, 1u64); }; +// preinternstrarray — SSoT for the #18 [N]str element-strlit intern +// ORDER (element order, then `...` repeat-fill). Shared by letpreintern's +// let arm and the #8/GAP-B def arm so both intern labels in the SAME +// order emitstrarraydata references them by — a divergent order would +// mis-pair the DATAR rows with their _S_ rodata. au is the chased +// TY_ARRAY tinfo, r the N_ARRLIT rhs; caller verified the element is str. +fn preinternstrarray(c: *cgen, au: *tinfo, r: *node) void = { + let alen: i32 = au.alen: i32; + let cnt: i32 = 0; + let last_ev: *node = nil; + let repeat: bool = false; + let e: *node = r.list; + for (e != nil && cnt < alen) { + if (e.kind == nkind.N_FIELD) { + if (streq(e.str, "...")) { + repeat = true; + break; + }; + }; + let ev: *node = e; + for (ev != nil && ev.kind == nkind.N_CAST) { + ev = ev.lhs; + }; + if (ev == nil) { break; }; + if (ev.kind != nkind.N_STRLIT) { break; }; + if (ev.str.len > 0) { + internstrlit(c, ev.str); + }; + last_ev = ev; + cnt += 1; + e = e.next; + }; + if (repeat && last_ev != nil) { + if (last_ev.str.len > 0) { + for (cnt < alen) { + internstrlit(c, last_ev.str); + cnt += 1; + }; + }; + }; +}; + // letpreintern — intern strlits referenced from top-level str-let // initialisers BEFORE emitdatasection runs. Mirrors cmd/w6c/cgen.c // let_pre_intern: emitletdataw later looks up the same label, and @@ -39628,6 +39681,29 @@ export fn letpreintern(c: *cgen, file: *node) void = { if (file == nil) { return; }; let d: *node = file.list; for (d != nil) { + // #8/GAP-B: a `def [N]str` needs the SAME element-strlit + // pre-interning as the let [N]str arm (the #18 ordering + // contract) so emitstrarraydata's DATAR rows find their _S_ + // rodata. letpreintern walked only N_LET; a def's labels were + // allocated too late (emitdefconstants pass) → dangling _S_. + // Str-array ONLY — def tuple/slice/tagged/scalar-str stay out + // of scope (#10/#270 / inline-Sdef). + if (d.kind == nkind.N_DEF) { + let dr: *node = d.rhs; + for (dr != nil && dr.kind == nkind.N_CAST) { + dr = dr.lhs; + }; + if (d.lhs != nil && dr != nil + && dr.kind == nkind.N_ARRLIT) { + let dau: *tinfo = tichase(d.lhs.type_: *tinfo); + if (dau != nil && dau.kind == tykind.TY_ARRAY) { + let deu: *tinfo = tichase(dau.sub); + if (deu != nil && deu.kind == tykind.TY_STR) { + preinternstrarray(c, dau, dr); + }; + }; + }; + }; if (d.kind == nkind.N_LET) { let r: *node = d.rhs; for (r != nil) { @@ -39650,39 +39726,7 @@ export fn letpreintern(c: *cgen, file: *node) void = { let eu: *tinfo = tichase(au.sub); if (eu != nil && eu.kind == tykind.TY_STR) { handled = true; - let alen: i32 = au.alen: i32; - let cnt: i32 = 0; - let last_ev: *node = nil; - let repeat: bool = false; - let e: *node = r.list; - for (e != nil && cnt < alen) { - if (e.kind == nkind.N_FIELD) { - if (streq(e.str, "...")) { - repeat = true; - break; - }; - }; - let ev: *node = e; - for (ev != nil && ev.kind == nkind.N_CAST) { - ev = ev.lhs; - }; - if (ev == nil) { break; }; - if (ev.kind != nkind.N_STRLIT) { break; }; - if (ev.str.len > 0) { - internstrlit(c, ev.str); - }; - last_ev = ev; - cnt += 1; - e = e.next; - }; - if (repeat && last_ev != nil) { - if (last_ev.str.len > 0) { - for (cnt < alen) { - internstrlit(c, last_ev.str); - cnt += 1; - }; - }; - }; + preinternstrarray(c, au, r); }; }; }; @@ -40342,9 +40386,11 @@ fn emitarraylitbytes(c: *cgen, arrt: *tinfo, rhs: *node, // pattern (DATAW header with a zero ptr placeholder + inline LE len, // then a per-element DATAR) at offset idx*esz. Each strlit was pre- // interned by letpreintern so its _S_ rodata row exists before this -// row's DATAR references it. Scoped to DATAW (writable `let`): A_DATAR -// requires a DATAW holder, so a read-only `def [N]str` can't carry the -// relocs (#18 follow-up). Returns false when the element type isn't str. +// row's DATAR references it. Always emits into DATAW (writable): A_DATAR +// requires a DATAW holder, so both `let` and a read-only `def [N]str` +// (#8/GAP-B) park their backing here — the section bit is the reloc- +// holder constraint, not a mutability grant (def immutability stays +// checker-enforced). Returns false when the element type isn't str. fn emitstrarraydata(c: *cgen, directive: str, name: str, module: str, arrt: *tinfo, rhs: *node) bool = { let au: *tinfo = arrt; @@ -40355,7 +40401,17 @@ fn emitstrarraydata(c: *cgen, directive: str, name: str, module: str, eu = tichase(eu); if (eu == nil) { return false; }; if (eu.kind != tykind.TY_STR) { return false; }; - if (!streq(directive, "DATAW")) { return false; }; + // #8/GAP-B: a str-element array's backing ALWAYS lives in DATAW + // (writable section), regardless of the caller's let/def directive — + // each element carries an A_DATAR ptr-reloc to its _S_ rodata row, and + // w6a requires a DATAR holder be a DATAW slot (asm.c:362). The passed + // directive ("DATA" for a def, "DATAW" for a let) is therefore IGNORED + // here; the emit below hardcodes DATAW. A `def [N]str` stays immutable + // — the checker rejects writes to a def; DATAW is only the reloc-holder + // placement, not a mutability grant (rule-8 placement detail). Pre-fix + // this gate skipped the def path → no DATA block → w6l undefined + // 'main.C' (#270 lineage; int-def is plain DATA, no holder constraint, + // so it was unaffected). let esz: i32 = au.sub.size: i32; let alen: i32 = au.alen: i32; diff --git a/selfhost/cmd/wcc/cgen.ww b/selfhost/cmd/wcc/cgen.ww index 423856e8..7a77439a 100644 --- a/selfhost/cmd/wcc/cgen.ww +++ b/selfhost/cmd/wcc/cgen.ww @@ -1350,6 +1350,48 @@ fn emitdatawbyte(b: u8) void = { emitbytes( bb.ptr, 1u64); }; +// preinternstrarray — SSoT for the #18 [N]str element-strlit intern +// ORDER (element order, then `...` repeat-fill). Shared by letpreintern's +// let arm and the #8/GAP-B def arm so both intern labels in the SAME +// order emitstrarraydata references them by — a divergent order would +// mis-pair the DATAR rows with their _S_ rodata. au is the chased +// TY_ARRAY tinfo, r the N_ARRLIT rhs; caller verified the element is str. +fn preinternstrarray(c: *cgen, au: *tinfo, r: *node) void = { + let alen: i32 = au.alen: i32; + let cnt: i32 = 0; + let last_ev: *node = nil; + let repeat: bool = false; + let e: *node = r.list; + for (e != nil && cnt < alen) { + if (e.kind == nkind.N_FIELD) { + if (streq(e.str, "...")) { + repeat = true; + break; + }; + }; + let ev: *node = e; + for (ev != nil && ev.kind == nkind.N_CAST) { + ev = ev.lhs; + }; + if (ev == nil) { break; }; + if (ev.kind != nkind.N_STRLIT) { break; }; + if (ev.str.len > 0) { + internstrlit(c, ev.str); + }; + last_ev = ev; + cnt += 1; + e = e.next; + }; + if (repeat && last_ev != nil) { + if (last_ev.str.len > 0) { + for (cnt < alen) { + internstrlit(c, last_ev.str); + cnt += 1; + }; + }; + }; +}; + // letpreintern — intern strlits referenced from top-level str-let // initialisers BEFORE emitdatasection runs. Mirrors cmd/w6c/cgen.c // let_pre_intern: emitletdataw later looks up the same label, and @@ -1360,6 +1402,29 @@ export fn letpreintern(c: *cgen, file: *node) void = { if (file == nil) { return; }; let d: *node = file.list; for (d != nil) { + // #8/GAP-B: a `def [N]str` needs the SAME element-strlit + // pre-interning as the let [N]str arm (the #18 ordering + // contract) so emitstrarraydata's DATAR rows find their _S_ + // rodata. letpreintern walked only N_LET; a def's labels were + // allocated too late (emitdefconstants pass) → dangling _S_. + // Str-array ONLY — def tuple/slice/tagged/scalar-str stay out + // of scope (#10/#270 / inline-Sdef). + if (d.kind == nkind.N_DEF) { + let dr: *node = d.rhs; + for (dr != nil && dr.kind == nkind.N_CAST) { + dr = dr.lhs; + }; + if (d.lhs != nil && dr != nil + && dr.kind == nkind.N_ARRLIT) { + let dau: *tinfo = tichase(d.lhs.type_: *tinfo); + if (dau != nil && dau.kind == tykind.TY_ARRAY) { + let deu: *tinfo = tichase(dau.sub); + if (deu != nil && deu.kind == tykind.TY_STR) { + preinternstrarray(c, dau, dr); + }; + }; + }; + }; if (d.kind == nkind.N_LET) { let r: *node = d.rhs; for (r != nil) { @@ -1382,39 +1447,7 @@ export fn letpreintern(c: *cgen, file: *node) void = { let eu: *tinfo = tichase(au.sub); if (eu != nil && eu.kind == tykind.TY_STR) { handled = true; - let alen: i32 = au.alen: i32; - let cnt: i32 = 0; - let last_ev: *node = nil; - let repeat: bool = false; - let e: *node = r.list; - for (e != nil && cnt < alen) { - if (e.kind == nkind.N_FIELD) { - if (streq(e.str, "...")) { - repeat = true; - break; - }; - }; - let ev: *node = e; - for (ev != nil && ev.kind == nkind.N_CAST) { - ev = ev.lhs; - }; - if (ev == nil) { break; }; - if (ev.kind != nkind.N_STRLIT) { break; }; - if (ev.str.len > 0) { - internstrlit(c, ev.str); - }; - last_ev = ev; - cnt += 1; - e = e.next; - }; - if (repeat && last_ev != nil) { - if (last_ev.str.len > 0) { - for (cnt < alen) { - internstrlit(c, last_ev.str); - cnt += 1; - }; - }; - }; + preinternstrarray(c, au, r); }; }; }; @@ -2074,9 +2107,11 @@ fn emitarraylitbytes(c: *cgen, arrt: *tinfo, rhs: *node, // pattern (DATAW header with a zero ptr placeholder + inline LE len, // then a per-element DATAR) at offset idx*esz. Each strlit was pre- // interned by letpreintern so its _S_ rodata row exists before this -// row's DATAR references it. Scoped to DATAW (writable `let`): A_DATAR -// requires a DATAW holder, so a read-only `def [N]str` can't carry the -// relocs (#18 follow-up). Returns false when the element type isn't str. +// row's DATAR references it. Always emits into DATAW (writable): A_DATAR +// requires a DATAW holder, so both `let` and a read-only `def [N]str` +// (#8/GAP-B) park their backing here — the section bit is the reloc- +// holder constraint, not a mutability grant (def immutability stays +// checker-enforced). Returns false when the element type isn't str. fn emitstrarraydata(c: *cgen, directive: str, name: str, module: str, arrt: *tinfo, rhs: *node) bool = { let au: *tinfo = arrt; @@ -2087,7 +2122,17 @@ fn emitstrarraydata(c: *cgen, directive: str, name: str, module: str, eu = tichase(eu); if (eu == nil) { return false; }; if (eu.kind != tykind.TY_STR) { return false; }; - if (!streq(directive, "DATAW")) { return false; }; + // #8/GAP-B: a str-element array's backing ALWAYS lives in DATAW + // (writable section), regardless of the caller's let/def directive — + // each element carries an A_DATAR ptr-reloc to its _S_ rodata row, and + // w6a requires a DATAR holder be a DATAW slot (asm.c:362). The passed + // directive ("DATA" for a def, "DATAW" for a let) is therefore IGNORED + // here; the emit below hardcodes DATAW. A `def [N]str` stays immutable + // — the checker rejects writes to a def; DATAW is only the reloc-holder + // placement, not a mutability grant (rule-8 placement detail). Pre-fix + // this gate skipped the def path → no DATA block → w6l undefined + // 'main.C' (#270 lineage; int-def is plain DATA, no holder constraint, + // so it was unaffected). let esz: i32 = au.sub.size: i32; let alen: i32 = au.alen: i32; diff --git a/selfhost/cmd/wcc/cgenexpr.ww b/selfhost/cmd/wcc/cgenexpr.ww index db4e76e3..5484a8fe 100644 --- a/selfhost/cmd/wcc/cgenexpr.ww +++ b/selfhost/cmd/wcc/cgenexpr.ww @@ -1881,6 +1881,17 @@ fn cgindex(c: *cgen, n: *node) void = { etn = idxelemtn(bl.tnode); } else { etn = idxelemtn(letvartnode(c, base.str)); + // #8/GAP-B: a def-global str/slice-array base lives + // in c.defs, not c.lets — letvartnode misses it → + // etn nil → elem mis-classified scalar, dropping the + // 3-word slice-header load (returns element ADDR not + // .len; cstage's Type-based classify loads the full + // header, the proven let form). defvartnode is the + // def-side sister — same fallback as the base-address + // resolution at cgenexpr.ww:1762. + if (etn == nil) { + etn = idxelemtn(defvartnode(c, base.str)); + }; }; // #60: an alias-NAMED base has no element tnode // (idxelemtn sees the N_TNAME leaf, nil) — classify diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index d4679c0e..7da2c9eb 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -23401,6 +23401,17 @@ fn cgindex(c: *cgen, n: *node) void = { etn = idxelemtn(bl.tnode); } else { etn = idxelemtn(letvartnode(c, base.str)); + // #8/GAP-B: a def-global str/slice-array base lives + // in c.defs, not c.lets — letvartnode misses it → + // etn nil → elem mis-classified scalar, dropping the + // 3-word slice-header load (returns element ADDR not + // .len; cstage's Type-based classify loads the full + // header, the proven let form). defvartnode is the + // def-side sister — same fallback as the base-address + // resolution at cgenexpr.ww:1762. + if (etn == nil) { + etn = idxelemtn(defvartnode(c, base.str)); + }; }; // #60: an alias-NAMED base has no element tnode // (idxelemtn sees the N_TNAME leaf, nil) — classify @@ -39618,6 +39629,48 @@ fn emitdatawbyte(b: u8) void = { emitbytes( bb.ptr, 1u64); }; +// preinternstrarray — SSoT for the #18 [N]str element-strlit intern +// ORDER (element order, then `...` repeat-fill). Shared by letpreintern's +// let arm and the #8/GAP-B def arm so both intern labels in the SAME +// order emitstrarraydata references them by — a divergent order would +// mis-pair the DATAR rows with their _S_ rodata. au is the chased +// TY_ARRAY tinfo, r the N_ARRLIT rhs; caller verified the element is str. +fn preinternstrarray(c: *cgen, au: *tinfo, r: *node) void = { + let alen: i32 = au.alen: i32; + let cnt: i32 = 0; + let last_ev: *node = nil; + let repeat: bool = false; + let e: *node = r.list; + for (e != nil && cnt < alen) { + if (e.kind == nkind.N_FIELD) { + if (streq(e.str, "...")) { + repeat = true; + break; + }; + }; + let ev: *node = e; + for (ev != nil && ev.kind == nkind.N_CAST) { + ev = ev.lhs; + }; + if (ev == nil) { break; }; + if (ev.kind != nkind.N_STRLIT) { break; }; + if (ev.str.len > 0) { + internstrlit(c, ev.str); + }; + last_ev = ev; + cnt += 1; + e = e.next; + }; + if (repeat && last_ev != nil) { + if (last_ev.str.len > 0) { + for (cnt < alen) { + internstrlit(c, last_ev.str); + cnt += 1; + }; + }; + }; +}; + // letpreintern — intern strlits referenced from top-level str-let // initialisers BEFORE emitdatasection runs. Mirrors cmd/w6c/cgen.c // let_pre_intern: emitletdataw later looks up the same label, and @@ -39628,6 +39681,29 @@ export fn letpreintern(c: *cgen, file: *node) void = { if (file == nil) { return; }; let d: *node = file.list; for (d != nil) { + // #8/GAP-B: a `def [N]str` needs the SAME element-strlit + // pre-interning as the let [N]str arm (the #18 ordering + // contract) so emitstrarraydata's DATAR rows find their _S_ + // rodata. letpreintern walked only N_LET; a def's labels were + // allocated too late (emitdefconstants pass) → dangling _S_. + // Str-array ONLY — def tuple/slice/tagged/scalar-str stay out + // of scope (#10/#270 / inline-Sdef). + if (d.kind == nkind.N_DEF) { + let dr: *node = d.rhs; + for (dr != nil && dr.kind == nkind.N_CAST) { + dr = dr.lhs; + }; + if (d.lhs != nil && dr != nil + && dr.kind == nkind.N_ARRLIT) { + let dau: *tinfo = tichase(d.lhs.type_: *tinfo); + if (dau != nil && dau.kind == tykind.TY_ARRAY) { + let deu: *tinfo = tichase(dau.sub); + if (deu != nil && deu.kind == tykind.TY_STR) { + preinternstrarray(c, dau, dr); + }; + }; + }; + }; if (d.kind == nkind.N_LET) { let r: *node = d.rhs; for (r != nil) { @@ -39650,39 +39726,7 @@ export fn letpreintern(c: *cgen, file: *node) void = { let eu: *tinfo = tichase(au.sub); if (eu != nil && eu.kind == tykind.TY_STR) { handled = true; - let alen: i32 = au.alen: i32; - let cnt: i32 = 0; - let last_ev: *node = nil; - let repeat: bool = false; - let e: *node = r.list; - for (e != nil && cnt < alen) { - if (e.kind == nkind.N_FIELD) { - if (streq(e.str, "...")) { - repeat = true; - break; - }; - }; - let ev: *node = e; - for (ev != nil && ev.kind == nkind.N_CAST) { - ev = ev.lhs; - }; - if (ev == nil) { break; }; - if (ev.kind != nkind.N_STRLIT) { break; }; - if (ev.str.len > 0) { - internstrlit(c, ev.str); - }; - last_ev = ev; - cnt += 1; - e = e.next; - }; - if (repeat && last_ev != nil) { - if (last_ev.str.len > 0) { - for (cnt < alen) { - internstrlit(c, last_ev.str); - cnt += 1; - }; - }; - }; + preinternstrarray(c, au, r); }; }; }; @@ -40342,9 +40386,11 @@ fn emitarraylitbytes(c: *cgen, arrt: *tinfo, rhs: *node, // pattern (DATAW header with a zero ptr placeholder + inline LE len, // then a per-element DATAR) at offset idx*esz. Each strlit was pre- // interned by letpreintern so its _S_ rodata row exists before this -// row's DATAR references it. Scoped to DATAW (writable `let`): A_DATAR -// requires a DATAW holder, so a read-only `def [N]str` can't carry the -// relocs (#18 follow-up). Returns false when the element type isn't str. +// row's DATAR references it. Always emits into DATAW (writable): A_DATAR +// requires a DATAW holder, so both `let` and a read-only `def [N]str` +// (#8/GAP-B) park their backing here — the section bit is the reloc- +// holder constraint, not a mutability grant (def immutability stays +// checker-enforced). Returns false when the element type isn't str. fn emitstrarraydata(c: *cgen, directive: str, name: str, module: str, arrt: *tinfo, rhs: *node) bool = { let au: *tinfo = arrt; @@ -40355,7 +40401,17 @@ fn emitstrarraydata(c: *cgen, directive: str, name: str, module: str, eu = tichase(eu); if (eu == nil) { return false; }; if (eu.kind != tykind.TY_STR) { return false; }; - if (!streq(directive, "DATAW")) { return false; }; + // #8/GAP-B: a str-element array's backing ALWAYS lives in DATAW + // (writable section), regardless of the caller's let/def directive — + // each element carries an A_DATAR ptr-reloc to its _S_ rodata row, and + // w6a requires a DATAR holder be a DATAW slot (asm.c:362). The passed + // directive ("DATA" for a def, "DATAW" for a let) is therefore IGNORED + // here; the emit below hardcodes DATAW. A `def [N]str` stays immutable + // — the checker rejects writes to a def; DATAW is only the reloc-holder + // placement, not a mutability grant (rule-8 placement detail). Pre-fix + // this gate skipped the def path → no DATA block → w6l undefined + // 'main.C' (#270 lineage; int-def is plain DATA, no holder constraint, + // so it was unaffected). let esz: i32 = au.sub.size: i32; let alen: i32 = au.alen: i32; diff --git a/test/wcc/819_def_str_table.c b/test/wcc/819_def_str_table.c new file mode 100644 index 00000000..2cde150b --- /dev/null +++ b/test/wcc/819_def_str_table.c @@ -0,0 +1,274 @@ +/* + * 819_def_str_table — `def C: [N]str = [...]` module-level str-array static + * init + element load (#8 / GAP-B, #270 family). A `let [N]str` global static- + * inits correctly (#18); the def-twin did NOT — both stages emitted the index + * ref `LEAQ main.C(SB)` but NEVER emitted the backing DATA block, so w6l failed + * with `undefined reference to 'main.C'` (LOUD, symmetric, both stages — not + * silent, not cs≠ww). + * + * ROOT: emit_strarray_data / emitstrarraydata (the str-array DATAW header + + * per-element A_DATAR ptr-reloc emitter) was gated to the "DATAW" directive + * (let only). A def array routes through emit_array_data(..,"DATA",..) so the + * gate skipped it → no DATA block. The str backing must live in DATAW anyway + * (w6a requires a DATAR reloc-holder be a DATAW slot, asm.c:362); def + * immutability is checker-enforced, independent of the section bit. The fix + * drops the directive gate in BOTH stages so a def str-array rides the same + * DATAW+DATAR emitter as let. int-defs are plain DATA (no reloc) — unaffected. + * + * row | shape | want + * -----------------+---------------------------------------------+------ + * def_str_len | def C:[2]str; C.len | 2 + * def_str_elem0 | def C:[2]str=["ab","cde"]; C[0].len | 2 + * def_str_elem1 | def C:[2]str=["ab","cde"]; C[1].len | 3 + * def_str_content | def C:[2]str=["ab","cde"]; C[1][0] ('c') | 99 (DATAR reloc + * | | resolves to the + * | | right _S_ rodata) + * def_str_multi | def C:[3]str=["a","bb","ccc"]; C[2].len | 3 + * def_str_varbind | def C:[3]str; let s=C[2]; s.len | 3 + * let_str_elem1 | let C:[2]str=["ab","cde"]; C[1].len (CTRL) | 3 (#18, already + * | | works; no-regr) + * + * Each def row fails-to-LINK pre-fix (mutation-sane: delete the gate-drop and + * the def rows go back to w6l undefined). let_str_elem1 is the #18 regression + * guard. A byte-id pass pins cstage==wwstage `.s` for every row (the both- + * wrong-identical convergence: pre-fix both omit the block, post-fix both emit + * the same DATAW+DATAR). + */ +#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; +} + +struct row { const char *label; const char *src; int want; }; + +static const struct row rows[] = { + /* def_str_len — `.len` on a def str-array (static element count; works + * via the def-twin .len arm even pre-#8, but pinned here for the set). */ + { "def_str_len", + "package main;\n" + "def C: [2]str = [\"ab\", \"cde\"];\n" + "export fn main() i32 = {\n" + "\treturn C.len: i32;\n" + "};\n", + 2 }, + + /* def_str_elem0 — THE regression: load element 0, read its .len. + * Pre-#8 the def DATA block is never emitted → w6l undefined main.C. */ + { "def_str_elem0", + "package main;\n" + "def C: [2]str = [\"ab\", \"cde\"];\n" + "export fn main() i32 = {\n" + "\treturn C[0].len: i32;\n" + "};\n", + 2 }, + + /* def_str_elem1 — element 1 .len (ken oracle row). */ + { "def_str_elem1", + "package main;\n" + "def C: [2]str = [\"ab\", \"cde\"];\n" + "export fn main() i32 = {\n" + "\treturn C[1].len: i32;\n" + "};\n", + 3 }, + + /* def_str_content — C[1][0] is 'c' (99). Proves the per-element A_DATAR + * ptr-reloc resolves to the right _S_ rodata row, not just that a block + * exists. */ + { "def_str_content", + "package main;\n" + "def C: [2]str = [\"ab\", \"cde\"];\n" + "export fn main() i32 = {\n" + "\treturn C[1][0]: i32;\n" + "};\n", + 99 }, + + /* def_str_multi — 3-element def array, element 2 .len. */ + { "def_str_multi", + "package main;\n" + "def C: [3]str = [\"a\", \"bb\", \"ccc\"];\n" + "export fn main() i32 = {\n" + "\treturn C[2].len: i32;\n" + "};\n", + 3 }, + + /* def_str_varbind — bind an element to a let, then read .len (ken + * matrix var-bind form: a full 24B str-header element copy). */ + { "def_str_varbind", + "package main;\n" + "def C: [3]str = [\"x\", \"yy\", \"zzz\"];\n" + "export fn main() i32 = {\n" + "\tlet s: str = C[2];\n" + "\treturn s.len: i32;\n" + "};\n", + 3 }, + + /* let_str_elem1 — CONTROL: the #18 let-global str-array (already works). + * Guards against the gate-drop regressing the let path. */ + { "let_str_elem1", + "package main;\n" + "let C: [2]str = [\"ab\", \"cde\"];\n" + "export fn main() i32 = {\n" + "\treturn C[1].len: i32;\n" + "};\n", + 3 }, +}; + +static int +run_driver(const char *driver, const struct row *r, int i) +{ + char src[64], tmpdir[64], cmd[1024]; + snprintf(src, sizeof src, "/tmp/dst_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/dst_%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 2>/dev/null", + tmpdir, driver, src); + if (runwait(cmd) != 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[128]; + 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); + return got; +} + +/* asm_byte_identical — w6c vs w6c_ww .s for the same source must match. */ +static int +asm_byte_identical(const char *bin, const struct row *r, int i) +{ + char src[64], cs[64], ws[64], cmd[1024]; + snprintf(src, sizeof src, "/tmp/dst_asm_%d_%d.ww", getpid(), i); + snprintf(cs, sizeof cs, "/tmp/dst_asm_%d_%d_c.s", getpid(), i); + snprintf(ws, sizeof ws, "/tmp/dst_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; + } + + FILE *fc = fopen(cs, "rb"); + FILE *fw = fopen(ws, "rb"); + int rc = 0; + if (!fc || !fw) { + rc = -1; + } else { + for (;;) { + int a = fgetc(fc); + int b = fgetc(fw); + if (a != b) { rc = -1; break; } + if (a == EOF) break; + } + } + if (fc) fclose(fc); + if (fw) fclose(fw); + 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[1024]; + 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[1024]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + char wdrv[1024]; + snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); + + struct { const char *name; const char *path; int gated_on_existence; } + 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_on_existence + && access(drivers[d].path, X_OK) != 0) { + fprintf(stderr, "def_str_table: skip %s (no %s)\n", + drivers[d].name, drivers[d].path); + continue; + } + for (int i = 0; i < n; i++) { + int got = run_driver(drivers[d].path, &rows[i], i); + total++; + if (got != rows[i].want) { + fprintf(stderr, + "def_str_table[%s][%s]: exit=%d want=%d\n", + drivers[d].name, rows[i].label, + got, rows[i].want); + fail++; + } + } + } + + if (access(wdrv, X_OK) == 0) { + for (int i = 0; i < n; i++) { + total++; + if (asm_byte_identical(bin, &rows[i], i) != 0) + fail++; + } + } + + if (fail) { + fprintf(stderr, + "def_str_table: %d/%d fixtures failed\n", fail, total); + return 1; + } + printf("def_str_table: %d/%d ok\n", total, total); + return 0; +}