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.
307 lines
10 KiB
C
307 lines
10 KiB
C
/*
|
|
* 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 <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/wait.h>
|
|
|
|
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;
|
|
}
|