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