[Wasm] Major compiler and stdlib update

This commit is contained in:
Svyatoslav Kuzmich
2020-04-27 14:39:20 +03:00
parent 3be38d1796
commit bfd0f21e9d
196 changed files with 12635 additions and 4774 deletions
@@ -12,6 +12,7 @@ dependencies {
compile(project(":compiler:ir.backend.common"))
compile(project(":compiler:ir.serialization.common"))
compile(project(":compiler:ir.serialization.js"))
compile(project(":compiler:ir.tree.persistent"))
compile(project(":js:js.ast"))
compile(project(":js:js.frontend"))
compile(project(":compiler:backend.js"))
@@ -7,24 +7,36 @@ package org.jetbrains.kotlin.backend.wasm
import org.jetbrains.kotlin.backend.common.ir.Ir
import org.jetbrains.kotlin.backend.common.ir.Symbols
import org.jetbrains.kotlin.backend.common.ir.addChild
import org.jetbrains.kotlin.backend.wasm.lower.WasmSharedVariablesManager
import org.jetbrains.kotlin.backend.wasm.utils.WasmInlineClassesUtils
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.impl.EmptyPackageFragmentDescriptor
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.SourceManager
import org.jetbrains.kotlin.ir.SourceRangeInfo
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.backend.js.JsCommonBackendContext
import org.jetbrains.kotlin.ir.backend.js.JsMapping
import org.jetbrains.kotlin.ir.backend.js.JsSharedVariablesManager
import org.jetbrains.kotlin.ir.backend.js.lower.JsInnerClassesSupport
import org.jetbrains.kotlin.ir.builders.declarations.addFunction
import org.jetbrains.kotlin.ir.builders.declarations.buildFun
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
import org.jetbrains.kotlin.ir.declarations.persistent.PersistentIrFactory
import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.impl.DescriptorlessExternalPackageFragmentSymbol
import org.jetbrains.kotlin.ir.util.IdSignature
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
class WasmBackendContext(
val module: ModuleDescriptor,
@@ -43,10 +55,12 @@ class WasmBackendContext(
override val irFactory: IrFactory = PersistentIrFactory
// Place to store declarations excluded from code generation
val excludedDeclarations: IrPackageFragment by lazy {
private val excludedDeclarations = mutableMapOf<FqName, IrPackageFragment>()
fun getExcludedPackageFragment(fqName: FqName): IrPackageFragment = excludedDeclarations.getOrPut(fqName) {
IrExternalPackageFragmentImpl(
DescriptorlessExternalPackageFragmentSymbol(),
FqName("kotlin")
fqName
)
}
@@ -54,14 +68,44 @@ class WasmBackendContext(
val innerClassesSupport = JsInnerClassesSupport(mapping, irFactory)
val objectToGetInstanceFunction = mutableMapOf<IrClassSymbol, IrSimpleFunction>()
override val internalPackageFqn = FqName("kotlin.wasm")
private val internalPackageFragment = IrExternalPackageFragmentImpl.createEmptyExternalPackageFragment(
builtIns.builtInsModule, FqName("kotlin.wasm.internal")
)
private val internalPackageFragmentDescriptor = EmptyPackageFragmentDescriptor(builtIns.builtInsModule, FqName("kotlin.wasm.internal"))
// TODO: Merge with JS IR Backend context lazy file
val internalPackageFragment by lazy {
IrFileImpl(object : SourceManager.FileEntry {
override val name = "<implicitDeclarations>"
override val maxOffset = UNDEFINED_OFFSET
override val sharedVariablesManager = JsSharedVariablesManager(TODO("..."))
override fun getSourceRangeInfo(beginOffset: Int, endOffset: Int) =
SourceRangeInfo(
"",
UNDEFINED_OFFSET,
UNDEFINED_OFFSET,
UNDEFINED_OFFSET,
UNDEFINED_OFFSET,
UNDEFINED_OFFSET,
UNDEFINED_OFFSET
)
override fun getLineNumber(offset: Int) = UNDEFINED_OFFSET
override fun getColumnNumber(offset: Int) = UNDEFINED_OFFSET
}, internalPackageFragmentDescriptor).also {
irModuleFragment.files += it
}
}
val startFunction = irFactory.buildFun {
name = Name.identifier("startFunction")
returnType = irBuiltIns.unitType
}.apply {
body = irFactory.createBlockBody(UNDEFINED_OFFSET, UNDEFINED_OFFSET)
internalPackageFragment.addChild(this)
}
override val sharedVariablesManager =
WasmSharedVariablesManager(this, irBuiltIns, internalPackageFragment)
val wasmSymbols: WasmSymbols = WasmSymbols(this@WasmBackendContext, symbolTable)
override val ir = object : Ir<WasmBackendContext>(this, irModuleFragment) {
@@ -69,6 +113,8 @@ class WasmBackendContext(
override fun shouldGenerateHandlerParameterForDefaultBodyFun() = true
}
override val inlineClassesUtils = WasmInlineClassesUtils(wasmSymbols)
override fun log(message: () -> String) {
/*TODO*/
if (inVerbosePhase) print(message())
@@ -5,23 +5,16 @@
package org.jetbrains.kotlin.backend.wasm
import org.jetbrains.kotlin.backend.common.ClassLoweringPass
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.lower
import org.jetbrains.kotlin.backend.common.lower.*
import org.jetbrains.kotlin.backend.common.lower.inline.FunctionInlining
import org.jetbrains.kotlin.backend.common.phaser.*
import org.jetbrains.kotlin.backend.common.runOnFilePostfix
import org.jetbrains.kotlin.backend.wasm.lower.BuiltInsLowering
import org.jetbrains.kotlin.backend.wasm.lower.WasmBlockDecomposerLowering
import org.jetbrains.kotlin.backend.wasm.lower.excludeDeclarationsFromCodegen
import org.jetbrains.kotlin.backend.wasm.lower.*
import org.jetbrains.kotlin.ir.backend.js.lower.*
import org.jetbrains.kotlin.ir.backend.js.lower.inline.RemoveInlineFunctionsWithReifiedTypeParametersLowering
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.util.patchDeclarationParents
private fun ClassLoweringPass.runOnFilesPostfix(moduleFragment: IrModuleFragment) = moduleFragment.files.forEach { runOnFilePostfix(it) }
private fun makeWasmModulePhase(
lowering: (WasmBackendContext) -> FileLoweringPass,
name: String,
@@ -60,6 +53,12 @@ private val expectDeclarationsRemovingPhase = makeWasmModulePhase(
description = "Remove expect declaration from module fragment"
)
private val stringConstructorLowering = makeWasmModulePhase(
::SimpleStringConcatenationLowering,
name = "StringConcatenation",
description = "String concatenation lowering"
)
private val lateinitNullableFieldsPhase = makeWasmModulePhase(
::NullableFieldsForLateinitCreationLowering,
name = "LateinitNullableFields",
@@ -86,12 +85,6 @@ private val provisionalFunctionExpressionPhase = makeWasmModulePhase(
description = "Transform IrFunctionExpression to a local function reference"
)
private val arrayConstructorPhase = makeWasmModulePhase(
::ArrayConstructorLowering,
name = "ArrayConstructor",
description = "Transform `Array(size) { index -> value }` into a loop"
)
private val functionInliningPhase = makeCustomWasmModulePhase(
{ context, module ->
FunctionInlining(context).inline(module)
@@ -112,7 +105,7 @@ private val removeInlineFunctionsWithReifiedTypeParametersLoweringPhase = makeWa
private val tailrecLoweringPhase = makeWasmModulePhase(
::TailrecLowering,
name = "TailrecLowering",
description = "Replace `tailrec` callsites with equivalent loop"
description = "Replace `tailrec` call sites with equivalent loop"
)
private val enumClassConstructorLoweringPhase = makeWasmModulePhase(
@@ -121,6 +114,62 @@ private val enumClassConstructorLoweringPhase = makeWasmModulePhase(
description = "Transform Enum Class into regular Class"
)
private val enumClassConstructorBodyLoweringPhase = makeWasmModulePhase(
::EnumClassConstructorBodyTransformer,
name = "EnumClassConstructorBodyLowering",
description = "Transform Enum Class into regular Class"
)
private val enumEntryInstancesLoweringPhase = makeWasmModulePhase(
::EnumEntryInstancesLowering,
name = "EnumEntryInstancesLowering",
description = "Create instance variable for each enum entry initialized with `null`",
prerequisite = setOf(enumClassConstructorLoweringPhase)
)
private val enumEntryInstancesBodyLoweringPhase = makeWasmModulePhase(
::EnumEntryInstancesBodyLowering,
name = "EnumEntryInstancesBodyLowering",
description = "Insert enum entry field initialization into corresponding class constructors",
prerequisite = setOf(enumEntryInstancesLoweringPhase)
)
private val enumClassCreateInitializerLoweringPhase = makeWasmModulePhase(
::EnumClassCreateInitializerLowering,
name = "EnumClassCreateInitializerLowering",
description = "Create initializer for enum entries",
prerequisite = setOf(enumClassConstructorLoweringPhase)
)
private val enumEntryCreateGetInstancesFunsLoweringPhase = makeWasmModulePhase(
::EnumEntryCreateGetInstancesFunsLowering,
name = "EnumEntryCreateGetInstancesFunsLowering",
description = "Create enumEntry_getInstance functions",
prerequisite = setOf(enumClassConstructorLoweringPhase)
)
private val enumSyntheticFunsLoweringPhase = makeWasmModulePhase(
::EnumSyntheticFunctionsLowering,
name = "EnumSyntheticFunctionsLowering",
description = "Implement `valueOf` and `values`",
prerequisite = setOf(enumClassConstructorLoweringPhase)
)
private val enumUsageLoweringPhase = makeWasmModulePhase(
::EnumUsageLowering,
name = "EnumUsageLowering",
description = "Replace enum access with invocation of corresponding function",
prerequisite = setOf(enumEntryCreateGetInstancesFunsLoweringPhase)
)
private val enumEntryRemovalLoweringPhase = makeWasmModulePhase(
::EnumClassRemoveEntriesLowering,
name = "EnumEntryRemovalLowering",
description = "Replace enum entry with corresponding class",
prerequisite = setOf(enumUsageLoweringPhase)
)
private val sharedVariablesLoweringPhase = makeWasmModulePhase(
::SharedVariablesLowering,
@@ -128,6 +177,12 @@ private val sharedVariablesLoweringPhase = makeWasmModulePhase(
description = "Box captured mutable variables"
)
private val callableReferencePhase = makeWasmModulePhase(
::WasmCallableReferenceLowering,
name = "WasmCallableReferenceLowering",
description = "Handle callable references"
)
private val localDelegatedPropertiesLoweringPhase = makeWasmModulePhase(
{ LocalDelegatedPropertiesLowering() },
name = "LocalDelegatedPropertiesLowering",
@@ -183,7 +238,7 @@ private val defaultArgumentPatchOverridesPhase = makeWasmModulePhase(
private val defaultParameterInjectorPhase = makeWasmModulePhase(
{ context -> DefaultParameterInjector(context, skipExternalMethods = true) },
name = "DefaultParameterInjector",
description = "Replace callsite with default parameters with corresponding stub function",
description = "Replace call site with default parameters with corresponding stub function",
prerequisite = setOf(innerClassesLoweringPhase)
)
@@ -193,18 +248,6 @@ private val defaultParameterCleanerPhase = makeWasmModulePhase(
description = "Clean default parameters up"
)
//private val jsDefaultCallbackGeneratorPhase = makeJsModulePhase(
// ::JsDefaultCallbackGenerator,
// name = "JsDefaultCallbackGenerator",
// description = "Build binding for super calls with default parameters"
//)
//private val varargLoweringPhase = makeJsModulePhase(
// ::VarargLowering,
// name = "VarargLowering",
// description = "Lower vararg arguments"
//)
private val propertiesLoweringPhase = makeWasmModulePhase(
{ PropertiesLowering() },
name = "PropertiesLowering",
@@ -254,7 +297,7 @@ private val returnableBlockLoweringPhase = makeWasmModulePhase(
)
private val bridgesConstructionPhase = makeWasmModulePhase(
::BridgesConstruction,
::WasmBridgesConstruction,
name = "BridgesConstruction",
description = "Generate bridges"
)
@@ -271,65 +314,57 @@ private val inlineClassUsageLoweringPhase = makeWasmModulePhase(
description = "Handle inline class usages"
)
//private val autoboxingTransformerPhase = makeJsModulePhase(
// ::AutoboxingTransformer,
// name = "AutoboxingTransformer",
// description = "Insert box/unbox intrinsics"
//)
private val blockDecomposerLoweringPhase = makeCustomWasmModulePhase(
{ context, module ->
WasmBlockDecomposerLowering(context).lower(module)
module.patchDeclarationParents()
},
name = "BlockDecomposerLowering",
description = "Transform statement-like-expression nodes into pure-statement to make it easily transform into JS"
private val autoboxingTransformerPhase = makeWasmModulePhase(
{ context -> AutoboxingTransformer(context) },
name = "AutoboxingTransformer",
description = "Insert box/unbox intrinsics"
)
private val wasmNullSpecializationLowering = makeWasmModulePhase(
{ context -> WasmNullCoercingLowering(context) },
name = "WasmNullCoercingLowering",
description = "Specialize assigning Nothing? values to other types."
)
private val staticMembersLoweringPhase = makeWasmModulePhase(
::StaticMembersLowering,
name = "StaticMembersLowering",
description = "Move static member declarations to top-level"
)
private val wasmVarargExpressionLoweringPhase = makeWasmModulePhase(
::WasmVarargExpressionLowering,
name = "WasmVarargExpressionLowering",
description = "Lower varargs"
)
private val wasmThrowDebugLoweringPhase = makeWasmModulePhase(
::WasmThrowDebugLowering,
name = "WasmThrowDebugLowering",
description = "Instrument throws with debug print information"
)
private val fieldInitializersLoweringPhase = makeWasmModulePhase(
::FieldInitializersLowering,
name = "FieldInitializersLowering",
description = "Move field initializers to start function"
)
private val builtInsLoweringPhase0 = makeWasmModulePhase(
::BuiltInsLowering,
name = "BuiltInsLowering0",
description = "Lower IR builtins 0"
)
//private val classReferenceLoweringPhase = makeJsModulePhase(
// ::ClassReferenceLowering,
// name = "ClassReferenceLowering",
// description = "Handle class references"
//)
//
//private val primitiveCompanionLoweringPhase = makeJsModulePhase(
// ::PrimitiveCompanionLowering,
// name = "PrimitiveCompanionLowering",
// description = "Replace common companion object access with platform one"
//)
//
//private val constLoweringPhase = makeJsModulePhase(
// ::ConstLowering,
// name = "ConstLowering",
// description = "Wrap Long and Char constants into constructor invocation"
//)
//
//private val callsLoweringPhase = makeJsModulePhase(
// ::CallsLowering,
// name = "CallsLowering",
// description = "Handle intrinsics"
//)
//
//private val testGenerationPhase = makeJsModulePhase(
// ::TestGenerator,
// name = "TestGenerationLowering",
// description = "Generate invocations to kotlin.test suite and test functions"
//)
//
//private val staticMembersLoweringPhase = makeWasmModulePhase(
// ::StaticMembersLowering,
// name = "StaticMembersLowering",
// description = "Move static member declarations to top-level"
//)
private val builtInsLoweringPhase = makeWasmModulePhase(
::BuiltInsLowering,
name = "BuiltInsLowering",
description = "Lower IR buildins"
description = "Lower IR builtins"
)
private val objectDeclarationLoweringPhase = makeWasmModulePhase(
::ObjectUsageLowering,
::ObjectDeclarationLowering,
name = "ObjectDeclarationLowering",
description = "Create lazy object instance generator functions"
)
@@ -340,26 +375,52 @@ private val objectUsageLoweringPhase = makeWasmModulePhase(
description = "Transform IrGetObjectValue into instance generator call"
)
private val typeOperatorLoweringPhase = makeWasmModulePhase(
::WasmTypeOperatorLowering,
name = "TypeOperatorLowering",
description = "Lower IrTypeOperator with corresponding logic"
)
private val genericReturnTypeLowering = makeWasmModulePhase(
::GenericReturnTypeLowering,
name = "GenericReturnTypeLowering",
description = "Cast calls to functions with generic return types"
)
private val eraseVirtualDispatchReceiverParametersTypes = makeWasmModulePhase(
::EraseVirtualDispatchReceiverParametersTypes,
name = "EraseVirtualDispatchReceiverParametersTypes",
description = "Erase types of virtual dispatch receivers to Any"
)
private val virtualDispatchReceiverExtractionPhase = makeWasmModulePhase(
::VirtualDispatchReceiverExtraction,
name = "VirtualDispatchReceiverExtraction",
description = "Eliminate side-effects in dispatch receivers of virtual function calls"
)
val wasmPhases = NamedCompilerPhase(
name = "IrModuleLowering",
description = "IR module lowering",
lower = validateIrBeforeLowering then
excludeDeclarationsFromCodegenPhase then
expectDeclarationsRemovingPhase then
provisionalFunctionExpressionPhase then
// TODO: Need some helpers from stdlib
// arrayConstructorPhase then
functionInliningPhase then
provisionalFunctionExpressionPhase then
lateinitNullableFieldsPhase then
lateinitDeclarationLoweringPhase then
lateinitUsageLoweringPhase then
tailrecLoweringPhase then
enumClassConstructorLoweringPhase then
enumClassConstructorBodyLoweringPhase then
sharedVariablesLoweringPhase then
callableReferencePhase then
localDelegatedPropertiesLoweringPhase then
localDeclarationsLoweringPhase then
localClassExtractionPhase then
@@ -373,68 +434,52 @@ val wasmPhases = NamedCompilerPhase(
initializersCleanupLoweringPhase then
// Common prefix ends
builtInsLoweringPhase then
// TODO: Commonize enumEntryToGetInstanceFunction
// Commonize array literal creation
// Extract external enum lowering to JS part
//
// enumClassLoweringPhase then
// enumUsageLoweringPhase then
enumEntryInstancesLoweringPhase then
enumEntryInstancesBodyLoweringPhase then
enumClassCreateInitializerLoweringPhase then
enumEntryCreateGetInstancesFunsLoweringPhase then
enumSyntheticFunsLoweringPhase then
enumUsageLoweringPhase then
enumEntryRemovalLoweringPhase then
// TODO: Requires stdlib
// suspendFunctionsLoweringPhase then
stringConstructorLowering then
returnableBlockLoweringPhase then
// TODO: Callable reference lowering is too JS specific.
// Should we reuse JVM or Native lowering?
// callableReferenceLoweringPhase then
defaultArgumentStubGeneratorPhase then
defaultArgumentPatchOverridesPhase then
defaultParameterInjectorPhase then
defaultParameterCleanerPhase then
// TODO: Investigate
// jsDefaultCallbackGeneratorPhase then
removeInlineFunctionsWithReifiedTypeParametersLoweringPhase then
// TODO: Varargs are too platform-specific. Reimplement.
// varargLoweringPhase then
// TODO: Investigate exception proposal
// TODO:
// multipleCatchesLoweringPhase then
bridgesConstructionPhase then
// TODO: Reimplement
// typeOperatorLoweringPhase then
// TODO: Reimplement
// secondaryConstructorLoweringPhase then
// secondaryFactoryInjectorLoweringPhase then
// TODO: Reimplement
// classReferenceLoweringPhase then
wasmVarargExpressionLoweringPhase then
inlineClassDeclarationLoweringPhase then
inlineClassUsageLoweringPhase then
// TODO: Commonize box/unbox intrinsics
// autoboxingTransformerPhase then
blockDecomposerLoweringPhase then
// TODO: Reimplement
// constLoweringPhase then
eraseVirtualDispatchReceiverParametersTypes then
bridgesConstructionPhase then
objectDeclarationLoweringPhase then
objectUsageLoweringPhase then
// staticMembersLoweringPhase then
fieldInitializersLoweringPhase then
genericReturnTypeLowering then
// Replace builtins before autoboxing
builtInsLoweringPhase0 then
autoboxingTransformerPhase then
objectUsageLoweringPhase then
typeOperatorLoweringPhase then
// Clean up built-ins after type operator lowering
builtInsLoweringPhase then
virtualDispatchReceiverExtractionPhase then
wasmThrowDebugLoweringPhase then
staticMembersLoweringPhase then
wasmNullSpecializationLowering then
validateIrAfterLowering
)
@@ -7,10 +7,12 @@ package org.jetbrains.kotlin.backend.wasm
import org.jetbrains.kotlin.backend.common.ir.Symbols
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.PackageViewDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.ir.builders.declarations.addFunction
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
@@ -18,24 +20,26 @@ import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.util.OperatorNameConventions
class WasmSymbols(
context: WasmBackendContext,
private val symbolTable: SymbolTable
) : Symbols<WasmBackendContext>(context, context.irBuiltIns, symbolTable) {
override val throwNullPointerException
get() = TODO()
override val throwNoWhenBranchMatchedException
get() = TODO()
override val throwTypeCastException
get() = TODO()
override val throwUninitializedPropertyAccessException
get() = TODO()
private val wasmInternalPackage: PackageViewDescriptor =
context.module.getPackage(FqName("kotlin.wasm.internal"))
override val throwNullPointerException = getInternalFunction("THROW_NPE")
override val throwISE = getInternalFunction("THROW_ISE")
override val throwNoWhenBranchMatchedException = throwISE
override val throwTypeCastException = getInternalFunction("THROW_CCE")
override val throwUninitializedPropertyAccessException =
getInternalFunction("throwUninitializedPropertyAccessException")
override val defaultConstructorMarker =
getIrClass(FqName("kotlin.wasm.internal.DefaultConstructorMarker"))
override val throwKotlinNothingValueException: IrSimpleFunctionSymbol
get() = TODO()
override val defaultConstructorMarker
get() = TODO()
override val stringBuilder
get() = TODO()
override val copyRangeTo: Map<ClassDescriptor, IrSimpleFunctionSymbol>
@@ -47,7 +51,7 @@ class WasmSymbols(
override val getContinuation
get() = TODO()
override val coroutineContextGetter by lazy {
context.irFactory.addFunction(context.excludedDeclarations) {
context.irFactory.addFunction(context.getExcludedPackageFragment(FqName("kotlin.excluded"))) {
name = Name.identifier("coroutineContextGetter\$Stub")
}.symbol
}
@@ -62,7 +66,9 @@ class WasmSymbols(
override val functionAdapter: IrClassSymbol
get() = TODO()
private val wasmInternalPackage = context.module.getPackage(FqName("kotlin.wasm.internal"))
val wasmUnreachable = getInternalFunction("wasm_unreachable")
val wasmFloatNaN = getInternalFunction("wasm_float_nan")
val wasmDoubleNaN = getInternalFunction("wasm_double_nan")
val equalityFunctions = mapOf(
context.irBuiltIns.booleanType to getInternalFunction("wasm_i32_eq"),
@@ -71,12 +77,16 @@ class WasmSymbols(
context.irBuiltIns.charType to getInternalFunction("wasm_i32_eq"),
context.irBuiltIns.intType to getInternalFunction("wasm_i32_eq"),
context.irBuiltIns.longType to getInternalFunction("wasm_i64_eq"),
context.irBuiltIns.stringType to getInternalFunction("wasm_string_eq")
)
val floatEqualityFunctions = mapOf(
context.irBuiltIns.floatType to getInternalFunction("wasm_f32_eq"),
context.irBuiltIns.doubleType to getInternalFunction("wasm_f64_eq")
)
private fun wasmString(classfier: IrClassifierSymbol): String = with(context.irBuiltIns) {
when (classfier) {
private fun wasmPrimitiveTypeName(classifier: IrClassifierSymbol): String = with(context.irBuiltIns) {
when (classifier) {
booleanClass, byteClass, shortClass, charClass, intClass -> "i32"
floatClass -> "f32"
doubleClass -> "f64"
@@ -85,23 +95,66 @@ class WasmSymbols(
}
}
val irBuiltInsToWasmIntrinsics = context.irBuiltIns.run {
mapOf(
val comparisonBuiltInsToWasmIntrinsics = context.irBuiltIns.run {
listOf(
lessFunByOperandType to "lt",
lessOrEqualFunByOperandType to "le",
greaterOrEqualFunByOperandType to "ge",
greaterFunByOperandType to "gt"
).map { (typeToBuiltIn, wasmOp) ->
typeToBuiltIn.map { (type, builtin) ->
val wasmType = wasmString(type)
val wasmType = wasmPrimitiveTypeName(type)
val markSign = if (wasmType == "i32" || wasmType == "i64") "_s" else ""
builtin to getInternalFunction("wasm_${wasmType}_$wasmOp$markSign")
}
}.flatten().toMap()
}
val booleanAnd = getInternalFunction("wasm_i32_and")
val refEq = getInternalFunction("wasm_ref_eq")
val refIsNull = getInternalFunction("wasm_ref_is_null")
val intToLong = getInternalFunction("wasm_i64_extend_i32_s")
val wasmRefCast = getInternalFunction("wasm_ref_cast")
val boxIntrinsic: IrSimpleFunctionSymbol = getInternalFunction("boxIntrinsic")
val unboxIntrinsic: IrSimpleFunctionSymbol = getInternalFunction("unboxIntrinsic")
val stringGetLiteral = getInternalFunction("stringLiteral")
val wasmClassId = getInternalFunction("wasmClassId")
val wasmInterfaceId = getInternalFunction("wasmInterfaceId")
val getVirtualMethodId = getInternalFunction("getVirtualMethodId")
val getInterfaceMethodId = getInternalFunction("getInterfaceMethodId")
val isSubClass = getInternalFunction("isSubClass")
val isInterface = getInternalFunction("isInterface")
val nullableEquals = getInternalFunction("nullableEquals")
val ensureNotNull = getInternalFunction("ensureNotNull")
val anyNtoString = getInternalFunction("anyNtoString")
val nullableFloatIeee754Equals = getInternalFunction("nullableFloatIeee754Equals")
val nullableDoubleIeee754Equals = getInternalFunction("nullableDoubleIeee754Equals")
val wasmThrow = getInternalFunction("wasmThrow")
private val functionNInterfaces = (0..22).map { arity ->
getIrClass(FqName("kotlin.wasm.internal.Function$arity"))
}
val functionNInvokeMethods by lazy {
functionNInterfaces.map { interfaceSymbol ->
interfaceSymbol.owner.declarations.filterIsInstance<IrSimpleFunction>().single { method ->
method.name == OperatorNameConventions.INVOKE
}.symbol
}
}
override fun functionN(n: Int): IrClassSymbol =
functionNInterfaces[n]
private fun findClass(memberScope: MemberScope, name: Name): ClassDescriptor =
memberScope.getContributedClassifier(name, NoLookupLocation.FROM_BACKEND) as ClassDescriptor
@@ -117,7 +170,7 @@ class WasmSymbols(
internal fun getProperty(fqName: FqName): PropertyDescriptor =
findProperty(context.module.getPackage(fqName.parent()).memberScope, fqName.shortName()).single()
internal fun getInternalFunction(name: String): IrSimpleFunctionSymbol {
private fun getInternalFunction(name: String): IrSimpleFunctionSymbol {
val tmp = findFunctions(wasmInternalPackage.memberScope, Name.identifier(name)).single()
return symbolTable.referenceSimpleFunction(tmp)
}
@@ -1,66 +0,0 @@
/*
* 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.wasm.ast
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
// TODO: Abstract out S-expression part of dumping?
fun WasmInstruction.toWat(ident: String = ""): String =
"$ident($mnemonic${immediate.toWat()}${operands.joinToString("") { " " + it.toWat("") }})"
fun WasmImmediate.toWat(): String = when (this) {
WasmImmediate.None -> ""
is WasmImmediate.DeclarationReference -> " $$name"
// SpiderMonkey jsshell won't parse Uppercase letters in literals
is WasmImmediate.LiteralValue<*> -> " $value".toLowerCaseAsciiOnly()
}
fun wasmModuleToWat(module: WasmModule): String =
"(module\n${module.fields.joinToString("") { wasmModuleFieldToWat(it) + "\n" }})"
fun wasmFunctionToWat(function: WasmFunction): String {
val watId = "$${function.name}"
val watImport = function.importPair?.let { importPair ->
" (import ${toWasString(importPair.module)} ${toWasString(importPair.name)})"
} ?: ""
val watLocals = function.locals.joinToString("") { " " + wasmLocalToWat(it) + "\n" }
val watParameters = function.parameters.joinToString("") { " " + wasmParameterToWat(it, function.importPair == null) }
val watResult = function.returnType?.let { type -> " (result ${type.mnemonic})" } ?: ""
val watBody = function.instructions.joinToString("") { it.toWat(" ") + "\n" }
return " (func $watId$watImport$watParameters$watResult\n$watLocals$watBody )"
}
fun wasmParameterToWat(parameter: WasmParameter, includeName: Boolean): String {
val name = if (includeName) " $${parameter.name}" else ""
return "(param$name ${parameter.type.mnemonic})"
}
fun wasmLocalToWat(local: WasmLocal): String =
local.run { "(local $$name ${type.mnemonic})" }
fun wasmGlobalToWat(global: WasmGlobal): String {
val watMut = if (global.isMutable) "mut " else ""
val watInit = global.init?.toWat("") ?: ""
return global.run { " (global $$name ($watMut${type.mnemonic}) $watInit)" }
}
fun wasmExportToWat(export: WasmExport): String =
export.run { " (export \"$exportedName\" (${kind.keyword} $$wasmName))" }
fun wasmModuleFieldToWat(moduleField: WasmModuleField): String =
when (moduleField) {
is WasmFunction -> wasmFunctionToWat(moduleField)
is WasmGlobal -> wasmGlobalToWat(moduleField)
is WasmExport -> wasmExportToWat(moduleField)
is WasmModuleFieldList -> moduleField.fields.joinToString("") { wasmModuleFieldToWat(it) + "\n" }
}
fun toWasString(s: String): String {
// TODO: escape characters according to
// https://webassembly.github.io/spec/core/text/values.html#strings
return "\"" + s + "\""
}
@@ -1,55 +0,0 @@
/*
* 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.wasm.ast
import org.jetbrains.kotlin.backend.wasm.utils.WasmImportPair
class WasmModule(
val fields: List<WasmModuleField>
)
sealed class WasmModuleField
class WasmModuleFieldList(
val fields: List<WasmModuleField>
) : WasmModuleField()
class WasmFunction(
val name: String,
val parameters: List<WasmParameter>,
val returnType: WasmValueType?,
val locals: List<WasmLocal>,
val instructions: List<WasmInstruction>,
val importPair: WasmImportPair?
) : WasmModuleField()
class WasmParameter(
val name: String,
val type: WasmValueType
)
class WasmLocal(
val name: String,
val type: WasmValueType
)
class WasmGlobal(
val name: String,
val type: WasmValueType,
val isMutable: Boolean,
val init: WasmInstruction?
) : WasmModuleField()
class WasmExport(
val wasmName: String,
val exportedName: String,
val kind: Kind
) : WasmModuleField() {
enum class Kind(val keyword: String) {
FUNCTION("func"),
GLOBAL("global")
}
}
@@ -1,65 +0,0 @@
/*
* 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.wasm.ast
sealed class WasmImmediate {
object None : WasmImmediate()
class DeclarationReference(val name: String) : WasmImmediate()
class LiteralValue<T : Number>(val value: T) : WasmImmediate()
}
sealed class WasmInstruction(
val mnemonic: String,
val immediate: WasmImmediate = WasmImmediate.None,
val operands: List<WasmInstruction> = emptyList()
)
class WasmSimpleInstruction(mnemonic: String, operands: List<WasmInstruction>) :
WasmInstruction(mnemonic, operands = operands)
class WasmNop : WasmInstruction("nop")
class WasmReturn(values: List<WasmInstruction>) :
WasmInstruction("return", operands = values)
class WasmDrop(instructions: List<WasmInstruction>) :
WasmInstruction("drop", operands = instructions)
class WasmCall(name: String, operands: List<WasmInstruction>) :
WasmInstruction("call", WasmImmediate.DeclarationReference(name), operands)
class WasmGetLocal(name: String) :
WasmInstruction("get_local", WasmImmediate.DeclarationReference(name))
class WasmGetGlobal(name: String) :
WasmInstruction("get_global", WasmImmediate.DeclarationReference(name))
class WasmSetGlobal(name: String, value: WasmInstruction) :
WasmInstruction("set_global", WasmImmediate.DeclarationReference(name), listOf(value))
class WasmSetLocal(name: String, value: WasmInstruction) :
WasmInstruction("set_local", WasmImmediate.DeclarationReference(name), listOf(value))
class WasmIf(condition: WasmInstruction, thenInstructions: WasmThen?, elseInstruction: WasmElse?) :
WasmInstruction("if", operands = listOfNotNull(condition, thenInstructions, elseInstruction))
class WasmThen(inst: WasmInstruction) :
WasmInstruction("then", operands = listOf(inst))
class WasmElse(inst: WasmInstruction) :
WasmInstruction("else", operands = listOf(inst))
class WasmBlock(instructions: List<WasmInstruction>) :
WasmInstruction("block", operands = instructions)
sealed class WasmConst<KotlinType : Number, WasmType : WasmValueType>(value: KotlinType, type: WasmType) :
WasmInstruction(type.mnemonic + ".const", WasmImmediate.LiteralValue<KotlinType>(value))
class WasmI32Const(value: Int) : WasmConst<Int, WasmI32>(value, WasmI32)
class WasmI64Const(value: Long) : WasmConst<Long, WasmI64>(value, WasmI64)
class WasmF32Const(value: Float) : WasmConst<Float, WasmF32>(value, WasmF32)
class WasmF64Const(value: Double) : WasmConst<Double, WasmF64>(value, WasmF64)
@@ -1,15 +0,0 @@
/*
* 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.wasm.ast
sealed class WasmValueType(val mnemonic: String)
object WasmI32 : WasmValueType("i32")
object WasmI64 : WasmValueType("i64")
object WasmF32 : WasmValueType("f32")
object WasmF64 : WasmValueType("f64")
object WasmAnyRef : WasmValueType("anyref")
@@ -1,16 +0,0 @@
/*
* 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.wasm.codegen
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.backend.js.utils.TODO
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
interface BaseTransformer<out R, in D> : IrElementVisitor<R, D> {
override fun visitElement(element: IrElement, data: D): R {
TODO(element)
}
}
@@ -1,123 +0,0 @@
/*
* 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.wasm.codegen
import org.jetbrains.kotlin.backend.wasm.ast.*
import org.jetbrains.kotlin.backend.wasm.utils.getWasmImportAnnotation
import org.jetbrains.kotlin.backend.wasm.utils.getWasmInstructionAnnotation
import org.jetbrains.kotlin.backend.wasm.utils.hasExcludedFromCodegenAnnotation
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
import org.jetbrains.kotlin.ir.util.isAnnotationClass
import org.jetbrains.kotlin.ir.util.isFakeOverride
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
class DeclarationTransformer : BaseTransformer<WasmModuleField?, WasmCodegenContext> {
override fun visitSimpleFunction(declaration: IrSimpleFunction, data: WasmCodegenContext): WasmModuleField? {
if (declaration.hasExcludedFromCodegenAnnotation())
return null
if (declaration.getWasmInstructionAnnotation() != null)
return null
if (declaration.isFakeOverride)
return null
// Virtual functions are not supported yet
if (declaration.origin == IrDeclarationOrigin.BRIDGE)
return null
// Collect local variables
val localNames = wasmNameTable<IrValueDeclaration>()
val wasmName = data.getGlobalName(declaration)
val irParameters = declaration.run {
listOfNotNull(dispatchReceiverParameter, extensionReceiverParameter) + valueParameters
}
val wasmParameters = irParameters.map { parameter ->
val name = localNames.declareFreshName(parameter, parameter.name.asString())
WasmParameter(name, data.transformType(parameter.type))
}
val wasmReturnType = when {
declaration.returnType.isUnit() -> null
else -> data.transformType(declaration.returnType)
}
val importedName = declaration.getWasmImportAnnotation()
if (importedName != null) {
data.imports.add(
WasmFunction(
name = wasmName,
parameters = wasmParameters,
returnType = wasmReturnType,
locals = emptyList(),
instructions = emptyList(),
importPair = importedName
)
)
return null
}
val body = declaration.body
?: error("Function ${declaration.fqNameWhenAvailable} without a body")
data.localNames = localNames.names
val locals = mutableListOf<WasmLocal>()
body.acceptChildrenVoid(object : IrElementVisitorVoid {
override fun visitElement(element: IrElement) {
element.acceptChildrenVoid(this)
}
override fun visitVariable(declaration: IrVariable) {
val name = localNames.declareFreshName(declaration, declaration.name.asString())
locals += WasmLocal(name, data.transformType(declaration.type))
super.visitVariable(declaration)
}
})
return WasmFunction(
name = wasmName,
parameters = wasmParameters,
returnType = wasmReturnType,
locals = locals,
instructions = bodyToWasmInstructionList(body, data),
importPair = null
)
}
override fun visitConstructor(declaration: IrConstructor, data: WasmCodegenContext): WasmModuleField? {
TODO()
}
override fun visitClass(declaration: IrClass, data: WasmCodegenContext): WasmModuleField? {
if (declaration.isAnnotationClass) return null
if (declaration.hasExcludedFromCodegenAnnotation()) return null
val wasmMembers = declaration.declarations.mapNotNull { member ->
when (member) {
is IrSimpleFunction -> this.visitSimpleFunction(member, data)
else -> null
}
}
return WasmModuleFieldList(wasmMembers)
}
override fun visitField(declaration: IrField, data: WasmCodegenContext): WasmModuleField {
return WasmGlobal(
name = data.getGlobalName(declaration),
type = data.transformType(declaration.type),
isMutable = true,
// TODO: move non-constexpr initializers out
init = declaration.initializer?.let {
expressionToWasmInstruction(it.expression, data)
}
)
}
}
@@ -1,204 +0,0 @@
/*
* 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.wasm.codegen
import org.jetbrains.kotlin.backend.common.ir.isElseBranch
import org.jetbrains.kotlin.backend.wasm.ast.*
import org.jetbrains.kotlin.backend.wasm.utils.getWasmInstructionAnnotation
import org.jetbrains.kotlin.ir.backend.js.utils.realOverrideTarget
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.types.isUnit
import org.jetbrains.kotlin.ir.util.dump
class ExpressionTransformer : BaseTransformer<WasmInstruction, WasmCodegenContext> {
override fun visitVararg(expression: IrVararg, data: WasmCodegenContext): WasmInstruction {
TODO("Support arrays")
}
override fun visitExpressionBody(body: IrExpressionBody, data: WasmCodegenContext): WasmInstruction =
body.expression.accept(this, data)
override fun visitFunctionReference(expression: IrFunctionReference, data: WasmCodegenContext): WasmInstruction {
TODO("?")
}
override fun <T> visitConst(expression: IrConst<T>, data: WasmCodegenContext): WasmInstruction {
return when (val kind = expression.kind) {
is IrConstKind.Null -> TODO()
is IrConstKind.String -> {
val value = kind.valueOf(expression)
val index = data.stringLiterals.size
data.stringLiterals.add(value)
val funName = data.getGlobalName(data.backendContext.wasmSymbols.stringGetLiteral.owner)
val operand = WasmI32Const(index)
WasmCall(funName, listOf(operand))
}
is IrConstKind.Boolean -> WasmI32Const(if (kind.valueOf(expression)) 1 else 0)
is IrConstKind.Byte -> WasmI32Const(kind.valueOf(expression).toInt())
is IrConstKind.Short -> WasmI32Const(kind.valueOf(expression).toInt())
is IrConstKind.Int -> WasmI32Const(kind.valueOf(expression))
is IrConstKind.Long -> WasmI64Const(kind.valueOf(expression))
is IrConstKind.Char -> WasmI32Const(kind.valueOf(expression).toInt())
is IrConstKind.Float -> WasmF32Const(kind.valueOf(expression))
is IrConstKind.Double -> WasmF64Const(kind.valueOf(expression))
}
}
override fun visitStringConcatenation(expression: IrStringConcatenation, data: WasmCodegenContext): WasmInstruction {
TODO("Implement kotlin.String")
}
override fun visitGetField(expression: IrGetField, data: WasmCodegenContext): WasmInstruction {
val fieldName = data.getGlobalName(expression.symbol.owner)
if (expression.receiver != null)
TODO("Support member fields")
return WasmGetGlobal(fieldName)
}
override fun visitGetValue(expression: IrGetValue, data: WasmCodegenContext): WasmInstruction =
WasmGetLocal(data.getLocalName(expression.symbol.owner))
override fun visitGetObjectValue(expression: IrGetObjectValue, data: WasmCodegenContext): WasmInstruction {
TODO("IrGetObjectValue")
}
override fun visitSetField(expression: IrSetField, data: WasmCodegenContext): WasmInstruction {
val fieldName = data.getGlobalName(expression.symbol.owner)
if (expression.receiver != null)
TODO("Support member fields")
val value = expression.value.accept(this, data)
return WasmSetGlobal(fieldName, value)
}
override fun visitSetValue(expression: IrSetValue, data: WasmCodegenContext): WasmInstruction {
val fieldName = data.getLocalName(expression.symbol.owner)
val value = expression.value.accept(this, data)
return WasmSetLocal(fieldName, value)
}
override fun visitConstructorCall(expression: IrConstructorCall, data: WasmCodegenContext): WasmInstruction {
TODO("IrConstructorCall")
}
override fun visitCall(expression: IrCall, data: WasmCodegenContext): WasmInstruction {
val function = expression.symbol.owner.realOverrideTarget
val valueArgs = (0 until expression.valueArgumentsCount).mapNotNull { expression.getValueArgument(it) }
val irArguments = listOfNotNull(expression.dispatchReceiver, expression.extensionReceiver) + valueArgs
val wasmArguments = irArguments.map { expressionToWasmInstruction(it, data) }
val wasmInstruction = function.getWasmInstructionAnnotation()
if (wasmInstruction != null) {
if (wasmInstruction == "nop") {
return wasmArguments.single()
}
return WasmSimpleInstruction(wasmInstruction, wasmArguments)
}
val name = data.getGlobalName(function)
return WasmCall(name, wasmArguments)
}
override fun visitTypeOperator(expression: IrTypeOperatorCall, data: WasmCodegenContext): WasmInstruction {
val wasmArgument = expressionToWasmInstruction(expression.argument, data)
if (expression.operator == IrTypeOperator.IMPLICIT_COERCION_TO_UNIT) {
return wasmArgument
}
TODO("IrTypeOperatorCall:\n ${expression.dump()}")
}
override fun visitGetEnumValue(expression: IrGetEnumValue, data: WasmCodegenContext): WasmInstruction {
TODO("IrGetEnumValue")
}
override fun visitBlockBody(body: IrBlockBody, data: WasmCodegenContext): WasmInstruction {
TODO()
}
override fun visitContainerExpression(expression: IrContainerExpression, data: WasmCodegenContext): WasmInstruction {
val expressions = expression.statements.map { it.accept(this, data) }
if (!expression.type.isUnit())
return WasmBlock(expressions + listOf(WasmDrop(emptyList())))
return WasmBlock(expressions)
}
override fun visitExpression(expression: IrExpression, data: WasmCodegenContext): WasmInstruction {
return expressionToWasmInstruction(expression, data)
}
override fun visitBreak(jump: IrBreak, data: WasmCodegenContext): WasmInstruction {
TODO()
}
override fun visitContinue(jump: IrContinue, data: WasmCodegenContext): WasmInstruction {
TODO()
}
override fun visitReturn(expression: IrReturn, data: WasmCodegenContext): WasmInstruction {
if (expression.value.type.isUnit()) return WasmReturn(emptyList())
return WasmReturn(listOf(expressionToWasmInstruction(expression.value, data)))
}
override fun visitThrow(expression: IrThrow, data: WasmCodegenContext): WasmInstruction {
TODO("IrThrow")
}
override fun visitVariable(declaration: IrVariable, data: WasmCodegenContext): WasmInstruction {
val init = declaration.initializer ?: return WasmNop()
val varName = data.getLocalName(declaration)
return WasmSetLocal(varName, expressionToWasmInstruction(init, data))
}
override fun visitDelegatingConstructorCall(expression: IrDelegatingConstructorCall, data: WasmCodegenContext): WasmInstruction {
TODO("IrDelegatingConstructorCall")
}
override fun visitInstanceInitializerCall(expression: IrInstanceInitializerCall, data: WasmCodegenContext): WasmInstruction {
TODO("IrInstanceInitializerCall")
}
override fun visitTry(aTry: IrTry, data: WasmCodegenContext): WasmInstruction {
TODO("IrTry")
}
override fun visitWhen(expression: IrWhen, data: WasmCodegenContext): WasmInstruction {
return expression.branches.foldRight(null) { br: IrBranch, inst: WasmInstruction? ->
val body = expressionToWasmInstruction(br.result, data)
if (isElseBranch(br)) body
else {
val condition = expressionToWasmInstruction(br.condition, data)
WasmIf(condition, WasmThen(body), inst?.let { WasmElse(inst) })
}
}!!
}
override fun visitWhileLoop(loop: IrWhileLoop, data: WasmCodegenContext): WasmInstruction {
TODO("IrWhileLoop")
}
override fun visitDoWhileLoop(loop: IrDoWhileLoop, data: WasmCodegenContext): WasmInstruction {
TODO("IrDoWhileLoop")
}
override fun visitSyntheticBody(body: IrSyntheticBody, data: WasmCodegenContext): WasmInstruction {
TODO("IrSyntheticBody")
}
override fun visitDynamicMemberExpression(expression: IrDynamicMemberExpression, data: WasmCodegenContext): WasmInstruction =
error("Dynamic operators are not supported for WASM target")
override fun visitDynamicOperatorExpression(expression: IrDynamicOperatorExpression, data: WasmCodegenContext): WasmInstruction =
error("Dynamic operators are not supported for WASM target")
}
fun expressionToWasmInstruction(expression: IrExpression, context: WasmCodegenContext): WasmInstruction {
return expression.accept(ExpressionTransformer(), context)
}
@@ -1,82 +0,0 @@
/*
* 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.wasm.codegen
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.backend.wasm.WasmCompilerResult
import org.jetbrains.kotlin.backend.wasm.ast.WasmExport
import org.jetbrains.kotlin.backend.wasm.ast.WasmModule
import org.jetbrains.kotlin.backend.wasm.ast.wasmModuleToWat
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.ir.backend.js.transformers.irToJs.jsAssignment
import org.jetbrains.kotlin.ir.backend.js.utils.sanitizeName
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
import org.jetbrains.kotlin.js.backend.ast.JsArrayLiteral
import org.jetbrains.kotlin.js.backend.ast.JsBlock
import org.jetbrains.kotlin.js.backend.ast.JsNameRef
import org.jetbrains.kotlin.js.backend.ast.JsStringLiteral
import org.jetbrains.kotlin.utils.addIfNotNull
class IrModuleToWasm(private val backendContext: WasmBackendContext) {
fun generateModule(module: IrModuleFragment): WasmCompilerResult {
val nameTable = generateWatTopLevelNames(module.files)
val context = WasmCodegenContext(nameTable, backendContext)
val irDeclarations = module.files.flatMap { it.declarations }
val wasmDeclarations = irDeclarations.mapNotNull { it.accept(DeclarationTransformer(), context) }
val exports = generateExports(module, context)
val wasmModule = WasmModule(context.imports + wasmDeclarations + exports)
val wat = wasmModuleToWat(wasmModule)
return WasmCompilerResult(wat, generateStringLiteralsSupport(context.stringLiterals))
}
private fun generateStringLiteralsSupport(literals: List<String>): String {
return JsBlock(
jsAssignment(
JsNameRef("stringLiterals", "runtime"),
JsArrayLiteral(literals.map { JsStringLiteral(it) })
).makeStmt()
).toString()
}
private fun generateExports(module: IrModuleFragment, context: WasmCodegenContext): List<WasmExport> {
val exports = mutableListOf<WasmExport>()
for (file in module.files) {
for (declaration in file.declarations) {
exports.addIfNotNull(generateExport(declaration, context))
}
}
return exports
}
private fun generateExport(declaration: IrDeclaration, context: WasmCodegenContext): WasmExport? {
if (declaration !is IrDeclarationWithVisibility ||
declaration !is IrDeclarationWithName ||
declaration !is IrSimpleFunction ||
declaration.visibility != DescriptorVisibilities.PUBLIC
) {
return null
}
if (!declaration.isExported(context))
return null
val internalName = context.getGlobalName(declaration)
val exportedName = sanitizeName(declaration.name.identifier)
return WasmExport(
wasmName = internalName,
exportedName = exportedName,
kind = WasmExport.Kind.FUNCTION
)
}
}
fun IrFunction.isExported(context: WasmCodegenContext): Boolean =
fqNameWhenAvailable in context.backendContext.additionalExportedDeclarations
@@ -1,64 +0,0 @@
/*
* 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.wasm.codegen
import org.jetbrains.kotlin.backend.common.ir.isTopLevel
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.backend.js.utils.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
fun <T> wasmNameTable() = NameTable<T>(sanitizer = ::sanitizeWatIdentifier)
fun generateWatTopLevelNames(packages: List<IrPackageFragment>): Map<IrDeclarationWithName, String> {
val names = wasmNameTable<IrDeclarationWithName>()
fun nameTopLevelDecl(declaration: IrDeclarationWithName) {
val suggestedName = declaration.fqNameWhenAvailable?.toString()
?: "fqname???" + declaration.name.asString()
names.declareFreshName(declaration, suggestedName)
}
for (p in packages) {
p.acceptChildrenVoid(object : IrElementVisitorVoid {
override fun visitElement(element: IrElement) {
element.acceptChildrenVoid(this)
}
override fun visitSimpleFunction(declaration: IrSimpleFunction) {
nameTopLevelDecl(declaration)
super.visitSimpleFunction(declaration)
}
override fun visitField(declaration: IrField) {
if (declaration.isTopLevel)
nameTopLevelDecl(declaration)
super.visitField(declaration)
}
})
}
return names.names
}
fun sanitizeWatIdentifier(ident: String): String {
if (ident.isEmpty())
return "_"
if (ident.all(::isValidWatIdentifier))
return ident
return ident.map { if (isValidWatIdentifier(it)) it else "_" }.joinToString("")
}
// https://webassembly.github.io/spec/core/text/values.html#text-id
fun isValidWatIdentifier(c: Char): Boolean =
c in '0'..'9' || c in 'A'..'Z' || c in 'a'..'z'
// TODO: SpiderMonkey js shell can't parse some of the
// permitted identifiers: '?', '<'
// || c in "!#$%&*+-./:<=>?@\\^_`|~"
|| c in "$.@_"
@@ -1,35 +0,0 @@
/*
* 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.wasm.codegen
import org.jetbrains.kotlin.backend.wasm.ast.WasmInstruction
import org.jetbrains.kotlin.backend.wasm.ast.WasmNop
import org.jetbrains.kotlin.backend.wasm.ast.WasmSetLocal
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.expressions.*
class StatementTransformer : BaseTransformer<WasmInstruction, WasmCodegenContext> {
override fun visitVariable(declaration: IrVariable, data: WasmCodegenContext): WasmInstruction {
val init = declaration.initializer ?: return WasmNop()
val varName = data.getLocalName(declaration)
return WasmSetLocal(varName, expressionToWasmInstruction(init, data))
}
override fun visitExpression(expression: IrExpression, data: WasmCodegenContext): WasmInstruction {
return expressionToWasmInstruction(expression, data)
}
}
fun statementToWasmInstruction(statement: IrStatement, context: WasmCodegenContext): WasmInstruction {
return statement.accept(StatementTransformer(), context)
}
fun bodyToWasmInstructionList(body: IrBody, context: WasmCodegenContext): List<WasmInstruction> {
if (body is IrBlockBody) {
return body.statements.map { statementToWasmInstruction(it, context) }
} else TODO()
}
@@ -1,27 +0,0 @@
/*
* 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.wasm.codegen
import org.jetbrains.kotlin.backend.wasm.ast.*
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.render
fun WasmCodegenContext.transformType(irType: IrType): WasmValueType =
when {
irType.isBoolean() -> WasmI32
irType.isByte() -> WasmI32
irType.isShort() -> WasmI32
irType.isInt() -> WasmI32
irType.isLong() -> WasmI64
irType.isChar() -> WasmI32
irType.isFloat() -> WasmF32
irType.isDouble() -> WasmF64
irType.isString() -> WasmAnyRef
irType.isAny() || irType.isNullableAny() -> WasmAnyRef
else ->
TODO("Unsupported type: ${irType.render()}")
}
@@ -1,29 +0,0 @@
/*
* 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.wasm.codegen
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.backend.wasm.ast.WasmModuleField
import org.jetbrains.kotlin.ir.declarations.IrDeclarationWithName
import org.jetbrains.kotlin.ir.declarations.IrValueDeclaration
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
class WasmCodegenContext(
private val topLevelNames: Map<IrDeclarationWithName, String>,
val backendContext: WasmBackendContext
) {
val imports = mutableListOf<WasmModuleField>()
var localNames: Map<IrValueDeclaration, String> = emptyMap()
val stringLiterals = mutableListOf<String>()
fun getGlobalName(declaration: IrDeclarationWithName): String =
topLevelNames[declaration]
?: error("Can't find name for ${declaration.fqNameWhenAvailable}")
fun getLocalName(declaration: IrValueDeclaration): String =
localNames[declaration]
?: error("Can't find local name for ${declaration.fqNameWhenAvailable}")
}
@@ -9,7 +9,9 @@ import com.intellij.openapi.project.Project
import org.jetbrains.kotlin.analyzer.AbstractAnalyzerWithCompilerReport
import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig
import org.jetbrains.kotlin.backend.common.phaser.invokeToplevel
import org.jetbrains.kotlin.backend.wasm.codegen.IrModuleToWasm
import org.jetbrains.kotlin.backend.wasm.ir2wasm.WasmModuleFragmentGenerator
import org.jetbrains.kotlin.backend.wasm.ir2wasm.WasmCompiledModuleFragment
import org.jetbrains.kotlin.backend.wasm.ir2wasm.generateStringLiteralsSupport
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.config.languageVersionSettings
import org.jetbrains.kotlin.ir.backend.js.MainModule
@@ -22,8 +24,11 @@ import org.jetbrains.kotlin.library.KotlinLibrary
import org.jetbrains.kotlin.library.resolver.KotlinLibraryResolveResult
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.wasm.ir.convertors.WasmIrToBinary
import org.jetbrains.kotlin.wasm.ir.convertors.WasmIrToText
import java.io.ByteArrayOutputStream
data class WasmCompilerResult(val wat: String, val js: String)
class WasmCompilerResult(val wat: String, val js: String, val wasm: ByteArray)
fun compileWasm(
project: Project,
@@ -60,9 +65,25 @@ fun compileWasm(
val irProviders = generateTypicalIrProviderList(moduleDescriptor, irBuiltIns, symbolTable, deserializer)
ExternalDependenciesGenerator(symbolTable, irProviders, configuration.languageVersionSettings).generateUnboundSymbolsAsDependencies()
moduleFragment.patchDeclarationParents()
deserializer.postProcess()
wasmPhases.invokeToplevel(phaseConfig, context, moduleFragment)
return IrModuleToWasm(context).generateModule(moduleFragment)
val compiledWasmModule = WasmCompiledModuleFragment()
val codeGenerator = WasmModuleFragmentGenerator(context, compiledWasmModule)
codeGenerator.generateModule(moduleFragment)
val linkedModule = compiledWasmModule.linkWasmCompiledFragments()
val watGenerator = WasmIrToText()
watGenerator.appendWasmModule(linkedModule)
val wat = watGenerator.toString()
val os = ByteArrayOutputStream()
WasmIrToBinary(os, linkedModule).appendWasmModule()
val byteArray = os.toByteArray()
return WasmCompilerResult(
wat,
generateStringLiteralsSupport(compiledWasmModule.stringLiterals),
wasm = byteArray
)
}
@@ -0,0 +1,492 @@
/*
* Copyright 2010-2020 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.
*/
@file:OptIn(ExperimentalUnsignedTypes::class)
package org.jetbrains.kotlin.backend.wasm.ir2wasm
import org.jetbrains.kotlin.backend.common.ir.isElseBranch
import org.jetbrains.kotlin.backend.common.ir.isOverridable
import org.jetbrains.kotlin.backend.common.ir.returnType
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.backend.wasm.WasmSymbols
import org.jetbrains.kotlin.backend.wasm.lower.wasmSignature
import org.jetbrains.kotlin.backend.wasm.utils.*
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.backend.js.utils.realOverrideTarget
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.ir.util.getInlineClassBackingField
import org.jetbrains.kotlin.ir.util.isInterface
import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.wasm.ir.*
class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorVoid {
val body: WasmExpressionBuilder = context.bodyGen
// Shortcuts
private val backendContext: WasmBackendContext = context.backendContext
private val wasmSymbols: WasmSymbols = backendContext.wasmSymbols
private val irBuiltIns: IrBuiltIns = backendContext.irBuiltIns
override fun visitElement(element: IrElement) {
error("Unexpected element of type ${element::class}")
}
override fun <T> visitConst(expression: IrConst<T>) {
when (val kind = expression.kind) {
is IrConstKind.Null -> generateDefaultInitializerForType(context.transformType(expression.type), body)
is IrConstKind.Boolean -> body.buildConstI32(if (kind.valueOf(expression)) 1 else 0)
is IrConstKind.Byte -> body.buildConstI32(kind.valueOf(expression).toInt())
is IrConstKind.Short -> body.buildConstI32(kind.valueOf(expression).toInt())
is IrConstKind.Int -> body.buildConstI32(kind.valueOf(expression))
is IrConstKind.Long -> body.buildConstI64(kind.valueOf(expression))
is IrConstKind.Char -> body.buildConstI32(kind.valueOf(expression).toInt())
is IrConstKind.Float -> body.buildConstF32(kind.valueOf(expression))
is IrConstKind.Double -> body.buildConstF64(kind.valueOf(expression))
is IrConstKind.String -> {
body.buildConstI32Symbol(context.referenceStringLiteral(kind.valueOf(expression)))
body.buildCall(context.referenceFunction(wasmSymbols.stringGetLiteral))
}
else -> error("Unknown constant kind")
}
}
override fun visitGetField(expression: IrGetField) {
val field: IrField = expression.symbol.owner
val receiver: IrExpression? = expression.receiver
if (receiver != null) {
generateExpression(receiver)
if (backendContext.inlineClassesUtils.isClassInlineLike(field.parentAsClass)) {
// Unboxed inline class instance is already represented as backing field.
// Doing nothing.
} else {
generateInstanceFieldAccess(field)
}
} else {
body.buildGetGlobal(context.referenceGlobal(field.symbol))
}
}
private fun generateInstanceFieldAccess(field: IrField) {
body.buildStructGet(
context.referenceStructType(field.parentAsClass.symbol),
context.getStructFieldRef(field)
)
}
override fun visitSetField(expression: IrSetField) {
val field = expression.symbol.owner
val receiver = expression.receiver
if (receiver != null) {
generateExpression(receiver)
generateExpression(expression.value)
body.buildStructSet(
struct = context.referenceStructType(field.parentAsClass.symbol),
fieldId = context.getStructFieldRef(field),
)
} else {
generateExpression(expression.value)
body.buildSetGlobal(context.referenceGlobal(expression.symbol))
}
}
override fun visitGetValue(expression: IrGetValue) {
body.buildGetLocal(context.referenceLocal(expression.symbol))
}
override fun visitSetValue(expression: IrSetValue) {
generateExpression(expression.value)
body.buildSetLocal(context.referenceLocal(expression.symbol))
}
override fun visitCall(expression: IrCall) {
generateCall(expression)
}
override fun visitConstructorCall(expression: IrConstructorCall) {
val klass: IrClass = expression.symbol.owner.parentAsClass
if (backendContext.inlineClassesUtils.isClassInlineLike(klass)) {
// Unboxed instance is just a constructor argument.
generateExpression(expression.getValueArgument(0)!!)
return
}
val wasmStruct: WasmSymbol<WasmStructDeclaration> = context.referenceStructType(klass.symbol)
val wasmClassId = context.referenceClassId(klass.symbol)
val irFields: List<IrField> = klass.allFields(backendContext.irBuiltIns)
irFields.forEachIndexed { index, field ->
if (index == 0)
body.buildConstI32Symbol(wasmClassId)
else
generateDefaultInitializerForType(context.transformType(field.type), body)
}
body.buildGetGlobal(context.referenceClassRTT(klass.symbol))
body.buildStructNew(wasmStruct)
generateCall(expression)
}
override fun visitDelegatingConstructorCall(expression: IrDelegatingConstructorCall) {
val klass = context.irFunction.parentAsClass
// Don't delegate constructors of Any to Any.
if (klass.defaultType.isAny()) {
return
}
body.buildGetLocal(context.referenceLocal(0)) // this parameter
generateCall(expression)
}
private fun generateCall(call: IrFunctionAccessExpression) {
// Box intrinsic has an additional klass ID argument.
// Processing it separately
if (call.symbol == wasmSymbols.boxIntrinsic) {
val toType = call.getTypeArgument(0)!!
val klass = toType.erasedUpperBound!!
val structTypeName = context.referenceStructType(klass.symbol)
val klassId = context.referenceClassId(klass.symbol)
body.buildConstI32Symbol(klassId)
generateExpression(call.getValueArgument(0)!!)
body.buildGetGlobal(context.referenceClassRTT(klass.symbol))
body.buildStructNew(structTypeName)
return
}
call.dispatchReceiver?.let { generateExpression(it) }
call.extensionReceiver?.let { generateExpression(it) }
for (i in 0 until call.valueArgumentsCount) {
generateExpression(call.getValueArgument(i)!!)
}
val function: IrFunction = call.symbol.owner.realOverrideTarget
if (tryToGenerateIntrinsicCall(call, function)) {
return
}
val isSuperCall = call is IrCall && call.superQualifierSymbol != null
if (function is IrSimpleFunction && function.isOverridable && !isSuperCall) {
// Generating index for indirect call
val klass = function.parentAsClass
if (!klass.isInterface) {
val classMetadata = context.getClassMetadata(klass.symbol)
val vfSlot = classMetadata.virtualMethods.map { it.function }.indexOf(function)
// Dispatch receiver should be simple and without side effects at this point
// TODO: Verify
generateExpression(call.dispatchReceiver!!)
body.buildConstI32(vfSlot)
body.buildCall(context.referenceFunction(wasmSymbols.getVirtualMethodId))
} else {
val signatureId = context.referenceSignatureId(function.wasmSignature(backendContext.irBuiltIns))
generateExpression(call.dispatchReceiver!!)
body.buildConstI32Symbol(signatureId)
body.buildCall(context.referenceFunction(wasmSymbols.getInterfaceMethodId))
}
body.buildCallIndirect(
symbol = context.referenceFunctionType(function.symbol)
)
} else {
// Static function call
body.buildCall(context.referenceFunction(function.symbol))
}
// Return types of imported functions cannot have concrete struct/array references.
// Non-primitive return types are represented as eqref which need to be casted back to expected type on call site.
if (function.getWasmImportAnnotation() != null) {
val resT = context.transformResultType(function.returnType)
if (resT is WasmRefNullType) {
generateTypeRTT(function.returnType)
body.buildRefCast(fromType = WasmEqRef, toType = resT)
}
}
}
private fun generateTypeRTT(type: IrType) {
val rtClass = type.erasedUpperBound?.symbol ?: context.backendContext.irBuiltIns.anyClass
body.buildGetGlobal(context.referenceClassRTT(rtClass))
}
// Return true if generated.
// Assumes call arguments are already on the stack
private fun tryToGenerateIntrinsicCall(
call: IrFunctionAccessExpression,
function: IrFunction
): Boolean {
if (tryToGenerateWasmOpIntrinsicCall(function)) {
return true
}
when (function.symbol) {
wasmSymbols.wasmClassId -> {
val klass = call.getTypeArgument(0)!!.getClass()
?: error("No class given for wasmClassId intrinsic")
assert(!klass.isInterface)
body.buildConstI32Symbol(context.referenceClassId(klass.symbol))
}
wasmSymbols.wasmInterfaceId -> {
val irInterface = call.getTypeArgument(0)!!.getClass()
?: error("No interface given for wasmInterfaceId intrinsic")
assert(irInterface.isInterface)
body.buildConstI32Symbol(context.referenceInterfaceId(irInterface.symbol))
}
wasmSymbols.wasmRefCast -> {
val fromType = call.getTypeArgument(0)!!
val toType = call.getTypeArgument(1)!!
generateTypeRTT(toType)
body.buildRefCast(context.transformType(fromType), context.transformType(toType))
}
wasmSymbols.wasmFloatNaN -> {
body.buildConstF32(Float.NaN)
}
wasmSymbols.wasmDoubleNaN -> {
body.buildConstF64(Double.NaN)
}
wasmSymbols.unboxIntrinsic -> {
val fromType = call.getTypeArgument(0)!!
if (fromType.isNothing()) {
body.buildUnreachable()
// TODO: Investigate why?
return true
}
// Workaround test codegen/box/elvis/nullNullOk.kt
if (fromType.makeNotNull().isNothing()) {
body.buildUnreachable()
return true
}
val toType = call.getTypeArgument(1)!!
val klass: IrClass = backendContext.inlineClassesUtils.getInlinedClass(toType)!!
val field = getInlineClassBackingField(klass)
generateTypeRTT(toType)
body.buildRefCast(context.transformType(fromType), context.transformBoxedType(toType))
generateInstanceFieldAccess(field)
}
else -> {
return false
}
}
return true
}
override fun visitContainerExpression(expression: IrContainerExpression) {
val statements = expression.statements
if (statements.isEmpty()) return
statements.dropLast(1).forEach {
statementToWasmInstruction(it)
}
if (expression.type != irBuiltIns.unitType) {
generateExpression(statements.last() as IrExpression)
} else {
statementToWasmInstruction(statements.last())
}
}
override fun visitBreak(jump: IrBreak) {
body.buildBr(context.referenceLoopLevel(jump.loop, LoopLabelType.BREAK))
}
override fun visitContinue(jump: IrContinue) {
body.buildBr(context.referenceLoopLevel(jump.loop, LoopLabelType.CONTINUE))
}
override fun visitReturn(expression: IrReturn) {
generateExpression(expression.value)
// FIXME: Hack for "returning" Unit from functions with generic return type.
// Common case -- lambdas returning unit.
if (expression.value.type == irBuiltIns.unitType &&
expression.returnTargetSymbol.owner.returnType(backendContext) != irBuiltIns.unitType
) {
val irReturnType = expression.returnTargetSymbol.owner.returnType(backendContext)
if (irReturnType != irBuiltIns.unitType) {
generateDefaultInitializerForType(context.transformType(irReturnType), body)
}
}
body.buildInstr(WasmOp.RETURN)
}
override fun visitWhen(expression: IrWhen) {
if (expression.type == irBuiltIns.unitType) {
var ifCount = 0
for (branch in expression.branches) {
if (!isElseBranch(branch)) {
generateExpression(branch.condition)
body.buildIf(label = null, resultType = null)
statementToWasmInstruction(branch.result)
body.buildElse()
ifCount++
} else {
statementToWasmInstruction(branch.result)
break
}
}
repeat(ifCount) { body.buildEnd() }
return
}
val resultType = context.transformBlockResultType(expression.type)
var ifCount = 0
for (branch in expression.branches) {
if (!isElseBranch(branch)) {
generateExpression(branch.condition)
body.buildIf(null, resultType)
generateExpression(branch.result)
if (expression.type == irBuiltIns.nothingType) {
body.buildUnreachable()
}
body.buildElse()
ifCount++
} else {
generateExpression(branch.result)
if (expression.type == irBuiltIns.nothingType) {
body.buildUnreachable()
}
break
}
}
repeat(ifCount) { body.buildEnd() }
}
override fun visitDoWhileLoop(loop: IrDoWhileLoop) {
// (loop $LABEL
// (block $BREAK_LABEL
// (block $CONTINUE_LABEL <LOOP BODY>)
// (br_if $LABEL <CONDITION>)))
val label = loop.label
body.buildLoop(label)
val wasmLoop = body.numberOfNestedBlocks
body.buildBlock("BREAK_$label")
val wasmBreakBlock = body.numberOfNestedBlocks
body.buildBlock("CONTINUE_$label")
val wasmContinueBlock = body.numberOfNestedBlocks
context.defineLoopLevel(loop, LoopLabelType.BREAK, wasmBreakBlock)
context.defineLoopLevel(loop, LoopLabelType.CONTINUE, wasmContinueBlock)
loop.body?.let { statementToWasmInstruction(it) }
body.buildEnd()
generateExpression(loop.condition)
body.buildBrIf(wasmLoop)
body.buildEnd()
body.buildEnd()
}
override fun visitWhileLoop(loop: IrWhileLoop) {
// (loop $CONTINUE_LABEL
// (block $BREAK_LABEL
// (br_if $BREAK_LABEL (i32.eqz <CONDITION>))
// <LOOP_BODY>
// (br $CONTINUE_LABEL)))
val label = loop.label
body.buildLoop(label)
val wasmLoop = body.numberOfNestedBlocks
body.buildBlock("BREAK_$label")
val wasmBreakBlock = body.numberOfNestedBlocks
context.defineLoopLevel(loop, LoopLabelType.BREAK, wasmBreakBlock)
context.defineLoopLevel(loop, LoopLabelType.CONTINUE, wasmLoop)
generateExpression(loop.condition)
body.buildInstr(WasmOp.I32_EQZ)
body.buildBrIf(wasmBreakBlock)
loop.body?.let {
statementToWasmInstruction(it)
}
body.buildBr(wasmLoop)
body.buildEnd()
body.buildEnd()
}
fun generateExpression(expression: IrExpression) {
expression.acceptVoid(this)
if (expression.type == irBuiltIns.nothingType) {
body.buildUnreachable()
}
}
fun statementToWasmInstruction(statement: IrStatement) {
if (statement is IrVariable) {
context.defineLocal(statement.symbol)
val init = statement.initializer ?: return
generateExpression(init)
val varName = context.referenceLocal(statement.symbol)
body.buildSetLocal(varName)
return
}
generateExpression(statement as IrExpression)
if (statement.type != irBuiltIns.unitType && statement.type != irBuiltIns.nothingType) {
body.buildInstr(WasmOp.DROP)
}
}
// Return true if function is recognized as intrinsic.
fun tryToGenerateWasmOpIntrinsicCall(function: IrFunction): Boolean {
if (function.hasWasmReinterpretAnnotation()) {
return true
}
val opString = function.getWasmOpAnnotation()
if (opString != null) {
val op = WasmOp.valueOf(opString)
var immediates = emptyArray<WasmImmediate>()
when (op.immediates.size) {
0 -> {
}
1 -> {
when (val imm = op.immediates[0]) {
WasmImmediateKind.MEM_ARG ->
immediates = arrayOf(WasmImmediate.MemArg(0u, 0u))
else ->
error("Immediate $imm is unsupported")
}
}
else ->
error("Op $opString is unsupported")
}
body.buildInstr(op, *immediates)
return true
}
return false
}
}
@@ -0,0 +1,113 @@
/*
* Copyright 2010-2020 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.wasm.ir2wasm
import org.jetbrains.kotlin.backend.common.ir.isOverridableOrOverrides
import org.jetbrains.kotlin.backend.wasm.lower.WasmSignature
import org.jetbrains.kotlin.backend.wasm.lower.wasmSignature
import org.jetbrains.kotlin.ir.backend.js.utils.realOverrideTarget
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrField
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.types.classifierOrFail
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
import org.jetbrains.kotlin.ir.util.isInterface
class ClassMetadata(
val klass: IrClass,
val superClass: ClassMetadata?,
irBuiltIns: IrBuiltIns
) {
// List of all fields including fields of super classes
// In Wasm order
val fields: List<IrField> =
superClass?.fields.orEmpty() + klass.declarations.filterIsInstance<IrField>()
// Implemented interfaces in no particular order
val interfaces: List<IrClass> = klass.allSuperInterfaces()
// Virtual methods in Wasm order
// TODO: Collect interface methods separately
val virtualMethods: List<VirtualMethodMetadata> = run {
val virtualFunctions =
klass.declarations
.filterVirtualFunctions()
.map {
VirtualMethodMetadata(
it,
it.wasmSignature(irBuiltIns)
)
}
val signatureToVirtualFunction = virtualFunctions.associateBy { it.signature }
val superSignatures = superClass?.virtualMethodsSignatures.orEmpty()
val newVirtualMethods = virtualFunctions.filter { it.signature !in superSignatures }
val superVirtualMethods = superClass?.virtualMethods.orEmpty().map {
signatureToVirtualFunction[it.signature] ?: it
}
val orderedVirtualFunctions = superVirtualMethods + newVirtualMethods
orderedVirtualFunctions
}
init {
val signatureToFunctions = mutableMapOf<WasmSignature, MutableList<IrSimpleFunction>>()
for (vm in virtualMethods) {
signatureToFunctions.getOrPut(vm.signature) { mutableListOf() }.add(vm.function)
}
for ((sig, functions) in signatureToFunctions) {
if (functions.size > 1) {
val funcList = functions.joinToString { " ---- ${it.fqNameWhenAvailable} \n" }
// TODO: Check in FE
error(
"Class ${klass.fqNameWhenAvailable} has ${functions.size} methods with the same signature $sig\n $funcList"
)
}
}
}
private val virtualMethodsSignatures: Set<WasmSignature> =
virtualMethods.map { it.signature }.toSet()
}
class VirtualMethodMetadata(
val function: IrSimpleFunction,
val signature: WasmSignature
)
fun IrClass.allSuperInterfaces(): List<IrClass> =
superTypes.map {
it.classifierOrFail.owner as IrClass
}.flatMap {
(if (it.isInterface) listOf(it) else emptyList()) + it.allSuperInterfaces()
}
fun List<IrDeclaration>.filterVirtualFunctions(): List<IrSimpleFunction> =
asSequence()
.filterIsInstance<IrSimpleFunction>()
.filter { it.dispatchReceiverParameter != null }
.map { it.realOverrideTarget }
.filter { it.isOverridableOrOverrides }
.distinct()
.toList()
fun IrClass.getSuperClass(builtIns: IrBuiltIns): IrClass? =
when (this) {
builtIns.anyClass.owner -> null
else -> {
superTypes
.map { it.classifierOrFail.owner as IrClass }
.singleOrNull { !it.isInterface } ?: builtIns.anyClass.owner
}
}
fun IrClass.allFields(builtIns: IrBuiltIns): List<IrField> =
getSuperClass(builtIns)?.allFields(builtIns).orEmpty() + declarations.filterIsInstance<IrField>()
@@ -0,0 +1,70 @@
/*
* Copyright 2010-2020 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.wasm.ir2wasm
import org.jetbrains.kotlin.wasm.ir.WasmSymbol
// Representation of constant data in Wasm memory
sealed class ConstantDataElement {
abstract val sizeInBytes: Int
abstract fun dump(indent: String = "", startAddress: Int = 0): String
abstract fun toBytes(): ByteArray
}
private fun addressToString(address: Int): String =
address.toString().padEnd(6, ' ')
class ConstantDataIntField(val name: String, val value: WasmSymbol<Int>) : ConstantDataElement() {
constructor(name: String, value: Int) : this(name, WasmSymbol(value))
override fun toBytes(): ByteArray = value.owner.toLittleEndianBytes()
override fun dump(indent: String, startAddress: Int): String {
return "${addressToString(startAddress)}: $indent i32 : ${value.owner} ;; $name\n"
}
override val sizeInBytes: Int = 4
}
class ConstantDataIntArray(val name: String, val value: List<WasmSymbol<Int>>) : ConstantDataElement() {
override fun toBytes(): ByteArray {
return value.fold(byteArrayOf()) { acc, el -> acc + el.owner.toLittleEndianBytes() }
}
override fun dump(indent: String, startAddress: Int): String {
if (value.isEmpty()) return ""
return "${addressToString(startAddress)}: $indent i32[] : ${value.map { it.owner }.toIntArray().contentToString()} ;; $name\n"
}
override val sizeInBytes: Int = value.size * 4
}
class ConstantDataStruct(val name: String, val elements: List<ConstantDataElement>) : ConstantDataElement() {
override fun toBytes(): ByteArray {
return elements.fold(byteArrayOf()) { acc, el -> acc + el.toBytes() }
}
override fun dump(indent: String, startAddress: Int): String {
var res = "$indent;; $name\n"
var elemStartAddr = startAddress
for (el in elements) {
res += el.dump("$indent ", elemStartAddr)
elemStartAddr += el.sizeInBytes
}
return res
}
override val sizeInBytes: Int = elements.map { it.sizeInBytes }.sum()
}
fun Int.toLittleEndianBytes(): ByteArray {
return ByteArray(4) {
(this ushr (it * 8)).toByte()
}
}
@@ -0,0 +1,320 @@
/*
* Copyright 2010-2020 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.wasm.ir2wasm
import org.jetbrains.kotlin.backend.common.ir.isOverridableOrOverrides
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.backend.wasm.utils.*
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.backend.js.utils.realOverrideTarget
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.expressions.IrBlockBody
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid
import org.jetbrains.kotlin.wasm.ir.*
class DeclarationGenerator(val context: WasmModuleCodegenContext) : IrElementVisitorVoid {
// Shortcuts
private val backendContext: WasmBackendContext = context.backendContext
private val irBuiltIns: IrBuiltIns = backendContext.irBuiltIns
override fun visitElement(element: IrElement) {
error("Unexpected element of type ${element::class}")
}
override fun visitTypeAlias(declaration: IrTypeAlias) {
// Type aliases are not material
}
override fun visitFunction(declaration: IrFunction) {
// Inline class constructors are currently empty
if (declaration is IrConstructor && backendContext.inlineClassesUtils.isClassInlineLike(declaration.parentAsClass))
return
val importedName = declaration.getWasmImportAnnotation()
val isIntrinsic = declaration.hasWasmReinterpretAnnotation() || declaration.getWasmOpAnnotation() != null
if (isIntrinsic) {
return
}
if (declaration.isFakeOverride)
return
// Generate function type
val watName = declaration.fqNameWhenAvailable.toString()
val irParameters = declaration.getEffectiveValueParameters()
val wasmFunctionType =
WasmFunctionType(
name = watName,
parameterTypes = irParameters.map {
val t = context.transformValueParameterType(it)
if (importedName != null && t is WasmRefNullType) {
WasmEqRef
} else {
t
}
},
resultTypes = listOfNotNull(
context.transformResultType(declaration.returnType).let {
if (importedName != null && it is WasmRefNullType) WasmEqRef else it
}
)
)
context.defineFunctionType(declaration.symbol, wasmFunctionType)
if (declaration is IrSimpleFunction) {
if (declaration.modality == Modality.ABSTRACT) return
if (declaration.isOverridableOrOverrides) {
// Register function as virtual, meaning this function
// will be stored Wasm table and could be called indirectly.
context.registerVirtualFunction(declaration.symbol)
}
}
assert(declaration == declaration.realOverrideTarget) {
"Sanity check that $declaration is a real function that can be used in calls"
}
if (importedName != null) {
// Imported functions don't have bodies. Declaring the signature:
context.defineFunction(
declaration.symbol,
WasmFunction.Imported(watName, wasmFunctionType, importedName)
)
// TODO: Support re-export of imported functions.
return
}
val function = WasmFunction.Defined(watName, wasmFunctionType)
val functionCodegenContext = WasmFunctionCodegenContextImpl(
declaration,
function,
backendContext,
context
)
for (irParameter in irParameters) {
functionCodegenContext.defineLocal(irParameter.symbol)
}
val exprGen = functionCodegenContext.bodyGen
val bodyBuilder = BodyGenerator(functionCodegenContext)
when (val body = declaration.body) {
is IrBlockBody ->
for (statement in body.statements) {
bodyBuilder.statementToWasmInstruction(statement)
}
is IrExpressionBody ->
bodyBuilder.generateExpression(body.expression)
else -> error("Unexpected body $body")
}
// Return implicit this from constructions to avoid extra tmp
// variables on constructor call sites.
// TODO: Redesign construction scheme.
if (declaration is IrConstructor) {
exprGen.buildGetLocal(/*implicit this*/ function.locals[0])
exprGen.buildInstr(WasmOp.RETURN)
}
// Add unreachable if function returns something but not as a last instruction.
if (wasmFunctionType.resultTypes.isNotEmpty() && declaration.body is IrBlockBody) {
// TODO: Add unreachable only if needed
exprGen.buildUnreachable()
}
context.defineFunction(declaration.symbol, function)
if (declaration == backendContext.startFunction)
context.setStartFunction(function)
if (declaration.isExported(backendContext)) {
context.addExport(
WasmExport.Function(
field = function,
// TODO: Add ability to specify exported name.
name = declaration.name.identifier
)
)
}
}
override fun visitClass(declaration: IrClass) {
if (declaration.isAnnotationClass) return
val symbol = declaration.symbol
if (declaration.isInterface) {
context.registerInterface(symbol)
} else {
val nameStr = declaration.fqNameWhenAvailable.toString()
val structType = WasmStructDeclaration(
name = nameStr,
fields = declaration.allFields(irBuiltIns).map {
WasmStructFieldDeclaration(
name = it.name.toString(),
type = context.transformType(it.type),
isMutable = true
)
}
)
context.defineStructType(symbol, structType)
var depth = 2
val metadata = context.getClassMetadata(symbol)
var subMetadata = metadata
while (true) {
subMetadata = subMetadata.superClass ?: break
depth++
}
val initBody = mutableListOf<WasmInstr>()
val wasmExpressionGenerator = WasmIrExpressionBuilder(initBody)
val superClass = metadata.superClass
if (superClass != null) {
val superRTT = context.referenceClassRTT(superClass.klass.symbol)
wasmExpressionGenerator.buildGetGlobal(superRTT)
} else {
wasmExpressionGenerator.buildRttCanon(WasmRefType(WasmHeapType.Simple.Eq))
}
wasmExpressionGenerator.buildRttSub(
WasmRefType(WasmHeapType.Type(WasmSymbol(structType)))
)
val rtt = WasmGlobal(
name = "rtt_of_$nameStr",
isMutable = false,
type = WasmRtt(depth, WasmHeapType.Type(WasmSymbol(structType))),
init = initBody
)
context.defineRTT(symbol, rtt)
context.registerClass(symbol)
context.generateTypeInfo(symbol, binaryDataStruct(metadata))
}
for (member in declaration.declarations) {
member.acceptVoid(this)
}
}
private fun binaryDataStruct(classMetadata: ClassMetadata): ConstantDataStruct {
val invalidIndex = -1
val superClass = classMetadata.superClass?.klass
val superClassSymbol: WasmSymbol<Int> =
superClass?.let { context.referenceClassId(it.symbol) } ?: WasmSymbol(invalidIndex)
val superTypeField =
ConstantDataIntField("Super class", superClassSymbol)
val interfacesArray = ConstantDataIntArray(
"data",
classMetadata.interfaces.map { context.referenceInterfaceId(it.symbol) }
)
val interfacesArraySize = ConstantDataIntField(
"size",
interfacesArray.value.size
)
val implementedInterfacesArrayWithSize = ConstantDataStruct(
"Implemented interfaces array",
listOf(interfacesArraySize, interfacesArray)
)
val vtableSizeField = ConstantDataIntField(
"V-table length",
classMetadata.virtualMethods.size
)
val vtableArray = ConstantDataIntArray(
"V-table",
classMetadata.virtualMethods.map {
if (it.function.modality == Modality.ABSTRACT) {
WasmSymbol(invalidIndex)
} else {
context.referenceVirtualFunctionId(it.function.symbol)
}
}
)
val signaturesArray = ConstantDataIntArray(
"Signatures",
classMetadata.virtualMethods.map {
if (it.function.modality == Modality.ABSTRACT) {
WasmSymbol(invalidIndex)
} else {
context.referenceSignatureId(it.signature)
}
}
)
return ConstantDataStruct(
"Class TypeInfo: ${classMetadata.klass.fqNameWhenAvailable} ",
listOf(
superTypeField,
vtableSizeField,
vtableArray,
signaturesArray,
implementedInterfacesArrayWithSize,
)
)
}
override fun visitField(declaration: IrField) {
// Member fields are generated as part of struct type
if (!declaration.isStatic) return
val wasmType = context.transformType(declaration.type)
val initBody = mutableListOf<WasmInstr>()
val wasmExpressionGenerator = WasmIrExpressionBuilder(initBody)
generateDefaultInitializerForType(wasmType, wasmExpressionGenerator)
val global = WasmGlobal(
name = declaration.fqNameWhenAvailable.toString(),
type = wasmType,
isMutable = true,
// All globals are currently initialized in start function
init = initBody
)
context.defineGlobal(declaration.symbol, global)
}
}
fun generateDefaultInitializerForType(type: WasmType, g: WasmExpressionBuilder) = when (type) {
WasmI32 -> g.buildConstI32(0)
WasmI1 -> g.buildConstI32(0)
WasmI64 -> g.buildConstI64(0)
WasmF32 -> g.buildConstF32(0f)
WasmF64 -> g.buildConstF64(0.0)
is WasmRefNullType -> g.buildRefNull(type.heapType)
is WasmExternRef -> g.buildRefNull(WasmHeapType.Simple.Extern)
WasmUnreachableType -> error("Unreachable type can't be initialized")
else -> error("Unknown value type ${type.name}")
}
fun IrFunction.getEffectiveValueParameters(): List<IrValueParameter> {
val implicitThis = if (this is IrConstructor) parentAsClass.thisReceiver!! else null
return listOfNotNull(implicitThis, dispatchReceiverParameter, extensionReceiverParameter) + valueParameters
}
fun IrFunction.isExported(context: WasmBackendContext): Boolean =
visibility == DescriptorVisibilities.PUBLIC && fqNameWhenAvailable in context.additionalExportedDeclarations
@@ -0,0 +1,21 @@
/*
* 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.wasm.ir2wasm
import org.jetbrains.kotlin.ir.backend.js.transformers.irToJs.jsAssignment
import org.jetbrains.kotlin.js.backend.ast.JsArrayLiteral
import org.jetbrains.kotlin.js.backend.ast.JsBlock
import org.jetbrains.kotlin.js.backend.ast.JsNameRef
import org.jetbrains.kotlin.js.backend.ast.JsStringLiteral
fun generateStringLiteralsSupport(literals: List<String>): String {
return JsBlock(
jsAssignment(
JsNameRef("stringLiterals", "runtime"),
JsArrayLiteral(literals.map { JsStringLiteral(it) })
).makeStmt()
).toString()
}
@@ -0,0 +1,118 @@
/*
* Copyright 2010-2020 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.wasm.ir2wasm
import org.jetbrains.kotlin.wasm.ir.*
import org.jetbrains.kotlin.backend.wasm.utils.hasWasmForeignAnnotation
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classOrNull
import org.jetbrains.kotlin.ir.types.classifierOrNull
import org.jetbrains.kotlin.ir.types.getClass
import org.jetbrains.kotlin.ir.util.getInlineClassUnderlyingType
import org.jetbrains.kotlin.ir.util.isInterface
class WasmTypeTransformer(
val context: WasmBaseCodegenContext,
val builtIns: IrBuiltIns
) {
fun IrType.toWasmResultType(): WasmType? =
when (this) {
builtIns.unitType,
builtIns.nothingType ->
null
else ->
toWasmValueType()
}
fun IrType.toWasmBlockResultType(): WasmType? =
when (this) {
builtIns.unitType ->
null
// TODO: Lower blocks with Nothing type?
builtIns.nothingType ->
WasmUnreachableType
else ->
toWasmValueType()
}
fun IrType.toStructType(): WasmType =
WasmRefNullType(WasmHeapType.Type(context.referenceStructType(erasedUpperBound?.symbol ?: builtIns.anyClass)))
fun IrType.toBoxedInlineClassType(): WasmType =
toStructType()
fun IrType.toWasmValueType(): WasmType =
when (this) {
builtIns.booleanType,
builtIns.byteType,
builtIns.shortType,
builtIns.intType,
builtIns.charType ->
WasmI32
builtIns.longType ->
WasmI64
builtIns.floatType ->
WasmF32
builtIns.doubleType ->
WasmF64
builtIns.stringType ->
WasmExternRef
builtIns.nothingNType ->
WasmExternRef
// Value will not be created. Just using a random Wasm type.
builtIns.nothingType ->
WasmExternRef
else -> {
val klass = this.getClass()
val ic = context.backendContext.inlineClassesUtils.getInlinedClass(this)
if (klass != null && klass.hasWasmForeignAnnotation()) {
WasmExternRef
} else if (ic != null) {
getInlineClassUnderlyingType(ic).toWasmValueType()
} else {
this.toStructType()
}
}
}
}
// Return null if upper bound is Any
val IrTypeParameter.erasedUpperBound: IrClass?
get() {
// Pick the (necessarily unique) non-interface upper bound if it exists
for (type in superTypes) {
return type.erasedUpperBound ?: continue
}
return null
}
val IrType.erasedUpperBound: IrClass?
get() = when (val classifier = classifierOrNull) {
is IrClassSymbol -> classifier.owner
is IrTypeParameterSymbol -> classifier.owner.erasedUpperBound
else -> throw IllegalStateException()
}.let {
if (it?.isInterface == true) null
else it
}
@@ -0,0 +1,45 @@
/*
* Copyright 2010-2020 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.wasm.ir2wasm
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.wasm.ir.*
import org.jetbrains.kotlin.backend.wasm.lower.WasmSignature
import org.jetbrains.kotlin.ir.declarations.IrField
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrFieldSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.types.IrType
interface WasmBaseCodegenContext {
val backendContext: WasmBackendContext
fun referenceFunction(irFunction: IrFunctionSymbol): WasmSymbol<WasmFunction>
fun referenceGlobal(irField: IrFieldSymbol): WasmSymbol<WasmGlobal>
fun referenceStructType(irClass: IrClassSymbol): WasmSymbol<WasmStructDeclaration>
fun referenceFunctionType(irFunction: IrFunctionSymbol): WasmSymbol<WasmFunctionType>
fun referenceClassId(irClass: IrClassSymbol): WasmSymbol<Int>
fun referenceInterfaceId(irInterface: IrClassSymbol): WasmSymbol<Int>
fun referenceVirtualFunctionId(irFunction: IrSimpleFunctionSymbol): WasmSymbol<Int>
fun referenceClassRTT(irClass: IrClassSymbol): WasmSymbol<WasmGlobal>
fun referenceSignatureId(signature: WasmSignature): WasmSymbol<Int>
fun referenceStringLiteral(string: String): WasmSymbol<Int>
fun transformType(irType: IrType): WasmType
fun transformBoxedType(irType: IrType): WasmType
fun transformValueParameterType(irValueParameter: IrValueParameter): WasmType
fun transformResultType(irType: IrType): WasmType?
fun transformBlockResultType(irType: IrType): WasmType?
fun getStructFieldRef(field: IrField): WasmSymbol<Int>
fun getClassMetadata(irClass: IrClassSymbol): ClassMetadata
}
@@ -0,0 +1,207 @@
/*
* Copyright 2010-2020 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.wasm.ir2wasm
import org.jetbrains.kotlin.wasm.ir.*
import org.jetbrains.kotlin.backend.wasm.lower.WasmSignature
import org.jetbrains.kotlin.ir.declarations.IrDeclarationWithName
import org.jetbrains.kotlin.ir.declarations.IrExternalPackageFragment
import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
import org.jetbrains.kotlin.ir.util.getPackageFragment
class WasmCompiledModuleFragment {
val functions =
ReferencableAndDefinable<IrFunctionSymbol, WasmFunction>()
val globals =
ReferencableAndDefinable<IrFieldSymbol, WasmGlobal>()
val functionTypes =
ReferencableAndDefinable<IrFunctionSymbol, WasmFunctionType>()
val structTypes =
ReferencableAndDefinable<IrClassSymbol, WasmStructDeclaration>()
val classIds =
ReferencableElements<IrClassSymbol, Int>()
val interfaceId =
ReferencableElements<IrClassSymbol, Int>()
val virtualFunctionId =
ReferencableElements<IrFunctionSymbol, Int>()
val signatureId =
ReferencableElements<WasmSignature, Int>()
val stringLiteralId =
ReferencableElements<String, Int>()
val runtimeTypes =
ReferencableAndDefinable<IrClassSymbol, WasmGlobal>()
val classes = mutableListOf<IrClassSymbol>()
val interfaces = mutableListOf<IrClassSymbol>()
val virtualFunctions = mutableListOf<IrSimpleFunctionSymbol>()
val signatures = LinkedHashSet<WasmSignature>()
val stringLiterals = mutableListOf<String>()
val typeInfo =
ReferencableAndDefinable<IrClassSymbol, ConstantDataElement>()
val exports = mutableListOf<WasmExport<*>>()
//
var startFunction: WasmFunction? = null
open class ReferencableElements<Ir, Wasm : Any> {
val unbound = mutableMapOf<Ir, WasmSymbol<Wasm>>()
fun reference(ir: Ir): WasmSymbol<Wasm> {
val declaration = (ir as? IrSymbol)?.owner as? IrDeclarationWithName
if (declaration != null) {
val packageFragment = declaration.getPackageFragment()
?: error("Referencing declaration without package fragment ${declaration.fqNameWhenAvailable}")
if (packageFragment is IrExternalPackageFragment) {
error("Referencing declaration without package fragment ${declaration.fqNameWhenAvailable}")
}
}
return unbound.getOrPut(ir) { WasmSymbol() }
}
}
class ReferencableAndDefinable<Ir, Wasm : Any> : ReferencableElements<Ir, Wasm>() {
fun define(ir: Ir, wasm: Wasm) {
if (ir in defined)
error("Trying to redefine element: IR: $ir Wasm: $wasm")
elements += wasm
defined[ir] = wasm
wasmToIr[wasm] = ir
}
val defined = LinkedHashMap<Ir, Wasm>()
val elements = mutableListOf<Wasm>()
val wasmToIr = mutableMapOf<Wasm, Ir>()
}
@OptIn(ExperimentalUnsignedTypes::class)
fun linkWasmCompiledFragments(): WasmModule {
bind(functions.unbound, functions.defined)
bind(globals.unbound, globals.defined)
bind(functionTypes.unbound, functionTypes.defined)
bind(structTypes.unbound, structTypes.defined)
bind(runtimeTypes.unbound, runtimeTypes.defined)
val klassIds = mutableMapOf<IrClassSymbol, Int>()
var classId = 0
for (typeInfoElement in typeInfo.elements) {
val ir = typeInfo.wasmToIr.getValue(typeInfoElement)
klassIds[ir] = classId
classId += typeInfoElement.sizeInBytes
}
bind(classIds.unbound, klassIds)
bindIndices(virtualFunctionId.unbound, virtualFunctions)
bindIndices(signatureId.unbound, signatures.toList())
bindIndices(interfaceId.unbound, interfaces)
bindIndices(stringLiteralId.unbound, stringLiterals)
val data = typeInfo.elements.map {
val ir = typeInfo.wasmToIr.getValue(it)
val id = klassIds.getValue(ir)
val offset = mutableListOf<WasmInstr>()
WasmIrExpressionBuilder(offset).buildConstI32(id)
WasmData(WasmDataMode.Active(0, offset), it.toBytes())
}
val logTypeInfo = false
if (logTypeInfo) {
println("Signatures: ")
for ((index, signature: WasmSignature) in signatures.withIndex()) {
println(" -- $index $signature")
}
println("Interfaces: ")
for ((index, iface: IrClassSymbol) in interfaces.withIndex()) {
println(" -- $index ${iface.owner.fqNameWhenAvailable}")
}
println("Virtual functions: ")
for ((index, vf: IrSimpleFunctionSymbol) in virtualFunctions.withIndex()) {
println(" -- $index ${vf.owner.fqNameWhenAvailable}")
}
println(
ConstantDataStruct("typeInfo", typeInfo.elements).dump("", 0)
)
}
val table = WasmTable(
limits = WasmLimits(virtualFunctions.size.toUInt(), virtualFunctions.size.toUInt()),
elementType = WasmFuncRef,
)
val offsetExpr = mutableListOf<WasmInstr>()
WasmIrExpressionBuilder(offsetExpr).buildConstI32(0)
val elements = WasmElement(
WasmFuncRef,
values = virtualFunctions.map {
WasmTable.Value.Function(functions.defined.getValue(it))
},
WasmElement.Mode.Active(table, offsetExpr)
)
val typeInfoSize = classId
val memorySizeInPages = (typeInfoSize / 65_536) + 1
val memory = WasmMemory(WasmLimits(memorySizeInPages.toUInt(), memorySizeInPages.toUInt()))
val importedFunctions = functions.elements.filterIsInstance<WasmFunction.Imported>()
// Sorting by depth for a valid init order
val sortedRttGlobals = runtimeTypes.elements.sortedBy { (it.type as WasmRtt).depth }
val module = WasmModule(
functionTypes = functionTypes.elements,
structs = structTypes.elements,
importsInOrder = importedFunctions,
importedFunctions = importedFunctions,
definedFunctions = functions.elements.filterIsInstance<WasmFunction.Defined>(),
tables = listOf(table),
memories = listOf(memory),
globals = globals.elements + sortedRttGlobals,
exports = exports,
startFunction = startFunction!!,
elements = listOf(elements),
data = data
)
module.calculateIds()
return module
}
}
fun <IrSymbolType, WasmDeclarationType : Any, WasmSymbolType : WasmSymbol<WasmDeclarationType>> bind(
unbound: Map<IrSymbolType, WasmSymbolType>,
defined: Map<IrSymbolType, WasmDeclarationType>
) {
unbound.forEach { (irSymbol, wasmSymbol) ->
if (irSymbol !in defined)
error("Can't link symbol ${irSymbolDebugDump(irSymbol)}")
wasmSymbol.bind(defined.getValue(irSymbol))
}
}
private fun irSymbolDebugDump(symbol: Any?): String =
when (symbol) {
is IrFunctionSymbol -> "function ${symbol.owner.fqNameWhenAvailable}"
is IrClassSymbol -> "class ${symbol.owner.fqNameWhenAvailable}"
else -> symbol.toString()
}
fun <IrSymbolType> bindIndices(
unbound: Map<IrSymbolType, WasmSymbol<Int>>,
ordered: List<IrSymbolType>
) {
unbound.forEach { (irSymbol, wasmSymbol) ->
val index = ordered.indexOf(irSymbol)
if (index == -1)
error("Can't link symbol with indices ${irSymbolDebugDump(irSymbol)}")
wasmSymbol.bind(index)
}
}
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2020 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.wasm.ir2wasm
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.expressions.IrLoop
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
import org.jetbrains.kotlin.wasm.ir.WasmExpressionBuilder
import org.jetbrains.kotlin.wasm.ir.WasmInstr
import org.jetbrains.kotlin.wasm.ir.WasmLocal
enum class LoopLabelType { BREAK, CONTINUE }
interface WasmFunctionCodegenContext : WasmBaseCodegenContext {
val irFunction: IrFunction
fun defineLocal(irValueDeclaration: IrValueSymbol)
fun referenceLocal(irValueDeclaration: IrValueSymbol): WasmLocal
fun referenceLocal(index: Int): WasmLocal
fun defineLoopLevel(irLoop: IrLoop, labelType: LoopLabelType, level: Int)
fun referenceLoopLevel(irLoop: IrLoop, labelType: LoopLabelType): Int
val bodyGen: WasmExpressionBuilder
}
@@ -0,0 +1,61 @@
/*
* Copyright 2010-2020 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.wasm.ir2wasm
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.expressions.IrLoop
import org.jetbrains.kotlin.ir.symbols.IrValueParameterSymbol
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
import org.jetbrains.kotlin.wasm.ir.*
class WasmFunctionCodegenContextImpl(
override val irFunction: IrFunction,
private val wasmFunction: WasmFunction.Defined,
override val backendContext: WasmBackendContext,
private val referencing: WasmBaseCodegenContext
) : WasmBaseCodegenContext by referencing,
WasmFunctionCodegenContext {
override val bodyGen: WasmExpressionBuilder =
WasmIrExpressionBuilder(wasmFunction.instructions)
private val wasmLocals = LinkedHashMap<IrValueSymbol, WasmLocal>()
private val loopLevels = LinkedHashMap<Pair<IrLoop, LoopLabelType>, Int>()
override fun defineLocal(irValueDeclaration: IrValueSymbol) {
assert(irValueDeclaration !in wasmLocals) { "Redefinition of local" }
val owner = irValueDeclaration.owner
val wasmLocal = WasmLocal(
wasmFunction.locals.size,
owner.name.asString(),
if (owner is IrValueParameter) transformValueParameterType(owner) else transformType(owner.type),
isParameter = irValueDeclaration is IrValueParameterSymbol
)
wasmLocals[irValueDeclaration] = wasmLocal
wasmFunction.locals += wasmLocal
}
override fun referenceLocal(irValueDeclaration: IrValueSymbol): WasmLocal {
return wasmLocals.getValue(irValueDeclaration)
}
override fun referenceLocal(index: Int): WasmLocal {
return wasmFunction.locals[index]
}
override fun defineLoopLevel(irLoop: IrLoop, labelType: LoopLabelType, level: Int) {
val loopKey = Pair(irLoop, labelType)
assert(loopKey !in loopLevels) { "Redefinition of loop" }
loopLevels[loopKey] = level
}
override fun referenceLoopLevel(irLoop: IrLoop, labelType: LoopLabelType): Int {
return loopLevels.getValue(Pair(irLoop, labelType))
}
}
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2020 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.wasm.ir2wasm
import org.jetbrains.kotlin.wasm.ir.*
import org.jetbrains.kotlin.backend.wasm.ir2wasm.ConstantDataElement
import org.jetbrains.kotlin.backend.wasm.ir2wasm.WasmBaseCodegenContext
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrFieldSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
/**
* Interface for generating WebAssembly module.
*/
interface WasmModuleCodegenContext : WasmBaseCodegenContext {
fun defineFunction(irFunction: IrFunctionSymbol, wasmFunction: WasmFunction)
fun defineGlobal(irField: IrFieldSymbol, wasmGlobal: WasmGlobal)
fun defineStructType(irClass: IrClassSymbol, wasmStruct: WasmStructDeclaration)
fun defineRTT(irClass: IrClassSymbol, wasmGlobal: WasmGlobal)
fun defineFunctionType(irFunction: IrFunctionSymbol, wasmFunctionType: WasmFunctionType)
fun setStartFunction(wasmFunction: WasmFunction)
fun addExport(wasmExport: WasmExport<*>)
fun registerVirtualFunction(irFunction: IrSimpleFunctionSymbol)
fun registerInterface(irInterface: IrClassSymbol)
fun registerClass(irClass: IrClassSymbol)
fun generateTypeInfo(irClass: IrClassSymbol, typeInfo: ConstantDataElement)
}
@@ -0,0 +1,169 @@
/*
* Copyright 2010-2020 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.wasm.ir2wasm
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.wasm.ir.*
import org.jetbrains.kotlin.backend.wasm.lower.WasmSignature
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.declarations.IrField
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrFieldSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.defaultType
import org.jetbrains.kotlin.ir.types.isNothing
import org.jetbrains.kotlin.ir.util.isFunction
import org.jetbrains.kotlin.ir.util.parentAsClass
class WasmModuleCodegenContextImpl(
override val backendContext: WasmBackendContext,
private val wasmFragment: WasmCompiledModuleFragment
) : WasmModuleCodegenContext {
private val typeTransformer =
WasmTypeTransformer(this, backendContext.irBuiltIns)
override fun transformType(irType: IrType): WasmType {
return with(typeTransformer) { irType.toWasmValueType() }
}
override fun transformBoxedType(irType: IrType): WasmType {
return with(typeTransformer) { irType.toBoxedInlineClassType() }
}
override fun transformValueParameterType(irValueParameter: IrValueParameter): WasmType {
return with(typeTransformer) {
if (context.backendContext.inlineClassesUtils.shouldValueParameterBeBoxed(irValueParameter)) {
irValueParameter.type.toBoxedInlineClassType()
} else {
irValueParameter.type.toWasmValueType()
}
}
}
override fun transformResultType(irType: IrType): WasmType? {
return with(typeTransformer) { irType.toWasmResultType() }
}
override fun transformBlockResultType(irType: IrType): WasmType? {
return with(typeTransformer) { irType.toWasmBlockResultType() }
}
override fun referenceStringLiteral(string: String): WasmSymbol<Int> {
wasmFragment.stringLiterals.add(string)
return wasmFragment.stringLiteralId.reference(string)
}
override fun generateTypeInfo(irClass: IrClassSymbol, typeInfo: ConstantDataElement) {
wasmFragment.typeInfo.define(irClass, typeInfo)
}
override fun setStartFunction(wasmFunction: WasmFunction) {
wasmFragment.startFunction = wasmFunction
}
override fun addExport(wasmExport: WasmExport<*>) {
wasmFragment.exports += wasmExport
}
override fun registerVirtualFunction(irFunction: IrSimpleFunctionSymbol) {
wasmFragment.virtualFunctions += irFunction
}
override fun registerInterface(irInterface: IrClassSymbol) {
wasmFragment.interfaces += irInterface
}
override fun registerClass(irClass: IrClassSymbol) {
wasmFragment.classes += irClass
}
override fun defineFunction(irFunction: IrFunctionSymbol, wasmFunction: WasmFunction) {
wasmFragment.functions.define(irFunction, wasmFunction)
}
override fun defineGlobal(irField: IrFieldSymbol, wasmGlobal: WasmGlobal) {
wasmFragment.globals.define(irField, wasmGlobal)
}
override fun defineStructType(irClass: IrClassSymbol, wasmStruct: WasmStructDeclaration) {
wasmFragment.structTypes.define(irClass, wasmStruct)
}
override fun defineRTT(irClass: IrClassSymbol, wasmGlobal: WasmGlobal) {
wasmFragment.runtimeTypes.define(irClass, wasmGlobal)
}
override fun defineFunctionType(irFunction: IrFunctionSymbol, wasmFunctionType: WasmFunctionType) {
wasmFragment.functionTypes.define(irFunction, wasmFunctionType)
}
private val classMetadataCache = mutableMapOf<IrClassSymbol, ClassMetadata>()
override fun getClassMetadata(irClass: IrClassSymbol): ClassMetadata =
classMetadataCache.getOrPut(irClass) {
val superClass = irClass.owner.getSuperClass(backendContext.irBuiltIns)
val superClassMetadata = superClass?.let { getClassMetadata(it.symbol) }
ClassMetadata(
irClass.owner,
superClassMetadata,
backendContext.irBuiltIns
)
}
override fun referenceFunction(irFunction: IrFunctionSymbol): WasmSymbol<WasmFunction> =
wasmFragment.functions.reference(irFunction)
override fun referenceGlobal(irField: IrFieldSymbol): WasmSymbol<WasmGlobal> =
wasmFragment.globals.reference(irField)
override fun referenceStructType(irClass: IrClassSymbol): WasmSymbol<WasmStructDeclaration> {
val type = irClass.defaultType
require(!type.isNothing()) {
"Can't reference Nothing type"
}
return wasmFragment.structTypes.reference(irClass)
}
override fun referenceClassRTT(irClass: IrClassSymbol): WasmSymbol<WasmGlobal> =
wasmFragment.runtimeTypes.reference(irClass)
override fun referenceFunctionType(irFunction: IrFunctionSymbol): WasmSymbol<WasmFunctionType> =
wasmFragment.functionTypes.reference(irFunction)
override fun referenceClassId(irClass: IrClassSymbol): WasmSymbol<Int> =
wasmFragment.classIds.reference(irClass)
override fun referenceInterfaceId(irInterface: IrClassSymbol): WasmSymbol<Int> {
// HACK to substitute kotlin.Function5 with kotlin.wasm.internal.Function5
val defaultType = irInterface.defaultType
if (defaultType.isFunction()) {
val n = irInterface.owner.typeParameters.size - 1
return wasmFragment.interfaceId.reference(backendContext.wasmSymbols.functionN(n))
}
return wasmFragment.interfaceId.reference(irInterface)
}
override fun referenceVirtualFunctionId(irFunction: IrSimpleFunctionSymbol): WasmSymbol<Int> {
if (irFunction.owner.modality == Modality.ABSTRACT)
error("Abstract functions are not stored in table")
return wasmFragment.virtualFunctionId.reference(irFunction)
}
override fun referenceSignatureId(signature: WasmSignature): WasmSymbol<Int> {
wasmFragment.signatures.add(signature)
return wasmFragment.signatureId.reference(signature)
}
override fun getStructFieldRef(field: IrField): WasmSymbol<Int> {
val klass = field.parentAsClass
val metadata = getClassMetadata(klass.symbol)
val fieldId = metadata.fields.indexOf(field)
return WasmSymbol(fieldId)
}
}
@@ -0,0 +1,41 @@
/*
* Copyright 2010-2020 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.wasm.ir2wasm
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.declarations.IrPackageFragment
import org.jetbrains.kotlin.ir.visitors.acceptVoid
class WasmModuleFragmentGenerator(
backendContext: WasmBackendContext,
wasmModuleFragment: WasmCompiledModuleFragment
) {
private val declarationGenerator =
DeclarationGenerator(
WasmModuleCodegenContextImpl(
backendContext,
wasmModuleFragment
)
)
fun generateModule(irModuleFragment: IrModuleFragment) {
for (irFile in irModuleFragment.files) {
generatePackageFragment(irFile)
}
}
fun generatePackageFragment(irPackageFragment: IrPackageFragment) {
for (irDeclaration in irPackageFragment.declarations) {
generateDeclaration(irDeclaration)
}
}
fun generateDeclaration(irDeclaration: IrDeclaration) {
irDeclaration.acceptVoid(declarationGenerator)
}
}
@@ -1,14 +0,0 @@
/*
* 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.wasm.lower
import org.jetbrains.kotlin.ir.backend.js.lower.AbstractBlockDecomposerLowering
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.ir.expressions.IrExpression
class WasmBlockDecomposerLowering(val context: WasmBackendContext) : AbstractBlockDecomposerLowering(context) {
override fun unreachableExpression(): IrExpression = TODO()
}
@@ -6,40 +6,139 @@
package org.jetbrains.kotlin.backend.wasm.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.lower.DeclarationIrBuilder
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.ir.backend.js.utils.isEqualsInheritedFromAny
import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.builders.irComposite
import org.jetbrains.kotlin.ir.builders.irInt
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
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.types.IrType
import org.jetbrains.kotlin.ir.types.getClass
import org.jetbrains.kotlin.ir.types.isNullable
import org.jetbrains.kotlin.ir.util.functions
import org.jetbrains.kotlin.ir.util.irCall
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.util.isFunction
import org.jetbrains.kotlin.ir.util.isNullConst
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.util.OperatorNameConventions
class BuiltInsLowering(val context: WasmBackendContext) : FileLoweringPass {
private val irBuiltins = context.irBuiltIns
private val symbols = context.wasmSymbols
fun transformCall(call: IrCall): IrExpression {
private fun IrType.findEqualsMethod(): IrSimpleFunction {
val klass = getClass() ?: irBuiltins.anyClass.owner
return klass.functions.single { it.isEqualsInheritedFromAny() }
}
fun transformCall(
call: IrCall,
builder: DeclarationIrBuilder
): IrExpression {
when (val symbol = call.symbol) {
irBuiltins.eqeqSymbol, irBuiltins.eqeqeqSymbol, in irBuiltins.ieee754equalsFunByOperandType.values -> {
irBuiltins.ieee754equalsFunByOperandType[irBuiltins.floatClass] -> {
if (call.getValueArgument(0)!!.type.isNullable() || call.getValueArgument(1)!!.type.isNullable()) {
return irCall(call, symbols.nullableFloatIeee754Equals)
}
return irCall(call, symbols.floatEqualityFunctions.getValue(irBuiltins.floatType))
}
irBuiltins.ieee754equalsFunByOperandType[irBuiltins.doubleClass] -> {
if (call.getValueArgument(0)!!.type.isNullable() || call.getValueArgument(1)!!.type.isNullable()) {
return irCall(call, symbols.nullableDoubleIeee754Equals)
}
return irCall(call, symbols.floatEqualityFunctions.getValue(irBuiltins.doubleType))
}
irBuiltins.eqeqSymbol -> {
val lhs = call.getValueArgument(0)!!
val rhs = call.getValueArgument(1)!!
val lhsType = lhs.type
val rhsType = rhs.type
if (lhsType == rhsType) {
val newSymbol = symbols.equalityFunctions[lhsType]
if (newSymbol != null) {
return irCall(call, newSymbol)
}
}
if (lhs.isNullConst()) {
return builder.irCall(symbols.refIsNull).apply { putValueArgument(0, rhs) }
}
if (rhs.isNullConst()) {
return builder.irCall(symbols.refIsNull).apply { putValueArgument(0, lhs) }
}
if (!lhsType.isNullable()) {
return irCall(call, lhsType.findEqualsMethod().symbol, argumentsAsReceivers = true)
}
return irCall(call, symbols.nullableEquals)
}
irBuiltins.eqeqeqSymbol -> {
val type = call.getValueArgument(0)!!.type
val newSymbol = symbols.equalityFunctions[type]
?: error("Unsupported equality operator with type: ${type.render()}")
val newSymbol = symbols.equalityFunctions[type] ?: symbols.floatEqualityFunctions[type] ?: symbols.refEq
return irCall(call, newSymbol)
}
in symbols.irBuiltInsToWasmIntrinsics.keys -> {
val newSymbol = symbols.irBuiltInsToWasmIntrinsics[symbol]!!
irBuiltins.checkNotNullSymbol -> {
return irCall(call, symbols.ensureNotNull).also {
it.putTypeArgument(0, call.type)
}
}
in symbols.comparisonBuiltInsToWasmIntrinsics.keys -> {
val newSymbol = symbols.comparisonBuiltInsToWasmIntrinsics[symbol]!!
return irCall(call, newSymbol)
}
// TODO: Implement
irBuiltins.noWhenBranchMatchedExceptionSymbol ->
return builder.irCall(symbols.wasmUnreachable, irBuiltins.nothingType)
// TODO: Implement
irBuiltins.illegalArgumentExceptionSymbol ->
return builder.irCall(symbols.wasmUnreachable, irBuiltins.nothingType)
irBuiltins.dataClassArrayMemberHashCodeSymbol -> {
// TODO: Implement
return builder.irComposite {
+call.getValueArgument(0)!!
+irInt(7777)
}
}
irBuiltins.dataClassArrayMemberToStringSymbol -> {
// TODO: Implement
return builder.irCall(symbols.anyNtoString).apply {
putValueArgument(0, call.getValueArgument(0))
}
}
}
val nativeInvokeArity = getKotlinFunctionInvokeArity(call)
if (nativeInvokeArity != null) {
return irCall(call, symbols.functionNInvokeMethods[nativeInvokeArity])
}
return call
}
private fun getKotlinFunctionInvokeArity(call: IrCall): Int? {
val simpleFunction = call.symbol.owner as? IrSimpleFunction ?: return null
val receiverType = simpleFunction.dispatchReceiverParameter?.type ?: return null
if (simpleFunction.isSuspend) return null
if (simpleFunction.name == OperatorNameConventions.INVOKE && receiverType.isFunction()) {
return simpleFunction.valueParameters.size
}
return null
}
override fun lower(irFile: IrFile) {
irFile.transformChildrenVoid(object : IrElementTransformerVoid() {
val builder = context.createIrBuilder(irFile.symbol)
irFile.transformChildrenVoid(object : IrElementTransformerVoidWithContext() {
override fun visitCall(expression: IrCall): IrExpression {
val newExpression = transformCall(expression)
val newExpression = transformCall(expression, builder)
newExpression.transformChildrenVoid(this)
return newExpression
}
@@ -0,0 +1,91 @@
/*
* 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.wasm.lower
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.ir.copyTo
import org.jetbrains.kotlin.backend.common.ir.isOverridableOrOverrides
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.builders.irGet
import org.jetbrains.kotlin.ir.builders.irImplicitCast
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrGetValue
import org.jetbrains.kotlin.ir.types.isAny
import org.jetbrains.kotlin.ir.util.isInterface
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
/**
* This lowering erases dispatch receiver types of virtual functions down to Any.
*
* WebAssembly function types are contravariant on their parameter types.
* But since child classes are not supertypes of parents, in order for virtual method
* reference to share the same v-table slot as parent's method, it's dispatch receiver type
* has to be erased at least down to type of the parent class.
*
* Current implementation is rather conservative:
* - Always erases parameter type down to Any
* - Inserts casts back to original type in every usage of dispatch receiver.
*
* Possible optimisations:
* - Instead of erasing type to Any, erase type down to least concrete supertype containing virtual method
* - Don't erase type at all if bridge will be needed anyway
* Cast receiver in bridge. This would keep precise type for direct calls
* - Cast `this` and assign it to local variable if dispatch receiver is used often
* - Don't cast if usages of `this` don't require precise type
* - Always use bridge + Wasm tail call
*
* Related issue: [https://github.com/WebAssembly/gc/issues/29]
*/
class EraseVirtualDispatchReceiverParametersTypes(val context: CommonBackendContext) : FileLoweringPass {
override fun lower(file: IrFile) {
file.acceptChildrenVoid(object : IrElementVisitorVoid {
override fun visitElement(element: IrElement) {
element.acceptChildrenVoid(this)
}
override fun visitFunction(declaration: IrFunction) {
lower(declaration)
super.visitFunction(declaration)
}
})
}
fun lower(irFunction: IrFunction) {
// Lower only functions that override other functions
if (irFunction !is IrSimpleFunction) return
if (!irFunction.isOverridableOrOverrides) return
val oldReceiver = irFunction.dispatchReceiverParameter!!
val originalReceiverType = oldReceiver.type
// Interfaces in Wasm are erased to Any, so they already have appropriate type
if (originalReceiverType.isInterface() || originalReceiverType.isAny()) return
val builder = context.createIrBuilder(irFunction.symbol)
val newReceiver = oldReceiver.copyTo(irFunction, type = context.irBuiltIns.anyType)
irFunction.dispatchReceiverParameter = newReceiver
// Cast receiver usages back to original type
irFunction.transformChildrenVoid(object : IrElementTransformerVoid() {
override fun visitGetValue(expression: IrGetValue): IrExpression {
if (expression.symbol == oldReceiver.symbol) {
return with(builder) {
irImplicitCast(irGet(newReceiver), originalReceiverType)
}
}
return expression
}
})
}
}
@@ -8,102 +8,22 @@ package org.jetbrains.kotlin.backend.wasm.lower
import org.jetbrains.kotlin.backend.common.ir.addChild
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.backend.wasm.utils.hasExcludedFromCodegenAnnotation
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
import org.jetbrains.kotlin.name.FqName
private val BODILESS_BUILTIN_CLASSES = listOf(
"kotlin.Nothing",
"kotlin.Array",
"kotlin.Any",
"kotlin.ByteArray",
"kotlin.CharArray",
"kotlin.ShortArray",
"kotlin.IntArray",
"kotlin.LongArray",
"kotlin.FloatArray",
"kotlin.DoubleArray",
"kotlin.BooleanArray",
"kotlin.Boolean",
"kotlin.Function",
"kotlin.Throwable",
"kotlin.Suppress",
"kotlin.SinceKotlin",
"kotlin.Deprecated",
"kotlin.ReplaceWith",
"kotlin.DeprecationLevel",
"kotlin.UnsafeVariance",
"kotlin.reflect.KType",
"kotlin.reflect.KTypeProjection",
"kotlin.reflect.Companion",
"kotlin.reflect.KTypeParameter",
"kotlin.reflect.KDeclarationContainer",
"kotlin.reflect.KProperty",
"kotlin.reflect.KProperty0",
"kotlin.reflect.KProperty1",
"kotlin.reflect.KProperty2",
"kotlin.reflect.KMutableProperty0",
"kotlin.reflect.KMutableProperty",
"kotlin.reflect.KMutableProperty1",
"kotlin.reflect.KMutableProperty2",
"kotlin.reflect.Accessor",
"kotlin.reflect.Getter",
"kotlin.reflect.KFunction",
"kotlin.reflect.KVariance",
"kotlin.reflect.KVisibility",
"kotlin.reflect.KClass",
"kotlin.reflect.KCallable",
"kotlin.reflect.KClassifier",
"kotlin.reflect.KParameter",
"kotlin.reflect.Kind",
"kotlin.reflect.KAnnotatedElement",
"kotlin.annotation.Target",
"kotlin.annotation.AnnotationTarget",
"kotlin.annotation.Retention",
"kotlin.annotation.AnnotationRetention",
"kotlin.annotation.MustBeDocumented",
"kotlin.Unit",
"kotlin.collections.BooleanIterator",
"kotlin.collections.CharIterator",
"kotlin.collections.ByteIterator",
"kotlin.collections.ShortIterator",
"kotlin.collections.IntIterator",
"kotlin.collections.FloatIterator",
"kotlin.collections.LongIterator",
"kotlin.collections.DoubleIterator",
"kotlin.internal.PlatformDependent",
"kotlin.CharSequence",
"kotlin.Annotation",
"kotlin.Comparable",
"kotlin.collections.Collection",
"kotlin.collections.Iterable",
"kotlin.collections.List",
"kotlin.collections.Map",
"kotlin.collections.Set",
"kotlin.collections.MutableCollection",
"kotlin.collections.MutableIterable",
"kotlin.collections.MutableSet",
"kotlin.collections.MutableList",
"kotlin.collections.MutableMap",
"kotlin.collections.Entry",
"kotlin.collections.MutableEntry",
"kotlin.Number",
"kotlin.Enum",
"kotlin.collections.Iterator",
"kotlin.collections.ListIterator",
"kotlin.collections.MutableIterator",
"kotlin.collections.MutableListIterator"
).map { FqName(it) }.toSet()
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrDeclarationWithName
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
/**
* Move intrinsics marked with @ExcludedFromCodegen to special excluded files.
* All references to these declarations must be lowered or treated in a special way in a codegen.
*/
fun excludeDeclarationsFromCodegen(context: WasmBackendContext, module: IrModuleFragment) {
fun isExcluded(declaration: IrDeclaration): Boolean {
if (declaration is IrDeclarationWithName && declaration.fqNameWhenAvailable in BODILESS_BUILTIN_CLASSES)
return true
// Annotation can be applied to top-level declarations ...
if (declaration.hasExcludedFromCodegenAnnotation())
return true
// ... or files as a whole
val parentFile = declaration.parent as? IrFile
if (parentFile?.hasExcludedFromCodegenAnnotation() == true)
return true
@@ -117,7 +37,8 @@ fun excludeDeclarationsFromCodegen(context: WasmBackendContext, module: IrModule
val d = it.next() as? IrDeclarationWithName ?: continue
if (isExcluded(d)) {
it.remove()
context.excludedDeclarations.addChild(d)
// Move to "excluded" package fragment preserving fq-name
context.getExcludedPackageFragment(file.fqName).addChild(d)
}
}
}
@@ -0,0 +1,53 @@
/*
* 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.wasm.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.lower.at
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.builders.irSetField
import org.jetbrains.kotlin.ir.declarations.IrField
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.expressions.IrBlockBody
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
/**
* Move initialization of global fields to start function.
*
* WebAssembly allows only constant expressions to be used directly in
* field initializers.
*
* TODO: Don't move constant expression initializers
* TODO: Make field initialization lazy. Needs design.
*/
class FieldInitializersLowering(val context: WasmBackendContext) : FileLoweringPass {
override fun lower(irFile: IrFile) {
val builder = context.createIrBuilder(context.startFunction.symbol)
val startFunctionBody = context.startFunction.body as IrBlockBody
irFile.acceptChildrenVoid(object : IrElementVisitorVoid {
override fun visitElement(element: IrElement) {
element.acceptChildrenVoid(this)
}
override fun visitField(declaration: IrField) {
super.visitField(declaration)
if (!declaration.isStatic) return
val initValue: IrExpression = declaration.initializer?.expression ?: return
startFunctionBody.statements.add(
builder.at(initValue).irSetField(null, declaration, initValue)
)
// Replace initializer with default one
declaration.initializer = null
}
})
}
}
@@ -0,0 +1,86 @@
/*
* 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.wasm.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.common.lower.irComposite
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.backend.wasm.ir2wasm.erasedUpperBound
import org.jetbrains.kotlin.ir.backend.js.utils.realOverrideTarget
import org.jetbrains.kotlin.ir.builders.irImplicitCast
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.ir.util.irCall
import org.jetbrains.kotlin.ir.util.isTypeParameter
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
/**
* This lowering adds implicit casts in places where erased generic function return type
* differs from expected type on the call site.
*/
class GenericReturnTypeLowering(val context: WasmBackendContext) : FileLoweringPass {
override fun lower(irFile: IrFile) {
irFile.transformChildrenVoid(object : IrElementTransformerVoidWithContext() {
override fun visitCall(expression: IrCall): IrExpression =
transformGenericCall(
super.visitCall(expression) as IrCall,
currentScope!!.scope.scopeOwnerSymbol
)
})
}
private fun IrType.eraseUpperBoundType(): IrType {
val type = erasedUpperBound?.defaultType ?: return context.irBuiltIns.anyNType
return if (this.isNullable())
type.makeNullable()
else
type
}
private fun transformGenericCall(call: IrCall, scopeOwnerSymbol: IrSymbol): IrExpression {
val function: IrSimpleFunction =
call.symbol.owner as? IrSimpleFunction ?: return call
if (!function.realOverrideTarget.returnType.isTypeParameter())
return call
val erasedReturnType: IrType =
function.realOverrideTarget.returnType.eraseUpperBoundType()
val callType = call.type
if (erasedReturnType != call.type) {
if (callType.isNothing()) return call
if (erasedReturnType.isSubtypeOf(callType, context.irBuiltIns)) return call
// Erase type parameter from call return type
val newCall = irCall(
call,
function.symbol,
newReturnType = erasedReturnType,
newSuperQualifierSymbol = call.superQualifierSymbol
)
context.createIrBuilder(scopeOwnerSymbol).apply {
if (call.type.isUnit()) {
return irComposite(call) {
+newCall
}
}
return irImplicitCast(newCall, call.type)
}
}
return call
}
}
@@ -0,0 +1,91 @@
/*
* Copyright 2010-2020 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.wasm.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.lower.DeclarationIrBuilder
import org.jetbrains.kotlin.backend.common.lower.at
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.builders.irString
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrStringConcatenation
import org.jetbrains.kotlin.ir.types.getClass
import org.jetbrains.kotlin.ir.types.isNullable
import org.jetbrains.kotlin.ir.types.isString
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.name.Name
/**
* Replace string concatenation with a chain of String.plus calls.
* TODO: Reuse common StringConcatenationLowering which uses string builder
*/
class SimpleStringConcatenationLowering(val context: WasmBackendContext) : FileLoweringPass {
override fun lower(irFile: IrFile) {
irFile.transformChildrenVoid(StringConcatenationTransformer(this))
}
}
private class StringConcatenationTransformer(val lower: SimpleStringConcatenationLowering) : IrElementTransformerVoidWithContext() {
private val context = lower.context
private val irBuiltIns = context.irBuiltIns
private val stringPlus = irBuiltIns.stringClass.owner.declarations.filterIsInstance<IrSimpleFunction>().find {
it.name == Name.identifier("plus")
}!!
private val anyToString = irBuiltIns.anyClass.owner.declarations.filterIsInstance<IrSimpleFunction>().find {
it.name == Name.identifier("toString")
}!!
private val anyNToString = context.wasmSymbols.anyNtoString
override fun visitStringConcatenation(expression: IrStringConcatenation): IrExpression {
val transformed = super.visitStringConcatenation(expression) as IrStringConcatenation
val builder: DeclarationIrBuilder = context.createIrBuilder(currentScope!!.scope.scopeOwnerSymbol).at(expression)
return transformed.arguments.fold<IrExpression, IrExpression>(
builder.irString("")
) { acc, el ->
builder.irCall(stringPlus).apply {
dispatchReceiver = acc
putValueArgument(0, expressionToString(el, builder))
}
}
}
private fun expressionToString(expression: IrExpression, builder: DeclarationIrBuilder): IrExpression {
if (expression.type.isString()) return expression
builder.at(expression)
val klass = expression.type.getClass()
if (expression.type.isNullable()) {
return builder.irCall(anyNToString).apply {
putValueArgument(0, expression)
}
}
val toStringMethod: IrSimpleFunction = if (klass != null) {
klass.declarations.filterIsInstance<IrSimpleFunction>().find { it.isToStringInheritedFromAny() }!!
} else {
anyToString
}
return builder.irCall(toStringMethod).apply {
dispatchReceiver = expression
}
}
}
fun IrFunction.isToStringInheritedFromAny() =
name == Name.identifier("toString") &&
dispatchReceiverParameter != null &&
extensionReceiverParameter == null &&
valueParameters.isEmpty()
@@ -0,0 +1,68 @@
/*
* 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.wasm.lower
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.ir.isOverridable
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.common.lower.irBlock
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.backend.js.utils.realOverrideTarget
import org.jetbrains.kotlin.ir.builders.createTmpVariable
import org.jetbrains.kotlin.ir.builders.irGet
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrGetValue
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
/**
* During Wasm code generation, dispatch receiver can be used multiple times.
* Move it to temporary variable if it is complex or can have side effects.
*/
class VirtualDispatchReceiverExtraction(val context: CommonBackendContext) : FileLoweringPass {
override fun lower(irFile: IrFile) {
irFile.acceptChildrenVoid(object : IrElementVisitorVoid {
override fun visitElement(element: IrElement) {
element.acceptChildrenVoid(this)
}
override fun visitFunction(declaration: IrFunction) {
lower(declaration)
super.visitFunction(declaration)
}
})
}
fun lower(irFunction: IrFunction) {
irFunction.transformChildrenVoid(object : IrElementTransformerVoid() {
override fun visitCall(expression: IrCall): IrExpression {
expression.transformChildrenVoid(this)
val function = expression.symbol.owner.realOverrideTarget
val receiver = expression.dispatchReceiver
if (receiver == null || !function.isOverridable)
return expression
// TODO: Keep other simple receivers without side effects
// receiver.isPure(true) ?
if (receiver is IrGetValue)
return expression
return with(context.createIrBuilder(irFunction.symbol)) {
irBlock(expression) {
val tmp = createTmpVariable(receiver)
expression.dispatchReceiver = irGet(tmp)
+expression
}
}
}
})
}
}
@@ -0,0 +1,55 @@
/*
* 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.wasm.lower
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.wasm.ir2wasm.erasedUpperBound
import org.jetbrains.kotlin.ir.backend.js.JsCommonBackendContext
import org.jetbrains.kotlin.ir.backend.js.lower.BridgesConstruction
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.isNullable
import org.jetbrains.kotlin.ir.types.makeNullable
import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.name.Name
class WasmBridgesConstruction(context: JsCommonBackendContext) : BridgesConstruction(context) {
override fun getFunctionSignature(function: IrSimpleFunction): WasmSignature =
function.wasmSignature(context.irBuiltIns)
// Dispatch receiver type must be casted when types are different.
override val shouldCastDispatchReceiver: Boolean = true
}
data class WasmSignature(
val name: Name,
val extensionReceiverType: IrType?,
val valueParametersType: List<IrType>,
val returnType: IrType
) {
override fun toString(): String {
val er = extensionReceiverType?.let { "(er: ${it.render()}) " } ?: ""
val parameters = valueParametersType.joinToString(", ") { it.render() }
return "[$er$name($parameters) -> ${returnType.render()}]"
}
}
fun IrSimpleFunction.wasmSignature(irBuiltIns: IrBuiltIns): WasmSignature =
WasmSignature(
name,
extensionReceiverParameter?.type?.eraseGenerics(irBuiltIns),
valueParameters.map { it.type.eraseGenerics(irBuiltIns) },
returnType.eraseGenerics(irBuiltIns)
)
private fun IrType.eraseGenerics(irBuiltIns: IrBuiltIns): IrType {
val defaultType = this.erasedUpperBound?.defaultType ?: irBuiltIns.anyType
if (!this.isNullable()) return defaultType
return defaultType.makeNullable()
}
@@ -0,0 +1,314 @@
/*
* 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.wasm.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.ir.copyTo
import org.jetbrains.kotlin.backend.common.ir.createImplicitParameterDeclarationWithWrappedDescriptor
import org.jetbrains.kotlin.backend.common.ir.isSuspend
import org.jetbrains.kotlin.backend.common.ir.moveBodyTo
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.builders.declarations.addConstructor
import org.jetbrains.kotlin.ir.builders.declarations.addFunction
import org.jetbrains.kotlin.ir.builders.declarations.addValueParameter
import org.jetbrains.kotlin.ir.builders.declarations.buildClass
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.name.SpecialNames
/**
* TODO: Temporary lowering stub. Needs to be redone.
* This is a copy of JVM lowering, but parts that don't compile are commented out.
* Turns out this works decently as a stub in most tests.
*/
val IrStatementOrigin?.isLambda: Boolean
get() = this == IrStatementOrigin.LAMBDA || this == IrStatementOrigin.ANONYMOUS_FUNCTION
// Originally copied from K/Native
internal class WasmCallableReferenceLowering(private val context: WasmBackendContext) : FileLoweringPass, IrElementTransformerVoidWithContext() {
// This pass ignores suspend function references and function references used in inline arguments to inline functions.
private val ignoredFunctionReferences = mutableSetOf<IrFunctionReference>()
private val IrFunctionReference.isIgnored: Boolean
get() = (!type.isFunctionOrKFunction() || ignoredFunctionReferences.contains(this)) && !isSuspendCallableReference()
// TODO: Currently, origin of callable references is null. Do we need to create one?
private fun IrFunctionReference.isSuspendCallableReference(): Boolean = isSuspend && origin == null
override fun lower(irFile: IrFile) {
// ignoredFunctionReferences.addAll(IrInlineReferenceLocator.scan(context, irFile))
irFile.transformChildrenVoid(this)
}
override fun visitBlock(expression: IrBlock): IrExpression {
if (!expression.origin.isLambda)
return super.visitBlock(expression)
val reference = expression.statements.last() as IrFunctionReference
if (reference.isIgnored)
return super.visitBlock(expression)
expression.statements.dropLast(1).forEach { it.transform(this, null) }
reference.transformChildrenVoid(this)
return FunctionReferenceBuilder(reference).build()
}
override fun visitFunctionReference(expression: IrFunctionReference): IrExpression {
expression.transformChildrenVoid(this)
return if (expression.isIgnored) expression else FunctionReferenceBuilder(expression).build()
}
// Handle SAM conversions which wrap a function reference:
// class sam$n(private val receiver: R) : Interface { override fun method(...) = receiver.target(...) }
//
// This avoids materializing an invokable KFunction representing, thus producing one less class.
// This is actually very common, as `Interface { something }` is a local function + a SAM-conversion
// of a reference to it into an implementation.
override fun visitTypeOperator(expression: IrTypeOperatorCall): IrExpression {
if (expression.operator == IrTypeOperator.SAM_CONVERSION) {
val invokable = expression.argument
val reference = if (invokable is IrFunctionReference) {
invokable
} else if (invokable is IrBlock && invokable.origin.isLambda && invokable.statements.last() is IrFunctionReference) {
invokable.statements.dropLast(1).forEach { it.transform(this, null) }
invokable.statements.last() as IrFunctionReference
} else {
return super.visitTypeOperator(expression)
}
reference.transformChildrenVoid()
return FunctionReferenceBuilder(reference, expression.typeOperand).build()
}
return super.visitTypeOperator(expression)
}
private inner class FunctionReferenceBuilder(val irFunctionReference: IrFunctionReference, val samSuperType: IrType? = null) {
private val isLambda = irFunctionReference.origin.isLambda
private val callee = irFunctionReference.symbol.owner
// Only function references can bind a receiver and even then we can only bind either an extension or a dispatch receiver.
// However, when we bind a value of an inline class type as a receiver, the receiver will turn into an argument of
// the function in question. Yet we still need to record it as the "receiver" in CallableReference in order for reflection
// to work correctly.
private val boundReceiver: Pair<IrValueParameter, IrExpression>? = irFunctionReference.getArgumentsWithIr().singleOrNull()
// The type of the reference is KFunction<in A1, ..., in An, out R>
private val parameterTypes = (irFunctionReference.type as IrSimpleType).arguments.map { (it as IrTypeProjection).type }
private val argumentTypes = parameterTypes.dropLast(1)
private val typeArgumentsMap = irFunctionReference.typeSubstitutionMap
private val functionSuperClass =
samSuperType?.classOrNull
?: if (irFunctionReference.isSuspend)
context.ir.symbols.suspendFunctionN(argumentTypes.size)
else
context.ir.symbols.functionN(argumentTypes.size)
private val superMethod =
functionSuperClass.functions.single { it.owner.modality == Modality.ABSTRACT }
// TODO(WASM)
// private val superType =
// samSuperType ?: (if (isLambda) context.ir.symbols.lambdaClass else context.ir.symbols.functionReference).defaultType
private val functionReferenceClass = context.irFactory.buildClass {
setSourceRange(irFunctionReference)
visibility = DescriptorVisibilities.LOCAL
// A callable reference results in a synthetic class, while a lambda is not synthetic.
// We don't produce GENERATED_SAM_IMPLEMENTATION, which is always synthetic.
// TODO(WASM)
// origin = if (isLambda) JvmLoweredDeclarationOrigin.LAMBDA_IMPL else JvmLoweredDeclarationOrigin.FUNCTION_REFERENCE_IMPL
name = SpecialNames.NO_NAME_PROVIDED
}.apply {
parent = currentDeclarationParent!!
// TODO(WASM)
// superTypes += superType
if (samSuperType == null)
superTypes += functionSuperClass.typeWith(parameterTypes)
// TODO(WASM)
// if (irFunctionReference.isSuspend) superTypes += context.ir.symbols.suspendFunctionInterface.defaultType
createImplicitParameterDeclarationWithWrappedDescriptor()
copyAttributes(irFunctionReference)
if (isLambda) {
this.metadata = irFunctionReference.symbol.owner.metadata
}
}
// WASM(TODO)
// private val receiverFieldFromSuper = context.ir.symbols.functionReferenceReceiverField.owner
//
// val fakeOverrideReceiverField = functionReferenceClass.addField {
// name = receiverFieldFromSuper.name
// origin = IrDeclarationOrigin.FAKE_OVERRIDE
// type = receiverFieldFromSuper.type
// isFinal = receiverFieldFromSuper.isFinal
// isStatic = receiverFieldFromSuper.isStatic
// visibility = receiverFieldFromSuper.visibility
// }
fun build(): IrExpression = context.createIrBuilder(currentScope!!.scope.scopeOwnerSymbol).run {
irBlock {
val constructor = createConstructor()
createInvokeMethod(
if (samSuperType != null && boundReceiver != null) {
irTemporary(boundReceiver.second)
} else null
)
// WASM(TODO)
// if (!isLambda && samSuperType == null) {
// createGetSignatureMethod(this@run.irSymbols.functionReferenceGetSignature.owner)
// createGetNameMethod(this@run.irSymbols.functionReferenceGetName.owner)
// createGetOwnerMethod(this@run.irSymbols.functionReferenceGetOwner.owner)
// }
+functionReferenceClass
+irCall(constructor.symbol).apply {
if (valueArgumentsCount > 0) putValueArgument(0, boundReceiver!!.second)
}
}
}
private fun createConstructor(): IrConstructor =
functionReferenceClass.addConstructor {
// origin = JvmLoweredDeclarationOrigin.GENERATED_MEMBER_IN_CALLABLE_REFERENCE
returnType = functionReferenceClass.defaultType
isPrimary = true
}.apply {
// Add receiver parameter for bound function references
if (samSuperType == null) {
boundReceiver?.first?.let { param ->
valueParameters += param.copyTo(
irFunction = this,
index = 0,
type = param.type.substitute(typeArgumentsMap)
)
}
}
// Super constructor:
// - For SAM references, the super class is Any
// - For function references with bound receivers, accepts arity and receiver
// - For lambdas and function references without bound receivers, accepts arity
// WASM_TODO
// val constructor = if (samSuperType != null) {
// context.irBuiltIns.anyClass.owner.constructors.single()
// } else {
// superType.getClass()!!.constructors.single {
// it.valueParameters.size == if (boundReceiver != null) 2 else 1
// }
// }
val constructor = context.irBuiltIns.anyClass.owner.constructors.single()
body = context.createIrBuilder(symbol).irBlockBody(startOffset, endOffset) {
+irDelegatingConstructorCall(constructor).apply {
// WASM_TODO
// if (samSuperType == null) {
// putValueArgument(0, irInt(argumentTypes.size + if (irFunctionReference.isSuspend) 1 else 0))
// if (boundReceiver != null)
// putValueArgument(1, irGet(valueParameters.first()))
// }
}
+IrInstanceInitializerCallImpl(startOffset, endOffset, functionReferenceClass.symbol, context.irBuiltIns.unitType)
}
}
private fun createInvokeMethod(receiverVar: IrValueDeclaration?): IrSimpleFunction =
functionReferenceClass.addFunction {
setSourceRange(if (isLambda) callee else irFunctionReference)
name = superMethod.owner.name
returnType = callee.returnType
isSuspend = callee.isSuspend
}.apply {
overriddenSymbols += superMethod
dispatchReceiverParameter = parentAsClass.thisReceiver!!.copyTo(this)
if (isLambda) createLambdaInvokeMethod() else createFunctionReferenceInvokeMethod(receiverVar)
}
// Inline the body of an anonymous function into the generated lambda subclass.
private fun IrSimpleFunction.createLambdaInvokeMethod() {
annotations += callee.annotations
val valueParameterMap = callee.explicitParameters.withIndex().associate { (index, param) ->
param to param.copyTo(this, index = index)
}
valueParameters += valueParameterMap.values
body = callee.moveBodyTo(this, valueParameterMap)
}
private fun IrSimpleFunction.createFunctionReferenceInvokeMethod(receiver: IrValueDeclaration?) {
for ((index, argumentType) in argumentTypes.withIndex()) {
addValueParameter {
name = Name.identifier("p$index")
type = argumentType
}
}
body = context.createIrBuilder(symbol).run {
var unboundIndex = 0
irExprBody(irCall(callee).apply {
for ((typeParameter, typeArgument) in typeArgumentsMap) {
putTypeArgument(typeParameter.owner.index, typeArgument)
}
for (parameter in callee.explicitParameters) {
when {
boundReceiver?.first == parameter ->
// Bound receiver parameter. For function references, this is stored in a field of the superclass.
// For sam references, we just capture the value in a local variable and LocalDeclarationsLowering
// will put it into a field.
// if (samSuperType == null)
// irImplicitCast(
// irGetField(irGet(dispatchReceiverParameter!!), fakeOverrideReceiverField),
// boundReceiver.second.type
// )
// else
irGet(receiver ?: error("Binding receivers is not supported yet"))
// If a vararg parameter corresponds to exactly one KFunction argument, which is an array, that array
// is forwarded as is.
//
// fun f(x: (Int, Array<String>) -> String) = x(0, arrayOf("OK", "FAIL"))
// fun h(i: Int, vararg xs: String) = xs[i]
// f(::h)
//
parameter.isVararg && unboundIndex < argumentTypes.size && parameter.type == valueParameters[unboundIndex].type ->
irGet(valueParameters[unboundIndex++])
// In all other cases, excess arguments are packed into a new array.
//
// fun g(x: (Int, String, String) -> String) = x(0, "OK", "FAIL")
// f(::h) == g(::h)
//
parameter.isVararg && (unboundIndex < argumentTypes.size || !parameter.hasDefaultValue()) ->
TODO()
unboundIndex >= argumentTypes.size ->
// Default value argument (this pass doesn't handle suspend functions, otherwise
// it could also be the continuation argument)
null
else ->
irGet(valueParameters[unboundIndex++])
}?.let { putArgument(callee, parameter, it) }
}
})
}
}
}
}
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2020 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.wasm.lower
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.backend.js.JsCommonBackendContext
import org.jetbrains.kotlin.ir.backend.js.ir.JsIrBuilder
import org.jetbrains.kotlin.ir.backend.js.lower.AbstractValueUsageLowering
import org.jetbrains.kotlin.ir.expressions.IrConstKind
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.isNothing
import org.jetbrains.kotlin.ir.types.isNullable
import org.jetbrains.kotlin.ir.types.makeNotNull
/**
* Replace null constants of type Nothing? with null constants of a concrete class types.
*
* Wasm GC doesn't have a nullref type anymore.
*/
class WasmNullCoercingLowering(context: JsCommonBackendContext) : AbstractValueUsageLowering(context) {
override fun IrExpression.useExpressionAsType(actualType: IrType, expectedType: IrType): IrExpression =
if (actualType.makeNotNull().isNothing() && actualType.isNullable() && !expectedType.makeNotNull().isNothing())
JsIrBuilder.buildComposite(
type,
listOf(this, IrConstImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, expectedType, IrConstKind.Null, null))
)
else
this
}
@@ -0,0 +1,220 @@
/*
* Copyright 2010-2020 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.wasm.lower
import org.jetbrains.kotlin.backend.common.ir.SharedVariablesManager
import org.jetbrains.kotlin.backend.common.lower.InnerClassesSupport
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.backend.js.JsCommonBackendContext
import org.jetbrains.kotlin.ir.backend.js.JsLoweredDeclarationOrigin
import org.jetbrains.kotlin.ir.backend.js.ir.JsIrBuilder
import org.jetbrains.kotlin.ir.builders.declarations.buildClass
import org.jetbrains.kotlin.ir.builders.declarations.buildValueParameter
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrVariableImpl
import org.jetbrains.kotlin.ir.descriptors.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrVariableSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrClassSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrConstructorSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrFieldSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrVariableSymbolImpl
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
import org.jetbrains.kotlin.ir.util.defaultType
import org.jetbrains.kotlin.name.Name
/**
* This is a copy of an old version of JS lowering, because JS did platform-specific optimization incompatible with Wasm.
* TODO: Revisit
*/
@OptIn(ObsoleteDescriptorBasedAPI::class)
class WasmSharedVariablesManager(val context: JsCommonBackendContext, val builtIns: IrBuiltIns, val implicitDeclarationsFile: IrPackageFragment) : SharedVariablesManager {
override fun declareSharedVariable(originalDeclaration: IrVariable): IrVariable {
val initializer = originalDeclaration.initializer ?: IrConstImpl.constNull(
originalDeclaration.startOffset,
originalDeclaration.endOffset,
builtIns.nothingNType
)
val constructorSymbol = closureBoxConstructorDeclaration.symbol
val irCall =
IrConstructorCallImpl.fromSymbolDescriptor(initializer.startOffset, initializer.endOffset, closureBoxType, constructorSymbol)
.apply {
putValueArgument(0, initializer)
}
val descriptor = WrappedVariableDescriptor()
return IrVariableImpl(
originalDeclaration.startOffset,
originalDeclaration.endOffset,
originalDeclaration.origin,
IrVariableSymbolImpl(descriptor),
originalDeclaration.name,
irCall.type,
false,
false,
false
).also {
descriptor.bind(it)
it.parent = originalDeclaration.parent
it.initializer = irCall
}
}
override fun defineSharedValue(originalDeclaration: IrVariable, sharedVariableDeclaration: IrVariable) = sharedVariableDeclaration
override fun getSharedValue(sharedVariableSymbol: IrVariableSymbol, originalGet: IrGetValue): IrExpression {
val getField = IrGetFieldImpl(
originalGet.startOffset, originalGet.endOffset,
closureBoxFieldDeclaration.symbol,
closureBoxFieldDeclaration.type,
IrGetValueImpl(
originalGet.startOffset,
originalGet.endOffset,
closureBoxType,
sharedVariableSymbol,
originalGet.origin
),
originalGet.origin
)
return IrTypeOperatorCallImpl(
originalGet.startOffset,
originalGet.endOffset,
originalGet.type,
IrTypeOperator.IMPLICIT_CAST,
originalGet.type,
getField
)
}
override fun setSharedValue(sharedVariableSymbol: IrVariableSymbol, originalSet: IrSetValue): IrExpression =
IrSetFieldImpl(
originalSet.startOffset,
originalSet.endOffset,
closureBoxFieldDeclaration.symbol,
IrGetValueImpl(
originalSet.startOffset,
originalSet.endOffset,
closureBoxType,
sharedVariableSymbol,
originalSet.origin
),
originalSet.value,
originalSet.type,
originalSet.origin
)
private val boxTypeName = "\$closureBox\$"
private val closureBoxClassDeclaration by lazy {
createClosureBoxClassDeclaration()
}
private val closureBoxConstructorDeclaration by lazy {
createClosureBoxConstructorDeclaration()
}
private val closureBoxFieldDeclaration by lazy {
closureBoxPropertyDeclaration
}
private val closureBoxPropertyDeclaration by lazy {
createClosureBoxPropertyDeclaration()
}
private lateinit var closureBoxType: IrType
private fun createClosureBoxClassDeclaration(): IrClass {
val declaration = context.irFactory.buildClass {
origin = JsLoweredDeclarationOrigin.JS_CLOSURE_BOX_CLASS_DECLARATION
name = Name.identifier(boxTypeName)
visibility = DescriptorVisibilities.PUBLIC
modality = Modality.FINAL
isCompanion = false
isInner = false
isData = false
isExternal = false
isInline = false
isExpect = false
isFun = false
}
declaration.parent = implicitDeclarationsFile
// TODO: substitute
closureBoxType = IrSimpleTypeImpl(declaration.symbol, false, emptyList(), emptyList())
declaration.thisReceiver = buildValueParameter(declaration) {
name = Name.identifier("_this_")
index = -1
type = closureBoxType
}
implicitDeclarationsFile.declarations += declaration
return declaration
}
private fun createClosureBoxPropertyDeclaration(): IrField {
val descriptor = WrappedFieldDescriptor()
val symbol = IrFieldSymbolImpl(descriptor)
val fieldName = Name.identifier("v")
return context.irFactory.createField(
UNDEFINED_OFFSET,
UNDEFINED_OFFSET,
InnerClassesSupport.FIELD_FOR_OUTER_THIS,
symbol,
fieldName,
builtIns.anyNType,
DescriptorVisibilities.PUBLIC,
isFinal = false,
isExternal = false,
isStatic = false,
).also {
descriptor.bind(it)
it.parent = closureBoxClassDeclaration
closureBoxClassDeclaration.declarations += it
}
}
private fun createClosureBoxConstructorDeclaration(): IrConstructor {
val descriptor = WrappedClassConstructorDescriptor()
val symbol = IrConstructorSymbolImpl(descriptor)
val declaration = context.irFactory.createConstructor(
UNDEFINED_OFFSET, UNDEFINED_OFFSET, JsLoweredDeclarationOrigin.JS_CLOSURE_BOX_CLASS_DECLARATION, symbol,
Name.special("<init>"), DescriptorVisibilities.PUBLIC, closureBoxClassDeclaration.defaultType,
isInline = false, isExternal = false, isPrimary = true, isExpect = false
)
descriptor.bind(declaration)
declaration.parent = closureBoxClassDeclaration
val parameterDeclaration = createClosureBoxConstructorParameterDeclaration(declaration)
declaration.valueParameters += parameterDeclaration
val receiver = JsIrBuilder.buildGetValue(closureBoxClassDeclaration.thisReceiver!!.symbol)
val value = JsIrBuilder.buildGetValue(parameterDeclaration.symbol)
val setField = JsIrBuilder.buildSetField(closureBoxFieldDeclaration.symbol, receiver, value, builtIns.unitType)
declaration.body = context.irFactory.createBlockBody(UNDEFINED_OFFSET, UNDEFINED_OFFSET, listOf(setField))
closureBoxClassDeclaration.declarations += declaration
return declaration
}
private fun createClosureBoxConstructorParameterDeclaration(irConstructor: IrConstructor): IrValueParameter {
return JsIrBuilder.buildValueParameter(irConstructor,"p", 0, closureBoxPropertyDeclaration.type)
}
}
@@ -0,0 +1,37 @@
/*
* Copyright 2010-2020 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.wasm.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrThrow
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
/**
* Replace throw expressions with a runtime function call.
* TODO: Remove when full-blown exception handling is implemented
*/
internal class WasmThrowDebugLowering(
private val context: WasmBackendContext
) : FileLoweringPass, IrElementTransformerVoidWithContext() {
override fun lower(irFile: IrFile) {
irFile.transformChildrenVoid(this)
}
override fun visitThrow(expression: IrThrow): IrExpression {
expression.transformChildrenVoid(this)
val builder = context.createIrBuilder(currentScope!!.scope.scopeOwnerSymbol)
return builder.irCall(context.wasmSymbols.wasmThrow).apply {
this.putValueArgument(0, expression.value)
}
}
}
@@ -0,0 +1,287 @@
/*
* Copyright 2010-2020 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.wasm.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.lower.DeclarationIrBuilder
import org.jetbrains.kotlin.backend.common.lower.at
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.common.lower.irNot
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.backend.wasm.ir2wasm.erasedUpperBound
import org.jetbrains.kotlin.ir.backend.js.utils.isPure
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
class WasmTypeOperatorLowering(val context: WasmBackendContext) : FileLoweringPass {
override fun lower(irFile: IrFile) {
irFile.transformChildrenVoid(WasmBaseTypeOperatorTransformer(context))
}
}
class WasmBaseTypeOperatorTransformer(val context: WasmBackendContext) : IrElementTransformerVoidWithContext() {
private val symbols = context.wasmSymbols
private val builtIns = context.irBuiltIns
private lateinit var builder: DeclarationIrBuilder
override fun visitTypeOperator(expression: IrTypeOperatorCall): IrExpression {
super.visitTypeOperator(expression)
builder = context.createIrBuilder(currentScope!!.scope.scopeOwnerSymbol).at(expression)
return when (expression.operator) {
IrTypeOperator.IMPLICIT_CAST -> lowerImplicitCast(expression)
IrTypeOperator.IMPLICIT_DYNAMIC_CAST -> error("Dynamic casts are not supported in Wasm backend")
IrTypeOperator.IMPLICIT_COERCION_TO_UNIT -> expression.argument
IrTypeOperator.IMPLICIT_INTEGER_COERCION -> lowerIntegerCoercion(expression)
IrTypeOperator.IMPLICIT_NOTNULL -> lowerImplicitCast(expression)
IrTypeOperator.INSTANCEOF -> lowerInstanceOf(expression, inverted = false)
IrTypeOperator.NOT_INSTANCEOF -> lowerInstanceOf(expression, inverted = true)
IrTypeOperator.CAST -> lowerCast(expression, isSafe = false)
IrTypeOperator.SAFE_CAST -> lowerCast(expression, isSafe = true)
IrTypeOperator.SAM_CONVERSION -> TODO("SAM conversion: ${expression.render()}")
IrTypeOperator.REINTERPRET_CAST -> expression
}
}
private fun lowerInstanceOf(
expression: IrTypeOperatorCall,
inverted: Boolean
): IrExpression {
return builder.irComposite(resultType = builtIns.booleanType) {
val argument = cacheValue(expression.argument)
val check = generateTypeCheck(argument, expression.typeOperand)
if (inverted) {
+builder.irNot(check)
} else {
+check
}
}
}
private fun IrBlockBuilder.cacheValue(value: IrExpression): () -> IrExpressionWithCopy {
if (value.isPure(true) && value is IrExpressionWithCopy) {
return { value.deepCopyWithSymbols() }
}
val tmpVal = createTmpVariable(value)
return { builder.irGet(tmpVal) }
}
private fun IrType.isInlined(): Boolean =
context.inlineClassesUtils.isTypeInlined(this)
private val IrType.erasedType: IrType
get() = this.erasedUpperBound?.defaultType ?: builtIns.anyType
private fun generateTypeCheck(
valueProvider: () -> IrExpressionWithCopy,
toType: IrType
): IrExpression {
val toNotNullable = toType.makeNotNull()
val valueInstance: IrExpressionWithCopy = valueProvider()
val fromType = (valueInstance as IrExpression).type
// Inlined values have no type information on runtime.
// But since they are final we can compute type checks on compile time.
if (fromType.isInlined()) {
val result = fromType.erasedType.isSubtypeOf(toType.erasedType, builtIns)
return builder.irBoolean(result)
}
val instanceCheck = generateTypeCheckNonNull(valueInstance, toNotNullable)
val isFromNullable = valueInstance.type.isNullable()
val isToNullable = toType.isNullable()
return when {
!isFromNullable -> instanceCheck
else ->
builder.irIfThenElse(
type = builtIns.booleanType,
condition = builder.irEqualsNull(valueProvider() as IrExpression),
thenPart = builder.irBoolean(isToNullable),
elsePart = instanceCheck
)
}
}
private fun lowerIntegerCoercion(expression: IrTypeOperatorCall): IrExpression =
when (expression.typeOperand) {
builtIns.byteType,
builtIns.shortType ->
expression.argument
builtIns.longType ->
builder.irCall(symbols.intToLong).apply {
putValueArgument(0, expression.argument)
}
else -> error("Unreachable execution (coercion to non-Integer type")
}
private fun generateTypeCheckNonNull(argument: IrExpressionWithCopy, toType: IrType): IrExpression {
assert(!toType.isMarkedNullable())
return when {
toType.isNothing() -> builder.irComposite(resultType = builtIns.booleanType) {
+(argument as IrExpression)
+builder.irFalse()
}
toType.isTypeParameter() -> generateTypeCheckWithTypeParameter(argument, toType)
toType.isInterface() -> generateIsInterface(argument as IrExpression, toType)
else -> generateIsSubClass(argument as IrExpression, toType)
}
}
private fun narrowType(fromType: IrType, toType: IrType, value: IrExpression): IrExpression {
if (fromType == toType) return value
if (toType == builtIns.nothingNType) {
return builder.irComposite(resultType = builtIns.nothingNType) {
+value
+builder.irNull()
}
}
// Handled by autoboxing transformer
if (toType.isInlined() && !fromType.isInlined()) {
return builder.irCall(
symbols.unboxIntrinsic,
toType,
typeArguments = listOf(fromType, toType)
).also {
it.putValueArgument(0, value)
}
}
if (!toType.isInlined() && fromType.isInlined()) {
return builder.irCall(
symbols.boxIntrinsic,
toType,
typeArguments = listOf(fromType, toType)
).also {
it.putValueArgument(0, value)
}
}
if (fromType.erasedType.isSubtypeOf(toType.erasedType, context.irBuiltIns)) {
return value
}
if (toType.isNothing()) {
return value
}
// Ref casts traps on null (https://github.com/WebAssembly/gc/issues/152)
// Handling null manually
if (toType.isNullable() && fromType.isNullable()) {
return builder.irComposite {
val value = cacheValue(value)
+builder.irIfNull(
type = toType,
subject = value() as IrExpression,
thenPart = builder.irNull(toType),
elsePart = builder.irCall(symbols.wasmRefCast, type = toType).apply {
putTypeArgument(0, fromType)
putTypeArgument(1, toType)
putValueArgument(0, value() as IrExpression)
}
)
}
}
return builder.irCall(symbols.wasmRefCast, type = toType).apply {
putTypeArgument(0, fromType)
putTypeArgument(1, toType)
putValueArgument(0, value)
}
}
private fun lowerCast(
expression: IrTypeOperatorCall,
isSafe: Boolean
): IrExpression {
val toType = expression.typeOperand
val fromType = expression.argument.type
if (fromType.erasedType.isSubtypeOf(expression.type.erasedType, context.irBuiltIns)) {
return narrowType(fromType, expression.type, expression.argument)
}
val failResult = if (isSafe) {
builder.irNull()
} else {
builder.irCall(context.ir.symbols.throwTypeCastException)
}
return builder.irComposite(resultType = expression.type) {
val argument = cacheValue(expression.argument)
val narrowArg = narrowType(fromType, expression.type, argument() as IrExpression)
val check = generateTypeCheck(argument, toType)
if (check is IrConst<*>) {
val value = check.value as Boolean
if (value) {
+narrowArg
} else {
+failResult
}
} else {
+builder.irIfThenElse(
type = expression.type,
condition = check,
thenPart = narrowArg,
elsePart = failResult
)
}
}
}
private fun lowerImplicitCast(expression: IrTypeOperatorCall): IrExpression =
narrowType(
fromType = expression.argument.type,
toType = expression.typeOperand,
value = expression.argument
)
private fun generateTypeCheckWithTypeParameter(argument: IrExpressionWithCopy, toType: IrType): IrExpression {
val typeParameter = toType.classifierOrNull?.owner as? IrTypeParameter
?: error("expected type parameter, but got $toType")
return typeParameter.superTypes.fold(builder.irTrue() as IrExpression) { r, t ->
val check = generateTypeCheckNonNull(argument.copy() as IrExpressionWithCopy, t.makeNotNull())
builder.irCall(symbols.booleanAnd).apply {
putValueArgument(0, r)
putValueArgument(1, check)
}
}
}
private fun generateIsInterface(argument: IrExpression, toType: IrType): IrExpression {
val interfaceId = builder.irCall(symbols.wasmInterfaceId).apply {
putTypeArgument(0, toType)
}
return builder.irCall(symbols.isInterface).apply {
putValueArgument(0, argument)
putValueArgument(1, interfaceId)
}
}
private fun generateIsSubClass(argument: IrExpression, toType: IrType): IrExpression {
val classId = builder.irCall(symbols.wasmClassId).apply {
putTypeArgument(0, toType)
}
return builder.irCall(symbols.isSubClass).apply {
putValueArgument(0, argument)
putValueArgument(1, classId)
}
}
}
@@ -0,0 +1,100 @@
/*
* Copyright 2010-2020 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.wasm.lower
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.common.lower.irComposite
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.builders.irGet
import org.jetbrains.kotlin.ir.builders.irInt
import org.jetbrains.kotlin.ir.builders.irTemporary
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
import org.jetbrains.kotlin.ir.expressions.IrVararg
import org.jetbrains.kotlin.ir.expressions.IrVarargElement
import org.jetbrains.kotlin.ir.types.classOrNull
import org.jetbrains.kotlin.ir.util.primaryConstructor
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.name.Name
internal class WasmVarargExpressionLowering(
private val context: WasmBackendContext
) : FileLoweringPass, IrElementTransformerVoidWithContext() {
val symbols = context.wasmSymbols
override fun lower(irFile: IrFile) {
irFile.transformChildrenVoid(this)
}
override fun visitVararg(expression: IrVararg): IrExpression {
val irVararg = super.visitVararg(expression) as IrVararg
val builder = context.createIrBuilder(currentScope!!.scope.scopeOwnerSymbol)
val arrayClass = irVararg.type.classOrNull!!.owner
val primaryConstructor = arrayClass.primaryConstructor!!
val setMethod = arrayClass.declarations.filterIsInstance<IrSimpleFunction>().find {
it.name == Name.identifier("set")
}!!
return builder.irComposite(irVararg) {
val arrayTempVariable = irTemporary(
value = irCall(primaryConstructor).apply {
putValueArgument(0, irInt(irVararg.elements.size))
if (primaryConstructor.typeParameters.isNotEmpty()) {
check(primaryConstructor.typeParameters.size == 1)
putTypeArgument(0, irVararg.varargElementType)
}
},
nameHint = "array_tmp"
)
for ((index: Int, element: IrVarargElement) in irVararg.elements.withIndex()) {
check(element is IrExpression) {
"TODO: Support $element as vararg elements"
}
+irCall(setMethod).apply {
dispatchReceiver = irGet(arrayTempVariable)
putValueArgument(0, irInt(index))
putValueArgument(1, element)
}
}
+irGet(arrayTempVariable)
}
}
override fun visitFunctionAccess(expression: IrFunctionAccessExpression) =
transformFunctionAccessExpression(expression)
private fun transformFunctionAccessExpression(expression: IrFunctionAccessExpression): IrExpression {
expression.transformChildrenVoid()
val builder by lazy { context.createIrBuilder(currentScope!!.scope.scopeOwnerSymbol) }
// Replace empty vararg arguments with empty array construction
for (argumentIdx in 0 until expression.valueArgumentsCount) {
val argument = expression.getValueArgument(argumentIdx)
val parameter = expression.symbol.owner.valueParameters[argumentIdx]
val varargElementType = parameter.varargElementType
if (argument == null && varargElementType != null) {
val arrayClass = parameter.type.classOrNull!!.owner
val primaryConstructor = arrayClass.primaryConstructor!!
val emptyArrayCall = with(builder) {
irCall(primaryConstructor).apply {
putValueArgument(0, irInt(0))
if (primaryConstructor.typeParameters.isNotEmpty()) {
check(primaryConstructor.typeParameters.size == 1)
putTypeArgument(0, parameter.varargElementType)
}
}
}
expression.putValueArgument(argumentIdx, emptyArrayCall)
}
}
return expression
}
}
@@ -11,19 +11,27 @@ import org.jetbrains.kotlin.ir.expressions.IrConst
import org.jetbrains.kotlin.ir.util.getAnnotation
import org.jetbrains.kotlin.ir.util.hasAnnotation
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.wasm.ir.WasmImportPair
fun IrAnnotationContainer.hasExcludedFromCodegenAnnotation(): Boolean =
hasAnnotation(FqName("kotlin.wasm.internal.ExcludedFromCodegen"))
fun IrAnnotationContainer.getWasmInstructionAnnotation(): String? =
getAnnotation(FqName("kotlin.wasm.internal.WasmInstruction"))?.getSingleConstStringArgument()
fun IrAnnotationContainer.getWasmOpAnnotation(): String? =
getAnnotation(FqName("kotlin.wasm.internal.WasmOp"))?.getSingleConstStringArgument()
fun IrAnnotationContainer.hasWasmReinterpretAnnotation(): Boolean =
hasAnnotation(FqName("kotlin.wasm.internal.WasmReinterpret"))
fun IrAnnotationContainer.hasWasmForeignAnnotation(): Boolean =
hasAnnotation(FqName("kotlin.wasm.internal.WasmForeign"))
fun IrAnnotationContainer.hasWasmPrimitiveAnnotation(): Boolean =
hasAnnotation(FqName("kotlin.wasm.internal.WasmPrimitive"))
class WasmImportPair(val module: String, val name: String)
@Suppress("UNCHECKED_CAST")
fun IrAnnotationContainer.getWasmImportAnnotation(): WasmImportPair? =
getAnnotation(FqName("kotlin.wasm.internal.WasmImport"))?.let {
WasmImportPair(
(it.getValueArgument(0) as IrConst<String>).value,
(it.getValueArgument(1) as IrConst<String>).value
(it.getValueArgument(0) as IrConst<*>).value as String,
(it.getValueArgument(1) as IrConst<*>).value as String
)
}
@@ -0,0 +1,43 @@
/*
* Copyright 2010-2020 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.wasm.utils
import org.jetbrains.kotlin.backend.wasm.WasmSymbols
import org.jetbrains.kotlin.ir.backend.js.InlineClassesUtils
import org.jetbrains.kotlin.ir.backend.js.utils.erase
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.types.IrSimpleType
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.isNullable
class WasmInlineClassesUtils(private val wasmSymbols: WasmSymbols) : InlineClassesUtils {
override fun isTypeInlined(type: IrType): Boolean {
return getInlinedClass(type) != null
}
override fun getInlinedClass(type: IrType): IrClass? {
if (type is IrSimpleType) {
// TODO: Make inlining less strict
if (type.isNullable()) return null
val erased = erase(type) ?: return null
if (isClassInlineLike(erased)) {
return erased
}
}
return null
}
override fun isClassInlineLike(klass: IrClass): Boolean {
return klass.isInline || klass.hasWasmPrimitiveAnnotation()
}
override val boxIntrinsic: IrSimpleFunctionSymbol
get() = wasmSymbols.boxIntrinsic
override val unboxIntrinsic: IrSimpleFunctionSymbol
get() = wasmSymbols.unboxIntrinsic
}