summaryrefslogtreecommitdiff
path: root/chapter18/src
diff options
context:
space:
mode:
Diffstat (limited to 'chapter18/src')
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/SubNode.java19
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter04Test.java28
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java7
3 files changed, 50 insertions, 4 deletions
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/SubNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/SubNode.java
index 357bbc3b..034f2fb1 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/SubNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/SubNode.java
@@ -41,13 +41,24 @@ public class SubNode extends Node {
@Override
public Node idealize() {
+ Node lhs = in(1);
+ Node rhs = in(2);
+ Type t1 = lhs._type;
+ Type t2 = rhs._type;
+
+ // 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( in(2) instanceof MinusNode minus )
- return new AddNode(in(1),minus.in(1));
+ if( rhs instanceof MinusNode minus )
+ return new AddNode(lhs,minus.in(1));
// (-x) - y is -(x+y)
- if( in(1) instanceof MinusNode minus )
- return new MinusNode(new AddNode(minus.in(1),in(2)).peephole());
+ if( lhs instanceof MinusNode minus )
+ return new MinusNode(new AddNode(minus.in(1),rhs).peephole());
return null;
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter04Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter04Test.java
index afed67d1..d2260e1f 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter04Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter04Test.java
@@ -52,6 +52,34 @@ public class Chapter04Test {
}
@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; ");
code.parse();
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java
index 43345809..1e7bfc6b 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java
@@ -8,6 +8,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"},