From 00d0c38fc318280f8dde1c45196c69ea6c07ab50 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Tue, 13 Dec 2022 17:50:34 +0100 Subject: [PATCH] [Wasm] Require location for `buildSetLocal` and fix all usages --- .../kotlin/backend/wasm/ir2wasm/BodyGenerator.kt | 12 ++++++------ .../backend/wasm/ir2wasm/OptimisedWhenGenerator.kt | 7 +++++-- .../kotlin/backend/wasm/ir2wasm/locationUtils.kt | 3 ++- .../kotlin/wasm/ir/WasmExpressionBuilder.kt | 4 ++-- 4 files changed, 15 insertions(+), 11 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 13acb6e272c..6c061851225 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 @@ -156,7 +156,7 @@ class BodyGenerator( // Exception object is on top of the stack, store it into the local aTry.catches.single().catchParameter.symbol.let { functionContext.defineLocal(it) - body.buildSetLocal(functionContext.referenceLocal(it)) + body.buildSetLocal(functionContext.referenceLocal(it), it.owner.getSourceLocation()) } generateExpression(aTry.catches.single().result) @@ -253,7 +253,7 @@ class BodyGenerator( override fun visitSetValue(expression: IrSetValue) { generateExpression(expression.value) - body.buildSetLocal(functionContext.referenceLocal(expression.symbol)) + body.buildSetLocal(functionContext.referenceLocal(expression.symbol), expression.getSourceLocation()) body.commentPreviousInstr { "type: ${expression.symbol.owner.type.render()}" } body.buildGetUnit() } @@ -330,7 +330,7 @@ class BodyGenerator( } } body.buildStructNew(context.referenceGcType(parentClass.symbol)) - body.buildSetLocal(thisParameter) + body.buildSetLocal(thisParameter, location) body.buildEnd() } body.commentGroupEnd() @@ -526,7 +526,7 @@ class BodyGenerator( if (irInterface.symbol in hierarchyDisjointUnions) { val classITable = context.referenceClassITableGcType(irInterface.symbol) val parameterLocal = functionContext.referenceLocal(SyntheticLocalType.IS_INTERFACE_PARAMETER) - body.buildSetLocal(parameterLocal) + body.buildSetLocal(parameterLocal, location) body.buildBlock("isInterface", WasmI32) { outerLabel -> body.buildBlock("isInterface", WasmRefNullType(WasmHeapType.Simple.Data)) { innerLabel -> body.buildGetLocal(parameterLocal, location) @@ -871,7 +871,7 @@ class BodyGenerator( val init = declaration.initializer!! generateExpression(init) val varName = functionContext.referenceLocal(declaration.symbol) - body.buildSetLocal(varName) + body.buildSetLocal(varName, declaration.getSourceLocation()) } // Return true if function is recognized as intrinsic. @@ -921,5 +921,5 @@ class BodyGenerator( return false } - private fun IrExpression.getSourceLocation() = getSourceLocation(functionContext.irFunction.fileOrNull?.fileEntry) + private fun IrElement.getSourceLocation() = getSourceLocation(functionContext.irFunction.fileOrNull?.fileEntry) } 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 c09715d9b43..53189d4adeb 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 @@ -61,7 +61,10 @@ internal fun BodyGenerator.tryGenerateOptimisedWhen(expression: IrWhen, symbols: val selectorLocal = functionContext.referenceLocal(SyntheticLocalType.TABLE_SWITCH_SELECTOR) generateExpression(subject) - body.buildSetLocal(selectorLocal) + + // TODO test + val noLocation = SourceLocation.NoLocation("When's binary search infra") + body.buildSetLocal(selectorLocal, noLocation) val resultType = context.transformBlockResultType(expression.type) //int overflow or load is too small then make table switch @@ -77,7 +80,7 @@ internal fun BodyGenerator.tryGenerateOptimisedWhen(expression: IrWhen, symbols: ) } else { createBinaryTable(selectorLocal, intBranches) - body.buildSetLocal(selectorLocal) + body.buildSetLocal(selectorLocal, noLocation) genTableIntSwitch( selectorLocal = selectorLocal, resultType = resultType, diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/locationUtils.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/locationUtils.kt index 6cd07c5503c..84204ecc3cb 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/locationUtils.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/locationUtils.kt @@ -5,11 +5,12 @@ package org.jetbrains.kotlin.backend.wasm.ir2wasm +import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.IrFileEntry import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.wasm.ir.source.location.SourceLocation -fun IrExpression.getSourceLocation(fileEntry: IrFileEntry?): SourceLocation { +fun IrElement.getSourceLocation(fileEntry: IrFileEntry?): SourceLocation { if (fileEntry == null) return SourceLocation.NoLocation("fileEntry is null") val path = fileEntry.name 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 08e7af97e0e..8ed53024c1a 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 @@ -133,8 +133,8 @@ abstract class WasmExpressionBuilder { buildInstr(WasmOp.LOCAL_GET, location, WasmImmediate.LocalIdx(local)) } - fun buildSetLocal(local: WasmLocal) { - buildInstr(WasmOp.LOCAL_SET, WasmImmediate.LocalIdx(local)) + fun buildSetLocal(local: WasmLocal, location: SourceLocation) { + buildInstr(WasmOp.LOCAL_SET, location, WasmImmediate.LocalIdx(local)) } fun buildGetGlobal(global: WasmSymbol) {