diff --git a/Makefile b/Makefile index cc4e2d87..8992eba9 100644 --- a/Makefile +++ b/Makefile @@ -239,6 +239,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_int_cast_signed $(BIN)/test_dot_chain \ $(BIN)/test_amp_dot $(BIN)/test_arr_elem_field \ $(BIN)/test_arr_elem_field_write \ + $(BIN)/test_arr_enum_elem \ $(BIN)/test_dot_str_chained_arg \ $(BIN)/test_dot_slice_arg \ $(BIN)/test_dot_tagged_source \ @@ -546,6 +547,12 @@ $(BIN)/test_arr_elem_field_write: test/wcc/681_arr_elem_field_write.c $(BIN)/ww $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_arr_enum_elem: test/wcc/682_arr_enum_elem.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_dot_str_chained_arg: test/wcc/692_dot_str_chained_arg.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 71fee2ba..cf1da7f2 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -16757,6 +16757,26 @@ fn elemsizeofc(c: *cgen, t: *node) i32 = { }; let direct: i32 = elemsizeof(t); if (direct != 8) { return direct; }; + // #8: direct==8 is elemsizeof's "unresolved alias/aggregate" sentinel. + // Read the element width off the checker-stamped tinfo, mirroring the + // sibling elem*c helpers (elemissignedc :915, elemisfloatc :939, which + // already read t.type_.sub) and cstage idx_eff(bt)->sub->size + // (cmd/w6c/cgen.c N_INDEX). elemsizeofc was the odd-one-out among the + // elem*c family — it derived size purely structurally, so a named-narrow + // element (`[N]tkind`, tkind = enum i32) slipped through to a raw 8B slot + // instead of its i32 backing (4), wrong-striding both the cgindex READ + // and the local-array-init STORE (frame-smash). Peel TY_NAMED on the + // indexable and on its element, matching the #270-2 nested-array block + // above. Structural slotsize fallback stays for the t.type_==nil case. + let ti: *tinfo = t.type_: *tinfo; + if (ti != nil) { + for (ti != nil && ti.kind == tykind.TY_NAMED) { ti = ti.under; }; + if (ti != nil) { + let esub: *tinfo = ti.sub; + for (esub != nil && esub.kind == tykind.TY_NAMED) { esub = esub.under; }; + if (esub != nil) { return esub.size: i32; }; + }; + }; let k: nkind = t.kind; let elem: *node = nil; if (k == nkind.N_TPTR) { elem = t.lhs; }; @@ -29491,6 +29511,20 @@ fn cglet(c: *cgen, n: *node) void = { || esubti.kind == tykind.TY_ARRAY || esubti.kind == tykind.TY_TUPLE); if (isagg) { esz = esubti.size: i32; }; + // #8: a named-narrow element (`[N]tk`, tk = enum i32) is + // neither a builtin prim (primsize=0 above, so esz stayed + // the 8 sentinel) nor an aggregate, so the scalar store kept + // an 8B stride/MOVQ and overran the stride-4 frame slot — + // smashing the saved BP / return addr (SEGFAULT). Mirror + // cstage's uniform lu->sub->size (cgen.c:6387) and the + // elemsizeofc read-side fix: take the stamped element tinfo's + // size for a narrow scalar (1/2/4). Wider non-prim elements + // (tagged/slice/str two-half) stay the documented follow-up + // at :1742-1744 — the single-MOVx store below is scalar-only. + if (!isstrel && !isagg && esz == 8 && esubti != nil) { + let es: i32 = esubti.size: i32; + if (es == 1 || es == 2 || es == 4) { esz = es; }; + }; let mop: str = tnodestoreop(c, elemn, esz); // float element → store FROM X0 (MOVSS/MOVSD): cgexpr // leaves a float in X0 and for f32 the #104 CVTSD2SS diff --git a/selfhost/cmd/wcc/cgenstmt.ww b/selfhost/cmd/wcc/cgenstmt.ww index 011577a6..88d1e900 100644 --- a/selfhost/cmd/wcc/cgenstmt.ww +++ b/selfhost/cmd/wcc/cgenstmt.ww @@ -1773,6 +1773,20 @@ fn cglet(c: *cgen, n: *node) void = { || esubti.kind == tykind.TY_ARRAY || esubti.kind == tykind.TY_TUPLE); if (isagg) { esz = esubti.size: i32; }; + // #8: a named-narrow element (`[N]tk`, tk = enum i32) is + // neither a builtin prim (primsize=0 above, so esz stayed + // the 8 sentinel) nor an aggregate, so the scalar store kept + // an 8B stride/MOVQ and overran the stride-4 frame slot — + // smashing the saved BP / return addr (SEGFAULT). Mirror + // cstage's uniform lu->sub->size (cgen.c:6387) and the + // elemsizeofc read-side fix: take the stamped element tinfo's + // size for a narrow scalar (1/2/4). Wider non-prim elements + // (tagged/slice/str two-half) stay the documented follow-up + // at :1742-1744 — the single-MOVx store below is scalar-only. + if (!isstrel && !isagg && esz == 8 && esubti != nil) { + let es: i32 = esubti.size: i32; + if (es == 1 || es == 2 || es == 4) { esz = es; }; + }; let mop: str = tnodestoreop(c, elemn, esz); // float element → store FROM X0 (MOVSS/MOVSD): cgexpr // leaves a float in X0 and for f32 the #104 CVTSD2SS diff --git a/selfhost/cmd/wcc/cgenutil.ww b/selfhost/cmd/wcc/cgenutil.ww index 2d9f9370..f30cf590 100644 --- a/selfhost/cmd/wcc/cgenutil.ww +++ b/selfhost/cmd/wcc/cgenutil.ww @@ -1166,6 +1166,26 @@ fn elemsizeofc(c: *cgen, t: *node) i32 = { }; let direct: i32 = elemsizeof(t); if (direct != 8) { return direct; }; + // #8: direct==8 is elemsizeof's "unresolved alias/aggregate" sentinel. + // Read the element width off the checker-stamped tinfo, mirroring the + // sibling elem*c helpers (elemissignedc :915, elemisfloatc :939, which + // already read t.type_.sub) and cstage idx_eff(bt)->sub->size + // (cmd/w6c/cgen.c N_INDEX). elemsizeofc was the odd-one-out among the + // elem*c family — it derived size purely structurally, so a named-narrow + // element (`[N]tkind`, tkind = enum i32) slipped through to a raw 8B slot + // instead of its i32 backing (4), wrong-striding both the cgindex READ + // and the local-array-init STORE (frame-smash). Peel TY_NAMED on the + // indexable and on its element, matching the #270-2 nested-array block + // above. Structural slotsize fallback stays for the t.type_==nil case. + let ti: *tinfo = t.type_: *tinfo; + if (ti != nil) { + for (ti != nil && ti.kind == tykind.TY_NAMED) { ti = ti.under; }; + if (ti != nil) { + let esub: *tinfo = ti.sub; + for (esub != nil && esub.kind == tykind.TY_NAMED) { esub = esub.under; }; + if (esub != nil) { return esub.size: i32; }; + }; + }; let k: nkind = t.kind; let elem: *node = nil; if (k == nkind.N_TPTR) { elem = t.lhs; }; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index bcfb871b..8d3a153f 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -16757,6 +16757,26 @@ fn elemsizeofc(c: *cgen, t: *node) i32 = { }; let direct: i32 = elemsizeof(t); if (direct != 8) { return direct; }; + // #8: direct==8 is elemsizeof's "unresolved alias/aggregate" sentinel. + // Read the element width off the checker-stamped tinfo, mirroring the + // sibling elem*c helpers (elemissignedc :915, elemisfloatc :939, which + // already read t.type_.sub) and cstage idx_eff(bt)->sub->size + // (cmd/w6c/cgen.c N_INDEX). elemsizeofc was the odd-one-out among the + // elem*c family — it derived size purely structurally, so a named-narrow + // element (`[N]tkind`, tkind = enum i32) slipped through to a raw 8B slot + // instead of its i32 backing (4), wrong-striding both the cgindex READ + // and the local-array-init STORE (frame-smash). Peel TY_NAMED on the + // indexable and on its element, matching the #270-2 nested-array block + // above. Structural slotsize fallback stays for the t.type_==nil case. + let ti: *tinfo = t.type_: *tinfo; + if (ti != nil) { + for (ti != nil && ti.kind == tykind.TY_NAMED) { ti = ti.under; }; + if (ti != nil) { + let esub: *tinfo = ti.sub; + for (esub != nil && esub.kind == tykind.TY_NAMED) { esub = esub.under; }; + if (esub != nil) { return esub.size: i32; }; + }; + }; let k: nkind = t.kind; let elem: *node = nil; if (k == nkind.N_TPTR) { elem = t.lhs; }; @@ -29491,6 +29511,20 @@ fn cglet(c: *cgen, n: *node) void = { || esubti.kind == tykind.TY_ARRAY || esubti.kind == tykind.TY_TUPLE); if (isagg) { esz = esubti.size: i32; }; + // #8: a named-narrow element (`[N]tk`, tk = enum i32) is + // neither a builtin prim (primsize=0 above, so esz stayed + // the 8 sentinel) nor an aggregate, so the scalar store kept + // an 8B stride/MOVQ and overran the stride-4 frame slot — + // smashing the saved BP / return addr (SEGFAULT). Mirror + // cstage's uniform lu->sub->size (cgen.c:6387) and the + // elemsizeofc read-side fix: take the stamped element tinfo's + // size for a narrow scalar (1/2/4). Wider non-prim elements + // (tagged/slice/str two-half) stay the documented follow-up + // at :1742-1744 — the single-MOVx store below is scalar-only. + if (!isstrel && !isagg && esz == 8 && esubti != nil) { + let es: i32 = esubti.size: i32; + if (es == 1 || es == 2 || es == 4) { esz = es; }; + }; let mop: str = tnodestoreop(c, elemn, esz); // float element → store FROM X0 (MOVSS/MOVSD): cgexpr // leaves a float in X0 and for f32 the #104 CVTSD2SS diff --git a/test/wcc/682_arr_enum_elem.c b/test/wcc/682_arr_enum_elem.c new file mode 100644 index 00000000..7f59b77b --- /dev/null +++ b/test/wcc/682_arr_enum_elem.c @@ -0,0 +1,304 @@ +/* + * 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; +}