[OCaml] Run tests twice, with ocamlc and ocamlopt (if available)
[oota-llvm.git] / test / Bindings / OCaml / executionengine.ml
1 (* RUN: cp %s %T/executionengine.ml
2  * RUN: %ocamlc -g -warn-error A -package llvm.executionengine -linkpkg %T/executionengine.ml -o %t
3  * RUN: %t
4  * RUN: %ocamlopt -g -warn-error A -package llvm.executionengine -linkpkg %T/executionengine.ml -o %t
5  * RUN: %t
6  * REQUIRES: native, object-emission
7  * XFAIL: vg_leak
8  *)
9
10 open Llvm
11 open Llvm_executionengine
12 open Llvm_target
13
14 (* Note that this takes a moment to link, so it's best to keep the number of
15    individual tests low. *)
16
17 let context = global_context ()
18 let i8_type = Llvm.i8_type context
19 let i32_type = Llvm.i32_type context
20 let i64_type = Llvm.i64_type context
21 let double_type = Llvm.double_type context
22
23 let () =
24   assert (Llvm_executionengine.initialize ())
25
26 let bomb msg =
27   prerr_endline msg;
28   exit 2
29
30 let define_getglobal m pg =
31   let fn = define_function "getglobal" (function_type i32_type [||]) m in
32   let b = builder_at_end (global_context ()) (entry_block fn) in
33   let g = build_call pg [||] "" b in
34   ignore (build_ret g b);
35   fn
36
37 let define_plus m =
38   let fn = define_function "plus" (function_type i32_type [| i32_type;
39                                                              i32_type |]) m in
40   let b = builder_at_end (global_context ()) (entry_block fn) in
41   let add = build_add (param fn 0) (param fn 1) "sum" b in
42   ignore (build_ret add b);
43   fn
44
45 let test_executionengine () =
46   let open Ctypes in
47
48   (* create *)
49   let m = create_module (global_context ()) "test_module" in
50   let ee = create m in
51
52   (* add plus *)
53   let plus = define_plus m in
54
55   (* add module *)
56   let m2 = create_module (global_context ()) "test_module2" in
57   add_module m2 ee;
58
59   (* add global mapping *)
60   (* BROKEN: see PR20656 *)
61   (* let g = declare_function "g" (function_type i32_type [||]) m2 in
62   let cg = coerce (Foreign.funptr (void @-> returning int32_t)) (ptr void)
63                                   (fun () -> 42l) in
64   add_global_mapping g cg ee;
65
66   (* check g *)
67   let cg' = get_pointer_to_global g (ptr void) ee in
68   if 0 <> ptr_compare cg cg' then bomb "int pointers to g differ";
69
70   (* add getglobal *)
71   let getglobal = define_getglobal m2 g in*)
72
73   (* run_static_ctors *)
74   run_static_ctors ee;
75
76   (* call plus *)
77   let cplusty = Foreign.funptr (int32_t @-> int32_t @-> returning int32_t) in
78   let cplus   = get_pointer_to_global plus cplusty ee in
79   if 4l <> cplus 2l 2l then bomb "plus didn't work";
80
81   (* call getglobal *)
82   (* let cgetglobalty = Foreign.funptr (void @-> returning int32_t) in
83   let cgetglobal   = get_pointer_to_global getglobal cgetglobalty ee in
84   if 42l <> cgetglobal () then bomb "getglobal didn't work"; *)
85
86   (* remove_module *)
87   remove_module m2 ee;
88   dispose_module m2;
89
90   (* run_static_dtors *)
91   run_static_dtors ee;
92
93   (* Show that the data layout binding links and runs.*)
94   let dl = data_layout ee in
95
96   (* Demonstrate that a garbage pointer wasn't returned. *)
97   let ty = DataLayout.intptr_type context dl in
98   if ty != i32_type && ty != i64_type then bomb "target_data did not work";
99
100   (* dispose *)
101   dispose ee
102
103 let () =
104   test_executionengine ();
105   Gc.compact ()