[Wasm] Copy property reference lowering from K/N

This commit is contained in:
Svyatoslav Kuzmich
2021-09-27 17:07:39 +03:00
parent 33314b25c0
commit 5c05ff48ff
93 changed files with 376 additions and 151 deletions
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.backend.common.ir
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.deepCopyWithVariables
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.DescriptorVisibility
@@ -699,3 +700,28 @@ fun IrExpression?.isPure(
return false
}
fun CommonBackendContext.createArrayOfExpression(
startOffset: Int, endOffset: Int,
arrayElementType: IrType,
arrayElements: List<IrExpression>
): IrExpression {
val arrayType = ir.symbols.array.typeWith(arrayElementType)
val arg0 = IrVarargImpl(startOffset, endOffset, arrayType, arrayElementType, arrayElements)
return IrCallImpl(
startOffset,
endOffset,
arrayType,
ir.symbols.arrayOf,
1,
1
).apply {
putTypeArgument(0, arrayElementType)
putValueArgument(0, arg0)
}
}
fun IrBuiltIns.getKFunctionType(returnType: IrType, parameterTypes: List<IrType>) =
kFunctionN(parameterTypes.size).typeWith(parameterTypes + returnType)
@@ -179,6 +179,12 @@ private val sharedVariablesLoweringPhase = makeWasmModulePhase(
description = "Box captured mutable variables"
)
private val propertyReferenceLowering = makeWasmModulePhase(
::WasmPropertyReferenceLowering,
name = "WasmPropertyReferenceLowering",
description = "Lower property references"
)
private val callableReferencePhase = makeWasmModulePhase(
::WasmCallableReferenceLowering,
name = "WasmCallableReferenceLowering",
@@ -454,6 +460,7 @@ val wasmPhases = NamedCompilerPhase(
enumClassConstructorBodyLoweringPhase then
sharedVariablesLoweringPhase then
propertyReferenceLowering then
callableReferencePhase then
localDelegatedPropertiesLoweringPhase then
localDeclarationsLoweringPhase then
@@ -18,6 +18,8 @@ import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.types.defaultType
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.typeWith
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
@@ -146,6 +148,21 @@ class WasmSymbols(
val exportString = getInternalFunction("exportString")
val unsafeGetScratchRawMemory = getInternalFunction("unsafeGetScratchRawMemory")
// KProperty implementations
val kLocalDelegatedPropertyImpl: IrClassSymbol = this.getInternalClass("KLocalDelegatedPropertyImpl")
val kLocalDelegatedMutablePropertyImpl: IrClassSymbol = this.getInternalClass("KLocalDelegatedMutablePropertyImpl")
val kProperty0Impl: IrClassSymbol = this.getInternalClass("KProperty0Impl")
val kProperty1Impl: IrClassSymbol = this.getInternalClass("KProperty1Impl")
val kProperty2Impl: IrClassSymbol = this.getInternalClass("KProperty2Impl")
val kMutableProperty0Impl: IrClassSymbol = this.getInternalClass("KMutableProperty0Impl")
val kMutableProperty1Impl: IrClassSymbol = this.getInternalClass("KMutableProperty1Impl")
val kMutableProperty2Impl: IrClassSymbol = this.getInternalClass("KMutableProperty2Impl")
val kMutableProperty0: IrClassSymbol = getIrClass(FqName("kotlin.reflect.KMutableProperty0"))
val kMutableProperty1: IrClassSymbol = getIrClass(FqName("kotlin.reflect.KMutableProperty1"))
val kMutableProperty2: IrClassSymbol = getIrClass(FqName("kotlin.reflect.KMutableProperty2"))
val kTypeStub = getInternalFunction("kTypeStub")
private val functionNInterfaces = (0..22).map { arity ->
getIrClass(FqName("kotlin.wasm.internal.Function$arity"))
}
@@ -187,4 +204,8 @@ class WasmSymbols(
private fun getInternalFunction(name: String) = getFunction(name, wasmInternalPackage)
private fun getIrClass(fqName: FqName): IrClassSymbol = symbolTable.referenceClass(getClass(fqName))
private fun getInternalClass(name: String): IrClassSymbol = getIrClass(FqName("kotlin.wasm.internal.$name"))
fun getKFunctionType(type: IrType, list: List<IrType>): IrType {
return irBuiltIns.functionN(list.size).typeWith(list + type)
}
}