backend.native: add minor improvements to code for compile time values

This commit is contained in:
Svyatoslav Scherbina
2016-10-19 11:30:54 +03:00
parent d43b0f62bc
commit 9f5f2eecfc
2 changed files with 12 additions and 10 deletions
@@ -92,13 +92,15 @@ internal interface ContextUtils {
/**
* Returns pointer to first element of given array.
*
* Note: this function doesn't depend on the context
*
* @param arrayPtr pointer to array
*/
private fun getPtrToFirstElem(arrayPtr: CompileTimeValue): CompileTimeValue {
val indices = longArrayOf(0, 0).map { LLVMConstInt(LLVMInt32Type(), it, 0) }.toTypedArray()
val indicesNativeArrayPtr = mallocNativeArrayOf(LLVMOpaqueValue, *indices)[0] // TODO: dispose
return compileTimeValue(LLVMBuildGEP(context.llvmBuilder, arrayPtr.getLlvmValue(), indicesNativeArrayPtr, indices.size, ""))
return compileTimeValue(LLVMConstGEP(arrayPtr.getLlvmValue(), indicesNativeArrayPtr, indices.size))
}
/**
@@ -8,9 +8,9 @@ import org.jetbrains.kotlin.utils.singletonOrEmptyList
/**
* Represents the value which can be emitted as bitcode const value
*/
internal abstract class CompileTimeValue {
internal interface CompileTimeValue {
abstract fun getLlvmValue(): LLVMOpaqueValue?
fun getLlvmValue(): LLVMOpaqueValue?
fun getLlvmType(): LLVMOpaqueType? {
return LLVMTypeOf(getLlvmValue())
@@ -18,7 +18,7 @@ internal abstract class CompileTimeValue {
}
internal class ConstArray(val elemType: LLVMOpaqueType?, val elements: List<CompileTimeValue>) : CompileTimeValue() {
internal class ConstArray(val elemType: LLVMOpaqueType?, val elements: List<CompileTimeValue>) : CompileTimeValue {
override fun getLlvmValue(): LLVMOpaqueValue? {
val values = elements.map { it.getLlvmValue() }.toTypedArray()
@@ -28,7 +28,7 @@ internal class ConstArray(val elemType: LLVMOpaqueType?, val elements: List<Comp
}
}
internal open class Struct(val type: LLVMOpaqueType?, val elements: List<CompileTimeValue>) : CompileTimeValue() {
internal open class Struct(val type: LLVMOpaqueType?, val elements: List<CompileTimeValue>) : CompileTimeValue {
constructor(type: LLVMOpaqueType?, vararg elements: CompileTimeValue) : this(type, elements.toList())
@@ -39,23 +39,23 @@ internal open class Struct(val type: LLVMOpaqueType?, val elements: List<Compile
}
}
internal class Int8(val value: Byte) : CompileTimeValue() {
internal class Int8(val value: Byte) : CompileTimeValue {
override fun getLlvmValue() = LLVMConstInt(LLVMInt8Type(), value.toLong(), 1)
}
internal class Int32(val value: Int) : CompileTimeValue() {
internal class Int32(val value: Int) : CompileTimeValue {
override fun getLlvmValue() = LLVMConstInt(LLVMInt32Type(), value.toLong(), 1)
}
internal class Int64(val value: Long) : CompileTimeValue() {
internal class Int64(val value: Long) : CompileTimeValue {
override fun getLlvmValue() = LLVMConstInt(LLVMInt64Type(), value, 1)
}
internal class Zero(val type: LLVMOpaqueType?) : CompileTimeValue() {
internal class Zero(val type: LLVMOpaqueType?) : CompileTimeValue {
override fun getLlvmValue() = LLVMConstNull(type)
}
internal fun compileTimeValue(value: LLVMOpaqueValue?) = object : CompileTimeValue() {
internal fun compileTimeValue(value: LLVMOpaqueValue?) = object : CompileTimeValue {
override fun getLlvmValue() = value
}