xref: /llvm-project/llvm/test/Bindings/OCaml/executionengine.ml (revision 81ccfeb86afc13e1ef3d4872452c855060b8f67e)
1(* RUN: rm -rf %t && mkdir -p %t && cp %s %t/executionengine.ml
2 * RUN: %ocamlc -g -w +A -thread -package ctypes.foreign,llvm.executionengine -linkpkg %t/executionengine.ml -o %t/executable
3 * RUN: %t/executable
4 * RUN: %ocamlopt -g -w +A -thread -package ctypes.foreign,llvm.executionengine -linkpkg %t/executionengine.ml -o %t/executable
5 * RUN: %t/executable
6 * REQUIRES: native
7 * XFAIL: vg_leak
8 *)
9
10open Llvm
11open Llvm_executionengine
12open 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
17let context = global_context ()
18let i8_type = Llvm.i8_type context
19let i32_type = Llvm.i32_type context
20let i64_type = Llvm.i64_type context
21let double_type = Llvm.double_type context
22
23let () =
24  assert (Llvm_executionengine.initialize ())
25
26let bomb msg =
27  prerr_endline msg;
28  exit 2
29
30let define_getglobal m pg =
31  let fty = function_type i32_type [||] in
32  let fn = define_function "getglobal" fty m in
33  let b = builder_at_end (global_context ()) (entry_block fn) in
34  let g = build_call fty pg [||] "" b in
35  ignore (build_ret g b);
36  fn
37
38let 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  fn
45
46let test_executionengine () =
47  let open Ctypes in
48
49  (* create *)
50  let m = create_module (global_context ()) "test_module" in
51  let ee = create m in
52
53  (* add plus *)
54  ignore (define_plus m);
55
56  (* declare global variable *)
57  ignore (define_global "globvar" (const_int i32_type 23) m);
58
59  (* add module *)
60  let m2 = create_module (global_context ()) "test_module2" in
61  add_module m2 ee;
62
63  (* add global mapping *)
64  (* BROKEN: see PR20656 *)
65  (* let g = declare_function "g" (function_type i32_type [||]) m2 in
66  let cg = coerce (Foreign.funptr (void @-> returning int32_t)) (ptr void)
67                                  (fun () -> 42l) in
68  add_global_mapping g cg ee;
69
70  (* check g *)
71  let cg' = get_pointer_to_global g (ptr void) ee in
72  if 0 <> ptr_compare cg cg' then bomb "int pointers to g differ";
73
74  (* add getglobal *)
75  let getglobal = define_getglobal m2 g in*)
76
77  (* run_static_ctors *)
78  run_static_ctors ee;
79
80  (* get a handle on globvar *)
81  let varh    = get_global_value_address "globvar" int32_t ee in
82  if 23l <> varh then bomb "get_global_value_address didn't work";
83
84  (* call plus *)
85  let cplusty = Foreign.funptr (int32_t @-> int32_t @-> returning int32_t) in
86  let cplus   = get_function_address "plus" cplusty ee in
87  if 4l <> cplus 2l 2l then bomb "plus didn't work";
88
89  (* call getglobal *)
90  (* let cgetglobalty = Foreign.funptr (void @-> returning int32_t) in
91  let cgetglobal   = get_pointer_to_global getglobal cgetglobalty ee in
92  if 42l <> cgetglobal () then bomb "getglobal didn't work"; *)
93
94  (* remove_module *)
95  remove_module m2 ee;
96  dispose_module m2;
97
98  (* run_static_dtors *)
99  run_static_dtors ee;
100
101  (* Show that the data layout binding links and runs.*)
102  let dl = data_layout ee in
103
104  (* Demonstrate that a garbage pointer wasn't returned. *)
105  let ty = DataLayout.intptr_type context dl in
106  if ty != i32_type && ty != i64_type then bomb "target_data did not work";
107
108  (* dispose *)
109  dispose ee
110
111let () =
112  test_executionengine ();
113  Gc.compact ()
114