JVM_IR: implement typeOf<T> as a codegen intrinsic

rather than a fake inline function.

Also, generate more correct instructions for typeOf. Not sure how that
even worked before - `aconst(Boolean)` isn't even valid.
This commit is contained in:
pyos
2021-05-27 16:18:06 +02:00
committed by max-kammerer
parent 7dbf08ae1c
commit eb4d831d27
12 changed files with 62 additions and 43 deletions
@@ -1342,10 +1342,10 @@ class ExpressionCodegen(
val classType = classReference.classType
val classifier = classType.classifierOrNull
if (classifier is IrTypeParameterSymbol) {
assert(classifier.owner.isReified) {
"Non-reified type parameter under ::class should be rejected by type checker: ${classifier.owner.dump()}"
val success = putReifiedOperationMarkerIfTypeIsReifiedParameter(classType, ReifiedTypeInliner.OperationKind.JAVA_CLASS)
assert(success) {
"Non-reified type parameter under ::class should be rejected by type checker: ${classType.render()}"
}
putReifiedOperationMarkerIfTypeIsReifiedParameter(classType, ReifiedTypeInliner.OperationKind.JAVA_CLASS)
}
generateClassInstance(mv, classType, typeMapper)
@@ -77,13 +77,13 @@ class IrInlineIntrinsicsSupport(
v.anew(implClass)
v.dup()
if (withArity) {
v.aconst(function.allParametersCount)
v.iconst(function.allParametersCount)
}
putClassInstance(v, FunctionReferenceLowering.getOwnerKClassType(declaration.parent, context))
v.aconst(declaration.name.asString())
// TODO: generate correct signature for functions and property accessors which have inline class types in the signature.
SignatureString.generateSignatureString(v, function, context)
v.aconst(FunctionReferenceLowering.getCallableReferenceTopLevelFlag(declaration))
v.iconst(FunctionReferenceLowering.getCallableReferenceTopLevelFlag(declaration))
val parameterTypes =
(if (withArity) listOf(INT_TYPE) else emptyList()) +
listOf(JAVA_CLASS_TYPE, JAVA_STRING_TYPE, JAVA_STRING_TYPE, INT_TYPE)
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.backend.jvm.codegen.MaterialValue
import org.jetbrains.kotlin.backend.jvm.codegen.materializedAt
import org.jetbrains.kotlin.codegen.AsmUtil
import org.jetbrains.kotlin.codegen.inline.ReifiedTypeInliner
import org.jetbrains.kotlin.codegen.putReifiedOperationMarkerIfTypeIsReifiedParameter
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
import org.jetbrains.org.objectweb.asm.Type
@@ -8,11 +8,12 @@ package org.jetbrains.kotlin.backend.jvm.intrinsics
import org.jetbrains.kotlin.backend.jvm.codegen.*
import org.jetbrains.kotlin.codegen.AsmUtil
import org.jetbrains.kotlin.codegen.inline.ReifiedTypeInliner
import org.jetbrains.kotlin.codegen.putReifiedOperationMarkerIfTypeIsReifiedParameter
import org.jetbrains.kotlin.ir.expressions.IrClassReference
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
import org.jetbrains.kotlin.ir.expressions.IrGetClass
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.util.dump
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
import org.jetbrains.org.objectweb.asm.Type
@@ -23,13 +24,13 @@ object GetJavaObjectType : IntrinsicMethod() {
is IrClassReference -> {
val symbol = receiver.symbol
if (symbol is IrTypeParameterSymbol) {
assert(symbol.owner.isReified) {
"Non-reified type parameter under ::class should be rejected by type checker: ${symbol.owner.dump()}"
}
codegen.putReifiedOperationMarkerIfTypeIsReifiedParameter(
val success = codegen.putReifiedOperationMarkerIfTypeIsReifiedParameter(
receiver.classType,
ReifiedTypeInliner.OperationKind.JAVA_CLASS
)
assert(success) {
"Non-reified type parameter under ::class should be rejected by type checker: ${receiver.render()}"
}
}
codegen.mv.aconst(AsmUtil.boxType(codegen.typeMapper.mapTypeAsDeclaration(receiver.classType)))
@@ -42,6 +42,7 @@ class IrIntrinsicMethods(val irBuiltIns: IrBuiltIns, val symbols: JvmSymbols) {
private val kotlinFqn = StandardNames.BUILT_INS_PACKAGE_FQ_NAME
private val kotlinJvmFqn = FqName("kotlin.jvm")
private val kotlinJvmInternalUnsafeFqn = FqName("kotlin.jvm.internal.unsafe")
private val kotlinReflectFqn = StandardNames.KOTLIN_REFLECT_FQ_NAME
private val anyFqn = StandardNames.FqNames.any.toSafe()
private val arrayFqn = StandardNames.FqNames.array.toSafe()
@@ -64,6 +65,7 @@ class IrIntrinsicMethods(val irBuiltIns: IrBuiltIns, val symbols: JvmSymbols) {
Key(kotlinFqn, null, "enumValues", listOf()) to EnumValues,
Key(kotlinFqn, null, "enumValueOf", listOf(stringFqn)) to EnumValueOf,
Key(kotlinFqn, stringFqn, "plus", listOf(anyFqn)) to StringPlus,
Key(kotlinReflectFqn, null, "typeOf", listOf()) to TypeOf,
irBuiltIns.eqeqSymbol.toKey()!! to Equals(KtTokens.EQEQ),
irBuiltIns.eqeqeqSymbol.toKey()!! to Equals(KtTokens.EQEQEQ),
irBuiltIns.ieee754equalsFunByOperandType[irBuiltIns.floatClass]!!.toKey()!! to Ieee754Equals(Type.FLOAT_TYPE),
@@ -9,6 +9,7 @@ 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.inline.ReifiedTypeInliner
import org.jetbrains.kotlin.codegen.putReifiedOperationMarkerIfTypeIsReifiedParameter
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
import org.jetbrains.kotlin.ir.types.getArrayElementType
import org.jetbrains.kotlin.ir.types.isArray
@@ -16,7 +17,7 @@ import org.jetbrains.kotlin.ir.types.isNullableArray
import org.jetbrains.org.objectweb.asm.Type
object NewArray : IntrinsicMethod() {
override fun invoke(expression: IrFunctionAccessExpression, codegen: ExpressionCodegen, data: BlockInfo): PromisedValue? {
override fun invoke(expression: IrFunctionAccessExpression, codegen: ExpressionCodegen, data: BlockInfo): PromisedValue {
codegen.gen(expression.getValueArgument(0)!!, Type.INT_TYPE, codegen.context.irBuiltIns.intType, data)
return with(codegen) {
val elementIrType = expression.type.getArrayElementType(context.irBuiltIns)
@@ -0,0 +1,26 @@
/*
* Copyright 2010-2021 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.IrInlineIntrinsicsSupport
import org.jetbrains.kotlin.codegen.inline.ReifiedTypeInliner
import org.jetbrains.kotlin.codegen.inline.generateTypeOf
import org.jetbrains.kotlin.codegen.putReifiedOperationMarkerIfTypeIsReifiedParameter
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
object TypeOf : IntrinsicMethod() {
override fun invoke(expression: IrFunctionAccessExpression, codegen: ExpressionCodegen, data: BlockInfo) = with(codegen) {
val type = expression.getTypeArgument(0)!!
if (putReifiedOperationMarkerIfTypeIsReifiedParameter(type, ReifiedTypeInliner.OperationKind.TYPE_OF)) {
mv.aconst(null) // see ReifiedTypeInliner.processTypeOf
} else {
typeMapper.typeSystem.generateTypeOf(mv, type, IrInlineIntrinsicsSupport(context, typeMapper))
}
expression.onStack
}
}