w6c+wwstage: read array field of a global struct (#249 BUG B)

Reading an array-typed field of a module-global struct value (`G.arr[i]`)
silently miscompiled: the N_INDEX fallback's cg_dotbase_addr (cstage) /
dotbaseaddr (wwstage) helper — the #135 sibling that computes &(s.field)
for a `[N]T` field — had no module-global-struct base arm. cstage emitted
`LEAQ (BP)` (localfind returns 0 for a global, so it read the stack frame
→ 0); wwstage's localfindnode returned nil and the fallback keyed on the
FIELD name, so it returned false and the caller's cgexpr(N_DOT) loaded the
field VALUE as a pointer → SEGFAULT. The .data was already correct
(emit_struct_lit_bytes #129 A.3); only the READ base address was wrong.

Both stages now emit `LEAQ name(SB) (+ ADDQ field_off)` for a global
value-struct base, mirroring the scalar global-field read (cgen.c:7532);
const globals resolve via def_isstructdef. Symmetric both stages (rule
10), byte-identical .s. Unblocks base64's `const std_encoding.encmap[i]`
reads (#22).

Test 949_structlit_arrfield_run: global `let`/`def` struct array-field
read, cstage run + cs==ww byte-id.
This commit is contained in:
2026-06-02 01:12:40 +09:00
parent e3f49234f9
commit 16b519465a
6 changed files with 273 additions and 6 deletions

View File

@@ -1754,10 +1754,20 @@ cg_dotbase_addr(Cg *c, Node *base, int dst_reg, Local *locals)
if (ft == NULL || ft->kind != TY_ARRAY) return 0;
int inner_off = localfind(locals, inner->str);
int foff = (int)f->offset;
/* #249 (sibling of #135): a module-GLOBAL struct value base. localfind
* returns 0 for a global, so the BP-rel form below would emit `LEAQ
* (BP)` (read the stack frame, not the global). Resolve the same way
* the scalar N_DOT global-field read does (cgen.c:7532) — LEAQ
* name(SB) + field offset. const globals are def_isstructdef. */
if (viaptr) {
ins2(c, A_MOVQ, amem(D_BP, inner_off), areg(dst_reg));
if (foff != 0)
ins2(c, A_ADDQ, aimm(foff), areg(dst_reg));
} else if (inner_off == 0 && (let_islet(inner->str)
|| def_isstructdef(inner->str))) {
ins2(c, A_LEAQ, masym(c, inner->str), areg(dst_reg));
if (foff != 0)
ins2(c, A_ADDQ, aimm(foff), areg(dst_reg));
} else {
ins2(c, A_LEAQ, amem(D_BP, inner_off + foff),
areg(dst_reg));