From 7d5890e35467bc047988d95d4cc2b47fbe752879 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Thu, 8 Dec 2016 11:51:39 +0700 Subject: [PATCH] backend.konan: apply common lowering --- .../backend/konan/KonanBackendContext.kt | 12 +++++++++++ .../kotlin/backend/konan/KonanLower.kt | 21 +++++++++++++++++++ .../kotlin/backend/konan/llvm/Context.kt | 5 ++++- .../kotlin/backend/konan/llvm/IrToBitcode.kt | 2 ++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanBackendContext.kt create mode 100644 backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanBackendContext.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanBackendContext.kt new file mode 100644 index 00000000000..122add605c2 --- /dev/null +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanBackendContext.kt @@ -0,0 +1,12 @@ +package org.jetbrains.kotlin.backend.konan + +import org.jetbrains.kotlin.backend.common.BackendContext +import org.jetbrains.kotlin.backend.konan.llvm.KonanPlatform + +open internal class KonanBackendContext : BackendContext { + override val builtIns = KonanPlatform.builtIns + + override val sharedVariablesManager by lazy { + TODO() + } +} \ No newline at end of file diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt new file mode 100644 index 00000000000..5fd846dd2c7 --- /dev/null +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt @@ -0,0 +1,21 @@ +package org.jetbrains.kotlin.backend.konan + +import org.jetbrains.kotlin.backend.common.lower.LocalFunctionsLowering +import org.jetbrains.kotlin.backend.common.lower.SharedVariablesLowering +import org.jetbrains.kotlin.backend.common.runOnFilePostfix +import org.jetbrains.kotlin.ir.declarations.IrFile +import org.jetbrains.kotlin.ir.declarations.IrModuleFragment + +internal class KonanLower(val context: KonanBackendContext) { + + fun lower(module: IrModuleFragment) { + module.files.forEach { + lower(it) + } + } + + fun lower(irFile: IrFile) { + SharedVariablesLowering(context).runOnFilePostfix(irFile) + LocalFunctionsLowering(context).runOnFilePostfix(irFile) + } +} \ No newline at end of file diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/Context.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/Context.kt index 144da676225..ade63eb27bb 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/Context.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/Context.kt @@ -1,14 +1,17 @@ package org.jetbrains.kotlin.backend.konan.llvm import llvm.* +import org.jetbrains.kotlin.backend.konan.KonanBackendContext import org.jetbrains.kotlin.backend.konan.ModuleIndex import org.jetbrains.kotlin.ir.declarations.IrModuleFragment import org.jetbrains.kotlin.resolve.BindingContext +// TODO: module-independent part should probably be extracted internal class Context(val irModule: IrModuleFragment, val runtime: Runtime, val llvmModule: LLVMModuleRef, - val bindingContext: BindingContext) { + val bindingContext: BindingContext) : KonanBackendContext() { + val moduleIndex = ModuleIndex(irModule) private fun importFunction(name: String, otherModule: LLVMModuleRef): LLVMValueRef { 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 b0e3418d76b..fb94635c58e 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 @@ -36,6 +36,8 @@ fun emitLLVM(module: IrModuleFragment, runtimeFile: String, outFile: String) { val context = Context( module, runtime, llvmModule, BindingContext.EMPTY) // TODO: dispose + KonanLower(context).lower(module) + module.acceptVoid(RTTIGeneratorVisitor(context)) println("\n--- Generate bitcode ------------------------------------------------------\n") module.acceptVoid(CodeGeneratorVisitor(context))