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