From 77d3489b2389116cad91151e486f893449fa2bde Mon Sep 17 00:00:00 2001 From: Graham Date: Mon, 23 Dec 2019 11:10:35 +0000 Subject: [PATCH] Convert BitShiftTransformer to Kotlin --- .../deob/transform/BitShiftTransformer.java | 50 ----------------- .../deob/transform/BitShiftTransformer.kt | 53 +++++++++++++++++++ 2 files changed, 53 insertions(+), 50 deletions(-) delete mode 100644 deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.java create mode 100644 deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.kt diff --git a/deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.java deleted file mode 100644 index 55ba5100ba..0000000000 --- a/deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.java +++ /dev/null @@ -1,50 +0,0 @@ -package dev.openrs2.deob.transform; - -import com.google.common.collect.ImmutableSet; -import dev.openrs2.asm.InsnMatcher; -import dev.openrs2.asm.InsnNodeUtils; -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.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public final class BitShiftTransformer extends Transformer { - private static final Logger logger = LoggerFactory.getLogger(BitShiftTransformer.class); - - private static final InsnMatcher CONST_SHIFT_MATCHER = InsnMatcher.compile("(ICONST | BIPUSH | SIPUSH | LDC) (ISHL | ISHR | IUSHR | LSHL | LSHR | LUSHR)"); - private static final ImmutableSet LONG_SHIFTS = ImmutableSet.of(Opcodes.LSHL, Opcodes.LSHR, Opcodes.LUSHR); - - private int simplified; - - @Override - public void preTransform(ClassPath classPath) { - simplified = 0; - } - - @Override - public boolean transformCode(ClassPath classPath, Library library, ClassNode clazz, MethodNode method) { - CONST_SHIFT_MATCHER.match(method).forEach(match -> { - var push = match.get(0); - var bits = InsnNodeUtils.getIntConstant(push); - - var opcode = match.get(1).getOpcode(); - var simplifiedBits = bits & (LONG_SHIFTS.contains(opcode) ? 63 : 31); - - if (bits != simplifiedBits) { - method.instructions.set(push, InsnNodeUtils.createIntConstant(simplifiedBits)); - simplified++; - } - }); - - return false; - } - - @Override - public void postTransform(ClassPath classPath) { - logger.info("Simplified {} bit shifts", simplified); - } -} diff --git a/deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.kt b/deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.kt new file mode 100644 index 0000000000..1dd91b6950 --- /dev/null +++ b/deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.kt @@ -0,0 +1,53 @@ +package dev.openrs2.deob.transform + +import com.github.michaelbull.logging.InlineLogger +import dev.openrs2.asm.InsnMatcher +import dev.openrs2.asm.InsnNodeUtils +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 + +class BitShiftTransformer : Transformer() { + private var simplified = 0 + + public override fun preTransform(classPath: ClassPath) { + simplified = 0 + } + + public override fun transformCode( + classPath: ClassPath, + library: Library, + clazz: ClassNode, + method: MethodNode + ): Boolean { + CONST_SHIFT_MATCHER.match(method).forEach { + val push = it[0] + val bits = InsnNodeUtils.getIntConstant(push) + + val opcode = it[1].opcode + val mask = if (LONG_SHIFTS.contains(opcode)) 63 else 31 + + val simplifiedBits = bits and mask + + if (bits != simplifiedBits) { + method.instructions[push] = InsnNodeUtils.createIntConstant(simplifiedBits) + simplified++ + } + } + return false + } + + public override fun postTransform(classPath: ClassPath) { + logger.info { "Simplified $simplified bit shifts" } + } + + companion object { + private val logger = InlineLogger() + private val CONST_SHIFT_MATCHER = + InsnMatcher.compile("(ICONST | BIPUSH | SIPUSH | LDC) (ISHL | ISHR | IUSHR | LSHL | LSHR | LUSHR)") + private val LONG_SHIFTS = setOf(Opcodes.LSHL, Opcodes.LSHR, Opcodes.LUSHR) + } +}