1 /*===-- executionengine_ocaml.c - LLVM OCaml Glue ---------------*- C++ -*-===*\
3 |* The LLVM Compiler Infrastructure *|
5 |* This file is distributed under the University of Illinois Open Source *|
6 |* License. See LICENSE.TXT for details. *|
8 |*===----------------------------------------------------------------------===*|
10 |* This file glues LLVM's OCaml interface to its C interface. These functions *|
11 |* are by and large transparent wrappers to the corresponding C functions. *|
13 |* Note that these functions intentionally take liberties with the CAMLparamX *|
14 |* macros, since most of the parameters are not GC heap objects. *|
16 \*===----------------------------------------------------------------------===*/
20 #include "llvm-c/ExecutionEngine.h"
21 #include "llvm-c/Target.h"
22 #include "caml/alloc.h"
23 #include "caml/custom.h"
24 #include "caml/fail.h"
25 #include "caml/memory.h"
26 #include "caml/callback.h"
28 void llvm_raise(value Prototype, char *Message);
31 CAMLprim value llvm_ee_initialize(value Unit) {
34 return Val_bool(!LLVMInitializeNativeTarget() &&
35 !LLVMInitializeNativeAsmParser() &&
36 !LLVMInitializeNativeAsmPrinter());
39 /* llmodule -> llcompileroption -> ExecutionEngine.t */
40 CAMLprim LLVMExecutionEngineRef llvm_ee_create(value OptRecordOpt, LLVMModuleRef M) {
42 LLVMExecutionEngineRef MCJIT;
44 struct LLVMMCJITCompilerOptions Options;
46 LLVMInitializeMCJITCompilerOptions(&Options, sizeof(Options));
47 if (OptRecordOpt != Val_int(0)) {
48 OptRecord = Field(OptRecordOpt, 0);
49 Options.OptLevel = Int_val(Field(OptRecord, 0));
50 Options.CodeModel = Int_val(Field(OptRecord, 1));
51 Options.NoFramePointerElim = Int_val(Field(OptRecord, 2));
52 Options.EnableFastISel = Int_val(Field(OptRecord, 3));
56 if (LLVMCreateMCJITCompilerForModule(&MCJIT, M, &Options,
57 sizeof(Options), &Error))
58 llvm_raise(*caml_named_value("Llvm_executionengine.Error"), Error);
62 /* ExecutionEngine.t -> unit */
63 CAMLprim value llvm_ee_dispose(LLVMExecutionEngineRef EE) {
64 LLVMDisposeExecutionEngine(EE);
68 /* llmodule -> ExecutionEngine.t -> unit */
69 CAMLprim value llvm_ee_add_module(LLVMModuleRef M, LLVMExecutionEngineRef EE) {
74 /* llmodule -> ExecutionEngine.t -> llmodule */
75 CAMLprim value llvm_ee_remove_module(LLVMModuleRef M, LLVMExecutionEngineRef EE) {
76 LLVMModuleRef RemovedModule;
78 if (LLVMRemoveModule(EE, M, &RemovedModule, &Error))
79 llvm_raise(*caml_named_value("Llvm_executionengine.Error"), Error);
83 /* ExecutionEngine.t -> unit */
84 CAMLprim value llvm_ee_run_static_ctors(LLVMExecutionEngineRef EE) {
85 LLVMRunStaticConstructors(EE);
89 /* ExecutionEngine.t -> unit */
90 CAMLprim value llvm_ee_run_static_dtors(LLVMExecutionEngineRef EE) {
91 LLVMRunStaticDestructors(EE);
95 extern value llvm_alloc_data_layout(LLVMTargetDataRef TargetData);
97 /* ExecutionEngine.t -> Llvm_target.DataLayout.t */
98 CAMLprim value llvm_ee_get_data_layout(LLVMExecutionEngineRef EE) {
100 LLVMTargetDataRef OrigDataLayout;
101 char* TargetDataCStr;
103 OrigDataLayout = LLVMGetExecutionEngineTargetData(EE);
104 TargetDataCStr = LLVMCopyStringRepOfTargetData(OrigDataLayout);
105 DataLayout = llvm_alloc_data_layout(LLVMCreateTargetData(TargetDataCStr));
106 LLVMDisposeMessage(TargetDataCStr);
111 /* Llvm.llvalue -> int64 -> llexecutionengine -> unit */
112 CAMLprim value llvm_ee_add_global_mapping(LLVMValueRef Global, value Ptr,
113 LLVMExecutionEngineRef EE) {
114 LLVMAddGlobalMapping(EE, Global, (void*) (Int64_val(Ptr)));
118 CAMLprim value llvm_ee_get_global_value_address(value Name,
119 LLVMExecutionEngineRef EE) {
120 return caml_copy_int64((int64_t) LLVMGetGlobalValueAddress(EE, String_val(Name)));
123 CAMLprim value llvm_ee_get_function_address(value Name,
124 LLVMExecutionEngineRef EE) {
125 return caml_copy_int64((int64_t) LLVMGetFunctionAddress(EE, String_val(Name)));