selfhost+cstage+test: graduate deflookup mod-qualified same-module-first (#11)

cstage Sdef walk #2 N_DOT branch used c->cur_mod where n->lhs->str is
the correct module hint. Sister of #4c wwstage graduation; same shape
as the TY_FN branch which already uses mafn(c, n->str, n->lhs->str).

cmd/w6c/cgen.c: add sdef_mod_match_hint(s, hint); walk #2 routes hint
first then head-pick fallback, matching #4a/#28/#31/#34 *mod variant
pattern. selfhost: add deflookuprhsmod(c, name, mod); cgdot N_DOT
mod-qualified str-def value-load routes through it. Rule-10 symmetric
stages: both stages now share the lhs.str polarity (was: both used
cur_mod / cur-module hint).

747_def_modqual_modshadow: table-driven sentinel — gamma calls
alpha.MSG with beta.MSG (same-leaf-name) at head of c.defs/sdefs.
want_imm "$38," (alpha strlit len), bad_imm "$27," (beta strlit len),
plus cs-vs-ws byte-id. Reverting cstage walk #2 to head-pick → fails
$38 on cstage + diverges cs-vs-ws; reverting wwstage cgdot to plain
deflookuprhs → fails $38 on wwstage.

make test 121/121; ww2==ww3==ww4 byte-id holds.
This commit is contained in:
2026-05-19 00:58:28 +09:00
parent c9cf25be28
commit 7a278c1a2d
7 changed files with 354 additions and 14 deletions

View File

@@ -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_fnret_bare_leaf_shadow \
$(BIN)/test_fnret34_modshadow \ $(BIN)/test_fnret34_modshadow \
$(BIN)/test_strdef_inline \ $(BIN)/test_strdef_inline \
$(BIN)/test_def_modqual_modshadow \
$(BIN)/test_param_shadow_mod \ $(BIN)/test_param_shadow_mod \
$(BIN)/test_localoff_scope \ $(BIN)/test_localoff_scope \
$(BIN)/test_cast_enum_movl \ $(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) $(BIN)/w6c $(BIN)/w6c_ww | $(BIN)
$(CC) $(CFLAGS) -o $@ $< $(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)/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 $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \

View File

@@ -548,6 +548,20 @@ sdef_mod_match(Cg *c, Sdef *s)
return strcmp(a, b) == 0; 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 /* Interned string literals — emitted as DATA directives after all
* function bodies, so the linker lays them out alongside .text. */ * function bodies, so the linker lays them out alongside .text. */
typedef struct Strlit Strlit; 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)); mafn(c, n->str, n->lhs->str), areg(D_AX));
break; break;
} }
for (Sdef *s = sdefs; s; s = s->next) { {
if (strcmp(s->name, n->str) != 0) continue; /* Same-module-first walk using n->lhs->str as
const char *lab = intern_strlit(c, s->bytes, * the explicit module hint (sister of wwstage
s->len); * deflookuprhsmod). The TY_FN branch above
ins2(c, A_LEAQ, asym(lab), areg(D_AX)); * already uses n->lhs->str via mafn for the
ins2(c, A_MOVQ, aimm((long long)s->len), * cross-module qualifier disambiguation; this
areg(D_BX)); * walk mirrors that polarity so `alpha.MSG`
goto dot_done; * 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 /* Same gating as the bare-ident catch-all: lets route
* through localloadop (their slot can be the target of * through localloadop (their slot can be the target of

View File

@@ -12673,8 +12673,11 @@ fn cgdot(c: *cgen, n: *node) void = {
// strlit-inline matches cstage Sdef walk #2 in // strlit-inline matches cstage Sdef walk #2 in
// cmd/w6c/cgen.c N_DOT mod-qualified. Without this // cmd/w6c/cgen.c N_DOT mod-qualified. Without this
// the MOVQ leaf(SB) fallback emits a bogus ref // the MOVQ leaf(SB) fallback emits a bogus ref
// (`alpha.MSG(SB)`, never DATAW-defined). Filed #12. // (`alpha.MSG(SB)`, never DATAW-defined). lhs.str is
let drhs: *node = deflookuprhs(c, fld); // 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 != nil) {
if (drhs.kind == nkind.N_STRLIT) { if (drhs.kind == nkind.N_STRLIT) {
let bytes: str = drhs.str; let bytes: str = drhs.str;
@@ -20880,6 +20883,29 @@ fn deflookuprhs(c: *cgen, name: str) *node = {
return nil; 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 -------------------------------------- // ---- module-private symbol map --------------------------------------
// //
// Every non-FFI top-level fn decl lives in its module's namespace — // Every non-FFI top-level fn decl lives in its module's namespace —

View File

@@ -1730,6 +1730,29 @@ fn deflookuprhs(c: *cgen, name: str) *node = {
return nil; 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 -------------------------------------- // ---- module-private symbol map --------------------------------------
// //
// Every non-FFI top-level fn decl lives in its module's namespace — // Every non-FFI top-level fn decl lives in its module's namespace —

View File

@@ -1744,8 +1744,11 @@ fn cgdot(c: *cgen, n: *node) void = {
// strlit-inline matches cstage Sdef walk #2 in // strlit-inline matches cstage Sdef walk #2 in
// cmd/w6c/cgen.c N_DOT mod-qualified. Without this // cmd/w6c/cgen.c N_DOT mod-qualified. Without this
// the MOVQ leaf(SB) fallback emits a bogus ref // the MOVQ leaf(SB) fallback emits a bogus ref
// (`alpha.MSG(SB)`, never DATAW-defined). Filed #12. // (`alpha.MSG(SB)`, never DATAW-defined). lhs.str is
let drhs: *node = deflookuprhs(c, fld); // 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 != nil) {
if (drhs.kind == nkind.N_STRLIT) { if (drhs.kind == nkind.N_STRLIT) {
let bytes: str = drhs.str; let bytes: str = drhs.str;

View File

@@ -12673,8 +12673,11 @@ fn cgdot(c: *cgen, n: *node) void = {
// strlit-inline matches cstage Sdef walk #2 in // strlit-inline matches cstage Sdef walk #2 in
// cmd/w6c/cgen.c N_DOT mod-qualified. Without this // cmd/w6c/cgen.c N_DOT mod-qualified. Without this
// the MOVQ leaf(SB) fallback emits a bogus ref // the MOVQ leaf(SB) fallback emits a bogus ref
// (`alpha.MSG(SB)`, never DATAW-defined). Filed #12. // (`alpha.MSG(SB)`, never DATAW-defined). lhs.str is
let drhs: *node = deflookuprhs(c, fld); // 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 != nil) {
if (drhs.kind == nkind.N_STRLIT) { if (drhs.kind == nkind.N_STRLIT) {
let bytes: str = drhs.str; let bytes: str = drhs.str;
@@ -20880,6 +20883,29 @@ fn deflookuprhs(c: *cgen, name: str) *node = {
return nil; 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 -------------------------------------- // ---- module-private symbol map --------------------------------------
// //
// Every non-FFI top-level fn decl lives in its module's namespace — // Every non-FFI top-level fn decl lives in its module's namespace —

View File

@@ -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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
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;
}