wcc/ww: path-qualify exported decls + drop exact-or-bare value mangle (#53)

Under M1 mangling, EXPORTED non-fn decls (let/def/type) skipped path-
qualification and emitted a BARE symbol (`types.I64_MAX` -> `I64_MAX`).
Under separate compilation two packages exporting the same data leaf
would then collide at w6l. Masked in-tree only because no two packages
export the same non-fn leaf.

§7-A (USER-locked, harec's model): path-qualify EVERY exported decl
(fn AND data) at the single mangle choke-point — mod_collect /
collectmods. Retire the `!isfn && d->export` (cstage) and `exported==0`
(wwstage) skips: every decl with a module now mangles `<mod>.<name>`.
The ONLY bare symbols left are @symbol FFI overrides (ffi_resolve at
emit) and the ROOT unit's `main` — both already carved out before the
map insert.

With exported decls in the map the exact-(name,hint)-or-bare value
dance is dead — its sole purpose was the bare-exported case. Delete
mod_lookup_value / mod_mangle_value / mahint (cstage) and
modlookupvalue / emitsymnamehint (wwstage); the value-global sites now
route through the same hint-aware-with-fallback lookup as fns
(mod_mangle_fn/mafn, emitfnname). Net negative LOC in the mangler.

Transparent rename on the live combined path: ref and def move in
lockstep, so cs==ww byte-id holds and the self-host still builds + runs
(fixed-point/995). Byte-id REBASELINE — all 5 ww binaries shift. The
w6c/wwdump combined.ww embed wcc cgen and are regenerated.
This commit is contained in:
2026-06-16 01:48:28 +09:00
parent f77739b1de
commit 0c4a5ecea0
6 changed files with 324 additions and 439 deletions

View File

@@ -1428,7 +1428,10 @@ decl_has_ffisym(Node *d)
return 0;
}
/* Skip rule = {@symbol, main, empty-module}. Do NOT skip on `export` for fns.
/* Skip rule = {@symbol, root-main, empty-module}. Do NOT skip on `export`:
* every exported decl (fn AND data) path-qualifies, so cross-package exports
* can't collide under separate compilation (§7-A / #53). The only bare
* symbols are @symbol FFI overrides and the ROOT unit's `main`.
* Both stages must match exactly — ww2/ww3/ww4 byte-identity depends on it. */
static void
mod_collect(Cg *c, Node *file)
@@ -1451,12 +1454,10 @@ mod_collect(Cg *c, Node *file)
int track = isfn || (d->kind == N_TYPEDECL)
|| (d->kind == N_DEF) || (d->kind == N_LET);
if (!track) continue;
/* Non-fn decls (let/def/type) still skip exported entries —
* their export-side namespace is the user-facing data ABI
* and mangling them changes the surface. Fns mangle
* unconditionally so cross-module same-leaf exports
* (os.read vs io.read) coexist at link time. */
if (!isfn && d->export) continue;
/* §7-A / #53: exported non-fn decls (let/def/type) path-qualify
* like fns — `export def MAX` becomes `<mod>.MAX`, not a bare
* `MAX` that two packages could clash on under sep-compile. No
* export skip: every decl with a module mangles identically. */
if (d->module == NULL || d->module[0] == '\0') continue;
if (decl_has_ffisym(d)) continue;
/* `main` is the linker entry-point convention. Even when not
@@ -1474,11 +1475,10 @@ mod_collect(Cg *c, Node *file)
}
}
/* Returns the originating module for a name, or NULL if the name
* isn't a registered private decl. By-name only — works for non-fn
* refs (let/def/type) where the mod_collect skip rule keeps each leaf
* unique across the program. Fn refs go through mod_lookup_for_fn
* since multiple modules can now export the same fn leaf. */
/* Returns the originating module for a name, or NULL if the name isn't a
* registered decl. By-name first-match — used at non-fn ref sites with an
* unambiguous leaf. Hint-disambiguated refs (and value globals where two
* modules could share a leaf) go through mod_lookup_for_fn. */
static const char *
mod_lookup(const char *name)
{
@@ -1510,36 +1510,6 @@ mod_lookup_for_fn(const char *name, const char *hint)
return first;
}
/* Value-global variant: mangle ONLY on an exact (name, hint) match;
* otherwise return NULL so the name stays bare. Unlike the fn variant
* there is NO first-leaf-match fallback — exported value globals are
* export-skipped from mod_map (mod_collect keeps their bare-name data
* ABI, see the skip at `!isfn && d->export`), so a first-match fallback
* would mis-mangle an exported `v` onto another module's private `v`
* (#1 cgen value-global module-qualifier, the cgen residual of #55).
* Bare-on-miss is correct: a missing entry means the leaf is either an
* exported global (its own bare symbol) or not module-private at all.
*
* HONEST BOUNDARY (rule 7) — do NOT "fix" the following into a
* workaround: if two modules BOTH export the same value leaf, both stay
* bare and the linker sees a duplicate symbol. That is a CORRECT, loud,
* link-time ABI clash (identical to C's two-extern-same-name rule), NOT
* a silent miscompile. A bare reference can never legitimately resolve
* to another module's PRIVATE global, so first-match is never wanted on
* the value path; the only ambiguity left is genuine duplicate exports,
* which belong to the linker, not to a cgen disambiguation heuristic. */
static const char *
mod_lookup_value(const char *name, const char *hint)
{
if (hint == NULL) return NULL;
for (Mod *m = mod_map; m; m = m->next) {
if (strcmp(m->name, name) != 0) continue;
if (m->module != NULL && strcmp(m->module, hint) == 0)
return m->module;
}
return NULL;
}
/* Collect every top-level `let` whose declared type we can store
* in a single .data slot. Names not in this map fall through to
* the old "drop assignment" path; with a clear link-time
@@ -1739,20 +1709,6 @@ mod_mangle_fn(Cg *c, const char *ident, const char *hint)
return mod_join(c, mod, ident);
}
/* Value-global flavoured mangle: same shape as mod_mangle_fn but over
* mod_lookup_value (exact-(ident,hint)-or-bare, no first-match
* fallback). See mod_lookup_value for why value globals can't share the
* fn fallback. */
static const char *
mod_mangle_value(Cg *c, const char *ident, const char *hint)
{
const char *resolved = ffi_resolve(ident);
if (resolved != ident) return resolved;
const char *mod = mod_lookup_value(ident, hint);
if (mod == NULL) return ident;
return mod_join(c, mod, ident);
}
/* Forward decl — masym below depends on asym defined further down. */
static Adr asym(const char *s);
@@ -1771,22 +1727,6 @@ mafn(Cg *c, const char *ident, const char *hint)
return asym(mod_mangle_fn(c, ident, hint));
}
/* Value-global address builder. masym's non-hinted mod_lookup picks
* the first leaf-name match, so two modules with a same-leaf value
* global (`let v` in both) collapse onto one DATA label and a bare
* cross-module read resolves to the wrong module (#1 cgen value-global
* module-qualifier, the cgen residual of #55). Thread a per-site hint
* the way mafn does — curmod at a bare reference, the decl's own module
* at the definition label — over the same module-generic decl map.
* Kept distinct from mafn (vs renamed) to leave the fn-mangle path
* byte-for-byte untouched. Routes through mod_mangle_value (exact-or-
* bare) so an exported global stays bare instead of mis-mangling onto
* another module's same-leaf private global. */
static Adr
mahint(Cg *c, const char *ident, const char *hint)
{
return asym(mod_mangle_value(c, ident, hint));
}
void
cg_init(Cg *c, Arena *a)
@@ -4248,7 +4188,7 @@ cgexpr(Cg *c, Node *n, Local *locals)
* third 8B (cap); the address holder CX gets
* overwritten by the cap as the last step, after
* we no longer need it (#1/Phase 3). */
ins2(c, A_LEAQ, mahint(c, n->str, c->cur_mod),
ins2(c, A_LEAQ, mafn(c, n->str, c->cur_mod),
areg(D_CX));
ins2(c, A_MOVQ, amem(D_CX, 0), areg(D_AX));
ins2(c, A_MOVQ, amem(D_CX, 8), areg(D_BX));
@@ -4266,7 +4206,7 @@ cgexpr(Cg *c, Node *n, Local *locals)
* (#129 Phase A.1 LOAD-side twin of the
* emit_floatlit_data DATA-side SSoT). */
int op = type_isf32(n->type) ? A_MOVSS : A_MOVSD;
ins2(c, A_LEAQ, mahint(c, n->str, c->cur_mod),
ins2(c, A_LEAQ, mafn(c, n->str, c->cur_mod),
areg(D_CX));
ins2(c, op, amem(D_CX, 0), areg(D_X0));
goto ident_done;
@@ -4301,14 +4241,14 @@ cgexpr(Cg *c, Node *n, Local *locals)
int gop = let_islet(n->str)
? localloadop(n->type) : A_MOVQ;
if (gop == A_MOVQ) {
ins2(c, A_MOVQ, mahint(c, n->str, c->cur_mod),
ins2(c, A_MOVQ, mafn(c, n->str, c->cur_mod),
areg(D_AX));
} else {
/* w6a has no MOVSXD/MOVSWQ/MOVSBQ D_EXTERN
* source form, so route through a LEAQ scratch
* the same way top-level str/slice/float lets
* do. */
ins2(c, A_LEAQ, mahint(c, n->str, c->cur_mod),
ins2(c, A_LEAQ, mafn(c, n->str, c->cur_mod),
areg(D_CX));
ins2(c, gop, amem(D_CX, 0), areg(D_AX));
}
@@ -4394,7 +4334,7 @@ cgexpr(Cg *c, Node *n, Local *locals)
* &aa.v takes aa's global, not a
* same-leaf collision. */
ins2(c, A_LEAQ,
mahint(c, opnd->str,
mafn(c, opnd->str,
use_hint(c->cur_mod, opnd->lhs->str)),
areg(D_AX));
break;
@@ -7934,7 +7874,7 @@ cgexpr(Cg *c, Node *n, Local *locals)
* same-module same-leaf global isn't
* mis-resolved. */
ins2(c, A_LEAQ,
mahint(c, a->str, c->cur_mod),
mafn(c, a->str, c->cur_mod),
areg(D_CX));
ins2(c, A_MOVQ, amem(D_CX, 8),
areg(D_AX));
@@ -10295,7 +10235,7 @@ cgexpr(Cg *c, Node *n, Local *locals)
* `is`/case-let binds read the same scratch. */
sl_off = localoff(c, &locals, "@match_spill",
slot_size, cg_frame);
ins2(c, A_LEAQ, mahint(c, s->str, c->cur_mod),
ins2(c, A_LEAQ, mafn(c, s->str, c->cur_mod),
areg(D_AX));
for (int k = 0; k < slot_size; k += 8) {
ins2(c, A_MOVQ, amem(D_AX, k), areg(D_DX));
@@ -11172,11 +11112,11 @@ cgexpr(Cg *c, Node *n, Local *locals)
* branch above already uses n->lhs->str via mafn. */
if (mqop == A_MOVQ) {
ins2(c, A_MOVQ,
mahint(c, n->str, use_hint(c->cur_mod, n->lhs->str)),
mafn(c, n->str, use_hint(c->cur_mod, n->lhs->str)),
areg(D_AX));
} else {
ins2(c, A_LEAQ,
mahint(c, n->str, use_hint(c->cur_mod, n->lhs->str)),
mafn(c, n->str, use_hint(c->cur_mod, n->lhs->str)),
areg(D_CX));
ins2(c, mqop, amem(D_CX, 0), areg(D_AX));
}
@@ -15370,7 +15310,7 @@ emit_floatlit_data(FILE *out, Cg *c, const char *directive,
}
}
fprintf(out, "%s %s(SB),\"", directive,
mod_mangle_value(c, name, module));
mod_mangle_fn(c, name, module));
/* IEEE-754 sign-bit XOR for negation happens INSIDE the emit
* loop on the top byte only — semantically identical to a whole-
* u64 XOR with 2^63 (or 2^31 for f32) but never materialises
@@ -15560,7 +15500,7 @@ emit_struct_data(FILE *out, Cg *c, const char *directive,
Type *u = type_chase_named(t);
if (u == NULL || u->kind != TY_STRUCT) return 0;
fprintf(out, "%s %s(SB),\"", directive,
mod_mangle_value(c, name, module));
mod_mangle_fn(c, name, module));
emit_struct_lit_bytes(out, c, t, rhs, 0);
fputs("\"\n", out);
return 1;
@@ -15890,7 +15830,7 @@ emit_strarray_data(FILE *out, Cg *c, const char *directive,
cnt++;
}
const char *sym = mod_mangle_value(c, name, module);
const char *sym = mod_mangle_fn(c, name, module);
fprintf(out, "DATAW %s(SB),\"", sym);
int idx = 0;
for (Node *e = rhs->list; e && idx < alen; e = e->next) {
@@ -15998,7 +15938,7 @@ emit_tagged_data(FILE *out, Cg *c, const char *name, const char *module,
Type *u = type_chase_named(t);
if (u == NULL || u->kind != TY_TAGGED || u->nullable) return 0;
int sz = (int)u->size;
const char *sym = mod_mangle_value(c, name, module);
const char *sym = mod_mangle_fn(c, name, module);
if (rhs == NULL) {
fprintf(out, "DATAW %s(SB),\"", sym);
emit_tagged_bytes(out, u, rhs, sz, 1);
@@ -16193,7 +16133,7 @@ emit_tuple_data(FILE *out, Cg *c, const char *name, const char *module,
{
Type *u = type_unwrap(t);
if (u == NULL || u->kind != TY_TUPLE) return 0;
const char *sym = mod_mangle_value(c, name, module);
const char *sym = mod_mangle_fn(c, name, module);
if (rhs == NULL) {
fprintf(out, "DATAW %s(SB),\"", sym);
for (int i = 0; i < (int)u->size; i++)
@@ -16225,7 +16165,7 @@ emit_array_data(FILE *out, Cg *c, const char *directive,
return 1;
if (!emit_array_lit_bytes(out, c, t, rhs, 0)) return 0;
fprintf(out, "%s %s(SB),\"", directive,
mod_mangle_value(c, name, module));
mod_mangle_fn(c, name, module));
emit_array_lit_bytes(out, c, t, rhs, 1);
fputs("\"\n", out);
return 1;
@@ -16274,7 +16214,7 @@ emit_slice_data(FILE *out, Cg *c, const char *directive, const char *name,
"length in a slice literal (#10, rule 7)");
k++;
}
const char *sym = mod_mangle_value(c, name, module);
const char *sym = mod_mangle_fn(c, name, module);
const char *bk = aprintf(c->a, "%s.d", sym);
if (eu && eu->kind == TY_TUPLE) {
/* #117 aggregate-element arm: k tuple rows. Validate every row
@@ -16431,7 +16371,7 @@ emit_lets(Cg *c, FILE *out, Node *file)
{
const char *fsym = node_fnptr_sym(c, r);
if (fsym != NULL) {
const char *sym = mod_mangle_value(c,
const char *sym = mod_mangle_fn(c,
d->str, d->module);
fprintf(out, "DATAW %s(SB),\"", sym);
for (int i = 0; i < 8; i++)
@@ -16452,7 +16392,7 @@ emit_lets(Cg *c, FILE *out, Node *file)
if (!fold_int_literal(r, &v)) continue;
}
emit_data_row(out, "DATAW",
mod_mangle_value(c, d->str, d->module), v);
mod_mangle_fn(c, d->str, d->module), v);
continue;
}
/* Strip leading casts on the rhs so a `nil: str` etc.
@@ -16470,7 +16410,7 @@ emit_lets(Cg *c, FILE *out, Node *file)
if (sz == (int)ty_str->size && r != NULL && r->kind == N_STRLIT
&& r->strlen > 0) {
const char *lab = intern_strlit(c, r->str, r->strlen);
const char *sym = mod_mangle_value(c, d->str, d->module);
const char *sym = mod_mangle_fn(c, d->str, d->module);
u64 v = r->strlen;
/* 16-byte payload: 8 zero placeholder + LE len. */
fprintf(out, "DATAW %s(SB),\"", sym);
@@ -16526,7 +16466,7 @@ emit_lets(Cg *c, FILE *out, Node *file)
if (r->kind != N_NIL && !empty_str) continue;
}
emit_data_row_zero(out, "DATAW",
mod_mangle_value(c, d->str, d->module), sz);
mod_mangle_fn(c, d->str, d->module), sz);
}
}
@@ -16555,7 +16495,7 @@ emit_defs(Cg *c, FILE *out, Node *file)
u64 v;
if (fold_int_literal(d->rhs, &v)) {
fprintf(out, "DATA %s(SB),\"",
mod_mangle_value(c, d->str, d->module));
mod_mangle_fn(c, d->str, d->module));
for (int i = 0; i < 8; i++) {
unsigned b = (unsigned)((v >> (i * 8)) & 0xff);
if (b == '"' || b == '\\')