2017-08-16 04:29:24 +08:00
|
|
|
(* RUN: rm -rf %t && mkdir -p %t && cp %s %t/executionengine.ml
|
|
|
|
* RUN: %ocamlc -g -w +A -package llvm.executionengine -linkpkg %t/executionengine.ml -o %t/executable
|
|
|
|
* RUN: %t/executable
|
|
|
|
* RUN: %ocamlopt -g -w +A -package llvm.executionengine -linkpkg %t/executionengine.ml -o %t/executable
|
|
|
|
* RUN: %t/executable
|
2014-10-30 16:29:45 +08:00
|
|
|
* REQUIRES: native, object-emission
|
|
|
|
* XFAIL: vg_leak
|
2007-12-24 00:59:28 +08:00
|
|
|
*)
|
|
|
|
|
|
|
|
open Llvm
|
|
|
|
open Llvm_executionengine
|
2008-03-27 08:27:14 +08:00
|
|
|
open Llvm_target
|
2007-12-24 00:59:28 +08:00
|
|
|
|
|
|
|
(* Note that this takes a moment to link, so it's best to keep the number of
|
|
|
|
individual tests low. *)
|
|
|
|
|
2009-08-20 01:32:24 +08: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-26 02:50:02 +08:00
|
|
|
let () =
|
2014-10-31 17:05:36 +08:00
|
|
|
assert (Llvm_executionengine.initialize ())
|
2014-10-26 02:50:02 +08:00
|
|
|
|
2007-12-24 00:59:28 +08:00
|
|
|
let bomb msg =
|
|
|
|
prerr_endline msg;
|
|
|
|
exit 2
|
|
|
|
|
2014-10-31 17:05:36 +08:00
|
|
|
let define_getglobal m pg =
|
|
|
|
let fn = define_function "getglobal" (function_type i32_type [||]) m in
|
2009-08-19 14:40:29 +08:00
|
|
|
let b = builder_at_end (global_context ()) (entry_block fn) in
|
2014-10-31 17:05:36 +08:00
|
|
|
let g = build_call pg [||] "" b in
|
|
|
|
ignore (build_ret g b);
|
2007-12-24 00:59:28 +08:00
|
|
|
fn
|
|
|
|
|
|
|
|
let define_plus m =
|
|
|
|
let fn = define_function "plus" (function_type i32_type [| i32_type;
|
|
|
|
i32_type |]) m in
|
2009-08-19 14:40:29 +08:00
|
|
|
let b = builder_at_end (global_context ()) (entry_block fn) in
|
2007-12-24 00:59:28 +08:00
|
|
|
let add = build_add (param fn 0) (param fn 1) "sum" b in
|
2014-10-31 17:05:36 +08:00
|
|
|
ignore (build_ret add b);
|
|
|
|
fn
|
2007-12-24 00:59:28 +08:00
|
|
|
|
2014-10-31 17:05:36 +08:00
|
|
|
let test_executionengine () =
|
|
|
|
let open Ctypes in
|
2014-10-26 02:49:56 +08:00
|
|
|
|
2014-10-31 17:05:36 +08:00
|
|
|
(* create *)
|
|
|
|
let m = create_module (global_context ()) "test_module" in
|
|
|
|
let ee = create m in
|
2014-10-26 02:49:56 +08:00
|
|
|
|
2014-10-31 17:05:36 +08:00
|
|
|
(* add plus *)
|
2014-12-24 09:52:51 +08:00
|
|
|
ignore (define_plus m);
|
|
|
|
|
|
|
|
(* declare global variable *)
|
|
|
|
ignore (define_global "globvar" (const_int i32_type 23) m);
|
2014-10-26 02:49:56 +08:00
|
|
|
|
2014-10-31 17:05:36 +08:00
|
|
|
(* add module *)
|
|
|
|
let m2 = create_module (global_context ()) "test_module2" in
|
|
|
|
add_module m2 ee;
|
2014-10-26 02:49:56 +08:00
|
|
|
|
2014-10-31 17:05:36 +08: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-26 02:49:56 +08:00
|
|
|
|
2014-10-31 17:05:36 +08: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-24 00:59:28 +08:00
|
|
|
|
2014-10-31 17:05:36 +08:00
|
|
|
(* add getglobal *)
|
|
|
|
let getglobal = define_getglobal m2 g in*)
|
2014-10-26 02:49:56 +08:00
|
|
|
|
2014-10-31 17:05:36 +08:00
|
|
|
(* run_static_ctors *)
|
|
|
|
run_static_ctors ee;
|
2014-10-26 02:49:56 +08:00
|
|
|
|
2014-12-24 09:52:51 +08: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 17:05:36 +08:00
|
|
|
(* call plus *)
|
|
|
|
let cplusty = Foreign.funptr (int32_t @-> int32_t @-> returning int32_t) in
|
2014-12-24 09:52:51 +08:00
|
|
|
let cplus = get_function_address "plus" cplusty ee in
|
2014-10-31 17:05:36 +08:00
|
|
|
if 4l <> cplus 2l 2l then bomb "plus didn't work";
|
2014-10-26 02:49:56 +08:00
|
|
|
|
2014-10-31 17:05:36 +08: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-26 02:49:56 +08:00
|
|
|
|
2010-03-03 07:59:00 +08:00
|
|
|
(* remove_module *)
|
2014-10-31 17:05:36 +08:00
|
|
|
remove_module m2 ee;
|
|
|
|
dispose_module m2;
|
2014-10-26 02:49:56 +08:00
|
|
|
|
2007-12-24 00:59:28 +08:00
|
|
|
(* run_static_dtors *)
|
2014-10-31 17:05:36 +08:00
|
|
|
run_static_dtors ee;
|
2008-03-27 08:27:14 +08:00
|
|
|
|
2013-11-15 10:51:44 +08:00
|
|
|
(* Show that the data layout binding links and runs.*)
|
2014-10-31 17:05:36 +08:00
|
|
|
let dl = data_layout ee in
|
2008-03-27 08:27:14 +08:00
|
|
|
|
|
|
|
(* Demonstrate that a garbage pointer wasn't returned. *)
|
2013-11-15 10:51:44 +08:00
|
|
|
let ty = DataLayout.intptr_type context dl in
|
2008-03-27 08:27:14 +08:00
|
|
|
if ty != i32_type && ty != i64_type then bomb "target_data did not work";
|
2014-10-26 02:49:56 +08:00
|
|
|
|
2007-12-24 00:59:28 +08:00
|
|
|
(* dispose *)
|
2014-10-31 17:05:36 +08:00
|
|
|
dispose ee
|
2007-12-24 00:59:28 +08:00
|
|
|
|
2014-10-26 02:49:56 +08:00
|
|
|
let () =
|
2014-10-31 17:05:36 +08:00
|
|
|
test_executionengine ();
|
|
|
|
Gc.compact ()
|