Add getSimpleExpression method

bzip2
Graham 5 years ago
parent 5d813a345d
commit e0d6390f87
  1. 30
      asm/src/main/java/dev/openrs2/asm/InsnListUtils.kt

@ -3,37 +3,39 @@ package dev.openrs2.asm
import org.objectweb.asm.tree.AbstractInsnNode import org.objectweb.asm.tree.AbstractInsnNode
import org.objectweb.asm.tree.InsnList import org.objectweb.asm.tree.InsnList
fun InsnList.replaceSimpleExpression(last: AbstractInsnNode, replacement: AbstractInsnNode?): Boolean { fun getSimpleExpression(last: AbstractInsnNode): List<AbstractInsnNode>? {
val deadInsns = mutableListOf<AbstractInsnNode>() val expr = mutableListOf<AbstractInsnNode>()
var height = 0 var height = 0
var insn: AbstractInsnNode? = last var insn: AbstractInsnNode? = last
do { do {
val (pops, pushes) = insn!!.stackMetadata() val (pops, pushes) = insn!!.stackMetadata()
if (insn !== last) { if (insn !== last) {
deadInsns.add(insn) expr.add(insn)
height -= pushes height -= pushes
} }
height += pops height += pops
if (height == 0) { if (height == 0) {
deadInsns.forEach(this::remove) return expr.asReversed()
if (replacement != null) {
this[last] = replacement
} else {
remove(last)
}
return true
} }
insn = insn.previous insn = insn.previous
} while (insn != null && insn.type != AbstractInsnNode.LABEL && insn.pure) } while (insn != null && insn.type != AbstractInsnNode.LABEL && insn.pure)
return false return null
}
fun InsnList.replaceSimpleExpression(last: AbstractInsnNode, replacement: AbstractInsnNode): Boolean {
val expr = getSimpleExpression(last) ?: return false
expr.forEach(this::remove)
this[last] = replacement
return true
} }
fun InsnList.deleteSimpleExpression(last: AbstractInsnNode): Boolean { fun InsnList.deleteSimpleExpression(last: AbstractInsnNode): Boolean {
return replaceSimpleExpression(last, null) val expr = getSimpleExpression(last) ?: return false
expr.forEach(this::remove)
remove(last)
return true
} }

Loading…
Cancel
Save