From 502e7e752f3efdb069a36b23ab51ac22f9d74e9e Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Tue, 29 Sep 2026 12:34:58 -0700 Subject: Chapter 10a: Structs and Memory --- src/main/java/com/seaofnodes/simple/IRPrinter.java | 71 ++++++++++++++++++++-- 1 file changed, 67 insertions(+), 4 deletions(-) (limited to 'src/main/java/com/seaofnodes/simple/IRPrinter.java') 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 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; } } -- cgit v1.2.3