aboutsummaryrefslogtreecommitdiffstats
path: root/test/Bindings/Ocaml/target.ml
diff options
context:
space:
mode:
Diffstat (limited to 'test/Bindings/Ocaml/target.ml')
-rw-r--r--test/Bindings/Ocaml/target.ml104
1 files changed, 81 insertions, 23 deletions
diff --git a/test/Bindings/Ocaml/target.ml b/test/Bindings/Ocaml/target.ml
index 7a35a79..d69fb0e 100644
--- a/test/Bindings/Ocaml/target.ml
+++ b/test/Bindings/Ocaml/target.ml
@@ -1,5 +1,9 @@
-(* RUN: %ocamlopt -warn-error A llvm.cmxa llvm_target.cmxa %s -o %t
+(* RUN: rm -rf %t.builddir
+ * RUN: mkdir -p %t.builddir
+ * RUN: cp %s %t.builddir
+ * RUN: %ocamlopt -g -warn-error A llvm.cmxa llvm_target.cmxa llvm_executionengine.cmxa %t.builddir/target.ml -o %t
* RUN: %t %t.bc
+ * REQUIRES: native, object-emission
* XFAIL: vg_leak
*)
@@ -10,6 +14,7 @@
open Llvm
open Llvm_target
+let _ = Llvm_executionengine.initialize_native_target ()
let context = global_context ()
let i32_type = Llvm.i32_type context
@@ -18,10 +23,11 @@ let i64_type = Llvm.i64_type context
(* Tiny unit test framework - really just to help find which line is busted *)
let print_checkpoints = false
-let suite name f =
- if print_checkpoints then
- prerr_endline (name ^ ":");
- f ()
+let _ =
+ Printexc.record_backtrace true
+
+let assert_equal a b =
+ if a <> b then failwith "assert_equal"
(*===-- Fixture -----------------------------------------------------------===*)
@@ -29,31 +35,83 @@ let suite name f =
let filename = Sys.argv.(1)
let m = create_module context filename
+let target = Target.by_triple (Target.default_triple ())
-(*===-- Target Data -------------------------------------------------------===*)
+let machine = TargetMachine.create (Target.default_triple ()) target
+
+(*===-- Data Layout -------------------------------------------------------===*)
let test_target_data () =
- let td = DataLayout.create (target_triple m) in
- let sty = struct_type context [| i32_type; i64_type |] in
-
- ignore (DataLayout.as_string td);
- ignore (byte_order td);
- ignore (pointer_size td);
- ignore (intptr_type td);
- ignore (size_in_bits td sty);
- ignore (store_size td sty);
- ignore (abi_size td sty);
- ignore (stack_align td sty);
- ignore (preferred_align td sty);
- ignore (preferred_align_of_global td (declare_global sty "g" m));
- ignore (element_at_offset td sty (Int64.of_int 1));
- ignore (offset_of_element td sty 1);
+ let module DL = DataLayout in
+ let layout = "e-p:32:32:32-S32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-" ^
+ "f16:16:16-f32:32:32-f64:32:64-f128:128:128-v64:32:64-v128:32:128-" ^
+ "a0:0:64-n32" in
+ let dl = DL.of_string layout in
+ let sty = struct_type context [| i32_type; i64_type |] in
- DataLayout.dispose td
+ assert_equal (DL.as_string dl) layout;
+ assert_equal (DL.byte_order dl) Endian.Little;
+ assert_equal (DL.pointer_size dl) 4;
+ assert_equal (DL.intptr_type context dl) i32_type;
+ assert_equal (DL.qualified_pointer_size 0 dl) 4;
+ assert_equal (DL.qualified_intptr_type context 0 dl) i32_type;
+ assert_equal (DL.size_in_bits sty dl) (Int64.of_int 96);
+ assert_equal (DL.store_size sty dl) (Int64.of_int 12);
+ assert_equal (DL.abi_size sty dl) (Int64.of_int 12);
+ assert_equal (DL.stack_align sty dl) 4;
+ assert_equal (DL.preferred_align sty dl) 8;
+ assert_equal (DL.preferred_align_of_global (declare_global sty "g" m) dl) 8;
+ assert_equal (DL.element_at_offset sty (Int64.of_int 1) dl) 0;
+ assert_equal (DL.offset_of_element sty 1 dl) (Int64.of_int 4);
+
+ let pm = PassManager.create () in
+ ignore (DL.add_to_pass_manager pm dl)
+
+
+(*===-- Target ------------------------------------------------------------===*)
+
+let test_target () =
+ let module T = Target in
+ ignore (T.succ target);
+ ignore (T.name target);
+ ignore (T.description target);
+ ignore (T.has_jit target);
+ ignore (T.has_target_machine target);
+ ignore (T.has_asm_backend target)
+
+
+(*===-- Target Machine ----------------------------------------------------===*)
+
+let test_target_machine () =
+ let module TM = TargetMachine in
+ assert_equal (TM.target machine) target;
+ assert_equal (TM.triple machine) (Target.default_triple ());
+ assert_equal (TM.cpu machine) "";
+ assert_equal (TM.features machine) "";
+ ignore (TM.data_layout machine)
+
+
+(*===-- Code Emission -----------------------------------------------------===*)
+
+let test_code_emission () =
+ TargetMachine.emit_to_file m CodeGenFileType.ObjectFile filename machine;
+ try
+ TargetMachine.emit_to_file m CodeGenFileType.ObjectFile
+ "/nonexistent/file" machine;
+ failwith "must raise"
+ with Llvm_target.Error _ ->
+ ();
+
+ let buf = TargetMachine.emit_to_memory_buffer m CodeGenFileType.ObjectFile
+ machine in
+ Llvm.MemoryBuffer.dispose buf
(*===-- Driver ------------------------------------------------------------===*)
let _ =
- suite "target data" test_target_data;
+ test_target_data ();
+ test_target ();
+ test_target_machine ();
+ (* test_code_emission (); *) (* broken without AsmParser support *)
dispose_module m