Convert createIntConstant to an extension method on Int

Signed-off-by: Graham <gpe@openrs2.dev>
bzip2
Graham 5 years ago
parent 2471418d4a
commit cf3474c016
  1. 8
      asm/src/main/java/dev/openrs2/asm/InsnNodeUtils.kt
  2. 4
      bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.kt
  3. 6
      bundler/src/main/java/dev/openrs2/bundler/transform/PlatformDetectionTransformer.kt
  4. 30
      bundler/src/main/java/dev/openrs2/bundler/transform/ResourceTransformer.kt
  5. 4
      deob/src/main/java/dev/openrs2/deob/transform/BitShiftTransformer.kt
  6. 4
      deob/src/main/java/dev/openrs2/deob/transform/ConstantArgTransformer.kt

@ -281,7 +281,7 @@ val AbstractInsnNode.isPure: Boolean
else -> throw IllegalArgumentException() else -> throw IllegalArgumentException()
} }
fun createIntConstant(value: Int): AbstractInsnNode = when (value) { fun Int.toAbstractInsnNode(): AbstractInsnNode = when (this) {
-1 -> InsnNode(Opcodes.ICONST_M1) -1 -> InsnNode(Opcodes.ICONST_M1)
0 -> InsnNode(Opcodes.ICONST_0) 0 -> InsnNode(Opcodes.ICONST_0)
1 -> InsnNode(Opcodes.ICONST_1) 1 -> InsnNode(Opcodes.ICONST_1)
@ -289,9 +289,9 @@ fun createIntConstant(value: Int): AbstractInsnNode = when (value) {
3 -> InsnNode(Opcodes.ICONST_3) 3 -> InsnNode(Opcodes.ICONST_3)
4 -> InsnNode(Opcodes.ICONST_4) 4 -> InsnNode(Opcodes.ICONST_4)
5 -> InsnNode(Opcodes.ICONST_5) 5 -> InsnNode(Opcodes.ICONST_5)
in Byte.MIN_VALUE..Byte.MAX_VALUE -> IntInsnNode(Opcodes.BIPUSH, value) in Byte.MIN_VALUE..Byte.MAX_VALUE -> IntInsnNode(Opcodes.BIPUSH, this)
in Short.MIN_VALUE..Short.MAX_VALUE -> IntInsnNode(Opcodes.SIPUSH, value) in Short.MIN_VALUE..Short.MAX_VALUE -> IntInsnNode(Opcodes.SIPUSH, this)
else -> LdcInsnNode(value) else -> LdcInsnNode(this)
} }
fun AbstractInsnNode.toPrettyString(): String { fun AbstractInsnNode.toPrettyString(): String {

@ -5,8 +5,8 @@ import dev.openrs2.asm.InsnMatcher
import dev.openrs2.asm.MemberRef import dev.openrs2.asm.MemberRef
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.createIntConstant
import dev.openrs2.asm.hasCode import dev.openrs2.asm.hasCode
import dev.openrs2.asm.toAbstractInsnNode
import dev.openrs2.asm.transform.Transformer import dev.openrs2.asm.transform.Transformer
import org.objectweb.asm.tree.ClassNode import org.objectweb.asm.tree.ClassNode
import org.objectweb.asm.tree.FieldInsnNode import org.objectweb.asm.tree.FieldInsnNode
@ -46,7 +46,7 @@ class BufferSizeTransformer : Transformer() {
for (match in NEW_BUFFER_MATCHER.match(method)) { for (match in NEW_BUFFER_MATCHER.match(method)) {
val putstatic = match[4] as FieldInsnNode val putstatic = match[4] as FieldInsnNode
if (MemberRef(putstatic) == buffer!!) { if (MemberRef(putstatic) == buffer!!) {
method.instructions[match[2]] = createIntConstant(65536) method.instructions[match[2]] = 65536.toAbstractInsnNode()
buffersResized++ buffersResized++
} }
} }

@ -5,8 +5,8 @@ import dev.openrs2.asm.InsnMatcher
import dev.openrs2.asm.MemberRef import dev.openrs2.asm.MemberRef
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.createIntConstant
import dev.openrs2.asm.intConstant import dev.openrs2.asm.intConstant
import dev.openrs2.asm.toAbstractInsnNode
import dev.openrs2.asm.transform.Transformer import dev.openrs2.asm.transform.Transformer
import org.objectweb.asm.Opcodes import org.objectweb.asm.Opcodes
import org.objectweb.asm.tree.AbstractInsnNode import org.objectweb.asm.tree.AbstractInsnNode
@ -93,12 +93,12 @@ class PlatformDetectionTransformer : Transformer() {
list.add(JumpInsnNode(Opcodes.IFEQ, i386)) list.add(JumpInsnNode(Opcodes.IFEQ, i386))
list.add(amd64) list.add(amd64)
list.add(createIntConstant(index * 2 + 1)) list.add((index * 2 + 1).toAbstractInsnNode())
list.add(VarInsnNode(Opcodes.ISTORE, platformVar)) list.add(VarInsnNode(Opcodes.ISTORE, platformVar))
list.add(JumpInsnNode(Opcodes.GOTO, end)) list.add(JumpInsnNode(Opcodes.GOTO, end))
list.add(i386) list.add(i386)
list.add(createIntConstant(index * 2)) list.add((index * 2).toAbstractInsnNode())
list.add(VarInsnNode(Opcodes.ISTORE, platformVar)) list.add(VarInsnNode(Opcodes.ISTORE, platformVar))
list.add(JumpInsnNode(Opcodes.GOTO, end)) list.add(JumpInsnNode(Opcodes.GOTO, end))

@ -4,7 +4,7 @@ import com.github.michaelbull.logging.InlineLogger
import dev.openrs2.asm.InsnMatcher import dev.openrs2.asm.InsnMatcher
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.createIntConstant import dev.openrs2.asm.toAbstractInsnNode
import dev.openrs2.asm.transform.Transformer import dev.openrs2.asm.transform.Transformer
import dev.openrs2.bundler.Resource import dev.openrs2.bundler.Resource
import org.objectweb.asm.Opcodes import org.objectweb.asm.Opcodes
@ -44,12 +44,12 @@ class ResourceTransformer(
source.cst = resource.sourceWithCrc source.cst = resource.sourceWithCrc
// update file sizes // update file sizes
method.instructions.set(match[22], createIntConstant(resource.uncompressedSize)) method.instructions.set(match[22], resource.uncompressedSize.toAbstractInsnNode())
method.instructions.set(match[23], createIntConstant(resource.compressedSize)) method.instructions.set(match[23], resource.compressedSize.toAbstractInsnNode())
// update digest // update digest
for ((j, byte) in resource.digest.withIndex()) { for ((j, byte) in resource.digest.withIndex()) {
method.instructions.set(match[28 + 4 * j], createIntConstant(byte.toInt())) method.instructions.set(match[28 + 4 * j], byte.toInt().toAbstractInsnNode())
} }
} }
} }
@ -105,12 +105,12 @@ class ResourceTransformer(
resources: List<List<Resource>>, resources: List<List<Resource>>,
messages: List<String> messages: List<String>
) { ) {
list.add(createIntConstant(resources.size)) list.add(resources.size.toAbstractInsnNode())
list.add(TypeInsnNode(Opcodes.ANEWARRAY, "[L$type;")) list.add(TypeInsnNode(Opcodes.ANEWARRAY, "[L$type;"))
for ((i, innerResources) in resources.withIndex()) { for ((i, innerResources) in resources.withIndex()) {
list.add(InsnNode(Opcodes.DUP)) list.add(InsnNode(Opcodes.DUP))
list.add(createIntConstant(i)) list.add(i.toAbstractInsnNode())
createResourceArray(list, type, innerResources, messages, progressSufix = true) createResourceArray(list, type, innerResources, messages, progressSufix = true)
@ -125,19 +125,19 @@ class ResourceTransformer(
messages: List<String>, messages: List<String>,
progressSufix: Boolean = false progressSufix: Boolean = false
) { ) {
list.add(createIntConstant(resources.size)) list.add(resources.size.toAbstractInsnNode())
list.add(TypeInsnNode(Opcodes.ANEWARRAY, type)) list.add(TypeInsnNode(Opcodes.ANEWARRAY, type))
for ((i, resource) in resources.withIndex()) { for ((i, resource) in resources.withIndex()) {
list.add(InsnNode(Opcodes.DUP)) list.add(InsnNode(Opcodes.DUP))
list.add(createIntConstant(i)) list.add(i.toAbstractInsnNode())
list.add(TypeInsnNode(Opcodes.NEW, type)) list.add(TypeInsnNode(Opcodes.NEW, type))
list.add(InsnNode(Opcodes.DUP)) list.add(InsnNode(Opcodes.DUP))
list.add(LdcInsnNode(resource.destination)) list.add(LdcInsnNode(resource.destination))
list.add(LdcInsnNode(resource.sourceWithCrc)) list.add(LdcInsnNode(resource.sourceWithCrc))
list.add(createIntConstant(messages.size)) list.add(messages.size.toAbstractInsnNode())
list.add(TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/String")) list.add(TypeInsnNode(Opcodes.ANEWARRAY, "java/lang/String"))
for ((j, message) in messages.withIndex()) { for ((j, message) in messages.withIndex()) {
@ -148,21 +148,21 @@ class ResourceTransformer(
} }
list.add(InsnNode(Opcodes.DUP)) list.add(InsnNode(Opcodes.DUP))
list.add(createIntConstant(j)) list.add(j.toAbstractInsnNode())
list.add(LdcInsnNode(messageWithSuffix)) list.add(LdcInsnNode(messageWithSuffix))
list.add(InsnNode(Opcodes.AASTORE)) list.add(InsnNode(Opcodes.AASTORE))
} }
list.add(createIntConstant(resource.uncompressedSize)) list.add(resource.uncompressedSize.toAbstractInsnNode())
list.add(createIntConstant(resource.compressedSize)) list.add(resource.compressedSize.toAbstractInsnNode())
list.add(createIntConstant(resource.digest.size)) list.add(resource.digest.size.toAbstractInsnNode())
list.add(IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_INT)) list.add(IntInsnNode(Opcodes.NEWARRAY, Opcodes.T_INT))
for ((j, byte) in resource.digest.withIndex()) { for ((j, byte) in resource.digest.withIndex()) {
list.add(InsnNode(Opcodes.DUP)) list.add(InsnNode(Opcodes.DUP))
list.add(createIntConstant(j)) list.add(j.toAbstractInsnNode())
list.add(createIntConstant(byte.toInt())) list.add(byte.toInt().toAbstractInsnNode())
list.add(InsnNode(Opcodes.IASTORE)) list.add(InsnNode(Opcodes.IASTORE))
} }

@ -4,8 +4,8 @@ import com.github.michaelbull.logging.InlineLogger
import dev.openrs2.asm.InsnMatcher import dev.openrs2.asm.InsnMatcher
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.createIntConstant
import dev.openrs2.asm.intConstant import dev.openrs2.asm.intConstant
import dev.openrs2.asm.toAbstractInsnNode
import dev.openrs2.asm.transform.Transformer import dev.openrs2.asm.transform.Transformer
import org.objectweb.asm.Opcodes import org.objectweb.asm.Opcodes
import org.objectweb.asm.tree.ClassNode import org.objectweb.asm.tree.ClassNode
@ -36,7 +36,7 @@ class BitShiftTransformer : Transformer() {
val simplifiedBits = bits and mask val simplifiedBits = bits and mask
if (bits != simplifiedBits) { if (bits != simplifiedBits) {
method.instructions[push] = createIntConstant(simplifiedBits) method.instructions[push] = simplifiedBits.toAbstractInsnNode()
simplified++ simplified++
} }
} }

@ -5,7 +5,6 @@ import dev.openrs2.asm.MemberRef
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.copy import dev.openrs2.asm.copy
import dev.openrs2.asm.createIntConstant
import dev.openrs2.asm.deleteExpression import dev.openrs2.asm.deleteExpression
import dev.openrs2.asm.hasCode import dev.openrs2.asm.hasCode
import dev.openrs2.asm.intConstant import dev.openrs2.asm.intConstant
@ -13,6 +12,7 @@ import dev.openrs2.asm.isPure
import dev.openrs2.asm.nextReal import dev.openrs2.asm.nextReal
import dev.openrs2.asm.replaceExpression import dev.openrs2.asm.replaceExpression
import dev.openrs2.asm.stackMetadata import dev.openrs2.asm.stackMetadata
import dev.openrs2.asm.toAbstractInsnNode
import dev.openrs2.asm.transform.Transformer import dev.openrs2.asm.transform.Transformer
import dev.openrs2.deob.ArgRef import dev.openrs2.deob.ArgRef
import dev.openrs2.deob.analysis.IntBranch import dev.openrs2.deob.analysis.IntBranch
@ -286,7 +286,7 @@ class ConstantArgTransformer : Transformer() {
continue continue
} }
val replacement = createIntConstant(value) val replacement = value.toAbstractInsnNode()
if (method.instructions.replaceExpression(insn, replacement, AbstractInsnNode::isPure)) { if (method.instructions.replaceExpression(insn, replacement, AbstractInsnNode::isPure)) {
inlined++ inlined++
} }

Loading…
Cancel
Save