Files
ww/test/wcc/944_alias_cgen_b6_run.c
Hojun-Cho 9659a0dfbe cgen: B6-c2 call-arg family single peels fold into type_chase_named — 8 lines, loud-preserving
The exact B6-c2 set (rob b6 spec §2): :8402 (callee fn-type resolve cu),
:8435 (variadic slice param vsu), :8561/:8563 + :8577/:8579 (tagged
widen-detect pu/au pairs, arg-class + #38b MEMORY-class) + the two
LOUD-PRESERVING chases :8829 (float-struct rule-7 fatal st) and :8934
(#32 tuple-arg rule-7 fatal targ). Raw `->under` in cgen.c 49→41.

LOUD-PRESERVING discipline: the :8829/:8934 chases widen what the gate
SEES, never what it ACCEPTS. DESIGNED ACCEPTANCE NARROWING (ken b6
oracle c2): kb6_fsarg2 — a 1-level-alias float-struct from a non-ident
source previously DODGED the #271/#165 fatal via the single peel; cs
accepted and GP-passed it runtime-correct by self-consistent luck
(caller+callee agreed on the wrong transport, no SSE eightbyte). Post-
c2 cs louds with the pinned #271/#165 text. ww's cell was already loud
at its own alias-return bound (#272/#276/#277 class) — fsarg2_bound
pins BOTH texts per-stage (experr_ww). fsarg0 plain control stays loud
both stages. :8934 is WATCH-ONLY (alias tuple-args are checker-blocked
upstream, #86/#99): kb5_tuparg_c two-key cells verified unmoved
(cs ok/0 + ww ok/1).

TRAIN INVARIANT: cs-only; w6c_ww/ww_ww bit-identical to the 4cac1cb
baselines (b6bddc8e…/4e9ca874…). cs movers bounded to exactly
kb6_fsarg2 (run-cell ok/0→ERR, no asm emitted — zero run-row movers);
rest of the corpus + five mains byte-NEUTRAL; kw1_101/fill2/tuparg_c/
xampdef/amplen1 detectors unmoved; 989 ratchet zero flips. kb6_sarg /
kb6_strarg / kb6_fsarg (ident twin) latent byte-NEUTRAL per ken's
structural bound.

944_alias_cgen_b6_run grows 3→8 rows, 9→22 checks: fsarg0_loud_ctl +
fsarg2_bound (per-stage experr pins; row struct gains experr_ww for
two-site loud pairs) + fsarg_ident_ctl/sarg_2lvl/strarg_2lvl controls.
All 944-family suites green; sizelint 0.
2026-06-06 03:51:34 +09:00

372 lines
12 KiB
C

/*
* 944_alias_cgen_b6_run — #5 alias arc F2b (B6 train): the cgen.c
* cgexpr/cgstmt INLINE single-peel sweep through type_chase_named, by
* consumer family — c1 assign/reassign (9 lines), c2 call-arg (8),
* c3 addr-of/field-walk/index spine (14), c4 cast/is/try (10),
* c5 reads/len/globals (8). All cs-only: every graduation is a
* DIVERGE→byte-id flip (cs aligns UP to ww's already-chasing asm);
* zero selfhost/lib bytes move across the train, so the _ww binaries
* stay bit-identical (the crossing detector).
*
* Row sources are ken's B6 first-position oracle probes
* (/tmp/ken_b5/src/kb6_*, .ai/ken-b6-oracle.md, matrix at 4cac1cb).
*
* row | shape (oracle row) | cs/ww
* --------------------+-------------------------------------+------
* ---- c1 (assign/reassign: :6359 :6403/:6405 :6460/:6462 -------
* :6518 + cgstmt :11696 :12047 :13625) -------------------
* streassign_2lvl | kb6_streassign: 2-lvl alias str |
* | reassign `b = a` — cs copied WORD0 |
* | (ptr) ONLY, len/cap stale (silent |
* | exit 1); the :6518 lu gate fell to |
* | the scalar default. LIVE graduation | 0/0
* wstore_a (B5 file) | kb5_wstore_a graduates K_RUN_NOID → |
* | K_RUN in 944_alias_cgen_b5_run.c — |
* | the ident-lhs N_ASSIGN tagged gate |
* | (:6359) was the divergence (ken |
* | C1-CORR-2, he owns the flip) | 0/0
* sreassign_2lvl | kb6_sreassign: whole-struct alias |
* | reassign — latent control | 0/0
* dassign_2lvl | kb6_dassign: (*p).f= over alias ptr |
* | — latent control | 0/0
* ---- c2 (call-arg: :8402 :8435 :8561/:8563/:8577/:8579 --------
* + LOUD-PRESERVING :8829 :8934) ------------------------
* fsarg0_loud_ctl | kb6_fsarg0: PLAIN float-struct from |
* | a non-ident source — #271/#165 loud |
* | BOTH stages (control, twin texts) | err/err
* fsarg2_bound | kb6_fsarg2: 1-alias-level respell — |
* | pre-c2 the :8829 single-peel let |
* | the alias DODGE the fatal; cs GP- |
* | passed by self-consistent luck. |
* | DESIGNED NARROWING: cs now louds |
* | with the pinned #271/#165 text |
* | ("widen what the gate SEES, never |
* | what it ACCEPTS"). ww's loud is its |
* | own alias-return bound (#272/#277 |
* | class, experr_ww) — per-stage texts | err/err
* fsarg_ident_ctl | kb6_fsarg: IDENT-source alias twin |
* | — ident domain out of the :8829 |
* | gate, latent control | 0/0
* sarg_2lvl | kb6_sarg: >16B alias struct arg — |
* | latent (B5 helper chases carry the |
* | type layer) | 0/0
* strarg_2lvl | kb6_strarg: alias str arg — latent | 0/0
* (:8934 tuple loud is WATCH-ONLY: alias spellings are |
* checker-blocked upstream (#86/#99) — the pin is the b5 |
* suite's tuparg_cast_bound99 two-key cells, which must |
* not move.) |
*
* K_RUN rows build+run BOTH drivers (cs exit==cswant, ww exit==wwwant)
* and assert cstage/wwstage asm byte-id. K_BUILDERR rows must FAIL
* with experr on BOTH drivers; experr_ww overrides the ww-side text
* when the two stages loud at DIFFERENT sites (per-stage texts).
* NNN<950, self-contained /tmp sources, no imports (944 precedent).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.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), 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, exits==wants, + byte-id */
#define K_BUILDERR 1 /* build FAILS with experr on BOTH drivers;
* experr_ww overrides the ww-side text */
struct row { const char *label; const char *src;
int cswant; int wwwant; int kind;
const char *experr; const char *experr_ww; };
/* errlog_has — a builderr cell must fail WITH its diagnostic; any other
* failure (parse error, crash, hang-kill) is a vacuous reject (940
* precedent). */
static int
errlog_has(const char *path, const char *needle)
{
FILE *f = fopen(path, "rb");
if (!f) return 0;
char buf[8192];
size_t got = fread(buf, 1, sizeof buf - 1, f);
fclose(f);
buf[got] = '\0';
return strstr(buf, needle) != NULL;
}
static const struct row rows[] = {
/* ---- c1: assign/reassign family */
/* LIVE c1 graduation: the :6518 reassign gate single-peeled the
* 2-level alias str lhs, missed the TY_STR kind test, and fell
* to the scalar default — ONE MOVQ stored the ptr word, len/cap
* kept the dead value's header (cs silent exit 1, ww correct:
* full 3-word {ptr,len,cap} store, Hare value semantics). */
{ "streassign_2lvl",
"package main;\n"
"type ms0 = str;\n"
"type ms = ms0;\n"
"export fn main() i32 = {\n"
" let a: ms = \"hello\";\n"
" let b: ms = \"x\";\n"
" b = a;\n"
" if (len(b) != 5) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL, NULL },
{ "sreassign_2lvl",
"package main;\n"
"type st0 = struct { a: i64, b: i64, c: i64 };\n"
"type st = st0;\n"
"export fn main() i32 = {\n"
" let x: st; x.a = 1; x.b = 2; x.c = 3;\n"
" let y: st; y.a = 0; y.b = 0; y.c = 0;\n"
" y = x;\n"
" if (y.c != 3 || y.b != 2) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL, NULL },
{ "dassign_2lvl",
"package main;\n"
"type st0 = struct { a: i64, b: i64 };\n"
"type st = st0;\n"
"export fn main() i32 = {\n"
" let x: st; x.a = 1; x.b = 2;\n"
" let p: *st = &x;\n"
" (*p).b = 9;\n"
" if (x.b != 9) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL, NULL },
/* ---- c2: call-arg family */
{ "fsarg0_loud_ctl",
"package main;\n"
"type fs0 = struct { x: f64 };\n"
"fn mk() fs0 = {\n"
" let s: fs0; s.x = 2.5;\n"
" return s;\n"
"};\n"
"fn g(p: fs0) f64 = { return p.x; };\n"
"export fn main() i32 = {\n"
" if (g(mk()) != 2.5) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_BUILDERR,
"#271/#165: float-bearing struct arg from a non-ident source",
NULL },
/* DESIGNED ACCEPTANCE NARROWING (B6-c2): pre-c2 cs accepted and
* GP-passed this float struct correct-by-self-consistent-luck
* (caller+callee agreed on the wrong transport) — the :8829
* single peel let a 1-level alias dodge the rule-7 fatal. The
* chase widens what the gate SEES; the loud stays loud for the
* shapes it guards. ww's cell was ALREADY loud at its own
* alias-return bound (#272/#276/#277 class) — different site,
* per-stage experr. */
{ "fsarg2_bound",
"package main;\n"
"type fs0 = struct { x: f64 };\n"
"type fs = fs0;\n"
"fn mk() fs = {\n"
" let s: fs; s.x = 2.5;\n"
" return s;\n"
"};\n"
"fn g(p: fs) f64 = { return p.x; };\n"
"export fn main() i32 = {\n"
" if (g(mk()) != 2.5) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_BUILDERR,
"#271/#165: float-bearing struct arg from a non-ident source",
"#272/#276/#277: aggregate return reaches scalar default" },
{ "fsarg_ident_ctl",
"package main;\n"
"type fs0 = struct { x: f64 };\n"
"type fs = fs0;\n"
"fn mk() fs0 = {\n"
" let s: fs0; s.x = 2.5;\n"
" return s;\n"
"};\n"
"fn g(p: fs) f64 = { return p.x; };\n"
"export fn main() i32 = {\n"
" let v: fs = mk();\n"
" if (g(v) != 2.5) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL, NULL },
{ "sarg_2lvl",
"package main;\n"
"type big0 = struct { a: i64, b: i64, c: i64 };\n"
"type big = big0;\n"
"fn g(p: big) i64 = { return p.a + p.c; };\n"
"export fn main() i32 = {\n"
" let s: big; s.a = 4; s.b = 5; s.c = 9;\n"
" if (g(s) != 13) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL, NULL },
{ "strarg_2lvl",
"package main;\n"
"type ms0 = str;\n"
"type ms = ms0;\n"
"fn g(s: ms) i64 = { return len(s): i64; };\n"
"export fn main() i32 = {\n"
" let a: ms = \"hello\";\n"
" if (g(a) != 5) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL, NULL },
};
static int
run_driver(const char *driver, const struct row *r, int i, int expect_err,
int want, const char *experr)
{
char src[96], tmpdir[96], errf[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/ab6_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/ab6_%d_d_%d", getpid(), i);
snprintf(errf, sizeof errf, "/tmp/ab6_%d_e_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd,
"cd %s && timeout 20 %s build %s >/dev/null 2>%s",
tmpdir, driver, src, errf);
int brc = runwait(cmd);
if (expect_err) {
int ok = (brc != 0)
&& (experr == NULL || errlog_has(errf, experr));
if (!ok)
fprintf(stderr, "row[%s]: %s expected loud builderr "
"\"%s\" (brc=%d)\n", r->label, driver,
experr ? experr : "", brc);
unlink(src); unlink(errf); rmdir(tmpdir);
return ok ? 0 : 1;
}
if (brc != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
unlink(src); unlink(errf); rmdir(tmpdir);
return -1;
}
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[256];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = runwait(outbin);
unlink(src); unlink(outbin); unlink(errf); rmdir(tmpdir);
if (got != want) {
fprintf(stderr, "row[%s]: %s exit %d, want %d\n",
r->label, driver, got, want);
return 1;
}
return 0;
}
static int
asm_byte_identical(const char *bin, const struct row *r, int i)
{
char src[96], cs[96], ws[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/ab6_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/ab6_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/ab6_asm_%d_%d_w.s", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c errored\n", r->label);
unlink(src);
return -1;
}
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null",
bin, ws, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label);
unlink(src); unlink(cs);
return -1;
}
int rc = slurp_eq(cs, ws);
if (rc != 0)
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
r->label);
unlink(src); unlink(cs); unlink(ws);
return rc;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[2080];
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[2120], wdrv[2120];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
total++;
int cs_err = rows[i].kind == K_BUILDERR;
if (run_driver(cdrv, &rows[i], i, cs_err,
rows[i].cswant, rows[i].experr) != 0) fail++;
}
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
int ww_err = rows[i].kind == K_BUILDERR;
const char *we = rows[i].experr_ww
? rows[i].experr_ww : rows[i].experr;
if (run_driver(wdrv, &rows[i], i, ww_err,
rows[i].wwwant, we) != 0) fail++;
}
for (int i = 0; i < n; i++) {
if (rows[i].kind != K_RUN)
continue;
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0) fail++;
}
}
if (fail) {
fprintf(stderr, "alias_cgen_b6: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("alias_cgen_b6: %d/%d ok\n", total, total);
return 0;
}