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/Parser.java2
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/ToIntegerNode.java23
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java4
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java19
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter17bTest.java4
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java5
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Eval2.java2
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java2
8 files changed, 53 insertions, 8 deletions
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/Parser.java b/chapter18/src/main/java/com/seaofnodes/simple/Parser.java
index 3e80aa60..82ad8d04 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/Parser.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/Parser.java
@@ -1350,6 +1350,8 @@ public class Parser {
// zero/sign extend. "i" is limited to either classic unsigned (min==0) or
// classic signed (min=minus-power-of-2); max=power-of-2-minus-1.
private Node zsMask(Node val, Type t ) {
+ if( val._type instanceof TypeFloat && t instanceof TypeInteger )
+ val = new ToIntegerNode(val).peephole();
if( !(val._type instanceof TypeInteger tval && t instanceof TypeInteger t0 && !tval.isa(t0)) ) {
if( !(val._type instanceof TypeFloat tval && t instanceof TypeFloat t0 && !tval.isa(t0)) )
return val;
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/ToIntegerNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/ToIntegerNode.java
new file mode 100644
index 00000000..df740202
--- /dev/null
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/ToIntegerNode.java
@@ -0,0 +1,23 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeFloat;
+import com.seaofnodes.simple.type.TypeInteger;
+
+// Truncate toward zero, saturating overflow; NaN converts to zero.
+public class ToIntegerNode extends Node {
+ public ToIntegerNode(Node lhs) { super(null,lhs); }
+ @Override public String label() { return "ToInteger"; }
+ @Override protected String format() { return "(int)%1"; }
+
+ @Override public Type compute() {
+ Type t = in(1)._type;
+ if( t.isHigh() ) return TypeInteger.TOP;
+ if( t instanceof TypeFloat tf && tf.isConstant() )
+ return TypeInteger.constant((long)tf.value());
+ return TypeInteger.BOT;
+ }
+
+ @Override public Node idealize() { return null; }
+ @Override Node copy(Node lhs, Node rhs) { return new ToIntegerNode(lhs); }
+}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java
index fc5b37d9..130ac8ea 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java
@@ -123,8 +123,8 @@ struct N { !N? !next; int i; };
n.i = 3.14;
return n.i;
""");
- try { code.parse().opto(); fail(); }
- catch( Exception e ) { assertEquals("Type 3.14 is not of declared type int",e.getMessage()); }
+ code.parse().opto();
+ assertEquals("3",Eval2.eval(code,0));
}
@Test
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java
index 7b065411..7eed49fb 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter14FloatTest.java
@@ -8,6 +8,25 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class Chapter14FloatTest {
+ @Test public void testNumericAssignments() {
+ String[][] tests = {
+ {"int rounded=17.3/2.5; return rounded;", "6", "6"},
+ {"int rounded=arg/2.5; return rounded;", "6", "-6"},
+ {"int rounded=0; rounded=arg/2.5; return rounded;", "6", "-6"},
+ {"i8 rounded=arg*16.0; return rounded;", "16", "-16"},
+ {"int rounded=1e100; return rounded;", "9223372036854775807", "9223372036854775807"},
+ {"int rounded=-1e100; return rounded;", "-9223372036854775808", "-9223372036854775808"},
+ {"int rounded=1e300*1e300-1e300*1e300; return rounded;", "0", "0"},
+ {"f32 rounded=arg+16777216.0; return rounded;", "1.6777232E7", "1.6777199E7"}
+ };
+ for( String[] test : tests ) {
+ var code = new CodeGen(test[0]).parse().opto().typeCheck();
+ assertEquals(test[0],test[1],Eval2.eval(code,17));
+ assertEquals(test[0],test[2],Eval2.eval(code,-17));
+ }
+ }
+
+
@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.
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter17bTest.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter17bTest.java
index e64d5b27..93b64747 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter17bTest.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter17bTest.java
@@ -124,8 +124,8 @@ return s.x;
@Test public void testInt10() {
CodeGen code = new CodeGen("flt x = 1.5; x++; int i = x; return i;");
- try { code.parse().opto(); fail(); }
- catch( Exception e ) { assertEquals("Type 2.5f is not of declared type int",e.getMessage()); }
+ code.parse().opto();
+ assertEquals("2",Eval2.eval(code,0));
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java
index 0758fbf5..22eb91b9 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java
@@ -14,11 +14,8 @@ public class Chapter18Test {
CodeGen zero = new CodeGen("var a = new "+type+"[arg]; return a[arg-1];")
.parse().opto().typeCheck();
assertEquals("0.0",Eval2.eval(zero,3));
- // Eval2 does not execute RoundF32 yet; check nonconstant narrowing here.
new CodeGen(type+" x = arg; var a = new "+type+"[arg]; a[0] = x; return a[0];")
.parse().opto().typeCheck();
- }
- for( String type : new String[]{"flt","f64"} ) {
CodeGen loop = new CodeGen("var a = new "+type+"[arg]; "+
"for(int i=0; i<arg; i++) a[i] += i+0.25; return a[0]+a[arg-1];")
.parse().opto().typeCheck();
@@ -28,7 +25,7 @@ public class Chapter18Test {
CodeGen rounded = new CodeGen("var a = new "+type+"[arg]; "+
"for(int i=0; i<arg; i++) a[i] = 16777216.0+i; return a[arg-1];")
.parse().opto().typeCheck();
- assertEquals("1.6777217E7",Eval2.eval(rounded,2));
+ assertEquals(type.equals("f32") ? "1.6777216E7" : "1.6777217E7",Eval2.eval(rounded,2));
}
CodeGen rounded = new CodeGen("var a = new f32[arg]; "+
"for(int i=0; i<arg; i++) a[i] = 16777217.0; return a[arg-1];")
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Eval2.java b/chapter18/src/test/java/com/seaofnodes/simple/Eval2.java
index c517ce6e..3f337e86 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Eval2.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Eval2.java
@@ -279,6 +279,8 @@ public abstract class Eval2 {
case StoreNode st -> store(st);
case SubNode sub -> x(sub.in(1)) - x(sub.in(2));
case SubFNode sbf -> d(sbf.in(1)) - d(sbf.in(2));
+ case RoundF32Node rnd -> (double)(float)d(rnd.in(1));
+ case ToIntegerNode cvt -> (long)d(cvt.in(1));
case ToFloatNode toflt-> (double)x(toflt.in(1));
case XorNode xor -> x(xor.in(1)) ^ x(xor.in(2));
default -> throw Utils.TODO();
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java b/chapter18/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
index 1e2a08a7..7dbc551e 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
@@ -296,6 +296,8 @@ public class Evaluator {
case OrNode or -> vall(or .in(1)) | vall(or .in(2));
case XorNode xor -> vall(xor.in(1)) ^ vall(xor.in(2));
case CastNode cast -> val(cast.in(1));
+ case RoundF32Node rnd -> (double)(float)vald(rnd.in(1));
+ case ToIntegerNode cvt -> (long)vald(cvt.in(1));
case ToFloatNode cast -> (double)vall(cast.in(1));
case LoadNode load -> load(load);
case StoreNode store -> store(store);