[OCaml] Rework Llvm_executionengine using ctypes.
[oota-llvm.git] / test / Bindings / Ocaml / executionengine.ml
1 (* RUN: cp %s %T/executionengine.ml
2  * RUN: %ocamlcomp -g -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 ())
23
24 let bomb msg =
25   prerr_endline msg;
26   exit 2
27
28 let define_getglobal m pg =
29   let fn = define_function "getglobal" (function_type i32_type [||]) m in
30   let b = builder_at_end (global_context ()) (entry_block fn) in
31   let g = build_call pg [||] "" b in
32   ignore (build_ret g b);
33   fn
34
35 let define_plus m =
36   let fn = define_function "plus" (function_type i32_type [| i32_type;
37                                                              i32_type |]) m in
38   let b = builder_at_end (global_context ()) (entry_block fn) in
39   let add = build_add (param fn 0) (param fn 1) "sum" b in
40   ignore (build_ret add b);
41   fn
42
43 let test_executionengine () =
44   let open Ctypes in
45
46   (* create *)
47   let m = create_module (global_context ()) "test_module" in
48   let ee = create m in
49
50   (* add plus *)
51   let plus = define_plus m in
52
53   (* add module *)
54   let m2 = create_module (global_context ()) "test_module2" in
55   add_module m2 ee;
56
57   (* add global mapping *)
58   (* BROKEN: see PR20656 *)
59   (* let g = declare_function "g" (function_type i32_type [||]) m2 in
60   let cg = coerce (Foreign.funptr (void @-> returning int32_t)) (ptr void)
61                                   (fun () -> 42l) in
62   add_global_mapping g cg ee;
63
64   (* check g *)
65   let cg' = get_pointer_to_global g (ptr void) ee in
66   if 0 <> ptr_compare cg cg' then bomb "int pointers to g differ";
67
68   (* add getglobal *)
69   let getglobal = define_getglobal m2 g in*)
70
71   (* run_static_ctors *)
72   run_static_ctors ee;
73
74   (* call plus *)
75   let cplusty = Foreign.funptr (int32_t @-> int32_t @-> returning int32_t) in
76   let cplus   = get_pointer_to_global plus cplusty ee in
77   if 4l <> cplus 2l 2l then bomb "plus didn't work";
78
79   (* call getglobal *)
80   (* let cgetglobalty = Foreign.funptr (void @-> returning int32_t) in
81   let cgetglobal   = get_pointer_to_global getglobal cgetglobalty ee in
82   if 42l <> cgetglobal () then bomb "getglobal didn't work"; *)
83
84   (* remove_module *)
85   remove_module m2 ee;
86   dispose_module m2;
87
88   (* run_static_dtors *)
89   run_static_dtors ee;
90
91   (* Show that the data layout binding links and runs.*)
92   let dl = data_layout ee in
93
94   (* Demonstrate that a garbage pointer wasn't returned. *)
95   let ty = DataLayout.intptr_type context dl in
96   if ty != i32_type && ty != i64_type then bomb "target_data did not work";
97
98   (* dispose *)
99   dispose ee
100
101 let () =
102   test_executionengine ();
103   Gc.compact ()