summaryrefslogtreecommitdiff
path: root/chapter13/src
diff options
context:
space:
mode:
Diffstat (limited to 'chapter13/src')
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/Parser.java48
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/node/AddFNode.java37
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/node/AddNode.java1
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/node/BoolNode.java36
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/node/DivFNode.java39
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/node/DivNode.java1
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/node/MinusFNode.java30
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/node/MinusNode.java1
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/node/MulFNode.java44
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/node/MulNode.java1
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/node/Node.java21
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/node/NotNode.java6
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/node/SubFNode.java37
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/node/SubNode.java1
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/node/ToFloatNode.java25
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java2
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/type/Type.java29
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/type/TypeFloat.java101
-rw-r--r--chapter13/src/main/java/com/seaofnodes/simple/type/TypeStruct.java16
-rw-r--r--chapter13/src/test/java/com/seaofnodes/simple/Chapter10bTest.java64
-rw-r--r--chapter13/src/test/java/com/seaofnodes/simple/Chapter11Test.java578
-rw-r--r--chapter13/src/test/java/com/seaofnodes/simple/Chapter12Test.java196
-rw-r--r--chapter13/src/test/java/com/seaofnodes/simple/Chapter13Test.java553
-rw-r--r--chapter13/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java24
-rw-r--r--chapter13/src/test/java/com/seaofnodes/simple/fuzzer/ScriptGenerator.java26
25 files changed, 713 insertions, 1204 deletions
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/Parser.java b/chapter13/src/main/java/com/seaofnodes/simple/Parser.java
index cc668f18..32a85173 100644
--- a/chapter13/src/main/java/com/seaofnodes/simple/Parser.java
+++ b/chapter13/src/main/java/com/seaofnodes/simple/Parser.java
@@ -68,7 +68,6 @@ public class Parser {
add("continue");
add("else");
add("false");
- add("flt");
add("if");
add("int");
add("new");
@@ -98,7 +97,6 @@ public class Parser {
IterPeeps.reset();
TYPES.clear();
TYPES.put("int",TypeInteger.BOT);
- TYPES.put("flt",TypeFloat .BOT);
SCHEDULED = false;
_lexer = new Lexer(source);
_scope = new ScopeNode();
@@ -437,9 +435,6 @@ public class Parser {
throw error("Undefined name '" + name + "'");
t = _scope.lookupDeclaredType(name);
}
- // Auto-widen int to float
- if( expr._type instanceof TypeInteger && t instanceof TypeFloat )
- expr = new ToFloatNode(expr).peephole();
// Auto-deepen forward ref types
Type e = expr._type;
if( e instanceof TypeMemPtr tmp && tmp._obj._fields==null )
@@ -506,7 +501,7 @@ public class Parser {
else break;
// Peepholes can fire, but lhs is already "hooked", kept alive
lhs.setDef(idx,parseAddition());
- lhs = lhs.widen().peephole();
+ lhs = lhs.peephole();
if( negate ) // Extra negate for !=
lhs = new NotNode(lhs).peephole();
}
@@ -529,7 +524,7 @@ public class Parser {
else if( match("-") ) lhs = new SubNode(lhs,null);
else break;
lhs.setDef(2,parseMultiplication());
- lhs = lhs.widen().peephole();
+ lhs = lhs.peephole();
}
return lhs;
}
@@ -550,7 +545,7 @@ public class Parser {
else if( match("/") ) lhs = new DivNode(lhs,null);
else break;
lhs.setDef(2,parseUnary());
- lhs = lhs.widen().peephole();
+ lhs = lhs.peephole();
}
return lhs;
}
@@ -564,7 +559,7 @@ public class Parser {
* @return a unary expression {@link Node}, never {@code null}
*/
private Node parseUnary() {
- if (match("-")) return new MinusNode(parseUnary()).widen().peephole();
+ if (match("-")) return new MinusNode(parseUnary()).peephole();
if (match("!")) return new NotNode(parseUnary()).peephole();
return parsePostfix(parsePrimary());
}
@@ -663,7 +658,6 @@ public class Parser {
*
* <pre>
* integerLiteral: [1-9][0-9]* | [0]
- * floatLiteral: [digits].[digits]?[e [digits]]?
* </pre>
*/
private ConstantNode parseLiteral() {
@@ -821,37 +815,19 @@ public class Parser {
return String.valueOf(peek());
}
-
+ boolean isNumber() {return isNumber(peek());}
boolean isNumber(char ch) {return Character.isDigit(ch);}
- // Return a constant Type, either TypeInteger or TypeFloat
private Type parseNumber() {
- int old = _position;
- int len = isLongOrDouble();
- if( len > 0 ) {
- if( len > 1 && _input[old]=='0' )
- throw error("Syntax error: integer values cannot start with '0'");
- return TypeInteger.constant(Long.parseLong(new String(_input,old,len)));
- }
- return TypeFloat.constant(Double.parseDouble(new String(_input,old,-len)));
+ String snum = parseNumberString();
+ if (snum.length() > 1 && snum.charAt(0) == '0')
+ throw error("Syntax error: integer values cannot start with '0'");
+ return TypeInteger.constant(Long.parseLong(snum));
}
private String parseNumberString() {
- int old = _position;
- int len = Math.abs(isLongOrDouble());
- _position += len;
- return new String(_input,old,len);
- }
-
- // Return +len that ends a long
- // Return -len that ends a double
- private int isLongOrDouble() {
- int old = _position;
- char c;
- while( Character.isDigit(c=nextChar()) ) ;
- if( !(c=='e' || c=='.') )
- return --_position - old;
- while( Character.isDigit(c=nextChar()) || c=='e' || c=='.' ) ;
- return -(--_position - old);
+ int start = _position;
+ while (isNumber(nextChar())) ;
+ return new String(_input, start, --_position - start);
}
// First letter of an identifier
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/node/AddFNode.java b/chapter13/src/main/java/com/seaofnodes/simple/node/AddFNode.java
deleted file mode 100644
index fd713156..00000000
--- a/chapter13/src/main/java/com/seaofnodes/simple/node/AddFNode.java
+++ /dev/null
@@ -1,37 +0,0 @@
-package com.seaofnodes.simple.node;
-
-
-import com.seaofnodes.simple.type.Type;
-import com.seaofnodes.simple.type.TypeFloat;
-
-
-public class AddFNode extends Node {
- public AddFNode(Node lhs, Node rhs) { super(null, lhs, rhs); }
-
- @Override public String label() { return "AddF"; }
-
- @Override protected String format() { return "(%1+%2)"; }
-
- @Override
- public Type compute() {
- if (in(1)._type instanceof TypeFloat i0 &&
- in(2)._type instanceof TypeFloat i1) {
- if (i0.isConstant() && i1.isConstant())
- return TypeFloat.constant(i0.value()+i1.value());
- }
- return in(1)._type.meet(in(2)._type);
- }
-
- @Override
- public Node idealize() {
- Node lhs = in(1);
- Type t2 = in(2)._type;
-
- // Add of 0.
- if ( t2.isConstant() && t2 instanceof TypeFloat i && i.value()==0 )
- return lhs;
-
- return null;
- }
- @Override Node copy(Node lhs, Node rhs) { return new AddFNode(lhs,rhs); }
-}
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/node/AddNode.java b/chapter13/src/main/java/com/seaofnodes/simple/node/AddNode.java
index 94fee330..eb7e2793 100644
--- a/chapter13/src/main/java/com/seaofnodes/simple/node/AddNode.java
+++ b/chapter13/src/main/java/com/seaofnodes/simple/node/AddNode.java
@@ -169,7 +169,6 @@ public class AddNode extends Node {
}
@Override Node copy(Node lhs, Node rhs) { return new AddNode(lhs,rhs); }
- @Override Node copyF() { return new AddFNode(null,null); }
@Override public String err() {
if( !(in(1)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(1)._type;
if( !(in(2)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(2)._type;
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/node/BoolNode.java b/chapter13/src/main/java/com/seaofnodes/simple/node/BoolNode.java
index e515a5f1..63452dba 100644
--- a/chapter13/src/main/java/com/seaofnodes/simple/node/BoolNode.java
+++ b/chapter13/src/main/java/com/seaofnodes/simple/node/BoolNode.java
@@ -2,11 +2,8 @@ package com.seaofnodes.simple.node;
import com.seaofnodes.print.ExprPrinter;
-import com.seaofnodes.simple.Parser;
-import com.seaofnodes.simple.Utils;
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
-import com.seaofnodes.simple.type.TypeFloat;
import com.seaofnodes.simple.type.TypeMemPtr;
@@ -27,21 +24,19 @@ abstract public class BoolNode extends Node {
@Override
public Type compute() {
- if( in(1)._type instanceof TypeInteger i0 &&
- in(2)._type instanceof TypeInteger i1 ) {
- if (i0.isConstant() && i1.isConstant())
- return TypeInteger.constant(doOp(i0.value(), i1.value()) ? 1 : 0);
- }
- if( in(1)._type instanceof TypeFloat i0 &&
- in(2)._type instanceof TypeFloat i1 ) {
+ Type t1 = in(1)._type, t2 = in(2)._type;
+ if( t1==Type.TOP || t1==TypeInteger.TOP ||
+ t2==Type.TOP || t2==TypeInteger.TOP )
+ return TypeInteger.TOP;
+ if( t1 instanceof TypeInteger i0 &&
+ t2 instanceof TypeInteger 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(); }
+ abstract boolean doOp(long lhs, long rhs);
@Override
public Node idealize() {
@@ -53,9 +48,11 @@ abstract public class BoolNode extends Node {
if( this instanceof EQ ) {
if( !(in(2) instanceof ConstantNode) ) {
// con==noncon becomes noncon==con
- if( in(1) instanceof ConstantNode || in(1)._nid > in(2)._nid )
+ if( in(1) instanceof ConstantNode )
+ return new EQ(in(2),in(1));
// Equals sorts by NID otherwise: non.high == non.low becomes non.low == non.high
- return in(1)._type instanceof TypeFloat ? new EQF(in(2),in(1)) : new EQ(in(2),in(1));
+ else if( in(1)._nid > in(2)._nid )
+ return new EQ(in(2),in(1));
}
// Equals X==0 becomes a !X
if( (in(2)._type == TypeInteger.ZERO || in(2)._type == TypeMemPtr.NULLPTR) )
@@ -71,12 +68,7 @@ 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); } 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; } 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; } }
-
+ 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); } }
+ 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); } }
+ 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); } }
}
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/node/DivFNode.java b/chapter13/src/main/java/com/seaofnodes/simple/node/DivFNode.java
deleted file mode 100644
index a3f4318d..00000000
--- a/chapter13/src/main/java/com/seaofnodes/simple/node/DivFNode.java
+++ /dev/null
@@ -1,39 +0,0 @@
-package com.seaofnodes.simple.node;
-
-
-import com.seaofnodes.simple.type.Type;
-import com.seaofnodes.simple.type.TypeFloat;
-
-
-public class DivFNode extends Node {
- public DivFNode(Node lhs, Node rhs) { super(null, lhs, rhs); }
-
- @Override public String label() { return "DivF"; }
-
- @Override protected String format() { return "(%1/%2)"; }
-
- @Override
- public Type compute() {
- if (in(1)._type instanceof TypeFloat i0 &&
- in(2)._type instanceof TypeFloat i1) {
- if (i0.isConstant() && i1.isConstant())
- return TypeFloat.constant(i0.value()/i1.value());
- }
- return in(1)._type.meet(in(2)._type);
- }
-
- @Override
- public Node idealize() {
- Node lhs = in(1);
- Node rhs = in(2);
- Type t1 = lhs._type;
- Type t2 = rhs._type;
-
- // Div of 1.
- if ( t2.isConstant() && t2 instanceof TypeFloat i && i.value()==1 )
- return lhs;
-
- return null;
- }
- @Override Node copy(Node lhs, Node rhs) { return new DivFNode(lhs,rhs); }
-}
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/node/DivNode.java b/chapter13/src/main/java/com/seaofnodes/simple/node/DivNode.java
index cd52d117..d7244ace 100644
--- a/chapter13/src/main/java/com/seaofnodes/simple/node/DivNode.java
+++ b/chapter13/src/main/java/com/seaofnodes/simple/node/DivNode.java
@@ -37,5 +37,4 @@ public class DivNode extends Node {
}
@Override Node copy(Node lhs, Node rhs) { return new DivNode(lhs,rhs); }
- @Override Node copyF() { return new DivFNode(null,null); }
}
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/node/MinusFNode.java b/chapter13/src/main/java/com/seaofnodes/simple/node/MinusFNode.java
deleted file mode 100644
index 1b45c7c5..00000000
--- a/chapter13/src/main/java/com/seaofnodes/simple/node/MinusFNode.java
+++ /dev/null
@@ -1,30 +0,0 @@
-package com.seaofnodes.simple.node;
-
-
-import com.seaofnodes.simple.type.Type;
-import com.seaofnodes.simple.type.TypeFloat;
-
-
-public class MinusFNode extends Node {
- public MinusFNode(Node in) { super(null, in); }
-
- @Override public String label() { return "MinusF"; }
-
- @Override protected String format() { return "(-%1)"; }
-
- @Override
- public Type compute() {
- if (in(1)._type instanceof TypeFloat i0)
- return i0.isConstant() ? TypeFloat.constant(-i0.value()) : i0;
- return TypeFloat.BOT;
- }
-
- @Override
- public Node idealize() {
- // -(-x) is x
- if( in(1) instanceof MinusFNode minus )
- return minus.in(1);
-
- return null;
- }
-}
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/node/MinusNode.java b/chapter13/src/main/java/com/seaofnodes/simple/node/MinusNode.java
index ae450b22..5b65b3fb 100644
--- a/chapter13/src/main/java/com/seaofnodes/simple/node/MinusNode.java
+++ b/chapter13/src/main/java/com/seaofnodes/simple/node/MinusNode.java
@@ -27,5 +27,4 @@ public class MinusNode extends Node {
return null;
}
- @Override Node copyF() { return new MinusFNode(null); }
}
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/node/MulFNode.java b/chapter13/src/main/java/com/seaofnodes/simple/node/MulFNode.java
deleted file mode 100644
index 6a57f3c5..00000000
--- a/chapter13/src/main/java/com/seaofnodes/simple/node/MulFNode.java
+++ /dev/null
@@ -1,44 +0,0 @@
-package com.seaofnodes.simple.node;
-
-
-import com.seaofnodes.simple.type.Type;
-import com.seaofnodes.simple.type.TypeFloat;
-
-
-public class MulFNode extends Node {
- public MulFNode(Node lhs, Node rhs) { super(null, lhs, rhs); }
-
- @Override public String label() { return "MulF"; }
-
- @Override protected String format() { return "(%1*%2)"; }
-
- @Override
- public Type compute() {
- if (in(1)._type instanceof TypeFloat i0 &&
- in(2)._type instanceof TypeFloat i1) {
- if (i0.isConstant() && i1.isConstant())
- return TypeFloat.constant(i0.value()*i1.value());
- }
- return in(1)._type.meet(in(2)._type);
- }
-
- @Override
- public Node idealize() {
- Node lhs = in(1);
- Node rhs = in(2);
- Type t1 = lhs._type;
- Type t2 = rhs._type;
-
- // Mul of 1. We do not check for (1*x) because this will already
- // canonicalize to (x*1)
- if ( t2.isConstant() && t2 instanceof TypeFloat i && i.value()==1 )
- return lhs;
-
- // Move constants to RHS: con*arg becomes arg*con
- if ( t1.isConstant() && !t2.isConstant() )
- return swap12();
-
- return null;
- }
- @Override Node copy(Node lhs, Node rhs) { return new MulFNode(lhs,rhs); }
-}
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/node/MulNode.java b/chapter13/src/main/java/com/seaofnodes/simple/node/MulNode.java
index 7fd52399..b0359a52 100644
--- a/chapter13/src/main/java/com/seaofnodes/simple/node/MulNode.java
+++ b/chapter13/src/main/java/com/seaofnodes/simple/node/MulNode.java
@@ -51,7 +51,6 @@ public class MulNode extends Node {
return null;
}
@Override Node copy(Node lhs, Node rhs) { return new MulNode(lhs,rhs); }
- @Override Node copyF() { return new MulFNode(null,null); }
@Override public String err() {
if( !(in(1)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(1)._type;
if( !(in(2)._type instanceof TypeInteger) ) return "Cannot '"+label()+"' " + in(2)._type;
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/node/Node.java b/chapter13/src/main/java/com/seaofnodes/simple/node/Node.java
index 7448e05e..2c8596fa 100644
--- a/chapter13/src/main/java/com/seaofnodes/simple/node/Node.java
+++ b/chapter13/src/main/java/com/seaofnodes/simple/node/Node.java
@@ -8,7 +8,6 @@ import com.seaofnodes.simple.IRPrinter;
import com.seaofnodes.simple.IterPeeps;
import com.seaofnodes.simple.Utils;
import com.seaofnodes.simple.type.Type;
-import com.seaofnodes.simple.type.TypeFloat;
import java.util.*;
import java.util.function.Function;
@@ -497,26 +496,6 @@ public abstract class Node extends BaseNode<Node> implements OutNode, Cloneable
/** Return block start from a isCFG() */
public Node getBlockStart() { return null; }
- // Semantic change to the graph (so NOT a peephole), used by the Parser.
- // If any input is a float, flip to a float-flavored opcode and widen any
- // non-float input.
- public final Node widen() {
- if( !hasFloatInput() ) return this;
- Node flt = copyF();
- if( flt==null ) return this;
- for( int i=1; i<nIns(); i++ )
- flt.setDef(i, in(i)._type instanceof TypeFloat ? in(i) : new ToFloatNode(in(i)).peephole());
- kill();
- return flt;
- }
- private boolean hasFloatInput() {
- for( int i=1; i<nIns(); i++ )
- if( in(i)._type instanceof TypeFloat )
- return true;
- return false;
- }
- Node copyF() { return null; }
-
// ------------------------------------------------------------------------
// Peephole utilities
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/node/NotNode.java b/chapter13/src/main/java/com/seaofnodes/simple/node/NotNode.java
index 4539f982..5e556390 100644
--- a/chapter13/src/main/java/com/seaofnodes/simple/node/NotNode.java
+++ b/chapter13/src/main/java/com/seaofnodes/simple/node/NotNode.java
@@ -15,10 +15,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 TypeFloat i0:
- if( i0==TypeFloat.TOP ) return TypeInteger.TOP;
- return i0.isConstant() ? TypeInteger.constant(i0.value()==0 ? 1 : 0) : TypeInteger.BOT;
+ case TypeInteger 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/chapter13/src/main/java/com/seaofnodes/simple/node/SubFNode.java b/chapter13/src/main/java/com/seaofnodes/simple/node/SubFNode.java
deleted file mode 100644
index 7c1a5dde..00000000
--- a/chapter13/src/main/java/com/seaofnodes/simple/node/SubFNode.java
+++ /dev/null
@@ -1,37 +0,0 @@
-package com.seaofnodes.simple.node;
-
-
-import com.seaofnodes.simple.type.Type;
-import com.seaofnodes.simple.type.TypeFloat;
-
-
-public class SubFNode extends Node {
- public SubFNode(Node lhs, Node rhs) { super(null, lhs, rhs); }
-
- @Override public String label() { return "SubF"; }
-
- @Override protected String format() { return "(%1-%2)"; }
-
- @Override
- public Type compute() {
- if (in(1)._type instanceof TypeFloat i0 &&
- in(2)._type instanceof TypeFloat i1) {
- if (i0.isConstant() && i1.isConstant())
- return TypeFloat.constant(i0.value()-i1.value());
- }
- return in(1)._type.meet(in(2)._type);
- }
-
- @Override
- public Node idealize() {
- Node lhs = in(1);
- Type t2 = in(2)._type;
-
- // Sub of 1.
- if ( t2.isConstant() && t2 instanceof TypeFloat i && i.value()==0 )
- return lhs;
-
- return null;
- }
- @Override Node copy(Node lhs, Node rhs) { return new SubFNode(lhs,rhs); }
-}
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/node/SubNode.java b/chapter13/src/main/java/com/seaofnodes/simple/node/SubNode.java
index f36f2af5..1589abe7 100644
--- a/chapter13/src/main/java/com/seaofnodes/simple/node/SubNode.java
+++ b/chapter13/src/main/java/com/seaofnodes/simple/node/SubNode.java
@@ -57,5 +57,4 @@ public class SubNode extends Node {
}
@Override Node copy(Node lhs, Node rhs) { return new SubNode(lhs,rhs); }
- @Override Node copyF() { return new SubFNode(null,null); }
}
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/node/ToFloatNode.java b/chapter13/src/main/java/com/seaofnodes/simple/node/ToFloatNode.java
deleted file mode 100644
index 26cf50e1..00000000
--- a/chapter13/src/main/java/com/seaofnodes/simple/node/ToFloatNode.java
+++ /dev/null
@@ -1,25 +0,0 @@
-package com.seaofnodes.simple.node;
-
-
-import com.seaofnodes.simple.type.Type;
-import com.seaofnodes.simple.type.TypeFloat;
-import com.seaofnodes.simple.type.TypeInteger;
-
-
-public class ToFloatNode extends Node {
- public ToFloatNode(Node lhs) { super(null, lhs); }
-
- @Override public String label() { return "ToFloat"; }
-
- @Override protected String format() { return "(flt)%1"; }
-
- @Override
- public Type compute() {
- if (in(1)._type instanceof TypeInteger i0 && i0.isConstant() )
- return TypeFloat.constant(i0.value());
- return TypeFloat.BOT;
- }
-
- @Override public Node idealize() { return null; }
- @Override Node copy(Node lhs, Node rhs) { return new ToFloatNode(lhs); }
-}
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java b/chapter13/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
index 9d996c03..e65963db 100644
--- a/chapter13/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
+++ b/chapter13/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
@@ -6,7 +6,7 @@ import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.node.Node;
import com.seaofnodes.simple.type.TypeMem;
-/** Chapter 13's view of the IR; browser, transport and layout are shared. */
+/** Chapter 12's view of the IR; browser, transport and layout are shared. */
public class SimpleGraphAdapter extends GraphAdapter<Node> {
@Override protected String[] edgeNames(Node n) {
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/type/Type.java b/chapter13/src/main/java/com/seaofnodes/simple/type/Type.java
index 1f211430..93e27c76 100644
--- a/chapter13/src/main/java/com/seaofnodes/simple/type/Type.java
+++ b/chapter13/src/main/java/com/seaofnodes/simple/type/Type.java
@@ -33,12 +33,11 @@ public class Type {
static final byte TXCTRL = 3; // Ctrl flow top (mini-lattice: any-xctrl-ctrl-all)
static final byte TSIMPLE = 4; // End of the Simple Types
static final byte TINT = 5; // All Integers; see TypeInteger
- static final byte TFLT = 6; // All Integers; see TypeInteger
- static final byte TTUPLE = 7; // Tuples; finite collections of unrelated Types, kept in parallel
- static final byte TMEM = 8; // All memory (alias 0) or A slice of memory - with specific alias
- static final byte TMEMPTR = 9; // Memory pointer type
- static final byte TSTRUCT =10; // Structs; tuples with named fields
- static final byte TFLD =11; // Fields into struct
+ static final byte TTUPLE = 6; // Tuples; finite collections of unrelated Types, kept in parallel
+ static final byte TMEM = 7; // All memory (alias 0) or A slice of memory - with specific alias
+ static final byte TMEMPTR = 8; // Memory pointer type
+ static final byte TSTRUCT = 9; // Structs; tuples with named fields
+ static final byte TFLD =10; // Fields into struct
public final byte _type;
@@ -82,16 +81,14 @@ public class Type {
// ----------------------------------------------------------
- // Notes on Type interning: At the moment it is not easy to reset the
- // interned types because we hold static references to several types and
- // these are scattered around. This means the INTERN cache will retain all
- // types from every run of the Parser. For this to work correctly types
- // must be rigorous about defining when they are the same. Also types need
- // to be immutable once defined. The rationale for interning is
- // *correctness* with cyclic type definitions. Simple structural recursive
- // checks go exponential with merely sharing, but with cycles they will
- // stack overflow and crash. Intering means we do not need to have sharing
- // checks with every type compare, only during interning.
+ // Notes on Type interning.
+ // At the moment it is not easy to reset the interned types
+ // because we hold static references to several types and these are scattered
+ // around. This means the INTERN cache will retain all types from
+ // every run of the Parser. For this to work correctly types must be
+ // rigorous about defining when they are the same. Also types need to be
+ // immutable once defined.
+ // The rationale for interning is performance.
// Factory method which interns "this"
public <T extends Type> T intern() {
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/type/TypeFloat.java b/chapter13/src/main/java/com/seaofnodes/simple/type/TypeFloat.java
deleted file mode 100644
index 738aebc9..00000000
--- a/chapter13/src/main/java/com/seaofnodes/simple/type/TypeFloat.java
+++ /dev/null
@@ -1,101 +0,0 @@
-package com.seaofnodes.simple.type;
-
-import java.util.ArrayList;
-
-/**
- * Float Type
- */
-public class TypeFloat extends Type {
-
- public final static TypeFloat TOP = make(false, 0);
- public final static TypeFloat BOT = make(false, 1);
- public final static TypeFloat ZERO= make(true , 0);
-
- public final boolean _is_con;
-
- /**
- * The constant value or
- * if not constant then 1=bottom, 0=top.
- */
- public final double _con;
-
- private TypeFloat(boolean is_con, double con) {
- super(TFLT);
- _is_con = is_con;
- _con = con;
- }
- public static TypeFloat make(boolean is_con, double con) {
- return new TypeFloat(is_con,con).intern();
- }
-
- public static TypeFloat constant(double con) { return make(true, con); }
-
- public static void gather(ArrayList<Type> ts) { ts.add(ZERO); ts.add(BOT); }
-
- // FIXME this display format is problematic
- // In visualizer '#' gets prepended if its a constant
- @Override
- public StringBuilder print(StringBuilder sb) {
- if( this==TOP ) return sb.append("FltTop");
- if( this==BOT ) return sb.append("FltBot");
- return sb.append(_con);
- }
-
- @Override public String str() {
- if( this==TOP ) return "~flt";
- if( this==BOT ) return "flt";
- return ""+_con;
- }
-
- /**
- * Display Type name in a format that's good for IR printer
- */
- @Override
- public StringBuilder typeName(StringBuilder sb) {
- if( this==TOP ) return sb.append("FltTop");
- if( this==BOT ) return sb.append("FltBot");
- return sb.append("Int");
- }
-
- @Override
- public boolean isHighOrConst() { return _is_con || _con==0; }
-
- @Override
- public boolean isConstant() { return _is_con; }
-
- public double value() { return _con; }
-
- @Override
- public Type xmeet(Type other) {
- // Invariant from caller: 'this' != 'other' and same class (TypeFloat)
- TypeFloat i = (TypeFloat)other; // Contract
- // BOT wins
- if ( this==BOT ) return this;
- if ( i ==BOT ) return i ;
- // TOP loses
- if ( i ==TOP ) return this;
- if ( this==TOP ) return i ;
- // Since both are constants, and are never equals (contract) unequals
- // constants fall to bottom
- return BOT;
- }
-
- @Override
- public Type dual() {
- if( isConstant() ) return this; // Constants are a self-dual
- return _con==0 ? BOT : TOP;
- }
-
- @Override
- public Type glb() { return BOT; }
-
- @Override public TypeFloat makeInit() { return ZERO; }
-
- @Override
- int hash() { return (int)(Double.hashCode(_con) ^ (_is_con ? 0 : 0x4000)); }
- @Override
- public boolean eq( Type t ) {
- TypeFloat i = (TypeFloat)t; // Contract
- return _con==i._con && _is_con==i._is_con;
- }
-}
diff --git a/chapter13/src/main/java/com/seaofnodes/simple/type/TypeStruct.java b/chapter13/src/main/java/com/seaofnodes/simple/type/TypeStruct.java
index 2599a383..02de7cc8 100644
--- a/chapter13/src/main/java/com/seaofnodes/simple/type/TypeStruct.java
+++ b/chapter13/src/main/java/com/seaofnodes/simple/type/TypeStruct.java
@@ -8,14 +8,12 @@ import java.util.ArrayList;
*/
public class TypeStruct extends Type {
- // A Struct has a name and a set of fields; the fields themselves have
- // names and types. Briefly during parsing its allowed to have a
- // forward-ref to a Struct; in this case the _fields array is null.
- // Its illegal to attempt to load a field from a forward-ref struct.
- //
- // During the normal optimization run, struct types "bottom out" at further
- // struct references, so we don't have to handle e.g. cyclic types. The
- // "bottom out" is again the forward-ref struct.
+ // Immutable shallow references, resolved by name in Parser.TYPES when needed.
+ // A name-only struct has _fields == null, even after its definition is known.
+ // For LLI { LLI? next; int i; }, L0 is name-only and L1.next points to L0;
+ // TYPES["LLI"] holds L1. We deliberately stop recursive type expansion here,
+ // keeping interning and lattice operations finite and acyclic. Never patch
+ // L0 into L1: field access looks up the complete declaration by name instead.
public final String _name;
public final Field[] _fields;
@@ -40,7 +38,7 @@ public class TypeStruct extends Type {
private static final TypeStruct S1F = make("S1");
private static final TypeStruct S2F = make("S2");
public static final TypeStruct S1 = make("S1", new Field[]{ Field.make("a", TypeInteger.BOT), Field.make("s2",TypeMemPtr.make(S2F,false)) });
- private static final TypeStruct S2 = make("S2", new Field[]{ Field.make("b", TypeFloat .BOT), Field.make("s1",TypeMemPtr.make(S1F,false)) });
+ private static final TypeStruct S2 = make("S2", new Field[]{ Field.make("b", TypeInteger.BOT), Field.make("s1",TypeMemPtr.make(S1F,false)) });
public static void gather(ArrayList<Type> ts) { ts.add(TEST); ts.add(BOT); ts.add(S1); ts.add(S2); }
public int find(String fname) {
diff --git a/chapter13/src/test/java/com/seaofnodes/simple/Chapter10bTest.java b/chapter13/src/test/java/com/seaofnodes/simple/Chapter10bTest.java
deleted file mode 100644
index 6f094cfb..00000000
--- a/chapter13/src/test/java/com/seaofnodes/simple/Chapter10bTest.java
+++ /dev/null
@@ -1,64 +0,0 @@
-package com.seaofnodes.simple;
-
-import com.seaofnodes.simple.node.*;
-import com.seaofnodes.simple.evaluator.Evaluator;
-import org.junit.Test;
-import static org.junit.Assert.*;
-
-public class Chapter10bTest {
- @Test public void testIndependentField() {
- var stop = new Parser("""
- struct S { int x; int y; }
- S s = new S;
- s.x = 42;
- while (arg > 0) { s.y = s.y + arg; arg = arg - 1; }
- return s.x;
- """).parse().iterate();
- assertEquals("return 42;",stop.toString());
- assertEquals(42L,Evaluator.evaluate(stop,5));
- assertNull(stop.walk(n -> n instanceof LoadNode ? n : null));
- }
-
- @Test public void testParallelMemoryPhis() {
- var stop = new Parser("""
- struct S { int x; int y; int z; }
- S a = new S; S b = new S;
- S p = a; if (arg) p = b;
- while (arg > 0) {
- a.x = p.x + arg;
- a.y = p.y + 2;
- a.z = p.z + 3;
- arg = arg - 1;
- }
- return a;
- """).parse().iterate();
- int[] phis = {0};
- stop.walk(n -> {
- if (n instanceof MemPhiNode) phis[0]++;
- if (n instanceof MemMergeNode m && m.in(1) instanceof BulkMemPhiNode b)
- for(int a=b._aliases.nextSetBit(0); a>=0; a=b._aliases.nextSetBit(a+1))
- assertTrue(a<m.nIns() && m.in(a)!=null);
- assertFalse(n instanceof PhiNode && n.isMem() &&
- !(n instanceof MemPhiNode) && !(n instanceof BulkMemPhiNode));
- return null;
- });
- assertTrue(phis[0]>=3);
- assertArrayEquals(new Object[]{1L,2L,3L},((Evaluator.Obj)Evaluator.evaluate(stop,4)).fields());
- }
-
- @Test public void testLateSliceWorklist() throws ReflectiveOperationException {
- // Seed 97 exposed a store selecting a bulk predecessor without queuing
- // it. Keep both the worklist assertion and the final heap check.
- for (int seed : new int[]{0,97,123,456}) {
- var parser = new Parser(Chapter10Test.NESTED_MEMORY);
- var field = IterPeeps.class.getDeclaredField("WORK");
- field.setAccessible(true);
- Object work = field.get(null);
- field = work.getClass().getDeclaredField("_R");
- field.setAccessible(true);
- ((java.util.Random)field.get(work)).setSeed(seed);
- var stop = parser.parse().iterate();
- assertArrayEquals(new Object[]{9L,25L,17L},((Evaluator.Obj)Evaluator.evaluate(stop,4)).fields());
- }
- }
-}
diff --git a/chapter13/src/test/java/com/seaofnodes/simple/Chapter11Test.java b/chapter13/src/test/java/com/seaofnodes/simple/Chapter11Test.java
index 7aab9752..5f92c9c1 100644
--- a/chapter13/src/test/java/com/seaofnodes/simple/Chapter11Test.java
+++ b/chapter13/src/test/java/com/seaofnodes/simple/Chapter11Test.java
@@ -1,532 +1,64 @@
package com.seaofnodes.simple;
-
+import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.evaluator.Evaluator;
-import com.seaofnodes.simple.node.StopNode;
import org.junit.Test;
-
-
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.*;
public class Chapter11Test {
-
- // A placeholder test used to rapidly rotate through fuzzer produced issues
- @Test
- public void testFuzzer() {
- Parser parser = new Parser(
-"""
-while(-----arg) {
- while(-arg) {
- while(-----arg*---0) {
- int N=--false==false<--arg;
- while(N<-4==N) {
- N=N+N;
- }
- }
- {
- int kzEl_r7EYz=3*----0!=-arg<(37*--97!=0/arg==arg)<=-(--arg!=0);
- while(kzEl_r7EYz<---1) {
- kzEl_r7EYz=kzEl_r7EYz+kzEl_r7EYz*arg>=24;
- if(7) continue;
- kzEl_r7EYz=false;
- if(-kzEl_r7EYz+arg<-3) continue;
- else {
- while(kzEl_r7EYz) continue;
- arg=----3;
- kzEl_r7EYz=-(-----arg*1>-kzEl_r7EYz);
- int v2uF_4ba=9;
- }
- }
- }
- int pjcp7rv=arg;
- {
- int m2ya3=--arg-pjcp7rv;
- while(m2ya3<-----pjcp7rv>=pjcp7rv+--pjcp7rv!=---pjcp7rv!=-arg<---(-m2ya3)>m2ya3<=-----true>14!=--m2ya3) {
- m2ya3=m2ya3+-(--0<=false<pjcp7rv/arg<=-true);
- while((--m2ya3)<--arg<-----arg*false!=arg<pjcp7rv*arg) {
- pjcp7rv=-3+-arg;
- break;
- }
- while(-3) {
- while(67+0-61) m2ya3=m2ya3;
- if(----pjcp7rv==---(---arg)) pjcp7rv=0!=-23;
- else pjcp7rv=----89;
- while(m2ya3!=m2ya3<pjcp7rv) m2ya3=1;
- while(--arg<=pjcp7rv) pjcp7rv=-32<=-m2ya3+62;
- }
- break;
- }
- }
- {
- int arg=-1<-9!=(-arg>=-pjcp7rv>=arg<=arg);
- while(arg<-arg) {
- arg=arg+--arg;
- {
- while((--93)--pjcp7rv*-pjcp7rv+arg) arg=0-(6>=(--(---41/(0))!=--pjcp7rv)>=---2*33);
- pjcp7rv=11;
- int c=pjcp7rv;
- if(--107) break;
- else break;
- }
- }
- }
- pjcp7rv=pjcp7rv;
- int aR0Wkua=-pjcp7rv==pjcp7rv;
- {
- if(-0!=-0) while(-true) break;
- else {
- while(aR0Wkua) pjcp7rv=-0/pjcp7rv;
- pjcp7rv=pjcp7rv;
- }
- }
- }
- if(-arg) {
- {
- int Isdlt0fr91=0!=arg;
- while(Isdlt0fr91<-Isdlt0fr91==-Isdlt0fr91+true) {
- Isdlt0fr91=Isdlt0fr91+-1!=true;
- if(-arg) if(--arg<=Isdlt0fr91) Isdlt0fr91=-Isdlt0fr91;
- int i=---arg<---arg!=--40;
- {
- int jVX43Ln4Dm=-(6)==-Isdlt0fr91/----0;
- while(jVX43Ln4Dm<--(3-3)) {
- jVX43Ln4Dm=jVX43Ln4Dm+-0<25;
- i=24<--(jVX43Ln4Dm);
- }
- }
- i=(arg!=----false>=7);
- int Te=5>-arg;
- break;
- }
- }
- arg=arg!=--true/arg;
- {
- int UX=arg;
- while(UX<-3*arg) {
- UX=UX+arg/-1;
- arg=-3--(-UX--47<arg==--arg==arg)/arg--UX>=-arg;
- if(---12) while(-UX) break;
- break;
- }
- }
- continue;
- }
- if(arg) arg=--0;
- else if(1) continue;
- int DtTlhjYq=19==arg;
- {
- arg=--DtTlhjYq<=(DtTlhjYq/---DtTlhjYq<=-DtTlhjYq);
- if(---arg) continue;
- else {
- int Zsl=40;
- while(Zsl<--Zsl--false>((Zsl))==Zsl!=--DtTlhjYq-DtTlhjYq-DtTlhjYq) {
- Zsl=Zsl+arg;
- }
- }
- if((arg)) if(-DtTlhjYq) {
- }
- else while(((2)==3*arg/DtTlhjYq)<=0==---arg) return false;
- else {
- int lwbF=arg>=0;
- while(lwbF<57) {
- lwbF=lwbF+--(-arg+lwbF)>=-2>=arg!=arg;
- arg=----0+0;
- while(49) break;
- if(true) DtTlhjYq=-----arg;
- DtTlhjYq=0!=-----(0);
- }
- }
- arg=---arg!=DtTlhjYq/--0;
- DtTlhjYq=-DtTlhjYq/(--arg<arg);
- if(32) {
- int q=-DtTlhjYq;
- while(q<arg*(0*q)/0) {
- q=q+DtTlhjYq;
- DtTlhjYq=arg;
- }
- }
- else if(--arg) if(15) break;
- else arg=0==arg<=-55>DtTlhjYq;
- else {
- }
- DtTlhjYq=-0;
- {
- while((0)<-arg) DtTlhjYq=-DtTlhjYq;
- }
- if(30/--(arg)>=0) DtTlhjYq=-0>=arg;
- else {
- int Y2wcIQJ=--DtTlhjYq*---11;
- if(arg==arg) {
- Y2wcIQJ=-----Y2wcIQJ;
- Y2wcIQJ=Y2wcIQJ<7;
- if(DtTlhjYq) arg=arg;
- else break;
- while(-arg!=-31==(false<=-15>=-DtTlhjYq+--6*-----Y2wcIQJ<=---arg!=-Y2wcIQJ<=---0<-arg*--Y2wcIQJ)) DtTlhjYq=arg!=3;
- }
- continue;
- }
- arg=DtTlhjYq;
- arg=--3/(---arg)>=80>=-DtTlhjYq<=DtTlhjYq;
- arg=51+---(-true)!=arg!=----1==-DtTlhjYq;
- if(11<=arg) if((arg)+79<32+(-arg)) continue;
- else if(true==DtTlhjYq) break;
- break;
- }
-}
-if(arg!=62==arg) {
- while(---arg) {
- {
- int bhmDacPM=arg/arg;
- while(bhmDacPM<arg) {
- bhmDacPM=bhmDacPM+80*((-bhmDacPM)/---bhmDacPM>1)<bhmDacPM-2!=7;
- arg=-0;
- arg=bhmDacPM>=-arg+-arg;
- break;
- }
- }
- int arg=true;
- arg=arg<=arg;
- {
- int KlC=30;
- while(KlC<-2) {
- KlC=KlC+-1!=25<=-KlC>KlC>----0;
- int Ij=arg>=-arg<=---10;
- {
- int LAbgurVa2R=-arg!=Ij;
- while(LAbgurVa2R<-true) {
- LAbgurVa2R=LAbgurVa2R+Ij;
- KlC=-LAbgurVa2R;
- Ij=42*(-Ij>true);
- while(Ij) arg=KlC;
- KlC=-----7>-(--KlC<--7)==-LAbgurVa2R;
- }
- }
- int a=Ij<=-KlC;
- if(Ij) {
- int r1W=a!=-false;
- }
- else {
- arg=56>=Ij;
- while(54) break;
- arg=8>=-(a<--Ij)/a>=-a-KlC;
- }
- int Yt_T=KlC;
- arg=KlC;
- }
+ @Test public void testIndependentField() {
+ var stop = new Parser("""
+ struct S { int x; int y; }
+ S s = new S;
+ s.x = 42;
+ while (arg > 0) { s.y = s.y + arg; arg = arg - 1; }
+ return s.x;
+ """).parse().iterate();
+ assertEquals("return 42;",stop.toString());
+ assertEquals(42L,Evaluator.evaluate(stop,5));
+ assertNull(stop.walk(n -> n instanceof LoadNode ? n : null));
+ }
+
+ @Test public void testParallelMemoryPhis() {
+ var stop = new Parser("""
+ struct S { int x; int y; int z; }
+ S a = new S; S b = new S;
+ S p = a; if (arg) p = b;
+ while (arg > 0) {
+ a.x = p.x + arg;
+ a.y = p.y + 2;
+ a.z = p.z + 3;
+ arg = arg - 1;
+ }
+ return a;
+ """).parse().iterate();
+ int[] phis = {0};
+ stop.walk(n -> {
+ if (n instanceof MemPhiNode) phis[0]++;
+ if (n instanceof MemMergeNode m && m.in(1) instanceof BulkMemPhiNode b)
+ for(int a=b._aliases.nextSetBit(0); a>=0; a=b._aliases.nextSetBit(a+1))
+ assertTrue(a<m.nIns() && m.in(a)!=null);
+ assertFalse(n instanceof PhiNode && n.isMem() &&
+ !(n instanceof MemPhiNode) && !(n instanceof BulkMemPhiNode));
+ return null;
+ });
+ assertTrue(phis[0]>=3);
+ assertArrayEquals(new Object[]{1L,2L,3L},((Evaluator.Obj)Evaluator.evaluate(stop,4)).fields());
+ }
+
+ @Test public void testLateSliceWorklist() throws ReflectiveOperationException {
+ // Seed 97 exposed a store selecting a bulk predecessor without queuing
+ // it. Keep both the worklist assertion and the final heap check.
+ for (int seed : new int[]{0,97,123,456}) {
+ var parser = new Parser(Chapter10Test.NESTED_MEMORY);
+ var field = IterPeeps.class.getDeclaredField("WORK");
+ field.setAccessible(true);
+ Object work = field.get(null);
+ field = work.getClass().getDeclaredField("_R");
+ field.setAccessible(true);
+ ((java.util.Random)field.get(work)).setSeed(seed);
+ var stop = parser.parse().iterate();
+ assertArrayEquals(new Object[]{9L,25L,17L},((Evaluator.Obj)Evaluator.evaluate(stop,4)).fields());
}
- if(----false/--arg/12+-----0) if(-9+-arg) while(arg!=(arg)) arg=--1>=-71-arg!=---(--arg*11+102)/--arg;
- else if(----arg) continue;
- if(-arg-arg) {
- arg=-(-arg)*--(-3)<(-7==-arg----arg);
- {
- int arg=arg;
- while(arg<-0>arg==-arg-(-arg>1+11)) {
- arg=arg+6-arg!=--0>---30>=---arg<=arg/(3)+---arg;
- }
- }
- arg=-arg+arg;
- arg=--47+-arg<1>arg*(-arg==-arg--0>3);
- arg=(----arg)!=arg+--59;
- arg=--4+arg==-(arg)-arg;
- }
- else {
- while(--true-----true*-25+-18>arg>=-----(-arg)---13) {
- arg=-0+--true<false;
- continue;
- }
- arg=true<=arg;
- break;
- }
- arg=arg*22<--(0+-arg);
- {
- continue;
- }
- }
- arg=1*---((3<=true)==---2);
- arg=7+-(-1);
- return arg+--true;
-}
-else {
- int o3ypEsewxM=-(---arg);
- int vzXB=(-(---11<=--o3ypEsewxM>=--0))<=-0/-arg;
- int L=-arg;
- int B4mHcrf=---vzXB;
- int oZWGdzZl=-o3ypEsewxM;
- int K6tQqa1=11;
- while(0!=--L) {
- int sdZfTM=vzXB*true+K6tQqa1;
- while(sdZfTM<false) {
- sdZfTM=sdZfTM+arg;
- sdZfTM=oZWGdzZl;
- K6tQqa1=-K6tQqa1;
- arg=---K6tQqa1;
- while((3+-K6tQqa1)<oZWGdzZl) {
- if(-----(-1)) sdZfTM=-false;
- {}
- }
- K6tQqa1=vzXB!=-(B4mHcrf+-7>false);
- sdZfTM=vzXB;
- }
- }
- L=false;
- while(31) B4mHcrf=arg;
- oZWGdzZl=-0<--oZWGdzZl<=--2;
- if(K6tQqa1>=20<=46) {
- if(-1+-L) if(oZWGdzZl) {
- int wmhv=-----0;
- }
- int arg=0;
- o3ypEsewxM=---(oZWGdzZl);
- return -oZWGdzZl/0;
- }
- else if(arg) {
- int yKk5=B4mHcrf>=4>K6tQqa1<-B4mHcrf/o3ypEsewxM;
- return -K6tQqa1;
- }
- else {
- if(---0) {
- {}
- {}
- }
- return o3ypEsewxM;
- }
-}
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("Stop[ return 9; return 0; return 0; return 0; return 0; ]", stop.toString());
- }
-
-
- @Test
- public void testPrimes() {
- Parser parser = new Parser(
-"""
-if( arg < 2 ) return 0;
-int primeCount = 1;
-int prime = 3;
-while( prime <= arg ) {
- int isPrime = 1;
- // Check for even case, so the next loop need only check odds
- if( (prime/2)*2 == prime )
- continue;
- // Check odds up to sqrt of prime
- int j = 3;
- while( j*j <= prime ) {
- if( (prime/j)*j == prime ) {
- isPrime = 0;
- break;
- }
- j = j + 2;
- }
- if( isPrime )
- primeCount = primeCount + 1;
- prime = prime + 2;
-}
-return primeCount;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("Stop[ return 0; return Phi(Loop21,1,Phi(Region88,Phi_primeCount,Phi(Region82,(Phi_primeCount+1),Phi_primeCount))); ]", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 1)); // No primes 1 or below
- assertEquals(1L, Evaluator.evaluate(stop, 2)); // 2
- assertEquals(2L, Evaluator.evaluate(stop, 3)); // 2, 3
- assertEquals(2L, Evaluator.evaluate(stop, 4)); // 2, 3
- assertEquals(3L, Evaluator.evaluate(stop, 5)); // 2, 3, 5
- assertEquals(4L, Evaluator.evaluate(stop, 10)); // 2, 3, 5, 7
- }
-
- @Test
- public void testAntiDeps1() {
- Parser parser = new Parser(
-"""
-struct S { int f; }
-S v=new S;
-v.f = 2;
-int i=new S.f;
-i=v.f;
-if (arg) v.f=1;
-return i;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return .f;", stop.toString());
- }
-
- @Test
- public void testAntiDeps2() {
- Parser parser = new Parser(
-"""
-struct S { int f; }
-S v = new S;
-S t = new S;
-int i = 0;
-if (arg) {
- if (arg+1) v = t;
- i = v.f;
-} else {
- v.f = 2;
-}
-return i;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region37,.f,0);", stop.toString());
- }
-
- @Test
- public void testAntiDeps3() {
- Parser parser = new Parser(
-"""
-struct S { int f; }
-S v0 = new S;
-S? v1;
-if (arg) v1 = new S;
-if (v1) {
- v0.f = v1.f;
-} else {
- v0.f = 2;
-}
-return v0;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return new S;", stop.toString());
- }
-
-
- @Test
- public void testAntiDeps4() {
- Parser parser = new Parser(
-"""
-struct S { int f; }
-S v = new S;
-S t = new S;
-int i = v.f;
-if (arg+1) arg= 0;
-while (arg) v.f = 2;
-return i;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return .f;", stop.toString());
- }
-
- @Test
- public void testAntiDeps5() {
- Parser parser = new Parser(
-"""
-struct S { int f; }
-S v = new S;
-while(1) {
- while(arg+1) { arg=arg-1; }
- if (arg) break;
- v.f = 2;
-}
-return v;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return new S;", stop.toString());
- }
-
- @Test
- public void testAntiDeps6() {
- Parser parser = new Parser(
-"""
-struct s { int v; }
-s ptr=new s;
-while( -arg )
- ptr = new s;
-while(1)
- arg = arg+ptr.v;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("Stop[ return 0; return 0; ]", stop.toString());
- }
-
- @Test
- public void testAntiDeps7() {
- Parser parser = new Parser(
-"""
-struct S { int f; }
-S v = new S;
-S t = new S;
-int i = v.f;
-while (arg) {
- v.f = arg;
- arg = arg-1;
-}
-return i;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return .f;", stop.toString());
- }
-
- @Test
- public void testAntiDeps8() {
- Parser parser = new Parser(
-"""
-struct S { int f; }
-S v = new S;
-S t = new S;
-while(arg) {
- arg=arg-1;
- int f = v.f;
- v.f = 2;
- if (arg) arg = f;
- else v.f = 3;
-}
-return arg;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop17,arg,Phi(Region41,.f,0));", stop.toString());
- }
-
- @Test
- public void testAntiDeps9() {
- Parser parser = new Parser(
-"""
-struct S { int f; }
-S v = new S;
-S t = new S;
-if (arg) {
- v.f=2;
- int i=t.f;
- v.f=i;
-}
-return v;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return new S;", stop.toString());
- }
-
- @Test
- public void testExample2() {
- Parser parser = new Parser(
- """
-struct S { int f; }
-S v = new S;
-int i = arg;
-while (arg > 0) {
- int j = i/3;
- if (arg == 5)
- v.f = j;
- arg = arg - 1;
-}
-return v;
- """);
- StopNode stop = parser.parse().iterate();
- //assertEquals("return new S;", stop.toString());
- }
-
- @Test
- public void testSplit() {
- Parser parser = new Parser(
- """
-struct S { int f; }
-S s = new S;
-if( arg==0 ) s.f = 1;
-else if (arg == 1) s.f = 1;
-return s.f;
-""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region43,1,Phi(Region38,1,0));", stop.toString());
- assertEquals(1L, Evaluator.evaluate(stop, 0));
- assertEquals(1L, Evaluator.evaluate(stop, 1));
- assertEquals(0L, Evaluator.evaluate(stop, 2));
}
}
diff --git a/chapter13/src/test/java/com/seaofnodes/simple/Chapter12Test.java b/chapter13/src/test/java/com/seaofnodes/simple/Chapter12Test.java
index ced5606c..cbb50dba 100644
--- a/chapter13/src/test/java/com/seaofnodes/simple/Chapter12Test.java
+++ b/chapter13/src/test/java/com/seaofnodes/simple/Chapter12Test.java
@@ -2,83 +2,199 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.evaluator.Evaluator;
import com.seaofnodes.simple.node.StopNode;
+import com.seaofnodes.simple.type.TypeStruct;
+import com.seaofnodes.simple.type.TypeMemPtr;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
public class Chapter12Test {
- @Test public void testSubZeroFloat() {
- var stop = new Parser("flt x = arg; return 0-x;").parse().iterate();
- // Compare strings so +0.0 and -0.0 remain distinct.
- assertEquals("0.0", Evaluator.evaluate(stop,0).toString());
- assertEquals("-1.0", Evaluator.evaluate(stop,1).toString());
+
+ @Test
+ public void testLinkedList0() {
+ Parser parser = new Parser(
+"""
+struct LLI { LLI? next; int i; }
+LLI? head = null;
+while( arg ) {
+ LLI x = new LLI;
+ x.next = head;
+ x.i = arg;
+ head = x;
+ arg = arg-1;
+}
+return head.next.i;
+""");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Might be null accessing 'i'",e.getMessage()); }
}
- @Test public void testDeadNumericReturns() {
- for( String[] test : new String[][] {
- {"if(1) return 7; else return 2.5;", "7"},
- {"if(0) return 7; else return 2.5;", "2.5"},
- {"return 7; return 2.5;", "7"},
- {"return 2.5; return 7;", "2.5"}
- } ) {
- String src = test[0];
- var stop = new Parser(src).parse().iterate();
- assertEquals(src,test[1],com.seaofnodes.simple.evaluator.Evaluator.evaluate(stop,0).toString());
- }
+ @Test
+ public void testLinkedList1() {
+ Parser parser = new Parser(
+"""
+struct LLI { LLI? next; int i; }
+LLI? head = null;
+while( arg ) {
+ LLI x = new LLI;
+ x.next = head;
+ x.i = arg;
+ head = x;
+ arg = arg-1;
+}
+if( !head ) return 0;
+LLI? next = head.next;
+if( next==null ) return 1;
+return next.i;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("Stop[ return 0; return 1; return .i; ]", stop.toString());
+ assertEquals(2L, Evaluator.evaluate(stop, 3));
}
+ @Test
+ public void testCoRecur() {
+ Parser parser = new Parser(
+"""
+struct Left { int i; Right? f; }
+struct Right { int f; Left? i; }
+Left left = new Left;
+left.i = 17;
+Right right = new Right;
+right.f = 314;
+left.f = right;
+right.i = left;
+return right.i.f.i.i;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return 17;", stop.toString());
+ }
@Test
- public void testJig() {
+ public void testNullReright() {
Parser parser = new Parser(
"""
-return 3.14;
+struct N { N next; int i; }
+N n = new N;
+return n.next;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return 3.14;", stop.toString());
- assertEquals(3.14, Evaluator.evaluate(stop, 0));
+ assertEquals("return null;", stop.toString());
}
@Test
- public void testFloat() {
+ public void testNullRef1() {
Parser parser = new Parser(
"""
-return 3.14;
+struct N { N next; int i; }
+N n = new N;
+n.next = new N;
+return n.next;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return 3.14;", stop.toString());
- assertEquals(3.14, Evaluator.evaluate(stop, 0));
+ assertEquals("return new N;", stop.toString());
}
@Test
- public void testSquareRoot() {
+ public void testNullRef2() {
Parser parser = new Parser(
"""
-flt guess = arg;
-while( 1 ) {
- flt next = (arg/guess + guess)/2;
- if( next == guess ) break;
- guess = next;
-}
-return guess;
+struct N { N next; int i; }
+N n = new N;
+n.next = null;
+return n.next;
+""");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Cannot store null into field *N next",e.getMessage()); }
+ }
+
+
+ @Test
+ public void testNullRef4() {
+ Parser parser = new Parser("-null-5/null-5");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Expected an identifier, found 'null'",e.getMessage()); }
+ }
+
+ @Test public void testNullRef5() {
+ Parser parser = new Parser("return null+42;");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Cannot 'Add' null",e.getMessage()); }
+ }
+
+ @Test
+ public void testEmpty() {
+ Parser parser = new Parser(
+"""
+struct S{};
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop10,(flt)arg,(((ToFloat/Phi_guess)+Phi_guess)/2.0));", stop.toString());
- assertEquals(3.0, Evaluator.evaluate(stop, 9));
- assertEquals(1.414213562373095, Evaluator.evaluate(stop, 2));
+ assertEquals("return 0;", stop.toString());
+ assertEquals(0L, Evaluator.evaluate(stop, 0));
+ }
+
+ @Test
+ public void testForwardReright() {
+ Parser parser = new Parser(
+"""
+struct S1 { S2 s; }
+return new S2;
+""");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Unknown struct type 'S2'",e.getMessage()); }
}
@Test
- public void testFPOps() {
+ public void testForwardRef1() {
Parser parser = new Parser(
"""
-flt x = arg;
-return x+1==x;
+struct S1 { S2? s; }
+struct S2 { int x; }
+return new S1.s=new S2;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return ((flt)arg==(ToFloat+1.0));", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 1));
+ assertEquals("return new S1;", stop.toString());
}
+ @Test
+ public void testcheckNull() {
+ Parser parser = new Parser(
+"""
+struct I {int i;}
+struct P {I pi;}
+P p1 = new P;
+P p2 = new P;
+p2.pi = new I;
+p2.pi.i = 2;
+if (arg) p1 = new P;
+return p1.pi.i + 1;
+""");
+ try { parser.parse().iterate(); fail(); }
+ catch( Exception e ) { assertEquals("Might be null accessing 'i'",e.getMessage()); }
+ }
+
+
+ @Test
+ public void testShallowRecursiveType() {
+ new Parser("struct LLI { LLI? next; int i; } return 0;").parse().iterate();
+ TypeStruct l1 = (TypeStruct)Parser.TYPES.get("LLI");
+ TypeStruct l0 = ((TypeMemPtr)l1._fields[0]._type)._obj;
+ assertEquals("LLI", l0._name);
+ assertNull(l0._fields);
+ assertEquals(2, l1._fields.length);
+ assertSame(l0, l1.meet(l0));
+ assertSame(l0, l0.dual());
+ assertSame(l0, l0.glb());
+ }
+
+ @Test
+ public void testUnusedForwardReference() {
+ StopNode stop = new Parser("struct Holder { Missing? ref; } Holder h = new Holder; return h.ref;")
+ .parse().iterate();
+ assertEquals("return null;", stop.toString());
+ assertNull(((TypeStruct)Parser.TYPES.get("Missing"))._fields);
+ }
}
diff --git a/chapter13/src/test/java/com/seaofnodes/simple/Chapter13Test.java b/chapter13/src/test/java/com/seaofnodes/simple/Chapter13Test.java
index 13ac58f4..2929d1a4 100644
--- a/chapter13/src/test/java/com/seaofnodes/simple/Chapter13Test.java
+++ b/chapter13/src/test/java/com/seaofnodes/simple/Chapter13Test.java
@@ -1,197 +1,532 @@
package com.seaofnodes.simple;
+
import com.seaofnodes.simple.evaluator.Evaluator;
import com.seaofnodes.simple.node.StopNode;
-import org.junit.Ignore;
import org.junit.Test;
+
+
import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
public class Chapter13Test {
+
+ // A placeholder test used to rapidly rotate through fuzzer produced issues
@Test
- public void testJig() {
+ public void testFuzzer() {
Parser parser = new Parser(
"""
-return 3.14;
+while(-----arg) {
+ while(-arg) {
+ while(-----arg*---0) {
+ int N=--false==false<--arg;
+ while(N<-4==N) {
+ N=N+N;
+ }
+ }
+ {
+ int kzEl_r7EYz=3*----0!=-arg<(37*--97!=0/arg==arg)<=-(--arg!=0);
+ while(kzEl_r7EYz<---1) {
+ kzEl_r7EYz=kzEl_r7EYz+kzEl_r7EYz*arg>=24;
+ if(7) continue;
+ kzEl_r7EYz=false;
+ if(-kzEl_r7EYz+arg<-3) continue;
+ else {
+ while(kzEl_r7EYz) continue;
+ arg=----3;
+ kzEl_r7EYz=-(-----arg*1>-kzEl_r7EYz);
+ int v2uF_4ba=9;
+ }
+ }
+ }
+ int pjcp7rv=arg;
+ {
+ int m2ya3=--arg-pjcp7rv;
+ while(m2ya3<-----pjcp7rv>=pjcp7rv+--pjcp7rv!=---pjcp7rv!=-arg<---(-m2ya3)>m2ya3<=-----true>14!=--m2ya3) {
+ m2ya3=m2ya3+-(--0<=false<pjcp7rv/arg<=-true);
+ while((--m2ya3)<--arg<-----arg*false!=arg<pjcp7rv*arg) {
+ pjcp7rv=-3+-arg;
+ break;
+ }
+ while(-3) {
+ while(67+0-61) m2ya3=m2ya3;
+ if(----pjcp7rv==---(---arg)) pjcp7rv=0!=-23;
+ else pjcp7rv=----89;
+ while(m2ya3!=m2ya3<pjcp7rv) m2ya3=1;
+ while(--arg<=pjcp7rv) pjcp7rv=-32<=-m2ya3+62;
+ }
+ break;
+ }
+ }
+ {
+ int arg=-1<-9!=(-arg>=-pjcp7rv>=arg<=arg);
+ while(arg<-arg) {
+ arg=arg+--arg;
+ {
+ while((--93)--pjcp7rv*-pjcp7rv+arg) arg=0-(6>=(--(---41/(0))!=--pjcp7rv)>=---2*33);
+ pjcp7rv=11;
+ int c=pjcp7rv;
+ if(--107) break;
+ else break;
+ }
+ }
+ }
+ pjcp7rv=pjcp7rv;
+ int aR0Wkua=-pjcp7rv==pjcp7rv;
+ {
+ if(-0!=-0) while(-true) break;
+ else {
+ while(aR0Wkua) pjcp7rv=-0/pjcp7rv;
+ pjcp7rv=pjcp7rv;
+ }
+ }
+ }
+ if(-arg) {
+ {
+ int Isdlt0fr91=0!=arg;
+ while(Isdlt0fr91<-Isdlt0fr91==-Isdlt0fr91+true) {
+ Isdlt0fr91=Isdlt0fr91+-1!=true;
+ if(-arg) if(--arg<=Isdlt0fr91) Isdlt0fr91=-Isdlt0fr91;
+ int i=---arg<---arg!=--40;
+ {
+ int jVX43Ln4Dm=-(6)==-Isdlt0fr91/----0;
+ while(jVX43Ln4Dm<--(3-3)) {
+ jVX43Ln4Dm=jVX43Ln4Dm+-0<25;
+ i=24<--(jVX43Ln4Dm);
+ }
+ }
+ i=(arg!=----false>=7);
+ int Te=5>-arg;
+ break;
+ }
+ }
+ arg=arg!=--true/arg;
+ {
+ int UX=arg;
+ while(UX<-3*arg) {
+ UX=UX+arg/-1;
+ arg=-3--(-UX--47<arg==--arg==arg)/arg--UX>=-arg;
+ if(---12) while(-UX) break;
+ break;
+ }
+ }
+ continue;
+ }
+ if(arg) arg=--0;
+ else if(1) continue;
+ int DtTlhjYq=19==arg;
+ {
+ arg=--DtTlhjYq<=(DtTlhjYq/---DtTlhjYq<=-DtTlhjYq);
+ if(---arg) continue;
+ else {
+ int Zsl=40;
+ while(Zsl<--Zsl--false>((Zsl))==Zsl!=--DtTlhjYq-DtTlhjYq-DtTlhjYq) {
+ Zsl=Zsl+arg;
+ }
+ }
+ if((arg)) if(-DtTlhjYq) {
+ }
+ else while(((2)==3*arg/DtTlhjYq)<=0==---arg) return false;
+ else {
+ int lwbF=arg>=0;
+ while(lwbF<57) {
+ lwbF=lwbF+--(-arg+lwbF)>=-2>=arg!=arg;
+ arg=----0+0;
+ while(49) break;
+ if(true) DtTlhjYq=-----arg;
+ DtTlhjYq=0!=-----(0);
+ }
+ }
+ arg=---arg!=DtTlhjYq/--0;
+ DtTlhjYq=-DtTlhjYq/(--arg<arg);
+ if(32) {
+ int q=-DtTlhjYq;
+ while(q<arg*(0*q)/0) {
+ q=q+DtTlhjYq;
+ DtTlhjYq=arg;
+ }
+ }
+ else if(--arg) if(15) break;
+ else arg=0==arg<=-55>DtTlhjYq;
+ else {
+ }
+ DtTlhjYq=-0;
+ {
+ while((0)<-arg) DtTlhjYq=-DtTlhjYq;
+ }
+ if(30/--(arg)>=0) DtTlhjYq=-0>=arg;
+ else {
+ int Y2wcIQJ=--DtTlhjYq*---11;
+ if(arg==arg) {
+ Y2wcIQJ=-----Y2wcIQJ;
+ Y2wcIQJ=Y2wcIQJ<7;
+ if(DtTlhjYq) arg=arg;
+ else break;
+ while(-arg!=-31==(false<=-15>=-DtTlhjYq+--6*-----Y2wcIQJ<=---arg!=-Y2wcIQJ<=---0<-arg*--Y2wcIQJ)) DtTlhjYq=arg!=3;
+ }
+ continue;
+ }
+ arg=DtTlhjYq;
+ arg=--3/(---arg)>=80>=-DtTlhjYq<=DtTlhjYq;
+ arg=51+---(-true)!=arg!=----1==-DtTlhjYq;
+ if(11<=arg) if((arg)+79<32+(-arg)) continue;
+ else if(true==DtTlhjYq) break;
+ break;
+ }
+}
+if(arg!=62==arg) {
+ while(---arg) {
+ {
+ int bhmDacPM=arg/arg;
+ while(bhmDacPM<arg) {
+ bhmDacPM=bhmDacPM+80*((-bhmDacPM)/---bhmDacPM>1)<bhmDacPM-2!=7;
+ arg=-0;
+ arg=bhmDacPM>=-arg+-arg;
+ break;
+ }
+ }
+ int arg=true;
+ arg=arg<=arg;
+ {
+ int KlC=30;
+ while(KlC<-2) {
+ KlC=KlC+-1!=25<=-KlC>KlC>----0;
+ int Ij=arg>=-arg<=---10;
+ {
+ int LAbgurVa2R=-arg!=Ij;
+ while(LAbgurVa2R<-true) {
+ LAbgurVa2R=LAbgurVa2R+Ij;
+ KlC=-LAbgurVa2R;
+ Ij=42*(-Ij>true);
+ while(Ij) arg=KlC;
+ KlC=-----7>-(--KlC<--7)==-LAbgurVa2R;
+ }
+ }
+ int a=Ij<=-KlC;
+ if(Ij) {
+ int r1W=a!=-false;
+ }
+ else {
+ arg=56>=Ij;
+ while(54) break;
+ arg=8>=-(a<--Ij)/a>=-a-KlC;
+ }
+ int Yt_T=KlC;
+ arg=KlC;
+ }
+ }
+ if(----false/--arg/12+-----0) if(-9+-arg) while(arg!=(arg)) arg=--1>=-71-arg!=---(--arg*11+102)/--arg;
+ else if(----arg) continue;
+ if(-arg-arg) {
+ arg=-(-arg)*--(-3)<(-7==-arg----arg);
+ {
+ int arg=arg;
+ while(arg<-0>arg==-arg-(-arg>1+11)) {
+ arg=arg+6-arg!=--0>---30>=---arg<=arg/(3)+---arg;
+ }
+ }
+ arg=-arg+arg;
+ arg=--47+-arg<1>arg*(-arg==-arg--0>3);
+ arg=(----arg)!=arg+--59;
+ arg=--4+arg==-(arg)-arg;
+ }
+ else {
+ while(--true-----true*-25+-18>arg>=-----(-arg)---13) {
+ arg=-0+--true<false;
+ continue;
+ }
+ arg=true<=arg;
+ break;
+ }
+ arg=arg*22<--(0+-arg);
+ {
+ continue;
+ }
+ }
+ arg=1*---((3<=true)==---2);
+ arg=7+-(-1);
+ return arg+--true;
+}
+else {
+ int o3ypEsewxM=-(---arg);
+ int vzXB=(-(---11<=--o3ypEsewxM>=--0))<=-0/-arg;
+ int L=-arg;
+ int B4mHcrf=---vzXB;
+ int oZWGdzZl=-o3ypEsewxM;
+ int K6tQqa1=11;
+ while(0!=--L) {
+ int sdZfTM=vzXB*true+K6tQqa1;
+ while(sdZfTM<false) {
+ sdZfTM=sdZfTM+arg;
+ sdZfTM=oZWGdzZl;
+ K6tQqa1=-K6tQqa1;
+ arg=---K6tQqa1;
+ while((3+-K6tQqa1)<oZWGdzZl) {
+ if(-----(-1)) sdZfTM=-false;
+ {}
+ }
+ K6tQqa1=vzXB!=-(B4mHcrf+-7>false);
+ sdZfTM=vzXB;
+ }
+ }
+ L=false;
+ while(31) B4mHcrf=arg;
+ oZWGdzZl=-0<--oZWGdzZl<=--2;
+ if(K6tQqa1>=20<=46) {
+ if(-1+-L) if(oZWGdzZl) {
+ int wmhv=-----0;
+ }
+ int arg=0;
+ o3ypEsewxM=---(oZWGdzZl);
+ return -oZWGdzZl/0;
+ }
+ else if(arg) {
+ int yKk5=B4mHcrf>=4>K6tQqa1<-B4mHcrf/o3ypEsewxM;
+ return -K6tQqa1;
+ }
+ else {
+ if(---0) {
+ {}
+ {}
+ }
+ return o3ypEsewxM;
+ }
+}
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return 3.14;", stop.toString());
- assertEquals(3.14, Evaluator.evaluate(stop, 0));
+ assertEquals("Stop[ return 9; return 0; return 0; return 0; return 0; ]", stop.toString());
}
+
@Test
- public void testLinkedList0() {
+ public void testPrimes() {
Parser parser = new Parser(
"""
-struct LLI { LLI? next; int i; }
-LLI? head = null;
-while( arg ) {
- LLI x = new LLI;
- x.next = head;
- x.i = arg;
- head = x;
- arg = arg-1;
+if( arg < 2 ) return 0;
+int primeCount = 1;
+int prime = 3;
+while( prime <= arg ) {
+ int isPrime = 1;
+ // Check for even case, so the next loop need only check odds
+ if( (prime/2)*2 == prime )
+ continue;
+ // Check odds up to sqrt of prime
+ int j = 3;
+ while( j*j <= prime ) {
+ if( (prime/j)*j == prime ) {
+ isPrime = 0;
+ break;
+ }
+ j = j + 2;
+ }
+ if( isPrime )
+ primeCount = primeCount + 1;
+ prime = prime + 2;
}
-return head.next.i;
+return primeCount;
""");
- try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Might be null accessing 'i'",e.getMessage()); }
+ StopNode stop = parser.parse().iterate();
+ assertEquals("Stop[ return 0; return Phi(Loop21,1,Phi(Region88,Phi_primeCount,Phi(Region82,(Phi_primeCount+1),Phi_primeCount))); ]", stop.toString());
+ assertEquals(0L, Evaluator.evaluate(stop, 1)); // No primes 1 or below
+ assertEquals(1L, Evaluator.evaluate(stop, 2)); // 2
+ assertEquals(2L, Evaluator.evaluate(stop, 3)); // 2, 3
+ assertEquals(2L, Evaluator.evaluate(stop, 4)); // 2, 3
+ assertEquals(3L, Evaluator.evaluate(stop, 5)); // 2, 3, 5
+ assertEquals(4L, Evaluator.evaluate(stop, 10)); // 2, 3, 5, 7
}
@Test
- public void testLinkedList1() {
+ public void testAntiDeps1() {
Parser parser = new Parser(
"""
-struct LLI { LLI? next; int i; }
-LLI? head = null;
-while( arg ) {
- LLI x = new LLI;
- x.next = head;
- x.i = arg;
- head = x;
- arg = arg-1;
-}
-if( !head ) return 0;
-LLI? next = head.next;
-if( next==null ) return 1;
-return next.i;
+struct S { int f; }
+S v=new S;
+v.f = 2;
+int i=new S.f;
+i=v.f;
+if (arg) v.f=1;
+return i;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("Stop[ return 0; return 1; return .i; ]", stop.toString());
- assertEquals(2L, Evaluator.evaluate(stop, 3));
+ assertEquals("return .f;", stop.toString());
}
@Test
- public void testCoRecur() {
+ public void testAntiDeps2() {
Parser parser = new Parser(
"""
-struct int0 { int i; flt0? f; }
-struct flt0 { flt f; int0? i; }
-int0 i0 = new int0;
-i0.i = 17;
-flt0 f0 = new flt0;
-f0.f = 3.14;
-i0.f = f0;
-f0.i = i0;
-return f0.i.f.i.i;
+struct S { int f; }
+S v = new S;
+S t = new S;
+int i = 0;
+if (arg) {
+ if (arg+1) v = t;
+ i = v.f;
+} else {
+ v.f = 2;
+}
+return i;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return 17;", stop.toString());
+ assertEquals("return Phi(Region37,.f,0);", stop.toString());
}
@Test
- public void testNullRef0() {
+ public void testAntiDeps3() {
Parser parser = new Parser(
"""
-struct N { N next; int i; }
-N n = new N;
-return n.next;
+struct S { int f; }
+S v0 = new S;
+S? v1;
+if (arg) v1 = new S;
+if (v1) {
+ v0.f = v1.f;
+} else {
+ v0.f = 2;
+}
+return v0;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return null;", stop.toString());
+ assertEquals("return new S;", stop.toString());
}
+
@Test
- public void testNullRef1() {
+ public void testAntiDeps4() {
Parser parser = new Parser(
"""
-struct N { N next; int i; }
-N n = new N;
-n.next = new N;
-return n.next;
+struct S { int f; }
+S v = new S;
+S t = new S;
+int i = v.f;
+if (arg+1) arg= 0;
+while (arg) v.f = 2;
+return i;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return new N;", stop.toString());
+ assertEquals("return .f;", stop.toString());
}
@Test
- public void testNullRef2() {
+ public void testAntiDeps5() {
Parser parser = new Parser(
"""
-struct N { N next; int i; }
-N n = new N;
-n.next = null;
-return n.next;
+struct S { int f; }
+S v = new S;
+while(1) {
+ while(arg+1) { arg=arg-1; }
+ if (arg) break;
+ v.f = 2;
+}
+return v;
""");
- try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Cannot store null into field *N next",e.getMessage()); }
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return new S;", stop.toString());
}
@Test
- public void testNullRef3() {
+ public void testAntiDeps6() {
Parser parser = new Parser(
"""
-struct N { N next; int i; }
-N n = new N;
-n.i = 3.14;
-return n.i;
+struct s { int v; }
+s ptr=new s;
+while( -arg )
+ ptr = new s;
+while(1)
+ arg = arg+ptr.v;
""");
- try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Cannot store 3.14 into field IntBot i",e.getMessage()); }
+ StopNode stop = parser.parse().iterate();
+ assertEquals("Stop[ return 0; return 0; ]", stop.toString());
}
@Test
- public void testNullRef4() {
- Parser parser = new Parser("-null-5/null-5");
- try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Expected an identifier, found 'null'",e.getMessage()); }
- }
-
- @Test public void testNullRef5() {
- Parser parser = new Parser("return null+42;");
- try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Cannot 'Add' null",e.getMessage()); }
+ public void testAntiDeps7() {
+ Parser parser = new Parser(
+"""
+struct S { int f; }
+S v = new S;
+S t = new S;
+int i = v.f;
+while (arg) {
+ v.f = arg;
+ arg = arg-1;
+}
+return i;
+""");
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return .f;", stop.toString());
}
@Test
- public void testEmpty() {
+ public void testAntiDeps8() {
Parser parser = new Parser(
"""
-struct S{};
+struct S { int f; }
+S v = new S;
+S t = new S;
+while(arg) {
+ arg=arg-1;
+ int f = v.f;
+ v.f = 2;
+ if (arg) arg = f;
+ else v.f = 3;
+}
+return arg;
""");
StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 0));
+ assertEquals("return Phi(Loop17,arg,Phi(Region41,.f,0));", stop.toString());
}
@Test
- public void testForwardRef0() {
+ public void testAntiDeps9() {
Parser parser = new Parser(
"""
-struct S1 { S2 s; }
-return new S2;
+struct S { int f; }
+S v = new S;
+S t = new S;
+if (arg) {
+ v.f=2;
+ int i=t.f;
+ v.f=i;
+}
+return v;
""");
- try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Unknown struct type 'S2'",e.getMessage()); }
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return new S;", stop.toString());
}
@Test
- public void testForwardRef1() {
+ public void testExample2() {
Parser parser = new Parser(
-"""
-struct S1 { S2? s; }
-struct S2 { int x; }
-return new S1.s=new S2;
-""");
+ """
+struct S { int f; }
+S v = new S;
+int i = arg;
+while (arg > 0) {
+ int j = i/3;
+ if (arg == 5)
+ v.f = j;
+ arg = arg - 1;
+}
+return v;
+ """);
StopNode stop = parser.parse().iterate();
- assertEquals("return new S1;", stop.toString());
+ //assertEquals("return new S;", stop.toString());
}
@Test
- public void testcheckNull() {
+ public void testSplit() {
Parser parser = new Parser(
-"""
-struct I {int i;}
-struct P {I pi;}
-P p1 = new P;
-P p2 = new P;
-p2.pi = new I;
-p2.pi.i = 2;
-if (arg) p1 = new P;
-return p1.pi.i + 1;
+ """
+struct S { int f; }
+S s = new S;
+if( arg==0 ) s.f = 1;
+else if (arg == 1) s.f = 1;
+return s.f;
""");
- try { parser.parse().iterate(); fail(); }
- catch( Exception e ) { assertEquals("Might be null accessing 'i'",e.getMessage()); }
+ StopNode stop = parser.parse().iterate();
+ assertEquals("return Phi(Region43,1,Phi(Region38,1,0));", stop.toString());
+ assertEquals(1L, Evaluator.evaluate(stop, 0));
+ assertEquals(1L, Evaluator.evaluate(stop, 1));
+ assertEquals(0L, Evaluator.evaluate(stop, 2));
}
-
}
diff --git a/chapter13/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java b/chapter13/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
index 40a2f78d..355d906b 100644
--- a/chapter13/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
+++ b/chapter13/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
@@ -2,7 +2,10 @@ package com.seaofnodes.simple.evaluator;
import com.seaofnodes.simple.Utils;
import com.seaofnodes.simple.node.*;
-import com.seaofnodes.simple.type.*;
+import com.seaofnodes.simple.type.Field;
+import com.seaofnodes.simple.type.TypeInteger;
+import com.seaofnodes.simple.type.TypeMemPtr;
+import com.seaofnodes.simple.type.TypeStruct;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
@@ -77,10 +80,6 @@ public class Evaluator {
long in2 = vall(div.in(2));
return in2 == 0 ? 0 : vall(div.in(1)) / in2;
}
- private double div(DivFNode div) {
- double in2 = vald(div.in(2));
- return in2 == 0 ? 0 : vald(div.in(1)) / in2;
- }
private Object alloc(NewNode alloc) {
var type = ((TypeMemPtr)alloc.compute())._obj;
@@ -116,16 +115,10 @@ public class Evaluator {
if (v instanceof Number n) return n.longValue();
throw new AssertionError("Not a long " + v);
}
- private double vald(Node node) {
- var v = val(node);
- if (v instanceof Number n) return n.doubleValue();
- throw new AssertionError("Not a double " + v);
- }
private Object cons(ConstantNode cons) {
var type = cons.compute();
if (type instanceof TypeInteger i) return i.value();
- if (type instanceof TypeFloat i) return i.value();
assert type instanceof TypeMemPtr;
return null;
}
@@ -134,24 +127,15 @@ public class Evaluator {
return switch (node) {
case ConstantNode cons -> cons(cons);
case AddNode add -> vall(add.in(1)) + vall(add.in(2));
- case AddFNode add -> vald(add.in(1)) + vald(add.in(2));
- case BoolNode.EQF eq -> vald(eq.in(1)) == vald(eq.in(2)) ? 1L : 0L;
- case BoolNode.LEF le -> vald(le.in(1)) <= vald(le.in(2)) ? 1L : 0L;
- case BoolNode.LTF lt -> vald(lt.in(1)) < vald(lt.in(2)) ? 1L : 0L;
case BoolNode.EQ eq -> Objects.equals(val(eq.in(1)), val(eq.in(2))) ? 1L : 0L;
case BoolNode.LE le -> vall(le.in(1)) <= vall(le.in(2)) ? 1L : 0L;
case BoolNode.LT lt -> vall(lt.in(1)) < vall(lt.in(2)) ? 1L : 0L;
case DivNode div -> div(div);
- case DivFNode div -> div(div);
case MinusNode minus -> -vall(minus.in(1));
- case MinusFNode minus -> -vald(minus.in(1));
case MulNode mul -> vall(mul.in(1)) * vall(mul.in(2));
- case MulFNode mul -> vald(mul.in(1)) * vald(mul.in(2));
case NotNode not -> isTrue(val(not.in(1))) ? 0L : 1L;
case SubNode sub -> vall(sub.in(1)) - vall(sub.in(2));
- case SubFNode sub -> vald(sub.in(1)) - vald(sub.in(2));
case CastNode cast -> val(cast.in(1));
- case ToFloatNode cast -> (double)vall(cast.in(1));
case LoadNode load -> load(load);
case StoreNode store -> store(store);
case MemMergeNode merge -> MEMORY;
diff --git a/chapter13/src/test/java/com/seaofnodes/simple/fuzzer/ScriptGenerator.java b/chapter13/src/test/java/com/seaofnodes/simple/fuzzer/ScriptGenerator.java
index 94929241..14baadba 100644
--- a/chapter13/src/test/java/com/seaofnodes/simple/fuzzer/ScriptGenerator.java
+++ b/chapter13/src/test/java/com/seaofnodes/simple/fuzzer/ScriptGenerator.java
@@ -87,21 +87,7 @@ public class ScriptGenerator {
}
- private static final Type TYPE_FLT = new Type("flt");
-
- private static class TypeInt extends Type {
-
- TypeInt() {
- super("int");
- }
-
- @Override
- boolean isa(Type other) {
- return super.isa(other) || other == TYPE_FLT;
- }
- }
-
- private static final Type TYPE_INT = new TypeInt();
+ private static final Type TYPE_INT = new Type("int");
private static class Variable {
@@ -368,7 +354,6 @@ public class ScriptGenerator {
private Type getType(boolean allowForward) {
int t = random.nextInt(10);
if( t<5 || structs.isEmpty() ) return TYPE_INT;
- if( t<7 ) return TYPE_FLT;
if (allowForward && random.nextInt(10)==0) {
int i=random.nextInt(forwardStructs.size()+1);
if (i>0) return forwardStructs.get(i-1).nullable;
@@ -725,7 +710,7 @@ public class ScriptGenerator {
*/
public void genExpression(Type type, boolean change) {
if (change && generateInvalid()) type = getType();
- if (type != TYPE_INT && type != TYPE_FLT) {
+ if (type != TYPE_INT) {
genUnary(type, false);
return;
}
@@ -742,7 +727,7 @@ public class ScriptGenerator {
*/
public void genUnary(Type type, boolean change) {
if (change && generateInvalid()) type = getType();
- if (type != TYPE_INT && type != TYPE_FLT) {
+ if (type != TYPE_INT) {
genSuffix(type, false);
return;
}
@@ -822,15 +807,14 @@ public class ScriptGenerator {
*/
public void genConst(Type type) {
if (generateInvalid()) type = getType();
- if (type == TYPE_INT || (type == TYPE_FLT && random.nextBoolean())) {
+ if (type == TYPE_INT) {
var rand = random.nextInt(10);
switch (rand) {
case 0 -> sb.append("true");
case 1 -> sb.append("false");
default -> sb.append(random.nextInt(1<<(rand-2)));
}
- } else if (type == TYPE_FLT) {
- sb.append(random.nextFloat());
+
} else if (type instanceof TypeNullable n) {
if ((n.base instanceof TypeStruct s && s.fields == null) || random.nextBoolean()) {
sb.append("null");