1 -----------------------------------------------------------------------------
5 -- (c) The University of Glasgow 2004
7 -----------------------------------------------------------------------------
11 GenCmmTop(..), CmmTop,
12 GenBasicBlock(..), CmmBasicBlock, blockId, blockStmts,
15 CmmStatic(..), Section(..),
16 CmmExpr(..), cmmExprRep,
17 CmmReg(..), cmmRegRep,
18 CmmLit(..), cmmLitRep,
19 LocalReg(..), localRegRep,
21 GlobalReg(..), globalRegRep,
23 node, nodeReg, spReg, hpReg,
26 #include "HsVersions.h"
29 import CLabel ( CLabel )
30 import ForeignCall ( CCallConv )
31 import Unique ( Unique, Uniquable(..) )
32 import FastString ( FastString )
34 -----------------------------------------------------------------------------
35 -- Cmm, CmmTop, CmmBasicBlock
36 -----------------------------------------------------------------------------
38 -- A file is a list of top-level chunks. These may be arbitrarily
39 -- re-orderd during code generation.
41 -- GenCmm is abstracted over
42 -- (a) the type of static data elements
43 -- (b) the contents of a basic block.
44 -- We expect there to be two main instances of this type:
45 -- (a) Plain C--, i.e. populated with CmmLit and CmmExpr respectively,
46 -- (b) Native code, populated with instructions
48 newtype GenCmm d i = Cmm [GenCmmTop d i]
50 type Cmm = GenCmm CmmStatic CmmStmt
52 -- A top-level chunk, abstracted over the type of the contents of
53 -- the basic blocks (Cmm or instructions are the likely instantiations).
56 [d] -- Info table, may be empty
57 CLabel -- Used to generate both info & entry labels
58 [LocalReg] -- Argument locals live on entry (C-- procedure params)
59 [GenBasicBlock i] -- Code, may be empty. The first block is
60 -- the entry point. The order is otherwise initially
61 -- unimportant, but at some point the code gen will
64 -- the BlockId of the first block does not give rise
65 -- to a label. To jump to the first block in a Proc,
66 -- use the appropriate CLabel.
68 [GenCmmCont i] -- Continuations declared in this proc
71 | CmmData Section [d] -- constant values only
73 type CmmTop = GenCmmTop CmmStatic CmmStmt
75 -- A basic block containing a single label, at the beginning.
76 -- The list of basic blocks in a top-level code block may be re-ordered.
77 -- Fall-through is not allowed: there must be an explicit jump at the
78 -- end of each basic block, but the code generator might rearrange basic
79 -- blocks in order to turn some jumps into fallthroughs.
81 data GenBasicBlock i = BasicBlock BlockId [i]
82 -- ToDo: Julian suggests that we might need to annotate this type
83 -- with the out & in edges in the graph, i.e. two * [BlockId]. This
84 -- information can be derived from the contents, but it might be
85 -- helpful to cache it here.
87 type CmmBasicBlock = GenBasicBlock CmmStmt
89 blockId :: GenBasicBlock i -> BlockId
90 -- The branch block id is that of the first block in
91 -- the branch, which is that branch's entry point
92 blockId (BasicBlock blk_id _ ) = blk_id
94 blockStmts :: GenBasicBlock i -> [i]
95 blockStmts (BasicBlock _ stmts) = stmts
100 [LocalReg] -- The formal params of the ccntinuation
103 type ContId = BlockId -- Continuation is named (in eg
104 -- also-unwinds-to annotations) via the
105 -- block-id of its GenBasicBlock
107 -----------------------------------------------------------------------------
109 -- A "statement". Note that all branches are explicit: there are no
110 -- control transfers to computed addresses, except when transfering
111 -- control to a new function.
112 -----------------------------------------------------------------------------
116 | CmmComment FastString
118 | CmmAssign CmmReg CmmExpr -- Assign to register
120 | CmmStore CmmExpr CmmExpr -- Assign to memory location. Size is
121 -- given by cmmExprRep of the rhs.
123 | CmmCall -- A foreign call, with
125 [(CmmReg,MachHint)] -- zero or more results
126 [(CmmExpr,MachHint)] -- zero or more arguments
129 | CmmBranch BlockId -- branch to another BB in this fn
131 | CmmCondBranch CmmExpr BlockId -- conditional branch
133 | CmmSwitch CmmExpr [Maybe BlockId] -- Table branch
134 -- The scrutinee is zero-based;
135 -- zero -> first block
136 -- one -> second block etc
137 -- Undefined outside range, and when there's a Nothing
139 | CmmJump CmmExpr [LocalReg] -- Jump to another function, with these
142 | CmmSpan CmmSpan [CmmStmt]
144 -----------------------------------------------------------------------------
146 -----------------------------------------------------------------------------
148 data CmmSpan = UpdateFrame | CatchFrame -- Etc.
150 -----------------------------------------------------------------------------
153 -- The target of a CmmCall.
154 -----------------------------------------------------------------------------
157 = CmmVanillaCall CmmExpr -- Ordinarly call to a Cmm procedure
159 | CmmForeignCall -- Call to a foreign function
160 CmmExpr -- literal label <=> static call
161 -- other expression <=> dynamic call
162 CCallConv -- The calling convention
164 | CmmPrim -- Call to a "primitive" (eg. sin, cos)
165 CallishMachOp -- These might be implemented as inline
166 -- code by the backend.
168 -----------------------------------------------------------------------------
171 -----------------------------------------------------------------------------
174 = Flow { normal_live :: FlowEdge ()
175 , also_unwinds :: [FlowEdge ContId]
179 = FlowEdge { edge_target :: a
180 , live_locals :: [LocalReg] -- Live across the call
181 , save_globals :: Maybe [GlobalReg] -- Global regs that may need to be saved
182 } -- if they will be clobbered by the call.
183 -- Nothing <=> save *all* globals that
184 -- might be clobbered
186 -----------------------------------------------------------------------------
188 -- An expression. Expressions have no side effects.
189 -----------------------------------------------------------------------------
192 = CmmLit CmmLit -- Literal
193 | CmmLoad CmmExpr MachRep -- Read memory location
194 | CmmReg CmmReg -- Contents of register
195 | CmmMachOp MachOp [CmmExpr] -- Machine operation (+, -, *, etc.)
196 | CmmRegOff CmmReg Int
198 -- ** is shorthand only, meaning **
199 -- CmmMachOp (MO_S_Add rep (CmmReg reg) (CmmLit (CmmInt i rep)))
200 -- where rep = cmmRegRep reg
201 | CmmPicBaseReg -- Base Register for PIC calculations
203 cmmExprRep :: CmmExpr -> MachRep
204 cmmExprRep (CmmLit lit) = cmmLitRep lit
205 cmmExprRep (CmmLoad _ rep) = rep
206 cmmExprRep (CmmReg reg) = cmmRegRep reg
207 cmmExprRep (CmmMachOp op _) = resultRepOfMachOp op
208 cmmExprRep (CmmRegOff reg _) = cmmRegRep reg
209 cmmExprRep CmmPicBaseReg = wordRep
213 | CmmGlobal GlobalReg
216 cmmRegRep :: CmmReg -> MachRep
217 cmmRegRep (CmmLocal reg) = localRegRep reg
218 cmmRegRep (CmmGlobal reg) = globalRegRep reg
221 = LocalReg !Unique MachRep
223 instance Eq LocalReg where
224 (LocalReg u1 _) == (LocalReg u2 _) = u1 == u2
226 instance Uniquable LocalReg where
227 getUnique (LocalReg uniq _) = uniq
229 localRegRep :: LocalReg -> MachRep
230 localRegRep (LocalReg _ rep) = rep
233 = CmmInt Integer MachRep
234 -- Interpretation: the 2's complement representation of the value
235 -- is truncated to the specified size. This is easier than trying
236 -- to keep the value within range, because we don't know whether
237 -- it will be used as a signed or unsigned value (the MachRep doesn't
238 -- distinguish between signed & unsigned).
239 | CmmFloat Rational MachRep
240 | CmmLabel CLabel -- Address of label
241 | CmmLabelOff CLabel Int -- Address of label + byte offset
243 -- Due to limitations in the C backend, the following
244 -- MUST ONLY be used inside the info table indicated by label2
245 -- (label2 must be the info label), and label1 must be an
246 -- SRT, a slow entrypoint or a large bitmap (see the Mangler)
247 -- Don't use it at all unless tablesNextToCode.
248 -- It is also used inside the NCG during when generating
249 -- position-independent code.
250 | CmmLabelDiffOff CLabel CLabel Int -- label1 - label2 + offset
252 cmmLitRep :: CmmLit -> MachRep
253 cmmLitRep (CmmInt _ rep) = rep
254 cmmLitRep (CmmFloat _ rep) = rep
255 cmmLitRep (CmmLabel _) = wordRep
256 cmmLitRep (CmmLabelOff _ _) = wordRep
257 cmmLitRep (CmmLabelDiffOff _ _ _) = wordRep
259 -----------------------------------------------------------------------------
262 -- Local labels must be unique within a single compilation unit.
264 newtype BlockId = BlockId Unique
267 instance Uniquable BlockId where
268 getUnique (BlockId u) = u
270 -----------------------------------------------------------------------------
272 -----------------------------------------------------------------------------
279 | OtherSection String
282 = CmmStaticLit CmmLit
283 -- a literal value, size given by cmmLitRep of the literal.
284 | CmmUninitialised Int
285 -- uninitialised data, N bytes long
287 -- align to next N-byte boundary (N must be a power of 2).
288 | CmmDataLabel CLabel
289 -- label the current position in this section.
291 -- string of 8-bit values only, not zero terminated.
292 -- ToDo: might be more honest to use [Word8] here?
294 -----------------------------------------------------------------------------
295 -- Global STG registers
296 -----------------------------------------------------------------------------
299 -- Argument and return registers
300 = VanillaReg -- pointers, unboxed ints and chars
301 {-# UNPACK #-} !Int -- its number
303 | FloatReg -- single-precision floating-point registers
304 {-# UNPACK #-} !Int -- its number
306 | DoubleReg -- double-precision floating-point registers
307 {-# UNPACK #-} !Int -- its number
309 | LongReg -- long int registers (64-bit, really)
310 {-# UNPACK #-} !Int -- its number
313 | Sp -- Stack ptr; points to last occupied stack location.
314 | SpLim -- Stack limit
315 | Hp -- Heap ptr; points to last occupied heap location.
316 | HpLim -- Heap limit register
317 | CurrentTSO -- pointer to current thread's TSO
318 | CurrentNursery -- pointer to allocation area
319 | HpAlloc -- allocation count for heap check failure
321 -- We keep the address of some commonly-called
322 -- functions in the register table, to keep code
324 | GCEnter1 -- stg_gc_enter_1
325 | GCFun -- stg_gc_fun
327 -- Base offset for the register table, used for accessing registers
328 -- which do not have real registers assigned to them. This register
329 -- will only appear after we have expanded GlobalReg into memory accesses
330 -- (where necessary) in the native code generator.
339 -- convenient aliases
340 spReg, hpReg, nodeReg :: CmmReg
343 nodeReg = CmmGlobal node
348 globalRegRep :: GlobalReg -> MachRep
349 globalRegRep (VanillaReg _) = wordRep
350 globalRegRep (FloatReg _) = F32
351 globalRegRep (DoubleReg _) = F64
352 globalRegRep (LongReg _) = I64
353 globalRegRep _ = wordRep