2014-10-29 16:15:54 +08:00
|
|
|
(*===-- llvm_executionengine.ml - LLVM OCaml Interface --------*- OCaml -*-===*
|
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
|
|
|
*
|
|
|
|
*===----------------------------------------------------------------------===*)
|
|
|
|
|
|
|
|
exception Error of string
|
|
|
|
|
2014-10-29 16:15:54 +08:00
|
|
|
let () = Callback.register_exception "Llvm_executionengine.Error" (Error "")
|
2007-12-24 00:59:28 +08:00
|
|
|
|
2014-10-31 17:05:36 +08:00
|
|
|
external initialize : unit -> bool
|
|
|
|
= "llvm_ee_initialize"
|
|
|
|
|
|
|
|
type llexecutionengine
|
|
|
|
|
|
|
|
type llcompileroptions = {
|
|
|
|
opt_level: int;
|
|
|
|
code_model: Llvm_target.CodeModel.t;
|
|
|
|
no_framepointer_elim: bool;
|
|
|
|
enable_fast_isel: bool;
|
|
|
|
}
|
|
|
|
|
|
|
|
let default_compiler_options = {
|
|
|
|
opt_level = 0;
|
|
|
|
code_model = Llvm_target.CodeModel.JITDefault;
|
|
|
|
no_framepointer_elim = false;
|
|
|
|
enable_fast_isel = false }
|
|
|
|
|
|
|
|
external create : ?options:llcompileroptions -> Llvm.llmodule -> llexecutionengine
|
|
|
|
= "llvm_ee_create"
|
|
|
|
external dispose : llexecutionengine -> unit
|
|
|
|
= "llvm_ee_dispose"
|
|
|
|
external add_module : Llvm.llmodule -> llexecutionengine -> unit
|
|
|
|
= "llvm_ee_add_module"
|
|
|
|
external remove_module : Llvm.llmodule -> llexecutionengine -> unit
|
|
|
|
= "llvm_ee_remove_module"
|
|
|
|
external run_static_ctors : llexecutionengine -> unit
|
|
|
|
= "llvm_ee_run_static_ctors"
|
|
|
|
external run_static_dtors : llexecutionengine -> unit
|
|
|
|
= "llvm_ee_run_static_dtors"
|
|
|
|
external data_layout : llexecutionengine -> Llvm_target.DataLayout.t
|
|
|
|
= "llvm_ee_get_data_layout"
|
2015-06-27 22:32:30 +08:00
|
|
|
external add_global_mapping_ : Llvm.llvalue -> nativeint -> llexecutionengine -> unit
|
2014-10-31 17:05:36 +08:00
|
|
|
= "llvm_ee_add_global_mapping"
|
2015-06-27 22:32:30 +08:00
|
|
|
external get_global_value_address_ : string -> llexecutionengine -> nativeint
|
2014-12-24 09:52:51 +08:00
|
|
|
= "llvm_ee_get_global_value_address"
|
2015-06-27 22:32:30 +08:00
|
|
|
external get_function_address_ : string -> llexecutionengine -> nativeint
|
2014-12-24 09:52:51 +08:00
|
|
|
= "llvm_ee_get_function_address"
|
2014-10-31 17:05:36 +08:00
|
|
|
|
|
|
|
let add_global_mapping llval ptr ee =
|
|
|
|
add_global_mapping_ llval (Ctypes.raw_address_of_ptr (Ctypes.to_voidp ptr)) ee
|
|
|
|
|
2014-12-24 09:52:51 +08:00
|
|
|
let get_global_value_address name typ ee =
|
|
|
|
let vptr = get_global_value_address_ name ee in
|
2015-06-27 22:32:30 +08:00
|
|
|
if Nativeint.to_int vptr <> 0 then
|
2014-12-24 09:52:51 +08:00
|
|
|
let open Ctypes in !@ (coerce (ptr void) (ptr typ) (ptr_of_raw_address vptr))
|
|
|
|
else
|
|
|
|
raise (Error ("Value " ^ name ^ " not found"))
|
|
|
|
|
|
|
|
let get_function_address name typ ee =
|
|
|
|
let fptr = get_function_address_ name ee in
|
2015-06-27 22:32:30 +08:00
|
|
|
if Nativeint.to_int fptr <> 0 then
|
2014-12-24 09:52:51 +08:00
|
|
|
let open Ctypes in coerce (ptr void) typ (ptr_of_raw_address fptr)
|
|
|
|
else
|
|
|
|
raise (Error ("Function " ^ name ^ " not found"))
|
2014-10-31 17:05:36 +08:00
|
|
|
|
|
|
|
(* The following are not bound. Patches are welcome.
|
|
|
|
target_machine : llexecutionengine -> Llvm_target.TargetMachine.t
|
|
|
|
*)
|