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

@@ -24835,7 +24835,7 @@ fn cgident(c: *cgen, n: *node) void = {
let mov: str = "MOVSD";
if (isf32type(c, n)) { mov = "MOVSS"; };
emitline("\tLEAQ\t");
emitsymnamehint(c, nm, c.curmod);
emitfnname(c, nm, c.curmod);
emitline("(SB), CX\n");
emitline("\t");
emitline(mov);
@@ -24843,7 +24843,7 @@ fn cgident(c: *cgen, n: *node) void = {
return;
};
emitline("\tMOVQ\t");
emitsymnamehint(c, nm, c.curmod);
emitfnname(c, nm, c.curmod);
emitline("(SB), AX\n");
return;
};
@@ -24893,7 +24893,7 @@ fn cgident(c: *cgen, n: *node) void = {
// is overwritten by the cap as the last step, after
// ptr/len are already loaded (#1/Phase 3).
emitline("\tLEAQ\t");
emitsymnamehint(c, nm, c.curmod);
emitfnname(c, nm, c.curmod);
emitline("(SB), CX\n");
emitline("\tMOVQ\t(CX), AX\n");
emitline("\tMOVQ\t8(CX), BX\n");
@@ -24926,7 +24926,7 @@ fn cgident(c: *cgen, n: *node) void = {
let mov: str = "MOVSD";
if (is32) { mov = "MOVSS"; };
emitline("\tLEAQ\t");
emitsymnamehint(c, nm, c.curmod);
emitfnname(c, nm, c.curmod);
emitline("(SB), CX\n");
emitline("\t");
emitline(mov);
@@ -24942,11 +24942,11 @@ fn cgident(c: *cgen, n: *node) void = {
let glop: str = localloadop(c, lvtnode);
if (streq(glop, "MOVQ")) {
emitline("\tMOVQ\t");
emitsymnamehint(c, nm, c.curmod);
emitfnname(c, nm, c.curmod);
emitline("(SB), AX\n");
} else {
emitline("\tLEAQ\t");
emitsymnamehint(c, nm, c.curmod);
emitfnname(c, nm, c.curmod);
emitline("(SB), CX\n");
emitline("\t");
emitline(glop);
@@ -26588,7 +26588,7 @@ fn cgmatch(c: *cgen, n: *node) void = {
let gspill: i32 = matchspillsz(c, gtt);
scrutoff = localalloc(c, "@match_spill", gspill, nil);
emitline("\tLEAQ\t");
emitsymnamehint(c, scrut.str, c.curmod);
emitfnname(c, scrut.str, c.curmod);
emitline("(SB), AX\n");
let gk: i32 = 0;
for (gk < gspill) {
@@ -27993,11 +27993,11 @@ fn cgdot(c: *cgen, n: *node) void = {
// threads lhs.str via emitfnname.
if (streq(mqop, "MOVQ")) {
emitline("\tMOVQ\t");
emitsymnamehint(c, fld, usehint(c, lhs.str));
emitfnname(c, fld, usehint(c, lhs.str));
emitline("(SB), AX\n");
} else {
emitline("\tLEAQ\t");
emitsymnamehint(c, fld, usehint(c, lhs.str));
emitfnname(c, fld, usehint(c, lhs.str));
emitline("(SB), CX\n");
emitline("\t");
emitline(mqop);
@@ -28889,7 +28889,7 @@ fn cgun(c: *cgen, n: *node) void = {
// &aa.v takes aa's global, not a
// same-leaf collision.
emitline("\tLEAQ\t");
emitsymnamehint(c, fld, usehint(c, basenm));
emitfnname(c, fld, usehint(c, basenm));
emitline("(SB), AX\n");
return;
};
@@ -29167,7 +29167,7 @@ fn cgstreqpush(c: *cgen, op: *node) void = {
let lc: *local = localfindnode(c, nm);
if (lc == nil && isletvar(c, nm)) {
emitline("\tLEAQ\t");
emitsymnamehint(c, nm, c.curmod);
emitfnname(c, nm, c.curmod);
emitline("(SB), BX\n");
emitline("\tMOVQ\t8(BX), AX\n");
emitline("\tPUSHQ\tAX\n");
@@ -31019,7 +31019,7 @@ fn cgcall(c: *cgen, n: *node) void = {
// mis-resolved.
if (isletvar(c, a.str)) {
emitline("\tLEAQ\t");
emitsymnamehint(c, a.str, c.curmod);
emitfnname(c, a.str, c.curmod);
emitline("(SB), CX\n");
emitline("\tMOVQ\t8(CX), AX\n");
return;
@@ -42935,7 +42935,7 @@ fn emitfloatlitdata(c: *cgen, directive: str, name: str, module: str,
};
emitline(directive);
emitline(" ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline("(SB),\"");
// IEEE-754 sign-bit XOR for negation happens INSIDE the emit
// loop on the top byte only — equivalent to a whole-u64 XOR with
@@ -43144,7 +43144,7 @@ fn emitstructdata(c: *cgen, directive: str, name: str, module: str,
if (su.kind != tykind.TY_STRUCT) { return false; };
emitline(directive);
emitline(" ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline("(SB),\"");
emitstructlitbytes(c, structt, rhs, 0u64);
emitline("\"\n");
@@ -43528,7 +43528,7 @@ fn emitstrarraydata(c: *cgen, directive: str, name: str, module: str,
};
emitline("DATAW ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline("(SB),\"");
let idx: i32 = 0;
e = rhs.list;
@@ -43580,7 +43580,7 @@ fn emitstrarraydata(c: *cgen, directive: str, name: str, module: str,
if (ev.str.len > 0) {
let lab: str = internstrlit(c, ev.str);
emitline("DATAR ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline("+");
emitint((idx * esz): i64);
emitline("(SB),");
@@ -43594,7 +43594,7 @@ fn emitstrarraydata(c: *cgen, directive: str, name: str, module: str,
if (repeat && last_ev != nil && last_ev.str.len > 0) {
let lab: str = internstrlit(c, last_ev.str);
emitline("DATAR ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline("+");
emitint((idx * esz): i64);
emitline("(SB),");
@@ -43624,7 +43624,7 @@ fn emitarraydata(c: *cgen, directive: str, name: str, module: str,
let total: u64 = arrt.size;
emitline(directive);
emitline(" ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline("(SB),\"");
let i: u64 = 0u64;
for (i < total) { emitdatawbyte(0u8); i = i + 1u64; };
@@ -43639,7 +43639,7 @@ fn emitarraydata(c: *cgen, directive: str, name: str, module: str,
if (!emitarraylitbytes(c, arrt, rhs, 0)) { return false; };
emitline(directive);
emitline(" ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline("(SB),\"");
emitarraylitbytes(c, arrt, rhs, 1);
emitline("\"\n");
@@ -43717,7 +43717,7 @@ fn emitslicedata(c: *cgen, name: str, module: str, slt: *tinfo,
};
// Backing: k rows, bytes (one DATAW) then per-row relocs.
emitline("DATAW ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline(".d(SB),\"");
e2 = rhs.list;
for (e2 != nil) {
@@ -43758,7 +43758,7 @@ fn emitslicedata(c: *cgen, name: str, module: str, slt: *tinfo,
};
// Writable backing data.
emitline("DATAW ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline(".d(SB),\"");
emitarraylitbytes(c, arrt, rhs, 1);
emitline("\"\n");
@@ -43766,7 +43766,7 @@ fn emitslicedata(c: *cgen, name: str, module: str, slt: *tinfo,
// 24B header: ptr placeholder + LE len + LE cap (both = k). Word
// sizes from the type table (rule 13).
emitline("DATAW ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline("(SB),\"");
let i: i32 = 0;
let ptrsz: i32 = primtypesize("uintptr"): i32;
@@ -43789,9 +43789,9 @@ fn emitslicedata(c: *cgen, name: str, module: str, slt: *tinfo,
emitline("\"\n");
// Patch the ptr word with the backing VA.
emitline("DATAR ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline("+0(SB),");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline(".d(SB)\n");
};
@@ -43854,7 +43854,7 @@ fn emittaggeddata(c: *cgen, name: str, module: str, tt: *node, rhs: *node, sz: i
if (tt == nil) { return false; };
if (rhs == nil) {
emitline("DATAW ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline("(SB),\"");
emittaggedbytes(c, tt.type_: *tinfo, rhs, sz, 1);
emitline("\"\n");
@@ -43887,7 +43887,7 @@ fn emittaggeddata(c: *cgen, name: str, module: str, tt: *node, rhs: *node, sz: i
if (r.kind != nkind.N_STRLIT) { return false; };
let lv: u64 = r.str.len: u64;
emitline("DATAW ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline("(SB),\"");
// tag@0
acc = tag: u64;
@@ -43911,7 +43911,7 @@ fn emittaggeddata(c: *cgen, name: str, module: str, tt: *node, rhs: *node, sz: i
if (r.str.len > 0) {
let lab: str = internstrlit(c, r.str);
emitline("DATAR ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline("+8(SB),");
emitbytes( lab.ptr, lab.len: u64);
emitline("(SB)\n");
@@ -43923,7 +43923,7 @@ fn emittaggeddata(c: *cgen, name: str, module: str, tt: *node, rhs: *node, sz: i
// leaving a half-written DATAW.
if (!emittaggedbytes(c, tt.type_: *tinfo, rhs, sz, 0)) { return false; };
emitline("DATAW ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline("(SB),\"");
emittaggedbytes(c, tt.type_: *tinfo, rhs, sz, 1);
emitline("\"\n");
@@ -44087,7 +44087,7 @@ fn emittuplerowrelocs(c: *cgen, name: str, module: str, backing: bool, rowoff: i
if (ev.str.len > 0) {
let lab: str = internstrlit(c, ev.str);
emitline("DATAR ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
if (backing) { emitline(".d"); };
emitline("+");
emitint(foff: i64);
@@ -44102,7 +44102,7 @@ fn emittuplerowrelocs(c: *cgen, name: str, module: str, backing: bool, rowoff: i
// with the fn's TEXT VA via emitfnname.
if (nodefnptr(c, ev)) {
emitline("DATAR ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
if (backing) { emitline(".d"); };
emitline("+");
emitint(foff: i64);
@@ -44145,7 +44145,7 @@ fn emittupledata(c: *cgen, name: str, module: str, tt: *node, rhs: *node) bool =
p0 = p0.next;
};
emitline("DATAW ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline("(SB),\"");
let zi: i32 = 0;
for (zi < zsz) { emitdatawbyte(0u8); zi += 1; };
@@ -44155,7 +44155,7 @@ fn emittupledata(c: *cgen, name: str, module: str, tt: *node, rhs: *node) bool =
if (rhs.kind != nkind.N_TUPLE) { return false; };
if (!tuplerowfoldable(c, tt, rhs)) { return false; };
emitline("DATAW ");
emitsymnamehint(c, name, module);
emitfnname(c, name, module);
emitline("(SB),\"");
emittuplerowbytes(c, tt, rhs);
emitline("\"\n");
@@ -44293,13 +44293,13 @@ fn emitletdataw(c: *cgen, file: *node) void = {
};
if (fnp) {
emitline("DATAW ");
emitsymnamehint(c, nm, d.nmod);
emitfnname(c, nm, d.nmod);
emitline("(SB),\"");
let zi: i32 = 0;
for (zi < 8) { emitdatawbyte(0u8); zi += 1; };
emitline("\"\n");
emitline("DATAR ");
emitsymnamehint(c, nm, d.nmod);
emitfnname(c, nm, d.nmod);
emitline("+0(SB),");
// #124: cross-module `&mod.fn` mangles the
// leaf with the MODULE ident; same-module `&fn`
@@ -44312,7 +44312,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
emitline("(SB)\n");
} else if (ok) {
emitline("DATAW ");
emitsymnamehint(c, nm, d.nmod);
emitfnname(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
let n: u64 = v;
@@ -44353,7 +44353,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
let lab: str = internstrlit(c, r.str);
let v: u64 = r.str.len: u64;
emitline("DATAW ");
emitsymnamehint(c, nm, d.nmod);
emitfnname(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
for (i < 8) { emitdatawbyte(0u8); i += 1; };
@@ -44366,7 +44366,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
};
emitline("\"\n");
emitline("DATAR ");
emitsymnamehint(c, nm, d.nmod);
emitfnname(c, nm, d.nmod);
emitline("+0(SB),");
emitbytes( lab.ptr, lab.len: u64);
emitline("(SB)\n");
@@ -44385,7 +44385,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
};
if (ok) {
emitline("DATAW ");
emitsymnamehint(c, nm, d.nmod);
emitfnname(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
let szstr: i32 = primtypesize("str"): i32;
@@ -44424,7 +44424,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
};
if (ok) {
emitline("DATAW ");
emitsymnamehint(c, nm, d.nmod);
emitfnname(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
let szsl: i32 = tyslicesize(): i32;
@@ -44453,7 +44453,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
let zsz: i32 = sz;
if (dti != nil) { zsz = dti.size: i32; };
emitline("DATAW ");
emitsymnamehint(c, nm, d.nmod);
emitfnname(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
for (i < zsz) {
@@ -44596,7 +44596,7 @@ fn emitdefconstants(c: *cgen, file: *node) void = {
// that motivated the divergence is gone), so the asm
// surface is unchanged on the corpus.
emitline("DATA ");
emitsymnamehint(c, d.str, d.nmod);
emitfnname(c, d.str, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
let n: u64 = v;
@@ -45085,28 +45085,26 @@ fn collectmods(c: *cgen, file: *node) void = {
};
};
};
// §7-A / #53: exported non-fn decls (def/type/let) path-qualify
// like fns — `export def MAX` becomes `<mod>.MAX`, not a bare
// `MAX` two packages could clash on under sep-compile. No export
// guard: every decl with a module mangles identically.
if (d.kind == nkind.N_DEF) {
if (d.exported == 0) {
if (d.nmod.len > 0) {
let m: *modent = alloc(modent{mname=d.str, nmod=d.nmod, omod=d.nmod, mnext=c.mods})!;
c.mods = m;
};
if (d.nmod.len > 0) {
let m: *modent = alloc(modent{mname=d.str, nmod=d.nmod, omod=d.nmod, mnext=c.mods})!;
c.mods = m;
};
};
if (d.kind == nkind.N_TYPEDECL) {
if (d.exported == 0) {
if (d.nmod.len > 0) {
let m: *modent = alloc(modent{mname=d.str, nmod=d.nmod, omod=d.nmod, mnext=c.mods})!;
c.mods = m;
};
if (d.nmod.len > 0) {
let m: *modent = alloc(modent{mname=d.str, nmod=d.nmod, omod=d.nmod, mnext=c.mods})!;
c.mods = m;
};
};
if (d.kind == nkind.N_LET) {
if (d.exported == 0) {
if (d.nmod.len > 0) {
let m: *modent = alloc(modent{mname=d.str, nmod=d.nmod, omod=d.nmod, mnext=c.mods})!;
c.mods = m;
};
if (d.nmod.len > 0) {
let m: *modent = alloc(modent{mname=d.str, nmod=d.nmod, omod=d.nmod, mnext=c.mods})!;
c.mods = m;
};
};
d = d.next;
@@ -45179,39 +45177,6 @@ fn modlookupforfn(c: *cgen, name: str, hint: str) str = {
return first;
};
// modlookupvalue — value-global variant: mangle ONLY on an exact
// (name, hint) match; otherwise empty so the name stays bare. Unlike
// modlookupforfn there is NO first-leaf-match fallback — exported value
// globals are export-skipped from c.mods (modcollect keeps their bare-
// name data ABI), 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). Mirrors cstage mod_lookup_value.
//
// 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 (like 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.
fn modlookupvalue(c: *cgen, name: str, hint: str) str = {
let empty: str;
empty.ptr = nil;
empty.len = 0;
if (hint.len == 0) { return empty; };
let m: *modent = c.mods;
for (m != nil) {
if (streq(m.mname, name)) {
if (m.nmod.len > 0 && streq(m.nmod, hint)) {
return m.nmod;
};
};
m = m.mnext;
};
return empty;
};
// emitsymname — write the asm symbol name for `ident`. Honours, in
// order: FFI mapping (@symbol), module mangling (private decls), bare
// name. Use everywhere a top-level non-fn name is emitted before `(SB)`
@@ -45252,32 +45217,6 @@ fn emitfnname(c: *cgen, ident: str, hint: str) void = {
emitbytes( ident.ptr, ident.len: u64);
};
// emitsymnamehint — write the asm symbol name for a value-global
// `ident`, threading `hint` the way emitfnname does for fns.
// emitsymname's non-hinted modlookup grabs 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). Pass c.curmod at a bare reference, the
// decl's own module (d.nmod) at a definition label. Routes through
// modlookupvalue (exact-or-bare) so an exported global stays bare
// instead of mis-mangling onto another module's same-leaf private
// global; kept distinct from emitfnname to leave the fn-mangle path
// byte-for-byte untouched.
fn emitsymnamehint(c: *cgen, ident: str, hint: str) void = {
let resolved: str = ffiresolve(c, ident);
if (resolved.ptr != ident.ptr) {
emitbytes( resolved.ptr, resolved.len: u64);
return;
};
let mod: str = modlookupvalue(c, ident, hint);
if (mod.len > 0) {
emitbytes( mod.ptr, mod.len: u64);
emitbytes( ".".ptr, 1u64);
};
emitbytes( ident.ptr, ident.len: u64);
};
// ---- FFI map ---------------------------------------------------------
fn fficollect(c: *cgen, file: *node) void = {