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

@@ -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;
}