d0564e6f68d7fc27d959b467859fa4871cc0fcd8
[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
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 -- 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 (BlockEnv 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 (BlockEnv 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 type CmmReplGraph e x = GenCmmReplGraph CmmNode e x
109 type GenCmmReplGraph n e x = UniqSM (Maybe (Graph n e x))
110 type CmmFwdRewrite f = FwdRewrite UniqSM CmmNode f
111 type CmmBwdRewrite f = BwdRewrite UniqSM CmmNode f
112
113 -----------------------------------------------------------------------------
114 -- Info Tables
115 -----------------------------------------------------------------------------
116
117 data CmmTopInfo = TopInfo { info_tbls :: BlockEnv CmmInfoTable
118 , stack_info :: CmmStackInfo }
119
120 topInfoTable :: GenCmmDecl a CmmTopInfo (GenCmmGraph n) -> Maybe CmmInfoTable
121 topInfoTable (CmmProc infos _ _ g) = mapLookup (g_entry g) (info_tbls infos)
122 topInfoTable _ = Nothing
123
124 data CmmStackInfo
125 = StackInfo {
126 arg_space :: ByteOff,
127 -- number of bytes of arguments on the stack on entry to the
128 -- the proc. This is filled in by StgCmm.codeGen, and used
129 -- by the stack allocator later.
130 updfr_space :: Maybe ByteOff,
131 -- XXX: this never contains anything useful, but it should.
132 -- See comment in CmmLayoutStack.
133 do_layout :: Bool
134 -- Do automatic stack layout for this proc. This is
135 -- True for all code generated by the code generator,
136 -- but is occasionally False for hand-written Cmm where
137 -- we want to do the stack manipulation manually.
138 }
139
140 -- | Info table as a haskell data type
141 data CmmInfoTable
142 = CmmInfoTable {
143 cit_lbl :: CLabel, -- Info table label
144 cit_rep :: SMRep,
145 cit_prof :: ProfilingInfo,
146 cit_srt :: C_SRT
147 }
148
149 data ProfilingInfo
150 = NoProfilingInfo
151 | ProfilingInfo [Word8] [Word8] -- closure_type, closure_desc
152
153 -- C_SRT is what StgSyn.SRT gets translated to...
154 -- we add a label for the table, and expect only the 'offset/length' form
155
156 data C_SRT = NoC_SRT
157 | C_SRT !CLabel !WordOff !StgHalfWord {-bitmap or escape-}
158 deriving (Eq)
159
160 needsSRT :: C_SRT -> Bool
161 needsSRT NoC_SRT = False
162 needsSRT (C_SRT _ _ _) = True
163
164 -----------------------------------------------------------------------------
165 -- Static Data
166 -----------------------------------------------------------------------------
167
168 data SectionType
169 = Text
170 | Data
171 | ReadOnlyData
172 | RelocatableReadOnlyData
173 | UninitialisedData
174 | ReadOnlyData16 -- .rodata.cst16 on x86_64, 16-byte aligned
175 | OtherSection String
176 deriving (Show)
177
178 data Section = Section SectionType CLabel
179
180 data CmmStatic
181 = CmmStaticLit CmmLit
182 -- a literal value, size given by cmmLitRep of the literal.
183 | CmmUninitialised Int
184 -- uninitialised data, N bytes long
185 | CmmString [Word8]
186 -- string of 8-bit values only, not zero terminated.
187
188 data CmmStatics
189 = Statics
190 CLabel -- Label of statics
191 [CmmStatic] -- The static data itself
192
193 -- -----------------------------------------------------------------------------
194 -- Basic blocks consisting of lists
195
196 -- These are used by the LLVM and NCG backends, when populating Cmm
197 -- with lists of instructions.
198
199 data GenBasicBlock i = BasicBlock BlockId [i]
200
201 -- | The branch block id is that of the first block in
202 -- the branch, which is that branch's entry point
203 blockId :: GenBasicBlock i -> BlockId
204 blockId (BasicBlock blk_id _ ) = blk_id
205
206 newtype ListGraph i = ListGraph [GenBasicBlock i]
207
208 instance Outputable instr => Outputable (ListGraph instr) where
209 ppr (ListGraph blocks) = vcat (map ppr blocks)
210
211 instance Outputable instr => Outputable (GenBasicBlock instr) where
212 ppr = pprBBlock
213
214 pprBBlock :: Outputable stmt => GenBasicBlock stmt -> SDoc
215 pprBBlock (BasicBlock ident stmts) =
216 hang (ppr ident <> colon) 4 (vcat (map ppr stmts))
217