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