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.
69 | CmmData Section [d] -- constant values only
71 type CmmTop = GenCmmTop CmmStatic CmmStmt
73 -- A basic block containing a single label, at the beginning.
74 -- The list of basic blocks in a top-level code block may be re-ordered.
75 -- Fall-through is not allowed: there must be an explicit jump at the
76 -- end of each basic block, but the code generator might rearrange basic
77 -- blocks in order to turn some jumps into fallthroughs.
79 data GenBasicBlock i = BasicBlock BlockId [i]
80 -- ToDo: Julian suggests that we might need to annotate this type
81 -- with the out & in edges in the graph, i.e. two * [BlockId]. This
82 -- information can be derived from the contents, but it might be
83 -- helpful to cache it here.
85 type CmmBasicBlock = GenBasicBlock CmmStmt
87 blockId :: GenBasicBlock i -> BlockId
88 -- The branch block id is that of the first block in
89 -- the branch, which is that branch's entry point
90 blockId (BasicBlock blk_id _ ) = blk_id
92 blockStmts :: GenBasicBlock i -> [i]
93 blockStmts (BasicBlock _ stmts) = stmts
96 -----------------------------------------------------------------------------
98 -- A "statement". Note that all branches are explicit: there are no
99 -- control transfers to computed addresses, except when transfering
100 -- control to a new function.
101 -----------------------------------------------------------------------------
105 | CmmComment FastString
107 | CmmAssign CmmReg CmmExpr -- Assign to register
109 | CmmStore CmmExpr CmmExpr -- Assign to memory location. Size is
110 -- given by cmmExprRep of the rhs.
112 | CmmCall -- A foreign call, with
114 [(CmmReg,MachHint)] -- zero or more results
115 [(CmmExpr,MachHint)] -- zero or more arguments
116 (Maybe [GlobalReg]) -- Global regs that may need to be saved
117 -- if they will be clobbered by the call.
118 -- Nothing <=> save *all* globals that
119 -- might be clobbered.
121 | CmmBranch BlockId -- branch to another BB in this fn
123 | CmmCondBranch CmmExpr BlockId -- conditional branch
125 | CmmSwitch CmmExpr [Maybe BlockId] -- Table branch
126 -- The scrutinee is zero-based;
127 -- zero -> first block
128 -- one -> second block etc
129 -- Undefined outside range, and when there's a Nothing
131 | CmmJump CmmExpr [LocalReg] -- Jump to another function, with these
134 -----------------------------------------------------------------------------
137 -- The target of a CmmCall.
138 -----------------------------------------------------------------------------
141 = CmmForeignCall -- Call to a foreign function
142 CmmExpr -- literal label <=> static call
143 -- other expression <=> dynamic call
144 CCallConv -- The calling convention
146 | CmmPrim -- Call to a "primitive" (eg. sin, cos)
147 CallishMachOp -- These might be implemented as inline
148 -- code by the backend.
150 -----------------------------------------------------------------------------
152 -- An expression. Expressions have no side effects.
153 -----------------------------------------------------------------------------
156 = CmmLit CmmLit -- Literal
157 | CmmLoad CmmExpr MachRep -- Read memory location
158 | CmmReg CmmReg -- Contents of register
159 | CmmMachOp MachOp [CmmExpr] -- Machine operation (+, -, *, etc.)
160 | CmmRegOff CmmReg Int
162 -- ** is shorthand only, meaning **
163 -- CmmMachOp (MO_S_Add rep (CmmReg reg) (CmmLit (CmmInt i rep)))
164 -- where rep = cmmRegRep reg
165 | CmmPicBaseReg -- Base Register for PIC calculations
167 cmmExprRep :: CmmExpr -> MachRep
168 cmmExprRep (CmmLit lit) = cmmLitRep lit
169 cmmExprRep (CmmLoad _ rep) = rep
170 cmmExprRep (CmmReg reg) = cmmRegRep reg
171 cmmExprRep (CmmMachOp op _) = resultRepOfMachOp op
172 cmmExprRep (CmmRegOff reg _) = cmmRegRep reg
173 cmmExprRep CmmPicBaseReg = wordRep
177 | CmmGlobal GlobalReg
180 cmmRegRep :: CmmReg -> MachRep
181 cmmRegRep (CmmLocal reg) = localRegRep reg
182 cmmRegRep (CmmGlobal reg) = globalRegRep reg
185 = LocalReg !Unique MachRep
187 instance Eq LocalReg where
188 (LocalReg u1 _) == (LocalReg u2 _) = u1 == u2
190 instance Uniquable LocalReg where
191 getUnique (LocalReg uniq _) = uniq
193 localRegRep :: LocalReg -> MachRep
194 localRegRep (LocalReg _ rep) = rep
197 = CmmInt Integer MachRep
198 -- Interpretation: the 2's complement representation of the value
199 -- is truncated to the specified size. This is easier than trying
200 -- to keep the value within range, because we don't know whether
201 -- it will be used as a signed or unsigned value (the MachRep doesn't
202 -- distinguish between signed & unsigned).
203 | CmmFloat Rational MachRep
204 | CmmLabel CLabel -- Address of label
205 | CmmLabelOff CLabel Int -- Address of label + byte offset
207 -- Due to limitations in the C backend, the following
208 -- MUST ONLY be used inside the info table indicated by label2
209 -- (label2 must be the info label), and label1 must be an
210 -- SRT, a slow entrypoint or a large bitmap (see the Mangler)
211 -- Don't use it at all unless tablesNextToCode.
212 -- It is also used inside the NCG during when generating
213 -- position-independent code.
214 | CmmLabelDiffOff CLabel CLabel Int -- label1 - label2 + offset
216 cmmLitRep :: CmmLit -> MachRep
217 cmmLitRep (CmmInt _ rep) = rep
218 cmmLitRep (CmmFloat _ rep) = rep
219 cmmLitRep (CmmLabel _) = wordRep
220 cmmLitRep (CmmLabelOff _ _) = wordRep
221 cmmLitRep (CmmLabelDiffOff _ _ _) = wordRep
223 -----------------------------------------------------------------------------
226 -- Local labels must be unique within a single compilation unit.
228 newtype BlockId = BlockId Unique
231 instance Uniquable BlockId where
232 getUnique (BlockId u) = u
234 -----------------------------------------------------------------------------
236 -----------------------------------------------------------------------------
242 | RelocatableReadOnlyData
244 | OtherSection String
247 = CmmStaticLit CmmLit
248 -- a literal value, size given by cmmLitRep of the literal.
249 | CmmUninitialised Int
250 -- uninitialised data, N bytes long
252 -- align to next N-byte boundary (N must be a power of 2).
253 | CmmDataLabel CLabel
254 -- label the current position in this section.
256 -- string of 8-bit values only, not zero terminated.
257 -- ToDo: might be more honest to use [Word8] here?
259 -----------------------------------------------------------------------------
260 -- Global STG registers
261 -----------------------------------------------------------------------------
264 -- Argument and return registers
265 = VanillaReg -- pointers, unboxed ints and chars
266 {-# UNPACK #-} !Int -- its number
268 | FloatReg -- single-precision floating-point registers
269 {-# UNPACK #-} !Int -- its number
271 | DoubleReg -- double-precision floating-point registers
272 {-# UNPACK #-} !Int -- its number
274 | LongReg -- long int registers (64-bit, really)
275 {-# UNPACK #-} !Int -- its number
278 | Sp -- Stack ptr; points to last occupied stack location.
279 | SpLim -- Stack limit
280 | Hp -- Heap ptr; points to last occupied heap location.
281 | HpLim -- Heap limit register
282 | CurrentTSO -- pointer to current thread's TSO
283 | CurrentNursery -- pointer to allocation area
284 | HpAlloc -- allocation count for heap check failure
286 -- We keep the address of some commonly-called
287 -- functions in the register table, to keep code
289 | GCEnter1 -- stg_gc_enter_1
290 | GCFun -- stg_gc_fun
292 -- Base offset for the register table, used for accessing registers
293 -- which do not have real registers assigned to them. This register
294 -- will only appear after we have expanded GlobalReg into memory accesses
295 -- (where necessary) in the native code generator.
304 -- convenient aliases
305 spReg, hpReg, nodeReg :: CmmReg
308 nodeReg = CmmGlobal node
313 globalRegRep :: GlobalReg -> MachRep
314 globalRegRep (VanillaReg _) = wordRep
315 globalRegRep (FloatReg _) = F32
316 globalRegRep (DoubleReg _) = F64
317 globalRegRep (LongReg _) = I64
318 globalRegRep _ = wordRep