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