Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 1 | (*===-- llvm_executionengine.mli - LLVM Ocaml Interface ---------*- C++ -*-===* |
| 2 | * |
| 3 | * The LLVM Compiler Infrastructure |
| 4 | * |
Chris Lattner | 6787a45 | 2007-12-29 22:59:10 +0000 | [diff] [blame] | 5 | * This file is distributed under the University of Illinois Open Source |
| 6 | * License. See LICENSE.TXT for details. |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 7 | * |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 8 | *===----------------------------------------------------------------------===*) |
| 9 | |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 10 | (** JIT Interpreter. |
| 11 | |
| 12 | This interface provides an ocaml API for LLVM execution engine (JIT/ |
| 13 | interpreter), the classes in the ExecutionEngine library. *) |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 14 | |
| 15 | exception Error of string |
| 16 | |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 17 | module GenericValue: sig |
| 18 | (** [GenericValue.t] is a boxed union type used to portably pass arguments to |
| 19 | and receive values from the execution engine. It supports only a limited |
| 20 | selection of types; for more complex argument types, it is necessary to |
| 21 | generate a stub function by hand or to pass parameters by reference. |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 22 | See the struct [llvm::GenericValue]. *) |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 23 | type t |
| 24 | |
| 25 | (** [of_float fpty n] boxes the float [n] in a float-valued generic value |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 26 | according to the floating point type [fpty]. See the fields |
| 27 | [llvm::GenericValue::DoubleVal] and [llvm::GenericValue::FloatVal]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 28 | val of_float : Llvm.lltype -> float -> t |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 29 | |
| 30 | (** [of_pointer v] boxes the pointer value [v] in a generic value. See the |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 31 | field [llvm::GenericValue::PointerVal]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 32 | val of_pointer : 'a -> t |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 33 | |
| 34 | (** [of_int32 n w] boxes the int32 [i] in a generic value with the bitwidth |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 35 | [w]. See the field [llvm::GenericValue::IntVal]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 36 | val of_int32 : Llvm.lltype -> int32 -> t |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 37 | |
| 38 | (** [of_int n w] boxes the int [i] in a generic value with the bitwidth |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 39 | [w]. See the field [llvm::GenericValue::IntVal]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 40 | val of_int : Llvm.lltype -> int -> t |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 41 | |
| 42 | (** [of_natint n w] boxes the native int [i] in a generic value with the |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 43 | bitwidth [w]. See the field [llvm::GenericValue::IntVal]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 44 | val of_nativeint : Llvm.lltype -> nativeint -> t |
| 45 | |
Erick Tryzelaar | 7dd2615 | 2010-03-03 23:51:28 +0000 | [diff] [blame] | 46 | |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 47 | (** [of_int64 n w] boxes the int64 [i] in a generic value with the bitwidth |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 48 | [w]. See the field [llvm::GenericValue::IntVal]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 49 | val of_int64 : Llvm.lltype -> int64 -> t |
Erick Tryzelaar | 7dd2615 | 2010-03-03 23:51:28 +0000 | [diff] [blame] | 50 | |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 51 | (** [as_float fpty gv] unboxes the floating point-valued generic value [gv] of |
| 52 | floating point type [fpty]. See the fields [llvm::GenericValue::DoubleVal] |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 53 | and [llvm::GenericValue::FloatVal]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 54 | val as_float : Llvm.lltype -> t -> float |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 55 | |
| 56 | (** [as_pointer gv] unboxes the pointer-valued generic value [gv]. See the |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 57 | field [llvm::GenericValue::PointerVal]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 58 | val as_pointer : t -> 'a |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 59 | |
| 60 | (** [as_int32 gv] unboxes the integer-valued generic value [gv] as an [int32]. |
| 61 | Is invalid if [gv] has a bitwidth greater than 32 bits. See the field |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 62 | [llvm::GenericValue::IntVal]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 63 | val as_int32 : t -> int32 |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 64 | |
| 65 | (** [as_int gv] unboxes the integer-valued generic value [gv] as an [int]. |
| 66 | Is invalid if [gv] has a bitwidth greater than the host bit width (but the |
| 67 | most significant bit may be lost). See the field |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 68 | [llvm::GenericValue::IntVal]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 69 | val as_int : t -> int |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 70 | |
| 71 | (** [as_natint gv] unboxes the integer-valued generic value [gv] as a |
| 72 | [nativeint]. Is invalid if [gv] has a bitwidth greater than |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 73 | [nativeint]. See the field [llvm::GenericValue::IntVal]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 74 | val as_nativeint : t -> nativeint |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 75 | |
| 76 | (** [as_int64 gv] returns the integer-valued generic value [gv] as an [int64]. |
| 77 | Is invalid if [gv] has a bitwidth greater than [int64]. See the field |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 78 | [llvm::GenericValue::IntVal]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 79 | val as_int64 : t -> int64 |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 80 | end |
| 81 | |
| 82 | |
| 83 | module ExecutionEngine: sig |
| 84 | (** An execution engine is either a JIT compiler or an interpreter, capable of |
| 85 | directly loading an LLVM module and executing its functions without first |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 86 | invoking a static compiler and generating a native executable. *) |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 87 | type t |
| 88 | |
Erick Tryzelaar | 98b05d6 | 2010-03-02 23:59:00 +0000 | [diff] [blame] | 89 | (** [create m] creates a new execution engine, taking ownership of the |
| 90 | module [m] if successful. Creates a JIT if possible, else falls back to an |
| 91 | interpreter. Raises [Error msg] if an error occurrs. The execution engine |
| 92 | is not garbage collected and must be destroyed with [dispose ee]. |
| 93 | See the function [llvm::EngineBuilder::create]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 94 | val create : Llvm.llmodule -> t |
Erick Tryzelaar | 98b05d6 | 2010-03-02 23:59:00 +0000 | [diff] [blame] | 95 | |
| 96 | (** [create_interpreter m] creates a new interpreter, taking ownership of the |
| 97 | module [m] if successful. Raises [Error msg] if an error occurrs. The |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 98 | execution engine is not garbage collected and must be destroyed with |
Erick Tryzelaar | 98b05d6 | 2010-03-02 23:59:00 +0000 | [diff] [blame] | 99 | [dispose ee]. |
| 100 | See the function [llvm::EngineBuilder::create]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 101 | val create_interpreter : Llvm.llmodule -> t |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 102 | |
Erick Tryzelaar | 94feaaf | 2010-03-02 23:59:03 +0000 | [diff] [blame] | 103 | (** [create_jit m optlevel] creates a new JIT (just-in-time compiler), taking |
| 104 | ownership of the module [m] if successful with the desired optimization |
| 105 | level [optlevel]. Raises [Error msg] if an error occurrs. The execution |
| 106 | engine is not garbage collected and must be destroyed with [dispose ee]. |
Reid Kleckner | fc8a2d5 | 2009-07-18 00:42:18 +0000 | [diff] [blame] | 107 | See the function [llvm::EngineBuilder::create]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 108 | val create_jit : Llvm.llmodule -> int -> t |
Erick Tryzelaar | 94feaaf | 2010-03-02 23:59:03 +0000 | [diff] [blame] | 109 | |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 110 | (** [dispose ee] releases the memory used by the execution engine and must be |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 111 | invoked to avoid memory leaks. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 112 | val dispose : t -> unit |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 113 | |
Erick Tryzelaar | 98b05d6 | 2010-03-02 23:59:00 +0000 | [diff] [blame] | 114 | (** [add_module m ee] adds the module [m] to the execution engine [ee]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 115 | val add_module : Llvm.llmodule -> t -> unit |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 116 | |
Erick Tryzelaar | 98b05d6 | 2010-03-02 23:59:00 +0000 | [diff] [blame] | 117 | (** [remove_module m ee] removes the module [m] from the execution engine |
| 118 | [ee], disposing of [m] and the module referenced by [mp]. Raises |
| 119 | [Error msg] if an error occurs. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 120 | val remove_module : Llvm.llmodule -> t -> Llvm.llmodule |
| 121 | |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 122 | |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 123 | (** [find_function n ee] finds the function named [n] defined in any of the |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 124 | modules owned by the execution engine [ee]. Returns [None] if the function |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 125 | is not found and [Some f] otherwise. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 126 | val find_function : string -> t -> Llvm.llvalue option |
| 127 | |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 128 | |
| 129 | (** [run_function f args ee] synchronously executes the function [f] with the |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 130 | arguments [args], which must be compatible with the parameter types. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 131 | val run_function : Llvm.llvalue -> GenericValue.t array -> t -> |
Erick Tryzelaar | 7dd2615 | 2010-03-03 23:51:28 +0000 | [diff] [blame] | 132 | GenericValue.t |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 133 | |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 134 | |
| 135 | (** [run_static_ctors ee] executes the static constructors of each module in |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 136 | the execution engine [ee]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 137 | val run_static_ctors : t -> unit |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 138 | |
| 139 | (** [run_static_dtors ee] executes the static destructors of each module in |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 140 | the execution engine [ee]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 141 | val run_static_dtors : t -> unit |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 142 | |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 143 | (** [run_function_as_main f args env ee] executes the function [f] as a main |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 144 | function, passing it [argv] and [argc] according to the string array |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 145 | [args], and [envp] as specified by the array [env]. Returns the integer |
| 146 | return value of the function. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 147 | val run_function_as_main : Llvm.llvalue -> string array -> |
Erick Tryzelaar | 7dd2615 | 2010-03-03 23:51:28 +0000 | [diff] [blame] | 148 | (string * string) array -> t -> int |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 149 | |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 150 | |
| 151 | (** [free_machine_code f ee] releases the memory in the execution engine [ee] |
Gordon Henriksen | 95f4b77 | 2008-03-09 07:17:38 +0000 | [diff] [blame] | 152 | used to store the machine code for the function [f]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 153 | val free_machine_code : Llvm.llvalue -> t -> unit |
| 154 | |
Erick Tryzelaar | 8ac07c2 | 2008-03-27 00:27:14 +0000 | [diff] [blame] | 155 | |
| 156 | (** [target_data ee] is the target data owned by the execution engine |
| 157 | [ee]. *) |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 158 | val target_data : t -> Llvm_target.TargetData.t |
| 159 | |
Gordon Henriksen | 2a8cd89 | 2007-12-23 16:59:28 +0000 | [diff] [blame] | 160 | end |
Erick Tryzelaar | b4e1917 | 2009-09-14 21:54:32 +0000 | [diff] [blame] | 161 | |
Torok Edwin | 5abf51b | 2010-12-23 15:49:26 +0000 | [diff] [blame] | 162 | val initialize_native_target : unit -> bool |
| 163 | |