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
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
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
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 ------------------------------------------------------------===*)
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;
93 TargetMachine.emit_to_file
m CodeGenFileType.ObjectFile
94 "/nonexistent/file" machine;
96 with Llvm_target.Error
_ ->
99 let buf = TargetMachine.emit_to_memory_buffer
m CodeGenFileType.ObjectFile
101 Llvm.MemoryBuffer.dispose
buf
104 (*===-- Driver ------------------------------------------------------------===*)
109 test_target_machine ();
110 test_code_emission ();