From 7b3ec975d958a98556c11065efdffd2845e6131e Mon Sep 17 00:00:00 2001 From: Graham Date: Tue, 27 Aug 2019 18:50:09 +0100 Subject: [PATCH] Inline constants in DummyArgTransformer This is a prerequisite for us to be able to remove the constant arguments. --- .../deob/transform/DummyArgTransformer.java | 45 ++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) 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 a25422f2..45f46e4f 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.java @@ -13,6 +13,7 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; import dev.openrs2.asm.InsnNodeUtils; import dev.openrs2.asm.MemberRef; +import dev.openrs2.asm.StackMetadata; import dev.openrs2.asm.classpath.ClassPath; import dev.openrs2.asm.transform.Transformer; import dev.openrs2.deob.analysis.IntInterpreter; @@ -20,6 +21,7 @@ import dev.openrs2.deob.analysis.IntValue; import dev.openrs2.util.collect.DisjointSet; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; +import org.objectweb.asm.tree.AbstractInsnNode; import org.objectweb.asm.tree.ClassNode; import org.objectweb.asm.tree.JumpInsnNode; import org.objectweb.asm.tree.MethodInsnNode; @@ -162,12 +164,13 @@ public final class DummyArgTransformer extends Transformer { private final Multimap argValues = HashMultimap.create(); private final Map, ImmutableSet[]> constArgs = new HashMap<>(); private DisjointSet inheritedMethodSets; - private int branchesSimplified; + private int branchesSimplified, constantsInlined; @Override protected void preTransform(ClassPath classPath) { inheritedMethodSets = classPath.createInheritedMethodSets(); branchesSimplified = 0; + constantsInlined = 0; } @Override @@ -189,6 +192,7 @@ public final class DummyArgTransformer extends Transformer { var alwaysTakenBranches = new ArrayList(); var neverTakenBranches = new ArrayList(); + var constInsns = new HashMap(); for (var i = 0; i < frames.length; i++) { var frame = frames[i]; @@ -256,6 +260,30 @@ public final class DummyArgTransformer extends Transformer { break; } break; + default: + if (InsnNodeUtils.hasSideEffects(insn) || InsnNodeUtils.isIntConstant(insn)) { + continue; + } + + if (StackMetadata.get(insn).getPushes() != 1) { + continue; + } + + var nextInsn = InsnNodeUtils.nextReal(insn); + if (nextInsn == null) { + continue; + } + + var nextInsnIndex = method.instructions.indexOf(nextInsn); + var nextFrame = frames[nextInsnIndex]; + + value = nextFrame.getStack(nextFrame.getStackSize() - 1); + if (!value.isSingleConstant()) { + continue; + } + + constInsns.put(insn, value.getIntValue()); + break; } } @@ -273,6 +301,19 @@ public final class DummyArgTransformer extends Transformer { } } + for (var entry : constInsns.entrySet()) { + var insn = entry.getKey(); + if (!method.instructions.contains(insn)) { + continue; + } + + var replacement = InsnNodeUtils.createIntConstant(entry.getValue()); + if (InsnNodeUtils.replaceSimpleExpression(method.instructions, insn, replacement)) { + constantsInlined++; + changed = true; + } + } + return changed; } @@ -302,6 +343,6 @@ public final class DummyArgTransformer extends Transformer { @Override protected void postTransform(ClassPath classPath) { - logger.info("Simplified {} dummy branches", branchesSimplified); + logger.info("Simplified {} dummy branches and inlined {} constants", branchesSimplified, constantsInlined); } }