From 3e0e7824e0dcb3a6c178748b065b4881a9b51222 Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 23 Dec 2019 19:50:58 +0000 Subject: [PATCH] Convert InsnListUtils to Kotlin --- .../java/dev/openrs2/asm/InsnListUtils.java | 47 ------------------- .../java/dev/openrs2/asm/InsnListUtils.kt | 39 +++++++++++++++ .../deob/transform/DummyArgTransformer.java | 8 ++-- .../deob/transform/DummyLocalTransformer.java | 4 +- 4 files changed, 45 insertions(+), 53 deletions(-) delete mode 100644 asm/src/main/java/dev/openrs2/asm/InsnListUtils.java create mode 100644 asm/src/main/java/dev/openrs2/asm/InsnListUtils.kt diff --git a/asm/src/main/java/dev/openrs2/asm/InsnListUtils.java b/asm/src/main/java/dev/openrs2/asm/InsnListUtils.java deleted file mode 100644 index 379c2251e5..0000000000 --- a/asm/src/main/java/dev/openrs2/asm/InsnListUtils.java +++ /dev/null @@ -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(); - - 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 */ - } -} diff --git a/asm/src/main/java/dev/openrs2/asm/InsnListUtils.kt b/asm/src/main/java/dev/openrs2/asm/InsnListUtils.kt new file mode 100644 index 0000000000..5de8301869 --- /dev/null +++ b/asm/src/main/java/dev/openrs2/asm/InsnListUtils.kt @@ -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() + + 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) +} 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 d45932ff09..06cafec5ad 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.java @@ -11,7 +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.InsnListUtilsKt; import dev.openrs2.asm.InsnMatcher; import dev.openrs2.asm.InsnNodeUtilsKt; import dev.openrs2.asm.MemberRef; @@ -414,14 +414,14 @@ public final class DummyArgTransformer extends Transformer { } for (var insn : alwaysTakenBranches) { - if (InsnListUtils.replaceSimpleExpression(method.instructions, insn, new JumpInsnNode(Opcodes.GOTO, insn.label))) { + if (InsnListUtilsKt.replaceSimpleExpression(method.instructions, insn, new JumpInsnNode(Opcodes.GOTO, insn.label))) { branchesSimplified++; changed = true; } } for (var insn : neverTakenBranches) { - if (InsnListUtils.deleteSimpleExpression(method.instructions, insn)) { + if (InsnListUtilsKt.deleteSimpleExpression(method.instructions, insn)) { branchesSimplified++; changed = true; } @@ -434,7 +434,7 @@ public final class DummyArgTransformer extends Transformer { } var replacement = InsnNodeUtilsKt.createIntConstant(entry.getValue()); - if (InsnListUtils.replaceSimpleExpression(method.instructions, insn, replacement)) { + if (InsnListUtilsKt.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 1513c94afb..dcc3b8d3a3 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.InsnListUtils; +import dev.openrs2.asm.InsnListUtilsKt; 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 (InsnListUtils.deleteSimpleExpression(method.instructions, insn)) { + if (InsnListUtilsKt.deleteSimpleExpression(method.instructions, insn)) { localsRemoved++; } }