1 (* RUN: %ocamlc -warn-error A llvm.cma llvm_executionengine.cma %s -o %t
6 open Llvm_executionengine
8 (* Note that this takes a moment to link, so it's best to keep the number of
9 individual tests low. *)
15 let define_main_fn m retval
=
17 let str_arr_type = pointer_type
(pointer_type i8_type
) in
18 define_function
"main" (function_type i32_type
[| i32_type
;
21 let b = builder_at_end
(entry_block
fn) in
22 ignore
(build_ret
(const_int i32_type retval
) b);
26 let fn = define_function
"plus" (function_type i32_type
[| i32_type
;
28 let b = builder_at_end
(entry_block
fn) in
29 let add = build_add
(param
fn 0) (param
fn 1) "sum" b in
30 ignore
(build_ret
add b)
32 let test_genericvalue () =
34 let ptrgv = GenericValue.of_pointer
tu in
35 assert (tu = GenericValue.as_pointer
ptrgv);
37 let fpgv = GenericValue.of_float double_type
2. in
38 assert (2. = GenericValue.as_float double_type
fpgv);
40 let intgv = GenericValue.of_int i32_type
3 in
41 assert (3 = GenericValue.as_int
intgv);
43 let i32gv = GenericValue.of_int32 i32_type
(Int32.of_int
4) in
44 assert ((Int32.of_int
4) = GenericValue.as_int32
i32gv);
46 let nigv = GenericValue.of_nativeint i32_type
(Nativeint.of_int
5) in
47 assert ((Nativeint.of_int
5) = GenericValue.as_nativeint
nigv);
49 let i64gv = GenericValue.of_int64 i64_type
(Int64.of_int
6) in
50 assert ((Int64.of_int
6) = GenericValue.as_int64
i64gv)
52 let test_executionengine () =
54 let m = create_module
"test_module" in
55 let main = define_main_fn m 42 in
57 let m2 = create_module
"test_module2" in
60 let ee = ExecutionEngine.create
(ModuleProvider.create
m) in
61 let mp2 = ModuleProvider.create
m2 in
62 ExecutionEngine.add_module_provider
mp2 ee;
64 (* run_static_ctors *)
65 ExecutionEngine.run_static_ctors
ee;
67 (* run_function_as_main *)
68 let res = ExecutionEngine.run_function_as_main
main [|"test"|] [||] ee in
69 if 42 != res then bomb "main did not return 42";
71 (* free_machine_code *)
72 ExecutionEngine.free_machine_code
main ee;
75 match ExecutionEngine.find_function
"dne" ee with
76 | Some _
-> raise
(Failure
"find_function 'dne' failed")
79 match ExecutionEngine.find_function
"plus" ee with
80 | None
-> raise
(Failure
"find_function 'plus' failed")
84 let res = ExecutionEngine.run_function plus
85 [| GenericValue.of_int i32_type
2;
86 GenericValue.of_int i32_type
2 |]
88 if 4 != GenericValue.as_int
res then bomb "plus did not work";
90 (* remove_module_provider *)
91 Llvm.dispose_module
(ExecutionEngine.remove_module_provider
mp2 ee);
93 (* run_static_dtors *)
94 ExecutionEngine.run_static_dtors
ee;
97 ExecutionEngine.dispose
ee
100 test_genericvalue ();
101 test_executionengine ()