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 == '\\')

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,30 +45085,28 @@ 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.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.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;
};
};
};
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 = {

View File

@@ -1725,7 +1725,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
@@ -1934,7 +1934,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");
@@ -2318,7 +2318,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;
@@ -2370,7 +2370,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),");
@@ -2384,7 +2384,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),");
@@ -2414,7 +2414,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; };
@@ -2429,7 +2429,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");
@@ -2507,7 +2507,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) {
@@ -2548,7 +2548,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");
@@ -2556,7 +2556,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;
@@ -2579,9 +2579,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");
};
@@ -2644,7 +2644,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");
@@ -2677,7 +2677,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;
@@ -2701,7 +2701,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");
@@ -2713,7 +2713,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");
@@ -2877,7 +2877,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);
@@ -2892,7 +2892,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);
@@ -2935,7 +2935,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; };
@@ -2945,7 +2945,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");
@@ -3083,13 +3083,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`
@@ -3102,7 +3102,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;
@@ -3143,7 +3143,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; };
@@ -3156,7 +3156,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");
@@ -3175,7 +3175,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;
@@ -3214,7 +3214,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;
@@ -3243,7 +3243,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) {
@@ -3386,7 +3386,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;
@@ -3875,30 +3875,28 @@ 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.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.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;
};
};
};
d = d.next;
};
};
@@ -3969,39 +3967,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)`
@@ -4042,32 +4007,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 = {

View File

@@ -1184,7 +1184,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);
@@ -1192,7 +1192,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;
};
@@ -1242,7 +1242,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");
@@ -1275,7 +1275,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);
@@ -1291,11 +1291,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);
@@ -2937,7 +2937,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) {
@@ -4342,11 +4342,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);
@@ -5238,7 +5238,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;
};
@@ -5516,7 +5516,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");
@@ -7368,7 +7368,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;

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,30 +45085,28 @@ 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.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.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;
};
};
};
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 = {

View File

@@ -208,6 +208,34 @@ static const char *sroot_src =
" return (a[0]: i32) + (b[4]: i32);\n"
"};\n";
/* #53 LINK leg: two packages each EXPORT the SAME-named non-fn leaf (`let v`,
* `def K`). Pre-#53 exported non-fn decls skipped path-qualification and
* emitted a BARE symbol, so linking cea+ceb clashed (w6l: duplicate symbol
* v/K) — the rest of the suite never caught it because no two in-tree packages
* export the same non-fn leaf. With §7-A every exported decl path-qualifies
* (cea.v/ceb.v, cea.K/ceb.K) so the link is clean and each getter reads its
* OWN module's data. A collided leaf resolving to the wrong package's data
* would corrupt the exit:
* cea.val() = 40 + 3 = 43
* ceb.val() = 90 + 7 = 97
* => 43 + 97 = 140. */
#define EXPECT_CXLINK 140
static const char *cea_src =
"package cea;\n"
"export let v: i64 = 40;\n"
"export def K: i64 = 3;\n"
"export fn val() i64 = { return v + K; };\n";
static const char *ceb_src =
"package ceb;\n"
"export let v: i64 = 90;\n"
"export def K: i64 = 7;\n"
"export fn val() i64 = { return v + K; };\n";
static const char *cxroot_src =
"package main;\n"
"import cea;\n"
"import ceb;\n"
"fn main() i32 = { return (cea.val(): i32) + (ceb.val(): i32); };\n";
/* A package's sep-unit + bodies-unit composition. `name` is the dotted package
* path; deps are spliced ahead under //ww:module <deppath>, then the target's
* own source under //ww:module-reset. */
@@ -542,6 +570,105 @@ main(void)
}
}
/* ---- #53 LINK leg: same-leaf exported non-fn decls path-qualify ----- */
/* cea and ceb each `export let v` + `export def K` (see cea_src note).
* Pre-#53 both emitted BARE v/K -> w6l duplicate-symbol clash; with §7-A
* path-qualification (cea.v/ceb.v, cea.K/ceb.K) the link is clean and each
* getter reads its OWN module's data. */
{
char ceaww[1024], cebww[1024], cxrootww[1024];
char ceawwi[1024], cebwwi[1024], cxrootsep[1024];
snprintf(ceaww, sizeof ceaww, "%s/cea.ww", td);
snprintf(cebww, sizeof cebww, "%s/ceb.ww", td);
snprintf(cxrootww, sizeof cxrootww, "%s/cxroot.ww", td);
snprintf(ceawwi, sizeof ceawwi, "%s/cea.wwi", td);
snprintf(cebwwi, sizeof cebwwi, "%s/ceb.wwi", td);
snprintf(cxrootsep, sizeof cxrootsep, "%s/cxroot.sep.ww", td);
if (write_file(ceaww, cea_src) || write_file(cebww, ceb_src) ||
write_file(cxrootww, cxroot_src)) { fail++; goto out; }
/* cea, ceb have no deps: produce their .wwi directly. */
snprintf(cmd, sizeof cmd,
"timeout 180 %s/w6c -I %s -o /dev/null %s >/dev/null 2>&1", bin, ceawwi, ceaww);
if (runwait(cmd) != 0) { fprintf(stderr, "m3sep FAIL: cea.wwi produce (#53)\n"); fail++; goto out; }
snprintf(cmd, sizeof cmd,
"timeout 180 %s/w6c -I %s -o /dev/null %s >/dev/null 2>&1", bin, cebwwi, cebww);
if (runwait(cmd) != 0) { fprintf(stderr, "m3sep FAIL: ceb.wwi produce (#53)\n"); fail++; goto out; }
/* cxroot imports cea + ceb: scope = both .wwi, then cxroot's body. */
{
FILE *u = fopen(cxrootsep, "wb");
if (!u) { fail++; goto out; }
if (append_section(u, "//ww:module cea", ceawwi) ||
append_section(u, "//ww:module ceb", cebwwi) ||
append_section(u, "//ww:module-reset", cxrootww)) { fclose(u); fail++; goto out; }
fclose(u);
}
struct { const char *tool_c, *tool_a, *tool_l; const char *tag; } tc[] = {
{ "w6c", "w6a", "w6l", "cs" },
{ "w6c_ww", "w6a_ww", "w6l_ww", "ww" },
};
char cxexe[2][1024];
for (int t = 0; t < 2; t++) {
char as[1024], ao[1024], bs[1024], bo[1024], rs[1024], ro[1024];
snprintf(as, sizeof as, "%s/cea.%s.s", td, tc[t].tag);
snprintf(ao, sizeof ao, "%s/cea.%s.o", td, tc[t].tag);
snprintf(bs, sizeof bs, "%s/ceb.%s.s", td, tc[t].tag);
snprintf(bo, sizeof bo, "%s/ceb.%s.o", td, tc[t].tag);
snprintf(rs, sizeof rs, "%s/cxroot.%s.s", td, tc[t].tag);
snprintf(ro, sizeof ro, "%s/cxroot.%s.o", td, tc[t].tag);
snprintf(cxexe[t], sizeof cxexe[t], "%s/cxprog.%s", td, tc[t].tag);
int ok = 1;
snprintf(cmd, sizeof cmd, "timeout 180 %s/%s -c -o %s %s >/dev/null 2>&1 && timeout 180 %s/%s -o %s %s >/dev/null 2>&1",
bin, tc[t].tool_c, as, ceaww, bin, tc[t].tool_a, ao, as);
if (runwait(cmd) != 0) ok = 0;
snprintf(cmd, sizeof cmd, "timeout 180 %s/%s -c -o %s %s >/dev/null 2>&1 && timeout 180 %s/%s -o %s %s >/dev/null 2>&1",
bin, tc[t].tool_c, bs, cebww, bin, tc[t].tool_a, bo, bs);
if (runwait(cmd) != 0) ok = 0;
snprintf(cmd, sizeof cmd, "timeout 180 %s/%s -c -o %s %s >/dev/null 2>&1 && timeout 180 %s/%s -o %s %s >/dev/null 2>&1",
bin, tc[t].tool_c, rs, cxrootsep, bin, tc[t].tool_a, ro, rs);
if (runwait(cmd) != 0) ok = 0;
/* #53 structural proof (cstage .s once): each package's exported
* non-fn leaf is MODULE-PREFIXED, never a shared bare v/K. */
if (t == 0) {
char *sa = NULL, *sb = NULL; size_t na = 0, nb = 0;
if (slurp(as, &sa, &na) == 0 && slurp(bs, &sb, &nb) == 0) {
if (strstr(sa, "cea.v(SB)") == NULL || strstr(sa, "cea.K(SB)") == NULL ||
strstr(sb, "ceb.v(SB)") == NULL || strstr(sb, "ceb.K(SB)") == NULL) {
fprintf(stderr, "m3sep FAIL: #53 exported non-fn leaf not module-prefixed\n");
fail++;
}
if (strstr(sa, "DATAW v(SB)") != NULL || strstr(sa, "DATA K(SB)") != NULL ||
strstr(sb, "DATAW v(SB)") != NULL || strstr(sb, "DATA K(SB)") != NULL) {
fprintf(stderr, "m3sep FAIL: #53 bare exported leaf survives (cross-unit link collision)\n");
fail++;
}
}
free(sa); free(sb);
}
/* link cxroot + cea + ceb + runtime: clean iff leaves are unique. */
snprintf(cmd, sizeof cmd, "timeout 180 %s/%s -o %s %s %s %s %s/../lib/libwwrt.a >/dev/null 2>&1",
bin, tc[t].tool_l, cxexe[t], ro, ao, bo, bin);
if (runwait(cmd) != 0) ok = 0;
if (!ok) {
fprintf(stderr, "m3sep FAIL: %s export-leaf sep build/link failed (#53)\n", tc[t].tag);
fail++;
cxexe[t][0] = '\0';
continue;
}
int rc = runwait(cxexe[t]);
if (rc != EXPECT_CXLINK) {
fprintf(stderr, "m3sep FAIL: %s export-leaf link program exit=%d, expected %d "
"(#53 same-leaf export collision corrupts the linked data)\n", tc[t].tag, rc, EXPECT_CXLINK);
fail++;
}
}
if (cxexe[0][0] && cxexe[1][0] && files_eq(cxexe[0], cxexe[1]) != 0) {
fprintf(stderr, "m3sep FAIL: cs export-leaf exe != ww exe (rule 10, #53)\n");
fail++;
}
}
out:
snprintf(cmd, sizeof cmd, "rm -rf %s", td);
runwait(cmd);
@@ -551,7 +678,8 @@ out:
}
printf("m3sep: synth leaf->mid->root — per-pkg bodies==.wwi (-c) + cs==ww "
".s/exe + determinism + value-global guard + behavioral (exit %d) + "
"#49 str-pkg sep-LINK (sleaf+smid, exit 160)\n",
"#49 str-pkg sep-LINK (sleaf+smid, exit 160) + "
"#53 export-leaf sep-LINK (cea+ceb same-leaf v/K, exit 140)\n",
EXPECT_EXIT);
return 0;
}