From 00526587b02a572e3eb75239cf41ffed3329f90a Mon Sep 17 00:00:00 2001 From: Graham Date: Tue, 24 Dec 2019 20:05:43 +0000 Subject: [PATCH] Convert DummyLocalTransformer to Kotlin --- .../deob/transform/DummyLocalTransformer.java | 71 ------------------- .../deob/transform/DummyLocalTransformer.kt | 59 +++++++++++++++ 2 files changed, 59 insertions(+), 71 deletions(-) delete mode 100644 deob/src/main/java/dev/openrs2/deob/transform/DummyLocalTransformer.java create mode 100644 deob/src/main/java/dev/openrs2/deob/transform/DummyLocalTransformer.kt diff --git a/deob/src/main/java/dev/openrs2/deob/transform/DummyLocalTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/DummyLocalTransformer.java deleted file mode 100644 index dcc3b8d3..00000000 --- a/deob/src/main/java/dev/openrs2/deob/transform/DummyLocalTransformer.java +++ /dev/null @@ -1,71 +0,0 @@ -package dev.openrs2.deob.transform; - -import dev.openrs2.asm.InsnListUtilsKt; -import dev.openrs2.asm.classpath.ClassPath; -import dev.openrs2.asm.classpath.Library; -import dev.openrs2.asm.transform.Transformer; -import org.objectweb.asm.Opcodes; -import org.objectweb.asm.tree.ClassNode; -import org.objectweb.asm.tree.MethodNode; -import org.objectweb.asm.tree.VarInsnNode; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public final class DummyLocalTransformer extends Transformer { - private static final Logger logger = LoggerFactory.getLogger(DummyLocalTransformer.class); - - private int localsRemoved; - - @Override - protected void preTransform(ClassPath classPath) { - localsRemoved = 0; - } - - @Override - protected boolean transformCode(ClassPath classPath, Library library, ClassNode clazz, MethodNode method) { - /* - * XXX(gpe): this is primitive (ideally we'd do a proper data flow - * analysis, but we'd need to do it in reverse and ASM only supports - * forward data flow), however, it seems to be good enough to catch - * most dummy locals. - */ - var loads = new boolean[method.maxLocals]; - - for (var it = method.instructions.iterator(); it.hasNext(); ) { - var insn = it.next(); - - var opcode = insn.getOpcode(); - if (opcode != Opcodes.ILOAD) { - continue; - } - - var load = (VarInsnNode) insn; - loads[load.var] = true; - } - - for (var it = method.instructions.iterator(); it.hasNext(); ) { - var insn = it.next(); - - var opcode = insn.getOpcode(); - if (opcode != Opcodes.ISTORE) { - continue; - } - - var store = (VarInsnNode) insn; - if (loads[store.var]) { - continue; - } - - if (InsnListUtilsKt.deleteSimpleExpression(method.instructions, insn)) { - localsRemoved++; - } - } - - return false; - } - - @Override - protected void postTransform(ClassPath classPath) { - logger.info("Removed {} dummy local variables", localsRemoved); - } -} diff --git a/deob/src/main/java/dev/openrs2/deob/transform/DummyLocalTransformer.kt b/deob/src/main/java/dev/openrs2/deob/transform/DummyLocalTransformer.kt new file mode 100644 index 00000000..cdd14db9 --- /dev/null +++ b/deob/src/main/java/dev/openrs2/deob/transform/DummyLocalTransformer.kt @@ -0,0 +1,59 @@ +package dev.openrs2.deob.transform + +import com.github.michaelbull.logging.InlineLogger +import dev.openrs2.asm.classpath.ClassPath +import dev.openrs2.asm.classpath.Library +import dev.openrs2.asm.deleteSimpleExpression +import dev.openrs2.asm.transform.Transformer +import org.objectweb.asm.Opcodes +import org.objectweb.asm.tree.ClassNode +import org.objectweb.asm.tree.MethodNode +import org.objectweb.asm.tree.VarInsnNode + +class DummyLocalTransformer : Transformer() { + private var localsRemoved = 0 + + override fun preTransform(classPath: ClassPath) { + localsRemoved = 0 + } + + override fun transformCode(classPath: ClassPath, library: Library, clazz: ClassNode, method: MethodNode): Boolean { + /* + * XXX(gpe): this is primitive (ideally we'd do a proper data flow + * analysis, but we'd need to do it in reverse and ASM only supports + * forward data flow), however, it seems to be good enough to catch + * most dummy locals. + */ + val loads = BooleanArray(method.maxLocals) + + for (insn in method.instructions) { + if (insn is VarInsnNode && insn.opcode == Opcodes.ILOAD) { + loads[insn.`var`] = true + } + } + + for (insn in method.instructions) { + if (insn !is VarInsnNode || insn.opcode != Opcodes.ISTORE) { + continue + } + + if (loads[insn.`var`]) { + continue + } + + if (method.instructions.deleteSimpleExpression(insn)) { + localsRemoved++ + } + } + + return false + } + + override fun postTransform(classPath: ClassPath) { + logger.info { "Removed $localsRemoved dummy local variables" } + } + + companion object { + private val logger = InlineLogger() + } +}