JVM IR: remove unused code, minor cleanup

This commit is contained in:
Alexander Udalov
2019-08-12 17:43:29 +02:00
parent 23e79bb32b
commit 7c4ed9c29e
11 changed files with 19 additions and 256 deletions
@@ -18,8 +18,6 @@ package org.jetbrains.kotlin.backend.jvm
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOriginImpl
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
import org.jetbrains.kotlin.ir.expressions.IrStatementOriginImpl
interface JvmLoweredDeclarationOrigin : IrDeclarationOrigin {
object CLASS_STATIC_INITIALIZER : IrDeclarationOriginImpl("CLASS_STATIC_INITIALIZER")
@@ -43,8 +41,3 @@ interface JvmLoweredDeclarationOrigin : IrDeclarationOrigin {
object SYNTHETIC_INLINE_CLASS_MEMBER : IrDeclarationOriginImpl("SYNTHETIC_INLINE_CLASS_MEMBER", isSynthetic = true)
object GENERATED_ASSERTION_ENABLED_FIELD : IrDeclarationOriginImpl("GENERATED_ASSERTION_ENABLED_FIELD", isSynthetic = true)
}
interface JvmLoweredStatementOrigin : IrStatementOrigin {
object DEFAULT_IMPLS_DELEGATION : IrStatementOriginImpl("DEFAULT_IMPL_DELEGATION")
object TO_ARRAY : IrDeclarationOriginImpl("TO_ARRAY")
}
@@ -291,12 +291,6 @@ class JvmSymbols(
val getOrCreateKotlinPackage: IrSimpleFunctionSymbol =
reflection.functionByName("getOrCreateKotlinPackage")
val getOrCreateKotlinClass: IrSimpleFunctionSymbol =
reflection.functionByName("getOrCreateKotlinClass")
val getOrCreateKotlinClasses: IrSimpleFunctionSymbol =
reflection.functionByName("getOrCreateKotlinClasses")
val desiredAssertionStatus: IrSimpleFunctionSymbol by lazy {
javaLangClass.functionByName("desiredAssertionStatus")
}
@@ -1,111 +0,0 @@
/*
* Copyright 2000-2018 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.codegen.AsmUtil
import org.jetbrains.kotlin.codegen.Callable
import org.jetbrains.kotlin.codegen.CallableMethod
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.org.objectweb.asm.Type
import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
import java.lang.UnsupportedOperationException
open class IntrinsicCallable(
override val returnType: Type,
override val valueParameterTypes: List<Type>,
override val dispatchReceiverType: Type?,
override val extensionReceiverType: Type?,
private val invoke: IntrinsicCallable.(v: InstructionAdapter) -> Unit = { throw UnsupportedOperationException() }
) : Callable {
constructor(
callable: CallableMethod,
invoke: IntrinsicCallable.(v: InstructionAdapter) -> Unit = {}
) : this(
callable.returnType,
callable.valueParameterTypes,
callable.dispatchReceiverType,
callable.extensionReceiverType,
invoke
)
override fun genInvokeInstruction(v: InstructionAdapter) {
invokeIntrinsic(v)
}
open fun invokeIntrinsic(v: InstructionAdapter) {
invoke(v)
}
override val parameterTypes: Array<Type>
get() = throw UnsupportedOperationException()
override val dispatchReceiverKotlinType: KotlinType?
get() = null
override val extensionReceiverKotlinType: KotlinType?
get() = null
override val returnKotlinType: KotlinType?
get() = null
override fun isStaticCall() = false
override val generateCalleeType: Type?
get() = null
override val owner: Type
get() = throw UnsupportedOperationException()
fun calcReceiverType(): Type =
extensionReceiverType ?: dispatchReceiverType!!
}
fun createBinaryIntrinsicCallable(
returnType: Type,
valueParameterType: Type,
thisType: Type? = null,
receiverType: Type? = null,
lambda: IntrinsicCallable.(v: InstructionAdapter) -> Unit
): IntrinsicCallable {
assert(AsmUtil.isPrimitive(returnType)) { "Return type of BinaryOp intrinsic should be of primitive type: $returnType" }
return object : IntrinsicCallable(returnType, listOf(valueParameterType), thisType, receiverType) {
override fun invokeIntrinsic(v: InstructionAdapter) {
lambda(v)
}
}
}
fun createUnaryIntrinsicCallable(
callable: CallableMethod,
newReturnType: Type? = null,
needPrimitiveCheck: Boolean = false,
newThisType: Type? = null,
invoke: IntrinsicCallable.(v: InstructionAdapter) -> Unit
): IntrinsicCallable {
val intrinsic = IntrinsicCallable(
newReturnType ?: callable.returnType,
callable.valueParameterTypes,
newThisType ?: callable.dispatchReceiverType,
callable.extensionReceiverType,
invoke
)
assert(intrinsic.valueParameterTypes.isEmpty()) { "Unary operation should not have any parameters" }
if (needPrimitiveCheck) {
assert(AsmUtil.isPrimitive(intrinsic.returnType)) {
"Return type of UnaryPlus intrinsic should be of primitive type: ${intrinsic.returnType}"
}
}
return intrinsic
}
fun createIntrinsicCallable(
callable: CallableMethod,
invoke: IntrinsicCallable.(v: InstructionAdapter) -> Unit
): IntrinsicCallable {
return IntrinsicCallable(callable, invoke)
}
@@ -13,7 +13,6 @@ import org.jetbrains.kotlin.codegen.Callable
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
import org.jetbrains.kotlin.ir.types.toKotlinType
import org.jetbrains.kotlin.resolve.calls.components.isVararg
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
import org.jetbrains.kotlin.types.KotlinType
@@ -145,7 +144,3 @@ fun IrFunctionAccessExpression.receiverAndArgs(): List<IrExpression> {
return (arrayListOf(this.dispatchReceiver, this.extensionReceiver) +
symbol.owner.valueParameters.mapIndexed { i, _ -> getValueArgument(i) }).filterNotNull()
}
fun List<IrExpression>.asmTypes(context: JvmBackendContext): List<Type> {
return map { context.state.typeMapper.mapType(it.type.toKotlinType()) }
}
@@ -33,39 +33,36 @@ import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.types.SimpleType
import org.jetbrains.kotlin.types.expressions.OperatorConventions
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.org.objectweb.asm.Opcodes.*
import org.jetbrains.org.objectweb.asm.Type
class IrIntrinsicMethods(val irBuiltIns: IrBuiltIns, val symbols: JvmSymbols) {
private val KOTLIN_INTERNAL_IR = FqName("kotlin.internal.ir")
private val KOTLIN_JVM = FqName("kotlin.jvm")
private val KOTLIN_JVM_INTERNAL_UNSAFE = FqName("kotlin.jvm.internal.unsafe")
private val kotlinJvm = FqName("kotlin.jvm")
private val kotlinJvmInternalUnsafe = FqName("kotlin.jvm.internal.unsafe")
private val intrinsicsMap = (
listOf(
Key(KOTLIN_JVM, FqName("T"), "<get-javaClass>", emptyList()) to JavaClassProperty,
Key(kotlinJvm, FqName("T"), "<get-javaClass>", emptyList()) to JavaClassProperty,
Key(
KOTLIN_JVM,
kotlinJvm,
KotlinBuiltIns.FQ_NAMES.kClass.toSafe(),
"<get-java>",
emptyList()
) to KClassJavaProperty,
Key(
KOTLIN_JVM_INTERNAL_UNSAFE,
kotlinJvmInternalUnsafe,
null,
"monitorEnter",
listOf(KotlinBuiltIns.FQ_NAMES.any.toSafe())
) to MonitorInstruction.MONITOR_ENTER,
Key(
KOTLIN_JVM_INTERNAL_UNSAFE,
kotlinJvmInternalUnsafe,
null,
"monitorExit",
listOf(KotlinBuiltIns.FQ_NAMES.any.toSafe())
) to MonitorInstruction.MONITOR_EXIT,
Key(
KOTLIN_JVM,
kotlinJvm,
KotlinBuiltIns.FQ_NAMES.array.toSafe(),
"isArrayOf",
emptyList()
@@ -35,7 +35,10 @@ import org.jetbrains.kotlin.ir.symbols.impl.IrEnumEntrySymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrExternalPackageFragmentSymbolImpl
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
import org.jetbrains.kotlin.ir.types.typeWith
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.ir.util.getAnnotation
import org.jetbrains.kotlin.ir.util.hasAnnotation
import org.jetbrains.kotlin.ir.util.isAnnotationClass
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
@@ -112,8 +115,6 @@ private class AdditionalClassAnnotationLowering(private val context: JvmBackendC
private val etField = buildEnumEntry(elementTypeEnum, "FIELD")
private val etLocalVariable = buildEnumEntry(elementTypeEnum, "LOCAL_VARIABLE")
private val etMethod = buildEnumEntry(elementTypeEnum, "METHOD")
private val etModule = buildEnumEntry(elementTypeEnum, "MODULE")
private val etPackage = buildEnumEntry(elementTypeEnum, "PACKAGE")
private val etParameter = buildEnumEntry(elementTypeEnum, "PARAMETER")
private val etType = buildEnumEntry(elementTypeEnum, "TYPE")
private val etTypeParameter = buildEnumEntry(elementTypeEnum, "TYPE_PARAMETER")
@@ -397,7 +397,4 @@ internal class CallableReferenceLowering(private val context: JvmBackendContext)
private val functionGetOwner =
context.ir.symbols.functionReference.functionByName("getOwner")
private val functionNInvokeFun =
context.ir.symbols.functionN.functionByName("invoke")
}
@@ -10,7 +10,10 @@ import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrConst
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrStringConcatenation
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrStringConcatenationImpl
import org.jetbrains.kotlin.ir.types.*
@@ -45,18 +48,14 @@ class FoldConstantLowering(private val context: JvmBackendContext) : IrElementTr
val operatorName: String
)
@Suppress("unused")
private data class PrimitiveType<T>(val name: String)
companion object {
private val BYTE = PrimitiveType<Byte>("Byte")
private val SHORT = PrimitiveType<Short>("Short")
private val INT = PrimitiveType<Int>("Int")
private val LONG = PrimitiveType<Long>("Long")
private val DOUBLE = PrimitiveType<Double>("Double")
private val FLOAT = PrimitiveType<Float>("Float")
private val CHAR = PrimitiveType<Char>("Char")
private val BOOLEAN = PrimitiveType<Boolean>("Boolean")
private val STRING = PrimitiveType<String>("String")
private val BINARY_OP_TO_EVALUATOR = HashMap<BinaryOp, Function2<Any?, Any?, Any>>()
@@ -1,42 +0,0 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.backend.jvm.lower
import org.jetbrains.annotations.NotNull
import org.jetbrains.annotations.Nullable
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.FunctionDescriptorImpl
import org.jetbrains.kotlin.name.Name
interface LoweredFunction: FunctionDescriptor
class LoweredFunctionImpl(containingDeclaration: DeclarationDescriptor,
original: FunctionDescriptor,
annotations: Annotations,
name: Name,
kind: CallableMemberDescriptor.Kind,
source: SourceElement) :
FunctionDescriptorImpl(containingDeclaration, original, annotations, name, kind, source), LoweredFunction {
override fun getDispatchReceiverParameter(): ReceiverParameterDescriptor? {
return super.getDispatchReceiverParameter()
}
override fun createSubstitutedCopy(newOwner: DeclarationDescriptor, original: FunctionDescriptor?, kind: CallableMemberDescriptor.Kind, newName: Name?, annotations: Annotations, source: SourceElement): FunctionDescriptorImpl {
TODO("not implemented")
}
}
@@ -63,7 +63,9 @@ internal class PropertyReferenceLowering(val context: JvmBackendContext) : Class
get() = "${JvmAbi.getterName(name.asString())}()${context.state.typeMapper.mapReturnType(descriptor)}"
private val IrMemberAccessExpression.signature: String
get() = getter?.let { getter -> localPropertyIndices[getter]?.let { "<v#$it>" } } ?: getter?.owner?.signature ?: field!!.owner.signature
get() = getter?.let { getter ->
localPropertyIndices[getter]?.let { "<v#$it>" }
} ?: getter?.owner?.signature ?: field!!.owner.signature
private val arrayItemGetter =
context.ir.symbols.array.owner.functions.single { it.name.asString() == "get" }
@@ -1,62 +0,0 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.backend.jvm.lower
import org.jetbrains.kotlin.backend.common.lower.InitializersLowering
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl
import org.jetbrains.kotlin.descriptors.impl.ValueParameterDescriptorImpl
import org.jetbrains.kotlin.name.Name
fun FunctionDescriptor.toStatic(
newOwner: ClassOrPackageFragmentDescriptor,
name: Name = this.name,
dispatchReceiverClass: ClassDescriptor? = this.containingDeclaration as? ClassDescriptor
): FunctionDescriptor {
val newFunction = SimpleFunctionDescriptorImpl.create(
newOwner, Annotations.EMPTY,
name,
CallableMemberDescriptor.Kind.DECLARATION, this.source
)
var offset = 0
val dispatchReceiver = dispatchReceiverParameter?.let {
ValueParameterDescriptorImpl.createWithDestructuringDeclarations(
newFunction, null, offset++, Annotations.EMPTY, Name.identifier("this"),
dispatchReceiverClass!!.defaultType, false, false, false, null, dispatchReceiverClass.source, null
)
}
val extensionReceiver = extensionReceiverParameter?.let {
ValueParameterDescriptorImpl.createWithDestructuringDeclarations(
newFunction, null, offset++, Annotations.EMPTY, Name.identifier("receiver"),
it.value.type, false, false, false, null, it.source, null
)
}
val valueParameters = listOfNotNull(dispatchReceiver, extensionReceiver) +
valueParameters.map { it.copy(newFunction, it.name, it.index + offset) }
newFunction.initialize(
null, null, emptyList()/*TODO: type parameters*/,
valueParameters, returnType, Modality.FINAL, Visibilities.PUBLIC
)
return newFunction
}
fun FunctionDescriptor.isClInit(): Boolean = this.name == InitializersLowering.clinitName