Bump version to 19.1.0-rc3
[llvm-project.git] / llvm / test / Bindings / OCaml / target.ml
blobe9465fd0eb2968949f0ab758f059375c3852c2a2
1 (* RUN: rm -rf %t && mkdir -p %t && cp %s %t/target.ml
2 * RUN: %ocamlc -g -w +A -package llvm.target -package llvm.all_backends -linkpkg %t/target.ml -o %t/executable
3 * RUN: %ocamlopt -g -w +A -package llvm.target -package llvm.all_backends -linkpkg %t/target.ml -o %t/executable
4 * RUN: %t/executable %t/bitcode.bc
5 * XFAIL: vg_leak
6 *)
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. *)
12 open Llvm
13 open Llvm_target
15 let () = Llvm_all_backends.initialize ()
17 let context = global_context ()
18 let i32_type = Llvm.i32_type context
19 let i64_type = Llvm.i64_type context
21 (* Tiny unit test framework - really just to help find which line is busted *)
22 let print_checkpoints = false
24 let _ =
25 Printexc.record_backtrace true
27 let assert_equal a b =
28 if a <> b then failwith "assert_equal"
31 (*===-- Fixture -----------------------------------------------------------===*)
33 let filename = Sys.argv.(1)
34 let m = create_module context filename
36 let target = Target.by_triple (Target.default_triple ())
38 let machine = TargetMachine.create (Target.default_triple ()) target
40 (*===-- Data Layout -------------------------------------------------------===*)
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
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)
64 (*===-- Target ------------------------------------------------------------===*)
66 let test_target () =
67 let module T = Target in
68 ignore (T.succ target);
69 ignore (T.name target);
70 ignore (T.description target);
71 ignore (T.has_jit target);
72 ignore (T.has_target_machine target);
73 ignore (T.has_asm_backend target)
76 (*===-- Target Machine ----------------------------------------------------===*)
78 let test_target_machine () =
79 let module TM = TargetMachine in
80 assert_equal (TM.target machine) target;
81 assert_equal (TM.triple machine) (Target.default_triple ());
82 assert_equal (TM.cpu machine) "";
83 assert_equal (TM.features machine) "";
84 ignore (TM.data_layout machine);
85 TM.set_verbose_asm true machine
88 (*===-- Code Emission -----------------------------------------------------===*)
90 let test_code_emission () =
91 TargetMachine.emit_to_file m CodeGenFileType.ObjectFile filename machine;
92 try
93 TargetMachine.emit_to_file m CodeGenFileType.ObjectFile
94 "/nonexistent/file" machine;
95 failwith "must raise"
96 with Llvm_target.Error _ ->
97 ();
99 let buf = TargetMachine.emit_to_memory_buffer m CodeGenFileType.ObjectFile
100 machine in
101 Llvm.MemoryBuffer.dispose buf
104 (*===-- Driver ------------------------------------------------------------===*)
106 let _ =
107 test_target_data ();
108 test_target ();
109 test_target_machine ();
110 test_code_emission ();
111 dispose_module m