Skip to content

Commit a956288

Browse files
Dot output
1 parent e839091 commit a956288

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/BasicBlock.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,28 @@ public static StringBuilder toStr(StringBuilder sb, BasicBlock bb, BitSet visite
164164
}
165165
return sb;
166166
}
167+
private String escapeHtmlChars(String s) {
168+
if (s.contains(">"))
169+
s = s.replaceAll(">", " > ");
170+
if (s.contains(">="))
171+
s = s.replaceAll(">=", " ≥ ");
172+
if (s.contains("<"))
173+
s = s.replaceAll("<", " &lt; ");
174+
if (s.contains("<="))
175+
s = s.replaceAll("<=", " &le; ");
176+
return s;
177+
}
178+
public StringBuilder toDot(StringBuilder sb, boolean verbose) {
179+
sb.append("<TABLE BORDER=\"1\" CELLBORDER=\"0\">\n");
180+
sb.append("<TR><TD><B>L").append(bid).append("</B></TD></TR>\n");
181+
for (Instruction i: instructions) {
182+
sb.append("<TR><TD>");
183+
sb.append(escapeHtmlChars(i.toStr(new StringBuilder()).toString()));
184+
sb.append("</TD></TR>\n");
185+
}
186+
sb.append("</TABLE>");
187+
return sb;
188+
}
167189
@Override
168190
public boolean equals(Object o) {
169191
if (this == o) return true;

optvm/src/main/java/com/compilerprogramming/ezlang/compiler/CompiledFunction.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class CompiledFunction {
1414

1515
public BasicBlock entry;
1616
public BasicBlock exit;
17-
private int bid = 0;
17+
private int BID = 0;
1818
public BasicBlock currentBlock;
1919
private BasicBlock currentBreakTarget;
2020
private BasicBlock currentContinueTarget;
@@ -41,7 +41,7 @@ public CompiledFunction(Symbol.FunctionTypeSymbol functionSymbol) {
4141
this.functionType = (Type.TypeFunction) functionSymbol.type;
4242
this.registerPool = new RegisterPool();
4343
setVirtualRegisters(funcDecl.scope);
44-
this.bid = 0;
44+
this.BID = 0;
4545
this.entry = this.currentBlock = createBlock();
4646
this.exit = createBlock();
4747
this.currentBreakTarget = null;
@@ -55,7 +55,7 @@ public CompiledFunction(Symbol.FunctionTypeSymbol functionSymbol) {
5555
public CompiledFunction(Type.TypeFunction functionType) {
5656
this.functionType = (Type.TypeFunction) functionType;
5757
this.registerPool = new RegisterPool();
58-
this.bid = 0;
58+
this.BID = 0;
5959
this.entry = this.currentBlock = createBlock();
6060
this.exit = createBlock();
6161
this.currentBreakTarget = null;
@@ -99,11 +99,11 @@ private void setVirtualRegisters(Scope scope) {
9999
}
100100

101101
public BasicBlock createBlock() {
102-
return new BasicBlock(bid++);
102+
return new BasicBlock(BID++);
103103
}
104104

105105
private BasicBlock createLoopHead() {
106-
return new BasicBlock(bid++, true);
106+
return new BasicBlock(BID++, true);
107107
}
108108

109109
private void compileBlock(AST.BlockStmt block) {
@@ -574,4 +574,19 @@ public void livenessAnalysis() {
574574
public List<BasicBlock> getBlocks() {
575575
return BBHelper.findAllBlocks(entry);
576576
}
577+
578+
public StringBuilder toDot(StringBuilder sb, boolean verbose) {
579+
sb.append("digraph CompiledFunction {\n");
580+
List<BasicBlock> blocks = getBlocks();
581+
for (BasicBlock block: blocks) {
582+
sb.append("L").append(block.bid).append(" [shape=none, margin=0, label=<");
583+
block.toDot(sb, verbose);
584+
sb.append(">];\n");
585+
for (BasicBlock s: block.successors) {
586+
sb.append("L").append(block.bid).append(" -> ").append("L").append(s.bid).append("\n");
587+
}
588+
}
589+
sb.append("}\n");
590+
return sb;
591+
}
577592
}

0 commit comments

Comments
 (0)