Home | History | Annotate | Download | only in OCaml
      1 (* RUN: cp %s %T/target.ml
      2  * RUN: %ocamlc -g -w +A -package llvm.target -package llvm.all_backends -linkpkg %T/target.ml -o %t
      3  * RUN: %ocamlopt -g -w +A -package llvm.target -package llvm.all_backends -linkpkg %T/target.ml -o %t
      4  * RUN: %t %t.bc
      5  * XFAIL: vg_leak
      6  *)
      7 
      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. *)
     11 
     12 open Llvm
     13 open Llvm_target
     14 
     15 let () = Llvm_all_backends.initialize ()
     16 
     17 let context = global_context ()
     18 let i32_type = Llvm.i32_type context
     19 let i64_type = Llvm.i64_type context
     20 
     21 (* Tiny unit test framework - really just to help find which line is busted *)
     22 let print_checkpoints = false
     23 
     24 let _ =
     25   Printexc.record_backtrace true
     26 
     27 let assert_equal a b =
     28   if a <> b then failwith "assert_equal"
     29 
     30 
     31 (*===-- Fixture -----------------------------------------------------------===*)
     32 
     33 let filename = Sys.argv.(1)
     34 let m = create_module context filename
     35 
     36 let target = Target.by_triple (Target.default_triple ())
     37 
     38 let machine = TargetMachine.create (Target.default_triple ()) target
     39 
     40 (*===-- Data Layout -------------------------------------------------------===*)
     41 
     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
     47 
     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)
     62 
     63 
     64 (*===-- Target ------------------------------------------------------------===*)
     65 
     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)
     74 
     75 
     76 (*===-- Target Machine ----------------------------------------------------===*)
     77 
     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;
     86   let pm = PassManager.create () in
     87   TM.add_analysis_passes pm machine
     88 
     89 
     90 (*===-- Code Emission -----------------------------------------------------===*)
     91 
     92 let test_code_emission () =
     93   TargetMachine.emit_to_file m CodeGenFileType.ObjectFile filename machine;
     94   try
     95     TargetMachine.emit_to_file m CodeGenFileType.ObjectFile
     96                                "/nonexistent/file" machine;
     97     failwith "must raise"
     98   with Llvm_target.Error _ ->
     99     ();
    100 
    101   let buf = TargetMachine.emit_to_memory_buffer m CodeGenFileType.ObjectFile
    102                                                 machine in
    103   Llvm.MemoryBuffer.dispose buf
    104 
    105 
    106 (*===-- Driver ------------------------------------------------------------===*)
    107 
    108 let _ =
    109   test_target_data ();
    110   test_target ();
    111   test_target_machine ();
    112   test_code_emission ();
    113   dispose_module m
    114