[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`.
This commit is contained in:
Zalim Bashorov
2022-12-15 20:51:38 +01:00
parent e62add937b
commit aa8a0fd7d7
3 changed files with 102 additions and 115 deletions
@@ -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
}
@@ -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
@@ -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 <R> withLocation(location: SourceLocation, body: LocationHolder.() -> R): R = LocationHolder(location).body()
inline fun <R> withNoLocation(description: String, body: LocationHolder.() -> R): R = withLocation(SourceLocation.NoLocation(description), body)