Files
ww/test/wcc/944_alias_global_decl_run.c
Hojun-Cho 486f7f87f9 wcc_ww/cgen: #77 alias-NAMED global ARRAY emit — tichase at the dispatch entry (g-fold G2)
ww half of the #77+#78 fused g-fold train; completes the family. cs
half landed as the previous commit (G1) — the two ship together, one
gated train, per the fuse ruling on both tasks.

Root: the global DATA emit walk dispatched on the UNCHASED decl tnode —
a NO-PEEL consumer (zero `.under` tokens on the path; it never learned
aliases exist). An alias-typed global array's N_TNAME matched no arm
and the documented skip-policy ate the decl: w6c_ww referenced
main.g(SB) but emitted zero DATAW → loud `w6l: undefined reference to
main.g` on every direct alias-global array row (ken NEW-1, all k_gidx*
shapes). Every other kind was already chased (letvarisstr/isslice/
isfloat/isstruct walk aliaslookup chains; the tuple gate walks tnodes;
emitarraydata/emitslicedata chase tinfo internally; letemitsize walks —
registration was never the gap), probe-confirmed: only array rows
failed ww-side.

Fix: ONE tichase at the dispatch entry, per the spec's entry-point rule
— not per-arm. Dispatch arms touched (enumerated):
  emitletdataw (cgen.ww): hoisted `dti = tichase(d.lhs.type_)` at the
    per-decl entry; the isarr8 scalar-shortcut gate and the array arm
    now key on dti.kind == TY_ARRAY (were d.lhs.kind == N_TARRAY) and
    emitarraydata receives dti; the struct zero-fill arm's inline
    TY_NAMED loop collapses into the same dti (ef93b16 precedent,
    byte-neutral). str/float/struct/slice/tuple gates unchanged.
  letpreintern (cgen.ww): the #18 [N]str array-leg gate keyed on the
    N_TARRAY tnode while its body already chased the tinfo — gate now
    keys on the chased kind, so alias-typed [N]str globals pre-intern
    their _S_ labels in decl order (label-order parity with cstage;
    the inner elem chase collapses into tichase).
For non-alias decls tichase is identity (same tinfo pointer) — the
emitted bytes are unchanged by construction; full byte-id invariant
holds (test-unit 288/288 incl. the new table).

main.combined.ww (w6c + wwdump) regenerated by `make` — diff verified
content-identical to the cgen.ww hunks, nothing else.

Graduation table committed as test/wcc/944_alias_global_decl_run.c —
23 rows x {cs run, ww run, byte-id} = 69 checks green. This table IS
the permanent guard: the path is lint-invisible (NO-PEEL — nothing for
the future peellint to see), so only a runtime+byte-id row pins it.
Rows: plain control; alias array 1-lvl read/write/decl-order; 2-lvl
read + order-permuted write (the #78 silent saved-BP rows); [4]u32
narrow-esz; scalar/str/f64/f32 2-lvl; alias-of-named-struct field w/r
(the cs SEGV-at-one-user-level row) + 3-layer + STRUCTLIT init; slice
2-lvl literal; [2]str 1-lvl/2-lvl (letpreintern label leg); def-side
2-lvl array/struct/float; no-regression holds (alias global SLICE,
alias ELEMENT [2]row). Values >255, LAST element asserted.

Probe-OUT rows documented in the test header, filed not pinned: #86
(named-tuple global init: cs checker loud-reject vs ww accept), #87
(plain tagged global: cs silent-wrong vs ww loud-reject, non-alias).
2026-06-05 21:10:35 +09:00

457 lines
14 KiB
C

/*
* 944_alias_global_decl_run — g-fold (#77 + #78 fused): alias-typed
* GLOBAL declarations, both stages. The decl/emit dispatch was
* alias-blind on both sides:
*
* cs (#78): the let_* helper family (let_emit_size + the five kind
* predicates, cgen.c) single-peeled — a 2-level alias chain (or ONE
* user alias over a named struct) left the type TY_NAMED, so the
* global was never registered, no DATA was emitted, and reads fell
* to the frame-local path at offset 0: silent saved-BP reads (garr2
* exit-wrong, gst1 SEGV, gstr2/gsl2 silent-wrong). The routed-to
* DATA emitters re-peeled at entry and return-0'd into the silent
* skip (condition-3 members, enrollment ruling in
* .ai/rob-gfold-spec.md).
*
* ww (#77): emitletdataw's array gates + letpreintern's array leg
* keyed on the UNCHASED N_TARRAY tnode — an alias-typed global's
* N_TNAME matched no arm and the skip-policy ate the decl: loud
* `w6l: undefined reference to main.g` on every alias-global ARRAY
* row. Fix: ONE tichase at the dispatch entry (dti), array gates +
* emitarraydata read it.
*
* THIS TABLE IS THE PERMANENT GUARD, not scaffolding: the global decl
* path is lint-invisible to the future peellint — the ww side was a
* NO-PEEL consumer (zero `.under` tokens; nothing for a peel-shape
* lint to flag), and the cs helpers now spell the one chased accessor.
* Only a runtime+byte-id row pins this behavior.
*
* Train trajectory (two commits, one gated train; per-leg states were
* probe-verified, banked /tmp/implG_probes.md):
* pre-G1: garr2 cs EXIT-WRONG / gst1 cs SEGV / gstr2+gsl2+gstlit cs
* silent-wrong / gsc2+gflt2 cs loud / ww array rows link-ERR
* post-G1 (cs half): every cs row runs 0; ww array rows STILL
* link-ERR (loud, documented)
* post-G2 (ww half): every row 0/0 byte-id — this file's assertion.
*
* Probe-OUT, documented NOT pinned (failed at a DIFFERENT site, filed):
* named-tuple global init — cs checker loud-reject, ww accepts (#86)
* plain tagged global — cs silent-wrong, ww loud-reject (#87, not
* alias-family; control row fails)
*
* row | shape | want
* -------------+----------------------------------------------+-----
* ctl_plain | control: plain [4]int global r/w | 0
* garr1_read | alias arr 1-lvl, read first+LAST | 0
* garr1_wr | alias arr 1-lvl, write+read | 0
* garr_ord | alias arr, type decl AFTER let | 0
* garr2_read | alias arr 2-LVL read (#78 saved-BP row) | 0
* garr2_ordwr | alias arr 2-lvl, order-permuted, w+r | 0
* garr_u32 | alias [4]u32 w+r LAST (narrow esz anti-luck) | 0
* gsc2 | alias int 2-lvl r/w | 0
* gsc2_ord | alias int 2-lvl read, decl-after-let | 0
* gstr2 | alias str 2-lvl, len readback | 0
* gflt2 | alias f64 2-lvl | 0
* gflt2_f32 | alias f32 2-lvl (load-width pin) | 0
* gst1 | alias of named struct, field w/r (SEGV row) | 0
* gst2 | alias-of-alias named struct | 0
* gstlit | alias struct, STRUCTLIT init | 0
* gsl2 | alias slice 2-lvl, literal init + idx read | 0
* gstrarr1 | alias [2]str 1-lvl (letpreintern label leg) | 0
* gstrarr2 | alias [2]str 2-LVL | 0
* gdef_a2 | DEF: 2-lvl alias array, index read | 0
* gdef_st | DEF: alias struct, structlit + field read | 0
* gdef_f2 | DEF: 2-lvl alias f64 | 0
* hold_slice1 | alias global SLICE 1-lvl (pre-fold green) | 0
* hold_elem | alias ELEMENT type [2]row (batch-1 win) | 0
*
* Values exceed 255 (no little-endian prefix-luck) and readbacks
* assert the LAST element. Every row runs BOTH drivers and asserts
* cstage/wwstage asm byte-identity. 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;
}
struct row { const char *label; const char *src; int want; };
static const struct row rows[] = {
{ "ctl_plain",
"package main;\n"
"let g: [4]int = [1001: int, 1002: int, 1003: int, 1004: int];\n"
"export fn main() i32 = {\n"
" if (g[3] != 1004) { return 1; };\n"
" g[3] = 9999;\n"
" if (g[3] != 9999) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "garr1_read",
"package main;\n"
"type arr = [4]int;\n"
"let g: arr = [1001: int, 1002: int, 1003: int, 1004: int];\n"
"export fn main() i32 = {\n"
" if (g[0] != 1001) { return 1; };\n"
" if (g[3] != 1004) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "garr1_wr",
"package main;\n"
"type arr = [4]int;\n"
"let g: arr = [1001: int, 1002: int, 1003: int, 1004: int];\n"
"export fn main() i32 = {\n"
" g[3] = 8888;\n"
" if (g[3] != 8888) { return 1; };\n"
" if (g[2] != 1003) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "garr_ord",
"package main;\n"
"let g: arr = [1001: int, 1002: int, 1003: int, 1004: int];\n"
"type arr = [4]int;\n"
"export fn main() i32 = {\n"
" if (g[3] != 1004) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
/* #78 headline: pre-fix cs emitted NO DATA, never referenced
* main.g, and resolved g frame-local at offset 0 — LEAQ (BP) read
* the saved-BP word, exiting silently wrong. */
{ "garr2_read",
"package main;\n"
"type arr = [4]int;\n"
"type arr2 = arr;\n"
"let g: arr2 = [1001: int, 1002: int, 1003: int, 1004: int];\n"
"export fn main() i32 = {\n"
" if (g[0] != 1001) { return 1; };\n"
" if (g[3] != 1004) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "garr2_ordwr",
"package main;\n"
"let g: arr2 = [1001: int, 1002: int, 1003: int, 1004: int];\n"
"type arr2 = arr;\n"
"type arr = [4]int;\n"
"export fn main() i32 = {\n"
" if (g[3] != 1004) { return 1; };\n"
" g[1] = 7777;\n"
" if (g[1] != 7777) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "garr_u32",
"package main;\n"
"type au = [4]u32;\n"
"let g: au = [1001: u32, 1002: u32, 1003: u32, 1004: u32];\n"
"export fn main() i32 = {\n"
" if (g[3] != 1004) { return 1; };\n"
" g[3] = 8888;\n"
" if (g[3] != 8888) { return 2; };\n"
" if (g[2] != 1003) { return 3; };\n"
" return 0;\n"
"};\n", 0 },
{ "gsc2",
"package main;\n"
"type myint = int;\n"
"type myint2 = myint;\n"
"let g: myint2 = 4242;\n"
"export fn main() i32 = {\n"
" if (g != 4242) { return 1; };\n"
" g = 9999;\n"
" if (g != 9999) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "gsc2_ord",
"package main;\n"
"let g: myint2 = 4242;\n"
"type myint2 = myint;\n"
"type myint = int;\n"
"export fn main() i32 = {\n"
" if (g != 4242) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gstr2",
"package main;\n"
"type ms = str;\n"
"type ms2 = ms;\n"
"let g: ms2 = \"hello\";\n"
"export fn main() i32 = {\n"
" if (g.len != 5) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gflt2",
"package main;\n"
"type mf = f64;\n"
"type mf2 = mf;\n"
"let g: mf2 = 1.5;\n"
"export fn main() i32 = {\n"
" if (g != 1.5) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gflt2_f32",
"package main;\n"
"type mg = f32;\n"
"type mg2 = mg;\n"
"let g: mg2 = 1.5f32;\n"
"export fn main() i32 = {\n"
" if (g != 1.5f32) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
/* ONE user alias level sufficed to SEGV cs pre-fix: the named
* struct already costs the single peel, so `pt1 = pt` is two
* TY_NAMED layers at the decl. */
{ "gst1",
"package main;\n"
"type pt = struct { x: int, y: int };\n"
"type pt1 = pt;\n"
"let g: pt1;\n"
"export fn main() i32 = {\n"
" g.y = 2002;\n"
" if (g.y != 2002) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gst2",
"package main;\n"
"type pt = struct { x: int, y: int };\n"
"type pt1 = pt;\n"
"type pt2 = pt1;\n"
"let g: pt2;\n"
"export fn main() i32 = {\n"
" g.y = 2002;\n"
" if (g.y != 2002) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gstlit",
"package main;\n"
"type pt = struct { x: int, y: int };\n"
"type pt1 = pt;\n"
"let g: pt1 = pt { x = 1001, y = 2002 };\n"
"export fn main() i32 = {\n"
" if (g.y != 2002) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gsl2",
"package main;\n"
"type sl = []int;\n"
"type sl2 = sl;\n"
"let g: sl2 = [1001: int, 1002: int, 1003: int];\n"
"export fn main() i32 = {\n"
" if (g.len != 3) { return 1; };\n"
" if (g[2] != 1003) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
/* [N]str rows also pin letpreintern's chased array-leg gate: a
* missed pre-intern desyncs _S_ label order vs cstage (byte-id
* would catch it as a label-number diff). */
{ "gstrarr1",
"package main;\n"
"type sa = [2]str;\n"
"let g: sa = [\"hello\", \"worlds!\"];\n"
"export fn main() i32 = {\n"
" if (g[1].len != 7) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gstrarr2",
"package main;\n"
"type sa = [2]str;\n"
"type sa2 = sa;\n"
"let g: sa2 = [\"hello\", \"worlds!\"];\n"
"export fn main() i32 = {\n"
" if (g[1].len != 7) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gdef_a2",
"package main;\n"
"type arr = [4]int;\n"
"type arr2 = arr;\n"
"def G: arr2 = [1001: int, 1002: int, 1003: int, 1004: int];\n"
"export fn main() i32 = {\n"
" if (G[3] != 1004) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gdef_st",
"package main;\n"
"type pt = struct { x: int, y: int };\n"
"type pt1 = pt;\n"
"def G: pt1 = pt { x = 1001, y = 2002 };\n"
"export fn main() i32 = {\n"
" if (G.y != 2002) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
{ "gdef_f2",
"package main;\n"
"type mf = f64;\n"
"type mf2 = mf;\n"
"def G: mf2 = 1.5;\n"
"export fn main() i32 = {\n"
" if (G != 1.5) { return 1; };\n"
" return 0;\n"
"};\n", 0 },
/* No-regression holds: alias global SLICE (green before this
* fold) and alias ELEMENT type (batch-1 win). */
{ "hold_slice1",
"package main;\n"
"type sl = []int;\n"
"let g: sl = [1001: int, 1002: int, 1003: int];\n"
"export fn main() i32 = {\n"
" if (g.len != 3) { return 1; };\n"
" if (g[2] != 1003) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
{ "hold_elem",
"package main;\n"
"type row = [3]int;\n"
"let g: [2]row = [[1010: int, 1020: int, 1030: int], [1040: int, 1050: int, 1060: int]];\n"
"export fn main() i32 = {\n"
" if (g[0][0] != 1010) { return 1; };\n"
" if (g[1][2] != 1060) { return 2; };\n"
" return 0;\n"
"};\n", 0 },
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char src[96], tmpdir[96], errf[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/agd_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/agd_%d_d_%d", getpid(), i);
snprintf(errf, sizeof errf, "/tmp/agd_%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 (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 != r->want) {
fprintf(stderr, "row[%s]: %s exit %d, want %d\n",
r->label, driver, got, r->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/agd_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/agd_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/agd_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++;
if (run_driver(cdrv, &rows[i], i) != 0) fail++;
}
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
if (run_driver(wdrv, &rows[i], i) != 0) fail++;
}
for (int i = 0; i < n; i++) {
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0) fail++;
}
}
if (fail) {
fprintf(stderr, "alias_global_decl: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("alias_global_decl: %d/%d ok\n", total, total);
return 0;
}