WASM: NFC. Move IrBlockBody visit into body builder

This commit is contained in:
Igor Laevsky
2021-09-02 17:28:24 +03:00
committed by TeamCityServer
parent a246ec636e
commit 3ecf9306b1
2 changed files with 8 additions and 10 deletions
@@ -339,6 +339,10 @@ class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorV
return true return true
} }
override fun visitBlockBody(body: IrBlockBody) {
body.statements.forEach(::statementToWasmInstruction)
}
override fun visitContainerExpression(expression: IrContainerExpression) { override fun visitContainerExpression(expression: IrContainerExpression) {
val statements = expression.statements val statements = expression.statements
if (statements.isEmpty()) { if (statements.isEmpty()) {
@@ -137,14 +137,8 @@ class DeclarationGenerator(val context: WasmModuleCodegenContext) : IrElementVis
val exprGen = functionCodegenContext.bodyGen val exprGen = functionCodegenContext.bodyGen
val bodyBuilder = BodyGenerator(functionCodegenContext) val bodyBuilder = BodyGenerator(functionCodegenContext)
when (val body = declaration.body) { require(declaration.body is IrBlockBody) { "Only IrBlockBody is supported" }
is IrBlockBody -> declaration.body?.acceptVoid(bodyBuilder)
for (statement in body.statements) {
bodyBuilder.statementToWasmInstruction(statement)
}
else -> error("Unexpected body $body")
}
// Return implicit this from constructions to avoid extra tmp // Return implicit this from constructions to avoid extra tmp
// variables on constructor call sites. // 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. // Add unreachable if function returns something but not as a last instruction.
if (wasmFunctionType.resultTypes.isNotEmpty() && declaration.body is IrBlockBody) { // We can do a separate lowering which adds explicit returns everywhere instead.
// TODO: Add unreachable only if needed if (wasmFunctionType.resultTypes.isNotEmpty()) {
exprGen.buildUnreachable() exprGen.buildUnreachable()
} }