wcc/cgen: #8 def str-array element load — emit + pre-intern def-twin + ww load (both stages)
def C:[N]str; C[i] was loud (undefined main.C) both stages. Three folded fixes, one commit (splitting would ship a bisect point where wwstage silently returns an element address instead of .len): P0: the str-array static-init emitter dropped its vestigial directive =="DATAW" gate so a def table rides the same DATAW-header + DATAR-reloc path as let. A def str/slice table lives in DATAW by w6a's A_DATAR-holder constraint -- placement only; def immutability stays checker-enforced. P1: let_pre_intern / letpreintern walked N_LET only, so a def str-array's element string-literals were never interned (dangling _S_n). Extracted a pre_intern_strarray SSoT helper, called for a def str-array arm too, both stages. Scoped to str fixed arrays; def []T / def [N][]T stay loud (#270). P2: wwstage cgenexpr lacked a defvartnode fallback in the indexed-element classify, so a def str-array element load returned the element address instead of the slice header -- a silent miscompile. One line, aligning wwstage up to cstage (which was correct). C[1].len now = 3 both stages, byte-identical. byte-id 990-997 8/8; w6c/w6c_ww move. test/wcc/819 table-driven. The def-global scalar str index sibling (def S:str; S[0]) stays task #14.
This commit is contained in:
113
cmd/w6c/cgen.c
113
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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user