[OCaml] [autoconf] Migrate to ocamlfind.
[oota-llvm.git] / test / Bindings / Ocaml / executionengine.ml
1 (* RUN: cp %s %T/executionengine.ml
2  * RUN: %ocamlcomp -warn-error A -package llvm.executionengine -linkpkg %T/executionengine.ml -o %t
3  * RUN: %t
4  * REQUIRES: native, object-emission
5  * XFAIL: vg_leak
6  *)
7
8 open Llvm
9 open Llvm_executionengine
10 open Llvm_target
11
12 (* Note that this takes a moment to link, so it's best to keep the number of
13    individual tests low. *)
14
15 let context = global_context ()
16 let i8_type = Llvm.i8_type context
17 let i32_type = Llvm.i32_type context
18 let i64_type = Llvm.i64_type context
19 let double_type = Llvm.double_type context
20
21 let () =
22   assert (Llvm_executionengine.initialize_native_target ())
23
24 let bomb msg =
25   prerr_endline msg;
26   exit 2
27
28 let define_main_fn m retval =
29   let fn =
30     let str_arr_type = pointer_type (pointer_type i8_type) in
31     define_function "main" (function_type i32_type [| i32_type;
32                                                       str_arr_type;
33                                                       str_arr_type |]) m in
34   let b = builder_at_end (global_context ()) (entry_block fn) in
35   ignore (build_ret (const_int i32_type retval) b);
36   fn
37
38 let define_plus m =
39   let fn = define_function "plus" (function_type i32_type [| i32_type;
40                                                              i32_type |]) m in
41   let b = builder_at_end (global_context ()) (entry_block fn) in
42   let add = build_add (param fn 0) (param fn 1) "sum" b in
43   ignore (build_ret add b)
44
45 let test_genericvalue () =
46   let tu = (1, 2) in
47   let ptrgv = GenericValue.of_pointer tu in
48   assert (tu = GenericValue.as_pointer ptrgv);
49
50   let fpgv = GenericValue.of_float double_type 2. in
51   assert (2. = GenericValue.as_float double_type fpgv);
52
53   let intgv = GenericValue.of_int i32_type 3 in
54   assert (3  = GenericValue.as_int intgv);
55
56   let i32gv = GenericValue.of_int32 i32_type (Int32.of_int 4) in
57   assert ((Int32.of_int 4) = GenericValue.as_int32 i32gv);
58
59   let nigv = GenericValue.of_nativeint i32_type (Nativeint.of_int 5) in
60   assert ((Nativeint.of_int 5) = GenericValue.as_nativeint nigv);
61
62   let i64gv = GenericValue.of_int64 i64_type (Int64.of_int 6) in
63   assert ((Int64.of_int 6) = GenericValue.as_int64 i64gv)
64
65 let test_executionengine engine =
66   (* create *)
67   let m = create_module (global_context ()) "test_module" in
68   let main = define_main_fn m 42 in
69
70   let m2 = create_module (global_context ()) "test_module2" in
71   define_plus m2;
72
73   let ee =
74     match engine with
75     | `Interpreter -> ExecutionEngine.create_interpreter m
76     | `JIT -> ExecutionEngine.create_jit m 0
77     | `MCJIT -> ExecutionEngine.create_mcjit m ExecutionEngine.default_compiler_options
78   in
79   ExecutionEngine.add_module m2 ee;
80
81   (* run_static_ctors *)
82   ExecutionEngine.run_static_ctors ee;
83
84   (* run_function_as_main *)
85   let res = ExecutionEngine.run_function_as_main main [|"test"|] [||] ee in
86   if 42 != res then bomb "main did not return 42";
87
88   (* free_machine_code *)
89   ExecutionEngine.free_machine_code main ee;
90
91   (* find_function *)
92   match ExecutionEngine.find_function "dne" ee with
93   | Some _ -> raise (Failure "find_function 'dne' failed")
94   | None ->
95
96   match ExecutionEngine.find_function "plus" ee with
97   | None -> raise (Failure "find_function 'plus' failed")
98   | Some plus ->
99
100   begin match engine with
101   | `MCJIT -> () (* Currently can only invoke 0-ary functions *)
102   | `JIT -> () (* JIT is now a shim around MCJIT, jokes on you *)
103   | _ ->
104     (* run_function *)
105     let res = ExecutionEngine.run_function plus
106                                            [| GenericValue.of_int i32_type 2;
107                                               GenericValue.of_int i32_type 2 |]
108                                            ee in
109     if 4 != GenericValue.as_int res then bomb "plus did not work";
110   end;
111
112   (* remove_module *)
113   Llvm.dispose_module (ExecutionEngine.remove_module m2 ee);
114
115   (* run_static_dtors *)
116   ExecutionEngine.run_static_dtors ee;
117
118   (* Show that the data layout binding links and runs.*)
119   let dl = ExecutionEngine.data_layout ee in
120
121   (* Demonstrate that a garbage pointer wasn't returned. *)
122   let ty = DataLayout.intptr_type context dl in
123   if ty != i32_type && ty != i64_type then bomb "target_data did not work";
124
125   (* dispose *)
126   ExecutionEngine.dispose ee
127
128 let () =
129   test_genericvalue ();
130   test_executionengine `Interpreter;
131   test_executionengine `JIT;
132   test_executionengine `MCJIT;
133   ()