forked from openrs2/openrs2
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
39 lines
1015 B
39 lines
1015 B
package dev.openrs2.asm
|
|
|
|
import org.objectweb.asm.tree.AbstractInsnNode
|
|
import org.objectweb.asm.tree.InsnList
|
|
|
|
fun InsnList.replaceSimpleExpression(last: AbstractInsnNode, replacement: AbstractInsnNode?): Boolean {
|
|
val deadInsns = mutableListOf<AbstractInsnNode>()
|
|
|
|
var height = 0
|
|
var insn: AbstractInsnNode? = last
|
|
do {
|
|
val (pops, pushes) = insn!!.stackMetadata()
|
|
if (insn !== last) {
|
|
deadInsns.add(insn)
|
|
height -= pushes
|
|
}
|
|
height += pops
|
|
|
|
if (height == 0) {
|
|
deadInsns.forEach(this::remove)
|
|
|
|
if (replacement != null) {
|
|
this[last] = replacement
|
|
} else {
|
|
remove(last)
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
insn = insn.previous
|
|
} while (insn != null && insn.type != AbstractInsnNode.LABEL && insn.pure)
|
|
|
|
return false
|
|
}
|
|
|
|
fun InsnList.deleteSimpleExpression(last: AbstractInsnNode): Boolean {
|
|
return replaceSimpleExpression(last, null)
|
|
}
|
|
|