3d0599b7eabebba6ffc16e0a4c75f6e356779751
[ghc.git] / compiler / cmm / OldCmm.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Old-style Cmm data types
4 --
5 -- (c) The University of Glasgow 2004-2006
6 --
7 -----------------------------------------------------------------------------
8
9 module OldCmm (
10 CmmGroup, GenCmmGroup, RawCmmGroup, CmmDecl, RawCmmDecl,
11 ListGraph(..),
12 CmmInfoTable(..), ClosureTypeInfo(..), topInfoTable,
13 CmmStatic(..), CmmStatics(..), CmmFormal, CmmActual,
14
15 cmmMapGraph, cmmTopMapGraph,
16
17 GenBasicBlock(..), CmmBasicBlock, blockId, blockStmts, mapBlockStmts,
18
19 CmmStmt(..), New.CmmReturnInfo(..), CmmHinted(..),
20 HintedCmmFormal, HintedCmmActual,
21
22 CmmSafety(..), CmmCallTarget(..),
23 New.GenCmmDecl(..), New.ForeignHint(..),
24
25 module CmmExpr,
26
27 Section(..), ProfilingInfo(..), C_SRT(..)
28 ) where
29
30 #include "HsVersions.h"
31
32 import qualified Cmm as New
33 import Cmm ( CmmInfoTable(..), GenCmmGroup, CmmStatics(..), GenCmmDecl(..),
34 CmmFormal, CmmActual, Section(..), CmmStatic(..),
35 ProfilingInfo(..), ClosureTypeInfo(..) )
36
37 import BlockId
38 import ClosureInfo
39 import CmmExpr
40 import FastString
41 import ForeignCall
42
43
44 -- A [[BlockId]] is a local label.
45 -- Local labels must be unique within an entire compilation unit, not
46 -- just a single top-level item, because local labels map one-to-one
47 -- with assembly-language labels.
48
49 -----------------------------------------------------------------------------
50 -- Cmm, CmmDecl, CmmBasicBlock
51 -----------------------------------------------------------------------------
52
53 -- A file is a list of top-level chunks. These may be arbitrarily
54 -- re-orderd during code generation.
55
56 -- | A control-flow graph represented as a list of extended basic blocks.
57 --
58 -- Code, may be empty. The first block is the entry point. The
59 -- order is otherwise initially unimportant, but at some point the
60 -- code gen will fix the order.
61 --
62 -- BlockIds must be unique across an entire compilation unit, since
63 -- they are translated to assembly-language labels, which scope
64 -- across a whole compilation unit.
65 newtype ListGraph i = ListGraph [GenBasicBlock i]
66
67 type CmmInfoTables = BlockEnv CmmInfoTable
68
69 -- | Cmm with the info table as a data type
70 type CmmGroup = GenCmmGroup CmmStatics CmmInfoTables (ListGraph CmmStmt)
71 type CmmDecl = GenCmmDecl CmmStatics CmmInfoTables (ListGraph CmmStmt)
72
73 -- | Cmm with the info tables converted to a list of 'CmmStatic' along with the info
74 -- table label. If we are building without tables-next-to-code there will be no statics
75 --
76 -- INVARIANT: if there is an info table, it has at least one CmmStatic
77 type RawCmmGroup = GenCmmGroup CmmStatics (BlockEnv CmmStatics) (ListGraph CmmStmt)
78 type RawCmmDecl = GenCmmDecl CmmStatics (BlockEnv CmmStatics) (ListGraph CmmStmt)
79
80
81 -- A basic block containing a single label, at the beginning.
82 -- The list of basic blocks in a top-level code block may be re-ordered.
83 -- Fall-through is not allowed: there must be an explicit jump at the
84 -- end of each basic block, but the code generator might rearrange basic
85 -- blocks in order to turn some jumps into fallthroughs.
86
87 data GenBasicBlock i = BasicBlock BlockId [i]
88 type CmmBasicBlock = GenBasicBlock CmmStmt
89
90 instance UserOfLocalRegs i => UserOfLocalRegs (GenBasicBlock i) where
91 foldRegsUsed f set (BasicBlock _ l) = foldRegsUsed f set l
92
93 -- | The branch block id is that of the first block in
94 -- the branch, which is that branch's entry point
95 blockId :: GenBasicBlock i -> BlockId
96 blockId (BasicBlock blk_id _ ) = blk_id
97
98 blockStmts :: GenBasicBlock i -> [i]
99 blockStmts (BasicBlock _ stmts) = stmts
100
101 mapBlockStmts :: (i -> i') -> GenBasicBlock i -> GenBasicBlock i'
102 mapBlockStmts f (BasicBlock id bs) = BasicBlock id (map f bs)
103
104 -- | Returns the info table associated with the CmmDecl's entry point,
105 -- if any.
106 topInfoTable :: GenCmmDecl a (BlockEnv i) (ListGraph b) -> Maybe i
107 topInfoTable (CmmProc infos _ (ListGraph (b:_)))
108 = mapLookup (blockId b) infos
109 topInfoTable _
110 = Nothing
111
112 ----------------------------------------------------------------
113 -- graph maps
114 ----------------------------------------------------------------
115
116 cmmMapGraph :: (g -> g') -> GenCmmGroup d h g -> GenCmmGroup d h g'
117 cmmMapGraph f tops = map (cmmTopMapGraph f) tops
118
119 cmmTopMapGraph :: (g -> g') -> GenCmmDecl d h g -> GenCmmDecl d h g'
120 cmmTopMapGraph f (CmmProc h l g) = CmmProc h l (f g)
121 cmmTopMapGraph _ (CmmData s ds) = CmmData s ds
122
123 -----------------------------------------------------------------------------
124 -- CmmStmt
125 -- A "statement". Note that all branches are explicit: there are no
126 -- control transfers to computed addresses, except when transfering
127 -- control to a new function.
128 -----------------------------------------------------------------------------
129
130 data CmmStmt
131 = CmmNop
132 | CmmComment FastString
133
134 | CmmAssign CmmReg CmmExpr -- Assign to register
135
136 | CmmStore CmmExpr CmmExpr -- Assign to memory location. Size is
137 -- given by cmmExprType of the rhs.
138
139 | CmmCall -- A call (foreign, native or primitive), with
140 CmmCallTarget
141 [HintedCmmFormal] -- zero or more results
142 [HintedCmmActual] -- zero or more arguments
143 New.CmmReturnInfo
144 -- Some care is necessary when handling the arguments of these, see
145 -- [Register parameter passing] and the hack in cmm/CmmOpt.hs
146
147 | CmmBranch BlockId -- branch to another BB in this fn
148
149 | CmmCondBranch CmmExpr BlockId -- conditional branch
150
151 | CmmSwitch -- Table branch
152 CmmExpr -- The scrutinee is zero-based;
153 [Maybe BlockId] -- zero -> first block
154 -- one -> second block etc
155 -- Undefined outside range, and when
156 -- there's a Nothing
157
158 | CmmJump -- Jump to another C-- function,
159 CmmExpr -- Target
160 (Maybe [GlobalReg]) -- Live registers at call site;
161 -- Nothing -> no information, assume
162 -- all live
163 -- Just .. -> info on liveness, []
164 -- means no live registers
165 -- This isn't all 'live' registers, just
166 -- the argument STG registers that are live
167 -- AND also possibly mapped to machine
168 -- registers. (So Sp, Hp, HpLim... ect
169 -- are never included here as they are
170 -- always live, only R2.., D1.. are
171 -- on this list)
172
173 | CmmReturn -- Return from a native C-- function,
174
175 data CmmHinted a
176 = CmmHinted {
177 hintlessCmm :: a,
178 cmmHint :: New.ForeignHint
179 }
180 deriving( Eq )
181
182 type HintedCmmFormal = CmmHinted CmmFormal
183 type HintedCmmActual = CmmHinted CmmActual
184
185 data CmmSafety
186 = CmmUnsafe
187 | CmmSafe C_SRT
188 | CmmInterruptible
189
190 -- | enable us to fold used registers over '[CmmActual]' and '[CmmFormal]'
191 instance UserOfLocalRegs CmmStmt where
192 foldRegsUsed f (set::b) s = stmt s set
193 where
194 stmt :: CmmStmt -> b -> b
195 stmt (CmmNop) = id
196 stmt (CmmComment {}) = id
197 stmt (CmmAssign _ e) = gen e
198 stmt (CmmStore e1 e2) = gen e1 . gen e2
199 stmt (CmmCall target _ es _) = gen target . gen es
200 stmt (CmmBranch _) = id
201 stmt (CmmCondBranch e _) = gen e
202 stmt (CmmSwitch e _) = gen e
203 stmt (CmmJump e _) = gen e
204 stmt (CmmReturn) = id
205
206 gen :: UserOfLocalRegs a => a -> b -> b
207 gen a set = foldRegsUsed f set a
208
209 instance UserOfLocalRegs CmmCallTarget where
210 foldRegsUsed f set (CmmCallee e _) = foldRegsUsed f set e
211 foldRegsUsed f set (CmmPrim _ mStmts) = foldRegsUsed f set mStmts
212
213 instance UserOfLocalRegs a => UserOfLocalRegs (CmmHinted a) where
214 foldRegsUsed f set a = foldRegsUsed f set (hintlessCmm a)
215
216 instance DefinerOfLocalRegs a => DefinerOfLocalRegs (CmmHinted a) where
217 foldRegsDefd f set a = foldRegsDefd f set (hintlessCmm a)
218
219 {-
220 Discussion
221 ~~~~~~~~~~
222
223 One possible problem with the above type is that the only way to do a
224 non-local conditional jump is to encode it as a branch to a block that
225 contains a single jump. This leads to inefficient code in the back end.
226
227 [N.B. This problem will go away when we make the transition to the
228 'zipper' form of control-flow graph, in which both targets of a
229 conditional jump are explicit. ---NR]
230
231 One possible way to fix this would be:
232
233 data CmmStat =
234 ...
235 | CmmJump CmmBranchDest
236 | CmmCondJump CmmExpr CmmBranchDest
237 ...
238
239 data CmmBranchDest
240 = Local BlockId
241 | NonLocal CmmExpr [LocalReg]
242
243 In favour:
244
245 + one fewer constructors in CmmStmt
246 + allows both cond branch and switch to jump to non-local destinations
247
248 Against:
249
250 - not strictly necessary: can already encode as branch+jump
251 - not always possible to implement any better in the back end
252 - could do the optimisation in the back end (but then plat-specific?)
253 - C-- doesn't have it
254 - back-end optimisation might be more general (jump shortcutting)
255
256 So we'll stick with the way it is, and add the optimisation to the NCG.
257 -}
258
259 -----------------------------------------------------------------------------
260 -- CmmCallTarget
261 --
262 -- The target of a CmmCall.
263 -----------------------------------------------------------------------------
264
265 data CmmCallTarget
266 = CmmCallee -- Call a function (foreign or native)
267 CmmExpr -- literal label <=> static call
268 -- other expression <=> dynamic call
269 CCallConv -- The calling convention
270
271 | CmmPrim -- Call a "primitive" (eg. sin, cos)
272 CallishMachOp -- These might be implemented as inline
273 -- code by the backend.
274 -- If we don't know how to implement the
275 -- mach op, then we can replace it with
276 -- this list of statements:
277 (Maybe [CmmStmt])
278