Files
ww/test/wcc/949_xmod_fnptr_const_run.c
Hojun-Cho bbd2ad390a test: retarget Pattern-A combined-feed gates to sep .sepwork layout (M4 E3, #94)
94a of the C-mig2 split (rob rule-11): 13 Pattern-A gates that fed a
.combined.ww to w6c/w6c_ww now drive two --sep builds (ww / ww_ww) and
byte-diff the concatenated per-package .sepwork/*.s, mirroring #93's
convention. Lands pre-flip while combined.ww still exists as the
reversible safety net. Test-only; all 5 binary pins HOLD.

989_enumcap_run deferred to 94b (its decisive assertion is a combined.ww
content diff, not a .s diff).
2026-06-18 16:44:41 +09:00

298 lines
11 KiB
C

/*
* 949_xmod_fnptr_const_run — #124: a CROSS-MODULE `&module.fn` inside a
* module-level const emits its &fn->DATAR reloc + the wwstage checker
* accepts the const table.
*
* #117/#119 wired the &fn->DATAR reloc for SAME-module `&fn` only. The
* cgen detect helper (node_fnptr_sym cgen.c / nodefnptr cgen.ww) was
* N_IDENT-only and mangled the leaf with cur_mod — so a cross-module
* `&cc.isa` (operand is N_DOT) returned NULL → no reloc → no DATA →
* w6l undefined-reference, BOTH stages. fold-6's charclass_map needs
* 12x cross-module `&ascii.isXXX` inside a const `[](str, *fn(rune)bool)`.
*
* The #124 fix is ONE both-stage, consumer-coupled commit:
* - cgen reloc (BOTH stages): node_fnptr_sym / nodefnptr grow an N_DOT
* arm forming mafn(leaf, MODULE-ident) — the same TEXT symbol the
* working runtime `&mod.fn` / direct call already emit. Shared by the
* scalar (#119) and tuple-row (#117) callers (one helper = SSoT).
* - wwstage checker (ww-only, align-UP): exprtype's TK_AMP arm now
* types a cross-module `&mod.fn` as `*fn(...)` (the N_DOT twin of the
* #206 N_IDENT synthesis, via scopelookupinmodule) — so the const
* table's nested cross-module &fn element typeeqs its `*fn` slot and
* isassignable AFFIRMATIVELY accepts (cstage types `&mod.fn` as `*fn`
* natively → ww aligns up to cstage's actual acceptance reason). The
* SK_FN gate keeps the accept narrow to genuine fn-pointers.
*
* POLARITY: align-BOTH — both stages were LOUD at base 754944a (cstage
* link-fail, wwstage checker reject). byte-id is the wwstage-correctness
* net (#263); the fn-reloc is pinned at runtime with DISTINCT fns so a
* wrong reloc (or swapped per-row offset) is caught, not masked.
*
* row | shape | exit | byte-id
* ----------------+------------------------------------+------+--------
* xmod_table_scalar| const tbl:[](str,*fn) of (&cc.isa,| 0 | cs==ww
* | &cc.isz) DISTINCT + scalar &cc.isa| |
* | — index tbl[i].1, call each back | |
* wrong_sig_table | const t:[](str,*fn(i32)i32) = | LOUD | (n/a)
* | [(..,&cc.isa)] — sig mismatch must | |
* | reject BOTH (pins the *fn typeeq | |
* | is real, not accept-everything) | |
* nonfn_dotted_ | const t:[](str,*fn) =[(..,&cc.kval)| LOUD | (n/a)
* table | )] — &<xmod NON-fn> must reject | |
* | BOTH (pins the SK_FN/N_FNDECL gate | |
* | — the #117 over-accept hazard) | |
*
* Cross-module via OWN 2-module fixtures in a PRIVATE mktemp dir (784
* model) — NOT the real lib/ascii: the driver writes intermediates next
* to traversed sources, so importing lib/ would race; a synthetic module
* `cc` exercises the IDENTICAL `&mod.fn` mangle (mafn(leaf, module)).
* NNN<950 so test-unit runs it; the private mktemp dir makes the ww_ww
* driver race-free (946 ww_ww-driver precedent).
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.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;
}
static int
slurp_eq(const char *a, const char *b)
{
FILE *fa = fopen(a, "rb");
FILE *fb = fopen(b, "rb");
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
int rc = 0;
for (;;) {
int ca = fgetc(fa);
int cb = fgetc(fb);
if (ca != cb) { rc = -1; break; }
if (ca == EOF) break;
}
fclose(fa); fclose(fb);
return rc;
}
#define K_RUN 0 /* build+run BOTH drivers, exit==want, + cs==ww byte-id */
#define K_BUILDERR 1 /* build must FAIL on BOTH drivers (rule 7 loud) */
struct file { const char *name; const char *src; };
struct scenario {
const char *label;
const struct file *files; /* name==NULL terminates */
int kind;
int want_exit;
};
/* The fold-6 consumer shape: a const cross-module `[](str,*fn(rune)bool)`
* + a scalar `*fn` global, both initialised with `&cc.<pred>`. DISTINCT
* predicates per row so a wrong reloc routes the wrong fn and a call-back
* catches it. The tuple's *fn is element .1 (the consumer ordering). */
static const struct file table_files[] = {
{ "cc.ww",
"package cc;\n"
"export fn isa(c: rune) bool = { return c == 'a'; };\n"
"export fn isz(c: rune) bool = { return c == 'z'; };\n" },
{ "main.ww",
"package main;\n"
"import cc;\n"
"const tbl: [](str, *fn(c: rune) bool) ="
" [(\":a\", &cc.isa), (\":z\", &cc.isz)];\n"
"const pf: *fn(c: rune) bool = &cc.isa;\n"
"export fn main() i32 = {\n"
" if (len(tbl) != 2) { return 10; };\n"
" let e0 = tbl[0];\n"
" let e1 = tbl[1];\n"
" let f0 = e0.1;\n"
" let f1 = e1.1;\n"
" if (!(*f0)('a')) { return 1; };\n"
" if ((*f0)('z')) { return 2; };\n"
" if (!(*f1)('z')) { return 3; };\n"
" if ((*f1)('a')) { return 4; };\n"
" if (!(*pf)('a')) { return 5; };\n"
" if ((*pf)('z')) { return 6; };\n"
" return 0;\n"
"};\n" },
{ NULL, NULL }
};
/* The SK_FN-gated bound: a cross-module &fn whose SIGNATURE doesn't match
* the declared `*fn` slot must still reject on BOTH stages (the *fn typeeq
* is real — the lever is affirmative recognition, not accept-everything). */
static const struct file wrong_sig_files[] = {
{ "cc.ww",
"package cc;\n"
"export fn isa(c: rune) bool = { return c == 'a'; };\n" },
{ "main.ww",
"package main;\n"
"import cc;\n"
"const t: [](str, *fn(x: i32) i32) = [(\":a\", &cc.isa)];\n"
"export fn main() i32 = { return len(t): i32; };\n" },
{ NULL, NULL }
};
/* The SK_FN/N_FNDECL-gated bound (distinct axis from wrong_sig_files):
* a cross-module dotted address-of a NON-fn export (`&cc.kval`, kval an
* exported var) must NOT synthesize a `*fn` and must reject the table on
* BOTH stages. This locks the lever's narrowness — the over-accept hazard
* reviewer-117 caught — so a future widening of the exprtype N_DOT arm
* past genuine fn-pointers is a loud test failure, not a silent accept. */
static const struct file nonfn_dotted_files[] = {
{ "cc.ww",
"package cc;\n"
"export let kval: int = 7;\n" },
{ "main.ww",
"package main;\n"
"import cc;\n"
"const t: [](str, *fn(c: rune) bool) = [(\":k\", &cc.kval)];\n"
"export fn main() i32 = { return len(t): i32; };\n" },
{ NULL, NULL }
};
static const struct scenario scenarios[] = {
{ "xmod_table_scalar", table_files, K_RUN, 0 },
{ "wrong_sig_table", wrong_sig_files, K_BUILDERR, 0 },
{ "nonfn_dotted_table", nonfn_dotted_files, K_BUILDERR, 0 },
};
/* Build `driver build --sep -o <dir>/<stem>` in the fixture dir; returns
* the driver's build rc and (for K_RUN) runs the produced binary, comparing
* exit to want_exit. #94: the per-package asm lands in <dir>/<stem>.sepwork/
* (cachetag keys a private WW_PKGCACHE so out/.pkgcache is untouched). */
static int
build_run(const char *dir, const char *driver, const struct scenario *sc,
const char *stem, const char *cachetag)
{
char cmd[4096], path[1024];
snprintf(cmd, sizeof cmd,
"cd %s && WW_PKGCACHE=%s/pkgc_%s %s build --sep -I %s -o %s/%s "
"%s/main.ww >/dev/null 2>%s/err",
dir, dir, cachetag, driver, dir, dir, stem, dir, dir);
int brc = runwait(cmd);
if (sc->kind == K_BUILDERR) {
if (brc == 0) {
fprintf(stderr, "949[%s]: %s built but expected LOUD "
"reject\n", sc->label, driver);
return -1;
}
return 0;
}
if (brc != 0) {
fprintf(stderr, "949[%s]: %s build failed\n", sc->label, driver);
return -1;
}
snprintf(path, sizeof path, "%s/%s", dir, stem);
int got = runwait(path);
if (got != sc->want_exit) {
fprintf(stderr, "949[%s]: %s exit %d, want %d\n",
sc->label, driver, got, sc->want_exit);
return -1;
}
return 0;
}
static int
run_scenario(const char *bin, const struct scenario *sc)
{
char dir[] = "/tmp/ww949_XXXXXX";
if (mkdtemp(dir) == NULL) {
fprintf(stderr, "949[%s]: mkdtemp failed\n", sc->label);
return -1;
}
char path[1024], cmd[4096], csdrv[2200], wwdrv[2200];
int rc = 0;
for (int i = 0; sc->files[i].name; i++) {
snprintf(path, sizeof path, "%s/%s", dir, sc->files[i].name);
FILE *f = fopen(path, "wb");
if (!f) { fprintf(stderr, "949[%s]: write %s\n", sc->label,
sc->files[i].name); rc = -1; goto done; }
fputs(sc->files[i].src, f);
fclose(f);
}
/* cstage driver --sep build (+run for K_RUN) → <dir>/main_c.sepwork/. */
snprintf(csdrv, sizeof csdrv, "%s/ww", bin);
if (build_run(dir, csdrv, sc, "main_c", "c") != 0) { rc = -1; goto done; }
/* wwstage driver --sep build (+run for K_RUN) — race-free in the
* private mktemp dir. Direct proof the wwstage emits a runnable
* binary. Distinct stem so its sepwork doesn't clobber cstage's. */
snprintf(wwdrv, sizeof wwdrv, "%s/ww_ww", bin);
if (build_run(dir, wwdrv, sc, "main_w", "w") != 0) { rc = -1; goto done; }
if (sc->kind == K_BUILDERR) goto done;
/* Byte-id net (#263 — the wwstage-correctness proof on a gate-blind
* align-BOTH fix): the per-package w6c-emitted .s (cstage sep build)
* vs w6c_ww-emitted .s (wwstage sep build) must be byte-identical.
* Concat each stem's per-package sepwork asm (sorted glob = same set). */
{
char cs_s[1024], ws_s[1024];
snprintf(cs_s, sizeof cs_s, "%s/all_cs.s", dir);
snprintf(ws_s, sizeof ws_s, "%s/all_ww.s", dir);
snprintf(cmd, sizeof cmd, "cat %s/main_c.sepwork/*.s > %s 2>/dev/null",
dir, cs_s); if (system(cmd)) {}
snprintf(cmd, sizeof cmd, "cat %s/main_w.sepwork/*.s > %s 2>/dev/null",
dir, ws_s); if (system(cmd)) {}
if (slurp_eq(cs_s, ws_s) != 0) {
fprintf(stderr, "949[%s]: cs.s/ww.s DIFFER (rule-10 "
"byte-id violation)\n", sc->label);
rc = -1;
}
}
done:
snprintf(cmd, sizeof cmd, "rm -rf %s", dir);
(void)runwait(cmd);
return rc;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[2048];
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 wdrv[2100];
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
if (access(wdrv, X_OK) != 0) {
fprintf(stderr, "949: ww_ww missing — cannot run the cs==ww "
"byte-id gate (the whole point of this test)\n");
return 1;
}
int n = (int)(sizeof scenarios / sizeof scenarios[0]);
int fail = 0;
for (int i = 0; i < n; i++) {
if (run_scenario(bin, &scenarios[i]) != 0)
fail++;
}
if (fail) {
fprintf(stderr, "949 xmod_fnptr_const: %d/%d scenarios failed\n",
fail, n);
return 1;
}
printf("xmod_fnptr_const: %d/%d ok (cs+ww run + cs==ww byte-id)\n",
n, n);
return 0;
}