w6c+wwstage: qualify cross-module value-global by defining module (#1)

A bare cross-module value-global load mis-qualified its symbol: cgen
mangled it with curmod via a non-preferring leaf lookup, so an exported
`let v` in module aa emitted both its DATA storage AND its bare-load as
main.v, colliding with main's private v. aa.getv() returned 99, not 7.
Functions were already correct (they thread a cur_mod hint via mafn /
emitfnname); value-globals did not. Both stages emitted IDENTICAL wrong
asm, so the byte-id gate was blind to it; combined.ww (frontend) is clean
-- the bug is purely in cgen. This is the cgen residual of #55 (#1 cgen
value-global module-qualifier).

Fix, symmetric in cmd/w6c/cgen.c + selfhost/cmd/wcc/{cgen,cgenexpr}.ww:
reference-site mangle uses the resolved module (curmod-prefer for bare
idents); definition/DATA-site mangle uses the decl's own module
(d->module / d.nmod) -- threaded per-site the way fns already do, via
mahint / emitsymnamehint. The fn-mangle path is left byte-for-byte
untouched.

Deviation from the signed-off spec (ratified by rob-pike after this
finding): the spec prescribed reusing the fn lookup (mod_mangle_fn /
modlookupforfn), but its first-match fallback mis-fires for value-
globals -- mod_collect export-skips exported non-fn decls (cgen.c:1059)
to keep their bare-name data ABI, so an exported leaf is absent from the
module map and the fallback grabs another module's same-leaf private
global. The value path therefore uses a distinct exact-(name,module)-or-
bare lookup (mod_lookup_value / modlookupvalue): mangle only on an exact
match, else stay bare. Byte-id-neutral on all existing single-owner code;
exported globals stay bare (ABI preserved), private stay module-qualified.

Honest boundary (rule 7): if two modules BOTH export the same value leaf,
both stay bare and the linker sees a duplicate symbol -- a correct, loud,
link-time ABI clash (like C), NOT a silent miscompile; left to the
linker, not papered over with a cgen heuristic.

Test: test/wcc/795_xmod_valglobal_run.c -- runtime (the exported global
read returns its own value, not the colliding private one) + cs==ww
byte-id, across i32-let / def-const / f64-let. Sibling to the checker
test 794_xmod_ident_prefer, which deliberately omitted byte-id because
this cgen bug diverged the asm independently.
This commit is contained in:
2026-06-01 08:43:02 +09:00
parent 954badd28f
commit 8481a05c3a
7 changed files with 575 additions and 101 deletions

View File

@@ -19353,7 +19353,7 @@ fn cgident(c: *cgen, n: *node) void = {
let mov: str = "MOVSD";
if (isf32type(c, n)) { mov = "MOVSS"; };
emitline("\tLEAQ\t");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), CX\n");
emitline("\t");
emitline(mov);
@@ -19361,7 +19361,7 @@ fn cgident(c: *cgen, n: *node) void = {
return;
};
emitline("\tMOVQ\t");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), AX\n");
return;
};
@@ -19393,7 +19393,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");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), CX\n");
emitline("\tMOVQ\t(CX), AX\n");
emitline("\tMOVQ\t8(CX), BX\n");
@@ -19412,7 +19412,7 @@ fn cgident(c: *cgen, n: *node) void = {
let mov: str = "MOVSD";
if (isf32type(c, lv.tnode)) { mov = "MOVSS"; };
emitline("\tLEAQ\t");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), CX\n");
emitline("\t");
emitline(mov);
@@ -19428,11 +19428,11 @@ fn cgident(c: *cgen, n: *node) void = {
let glop: str = localloadop(c, lvtnode);
if (streq(glop, "MOVQ")) {
emitline("\tMOVQ\t");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), AX\n");
} else {
emitline("\tLEAQ\t");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), CX\n");
emitline("\t");
emitline(glop);
@@ -29849,7 +29849,7 @@ export fn letpreintern(c: *cgen, file: *node) void = {
// an IEEE-754 sign-bit XOR (bit 63 f64, bit 31 f32) to avoid pulling
// f64/f32 bitcast helpers into cgen. Returns true on emit, false if
// rhs doesn't reduce to a foldable float literal.
fn emitfloatlitdata(c: *cgen, directive: str, name: str,
fn emitfloatlitdata(c: *cgen, directive: str, name: str, module: str,
sz: i32, rhs: *node) bool = {
let isf32: bool = (sz == 4);
let bits: u64 = 0u64;
@@ -29898,7 +29898,7 @@ fn emitfloatlitdata(c: *cgen, directive: str, name: str,
};
emitline(directive);
emitline(" ");
emitsymname(c, name);
emitsymnamehint(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
@@ -30084,7 +30084,7 @@ fn emitstructlitbytes(c: *cgen, structt: *tinfo, rhs: *node,
// emitstructdata — top-level wrapper. Opens the DATA/DATAW directive
// then delegates to emitstructlitbytes. Shared between emitletdataw
// struct arm and emitdefconstants struct arm (#129 A.2).
fn emitstructdata(c: *cgen, directive: str, name: str,
fn emitstructdata(c: *cgen, directive: str, name: str, module: str,
structt: *tinfo, rhs: *node) bool = {
let su: *tinfo = structt;
for (su != nil && su.kind == tykind.TY_NAMED) { su = su.under; };
@@ -30092,7 +30092,7 @@ fn emitstructdata(c: *cgen, directive: str, name: str,
if (su.kind != tykind.TY_STRUCT) { return false; };
emitline(directive);
emitline(" ");
emitsymname(c, name);
emitsymnamehint(c, name, module);
emitline("(SB),\"");
emitstructlitbytes(c, structt, rhs, 0u64);
emitline("\"\n");
@@ -30371,7 +30371,7 @@ fn emitarraylitbytes(c: *cgen, arrt: *tinfo, rhs: *node,
// behavior (the old loop emitted zeros when `elems` was nil); the
// refactor would have skipped emit entirely without this branch,
// causing `undefined reference to strconv.f64tos_buf` at link.
fn emitarraydata(c: *cgen, directive: str, name: str,
fn emitarraydata(c: *cgen, directive: str, name: str, module: str,
arrt: *tinfo, rhs: *node) bool = {
let au: *tinfo = arrt;
for (au != nil && au.kind == tykind.TY_NAMED) { au = au.under; };
@@ -30381,7 +30381,7 @@ fn emitarraydata(c: *cgen, directive: str, name: str,
let total: u64 = arrt.size;
emitline(directive);
emitline(" ");
emitsymname(c, name);
emitsymnamehint(c, name, module);
emitline("(SB),\"");
let i: u64 = 0u64;
for (i < total) { emitdatawbyte(0u8); i = i + 1u64; };
@@ -30391,7 +30391,7 @@ fn emitarraydata(c: *cgen, directive: str, name: str,
if (!emitarraylitbytes(c, arrt, rhs, 0)) { return false; };
emitline(directive);
emitline(" ");
emitsymname(c, name);
emitsymnamehint(c, name, module);
emitline("(SB),\"");
emitarraylitbytes(c, arrt, rhs, 1);
emitline("\"\n");
@@ -30414,8 +30414,8 @@ fn emitletdataw(c: *cgen, file: *node) void = {
// (#129 Phase A.1, rule-12). Bare-call
// discards the bool return (mirrors
// cgen.ww:723 fmt.fprintln pattern).
emitfloatlitdata(c, "DATAW", nm, fsz,
d.rhs);
emitfloatlitdata(c, "DATAW", nm, d.nmod,
fsz, d.rhs);
};
// #129 A.2: struct-typed let with N_STRUCTLIT rhs
// routes through the emitstructdata SSoT helper.
@@ -30428,7 +30428,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
if (r.kind == nkind.N_STRUCTLIT) {
let st: *tinfo = d.lhs.type_: *tinfo;
emitstructdata(c, "DATAW", nm,
st, r);
d.nmod, st, r);
};
};
};
@@ -30459,7 +30459,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
};
if (ok) {
emitline("DATAW ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
let n: u64 = v;
@@ -30493,7 +30493,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
let lab: str = internstrlit(c, r.str);
let v: u64 = r.str.len: u64;
emitline("DATAW ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
for (i < 8) { emitdatawbyte(0u8); i += 1; };
@@ -30506,7 +30506,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
};
emitline("\"\n");
emitline("DATAR ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("+0(SB),");
emitbytes( lab.ptr, lab.len: u64);
emitline("(SB)\n");
@@ -30525,7 +30525,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
};
if (ok) {
emitline("DATAW ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
let szstr: i32 = primtypesize("str"): i32;
@@ -30556,7 +30556,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
};
if (ok) {
emitline("DATAW ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
let szsl: i32 = tyslicesize(): i32;
@@ -30574,7 +30574,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
if (issg) {
if (d.rhs == nil) {
emitline("DATAW ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
for (i < sz) {
@@ -30607,7 +30607,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
if (route) {
let at: *tinfo = d.lhs.type_: *tinfo;
emitarraydata(c, "DATAW", nm,
at, rh);
d.nmod, at, rh);
};
};
};
@@ -30651,7 +30651,7 @@ fn emitdefconstants(c: *cgen, file: *node) void = {
};
if (dfsz > 0) {
emitfloatlitdata(c, "DATA", d.str,
dfsz, d.rhs);
d.nmod, dfsz, d.rhs);
} else {
// #129 A.2: struct-typed def with N_STRUCTLIT
// rhs. The checker stamps d.lhs.type_ with the
@@ -30667,7 +30667,7 @@ fn emitdefconstants(c: *cgen, file: *node) void = {
if (su != nil) {
if (su.kind == tykind.TY_STRUCT) {
emitstructdata(c, "DATA",
d.str, st, r);
d.str, d.nmod, st, r);
};
};
};};
@@ -30682,7 +30682,7 @@ fn emitdefconstants(c: *cgen, file: *node) void = {
if (au != nil) {
if (au.kind == tykind.TY_ARRAY) {
emitarraydata(c, "DATA",
d.str, at, r);
d.str, d.nmod, at, r);
};
};
};};
@@ -30699,7 +30699,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 ");
emitsymname(c, d.str);
emitsymnamehint(c, d.str, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
let n: u64 = v;
@@ -31218,6 +31218,39 @@ 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)`
@@ -31258,6 +31291,32 @@ 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 = {

View File

@@ -1259,7 +1259,7 @@ export fn letpreintern(c: *cgen, file: *node) void = {
// an IEEE-754 sign-bit XOR (bit 63 f64, bit 31 f32) to avoid pulling
// f64/f32 bitcast helpers into cgen. Returns true on emit, false if
// rhs doesn't reduce to a foldable float literal.
fn emitfloatlitdata(c: *cgen, directive: str, name: str,
fn emitfloatlitdata(c: *cgen, directive: str, name: str, module: str,
sz: i32, rhs: *node) bool = {
let isf32: bool = (sz == 4);
let bits: u64 = 0u64;
@@ -1308,7 +1308,7 @@ fn emitfloatlitdata(c: *cgen, directive: str, name: str,
};
emitline(directive);
emitline(" ");
emitsymname(c, name);
emitsymnamehint(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
@@ -1494,7 +1494,7 @@ fn emitstructlitbytes(c: *cgen, structt: *tinfo, rhs: *node,
// emitstructdata — top-level wrapper. Opens the DATA/DATAW directive
// then delegates to emitstructlitbytes. Shared between emitletdataw
// struct arm and emitdefconstants struct arm (#129 A.2).
fn emitstructdata(c: *cgen, directive: str, name: str,
fn emitstructdata(c: *cgen, directive: str, name: str, module: str,
structt: *tinfo, rhs: *node) bool = {
let su: *tinfo = structt;
for (su != nil && su.kind == tykind.TY_NAMED) { su = su.under; };
@@ -1502,7 +1502,7 @@ fn emitstructdata(c: *cgen, directive: str, name: str,
if (su.kind != tykind.TY_STRUCT) { return false; };
emitline(directive);
emitline(" ");
emitsymname(c, name);
emitsymnamehint(c, name, module);
emitline("(SB),\"");
emitstructlitbytes(c, structt, rhs, 0u64);
emitline("\"\n");
@@ -1781,7 +1781,7 @@ fn emitarraylitbytes(c: *cgen, arrt: *tinfo, rhs: *node,
// behavior (the old loop emitted zeros when `elems` was nil); the
// refactor would have skipped emit entirely without this branch,
// causing `undefined reference to strconv.f64tos_buf` at link.
fn emitarraydata(c: *cgen, directive: str, name: str,
fn emitarraydata(c: *cgen, directive: str, name: str, module: str,
arrt: *tinfo, rhs: *node) bool = {
let au: *tinfo = arrt;
for (au != nil && au.kind == tykind.TY_NAMED) { au = au.under; };
@@ -1791,7 +1791,7 @@ fn emitarraydata(c: *cgen, directive: str, name: str,
let total: u64 = arrt.size;
emitline(directive);
emitline(" ");
emitsymname(c, name);
emitsymnamehint(c, name, module);
emitline("(SB),\"");
let i: u64 = 0u64;
for (i < total) { emitdatawbyte(0u8); i = i + 1u64; };
@@ -1801,7 +1801,7 @@ fn emitarraydata(c: *cgen, directive: str, name: str,
if (!emitarraylitbytes(c, arrt, rhs, 0)) { return false; };
emitline(directive);
emitline(" ");
emitsymname(c, name);
emitsymnamehint(c, name, module);
emitline("(SB),\"");
emitarraylitbytes(c, arrt, rhs, 1);
emitline("\"\n");
@@ -1824,8 +1824,8 @@ fn emitletdataw(c: *cgen, file: *node) void = {
// (#129 Phase A.1, rule-12). Bare-call
// discards the bool return (mirrors
// cgen.ww:723 fmt.fprintln pattern).
emitfloatlitdata(c, "DATAW", nm, fsz,
d.rhs);
emitfloatlitdata(c, "DATAW", nm, d.nmod,
fsz, d.rhs);
};
// #129 A.2: struct-typed let with N_STRUCTLIT rhs
// routes through the emitstructdata SSoT helper.
@@ -1838,7 +1838,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
if (r.kind == nkind.N_STRUCTLIT) {
let st: *tinfo = d.lhs.type_: *tinfo;
emitstructdata(c, "DATAW", nm,
st, r);
d.nmod, st, r);
};
};
};
@@ -1869,7 +1869,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
};
if (ok) {
emitline("DATAW ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
let n: u64 = v;
@@ -1903,7 +1903,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
let lab: str = internstrlit(c, r.str);
let v: u64 = r.str.len: u64;
emitline("DATAW ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
for (i < 8) { emitdatawbyte(0u8); i += 1; };
@@ -1916,7 +1916,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
};
emitline("\"\n");
emitline("DATAR ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("+0(SB),");
emitbytes( lab.ptr, lab.len: u64);
emitline("(SB)\n");
@@ -1935,7 +1935,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
};
if (ok) {
emitline("DATAW ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
let szstr: i32 = primtypesize("str"): i32;
@@ -1966,7 +1966,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
};
if (ok) {
emitline("DATAW ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
let szsl: i32 = tyslicesize(): i32;
@@ -1984,7 +1984,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
if (issg) {
if (d.rhs == nil) {
emitline("DATAW ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
for (i < sz) {
@@ -2017,7 +2017,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
if (route) {
let at: *tinfo = d.lhs.type_: *tinfo;
emitarraydata(c, "DATAW", nm,
at, rh);
d.nmod, at, rh);
};
};
};
@@ -2061,7 +2061,7 @@ fn emitdefconstants(c: *cgen, file: *node) void = {
};
if (dfsz > 0) {
emitfloatlitdata(c, "DATA", d.str,
dfsz, d.rhs);
d.nmod, dfsz, d.rhs);
} else {
// #129 A.2: struct-typed def with N_STRUCTLIT
// rhs. The checker stamps d.lhs.type_ with the
@@ -2077,7 +2077,7 @@ fn emitdefconstants(c: *cgen, file: *node) void = {
if (su != nil) {
if (su.kind == tykind.TY_STRUCT) {
emitstructdata(c, "DATA",
d.str, st, r);
d.str, d.nmod, st, r);
};
};
};};
@@ -2092,7 +2092,7 @@ fn emitdefconstants(c: *cgen, file: *node) void = {
if (au != nil) {
if (au.kind == tykind.TY_ARRAY) {
emitarraydata(c, "DATA",
d.str, at, r);
d.str, d.nmod, at, r);
};
};
};};
@@ -2109,7 +2109,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 ");
emitsymname(c, d.str);
emitsymnamehint(c, d.str, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
let n: u64 = v;
@@ -2628,6 +2628,39 @@ 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)`
@@ -2668,6 +2701,32 @@ 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 = {

View File

@@ -740,7 +740,7 @@ fn cgident(c: *cgen, n: *node) void = {
let mov: str = "MOVSD";
if (isf32type(c, n)) { mov = "MOVSS"; };
emitline("\tLEAQ\t");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), CX\n");
emitline("\t");
emitline(mov);
@@ -748,7 +748,7 @@ fn cgident(c: *cgen, n: *node) void = {
return;
};
emitline("\tMOVQ\t");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), AX\n");
return;
};
@@ -780,7 +780,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");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), CX\n");
emitline("\tMOVQ\t(CX), AX\n");
emitline("\tMOVQ\t8(CX), BX\n");
@@ -799,7 +799,7 @@ fn cgident(c: *cgen, n: *node) void = {
let mov: str = "MOVSD";
if (isf32type(c, lv.tnode)) { mov = "MOVSS"; };
emitline("\tLEAQ\t");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), CX\n");
emitline("\t");
emitline(mov);
@@ -815,11 +815,11 @@ fn cgident(c: *cgen, n: *node) void = {
let glop: str = localloadop(c, lvtnode);
if (streq(glop, "MOVQ")) {
emitline("\tMOVQ\t");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), AX\n");
} else {
emitline("\tLEAQ\t");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), CX\n");
emitline("\t");
emitline(glop);

View File

@@ -19353,7 +19353,7 @@ fn cgident(c: *cgen, n: *node) void = {
let mov: str = "MOVSD";
if (isf32type(c, n)) { mov = "MOVSS"; };
emitline("\tLEAQ\t");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), CX\n");
emitline("\t");
emitline(mov);
@@ -19361,7 +19361,7 @@ fn cgident(c: *cgen, n: *node) void = {
return;
};
emitline("\tMOVQ\t");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), AX\n");
return;
};
@@ -19393,7 +19393,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");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), CX\n");
emitline("\tMOVQ\t(CX), AX\n");
emitline("\tMOVQ\t8(CX), BX\n");
@@ -19412,7 +19412,7 @@ fn cgident(c: *cgen, n: *node) void = {
let mov: str = "MOVSD";
if (isf32type(c, lv.tnode)) { mov = "MOVSS"; };
emitline("\tLEAQ\t");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), CX\n");
emitline("\t");
emitline(mov);
@@ -19428,11 +19428,11 @@ fn cgident(c: *cgen, n: *node) void = {
let glop: str = localloadop(c, lvtnode);
if (streq(glop, "MOVQ")) {
emitline("\tMOVQ\t");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), AX\n");
} else {
emitline("\tLEAQ\t");
emitsymname(c, nm);
emitsymnamehint(c, nm, c.curmod);
emitline("(SB), CX\n");
emitline("\t");
emitline(glop);
@@ -29849,7 +29849,7 @@ export fn letpreintern(c: *cgen, file: *node) void = {
// an IEEE-754 sign-bit XOR (bit 63 f64, bit 31 f32) to avoid pulling
// f64/f32 bitcast helpers into cgen. Returns true on emit, false if
// rhs doesn't reduce to a foldable float literal.
fn emitfloatlitdata(c: *cgen, directive: str, name: str,
fn emitfloatlitdata(c: *cgen, directive: str, name: str, module: str,
sz: i32, rhs: *node) bool = {
let isf32: bool = (sz == 4);
let bits: u64 = 0u64;
@@ -29898,7 +29898,7 @@ fn emitfloatlitdata(c: *cgen, directive: str, name: str,
};
emitline(directive);
emitline(" ");
emitsymname(c, name);
emitsymnamehint(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
@@ -30084,7 +30084,7 @@ fn emitstructlitbytes(c: *cgen, structt: *tinfo, rhs: *node,
// emitstructdata — top-level wrapper. Opens the DATA/DATAW directive
// then delegates to emitstructlitbytes. Shared between emitletdataw
// struct arm and emitdefconstants struct arm (#129 A.2).
fn emitstructdata(c: *cgen, directive: str, name: str,
fn emitstructdata(c: *cgen, directive: str, name: str, module: str,
structt: *tinfo, rhs: *node) bool = {
let su: *tinfo = structt;
for (su != nil && su.kind == tykind.TY_NAMED) { su = su.under; };
@@ -30092,7 +30092,7 @@ fn emitstructdata(c: *cgen, directive: str, name: str,
if (su.kind != tykind.TY_STRUCT) { return false; };
emitline(directive);
emitline(" ");
emitsymname(c, name);
emitsymnamehint(c, name, module);
emitline("(SB),\"");
emitstructlitbytes(c, structt, rhs, 0u64);
emitline("\"\n");
@@ -30371,7 +30371,7 @@ fn emitarraylitbytes(c: *cgen, arrt: *tinfo, rhs: *node,
// behavior (the old loop emitted zeros when `elems` was nil); the
// refactor would have skipped emit entirely without this branch,
// causing `undefined reference to strconv.f64tos_buf` at link.
fn emitarraydata(c: *cgen, directive: str, name: str,
fn emitarraydata(c: *cgen, directive: str, name: str, module: str,
arrt: *tinfo, rhs: *node) bool = {
let au: *tinfo = arrt;
for (au != nil && au.kind == tykind.TY_NAMED) { au = au.under; };
@@ -30381,7 +30381,7 @@ fn emitarraydata(c: *cgen, directive: str, name: str,
let total: u64 = arrt.size;
emitline(directive);
emitline(" ");
emitsymname(c, name);
emitsymnamehint(c, name, module);
emitline("(SB),\"");
let i: u64 = 0u64;
for (i < total) { emitdatawbyte(0u8); i = i + 1u64; };
@@ -30391,7 +30391,7 @@ fn emitarraydata(c: *cgen, directive: str, name: str,
if (!emitarraylitbytes(c, arrt, rhs, 0)) { return false; };
emitline(directive);
emitline(" ");
emitsymname(c, name);
emitsymnamehint(c, name, module);
emitline("(SB),\"");
emitarraylitbytes(c, arrt, rhs, 1);
emitline("\"\n");
@@ -30414,8 +30414,8 @@ fn emitletdataw(c: *cgen, file: *node) void = {
// (#129 Phase A.1, rule-12). Bare-call
// discards the bool return (mirrors
// cgen.ww:723 fmt.fprintln pattern).
emitfloatlitdata(c, "DATAW", nm, fsz,
d.rhs);
emitfloatlitdata(c, "DATAW", nm, d.nmod,
fsz, d.rhs);
};
// #129 A.2: struct-typed let with N_STRUCTLIT rhs
// routes through the emitstructdata SSoT helper.
@@ -30428,7 +30428,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
if (r.kind == nkind.N_STRUCTLIT) {
let st: *tinfo = d.lhs.type_: *tinfo;
emitstructdata(c, "DATAW", nm,
st, r);
d.nmod, st, r);
};
};
};
@@ -30459,7 +30459,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
};
if (ok) {
emitline("DATAW ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
let n: u64 = v;
@@ -30493,7 +30493,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
let lab: str = internstrlit(c, r.str);
let v: u64 = r.str.len: u64;
emitline("DATAW ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
for (i < 8) { emitdatawbyte(0u8); i += 1; };
@@ -30506,7 +30506,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
};
emitline("\"\n");
emitline("DATAR ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("+0(SB),");
emitbytes( lab.ptr, lab.len: u64);
emitline("(SB)\n");
@@ -30525,7 +30525,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
};
if (ok) {
emitline("DATAW ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
let szstr: i32 = primtypesize("str"): i32;
@@ -30556,7 +30556,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
};
if (ok) {
emitline("DATAW ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
let szsl: i32 = tyslicesize(): i32;
@@ -30574,7 +30574,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
if (issg) {
if (d.rhs == nil) {
emitline("DATAW ");
emitsymname(c, nm);
emitsymnamehint(c, nm, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
for (i < sz) {
@@ -30607,7 +30607,7 @@ fn emitletdataw(c: *cgen, file: *node) void = {
if (route) {
let at: *tinfo = d.lhs.type_: *tinfo;
emitarraydata(c, "DATAW", nm,
at, rh);
d.nmod, at, rh);
};
};
};
@@ -30651,7 +30651,7 @@ fn emitdefconstants(c: *cgen, file: *node) void = {
};
if (dfsz > 0) {
emitfloatlitdata(c, "DATA", d.str,
dfsz, d.rhs);
d.nmod, dfsz, d.rhs);
} else {
// #129 A.2: struct-typed def with N_STRUCTLIT
// rhs. The checker stamps d.lhs.type_ with the
@@ -30667,7 +30667,7 @@ fn emitdefconstants(c: *cgen, file: *node) void = {
if (su != nil) {
if (su.kind == tykind.TY_STRUCT) {
emitstructdata(c, "DATA",
d.str, st, r);
d.str, d.nmod, st, r);
};
};
};};
@@ -30682,7 +30682,7 @@ fn emitdefconstants(c: *cgen, file: *node) void = {
if (au != nil) {
if (au.kind == tykind.TY_ARRAY) {
emitarraydata(c, "DATA",
d.str, at, r);
d.str, d.nmod, at, r);
};
};
};};
@@ -30699,7 +30699,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 ");
emitsymname(c, d.str);
emitsymnamehint(c, d.str, d.nmod);
emitline("(SB),\"");
let i: i32 = 0;
let n: u64 = v;
@@ -31218,6 +31218,39 @@ 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)`
@@ -31258,6 +31291,32 @@ 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 = {