WASM: Generate throw instruction instead of wasmThrow call
This commit is contained in:
committed by
TeamCityServer
parent
e5e44a0152
commit
6ca965af6f
@@ -340,12 +340,6 @@ private val wasmVarargExpressionLoweringPhase = makeWasmModulePhase(
|
||||
description = "Lower varargs"
|
||||
)
|
||||
|
||||
private val wasmThrowDebugLoweringPhase = makeWasmModulePhase(
|
||||
::WasmThrowDebugLowering,
|
||||
name = "WasmThrowDebugLowering",
|
||||
description = "Instrument throws with debug print information"
|
||||
)
|
||||
|
||||
private val fieldInitializersLoweringPhase = makeWasmModulePhase(
|
||||
::FieldInitializersLowering,
|
||||
name = "FieldInitializersLowering",
|
||||
@@ -503,7 +497,6 @@ val wasmPhases = NamedCompilerPhase(
|
||||
builtInsLoweringPhase then
|
||||
|
||||
virtualDispatchReceiverExtractionPhase then
|
||||
wasmThrowDebugLoweringPhase then
|
||||
staticMembersLoweringPhase then
|
||||
wasmNullSpecializationLowering then
|
||||
validateIrAfterLowering
|
||||
|
||||
@@ -138,8 +138,6 @@ class WasmSymbols(
|
||||
val nullableFloatIeee754Equals = getInternalFunction("nullableFloatIeee754Equals")
|
||||
val nullableDoubleIeee754Equals = getInternalFunction("nullableDoubleIeee754Equals")
|
||||
|
||||
val wasmThrow = getInternalFunction("wasmThrow")
|
||||
|
||||
val exportString = getInternalFunction("exportString")
|
||||
val unsafeGetScratchRawMemory = getInternalFunction("unsafeGetScratchRawMemory")
|
||||
|
||||
|
||||
+6
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.ir.types.*
|
||||
import org.jetbrains.kotlin.ir.util.*
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptVoid
|
||||
import org.jetbrains.kotlin.psi2ir.intermediate.generateExpressionValue
|
||||
import org.jetbrains.kotlin.wasm.ir.*
|
||||
|
||||
class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorVoid {
|
||||
@@ -61,6 +62,11 @@ class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorV
|
||||
error("Unexpected element of type ${element::class}")
|
||||
}
|
||||
|
||||
override fun visitThrow(expression: IrThrow) {
|
||||
generateExpression(expression.value)
|
||||
body.buildThrow(context.tagIdx)
|
||||
}
|
||||
|
||||
override fun visitTypeOperator(expression: IrTypeOperatorCall) {
|
||||
when (expression.operator) {
|
||||
IrTypeOperator.REINTERPRET_CAST -> generateExpression(expression.argument)
|
||||
|
||||
+5
-2
@@ -33,6 +33,8 @@ class WasmCompiledModuleFragment {
|
||||
ReferencableElements<WasmSignature, Int>()
|
||||
val stringLiteralId =
|
||||
ReferencableElements<String, Int>()
|
||||
val tagFuncType = WasmFunctionType("ex_handling_tag", listOf(WasmAnyRef), emptyList())
|
||||
val tag = WasmTag(tagFuncType)
|
||||
|
||||
val runtimeTypes =
|
||||
ReferencableAndDefinable<IrClassSymbol, WasmGlobal>()
|
||||
@@ -250,7 +252,7 @@ class WasmCompiledModuleFragment {
|
||||
val sortedRttGlobals = runtimeTypes.elements.sortedBy { (it.type as WasmRtt).depth }
|
||||
|
||||
val module = WasmModule(
|
||||
functionTypes = functionTypes.elements,
|
||||
functionTypes = functionTypes.elements + tagFuncType,
|
||||
gcTypes = gcTypes.elements,
|
||||
importsInOrder = importedFunctions,
|
||||
importedFunctions = importedFunctions,
|
||||
@@ -261,7 +263,8 @@ class WasmCompiledModuleFragment {
|
||||
exports = exports,
|
||||
startFunction = startFunction!!,
|
||||
elements = listOf(elements) + interfaceTableElements,
|
||||
data = data
|
||||
data = data,
|
||||
tags = listOf(tag)
|
||||
)
|
||||
module.calculateIds()
|
||||
return module
|
||||
|
||||
+3
@@ -24,5 +24,8 @@ interface WasmFunctionCodegenContext : WasmBaseCodegenContext {
|
||||
fun defineLoopLevel(irLoop: IrLoop, labelType: LoopLabelType, level: Int)
|
||||
fun referenceLoopLevel(irLoop: IrLoop, labelType: LoopLabelType): Int
|
||||
|
||||
// So far always a single tag
|
||||
val tagIdx: Int
|
||||
|
||||
val bodyGen: WasmExpressionBuilder
|
||||
}
|
||||
+3
@@ -23,6 +23,9 @@ class WasmFunctionCodegenContextImpl(
|
||||
override val bodyGen: WasmExpressionBuilder =
|
||||
WasmIrExpressionBuilder(wasmFunction.instructions)
|
||||
|
||||
override val tagIdx: Int
|
||||
get() = 0
|
||||
|
||||
private val wasmLocals = LinkedHashMap<IrValueSymbol, WasmLocal>()
|
||||
private val loopLevels = LinkedHashMap<Pair<IrLoop, LoopLabelType>, Int>()
|
||||
|
||||
|
||||
-37
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
* 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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -86,6 +86,7 @@ abstract class BasicWasmBoxTest(
|
||||
.run(
|
||||
"--experimental-wasm-typed-funcref",
|
||||
"--experimental-wasm-gc",
|
||||
"--experimental-wasm-eh",
|
||||
outputJsFile
|
||||
)
|
||||
}
|
||||
|
||||
@@ -67,8 +67,3 @@ internal fun <T, R> boxIntrinsic(x: T): R =
|
||||
@ExcludedFromCodegen
|
||||
internal fun <T, R> unboxIntrinsic(x: T): R =
|
||||
implementedAsIntrinsic
|
||||
|
||||
internal fun wasmThrow(e: Throwable): Nothing {
|
||||
println("Kotlin/Wasm exception wasm thrown: ${e.message}")
|
||||
wasm_unreachable()
|
||||
}
|
||||
@@ -72,6 +72,14 @@ abstract class WasmExpressionBuilder {
|
||||
buildInstr(WasmOp.BR, WasmImmediate.LabelIdx(relativeLevel))
|
||||
}
|
||||
|
||||
fun buildThrow(tagIdx: Int) {
|
||||
buildInstr(WasmOp.THROW, WasmImmediate.TagIdx(tagIdx))
|
||||
}
|
||||
|
||||
fun buildCatch(tagIdx: Int) {
|
||||
buildInstr(WasmOp.CATCH, WasmImmediate.TagIdx(tagIdx))
|
||||
}
|
||||
|
||||
fun buildBrIf(absoluteBlockLevel: Int) {
|
||||
val relativeLevel = numberOfNestedBlocks - absoluteBlockLevel
|
||||
assert(relativeLevel >= 0) { "Negative relative block index" }
|
||||
|
||||
Reference in New Issue
Block a user