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