1 -- ----------------------------------------------------------------------------
2 -- | Base LLVM Code Generation module
4 -- Contains functions useful through out the code generator.
7 module LlvmCodeGen.Base (
9 LlvmCmmTop, LlvmBasicBlock,
10 LlvmUnresData, LlvmData, UnresLabel, UnresStatic,
12 LlvmEnv, initLlvmEnv, clearVars, varLookup, varInsert,
15 cmmToLlvmType, widthToLlvmFloat, widthToLlvmInt, llvmFunTy,
16 llvmFunSig, llvmStdFunAttrs, llvmFunAlign, llvmInfAlign,
17 llvmPtrBits, mkLlvmFunc, tysToParams,
19 strCLabel_llvm, genCmmLabelRef, genStringLabelRef
23 #include "HsVersions.h"
26 import LlvmCodeGen.Regs
28 import CgUtils ( activeStgRegs )
33 import qualified Outputable as Outp
37 -- ----------------------------------------------------------------------------
41 type LlvmCmmTop = GenCmmTop LlvmData [CmmStatic] (ListGraph LlvmStatement)
42 type LlvmBasicBlock = GenBasicBlock LlvmStatement
45 -- Of the form: (data label, data type, unresovled data)
46 type LlvmUnresData = (CLabel, Section, LlvmType, [UnresStatic])
48 -- | Top level LLVM Data (globals and type aliases)
49 type LlvmData = ([LMGlobal], [LlvmType])
51 -- | An unresolved Label.
53 -- Labels are unresolved when we haven't yet determined if they are defined in
54 -- the module we are currently compiling, or an external one.
55 type UnresLabel = CmmLit
56 type UnresStatic = Either UnresLabel LlvmStatic
58 -- ----------------------------------------------------------------------------
59 -- * Type translations
62 -- | Translate a basic CmmType to an LlvmType.
63 cmmToLlvmType :: CmmType -> LlvmType
64 cmmToLlvmType ty | isFloatType ty = widthToLlvmFloat $ typeWidth ty
65 | otherwise = widthToLlvmInt $ typeWidth ty
67 -- | Translate a Cmm Float Width to a LlvmType.
68 widthToLlvmFloat :: Width -> LlvmType
69 widthToLlvmFloat W32 = LMFloat
70 widthToLlvmFloat W64 = LMDouble
71 widthToLlvmFloat W80 = LMFloat80
72 widthToLlvmFloat W128 = LMFloat128
73 widthToLlvmFloat w = panic $ "widthToLlvmFloat: Bad float size: " ++ show w
75 -- | Translate a Cmm Bit Width to a LlvmType.
76 widthToLlvmInt :: Width -> LlvmType
77 widthToLlvmInt w = LMInt $ widthInBits w
79 -- | GHC Call Convention for LLVM
80 llvmGhcCC :: LlvmCallConvention
83 -- | Llvm Function type for Cmm function
85 llvmFunTy = LMFunction $ llvmFunSig' (fsLit "a") ExternallyVisible
87 -- | Llvm Function signature
88 llvmFunSig :: CLabel -> LlvmLinkageType -> LlvmFunctionDecl
89 llvmFunSig lbl link = llvmFunSig' (strCLabel_llvm lbl) link
91 llvmFunSig' :: LMString -> LlvmLinkageType -> LlvmFunctionDecl
92 llvmFunSig' lbl link = LlvmFunctionDecl lbl link llvmGhcCC LMVoid FixedArgs
93 (tysToParams $ map getVarType llvmFunArgs) llvmFunAlign
95 -- | Create a Haskell function in LLVM.
96 mkLlvmFunc :: CLabel -> LlvmLinkageType -> LMSection -> LlvmBlocks -> LlvmFunction
97 mkLlvmFunc lbl link sec blks
98 = let funDec = llvmFunSig lbl link
99 funArgs = map (fsLit . getPlainName) llvmFunArgs
100 in LlvmFunction funDec funArgs llvmStdFunAttrs sec blks
102 -- | Alignment to use for functions
103 llvmFunAlign :: LMAlign
104 llvmFunAlign = Just 4
106 -- | Alignment to use for into tables
107 llvmInfAlign :: LMAlign
108 llvmInfAlign = Just 4
110 -- | A Function's arguments
111 llvmFunArgs :: [LlvmVar]
112 llvmFunArgs = map lmGlobalRegArg activeStgRegs
114 -- | Llvm standard fun attributes
115 llvmStdFunAttrs :: [LlvmFuncAttr]
116 llvmStdFunAttrs = [NoUnwind]
118 -- | Convert a list of types to a list of function parameters
119 -- (each with no parameter attributes)
120 tysToParams :: [LlvmType] -> [LlvmParameter]
121 tysToParams = map (\ty -> (ty, []))
125 llvmPtrBits = widthInBits $ typeWidth gcWord
128 -- ----------------------------------------------------------------------------
129 -- * Environment Handling
132 type LlvmEnvMap = UniqFM LlvmType
133 -- two maps, one for functions and one for local vars.
134 type LlvmEnv = (LlvmEnvMap, LlvmEnvMap)
136 -- | Get initial Llvm environment.
137 initLlvmEnv :: LlvmEnv
138 initLlvmEnv = (emptyUFM, emptyUFM)
140 -- | Clear variables from the environment.
141 clearVars :: LlvmEnv -> LlvmEnv
142 clearVars (e1, _) = (e1, emptyUFM)
144 -- | Insert functions into the environment.
145 varInsert, funInsert :: Uniquable key => key -> LlvmType -> LlvmEnv -> LlvmEnv
146 varInsert s t (e1, e2) = (e1, addToUFM e2 s t)
147 funInsert s t (e1, e2) = (addToUFM e1 s t, e2)
149 -- | Lookup functions in the environment.
150 varLookup, funLookup :: Uniquable key => key -> LlvmEnv -> Maybe LlvmType
151 varLookup s (_, e2) = lookupUFM e2 s
152 funLookup s (e1, _) = lookupUFM e1 s
155 -- ----------------------------------------------------------------------------
159 -- | Pretty print a 'CLabel'.
160 strCLabel_llvm :: CLabel -> LMString
161 strCLabel_llvm l = (fsLit . show . llvmSDoc . pprCLabel) l
163 -- | Create an external definition for a 'CLabel' defined in another module.
164 genCmmLabelRef :: CLabel -> LMGlobal
165 genCmmLabelRef = genStringLabelRef . strCLabel_llvm
167 -- | As above ('genCmmLabelRef') but taking a 'LMString', not 'CLabel'.
168 genStringLabelRef :: LMString -> LMGlobal
170 = let ty = LMPointer $ LMArray 0 llvmWord
171 in (LMGlobalVar cl ty External Nothing Nothing False, Nothing)
174 -- ----------------------------------------------------------------------------
180 panic s = Outp.panic $ "LlvmCodeGen.Base." ++ s