Pull recent Hadrian changes from upstream
[ghc.git] / compiler / cmm / Cmm.hs
1 -- Cmm representations using Hoopl's Graph CmmNode e x.
2 {-# LANGUAGE CPP, GADTs #-}
3
4 module Cmm (
5 -- * Cmm top-level datatypes
6 CmmProgram, CmmGroup, GenCmmGroup,
7 CmmDecl, GenCmmDecl(..),
8 CmmGraph, GenCmmGraph(..),
9 CmmBlock,
10 RawCmmDecl, RawCmmGroup,
11 Section(..), SectionType(..), CmmStatics(..), CmmStatic(..),
12 isSecConstant,
13
14 -- ** Blocks containing lists
15 GenBasicBlock(..), blockId,
16 ListGraph(..), pprBBlock,
17
18 -- * Info Tables
19 CmmTopInfo(..), CmmStackInfo(..), CmmInfoTable(..), topInfoTable,
20 ClosureTypeInfo(..),
21 C_SRT(..), needsSRT,
22 ProfilingInfo(..), ConstrDescription,
23
24 -- * Statements, expressions and types
25 module CmmNode,
26 module CmmExpr,
27 ) where
28
29 import GhcPrelude
30
31 import CLabel
32 import BlockId
33 import CmmNode
34 import SMRep
35 import CmmExpr
36 import Hoopl.Block
37 import Hoopl.Collections
38 import Hoopl.Graph
39 import Hoopl.Label
40 import Outputable
41
42 import Data.Word ( Word8 )
43
44 #include "HsVersions.h"
45
46 -----------------------------------------------------------------------------
47 -- Cmm, GenCmm
48 -----------------------------------------------------------------------------
49
50 -- A CmmProgram is a list of CmmGroups
51 -- A CmmGroup is a list of top-level declarations
52
53 -- When object-splitting is on, each group is compiled into a separate
54 -- .o file. So typically we put closely related stuff in a CmmGroup.
55 -- Section-splitting follows suit and makes one .text subsection for each
56 -- CmmGroup.
57
58 type CmmProgram = [CmmGroup]
59
60 type GenCmmGroup d h g = [GenCmmDecl d h g]
61 type CmmGroup = GenCmmGroup CmmStatics CmmTopInfo CmmGraph
62 type RawCmmGroup = GenCmmGroup CmmStatics (LabelMap CmmStatics) CmmGraph
63
64 -----------------------------------------------------------------------------
65 -- CmmDecl, GenCmmDecl
66 -----------------------------------------------------------------------------
67
68 -- GenCmmDecl is abstracted over
69 -- d, the type of static data elements in CmmData
70 -- h, the static info preceding the code of a CmmProc
71 -- g, the control-flow graph of a CmmProc
72 --
73 -- We expect there to be two main instances of this type:
74 -- (a) C--, i.e. populated with various C-- constructs
75 -- (b) Native code, populated with data/instructions
76
77 -- | A top-level chunk, abstracted over the type of the contents of
78 -- the basic blocks (Cmm or instructions are the likely instantiations).
79 data GenCmmDecl d h g
80 = CmmProc -- A procedure
81 h -- Extra header such as the info table
82 CLabel -- Entry label
83 [GlobalReg] -- Registers live on entry. Note that the set of live
84 -- registers will be correct in generated C-- code, but
85 -- not in hand-written C-- code. However,
86 -- splitAtProcPoints calculates correct liveness
87 -- information for CmmProcs.
88 g -- Control-flow graph for the procedure's code
89
90 | CmmData -- Static data
91 Section
92 d
93
94 type CmmDecl = GenCmmDecl CmmStatics CmmTopInfo CmmGraph
95
96 type RawCmmDecl
97 = GenCmmDecl
98 CmmStatics
99 (LabelMap CmmStatics)
100 CmmGraph
101
102 -----------------------------------------------------------------------------
103 -- Graphs
104 -----------------------------------------------------------------------------
105
106 type CmmGraph = GenCmmGraph CmmNode
107 data GenCmmGraph n = CmmGraph { g_entry :: BlockId, g_graph :: Graph n C C }
108 type CmmBlock = Block CmmNode C C
109
110 -----------------------------------------------------------------------------
111 -- Info Tables
112 -----------------------------------------------------------------------------
113
114 data CmmTopInfo = TopInfo { info_tbls :: LabelMap CmmInfoTable
115 , stack_info :: CmmStackInfo }
116
117 topInfoTable :: GenCmmDecl a CmmTopInfo (GenCmmGraph n) -> Maybe CmmInfoTable
118 topInfoTable (CmmProc infos _ _ g) = mapLookup (g_entry g) (info_tbls infos)
119 topInfoTable _ = Nothing
120
121 data CmmStackInfo
122 = StackInfo {
123 arg_space :: ByteOff,
124 -- number of bytes of arguments on the stack on entry to the
125 -- the proc. This is filled in by StgCmm.codeGen, and used
126 -- by the stack allocator later.
127 updfr_space :: Maybe ByteOff,
128 -- XXX: this never contains anything useful, but it should.
129 -- See comment in CmmLayoutStack.
130 do_layout :: Bool
131 -- Do automatic stack layout for this proc. This is
132 -- True for all code generated by the code generator,
133 -- but is occasionally False for hand-written Cmm where
134 -- we want to do the stack manipulation manually.
135 }
136
137 -- | Info table as a haskell data type
138 data CmmInfoTable
139 = CmmInfoTable {
140 cit_lbl :: CLabel, -- Info table label
141 cit_rep :: SMRep,
142 cit_prof :: ProfilingInfo,
143 cit_srt :: C_SRT
144 }
145
146 data ProfilingInfo
147 = NoProfilingInfo
148 | ProfilingInfo [Word8] [Word8] -- closure_type, closure_desc
149
150 -- C_SRT is what StgSyn.SRT gets translated to...
151 -- we add a label for the table, and expect only the 'offset/length' form
152
153 data C_SRT = NoC_SRT
154 | C_SRT !CLabel !WordOff !StgHalfWord {-bitmap or escape-}
155 deriving (Eq)
156
157 needsSRT :: C_SRT -> Bool
158 needsSRT NoC_SRT = False
159 needsSRT (C_SRT _ _ _) = True
160
161 -----------------------------------------------------------------------------
162 -- Static Data
163 -----------------------------------------------------------------------------
164
165 data SectionType
166 = Text
167 | Data
168 | ReadOnlyData
169 | RelocatableReadOnlyData
170 | UninitialisedData
171 | ReadOnlyData16 -- .rodata.cst16 on x86_64, 16-byte aligned
172 | CString
173 | OtherSection String
174 deriving (Show)
175
176 -- | Should a data in this section be considered constant
177 isSecConstant :: Section -> Bool
178 isSecConstant (Section t _) = case t of
179 Text -> True
180 ReadOnlyData -> True
181 RelocatableReadOnlyData -> True
182 ReadOnlyData16 -> True
183 CString -> True
184 Data -> False
185 UninitialisedData -> False
186 (OtherSection _) -> False
187
188 data Section = Section SectionType CLabel
189
190 data CmmStatic
191 = CmmStaticLit CmmLit
192 -- a literal value, size given by cmmLitRep of the literal.
193 | CmmUninitialised Int
194 -- uninitialised data, N bytes long
195 | CmmString [Word8]
196 -- string of 8-bit values only, not zero terminated.
197
198 data CmmStatics
199 = Statics
200 CLabel -- Label of statics
201 [CmmStatic] -- The static data itself
202
203 -- -----------------------------------------------------------------------------
204 -- Basic blocks consisting of lists
205
206 -- These are used by the LLVM and NCG backends, when populating Cmm
207 -- with lists of instructions.
208
209 data GenBasicBlock i = BasicBlock BlockId [i]
210
211 -- | The branch block id is that of the first block in
212 -- the branch, which is that branch's entry point
213 blockId :: GenBasicBlock i -> BlockId
214 blockId (BasicBlock blk_id _ ) = blk_id
215
216 newtype ListGraph i = ListGraph [GenBasicBlock i]
217
218 instance Outputable instr => Outputable (ListGraph instr) where
219 ppr (ListGraph blocks) = vcat (map ppr blocks)
220
221 instance Outputable instr => Outputable (GenBasicBlock instr) where
222 ppr = pprBBlock
223
224 pprBBlock :: Outputable stmt => GenBasicBlock stmt -> SDoc
225 pprBBlock (BasicBlock ident stmts) =
226 hang (ppr ident <> colon) 4 (vcat (map ppr stmts))
227