selfhost: migrate tool sources to directory packages

Build w6a and w6l from package-main directories and expose the wcc backend through a narrow package API so w6c and wwdump no longer import implementation files. Retarget the remaining load-bearing fixtures and example sources to directory packages; retain the one intentional flat compiler collision as an explicitly composed raw unit.
This commit is contained in:
2026-08-12 17:12:03 +09:00
parent 90f9d60291
commit c7d9dc92de
38 changed files with 197 additions and 252 deletions

View File

@@ -112,13 +112,11 @@ main(void)
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
/* #93 sep layout: the selfhost tools no longer leave a single
* next-to-source main.s. Self-build one real tool via the explicit
* main.ww compatibility root and feed EVERY emitted
* next-to-source main.s. Self-build one real directory package and
* feed EVERY emitted
* <tool>.sepwork/<pkg>.s as
* the generated-asm corpus — the root tool code plus each dep
* package's .s (richer than the old 3 monolithic main.s). The directory
* intentionally contains package main plus legacy file-import packages,
* so it is not a directory-package root. */
* package's .s (richer than the old 3 monolithic main.s). */
char stem[256], cmd[4096];
int fail = 0;
int n = 0;
@@ -126,11 +124,11 @@ main(void)
snprintf(stem, sizeof stem, "/tmp/wwa_%d_sh", getpid());
snprintf(cmd, sizeof cmd,
"timeout 400 %s/ww build -o %s "
"%s/selfhost/cmd/w6a/main.ww >/dev/null 2>&1",
"%s/selfhost/cmd/w6a >/dev/null 2>&1",
bin, stem, cwd);
if (runwait(cmd) != 0) {
fprintf(stderr,
"w6a_ww FAIL: cannot sep-build selfhost/cmd/w6a/main.ww\n");
"w6a_ww FAIL: cannot sep-build selfhost/cmd/w6a\n");
/* a failed build can leave a partial stem/.sepwork tree. */
fail = 1;
goto cleanup;

View File

@@ -57,9 +57,8 @@ slurp(const char *path, char **outbuf, size_t *outlen)
* the linkable unit is now the per-package .o + reverse-topo .a set the
* sep driver assembles (a raw `w6l <root>.o *.a libwwrt.a` from the test
* side fails — `undefined reference` — because it can't reproduce the
* driver's topo order). The tools are legacy inline-package directories,
* so their explicit main.ww files remain the compatibility roots. Drive
* the full sep build twice; both compile directly and differ only in the
* driver's topo order). Drive each real directory package through the
* full sep build twice; both compile directly and differ only in the
* linker: once with the default C w6l, once with WW_W6L=w6l_ww. Diff
* the two real tool binaries. Exercises the full archive two-pass loader
* + reverse-topo .a resolution — a stronger link than the old single
@@ -77,7 +76,7 @@ build_and_diff(const char *bin, const char *cwd, const char *tool, int id)
snprintf(cmd, sizeof cmd,
"timeout 300 %s/ww build -o %s "
"%s/selfhost/cmd/%s/main.ww >/dev/null 2>&1",
"%s/selfhost/cmd/%s >/dev/null 2>&1",
bin, cstem, cwd, tool);
if (runwait(cmd) != 0) {
fprintf(stderr, "w6l_ww FAIL: C-link sep-build of %s\n", tool);
@@ -86,7 +85,7 @@ build_and_diff(const char *bin, const char *cwd, const char *tool, int id)
}
snprintf(cmd, sizeof cmd,
"WW_W6L=%s/w6l_ww timeout 300 %s/ww build "
"-o %s %s/selfhost/cmd/%s/main.ww >/dev/null 2>&1",
"-o %s %s/selfhost/cmd/%s >/dev/null 2>&1",
bin, bin, wstem, cwd, tool);
if (runwait(cmd) != 0) {
fprintf(stderr, "w6l_ww FAIL: ww-link sep-build of %s\n", tool);

View File

@@ -294,7 +294,7 @@ main(void)
static char wwdump_src[2048], wwdump_incs[4096];
snprintf(wwdump_src, sizeof wwdump_src, "%s/selfhost/cmd/wwdump/main.ww", cwd);
snprintf(wwdump_incs, sizeof wwdump_incs,
"%s/lib/ww:%s/lib/encoding/utf8:%s/selfhost/cmd/wcc",
"%s/lib/ww:%s/lib/encoding/utf8:%s/selfhost/cmd",
cwd, cwd, cwd);
cases[1].src = wwdump_src;
cases[1].incs = wwdump_incs;

View File

@@ -361,7 +361,7 @@ main(void)
{
char inc[4096], cmd[16384];
snprintf(inc, sizeof inc,
"-I %s/lib -I %s/lib/ww -I %s/selfhost/cmd/wcc", cwd, cwd, cwd);
"-I %s/lib -I %s/lib/ww -I %s/selfhost/cmd", cwd, cwd, cwd);
char root[2048];
snprintf(root, sizeof root, "%s/selfhost/cmd/w6c/main.ww", cwd);

View File

@@ -63,18 +63,15 @@ slurp_eq(const char *a, const char *b)
return rc;
}
/* Each tool builds via `ww_ww build -I <local> -I lib/ww -I selfhost/cmd/wcc src`.
/* Each tool builds via `ww_ww build -I lib/ww -I selfhost/cmd src`.
* lib/ww holds the language introspection (lex/tok/ast/parse/typ/sym);
* selfhost/cmd/wcc holds the compiler internals (mem/check/cgen*).
* selfhost/cmd holds the compiler-internal `wcc` directory package.
* Dotted `import encoding.utf8;` finds lib/encoding/utf8/ via the
* driver's default srclib path post-task-#22 dir-enum.
* Some tools have a local module dir (w6a, w6l with sibling .ww files).
* inc_local is "" for tools without one (w6c, ww, wwdump).
*/
struct buildjob {
const char *tool;
const char *src_rel;
const char *inc_local;
char workdir[64];
pid_t pid;
};
@@ -99,20 +96,11 @@ spawn_build(const char *bin, const char *cwd, struct buildjob *j)
}
char cmd[4096];
if (j->inc_local && j->inc_local[0]) {
snprintf(cmd, sizeof cmd,
"cd %s && timeout 180 %s/ww_ww build -o %s/main -I %s/%s "
"-I %s/lib/ww "
"-I %s/selfhost/cmd/wcc %s/%s >/dev/null 2>%s/build.err",
j->workdir, bin, j->workdir, cwd, j->inc_local,
cwd, cwd, cwd, j->src_rel, j->workdir);
} else {
snprintf(cmd, sizeof cmd,
"cd %s && timeout 180 %s/ww_ww build -o %s/main "
"-I %s/lib/ww "
"-I %s/selfhost/cmd/wcc %s/%s >/dev/null 2>%s/build.err",
j->workdir, bin, j->workdir, cwd, cwd, cwd, j->src_rel, j->workdir);
}
snprintf(cmd, sizeof cmd,
"cd %s && timeout 180 %s/ww_ww build -o %s/main "
"-I %s/lib/ww -I %s/selfhost/cmd "
"%s/%s >/dev/null 2>%s/build.err",
j->workdir, bin, j->workdir, cwd, cwd, cwd, j->src_rel, j->workdir);
pid_t p = fork();
if (p < 0) {
@@ -184,11 +172,11 @@ main(void)
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
struct buildjob jobs[] = {
{ "w6c", "selfhost/cmd/w6c/main.ww", "", {0}, 0 },
{ "w6a", "selfhost/cmd/w6a/main.ww", "selfhost/cmd/w6a", {0}, 0 },
{ "w6l", "selfhost/cmd/w6l/main.ww", "selfhost/cmd/w6l", {0}, 0 },
{ "ww", "selfhost/cmd/ww/main.ww", "", {0}, 0 },
{ "wwdump", "selfhost/cmd/wwdump/main.ww", "", {0}, 0 },
{ "w6c", "selfhost/cmd/w6c/main.ww", {0}, 0 },
{ "w6a", "selfhost/cmd/w6a", {0}, 0 },
{ "w6l", "selfhost/cmd/w6l", {0}, 0 },
{ "ww", "selfhost/cmd/ww/main.ww", {0}, 0 },
{ "wwdump", "selfhost/cmd/wwdump/main.ww", {0}, 0 },
};
const int n = (int)(sizeof jobs / sizeof jobs[0]);

View File

@@ -1,12 +0,0 @@
// crossmod — sibling module whose probe() takes a param named like the
// module `shadowmod` that paramshadowmod (NOT crossmod) imports. The
// shadow rule is filtered by the BINDING's own module (src_imports
// cur_mod filter): crossmod carries no `import shadowmod`, so this param
// must NOT trip even though a sibling module in the same bundle imports
// that leaf. Legit-form survivor of the abolished self-import tolerance.
package crossmod;
export fn probe(shadowmod: str) i32 = {
return shadowmod.len;
};

View File

@@ -0,0 +1,11 @@
// crossmod — directory package whose probe() takes a param named like the
// module `shadowmod` that paramshadowmod (NOT crossmod) imports. The
// shadow rule is filtered by the binding's own module (src_imports
// cur_mod filter): crossmod carries no `import shadowmod`, so this param
// must NOT trip even though a sibling package imports that leaf.
package crossmod;
export fn probe(shadowmod: str) i32 = {
return shadowmod.len;
};

View File

@@ -133,60 +133,61 @@ fn textcount(s: str, sym: str) i32 = {
testenv.clean(td);
};
// barevalue (#55 cgen-side sibling) — the migrated 794 carrier
// barevalue (#55 cgen-side sibling) — the migrated 794 compiler carrier
// (test/wcc/794_xmod_ident_prefer.c, retired with this row; its header
// documents both #55 halves). A bare VALUE ident read inside an
// imported module must be classified by the checker-stamped type, not
// a unit-wide leaf table: aa exports `v: i32 = 7` and getv() reads the
// bare `v`; the root declares a same-leaf `fn v() i64`. FLAT layout
// (file-keyed import) is REQUIRED — both files fold into one unit so
// the foreign fn lands in the leaf table; a dir-keyed aa/ compiles aa
// as its own sep unit where main.v is invisible and the collision
// cannot express. Pre-fix wwstage cgen took fnretlookup's leaf
// bare `v`; the root declares a same-leaf `fn v() i64`. This compiler
// collision requires one explicitly composed raw unit; directory packages
// correctly isolate the leaf tables and the driver no longer folds source
// imports. Pre-fix wwstage cgen took fnretlookup's leaf
// fallback and emitted `LEAQ aa.v(SB)` (fn address, no load) — the
// binary exited 0; cstage loads 7 (LEAQ+MOVSXD). Both drivers must
// build, run 7, and the flat __root.s must be cs==ww byte-identical
// (rule 10) — the stronger assertion set the carrier deferred while
// the cgen side was open.
// binary exited 0; cstage loads 7 (LEAQ+MOVSXD). Both compilers must
// emit byte-identical assembly and the raw fixture must run 7.
@test fn barevalue() void = {
let td: str = testenv.fresh();
testenv.writefile(strings.concat(td, "/aa.ww"), strings.concat(
let unit: str = strings.concat(td, "/barevalue.unit.ww");
testenv.writefile(unit, strings.concat(
"//ww:module aa\n",
"package aa;\n",
"export let v: i32 = 7;\n",
"export fn getv() i32 = {\n",
" return v;\n",
"};\n"));
testenv.writefile(strings.concat(td, "/main.ww"), strings.concat(
"};\n",
"//ww:module-reset\n",
"package main;\n",
"import aa;\n",
"fn v() i64 = { return 100; };\n",
"export fn main() i32 = {\n",
" return aa.getv();\n",
"};\n"));
let drvs: []str = ["ww", "ww_ww"];
let comps: []str = ["w6c", "w6c_ww"];
let tags: []str = ["cs", "ww"];
let asms: []str = ["", ""];
let s: i32 = 0;
for (s < 2) {
let stem: str = strings.concat(td, "/prog.", tags[s]);
let av: []str = [testenv.driver(drvs[s]), "build", "-o", stem,
"-I", td, strings.concat(td, "/main.ww")];
asms[s] = strings.concat(td, "/", tags[s], ".s");
let av: []str = [testenv.driver(comps[s]), "-o", asms[s], unit];
if (runcode(td, strings.concat("build_", tags[s]), av) != 0) {
fail("barevalue", strings.concat(drvs[s], " build failed"));
fail("barevalue", strings.concat(comps[s], " compile failed"));
};
let rav: []str = [stem];
if (runcode(td, strings.concat("run_", tags[s]), rav) != 7) {
fail("barevalue", strings.concat(drvs[s], " exit != 7 ",
"(bare v in aa.getv must LOAD aa.v, not take the ",
"foreign fn's address)"));
};
asms[s] = testenv.readfile(strings.concat(stem,
".sepwork/__root.s"));
s += 1;
};
if (!testenv.same(asms[0], asms[1])) {
if (!testenv.same(testenv.readfile(asms[0]), testenv.readfile(asms[1]))) {
fail("barevalue", "cs .s != ww .s (rule 10)");
};
let obj: str = strings.concat(td, "/barevalue.o");
let aav: []str = [testenv.driver("w6a"), "-o", obj, asms[0]];
if (runcode(td, "assemble", aav) != 0) { fail("barevalue", "w6a failed"); };
let prog: str = strings.concat(td, "/barevalue");
let lav: []str = [testenv.driver("w6l"), "-o", prog, obj,
strings.concat(testenv.repo(), "/out/lib/libwwrt.a")];
if (runcode(td, "link", lav) != 0) { fail("barevalue", "w6l failed"); };
let rav: []str = [prog];
if (runcode(td, "run", rav) != 7) {
fail("barevalue", "bare v loaded the foreign fn address");
};
testenv.clean(td);
};
@@ -198,7 +199,8 @@ fn textcount(s: str, sym: str) i32 = {
// the mirror corner (d) in cgdot. Runtime pre-fix: 141, want 42.
@test fn defshadow() void = {
let td: str = testenv.fresh();
testenv.writefile(strings.concat(td, "/p5aa.ww"), strings.concat(
assert(os.mkdir(strings.concat(td, "/p5aa"), 493) == 0);
testenv.writefile(strings.concat(td, "/p5aa/p5aa.ww"), strings.concat(
"package p5aa;\n",
"export def MSG: i32 = 3;\n"));
testenv.writefile(strings.concat(td, "/main.ww"), strings.concat(
@@ -241,7 +243,8 @@ fn textcount(s: str, sym: str) i32 = {
// gate (proves the checker stamps the N_DOT fn rvalue as TY_FN).
@test fn modqualfnval() void = {
let td: str = testenv.fresh();
testenv.writefile(strings.concat(td, "/p6aa.ww"), strings.concat(
assert(os.mkdir(strings.concat(td, "/p6aa"), 493) == 0);
testenv.writefile(strings.concat(td, "/p6aa/p6aa.ww"), strings.concat(
"package p6aa;\n",
"export fn hit(x: i32) i32 = { return x + 40; };\n"));
testenv.writefile(strings.concat(td, "/main.ww"), strings.concat(

View File

@@ -182,7 +182,8 @@ fn xrow(label: str, esrc: str, msrc: str, expectbuild: bool) void = {
let i: i32 = 0;
for (i < 2) {
let td: str = testenv.fresh();
testenv.writefile(strings.concat(td, "/e.ww"), esrc);
assert(os.mkdir(strings.concat(td, "/e"), 493) == 0);
testenv.writefile(strings.concat(td, "/e/e.ww"), esrc);
testenv.writefile(strings.concat(td, "/main.ww"), msrc);
let av: []str = [testenv.driver(drvs[i]), "build", "-I", td,
strings.concat(td, "/main.ww")];