/* * 631_def_neg_global — top-level `def X: T = N;` for non-trivially- * literal N. The pre-fix bug (#24): `def NEG: i32 = -100;` parsed * the rhs as N_UN(TK_MINUS, N_INTLIT) and both stages' emit_defs / * emitdefconstants skipped any non-leaf-literal shape — no DATA row * was emitted, and any reference to NEG failed to link with * "undefined reference". Workaround in tree had been bundling such * flags into an enum (see the `at` enum at lib/os/os.ww:393). * * The fix lifts the literal-fold core into a shared helper * (fold_int_literal / foldintliteral) that handles * INTLIT/RUNELIT/TRUE/FALSE/NIL plus a unary +/-/~ wrapper over the * same. emit_defs (cstage cmd/w6c/cgen.c) and emitdefconstants * (wwstage selfhost/cmd/wcc/cgen.ww) both gate on it. The shared * helper is reused by eval_enum_value / enumevalmember so the fold * logic lives in one place per stage. * * Rows pin: * - negative-i32: the headline bug. exit=42. * - positive-i32: regression check; the same emit path must still * produce a DATA row for an unwrapped literal. * - tilde-i32: unary ~ over N_INTLIT (the other op in the unary * whitelist beyond TK_MINUS / TK_PLUS). * - negative-i64: 8-byte slot via the same path; verifies sign * extension through the i64 load. * - negative-u32-cast: `def X: u32 = (-1): u32;` exercises an * N_CAST wrapping the N_UN. The fold gate doesn't peel N_CAST * — but cgen sees the cast and consumes it on the read side — * so this is OUT OF SCOPE for the gate; we use u32 differently. * Instead the u32 row uses a tilde to get the all-ones pattern: * `def X: u32 = ~0u32;` truncates cleanly into a u32 slot and * reads back as 0xFFFFFFFF. * - unary-plus-i32: `def X: i32 = +5;` — TK_PLUS noop, completes * the unary whitelist coverage. * * Per-fixture: compile+link+run via the `ww` driver (cstage) and * via `ww_ww` (wwstage) if it exists. Asm byte-identity diff between * cstage's w6c and wwstage's w6c_ww closes the symmetry corner. */ #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[] = { /* The headline #24 case: N_UN(TK_MINUS, N_INTLIT) rhs. * Pre-fix this failed at link time with * "undefined reference to '.NEG'". */ { "negative-i32", "def NEG: i32 = -100;\n" "fn main() i32 = {\n" "\tlet x: i32 = NEG;\n" "\tif (x == -100) { return 42; };\n" "\treturn 1;\n" "};\n", 42 }, /* Regression check: an unwrapped N_INTLIT rhs must still * emit a DATA row. If the fold-gate rewrite accidentally * narrows the whitelist, this row catches it. */ { "positive-i32", "def POS: i32 = 100;\n" "fn main() i32 = {\n" "\tlet x: i32 = POS;\n" "\tif (x == 100) { return 42; };\n" "\treturn 1;\n" "};\n", 42 }, /* Unary tilde over N_INTLIT — the other arithmetic op in * the unary whitelist. ~0 is -1 in i32 two's complement; * reading and comparing as i32 pins the slot's full * sign-extended form. */ { "tilde-i32", "def NTIL: i32 = ~0;\n" "fn main() i32 = {\n" "\tlet x: i32 = NTIL;\n" "\tif (x == -1) { return 42; };\n" "\treturn 1;\n" "};\n", 42 }, /* 8-byte slot via the same fold path. Catches any width- * specific bug in the DATA-row emit (DATA always writes * 8 bytes; the i64 typed def is the natural cardinality * match for that slot). */ { "negative-i64", "def NEG: i64 = -1234567890i64;\n" "fn main() i32 = {\n" "\tlet x: i64 = NEG;\n" "\tif (x == -1234567890i64) { return 42; };\n" "\treturn 1;\n" "};\n", 42 }, /* Unsigned (u32) slot. `~0u32` is all-ones; reading it * back through a u32 local and comparing against the * literal pins both the fold (TK_TILDE) and the * sign-vs-zero-extend on load. */ { "tilde-u32", "def UMAX: u32 = ~0u32;\n" "fn main() i32 = {\n" "\tlet x: u32 = UMAX;\n" "\tif (x == 4294967295u32) { return 42; };\n" "\treturn 1;\n" "};\n", 42 }, /* Unary plus — TK_PLUS noop in the fold. Completes the * unary whitelist coverage. */ { "unary-plus-i32", "def P: i32 = +5;\n" "fn main() i32 = {\n" "\tlet x: i32 = P;\n" "\tif (x == 5) { return 42; };\n" "\treturn 1;\n" "};\n", 42 }, }; static int run_driver(const char *driver, const struct row *r, int i) { char tmpdir[64], src[128], outbin[128], rmcmd[160], cmd[1024]; snprintf(tmpdir, sizeof tmpdir, "/tmp/dng_%d_d_%d", getpid(), i); mkdir(tmpdir, 0755); snprintf(src, sizeof src, "%s/dng_%d_%d.ww", tmpdir, getpid(), i); snprintf(outbin, sizeof outbin, "%s/dng_%d_%d", tmpdir, getpid(), i); snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir); FILE *f = fopen(src, "wb"); if (!f) { runwait(rmcmd); return -1; } fputs(r->src, f); fclose(f); snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null", driver, outbin, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: build via %s failed\n", r->label, driver); runwait(rmcmd); return -1; } int got = runwait(outbin); runwait(rmcmd); return got; } /* asm_byte_identical — generate .s via cstage's w6c and wwstage's * w6c_ww and diff. Pins the symmetric-emit contract: if either * stage's fold helper drifts (e.g. one accepts N_NIL the other * doesn't), the DATA row differs and ww2!=ww3 byte-id breaks. */ 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/dng_asm_%d_%d.ww", getpid(), i); snprintf(cs, sizeof cs, "/tmp/dng_asm_%d_%d_c.s", getpid(), i); snprintf(ws, sizeof ws, "/tmp/dng_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, "def_neg_global: 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, "def_neg_global[%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. */ 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, "def_neg_global: %d/%d fixtures failed\n", fail, total); return 1; } printf("def_neg_global: %d/%d ok\n", total, total); return 0; }