/* * 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. * * Runtime ownership moved to the r56_def_* runww fixtures. This wrapper * retains only the source-to-assembly byte-identity comparison between * cstage's w6c and wwstage's w6c_ww. */ #include #include #include #include #include #include "wwtestpkg.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; }; 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" }, /* 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" }, /* 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" }, /* 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" }, /* 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" }, /* 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" }, }; /* 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; wwtest_fputs(r->src, f); fclose(f); int rc = -1; 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); goto cleanup; } 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); goto cleanup; } FILE *fc = fopen(cs, "rb"); FILE *fw = fopen(ws, "rb"); 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); cleanup:; /* a failing compile can still leave a partial .s — ENOENT is the * only tolerable unlink error on the never-created legs. */ int cleanfail = 0; if (unlink(src) != 0 && errno != ENOENT) { perror(src); cleanfail = 1; } if (unlink(cs) != 0 && errno != ENOENT) { perror(cs); cleanfail = 1; } if (unlink(ws) != 0 && errno != ENOENT) { perror(ws); cleanfail = 1; } if (cleanfail && rc == 0) rc = -1; 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; } /* gate on the binary the rows invoke (w6c_ww), not the ww_ww * driver — probing the wrong one skips or fails all rows. */ char wdrv[1024]; snprintf(wdrv, sizeof wdrv, "%s/w6c_ww", bin); int n = (int)(sizeof rows / sizeof rows[0]); int total = 0, fail = 0; /* 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; }