/* * 951_defdim_struct_run — runtime + byte-id net for #141: a struct * field whose array dimension is a `def` (`[MAX]u8`, MAX a same-module * def), not an integer literal. BOTH stages were wrong, oppositely: * * cstage LOUD-rejected ("array length must be an integer literal"). * resolve_type's N_TARRAY arm folded only N_INTLIT dims; a def-ref * fell through to the err. Root was a phase-ordering trap: def names * weren't bound in scope when resolve_typedecl walked the struct * body, so eval_def_const couldn't see MAX. Fix binds def-name stubs * before type-body resolution, then folds the dim via eval_def_const. * * wwstage SILENTLY mis-laid-out the struct. astsize sized the def-dim * field to 0 (only N_INTLIT dims were read), so the N_TSTRUCT * `off += astsize(field)` loop gave the NEXT field the array's offset * — the trailing scalar overlapped the array, reading/writing the * wrong bytes (ken pdefM4: end != written). Fix routes every N_TARRAY * length reader through a shared arrayelen() that folds the def. * * The TEETH row is the trailing scalar after the def-dim array: pre-fix * ww read a corrupted `end`; post-fix it round-trips and cs==ww byte-id. * Closes #13's def-dim half (the slice-repeat clause stays open). * * cstage `ww build` + run for exit code; w6c vs w6c_ww `.s` cmp for the * rule-10 byte-id gate. The byte-id leg compiles the `ww build`-produced * .combined.ww, not the raw src: w6c does no import resolution, so the * cross-module row (C1, `import os`) only folds once concatenated, and a * same-module row combines to itself. The last row is the ACTUAL * path::buffer shape — a cross-module `os.PATH_MAX` def dimension (the * N_DOT fold arm). u8 array elements + i32 trailing scalars only (the * i32 return is MOVL/MOVSXD byte-id; see 949_dotbase_arr_run). */ #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; } struct row { const char *label; const char *src; int want_exit; }; static const struct row rows[] = { /* TEETH: def-dim array FOLLOWED by a scalar; write both, read the * scalar back. Pre-fix ww laid `end` at the array's offset (array * sized 0) → end read garbage. Post-fix end at offset 4 → 42. */ { "m4_end", "package main;\n" "def M: i32 = 4;\n" "type t = struct { b: [M]u8, end: i32 };\n" "export fn main() i32 = {\n" " let s: t;\n" " s.b[3] = 9u8;\n" " s.end = 42;\n" " return s.end;\n" "};\n", 42 }, /* the array element itself must survive too — read the last byte * (pre-fix the overlapping `end` write would clobber it). */ { "m4_belem", "package main;\n" "def M: i32 = 4;\n" "type t = struct { b: [M]u8, end: i32 };\n" "export fn main() i32 = {\n" " let s: t;\n" " s.b[3] = 200u8;\n" " s.end = 7;\n" " return s.b[3]: i32;\n" "};\n", 200 }, /* the ~4KB path::buffer shape — def MAX=4095, read the trailing * scalar after a 4095-byte array. */ { "m4095_end", "package main;\n" "def MAX: i32 = 4095;\n" "type t = struct { b: [MAX]u8, end: i32 };\n" "export fn main() i32 = {\n" " let s: t;\n" " s.b[4094] = 5u8;\n" " s.end = 99;\n" " return s.end;\n" "};\n", 99 }, /* two scalars after the def-dim array — pins the cumulative offset * (a+c read their written values: 11+22=33). */ { "twoafter", "package main;\n" "def M: i32 = 4;\n" "type t = struct { b: [M]u8, a: i32, c: i32 };\n" "export fn main() i32 = {\n" " let s: t;\n" " s.a = 11;\n" " s.c = 22;\n" " return s.a + s.c;\n" "};\n", 33 }, /* C1 (rob-mandatory): the ACTUAL path::buffer shape — a CROSS- * MODULE def dimension. `def MAX = os.PATH_MAX - 1` folds an N_DOT * (os.PATH_MAX) const-ref through eval_def_const's cross-module arm, * which the same-module rows don't exercise. os.PATH_MAX is 4096, so * MAX is 4095. Byte-id runs on the ww-build combined.ww (w6c alone * does no import resolution, so it can't compile the raw `import os` * source). */ { "xmod_pathmax", "package main;\n" "import os;\n" "def MAX: i32 = os.PATH_MAX - 1;\n" "type t = struct { b: [MAX]u8, end: i32 };\n" "export fn main() i32 = {\n" " let s: t;\n" " s.b[4094] = 7u8;\n" " s.end = 77;\n" " return s.end;\n" "};\n", 77 }, { NULL, NULL, 0 } }; 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; } int main(void) { const char *bin = getenv("BIN"); if (!bin) bin = "out/bin"; char absbin[1024]; 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[1100]; snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); if (access(wdrv, X_OK) != 0) { fprintf(stderr, "defdim_struct: ww_ww missing — cannot run " "the cs==ww byte-id gate (the whole point of this test)\n"); return 1; } int n = 0, fail = 0; for (int i = 0; rows[i].src; i++, n++) { char src[64]; snprintf(src, sizeof src, "/tmp/wwdds_%d_%d.ww", getpid(), i); FILE *f = fopen(src, "wb"); if (f == NULL) { fail++; continue; } fputs(rows[i].src, f); fclose(f); char tmpdir[64]; snprintf(tmpdir, sizeof tmpdir, "/tmp/wwdds_%d_d_%d", getpid(), i); mkdir(tmpdir, 0755); char base[64]; const char *b = strrchr(src, '/'); b = b ? b + 1 : src; snprintf(base, sizeof base, "%s", b); char *dot = strrchr(base, '.'); if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; /* #94 sep layout: each driver's --sep build emits the runnable * binary at + per-package asm under .sepwork/. * Distinct stems so the wwstage sepwork doesn't clobber cstage's; * pin WW_PKGCACHE under tmpdir so out/.pkgcache is untouched. A * cross-module row's `import os` resolves per-package, a same- * module row is just __root — both compare via the sorted glob. */ char cmd[2048], stem_c[160], stem_w[160]; snprintf(stem_c, sizeof stem_c, "%s/%s_c", tmpdir, base); snprintf(stem_w, sizeof stem_w, "%s/%s_w", tmpdir, base); snprintf(cmd, sizeof cmd, "cd %s && WW_PKGCACHE=%s/pkgc_c %s/ww build --sep -o %s %s", tmpdir, tmpdir, bin, stem_c, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: cstage build failed\n", rows[i].label); fail++; unlink(src); snprintf(cmd, sizeof cmd, "rm -rf %s", tmpdir); if (system(cmd)) {} continue; } int got = runwait(stem_c); if (got != rows[i].want_exit) { fprintf(stderr, "row[%s]: cstage exit %d, want %d\n", rows[i].label, got, rows[i].want_exit); fail++; } char cs_s[64], ws_s[64]; snprintf(cs_s, sizeof cs_s, "/tmp/wwdds_%d_%d_cs.s", getpid(), i); snprintf(ws_s, sizeof ws_s, "/tmp/wwdds_%d_%d_ww.s", getpid(), i); snprintf(cmd, sizeof cmd, "cd %s && WW_PKGCACHE=%s/pkgc_w %s/ww_ww build --sep -o %s %s " ">/dev/null 2>&1", tmpdir, tmpdir, bin, stem_w, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: ww_ww build failed\n", rows[i].label); fail++; } else { snprintf(cmd, sizeof cmd, "cat %s.sepwork/*.s > %s 2>/dev/null", stem_c, cs_s); if (system(cmd)) {} snprintf(cmd, sizeof cmd, "cat %s.sepwork/*.s > %s 2>/dev/null", stem_w, ws_s); if (system(cmd)) {} if (slurp_eq(cs_s, ws_s) != 0) { fprintf(stderr, "row[%s]: cstage/wwstage .s DIFFER " "(rule-10 byte-id violation)\n", rows[i].label); fail++; } } snprintf(cmd, sizeof cmd, "rm -rf %s", tmpdir); if (system(cmd)) {} unlink(src); unlink(cs_s); unlink(ws_s); } if (fail) { fprintf(stderr, "%d/%d defdim-struct tests failed\n", fail, n); return 1; } printf("defdim_struct: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n); return 0; }