From 78a912f63705ffdf36e817fedbabd3801ca5137b Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Thu, 29 Sep 2016 17:28:36 +0700 Subject: [PATCH] backend.native: tie compiler to runtime to make compiler able to refer entities declared in runtime --- backend.native/build.gradle | 7 +++ .../cli/bc/K2NativeCompilerArguments.java | 4 ++ .../kotlin/backend/native/llvm/Runtime.kt | 43 +++++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/Runtime.kt diff --git a/backend.native/build.gradle b/backend.native/build.gradle index a3d8ae6c384..a8567d74143 100644 --- a/backend.native/build.gradle +++ b/backend.native/build.gradle @@ -65,6 +65,13 @@ task run(type: JavaExec) { main 'org.jetbrains.kotlin.cli.bc.K2NativeKt' classpath = sourceSets.main.runtimeClasspath.add(files(['build/classes/main'])) args '../experiments/translator/src/test/kotlin/tests/classfields_1/classfields_1.kt' + + dependsOn ':runtime:build' + + doFirst { + args '-runtime', project(':runtime').build.outputs.files.singleFile + println(args) + } } task jars { diff --git a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.java b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.java index 7741daa2fcf..e5babda3546 100644 --- a/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.java +++ b/backend.native/cli.bc/src/org/jetbrains/kotlin/cli/bc/K2NativeCompilerArguments.java @@ -10,4 +10,8 @@ public class K2NativeCompilerArguments extends CommonCompilerArguments { @ValueDescription("") public String outputFile; + @Argument(value = "runtime", description = "Runtime file path") + @ValueDescription("") + public String runtimeFile; + } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/Runtime.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/Runtime.kt new file mode 100644 index 00000000000..359a6945efc --- /dev/null +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/native/llvm/Runtime.kt @@ -0,0 +1,43 @@ +package org.jetbrains.kotlin.backend.native.llvm + +import kotlin_native.interop.* +import llvm.* + +class Runtime(private val bitcodeFile: String) { + val llvmModule: LLVMOpaqueModule + + init { + val arena = Arena() + try { + + val bufRef = arena.alloc(LLVMOpaqueMemoryBuffer.ref) + val errorRef = arena.alloc(Int8Box.ref) + val res = LLVMCreateMemoryBufferWithContentsOfFile(bitcodeFile, bufRef, errorRef) + if (res != 0) { + throw Error(errorRef.value?.asCString()?.toString()) + } + + val moduleRef = arena.alloc(LLVMOpaqueModule.ref) + val parseRes = LLVMParseBitcode2(bufRef.value, moduleRef) + if (parseRes != 0) { + throw Error(parseRes.toString()) + } + + llvmModule = moduleRef.value!! + + } finally { + arena.clear() + } + } + + val typeInfoType = LLVMGetTypeByName(llvmModule, "struct.TypeInfo") + val fieldTableRecordType = LLVMGetTypeByName(llvmModule, "struct.FieldTableRecord") + val methodTableRecordType = LLVMGetTypeByName(llvmModule, "struct.MethodTableRecord") + + val target = LLVMGetTarget(llvmModule)!!.asCString().toString() + + val dataLayout = LLVMGetDataLayout(llvmModule)!!.asCString().toString() + + val targetData = LLVMCreateTargetData(dataLayout) + +} \ No newline at end of file