From aa8a0fd7d75842804a7f0c8db3e822fc98d12089 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Thu, 15 Dec 2022 20:51:38 +0100 Subject: [PATCH] [Wasm] Remove LocationHolder and helper functions It turned out that dedicated scoping function for locations are not so useful. Now, if you want to scope a location use functions from stdlib, like `let`. --- .../backend/wasm/ir2wasm/BodyGenerator.kt | 198 +++++++++--------- .../wasm/ir2wasm/DeclarationGenerator.kt | 6 +- .../wasm/ir/source/location/LocationHolder.kt | 13 -- 3 files changed, 102 insertions(+), 115 deletions(-) delete mode 100644 wasm/wasm.ir/src/org/jetbrains/kotlin/wasm/ir/source/location/LocationHolder.kt diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/BodyGenerator.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/BodyGenerator.kt index c87fc81a297..84bb389a865 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/BodyGenerator.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/BodyGenerator.kt @@ -29,8 +29,6 @@ import org.jetbrains.kotlin.ir.visitors.acceptVoid import org.jetbrains.kotlin.js.config.JSConfigurationKeys import org.jetbrains.kotlin.wasm.ir.* import org.jetbrains.kotlin.wasm.ir.source.location.SourceLocation -import org.jetbrains.kotlin.wasm.ir.source.location.withLocation -import org.jetbrains.kotlin.wasm.ir.source.location.withNoLocation class BodyGenerator( val context: WasmModuleCodegenContext, @@ -108,7 +106,7 @@ class BodyGenerator( val constantArrayId = context.referenceConstantArray(resource) - withLocation(irVararg.getSourceLocation()) { + irVararg.getSourceLocation().let { location -> body.buildConstI32(0, location) body.buildConstI32(irVararg.elements.size, location) body.buildInstr(WasmOp.ARRAY_NEW_DATA, location, wasmArrayType, WasmImmediate.DataIdx(constantArrayId)) @@ -330,7 +328,7 @@ class BodyGenerator( if (parentClass.isAbstractOrSealed) return val thisParameter = functionContext.referenceLocal(parentClass.thisReceiver!!.symbol) body.commentGroupStart { "Object creation prefix" } - withNoLocation("Constructor preamble") { + SourceLocation.NoLocation("Constructor preamble").let { location -> body.buildGetLocal(thisParameter, location) body.buildInstr(WasmOp.REF_IS_NULL, location) body.buildIf("this_init") @@ -518,109 +516,113 @@ class BodyGenerator( return true } - withLocation(call.getSourceLocation()) { - 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), location) - } + val location = call.getSourceLocation() - wasmSymbols.wasmInterfaceId -> { - val irInterface = call.getTypeArgument(0)!!.getClass() - ?: error("No interface given for wasmInterfaceId intrinsic") - assert(irInterface.isInterface) - body.buildConstI32Symbol(context.referenceInterfaceId(irInterface.symbol), location) - } + 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), location) + } - wasmSymbols.wasmIsInterface -> { - val irInterface = call.getTypeArgument(0)!!.getClass() - ?: error("No interface given for wasmInterfaceId intrinsic") - assert(irInterface.isInterface) - if (irInterface.symbol in hierarchyDisjointUnions) { - val classITable = context.referenceClassITableGcType(irInterface.symbol) - val parameterLocal = functionContext.referenceLocal(SyntheticLocalType.IS_INTERFACE_PARAMETER) - body.buildSetLocal(parameterLocal, location) - body.buildBlock("isInterface", WasmI32) { outerLabel -> - body.buildBlock("isInterface", WasmRefNullType(WasmHeapType.Simple.Data)) { innerLabel -> - body.buildGetLocal(parameterLocal, location) - body.buildStructGet(context.referenceGcType(irBuiltIns.anyClass), WasmSymbol(1), location) - body.buildBrInstr(WasmOp.BR_ON_CAST_FAIL_DEPRECATED, innerLabel, classITable, location) - body.buildStructGet(classITable, context.referenceClassITableInterfaceSlot(irInterface.symbol), location) - body.buildInstr(WasmOp.REF_IS_NULL, location) - body.buildInstr(WasmOp.I32_EQZ, location) - body.buildBr(outerLabel, location) - } - body.buildDrop(location) - body.buildConstI32(0, location) + wasmSymbols.wasmInterfaceId -> { + val irInterface = call.getTypeArgument(0)!!.getClass() + ?: error("No interface given for wasmInterfaceId intrinsic") + assert(irInterface.isInterface) + body.buildConstI32Symbol(context.referenceInterfaceId(irInterface.symbol), location) + } + + wasmSymbols.wasmIsInterface -> { + val irInterface = call.getTypeArgument(0)!!.getClass() + ?: error("No interface given for wasmInterfaceId intrinsic") + assert(irInterface.isInterface) + if (irInterface.symbol in hierarchyDisjointUnions) { + val classITable = context.referenceClassITableGcType(irInterface.symbol) + val parameterLocal = functionContext.referenceLocal(SyntheticLocalType.IS_INTERFACE_PARAMETER) + body.buildSetLocal(parameterLocal, location) + body.buildBlock("isInterface", WasmI32) { outerLabel -> + body.buildBlock("isInterface", WasmRefNullType(WasmHeapType.Simple.Data)) { innerLabel -> + body.buildGetLocal(parameterLocal, location) + body.buildStructGet(context.referenceGcType(irBuiltIns.anyClass), WasmSymbol(1), location) + body.buildBrInstr(WasmOp.BR_ON_CAST_FAIL_DEPRECATED, innerLabel, classITable, location) + body.buildStructGet(classITable, context.referenceClassITableInterfaceSlot(irInterface.symbol), location) + body.buildInstr(WasmOp.REF_IS_NULL, location) + body.buildInstr(WasmOp.I32_EQZ, location) + body.buildBr(outerLabel, location) } - } else { body.buildDrop(location) body.buildConstI32(0, location) } - } - - wasmSymbols.refCastNull -> { - generateRefNullCast( - fromType = call.getValueArgument(0)!!.type, - toType = call.getTypeArgument(0)!!, - location = location - ) - } - - wasmSymbols.refTest -> { - generateRefTest( - fromType = call.getValueArgument(0)!!.type, - toType = call.getTypeArgument(0)!!, - location - ) - } - - wasmSymbols.unboxIntrinsic -> { - val fromType = call.getTypeArgument(0)!! - - if (fromType.isNothing()) { - body.buildUnreachableAfterNothingType() - // TODO: Investigate why? - return true - } - - val toType = call.getTypeArgument(1)!! - val klass: IrClass = backendContext.inlineClassesUtils.getInlinedClass(toType)!! - val field = getInlineClassBackingField(klass) - - generateRefNullCast(fromType, toType, location) - generateInstanceFieldAccess(field, location) - } - - wasmSymbols.unsafeGetScratchRawMemory -> { - body.buildConstI32Symbol(context.scratchMemAddr, location) - } - - wasmSymbols.wasmArrayCopy -> { - val immediate = WasmImmediate.GcType( - context.referenceGcType(call.getTypeArgument(0)!!.getRuntimeClass(irBuiltIns).symbol) - ) - body.buildInstr(WasmOp.ARRAY_COPY, location, immediate, immediate) - } - - wasmSymbols.stringGetPoolSize -> { - body.buildConstI32Symbol(context.stringPoolSize, location) - } - - wasmSymbols.wasmArrayNewData0 -> { - val arrayGcType = WasmImmediate.GcType( - context.referenceGcType(call.getTypeArgument(0)!!.getRuntimeClass(irBuiltIns).symbol) - ) - body.buildInstr(WasmOp.ARRAY_NEW_DATA, location, arrayGcType, WasmImmediate.DataIdx(0)) - } - - else -> { - return false + } else { + body.buildDrop(location) + body.buildConstI32(0, location) } } + + wasmSymbols.refCastNull -> { + generateRefNullCast( + fromType = call.getValueArgument(0)!!.type, + toType = call.getTypeArgument(0)!!, + location = location + ) + } + + wasmSymbols.refTest -> { + generateRefTest( + fromType = call.getValueArgument(0)!!.type, + toType = call.getTypeArgument(0)!!, + location + ) + } + + wasmSymbols.unboxIntrinsic -> { + val fromType = call.getTypeArgument(0)!! + + if (fromType.isNothing()) { + body.buildUnreachableAfterNothingType() + // TODO: Investigate why? + return true + } + + val toType = call.getTypeArgument(1)!! + val klass: IrClass = backendContext.inlineClassesUtils.getInlinedClass(toType)!! + val field = getInlineClassBackingField(klass) + + generateRefNullCast(fromType, toType, location) + generateInstanceFieldAccess(field, location) + } + + wasmSymbols.unsafeGetScratchRawMemory -> { + + body.buildConstI32Symbol(context.scratchMemAddr, location) + } + + + + wasmSymbols.wasmArrayCopy -> { + val immediate = WasmImmediate.GcType( + context.referenceGcType(call.getTypeArgument(0)!!.getRuntimeClass(irBuiltIns).symbol) + ) + body.buildInstr(WasmOp.ARRAY_COPY, location, immediate, immediate) + } + + wasmSymbols.stringGetPoolSize -> { + body.buildConstI32Symbol(context.stringPoolSize, location) + } + + wasmSymbols.wasmArrayNewData0 -> { + val arrayGcType = WasmImmediate.GcType( + context.referenceGcType(call.getTypeArgument(0)!!.getRuntimeClass(irBuiltIns).symbol) + ) + body.buildInstr(WasmOp.ARRAY_NEW_DATA, location, arrayGcType, WasmImmediate.DataIdx(0)) + } + + else -> { + return false + } } + return true } diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt index 07b7c571205..454c3da9e67 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt @@ -30,8 +30,6 @@ import org.jetbrains.kotlin.ir.visitors.acceptVoid import org.jetbrains.kotlin.name.parentOrNull import org.jetbrains.kotlin.wasm.ir.* import org.jetbrains.kotlin.wasm.ir.source.location.SourceLocation -import org.jetbrains.kotlin.wasm.ir.source.location.withLocation -import org.jetbrains.kotlin.wasm.ir.source.location.withNoLocation class DeclarationGenerator( val context: WasmModuleCodegenContext, @@ -477,7 +475,7 @@ class DeclarationGenerator( } fun generateDefaultInitializerForType(type: WasmType, g: WasmExpressionBuilder) = - withNoLocation("Default initializer, usually don't require location") { + SourceLocation.NoLocation("Default initializer, usually don't require location").let { location -> when (type) { WasmI32 -> g.buildConstI32(0, location) WasmI64 -> g.buildConstI64(0, location) @@ -503,7 +501,7 @@ fun IrFunction.isExported(): Boolean = fun generateConstExpression(expression: IrConst<*>, body: WasmExpressionBuilder, context: WasmModuleCodegenContext, fileEntry: IrFileEntry?) = - withLocation(expression.getSourceLocation(fileEntry)) { + expression.getSourceLocation(fileEntry).let { location -> when (val kind = expression.kind) { is IrConstKind.Null -> { val bottomType = if (expression.type.getClass()?.isExternal == true) WasmRefNullExternrefType else WasmRefNullNoneType diff --git a/wasm/wasm.ir/src/org/jetbrains/kotlin/wasm/ir/source/location/LocationHolder.kt b/wasm/wasm.ir/src/org/jetbrains/kotlin/wasm/ir/source/location/LocationHolder.kt deleted file mode 100644 index 46d9b15da9a..00000000000 --- a/wasm/wasm.ir/src/org/jetbrains/kotlin/wasm/ir/source/location/LocationHolder.kt +++ /dev/null @@ -1,13 +0,0 @@ -/* - * Copyright 2010-2022 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.wasm.ir.source.location - -@JvmInline -value class LocationHolder(val location: SourceLocation) - -inline fun withLocation(location: SourceLocation, body: LocationHolder.() -> R): R = LocationHolder(location).body() - -inline fun withNoLocation(description: String, body: LocationHolder.() -> R): R = withLocation(SourceLocation.NoLocation(description), body)