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