2014-10-30 08:29:45 +00:00
|
|
|
(* RUN: cp %s %T/target.ml
|
2014-11-03 09:50:53 +00:00
|
|
|
* RUN: %ocamlc -g -warn-error A -package llvm.target -package llvm.all_backends -linkpkg %T/target.ml -o %t
|
|
|
|
* RUN: %ocamlopt -g -warn-error A -package llvm.target -package llvm.all_backends -linkpkg %T/target.ml -o %t
|
2010-08-20 14:51:26 +00:00
|
|
|
* RUN: %t %t.bc
|
2010-09-09 15:50:19 +00:00
|
|
|
* XFAIL: vg_leak
|
2008-03-16 20:08:03 +00:00
|
|
|
*)
|
|
|
|
|
2009-09-03 23:27:31 +00:00
|
|
|
(* Note: It takes several seconds for ocamlopt to link an executable with
|
2008-03-16 20:08:03 +00:00
|
|
|
libLLVMCore.a, so it's better to write a big test than a bunch of
|
|
|
|
little ones. *)
|
|
|
|
|
|
|
|
open Llvm
|
|
|
|
open Llvm_target
|
|
|
|
|
2014-10-30 08:30:01 +00:00
|
|
|
let () = Llvm_all_backends.initialize ()
|
2010-08-20 14:51:26 +00:00
|
|
|
|
2009-08-19 17:32:24 +00:00
|
|
|
let context = global_context ()
|
|
|
|
let i32_type = Llvm.i32_type context
|
|
|
|
let i64_type = Llvm.i64_type context
|
|
|
|
|
2008-03-16 20:08:03 +00:00
|
|
|
(* Tiny unit test framework - really just to help find which line is busted *)
|
2010-08-20 14:51:26 +00:00
|
|
|
let print_checkpoints = false
|
|
|
|
|
2013-11-03 08:27:13 +00:00
|
|
|
let _ =
|
|
|
|
Printexc.record_backtrace true
|
|
|
|
|
|
|
|
let assert_equal a b =
|
|
|
|
if a <> b then failwith "assert_equal"
|
2008-03-16 20:08:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
(*===-- Fixture -----------------------------------------------------------===*)
|
|
|
|
|
|
|
|
let filename = Sys.argv.(1)
|
2009-08-19 17:32:24 +00:00
|
|
|
let m = create_module context filename
|
2008-03-16 20:08:03 +00:00
|
|
|
|
2013-11-15 03:19:08 +00:00
|
|
|
let target = Target.by_triple (Target.default_triple ())
|
2008-03-16 20:08:03 +00:00
|
|
|
|
2013-11-15 03:19:08 +00:00
|
|
|
let machine = TargetMachine.create (Target.default_triple ()) target
|
2013-11-15 02:51:57 +00:00
|
|
|
|
|
|
|
(*===-- Data Layout -------------------------------------------------------===*)
|
2008-03-16 20:08:03 +00:00
|
|
|
|
|
|
|
let test_target_data () =
|
2013-11-15 02:51:44 +00:00
|
|
|
let module DL = DataLayout in
|
2013-12-19 23:38:09 +00:00
|
|
|
let layout = "e-p:32:32-f64:32:64-v64:32:64-v128:32:128-n32-S32" in
|
2013-11-15 02:51:44 +00:00
|
|
|
let dl = DL.of_string layout in
|
2013-11-03 08:27:13 +00:00
|
|
|
let sty = struct_type context [| i32_type; i64_type |] in
|
2014-06-09 18:28:47 +00:00
|
|
|
|
2013-11-15 02:51:44 +00:00
|
|
|
assert_equal (DL.as_string dl) layout;
|
|
|
|
assert_equal (DL.byte_order dl) Endian.Little;
|
|
|
|
assert_equal (DL.pointer_size dl) 4;
|
|
|
|
assert_equal (DL.intptr_type context dl) i32_type;
|
|
|
|
assert_equal (DL.qualified_pointer_size 0 dl) 4;
|
|
|
|
assert_equal (DL.qualified_intptr_type context 0 dl) i32_type;
|
|
|
|
assert_equal (DL.size_in_bits sty dl) (Int64.of_int 96);
|
|
|
|
assert_equal (DL.store_size sty dl) (Int64.of_int 12);
|
|
|
|
assert_equal (DL.abi_size sty dl) (Int64.of_int 12);
|
|
|
|
assert_equal (DL.stack_align sty dl) 4;
|
|
|
|
assert_equal (DL.preferred_align sty dl) 8;
|
|
|
|
assert_equal (DL.preferred_align_of_global (declare_global sty "g" m) dl) 8;
|
|
|
|
assert_equal (DL.element_at_offset sty (Int64.of_int 1) dl) 0;
|
|
|
|
assert_equal (DL.offset_of_element sty 1 dl) (Int64.of_int 4);
|
|
|
|
|
|
|
|
let pm = PassManager.create () in
|
|
|
|
ignore (DL.add_to_pass_manager pm dl)
|
2008-03-16 20:08:03 +00:00
|
|
|
|
|
|
|
|
2013-11-15 02:51:57 +00:00
|
|
|
(*===-- Target ------------------------------------------------------------===*)
|
|
|
|
|
|
|
|
let test_target () =
|
|
|
|
let module T = Target in
|
|
|
|
ignore (T.succ target);
|
2013-11-15 03:19:08 +00:00
|
|
|
ignore (T.name target);
|
|
|
|
ignore (T.description target);
|
|
|
|
ignore (T.has_jit target);
|
|
|
|
ignore (T.has_target_machine target);
|
|
|
|
ignore (T.has_asm_backend target)
|
2013-11-15 02:51:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
(*===-- Target Machine ----------------------------------------------------===*)
|
|
|
|
|
|
|
|
let test_target_machine () =
|
|
|
|
let module TM = TargetMachine in
|
|
|
|
assert_equal (TM.target machine) target;
|
2013-11-15 03:19:08 +00:00
|
|
|
assert_equal (TM.triple machine) (Target.default_triple ());
|
|
|
|
assert_equal (TM.cpu machine) "";
|
2013-11-15 02:51:57 +00:00
|
|
|
assert_equal (TM.features machine) "";
|
2014-06-09 18:28:47 +00:00
|
|
|
ignore (TM.data_layout machine);
|
2014-10-29 08:16:14 +00:00
|
|
|
TM.set_verbose_asm true machine;
|
|
|
|
let pm = PassManager.create () in
|
|
|
|
TM.add_analysis_passes pm machine
|
2013-11-15 02:51:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
(*===-- Code Emission -----------------------------------------------------===*)
|
|
|
|
|
|
|
|
let test_code_emission () =
|
|
|
|
TargetMachine.emit_to_file m CodeGenFileType.ObjectFile filename machine;
|
|
|
|
try
|
|
|
|
TargetMachine.emit_to_file m CodeGenFileType.ObjectFile
|
|
|
|
"/nonexistent/file" machine;
|
|
|
|
failwith "must raise"
|
|
|
|
with Llvm_target.Error _ ->
|
|
|
|
();
|
|
|
|
|
|
|
|
let buf = TargetMachine.emit_to_memory_buffer m CodeGenFileType.ObjectFile
|
|
|
|
machine in
|
|
|
|
Llvm.MemoryBuffer.dispose buf
|
|
|
|
|
|
|
|
|
2008-03-16 20:08:03 +00:00
|
|
|
(*===-- Driver ------------------------------------------------------------===*)
|
|
|
|
|
|
|
|
let _ =
|
2013-11-03 08:27:13 +00:00
|
|
|
test_target_data ();
|
2013-11-15 02:51:57 +00:00
|
|
|
test_target ();
|
|
|
|
test_target_machine ();
|
2014-10-30 08:30:01 +00:00
|
|
|
test_code_emission ();
|
2008-03-16 20:08:03 +00:00
|
|
|
dispose_module m
|