[Alignment][NFC] Instructions::getLoadStoreAlignment
[llvm-complete.git] / test / Bindings / OCaml / executionengine.ml
blobd28416820e6278d2e203880b1527d36ac3a5c7a2
1 (* RUN: rm -rf %t && mkdir -p %t && cp %s %t/executionengine.ml
2 * RUN: %ocamlc -g -w +A -package llvm.executionengine -linkpkg %t/executionengine.ml -o %t/executable
3 * RUN: %t/executable
4 * RUN: %ocamlopt -g -w +A -package llvm.executionengine -linkpkg %t/executionengine.ml -o %t/executable
5 * RUN: %t/executable
6 * REQUIRES: native, object-emission
7 * XFAIL: vg_leak
8 *)
10 open Llvm
11 open Llvm_executionengine
12 open Llvm_target
14 (* Note that this takes a moment to link, so it's best to keep the number of
15 individual tests low. *)
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
23 let () =
24 assert (Llvm_executionengine.initialize ())
26 let bomb msg =
27 prerr_endline msg;
28 exit 2
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);
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);
45 let test_executionengine () =
46 let open Ctypes in
48 (* create *)
49 let m = create_module (global_context ()) "test_module" in
50 let ee = create m in
52 (* add plus *)
53 ignore (define_plus m);
55 (* declare global variable *)
56 ignore (define_global "globvar" (const_int i32_type 23) m);
58 (* add module *)
59 let m2 = create_module (global_context ()) "test_module2" in
60 add_module m2 ee;
62 (* add global mapping *)
63 (* BROKEN: see PR20656 *)
64 (* let g = declare_function "g" (function_type i32_type [||]) m2 in
65 let cg = coerce (Foreign.funptr (void @-> returning int32_t)) (ptr void)
66 (fun () -> 42l) in
67 add_global_mapping g cg ee;
69 (* check g *)
70 let cg' = get_pointer_to_global g (ptr void) ee in
71 if 0 <> ptr_compare cg cg' then bomb "int pointers to g differ";
73 (* add getglobal *)
74 let getglobal = define_getglobal m2 g in*)
76 (* run_static_ctors *)
77 run_static_ctors ee;
79 (* get a handle on globvar *)
80 let varh = get_global_value_address "globvar" int32_t ee in
81 if 23l <> varh then bomb "get_global_value_address didn't work";
83 (* call plus *)
84 let cplusty = Foreign.funptr (int32_t @-> int32_t @-> returning int32_t) in
85 let cplus = get_function_address "plus" cplusty ee in
86 if 4l <> cplus 2l 2l then bomb "plus didn't work";
88 (* call getglobal *)
89 (* let cgetglobalty = Foreign.funptr (void @-> returning int32_t) in
90 let cgetglobal = get_pointer_to_global getglobal cgetglobalty ee in
91 if 42l <> cgetglobal () then bomb "getglobal didn't work"; *)
93 (* remove_module *)
94 remove_module m2 ee;
95 dispose_module m2;
97 (* run_static_dtors *)
98 run_static_dtors ee;
100 (* Show that the data layout binding links and runs.*)
101 let dl = data_layout ee in
103 (* Demonstrate that a garbage pointer wasn't returned. *)
104 let ty = DataLayout.intptr_type context dl in
105 if ty != i32_type && ty != i64_type then bomb "target_data did not work";
107 (* dispose *)
108 dispose ee
110 let () =
111 test_executionengine ();
112 Gc.compact ()