diff --git a/Makefile b/Makefile index e8c49678..c53c5430 100644 --- a/Makefile +++ b/Makefile @@ -281,6 +281,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_fnret_bare_leaf_shadow \ $(BIN)/test_fnret34_modshadow \ $(BIN)/test_strdef_inline \ + $(BIN)/test_def_modqual_modshadow \ $(BIN)/test_param_shadow_mod \ $(BIN)/test_localoff_scope \ $(BIN)/test_cast_enum_movl \ @@ -694,6 +695,10 @@ $(BIN)/test_strdef_inline: test/wcc/746_strdef_inline.c \ $(BIN)/w6c $(BIN)/w6c_ww | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_def_modqual_modshadow: test/wcc/747_def_modqual_modshadow.c \ + $(BIN)/w6c $(BIN)/w6c_ww | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_match_4arm_cross_module_run: test/wcc/929_match_4arm_cross_module_run.c \ $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ diff --git a/cmd/w6c/cgen.c b/cmd/w6c/cgen.c index eb796ab9..bb5b7e50 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -548,6 +548,20 @@ sdef_mod_match(Cg *c, Sdef *s) return strcmp(a, b) == 0; } +/* Explicit-hint variant for `mod.NAME` N_DOT mod-qualified Sdef walks + * (sister of wwstage deflookuprhsmod). Walk #2 needs n->lhs->str — a + * cross-module qualifier from a third module won't match c->cur_mod + * and would fall back to head-pick, possibly inlining the wrong-module + * strlit when both source modules export the same-leaf str def. */ +static int +sdef_mod_match_hint(Sdef *s, const char *hint) +{ + const char *a = s->mod; + if (a == hint) return 1; + if (a == NULL || hint == NULL) return 0; + return strcmp(a, hint) == 0; +} + /* Interned string literals — emitted as DATA directives after all * function bodies, so the linker lays them out alongside .text. */ typedef struct Strlit Strlit; @@ -5317,14 +5331,36 @@ cgexpr(Cg *c, Node *n, Local *locals) mafn(c, n->str, n->lhs->str), areg(D_AX)); break; } - for (Sdef *s = sdefs; s; s = s->next) { - if (strcmp(s->name, n->str) != 0) continue; - const char *lab = intern_strlit(c, s->bytes, - s->len); - ins2(c, A_LEAQ, asym(lab), areg(D_AX)); - ins2(c, A_MOVQ, aimm((long long)s->len), - areg(D_BX)); - goto dot_done; + { + /* Same-module-first walk using n->lhs->str as + * the explicit module hint (sister of wwstage + * deflookuprhsmod). The TY_FN branch above + * already uses n->lhs->str via mafn for the + * cross-module qualifier disambiguation; this + * walk mirrors that polarity so `alpha.MSG` + * from a third module beats a head-of-sdefs + * beta.MSG collision (#11, sister of #4c). */ + Sdef *s; + for (s = sdefs; s; s = s->next) { + if (strcmp(s->name, n->str) != 0) + continue; + if (sdef_mod_match_hint(s, n->lhs->str)) + break; + } + if (s == NULL) { + for (s = sdefs; s; s = s->next) + if (strcmp(s->name, n->str) == 0) + break; + } + if (s != NULL) { + const char *lab = intern_strlit(c, + s->bytes, s->len); + ins2(c, A_LEAQ, asym(lab), areg(D_AX)); + ins2(c, A_MOVQ, + aimm((long long)s->len), + areg(D_BX)); + goto dot_done; + } } /* Same gating as the bare-ident catch-all: lets route * through localloadop (their slot can be the target of diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 54822ef2..0afd801d 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -12673,8 +12673,11 @@ fn cgdot(c: *cgen, n: *node) void = { // strlit-inline matches cstage Sdef walk #2 in // cmd/w6c/cgen.c N_DOT mod-qualified. Without this // the MOVQ leaf(SB) fallback emits a bogus ref - // (`alpha.MSG(SB)`, never DATAW-defined). Filed #12. - let drhs: *node = deflookuprhs(c, fld); + // (`alpha.MSG(SB)`, never DATAW-defined). lhs.str is + // the explicit module hint — a 3rd-module qualifier + // `alpha.MSG` from gamma needs alpha (not c.curmod) + // to beat a head-of-c.defs beta.MSG collision (#11). + let drhs: *node = deflookuprhsmod(c, fld, lhs.str); if (drhs != nil) { if (drhs.kind == nkind.N_STRLIT) { let bytes: str = drhs.str; @@ -20880,6 +20883,29 @@ fn deflookuprhs(c: *cgen, name: str) *node = { return nil; }; +// deflookuprhsmod — same-module-first walk for `mod.NAME` references. +// Trio-leaf *mod variant mirroring fnretlookupmod (#31) / fnparamslookupmod +// (#28) / enumlookupmod (#4a). Module-qualified `alpha.MSG` from a third +// module needs the explicit alpha hint; deflookuprhs prefers c.curmod +// (which doesn't match either source module on a 3rd-module qualifier) +// and falls back to head-pick, possibly inlining beta.MSG's strlit when +// both alpha and beta declare same-leaf str defs. cgdot's mod-qualified +// str-def value-load routes here so a cross-module N_DOT collision +// resolves to the explicit module. Falls back to deflookuprhs's bare- +// leaf two-pass when no module matches. +fn deflookuprhsmod(c: *cgen, name: str, mod: str) *node = { + if (mod.len > 0) { + let e: *defent = c.defs; + for (e != nil) { + if (streq(e.dname, name)) { + if (streq(e.dmod, mod)) { return e.drhs; }; + }; + e = e.dnext; + }; + }; + return deflookuprhs(c, name); +}; + // ---- module-private symbol map -------------------------------------- // // Every non-FFI top-level fn decl lives in its module's namespace — diff --git a/selfhost/cmd/wcc/cgen.ww b/selfhost/cmd/wcc/cgen.ww index 72403724..0a50f248 100644 --- a/selfhost/cmd/wcc/cgen.ww +++ b/selfhost/cmd/wcc/cgen.ww @@ -1730,6 +1730,29 @@ fn deflookuprhs(c: *cgen, name: str) *node = { return nil; }; +// deflookuprhsmod — same-module-first walk for `mod.NAME` references. +// Trio-leaf *mod variant mirroring fnretlookupmod (#31) / fnparamslookupmod +// (#28) / enumlookupmod (#4a). Module-qualified `alpha.MSG` from a third +// module needs the explicit alpha hint; deflookuprhs prefers c.curmod +// (which doesn't match either source module on a 3rd-module qualifier) +// and falls back to head-pick, possibly inlining beta.MSG's strlit when +// both alpha and beta declare same-leaf str defs. cgdot's mod-qualified +// str-def value-load routes here so a cross-module N_DOT collision +// resolves to the explicit module. Falls back to deflookuprhs's bare- +// leaf two-pass when no module matches. +fn deflookuprhsmod(c: *cgen, name: str, mod: str) *node = { + if (mod.len > 0) { + let e: *defent = c.defs; + for (e != nil) { + if (streq(e.dname, name)) { + if (streq(e.dmod, mod)) { return e.drhs; }; + }; + e = e.dnext; + }; + }; + return deflookuprhs(c, name); +}; + // ---- module-private symbol map -------------------------------------- // // Every non-FFI top-level fn decl lives in its module's namespace — diff --git a/selfhost/cmd/wcc/cgenexpr.ww b/selfhost/cmd/wcc/cgenexpr.ww index 8345577d..a72f51ca 100644 --- a/selfhost/cmd/wcc/cgenexpr.ww +++ b/selfhost/cmd/wcc/cgenexpr.ww @@ -1744,8 +1744,11 @@ fn cgdot(c: *cgen, n: *node) void = { // strlit-inline matches cstage Sdef walk #2 in // cmd/w6c/cgen.c N_DOT mod-qualified. Without this // the MOVQ leaf(SB) fallback emits a bogus ref - // (`alpha.MSG(SB)`, never DATAW-defined). Filed #12. - let drhs: *node = deflookuprhs(c, fld); + // (`alpha.MSG(SB)`, never DATAW-defined). lhs.str is + // the explicit module hint — a 3rd-module qualifier + // `alpha.MSG` from gamma needs alpha (not c.curmod) + // to beat a head-of-c.defs beta.MSG collision (#11). + let drhs: *node = deflookuprhsmod(c, fld, lhs.str); if (drhs != nil) { if (drhs.kind == nkind.N_STRLIT) { let bytes: str = drhs.str; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 82ac9036..c15e82a1 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -12673,8 +12673,11 @@ fn cgdot(c: *cgen, n: *node) void = { // strlit-inline matches cstage Sdef walk #2 in // cmd/w6c/cgen.c N_DOT mod-qualified. Without this // the MOVQ leaf(SB) fallback emits a bogus ref - // (`alpha.MSG(SB)`, never DATAW-defined). Filed #12. - let drhs: *node = deflookuprhs(c, fld); + // (`alpha.MSG(SB)`, never DATAW-defined). lhs.str is + // the explicit module hint — a 3rd-module qualifier + // `alpha.MSG` from gamma needs alpha (not c.curmod) + // to beat a head-of-c.defs beta.MSG collision (#11). + let drhs: *node = deflookuprhsmod(c, fld, lhs.str); if (drhs != nil) { if (drhs.kind == nkind.N_STRLIT) { let bytes: str = drhs.str; @@ -20880,6 +20883,29 @@ fn deflookuprhs(c: *cgen, name: str) *node = { return nil; }; +// deflookuprhsmod — same-module-first walk for `mod.NAME` references. +// Trio-leaf *mod variant mirroring fnretlookupmod (#31) / fnparamslookupmod +// (#28) / enumlookupmod (#4a). Module-qualified `alpha.MSG` from a third +// module needs the explicit alpha hint; deflookuprhs prefers c.curmod +// (which doesn't match either source module on a 3rd-module qualifier) +// and falls back to head-pick, possibly inlining beta.MSG's strlit when +// both alpha and beta declare same-leaf str defs. cgdot's mod-qualified +// str-def value-load routes here so a cross-module N_DOT collision +// resolves to the explicit module. Falls back to deflookuprhs's bare- +// leaf two-pass when no module matches. +fn deflookuprhsmod(c: *cgen, name: str, mod: str) *node = { + if (mod.len > 0) { + let e: *defent = c.defs; + for (e != nil) { + if (streq(e.dname, name)) { + if (streq(e.dmod, mod)) { return e.drhs; }; + }; + e = e.dnext; + }; + }; + return deflookuprhs(c, name); +}; + // ---- module-private symbol map -------------------------------------- // // Every non-FFI top-level fn decl lives in its module's namespace — diff --git a/test/wcc/747_def_modqual_modshadow.c b/test/wcc/747_def_modqual_modshadow.c new file mode 100644 index 00000000..17bf7195 --- /dev/null +++ b/test/wcc/747_def_modqual_modshadow.c @@ -0,0 +1,221 @@ +/* + * 747_def_modqual_modshadow — sentinel for task #11: cstage Sdef walk #2 + * (cmd/w6c/cgen.c N_DOT mod-qualified bare-value load) + wwstage + * deflookuprhsmod (selfhost/cmd/wcc/cgenexpr.ww cgdot N_DOT branch). + * Pins `alpha.MSG` from a third module gamma to resolve via n->lhs->str + * (= "alpha") rather than head-pick of c.defs / sdefs so the inlined + * strlit address + length come from alpha's MSG even when beta declares + * a same-leaf MSG sitting at the head of the table. + * + * Pre-fix cstage walk #2 had no prefer-pass at all — head-pick of sdefs + * regardless of the explicit qualifier. Pre-fix wwstage called the bare- + * leaf deflookuprhs (#4c) which prefers c.curmod and falls back to head + * on mismatch — for a 3rd-module qualifier `alpha.MSG` from gamma the + * curmod hint matches nothing and the head-pick lands on beta.MSG. + * Sister of #4c's deflookuprhs / Sdef walk #3 graduation: the *mod + * variant pattern that #4a (enumlookupmod), #28 (fnparamslookupmod) and + * #31 (fnretlookupmod) established for N_DOT qualifier disambiguation + * — the dotted LHS module name is the right hint source, not c.curmod. + * + * Cstage walk #2 grows the sdef_mod_match_hint helper (sister of the + * existing sdef_mod_match which keys c->cur_mod) and routes the walk + * through it with n->lhs->str. Wwstage grows deflookuprhsmod(c, name, + * mod) (sister of deflookuprhs) and cgdot routes the mod-qualified + * branch through it with lhs.str. Both stages converge on the same + * polarity → byte-id holds on the 3rd-module qualifier shape. + * + * Was blocked solely on #12 (wwstage str-def value-load shape); #12 + * landed in d985622 so the sentinel-flip byte-id check now passes. + * + * Pin: row 1 sentinel-flips both stages' new prefer-pass — revert and + * row 1 fails on both (LEAQ of beta.MSG's strlit label instead of + * alpha.MSG's, and MOVQ $27 instead of $38). Asserts the matching + * `MOVQ $38,` immediate inside gamma.gfn TEXT + bad_imm `MOVQ $27,` + * anti-check on each stage plus cs-vs-ws byte-id per row. + */ +#include +#include +#include +#include +#include + +static int +runwait(const char *cmd) +{ + int rc = system(cmd); + if (rc == -1) return -1; + if (WIFEXITED(rc)) return WEXITSTATUS(rc); + return -1; +} + +struct row { + const char *label; + const char *src; + const char *textsym; /* TEXT sym containing the load */ + const char *want_imm; /* the immediate that MUST appear */ + const char *bad_imm; /* the immediate that MUST NOT appear */ +}; + +/* Source orderings: alpha declared before beta so beta.MSG sits at the + * HEAD of c.defs / sdefs after the prepend-walk of collectdefs / + * sdef_collect. Caller lives in a third module gamma (not alpha, not + * beta) so c.curmod / c->cur_mod doesn't accidentally match either + * source module — only the lhs.str / n->lhs->str hint resolves to + * alpha. Strlit lengths 39 vs 27 chosen so neither digit-string is a + * substring of the other and neither aliases a common frame/offset + * immediate the asm emits (0/8/16/24/32/40/48 etc). + * + * The TY_FN branch above walk #2 in cstage already uses n->lhs->str via + * mafn — this row pins the parallel polarity on the Sdef walk just + * below it. */ +static const struct row rows[] = { + { "third_module_qualifier_shadow", + "package gamma;\n" + "import alpha;\n" + "import beta;\n" + "export fn main() i32 = { return 0; };\n" + "export fn gfn() str = { return alpha.MSG; };\n" + "package alpha;\n" + "export def MSG: str = \"alpha_modqual_pin_38chr_aaaaaaaaaaaaaa\";\n" + "package beta;\n" + "export def MSG: str = \"beta_modqual_pin_27chr_BBBB\";\n", + "TEXT gamma.gfn", "$38,", "$27," }, +}; + +static int +slurp(const char *path, char *buf, size_t cap) +{ + FILE *f = fopen(path, "rb"); + if (!f) return -1; + size_t n = fread(buf, 1, cap - 1, f); + fclose(f); + buf[n] = '\0'; + return (int)n; +} + +static int +emit_s(const char *w6c, const struct row *r, int i, char *out_s, size_t cap) +{ + char src[64], cmd[1024]; + snprintf(src, sizeof src, "/tmp/dmq_%d_%d.ww", getpid(), i); + snprintf(out_s, cap, "/tmp/dmq_%d_%d_%s.s", + getpid(), i, w6c[strlen(w6c) - 1] == 'w' ? "ww" : "c"); + + FILE *f = fopen(src, "wb"); + if (!f) return -1; + fputs(r->src, f); + fclose(f); + + snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, out_s, src); + int rc = runwait(cmd); + unlink(src); + return rc; +} + +/* Inside the named TEXT sym, before its first RET, want_imm MUST appear + * and bad_imm MUST NOT. bad_imm flags pre-fix head-walk picking + * beta.MSG's strlit length (27) instead of alpha.MSG's (39). */ +static int +check_imm(const char *spath, const struct row *r, const char *stage) +{ + char buf[1 << 14]; + if (slurp(spath, buf, sizeof buf) < 0) { + fprintf(stderr, "row[%s][%s]: cannot read %s\n", + r->label, stage, spath); + return -1; + } + const char *fn = strstr(buf, r->textsym); + if (!fn) { + fprintf(stderr, "row[%s][%s]: no %s in %s\n", + r->label, stage, r->textsym, spath); + return -1; + } + const char *ret = strstr(fn, "\tRET"); + if (!ret) { + fprintf(stderr, "row[%s][%s]: no RET inside %s\n", + r->label, stage, r->textsym); + return -1; + } + const char *good = strstr(fn, r->want_imm); + if (!good || good >= ret) { + fprintf(stderr, + "row[%s][%s]: want_imm %s missing inside %s\n", + r->label, stage, r->want_imm, r->textsym); + return -1; + } + const char *bad = strstr(fn, r->bad_imm); + if (bad && bad < ret) { + fprintf(stderr, + "row[%s][%s]: bad_imm %s present inside %s — wrong-module MSG\n", + r->label, stage, r->bad_imm, r->textsym); + return -1; + } + return 0; +} + +int +main(void) +{ + const char *bin = getenv("BIN"); + if (!bin) bin = "out/bin"; + char absbin[512]; + if (bin[0] != '/') { + char cwd[256]; + if (getcwd(cwd, sizeof cwd) == NULL) return 1; + snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); + bin = absbin; + } + + char w6c[640], w6c_ww[640]; + snprintf(w6c, sizeof w6c, "%s/w6c", bin); + snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin); + + int have_ww = (access(w6c_ww, X_OK) == 0); + int n = (int)(sizeof rows / sizeof rows[0]); + int total = 0, fail = 0; + + for (int i = 0; i < n; i++) { + char cs_path[128], ws_path[128]; + + if (emit_s(w6c, &rows[i], i, cs_path, sizeof cs_path) != 0) { + fprintf(stderr, + "def_modqual_modshadow[cstage][%s]: w6c failed\n", + rows[i].label); + fail++; total++; continue; + } + total++; + if (check_imm(cs_path, &rows[i], "cstage") != 0) fail++; + + if (!have_ww) { unlink(cs_path); continue; } + + if (emit_s(w6c_ww, &rows[i], i, ws_path, sizeof ws_path) != 0) { + fprintf(stderr, + "def_modqual_modshadow[wwstage][%s]: w6c_ww failed\n", + rows[i].label); + fail++; total++; + unlink(cs_path); continue; + } + total++; + if (check_imm(ws_path, &rows[i], "wwstage") != 0) fail++; + + total++; + char cmd[512]; + snprintf(cmd, sizeof cmd, "cmp -s %s %s", cs_path, ws_path); + if (runwait(cmd) != 0) { + fprintf(stderr, + "def_modqual_modshadow[%s]: cstage vs wwstage asm differs\n", + rows[i].label); + fail++; + } + + unlink(cs_path); unlink(ws_path); + } + + if (fail) { + fprintf(stderr, + "def_modqual_modshadow: %d/%d fixtures failed\n", fail, total); + return 1; + } + printf("def_modqual_modshadow: %d/%d ok\n", total, total); + return 0; +}