From b5632b1fbed15744cf9ef4a7793d9891d4935bea Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Sat, 16 May 2026 09:53:58 +0900 Subject: [PATCH] =?UTF-8?q?cstage+test:=20skip=20redundant=20MOVL=20on=20u?= =?UTF-8?q?32=20=E2=86=92=20enum-u32=20cast=20(#25)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cstage's N_CAST narrow-clamp emitted `MOVL AX, AX` on u32 → enum-u32 casts. wwstage's cgcast walker steps through N_TBANG / N_TNAME alias links only; an enum's aliaslookup returns the N_TENUM body, which breaks the loop and skips the clamp. The asymmetry surfaced during #10 (lib/os/stat) when kstat.mode typed as u32 tripped the cross- stage diff; worker-stat sidestepped by typing kstat.mode as `mode`. Single-site gate: skip the narrow-clamp when the destination type is TY_ENUM. Mirrors wwstage's N_TENUM lacuna exactly. Predicate recursion (type_isint, type_isunsigned) over TY_ENUM stays intact — this is emit-side only. Wwstage unchanged. Test 710 (cast_enum_movl): 5 rows × {exit-code per driver, asm byte-id when w6c_ww built} = 10+ assertions. u32→enum-u32 headline, enum-u32→u32 reverse (pins direction-of-asymmetry), u32→enum-u8 different-width, i64→enum-i32 signed-narrow, struct-field rt mirror of `out.mode = k.mode` (the #10 trip-wire). A principled identity-width identity-sign skip across both stages is filed as #33. The lib/os.ww kstat.mode workaround revert is a sibling cleanup, not in #25 scope. --- Makefile | 7 + cmd/w6c/cgen.c | 14 +- test/wcc/710_cast_enum_movl.c | 306 ++++++++++++++++++++++++++++++++++ 3 files changed, 326 insertions(+), 1 deletion(-) create mode 100644 test/wcc/710_cast_enum_movl.c diff --git a/Makefile b/Makefile index 7f366a0a..bf2ffb77 100644 --- a/Makefile +++ b/Makefile @@ -236,6 +236,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_cgreturn_variant_zero \ $(BIN)/test_param_shadow_mod \ $(BIN)/test_localoff_scope \ + $(BIN)/test_cast_enum_movl \ $(BIN)/test_use_promote_alias \ $(BIN)/test_field_signed $(BIN)/test_frame_argcount \ $(BIN)/test_selfhost $(BIN)/test_w6a_ww $(BIN)/test_w6l_ww \ @@ -443,6 +444,12 @@ $(BIN)/test_localoff_scope: test/wcc/709_localoff_scope.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_cast_enum_movl: test/wcc/710_cast_enum_movl.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_use_promote_alias: test/wcc/699_use_promote_alias.c \ $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(LIB)/libwwrt.a | $(BIN) diff --git a/cmd/w6c/cgen.c b/cmd/w6c/cgen.c index 16c6ac8a..155fcfee 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -4793,8 +4793,20 @@ cgexpr(Cg *c, Node *n, Local *locals) if (!from_f && !to_f && n->type) { Type *tt = n->type; Type *tu = (tt && tt->kind == TY_NAMED) ? tt->under : tt; + /* Wwstage's cgcast walker only steps through N_TBANG / + * N_TNAME alias links; an enum's `aliaslookup` returns + * the N_TENUM body, which breaks the loop and skips the + * clamp. Mirror that here so cstage agrees byte-for-byte + * on a u32→enum-u32 cast (task #25). Predicate recursion + * through TY_ENUM in `type_isint`/`type_isunsigned` + * stays — other call sites rely on it; only this site + * gates on TY_ENUM explicitly. Skip is by dst kind only: + * `enum-u32 → u32` still emits the clamp, matching + * wwstage's asymmetry. (See followup: identity-width + * identity-sign hygiene across both stages.) */ + int dst_is_enum = tu && tu->kind == TY_ENUM; if (tu && type_isint(tu) && tu->size > 0 - && tu->size < 8) { + && tu->size < 8 && !dst_is_enum) { if (type_isunsigned(tu)) { if (tu->size == 4) { ins2(c, A_MOVL, diff --git a/test/wcc/710_cast_enum_movl.c b/test/wcc/710_cast_enum_movl.c new file mode 100644 index 00000000..a61e5020 --- /dev/null +++ b/test/wcc/710_cast_enum_movl.c @@ -0,0 +1,306 @@ +/* + * 710_cast_enum_movl — cstage and wwstage agree byte-for-byte on the + * N_CAST narrow-clamp when the dst is an enum (task #25). + * + * Pre-fix: cstage's N_CAST handler in cmd/w6c/cgen.c walked + * `type_isint`/`type_isunsigned` which recurse through TY_ENUM into + * `t->sub`; for a u32→enum-u32 cast that landed on the size==4 unsigned + * branch and emitted a redundant `MOVL AX, AX`. Wwstage's cgcast walker + * (selfhost/cmd/wcc/cgenexpr.ww) only steps through N_TBANG / N_TNAME; + * `aliaslookup` on an enum returns the N_TENUM body, which breaks the + * loop and skips the clamp. + * + * Surfaced by worker-stat during #10: when kstat.mode was first typed + * as raw `u32`, `out.mode = k.mode` parsed as a u32→enum-u32 cast via + * `fs.mode`, and the cstage→wwstage asm divergence broke 993_ww_ww + + * 995_self_rebuild on the first selfhost pass. Workaround in tree + * (lib/os/os.ww:498, kstat.mode: mode) sidesteps until this fix lands; + * reverting it is a sibling cleanup, out of scope here. + * + * Fix shape: cstage's N_CAST clamp gate adds an explicit + * `tu->kind == TY_ENUM` skip, mirroring wwstage's lacuna. The predicate + * recursion through TY_ENUM in `type_isint`/`type_isunsigned` is + * preserved — other call sites depend on it; only this site gates + * explicitly. Skip is on dst kind only, so `enum-u32 → u32` still emits + * the clamp, matching wwstage's asymmetry. A principled + * identity-width identity-sign skip across both stages is filed as + * a separate followup. + * + * row | shape | gate + * -----------------+----------------------------------------+---------- + * u32_to_enum_u32 | `let y: m = x: m;` with m=enum u32. | exit=7 + * | Headline. Both stages emit ONE MOVL | + byte-id + * | (the slot load); no clamp. | + * enum_u32_to_u32 | reverse direction: `let z: u32 = y;`. | exit=7 + * | Both stages emit clamp MOVL AX, AX | + byte-id + * | (post-load) — pins the asymmetry. | + * u32_to_enum_u8 | dst is enum u8. Pre-fix cstage emits | exit=7 + * | ANDQ $0xFF; wwstage skips. Post-fix | + byte-id + * | both skip — matches wwstage. (Width | + * | narrowing through the enum-u8 is a | + * | known shared gap; principled followup. | + * | The u8-typed local's slot load uses | + * | MOVZBQ which masks anyway, so program | + * | semantics stays right at this width.) | + * i64_to_enum_i32 | signed-narrow: dst is enum i32. | exit=7 + * | Pre-fix cstage emits MOVSXD; post-fix | + byte-id + * | skips. The slot is read with MOVSXD | + * | downstream so sign-ext survives. | + * struct_field_rt | mirror of lib/os fillfilestat: a u32 | exit=7 + * | struct field copied into an enum-typed | + byte-id + * | field by chained N_DOT. Pins the field-| + * | store path through the cast. | + * + * Cstage exit-code rows confirm the binary still runs correctly + * post-fix; the asm-byte-id rows are the regression-pinning rows for + * the symmetric-emit contract. ww2!=ww3 byte-id (995_self_rebuild) + * covers a broader surface but doesn't isolate this corner. + */ +#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[] = { + /* 1. Headline #25 case: u32 → enum-u32. Pre-fix cstage emitted + * `MOVL AX, AX` after the slot load; wwstage skipped. The cast + * is a no-op at runtime so program semantics is unchanged + * either way — exit code stays 7 regardless. The asm-byte-id + * row is what catches the regression. */ + { "u32_to_enum_u32", + "type mymode = enum u32 { A = 1u32, B = 2u32 };\n" + "fn main() i32 = {\n" + "\tlet x: u32 = 7u32;\n" + "\tlet y: mymode = x: mymode;\n" + "\treturn (y: u32): i32;\n" + "};\n", + 7 }, + + /* 2. Reverse direction: enum-u32 → u32. Both stages still emit + * the clamp `MOVL AX, AX` here (wwstage's walker steps through + * the N_TNAME("u32") rhs and primsize=4 fires). The byte-id + * row pins that the fix didn't accidentally widen the skip to + * include this case. */ + { "enum_u32_to_u32", + "type mymode = enum u32 { A = 1u32 };\n" + "fn main() i32 = {\n" + "\tlet y: mymode = 7u32: mymode;\n" + "\tlet z: u32 = y: u32;\n" + "\treturn z: i32;\n" + "};\n", + 7 }, + + /* 3. Different enum width: u32 → enum-u8. Wwstage skips clamp + * because dst is an enum (lacuna); cstage now also skips + * (mirror). The local's u8 slot reads with MOVZBQ later, so + * the value 7 still reads back as 7. Pinning byte-id here + * documents that the skip is by dst-kind, not by dst-size — + * the principled identity-width fix would behave differently + * here, so this row is the canary that flips when the + * followup lands. */ + { "u32_to_enum_u8", + "type small = enum u8 { A = 1u8 };\n" + "fn main() i32 = {\n" + "\tlet x: u32 = 7u32;\n" + "\tlet y: small = x: small;\n" + "\treturn (y: u32): i32;\n" + "};\n", + 7 }, + + /* 4. Signed-narrow path: i64 → enum-i32. Pre-fix cstage emitted + * `MOVSXD AX, AX`; wwstage skipped. Post-fix both skip. The + * enum-i32 local's slot read uses MOVSXD downstream so the + * sign-extension is recovered on use. */ + { "i64_to_enum_i32", + "type sflag = enum i32 { A = 1i32 };\n" + "fn main() i32 = {\n" + "\tlet x: i64 = 7i64;\n" + "\tlet y: sflag = x: sflag;\n" + "\treturn (y: i32);\n" + "};\n", + 7 }, + + /* 5. Mirror of lib/os fillfilestat: struct field of one type + * copied into an enum-typed field of another struct via + * chained N_DOT. The `out.mode = k.mode` shape is exactly + * what blew up worker-stat's first kstat.mode: u32 attempt + * pre-fix (993_ww_ww + 995_self_rebuild went red). */ + { "struct_field_rt", + "type mymode = enum u32 { A = 1u32 };\n" + "type src = struct { mode: u32 };\n" + "type dst = struct { mode: mymode };\n" + "fn main() i32 = {\n" + "\tlet a: src = src { mode = 7u32 };\n" + "\tlet b: dst;\n" + "\tb.mode = a.mode: mymode;\n" + "\treturn (b.mode: u32): i32;\n" + "};\n", + 7 }, +}; + +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/cem_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/cem_%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 #25; an + * exit-code-only comparison wouldn't catch a redundant MOVL drift + * because the program semantics is unchanged. */ +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/cem_asm_%d_%d.ww", getpid(), i); + snprintf(cs, sizeof cs, "/tmp/cem_asm_%d_%d_c.s", getpid(), i); + snprintf(ws, sizeof ws, "/tmp/cem_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, "cast_enum_movl: 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, + "cast_enum_movl[%s][%s]: exit=%d want=%d\n", + drivers[d].name, rows[i].label, + got, rows[i].want); + fail++; + } + } + } + + /* Asm byte-identity diff, only when wwstage is built. This is + * the row that pins the #25 fix. */ + 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, + "cast_enum_movl: %d/%d fixtures failed\n", fail, total); + return 1; + } + printf("cast_enum_movl: %d/%d ok\n", total, total); + return 0; +}