summaryrefslogtreecommitdiff
path: root/chapter02/src/main
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-10-01 12:01:57 -0700
committerCliff Click <cliffc@acm.org>2026-10-01 12:01:57 -0700
commit5ea32e69433d0c850d02bbc1e6c4a260bc095fec (patch)
treeba4917ded278097332ee8f9f33227be74d3ac104 /chapter02/src/main
parentbf7f603dd4e8ec781fe578243cdc18180322420b (diff)
downloadSimple-5ea32e69433d0c850d02bbc1e6c4a260bc095fec.tar.gz
Simple-5ea32e69433d0c850d02bbc1e6c4a260bc095fec.zip
Move debug printing into shared space
Same as the graph viewer, moves a lot of code out of all Nodes
Diffstat (limited to 'chapter02/src/main')
-rw-r--r--chapter02/src/main/java/com/seaofnodes/simple/node/AddNode.java8
-rw-r--r--chapter02/src/main/java/com/seaofnodes/simple/node/ConstantNode.java15
-rw-r--r--chapter02/src/main/java/com/seaofnodes/simple/node/DivNode.java8
-rw-r--r--chapter02/src/main/java/com/seaofnodes/simple/node/MinusNode.java7
-rw-r--r--chapter02/src/main/java/com/seaofnodes/simple/node/MulNode.java8
-rw-r--r--chapter02/src/main/java/com/seaofnodes/simple/node/Node.java56
-rw-r--r--chapter02/src/main/java/com/seaofnodes/simple/node/ReturnNode.java6
-rw-r--r--chapter02/src/main/java/com/seaofnodes/simple/node/StartNode.java7
-rw-r--r--chapter02/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java28
9 files changed, 32 insertions, 111 deletions
diff --git a/chapter02/src/main/java/com/seaofnodes/simple/node/AddNode.java b/chapter02/src/main/java/com/seaofnodes/simple/node/AddNode.java
index 814b99b8..94aa4fbb 100644
--- a/chapter02/src/main/java/com/seaofnodes/simple/node/AddNode.java
+++ b/chapter02/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/chapter02/src/main/java/com/seaofnodes/simple/node/ConstantNode.java b/chapter02/src/main/java/com/seaofnodes/simple/node/ConstantNode.java
index 43a22426..cc1db452 100644
--- a/chapter02/src/main/java/com/seaofnodes/simple/node/ConstantNode.java
+++ b/chapter02/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/chapter02/src/main/java/com/seaofnodes/simple/node/DivNode.java b/chapter02/src/main/java/com/seaofnodes/simple/node/DivNode.java
index fc893e27..d1a554bc 100644
--- a/chapter02/src/main/java/com/seaofnodes/simple/node/DivNode.java
+++ b/chapter02/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/chapter02/src/main/java/com/seaofnodes/simple/node/MinusNode.java b/chapter02/src/main/java/com/seaofnodes/simple/node/MinusNode.java
index b83c41a5..11769935 100644
--- a/chapter02/src/main/java/com/seaofnodes/simple/node/MinusNode.java
+++ b/chapter02/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/chapter02/src/main/java/com/seaofnodes/simple/node/MulNode.java b/chapter02/src/main/java/com/seaofnodes/simple/node/MulNode.java
index b2b2f9c0..abae6925 100644
--- a/chapter02/src/main/java/com/seaofnodes/simple/node/MulNode.java
+++ b/chapter02/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/chapter02/src/main/java/com/seaofnodes/simple/node/Node.java b/chapter02/src/main/java/com/seaofnodes/simple/node/Node.java
index acf6b236..3c0c3905 100644
--- a/chapter02/src/main/java/com/seaofnodes/simple/node/Node.java
+++ b/chapter02/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,9 +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
@@ -217,7 +172,6 @@ public abstract class Node {
return null; // No progress
}
-
/**
* This function needs to be
* <a href="https://en.wikipedia.org/wiki/Monotonic_function">Monotonic</a>
diff --git a/chapter02/src/main/java/com/seaofnodes/simple/node/ReturnNode.java b/chapter02/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
index 7fa470f8..b48682ca 100644
--- a/chapter02/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
+++ b/chapter02/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/chapter02/src/main/java/com/seaofnodes/simple/node/StartNode.java b/chapter02/src/main/java/com/seaofnodes/simple/node/StartNode.java
index 99e240ad..12f35736 100644
--- a/chapter02/src/main/java/com/seaofnodes/simple/node/StartNode.java
+++ b/chapter02/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/chapter02/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java b/chapter02/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
index 502a2a8e..8f2f31ec 100644
--- a/chapter02/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
+++ b/chapter02/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
@@ -1,43 +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 2'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>();
- for( int i = 0; i < n.nIns(); i++ ) {
- String name = null;
- edges.add(new Edge(i, ref(n.in(i)), role(n, i), name));
- }
- return edges;
- }
-
- @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 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 ConstantNode ) return Role.ASSOC;
if( i == 0 ) return Role.CTRL;
return Role.DATA;