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