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