2013-11-01 08:26:01 +08:00
|
|
|
/*===-- executionengine_ocaml.c - LLVM OCaml Glue ---------------*- C++ -*-===*\
|
2007-12-24 00:59:28 +08:00
|
|
|
|* *|
|
|
|
|
|* The LLVM Compiler Infrastructure *|
|
|
|
|
|* *|
|
2007-12-30 06:59:10 +08:00
|
|
|
|* This file is distributed under the University of Illinois Open Source *|
|
|
|
|
|* License. See LICENSE.TXT for details. *|
|
2007-12-24 00:59:28 +08:00
|
|
|
|* *|
|
|
|
|
|*===----------------------------------------------------------------------===*|
|
|
|
|
|* *|
|
2013-11-01 08:26:01 +08:00
|
|
|
|* This file glues LLVM's OCaml interface to its C interface. These functions *|
|
2007-12-24 00:59:28 +08:00
|
|
|
|* are by and large transparent wrappers to the corresponding C functions. *|
|
|
|
|
|* *|
|
|
|
|
|* Note that these functions intentionally take liberties with the CAMLparamX *|
|
|
|
|
|* macros, since most of the parameters are not GC heap objects. *|
|
|
|
|
|* *|
|
|
|
|
\*===----------------------------------------------------------------------===*/
|
|
|
|
|
2014-10-29 16:15:54 +08:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2016-03-31 16:39:42 +08:00
|
|
|
#include "llvm-c/Core.h"
|
2007-12-24 00:59:28 +08:00
|
|
|
#include "llvm-c/ExecutionEngine.h"
|
2009-06-25 05:09:18 +08:00
|
|
|
#include "llvm-c/Target.h"
|
2007-12-24 00:59:28 +08:00
|
|
|
#include "caml/alloc.h"
|
|
|
|
#include "caml/custom.h"
|
|
|
|
#include "caml/fail.h"
|
|
|
|
#include "caml/memory.h"
|
2014-10-29 16:15:54 +08:00
|
|
|
#include "caml/callback.h"
|
2007-12-24 00:59:28 +08:00
|
|
|
|
2014-10-30 16:29:29 +08:00
|
|
|
void llvm_raise(value Prototype, char *Message);
|
2007-12-24 00:59:28 +08:00
|
|
|
|
2014-10-29 16:15:54 +08:00
|
|
|
/* unit -> bool */
|
2014-10-31 17:05:36 +08:00
|
|
|
CAMLprim value llvm_ee_initialize(value Unit) {
|
2014-10-29 16:15:54 +08:00
|
|
|
LLVMLinkInMCJIT();
|
|
|
|
|
|
|
|
return Val_bool(!LLVMInitializeNativeTarget() &&
|
|
|
|
!LLVMInitializeNativeAsmParser() &&
|
|
|
|
!LLVMInitializeNativeAsmPrinter());
|
|
|
|
}
|
|
|
|
|
2014-10-26 02:49:56 +08:00
|
|
|
/* llmodule -> llcompileroption -> ExecutionEngine.t */
|
2014-10-31 17:05:36 +08:00
|
|
|
CAMLprim LLVMExecutionEngineRef llvm_ee_create(value OptRecordOpt, LLVMModuleRef M) {
|
|
|
|
value OptRecord;
|
2014-10-26 02:49:56 +08:00
|
|
|
LLVMExecutionEngineRef MCJIT;
|
|
|
|
char *Error;
|
2014-10-29 16:15:54 +08:00
|
|
|
struct LLVMMCJITCompilerOptions Options;
|
|
|
|
|
|
|
|
LLVMInitializeMCJITCompilerOptions(&Options, sizeof(Options));
|
2014-10-31 17:05:36 +08:00
|
|
|
if (OptRecordOpt != Val_int(0)) {
|
|
|
|
OptRecord = Field(OptRecordOpt, 0);
|
|
|
|
Options.OptLevel = Int_val(Field(OptRecord, 0));
|
|
|
|
Options.CodeModel = Int_val(Field(OptRecord, 1));
|
|
|
|
Options.NoFramePointerElim = Int_val(Field(OptRecord, 2));
|
|
|
|
Options.EnableFastISel = Int_val(Field(OptRecord, 3));
|
|
|
|
Options.MCJMM = NULL;
|
|
|
|
}
|
2014-10-29 16:15:54 +08:00
|
|
|
|
2014-10-26 02:49:56 +08:00
|
|
|
if (LLVMCreateMCJITCompilerForModule(&MCJIT, M, &Options,
|
|
|
|
sizeof(Options), &Error))
|
2014-10-29 16:15:54 +08:00
|
|
|
llvm_raise(*caml_named_value("Llvm_executionengine.Error"), Error);
|
2014-10-26 02:49:56 +08:00
|
|
|
return MCJIT;
|
|
|
|
}
|
|
|
|
|
2007-12-24 00:59:28 +08:00
|
|
|
/* ExecutionEngine.t -> unit */
|
|
|
|
CAMLprim value llvm_ee_dispose(LLVMExecutionEngineRef EE) {
|
|
|
|
LLVMDisposeExecutionEngine(EE);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2010-03-03 07:59:00 +08:00
|
|
|
/* llmodule -> ExecutionEngine.t -> unit */
|
2010-03-04 07:51:30 +08:00
|
|
|
CAMLprim value llvm_ee_add_module(LLVMModuleRef M, LLVMExecutionEngineRef EE) {
|
2010-03-03 07:59:00 +08:00
|
|
|
LLVMAddModule(EE, M);
|
2007-12-24 00:59:28 +08:00
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2010-03-03 07:59:00 +08:00
|
|
|
/* llmodule -> ExecutionEngine.t -> llmodule */
|
2014-10-31 17:05:36 +08:00
|
|
|
CAMLprim value llvm_ee_remove_module(LLVMModuleRef M, LLVMExecutionEngineRef EE) {
|
2007-12-24 00:59:28 +08:00
|
|
|
LLVMModuleRef RemovedModule;
|
|
|
|
char *Error;
|
2010-03-03 07:59:00 +08:00
|
|
|
if (LLVMRemoveModule(EE, M, &RemovedModule, &Error))
|
2014-10-29 16:15:54 +08:00
|
|
|
llvm_raise(*caml_named_value("Llvm_executionengine.Error"), Error);
|
2014-10-31 17:05:36 +08:00
|
|
|
return Val_unit;
|
2007-12-24 00:59:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ExecutionEngine.t -> unit */
|
|
|
|
CAMLprim value llvm_ee_run_static_ctors(LLVMExecutionEngineRef EE) {
|
|
|
|
LLVMRunStaticConstructors(EE);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ExecutionEngine.t -> unit */
|
|
|
|
CAMLprim value llvm_ee_run_static_dtors(LLVMExecutionEngineRef EE) {
|
|
|
|
LLVMRunStaticDestructors(EE);
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2013-11-15 10:51:44 +08:00
|
|
|
extern value llvm_alloc_data_layout(LLVMTargetDataRef TargetData);
|
2013-11-11 22:47:11 +08:00
|
|
|
|
2013-11-15 10:51:44 +08:00
|
|
|
/* ExecutionEngine.t -> Llvm_target.DataLayout.t */
|
|
|
|
CAMLprim value llvm_ee_get_data_layout(LLVMExecutionEngineRef EE) {
|
|
|
|
value DataLayout;
|
|
|
|
LLVMTargetDataRef OrigDataLayout;
|
|
|
|
char* TargetDataCStr;
|
2014-10-29 16:15:54 +08:00
|
|
|
|
|
|
|
OrigDataLayout = LLVMGetExecutionEngineTargetData(EE);
|
2013-11-15 10:51:44 +08:00
|
|
|
TargetDataCStr = LLVMCopyStringRepOfTargetData(OrigDataLayout);
|
|
|
|
DataLayout = llvm_alloc_data_layout(LLVMCreateTargetData(TargetDataCStr));
|
|
|
|
LLVMDisposeMessage(TargetDataCStr);
|
|
|
|
|
|
|
|
return DataLayout;
|
2013-11-11 22:47:11 +08:00
|
|
|
}
|
2014-10-31 17:05:36 +08:00
|
|
|
|
|
|
|
/* Llvm.llvalue -> int64 -> llexecutionengine -> unit */
|
|
|
|
CAMLprim value llvm_ee_add_global_mapping(LLVMValueRef Global, value Ptr,
|
|
|
|
LLVMExecutionEngineRef EE) {
|
|
|
|
LLVMAddGlobalMapping(EE, Global, (void*) (Int64_val(Ptr)));
|
|
|
|
return Val_unit;
|
|
|
|
}
|
|
|
|
|
2014-12-24 09:52:51 +08:00
|
|
|
CAMLprim value llvm_ee_get_global_value_address(value Name,
|
|
|
|
LLVMExecutionEngineRef EE) {
|
|
|
|
return caml_copy_int64((int64_t) LLVMGetGlobalValueAddress(EE, String_val(Name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
CAMLprim value llvm_ee_get_function_address(value Name,
|
|
|
|
LLVMExecutionEngineRef EE) {
|
|
|
|
return caml_copy_int64((int64_t) LLVMGetFunctionAddress(EE, String_val(Name)));
|
2014-10-31 17:05:36 +08:00
|
|
|
}
|