Open-source multiplayer game server compatible with the RuneScape client
https://www.openrs2.org/
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.
47 lines
1.1 KiB
47 lines
1.1 KiB
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 && !InsnNodeUtils.hasSideEffects(insn));
|
|
|
|
return false;
|
|
}
|
|
|
|
public static boolean deleteSimpleExpression(InsnList list, AbstractInsnNode last) {
|
|
return replaceSimpleExpression(list, last, null);
|
|
}
|
|
|
|
private InsnListUtils() {
|
|
/* empty */
|
|
}
|
|
}
|
|
|