summaryrefslogtreecommitdiff
path: root/chapter12
diff options
context:
space:
mode:
Diffstat (limited to 'chapter12')
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/Parser.java13
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/BoolNode.java21
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/Node.java1
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/NotNode.java4
-rw-r--r--chapter12/src/test/java/com/seaofnodes/simple/Chapter12Test.java16
5 files changed, 28 insertions, 27 deletions
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/Parser.java b/chapter12/src/main/java/com/seaofnodes/simple/Parser.java
index cc1fb0df..8d26c975 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/Parser.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/Parser.java
@@ -178,9 +178,13 @@ public class Parser {
* </pre>
*/
private Field parseField(String sname) {
- if( matchx("int") ) // Currently only parsing "int" type fields
- return require(Field.make(requireId().intern(),TypeInteger.BOT),";");
- throw errorSyntax("A field type is expected, only type 'int' is supported at present");
+ int x=0;
+ if( matchx("int") ) x=1;
+ else if( matchx("flt") ) x=2;
+ if( x>0 )
+ return require(Field.make(requireId().intern(),x==1 ? TypeInteger.BOT : TypeFloat.BOT),";");
+ String tok = _lexer.getAnyNextToken();
+ throw errorSyntax("Requires a field type, found '"+tok+"'");
}
/**
@@ -430,7 +434,7 @@ public class Parser {
_scope.update(name,expr);
}
if( expr._type instanceof TypeInteger && t instanceof TypeFloat )
- expr = expr.widen();
+ expr = new ToFloatNode(expr).peephole();
if( !expr._type.isa(t) )
throw error("Type " + expr._type.str() + " is not of declared type " + t.str());
return expr;
@@ -634,6 +638,7 @@ public class Parser {
throw error("Expected struct reference but got " + expr._type.str());
String name = requireId().intern();
+ if( expr._type == TypeMemPtr.TOP ) throw error("Accessing field '" + name + "' from null");
int idx = ptr._obj==null ? -1 : ptr._obj.find(name);
if( idx == -1 ) throw error("Accessing unknown field '" + name + "' from '" + ptr.str() + "'");
int alias = START._aliasStarts.get(ptr._obj._name)+idx;
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/BoolNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/BoolNode.java
index b9faeb32..48faefd2 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/BoolNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/BoolNode.java
@@ -37,12 +37,17 @@ abstract public class BoolNode extends Node {
if (i0.isConstant() && i1.isConstant())
return TypeInteger.constant(doOp(i0.value(), i1.value()) ? 1 : 0);
}
- return in(1)._type.meet(in(2)._type);
+ if( in(1)._type instanceof TypeFloat i0 &&
+ in(2)._type instanceof TypeFloat i1 ) {
+ if (i0.isConstant() && i1.isConstant())
+ return TypeInteger.constant(doOp(i0.value(), i1.value()) ? 1 : 0);
+ }
+ return TypeInteger.BOT;
}
boolean doOp(long lhs, long rhs) { throw Utils.TODO(); }
boolean doOp(double lhs, double rhs) { throw Utils.TODO(); }
- BoolNode copyF(Node lhs, Node rhs) { throw Utils.TODO(); }
+ Node copyF(Node lhs, Node rhs) { throw Utils.TODO(); }
@Override
public Node idealize() {
@@ -72,12 +77,12 @@ abstract public class BoolNode extends Node {
return null;
}
- public static class EQ extends BoolNode { public EQ(Node lhs, Node rhs) { super(lhs,rhs); } String op() { return "=="; } boolean doOp(long lhs, long rhs) { return lhs == rhs; } Node copy(Node lhs, Node rhs) { return new EQ(lhs,rhs); } BoolNode copyF() { return new EQF(null,null); } }
- public static class LT extends BoolNode { public LT(Node lhs, Node rhs) { super(lhs,rhs); } String op() { return "<" ; } boolean doOp(long lhs, long rhs) { return lhs < rhs; } Node copy(Node lhs, Node rhs) { return new LT(lhs,rhs); } BoolNode copyF() { return new LTF(null,null); }}
- public static class LE extends BoolNode { public LE(Node lhs, Node rhs) { super(lhs,rhs); } String op() { return "<="; } boolean doOp(long lhs, long rhs) { return lhs <= rhs; } Node copy(Node lhs, Node rhs) { return new LE(lhs,rhs); } BoolNode copyF() { return new LEF(null,null); } }
+ public static class EQ extends BoolNode { public EQ(Node lhs, Node rhs) { super(lhs,rhs); } String op() { return "=="; } boolean doOp(long lhs, long rhs) { return lhs == rhs; } Node copy(Node lhs, Node rhs) { return new EQ(lhs,rhs); } Node copyF() { return new EQF(null,null); } }
+ public static class LT extends BoolNode { public LT(Node lhs, Node rhs) { super(lhs,rhs); } String op() { return "<" ; } boolean doOp(long lhs, long rhs) { return lhs < rhs; } Node copy(Node lhs, Node rhs) { return new LT(lhs,rhs); } Node copyF() { return new LTF(null,null); }}
+ public static class LE extends BoolNode { public LE(Node lhs, Node rhs) { super(lhs,rhs); } String op() { return "<="; } boolean doOp(long lhs, long rhs) { return lhs <= rhs; } Node copy(Node lhs, Node rhs) { return new LE(lhs,rhs); } Node copyF() { return new LEF(null,null); } }
- public static class EQF extends EQ { public EQF(Node lhs, Node rhs) { super(lhs,rhs); } boolean doOp(double lhs, double rhs) { return lhs == rhs; } }
- public static class LTF extends LT { public LTF(Node lhs, Node rhs) { super(lhs,rhs); } boolean doOp(double lhs, double rhs) { return lhs < rhs; } }
- public static class LEF extends LE { public LEF(Node lhs, Node rhs) { super(lhs,rhs); } boolean doOp(double lhs, double rhs) { return lhs <= rhs; } }
+ public static class EQF extends EQ { public EQF(Node lhs, Node rhs) { super(lhs,rhs); } boolean doOp(double lhs, double rhs) { return lhs == rhs; } Node copyF() { return null; } }
+ public static class LTF extends LT { public LTF(Node lhs, Node rhs) { super(lhs,rhs); } boolean doOp(double lhs, double rhs) { return lhs < rhs; } Node copyF() { return null; } }
+ public static class LEF extends LE { public LEF(Node lhs, Node rhs) { super(lhs,rhs); } boolean doOp(double lhs, double rhs) { return lhs <= rhs; } Node copyF() { return null; } }
}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/Node.java b/chapter12/src/main/java/com/seaofnodes/simple/node/Node.java
index 03b3d5e0..52282737 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/Node.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/Node.java
@@ -236,6 +236,7 @@ public abstract class Node implements OutNode {
*/
public void kill( ) {
unlock();
+ moveDepsToWorklist();
assert isUnused(); // Has no uses, so it is dead
_type=null; // Flag as dead
while( nIns()>0 ) { // Set all inputs to null, recursively killing unused Nodes
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/NotNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/NotNode.java
index 05e44eb0..3cf69520 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/NotNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/NotNode.java
@@ -21,8 +21,8 @@ public class NotNode extends Node {
public Type compute() {
Type t0 = in(1)._type;
switch( t0 ) {
- case TypeInteger i0:
- return i0.isConstant() ? TypeInteger.constant(i0.value()==0 ? 1 : 0) : i0;
+ case TypeInteger i0: return i0.isConstant() ? TypeInteger.constant(i0.value()==0 ? 1 : 0) : i0;
+ case TypeFloat i0: return i0.isConstant() ? TypeInteger.constant(i0.value()==0 ? 1 : 0) : i0;
case TypeMemPtr p0:
// top->top, bot->bot, null->1, *void->0, not-null ptr->0, ptr/nil->bot
// If input in null then true
diff --git a/chapter12/src/test/java/com/seaofnodes/simple/Chapter12Test.java b/chapter12/src/test/java/com/seaofnodes/simple/Chapter12Test.java
index 2eeccba9..92ed9031 100644
--- a/chapter12/src/test/java/com/seaofnodes/simple/Chapter12Test.java
+++ b/chapter12/src/test/java/com/seaofnodes/simple/Chapter12Test.java
@@ -13,21 +13,11 @@ public class Chapter12Test {
public void testJig() {
Parser parser = new Parser(
"""
-struct s0 {
- int v0;
-}
-s0? fIflMOdp;
-while(1) {
- if(fIflMOdp){
- fIflMOdp.v0=-fIflMOdp.v0;
- continue;
- }
- break;
-}
+return 3.14
""");
StopNode stop = parser.parse(false).iterate(false);
- assertEquals("return 0;", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 0));
+ assertEquals("return 3.14;", stop.toString());
+ assertEquals(3.14, Evaluator.evaluate(stop, 0));
}
@Test