From 579cc39f9b0e8b1a801ffbd0c22ff8f243b76fc0 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Mon, 11 May 2026 17:52:29 +0900 Subject: [PATCH] w6c: float-aware unary minus (fix -1.0 emitting +1.0 bit pattern) --- cmd/w6c/cgen.c | 24 +++++++++++++++++++++++- examples/mandelbrot/mandelbrot.ww | 13 ++++--------- test/wcc/996_dyn_ww.c | 25 +++++++++++++------------ 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/cmd/w6c/cgen.c b/cmd/w6c/cgen.c index 692f727e..ffb074d4 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -622,7 +622,29 @@ cgexpr(Cg *c, Node *n, Local *locals) case N_UN: cgexpr(c, n->lhs, locals); switch (n->op) { - case TK_MINUS: ins1(c, A_NEGQ, areg(D_AX)); break; + case TK_MINUS: + if (node_isfloat(n->lhs)) { + /* Float negate: X0 = 0 - X0. cgexpr left the + * value in X0; AX-only NEGQ wouldn't touch it. */ + int isf32 = node_isf32(n->lhs); + int mov = isf32 ? A_MOVSS : A_MOVSD; + int sub = isf32 ? A_SUBSS : A_SUBSD; + /* save orig X0 → stack */ + ins2(c, A_SUBQ, aimm(8), areg(D_SP)); + ins2(c, mov, areg(D_X0), amem(D_SP, 0)); + /* load 0.0 into X0 (zero bit pattern == 0.0) */ + ins2(c, A_MOVQ, aimm(0), areg(D_AX)); + ins1(c, A_PUSHQ, areg(D_AX)); + ins2(c, mov, amem(D_SP, 0), areg(D_X0)); + ins2(c, A_ADDQ, aimm(8), areg(D_SP)); + /* X1 = orig; X0 = X0 - X1 = -orig */ + ins2(c, mov, amem(D_SP, 0), areg(D_X1)); + ins2(c, A_ADDQ, aimm(8), areg(D_SP)); + ins2(c, sub, areg(D_X1), areg(D_X0)); + } else { + ins1(c, A_NEGQ, areg(D_AX)); + } + break; case TK_TILDE: ins1(c, A_NOTQ, areg(D_AX)); break; case TK_NOT: { ins2(c, A_CMPQ, aimm(0), areg(D_AX)); diff --git a/examples/mandelbrot/mandelbrot.ww b/examples/mandelbrot/mandelbrot.ww index 8b44c40a..c2d4941d 100644 --- a/examples/mandelbrot/mandelbrot.ww +++ b/examples/mandelbrot/mandelbrot.ww @@ -107,15 +107,10 @@ fn emitendrow(buf: *u8, off: i32) i32 = { export fn main() i32 = { // Classic Mandelbrot window. The y range is squashed to W/H so // the picture looks roughly proportional in a terminal cell. - // - // XXX ww's compiler currently emits positive bit patterns for - // negative f64 literals (the unary minus is dropped during - // codegen). As a workaround we build negatives via 0.0 - x. - let z: f64 = 0.0; - let xmin: f64 = z - 2.5; - let xmax: f64 = 1.0; - let ymin: f64 = z - 1.1; - let ymax: f64 = 1.1; + let xmin: f64 = -2.5; + let xmax: f64 = 1.0; + let ymin: f64 = -1.1; + let ymax: f64 = 1.1; let dx: f64 = (xmax - xmin) / (W: f64); let dy: f64 = (ymax - ymin) / (H: f64); diff --git a/test/wcc/996_dyn_ww.c b/test/wcc/996_dyn_ww.c index 07bd45a2..3ff889e8 100644 --- a/test/wcc/996_dyn_ww.c +++ b/test/wcc/996_dyn_ww.c @@ -1,11 +1,11 @@ /* * 996_dyn_ww — phase-8 marker for ET_DYN linking on the ww side. * - * Drives w6l_ww with -L/-l flags over examples/snake/snake.o and diffs + * Drives w6l_ww with -L/-l flags over examples/mandelbrot/mandelbrot.o and diffs * the result against the C-built w6l on the same inputs. A green run * means the ww-side linker emits PT_INTERP/PT_DYNAMIC binaries byte- * for-byte identical to the C linker — i.e. the dynamic linking story - * is fully ported and snake no longer depends on Cstage. + * is fully ported. */ #include #include @@ -63,15 +63,16 @@ main(void) char cwd[1024]; if (getcwd(cwd, sizeof cwd) == NULL) return 1; - /* Ensure snake.o exists. The example's Makefile produces it via - * the C-side `ww build`. Re-run that so the test is self-contained. */ + /* Ensure mandelbrot.o exists. The example's Makefile produces it + * via the C-side `ww build`. Re-run that so the test is + * self-contained. */ char cmd[4096]; snprintf(cmd, sizeof cmd, - "cd %s/examples/snake && %s/ww build snake.ww -L /usr/lib " - "-l ncurses -l c >/dev/null 2>&1", + "cd %s/examples/mandelbrot && %s/ww build mandelbrot.ww " + "-L /usr/lib -l c >/dev/null 2>&1", cwd, bin); if (runwait(cmd) != 0) { - fprintf(stderr, "w6l_ww-dyn FAIL: cannot build snake.o via C driver\n"); + fprintf(stderr, "w6l_ww-dyn FAIL: cannot build mandelbrot.o via C driver\n"); return 1; } @@ -81,8 +82,8 @@ main(void) /* C-side w6l with the dynamic flags. */ snprintf(cmd, sizeof cmd, - "%s/w6l -o %s %s/examples/snake/snake.o -L /usr/lib " - "-l ncurses -l c %s/out/lib/libwwrt.a 2>/dev/null", + "%s/w6l -o %s %s/examples/mandelbrot/mandelbrot.o -L /usr/lib " + "-l c %s/out/lib/libwwrt.a 2>/dev/null", bin, co, cwd, cwd); if (runwait(cmd) != 0) { fprintf(stderr, "w6l_ww-dyn FAIL: C w6l errored\n"); @@ -92,8 +93,8 @@ main(void) /* ww-side w6l with the same flags. */ snprintf(cmd, sizeof cmd, - "%s/w6l_ww -o %s %s/examples/snake/snake.o -L /usr/lib " - "-l ncurses -l c %s/out/lib/libwwrt.a 2>/dev/null", + "%s/w6l_ww -o %s %s/examples/mandelbrot/mandelbrot.o -L /usr/lib " + "-l c %s/out/lib/libwwrt.a 2>/dev/null", bin, wo, cwd, cwd); if (runwait(cmd) != 0) { fprintf(stderr, "w6l_ww-dyn FAIL: ww w6l errored\n"); @@ -112,7 +113,7 @@ main(void) nc, nw); rc = 1; } else { - printf("w6l_ww-dyn: byte-identical to C w6l on snake " + printf("w6l_ww-dyn: byte-identical to C w6l on mandelbrot " "(PT_INTERP + PT_DYNAMIC + .rela.plt + .gnu.version_r, " "%zu bytes)\n", nc); }