[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:
+100
-98
@@ -29,8 +29,6 @@ import org.jetbrains.kotlin.ir.visitors.acceptVoid
|
|||||||
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
|
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
|
||||||
import org.jetbrains.kotlin.wasm.ir.*
|
import org.jetbrains.kotlin.wasm.ir.*
|
||||||
import org.jetbrains.kotlin.wasm.ir.source.location.SourceLocation
|
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(
|
class BodyGenerator(
|
||||||
val context: WasmModuleCodegenContext,
|
val context: WasmModuleCodegenContext,
|
||||||
@@ -108,7 +106,7 @@ class BodyGenerator(
|
|||||||
|
|
||||||
val constantArrayId = context.referenceConstantArray(resource)
|
val constantArrayId = context.referenceConstantArray(resource)
|
||||||
|
|
||||||
withLocation(irVararg.getSourceLocation()) {
|
irVararg.getSourceLocation().let { location ->
|
||||||
body.buildConstI32(0, location)
|
body.buildConstI32(0, location)
|
||||||
body.buildConstI32(irVararg.elements.size, location)
|
body.buildConstI32(irVararg.elements.size, location)
|
||||||
body.buildInstr(WasmOp.ARRAY_NEW_DATA, location, wasmArrayType, WasmImmediate.DataIdx(constantArrayId))
|
body.buildInstr(WasmOp.ARRAY_NEW_DATA, location, wasmArrayType, WasmImmediate.DataIdx(constantArrayId))
|
||||||
@@ -330,7 +328,7 @@ class BodyGenerator(
|
|||||||
if (parentClass.isAbstractOrSealed) return
|
if (parentClass.isAbstractOrSealed) return
|
||||||
val thisParameter = functionContext.referenceLocal(parentClass.thisReceiver!!.symbol)
|
val thisParameter = functionContext.referenceLocal(parentClass.thisReceiver!!.symbol)
|
||||||
body.commentGroupStart { "Object creation prefix" }
|
body.commentGroupStart { "Object creation prefix" }
|
||||||
withNoLocation("Constructor preamble") {
|
SourceLocation.NoLocation("Constructor preamble").let { location ->
|
||||||
body.buildGetLocal(thisParameter, location)
|
body.buildGetLocal(thisParameter, location)
|
||||||
body.buildInstr(WasmOp.REF_IS_NULL, location)
|
body.buildInstr(WasmOp.REF_IS_NULL, location)
|
||||||
body.buildIf("this_init")
|
body.buildIf("this_init")
|
||||||
@@ -518,109 +516,113 @@ class BodyGenerator(
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
withLocation(call.getSourceLocation()) {
|
val location = 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)
|
|
||||||
}
|
|
||||||
|
|
||||||
wasmSymbols.wasmInterfaceId -> {
|
when (function.symbol) {
|
||||||
val irInterface = call.getTypeArgument(0)!!.getClass()
|
wasmSymbols.wasmClassId -> {
|
||||||
?: error("No interface given for wasmInterfaceId intrinsic")
|
val klass = call.getTypeArgument(0)!!.getClass()
|
||||||
assert(irInterface.isInterface)
|
?: error("No class given for wasmClassId intrinsic")
|
||||||
body.buildConstI32Symbol(context.referenceInterfaceId(irInterface.symbol), location)
|
assert(!klass.isInterface)
|
||||||
}
|
body.buildConstI32Symbol(context.referenceClassId(klass.symbol), location)
|
||||||
|
}
|
||||||
|
|
||||||
wasmSymbols.wasmIsInterface -> {
|
wasmSymbols.wasmInterfaceId -> {
|
||||||
val irInterface = call.getTypeArgument(0)!!.getClass()
|
val irInterface = call.getTypeArgument(0)!!.getClass()
|
||||||
?: error("No interface given for wasmInterfaceId intrinsic")
|
?: error("No interface given for wasmInterfaceId intrinsic")
|
||||||
assert(irInterface.isInterface)
|
assert(irInterface.isInterface)
|
||||||
if (irInterface.symbol in hierarchyDisjointUnions) {
|
body.buildConstI32Symbol(context.referenceInterfaceId(irInterface.symbol), location)
|
||||||
val classITable = context.referenceClassITableGcType(irInterface.symbol)
|
}
|
||||||
val parameterLocal = functionContext.referenceLocal(SyntheticLocalType.IS_INTERFACE_PARAMETER)
|
|
||||||
body.buildSetLocal(parameterLocal, location)
|
wasmSymbols.wasmIsInterface -> {
|
||||||
body.buildBlock("isInterface", WasmI32) { outerLabel ->
|
val irInterface = call.getTypeArgument(0)!!.getClass()
|
||||||
body.buildBlock("isInterface", WasmRefNullType(WasmHeapType.Simple.Data)) { innerLabel ->
|
?: error("No interface given for wasmInterfaceId intrinsic")
|
||||||
body.buildGetLocal(parameterLocal, location)
|
assert(irInterface.isInterface)
|
||||||
body.buildStructGet(context.referenceGcType(irBuiltIns.anyClass), WasmSymbol(1), location)
|
if (irInterface.symbol in hierarchyDisjointUnions) {
|
||||||
body.buildBrInstr(WasmOp.BR_ON_CAST_FAIL_DEPRECATED, innerLabel, classITable, location)
|
val classITable = context.referenceClassITableGcType(irInterface.symbol)
|
||||||
body.buildStructGet(classITable, context.referenceClassITableInterfaceSlot(irInterface.symbol), location)
|
val parameterLocal = functionContext.referenceLocal(SyntheticLocalType.IS_INTERFACE_PARAMETER)
|
||||||
body.buildInstr(WasmOp.REF_IS_NULL, location)
|
body.buildSetLocal(parameterLocal, location)
|
||||||
body.buildInstr(WasmOp.I32_EQZ, location)
|
body.buildBlock("isInterface", WasmI32) { outerLabel ->
|
||||||
body.buildBr(outerLabel, location)
|
body.buildBlock("isInterface", WasmRefNullType(WasmHeapType.Simple.Data)) { innerLabel ->
|
||||||
}
|
body.buildGetLocal(parameterLocal, location)
|
||||||
body.buildDrop(location)
|
body.buildStructGet(context.referenceGcType(irBuiltIns.anyClass), WasmSymbol(1), location)
|
||||||
body.buildConstI32(0, 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.buildDrop(location)
|
||||||
body.buildConstI32(0, location)
|
body.buildConstI32(0, location)
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
body.buildDrop(location)
|
||||||
wasmSymbols.refCastNull -> {
|
body.buildConstI32(0, location)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-4
@@ -30,8 +30,6 @@ import org.jetbrains.kotlin.ir.visitors.acceptVoid
|
|||||||
import org.jetbrains.kotlin.name.parentOrNull
|
import org.jetbrains.kotlin.name.parentOrNull
|
||||||
import org.jetbrains.kotlin.wasm.ir.*
|
import org.jetbrains.kotlin.wasm.ir.*
|
||||||
import org.jetbrains.kotlin.wasm.ir.source.location.SourceLocation
|
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(
|
class DeclarationGenerator(
|
||||||
val context: WasmModuleCodegenContext,
|
val context: WasmModuleCodegenContext,
|
||||||
@@ -477,7 +475,7 @@ class DeclarationGenerator(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun generateDefaultInitializerForType(type: WasmType, g: WasmExpressionBuilder) =
|
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) {
|
when (type) {
|
||||||
WasmI32 -> g.buildConstI32(0, location)
|
WasmI32 -> g.buildConstI32(0, location)
|
||||||
WasmI64 -> g.buildConstI64(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?) =
|
fun generateConstExpression(expression: IrConst<*>, body: WasmExpressionBuilder, context: WasmModuleCodegenContext, fileEntry: IrFileEntry?) =
|
||||||
withLocation(expression.getSourceLocation(fileEntry)) {
|
expression.getSourceLocation(fileEntry).let { location ->
|
||||||
when (val kind = expression.kind) {
|
when (val kind = expression.kind) {
|
||||||
is IrConstKind.Null -> {
|
is IrConstKind.Null -> {
|
||||||
val bottomType = if (expression.type.getClass()?.isExternal == true) WasmRefNullExternrefType else WasmRefNullNoneType
|
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)
|
|
||||||
Reference in New Issue
Block a user