From 1ec7d806fac206fc0e9ab3e0aedcb7837da0ec74 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Thu, 27 Oct 2016 17:57:00 +0300 Subject: [PATCH] backend: properly import functions from runtime --- .../kotlin/backend/native/llvm/Context.kt | 19 ++++++++++++++++--- .../kotlin/backend/native/llvm/IrToBitcode.kt | 3 +-- .../kotlin/backend/native/llvm/LlvmUtils.kt | 5 +++++ .../kotlin/backend/native/llvm/Runtime.kt | 9 --------- 4 files changed, 22 insertions(+), 14 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/Context.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/Context.kt index 3eb80618614..22d48eb8bea 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/Context.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/Context.kt @@ -1,8 +1,6 @@ package org.jetbrains.kotlin.backend.native.llvm -import llvm.LLVMCreateBuilder -import llvm.LLVMDisposeBuilder -import llvm.LLVMOpaqueModule +import llvm.* import org.jetbrains.kotlin.backend.native.ModuleIndex import org.jetbrains.kotlin.ir.declarations.IrModuleFragment @@ -11,6 +9,21 @@ internal class Context(val irModule: IrModuleFragment, val runtime: Runtime, val val llvmBuilder = LLVMCreateBuilder() + private fun importFunction(name: String, otherModule: LLVMOpaqueModule): LLVMOpaqueValue { + if (LLVMGetNamedFunction(llvmModule, name) != null) { + throw IllegalArgumentException("function $name already exists") + } + + val externalFunction = LLVMGetNamedFunction(otherModule, name)!! + + val functionType = getFunctionType(externalFunction) + return LLVMAddFunction(llvmModule, name, functionType)!! + } + + private fun importRtFunction(name: String) = importFunction(name, runtime.llvmModule) + + val allocInstanceFunction = importRtFunction("AllocInstance") + fun dispose() { LLVMDisposeBuilder(llvmBuilder) } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/IrToBitcode.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/IrToBitcode.kt index b768602a4b4..79c1a00e167 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/IrToBitcode.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/IrToBitcode.kt @@ -22,7 +22,6 @@ fun emitLLVM(module: IrModuleFragment, runtimeFile: String, outFile: String) { val context = Context(module, runtime, llvmModule) // TODO: dispose module.acceptVoid(RTTIGeneratorVisitor(context)) - context.runtime.importRuntime(llvmModule) module.acceptVoid(CodeGeneratorVisitor(context)) memScoped { val errorRef = alloc(Int8Box.ref) @@ -180,7 +179,7 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid private fun evaluateConstructorCall(variableName: String, callee: IrCall, args: MutableList): LLVMOpaqueValue? { memScoped { val params = allocNativeArrayOf(LLVMOpaqueValue, generator.typeInfoValue((callee.descriptor as ClassConstructorDescriptor).containingDeclaration), Int32(1).getLlvmValue()) - val thisValue = LLVMBuildCall(context.llvmBuilder, context.runtime.allocInstanceFunction, params[0], 2, variableName) + val thisValue = LLVMBuildCall(context.llvmBuilder, context.allocInstanceFunction, params[0], 2, variableName) val constructorParams: MutableList = mutableListOf() diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/LlvmUtils.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/LlvmUtils.kt index e747ec8115c..6a5cb75d9c4 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/LlvmUtils.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/LlvmUtils.kt @@ -97,4 +97,9 @@ internal fun NativeArray.asCompileTimeValue(size: Int): ConstArray { return ConstArray(int8Type, (0 .. size-1).map { Int8(this[it].value) }) +} + +internal fun getFunctionType(ptrToFunction: LLVMOpaqueValue?): LLVMOpaqueType { + val typeOfPtrToFunction = LLVMTypeOf(ptrToFunction) + return LLVMGetElementType(typeOfPtrToFunction)!! } \ No newline at end of file diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/Runtime.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/Runtime.kt index 38cf961ad48..faf748978ae 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/Runtime.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/Runtime.kt @@ -34,15 +34,6 @@ class Runtime(private val bitcodeFile: String) { val fieldTableRecordType = LLVMGetTypeByName(llvmModule, "struct.FieldTableRecord") val methodTableRecordType = LLVMGetTypeByName(llvmModule, "struct.MethodTableRecord") val globalhHashType = LLVMGetTypeByName(llvmModule, "struct.GlobalHash") - val allocInstanceFunction = LLVMGetNamedFunction(llvmModule, "AllocInstance") - - fun importRuntime(module: LLVMOpaqueModule) { - memScoped { - val params = allocNativeArrayOf(LLVMOpaqueType, pointerType(typeInfoType), LLVMInt32Type()) - val functionAllocInstanceType = LLVMFunctionType(pointerType(LLVMInt8Type()),params[0], 2, 0) - LLVMAddFunction(module, "AllocInstance", functionAllocInstanceType) - } - } val target = LLVMGetTarget(llvmModule)!!.asCString().toString()