From a9312f3cd27426d518c480d2634755e9d6b3b066 Mon Sep 17 00:00:00 2001 From: Major Date: Sat, 7 Mar 2020 11:41:45 +0000 Subject: [PATCH] Remove empty try-catch blocks in DCE Signed-off-by: Major --- asm/src/main/java/dev/openrs2/asm/InsnNodeUtils.kt | 14 ++++++++++++++ .../main/java/dev/openrs2/asm/MethodNodeUtils.kt | 4 +++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/asm/src/main/java/dev/openrs2/asm/InsnNodeUtils.kt b/asm/src/main/java/dev/openrs2/asm/InsnNodeUtils.kt index 9b62685a..ddc3102f 100644 --- a/asm/src/main/java/dev/openrs2/asm/InsnNodeUtils.kt +++ b/asm/src/main/java/dev/openrs2/asm/InsnNodeUtils.kt @@ -9,6 +9,7 @@ import org.objectweb.asm.tree.LabelNode import org.objectweb.asm.tree.LdcInsnNode import org.objectweb.asm.tree.LookupSwitchInsnNode import org.objectweb.asm.tree.TableSwitchInsnNode +import org.objectweb.asm.tree.TryCatchBlockNode import org.objectweb.asm.util.Textifier import org.objectweb.asm.util.TraceMethodVisitor import java.io.PrintWriter @@ -306,3 +307,16 @@ fun AbstractInsnNode.toPrettyString(): String { } } } + +fun TryCatchBlockNode.isBodyEmpty(): Boolean { + var current = start.next + + while (true) { + when { + current == null -> return false + current === end -> return true + current.opcode != -1 -> return false + else -> current = current.next + } + } +} diff --git a/asm/src/main/java/dev/openrs2/asm/MethodNodeUtils.kt b/asm/src/main/java/dev/openrs2/asm/MethodNodeUtils.kt index 8cc4c4c0..b02922b8 100644 --- a/asm/src/main/java/dev/openrs2/asm/MethodNodeUtils.kt +++ b/asm/src/main/java/dev/openrs2/asm/MethodNodeUtils.kt @@ -125,7 +125,9 @@ fun MethodNode.removeDeadCode(owner: String) { } } - changed = changed or tryCatchBlocks.removeIf { it.start in deadLabels && it.end in deadLabels } + changed = changed or tryCatchBlocks.removeIf { + it.start in deadLabels && it.end in deadLabels || it.isBodyEmpty() + } } while (changed) }