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