From 7d49b72b793399937317b9b52040cabb8524aef8 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Fri, 9 Aug 2019 13:08:52 +0300 Subject: [PATCH] JVM_IR: Support 'CHECK_NOT_NULL' intrinsic TODO: migrate to changes related to KT-22275 --- .../backend/jvm/intrinsics/IrCheckNotNull.kt | 33 +++++++++++++++++++ .../jvm/intrinsics/IrIntrinsicMethods.kt | 2 +- .../boxUnboxInsideLambdaAsLastExpression.kt | 1 - ...neClassesUnboxingAfterAssertionOperator.kt | 1 - .../unboxInlineClassAfterElvis.kt | 1 - .../codegen/bytecodeText/noNumberCheckCast.kt | 2 +- .../expressionValueIsNotNullAfterExclExcl.kt | 1 - 7 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrCheckNotNull.kt diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrCheckNotNull.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrCheckNotNull.kt new file mode 100644 index 00000000000..ce6d3a8aaa9 --- /dev/null +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrCheckNotNull.kt @@ -0,0 +1,33 @@ +/* + * 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.intrinsics + +import org.jetbrains.kotlin.backend.jvm.codegen.BlockInfo +import org.jetbrains.kotlin.backend.jvm.codegen.ExpressionCodegen +import org.jetbrains.kotlin.backend.jvm.codegen.PromisedValue +import org.jetbrains.kotlin.codegen.AsmUtil +import org.jetbrains.kotlin.codegen.intrinsics.IntrinsicMethods +import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression +import org.jetbrains.org.objectweb.asm.Label +import org.jetbrains.org.objectweb.asm.Type + +object IrCheckNotNull : IntrinsicMethod() { + override fun invoke(expression: IrFunctionAccessExpression, codegen: ExpressionCodegen, data: BlockInfo): PromisedValue? { + val arg0 = expression.getValueArgument(0)!!.accept(codegen, data) + if (AsmUtil.isPrimitive(arg0.type)) return arg0 + + return object : PromisedValue(codegen, arg0.type, expression.type) { + override fun materialize() { + arg0.materialize() + mv.dup() + val ifNonNullLabel = Label() + mv.ifnonnull(ifNonNullLabel) + mv.invokestatic(IntrinsicMethods.INTRINSICS_CLASS_NAME, "throwNpe", Type.getMethodDescriptor(Type.VOID_TYPE), false) + mv.mark(ifNonNullLabel) + } + } + } +} \ No newline at end of file diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrIntrinsicMethods.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrIntrinsicMethods.kt index a405cb7fca5..2b5cdbe9035 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrIntrinsicMethods.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/intrinsics/IrIntrinsicMethods.kt @@ -96,7 +96,7 @@ class IrIntrinsicMethods(val irBuiltIns: IrBuiltIns, val symbols: JvmSymbols) { irBuiltIns.enumValueOfSymbol.toKey()!! to IrEnumValueOf, irBuiltIns.noWhenBranchMatchedExceptionSymbol.toKey()!! to IrNoWhenBranchMatchedException, irBuiltIns.illegalArgumentExceptionSymbol.toKey()!! to IrIllegalArgumentException, - // irBuiltIns.throwNpeSymbol.toKey()!! to ThrowNPE, -- TODO checkNotNullSymbol + irBuiltIns.checkNotNullSymbol.toKey()!! to IrCheckNotNull, irBuiltIns.andandSymbol.toKey()!! to AndAnd, irBuiltIns.ororSymbol.toKey()!! to OrOr, symbols.unsafeCoerceIntrinsicSymbol.toKey()!! to UnsafeCoerce diff --git a/compiler/testData/codegen/bytecodeText/inlineClasses/boxUnboxInsideLambdaAsLastExpression.kt b/compiler/testData/codegen/bytecodeText/inlineClasses/boxUnboxInsideLambdaAsLastExpression.kt index 54df7db21e4..f11db249382 100644 --- a/compiler/testData/codegen/bytecodeText/inlineClasses/boxUnboxInsideLambdaAsLastExpression.kt +++ b/compiler/testData/codegen/bytecodeText/inlineClasses/boxUnboxInsideLambdaAsLastExpression.kt @@ -1,5 +1,4 @@ // !LANGUAGE: +InlineClasses -// IGNORE_BACKEND: JVM_IR // FILE: utils.kt diff --git a/compiler/testData/codegen/bytecodeText/inlineClasses/inlineClassesUnboxingAfterAssertionOperator.kt b/compiler/testData/codegen/bytecodeText/inlineClasses/inlineClassesUnboxingAfterAssertionOperator.kt index abbed59f183..50338ee9958 100644 --- a/compiler/testData/codegen/bytecodeText/inlineClasses/inlineClassesUnboxingAfterAssertionOperator.kt +++ b/compiler/testData/codegen/bytecodeText/inlineClasses/inlineClassesUnboxingAfterAssertionOperator.kt @@ -1,5 +1,4 @@ // !LANGUAGE: +InlineClasses -// IGNORE_BACKEND: JVM_IR // FILE: utils.kt diff --git a/compiler/testData/codegen/bytecodeText/inlineClasses/unboxInlineClassAfterElvis.kt b/compiler/testData/codegen/bytecodeText/inlineClasses/unboxInlineClassAfterElvis.kt index 7738965a817..fe15ea8d553 100644 --- a/compiler/testData/codegen/bytecodeText/inlineClasses/unboxInlineClassAfterElvis.kt +++ b/compiler/testData/codegen/bytecodeText/inlineClasses/unboxInlineClassAfterElvis.kt @@ -1,5 +1,4 @@ // !LANGUAGE: +InlineClasses -// IGNORE_BACKEND: JVM_IR // FILE: utils.kt diff --git a/compiler/testData/codegen/bytecodeText/noNumberCheckCast.kt b/compiler/testData/codegen/bytecodeText/noNumberCheckCast.kt index 1ae6b24b34d..ed0879d07d1 100644 --- a/compiler/testData/codegen/bytecodeText/noNumberCheckCast.kt +++ b/compiler/testData/codegen/bytecodeText/noNumberCheckCast.kt @@ -8,4 +8,4 @@ fun stubPreventBoxingOptimization(s: Int?) { s } -//0 CHECKCAST java/lang/Number \ No newline at end of file +// 0 CHECKCAST java/lang/Number \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/nullCheckOptimization/expressionValueIsNotNullAfterExclExcl.kt b/compiler/testData/codegen/bytecodeText/nullCheckOptimization/expressionValueIsNotNullAfterExclExcl.kt index d0b7d4b01ef..c751a9fe5b7 100644 --- a/compiler/testData/codegen/bytecodeText/nullCheckOptimization/expressionValueIsNotNullAfterExclExcl.kt +++ b/compiler/testData/codegen/bytecodeText/nullCheckOptimization/expressionValueIsNotNullAfterExclExcl.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR // FILE: j/J.java package j;