Move InsnList-related methods to InsnListUtils

master
Graham 5 years ago
parent db785e38d0
commit 8b57525c88
  1. 47
      asm/src/main/java/dev/openrs2/asm/InsnListUtils.java
  2. 37
      asm/src/main/java/dev/openrs2/asm/InsnNodeUtils.java
  3. 7
      deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.java
  4. 4
      deob/src/main/java/dev/openrs2/deob/transform/DummyLocalTransformer.java

@ -0,0 +1,47 @@
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 */
}
}

@ -4,11 +4,9 @@ import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.io.UncheckedIOException; import java.io.UncheckedIOException;
import java.util.ArrayList;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode; import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.InsnList;
import org.objectweb.asm.tree.InsnNode; import org.objectweb.asm.tree.InsnNode;
import org.objectweb.asm.tree.IntInsnNode; import org.objectweb.asm.tree.IntInsnNode;
import org.objectweb.asm.tree.LdcInsnNode; import org.objectweb.asm.tree.LdcInsnNode;
@ -314,41 +312,6 @@ public final class InsnNodeUtils {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
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 && !hasSideEffects(insn));
return false;
}
public static boolean deleteSimpleExpression(InsnList list, AbstractInsnNode last) {
return replaceSimpleExpression(list, last, null);
}
public static String toString(AbstractInsnNode insn) { public static String toString(AbstractInsnNode insn) {
var printer = new Textifier(); var printer = new Textifier();

@ -11,6 +11,7 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.HashMultimap; import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import dev.openrs2.asm.InsnListUtils;
import dev.openrs2.asm.InsnMatcher; import dev.openrs2.asm.InsnMatcher;
import dev.openrs2.asm.InsnNodeUtils; import dev.openrs2.asm.InsnNodeUtils;
import dev.openrs2.asm.MemberRef; import dev.openrs2.asm.MemberRef;
@ -413,14 +414,14 @@ public final class DummyArgTransformer extends Transformer {
} }
for (var insn : alwaysTakenBranches) { for (var insn : alwaysTakenBranches) {
if (InsnNodeUtils.replaceSimpleExpression(method.instructions, insn, new JumpInsnNode(Opcodes.GOTO, insn.label))) { if (InsnListUtils.replaceSimpleExpression(method.instructions, insn, new JumpInsnNode(Opcodes.GOTO, insn.label))) {
branchesSimplified++; branchesSimplified++;
changed = true; changed = true;
} }
} }
for (var insn : neverTakenBranches) { for (var insn : neverTakenBranches) {
if (InsnNodeUtils.deleteSimpleExpression(method.instructions, insn)) { if (InsnListUtils.deleteSimpleExpression(method.instructions, insn)) {
branchesSimplified++; branchesSimplified++;
changed = true; changed = true;
} }
@ -433,7 +434,7 @@ public final class DummyArgTransformer extends Transformer {
} }
var replacement = InsnNodeUtils.createIntConstant(entry.getValue()); var replacement = InsnNodeUtils.createIntConstant(entry.getValue());
if (InsnNodeUtils.replaceSimpleExpression(method.instructions, insn, replacement)) { if (InsnListUtils.replaceSimpleExpression(method.instructions, insn, replacement)) {
constantsInlined++; constantsInlined++;
changed = true; changed = true;
} }

@ -1,6 +1,6 @@
package dev.openrs2.deob.transform; package dev.openrs2.deob.transform;
import dev.openrs2.asm.InsnNodeUtils; import dev.openrs2.asm.InsnListUtils;
import dev.openrs2.asm.classpath.ClassPath; import dev.openrs2.asm.classpath.ClassPath;
import dev.openrs2.asm.classpath.Library; import dev.openrs2.asm.classpath.Library;
import dev.openrs2.asm.transform.Transformer; import dev.openrs2.asm.transform.Transformer;
@ -56,7 +56,7 @@ public final class DummyLocalTransformer extends Transformer {
continue; continue;
} }
if (InsnNodeUtils.deleteSimpleExpression(method.instructions, insn)) { if (InsnListUtils.deleteSimpleExpression(method.instructions, insn)) {
localsRemoved++; localsRemoved++;
} }
} }

Loading…
Cancel
Save