1 (* RUN: %ocamlc -warn-error A llvm.cma llvm_target.cma llvm_executionengine.cma %s -o %t 2> /dev/null
6 open Llvm_executionengine
9 (* Note that this takes a moment to link, so it's best to keep the number of
10 individual tests low. *)
16 let define_main_fn m retval
=
18 let str_arr_type = pointer_type
(pointer_type i8_type
) in
19 define_function
"main" (function_type i32_type
[| i32_type
;
22 let b = builder_at_end
(entry_block
fn) in
23 ignore
(build_ret
(const_int i32_type retval
) b);
27 let fn = define_function
"plus" (function_type i32_type
[| i32_type
;
29 let b = builder_at_end
(entry_block
fn) in
30 let add = build_add
(param
fn 0) (param
fn 1) "sum" b in
31 ignore
(build_ret
add b)
33 let test_genericvalue () =
35 let ptrgv = GenericValue.of_pointer
tu in
36 assert (tu = GenericValue.as_pointer
ptrgv);
38 let fpgv = GenericValue.of_float double_type
2. in
39 assert (2. = GenericValue.as_float double_type
fpgv);
41 let intgv = GenericValue.of_int i32_type
3 in
42 assert (3 = GenericValue.as_int
intgv);
44 let i32gv = GenericValue.of_int32 i32_type
(Int32.of_int
4) in
45 assert ((Int32.of_int
4) = GenericValue.as_int32
i32gv);
47 let nigv = GenericValue.of_nativeint i32_type
(Nativeint.of_int
5) in
48 assert ((Nativeint.of_int
5) = GenericValue.as_nativeint
nigv);
50 let i64gv = GenericValue.of_int64 i64_type
(Int64.of_int
6) in
51 assert ((Int64.of_int
6) = GenericValue.as_int64
i64gv)
53 let test_executionengine () =
55 let m = create_module
"test_module" in
56 let main = define_main_fn m 42 in
58 let m2 = create_module
"test_module2" in
61 let ee = ExecutionEngine.create
(ModuleProvider.create
m) in
62 let mp2 = ModuleProvider.create
m2 in
63 ExecutionEngine.add_module_provider
mp2 ee;
65 (* run_static_ctors *)
66 ExecutionEngine.run_static_ctors
ee;
68 (* run_function_as_main *)
69 let res = ExecutionEngine.run_function_as_main
main [|"test"|] [||] ee in
70 if 42 != res then bomb "main did not return 42";
72 (* free_machine_code *)
73 ExecutionEngine.free_machine_code
main ee;
76 match ExecutionEngine.find_function
"dne" ee with
77 | Some _
-> raise
(Failure
"find_function 'dne' failed")
80 match ExecutionEngine.find_function
"plus" ee with
81 | None
-> raise
(Failure
"find_function 'plus' failed")
85 let res = ExecutionEngine.run_function plus
86 [| GenericValue.of_int i32_type
2;
87 GenericValue.of_int i32_type
2 |]
89 if 4 != GenericValue.as_int
res then bomb "plus did not work";
91 (* remove_module_provider *)
92 Llvm.dispose_module
(ExecutionEngine.remove_module_provider
mp2 ee);
94 (* run_static_dtors *)
95 ExecutionEngine.run_static_dtors
ee;
97 (* Show that the target data binding links and runs.*)
98 let td = ExecutionEngine.target_data
ee in
100 (* Demonstrate that a garbage pointer wasn't returned. *)
101 let ty = intptr_type
td in
102 if ty != i32_type
&& ty != i64_type
then bomb "target_data did not work";
105 ExecutionEngine.dispose
ee
108 test_genericvalue ();
109 test_executionengine ()