From 3ecf9306b1e02ce9a742ad96fa8fa5e68053c3dd Mon Sep 17 00:00:00 2001 From: Igor Laevsky Date: Thu, 2 Sep 2021 17:28:24 +0300 Subject: [PATCH] WASM: NFC. Move IrBlockBody visit into body builder --- .../kotlin/backend/wasm/ir2wasm/BodyGenerator.kt | 4 ++++ .../backend/wasm/ir2wasm/DeclarationGenerator.kt | 14 ++++---------- 2 files changed, 8 insertions(+), 10 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 5977e3c907c..270a8a8b675 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 @@ -339,6 +339,10 @@ class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorV return true } + override fun visitBlockBody(body: IrBlockBody) { + body.statements.forEach(::statementToWasmInstruction) + } + override fun visitContainerExpression(expression: IrContainerExpression) { val statements = expression.statements if (statements.isEmpty()) { 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 b13e9e0a8fc..6358ae5d16b 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 @@ -137,14 +137,8 @@ class DeclarationGenerator(val context: WasmModuleCodegenContext) : IrElementVis val exprGen = functionCodegenContext.bodyGen val bodyBuilder = BodyGenerator(functionCodegenContext) - when (val body = declaration.body) { - is IrBlockBody -> - for (statement in body.statements) { - bodyBuilder.statementToWasmInstruction(statement) - } - - else -> error("Unexpected body $body") - } + require(declaration.body is IrBlockBody) { "Only IrBlockBody is supported" } + declaration.body?.acceptVoid(bodyBuilder) // Return implicit this from constructions to avoid extra tmp // variables on constructor call sites. @@ -155,8 +149,8 @@ class DeclarationGenerator(val context: WasmModuleCodegenContext) : IrElementVis } // Add unreachable if function returns something but not as a last instruction. - if (wasmFunctionType.resultTypes.isNotEmpty() && declaration.body is IrBlockBody) { - // TODO: Add unreachable only if needed + // We can do a separate lowering which adds explicit returns everywhere instead. + if (wasmFunctionType.resultTypes.isNotEmpty()) { exprGen.buildUnreachable() }