/* * 682_arr_enum_elem — cstage and wwstage agree, byte-for-byte and at * runtime, on `[N]enum` element access (task #8). * * The bug: wwstage's elemsizeofc (selfhost/cmd/wcc/cgenutil.ww) was the * odd-one-out among the elem*c helpers — its siblings elemissignedc / * elemisfloatc read the checker-stamped tinfo (t.type_.sub), but * elemsizeofc derived the element width purely structurally * (elemsizeof -> primsize -> slotsize). A named-narrow element * (`[N]tk`, tk = enum i32) is not a builtin prim, so primsize returned * 0 and elemsizeofc fell through to a raw 8-byte slot instead of the * enum's i32 backing (4). That mis-sized BOTH manifestations through * the one choke-point: * (a) cgindex READ — `a[i]` strode by 8 (MOVQ) where cstage strode by * 4 (MOVSXD), reading the wrong/out-of-bounds element for i>=1. * (b) array-init STORE — a local `[N]enum` literal init stored at * stride 8 into a stride-4 frame slot, overrunning the slot and * smashing the frame -> wwstage-built binary SEGFAULTED. * cstage is runtime-correct (cgen.c N_INDEX idx_eff(bt)->sub->size, * N_LET array-init lu->sub->size, cgen.c:6387). The fix aligns wwstage * UP: elemsizeofc now reads the element size off the stamped tinfo * (peeling TY_NAMED), recovering 4 for `enum i32` and bringing all four * elem*c helpers onto the same tinfo SSoT. * * No in-tree `[N]enum` / aliased-narrow element existed before kwtab, * which is why this was byte-id-gate-blind until now. * * row | shape | want * -----------------+------------------------------------+-------------- * global_idx0 | global [4]tk, return g[0]. idx0 is | 7 + byte-id * | stride-independent (offset 0). | * global_read | global [4]tk, return g[2]. Pre-fix | 99 + byte-id * | stride-8 read offset 16 = past the | * | 16-byte array -> garbage. | * local_read | LOCAL [4]tk init, return a[1]. | 21 + byte-id * | Exercises the array-init STORE + | * | the stride-4 READ together. | * local_init_sum | LOCAL [4]tk, return a[0]+a[3]. | 10 + byte-id * | First+last element after init. | * local_signed | LOCAL [2]tk with a negative enum | 251 (-5) * | value, return a[0]:i32. Pins the | + byte-id * | MOVSXD sign-extend at stride 4 | * | (pre-fix MOVQ read 8 bytes). | * frame_smash | LOCAL [6]tk init + read a[5]. | 5 + byte-id * | Pre-fix stride-8 store writes 48 | * | bytes into a 24-byte slot -> | * | frame smash / SEGFAULT under | * | wwstage. Correct stride-4 store | * | (24 bytes) round-trips a[5]=5. | * width1_u8 | LOCAL [3]tu (enum u8), read a[1]. | 200 + byte-id * | Pins the narrow-scalar class at | * | width 1: stride-1 store + MOVZBQ | * | zero-extend load (UNSIGNED enum). | * | Pre-fix stride-8 store/read over- | * | runs the 3-byte slot -> garbage. | * width2_i16 | LOCAL [3]ts (enum i16) w/ negative | 251 (-5) * | value, read a[1]:i32. Pins width 2:| + byte-id * | stride-2 store + MOVSWQ sign- | * | extend (pre-fix stride-8 garbage). | * * width1/width2 widen the coverage from the single enum-i32 (width 4) * case to the whole {1,2,4} narrow-scalar class the fix closes, and add * the unsigned (MOVZBQ) load path alongside the signed (MOVSXD/MOVSWQ). * * Exit-code rows confirm both stages run correctly. The asm-byte-id * rows pin the symmetric stride-4 MOVSXD emit (cstage == wwstage); * pre-fix wwstage emitted stride-8 MOVQ, so the diff was non-empty. */ #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; }; static const struct row rows[] = { { "global_idx0", "type tk = enum i32 { A = 7i32, B = 21i32, C = 99i32, D = 3i32 };\n" "let g: [4]tk = [tk.A, tk.B, tk.C, tk.D];\n" "fn main() i32 = {\n" "\treturn g[0]: i32;\n" "};\n", 7 }, { "global_read", "type tk = enum i32 { A = 7i32, B = 21i32, C = 99i32, D = 3i32 };\n" "let g: [4]tk = [tk.A, tk.B, tk.C, tk.D];\n" "fn main() i32 = {\n" "\treturn g[2]: i32;\n" "};\n", 99 }, { "local_read", "type tk = enum i32 { A = 7i32, B = 21i32, C = 99i32, D = 3i32 };\n" "fn main() i32 = {\n" "\tlet a: [4]tk = [tk.A, tk.B, tk.C, tk.D];\n" "\treturn a[1]: i32;\n" "};\n", 21 }, { "local_init_sum", "type tk = enum i32 { A = 7i32, B = 21i32, C = 99i32, D = 3i32 };\n" "fn main() i32 = {\n" "\tlet a: [4]tk = [tk.A, tk.B, tk.C, tk.D];\n" "\treturn (a[0]: i32) + (a[3]: i32);\n" "};\n", 10 }, { "local_signed", "type tk = enum i32 { A = 7i32, G = -5i32 };\n" "fn main() i32 = {\n" "\tlet a: [2]tk = [tk.G, tk.A];\n" "\treturn a[0]: i32;\n" "};\n", 251 }, { "frame_smash", "type tk = enum i32 { A = 7i32, B = 21i32, C = 99i32,\n" "\tD = 3i32, E = 11i32, F = 5i32 };\n" "fn main() i32 = {\n" "\tlet a: [6]tk = [tk.A, tk.B, tk.C, tk.D, tk.E, tk.F];\n" "\treturn a[5]: i32;\n" "};\n", 5 }, { "width1_u8", "type tu = enum u8 { A = 7u8, B = 200u8, C = 3u8 };\n" "fn main() i32 = {\n" "\tlet a: [3]tu = [tu.A, tu.B, tu.C];\n" "\treturn a[1]: i32;\n" "};\n", 200 }, { "width2_i16", "type ts = enum i16 { A = 7i16, G = -5i16, C = 99i16 };\n" "fn main() i32 = {\n" "\tlet a: [3]ts = [ts.A, ts.G, ts.C];\n" "\treturn a[1]: i32;\n" "};\n", 251 }, }; static int run_driver(const char *driver, const struct row *r, int i) { char src[64], tmpdir[64], cmd[1024]; snprintf(src, sizeof src, "/tmp/aee_%d_%d.ww", getpid(), i); snprintf(tmpdir, sizeof tmpdir, "/tmp/aee_%d_d_%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 && %s build %s 2>/dev/null", tmpdir, driver, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: build via %s failed\n", r->label, driver); unlink(src); rmdir(tmpdir); return -1; } const char *base = strrchr(src, '/'); base = base ? base + 1 : src; char outbin[128]; 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); rmdir(tmpdir); return got; } /* asm_byte_identical — generate .s via cstage's w6c and wwstage's * w6c_ww and diff. This is the regression-pinning row for #8: pre-fix * wwstage emitted stride-8 MOVQ where cstage emitted stride-4 MOVSXD, * so the diff was non-empty. */ static int asm_byte_identical(const char *bin, const struct row *r, int i) { char src[64], cs[64], ws[64], cmd[1024]; snprintf(src, sizeof src, "/tmp/aee_asm_%d_%d.ww", getpid(), i); snprintf(cs, sizeof cs, "/tmp/aee_asm_%d_%d_c.s", getpid(), i); snprintf(ws, sizeof ws, "/tmp/aee_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; } FILE *fc = fopen(cs, "rb"); FILE *fw = fopen(ws, "rb"); int rc = 0; if (!fc || !fw) { rc = -1; } else { for (;;) { int a = fgetc(fc); int b = fgetc(fw); if (a != b) { rc = -1; break; } if (a == EOF) break; } } if (fc) fclose(fc); if (fw) fclose(fw); 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[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 cdrv[1024]; snprintf(cdrv, sizeof cdrv, "%s/ww", bin); char wdrv[1024]; snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); struct { const char *name; const char *path; int gated_on_existence; } drivers[] = { { "cstage", cdrv, 0 }, { "wwstage", wdrv, 1 }, { NULL, NULL, 0 }, }; int n = (int)(sizeof rows / sizeof rows[0]); int total = 0, fail = 0; for (int d = 0; drivers[d].name; d++) { if (drivers[d].gated_on_existence && access(drivers[d].path, X_OK) != 0) { fprintf(stderr, "arr_enum_elem: skip %s (no %s)\n", drivers[d].name, drivers[d].path); continue; } for (int i = 0; i < n; i++) { int got = run_driver(drivers[d].path, &rows[i], i); total++; if (got != rows[i].want) { fprintf(stderr, "arr_enum_elem[%s][%s]: exit=%d want=%d\n", drivers[d].name, rows[i].label, got, rows[i].want); fail++; } } } if (access(wdrv, X_OK) == 0) { for (int i = 0; i < n; i++) { total++; if (asm_byte_identical(bin, &rows[i], i) != 0) fail++; } } if (fail) { fprintf(stderr, "arr_enum_elem: %d/%d fixtures failed\n", fail, total); return 1; } printf("arr_enum_elem: %d/%d ok\n", total, total); return 0; }