From 8b57525c88a8d7258370037e3839497974ed39f3 Mon Sep 17 00:00:00 2001 From: Graham Date: Sun, 22 Dec 2019 17:21:05 +0000 Subject: [PATCH] Move InsnList-related methods to InsnListUtils --- .../java/dev/openrs2/asm/InsnListUtils.java | 47 +++++++++++++++++++ .../java/dev/openrs2/asm/InsnNodeUtils.java | 37 --------------- .../deob/transform/DummyArgTransformer.java | 7 +-- .../deob/transform/DummyLocalTransformer.java | 4 +- 4 files changed, 53 insertions(+), 42 deletions(-) create mode 100644 asm/src/main/java/dev/openrs2/asm/InsnListUtils.java diff --git a/asm/src/main/java/dev/openrs2/asm/InsnListUtils.java b/asm/src/main/java/dev/openrs2/asm/InsnListUtils.java new file mode 100644 index 0000000000..db68fa6fd9 --- /dev/null +++ b/asm/src/main/java/dev/openrs2/asm/InsnListUtils.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(); + + 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 */ + } +} diff --git a/asm/src/main/java/dev/openrs2/asm/InsnNodeUtils.java b/asm/src/main/java/dev/openrs2/asm/InsnNodeUtils.java index 0712370d45..55dc463b9e 100644 --- a/asm/src/main/java/dev/openrs2/asm/InsnNodeUtils.java +++ b/asm/src/main/java/dev/openrs2/asm/InsnNodeUtils.java @@ -4,11 +4,9 @@ import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.io.UncheckedIOException; -import java.util.ArrayList; import org.objectweb.asm.Opcodes; import org.objectweb.asm.tree.AbstractInsnNode; -import org.objectweb.asm.tree.InsnList; import org.objectweb.asm.tree.InsnNode; import org.objectweb.asm.tree.IntInsnNode; import org.objectweb.asm.tree.LdcInsnNode; @@ -314,41 +312,6 @@ public final class InsnNodeUtils { throw new IllegalArgumentException(); } - public static boolean replaceSimpleExpression(InsnList list, AbstractInsnNode last, AbstractInsnNode replacement) { - var deadInsns = new ArrayList(); - - 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) { var printer = new Textifier(); diff --git a/deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.java index 7a37b7cf19..0e3144d003 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.java @@ -11,6 +11,7 @@ import com.google.common.base.Preconditions; import com.google.common.collect.HashMultimap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; +import dev.openrs2.asm.InsnListUtils; import dev.openrs2.asm.InsnMatcher; import dev.openrs2.asm.InsnNodeUtils; import dev.openrs2.asm.MemberRef; @@ -413,14 +414,14 @@ public final class DummyArgTransformer extends Transformer { } 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++; changed = true; } } for (var insn : neverTakenBranches) { - if (InsnNodeUtils.deleteSimpleExpression(method.instructions, insn)) { + if (InsnListUtils.deleteSimpleExpression(method.instructions, insn)) { branchesSimplified++; changed = true; } @@ -433,7 +434,7 @@ public final class DummyArgTransformer extends Transformer { } var replacement = InsnNodeUtils.createIntConstant(entry.getValue()); - if (InsnNodeUtils.replaceSimpleExpression(method.instructions, insn, replacement)) { + if (InsnListUtils.replaceSimpleExpression(method.instructions, insn, replacement)) { constantsInlined++; changed = true; } diff --git a/deob/src/main/java/dev/openrs2/deob/transform/DummyLocalTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/DummyLocalTransformer.java index 1b17c43490..1513c94afb 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/DummyLocalTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/DummyLocalTransformer.java @@ -1,6 +1,6 @@ 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.Library; import dev.openrs2.asm.transform.Transformer; @@ -56,7 +56,7 @@ public final class DummyLocalTransformer extends Transformer { continue; } - if (InsnNodeUtils.deleteSimpleExpression(method.instructions, insn)) { + if (InsnListUtils.deleteSimpleExpression(method.instructions, insn)) { localsRemoved++; } }