Fix the running of ocaml tests.
[oota-llvm.git] / test / Bindings / Ocaml / executionengine.ml
1 (* RUN: %ocamlopt -warn-error A llvm.cmxa llvm_target.cmxa llvm_executionengine.cmxa %s -o %t
2  * RUN: %t
3  *)
4
5 open Llvm
6 open Llvm_executionengine
7 open Llvm_target
8
9 (* Note that this takes a moment to link, so it's best to keep the number of
10    individual tests low. *)
11
12 let context = global_context ()
13 let i8_type = Llvm.i8_type context
14 let i32_type = Llvm.i32_type context
15 let i64_type = Llvm.i64_type context
16 let double_type = Llvm.double_type context
17
18 let bomb msg =
19   prerr_endline msg;
20   exit 2
21
22 let define_main_fn m retval =
23   let fn =
24     let str_arr_type = pointer_type (pointer_type i8_type) in
25     define_function "main" (function_type i32_type [| i32_type;
26                                                       str_arr_type;
27                                                       str_arr_type |]) m in
28   let b = builder_at_end (global_context ()) (entry_block fn) in
29   ignore (build_ret (const_int i32_type retval) b);
30   fn
31
32 let define_plus m =
33   let fn = define_function "plus" (function_type i32_type [| i32_type;
34                                                              i32_type |]) m in
35   let b = builder_at_end (global_context ()) (entry_block fn) in
36   let add = build_add (param fn 0) (param fn 1) "sum" b in
37   ignore (build_ret add b)
38
39 let test_genericvalue () =
40   let tu = (1, 2) in
41   let ptrgv = GenericValue.of_pointer tu in
42   assert (tu = GenericValue.as_pointer ptrgv);
43   
44   let fpgv = GenericValue.of_float double_type 2. in
45   assert (2. = GenericValue.as_float double_type fpgv);
46   
47   let intgv = GenericValue.of_int i32_type 3 in
48   assert (3  = GenericValue.as_int intgv);
49   
50   let i32gv = GenericValue.of_int32 i32_type (Int32.of_int 4) in
51   assert ((Int32.of_int 4) = GenericValue.as_int32 i32gv);
52   
53   let nigv = GenericValue.of_nativeint i32_type (Nativeint.of_int 5) in
54   assert ((Nativeint.of_int 5) = GenericValue.as_nativeint nigv);
55   
56   let i64gv = GenericValue.of_int64 i64_type (Int64.of_int 6) in
57   assert ((Int64.of_int 6) = GenericValue.as_int64 i64gv)
58
59 let test_executionengine () =
60   (* create *)
61   let m = create_module (global_context ()) "test_module" in
62   let main = define_main_fn m 42 in
63   
64   let m2 = create_module (global_context ()) "test_module2" in
65   define_plus m2;
66   
67   let ee = ExecutionEngine.create m in
68   ExecutionEngine.add_module m2 ee;
69   
70   (* run_static_ctors *)
71   ExecutionEngine.run_static_ctors ee;
72   
73   (* run_function_as_main *)
74   let res = ExecutionEngine.run_function_as_main main [|"test"|] [||] ee in
75   if 42 != res then bomb "main did not return 42";
76   
77   (* free_machine_code *)
78   ExecutionEngine.free_machine_code main ee;
79   
80   (* find_function *)
81   match ExecutionEngine.find_function "dne" ee with
82   | Some _ -> raise (Failure "find_function 'dne' failed")
83   | None ->
84   
85   match ExecutionEngine.find_function "plus" ee with
86   | None -> raise (Failure "find_function 'plus' failed")
87   | Some plus ->
88   
89   (* run_function *)
90   let res = ExecutionEngine.run_function plus
91                                          [| GenericValue.of_int i32_type 2;
92                                             GenericValue.of_int i32_type 2 |]
93                                          ee in
94   if 4 != GenericValue.as_int res then bomb "plus did not work";
95   
96   (* remove_module *)
97   Llvm.dispose_module (ExecutionEngine.remove_module m2 ee);
98   
99   (* run_static_dtors *)
100   ExecutionEngine.run_static_dtors ee;
101
102   (* Show that the target data binding links and runs.*)
103   let td = ExecutionEngine.target_data ee in
104
105   (* Demonstrate that a garbage pointer wasn't returned. *)
106   let ty = intptr_type td in
107   if ty != i32_type && ty != i64_type then bomb "target_data did not work";
108   
109   (* dispose *)
110   ExecutionEngine.dispose ee
111
112 let _ =
113   test_genericvalue ();
114   test_executionengine ()