committed by
Nikolay Igotti
parent
e36b5d3476
commit
c1d9dc1cbb
+5
-5
@@ -7,7 +7,7 @@ internal fun localHash(data: ByteArray): Long {
|
||||
memScoped {
|
||||
val res = alloc<LocalHashVar>()
|
||||
val bytes = allocArrayOf(data)
|
||||
MakeLocalHash(bytes[0].ptr, data.size, res.ptr)
|
||||
MakeLocalHash(bytes, data.size, res.ptr)
|
||||
return res.value
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ internal fun globalHash(data: ByteArray, retValPlacement: NativePlacement): Glob
|
||||
val res = retValPlacement.alloc<GlobalHash>()
|
||||
memScoped {
|
||||
val bytes = allocArrayOf(data)
|
||||
MakeGlobalHash(bytes[0].ptr, data.size, res.ptr)
|
||||
MakeGlobalHash(bytes, data.size, res.ptr)
|
||||
}
|
||||
return res
|
||||
}
|
||||
@@ -26,9 +26,9 @@ public fun base64Encode(data: ByteArray): String {
|
||||
val resultSize = 4 * data.size / 3 + 3 + 1
|
||||
val result = allocArray<CInt8Var>(resultSize)
|
||||
val bytes = allocArrayOf(data)
|
||||
EncodeBase64(bytes.ptr, data.size, result.ptr, resultSize)
|
||||
EncodeBase64(bytes, data.size, result, resultSize)
|
||||
// TODO: any better way to do that without two copies?
|
||||
return result[0].ptr.toKString()
|
||||
return result.toKString()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +38,7 @@ public fun base64Decode(encoded: String): ByteArray {
|
||||
val result = allocArray<CInt8Var>(bufferSize)
|
||||
val resultSize = allocArray<uint32_tVar>(1)
|
||||
resultSize[0].value = bufferSize
|
||||
val errorCode = DecodeBase64(encoded, encoded.length, result[0].ptr, resultSize[0].ptr)
|
||||
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 bytes = ByteArray(realSize)
|
||||
|
||||
+1
-1
@@ -155,7 +155,7 @@ internal fun RuntimeAware.isObjectType(type: LLVMTypeRef): Boolean {
|
||||
/**
|
||||
* Reads [size] bytes contained in this array.
|
||||
*/
|
||||
internal fun CArray<CInt8Var>.getBytes(size: Long) =
|
||||
internal fun CArrayPointer<CInt8Var>.getBytes(size: Long) =
|
||||
(0 .. size-1).map { this[it].value }.toByteArray()
|
||||
|
||||
internal fun getFunctionType(ptrToFunction: LLVMValueRef): LLVMTypeRef {
|
||||
|
||||
+3
-3
@@ -90,7 +90,7 @@ class MetadataReader(file: File) : Closeable {
|
||||
val nodeCount = LLVMGetNamedMetadataNumOperands(llvmModule, name)
|
||||
val nodeArray = allocArray<LLVMValueRefVar>(nodeCount)
|
||||
|
||||
LLVMGetNamedMetadataOperands(llvmModule, name, nodeArray[0].ptr)
|
||||
LLVMGetNamedMetadataOperands(llvmModule, name, nodeArray)
|
||||
|
||||
//return Pair(nodeCount, nodeArray[0].value!!)
|
||||
for (index in 0..nodeCount-1) {
|
||||
@@ -105,7 +105,7 @@ class MetadataReader(file: File) : Closeable {
|
||||
val nodeCount = LLVMGetNamedMetadataNumOperands(llvmModule, name)
|
||||
val nodeArray = allocArray<LLVMValueRefVar>(nodeCount)
|
||||
|
||||
LLVMGetNamedMetadataOperands(llvmModule, name, nodeArray[0].ptr)
|
||||
LLVMGetNamedMetadataOperands(llvmModule, name, nodeArray)
|
||||
|
||||
return Pair(nodeCount, nodeArray[0].value!!)
|
||||
}
|
||||
@@ -116,7 +116,7 @@ class MetadataReader(file: File) : Closeable {
|
||||
val operandCount = LLVMGetMDNodeNumOperands(metadataNode)
|
||||
val operandArray = allocArray<LLVMValueRefVar>(operandCount)
|
||||
|
||||
LLVMGetMDNodeOperands(metadataNode, operandArray[0].ptr)
|
||||
LLVMGetMDNodeOperands(metadataNode, operandArray)
|
||||
|
||||
return Array(operandCount, {index -> operandArray[index].value!!})
|
||||
}
|
||||
|
||||
+24
-7
@@ -90,15 +90,23 @@ private class InteropTransformer(val context: Context, val irFile: IrFile) : IrB
|
||||
return alignOf(typeObject)
|
||||
}
|
||||
|
||||
private fun IrBuilderWithScope.interpretPointed(expression: IrCall, type: KotlinType): IrExpression =
|
||||
irCall(interop.interpretNullablePointed).apply {
|
||||
putValueArgument(0, expression)
|
||||
}
|
||||
// TODO: make the result type more correct.
|
||||
|
||||
private fun IrBuilderWithScope.arrayGet(array: IrExpression, index: IrExpression): IrExpression? {
|
||||
val elementSize = sizeOf(array.type.arguments.single().type) ?: return null
|
||||
val elementType = array.type.arguments.single().type
|
||||
val elementSize = sizeOf(elementType) ?: return null
|
||||
|
||||
val offset = times(elementSize, index)
|
||||
|
||||
return irCall(interop.memberAt).apply {
|
||||
extensionReceiver = array
|
||||
putValueArgument(0, offset)
|
||||
val resultRawPtr = irCall(interop.nativePtrPlusLong).apply {
|
||||
dispatchReceiver = irCall(interop.cPointerGetRawValue).apply {
|
||||
extensionReceiver = array
|
||||
}
|
||||
putValueArgument(0, times(elementSize, index))
|
||||
}
|
||||
return interpretPointed(resultRawPtr, elementType)
|
||||
}
|
||||
|
||||
private fun IrBuilderWithScope.times(left: IrExpression, right: IrExpression): IrCall {
|
||||
@@ -125,6 +133,15 @@ private class InteropTransformer(val context: Context, val irFile: IrFile) : IrB
|
||||
}
|
||||
}
|
||||
|
||||
private fun IrBuilderWithScope.nativePointedToCPointer(
|
||||
expression: IrExpression, pointeeType: KotlinType
|
||||
): IrExpression = irCall(interop.interpretCPointer).apply {
|
||||
putValueArgument(0, irCall(interop.nativePointedGetRawPointer).apply {
|
||||
extensionReceiver = expression
|
||||
})
|
||||
}
|
||||
// TODO: make the result type more correct.
|
||||
|
||||
private fun IrBuilderWithScope.allocArray(placement: IrExpression,
|
||||
elementType: KotlinType,
|
||||
length: IrExpression
|
||||
@@ -134,7 +151,7 @@ private class InteropTransformer(val context: Context, val irFile: IrFile) : IrB
|
||||
val size = times(elementSize, length)
|
||||
val align = alignOf(elementType) ?: return null
|
||||
|
||||
return alloc(placement, size, align)
|
||||
return nativePointedToCPointer(alloc(placement, size, align), elementType)
|
||||
}
|
||||
|
||||
private fun IrBuilderWithScope.alloc(placement: IrExpression, type: KotlinType): IrExpression? {
|
||||
|
||||
@@ -11,7 +11,7 @@ fun main(args: Array<String>) {
|
||||
values[3].value = 13
|
||||
values[4].value = 8
|
||||
|
||||
cstdlib.qsort(values[0].ptr, count.toLong(), CInt32Var.size, staticCFunction(::comparator))
|
||||
cstdlib.qsort(values, count.toLong(), CInt32Var.size, staticCFunction(::comparator))
|
||||
|
||||
for (i in 0 .. count - 1) {
|
||||
print(values[i].value)
|
||||
|
||||
@@ -35,14 +35,14 @@ fun main(args: Array<String>) {
|
||||
.ensureUnixCallResult { it >= 0 }
|
||||
|
||||
while (true) {
|
||||
val length = read(commFd, buffer.ptr, bufferLength)
|
||||
val length = read(commFd, buffer, bufferLength)
|
||||
.ensureUnixCallResult { it >= 0 }
|
||||
|
||||
if (length == 0L) {
|
||||
break
|
||||
}
|
||||
|
||||
write(commFd, buffer.ptr, length)
|
||||
write(commFd, buffer, length)
|
||||
.ensureUnixCallResult { it >= 0 }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user