1(*===-- llvm_target.ml - LLVM OCaml Interface ------------------*- OCaml -*-===*
2 *
3 *                     The LLVM Compiler Infrastructure
4 *
5 * This file is distributed under the University of Illinois Open Source
6 * License. See LICENSE.TXT for details.
7 *
8 *===----------------------------------------------------------------------===*)
9
10module Endian = struct
11  type t =
12  | Big
13  | Little
14end
15
16module CodeGenOptLevel = struct
17  type t =
18  | None
19  | Less
20  | Default
21  | Aggressive
22end
23
24module RelocMode = struct
25  type t =
26  | Default
27  | Static
28  | PIC
29  | DynamicNoPIC
30end
31
32module CodeModel = struct
33  type t =
34  | Default
35  | JITDefault
36  | Small
37  | Kernel
38  | Medium
39  | Large
40end
41
42module CodeGenFileType = struct
43  type t =
44  | AssemblyFile
45  | ObjectFile
46end
47
48exception Error of string
49
50let () = Callback.register_exception "Llvm_target.Error" (Error "")
51
52module DataLayout = struct
53  type t
54
55  external of_string : string -> t = "llvm_datalayout_of_string"
56  external as_string : t -> string = "llvm_datalayout_as_string"
57  external add_to_pass_manager : [<Llvm.PassManager.any]
58                                 Llvm.PassManager.t -> t -> unit
59                               = "llvm_datalayout_add_to_pass_manager"
60  external byte_order : t -> Endian.t = "llvm_datalayout_byte_order"
61  external pointer_size : t -> int = "llvm_datalayout_pointer_size"
62  external intptr_type : Llvm.llcontext -> t -> Llvm.lltype
63                       = "llvm_datalayout_intptr_type"
64  external qualified_pointer_size : int -> t -> int
65                                  = "llvm_datalayout_qualified_pointer_size"
66  external qualified_intptr_type : Llvm.llcontext -> int -> t -> Llvm.lltype
67                                 = "llvm_datalayout_qualified_intptr_type"
68  external size_in_bits : Llvm.lltype -> t -> Int64.t
69                        = "llvm_datalayout_size_in_bits"
70  external store_size : Llvm.lltype -> t -> Int64.t
71                      = "llvm_datalayout_store_size"
72  external abi_size : Llvm.lltype -> t -> Int64.t
73                    = "llvm_datalayout_abi_size"
74  external abi_align : Llvm.lltype -> t -> int
75                     = "llvm_datalayout_abi_align"
76  external stack_align : Llvm.lltype -> t -> int
77                       = "llvm_datalayout_stack_align"
78  external preferred_align : Llvm.lltype -> t -> int
79                           = "llvm_datalayout_preferred_align"
80  external preferred_align_of_global : Llvm.llvalue -> t -> int
81                                   = "llvm_datalayout_preferred_align_of_global"
82  external element_at_offset : Llvm.lltype -> Int64.t -> t -> int
83                             = "llvm_datalayout_element_at_offset"
84  external offset_of_element : Llvm.lltype -> int -> t -> Int64.t
85                             = "llvm_datalayout_offset_of_element"
86end
87
88module Target = struct
89  type t
90
91  external default_triple : unit -> string = "llvm_target_default_triple"
92  external first : unit -> t option = "llvm_target_first"
93  external succ : t -> t option = "llvm_target_succ"
94  external by_name : string -> t option = "llvm_target_by_name"
95  external by_triple : string -> t = "llvm_target_by_triple"
96  external name : t -> string = "llvm_target_name"
97  external description : t -> string = "llvm_target_description"
98  external has_jit : t -> bool = "llvm_target_has_jit"
99  external has_target_machine : t -> bool = "llvm_target_has_target_machine"
100  external has_asm_backend : t -> bool = "llvm_target_has_asm_backend"
101
102  let all () =
103    let rec step elem lst =
104      match elem with
105      | Some target -> step (succ target) (target :: lst)
106      | None        -> lst
107    in
108    step (first ()) []
109end
110
111module TargetMachine = struct
112  type t
113
114  external create : triple:string -> ?cpu:string -> ?features:string ->
115                    ?level:CodeGenOptLevel.t -> ?reloc_mode:RelocMode.t ->
116                    ?code_model:CodeModel.t -> Target.t -> t
117                  = "llvm_create_targetmachine_bytecode"
118                    "llvm_create_targetmachine_native"
119  external target : t -> Target.t
120                  = "llvm_targetmachine_target"
121  external triple : t -> string
122                  = "llvm_targetmachine_triple"
123  external cpu : t -> string
124               = "llvm_targetmachine_cpu"
125  external features : t -> string
126                    = "llvm_targetmachine_features"
127  external data_layout : t -> DataLayout.t
128                       = "llvm_targetmachine_data_layout"
129  external add_analysis_passes : [< Llvm.PassManager.any ] Llvm.PassManager.t -> t -> unit
130                               = "llvm_targetmachine_add_analysis_passes"
131  external set_verbose_asm : bool -> t -> unit
132                           = "llvm_targetmachine_set_verbose_asm"
133  external emit_to_file : Llvm.llmodule -> CodeGenFileType.t -> string ->
134                          t -> unit
135                        = "llvm_targetmachine_emit_to_file"
136  external emit_to_memory_buffer : Llvm.llmodule -> CodeGenFileType.t ->
137                                   t -> Llvm.llmemorybuffer
138                                 = "llvm_targetmachine_emit_to_memory_buffer"
139end
140