JVM IR: introduce IrCallableMethod as a simpler replacement for CallableMethod

This commit is contained in:
Alexander Udalov
2019-08-06 04:38:29 +02:00
parent 6969717201
commit 6191fc10f0
6 changed files with 37 additions and 18 deletions
@@ -10,7 +10,6 @@ import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterSignature
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.org.objectweb.asm.Opcodes.INVOKESPECIAL
import org.jetbrains.org.objectweb.asm.Opcodes.INVOKESTATIC
import org.jetbrains.org.objectweb.asm.Type
@@ -23,15 +22,15 @@ class CallableMethod(
private val defaultImplOwner: Type?,
computeDefaultMethodDesc: () -> String,
private val signature: JvmMethodSignature,
private val invokeOpcode: Int,
val invokeOpcode: Int,
override val dispatchReceiverType: Type?,
override val dispatchReceiverKotlinType: KotlinType?,
override val extensionReceiverType: Type?,
override val extensionReceiverKotlinType: KotlinType?,
override val generateCalleeType: Type?,
override val returnKotlinType: KotlinType?,
private val isInterfaceMethod: Boolean = Opcodes.INVOKEINTERFACE == invokeOpcode,
private val isDefaultMethodInInterface: Boolean = false
val isInterfaceMethod: Boolean,
private val isDefaultMethodInInterface: Boolean
) : Callable {
private val defaultMethodDesc: String by lazy(LazyThreadSafetyMode.PUBLICATION, computeDefaultMethodDesc)
@@ -412,9 +412,9 @@ class ExpressionCodegen(
immaterialUnitValue
expression.type.isUnit() ->
// NewInference allows casting `() -> T` to `() -> Unit`. A CHECKCAST here will fail.
MaterialValue(this, callable.returnType, returnType).discard().coerce(expression.type)
MaterialValue(this, callable.asmMethod.returnType, returnType).discard().coerce(expression.type)
else ->
MaterialValue(this, callable.returnType, returnType).coerce(expression.type)
MaterialValue(this, callable.asmMethod.returnType, returnType).coerce(expression.type)
}
}
@@ -983,7 +983,7 @@ class ExpressionCodegen(
return classReference.onStack
}
private fun resolveToCallable(irCall: IrFunctionAccessExpression, isSuper: Boolean) =
private fun resolveToCallable(irCall: IrFunctionAccessExpression, isSuper: Boolean): IrCallableMethod =
typeMapper.mapToCallableMethod(irCall.symbol.owner, isSuper)
private fun getOrCreateCallGenerator(element: IrFunctionAccessExpression, data: BlockInfo): IrCallGenerator {
@@ -16,20 +16,18 @@
package org.jetbrains.kotlin.backend.jvm.codegen
import org.jetbrains.kotlin.codegen.Callable
import org.jetbrains.kotlin.codegen.CallableMethod
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.codegen.ValueKind
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.org.objectweb.asm.Type
interface IrCallGenerator {
fun genCall(callableMethod: Callable, codegen: ExpressionCodegen, expression: IrFunctionAccessExpression) {
callableMethod.genInvokeInstruction(codegen.mv)
fun genCall(callableMethod: IrCallableMethod, codegen: ExpressionCodegen, expression: IrFunctionAccessExpression) {
with(callableMethod) {
codegen.mv.visitMethodInsn(invokeOpcode, owner.internalName, asmMethod.name, asmMethod.descriptor, isInterfaceMethod)
}
}
fun beforeValueParametersStart() {
@@ -51,4 +49,4 @@ interface IrCallGenerator {
}
object DefaultCallGenerator : IrCallGenerator
}
}
@@ -0,0 +1,19 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.backend.jvm.codegen
import org.jetbrains.org.objectweb.asm.Type
import org.jetbrains.org.objectweb.asm.commons.Method
class IrCallableMethod(
val owner: Type,
val valueParameterTypes: List<Type>,
val invokeOpcode: Int,
val asmMethod: Method,
val dispatchReceiverType: Type?,
val extensionReceiverType: Type?,
val isInterfaceMethod: Boolean
)
@@ -98,7 +98,7 @@ class IrInlineCodegen(
}
override fun genCall(
callableMethod: Callable,
callableMethod: IrCallableMethod,
codegen: ExpressionCodegen,
expression: IrFunctionAccessExpression
) {
@@ -26,7 +26,6 @@ import org.jetbrains.kotlin.load.kotlin.TypeMappingMode
import org.jetbrains.kotlin.load.kotlin.computeExpandedTypeForInlineClass
import org.jetbrains.kotlin.load.kotlin.mapBuiltInType
import org.jetbrains.kotlin.name.SpecialNames
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodGenericSignature
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
@@ -66,8 +65,12 @@ class IrTypeMapper(private val context: JvmBackendContext) {
fun mapSignatureWithGeneric(f: IrFunction, kind: OwnerKind): JvmMethodGenericSignature =
kotlinTypeMapper.mapSignatureWithGeneric(f.descriptor, kind)
fun mapToCallableMethod(f: IrFunction, superCall: Boolean, kind: OwnerKind? = null, resolvedCall: ResolvedCall<*>? = null) =
kotlinTypeMapper.mapToCallableMethod(f.descriptor, superCall, kind, resolvedCall)
fun mapToCallableMethod(f: IrFunction, superCall: Boolean): IrCallableMethod =
with(kotlinTypeMapper.mapToCallableMethod(f.descriptor, superCall)) {
IrCallableMethod(
owner, valueParameterTypes, invokeOpcode, getAsmMethod(), dispatchReceiverType, extensionReceiverType, isInterfaceMethod
)
}
fun writeFormalTypeParameters(irParameters: List<IrTypeParameter>, sw: JvmSignatureWriter) =
kotlinTypeMapper.writeFormalTypeParameters(irParameters.map { it.descriptor }, sw)