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

@@ -20025,6 +20025,7 @@ fn dotbaseaddr(c: *cgen, base: *node, dstreg: str) bool = {
// 8 bytes as if it were a pointer-var — wrong shape (cstage
// sister fix in cg_dotbase_addr).
let lc: *local = localfindnode(c, inner.str);
let isglobal: bool = false;
if (lc == nil) {
let gt: *node = letvartnode(c, base.str);
if (gt != nil && gt.kind == nkind.N_TARRAY) {
@@ -20035,7 +20036,12 @@ fn dotbaseaddr(c: *cgen, base: *node, dstreg: str) bool = {
emitline("\n");
return true;
};
return false;
// #249 (sibling of #135): inner is a module-GLOBAL struct value
// (let/def), not a local — lc is nil but inner.type_ is a valid
// struct. Resolve the field below and emit a global base (LEAQ
// name(SB)). A non-struct inner (e.g. an SK_USE module qualifier,
// type ty_err) falls through the struct gate to `return false`.
isglobal = true;
};
let bu: *tinfo = inner.type_: *tinfo;
for (bu != nil && bu.kind == tykind.TY_NAMED) { bu = bu.under; };
@@ -20072,7 +20078,8 @@ fn dotbaseaddr(c: *cgen, base: *node, dstreg: str) bool = {
for (ft != nil && ft.kind == tykind.TY_NAMED) { ft = ft.under; };
if (ft == nil) { return false; };
if (ft.kind != tykind.TY_ARRAY) { return false; };
let innoff: i64 = lc.off: i64;
let innoff: i64 = 0;
if (lc != nil) { innoff = lc.off: i64; };
if (viaptr) {
emitline("\tMOVQ\t");
emitoff(innoff);
@@ -20086,6 +20093,21 @@ fn dotbaseaddr(c: *cgen, base: *node, dstreg: str) bool = {
emitline(dstreg);
emitline("\n");
};
} else if (isglobal) {
// #249: LEAQ name(SB) + field offset. Mirror cstage
// cg_dotbase_addr's global value-struct arm.
emitline("\tLEAQ\t");
emitsymname(c, inner.str);
emitline("(SB), ");
emitline(dstreg);
emitline("\n");
if (foff != 0) {
emitline("\tADDQ\t$");
emitint(foff);
emitline(", ");
emitline(dstreg);
emitline("\n");
};
} else {
emitline("\tLEAQ\t");
emitoff(innoff + foff);