From 7bcd738bb1d7d85803422f1f1d1d0afa15f42016 Mon Sep 17 00:00:00 2001 From: pyos Date: Wed, 26 May 2021 11:38:33 +0200 Subject: [PATCH] JVM: inline createInlineMethodNode into InlineCodegenForDefaultBody Most of it is unused, since there we know the target function will also need to be compiled from source. --- .../kotlin/codegen/ExpressionCodegen.java | 2 +- .../kotlin/codegen/inline/InlineCodegen.kt | 8 +--- .../inline/InlineCodegenForDefaultBody.kt | 38 +++++++------------ .../inline/PsiSourceCompilerForInline.kt | 2 +- .../codegen/inline/SourceCompilerForInline.kt | 2 +- .../jvm/codegen/IrSourceCompilerForInline.kt | 2 +- 6 files changed, 19 insertions(+), 35 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index f835d1921c5..590d7f182ea 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -2969,7 +2969,7 @@ public class ExpressionCodegen extends KtVisitor impleme } Type methodOwner = typeMapper.mapImplementationOwner(functionDescriptor); if (isDefaultCompilation) { - return new InlineCodegenForDefaultBody(functionDescriptor, this, state, methodOwner, signature, sourceCompiler); + return new InlineCodegenForDefaultBody(functionDescriptor, this, state, signature, sourceCompiler); } else { return new PsiInlineCodegen(this, state, functionDescriptor, methodOwner, signature, typeParameterMappings, sourceCompiler, diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt index d6e2d46fe6c..2bcc208d399 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt @@ -59,11 +59,7 @@ abstract class InlineCodegen( private val initialFrameSize = codegen.frameMap.currentSize protected val invocationParamBuilder = ParametersBuilder.newBuilder() - protected val expressionMap = linkedMapOf() - - private val sourceMapper = sourceCompiler.lazySourceMapper - protected val maskValues = ArrayList() protected var maskStartIndex = -1 protected var methodHandleInDefaultMethodIndex = -1 @@ -209,6 +205,7 @@ abstract class InlineCodegen( sourceCompiler, sourceCompiler.inlineCallSiteInfo, reifiedTypeInliner, typeParameterMappings ) + val sourceMapper = sourceCompiler.sourceMapper val sourceInfo = sourceMapper.sourceInfo!! val callSite = SourcePosition(codegen.lastLineNumber, sourceInfo.sourceFileName!!, sourceInfo.pathOrCleanFQN) val inliner = MethodInliner( @@ -431,8 +428,7 @@ abstract class InlineCodegen( ) } - - internal fun createInlineMethodNode( + private fun createInlineMethodNode( callDefault: Boolean, typeArguments: List?, typeSystem: TypeSystemCommonBackendContext diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegenForDefaultBody.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegenForDefaultBody.kt index f5b9acba134..eb68684c870 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegenForDefaultBody.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegenForDefaultBody.kt @@ -15,15 +15,13 @@ import org.jetbrains.kotlin.resolve.inline.InlineUtil import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature import org.jetbrains.org.objectweb.asm.Label import org.jetbrains.org.objectweb.asm.Type -import org.jetbrains.org.objectweb.asm.tree.MethodNode class InlineCodegenForDefaultBody( private val function: FunctionDescriptor, codegen: ExpressionCodegen, val state: GenerationState, - private val methodOwner: Type, private val jvmSignature: JvmMethodSignature, - private val sourceCompilerForInline: SourceCompilerForInline + private val sourceCompilerForInline: PsiSourceCompilerForInline ) : CallGenerator { private val sourceMapper: SourceMapper = codegen.parentCodegen.orCreateSourceMapper @@ -39,32 +37,22 @@ class InlineCodegenForDefaultBody( } override fun genCallInner(callableMethod: Callable, resolvedCall: ResolvedCall<*>?, callDefault: Boolean, codegen: ExpressionCodegen) { - val nodeAndSmap = PsiInlineCodegen( - codegen, state, function, methodOwner, jvmSignature, TypeParameterMappings(), sourceCompilerForInline - ).createInlineMethodNode( - callDefault, null, codegen.typeSystem - ) - val childSourceMapper = SourceMapCopier(sourceMapper, nodeAndSmap.classSMAP) - - val node = nodeAndSmap.node - val transformedMethod = MethodNode( - node.access, - node.name, - node.desc, - node.signature, - node.exceptions.toTypedArray() - ) + assert(!callDefault) { "inlining default stub into another default stub" } + val (node, smap) = sourceCompilerForInline.compileInlineFunction(jvmSignature, callDefault, jvmSignature.asmMethod) + val childSourceMapper = SourceMapCopier(sourceMapper, smap) val argsSize = (Type.getArgumentsAndReturnSizes(jvmSignature.asmMethod.descriptor) ushr 2) - if (callableMethod.isStaticCall()) 1 else 0 - node.accept(object : InlineAdapter(transformedMethod, 0, childSourceMapper) { - override fun visitLocalVariable(name: String, desc: String, signature: String?, start: Label, end: Label, index: Int) { - val startLabel = if (index < argsSize) methodStartLabel else start - super.visitLocalVariable(name, desc, signature, startLabel, end, index) - } - }) + // `$default` is only for Kotlin use so it has no `$$forInline` version - this *is* what the inliner will use. + node.preprocessSuspendMarkers(forInline = true, keepFakeContinuation = false) + node.accept(object : MethodBodyVisitor(codegen.visitor) { + // The LVT was not generated at all, so move the start of parameters to the start of the method. + override fun visitLocalVariable(name: String, desc: String, signature: String?, start: Label, end: Label, index: Int) = + super.visitLocalVariable(name, desc, signature, if (index < argsSize) methodStartLabel else start, end, index) - transformedMethod.accept(MethodBodyVisitor(codegen.visitor)) + override fun visitLineNumber(line: Int, start: Label) = + super.visitLineNumber(childSourceMapper.mapLineNumber(line), start) + }) } override fun genValueAndPut( diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiSourceCompilerForInline.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiSourceCompilerForInline.kt index 22182390d3e..85475259d18 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiSourceCompilerForInline.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiSourceCompilerForInline.kt @@ -82,7 +82,7 @@ class PsiSourceCompilerForInline( ) } - override val lazySourceMapper + override val sourceMapper get() = codegen.parentCodegen.orCreateSourceMapper override fun generateLambdaBody(lambdaInfo: ExpressionLambda, reifiedTypeParameters: ReifiedTypeParametersUsages): SMAPAndMethodNode { diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/SourceCompilerForInline.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/SourceCompilerForInline.kt index 14b37a743af..80db374c058 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/SourceCompilerForInline.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/SourceCompilerForInline.kt @@ -33,7 +33,7 @@ interface SourceCompilerForInline { val inlineCallSiteInfo: InlineCallSiteInfo - val lazySourceMapper: SourceMapper + val sourceMapper: SourceMapper fun generateLambdaBody(lambdaInfo: ExpressionLambda, reifiedTypeParameters: ReifiedTypeParametersUsages): SMAPAndMethodNode diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrSourceCompilerForInline.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrSourceCompilerForInline.kt index 7ae1ed248f8..4568cea5e6e 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrSourceCompilerForInline.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrSourceCompilerForInline.kt @@ -91,7 +91,7 @@ class IrSourceCompilerForInline( ) } - override val lazySourceMapper: SourceMapper + override val sourceMapper: SourceMapper get() = codegen.smap override fun generateLambdaBody(lambdaInfo: ExpressionLambda, reifiedTypeParameters: ReifiedTypeParametersUsages): SMAPAndMethodNode {