wcc: address-of symbol-bearing def + cross-module module-qual (#149)

Widen cgaddr / N_UN TK_AMP for addressable globals — the address-of
twin of A.2/A.3's value-LOAD widening (which covered cgexpr N_DOT /
cgindex but never the &-path, so &<mod>.<def> emitted uninitialized-AX
garbage). Two shapes, one class:
- Shape 1 (&G, N_IDENT def): LEAQ masym(G) for struct/array/scalar
  defs. Scalar gate = emit_defs/emit_floatlit_data eligibility exactly
  (fold_int_literal OR rhs-peels-to-N_FLOATLIT) so the addressable set
  equals the symbol-bearing set — never LEAQs a missing symbol. Reuses
  DefStruct/DefArray registries (A.2/A.3); new def_isscalardef/DefAny.
- Shape 2 (&mod.G, N_DOT module-qual): LEAQ leaf (TY_FN -> mafn),
  mirroring the value-READ resolution (cgen.c:6043). Kind-agnostic —
  fixes &mod.def, &mod.let, &mod.scalar, &mod.func. This is fold-4's
  &math.f64info pattern.
Non-addressable def (str-def, computed-float-def) -> loud error both
stages; upgrades #147 &NAN(0.0/0.0) from silent wild-deref to loud
compile error. Computed-float-def symbol emission split to #147
(needs float-const-fold; not needed by gamma/fold-4). wwstage Shape-2
gate carries !deflookup so a def base routes to silent-drop matching
cstage's type-gate (rule-10 parity).

Unblocks gamma-cleanup (#40) + strconv fold-4 (&math.f64info).
Bootstrap-NEUTRAL (no &global in lib/selfhost; only &local). Test 921
(12 rows: same-pkg struct/array/scalar-int/scalar-float + cross-pkg
def_struct/let_struct/scalar_let/func + regression &let/&arr[i] +
2 loud-reject). Make test: 184/184 incl 990-997 byte-id +
combined_ww_fresh.

Followups: #147 (computed-float-def symbol), #152/#153 (pre-existing
ptr-param element read divergences, dodged by 921), #154 (address-of
def subparts &def.field/&def_array[i]).
This commit is contained in:
2026-05-27 10:27:55 +09:00
parent 0546bda6c4
commit db7523e0e7
7 changed files with 748 additions and 1 deletions

View File

@@ -2385,6 +2385,54 @@ fn deflookuprhsmod(c: *cgen, name: str, mod: str) *node = {
return deflookuprhs(c, name);
};
// #149: rhs peels (N_CAST / unary ±) to a float literal — the exact
// shape emitfloatlitdata (cgen.ww) emits a DATA symbol for. The scalar-
// float address-of gate must equal that emission set, or `&def` LEAQs a
// symbol the data pass never wrote. Keep in sync with emitfloatlitdata's
// peel.
fn floatlitleaf(rhs: *node) bool = {
let r: *node = rhs;
for (r != nil) {
if (r.kind != nkind.N_CAST) { break; };
r = r.lhs;
};
if (r != nil) {
if (r.kind == nkind.N_UN) {
if (r.op == tkind.TK_MINUS) {
r = r.lhs;
for (r != nil) { if (r.kind != nkind.N_CAST) { break; }; r = r.lhs; };
} else { if (r.op == tkind.TK_PLUS) {
r = r.lhs;
for (r != nil) { if (r.kind != nkind.N_CAST) { break; }; r = r.lhs; };
}; };
};
};
if (r == nil) { return false; };
return r.kind == nkind.N_FLOATLIT;
};
// #149/#147: a top-level def is addressable for `&def` iff emitdefs emits
// a DATA symbol for it — struct, array, scalar int (foldintliteral), or
// scalar float whose rhs peels to a FLOATLIT. Gate held identical to
// cstage def_is{struct,array,scalar}def so the addressable set matches
// byte-for-byte (rule 10). str defs and computed-rhs floats (#147
// `def NAN = 0.0/0.0`) have no symbol and are excluded → routed to the
// loud error, never a LEAQ of a missing symbol. `opnd` is the `&`-operand
// N_IDENT; its checker-stamped type_ carries the def's type (same as the
// cgident float-def read at cgenexpr.ww).
fn defisaddressable(c: *cgen, opnd: *node) bool = {
let nm: str = opnd.str;
if (defvarstructinfo(c, nm) != nil) { return true; };
let dtn: *node = defvartnode(c, nm);
if (dtn != nil) { if (dtn.kind == nkind.N_TARRAY) { return true; }; };
let drhs: *node = deflookuprhs(c, nm);
if (drhs == nil) { return false; };
let v: u64 = 0u64;
if (foldintliteral(drhs, &v)) { return true; };
if (isfloattype(c, opnd)) { if (floatlitleaf(drhs)) { return true; }; };
return false;
};
// ---- module-private symbol map --------------------------------------
//
// Every non-FFI top-level fn decl lives in its module's namespace —