summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2024-12-25 22:24:58 -0800
committerCliff Click <cliffc@acm.org>2025-01-15 15:07:30 -0800
commit9814de0f115704c58c1ccbc0ebfa1fa98338f656 (patch)
treea31c2ea288bb4f8f618a67b1caa45c3a436acd42
parentfcd6dcf5265b4b23fd69af761316c064b8f9f7ce (diff)
downloadSimple-9814de0f115704c58c1ccbc0ebfa1fa98338f656.tar.gz
Simple-9814de0f115704c58c1ccbc0ebfa1fa98338f656.zip
Cleanup compile driver
Top level makes a "main" function and calls it. Opto() pass inlines main into top-level and removes the function call. If peeps are off, the function call remains; this breaks many tests. Add a FunNode function header; Return is its end. Return no long used at the top level; only a single return per-function, merging all returns (breaks many tests). Add a Call & CallEnd nodes. Fix many tests to pass again. Fix a bunch of minor bugs.
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/CodeGen.java92
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java15
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/GraphVisualizer.java2
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/IRPrinter.java49
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/IterPeeps.java3
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/JSViewer.java36
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/Parser.java168
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/AndNode.java2
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/CFGNode.java8
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/CProjNode.java3
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/CallEndNode.java79
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/CallNode.java96
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/FunNode.java47
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/LoopNode.java23
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/MemOpNode.java2
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/MultiNode.java11
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/MultiUse.java4
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/Node.java14
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/OrNode.java2
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/OutNode.java7
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/ParmNode.java47
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/PhiNode.java2
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/ProjNode.java6
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/RegionNode.java2
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/ReturnNode.java47
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/SarNode.java2
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/ScopeMinNode.java25
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/ScopeNode.java5
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/ShlNode.java2
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/StopNode.java49
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/StoreNode.java2
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/XorNode.java2
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/type/TypeFunPtr.java74
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/type/TypeTuple.java50
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/BrainFuckTest.java6
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter01Test.java32
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter02Test.java44
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter03Test.java36
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter04Test.java93
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter05Test.java63
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter06Test.java107
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter07Test.java100
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter08Test.java86
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter09Test.java129
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter10Test.java130
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter11Test.java102
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java34
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter13Test.java69
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter14Test.java116
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter15Test.java152
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter16Test.java128
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter17Test.java310
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java28
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/SchedulerTest.java18
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/TypeTest.java1
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java51
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/evaluator/Scheduler.java42
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/fuzzer/Fuzzer.java9
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/fuzzer/FuzzerUtils.java8
59 files changed, 1693 insertions, 1179 deletions
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/CodeGen.java b/chapter18/src/main/java/com/seaofnodes/simple/CodeGen.java
new file mode 100644
index 00000000..184b6f9c
--- /dev/null
+++ b/chapter18/src/main/java/com/seaofnodes/simple/CodeGen.java
@@ -0,0 +1,92 @@
+ package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.node.*;
+import com.seaofnodes.simple.type.*;
+import java.util.*;
+
+public class CodeGen {
+
+ public enum Phase {
+ Parse, Opto, TypeCheck, Schedule, RegAlloc;
+ }
+ public Phase _phase;
+
+ public final String _src;
+ // Compile-time known initial argument type
+ public final TypeInteger _arg;
+
+ public Parser P;
+ public StartNode _start;
+ public StopNode _stop ;
+
+ // Last created CodeGen as a global, for easier debugging prints
+ public static CodeGen CODE;
+
+ CodeGen( String src, TypeInteger arg ) { _phase = null; _src = src; _arg=arg; CODE=this; }
+ public CodeGen( String src ) { this(src,TypeInteger.BOT); }
+
+ public CodeGen parse() { return parse(false); }
+ public CodeGen parse(boolean disable) {
+ assert _phase == null;
+ _phase = Phase.Parse;
+ P = new Parser(_src,_arg);
+ Node._disablePeephole = disable;
+ _stop = P.parse();
+ _start = Parser.START; // Capture global start
+ P = null; // No longer parsing
+ return this;
+ }
+
+ public CodeGen opto() {
+ assert _phase == Phase.Parse;
+ _phase = Phase.Opto;
+
+ // Optimization
+ IterPeeps.iterate(_stop);
+
+ // TODO:
+ // Optimistic
+ return this;
+ }
+
+ public CodeGen typeCheck() {
+ assert _phase == Phase.Opto;
+ _phase = Phase.TypeCheck;
+
+ // Type check
+ String err = _stop.walk( Node::err );
+ if( err != null )
+ throw new RuntimeException(err);
+ return this;
+ }
+
+
+ public CodeGen codegen() { return codegen(false); }
+ public CodeGen codegen(boolean show) {
+ assert _phase == Phase.TypeCheck;
+ _phase = Phase.Schedule;
+
+ // Build the loop tree, fix never-exit loops
+ _start.buildLoopTree(_stop);
+ if( show )
+ System.out.println(new GraphVisualizer().generateDotOutput(_stop,null,null));
+
+ // TODO:
+ // loop unroll, peel, RCE, etc
+
+ GlobalCodeMotion.buildCFG(_start,_stop);
+
+ _phase = Phase.RegAlloc;
+
+ return this;
+ }
+
+ // Debugging helper
+ @Override public String toString() {
+ return _stop.p(9999);
+ }
+
+ // Debugging helper
+ public Node f(int idx) { return _stop.find(idx); }
+}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java b/chapter18/src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java
index 9698abd9..7ce12091 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java
@@ -10,10 +10,9 @@ public abstract class GlobalCodeMotion {
// Arrange that the existing isCFG() Nodes form a valid CFG. The
// Node.use(0) is always a block tail (either IfNode or head of the
// following block). There are no unreachable infinite loops.
- public static void buildCFG( StopNode stop ) {
- schedEarly();
- Parser.SCHEDULED = true;
- schedLate( stop);
+ public static void buildCFG( StartNode start, StopNode stop ) {
+ schedEarly(start);
+ schedLate(stop);
}
@@ -22,10 +21,10 @@ public abstract class GlobalCodeMotion {
// (except at loops). Since defs are visited first - and hoisted as early
// as possible, when we come to a use we place it just after its deepest
// input.
- private static void schedEarly() {
+ private static void schedEarly(StartNode start) {
ArrayList<CFGNode> rpo = new ArrayList<>();
BitSet visit = new BitSet();
- _rpo_cfg(Parser.START, visit, rpo);
+ _rpo_cfg(start, visit, rpo);
// Reverse Post-Order on CFG
for( int j=rpo.size()-1; j>=0; j-- ) {
CFGNode cfg = rpo.get(j);
@@ -164,8 +163,8 @@ public abstract class GlobalCodeMotion {
CFGNode found=null;
for( int i=1; i<phi.nIns(); i++ )
if( phi.in(i)==n )
- if( found==null ) found = phi.region().cfg(i);
- else Utils.TODO(); // Can be more than once
+ found = phi.region().cfg(i)._idom(found,null);
+
assert found!=null;
return found;
}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/GraphVisualizer.java b/chapter18/src/main/java/com/seaofnodes/simple/GraphVisualizer.java
index ea6862d2..bd778696 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/GraphVisualizer.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/GraphVisualizer.java
@@ -137,7 +137,7 @@ public class GraphVisualizer {
for (Node n : all) {
if (n instanceof RegionNode region) {
sb.append("\t\t{ rank=same; ");
- sb.append(region).append(";");
+ sb.append(region.uniqueName()).append(";");
for (Node phi : region._outputs)
if (phi instanceof PhiNode) sb.append(phi.uniqueName()).append(";");
sb.append("}\n");
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/IRPrinter.java b/chapter18/src/main/java/com/seaofnodes/simple/IRPrinter.java
index bd071870..4f422a25 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/IRPrinter.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/IRPrinter.java
@@ -18,7 +18,9 @@ public class IRPrinter {
return;
}
for( Node def : n._inputs )
- sb.append(def==null ? "____ " : "%4d ".formatted(def._nid));
+ sb.append(def==null ? "____" : "%4d".formatted(def._nid))
+ // Lazy Phi indicator
+ .append(n instanceof ScopeMinNode && def instanceof ScopeMinNode ? "^" : " ");
for( int i = n._inputs.size(); i<4; i++ )
sb.append(" ");
sb.append(" [[ ");
@@ -41,7 +43,7 @@ public class IRPrinter {
}
public static String prettyPrint(Node node, int depth) {
- return Parser.SCHEDULED
+ return CodeGen.CODE._phase.ordinal() > CodeGen.Phase.Schedule.ordinal()
? _prettyPrintScheduled( node, depth )
: _prettyPrint( node, depth );
}
@@ -54,7 +56,7 @@ public class IRPrinter {
ArrayList<Node> rpos = new ArrayList<>();
BitSet visit = new BitSet();
for( int i=bfs._lim; i< bfs._bfs.size(); i++ )
- postOrd( bfs._bfs.get(i), rpos, visit, bfs._bs);
+ postOrd( bfs._bfs.get(i), null, rpos, visit, bfs._bs);
// Reverse the post-order walk
StringBuilder sb = new StringBuilder();
boolean gap=false;
@@ -78,23 +80,26 @@ public class IRPrinter {
return sb.toString();
}
- private static void postOrd(Node n, ArrayList<Node> rpos, BitSet visit, BitSet bfs) {
+ private static void postOrd(Node n, Node prior, ArrayList<Node> rpos, BitSet visit, BitSet bfs) {
if( !bfs.get(n._nid) )
return; // Not in the BFS visit
+ if( n instanceof FunNode && !(prior instanceof StartNode) )
+ return; // Only visit Fun from Start
if( visit.get(n._nid) ) return; // Already post-order walked
visit.set(n._nid);
// First walk the CFG, then everything
if( n instanceof CFGNode ) {
for( Node use : n._outputs )
- if( use instanceof CFGNode && use.nOuts() >= 1 && !(use._outputs.get( 0 ) instanceof LoopNode) )
- postOrd(use, rpos,visit,bfs);
+ if( use instanceof CFGNode && use.nOuts() >= 1 &&
+ !(use._outputs.get( 0 ) instanceof LoopNode) )
+ postOrd(use, n, rpos,visit,bfs);
for( Node use : n._outputs )
if( use instanceof CFGNode )
- postOrd(use,rpos,visit,bfs);
+ postOrd(use,n,rpos,visit,bfs);
}
for( Node use : n._outputs )
if( use != null )
- postOrd(use, rpos,visit,bfs);
+ postOrd(use, n, rpos,visit,bfs);
// Post-order
rpos.add(n);
}
@@ -176,15 +181,15 @@ public class IRPrinter {
}
// Print by block with least idepth
StringBuilder sb = new StringBuilder();
- ArrayList<Node> bns = new ArrayList<>();
+ Ary<Node> bns = new Ary<>(Node.class);
while( !ds.isEmpty() ) {
CFGNode blk = null;
for( Node n : ns ) {
CFGNode cfg = n instanceof CFGNode cfg0 && cfg0.blockHead() ? cfg0 : n.cfg0();
- if( blk==null || cfg.idepth() < blk.idepth() )
+ if( blk==null || cfg.idepth() < blk.idepth() || (blk instanceof FunNode && !(cfg instanceof FunNode)))
blk = cfg;
}
- ds.remove(blk._nid);
+ Integer d = ds.remove(blk._nid);
ns.remove(blk);
// Print block header
@@ -192,11 +197,13 @@ public class IRPrinter {
sb.append( " ".repeat(4) ).append(" [[ ");
if( blk instanceof StartNode ) ;
else if( blk instanceof RegionNode || blk instanceof StopNode )
- for( int i=(blk instanceof StopNode ? 0 : 1); i<blk.nIns(); i++ )
+ for( int i=(blk instanceof StopNode ? 3 : 1); i<blk.nIns(); i++ )
label(sb,blk.cfg(i));
else
label(sb,blk.cfg(0));
sb.append(" ]] \n");
+ sb.append(d);
+ printLine(blk,sb);
// Collect block contents that are in the depth limit
bns.clear();
@@ -204,7 +211,8 @@ public class IRPrinter {
for( Node use : blk._outputs ) {
Integer i = ds.get(use._nid);
if( i!=null && !(use instanceof CFGNode cfg && cfg.blockHead()) ) {
- bns.add(use);
+ if( bns.find(use)==-1 )
+ bns.add(use);
xd = Math.min(xd,i);
}
}
@@ -233,12 +241,16 @@ public class IRPrinter {
private static void _walk( HashMap<Integer,Integer> ds, ArrayList<Node> ns, Node node, int d ) {
Integer nd = ds.get(node._nid);
- if( nd!=null && d <= nd ) return; // Been there, done that
- if( ds.put(node._nid,d) == null )
+ if( nd!=null && d >= nd ) return; // Been there, done that
+ Integer old = ds.put(node._nid,d) ;
+ if( old == null )
ns.add(node);
if( d == 0 ) return; // Depth cutoff
for( Node def : node._inputs )
- if( def != null )
+ if( def != null &&
+ // Don't walk into or out of functions
+ !(node instanceof CallEndNode && def instanceof ReturnNode) &&
+ !(node instanceof FunNode && def instanceof CallNode) )
_walk(ds,ns,def,d-1);
}
@@ -250,9 +262,10 @@ public class IRPrinter {
if( !blk.blockHead() ) blk = blk.cfg(0);
sb.append( "%-9.9s ".formatted( label( blk ) ) );
}
- static void printLine( Node n, StringBuilder sb, ArrayList<Node> bns, int i, HashMap<Integer,Integer> ds, ArrayList<Node> ns ) {
+ static void printLine( Node n, StringBuilder sb, Ary<Node> bns, int i, HashMap<Integer,Integer> ds, ArrayList<Node> ns ) {
+ sb.append(ds.get(n._nid));
printLine( n, sb );
- if( i != -1 ) Utils.del(bns,i);
+ if( i != -1 ) bns.del(i);
ds.remove(n._nid);
ns.remove(n);
}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/IterPeeps.java b/chapter18/src/main/java/com/seaofnodes/simple/IterPeeps.java
index aad9bf31..a321b13e 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/IterPeeps.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/IterPeeps.java
@@ -57,7 +57,7 @@ public abstract class IterPeeps {
/**
* Iterate peepholes to a fixed point
*/
- public static StopNode iterate(StopNode stop) {
+ public static void iterate( StopNode stop) {
assert progressOnList(stop);
int cnt=0;
@@ -93,7 +93,6 @@ public abstract class IterPeeps {
n.kill(); // Just plain dead
}
- return stop;
}
// Visit ALL nodes and confirm the invariant:
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/JSViewer.java b/chapter18/src/main/java/com/seaofnodes/simple/JSViewer.java
index 7b8f2631..869448fa 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/JSViewer.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/JSViewer.java
@@ -1,10 +1,11 @@
package com.seaofnodes.simple;
+import com.seaofnodes.simple.CodeGen;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.type.*;
-
-import java.nio.file.Paths;
import java.io.IOException;
+import java.util.Stack;
+import java.nio.file.Paths;
import java.util.Collection;
public class JSViewer implements AutoCloseable {
@@ -21,8 +22,7 @@ public class JSViewer implements AutoCloseable {
static SimpleWebSocket SERVER;
static int N; // Dot frames
- static Parser P; //
- static StopNode STOP; //
+ static CodeGen CODE; //
JSViewer() throws Exception {
// Launch server; hand shake
@@ -42,16 +42,13 @@ public class JSViewer implements AutoCloseable {
try {
N=0;
// Use parser scope, xscope when building views
- P = new Parser(src);
- STOP = P.STOP;
+ CODE = new CodeGen(src);
SHOW = true;
show();
// Parse program, generating views at every parse point
- P.parse();
+ CODE.parse();
// No longer user parse internal state when building views
- P = null;
- STOP.iterate();
-
+ CODE.opto();
// Catch and ignore Parser errors
} catch(RuntimeException re) {
@@ -72,11 +69,14 @@ public class JSViewer implements AutoCloseable {
public static void show() { if( SERVER!=null && SHOW ) _show(); }
private static void _show() {
- Collection<Node> all = GraphVisualizer.findAll(P==null ? null : P._xScopes, STOP, P==null ? null : P._scope);
+ boolean midParse = CODE.P!=null;
+ Stack<ScopeNode> xScopes = midParse ? CODE.P._xScopes : null;
+
+ Collection<Node> all = GraphVisualizer.findAll(xScopes, CODE._stop, midParse ? CODE.P._scope: null);
SB sb = new SB();
sb.p("digraph view_").p(N++).p(" {\n").ii();
- if( P!=null )
- sb.i().p("// POS: ").p(P.pos()).nl();
+ if( midParse )
+ sb.i().p("// POS: ").p(CODE.P.pos()).nl();
// To keep the Scopes below the graph and pointing up into the graph we
// need to group the Nodes in a subgraph cluster, and the scopes into a
@@ -101,17 +101,17 @@ public class JSViewer implements AutoCloseable {
nodes(sb, all);
// Now the scopes, in a cluster no edges
- if( P!=null )
- for( ScopeNode xscope : P._xScopes )
+ if( midParse )
+ for( ScopeNode xscope : xScopes )
if( !xscope.isDead() )
scope( sb, xscope );
// Walk the Node edges
- nodeEdges(sb, P!=null && !P._xScopes.empty() ? P._xScopes.peek() : null, all);
+ nodeEdges(sb, midParse && !xScopes.empty() ? xScopes.peek() : null, all);
// Walk the active Scope edges
- if( P!=null )
- for( ScopeNode xscope : P._xScopes )
+ if( midParse )
+ for( ScopeNode xscope : xScopes )
if( !xscope.isDead() )
scopeEdges( sb, xscope );
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/Parser.java b/chapter18/src/main/java/com/seaofnodes/simple/Parser.java
index 7301f63b..f6bf71a9 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/Parser.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/Parser.java
@@ -32,9 +32,6 @@ public class Parser {
public StopNode STOP;
- // Debugger Printing.
- public static boolean SCHEDULED; // True if debug printer can use schedule info
-
// The Lexer. Thin wrapper over a byte[] buffer with a cursor.
private final Lexer _lexer;
@@ -91,6 +88,7 @@ public class Parser {
ScopeNode _continueScope;
ScopeNode _breakScope; // Merge all the while-breaks here
+ FunNode _fun; // Current function being parsed
// Mapping from a type name to a Type. The string name matches
// `type.str()` call. No TypeMemPtrs are in here, because Simple does not
@@ -100,12 +98,16 @@ public class Parser {
// Mapping from a type name to the constructor for a Type.
public final HashMap<String, StructNode> INITS;
+ // "Linker" mapping from constant TypeFunPtrs to heads of function
+ public static final HashMap<TypeFunPtr,FunNode> LINKER = new HashMap<>();
+
+
public Parser(String source) { this(source, TypeInteger.BOT); }
public Parser(String source, TypeInteger arg) {
Node.reset();
IterPeeps.reset();
- SCHEDULED = false;
+ LINKER.clear();
_lexer = new Lexer(source);
_scope = new ScopeNode();
_continueScope = _breakScope = null;
@@ -154,25 +156,39 @@ public class Parser {
_xScopes.push(_scope);
// Enter a new scope for the initial control and arguments
_scope.push();
+ Node ctrl = new CProjNode(START, 0, ScopeNode.CTRL).peephole();
+ Node pmem = new ProjNode(START, 1, ScopeNode.MEM0).peephole();
+ Node arg = new ProjNode(START, 2, ScopeNode.ARG0).peephole();
ScopeMinNode mem = new ScopeMinNode();
mem.addDef(null);
- _scope.define(ScopeNode.CTRL, Type.CONTROL , false, new CProjNode(START, 0, ScopeNode.CTRL).peephole());
- mem.addDef( new ProjNode(START, 1, ScopeNode.MEM0).peephole());
- _scope.define(ScopeNode.MEM0, TypeMem.BOT , false, mem .peephole());
- _scope.define(ScopeNode.ARG0, TypeInteger.BOT, false, new ProjNode(START, 2, ScopeNode.ARG0).peephole());
- JSViewer.show();
+ mem.addDef(pmem);
+ _scope.define(ScopeNode.CTRL, Type.CONTROL , false, ctrl);
+ _scope.define(ScopeNode.MEM0, TypeMem.BOT , false, mem );
+ _scope.define(ScopeNode.ARG0, TypeInteger.BOT, false, arg );
+
+ // Call main
+ CallNode main = (CallNode)new CallNode(ctrl,pmem,arg,con(TypeFunPtr.MAIN)).peephole();
+ // Call End
+ CallEndNode cend = (CallEndNode)new CallEndNode(main).peephole();
+ STOP.addDef(new CProjNode(cend,0,ScopeNode.CTRL).peephole());
+ STOP.addDef(new ProjNode(cend,1,ScopeNode.MEM0).peephole());
+ STOP.addDef(new ProjNode(cend,2,ScopeNode.ARG0).peephole());
+
+ // Parse whole program, as-if function header "{ int arg -> body }"
+ ReturnNode ret = parseFunctionBody("main",TypeFunPtr.MAIN,"arg");
+ LINKER.put(TypeFunPtr.MAIN,ret.fun());
+ main.peephole();
+ if( cend.peephole() != null )
+ IterPeeps.addAll(cend._outputs);
- // Parse whole program
- parseBlock(false);
+ if( !_lexer.isEOF() ) throw _errorSyntax("unexpected");
- if( ctrl()._type==Type.CONTROL )
- STOP.addReturn(new ReturnNode(ctrl(), ZERO, _scope).peephole());
+ // Clean up and reset
_scope.kill();
_xScopes.pop();
for( StructNode init : INITS.values() )
init.unkeep().kill();
INITS.clear();
- if( !_lexer.isEOF() ) throw _errorSyntax("unexpected");
STOP.peephole();
JSViewer.show();
if( show ) showGraph();
@@ -180,6 +196,67 @@ public class Parser {
}
/**
+ * Parses a function body, assuming the header is parsed.
+ */
+ private ReturnNode parseFunctionBody(String name, TypeFunPtr sig, String... ids) {
+ sig.setName(name);
+ FunNode fun = _fun = (FunNode)peep(new FunNode(START,sig,name));
+
+ // Build a multi-exit return point for all function returns
+ RegionNode r = new RegionNode(null,null);
+ assert r.inProgress();
+ PhiNode rmem = new PhiNode(ScopeNode.MEM0,TypeMem.BOT,r,null);
+ PhiNode rrez = new PhiNode(ScopeNode.ARG0,Type.BOTTOM,r,null);
+ ReturnNode ret = new ReturnNode(r, rmem, rrez, fun);
+ assert ret.inProgress();
+ STOP.addDef(ret);
+
+ // Pre-call the function from Start, with worse-case arguments. This
+ // represents all the future, yet-to-be-parsed functions calls and
+ // external calls.
+ _scope.push();
+ ctrl(fun); // Scope control from function
+ // Private mem alias tracking per function
+ ScopeMinNode startMem = _scope.mem();
+ ScopeMinNode mem = new ScopeMinNode();
+ mem.addDef(null); // Alias#0
+ mem.addDef(new ParmNode(ScopeNode.MEM0,1,TypeMem.BOT,fun,startMem).peephole()); // All aliases
+ _scope.mem(mem);
+ // All args, "as-if" called externally
+ for( int i=0; i<ids.length; i++ ) {
+ Type t = sig.arg(i);
+ assert t==t.glb(); // Expect args declared type not be lifted
+ _scope.define(ids[i], t, false, new ParmNode(ids[i],i+2,t,fun,con(t)).peephole());
+ }
+
+ // Parse the body
+ Node last=null;
+ while (!peek('}') && !_lexer.isEOF())
+ last = parseStatement();
+
+ // Last expression is the return
+ if( ctrl()._type==Type.CONTROL )
+ STOP.addReturn(ctrl(), _scope.mem(), last, fun);
+
+ // Function scope ends
+ _scope.pop();
+ _fun = null;
+
+ // Pop off the inProgress node on the multi-exit Region merge
+ assert r.inProgress();
+ r ._inputs.pop();
+ rmem._inputs.pop();
+ rrez._inputs.pop();
+ assert !r.inProgress();
+
+ // Force peeps, which have been avoided due to inProgress
+ ret.setDef(1,rmem.peephole());
+ ret.setDef(2,rrez.peephole());
+ ret.setDef(0,r.peephole());
+ return (ReturnNode)ret.peephole();
+ }
+
+ /**
* Parses a block
*
* <pre>
@@ -191,11 +268,13 @@ public class Parser {
private Node parseBlock(boolean inCon) {
// Enter a new scope
_scope.push(inCon);
+ Node last = ZERO;
while (!peek('}') && !_lexer.isEOF())
- parseStatement();
+ last = parseStatement();
// Exit scope
+ last.keep();
_scope.pop();
- return null;
+ return last.unkeep();
}
/**
@@ -323,7 +402,7 @@ public class Parser {
// Our current scope is the body Scope
ctrl(ifT.unkeep()); // set ctrl token to ifTrue projection
_scope.addGuards(ifT,pred.unkeep(),false); // Up-cast predicate
- parseStatement(); // Parse loop body
+ Node expr = parseStatement().keep(); // Parse loop body
_scope.removeGuards(ifT);
// Merge the loop bottom into other continue statements
@@ -363,7 +442,7 @@ public class Parser {
_xScopes.pop();
_xScopes.push(exit);
_scope = exit;
- return _scope;
+ return peep(expr.unkeep());
}
private ScopeNode jumpTo(ScopeNode toScope) {
@@ -386,7 +465,7 @@ public class Parser {
private void checkLoopActive() { if (_breakScope == null) throw error("No active loop for a break or continue"); }
- private Node parseContinue() { checkLoopActive(); return (_continueScope = require(jumpTo( _continueScope ),";")); }
+ private Node parseContinue() { checkLoopActive(); _continueScope = require(jumpTo( _continueScope ),";"); return ZERO; }
private Node parseBreak () {
checkLoopActive();
// At the time of the break, and loop-exit conditions are only valid if
@@ -395,11 +474,11 @@ public class Parser {
_breakScope.removeGuards(_breakScope.ctrl());
_breakScope = require(jumpTo(_breakScope ),";");
_breakScope.addGuards(_breakScope.ctrl(), null, false);
- return _breakScope;
+ return ZERO;
}
// Look for an unbalanced `)`, skipping balanced
- private Type skipAsgn() {
+ private void skipAsgn() {
int paren=0;
while( true )
// Next X char handles skipping complex comments
@@ -407,8 +486,10 @@ public class Parser {
case Character.MAX_VALUE:
throw Utils.TODO();
case ')':
- if( --paren<0 )
- return posT(pos()-1); // Leave the `)` behind
+ if( --paren<0 ) {
+ posT( pos() - 1 );
+ return; // Leave the `)` behind
+ }
break;
case '(': paren ++; break;
default: break;
@@ -449,7 +530,7 @@ public class Parser {
// Parse the true side
ctrl(ifT.unkeep()); // set ctrl token to ifTrue projection
_scope.addGuards(ifT,pred,false); // Up-cast predicate
- Node lhs = stmt ? parseStatement() : parseAsgn().keep(); // Parse true-side
+ Node lhs = (stmt ? parseStatement() : parseAsgn()).keep(); // Parse true-side
_scope.removeGuards(ifT);
ScopeNode tScope = _scope;
@@ -461,9 +542,9 @@ public class Parser {
// remain true if the true clause exits: `if( !ptr ) return 0; return ptr.fld;`
_scope.addGuards(ifF,pred,true);
boolean doRHS = match(fside);
- Node rhs = doRHS
+ Node rhs = (doRHS
? (stmt ? parseStatement() : parseAsgn())
- : (stmt ? null : con(lhs._type.makeZero()));
+ : con(lhs._type.makeZero())).keep();
_scope.removeGuards(ifF);
if( doRHS )
fScope = _scope;
@@ -475,9 +556,8 @@ public class Parser {
// Check the trinary widening int/flt
if( !stmt ) {
- rhs = widenInt( rhs, lhs._type ).keep();
+ rhs = widenInt( rhs.unkeep(), lhs._type ).keep();
lhs = widenInt( lhs.unkeep(), rhs._type ).keep();
- rhs.unkeep();
}
// Merge results
@@ -485,7 +565,7 @@ public class Parser {
_xScopes.pop(); // Discard pushed from graph display
RegionNode r = ctrl(tScope.mergeScopes(fScope));
- Node ret = stmt ? r : peep(new PhiNode("",lhs._type.meet(rhs._type),r,lhs.unkeep(),rhs));
+ Node ret = peep(new PhiNode("",lhs._type.meet(rhs._type),r,lhs.unkeep(),rhs.unkeep()));
r.peephole();
return ret;
}
@@ -501,9 +581,9 @@ public class Parser {
*/
private Node parseReturn() {
var expr = require(parseAsgn(), ";");
- Node ret = STOP.addReturn(new ReturnNode(ctrl(), expr, _scope).peephole());
+ STOP.addReturn(ctrl(), _scope.mem(), expr, _fun);
ctrl(XCTRL); // Kill control
- return ret;
+ return expr;
}
/**
@@ -540,11 +620,11 @@ public class Parser {
throw error("Cannot reassign final '"+name+"'");
// Lift expression, based on type
- Node lift = liftExpr(expr, def.type(), def._final);
+ Node lift = liftExpr(expr.keep(), def.type(), def._final);
// Update
_scope.update(name,lift);
// Return un-lifted expr
- return expr;
+ return expr.unkeep();
}
// Make finals deep; widen ints to floats; narrow wide int types.
@@ -636,7 +716,7 @@ public class Parser {
// Define a new name,
if( !_scope.define(name,t,xfinal,lift) )
throw error("Redefining name '" + name + "'");
- return null;
+ return lift;
}
@@ -678,7 +758,7 @@ public class Parser {
require("}");
require(";");
_scope.pop();
- return null;
+ return ZERO;
}
@@ -756,21 +836,18 @@ public class Parser {
if( match("}") ) // No-arg function { -> type }
throw Utils.TODO();
Ary<Type> ts = new Ary<>(Type.class);
- ts.push(null); // Return filled in later
ts.push(t0); // First argument
while( true ) {
if( match("->") ) { // End of arguments, parse return
Type ret = type();
- if( ret==null ) return posT(old);
- ts.set(0,ret);
- if( !match("}") ) return posT(old); // Not a function
- return TypeFunPtr.make(match("?"),TypeTuple.make(ts.asAry()));
+ if( ret==null || !match("}") )
+ return posT(old); // Not a function
+ return TypeFunPtr.make(match("?"),TypeTuple.make(ts.asAry()),ret);
}
Type t1 = type();
if( t1==null ) return posT(old); // Not a function
ts.push(t1);
}
-
}
// True if a TypeFunPtr, without advancing parser
@@ -1215,8 +1292,6 @@ public class Parser {
int old = pos(); // Record lexer position
Ary<Type> ts = new Ary<>(Type.class);
Ary<String> ids = new Ary<>(String.class);
- ts .push(Type.BOTTOM); // Return type, unknown yet
- ids.push(null); // Not an argument
while( true ) {
Type t = type(); // Arg type
if( t==null ) break;
@@ -1227,13 +1302,10 @@ public class Parser {
}
require("->");
// Make a concrete function type, with a fidx
- TypeFunPtr tfp = TypeFunPtr.makeFun(TypeTuple.make(ts.asAry()));
- //FunNode fun = (FunNode)peep(new FunNode());
-
- throw Utils.TODO();
+ TypeFunPtr tfp = TypeFunPtr.makeFun(TypeTuple.make(ts.asAry()),Type.BOTTOM);
+ return parseFunctionBody(null,tfp,ids.asAry());
}
-
/**
* Parse integer literal
*
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/AndNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/AndNode.java
index 17a4e0ca..68a8079d 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/AndNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/AndNode.java
@@ -60,7 +60,7 @@ public class AndNode extends Node {
return null;
}
@Override Node copy(Node lhs, Node rhs) { return new AndNode(lhs,rhs); }
- @Override String err() {
+ @Override public String err() {
if( !(in(1)._type instanceof TypeInteger) ) return "Cannot '&' " + in(1)._type;
if( !(in(2)._type instanceof TypeInteger) ) return "Cannot '&' " + in(2)._type;
return null;
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/CFGNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/CFGNode.java
index 052ad8fc..bed4eb17 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/CFGNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/CFGNode.java
@@ -82,16 +82,16 @@ public abstract class CFGNode extends Node {
// Start is a LoopNode which contains all at depth 1.
public void buildLoopTree(StopNode stop) {
_ltree = stop._ltree = Parser.XCTRL._ltree = new LoopTree((StartNode)this);
- _bltWalk(2,stop, new BitSet());
+ _bltWalk(2,null,stop, new BitSet());
}
- private int _bltWalk(int pre, StopNode stop, BitSet post) {
+ private int _bltWalk(int pre, FunNode fun, StopNode stop, BitSet post) {
// Pre-walked?
if( _pre!=0 ) return pre;
_pre = pre++;
// Pre-walk
for( Node use : _outputs )
if( use instanceof CFGNode usecfg )
- pre = usecfg._bltWalk(pre,stop,post);
+ pre = usecfg._bltWalk(pre,use instanceof FunNode fuse ? fuse : fun,stop,post);
// Post-order work: find innermost loop
LoopTree inner = null, ltree;
@@ -109,7 +109,7 @@ public abstract class CFGNode extends Node {
if( ltree._par == null )
// This loop never had an If test choose to take its
// exit, i.e. it is a no-exit infinite loop.
- ltree._par = ltree._head.forceExit(stop)._ltree;
+ ltree._par = ltree._head.forceExit(fun,stop)._ltree;
ltree = ltree._par;
}
}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/CProjNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/CProjNode.java
index 0b9d89d2..01119310 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/CProjNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/CProjNode.java
@@ -49,7 +49,8 @@ public class CProjNode extends CFGNode implements MultiUse {
if( ctrl() instanceof IfNode iff && iff.pred().addDep(this) instanceof NotNode not )
return new CProjNode(new IfNode(iff.ctrl(),not.in(1)).peephole(),1-_idx,_idx==0 ? "False" : "True");
- return null;
+ // Copy of some other input
+ return ((MultiNode)ctrl()).pcopy(_idx);
}
@Override
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/CallEndNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/CallEndNode.java
new file mode 100644
index 00000000..31c245f9
--- /dev/null
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/CallEndNode.java
@@ -0,0 +1,79 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.IterPeeps;
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.Utils;
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeFunPtr;
+import java.util.BitSet;
+
+/**
+ * CallEnd
+ */
+public class CallEndNode extends CFGNode implements MultiNode {
+
+ // When set true, this Call/CallEnd/Fun/Return is being trivially inlined
+ private boolean _folding;
+
+ public CallEndNode(CallNode call) { super(call); }
+
+ @Override
+ public String label() { return "CallEnd"; }
+ @Override public boolean blockHead() { return true; }
+
+ CallNode call() { return (CallNode)in(0); }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ sb.append("cend( ");
+ if( call()==null ) sb.append("---, ");
+ else sb.append("Call, ");
+ for( int i=1; i<nIns()-1; i++ )
+ in(i)._print0(sb,visited).append(",");
+ sb.setLength(sb.length()-1);
+ return sb.append(")");
+ }
+
+ @Override
+ public Type compute() {
+ for( Node n : _inputs )
+ if( n!=null && n._type==Type.CONTROL )
+ return Type.CONTROL;
+ return Type.XCONTROL;
+ }
+
+ @Override
+ public Node idealize() {
+
+ // Trivial inlining: call site calls a single function; single function
+ // is only called by this call site.
+ if( !_folding && nIns()==2 ) {
+ CallNode call = call();
+ Node fptr = call.fptr();
+ if( fptr.nOuts() == 1 ) { // Only user is this call
+ ReturnNode ret = (ReturnNode)in(1);
+ FunNode fun = ret.fun();
+ assert fun.nIns()==3; // Expecting Start, and the Call
+ assert fun.in(1) instanceof StartNode && fun.in(2)==call;
+
+ // Trivial inline: rewrite
+ _folding = true;
+ // Rewrite Fun so the normal RegionNode ideal collapses
+ fun._folding = true;
+ fun.setDef(1,Parser.XCTRL); // No default/unknown StartNode caller
+ fun.setDef(2,call.ctrl()); // Bypass the Call;
+ fun.ret().setDef(3,null); // Return is folding also
+ IterPeeps.addAll(fun._outputs);
+ return this;
+ } else { // Function ptr has multiple users (so maybe multiple call sites)
+ fptr.addDep(this);
+ }
+ }
+
+ return null;
+ }
+
+ @Override public Node pcopy(int idx) {
+ return _folding ? in(1).in(idx) : null;
+ }
+}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/CallNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/CallNode.java
new file mode 100644
index 00000000..0839ad2c
--- /dev/null
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/CallNode.java
@@ -0,0 +1,96 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.Utils;
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeFunPtr;
+import java.util.BitSet;
+
+/**
+ * Call
+ */
+public class CallNode extends CFGNode {
+
+ public CallNode(Node... nodes) { super(nodes); }
+
+ @Override
+ public String label() { return "Call"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ String fname = null;
+ Node fptr = fptr();
+ if( fptr._type instanceof TypeFunPtr tfp && tfp.isConstant() )
+ fname = Parser.LINKER.get(tfp)._name;
+ if( fname==null ) fptr._print0(sb,visited);
+ else sb.append(fname);
+ sb.append("( ");
+ for( int i=2; i<nIns()-1; i++ )
+ in(i)._print0(sb,visited).append(",");
+ sb.setLength(sb.length()-1);
+ return sb.append(")");
+ }
+
+ Node ctrl() { return in(0); }
+ Node mem () { return in(1); }
+ // Args mapped 1-to-1 on inputs
+ Node arg(int idx) { return in(idx); }
+ // args from input 2 to last
+ Node fptr() { return _inputs.last(); }
+
+ // Find the Call End from the Call
+ CallEndNode cend() {
+ // Always in use slot 0
+ return (CallEndNode)out(0);
+ }
+
+ @Override
+ public Type compute() {
+ return ctrl()._type;
+ }
+
+ @Override
+ public Node idealize() {
+ // Link: call calls target function. Linking makes the target FunNode
+ // point to this Call, and all his Parms point to the call arguments;
+ // also the CallEnd points to the Return.
+ if( fptr()._type instanceof TypeFunPtr tfp ) {
+ // If fidxs is negative, then infinite unknown functions
+ long fidxs = tfp.fidxs();
+ if( fidxs > 0 ) {
+ // Walk the (63 max) bits and link
+ for( ; fidxs!=0; fidxs = TypeFunPtr.nextFIDX(fidxs) ) {
+ int fidx = Long.numberOfTrailingZeros(fidxs);
+ TypeFunPtr tfp0 = tfp.makeFrom(fidx);
+ FunNode fun = Parser.LINKER.get(tfp0);
+ if( fun!=null && !fun._folding && !linked(fun) )
+ link(fun);
+ }
+ }
+ }
+
+ return null;
+ }
+
+ // True if Fun is linked to this Call
+ boolean linked( FunNode fun ) {
+ for( Node n : fun._inputs )
+ if( n == this )
+ return true;
+ return false;
+ }
+
+
+ // Link so this calls fun
+ private void link( FunNode fun ) {
+ assert !linked(fun);
+ fun.addDef(this);
+ for( Node use : fun._outputs )
+ if( use instanceof ParmNode parm )
+ parm.addDef(arg(parm._idx));
+ // Call end points to function return
+ cend().addDef(fun.ret());
+ assert linked(fun);
+ }
+
+}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/FunNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/FunNode.java
new file mode 100644
index 00000000..9087926e
--- /dev/null
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/FunNode.java
@@ -0,0 +1,47 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Utils;
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeFunPtr;
+
+public class FunNode extends RegionNode {
+
+ // When set true, this Call/CallEnd/Fun/Return is being trivially inlined
+ boolean _folding;
+
+ TypeFunPtr _sig; // Initial signature may be missing return
+ String _name; // Debug label, optional
+
+ public FunNode( StartNode start, TypeFunPtr sig, String name ) { super(null,start); _sig=sig; _name=name; }
+
+ @Override
+ public String label() { return _name; }
+
+ ReturnNode ret() {
+ ReturnNode ret=null;
+ for( Node n : _outputs )
+ if( n instanceof ReturnNode ret0 ) {
+ assert ret==null || ret==ret0; // Found more than one return
+ ret=ret0;
+ }
+ return ret;
+ }
+
+
+ @Override
+ public Type compute() {
+ // Only dead if no callers after SCCP
+ return Type.CONTROL;
+ }
+
+ @Override
+ public Node idealize() {
+ // If no default/unknown caller, use the normal RegionNode ideal rules
+ // to collapse
+ if( inProgress() ) return null;
+ return super.idealize();
+ }
+
+ // Always in-progress until we run out of unknown callers
+ @Override public boolean inProgress() { return in(1) instanceof StartNode; }
+}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/LoopNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/LoopNode.java
index ecda9c63..33420eb9 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/LoopNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/LoopNode.java
@@ -3,6 +3,7 @@ package com.seaofnodes.simple.node;
import com.seaofnodes.simple.Parser;
import com.seaofnodes.simple.Utils;
import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeMem;
import com.seaofnodes.simple.type.TypeInteger;
import java.util.BitSet;
import java.util.HashSet;
@@ -34,7 +35,7 @@ public class LoopNode extends RegionNode {
// If this is an unreachable loop, it may not have an exit. If it does not
// (i.e., infinite loop), force an exit to make it reachable.
- public StopNode forceExit( StopNode stop ) {
+ public StopNode forceExit( FunNode fun, StopNode stop ) {
// Walk the backedge, then immediate dominator tree util we hit this
// Loop again. If we ever hit a CProj from an If (as opposed to
// directly on the If) we found our exit.
@@ -50,12 +51,28 @@ public class LoopNode extends RegionNode {
// Found a no-exit loop. Insert an exit
NeverNode iff = new NeverNode(back());
for( Node use : _outputs )
- if( use instanceof PhiNode phi )
+ if( use instanceof PhiNode )
iff.addDef(use);
CProjNode t = new CProjNode(iff,0,"True" );
CProjNode f = new CProjNode(iff,1,"False");
setDef(2,f);
- stop.addDef(new ReturnNode(t,Parser.ZERO,null));
+
+ // Now fold control into the exit
+ if( fun!=null )
+ throw Utils.TODO();
+
+ // If not in a function, look for a last-level merge point;
+ // make if not there.
+ RegionNode ctrl = stop.ctrl() instanceof RegionNode r ? r : new RegionNode(null,stop.ctrl());
+ PhiNode mem = stop.mem () instanceof PhiNode phi && phi.in(0)==ctrl ? phi : new PhiNode("",stop.mem ()._type,ctrl,stop.mem ());
+ PhiNode expr = stop.expr() instanceof PhiNode phi && phi.in(0)==ctrl ? phi : new PhiNode("",stop.expr()._type,ctrl,stop.expr());
+ if( stop.ctrl()!=ctrl ) stop.setDef(0,ctrl);
+ if( stop.mem ()!=mem ) stop.setDef(1,mem );
+ if( stop.expr()!=expr ) stop.setDef(2,expr);
+ Node top = new ConstantNode(Type.TOP).peephole();
+ ctrl.addDef(t );
+ mem .addDef(top);
+ expr.addDef(top);
return stop;
}
}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/MemOpNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/MemOpNode.java
index 62323031..96274789 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/MemOpNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/MemOpNode.java
@@ -44,7 +44,7 @@ public abstract class MemOpNode extends Node {
int hash() { return _alias; }
@Override
- String err() {
+ public String err() {
Type ptr = ptr()._type;
// Already an error, but better error messages come from elsewhere
if( ptr == Type.BOTTOM ) return null;
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/MultiNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/MultiNode.java
index ac9c0045..17d00881 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/MultiNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/MultiNode.java
@@ -1,6 +1,11 @@
package com.seaofnodes.simple.node;
-public interface MultiNode extends OutNode {
+import com.seaofnodes.simple.Ary;
+
+
+public interface MultiNode {
+
+ abstract Ary<Node> outs();
// Find a projection by index
default ProjNode proj( int idx ) {
@@ -17,4 +22,8 @@ public interface MultiNode extends OutNode {
return prj;
return null;
}
+
+ // Return not-null if this projection index is a ideal copy.
+ // Called by ProjNode ideal and used to collapse Multis.
+ default Node pcopy(int idx) { return null; }
}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/MultiUse.java b/chapter18/src/main/java/com/seaofnodes/simple/node/MultiUse.java
index e4548dfd..436d3432 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/MultiUse.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/MultiUse.java
@@ -1,3 +1,5 @@
package com.seaofnodes.simple.node;
-public interface MultiUse { abstract int idx(); }
+public interface MultiUse {
+ abstract int idx();
+}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/Node.java b/chapter18/src/main/java/com/seaofnodes/simple/node/Node.java
index 1a595f49..6369fb01 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/Node.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/Node.java
@@ -11,7 +11,7 @@ import java.util.function.Function;
* The Node class provides common functionality used by all subtypes.
* Subtypes of Node specialize by overriding methods.
*/
-public abstract class Node implements OutNode {
+public abstract class Node {
/**
* Each node has a unique dense Node ID within a compilation context
@@ -125,7 +125,7 @@ public abstract class Node implements OutNode {
*/
public Node in(int i) { return _inputs.get(i); }
public Node out(int i) { return _outputs.get(i); }
- @Override public Ary<Node> outs() { return _outputs; }
+ public final Ary<Node> outs() { return _outputs; }
public int nIns() { return _inputs.size(); }
@@ -162,9 +162,11 @@ public abstract class Node implements OutNode {
new_def.addUse(this);
// Set the new_def over the old (killed) edge
_inputs.set(idx,new_def);
- if( old_def != null && // If the old def exists, remove a def->use edge
- old_def.delUse(this) ) // If we removed the last use, the old def is now dead
- old_def.kill(); // Kill old def
+ if( old_def != null ) { // If the old def exists, remove a def->use edge
+ if( old_def.delUse(this) ) // If we removed the last use, the old def is now dead
+ old_def.kill(); // Kill old def
+ else IterPeeps.add(old_def);// Else old lost a use, so onto worklist
+ }
moveDepsToWorklist();
// Return new_def for easy flow-coding
return new_def;
@@ -609,7 +611,7 @@ public abstract class Node implements OutNode {
Node copy(Node lhs, Node rhs) { throw Utils.TODO("Binary ops need to implement copy"); }
// Report any post-optimize errors
- String err() { return null; }
+ public String err() { return null; }
/**
* Used to allow repeating tests in the same JVM. This just resets the
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/OrNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/OrNode.java
index b572346f..bd771198 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/OrNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/OrNode.java
@@ -57,7 +57,7 @@ public class OrNode extends Node {
return null;
}
@Override Node copy(Node lhs, Node rhs) { return new OrNode(lhs,rhs); }
- @Override String err() {
+ @Override public String err() {
if( !(in(1)._type instanceof TypeInteger) ) return "Cannot '|' " + in(1)._type;
if( !(in(2)._type instanceof TypeInteger) ) return "Cannot '|' " + in(2)._type;
return null;
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/OutNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/OutNode.java
deleted file mode 100644
index dbac136b..00000000
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/OutNode.java
+++ /dev/null
@@ -1,7 +0,0 @@
-package com.seaofnodes.simple.node;
-
-import com.seaofnodes.simple.Ary;
-
-public interface OutNode {
- Ary<Node> outs();
-}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/ParmNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/ParmNode.java
new file mode 100644
index 00000000..136494b5
--- /dev/null
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/ParmNode.java
@@ -0,0 +1,47 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Utils;
+import com.seaofnodes.simple.type.Type;
+import java.util.BitSet;
+
+public class ParmNode extends PhiNode {
+
+ // Argument indices are mapped one-to-one on CallNode inputs
+ final int _idx; // Argument index
+
+ public ParmNode(String label, int idx, Type declaredType, Node... inputs) {
+ super(label,declaredType,inputs);
+ _idx = idx;
+ }
+
+ @Override public String label() { return MemOpNode.mlabel(_label); }
+
+ @Override public String glabel() { return _label; }
+
+ FunNode fun() { return (FunNode)in(0); }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ if( fun()._name.equals("main") && _label.equals("arg") )
+ return sb.append("arg");
+ sb.append(_label).append("(");
+ for( Node in : _inputs ) {
+ if (in == null) sb.append("____");
+ else in._print0(sb, visited);
+ sb.append(",");
+ }
+ sb.setLength(sb.length()-1);
+ sb.append(")");
+ return sb;
+ }
+
+
+ @Override
+ public Node idealize() {
+ if( inProgress() ) return null;
+ return super.idealize();
+ }
+
+ // Always in-progress until we run out of unknown callers
+ @Override public boolean inProgress() { return fun().inProgress(); }
+}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/PhiNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/PhiNode.java
index c0557639..6dd4f57d 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/PhiNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/PhiNode.java
@@ -155,7 +155,7 @@ public class PhiNode extends Node {
}
// True if last input is null
- public final boolean inProgress() {
+ public boolean inProgress() {
return in(nIns()-1) == null;
}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/ProjNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/ProjNode.java
index 525b5df0..9519f5ec 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/ProjNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/ProjNode.java
@@ -26,7 +26,9 @@ public class ProjNode extends Node implements MultiUse {
@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) { return sb.append(_label); }
- @Override public CFGNode cfg0() { return in(0).cfg0(); }
+ @Override public CFGNode cfg0() {
+ return in(0) instanceof CFGNode cfg ? cfg : in(0).cfg0();
+ }
@Override public boolean isMultiTail() { return in(0).isMultiHead(); }
@Override public boolean isMem() { return _type instanceof TypeMem; }
@@ -38,7 +40,7 @@ public class ProjNode extends Node implements MultiUse {
return t instanceof TypeTuple tt ? tt._types[_idx] : Type.BOTTOM;
}
- @Override public Node idealize() { return null; }
+ @Override public Node idealize() { return ((MultiNode)in(0)).pcopy(_idx); }
@Override
boolean eq( Node n ) { return _idx == ((ProjNode)n)._idx; }
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/RegionNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/RegionNode.java
index fb39af17..0f9fce7a 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/RegionNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/RegionNode.java
@@ -109,7 +109,7 @@ public class RegionNode extends CFGNode {
}
// True if last input is null
- public final boolean inProgress() {
+ public boolean inProgress() {
return nIns()>1 && in(nIns()-1) == null;
}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/ReturnNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
index b586a44f..b305a96e 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
@@ -1,5 +1,6 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.simple.Parser;
import com.seaofnodes.simple.type.*;
import java.util.BitSet;
@@ -15,16 +16,14 @@ import java.util.BitSet;
*/
public class ReturnNode extends CFGNode {
- public ReturnNode(Node ctrl, Node data, ScopeNode scope) {
- // Add memory slices to Return, so all memory updates are live-on-exit.
- super(ctrl, data);
- if( scope!=null )
- for( int i=2; i<scope.mem().nIns(); i++ )
- addDef(scope.mem().in(i));
+ public ReturnNode(Node ctrl, Node mem, Node data, FunNode fun) {
+ super(ctrl, mem, data, fun);
}
public Node ctrl() { return in(0); }
- public Node expr() { return in(1); }
+ public Node mem () { return in(1); }
+ public Node expr() { return in(2); }
+ public FunNode fun() { return (FunNode)in(3); }
@Override
public String label() { return "Return"; }
@@ -32,19 +31,41 @@ public class ReturnNode extends CFGNode {
@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
sb.append("return ");
+ if( expr()==null ) return sb.append("INPROGRESS");
expr()._print0(sb, visited);
return sb.append(";");
}
@Override
public Type compute() {
- return TypeTuple.make(ctrl()._type,expr()._type);
+ if( inProgress() ) return TypeTuple.RET; // In progress
+ return TypeTuple.make(ctrl()._type,mem()._type,expr()._type);
}
- @Override
- public Node idealize() {
- if( ctrl()._type==Type.XCONTROL )
- return ctrl();
- return null;
+ @Override public Node idealize() { return null; }
+
+ public boolean inProgress() {
+ return ctrl() instanceof RegionNode r && r.inProgress();
+ }
+
+ // Add a return exit to the current parsing function
+ void addReturn(Node ctrl, Node rmem, Node expr) {
+ assert inProgress();
+ RegionNode r = (RegionNode)ctrl();
+ // Assert that the Phis are in particular outputs; not reordered or shuffled
+ PhiNode mem = (PhiNode)r.out(0); assert mem._declaredType == TypeMem.BOT;
+ PhiNode rez = (PhiNode)r.out(1); assert rez._declaredType == Type.BOTTOM;
+ // Pop "inProgress" null off
+ r ._inputs.pop();
+ mem._inputs.pop();
+ rez._inputs.pop();
+ // Add new return point
+ r .addDef(ctrl);
+ mem.addDef(rmem);
+ rez.addDef(expr);
+ // Back to being inProgress
+ r .addDef(null);
+ mem.addDef(null);
+ rez.addDef(null);
}
}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/SarNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/SarNode.java
index 4270ef88..a3d1045f 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/SarNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/SarNode.java
@@ -52,7 +52,7 @@ public class SarNode extends Node {
return null;
}
@Override Node copy(Node lhs, Node rhs) { return new SarNode(lhs,rhs); }
- @Override String err() {
+ @Override public String err() {
if( !(in(1)._type instanceof TypeInteger) ) return "Cannot '>>' " + in(1)._type;
if( !(in(2)._type instanceof TypeInteger) ) return "Cannot '>>' " + in(2)._type;
return null;
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/ScopeMinNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/ScopeMinNode.java
index e9a4eb82..f5a33890 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/ScopeMinNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/ScopeMinNode.java
@@ -42,7 +42,7 @@ public class ScopeMinNode extends Node {
public ScopeMinNode() { _type = TypeMem.BOT; }
- @Override public String label() { return "$mem"; }
+ @Override public String label() { return "ALLMEM"; }
@Override public boolean isMem() { return true; }
@Override
@@ -66,7 +66,15 @@ public class ScopeMinNode extends Node {
@Override public Type compute() { return TypeMem.BOT; }
- @Override public Node idealize() { return null; }
+ @Override public Node idealize() {
+ if( inProgress() ) return null;
+
+ // If not merging any memory (all memory is just the default)
+ if( nIns()==2 )
+ return in(1); // Become default memory
+
+ return null;
+ }
public Node in( Var v ) { return in(v._idx); }
@@ -79,7 +87,6 @@ public class ScopeMinNode extends Node {
return setDef(alias,st);
}
-
// Read or update from memory.
// A shared implementation allows us to create lazy phis both during
// lookups and updates; the lazy phi creation is part of chapter 8.
@@ -148,4 +155,16 @@ public class ScopeMinNode extends Node {
}
}
+ // If being used by a Scope, this is "in progress" from the Parser.
+ // Once no more scope users, this is just a memory-merge.
+ boolean inProgress() {
+ for( Node n : _outputs )
+ if( n instanceof ScopeNode scope )
+ return true;
+ return false;
+ }
+
+ @Override boolean eq( Node n ) {
+ return this==n || !inProgress();
+ }
}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/ScopeNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/ScopeNode.java
index e720a58f..8685e66b 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/ScopeNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/ScopeNode.java
@@ -81,6 +81,7 @@ public class ScopeNode extends ScopeMinNode {
* @return Node that was bound
*/
public <N extends Node> N ctrl(N n) { return setDef(0,n); }
+ public Node mem(Node n) { return setDef(1,n); }
public void push() { push(false); }
public void push(boolean inCon) {
@@ -350,7 +351,9 @@ public class ScopeNode extends ScopeMinNode {
if( !(n instanceof CFGNode) )
n.unkill();
_guards.clear();
- super.kill();
+ // Can have lazy uses remaining
+ if( isUnused() )
+ super.kill();
}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/ShlNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/ShlNode.java
index d3639ed7..e454362a 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/ShlNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/ShlNode.java
@@ -56,7 +56,7 @@ public class ShlNode extends Node {
return null;
}
@Override Node copy(Node lhs, Node rhs) { return new ShlNode(lhs,rhs); }
- @Override String err() {
+ @Override public String err() {
if( !(in(1)._type instanceof TypeInteger) ) return "Cannot '<<' " + in(1)._type;
if( !(in(2)._type instanceof TypeInteger) ) return "Cannot '<<' " + in(2)._type;
return null;
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/StopNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/StopNode.java
index 269c2f6c..4b91f1eb 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/StopNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/StopNode.java
@@ -1,11 +1,7 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.simple.Utils;
import com.seaofnodes.simple.type.Type;
-import com.seaofnodes.simple.Parser;
-import com.seaofnodes.simple.IterPeeps;
-import com.seaofnodes.simple.GlobalCodeMotion;
-import com.seaofnodes.simple.GraphVisualizer;
-
import java.util.BitSet;
public class StopNode extends CFGNode {
@@ -25,7 +21,9 @@ public class StopNode extends CFGNode {
@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
- if( ret()!=null ) return ret()._print0(sb, visited);
+ // For the sake of many old tests, and single value prints as "return val"
+ if( nIns() == 3 ) return expr()._print0(sb.append("return "),visited).append(";");
+ if( ret()!=null ) return ret()._print0(sb,visited);
sb.append("Stop[ ");
for( Node ret : _inputs )
ret._print0(sb, visited).append(" ");
@@ -34,10 +32,15 @@ public class StopNode extends CFGNode {
@Override public boolean blockHead() { return true; }
+
+ public CFGNode ctrl() { return (CFGNode)in(0); }
+ public Node mem () { return in(1); }
+ public Node expr() { return in(2); }
+
// If a single Return, return it.
// Otherwise, null because ambiguous.
public ReturnNode ret() {
- return nIns()==1 ? (ReturnNode)in(0) : null;
+ return nIns()==4 && in(3) instanceof ReturnNode ret ? ret : null;
}
@Override
@@ -48,8 +51,8 @@ public class StopNode extends CFGNode {
@Override
public Node idealize() {
int len = nIns();
- for( int i=0; i<nIns(); i++ )
- if( in(i)._type==Type.XCONTROL )
+ for( int i=3; i<nIns(); i++ )
+ if( ((ReturnNode)in(i)).fun()==null )
delDef(i--);
if( len != nIns() ) return this;
return null;
@@ -59,28 +62,16 @@ public class StopNode extends CFGNode {
if( _idepth!=0 ) return _idepth;
int d=0;
for( Node n : _inputs )
- if( n!=null )
- d = Math.max(d,((CFGNode)n).idepth()+1);
+ if( n instanceof CFGNode cfg )
+ d = Math.max(d,cfg.idepth()+1);
return _idepth=d;
}
- @Override public CFGNode idom(Node dep) { return null; }
- public Node addReturn(Node node) {
- return addDef(node);
- }
-
- public StopNode iterate( ) { return IterPeeps.iterate(this).typeCheck().GCM(false); }
- public StopNode iterate(boolean show) { return IterPeeps.iterate(this).typeCheck().GCM(show ); }
- StopNode typeCheck() {
- String err = walk( Node::err );
- if( err != null ) throw new RuntimeException(err);
- return this;
- }
- StopNode GCM(boolean show) {
- Parser.START.buildLoopTree(this);
- if( show )
- System.out.println(new GraphVisualizer().generateDotOutput(this,null,null));
- GlobalCodeMotion.buildCFG(this);
- return this;
+ // Add a new function exit point. Validates Parsers function borders, then
+ // hands off to the guaranteed return.
+ public void addReturn(Node ctrl, Node mem, Node rez, FunNode fun) {
+ ReturnNode ret = (ReturnNode)_inputs.last();
+ assert ret.fun()==fun && ret.inProgress();
+ ret.addReturn(ctrl,mem,rez);
}
}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/StoreNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/StoreNode.java
index 5bc88aa5..39a2dc2c 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/StoreNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/StoreNode.java
@@ -111,7 +111,7 @@ public class StoreNode extends MemOpNode {
}
@Override
- String err() {
+ public String err() {
String err = super.err();
if( err != null ) return err;
TypeMemPtr tmp = (TypeMemPtr)ptr()._type;
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/XorNode.java b/chapter18/src/main/java/com/seaofnodes/simple/node/XorNode.java
index 191933f9..c140ba83 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/XorNode.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/XorNode.java
@@ -57,7 +57,7 @@ public class XorNode extends Node {
return null;
}
@Override Node copy(Node lhs, Node rhs) { return new XorNode(lhs,rhs); }
- @Override String err() {
+ @Override public String err() {
if( !(in(1)._type instanceof TypeInteger) ) return "Cannot '^' " + in(1)._type;
if( !(in(2)._type instanceof TypeInteger) ) return "Cannot '^' " + in(2)._type;
return null;
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/type/TypeFunPtr.java b/chapter18/src/main/java/com/seaofnodes/simple/type/TypeFunPtr.java
index f1a16de4..6acd6798 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/type/TypeFunPtr.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/type/TypeFunPtr.java
@@ -20,21 +20,25 @@ import java.util.HashMap;
public class TypeFunPtr extends TypeNil {
// A TypeFunPtr is Signature and a set of functions.
public final TypeTuple _sig;
+ public final Type _ret;
// Cheesy easy implementation for a small set; 1 bit per unique function
// within the TypeTuple. Can be upgraded to a BitSet for larger classes of
- // functions.
- private final long _fidxs; // 64 unique functions per signature
+ // functions. Negative means "these 63 concrete bits plus infinite unknown more"
+ private final long _fidxs; // 63 unique functions per signature
+ public String _name; // Optional debug function name; only for named single function
- private TypeFunPtr(byte nil, TypeTuple sig, long fidxs) {
+ private TypeFunPtr(byte nil, TypeTuple sig, Type ret, long fidxs) {
super(TFUNPTR,nil);
assert sig != null;
_sig = sig;
+ _ret = ret;
_fidxs = fidxs;
}
- static TypeFunPtr make( byte nil, TypeTuple sig, long fidxs ) { return new TypeFunPtr(nil,sig,fidxs).intern(); }
- public static TypeFunPtr make( boolean nil, TypeTuple sig ) { return make((byte)(nil ? 3 : 2),sig,-1); }
- @Override TypeFunPtr makeFrom( byte nil ) { return nil==_nil ? this : make(nil,_sig,_fidxs); }
+ static TypeFunPtr make( byte nil, TypeTuple sig, Type ret, long fidxs ) { return new TypeFunPtr(nil,sig,ret,fidxs).intern(); }
+ public static TypeFunPtr make( boolean nil, TypeTuple sig, Type ret ) { return make((byte)(nil ? 3 : 2),sig,ret,-1); }
+ @Override TypeFunPtr makeFrom( byte nil ) { return nil==_nil ? this : make(nil,_sig,_ret,_fidxs); }
+ public TypeFunPtr makeFrom( int fidx ) { return make(_nil,_sig,_ret,1L<<fidx); }
// Compute "function indices": FIDX
private static final HashMap<TypeTuple,Integer> FIDXS = new HashMap<>();
@@ -45,42 +49,51 @@ public class TypeFunPtr extends TypeNil {
assert fidx<64; // TODO: need a larger FIDX space
return fidx; // Return
}
- public static TypeFunPtr makeFun( TypeTuple sig ) {
- return make((byte)2,sig,1L<<nextFIDX(sig));
+ public static TypeFunPtr makeFun( TypeTuple sig, Type ret ) {
+ return make((byte)2,sig,ret,1L<<nextFIDX(sig));
}
- public static TypeFunPtr BOT = make((byte)3,TypeTuple.SIGBOT,-1);
- public static TypeFunPtr TEST = make((byte)2,TypeTuple.SIGTEST,1);
- public static TypeFunPtr TEST0 = make((byte)3,TypeTuple.SIGTEST,3);
- public static void gather(ArrayList<Type> ts) { ts.add(TEST); ts.add(TEST0); ts.add(BOT); }
+ public static TypeFunPtr BOT = make((byte)3,TypeTuple.BOT,Type.BOTTOM,-1);
+ public static TypeFunPtr TEST = make((byte)2,TypeTuple.TEST,TypeInteger.BOT,1);
+ public static TypeFunPtr TEST0 = make((byte)3,TypeTuple.TEST,TypeInteger.BOT,3);
+ public static TypeFunPtr MAIN = makeFun(TypeTuple.MAIN,Type.BOTTOM);
+ public static void gather(ArrayList<Type> ts) { ts.add(TEST); ts.add(TEST0); ts.add(BOT); ts.add(MAIN); }
@Override
Type xmeet(Type t) {
TypeFunPtr that = (TypeFunPtr) t;
- return TypeFunPtr.make(xmeet0(that),(TypeTuple)_sig.meet(that._sig), _fidxs | that._fidxs);
+ return TypeFunPtr.make(xmeet0(that),(TypeTuple)_sig.meet(that._sig), _ret.meet(that._ret),_fidxs | that._fidxs);
}
@Override
- public TypeFunPtr dual() { return TypeFunPtr.make(dual0(), _sig.dual(), ~_fidxs); }
+ public TypeFunPtr dual() { return TypeFunPtr.make(dual0(), _sig.dual(), _ret.dual(), ~_fidxs); }
// RHS is NIL
- @Override public Type meet0() { return _nil==3 ? this : make((byte)3,_sig,_fidxs); }
+ @Override public Type meet0() { return _nil==3 ? this : make((byte)3,_sig,_ret,_fidxs); }
@Override public TypeFunPtr glb() { throw Utils.TODO(); }
- @Override public boolean isConstant() { return Long.bitCount(_fidxs)==1; }
+ @Override public boolean isConstant() { return _nil==2 && Long.bitCount(_fidxs)==1; }
@Override public int log_size() { return 2; } // (1<<2)==4-byte pointers
+ public Type arg(int i) { return _sig._types[i]; }
+ public long fidxs() { return _fidxs; }
+
@Override
- int hash() { return Utils.fold(_sig.hashCode() ^ _fidxs ^ super.hash()); }
+ int hash() { return Utils.fold(_sig.hashCode() ^ _ret.hashCode() ^ _fidxs ^ super.hash()); }
@Override
boolean eq(Type t) {
TypeFunPtr ptr = (TypeFunPtr)t; // Invariant
- return _sig == ptr._sig && _fidxs == ptr._fidxs && super.eq(ptr);
+ return _sig == ptr._sig && _ret == ptr._ret && _fidxs == ptr._fidxs && super.eq(ptr);
}
+ public void setName(String name) {
+ assert _name==null || _name.equals(name);
+ assert _fidxs > 0 && Long.bitCount(_fidxs) == 1;
+ _name = name;
+ }
@Override public String str() { return print(new SB()).toString(); }
@Override public SB print(SB sb) { return _print(sb,false); }
@@ -88,15 +101,22 @@ public class TypeFunPtr extends TypeNil {
private static SB _print(SB sb, boolean g, Type t) { return g ? t.gprint(sb) : t.print(sb); }
private SB _print(SB sb, boolean g) {
sb.p(x()).p("{ ");
- for( int i=1; i<_sig._types.length; i++ )
- _print(sb,g,_sig._types[i]).p(" ");
- _print(sb.p("-> "),g,_sig._types[0]).p(" #");
- long fidxs = isHigh() ? ~_fidxs : _fidxs;
- String fidx = fidxs==0 ? ""
- : Long.bitCount(fidxs) == 1 ? ""+Long.numberOfTrailingZeros(fidxs)
- : fidxs == -1 ? "ALL"
- : "b"+Long.toBinaryString(fidxs); // Just some function bits
- return sb.p(fidx).p("}").p(q());
+ if( _name!=null ) sb.p(_name);
+ else {
+ for( Type t : _sig._types )
+ _print(sb,g,t).p(" ");
+ _print(sb.p("-> "),g,_ret).p(" #");
+ long fidxs = isHigh() ? ~_fidxs : _fidxs;
+ String fidx = fidxs==0 ? ""
+ : Long.bitCount(fidxs) == 1 ? ""+Long.numberOfTrailingZeros(fidxs)
+ : fidxs == -1 ? "ALL"
+ : "b"+Long.toBinaryString(fidxs); // Just some function bits
+ sb.p(fidx);
+ }
+ return sb.p("}").p(q());
}
+ // Usage: for( long fidxs=fidxs(); fidxs!=0; fidxs=nextFIDX(fidxs) { int fidxs = Long.numberOfTrailingZeros(fidxs); ... }
+ public static long nextFIDX(long fidxs) { return fidxs & (fidxs-1); }
+
}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/type/TypeTuple.java b/chapter18/src/main/java/com/seaofnodes/simple/type/TypeTuple.java
index be4c18b1..6d3a2b69 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/type/TypeTuple.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/type/TypeTuple.java
@@ -10,20 +10,28 @@ public class TypeTuple extends Type {
private TypeTuple(Type[] types) { super(TTUPLE); _types = types; }
public static TypeTuple make(Type... types) { return new TypeTuple(types).intern(); }
- private static final TypeTuple TEST = make(TypeInteger.BOT,TypeMemPtr.TEST);
- public static final TypeTuple START = make(Type.CONTROL,TypeMem.TOP,TypeInteger.BOT);
- public static final TypeTuple SIGTEST = make(TypeInteger.BOT,TypeInteger.BOT);
- public static final TypeTuple SIGBOT = make(Type.BOTTOM);
- public static final TypeTuple SIGTOP = SIGBOT.dual();
- public static void gather(ArrayList<Type> ts) { ts.add(TEST); ts.add(START); ts.add(SIGTEST); }
+ public static final TypeTuple BOT = new TypeTuple(new Type[0]).intern();
+ public static final TypeTuple TOP = new TypeTuple(null).intern();
+
+ public static final TypeTuple TEST = make(TypeInteger.BOT,TypeMemPtr.TEST);
+ public static final TypeTuple START= make(Type.CONTROL,TypeMem.TOP,TypeInteger.BOT);
+ public static final TypeTuple MAIN = make(TypeInteger.BOT);
+ public static final TypeTuple RET = make(Type.CONTROL,TypeMem.BOT,Type.BOTTOM);
+
+ public static final TypeTuple IF_BOTH = make(new Type[]{Type. CONTROL,Type. CONTROL});
+ public static final TypeTuple IF_NEITHER = make(new Type[]{Type.XCONTROL,Type.XCONTROL});
+ public static final TypeTuple IF_TRUE = make(new Type[]{Type. CONTROL,Type.XCONTROL});
+ public static final TypeTuple IF_FALSE = make(new Type[]{Type.XCONTROL,Type. CONTROL});
+
+ public static void gather(ArrayList<Type> ts) { ts.add(BOT); ts.add(TEST); ts.add(START); ts.add(MAIN); ts.add(IF_TRUE); }
@Override
Type xmeet(Type other) {
TypeTuple tt = (TypeTuple)other; // contract from xmeet
- if( this==SIGTOP ) return other;
- if( tt ==SIGTOP ) return this ;
+ if( this==TOP ) return other;
+ if( tt ==TOP ) return this ;
if( _types.length != tt._types.length )
- return SIGBOT;
+ return BOT;
Type[] ts = new Type[_types.length];
for( int i=0; i<_types.length; i++ )
ts[i] = _types[i].meet(tt._types[i]);
@@ -31,6 +39,8 @@ public class TypeTuple extends Type {
}
@Override public TypeTuple dual() {
+ if( this==TOP ) return BOT;
+ if( this==BOT ) return TOP;
Type[] ts = new Type[_types.length];
for( int i=0; i<_types.length; i++ )
ts[i] = _types[i].dual();
@@ -45,41 +55,37 @@ public class TypeTuple extends Type {
return make(ts);
}
+ @Override public String str() { return print(new SB()).toString(); }
+
@Override public SB print(SB sb) {
+ if( this==TOP ) return sb.p("[TOP]");
+ if( this==BOT ) return sb.p("[BOT]");
sb.p("[ ");
for( Type t : _types )
t.print(sb).p(", ");
return sb.unchar(2).p("]");
}
@Override public SB gprint(SB sb) {
+ if( this==TOP ) return sb.p("[TOP]");
+ if( this==BOT ) return sb.p("[BOT]");
sb.p("[ ");
for( Type t : _types )
t.gprint(sb).p(", ");
return sb.unchar(2).p("]");
}
- @Override public String str() {
- SB sb = new SB().p("[ ");
- for( Type t : _types )
- sb.p(t.str()).p(", ");
- return sb.unchar(2).p("]").toString();
- }
-
- public static final TypeTuple IF_BOTH = make(new Type[]{Type. CONTROL,Type. CONTROL});
- public static final TypeTuple IF_NEITHER = make(new Type[]{Type.XCONTROL,Type.XCONTROL});
- public static final TypeTuple IF_TRUE = make(new Type[]{Type. CONTROL,Type.XCONTROL});
- public static final TypeTuple IF_FALSE = make(new Type[]{Type.XCONTROL,Type. CONTROL});
-
@Override
int hash() {
int sum = 0;
- for( Type type : _types ) sum ^= type.hashCode();
+ if( _types!=null ) for( Type type : _types ) sum ^= type.hashCode();
return sum;
}
@Override
boolean eq( Type t ) {
TypeTuple tt = (TypeTuple)t; // Contract
+ if( _types==null && tt._types==null ) return true;
+ if( _types==null || tt._types==null ) return false;
if( _types.length != tt._types.length ) return false;
for( int i=0; i<_types.length; i++ )
if( _types[i]!=tt._types[i] )
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/BrainFuckTest.java b/chapter18/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
index b1ca7da9..f21b9afa 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/BrainFuckTest.java
@@ -21,7 +21,7 @@ public class BrainFuckTest {
encoded.append("program[").append(i).append("] = ").append(value).append(";");
}
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
encoded + """
int d = 0;
@@ -67,7 +67,7 @@ for( int pc = 0; pc < program#; pc++ ) {
}
return output;
""");
- StopNode stop = parser.parse(false).iterate(false);
- assertEquals("Hello World!\n", Evaluator.evaluate(stop, 0, 10000).toString());
+ code.parse().opto().typeCheck().codegen();
+ assertEquals("Hello World!\n", Evaluator.evaluate(code._stop, 0, 10000).toString());
}
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter01Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter01Test.java
index 44f1af0d..7f111039 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter01Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter01Test.java
@@ -10,15 +10,10 @@ public class Chapter01Test {
@Test
public void testSimpleProgram() {
- Parser parser = new Parser("return 1;");
- StopNode stop = parser.parse();
- StartNode start = Parser.START;
- ReturnNode ret = (ReturnNode)stop.in(0);
-
- assertTrue(ret.ctrl() instanceof CProjNode);
- Node expr = ret.expr();
+ CodeGen code = new CodeGen("return 1;").parse().opto();
+ Node expr = code._stop.expr();
if( expr instanceof ConstantNode con ) {
- assertEquals(start,con.in(0));
+ assertEquals(code._start,con.in(0));
assertEquals(TypeInteger.constant(1), con._type);
} else {
fail();
@@ -27,10 +22,9 @@ public class Chapter01Test {
@Test
public void testZero() {
- Parser parser = new Parser("return 0;");
- parser.parse();
- StartNode start = Parser.START;
- for( Node use : start._outputs )
+ CodeGen code = new CodeGen("return 0;");
+ code.parse();
+ for( Node use : code._start._outputs )
if( use instanceof ConstantNode con && con._type instanceof TypeInteger )
assertEquals(TypeInteger.constant(0),con._type);
}
@@ -38,7 +32,7 @@ public class Chapter01Test {
@Test
public void testBad1() {
try {
- new Parser("ret").parse();
+ new CodeGen("ret").parse();
fail();
} catch( RuntimeException e ) {
assertEquals("Undefined name 'ret'",e.getMessage());
@@ -48,7 +42,7 @@ public class Chapter01Test {
@Test
public void testBad2() {
try {
- new Parser("return 0123;").parse();
+ new CodeGen("return 0123;").parse();
fail();
} catch( RuntimeException e ) {
assertEquals("Syntax error: integer values cannot start with '0'",e.getMessage());
@@ -58,13 +52,13 @@ public class Chapter01Test {
@Test
public void testNotBad3() {
// this test used to fail in chapter 1
- assertEquals("return 12;", new Parser("return - -12;").parse().print());
+ assertEquals("return 12;", new CodeGen("return - -12;").parse().opto()._stop.print());
}
@Test
public void testBad4() {
try {
- new Parser("return 100").parse();
+ new CodeGen("return 100").parse();
fail();
} catch( RuntimeException e ) {
assertEquals("Syntax error, expected ;: ",e.getMessage());
@@ -74,13 +68,13 @@ public class Chapter01Test {
@Test
public void testNotBad5() {
// this test used to fail in chapter 1
- assertEquals("return -100;", new Parser("return -100;").parse().print());
+ assertEquals("return -100;", new CodeGen("return -100;").parse().opto()._stop.print());
}
@Test
public void testBad6() {
try {
- new Parser("return100").parse();
+ new CodeGen("return100").parse();
fail();
} catch( RuntimeException e ) {
assertEquals("Undefined name 'return100'",e.getMessage());
@@ -90,7 +84,7 @@ public class Chapter01Test {
@Test
public void testBad7() {
try {
- new Parser("return 1;}").parse();
+ new CodeGen("return 1;}").parse();
fail();
} catch( RuntimeException e ) {
assertEquals("Syntax error, unexpected: }",e.getMessage());
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter02Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter02Test.java
index 75ee0b70..c85bec6f 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter02Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter02Test.java
@@ -10,53 +10,51 @@ public class Chapter02Test {
@Test
public void testParseGrammar() {
- Parser parser = new Parser("return 1+2*3+-5;");
- Node._disablePeephole = true; // disable peephole so we can observe full graph
- StopNode ret = parser.parse();
- assertEquals("return ((1+(2*3))+(-5));", ret.print());
- Node._disablePeephole = false;
+ CodeGen code = new CodeGen("return 1+2*3+-5;");
+ code.parse(true); // Disable peepholes
+ assertEquals("return Phi(Region,((1+(2*3))+(-5)));", code._stop.ret().print());
}
@Test
public void testAddPeephole() {
- Parser parser = new Parser("return 1+2;");
- StopNode ret = parser.parse();
- assertEquals("return 3;", ret.print());
+ CodeGen code = new CodeGen("return 1+2;");
+ code.parse().opto();
+ assertEquals("return 3;", code._stop.print());
}
@Test
public void testSubPeephole() {
- Parser parser = new Parser("return 1-2;");
- StopNode ret = parser.parse();
- assertEquals("return -1;", ret.print());
+ CodeGen code = new CodeGen("return 1-2;");
+ code.parse().opto();
+ assertEquals("return -1;", code._stop.print());
}
@Test
public void testMulPeephole() {
- Parser parser = new Parser("return 2*3;");
- StopNode ret = parser.parse();
- assertEquals("return 6;", ret.print());
+ CodeGen code = new CodeGen("return 2*3;");
+ code.parse().opto();
+ assertEquals("return 6;", code._stop.print());
}
@Test
public void testDivPeephole() {
- Parser parser = new Parser("return 6/3;");
- StopNode ret = parser.parse();
- assertEquals("return 2;", ret.print());
+ CodeGen code = new CodeGen("return 6/3;");
+ code.parse().opto();
+ assertEquals("return 2;", code._stop.print());
}
@Test
public void testMinusPeephole() {
- Parser parser = new Parser("return 6/-3;");
- StopNode ret = parser.parse();
- assertEquals("return -2;", ret.print());
+ CodeGen code = new CodeGen("return 6/-3;");
+ code.parse().opto();
+ assertEquals("return -2;", code._stop.print());
}
@Test
public void testExample() {
- Parser parser = new Parser("return 1+2*3+-5;");
- StopNode ret = parser.parse();
- assertEquals("return 2;", ret.print());
+ CodeGen code = new CodeGen("return 1+2*3+-5;");
+ code.parse().opto();
+ assertEquals("return 2;", code._stop.print());
}
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter03Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter03Test.java
index 62304b38..9a97d5b8 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter03Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter03Test.java
@@ -1,6 +1,5 @@
package com.seaofnodes.simple;
-import com.seaofnodes.simple.node.*;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -9,45 +8,42 @@ public class Chapter03Test {
@Test
public void testVarDecl() {
- Parser parser = new Parser("int a=1; return a;");
- StopNode ret = parser.parse();
- assertEquals("return 1;", ret.print());
+ CodeGen code = new CodeGen("int a=1; return a;");
+ code.parse().opto(); assertEquals("return 1;", code._stop.print());
}
@Test
public void testVarAdd() {
- Parser parser = new Parser("int a=1; int b=2; return a+b;");
- StopNode ret = parser.parse();
- assertEquals("return 3;", ret.print());
+ CodeGen code = new CodeGen("int a=1; int b=2; return a+b;");
+ code.parse().opto();
+ assertEquals("return 3;", code._stop.print());
}
@Test
public void testVarScope() {
- Parser parser = new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b; } return c;");
- StopNode ret = parser.parse();
- assertEquals("return 4;", ret.print());
+ CodeGen code = new CodeGen("int a=1; int b=2; int c=0; { int b=3; c=a+b; } return c;");
+ code.parse().opto();
+ assertEquals("return 4;", code._stop.print());
}
@Test
public void testVarScopeNoPeephole() {
- Parser parser = new Parser("int a=1; int b=2; int !c=0; { int b=3; c=a+b; } return c; ");
- Node._disablePeephole = true;
- StopNode ret = parser.parse();
- Node._disablePeephole = false;
- assertEquals("return (1+3);", ret.print());
+ CodeGen code = new CodeGen("int a=1; int b=2; int !c=0; { int b=3; c=a+b; } return c; ");
+ code.parse(true);
+ assertEquals("return Phi(Region,(1+3));", code._stop.print());
}
@Test
public void testVarDist() {
- Parser parser = new Parser("int x0=1; int y0=2; int x1=3; int y1=4; return (x0-x1)*(x0-x1) + (y0-y1)*(y0-y1); ");
- StopNode ret = parser.parse();
- assertEquals("return 8;", ret.print());
+ CodeGen code = new CodeGen("int x0=1; int y0=2; int x1=3; int y1=4; return (x0-x1)*(x0-x1) + (y0-y1)*(y0-y1); ");
+ code.parse().opto();
+ assertEquals("return 8;", code._stop.print());
}
@Test
public void testSelfAssign() {
try {
- new Parser("int a=a; return a;").parse();
+ new CodeGen("int a=a; return a;").parse().opto();
fail();
} catch( RuntimeException e ) {
assertEquals("Undefined name 'a'",e.getMessage());
@@ -57,7 +53,7 @@ public class Chapter03Test {
@Test
public void testBad1() {
try {
- new Parser("int a=1; int b=2; int !c=0; { int b=3; c=a+b;").parse();
+ new CodeGen("int a=1; int b=2; int !c=0; { int b=3; c=a+b;").parse().opto();
fail();
} catch( RuntimeException e ) {
assertEquals("Syntax error, expected }: ",e.getMessage());
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter04Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter04Test.java
index 24612fee..c4073fef 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter04Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter04Test.java
@@ -10,102 +10,101 @@ public class Chapter04Test {
@Test
public void testPeephole() {
- Parser parser = new Parser("return 1+arg+2; ");
- StopNode ret = parser.parse();
- assertEquals("return (arg+3);", ret.print());
+ CodeGen code = new CodeGen("return 1+arg+2; ");
+ code.parse().opto();
+ assertEquals("return (arg+3);", code._stop.print());
}
@Test
public void testPeephole2() {
- Parser parser = new Parser("return (1+arg)+2;");
- StopNode ret = parser.parse();
- assertEquals("return (arg+3);", ret.print());
+ CodeGen code = new CodeGen("return (1+arg)+2;");
+ code.parse().opto();
+ assertEquals("return (arg+3);", code._stop.print());
}
@Test
public void testAdd0() {
- Parser parser = new Parser("return 0+arg;");
- StopNode ret = parser.parse();
- assertEquals("return arg;", ret.print());
+ CodeGen code = new CodeGen("return 0+arg;");
+ code.parse().opto();
+ assertEquals("return arg;", code._stop.print());
}
@Test
public void testAddAddMul() {
- Parser parser = new Parser("return arg+0+arg;");
- StopNode ret = parser.parse();
- assertEquals("return (arg*2);", ret.print());
+ CodeGen code = new CodeGen("return arg+0+arg;");
+ code.parse().opto();
+ assertEquals("return (arg*2);", code._stop.print());
}
@Test
public void testPeephole3() {
- Parser parser = new Parser("return 1+arg+2+arg+3; ");
- StopNode ret = parser.parse();
- assertEquals("return ((arg*2)+6);", ret.print());
+ CodeGen code = new CodeGen("return 1+arg+2+arg+3; ");
+ code.parse().opto();
+ assertEquals("return ((arg*2)+6);", code._stop.print());
}
@Test
public void testMul1() {
- Parser parser = new Parser("return 1*arg;");
- StopNode ret = parser.parse();
- assertEquals("return arg;", ret.print());
+ CodeGen code = new CodeGen("return 1*arg;");
+ code.parse().opto();
+ assertEquals("return arg;", code._stop.print());
}
@Test
public void testVarArg() {
- Parser parser = new Parser("return arg; ");
- StopNode stop = parser.parse();
- ReturnNode ret = stop.ret();
- assertTrue(ret.in(0) instanceof CProjNode);
- assertTrue(ret.in(1) instanceof ProjNode);
+ CodeGen code = new CodeGen("return arg; ");
+ code.parse().opto();
+ assertTrue(code._stop.ctrl() instanceof CProjNode);
+ assertTrue(code._stop.expr() instanceof ProjNode);
}
@Test
public void testConstantArg() {
- Parser parser = new Parser("return arg; ", TypeInteger.constant(2));
- StopNode ret = parser.parse();
- assertEquals("return 2;", ret.print());
+ CodeGen code = new CodeGen("return arg; ", TypeInteger.constant(2));
+ code.parse().opto();
+ assertEquals("return 2;", code._stop.print());
}
@Test
public void testCompEq() {
- Parser parser = new Parser("return 3==3; ");
- StopNode ret = parser.parse();
- assertEquals("return 1;", ret.print());
+ CodeGen code = new CodeGen("return 3==3; ");
+ code.parse().opto();
+ assertEquals("return 1;", code._stop.print());
}
@Test
public void testCompEq2() {
- Parser parser = new Parser("return 3==4; ");
- StopNode ret = parser.parse();
- assertEquals("return 0;", ret.print());
+ CodeGen code = new CodeGen("return 3==4; ");
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.print());
}
@Test
public void testCompNEq() {
- Parser parser = new Parser("return 3!=3; ");
- StopNode ret = parser.parse();
- assertEquals("return 0;", ret.print());
+ CodeGen code = new CodeGen("return 3!=3; ");
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.print());
}
@Test
public void testCompNEq2() {
- Parser parser = new Parser("return 3!=4; ");
- StopNode ret = parser.parse();
- assertEquals("return 1;", ret.print());
+ CodeGen code = new CodeGen("return 3!=4; ");
+ code.parse().opto();
+ assertEquals("return 1;", code._stop.print());
}
@Test
public void testBug1() {
- Parser parser = new Parser("int a=arg+1; int !b=a; b=1; return a+2; ");
- StopNode ret = parser.parse();
- assertEquals("return (arg+3);", ret.print());
+ CodeGen code = new CodeGen("int a=arg+1; int !b=a; b=1; return a+2; ");
+ code.parse().opto();
+ assertEquals("return (arg+3);", code._stop.print());
}
@Test
public void testBug2() {
- Parser parser = new Parser("int !a=arg+1; a=a; return a; ");
- StopNode ret = parser.parse();
- assertEquals("return (arg+1);", ret.print());
+ CodeGen code = new CodeGen("int !a=arg+1; a=a; return a; ");
+ code.parse().opto();
+ assertEquals("return (arg+1);", code._stop.print());
}
@Test
@@ -120,9 +119,9 @@ public class Chapter04Test {
@Test
public void testBug4() {
- Parser parser = new Parser("return -arg;");
- StopNode ret = parser.parse();
- assertEquals("return (-arg);", ret.print());
+ CodeGen code = new CodeGen("return -arg;");
+ code.parse().opto();
+ assertEquals("return (-arg);", code._stop.print());
}
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter05Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter05Test.java
index 9afb8f28..bf074d0a 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter05Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter05Test.java
@@ -1,7 +1,6 @@
package com.seaofnodes.simple;
import com.seaofnodes.simple.node.*;
-import com.seaofnodes.simple.type.TypeInteger;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -10,7 +9,7 @@ public class Chapter05Test {
@Test
public void testIfStmt() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a = 1;
if (arg == 1)
@@ -20,13 +19,13 @@ else {
}
return a;
""");
- StopNode ret = parser.parse().iterate();
- assertEquals("return Phi(Region,(arg+2),(arg-3));", ret.toString());
+ code.parse().opto();
+ assertEquals("return Phi(Region,(arg+2),(arg-3));", code._stop.toString());
}
@Test
public void testTest() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int c = 3;
int b = 2;
@@ -34,27 +33,28 @@ if (arg == 1) {
b = 3;
c = 4;
}
-return c;""", TypeInteger.BOT);
- StopNode ret = parser.parse().iterate();
- assertEquals("return Phi(Region,4,3);", ret.toString());
+return c;
+""");
+ code.parse().opto();
+ assertEquals("return Phi(Region,4,3);", code._stop.toString());
}
@Test
public void testReturn2() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
if( arg==1 )
return 3;
else
return 4;
-""", TypeInteger.BOT);
- StopNode stop = parser.parse();
- assertEquals("Stop[ return 3; return 4; ]", stop.toString());
+""");
+ code.parse();
+ assertEquals("return Phi(Region,3,4);", code._stop.expr().in(0).in(1).toString());
}
@Test
public void testIfMergeB() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a=arg+1;
int b=0;
@@ -63,13 +63,13 @@ if( arg==1 )
else
b=a+1;
return a+b;""");
- StopNode ret = parser.parse().iterate();
- assertEquals("return ((arg*2)+Phi(Region,2,3));", ret.toString());
+ code.parse().opto();
+ assertEquals("return ((arg*2)+Phi(Region,2,3));", code._stop.toString());
}
@Test
public void testIfMerge2() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a=arg+1;
int b=arg+2;
@@ -78,13 +78,13 @@ if( arg==1 )
else
a=b+1;
return a+b;""");
- StopNode ret = parser.parse().iterate();
- assertEquals("return ((Phi(Region,(arg*2),arg)+arg)+Phi(Region,4,5));", ret.toString());
+ code.parse().opto();
+ assertEquals("return ((Phi(Region,(arg*2),arg)+arg)+Phi(Region,4,5));", code._stop.toString());
}
@Test
public void testMerge3() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a=1;
if( arg==1 )
@@ -97,14 +97,14 @@ else if( arg==3 )
else
a=5;
return a;
-""", TypeInteger.BOT);
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,Phi(Region,2,3),Phi(Region,4,5));", stop.toString());
+""");
+ code.parse().opto();
+ assertEquals("return Phi(Region,Phi(Region,2,3),Phi(Region,4,5));", code._stop.toString());
}
@Test
public void testMerge4() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a=0;
int b=0;
@@ -113,29 +113,30 @@ if( arg )
if( arg==0 )
b=2;
return arg+a+b;
-""", TypeInteger.BOT);
- StopNode stop = parser.parse();
- assertEquals("return ((arg+Phi(Region,1,0))+Phi(Region,2,0));", stop.toString());
+""");
+ code.parse().opto();
+ assertEquals("return ((arg+Phi(Region,1,0))+Phi(Region,2,0));", code._stop.toString());
}
@Test
public void testMerge5() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a=arg==2;
if( arg==1 )
{
a=arg==3;
}
-return a;""");
- StopNode ret = parser.parse().iterate();
- assertEquals("return (arg==Phi(Region,3,2));", ret.toString());
+return a;
+""");
+ code.parse().opto();
+ assertEquals("return (arg==Phi(Region,3,2));", code._stop.toString());
}
@Test
public void testTrue() {
StopNode stop = new Parser("return true;").parse();
- assertEquals("return 1;",stop.toString());
+ assertEquals("return 1;",stop.expr().in(0).in(1).toString());
}
@Test
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter06Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter06Test.java
index 50c0580d..02814501 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter06Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter06Test.java
@@ -10,32 +10,32 @@ public class Chapter06Test {
@Test
public void testPeepholeReturn() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
if( true ) return 2;
return 1;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 2;", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof CProjNode);
+ code.parse().opto();
+ assertEquals("return 2;", code._stop.toString());
+ assertTrue(code._stop.in(0) instanceof CProjNode);
}
@Test
public void testPeepholeRotate() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a = 1;
if (arg)
a = 2;
return (arg < a) < 3; // Because (arg < a) is a bool/uint1/[0-1], its always less than 3
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 1;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 1;", code._stop.toString());
}
@Test
public void testPeepholeCFG() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a=1;
if( true )
@@ -44,14 +44,14 @@ else
a=3;
return a;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 2;", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof CProjNode);
+ code.parse().opto();
+ assertEquals("return 2;", code._stop.toString());
+ assertTrue(code._stop.in(0) instanceof CProjNode);
}
@Test
public void testIfIf() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a=1;
if( arg!=1 )
@@ -63,14 +63,15 @@ if( a==2 )
b=42;
else
b=5;
-return b;""", TypeInteger.BOT);
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,42,5);", stop.toString());
+return b;
+""");
+ code.parse().opto();
+ assertEquals("return Phi(Region,42,5);", code._stop.toString());
}
@Test
public void testIfArgIf() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a=1;
if( 1==1 )
@@ -82,14 +83,14 @@ if( arg==2 )
b=a;
else
b=5;
-return b;""", TypeInteger.BOT);
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,2,5);", stop.toString());
+return b;""");
+ code.parse().opto();
+ assertEquals("return Phi(Region,2,5);", code._stop.toString());
}
@Test
public void testMerge3With2() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a=1;
if( arg==1 )
@@ -103,13 +104,13 @@ else
a=5;
return a;
""", TypeInteger.constant(2));
- StopNode stop = parser.parse();
- assertEquals("return 5;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 5;", code._stop.toString());
}
@Test
public void testMerge3With1() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a=1;
if( arg==1 )
@@ -123,13 +124,13 @@ else
a=5;
return a;
""", TypeInteger.constant(1));
- StopNode stop = parser.parse().iterate();
- assertEquals("return 3;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 3;", code._stop.toString());
}
@Test
public void testMerge3Peephole() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a=1;
if( arg==1 )
@@ -142,14 +143,14 @@ else if( arg==3 )
else
a=5;
return a;
-""", TypeInteger.BOT);
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,3,Phi(Region,4,5));", stop.toString());
+""");
+ code.parse().opto();
+ assertEquals("return Phi(Region,3,Phi(Region,4,5));", code._stop.toString());
}
@Test
public void testMerge3Peephole1() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a=1;
if( arg==1 )
@@ -163,13 +164,13 @@ else
a=5;
return a;
""", TypeInteger.constant(1));
- StopNode stop = parser.parse().iterate();
- assertEquals("return 3;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 3;", code._stop.toString());
}
@Test
public void testMerge3Peephole3() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a=1;
if( arg==1 )
@@ -183,13 +184,13 @@ else
a=5;
return a;
""", TypeInteger.constant(3));
- StopNode stop = parser.parse().iterate();
- assertEquals("return 4;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 4;", code._stop.toString());
}
@Test
public void testDemo1NonConst() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int a = 0;
int b = 1;
if( arg ) {
@@ -199,14 +200,14 @@ if( arg ) {
}
return a+b;
""");
- StopNode ret = parser.parse().iterate();
- assertEquals("return Phi(Region,4,1);", ret.toString());
+ code.parse().opto();
+ assertEquals("return Phi(Region,4,1);", code._stop.toString());
}
@Test
public void testDemo1True() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int a = 0;
int b = 1;
if( arg ) {
@@ -216,13 +217,13 @@ if( arg ) {
}
return a+b;
""", TypeInteger.constant(1));
- StopNode ret = parser.parse().iterate();
- assertEquals("return 4;", ret.toString());
+ code.parse().opto();
+ assertEquals("return 4;", code._stop.toString());
}
@Test
public void testDemo1False() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int a = 0;
int b = 1;
if( arg ) {
@@ -232,13 +233,13 @@ if( arg ) {
}
return a+b;
""", TypeInteger.constant(0));
- StopNode ret = parser.parse().iterate();
- assertEquals("return 1;", ret.toString());
+ code.parse().opto();
+ assertEquals("return 1;", code._stop.toString());
}
@Test
public void testDemo2NonConst() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int a = 0;
int b = 1;
int c = 0;
@@ -250,14 +251,14 @@ if( arg ) {
}
return a+b+c;
""");
- StopNode ret = parser.parse().iterate();
- assertEquals("return (Phi(Region,Phi(Region,2,3),0)+Phi(Region,3,1));", ret.toString());
+ code.parse().opto();
+ assertEquals("return (Phi(Region,Phi(Region,2,3),0)+Phi(Region,3,1));", code._stop.toString());
}
@Test
public void testDemo2True() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int a = 0;
int b = 1;
int c = 0;
@@ -269,13 +270,13 @@ if( arg ) {
}
return a+b+c;
""", TypeInteger.constant(1));
- StopNode ret = parser.parse().iterate();
- assertEquals("return 6;", ret.toString());
+ code.parse().opto();
+ assertEquals("return 6;", code._stop.toString());
}
@Test
public void testDemo2arg2() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int a = 0;
int b = 1;
int c = 0;
@@ -287,8 +288,8 @@ if( arg ) {
}
return a+b+c;
""", TypeInteger.constant(2));
- StopNode ret = parser.parse().iterate();
- assertEquals("return 5;", ret.toString());
+ code.parse().opto();
+ assertEquals("return 5;", code._stop.toString());
}
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter07Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter07Test.java
index 355bbba4..aad6a765 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter07Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter07Test.java
@@ -9,7 +9,7 @@ public class Chapter07Test {
@Test
public void testExample() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
while(arg < 10) {
arg = arg + 1;
@@ -17,15 +17,15 @@ while(arg < 10) {
return arg;
""");
Node._disablePeephole = true;
- StopNode stop = parser.parse();
- assertEquals("return Phi(Loop,arg,(Phi_arg+1));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof CProjNode);
+ code.parse();
+ assertEquals("return Phi(Loop,arg,(Phi_arg+1));", code._stop.expr().in(0).in(1).toString());
+ assertTrue(code._stop.ctrl() instanceof CProjNode);
Node._disablePeephole = false;
}
@Test
public void testRegression() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a = 1;
if(arg){}else{
@@ -35,13 +35,13 @@ if(arg){}else{
}
return a;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,1,Phi(Loop,1,(Phi_a+1)));", stop.toString());
+ code.parse().opto();
+ assertEquals("return Phi(Region,1,Phi(Loop,1,(Phi_a+1)));", code._stop.toString());
}
@Test
public void testWhileNested() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int sum = 0;
int i = 0;
@@ -55,13 +55,13 @@ while(i < arg) {
}
return sum;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,0,Phi(Loop,Phi_sum,(Phi_sum+Phi(Loop,0,(Phi_j+1)))));", stop.toString());
+ code.parse().opto();
+ assertEquals("return Phi(Loop,0,Phi(Loop,Phi_sum,(Phi_sum+Phi(Loop,0,(Phi_j+1)))));", code._stop.toString());
}
@Test
public void testWhileScope() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a = 1;
int b = 2;
@@ -72,15 +72,15 @@ while(a < 10) {
return b;
""");
Node._disablePeephole = true;
- StopNode stop = parser.parse();
- assertEquals("return Phi(Loop,2,Phi(Region,Phi_b,4));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof CProjNode);
+ code.parse();
+ assertEquals("return Phi(Loop,2,Phi(Region,Phi_b,4));", code._stop.expr().in(0).in(1).toString());
+ assertTrue(code._stop.ctrl() instanceof CProjNode);
Node._disablePeephole = false;
}
@Test
public void testWhileNestedIfAndInc() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a = 1;
int b = 2;
@@ -92,15 +92,15 @@ while(a < 10) {
}
return b;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,2,(Phi(Region,Phi_b,4)+1));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof CProjNode);
+ code.parse().opto();
+ assertEquals("return Phi(Loop,2,(Phi(Region,Phi_b,4)+1));", code._stop.toString());
+ assertTrue(code._stop.ctrl() instanceof CProjNode);
}
@Test
public void testWhile() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a = 1;
while(a < 10) {
@@ -110,15 +110,15 @@ while(a < 10) {
return a;
""");
Node._disablePeephole = true;
- StopNode stop = parser.parse();
- assertEquals("return Phi(Loop,1,((Phi_a+1)+2));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof CProjNode);
+ code.parse();
+ assertEquals("return Phi(Loop,1,(Phi_a+3));", code._stop.expr().in(0).in(1).toString());
+ assertTrue(code._stop.ctrl() instanceof CProjNode);
Node._disablePeephole = false;
}
@Test
public void testWhilePeep() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a = 1;
while(a < 10) {
@@ -127,42 +127,42 @@ while(a < 10) {
}
return a;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,1,(Phi_a+3));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof CProjNode);
+ code.parse().opto();
+ assertEquals("return Phi(Loop,1,(Phi_a+3));", code._stop.toString());
+ assertTrue(code._stop.ctrl() instanceof CProjNode);
}
@Test
public void testWhile2() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a = 1;
while(arg) a = 2;
return a;
""");
Node._disablePeephole = true;
- StopNode stop = parser.parse();
- assertEquals("return Phi(Loop,1,2);", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof CProjNode);
+ code.parse();
+ assertEquals("return Phi(Loop,1,2);", code._stop.expr().in(0).in(1).toString());
+ assertTrue(code._stop.ctrl() instanceof CProjNode);
Node._disablePeephole = false;
}
@Test
public void testWhile2Peep() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a = 1;
while(arg) a = 2;
return a;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,1,2);", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof CProjNode);
+ code.parse().opto();
+ assertEquals("return Phi(Loop,1,2);", code._stop.toString());
+ assertTrue(code._stop.ctrl() instanceof CProjNode);
}
@Test
public void testWhile3() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a = 1;
while(a < 10) {
@@ -172,15 +172,15 @@ while(a < 10) {
return a;
""");
Node._disablePeephole = true;
- StopNode stop = parser.parse();
- assertEquals("return Phi(Loop,1,((Phi_a+1)+2));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof CProjNode);
+ code.parse();
+ assertEquals("return Phi(Loop,1,(Phi_a+3));", code._stop.expr().in(0).in(1).toString());
+ assertTrue(code._stop.ctrl() instanceof CProjNode);
Node._disablePeephole = false;
}
@Test
public void testWhile3Peep() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a = 1;
while(a < 10) {
@@ -189,14 +189,14 @@ while(a < 10) {
}
return a;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,1,(Phi_a+3));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof CProjNode);
+ code.parse().opto();
+ assertEquals("return Phi(Loop,1,(Phi_a+3));", code._stop.toString());
+ assertTrue(code._stop.ctrl() instanceof CProjNode);
}
@Test
public void testWhile4() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a = 1;
int b = 2;
@@ -207,15 +207,15 @@ while(a < 10) {
return a;
""");
Node._disablePeephole = true;
- StopNode stop = parser.parse();
- assertEquals("return Phi(Loop,1,((Phi_a+1)+2));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof CProjNode);
+ code.parse();
+ assertEquals("return Phi(Loop,1,(Phi_a+3));", code._stop.expr().in(0).in(1).toString());
+ assertTrue(code._stop.ctrl() instanceof CProjNode);
Node._disablePeephole = false;
}
@Test
public void testWhile4Peep() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a = 1;
int b = 2;
@@ -225,9 +225,9 @@ while(a < 10) {
}
return a;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,1,(Phi_a+3));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof CProjNode);
+ code.parse().opto();
+ assertEquals("return Phi(Loop,1,(Phi_a+3));", code._stop.toString());
+ assertTrue(code._stop.ctrl() instanceof CProjNode);
}
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter08Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter08Test.java
index bb3bd674..831a3272 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter08Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter08Test.java
@@ -11,7 +11,7 @@ public class Chapter08Test {
@Test
public void testEx6() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
while(arg < 10) {
arg = arg + 1;
@@ -22,17 +22,17 @@ while(arg < 10) {
}
return arg;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,Phi(Region,Phi(Loop,arg,(Phi_arg+1)),Add),Add);", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof RegionNode);
- Assert.assertEquals(5L, Evaluator.evaluate(stop, 1));
- Assert.assertEquals(10L, Evaluator.evaluate(stop, 6));
+ code.parse().opto();
+ assertEquals("return Phi(Region,Phi(Region,Phi(Loop,arg,(Phi_arg+1)),Add),Add);", code._stop.toString());
+ assertTrue(code._stop.in(0) instanceof RegionNode);
+ Assert.assertEquals(5L, Evaluator.evaluate(code._stop, 1));
+ Assert.assertEquals(10L, Evaluator.evaluate(code._stop, 6));
}
@Test
public void testEx5() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a = 1;
while(arg < 10) {
@@ -45,15 +45,15 @@ while(arg < 10) {
}
return a;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,1,Phi(Region,Phi_a,(Phi_a+1)));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof CProjNode);
+ code.parse().opto();
+ assertEquals("return Phi(Loop,1,Phi(Region,Phi_a,(Phi_a+1)));", code._stop.toString());
+ assertTrue(code._stop.ctrl() instanceof CProjNode);
}
@Test
public void testEx4() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
while(arg < 10) {
arg = arg + 1;
@@ -64,14 +64,14 @@ while(arg < 10) {
}
return arg;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,Phi(Loop,arg,(Phi_arg+1)),Add);", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof RegionNode);
+ code.parse().opto();
+ assertEquals("return Phi(Region,Phi(Loop,arg,(Phi_arg+1)),Add);", code._stop.toString());
+ assertTrue(code._stop.ctrl() instanceof RegionNode);
}
@Test
public void testEx3() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
while(arg < 10) {
arg = arg + 1;
@@ -80,15 +80,15 @@ while(arg < 10) {
}
return arg;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,Phi(Loop,arg,(Phi_arg+1)),Add);", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof RegionNode);
+ code.parse().opto();
+ assertEquals("return Phi(Region,Phi(Loop,arg,(Phi_arg+1)),Add);", code._stop.toString());
+ assertTrue(code._stop.ctrl() instanceof RegionNode);
}
@Test
public void testEx2() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
while(arg < 10) {
arg = arg + 1;
@@ -99,15 +99,15 @@ while(arg < 10) {
}
return arg;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,arg,(Phi_arg+1));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof CProjNode);
+ code.parse().opto();
+ assertEquals("return Phi(Loop,arg,(Phi_arg+1));", code._stop.toString());
+ assertTrue(code._stop.ctrl() instanceof CProjNode);
}
@Test
public void testEx1() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
while(arg < 10) {
arg = arg + 1;
@@ -116,14 +116,14 @@ while(arg < 10) {
}
return arg;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,arg,(Phi_arg+1));", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof CProjNode);
+ code.parse().opto();
+ assertEquals("return Phi(Loop,arg,(Phi_arg+1));", code._stop.toString());
+ assertTrue(code._stop.ctrl() instanceof CProjNode);
}
@Test
public void testRegress1() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
while( arg < 10 ) {
int a = arg+2;
@@ -132,15 +132,15 @@ while( arg < 10 ) {
}
return arg;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return arg;", stop.toString());
+ code.parse().opto();
+ assertEquals("return arg;", code._stop.toString());
}
@Test
public void testRegress2() {
- Parser parser = new Parser("if(1) return 0; else while(arg>- -arg) arg=arg+1; return 0;");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ CodeGen code = new CodeGen("if(1) return 0; else while(arg>- -arg) arg=arg+1; return 0;");
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
@@ -161,19 +161,19 @@ return arg;
@Test
public void testRegress3() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
while(arg < 10) {
break;
}
return arg;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return arg;", stop.toString());
+ code.parse().opto();
+ assertEquals("return arg;", code._stop.toString());
}
@Test
public void testRegress4() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int a = 1;
while(arg < 10) {
a = a + 1;
@@ -184,14 +184,14 @@ while(arg < 10) {
}
return a;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,Phi(Loop,1,(Phi_a+1)),Add);", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof RegionNode);
+ code.parse().opto();
+ assertEquals("return Phi(Region,Phi(Loop,1,(Phi_a+1)),Add);", code._stop.toString());
+ assertTrue(code._stop.ctrl() instanceof RegionNode);
}
@Test
public void testRegress5() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int a = 1;
while(1) {
a = a + 1;
@@ -200,9 +200,9 @@ while(1) {
}
return a;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return (Phi(Loop,1,Add)+1);", stop.toString());
- assertTrue(stop.ret().ctrl() instanceof CProjNode prj && prj._idx==1 );
+ code.parse().opto();
+ assertEquals("return (Phi(Loop,1,Add)+1);", code._stop.toString());
+ assertTrue(code._stop.ctrl() instanceof CProjNode prj && prj._idx==1 );
}
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter09Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter09Test.java
index c31afcd6..27cd2be7 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter09Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter09Test.java
@@ -1,7 +1,6 @@
package com.seaofnodes.simple;
import com.seaofnodes.simple.evaluator.Evaluator;
-import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
import org.junit.Assert;
@@ -15,7 +14,7 @@ public class Chapter09Test {
@Test
@Ignore
public void testJig() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int v0=0;
arg=0;
while(v0) {
@@ -25,12 +24,12 @@ while(v0) {
}
return 0;
""");
- StopNode stop = parser.parse().iterate();
+ code.parse().opto();
}
@Test
public void testGVN1() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int x = arg + arg;
if(arg < 10) {
@@ -41,26 +40,26 @@ else {
}
return x;
""");
- StopNode stop = parser.parse();
- assertEquals("Stop[ return (arg*2); return (Mul+1); ]", stop.toString());
- Assert.assertEquals(2L, Evaluator.evaluate(stop, 1));
- Assert.assertEquals(23L, Evaluator.evaluate(stop, 11));
+ code.parse().opto();
+ assertEquals("return Phi(Region,(arg*2),(Mul+1));", code._stop.toString());
+ Assert.assertEquals(2L, Evaluator.evaluate(code._stop, 1));
+ Assert.assertEquals(23L, Evaluator.evaluate(code._stop, 11));
}
@Test
public void testGVN2() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
return arg*arg-arg*arg;
""");
- StopNode stop = parser.parse();
- assertEquals("return 0;", stop.toString());
- Assert.assertEquals(0L, Evaluator.evaluate(stop, 1));
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
+ Assert.assertEquals(0L, Evaluator.evaluate(code._stop, 1));
}
@Test
public void testWorklist1() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int step = 1;
while (arg < 10) {
@@ -68,14 +67,14 @@ while (arg < 10) {
}
return arg;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,arg,(Phi_arg+2));", stop.toString());
- Assert.assertEquals(11L, Evaluator.evaluate(stop, 1));
+ code.parse().opto();
+ assertEquals("return Phi(Loop,arg,(Phi_arg+2));", code._stop.toString());
+ Assert.assertEquals(11L, Evaluator.evaluate(code._stop, 1));
}
@Test
public void testWorklist2() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int cond = 0;
int one = 1;
@@ -85,14 +84,14 @@ while (arg < 10) {
}
return arg;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,arg,(Phi_arg+4));", stop.toString());
- Assert.assertEquals(13L, Evaluator.evaluate(stop, 1));
+ code.parse().opto();
+ assertEquals("return Phi(Loop,arg,(Phi_arg+4));", code._stop.toString());
+ Assert.assertEquals(13L, Evaluator.evaluate(code._stop, 1));
}
@Test
public void testWorklist3() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int v1 = 0;
int v2 = 0;
@@ -114,13 +113,13 @@ while (arg) {
}
return arg;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
public void testRegionPeepBug() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int v0=0;
int v1=0;
@@ -132,48 +131,48 @@ while(v1+arg) {
v1=v2;
}
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("Stop[ return 0; return 0; ]", stop.toString());
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
public void testWhile0() {
- Parser parser = new Parser("while(0) continue; if(0) arg=0;");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ CodeGen code = new CodeGen("while(0) continue; if(0) arg=0;");
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
public void testWhile1() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
if(0) while(0) {
int arg=arg;
while(0) {}
}
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
public void testPrecedence() {
- Parser parser = new Parser("return 3-1+2;");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 4;", stop.toString());
+ CodeGen code = new CodeGen("return 3-1+2;");
+ code.parse().opto();
+ assertEquals("return 4;", code._stop.toString());
}
@Test
public void testSwap2() {
- Parser parser = new Parser("return 1+(1+1);");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 3;", stop.toString());
+ CodeGen code = new CodeGen("return 1+(1+1);");
+ code.parse().opto();
+ assertEquals("return 3;", code._stop.toString());
}
@Test
public void testFuzz0() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int one = 1;
int a = 0;
int zero = 0;
@@ -184,13 +183,13 @@ while(arg) {
}
return a;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,0,(-(Phi_a+3)));", stop.toString());
+ code.parse().opto();
+ assertEquals("return Phi(Loop,0,(-(Phi_a+3)));", code._stop.toString());
}
@Test
public void testFuzz1() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
while(1) {}
while(arg) break;
while(arg) arg=0;
@@ -198,40 +197,40 @@ arg=0;
int v0=0!=0<-0;
return -0+0+0;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
public void testFuzz2() {
- Parser parser = new Parser("return 0+-0;");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ CodeGen code = new CodeGen("return 0+-0;");
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
public void testFuzz3() {
- Parser parser = new Parser("int v0=0; while(0==69) while(v0) return 0;");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ CodeGen code = new CodeGen("int v0=0; while(0==69) while(v0) return 0;");
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
public void testFuzz4() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
while(1) {
arg=0<=0;
if(1<0) while(arg==-0) arg=arg-arg;
}
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return Top;", code._stop.toString());
}
@Test
public void testFuzz5() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
{
int v0=0;
while(1)
@@ -247,25 +246,25 @@ while(1) {
}
return 0!=0;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
public void testFuzz6() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int v0=0;
while(0==1) while(v0)
v0=1+v0;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return ~int;", code._stop.toString());
}
@Test
public void testFuzz7() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
while(1) {}
int v0=0;
while(v0)
@@ -275,15 +274,15 @@ while(1)
v1=1;
return v1+v0;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return Top;", code._stop.toString());
}
@Test
public void testFuzz8() {
- Parser parser = new Parser("while(arg) arg = arg - 1; return arg;");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ CodeGen code = new CodeGen("while(arg) arg = arg - 1; return arg;");
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter10Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter10Test.java
index 86d807a5..461c8ed8 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter10Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter10Test.java
@@ -10,7 +10,7 @@ public class Chapter10Test {
@Test
public void testFuzzer() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int a = arg/3;
int b = arg*5;
@@ -29,13 +29,13 @@ if( (arg/13)==0 ) {
int r = g+h;
return p-r;
""");
- StopNode stop = parser.parse(false).iterate(false);
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
public void testStruct() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Bar {
int a;
int b;
@@ -49,13 +49,13 @@ bar.a = 1;
bar.a = 2;
return bar.a;
""");
- StopNode stop = parser.parse(false).iterate();
- assertEquals("return 2;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 2;", code._stop.toString());
}
@Test
public void testExample() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Vector2D { int x; int y; };
Vector2D !v = new Vector2D;
v.x = 1;
@@ -65,36 +65,36 @@ else
v.y = 3;
return v;
""");
- StopNode stop = parser.parse(false).iterate();
- assertEquals("return Vector2D;", stop.toString());
+ code.parse().opto();
+ assertEquals("return Vector2D;", code._stop.toString());
}
@Test
public void testBug() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct s0 {
int v0;
};
s0? v1=null;
int v3=v1.zAicm;
""");
- try { parser.parse(); fail(); }
+ try { code.parse(); fail(); }
catch( Exception e ) { assertEquals("Accessing unknown field 'zAicm' from 'null'",e.getMessage()); }
}
@Test
public void testBug2() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct s0 { int v0; };
arg=0+new s0.0;
""");
- try { parser.parse(); fail(); }
+ try { code.parse(); fail(); }
catch( Exception e ) { assertEquals("Expected an identifier, found 'null'",e.getMessage()); }
}
@Test
public void testLoop() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Bar { int a; };
Bar !bar = new Bar;
while (arg) {
@@ -103,65 +103,65 @@ while (arg) {
}
return bar.a;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,0,(Phi_a+2));", stop.toString());
+ code.parse().opto();
+ assertEquals("return Phi(Loop,0,(Phi_a+2));", code._stop.toString());
}
@Test
public void testIf() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Bar { int a; };
Bar !bar = new Bar;
if (arg) bar = null;
bar.a = 1;
return bar.a;
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto(); fail(); }
catch( Exception e ) { assertEquals("Type null is not of declared type *Bar",e.getMessage()); }
}
@Test
public void testIf2() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Bar { int a; };
Bar? !bar = null;
if (arg) bar = new Bar;
bar.a = 1;
return bar.a;
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto().typeCheck(); fail(); }
catch( Exception e ) { assertEquals("Might be null accessing 'a'",e.getMessage()); }
}
@Test
public void testIf3() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Bar { int a; };
Bar bar = null;
if (arg) bar = null;
bar.a = 1;
return bar.a;
""");
- try { parser.parse(); fail(); }
+ try { code.parse(); fail(); }
catch( Exception e ) { assertEquals("Type null is not of declared type *Bar", e.getMessage()); }
}
@Test
public void testIfOrNull() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Bar { int a; };
Bar? !bar = new Bar;
if (arg) bar = null;
if( bar ) bar.a = 1;
return bar;
""");
- StopNode stop = parser.parse(false).iterate();
- assertEquals("return Phi(Region,null,Bar);", stop.toString());
+ code.parse().opto();
+ assertEquals("return Phi(Region,null,Bar);", code._stop.toString());
}
@Test
public void testIfOrNull2() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct Bar { int a; };
Bar? !bar = new Bar;
@@ -171,13 +171,13 @@ if( !bar ) rez=4;
else bar.a = 1;
return rez;
""");
- StopNode stop = parser.parse(false).iterate();
- assertEquals("return Phi(Region,4,3);", stop.toString());
+ code.parse().opto();
+ assertEquals("return Phi(Region,4,3);", code._stop.toString());
}
@Test
public void testWhileWithNullInside() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct s0 {int v0;};
s0? !v0 = new s0;
int ret = 0;
@@ -188,14 +188,14 @@ while(arg) {
}
return ret;
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto().typeCheck(); fail(); }
catch( Exception e ) {
assertEquals("Might be null accessing 'v0'", e.getMessage()); }
}
@Test
public void testRedeclareStruct() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct s0 {
int v0;
};
@@ -203,14 +203,14 @@ s0? v1=new s0;
s0? v1;
v1=new s0;
""");
- try { parser.parse(); fail(); }
+ try { code.parse(); fail(); }
catch( Exception e ) { assertEquals("Redefining name 'v1'", e.getMessage()); }
}
@Test
public void testIter() {
// Build and use an iterator
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct Iter {
int x;
@@ -225,14 +225,14 @@ while( i.x < i.len ) {
}
return sum;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,0,(Phi(Loop,0,(Phi_x+1))+Phi_sum));", stop.toString());
+ code.parse().opto();
+ assertEquals("return Phi(Loop,0,(Phi(Loop,0,(Phi_x+1))+Phi_sum));", code._stop.toString());
}
@Test
public void test1() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct s0 {int v0;};
s0 !ret = new s0;
while(arg) {
@@ -244,13 +244,13 @@ while(arg) {
}
return ret;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,s0,Phi(Region,s0,Phi_ret));", stop.toString());
+ code.parse().opto();
+ assertEquals("return Phi(Loop,s0,Phi(Region,s0,Phi_ret));", code._stop.toString());
}
@Test
public void test2() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct s0 {int v0;};
s0 !ret = new s0;
s0 !v0 = new s0;
@@ -262,14 +262,14 @@ while(arg) {
}
return ret;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,s0,Phi(Region,s0,Phi_ret));", stop.toString());
+ code.parse().opto();
+ assertEquals("return Phi(Loop,s0,Phi(Region,s0,Phi_ret));", code._stop.toString());
}
@Test
public void test3() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct s0 {int v0;};
s0 !ret = new s0;
while(arg < 10) {
@@ -279,25 +279,25 @@ while(arg < 10) {
}
return ret;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,s0,Phi(Region,s0,Phi_ret));", stop.toString());
+ code.parse().opto();
+ assertEquals("return Phi(Loop,s0,Phi(Region,s0,Phi_ret));", code._stop.toString());
}
@Test
public void testBug3() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct s0 { int f0; };
return new s0;
int v0=null.f0;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return s0;", stop.toString());
- assertEquals("Obj<s0>{f0=0}", Evaluator.evaluate(stop, 0).toString());
+ code.parse().opto();
+ assertEquals("return s0;", code._stop.toString());
+ assertEquals("Obj<s0>{f0=0}", Evaluator.evaluate(code._stop, 0).toString());
}
@Test
public void testBug4() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
if(0) {
while(0) if(arg) continue;
int v0=0;
@@ -307,13 +307,13 @@ if(0) {
}
}
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
public void testBug5() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct s0 {
int f0;
};
@@ -321,13 +321,13 @@ if(0) return 0;
else return new s0;
if(new s0.f0) return 0;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return s0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return s0;", code._stop.toString());
}
@Test
public void testBug6MissedWorklist() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
while(0) {}
int v4=0;
while(0<arg) {
@@ -337,26 +337,26 @@ while(0<arg) {
}
return 0;
""");
- StopNode stop = parser.parse().iterate();
+ code.parse().opto();
}
@Test
public void testBug7() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct s0 { int f0; };
s0 v0 = new s0;
while(v0.f0) {}
s0 v1 = v0;
return v1;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return (const)s0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return (const)s0;", code._stop.toString());
}
@Test
public void testBug8() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int v2=0;
while(0)
while(0) {}
@@ -381,19 +381,19 @@ while(0) {}
} }
}
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
public void testBug9() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int v0=arg==0;
while(v0) continue;
return 0;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter11Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter11Test.java
index 613b979b..2854941f 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter11Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter11Test.java
@@ -1,7 +1,6 @@
package com.seaofnodes.simple;
import com.seaofnodes.simple.evaluator.Evaluator;
-import com.seaofnodes.simple.node.StopNode;
import org.junit.Test;
@@ -12,7 +11,7 @@ public class Chapter11Test {
// A placeholder test used to rapidly rotate through fuzzer produced issues
@Test
public void testFuzzer() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
while(---- -arg) {
while(-arg) {
@@ -291,14 +290,14 @@ else {
}
}
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("Stop[ return 9; return 0; return 0; return 0; return 0; ]", stop.toString());
+ code.parse().opto().typeCheck().codegen();
+ assertEquals("return Phi(Region,9,Top,Top,Top,Top);", code._stop.toString());
}
@Test
public void testPrimes() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
if( arg < 2 ) return 0;
int primeCount = 1;
@@ -323,19 +322,19 @@ while( prime <= arg ) {
}
return primeCount;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("Stop[ return 0; return Phi(Loop,1,Phi(Region,Phi_primeCount,Phi(Region,(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
+ code.parse().opto();
+ assertEquals("return Phi(Region,0,Phi(Loop,1,Phi(Region,Phi_primeCount,Phi(Region,(Phi_primeCount+1),Phi_primeCount))));", code._stop.toString());
+ assertEquals(0L, Evaluator.evaluate(code._stop, 1)); // No primes 1 or below
+ assertEquals(1L, Evaluator.evaluate(code._stop, 2)); // 2
+ assertEquals(2L, Evaluator.evaluate(code._stop, 3)); // 2, 3
+ assertEquals(2L, Evaluator.evaluate(code._stop, 4)); // 2, 3
+ assertEquals(3L, Evaluator.evaluate(code._stop, 5)); // 2, 3, 5
+ assertEquals(4L, Evaluator.evaluate(code._stop, 10)); // 2, 3, 5, 7
}
@Test
public void testAntiDeps1() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v=new S;
@@ -345,15 +344,15 @@ i=v.f;
if (arg) v.f=1;
return i;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 2;", stop.toString());
- assertEquals(2L, Evaluator.evaluate(stop, 0));
- assertEquals(2L, Evaluator.evaluate(stop, 1));
+ code.parse().opto();
+ assertEquals("return 2;", code._stop.toString());
+ assertEquals(2L, Evaluator.evaluate(code._stop, 0));
+ assertEquals(2L, Evaluator.evaluate(code._stop, 1));
}
@Test
public void testAntiDeps2() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v = new S;
@@ -368,13 +367,13 @@ if (arg) {
}
return i;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,.f,0);", stop.toString());
+ code.parse().opto();
+ assertEquals("return Phi(Region,.f,0);", code._stop.toString());
}
@Test
public void testAntiDeps3() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v0 = new S;
@@ -387,14 +386,14 @@ if (v1) {
}
return v0;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return S;", stop.toString());
+ code.parse().opto();
+ assertEquals("return S;", code._stop.toString());
}
@Test
public void testAntiDeps4() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v = new S;
@@ -405,13 +404,13 @@ if (arg+1) arg= 0;
while (arg) v.f = 2;
return i;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return arg;", stop.toString());
+ code.parse().opto();
+ assertEquals("return arg;", code._stop.toString());
}
@Test
public void testAntiDeps5() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v = new S;
@@ -422,13 +421,13 @@ while(1) {
}
return v;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return S;", stop.toString());
+ code.parse().opto();
+ assertEquals("return S;", code._stop.toString());
}
@Test
public void testAntiDeps6() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct s { int v; };
s !ptr=new s;
@@ -437,13 +436,13 @@ while( -arg )
while(1)
arg = arg+ptr.v;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return Top;", code._stop.toString());
}
@Test
public void testAntiDeps7() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v = new S;
@@ -455,13 +454,13 @@ while (arg) {
}
return i;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
public void testAntiDeps8() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v = new S;
@@ -475,13 +474,13 @@ while(arg) {
}
return arg;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
public void testAntiDeps9() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v = new S;
@@ -493,13 +492,13 @@ if (arg) {
}
return v;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return S;", stop.toString());
+ code.parse().opto();
+ assertEquals("return S;", code._stop.toString());
}
@Test
public void testExample2() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct S { int f; };
S !v = new S;
@@ -512,13 +511,13 @@ while (arg > 0) {
}
return v;
""");
- StopNode stop = parser.parse().iterate();
- //assertEquals("return new S;", stop.toString());
+ code.parse().opto();
+ //assertEquals("return new S;", code._stop.toString());
}
@Test
public void testScheduleUse() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int v0=0;
while(0>=0) {
@@ -529,13 +528,13 @@ while(0>=0) {
}
return 0;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return Top;", code._stop.toString());
}
@Test
public void testLoopCarriedDep() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
u32 v0=0;
{
@@ -544,10 +543,11 @@ u32 v0=0;
v1=1>>>v0!=0;
v0=v1/0;
}
+ return v1;
}
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java
index 39824d39..b5b6405c 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter12Test.java
@@ -10,29 +10,29 @@ import static org.junit.Assert.fail;
public class Chapter12Test {
@Test
public void testJig() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
return 3.14;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 3.14;", stop.toString());
- assertEquals(3.14, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 3.14;", code._stop.toString());
+ assertEquals(3.14, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testFloat() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
return 3.14;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 3.14;", stop.toString());
- assertEquals(3.14, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 3.14;", code._stop.toString());
+ assertEquals(3.14, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testSquareRoot() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
flt guess = arg;
while( 1 ) {
@@ -42,21 +42,21 @@ while( 1 ) {
}
return guess;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,(flt)arg,(((ToFloat/Phi_guess)+Phi_guess)/2.0f));", stop.toString());
- assertEquals(3.0, Evaluator.evaluate(stop, 9));
- assertEquals(1.414213562373095, Evaluator.evaluate(stop, 2));
+ code.parse().opto();
+ assertEquals("return Phi(Loop,(flt)arg,(((ToFloat/Phi_guess)+Phi_guess)/2.0f));", code._stop.toString());
+ assertEquals(3.0, Evaluator.evaluate(code._stop, 9));
+ assertEquals(1.414213562373095, Evaluator.evaluate(code._stop, 2));
}
@Test
public void testFPOps() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
flt x = arg;
return x+1==x;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return ((flt)arg==(ToFloat+1.0f));", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 1));
+ code.parse().opto();
+ assertEquals("return ((flt)arg==(ToFloat+1.0f));", code._stop.toString());
+ assertEquals(0L, Evaluator.evaluate(code._stop, 1));
}
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter13Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter13Test.java
index 30d9b147..ca811a2a 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter13Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter13Test.java
@@ -1,7 +1,6 @@
package com.seaofnodes.simple;
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.fail;
@@ -9,18 +8,18 @@ import static org.junit.Assert.fail;
public class Chapter13Test {
@Test
public void testJig() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
return 3.14;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 3.14;", stop.toString());
- assertEquals(3.14, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 3.14;", code._stop.toString());
+ assertEquals(3.14, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testLinkedList0() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct LLI { LLI? next; int i; };
LLI? !head = null;
@@ -33,13 +32,13 @@ while( arg ) {
}
return head.next.i;
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto().typeCheck(); fail(); }
catch( Exception e ) { assertEquals("Might be null accessing 'i'",e.getMessage()); }
}
@Test
public void testLinkedList1() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct LLI { LLI? next; int i; };
LLI? !head = null;
@@ -55,14 +54,14 @@ 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));
+ code.parse().opto();
+ assertEquals("return Phi(Region,0,1,.i);", code._stop.toString());
+ assertEquals(2L, Evaluator.evaluate(code._stop, 3));
}
@Test
public void testCoRecur() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct int0 { int i; flt0? f; };
struct flt0 { flt f; int0? i; };
@@ -74,98 +73,98 @@ i0.f = f0;
f0.i = i0;
return f0.i.f.i.i;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 17;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 17;", code._stop.toString());
}
@Test
public void testNullRef0() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct N { N? next; int i; };
N n = new N;
return n.next;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return null;", stop.toString());
+ code.parse().opto();
+ assertEquals("return null;", code._stop.toString());
}
@Test
public void testNullRef1() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct M { int m; };
struct N { M next; int i; };
N n = new N { next = new M; };
return n.next;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return (const)M;", stop.toString());
+ code.parse().opto();
+ assertEquals("return (const)M;", code._stop.toString());
}
@Test
public void testNullRef2() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct M { int m; };
struct N { M next; int i; };
N n = new N { next = null; }
return n.next;
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto(); fail(); }
catch( Exception e ) { assertEquals("Type null is not of declared type *M",e.getMessage()); }
}
@Test
public void testNullRef3() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct N { N? next; int i; };
N !n = new N;
n.i = 3.14;
return n.i;
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto(); fail(); }
catch( Exception e ) { assertEquals("Type 3.14 is not of declared type int",e.getMessage()); }
}
@Test
public void testEmpty() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct S{};
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
+ assertEquals(0L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testForwardRef0() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct S1 { S2? s; };
return new S2;
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto(); fail(); }
catch( Exception e ) { assertEquals("Unknown struct type 'S2'",e.getMessage()); }
}
@Test
public void testForwardRef1() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct S1 { S2? s; };
struct S2 { int x; };
return new S1.s=new S2;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return S2;", stop.toString());
+ code.parse().opto();
+ assertEquals("return S2;", code._stop.toString());
}
@Test
public void testcheckNull() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct I {int i;};
struct P { I? pi; };
@@ -176,7 +175,7 @@ p2.pi.i = 2;
if (arg) p1 = new P;
return p1.pi.i + 1;
""");
- try { parser.parse(false).iterate(); fail(); }
+ try { code.parse().opto().typeCheck(); fail(); }
catch( Exception e ) { assertEquals("Might be null accessing 'i'",e.getMessage()); }
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter14Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter14Test.java
index f4e35fe5..41fb546d 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter14Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter14Test.java
@@ -11,18 +11,18 @@ public class Chapter14Test {
@Test
public void testJig() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
return 3.14;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 3.14;", stop.toString());
- assertEquals(3.14, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 3.14;", code._stop.toString());
+ assertEquals(3.14, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testRange() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int b;
if( arg ) b=1; else b=0;
@@ -31,157 +31,157 @@ if( b < 0 ) c = -1;
if( b > 2 ) c = 1;
return c;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 99;", stop.toString());
- assertEquals(99L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 99;", code._stop.toString());
+ assertEquals(99L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testU8() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
u8 b = 123;
b = b + 456;// Truncate
return b;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 67;", stop.toString());
- assertEquals(67L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 67;", code._stop.toString());
+ assertEquals(67L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testU8While() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
u8 b = 123;
while( b ) b = b + 456;// Truncate
return b;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
public void testU1() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
bool b = 123;
b = b + 456;// Truncate
u1 c = b; // No more truncate needed
return c;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 1;", stop.toString());
- assertEquals(1L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 1;", code._stop.toString());
+ assertEquals(1L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testAnd() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int b = 123;
b = b+456 & 31; // Precedence
return b;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 3;", stop.toString());
- assertEquals(3L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 3;", code._stop.toString());
+ assertEquals(3L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testRefLoad() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct Foo { u1 b; };
Foo !f = new Foo;
f.b = 123;
return f.b;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 1;", stop.toString());
- assertEquals(1L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 1;", code._stop.toString());
+ assertEquals(1L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testSigned() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
i8 b = 255; // Chopped
return b; // Sign extend
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return -1;", stop.toString());
- assertEquals(-1L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return -1;", code._stop.toString());
+ assertEquals(-1L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testI8() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
i8 b = arg;
b = b + 1;// Truncate
return b;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return (((((arg<<56)>>56)+1)<<56)>>56);", stop.toString());
- assertEquals(1L, Evaluator.evaluate(stop, 0));
- assertEquals(-128L, Evaluator.evaluate(stop, 127));
+ code.parse().opto();
+ assertEquals("return (((((arg<<56)>>56)+1)<<56)>>56);", code._stop.toString());
+ assertEquals(1L, Evaluator.evaluate(code._stop, 0));
+ assertEquals(-128L, Evaluator.evaluate(code._stop, 127));
}
@Test
public void testMask() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
u16 mask = (1<<16)-1; // AND mask
int c = 123456789 & mask;
return c; //
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 52501;", stop.toString());
- assertEquals(52501L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 52501;", code._stop.toString());
+ assertEquals(52501L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testOr() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
return (arg | 123 ^ 456) >>> 1;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return (((arg|123)^456)>>>1);", stop.toString());
- assertEquals(217L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return (((arg|123)^456)>>>1);", code._stop.toString());
+ assertEquals(217L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testMaskFloat() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
flt f = arg;
arg = f & 1;
return arg;
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto().typeCheck(); fail(); }
catch( Exception e ) { assertEquals("Cannot '&' flt",e.getMessage()); }
}
@Test
public void testCloneAnd() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int v0=0;
u32 v1 = 1&(1<<arg)&(1<<arg);
while(arg) v1=-v0;
while(v1) break;
return v1;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return (Phi(Loop,((1<<arg)&1),0)&Phi(Loop,Shl,4294967295));", stop.toString());
- assertEquals(1L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return (Phi(Loop,((1<<arg)&1),0)&Phi(Loop,Shl,4294967295));", code._stop.toString());
+ assertEquals(1L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testAndHigh() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int v0=0;
if(0&0>>>0) {
while(0) {
@@ -193,14 +193,14 @@ if(0&0>>>0) {
}
return v0;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
+ assertEquals(0L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testTypes() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
i8 xi8 = 123456789; if( xi8 != 21 ) return -8;
i16 xi16 = 123456789; if( xi16 != -13035 ) return -16;
@@ -219,9 +219,9 @@ f32 ff32 = 3.141592653589793; if( ff32 != 3.1415927410125732) return 5;
return 0;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
+ assertEquals(0L, Evaluator.evaluate(code._stop, 0));
}
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter15Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter15Test.java
index e7dec389..fc9624d8 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter15Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter15Test.java
@@ -9,32 +9,32 @@ public class Chapter15Test {
@Test
public void testJig() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
return 3.14;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 3.14;", stop.toString());
- assertEquals(3.14, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 3.14;", code._stop.toString());
+ assertEquals(3.14, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testCyclic() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct C { C? l; };
C !c = new C;
c.l = c;
return c;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return C;", stop.toString());
- assertEquals("Obj<C>@1{l=obj@1}", Evaluator.evaluate(stop, 0).toString());
+ code.parse().opto();
+ assertEquals("return C;", code._stop.toString());
+ assertEquals("Obj<C>@1{l=obj@1}", Evaluator.evaluate(code._stop, 0).toString());
}
@Test
public void testSafetyCheck() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
u8[] old = new u8[0];
u8[] !output = new u8[1];
@@ -46,66 +46,66 @@ while (i < old#) {
output[i] = 1;
return output;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return [u8];", stop.toString());
- assertEquals("\u0001", Evaluator.evaluate(stop, 0).toString());
+ code.parse().opto();
+ assertEquals("return [u8];", code._stop.toString());
+ assertEquals("\u0001", Evaluator.evaluate(code._stop, 0).toString());
}
@Test
public void testBasic1() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int[] is = new int[2];
return is[1];
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
+ assertEquals(0L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testBasic2() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int[] is = new int[2];
int[] is2 = new int[2];
return is[1];
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
+ assertEquals(0L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testBasic3() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int[] !a = new int[2];
a[0] = 1;
a[1] = 2;
return a[0];
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 1;", stop.toString());
- assertEquals(1L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 1;", code._stop.toString());
+ assertEquals(1L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testBasic4() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct A { int i; };
A?[] !a = new A?[2];
return a;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return [*A?];", stop.toString());
- assertEquals("Obj<[*A?]>{#=2,[]=[null,null]}", Evaluator.evaluate(stop, 0).toString());
+ code.parse().opto();
+ assertEquals("return [*A?];", code._stop.toString());
+ assertEquals("Obj<[*A?]>{#=2,[]=[null,null]}", Evaluator.evaluate(code._stop, 0).toString());
}
@Test
public void testBasic5() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct S { int x; flt y; };
// A new S
@@ -127,15 +127,15 @@ else {
}
return rez;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,1.2,Phi(Region,2.3,3.14));", stop.toString());
- assertEquals(3.14, Evaluator.evaluate(stop, 0));
- assertEquals(1.2 , Evaluator.evaluate(stop, 1));
+ code.parse().opto();
+ assertEquals("return Phi(Region,1.2,Phi(Region,2.3,3.14));", code._stop.toString());
+ assertEquals(3.14, Evaluator.evaluate(code._stop, 0));
+ assertEquals(1.2 , Evaluator.evaluate(code._stop, 1));
}
@Test
public void testBasic6() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct S { int x; flt y; };
// A new S
@@ -153,13 +153,13 @@ if( iss[arg] )
rez = iss[arg][2].y;
return rez;
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto().typeCheck(); fail(); }
catch( Exception e ) { assertEquals("Might be null accessing 'y'",e.getMessage()); }
}
@Test
public void testTree() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
// Can we define a forward-reference array?
struct Tree { Tree?[]? _kids; };
@@ -168,25 +168,25 @@ root._kids = new Tree?[2]; // NO BANG SO ARRAY IS OF IMMUTABLE TREES????
root._kids[0] = new Tree;
return root;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Tree;", stop.toString());
- assertEquals("Obj<Tree>{_kids=Obj<[*Tree?]>{#=2,[]=[Obj<Tree>{_kids=null},null]}}", Evaluator.evaluate(stop, 0).toString());
+ code.parse().opto();
+ assertEquals("return Tree;", code._stop.toString());
+ assertEquals("Obj<Tree>{_kids=Obj<[*Tree?]>{#=2,[]=[Obj<Tree>{_kids=null},null]}}", Evaluator.evaluate(code._stop, 0).toString());
}
@Test
public void testNestedStructAddMemProj() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct S { int a; int[] b; };
return 0;
""");
- StopNode stop = parser.parse();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
public void testRollingSum() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int[] !ary = new int[arg];
// Fill [0,1,2,3,4,...]
@@ -203,14 +203,14 @@ while( i < ary# - 1 ) {
}
return ary[1] * 1000 + ary[3]; // 1 * 1000 + 6
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return (.[]+(.[]*1000));", stop.toString());
- assertEquals(1006L, Evaluator.evaluate(stop, 4));
+ code.parse().opto();
+ assertEquals("return (.[]+(.[]*1000));", code._stop.toString());
+ assertEquals(1006L, Evaluator.evaluate(code._stop, 4));
}
@Test
public void sieveOEratosthenes() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int[] !ary = new int[arg], !primes = new int[arg];
int nprimes = 0, p=2;
@@ -244,9 +244,9 @@ while( j < nprimes ) {
}
return rez;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return [int];", stop.toString());
- Evaluator.Obj obj = (Evaluator.Obj)Evaluator.evaluate(stop, 20);
+ code.parse().opto();
+ assertEquals("return [int];", code._stop.toString());
+ Evaluator.Obj obj = (Evaluator.Obj)Evaluator.evaluate(code._stop, 20);
assertEquals("[int] {int #; int ![]; }",obj.struct().toString());
long nprimes = (Long)obj.fields()[0];
long[] primes = new long[]{2,3,5,7,11,13,17,19};
@@ -256,7 +256,7 @@ return rez;
@Test
public void testNewNodeInit() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct S {int i; flt f;};
S !s1 = new S;
@@ -266,62 +266,62 @@ s2.f = 2.0;
if (arg) s1 = new S;
return s1.i + s1.f;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return ((flt).i+.f);", stop.toString());
- assertEquals(0.0, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return ((flt).i+.f);", code._stop.toString());
+ assertEquals(0.0, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testBad0() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
return new flt;
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto(); fail(); }
catch( Exception e ) { assertEquals("Cannot allocate a flt",e.getMessage()); }
}
@Test
public void testBad1() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int is = new int[2];
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto(); fail(); }
catch( Exception e ) { assertEquals("Type *[int] is not of declared type int",e.getMessage()); }
}
@Test
public void testBad2() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int[] is = new int[3.14];
return is[1];
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto(); fail(); }
catch( Exception e ) { assertEquals("Cannot allocate an array with length 3.14",e.getMessage()); }
}
@Test
public void testBad3() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int[] is = new int[arg];
return is[1];
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 4));
- try { Evaluator.evaluate(stop,0); }
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
+ assertEquals(0L, Evaluator.evaluate(code._stop, 4));
+ try { Evaluator.evaluate(code._stop,0); }
catch( ArrayIndexOutOfBoundsException e ) { assertEquals("Array index 1 out of bounds for array length 0",e.getMessage()); }
- try { Evaluator.evaluate(stop,-1); }
+ try { Evaluator.evaluate(code._stop,-1); }
catch( NegativeArraySizeException e ) { assertEquals("-1",e.getMessage()); }
}
@Test
public void testProgress() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
i8 v1=0&0;
u8 v2=0;
byte v4=0;
@@ -340,14 +340,14 @@ while(v5+(0&0)) {
}
return v1;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
+ assertEquals(0L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testSharpNot() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
if(0>>0) {}
while(0) {}
@@ -355,14 +355,14 @@ u32 v7=0;
int v8=0;
while(0<--1>>>---(v7*0==v8)) {}
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
@Test
public void testProgress2() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
if(1) {}
else {
@@ -379,7 +379,7 @@ else {
}
}
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
}
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter16Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter16Test.java
index 5450824a..255a721a 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter16Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter16Test.java
@@ -10,180 +10,180 @@ public class Chapter16Test {
@Test
public void testJig() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
return 3.14;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 3.14;", stop.toString());
- assertEquals(3.14, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 3.14;", code._stop.toString());
+ assertEquals(3.14, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testMulti0() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int x, y;
return x+y;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
+ assertEquals(0L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testMulti1() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int x=2, y=x+1;
return x+y;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 5;", stop.toString());
- assertEquals(5L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 5;", code._stop.toString());
+ assertEquals(5L, Evaluator.evaluate(code._stop, 0));
}
// @Test
// public void testFinal0() {
-// Parser parser = new Parser(
+// CodeGen code = new CodeGen(
//"""
//int !x=2;
//x=3;
//return x;
//""");
-// try { parser.parse().iterate(); fail(); }
+// try { code.parse().opto(); fail(); }
// catch( Exception e ) { assertEquals("Cannot reassign final 'x'",e.getMessage()); }
// }
@Test
public void testFinal1() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
int x=2, y=3;
if( arg ) { int x = y; x = x*x; y=x; } // Shadow final x
return y;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,9,3);", stop.toString());
- assertEquals(3L, Evaluator.evaluate(stop, 0));
- assertEquals(9L, Evaluator.evaluate(stop, 1));
+ code.parse().opto();
+ assertEquals("return Phi(Region,9,3);", code._stop.toString());
+ assertEquals(3L, Evaluator.evaluate(code._stop, 0));
+ assertEquals(9L, Evaluator.evaluate(code._stop, 1));
}
@Test
public void testConstruct0() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct X { int x=3; };
X z = new X;
return z.x;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 3;", stop.toString());
- assertEquals(3L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 3;", code._stop.toString());
+ assertEquals(3L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testConstruct1() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct X { int !x; };
X z = new X { x=3; };
return z.x;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 3;", stop.toString());
- assertEquals(3L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 3;", code._stop.toString());
+ assertEquals(3L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testConstruct2() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct X { int x=3; };
X z = new X { x = 4; };
return z.x;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 4;", stop.toString());
- assertEquals(4L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 4;", code._stop.toString());
+ assertEquals(4L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testStructFinal0() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Point { int !x, !y; };
Point p = new Point { x=3; y=4; };
return p;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return (const)Point;", stop.toString());
- assertEquals("Obj<Point>{x=3,y=4}", Evaluator.evaluate(stop, 0).toString());
+ code.parse().opto();
+ assertEquals("return (const)Point;", code._stop.toString());
+ assertEquals("Obj<Point>{x=3,y=4}", Evaluator.evaluate(code._stop, 0).toString());
}
@Test
public void testStructFinal1() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Point { int x=3, y=4; };
Point p = new Point { x=5; y=6; };
p.x++;
return p;
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto().typeCheck(); fail(); }
catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
}
@Test
public void testStructFinal2() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Point { int x=3, y=4; };
Point p = new Point;
p.x++;
return p;
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto().typeCheck(); fail(); }
catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
}
@Test
public void testStructFinal3() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Point { var x; var y; };
Point p = new Point;
p.x++;
return p;
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto(); fail(); }
catch( Exception e ) { assertEquals("'Point' is not fully initialized, field 'x' needs to be set in a constructor",e.getMessage()); }
}
@Test
public void testStructFinal4() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Point { val x=3; val y=4; };
Point p = new Point;
p.x++;
return p;
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto(); fail(); }
catch( Exception e ) { assertEquals("Cannot reassign final 'x'",e.getMessage()); }
}
@Test
public void testStructFinal5() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Point { var x=3; var y=4; };
Point !p = new Point;
p.x++;
return p;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Point;", stop.toString());
- assertEquals("Obj<Point>{x=4,y=4}", Evaluator.evaluate(stop, 0).toString());
+ code.parse().opto();
+ assertEquals("return Point;", code._stop.toString());
+ assertEquals("Obj<Point>{x=4,y=4}", Evaluator.evaluate(code._stop, 0).toString());
}
// Same as the Chapter13 test with the same name, but using the new
// constructor syntax
@Test
public void testLinkedList1() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct LLI { LLI? next; int i; };
LLI? !head = null;
@@ -196,16 +196,16 @@ LLI? next = head.next;
if( !next ) return 1;
return next.i;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("Stop[ return 0; return 1; return .i; ]", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 0));
- assertEquals(1L, Evaluator.evaluate(stop, 1));
- assertEquals(2L, Evaluator.evaluate(stop, 3));
+ code.parse().opto();
+ assertEquals("return Phi(Region,0,1,.i);", code._stop.toString());
+ assertEquals(0L, Evaluator.evaluate(code._stop, 0));
+ assertEquals(1L, Evaluator.evaluate(code._stop, 1));
+ assertEquals(2L, Evaluator.evaluate(code._stop, 3));
}
@Test
public void testLinkedList2() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct LLI { LLI? next; int i; };
LLI? !head = null;
@@ -227,16 +227,16 @@ LLI? next = head.next;
if( !next ) return 1;
return next.i;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("Stop[ return 0; return 1; return .i; ]", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 0));
- assertEquals(1L, Evaluator.evaluate(stop, 1));
- assertEquals(2L, Evaluator.evaluate(stop, 11));
+ code.parse().opto();
+ assertEquals("return Phi(Region,0,1,.i);", code._stop.toString());
+ assertEquals(0L, Evaluator.evaluate(code._stop, 0));
+ assertEquals(1L, Evaluator.evaluate(code._stop, 1));
+ assertEquals(2L, Evaluator.evaluate(code._stop, 11));
}
@Test
public void testSquare() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
struct Square {
flt !side = arg;
@@ -252,9 +252,9 @@ struct Square {
};
return new Square;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Square;", stop.toString());
- assertEquals("Obj<Square>{side=3.0,diag=1.7320508075688772}", Evaluator.evaluate(stop, 3).toString());
- assertEquals("Obj<Square>{side=4.0,diag=2.0}", Evaluator.evaluate(stop, 4).toString());
+ code.parse().opto();
+ assertEquals("return Square;", code._stop.toString());
+ assertEquals("Obj<Square>{side=3.0,diag=1.7320508075688772}", Evaluator.evaluate(code._stop, 3).toString());
+ assertEquals("Obj<Square>{side=4.0,diag=2.0}", Evaluator.evaluate(code._stop, 4).toString());
}
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter17Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter17Test.java
index 9a2676d3..06eb150d 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter17Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter17Test.java
@@ -11,187 +11,187 @@ public class Chapter17Test {
@Test
public void testJig() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int i = 0;
i=i=1;
return i;
//return 3.14;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 1;", stop.toString());
- assertEquals(1L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 1;", code._stop.toString());
+ assertEquals(1L, Evaluator.evaluate(code._stop, 0));
}
// ---------------------------------------------------------------
@Test
public void testInc0() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
return arg++;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return arg;", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return arg;", code._stop.toString());
+ assertEquals(0L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testInc1() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
return arg+++arg++;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return ((arg*2)+1);", stop.toString());
- assertEquals(1L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return ((arg*2)+1);", code._stop.toString());
+ assertEquals(1L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testInc2() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
// -(arg--)-(arg--)
return -arg---arg--;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return (-((arg*2)+-1));", stop.toString());
- assertEquals(1L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return (-((arg*2)+-1));", code._stop.toString());
+ assertEquals(1L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testInc3() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int[] !xs = new int[arg];
xs[0]++;
xs[1]++;
return xs[0];
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 1;", stop.toString());
- assertEquals(1L, Evaluator.evaluate(stop, 2));
+ code.parse().opto();
+ assertEquals("return 1;", code._stop.toString());
+ assertEquals(1L, Evaluator.evaluate(code._stop, 2));
}
@Test
public void testInc4() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
u8[] !xs = new u8[1];
xs[0]--;
return xs[0];
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 255;", stop.toString());
- assertEquals(255L, Evaluator.evaluate(stop, 2));
+ code.parse().opto();
+ assertEquals("return 255;", code._stop.toString());
+ assertEquals(255L, Evaluator.evaluate(code._stop, 2));
}
@Test
public void testInc5() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct S { u16 x; };
S !s = new S;
s.x--;
return s.x;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 65535;", stop.toString());
- assertEquals(65535L, Evaluator.evaluate(stop, 2));
+ code.parse().opto();
+ assertEquals("return 65535;", code._stop.toString());
+ assertEquals(65535L, Evaluator.evaluate(code._stop, 2));
}
@Test
public void testInc6() {
- Parser parser = new Parser("return --arg;");
- StopNode stop = parser.parse().iterate();
- assertEquals("return (arg+-1);", stop.toString());
- assertEquals(-1L, Evaluator.evaluate(stop, 0));
+ CodeGen code = new CodeGen("return --arg;");
+ code.parse().opto();
+ assertEquals("return (arg+-1);", code._stop.toString());
+ assertEquals(-1L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testInc7() {
- Parser parser = new Parser("u8 x=0; return --x;");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 255;", stop.toString());
- assertEquals(255L, Evaluator.evaluate(stop, 0));
+ CodeGen code = new CodeGen("u8 x=0; return --x;");
+ code.parse().opto();
+ assertEquals("return 255;", code._stop.toString());
+ assertEquals(255L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testInc8() {
- Parser parser = new Parser("int x; x+=2; return x+=3;");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 5;", stop.toString());
- assertEquals(5L, Evaluator.evaluate(stop, 0));
+ CodeGen code = new CodeGen("int x; x+=2; return x+=3;");
+ code.parse().opto();
+ assertEquals("return 5;", code._stop.toString());
+ assertEquals(5L, Evaluator.evaluate(code._stop, 0));
}
// ---------------------------------------------------------------
@Test public void testVar0() {
- Parser parser = new Parser("var d; return d;");
- try { parser.parse().iterate(); fail(); }
+ CodeGen code = new CodeGen("var d; return d;");
+ try { code.parse().opto(); fail(); }
catch( Exception e ) { assertEquals("Syntax error, expected =expression: ;",e.getMessage()); }
}
@Test public void testVar1() {
- Parser parser = new Parser("val d; return d;");
- try { parser.parse().iterate(); fail(); }
+ CodeGen code = new CodeGen("val d; return d;");
+ try { code.parse().opto(); fail(); }
catch( Exception e ) { assertEquals("Syntax error, expected =expression: ;",e.getMessage()); }
}
@Test public void testVar2() {
- Parser parser = new Parser("int x; x=3; x++; return x; // Ok, no initializer so x is mutable ");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 4;", stop.toString());
- assertEquals(4L, Evaluator.evaluate(stop, 0));
+ CodeGen code = new CodeGen("int x; x=3; x++; return x; // Ok, no initializer so x is mutable ");
+ code.parse().opto();
+ assertEquals("return 4;", code._stop.toString());
+ assertEquals(4L, Evaluator.evaluate(code._stop, 0));
}
@Test public void testVar3() {
- Parser parser = new Parser("int x=3; x++; return x; // Ok, primitive so x is mutable despite initializer ");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 4;", stop.toString());
- assertEquals(4L, Evaluator.evaluate(stop, 0));
+ CodeGen code = new CodeGen("int x=3; x++; return x; // Ok, primitive so x is mutable despite initializer ");
+ code.parse().opto();
+ assertEquals("return 4;", code._stop.toString());
+ assertEquals(4L, Evaluator.evaluate(code._stop, 0));
}
@Test public void testVar4() {
- Parser parser = new Parser("struct S{int x;}; S s; s=new S; s.x++; return s.x; // Ok, no initializer so x is mutable ");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 1;", stop.toString());
- assertEquals(1L, Evaluator.evaluate(stop, 0));
+ CodeGen code = new CodeGen("struct S{int x;}; S s; s=new S; s.x++; return s.x; // Ok, no initializer so x is mutable ");
+ code.parse().opto();
+ assertEquals("return 1;", code._stop.toString());
+ assertEquals(1L, Evaluator.evaluate(code._stop, 0));
}
@Test public void testVar5() {
- Parser parser = new Parser("struct S{int x;}; S s; s=new S{x=3;}; s.x++; return s.x; // Ok, no initializer so x is mutable ");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 4;", stop.toString());
- assertEquals(4L, Evaluator.evaluate(stop, 0));
+ CodeGen code = new CodeGen("struct S{int x;}; S s; s=new S{x=3;}; s.x++; return s.x; // Ok, no initializer so x is mutable ");
+ code.parse().opto();
+ assertEquals("return 4;", code._stop.toString());
+ assertEquals(4L, Evaluator.evaluate(code._stop, 0));
}
@Test public void testVar6() {
- Parser parser = new Parser("struct S{int x;}; S s=new S; s.x++; return s.x; // Error initializer so x is immutable ");
- try { parser.parse().iterate(); fail(); }
+ CodeGen code = new CodeGen("struct S{int x;}; S s=new S; s.x++; return s.x; // Error initializer so x is immutable ");
+ try { code.parse().opto().typeCheck(); fail(); }
catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
}
@Test public void testVar7() {
- Parser parser = new Parser("struct S{int x;}; S s=new S{x=3;}; s.x++; return s.x; // Error initializer so x is immutable ");
- try { parser.parse().iterate(); fail(); }
+ CodeGen code = new CodeGen("struct S{int x;}; S s=new S{x=3;}; s.x++; return s.x; // Error initializer so x is immutable ");
+ try { code.parse().opto().typeCheck(); fail(); }
catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
}
@Test public void testVar8() {
- Parser parser = new Parser("struct S{int x;}; S !s=new S; s.x++; return s.x; // Ok, has '!' so s.x is mutable ");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 1;", stop.toString());
- assertEquals(1L, Evaluator.evaluate(stop, 0));
+ CodeGen code = new CodeGen("struct S{int x;}; S !s=new S; s.x++; return s.x; // Ok, has '!' so s.x is mutable ");
+ code.parse().opto();
+ assertEquals("return 1;", code._stop.toString());
+ assertEquals(1L, Evaluator.evaluate(code._stop, 0));
}
@Test public void testVar9() {
- Parser parser = new Parser("struct S{int x;}; var s=new S; s.x++; return s.x; // Ok, has var so s.x is mutable ");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 1;", stop.toString());
- assertEquals(1L, Evaluator.evaluate(stop, 0));
+ CodeGen code = new CodeGen("struct S{int x;}; var s=new S; s.x++; return s.x; // Ok, has var so s.x is mutable ");
+ code.parse().opto();
+ assertEquals("return 1;", code._stop.toString());
+ assertEquals(1L, Evaluator.evaluate(code._stop, 0));
}
@Test public void testVar10() {
- Parser parser = new Parser("struct S{int x;}; val s=new S; s.x++; return s.x; // Error, has val so x is immutable ");
- try { parser.parse().iterate(); fail(); }
+ CodeGen code = new CodeGen("struct S{int x;}; val s=new S; s.x++; return s.x; // Error, has val so x is immutable ");
+ try { code.parse().opto().typeCheck(); fail(); }
catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
}
@Test public void testVar11() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Bar { int x; };
Bar !bar = new Bar;
bar.x = 3; // Ok, bar is mutable
@@ -204,12 +204,12 @@ foo.bar.x++; // Ok foo and foo.bar and foo.bar.x are all mutable
val xfoo = foo; // Throw away mutability
xfoo.bar.x++; // Error, cannot mutate through xfoo
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto().typeCheck(); fail(); }
catch( Exception e ) { assertEquals("Cannot modify final field 'x'",e.getMessage()); }
}
@Test public void testVar12() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Bar { int x; };
Bar !bar = new Bar;
bar.x = 3; // Ok, bar is mutable
@@ -225,24 +225,24 @@ foo.bar.x++; // Bumps to 5
int x5 = xfoo.bar.x; // Ok to read through xfoo, gets 5
return x4*10+x5;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 45;", stop.toString());
- assertEquals(45L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 45;", code._stop.toString());
+ assertEquals(45L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testVar13() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int i,i++;
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto(); fail(); }
catch( Exception e ) { assertEquals("Redefining name 'i'",e.getMessage()); }
}
@Test
public void testVar14() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct B {};
struct A { B b; };
A x = new A {
@@ -250,127 +250,127 @@ A x = new A {
b = new B;
};
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto(); fail(); }
catch( Exception e ) { assertEquals("Cannot read uninitialized field 'b'",e.getMessage()); }
}
@Test
public void testVar15() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct B {};
struct A { B b; };
return new A {
if (arg) b = new B; // Constructor ends with partial init of b
}.b;
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto(); fail(); }
catch( Exception e ) { assertEquals("'A' is not fully initialized, field 'b' needs to be set in a constructor",e.getMessage()); }
}
// ---------------------------------------------------------------
@Test
public void testTrinary0() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
return arg ? 1 : 2;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,1,2);", stop.toString());
- assertEquals(2L, Evaluator.evaluate(stop, 0));
- assertEquals(1L, Evaluator.evaluate(stop, 1));
+ code.parse().opto();
+ assertEquals("return Phi(Region,1,2);", code._stop.toString());
+ assertEquals(2L, Evaluator.evaluate(code._stop, 0));
+ assertEquals(1L, Evaluator.evaluate(code._stop, 1));
}
@Test
public void testTrinary1() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
return arg ? 0 : arg;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 0;", stop.toString());
- assertEquals(0L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
+ assertEquals(0L, Evaluator.evaluate(code._stop, 0));
}
@Test
public void testTrinary2() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Bar { int x; };
var b = arg ? new Bar : null;
return b ? b.x++ + b.x++ : -1;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,1,-1);", stop.toString());
- assertEquals(-1L, Evaluator.evaluate(stop, 0));
- assertEquals( 1L, Evaluator.evaluate(stop, 1));
+ code.parse().opto();
+ assertEquals("return Phi(Region,1,-1);", code._stop.toString());
+ assertEquals(-1L, Evaluator.evaluate(code._stop, 0));
+ assertEquals( 1L, Evaluator.evaluate(code._stop, 1));
}
@Test
public void testTrinary3() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Bar { int x; };
var b = arg ? new Bar;
return b ? b.x++ + b.x++ : -1;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,1,-1);", stop.toString());
- assertEquals(-1L, Evaluator.evaluate(stop, 0));
- assertEquals( 1L, Evaluator.evaluate(stop, 1));
+ code.parse().opto();
+ assertEquals("return Phi(Region,1,-1);", code._stop.toString());
+ assertEquals(-1L, Evaluator.evaluate(code._stop, 0));
+ assertEquals( 1L, Evaluator.evaluate(code._stop, 1));
}
@Test
public void testTrinary4() {
// This test case will benefit from an unzipping transformation
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct Bar { Bar? next; int x; };
var b = arg ? new Bar { next = (arg==2) ? new Bar{x=2;}; x=1; };
return b ? b.next ? b.next.x : b.x; // parses "b ? (b.next ? b.next.x : b.x) : 0"
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,Phi(Region,.x,.x),0);", stop.toString());
- assertEquals( 0L, Evaluator.evaluate(stop, 0));
- assertEquals( 1L, Evaluator.evaluate(stop, 1));
- assertEquals( 2L, Evaluator.evaluate(stop, 2));
+ code.parse().opto();
+ assertEquals("return Phi(Region,Phi(Region,.x,.x),0);", code._stop.toString());
+ assertEquals( 0L, Evaluator.evaluate(code._stop, 0));
+ assertEquals( 1L, Evaluator.evaluate(code._stop, 1));
+ assertEquals( 2L, Evaluator.evaluate(code._stop, 2));
}
@Test
public void testTrinary5() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
flt f=arg?1:1.2;
return f; // missing widening
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Region,1.0f,1.2);", stop.toString());
- assertEquals(1.2, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return Phi(Region,1.0f,1.2);", code._stop.toString());
+ assertEquals(1.2, Evaluator.evaluate(code._stop, 0));
}
// ---------------------------------------------------------------
@Test
public void testFor0() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int sum=0;
for( int i=0; i<arg; i++ )
sum += i;
return sum;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,0,(Phi_sum+Phi(Loop,0,(Phi_i+1))));", stop.toString());
- assertEquals(3L, Evaluator.evaluate(stop, 3));
- assertEquals(45L, Evaluator.evaluate(stop, 10));
+ code.parse().opto();
+ assertEquals("return Phi(Loop,0,(Phi_sum+Phi(Loop,0,(Phi_i+1))));", code._stop.toString());
+ assertEquals(3L, Evaluator.evaluate(code._stop, 3));
+ assertEquals(45L, Evaluator.evaluate(code._stop, 10));
}
@Test
public void testFor1() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int sum=0, i=0;
for( ; i<arg; i++ )
sum += i;
return sum;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,0,(Phi_sum+Phi(Loop,0,(Phi_i+1))));", stop.toString());
- assertEquals(3L, Evaluator.evaluate(stop, 3));
- assertEquals(45L, Evaluator.evaluate(stop, 10));
+ code.parse().opto();
+ assertEquals("return Phi(Loop,0,(Phi_sum+Phi(Loop,0,(Phi_i+1))));", code._stop.toString());
+ assertEquals(3L, Evaluator.evaluate(code._stop, 3));
+ assertEquals(45L, Evaluator.evaluate(code._stop, 10));
}
@Test
public void testFor2() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int sum=0;
for( int i=0; ; i++ ) {
if( i>=arg ) break;
@@ -378,41 +378,41 @@ for( int i=0; ; i++ ) {
}
return sum;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,0,(Phi_sum+Phi(Loop,0,(Phi_i+1))));", stop.toString());
- assertEquals(3L, Evaluator.evaluate(stop, 3));
- assertEquals(45L, Evaluator.evaluate(stop, 10));
+ code.parse().opto();
+ assertEquals("return Phi(Loop,0,(Phi_sum+Phi(Loop,0,(Phi_i+1))));", code._stop.toString());
+ assertEquals(3L, Evaluator.evaluate(code._stop, 3));
+ assertEquals(45L, Evaluator.evaluate(code._stop, 10));
}
@Test
public void testFor3() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int sum=0;
for( int i=0; i<arg; )
sum += i++;
return sum;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,0,(Phi_sum+Phi(Loop,0,(Phi_i+1))));", stop.toString());
- assertEquals(3L, Evaluator.evaluate(stop, 3));
- assertEquals(45L, Evaluator.evaluate(stop, 10));
+ code.parse().opto();
+ assertEquals("return Phi(Loop,0,(Phi_sum+Phi(Loop,0,(Phi_i+1))));", code._stop.toString());
+ assertEquals(3L, Evaluator.evaluate(code._stop, 3));
+ assertEquals(45L, Evaluator.evaluate(code._stop, 10));
}
@Test
public void testFor4() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
int sum=0;
for( int i=0; i<arg; i++ )
sum += i;
return i;
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto(); fail(); }
catch( Exception e ) { assertEquals("Undefined name 'i'",e.getMessage()); }
}
@Test
public void testFor5() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
for(;;arg++;) {}
""");
- try { parser.parse().iterate(); fail(); }
+ try { code.parse().opto(); fail(); }
catch( Exception e ) { assertEquals("Syntax error, expected Unexpected code after expression: ;",e.getMessage()); }
}
@@ -421,36 +421,36 @@ for(;;arg++;) {}
// ---------------------------------------------------------------
@Test
public void testForward0() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct A{
B? f1;
B? f2;
};
return new A;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return A;", stop.toString());
- assertEquals("Obj<A>{f1=null,f2=null}", Evaluator.evaluate(stop).toString());
+ code.parse().opto();
+ assertEquals("return A;", code._stop.toString());
+ assertEquals("Obj<A>{f1=null,f2=null}", Evaluator.evaluate(code._stop).toString());
}
@Test
public void testForward1() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct A{
B?[]? nil_array_of_b;
B?[] not_array_of_b = new B?[0];
};
return new A.not_array_of_b;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return (const)[*B?];", stop.toString());
- assertEquals("Obj<[*B?]>{#=0,[]=[]}", Evaluator.evaluate(stop).toString());
+ code.parse().opto();
+ assertEquals("return (const)[*B?];", code._stop.toString());
+ assertEquals("Obj<[*B?]>{#=0,[]=[]}", Evaluator.evaluate(code._stop).toString());
}
// ---------------------------------------------------------------
@Test
public void testLinkedList2() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct LLI { LLI? next; int i; };
LLI? !head = null;
while( arg-- )
@@ -461,14 +461,14 @@ for( ; ptr; ptr = ptr.next )
sum += ptr.i;
return sum;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return Phi(Loop,0,(Phi_sum+.i));", stop.toString());
- assertEquals(45L, Evaluator.evaluate(stop, 10));
+ code.parse().opto();
+ assertEquals("return Phi(Loop,0,(Phi_sum+.i));", code._stop.toString());
+ assertEquals(45L, Evaluator.evaluate(code._stop, 10));
}
@Test
public void sieveOfEratosthenes() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
var ary = new bool[arg], primes = new int[arg];
var nprimes=0, p=0;
@@ -492,9 +492,9 @@ for( int j=0; j<nprimes; j++ )
rez[j] = primes[j];
return rez;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return [int];", stop.toString());
- Evaluator.Obj obj = (Evaluator.Obj)Evaluator.evaluate(stop, 20);
+ code.parse().opto();
+ assertEquals("return [int];", code._stop.toString());
+ Evaluator.Obj obj = (Evaluator.Obj)Evaluator.evaluate(code._stop, 20);
assertEquals("[int] {int #; int ![]; }",obj.struct().toString());
long nprimes = (Long)obj.fields()[0];
long[] primes = new long[]{2,3,5,7,11,13,17,19};
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java
index 369a0c39..938feab0 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java
@@ -11,39 +11,41 @@ public class Chapter18Test {
@Test
public void testJig() {
- Parser parser = new Parser(
+ CodeGen code = new CodeGen(
"""
-return 3.14;
+0 ;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return 3.14;", stop.toString());
- assertEquals(3.14, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return 0;", code._stop.toString());
+ assertEquals(0L, Evaluator.evaluate(code._stop, 0));
}
+ /*
+ */
// ---------------------------------------------------------------
@Test
public void testType0() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
{int -> int}? x2 = null; // null function ptr
return x2;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return null;", stop.toString());
- assertNull( Evaluator.evaluate( stop, 0 ) );
+ code.parse().opto();
+ assertEquals("return null;", code._stop.toString());
+ assertNull( Evaluator.evaluate( code._stop, 0 ) );
}
@Ignore @Test
public void testFcn0() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
{int -> int}? sq = { int x ->
x*x
};
return sq;
""");
- StopNode stop = parser.parse().iterate();
- assertEquals("return fcn;", stop.toString());
- assertEquals(1L, Evaluator.evaluate(stop, 0));
+ code.parse().opto();
+ assertEquals("return fcn;", code._stop.toString());
+ assertEquals(1L, Evaluator.evaluate(code._stop, 0));
}
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/SchedulerTest.java b/chapter18/src/test/java/com/seaofnodes/simple/SchedulerTest.java
index c67217af..e869e133 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/SchedulerTest.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/SchedulerTest.java
@@ -20,7 +20,7 @@ public class SchedulerTest {
@Test
@Ignore
public void testJig() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct S {
int f;
}
@@ -31,14 +31,14 @@ if (arg) v.f=1;
return i;
""");
StopNode._disablePeephole = true;
- StopNode stop = parser.parse();//.iterate();
- var eval = new Evaluator(stop);
+ code.parse();//.opto();
+ var eval = new Evaluator(code._stop);
assertEquals(2L, eval.evaluate(1, 10));
}
@Test
public void testStoreInIf() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct S {
int f;
};
@@ -46,15 +46,15 @@ S !v0=new S;
if(arg) v0.f=1;
return v0;
""");
- StopNode stop = parser.parse().iterate();
- var eval = new Evaluator(stop);
+ code.parse().opto();
+ var eval = new Evaluator(code._stop);
assertObj(eval.evaluate(0, 10), "S", 0L);
assertObj(eval.evaluate(1, 10), "S", 1L);
}
@Test
public void testStoreInIf2() {
- Parser parser = new Parser("""
+ CodeGen code = new CodeGen("""
struct S {
int f;
};
@@ -65,8 +65,8 @@ i=v.f;
if (arg) v.f=1;
return i;
""");
- StopNode stop = parser.parse().iterate();
- var eval = new Evaluator(stop);
+ code.parse().opto();
+ var eval = new Evaluator(code._stop);
assertEquals(2L, eval.evaluate(0, 10));
assertEquals(2L, eval.evaluate(1, 10));
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/TypeTest.java b/chapter18/src/test/java/com/seaofnodes/simple/TypeTest.java
index 6a1d737d..effd5db5 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/TypeTest.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/TypeTest.java
@@ -3,7 +3,6 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.type.*;
import org.junit.Assert;
import org.junit.Test;
-import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
public class TypeTest {
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 ac442585..ab413ed2 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/evaluator/Evaluator.java
@@ -310,6 +310,7 @@ public class Evaluator {
case ProjNode proj -> ((Object[])val( proj.in(0) ))[ proj._idx];
case ScopeMinNode mem -> null;
case ReadOnlyNode ro -> val(ro.in(1));
+ case CallNode call -> Utils.TODO(); // should not reach here, go the IfNode route
default -> throw new AssertionError("Unexpected node " + node);
};
}
@@ -329,31 +330,31 @@ public class Evaluator {
for(; i<block.nodes().length;i++) values[block.nodes()[i]._nid] = exec(block.nodes()[i]);
i=0;
switch (block.exit()) {
- case null:
- return Status.FALLTHROUGH;
- case ReturnNode ret:
- return val(ret.in(1));
- case IfNode ifn:
- block = block.next()[isTrue(val(ifn.in(1))) ? 0 : 1];
- if (block == null) return Status.FALLTHROUGH;
- break;
- case RegionNode region:
- if (loops-- <= 0) return Status.TIMEOUT;
- int exit = block.exitId();
- assert exit > 0 && region.nIns() > exit;
- block = block.next()[0];
- assert block != null;
- for (; i < block.nodes().length; i++) {
- if (!(block.nodes()[i] instanceof PhiNode)) break;
- phiCache.add(val(block.nodes()[i].in(exit)));
- }
- for (i=0; i<phiCache.size(); i++) {
- values[block.nodes()[i]._nid] = phiCache.get(i);
- }
- phiCache.clear();
- break;
- default:
- throw Utils.TODO("Unexpected control node " + block.exit());
+ case null:
+ return Status.FALLTHROUGH;
+ case ReturnNode ret : return val(ret .expr());
+ case StopNode stop: return val(stop.expr());
+ case IfNode ifn:
+ block = block.next()[isTrue(val(ifn.in(1))) ? 0 : 1];
+ if (block == null) return Status.FALLTHROUGH;
+ break;
+ case RegionNode region:
+ if (loops-- <= 0) return Status.TIMEOUT;
+ int exit = block.exitId();
+ assert exit > 0 && region.nIns() > exit;
+ block = block.next()[0];
+ assert block != null;
+ for (; i < block.nodes().length; i++) {
+ if (!(block.nodes()[i] instanceof PhiNode)) break;
+ phiCache.add(val(block.nodes()[i].in(exit)));
+ }
+ for (i=0; i<phiCache.size(); i++) {
+ values[block.nodes()[i]._nid] = phiCache.get(i);
+ }
+ phiCache.clear();
+ break;
+ default:
+ throw Utils.TODO("Unexpected control node " + block.exit());
}
}
}
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/evaluator/Scheduler.java b/chapter18/src/test/java/com/seaofnodes/simple/evaluator/Scheduler.java
index 06169e2c..8b13f12f 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/evaluator/Scheduler.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/evaluator/Scheduler.java
@@ -363,30 +363,21 @@ public class Scheduler {
var data = queue.pop();
var node = data.node;
assert node instanceof CFGNode;
- BasicBlock block;
- switch (node) {
- case StartNode s:
- block = new BasicBlock(s, depth++);
- break;
- case LoopNode l:
- block = new BasicBlock(l, depth++, d(l.in(1)).block);
- break;
- case RegionNode r:
+ BasicBlock block = switch (node) {
+ case StartNode s -> new BasicBlock(s, depth++);
+ case LoopNode l -> new BasicBlock(l, depth++, d(l.in(1)).block);
+ case RegionNode r -> {
var prev = new ArrayList<BasicBlock>();
for(int i=1; i<r.nIns(); i++) {
od(r.in(i)).ifPresent(d->prev.add(d.block));
}
- block = new BasicBlock(r, depth++, prev.toArray(BasicBlock[]::new));
- break;
- case IfNode i:
- block = d(i.in(0)).block;
- break;
- case ReturnNode r:
- block = d(r.in(0)).block;
- break;
- default:
- block = new BasicBlock(node, depth++, d(node.in(0)).block);
+ yield new BasicBlock(r, depth++, prev.toArray(BasicBlock[]::new));
}
+ case IfNode i -> d(i.in(0)).block;
+ case ReturnNode ret -> d(ret .in(0)).block;
+ case StopNode stop -> d(stop.in(0)).block;
+ default -> new BasicBlock(node, depth++, d(node.in(0)).block);
+ };
data.block = block;
if (node instanceof RegionNode r) {
// Regions might have phis which need to be scheduled.
@@ -401,7 +392,7 @@ public class Scheduler {
}
}
}
- if (!(node instanceof ReturnNode))
+ if( !(node instanceof ReturnNode) && !(node instanceof StopNode) )
for (Node n : node._outputs)
if(n instanceof CFGNode && isCFGNodeReady(n) && d(n).block == null)
queue.push(d(n));
@@ -449,7 +440,7 @@ public class Scheduler {
var data = cfgQueue.pop();
node = data.node;
assert node instanceof CFGNode;
- if (!(node instanceof ReturnNode)) {
+ if (!(node instanceof ReturnNode) && !(node instanceof StopNode) ) {
for (var out : node._outputs) if (out!=null && out instanceof CFGNode && isNotXCtrl(out)) markAlive(cfgQueue, out, true);
}
for (var in : node._inputs) if(in!=null && !(in instanceof CFGNode) && isNotXCtrl(in)) markAlive(dataQueue, in, false);
@@ -506,6 +497,9 @@ public class Scheduler {
for(var n:node._outputs) if (n instanceof CProjNode p && p._idx==0) return p;
return null;
}
+ // From start, a Call, or one of many functions
+ if( node instanceof CProjNode c && c.in(0) instanceof StartNode )
+ return c.out(0);
assert node._outputs.stream().filter(x -> x instanceof CFGNode).limit(2).count()<=1;
for(var n:node._outputs) if(n instanceof CFGNode) return n;
return null;
@@ -550,6 +544,7 @@ public class Scheduler {
break;
}
if (last instanceof ReturnNode) break;
+ if( last instanceof StopNode ) break;
if (last instanceof IfNode if_) {
for(var out:if_._outputs) if (out instanceof CFGNode && blocks.get(out) == null) queue.push(d(out));
break;
@@ -561,7 +556,8 @@ public class Scheduler {
case null -> new Block(arr, null, -1, new Block[0]);
case IfNode i -> new Block(arr, i, -1, new Block[2]);
case RegionNode r -> new Block(arr, r, r._inputs.find(prev), new Block[1]);
- case ReturnNode r -> new Block(arr, r, -1, new Block[0]);
+ case ReturnNode r -> new Block(arr, r , -1, new Block[0]);
+ case StopNode stop-> new Block(arr, stop, -1, new Block[0]);
default -> throw new AssertionError("Unexpected block exit node");
});
}
@@ -576,7 +572,7 @@ public class Scheduler {
block.next[0] = blocks.get(r);
break;
default:
- assert block.exit instanceof ReturnNode;
+ assert block.exit instanceof ReturnNode || block.exit instanceof StopNode;
}
}
// And return the block for the start node.
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/fuzzer/Fuzzer.java b/chapter18/src/test/java/com/seaofnodes/simple/fuzzer/Fuzzer.java
index e86fb7b8..6f5307bc 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/fuzzer/Fuzzer.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/fuzzer/Fuzzer.java
@@ -1,10 +1,9 @@
package com.seaofnodes.simple.fuzzer;
-import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.CodeGen;
import com.seaofnodes.simple.evaluator.Evaluator;
import com.seaofnodes.simple.node.Node;
import com.seaofnodes.simple.node.StopNode;
-
import java.util.ArrayList;
import java.util.Objects;
import java.util.Random;
@@ -137,9 +136,9 @@ public class Fuzzer {
var sb = new StringBuilder();
new ScriptGenerator(rand, sb, true).genProgram();
try {
- var parser = new Parser(sb.toString());
+ var code = new CodeGen(sb.toString());
- var stop = parser.parse();
+ code.parse();
int parse_peeps= Node.ITER_CNT;
int parse_nops = Node.ITER_NOP_CNT;
double parse_nop_ratio = (double)parse_nops/parse_peeps;
@@ -148,7 +147,7 @@ public class Fuzzer {
double parse_peeps_per_node = (double)parse_peeps/nids;
- stop.iterate();
+ code.opto();
int iter_peeps= Node.ITER_CNT;
int iter_nops = Node.ITER_NOP_CNT;
double iter_nop_ratio = (double)iter_nops/iter_peeps;
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/fuzzer/FuzzerUtils.java b/chapter18/src/test/java/com/seaofnodes/simple/fuzzer/FuzzerUtils.java
index 18ef069a..1efe270c 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/fuzzer/FuzzerUtils.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/fuzzer/FuzzerUtils.java
@@ -1,10 +1,10 @@
package com.seaofnodes.simple.fuzzer;
+import com.seaofnodes.simple.CodeGen;
import com.seaofnodes.simple.IterPeeps;
import com.seaofnodes.simple.Parser;
import com.seaofnodes.simple.node.Node;
import com.seaofnodes.simple.node.StopNode;
-
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.invoke.MethodHandle;
@@ -126,10 +126,10 @@ class FuzzerUtils {
} catch (Throwable e) {
throw rethrow(e);
}
- var parser = new Parser(script);
+ var code = new CodeGen(script);
Node._disablePeephole = !runPeeps;
- var stop = parser.parse();
- return runPeeps ? stop.iterate() : stop;
+ code.parse();
+ return runPeeps ? code.opto().typeCheck().codegen()._stop : code._stop;
} finally {
NodeWalkVisit.clear();
System.setErr(err);