summaryrefslogtreecommitdiff
path: root/chapter07/src/main/java/com/seaofnodes
diff options
context:
space:
mode:
Diffstat (limited to 'chapter07/src/main/java/com/seaofnodes')
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/IRPrinter.java124
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/AddNode.java9
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/BoolNode.java10
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/ConstantNode.java16
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/DivNode.java9
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/IfNode.java8
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/MinusNode.java8
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/MulNode.java9
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/Node.java80
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/NotNode.java8
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/PhiNode.java19
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/ProjNode.java5
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/RegionNode.java8
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/ReturnNode.java7
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/ScopeNode.java13
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/StartNode.java8
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/StopNode.java13
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/node/SubNode.java9
-rw-r--r--chapter07/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java30
19 files changed, 74 insertions, 319 deletions
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/IRPrinter.java b/chapter07/src/main/java/com/seaofnodes/simple/IRPrinter.java
deleted file mode 100644
index 2a6dbefd..00000000
--- a/chapter07/src/main/java/com/seaofnodes/simple/IRPrinter.java
+++ /dev/null
@@ -1,124 +0,0 @@
-package com.seaofnodes.simple;
-
-import com.seaofnodes.simple.node.LoopNode;
-import com.seaofnodes.simple.node.Node;
-
-import java.util.ArrayList;
-import java.util.BitSet;
-
-public class IRPrinter {
-
- // Another bulk pretty-printer. Makes more effort at basic-block grouping.
- public static String prettyPrint(Node node, int depth) {
- // First, a Breadth First Search at a fixed depth.
- BFS bfs = new BFS(node,depth);
- // Convert just that set to a post-order
- 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);
- // Reverse the post-order walk
- StringBuilder sb = new StringBuilder();
- boolean gap=false;
- for( int i=rpos.size()-1; i>=0; i-- ) {
- Node n = rpos.get(i);
- if( n.isCFG() || n.isMultiHead() ) {
- if( !gap ) sb.append("\n"); // Blank before multihead
- n._printLine(sb); // Print head
- while( --i >= 0 ) {
- Node t = rpos.get(i);
- if( !t.isMultiTail() ) { i++; break; }
- t._printLine(sb);
- }
- sb.append("\n"); // Blank after multitail
- gap = true;
- } else {
- n._printLine( sb );
- gap = false;
- }
- }
- return sb.toString();
- }
-
- private static void postOrd(Node n, ArrayList<Node> rpos, BitSet visit, BitSet bfs) {
- if( !bfs.get(n._nid) )
- return; // Not in the BFS visit
- if( visit.get(n._nid) ) return; // Already post-order walked
- visit.set(n._nid);
- // First walk the CFG, then everything
- if( n.isCFG() ) {
- for( Node use : n._outputs )
- if( use.isCFG() && use.nOuts()>=1 && !(use._outputs.get(0) instanceof LoopNode) )
- postOrd(use, rpos,visit,bfs);
- for( Node use : n._outputs )
- if( use.isCFG() )
- postOrd(use,rpos,visit,bfs);
- }
- for( Node use : n._outputs )
- postOrd(use, rpos,visit,bfs);
- // Post-order
- rpos.add(n);
- }
-
- // Breadth-first search, broken out in a class to keep in more independent.
- // Maintains a root-set of Nodes at the limit (or past by 1 if MultiHead).
- public static class BFS {
- // A breadth first search, plus MultiHeads for any MultiTails
- public final ArrayList<Node> _bfs;
- public final BitSet _bs; // Visited members by node id
- public final int _depth; // Depth limit
- public final int _lim; // From here to _bfs._len can be roots for a reverse search
- public BFS( Node base, int d ) {
- _depth = d;
- _bfs = new ArrayList<>();
- _bs = new BitSet();
-
- add(base); // Prime the pump
- int idx=0, lim=1; // Limit is where depth counter changes
- while( idx < _bfs.size() ) { // Ran out of nodes below depth
- Node n = _bfs.get(idx++);
- for( Node def : n._inputs )
- if( def!=null && !_bs.get(def._nid) )
- add(def);
- if( idx==lim ) { // Depth counter changes at limit
- if( --d < 0 )
- break; // Ran out of depth
- lim = _bfs.size(); // New depth limit
- }
- }
- // Toss things past the limit except multi-heads
- while( idx < _bfs.size() ) {
- Node n = _bfs.get(idx);
- if( n.isMultiHead() ) idx++;
- else del(idx);
- }
- // Root set is any node with no inputs in the visited set
- lim = _bfs.size();
- for( int i=_bfs.size()-1; i>=0; i-- )
- if( !any_visited(_bfs.get(i)) )
- swap( i,--lim);
- _lim = lim;
- }
- void swap( int x, int y ) {
- if( x==y ) return;
- Node tx = _bfs.get(x);
- Node ty = _bfs.get(y);
- _bfs.set(x,ty);
- _bfs.set(y,tx);
- }
- void add(Node n) {
- _bfs.add(n);
- _bs.set(n._nid);
- }
- void del(int idx) {
- Node n = Utils.del(_bfs, idx);
- _bs.clear(n._nid);
- }
- boolean any_visited( Node n ) {
- for( Node def : n._inputs )
- if( def!=null && _bs.get(def._nid) )
- return true;
- return false;
- }
- }
-}
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/AddNode.java b/chapter07/src/main/java/com/seaofnodes/simple/node/AddNode.java
index 84367cc7..5ee742f7 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/AddNode.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/AddNode.java
@@ -1,20 +1,15 @@
package com.seaofnodes.simple.node;
+
import com.seaofnodes.simple.type.*;
-import java.util.BitSet;
public class AddNode extends Node {
public AddNode(Node lhs, Node rhs) { super(null, lhs, rhs); }
@Override public String label() { return "Add"; }
- @Override
- StringBuilder _print1(StringBuilder sb, BitSet visited) {
- in(1)._print0(sb.append("("), visited);
- in(2)._print0(sb.append("+"), visited);
- return sb.append(")");
- }
+ @Override protected String format() { return "(%1+%2)"; }
@Override
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/BoolNode.java b/chapter07/src/main/java/com/seaofnodes/simple/node/BoolNode.java
index f5bbc9ad..e363bf75 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/BoolNode.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/BoolNode.java
@@ -1,9 +1,10 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.print.ExprPrinter;
+
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
-import java.util.BitSet;
abstract public class BoolNode extends Node {
@@ -16,11 +17,8 @@ abstract public class BoolNode extends Node {
@Override
public String label() { return getClass().getSimpleName(); }
- @Override
- StringBuilder _print1(StringBuilder sb, BitSet visited) {
- in(1)._print0(sb.append("("), visited);
- in(2)._print0(sb.append(op()), visited);
- return sb.append(")");
+ @Override protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) {
+ return p.open().n(in(1)).p(op()).n(in(2)).close();
}
@Override
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/ConstantNode.java b/chapter07/src/main/java/com/seaofnodes/simple/node/ConstantNode.java
index b7ec8292..19166ec6 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/ConstantNode.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/ConstantNode.java
@@ -1,9 +1,10 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.print.ExprPrinter;
+
import com.seaofnodes.simple.Parser;
import com.seaofnodes.simple.type.Type;
-import java.util.BitSet;
/**
* A Constant node represents a constant value. At present, the only constants
@@ -24,15 +25,16 @@ public class ConstantNode extends Node {
_con = type;
}
- @Override
- public String label() { return ""+_con; }
+ @Override protected String repeatName() {
+ return _con==null || _con.toString().length()<=32 ? null : uniqueName();
+ }
@Override
- public String uniqueName() { return "Con_" + _nid; }
+ public String label() { return "Con"; }
- @Override
- StringBuilder _print1(StringBuilder sb, BitSet visited) {
- return _con.print(sb);
+
+ @Override protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) {
+ return p.p(_con);
}
@Override public boolean isMultiTail() { return true; }
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/DivNode.java b/chapter07/src/main/java/com/seaofnodes/simple/node/DivNode.java
index f71a32db..4c3c2feb 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/DivNode.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/DivNode.java
@@ -1,21 +1,16 @@
package com.seaofnodes.simple.node;
+
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
-import java.util.BitSet;
public class DivNode extends Node {
public DivNode(Node lhs, Node rhs) { super(null, lhs, rhs); }
@Override public String label() { return "Div"; }
- @Override
- StringBuilder _print1(StringBuilder sb, BitSet visited) {
- in(1)._print0(sb.append("("), visited);
- in(2)._print0(sb.append("/"), visited);
- return sb.append(")");
- }
+ @Override protected String format() { return "(%1/%2)"; }
@Override
public Type compute() {
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/IfNode.java b/chapter07/src/main/java/com/seaofnodes/simple/node/IfNode.java
index 0f414c08..52d7e31e 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/IfNode.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/IfNode.java
@@ -1,10 +1,10 @@
package com.seaofnodes.simple.node;
+
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
import com.seaofnodes.simple.type.TypeTuple;
-import java.util.BitSet;
public class IfNode extends MultiNode {
@@ -15,11 +15,7 @@ public class IfNode extends MultiNode {
@Override
public String label() { return "If"; }
- @Override
- StringBuilder _print1(StringBuilder sb, BitSet visited) {
- sb.append("if( ");
- return in(1)._print0(sb, visited).append(" )");
- }
+ @Override protected String format() { return "if( %1 )"; }
@Override public boolean isCFG() { return true; }
@Override public boolean isMultiHead() { return true; }
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/MinusNode.java b/chapter07/src/main/java/com/seaofnodes/simple/node/MinusNode.java
index 9d3d3f5d..fb51777e 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/MinusNode.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/MinusNode.java
@@ -1,20 +1,16 @@
package com.seaofnodes.simple.node;
+
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
-import java.util.BitSet;
public class MinusNode extends Node {
public MinusNode(Node in) { super(null, in); }
@Override public String label() { return "Minus"; }
- @Override
- StringBuilder _print1(StringBuilder sb, BitSet visited) {
- in(1)._print0(sb.append("(-"), visited);
- return sb.append(")");
- }
+ @Override protected String format() { return "(-%1)"; }
@Override
public Type compute() {
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/MulNode.java b/chapter07/src/main/java/com/seaofnodes/simple/node/MulNode.java
index 4884ee18..2d0fa95b 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/MulNode.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/MulNode.java
@@ -1,21 +1,16 @@
package com.seaofnodes.simple.node;
+
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
-import java.util.BitSet;
public class MulNode extends Node {
public MulNode(Node lhs, Node rhs) { super(null, lhs, rhs); }
@Override public String label() { return "Mul"; }
- @Override
- StringBuilder _print1(StringBuilder sb, BitSet visited) {
- in(1)._print0(sb.append("("), visited);
- in(2)._print0(sb.append("*"), visited);
- return sb.append(")");
- }
+ @Override protected String format() { return "(%1*%2)"; }
@Override
public Type compute() {
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/Node.java b/chapter07/src/main/java/com/seaofnodes/simple/node/Node.java
index 921292cb..1430cdac 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/Node.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/Node.java
@@ -1,5 +1,7 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.print.BaseNode;
+
import com.seaofnodes.simple.Parser;
import com.seaofnodes.simple.Utils;
@@ -12,14 +14,7 @@ import java.util.*;
* The Node class provides common functionality used by all subtypes.
* Subtypes of Node specialize by overriding methods.
*/
-public abstract class Node {
-
- /**
- * Each node has a unique dense Node ID within a compilation context
- * The ID is useful for debugging, for using as an offset in a bitvector,
- * as well as for computing equality of nodes (to be implemented later).
- */
- public final int _nid;
+public abstract class Node extends BaseNode<Node> {
/**
* Inputs to the node. These are use-def references to Nodes.
@@ -41,14 +36,12 @@ public abstract class Node {
*/
public final ArrayList<Node> _outputs;
-
/**
* Current computed type for this Node. This value changes as the graph
* changes and more knowledge is gained about the program.
*/
public Type _type;
-
/**
* Immediate dominator tree depth, used to approximate a real IDOM during
* parsing where we do not have the whole program, and also peepholes
@@ -66,7 +59,7 @@ public abstract class Node {
private static int UNIQUE_ID = 1;
protected Node(Node... inputs) {
- _nid = UNIQUE_ID++; // allocate unique dense ID
+ super(UNIQUE_ID++); // allocate unique dense ID
_inputs = new ArrayList<>();
Collections.addAll(_inputs,inputs);
_outputs = new ArrayList<>();
@@ -78,70 +71,14 @@ public abstract class Node {
// Easy reading label for debugger, e.g. "Add" or "Region" or "EQ"
public abstract String label();
- // Unique label for debugging, e.g. "Add12" or "Region30" or "EQ99"
- public String uniqueName() { return label() + _nid; }
-
- // ------------------------------------------------------------------------
-
- // Debugger Printing.
-
- // {@code toString} is what you get in the debugger. It has to print 1
- // line (because this is what a debugger typically displays by default) and
- // has to be robust with broken graph/nodes.
- @Override
- public final String toString() { return print(); }
-
- // This is a *deep* print. This version will fail on cycles, which we will
- // correct later when we can parse programs with loops. We print with a
- // tik-tok style; the common _print0 calls the per-Node _print1, which
- // calls back to _print0;
- public final String print() {
- return _print0(new StringBuilder(), new BitSet()).toString();
- }
- // This is the common print: check for repeats, check for DEAD and print
- // "DEAD" else call the per-Node print1.
- final StringBuilder _print0(StringBuilder sb, BitSet visited) {
- if (visited.get(_nid))
- return sb.append(label());
- visited.set(_nid);
- return isDead()
- ? sb.append(uniqueName()).append(":DEAD")
- : _print1(sb, visited);
- }
- // Every Node implements this; a partial-line recursive print
- abstract StringBuilder _print1(StringBuilder sb, BitSet visited);
-
-
- // Print a node on 1 line, columnar aligned, as:
- // NNID NNAME DDEF DDEF [[ UUSE UUSE ]] TYPE
- // 1234 sssss 1234 1234 1234 1234 1234 1234 tttttt
- public void _printLine(StringBuilder sb ) {
- sb.append("%4d %-7.7s ".formatted(_nid,label()));
- if( isDead() ) {
- sb.append("DEAD\n");
- return;
- }
- for( Node def : _inputs )
- sb.append(def==null ? "____ " : "%4d ".formatted(def._nid));
- for( int i = _inputs.size(); i<3; i++ )
- sb.append(" ");
- sb.append(" [[ ");
- for( Node use : _outputs )
- sb.append("%4d ".formatted(use._nid));
- int lim = 5 - Math.max(_inputs.size(),3);
- for( int i = _outputs.size(); i<lim; i++ )
- sb.append(" ");
- sb.append(" ]] ");
- if( _type!= null ) _type.print(sb);
- sb.append("\n");
- }
-
public boolean isMultiHead() { return false; }
public boolean isMultiTail() { return false; }
// ------------------------------------------------------------------------
// Graph Node & Edge manipulation
+ @Override public String typeName() { return _type==null ? null : _type.toString(); }
+
/**
* Gets the ith input node
* @param i Offset of the input node
@@ -157,8 +94,6 @@ public abstract class Node {
public boolean isUnused() { return nOuts() == 0; }
- public boolean isCFG() { return false; }
-
/**
* Change a <em>def</em> into a Node. Keeps the edges correct, by removing
* the corresponding <em>use->def</em> edge. This may make the original
@@ -262,7 +197,6 @@ public abstract class Node {
// Remove bogus null.
public <N extends Node> N unkeep() { delUse(null); return (N)this; }
-
// Replace self with nnn in the graph, making 'this' go dead
void subsume( Node nnn ) {
assert nnn!=this;
@@ -404,7 +338,6 @@ public abstract class Node {
*/
public abstract Node idealize();
-
// ------------------------------------------------------------------------
// Peephole utilities
@@ -448,7 +381,6 @@ public abstract class Node {
return _idepth = (char)depth;
}
-
// Make a shallow copy (same class) of this Node, with given inputs and
// empty outputs and a new Node ID. The original inputs are ignored.
// Does not need to be implemented in isCFG() nodes.
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/NotNode.java b/chapter07/src/main/java/com/seaofnodes/simple/node/NotNode.java
index 4c0be167..538016bd 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/NotNode.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/NotNode.java
@@ -1,20 +1,16 @@
package com.seaofnodes.simple.node;
+
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
-import java.util.BitSet;
public class NotNode extends Node {
public NotNode(Node in) { super(null, in); }
@Override public String label() { return "Not"; }
- @Override
- StringBuilder _print1(StringBuilder sb, BitSet visited) {
- in(1)._print0(sb.append("(!"), visited);
- return sb.append(")");
- }
+ @Override protected String format() { return "(!%1)"; }
@Override
public Type compute() {
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/PhiNode.java b/chapter07/src/main/java/com/seaofnodes/simple/node/PhiNode.java
index 46646773..64cd3dd7 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/PhiNode.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/PhiNode.java
@@ -1,8 +1,9 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.print.ExprPrinter;
+
import com.seaofnodes.simple.type.Type;
-import java.util.BitSet;
public class PhiNode extends Node {
@@ -13,18 +14,12 @@ public class PhiNode extends Node {
@Override public String label() { return "Phi_"+_label; }
@Override
- StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) {
if( region().inProgress() )
- sb.append("Z");
- sb.append("Phi(");
- 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;
+ p.p("Z");
+ p.p("Phi(");
+ for( Node in : _inputs ) p.n(in).p(",");
+ return p.unchar(',').close();
}
RegionNode region() { return (RegionNode)in(0); }
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/ProjNode.java b/chapter07/src/main/java/com/seaofnodes/simple/node/ProjNode.java
index 3c3f73b8..605c73bd 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/ProjNode.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/ProjNode.java
@@ -1,9 +1,10 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.print.ExprPrinter;
+
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeTuple;
-import java.util.BitSet;
public class ProjNode extends Node {
@@ -23,7 +24,7 @@ public class ProjNode extends Node {
public String label() { return _label; }
@Override
- StringBuilder _print1(StringBuilder sb, BitSet visited) { return sb.append(_label); }
+ protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) { return p.p(_label); }
@Override public boolean isCFG() { return _idx==0 || ctrl() instanceof IfNode; }
@Override public boolean isMultiTail() { return in(0).isMultiHead(); }
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/RegionNode.java b/chapter07/src/main/java/com/seaofnodes/simple/node/RegionNode.java
index e357796b..e0dd45d7 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/RegionNode.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/RegionNode.java
@@ -1,9 +1,10 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.print.ExprPrinter;
+
import com.seaofnodes.simple.Utils;
import com.seaofnodes.simple.type.Type;
-import java.util.BitSet;
public class RegionNode extends Node {
@@ -12,9 +13,8 @@ public class RegionNode extends Node {
@Override
public String label() { return "Region"; }
- @Override
- StringBuilder _print1(StringBuilder sb, BitSet visited) {
- return sb.append(label()).append(_nid);
+ @Override protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) {
+ return p.p(label()).p(_nid);
}
@Override public boolean isCFG() { return true; }
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/ReturnNode.java b/chapter07/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
index 312058b9..9f4cad7a 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
@@ -1,8 +1,9 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.print.ExprPrinter;
+
import com.seaofnodes.simple.type.*;
-import java.util.BitSet;
/**
* The Return node has two inputs. The first input is a control node and the
@@ -26,8 +27,8 @@ public class ReturnNode extends Node {
public String label() { return "Return"; }
@Override
- StringBuilder _print1(StringBuilder sb, BitSet visited) {
- return expr()._print0(sb.append("return "), visited).append(";");
+ protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) {
+ return p.p("return ").n(expr()).p(";");
}
@Override public boolean isCFG() { return true; }
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/ScopeNode.java b/chapter07/src/main/java/com/seaofnodes/simple/node/ScopeNode.java
index a4fcbe65..bd6b0d8c 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/ScopeNode.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/ScopeNode.java
@@ -1,5 +1,7 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.print.ExprPrinter;
+
import com.seaofnodes.simple.type.Type;
import java.util.*;
@@ -33,16 +35,15 @@ public class ScopeNode extends Node {
@Override public String label() { return "Scope"; }
@Override
- StringBuilder _print1(StringBuilder sb, BitSet visited) {
- sb.append("Scope[ ");
+ protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) {
+ p.p("Scope[ ");
String[] names = reverseNames();
for( int j=0; j<nIns(); j++ ) {
- sb.append(names[j]).append(":");
+ p.p(names[j]).p(":");
Node n = in(j);
- n._print0(sb, visited).append(" ");
+ p.n(n).p(" ");
}
- sb.setLength(sb.length()-1);
- return sb.append("]");
+ return p.unchar(' ').p("]");
}
/**
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/StartNode.java b/chapter07/src/main/java/com/seaofnodes/simple/node/StartNode.java
index bab0f18c..56689503 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/StartNode.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/StartNode.java
@@ -1,9 +1,10 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.print.ExprPrinter;
+
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeTuple;
-import java.util.BitSet;
/**
* The Start node represents the start of the function. For now, we do not
@@ -25,9 +26,8 @@ public class StartNode extends MultiNode {
@Override
public String label() { return "Start"; }
- @Override
- StringBuilder _print1(StringBuilder sb, BitSet visited) {
- return sb.append(label());
+ @Override protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) {
+ return p.p(label());
}
@Override public boolean isCFG() { return true; }
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/StopNode.java b/chapter07/src/main/java/com/seaofnodes/simple/node/StopNode.java
index 417790d9..aa655f63 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/StopNode.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/StopNode.java
@@ -1,8 +1,9 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.print.ExprPrinter;
+
import com.seaofnodes.simple.type.Type;
-import java.util.BitSet;
public class StopNode extends Node {
public StopNode(Node... inputs) {
@@ -15,12 +16,12 @@ public class StopNode extends Node {
}
@Override
- StringBuilder _print1(StringBuilder sb, BitSet visited) {
- if( ret()!=null ) return ret()._print0(sb, visited);
- sb.append("Stop[ ");
+ protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) {
+ if( ret()!=null ) return p.n(ret());
+ p.p("Stop[ ");
for( Node ret : _inputs )
- ret._print0(sb, visited).append(" ");
- return sb.append("]");
+ p.n(ret).p(" ");
+ return p.p("]");
}
@Override public boolean isCFG() { return true; }
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/node/SubNode.java b/chapter07/src/main/java/com/seaofnodes/simple/node/SubNode.java
index 421e659b..bf938504 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/node/SubNode.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/node/SubNode.java
@@ -1,21 +1,16 @@
package com.seaofnodes.simple.node;
+
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
-import java.util.BitSet;
public class SubNode extends Node {
public SubNode(Node lhs, Node rhs) { super(null, lhs, rhs); }
@Override public String label() { return "Sub"; }
- @Override
- StringBuilder _print1(StringBuilder sb, BitSet visited) {
- in(1)._print0(sb.append("("), visited);
- in(2)._print0(sb.append("-"), visited);
- return sb.append(")");
- }
+ @Override protected String format() { return "(%1-%2)"; }
@Override
public Type compute() {
diff --git a/chapter07/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java b/chapter07/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
index 9f33520e..c5ab1c60 100644
--- a/chapter07/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
+++ b/chapter07/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
@@ -1,39 +1,19 @@
package com.seaofnodes.simple.print;
import com.seaofnodes.graph.GraphAdapter;
-import com.seaofnodes.graph.GraphSnapshot;
import com.seaofnodes.graph.GraphSnapshot.*;
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.node.Node;
-import java.util.ArrayList;
/** Chapter 7's view of the IR; browser, transport and layout are shared. */
public class SimpleGraphAdapter extends GraphAdapter<Node> {
- @Override protected boolean dead(Node n) { return n.isDead(); }
- @Override protected int id(Node n) { return n._nid; }
- @Override protected int nIns(Node n) { return n.nIns(); }
- @Override protected Node in(Node n, int idx) { return n.in(idx); }
- @Override protected int nOuts(Node n) { return n.nOuts(); }
- @Override protected Node out(Node n, int idx) { return n.out(idx); }
- private ArrayList<Edge> edges(Node n) {
- var edges = new ArrayList<Edge>();
- String[] names = n instanceof ScopeNode scope && n.nIns() != 0 ? scope.reverseNames() : null;
- for( int i = 0; i < n.nIns(); i++ ) {
- String name = names == null || i >= names.length ? null : names[i];
- edges.add(new Edge(i, ref(n.in(i)), role(n, i), name));
- }
- return edges;
+ @Override protected String[] edgeNames(Node n) {
+ return n instanceof ScopeNode scope && n.nIns()!=0 ? scope.reverseNames() : null;
}
+ @Override protected int projectionIndex(Node n) { return n instanceof ProjNode p ? p._idx : -1; }
- @Override protected GraphSnapshot.Node desc(Node n) {
- Projection proj = n instanceof ProjNode p ? new Projection(ref(n.in(0)), p._idx) : null;
- String label = n.label();
- return new GraphSnapshot.Node(n._nid, label == null ? n.getClass().getSimpleName() : label,
- n._type == null ? null : n._type.toString(), kind(n), edges(n), proj);
- }
-
- private Kind kind(Node n) {
+ @Override protected Kind kind(Node n) {
if( n instanceof StopNode ) return Kind.STOP;
if( n instanceof ScopeNode ) return Kind.SCOPE;
if( n instanceof StartNode ) return Kind.CTRL;
@@ -43,7 +23,7 @@ public class SimpleGraphAdapter extends GraphAdapter<Node> {
return n.isCFG() ? Kind.CTRL : Kind.DATA;
}
- private Role role(Node n, int i) {
+ @Override protected Role role(Node n, int i) {
if( n instanceof ScopeNode || n instanceof ConstantNode ) return Role.ASSOC;
if( n instanceof PhiNode ) return i == 0 ? Role.ASSOC : Role.DATA;
if( n instanceof ProjNode ) return n.isCFG() ? Role.CTRL : Role.DATA;