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 globals decls funcs)
42 = ppLlvmComments comments
44 $+$ ppLlvmGlobals globals
46 $+$ ppLlvmFunctionDecls decls
48 $+$ ppLlvmFunctions funcs
50 -- | Print out a multi-line comment, can be inside a function or on its own
51 ppLlvmComments :: [LMString] -> Doc
52 ppLlvmComments comments = vcat $ map ppLlvmComment comments
54 -- | Print out a comment, can be inside a function or on its own
55 ppLlvmComment :: LMString -> Doc
56 ppLlvmComment com = semi <+> ftext com
59 -- | Print out a list of global mutable variable definitions
60 ppLlvmGlobals :: [LMGlobal] -> Doc
61 ppLlvmGlobals ls = vcat $ map ppLlvmGlobal ls
63 -- | Print out a global mutable variable definition
64 ppLlvmGlobal :: LMGlobal -> Doc
65 ppLlvmGlobal (var@(LMGlobalVar _ _ link x a c), dat) =
67 Just x' -> text ", section" <+> doubleQuotes (ftext x')
71 Just a' -> text ", align" <+> int a'
75 Just stat -> texts stat
76 Nothing -> texts (pLower $ getVarType var)
78 const' = if c then text "constant" else text "global"
80 in ppAssignment var $ texts link <+> const' <+> rhs <> sect <> align
82 ppLlvmGlobal oth = error $ "Non Global var ppr as global! " ++ show oth
85 -- | Print out a list of LLVM type aliases.
86 ppLlvmTypes :: [LlvmType] -> Doc
87 ppLlvmTypes tys = vcat $ map ppLlvmType tys
89 -- | Print out an LLVM type alias.
90 ppLlvmType :: LlvmType -> Doc
92 ppLlvmType al@(LMAlias _ t)
93 = texts al <+> equals <+> text "type" <+> texts t
95 ppLlvmType (LMFunction t)
96 = ppLlvmFunctionDecl t
101 -- | Print out a list of function definitions.
102 ppLlvmFunctions :: LlvmFunctions -> Doc
103 ppLlvmFunctions funcs = vcat $ map ppLlvmFunction funcs
105 -- | Print out a function definition.
106 ppLlvmFunction :: LlvmFunction -> Doc
107 ppLlvmFunction (LlvmFunction dec args attrs sec body) =
108 let attrDoc = ppSpaceJoin attrs
110 Just s' -> text "section" <+> (doubleQuotes $ ftext s')
112 in text "define" <+> ppLlvmFunctionHeader dec args
113 <+> attrDoc <+> secDoc
115 $+$ ppLlvmBlocks body
118 -- | Print out a function defenition header.
119 ppLlvmFunctionHeader :: LlvmFunctionDecl -> [LMString] -> Doc
120 ppLlvmFunctionHeader (LlvmFunctionDecl n l c r varg p a) args
121 = let varg' = if varg == VarArgs then text ", ..." else empty
123 Just a' -> space <> text "align" <+> texts a'
125 args' = map (\((ty,p),n) -> texts ty <+> ppSpaceJoin p <+> text "%"
128 in texts l <+> texts c <+> texts r <+> text "@" <> ftext n <> lparen <>
129 (hcat $ intersperse comma args') <> varg' <> rparen <> align
132 -- | Print out a list of function declaration.
133 ppLlvmFunctionDecls :: LlvmFunctionDecls -> Doc
134 ppLlvmFunctionDecls decs = vcat $ map ppLlvmFunctionDecl decs
136 -- | Print out a function declaration.
137 -- Declarations define the function type but don't define the actual body of
139 ppLlvmFunctionDecl :: LlvmFunctionDecl -> Doc
140 ppLlvmFunctionDecl dec = text "declare" <+> texts dec
143 -- | Print out a list of LLVM blocks.
144 ppLlvmBlocks :: LlvmBlocks -> Doc
145 ppLlvmBlocks blocks = vcat $ map ppLlvmBlock blocks
147 -- | Print out an LLVM block.
148 -- It must be part of a function definition.
149 ppLlvmBlock :: LlvmBlock -> Doc
150 ppLlvmBlock (LlvmBlock blockId stmts)
151 = ppLlvmStatement (MkLabel blockId)
152 $+$ nest 4 (vcat $ map ppLlvmStatement stmts)
155 -- | Print out an LLVM statement.
156 ppLlvmStatement :: LlvmStatement -> Doc
159 Assignment dst expr -> ppAssignment dst (ppLlvmExpression expr)
160 Branch target -> ppBranch target
161 BranchIf cond ifT ifF -> ppBranchIf cond ifT ifF
162 Comment comments -> ppLlvmComments comments
163 MkLabel label -> (llvmSDoc $ pprUnique label) <> colon
164 Store value ptr -> ppStore value ptr
165 Switch scrut def tgs -> ppSwitch scrut def tgs
166 Return result -> ppReturn result
167 Expr expr -> ppLlvmExpression expr
168 Unreachable -> text "unreachable"
171 -- | Print out an LLVM expression.
172 ppLlvmExpression :: LlvmExpression -> Doc
173 ppLlvmExpression expr
175 Alloca tp amount -> ppAlloca tp amount
176 LlvmOp op left right -> ppMachOp op left right
177 Call tp fp args attrs -> ppCall tp fp args attrs
178 Cast op from to -> ppCast op from to
179 Compare op left right -> ppCmpOp op left right
180 GetElemPtr ptr indexes -> ppGetElementPtr ptr indexes
181 Load ptr -> ppLoad ptr
182 Malloc tp amount -> ppMalloc tp amount
183 Phi tp precessors -> ppPhi tp precessors
186 --------------------------------------------------------------------------------
187 -- * Individual print functions
188 --------------------------------------------------------------------------------
190 -- | Should always be a function pointer. So a global var of function type
191 -- (since globals are always pointers) or a local var of pointer function type.
192 ppCall :: LlvmCallType -> LlvmVar -> [LlvmVar] -> [LlvmFuncAttr] -> Doc
193 ppCall ct fptr vals attrs = case fptr of
195 -- if local var function pointer, unwrap
196 LMLocalVar _ (LMPointer (LMFunction d)) -> ppCall' d
198 -- should be function type otherwise
199 LMGlobalVar _ (LMFunction d) _ _ _ _ -> ppCall' d
201 -- not pointer or function, so error
202 _other -> error $ "ppCall called with non LMFunction type!\nMust be "
203 ++ " called with either global var of function type or "
204 ++ "local var of pointer function type."
207 ppCall' (LlvmFunctionDecl _ _ cc ret argTy params _) =
208 let tc = if ct == TailCall then text "tail " else empty
209 ppValues = ppCommaJoin vals
210 ppParams = map (\(ty,p) -> texts ty <+> ppSpaceJoin p) params
211 ppArgTy = (hcat $ intersperse comma ppParams) <>
213 VarArgs -> text ", ..."
215 fnty = space <> lparen <> ppArgTy <> rparen <> text "*"
216 attrDoc = ppSpaceJoin attrs
217 in tc <> text "call" <+> texts cc <+> texts ret
218 <> fnty <+> (text $ getName fptr) <> lparen <+> ppValues
219 <+> rparen <+> attrDoc
222 ppMachOp :: LlvmMachOp -> LlvmVar -> LlvmVar -> Doc
223 ppMachOp op left right =
224 (texts op) <+> (texts (getVarType left)) <+> (text $ getName left)
225 <> comma <+> (text $ getName right)
228 ppCmpOp :: LlvmCmpOp -> LlvmVar -> LlvmVar -> Doc
229 ppCmpOp op left right =
231 | isInt (getVarType left) && isInt (getVarType right) = text "icmp"
232 | isFloat (getVarType left) && isFloat (getVarType right) = text "fcmp"
233 | otherwise = text "icmp" -- Just continue as its much easier to debug
235 | otherwise = error ("can't compare different types, left = "
236 ++ (show $ getVarType left) ++ ", right = "
237 ++ (show $ getVarType right))
239 in cmpOp <+> texts op <+> texts (getVarType left)
240 <+> (text $ getName left) <> comma <+> (text $ getName right)
243 ppAssignment :: LlvmVar -> Doc -> Doc
244 ppAssignment var expr = (text $ getName var) <+> equals <+> expr
247 ppLoad :: LlvmVar -> Doc
248 ppLoad var = text "load" <+> texts var
251 ppStore :: LlvmVar -> LlvmVar -> Doc
252 ppStore val dst = text "store" <+> texts val <> comma <+> texts dst
255 ppCast :: LlvmCastOp -> LlvmVar -> LlvmType -> Doc
256 ppCast op from to = texts op <+> texts from <+> text "to" <+> texts to
259 ppMalloc :: LlvmType -> Int -> Doc
261 let amount' = LMLitVar $ LMIntLit (toInteger amount) i32
262 in text "malloc" <+> texts tp <> comma <+> texts amount'
265 ppAlloca :: LlvmType -> Int -> Doc
267 let amount' = LMLitVar $ LMIntLit (toInteger amount) i32
268 in text "alloca" <+> texts tp <> comma <+> texts amount'
271 ppGetElementPtr :: LlvmVar -> [Int] -> Doc
272 ppGetElementPtr ptr idx =
273 let indexes = hcat $ map ((comma <+> texts i32 <+>) . texts) idx
274 in text "getelementptr" <+> texts ptr <> indexes
277 ppReturn :: Maybe LlvmVar -> Doc
278 ppReturn (Just var) = text "ret" <+> texts var
279 ppReturn Nothing = text "ret" <+> texts LMVoid
282 ppBranch :: LlvmVar -> Doc
283 ppBranch var = text "br" <+> texts var
286 ppBranchIf :: LlvmVar -> LlvmVar -> LlvmVar -> Doc
287 ppBranchIf cond trueT falseT
288 = text "br" <+> texts cond <> comma <+> texts trueT <> comma <+> texts falseT
291 ppPhi :: LlvmType -> [(LlvmVar,LlvmVar)] -> Doc
293 let ppPreds (val, label) = brackets $ (text $ getName val) <> comma
294 <+> (text $ getName label)
295 in text "phi" <+> texts tp <+> hcat (intersperse comma $ map ppPreds preds)
298 ppSwitch :: LlvmVar -> LlvmVar -> [(LlvmVar,LlvmVar)] -> Doc
299 ppSwitch scrut dflt targets =
300 let ppTarget (val, lab) = texts val <> comma <+> texts lab
301 ppTargets xs = brackets $ vcat (map ppTarget xs)
302 in text "switch" <+> texts scrut <> comma <+> texts dflt
303 <+> ppTargets targets
306 --------------------------------------------------------------------------------
308 --------------------------------------------------------------------------------
309 ppCommaJoin :: (Show a) => [a] -> Doc
310 ppCommaJoin strs = hcat $ intersperse comma (map texts strs)
312 ppSpaceJoin :: (Show a) => [a] -> Doc
313 ppSpaceJoin strs = hcat $ intersperse space (map texts strs)
315 -- | Convert SDoc to Doc
316 llvmSDoc :: Out.SDoc -> Doc
318 = Out.withPprStyleDoc (Out.mkCodeStyle Out.CStyle) d
321 texts :: (Show a) => a -> Doc
322 texts = (text . show)