Code clean after convertion

This commit is contained in:
Mikhael Bogdanov
2017-05-15 21:15:01 +02:00
parent 5b947ac27e
commit 77af888b6f
2 changed files with 31 additions and 41 deletions
@@ -17,12 +17,20 @@
package org.jetbrains.kotlin.codegen.inline package org.jetbrains.kotlin.codegen.inline
import org.jetbrains.kotlin.codegen.StackValue import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.codegen.optimization.common.InsnSequence
import org.jetbrains.org.objectweb.asm.Opcodes import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode import org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode
import org.jetbrains.org.objectweb.asm.tree.FieldInsnNode import org.jetbrains.org.objectweb.asm.tree.FieldInsnNode
import org.jetbrains.org.objectweb.asm.tree.MethodNode import org.jetbrains.org.objectweb.asm.tree.MethodNode
open class FieldRemapper(val lambdaInternalName: String?, @JvmField val parent: FieldRemapper?, private val params: Parameters) { open class FieldRemapper(
val lambdaInternalName: String?,
@JvmField val parent: FieldRemapper?,
private val params: Parameters
) {
val isRoot = parent == null
open val isInsideInliningLambda: Boolean = parent?.isInsideInliningLambda ?: false
protected open fun canProcess(fieldOwner: String, fieldName: String, isFolding: Boolean): Boolean { protected open fun canProcess(fieldOwner: String, fieldName: String, isFolding: Boolean): Boolean {
return fieldOwner == lambdaInternalName && return fieldOwner == lambdaInternalName &&
@@ -30,30 +38,24 @@ open class FieldRemapper(val lambdaInternalName: String?, @JvmField val parent:
InlineCodegenUtil.isCapturedFieldName(fieldName) InlineCodegenUtil.isCapturedFieldName(fieldName)
} }
fun foldFieldAccessChainIfNeeded(capturedFieldAccess: List<AbstractInsnNode>, node: MethodNode): AbstractInsnNode? { fun foldFieldAccessChainIfNeeded(capturedFieldAccess: List<AbstractInsnNode>, node: MethodNode): AbstractInsnNode? =
if (capturedFieldAccess.size == 1) { if (capturedFieldAccess.size == 1)
//just aload null //single aload
return null else
} foldFieldAccessChainIfNeeded(capturedFieldAccess, 1, node)
return foldFieldAccessChainIfNeeded(capturedFieldAccess, 1, node)
}
//TODO: seems that this method is redundant but it added from safety purposes before new milestone //TODO: seems that this method is redundant but it added from safety purposes before new milestone
open fun processNonAload0FieldAccessChains(isInlinedLambda: Boolean): Boolean { open fun processNonAload0FieldAccessChains(isInlinedLambda: Boolean): Boolean = false
return false
}
private fun foldFieldAccessChainIfNeeded( private fun foldFieldAccessChainIfNeeded(
capturedFieldAccess: List<AbstractInsnNode>, capturedFieldAccess: List<AbstractInsnNode>,
currentInstruction: Int, currentInstruction: Int,
node: MethodNode node: MethodNode
): AbstractInsnNode? { ): AbstractInsnNode? {
val checkParent = !isRoot && currentInstruction < capturedFieldAccess.size - 1 if (currentInstruction < capturedFieldAccess.lastIndex) {
if (checkParent) { //try to fold longest chain first
val transformed = parent!!.foldFieldAccessChainIfNeeded(capturedFieldAccess, currentInstruction + 1, node) parent?.foldFieldAccessChainIfNeeded(capturedFieldAccess, currentInstruction + 1, node)?.let {
if (transformed != null) { return@foldFieldAccessChainIfNeeded it
return transformed
} }
} }
@@ -62,24 +64,15 @@ open class FieldRemapper(val lambdaInternalName: String?, @JvmField val parent:
insnNode.name = "$$$" + insnNode.name insnNode.name = "$$$" + insnNode.name
insnNode.opcode = Opcodes.GETSTATIC insnNode.opcode = Opcodes.GETSTATIC
var next = capturedFieldAccess[0] node.remove(InsnSequence(capturedFieldAccess[0], insnNode))
while (next !== insnNode) {
val toDelete = next
next = next.next
node.instructions.remove(toDelete)
}
return capturedFieldAccess[capturedFieldAccess.size - 1] return capturedFieldAccess[capturedFieldAccess.size - 1]
} }
return null return null
} }
fun findField(fieldInsnNode: FieldInsnNode): CapturedParamInfo? { @JvmOverloads
return findField(fieldInsnNode, params.captured) open fun findField(fieldInsnNode: FieldInsnNode, captured: Collection<CapturedParamInfo> = params.captured): CapturedParamInfo? {
}
open fun findField(fieldInsnNode: FieldInsnNode, captured: Collection<CapturedParamInfo>): CapturedParamInfo? {
for (valueDescriptor in captured) { for (valueDescriptor in captured) {
if (valueDescriptor.originalFieldName == fieldInsnNode.name && valueDescriptor.containingLambdaName == fieldInsnNode.owner) { if (valueDescriptor.originalFieldName == fieldInsnNode.name && valueDescriptor.containingLambdaName == fieldInsnNode.owner) {
return valueDescriptor return valueDescriptor
@@ -88,17 +81,9 @@ open class FieldRemapper(val lambdaInternalName: String?, @JvmField val parent:
return null return null
} }
open fun getNewLambdaInternalName(): String { open val newLambdaInternalName: String
return lambdaInternalName!! get() = lambdaInternalName!!
}
val isRoot: Boolean open fun getFieldForInline(node: FieldInsnNode, prefix: StackValue?): StackValue? =
get() = parent == null MethodInliner.findCapturedField(node, this).remapValue
open fun getFieldForInline(node: FieldInsnNode, prefix: StackValue?): StackValue? {
return MethodInliner.findCapturedField(node, this).remapValue
}
open val isInsideInliningLambda: Boolean
get() = !isRoot && parent!!.isInsideInliningLambda
} }
@@ -99,6 +99,11 @@ fun parameterOffsets(isStatic: Boolean, valueParameters: List<JvmMethodParameter
} }
} }
fun MethodNode.remove(instructions: Sequence<AbstractInsnNode>) =
instructions.forEach {
this@remove.instructions.remove(it)
}
fun MethodNode.remove(instructions: Collection<AbstractInsnNode>) { fun MethodNode.remove(instructions: Collection<AbstractInsnNode>) {
instructions.forEach { instructions.forEach {
this@remove.instructions.remove(it) this@remove.instructions.remove(it)