code for graph building is awful but works

This commit is contained in:
Claudio Maggioni 2023-10-10 17:13:02 +02:00
parent d19481edca
commit 8b36443331

View file

@ -97,7 +97,7 @@ public class ControlFlowGraphBuilder {
labels.add((LabelNode) instruction); labels.add((LabelNode) instruction);
} }
if (isBranchingInstruction(type, isPointedLabel)) { if (isEndOfBlock(instruction, labelToIncomingEdges)) {
final int nextI = i + 1; final int nextI = i + 1;
// if we're not at the end // if we're not at the end
@ -137,11 +137,21 @@ public class ControlFlowGraphBuilder {
|| opcode == Opcodes.FRETURN; || opcode == Opcodes.FRETURN;
} }
private static boolean isBranchingInstruction(int type, boolean isPointedLabel) { private static boolean isEndOfBlock(AbstractInsnNode instruction, MultiMap<LabelNode, Edge> labelToInstruction) {
return type == AbstractInsnNode.JUMP_INSN final AbstractInsnNode nextInsn = instruction.getNext();
|| type == AbstractInsnNode.LOOKUPSWITCH_INSN if (nextInsn == null) {
|| type == AbstractInsnNode.TABLESWITCH_INSN return false; // cannot start another bb at the end of the method with 0 instructions
|| isPointedLabel; }
final int type = instruction.getType();
if (type == AbstractInsnNode.JUMP_INSN
|| type == AbstractInsnNode.LOOKUPSWITCH_INSN
|| type == AbstractInsnNode.TABLESWITCH_INSN) {
return true; // if we're branching or jumping after this instruction, we NEED to cut the current bb short
}
// if the next instruction is a label some other bb may jump into then cut, otherwise continue
return nextInsn instanceof LabelNode && labelToInstruction.containsKey((LabelNode) nextInsn);
} }
private record Edge(AbstractInsnNode instruction, String condition) { private record Edge(AbstractInsnNode instruction, String condition) {