-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCodeGen.hs
More file actions
48 lines (36 loc) · 1.63 KB
/
Copy pathCodeGen.hs
File metadata and controls
48 lines (36 loc) · 1.63 KB
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
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module CodeGen () where
import LLVM.General.AST
import LLVM.General.AST.Global
import qualified LLVM.General.AST as AST
import Data.Map as Map
import Data.Word
import Control.Monad.State
import Control.Applicative
double :: Type
double = FloatingPointType 64 IEEE
type Names = Map.Map String Int
type SymbolTable = [(String, Operand)]
data CodegenState = CodegenState { currentBlock :: Name
, blocks :: Map.Map Name BlockState
, symtab :: SymbolTable
, blockCount :: Int
, count :: Word
, names :: Names
} deriving Show
data BlockState = BlockState { idx :: Int
, stack :: [Named Instruction]
, term :: Maybe (Named Terminator)
} deriving Show
newtype Codegen a = Codegen { runCodegen :: State CodegenState a }
deriving (Functor, Applicative, Monad, MonadState CodegenState )
newtype LLVM a = LLVM { unLLVM :: State AST.Module a }
deriving (Functor, Applicative, Monad, MonadState AST.Module )
runLLVM :: AST.Module -> LLVM a -> AST.Module
runLLVM = flip (execState . unLLVM)
emptyModule :: String -> AST.Module
emptyModule label = defaultModule { moduleName = label }
addDefn :: Definition -> LLVM ()
addDefn d = do
defs <- gets moduleDefinitions
modify $ \s -> s { moduleDefinitions = defs ++ [d] }