2013-08-23 08:55:32 +08:00
|
|
|
(* RUN: rm -rf %t.builddir
|
|
|
|
* RUN: mkdir -p %t.builddir
|
|
|
|
* RUN: cp %s %t.builddir
|
2013-11-15 10:51:57 +08:00
|
|
|
* RUN: %ocamlopt -g -warn-error A llvm.cmxa llvm_target.cmxa llvm_X86.cmxa %t.builddir/target.ml -o %t
|
2010-08-20 22:51:26 +08:00
|
|
|
* RUN: %t %t.bc
|
2010-09-09 23:50:19 +08:00
|
|
|
* XFAIL: vg_leak
|
2008-03-17 04:08:03 +08:00
|
|
|
*)
|
|
|
|
|
2009-09-04 07:27:31 +08:00
|
|
|
(* Note: It takes several seconds for ocamlopt to link an executable with
|
2008-03-17 04:08:03 +08:00
|
|
|
libLLVMCore.a, so it's better to write a big test than a bunch of
|
|
|
|
little ones. *)
|
|
|
|
|
|
|
|
open Llvm
|
|
|
|
open Llvm_target
|
|
|
|
|
2013-11-15 10:51:57 +08:00
|
|
|
let _ = Llvm_X86.initialize ()
|
2010-08-20 22:51:26 +08:00
|
|
|
|
2009-08-20 01:32:24 +08:00
|
|
|
let context = global_context ()
|
|
|
|
let i32_type = Llvm.i32_type context
|
|
|
|
let i64_type = Llvm.i64_type context
|
|
|
|
|
2008-03-17 04:08:03 +08:00
|
|
|
(* Tiny unit test framework - really just to help find which line is busted *)
|
2010-08-20 22:51:26 +08:00
|
|
|
let print_checkpoints = false
|
|
|
|
|
2013-11-03 16:27:13 +08:00
|
|
|
let _ =
|
|
|
|
Printexc.record_backtrace true
|
|
|
|
|
|
|
|
let assert_equal a b =
|
|
|
|
if a <> b then failwith "assert_equal"
|
2008-03-17 04:08:03 +08:00
|
|
|
|
|
|
|
|
|
|
|
(*===-- Fixture -----------------------------------------------------------===*)
|
|
|
|
|
|
|
|
let filename = Sys.argv.(1)
|
2009-08-20 01:32:24 +08:00
|
|
|
let m = create_module context filename
|
2008-03-17 04:08:03 +08:00
|
|
|
|
2013-11-15 10:51:57 +08:00
|
|
|
let target =
|
|
|
|
match Target.by_name "x86" with
|
|
|
|
| Some t -> t
|
|
|
|
| None -> failwith "need a target"
|
2008-03-17 04:08:03 +08:00
|
|
|
|
2013-11-15 10:51:57 +08:00
|
|
|
let machine =
|
|
|
|
TargetMachine.create ~triple:"i686-linux-gnu" ~cpu:"core2" target
|
|
|
|
|
|
|
|
(*===-- Data Layout -------------------------------------------------------===*)
|
2008-03-17 04:08:03 +08:00
|
|
|
|
|
|
|
let test_target_data () =
|
2013-11-15 10:51:44 +08:00
|
|
|
let module DL = DataLayout in
|
2013-11-03 16:27:13 +08:00
|
|
|
let layout = "e-p:32:32:32-S32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-" ^
|
|
|
|
"f16:16:16-f32:32:32-f64:32:64-f128:128:128-v64:32:64-v128:32:128-" ^
|
|
|
|
"a0:0:64-n32" in
|
2013-11-15 10:51:44 +08:00
|
|
|
let dl = DL.of_string layout in
|
2013-11-03 16:27:13 +08:00
|
|
|
let sty = struct_type context [| i32_type; i64_type |] in
|
2008-03-17 04:08:03 +08:00
|
|
|
|
2013-11-15 10:51:44 +08: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-17 04:08:03 +08:00
|
|
|
|
|
|
|
|
2013-11-15 10:51:57 +08:00
|
|
|
(*===-- Target ------------------------------------------------------------===*)
|
|
|
|
|
|
|
|
let test_target () =
|
|
|
|
let module T = Target in
|
|
|
|
ignore (T.succ target);
|
|
|
|
assert_equal (T.name target) "x86";
|
|
|
|
assert_equal (T.description target) "32-bit X86: Pentium-Pro and above";
|
|
|
|
assert_equal (T.has_jit target) true;
|
|
|
|
assert_equal (T.has_target_machine target) true;
|
|
|
|
assert_equal (T.has_asm_backend target) true
|
|
|
|
|
|
|
|
|
|
|
|
(*===-- Target Machine ----------------------------------------------------===*)
|
|
|
|
|
|
|
|
let test_target_machine () =
|
|
|
|
let module TM = TargetMachine in
|
|
|
|
assert_equal (TM.target machine) target;
|
|
|
|
assert_equal (TM.triple machine) "i686-linux-gnu";
|
|
|
|
assert_equal (TM.cpu machine) "core2";
|
|
|
|
assert_equal (TM.features machine) "";
|
|
|
|
ignore (TM.data_layout machine)
|
|
|
|
|
|
|
|
|
|
|
|
(*===-- 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-17 04:08:03 +08:00
|
|
|
(*===-- Driver ------------------------------------------------------------===*)
|
|
|
|
|
|
|
|
let _ =
|
2013-11-03 16:27:13 +08:00
|
|
|
test_target_data ();
|
2013-11-15 10:51:57 +08:00
|
|
|
test_target ();
|
|
|
|
test_target_machine ();
|
|
|
|
(* test_code_emission (); *) (* broken without AsmParser support *)
|
2008-03-17 04:08:03 +08:00
|
|
|
dispose_module m
|