JVM IR: minimize usages of some methods in MethodSignatureMapper

Also do not pass useless parameters such as OwnerKind which is always
IMPLEMENTATION in the IR backend
This commit is contained in:
Alexander Udalov
2019-08-27 14:45:22 +02:00
parent 4dc1d61c38
commit 46ced326b8
9 changed files with 25 additions and 36 deletions
@@ -10,7 +10,6 @@ import org.jetbrains.kotlin.backend.common.lower.*
import org.jetbrains.kotlin.backend.common.lower.loops.forLoopsPhase import org.jetbrains.kotlin.backend.common.lower.loops.forLoopsPhase
import org.jetbrains.kotlin.backend.common.phaser.* import org.jetbrains.kotlin.backend.common.phaser.*
import org.jetbrains.kotlin.backend.jvm.lower.* import org.jetbrains.kotlin.backend.jvm.lower.*
import org.jetbrains.kotlin.codegen.OwnerKind
import org.jetbrains.kotlin.config.LanguageFeature import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.descriptors.Visibility
@@ -75,7 +74,7 @@ private val propertiesPhase = makeIrFilePhase<JvmBackendContext>(
val baseName = val baseName =
if (context.state.languageVersionSettings.supportsFeature(LanguageFeature.UseGetterNameForPropertyAnnotationsMethodOnJvm)) { if (context.state.languageVersionSettings.supportsFeature(LanguageFeature.UseGetterNameForPropertyAnnotationsMethodOnJvm)) {
property.getter?.let { getter -> property.getter?.let { getter ->
context.methodSignatureMapper.mapFunctionName(getter, OwnerKind.IMPLEMENTATION) context.methodSignatureMapper.mapFunctionName(getter)
} ?: JvmAbi.getterName(property.name.asString()) } ?: JvmAbi.getterName(property.name.asString())
} else { } else {
property.name.asString() property.name.asString()
@@ -15,7 +15,6 @@ import org.jetbrains.kotlin.backend.jvm.lower.inlineclasses.unboxInlineClass
import org.jetbrains.kotlin.codegen.AsmUtil.* import org.jetbrains.kotlin.codegen.AsmUtil.*
import org.jetbrains.kotlin.codegen.BaseExpressionCodegen import org.jetbrains.kotlin.codegen.BaseExpressionCodegen
import org.jetbrains.kotlin.codegen.CallGenerator import org.jetbrains.kotlin.codegen.CallGenerator
import org.jetbrains.kotlin.codegen.OwnerKind
import org.jetbrains.kotlin.codegen.StackValue import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.codegen.coroutines.INVOKE_SUSPEND_METHOD_NAME import org.jetbrains.kotlin.codegen.coroutines.INVOKE_SUSPEND_METHOD_NAME
import org.jetbrains.kotlin.codegen.extractReificationArgument import org.jetbrains.kotlin.codegen.extractReificationArgument
@@ -98,6 +97,7 @@ class VariableInfo(val declaration: IrVariable, val index: Int, val type: Type,
class ExpressionCodegen( class ExpressionCodegen(
val irFunction: IrFunction, val irFunction: IrFunction,
val signature: JvmMethodSignature,
override val frameMap: IrFrameMap, override val frameMap: IrFrameMap,
val mv: InstructionAdapter, val mv: InstructionAdapter,
val classCodegen: ClassCodegen, val classCodegen: ClassCodegen,
@@ -179,7 +179,7 @@ class ExpressionCodegen(
if (irFunction.origin != JvmLoweredDeclarationOrigin.CLASS_STATIC_INITIALIZER) { if (irFunction.origin != JvmLoweredDeclarationOrigin.CLASS_STATIC_INITIALIZER) {
irFunction.markLineNumber(startOffset = irFunction is IrConstructor && irFunction.isPrimary) irFunction.markLineNumber(startOffset = irFunction is IrConstructor && irFunction.isPrimary)
} }
val returnType = methodSignatureMapper.mapReturnType(irFunction) val returnType = signature.returnType
val returnIrType = if (irFunction !is IrConstructor) irFunction.returnType else context.irBuiltIns.unitType val returnIrType = if (irFunction !is IrConstructor) irFunction.returnType else context.irBuiltIns.unitType
result.coerce(returnType, returnIrType).materialize() result.coerce(returnType, returnIrType).materialize()
mv.areturn(returnType) mv.areturn(returnType)
@@ -537,8 +537,7 @@ class ExpressionCodegen(
?: error("Unsupported IrReturnTarget: $returnTarget") ?: error("Unsupported IrReturnTarget: $returnTarget")
//TODO: should be owner != irFunction //TODO: should be owner != irFunction
val isNonLocalReturn = val isNonLocalReturn =
methodSignatureMapper.mapFunctionName(owner, OwnerKind.IMPLEMENTATION) != methodSignatureMapper.mapFunctionName(owner) != methodSignatureMapper.mapFunctionName(irFunction)
methodSignatureMapper.mapFunctionName(irFunction, OwnerKind.IMPLEMENTATION)
if (isNonLocalReturn && state.isInlineDisabled) { if (isNonLocalReturn && state.isInlineDisabled) {
//TODO: state.diagnostics.report(Errors.NON_LOCAL_RETURN_IN_DISABLED_INLINE.on(expression)) //TODO: state.diagnostics.report(Errors.NON_LOCAL_RETURN_IN_DISABLED_INLINE.on(expression))
genThrow( genThrow(
@@ -548,7 +547,7 @@ class ExpressionCodegen(
return immaterialUnitValue return immaterialUnitValue
} }
val returnType = methodSignatureMapper.mapReturnType(owner) val returnType = if (owner == irFunction) signature.returnType else methodSignatureMapper.mapReturnType(owner)
val afterReturnLabel = Label() val afterReturnLabel = Label()
expression.value.accept(this, data).coerce(returnType, owner.returnType).materialize() expression.value.accept(this, data).coerce(returnType, owner.returnType).materialize()
generateFinallyBlocksIfNeeded(returnType, afterReturnLabel, data) generateFinallyBlocksIfNeeded(returnType, afterReturnLabel, data)
@@ -9,7 +9,6 @@ import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
import org.jetbrains.kotlin.codegen.AsmUtil import org.jetbrains.kotlin.codegen.AsmUtil
import org.jetbrains.kotlin.codegen.ClassBuilderMode import org.jetbrains.kotlin.codegen.ClassBuilderMode
import org.jetbrains.kotlin.codegen.OwnerKind
import org.jetbrains.kotlin.codegen.visitAnnotableParameterCount import org.jetbrains.kotlin.codegen.visitAnnotableParameterCount
import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.*
@@ -44,7 +43,7 @@ open class FunctionCodegen(
} }
private fun doGenerate(): JvmMethodGenericSignature { private fun doGenerate(): JvmMethodGenericSignature {
val signature = classCodegen.methodSignatureMapper.mapSignatureWithGeneric(irFunction, OwnerKind.IMPLEMENTATION) val signature = classCodegen.methodSignatureMapper.mapSignatureWithGeneric(irFunction)
val flags = calculateMethodFlags(irFunction.isStatic) val flags = calculateMethodFlags(irFunction.isStatic)
var methodVisitor = createMethod(flags, signature) var methodVisitor = createMethod(flags, signature)
@@ -90,7 +89,7 @@ open class FunctionCodegen(
) )
else -> methodVisitor else -> methodVisitor
} }
ExpressionCodegen(irFunction, frameMap, InstructionAdapter(methodVisitor), classCodegen, isInlineLambda).generate() ExpressionCodegen(irFunction, signature, frameMap, InstructionAdapter(methodVisitor), classCodegen, isInlineLambda).generate()
methodVisitor.visitMaxs(-1, -1) methodVisitor.visitMaxs(-1, -1)
continuationClassBuilder?.done() continuationClassBuilder?.done()
} }
@@ -57,11 +57,10 @@ class IrSourceCompilerForInline(
override val inlineCallSiteInfo: InlineCallSiteInfo override val inlineCallSiteInfo: InlineCallSiteInfo
get() { get() {
//TODO: support nested inline calls //TODO: support nested inline calls
val signature = codegen.methodSignatureMapper.mapSignatureSkipGeneric(codegen.irFunction)
return InlineCallSiteInfo( return InlineCallSiteInfo(
codegen.classCodegen.type.internalName, codegen.classCodegen.type.internalName,
signature.asmMethod.name, codegen.signature.asmMethod.name,
signature.asmMethod.descriptor, codegen.signature.asmMethod.descriptor,
//compilationContextFunctionDescriptor.isInlineOrInsideInline() //compilationContextFunctionDescriptor.isInlineOrInsideInline()
false, false,
compilationContextFunctionDescriptor.isSuspend compilationContextFunctionDescriptor.isSuspend
@@ -132,7 +131,8 @@ class IrSourceCompilerForInline(
override fun createCodegenForExternalFinallyBlockGenerationOnNonLocalReturn(finallyNode: MethodNode, curFinallyDepth: Int) = override fun createCodegenForExternalFinallyBlockGenerationOnNonLocalReturn(finallyNode: MethodNode, curFinallyDepth: Int) =
ExpressionCodegen( ExpressionCodegen(
codegen.irFunction, codegen.frameMap, InstructionAdapter(finallyNode), codegen.classCodegen, codegen.isInlineLambda codegen.irFunction, codegen.signature, codegen.frameMap, InstructionAdapter(finallyNode), codegen.classCodegen,
codegen.isInlineLambda
).also { ).also {
it.finallyDepth = curFinallyDepth it.finallyDepth = curFinallyDepth
} }
@@ -35,8 +35,8 @@ class MethodSignatureMapper(context: JvmBackendContext) {
fun mapFieldSignature(irField: IrField): String? = fun mapFieldSignature(irField: IrField): String? =
kotlinTypeMapper.mapFieldSignature(irField.type.toKotlinType(), irField.descriptor) kotlinTypeMapper.mapFieldSignature(irField.type.toKotlinType(), irField.descriptor)
fun mapFunctionName(irFunction: IrFunction, ownerKind: OwnerKind?): String = fun mapFunctionName(irFunction: IrFunction): String =
kotlinTypeMapper.mapFunctionName(irFunction.descriptor, ownerKind) kotlinTypeMapper.mapFunctionName(irFunction.descriptor, OwnerKind.IMPLEMENTATION)
fun mapAnnotationParameterName(field: IrField): String = fun mapAnnotationParameterName(field: IrField): String =
kotlinTypeMapper.mapAnnotationParameterName(field.descriptor) kotlinTypeMapper.mapAnnotationParameterName(field.descriptor)
@@ -47,11 +47,11 @@ class MethodSignatureMapper(context: JvmBackendContext) {
fun mapReturnType(irFunction: IrFunction): Type = fun mapReturnType(irFunction: IrFunction): Type =
kotlinTypeMapper.mapReturnType(irFunction.descriptor) kotlinTypeMapper.mapReturnType(irFunction.descriptor)
fun mapSignatureSkipGeneric(f: IrFunction, kind: OwnerKind = OwnerKind.IMPLEMENTATION): JvmMethodSignature = fun mapSignatureSkipGeneric(f: IrFunction): JvmMethodSignature =
kotlinTypeMapper.mapSignatureSkipGeneric(f.descriptor, kind) kotlinTypeMapper.mapSignatureSkipGeneric(f.descriptor, OwnerKind.IMPLEMENTATION)
fun mapSignatureWithGeneric(f: IrFunction, kind: OwnerKind): JvmMethodGenericSignature = fun mapSignatureWithGeneric(f: IrFunction): JvmMethodGenericSignature =
kotlinTypeMapper.mapSignatureWithGeneric(f.descriptor, kind) kotlinTypeMapper.mapSignatureWithGeneric(f.descriptor, OwnerKind.IMPLEMENTATION)
fun mapToCallableMethod(expression: IrFunctionAccessExpression): IrCallableMethod { fun mapToCallableMethod(expression: IrFunctionAccessExpression): IrCallableMethod {
val callee = expression.symbol.owner val callee = expression.symbol.owner
@@ -12,7 +12,6 @@ import org.jetbrains.kotlin.backend.common.ir.*
import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
import org.jetbrains.kotlin.backend.jvm.codegen.MethodSignatureMapper import org.jetbrains.kotlin.backend.jvm.codegen.MethodSignatureMapper
import org.jetbrains.kotlin.builtins.CompanionObjectMapping.isMappedIntrinsicCompanionObject import org.jetbrains.kotlin.builtins.CompanionObjectMapping.isMappedIntrinsicCompanionObject
import org.jetbrains.kotlin.codegen.OwnerKind
import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibilities
@@ -135,7 +134,7 @@ class JvmDeclarationFactory(
return defaultImplsMethods.getOrPut(interfaceFun) { return defaultImplsMethods.getOrPut(interfaceFun) {
val defaultImpls = getDefaultImplsClass(interfaceFun.parentAsClass) val defaultImpls = getDefaultImplsClass(interfaceFun.parentAsClass)
val name = Name.identifier(methodSignatureMapper.mapFunctionName(interfaceFun, OwnerKind.IMPLEMENTATION)) val name = Name.identifier(methodSignatureMapper.mapFunctionName(interfaceFun))
createStaticFunctionWithReceivers( createStaticFunctionWithReceivers(
defaultImpls, name, interfaceFun, defaultImpls, name, interfaceFun,
dispatchReceiverType = parent.defaultType, dispatchReceiverType = parent.defaultType,
@@ -19,10 +19,9 @@ package org.jetbrains.kotlin.backend.jvm.intrinsics
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.codegen.BlockInfo import org.jetbrains.kotlin.backend.jvm.codegen.BlockInfo
import org.jetbrains.kotlin.backend.jvm.codegen.ExpressionCodegen import org.jetbrains.kotlin.backend.jvm.codegen.ExpressionCodegen
import org.jetbrains.kotlin.codegen.OwnerKind
import org.jetbrains.kotlin.codegen.StackValue import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
import org.jetbrains.kotlin.resolve.jvm.AsmTypes import org.jetbrains.kotlin.resolve.jvm.AsmTypes.JAVA_STRING_TYPE
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
@@ -31,19 +30,15 @@ object IrEnumValueOf : IntrinsicMethod() {
expression: IrFunctionAccessExpression, expression: IrFunctionAccessExpression,
signature: JvmMethodSignature, signature: JvmMethodSignature,
context: JvmBackendContext context: JvmBackendContext
): IrIntrinsicFunction { ): IrIntrinsicFunction =
val enumType = context.typeMapper.mapType(expression.type) object : IrIntrinsicFunction(expression, signature, context, listOf(JAVA_STRING_TYPE)) {
val newSignature = context.methodSignatureMapper.mapSignatureSkipGeneric(expression.symbol.owner, OwnerKind.IMPLEMENTATION)
val stringType = AsmTypes.JAVA_STRING_TYPE
return object : IrIntrinsicFunction(expression, newSignature, context, listOf(stringType)) {
override fun invoke(v: InstructionAdapter, codegen: ExpressionCodegen, data: BlockInfo): StackValue { override fun invoke(v: InstructionAdapter, codegen: ExpressionCodegen, data: BlockInfo): StackValue {
val enumType = context.typeMapper.mapType(expression.type)
v.tconst(enumType) v.tconst(enumType)
codegen.gen(expression.getValueArgument(0)!!, stringType, codegen.context.irBuiltIns.stringType, data) codegen.gen(expression.getValueArgument(0)!!, JAVA_STRING_TYPE, context.irBuiltIns.stringType, data)
v.invokestatic("java/lang/Enum", "valueOf", "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;", false) v.invokestatic("java/lang/Enum", "valueOf", "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;", false)
v.checkcast(enumType) v.checkcast(enumType)
return StackValue.onStack(enumType) return StackValue.onStack(enumType)
} }
} }
}
} }
@@ -29,7 +29,6 @@ import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrTypeOperator import org.jetbrains.kotlin.ir.expressions.IrTypeOperator
import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrSimpleFunctionSymbolImpl
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
@@ -64,7 +63,7 @@ private class CompanionObjectJvmStaticLowering(val context: JvmBackendContext) :
companion?.declarations?.filter(::isJvmStaticFunction)?.forEach { companion?.declarations?.filter(::isJvmStaticFunction)?.forEach {
val jvmStaticFunction = it as IrSimpleFunction val jvmStaticFunction = it as IrSimpleFunction
val newName = Name.identifier(context.methodSignatureMapper.mapFunctionName(jvmStaticFunction, null)) val newName = Name.identifier(context.methodSignatureMapper.mapFunctionName(jvmStaticFunction))
if (!jvmStaticFunction.visibility.isPublicAPI) { if (!jvmStaticFunction.visibility.isPublicAPI) {
// TODO: Synthetic accessor creation logic should be supported in SyntheticAccessorLowering in the future. // TODO: Synthetic accessor creation logic should be supported in SyntheticAccessorLowering in the future.
val accessorName = Name.identifier("access\$$newName") val accessorName = Name.identifier("access\$$newName")
@@ -15,7 +15,6 @@ import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
import org.jetbrains.kotlin.backend.jvm.intrinsics.receiverAndArgs import org.jetbrains.kotlin.backend.jvm.intrinsics.receiverAndArgs
import org.jetbrains.kotlin.codegen.OwnerKind
import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.descriptors.Visibility
@@ -416,7 +415,7 @@ private class SyntheticAccessorLowering(val context: JvmBackendContext) : IrElem
private var nameCounter = 0 private var nameCounter = 0
private fun IrFunction.accessorName(): Name { private fun IrFunction.accessorName(): Name {
val jvmName = context.methodSignatureMapper.mapFunctionName(this, OwnerKind.IMPLEMENTATION) val jvmName = context.methodSignatureMapper.mapFunctionName(this)
return Name.identifier("access\$$jvmName\$${nameCounter++}") return Name.identifier("access\$$jvmName\$${nameCounter++}")
} }