1 -----------------------------------------------------------------------------
5 -- (c) The University of Glasgow 2004-2006
7 -----------------------------------------------------------------------------
11 GenCmmTop(..), CmmTop,
12 GenBasicBlock(..), CmmBasicBlock, blockId, blockStmts,
13 CmmStmt(..), CmmActuals, CmmFormals,
15 CmmStatic(..), Section(..),
16 CmmExpr(..), cmmExprRep,
17 CmmReg(..), cmmRegRep,
18 CmmLit(..), cmmLitRep,
19 LocalReg(..), localRegRep,
20 BlockId(..), BlockEnv,
21 GlobalReg(..), globalRegRep,
23 node, nodeReg, spReg, hpReg, spLimReg
26 #include "HsVersions.h"
37 -----------------------------------------------------------------------------
38 -- Cmm, CmmTop, CmmBasicBlock
39 -----------------------------------------------------------------------------
41 -- A file is a list of top-level chunks. These may be arbitrarily
42 -- re-orderd during code generation.
44 -- GenCmm is abstracted over
45 -- (a) the type of static data elements
46 -- (b) the contents of a basic block.
47 -- We expect there to be two main instances of this type:
48 -- (a) Plain C--, i.e. populated with CmmLit and CmmExpr respectively,
49 -- (b) Native code, populated with instructions
51 newtype GenCmm d i = Cmm [GenCmmTop d i]
53 type Cmm = GenCmm CmmStatic CmmStmt
55 -- A top-level chunk, abstracted over the type of the contents of
56 -- the basic blocks (Cmm or instructions are the likely instantiations).
59 [d] -- Info table, may be empty
60 CLabel -- Used to generate both info & entry labels
61 CmmFormals -- Argument locals live on entry (C-- procedure params)
62 [GenBasicBlock i] -- Code, may be empty. The first block is
63 -- the entry point. The order is otherwise initially
64 -- unimportant, but at some point the code gen will
67 -- the BlockId of the first block does not give rise
68 -- to a label. To jump to the first block in a Proc,
69 -- use the appropriate CLabel.
72 | CmmData Section [d] -- constant values only
74 type CmmTop = GenCmmTop CmmStatic CmmStmt
76 -- A basic block containing a single label, at the beginning.
77 -- The list of basic blocks in a top-level code block may be re-ordered.
78 -- Fall-through is not allowed: there must be an explicit jump at the
79 -- end of each basic block, but the code generator might rearrange basic
80 -- blocks in order to turn some jumps into fallthroughs.
82 data GenBasicBlock i = BasicBlock BlockId [i]
83 -- ToDo: Julian suggests that we might need to annotate this type
84 -- with the out & in edges in the graph, i.e. two * [BlockId]. This
85 -- information can be derived from the contents, but it might be
86 -- helpful to cache it here.
88 type CmmBasicBlock = GenBasicBlock CmmStmt
90 blockId :: GenBasicBlock i -> BlockId
91 -- The branch block id is that of the first block in
92 -- the branch, which is that branch's entry point
93 blockId (BasicBlock blk_id _ ) = blk_id
95 blockStmts :: GenBasicBlock i -> [i]
96 blockStmts (BasicBlock _ stmts) = stmts
99 -----------------------------------------------------------------------------
101 -- A "statement". Note that all branches are explicit: there are no
102 -- control transfers to computed addresses, except when transfering
103 -- control to a new function.
104 -----------------------------------------------------------------------------
108 | CmmComment FastString
110 | CmmAssign CmmReg CmmExpr -- Assign to register
112 | CmmStore CmmExpr CmmExpr -- Assign to memory location. Size is
113 -- given by cmmExprRep of the rhs.
115 | CmmCall -- A foreign call, with
117 CmmFormals -- zero or more results
118 CmmActuals -- zero or more arguments
120 | CmmBranch BlockId -- branch to another BB in this fn
122 | CmmCondBranch CmmExpr BlockId -- conditional branch
124 | CmmSwitch CmmExpr [Maybe BlockId] -- Table branch
125 -- The scrutinee is zero-based;
126 -- zero -> first block
127 -- one -> second block etc
128 -- Undefined outside range, and when there's a Nothing
130 | CmmJump CmmExpr -- Jump to another function,
131 CmmActuals -- with these parameters.
133 | CmmReturn -- Return from a function,
134 CmmActuals -- with these return values.
136 type CmmActuals = [(CmmExpr,MachHint)]
137 type CmmFormals = [(CmmReg,MachHint)]
143 One possible problem with the above type is that the only way to do a
144 non-local conditional jump is to encode it as a branch to a block that
145 contains a single jump. This leads to inefficient code in the back end.
147 One possible way to fix this would be:
151 | CmmJump CmmBranchDest
152 | CmmCondJump CmmExpr CmmBranchDest
157 | NonLocal CmmExpr [LocalReg]
161 + one fewer constructors in CmmStmt
162 + allows both cond branch and switch to jump to non-local destinations
166 - not strictly necessary: can already encode as branch+jump
167 - not always possible to implement any better in the back end
168 - could do the optimisation in the back end (but then plat-specific?)
169 - C-- doesn't have it
170 - back-end optimisation might be more general (jump shortcutting)
172 So we'll stick with the way it is, and add the optimisation to the NCG.
175 -----------------------------------------------------------------------------
178 -- The target of a CmmCall.
179 -----------------------------------------------------------------------------
182 = CmmForeignCall -- Call to a foreign function
183 CmmExpr -- literal label <=> static call
184 -- other expression <=> dynamic call
185 CCallConv -- The calling convention
187 | CmmPrim -- Call to a "primitive" (eg. sin, cos)
188 CallishMachOp -- These might be implemented as inline
189 -- code by the backend.
191 -----------------------------------------------------------------------------
193 -- An expression. Expressions have no side effects.
194 -----------------------------------------------------------------------------
197 = CmmLit CmmLit -- Literal
198 | CmmLoad CmmExpr MachRep -- Read memory location
199 | CmmReg CmmReg -- Contents of register
200 | CmmMachOp MachOp [CmmExpr] -- Machine operation (+, -, *, etc.)
201 | CmmRegOff CmmReg Int
203 -- ** is shorthand only, meaning **
204 -- CmmMachOp (MO_S_Add rep (CmmReg reg) (CmmLit (CmmInt i rep)))
205 -- where rep = cmmRegRep reg
208 cmmExprRep :: CmmExpr -> MachRep
209 cmmExprRep (CmmLit lit) = cmmLitRep lit
210 cmmExprRep (CmmLoad _ rep) = rep
211 cmmExprRep (CmmReg reg) = cmmRegRep reg
212 cmmExprRep (CmmMachOp op _) = resultRepOfMachOp op
213 cmmExprRep (CmmRegOff reg _) = cmmRegRep reg
217 | CmmGlobal GlobalReg
220 cmmRegRep :: CmmReg -> MachRep
221 cmmRegRep (CmmLocal reg) = localRegRep reg
222 cmmRegRep (CmmGlobal reg) = globalRegRep reg
225 = LocalReg !Unique MachRep
227 instance Eq LocalReg where
228 (LocalReg u1 _) == (LocalReg u2 _) = u1 == u2
230 instance Uniquable LocalReg where
231 getUnique (LocalReg uniq _) = uniq
233 localRegRep :: LocalReg -> MachRep
234 localRegRep (LocalReg _ rep) = rep
237 = CmmInt Integer MachRep
238 -- Interpretation: the 2's complement representation of the value
239 -- is truncated to the specified size. This is easier than trying
240 -- to keep the value within range, because we don't know whether
241 -- it will be used as a signed or unsigned value (the MachRep doesn't
242 -- distinguish between signed & unsigned).
243 | CmmFloat Rational MachRep
244 | CmmLabel CLabel -- Address of label
245 | CmmLabelOff CLabel Int -- Address of label + byte offset
247 -- Due to limitations in the C backend, the following
248 -- MUST ONLY be used inside the info table indicated by label2
249 -- (label2 must be the info label), and label1 must be an
250 -- SRT, a slow entrypoint or a large bitmap (see the Mangler)
251 -- Don't use it at all unless tablesNextToCode.
252 -- It is also used inside the NCG during when generating
253 -- position-independent code.
254 | CmmLabelDiffOff CLabel CLabel Int -- label1 - label2 + offset
257 cmmLitRep :: CmmLit -> MachRep
258 cmmLitRep (CmmInt _ rep) = rep
259 cmmLitRep (CmmFloat _ rep) = rep
260 cmmLitRep (CmmLabel _) = wordRep
261 cmmLitRep (CmmLabelOff _ _) = wordRep
262 cmmLitRep (CmmLabelDiffOff _ _ _) = wordRep
264 -----------------------------------------------------------------------------
267 -- Local labels must be unique within a single compilation unit.
269 newtype BlockId = BlockId Unique
272 instance Uniquable BlockId where
273 getUnique (BlockId u) = u
275 type BlockEnv a = UniqFM {- BlockId -} a
277 -----------------------------------------------------------------------------
279 -----------------------------------------------------------------------------
285 | RelocatableReadOnlyData
287 | ReadOnlyData16 -- .rodata.cst16 on x86_64, 16-byte aligned
288 | OtherSection String
291 = CmmStaticLit CmmLit
292 -- a literal value, size given by cmmLitRep of the literal.
293 | CmmUninitialised Int
294 -- uninitialised data, N bytes long
296 -- align to next N-byte boundary (N must be a power of 2).
297 | CmmDataLabel CLabel
298 -- label the current position in this section.
300 -- string of 8-bit values only, not zero terminated.
302 -----------------------------------------------------------------------------
303 -- Global STG registers
304 -----------------------------------------------------------------------------
307 -- Argument and return registers
308 = VanillaReg -- pointers, unboxed ints and chars
309 {-# UNPACK #-} !Int -- its number
311 | FloatReg -- single-precision floating-point registers
312 {-# UNPACK #-} !Int -- its number
314 | DoubleReg -- double-precision floating-point registers
315 {-# UNPACK #-} !Int -- its number
317 | LongReg -- long int registers (64-bit, really)
318 {-# UNPACK #-} !Int -- its number
321 | Sp -- Stack ptr; points to last occupied stack location.
322 | SpLim -- Stack limit
323 | Hp -- Heap ptr; points to last occupied heap location.
324 | HpLim -- Heap limit register
325 | CurrentTSO -- pointer to current thread's TSO
326 | CurrentNursery -- pointer to allocation area
327 | HpAlloc -- allocation count for heap check failure
329 -- We keep the address of some commonly-called
330 -- functions in the register table, to keep code
332 | GCEnter1 -- stg_gc_enter_1
333 | GCFun -- stg_gc_fun
335 -- Base offset for the register table, used for accessing registers
336 -- which do not have real registers assigned to them. This register
337 -- will only appear after we have expanded GlobalReg into memory accesses
338 -- (where necessary) in the native code generator.
341 -- Base Register for PIC (position-independent code) calculations
342 -- Only used inside the native code generator. It's exact meaning differs
343 -- from platform to platform (see module PositionIndependentCode).
352 -- convenient aliases
353 spReg, hpReg, spLimReg, nodeReg :: CmmReg
356 spLimReg = CmmGlobal SpLim
357 nodeReg = CmmGlobal node
362 globalRegRep :: GlobalReg -> MachRep
363 globalRegRep (VanillaReg _) = wordRep
364 globalRegRep (FloatReg _) = F32
365 globalRegRep (DoubleReg _) = F64
366 globalRegRep (LongReg _) = I64
367 globalRegRep _ = wordRep