1(*===-- llvm/llvm.ml - LLVM OCaml Interface -------------------------------===*
2 *
3 * Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 * See https://llvm.org/LICENSE.txt for license information.
5 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 *
7 *===----------------------------------------------------------------------===*)
8
9
10type llcontext
11type llmodule
12type lltype
13type llvalue
14type lluse
15type llbasicblock
16type llbuilder
17type llattrkind
18type llattribute
19type llmemorybuffer
20type llmdkind
21
22exception FeatureDisabled of string
23
24let () = Callback.register_exception "Llvm.FeatureDisabled" (FeatureDisabled "")
25
26module TypeKind = struct
27  type t =
28  | Void
29  | Half
30  | Float
31  | Double
32  | X86fp80
33  | Fp128
34  | Ppc_fp128
35  | Label
36  | Integer
37  | Function
38  | Struct
39  | Array
40  | Pointer
41  | Vector
42  | Metadata
43  | X86_mmx
44  | Token
45end
46
47module Linkage = struct
48  type t =
49  | External
50  | Available_externally
51  | Link_once
52  | Link_once_odr
53  | Link_once_odr_auto_hide
54  | Weak
55  | Weak_odr
56  | Appending
57  | Internal
58  | Private
59  | Dllimport
60  | Dllexport
61  | External_weak
62  | Ghost
63  | Common
64  | Linker_private
65  | Linker_private_weak
66end
67
68module Visibility = struct
69  type t =
70  | Default
71  | Hidden
72  | Protected
73end
74
75module DLLStorageClass = struct
76  type t =
77  | Default
78  | DLLImport
79  | DLLExport
80end
81
82module CallConv = struct
83  let c = 0
84  let fast = 8
85  let cold = 9
86  let x86_stdcall = 64
87  let x86_fastcall = 65
88end
89
90module AttrRepr = struct
91  type t =
92  | Enum of llattrkind * int64
93  | String of string * string
94end
95
96module AttrIndex = struct
97  type t =
98  | Function
99  | Return
100  | Param of int
101
102  let to_int index =
103    match index with
104    | Function -> -1
105    | Return -> 0
106    | Param(n) -> 1 + n
107end
108
109module Attribute = struct
110  type t =
111  | Zext
112  | Sext
113  | Noreturn
114  | Inreg
115  | Structret
116  | Nounwind
117  | Noalias
118  | Byval
119  | Nest
120  | Readnone
121  | Readonly
122  | Noinline
123  | Alwaysinline
124  | Optsize
125  | Ssp
126  | Sspreq
127  | Alignment of int
128  | Nocapture
129  | Noredzone
130  | Noimplicitfloat
131  | Naked
132  | Inlinehint
133  | Stackalignment of int
134  | ReturnsTwice
135  | UWTable
136  | NonLazyBind
137end
138
139module Icmp = struct
140  type t =
141  | Eq
142  | Ne
143  | Ugt
144  | Uge
145  | Ult
146  | Ule
147  | Sgt
148  | Sge
149  | Slt
150  | Sle
151end
152
153module Fcmp = struct
154  type t =
155  | False
156  | Oeq
157  | Ogt
158  | Oge
159  | Olt
160  | Ole
161  | One
162  | Ord
163  | Uno
164  | Ueq
165  | Ugt
166  | Uge
167  | Ult
168  | Ule
169  | Une
170  | True
171end
172
173module Opcode  = struct
174  type t =
175  | Invalid (* not an instruction *)
176  (* Terminator Instructions *)
177  | Ret
178  | Br
179  | Switch
180  | IndirectBr
181  | Invoke
182  | Invalid2
183  | Unreachable
184  (* Standard Binary Operators *)
185  | Add
186  | FAdd
187  | Sub
188  | FSub
189  | Mul
190  | FMul
191  | UDiv
192  | SDiv
193  | FDiv
194  | URem
195  | SRem
196  | FRem
197  (* Logical Operators *)
198  | Shl
199  | LShr
200  | AShr
201  | And
202  | Or
203  | Xor
204  (* Memory Operators *)
205  | Alloca
206  | Load
207  | Store
208  | GetElementPtr
209  (* Cast Operators *)
210  | Trunc
211  | ZExt
212  | SExt
213  | FPToUI
214  | FPToSI
215  | UIToFP
216  | SIToFP
217  | FPTrunc
218  | FPExt
219  | PtrToInt
220  | IntToPtr
221  | BitCast
222  (* Other Operators *)
223  | ICmp
224  | FCmp
225  | PHI
226  | Call
227  | Select
228  | UserOp1
229  | UserOp2
230  | VAArg
231  | ExtractElement
232  | InsertElement
233  | ShuffleVector
234  | ExtractValue
235  | InsertValue
236  | Fence
237  | AtomicCmpXchg
238  | AtomicRMW
239  | Resume
240  | LandingPad
241  | AddrSpaceCast
242  | CleanupRet
243  | CatchRet
244  | CatchPad
245  | CleanupPad
246  | CatchSwitch
247  | FNeg
248  | CallBr
249end
250
251module LandingPadClauseTy = struct
252  type t =
253  | Catch
254  | Filter
255end
256
257module ThreadLocalMode = struct
258  type t =
259  | None
260  | GeneralDynamic
261  | LocalDynamic
262  | InitialExec
263  | LocalExec
264end
265
266module AtomicOrdering = struct
267  type t =
268  | NotAtomic
269  | Unordered
270  | Monotonic
271  | Invalid
272  | Acquire
273  | Release
274  | AcqiureRelease
275  | SequentiallyConsistent
276end
277
278module AtomicRMWBinOp = struct
279  type t =
280  | Xchg
281  | Add
282  | Sub
283  | And
284  | Nand
285  | Or
286  | Xor
287  | Max
288  | Min
289  | UMax
290  | UMin
291end
292
293module ValueKind = struct
294  type t =
295  | NullValue
296  | Argument
297  | BasicBlock
298  | InlineAsm
299  | MDNode
300  | MDString
301  | BlockAddress
302  | ConstantAggregateZero
303  | ConstantArray
304  | ConstantDataArray
305  | ConstantDataVector
306  | ConstantExpr
307  | ConstantFP
308  | ConstantInt
309  | ConstantPointerNull
310  | ConstantStruct
311  | ConstantVector
312  | Function
313  | GlobalAlias
314  | GlobalIFunc
315  | GlobalVariable
316  | UndefValue
317  | PoisonValue
318  | Instruction of Opcode.t
319end
320
321module DiagnosticSeverity = struct
322  type t =
323  | Error
324  | Warning
325  | Remark
326  | Note
327end
328
329exception IoError of string
330
331let () = Callback.register_exception "Llvm.IoError" (IoError "")
332
333external install_fatal_error_handler : (string -> unit) -> unit
334                                     = "llvm_install_fatal_error_handler"
335external reset_fatal_error_handler : unit -> unit
336                                   = "llvm_reset_fatal_error_handler"
337external enable_pretty_stacktrace : unit -> unit
338                                  = "llvm_enable_pretty_stacktrace"
339external parse_command_line_options : ?overview:string -> string array -> unit
340                                    = "llvm_parse_command_line_options"
341
342type ('a, 'b) llpos =
343| At_end of 'a
344| Before of 'b
345
346type ('a, 'b) llrev_pos =
347| At_start of 'a
348| After of 'b
349
350
351(*===-- Context error handling --------------------------------------------===*)
352module Diagnostic = struct
353  type t
354
355  external description : t -> string = "llvm_get_diagnostic_description"
356  external severity : t -> DiagnosticSeverity.t
357                    = "llvm_get_diagnostic_severity"
358end
359
360external set_diagnostic_handler
361  : llcontext -> (Diagnostic.t -> unit) option -> unit
362  = "llvm_set_diagnostic_handler"
363
364(*===-- Contexts ----------------------------------------------------------===*)
365external create_context : unit -> llcontext = "llvm_create_context"
366external dispose_context : llcontext -> unit = "llvm_dispose_context"
367external global_context : unit -> llcontext = "llvm_global_context"
368external mdkind_id : llcontext -> string -> llmdkind = "llvm_mdkind_id"
369
370(*===-- Attributes --------------------------------------------------------===*)
371exception UnknownAttribute of string
372
373let () = Callback.register_exception "Llvm.UnknownAttribute"
374                                     (UnknownAttribute "")
375
376external enum_attr_kind : string -> llattrkind = "llvm_enum_attr_kind"
377external llvm_create_enum_attr : llcontext -> llattrkind -> int64 ->
378                                 llattribute
379                               = "llvm_create_enum_attr_by_kind"
380external is_enum_attr : llattribute -> bool = "llvm_is_enum_attr"
381external get_enum_attr_kind : llattribute -> llattrkind
382                            = "llvm_get_enum_attr_kind"
383external get_enum_attr_value : llattribute -> int64
384                             = "llvm_get_enum_attr_value"
385external llvm_create_string_attr : llcontext -> string -> string ->
386                                   llattribute
387                                 = "llvm_create_string_attr"
388external is_string_attr : llattribute -> bool = "llvm_is_string_attr"
389external get_string_attr_kind : llattribute -> string
390                              = "llvm_get_string_attr_kind"
391external get_string_attr_value : llattribute -> string
392                               = "llvm_get_string_attr_value"
393
394let create_enum_attr context name value =
395  llvm_create_enum_attr context (enum_attr_kind name) value
396let create_string_attr context kind value =
397  llvm_create_string_attr context kind value
398
399let attr_of_repr context repr =
400  match repr with
401  | AttrRepr.Enum(kind, value) -> llvm_create_enum_attr context kind value
402  | AttrRepr.String(key, value) -> llvm_create_string_attr context key value
403
404let repr_of_attr attr =
405  if is_enum_attr attr then
406    AttrRepr.Enum(get_enum_attr_kind attr, get_enum_attr_value attr)
407  else if is_string_attr attr then
408    AttrRepr.String(get_string_attr_kind attr, get_string_attr_value attr)
409  else assert false
410
411(*===-- Modules -----------------------------------------------------------===*)
412external create_module : llcontext -> string -> llmodule = "llvm_create_module"
413external dispose_module : llmodule -> unit = "llvm_dispose_module"
414external target_triple: llmodule -> string
415                      = "llvm_target_triple"
416external set_target_triple: string -> llmodule -> unit
417                          = "llvm_set_target_triple"
418external data_layout: llmodule -> string
419                    = "llvm_data_layout"
420external set_data_layout: string -> llmodule -> unit
421                        = "llvm_set_data_layout"
422external dump_module : llmodule -> unit = "llvm_dump_module"
423external print_module : string -> llmodule -> unit = "llvm_print_module"
424external string_of_llmodule : llmodule -> string = "llvm_string_of_llmodule"
425external set_module_inline_asm : llmodule -> string -> unit
426                               = "llvm_set_module_inline_asm"
427external module_context : llmodule -> llcontext = "LLVMGetModuleContext"
428
429(*===-- Types -------------------------------------------------------------===*)
430external classify_type : lltype -> TypeKind.t = "llvm_classify_type"
431external type_context : lltype -> llcontext = "llvm_type_context"
432external type_is_sized : lltype -> bool = "llvm_type_is_sized"
433external dump_type : lltype -> unit = "llvm_dump_type"
434external string_of_lltype : lltype -> string = "llvm_string_of_lltype"
435
436(*--... Operations on integer types ........................................--*)
437external i1_type : llcontext -> lltype = "llvm_i1_type"
438external i8_type : llcontext -> lltype = "llvm_i8_type"
439external i16_type : llcontext -> lltype = "llvm_i16_type"
440external i32_type : llcontext -> lltype = "llvm_i32_type"
441external i64_type : llcontext -> lltype = "llvm_i64_type"
442
443external integer_type : llcontext -> int -> lltype = "llvm_integer_type"
444external integer_bitwidth : lltype -> int = "llvm_integer_bitwidth"
445
446(*--... Operations on real types ...........................................--*)
447external float_type : llcontext -> lltype = "llvm_float_type"
448external double_type : llcontext -> lltype = "llvm_double_type"
449external x86fp80_type : llcontext -> lltype = "llvm_x86fp80_type"
450external fp128_type : llcontext -> lltype = "llvm_fp128_type"
451external ppc_fp128_type : llcontext -> lltype = "llvm_ppc_fp128_type"
452
453(*--... Operations on function types .......................................--*)
454external function_type : lltype -> lltype array -> lltype = "llvm_function_type"
455external var_arg_function_type : lltype -> lltype array -> lltype
456                               = "llvm_var_arg_function_type"
457external is_var_arg : lltype -> bool = "llvm_is_var_arg"
458external return_type : lltype -> lltype = "LLVMGetReturnType"
459external param_types : lltype -> lltype array = "llvm_param_types"
460
461(*--... Operations on struct types .........................................--*)
462external struct_type : llcontext -> lltype array -> lltype = "llvm_struct_type"
463external packed_struct_type : llcontext -> lltype array -> lltype
464                            = "llvm_packed_struct_type"
465external struct_name : lltype -> string option = "llvm_struct_name"
466external named_struct_type : llcontext -> string -> lltype =
467    "llvm_named_struct_type"
468external struct_set_body : lltype -> lltype array -> bool -> unit =
469    "llvm_struct_set_body"
470external struct_element_types : lltype -> lltype array
471                              = "llvm_struct_element_types"
472external is_packed : lltype -> bool = "llvm_is_packed"
473external is_opaque : lltype -> bool = "llvm_is_opaque"
474external is_literal : lltype -> bool = "llvm_is_literal"
475
476(*--... Operations on pointer, vector, and array types .....................--*)
477
478external subtypes : lltype -> lltype array = "llvm_subtypes"
479external array_type : lltype -> int -> lltype = "llvm_array_type"
480external pointer_type : lltype -> lltype = "llvm_pointer_type"
481external qualified_pointer_type : lltype -> int -> lltype
482                                = "llvm_qualified_pointer_type"
483external vector_type : lltype -> int -> lltype = "llvm_vector_type"
484
485external element_type : lltype -> lltype = "LLVMGetElementType"
486external array_length : lltype -> int = "llvm_array_length"
487external address_space : lltype -> int = "llvm_address_space"
488external vector_size : lltype -> int = "llvm_vector_size"
489
490(*--... Operations on other types ..........................................--*)
491external void_type : llcontext -> lltype = "llvm_void_type"
492external label_type : llcontext -> lltype = "llvm_label_type"
493external x86_mmx_type : llcontext -> lltype = "llvm_x86_mmx_type"
494external type_by_name : llmodule -> string -> lltype option = "llvm_type_by_name"
495
496external classify_value : llvalue -> ValueKind.t = "llvm_classify_value"
497(*===-- Values ------------------------------------------------------------===*)
498external type_of : llvalue -> lltype = "llvm_type_of"
499external value_name : llvalue -> string = "llvm_value_name"
500external set_value_name : string -> llvalue -> unit = "llvm_set_value_name"
501external dump_value : llvalue -> unit = "llvm_dump_value"
502external string_of_llvalue : llvalue -> string = "llvm_string_of_llvalue"
503external replace_all_uses_with : llvalue -> llvalue -> unit
504                               = "llvm_replace_all_uses_with"
505
506(*--... Operations on uses .................................................--*)
507external use_begin : llvalue -> lluse option = "llvm_use_begin"
508external use_succ : lluse -> lluse option = "llvm_use_succ"
509external user : lluse -> llvalue = "llvm_user"
510external used_value : lluse -> llvalue = "llvm_used_value"
511
512let iter_uses f v =
513  let rec aux = function
514    | None -> ()
515    | Some u ->
516        f u;
517        aux (use_succ u)
518  in
519  aux (use_begin v)
520
521let fold_left_uses f init v =
522  let rec aux init u =
523    match u with
524    | None -> init
525    | Some u -> aux (f init u) (use_succ u)
526  in
527  aux init (use_begin v)
528
529let fold_right_uses f v init =
530  let rec aux u init =
531    match u with
532    | None -> init
533    | Some u -> f u (aux (use_succ u) init)
534  in
535  aux (use_begin v) init
536
537
538(*--... Operations on users ................................................--*)
539external operand : llvalue -> int -> llvalue = "llvm_operand"
540external operand_use : llvalue -> int -> lluse = "llvm_operand_use"
541external set_operand : llvalue -> int -> llvalue -> unit = "llvm_set_operand"
542external num_operands : llvalue -> int = "llvm_num_operands"
543external indices : llvalue -> int array = "llvm_indices"
544
545(*--... Operations on constants of (mostly) any type .......................--*)
546external is_constant : llvalue -> bool = "llvm_is_constant"
547external const_null : lltype -> llvalue = "LLVMConstNull"
548external const_all_ones : (*int|vec*)lltype -> llvalue = "LLVMConstAllOnes"
549external const_pointer_null : lltype -> llvalue = "LLVMConstPointerNull"
550external undef : lltype -> llvalue = "LLVMGetUndef"
551external poison : lltype -> llvalue = "LLVMGetPoison"
552external is_null : llvalue -> bool = "llvm_is_null"
553external is_undef : llvalue -> bool = "llvm_is_undef"
554external is_poison : llvalue -> bool = "llvm_is_poison"
555external constexpr_opcode : llvalue -> Opcode.t = "llvm_constexpr_get_opcode"
556
557(*--... Operations on instructions .........................................--*)
558external has_metadata : llvalue -> bool = "llvm_has_metadata"
559external metadata : llvalue -> llmdkind -> llvalue option = "llvm_metadata"
560external set_metadata : llvalue -> llmdkind -> llvalue -> unit = "llvm_set_metadata"
561external clear_metadata : llvalue -> llmdkind -> unit = "llvm_clear_metadata"
562
563(*--... Operations on metadata .......,.....................................--*)
564external mdstring : llcontext -> string -> llvalue = "llvm_mdstring"
565external mdnode : llcontext -> llvalue array -> llvalue = "llvm_mdnode"
566external mdnull : llcontext -> llvalue = "llvm_mdnull"
567external get_mdstring : llvalue -> string option = "llvm_get_mdstring"
568external get_mdnode_operands : llvalue -> llvalue array
569                            = "llvm_get_mdnode_operands"
570external get_named_metadata : llmodule -> string -> llvalue array
571                            = "llvm_get_namedmd"
572external add_named_metadata_operand : llmodule -> string -> llvalue -> unit
573                                    = "llvm_append_namedmd"
574
575(*--... Operations on scalar constants .....................................--*)
576external const_int : lltype -> int -> llvalue = "llvm_const_int"
577external const_of_int64 : lltype -> Int64.t -> bool -> llvalue
578                        = "llvm_const_of_int64"
579external int64_of_const : llvalue -> Int64.t option
580                        = "llvm_int64_of_const"
581external const_int_of_string : lltype -> string -> int -> llvalue
582                             = "llvm_const_int_of_string"
583external const_float : lltype -> float -> llvalue = "llvm_const_float"
584external float_of_const : llvalue -> float option
585                        = "llvm_float_of_const"
586external const_float_of_string : lltype -> string -> llvalue
587                               = "llvm_const_float_of_string"
588
589(*--... Operations on composite constants ..................................--*)
590external const_string : llcontext -> string -> llvalue = "llvm_const_string"
591external const_stringz : llcontext -> string -> llvalue = "llvm_const_stringz"
592external const_array : lltype -> llvalue array -> llvalue = "llvm_const_array"
593external const_struct : llcontext -> llvalue array -> llvalue
594                      = "llvm_const_struct"
595external const_named_struct : lltype -> llvalue array -> llvalue
596                      = "llvm_const_named_struct"
597external const_packed_struct : llcontext -> llvalue array -> llvalue
598                             = "llvm_const_packed_struct"
599external const_vector : llvalue array -> llvalue = "llvm_const_vector"
600external string_of_const : llvalue -> string option = "llvm_string_of_const"
601external const_element : llvalue -> int -> llvalue = "llvm_const_element"
602
603(*--... Constant expressions ...............................................--*)
604external align_of : lltype -> llvalue = "LLVMAlignOf"
605external size_of : lltype -> llvalue = "LLVMSizeOf"
606external const_neg : llvalue -> llvalue = "LLVMConstNeg"
607external const_nsw_neg : llvalue -> llvalue = "LLVMConstNSWNeg"
608external const_nuw_neg : llvalue -> llvalue = "LLVMConstNUWNeg"
609external const_fneg : llvalue -> llvalue = "LLVMConstFNeg"
610external const_not : llvalue -> llvalue = "LLVMConstNot"
611external const_add : llvalue -> llvalue -> llvalue = "LLVMConstAdd"
612external const_nsw_add : llvalue -> llvalue -> llvalue = "LLVMConstNSWAdd"
613external const_nuw_add : llvalue -> llvalue -> llvalue = "LLVMConstNUWAdd"
614external const_fadd : llvalue -> llvalue -> llvalue = "LLVMConstFAdd"
615external const_sub : llvalue -> llvalue -> llvalue = "LLVMConstSub"
616external const_nsw_sub : llvalue -> llvalue -> llvalue = "LLVMConstNSWSub"
617external const_nuw_sub : llvalue -> llvalue -> llvalue = "LLVMConstNUWSub"
618external const_fsub : llvalue -> llvalue -> llvalue = "LLVMConstFSub"
619external const_mul : llvalue -> llvalue -> llvalue = "LLVMConstMul"
620external const_nsw_mul : llvalue -> llvalue -> llvalue = "LLVMConstNSWMul"
621external const_nuw_mul : llvalue -> llvalue -> llvalue = "LLVMConstNUWMul"
622external const_fmul : llvalue -> llvalue -> llvalue = "LLVMConstFMul"
623external const_udiv : llvalue -> llvalue -> llvalue = "LLVMConstUDiv"
624external const_sdiv : llvalue -> llvalue -> llvalue = "LLVMConstSDiv"
625external const_exact_sdiv : llvalue -> llvalue -> llvalue = "LLVMConstExactSDiv"
626external const_fdiv : llvalue -> llvalue -> llvalue = "LLVMConstFDiv"
627external const_urem : llvalue -> llvalue -> llvalue = "LLVMConstURem"
628external const_srem : llvalue -> llvalue -> llvalue = "LLVMConstSRem"
629external const_frem : llvalue -> llvalue -> llvalue = "LLVMConstFRem"
630external const_and : llvalue -> llvalue -> llvalue = "LLVMConstAnd"
631external const_or : llvalue -> llvalue -> llvalue = "LLVMConstOr"
632external const_xor : llvalue -> llvalue -> llvalue = "LLVMConstXor"
633external const_icmp : Icmp.t -> llvalue -> llvalue -> llvalue
634                    = "llvm_const_icmp"
635external const_fcmp : Fcmp.t -> llvalue -> llvalue -> llvalue
636                    = "llvm_const_fcmp"
637external const_shl : llvalue -> llvalue -> llvalue = "LLVMConstShl"
638external const_lshr : llvalue -> llvalue -> llvalue = "LLVMConstLShr"
639external const_ashr : llvalue -> llvalue -> llvalue = "LLVMConstAShr"
640external const_gep : llvalue -> llvalue array -> llvalue = "llvm_const_gep"
641external const_in_bounds_gep : llvalue -> llvalue array -> llvalue
642                            = "llvm_const_in_bounds_gep"
643external const_trunc : llvalue -> lltype -> llvalue = "LLVMConstTrunc"
644external const_sext : llvalue -> lltype -> llvalue = "LLVMConstSExt"
645external const_zext : llvalue -> lltype -> llvalue = "LLVMConstZExt"
646external const_fptrunc : llvalue -> lltype -> llvalue = "LLVMConstFPTrunc"
647external const_fpext : llvalue -> lltype -> llvalue = "LLVMConstFPExt"
648external const_uitofp : llvalue -> lltype -> llvalue = "LLVMConstUIToFP"
649external const_sitofp : llvalue -> lltype -> llvalue = "LLVMConstSIToFP"
650external const_fptoui : llvalue -> lltype -> llvalue = "LLVMConstFPToUI"
651external const_fptosi : llvalue -> lltype -> llvalue = "LLVMConstFPToSI"
652external const_ptrtoint : llvalue -> lltype -> llvalue = "LLVMConstPtrToInt"
653external const_inttoptr : llvalue -> lltype -> llvalue = "LLVMConstIntToPtr"
654external const_bitcast : llvalue -> lltype -> llvalue = "LLVMConstBitCast"
655external const_zext_or_bitcast : llvalue -> lltype -> llvalue
656                             = "LLVMConstZExtOrBitCast"
657external const_sext_or_bitcast : llvalue -> lltype -> llvalue
658                             = "LLVMConstSExtOrBitCast"
659external const_trunc_or_bitcast : llvalue -> lltype -> llvalue
660                              = "LLVMConstTruncOrBitCast"
661external const_pointercast : llvalue -> lltype -> llvalue
662                           = "LLVMConstPointerCast"
663external const_intcast : llvalue -> lltype -> is_signed:bool -> llvalue
664                       = "llvm_const_intcast"
665external const_fpcast : llvalue -> lltype -> llvalue = "LLVMConstFPCast"
666external const_select : llvalue -> llvalue -> llvalue -> llvalue
667                      = "LLVMConstSelect"
668external const_extractelement : llvalue -> llvalue -> llvalue
669                              = "LLVMConstExtractElement"
670external const_insertelement : llvalue -> llvalue -> llvalue -> llvalue
671                             = "LLVMConstInsertElement"
672external const_shufflevector : llvalue -> llvalue -> llvalue -> llvalue
673                             = "LLVMConstShuffleVector"
674external const_extractvalue : llvalue -> int array -> llvalue
675                            = "llvm_const_extractvalue"
676external const_insertvalue : llvalue -> llvalue -> int array -> llvalue
677                           = "llvm_const_insertvalue"
678external const_inline_asm : lltype -> string -> string -> bool -> bool ->
679                            llvalue
680                          = "llvm_const_inline_asm"
681external block_address : llvalue -> llbasicblock -> llvalue = "LLVMBlockAddress"
682
683(*--... Operations on global variables, functions, and aliases (globals) ...--*)
684external global_parent : llvalue -> llmodule = "LLVMGetGlobalParent"
685external is_declaration : llvalue -> bool = "llvm_is_declaration"
686external linkage : llvalue -> Linkage.t = "llvm_linkage"
687external set_linkage : Linkage.t -> llvalue -> unit = "llvm_set_linkage"
688external unnamed_addr : llvalue -> bool = "llvm_unnamed_addr"
689external set_unnamed_addr : bool -> llvalue -> unit = "llvm_set_unnamed_addr"
690external section : llvalue -> string = "llvm_section"
691external set_section : string -> llvalue -> unit = "llvm_set_section"
692external visibility : llvalue -> Visibility.t = "llvm_visibility"
693external set_visibility : Visibility.t -> llvalue -> unit = "llvm_set_visibility"
694external dll_storage_class : llvalue -> DLLStorageClass.t = "llvm_dll_storage_class"
695external set_dll_storage_class : DLLStorageClass.t -> llvalue -> unit = "llvm_set_dll_storage_class"
696external alignment : llvalue -> int = "llvm_alignment"
697external set_alignment : int -> llvalue -> unit = "llvm_set_alignment"
698external is_global_constant : llvalue -> bool = "llvm_is_global_constant"
699external set_global_constant : bool -> llvalue -> unit
700                             = "llvm_set_global_constant"
701
702(*--... Operations on global variables .....................................--*)
703external declare_global : lltype -> string -> llmodule -> llvalue
704                        = "llvm_declare_global"
705external declare_qualified_global : lltype -> string -> int -> llmodule ->
706                                    llvalue
707                                  = "llvm_declare_qualified_global"
708external define_global : string -> llvalue -> llmodule -> llvalue
709                       = "llvm_define_global"
710external define_qualified_global : string -> llvalue -> int -> llmodule ->
711                                   llvalue
712                                 = "llvm_define_qualified_global"
713external lookup_global : string -> llmodule -> llvalue option
714                       = "llvm_lookup_global"
715external delete_global : llvalue -> unit = "llvm_delete_global"
716external global_initializer : llvalue -> llvalue = "LLVMGetInitializer"
717external set_initializer : llvalue -> llvalue -> unit = "llvm_set_initializer"
718external remove_initializer : llvalue -> unit = "llvm_remove_initializer"
719external is_thread_local : llvalue -> bool = "llvm_is_thread_local"
720external set_thread_local : bool -> llvalue -> unit = "llvm_set_thread_local"
721external thread_local_mode : llvalue -> ThreadLocalMode.t
722                           = "llvm_thread_local_mode"
723external set_thread_local_mode : ThreadLocalMode.t -> llvalue -> unit
724                               = "llvm_set_thread_local_mode"
725external is_externally_initialized : llvalue -> bool
726                                   = "llvm_is_externally_initialized"
727external set_externally_initialized : bool -> llvalue -> unit
728                                    = "llvm_set_externally_initialized"
729external global_begin : llmodule -> (llmodule, llvalue) llpos
730                      = "llvm_global_begin"
731external global_succ : llvalue -> (llmodule, llvalue) llpos
732                     = "llvm_global_succ"
733external global_end : llmodule -> (llmodule, llvalue) llrev_pos
734                    = "llvm_global_end"
735external global_pred : llvalue -> (llmodule, llvalue) llrev_pos
736                     = "llvm_global_pred"
737
738let rec iter_global_range f i e =
739  if i = e then () else
740  match i with
741  | At_end _ -> raise (Invalid_argument "Invalid global variable range.")
742  | Before bb ->
743      f bb;
744      iter_global_range f (global_succ bb) e
745
746let iter_globals f m =
747  iter_global_range f (global_begin m) (At_end m)
748
749let rec fold_left_global_range f init i e =
750  if i = e then init else
751  match i with
752  | At_end _ -> raise (Invalid_argument "Invalid global variable range.")
753  | Before bb -> fold_left_global_range f (f init bb) (global_succ bb) e
754
755let fold_left_globals f init m =
756  fold_left_global_range f init (global_begin m) (At_end m)
757
758let rec rev_iter_global_range f i e =
759  if i = e then () else
760  match i with
761  | At_start _ -> raise (Invalid_argument "Invalid global variable range.")
762  | After bb ->
763      f bb;
764      rev_iter_global_range f (global_pred bb) e
765
766let rev_iter_globals f m =
767  rev_iter_global_range f (global_end m) (At_start m)
768
769let rec fold_right_global_range f i e init =
770  if i = e then init else
771  match i with
772  | At_start _ -> raise (Invalid_argument "Invalid global variable range.")
773  | After bb -> fold_right_global_range f (global_pred bb) e (f bb init)
774
775let fold_right_globals f m init =
776  fold_right_global_range f (global_end m) (At_start m) init
777
778(*--... Operations on aliases ..............................................--*)
779external add_alias : llmodule -> lltype -> llvalue -> string -> llvalue
780                   = "llvm_add_alias"
781
782(*--... Operations on functions ............................................--*)
783external declare_function : string -> lltype -> llmodule -> llvalue
784                          = "llvm_declare_function"
785external define_function : string -> lltype -> llmodule -> llvalue
786                         = "llvm_define_function"
787external lookup_function : string -> llmodule -> llvalue option
788                         = "llvm_lookup_function"
789external delete_function : llvalue -> unit = "llvm_delete_function"
790external is_intrinsic : llvalue -> bool = "llvm_is_intrinsic"
791external function_call_conv : llvalue -> int = "llvm_function_call_conv"
792external set_function_call_conv : int -> llvalue -> unit
793                                = "llvm_set_function_call_conv"
794external gc : llvalue -> string option = "llvm_gc"
795external set_gc : string option -> llvalue -> unit = "llvm_set_gc"
796external function_begin : llmodule -> (llmodule, llvalue) llpos
797                        = "llvm_function_begin"
798external function_succ : llvalue -> (llmodule, llvalue) llpos
799                       = "llvm_function_succ"
800external function_end : llmodule -> (llmodule, llvalue) llrev_pos
801                      = "llvm_function_end"
802external function_pred : llvalue -> (llmodule, llvalue) llrev_pos
803                       = "llvm_function_pred"
804
805let rec iter_function_range f i e =
806  if i = e then () else
807  match i with
808  | At_end _ -> raise (Invalid_argument "Invalid function range.")
809  | Before fn ->
810      f fn;
811      iter_function_range f (function_succ fn) e
812
813let iter_functions f m =
814  iter_function_range f (function_begin m) (At_end m)
815
816let rec fold_left_function_range f init i e =
817  if i = e then init else
818  match i with
819  | At_end _ -> raise (Invalid_argument "Invalid function range.")
820  | Before fn -> fold_left_function_range f (f init fn) (function_succ fn) e
821
822let fold_left_functions f init m =
823  fold_left_function_range f init (function_begin m) (At_end m)
824
825let rec rev_iter_function_range f i e =
826  if i = e then () else
827  match i with
828  | At_start _ -> raise (Invalid_argument "Invalid function range.")
829  | After fn ->
830      f fn;
831      rev_iter_function_range f (function_pred fn) e
832
833let rev_iter_functions f m =
834  rev_iter_function_range f (function_end m) (At_start m)
835
836let rec fold_right_function_range f i e init =
837  if i = e then init else
838  match i with
839  | At_start _ -> raise (Invalid_argument "Invalid function range.")
840  | After fn -> fold_right_function_range f (function_pred fn) e (f fn init)
841
842let fold_right_functions f m init =
843  fold_right_function_range f (function_end m) (At_start m) init
844
845external llvm_add_function_attr : llvalue -> llattribute -> int -> unit
846                                = "llvm_add_function_attr"
847external llvm_function_attrs : llvalue -> int -> llattribute array
848                             = "llvm_function_attrs"
849external llvm_remove_enum_function_attr : llvalue -> llattrkind -> int -> unit
850                                        = "llvm_remove_enum_function_attr"
851external llvm_remove_string_function_attr : llvalue -> string -> int -> unit
852                                          = "llvm_remove_string_function_attr"
853
854let add_function_attr f a i =
855  llvm_add_function_attr f a (AttrIndex.to_int i)
856let function_attrs f i =
857  llvm_function_attrs f (AttrIndex.to_int i)
858let remove_enum_function_attr f k i =
859  llvm_remove_enum_function_attr f k (AttrIndex.to_int i)
860let remove_string_function_attr f k i =
861  llvm_remove_string_function_attr f k (AttrIndex.to_int i)
862
863(*--... Operations on params ...............................................--*)
864external params : llvalue -> llvalue array = "llvm_params"
865external param : llvalue -> int -> llvalue = "llvm_param"
866external param_parent : llvalue -> llvalue = "LLVMGetParamParent"
867external param_begin : llvalue -> (llvalue, llvalue) llpos = "llvm_param_begin"
868external param_succ : llvalue -> (llvalue, llvalue) llpos = "llvm_param_succ"
869external param_end : llvalue -> (llvalue, llvalue) llrev_pos = "llvm_param_end"
870external param_pred : llvalue -> (llvalue, llvalue) llrev_pos ="llvm_param_pred"
871
872let rec iter_param_range f i e =
873  if i = e then () else
874  match i with
875  | At_end _ -> raise (Invalid_argument "Invalid parameter range.")
876  | Before p ->
877      f p;
878      iter_param_range f (param_succ p) e
879
880let iter_params f fn =
881  iter_param_range f (param_begin fn) (At_end fn)
882
883let rec fold_left_param_range f init i e =
884  if i = e then init else
885  match i with
886  | At_end _ -> raise (Invalid_argument "Invalid parameter range.")
887  | Before p -> fold_left_param_range f (f init p) (param_succ p) e
888
889let fold_left_params f init fn =
890  fold_left_param_range f init (param_begin fn) (At_end fn)
891
892let rec rev_iter_param_range f i e =
893  if i = e then () else
894  match i with
895  | At_start _ -> raise (Invalid_argument "Invalid parameter range.")
896  | After p ->
897      f p;
898      rev_iter_param_range f (param_pred p) e
899
900let rev_iter_params f fn =
901  rev_iter_param_range f (param_end fn) (At_start fn)
902
903let rec fold_right_param_range f init i e =
904  if i = e then init else
905  match i with
906  | At_start _ -> raise (Invalid_argument "Invalid parameter range.")
907  | After p -> fold_right_param_range f (f p init) (param_pred p) e
908
909let fold_right_params f fn init =
910  fold_right_param_range f init (param_end fn) (At_start fn)
911
912(*--... Operations on basic blocks .........................................--*)
913external value_of_block : llbasicblock -> llvalue = "LLVMBasicBlockAsValue"
914external value_is_block : llvalue -> bool = "llvm_value_is_block"
915external block_of_value : llvalue -> llbasicblock = "LLVMValueAsBasicBlock"
916external block_parent : llbasicblock -> llvalue = "LLVMGetBasicBlockParent"
917external basic_blocks : llvalue -> llbasicblock array = "llvm_basic_blocks"
918external entry_block : llvalue -> llbasicblock = "LLVMGetEntryBasicBlock"
919external delete_block : llbasicblock -> unit = "llvm_delete_block"
920external remove_block : llbasicblock -> unit = "llvm_remove_block"
921external move_block_before : llbasicblock -> llbasicblock -> unit
922                           = "llvm_move_block_before"
923external move_block_after : llbasicblock -> llbasicblock -> unit
924                          = "llvm_move_block_after"
925external append_block : llcontext -> string -> llvalue -> llbasicblock
926                      = "llvm_append_block"
927external insert_block : llcontext -> string -> llbasicblock -> llbasicblock
928                      = "llvm_insert_block"
929external block_begin : llvalue -> (llvalue, llbasicblock) llpos
930                     = "llvm_block_begin"
931external block_succ : llbasicblock -> (llvalue, llbasicblock) llpos
932                    = "llvm_block_succ"
933external block_end : llvalue -> (llvalue, llbasicblock) llrev_pos
934                   = "llvm_block_end"
935external block_pred : llbasicblock -> (llvalue, llbasicblock) llrev_pos
936                    = "llvm_block_pred"
937external block_terminator : llbasicblock -> llvalue option =
938    "llvm_block_terminator"
939
940let rec iter_block_range f i e =
941  if i = e then () else
942  match i with
943  | At_end _ -> raise (Invalid_argument "Invalid block range.")
944  | Before bb ->
945      f bb;
946      iter_block_range f (block_succ bb) e
947
948let iter_blocks f fn =
949  iter_block_range f (block_begin fn) (At_end fn)
950
951let rec fold_left_block_range f init i e =
952  if i = e then init else
953  match i with
954  | At_end _ -> raise (Invalid_argument "Invalid block range.")
955  | Before bb -> fold_left_block_range f (f init bb) (block_succ bb) e
956
957let fold_left_blocks f init fn =
958  fold_left_block_range f init (block_begin fn) (At_end fn)
959
960let rec rev_iter_block_range f i e =
961  if i = e then () else
962  match i with
963  | At_start _ -> raise (Invalid_argument "Invalid block range.")
964  | After bb ->
965      f bb;
966      rev_iter_block_range f (block_pred bb) e
967
968let rev_iter_blocks f fn =
969  rev_iter_block_range f (block_end fn) (At_start fn)
970
971let rec fold_right_block_range f init i e =
972  if i = e then init else
973  match i with
974  | At_start _ -> raise (Invalid_argument "Invalid block range.")
975  | After bb -> fold_right_block_range f (f bb init) (block_pred bb) e
976
977let fold_right_blocks f fn init =
978  fold_right_block_range f init (block_end fn) (At_start fn)
979
980(*--... Operations on instructions .........................................--*)
981external instr_parent : llvalue -> llbasicblock = "LLVMGetInstructionParent"
982external instr_begin : llbasicblock -> (llbasicblock, llvalue) llpos
983                     = "llvm_instr_begin"
984external instr_succ : llvalue -> (llbasicblock, llvalue) llpos
985                     = "llvm_instr_succ"
986external instr_end : llbasicblock -> (llbasicblock, llvalue) llrev_pos
987                     = "llvm_instr_end"
988external instr_pred : llvalue -> (llbasicblock, llvalue) llrev_pos
989                     = "llvm_instr_pred"
990
991external instr_opcode : llvalue -> Opcode.t = "llvm_instr_get_opcode"
992external icmp_predicate : llvalue -> Icmp.t option = "llvm_instr_icmp_predicate"
993external fcmp_predicate : llvalue -> Fcmp.t option = "llvm_instr_fcmp_predicate"
994external instr_clone : llvalue -> llvalue = "llvm_instr_clone"
995
996let rec iter_instrs_range f i e =
997  if i = e then () else
998  match i with
999  | At_end _ -> raise (Invalid_argument "Invalid instruction range.")
1000  | Before i ->
1001      f i;
1002      iter_instrs_range f (instr_succ i) e
1003
1004let iter_instrs f bb =
1005  iter_instrs_range f (instr_begin bb) (At_end bb)
1006
1007let rec fold_left_instrs_range f init i e =
1008  if i = e then init else
1009  match i with
1010  | At_end _ -> raise (Invalid_argument "Invalid instruction range.")
1011  | Before i -> fold_left_instrs_range f (f init i) (instr_succ i) e
1012
1013let fold_left_instrs f init bb =
1014  fold_left_instrs_range f init (instr_begin bb) (At_end bb)
1015
1016let rec rev_iter_instrs_range f i e =
1017  if i = e then () else
1018  match i with
1019  | At_start _ -> raise (Invalid_argument "Invalid instruction range.")
1020  | After i ->
1021      f i;
1022      rev_iter_instrs_range f (instr_pred i) e
1023
1024let rev_iter_instrs f bb =
1025  rev_iter_instrs_range f (instr_end bb) (At_start bb)
1026
1027let rec fold_right_instr_range f i e init =
1028  if i = e then init else
1029  match i with
1030  | At_start _ -> raise (Invalid_argument "Invalid instruction range.")
1031  | After i -> fold_right_instr_range f (instr_pred i) e (f i init)
1032
1033let fold_right_instrs f bb init =
1034  fold_right_instr_range f (instr_end bb) (At_start bb) init
1035
1036
1037(*--... Operations on call sites ...........................................--*)
1038external instruction_call_conv: llvalue -> int
1039                              = "llvm_instruction_call_conv"
1040external set_instruction_call_conv: int -> llvalue -> unit
1041                                  = "llvm_set_instruction_call_conv"
1042
1043external llvm_add_call_site_attr : llvalue -> llattribute -> int -> unit
1044                                = "llvm_add_call_site_attr"
1045external llvm_call_site_attrs : llvalue -> int -> llattribute array
1046                             = "llvm_call_site_attrs"
1047external llvm_remove_enum_call_site_attr : llvalue -> llattrkind -> int -> unit
1048                                        = "llvm_remove_enum_call_site_attr"
1049external llvm_remove_string_call_site_attr : llvalue -> string -> int -> unit
1050                                          = "llvm_remove_string_call_site_attr"
1051
1052let add_call_site_attr f a i =
1053  llvm_add_call_site_attr f a (AttrIndex.to_int i)
1054let call_site_attrs f i =
1055  llvm_call_site_attrs f (AttrIndex.to_int i)
1056let remove_enum_call_site_attr f k i =
1057  llvm_remove_enum_call_site_attr f k (AttrIndex.to_int i)
1058let remove_string_call_site_attr f k i =
1059  llvm_remove_string_call_site_attr f k (AttrIndex.to_int i)
1060
1061(*--... Operations on call and invoke instructions (only) ..................--*)
1062external num_arg_operands : llvalue -> int = "llvm_num_arg_operands"
1063external is_tail_call : llvalue -> bool = "llvm_is_tail_call"
1064external set_tail_call : bool -> llvalue -> unit = "llvm_set_tail_call"
1065external get_normal_dest : llvalue -> llbasicblock = "LLVMGetNormalDest"
1066external get_unwind_dest : llvalue -> llbasicblock = "LLVMGetUnwindDest"
1067
1068(*--... Operations on load/store instructions (only) .......................--*)
1069external is_volatile : llvalue -> bool = "llvm_is_volatile"
1070external set_volatile : bool -> llvalue -> unit = "llvm_set_volatile"
1071
1072(*--... Operations on terminators ..........................................--*)
1073
1074let is_terminator llv =
1075  let open ValueKind in
1076  let open Opcode in
1077  match classify_value llv with
1078    | Instruction (Br | IndirectBr | Invoke | Resume | Ret | Switch | Unreachable)
1079      -> true
1080    | _ -> false
1081
1082external successor : llvalue -> int -> llbasicblock = "llvm_successor"
1083external set_successor : llvalue -> int -> llbasicblock -> unit
1084                       = "llvm_set_successor"
1085external num_successors : llvalue -> int = "llvm_num_successors"
1086
1087let successors llv =
1088  if not (is_terminator llv) then
1089    raise (Invalid_argument "Llvm.successors can only be used on terminators")
1090  else
1091    Array.init (num_successors llv) (successor llv)
1092
1093let iter_successors f llv =
1094  if not (is_terminator llv) then
1095    raise (Invalid_argument "Llvm.iter_successors can only be used on terminators")
1096  else
1097    for i = 0 to num_successors llv - 1 do
1098      f (successor llv i)
1099    done
1100
1101let fold_successors f llv z =
1102  if not (is_terminator llv) then
1103    raise (Invalid_argument "Llvm.fold_successors can only be used on terminators")
1104  else
1105    let n = num_successors llv in
1106    let rec aux i acc =
1107      if i >= n then acc
1108      else begin
1109        let llb = successor llv i in
1110        aux (i+1) (f llb acc)
1111      end
1112    in aux 0 z
1113
1114
1115(*--... Operations on branches .............................................--*)
1116external condition : llvalue -> llvalue = "llvm_condition"
1117external set_condition : llvalue -> llvalue -> unit
1118                       = "llvm_set_condition"
1119external is_conditional : llvalue -> bool = "llvm_is_conditional"
1120
1121let get_branch llv =
1122  if classify_value llv <> ValueKind.Instruction Opcode.Br then
1123    None
1124  else if is_conditional llv then
1125    Some (`Conditional (condition llv, successor llv 0, successor llv 1))
1126  else
1127    Some (`Unconditional (successor llv 0))
1128
1129(*--... Operations on phi nodes ............................................--*)
1130external add_incoming : (llvalue * llbasicblock) -> llvalue -> unit
1131                      = "llvm_add_incoming"
1132external incoming : llvalue -> (llvalue * llbasicblock) list = "llvm_incoming"
1133
1134external delete_instruction : llvalue -> unit = "llvm_delete_instruction"
1135
1136(*===-- Instruction builders ----------------------------------------------===*)
1137external builder : llcontext -> llbuilder = "llvm_builder"
1138external position_builder : (llbasicblock, llvalue) llpos -> llbuilder -> unit
1139                          = "llvm_position_builder"
1140external insertion_block : llbuilder -> llbasicblock = "llvm_insertion_block"
1141external insert_into_builder : llvalue -> string -> llbuilder -> unit
1142                             = "llvm_insert_into_builder"
1143
1144let builder_at context ip =
1145  let b = builder context in
1146  position_builder ip b;
1147  b
1148
1149let builder_before context i = builder_at context (Before i)
1150let builder_at_end context bb = builder_at context (At_end bb)
1151
1152let position_before i = position_builder (Before i)
1153let position_at_end bb = position_builder (At_end bb)
1154
1155
1156(*--... Metadata ...........................................................--*)
1157external set_current_debug_location : llbuilder -> llvalue -> unit
1158                                    = "llvm_set_current_debug_location"
1159external clear_current_debug_location : llbuilder -> unit
1160                                      = "llvm_clear_current_debug_location"
1161external current_debug_location : llbuilder -> llvalue option
1162                                    = "llvm_current_debug_location"
1163external set_inst_debug_location : llbuilder -> llvalue -> unit
1164                                 = "llvm_set_inst_debug_location"
1165
1166
1167(*--... Terminators ........................................................--*)
1168external build_ret_void : llbuilder -> llvalue = "llvm_build_ret_void"
1169external build_ret : llvalue -> llbuilder -> llvalue = "llvm_build_ret"
1170external build_aggregate_ret : llvalue array -> llbuilder -> llvalue
1171                             = "llvm_build_aggregate_ret"
1172external build_br : llbasicblock -> llbuilder -> llvalue = "llvm_build_br"
1173external build_cond_br : llvalue -> llbasicblock -> llbasicblock -> llbuilder ->
1174                         llvalue = "llvm_build_cond_br"
1175external build_switch : llvalue -> llbasicblock -> int -> llbuilder -> llvalue
1176                      = "llvm_build_switch"
1177external build_malloc : lltype -> string -> llbuilder -> llvalue =
1178    "llvm_build_malloc"
1179external build_array_malloc : lltype -> llvalue -> string -> llbuilder ->
1180    llvalue = "llvm_build_array_malloc"
1181external build_free : llvalue -> llbuilder -> llvalue = "llvm_build_free"
1182external add_case : llvalue -> llvalue -> llbasicblock -> unit
1183                  = "llvm_add_case"
1184external switch_default_dest : llvalue -> llbasicblock =
1185    "LLVMGetSwitchDefaultDest"
1186external build_indirect_br : llvalue -> int -> llbuilder -> llvalue
1187                           = "llvm_build_indirect_br"
1188external add_destination : llvalue -> llbasicblock -> unit
1189                         = "llvm_add_destination"
1190external build_invoke : llvalue -> llvalue array -> llbasicblock ->
1191                        llbasicblock -> string -> llbuilder -> llvalue
1192                      = "llvm_build_invoke_bc" "llvm_build_invoke_nat"
1193external build_landingpad : lltype -> llvalue -> int -> string -> llbuilder ->
1194                            llvalue = "llvm_build_landingpad"
1195external is_cleanup : llvalue -> bool = "llvm_is_cleanup"
1196external set_cleanup : llvalue -> bool -> unit = "llvm_set_cleanup"
1197external add_clause : llvalue -> llvalue -> unit = "llvm_add_clause"
1198external build_resume : llvalue -> llbuilder -> llvalue = "llvm_build_resume"
1199external build_unreachable : llbuilder -> llvalue = "llvm_build_unreachable"
1200
1201(*--... Arithmetic .........................................................--*)
1202external build_add : llvalue -> llvalue -> string -> llbuilder -> llvalue
1203                   = "llvm_build_add"
1204external build_nsw_add : llvalue -> llvalue -> string -> llbuilder -> llvalue
1205                       = "llvm_build_nsw_add"
1206external build_nuw_add : llvalue -> llvalue -> string -> llbuilder -> llvalue
1207                       = "llvm_build_nuw_add"
1208external build_fadd : llvalue -> llvalue -> string -> llbuilder -> llvalue
1209                    = "llvm_build_fadd"
1210external build_sub : llvalue -> llvalue -> string -> llbuilder -> llvalue
1211                   = "llvm_build_sub"
1212external build_nsw_sub : llvalue -> llvalue -> string -> llbuilder -> llvalue
1213                       = "llvm_build_nsw_sub"
1214external build_nuw_sub : llvalue -> llvalue -> string -> llbuilder -> llvalue
1215                       = "llvm_build_nuw_sub"
1216external build_fsub : llvalue -> llvalue -> string -> llbuilder -> llvalue
1217                    = "llvm_build_fsub"
1218external build_mul : llvalue -> llvalue -> string -> llbuilder -> llvalue
1219                   = "llvm_build_mul"
1220external build_nsw_mul : llvalue -> llvalue -> string -> llbuilder -> llvalue
1221                       = "llvm_build_nsw_mul"
1222external build_nuw_mul : llvalue -> llvalue -> string -> llbuilder -> llvalue
1223                       = "llvm_build_nuw_mul"
1224external build_fmul : llvalue -> llvalue -> string -> llbuilder -> llvalue
1225                    = "llvm_build_fmul"
1226external build_udiv : llvalue -> llvalue -> string -> llbuilder -> llvalue
1227                    = "llvm_build_udiv"
1228external build_sdiv : llvalue -> llvalue -> string -> llbuilder -> llvalue
1229                    = "llvm_build_sdiv"
1230external build_exact_sdiv : llvalue -> llvalue -> string -> llbuilder -> llvalue
1231                          = "llvm_build_exact_sdiv"
1232external build_fdiv : llvalue -> llvalue -> string -> llbuilder -> llvalue
1233                    = "llvm_build_fdiv"
1234external build_urem : llvalue -> llvalue -> string -> llbuilder -> llvalue
1235                    = "llvm_build_urem"
1236external build_srem : llvalue -> llvalue -> string -> llbuilder -> llvalue
1237                    = "llvm_build_srem"
1238external build_frem : llvalue -> llvalue -> string -> llbuilder -> llvalue
1239                    = "llvm_build_frem"
1240external build_shl : llvalue -> llvalue -> string -> llbuilder -> llvalue
1241                   = "llvm_build_shl"
1242external build_lshr : llvalue -> llvalue -> string -> llbuilder -> llvalue
1243                    = "llvm_build_lshr"
1244external build_ashr : llvalue -> llvalue -> string -> llbuilder -> llvalue
1245                    = "llvm_build_ashr"
1246external build_and : llvalue -> llvalue -> string -> llbuilder -> llvalue
1247                   = "llvm_build_and"
1248external build_or : llvalue -> llvalue -> string -> llbuilder -> llvalue
1249                  = "llvm_build_or"
1250external build_xor : llvalue -> llvalue -> string -> llbuilder -> llvalue
1251                   = "llvm_build_xor"
1252external build_neg : llvalue -> string -> llbuilder -> llvalue
1253                   = "llvm_build_neg"
1254external build_nsw_neg : llvalue -> string -> llbuilder -> llvalue
1255                       = "llvm_build_nsw_neg"
1256external build_nuw_neg : llvalue -> string -> llbuilder -> llvalue
1257                       = "llvm_build_nuw_neg"
1258external build_fneg : llvalue -> string -> llbuilder -> llvalue
1259                    = "llvm_build_fneg"
1260external build_not : llvalue -> string -> llbuilder -> llvalue
1261                   = "llvm_build_not"
1262
1263(*--... Memory .............................................................--*)
1264external build_alloca : lltype -> string -> llbuilder -> llvalue
1265                      = "llvm_build_alloca"
1266external build_array_alloca : lltype -> llvalue -> string -> llbuilder ->
1267                              llvalue = "llvm_build_array_alloca"
1268external build_load : llvalue -> string -> llbuilder -> llvalue
1269                    = "llvm_build_load"
1270external build_store : llvalue -> llvalue -> llbuilder -> llvalue
1271                     = "llvm_build_store"
1272external build_atomicrmw : AtomicRMWBinOp.t -> llvalue -> llvalue ->
1273                           AtomicOrdering.t -> bool -> string -> llbuilder ->
1274                           llvalue
1275                         = "llvm_build_atomicrmw_bytecode"
1276                           "llvm_build_atomicrmw_native"
1277external build_gep : llvalue -> llvalue array -> string -> llbuilder -> llvalue
1278                   = "llvm_build_gep"
1279external build_in_bounds_gep : llvalue -> llvalue array -> string ->
1280                             llbuilder -> llvalue = "llvm_build_in_bounds_gep"
1281external build_struct_gep : llvalue -> int -> string -> llbuilder -> llvalue
1282                         = "llvm_build_struct_gep"
1283
1284external build_global_string : string -> string -> llbuilder -> llvalue
1285                             = "llvm_build_global_string"
1286external build_global_stringptr  : string -> string -> llbuilder -> llvalue
1287                                 = "llvm_build_global_stringptr"
1288
1289(*--... Casts ..............................................................--*)
1290external build_trunc : llvalue -> lltype -> string -> llbuilder -> llvalue
1291                     = "llvm_build_trunc"
1292external build_zext : llvalue -> lltype -> string -> llbuilder -> llvalue
1293                    = "llvm_build_zext"
1294external build_sext : llvalue -> lltype -> string -> llbuilder -> llvalue
1295                    = "llvm_build_sext"
1296external build_fptoui : llvalue -> lltype -> string -> llbuilder -> llvalue
1297                      = "llvm_build_fptoui"
1298external build_fptosi : llvalue -> lltype -> string -> llbuilder -> llvalue
1299                      = "llvm_build_fptosi"
1300external build_uitofp : llvalue -> lltype -> string -> llbuilder -> llvalue
1301                      = "llvm_build_uitofp"
1302external build_sitofp : llvalue -> lltype -> string -> llbuilder -> llvalue
1303                      = "llvm_build_sitofp"
1304external build_fptrunc : llvalue -> lltype -> string -> llbuilder -> llvalue
1305                       = "llvm_build_fptrunc"
1306external build_fpext : llvalue -> lltype -> string -> llbuilder -> llvalue
1307                     = "llvm_build_fpext"
1308external build_ptrtoint : llvalue -> lltype -> string -> llbuilder -> llvalue
1309                        = "llvm_build_prttoint"
1310external build_inttoptr : llvalue -> lltype -> string -> llbuilder -> llvalue
1311                        = "llvm_build_inttoptr"
1312external build_bitcast : llvalue -> lltype -> string -> llbuilder -> llvalue
1313                       = "llvm_build_bitcast"
1314external build_zext_or_bitcast : llvalue -> lltype -> string -> llbuilder ->
1315                                 llvalue = "llvm_build_zext_or_bitcast"
1316external build_sext_or_bitcast : llvalue -> lltype -> string -> llbuilder ->
1317                                 llvalue = "llvm_build_sext_or_bitcast"
1318external build_trunc_or_bitcast : llvalue -> lltype -> string -> llbuilder ->
1319                                  llvalue = "llvm_build_trunc_or_bitcast"
1320external build_pointercast : llvalue -> lltype -> string -> llbuilder -> llvalue
1321                           = "llvm_build_pointercast"
1322external build_intcast : llvalue -> lltype -> string -> llbuilder -> llvalue
1323                       = "llvm_build_intcast"
1324external build_fpcast : llvalue -> lltype -> string -> llbuilder -> llvalue
1325                      = "llvm_build_fpcast"
1326
1327(*--... Comparisons ........................................................--*)
1328external build_icmp : Icmp.t -> llvalue -> llvalue -> string ->
1329                      llbuilder -> llvalue = "llvm_build_icmp"
1330external build_fcmp : Fcmp.t -> llvalue -> llvalue -> string ->
1331                      llbuilder -> llvalue = "llvm_build_fcmp"
1332
1333(*--... Miscellaneous instructions .........................................--*)
1334external build_phi : (llvalue * llbasicblock) list -> string -> llbuilder ->
1335                     llvalue = "llvm_build_phi"
1336external build_empty_phi : lltype -> string -> llbuilder -> llvalue
1337                         = "llvm_build_empty_phi"
1338external build_call : llvalue -> llvalue array -> string -> llbuilder -> llvalue
1339                    = "llvm_build_call"
1340external build_select : llvalue -> llvalue -> llvalue -> string -> llbuilder ->
1341                        llvalue = "llvm_build_select"
1342external build_va_arg : llvalue -> lltype -> string -> llbuilder -> llvalue
1343                      = "llvm_build_va_arg"
1344external build_extractelement : llvalue -> llvalue -> string -> llbuilder ->
1345                                llvalue = "llvm_build_extractelement"
1346external build_insertelement : llvalue -> llvalue -> llvalue -> string ->
1347                               llbuilder -> llvalue = "llvm_build_insertelement"
1348external build_shufflevector : llvalue -> llvalue -> llvalue -> string ->
1349                               llbuilder -> llvalue = "llvm_build_shufflevector"
1350external build_extractvalue : llvalue -> int -> string -> llbuilder -> llvalue
1351                            = "llvm_build_extractvalue"
1352external build_insertvalue : llvalue -> llvalue -> int -> string -> llbuilder ->
1353                             llvalue = "llvm_build_insertvalue"
1354
1355external build_is_null : llvalue -> string -> llbuilder -> llvalue
1356                       = "llvm_build_is_null"
1357external build_is_not_null : llvalue -> string -> llbuilder -> llvalue
1358                           = "llvm_build_is_not_null"
1359external build_ptrdiff : llvalue -> llvalue -> string -> llbuilder -> llvalue
1360                       = "llvm_build_ptrdiff"
1361external build_freeze : llvalue -> string -> llbuilder -> llvalue
1362                      = "llvm_build_freeze"
1363
1364
1365(*===-- Memory buffers ----------------------------------------------------===*)
1366
1367module MemoryBuffer = struct
1368  external of_file : string -> llmemorybuffer = "llvm_memorybuffer_of_file"
1369  external of_stdin : unit -> llmemorybuffer = "llvm_memorybuffer_of_stdin"
1370  external of_string : ?name:string -> string -> llmemorybuffer
1371                     = "llvm_memorybuffer_of_string"
1372  external as_string : llmemorybuffer -> string = "llvm_memorybuffer_as_string"
1373  external dispose : llmemorybuffer -> unit = "llvm_memorybuffer_dispose"
1374end
1375
1376
1377(*===-- Pass Manager ------------------------------------------------------===*)
1378
1379module PassManager = struct
1380  type 'a t
1381  type any = [ `Module | `Function ]
1382  external create : unit -> [ `Module ] t = "llvm_passmanager_create"
1383  external create_function : llmodule -> [ `Function ] t
1384                           = "LLVMCreateFunctionPassManager"
1385  external run_module : llmodule -> [ `Module ] t -> bool
1386                      = "llvm_passmanager_run_module"
1387  external initialize : [ `Function ] t -> bool = "llvm_passmanager_initialize"
1388  external run_function : llvalue -> [ `Function ] t -> bool
1389                        = "llvm_passmanager_run_function"
1390  external finalize : [ `Function ] t -> bool = "llvm_passmanager_finalize"
1391  external dispose : [< any ] t -> unit = "llvm_passmanager_dispose"
1392end
1393