1 -----------------------------------------------------------------------------
5 -- (c) The University of Glasgow 2004-2006
7 -----------------------------------------------------------------------------
10 GenCmm(..), Cmm, RawCmm,
11 GenCmmTop(..), CmmTop, RawCmmTop,
13 cmmMapGraph, cmmTopMapGraph,
14 cmmMapGraphM, cmmTopMapGraphM,
15 CmmInfo(..), UpdateFrame(..),
16 CmmInfoTable(..), HasStaticClosure, ClosureTypeInfo(..), ConstrDescription,
17 ProfilingInfo(..), ClosureTypeTag,
18 GenBasicBlock(..), CmmBasicBlock, blockId, blockStmts, mapBlockStmts,
20 CmmStmt(..), CmmActual, CmmActuals, CmmFormal, CmmFormals,
21 HintedCmmFormal, HintedCmmFormals, HintedCmmActual, HintedCmmActuals,
23 CmmCallTarget(..), CallishMachOp(..), pprCallishMachOp,
24 ForeignHint(..), CmmHinted(..),
25 CmmStatic(..), Section(..),
29 #include "HsVersions.h"
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.
49 -----------------------------------------------------------------------------
50 -- Cmm, CmmTop, CmmBasicBlock
51 -----------------------------------------------------------------------------
53 -- A file is a list of top-level chunks. These may be arbitrarily
54 -- re-orderd during code generation.
56 -- GenCmm is abstracted over
57 -- d, the type of static data elements in CmmData
58 -- h, the static info preceding the code of a CmmProc
59 -- g, the control-flow graph of a CmmProc
61 -- We expect there to be two main instances of this type:
62 -- (a) C--, i.e. populated with various C-- constructs
63 -- (Cmm and RawCmm below)
64 -- (b) Native code, populated with data/instructions
66 -- A second family of instances based on ZipCfg is work in progress.
68 newtype GenCmm d h g = Cmm [GenCmmTop d h g]
70 -- | A top-level chunk, abstracted over the type of the contents of
71 -- the basic blocks (Cmm or instructions are the likely instantiations).
73 = CmmProc -- A procedure
74 h -- Extra header such as the info table
75 CLabel -- Used to generate both info & entry labels
76 CmmFormals -- Argument locals live on entry (C-- procedure params)
77 -- XXX Odd that there are no kinds, but there you are ---NR
78 g -- Control-flow graph for the procedure's code
80 | CmmData -- Static data
84 -- | A control-flow graph represented as a list of extended basic blocks.
85 newtype ListGraph i = ListGraph [GenBasicBlock i]
86 -- ^ Code, may be empty. The first block is the entry point. The
87 -- order is otherwise initially unimportant, but at some point the
88 -- code gen will fix the order.
90 -- BlockIds must be unique across an entire compilation unit, since
91 -- they are translated to assembly-language labels, which scope
92 -- across a whole compilation unit.
94 -- | Cmm with the info table as a data type
95 type Cmm = GenCmm CmmStatic CmmInfo (ListGraph CmmStmt)
96 type CmmTop = GenCmmTop CmmStatic CmmInfo (ListGraph CmmStmt)
98 -- | Cmm with the info tables converted to a list of 'CmmStatic'
99 type RawCmm = GenCmm CmmStatic [CmmStatic] (ListGraph CmmStmt)
100 type RawCmmTop = GenCmmTop CmmStatic [CmmStatic] (ListGraph CmmStmt)
103 -- A basic block containing a single label, at the beginning.
104 -- The list of basic blocks in a top-level code block may be re-ordered.
105 -- Fall-through is not allowed: there must be an explicit jump at the
106 -- end of each basic block, but the code generator might rearrange basic
107 -- blocks in order to turn some jumps into fallthroughs.
109 data GenBasicBlock i = BasicBlock BlockId [i]
110 type CmmBasicBlock = GenBasicBlock CmmStmt
112 instance UserOfLocalRegs i => UserOfLocalRegs (GenBasicBlock i) where
113 foldRegsUsed f set (BasicBlock _ l) = foldRegsUsed f set l
115 blockId :: GenBasicBlock i -> BlockId
116 -- The branch block id is that of the first block in
117 -- the branch, which is that branch's entry point
118 blockId (BasicBlock blk_id _ ) = blk_id
120 blockStmts :: GenBasicBlock i -> [i]
121 blockStmts (BasicBlock _ stmts) = stmts
124 mapBlockStmts :: (i -> i') -> GenBasicBlock i -> GenBasicBlock i'
125 mapBlockStmts f (BasicBlock id bs) = BasicBlock id (map f bs)
126 ----------------------------------------------------------------
128 ----------------------------------------------------------------
130 cmmMapGraph :: (g -> g') -> GenCmm d h g -> GenCmm d h g'
131 cmmTopMapGraph :: (g -> g') -> GenCmmTop d h g -> GenCmmTop d h g'
133 cmmMapGraphM :: Monad m => (String -> g -> m g') -> GenCmm d h g -> m (GenCmm d h g')
134 cmmTopMapGraphM :: Monad m => (String -> g -> m g') -> GenCmmTop d h g -> m (GenCmmTop d h g')
136 cmmMapGraph f (Cmm tops) = Cmm $ map (cmmTopMapGraph f) tops
137 cmmTopMapGraph f (CmmProc h l args g) = CmmProc h l args (f g)
138 cmmTopMapGraph _ (CmmData s ds) = CmmData s ds
140 cmmMapGraphM f (Cmm tops) = mapM (cmmTopMapGraphM f) tops >>= return . Cmm
141 cmmTopMapGraphM f (CmmProc h l args g) =
142 f (showSDoc $ ppr l) g >>= return . CmmProc h l args
143 cmmTopMapGraphM _ (CmmData s ds) = return $ CmmData s ds
145 -----------------------------------------------------------------------------
147 -----------------------------------------------------------------------------
151 (Maybe BlockId) -- GC target. Nothing <=> CPS won't do stack check
152 -- JD: NOT USED BY NEW CODE GEN
153 (Maybe UpdateFrame) -- Update frame
154 CmmInfoTable -- Info table
156 -- Info table as a haskell data type
161 ClosureTypeTag -- Int
163 | CmmNonInfoTable -- Procedure doesn't need an info table
165 type HasStaticClosure = Bool
167 -- TODO: The GC target shouldn't really be part of CmmInfo
168 -- as it doesn't appear in the resulting info table.
169 -- It should be factored out.
172 = ConstrInfo ClosureLayout ConstrTag ConstrDescription
173 | FunInfo ClosureLayout C_SRT FunArity ArgDescr SlowEntry
174 | ThunkInfo ClosureLayout C_SRT
175 | ThunkSelectorInfo SelectorOffset C_SRT
177 [Maybe LocalReg] -- Stack layout: Just x, an item x
178 -- Nothing: a 1-word gap
179 -- Start of list is the *young* end
182 data CmmReturnInfo = CmmMayReturn
185 -- TODO: These types may need refinement
186 data ProfilingInfo = ProfilingInfo CmmLit CmmLit -- closure_type, closure_desc
187 type ClosureTypeTag = StgHalfWord
188 type ClosureLayout = (StgHalfWord, StgHalfWord) -- ptrs, nptrs
189 type ConstrTag = StgHalfWord
190 type ConstrDescription = CmmLit
191 type FunArity = StgHalfWord
192 type SlowEntry = CmmLit
193 -- We would like this to be a CLabel but
194 -- for now the parser sets this to zero on an INFO_TABLE_FUN.
195 type SelectorOffset = StgWord
197 -- | A frame that is to be pushed before entry to the function.
198 -- Used to handle 'update' frames.
201 CmmExpr -- Frame header. Behaves like the target of a 'jump'.
202 [CmmExpr] -- Frame remainder. Behaves like the arguments of a 'jump'.
204 -----------------------------------------------------------------------------
206 -- A "statement". Note that all branches are explicit: there are no
207 -- control transfers to computed addresses, except when transfering
208 -- control to a new function.
209 -----------------------------------------------------------------------------
211 data CmmStmt -- Old-style
213 | CmmComment FastString
215 | CmmAssign CmmReg CmmExpr -- Assign to register
217 | CmmStore CmmExpr CmmExpr -- Assign to memory location. Size is
218 -- given by cmmExprType of the rhs.
220 | CmmCall -- A call (forign, native or primitive), with
222 HintedCmmFormals -- zero or more results
223 HintedCmmActuals -- zero or more arguments
224 CmmSafety -- whether to build a continuation
227 | CmmBranch BlockId -- branch to another BB in this fn
229 | CmmCondBranch CmmExpr BlockId -- conditional branch
231 | CmmSwitch CmmExpr [Maybe BlockId] -- Table branch
232 -- The scrutinee is zero-based;
233 -- zero -> first block
234 -- one -> second block etc
235 -- Undefined outside range, and when there's a Nothing
237 | CmmJump CmmExpr -- Jump to another C-- function,
238 HintedCmmActuals -- with these parameters. (parameters never used)
240 | CmmReturn -- Return from a native C-- function,
241 HintedCmmActuals -- with these return values. (parameters never used)
243 type CmmActual = CmmExpr
244 type CmmFormal = LocalReg
245 type CmmActuals = [CmmActual]
246 type CmmFormals = [CmmFormal]
248 data CmmHinted a = CmmHinted { hintlessCmm :: a, cmmHint :: ForeignHint }
251 type HintedCmmActuals = [HintedCmmActual]
252 type HintedCmmFormals = [HintedCmmFormal]
253 type HintedCmmFormal = CmmHinted CmmFormal
254 type HintedCmmActual = CmmHinted CmmActual
256 data CmmSafety = CmmUnsafe | CmmSafe C_SRT
258 -- | enable us to fold used registers over 'CmmActuals' and 'CmmFormals'
259 instance UserOfLocalRegs CmmStmt where
260 foldRegsUsed f set s = stmt s set
261 where stmt (CmmNop) = id
262 stmt (CmmComment {}) = id
263 stmt (CmmAssign _ e) = gen e
264 stmt (CmmStore e1 e2) = gen e1 . gen e2
265 stmt (CmmCall target _ es _ _) = gen target . gen es
266 stmt (CmmBranch _) = id
267 stmt (CmmCondBranch e _) = gen e
268 stmt (CmmSwitch e _) = gen e
269 stmt (CmmJump e es) = gen e . gen es
270 stmt (CmmReturn es) = gen es
271 gen a set = foldRegsUsed f set a
273 instance UserOfLocalRegs CmmCallTarget where
274 foldRegsUsed f set (CmmCallee e _) = foldRegsUsed f set e
275 foldRegsUsed _ set (CmmPrim {}) = set
277 instance UserOfSlots CmmCallTarget where
278 foldSlotsUsed f set (CmmCallee e _) = foldSlotsUsed f set e
279 foldSlotsUsed _ set (CmmPrim {}) = set
281 instance UserOfLocalRegs a => UserOfLocalRegs (CmmHinted a) where
282 foldRegsUsed f set a = foldRegsUsed f set (hintlessCmm a)
284 instance UserOfSlots a => UserOfSlots (CmmHinted a) where
285 foldSlotsUsed f set a = foldSlotsUsed f set (hintlessCmm a)
287 instance DefinerOfLocalRegs a => DefinerOfLocalRegs (CmmHinted a) where
288 foldRegsDefd f set a = foldRegsDefd f set (hintlessCmm a)
294 One possible problem with the above type is that the only way to do a
295 non-local conditional jump is to encode it as a branch to a block that
296 contains a single jump. This leads to inefficient code in the back end.
298 [N.B. This problem will go away when we make the transition to the
299 'zipper' form of control-flow graph, in which both targets of a
300 conditional jump are explicit. ---NR]
302 One possible way to fix this would be:
306 | CmmJump CmmBranchDest
307 | CmmCondJump CmmExpr CmmBranchDest
312 | NonLocal CmmExpr [LocalReg]
316 + one fewer constructors in CmmStmt
317 + allows both cond branch and switch to jump to non-local destinations
321 - not strictly necessary: can already encode as branch+jump
322 - not always possible to implement any better in the back end
323 - could do the optimisation in the back end (but then plat-specific?)
324 - C-- doesn't have it
325 - back-end optimisation might be more general (jump shortcutting)
327 So we'll stick with the way it is, and add the optimisation to the NCG.
330 -----------------------------------------------------------------------------
333 -- The target of a CmmCall.
334 -----------------------------------------------------------------------------
337 = CmmCallee -- Call a function (foreign or native)
338 CmmExpr -- literal label <=> static call
339 -- other expression <=> dynamic call
340 CCallConv -- The calling convention
342 | CmmPrim -- Call a "primitive" (eg. sin, cos)
343 CallishMachOp -- These might be implemented as inline
344 -- code by the backend.
349 = NoHint | AddrHint | SignedHint
351 -- Used to give extra per-argument or per-result
352 -- information needed by foreign calling conventions
355 -- CallishMachOps tend to be implemented by foreign calls in some backends,
356 -- so we separate them out. In Cmm, these can only occur in a
357 -- statement position, in contrast to an ordinary MachOp which can occur
358 -- anywhere in an expression.
387 | MO_Touch -- Keep variables live (when using interior pointers)
390 pprCallishMachOp :: CallishMachOp -> SDoc
391 pprCallishMachOp mo = text (show mo)
393 -----------------------------------------------------------------------------
395 -----------------------------------------------------------------------------
401 | RelocatableReadOnlyData
403 | ReadOnlyData16 -- .rodata.cst16 on x86_64, 16-byte aligned
404 | OtherSection String
407 = CmmStaticLit CmmLit
408 -- a literal value, size given by cmmLitRep of the literal.
409 | CmmUninitialised Int
410 -- uninitialised data, N bytes long
412 -- align to next N-byte boundary (N must be a power of 2).
413 | CmmDataLabel CLabel
414 -- label the current position in this section.
416 -- string of 8-bit values only, not zero terminated.