From 0c81cfdb24a3fbde560976eaa820444e5a009a7d Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Tue, 29 Nov 2016 13:59:24 +0700 Subject: [PATCH] Review feedback, part 2: implement return to any target from FinalizingScope --- .../kotlin/backend/konan/llvm/IrToBitcode.kt | 27 +++++++++---------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt index 25438c57517..18f249173fa 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt @@ -748,26 +748,25 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid private abstract inner class FinalizingScope() : CatchingScope() { /** - * The [ContinuationBlock] to be used for `return` from the function. - * Expects return value as its value. + * Cache for [genReturn]. + * + * `returnBlocks[func]` contains the [ContinuationBlock] to be used for `return` from `func`; + * the block expects return value as its value. */ - private val returnFromCurrent: ContinuationBlock by lazy { - using(outerContext) { - val target = codegen.currentFunction!! - continuationBlock(target.returnType!!) { returnValue -> - genFinalize() - outerContext.genReturn(target, returnValue) - } - } - } + private val returnBlocks = mutableMapOf() // Jump to finalize-and-return instead of simply returning. override fun genReturn(target: CallableDescriptor, value: LLVMValueRef?) { - if (target != codegen.currentFunction) { - TODO() + val block = returnBlocks.getOrPut(target) { + using(outerContext) { + continuationBlock(target.returnType!!) { returnValue -> + genFinalize() + outerContext.genReturn(target, returnValue) + } + } } - jump(returnFromCurrent, value) + jump(block, value) } override fun genBreak(loop: IrLoop) = TODO()