summaryrefslogtreecommitdiff
path: root/chapter12
diff options
context:
space:
mode:
Diffstat (limited to 'chapter12')
-rw-r--r--chapter12/Makefile8
-rw-r--r--chapter12/docs/12-grammar.md4
-rw-r--r--chapter12/pom.xml3
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/GraphVisualizer.java280
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/IterPeeps.java13
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/Parser.java23
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/AddFNode.java2
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/AddNode.java2
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/BoolNode.java3
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/DivFNode.java2
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/DivNode.java2
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/LoadNode.java3
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/MinusFNode.java2
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/MinusNode.java2
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/MulFNode.java2
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/MulNode.java2
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/NewNode.java3
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/Node.java29
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/NotNode.java2
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/PhiNode.java2
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/StopNode.java3
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/StoreNode.java2
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/SubFNode.java2
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/SubNode.java2
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/ToFloatNode.java2
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java62
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/print/SimpleGraphObserver.java43
-rw-r--r--chapter12/src/test/java/com/seaofnodes/simple/Chapter12Test.java8
-rw-r--r--chapter12/src/test/java/com/seaofnodes/simple/SchedulerTest.java2
29 files changed, 155 insertions, 360 deletions
diff --git a/chapter12/Makefile b/chapter12/Makefile
index ed0a03c9..77df7182 100644
--- a/chapter12/Makefile
+++ b/chapter12/Makefile
@@ -36,6 +36,7 @@ CLZDIR:= build/classes
main_javas := $(wildcard $(SRC)/$(SIMPLE)/*java $(SRC)/$(SIMPLE)/*/*java)
test_javas := $(wildcard $(TST)/$(SIMPLE)/*java $(TST)/$(SIMPLE)/*/*java)
main_classes := $(patsubst $(SRC)/%java,$(CLZDIR)/main/%class,$(main_javas))
+include $(firstword $(wildcard ../graph/graph.mk graph/graph.mk))
test_classes := $(patsubst $(TST)/%java,$(CLZDIR)/test/%class,$(test_javas))
test_cp := $(patsubst $(TST)/$(SIMPLE)/%.java,com.seaofnodes.simple.%,$(wildcard $(TST)/$(SIMPLE)/*Test.java))
classes = $(main_classes) $(test_classes)
@@ -53,7 +54,7 @@ endif
default: $(default_targets)
# Compile just the out-of-date files
-$(main_classes): build/classes/main/%class: $(SRC)/%java
+$(main_classes): build/classes/main/%class: $(SRC)/%java $(graph_javas)
@echo "compiling " $@ " because " $?
@[ -d $(CLZDIR)/main ] || mkdir -p $(CLZDIR)/main
@javac $(JAVAC_ARGS) -cp "$(CLZDIR)/main$(SEP)$(jars)" -sourcepath $(SRC) -d $(CLZDIR)/main $(main_javas)
@@ -98,4 +99,7 @@ tags: $(main_javas) $(test_javas)
@rm -f TAGS
@$(CTAGS) -e --recurse=yes --extra=+q --fields=+fksaiS $(SRC) $(TST)
-include ../build-support/chapter-release.mk
+include $(firstword $(wildcard ../build-support/chapter-release.mk build-support/chapter-release.mk))
+
+.PHONY: build
+build: $(main_classes)
diff --git a/chapter12/docs/12-grammar.md b/chapter12/docs/12-grammar.md
index f0fe2e40..4c071a98 100644
--- a/chapter12/docs/12-grammar.md
+++ b/chapter12/docs/12-grammar.md
@@ -17,7 +17,6 @@ statement
| whileStatement
| breakStatement
| continueStatment
- | metaStatement
;
PRIMTYPE
@@ -53,9 +52,6 @@ ifStatement
: 'if' '(' expression ')' statement ('else' statement)?
;
-metaStatement
- : '#showGraph' ';'
- ;
expressionStatement
: IDENTIFIER '=' expression ';'
diff --git a/chapter12/pom.xml b/chapter12/pom.xml
index c46bf13d..f70175a8 100644
--- a/chapter12/pom.xml
+++ b/chapter12/pom.xml
@@ -11,4 +11,7 @@
<artifactId>chapter12</artifactId>
<packaging>jar</packaging>
<name>Chapter 12</name>
+ <properties>
+ <simple.graph.directory>${project.basedir}/../graph</simple.graph.directory>
+ </properties>
</project>
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/GraphVisualizer.java b/chapter12/src/main/java/com/seaofnodes/simple/GraphVisualizer.java
deleted file mode 100644
index 604770a4..00000000
--- a/chapter12/src/main/java/com/seaofnodes/simple/GraphVisualizer.java
+++ /dev/null
@@ -1,280 +0,0 @@
-package com.seaofnodes.simple;
-
-import com.seaofnodes.simple.Parser;
-import com.seaofnodes.simple.node.*;
-
-import java.util.*;
-
-/**
- * Simple visualizer that outputs GraphViz dot format.
- * The dot output must be saved to a file and run manually via dot to generate the SVG output.
- * Currently, this is done manually.
- */
-public class GraphVisualizer {
-
- /**
- * If set to true we put the control nodes in a separate cluster from
- * data nodes.
- */
- boolean _separateControlCluster = false;
-
- public GraphVisualizer(boolean separateControlCluster) { this._separateControlCluster = separateControlCluster; }
- public GraphVisualizer() { this(false); }
-
- public String generateDotOutput(Parser parse) { return generateDotOutput(parse.STOP,parse._scope,parse._xScopes); }
- public String generateDotOutput(StopNode stop, Node scope, Stack<ScopeNode> xScopes) {
-
- // Since the graph has cycles, we need to create a flat list of all the
- // nodes in the graph.
- Collection<Node> all = findAll(stop, scope);
- StringBuilder sb = new StringBuilder();
- sb.append("digraph chapter11 {\n");
- sb.append("/*\n");
- sb.append(stop._src);
- sb.append("\n*/\n");
-
- // 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
- // different subgraph cluster. THEN we can draw edges between the
- // scopes and nodes. If we try to cross subgraph cluster borders while
- // still making the subgraphs DOT gets confused.
- sb.append("\trankdir=BT;\n"); // Force Nodes before Scopes
-
- // CNC - turned off Apr/8/2024, gives more flex in the layout and
- // removes some of the more ludicrous layout choices.
- // Preserve node input order
- //sb.append("\tordering=\"in\";\n");
-
- // Merge multiple edges hitting the same node. Makes common shared
- // nodes much prettier to look at.
- sb.append("\tconcentrate=\"true\";\n");
-
- // Force nested scopes to order
- sb.append("\tcompound=\"true\";\n");
-
- // Just the Nodes first, in a cluster no edges
- nodes(sb, all);
-
- // Now the scopes, in a cluster no edges
- if( xScopes != null )
- for( ScopeNode xscope : xScopes )
- if( !xscope.isDead() )
- scope( sb, xscope );
-
- // Walk the Node edges
- nodeEdges(sb, all);
-
- // Walk the active Scope edges
- if( xScopes != null )
- for( ScopeNode xscope : xScopes )
- if( !xscope.isDead() )
- scopeEdges( sb, xscope );
-
- sb.append("}\n");
- return sb.toString();
- }
-
- private void nodesByCluster(StringBuilder sb, boolean doCtrl, Collection<Node> all) {
- if (!_separateControlCluster && doCtrl) // all nodes in 1 cluster
- return;
- // Just the Nodes first, in a cluster no edges
- sb.append(doCtrl ? "\tsubgraph cluster_Controls {\n" : "\tsubgraph cluster_Nodes {\n"); // Magic "cluster_" in the subgraph name
- for (Node n : all) {
- if (n instanceof ProjNode || n instanceof CProjNode || n instanceof ScopeNode || n==Parser.XCTRL)
- continue; // Do not emit, rolled into MultiNode or Scope cluster already
- if (_separateControlCluster && doCtrl && !n.isCFG()) continue;
- if (_separateControlCluster && !doCtrl && n.isCFG()) continue;
- sb.append("\t\t").append(n.uniqueName()).append(" [ ");
- String lab = n.glabel();
- if (n instanceof MultiNode) {
- // Make a box with the MultiNode on top, and all the projections on the bottom
- sb.append("shape=plaintext label=<\n");
- sb.append("\t\t\t<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\" CELLPADDING=\"4\">\n");
- sb.append("\t\t\t<TR><TD BGCOLOR=\"yellow\">").append(lab).append("</TD></TR>\n");
- sb.append("\t\t\t<TR>");
- boolean doProjTable = false;
- for (Node use : n._outputs) {
- if (use instanceof ProjNode proj) {
- if (!doProjTable) {
- doProjTable = true;
- sb.append("<TD>").append("\n");
- sb.append("\t\t\t\t<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\">").append("\n");
- sb.append("\t\t\t\t<TR>");
- }
- sb.append("<TD PORT=\"p").append(proj._idx).append("\">").append(proj.glabel()).append("</TD>");
- }
- if (use instanceof CProjNode proj) {
- if (!doProjTable) {
- doProjTable = true;
- sb.append("<TD>").append("\n");
- sb.append("\t\t\t\t<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\">").append("\n");
- sb.append("\t\t\t\t<TR>");
- }
- sb.append("<TD PORT=\"p").append(proj._idx).append("\" BGCOLOR=\"yellow\">").append(proj.glabel()).append("</TD>");
- }
- }
- if (doProjTable) {
- sb.append("</TR>").append("\n");
- sb.append("\t\t\t\t</TABLE>").append("\n");
- sb.append("\t\t\t</TD>");
- }
- sb.append("</TR>\n");
- sb.append("\t\t\t</TABLE>>\n\t\t");
-
- } else {
- // control nodes have box shape
- // other nodes are ellipses, i.e. default shape
- if (n.isCFG()) sb.append("shape=box style=filled fillcolor=yellow ");
- else if (n instanceof PhiNode) sb.append("style=filled fillcolor=lightyellow ");
- sb.append("label=\"").append(lab).append("\" ");
- }
- sb.append("];\n");
- }
- if (!_separateControlCluster) {
- // Force Region & Phis to line up
- for (Node n : all) {
- if (n instanceof RegionNode region) {
- sb.append("\t\t{ rank=same; ");
- sb.append(region).append(";");
- for (Node phi : region._outputs)
- if (phi instanceof PhiNode) sb.append(phi.uniqueName()).append(";");
- sb.append("}\n");
- }
- }
- }
- sb.append("\t}\n"); // End Node cluster
- }
-
- private void nodes(StringBuilder sb, Collection<Node> all) {
- nodesByCluster(sb, true, all);
- nodesByCluster(sb, false, all);
- }
-
- // Build a nested scope display, walking the _prev edge
- private void scope( StringBuilder sb, ScopeNode scope ) {
- sb.append("\tnode [shape=plaintext];\n");
- int level=1;
- for( int idx = scope._scopes.size()-1; idx>=0; idx-- ) {
- var syms = scope._scopes.get(idx);
- String scopeName = makeScopeName(scope, level);
- sb.append("\tsubgraph cluster_").append(scopeName).append(" {\n"); // Magic "cluster_" in the subgraph name
- sb.append("\t\t").append(scopeName).append(" [label=<\n");
- sb.append("\t\t\t<TABLE BORDER=\"0\" CELLBORDER=\"1\" CELLSPACING=\"0\">\n");
- // Add the scope level
- int scopeLevel = scope._scopes.size()-level;
- sb.append("\t\t\t<TR><TD BGCOLOR=\"cyan\">").append(scopeLevel).append("</TD>");
- for(String name: syms.keySet())
- sb.append("<TD PORT=\"").append(makePortName(scopeName, name)).append("\">").append(name).append("</TD>");
- sb.append("</TR>\n");
- sb.append("\t\t\t</TABLE>>];\n");
- level++;
- }
- // Scope clusters nest, so the graphics shows the nested scopes, so
- // they are not closed as they are printed; so they just keep nesting.
- // We close them all at once here.
- sb.append( "\t}\n".repeat( level-1 ) ); // End all Scope clusters
- }
-
- private String makeScopeName(ScopeNode sn, int level) { return sn.uniqueName() + "_" + level; }
- private String makePortName(String scopeName, String varName) { return scopeName + "_" + varName; }
-
- // Walk the node edges
- private void nodeEdges(StringBuilder sb, Collection<Node> all) {
- // All them edge labels
- sb.append("\tedge [ fontname=Helvetica, fontsize=8 ];\n");
- for( Node n : all ) {
- // Do not display the Constant->Start edge;
- // ProjNodes handled by Multi;
- // ScopeNodes are done separately
- if( n instanceof ConstantNode || n instanceof ProjNode || n instanceof CProjNode || n instanceof ScopeNode || n==Parser.XCTRL )
- continue;
- for( int i=0; i<n.nIns(); i++ ) {
- Node def = n.in(i);
- if( n instanceof PhiNode && def instanceof RegionNode ) {
- // Draw a dotted use->def edge from Phi to Region.
- sb.append('\t').append(n.uniqueName());
- sb.append(" -> ");
- sb.append(def.uniqueName());
- sb.append(" [style=dotted taillabel=").append(i).append("];\n");
- } else if( def != null ) {
- // Most edges land here use->def
- sb.append('\t').append(n.uniqueName()).append(" -> ");
- if( def instanceof CProjNode proj ) {
- String mname = proj.ctrl().uniqueName();
- sb.append(mname).append(":p").append(proj._idx);
- } else if( def instanceof ProjNode proj ) {
- String mname = proj.in(0).uniqueName();
- sb.append(mname).append(":p").append(proj._idx);
- } else sb.append(def.uniqueName());
- // Number edges, so we can see how they track
- sb.append("[taillabel=").append(i);
- // The edge from New to ctrl is just for anchoring the New
- if ( n instanceof NewNode )
- sb.append(" color=green");
- // control edges are colored red
- else if( def.isCFG() )
- sb.append(" color=red");
- else if( def.isMem() )
- sb.append(" color=blue");
- // Backedges do not add a ranking constraint
- if( i==2 && (n instanceof PhiNode || n instanceof LoopNode) )
- sb.append(" constraint=false");
- sb.append("];\n");
- }
- }
- }
- }
-
- // Walk the scope edges
- private void scopeEdges( StringBuilder sb, ScopeNode scope ) {
- sb.append("\tedge [style=dashed color=cornflowerblue];\n");
- int level=1;
- for( int i = scope._scopes.size()-1; i>=0; i-- ) {
- var syms = scope._scopes.get(i);
- String scopeName = makeScopeName(scope, level);
- for( String name : syms.keySet() ) {
- int idx = syms.get(name);
- Node def = scope.in(idx);
- while( def instanceof ScopeNode lazy )
- def = lazy.in(idx);
- if( def==null ) continue;
- sb.append("\t")
- .append(scopeName).append(":")
- .append('"').append(makePortName(scopeName, name)).append('"') // wrap port name with quotes because $ctrl is not valid unquoted
- .append(" -> ");
- if( def instanceof CProjNode proj ) {
- String mname = proj.ctrl().uniqueName();
- sb.append(mname).append(":p").append(proj._idx);
- } else sb.append(def.uniqueName());
- sb.append(";\n");
- }
- level++;
- }
- }
-
- /**
- * Finds all nodes in the graph.
- */
- private Collection<Node> findAll(Node stop, Node scope) {
- final HashMap<Integer, Node> all = new HashMap<>();
- for( Node n : stop._inputs )
- walk(all, n);
- if( scope != null )
- for( Node n : scope._inputs )
- walk(all, n);
- return all.values();
- }
-
- /**
- * Walk a subgraph and populate distinct nodes in the all list.
- */
- private void walk(HashMap<Integer,Node> all, Node n) {
- if(n == null ) return;
- if (all.get(n._nid) != null) return; // Been there, done that
- all.put(n._nid, n);
- for (Node c : n._inputs)
- walk(all, c);
- for( Node c : n._outputs )
- walk(all, c);
- }
-}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/IterPeeps.java b/chapter12/src/main/java/com/seaofnodes/simple/IterPeeps.java
index 121d0233..65a5e64f 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/IterPeeps.java
+++ b/chapter12/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, boolean show) {
+ public static StopNode iterate(StopNode stop) {
assert progressOnList(stop);
int cnt=0;
@@ -65,9 +65,14 @@ public abstract class IterPeeps {
while( (n=WORK.pop()) != null ) {
if( n.isDead() ) continue;
cnt++; // Useful for debugging, searching which peephole broke things
+ var obs = Parser.PARSER == null ? null : Parser.PARSER._obs;
+ if( obs != null ) obs.before(n);
Node x = n.peepholeOpt();
if( x != null ) {
- if( x.isDead() ) continue;
+ if( x.isDead() ) {
+ if( obs != null ) obs.after(n, x, true);
+ continue;
+ }
// peepholeOpt can return brand-new nodes, needing an initial type set
if( x._type==null ) x.setType(x.compute());
// Changes require neighbors onto the worklist
@@ -90,12 +95,12 @@ public abstract class IterPeeps {
}
if( n.isUnused() && !(n instanceof StopNode) )
n.kill(); // Just plain dead
+ if( obs != null ) obs.after(n, x, true);
}
// Break infinite loops, forcing a Never-branch to exit
GlobalCodeMotion.fixLoops(stop);
- if( show )
- System.out.println(new GraphVisualizer().generateDotOutput(stop,null,null));
+
GlobalCodeMotion.buildCFG(stop);
return stop;
}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/Parser.java b/chapter12/src/main/java/com/seaofnodes/simple/Parser.java
index 1a223726..2bcd8da7 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/Parser.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/Parser.java
@@ -1,5 +1,7 @@
package com.seaofnodes.simple;
+import com.seaofnodes.graph.GraphObserver;
+
import com.seaofnodes.simple.node.*;
import com.seaofnodes.simple.type.*;
@@ -14,6 +16,12 @@ import java.util.*;
*/
public class Parser {
+ // Current compilation context; the observer belongs to the parser.
+ public static Parser PARSER;
+ public GraphObserver<Node> _obs;
+ public int pos() { return _lexer._position; }
+
+
/**
* A Global Static, unique to each compilation. This is a public, so we
* can make constants everywhere without having to thread the StartNode
@@ -81,6 +89,7 @@ public class Parser {
public static HashMap<String, Type> TYPES = new HashMap<>();
public Parser(String source, TypeInteger arg) {
+ PARSER = this;
Node.reset();
IterPeeps.reset();
TYPES.clear();
@@ -112,8 +121,7 @@ public class Parser {
private Node ctrl(Node n) { return _scope.ctrl(n); }
- public StopNode parse() { return parse(false); }
- public StopNode parse(boolean show) {
+ public StopNode parse() {
_xScopes.push(_scope);
// Enter a new scope for the initial control and arguments
_scope.push();
@@ -126,7 +134,6 @@ public class Parser {
_xScopes.pop();
if (!_lexer.isEOF()) throw error("Syntax error, unexpected " + _lexer.getAnyNextToken());
STOP.peephole();
- if( show ) showGraph();
return STOP;
}
@@ -166,7 +173,6 @@ public class Parser {
else if (matchx("break") ) return parseBreak();
else if (matchx("continue")) return parseContinue();
else if (matchx("struct") ) return parseStruct();
- else if (matchx("#showGraph")) return require(showGraph(),";");
else if (matchx(";") ) return null; // Empty statement
// declarations of vars with struct type are handled in parseExpressionStatement due
// to ambiguity
@@ -384,14 +390,7 @@ public class Parser {
return ret;
}
- /**
- * Dumps out the node graph
- * @return {@code null}
- */
- Node showGraph() {
- System.out.println(new GraphVisualizer().generateDotOutput(STOP,_scope,_xScopes));
- return null;
- }
+
/**
* Parses an expression statement or a declaration statement where type is a struct
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/AddFNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/AddFNode.java
index 7d4e2910..5d0a473a 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/AddFNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/AddFNode.java
@@ -10,8 +10,6 @@ public class AddFNode extends Node {
@Override public String label() { return "AddF"; }
- @Override public String glabel() { return "+"; }
-
@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
in(1)._print0(sb.append("("), visited);
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/AddNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/AddNode.java
index 48350957..f51adcf1 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/AddNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/AddNode.java
@@ -9,8 +9,6 @@ public class AddNode extends Node {
@Override public String label() { return "Add"; }
- @Override public String glabel() { return "+"; }
-
@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
in(1)._print0(sb.append("("), visited);
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/BoolNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/BoolNode.java
index f202b263..2055ea6f 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/BoolNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/BoolNode.java
@@ -21,9 +21,6 @@ abstract public class BoolNode extends Node {
public String label() { return getClass().getSimpleName(); }
@Override
- public String glabel() { return op(); }
-
- @Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
in(1)._print0(sb.append("("), visited);
in(2)._print0(sb.append(op()), visited);
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/DivFNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/DivFNode.java
index 36fd4524..6c0c2eb3 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/DivFNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/DivFNode.java
@@ -10,8 +10,6 @@ public class DivFNode extends Node {
@Override public String label() { return "DivF"; }
- @Override public String glabel() { return "/"; }
-
@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
in(1)._print0(sb.append("("), visited);
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/DivNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/DivNode.java
index e0c95019..ae73284f 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/DivNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/DivNode.java
@@ -10,8 +10,6 @@ public class DivNode extends Node {
@Override public String label() { return "Div"; }
- @Override public String glabel() { return "//"; }
-
@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
in(1)._print0(sb.append("("), visited);
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/LoadNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/LoadNode.java
index 244a0919..a56678df 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/LoadNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/LoadNode.java
@@ -29,9 +29,6 @@ public class LoadNode extends MemOpNode {
public String label() { return "Load"; }
@Override
- public String glabel() { return "."+_name; }
-
- @Override
StringBuilder _print1(StringBuilder sb, BitSet visited) { return sb.append(".").append(_name); }
@Override
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/MinusFNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/MinusFNode.java
index 9727f58b..7cacf2c9 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/MinusFNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/MinusFNode.java
@@ -10,8 +10,6 @@ public class MinusFNode extends Node {
@Override public String label() { return "MinusF"; }
- @Override public String glabel() { return "-"; }
-
@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
in(1)._print0(sb.append("(-"), visited);
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/MinusNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/MinusNode.java
index ae8e9e1b..0f2669b9 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/MinusNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/MinusNode.java
@@ -10,8 +10,6 @@ public class MinusNode extends Node {
@Override public String label() { return "Minus"; }
- @Override public String glabel() { return "-"; }
-
@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
in(1)._print0(sb.append("(-"), visited);
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/MulFNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/MulFNode.java
index 2d44a9fb..4b753ba7 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/MulFNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/MulFNode.java
@@ -10,8 +10,6 @@ public class MulFNode extends Node {
@Override public String label() { return "MulF"; }
- @Override public String glabel() { return "*"; }
-
@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
in(1)._print0(sb.append("("), visited);
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/MulNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/MulNode.java
index 8c8a6e0a..09a27582 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/MulNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/MulNode.java
@@ -10,8 +10,6 @@ public class MulNode extends Node {
@Override public String label() { return "Mul"; }
- @Override public String glabel() { return "*"; }
-
@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
in(1)._print0(sb.append("("), visited);
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/NewNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/NewNode.java
index c00fee62..1c82d5f5 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/NewNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/NewNode.java
@@ -18,9 +18,6 @@ public class NewNode extends Node {
public String label() { return "new"; }
@Override
- public String glabel() { return "new " + _ptr._obj._name; }
-
- @Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
sb.append("new ");
return sb.append(_ptr._obj.str());
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/Node.java b/chapter12/src/main/java/com/seaofnodes/simple/node/Node.java
index 352257a9..6b48799b 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/Node.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/Node.java
@@ -1,5 +1,7 @@
package com.seaofnodes.simple.node;
+import com.seaofnodes.simple.Parser;
+
import com.seaofnodes.simple.IRPrinter;
import com.seaofnodes.simple.IterPeeps;
import com.seaofnodes.simple.Utils;
@@ -72,17 +74,11 @@ public abstract class Node implements OutNode {
// Easy reading label for debugger, e.g. "Add" or "Region" or "EQ"
public abstract String label();
- // Unique label for graph visualization, e.g. "Add12" or "Region30" or "EQ99"
+ // Unique label for debugging, e.g. "Add12" or "Region30" or "EQ99"
public String uniqueName() {
- // Get rid of $ as graphviz doesn't like it
- String label = label().replaceAll("\\$", "");
- return label + _nid;
+ return label() + _nid;
}
- // Graphical label, e.g. "+" or "Region" or "=="
- public String glabel() { return label(); }
-
-
// ------------------------------------------------------------------------
// Debugger Printing.
@@ -288,13 +284,14 @@ public abstract class Node implements OutNode {
* Try to peephole at this node and return a better replacement Node.
* Always returns some not-null Node (often this).
*/
- public final Node peephole( ) {
- if (_disablePeephole) {
- _type = compute();
- return this; // Peephole optimizations turned off
- }
+ public final Node peephole() {
+ if( _disablePeephole ) { _type = compute(); return this; }
+ var obs = IterPeeps.midAssert() ? null : Parser.PARSER == null ? null : Parser.PARSER._obs;
+ if( obs != null ) obs.before(this);
Node n = peepholeOpt();
- return n==null ? this : deadCodeElim(n.peephole()); // Cannot return null for no-progress
+ Node rez = n == null ? this : deadCodeElim(n.peephole());
+ if( obs != null ) obs.after(this, n == null ? null : rez, false);
+ return rez;
}
/**
@@ -452,6 +449,8 @@ public abstract class Node implements OutNode {
// retry the peephole. Track a set of Nodes dependent on `this`, and
// revisit them if `this` changes.
ArrayList<Node> _deps;
+ public int nDeps() { return _deps == null ? 0 : _deps.size(); }
+ public Node dep(int idx) { return _deps.get(idx); }
/**
* Add a node to the list of dependencies. Only add it if its not an input
@@ -462,6 +461,8 @@ public abstract class Node implements OutNode {
// Running peepholes during the big assert cannot have side effects
// like adding dependencies.
if( IterPeeps.midAssert() ) return this;
+ var obs = IterPeeps.midAssert() ? null : Parser.PARSER == null ? null : Parser.PARSER._obs;
+ if( obs != null ) obs.dep(this, dep);
if( _deps==null ) _deps = new ArrayList<>();
if( Utils.find(_deps ,dep) != -1 ) return this; // Already on list
if( Utils.find(_inputs,dep) != -1 ) return this; // No need for deps on immediate neighbors
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/NotNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/NotNode.java
index 3cf69520..e987bd82 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/NotNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/NotNode.java
@@ -9,8 +9,6 @@ public class NotNode extends Node {
@Override public String label() { return "Not"; }
- @Override public String glabel() { return "!"; }
-
@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
in(1)._print0(sb.append("(!"), visited);
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/PhiNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/PhiNode.java
index 02a4020f..dbe74f57 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/PhiNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/PhiNode.java
@@ -18,8 +18,6 @@ public class PhiNode extends Node {
@Override public String label() { return "Phi_"+_label; }
- @Override public String glabel() { return "&phi;_"+_label; }
-
@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
if( !(region() instanceof RegionNode r) || r.inProgress() )
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/StopNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/StopNode.java
index fe96bd2a..c36bdf4d 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/StopNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/StopNode.java
@@ -67,8 +67,7 @@ public class StopNode extends CFGNode {
return addDef(node);
}
- public StopNode iterate( ) { return IterPeeps.iterate(this,false).typeCheck(); }
- public StopNode iterate(boolean show) { return IterPeeps.iterate(this,show ).typeCheck(); }
+ public StopNode iterate() { return IterPeeps.iterate(this).typeCheck(); }
StopNode typeCheck() {
String err = walk( Node::err );
if( err != null ) throw new RuntimeException(err);
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/StoreNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/StoreNode.java
index db8e2ce6..5b30b3ec 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/StoreNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/StoreNode.java
@@ -24,8 +24,6 @@ public class StoreNode extends MemOpNode {
@Override
public String label() { return "Store"; }
@Override
- public String glabel() { return "."+_name+" ="; }
- @Override
public boolean isMem() { return true; }
public Node val() { return in(3); }
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/SubFNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/SubFNode.java
index b591718d..27bbda8d 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/SubFNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/SubFNode.java
@@ -10,8 +10,6 @@ public class SubFNode extends Node {
@Override public String label() { return "SubF"; }
- @Override public String glabel() { return "-"; }
-
@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
in(1)._print0(sb.append("("), visited);
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/SubNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/SubNode.java
index f0c77743..c9d20ce0 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/SubNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/SubNode.java
@@ -11,8 +11,6 @@ public class SubNode extends Node {
@Override public String label() { return "Sub"; }
- @Override public String glabel() { return "-"; }
-
@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
in(1)._print0(sb.append("("), visited);
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/ToFloatNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/ToFloatNode.java
index 1d147cf6..fde483d6 100644
--- a/chapter12/src/main/java/com/seaofnodes/simple/node/ToFloatNode.java
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/ToFloatNode.java
@@ -11,8 +11,6 @@ public class ToFloatNode extends Node {
@Override public String label() { return "ToFloat"; }
- @Override public String glabel() { return "(flt)"; }
-
@Override
StringBuilder _print1(StringBuilder sb, BitSet visited) {
return in(1)._print0(sb.append("(flt)"), visited);
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java b/chapter12/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
new file mode 100644
index 00000000..8e909e1c
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/print/SimpleGraphAdapter.java
@@ -0,0 +1,62 @@
+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 com.seaofnodes.simple.type.TypeMem;
+import java.util.ArrayList;
+
+/** Chapter 12'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); }
+ @Override protected int nDeps(Node n) { return n.nDeps(); }
+ @Override protected Node dep(Node n, int idx) { return n.dep(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 GraphSnapshot.Node desc(Node n) {
+ Projection proj = n instanceof ProjNode p ? new Projection(ref(n.in(0)), p._idx)
+ : n instanceof CProjNode 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) {
+ if( n instanceof ScopeNode ) return Kind.SCOPE;
+ if( n instanceof StartNode ) return Kind.CTRL;
+ if( n instanceof LoopNode ) return Kind.LOOP;
+ if( n instanceof RegionNode ) return Kind.REGION;
+ if( n instanceof PhiNode ) return Kind.PHI;
+ return n.isCFG() ? Kind.CTRL : isMem(n) ? Kind.MEM : Kind.DATA;
+ }
+
+ private Role role(Node n, int i) {
+ if( n instanceof ScopeNode || n instanceof ConstantNode || n instanceof XCtrlNode ) return Role.ASSOC;
+ if( n instanceof PhiNode ) return i == 0 ? Role.ASSOC : isMem(n) ? Role.MEM : Role.DATA;
+ if( n instanceof ProjNode || n instanceof CProjNode ) return n.isCFG() ? Role.CTRL : isMem(n) ? Role.MEM : Role.DATA;
+ if( n instanceof RegionNode && i == 0 ) return Role.ASSOC;
+ if( i == 0 || n instanceof RegionNode || n instanceof StopNode ) return Role.CTRL;
+ if( i == 1 && (n instanceof MemOpNode || n instanceof ReturnNode) ) return Role.MEM;
+ Node def = n.in(i);
+ if( def != null && isMem(def) ) return Role.MEM;
+ return Role.DATA;
+ }
+
+ private boolean isMem(Node n) { return n.isMem() || n._type instanceof TypeMem; }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/print/SimpleGraphObserver.java b/chapter12/src/main/java/com/seaofnodes/simple/print/SimpleGraphObserver.java
new file mode 100644
index 00000000..3d9ec14a
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/print/SimpleGraphObserver.java
@@ -0,0 +1,43 @@
+package com.seaofnodes.simple.print;
+
+import com.seaofnodes.graph.GraphCapture;
+import com.seaofnodes.graph.GraphViewer;
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.node.Node;
+import java.util.ArrayList;
+import java.io.IOException;
+
+/** Chapter 12's compilation context; capture and event bookkeeping are shared. */
+public class SimpleGraphObserver extends GraphCapture<Node> {
+ private Parser _parser;
+ private Node _ret;
+ private String _phase;
+ public SimpleGraphObserver() { super(new SimpleGraphAdapter()); }
+ public static void main(String[] args) throws IOException {
+ GraphViewer.run(new SimpleGraphObserver());
+ }
+ @Override protected void compile(String src) {
+ _parser = new Parser(src);
+ _parser._obs = this;
+ _phase = "Parse";
+ _ret = _parser.parse();
+ phase(_phase);
+ _phase = "Iter";
+ com.seaofnodes.simple.IterPeeps.iterate(_parser.STOP);
+ phase(_phase);
+ }
+ @Override protected String phase() { return _phase; }
+ @Override protected int pos() { return "Parse".equals(_phase) ? _parser.pos() : -1; }
+ @Override protected void detach() {
+ if( _parser != null ) _parser._obs = null;
+ _parser = null;
+ _ret = null;
+ }
+ @Override protected void roots(ArrayList<Node> roots) {
+ roots.add(Parser.START);
+ roots.add(_parser.STOP);
+ roots.add(_parser._scope);
+ roots.addAll(_parser._xScopes);
+ roots.add(_ret);
+ }
+}
diff --git a/chapter12/src/test/java/com/seaofnodes/simple/Chapter12Test.java b/chapter12/src/test/java/com/seaofnodes/simple/Chapter12Test.java
index 00b2591c..d862df95 100644
--- a/chapter12/src/test/java/com/seaofnodes/simple/Chapter12Test.java
+++ b/chapter12/src/test/java/com/seaofnodes/simple/Chapter12Test.java
@@ -29,7 +29,7 @@ public class Chapter12Test {
"""
return 3.14;
""");
- StopNode stop = parser.parse(false).iterate(false);
+ StopNode stop = parser.parse().iterate();
assertEquals("return 3.14;", stop.toString());
assertEquals(3.14, Evaluator.evaluate(stop, 0));
}
@@ -40,7 +40,7 @@ return 3.14;
"""
return 3.14;
""");
- StopNode stop = parser.parse(false).iterate(false);
+ StopNode stop = parser.parse().iterate();
assertEquals("return 3.14;", stop.toString());
assertEquals(3.14, Evaluator.evaluate(stop, 0));
}
@@ -57,7 +57,7 @@ while( 1 ) {
}
return guess;
""");
- StopNode stop = parser.parse(false).iterate(false);
+ StopNode stop = parser.parse().iterate();
assertEquals("return Phi(Loop9,(flt)arg,(((ToFloat/Phi_guess)+Phi_guess)/2.0));", stop.toString());
assertEquals(3.0, Evaluator.evaluate(stop, 9));
assertEquals(1.414213562373095, Evaluator.evaluate(stop, 2));
@@ -70,7 +70,7 @@ return guess;
flt x = arg;
return x+1==x;
""");
- StopNode stop = parser.parse(false).iterate(false);
+ StopNode stop = parser.parse().iterate();
assertEquals("return ((flt)arg==(ToFloat+1.0));", stop.toString());
assertEquals(0L, Evaluator.evaluate(stop, 1));
}
diff --git a/chapter12/src/test/java/com/seaofnodes/simple/SchedulerTest.java b/chapter12/src/test/java/com/seaofnodes/simple/SchedulerTest.java
index 5def2782..853999b1 100644
--- a/chapter12/src/test/java/com/seaofnodes/simple/SchedulerTest.java
+++ b/chapter12/src/test/java/com/seaofnodes/simple/SchedulerTest.java
@@ -31,7 +31,7 @@ if (arg) v.f=1;
return i;
""");
StopNode._disablePeephole = true;
- StopNode stop = parser.parse(true);//.iterate();
+ StopNode stop = parser.parse();//.iterate();
var eval = new Evaluator(stop);
assertEquals(2L, eval.evaluate(1, 10));
}