llvm-0.8.1.0: Bindings to the LLVM compiler toolkit.

LLVM.Core

Contents

Description

The LLVM (Low Level Virtual Machine) is virtual machine at a machine code level. It supports both stand alone code generation and JITing. The Haskell llvm package is a (relatively) high level interface to the LLVM. The high level interface makes it easy to construct LLVM code. There is also an interface to the raw low level LLVM API as exposed by the LLVM C interface.

LLVM code is organized into modules (type Module). Each module contains a number of global variables and functions (type Function). Each functions has a number of basic blocks (type BasicBlock). Each basic block has a number instructions, where each instruction produces a value (type Value).

Unlike assembly code for a real processor the assembly code for LLVM is in SSA (Static Single Assignment) form. This means that each instruction generates a new bound variable which may not be assigned again. A consequence of this is that where control flow joins from several execution paths there has to be a phi pseudo instruction if you want different variables to be joined into one.

The definition of several of the LLVM entities (Module, Function, and BasicBlock) follow the same pattern. First the entity has to be created using newX (where X is one of Module, Function, or BasicBlock), then at some later point it has to given its definition using defineX. The reason for splitting the creation and definition is that you often need to be able to refer to an entity before giving it's body, e.g., in two mutually recursive functions. The the newX and defineX function can also be done at the same time by using createX. Furthermore, an explicit name can be given to an entity by the newNamedX function; the newX function just generates a fresh name.

Synopsis

Initialize

initializeNativeTarget :: IO ()Source

Initialize jitter to the native target. The operation is idempotent.

Modules

data Module Source

Type of top level modules.

newModule :: IO ModuleSource

Create a new module.

newNamedModuleSource

Arguments

:: String

module name

-> IO Module 

Create a new explicitely named module.

defineModuleSource

Arguments

:: Module

module that is defined

-> CodeGenModule a

module body

-> IO a 

Give the body for a module.

destroyModule :: Module -> IO ()Source

Free all storage related to a module. *Note*, this is a dangerous call, since referring to the module after this call is an error. The reason for the explicit call to free the module instead of an automatic lifetime management is that modules have a somewhat complicated ownership. Handing a module to a module provider changes the ownership of the module, and the module provider will free the module when necessary.

createModuleSource

Arguments

:: CodeGenModule a

module body

-> IO a 

Create a new module with the given body.

data ModuleProvider Source

A module provider is used by the code generator to get access to a module.

createModuleProviderForExistingModule :: Module -> IO ModuleProviderSource

Turn a module into a module provider.

data PassManager Source

Manage compile passes.

createPassManager :: IO PassManagerSource

Create a pass manager.

createFunctionPassManager :: ModuleProvider -> IO PassManagerSource

Create a pass manager for a module.

writeBitcodeToFile :: String -> Module -> IO ()Source

Write a module to a file.

readBitcodeFromFile :: String -> IO ModuleSource

Read a module from a file.

Instructions

Terminator instructions

ret :: Ret a r => a -> CodeGenFunction r TerminateSource

Return from the current function with the given value. Use () as the return value for what would be a void function is C.

condBrSource

Arguments

:: Value Bool

Boolean to branch upon.

-> BasicBlock

Target for true.

-> BasicBlock

Target for false.

-> CodeGenFunction r Terminate 

Branch to the first basic block if the boolean is true, otherwise to the second basic block.

brSource

Arguments

:: BasicBlock

Branch target.

-> CodeGenFunction r Terminate 

Unconditionally branch to the given basic block.

switchSource

Arguments

:: IsInteger a 
=> Value a

Value to branch upon.

-> BasicBlock

Default branch target.

-> [(ConstValue a, BasicBlock)]

Labels and corresponding branch targets.

-> CodeGenFunction r Terminate 

Branch table instruction.

invokeSource

Arguments

:: CallArgs f g 
=> BasicBlock

Normal return point.

-> BasicBlock

Exception return point.

-> Function f

Function to call.

-> g 

Call a function with exception handling.

unwind :: CodeGenFunction r TerminateSource

Unwind the call stack until a function call performed with invoke is reached. I.e., throw a non-local exception.

unreachable :: CodeGenFunction r TerminateSource

Inform the code generator that this code can never be reached.

Arithmetic binary operations

Arithmetic operations with the normal semantics. The u instractions are unsigned, the s instructions are signed.

add :: (IsArithmetic c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

sub :: (IsArithmetic c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

mul :: (IsArithmetic c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

fadd :: (IsFloating c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

fsub :: (IsFloating c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

fmul :: (IsFloating c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

udiv :: (IsInteger c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

sdiv :: (IsInteger c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

fdiv :: (IsFloating c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

Floating point division.

urem :: (IsInteger c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

srem :: (IsInteger c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

frem :: (IsFloating c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

Floating point remainder.

Logical binary operations

Logical instructions with the normal semantics.

shl :: (IsInteger c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

lshr :: (IsInteger c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

ashr :: (IsInteger c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

and :: (IsInteger c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

or :: (IsInteger c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

xor :: (IsInteger c, ABinOp a b (v c)) => a -> b -> CodeGenFunction r (v c)Source

Vector operations

extractelementSource

Arguments

:: Value (Vector n a)

Vector

-> Value Word32

Index into the vector

-> CodeGenFunction r (Value a) 

Get a value from a vector.

insertelementSource

Arguments

:: Value (Vector n a)

Vector

-> Value a

Value to insert

-> Value Word32

Index into the vector

-> CodeGenFunction r (Value (Vector n a)) 

Insert a value into a vector, nondestructive.

shufflevector :: Value (Vector n a) -> Value (Vector n a) -> ConstValue (Vector n Word32) -> CodeGenFunction r (Value (Vector n a))Source

Permute vector.

Aggregate operations

extractvalueSource

Arguments

:: forall r agg i a . GetValue agg i a 
=> Value agg

Aggregate

-> i

Index into the aggregate

-> CodeGenFunction r (Value a) 

Get a value from an aggregate.

insertvalueSource

Arguments

:: forall r agg i a . GetValue agg i a 
=> Value agg

Aggregate

-> Value a

Value to insert

-> i

Index into the aggregate

-> CodeGenFunction r (Value agg) 

Insert a value into an aggregate, nondestructive.

Memory access

malloc :: forall a r s. IsSized a s => CodeGenFunction r (Value (Ptr a))Source

Allocate heap memory.

arrayMalloc :: forall a n r s. (IsSized a n, AllocArg s) => s -> CodeGenFunction r (Value (Ptr a))Source

Allocate heap (array) memory.

alloca :: forall a r s. IsSized a s => CodeGenFunction r (Value (Ptr a))Source

Allocate stack memory.

arrayAlloca :: forall a n r s. (IsSized a n, AllocArg s) => s -> CodeGenFunction r (Value (Ptr a))Source

Allocate stack (array) memory.

free :: Value (Ptr a) -> CodeGenFunction r (Value ())Source

Free heap memory.

loadSource

Arguments

:: Value (Ptr a)

Address to load from.

-> CodeGenFunction r (Value a) 

Load a value from memory.

storeSource

Arguments

:: Value a

Value to store.

-> Value (Ptr a)

Address to store to.

-> CodeGenFunction r () 

Store a value in memory

getElementPtr :: forall a o i n r. (GetElementPtr o i n, IsIndexArg a) => Value (Ptr o) -> (a, i) -> CodeGenFunction r (Value (Ptr n))Source

Address arithmetic. See LLVM description. The index is a nested tuple of the form (i1,(i2,( ... ()))). (This is without a doubt the most confusing LLVM instruction, but the types help.)

getElementPtr0 :: GetElementPtr o i n => Value (Ptr o) -> i -> CodeGenFunction r (Value (Ptr n))Source

Like getElementPtr, but with an initial index that is 0. This is useful since any pointer first need to be indexed off the pointer, and then into its actual value. This first indexing is often with 0.

Conversions

trunc :: (IsInteger a, IsInteger b, IsPrimitive a, IsPrimitive b, IsSized a sa, IsSized b sb, sa :>: sb) => Value a -> CodeGenFunction r (Value b)Source

Truncate a value to a shorter bit width.

zext :: (IsInteger a, IsInteger b, IsPrimitive a, IsPrimitive b, IsSized a sa, IsSized b sb, sa :<: sb) => Value a -> CodeGenFunction r (Value b)Source

Zero extend a value to a wider width.

sext :: (IsInteger a, IsInteger b, IsPrimitive a, IsPrimitive b, IsSized a sa, IsSized b sb, sa :<: sb) => Value a -> CodeGenFunction r (Value b)Source

Sign extend a value to wider width.

fptrunc :: (IsFloating a, IsFloating b, IsPrimitive a, IsPrimitive b, IsSized a sa, IsSized b sb, sa :>: sb) => Value a -> CodeGenFunction r (Value b)Source

Truncate a floating point value.

fpext :: (IsFloating a, IsFloating b, IsPrimitive a, IsPrimitive b, IsSized a sa, IsSized b sb, sa :<: sb) => Value a -> CodeGenFunction r (Value b)Source

Extend a floating point value.

fptoui :: (IsFloating a, IsInteger b, NumberOfElements n a, NumberOfElements n b) => Value a -> CodeGenFunction r (Value b)Source

Convert a floating point value to an unsigned integer.

fptosi :: (IsFloating a, IsInteger b, NumberOfElements n a, NumberOfElements n b) => Value a -> CodeGenFunction r (Value b)Source

Convert a floating point value to a signed integer.

uitofp :: (IsInteger a, IsFloating b, NumberOfElements n a, NumberOfElements n b) => Value a -> CodeGenFunction r (Value b)Source

Convert an unsigned integer to a floating point value.

sitofp :: (IsInteger a, IsFloating b, NumberOfElements n a, NumberOfElements n b) => Value a -> CodeGenFunction r (Value b)Source

Convert a signed integer to a floating point value.

ptrtoint :: (IsInteger b, IsPrimitive b) => Value (Ptr a) -> CodeGenFunction r (Value b)Source

Convert a pointer to an integer.

inttoptr :: (IsInteger a, IsType b) => Value a -> CodeGenFunction r (Value (Ptr b))Source

Convert an integer to a pointer.

bitcast :: (IsFirstClass a, IsFirstClass b, IsSized a sa, IsSized b sb, sa :==: sb) => Value a -> CodeGenFunction r (Value b)Source

Convert between to values of the same size by just copying the bit pattern.

bitcastUnify :: (IsFirstClass a, IsFirstClass b, IsSized a s, IsSized b s) => Value a -> CodeGenFunction r (Value b)Source

Same as bitcast but instead of the '(:==:)' type class it uses type unification. This way, properties like reflexivity, symmetry and transitivity are obvious to the Haskell compiler.

Comparison

data IntPredicate Source

Constructors

IntEQ

equal

IntNE

not equal

IntUGT

unsigned greater than

IntUGE

unsigned greater or equal

IntULT

unsigned less than

IntULE

unsigned less or equal

IntSGT

signed greater than

IntSGE

signed greater or equal

IntSLT

signed less than

IntSLE

signed less or equal

data FPPredicate Source

Constructors

FPFalse

Always false (always folded)

FPOEQ

True if ordered and equal

FPOGT

True if ordered and greater than

FPOGE

True if ordered and greater than or equal

FPOLT

True if ordered and less than

FPOLE

True if ordered and less than or equal

FPONE

True if ordered and operands are unequal

FPORD

True if ordered (no nans)

FPUNO

True if unordered: isnan(X) | isnan(Y)

FPUEQ

True if unordered or equal

FPUGT

True if unordered or greater than

FPUGE

True if unordered, greater than, or equal

FPULT

True if unordered or less than

FPULE

True if unordered, less than, or equal

FPUNE

True if unordered or not equal

FPT

Always true (always folded)

icmp :: (IsIntegerOrPointer c, CmpOp a b c d, CmpRet c d) => IntPredicate -> a -> b -> CodeGenFunction r (Value d)Source

Compare integers.

fcmp :: (IsFloating c, CmpOp a b c d, CmpRet c d) => FPPredicate -> a -> b -> CodeGenFunction r (Value d)Source

Compare floating point values.

select :: (IsFirstClass a, CmpRet a b) => Value b -> Value a -> Value a -> CodeGenFunction r (Value a)Source

Select between two values depending on a boolean.

Other

phi :: forall a r. IsFirstClass a => [(Value a, BasicBlock)] -> CodeGenFunction r (Value a)Source

Join several variables (virtual registers) from different basic blocks into one. All of the variables in the list are joined. See also addPhiInputs.

addPhiInputsSource

Arguments

:: forall a r . IsFirstClass a 
=> Value a

Must be a variable from a call to phi.

-> [(Value a, BasicBlock)]

Variables to add.

-> CodeGenFunction r () 

Add additional inputs to an existing phi node. The reason for this instruction is that sometimes the structure of the code makes it impossible to have all variables in scope at the point where you need the phi node.

call :: CallArgs f g => Function f -> gSource

Call a function with the given arguments. The call instruction is variadic, i.e., the number of arguments it takes depends on the type of f.

Classes and types

class Ret a r Source

Acceptable arguments to the ret instruction.

Instances

Ret () () 
(IsFirstClass a, IsConst a) => Ret a a 
Ret (Value a) a 

class CallArgs f g | f -> g, g -> fSource

Acceptable arguments to call.

Instances

CallArgs (IO a) (CodeGenFunction r (Value a)) 
CallArgs b b' => CallArgs (a -> b) (Value a -> b') 

class ABinOp a b c | a b -> cSource

Acceptable arguments to arithmetic binary instructions.

Instances

IsConst a => ABinOp a (Value a) (Value a) 
IsConst a => ABinOp (Value a) a (Value a) 
ABinOp (ConstValue a) (ConstValue a) (ConstValue a) 
ABinOp (ConstValue a) (Value a) (Value a) 
ABinOp (Value a) (ConstValue a) (Value a) 
ABinOp (Value a) (Value a) (Value a) 

class CmpOp a b c d | a b -> cSource

Acceptable operands to comparison instructions.

Instances

IsConst a => CmpOp a (Value a) a d 
IsConst a => CmpOp (Value a) a a d 
CmpOp (Value a) (Value a) a d 

class FunctionArgs f g r | f -> g r, g r -> fSource

Instances

FunctionArgs (IO Bool) (FA Bool) (FA Bool) 
FunctionArgs (IO Double) (FA Double) (FA Double) 
FunctionArgs (IO Float) (FA Float) (FA Float) 
FunctionArgs (IO Int8) (FA Int8) (FA Int8) 
FunctionArgs (IO Int16) (FA Int16) (FA Int16) 
FunctionArgs (IO Int32) (FA Int32) (FA Int32) 
FunctionArgs (IO Int64) (FA Int64) (FA Int64) 
FunctionArgs (IO (StablePtr a)) (FA (StablePtr a)) (FA (StablePtr a)) 
FunctionArgs (IO Word8) (FA Word8) (FA Word8) 
FunctionArgs (IO Word16) (FA Word16) (FA Word16) 
FunctionArgs (IO Word32) (FA Word32) (FA Word32) 
FunctionArgs (IO Word64) (FA Word64) (FA Word64) 
IsType a => FunctionArgs (IO (Ptr a)) (FA (Ptr a)) (FA (Ptr a)) 
FunctionArgs (IO ()) (FA ()) (FA ()) 
(Pos n, IsPrimitive a) => FunctionArgs (IO (Vector n a)) (FA (Vector n a)) (FA (Vector n a)) 
FunctionArgs (IO FP128) (FA FP128) (FA FP128) 
Pos n => FunctionArgs (IO (WordN n)) (FA (WordN n)) (FA (WordN n)) 
Pos n => FunctionArgs (IO (IntN n)) (FA (IntN n)) (FA (IntN n)) 
FunctionArgs b b' r => FunctionArgs (a -> b) (Value a -> b') r 

class FunctionArgs (IO a) (CodeGenFunction a ()) (CodeGenFunction a ()) => FunctionRet a Source

This class is just to simplify contexts.

Instances

class AllocArg a Source

Acceptable argument to array memory allocation.

class GetElementPtr optr ixs nptr | optr ixs -> nptrSource

Acceptable arguments to getElementPointer.

Instances

GetElementPtr a () a 
(GetElementPtr o i n, GetField fs a o, Nat a) => GetElementPtr (PackedStruct fs) (a, i) n 
(GetElementPtr o i n, GetField fs a o, Nat a) => GetElementPtr (Struct fs) (a, i) n 
(GetElementPtr o i n, IsIndexArg a) => GetElementPtr (Vector k o) (a, i) n 
(GetElementPtr o i n, IsIndexArg a) => GetElementPtr (Array k o) (a, i) n 

class GetValue agg ix el | agg ix -> elSource

Acceptable arguments to extractvalue and insertvalue.

Instances

(GetField as i a, Nat i) => GetValue (Struct as) i a 
(IsFirstClass a, Nat n) => GetValue (Array n a) Word64 a 
(IsFirstClass a, Nat n) => GetValue (Array n a) Word32 a 

Types classification

Type classifier

class IsType a whereSource

The IsType class classifies all types that have an LLVM representation.

Methods

typeDesc :: a -> TypeDescSource

Instances

Special type classifiers

class NatI n => Nat n

Naturals (Positives and zero)

Instances

NatI n => Nat n 

class (IsType a, Pos s) => IsSized a s | a -> sSource

Types with a fixed size.

Instances

IsSized Bool D1 
IsSized Double D64 
IsSized Float D32 
IsSized Int8 D8 
IsSized Int16 D16 
IsSized Int32 D32 
IsSized Int64 D64 
IsSized Word8 D8 
IsSized Word16 D16 
IsSized Word32 D32 
IsSized Word64 D64 
IsSized FP128 D128 
IsSized (StablePtr a) PtrSize 
IsType a => IsSized (Ptr a) PtrSize 
StructFields as => IsSized (PackedStruct as) UnknownSize 
StructFields as => IsSized (Struct as) UnknownSize 
Pos n => IsSized (WordN n) n 
Pos n => IsSized (IntN n) n 
(Nat n, IsPrimitive a, IsSized a s, Mul n s ns, Pos ns) => IsSized (Vector n a) ns 
(Nat n, IsSized a s, Mul n s ns, Pos ns) => IsSized (Array n a) ns 

class IsType a => IsFunction a Source

Function type.

Instances

Others

Structs

type :& a as = (a, as)Source

(&) :: a -> as -> a :& asSource

Type tests

data TypeDesc Source

Type descriptor, used to convey type information through the LLVM API.

typeRefSource

Arguments

:: IsType a 
=> a 
-> TypeRef

The argument is never evaluated

data VarArgs a Source

The VarArgs type is a placeholder for the real IO type that can be obtained with castVarArgs.

class CastVarArgs a b Source

Define what vararg types are permissible.

Instances

CastVarArgs (VarArgs a) (IO a) 
(IsFirstClass a, CastVarArgs (VarArgs b) c) => CastVarArgs (VarArgs b) (a -> c) 
CastVarArgs b c => CastVarArgs (a -> b) (a -> c) 

Extra types

newtype Pos n => IntN n Source

Variable sized signed integer. The n parameter should belong to PosI.

Constructors

IntN Integer 

Instances

Typeable1 IntN 
Pos n => NumberOfElements D1 (IntN n) 
PosI n => Show (IntN n) 
Pos n => IsFirstClass (IntN n) 
Pos n => IsPrimitive (IntN n) 
Pos n => IsIntegerOrPointer (IntN n) 
Pos n => IsInteger (IntN n) 
Pos n => IsArithmetic (IntN n) 
Pos n => IsType (IntN n) 
Pos n => IsSized (IntN n) n 
Pos n => FunctionArgs (IO (IntN n)) (FA (IntN n)) (FA (IntN n)) 

newtype Pos n => WordN n Source

Variable sized unsigned integer. The n parameter should belong to PosI.

Constructors

WordN Integer 

Instances

Typeable1 WordN 
Pos n => NumberOfElements D1 (WordN n) 
PosI n => Show (WordN n) 
Pos n => IsFirstClass (WordN n) 
Pos n => IsPrimitive (WordN n) 
Pos n => IsIntegerOrPointer (WordN n) 
Pos n => IsInteger (WordN n) 
Pos n => IsArithmetic (WordN n) 
Pos n => IsType (WordN n) 
Pos n => IsSized (WordN n) n 
Pos n => FunctionArgs (IO (WordN n)) (FA (WordN n)) (FA (WordN n)) 

newtype Nat n => Array n a Source

Fixed sized arrays, the array size is encoded in the n parameter.

Constructors

Array [a] 

Instances

Typeable2 Array 
(Show a, NatI n) => Show (Array n a) 
(Nat n, IsType a, IsSized a s) => IsFirstClass (Array n a) 
(Nat n, IsSized a s) => IsType (Array n a) 
(IsConst a, IsSized a s, Nat n) => IsConst (Array n a) 
(Nat n, IsSized a s, Mul n s ns, Pos ns) => IsSized (Array n a) ns 
(IsFirstClass a, Nat n) => GetValue (Array n a) Word64 a 
(IsFirstClass a, Nat n) => GetValue (Array n a) Word32 a 
(GetElementPtr o i n, IsIndexArg a) => GetElementPtr (Array k o) (a, i) n 

newtype Vector n a Source

Fixed sized vector, the array size is encoded in the n parameter.

Constructors

Vector [a] 

Instances

Typeable2 Vector 
(Nat n, IsPrimitive a) => NumberOfElements n (Vector n a) 
(Pos n, IsPrimitive a) => FunctionArgs (IO (Vector n a)) (FA (Vector n a)) (FA (Vector n a)) 
(Enum a, Pos n) => Enum (Vector n a) 
Eq a => Eq (Vector n a) 
(Floating a, Pos n) => Floating (Vector n a) 
(Fractional a, Pos n) => Fractional (Vector n a) 
(Integral a, Pos n) => Integral (Vector n a) 
(Num a, Pos n) => Num (Vector n a) 
Ord a => Ord (Vector n a) 
(Real a, Pos n) => Real (Vector n a) 
(RealFloat a, Pos n) => RealFloat (Vector n a) 
(RealFrac a, Pos n) => RealFrac (Vector n a) 
Show a => Show (Vector n a) 
(Storable a, Nat n, IsPrimitive a) => Storable (Vector n a) 
(Nat n, IsPrimitive a) => IsFirstClass (Vector n a) 
(Nat n, IsPrimitive a, IsFloating a) => IsFloating (Vector n a) 
(Nat n, IsPrimitive a, IsInteger a) => IsIntegerOrPointer (Vector n a) 
(Nat n, IsPrimitive a, IsInteger a) => IsInteger (Vector n a) 
(Nat n, IsPrimitive a, IsArithmetic a) => IsArithmetic (Vector n a) 
(Nat n, IsPrimitive a) => IsType (Vector n a) 
(IsPrimitive a, IsConst a, Nat n) => IsConst (Vector n a) 
(Nat n, IsPrimitive a, CallIntrinsic a) => CallIntrinsic (Vector n a) 
(Nat n, IsPrimitive a, IsSized a s, Mul n s ns, Pos ns) => IsSized (Vector n a) ns 
CmpRet (Vector n a) (Vector n Bool) 
Nat n => Cmp (Vector n Word32) (Vector n Bool) 
Nat n => Cmp (Vector n Float) (Vector n Bool) 
(GetElementPtr o i n, IsIndexArg a) => GetElementPtr (Vector k o) (a, i) n 

data Ptr a

A value of type Ptr a represents a pointer to an object, or an array of objects, which may be marshalled to or from Haskell values of type a.

The type a will often be an instance of class Foreign.Storable.Storable which provides the marshalling operations. However this is not essential, and you can provide your own operations to access the pointer. For example you might write small foreign functions to get or set the fields of a C struct.

Instances

Typeable1 Ptr 
Eq (Ptr a) 
Ord (Ptr a) 
Show (Ptr a) 
Storable (Ptr a) 
IsType a => IsFirstClass (Ptr a) 
IsType a => IsIntegerOrPointer (Ptr a) 
IsType a => IsType (Ptr a) 
IsType a => IsConst (Ptr a) 
Generic (Ptr a) 
IsType a => IsSized (Ptr a) PtrSize 
CmpRet (Ptr a) Bool 
IsType a => FunctionArgs (IO (Ptr a)) (FA (Ptr a)) (FA (Ptr a)) 

data Label Source

Label type, produced by a basic block.

newtype Struct a Source

Struct types; a list (nested tuple) of component types.

Constructors

Struct a 

Instances

Typeable1 Struct 
Show a => Show (Struct a) 
StructFields as => IsFirstClass (Struct as) 
StructFields a => IsType (Struct a) 
IsConstFields a => IsConst (Struct a) 
StructFields as => IsSized (Struct as) UnknownSize 
(GetField as i a, Nat i) => GetValue (Struct as) i a 
(GetElementPtr o i n, GetField fs a o, Nat a) => GetElementPtr (Struct fs) (a, i) n 

newtype PackedStruct a Source

Constructors

PackedStruct a 

Instances

Typeable1 PackedStruct 
Show a => Show (PackedStruct a) 
StructFields a => IsType (PackedStruct a) 
IsConstFields a => IsConst (PackedStruct a) 
StructFields as => IsSized (PackedStruct as) UnknownSize 
(GetElementPtr o i n, GetField fs a o, Nat a) => GetElementPtr (PackedStruct fs) (a, i) n 

Values and constants

data Value a Source

Instances

Typeable1 Value 
IsConst a => CmpOp a (Value a) a d 
IsConst a => ABinOp a (Value a) (Value a) 
Show (Value a) 
IsIndexArg (Value Int32) 
IsIndexArg (Value Int64) 
IsIndexArg (Value Word32) 
IsIndexArg (Value Word64) 
AllocArg (Value Word32) 
IsFirstClass a => Phi (Value a) 
Ret (Value a) a 
IsConst a => CmpOp (Value a) a a d 
IsConst a => ABinOp (Value a) a (Value a) 
CmpOp (Value a) (Value a) a d 
ABinOp (ConstValue a) (Value a) (Value a) 
ABinOp (Value a) (ConstValue a) (Value a) 
ABinOp (Value a) (Value a) (Value a) 
CallArgs (IO a) (CodeGenFunction r (Value a)) 
CallArgs b b' => CallArgs (a -> b) (Value a -> b') 
FunctionArgs b b' r => FunctionArgs (a -> b) (Value a -> b') r 

valueOf :: IsConst a => a -> Value aSource

zero :: forall a. IsType a => ConstValue aSource

allOnes :: forall a. IsInteger a => ConstValue aSource

undef :: forall a. IsType a => ConstValue aSource

constVector :: forall a n. Nat n => [ConstValue a] -> ConstValue (Vector n a)Source

Make a constant vector. Replicates or truncates the list to get length n.

constArray :: forall a n s. (IsSized a s, Nat n) => [ConstValue a] -> ConstValue (Array n a)Source

Make a constant array. Replicates or truncates the list to get length n.

constStruct :: IsConstStruct c a => c -> ConstValue (Struct a)Source

Make a constant struct.

constPackedStruct :: IsConstStruct c a => c -> ConstValue (PackedStruct a)Source

Make a constant packed struct.

toVector :: MkVector va n a => va -> Vector n aSource

fromVector :: MkVector va n a => Vector n a -> vaSource

vector :: forall a n. Nat n => [a] -> Vector n aSource

Make a constant vector. Replicates or truncates the list to get length n. This behaviour is consistent with that of LLVM.Core.CodeGen.constVector.

Code generation

data CodeGenFunction r a Source

Instances

Typeable2 CodeGenFunction 
LiftTuple r b b' => LiftTuple r (CodeGenFunction r a, b) (a, b') 
Monad (CodeGenFunction r) 
Functor (CodeGenFunction r) 
MonadIO (CodeGenFunction r) 
MonadState (CGFState r) (CodeGenFunction r) 
CallArgs (IO a) (CodeGenFunction r (Value a)) 
UncurryN (CodeGenFunction r a) (() -> CodeGenFunction r a) 
ArithFunction b b' => ArithFunction (CodeGenFunction r a -> b) (a -> b') 
Ret a r => ArithFunction (CodeGenFunction r a) (CodeGenFunction r ()) 

Functions

type Function a = Value (Ptr a)Source

A function is simply a pointer to the function.

newFunction :: forall a. IsFunction a => Linkage -> CodeGenModule (Function a)Source

Create a new function. Use newNamedFunction to create a function with external linkage, since it needs a known name.

newNamedFunctionSource

Arguments

:: forall a . IsFunction a 
=> Linkage 
-> String

Function name

-> CodeGenModule (Function a) 

Create a new named function.

defineFunctionSource

Arguments

:: forall f g r . FunctionArgs f g (CodeGenFunction r ()) 
=> Function f

Function to define (created by newFunction).

-> g

Function body.

-> CodeGenModule () 

Define a function body. The basic block returned by the function is the function entry point.

createFunctionSource

Arguments

:: (IsFunction f, FunctionArgs f g (CodeGenFunction r ())) 
=> Linkage 
-> g

Function body.

-> CodeGenModule (Function f) 

Create a new function with the given body.

createNamedFunctionSource

Arguments

:: (IsFunction f, FunctionArgs f g (CodeGenFunction r ())) 
=> Linkage 
-> String 
-> g

Function body.

-> CodeGenModule (Function f) 

Create a new function with the given body.

Global variable creation

type Global a = Value (Ptr a)Source

newGlobal :: forall a. IsType a => Bool -> Linkage -> TGlobal aSource

Create a new global variable.

newNamedGlobalSource

Arguments

:: forall a . IsType a 
=> Bool

Constant?

-> Linkage

Visibility

-> String

Name

-> TGlobal a 

Create a new named global variable.

defineGlobal :: Global a -> ConstValue a -> CodeGenModule ()Source

Give a global variable a (constant) value.

createGlobal :: IsType a => Bool -> Linkage -> ConstValue a -> TGlobal aSource

Create and define a global variable.

createNamedGlobal :: IsType a => Bool -> Linkage -> String -> ConstValue a -> TGlobal aSource

Create and define a named global variable.

externFunction :: forall a r. IsFunction a => String -> CodeGenFunction r (Function a)Source

Create a reference to an external function while code generating for a function. If LLVM cannot resolve its name, then you may try staticFunction.

staticFunction :: IsFunction f => FunPtr f -> CodeGenFunction r (Function f)Source

Make an external C function with a fixed address callable from LLVM code. This callback function can also be a Haskell function, that was imported like

 foreign import ccall "&nextElement"
    nextElementFunPtr :: FunPtr (StablePtr (IORef [Word32]) -> IO Word32)

See examples/List.hs.

When you only use externFunction, then LLVM cannot resolve the name. (However, I do not know why.) Thus staticFunction manages a list of static functions. This list is automatically installed by ExecutionEngine.simpleFunction and can be manually obtained by getGlobalMappings and installed by ExecutionEngine.addGlobalMappings. "Installing" means calling LLVM's addGlobalMapping according to http://old.nabble.com/jit-with-external-functions-td7769793.html.

getGlobalMappings :: CodeGenModule GlobalMappingsSource

Get a list created by calls to staticFunction that must be passed to the execution engine via LLVM.ExecutionEngine.addGlobalMappings.

Globals

data Linkage Source

An enumeration for the kinds of linkage for global values.

Constructors

ExternalLinkage

Externally visible function

AvailableExternallyLinkage 
LinkOnceAnyLinkage

Keep one copy of function when linking (inline)

LinkOnceODRLinkage

Same, but only replaced by something equivalent.

WeakAnyLinkage

Keep one copy of named function when linking (weak)

WeakODRLinkage

Same, but only replaced by something equivalent.

AppendingLinkage

Special purpose, only applies to global arrays

InternalLinkage

Rename collisions when linking (static functions)

PrivateLinkage

Like Internal, but omit from symbol table

DLLImportLinkage

Function to be imported from DLL

DLLExportLinkage

Function to be accessible from DLL

ExternalWeakLinkage

ExternalWeak linkage description

GhostLinkage

Stand-in functions for streaming fns from BC files

CommonLinkage

Tentative definitions

LinkerPrivateLinkage

Like Private, but linker removes.

Basic blocks

data BasicBlock Source

A basic block is a sequence of non-branching instructions, terminated by a control flow instruction.

Misc

addAttributes :: Value a -> Int -> [Attribute] -> CodeGenFunction r ()Source

Add attributes to a value. Beware, what attributes are allowed depends on what kind of value it is.

castVarArgs :: CastVarArgs a b => Function a -> Function bSource

Convert a varargs function to a regular function.

Debugging

dumpValue :: Value a -> IO ()Source

Print a value.

dumpType :: Value a -> IO ()Source

Print a type.

getValueName :: Value a -> IO StringSource

Get the name of a Value.