From 0b59ef58dc895c29ea3e848e9c1f8d9b93dddef3 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Tue, 13 Dec 2022 17:40:44 +0100 Subject: [PATCH] [Wasm] Require location for `buildGetLocal` and fix all usages --- .../backend/wasm/ir2wasm/BodyGenerator.kt | 34 +++++++++++-------- .../wasm/ir2wasm/DeclarationGenerator.kt | 3 +- .../wasm/ir2wasm/OptimisedWhenGenerator.kt | 7 ++-- .../kotlin/wasm/ir/WasmExpressionBuilder.kt | 4 +-- 4 files changed, 27 insertions(+), 21 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 3d1f6e4aa2b..13acb6e272c 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 @@ -30,6 +30,7 @@ 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, @@ -244,7 +245,8 @@ class BodyGenerator( if (valueDeclaration.isDispatchReceiver) functionContext.referenceLocal(0) else - functionContext.referenceLocal(valueSymbol) + functionContext.referenceLocal(valueSymbol), + expression.getSourceLocation() ) body.commentPreviousInstr { "type: ${valueDeclaration.type.render()}" } } @@ -316,19 +318,21 @@ class BodyGenerator( if (parentClass.isAbstractOrSealed) return val thisParameter = functionContext.referenceLocal(parentClass.thisReceiver!!.symbol) body.commentGroupStart { "Object creation prefix" } - body.buildGetLocal(thisParameter) - body.buildInstr(WasmOp.REF_IS_NULL) - body.buildIf("this_init") - generateAnyParameters(parentClass.symbol, SourceLocation.NoLocation("Constructor preamble")) - val irFields: List = parentClass.allFields(backendContext.irBuiltIns) - irFields.forEachIndexed { index, field -> - if (index > 1) { - generateDefaultInitializerForType(context.transformType(field.type), body) + withNoLocation("Constructor preamble") { + body.buildGetLocal(thisParameter, location) + body.buildInstr(WasmOp.REF_IS_NULL) + body.buildIf("this_init") + generateAnyParameters(parentClass.symbol, location) + val irFields: List = parentClass.allFields(backendContext.irBuiltIns) + irFields.forEachIndexed { index, field -> + if (index > 1) { + generateDefaultInitializerForType(context.transformType(field.type), body) + } } + body.buildStructNew(context.referenceGcType(parentClass.symbol)) + body.buildSetLocal(thisParameter) + body.buildEnd() } - body.buildStructNew(context.referenceGcType(parentClass.symbol)) - body.buildSetLocal(thisParameter) - body.buildEnd() body.commentGroupEnd() } @@ -341,7 +345,7 @@ class BodyGenerator( return } - body.buildGetLocal(functionContext.referenceLocal(0)) // this parameter + body.buildGetLocal(functionContext.referenceLocal(0), SourceLocation.NoLocation("Get implicit dispatch receiver")) // this parameter generateCall(expression) } @@ -525,7 +529,7 @@ class BodyGenerator( body.buildSetLocal(parameterLocal) body.buildBlock("isInterface", WasmI32) { outerLabel -> body.buildBlock("isInterface", WasmRefNullType(WasmHeapType.Simple.Data)) { innerLabel -> - body.buildGetLocal(parameterLocal) + body.buildGetLocal(parameterLocal, location) body.buildStructGet(context.referenceGcType(irBuiltIns.anyClass), WasmSymbol(1)) body.buildBrInstr(WasmOp.BR_ON_CAST_FAIL_DEPRECATED, innerLabel, classITable) body.buildStructGet(classITable, context.referenceClassITableInterfaceSlot(irInterface.symbol)) @@ -669,7 +673,7 @@ class BodyGenerator( } if (functionContext.irFunction is IrConstructor) { - body.buildGetLocal(functionContext.referenceLocal(0)) + body.buildGetLocal(functionContext.referenceLocal(0), SourceLocation.NoLocation("Get implicit dispatch receiver")) } body.buildInstr(WasmOp.RETURN, expression.getSourceLocation()) diff --git a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt index 0db2c6a67f9..b83fcfe0508 100644 --- a/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt +++ b/compiler/ir/backend.wasm/src/org/jetbrains/kotlin/backend/wasm/ir2wasm/DeclarationGenerator.kt @@ -29,6 +29,7 @@ import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid 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 @@ -164,7 +165,7 @@ class DeclarationGenerator( // variables on constructor call sites. // TODO: Redesign construction scheme. if (declaration is IrConstructor) { - exprGen.buildGetLocal(/*implicit this*/ function.locals[0]) + exprGen.buildGetLocal(/*implicit this*/ function.locals[0], SourceLocation.NoLocation("Get implicit dispatch receiver")) exprGen.buildInstr(WasmOp.RETURN) } 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 7612ac37e8d..c09715d9b43 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 @@ -245,12 +245,13 @@ private fun BodyGenerator.createBinaryTable( thenBody: (T) -> Unit, elseBody: () -> Unit ) { + // TODO test val location = SourceLocation.NoLocation("When's binary search infra") val size = toExcl - fromIncl if (size == 1) { val (case, result) = sortedCases[fromIncl] - body.buildGetLocal(selectorLocal) + body.buildGetLocal(selectorLocal, location) body.buildConstI32(case, location) body.buildInstr(WasmOp.I32_EQ) body.buildIf("binary_tree_branch", resultType) @@ -263,7 +264,7 @@ private fun BodyGenerator.createBinaryTable( val border = fromIncl + size / 2 - body.buildGetLocal(selectorLocal) + body.buildGetLocal(selectorLocal, location) body.buildConstI32(sortedCases[border].first, location) body.buildInstr(WasmOp.I32_LT_S) body.buildIf("binary_tree_node", resultType) @@ -315,7 +316,7 @@ private fun BodyGenerator.genTableIntSwitch( } resultType?.let { generateDefaultInitializerForType(it, body) } //stub value - body.buildGetLocal(selectorLocal) + body.buildGetLocal(selectorLocal, location) if (shift != 0) { body.buildConstI32(shift, location) body.buildInstr(WasmOp.I32_SUB) 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 f82c57f730d..08e7af97e0e 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 @@ -129,8 +129,8 @@ abstract class WasmExpressionBuilder { ) } - fun buildGetLocal(local: WasmLocal) { - buildInstr(WasmOp.LOCAL_GET, WasmImmediate.LocalIdx(local)) + fun buildGetLocal(local: WasmLocal, location: SourceLocation) { + buildInstr(WasmOp.LOCAL_GET, location, WasmImmediate.LocalIdx(local)) } fun buildSetLocal(local: WasmLocal) {