diff options
| author | Cliff Click <cliffc@acm.org> | 2026-09-25 15:28:21 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-09-25 15:28:21 -0700 |
| commit | 29e0127170dce8964d7f85d808d1ec07426d2789 (patch) | |
| tree | f9174685beaf274f3324a08c68f2b0a7137309c2 /chapter23/src | |
| parent | 12bde12b2e18c8b82b1510ac4305de66d1205e1a (diff) | |
| download | Simple-29e0127170dce8964d7f85d808d1ec07426d2789.tar.gz Simple-29e0127170dce8964d7f85d808d1ec07426d2789.zip | |
Restrict subtraction zero peepholes to integer operandspr254-fixes
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.
Diffstat (limited to 'chapter23/src')
3 files changed, 21 insertions, 5 deletions
diff --git a/chapter23/src/main/java/com/seaofnodes/simple/node/SubNode.java b/chapter23/src/main/java/com/seaofnodes/simple/node/SubNode.java index 6dbd55cb..2c4fed39 100644 --- a/chapter23/src/main/java/com/seaofnodes/simple/node/SubNode.java +++ b/chapter23/src/main/java/com/seaofnodes/simple/node/SubNode.java @@ -32,11 +32,11 @@ public class SubNode extends ArithNode { Type t1 = lhs._type; Type t2 = rhs._type; - if( t2.isConstant() && t2 instanceof TypeInteger i && i.value()==0 ) - return lhs; - - if( t1.isConstant() && t1 instanceof TypeInteger i && i.value()==0 ) - return new MinusNode(rhs); + // Keep invalid operands for type checking. + if( t1 instanceof TypeInteger x && t2 instanceof TypeInteger y ) { + if( y.isConstant() && y.value()==0 ) return lhs; + if( x.isConstant() && x.value()==0 ) return new MinusNode(rhs); + } // x - (-y) is x+y if( rhs instanceof MinusNode minus ) diff --git a/chapter23/src/test/java/com/seaofnodes/simple/Chapter12Test.java b/chapter23/src/test/java/com/seaofnodes/simple/Chapter12Test.java index 21759b78..c50be559 100644 --- a/chapter23/src/test/java/com/seaofnodes/simple/Chapter12Test.java +++ b/chapter23/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"}, diff --git a/chapter23/src/test/java/com/seaofnodes/simple/Chapter22Test.java b/chapter23/src/test/java/com/seaofnodes/simple/Chapter22Test.java index a3e9a016..bca06f0c 100644 --- a/chapter23/src/test/java/com/seaofnodes/simple/Chapter22Test.java +++ b/chapter23/src/test/java/com/seaofnodes/simple/Chapter22Test.java @@ -13,6 +13,15 @@ import org.junit.Test; import static org.junit.Assert.*; public class Chapter22Test { + @Test public void testSubZeroTypeError() { + try { + new CodeGen("return null-0;").parse().opto().typeCheck(); + fail("Subtraction must reject null even when the other operand is zero"); + } catch( Parser.ParseException e ) { + assertEquals("Cannot '-' null",e.getMessage()); + } + } + @Test public void testInfiniteReturn() { String src = "struct S { int i; }; S !s = new S; while(1) s.i++; return s.i;"; testCPU(src,"x86_64_v2","SystemV",0,"return Top;"); |
