summaryrefslogtreecommitdiff
path: root/chapter12/src/main/java/com
diff options
context:
space:
mode:
Diffstat (limited to 'chapter12/src/main/java/com')
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java238
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/GraphVisualizer.java280
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/IRPrinter.java266
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/IterPeeps.java205
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/Parser.java853
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/Utils.java58
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/AddNode.java174
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/BoolNode.java75
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/CFGNode.java92
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/CProjNode.java64
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/CastNode.java38
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/ConstantNode.java56
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/DivNode.java38
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/IfNode.java71
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/LoadNode.java78
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/LoopNode.java77
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/MemOpNode.java44
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/MinusNode.java36
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/MulNode.java57
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/MultiNode.java12
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/NeverNode.java19
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/NewNode.java42
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/Node.java624
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/NotNode.java44
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/OutNode.java7
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/PhiNode.java159
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/ProjNode.java46
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/RegionNode.java129
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/ReturnNode.java57
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/ScopeNode.java297
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/StartNode.java85
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/StopNode.java79
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/StoreNode.java70
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/SubNode.java51
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/node/XCtrlNode.java13
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/type/Field.java60
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/type/Type.java187
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/type/TypeInteger.java101
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/type/TypeMem.java64
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/type/TypeMemPtr.java78
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/type/TypeStruct.java121
-rw-r--r--chapter12/src/main/java/com/seaofnodes/simple/type/TypeTuple.java87
42 files changed, 5232 insertions, 0 deletions
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java b/chapter12/src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java
new file mode 100644
index 00000000..054a489c
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/GlobalCodeMotion.java
@@ -0,0 +1,238 @@
+ package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.node.*;
+import com.seaofnodes.simple.type.*;
+import java.util.*;
+
+public abstract class GlobalCodeMotion {
+
+ // Arrange that the existing isCFG() Nodes form a valid CFG. The
+ // Node.use(0) is always a block tail (either IfNode or head of the
+ // following block). There are no unreachable infinite loops.
+ public static void buildCFG( StopNode stop ) {
+ fixLoops(stop);
+ schedEarly();
+ Parser.SCHEDULED = true;
+ schedLate(Parser.START);
+ }
+
+ // ------------------------------------------------------------------------
+ // Backwards walk on the CFG only, looking for unreachable code - which has
+ // to be an infinite loop. Insert a bogus never-taken exit to Stop, so the
+ // loop becomes reachable. Also, set loop nesting depth
+ private static void fixLoops(StopNode stop) {
+ // Backwards walk from Stop, looking for unreachable code
+ BitSet visit = new BitSet();
+ HashSet<CFGNode> unreach = new HashSet<>();
+ unreach.add(Parser.START);
+ for( Node ret : stop._inputs )
+ ((ReturnNode)ret).walkUnreach(visit,unreach);
+ if( unreach.isEmpty() ) return;
+
+ // Forwards walk from unreachable, looking for loops with no exit test.
+ visit.clear();
+ for( CFGNode cfg : unreach )
+ walkInfinite(cfg,visit,stop);
+ // Set loop depth on remaining graph
+ unreach.clear();
+ visit.clear();
+ for( Node ret : stop._inputs )
+ ((ReturnNode)ret).walkUnreach(visit,unreach);
+ assert unreach.isEmpty();
+ }
+
+
+ // Forwards walk over previously unreachable, looking for loops with no
+ // exit test.
+ static private void walkInfinite( CFGNode n, BitSet visit, StopNode stop ) {
+ if( visit.get(n._nid) ) return; // Been there, done that
+ visit.set(n._nid);
+ if( n instanceof LoopNode loop )
+ loop.forceExit(stop);
+ for( Node use : n._outputs )
+ if( use instanceof CFGNode cfg )
+ walkInfinite(cfg,visit,stop);
+ }
+
+ // ------------------------------------------------------------------------
+ private static void schedEarly() {
+ ArrayList<CFGNode> rpo = new ArrayList<>();
+ BitSet visit = new BitSet();
+ _rpo_cfg(Parser.START, visit, rpo);
+ // Reverse Post-Order on CFG
+ for( int j=rpo.size()-1; j>=0; j-- ) {
+ CFGNode cfg = rpo.get(j);
+ cfg.loopDepth();
+ for( Node n : cfg._inputs )
+ _schedEarly(n,visit);
+ // Strictly for dead infinite loops, we can have entire code blocks
+ // not reachable from below - so we reach down, from above, one
+ // step. Since _schedEarly modifies the output arrays, the normal
+ // region._outputs ArrayList iterator throws CME. The extra edges
+ // are always *added* after any Phis, so just walk the Phi prefix.
+ if( cfg instanceof RegionNode region ) {
+ int len = region.nOuts();
+ for( int i=0; i<len; i++ )
+ if( region.out(i) instanceof PhiNode phi )
+ _schedEarly(phi,visit);
+ }
+ }
+ }
+
+ // Post-Order of CFG
+ private static void _rpo_cfg(Node n, BitSet visit, ArrayList<CFGNode> rpo) {
+ if( !(n instanceof CFGNode cfg) || visit.get(cfg._nid) )
+ return; // Been there, done that
+ visit.set(cfg._nid);
+ for( Node use : cfg._outputs )
+ _rpo_cfg(use,visit,rpo);
+ rpo.add(cfg);
+ }
+
+ private static void _schedEarly(Node n, BitSet visit) {
+ if( n==null || visit.get(n._nid) ) return; // Been there, done that
+ visit.set(n._nid);
+ // Schedule not-pinned not-CFG inputs before self. Since skipping
+ // Pinned, this never walks the backedge of Phis (and thus spins around
+ // a data-only loop, eventually attempting relying on some pre-visited-
+ // not-post-visited data op with no scheduled control.
+ for( Node def : n._inputs )
+ if( def!=null && !def.isPinned() )
+ _schedEarly(def,visit);
+ // If not-pinned (e.g. constants, projections, phi) and not-CFG
+ if( !n.isPinned() ) {
+ // Schedule at deepest input
+ CFGNode early = Parser.START; // Maximally early, lowest idepth
+ for( int i=1; i<n.nIns(); i++ )
+ if( n.in(i).cfg0().idepth() > early.idepth() )
+ early = n.in(i).cfg0(); // Latest/deepest input
+ n.setDef(0,early); // First place this can go
+ }
+ }
+
+ // ------------------------------------------------------------------------
+ private static void schedLate(StartNode start) {
+ CFGNode[] late = new CFGNode[Node.UID()];
+ Node[] ns = new Node[Node.UID()];
+ _schedLate(start,ns,late);
+ for( int i=0; i<late.length; i++ )
+ if( ns[i] != null )
+ ns[i].setDef(0,late[i]);
+ }
+
+ // Forwards post-order pass. Schedule all outputs first, then draw a
+ // idom-tree line from the LCA of uses to the early schedule. Schedule is
+ // legal anywhere on this line; pick the most control-dependent (largest
+ // idepth) in the shallowest loop nest.
+ private static void _schedLate(Node n, Node[] ns, CFGNode[] late) {
+ if( late[n._nid]!=null ) return; // Been there, done that
+ // These I know the late schedule of, and need to set early for loops
+ if( n instanceof CFGNode cfg ) late[n._nid] = cfg.blockHead() ? cfg : cfg.cfg(0);
+ if( n instanceof PhiNode phi ) late[n._nid] = phi.region();
+
+ // Walk Stores before Loads, so we can get the anti-deps right
+ for( Node use : n._outputs )
+ if( isForwardsEdge(use,n) &&
+ use._type instanceof TypeMem )
+ _schedLate(use,ns,late);
+ // Walk everybody now
+ for( Node use : n._outputs )
+ if( isForwardsEdge(use,n) )
+ _schedLate(use,ns,late);
+ // Already implicitly scheduled
+ if( n.isPinned() ) return;
+ // Need to schedule n
+
+ // Walk uses, gathering the LCA (Least Common Ancestor) of uses
+ CFGNode early = (CFGNode)n.in(0);
+ assert early != null;
+ CFGNode lca = null;
+ for( Node use : n._outputs )
+ lca = use_block(n,use, late).idom(lca);
+
+ // Loads may need anti-dependencies, raising their LCA
+ if( n instanceof LoadNode load )
+ lca = find_anti_dep(lca,load,early,late);
+
+ // Walk up from the LCA to the early, looking for best place. This is
+ // lowest execution frequency, approximated by least loop depth and
+ // deepest control flow.
+ CFGNode best = lca;
+ lca = lca.idom(); // Already found best for starting LCA
+ for( ; lca != early.idom(); lca = lca.idom() )
+ if( better(lca,best) )
+ best = lca;
+ assert !(best instanceof IfNode);
+ ns [n._nid] = n;
+ late[n._nid] = best;
+ }
+
+ // Block of use. Normally from late[] schedule, except for Phis, which go
+ // to the matching Region input.
+ private static CFGNode use_block(Node n, Node use, CFGNode[] late) {
+ if( !(use instanceof PhiNode phi) )
+ return late[use._nid];
+ CFGNode found=null;
+ for( int i=1; i<phi.nIns(); i++ )
+ if( phi.in(i)==n )
+ if( found==null ) found = phi.region().cfg(i);
+ else Utils.TODO(); // Can be more than once
+ assert found!=null;
+ return found;
+ }
+
+
+ // Least loop depth first, then largest idepth
+ private static boolean better( CFGNode lca, CFGNode best ) {
+ return lca._loopDepth < best._loopDepth ||
+ (lca.idepth() > best.idepth() || best instanceof IfNode);
+ }
+
+ // Skip iteration if a backedge
+ private static boolean isForwardsEdge(Node use, Node def) {
+ return use != null && def != null &&
+ !(use.nIns()>2 && use.in(2)==def && (use instanceof LoopNode || (use instanceof PhiNode phi && phi.region() instanceof LoopNode)));
+ }
+
+ private static CFGNode find_anti_dep(CFGNode lca, LoadNode load, CFGNode early, CFGNode[] late) {
+ // We could skip final-field loads here.
+ // Walk LCA->early, flagging Load's block location choices
+ for( CFGNode cfg=lca; early!=null && cfg!=early.idom(); cfg = cfg.idom() )
+ cfg._anti = load._nid;
+ // Walk load->mem uses, looking for Stores causing an anti-dep
+ for( Node mem : load.mem()._outputs ) {
+ switch( mem ) {
+ case StoreNode st:
+ lca = anti_dep(load,late[st._nid],st.cfg0(),lca,st);
+ break;
+ case PhiNode phi:
+ // Repeat anti-dep for matching Phi inputs.
+ // No anti-dep edges but may raise the LCA.
+ for( int i=1; i<phi.nIns(); i++ )
+ if( phi.in(i)==load.mem() )
+ lca = anti_dep(load,phi.region().cfg(i),load.mem().cfg0(),lca,null);
+ break;
+ case LoadNode ld: break; // Loads do not cause anti-deps on other loads
+ case ReturnNode ret: break; // Load must already be ahead of Return
+ default: throw Utils.TODO();
+ }
+ }
+ return lca;
+ }
+
+ //
+ private static CFGNode anti_dep( LoadNode load, CFGNode stblk, CFGNode defblk, CFGNode lca, Node st ) {
+ // Walk store blocks "reach" from its scheduled location to its earliest
+ for( ; stblk != defblk.idom(); stblk = stblk.idom() ) {
+ // Store and Load overlap, need anti-dependence
+ if( stblk._anti==load._nid ) {
+ lca = stblk.idom(lca); // Raise Loads LCA
+ if( lca == stblk && st != null && Utils.find(st._inputs,load) == -1 ) // And if something moved,
+ st.addDef(load); // Add anti-dep as well
+ return lca; // Cap this stores' anti-dep to here
+ }
+ }
+ return lca;
+ }
+
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/GraphVisualizer.java b/chapter12/src/main/java/com/seaofnodes/simple/GraphVisualizer.java
new file mode 100644
index 00000000..604770a4
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/GraphVisualizer.java
@@ -0,0 +1,280 @@
+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/IRPrinter.java b/chapter12/src/main/java/com/seaofnodes/simple/IRPrinter.java
new file mode 100644
index 00000000..43c6c9f7
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/IRPrinter.java
@@ -0,0 +1,266 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.node.*;
+import java.lang.StringBuilder;
+import java.util.ArrayList;
+import java.util.BitSet;
+import java.util.HashMap;
+
+public class IRPrinter {
+
+ // 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 static void _printLine( Node n, StringBuilder sb ) {
+ sb.append("%4d %-7.7s ".formatted(n._nid,n.label()));
+ if( n._inputs==null ) {
+ sb.append("DEAD\n");
+ return;
+ }
+ for( Node def : n._inputs )
+ sb.append(def==null ? "____ " : "%4d ".formatted(def._nid));
+ for( int i = n._inputs.size(); i<4; i++ )
+ sb.append(" ");
+ sb.append(" [[ ");
+ for( Node use : n._outputs )
+ sb.append(use==null ? "____ " : "%4d ".formatted(use._nid));
+ int lim = 6 - Math.max(n._inputs.size(),4);
+ for( int i = n._outputs.size(); i<lim; i++ )
+ sb.append(" ");
+ sb.append(" ]] ");
+ if( n._type!= null ) sb.append(n._type.str());
+ sb.append("\n");
+ }
+
+ private static StringBuilder nodeId(StringBuilder sb, Node n) {
+ sb.append("%%%d".formatted(n._nid));
+ if (n instanceof ProjNode proj) {
+ sb.append(".").append(proj._idx);
+ }
+ return sb;
+ }
+
+ // Print a node on 1 line, format is inspired by LLVM
+ // %id: TYPE = NODE(inputs ....)
+ // Nodes as referred to as %id
+ public static void _printLineLlvmFormat( Node n, StringBuilder sb ) {
+ nodeId(sb, n).append(": ");
+ if( n._inputs==null ) {
+ sb.append("DEAD\n");
+ return;
+ }
+ if( n._type!= null ) n._type.typeName(sb);
+ sb.append(" = ").append( n.label() ).append( "(" );
+ for( int i = 0; i < n._inputs.size(); i++ ) {
+ Node def = n.in(i);
+ if (i > 0)
+ sb.append(", ");
+ if (def == null) sb.append("_");
+ else nodeId(sb, def);
+ }
+ sb.append(")").append("\n");
+ }
+
+ public static void printLine( Node n, StringBuilder sb, boolean llvmFormat ) {
+ if (llvmFormat) _printLineLlvmFormat( n, sb );
+ else _printLine ( n, sb );
+ }
+
+ public static String prettyPrint(Node node, int depth) {
+ return Parser.SCHEDULED
+ ? prettyPrintScheduled( node, depth, false )
+ : prettyPrint( node, depth, false );
+ }
+
+ // Another bulk pretty-printer. Makes more effort at basic-block grouping.
+ public static String prettyPrint( Node node, int depth, boolean llvmFormat ) {
+ // 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
+ printLine( n, sb, llvmFormat ); // Print head
+ while( --i >= 0 ) {
+ Node t = rpos.get(i);
+ if( !t.isMultiTail() ) { i++; break; }
+ printLine( t, sb, llvmFormat );
+ }
+ sb.append("\n"); // Blank after multitail
+ gap = true;
+ } else {
+ printLine( n, sb, llvmFormat );
+ 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!=null && use.isCFG() && use.nOuts()>=1 && !(use._outputs.get(0) instanceof LoopNode) )
+ postOrd(use, rpos,visit,bfs);
+ for( Node use : n._outputs )
+ if( use!=null && use.isCFG() )
+ postOrd(use,rpos,visit,bfs);
+ }
+ for( Node use : n._outputs )
+ if( use != null )
+ 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;
+ }
+ }
+
+ // Bulk pretty printer, knowing scheduling information is available
+ public static String prettyPrintScheduled( Node node, int depth, boolean llvmFormat ) {
+ // Backwards DFS walk to depth.
+ HashMap<Node,Integer> ds = new HashMap<>();
+ _walk(ds,node,depth);
+ // Print by block with least idepth
+ StringBuilder sb = new StringBuilder();
+ ArrayList<Node> bns = new ArrayList<>();
+ while( !ds.isEmpty() ) {
+ CFGNode blk = null;
+ for( Node n : ds.keySet() ) {
+ CFGNode cfg = n instanceof CFGNode cfg0 && cfg0.blockHead() ? cfg0 : (CFGNode)n.in(0);
+ if( blk==null || cfg.idepth() < blk.idepth() )
+ blk = cfg;
+ }
+ ds.remove(blk);
+
+ // Print block header
+ sb.append("%-13.13s".formatted(label(blk)+":"));
+ sb.append( " ".repeat(4) ).append(" [[ ");
+ if( blk instanceof RegionNode || blk instanceof StopNode )
+ for( int i=(blk instanceof StopNode ? 0 : 1); i<blk.nIns(); i++ )
+ label(sb,blk.cfg(i));
+ else if( !(blk instanceof StartNode) )
+ label(sb,blk.cfg(0));
+ sb.append(" ]] \n");
+
+ // Collect block contents that are in the depth limit
+ bns.clear();
+ int xd = Integer.MAX_VALUE;
+ for( Node use : blk._outputs ) {
+ Integer i = ds.get(use);
+ if( i!=null && !(use instanceof CFGNode cfg && cfg.blockHead()) ) {
+ bns.add(use);
+ xd = Math.min(xd,i);
+ }
+ }
+ // Print Phis up front, if any
+ for( int i=0; i<bns.size(); i++ )
+ if( bns.get(i) instanceof PhiNode phi )
+ printLine( phi, sb, llvmFormat,bns,i--,ds);
+
+ // Print block contents in depth order, bumping depth until whole block printed
+ for( ; !bns.isEmpty(); xd++ )
+ for( int i=0; i<bns.size(); i++ )
+ if( ds.get(bns.get(i))==xd )
+ printLine( bns.get(i), sb, llvmFormat, bns, i--, ds );
+ sb.append("\n");
+ }
+ return sb.toString();
+ }
+
+ private static void _walk( HashMap<Node,Integer> ds, Node node, int d ) {
+ Integer nd = ds.get(node);
+ if( nd!=null && d <= nd ) return; // Been there, done that
+ ds.put(node,d);
+ if( d == 0 ) return; // Depth cutoff
+ for( Node def : node._inputs )
+ if( def != null )
+ _walk(ds,def,d-1);
+ }
+
+ static String label( CFGNode blk ) {
+ if( blk instanceof StartNode ) return "START";
+ return (blk instanceof LoopNode ? "LOOP" : "L")+blk._nid;
+ }
+ static void label( StringBuilder sb, CFGNode blk ) {
+ if( !blk.blockHead() ) blk = blk.cfg(0);
+ sb.append( "%-9.9s ".formatted( label( blk ) ) );
+ }
+ static void printLine( Node n, StringBuilder sb, boolean llvmFormat, ArrayList<Node> bns, int i, HashMap<Node,Integer> ds ) {
+ printLine( n, sb, llvmFormat );
+ Utils.del(bns,i);
+ ds.remove(n);
+ }
+
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/IterPeeps.java b/chapter12/src/main/java/com/seaofnodes/simple/IterPeeps.java
new file mode 100644
index 00000000..b2b76777
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/IterPeeps.java
@@ -0,0 +1,205 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.node.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.Random;
+
+/**
+ * The IterOptim runs after parsing. It iterates the peepholes to a fixed point
+ * so that no more peepholes apply. This should be linear because peepholes rarely
+ * (never?) increase code size. The graph should monotonically reduce in some
+ * dimension, which is usually size. It might also reduce in e.g. number of
+ * MulNodes or Load/Store nodes, swapping out more "expensive" Nodes for cheaper
+ * ones.
+ *
+ * The theoretical overall worklist is mindless just grabbing the next thing and
+ * doing it. If the graph changes, put the neighbors on the worklist. Lather,
+ * Rinse, Repeat until the worklist runs dry.
+ *
+ * The main issues we have to deal with:
+ *
+ * <ul>
+ * <li>Nodes have uses; replacing some set of Nodes with another requires more graph
+ * reworking. Not rocket science, but it can be fiddly. Its helpful to have a
+ * small set of graph munging utilities, and the strong invariant that the graph
+ * is stable and correct between peepholes. In our case `Node.subsume` does
+ * most of the munging, building on our prior stable Node utilities.</li>
+ *
+ * <li>Changing a Node also changes the graph "neighborhood". The neigbors need to
+ * be checked to see if THEY can also peephole, and so on. After any peephole
+ * or graph update we put a Nodes uses and defs on the worklist.</li>
+ *
+ * <li>Our strong invariant is that for all Nodes, either they are on the worklist
+ * OR no peephole applies. This invariant is easy to check, although expensive.
+ * Basically the normal "iterate peepholes to a fixed point" is linear, and this
+ * check is linear at each peephole step... so quadratic overall. Its a useful
+ * assert, but one we can disable once the overall algorithm is stable - and
+ * then turn it back on again when some new set of peepholes is misbehaving.
+ * The code for this is turned on in `IterOptim.iterate` as `assert
+ * progressOnList(stop);`</li>
+ * </ul>
+ */
+public abstract class IterPeeps {
+
+ private static final WorkList<Node> WORK = new WorkList<>();
+
+ public static <N extends Node> N add( N n ) {
+ return (N)WORK.push(n);
+ }
+
+ public static void addAll( ArrayList<Node> ary ) {
+ WORK.addAll(ary);
+ }
+
+ /**
+ * Iterate peepholes to a fixed point
+ */
+ public static StopNode iterate(StopNode stop, boolean show) {
+ assert progressOnList(stop);
+ int cnt=0;
+
+ Node n;
+ while( (n=WORK.pop()) != null ) {
+ if( n.isDead() ) continue;
+ cnt++; // Useful for debugging, searching which peephole broke things
+ Node x = n.peepholeOpt();
+ if( x != null ) {
+ if( x.isDead() ) 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
+ if( x != n || !(x instanceof ConstantNode) ) {
+ // All outputs of n (changing node) not x (prior existing node).
+ for( Node z : n._outputs ) WORK.push(z);
+ // Everybody gets a free "go again" in case they didn't get
+ // made in their final form.
+ WORK.push(x);
+ // If the result is not self, revisit all inputs (because
+ // there's a new user), and replace in the graph.
+ if( x != n ) {
+ for( Node z : n. _inputs ) WORK.push(z);
+ n.subsume(x);
+ }
+ }
+ // If there are distant neighbors, move to worklist
+ n.moveDepsToWorklist();
+ assert progressOnList(stop); // Very expensive assert
+ }
+ if( n.isUnused() && !(n instanceof StopNode) )
+ n.kill(); // Just plain dead
+ }
+
+ if( show )
+ System.out.println(new GraphVisualizer().generateDotOutput(stop,null,null));
+ GlobalCodeMotion.buildCFG(stop);
+ return stop;
+ }
+
+ // Visit ALL nodes and confirm the invariant:
+ // Either you are on the WORK worklist OR running `iter()` makes no progress.
+
+ // This invariant ensures that no progress is missed, i.e., when the
+ // worklist is empty we have indeed done all that can be done. To help
+ // with debugging, the {@code assert} is broken out in a place where it is easy to
+ // stop if a change is found.
+
+ // Also, the normal usage of `iter()` may attempt peepholes with distance
+ // neighbors and these should fail, but will then try to add dependencies
+ // {@link #Node.addDep} which is a side effect in an assert. The {@link
+ // #midAssert} is used to stop this side effect.
+ private static boolean MID_ASSERT;
+ public static boolean midAssert() { return MID_ASSERT; }
+ private static boolean progressOnList(Node stop) {
+ MID_ASSERT = true;
+ int old_cnt = Node.ITER_CNT, old_nop = Node.ITER_NOP_CNT;
+ Node changed = stop.walk( n -> {
+ if( WORK.on(n) ) return null;
+ Node m = n.peepholeOpt();
+ if( m==null ) return null;
+ System.err.println("BREAK HERE FOR BUG");
+ return m;
+ });
+ Node.ITER_CNT = old_cnt; Node.ITER_NOP_CNT = old_nop;
+ MID_ASSERT = false;
+ return changed==null;
+ }
+
+ public static void reset() {
+ WORK.clear();
+ }
+
+ /**
+ * Classic WorkList, with a fast add/remove, dup removal, random pull.
+ * The Node's nid is used to check membership in the worklist.
+ */
+ public static class WorkList<E extends Node> {
+
+ private Node[] _es;
+ private int _len;
+ private final BitSet _on; // Bit set if Node._nid is on WorkList
+ private final Random _R; // For randomizing pull from the WorkList
+ private final long _seed;
+
+ /* Useful stat - how many nodes are processed in the post parse iterative opt */
+ private long _totalWork = 0;
+
+ WorkList() { this(123); }
+ WorkList(long seed) {
+ _es = new Node[1];
+ _len=0;
+ _on = new BitSet();
+ _seed = seed;
+ _R = new Random();
+ _R.setSeed(_seed);
+ }
+
+ /**
+ * Pushes a Node on the WorkList, ensuring no duplicates
+ * If Node is null it will not be added.
+ */
+ public E push( E x ) {
+ if( x==null ) return null;
+ int idx = x._nid;
+ if( !_on.get(idx) ) {
+ _on.set(idx);
+ if( _len==_es.length )
+ _es = Arrays.copyOf(_es,_len<<1);
+ _es[_len++] = x;
+ _totalWork++;
+ }
+ return x;
+ }
+
+ public void addAll( ArrayList<E> ary ) {
+ for( E n : ary )
+ push(n);
+ }
+
+ /**
+ * True if Node is on the WorkList
+ */
+ boolean on( E x ) { return _on.get(x._nid); }
+
+ /**
+ * Removes a random Node from the WorkList; null if WorkList is empty
+ */
+ E pop() {
+ if( _len == 0 ) return null;
+ int idx = _R.nextInt(_len);
+ E x = (E)_es[idx];
+ _es[idx] = _es[--_len]; // Compress array
+ _on.clear(x._nid);
+ return x;
+ }
+
+ public void clear() {
+ _len = 0;
+ _on.clear();
+ _R.setSeed(_seed);
+ _totalWork = 0;
+ }
+ }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/Parser.java b/chapter12/src/main/java/com/seaofnodes/simple/Parser.java
new file mode 100644
index 00000000..7e957d9d
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/Parser.java
@@ -0,0 +1,853 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.node.*;
+import com.seaofnodes.simple.type.*;
+
+import java.util.*;
+
+/**
+ * The Parser converts a Simple source program to the Sea of Nodes intermediate
+ * representation directly in one pass. There is no intermediate Abstract
+ * Syntax Tree structure.
+ * <p>
+ * This is a simple recursive descent parser. All lexical analysis is done here as well.
+ */
+public class Parser {
+
+ /**
+ * A Global Static, unique to each compilation. This is a public, so we
+ * can make constants everywhere without having to thread the StartNode
+ * through the entire parser and optimizer.
+ * <p>
+ * To make the compiler multithreaded, this field will have to move into a TLS.
+ */
+ public static StartNode START;
+
+ public static ConstantNode ZERO;
+ public static XCtrlNode XCTRL;
+
+ public StopNode STOP;
+
+ // Debugger Printing.
+ public static boolean SCHEDULED; // True if debug printer can use schedule info
+
+ // The Lexer. Thin wrapper over a byte[] buffer with a cursor.
+ private final Lexer _lexer;
+
+ /**
+ * Current ScopeNode - ScopeNodes change as we parse code, but at any point of time
+ * there is one current ScopeNode. The reason the current ScopeNode can change is to do with how
+ * we handle branching. See {@link #parseIf()}.
+ * <p>
+ * Each ScopeNode contains a stack of lexical scopes, each scope is a symbol table that binds
+ * variable names to Nodes. The top of this stack represents current scope.
+ * <p>
+ * We keep a list of all ScopeNodes so that we can show them in graphs.
+ * @see #parseIf()
+ * @see #_xScopes
+ */
+ public ScopeNode _scope;
+
+ /**
+ * List of keywords disallowed as identifiers
+ */
+ private static final HashSet<String> KEYWORDS = new HashSet<>(){{
+ add("break");
+ add("continue");
+ add("else");
+ add("false");
+ add("if");
+ add("int");
+ add("new");
+ add("null");
+ add("return");
+ add("struct");
+ add("true");
+ add("while");
+ }};
+
+
+ /**
+ * We clone ScopeNodes when control flows branch; it is useful to have
+ * a list of all active ScopeNodes for purposes of visualization of the SoN graph
+ */
+ public final Stack<ScopeNode> _xScopes = new Stack<>();
+
+ ScopeNode _continueScope;
+ ScopeNode _breakScope;
+
+ // Mapping from a Struct name to a Struct.
+ public static Map<String, TypeStruct> OBJS = new HashMap<>();
+
+ public Parser(String source, TypeInteger arg) {
+ Node.reset();
+ IterPeeps.reset();
+ OBJS.clear();
+ SCHEDULED = false;
+ _lexer = new Lexer(source);
+ _scope = new ScopeNode();
+ _continueScope = _breakScope = null;
+ START = new StartNode(new Type[]{ Type.CONTROL, arg });
+ STOP = new StopNode(source);
+ ZERO = new ConstantNode(TypeInteger.constant(0)).peephole().keep();
+ XCTRL= new XCtrlNode().peephole().keep();
+ }
+
+ public Parser(String source) {
+ this(source, TypeInteger.BOT);
+ }
+
+ @Override
+ public String toString() { return _lexer.toString(); }
+
+ String src() { return new String( _lexer._input ); }
+
+ // Debugging utility to find a Node by index
+ public static Node find(int nid) { return START.find(nid); }
+
+ private Node ctrl() { return _scope.ctrl(); }
+
+ private Node ctrl(Node n) { return _scope.ctrl(n); }
+
+ public StopNode parse() { return parse(false); }
+ public StopNode parse(boolean show) {
+ _xScopes.push(_scope);
+ // Enter a new scope for the initial control and arguments
+ _scope.push();
+ _scope.define(ScopeNode.CTRL, Type.CONTROL , new CProjNode(START, 0, ScopeNode.CTRL).peephole());
+ _scope.define(ScopeNode.ARG0, TypeInteger.BOT, new ProjNode(START, 1, ScopeNode.ARG0).peephole());
+ parseBlock();
+ if( ctrl()._type==Type.CONTROL )
+ STOP.addReturn(new ReturnNode(ctrl(), new ConstantNode(TypeInteger.constant(0)).peephole(), _scope).peephole());
+ _scope.pop();
+ _xScopes.pop();
+ if (!_lexer.isEOF()) throw error("Syntax error, unexpected " + _lexer.getAnyNextToken());
+ STOP.peephole();
+ if( show ) showGraph();
+ return STOP;
+ }
+
+ /**
+ * Parses a block
+ *
+ * <pre>
+ * '{' statements '}'
+ * </pre>
+ * Does not parse the opening or closing '{}'
+ * @return a {@link Node} or {@code null}
+ */
+ private Node parseBlock() {
+ // Enter a new scope
+ _scope.push();
+ while (!peek('}') && !_lexer.isEOF())
+ parseStatement();
+ // Exit scope
+ _scope.pop();
+ return null;
+ }
+
+ /**
+ * Parses a statement
+ *
+ * <pre>
+ * returnStatement | declStatement | blockStatement | ifStatement | expressionStatement
+ * </pre>
+ * @return a {@link Node} or {@code null}
+ */
+ private Node parseStatement() {
+ if( false ) return null;
+ else if (matchx("return") ) return parseReturn();
+ else if (matchx("int") ) return parseDecl(TypeInteger.BOT);
+ else if (match ("{") ) return require(parseBlock(),"}");
+ else if (matchx("if") ) return parseIf();
+ else if (matchx("while") ) return parseWhile();
+ 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(),";");
+ // declarations of vars with struct type are handled in parseExpressionStatement due
+ // to ambiguity
+ else return parseExpressionStatement();
+ }
+
+ /**
+ * Parse a struct field.
+ * <pre>
+ * int IDENTIFIER ;
+ * </pre>
+ */
+ private Field parseField(String sname) {
+ if( matchx("int") ) // Currently only parsing "int" type fields
+ return require(Field.make(requireId().intern(),TypeInteger.BOT),";");
+ throw errorSyntax("A field type is expected, only type 'int' is supported at present");
+ }
+
+ /**
+ * Parse a struct declaration, and return the following statement.
+ * Only allowed in top level scope.
+ * Structs cannot be redefined.
+ *
+ * @return The statement following the struct
+ */
+ private Node parseStruct() {
+ if (_xScopes.size() > 1) throw errorSyntax("struct declarations can only appear in top level scope");
+ String typeName = requireId();
+ if ( OBJS.containsKey(typeName)) throw errorSyntax("struct '" + typeName + "' cannot be redefined");
+ ArrayList<Field> fields = new ArrayList<>();
+ require("{");
+ while (!peek('}') && !_lexer.isEOF()) {
+ Field field = parseField(typeName);
+ if (fields.contains(field)) throw errorSyntax("Field '" + field + "' already defined in struct '" + typeName + "'");
+ fields.add(field);
+ }
+ require("}");
+ // Build and install the TypeStruct
+ TypeStruct ts = TypeStruct.make(typeName, fields);
+ OBJS.put(typeName, ts); // Insert the struct name in the collection of all struct names
+ START.addMemProj(ts, _scope); // Insert memory edges
+ return parseStatement();
+ }
+
+ /**
+ * Parses a while statement
+ *
+ * <pre>
+ * while ( expression ) statement
+ * </pre>
+ * @return a {@link Node}, never {@code null}
+ */
+ private Node parseWhile() {
+
+ var savedContinueScope = _continueScope;
+ var savedBreakScope = _breakScope;
+
+ require("(");
+
+ // Loop region has two control inputs, the first is the entry
+ // point, and second is back edge that is set after loop is parsed
+ // (see end_loop() call below). Note that the absence of back edge is
+ // used as an indicator to switch off peepholes of the region and
+ // associated phis; see {@code inProgress()}.
+
+ ctrl(new LoopNode(ctrl()).peephole()); // Note we set back edge to null here
+
+ // At loop head, we clone the current Scope (this includes all
+ // names in every nesting level within the Scope).
+ // We create phis eagerly for all the names we find, see dup().
+
+ // Save the current scope as the loop head
+ ScopeNode head = _scope.keep();
+ // Clone the head Scope to create a new Scope for the body.
+ // Create phis eagerly as part of cloning
+ _xScopes.push(_scope = _scope.dup(true)); // The true argument triggers creating phis
+
+ // Parse predicate
+ var pred = require(parseExpression(), ")");
+ // IfNode takes current control and predicate
+ Node ifNode = new IfNode(ctrl(), pred).peephole();
+ // Setup projection nodes
+ Node ifT = new CProjNode(ifNode. keep(), 0, "True" ).peephole().keep();
+ Node ifF = new CProjNode(ifNode.unkeep(), 1, "False").peephole();
+
+ // Clone the body Scope to create the break/exit Scope which accounts for any
+ // side effects in the predicate. The break/exit Scope will be the final
+ // scope after the loop, and its control input is the False branch of
+ // the loop predicate. Note that body Scope is still our current scope.
+ ctrl(ifF);
+ _xScopes.push(_breakScope = _scope.dup());
+
+ // No continues yet
+ _continueScope = null;
+
+ // Parse the true side, which corresponds to loop body
+ // Our current scope is the body Scope
+ ctrl(ifT.unkeep()); // set ctrl token to ifTrue projection
+ parseStatement(); // Parse loop body
+
+ // Merge the loop bottom into other continue statements
+ if (_continueScope != null) {
+ _continueScope = jumpTo(_continueScope);
+ _scope.kill();
+ _scope = _continueScope;
+ }
+
+ // The true branch loops back, so whatever is current _scope.ctrl gets
+ // added to head loop as input. endLoop() updates the head scope, and
+ // goes through all the phis that were created earlier. For each phi,
+ // it sets the second input to the corresponding input from the back
+ // edge. If the phi is redundant, it is replaced by its sole input.
+ var exit = _breakScope;
+ head.endLoop(_scope, exit);
+ head.unkeep().kill();
+
+ _xScopes.pop(); // Cleanup
+ _xScopes.pop(); // Cleanup
+
+ _continueScope = savedContinueScope;
+ _breakScope = savedBreakScope;
+
+ // At exit the false control is the current control, and
+ // the scope is the exit scope after the exit test.
+ return _scope = exit;
+ }
+
+ private ScopeNode jumpTo(ScopeNode toScope) {
+ ScopeNode cur = _scope.dup();
+ ctrl(XCTRL); // Kill current scope
+ // Prune nested lexical scopes that have depth > than the loop head
+ // We use _breakScope as a proxy for the loop head scope to obtain the depth
+ while( cur._scopes.size() > _breakScope._scopes.size() )
+ cur.pop();
+ // If this is a continue then first time the target is null
+ // So we just use the pruned current scope as the base for the
+ // "continue"
+ if (toScope == null)
+ return cur;
+ // toScope is either the break scope, or a scope that was created here
+ assert toScope._scopes.size() <= _breakScope._scopes.size();
+ toScope.ctrl(toScope.mergeScopes(cur));
+ return toScope;
+ }
+
+ private void checkLoopActive() { if (_breakScope == null) throw Parser.error("No active loop for a break or continue"); }
+
+ private Node parseBreak () { checkLoopActive(); return ( _breakScope = require(jumpTo( _breakScope ),";")); }
+ private Node parseContinue() { checkLoopActive(); return (_continueScope = require(jumpTo( _continueScope ),";")); }
+
+ /**
+ * Parses a statement
+ *
+ * <pre>
+ * if ( expression ) statement [else statement]
+ * </pre>
+ * @return a {@link Node}, never {@code null}
+ */
+ private Node parseIf() {
+ require("(");
+ // Parse predicate
+ var pred = require(parseExpression(), ")").keep();
+ // IfNode takes current control and predicate
+ Node ifNode = new IfNode(ctrl(), pred).peephole();
+ // Setup projection nodes
+ Node ifT = new CProjNode(ifNode. keep(), 0, "True" ).peephole().keep();
+ Node ifF = new CProjNode(ifNode.unkeep(), 1, "False").peephole().keep();
+ // In if true branch, the ifT proj node becomes the ctrl
+ // But first clone the scope and set it as current
+ int ndefs = _scope.nIns();
+ ScopeNode fScope = _scope.dup(); // Duplicate current scope
+ _xScopes.push(fScope); // For graph visualization we need all scopes
+
+ // Parse the true side
+ ctrl(ifT.unkeep()); // set ctrl token to ifTrue projection
+ _scope.upcast(ifT,pred,false); // Up-cast predicate
+ parseStatement(); // Parse true-side
+ ScopeNode tScope = _scope;
+
+ // Parse the false side
+ _scope = fScope; // Restore scope, then parse else block if any
+ ctrl(ifF.unkeep()); // Ctrl token is now set to ifFalse projection
+ if (matchx("else")) {
+ _scope.upcast(ifF,pred,true); // Up-cast predicate
+ parseStatement();
+ fScope = _scope;
+ }
+ pred.unkeep();
+
+ if( tScope.nIns() != ndefs || fScope.nIns() != ndefs )
+ throw error("Cannot define a new name on one arm of an if");
+
+ // Merge results
+ _scope = tScope;
+ _xScopes.pop(); // Discard pushed from graph display
+
+ return ctrl(tScope.mergeScopes(fScope));
+ }
+
+
+ /**
+ * Parses a return statement; "return" already parsed.
+ * The $ctrl edge is killed.
+ *
+ * <pre>
+ * 'return' expr ;
+ * </pre>
+ * @return an expression {@link Node}, never {@code null}
+ */
+ private Node parseReturn() {
+ var expr = require(parseExpression(), ";");
+ Node ret = STOP.addReturn(new ReturnNode(ctrl(), expr, _scope).peephole());
+ ctrl(XCTRL); // Kill control
+ 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
+ *
+ * <pre>
+ * name; // Error
+ * type name; // Define name with default initial value
+ * type name = expr; // Define name with given initial value
+ * name = expr; // Reassign existing
+ * expr // Something else
+ * </pre>
+ * @return an expression {@link Node}, never {@code null}
+ */
+ private Node parseExpressionStatement() {
+ int old = _lexer._position;
+ Type t = type();
+ String name = requireId();
+ Node expr;
+ if( match(";") ) { // Assign a default value
+ // No type and no expr is an error
+ if( t==null ) throw errorSyntax("expression");
+ expr = new ConstantNode(t.makeInit()).peephole();
+ } else if( match("=") ) { // Assign "= expr;"
+ expr = require(parseExpression(), ";");
+ } else { // Neither, so just a normal expression parse
+ _lexer._position = old;
+ return require(parseExpression(),";");
+ }
+
+ // Defining a new variable vs updating an old one
+ if( t != null ) {
+ if( _scope.define(name,t,expr) == null )
+ throw error("Redefining name '" + name + "'");
+ } else {
+ Node n = _scope.lookup(name);
+ t = _scope.lookupDeclaredType(name);
+ if( n==null )
+ throw error("Undefined name '" + name + "'");
+ _scope.update(name,expr);
+ }
+ if( !expr._type.isa(t) )
+ throw error("Type " + expr._type.str() + " is not of declared type " + t.str());
+ return expr;
+ }
+
+ // Parse a type-or-null
+ private Type type() {
+ int old = _lexer._position;
+ String tname = _lexer.matchId();
+ if( tname==null ) return null;
+ if( tname.equals("int") ) return TypeInteger.BOT;
+ TypeStruct obj = OBJS.get(tname);
+ if( obj != null )
+ return TypeMemPtr.make(obj,match("?"));
+ // Not a type; unwind the parse
+ _lexer._position = old;
+ return null;
+ }
+
+ /**
+ * Parses a declStatement
+ *
+ * <pre>
+ * type name = expression ';'
+ * </pre>
+ * @return an expression {@link Node}
+ */
+ private Node parseDecl(Type t) {
+ var name = requireId();
+ var expr = match(";")
+ // Assign a null value
+ ? new ConstantNode(t.makeInit()).peephole()
+ // Assign "= expr;"
+ : require(require("=").parseExpression(), ";");
+ if( !expr._type.isa(t) )
+ throw error("Type " + expr._type.str() + " is not of declared type " + t.str());
+ if( _scope.define(name,t,expr) == null )
+ throw error("Redefining name '" + name + "'");
+ return expr;
+ }
+
+
+ /**
+ * Parse an expression of the form:
+ *
+ * <pre>
+ * expr : compareExpr
+ * </pre>
+ * @return an expression {@link Node}, never {@code null}
+ */
+ private Node parseExpression() { return parseComparison(); }
+
+ /**
+ * Parse an expression of the form:
+ *
+ * <pre>
+ * expr : additiveExpr op additiveExpr
+ * </pre>
+ * @return an comparator expression {@link Node}, never {@code null}
+ */
+ private Node parseComparison() {
+ var lhs = parseAddition();
+ while( true ) {
+ int idx=0; boolean negate=false;
+ // Test for any local nodes made, and "keep" lhs during peepholes
+ if( false ) ;
+ else if( match("==") ) { idx=2; lhs = new BoolNode.EQ(lhs, null); }
+ else if( match("!=") ) { idx=2; lhs = new BoolNode.EQ(lhs, null); negate=true; }
+ else if( match("<=") ) { idx=2; lhs = new BoolNode.LE(lhs, null); }
+ else if( match("<" ) ) { idx=2; lhs = new BoolNode.LT(lhs, null); }
+ else if( match(">=") ) { idx=1; lhs = new BoolNode.LE(null, lhs); }
+ else if( match(">" ) ) { idx=1; lhs = new BoolNode.LT(null, lhs); }
+ else break;
+ // Peepholes can fire, but lhs is already "hooked", kept alive
+ lhs.setDef(idx,parseAddition());
+ lhs = lhs.peephole();
+ if( negate ) // Extra negate for !=
+ lhs = new NotNode(lhs).peephole();
+ }
+ return lhs;
+ }
+
+ /**
+ * Parse an additive expression
+ *
+ * <pre>
+ * additiveExpr : multiplicativeExpr (('+' | '-') multiplicativeExpr)*
+ * </pre>
+ * @return an add expression {@link Node}, never {@code null}
+ */
+ private Node parseAddition() {
+ Node lhs = parseMultiplication();
+ while( true ) {
+ if( false ) ;
+ else if( match("+") ) lhs = new AddNode(lhs,null);
+ else if( match("-") ) lhs = new SubNode(lhs,null);
+ else break;
+ lhs.setDef(2,parseMultiplication());
+ lhs = lhs.peephole();
+ }
+ return lhs;
+ }
+
+ /**
+ * Parse an multiplicativeExpr expression
+ *
+ * <pre>
+ * multiplicativeExpr : unaryExpr (('*' | '/') unaryExpr)*
+ * </pre>
+ * @return a multiply expression {@link Node}, never {@code null}
+ */
+ private Node parseMultiplication() {
+ var lhs = parseUnary();
+ while( true ) {
+ if( false ) ;
+ else if( match("*") ) lhs = new MulNode(lhs,null);
+ else if( match("/") ) lhs = new DivNode(lhs,null);
+ else break;
+ lhs.setDef(2,parseUnary());
+ lhs = lhs.peephole();
+ }
+ return lhs;
+ }
+
+ /**
+ * Parse a unary minus expression.
+ *
+ * <pre>
+ * unaryExpr : ('-') | '!') unaryExpr | postfixExpr | primaryExpr
+ * </pre>
+ * @return a unary expression {@link Node}, never {@code null}
+ */
+ private Node parseUnary() {
+ if (match("-")) return new MinusNode(parseUnary()).peephole();
+ if (match("!")) return new NotNode(parseUnary()).peephole();
+ return parsePostfix(parsePrimary());
+ }
+
+ /**
+ * Parse a primary expression:
+ *
+ * <pre>
+ * primaryExpr : integerLiteral | Identifier | true | false | null | new Identifier | '(' expression ')'
+ * </pre>
+ * @return a primary {@link Node}, never {@code null}
+ */
+ private Node parsePrimary() {
+ if( _lexer.isNumber() ) return parseIntegerLiteral();
+ if( match("(") ) return require(parseExpression(), ")");
+ if( matchx("true" ) ) return new ConstantNode(TypeInteger.constant(1)).peephole();
+ if( matchx("false") ) return ZERO;
+ if( matchx("null" ) ) return new ConstantNode(TypeMemPtr.NULLPTR).peephole();
+ if( matchx("new") ) {
+ String structName = requireId();
+ TypeStruct obj = OBJS.get(structName);
+ if( obj == null) throw errorSyntax("Unknown struct type '" + structName + "'");
+ return newStruct(obj);
+ }
+ String name = _lexer.matchId();
+ if( name == null) throw errorSyntax("an identifier or expression");
+ Node n = _scope.lookup(name);
+ if( n!=null ) return n;
+ throw error("Undefined name '" + name + "'");
+ }
+
+ /**
+ * Return a NewNode but also generate instructions to initialize it.
+ */
+ private Node newStruct(TypeStruct obj) {
+ Node n = new NewNode(TypeMemPtr.make(obj), ctrl()).peephole().keep();
+ int alias = START._aliasStarts.get(obj._name);
+ for( Field field : obj._fields ) {
+ memAlias(alias, new StoreNode(field._fname, alias, memAlias(alias), n, ZERO).peephole());
+ alias++;
+ }
+ return n.unkeep();
+ }
+
+ // We set up memory aliases by inserting special vars in the scope these
+ // variables are prefixed by $ so they cannot be referenced in Simple code.
+ // Using vars has the benefit that all the existing machinery of scoping
+ // and phis work as expected
+ private Node memAlias(int alias ) { return _scope.lookup(memName(alias) ); }
+ private Node memAlias(int alias, Node st) { return _scope.update(memName(alias), st); }
+ public static String memName(int alias) { return ("$"+alias).intern(); }
+
+ /**
+ * Parse postfix expression. For now this is just a field
+ * expression, but in future could be array index too.
+ *
+ * <pre>
+ * expr ('.' IDENTIFIER)* [ = expr ]
+ * </pre>
+ */
+ private Node parsePostfix(Node expr) {
+ if( !match(".") ) return expr;
+
+ if( !(expr._type instanceof TypeMemPtr ptr) )
+ throw error("Expected struct reference but got " + expr._type.str());
+
+ String name = requireId().intern();
+ int idx = ptr._obj==null ? -1 : ptr._obj.find(name);
+ if( idx == -1 ) throw error("Accessing unknown field '" + name + "' from '" + ptr.str() + "'");
+ int alias = START._aliasStarts.get(ptr._obj._name)+idx;
+
+ if( match("=") ) {
+ // Disambiguate "obj.fld==x" boolean test from "obj.fld=x" field assignment
+ if( peek('=') ) _lexer._position--;
+ else {
+ Node val = parseExpression();
+ memAlias(alias, new StoreNode(name, alias, memAlias(alias), expr, val).peephole());
+ return expr; // "obj.a = expr" returns the expression while updating memory
+ }
+ }
+
+ Type declaredType = ptr._obj._fields[idx]._type;
+ return parsePostfix(new LoadNode(name, alias, declaredType, memAlias(alias), expr).peephole());
+ }
+
+ /**
+ * Parse integer literal
+ *
+ * <pre>
+ * integerLiteral: [1-9][0-9]* | [0]
+ * </pre>
+ */
+ private ConstantNode parseIntegerLiteral() {
+ return (ConstantNode) new ConstantNode(_lexer.parseNumber()).peephole();
+ }
+
+ //////////////////////////////////
+ // Utilities for lexical analysis
+
+ // Return true and skip if "syntax" is next in the stream.
+ private boolean match (String syntax) { return _lexer.match (syntax); }
+ // Match must be "exact", not be followed by more id letters
+ private boolean matchx(String syntax) { return _lexer.matchx(syntax); }
+ // Return true and do NOT skip if 'ch' is next
+ private boolean peek(char ch) { return _lexer.peek(ch); }
+ private boolean peekIsId() { return _lexer.peekIsId(); }
+
+ // Require and return an identifier
+ private String requireId() {
+ String id = _lexer.matchId();
+ if (id != null && !KEYWORDS.contains(id) ) return id;
+ throw error("Expected an identifier, found '"+id+"'");
+ }
+
+
+
+ // Require an exact match
+ private Parser require(String syntax) { require(null, syntax); return this; }
+ private <N> N require(N n, String syntax) {
+ if (match(syntax)) return n;
+ throw errorSyntax(syntax);
+ }
+
+ RuntimeException errorSyntax(String syntax) {
+ return error("Syntax error, expected " + syntax + ": " + _lexer.getAnyNextToken());
+ }
+
+ public static RuntimeException error( String errorMessage ) {
+ return new RuntimeException(errorMessage);
+ }
+
+ ////////////////////////////////////
+ // Lexer components
+
+ private static class Lexer {
+
+ // Input buffer; an array of text bytes read from a file or a string
+ private final byte[] _input;
+ // Tracks current position in input buffer
+ private int _position = 0;
+
+ /**
+ * Record the source text for lexing
+ */
+ public Lexer(String source) {
+ this(source.getBytes());
+ }
+
+ /**
+ * Direct from disk file source
+ */
+ public Lexer(byte[] buf) {
+ _input = buf;
+ }
+
+ // Very handy in the debugger, shows the unparsed program
+ @Override
+ public String toString() {
+ return new String(_input, _position, _input.length - _position);
+ }
+
+ // True if at EOF
+ private boolean isEOF() {
+ return _position >= _input.length;
+ }
+
+ // Peek next character, or report EOF
+ private char peek() {
+ return isEOF() ? Character.MAX_VALUE // Special value that causes parsing to terminate
+ : (char) _input[_position];
+ }
+
+ private char nextChar() {
+ char ch = peek();
+ _position++;
+ return ch;
+ }
+
+ // True if a white space
+ private boolean isWhiteSpace() {
+ return peek() <= ' '; // Includes all the use space, tab, newline, CR
+ }
+
+ /**
+ * Return the next non-white-space character
+ */
+ private void skipWhiteSpace() {
+ while( true ) {
+ if( isWhiteSpace() ) _position++;
+ // Skip // to end of line
+ else if( _position+2 < _input.length &&
+ _input[_position ] == '/' &&
+ _input[_position+1] == '/') {
+ _position += 2;
+ while( !isEOF() && _input[_position] != '\n' ) _position++;
+ } else break;
+ }
+ }
+
+
+ // Return true, if we find "syntax" after skipping white space; also
+ // then advance the cursor past syntax.
+ // Return false otherwise, and do not advance the cursor.
+ boolean match(String syntax) {
+ skipWhiteSpace();
+ int len = syntax.length();
+ if (_position + len > _input.length) return false;
+ for (int i = 0; i < len; i++)
+ if ((char) _input[_position + i] != syntax.charAt(i))
+ return false;
+ _position += len;
+ return true;
+ }
+
+ // Match must be exact and not followed by more ID characters.
+ // Prevents identifier "ifxy" from matching an "if" statement.
+ boolean matchx(String syntax) {
+ if( !match(syntax) ) return false;
+ if( !isIdLetter(peek()) ) return true;
+ _position -= syntax.length();
+ return false;
+ }
+
+ private boolean peek(char ch) {
+ skipWhiteSpace();
+ return peek()==ch;
+ }
+
+ boolean peekIsId() {
+ skipWhiteSpace();
+ return isIdStart(peek());
+ }
+
+ // Return an identifier or null
+ String matchId() {
+ return peekIsId() ? parseId() : null;
+ }
+
+ // Used for errors
+ String getAnyNextToken() {
+ if (isEOF()) return "";
+ if (isIdStart(peek())) return parseId();
+ if (isNumber(peek())) return parseNumberString();
+ if (isPunctuation(peek())) return parsePunctuation();
+ return String.valueOf(peek());
+ }
+
+ boolean isNumber() {return isNumber(peek());}
+ boolean isNumber(char ch) {return Character.isDigit(ch);}
+
+ private Type parseNumber() {
+ String snum = parseNumberString();
+ if (snum.length() > 1 && snum.charAt(0) == '0')
+ throw error("Syntax error: integer values cannot start with '0'");
+ return TypeInteger.constant(Long.parseLong(snum));
+ }
+ private String parseNumberString() {
+ int start = _position;
+ while (isNumber(nextChar())) ;
+ return new String(_input, start, --_position - start);
+ }
+
+ // First letter of an identifier
+ private boolean isIdStart(char ch) {
+ return Character.isAlphabetic(ch) || ch == '_';
+ }
+
+ // All characters of an identifier, e.g. "_x123"
+ private boolean isIdLetter(char ch) {
+ return Character.isLetterOrDigit(ch) || ch == '_';
+ }
+
+ private String parseId() {
+ int start = _position;
+ while (isIdLetter(nextChar())) ;
+ return new String(_input, start, --_position - start);
+ }
+
+ //
+ private boolean isPunctuation(char ch) {
+ return "=;[]<>()+-/*".indexOf(ch) != -1;
+ }
+
+ private String parsePunctuation() {
+ int start = _position;
+ return new String(_input, start, 1);
+ }
+ }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/Utils.java b/chapter12/src/main/java/com/seaofnodes/simple/Utils.java
new file mode 100644
index 00000000..f3ac25b1
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/Utils.java
@@ -0,0 +1,58 @@
+package com.seaofnodes.simple;
+
+import java.util.ArrayList;
+
+public class Utils {
+ public static RuntimeException TODO() { return TODO("Not yet implemented"); }
+ public static RuntimeException TODO(String msg) { return new RuntimeException(msg); }
+
+ /**
+ * Fast, constant-time, element removal. Does not preserve order
+ *
+ * @param array ArrayList to modify
+ * @param i element to be removed
+ * @return element removed
+ */
+ public static <E> E del(ArrayList<E> array, int i) {
+ if ( i >= 0 && i < array.size() ) {
+ E tmp = array.get(i);
+ E last = array.removeLast();
+ if (i < array.size()) array.set(i, last);
+ return tmp;
+ }
+ return null;
+ }
+
+ /**
+ * Search a list for an element by reference
+ *
+ * @param ary List to search in
+ * @param x Object to be searched
+ * @return >= 0 on success, -1 on failure
+ */
+ public static <E> int find( ArrayList<E> ary, E x ) {
+ for( int i=0; i<ary.size(); i++ )
+ if( ary.get(i)==x )
+ return i;
+ return -1;
+ }
+ /**
+ * Search a list for an element by reference
+ *
+ * @param ary List to search in
+ * @param x Object to be searched
+ * @return >= 0 on success, -1 on failure
+ */
+ public static <E> int find( E[] ary, E x ) {
+ for( int i=0; i<ary.length; i++ )
+ if( ary[i]==x )
+ return i;
+ return -1;
+ }
+
+ // Rotate a long, nice for hashes
+ public static long rot( long x, int n ) { return (x<<n) | (x>>>n); }
+
+ // Fold a 64bit hash into 32 bits
+ public static int fold( long x ) { return (int)((x>>32) ^ x); }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/AddNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/AddNode.java
new file mode 100644
index 00000000..2cc0b465
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/AddNode.java
@@ -0,0 +1,174 @@
+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 public String glabel() { return "+"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ in(1)._print0(sb.append("("), visited);
+ in(2)._print0(sb.append("+"), visited);
+ return sb.append(")");
+ }
+
+
+ @Override
+ public Type compute() {
+ if( in(1)._type instanceof TypeInteger i0 &&
+ in(2)._type instanceof TypeInteger i1 ) {
+ if (i0.isConstant() && i1.isConstant())
+ return TypeInteger.constant(i0.value()+i1.value());
+ }
+ return in(1)._type.meet(in(2)._type);
+ }
+
+ @Override
+ public Node idealize () {
+ Node lhs = in(1);
+ Node rhs = in(2);
+ Type t2 = rhs._type;
+
+ // Add of 0. We do not check for (0+x) because this will already
+ // canonicalize to (x+0)
+ if( t2 instanceof TypeInteger i && i.value()==0 )
+ return lhs;
+
+ // Add of same to a multiply by 2
+ if( lhs==rhs )
+ return new MulNode(lhs,new ConstantNode(TypeInteger.constant(2)).peephole());
+
+ // Goal: a left-spine set of adds, with constants on the rhs (which then fold).
+
+ // Move non-adds to RHS
+ if( !(lhs instanceof AddNode) && rhs instanceof AddNode )
+ return swap12();
+
+ // x+(-y) becomes x-y
+ if( rhs instanceof MinusNode minus )
+ return new SubNode(lhs,minus.in(1));
+
+ // Now we might see (add add non) or (add non non) or (add add add) but never (add non add)
+
+ // Do we have x + (y + z) ?
+ // Swap to (x + y) + z
+ // Rotate (add add add) to remove the add on RHS
+ if( rhs instanceof AddNode add )
+ return new AddNode(new AddNode(lhs,add.in(1)).peephole(), add.in(2));
+
+ // Now we might see (add add non) or (add non non) but never (add non add) nor (add add add)
+ if( !(lhs instanceof AddNode) )
+ // Rotate; look for (add (phi cons) con/(phi cons))
+ return spine_cmp(lhs,rhs,this) ? swap12() : phiCon(this,true);
+
+ // Now we only see (add add non)
+
+ // Dead data cycle; comes about from dead infinite loops. Do nothing,
+ // the loop will peep as dead after a bit.
+ if( lhs.in(1) == lhs )
+ return null;
+
+ // Do we have (x + con1) + con2?
+ // Replace with (x + (con1+con2) which then fold the constants
+ // lhs.in(2) is con1 here
+ // If lhs.in(2) is not a constant, we add ourselves as a dependency
+ // because if it later became a constant then we could make this
+ // transformation.
+ if( lhs.in(2).addDep(this)._type.isConstant() && rhs._type.isConstant() )
+ return new AddNode(lhs.in(1),new AddNode(lhs.in(2),rhs).peephole());
+
+
+ // Do we have ((x + (phi cons)) + con) ?
+ // Do we have ((x + (phi cons)) + (phi cons)) ?
+ // Push constant up through the phi: x + (phi con0+con0 con1+con1...)
+ Node phicon = phiCon(this,true);
+ if( phicon!=null ) return phicon;
+
+ // Now we sort along the spine via rotates, to gather similar things together.
+
+ // Do we rotate (x + y) + z
+ // into (x + z) + y ?
+ if( spine_cmp(lhs.in(2),rhs,this) )
+ return new AddNode(new AddNode(lhs.in(1),rhs).peephole(),lhs.in(2));
+
+ return null;
+ }
+
+ // Rotation is only valid for associative ops, e.g. Add, Mul, And, Or.
+ // Do we have ((phi cons)|(x + (phi cons)) + con|(phi cons)) ?
+ // Push constant up through the phi: x + (phi con0+con0 con1+con1...)
+ static Node phiCon(Node op, boolean rotate) {
+ Node lhs = op.in(1);
+ Node rhs = op.in(2);
+ // LHS is either a Phi of constants, or another op with Phi of constants
+ PhiNode lphi = pcon(lhs,op);
+ if( rotate && lphi==null && lhs.nIns() > 2 ) {
+ // Only valid to rotate constants if both are same associative ops
+ if( lhs.getClass() != op.getClass() ) return null;
+ lphi = pcon(lhs.in(2),op); // Will rotate with the Phi push
+ }
+ if( lphi==null ) return null;
+
+ // RHS is a constant or a Phi of constants
+ if( !(rhs instanceof ConstantNode) && pcon(rhs,op)==null )
+ return null;
+
+ // If both are Phis, must be same Region
+ if( rhs instanceof PhiNode && lphi.in(0) != rhs.in(0) )
+ return null;
+
+ // Note that this is the exact reverse of Phi pulling a common op down
+ // to reduce total op-count. We don't get in an endless push-up
+ // push-down peephole cycle because the constants all fold first.
+ Node[] ns = new Node[lphi.nIns()];
+ ns[0] = lphi.in(0);
+ // Push constant up through the phi: x + (phi con0+con0 con1+con1...)
+ for( int i=1; i<ns.length; i++ )
+ ns[i] = op.copy(lphi.in(i), rhs instanceof PhiNode ? rhs.in(i) : rhs).peephole();
+ String label = lphi._label + (rhs instanceof PhiNode rphi ? rphi._label : "");
+ Node phi = new PhiNode(label,lphi._declaredType,ns).peephole();
+ // Rotate needs another op, otherwise just the phi
+ return lhs==lphi ? phi : op.copy(lhs.in(1),phi);
+ }
+
+ /**
+ * Tests if the op is a phi and has all constant inputs.
+ * If not, returns null.
+ * If op is a phi, but its inputs are not all constants, then dep is added as
+ * a dependency to the phi's non-const input, because if later the input turn into a constant
+ * dep can make progress.
+ */
+ static PhiNode pcon(Node op, Node dep) {
+ return op instanceof PhiNode phi && phi.allCons(dep) ? phi : null;
+ }
+
+ // Compare two off-spine nodes and decide what order they should be in.
+ // Do we rotate ((x + hi) + lo) into ((x + lo) + hi) ?
+ // Generally constants always go right, then Phi-of-constants, then muls, then others.
+ // Ties with in a category sort by node ID.
+ // TRUE if swapping hi and lo.
+ static boolean spine_cmp( Node hi, Node lo, Node dep ) {
+ if( lo._type.isConstant() ) return false;
+ if( hi._type.isConstant() ) return true ;
+
+ if( lo instanceof PhiNode lphi && lphi.region()._type==Type.XCONTROL ) return false;
+ if( hi instanceof PhiNode hphi && hphi.region()._type==Type.XCONTROL ) return false;
+
+ if( lo instanceof PhiNode && lo.allCons(dep) ) return false;
+ if( hi instanceof PhiNode && hi.allCons(dep) ) return true ;
+
+ if( lo instanceof PhiNode && !(hi instanceof PhiNode) ) return true;
+ if( hi instanceof PhiNode && !(lo instanceof PhiNode) ) return false;
+
+ // Same category of "others"
+ return lo._nid > hi._nid;
+ }
+
+ @Override Node copy(Node lhs, Node rhs) { return new AddNode(lhs,rhs); }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/BoolNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/BoolNode.java
new file mode 100644
index 00000000..9c15f01b
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/BoolNode.java
@@ -0,0 +1,75 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeInteger;
+import com.seaofnodes.simple.type.TypeMemPtr;
+
+import java.util.BitSet;
+
+abstract public class BoolNode extends Node {
+
+ public BoolNode(Node lhs, Node rhs) {
+ super(null, lhs, rhs);
+ }
+
+ abstract String op(); // String opcode name
+
+ @Override
+ 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);
+ return sb.append(")");
+ }
+
+ @Override
+ public Type compute() {
+ if( in(1)._type instanceof TypeInteger i0 &&
+ in(2)._type instanceof TypeInteger i1 ) {
+ if (i0.isConstant() && i1.isConstant())
+ return TypeInteger.constant(doOp(i0.value(), i1.value()) ? 1 : 0);
+ }
+ return in(1)._type.meet(in(2)._type);
+ }
+
+ abstract boolean doOp(long lhs, long rhs);
+
+ @Override
+ public Node idealize() {
+ // Compare of same
+ if( in(1)==in(2) )
+ return new ConstantNode(TypeInteger.constant(doOp(3,3)?1:0));
+
+ // Equals pushes constant to the right; 5==X becomes X==5.
+ if( this instanceof EQ ) {
+ if( !(in(2) instanceof ConstantNode) ) {
+ // con==noncon becomes noncon==con
+ if( in(1) instanceof ConstantNode )
+ return new EQ(in(2),in(1));
+ // Equals sorts by NID otherwise: non.high == non.low becomes non.low == non.high
+ else if( in(1)._nid > in(2)._nid )
+ return new EQ(in(2),in(1));
+ }
+ // Equals X==0 becomes a !X
+ if( (in(2)._type == TypeInteger.ZERO || in(2)._type == TypeMemPtr.NULLPTR) )
+ return new NotNode(in(1));
+ }
+
+ // Do we have ((x * (phi cons)) * con) ?
+ // Do we have ((x * (phi cons)) * (phi cons)) ?
+ // Push constant up through the phi: x * (phi con0*con0 con1*con1...)
+ Node phicon = AddNode.phiCon(this,this instanceof EQ);
+ if( phicon!=null ) return phicon;
+
+ return null;
+ }
+
+ public static class EQ extends BoolNode { public EQ(Node lhs, Node rhs) { super(lhs,rhs); } String op() { return "=="; } boolean doOp(long lhs, long rhs) { return lhs == rhs; } Node copy(Node lhs, Node rhs) { return new EQ(lhs,rhs); } }
+ public static class LT extends BoolNode { public LT(Node lhs, Node rhs) { super(lhs,rhs); } String op() { return "<" ; } boolean doOp(long lhs, long rhs) { return lhs < rhs; } Node copy(Node lhs, Node rhs) { return new LT(lhs,rhs); } }
+ public static class LE extends BoolNode { public LE(Node lhs, Node rhs) { super(lhs,rhs); } String op() { return "<="; } boolean doOp(long lhs, long rhs) { return lhs <= rhs; } Node copy(Node lhs, Node rhs) { return new LE(lhs,rhs); } }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/CFGNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/CFGNode.java
new file mode 100644
index 00000000..b605071b
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/CFGNode.java
@@ -0,0 +1,92 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.type.*;
+import com.seaofnodes.simple.Utils;
+
+import java.util.BitSet;
+import java.util.HashSet;
+import java.util.HashMap;
+
+/** Control Flow Graph Nodes
+ * <p>
+ * CFG nodes have a immediate dominator depth (idepth) and a loop nesting
+ * depth(loop_depth).
+ * <p>
+ * idepth is computed lazily upon first request, and is valid even in the
+ * Parser, and is used by peepholes during parsing and afterward.
+ * <p>
+ * loop_depth is computed after optimization as part of scheduling.
+ *
+ */
+public abstract class CFGNode extends Node {
+
+ public CFGNode(Node... nodes) { super(nodes); }
+
+ @Override public boolean isCFG() { return true; }
+ @Override public boolean isPinned() { return true; }
+
+ public CFGNode cfg(int idx) { return (CFGNode)in(idx); }
+
+ // Block head is Start, Region, CProj, but not e.g. If, Return, Stop
+ public boolean blockHead() { return false; }
+
+ // Should be exactly 1 tail from a block head
+ public CFGNode blockTail() {
+ assert blockHead();
+ for( Node n : _outputs )
+ if( n instanceof CFGNode cfg )
+ return cfg;
+ return null;
+ }
+
+ // ------------------------------------------------------------------------
+ /**
+ * Immediate dominator tree depth, used to approximate a real IDOM during
+ * parsing where we do not have the whole program, and also peepholes
+ * change the CFG incrementally.
+ * <p>
+ * See {@link <a href="https://en.wikipedia.org/wiki/Dominator_(graph_theory)">...</a>}
+ */
+ public int _idepth;
+ public int idepth() { return _idepth==0 ? (_idepth=idom().idepth()+1) : _idepth; }
+
+ // Return the immediate dominator of this Node and compute dom tree depth.
+ public CFGNode idom() { return cfg(0); }
+
+ // Return the LCA of two idoms
+ public CFGNode idom(CFGNode rhs) {
+ if( rhs==null ) return this;
+ CFGNode lhs = this;
+ while( lhs != rhs ) {
+ var comp = lhs.idepth() - rhs.idepth();
+ if( comp >= 0 ) lhs = lhs.idom();
+ if( comp <= 0 ) rhs = rhs.idom();
+ }
+ return lhs;
+ }
+
+ // Loop nesting depth
+ public int _loopDepth;
+ public int loopDepth() { return _loopDepth==0 ? (_loopDepth = cfg(0).loopDepth()) : _loopDepth; }
+
+ // Anti-dependence field support
+ public int _anti; // Per-CFG field to help find anti-deps
+
+ // ------------------------------------------------------------------------
+ // Support routines for Global Code Motion
+
+ // Tik-tok recursion pattern. This method is final, and every caller does
+ // this work.
+ public final void walkUnreach( BitSet visit, HashSet<CFGNode> unreach ) {
+ if( visit.get(_nid) ) return;
+ visit.set(_nid);
+ _walkUnreach(visit,unreach);
+ unreach.remove(this); // Since we reached here... Node was not unreachable
+ }
+
+ // Tik-tok recursion pattern; not-final; callers override this.
+ void _walkUnreach( BitSet visit, HashSet<CFGNode> unreach ) {
+ cfg(0).walkUnreach(visit,unreach);
+ }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/CProjNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/CProjNode.java
new file mode 100644
index 00000000..3d6fd1fa
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/CProjNode.java
@@ -0,0 +1,64 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeMem;
+import com.seaofnodes.simple.type.TypeTuple;
+import java.util.BitSet;
+
+public class CProjNode extends CFGNode {
+
+ // Which slice of the incoming multipart value
+ public final int _idx;
+
+ // Debugging label
+ public final String _label;
+
+ public CProjNode(Node ctrl, int idx, String label) {
+ super(ctrl);
+ _idx = idx;
+ _label = label;
+ }
+
+ @Override public String label() { return _label; }
+
+ @Override StringBuilder _print1(StringBuilder sb, BitSet visited) { return sb.append(_label); }
+
+ @Override public boolean isMultiTail() { return in(0).isMultiHead(); }
+ @Override public boolean blockHead() { return true; }
+
+ public CFGNode ctrl() { return cfg(0); }
+
+ @Override
+ public Type compute() {
+ Type t = ctrl()._type;
+ return t instanceof TypeTuple tt ? tt._types[_idx] : Type.BOTTOM;
+ }
+
+ @Override
+ public Node idealize() {
+ if( ctrl()._type instanceof TypeTuple tt ) {
+ if( tt._types[_idx]==Type.XCONTROL )
+ return Parser.XCTRL; // We are dead
+ if( ctrl() instanceof IfNode && tt._types[1-_idx]==Type.XCONTROL ) // Only true for IfNodes
+ return ctrl().in(0); // We become our input control
+ }
+
+ // Flip a negating if-test, to remove the not
+ if( ctrl() instanceof IfNode iff && iff.pred().addDep(this) instanceof NotNode not )
+ return new CProjNode(new IfNode(iff.ctrl(),not.in(1)).peephole(),1-_idx,_idx==0 ? "False" : "True");
+
+ return null;
+ }
+
+ @Override
+ public Node getBlockStart() {
+ return ctrl() instanceof IfNode ? this : ctrl().getBlockStart();
+ }
+ @Override
+ boolean eq( Node n ) { return _idx == ((CProjNode)n)._idx; }
+
+ @Override
+ int hash() { return _idx; }
+
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/CastNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/CastNode.java
new file mode 100644
index 00000000..fec540f7
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/CastNode.java
@@ -0,0 +1,38 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeInteger;
+import com.seaofnodes.simple.IterPeeps;
+
+import java.util.BitSet;
+
+// Upcast (join) the input to a t. Used after guard test to lift an input.
+// Can also be used to make a type-assertion if ctrl is null.
+public class CastNode extends Node {
+ private final Type _t;
+ public CastNode(Type t, Node ctrl, Node in) {
+ super(ctrl, in);
+ _t = t;
+ setType(compute());
+ }
+
+ @Override public String label() { return "("+_t.str()+")"; }
+
+ @Override
+ public String uniqueName() { return "Cast_" + _nid; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ return in(1)._print0(sb.append(label()), visited);
+ }
+
+ @Override
+ public Type compute() {
+ return in(1)._type.join(_t);
+ }
+
+ @Override
+ public Node idealize() {
+ return in(1)._type.isa(_t) ? in(1) : null;
+ }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/ConstantNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/ConstantNode.java
new file mode 100644
index 00000000..cf1a76b0
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/ConstantNode.java
@@ -0,0 +1,56 @@
+package com.seaofnodes.simple.node;
+
+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
+ * that we allow are integer literals; therefore Constants contain an integer
+ * value. As we add other types of constants, we will refactor how we represent
+ * Constants.
+ * <p>
+ * Constants have no semantic inputs. However, we set Start as an input to
+ * Constants to enable a forward graph walk. This edge carries no semantic
+ * meaning, and it is present <em>solely</em> to allow visitation.
+ * <p>
+ * The Constant's value is the value stored in it.
+ */
+public class ConstantNode extends Node {
+ Type _con;
+ public ConstantNode( Type type ) {
+ super(Parser.START);
+ _con = type;
+ }
+
+ @Override
+ public String label() { return "#"+_con; }
+
+ @Override
+ public String uniqueName() { return "Con_" + _nid; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ return _con._print(sb);
+ }
+
+ @Override public boolean isMultiTail() { return true; }
+ // The special forever-zero is pinned, all others can sink
+ @Override public boolean isPinned() { return this==Parser.ZERO; }
+
+ @Override
+ public Type compute() { return _con; }
+
+ @Override
+ public Node idealize() { return null; }
+
+ @Override
+ boolean eq(Node n) {
+ ConstantNode con = (ConstantNode)n; // Contract
+ return _con==con._con;
+ }
+
+ @Override
+ int hash() { return _con.hashCode(); }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/DivNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/DivNode.java
new file mode 100644
index 00000000..ba06d037
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/DivNode.java
@@ -0,0 +1,38 @@
+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 public String glabel() { return "//"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ in(1)._print0(sb.append("("), visited);
+ in(2)._print0(sb.append("/"), visited);
+ return sb.append(")");
+ }
+
+ @Override
+ public Type compute() {
+ if (in(1)._type instanceof TypeInteger i0 &&
+ in(2)._type instanceof TypeInteger i1) {
+ if (i0.isConstant() && i1.isConstant())
+ return i1.value() == 0
+ ? TypeInteger.ZERO
+ : TypeInteger.constant(i0.value()/i1.value());
+ }
+ return in(1)._type.meet(in(2)._type);
+ }
+
+ @Override
+ public Node idealize() { return null; }
+
+ @Override Node copy(Node lhs, Node rhs) { return new DivNode(lhs,rhs); }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/IfNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/IfNode.java
new file mode 100644
index 00000000..5457ac6e
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/IfNode.java
@@ -0,0 +1,71 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.IterPeeps;
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeInteger;
+import com.seaofnodes.simple.type.TypeTuple;
+import java.util.BitSet;
+import java.util.HashSet;
+
+public class IfNode extends CFGNode implements MultiNode {
+
+ public IfNode(Node ctrl, Node pred) {
+ super(ctrl, pred);
+ IterPeeps.add(this); // Because idoms are complex, just add it
+ }
+
+ @Override
+ public String label() { return "If"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ sb.append("if( ");
+ return in(1)._print0(sb, visited).append(" )");
+ }
+
+ @Override public boolean isMultiHead() { return true; }
+
+ public Node ctrl() { return in(0); }
+ public Node pred() { return in(1); }
+
+ @Override
+ public Type compute() {
+ // If the If node is not reachable then neither is any following Proj
+ if (ctrl()._type != Type.CONTROL && ctrl()._type != Type.BOTTOM )
+ return TypeTuple.IF_NEITHER;
+ Node pred = pred();
+ Type t = pred._type;
+ // High types mean NEITHER side is reachable.
+ // Wait until the type falls to decide which way to go.
+ if( t == Type.TOP || t == TypeInteger.TOP )
+ return TypeTuple.IF_NEITHER;
+ // If constant is 0 then false branch is reachable
+ // Else true branch is reachable
+ if (t instanceof TypeInteger ti && ti.isConstant())
+ return ti==TypeInteger.ZERO ? TypeTuple.IF_FALSE : TypeTuple.IF_TRUE;
+
+ return TypeTuple.IF_BOTH;
+ }
+
+ @Override
+ public Node idealize() {
+ // Hunt up the immediate dominator tree. If we find an identical if
+ // test on either the true or false branch, that side wins.
+ if( !pred()._type.isHighOrConst() )
+ for( CFGNode dom = idom(), prior=this; dom!=null; prior = dom, dom = dom.idom() )
+ if( dom.addDep(this) instanceof IfNode iff && iff.pred().addDep(this)==pred() && prior instanceof CProjNode prj ) {
+ setDef(1,new ConstantNode(TypeInteger.make(true,prj._idx==0?1:0)).peephole());
+ return this;
+ }
+ return null;
+ }
+
+ @Override void _walkUnreach( BitSet visit, HashSet<CFGNode> unreach ) {
+ for( Node proj : _outputs )
+ if( ((CProjNode)proj)._loopDepth == 0 )
+ unreach.add((CProjNode)proj);
+ super._walkUnreach(visit,unreach);
+ }
+
+ @Override public Node getBlockStart() { return ctrl().getBlockStart(); }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/LoadNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/LoadNode.java
new file mode 100644
index 00000000..4ed06da4
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/LoadNode.java
@@ -0,0 +1,78 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Utils;
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.Field;
+
+import java.util.BitSet;
+
+/**
+ * Load represents extracting a value from inside a memory object,
+ * in chapter 10 this means Struct fields.
+ */
+public class LoadNode extends MemOpNode {
+
+ Type _declaredType;
+ /**
+ * Load a value from a ptr.field.
+ *
+ * @param name The field we are loading
+ * @param memSlice The memory alias node - this is updated after a Store
+ * @param memPtr The ptr to the struct from where we load a field
+ */
+ public LoadNode(String name, int alias, Type glb, Node memSlice, Node memPtr) {
+ super(name, alias, memSlice, memPtr);
+ _declaredType = glb;
+ }
+
+ @Override
+ public String label() { return "Load"; }
+
+ @Override
+ public String glabel() { return "."+_name; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) { return sb.append(".").append(_name); }
+
+ @Override
+ public Type compute() {
+ return _declaredType;
+ }
+
+ @Override
+ public Node idealize() {
+
+ // Simple Load-after-Store on same address.
+ if( mem() instanceof StoreNode st &&
+ ptr() == st.ptr() ) { // Must check same object
+ assert _name.equals(st._name); // Equiv class aliasing is perfect
+ return st.val();
+ }
+
+ // Push a Load up through a Phi, as long as it collapses on at least
+ // one arm. If at a Loop, the backedge MUST collapse - else we risk
+ // spinning the same transform around the loop indefinitely.
+ // BEFORE (2 Sts, 1 Ld): AFTER (1 St, 0 Ld):
+ // if( pred ) ptr.x = e0; val = pred ? e0
+ // else ptr.x = e1; : e1;
+ // val = ptr.x; ptr.x = val;
+ if( mem() instanceof PhiNode memphi && memphi.region()._type == Type.CONTROL && memphi.nIns()== 3 ) {
+ // Profit on RHS/Loop backedge
+ if( profit(memphi,2) ||
+ // Else must not be a loop to count profit on LHS.
+ (!(memphi.region() instanceof LoopNode) && profit(memphi,1)) ) {
+ Node ld1 = new LoadNode(_name,_alias,_declaredType,memphi.in(1),ptr()).peephole();
+ Node ld2 = new LoadNode(_name,_alias,_declaredType,memphi.in(2),ptr()).peephole();
+ return new PhiNode(_name,_type,memphi.region(),ld1,ld2);
+ }
+ }
+
+ return null;
+ }
+
+ // Profitable if we find a matching Store on this Phi arm.
+ private boolean profit(PhiNode phi, int idx) {
+ Node px = phi.in(idx);
+ return px!=null && px.addDep(this) instanceof StoreNode st1 && ptr()==st1.ptr();
+ }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/LoopNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/LoopNode.java
new file mode 100644
index 00000000..c662dee6
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/LoopNode.java
@@ -0,0 +1,77 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.Utils;
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeInteger;
+import java.util.BitSet;
+import java.util.HashSet;
+
+public class LoopNode extends RegionNode {
+ public LoopNode( Node entry ) { super(null,entry,null); }
+
+ public CFGNode entry() { return cfg(1); }
+ public CFGNode back () { return cfg(2); }
+
+ @Override
+ public String label() { return "Loop"; }
+
+ @Override
+ public Type compute() {
+ if( inProgress() ) return Type.CONTROL;
+ return entry()._type;
+ }
+
+ @Override
+ public Node idealize() {
+ return inProgress() ? null : super.idealize();
+ }
+
+ // Bypass Region idom, same as the default idom() using use in(1) instead of in(0)
+ @Override public int idepth() { return _idepth==0 ? (_idepth=idom().idepth()+1) : _idepth; }
+ // Bypass Region idom, same as the default idom() using use in(1) instead of in(0)
+ @Override public CFGNode idom() { return entry(); }
+
+ @Override public int loopDepth() {
+ if( _loopDepth!=0 ) return _loopDepth; // Was already set
+ _loopDepth = entry()._loopDepth+1; // Entry depth plus one
+ // One-time tag loop exits
+ for( CFGNode idom = back(); idom!=this; idom = idom.idom() ) {
+ // Walk idom in loop, setting depth
+ idom._loopDepth = _loopDepth;
+ // Loop exit hits the CProj before the If, instead of jumping from
+ // Region directly to If.
+ if( idom instanceof CProjNode proj ) {
+ assert proj.in(0) instanceof IfNode; // Loop exit test
+ // Find the loop exit CProj, and set loop_depth
+ for( Node use : proj.in(0)._outputs )
+ if( use instanceof CProjNode proj2 && proj2 != idom )
+ proj2._loopDepth = _loopDepth-1;
+ }
+ }
+ return _loopDepth;
+ }
+
+ // If this is an unreachable loop, it may not have an exit. If it does not
+ // (i.e., infinite loop), force an exit to make it reachable.
+ public void forceExit( StopNode stop ) {
+ // Walk the backedge, then immediate dominator tree util we hit this
+ // Loop again. If we ever hit a CProj from an If (as opposed to
+ // directly on the If) we found our exit.
+ CFGNode x = back();
+ while( x != this ) {
+ if( x instanceof CProjNode exit )
+ return; // Found an exit, not an infinite loop
+ x = x.idom();
+ }
+ // Found a no-exit loop. Insert an exit
+ NeverNode iff = new NeverNode(back());
+ for( Node use : _outputs )
+ if( use instanceof PhiNode phi )
+ iff.addDef(use);
+ CProjNode t = new CProjNode(iff,0,"True" );
+ CProjNode f = new CProjNode(iff,1,"False");
+ setDef(2,f);
+ stop.addDef(new ReturnNode(t,Parser.ZERO,null));
+ }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/MemOpNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/MemOpNode.java
new file mode 100644
index 00000000..7f8d060a
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/MemOpNode.java
@@ -0,0 +1,44 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Field;
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeMemPtr;
+
+/**
+ * Convenience common base for Load and Store.
+ */
+public abstract class MemOpNode extends Node {
+
+ public final String _name;
+ public final int _alias;
+
+ public MemOpNode(String name, int alias, Node memSlice, Node memPtr) {
+ super(null, memSlice, memPtr);
+ _name = name;
+ _alias = alias;
+ }
+ public MemOpNode(String name, int alias, Node memSlice, Node memPtr, Node value) {
+ this(name, alias, memSlice, memPtr);
+ addDef(value);
+ }
+
+ public Node mem() { return in(1); }
+ public Node ptr() { return in(2); }
+
+ @Override
+ boolean eq(Node n) {
+ MemOpNode mem = (MemOpNode)n; // Invariant
+ return _alias==mem._alias; // When comparing types error to use "equals"; always use "=="
+ }
+
+ @Override
+ int hash() { return _alias; }
+
+ @Override
+ String err() {
+ Type ptr = ptr()._type;
+ return (ptr==Type.BOTTOM || (ptr instanceof TypeMemPtr tmp && tmp._nil) )
+ ? "Might be null accessing '" + _name + "'"
+ : null;
+ }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/MinusNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/MinusNode.java
new file mode 100644
index 00000000..9b687613
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/MinusNode.java
@@ -0,0 +1,36 @@
+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 public String glabel() { return "-"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ in(1)._print0(sb.append("(-"), visited);
+ return sb.append(")");
+ }
+
+ @Override
+ public Type compute() {
+ if (in(1)._type instanceof TypeInteger i0)
+ return i0.isConstant() ? TypeInteger.constant(-i0.value()) : i0;
+ return TypeInteger.TOP.meet(in(1)._type);
+ }
+
+ @Override
+ public Node idealize() {
+ // -(-x) is x
+ if( in(1) instanceof MinusNode minus )
+ return minus.in(1);
+
+ return null;
+ }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/MulNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/MulNode.java
new file mode 100644
index 00000000..7e9f88a2
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/MulNode.java
@@ -0,0 +1,57 @@
+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 public String glabel() { return "*"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ in(1)._print0(sb.append("("), visited);
+ in(2)._print0(sb.append("*"), visited);
+ return sb.append(")");
+ }
+
+ @Override
+ public Type compute() {
+ if (in(1)._type instanceof TypeInteger i0 &&
+ in(2)._type instanceof TypeInteger i1) {
+ if (i0.isConstant() && i1.isConstant())
+ return TypeInteger.constant(i0.value()*i1.value());
+ }
+ return in(1)._type.meet(in(2)._type);
+ }
+
+ @Override
+ public Node idealize() {
+ Node lhs = in(1);
+ Node rhs = in(2);
+ Type t1 = lhs._type;
+ Type t2 = rhs._type;
+
+ // Mul of 1. We do not check for (1*x) because this will already
+ // canonicalize to (x*1)
+ if ( t2.isConstant() && t2 instanceof TypeInteger i && i.value()==1 )
+ return lhs;
+
+ // Move constants to RHS: con*arg becomes arg*con
+ if ( t1.isConstant() && !t2.isConstant() )
+ return swap12();
+
+ // Do we have ((x * (phi cons)) * con) ?
+ // Do we have ((x * (phi cons)) * (phi cons)) ?
+ // Push constant up through the phi: x * (phi con0*con0 con1*con1...)
+ Node phicon = AddNode.phiCon(this,true);
+ if( phicon!=null ) return phicon;
+
+ return null;
+ }
+ @Override Node copy(Node lhs, Node rhs) { return new MulNode(lhs,rhs); }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/MultiNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/MultiNode.java
new file mode 100644
index 00000000..379e140d
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/MultiNode.java
@@ -0,0 +1,12 @@
+package com.seaofnodes.simple.node;
+
+public interface MultiNode extends OutNode {
+
+ // Find a projection by index
+ default ProjNode proj( int idx ) {
+ for( Node out : outs() )
+ if( out instanceof ProjNode prj && prj._idx==idx )
+ return prj;
+ return null;
+ }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/NeverNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/NeverNode.java
new file mode 100644
index 00000000..f50d3503
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/NeverNode.java
@@ -0,0 +1,19 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.type.TypeInteger;
+import com.seaofnodes.simple.type.Type;
+import java.util.BitSet;
+
+// "Never true" for infinite loop exits
+public class NeverNode extends IfNode {
+ public NeverNode(Node ctrl) { super(ctrl,Parser.ZERO); }
+
+ @Override public String label() { return "Never"; }
+
+ @Override StringBuilder _print1(StringBuilder sb, BitSet visited) { return sb.append("Never"); }
+
+ @Override public Type compute() { return TypeInteger.BOT; }
+
+ @Override public Node idealize() { return null; }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/NewNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/NewNode.java
new file mode 100644
index 00000000..0724ec56
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/NewNode.java
@@ -0,0 +1,42 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeMemPtr;
+
+import java.util.BitSet;
+
+public class NewNode extends Node {
+
+ TypeMemPtr _ptr;
+
+ public NewNode(TypeMemPtr ptr, Node ctrl) {
+ super(ctrl);
+ this._ptr = ptr;
+ }
+
+ @Override
+ 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());
+ }
+
+ @Override public boolean isPinned() { return true; }
+
+ @Override
+ public Type compute() { return _ptr; }
+
+ @Override
+ public Node idealize() { return null; }
+
+ @Override
+ boolean eq(Node n) { return this == n; }
+
+ @Override
+ int hash() { return _ptr.hashCode(); }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/Node.java b/chapter12/src/main/java/com/seaofnodes/simple/node/Node.java
new file mode 100644
index 00000000..fc49ad6a
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/Node.java
@@ -0,0 +1,624 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.IRPrinter;
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.IterPeeps;
+import com.seaofnodes.simple.Utils;
+import com.seaofnodes.simple.type.Type;
+
+import java.util.*;
+import java.util.function.Function;
+
+/**
+ * All Nodes in the Sea of Nodes IR inherit from the Node class.
+ * The Node class provides common functionality used by all subtypes.
+ * Subtypes of Node specialize by overriding methods.
+ */
+public abstract class Node implements OutNode {
+
+ /**
+ * 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;
+
+ /**
+ * Inputs to the node. These are use-def references to Nodes.
+ * <p>
+ * Generally fixed length, ordered, nulls allowed, no unused trailing space.
+ * Ordering is required because e.g. "a/b" is different from "b/a".
+ * The first input (offset 0) is often a {@link #isCFG} node.
+ */
+ public final ArrayList<Node> _inputs;
+
+ /**
+ * Outputs reference Nodes that are not null and have this Node as an
+ * input. These nodes are users of this node, thus these are def-use
+ * references to Nodes.
+ * <p>
+ * Outputs directly match inputs, making a directed graph that can be
+ * walked in either direction. These outputs are typically used for
+ * efficient optimizations but otherwise have no semantics meaning.
+ */
+ 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;
+
+
+ /**
+ * A private Global Static mutable counter, for unique node id generation.
+ * To make the compiler multithreaded, this field will have to move into a TLS.
+ * Starting with value 1, to avoid bugs confusing node ID 0 with uninitialized values.
+ * */
+ private static int UNIQUE_ID = 1;
+ public static int UID() { return UNIQUE_ID; }
+
+ protected Node(Node... inputs) {
+ _nid = UNIQUE_ID++; // allocate unique dense ID
+ _inputs = new ArrayList<>();
+ Collections.addAll(_inputs,inputs);
+ _outputs = new ArrayList<>();
+ for( Node n : _inputs )
+ if( n != null )
+ n.addUse( this );
+ }
+
+ // 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"
+ public String uniqueName() {
+ // Get rid of $ as graphviz doesn't like it
+ String label = label().replaceAll("\\$", "");
+ return label + _nid;
+ }
+
+ // Graphical label, e.g. "+" or "Region" or "=="
+ public String glabel() { return label(); }
+
+
+ // ------------------------------------------------------------------------
+
+ // 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) && !(this instanceof ConstantNode) )
+ 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);
+
+ public String p(int depth) { return IRPrinter.prettyPrint(this,depth); }
+
+ public boolean isMultiHead() { return false; }
+ public boolean isMultiTail() { return false; }
+
+ // ------------------------------------------------------------------------
+ // Graph Node & Edge manipulation
+
+ /**
+ * Gets the ith input node
+ * @param i Offset of the input node
+ * @return Input node or null
+ */
+ public Node in(int i) { return _inputs.get(i); }
+ public Node out(int i) { return _outputs.get(i); }
+ @Override public ArrayList<Node> outs() { return _outputs; }
+
+ public int nIns() { return _inputs.size(); }
+
+ public int nOuts() { return _outputs.size(); }
+
+ public boolean isUnused() { return nOuts() == 0; }
+
+ public CFGNode cfg0() { return (CFGNode)in(0); }
+
+ /**
+ * 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
+ * <em>def</em> go dead. This function is co-recursive with {@link #kill}.
+ * <p>
+
+ * This method is the normal path for altering a Node, because it does the
+ * proper default edge maintenance. It also <em>immediately</em> kills
+ * Nodes that lose their last use; at times care must be taken to avoid
+ * killing Nodes that are being used without having an output Node. This
+ * definitely happens in the middle of recursive {@link #peephole} calls.
+ *
+ * @param idx which def to set
+ * @param new_def the new definition
+ * @return new_def for flow coding
+ */
+ public Node setDef(int idx, Node new_def ) {
+ unlock();
+ Node old_def = in(idx);
+ if( old_def == new_def ) return new_def; // No change
+ // If new def is not null, add the corresponding def->use edge
+ // This needs to happen before removing the old node's def->use edge as
+ // the new_def might get killed if the old node kills it recursively.
+ if( new_def != null )
+ new_def.addUse(this);
+ // Set the new_def over the old (killed) edge
+ _inputs.set(idx,new_def);
+ if( old_def != null && // If the old def exists, remove a def->use edge
+ old_def.delUse(this) ) // If we removed the last use, the old def is now dead
+ old_def.kill(); // Kill old def
+ moveDepsToWorklist();
+ // Return self for easy flow-coding
+ return new_def;
+ }
+
+ // Remove the numbered input, compressing the inputs in-place. This
+ // shuffles the order deterministically - which is suitable for Region and
+ // Phi, but not for every Node. If the def goes dead, it is recursively
+ // killed, which may include 'this' Node.
+ Node delDef(int idx) {
+ unlock();
+ Node old_def = in(idx);
+ if( old_def != null && // If the old def exists, remove a def->use edge
+ old_def.delUse(this) ) // If we removed the last use, the old def is now dead
+ old_def.kill(); // Kill old def
+ old_def.moveDepsToWorklist();
+ Utils.del(_inputs, idx);
+ return this;
+ }
+
+ /**
+ * Add a new def to an existing Node. Keep the edges correct by
+ * adding the corresponding <em>def->use</em> edge.
+ *
+ * @param new_def the new definition, appended to the end of existing definitions
+ * @return new_def for flow coding
+ */
+ public Node addDef(Node new_def) {
+ unlock();
+ // Add use->def edge
+ _inputs.add(new_def);
+ // If new def is not null, add the corresponding def->use edge
+ if( new_def != null )
+ new_def.addUse(this);
+ return new_def;
+ }
+
+ // Breaks the edge invariants, used temporarily
+ protected <N extends Node> N addUse(Node n) { _outputs.add(n); return (N)this; }
+
+ // Remove node 'use' from 'def's (i.e. our) output list, by compressing the list in-place.
+ // Return true if the output list is empty afterward.
+ // Error is 'use' does not exist; ok for 'use' to be null.
+ protected boolean delUse( Node use ) {
+ Utils.del(_outputs, Utils.find(_outputs, use));
+ return _outputs.isEmpty();
+ }
+
+ // Shortcut for "popping" n nodes. A "pop" is basically a
+ // setDef(last,null) followed by lowering the nIns() count.
+ void popN(int n) {
+ unlock();
+ for( int i=0; i<n; i++ ) {
+ Node old_def = _inputs.removeLast();
+ if( old_def != null && // If it exists and
+ old_def.delUse(this) ) // If we removed the last use, the old def is now dead
+ old_def.kill(); // Kill old def
+ }
+ }
+
+ /**
+ * Kill a Node with no <em>uses</em>, by setting all of its <em>defs</em>
+ * to null. This may recursively kill more Nodes, and is basically dead
+ * code elimination.
+ */
+ public void kill( ) {
+ unlock();
+ assert isUnused(); // Has no uses, so it is dead
+ _type=null; // Flag as dead
+ while( nIns()>0 ) { // Set all inputs to null, recursively killing unused Nodes
+ Node old_def = _inputs.removeLast();
+ if( old_def != null ) {
+ IterPeeps.add(old_def);// Revisit neighbor because removed use
+ if( old_def.delUse(this) ) // If we removed the last use, the old def is now dead
+ old_def.kill(); // Kill old def
+ }
+ }
+ assert isDead(); // Really dead now
+ }
+
+ // Mostly used for asserts and printing.
+ public boolean isDead() { return isUnused() && nIns()==0 && _type==null; }
+
+ // Shortcuts to stop DCE mid-parse
+ // Add bogus null use to keep node alive
+ public <N extends Node> N keep() { return addUse(null); }
+ // Remove bogus null.
+ public <N extends Node> N unkeep() {
+ delUse(null);
+ return (N)this;
+ }
+ // Test "keep" status
+ public boolean iskeep() { return Utils.find(_outputs,null) != -1; }
+
+
+ // Replace self with nnn in the graph, making 'this' go dead
+ public void subsume( Node nnn ) {
+ assert nnn!=this;
+ while( nOuts() > 0 ) {
+ Node n = _outputs.removeLast();
+ n.unlock();
+ int idx = Utils.find(n._inputs, this);
+ n._inputs.set(idx,nnn);
+ nnn.addUse(n);
+ }
+ kill();
+ }
+
+ // ------------------------------------------------------------------------
+ // Graph-based optimizations
+
+ /**
+ * We allow disabling peephole opt so that we can observe the
+ * full graph, vs the optimized graph.
+ */
+ public static boolean _disablePeephole = false;
+
+ /**
+ * 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
+ }
+ Node n = peepholeOpt();
+ return n==null ? this : deadCodeElim(n.peephole()); // Cannot return null for no-progress
+ }
+
+ /**
+ * Try to peephole at this node and return a better replacement Node if
+ * possible. We compute a {@link Type} and then check and replace:
+ * <ul>
+ * <li>if the Type {@link Type#isConstant}, we replace with a {@link ConstantNode}</li>
+ * <li>in a future chapter we will look for a
+ * <a href="https://en.wikipedia.org/wiki/Common_subexpression_elimination">Common Subexpression</a>
+ * to eliminate.</li>
+ * <li>we ask the Node for a better replacement. The "better replacement"
+ * is things like {@code (1+2)} becomes {@code 3} and {@code (1+(x+2))} becomes
+ * {@code (x+(1+2))}. By canonicalizing expressions we fold common addressing
+ * math constants, remove algebraic identities and generally simplify the
+ * code. </li>
+ *
+ * Unlike peephole above, this explicitly returns null for no-change, or not-null
+ * for a better replacement (which can be this).
+ * </ul>
+ */
+ public final Node peepholeOpt( ) {
+ ITER_CNT++;
+ // Compute initial or improved Type
+ Type old = setType(compute());
+
+ // Replace constant computations from non-constants with a constant node
+ if( !(this instanceof ConstantNode) && !(this instanceof XCtrlNode) && _type.isHighOrConst() )
+ return (_type==Type.XCONTROL ? new XCtrlNode() : new ConstantNode(_type)).peepholeOpt();
+
+ // Global Value Numbering
+ if( _hash==0 ) {
+ Node n = GVN.get(this); // Will set _hash as a side effect
+ if( n==null )
+ GVN.put(this,this); // Put in table now
+ else {
+ // Because of random worklist ordering, the two equal nodes
+ // might have different types. Because of monotonicity, both
+ // types are valid. To preserve monotonicity, the resulting
+ // shared Node has to have the best of both types.
+ n.setType(n._type.join(_type));
+ _hash = 0; // Clear, since it never went in the table
+ return deadCodeElim(n);// Return previous; does Common Subexpression Elimination
+ }
+ }
+
+ // Ask each node for a better replacement
+ Node n = idealize();
+ if( n != null ) // Something changed
+ return n; // Report progress
+
+ if( old==_type ) ITER_NOP_CNT++;
+ return old==_type ? null : this; // Report progress
+ }
+ public static int ITER_CNT, ITER_NOP_CNT;
+
+ // m is the new Node, self is the old.
+ // Return 'm', which may have zero uses but is alive nonetheless.
+ // If self has zero uses (and is not 'm'), {@link #kill} self.
+ private Node deadCodeElim(Node m) {
+ // If self is going dead and not being returned here (Nodes returned
+ // from peephole commonly have no uses (yet)), then kill self.
+ if( m != this && isUnused() && !isDead() ) {
+ // Killing self - and since self recursively kills self's inputs we
+ // might end up killing 'm', which we are returning as a live Node.
+ // So we add a bogus extra null output edge to stop kill().
+ m.keep(); // Keep m alive
+ kill(); // Kill self because replacing with 'm'
+ m.unkeep(); // Okay to peephole m
+ }
+ return m;
+ }
+
+ /**
+ * This function needs to be
+ * <a href="https://en.wikipedia.org/wiki/Monotonic_function">Monotonic</a>
+ * as it is part of a Monotone Analysis Framework.
+ * <a href="https://www.cse.psu.edu/~gxt29/teaching/cse597s21/slides/08monotoneFramework.pdf">See for example this set of slides</a>.
+ * <p>
+ * For Chapter 2, all our Types are really integer constants, and so all
+ * the needed properties are trivially true, and we can ignore the high
+ * theory. Much later on, this will become important and allow us to do
+ * many fancy complex optimizations trivially... because theory.
+ * <p>
+ * {@link #compute()} needs to be stand-alone, and cannot recursively call compute
+ * on its inputs, because programs are cyclic (have loops!) and this will just
+ * infinitely recurse until stack overflow. Instead, compute typically
+ * computes a new type from the {@link #_type} field of its inputs.
+ */
+ public abstract Type compute();
+
+ // Set the type. Assert monotonic progress.
+ // If changing, add users to worklist.
+ public Type setType(Type type) {
+ Type old = _type;
+ assert old==null || type.isa(old); // Since _type not set, can just re-run this in assert in the debugger
+ if( old == type ) return old;
+ _type = type; // Set _type late for easier assert debugging
+ IterPeeps.addAll(_outputs);
+ moveDepsToWorklist();
+ return old;
+ }
+
+
+ /**
+ * This function rewrites the current Node into a more "idealized" form.
+ * This is the bulk of our peephole rewrite rules, and we use this to
+ * e.g. turn arbitrary collections of adds and multiplies with mixed
+ * constants into a normal form that's easy for hardware to implement.
+ * Example: An array addressing expression:
+ * <pre> ary[idx+1]</pre>
+ * might turn into Sea-of-Nodes IR:
+ * <pre> (ary+12)+((idx+1) * 4)</pre>
+ * This expression can then be idealized into:
+ * <pre> ary + ((idx*4) + (12 + (1*4)))</pre>
+ * And more folding:
+ * <pre> ary + ((idx<<2) + 16)</pre>
+ * And during code-gen:
+ * <pre> MOV4 Rary,Ridx,16 // or some such hardware-specific notation </pre>
+ * <p>
+ * {@link #idealize} has a very specific calling convention:
+ * <ul>
+ * <li>If NO change is made, return {@code null}
+ * <li>If ANY change is made, return not-null; this can be {@code this}
+ * <li>The returned Node does NOT call {@link #peephole} on itself; the {@link #peephole} call will recursively peephole it.
+ * <li>Any NEW nodes that are not directly returned DO call {@link #peephole}.
+ * </ul>
+ * <p>
+ * Examples:
+ * <table border="3">
+ * <tr><th> before </th><th> after </th><th>return </th><th>comment </th></tr>
+ * <tr><td>{@code (x+5) }</td><td>{@code (x+5) }</td><td>{@code null }</td><td>No change</td></tr>
+ * <tr><td>{@code (5+x) }</td><td>{@code (x+5) }</td><td>{@code this }</td><td>Swapped arguments</td></tr>
+ * <tr><td>{@code ((x+1)+2)}</td><td>{@code (x+(1+2))}</td><td>{@code (x+_) }</td><td>Returns 2 new Nodes</td></tr>
+ * </table>
+ *
+ * The last entry deserves more discussion. The new Node {@code (1+2)}
+ * created in {@link #idealize} calls {@link #peephole} (which then folds
+ * into a constant). The other new Node {@code (x+3)} does not call
+ * peephole, because it is returned and peephole itself will recursively
+ * call peephole.
+ * <p>
+ * Since idealize calls peephole and peephole calls idealize, you must be
+ * careful that all idealizations are <em>monotonic</em>: all transforms
+ * remove some feature, so that the set of available transforms always
+ * shrinks. If you don't, you risk an infinite peephole loop!
+ *
+ * @return Either a new or changed node, or null for no changes.
+ */
+ public abstract Node idealize();
+
+
+ // Some of the peephole rules get complex, and search further afield than
+ // just the nearest neighbor. These peepholes can fail the pattern match
+ // on a node some distance away, and if that node ever changes we should
+ // retry the peephole. Track a set of Nodes dependent on `this`, and
+ // revisit them if `this` changes.
+ ArrayList<Node> _deps;
+
+ /**
+ * Add a node to the list of dependencies. Only add it if its not an input
+ * or output of this node, that is, it is at least one step away. The node
+ * being added must benefit from this node being peepholed.
+ */
+ Node addDep( Node dep ) {
+ // Running peepholes during the big assert cannot have side effects
+ // like adding dependencies.
+ if( IterPeeps.midAssert() ) return this;
+ 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
+ if( Utils.find(_outputs,dep)!= -1 ) return this;
+ _deps.add(dep);
+ return this;
+ }
+
+ // Move the dependents onto a worklist, and clear for future dependents.
+ public void moveDepsToWorklist( ) {
+ if( _deps==null ) return;
+ IterPeeps.addAll(_deps);
+ _deps.clear();
+ }
+
+
+ // Global Value Numbering. Hash over opcode and inputs; hits in this table
+ // are structurally equal.
+ public static final HashMap<Node,Node> GVN = new HashMap<>();
+
+ // Two nodes are equal if they have the same inputs and the same "opcode"
+ // which means the same Java class, plus same internal parts.
+ @Override public final boolean equals( Object o ) {
+ if( o==this ) return true;
+ if( o.getClass() != getClass() ) return false;
+ Node n = (Node)o;
+ int len = _inputs.size();
+ if( len != n._inputs.size() ) return false;
+ for( int i=0; i<len; i++ )
+ if( in(i) != n.in(i) )
+ return false;
+ return eq(n);
+ }
+ // Subclasses add extra checks (such as ConstantNodes have same constant),
+ // and can assume "this!=n" and has the same Java class.
+ boolean eq( Node n ) { return true; }
+
+
+ // Cached hash. If zero, then not computed AND this Node is NOT in the GVN
+ // table - and can have its edges hacked (which will change his hash
+ // anyway). If Non-Zero then this Node is IN the GVN table, or is being
+ // probed to see if it can be inserted. His edges are "locked", because
+ // hacking his edges will change his hash.
+ int _hash;
+
+ // If the _hash is set, then the Node is in the GVN table; remove it.
+ void unlock() {
+ if( _hash==0 ) return;
+ Node old = GVN.remove(this); // Pull from table
+ assert old==this;
+ _hash=0; // Out of table now
+ }
+
+
+ // Hash of opcode and inputs
+ @Override public final int hashCode() {
+ if( _hash != 0 ) return _hash;
+ int hash = hash();
+ for( Node n : _inputs )
+ if( n != null )
+ hash = hash ^ (hash<<17) ^ (hash>>13) ^ n._nid;
+ if( hash==0 ) hash = 0xDEADBEEF; // Bad hash, so use some junky thing
+ return (_hash=hash);
+ }
+ // Subclasses add extra hash info (such as ConstantNodes constant)
+ int hash() { return 0; }
+
+ // ------------------------------------------------------------------------
+ //
+
+ /** Is this Node control-flow-graph related */
+ public boolean isCFG() { return false; }
+
+ /** Is this Node Memory related */
+ public boolean isMem() { return false; }
+
+ /** Return block start from a isCFG() */
+ public Node getBlockStart() { return null; }
+
+ /** Pinned in the schedule */
+ public boolean isPinned() { return false; }
+
+ // ------------------------------------------------------------------------
+ // Peephole utilities
+
+ // Swap inputs without letting either input go dead during the swap.
+ Node swap12() {
+ unlock(); // Hash is order dependent
+ Node tmp = in(1);
+ _inputs.set(1,in(2));
+ _inputs.set(2,tmp);
+ return this;
+ }
+
+ /**
+ * Does this node contain all constants? Ignores in(0), as is usually
+ * control. In an input is not a constant, we add dep as a dependency to
+ * it because dep can make progress if the input becomes a constant later.
+ * It is sufficient for one of the non-const inputs to have the dependency,
+ * so we don't bother checking the rest.
+ */
+ boolean allCons(Node dep) {
+ for( int i=1; i<nIns(); i++ )
+ if( !(in(i)._type.isConstant()) ) {
+ in(i).addDep(dep); // If in(i) becomes a constant later, will trigger some peephole
+ return false;
+ }
+ return true;
+ }
+
+
+ // 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.
+ Node copy(Node lhs, Node rhs) { throw Utils.TODO("Binary ops need to implement copy"); }
+
+ // Report any post-optimize errors
+ String err() { return null; }
+
+ /**
+ * Used to allow repeating tests in the same JVM. This just resets the
+ * Node unique id generator, and is done as part of making a new Parser.
+ */
+ public static void reset() {
+ UNIQUE_ID = 1;
+ _disablePeephole=false;
+ GVN.clear();
+ ITER_CNT = ITER_NOP_CNT = 0;
+ }
+
+
+ // Utility to walk the entire graph applying a function; return the first
+ // not-null result.
+ private static final BitSet WVISIT = new BitSet();
+ final public <E> E walk( Function<Node,E> pred ) {
+ assert WVISIT.isEmpty();
+ E rez = _walk(pred);
+ WVISIT.clear();
+ return rez;
+ }
+
+ private <E> E _walk( Function<Node,E> pred ) {
+ if( WVISIT.get(_nid) ) return null; // Been there, done that
+ WVISIT.set(_nid);
+ E x = pred.apply(this);
+ if( x != null ) return x;
+ for( Node def : _inputs ) if( def != null && (x = def._walk(pred)) != null ) return x;
+ for( Node use : _outputs ) if( use != null && (x = use._walk(pred)) != null ) return x;
+ return null;
+ }
+
+ /**
+ * Debugging utility to find a Node by index
+ */
+ public Node find(int nid) {
+ return walk( n -> n._nid==nid ? n : null );
+ }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/NotNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/NotNode.java
new file mode 100644
index 00000000..05e44eb0
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/NotNode.java
@@ -0,0 +1,44 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Utils;
+import com.seaofnodes.simple.type.*;
+import java.util.BitSet;
+
+public class NotNode extends Node {
+ public NotNode(Node in) { super(null, in); }
+
+ @Override public String label() { return "Not"; }
+
+ @Override public String glabel() { return "!"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ in(1)._print0(sb.append("(!"), visited);
+ return sb.append(")");
+ }
+
+ @Override
+ public Type compute() {
+ Type t0 = in(1)._type;
+ switch( t0 ) {
+ case TypeInteger i0:
+ return i0.isConstant() ? TypeInteger.constant(i0.value()==0 ? 1 : 0) : i0;
+ case TypeMemPtr p0:
+ // top->top, bot->bot, null->1, *void->0, not-null ptr->0, ptr/nil->bot
+ // If input in null then true
+ // If input is not null ptr then false
+ if( p0 == TypeMemPtr.TOP ) return TypeInteger.TOP;
+ if( p0 == TypeMemPtr.NULLPTR ) return TypeInteger.constant(1);
+ if( !p0._nil ) return TypeInteger.constant(0);
+ return TypeInteger.BOT;
+ case Type t:
+ if( t0.getClass() != Type.class )
+ // Only doing NOT on ints and ptrs
+ throw Utils.TODO();
+ return t0==Type.TOP ? Type.TOP : Type.BOTTOM;
+ }
+ }
+
+ @Override
+ public Node idealize() { return null; }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/OutNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/OutNode.java
new file mode 100644
index 00000000..7e531145
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/OutNode.java
@@ -0,0 +1,7 @@
+package com.seaofnodes.simple.node;
+
+import java.util.ArrayList;
+
+public interface OutNode {
+ ArrayList<Node> outs();
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/PhiNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/PhiNode.java
new file mode 100644
index 00000000..a07dc140
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/PhiNode.java
@@ -0,0 +1,159 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeMem;
+import com.seaofnodes.simple.Utils;
+
+import java.util.BitSet;
+
+public class PhiNode extends Node {
+
+ final String _label;
+
+ // The Phi type we compute must stay within the domain of the Phi.
+ // Example Int stays Int, Ptr stays Ptr, Control stays Control, Mem stays Mem.
+ final Type _declaredType;
+
+ public PhiNode(String label, Type declaredType, Node... inputs) { super(inputs); _label = label; assert declaredType!=null; _declaredType = declaredType; }
+
+ @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() )
+ 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;
+ }
+
+ public CFGNode region() { return (CFGNode)in(0); }
+ @Override public boolean isMultiTail() { return true; }
+ @Override public boolean isMem() { return _declaredType instanceof TypeMem; }
+ @Override public boolean isPinned() { return true; }
+
+ @Override
+ public Type compute() {
+ if( !(region() instanceof RegionNode r) )
+ return region()._type==Type.XCONTROL ? Type.TOP : _type;
+ // During parsing Phis have to be computed type pessimistically.
+ if( r.inProgress() ) return _declaredType;
+ // Set type to local top of the starting type
+ Type t = _declaredType.glb().dual();
+ for (int i = 1; i < nIns(); i++)
+ // If the region's control input is live, add this as a dependency
+ // to the control because we can be peeped should it become dead.
+ if( r.in(i).addDep(this)._type != Type.XCONTROL )
+ t = t.meet(in(i)._type);
+ return t;
+ }
+
+ @Override
+ public Node idealize() {
+ if( !(region() instanceof RegionNode r ) )
+ return in(1); // Input has collapse to e.g. starting control.
+ if( r.inProgress() || r.nIns()<=1 )
+ return null; // Input is in-progress
+
+ // If we have only a single unique input, become it.
+ Node live = singleUniqueInput();
+ if (live != null)
+ return live;
+
+ // Pull "down" a common data op. One less op in the world. One more
+ // Phi, but Phis do not make code.
+ // Phi(op(A,B),op(Q,R),op(X,Y)) becomes
+ // op(Phi(A,Q,X), Phi(B,R,Y)).
+ Node op = in(1);
+ if( op.nIns()==3 && op.in(0)==null && !op.isCFG() && same_op() ) {
+ Node[] lhss = new Node[nIns()];
+ Node[] rhss = new Node[nIns()];
+ lhss[0] = rhss[0] = in(0); // Set Region
+ for( int i=1; i<nIns(); i++ ) {
+ lhss[i] = in(i).in(1);
+ rhss[i] = in(i).in(2);
+ }
+ Node phi_lhs = new PhiNode(_label, _declaredType,lhss).peephole();
+ Node phi_rhs = new PhiNode(_label, _declaredType,rhss).peephole();
+ return op.copy(phi_lhs,phi_rhs);
+ }
+
+ // If merging Phi(N, cast(N)) - we are losing the cast JOIN effects, so just remove.
+ if( nIns()==3 ) {
+ if( in(1) instanceof CastNode cast && cast.in(1).addDep(this)==in(2) ) return in(2);
+ if( in(2) instanceof CastNode cast && cast.in(1).addDep(this)==in(1) ) return in(1);
+ }
+ // If merging a null-checked null and the checked value, just use the value.
+ // if( val ) ..; phi(Region,False=0/null,True=val);
+ // then replace with plain val.
+ if( nIns()==3 ) {
+ int nullx = -1;
+ if( in(1)._type == in(1)._type.makeInit() ) nullx = 1;
+ if( in(2)._type == in(2)._type.makeInit() ) nullx = 2;
+ if( nullx != -1 ) {
+ Node val = in(3-nullx);
+ if( region().idom() instanceof IfNode iff && iff.pred().addDep(this)==val ) {
+ // Must walk the idom on the null side to make sure we hit False.
+ CFGNode idom = (CFGNode)region().in(nullx);
+ while( idom.in(0) != iff ) idom = idom.idom();
+ if( idom instanceof CProjNode proj && proj._idx==1 )
+ return val;
+ }
+ }
+ }
+
+ return null;
+ }
+
+ private boolean same_op() {
+ for( int i=2; i<nIns(); i++ )
+ if( in(1).getClass() != in(i).getClass() )
+ return false;
+ return true;
+ }
+
+ /**
+ * If only single unique input, return it
+ */
+ private Node singleUniqueInput() {
+ if( region() instanceof LoopNode loop && loop.entry()._type == Type.XCONTROL )
+ return null; // Dead entry loops just ignore and let the loop collapse
+ Node live = null;
+ for( int i=1; i<nIns(); i++ ) {
+ // If the region's control input is live, add this as a dependency
+ // to the control because we can be peeped should it become dead.
+ if( region().in(i).addDep(this)._type != Type.XCONTROL && in(i) != this )
+ if( live == null || live == in(i) ) live = in(i);
+ else return null;
+ }
+ return live;
+ }
+
+ @Override
+ boolean allCons(Node dep) {
+ if( !(region() instanceof RegionNode r) ) return false;
+ // When the region completes (is no longer in progress) the Phi can
+ // become a "all constants" Phi, and the "dep" might make progress.
+ addDep(dep);
+ if( r.inProgress() ) return false;
+ return super.allCons(dep);
+ }
+
+ // True if last input is null
+ public final boolean inProgress() {
+ return in(nIns()-1) == null;
+ }
+
+ // Never equal if inProgress
+ @Override boolean eq( Node n ) {
+ return !inProgress();
+ }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/ProjNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/ProjNode.java
new file mode 100644
index 00000000..f94e98a7
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/ProjNode.java
@@ -0,0 +1,46 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.type.TypeMem;
+import com.seaofnodes.simple.type.TypeTuple;
+
+import java.util.BitSet;
+
+public class ProjNode extends Node {
+
+ // Which slice of the incoming multipart value
+ public final int _idx;
+
+ // Debugging label
+ public final String _label;
+
+ public ProjNode(Node ctrl, int idx, String label) {
+ super(ctrl);
+ _idx = idx;
+ _label = label;
+ }
+
+ @Override
+ public String label() { return _label; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) { return sb.append(_label); }
+
+ @Override public boolean isMultiTail() { return in(0).isMultiHead(); }
+ @Override public boolean isMem() { return _type instanceof TypeMem; }
+ @Override public boolean isPinned() { return true; }
+
+ @Override
+ public Type compute() {
+ Type t = in(0)._type;
+ return t instanceof TypeTuple tt ? tt._types[_idx] : Type.BOTTOM;
+ }
+
+ @Override public Node idealize() { return null; }
+
+ @Override
+ boolean eq( Node n ) { return _idx == ((ProjNode)n)._idx; }
+
+ @Override
+ int hash() { return _idx; }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/RegionNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/RegionNode.java
new file mode 100644
index 00000000..dcc7ccef
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/RegionNode.java
@@ -0,0 +1,129 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.IterPeeps;
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.Utils;
+import com.seaofnodes.simple.type.Type;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.HashSet;
+
+public class RegionNode extends CFGNode {
+
+ public RegionNode(Node... nodes) { super(nodes); }
+
+ @Override
+ public String label() { return "Region"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ return sb.append(label()).append(_nid);
+ }
+
+ @Override public boolean isMultiHead() { return true; }
+ @Override public boolean blockHead() { return true; }
+
+ @Override
+ public Type compute() {
+ if( inProgress() ) return Type.CONTROL;
+ Type t = Type.XCONTROL;
+ for (int i = 1; i < nIns(); i++)
+ t = t.meet(in(i)._type);
+ return t;
+ }
+
+ @Override
+ public Node idealize() {
+ if( inProgress() ) return null;
+ // Delete dead paths into a Region
+ int path = findDeadInput();
+ if( path != 0 &&
+ // Do not delete the entry path of a loop (ok to remove the back
+ // edge and make the loop a single-entry Region which folds away
+ // the Loop). Folding the entry path confused the loop structure,
+ // moving the backedge to the entry point.
+ !(this instanceof LoopNode loop && loop.entry()==in(path)) ) {
+ // Cannot use the obvious output iterator here, because a Phi
+ // deleting an input might recursively delete *itself*. This
+ // shuffles the output array, and we might miss iterating an
+ // unrelated Phi. So on rare occasions we repeat the loop to get
+ // all the Phis.
+ int nouts = 0;
+ while( nouts != nOuts() ) {
+ nouts = nOuts();
+ for( int i=0; i<nOuts(); i++ )
+ if( out(i) instanceof PhiNode phi && phi.nIns()==nIns() ) {
+ phi.delDef(path);
+ IterPeeps.addAll(phi._outputs);
+ }
+ }
+ return isDead() ? Parser.XCTRL : delDef(path);
+ }
+ // If down to a single input, become that input
+ if( nIns()==2 && !hasPhi() )
+ return in(1); // Collapse if no Phis; 1-input Phis will collapse on their own
+
+ // If a CFG diamond with no merging, delete: "if( pred ) {} else {};"
+ if( !hasPhi() && // No Phi users, just a control user
+ in(1) instanceof ProjNode p1 &&
+ in(2) instanceof ProjNode p2 &&
+ p1.in(0)==p2.in(0) &&
+ p1.in(0) instanceof IfNode iff )
+ return iff.ctrl();
+
+ return null;
+ }
+
+ private int findDeadInput() {
+ for( int i=1; i<nIns(); i++ )
+ if( in(i)._type==Type.XCONTROL )
+ return i;
+ return 0; // All inputs alive
+ }
+
+ private boolean hasPhi() {
+ for( Node phi : _outputs )
+ if( phi instanceof PhiNode )
+ return true;
+ return false;
+ }
+
+ // Immediate dominator of Region is a little more complicated.
+ @Override public int idepth() {
+ if( _idepth!=0 ) return _idepth;
+ int d=0;
+ for( Node n : _inputs )
+ if( n!=null )
+ d = Math.max(d,((CFGNode)n).idepth()+1);
+ return _idepth=d;
+ }
+
+ @Override public CFGNode idom() {
+ CFGNode lca = null;
+ // Walk the LHS & RHS idom trees in parallel until they match, or either fails.
+ // Because this does not cache, it can be linear in the size of the program.
+ for( int i=1; i<nIns(); i++ )
+ lca = cfg(i).idom(lca);
+ return lca;
+ }
+
+ @Override void _walkUnreach( BitSet visit, HashSet<CFGNode> unreach ) {
+ for( int i=1; i<nIns(); i++ )
+ cfg(i).walkUnreach(visit,unreach);
+ }
+
+ @Override public int loopDepth() { return _loopDepth==0 ? (_loopDepth = cfg(1).loopDepth()) : _loopDepth; }
+
+ // True if last input is null
+ public final boolean inProgress() {
+ return nIns()>1 && in(nIns()-1) == null;
+ }
+
+ // Never equal if inProgress
+ @Override boolean eq( Node n ) {
+ return !inProgress();
+ }
+
+ @Override public Node getBlockStart() { return this; }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/ReturnNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
new file mode 100644
index 00000000..444b76a8
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/ReturnNode.java
@@ -0,0 +1,57 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.*;
+
+import java.util.BitSet;
+
+/**
+ * The Return node has two inputs. The first input is a control node and the
+ * second is the data node that supplies the return value.
+ * <p>
+ * In this presentation, Return functions as a Stop node, since multiple <code>return</code> statements are not possible.
+ * The Stop node will be introduced in Chapter 6 when we implement <code>if</code> statements.
+ * <p>
+ * The Return's output is the value from the data node.
+ */
+public class ReturnNode extends CFGNode {
+
+ public ReturnNode(Node ctrl, Node data, ScopeNode scope) {
+ super(ctrl, data);
+ // We lookup memory slices by the naming convention that they start with $
+ // We could also use implicit knowledge that all memory projects are at offset >= 2
+ if( scope != null ) {
+ String[] names = scope.reverseNames();
+ for (String name: names) {
+ if (!name.equals("$ctrl") && name.startsWith("$"))
+ addDef(scope.lookup(name));
+ }
+ }
+ }
+
+ public Node ctrl() { return in(0); }
+ public Node expr() { return in(1); }
+
+ @Override
+ public String label() { return "Return"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ sb.append("return ");
+ expr()._print0(sb, visited);
+ return sb.append(";");
+ }
+
+ @Override
+ public Type compute() {
+ return TypeTuple.make(ctrl()._type,expr()._type);
+ }
+
+ @Override
+ public Node idealize() {
+ if( ctrl()._type==Type.XCONTROL )
+ return ctrl();
+ return null;
+ }
+
+ @Override public Node getBlockStart() { return ctrl().getBlockStart(); }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/ScopeNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/ScopeNode.java
new file mode 100644
index 00000000..843ce733
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/ScopeNode.java
@@ -0,0 +1,297 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.IterPeeps;
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.Utils;
+import com.seaofnodes.simple.type.TypeMemPtr;
+
+import java.util.*;
+
+/**
+ * The Scope node is purely a parser helper - it tracks names to nodes with a
+ * stack of scopes.
+ */
+public class ScopeNode extends Node {
+
+ /**
+ * The control is a name that binds to the currently active control
+ * node in the graph
+ */
+ public static final String CTRL = "$ctrl";
+ public static final String ARG0 = "arg";
+
+ /**
+ * Names for every input edge
+ */
+ public final Stack<HashMap<String, Integer>> _scopes;
+
+ /**
+ * Tracks declared types for every name
+ */
+ public Stack<HashMap<String, Type>> _types;
+
+
+ // A new ScopeNode
+ public ScopeNode() {
+ _scopes = new Stack<>();
+ _types = new Stack<>();
+ _type = Type.BOTTOM;
+ }
+
+
+ @Override public String label() { return "Scope"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ sb.append("Scope[ ");
+ String[] names = reverseNames();
+ for( int j=0; j<nIns(); j++ ) {
+ sb.append(names[j]).append(":");
+ Node n = in(j);
+ while( n instanceof ScopeNode loop ) {
+ sb.append("Lazy_");
+ n = loop.in(j);
+ }
+ n._print0(sb, visited).append(" ");
+ }
+ sb.setLength(sb.length()-1);
+ return sb.append("]");
+ }
+
+ /**
+ * Recover the names for all variable bindings.
+ * The result is an array of names that is aligned with the
+ * inputs to the Node.
+ *
+ * This is an expensive operation.
+ */
+ public String[] reverseNames() {
+ String[] names = new String[nIns()];
+ for( HashMap<String,Integer> syms : _scopes )
+ for( String name : syms.keySet() )
+ names[syms.get(name)] = name;
+ return names;
+ }
+
+ @Override public Type compute() { return Type.BOTTOM; }
+
+ @Override public Node idealize() { return null; }
+
+ public void push() { _scopes.push(new HashMap<>()); _types.push(new HashMap<>()); }
+ public void pop() { popN(_scopes.pop().size()); _types.pop(); }
+
+ /**
+ * Create a new name in the current scope
+ */
+ public Node define( String name, Type declaredType, Node n ) {
+ HashMap<String,Integer> syms = _scopes.lastElement();
+ _types.lastElement().put(name,declaredType);
+ if( syms.put(name,nIns()) != null )
+ return null; // Double define
+ return addDef(n);
+ }
+ /**
+ * Lookup a name in all scopes starting from most deeply nested.
+ *
+ * @param name Name to be looked up
+ * @see #update(String, Node, int)
+ */
+ public Node lookup( String name ) { return update(name,null,_scopes.size()-1); }
+ /**
+ * If the name is present in any scope, then redefine else null
+ *
+ * @param name Name being redefined
+ * @param n The node to bind to the name
+ */
+ public Node update( String name, Node n ) { return update(name,n,_scopes.size()-1); }
+ /**
+ * Both recursive lookup and update.
+ * <p>
+ * A shared implementation allows us to create lazy phis both during
+ * lookups and updates; the lazy phi creation is part of chapter 8.
+ *
+ * @param name Name whose binding is being updated or looked up
+ * @param n If null, do a lookup, else update binding
+ * @param nestingLevel The starting nesting level
+ * @return node being looked up, or the one that was updated
+ */
+ private Node update( String name, Node n, int nestingLevel ) {
+ if( nestingLevel<0 ) return null; // Missed in all scopes, not found
+ var syms = _scopes.get(nestingLevel); // Get the symbol table for nesting level
+ var idx = syms.get(name);
+ if( idx == null ) return update(name,n,nestingLevel-1); // Not found in this scope, recursively look in parent scope
+ Node old = in(idx);
+ if( old instanceof ScopeNode loop ) {
+ // Lazy Phi!
+ old = loop.in(idx) instanceof PhiNode phi && loop.ctrl()==phi.region()
+ // Loop already has a real Phi, use it
+ ? loop.in(idx)
+ // Set real Phi in the loop head
+ // The phi takes its one input (no backedge yet) from a recursive
+ // lookup, which might have insert a Phi in every loop nest.
+ : loop.setDef(idx,new PhiNode(name, lookupDeclaredType(name),loop.ctrl(),loop.update(name,null,nestingLevel),null).peephole());
+ setDef(idx,old);
+ }
+ return n==null ? old : setDef(idx,n); // Not lazy, so this is the answer
+ }
+
+ // Return declared type
+ public Type lookupDeclaredType( String name ) {
+ for( int i=_types.size(); i>0; i-- ) {
+ Type t = _types.get(i-1).get(name);
+ if( t != null ) return t;
+ }
+ return null;
+ }
+
+ public Node ctrl() { return in(0); }
+
+ /**
+ * The ctrl of a ScopeNode is always bound to the currently active
+ * control node in the graph, via a special name '$ctrl' that is not
+ * a valid identifier in the language grammar and hence cannot be
+ * referenced in Simple code.
+ *
+ * @param n The node to be bound to '$ctrl'
+ *
+ * @return Node that was bound
+ */
+ public Node ctrl(Node n) { return setDef(0,n); }
+
+ /**
+ * Duplicate a ScopeNode; including all levels, up to Nodes. So this is
+ * neither shallow (would dup the Scope but not the internal HashMap
+ * tables), nor deep (would dup the Scope, the HashMap tables, but then
+ * also the program Nodes).
+ * <p>
+ * If the {@code loop} flag is set, the edges are filled in as the original
+ * Scope, as a indication of Lazy Phis at loop heads. The goal here is to
+ * not make Phis at loop heads for variables which are never touched in the
+ * loop body.
+ * <p>
+ * The new Scope is a full-fledged Node with proper use<->def edges.
+ */
+ public ScopeNode dup() { return dup(false); }
+ public ScopeNode dup(boolean loop) {
+ ScopeNode dup = new ScopeNode();
+ // Our goals are:
+ // 1) duplicate the name bindings of the ScopeNode across all stack levels
+ // 2) Make the new ScopeNode a user of all the nodes bound
+ // 3) Ensure that the order of defs is the same to allow easy merging
+ for( HashMap<String,Integer> syms : _scopes )
+ dup._scopes.push(new HashMap<>(syms));
+ for( HashMap<String,Type> ts : _types )
+ dup._types.push(ts); // Types don't change, just keep stacks aligned
+
+ dup.addDef(ctrl()); // Control input is just copied
+ for( int i=1; i<nIns(); i++ )
+ // For lazy phis on loops we use a sentinel
+ // that will trigger phi creation on update
+ dup.addDef(loop ? this : in(i));
+ return dup;
+ }
+
+ /**
+ * Merges the names whose node bindings differ, by creating Phi node for such names
+ * The names could occur at all stack levels, but a given name can only differ in the
+ * innermost stack level where the name is bound.
+ *
+ * @param that The ScopeNode to be merged into this
+ * @return A new node representing the merge point
+ */
+ public Node mergeScopes(ScopeNode that) {
+ RegionNode r = (RegionNode) ctrl(new RegionNode(null,ctrl(), that.ctrl()).keep());
+ String[] ns = reverseNames();
+ // Note that we skip i==0, which is bound to '$ctrl'
+ for (int i = 1; i < nIns(); i++)
+ if( in(i) != that.in(i) ) // No need for redundant Phis
+ // If we are in lazy phi mode we need to a lookup
+ // by name as it will trigger a phi creation
+ setDef(i, new PhiNode(ns[i], this.lookupDeclaredType(ns[i]), r, this.lookup(ns[i]), that.lookup(ns[i])).peephole());
+ that.kill(); // Kill merged scope
+ IterPeeps.add(r);
+ return r.unkeep().peephole();
+ }
+
+ // Merge the backedge scope into this loop head scope
+ // We set the second input to the phi from the back edge (i.e. loop body)
+ public void endLoop(ScopeNode back, ScopeNode exit ) {
+ Node ctrl = ctrl();
+ assert ctrl instanceof LoopNode loop && loop.inProgress();
+ ctrl.setDef(2,back.ctrl());
+ for( int i=1; i<nIns(); i++ ) {
+ if( back.in(i) != this ) {
+ PhiNode phi = (PhiNode)in(i);
+ assert phi.region()==ctrl && phi.in(2)==null;
+ phi.setDef(2,back.in(i));
+ }
+ if( exit.in(i) == this ) // Replace a lazy-phi on the exit path also
+ exit.setDef(i,in(i));
+ }
+ back.kill(); // Loop backedge is dead
+ // Now one-time do a useless-phi removal
+ for( int i=1; i<nIns(); i++ ) {
+ if( in(i) instanceof PhiNode phi ) {
+ // Do an eager useless-phi removal
+ Node in = phi.peephole();
+ IterPeeps.addAll(phi._outputs);
+ phi.moveDepsToWorklist();
+ if( in != phi ) {
+ if( !phi.iskeep() ) // Keeping phi around for parser elsewhere
+ phi.subsume(in);
+ setDef(i,in); // Set the update back into Scope
+ }
+ }
+ }
+
+ }
+
+
+ // Up-casting: using the results of an If to improve a value.
+ // E.g. "if( ptr ) ptr.field;" is legal because ptr is known not-null.
+
+ // This Scope looks for direct variable uses, or certain simple
+ // combinations, and replaces the variable with the upcast variant.
+ public Node upcast( Node ctrl, Node pred, boolean invert ) {
+ if( ctrl._type==Type.XCONTROL ) return null;
+ // Invert the If conditional
+ if( invert )
+ pred = pred instanceof NotNode not ? not.in(1) : IterPeeps.add(new NotNode(pred).peephole());
+
+ // Direct use of a value as predicate. This is a zero/null test.
+ if( Utils.find(_inputs, pred) != -1 ) {
+ if( !(pred._type instanceof TypeMemPtr tmp) )
+ // Must be an `int`, since int and ptr are the only two value types
+ // being tested. No representation for a generic not-null int, so no upcast.
+ return null;
+ if( tmp.isa(TypeMemPtr.VOIDPTR) )
+ return null; // Already not-null, no reason to upcast
+ // Upcast the ptr to not-null ptr, and replace in scope
+ return replace(pred,new CastNode(TypeMemPtr.VOIDPTR,ctrl,pred).peephole());
+ }
+
+ if( pred instanceof NotNode not ) {
+ // Direct use of a !value as predicate. This is a zero/null test.
+ if( Utils.find(_inputs, not.in(1)) != -1 ) {
+ Type tinit = not.in(1)._type.makeInit();
+ if( not.in(1)._type.isa(tinit) ) return null; // Already zero/null, no reason to upcast
+ return replace(not.in(1), new ConstantNode(tinit).peephole());
+ }
+ }
+ // Apr/9/2024: Attempted to replace X with Y if guarded by a test of
+ // X==Y. This didn't seem to help very much, or at least in the test
+ // cases seen so far was a very minor help.
+
+ // No upcast
+ return null;
+ }
+
+ private Node replace( Node old, Node cast ) {
+ assert old!=null && old!=cast;
+ for( int i=0; i<nIns(); i++ )
+ if( in(i)==old )
+ setDef(i,cast);
+ return cast;
+ }
+
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/StartNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/StartNode.java
new file mode 100644
index 00000000..62fc8a6b
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/StartNode.java
@@ -0,0 +1,85 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.type.*;
+import java.util.Arrays;
+import java.util.BitSet;
+import java.util.HashMap;
+import java.util.HashSet;
+
+/**
+ * The Start node represents the start of the function.
+ *
+ * Start initially has 1 input (arg) from outside and the initial control.
+ * In ch10 we also add mem aliases as structs get defined; each field in struct
+ * adds a distinct alias to Start's tuple.
+ */
+public class StartNode extends CFGNode implements MultiNode {
+
+ // This field is NOT final, because the tuple expands as we add memory aliases
+ TypeTuple _args;
+
+ // The alias number of the first field in the struct.
+ //
+ public final HashMap<String,Integer> _aliasStarts;
+
+
+ public StartNode(Type[] args) {
+ super();
+ _args = TypeTuple.make(args);
+ _type = _args;
+ _aliasStarts = new HashMap<>();
+ }
+
+ /**
+ * Creates a projection for each of the struct's fields, using the field alias
+ * as the key.
+ */
+ public void addMemProj( TypeStruct ts, ScopeNode scope) {
+ int len = _args._types.length;
+ _aliasStarts.put(ts._name,len);
+
+ // resize the tuple's type array to include all fields of the struct
+ int max = len + ts._fields.length;
+ Type[] args = Arrays.copyOf(_args._types, max);
+
+ // The new members of the tuple get a mem type with an alias
+ for( int alias = len; alias < max; alias++ )
+ args[alias] = TypeMem.make(alias);
+ _type = _args = TypeTuple.make(args);
+ // For each of the fields we now add a mem projection. Note that the
+ // alias matches the slot of the field in the tuple
+ for( int alias = len; alias < max; alias++ ) {
+ String name = Parser.memName(alias);
+ Node n = new ProjNode(this, alias, name).peephole();
+ scope.define(name, args[alias], n);
+ }
+ }
+
+ @Override
+ public String label() { return "Start"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ return sb.append(label());
+ }
+
+ @Override public boolean isMultiHead() { return true; }
+ @Override public boolean blockHead() { return true; }
+
+ @Override
+ public TypeTuple compute() { return _args; }
+
+ @Override
+ public Node idealize() { return null; }
+
+ // No immediate dominator, and idepth==0
+ @Override public int idepth() { return 0; }
+ @Override public CFGNode idom() { return null; }
+
+ @Override void _walkUnreach( BitSet visit, HashSet<CFGNode> unreach ) { }
+
+ @Override public int loopDepth() { return (_loopDepth=1); }
+
+ @Override public Node getBlockStart() { return this; }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/StopNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/StopNode.java
new file mode 100644
index 00000000..94b2ad6d
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/StopNode.java
@@ -0,0 +1,79 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.type.Type;
+import com.seaofnodes.simple.IterPeeps;
+
+import java.util.BitSet;
+
+public class StopNode extends CFGNode {
+
+ public final String _src;
+
+ public StopNode(String src) {
+ super();
+ _src = src;
+ }
+
+ @Override
+ public String label() {
+ return "Stop";
+ }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ if( ret()!=null ) return ret()._print0(sb, visited);
+ sb.append("Stop[ ");
+ for( Node ret : _inputs )
+ ret._print0(sb, visited).append(" ");
+ return sb.append("]");
+ }
+
+ @Override public boolean blockHead() { return true; }
+
+ // If a single Return, return it.
+ // Otherwise, null because ambiguous.
+ public ReturnNode ret() {
+ return nIns()==1 ? (ReturnNode)in(0) : null;
+ }
+
+ @Override
+ public Type compute() {
+ return Type.BOTTOM;
+ }
+
+ @Override
+ public Node idealize() {
+ int len = nIns();
+ for( int i=0; i<nIns(); i++ )
+ if( in(i)._type==Type.XCONTROL )
+ delDef(i--);
+ if( len != nIns() ) return this;
+ return null;
+ }
+
+ @Override public int idepth() {
+ if( _idepth!=0 ) return _idepth;
+ int d=0;
+ for( Node n : _inputs )
+ if( n!=null )
+ d = Math.max(d,((CFGNode)n).idepth()+1);
+ return _idepth=d;
+ }
+ @Override public CFGNode idom() { return null; }
+
+ @Override public int loopDepth() { return (_loopDepth=1); }
+
+ public Node addReturn(Node node) {
+ return addDef(node);
+ }
+
+ public StopNode iterate( ) { return IterPeeps.iterate(this,false).typeCheck(); }
+ public StopNode iterate(boolean show) { return IterPeeps.iterate(this,show ).typeCheck(); }
+ StopNode typeCheck() {
+ String err = walk( Node::err );
+ if( err != null ) throw new RuntimeException(err);
+ return this;
+ }
+
+ @Override public Node getBlockStart() { return this; }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/StoreNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/StoreNode.java
new file mode 100644
index 00000000..7c41e767
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/StoreNode.java
@@ -0,0 +1,70 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Utils;
+import com.seaofnodes.simple.type.*;
+
+import java.util.BitSet;
+
+/**
+ * Store represents setting a value to a memory based object, in chapter 10
+ * this means a field inside a struct.
+ */
+public class StoreNode extends MemOpNode {
+
+ /**
+ * @param name The struct field we are assigning to
+ * @param memSlice The memory alias node - this is updated after a Store
+ * @param memPtr The ptr to the struct where we will store a value
+ * @param value Value to be stored
+ */
+ public StoreNode(String name, int alias, Node memSlice, Node memPtr, Node value) {
+ super(name, alias, memSlice, memPtr, value);
+ }
+
+ @Override
+ public String label() { return "Store"; }
+ @Override
+ public String glabel() { return "."+_name+" ="; }
+ @Override
+ public boolean isMem() { return true; }
+ public Node val() { return in(3); }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ return sb.append(".").append(_name).append("=").append( val()).append(";");
+ }
+
+ @Override
+ public Type compute() { return TypeMem.make(_alias); }
+
+ @Override
+ public Node idealize() {
+
+ // Simple store-after-store on same address. Should pick up the
+ // required init-store being stomped by a first user store.
+ if( mem() instanceof StoreNode st &&
+ ptr()==st.ptr() && // Must check same object
+ ptr()._type instanceof TypeMemPtr && // No bother if weird dead pointers
+ // Must have exactly one use of "this" or you get weird
+ // non-serializable memory effects in the worse case.
+ st.checkNoUseBeyond(this) ) {
+ assert _name.equals(st._name); // Equiv class aliasing is perfect
+ setDef(1,st.mem());
+ return this;
+ }
+
+ return null;
+ }
+
+ // Check that `this` has no uses beyond `that`
+ private boolean checkNoUseBeyond(Node that) {
+ if( nOuts()==1 ) return true;
+ // Add deps on the other uses (can be e.g. ScopeNode mid-parse) so that
+ // when the other uses go away we can retry.
+ for( Node use : _outputs )
+ if( use != that )
+ use.addDep(that);
+ return false;
+ }
+
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/SubNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/SubNode.java
new file mode 100644
index 00000000..7137f38e
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/SubNode.java
@@ -0,0 +1,51 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Parser;
+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 public String glabel() { return "-"; }
+
+ @Override
+ StringBuilder _print1(StringBuilder sb, BitSet visited) {
+ in(1)._print0(sb.append("("), visited);
+ in(2)._print0(sb.append("-"), visited);
+ return sb.append(")");
+ }
+
+ @Override
+ public Type compute() {
+ if (in(1)._type instanceof TypeInteger i0 &&
+ in(2)._type instanceof TypeInteger i1) {
+ if (i0.isConstant() && i1.isConstant())
+ return TypeInteger.constant(i0.value()-i1.value());
+ }
+ return in(1)._type.meet(in(2)._type);
+ }
+
+ @Override
+ public Node idealize() {
+ // Sub of same is 0
+ if( in(1)==in(2) )
+ return Parser.ZERO;
+
+ // x - (-y) is x+y
+ if( in(2) instanceof MinusNode minus )
+ return new AddNode(in(1),minus.in(1));
+
+ // (-x) - y is -(x+y)
+ if( in(1) instanceof MinusNode minus )
+ return new MinusNode(new AddNode(minus.in(1),in(2)).peephole());
+
+ return null;
+ }
+
+ @Override Node copy(Node lhs, Node rhs) { return new SubNode(lhs,rhs); }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/node/XCtrlNode.java b/chapter12/src/main/java/com/seaofnodes/simple/node/XCtrlNode.java
new file mode 100644
index 00000000..83779dd2
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/node/XCtrlNode.java
@@ -0,0 +1,13 @@
+package com.seaofnodes.simple.node;
+
+import com.seaofnodes.simple.Parser;
+import com.seaofnodes.simple.type.Type;
+import java.util.BitSet;
+
+public class XCtrlNode extends CFGNode {
+ public XCtrlNode( ) { super(Parser.START); }
+ @Override public String label() { return "Xctrl"; }
+ @Override StringBuilder _print1(StringBuilder sb, BitSet visited) { return sb.append("Xctrl"); }
+ @Override public Type compute() { return Type.XCONTROL; }
+ @Override public Node idealize() { return null; }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/type/Field.java b/chapter12/src/main/java/com/seaofnodes/simple/type/Field.java
new file mode 100644
index 00000000..e1c337d4
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/type/Field.java
@@ -0,0 +1,60 @@
+package com.seaofnodes.simple.type;
+
+import com.seaofnodes.simple.Utils;
+import java.util.ArrayList;
+
+/**
+ * Represents a field in a struct. This is not a Type in the type system.
+ */
+public class Field extends Type {
+
+ // The pair {fieldName,type} uniquely identifies a field.
+
+ // Field name
+ public final String _fname;
+ // Type of the field
+ public final Type _type;
+
+ private Field(String fname, Type type ) {
+ super(TFLD);
+ _fname = fname;
+ _type = type;
+ }
+ // Make with existing alias
+ public static Field make( String fname, Type type ) {
+ return new Field(fname,type).intern();
+ }
+
+ public static final Field TEST = make("test",TypeInteger.ZERO);
+ public static void gather(ArrayList<Type> ts) { ts.add(TEST); }
+
+ @Override Field xmeet( Type that ) {
+ Field fld = (Field)that; // Invariant
+ assert _fname.equals(fld._fname);
+ return make(_fname,_type.meet(fld._type));
+ }
+
+ @Override
+ public Field dual() { return make(_fname,_type.dual()); }
+
+ @Override
+ public Field glb() {
+ return make(_fname,_type.glb());
+ }
+
+ // Override in subclasses
+ int hash() { return _fname.hashCode() ^ _type.hashCode(); }
+
+ boolean eq(Type t) {
+ Field f = (Field)t;
+ return _fname.equals(f._fname) && _type==f._type;
+ }
+
+
+ @Override
+ public StringBuilder _print( StringBuilder sb ) {
+ return _type._print(sb.append(_fname).append(":"));
+ }
+
+ @Override public String str() { return _fname; }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/type/Type.java b/chapter12/src/main/java/com/seaofnodes/simple/type/Type.java
new file mode 100644
index 00000000..aa0ba1ea
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/type/Type.java
@@ -0,0 +1,187 @@
+package com.seaofnodes.simple.type;
+
+import com.seaofnodes.simple.Utils;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+
+/**
+ * These types are part of a Monotone Analysis Framework,
+ * @see <a href="https://www.cse.psu.edu/~gxt29/teaching/cse597s21/slides/08monotoneFramework.pdf">see for example this set of slides</a>.
+ * <p>
+ * The types form a lattice; @see <a href="https://en.wikipedia.org/wiki/Lattice_(order)">a symmetric complete bounded (ranked) lattice.</a>
+ * <p>
+ * This wild lattice theory will be needed later to allow us to easily beef up
+ * the analysis and optimization of the Simple compiler... but we don't need it
+ * now, just know that it is coming along in a later Chapter.
+ * <p>g
+ * One of the fun things here is that while the theory is deep and subtle, the
+ * actual implementation is darn near trivial and is generally really obvious
+ * what we're doing with it. Right now, it's just simple integer math to do
+ * simple constant folding e.g. 1+2 == 3 stuff.
+ */
+
+public class Type {
+ static final HashMap<Type,Type> INTERN = new HashMap<>();
+
+ // ----------------------------------------------------------
+ // Simple types are implemented fully here. "Simple" means: the code and
+ // type hierarchy are simple, not that the Type is conceptually simple.
+ static final byte TBOT = 0; // Bottom (ALL)
+ static final byte TTOP = 1; // Top (ANY)
+ static final byte TCTRL = 2; // Ctrl flow bottom
+ static final byte TXCTRL = 3; // Ctrl flow top (mini-lattice: any-xctrl-ctrl-all)
+ static final byte TSIMPLE = 4; // End of the Simple Types
+ static final byte TINT = 5; // All Integers; see TypeInteger
+ static final byte TTUPLE = 6; // Tuples; finite collections of unrelated Types, kept in parallel
+ static final byte TMEM = 7; // All memory (alias 0) or A slice of memory - with specific alias
+ static final byte TMEMPTR = 8; // Memory pointer type
+ static final byte TSTRUCT = 9; // Structs; tuples with named fields
+ static final byte TFLD =10; // Fields into struct
+
+ public final byte _type;
+
+ public boolean is_simple() { return _type < TSIMPLE; }
+ private static final String[] STRS = new String[]{"Bot","Top","Ctrl","~Ctrl"};
+ protected Type(byte type) { _type = type; }
+
+ public static final Type BOTTOM = new Type( TBOT ).intern(); // ALL
+ public static final Type TOP = new Type( TTOP ).intern(); // ANY
+ public static final Type CONTROL = new Type( TCTRL ).intern(); // Ctrl
+ public static final Type XCONTROL = new Type( TXCTRL ).intern(); // ~Ctrl
+ public static Type[] gather() {
+ ArrayList<Type> ts = new ArrayList<>();
+ ts.add(BOTTOM);
+ ts.add(CONTROL);
+ Field.gather(ts);
+ TypeInteger.gather(ts);
+ TypeMem.gather(ts);
+ TypeMemPtr.gather(ts);
+ TypeStruct.gather(ts);
+ TypeTuple.gather(ts);
+ int sz = ts.size();
+ for( int i = 0; i < sz; i++ )
+ ts.add(ts.get(i).dual());
+ return ts.toArray(new Type[ts.size()]);
+ }
+
+ // Is high or on the lattice centerline.
+ public boolean isHighOrConst() { return _type==TTOP || _type==TXCTRL; }
+
+ // Strict constant values, things on the lattice centerline.
+ // Excludes both high and low values
+ public boolean isConstant() { return false; }
+
+ /**
+ * Display Type name in a format that's good for IR printer
+ */
+ public StringBuilder typeName( StringBuilder sb) { return _print(sb); }
+
+ public Type makeInit() { return null; }
+
+ // ----------------------------------------------------------
+
+ // Notes on Type interning.
+ // At the moment it is not easy to reset the interned types
+ // because we hold static references to several types and these are scattered
+ // around. This means the INTERN cache will retain all types from
+ // every run of the Parser. For this to work correctly types must be
+ // rigorous about defining when they are the same. Also types need to be
+ // immutable once defined.
+ // The rationale for interning is performance.
+
+ // Factory method which interns "this"
+ public <T extends Type> T intern() {
+ T nnn = (T)INTERN.get(this);
+ if( nnn==null )
+ INTERN.put(nnn=(T)this,this);
+ return nnn;
+ }
+
+ private int _hash; // Hash cache; not-zero when set.
+ @Override
+ public final int hashCode() {
+ if( _hash!=0 ) return _hash;
+ _hash = hash();
+ if( _hash==0 ) _hash = 0xDEADBEEF; // Bad hash from subclass; use some junk thing
+ return _hash;
+ }
+ // Override in subclasses
+ int hash() { return _type; }
+
+ @Override
+ public final boolean equals( Object o ) {
+ if( o==this ) return true;
+ if( !(o instanceof Type t)) return false;
+ if( _type != t._type ) return false;
+ return eq(t);
+ }
+ // Overridden in subclasses; subclass can assume "this!=t" and java classes are same
+ boolean eq(Type t) { return true; }
+
+
+ // ----------------------------------------------------------
+ public final Type meet(Type t) {
+ // Shortcut for the self case
+ if( t == this ) return this;
+ // Same-type is always safe in the subclasses
+ if( _type==t._type ) return xmeet(t);
+ // Reverse; xmeet 2nd arg is never "is_simple" and never equal to "this".
+ if( is_simple() ) return this.xmeet(t );
+ if( t.is_simple() ) return t .xmeet(this);
+ return Type.BOTTOM; // Mixing 2 unrelated types
+ }
+
+ // Compute meet right now. Overridden in subclasses.
+ // Handle cases where 'this.is_simple()' and unequal to 't'.
+ // Subclassed xmeet calls can assert that '!t.is_simple()'.
+ Type xmeet(Type t) {
+ assert is_simple(); // Should be overridden in subclass
+ // ANY meet anything is thing; thing meet ALL is ALL
+ if( _type==TBOT || t._type==TTOP ) return this;
+ if( _type==TTOP || t._type==TBOT ) return t;
+ // 'this' is {TCTRL,TXCTRL}
+ if( !t.is_simple() ) return BOTTOM;
+ // 't' is {TCTRL,TXCTRL}
+ return _type==TCTRL || t._type==TCTRL ? CONTROL : XCONTROL;
+ }
+
+ public Type dual() {
+ return switch( _type ) {
+ case TBOT -> TOP;
+ case TTOP -> BOTTOM;
+ case TCTRL -> XCONTROL;
+ case TXCTRL -> CONTROL;
+ default -> throw Utils.TODO(); // Should not reach here
+ };
+ }
+
+ // ----------------------------------------------------------
+ // Our lattice is defined with a MEET and a DUAL.
+ // JOIN is dual of meet of both duals.
+ public final Type join(Type t) {
+ if( this==t ) return this;
+ return dual().meet(t.dual()).dual();
+ }
+
+ // True if this "isa" t; e.g. 17 isa TypeInteger.BOT
+ public boolean isa( Type t ) { return meet(t)==t; }
+
+ /**
+ * Compute greatest lower bound in the lattice
+ */
+ public Type glb() { return _type==TCTRL ? XCONTROL : BOTTOM; }
+
+ // ----------------------------------------------------------
+ // Useful in the debugger, which calls toString everywhere.
+ // This is a more verbose dev-friendly print.
+ @Override
+ public final String toString() {
+ return _print(new StringBuilder()).toString();
+ }
+
+ public StringBuilder _print(StringBuilder sb) { return is_simple() ? sb.append(STRS[_type]) : sb;}
+
+ // This is used by error messages, and is a shorted print.
+ public String str() { return STRS[_type]; }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/type/TypeInteger.java b/chapter12/src/main/java/com/seaofnodes/simple/type/TypeInteger.java
new file mode 100644
index 00000000..f2a93a3a
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/type/TypeInteger.java
@@ -0,0 +1,101 @@
+package com.seaofnodes.simple.type;
+
+import java.util.ArrayList;
+
+/**
+ * Integer Type
+ */
+public class TypeInteger extends Type {
+
+ public final static TypeInteger TOP = make(false, 0);
+ public final static TypeInteger BOT = make(false, 1);
+ public final static TypeInteger ZERO= make(true , 0);
+
+ public final boolean _is_con;
+
+ /**
+ * The constant value or
+ * if not constant then 1=bottom, 0=top.
+ */
+ public final long _con;
+
+ private TypeInteger(boolean is_con, long con) {
+ super(TINT);
+ _is_con = is_con;
+ _con = con;
+ }
+ public static TypeInteger make(boolean is_con, long con) {
+ return new TypeInteger(is_con,con).intern();
+ }
+
+ public static TypeInteger constant(long con) { return make(true, con); }
+
+ public static void gather(ArrayList<Type> ts) { ts.add(ZERO); ts.add(BOT); }
+
+ // FIXME this display format is problematic
+ // In visualizer '#' gets prepended if its a constant
+ @Override
+ public StringBuilder _print(StringBuilder sb) {
+ if( this==TOP ) return sb.append("IntTop");
+ if( this==BOT ) return sb.append("IntBot");
+ return sb.append(_con);
+ }
+
+ @Override public String str() {
+ if( this==TOP ) return "~int";
+ if( this==BOT ) return "int";
+ return ""+_con;
+ }
+
+ /**
+ * Display Type name in a format that's good for IR printer
+ */
+ @Override
+ public StringBuilder typeName(StringBuilder sb) {
+ if( this==TOP ) return sb.append("IntTop");
+ if( this==BOT ) return sb.append("IntBot");
+ return sb.append("Int");
+ }
+
+ @Override
+ public boolean isHighOrConst() { return _is_con || _con==0; }
+
+ @Override
+ public boolean isConstant() { return _is_con; }
+
+ public long value() { return _con; }
+
+ @Override
+ public Type xmeet(Type other) {
+ // Invariant from caller: 'this' != 'other' and same class (TypeInteger)
+ TypeInteger i = (TypeInteger)other; // Contract
+ // BOT wins
+ if ( this==BOT ) return this;
+ if ( i ==BOT ) return i ;
+ // TOP loses
+ if ( i ==TOP ) return this;
+ if ( this==TOP ) return i ;
+ // Since both are constants, and are never equals (contract) unequals
+ // constants fall to bottom
+ return BOT;
+ }
+
+ @Override
+ public Type dual() {
+ if( isConstant() ) return this; // Constants are a self-dual
+ return _con==0 ? BOT : TOP;
+ }
+
+ @Override
+ public Type glb() { return BOT; }
+
+ @Override public TypeInteger makeInit() { return ZERO; }
+
+ @Override
+ int hash() { return (int)(_con ^ (_is_con ? 0 : 0x4000)); }
+ @Override
+ public boolean eq( Type t ) {
+ TypeInteger i = (TypeInteger)t; // Contract
+ return _con==i._con && _is_con==i._is_con;
+ }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/type/TypeMem.java b/chapter12/src/main/java/com/seaofnodes/simple/type/TypeMem.java
new file mode 100644
index 00000000..198d49ee
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/type/TypeMem.java
@@ -0,0 +1,64 @@
+package com.seaofnodes.simple.type;
+
+import com.seaofnodes.simple.Utils;
+import java.lang.Long;
+import java.util.BitSet;
+import java.util.ArrayList;
+
+
+/**
+ * Represents a slice of memory corresponding to a set of aliases
+ */
+public class TypeMem extends Type {
+
+ // Which slice of memory?
+ // 0 means TOP, no slice.
+ // -1 means BOT, all memory.
+ // N means slice#N.
+ private final int _alias;
+
+ private TypeMem(int alias) { super(TMEM); _alias = alias; }
+
+ public static TypeMem make(int alias) { return new TypeMem(alias).intern(); }
+ public static final TypeMem TOP = make( 0);
+ public static final TypeMem BOT = make(-1);
+
+
+ public static void gather(ArrayList<Type> ts) { ts.add(make(1)); ts.add(BOT); }
+
+ @Override
+ TypeMem xmeet(Type t) {
+ TypeMem that = (TypeMem) t; // Invariant: TypeMem and unequal
+ return _alias==0 ? that : (that._alias==0 ? this : BOT);
+ }
+
+ @Override
+ public Type dual() {
+ if( _alias== 0 ) return BOT;
+ if( _alias==-1 ) return TOP;
+ return this;
+ }
+
+ @Override
+ public Type glb() { return TypeMem.BOT; }
+
+ @Override
+ int hash() { return 9876543 + _alias; }
+
+ @Override
+ boolean eq(Type t) {
+ TypeMem that = (TypeMem) t; // Invariant
+ return _alias == that._alias;
+ }
+
+ @Override
+ public StringBuilder _print(StringBuilder sb) {
+ return sb.append("MEM#").append( switch(_alias) {
+ case 0 -> "TOP";
+ case -1 -> "BOT";
+ default -> ""+_alias;
+ });
+ }
+
+ @Override public String str() { return _print(new StringBuilder()).toString(); }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/type/TypeMemPtr.java b/chapter12/src/main/java/com/seaofnodes/simple/type/TypeMemPtr.java
new file mode 100644
index 00000000..65352402
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/type/TypeMemPtr.java
@@ -0,0 +1,78 @@
+package com.seaofnodes.simple.type;
+
+import java.util.ArrayList;
+
+/**
+ * Represents a Pointer to memory.
+ *
+ * Null is generic pointer to non-existent memory.
+ * *void is a non-Null pointer to all possible structs.
+ * Pointers can be to specific struct types, or a union with Null.
+ * The distinguished *$BOT ptr represents union of *void and Null.
+ * The distinguished *$TOP ptr represents the dual of *$BOT.
+ */
+public class TypeMemPtr extends Type {
+ // A TypeMemPtr is pair (obj,nil)
+ // where obj is one of
+ // (null,TypeStruct).
+ // where nil is one of
+ // (true,false) meaning an explicit null is allowed or not
+
+ public final TypeStruct _obj; // null, a TypeStruct, or sentinel TypeStruct.MANY
+ public final boolean _nil;
+
+ private TypeMemPtr(TypeStruct obj, boolean nil) {
+ super(TMEMPTR);
+ _obj = obj;
+ _nil = nil;
+ }
+ public static TypeMemPtr make(TypeStruct obj, boolean nil) { return new TypeMemPtr(obj, nil).intern(); }
+ public static TypeMemPtr make(TypeStruct obj) { return TypeMemPtr.make(obj, false); }
+
+ public static TypeMemPtr BOT = make(TypeStruct.BOT,true);
+ public static TypeMemPtr TOP = BOT.dual();
+ public static TypeMemPtr NULLPTR = make(TypeStruct.TOP,true);
+ public static TypeMemPtr VOIDPTR = NULLPTR.dual(); // A bottom mix of not-null ptrs, like C's void* but not null
+ public static TypeMemPtr TEST= make(TypeStruct.TEST,false);
+ public static void gather(ArrayList<Type> ts) { ts.add(NULLPTR); ts.add(BOT); ts.add(TEST); }
+
+ @Override
+ Type xmeet(Type t) {
+ TypeMemPtr that = (TypeMemPtr) t;
+ return TypeMemPtr.make((TypeStruct)_obj.meet(that._obj), _nil | that._nil);
+ }
+
+ @Override
+ public TypeMemPtr dual() { return TypeMemPtr.make(_obj==null ? null : _obj.dual(), !_nil); }
+
+ @Override
+ public Type glb() {
+ if( _obj==null ) return BOT;
+ return make(_obj.glb(),true);
+ }
+ @Override public TypeMemPtr makeInit() { return NULLPTR; }
+
+ @Override
+ int hash() { return (_obj==null ? 0xDEADBEEF : _obj.hashCode()) ^ (_nil ? 1024 : 0); }
+
+ @Override
+ boolean eq(Type t) {
+ TypeMemPtr ptr = (TypeMemPtr)t; // Invariant
+ return _obj == ptr._obj && _nil == ptr._nil;
+ }
+
+ // [void,name,MANY]*[,?]
+ @Override
+ public StringBuilder _print(StringBuilder sb) {
+ if( this== NULLPTR) return sb.append("null");
+ if( this== VOIDPTR) return sb.append("*void");
+ return _obj._print(sb.append("*")).append(_nil ? "?" : "");
+ }
+
+ @Override public String str() {
+ if( this== NULLPTR) return "null";
+ if( this== VOIDPTR) return "*void";
+ return "*"+_obj.str()+(_nil ? "?" : "");
+ }
+
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/type/TypeStruct.java b/chapter12/src/main/java/com/seaofnodes/simple/type/TypeStruct.java
new file mode 100644
index 00000000..29102675
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/type/TypeStruct.java
@@ -0,0 +1,121 @@
+package com.seaofnodes.simple.type;
+
+import com.seaofnodes.simple.Utils;
+import java.util.ArrayList;
+
+/**
+ * Represents a struct type.
+ */
+public class TypeStruct extends Type {
+
+ // For now in terms of the lattice
+ // a struct type just stays as is
+
+ public final String _name;
+ public final Field[] _fields;
+
+ private TypeStruct(String name, Field[] fields) {
+ super(Type.TSTRUCT);
+ _name = name;
+ _fields = fields;
+ }
+
+ public static TypeStruct make(String name, Field[] fields) { return new TypeStruct(name, fields).intern(); }
+ public static TypeStruct make(String name, ArrayList<Field> fields) { return make(name,fields.toArray(new Field[fields.size()])); }
+
+ public static final TypeStruct TOP = make("$TOP",new Field [0]);
+ public static final TypeStruct BOT = make("$BOT",new Field [0]);
+ public static final TypeStruct TEST = make("test",new Field[]{Field.TEST});
+ public static void gather(ArrayList<Type> ts) { ts.add(TEST); ts.add(BOT); }
+
+ public int find(String fname) {
+ for( int i=0; i<_fields.length; i++ )
+ if( _fields[i]._fname.equals(fname) )
+ return i;
+ return -1;
+ }
+
+ @Override
+ Type xmeet(Type t) {
+ TypeStruct that = (TypeStruct) t;
+ if( this==TOP ) return that;
+ if( that==TOP ) return this;
+ if( this==BOT ) return BOT;
+ if( that==BOT ) return BOT;
+
+ // Within the same compilation unit, struct names are unique. If the
+ // names differ, its different structs. Across many compilation units,
+ // structs with the same name but different field layouts can be
+ // interned... which begs the question:
+ // "What is the meet of structs from two different compilation units?"
+ // And the answer is: "don't ask".
+ if( !_name.equals(that._name) )
+ return BOT; // It's a struct; that's about all we know
+ // Now all fields should be the same, so just do field meets
+ assert _fields.length == that._fields.length;
+ Field[] flds = new Field[_fields.length];
+ for( int i=0; i<_fields.length; i++ )
+ flds[i] = (Field)_fields[i].meet(that._fields[i]);
+ return make(_name,flds);
+ }
+
+ @Override
+ public TypeStruct dual() {
+ if( this==TOP ) return BOT;
+ if( this==BOT ) return TOP;
+ Field[] flds = new Field[_fields.length];
+ for( int i=0; i<_fields.length; i++ )
+ flds[i] = _fields[i].dual();
+ return make(_name,flds);
+ }
+
+ // Keeps the same struct, but lower-bounds all fields.
+ @Override
+ public TypeStruct glb() {
+ if( _glb() ) return this;
+ // Need to glb each field
+ Field[] flds = new Field[_fields.length];
+ for( int i=0; i<_fields.length; i++ )
+ flds[i] = _fields[i].glb();
+ return make(_name,flds);
+ }
+ private boolean _glb() {
+ for( Field f : _fields )
+ if( f.glb() != f )
+ return false;
+ return true;
+ }
+
+ @Override
+ boolean eq(Type t) {
+ TypeStruct ts = (TypeStruct)t; // Invariant
+ if( !_name.equals(ts._name) )
+ return false;
+ if( _fields.length!=ts._fields.length )
+ return false;
+ for( int i = 0; i < _fields.length; i++ )
+ if( _fields[i] != ts._fields[i] )
+ return false;
+ return true;
+ }
+
+ @Override
+ int hash() {
+ long hash = _name.hashCode();
+ for( Field f : _fields ) hash = Utils.rot(hash,13) ^ f.hashCode();
+ return Utils.fold(hash);
+ }
+
+ @Override
+ public StringBuilder _print(StringBuilder sb) {
+ sb.append(_name).append(" {\n");
+ for( Field f : _fields ) {
+ sb.append(" ").append(f._fname).append(":");
+ f._type._print(sb);
+ sb.append(";\n");
+ }
+ return sb.append("}");
+ }
+
+ @Override public String str() { return _name; }
+}
diff --git a/chapter12/src/main/java/com/seaofnodes/simple/type/TypeTuple.java b/chapter12/src/main/java/com/seaofnodes/simple/type/TypeTuple.java
new file mode 100644
index 00000000..01bcbe9f
--- /dev/null
+++ b/chapter12/src/main/java/com/seaofnodes/simple/type/TypeTuple.java
@@ -0,0 +1,87 @@
+package com.seaofnodes.simple.type;
+
+import java.util.ArrayList;
+
+public class TypeTuple extends Type {
+
+ public final Type[] _types;
+
+ private TypeTuple(Type[] types) { super(TTUPLE); _types = types; }
+ public static TypeTuple make(Type... types) { return new TypeTuple(types).intern(); }
+
+ private static TypeTuple TEST = make(TypeInteger.BOT,TypeMemPtr.TEST);
+ public static void gather(ArrayList<Type> ts) { ts.add(TEST); }
+
+ @Override
+ Type xmeet(Type other) {
+ TypeTuple tt = (TypeTuple)other; // contract from xmeet
+ assert _types.length == tt._types.length;
+ Type[] ts = new Type[_types.length];
+ for( int i=0; i<_types.length; i++ )
+ ts[i] = _types[i].meet(tt._types[i]);
+ return make(ts);
+ }
+
+ @Override public Type dual() {
+ Type[] ts = new Type[_types.length];
+ for( int i=0; i<_types.length; i++ )
+ ts[i] = _types[i].dual();
+ return make(ts);
+ }
+
+ @Override
+ public Type glb() {
+ Type[] ts = new Type[_types.length];
+ for( int i=0; i<_types.length; i++ )
+ ts[i] = _types[i].glb();
+ return make(ts);
+ }
+
+ @Override
+ public StringBuilder _print(StringBuilder sb) {
+ sb.append("[");
+ for( Type t : _types )
+ t._print(sb).append(",");
+ sb.setLength(sb.length()-1);
+ sb.append("]");
+ return sb;
+ }
+
+ @Override public String str() { return _print(new StringBuilder()).toString(); }
+
+ /**
+ * Display Type name in a format that's good for IR printer
+ */
+ @Override
+ public StringBuilder typeName(StringBuilder sb) {
+ sb.append("[");
+ for( Type t : _types )
+ t.typeName(sb).append(",");
+ sb.setLength(sb.length()-1);
+ return sb.append("]");
+ }
+
+ public static final TypeTuple IF_BOTH = make(new Type[]{Type. CONTROL,Type. CONTROL});
+ public static final TypeTuple IF_NEITHER = make(new Type[]{Type.XCONTROL,Type.XCONTROL});
+ public static final TypeTuple IF_TRUE = make(new Type[]{Type. CONTROL,Type.XCONTROL});
+ public static final TypeTuple IF_FALSE = make(new Type[]{Type.XCONTROL,Type. CONTROL});
+
+ @Override
+ int hash() {
+ int sum = 0;
+ for( Type type : _types ) sum ^= type.hashCode();
+ return sum;
+ }
+
+ @Override
+ boolean eq( Type t ) {
+ TypeTuple tt = (TypeTuple)t; // Contract
+ if( _types.length != tt._types.length ) return false;
+ for( int i=0; i<_types.length; i++ )
+ if( _types[i]!=tt._types[i] )
+ return false;
+ return true;
+ }
+
+
+}