2014-10-30 08:29:45 +00:00
|
|
|
(* RUN: cp %s %T/executionengine.ml
|
2014-11-03 09:50:53 +00:00
|
|
|
* RUN: %ocamlc -g -warn-error A -package llvm.executionengine -linkpkg %T/executionengine.ml -o %t
|
|
|
|
* RUN: %t
|
|
|
|
* RUN: %ocamlopt -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 () =
|
2014-10-31 09:05:36 +00:00
|
|
|
assert (Llvm_executionengine.initialize ())
|
2014-10-25 18:50:02 +00:00
|
|
|
|
2007-12-23 16:59:28 +00:00
|
|
|
let bomb msg =
|
|
|
|
prerr_endline msg;
|
|
|
|
exit 2
|
|
|
|
|
2014-10-31 09:05:36 +00:00
|
|
|
let define_getglobal m pg =
|
|
|
|
let fn = define_function "getglobal" (function_type i32_type [||]) m in
|
2009-08-19 06:40:29 +00:00
|
|
|
let b = builder_at_end (global_context ()) (entry_block fn) in
|
2014-10-31 09:05:36 +00:00
|
|
|
let g = build_call pg [||] "" b in
|
|
|
|
ignore (build_ret g b);
|
2007-12-23 16:59:28 +00:00
|
|
|
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
|
2014-10-31 09:05:36 +00:00
|
|
|
ignore (build_ret add b);
|
|
|
|
fn
|
2007-12-23 16:59:28 +00:00
|
|
|
|
2014-10-31 09:05:36 +00:00
|
|
|
let test_executionengine () =
|
|
|
|
let open Ctypes in
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2014-10-31 09:05:36 +00:00
|
|
|
(* create *)
|
|
|
|
let m = create_module (global_context ()) "test_module" in
|
|
|
|
let ee = create m in
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2014-10-31 09:05:36 +00:00
|
|
|
(* add plus *)
|
2014-12-24 01:52:51 +00:00
|
|
|
ignore (define_plus m);
|
|
|
|
|
|
|
|
(* declare global variable *)
|
|
|
|
ignore (define_global "globvar" (const_int i32_type 23) m);
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2014-10-31 09:05:36 +00:00
|
|
|
(* add module *)
|
|
|
|
let m2 = create_module (global_context ()) "test_module2" in
|
|
|
|
add_module m2 ee;
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2014-10-31 09:05:36 +00:00
|
|
|
(* add global mapping *)
|
|
|
|
(* BROKEN: see PR20656 *)
|
|
|
|
(* let g = declare_function "g" (function_type i32_type [||]) m2 in
|
|
|
|
let cg = coerce (Foreign.funptr (void @-> returning int32_t)) (ptr void)
|
|
|
|
(fun () -> 42l) in
|
|
|
|
add_global_mapping g cg ee;
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2014-10-31 09:05:36 +00:00
|
|
|
(* check g *)
|
|
|
|
let cg' = get_pointer_to_global g (ptr void) ee in
|
|
|
|
if 0 <> ptr_compare cg cg' then bomb "int pointers to g differ";
|
2007-12-23 16:59:28 +00:00
|
|
|
|
2014-10-31 09:05:36 +00:00
|
|
|
(* add getglobal *)
|
|
|
|
let getglobal = define_getglobal m2 g in*)
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2014-10-31 09:05:36 +00:00
|
|
|
(* run_static_ctors *)
|
|
|
|
run_static_ctors ee;
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2014-12-24 01:52:51 +00:00
|
|
|
(* get a handle on globvar *)
|
|
|
|
let varh = get_global_value_address "globvar" int32_t ee in
|
|
|
|
if 23l <> varh then bomb "get_global_value_address didn't work";
|
|
|
|
|
2014-10-31 09:05:36 +00:00
|
|
|
(* call plus *)
|
|
|
|
let cplusty = Foreign.funptr (int32_t @-> int32_t @-> returning int32_t) in
|
2014-12-24 01:52:51 +00:00
|
|
|
let cplus = get_function_address "plus" cplusty ee in
|
2014-10-31 09:05:36 +00:00
|
|
|
if 4l <> cplus 2l 2l then bomb "plus didn't work";
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2014-10-31 09:05:36 +00:00
|
|
|
(* call getglobal *)
|
|
|
|
(* let cgetglobalty = Foreign.funptr (void @-> returning int32_t) in
|
|
|
|
let cgetglobal = get_pointer_to_global getglobal cgetglobalty ee in
|
|
|
|
if 42l <> cgetglobal () then bomb "getglobal didn't work"; *)
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2010-03-02 23:59:00 +00:00
|
|
|
(* remove_module *)
|
2014-10-31 09:05:36 +00:00
|
|
|
remove_module m2 ee;
|
|
|
|
dispose_module m2;
|
2014-10-25 18:49:56 +00:00
|
|
|
|
2007-12-23 16:59:28 +00:00
|
|
|
(* run_static_dtors *)
|
2014-10-31 09:05:36 +00:00
|
|
|
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.*)
|
2014-10-31 09:05:36 +00:00
|
|
|
let dl = 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 *)
|
2014-10-31 09:05:36 +00:00
|
|
|
dispose ee
|
2007-12-23 16:59:28 +00:00
|
|
|
|
2014-10-25 18:49:56 +00:00
|
|
|
let () =
|
2014-10-31 09:05:36 +00:00
|
|
|
test_executionengine ();
|
|
|
|
Gc.compact ()
|