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