Fix the ocaml bindings.
[oota-llvm.git] / bindings / ocaml / executionengine / executionengine_ocaml.c
index 647759fb074424e85c95f934bbe41a0784519823..4896c74121bb5b660447f4580510cfe51251d067 100644 (file)
@@ -1,4 +1,4 @@
-/*===-- executionengine_ocaml.c - LLVM Ocaml Glue ---------------*- C++ -*-===*\
+/*===-- executionengine_ocaml.c - LLVM OCaml Glue ---------------*- C++ -*-===*\
 |*                                                                            *|
 |*                     The LLVM Compiler Infrastructure                       *|
 |*                                                                            *|
@@ -7,7 +7,7 @@
 |*                                                                            *|
 |*===----------------------------------------------------------------------===*|
 |*                                                                            *|
-|* This file glues LLVM's ocaml interface to its C interface. These functions *|
+|* This file glues LLVM's OCaml interface to its C interface. These functions *|
 |* are by and large transparent wrappers to the corresponding C functions.    *|
 |*                                                                            *|
 |* Note that these functions intentionally take liberties with the CAMLparamX *|
 #include <string.h>
 #include <assert.h>
 
-/* Force the LLVM interpreter, JIT, and native target to be linked in. */
+/* Force the LLVM interpreter and JIT to be linked in. */
 void llvm_initialize(void) {
   LLVMLinkInInterpreter();
-  LLVMLinkInJIT();
-  LLVMInitializeNativeTarget();
+  LLVMLinkInMCJIT();
+}
+
+/* unit -> bool */
+CAMLprim value llvm_initialize_native_target(value Unit) {
+  return Val_bool(LLVMInitializeNativeTarget());
 }
 
 /* Can't use the recommended caml_named_value mechanism for backwards
@@ -71,6 +75,9 @@ static struct custom_operations generic_value_ops = {
   custom_hash_default,
   custom_serialize_default,
   custom_deserialize_default
+#ifdef custom_compare_ext_default
+  , custom_compare_ext_default
+#endif
 };
 
 static value alloc_generic_value(LLVMGenericValueRef Ref) {
@@ -87,7 +94,7 @@ CAMLprim value llvm_genericvalue_of_float(LLVMTypeRef Ty, value N) {
 }
 
 /* 'a -> t */
-CAMLprim value llvm_genericvalue_of_value(value V) {
+CAMLprim value llvm_genericvalue_of_pointer(value V) {
   CAMLparam1(V);
   CAMLreturn(alloc_generic_value(LLVMCreateGenericValueOfPointer(Op_val(V))));
 }
@@ -126,7 +133,7 @@ CAMLprim value llvm_genericvalue_as_float(LLVMTypeRef Ty, value GenVal) {
 }
 
 /* t -> 'a */
-CAMLprim value llvm_genericvalue_as_value(value GenVal) {
+CAMLprim value llvm_genericvalue_as_pointer(value GenVal) {
   return Val_op(LLVMGenericValueToPointer(Genericvalue_val(GenVal)));
 }
 
@@ -164,41 +171,31 @@ CAMLprim value llvm_genericvalue_as_nativeint(value GenVal) {
 
 /*--... Operations on execution engines ....................................--*/
 
-/* llmoduleprovider -> ExecutionEngine.t */
-CAMLprim LLVMExecutionEngineRef llvm_ee_create(LLVMModuleProviderRef MP) {
+/* llmodule -> ExecutionEngine.t */
+CAMLprim LLVMExecutionEngineRef llvm_ee_create(LLVMModuleRef M) {
   LLVMExecutionEngineRef Interp;
   char *Error;
-  if (LLVMCreateExecutionEngine(&Interp, MP, &Error))
+  if (LLVMCreateExecutionEngineForModule(&Interp, M, &Error))
     llvm_raise(llvm_ee_error_exn, Error);
   return Interp;
 }
 
-/* llmoduleprovider -> ExecutionEngine.t */
+/* llmodule -> ExecutionEngine.t */
 CAMLprim LLVMExecutionEngineRef
-llvm_ee_create_interpreter(LLVMModuleProviderRef MP) {
+llvm_ee_create_interpreter(LLVMModuleRef M) {
   LLVMExecutionEngineRef Interp;
   char *Error;
-  if (LLVMCreateInterpreter(&Interp, MP, &Error))
+  if (LLVMCreateInterpreterForModule(&Interp, M, &Error))
     llvm_raise(llvm_ee_error_exn, Error);
   return Interp;
 }
 
-/* llmoduleprovider -> ExecutionEngine.t */
+/* llmodule -> int -> ExecutionEngine.t */
 CAMLprim LLVMExecutionEngineRef
-llvm_ee_create_jit(LLVMModuleProviderRef MP) {
+llvm_ee_create_jit(LLVMModuleRef M, value OptLevel) {
   LLVMExecutionEngineRef JIT;
   char *Error;
-  if (LLVMCreateJITCompiler(&JIT, MP, 3, &Error))
-    llvm_raise(llvm_ee_error_exn, Error);
-  return JIT;
-}
-
-/* llmoduleprovider -> ExecutionEngine.t */
-CAMLprim LLVMExecutionEngineRef
-llvm_ee_create_fast_jit(LLVMModuleProviderRef MP) {
-  LLVMExecutionEngineRef JIT;
-  char *Error;
-  if (LLVMCreateJITCompiler(&JIT, MP, 0, &Error))
+  if (LLVMCreateJITCompilerForModule(&JIT, M, Int_val(OptLevel), &Error))
     llvm_raise(llvm_ee_error_exn, Error);
   return JIT;
 }
@@ -209,19 +206,18 @@ CAMLprim value llvm_ee_dispose(LLVMExecutionEngineRef EE) {
   return Val_unit;
 }
 
-/* llmoduleprovider -> ExecutionEngine.t -> unit */
-CAMLprim value llvm_ee_add_mp(LLVMModuleProviderRef MP,
-                              LLVMExecutionEngineRef EE) {
-  LLVMAddModuleProvider(EE, MP);
+/* llmodule -> ExecutionEngine.t -> unit */
+CAMLprim value llvm_ee_add_module(LLVMModuleRef M, LLVMExecutionEngineRef EE) {
+  LLVMAddModule(EE, M);
   return Val_unit;
 }
 
-/* llmoduleprovider -> ExecutionEngine.t -> llmodule */
-CAMLprim LLVMModuleRef llvm_ee_remove_mp(LLVMModuleProviderRef MP,
-                                         LLVMExecutionEngineRef EE) {
+/* llmodule -> ExecutionEngine.t -> llmodule */
+CAMLprim LLVMModuleRef llvm_ee_remove_module(LLVMModuleRef M,
+                                             LLVMExecutionEngineRef EE) {
   LLVMModuleRef RemovedModule;
   char *Error;
-  if (LLVMRemoveModuleProvider(EE, MP, &RemovedModule, &Error))
+  if (LLVMRemoveModule(EE, M, &RemovedModule, &Error))
     llvm_raise(llvm_ee_error_exn, Error);
   return RemovedModule;
 }
@@ -233,7 +229,7 @@ CAMLprim value llvm_ee_find_function(value Name, LLVMExecutionEngineRef EE) {
   LLVMValueRef Found;
   if (LLVMFindFunction(EE, String_val(Name), &Found))
     CAMLreturn(Val_unit);
-  Option = alloc(1, 1);
+  Option = alloc(1, 0);
   Field(Option, 0) = Val_op(Found);
   CAMLreturn(Option);
 }
@@ -328,3 +324,18 @@ CAMLprim value llvm_ee_free_machine_code(LLVMValueRef F,
   return Val_unit;
 }
 
+extern value llvm_alloc_data_layout(LLVMTargetDataRef TargetData);
+
+/* ExecutionEngine.t -> Llvm_target.DataLayout.t */
+CAMLprim value llvm_ee_get_data_layout(LLVMExecutionEngineRef EE) {
+  value DataLayout;
+  LLVMTargetDataRef OrigDataLayout;
+  OrigDataLayout = LLVMGetExecutionEngineTargetData(EE);
+
+  char* TargetDataCStr;
+  TargetDataCStr = LLVMCopyStringRepOfTargetData(OrigDataLayout);
+  DataLayout = llvm_alloc_data_layout(LLVMCreateTargetData(TargetDataCStr));
+  LLVMDisposeMessage(TargetDataCStr);
+
+  return DataLayout;
+}