From 84f18c4d108d8cb10ef1038c8c4a1a7b2143662e Mon Sep 17 00:00:00 2001 From: Graham Date: Thu, 26 Dec 2019 11:26:19 +0000 Subject: [PATCH] Convert ConstSourceValue to a sealed class --- .../deob/analysis/ConstSourceInterpreter.kt | 20 ++--- .../deob/analysis/ConstSourceValue.java | 78 ------------------- .../openrs2/deob/analysis/ConstSourceValue.kt | 16 ++++ .../deob/transform/UnusedArgTransformer.kt | 7 +- 4 files changed, 30 insertions(+), 91 deletions(-) delete mode 100644 deob/src/main/java/dev/openrs2/deob/analysis/ConstSourceValue.java create mode 100644 deob/src/main/java/dev/openrs2/deob/analysis/ConstSourceValue.kt diff --git a/deob/src/main/java/dev/openrs2/deob/analysis/ConstSourceInterpreter.kt b/deob/src/main/java/dev/openrs2/deob/analysis/ConstSourceInterpreter.kt index 4e3b508e19..56c3f10ce8 100644 --- a/deob/src/main/java/dev/openrs2/deob/analysis/ConstSourceInterpreter.kt +++ b/deob/src/main/java/dev/openrs2/deob/analysis/ConstSourceInterpreter.kt @@ -13,29 +13,29 @@ class ConstSourceInterpreter : Interpreter(Opcodes.ASM7) { override fun newValue(type: Type?): ConstSourceValue? { val basicValue = basicInterpreter.newValue(type) ?: return null - return ConstSourceValue.createUnknown(basicValue) + return ConstSourceValue.Unknown(basicValue) } @Throws(AnalyzerException::class) override fun newOperation(insn: AbstractInsnNode): ConstSourceValue { val basicValue = basicInterpreter.newOperation(insn) return if (insn.intConstant != null) { - ConstSourceValue.createSingleSourceConstant(basicValue, insn) + ConstSourceValue.Single(basicValue, insn) } else { - ConstSourceValue.createUnknown(basicValue) + ConstSourceValue.Unknown(basicValue) } } @Throws(AnalyzerException::class) override fun copyOperation(insn: AbstractInsnNode, value: ConstSourceValue): ConstSourceValue { val basicValue = basicInterpreter.copyOperation(insn, value.basicValue) - return ConstSourceValue.createUnknown(basicValue) + return ConstSourceValue.Unknown(basicValue) } @Throws(AnalyzerException::class) override fun unaryOperation(insn: AbstractInsnNode, value: ConstSourceValue): ConstSourceValue? { val basicValue = basicInterpreter.unaryOperation(insn, value.basicValue) ?: return null - return ConstSourceValue.createUnknown(basicValue) + return ConstSourceValue.Unknown(basicValue) } @Throws(AnalyzerException::class) @@ -46,7 +46,7 @@ class ConstSourceInterpreter : Interpreter(Opcodes.ASM7) { ): ConstSourceValue? { val basicValue = basicInterpreter.binaryOperation(insn, value1.basicValue, value2.basicValue) ?: return null - return ConstSourceValue.createUnknown(basicValue) + return ConstSourceValue.Unknown(basicValue) } @Throws(AnalyzerException::class) @@ -62,7 +62,7 @@ class ConstSourceInterpreter : Interpreter(Opcodes.ASM7) { value2.basicValue, value3.basicValue ) ?: return null - return ConstSourceValue.createUnknown(basicValue) + return ConstSourceValue.Unknown(basicValue) } @Throws(AnalyzerException::class) @@ -72,7 +72,7 @@ class ConstSourceInterpreter : Interpreter(Opcodes.ASM7) { ): ConstSourceValue? { val args = values.map { it.basicValue }.toList() val basicValue = basicInterpreter.naryOperation(insn, args) ?: return null - return ConstSourceValue.createUnknown(basicValue) + return ConstSourceValue.Unknown(basicValue) } @Throws(AnalyzerException::class) @@ -82,10 +82,10 @@ class ConstSourceInterpreter : Interpreter(Opcodes.ASM7) { override fun merge(value1: ConstSourceValue, value2: ConstSourceValue): ConstSourceValue { val basicValue = basicInterpreter.merge(value1.basicValue, value2.basicValue) - return if (value1.isSingleSourceConstant && value1 == value2) { + return if (value1 is ConstSourceValue.Single && value1 == value2) { value1 } else { - ConstSourceValue.createUnknown(basicValue) + ConstSourceValue.Unknown(basicValue) } } } diff --git a/deob/src/main/java/dev/openrs2/deob/analysis/ConstSourceValue.java b/deob/src/main/java/dev/openrs2/deob/analysis/ConstSourceValue.java deleted file mode 100644 index 7a791a33e3..0000000000 --- a/deob/src/main/java/dev/openrs2/deob/analysis/ConstSourceValue.java +++ /dev/null @@ -1,78 +0,0 @@ -package dev.openrs2.deob.analysis; - -import java.util.Objects; - -import com.google.common.base.MoreObjects; -import com.google.common.base.Preconditions; -import org.objectweb.asm.tree.AbstractInsnNode; -import org.objectweb.asm.tree.analysis.BasicValue; -import org.objectweb.asm.tree.analysis.Value; - -public final class ConstSourceValue implements Value { - public static ConstSourceValue createUnknown(BasicValue basicValue) { - Preconditions.checkNotNull(basicValue); - return new ConstSourceValue(basicValue, null); - } - - public static ConstSourceValue createSingleSourceConstant(BasicValue basicValue, AbstractInsnNode source) { - Preconditions.checkArgument(basicValue == BasicValue.INT_VALUE); - Preconditions.checkNotNull(source); - return new ConstSourceValue(basicValue, source); - } - - private final BasicValue basicValue; - private final AbstractInsnNode source; - - private ConstSourceValue(BasicValue basicValue, AbstractInsnNode source) { - this.basicValue = basicValue; - this.source = source; - } - - public BasicValue getBasicValue() { - return basicValue; - } - - public boolean isUnknown() { - return source == null; - } - - public boolean isSingleSourceConstant() { - return source != null; - } - - public AbstractInsnNode getSource() { - Preconditions.checkState(source != null); - return source; - } - - @Override - public int getSize() { - return basicValue.getSize(); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - ConstSourceValue that = (ConstSourceValue) o; - return basicValue.equals(that.basicValue) && - Objects.equals(source, that.source); - } - - @Override - public int hashCode() { - return Objects.hash(basicValue, source); - } - - @Override - public String toString() { - return MoreObjects.toStringHelper(this) - .add("basicValue", basicValue) - .add("source", source) - .toString(); - } -} diff --git a/deob/src/main/java/dev/openrs2/deob/analysis/ConstSourceValue.kt b/deob/src/main/java/dev/openrs2/deob/analysis/ConstSourceValue.kt new file mode 100644 index 0000000000..0c7872154b --- /dev/null +++ b/deob/src/main/java/dev/openrs2/deob/analysis/ConstSourceValue.kt @@ -0,0 +1,16 @@ +package dev.openrs2.deob.analysis + +import org.objectweb.asm.tree.AbstractInsnNode +import org.objectweb.asm.tree.analysis.BasicValue +import org.objectweb.asm.tree.analysis.Value + +sealed class ConstSourceValue : Value { + data class Unknown(override val basicValue: BasicValue) : ConstSourceValue() + data class Single(override val basicValue: BasicValue, val source: AbstractInsnNode) : ConstSourceValue() + + abstract val basicValue: BasicValue + + override fun getSize(): Int { + return basicValue.size + } +} diff --git a/deob/src/main/java/dev/openrs2/deob/transform/UnusedArgTransformer.kt b/deob/src/main/java/dev/openrs2/deob/transform/UnusedArgTransformer.kt index 05ce21a4fc..c1709e79d0 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/UnusedArgTransformer.kt +++ b/deob/src/main/java/dev/openrs2/deob/transform/UnusedArgTransformer.kt @@ -10,6 +10,7 @@ import dev.openrs2.asm.transform.Transformer import dev.openrs2.common.collect.DisjointSet import dev.openrs2.deob.ArgRef import dev.openrs2.deob.analysis.ConstSourceInterpreter +import dev.openrs2.deob.analysis.ConstSourceValue import dev.openrs2.deob.remap.TypedRemapper import org.objectweb.asm.Opcodes import org.objectweb.asm.Type @@ -75,7 +76,7 @@ class UnusedArgTransformer : Transformer() { val args = Type.getArgumentTypes(insn.desc).size for (j in 0 until args) { val source = frame.getStack(stackSize - args + j) - if (!source.isSingleSourceConstant) { + if (source !is ConstSourceValue.Single) { retainedArgs.add(ArgRef(invokePartition, j)) } } @@ -123,8 +124,8 @@ class UnusedArgTransformer : Transformer() { for ((j, argType) in argTypes.withIndex()) { if (INT_SORTS.contains(argType.sort) && !retainedArgs.contains(ArgRef(partition, j))) { - val source = frame.getStack(stackSize - argTypes.size + j).source - deadInsns.add(source) + val value = frame.getStack(stackSize - argTypes.size + j) as ConstSourceValue.Single + deadInsns.add(value.source) } else { newArgTypes.add(argType) }