/* * 989_m1mangle_sym — M1 (#22) symbol-name proof. Builds a fixture that * imports the real nested DIRECTORY package `encoding.utf8` to `.s` on * BOTH stages and asserts the emitted symbol table: * * needle | want | proves * --------------------------+------+----------------------------------- * "encoding.utf8.runesz" | yes | nested pkg path-mangles (the M1 * | | DELTA: leaf `utf8` → path * | | `encoding.utf8`) * "TEXT main," | yes | root entry stays BARE (#32) * "TEXT utf8.runesz," | no | the pre-M1 leaf-only mangle is gone * * Plus cstage.s == wwstage.s byte-for-byte on the re-baselined names * (rule-10 — the cs==ww gate over the new symbols). */ #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; } static int file_has(const char *path, const char *needle) { FILE *f = fopen(path, "rb"); if (!f) return -1; char line[4096]; int found = 0; while (fgets(line, sizeof line, f)) { if (strstr(line, needle)) { found = 1; break; } } fclose(f); return found; } struct check { const char *needle; int want; }; static const struct check checks[] = { { "encoding.utf8.runesz", 1 }, { "TEXT main,", 1 }, { "TEXT utf8.runesz,", 0 }, }; /* build_s — build `src` to `.s` via `driver`; returns 0 on success. */ static int build_s(const char *driver, const char *src, const char *stem) { char cmd[1024]; snprintf(cmd, sizeof cmd, "%s build -o '%s' '%s' 2>/dev/null", driver, stem, src); return runwait(cmd); } static int checkall(const char *name, const char *sp) { int fail = 0; for (int i = 0; i < (int)(sizeof checks / sizeof checks[0]); i++) { int got = file_has(sp, checks[i].needle); if (got != checks[i].want) { fprintf(stderr, "m1mangle_sym[%s]: '%s' present=%d want=%d\n", name, checks[i].needle, got, checks[i].want); fail++; } } return fail; } int main(void) { const char *bin = getenv("BIN"); if (!bin) bin = "out/bin"; char absbin[1024]; if (bin[0] != '/') { char cwd[1024]; if (getcwd(cwd, sizeof cwd) == NULL) return 1; snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); bin = absbin; } char cdrv[1024], wdrv[1024]; snprintf(cdrv, sizeof cdrv, "%s/ww", bin); snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); char src[64]; snprintf(src, sizeof src, "/tmp/m1sym_%d.ww", getpid()); FILE *f = fopen(src, "wb"); if (!f) return 1; fputs("package main;\n" "import encoding.utf8;\n" "export fn main() int = { return utf8.runesz('A'): int; };\n", f); fclose(f); int fail = 0; char cstem[64], cs[80]; snprintf(cstem, sizeof cstem, "/tmp/m1sym_c_%d", getpid()); snprintf(cs, sizeof cs, "%s.s", cstem); if (build_s(cdrv, src, cstem) != 0) { fprintf(stderr, "m1mangle_sym: cstage build failed\n"); fail++; } else { fail += checkall("cstage", cs); } int have_ww = (access(wdrv, X_OK) == 0); char wstem[64], ws[80]; snprintf(wstem, sizeof wstem, "/tmp/m1sym_w_%d", getpid()); snprintf(ws, sizeof ws, "%s.s", wstem); if (have_ww) { if (build_s(wdrv, src, wstem) != 0) { fprintf(stderr, "m1mangle_sym: wwstage build failed\n"); fail++; } else { fail += checkall("wwstage", ws); /* rule-10: cs.s == ww.s on the new mangled names. */ char cmp[256]; snprintf(cmp, sizeof cmp, "cmp -s '%s' '%s'", cs, ws); if (runwait(cmp) != 0) { fprintf(stderr, "m1mangle_sym: cstage.s != " "wwstage.s (byte-id break)\n"); fail++; } } } else { fprintf(stderr, "m1mangle_sym: skip wwstage (no ww_ww)\n"); } char cmd[256]; snprintf(cmd, sizeof cmd, "rm -f '%s' '%s'* '%s'*", src, cstem, wstem); runwait(cmd); if (fail) { fprintf(stderr, "m1mangle_sym: %d checks failed\n", fail); return 1; } printf("m1mangle_sym: ok\n"); return 0; }