2014-10-30 08:29:45 +00:00
|
|
|
(* RUN: cp %s %T/executionengine.ml
|
2014-10-30 08:29:57 +00:00
|
|
|
* RUN: %ocamlcomp -g -warn-error A -package llvm.executionengine -linkpkg %T/executionengine.ml -o %t
|
2010-08-20 14:51:26 +00:00
|
|
|
* RUN: %t
|
2014-10-30 08:29:45 +00:00
|
|
|
* REQUIRES: native, object-emission
|
|
|
|
* XFAIL: vg_leak
|
2007-12-23 16:59:28 +00:00
|
|
|
*)
|
|
|
|
|
|
|
|
open Llvm
|
|
|
|
open Llvm_executionengine
|
2008-03-27 00:27:14 +00:00
|
|
|
open Llvm_target
|
2007-12-23 16:59:28 +00:00
|
|
|
|
|
|
|
(* Note that this takes a moment to link, so it's best to keep the number of
|
|
|
|
individual tests low. *)
|
|
|
|
|
2009-08-19 17:32:24 +00:00
|
|
|
let context = global_context ()
|
|
|
|
let i8_type = Llvm.i8_type context
|
|
|
|
let i32_type = Llvm.i32_type context
|
|
|
|
let i64_type = Llvm.i64_type context
|
|
|
|
let double_type = Llvm.double_type context
|
|
|
|
|
2014-10-25 18:50:02 +00:00
|
|
|
let () =
|
|
|
|
assert (Llvm_executionengine.initialize_native_target ())
|
|
|
|
|
2007-12-23 16:59:28 +00:00
|
|
|
let bomb msg =
|
|
|
|
prerr_endline msg;
|
|
|
|
exit 2
|
|
|
|
|
|
|
|
let define_main_fn m retval =
|
|
|
|
let fn =
|
|
|
|
let str_arr_type = pointer_type (pointer_type i8_type) in
|
|
|
|
define_function "main" (function_type i32_type [| i32_type;
|
|
|
|
str_arr_type;
|
|
|
|
str_arr_type |]) m in
|
2009-08-19 06:40:29 +00:00
|
|
|
let b = builder_at_end (global_context ()) (entry_block fn) in
|
2007-12-23 16:59:28 +00:00
|
|
|
ignore (build_ret (const_int i32_type retval) b);
|
|
|
|
fn
|
|
|
|
|
|
|
|
let define_plus m =
|
|
|
|
let fn = define_function "plus" (function_type i32_type [| i32_type;
|
|
|
|
i32_type |]) m in
|
2009-08-19 06:40:29 +00:00
|
|
|
let b = builder_at_end (global_context ()) (entry_block fn) in
|
2007-12-23 16:59:28 +00:00
|
|
|
let add = build_add (param fn 0) (param fn 1) "sum" b in
|
|
|
|
ignore (build_ret add b)
|
|
|
|
|
|
|
|
let test_genericvalue () =
|
|
|
|
let tu = (1, 2) in
|
|
|
|
let ptrgv = GenericValue.of_pointer tu in
|
|
|
|
assert (tu = GenericValue.as_pointer ptrgv);
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2007-12-23 16:59:28 +00:00
|
|
|
let fpgv = GenericValue.of_float double_type 2. in
|
|
|
|
assert (2. = GenericValue.as_float double_type fpgv);
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2007-12-23 16:59:28 +00:00
|
|
|
let intgv = GenericValue.of_int i32_type 3 in
|
|
|
|
assert (3 = GenericValue.as_int intgv);
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2007-12-30 22:48:58 +00:00
|
|
|
let i32gv = GenericValue.of_int32 i32_type (Int32.of_int 4) in
|
|
|
|
assert ((Int32.of_int 4) = GenericValue.as_int32 i32gv);
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2007-12-30 22:48:58 +00:00
|
|
|
let nigv = GenericValue.of_nativeint i32_type (Nativeint.of_int 5) in
|
|
|
|
assert ((Nativeint.of_int 5) = GenericValue.as_nativeint nigv);
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2007-12-30 22:48:58 +00:00
|
|
|
let i64gv = GenericValue.of_int64 i64_type (Int64.of_int 6) in
|
|
|
|
assert ((Int64.of_int 6) = GenericValue.as_int64 i64gv)
|
2007-12-23 16:59:28 +00:00
|
|
|
|
2014-10-25 18:49:56 +00:00
|
|
|
let test_executionengine engine =
|
2007-12-23 16:59:28 +00:00
|
|
|
(* create *)
|
2009-08-19 06:40:29 +00:00
|
|
|
let m = create_module (global_context ()) "test_module" in
|
2007-12-23 16:59:28 +00:00
|
|
|
let main = define_main_fn m 42 in
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2009-08-19 06:40:29 +00:00
|
|
|
let m2 = create_module (global_context ()) "test_module2" in
|
2007-12-23 16:59:28 +00:00
|
|
|
define_plus m2;
|
2014-10-25 18:49:56 +00:00
|
|
|
|
|
|
|
let ee =
|
|
|
|
match engine with
|
|
|
|
| `Interpreter -> ExecutionEngine.create_interpreter m
|
|
|
|
| `JIT -> ExecutionEngine.create_jit m 0
|
|
|
|
| `MCJIT -> ExecutionEngine.create_mcjit m ExecutionEngine.default_compiler_options
|
|
|
|
in
|
2010-03-02 23:59:00 +00:00
|
|
|
ExecutionEngine.add_module m2 ee;
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2007-12-23 16:59:28 +00:00
|
|
|
(* run_static_ctors *)
|
|
|
|
ExecutionEngine.run_static_ctors ee;
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2007-12-23 16:59:28 +00:00
|
|
|
(* run_function_as_main *)
|
|
|
|
let res = ExecutionEngine.run_function_as_main main [|"test"|] [||] ee in
|
|
|
|
if 42 != res then bomb "main did not return 42";
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2007-12-23 16:59:28 +00:00
|
|
|
(* free_machine_code *)
|
|
|
|
ExecutionEngine.free_machine_code main ee;
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2007-12-23 16:59:28 +00:00
|
|
|
(* find_function *)
|
|
|
|
match ExecutionEngine.find_function "dne" ee with
|
|
|
|
| Some _ -> raise (Failure "find_function 'dne' failed")
|
|
|
|
| None ->
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2007-12-23 16:59:28 +00:00
|
|
|
match ExecutionEngine.find_function "plus" ee with
|
|
|
|
| None -> raise (Failure "find_function 'plus' failed")
|
|
|
|
| Some plus ->
|
2014-10-25 18:49:56 +00:00
|
|
|
|
|
|
|
begin match engine with
|
|
|
|
| `MCJIT -> () (* Currently can only invoke 0-ary functions *)
|
|
|
|
| `JIT -> () (* JIT is now a shim around MCJIT, jokes on you *)
|
|
|
|
| _ ->
|
|
|
|
(* run_function *)
|
|
|
|
let res = ExecutionEngine.run_function plus
|
|
|
|
[| GenericValue.of_int i32_type 2;
|
|
|
|
GenericValue.of_int i32_type 2 |]
|
|
|
|
ee in
|
|
|
|
if 4 != GenericValue.as_int res then bomb "plus did not work";
|
|
|
|
end;
|
|
|
|
|
2010-03-02 23:59:00 +00:00
|
|
|
(* remove_module *)
|
|
|
|
Llvm.dispose_module (ExecutionEngine.remove_module m2 ee);
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2007-12-23 16:59:28 +00:00
|
|
|
(* run_static_dtors *)
|
|
|
|
ExecutionEngine.run_static_dtors ee;
|
2008-03-27 00:27:14 +00:00
|
|
|
|
2013-11-15 02:51:44 +00:00
|
|
|
(* Show that the data layout binding links and runs.*)
|
|
|
|
let dl = ExecutionEngine.data_layout ee in
|
2008-03-27 00:27:14 +00:00
|
|
|
|
|
|
|
(* Demonstrate that a garbage pointer wasn't returned. *)
|
2013-11-15 02:51:44 +00:00
|
|
|
let ty = DataLayout.intptr_type context dl in
|
2008-03-27 00:27:14 +00:00
|
|
|
if ty != i32_type && ty != i64_type then bomb "target_data did not work";
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2007-12-23 16:59:28 +00:00
|
|
|
(* dispose *)
|
|
|
|
ExecutionEngine.dispose ee
|
|
|
|
|
2014-10-25 18:49:56 +00:00
|
|
|
let () =
|
2007-12-23 16:59:28 +00:00
|
|
|
test_genericvalue ();
|
2014-10-25 18:49:56 +00:00
|
|
|
test_executionengine `Interpreter;
|
|
|
|
test_executionengine `JIT;
|
|
|
|
test_executionengine `MCJIT;
|
|
|
|
()
|