Simplify removeDeadCode()

I think relying on just the isBodyEmpty() check is more correct too, as
the end label of a try/catch is exclusive. Furthermore, we don't want to
delete try/catch nodes where the middle of the body is not dead (even if
the start and end are dead).

Signed-off-by: Graham <gpe@openrs2.dev>
Graham 5 years ago
parent 5c1e630cb8
commit 0395224000
  1. 11
      asm/src/main/java/dev/openrs2/asm/MethodNodeUtils.kt

@ -108,25 +108,18 @@ fun MethodNode.removeDeadCode(owner: String) {
val analyzer = Analyzer(BasicInterpreter()) val analyzer = Analyzer(BasicInterpreter())
val frames = analyzer.analyze(owner, this) val frames = analyzer.analyze(owner, this)
val deadLabels = mutableSetOf<LabelNode>()
val it = instructions.iterator() val it = instructions.iterator()
var i = 0 var i = 0
for (insn in it) { for (insn in it) {
if (frames[i++] != null) { if (frames[i++] != null || insn is LabelNode) {
continue continue
} }
if (insn is LabelNode) {
deadLabels.add(insn)
} else {
it.remove() it.remove()
changed = true changed = true
} }
}
changed = changed or tryCatchBlocks.removeIf { changed = changed or tryCatchBlocks.removeIf { it.isBodyEmpty() }
it.start in deadLabels && it.end in deadLabels || it.isBodyEmpty()
}
} while (changed) } while (changed)
} }

Loading…
Cancel
Save