From 1244cd1259bb1f725c64ed2b8930541a2e7ad148 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Tue, 13 Dec 2022 20:16:35 +0100 Subject: [PATCH] [Wasm] Require location for `buildBr*` and fix all usages --- .../backend/wasm/ir2wasm/BodyGenerator.kt | 19 ++++++++++--------- .../wasm/ir2wasm/OptimisedWhenGenerator.kt | 4 ++-- .../kotlin/wasm/ir/WasmExpressionBuilder.kt | 16 ++++++++-------- 3 files changed, 20 insertions(+), 19 deletions(-) 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 44341ae5e2d..ea159975f29 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 @@ -540,11 +540,11 @@ class BodyGenerator( 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) + 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) body.buildInstr(WasmOp.I32_EQZ) - body.buildBr(outerLabel) + body.buildBr(outerLabel, location) } body.buildDrop(location) body.buildConstI32(0, location) @@ -661,12 +661,12 @@ class BodyGenerator( override fun visitBreak(jump: IrBreak) { assert(jump.type == irBuiltIns.nothingType) - body.buildBr(functionContext.referenceLoopLevel(jump.loop, LoopLabelType.BREAK)) + body.buildBr(functionContext.referenceLoopLevel(jump.loop, LoopLabelType.BREAK), jump.getSourceLocation()) } override fun visitContinue(jump: IrContinue) { assert(jump.type == irBuiltIns.nothingType) - body.buildBr(functionContext.referenceLoopLevel(jump.loop, LoopLabelType.CONTINUE)) + body.buildBr(functionContext.referenceLoopLevel(jump.loop, LoopLabelType.CONTINUE), jump.getSourceLocation()) } private fun visitFunctionReturn(expression: IrReturn) { @@ -775,7 +775,7 @@ class BodyGenerator( val nonLocalReturnSymbol = expression.returnTargetSymbol as? IrReturnableBlockSymbol if (nonLocalReturnSymbol != null) { generateWithExpectedType(expression.value, nonLocalReturnSymbol.owner.type) - body.buildBr(functionContext.referenceNonLocalReturnLevel(nonLocalReturnSymbol)) + body.buildBr(functionContext.referenceNonLocalReturnLevel(nonLocalReturnSymbol), expression.getSourceLocation()) } else { visitFunctionReturn(expression) } @@ -838,7 +838,7 @@ class BodyGenerator( loop.body?.let { generateAsStatement(it) } } generateExpression(loop.condition) - body.buildBrIf(wasmLoop) + body.buildBrIf(wasmLoop, loop.condition.getSourceLocation()) } } @@ -860,12 +860,13 @@ class BodyGenerator( functionContext.defineLoopLevel(loop, LoopLabelType.CONTINUE, wasmLoop) generateExpression(loop.condition) - body.buildInstr(WasmOp.I32_EQZ) - body.buildBrIf(wasmBreakBlock) + val location = loop.condition.getSourceLocation() + body.buildInstr(WasmOp.I32_EQZ, location) + body.buildBrIf(wasmBreakBlock, location) loop.body?.let { generateAsStatement(it) } - body.buildBr(wasmLoop) + body.buildBr(wasmLoop, SourceLocation.NoLocation("Continue in the loop")) } } diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/OptimisedWhenGenerator.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/OptimisedWhenGenerator.kt index 4debd537eb6..8c5bc45cdaa 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/OptimisedWhenGenerator.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/OptimisedWhenGenerator.kt @@ -211,7 +211,7 @@ private fun BodyGenerator.createBinaryTable( body.buildBlock("when_block", resultType) { currentBlock -> val thenBody = { result: IrExpression -> generateWithExpectedType(result, expectedType) - body.buildBr(currentBlock) + body.buildBr(currentBlock, SourceLocation.NoLocation("Break from a when")) } createBinaryTable( selectorLocal = selectorLocal, @@ -337,7 +337,7 @@ private fun BodyGenerator.genTableIntSwitch( } generateWithExpectedType(expression.expression, expectedType) - body.buildBr(baseBlockIndex + 1) + body.buildBr(baseBlockIndex + 1, location) body.buildEnd() } diff --git a/wasm/wasm.ir/src/org/jetbrains/kotlin/wasm/ir/WasmExpressionBuilder.kt b/wasm/wasm.ir/src/org/jetbrains/kotlin/wasm/ir/WasmExpressionBuilder.kt index 133050766f2..eed15184d47 100644 --- a/wasm/wasm.ir/src/org/jetbrains/kotlin/wasm/ir/WasmExpressionBuilder.kt +++ b/wasm/wasm.ir/src/org/jetbrains/kotlin/wasm/ir/WasmExpressionBuilder.kt @@ -78,20 +78,20 @@ abstract class WasmExpressionBuilder { } - fun buildBrInstr(brOp: WasmOp, absoluteBlockLevel: Int) { + fun buildBrInstr(brOp: WasmOp, absoluteBlockLevel: Int, location: SourceLocation) { val relativeLevel = numberOfNestedBlocks - absoluteBlockLevel assert(relativeLevel >= 0) { "Negative relative block index" } - buildInstr(brOp, WasmImmediate.LabelIdx(relativeLevel)) + buildInstr(brOp, location, WasmImmediate.LabelIdx(relativeLevel)) } - fun buildBrInstr(brOp: WasmOp, absoluteBlockLevel: Int, symbol: WasmSymbolReadOnly) { + fun buildBrInstr(brOp: WasmOp, absoluteBlockLevel: Int, symbol: WasmSymbolReadOnly, location: SourceLocation) { val relativeLevel = numberOfNestedBlocks - absoluteBlockLevel assert(relativeLevel >= 0) { "Negative relative block index" } - buildInstr(brOp, WasmImmediate.LabelIdx(relativeLevel), WasmImmediate.TypeIdx(symbol)) + buildInstr(brOp, location, WasmImmediate.LabelIdx(relativeLevel), WasmImmediate.TypeIdx(symbol)) } - fun buildBr(absoluteBlockLevel: Int) { - buildBrInstr(WasmOp.BR, absoluteBlockLevel) + fun buildBr(absoluteBlockLevel: Int, location: SourceLocation) { + buildBrInstr(WasmOp.BR, absoluteBlockLevel, location) } fun buildThrow(tagIdx: Int) { @@ -108,8 +108,8 @@ abstract class WasmExpressionBuilder { buildInstr(WasmOp.CATCH, WasmImmediate.TagIdx(tagIdx)) } - fun buildBrIf(absoluteBlockLevel: Int) { - buildBrInstr(WasmOp.BR_IF, absoluteBlockLevel) + fun buildBrIf(absoluteBlockLevel: Int, location: SourceLocation) { + buildBrInstr(WasmOp.BR_IF, absoluteBlockLevel, location) } fun buildCall(symbol: WasmSymbol, location: SourceLocation) {