First flowgraph?

This commit is contained in:
ChUrl
2021-01-28 11:49:15 +01:00
parent a027e449cf
commit fd483811b9
5 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,39 @@
package codegen.analysis.flowgraph;
import java.util.List;
import java.util.stream.Collectors;
public class FlowGraph {
private final FlowGraphHead head;
private final List<FlowGraphBlock> blocks;
private final FlowGraphTail tail;
public FlowGraph(FlowGraphHead head, List<FlowGraphBlock> blocks, FlowGraphTail tail) {
this.head = head;
this.blocks = blocks;
this.tail = tail;
}
public void print() {
final String blocksString = this.blocks.stream()
.map(FlowGraphBlock::toString)
.map(string -> string + "-".repeat(50))
.collect(Collectors.joining());
System.out.println(this.head + "-".repeat(100)
+ blocksString + "-".repeat(100)
+ this.tail);
}
@Override
public String toString() {
final String blocksString = this.blocks.stream()
.map(FlowGraphBlock::toString)
.collect(Collectors.joining());
return this.head
+ blocksString
+ this.tail;
}
}

View File

@ -0,0 +1,25 @@
package codegen.analysis.flowgraph;
import java.util.List;
import java.util.stream.Collectors;
public class FlowGraphBlock {
private final String label;
private final List<FlowGraphLine> lines;
public FlowGraphBlock(String label, List<FlowGraphLine> lines) {
this.label = label;
this.lines = lines;
}
@Override
public String toString() {
final String linesString = this.lines.stream()
.map(FlowGraphLine::toString)
.collect(Collectors.joining());
return this.label + ":\n"
+ linesString;
}
}

View File

@ -0,0 +1,37 @@
package codegen.analysis.flowgraph;
public class FlowGraphHead {
private final String bytecodeVersion;
private final String source;
private final String clazz;
private final int stackSize;
private final int localCount;
public FlowGraphHead(String bytecodeVersion, String source, String clazz, int stackSize, int localCount) {
this.bytecodeVersion = bytecodeVersion;
this.source = source;
this.clazz = clazz;
this.stackSize = stackSize;
this.localCount = localCount;
}
@Override
public String toString() {
return ".bytecode " + this.bytecodeVersion + "\n"
+ ".source " + this.source + "\n"
+ ".class public" + this.clazz + "\n"
+ ".super java/lang/Object\n"
+ ".method public <init>()V\n"
+ "\t.limit stack 1\n"
+ "\t.limit locals 1\n"
+ "\t\taload_0\n"
+ "\t\tinvokespecial java/lang/Object/<init>()V\n"
+ "\t\treturn\n"
+ ".end method\n\n"
+ ".method public static main([Ljava/lang/String;)V\n"
+ "\t.limit stack " + this.stackSize + "\n"
+ "\t.limit locals " + this.localCount + "\n";
}
}

View File

@ -0,0 +1,19 @@
package codegen.analysis.flowgraph;
public class FlowGraphLine {
private final String instruction;
private final String[] args;
public FlowGraphLine(String instruction, String... args) {
this.instruction = instruction;
this.args = args;
}
@Override
public String toString() {
final String argsString = String.join(" ", this.args);
return "\t\t" + this.instruction + " " + argsString;
}
}

View File

@ -0,0 +1,10 @@
package codegen.analysis.flowgraph;
public class FlowGraphTail {
@Override
public String toString() {
return "\t\treturn\n"
+ ".end method";
}
}