Safe Haskell | None |
---|---|
Language | Haskell98 |
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
- initializeNativeTarget :: IO ()
- data Module
- newModule :: IO Module
- newNamedModule :: String -> IO Module
- defineModule :: Module -> CodeGenModule a -> IO a
- destroyModule :: Module -> IO ()
- createModule :: CodeGenModule a -> IO a
- getModule :: CodeGenModule Module
- setTarget :: String -> CodeGenModule ()
- hostTriple :: String
- setDataLayout :: String -> CodeGenModule ()
- data PassManager
- createPassManager :: IO PassManager
- createFunctionPassManager :: Module -> IO PassManager
- writeBitcodeToFile :: String -> Module -> IO ()
- readBitcodeFromFile :: String -> IO Module
- getModuleValues :: Module -> IO [(String, ModuleValue)]
- getFunctions :: Module -> IO [(String, Value)]
- getGlobalVariables :: Module -> IO [(String, Value)]
- data ModuleValue
- castModuleValue :: IsType a => ModuleValue -> Maybe (Value a)
- data Value a
- data ConstValue a
- valueOf :: IsConst a => a -> Value a
- constOf :: IsConst a => a -> ConstValue a
- value :: ConstValue a -> Value a
- zero :: IsType a => ConstValue a
- allOnes :: IsInteger a => ConstValue a
- undef :: IsType a => ConstValue a
- class IsConst a
- class IsConstFields a
- createString :: String -> TGlobal (Array n Word8)
- createStringNul :: String -> TGlobal (Array n Word8)
- withString :: String -> (forall n. Natural n => Global (Array n Word8) -> CodeGenModule a) -> CodeGenModule a
- withStringNul :: String -> (forall n. Natural n => Global (Array n Word8) -> CodeGenModule a) -> CodeGenModule a
- constVector :: (Positive n, ToUnary n ~ u, Length (FixedList u) ~ u) => FixedList u (ConstValue a) -> ConstValue (Vector n a)
- constArray :: (IsSized a, Natural n) => [ConstValue a] -> ConstValue (Array n a)
- constCyclicVector :: Positive n => T [] (ConstValue a) -> ConstValue (Vector n a)
- constCyclicArray :: (IsSized a, Natural n) => T [] (ConstValue a) -> ConstValue (Vector n a)
- constStruct :: IsConstStruct c => c -> ConstValue (Struct (ConstStructOf c))
- constPackedStruct :: IsConstStruct c => c -> ConstValue (PackedStruct (ConstStructOf c))
- toVector :: MkVector n => Tuple n a -> Vector n a
- fromVector :: MkVector n => Vector n a -> Tuple n a
- vector :: Positive n => FixedList (ToUnary n) a -> Vector n a
- cyclicVector :: Positive n => T [] a -> Vector n a
- consVector :: (ConsVector f, ResultSize f ~ n, NumberOfArguments f ~ u, u ~ ToUnary n, FromUnary u ~ n, Natural n) => f
- data CodeGenFunction r a
- data CodeGenModule a
- type Function a = Value (FunPtr a)
- newFunction :: IsFunction a => Linkage -> CodeGenModule (Function a)
- newNamedFunction :: IsFunction a => Linkage -> String -> CodeGenModule (Function a)
- defineFunction :: FunctionArgs f => Function f -> FunctionCodeGen f -> CodeGenModule ()
- createFunction :: FunctionArgs f => Linkage -> FunctionCodeGen f -> CodeGenModule (Function f)
- createNamedFunction :: FunctionArgs f => Linkage -> String -> FunctionCodeGen f -> CodeGenModule (Function f)
- setFuncCallConv :: Function a -> CallingConvention -> CodeGenModule ()
- functionParameter :: Natural i => Function f -> Proxy i -> Value (FunctionParameter f i)
- type TFunction a = CodeGenModule (Function a)
- liftCodeGenModule :: CodeGenModule a -> CodeGenFunction r a
- getParams :: Value -> IO [(String, Value)]
- type Global a = Value (Ptr a)
- newGlobal :: IsType a => Bool -> Linkage -> TGlobal a
- newNamedGlobal :: IsType a => Bool -> Linkage -> String -> TGlobal a
- defineGlobal :: Global a -> ConstValue a -> CodeGenModule ()
- createGlobal :: IsType a => Bool -> Linkage -> ConstValue a -> TGlobal a
- createNamedGlobal :: IsType a => Bool -> Linkage -> String -> ConstValue a -> TGlobal a
- externFunction :: IsFunction a => String -> CodeGenFunction r (Function a)
- staticFunction :: IsFunction f => FunPtr f -> CodeGenFunction r (Function f)
- staticNamedFunction :: IsFunction f => String -> FunPtr f -> CodeGenFunction r (Function f)
- externGlobal :: IsType a => Bool -> String -> CodeGenFunction r (Global a)
- staticGlobal :: IsType a => Bool -> Ptr a -> CodeGenFunction r (Global a)
- data GlobalMappings
- getGlobalMappings :: CodeGenModule GlobalMappings
- type TGlobal a = CodeGenModule (Global a)
- data Linkage
- = ExternalLinkage
- | AvailableExternallyLinkage
- | LinkOnceAnyLinkage
- | LinkOnceODRLinkage
- | LinkOnceODRAutoHideLinkage
- | WeakAnyLinkage
- | WeakODRLinkage
- | AppendingLinkage
- | InternalLinkage
- | PrivateLinkage
- | DLLImportLinkage
- | DLLExportLinkage
- | ExternalWeakLinkage
- | GhostLinkage
- | CommonLinkage
- | LinkerPrivateLinkage
- | LinkerPrivateWeakLinkage
- data BasicBlock
- newBasicBlock :: CodeGenFunction r BasicBlock
- newNamedBasicBlock :: String -> CodeGenFunction r BasicBlock
- defineBasicBlock :: BasicBlock -> CodeGenFunction r ()
- createBasicBlock :: CodeGenFunction r BasicBlock
- getCurrentBasicBlock :: CodeGenFunction r BasicBlock
- getBasicBlocks :: Value -> IO [(String, BasicBlock)]
- fromLabel :: Value Label -> BasicBlock
- toLabel :: BasicBlock -> Value Label
- getInstructions :: BasicBlock -> IO [(String, Value)]
- getOperands :: Value -> IO [(String, Value)]
- hasUsers :: Value -> IO Bool
- getUsers :: [Use] -> IO [(String, Value)]
- getUses :: Value -> IO [Use]
- getUser :: Use -> IO Value
- isChildOf :: BasicBlock -> Value -> IO Bool
- getDep :: Use -> IO (String, String)
- addAttributes :: Value a -> AttributeIndex -> [Attribute] -> CodeGenFunction r ()
- data Attribute
- newtype AttributeIndex = AttributeIndex Word32
- attributeReturnIndex :: AttributeIndex
- attributeFunctionIndex :: AttributeIndex
- castVarArgs :: CastVarArgs a b => Function a -> Function b
- dumpValue :: Value a -> IO ()
- dumpType :: Value a -> IO ()
- getValueName :: Value a -> IO String
- annotateValueList :: [Value] -> IO [(String, Value)]
Initialize
initializeNativeTarget :: IO () #
Initialize jitter to the native target. The operation is idempotent.
Modules
newNamedModule :: String -> IO Module #
defineModule :: Module -> CodeGenModule a -> IO a #
destroyModule :: Module -> IO () #
createModule :: CodeGenModule a -> IO a #
setTarget :: String -> CodeGenModule () #
hostTriple :: String #
setDataLayout :: String -> CodeGenModule () #
data PassManager #
Instances
Show PassManager | |
Defined in LLVM.Core.Util showsPrec :: Int -> PassManager -> ShowS # show :: PassManager -> String # showList :: [PassManager] -> ShowS # |
writeBitcodeToFile :: String -> Module -> IO () #
readBitcodeFromFile :: String -> IO Module #
getModuleValues :: Module -> IO [(String, ModuleValue)] #
getFunctions :: Module -> IO [(String, Value)] #
getGlobalVariables :: Module -> IO [(String, Value)] #
data ModuleValue #
Instances
Show ModuleValue | |
Defined in LLVM.Core.CodeGen showsPrec :: Int -> ModuleValue -> ShowS # show :: ModuleValue -> String # showList :: [ModuleValue] -> ShowS # |
castModuleValue :: IsType a => ModuleValue -> Maybe (Value a) #
Instructions
Types classification
Extra types
Values and constants
Instances
data ConstValue a #
Instances
constOf :: IsConst a => a -> ConstValue a #
value :: ConstValue a -> Value a #
zero :: IsType a => ConstValue a #
allOnes :: IsInteger a => ConstValue a #
undef :: IsType a => ConstValue a #
Instances
class IsConstFields a #
constFieldsOf
Instances
IsConstFields () | |
Defined in LLVM.Core.CodeGen constFieldsOf :: () -> [ValueRef] | |
(IsConst a, IsConstFields as) => IsConstFields (a, as) | |
Defined in LLVM.Core.CodeGen constFieldsOf :: (a, as) -> [ValueRef] |
createString :: String -> TGlobal (Array n Word8) #
createStringNul :: String -> TGlobal (Array n Word8) #
withString :: String -> (forall n. Natural n => Global (Array n Word8) -> CodeGenModule a) -> CodeGenModule a #
withStringNul :: String -> (forall n. Natural n => Global (Array n Word8) -> CodeGenModule a) -> CodeGenModule a #
constVector :: (Positive n, ToUnary n ~ u, Length (FixedList u) ~ u) => FixedList u (ConstValue a) -> ConstValue (Vector n a) #
constArray :: (IsSized a, Natural n) => [ConstValue a] -> ConstValue (Array n a) #
constCyclicVector :: Positive n => T [] (ConstValue a) -> ConstValue (Vector n a) #
constCyclicArray :: (IsSized a, Natural n) => T [] (ConstValue a) -> ConstValue (Vector n a) #
constStruct :: IsConstStruct c => c -> ConstValue (Struct (ConstStructOf c)) #
constPackedStruct :: IsConstStruct c => c -> ConstValue (PackedStruct (ConstStructOf c)) #
fromVector :: MkVector n => Vector n a -> Tuple n a #
cyclicVector :: Positive n => T [] a -> Vector n a #
consVector :: (ConsVector f, ResultSize f ~ n, NumberOfArguments f ~ u, u ~ ToUnary n, FromUnary u ~ n, Natural n) => f #
Code generation
data CodeGenFunction r a #
Instances
data CodeGenModule a #
Instances
Monad CodeGenModule | |
Defined in LLVM.Core.CodeGenMonad (>>=) :: CodeGenModule a -> (a -> CodeGenModule b) -> CodeGenModule b # (>>) :: CodeGenModule a -> CodeGenModule b -> CodeGenModule b # return :: a -> CodeGenModule a # fail :: String -> CodeGenModule a # | |
Functor CodeGenModule | |
Defined in LLVM.Core.CodeGenMonad fmap :: (a -> b) -> CodeGenModule a -> CodeGenModule b # (<$) :: a -> CodeGenModule b -> CodeGenModule a # | |
Applicative CodeGenModule | |
Defined in LLVM.Core.CodeGenMonad pure :: a -> CodeGenModule a # (<*>) :: CodeGenModule (a -> b) -> CodeGenModule a -> CodeGenModule b # liftA2 :: (a -> b -> c) -> CodeGenModule a -> CodeGenModule b -> CodeGenModule c # (*>) :: CodeGenModule a -> CodeGenModule b -> CodeGenModule b # (<*) :: CodeGenModule a -> CodeGenModule b -> CodeGenModule a # | |
MonadIO CodeGenModule | |
Defined in LLVM.Core.CodeGenMonad liftIO :: IO a -> CodeGenModule a # |
Functions
newFunction :: IsFunction a => Linkage -> CodeGenModule (Function a) #
newNamedFunction :: IsFunction a => Linkage -> String -> CodeGenModule (Function a) #
defineFunction :: FunctionArgs f => Function f -> FunctionCodeGen f -> CodeGenModule () #
createFunction :: FunctionArgs f => Linkage -> FunctionCodeGen f -> CodeGenModule (Function f) #
createNamedFunction :: FunctionArgs f => Linkage -> String -> FunctionCodeGen f -> CodeGenModule (Function f) #
setFuncCallConv :: Function a -> CallingConvention -> CodeGenModule () #
type TFunction a = CodeGenModule (Function a) #
liftCodeGenModule :: CodeGenModule a -> CodeGenFunction r a #
Global variable creation
defineGlobal :: Global a -> ConstValue a -> CodeGenModule () #
createGlobal :: IsType a => Bool -> Linkage -> ConstValue a -> TGlobal a #
createNamedGlobal :: IsType a => Bool -> Linkage -> String -> ConstValue a -> TGlobal a #
externFunction :: IsFunction a => String -> CodeGenFunction r (Function a) #
staticFunction :: IsFunction f => FunPtr f -> CodeGenFunction r (Function f) #
staticNamedFunction :: IsFunction f => String -> FunPtr f -> CodeGenFunction r (Function f) #
externGlobal :: IsType a => Bool -> String -> CodeGenFunction r (Global a) #
staticGlobal :: IsType a => Bool -> Ptr a -> CodeGenFunction r (Global a) #
data GlobalMappings #
Instances
Show GlobalMappings | |
Defined in LLVM.Core.CodeGenMonad showsPrec :: Int -> GlobalMappings -> ShowS # show :: GlobalMappings -> String # showList :: [GlobalMappings] -> ShowS # | |
Semigroup GlobalMappings | |
Defined in LLVM.Core.CodeGenMonad (<>) :: GlobalMappings -> GlobalMappings -> GlobalMappings # sconcat :: NonEmpty GlobalMappings -> GlobalMappings # stimes :: Integral b => b -> GlobalMappings -> GlobalMappings # | |
Monoid GlobalMappings | |
Defined in LLVM.Core.CodeGenMonad mappend :: GlobalMappings -> GlobalMappings -> GlobalMappings # mconcat :: [GlobalMappings] -> GlobalMappings # |
type TGlobal a = CodeGenModule (Global a) #
Globals
An enumeration for the kinds of linkage for global values.
ExternalLinkage | Externally visible function |
AvailableExternallyLinkage | |
LinkOnceAnyLinkage | Keep one copy of function when linking (inline) |
LinkOnceODRLinkage | Same, but only replaced by something equivalent. |
LinkOnceODRAutoHideLinkage | Like LinkOnceODR, but possibly hidden. |
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. |
LinkerPrivateWeakLinkage | Like LinkerPrivate, but is weak. |
Basic blocks
data BasicBlock #
Instances
Show BasicBlock | |
Defined in LLVM.Core.CodeGen showsPrec :: Int -> BasicBlock -> ShowS # show :: BasicBlock -> String # showList :: [BasicBlock] -> ShowS # |
defineBasicBlock :: BasicBlock -> CodeGenFunction r () #
getBasicBlocks :: Value -> IO [(String, BasicBlock)] #
fromLabel :: Value Label -> BasicBlock #
toLabel :: BasicBlock -> Value Label #
getInstructions :: BasicBlock -> IO [(String, Value)] #
getOperands :: Value -> IO [(String, Value)] #
Misc
addAttributes :: Value a -> AttributeIndex -> [Attribute] -> CodeGenFunction r () #
newtype AttributeIndex #
castVarArgs :: CastVarArgs a b => Function a -> Function b Source #
Convert a varargs function to a regular function.
Debugging
annotateValueList :: [Value] -> IO [(String, Value)] #