Explicitly sign extend arguments of exported C functions. (#2370)
This commit is contained in:
+27
-10
@@ -5,10 +5,7 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.backend.konan.llvm
|
package org.jetbrains.kotlin.backend.konan.llvm
|
||||||
|
|
||||||
import kotlinx.cinterop.allocArray
|
import kotlinx.cinterop.*
|
||||||
import kotlinx.cinterop.cValuesOf
|
|
||||||
import kotlinx.cinterop.get
|
|
||||||
import kotlinx.cinterop.memScoped
|
|
||||||
import llvm.*
|
import llvm.*
|
||||||
import org.jetbrains.kotlin.backend.konan.Context
|
import org.jetbrains.kotlin.backend.konan.Context
|
||||||
import org.jetbrains.kotlin.backend.konan.descriptors.findPackage
|
import org.jetbrains.kotlin.backend.konan.descriptors.findPackage
|
||||||
@@ -31,16 +28,22 @@ import kotlin.reflect.KProperty
|
|||||||
internal sealed class SlotType {
|
internal sealed class SlotType {
|
||||||
// Frame local arena slot can be used.
|
// Frame local arena slot can be used.
|
||||||
object ARENA : SlotType()
|
object ARENA : SlotType()
|
||||||
|
|
||||||
// Return slot can be used.
|
// Return slot can be used.
|
||||||
object RETURN : SlotType()
|
object RETURN : SlotType()
|
||||||
|
|
||||||
// Return slot, if it is an arena, can be used.
|
// Return slot, if it is an arena, can be used.
|
||||||
object RETURN_IF_ARENA : SlotType()
|
object RETURN_IF_ARENA : SlotType()
|
||||||
|
|
||||||
// Param slot, if it is an arena, can be used.
|
// Param slot, if it is an arena, can be used.
|
||||||
class PARAM_IF_ARENA(val parameter: Int) : SlotType()
|
class PARAM_IF_ARENA(val parameter: Int) : SlotType()
|
||||||
|
|
||||||
// Params slot, if it is an arena, can be used.
|
// Params slot, if it is an arena, can be used.
|
||||||
class PARAMS_IF_ARENA(val parameters: IntArray, val useReturnSlot: Boolean) : SlotType()
|
class PARAMS_IF_ARENA(val parameters: IntArray, val useReturnSlot: Boolean) : SlotType()
|
||||||
|
|
||||||
// Anonymous slot.
|
// Anonymous slot.
|
||||||
object ANONYMOUS : SlotType()
|
object ANONYMOUS : SlotType()
|
||||||
|
|
||||||
// Unknown slot type.
|
// Unknown slot type.
|
||||||
object UNKNOWN : SlotType()
|
object UNKNOWN : SlotType()
|
||||||
}
|
}
|
||||||
@@ -245,6 +248,7 @@ internal val Name.localHash: LocalHash
|
|||||||
internal val FqName.localHash: LocalHash
|
internal val FqName.localHash: LocalHash
|
||||||
get() = this.toString().localHash
|
get() = this.toString().localHash
|
||||||
|
|
||||||
|
|
||||||
internal class Llvm(val context: Context, val llvmModule: LLVMModuleRef) {
|
internal class Llvm(val context: Context, val llvmModule: LLVMModuleRef) {
|
||||||
|
|
||||||
private fun importFunction(name: String, otherModule: LLVMModuleRef): LLVMValueRef {
|
private fun importFunction(name: String, otherModule: LLVMModuleRef): LLVMValueRef {
|
||||||
@@ -302,7 +306,21 @@ internal class Llvm(val context: Context, val llvmModule: LLVMModuleRef) {
|
|||||||
assert(LLVMGetLinkage(found) == LLVMLinkage.LLVMExternalLinkage)
|
assert(LLVMGetLinkage(found) == LLVMLinkage.LLVMExternalLinkage)
|
||||||
return found
|
return found
|
||||||
} else {
|
} else {
|
||||||
return LLVMAddFunction(llvmModule, name, type)!!
|
// As exported functions are written in C++ they assume sign extension for promoted types -
|
||||||
|
// mention that in attributes.
|
||||||
|
val function = LLVMAddFunction(llvmModule, name, type)!!
|
||||||
|
return memScoped {
|
||||||
|
val paramCount = LLVMCountParamTypes(type)
|
||||||
|
val paramTypes = allocArray<LLVMTypeRefVar>(paramCount)
|
||||||
|
LLVMGetParamTypes(type, paramTypes)
|
||||||
|
(0 until paramCount).forEach { index ->
|
||||||
|
val paramType = paramTypes[index]
|
||||||
|
addFunctionSignext(function, index + 1, paramType)
|
||||||
|
}
|
||||||
|
val returnType = LLVMGetReturnType(type)
|
||||||
|
addFunctionSignext(function, 0, returnType)
|
||||||
|
function
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -338,7 +356,8 @@ internal class Llvm(val context: Context, val llvmModule: LLVMModuleRef) {
|
|||||||
.getFullList(TopologicalLibraryOrder)
|
.getFullList(TopologicalLibraryOrder)
|
||||||
}
|
}
|
||||||
|
|
||||||
val librariesForLibraryManifest: List<KonanLibrary> get() {
|
val librariesForLibraryManifest: List<KonanLibrary>
|
||||||
|
get() {
|
||||||
// Note: library manifest should contain the list of all user libraries and frontend-used default libraries.
|
// Note: library manifest should contain the list of all user libraries and frontend-used default libraries.
|
||||||
// However this would result into linking too many default libraries into the application which uses current
|
// However this would result into linking too many default libraries into the application which uses current
|
||||||
// library. This problem should probably be fixed by adding different kind of dependencies to library
|
// library. This problem should probably be fixed by adding different kind of dependencies to library
|
||||||
@@ -411,10 +430,8 @@ internal class Llvm(val context: Context, val llvmModule: LLVMModuleRef) {
|
|||||||
val tlsMode by lazy {
|
val tlsMode by lazy {
|
||||||
when (target) {
|
when (target) {
|
||||||
KonanTarget.WASM32,
|
KonanTarget.WASM32,
|
||||||
is KonanTarget.ZEPHYR
|
is KonanTarget.ZEPHYR -> LLVMThreadLocalMode.LLVMNotThreadLocal
|
||||||
-> LLVMThreadLocalMode.LLVMNotThreadLocal
|
else -> LLVMThreadLocalMode.LLVMGeneralDynamicTLSModel
|
||||||
else
|
|
||||||
-> LLVMThreadLocalMode.LLVMGeneralDynamicTLSModel
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+15
-2
@@ -267,8 +267,14 @@ fun parseBitcodeFile(path: String): LLVMModuleRef = memScoped {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private val nounwindAttrKindId: Int
|
private val nounwindAttrKindId by lazy {
|
||||||
get() = getAttributeKindId("nounwind")
|
getAttributeKindId("nounwind")
|
||||||
|
}
|
||||||
|
|
||||||
|
private val signextAttrKindId by lazy {
|
||||||
|
getAttributeKindId("signext")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
fun isFunctionNoUnwind(function: LLVMValueRef): Boolean {
|
fun isFunctionNoUnwind(function: LLVMValueRef): Boolean {
|
||||||
|
|
||||||
@@ -289,6 +295,13 @@ fun setFunctionNoUnwind(function: LLVMValueRef) {
|
|||||||
LLVMAddAttributeAtIndex(function, LLVMAttributeFunctionIndex, attribute)
|
LLVMAddAttributeAtIndex(function, LLVMAttributeFunctionIndex, attribute)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun addFunctionSignext(function: LLVMValueRef, index: Int, type: LLVMTypeRef?) {
|
||||||
|
if (type == int1Type || type == int8Type || type == int16Type) {
|
||||||
|
val attribute = LLVMCreateEnumAttribute(LLVMGetTypeContext(function.type), signextAttrKindId, 0)!!
|
||||||
|
LLVMAddAttributeAtIndex(function, index, attribute)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
internal fun String.mdString() = LLVMMDString(this, this.length)!!
|
internal fun String.mdString() = LLVMMDString(this, this.length)!!
|
||||||
internal fun node(vararg it:LLVMValueRef) = LLVMMDNode(it.toList().toCValues(), it.size)
|
internal fun node(vararg it:LLVMValueRef) = LLVMMDNode(it.toList().toCValues(), it.size)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user