summaryrefslogtreecommitdiff
path: root/chapter03/src/main/java/com/seaofnodes
diff options
context:
space:
mode:
Diffstat (limited to 'chapter03/src/main/java/com/seaofnodes')
-rw-r--r--chapter03/src/main/java/com/seaofnodes/simple/node/AddNode.java8
-rw-r--r--chapter03/src/main/java/com/seaofnodes/simple/node/ConstantNode.java15
-rw-r--r--chapter03/src/main/java/com/seaofnodes/simple/node/DivNode.java8
-rw-r--r--chapter03/src/main/java/com/seaofnodes/simple/node/MinusNode.java7
-rw-r--r--chapter03/src/main/java/com/seaofnodes/simple/node/MulNode.java8
-rw-r--r--chapter03/src/main/java/com/seaofnodes/simple/node/Node.java56
-rw-r--r--chapter03/src/main/java/com/seaofnodes/simple/node/ReturnNode.java6
-rw-r--r--chapter03/src/main/java/com/seaofnodes/simple/node/ScopeNode.java20
-rw-r--r--chapter03/src/main/java/com/seaofnodes/simple/node/StartNode.java7
-rw-r--r--chapter03/src/main/java/com/seaofnodes/simple/node/SubNode.java8
-rw-r--r--chapter03/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java29
11 files changed, 47 insertions, 125 deletions
diff --git a/chapter03/src/main/java/com/seaofnodes/simple/node/AddNode.java b/chapter03/src/main/java/com/seaofnodes/simple/node/AddNode.java
index 814b99b8..94aa4fbb 100644
--- a/chapter03/src/main/java/com/seaofnodes/simple/node/AddNode.java
+++ b/chapter03/src/main/java/com/seaofnodes/simple/node/AddNode.java
@@ -1,5 +1,6 @@
package com.seaofnodes.simple.node;
+
import com.seaofnodes.simple.type.*;
public class AddNode extends Node {
@@ -7,12 +8,7 @@ public class AddNode extends Node {
@Override public String label() { return "Add"; }
- @Override
- StringBuilder _print1(StringBuilder sb) {
- in(1)._print0(sb.append("("));
- in(2)._print0(sb.append("+"));
- return sb.append(")");
- }
+ @Override protected String format() { return "(%1+%2)"; }
@Override
diff --git a/chapter03/src/main/java/com/seaofnodes/simple/node/ConstantNode.java b/chapter03/src/main/java/com/seaofnodes/simple/node/ConstantNode.java
index 43a22426..cc1db452 100644
--- a/chapter03/src/main/java/com/seaofnodes/simple/node/ConstantNode.java
+++ b/chapter03/src/main/java/com/seaofnodes/simple/node/ConstantNode.java
@@ -1,5 +1,7 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.print.ExprPrinter;
+
import com.seaofnodes.simple.Parser;
import com.seaofnodes.simple.type.Type;
@@ -22,15 +24,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) {
- return _con.print(sb);
+
+ @Override protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) {
+ return p.p(_con);
}
@Override
diff --git a/chapter03/src/main/java/com/seaofnodes/simple/node/DivNode.java b/chapter03/src/main/java/com/seaofnodes/simple/node/DivNode.java
index fc893e27..d1a554bc 100644
--- a/chapter03/src/main/java/com/seaofnodes/simple/node/DivNode.java
+++ b/chapter03/src/main/java/com/seaofnodes/simple/node/DivNode.java
@@ -1,5 +1,6 @@
package com.seaofnodes.simple.node;
+
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
@@ -8,12 +9,7 @@ public class DivNode extends Node {
@Override public String label() { return "Div"; }
- @Override
- StringBuilder _print1(StringBuilder sb) {
- in(1)._print0(sb.append("("));
- in(2)._print0(sb.append("/"));
- return sb.append(")");
- }
+ @Override protected String format() { return "(%1/%2)"; }
@Override
public Type compute() {
diff --git a/chapter03/src/main/java/com/seaofnodes/simple/node/MinusNode.java b/chapter03/src/main/java/com/seaofnodes/simple/node/MinusNode.java
index b83c41a5..11769935 100644
--- a/chapter03/src/main/java/com/seaofnodes/simple/node/MinusNode.java
+++ b/chapter03/src/main/java/com/seaofnodes/simple/node/MinusNode.java
@@ -1,5 +1,6 @@
package com.seaofnodes.simple.node;
+
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
@@ -8,11 +9,7 @@ public class MinusNode extends Node {
@Override public String label() { return "Minus"; }
- @Override
- StringBuilder _print1(StringBuilder sb) {
- in(1)._print0(sb.append("(-"));
- return sb.append(")");
- }
+ @Override protected String format() { return "(-%1)"; }
@Override
public Type compute() {
diff --git a/chapter03/src/main/java/com/seaofnodes/simple/node/MulNode.java b/chapter03/src/main/java/com/seaofnodes/simple/node/MulNode.java
index b2b2f9c0..abae6925 100644
--- a/chapter03/src/main/java/com/seaofnodes/simple/node/MulNode.java
+++ b/chapter03/src/main/java/com/seaofnodes/simple/node/MulNode.java
@@ -1,5 +1,6 @@
package com.seaofnodes.simple.node;
+
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
@@ -8,12 +9,7 @@ public class MulNode extends Node {
@Override public String label() { return "Mul"; }
- @Override
- StringBuilder _print1(StringBuilder sb) {
- in(1)._print0(sb.append("("));
- in(2)._print0(sb.append("*"));
- return sb.append(")");
- }
+ @Override protected String format() { return "(%1*%2)"; }
@Override
public Type compute() {
diff --git a/chapter03/src/main/java/com/seaofnodes/simple/node/Node.java b/chapter03/src/main/java/com/seaofnodes/simple/node/Node.java
index 3fa0fd1e..54687b2e 100644
--- a/chapter03/src/main/java/com/seaofnodes/simple/node/Node.java
+++ b/chapter03/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,7 +36,6 @@ 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.
@@ -56,7 +50,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<>();
@@ -68,43 +62,7 @@ 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() {
- // TODO: The print evolves in later chapters. For now, a simple
- // recursive print does well. Later with control flow and later still
- // with loops, we need some serious algorithm work to get a decent
- // print. So you should consider the "print" to always be a work-in-
- // progress as we march through the chapters.
- 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()).toString();
- }
- // This is the common print: check for DEAD and print "DEAD" else call the
- // per-Node print1.
- final StringBuilder _print0(StringBuilder sb) {
- return isDead()
- ? sb.append(uniqueName()).append(":DEAD")
- : _print1(sb);
- }
- // Every Node implements this.
- abstract StringBuilder _print1(StringBuilder sb);
-
+ @Override public String typeName() { return _type==null ? null : _type.toString(); }
/**
* Gets the ith input node
@@ -121,10 +79,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
diff --git a/chapter03/src/main/java/com/seaofnodes/simple/node/ReturnNode.java b/chapter03/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
index 7fa470f8..b48682ca 100644
--- a/chapter03/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
+++ b/chapter03/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
@@ -1,5 +1,7 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.print.ExprPrinter;
+
import com.seaofnodes.simple.type.*;
/**
@@ -24,8 +26,8 @@ public class ReturnNode extends Node {
public String label() { return "Return"; }
@Override
- StringBuilder _print1(StringBuilder sb) {
- return expr()._print0(sb.append("return ")).append(";");
+ protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) {
+ return p.p("return ").n(expr()).p(";");
}
@Override public boolean isCFG() { return true; }
diff --git a/chapter03/src/main/java/com/seaofnodes/simple/node/ScopeNode.java b/chapter03/src/main/java/com/seaofnodes/simple/node/ScopeNode.java
index 379ef033..95c25391 100644
--- a/chapter03/src/main/java/com/seaofnodes/simple/node/ScopeNode.java
+++ b/chapter03/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.*;
@@ -27,22 +29,22 @@ public class ScopeNode extends Node {
@Override public String label() { return "Scope"; }
@Override
- StringBuilder _print1(StringBuilder sb) {
- sb.append(label());
+ protected ExprPrinter<Node> _print1(ExprPrinter<Node> p) {
+ p.p(label());
for( HashMap<String,Integer> scope : _scopes ) {
- sb.append("[");
+ p.p("[");
boolean first=true;
for( String name : scope.keySet() ) {
- if( !first ) sb.append(", ");
+ if( !first ) p.p(", ");
first=false;
- sb.append(name).append(":");
+ p.p(name).p(":");
Node n = in(scope.get(name));
- if( n==null ) sb.append("null");
- else n._print0(sb);
+ if( n==null ) p.p("null");
+ else p.n(n);
}
- sb.append("]");
+ p.p("]");
}
- return sb;
+ return p;
}
/**
diff --git a/chapter03/src/main/java/com/seaofnodes/simple/node/StartNode.java b/chapter03/src/main/java/com/seaofnodes/simple/node/StartNode.java
index 99e240ad..12f35736 100644
--- a/chapter03/src/main/java/com/seaofnodes/simple/node/StartNode.java
+++ b/chapter03/src/main/java/com/seaofnodes/simple/node/StartNode.java
@@ -1,5 +1,7 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.print.ExprPrinter;
+
import com.seaofnodes.simple.type.Type;
/**
@@ -18,9 +20,8 @@ public class StartNode extends Node {
@Override
public String label() { return "Start"; }
- @Override
- StringBuilder _print1(StringBuilder sb) {
- 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/chapter03/src/main/java/com/seaofnodes/simple/node/SubNode.java b/chapter03/src/main/java/com/seaofnodes/simple/node/SubNode.java
index 1d2ddaa0..88106018 100644
--- a/chapter03/src/main/java/com/seaofnodes/simple/node/SubNode.java
+++ b/chapter03/src/main/java/com/seaofnodes/simple/node/SubNode.java
@@ -1,5 +1,6 @@
package com.seaofnodes.simple.node;
+
import com.seaofnodes.simple.type.Type;
import com.seaofnodes.simple.type.TypeInteger;
@@ -8,12 +9,7 @@ public class SubNode extends Node {
@Override public String label() { return "Sub"; }
- @Override
- StringBuilder _print1(StringBuilder sb) {
- in(1)._print0(sb.append("("));
- in(2)._print0(sb.append("-"));
- return sb.append(")");
- }
+ @Override protected String format() { return "(%1-%2)"; }
@Override
public Type compute() {
diff --git a/chapter03/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java b/chapter03/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
index a615741d..c97aaf55 100644
--- a/chapter03/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
+++ b/chapter03/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
@@ -1,45 +1,24 @@
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 3'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 GraphSnapshot.Node desc(Node n) {
- Projection proj = 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 ScopeNode ) return Kind.SCOPE;
if( n instanceof StartNode ) return Kind.CTRL;
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( i == 0 ) return Role.CTRL;
return Role.DATA;