summaryrefslogtreecommitdiff
path: root/src/main/java/com/seaofnodes/simple/IRPrinter.java
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-09-29 12:34:58 -0700
committerCliff Click <cliffc@acm.org>2026-09-29 12:34:58 -0700
commit502e7e752f3efdb069a36b23ab51ac22f9d74e9e (patch)
treed2b3c84357be365b5a6cc674285d1d2778c0072c /src/main/java/com/seaofnodes/simple/IRPrinter.java
parentfdfc4e4b824e97952110069558de70cd16c725a1 (diff)
downloadSimple-linear-chapter10a.tar.gz
Simple-linear-chapter10a.zip
Chapter 10a: Structs and Memorylinear-chapter10a
Diffstat (limited to 'src/main/java/com/seaofnodes/simple/IRPrinter.java')
-rw-r--r--src/main/java/com/seaofnodes/simple/IRPrinter.java71
1 files changed, 67 insertions, 4 deletions
diff --git a/src/main/java/com/seaofnodes/simple/IRPrinter.java b/src/main/java/com/seaofnodes/simple/IRPrinter.java
index c06ac9d0..1ac32b1a 100644
--- a/src/main/java/com/seaofnodes/simple/IRPrinter.java
+++ b/src/main/java/com/seaofnodes/simple/IRPrinter.java
@@ -2,14 +2,77 @@ package com.seaofnodes.simple;
import com.seaofnodes.simple.node.LoopNode;
import com.seaofnodes.simple.node.Node;
+import com.seaofnodes.simple.node.ProjNode;
import java.util.ArrayList;
import java.util.BitSet;
public class IRPrinter {
- // Another bulk pretty-printer. Makes more effort at basic-block grouping.
+ // 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.isDead() ) {
+ 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<3; i++ )
+ sb.append(" ");
+ sb.append(" [[ ");
+ for( Node use : n._outputs )
+ sb.append(use==null ? "____ " : "%4d ".formatted(use._nid));
+ int lim = 5 - Math.max(n._inputs.size(),3);
+ 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.isDead() ) {
+ 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 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
@@ -24,16 +87,16 @@ public class IRPrinter {
Node n = rpos.get(i);
if( n.isCFG() || n.isMultiHead() ) {
if( !gap ) sb.append("\n"); // Blank before multihead
- n._printLine(sb); // Print head
+ printLine( n, sb, llvmFormat ); // Print head
while( --i >= 0 ) {
Node t = rpos.get(i);
if( !t.isMultiTail() ) { i++; break; }
- t._printLine(sb);
+ printLine( t, sb, llvmFormat );
}
sb.append("\n"); // Blank after multitail
gap = true;
} else {
- n._printLine( sb );
+ printLine( n, sb, llvmFormat );
gap = false;
}
}