Update Trac ticket URLs to point to GitLab
[ghc.git] / compiler / llvmGen / Llvm / Types.hs
1 {-# LANGUAGE CPP, GeneralizedNewtypeDeriving #-}
2
3 --------------------------------------------------------------------------------
4 -- | The LLVM Type System.
5 --
6
7 module Llvm.Types where
8
9 #include "HsVersions.h"
10
11 import GhcPrelude
12
13 import Data.Char
14 import Data.Int
15 import Numeric
16
17 import DynFlags
18 import FastString
19 import Outputable
20 import Unique
21
22 -- from NCG
23 import PprBase
24
25 import GHC.Float
26
27 -- -----------------------------------------------------------------------------
28 -- * LLVM Basic Types and Variables
29 --
30
31 -- | A global mutable variable. Maybe defined or external
32 data LMGlobal = LMGlobal {
33 getGlobalVar :: LlvmVar, -- ^ Returns the variable of the 'LMGlobal'
34 getGlobalValue :: Maybe LlvmStatic -- ^ Return the value of the 'LMGlobal'
35 }
36
37 -- | A String in LLVM
38 type LMString = FastString
39
40 -- | A type alias
41 type LlvmAlias = (LMString, LlvmType)
42
43 -- | Llvm Types
44 data LlvmType
45 = LMInt Int -- ^ An integer with a given width in bits.
46 | LMFloat -- ^ 32 bit floating point
47 | LMDouble -- ^ 64 bit floating point
48 | LMFloat80 -- ^ 80 bit (x86 only) floating point
49 | LMFloat128 -- ^ 128 bit floating point
50 | LMPointer LlvmType -- ^ A pointer to a 'LlvmType'
51 | LMArray Int LlvmType -- ^ An array of 'LlvmType'
52 | LMVector Int LlvmType -- ^ A vector of 'LlvmType'
53 | LMLabel -- ^ A 'LlvmVar' can represent a label (address)
54 | LMVoid -- ^ Void type
55 | LMStruct [LlvmType] -- ^ Packed structure type
56 | LMStructU [LlvmType] -- ^ Unpacked structure type
57 | LMAlias LlvmAlias -- ^ A type alias
58 | LMMetadata -- ^ LLVM Metadata
59
60 -- | Function type, used to create pointers to functions
61 | LMFunction LlvmFunctionDecl
62 deriving (Eq)
63
64 instance Outputable LlvmType where
65 ppr (LMInt size ) = char 'i' <> ppr size
66 ppr (LMFloat ) = text "float"
67 ppr (LMDouble ) = text "double"
68 ppr (LMFloat80 ) = text "x86_fp80"
69 ppr (LMFloat128 ) = text "fp128"
70 ppr (LMPointer x ) = ppr x <> char '*'
71 ppr (LMArray nr tp ) = char '[' <> ppr nr <> text " x " <> ppr tp <> char ']'
72 ppr (LMVector nr tp ) = char '<' <> ppr nr <> text " x " <> ppr tp <> char '>'
73 ppr (LMLabel ) = text "label"
74 ppr (LMVoid ) = text "void"
75 ppr (LMStruct tys ) = text "<{" <> ppCommaJoin tys <> text "}>"
76 ppr (LMStructU tys ) = text "{" <> ppCommaJoin tys <> text "}"
77 ppr (LMMetadata ) = text "metadata"
78
79 ppr (LMFunction (LlvmFunctionDecl _ _ _ r varg p _))
80 = ppr r <+> lparen <> ppParams varg p <> rparen
81
82 ppr (LMAlias (s,_)) = char '%' <> ftext s
83
84 ppParams :: LlvmParameterListType -> [LlvmParameter] -> SDoc
85 ppParams varg p
86 = let varg' = case varg of
87 VarArgs | null args -> sLit "..."
88 | otherwise -> sLit ", ..."
89 _otherwise -> sLit ""
90 -- by default we don't print param attributes
91 args = map fst p
92 in ppCommaJoin args <> ptext varg'
93
94 -- | An LLVM section definition. If Nothing then let LLVM decide the section
95 type LMSection = Maybe LMString
96 type LMAlign = Maybe Int
97
98 data LMConst = Global -- ^ Mutable global variable
99 | Constant -- ^ Constant global variable
100 | Alias -- ^ Alias of another variable
101 deriving (Eq)
102
103 -- | LLVM Variables
104 data LlvmVar
105 -- | Variables with a global scope.
106 = LMGlobalVar LMString LlvmType LlvmLinkageType LMSection LMAlign LMConst
107 -- | Variables local to a function or parameters.
108 | LMLocalVar Unique LlvmType
109 -- | Named local variables. Sometimes we need to be able to explicitly name
110 -- variables (e.g for function arguments).
111 | LMNLocalVar LMString LlvmType
112 -- | A constant variable
113 | LMLitVar LlvmLit
114 deriving (Eq)
115
116 instance Outputable LlvmVar where
117 ppr (LMLitVar x) = ppr x
118 ppr (x ) = ppr (getVarType x) <+> ppName x
119
120
121 -- | Llvm Literal Data.
122 --
123 -- These can be used inline in expressions.
124 data LlvmLit
125 -- | Refers to an integer constant (i64 42).
126 = LMIntLit Integer LlvmType
127 -- | Floating point literal
128 | LMFloatLit Double LlvmType
129 -- | Literal NULL, only applicable to pointer types
130 | LMNullLit LlvmType
131 -- | Vector literal
132 | LMVectorLit [LlvmLit]
133 -- | Undefined value, random bit pattern. Useful for optimisations.
134 | LMUndefLit LlvmType
135 deriving (Eq)
136
137 instance Outputable LlvmLit where
138 ppr l@(LMVectorLit {}) = ppLit l
139 ppr l = ppr (getLitType l) <+> ppLit l
140
141
142 -- | Llvm Static Data.
143 --
144 -- These represent the possible global level variables and constants.
145 data LlvmStatic
146 = LMComment LMString -- ^ A comment in a static section
147 | LMStaticLit LlvmLit -- ^ A static variant of a literal value
148 | LMUninitType LlvmType -- ^ For uninitialised data
149 | LMStaticStr LMString LlvmType -- ^ Defines a static 'LMString'
150 | LMStaticArray [LlvmStatic] LlvmType -- ^ A static array
151 | LMStaticStruc [LlvmStatic] LlvmType -- ^ A static structure type
152 | LMStaticPointer LlvmVar -- ^ A pointer to other data
153
154 -- static expressions, could split out but leave
155 -- for moment for ease of use. Not many of them.
156
157 | LMTrunc LlvmStatic LlvmType -- ^ Truncate
158 | LMBitc LlvmStatic LlvmType -- ^ Pointer to Pointer conversion
159 | LMPtoI LlvmStatic LlvmType -- ^ Pointer to Integer conversion
160 | LMAdd LlvmStatic LlvmStatic -- ^ Constant addition operation
161 | LMSub LlvmStatic LlvmStatic -- ^ Constant subtraction operation
162
163 instance Outputable LlvmStatic where
164 ppr (LMComment s) = text "; " <> ftext s
165 ppr (LMStaticLit l ) = ppr l
166 ppr (LMUninitType t) = ppr t <> text " undef"
167 ppr (LMStaticStr s t) = ppr t <> text " c\"" <> ftext s <> text "\\00\""
168 ppr (LMStaticArray d t) = ppr t <> text " [" <> ppCommaJoin d <> char ']'
169 ppr (LMStaticStruc d t) = ppr t <> text "<{" <> ppCommaJoin d <> text "}>"
170 ppr (LMStaticPointer v) = ppr v
171 ppr (LMTrunc v t)
172 = ppr t <> text " trunc (" <> ppr v <> text " to " <> ppr t <> char ')'
173 ppr (LMBitc v t)
174 = ppr t <> text " bitcast (" <> ppr v <> text " to " <> ppr t <> char ')'
175 ppr (LMPtoI v t)
176 = ppr t <> text " ptrtoint (" <> ppr v <> text " to " <> ppr t <> char ')'
177
178 ppr (LMAdd s1 s2)
179 = pprStaticArith s1 s2 (sLit "add") (sLit "fadd") "LMAdd"
180 ppr (LMSub s1 s2)
181 = pprStaticArith s1 s2 (sLit "sub") (sLit "fsub") "LMSub"
182
183
184 pprSpecialStatic :: LlvmStatic -> SDoc
185 pprSpecialStatic (LMBitc v t) =
186 ppr (pLower t) <> text ", bitcast (" <> ppr v <> text " to " <> ppr t
187 <> char ')'
188 pprSpecialStatic stat = ppr stat
189
190
191 pprStaticArith :: LlvmStatic -> LlvmStatic -> PtrString -> PtrString
192 -> String -> SDoc
193 pprStaticArith s1 s2 int_op float_op op_name =
194 let ty1 = getStatType s1
195 op = if isFloat ty1 then float_op else int_op
196 in if ty1 == getStatType s2
197 then ppr ty1 <+> ptext op <+> lparen <> ppr s1 <> comma <> ppr s2 <> rparen
198 else sdocWithDynFlags $ \dflags ->
199 error $ op_name ++ " with different types! s1: "
200 ++ showSDoc dflags (ppr s1) ++ ", s2: " ++ showSDoc dflags (ppr s2)
201
202 -- -----------------------------------------------------------------------------
203 -- ** Operations on LLVM Basic Types and Variables
204 --
205
206 -- | Return the variable name or value of the 'LlvmVar'
207 -- in Llvm IR textual representation (e.g. @\@x@, @%y@ or @42@).
208 ppName :: LlvmVar -> SDoc
209 ppName v@(LMGlobalVar {}) = char '@' <> ppPlainName v
210 ppName v@(LMLocalVar {}) = char '%' <> ppPlainName v
211 ppName v@(LMNLocalVar {}) = char '%' <> ppPlainName v
212 ppName v@(LMLitVar {}) = ppPlainName v
213
214 -- | Return the variable name or value of the 'LlvmVar'
215 -- in a plain textual representation (e.g. @x@, @y@ or @42@).
216 ppPlainName :: LlvmVar -> SDoc
217 ppPlainName (LMGlobalVar x _ _ _ _ _) = ftext x
218 ppPlainName (LMLocalVar x LMLabel ) = text (show x)
219 ppPlainName (LMLocalVar x _ ) = text ('l' : show x)
220 ppPlainName (LMNLocalVar x _ ) = ftext x
221 ppPlainName (LMLitVar x ) = ppLit x
222
223 -- | Print a literal value. No type.
224 ppLit :: LlvmLit -> SDoc
225 ppLit (LMIntLit i (LMInt 32)) = ppr (fromInteger i :: Int32)
226 ppLit (LMIntLit i (LMInt 64)) = ppr (fromInteger i :: Int64)
227 ppLit (LMIntLit i _ ) = ppr ((fromInteger i)::Int)
228 ppLit (LMFloatLit r LMFloat ) = ppFloat $ narrowFp r
229 ppLit (LMFloatLit r LMDouble) = ppDouble r
230 ppLit f@(LMFloatLit _ _) = sdocWithDynFlags (\dflags ->
231 error $ "Can't print this float literal!" ++ showSDoc dflags (ppr f))
232 ppLit (LMVectorLit ls ) = char '<' <+> ppCommaJoin ls <+> char '>'
233 ppLit (LMNullLit _ ) = text "null"
234 -- #11487 was an issue where we passed undef for some arguments
235 -- that were actually live. By chance the registers holding those
236 -- arguments usually happened to have the right values anyways, but
237 -- that was not guaranteed. To find such bugs reliably, we set the
238 -- flag below when validating, which replaces undef literals (at
239 -- common types) with values that are likely to cause a crash or test
240 -- failure.
241 ppLit (LMUndefLit t ) = sdocWithDynFlags f
242 where f dflags
243 | gopt Opt_LlvmFillUndefWithGarbage dflags,
244 Just lit <- garbageLit t = ppLit lit
245 | otherwise = text "undef"
246
247 garbageLit :: LlvmType -> Maybe LlvmLit
248 garbageLit t@(LMInt w) = Just (LMIntLit (0xbbbbbbbbbbbbbbb0 `mod` (2^w)) t)
249 -- Use a value that looks like an untagged pointer, so we are more
250 -- likely to try to enter it
251 garbageLit t
252 | isFloat t = Just (LMFloatLit 12345678.9 t)
253 garbageLit t@(LMPointer _) = Just (LMNullLit t)
254 -- Using null isn't totally ideal, since some functions may check for null.
255 -- But producing another value is inconvenient since it needs a cast,
256 -- and the knowledge for how to format casts is in PpLlvm.
257 garbageLit _ = Nothing
258 -- More cases could be added, but this should do for now.
259
260 -- | Return the 'LlvmType' of the 'LlvmVar'
261 getVarType :: LlvmVar -> LlvmType
262 getVarType (LMGlobalVar _ y _ _ _ _) = y
263 getVarType (LMLocalVar _ y ) = y
264 getVarType (LMNLocalVar _ y ) = y
265 getVarType (LMLitVar l ) = getLitType l
266
267 -- | Return the 'LlvmType' of a 'LlvmLit'
268 getLitType :: LlvmLit -> LlvmType
269 getLitType (LMIntLit _ t) = t
270 getLitType (LMFloatLit _ t) = t
271 getLitType (LMVectorLit []) = panic "getLitType"
272 getLitType (LMVectorLit ls) = LMVector (length ls) (getLitType (head ls))
273 getLitType (LMNullLit t) = t
274 getLitType (LMUndefLit t) = t
275
276 -- | Return the 'LlvmType' of the 'LlvmStatic'
277 getStatType :: LlvmStatic -> LlvmType
278 getStatType (LMStaticLit l ) = getLitType l
279 getStatType (LMUninitType t) = t
280 getStatType (LMStaticStr _ t) = t
281 getStatType (LMStaticArray _ t) = t
282 getStatType (LMStaticStruc _ t) = t
283 getStatType (LMStaticPointer v) = getVarType v
284 getStatType (LMTrunc _ t) = t
285 getStatType (LMBitc _ t) = t
286 getStatType (LMPtoI _ t) = t
287 getStatType (LMAdd t _) = getStatType t
288 getStatType (LMSub t _) = getStatType t
289 getStatType (LMComment _) = error "Can't call getStatType on LMComment!"
290
291 -- | Return the 'LlvmLinkageType' for a 'LlvmVar'
292 getLink :: LlvmVar -> LlvmLinkageType
293 getLink (LMGlobalVar _ _ l _ _ _) = l
294 getLink _ = Internal
295
296 -- | Add a pointer indirection to the supplied type. 'LMLabel' and 'LMVoid'
297 -- cannot be lifted.
298 pLift :: LlvmType -> LlvmType
299 pLift LMLabel = error "Labels are unliftable"
300 pLift LMVoid = error "Voids are unliftable"
301 pLift LMMetadata = error "Metadatas are unliftable"
302 pLift x = LMPointer x
303
304 -- | Lift a variable to 'LMPointer' type.
305 pVarLift :: LlvmVar -> LlvmVar
306 pVarLift (LMGlobalVar s t l x a c) = LMGlobalVar s (pLift t) l x a c
307 pVarLift (LMLocalVar s t ) = LMLocalVar s (pLift t)
308 pVarLift (LMNLocalVar s t ) = LMNLocalVar s (pLift t)
309 pVarLift (LMLitVar _ ) = error $ "Can't lower a literal type!"
310
311 -- | Remove the pointer indirection of the supplied type. Only 'LMPointer'
312 -- constructors can be lowered.
313 pLower :: LlvmType -> LlvmType
314 pLower (LMPointer x) = x
315 pLower x = pprPanic "llvmGen(pLower)"
316 $ ppr x <+> text " is a unlowerable type, need a pointer"
317
318 -- | Lower a variable of 'LMPointer' type.
319 pVarLower :: LlvmVar -> LlvmVar
320 pVarLower (LMGlobalVar s t l x a c) = LMGlobalVar s (pLower t) l x a c
321 pVarLower (LMLocalVar s t ) = LMLocalVar s (pLower t)
322 pVarLower (LMNLocalVar s t ) = LMNLocalVar s (pLower t)
323 pVarLower (LMLitVar _ ) = error $ "Can't lower a literal type!"
324
325 -- | Test if the given 'LlvmType' is an integer
326 isInt :: LlvmType -> Bool
327 isInt (LMInt _) = True
328 isInt _ = False
329
330 -- | Test if the given 'LlvmType' is a floating point type
331 isFloat :: LlvmType -> Bool
332 isFloat LMFloat = True
333 isFloat LMDouble = True
334 isFloat LMFloat80 = True
335 isFloat LMFloat128 = True
336 isFloat _ = False
337
338 -- | Test if the given 'LlvmType' is an 'LMPointer' construct
339 isPointer :: LlvmType -> Bool
340 isPointer (LMPointer _) = True
341 isPointer _ = False
342
343 -- | Test if the given 'LlvmType' is an 'LMVector' construct
344 isVector :: LlvmType -> Bool
345 isVector (LMVector {}) = True
346 isVector _ = False
347
348 -- | Test if a 'LlvmVar' is global.
349 isGlobal :: LlvmVar -> Bool
350 isGlobal (LMGlobalVar _ _ _ _ _ _) = True
351 isGlobal _ = False
352
353 -- | Width in bits of an 'LlvmType', returns 0 if not applicable
354 llvmWidthInBits :: DynFlags -> LlvmType -> Int
355 llvmWidthInBits _ (LMInt n) = n
356 llvmWidthInBits _ (LMFloat) = 32
357 llvmWidthInBits _ (LMDouble) = 64
358 llvmWidthInBits _ (LMFloat80) = 80
359 llvmWidthInBits _ (LMFloat128) = 128
360 -- Could return either a pointer width here or the width of what
361 -- it points to. We will go with the former for now.
362 -- PMW: At least judging by the way LLVM outputs constants, pointers
363 -- should use the former, but arrays the latter.
364 llvmWidthInBits dflags (LMPointer _) = llvmWidthInBits dflags (llvmWord dflags)
365 llvmWidthInBits dflags (LMArray n t) = n * llvmWidthInBits dflags t
366 llvmWidthInBits dflags (LMVector n ty) = n * llvmWidthInBits dflags ty
367 llvmWidthInBits _ LMLabel = 0
368 llvmWidthInBits _ LMVoid = 0
369 llvmWidthInBits dflags (LMStruct tys) = sum $ map (llvmWidthInBits dflags) tys
370 llvmWidthInBits _ (LMStructU _) =
371 -- It's not trivial to calculate the bit width of the unpacked structs,
372 -- since they will be aligned depending on the specified datalayout (
373 -- http://llvm.org/docs/LangRef.html#data-layout ). One way we could support
374 -- this could be to make the LlvmCodeGen.Ppr.moduleLayout be a data type
375 -- that exposes the alignment information. However, currently the only place
376 -- we use unpacked structs is LLVM intrinsics that return them (e.g.,
377 -- llvm.sadd.with.overflow.*), so we don't actually need to compute their
378 -- bit width.
379 panic "llvmWidthInBits: not implemented for LMStructU"
380 llvmWidthInBits _ (LMFunction _) = 0
381 llvmWidthInBits dflags (LMAlias (_,t)) = llvmWidthInBits dflags t
382 llvmWidthInBits _ LMMetadata = panic "llvmWidthInBits: Meta-data has no runtime representation!"
383
384
385 -- -----------------------------------------------------------------------------
386 -- ** Shortcut for Common Types
387 --
388
389 i128, i64, i32, i16, i8, i1, i8Ptr :: LlvmType
390 i128 = LMInt 128
391 i64 = LMInt 64
392 i32 = LMInt 32
393 i16 = LMInt 16
394 i8 = LMInt 8
395 i1 = LMInt 1
396 i8Ptr = pLift i8
397
398 -- | The target architectures word size
399 llvmWord, llvmWordPtr :: DynFlags -> LlvmType
400 llvmWord dflags = LMInt (wORD_SIZE dflags * 8)
401 llvmWordPtr dflags = pLift (llvmWord dflags)
402
403 -- -----------------------------------------------------------------------------
404 -- * LLVM Function Types
405 --
406
407 -- | An LLVM Function
408 data LlvmFunctionDecl = LlvmFunctionDecl {
409 -- | Unique identifier of the function
410 decName :: LMString,
411 -- | LinkageType of the function
412 funcLinkage :: LlvmLinkageType,
413 -- | The calling convention of the function
414 funcCc :: LlvmCallConvention,
415 -- | Type of the returned value
416 decReturnType :: LlvmType,
417 -- | Indicates if this function uses varargs
418 decVarargs :: LlvmParameterListType,
419 -- | Parameter types and attributes
420 decParams :: [LlvmParameter],
421 -- | Function align value, must be power of 2
422 funcAlign :: LMAlign
423 }
424 deriving (Eq)
425
426 instance Outputable LlvmFunctionDecl where
427 ppr (LlvmFunctionDecl n l c r varg p a)
428 = let align = case a of
429 Just a' -> text " align " <> ppr a'
430 Nothing -> empty
431 in ppr l <+> ppr c <+> ppr r <+> char '@' <> ftext n <>
432 lparen <> ppParams varg p <> rparen <> align
433
434 type LlvmFunctionDecls = [LlvmFunctionDecl]
435
436 type LlvmParameter = (LlvmType, [LlvmParamAttr])
437
438 -- | LLVM Parameter Attributes.
439 --
440 -- Parameter attributes are used to communicate additional information about
441 -- the result or parameters of a function
442 data LlvmParamAttr
443 -- | This indicates to the code generator that the parameter or return value
444 -- should be zero-extended to a 32-bit value by the caller (for a parameter)
445 -- or the callee (for a return value).
446 = ZeroExt
447 -- | This indicates to the code generator that the parameter or return value
448 -- should be sign-extended to a 32-bit value by the caller (for a parameter)
449 -- or the callee (for a return value).
450 | SignExt
451 -- | This indicates that this parameter or return value should be treated in
452 -- a special target-dependent fashion during while emitting code for a
453 -- function call or return (usually, by putting it in a register as opposed
454 -- to memory).
455 | InReg
456 -- | This indicates that the pointer parameter should really be passed by
457 -- value to the function.
458 | ByVal
459 -- | This indicates that the pointer parameter specifies the address of a
460 -- structure that is the return value of the function in the source program.
461 | SRet
462 -- | This indicates that the pointer does not alias any global or any other
463 -- parameter.
464 | NoAlias
465 -- | This indicates that the callee does not make any copies of the pointer
466 -- that outlive the callee itself
467 | NoCapture
468 -- | This indicates that the pointer parameter can be excised using the
469 -- trampoline intrinsics.
470 | Nest
471 deriving (Eq)
472
473 instance Outputable LlvmParamAttr where
474 ppr ZeroExt = text "zeroext"
475 ppr SignExt = text "signext"
476 ppr InReg = text "inreg"
477 ppr ByVal = text "byval"
478 ppr SRet = text "sret"
479 ppr NoAlias = text "noalias"
480 ppr NoCapture = text "nocapture"
481 ppr Nest = text "nest"
482
483 -- | Llvm Function Attributes.
484 --
485 -- Function attributes are set to communicate additional information about a
486 -- function. Function attributes are considered to be part of the function,
487 -- not of the function type, so functions with different parameter attributes
488 -- can have the same function type. Functions can have multiple attributes.
489 --
490 -- Descriptions taken from <http://llvm.org/docs/LangRef.html#fnattrs>
491 data LlvmFuncAttr
492 -- | This attribute indicates that the inliner should attempt to inline this
493 -- function into callers whenever possible, ignoring any active inlining
494 -- size threshold for this caller.
495 = AlwaysInline
496 -- | This attribute indicates that the source code contained a hint that
497 -- inlining this function is desirable (such as the \"inline\" keyword in
498 -- C/C++). It is just a hint; it imposes no requirements on the inliner.
499 | InlineHint
500 -- | This attribute indicates that the inliner should never inline this
501 -- function in any situation. This attribute may not be used together
502 -- with the alwaysinline attribute.
503 | NoInline
504 -- | This attribute suggests that optimization passes and code generator
505 -- passes make choices that keep the code size of this function low, and
506 -- otherwise do optimizations specifically to reduce code size.
507 | OptSize
508 -- | This function attribute indicates that the function never returns
509 -- normally. This produces undefined behavior at runtime if the function
510 -- ever does dynamically return.
511 | NoReturn
512 -- | This function attribute indicates that the function never returns with
513 -- an unwind or exceptional control flow. If the function does unwind, its
514 -- runtime behavior is undefined.
515 | NoUnwind
516 -- | This attribute indicates that the function computes its result (or
517 -- decides to unwind an exception) based strictly on its arguments, without
518 -- dereferencing any pointer arguments or otherwise accessing any mutable
519 -- state (e.g. memory, control registers, etc) visible to caller functions.
520 -- It does not write through any pointer arguments (including byval
521 -- arguments) and never changes any state visible to callers. This means
522 -- that it cannot unwind exceptions by calling the C++ exception throwing
523 -- methods, but could use the unwind instruction.
524 | ReadNone
525 -- | This attribute indicates that the function does not write through any
526 -- pointer arguments (including byval arguments) or otherwise modify any
527 -- state (e.g. memory, control registers, etc) visible to caller functions.
528 -- It may dereference pointer arguments and read state that may be set in
529 -- the caller. A readonly function always returns the same value (or unwinds
530 -- an exception identically) when called with the same set of arguments and
531 -- global state. It cannot unwind an exception by calling the C++ exception
532 -- throwing methods, but may use the unwind instruction.
533 | ReadOnly
534 -- | This attribute indicates that the function should emit a stack smashing
535 -- protector. It is in the form of a \"canary\"—a random value placed on the
536 -- stack before the local variables that's checked upon return from the
537 -- function to see if it has been overwritten. A heuristic is used to
538 -- determine if a function needs stack protectors or not.
539 --
540 -- If a function that has an ssp attribute is inlined into a function that
541 -- doesn't have an ssp attribute, then the resulting function will have an
542 -- ssp attribute.
543 | Ssp
544 -- | This attribute indicates that the function should always emit a stack
545 -- smashing protector. This overrides the ssp function attribute.
546 --
547 -- If a function that has an sspreq attribute is inlined into a function
548 -- that doesn't have an sspreq attribute or which has an ssp attribute,
549 -- then the resulting function will have an sspreq attribute.
550 | SspReq
551 -- | This attribute indicates that the code generator should not use a red
552 -- zone, even if the target-specific ABI normally permits it.
553 | NoRedZone
554 -- | This attributes disables implicit floating point instructions.
555 | NoImplicitFloat
556 -- | This attribute disables prologue / epilogue emission for the function.
557 -- This can have very system-specific consequences.
558 | Naked
559 deriving (Eq)
560
561 instance Outputable LlvmFuncAttr where
562 ppr AlwaysInline = text "alwaysinline"
563 ppr InlineHint = text "inlinehint"
564 ppr NoInline = text "noinline"
565 ppr OptSize = text "optsize"
566 ppr NoReturn = text "noreturn"
567 ppr NoUnwind = text "nounwind"
568 ppr ReadNone = text "readnon"
569 ppr ReadOnly = text "readonly"
570 ppr Ssp = text "ssp"
571 ppr SspReq = text "ssqreq"
572 ppr NoRedZone = text "noredzone"
573 ppr NoImplicitFloat = text "noimplicitfloat"
574 ppr Naked = text "naked"
575
576
577 -- | Different types to call a function.
578 data LlvmCallType
579 -- | Normal call, allocate a new stack frame.
580 = StdCall
581 -- | Tail call, perform the call in the current stack frame.
582 | TailCall
583 deriving (Eq,Show)
584
585 -- | Different calling conventions a function can use.
586 data LlvmCallConvention
587 -- | The C calling convention.
588 -- This calling convention (the default if no other calling convention is
589 -- specified) matches the target C calling conventions. This calling
590 -- convention supports varargs function calls and tolerates some mismatch in
591 -- the declared prototype and implemented declaration of the function (as
592 -- does normal C).
593 = CC_Ccc
594 -- | This calling convention attempts to make calls as fast as possible
595 -- (e.g. by passing things in registers). This calling convention allows
596 -- the target to use whatever tricks it wants to produce fast code for the
597 -- target, without having to conform to an externally specified ABI
598 -- (Application Binary Interface). Implementations of this convention should
599 -- allow arbitrary tail call optimization to be supported. This calling
600 -- convention does not support varargs and requires the prototype of al
601 -- callees to exactly match the prototype of the function definition.
602 | CC_Fastcc
603 -- | This calling convention attempts to make code in the caller as efficient
604 -- as possible under the assumption that the call is not commonly executed.
605 -- As such, these calls often preserve all registers so that the call does
606 -- not break any live ranges in the caller side. This calling convention
607 -- does not support varargs and requires the prototype of all callees to
608 -- exactly match the prototype of the function definition.
609 | CC_Coldcc
610 -- | The GHC-specific 'registerised' calling convention.
611 | CC_Ghc
612 -- | Any calling convention may be specified by number, allowing
613 -- target-specific calling conventions to be used. Target specific calling
614 -- conventions start at 64.
615 | CC_Ncc Int
616 -- | X86 Specific 'StdCall' convention. LLVM includes a specific alias for it
617 -- rather than just using CC_Ncc.
618 | CC_X86_Stdcc
619 deriving (Eq)
620
621 instance Outputable LlvmCallConvention where
622 ppr CC_Ccc = text "ccc"
623 ppr CC_Fastcc = text "fastcc"
624 ppr CC_Coldcc = text "coldcc"
625 ppr CC_Ghc = text "ghccc"
626 ppr (CC_Ncc i) = text "cc " <> ppr i
627 ppr CC_X86_Stdcc = text "x86_stdcallcc"
628
629
630 -- | Functions can have a fixed amount of parameters, or a variable amount.
631 data LlvmParameterListType
632 -- Fixed amount of arguments.
633 = FixedArgs
634 -- Variable amount of arguments.
635 | VarArgs
636 deriving (Eq,Show)
637
638
639 -- | Linkage type of a symbol.
640 --
641 -- The description of the constructors is copied from the Llvm Assembly Language
642 -- Reference Manual <http://www.llvm.org/docs/LangRef.html#linkage>, because
643 -- they correspond to the Llvm linkage types.
644 data LlvmLinkageType
645 -- | Global values with internal linkage are only directly accessible by
646 -- objects in the current module. In particular, linking code into a module
647 -- with an internal global value may cause the internal to be renamed as
648 -- necessary to avoid collisions. Because the symbol is internal to the
649 -- module, all references can be updated. This corresponds to the notion
650 -- of the @static@ keyword in C.
651 = Internal
652 -- | Globals with @linkonce@ linkage are merged with other globals of the
653 -- same name when linkage occurs. This is typically used to implement
654 -- inline functions, templates, or other code which must be generated
655 -- in each translation unit that uses it. Unreferenced linkonce globals are
656 -- allowed to be discarded.
657 | LinkOnce
658 -- | @weak@ linkage is exactly the same as linkonce linkage, except that
659 -- unreferenced weak globals may not be discarded. This is used for globals
660 -- that may be emitted in multiple translation units, but that are not
661 -- guaranteed to be emitted into every translation unit that uses them. One
662 -- example of this are common globals in C, such as @int X;@ at global
663 -- scope.
664 | Weak
665 -- | @appending@ linkage may only be applied to global variables of pointer
666 -- to array type. When two global variables with appending linkage are
667 -- linked together, the two global arrays are appended together. This is
668 -- the Llvm, typesafe, equivalent of having the system linker append
669 -- together @sections@ with identical names when .o files are linked.
670 | Appending
671 -- | The semantics of this linkage follow the ELF model: the symbol is weak
672 -- until linked, if not linked, the symbol becomes null instead of being an
673 -- undefined reference.
674 | ExternWeak
675 -- | The symbol participates in linkage and can be used to resolve external
676 -- symbol references.
677 | ExternallyVisible
678 -- | Alias for 'ExternallyVisible' but with explicit textual form in LLVM
679 -- assembly.
680 | External
681 -- | Symbol is private to the module and should not appear in the symbol table
682 | Private
683 deriving (Eq)
684
685 instance Outputable LlvmLinkageType where
686 ppr Internal = text "internal"
687 ppr LinkOnce = text "linkonce"
688 ppr Weak = text "weak"
689 ppr Appending = text "appending"
690 ppr ExternWeak = text "extern_weak"
691 -- ExternallyVisible does not have a textual representation, it is
692 -- the linkage type a function resolves to if no other is specified
693 -- in Llvm.
694 ppr ExternallyVisible = empty
695 ppr External = text "external"
696 ppr Private = text "private"
697
698 -- -----------------------------------------------------------------------------
699 -- * LLVM Operations
700 --
701
702 -- | Llvm binary operators machine operations.
703 data LlvmMachOp
704 = LM_MO_Add -- ^ add two integer, floating point or vector values.
705 | LM_MO_Sub -- ^ subtract two ...
706 | LM_MO_Mul -- ^ multiply ..
707 | LM_MO_UDiv -- ^ unsigned integer or vector division.
708 | LM_MO_SDiv -- ^ signed integer ..
709 | LM_MO_URem -- ^ unsigned integer or vector remainder (mod)
710 | LM_MO_SRem -- ^ signed ...
711
712 | LM_MO_FAdd -- ^ add two floating point or vector values.
713 | LM_MO_FSub -- ^ subtract two ...
714 | LM_MO_FMul -- ^ multiply ...
715 | LM_MO_FDiv -- ^ divide ...
716 | LM_MO_FRem -- ^ remainder ...
717
718 -- | Left shift
719 | LM_MO_Shl
720 -- | Logical shift right
721 -- Shift right, filling with zero
722 | LM_MO_LShr
723 -- | Arithmetic shift right
724 -- The most significant bits of the result will be equal to the sign bit of
725 -- the left operand.
726 | LM_MO_AShr
727
728 | LM_MO_And -- ^ AND bitwise logical operation.
729 | LM_MO_Or -- ^ OR bitwise logical operation.
730 | LM_MO_Xor -- ^ XOR bitwise logical operation.
731 deriving (Eq)
732
733 instance Outputable LlvmMachOp where
734 ppr LM_MO_Add = text "add"
735 ppr LM_MO_Sub = text "sub"
736 ppr LM_MO_Mul = text "mul"
737 ppr LM_MO_UDiv = text "udiv"
738 ppr LM_MO_SDiv = text "sdiv"
739 ppr LM_MO_URem = text "urem"
740 ppr LM_MO_SRem = text "srem"
741 ppr LM_MO_FAdd = text "fadd"
742 ppr LM_MO_FSub = text "fsub"
743 ppr LM_MO_FMul = text "fmul"
744 ppr LM_MO_FDiv = text "fdiv"
745 ppr LM_MO_FRem = text "frem"
746 ppr LM_MO_Shl = text "shl"
747 ppr LM_MO_LShr = text "lshr"
748 ppr LM_MO_AShr = text "ashr"
749 ppr LM_MO_And = text "and"
750 ppr LM_MO_Or = text "or"
751 ppr LM_MO_Xor = text "xor"
752
753
754 -- | Llvm compare operations.
755 data LlvmCmpOp
756 = LM_CMP_Eq -- ^ Equal (Signed and Unsigned)
757 | LM_CMP_Ne -- ^ Not equal (Signed and Unsigned)
758 | LM_CMP_Ugt -- ^ Unsigned greater than
759 | LM_CMP_Uge -- ^ Unsigned greater than or equal
760 | LM_CMP_Ult -- ^ Unsigned less than
761 | LM_CMP_Ule -- ^ Unsigned less than or equal
762 | LM_CMP_Sgt -- ^ Signed greater than
763 | LM_CMP_Sge -- ^ Signed greater than or equal
764 | LM_CMP_Slt -- ^ Signed less than
765 | LM_CMP_Sle -- ^ Signed less than or equal
766
767 -- Float comparisons. GHC uses a mix of ordered and unordered float
768 -- comparisons.
769 | LM_CMP_Feq -- ^ Float equal
770 | LM_CMP_Fne -- ^ Float not equal
771 | LM_CMP_Fgt -- ^ Float greater than
772 | LM_CMP_Fge -- ^ Float greater than or equal
773 | LM_CMP_Flt -- ^ Float less than
774 | LM_CMP_Fle -- ^ Float less than or equal
775 deriving (Eq)
776
777 instance Outputable LlvmCmpOp where
778 ppr LM_CMP_Eq = text "eq"
779 ppr LM_CMP_Ne = text "ne"
780 ppr LM_CMP_Ugt = text "ugt"
781 ppr LM_CMP_Uge = text "uge"
782 ppr LM_CMP_Ult = text "ult"
783 ppr LM_CMP_Ule = text "ule"
784 ppr LM_CMP_Sgt = text "sgt"
785 ppr LM_CMP_Sge = text "sge"
786 ppr LM_CMP_Slt = text "slt"
787 ppr LM_CMP_Sle = text "sle"
788 ppr LM_CMP_Feq = text "oeq"
789 ppr LM_CMP_Fne = text "une"
790 ppr LM_CMP_Fgt = text "ogt"
791 ppr LM_CMP_Fge = text "oge"
792 ppr LM_CMP_Flt = text "olt"
793 ppr LM_CMP_Fle = text "ole"
794
795
796 -- | Llvm cast operations.
797 data LlvmCastOp
798 = LM_Trunc -- ^ Integer truncate
799 | LM_Zext -- ^ Integer extend (zero fill)
800 | LM_Sext -- ^ Integer extend (sign fill)
801 | LM_Fptrunc -- ^ Float truncate
802 | LM_Fpext -- ^ Float extend
803 | LM_Fptoui -- ^ Float to unsigned Integer
804 | LM_Fptosi -- ^ Float to signed Integer
805 | LM_Uitofp -- ^ Unsigned Integer to Float
806 | LM_Sitofp -- ^ Signed Int to Float
807 | LM_Ptrtoint -- ^ Pointer to Integer
808 | LM_Inttoptr -- ^ Integer to Pointer
809 | LM_Bitcast -- ^ Cast between types where no bit manipulation is needed
810 deriving (Eq)
811
812 instance Outputable LlvmCastOp where
813 ppr LM_Trunc = text "trunc"
814 ppr LM_Zext = text "zext"
815 ppr LM_Sext = text "sext"
816 ppr LM_Fptrunc = text "fptrunc"
817 ppr LM_Fpext = text "fpext"
818 ppr LM_Fptoui = text "fptoui"
819 ppr LM_Fptosi = text "fptosi"
820 ppr LM_Uitofp = text "uitofp"
821 ppr LM_Sitofp = text "sitofp"
822 ppr LM_Ptrtoint = text "ptrtoint"
823 ppr LM_Inttoptr = text "inttoptr"
824 ppr LM_Bitcast = text "bitcast"
825
826
827 -- -----------------------------------------------------------------------------
828 -- * Floating point conversion
829 --
830
831 -- | Convert a Haskell Double to an LLVM hex encoded floating point form. In
832 -- Llvm float literals can be printed in a big-endian hexadecimal format,
833 -- regardless of underlying architecture.
834 --
835 -- See Note [LLVM Float Types].
836 ppDouble :: Double -> SDoc
837 ppDouble d
838 = let bs = doubleToBytes d
839 hex d' = case showHex d' "" of
840 [] -> error "dToStr: too few hex digits for float"
841 [x] -> ['0',x]
842 [x,y] -> [x,y]
843 _ -> error "dToStr: too many hex digits for float"
844
845 str = map toUpper $ concat $ fixEndian $ map hex bs
846 in text "0x" <> text str
847
848 -- Note [LLVM Float Types]
849 -- ~~~~~~~~~~~~~~~~~~~~~~~
850 -- We use 'ppDouble' for both printing Float and Double floating point types. This is
851 -- as LLVM expects all floating point constants (single & double) to be in IEEE
852 -- 754 Double precision format. However, for single precision numbers (Float)
853 -- they should be *representable* in IEEE 754 Single precision format. So the
854 -- easiest way to do this is to narrow and widen again.
855 -- (i.e., Double -> Float -> Double). We must be careful doing this that GHC
856 -- doesn't optimize that away.
857
858 -- Note [narrowFp & widenFp]
859 -- ~~~~~~~~~~~~~~~~~~~~~~~~~
860 -- NOTE: we use float2Double & co directly as GHC likes to optimize away
861 -- successive calls of 'realToFrac', defeating the narrowing. (Bug #7600).
862 -- 'realToFrac' has inconsistent behaviour with optimisation as well that can
863 -- also cause issues, these methods don't.
864
865 narrowFp :: Double -> Float
866 {-# NOINLINE narrowFp #-}
867 narrowFp = double2Float
868
869 widenFp :: Float -> Double
870 {-# NOINLINE widenFp #-}
871 widenFp = float2Double
872
873 ppFloat :: Float -> SDoc
874 ppFloat = ppDouble . widenFp
875
876 -- | Reverse or leave byte data alone to fix endianness on this target.
877 fixEndian :: [a] -> [a]
878 #if defined(WORDS_BIGENDIAN)
879 fixEndian = id
880 #else
881 fixEndian = reverse
882 #endif
883
884
885 --------------------------------------------------------------------------------
886 -- * Misc functions
887 --------------------------------------------------------------------------------
888
889 ppCommaJoin :: (Outputable a) => [a] -> SDoc
890 ppCommaJoin strs = hsep $ punctuate comma (map ppr strs)
891
892 ppSpaceJoin :: (Outputable a) => [a] -> SDoc
893 ppSpaceJoin strs = hsep (map ppr strs)