llvm.org GIT mirror llvm / 16609f3
Remove module providers from ocaml. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@97609 91177308-0d34-0410-b5e6-96231b3b80d8 Erick Tryzelaar 10 years ago
13 changed file(s) with 91 addition(s) and 144 deletion(s). Raw diff Collapse all Expand all
4545 /*===-- Modules -----------------------------------------------------------===*/
4646
4747 /* Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule */
48 CAMLprim value llvm_get_module_provider(LLVMContextRef C,
49 LLVMMemoryBufferRef MemBuf) {
48 CAMLprim value llvm_get_module(LLVMContextRef C, LLVMMemoryBufferRef MemBuf) {
5049 CAMLparam0();
5150 CAMLlocal2(Variant, MessageVal);
5251 char *Message;
5352
54 LLVMModuleProviderRef MP;
55 if (LLVMGetBitcodeModuleProviderInContext(C, MemBuf, &MP, &Message))
53 LLVMModuleRef M;
54 if (LLVMGetBitcodeModuleInContext(C, MemBuf, &M, &Message))
5655 llvm_raise(llvm_bitreader_error_exn, Message);
5756
58 CAMLreturn((value) MP);
57 CAMLreturn((value) M);
5958 }
6059
6160 /* Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule */
1212 external register_exns : exn -> unit = "llvm_register_bitreader_exns"
1313 let _ = register_exns (Error "")
1414
15 external get_module_provider : Llvm.llcontext -> Llvm.llmemorybuffer ->
16 Llvm.llmoduleprovider
17 = "llvm_get_module_provider"
15 external get_module : Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule
16 = "llvm_get_module"
1817
1918 external parse_bitcode : Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule
2019 = "llvm_parse_bitcode"
1313
1414 exception Error of string
1515
16 (** [get_module_provider context mb] reads the bitcode for a new
17 module provider [m] from the memory buffer [mb] in the context [context].
18 Returns [m] if successful, or raises [Error msg] otherwise, where [msg] is a
19 description of the error encountered. See the function
20 [llvm::getBitcodeModuleProvider]. *)
21 external get_module_provider : Llvm.llcontext -> Llvm.llmemorybuffer ->
22 Llvm.llmoduleprovider
23 = "llvm_get_module_provider"
16 (** [get_module context mb] reads the bitcode for a new module [m] from the
17 memory buffer [mb] in the context [context]. Returns [m] if successful, or
18 raises [Error msg] otherwise, where [msg] is a description of the error
19 encountered. See the function [llvm::getBitcodeModule]. *)
20 external get_module : Llvm.llcontext -> Llvm.llmemorybuffer -> Llvm.llmodule
21 = "llvm_get_module"
2422
2523 (** [parse_bitcode context mb] parses the bitcode for a new module [m] from the
2624 memory buffer [mb] in the context [context]. Returns [m] if successful, or
167167
168168 /*--... Operations on execution engines ....................................--*/
169169
170 /* llmoduleprovider -> ExecutionEngine.t */
171 CAMLprim LLVMExecutionEngineRef llvm_ee_create(LLVMModuleProviderRef MP) {
170 /* llmodule -> ExecutionEngine.t */
171 CAMLprim LLVMExecutionEngineRef llvm_ee_create(LLVMModuleRef M) {
172172 LLVMExecutionEngineRef Interp;
173173 char *Error;
174 if (LLVMCreateExecutionEngine(&Interp, MP, &Error))
174 if (LLVMCreateExecutionEngineForModule(&Interp, M, &Error))
175175 llvm_raise(llvm_ee_error_exn, Error);
176176 return Interp;
177177 }
178178
179 /* llmoduleprovider -> ExecutionEngine.t */
179 /* llmodule -> ExecutionEngine.t */
180180 CAMLprim LLVMExecutionEngineRef
181 llvm_ee_create_interpreter(LLVMModuleProviderRef MP) {
181 llvm_ee_create_interpreter(LLVMModuleRef M) {
182182 LLVMExecutionEngineRef Interp;
183183 char *Error;
184 if (LLVMCreateInterpreter(&Interp, MP, &Error))
184 if (LLVMCreateInterpreterForModule(&Interp, M, &Error))
185185 llvm_raise(llvm_ee_error_exn, Error);
186186 return Interp;
187187 }
188188
189 /* llmoduleprovider -> ExecutionEngine.t */
189 /* llmodule -> ExecutionEngine.t */
190190 CAMLprim LLVMExecutionEngineRef
191 llvm_ee_create_jit(LLVMModuleProviderRef MP) {
191 llvm_ee_create_jit(LLVMModuleRef M) {
192192 LLVMExecutionEngineRef JIT;
193193 char *Error;
194 if (LLVMCreateJITCompiler(&JIT, MP, 3, &Error))
194 if (LLVMCreateJITCompilerForModule(&JIT, M, 3, &Error))
195195 llvm_raise(llvm_ee_error_exn, Error);
196196 return JIT;
197197 }
198198
199 /* llmoduleprovider -> ExecutionEngine.t */
199 /* llmodule -> ExecutionEngine.t */
200200 CAMLprim LLVMExecutionEngineRef
201 llvm_ee_create_fast_jit(LLVMModuleProviderRef MP) {
201 llvm_ee_create_fast_jit(LLVMModuleRef M) {
202202 LLVMExecutionEngineRef JIT;
203203 char *Error;
204 if (LLVMCreateJITCompiler(&JIT, MP, 0, &Error))
204 if (LLVMCreateJITCompiler(&JIT, M, 0, &Error))
205205 llvm_raise(llvm_ee_error_exn, Error);
206206 return JIT;
207207 }
212212 return Val_unit;
213213 }
214214
215 /* llmoduleprovider -> ExecutionEngine.t -> unit */
216 CAMLprim value llvm_ee_add_mp(LLVMModuleProviderRef MP,
217 LLVMExecutionEngineRef EE) {
218 LLVMAddModuleProvider(EE, MP);
219 return Val_unit;
220 }
221
222 /* llmoduleprovider -> ExecutionEngine.t -> llmodule */
223 CAMLprim LLVMModuleRef llvm_ee_remove_mp(LLVMModuleProviderRef MP,
215 /* llmodule -> ExecutionEngine.t -> unit */
216 CAMLprim value llvm_ee_add_mp(LLVMModuleRef M, LLVMExecutionEngineRef EE) {
217 LLVMAddModule(EE, M);
218 return Val_unit;
219 }
220
221 /* llmodule -> ExecutionEngine.t -> llmodule */
222 CAMLprim LLVMModuleRef llvm_ee_remove_mp(LLVMModuleRef M,
224223 LLVMExecutionEngineRef EE) {
225224 LLVMModuleRef RemovedModule;
226225 char *Error;
227 if (LLVMRemoveModuleProvider(EE, MP, &RemovedModule, &Error))
226 if (LLVMRemoveModule(EE, M, &RemovedModule, &Error))
228227 llvm_raise(llvm_ee_error_exn, Error);
229228 return RemovedModule;
230229 }
5555 call into LLVM. *)
5656 let _ = register_exns (Error "")
5757
58 external create: Llvm.llmoduleprovider -> t
58 external create: Llvm.llmodule -> t
5959 = "llvm_ee_create"
60 external create_interpreter: Llvm.llmoduleprovider -> t
60 external create_interpreter: Llvm.llmodule -> t
6161 = "llvm_ee_create_interpreter"
62 external create_jit: Llvm.llmoduleprovider -> t
62 external create_jit: Llvm.llmodule -> t
6363 = "llvm_ee_create_jit"
64 external create_fast_jit: Llvm.llmoduleprovider -> t
64 external create_fast_jit: Llvm.llmodule -> t
6565 = "llvm_ee_create_fast_jit"
6666 external dispose: t -> unit
6767 = "llvm_ee_dispose"
68 external add_module_provider: Llvm.llmoduleprovider -> t -> unit
68 external add_module: Llvm.llmodule -> t -> unit
6969 = "llvm_ee_add_mp"
70 external remove_module_provider: Llvm.llmoduleprovider -> t -> Llvm.llmodule
70 external remove_module: Llvm.llmodule -> t -> Llvm.llmodule
7171 = "llvm_ee_remove_mp"
7272 external find_function: string -> t -> Llvm.llvalue option
7373 = "llvm_ee_find_function"
8484 invoking a static compiler and generating a native executable. *)
8585 type t
8686
87 (** [create mp] creates a new execution engine, taking ownership of the
88 module provider [mp] if successful. Creates a JIT if possible, else falls
89 back to an interpreter. Raises [Error msg] if an error occurrs. The
87 (** [create m] creates a new execution engine, taking ownership of the
88 module [m] if successful. Creates a JIT if possible, else falls back to an
89 interpreter. Raises [Error msg] if an error occurrs. The execution engine
90 is not garbage collected and must be destroyed with [dispose ee].
91 See the function [llvm::EngineBuilder::create]. *)
92 val create: Llvm.llmodule -> t
93
94 (** [create_interpreter m] creates a new interpreter, taking ownership of the
95 module [m] if successful. Raises [Error msg] if an error occurrs. The
9096 execution engine is not garbage collected and must be destroyed with
91 [dispose ee]. See the function [llvm::EngineBuilder::create]. *)
92 val create: Llvm.llmoduleprovider -> t
97 [dispose ee].
98 See the function [llvm::EngineBuilder::create]. *)
99 val create_interpreter: Llvm.llmodule -> t
93100
94 (** [create_interpreter mp] creates a new interpreter, taking ownership of the
95 module provider [mp] if successful. Raises [Error msg] if an error
96 occurrs. The execution engine is not garbage collected and must be
101 (** [create_jit m] creates a new JIT (just-in-time compiler), taking
102 ownership of the module [m] if successful. This function creates a JIT
103 which favors code quality over compilation speed. Raises [Error msg] if an
104 error occurrs. The execution engine is not garbage collected and must be
97105 destroyed with [dispose ee].
98106 See the function [llvm::EngineBuilder::create]. *)
99 val create_interpreter: Llvm.llmoduleprovider -> t
107 val create_jit: Llvm.llmodule -> t
100108
101 (** [create_jit mp] creates a new JIT (just-in-time compiler), taking
102 ownership of the module provider [mp] if successful. This function creates
103 a JIT which favors code quality over compilation speed. Raises [Error msg]
104 if an error occurrs. The execution engine is not garbage collected and
105 must be destroyed with [dispose ee].
109 (** [create_fast_jit m] creates a new JIT (just-in-time compiler) which
110 favors compilation speed over code quality. It takes ownership of the
111 module [m] if successful. Raises [Error msg] if an error occurrs. The
112 execution engine is not garbage collected and must be destroyed with
113 [dispose ee].
106114 See the function [llvm::EngineBuilder::create]. *)
107 val create_jit: Llvm.llmoduleprovider -> t
108
109 (** [create_fast_jit mp] creates a new JIT (just-in-time compiler) which
110 favors compilation speed over code quality. It takes ownership of the
111 module provider [mp] if successful. Raises [Error msg] if an error
112 occurrs. The execution engine is not garbage collected and must be
113 destroyed with [dispose ee].
114 See the function [llvm::EngineBuilder::create]. *)
115 val create_fast_jit: Llvm.llmoduleprovider -> t
115 val create_fast_jit: Llvm.llmodule -> t
116116
117117 (** [dispose ee] releases the memory used by the execution engine and must be
118118 invoked to avoid memory leaks. *)
119119 val dispose: t -> unit
120120
121 (** [add_module_provider mp ee] adds the module provider [mp] to the execution
122 engine [ee]. *)
123 val add_module_provider: Llvm.llmoduleprovider -> t -> unit
121 (** [add_module m ee] adds the module [m] to the execution engine [ee]. *)
122 val add_module: Llvm.llmodule -> t -> unit
124123
125 (** [remove_module_provider mp ee] removes the module provider [mp] from the
126 execution engine [ee], disposing of [mp] and the module referenced by
127 [mp]. Raises [Error msg] if an error occurs. *)
128 val remove_module_provider: Llvm.llmoduleprovider -> t -> Llvm.llmodule
124 (** [remove_module m ee] removes the module [m] from the execution engine
125 [ee], disposing of [m] and the module referenced by [mp]. Raises
126 [Error msg] if an error occurs. *)
127 val remove_module: Llvm.llmodule -> t -> Llvm.llmodule
129128
130129 (** [find_function n ee] finds the function named [n] defined in any of the
131130 modules owned by the execution engine [ee]. Returns [None] if the function
1515 type lluse
1616 type llbasicblock
1717 type llbuilder
18 type llmoduleprovider
1918 type llmemorybuffer
2019
2120 module TypeKind = struct
947946 external build_ptrdiff : llvalue -> llvalue -> string -> llbuilder -> llvalue
948947 = "llvm_build_ptrdiff"
949948
950 (*===-- Module providers --------------------------------------------------===*)
951
952 module ModuleProvider = struct
953 external create : llmodule -> llmoduleprovider
954 = "LLVMCreateModuleProviderForExistingModule"
955 external dispose : llmoduleprovider -> unit = "llvm_dispose_module_provider"
956 end
957
958949
959950 (*===-- Memory buffers ----------------------------------------------------===*)
960951
971962 type 'a t
972963 type any = [ `Module | `Function ]
973964 external create : unit -> [ `Module ] t = "llvm_passmanager_create"
974 external create_function : llmoduleprovider -> [ `Function ] t
965 external create_function : llmodule -> [ `Function ] t
975966 = "LLVMCreateFunctionPassManager"
976967 external run_module : llmodule -> [ `Module ] t -> bool
977968 = "llvm_passmanager_run_module"
4747 (** Used to generate instructions in the LLVM IR. See the [llvm::LLVMBuilder]
4848 class. *)
4949 type llbuilder
50
51 (** Used to provide a module to JIT or interpreter.
52 See the [llvm::ModuleProvider] class. *)
53 type llmoduleprovider
5450
5551 (** Used to efficiently handle large buffers of read-only binary data.
5652 See the [llvm::MemoryBuffer] class. *)
21972193 external build_ptrdiff : llvalue -> llvalue -> string -> llbuilder -> llvalue
21982194 = "llvm_build_ptrdiff"
21992195
2200 (** {6 Module providers} *)
2201
2202 module ModuleProvider : sig
2203 (** [create_module_provider m] encapsulates [m] in a module provider and takes
2204 ownership of the module. See the constructor
2205 [llvm::ExistingModuleProvider::ExistingModuleProvider]. *)
2206 external create : llmodule -> llmoduleprovider
2207 = "LLVMCreateModuleProviderForExistingModule"
2208
2209 (** [dispose_module_provider mp] destroys the module provider [mp] as well as
2210 the contained module. *)
2211 external dispose : llmoduleprovider -> unit = "llvm_dispose_module_provider"
2212 end
2213
22142196
22152197 (** {6 Memory buffers} *)
22162198
22422224 See the constructor of [llvm::PassManager]. *)
22432225 external create : unit -> [ `Module ] t = "llvm_passmanager_create"
22442226
2245 (** [PassManager.create_function mp] constructs a new function-by-function
2246 pass pipeline over the module provider [mp]. It does not take ownership of
2247 [mp]. This type of pipeline is suitable for code generation and JIT
2248 compilation tasks.
2227 (** [PassManager.create_function m] constructs a new function-by-function
2228 pass pipeline over the module [m]. It does not take ownership of [m].
2229 This type of pipeline is suitable for code generation and JIT compilation
2230 tasks.
22492231 See the constructor of [llvm::FunctionPassManager]. *)
2250 external create_function : llmoduleprovider -> [ `Function ] t
2232 external create_function : llmodule -> [ `Function ] t
22512233 = "LLVMCreateFunctionPassManager"
22522234
22532235 (** [run_module m pm] initializes, executes on the module [m], and finalizes
22772259 external finalize : [ `Function ] t -> bool = "llvm_passmanager_finalize"
22782260
22792261 (** Frees the memory of a pass pipeline. For function pipelines, does not free
2280 the module provider.
2262 the module.
22812263 See the destructor of [llvm::BasePassManager]. *)
22822264 external dispose : [< any ] t -> unit = "llvm_passmanager_dispose"
22832265 end
17511751 return LLVMBuildPtrDiff(Builder_val(B), LHS, RHS, String_val(Name));
17521752 }
17531753
1754 /*===-- Module Providers --------------------------------------------------===*/
1755
1756 /* llmoduleprovider -> unit */
1757 CAMLprim value llvm_dispose_module_provider(LLVMModuleProviderRef MP) {
1758 LLVMDisposeModuleProvider(MP);
1759 return Val_unit;
1760 }
1761
17621754
17631755 /*===-- Memory buffers ----------------------------------------------------===*/
17641756
4040 true
4141 end;
4242
43 (* get_module_provider *)
43 (* get_module *)
4444 begin
4545 let mb = Llvm.MemoryBuffer.of_file fn in
46 let mp = begin try
47 Llvm_bitreader.get_module_provider context mb
46 let m = begin try
47 Llvm_bitreader.get_module context mb
4848 with x ->
4949 Llvm.MemoryBuffer.dispose mb;
5050 raise x
5151 end in
52 Llvm.ModuleProvider.dispose mp
52 Llvm.dispose_module m
5353 end;
5454
5555 (* corrupt the bitcode *)
5959 close_out oc
6060 end;
6161
62 (* test get_module_provider exceptions *)
62 (* test get_module exceptions *)
6363 test begin
6464 try
6565 let mb = Llvm.MemoryBuffer.of_file fn in
66 let mp = begin try
67 Llvm_bitreader.get_module_provider context mb
66 let m = begin try
67 Llvm_bitreader.get_module context mb
6868 with x ->
6969 Llvm.MemoryBuffer.dispose mb;
7070 raise x
7171 end in
72 Llvm.ModuleProvider.dispose mp;
72 Llvm.dispose_module m;
7373 false
7474 with Llvm_bitreader.Error _ ->
7575 true
6363 let m2 = create_module (global_context ()) "test_module2" in
6464 define_plus m2;
6565
66 let ee = ExecutionEngine.create (ModuleProvider.create m) in
67 let mp2 = ModuleProvider.create m2 in
68 ExecutionEngine.add_module_provider mp2 ee;
66 let ee = ExecutionEngine.create m in
67 ExecutionEngine.add_module m2 ee;
6968
7069 (* run_static_ctors *)
7170 ExecutionEngine.run_static_ctors ee;
9392 ee in
9493 if 4 != GenericValue.as_int res then bomb "plus did not work";
9594
96 (* remove_module_provider *)
97 Llvm.dispose_module (ExecutionEngine.remove_module_provider mp2 ee);
95 (* remove_module *)
96 Llvm.dispose_module (ExecutionEngine.remove_module m2 ee);
9897
9998 (* run_static_dtors *)
10099 ExecutionEngine.run_static_dtors ee;
2121
2222 let filename = Sys.argv.(1)
2323 let m = create_module context filename
24 let mp = ModuleProvider.create m
2524
2625
2726 (*===-- Transforms --------------------------------------------------------===*)
3534
3635 let td = TargetData.create (target_triple m) in
3736
38 ignore (PassManager.create_function mp
37 ignore (PassManager.create_function m
3938 ++ TargetData.add td
4039 ++ add_instruction_combining
4140 ++ add_reassociation
5453
5554 let _ =
5655 suite "transforms" test_transforms;
57 ModuleProvider.dispose mp
56 dispose_module m
5757
5858 let filename = Sys.argv.(1)
5959 let m = create_module context filename
60 let mp = ModuleProvider.create m
6160
6261
6362 (*===-- Target ------------------------------------------------------------===*)
12631262 end
12641263
12651264
1266 (*===-- Module Provider ---------------------------------------------------===*)
1267
1268 let test_module_provider () =
1269 let m = create_module context "test" in
1270 let mp = ModuleProvider.create m in
1271 ModuleProvider.dispose mp
1272
1273
12741265 (*===-- Pass Managers -----------------------------------------------------===*)
12751266
12761267 let test_pass_manager () =
12871278 let fn = define_function "FunctionPassManager" fty m in
12881279 ignore (build_ret_void (builder_at_end context (entry_block fn)));
12891280
1290 ignore (PassManager.create_function mp
1281 ignore (PassManager.create_function m
12911282 ++ PassManager.initialize
12921283 ++ PassManager.run_function fn
12931284 ++ PassManager.finalize
13061297 group "writer";
13071298 insist (write_bitcode_file m filename);
13081299
1309 ModuleProvider.dispose mp
1300 dispose_module m
13101301
13111302
13121303 (*===-- Driver ------------------------------------------------------------===*)
13251316 suite "basic blocks" test_basic_blocks;
13261317 suite "instructions" test_instructions;
13271318 suite "builder" test_builder;
1328 suite "module provider" test_module_provider;
13291319 suite "pass manager" test_pass_manager;
13301320 suite "writer" test_writer; (* Keep this last; it disposes m. *)
13311321 exit !exit_status