cstage+selfhost+test: fold unary-over-literal in def DATA emit (#24)
Top-level `def NEG: i32 = -100;` skipped DATA emission in both stages — cstage's emit_defs and wwstage's emitdefconstants each carried a literal-leaf whitelist that excluded N_UN nodes. Same gap in check.c's eval_enum_value cstage-side. Surfaced during #10 (lib/os forced an `at` enum for AT_FDCWD=-100 etc. as workaround). Factor a single fold_int_literal helper (cstage check.c; wwstage cgen.ww). Handles N_INTLIT / N_RUNELIT / N_TRUE / N_FALSE / N_NIL plus N_UN with TK_MINUS / TK_TILDE / TK_PLUS recursively. Consume from eval_enum_value, emit_defs, emitdefconstants, enumevalmember — single source of truth for "is this a literal-leaf foldable". Side effect: cstage's def-emit set widens from {INTLIT, RUNELIT, TRUE} to match wwstage's pre-existing 5-shape set plus the new unary peel. Bootstrap byte-id holds (995_self_rebuild green). Test 631 (def_neg_global): 6 rows × cstage/wwstage run + asm byte-identity diff. Covers all three unary arms (-, ~, +), positive regression-pin, i32 + i64 + u32 slots. Follows up #26: revert lib/os.ww `at` enum to three top-level defs.
This commit is contained in:
285
test/wcc/631_def_neg_global.c
Normal file
285
test/wcc/631_def_neg_global.c
Normal file
@@ -0,0 +1,285 @@
|
||||
/*
|
||||
* 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 <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[] = {
|
||||
/* The headline #24 case: N_UN(TK_MINUS, N_INTLIT) rhs.
|
||||
* Pre-fix this failed at link time with
|
||||
* "undefined reference to '<tmpname>.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 src[64], tmpdir[64], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/dng_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/dng_%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. 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;
|
||||
}
|
||||
Reference in New Issue
Block a user