1 --------------------------------------------------------------------------------
2 -- | Pretty print LLVM IR Code.
7 -- * Top level LLVM objects.
20 -- * Utility functions
25 #include "HsVersions.h"
30 import Data.List ( intersperse )
32 import qualified Outputable as Out
35 --------------------------------------------------------------------------------
36 -- * Top Level Print functions
37 --------------------------------------------------------------------------------
39 -- | Print out a whole LLVM module.
40 ppLlvmModule :: LlvmModule -> Doc
41 ppLlvmModule (LlvmModule comments aliases globals decls funcs)
42 = ppLlvmComments comments
44 $+$ ppLlvmAliases aliases
46 $+$ ppLlvmGlobals globals
48 $+$ ppLlvmFunctionDecls decls
50 $+$ ppLlvmFunctions funcs
52 -- | Print out a multi-line comment, can be inside a function or on its own
53 ppLlvmComments :: [LMString] -> Doc
54 ppLlvmComments comments = vcat $ map ppLlvmComment comments
56 -- | Print out a comment, can be inside a function or on its own
57 ppLlvmComment :: LMString -> Doc
58 ppLlvmComment com = semi <+> ftext com
61 -- | Print out a list of global mutable variable definitions
62 ppLlvmGlobals :: [LMGlobal] -> Doc
63 ppLlvmGlobals ls = vcat $ map ppLlvmGlobal ls
65 -- | Print out a global mutable variable definition
66 ppLlvmGlobal :: LMGlobal -> Doc
67 ppLlvmGlobal (var@(LMGlobalVar _ _ link x a c), dat) =
69 Just x' -> text ", section" <+> doubleQuotes (ftext x')
73 Just a' -> text ", align" <+> int a'
77 Just stat -> texts stat
78 Nothing -> texts (pLower $ getVarType var)
80 const' = if c then text "constant" else text "global"
82 in ppAssignment var $ texts link <+> const' <+> rhs <> sect <> align
84 ppLlvmGlobal oth = error $ "Non Global var ppr as global! " ++ show oth
87 -- | Print out a list of LLVM type aliases.
88 ppLlvmAliases :: [LlvmAlias] -> Doc
89 ppLlvmAliases tys = vcat $ map ppLlvmAlias tys
91 -- | Print out an LLVM type alias.
92 ppLlvmAlias :: LlvmAlias -> Doc
93 ppLlvmAlias (name, ty) = text "%" <> ftext name <+> equals <+> text "type" <+> texts ty
96 -- | Print out a list of function definitions.
97 ppLlvmFunctions :: LlvmFunctions -> Doc
98 ppLlvmFunctions funcs = vcat $ map ppLlvmFunction funcs
100 -- | Print out a function definition.
101 ppLlvmFunction :: LlvmFunction -> Doc
102 ppLlvmFunction (LlvmFunction dec args attrs sec body) =
103 let attrDoc = ppSpaceJoin attrs
105 Just s' -> text "section" <+> (doubleQuotes $ ftext s')
107 in text "define" <+> ppLlvmFunctionHeader dec args
108 <+> attrDoc <+> secDoc
110 $+$ ppLlvmBlocks body
113 -- | Print out a function defenition header.
114 ppLlvmFunctionHeader :: LlvmFunctionDecl -> [LMString] -> Doc
115 ppLlvmFunctionHeader (LlvmFunctionDecl n l c r varg p a) args
116 = let varg' = if varg == VarArgs then text ", ..." else empty
118 Just a' -> space <> text "align" <+> texts a'
120 args' = map (\((ty,p),n) -> texts ty <+> ppSpaceJoin p <+> text "%"
123 in texts l <+> texts c <+> texts r <+> text "@" <> ftext n <> lparen <>
124 (hcat $ intersperse comma args') <> varg' <> rparen <> align
127 -- | Print out a list of function declaration.
128 ppLlvmFunctionDecls :: LlvmFunctionDecls -> Doc
129 ppLlvmFunctionDecls decs = vcat $ map ppLlvmFunctionDecl decs
131 -- | Print out a function declaration.
132 -- Declarations define the function type but don't define the actual body of
134 ppLlvmFunctionDecl :: LlvmFunctionDecl -> Doc
135 ppLlvmFunctionDecl dec = text "declare" <+> texts dec
138 -- | Print out a list of LLVM blocks.
139 ppLlvmBlocks :: LlvmBlocks -> Doc
140 ppLlvmBlocks blocks = vcat $ map ppLlvmBlock blocks
142 -- | Print out an LLVM block.
143 -- It must be part of a function definition.
144 ppLlvmBlock :: LlvmBlock -> Doc
145 ppLlvmBlock (LlvmBlock blockId stmts)
146 = ppLlvmStatement (MkLabel blockId)
147 $+$ nest 4 (vcat $ map ppLlvmStatement stmts)
150 -- | Print out an LLVM statement.
151 ppLlvmStatement :: LlvmStatement -> Doc
154 Assignment dst expr -> ppAssignment dst (ppLlvmExpression expr)
155 Branch target -> ppBranch target
156 BranchIf cond ifT ifF -> ppBranchIf cond ifT ifF
157 Comment comments -> ppLlvmComments comments
158 MkLabel label -> (llvmSDoc $ pprUnique label) <> colon
159 Store value ptr -> ppStore value ptr
160 Switch scrut def tgs -> ppSwitch scrut def tgs
161 Return result -> ppReturn result
162 Expr expr -> ppLlvmExpression expr
163 Unreachable -> text "unreachable"
166 -- | Print out an LLVM expression.
167 ppLlvmExpression :: LlvmExpression -> Doc
168 ppLlvmExpression expr
170 Alloca tp amount -> ppAlloca tp amount
171 LlvmOp op left right -> ppMachOp op left right
172 Call tp fp args attrs -> ppCall tp fp args attrs
173 Cast op from to -> ppCast op from to
174 Compare op left right -> ppCmpOp op left right
175 GetElemPtr inb ptr indexes -> ppGetElementPtr inb ptr indexes
176 Load ptr -> ppLoad ptr
177 Malloc tp amount -> ppMalloc tp amount
178 Phi tp precessors -> ppPhi tp precessors
179 Asm asm c ty v se sk -> ppAsm asm c ty v se sk
182 --------------------------------------------------------------------------------
183 -- * Individual print functions
184 --------------------------------------------------------------------------------
186 -- | Should always be a function pointer. So a global var of function type
187 -- (since globals are always pointers) or a local var of pointer function type.
188 ppCall :: LlvmCallType -> LlvmVar -> [LlvmVar] -> [LlvmFuncAttr] -> Doc
189 ppCall ct fptr vals attrs = case fptr of
191 -- if local var function pointer, unwrap
192 LMLocalVar _ (LMPointer (LMFunction d)) -> ppCall' d
194 -- should be function type otherwise
195 LMGlobalVar _ (LMFunction d) _ _ _ _ -> ppCall' d
197 -- not pointer or function, so error
198 _other -> error $ "ppCall called with non LMFunction type!\nMust be "
199 ++ " called with either global var of function type or "
200 ++ "local var of pointer function type."
203 ppCall' (LlvmFunctionDecl _ _ cc ret argTy params _) =
204 let tc = if ct == TailCall then text "tail " else empty
205 ppValues = ppCommaJoin vals
206 ppParams = map (\(ty,p) -> texts ty <+> ppSpaceJoin p) params
207 ppArgTy = (hcat $ intersperse comma ppParams) <>
209 VarArgs -> text ", ..."
211 fnty = space <> lparen <> ppArgTy <> rparen <> text "*"
212 attrDoc = ppSpaceJoin attrs
213 in tc <> text "call" <+> texts cc <+> texts ret
214 <> fnty <+> (text $ getName fptr) <> lparen <+> ppValues
215 <+> rparen <+> attrDoc
218 ppMachOp :: LlvmMachOp -> LlvmVar -> LlvmVar -> Doc
219 ppMachOp op left right =
220 (texts op) <+> (texts (getVarType left)) <+> (text $ getName left)
221 <> comma <+> (text $ getName right)
224 ppCmpOp :: LlvmCmpOp -> LlvmVar -> LlvmVar -> Doc
225 ppCmpOp op left right =
227 | isInt (getVarType left) && isInt (getVarType right) = text "icmp"
228 | isFloat (getVarType left) && isFloat (getVarType right) = text "fcmp"
229 | otherwise = text "icmp" -- Just continue as its much easier to debug
231 | otherwise = error ("can't compare different types, left = "
232 ++ (show $ getVarType left) ++ ", right = "
233 ++ (show $ getVarType right))
235 in cmpOp <+> texts op <+> texts (getVarType left)
236 <+> (text $ getName left) <> comma <+> (text $ getName right)
239 ppAssignment :: LlvmVar -> Doc -> Doc
240 ppAssignment var expr = (text $ getName var) <+> equals <+> expr
243 ppLoad :: LlvmVar -> Doc
244 ppLoad var = text "load" <+> texts var
247 ppStore :: LlvmVar -> LlvmVar -> Doc
248 ppStore val dst = text "store" <+> texts val <> comma <+> texts dst
251 ppCast :: LlvmCastOp -> LlvmVar -> LlvmType -> Doc
252 ppCast op from to = texts op <+> texts from <+> text "to" <+> texts to
255 ppMalloc :: LlvmType -> Int -> Doc
257 let amount' = LMLitVar $ LMIntLit (toInteger amount) i32
258 in text "malloc" <+> texts tp <> comma <+> texts amount'
261 ppAlloca :: LlvmType -> Int -> Doc
263 let amount' = LMLitVar $ LMIntLit (toInteger amount) i32
264 in text "alloca" <+> texts tp <> comma <+> texts amount'
267 ppGetElementPtr :: Bool -> LlvmVar -> [LlvmVar] -> Doc
268 ppGetElementPtr inb ptr idx =
269 let indexes = comma <+> ppCommaJoin idx
270 inbound = if inb then text "inbounds" else empty
271 in text "getelementptr" <+> inbound <+> texts ptr <> indexes
274 ppReturn :: Maybe LlvmVar -> Doc
275 ppReturn (Just var) = text "ret" <+> texts var
276 ppReturn Nothing = text "ret" <+> texts LMVoid
279 ppBranch :: LlvmVar -> Doc
280 ppBranch var = text "br" <+> texts var
283 ppBranchIf :: LlvmVar -> LlvmVar -> LlvmVar -> Doc
284 ppBranchIf cond trueT falseT
285 = text "br" <+> texts cond <> comma <+> texts trueT <> comma <+> texts falseT
288 ppPhi :: LlvmType -> [(LlvmVar,LlvmVar)] -> Doc
290 let ppPreds (val, label) = brackets $ (text $ getName val) <> comma
291 <+> (text $ getName label)
292 in text "phi" <+> texts tp <+> hcat (intersperse comma $ map ppPreds preds)
295 ppSwitch :: LlvmVar -> LlvmVar -> [(LlvmVar,LlvmVar)] -> Doc
296 ppSwitch scrut dflt targets =
297 let ppTarget (val, lab) = texts val <> comma <+> texts lab
298 ppTargets xs = brackets $ vcat (map ppTarget xs)
299 in text "switch" <+> texts scrut <> comma <+> texts dflt
300 <+> ppTargets targets
303 ppAsm :: LMString -> LMString -> LlvmType -> [LlvmVar] -> Bool -> Bool -> Doc
304 ppAsm asm constraints rty vars sideeffect alignstack =
305 let asm' = doubleQuotes $ ftext asm
306 cons = doubleQuotes $ ftext constraints
308 vars' = lparen <+> ppCommaJoin vars <+> rparen
309 side = if sideeffect then text "sideeffect" else empty
310 align = if alignstack then text "alignstack" else empty
311 in text "call" <+> rty' <+> text "asm" <+> side <+> align <+> asm' <> comma
315 --------------------------------------------------------------------------------
317 --------------------------------------------------------------------------------
318 ppCommaJoin :: (Show a) => [a] -> Doc
319 ppCommaJoin strs = hcat $ intersperse comma (map texts strs)
321 ppSpaceJoin :: (Show a) => [a] -> Doc
322 ppSpaceJoin strs = hcat $ intersperse space (map texts strs)
324 -- | Convert SDoc to Doc
325 llvmSDoc :: Out.SDoc -> Doc
327 = Out.withPprStyleDoc (Out.mkCodeStyle Out.CStyle) d
330 texts :: (Show a) => a -> Doc
331 texts = (text . show)