的Module
和Function
类型是围绕指针只是薄包装到相应的C++的对象(即,Module*
和Value*
):
-- LLVM.Core.Util
newtype Module = Module {
fromModule :: FFI.ModuleRef
}
deriving (Show, Typeable)
type Function a = Value (Ptr a)
newtype Value a = Value { unValue :: FFI.ValueRef }
deriving (Show, Typeable)
-- LLVM.FFI.Core
data Module
deriving (Typeable)
type ModuleRef = Ptr Module
data Value
deriving (Typeable)
type ValueRef = Ptr Value
的CodeGenModule
和CodeGenFunction
类型是建立在顶部的EDSL的部件LLVM.FFI.*
模块。他们使用Function
,Module
和LLVM.FFI.*
内部的功能,让你写LLVM IR在Haskell简明使用DO-符号(例如,从Lennart Augustsson's blog拍摄):
mFib :: CodeGenModule (Function (Word32 -> IO Word32))
mFib = do
fib <- newFunction ExternalLinkage
defineFunction fib $ \ arg -> do
-- Create the two basic blocks.
recurse <- newBasicBlock
exit <- newBasicBlock
[...]
ret r
return fib
你能想到的CodeGenModule
作为AST代表解析LLVM汇编文件(.ll
)。给定CodeGenModule
,您可以其写入.bc
文件:
-- newModule :: IO Module
mod <- newModule
-- defineModule :: Module -> CodeGenModule a -> IO a
defineModule mod $ do [...]
-- writeBitcodeToFile :: FilePath -> Module -> IO()
writeBitcodeToFile "mymodule.bc" mod
--- Alternatively, just use this function from LLVM.Util.File:
writeCodeGenModule :: FilePath -> CodeGenModule a -> IO()
我也建议你自己熟悉core classes of LLVM,因为它们也Haskell的API中显示出来。