From fa21a260b1dbd2019c6a7853bb1872b078edf07e Mon Sep 17 00:00:00 2001 From: Igor Chevdar Date: Wed, 4 Oct 2017 17:14:33 +0300 Subject: [PATCH] Fixed coroutines for wasm Replaced llvm indirectBr instruction with switch instruction. --- .../kotlin/backend/konan/KonanConfig.kt | 2 ++ .../backend/konan/llvm/CodeGenerator.kt | 8 +++++ .../kotlin/backend/konan/llvm/IrToBitcode.kt | 32 ++++++++++++++----- backend.native/tests/build.gradle | 26 --------------- 4 files changed, 34 insertions(+), 34 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt index 082a4205900..0fba27135e9 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanConfig.kt @@ -46,6 +46,8 @@ class KonanConfig(val project: Project, val configuration: CompilerConfiguration internal val targetManager = TargetManager( configuration.get(KonanConfigKeys.TARGET)) + val indirectBranchesAreAllowed = targetManager.target != KonanTarget.WASM32 + init { val target = targetManager.target if (!target.enabled) { diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt index 8a7ede5912d..a0f81b99a9a 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/CodeGenerator.kt @@ -461,6 +461,14 @@ internal class FunctionGenerationContext(val function: LLVMValueRef, currentPositionHolder.setAfterTerminator() return indirectBr } + + fun switch(value: LLVMValueRef, cases: Collection>, elseBB: LLVMBasicBlockRef): LLVMValueRef? { + val switch = LLVMBuildSwitch(builder, value, elseBB, cases.size) + cases.forEach { LLVMAddCase(switch, it.first, it.second) } + currentPositionHolder.setAfterTerminator() + return switch + } + fun resetDebugLocation() { if (!context.shouldContainDebugInfo()) return if (!currentPositionHolder.isAfterTerminator) 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 3102d642468..c1862bd3dc9 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 @@ -189,7 +189,7 @@ internal interface CodeContext { */ fun classScope(): CodeContext? - fun addResumePoint(bbLabel: LLVMBasicBlockRef) + fun addResumePoint(bbLabel: LLVMBasicBlockRef): Int } //-------------------------------------------------------------------------// @@ -1747,8 +1747,10 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map) : InnerScopeImpl() { - override fun addResumePoint(bbLabel: LLVMBasicBlockRef) { + override fun addResumePoint(bbLabel: LLVMBasicBlockRef): Int { + val result = resumePoints.size resumePoints.add(bbLabel) + return result } } @@ -1765,26 +1767,40 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map