-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCodegen.hs
322 lines (258 loc) · 9.45 KB
/
Codegen.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverloadedStrings #-}
module Codegen where
import Control.Applicative
import Control.Monad.State
import Data.ByteString.Short
import Data.Function
import Data.List
import qualified Data.Map as Map
import Data.Monoid ((<>))
import Data.String
import Data.Word
import LLVM.AST
import qualified LLVM.AST as AST
import LLVM.AST.AddrSpace
import qualified LLVM.AST.Attribute as A
import qualified LLVM.AST.CallingConvention as CC
import qualified LLVM.AST.Constant as C
import qualified LLVM.AST.FloatingPointPredicate as FP
import LLVM.AST.Global
import qualified LLVM.AST.Linkage as L
import LLVM.AST.Type
import LLVM.AST.Typed (typeOf)
-------------------------------------------------------------------------------
-- Module Level
-------------------------------------------------------------------------------
newtype LLVM a = LLVM (State AST.Module a)
deriving (Functor, Applicative, Monad, MonadState AST.Module)
runLLVM :: AST.Module -> LLVM a -> AST.Module
runLLVM mod (LLVM m) = execState m mod
emptyModule :: ShortByteString -> AST.Module
emptyModule label = defaultModule {moduleName = label}
addDefn :: Definition -> LLVM ()
addDefn d = do
defs <- gets moduleDefinitions
modify $ \s -> s {moduleDefinitions = defs ++ [d]}
define :: Type -> ShortByteString -> [(Type, Name)] -> (Type -> Codegen a) -> LLVM ()
define retty label argtys body =
addDefn
$ GlobalDefinition
$ functionDefaults
{ name = Name label,
parameters = ([Parameter ty nm [] | (ty, nm) <- argtys], False),
returnType = retty,
basicBlocks = bls
}
where
bls = createBlocks $ execCodegen $ do
enter <- addBlock entryBlockName
_ <- setBlock enter
body ptrThisType
ptrThisType = PointerType
{ pointerReferent = FunctionType
{ resultType = retty,
argumentTypes = map fst argtys,
isVarArg = False
},
pointerAddrSpace = AddrSpace 0
}
external :: Type -> ShortByteString -> [(Type, Name)] -> LLVM ()
external retty label argtys =
addDefn
$ GlobalDefinition
$ functionDefaults
{ name = Name label,
linkage = L.External,
parameters = ([Parameter ty nm [] | (ty, nm) <- argtys], False),
returnType = retty,
basicBlocks = []
}
fnPtr :: Name -> LLVM Type
fnPtr nm = findType <$> gets moduleDefinitions
where
findType defs =
case fnDefByName of
[] -> error $ "Undefined function: " ++ show nm
[fn] -> PointerType (typeOf fn) (AddrSpace 0)
_ -> error $ "Ambiguous function name: " ++ show nm
where
globalDefs = [g | GlobalDefinition g <- defs]
fnDefByName = [f | f@(Function {name = nm'}) <- globalDefs, nm' == nm]
---------------------------------------------------------------------------------
-- Types
-------------------------------------------------------------------------------
-- IEEE 754 double
double :: Type
double = FloatingPointType DoubleFP
void :: Type
void = AST.VoidType
-------------------------------------------------------------------------------
-- Names
-------------------------------------------------------------------------------
type Names = Map.Map ShortByteString Int
uniqueName :: ShortByteString -> Names -> (ShortByteString, Names)
uniqueName nm ns =
case Map.lookup nm ns of
Nothing -> (nm, Map.insert nm 1 ns)
Just ix -> (nm <> fromString (show ix), Map.insert nm (ix + 1) ns)
-------------------------------------------------------------------------------
-- Codegen State
-------------------------------------------------------------------------------
type SymbolTable = [(ShortByteString, Operand)]
data CodegenState
= CodegenState
{ currentBlock :: Name, -- Name of the active block to append to
blocks :: Map.Map Name BlockState, -- Blocks for function
symtab :: SymbolTable, -- Function scope symbol table
blockCount :: Int, -- Count of basic blocks
count :: Word, -- Count of unnamed instructions
names :: Names -- Name Supply
}
deriving (Show)
data BlockState
= BlockState
{ idx :: Int, -- Block index
stack :: [Named Instruction], -- Stack of instructions
term :: Maybe (Named Terminator) -- Block terminator
}
deriving (Show)
-------------------------------------------------------------------------------
-- Codegen Operations
-------------------------------------------------------------------------------
newtype Codegen a = Codegen {runCodegen :: State CodegenState a}
deriving (Functor, Applicative, Monad, MonadState CodegenState)
sortBlocks :: [(Name, BlockState)] -> [(Name, BlockState)]
sortBlocks = sortBy (compare `on` (idx . snd))
createBlocks :: CodegenState -> [BasicBlock]
createBlocks m = map makeBlock $ sortBlocks $ Map.toList (blocks m)
makeBlock :: (Name, BlockState) -> BasicBlock
makeBlock (l, (BlockState _ s t)) = BasicBlock l (reverse s) (maketerm t)
where
maketerm (Just x) = x
maketerm Nothing = error $ "Block has no terminator: " ++ (show l)
entryBlockName :: ShortByteString
entryBlockName = "entry"
emptyBlock :: Int -> BlockState
emptyBlock i = BlockState i [] Nothing
emptyCodegen :: CodegenState
emptyCodegen = CodegenState (Name entryBlockName) Map.empty [] 1 0 Map.empty
execCodegen :: Codegen a -> CodegenState
execCodegen m = execState (runCodegen m) emptyCodegen
fresh :: Codegen Word
fresh = do
i <- gets count
modify $ \s -> s {count = 1 + i}
return $ i + 1
instr :: Type -> Instruction -> Codegen (Operand)
instr ty ins = do
n <- fresh
let ref = (UnName n)
blk <- current
let i = stack blk
modifyBlock (blk {stack = (ref := ins) : i})
return $ local ty ref
unnminstr :: Instruction -> Codegen ()
unnminstr ins = do
blk <- current
let i = stack blk
modifyBlock (blk {stack = (Do ins) : i})
terminator :: Named Terminator -> Codegen (Named Terminator)
terminator trm = do
blk <- current
modifyBlock (blk {term = Just trm})
return trm
-------------------------------------------------------------------------------
-- Block Stack
-------------------------------------------------------------------------------
entry :: Codegen Name
entry = gets currentBlock
addBlock :: ShortByteString -> Codegen Name
addBlock bname = do
bls <- gets blocks
ix <- gets blockCount
nms <- gets names
let new = emptyBlock ix
(qname, supply) = uniqueName bname nms
modify $ \s ->
s
{ blocks = Map.insert (Name qname) new bls,
blockCount = ix + 1,
names = supply
}
return (Name qname)
setBlock :: Name -> Codegen Name
setBlock bname = do
modify $ \s -> s {currentBlock = bname}
return bname
getBlock :: Codegen Name
getBlock = gets currentBlock
modifyBlock :: BlockState -> Codegen ()
modifyBlock new = do
active <- gets currentBlock
modify $ \s -> s {blocks = Map.insert active new (blocks s)}
current :: Codegen BlockState
current = do
c <- gets currentBlock
blks <- gets blocks
case Map.lookup c blks of
Just x -> return x
Nothing -> error $ "No such block: " ++ show c
-------------------------------------------------------------------------------
-- Symbol Table
-------------------------------------------------------------------------------
assign :: ShortByteString -> Operand -> Codegen ()
assign var x = do
lcls <- gets symtab
modify $ \s -> s {symtab = [(var, x)] ++ lcls}
getvar :: ShortByteString -> Codegen Operand
getvar var = do
syms <- gets symtab
case lookup var syms of
Just x -> return x
Nothing -> error $ "Local variable not in scope: " ++ show var
-------------------------------------------------------------------------------
-- References
local :: Type -> Name -> Operand
local = LocalReference
global :: Type -> Name -> C.Constant
global = C.GlobalReference
externf :: Type -> Name -> Operand
externf ty nm = ConstantOperand (C.GlobalReference ty nm)
-- Arithmetic and Constants
fadd :: Operand -> Operand -> Codegen Operand
fadd a b = instr float $ FAdd noFastMathFlags a b []
fsub :: Operand -> Operand -> Codegen Operand
fsub a b = instr float $ FSub noFastMathFlags a b []
fmul :: Operand -> Operand -> Codegen Operand
fmul a b = instr float $ FMul noFastMathFlags a b []
fdiv :: Operand -> Operand -> Codegen Operand
fdiv a b = instr float $ FDiv noFastMathFlags a b []
fcmp :: FP.FloatingPointPredicate -> Operand -> Operand -> Codegen Operand
fcmp cond a b = instr float $ FCmp cond a b []
cons :: C.Constant -> Operand
cons = ConstantOperand
uitofp :: Type -> Operand -> Codegen Operand
uitofp ty a = instr float $ UIToFP a ty []
toArgs :: [Operand] -> [(Operand, [A.ParameterAttribute])]
toArgs = map (\x -> (x, []))
-- Effects
call :: Operand -> [Operand] -> Codegen Operand
call fn args = instr float $ Call Nothing CC.C [] (Right fn) (toArgs args) [] []
alloca :: Type -> Codegen Operand
alloca ty = instr float $ Alloca ty Nothing 0 []
store :: Operand -> Operand -> Codegen ()
store ptr val = unnminstr $ Store False ptr val Nothing 0 []
load :: Operand -> Codegen Operand
load ptr = instr float $ Load False ptr Nothing 0 []
-- Control Flow
br :: Name -> Codegen (Named Terminator)
br val = terminator $ Do $ Br val []
cbr :: Operand -> Name -> Name -> Codegen (Named Terminator)
cbr cond tr fl = terminator $ Do $ CondBr cond tr fl []
phi :: Type -> [(Operand, Name)] -> Codegen Operand
phi ty incoming = instr float $ Phi ty incoming []
ret :: Operand -> Codegen (Named Terminator)
ret val = terminator $ Do $ Ret (Just val) []
retvoid :: Codegen (Named Terminator)
retvoid = terminator $ Do $ Ret Nothing []