e532ad55ebf3ebf6557554af79b631a066e1de41
[oota-llvm.git] / test / Bindings / OCaml / target.ml
1 (* RUN: cp %s %T/target.ml
2  * RUN: %ocamlcomp -g -warn-error A -package llvm.target -package llvm.all_backends -linkpkg %T/target.ml -o %t
3  * RUN: %t %t.bc
4  * XFAIL: vg_leak
5  *)
6
7 (* Note: It takes several seconds for ocamlopt to link an executable with
8          libLLVMCore.a, so it's better to write a big test than a bunch of
9          little ones. *)
10
11 open Llvm
12 open Llvm_target
13
14 let () = Llvm_all_backends.initialize ()
15
16 let context = global_context ()
17 let i32_type = Llvm.i32_type context
18 let i64_type = Llvm.i64_type context
19
20 (* Tiny unit test framework - really just to help find which line is busted *)
21 let print_checkpoints = false
22
23 let _ =
24   Printexc.record_backtrace true
25
26 let assert_equal a b =
27   if a <> b then failwith "assert_equal"
28
29
30 (*===-- Fixture -----------------------------------------------------------===*)
31
32 let filename = Sys.argv.(1)
33 let m = create_module context filename
34
35 let target = Target.by_triple (Target.default_triple ())
36
37 let machine = TargetMachine.create (Target.default_triple ()) target
38
39 (*===-- Data Layout -------------------------------------------------------===*)
40
41 let test_target_data () =
42   let module DL = DataLayout in
43   let layout = "e-p:32:32-f64:32:64-v64:32:64-v128:32:128-n32-S32" in
44   let dl     = DL.of_string layout in
45   let sty    = struct_type context [| i32_type; i64_type |] in
46
47   assert_equal (DL.as_string dl) layout;
48   assert_equal (DL.byte_order dl) Endian.Little;
49   assert_equal (DL.pointer_size dl) 4;
50   assert_equal (DL.intptr_type context dl) i32_type;
51   assert_equal (DL.qualified_pointer_size 0 dl) 4;
52   assert_equal (DL.qualified_intptr_type context 0 dl) i32_type;
53   assert_equal (DL.size_in_bits sty dl) (Int64.of_int 96);
54   assert_equal (DL.store_size sty dl) (Int64.of_int 12);
55   assert_equal (DL.abi_size sty dl) (Int64.of_int 12);
56   assert_equal (DL.stack_align sty dl) 4;
57   assert_equal (DL.preferred_align sty dl) 8;
58   assert_equal (DL.preferred_align_of_global (declare_global sty "g" m) dl) 8;
59   assert_equal (DL.element_at_offset sty (Int64.of_int 1) dl) 0;
60   assert_equal (DL.offset_of_element sty 1 dl) (Int64.of_int 4);
61
62   let pm = PassManager.create () in
63   ignore (DL.add_to_pass_manager pm dl)
64
65
66 (*===-- Target ------------------------------------------------------------===*)
67
68 let test_target () =
69   let module T = Target in
70   ignore (T.succ target);
71   ignore (T.name target);
72   ignore (T.description target);
73   ignore (T.has_jit target);
74   ignore (T.has_target_machine target);
75   ignore (T.has_asm_backend target)
76
77
78 (*===-- Target Machine ----------------------------------------------------===*)
79
80 let test_target_machine () =
81   let module TM = TargetMachine in
82   assert_equal (TM.target machine) target;
83   assert_equal (TM.triple machine) (Target.default_triple ());
84   assert_equal (TM.cpu machine) "";
85   assert_equal (TM.features machine) "";
86   ignore (TM.data_layout machine);
87   TM.set_verbose_asm true machine;
88   let pm = PassManager.create () in
89   TM.add_analysis_passes pm machine
90
91
92 (*===-- Code Emission -----------------------------------------------------===*)
93
94 let test_code_emission () =
95   TargetMachine.emit_to_file m CodeGenFileType.ObjectFile filename machine;
96   try
97     TargetMachine.emit_to_file m CodeGenFileType.ObjectFile
98                                "/nonexistent/file" machine;
99     failwith "must raise"
100   with Llvm_target.Error _ ->
101     ();
102
103   let buf = TargetMachine.emit_to_memory_buffer m CodeGenFileType.ObjectFile
104                                                 machine in
105   Llvm.MemoryBuffer.dispose buf
106
107
108 (*===-- Driver ------------------------------------------------------------===*)
109
110 let _ =
111   test_target_data ();
112   test_target ();
113   test_target_machine ();
114   test_code_emission ();
115   dispose_module m