[Wasm] Require location for buildGetLocal and fix all usages

This commit is contained in:
Zalim Bashorov
2022-12-13 17:40:44 +01:00
parent 9e86ac071d
commit 0b59ef58dc
4 changed files with 27 additions and 21 deletions
@@ -30,6 +30,7 @@ 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.withLocation
import org.jetbrains.kotlin.wasm.ir.source.location.withNoLocation
class BodyGenerator( class BodyGenerator(
val context: WasmModuleCodegenContext, val context: WasmModuleCodegenContext,
@@ -244,7 +245,8 @@ class BodyGenerator(
if (valueDeclaration.isDispatchReceiver) if (valueDeclaration.isDispatchReceiver)
functionContext.referenceLocal(0) functionContext.referenceLocal(0)
else else
functionContext.referenceLocal(valueSymbol) functionContext.referenceLocal(valueSymbol),
expression.getSourceLocation()
) )
body.commentPreviousInstr { "type: ${valueDeclaration.type.render()}" } body.commentPreviousInstr { "type: ${valueDeclaration.type.render()}" }
} }
@@ -316,19 +318,21 @@ 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" }
body.buildGetLocal(thisParameter) withNoLocation("Constructor preamble") {
body.buildInstr(WasmOp.REF_IS_NULL) body.buildGetLocal(thisParameter, location)
body.buildIf("this_init") body.buildInstr(WasmOp.REF_IS_NULL)
generateAnyParameters(parentClass.symbol, SourceLocation.NoLocation("Constructor preamble")) body.buildIf("this_init")
val irFields: List<IrField> = parentClass.allFields(backendContext.irBuiltIns) generateAnyParameters(parentClass.symbol, location)
irFields.forEachIndexed { index, field -> val irFields: List<IrField> = parentClass.allFields(backendContext.irBuiltIns)
if (index > 1) { irFields.forEachIndexed { index, field ->
generateDefaultInitializerForType(context.transformType(field.type), body) 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() body.commentGroupEnd()
} }
@@ -341,7 +345,7 @@ class BodyGenerator(
return return
} }
body.buildGetLocal(functionContext.referenceLocal(0)) // this parameter body.buildGetLocal(functionContext.referenceLocal(0), SourceLocation.NoLocation("Get implicit dispatch receiver")) // this parameter
generateCall(expression) generateCall(expression)
} }
@@ -525,7 +529,7 @@ class BodyGenerator(
body.buildSetLocal(parameterLocal) body.buildSetLocal(parameterLocal)
body.buildBlock("isInterface", WasmI32) { outerLabel -> body.buildBlock("isInterface", WasmI32) { outerLabel ->
body.buildBlock("isInterface", WasmRefNullType(WasmHeapType.Simple.Data)) { innerLabel -> body.buildBlock("isInterface", WasmRefNullType(WasmHeapType.Simple.Data)) { innerLabel ->
body.buildGetLocal(parameterLocal) body.buildGetLocal(parameterLocal, location)
body.buildStructGet(context.referenceGcType(irBuiltIns.anyClass), WasmSymbol(1)) body.buildStructGet(context.referenceGcType(irBuiltIns.anyClass), WasmSymbol(1))
body.buildBrInstr(WasmOp.BR_ON_CAST_FAIL_DEPRECATED, innerLabel, classITable) body.buildBrInstr(WasmOp.BR_ON_CAST_FAIL_DEPRECATED, innerLabel, classITable)
body.buildStructGet(classITable, context.referenceClassITableInterfaceSlot(irInterface.symbol)) body.buildStructGet(classITable, context.referenceClassITableInterfaceSlot(irInterface.symbol))
@@ -669,7 +673,7 @@ class BodyGenerator(
} }
if (functionContext.irFunction is IrConstructor) { 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()) body.buildInstr(WasmOp.RETURN, expression.getSourceLocation())
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid 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.withLocation import org.jetbrains.kotlin.wasm.ir.source.location.withLocation
import org.jetbrains.kotlin.wasm.ir.source.location.withNoLocation import org.jetbrains.kotlin.wasm.ir.source.location.withNoLocation
@@ -164,7 +165,7 @@ class DeclarationGenerator(
// variables on constructor call sites. // variables on constructor call sites.
// TODO: Redesign construction scheme. // TODO: Redesign construction scheme.
if (declaration is IrConstructor) { 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) exprGen.buildInstr(WasmOp.RETURN)
} }
@@ -245,12 +245,13 @@ private fun <T> BodyGenerator.createBinaryTable(
thenBody: (T) -> Unit, thenBody: (T) -> Unit,
elseBody: () -> Unit elseBody: () -> Unit
) { ) {
// TODO test
val location = SourceLocation.NoLocation("When's binary search infra") val location = SourceLocation.NoLocation("When's binary search infra")
val size = toExcl - fromIncl val size = toExcl - fromIncl
if (size == 1) { if (size == 1) {
val (case, result) = sortedCases[fromIncl] val (case, result) = sortedCases[fromIncl]
body.buildGetLocal(selectorLocal) body.buildGetLocal(selectorLocal, location)
body.buildConstI32(case, location) body.buildConstI32(case, location)
body.buildInstr(WasmOp.I32_EQ) body.buildInstr(WasmOp.I32_EQ)
body.buildIf("binary_tree_branch", resultType) body.buildIf("binary_tree_branch", resultType)
@@ -263,7 +264,7 @@ private fun <T> BodyGenerator.createBinaryTable(
val border = fromIncl + size / 2 val border = fromIncl + size / 2
body.buildGetLocal(selectorLocal) body.buildGetLocal(selectorLocal, location)
body.buildConstI32(sortedCases[border].first, location) body.buildConstI32(sortedCases[border].first, location)
body.buildInstr(WasmOp.I32_LT_S) body.buildInstr(WasmOp.I32_LT_S)
body.buildIf("binary_tree_node", resultType) body.buildIf("binary_tree_node", resultType)
@@ -315,7 +316,7 @@ private fun BodyGenerator.genTableIntSwitch(
} }
resultType?.let { generateDefaultInitializerForType(it, body) } //stub value resultType?.let { generateDefaultInitializerForType(it, body) } //stub value
body.buildGetLocal(selectorLocal) body.buildGetLocal(selectorLocal, location)
if (shift != 0) { if (shift != 0) {
body.buildConstI32(shift, location) body.buildConstI32(shift, location)
body.buildInstr(WasmOp.I32_SUB) body.buildInstr(WasmOp.I32_SUB)
@@ -129,8 +129,8 @@ abstract class WasmExpressionBuilder {
) )
} }
fun buildGetLocal(local: WasmLocal) { fun buildGetLocal(local: WasmLocal, location: SourceLocation) {
buildInstr(WasmOp.LOCAL_GET, WasmImmediate.LocalIdx(local)) buildInstr(WasmOp.LOCAL_GET, location, WasmImmediate.LocalIdx(local))
} }
fun buildSetLocal(local: WasmLocal) { fun buildSetLocal(local: WasmLocal) {