From b6c3c9942d0ea22162466bbb4a9300387a2c4b74 Mon Sep 17 00:00:00 2001 From: pyos Date: Tue, 25 May 2021 14:01:38 +0200 Subject: [PATCH] JVM: move activeLambda to PsiInlineCodegen --- .../kotlin/codegen/ExpressionCodegen.java | 4 ++-- .../kotlin/codegen/inline/InlineCodegen.kt | 12 ++++------- .../kotlin/codegen/inline/PsiInlineCodegen.kt | 8 ++++++-- .../backend/jvm/codegen/IrInlineCodegen.kt | 20 +++++++------------ 4 files changed, 19 insertions(+), 25 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index e826b9d513c..2063c5b2470 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -1261,8 +1261,8 @@ public class ExpressionCodegen extends KtVisitor impleme if (closure.isSuspendLambda()) { // When inlining crossinline lambda, the ACONST_NULL is never popped. // Thus, do not generate it. Otherwise, it leads to VerifyError on run-time. - boolean isCrossinlineLambda = (callGenerator instanceof InlineCodegen) && - Objects.requireNonNull(((InlineCodegen) callGenerator).getActiveLambda(), + boolean isCrossinlineLambda = (callGenerator instanceof PsiInlineCodegen) && + Objects.requireNonNull(((PsiInlineCodegen) callGenerator).getActiveLambda(), "no active lambda found").isCrossInline; if (!isCrossinlineLambda) { v.aconst(null); 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 555b3ffc65d..3642380542c 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt @@ -69,9 +69,6 @@ abstract class InlineCodegen( protected val expressionMap = linkedMapOf() - var activeLambda: LambdaInfo? = null - protected set - private val sourceMapper = sourceCompiler.lazySourceMapper protected var delayedHiddenWriting: Function0? = null @@ -356,7 +353,7 @@ abstract class InlineCodegen( protected fun putArgumentOrCapturedToLocalVal( jvmKotlinType: JvmKotlinType, stackValue: StackValue, - capturedParamIndex: Int, + capturedParam: CapturedParamDesc?, parameterIndex: Int, kind: ValueKind ) { @@ -373,9 +370,8 @@ abstract class InlineCodegen( val remappedValue = if (couldBeRemapped) stackValue else null val info: ParameterInfo - if (capturedParamIndex >= 0) { - val capturedParamInfoInLambda = activeLambda!!.capturedVars[capturedParamIndex] - info = invocationParamBuilder.addCapturedParam(capturedParamInfoInLambda, capturedParamInfoInLambda.fieldName, false) + if (capturedParam != null) { + info = invocationParamBuilder.addCapturedParam(capturedParam, capturedParam.fieldName, false) info.remapValue = remappedValue } else { info = invocationParamBuilder.addNextValueParameter(jvmType, false, remappedValue, parameterIndex) @@ -456,7 +452,7 @@ abstract class InlineCodegen( //HACK: actually parameter would be placed on stack in default function // also see ValueKind.DEFAULT_LAMBDA_CAPTURED_PARAMETER check StackValue.onStack(captured.type), - paramIndex, + captured, paramIndex, ValueKind.DEFAULT_LAMBDA_CAPTURED_PARAMETER ) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiInlineCodegen.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiInlineCodegen.kt index 92b0ab6c180..e34ce8e742f 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiInlineCodegen.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiInlineCodegen.kt @@ -102,6 +102,9 @@ class PsiInlineCodegen( delayedHiddenWriting = recordParameterValueInLocalVal(justProcess, false, *hiddenParameters.toTypedArray()) } + var activeLambda: LambdaInfo? = null + private set + override fun putClosureParametersOnStack(next: LambdaInfo, functionReferenceReceiver: StackValue?) { activeLambda = next when (next) { @@ -192,12 +195,13 @@ class PsiInlineCodegen( assert(maskValues.isEmpty()) { "Additional default call arguments should be last ones, but $value" } - putArgumentOrCapturedToLocalVal(parameterType, value, -1, parameterIndex, kind) + putArgumentOrCapturedToLocalVal(parameterType, value, null, parameterIndex, kind) } override fun putCapturedValueOnStack(stackValue: StackValue, valueType: Type, paramIndex: Int) { + val param = activeLambda!!.capturedVars[paramIndex] putArgumentOrCapturedToLocalVal( - JvmKotlinType(stackValue.type, stackValue.kotlinType), stackValue, paramIndex, paramIndex, ValueKind.CAPTURED + JvmKotlinType(stackValue.type, stackValue.kotlinType), stackValue, param, paramIndex, ValueKind.CAPTURED ) } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt index 456383789bf..9a31d0bde32 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt @@ -58,17 +58,13 @@ class IrInlineCodegen( } override fun putClosureParametersOnStack(next: LambdaInfo, functionReferenceReceiver: StackValue?) { - activeLambda = next - when (next) { is IrExpressionLambdaImpl -> next.reference.getArgumentsWithIr().forEachIndexed { index, (_, ir) -> - putCapturedValueOnStack(ir, next.capturedVars[index].type, index) + putCapturedValueOnStack(ir, next.capturedVars[index], index) } is IrDefaultLambda -> rememberCapturedForDefaultLambda(next) else -> throw RuntimeException("Unknown lambda: $next") } - - activeLambda = null } override fun genValueAndPut( @@ -87,9 +83,7 @@ class IrInlineCodegen( expressionMap[closureInfo.index] = lambdaInfo val boundReceiver = irReference.extensionReceiver if (boundReceiver != null) { - activeLambda = lambdaInfo - putCapturedValueOnStack(boundReceiver, lambdaInfo.capturedVars.single().type, 0) - activeLambda = null + putCapturedValueOnStack(boundReceiver, lambdaInfo.capturedVars.single(), 0) } } else { val kind = when (irValueParameter.origin) { @@ -125,15 +119,15 @@ class IrInlineCodegen( //TODO support default argument erasure if (!processDefaultMaskOrMethodHandler(onStack, kind)) { val expectedType = JvmKotlinType(parameterType, irValueParameter.type.toIrBasedKotlinType()) - putArgumentOrCapturedToLocalVal(expectedType, onStack, -1, irValueParameter.index, kind) + putArgumentOrCapturedToLocalVal(expectedType, onStack, null, irValueParameter.index, kind) } } } - private fun putCapturedValueOnStack(argumentExpression: IrExpression, valueType: Type, capturedParamIndex: Int) { - val onStack = codegen.genOrGetLocal(argumentExpression, valueType, argumentExpression.type, BlockInfo()) - val expectedType = JvmKotlinType(valueType, argumentExpression.type.toIrBasedKotlinType()) - putArgumentOrCapturedToLocalVal(expectedType, onStack, capturedParamIndex, capturedParamIndex, ValueKind.CAPTURED) + private fun putCapturedValueOnStack(argumentExpression: IrExpression, param: CapturedParamDesc, capturedParamIndex: Int) { + val onStack = codegen.genOrGetLocal(argumentExpression, param.type, argumentExpression.type, BlockInfo()) + val expectedType = JvmKotlinType(param.type, argumentExpression.type.toIrBasedKotlinType()) + putArgumentOrCapturedToLocalVal(expectedType, onStack, param, capturedParamIndex, ValueKind.CAPTURED) } override fun beforeValueParametersStart() {