From 77af888b6f03fcf2cee5e323558959343d7963eb Mon Sep 17 00:00:00 2001 From: Mikhael Bogdanov Date: Mon, 15 May 2017 21:15:01 +0200 Subject: [PATCH] Code clean after convertion --- .../kotlin/codegen/inline/FieldRemapper.kt | 67 +++++++------------ .../codegen/inline/MethodInlinerUtil.kt | 5 ++ 2 files changed, 31 insertions(+), 41 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/FieldRemapper.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/FieldRemapper.kt index ba8503f90aa..08ea117dc06 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/FieldRemapper.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/FieldRemapper.kt @@ -17,12 +17,20 @@ package org.jetbrains.kotlin.codegen.inline 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.tree.AbstractInsnNode import org.jetbrains.org.objectweb.asm.tree.FieldInsnNode 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 { return fieldOwner == lambdaInternalName && @@ -30,30 +38,24 @@ open class FieldRemapper(val lambdaInternalName: String?, @JvmField val parent: InlineCodegenUtil.isCapturedFieldName(fieldName) } - fun foldFieldAccessChainIfNeeded(capturedFieldAccess: List, node: MethodNode): AbstractInsnNode? { - if (capturedFieldAccess.size == 1) { - //just aload - return null - } - - return foldFieldAccessChainIfNeeded(capturedFieldAccess, 1, node) - } + fun foldFieldAccessChainIfNeeded(capturedFieldAccess: List, node: MethodNode): AbstractInsnNode? = + if (capturedFieldAccess.size == 1) + null //single aload + else + foldFieldAccessChainIfNeeded(capturedFieldAccess, 1, node) //TODO: seems that this method is redundant but it added from safety purposes before new milestone - open fun processNonAload0FieldAccessChains(isInlinedLambda: Boolean): Boolean { - return false - } + open fun processNonAload0FieldAccessChains(isInlinedLambda: Boolean): Boolean = false private fun foldFieldAccessChainIfNeeded( capturedFieldAccess: List, currentInstruction: Int, node: MethodNode ): AbstractInsnNode? { - val checkParent = !isRoot && currentInstruction < capturedFieldAccess.size - 1 - if (checkParent) { - val transformed = parent!!.foldFieldAccessChainIfNeeded(capturedFieldAccess, currentInstruction + 1, node) - if (transformed != null) { - return transformed + if (currentInstruction < capturedFieldAccess.lastIndex) { + //try to fold longest chain first + parent?.foldFieldAccessChainIfNeeded(capturedFieldAccess, currentInstruction + 1, node)?.let { + return@foldFieldAccessChainIfNeeded it } } @@ -62,24 +64,15 @@ open class FieldRemapper(val lambdaInternalName: String?, @JvmField val parent: insnNode.name = "$$$" + insnNode.name insnNode.opcode = Opcodes.GETSTATIC - var next = capturedFieldAccess[0] - while (next !== insnNode) { - val toDelete = next - next = next.next - node.instructions.remove(toDelete) - } - + node.remove(InsnSequence(capturedFieldAccess[0], insnNode)) return capturedFieldAccess[capturedFieldAccess.size - 1] } return null } - fun findField(fieldInsnNode: FieldInsnNode): CapturedParamInfo? { - return findField(fieldInsnNode, params.captured) - } - - open fun findField(fieldInsnNode: FieldInsnNode, captured: Collection): CapturedParamInfo? { + @JvmOverloads + open fun findField(fieldInsnNode: FieldInsnNode, captured: Collection = params.captured): CapturedParamInfo? { for (valueDescriptor in captured) { if (valueDescriptor.originalFieldName == fieldInsnNode.name && valueDescriptor.containingLambdaName == fieldInsnNode.owner) { return valueDescriptor @@ -88,17 +81,9 @@ open class FieldRemapper(val lambdaInternalName: String?, @JvmField val parent: return null } - open fun getNewLambdaInternalName(): String { - return lambdaInternalName!! - } + open val newLambdaInternalName: String + get() = lambdaInternalName!! - val isRoot: Boolean - get() = parent == null - - open fun getFieldForInline(node: FieldInsnNode, prefix: StackValue?): StackValue? { - return MethodInliner.findCapturedField(node, this).remapValue - } - - open val isInsideInliningLambda: Boolean - get() = !isRoot && parent!!.isInsideInliningLambda + open fun getFieldForInline(node: FieldInsnNode, prefix: StackValue?): StackValue? = + MethodInliner.findCapturedField(node, this).remapValue } diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInlinerUtil.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInlinerUtil.kt index a2c5ad30260..4af07843a9e 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInlinerUtil.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/MethodInlinerUtil.kt @@ -99,6 +99,11 @@ fun parameterOffsets(isStatic: Boolean, valueParameters: List) = + instructions.forEach { + this@remove.instructions.remove(it) + } + fun MethodNode.remove(instructions: Collection) { instructions.forEach { this@remove.instructions.remove(it)