Module Llvm

module Llvm: sig .. end
Core API.

This interface provides an OCaml API for the LLVM intermediate representation, the classes in the VMCore library.



Abstract types

These abstract types correlate directly to the LLVMCore classes.

type llcontext 
The top-level container for all LLVM global data. See the llvm::LLVMContext class.
type llmodule 
The top-level container for all other LLVM Intermediate Representation (IR) objects. See the llvm::Module class.
type lltype 
Each value in the LLVM IR has a type, an instance of lltype. See the llvm::Type class.
type llvalue 
Any value in the LLVM IR. Functions, instructions, global variables, constants, and much more are all llvalues. See the llvm::Value class. This type covers a wide range of subclasses.
type lluse 
Used to store users and usees of values. See the llvm::Use class.
type llbasicblock 
A basic block in LLVM IR. See the llvm::BasicBlock class.
type llbuilder 
Used to generate instructions in the LLVM IR. See the llvm::LLVMBuilder class.
type llattrkind 
Used to represent attribute kinds.
type llattribute 
An attribute in LLVM IR. See the llvm::Attribute class.
type llmemorybuffer 
Used to efficiently handle large buffers of read-only binary data. See the llvm::MemoryBuffer class.
type llmdkind 
The kind id of metadata attached to an instruction.
module TypeKind: sig .. end
The kind of an lltype, the result of classify_type ty.
module Linkage: sig .. end
The linkage of a global value, accessed with Llvm.linkage and Llvm.set_linkage.
module Visibility: sig .. end
The linker visibility of a global value, accessed with Llvm.visibility and Llvm.set_visibility.
module DLLStorageClass: sig .. end
The DLL storage class of a global value, accessed with Llvm.dll_storage_class and Llvm.set_dll_storage_class.
module CallConv: sig .. end
The following calling convention values may be accessed with Llvm.function_call_conv and Llvm.set_function_call_conv.
module AttrRepr: sig .. end
The logical representation of an attribute.
module AttrIndex: sig .. end
The position of an attribute.
module Icmp: sig .. end
The predicate for an integer comparison (icmp) instruction.
module Fcmp: sig .. end
The predicate for a floating-point comparison (fcmp) instruction.
module Opcode: sig .. end
The opcodes for LLVM instructions and constant expressions.
module LandingPadClauseTy: sig .. end
The type of a clause of a landingpad instruction.
module ThreadLocalMode: sig .. end
The thread local mode of a global value, accessed with Llvm.thread_local_mode and Llvm.set_thread_local_mode.
module AtomicOrdering: sig .. end
The ordering of an atomic load, store, cmpxchg, atomicrmw or fence instruction.
module AtomicRMWBinOp: sig .. end
The opcode of an atomicrmw instruction.
module ValueKind: sig .. end
The kind of an llvalue, the result of classify_value v.
module DiagnosticSeverity: sig .. end
The kind of Diagnostic, the result of Diagnostic.severity d.

Iteration

type ('a, 'b) llpos = 
| At_end of 'a
| Before of 'b
Before b and At_end a specify positions from the start of the 'b list of a. llpos is used to specify positions in and for forward iteration through the various value lists maintained by the LLVM IR.
type ('a, 'b) llrev_pos = 
| At_start of 'a
| After of 'b
After b and At_start a specify positions from the end of the 'b list of a. llrev_pos is used for reverse iteration through the various value lists maintained by the LLVM IR.

Exceptions

exception FeatureDisabled of string
exception IoError of string

Global configuration

val enable_pretty_stacktrace : unit -> unit
enable_pretty_stacktraces () enables LLVM's built-in stack trace code. This intercepts the OS's crash signals and prints which component of LLVM you were in at the time of the crash.
val install_fatal_error_handler : (string -> unit) -> unit
install_fatal_error_handler f installs f as LLVM's fatal error handler. The handler will receive the reason for termination as a string. After the handler has been executed, LLVM calls exit(1).
val reset_fatal_error_handler : unit -> unit
reset_fatal_error_handler () resets LLVM's fatal error handler.
val parse_command_line_options : ?overview:string -> string array -> unit
parse_command_line_options ?overview args parses args using the LLVM command line parser. Note that the only stable thing about this function is its signature; you cannot rely on any particular set of command line arguments being interpreted the same way across LLVM versions.

See the function llvm::cl::ParseCommandLineOptions().


Context error handling

module Diagnostic: sig .. end
val set_diagnostic_handler : llcontext -> (Diagnostic.t -> unit) option -> unit
set_diagnostic_handler c h set the diagnostic handler of c to h. See the method llvm::LLVMContext::setDiagnosticHandler.

Contexts

val create_context : unit -> llcontext
create_context () creates a context for storing the "global" state in LLVM. See the constructor llvm::LLVMContext.
val dispose_context : llcontext -> unit
destroy_context () destroys a context. See the destructor llvm::LLVMContext::~LLVMContext.
val global_context : unit -> llcontext
See the function LLVMGetGlobalContext.
val mdkind_id : llcontext -> string -> llmdkind
mdkind_id context name returns the MDKind ID that corresponds to the name name in the context context. See the function llvm::LLVMContext::getMDKindID.

Attributes

exception UnknownAttribute of string
UnknownAttribute attr is raised when a enum attribute name name is not recognized by LLVM.
val enum_attr_kind : string -> llattrkind
enum_attr_kind name returns the kind of enum attributes named name. May raise UnknownAttribute.
val create_enum_attr : llcontext -> string -> int64 -> llattribute
create_enum_attr context value kind creates an enum attribute with the supplied kind and value in context; if the value is not required (as for the majority of attributes), use 0L. May raise UnknownAttribute. See the constructor llvm::Attribute::get.
val create_string_attr : llcontext -> string -> string -> llattribute
create_string_attr context kind value creates a string attribute with the supplied kind and value in context. See the constructor llvm::Attribute::get.
val attr_of_repr : llcontext -> AttrRepr.t -> llattribute
attr_of_repr context repr creates an attribute with the supplied representation repr in context.
val repr_of_attr : llattribute -> AttrRepr.t
repr_of_attr attr describes the representation of attribute attr.

Modules

val create_module : llcontext -> string -> llmodule
create_module context id creates a module with the supplied module ID in the context context. Modules are not garbage collected; it is mandatory to call Llvm.dispose_module to free memory. See the constructor llvm::Module::Module.
val dispose_module : llmodule -> unit
dispose_module m destroys a module m and all of the IR objects it contained. All references to subordinate objects are invalidated; referencing them will invoke undefined behavior. See the destructor llvm::Module::~Module.
val target_triple : llmodule -> string
target_triple m is the target specifier for the module m, something like i686-apple-darwin8. See the method llvm::Module::getTargetTriple.
val set_target_triple : string -> llmodule -> unit
target_triple triple m changes the target specifier for the module m to the string triple. See the method llvm::Module::setTargetTriple.
val data_layout : llmodule -> string
data_layout m is the data layout specifier for the module m, something like e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-...-a0:0:64-f80:128:128. See the method llvm::Module::getDataLayout.
val set_data_layout : string -> llmodule -> unit
set_data_layout s m changes the data layout specifier for the module m to the string s. See the method llvm::Module::setDataLayout.
val dump_module : llmodule -> unit
dump_module m prints the .ll representation of the module m to standard error. See the method llvm::Module::dump.
val print_module : string -> llmodule -> unit
print_module f m prints the .ll representation of the module m to file f. See the method llvm::Module::print.
val string_of_llmodule : llmodule -> string
string_of_llmodule m returns the .ll representation of the module m as a string. See the method llvm::Module::print.
val set_module_inline_asm : llmodule -> string -> unit
set_module_inline_asm m asm sets the inline assembler for the module. See the method llvm::Module::setModuleInlineAsm.
val module_context : llmodule -> llcontext
module_context m returns the context of the specified module. See the method llvm::Module::getContext

Types

val classify_type : lltype -> TypeKind.t
classify_type ty returns the Llvm.TypeKind.t corresponding to the type ty. See the method llvm::Type::getTypeID.
val type_is_sized : lltype -> bool
type_is_sized ty returns whether the type has a size or not. If it doesn't then it is not safe to call the DataLayout:: methods on it.
val type_context : lltype -> llcontext
type_context ty returns the Llvm.llcontext corresponding to the type ty. See the method llvm::Type::getContext.
val dump_type : lltype -> unit
dump_type ty prints the .ll representation of the type ty to standard error. See the method llvm::Type::dump.
val string_of_lltype : lltype -> string
string_of_lltype ty returns a string describing the type ty.

Operations on integer types

val i1_type : llcontext -> lltype
i1_type c returns an integer type of bitwidth 1 in the context c. See llvm::Type::Int1Ty.
val i8_type : llcontext -> lltype
i8_type c returns an integer type of bitwidth 8 in the context c. See llvm::Type::Int8Ty.
val i16_type : llcontext -> lltype
i16_type c returns an integer type of bitwidth 16 in the context c. See llvm::Type::Int16Ty.
val i32_type : llcontext -> lltype
i32_type c returns an integer type of bitwidth 32 in the context c. See llvm::Type::Int32Ty.
val i64_type : llcontext -> lltype
i64_type c returns an integer type of bitwidth 64 in the context c. See llvm::Type::Int64Ty.
val integer_type : llcontext -> int -> lltype
integer_type c n returns an integer type of bitwidth n in the context c. See the method llvm::IntegerType::get.
val integer_bitwidth : lltype -> int
integer_bitwidth c ty returns the number of bits in the integer type ty in the context c. See the method llvm::IntegerType::getBitWidth.

Operations on real types

val float_type : llcontext -> lltype
float_type c returns the IEEE 32-bit floating point type in the context c. See llvm::Type::FloatTy.
val double_type : llcontext -> lltype
double_type c returns the IEEE 64-bit floating point type in the context c. See llvm::Type::DoubleTy.
val x86fp80_type : llcontext -> lltype
x86fp80_type c returns the x87 80-bit floating point type in the context c. See llvm::Type::X86_FP80Ty.
val fp128_type : llcontext -> lltype
fp128_type c returns the IEEE 128-bit floating point type in the context c. See llvm::Type::FP128Ty.
val ppc_fp128_type : llcontext -> lltype
ppc_fp128_type c returns the PowerPC 128-bit floating point type in the context c. See llvm::Type::PPC_FP128Ty.

Operations on function types

val function_type : lltype -> lltype array -> lltype
function_type ret_ty param_tys returns the function type returning ret_ty and taking param_tys as parameters. See the method llvm::FunctionType::get.
val var_arg_function_type : lltype -> lltype array -> lltype
var_arg_function_type ret_ty param_tys is just like function_type ret_ty param_tys except that it returns the function type which also takes a variable number of arguments. See the method llvm::FunctionType::get.
val is_var_arg : lltype -> bool
is_var_arg fty returns true if fty is a varargs function type, false otherwise. See the method llvm::FunctionType::isVarArg.
val return_type : lltype -> lltype
return_type fty gets the return type of the function type fty. See the method llvm::FunctionType::getReturnType.
val param_types : lltype -> lltype array
param_types fty gets the parameter types of the function type fty. See the method llvm::FunctionType::getParamType.

Operations on struct types

val struct_type : llcontext -> lltype array -> lltype
struct_type context tys returns the structure type in the context context containing in the types in the array tys. See the method llvm::StructType::get.
val packed_struct_type : llcontext -> lltype array -> lltype
packed_struct_type context ys returns the packed structure type in the context context containing in the types in the array tys. See the method llvm::StructType::get.
val struct_name : lltype -> string option
struct_name ty returns the name of the named structure type ty, or None if the structure type is not named
val named_struct_type : llcontext -> string -> lltype
named_struct_type context name returns the named structure type name in the context context. See the method llvm::StructType::get.
val struct_set_body : lltype -> lltype array -> bool -> unit
struct_set_body ty elts ispacked sets the body of the named struct ty to the elts elements. See the moethd llvm::StructType::setBody.
val struct_element_types : lltype -> lltype array
struct_element_types sty returns the constituent types of the struct type sty. See the method llvm::StructType::getElementType.
val is_packed : lltype -> bool
is_packed sty returns true if the structure type sty is packed, false otherwise. See the method llvm::StructType::isPacked.
val is_opaque : lltype -> bool
is_opaque sty returns true if the structure type sty is opaque. false otherwise. See the method llvm::StructType::isOpaque.

Operations on pointer, vector, and array types

val subtypes : lltype -> lltype array
subtypes ty returns ty's subtypes
val array_type : lltype -> int -> lltype
array_type ty n returns the array type containing n elements of type ty. See the method llvm::ArrayType::get.
val pointer_type : lltype -> lltype
pointer_type ty returns the pointer type referencing objects of type ty in the default address space (0). See the method llvm::PointerType::getUnqual.
val qualified_pointer_type : lltype -> int -> lltype
qualified_pointer_type ty as returns the pointer type referencing objects of type ty in address space as. See the method llvm::PointerType::get.
val vector_type : lltype -> int -> lltype
vector_type ty n returns the array type containing n elements of the primitive type ty. See the method llvm::ArrayType::get.
val element_type : lltype -> lltype
element_type ty returns the element type of the pointer, vector, or array type ty. See the method llvm::SequentialType::get.
val array_length : lltype -> int
element_type aty returns the element count of the array type aty. See the method llvm::ArrayType::getNumElements.
val address_space : lltype -> int
address_space pty returns the address space qualifier of the pointer type pty. See the method llvm::PointerType::getAddressSpace.
val vector_size : lltype -> int
element_type ty returns the element count of the vector type ty. See the method llvm::VectorType::getNumElements.

Operations on other types

val void_type : llcontext -> lltype
void_type c creates a type of a function which does not return any value in the context c. See llvm::Type::VoidTy.
val label_type : llcontext -> lltype
label_type c creates a type of a basic block in the context c. See llvm::Type::LabelTy.
val x86_mmx_type : llcontext -> lltype
x86_mmx_type c returns the x86 64-bit MMX register type in the context c. See llvm::Type::X86_MMXTy.
val type_by_name : llmodule -> string -> lltype option
type_by_name m name returns the specified type from the current module if it exists. See the method llvm::Module::getTypeByName

Values

val type_of : llvalue -> lltype
type_of v returns the type of the value v. See the method llvm::Value::getType.
val classify_value : llvalue -> ValueKind.t
classify_value v returns the kind of the value v.
val value_name : llvalue -> string
value_name v returns the name of the value v. For global values, this is the symbol name. For instructions and basic blocks, it is the SSA register name. It is meaningless for constants. See the method llvm::Value::getName.
val set_value_name : string -> llvalue -> unit
set_value_name n v sets the name of the value v to n. See the method llvm::Value::setName.
val dump_value : llvalue -> unit
dump_value v prints the .ll representation of the value v to standard error. See the method llvm::Value::dump.
val string_of_llvalue : llvalue -> string
string_of_llvalue v returns a string describing the value v.
val replace_all_uses_with : llvalue -> llvalue -> unit
replace_all_uses_with old new replaces all uses of the value old with the value new. See the method llvm::Value::replaceAllUsesWith.

Uses

val use_begin : llvalue -> lluse option
use_begin v returns the first position in the use list for the value v. use_begin and use_succ can e used to iterate over the use list in order. See the method llvm::Value::use_begin.
val use_succ : lluse -> lluse option
use_succ u returns the use list position succeeding u. See the method llvm::use_value_iterator::operator++.
val user : lluse -> llvalue
user u returns the user of the use u. See the method llvm::Use::getUser.
val used_value : lluse -> llvalue
used_value u returns the usee of the use u. See the method llvm::Use::getUsedValue.
val iter_uses : (lluse -> unit) -> llvalue -> unit
iter_uses f v applies function f to each of the users of the value v in order. Tail recursive.
val fold_left_uses : ('a -> lluse -> 'a) -> 'a -> llvalue -> 'a
fold_left_uses f init v is f (... (f init u1) ...) uN where u1,...,uN are the users of the value v. Tail recursive.
val fold_right_uses : (lluse -> 'a -> 'a) -> llvalue -> 'a -> 'a
fold_right_uses f v init is f u1 (... (f uN init) ...) where u1,...,uN are the users of the value v. Not tail recursive.

Users

val operand : llvalue -> int -> llvalue
operand v i returns the operand at index i for the value v. See the method llvm::User::getOperand.
val operand_use : llvalue -> int -> lluse
operand_use v i returns the use of the operand at index i for the value v. See the method llvm::User::getOperandUse.
val set_operand : llvalue -> int -> llvalue -> unit
set_operand v i o sets the operand of the value v at the index i to the value o. See the method llvm::User::setOperand.
val num_operands : llvalue -> int
num_operands v returns the number of operands for the value v. See the method llvm::User::getNumOperands.

Operations on constants of (mostly) any type

val is_constant : llvalue -> bool
is_constant v returns true if the value v is a constant, false otherwise. Similar to llvm::isa<Constant>.
val const_null : lltype -> llvalue
const_null ty returns the constant null (zero) of the type ty. See the method llvm::Constant::getNullValue.
val const_all_ones : lltype -> llvalue
const_all_ones ty returns the constant '-1' of the integer or vector type ty. See the method llvm::Constant::getAllOnesValue.
val const_pointer_null : lltype -> llvalue
const_pointer_null ty returns the constant null (zero) pointer of the type ty. See the method llvm::ConstantPointerNull::get.
val undef : lltype -> llvalue
undef ty returns the undefined value of the type ty. See the method llvm::UndefValue::get.
val is_null : llvalue -> bool
is_null v returns true if the value v is the null (zero) value. See the method llvm::Constant::isNullValue.
val is_undef : llvalue -> bool
is_undef v returns true if the value v is an undefined value, false otherwise. Similar to llvm::isa<UndefValue>.
val constexpr_opcode : llvalue -> Opcode.t
constexpr_opcode v returns an Opcode.t corresponding to constexpr value v, or Opcode.Invalid if v is not a constexpr.

Operations on instructions

val has_metadata : llvalue -> bool
has_metadata i returns whether or not the instruction i has any metadata attached to it. See the function llvm::Instruction::hasMetadata.
val metadata : llvalue -> llmdkind -> llvalue option
metadata i kind optionally returns the metadata associated with the kind kind in the instruction i See the function llvm::Instruction::getMetadata.
val set_metadata : llvalue -> llmdkind -> llvalue -> unit
set_metadata i kind md sets the metadata md of kind kind in the instruction i. See the function llvm::Instruction::setMetadata.
val clear_metadata : llvalue -> llmdkind -> unit
clear_metadata i kind clears the metadata of kind kind in the instruction i. See the function llvm::Instruction::setMetadata.

Operations on metadata

val mdstring : llcontext -> string -> llvalue
mdstring c s returns the MDString of the string s in the context c. See the method llvm::MDNode::get.
val mdnode : llcontext -> llvalue array -> llvalue
mdnode c elts returns the MDNode containing the values elts in the context c. See the method llvm::MDNode::get.
val mdnull : llcontext -> llvalue
mdnull c  returns a null MDNode in context c.
val get_mdstring : llvalue -> string option
get_mdstring v returns the MDString. See the method llvm::MDString::getString
val get_mdnode_operands : llvalue -> llvalue array
get_mdnode_operands v returns the operands in the MDNode.
val get_named_metadata : llmodule -> string -> llvalue array
get_named_metadata m name returns all the MDNodes belonging to the named metadata (if any). See the method llvm::NamedMDNode::getOperand.
val add_named_metadata_operand : llmodule -> string -> llvalue -> unit
add_named_metadata_operand m name v adds v as the last operand of metadata named name in module m. If the metadata does not exist, it is created. See the methods llvm::Module::getNamedMetadata() and llvm::MDNode::addOperand().

Operations on scalar constants

val const_int : lltype -> int -> llvalue
const_int ty i returns the integer constant of type ty and value i. See the method llvm::ConstantInt::get.
val const_of_int64 : lltype -> Int64.t -> bool -> llvalue
const_of_int64 ty i returns the integer constant of type ty and value i. See the method llvm::ConstantInt::get.
val int64_of_const : llvalue -> Int64.t option
int64_of_const c returns the int64 value of the c constant integer. None is returned if this is not an integer constant, or bitwidth exceeds 64. See the method llvm::Const