HEAD UPS! Global move: experiments -> kotlin-native

This commit is contained in:
Vasily Levchenko
2016-09-28 13:22:36 +03:00
parent 541f9ac1e7
commit 0eaaa58ed9
49 changed files with 1 additions and 1 deletions
+31
View File
@@ -0,0 +1,31 @@
buildscript {
ext.kotlin_version = '1.0.3'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'kotlin'
apply plugin: org.jetbrains.kotlin.NativeInteropPlugin
apply plugin: 'application'
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
genInteropStubs {
// these variables will be passed to native toolchain used by stub generator:
environment 'CPATH' : "$llvmInstallPath/include"
environment 'LIBRARY_PATH' : "$llvmInstallPath/lib"
}
mainClassName = 'MainKt'
@@ -0,0 +1,29 @@
libName = llvmbridge
headers = llvm-c/Core.h llvm-c/ExecutionEngine.h llvm-c/Target.h llvm-c/Analysis.h llvm-c/BitWriter.h
compiler = clang
compilerOpts = -std=c99 -fPIC \
-Wall -W -Wno-unused-parameter -Wwrite-strings -Wmissing-field-initializers \
-pedantic -Wno-long-long -Wcovered-switch-default -Wdelete-non-virtual-dtor \
-DNDEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
linker = clang++
linkerOpts = -stdlib=libc++ -fPIC -fvisibility-inlines-hidden \
-Wall -W -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers \
-pedantic -Wno-long-long -Wcovered-switch-default -Wnon-virtual-dtor -Wdelete-non-virtual-dtor \
-std=c++11 \
-DNDEBUG -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS \
-Wl,-search_paths_first -Wl,-headerpad_max_install_names \
-lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter \
-lLLVMX86Desc -lLLVMMCDisassembler -lLLVMX86Info -lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMInterpreter \
-lLLVMCodeGen -lLLVMScalarOpts -lLLVMInstCombine -lLLVMInstrumentation -lLLVMProfileData -lLLVMTransformUtils \
-lLLVMBitWriter -lLLVMExecutionEngine -lLLVMTarget -lLLVMAnalysis -lLLVMRuntimeDyld -lLLVMObject -lLLVMMCParser \
-lLLVMBitReader -lLLVMMC -lLLVMCore -lLLVMSupport -lcurses -lpthread -lz -lm \
-lffi
excludedFunctions =
+48
View File
@@ -0,0 +1,48 @@
import kotlin_native.interop.*
import llvm.*
fun main(args: Array<String>) {
val module = LLVMModuleCreateWithName("module")
println("module=" + module.getNativePtr().asLong())
val paramTypes = mallocNativeArrayOf(LLVMOpaqueType, LLVMInt32Type(), LLVMInt32Type())
val retType = LLVMFunctionType(LLVMInt32Type(), paramTypes[0], 2, 0)
free(paramTypes)
val sum = LLVMAddFunction(module, "sum", retType)
val entry = LLVMAppendBasicBlock(sum, "entry")
val builder = LLVMCreateBuilder()
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)
LLVMInitializeNativeTarget()
errorRef.value = null
if (LLVMCreateExecutionEngineForModule(engineRef, module, errorRef) != 0) {
println("failed to create execution engine")
return
}
val error = errorRef.value
if (error != null) {
println(CString.fromArray(NativeArray.byRefToFirstElem(error, Int8Box)).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])
println(LLVMGenericValueToInt(runRes, 0))
if (LLVMWriteBitcodeToFile(module, "/tmp/sum.bc") != 0) {
println("error writing bitcode to file, skipping")
}
LLVMDisposeBuilder(builder)
LLVMDisposeExecutionEngine(engineRef.value)
}