Convert InsnListUtils to Kotlin

master
Graham 5 years ago
parent 8020ac98ab
commit 3e0e7824e0
  1. 47
      asm/src/main/java/dev/openrs2/asm/InsnListUtils.java
  2. 39
      asm/src/main/java/dev/openrs2/asm/InsnListUtils.kt
  3. 8
      deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.java
  4. 4
      deob/src/main/java/dev/openrs2/deob/transform/DummyLocalTransformer.java

@ -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<AbstractInsnNode>();
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 */
}
}

@ -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<AbstractInsnNode>()
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)
}

@ -11,7 +11,7 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.HashMultimap; import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap; 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.InsnMatcher;
import dev.openrs2.asm.InsnNodeUtilsKt; import dev.openrs2.asm.InsnNodeUtilsKt;
import dev.openrs2.asm.MemberRef; import dev.openrs2.asm.MemberRef;
@ -414,14 +414,14 @@ public final class DummyArgTransformer extends Transformer {
} }
for (var insn : alwaysTakenBranches) { 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++; branchesSimplified++;
changed = true; changed = true;
} }
} }
for (var insn : neverTakenBranches) { for (var insn : neverTakenBranches) {
if (InsnListUtils.deleteSimpleExpression(method.instructions, insn)) { if (InsnListUtilsKt.deleteSimpleExpression(method.instructions, insn)) {
branchesSimplified++; branchesSimplified++;
changed = true; changed = true;
} }
@ -434,7 +434,7 @@ public final class DummyArgTransformer extends Transformer {
} }
var replacement = InsnNodeUtilsKt.createIntConstant(entry.getValue()); var replacement = InsnNodeUtilsKt.createIntConstant(entry.getValue());
if (InsnListUtils.replaceSimpleExpression(method.instructions, insn, replacement)) { if (InsnListUtilsKt.replaceSimpleExpression(method.instructions, insn, replacement)) {
constantsInlined++; constantsInlined++;
changed = true; changed = true;
} }

@ -1,6 +1,6 @@
package dev.openrs2.deob.transform; 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.ClassPath;
import dev.openrs2.asm.classpath.Library; import dev.openrs2.asm.classpath.Library;
import dev.openrs2.asm.transform.Transformer; import dev.openrs2.asm.transform.Transformer;
@ -56,7 +56,7 @@ public final class DummyLocalTransformer extends Transformer {
continue; continue;
} }
if (InsnListUtils.deleteSimpleExpression(method.instructions, insn)) { if (InsnListUtilsKt.deleteSimpleExpression(method.instructions, insn)) {
localsRemoved++; localsRemoved++;
} }
} }

Loading…
Cancel
Save