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

@@ -722,6 +722,19 @@ struct DefArray {
};
static DefArray *defarrays;
/* #149: every top-level `def`, regardless of kind. Backs the address-of
* path's is-any-def check (loud error on `&<non-addressable def>`) and
* the scalar-addressable gate. Mirrors wwstage collectdefs / deflookup,
* which already track all N_DEF. */
typedef struct DefAny DefAny;
struct DefAny {
const char *name;
Type *type;
Node *rhs;
DefAny *next;
};
static DefAny *defall;
/* Slot size for a top-level `let` of type t, or 0 if the type isn't
* supported as a writable global yet. Tagged unions are deferred.
* enums route through their storage type.
@@ -926,6 +939,7 @@ let_collect(Cg *c, Node *file)
letvars = NULL;
defstructs = NULL;
defarrays = NULL;
defall = NULL;
if (file == NULL) return;
for (Node *d = file->list; d; d = d->next) {
if (d->kind == N_LET) {
@@ -940,6 +954,15 @@ let_collect(Cg *c, Node *file)
}
if (d->kind == N_DEF) {
if (d->str == NULL || d->str[0] == '\0') continue;
/* #149: track every def (any kind) so the address-of
* path can tell a def from an unknown ident and loud-
* error on `&<non-addressable def>`. */
DefAny *dn = amalloc(c->a, sizeof *dn);
dn->name = d->str;
dn->type = d->type;
dn->rhs = d->rhs;
dn->next = defall;
defall = dn;
/* #129 A.2: struct-typed defs now have DATA storage
* (emit_defs struct arm); register them so the N_DOT
* struct-let LEAQ-and-offset shape widens to cover
@@ -987,6 +1010,54 @@ def_isarraydef(const char *name)
return 0;
}
/* #149: rhs peels (N_CAST / unary ±) to a float literal — the exact
* shape emit_floatlit_data (cgen.c) emits a DATA symbol for. The scalar-
* def address-of gate MUST equal that emission set, or `&def` LEAQs a
* symbol the data pass never wrote. Keep in sync with the peel inside
* emit_floatlit_data. */
static int
floatlit_leaf(Node *rhs)
{
Node *r = rhs;
while (r != NULL && r->kind == N_CAST) r = r->lhs;
if (r != NULL && r->kind == N_UN
&& (r->op == TK_MINUS || r->op == TK_PLUS)) {
r = r->lhs;
while (r != NULL && r->kind == N_CAST) r = r->lhs;
}
return r != NULL && r->kind == N_FLOATLIT;
}
/* #149/#147: a scalar (int/float) def is addressable iff emit_defs emits
* a DATA symbol for it — int via fold_int_literal, float via the
* FLOATLIT-leaf shape. Gate is held identical to emit_defs's emission
* gate so the addressable set matches byte-for-byte. Computed-rhs floats
* (`def NAN = 0.0/0.0`, #147) fold to no symbol and are excluded → they
* route to the address-of loud error, never a LEAQ of a missing sym. */
static int
def_isscalardef(const char *name)
{
if (name == NULL) return 0;
for (DefAny *dn = defall; dn; dn = dn->next) {
if (strcmp(dn->name, name) != 0) continue;
if (dn->rhs == NULL) return 0;
u64 v;
if (fold_int_literal(dn->rhs, &v)) return 1;
if (let_isfloat(dn->type) && floatlit_leaf(dn->rhs)) return 1;
return 0;
}
return 0;
}
static int
def_isanydef(const char *name)
{
if (name == NULL) return 0;
for (DefAny *dn = defall; dn; dn = dn->next)
if (strcmp(dn->name, name) == 0) return 1;
return 0;
}
static int
let_islet(const char *name)
{
@@ -2229,13 +2300,60 @@ cgexpr(Cg *c, Node *n, Local *locals)
int off = localfind(locals, opnd->str);
if (off != 0) {
ins2(c, A_LEAQ, amem(D_BP, off), areg(D_AX));
} else if (let_islet(opnd->str)) {
} else if (let_islet(opnd->str)
|| def_isstructdef(opnd->str)
|| def_isarraydef(opnd->str)
|| def_isscalardef(opnd->str)) {
/* #149/#147: address-of a top-level def
* with DATA storage. emit_defs / emit_
* struct_data / emit_array_data all emit
* to mod_mangle(name), so the address is
* the same LEAQ name(SB) as a let. The
* address-of twin of A.2/A.3's LOAD-side
* widening. */
ins2(c, A_LEAQ, masym(c, opnd->str),
areg(D_AX));
} else if (def_isanydef(opnd->str)) {
/* #149/#147 rule-7: the name IS a def but
* has no DATA symbol (str def inlined, or
* computed-rhs float like `def NAN =
* 0.0/0.0`). Loud, not a wild deref. */
fatal("cannot take address of non-"
"addressable def '%s': no DATA symbol "
"(str/computed-rhs def; #149/#147)",
opnd->str);
}
break;
}
if (opnd && opnd->kind == N_DOT) {
/* #149 Shape 2: `&mod.G` — module-qualified
* address-of of an exported global (let or def).
* The checker leaves SK_USE module idents untyped
* (NULL/ty_err); detect that and LEAQ the leaf
* symbol. Kind-agnostic (covers cross-module &let
* / &def / &scalar) — the address-of twin of the
* value-read mod-qual path below. A TY_FN leaf
* resolves via mafn (fn address), mirroring the
* read path's TY_FN branch. Placed before the
* spine walk, which aborts on the untyped base
* anyway. */
if (opnd->lhs && opnd->lhs->kind == N_IDENT
&& (opnd->lhs->type == NULL
|| opnd->lhs->type == ty_err)) {
Type *lt = opnd->type;
Type *lu = (lt && lt->kind == TY_NAMED)
? lt->under : lt;
if (lu && lu->kind == TY_FN)
ins2(c, A_LEAQ,
mafn(c, opnd->str,
opnd->lhs->str),
areg(D_AX));
else
ins2(c, A_LEAQ,
masym(c, opnd->str),
areg(D_AX));
break;
}
/* Address-of through a DOT chain. The early-exit
* above handled `&ident` and `&base[i]`; everything
* else was silently dropped. Three shapes converge