forked from openrs2/openrs2
parent
8020ac98ab
commit
3e0e7824e0
@ -1,47 +0,0 @@ |
||||
package dev.openrs2.asm; |
||||
|
||||
import java.util.ArrayList; |
||||
|
||||
import org.objectweb.asm.tree.AbstractInsnNode; |
||||
import org.objectweb.asm.tree.InsnList; |
||||
|
||||
public final class InsnListUtils { |
||||
public static boolean replaceSimpleExpression(InsnList list, AbstractInsnNode last, AbstractInsnNode replacement) { |
||||
var deadInsns = new ArrayList<AbstractInsnNode>(); |
||||
|
||||
var height = 0; |
||||
var insn = last; |
||||
do { |
||||
var metadata = StackMetadataKt.stackMetadata(insn); |
||||
if (insn != last) { |
||||
deadInsns.add(insn); |
||||
height -= metadata.getPushes(); |
||||
} |
||||
height += metadata.getPops(); |
||||
|
||||
if (height == 0) { |
||||
deadInsns.forEach(list::remove); |
||||
|
||||
if (replacement != null) { |
||||
list.set(last, replacement); |
||||
} else { |
||||
list.remove(last); |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
|
||||
insn = insn.getPrevious(); |
||||
} while (insn != null && insn.getType() != AbstractInsnNode.LABEL && InsnNodeUtilsKt.getPure(insn)); |
||||
|
||||
return false; |
||||
} |
||||
|
||||
public static boolean deleteSimpleExpression(InsnList list, AbstractInsnNode last) { |
||||
return replaceSimpleExpression(list, last, null); |
||||
} |
||||
|
||||
private InsnListUtils() { |
||||
/* empty */ |
||||
} |
||||
} |
@ -0,0 +1,39 @@ |
||||
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) |
||||
} |
Loading…
Reference in new issue