diff --git a/Makefile b/Makefile index 1ea2cfd5..60f65f02 100644 --- a/Makefile +++ b/Makefile @@ -332,6 +332,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_f64crossmod_run \ $(BIN)/test_tuprecv_run \ $(BIN)/test_f64xmm_run \ + $(BIN)/test_f32lit_run \ $(BIN)/test_tuprecv_f64_run \ $(BIN)/test_floats_run \ $(BIN)/test_size_type_run \ @@ -1133,6 +1134,11 @@ $(BIN)/test_f64xmm_run: test/wcc/955_f64xmm_run.c $(BIN)/ww \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_f32lit_run: test/wcc/964_f32lit_run.c $(BIN)/ww \ + $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_tuprecv_f64_run: test/wcc/956_tuprecv_f64_run.c $(BIN)/ww \ $(BIN)/w6c $(BIN)/w6c_ww $(BIN)/w6a $(BIN)/w6l \ $(LIB)/libwwrt.a | $(BIN) diff --git a/cmd/w6c/cgen.c b/cmd/w6c/cgen.c index 903ab0d8..299776a3 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -1887,12 +1887,20 @@ cgexpr(Cg *c, Node *n, Local *locals) case N_RUNELIT: if (node_isfloat(n)) { cgexpr_float(c, (double)(long long)n->uval); + /* #104: cgexpr_float materialises a DOUBLE in X0; an + * f32-typed literal must narrow with hardware single- + * rounding so the downstream MOVSS reads a true single. */ + if (node_isf32(n)) + ins2(c, A_CVTSD2SS, areg(D_X0), areg(D_X0)); break; } cgexpr_int(c, (long long)n->uval); break; case N_FLOATLIT: cgexpr_float(c, n->fval); + /* #104: narrow the double in X0 to single for an f32 literal. */ + if (node_isf32(n)) + ins2(c, A_CVTSD2SS, areg(D_X0), areg(D_X0)); break; case N_STRLIT: { /* str IS []u8: the (ptr, len, cap) triple — ptr in AX, len in diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 3ab14743..b4a40076 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -14209,6 +14209,12 @@ fn cgexpr(c: *cgen, n: *node) void = { let fv: f64 = (n.uval: i64): f64; let pu: *u64 = (&fv): *u64; cgfloatbits(c, *pu); + // #104: cgfloatbits materialises a DOUBLE in X0; an + // f32-typed literal must narrow with hardware single- + // rounding so the downstream MOVSS reads a true single. + if (isf32type(c, n)) { + emitline("\tCVTSD2SS\tX0, X0\n"); + }; return; }; // Print signed (i64), not unsigned (u64). C cgen uses @@ -14222,10 +14228,12 @@ fn cgexpr(c: *cgen, n: *node) void = { }; if (k == nkind.N_FLOATLIT) { // The bits come from n.uval — the parser populates it from - // the lexer's bitcast of t.fval. The f32 narrowing is handled - // at the consumer site, not here — the literal always carries - // the full double precision until typed by context. + // the lexer's bitcast of t.fval. cgfloatbits(c, n.uval); + // #104: narrow the double in X0 to single for an f32 literal. + if (isf32type(c, n)) { + emitline("\tCVTSD2SS\tX0, X0\n"); + }; return; }; if (k == nkind.N_RUNELIT) { diff --git a/selfhost/cmd/wcc/cgenexpr.ww b/selfhost/cmd/wcc/cgenexpr.ww index 00d05171..b78b6e67 100644 --- a/selfhost/cmd/wcc/cgenexpr.ww +++ b/selfhost/cmd/wcc/cgenexpr.ww @@ -50,6 +50,12 @@ fn cgexpr(c: *cgen, n: *node) void = { let fv: f64 = (n.uval: i64): f64; let pu: *u64 = (&fv): *u64; cgfloatbits(c, *pu); + // #104: cgfloatbits materialises a DOUBLE in X0; an + // f32-typed literal must narrow with hardware single- + // rounding so the downstream MOVSS reads a true single. + if (isf32type(c, n)) { + emitline("\tCVTSD2SS\tX0, X0\n"); + }; return; }; // Print signed (i64), not unsigned (u64). C cgen uses @@ -63,10 +69,12 @@ fn cgexpr(c: *cgen, n: *node) void = { }; if (k == nkind.N_FLOATLIT) { // The bits come from n.uval — the parser populates it from - // the lexer's bitcast of t.fval. The f32 narrowing is handled - // at the consumer site, not here — the literal always carries - // the full double precision until typed by context. + // the lexer's bitcast of t.fval. cgfloatbits(c, n.uval); + // #104: narrow the double in X0 to single for an f32 literal. + if (isf32type(c, n)) { + emitline("\tCVTSD2SS\tX0, X0\n"); + }; return; }; if (k == nkind.N_RUNELIT) { diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index c0ed90b8..a44c3b54 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -14209,6 +14209,12 @@ fn cgexpr(c: *cgen, n: *node) void = { let fv: f64 = (n.uval: i64): f64; let pu: *u64 = (&fv): *u64; cgfloatbits(c, *pu); + // #104: cgfloatbits materialises a DOUBLE in X0; an + // f32-typed literal must narrow with hardware single- + // rounding so the downstream MOVSS reads a true single. + if (isf32type(c, n)) { + emitline("\tCVTSD2SS\tX0, X0\n"); + }; return; }; // Print signed (i64), not unsigned (u64). C cgen uses @@ -14222,10 +14228,12 @@ fn cgexpr(c: *cgen, n: *node) void = { }; if (k == nkind.N_FLOATLIT) { // The bits come from n.uval — the parser populates it from - // the lexer's bitcast of t.fval. The f32 narrowing is handled - // at the consumer site, not here — the literal always carries - // the full double precision until typed by context. + // the lexer's bitcast of t.fval. cgfloatbits(c, n.uval); + // #104: narrow the double in X0 to single for an f32 literal. + if (isf32type(c, n)) { + emitline("\tCVTSD2SS\tX0, X0\n"); + }; return; }; if (k == nkind.N_RUNELIT) { diff --git a/test/wcc/964_f32lit_run.c b/test/wcc/964_f32lit_run.c new file mode 100644 index 00000000..14e2e397 --- /dev/null +++ b/test/wcc/964_f32lit_run.c @@ -0,0 +1,219 @@ +/* + * 964_f32lit_run — runtime + byte-id regression net for #104 fold-1: an + * f32-typed float literal must NARROW to single precision in X0 before + * the f32 consumer reads it. Both stages (cstage cgen.c cgexpr_float, + * wwstage cgenexpr.ww cgfloatbits) materialise a float literal as a + * 64-bit DOUBLE in X0 (MOVQ bits -> MOVSD). For an f32-typed literal the + * downstream MOVSS store/return then reads the LOW 4 BYTES of that + * double — garbage (0x00000000 == 0.0f for most clean values, which is + * why 0.0 coincidentally survived the bug). fold-1 appends CVTSD2SS + * X0,X0 at both literal sites (N_FLOATLIT + the float-typed N_INTLIT + * arm) when the node is f32-typed, so the value reaches X0 as a true + * single. Both stages emit byte-identical asm, so the 990-997 byte-id + * gates can NEVER catch a reintroduction — only an executed-and-checked + * runtime probe can. (951_f64cgen is GATE-BLIND here: its f32 rows only + * assert NaN ordering, never a concrete f32 value.) + * + * SCOPE: fold-1 covers literals that carry an explicit f32 TYPE — the + * `f32` suffix (`1.0f32`, `1.5f32`) and the no-decimal N_INTLIT-float + * arm (`8f32`). An UN-suffixed literal in an f32 context (`let x: f32 = + * 1.0`) stays ty_untyped_float through the checker, so the literal node + * is never f32-typed and fold-1's branch can't fire — materialised as a + * double, stored low-4-bytes -> 0.0f. Fixing that needs fold-2: the + * checker lowering untyped-float literals to their f32 context type + * (#104, both checkers). This probe therefore uses suffixed literals + * exclusively; the un-suffixed gap is tracked under #104 fold-2. + * + * Each row carries BOTH dimensions (like 955_f64xmm_run): + * (a) cstage `ww build` + run, asserting the exit code. + * (b) w6c vs w6c_ww `.s` cmp — FAILS if the stages diverge (rule-10). + */ +#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_exit; }; + +static const struct row rows[] = { + /* The hole 951 misses: a CONCRETE non-trivial f32 value. On the + * bug `let x: f32 = 1.0f32` stores the low 4 bytes of double 1.0 + * (== 0x00000000 == 0.0f), so x:f64 == 0.0 != 1.0 -> 1. */ + { "bare_value", + "package main;\n" + "export fn main() i32 = {\n" + " let x: f32 = 1.0f32;\n" + " if (x: f64 != 1.0) { return 1; };\n" + " return 0;\n" + "};\n", 0 }, + /* arith on f32 literals: 1.5 + 2.5 == 4.0. On the bug both operands + * land as 0.0f -> sum 0.0 != 4.0. */ + { "arith", + "package main;\n" + "export fn main() i32 = {\n" + " let a: f32 = 1.5f32;\n" + " let b: f32 = 2.5f32;\n" + " let s: f32 = a + b;\n" + " if (s: f64 != 4.0) { return 1; };\n" + " return 0;\n" + "};\n", 0 }, + /* value-propagation: f32 returned through an f32 fn + arith on an + * f32 literal, truncated to i32. 2.5 + 1.5 == 4.0 -> 4. */ + { "return_arith", + "package main;\n" + "fn g() f32 = { return 2.5f32; };\n" + "export fn main() i32 = {\n" + " let r: f32 = g() + 1.5f32;\n" + " return r: i32;\n" + "};\n", 4 }, + /* the float-typed N_INTLIT arm (`8f32` — no decimal, f32 suffix). + * Same materialiser, same fold-1 branch. -> 8. */ + { "intlit_f32_arm", + "package main;\n" + "export fn main() i32 = {\n" + " let y: f32 = 8f32;\n" + " return y: i32;\n" + "};\n", 8 }, + /* genuine single-rounding: 2^24 + 1 is NOT representable in f32 and + * rounds back to 2^24 (round-to-even). If the add ran in double it + * would be 16777217.0 != 16777216.0 -> 1. Proves the value is a + * true single, not the low half of a double. */ + { "single_round", + "package main;\n" + "export fn main() i32 = {\n" + " let big: f32 = 16777216.0f32;\n" + " let r: f32 = big + 1.0f32;\n" + " if (r: f64 != 16777216.0) { return 1; };\n" + " return 0;\n" + "};\n", 0 }, + { NULL, NULL, 0 } +}; + +static int +slurp_eq(const char *a, const char *b) +{ + FILE *fa = fopen(a, "rb"); + FILE *fb = fopen(b, "rb"); + if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; } + int rc = 0; + for (;;) { + int ca = fgetc(fa); + int cb = fgetc(fb); + if (ca != cb) { rc = -1; break; } + if (ca == EOF) break; + } + fclose(fa); fclose(fb); + 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 w6c[1100], w6c_ww[1100]; + snprintf(w6c, sizeof w6c, "%s/w6c", bin); + snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin); + if (access(w6c_ww, X_OK) != 0) { + fprintf(stderr, "f32lit: w6c_ww missing — cannot run the " + "cs==ww byte-id gate (the whole point of this test)\n"); + return 1; + } + + int n = 0, fail = 0; + for (int i = 0; rows[i].src; i++, n++) { + char src[64]; + snprintf(src, sizeof src, "/tmp/wwf32l_%d_%d.ww", getpid(), i); + FILE *f = fopen(src, "wb"); + if (f == NULL) { fail++; continue; } + fputs(rows[i].src, f); + fclose(f); + + /* (a) cstage build + run. */ + char tmpdir[64]; + snprintf(tmpdir, sizeof tmpdir, "/tmp/wwf32l_%d_d_%d", + getpid(), i); + mkdir(tmpdir, 0755); + + char cmd[2048]; + snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s", + tmpdir, bin, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: cstage build failed\n", + rows[i].label); + fail++; + unlink(src); rmdir(tmpdir); + continue; + } + + char outbin[128]; + const char *base = strrchr(src, '/'); + base = base ? base + 1 : src; + snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base); + char *dot = strrchr(outbin, '.'); + if (dot && strcmp(dot, ".ww") == 0) *dot = '\0'; + + int got = runwait(outbin); + if (got != rows[i].want_exit) { + fprintf(stderr, "row[%s]: cstage exit %d, want %d\n", + rows[i].label, got, rows[i].want_exit); + fail++; + } + unlink(outbin); rmdir(tmpdir); + + /* (b) cs==ww byte-id gate: emit .s from both stages, cmp. */ + char cs_s[64], ws_s[64]; + snprintf(cs_s, sizeof cs_s, "/tmp/wwf32l_%d_%d_cs.s", + getpid(), i); + snprintf(ws_s, sizeof ws_s, "/tmp/wwf32l_%d_%d_ww.s", + getpid(), i); + + snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", + w6c, cs_s, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: w6c failed\n", rows[i].label); + fail++; unlink(src); continue; + } + snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", + w6c_ww, ws_s, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: w6c_ww failed\n", + rows[i].label); + fail++; unlink(src); unlink(cs_s); continue; + } + if (slurp_eq(cs_s, ws_s) != 0) { + fprintf(stderr, + "row[%s]: cstage/wwstage .s DIFFER (rule-10 " + "byte-id violation)\n", rows[i].label); + fail++; + } + unlink(src); unlink(cs_s); unlink(ws_s); + } + + if (fail) { + fprintf(stderr, "%d/%d f32 literal-materialise tests failed\n", + fail, n); + return 1; + } + printf("f32lit: %d/%d ok (cstage run + cs==ww byte-id)\n", n, n); + return 0; +}