w6c: float-aware unary minus (fix -1.0 emitting +1.0 bit pattern)

This commit is contained in:
2026-05-11 17:52:29 +09:00
parent e50849d5ec
commit 579cc39f9b
3 changed files with 40 additions and 22 deletions

View File

@@ -622,7 +622,29 @@ cgexpr(Cg *c, Node *n, Local *locals)
case N_UN: case N_UN:
cgexpr(c, n->lhs, locals); cgexpr(c, n->lhs, locals);
switch (n->op) { 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_TILDE: ins1(c, A_NOTQ, areg(D_AX)); break;
case TK_NOT: { case TK_NOT: {
ins2(c, A_CMPQ, aimm(0), areg(D_AX)); ins2(c, A_CMPQ, aimm(0), areg(D_AX));

View File

@@ -107,15 +107,10 @@ fn emitendrow(buf: *u8, off: i32) i32 = {
export fn main() i32 = { export fn main() i32 = {
// Classic Mandelbrot window. The y range is squashed to W/H so // Classic Mandelbrot window. The y range is squashed to W/H so
// the picture looks roughly proportional in a terminal cell. // the picture looks roughly proportional in a terminal cell.
// let xmin: f64 = -2.5;
// XXX ww's compiler currently emits positive bit patterns for let xmax: f64 = 1.0;
// negative f64 literals (the unary minus is dropped during let ymin: f64 = -1.1;
// codegen). As a workaround we build negatives via 0.0 - x. let ymax: f64 = 1.1;
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 dx: f64 = (xmax - xmin) / (W: f64); let dx: f64 = (xmax - xmin) / (W: f64);
let dy: f64 = (ymax - ymin) / (H: f64); let dy: f64 = (ymax - ymin) / (H: f64);

View File

@@ -1,11 +1,11 @@
/* /*
* 996_dyn_ww — phase-8 marker for ET_DYN linking on the ww side. * 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 * 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- * 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 * 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 <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -63,15 +63,16 @@ main(void)
char cwd[1024]; char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1; if (getcwd(cwd, sizeof cwd) == NULL) return 1;
/* Ensure snake.o exists. The example's Makefile produces it via /* Ensure mandelbrot.o exists. The example's Makefile produces it
* the C-side `ww build`. Re-run that so the test is self-contained. */ * via the C-side `ww build`. Re-run that so the test is
* self-contained. */
char cmd[4096]; char cmd[4096];
snprintf(cmd, sizeof cmd, snprintf(cmd, sizeof cmd,
"cd %s/examples/snake && %s/ww build snake.ww -L /usr/lib " "cd %s/examples/mandelbrot && %s/ww build mandelbrot.ww "
"-l ncurses -l c >/dev/null 2>&1", "-L /usr/lib -l c >/dev/null 2>&1",
cwd, bin); cwd, bin);
if (runwait(cmd) != 0) { 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; return 1;
} }
@@ -81,8 +82,8 @@ main(void)
/* C-side w6l with the dynamic flags. */ /* C-side w6l with the dynamic flags. */
snprintf(cmd, sizeof cmd, snprintf(cmd, sizeof cmd,
"%s/w6l -o %s %s/examples/snake/snake.o -L /usr/lib " "%s/w6l -o %s %s/examples/mandelbrot/mandelbrot.o -L /usr/lib "
"-l ncurses -l c %s/out/lib/libwwrt.a 2>/dev/null", "-l c %s/out/lib/libwwrt.a 2>/dev/null",
bin, co, cwd, cwd); bin, co, cwd, cwd);
if (runwait(cmd) != 0) { if (runwait(cmd) != 0) {
fprintf(stderr, "w6l_ww-dyn FAIL: C w6l errored\n"); fprintf(stderr, "w6l_ww-dyn FAIL: C w6l errored\n");
@@ -92,8 +93,8 @@ main(void)
/* ww-side w6l with the same flags. */ /* ww-side w6l with the same flags. */
snprintf(cmd, sizeof cmd, snprintf(cmd, sizeof cmd,
"%s/w6l_ww -o %s %s/examples/snake/snake.o -L /usr/lib " "%s/w6l_ww -o %s %s/examples/mandelbrot/mandelbrot.o -L /usr/lib "
"-l ncurses -l c %s/out/lib/libwwrt.a 2>/dev/null", "-l c %s/out/lib/libwwrt.a 2>/dev/null",
bin, wo, cwd, cwd); bin, wo, cwd, cwd);
if (runwait(cmd) != 0) { if (runwait(cmd) != 0) {
fprintf(stderr, "w6l_ww-dyn FAIL: ww w6l errored\n"); fprintf(stderr, "w6l_ww-dyn FAIL: ww w6l errored\n");
@@ -112,7 +113,7 @@ main(void)
nc, nw); nc, nw);
rc = 1; rc = 1;
} else { } 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, " "(PT_INTERP + PT_DYNAMIC + .rela.plt + .gnu.version_r, "
"%zu bytes)\n", nc); "%zu bytes)\n", nc);
} }