Simplify working with values in interop
* Access primitive interop array elements and struct fields without `.value` * Simplify C*Var* interop types names
This commit is contained in:
committed by
SvyatoslavScherbina
parent
f94a47518a
commit
50c3be3fc2
+2
-2
@@ -80,11 +80,11 @@ internal class InteropBuiltIns(builtIns: KonanBuiltIns) {
|
||||
val interpretCPointer = packageScope.getContributedFunctions("interpretCPointer").single()
|
||||
|
||||
val arrayGetByIntIndex = packageScope.getContributedFunctions("get").single {
|
||||
KotlinBuiltIns.isInt(it.valueParameters.single().type)
|
||||
KotlinBuiltIns.isInt(it.valueParameters.single().type) && it.isInline
|
||||
}
|
||||
|
||||
val arrayGetByLongIndex = packageScope.getContributedFunctions("get").single {
|
||||
KotlinBuiltIns.isLong(it.valueParameters.single().type)
|
||||
KotlinBuiltIns.isLong(it.valueParameters.single().type) && it.isInline
|
||||
}
|
||||
|
||||
val allocUninitializedArrayWithIntLength = packageScope.getContributedFunctions("allocArray").single {
|
||||
|
||||
+5
-5
@@ -40,7 +40,7 @@ internal fun globalHash(data: ByteArray, retValPlacement: NativePlacement): Glob
|
||||
public fun base64Encode(data: ByteArray): String {
|
||||
memScoped {
|
||||
val resultSize = 4 * data.size / 3 + 3 + 1
|
||||
val result = allocArray<CInt8Var>(resultSize)
|
||||
val result = allocArray<ByteVar>(resultSize)
|
||||
val bytes = allocArrayOf(data)
|
||||
EncodeBase64(bytes, data.size, result, resultSize)
|
||||
// TODO: any better way to do that without two copies?
|
||||
@@ -51,14 +51,14 @@ public fun base64Encode(data: ByteArray): String {
|
||||
public fun base64Decode(encoded: String): ByteArray {
|
||||
memScoped {
|
||||
val bufferSize: Int = 3 * encoded.length / 4
|
||||
val result = allocArray<CInt8Var>(bufferSize)
|
||||
val result = allocArray<ByteVar>(bufferSize)
|
||||
val resultSize = allocArray<uint32_tVar>(1)
|
||||
resultSize[0].value = bufferSize
|
||||
resultSize[0] = bufferSize
|
||||
val errorCode = DecodeBase64(encoded, encoded.length, result, resultSize)
|
||||
if (errorCode != 0) throw Error("Non-zero exit code of DecodeBase64: ${errorCode}")
|
||||
val realSize = resultSize[0].value!!
|
||||
val realSize = resultSize[0]
|
||||
val bytes = ByteArray(realSize)
|
||||
nativeMemUtils.getByteArray(result[0], bytes, realSize)
|
||||
nativeMemUtils.getByteArray(result.pointed, bytes, realSize)
|
||||
return bytes
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -92,7 +92,7 @@ internal fun emitLLVM(context: Context) {
|
||||
|
||||
internal fun verifyModule(llvmModule: LLVMModuleRef, current: String = "") {
|
||||
memScoped {
|
||||
val errorRef = allocPointerTo<CInt8Var>()
|
||||
val errorRef = allocPointerTo<ByteVar>()
|
||||
// TODO: use LLVMDisposeMessage() on errorRef, once possible in interop.
|
||||
if (LLVMVerifyModule(
|
||||
llvmModule, LLVMVerifierFailureAction.LLVMPrintMessageAction, errorRef.ptr) == 1) {
|
||||
|
||||
+2
-2
@@ -171,7 +171,7 @@ internal fun RuntimeAware.isObjectType(type: LLVMTypeRef): Boolean {
|
||||
/**
|
||||
* Reads [size] bytes contained in this array.
|
||||
*/
|
||||
internal fun CArrayPointer<CInt8Var>.getBytes(size: Long) =
|
||||
internal fun CArrayPointer<ByteVar>.getBytes(size: Long) =
|
||||
(0 .. size-1).map { this[it].value }.toByteArray()
|
||||
|
||||
internal fun getFunctionType(ptrToFunction: LLVMValueRef): LLVMTypeRef {
|
||||
@@ -220,7 +220,7 @@ fun getStructElements(type: LLVMTypeRef): List<LLVMTypeRef> {
|
||||
|
||||
fun parseBitcodeFile(path: String): LLVMModuleRef = memScoped {
|
||||
val bufRef = alloc<LLVMMemoryBufferRefVar>()
|
||||
val errorRef = allocPointerTo<CInt8Var>()
|
||||
val errorRef = allocPointerTo<ByteVar>()
|
||||
|
||||
val res = LLVMCreateMemoryBufferWithContentsOfFile(path, bufRef.ptr, errorRef.ptr)
|
||||
if (res != 0) {
|
||||
|
||||
+5
-5
@@ -83,7 +83,7 @@ class MetadataReader(file: File) : Closeable {
|
||||
init {
|
||||
memScoped {
|
||||
val bufRef = alloc<LLVMMemoryBufferRefVar>()
|
||||
val errorRef = allocPointerTo<CInt8Var>()
|
||||
val errorRef = allocPointerTo<ByteVar>()
|
||||
val res = LLVMCreateMemoryBufferWithContentsOfFile(file.toString(), bufRef.ptr, errorRef.ptr)
|
||||
if (res != 0) {
|
||||
throw Error(errorRef.value?.toKString())
|
||||
@@ -103,7 +103,7 @@ class MetadataReader(file: File) : Closeable {
|
||||
|
||||
fun string(node: LLVMValueRef): String {
|
||||
memScoped {
|
||||
val len = alloc<CInt32Var>()
|
||||
val len = alloc<IntVar>()
|
||||
val str1 = LLVMGetMDString(node, len.ptr)!!
|
||||
val str = str1.toKString()
|
||||
return str
|
||||
@@ -121,7 +121,7 @@ class MetadataReader(file: File) : Closeable {
|
||||
|
||||
//return Pair(nodeCount, nodeArray[0].value!!)
|
||||
for (index in 0..nodeCount-1) {
|
||||
result.add(nodeArray[index].value!!)
|
||||
result.add(nodeArray[index]!!)
|
||||
}
|
||||
}
|
||||
return result.toTypedArray<LLVMValueRef>()
|
||||
@@ -134,7 +134,7 @@ class MetadataReader(file: File) : Closeable {
|
||||
|
||||
LLVMGetNamedMetadataOperands(llvmModule, name, nodeArray)
|
||||
|
||||
return Pair(nodeCount, nodeArray[0].value!!)
|
||||
return Pair(nodeCount, nodeArray[0]!!)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -145,7 +145,7 @@ class MetadataReader(file: File) : Closeable {
|
||||
|
||||
LLVMGetMDNodeOperands(metadataNode, operandArray)
|
||||
|
||||
return Array(operandCount, {index -> operandArray[index].value!!})
|
||||
return Array(operandCount, {index -> operandArray[index]!!})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,5 +5,5 @@ fun main(args: Array<String>) {
|
||||
val statBuf = nativeHeap.alloc<statStruct>()
|
||||
val res = stat("/", statBuf.ptr)
|
||||
println(res)
|
||||
println(statBuf.st_uid.value)
|
||||
println(statBuf.st_uid)
|
||||
}
|
||||
@@ -4,17 +4,17 @@ fun main(args: Array<String>) {
|
||||
memScoped {
|
||||
val count = 5
|
||||
|
||||
val values = allocArray<CInt32Var>(count)
|
||||
values[0].value = 14
|
||||
values[1].value = 12
|
||||
values[2].value = 9
|
||||
values[3].value = 13
|
||||
values[4].value = 8
|
||||
val values = allocArray<IntVar>(count)
|
||||
values[0] = 14
|
||||
values[1] = 12
|
||||
values[2] = 9
|
||||
values[3] = 13
|
||||
values[4] = 8
|
||||
|
||||
cstdlib.qsort(values, count.toLong(), CInt32Var.size, staticCFunction(::comparator))
|
||||
cstdlib.qsort(values, count.toLong(), IntVar.size, staticCFunction(::comparator))
|
||||
|
||||
for (i in 0 .. count - 1) {
|
||||
print(values[i].value)
|
||||
print(values[i])
|
||||
print(" ")
|
||||
}
|
||||
println()
|
||||
@@ -22,8 +22,8 @@ fun main(args: Array<String>) {
|
||||
}
|
||||
|
||||
private fun comparator(a: COpaquePointer?, b: COpaquePointer?): Int {
|
||||
val aValue = a!!.reinterpret<CInt32Var>().pointed.value
|
||||
val bValue = b!!.reinterpret<CInt32Var>().pointed.value
|
||||
val aValue = a!!.reinterpret<IntVar>()[0]
|
||||
val bValue = b!!.reinterpret<IntVar>()[0]
|
||||
|
||||
return (aValue - bValue)
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ fun main(args: Array<String>) {
|
||||
"a", "b".cstr, (-1).toByte(), 2.toShort(), 3, Long.MAX_VALUE, 0.1.toFloat(), 0.2)
|
||||
|
||||
memScoped {
|
||||
val aVar = alloc<CInt32Var>()
|
||||
val bVar = alloc<CInt32Var>()
|
||||
val aVar = alloc<IntVar>()
|
||||
val bVar = alloc<IntVar>()
|
||||
val sscanfResult = sscanf("42", "%d%d", aVar.ptr, bVar.ptr)
|
||||
printf("%d %d\n", sscanfResult, aVar.value)
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ fun main(args: Array<String>) {
|
||||
memScoped {
|
||||
|
||||
val bufferLength = 100L
|
||||
val buffer = allocArray<CInt8Var>(bufferLength)
|
||||
val buffer = allocArray<ByteVar>(bufferLength)
|
||||
val serverAddr = alloc<sockaddr_in>()
|
||||
|
||||
val listenFd = socket(AF_INET, SOCK_STREAM, 0)
|
||||
@@ -20,9 +20,9 @@ fun main(args: Array<String>) {
|
||||
|
||||
with(serverAddr) {
|
||||
memset(this.ptr, 0, sockaddr_in.size)
|
||||
sin_family.value = AF_INET.narrow()
|
||||
sin_addr.s_addr.value = htons(0).toInt()
|
||||
sin_port.value = htons(port)
|
||||
sin_family = AF_INET.narrow()
|
||||
sin_addr.s_addr = htons(0).toInt()
|
||||
sin_port = htons(port)
|
||||
}
|
||||
|
||||
bind(listenFd, serverAddr.ptr.reinterpret(), sockaddr_in.size.toInt())
|
||||
|
||||
@@ -83,7 +83,7 @@ fun initialize() {
|
||||
fun main(args: Array<String>) {
|
||||
// initialize and run program
|
||||
memScoped {
|
||||
val argc = alloc<CInt32Var>().apply { value = 0 }
|
||||
val argc = alloc<IntVar>().apply { value = 0 }
|
||||
glutInit(argc.ptr, null) // TODO: pass real args
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user