Files
ww/test/wcc/949_xmod_fnptr_const_run.c
Hojun-Cho 66d69537a5 wcc/cgen: #124 cross-module &fn in a const — N_DOT reloc + checker accept (both-stage)
A cross-module `&module.fn` in a const emitted no static reloc (the
const was never defined -> w6l undefined-reference, both stages) and
wwstage's checker rejected the const fn-table. #117/#119 wired the
&fn->DATAR const-data reloc for SAME-module &fn only; charclass_map
(fold-6) needs cross-module (12x &ascii.isXXX).

cgen: add the N_DOT arm to the &fn->symbol helper (node_fnptr_sym /
nodefnptr + the two ww emit sites), emitting mafn(leaf, module-ident)
-- exactly the symbol a runtime &mod.fn or a direct cross-module call
already emits. The helper is the SSoT for both the scalar (#119) and
tuple-row (#117) const-data paths, so one arm closes both.

checker: type a cross-module `&mod.fn` as `*fn(...)` in the TK_AMP arm
(the N_DOT twin of #206's N_IDENT fn-ptr synthesis, gated on a resolved
SK_FN/N_FNDECL leaf), so isassignable affirmatively accepts the const
table -- aligning wwstage UP to cstage's actual acceptance reason
rather than by abdication. The SK_FN gate keeps a non-fn `&mod.var`
from synthesizing a fn type (the one pre-existing nonfn-scalar cs!=ww
slip is N_IDENT-base, untouched and reproduces same-module).

One consumer-coupled commit (the checker accept gates wwstage cgen, so
neither half is independently testable). Narrow: slice-row + scalar
only; fixed-array (#118) and struct-field (#129) stay separate. Both
stages emit the correct cross-module symbols at the right tuple-slot
offsets -> byte-identical (990-997 green). Pin 949_xmod_fnptr_const_run
(distinct fns so a wrong reloc is caught + the SK_FN-gate axis). This
was the last fold-6 cgen blocker; charclass_map is now unblocked.
2026-06-06 23:34:46 +09:00

317 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 via `driver` in the fixture dir; returns the driver's build rc and
* (for K_RUN) runs the produced binary, comparing exit to want_exit. */
static int
build_run(const char *dir, const char *driver, const struct scenario *sc)
{
char cmd[4096], path[1024];
snprintf(cmd, sizeof cmd,
"cd %s && rm -f main main.s main.o main.combined.ww && "
"%s build -I %s %s/main.ww >/dev/null 2>%s/err",
dir, driver, dir, 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/main", dir);
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 char *w6c, const char *w6c_ww,
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], 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 build (+run for K_RUN). */
{
char csdrv[2200];
snprintf(csdrv, sizeof csdrv, "%s/ww", bin);
if (build_run(dir, csdrv, sc) != 0) { rc = -1; goto done; }
}
/* wwstage driver build (+run for K_RUN) — race-free in the private
* mktemp dir. Direct proof the wwstage emits a runnable binary. */
snprintf(wwdrv, sizeof wwdrv, "%s/ww_ww", bin);
if (build_run(dir, wwdrv, sc) != 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): raw w6c vs w6c_ww on the cstage-driver combined. */
{
char cs_s[1024], ws_s[1024], comb[1024];
/* regen the combined via the cstage driver (deterministic). */
snprintf(cmd, sizeof cmd,
"cd %s && %s/ww build -I %s %s/main.ww >/dev/null 2>&1",
dir, bin, dir, dir);
if (runwait(cmd) != 0) {
fprintf(stderr, "949[%s]: combined regen failed\n",
sc->label);
rc = -1; goto done;
}
snprintf(comb, sizeof comb, "%s/main.combined.ww", dir);
snprintf(cs_s, sizeof cs_s, "%s/cs.s", dir);
snprintf(ws_s, sizeof ws_s, "%s/ww.s", dir);
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
w6c, cs_s, comb);
if (runwait(cmd) != 0) {
fprintf(stderr, "949[%s]: w6c on combined failed\n",
sc->label);
rc = -1; goto done;
}
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null",
w6c_ww, ws_s, comb);
if (runwait(cmd) != 0) {
fprintf(stderr, "949[%s]: w6c_ww on combined failed\n",
sc->label);
rc = -1; goto done;
}
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 w6c[2100], w6c_ww[2100];
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
if (access(w6c_ww, X_OK) != 0) {
fprintf(stderr, "949: w6c_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, w6c, w6c_ww, &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;
}