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