diff --git a/Makefile b/Makefile index 3aa83b68..7494ec00 100644 --- a/Makefile +++ b/Makefile @@ -305,6 +305,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_alias_decl_order_size_run \ $(BIN)/test_alias_accept_run \ $(BIN)/test_alias_idx_family_run \ + $(BIN)/test_alias_global_decl_run \ $(BIN)/test_tuple_nary_destructure_run \ $(BIN)/test_rvalue_tuple_destructure_run \ $(BIN)/test_overcap_tuple_field_store_run \ @@ -1467,6 +1468,12 @@ $(BIN)/test_alias_idx_family_run: test/wcc/944_alias_idx_family_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_alias_global_decl_run: test/wcc/944_alias_global_decl_run.c \ + $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_tuple_nary_destructure_run: test/wcc/945_tuple_nary_destructure_run.c \ $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index a37e7b2d..70fda604 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -38257,23 +38257,15 @@ export fn letpreintern(c: *cgen, file: *node) void = { // so emitstrarraydata's DATAR rows find an _S_ rodata // row. Must match that helper's interning order exactly // to keep labels stable. + // g-fold #77: gate on the CHASED tinfo kind — the + // N_TARRAY tnode test missed alias-typed [N]str + // globals, desyncing label order vs cstage. let handled: bool = false; if (d.lhs != nil && r != nil) { - if (d.lhs.kind == nkind.N_TARRAY + let au: *tinfo = tichase(d.lhs.type_: *tinfo); + if (au != nil && au.kind == tykind.TY_ARRAY && r.kind == nkind.N_ARRLIT) { - let au: *tinfo = d.lhs.type_: *tinfo; - for (au != nil && au.kind == tykind.TY_NAMED) { - au = au.under; - }; - let eu: *tinfo = nil; - if (au != nil) { - if (au.kind == tykind.TY_ARRAY) { - eu = au.sub; - for (eu != nil && eu.kind == tykind.TY_NAMED) { - eu = eu.under; - }; - }; - }; + let eu: *tinfo = tichase(au.sub); if (eu != nil && eu.kind == tykind.TY_STR) { handled = true; let alen: i32 = au.alen: i32; @@ -39302,6 +39294,15 @@ fn emitletdataw(c: *cgen, file: *node) void = { let sz: i32 = letemitsize(c, d); let issg: bool = letvarisstruct(c, nm); let fsz: i32 = letvarisfloat(c, nm); + // g-fold #77: ONE chase at the dispatch entry. The + // array gates below keyed on the N_TARRAY tnode — + // an alias-typed global's N_TNAME matched no arm + // and the skip-policy ate the decl: no DATAW, + // undefined reference at link. The str/float/ + // struct/slice/tuple gates already alias-walk + // (letvaris* / the tlt tnode walk) and stay put. + let dti: *tinfo = nil; + if (d.lhs != nil) { dti = tichase(d.lhs.type_: *tinfo); }; // C-t3 (#48): tuple global — slot-laid DATAW // row (+ DATAR ptr patches for str elements) // via emittupledata. Unsupported element @@ -39363,8 +39364,8 @@ fn emitletdataw(c: *cgen, file: *node) void = { // below handles it and the duplicate DATAW would // otherwise differ across stages on user code. let isarr8: bool = false; - if (d.lhs != nil) { - if (d.lhs.kind == nkind.N_TARRAY) { isarr8 = true; }; + if (dti != nil) { + if (dti.kind == tykind.TY_ARRAY) { isarr8 = true; }; }; if (sz == 8 && !issg && fsz == 0 && !isarr8 && !istup) { let v: u64 = 0u64; @@ -39516,13 +39517,7 @@ fn emitletdataw(c: *cgen, file: *node) void = { if (issg) { if (d.rhs == nil) { let zsz: i32 = sz; - if (d.lhs != nil) { - let ti: *tinfo = d.lhs.type_: *tinfo; - for (ti != nil && ti.kind == tykind.TY_NAMED) { - ti = ti.under; - }; - if (ti != nil) { zsz = ti.size: i32; }; - }; + if (dti != nil) { zsz = dti.size: i32; }; emitline("DATAW "); emitsymnamehint(c, nm, d.nmod); emitline("(SB),\""); @@ -39544,8 +39539,8 @@ fn emitletdataw(c: *cgen, file: *node) void = { // fold-failures don't corrupt the DATA literal. // No-rhs arrays (e.g. `let buf: [N]u8;`) go through // the same helper with rhs=nil → zero-fill branch. - if (d.lhs != nil) { - if (d.lhs.kind == nkind.N_TARRAY) { + if (dti != nil) { + if (dti.kind == tykind.TY_ARRAY) { let rh: *node = d.rhs; let route: bool = false; if (rh == nil) { route = true; }; @@ -39555,9 +39550,8 @@ fn emitletdataw(c: *cgen, file: *node) void = { }; }; if (route) { - let at: *tinfo = d.lhs.type_: *tinfo; emitarraydata(c, "DATAW", nm, - d.nmod, at, rh); + d.nmod, dti, rh); }; }; }; diff --git a/selfhost/cmd/wcc/cgen.ww b/selfhost/cmd/wcc/cgen.ww index 71aebef5..64d8dc5e 100644 --- a/selfhost/cmd/wcc/cgen.ww +++ b/selfhost/cmd/wcc/cgen.ww @@ -1296,23 +1296,15 @@ export fn letpreintern(c: *cgen, file: *node) void = { // so emitstrarraydata's DATAR rows find an _S_ rodata // row. Must match that helper's interning order exactly // to keep labels stable. + // g-fold #77: gate on the CHASED tinfo kind — the + // N_TARRAY tnode test missed alias-typed [N]str + // globals, desyncing label order vs cstage. let handled: bool = false; if (d.lhs != nil && r != nil) { - if (d.lhs.kind == nkind.N_TARRAY + let au: *tinfo = tichase(d.lhs.type_: *tinfo); + if (au != nil && au.kind == tykind.TY_ARRAY && r.kind == nkind.N_ARRLIT) { - let au: *tinfo = d.lhs.type_: *tinfo; - for (au != nil && au.kind == tykind.TY_NAMED) { - au = au.under; - }; - let eu: *tinfo = nil; - if (au != nil) { - if (au.kind == tykind.TY_ARRAY) { - eu = au.sub; - for (eu != nil && eu.kind == tykind.TY_NAMED) { - eu = eu.under; - }; - }; - }; + let eu: *tinfo = tichase(au.sub); if (eu != nil && eu.kind == tykind.TY_STR) { handled = true; let alen: i32 = au.alen: i32; @@ -2341,6 +2333,15 @@ fn emitletdataw(c: *cgen, file: *node) void = { let sz: i32 = letemitsize(c, d); let issg: bool = letvarisstruct(c, nm); let fsz: i32 = letvarisfloat(c, nm); + // g-fold #77: ONE chase at the dispatch entry. The + // array gates below keyed on the N_TARRAY tnode — + // an alias-typed global's N_TNAME matched no arm + // and the skip-policy ate the decl: no DATAW, + // undefined reference at link. The str/float/ + // struct/slice/tuple gates already alias-walk + // (letvaris* / the tlt tnode walk) and stay put. + let dti: *tinfo = nil; + if (d.lhs != nil) { dti = tichase(d.lhs.type_: *tinfo); }; // C-t3 (#48): tuple global — slot-laid DATAW // row (+ DATAR ptr patches for str elements) // via emittupledata. Unsupported element @@ -2402,8 +2403,8 @@ fn emitletdataw(c: *cgen, file: *node) void = { // below handles it and the duplicate DATAW would // otherwise differ across stages on user code. let isarr8: bool = false; - if (d.lhs != nil) { - if (d.lhs.kind == nkind.N_TARRAY) { isarr8 = true; }; + if (dti != nil) { + if (dti.kind == tykind.TY_ARRAY) { isarr8 = true; }; }; if (sz == 8 && !issg && fsz == 0 && !isarr8 && !istup) { let v: u64 = 0u64; @@ -2555,13 +2556,7 @@ fn emitletdataw(c: *cgen, file: *node) void = { if (issg) { if (d.rhs == nil) { let zsz: i32 = sz; - if (d.lhs != nil) { - let ti: *tinfo = d.lhs.type_: *tinfo; - for (ti != nil && ti.kind == tykind.TY_NAMED) { - ti = ti.under; - }; - if (ti != nil) { zsz = ti.size: i32; }; - }; + if (dti != nil) { zsz = dti.size: i32; }; emitline("DATAW "); emitsymnamehint(c, nm, d.nmod); emitline("(SB),\""); @@ -2583,8 +2578,8 @@ fn emitletdataw(c: *cgen, file: *node) void = { // fold-failures don't corrupt the DATA literal. // No-rhs arrays (e.g. `let buf: [N]u8;`) go through // the same helper with rhs=nil → zero-fill branch. - if (d.lhs != nil) { - if (d.lhs.kind == nkind.N_TARRAY) { + if (dti != nil) { + if (dti.kind == tykind.TY_ARRAY) { let rh: *node = d.rhs; let route: bool = false; if (rh == nil) { route = true; }; @@ -2594,9 +2589,8 @@ fn emitletdataw(c: *cgen, file: *node) void = { }; }; if (route) { - let at: *tinfo = d.lhs.type_: *tinfo; emitarraydata(c, "DATAW", nm, - d.nmod, at, rh); + d.nmod, dti, rh); }; }; }; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index d86f0671..8f49b20c 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -38257,23 +38257,15 @@ export fn letpreintern(c: *cgen, file: *node) void = { // so emitstrarraydata's DATAR rows find an _S_ rodata // row. Must match that helper's interning order exactly // to keep labels stable. + // g-fold #77: gate on the CHASED tinfo kind — the + // N_TARRAY tnode test missed alias-typed [N]str + // globals, desyncing label order vs cstage. let handled: bool = false; if (d.lhs != nil && r != nil) { - if (d.lhs.kind == nkind.N_TARRAY + let au: *tinfo = tichase(d.lhs.type_: *tinfo); + if (au != nil && au.kind == tykind.TY_ARRAY && r.kind == nkind.N_ARRLIT) { - let au: *tinfo = d.lhs.type_: *tinfo; - for (au != nil && au.kind == tykind.TY_NAMED) { - au = au.under; - }; - let eu: *tinfo = nil; - if (au != nil) { - if (au.kind == tykind.TY_ARRAY) { - eu = au.sub; - for (eu != nil && eu.kind == tykind.TY_NAMED) { - eu = eu.under; - }; - }; - }; + let eu: *tinfo = tichase(au.sub); if (eu != nil && eu.kind == tykind.TY_STR) { handled = true; let alen: i32 = au.alen: i32; @@ -39302,6 +39294,15 @@ fn emitletdataw(c: *cgen, file: *node) void = { let sz: i32 = letemitsize(c, d); let issg: bool = letvarisstruct(c, nm); let fsz: i32 = letvarisfloat(c, nm); + // g-fold #77: ONE chase at the dispatch entry. The + // array gates below keyed on the N_TARRAY tnode — + // an alias-typed global's N_TNAME matched no arm + // and the skip-policy ate the decl: no DATAW, + // undefined reference at link. The str/float/ + // struct/slice/tuple gates already alias-walk + // (letvaris* / the tlt tnode walk) and stay put. + let dti: *tinfo = nil; + if (d.lhs != nil) { dti = tichase(d.lhs.type_: *tinfo); }; // C-t3 (#48): tuple global — slot-laid DATAW // row (+ DATAR ptr patches for str elements) // via emittupledata. Unsupported element @@ -39363,8 +39364,8 @@ fn emitletdataw(c: *cgen, file: *node) void = { // below handles it and the duplicate DATAW would // otherwise differ across stages on user code. let isarr8: bool = false; - if (d.lhs != nil) { - if (d.lhs.kind == nkind.N_TARRAY) { isarr8 = true; }; + if (dti != nil) { + if (dti.kind == tykind.TY_ARRAY) { isarr8 = true; }; }; if (sz == 8 && !issg && fsz == 0 && !isarr8 && !istup) { let v: u64 = 0u64; @@ -39516,13 +39517,7 @@ fn emitletdataw(c: *cgen, file: *node) void = { if (issg) { if (d.rhs == nil) { let zsz: i32 = sz; - if (d.lhs != nil) { - let ti: *tinfo = d.lhs.type_: *tinfo; - for (ti != nil && ti.kind == tykind.TY_NAMED) { - ti = ti.under; - }; - if (ti != nil) { zsz = ti.size: i32; }; - }; + if (dti != nil) { zsz = dti.size: i32; }; emitline("DATAW "); emitsymnamehint(c, nm, d.nmod); emitline("(SB),\""); @@ -39544,8 +39539,8 @@ fn emitletdataw(c: *cgen, file: *node) void = { // fold-failures don't corrupt the DATA literal. // No-rhs arrays (e.g. `let buf: [N]u8;`) go through // the same helper with rhs=nil → zero-fill branch. - if (d.lhs != nil) { - if (d.lhs.kind == nkind.N_TARRAY) { + if (dti != nil) { + if (dti.kind == tykind.TY_ARRAY) { let rh: *node = d.rhs; let route: bool = false; if (rh == nil) { route = true; }; @@ -39555,9 +39550,8 @@ fn emitletdataw(c: *cgen, file: *node) void = { }; }; if (route) { - let at: *tinfo = d.lhs.type_: *tinfo; emitarraydata(c, "DATAW", nm, - d.nmod, at, rh); + d.nmod, dti, rh); }; }; }; diff --git a/test/wcc/944_alias_global_decl_run.c b/test/wcc/944_alias_global_decl_run.c new file mode 100644 index 00000000..972de889 --- /dev/null +++ b/test/wcc/944_alias_global_decl_run.c @@ -0,0 +1,456 @@ +/* + * 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 +#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 +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; +}