-
Notifications
You must be signed in to change notification settings - Fork 0
/
L8.hs
52 lines (46 loc) · 1.22 KB
/
L8.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import LLVM.General.AST
import qualified LLVM.General.AST as AST
import LLVM.General.Context
import LLVM.General.Module
import Control.Monad.Error
import LLVM.General.AST.Global
import LLVM.General.AST.Instruction
import LLVM.General.AST.Operand
import LLVM.General.AST.Constant
initModule :: AST.Module
initModule = defaultModule {
moduleName = "top"
, moduleDefinitions = [func]
}
retTy :: Type
retTy = IntegerType 32
terminator :: Named Terminator
terminator = Do Ret {
returnOperand = Just (ConstantOperand Int {
integerBits = 32,
integerValue = 0
}),
metadata' = []
}
blk :: BasicBlock
blk = BasicBlock (Name "entrypoint") [] terminator
func = GlobalDefinition $ functionDefaults {
name = Name "main"
, returnType = retTy
, basicBlocks = [blk]
}
-- withContext :: (Context -> IO a) -> IO a
{-
withModuleFromAST
:: Context
-> AST.Module
-> (LLVM.General.Module.Module -> IO a)
-> Control.Monad.Trans.Error.ErrorT String IO a
-}
main :: IO ()
main = do
s <- withContext $ \context ->
runErrorT $ withModuleFromAST context initModule $ \m -> moduleLLVMAssembly m
case s of
Left err -> print err
Right ll -> putStrLn ll