From 8b36443331fb568dac9a40783d0605a97e89ef05 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Tue, 10 Oct 2023 17:13:02 +0200 Subject: [PATCH] code for graph building is awful but works --- .../inf/sp/cfg/ControlFlowGraphBuilder.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/ch/usi/inf/sp/cfg/ControlFlowGraphBuilder.java b/src/ch/usi/inf/sp/cfg/ControlFlowGraphBuilder.java index f78a2c8..718f208 100644 --- a/src/ch/usi/inf/sp/cfg/ControlFlowGraphBuilder.java +++ b/src/ch/usi/inf/sp/cfg/ControlFlowGraphBuilder.java @@ -97,7 +97,7 @@ public class ControlFlowGraphBuilder { labels.add((LabelNode) instruction); } - if (isBranchingInstruction(type, isPointedLabel)) { + if (isEndOfBlock(instruction, labelToIncomingEdges)) { final int nextI = i + 1; // if we're not at the end @@ -137,11 +137,21 @@ public class ControlFlowGraphBuilder { || opcode == Opcodes.FRETURN; } - private static boolean isBranchingInstruction(int type, boolean isPointedLabel) { - return type == AbstractInsnNode.JUMP_INSN - || type == AbstractInsnNode.LOOKUPSWITCH_INSN - || type == AbstractInsnNode.TABLESWITCH_INSN - || isPointedLabel; + private static boolean isEndOfBlock(AbstractInsnNode instruction, MultiMap labelToInstruction) { + final AbstractInsnNode nextInsn = instruction.getNext(); + if (nextInsn == null) { + return false; // cannot start another bb at the end of the method with 0 instructions + } + + 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) {