From c5f00c3301dd0e2b407e5ab91eb4c655fd82087c Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Tue, 22 Nov 2016 13:18:49 +0700 Subject: [PATCH] InteropExample: port to new interop --- InteropExample/src/main/kotlin/main.kt | 31 ++++++++++++-------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/InteropExample/src/main/kotlin/main.kt b/InteropExample/src/main/kotlin/main.kt index 9a0c14a55e3..a617122187a 100644 --- a/InteropExample/src/main/kotlin/main.kt +++ b/InteropExample/src/main/kotlin/main.kt @@ -1,13 +1,12 @@ -import kotlin_native.interop.* +import kotlin_.cinterop.* import llvm.* -fun main(args: Array) { +fun main(args: Array) = memScoped { val module = LLVMModuleCreateWithName("module") - println("module=" + module.getNativePtr().asLong()) + println("module=" + module.rawValue) - val paramTypes = mallocNativeArrayOf(LLVMOpaqueType, LLVMInt32Type(), LLVMInt32Type()) - val retType = LLVMFunctionType(LLVMInt32Type(), paramTypes[0], 2, 0) - free(paramTypes) + val paramTypes = allocArrayOf(LLVMInt32Type(), LLVMInt32Type()) + val retType = LLVMFunctionType(LLVMInt32Type(), paramTypes[0].ptr, 2, 0) val sum = LLVMAddFunction(module, "sum", retType) val entry = LLVMAppendBasicBlock(sum, "entry") @@ -15,34 +14,32 @@ fun main(args: Array) { LLVMPositionBuilderAtEnd(builder, entry) val tmp = LLVMBuildAdd(builder, LLVMGetParam(sum, 0), LLVMGetParam(sum, 1), "tmp") LLVMBuildRet(builder, tmp) - val engineRef = malloc(Ref to LLVMOpaqueExecutionEngine) - val errorRef = malloc(Ref to Int8Box) + val engineRef = alloc() + val errorRef = allocPointerTo() LLVMInitializeNativeTarget() errorRef.value = null - if (LLVMCreateExecutionEngineForModule(engineRef, module, errorRef) != 0) { + if (LLVMCreateExecutionEngineForModule(engineRef.ptr, module, errorRef.ptr) != 0) { println("failed to create execution engine") return } val error = errorRef.value if (error != null) { - println(CString.fromArray(NativeArray.byRefToFirstElem(error, Int8Box)).toString()) + println(error.asCString().toString()) return } println(LLVMGetTypeKind(LLVMInt32Type())) val x = 5L val y = 6L - val args = malloc(array[2](Ref to LLVMOpaqueGenericValue)) - args[0].value = LLVMCreateGenericValueOfInt(LLVMInt32Type(), x, 0) - args[1].value = LLVMCreateGenericValueOfInt(LLVMInt32Type(), y, 0) - val runRes = LLVMRunFunction(engineRef.value, sum, 2, args[0]) + val args = allocArrayOf( + LLVMCreateGenericValueOfInt(LLVMInt32Type(), x, 0), + LLVMCreateGenericValueOfInt(LLVMInt32Type(), y, 0)) + + val runRes = LLVMRunFunction(engineRef.value, sum, 2, args[0].ptr) println(LLVMGenericValueToInt(runRes, 0)) if (LLVMWriteBitcodeToFile(module, "/tmp/sum.bc") != 0) { println("error writing bitcode to file, skipping") } LLVMDisposeBuilder(builder) LLVMDisposeExecutionEngine(engineRef.value) - - - } \ No newline at end of file