From dd907c2d86eaaad0910e7bfe3f0d25e5caf8bdf7 Mon Sep 17 00:00:00 2001 From: Hsiang-Ying Fu <66062782+fuhsnn@users.noreply.github.com> Date: Sat, 26 Sep 2026 06:35:22 +0800 Subject: Extend and backport SubNode peephole (#254) * Extend and backport SubNode peephole * Add tests * Restrict subtraction zero peepholes to integer operands Require resolved integer mode in chapter 25 so zero subtraction preserves floating-point signed zero. Keep nonnumeric operands in the graph for subtraction diagnostics. Add signed-zero coverage from chapter 12 and null-subtraction diagnostics from chapter 22. Confirmed the regressions fail on the original PR. Full Make suites pass in chapters 4-25 (5462 tests), including fresh chapter 25 sys.o compilation. --------- Co-authored-by: Cliff Click --- .../java/com/seaofnodes/simple/Chapter04Test.java | 28 ++++++++++++++++++++++ .../java/com/seaofnodes/simple/Chapter12Test.java | 7 ++++++ 2 files changed, 35 insertions(+) (limited to 'chapter21/src/test/java/com') diff --git a/chapter21/src/test/java/com/seaofnodes/simple/Chapter04Test.java b/chapter21/src/test/java/com/seaofnodes/simple/Chapter04Test.java index c6199193..e5a16680 100644 --- a/chapter21/src/test/java/com/seaofnodes/simple/Chapter04Test.java +++ b/chapter21/src/test/java/com/seaofnodes/simple/Chapter04Test.java @@ -52,6 +52,34 @@ public class Chapter04Test { assertEquals("return arg;", code.print()); } + @Test + public void testSub0() { + CodeGen code = new CodeGen("return arg-0;"); + code.parse(); + assertEquals("return arg;", code.print()); + } + + @Test + public void test0Sub() { + CodeGen code = new CodeGen("return 0-arg;"); + code.parse(); + assertEquals("return (-arg);", code.print()); + } + + @Test + public void testSubNeg() { + CodeGen code = new CodeGen("return 3--arg;"); + code.parse(); + assertEquals("return (arg+3);", code.print()); + } + + @Test + public void testNegSub() { + CodeGen code = new CodeGen("return -arg-3;"); + code.parse(); + assertEquals("return (-(arg+3));", code.print()); + } + @Test public void testVarArg() { CodeGen code = new CodeGen("return arg; "); diff --git a/chapter21/src/test/java/com/seaofnodes/simple/Chapter12Test.java b/chapter21/src/test/java/com/seaofnodes/simple/Chapter12Test.java index 21759b78..c50be559 100644 --- a/chapter21/src/test/java/com/seaofnodes/simple/Chapter12Test.java +++ b/chapter21/src/test/java/com/seaofnodes/simple/Chapter12Test.java @@ -7,6 +7,13 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; public class Chapter12Test { + @Test public void testSubZeroFloat() { + var code = new CodeGen("flt x = arg; return 0-x;").parse().opto().typeCheck(); + // Compare strings so +0.0 and -0.0 remain distinct. + assertEquals("0.0", Eval2.eval(code,0)); + assertEquals("-1.0", Eval2.eval(code,1)); + } + @Test public void testDeadNumericReturns() { for( String[] test : new String[][] { {"if(1) return 7; else return 2.5;", "7"}, -- cgit v1.2.3