backend.native: improve type mapping

including types of functions
This commit is contained in:
Svyatoslav Scherbina
2016-10-04 10:34:48 +03:00
parent ba69e02ea6
commit 5ea3c9521e
3 changed files with 20 additions and 3 deletions
@@ -49,7 +49,8 @@ internal interface ContextUtils {
assert (this.kind.isReal)
val globalName = this.symbolName
val module = context.llvmModule
val functionType = LLVMFunctionType(LLVMVoidType(), null, 0, 0) // FIXME: use correct types
val functionType = getLlvmFunctionType(this)
val function = LLVMGetNamedFunction(module, globalName) ?: LLVMAddFunction(module, globalName, functionType)
return compileTimeValue(function)
}
@@ -6,10 +6,12 @@ import org.jetbrains.kotlin.types.KotlinType
internal fun getLLVMType(type: KotlinType): LLVMOpaqueType {
return when {
KotlinBuiltIns.isBoolean(type) || KotlinBuiltIns.isByte(type) -> LLVMInt8Type()
KotlinBuiltIns.isBoolean(type) -> LLVMInt1Type()
KotlinBuiltIns.isByte(type) -> LLVMInt8Type()
KotlinBuiltIns.isShort(type) || KotlinBuiltIns.isChar(type) -> LLVMInt16Type()
KotlinBuiltIns.isInt(type) -> LLVMInt32Type()
KotlinBuiltIns.isLong(type) -> LLVMInt64Type()
KotlinBuiltIns.isUnit(type) -> LLVMVoidType() // TODO: handle Unit parameter case
!KotlinBuiltIns.isPrimitiveType(type) -> LLVMPointerType(LLVMInt8Type(), 0)
else -> throw NotImplementedError()
}!!
@@ -2,6 +2,8 @@ package org.jetbrains.kotlin.backend.native.llvm
import kotlin_native.interop.mallocNativeArrayOf
import llvm.*
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.utils.singletonOrEmptyList
/**
* Represents the value which can be emitted as bitcode const value
@@ -55,4 +57,16 @@ internal fun compileTimeValue(value: LLVMOpaqueValue?) = object : CompileTimeVal
internal val int32Type = LLVMInt32Type()
internal fun pointerType(pointeeType: LLVMOpaqueType?) = LLVMPointerType(pointeeType, 0)
internal fun pointerType(pointeeType: LLVMOpaqueType?) = LLVMPointerType(pointeeType, 0)
internal fun getLlvmFunctionType(function: FunctionDescriptor): LLVMOpaqueType? {
val returnType = getLLVMType(function.returnType!!)
val params = function.dispatchReceiverParameter.singletonOrEmptyList() +
function.extensionReceiverParameter.singletonOrEmptyList() +
function.valueParameters
val paramTypes = params.map { getLLVMType(it.type) }.toTypedArray()
val paramTypesPtr = mallocNativeArrayOf(LLVMOpaqueType, *paramTypes)[0] // TODO: dispose
return LLVMFunctionType(returnType, paramTypesPtr, paramTypes.size, 0)
}