Module header tidyup, phase 1
[ghc-hetmet.git] / compiler / cmm / Cmm.hs
1 -----------------------------------------------------------------------------
2 --
3 -- Cmm data types
4 --
5 -- (c) The University of Glasgow 2004-2006
6 --
7 -----------------------------------------------------------------------------
8
9 module Cmm ( 
10         GenCmm(..), Cmm,
11         GenCmmTop(..), CmmTop,
12         GenBasicBlock(..), CmmBasicBlock, blockId, blockStmts,
13         CmmStmt(..),  
14         CmmCallTarget(..),
15         CmmStatic(..), Section(..),
16         CmmExpr(..), cmmExprRep, 
17         CmmReg(..), cmmRegRep,
18         CmmLit(..), cmmLitRep,
19         LocalReg(..), localRegRep,
20         BlockId(..),
21         GlobalReg(..), globalRegRep,
22
23         node, nodeReg, spReg, hpReg,
24   ) where
25
26 #include "HsVersions.h"
27
28 import MachOp
29 import CLabel
30 import ForeignCall
31 import Unique
32 import FastString
33
34 import Data.Word
35
36 -----------------------------------------------------------------------------
37 --              Cmm, CmmTop, CmmBasicBlock
38 -----------------------------------------------------------------------------
39
40 -- A file is a list of top-level chunks.  These may be arbitrarily
41 -- re-orderd during code generation.
42
43 -- GenCmm is abstracted over
44 --   (a) the type of static data elements
45 --   (b) the contents of a basic block.
46 -- We expect there to be two main instances of this type:
47 --   (a) Plain C--, i.e. populated with CmmLit and CmmExpr respectively,
48 --   (b) Native code, populated with instructions
49 --
50 newtype GenCmm d i = Cmm [GenCmmTop d i]
51
52 type Cmm = GenCmm CmmStatic CmmStmt
53
54 -- A top-level chunk, abstracted over the type of the contents of
55 -- the basic blocks (Cmm or instructions are the likely instantiations).
56 data GenCmmTop d i
57   = CmmProc
58      [d]               -- Info table, may be empty
59      CLabel            -- Used to generate both info & entry labels
60      [LocalReg]        -- Argument locals live on entry (C-- procedure params)
61      [GenBasicBlock i] -- Code, may be empty.  The first block is
62                        -- the entry point.  The order is otherwise initially 
63                        -- unimportant, but at some point the code gen will
64                        -- fix the order.
65
66                        -- the BlockId of the first block does not give rise
67                        -- to a label.  To jump to the first block in a Proc,
68                        -- use the appropriate CLabel.
69
70   -- some static data.
71   | CmmData Section [d] -- constant values only
72
73 type CmmTop = GenCmmTop CmmStatic CmmStmt
74
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.
80
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.
86
87 type CmmBasicBlock = GenBasicBlock CmmStmt
88
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
93
94 blockStmts :: GenBasicBlock i -> [i]
95 blockStmts (BasicBlock _ stmts) = stmts
96
97
98 -----------------------------------------------------------------------------
99 --              CmmStmt
100 -- A "statement".  Note that all branches are explicit: there are no
101 -- control transfers to computed addresses, except when transfering
102 -- control to a new function.
103 -----------------------------------------------------------------------------
104
105 data CmmStmt
106   = CmmNop
107   | CmmComment FastString
108
109   | CmmAssign CmmReg CmmExpr     -- Assign to register
110
111   | CmmStore CmmExpr CmmExpr     -- Assign to memory location.  Size is
112                                  -- given by cmmExprRep of the rhs.
113
114   | CmmCall                      -- A foreign call, with 
115      CmmCallTarget
116      [(CmmReg,MachHint)]         -- zero or more results
117      [(CmmExpr,MachHint)]        -- zero or more arguments
118      (Maybe [GlobalReg])         -- Global regs that may need to be saved
119                                  -- if they will be clobbered by the call.
120                                  -- Nothing <=> save *all* globals that
121                                  -- might be clobbered.
122
123   | CmmBranch BlockId             -- branch to another BB in this fn
124
125   | CmmCondBranch CmmExpr BlockId -- conditional branch
126
127   | CmmSwitch CmmExpr [Maybe BlockId]   -- Table branch
128         -- The scrutinee is zero-based; 
129         --      zero -> first block
130         --      one  -> second block etc
131         -- Undefined outside range, and when there's a Nothing
132
133   | CmmJump CmmExpr [LocalReg]    -- Jump to another function, with these 
134                                   -- parameters.
135
136 -----------------------------------------------------------------------------
137 --              CmmCallTarget
138 --
139 -- The target of a CmmCall.
140 -----------------------------------------------------------------------------
141
142 data CmmCallTarget
143   = CmmForeignCall              -- Call to a foreign function
144         CmmExpr                 -- literal label <=> static call
145                                 -- other expression <=> dynamic call
146         CCallConv               -- The calling convention
147
148   | CmmPrim                     -- Call to a "primitive" (eg. sin, cos)
149         CallishMachOp           -- These might be implemented as inline
150                                 -- code by the backend.
151
152 -----------------------------------------------------------------------------
153 --              CmmExpr
154 -- An expression.  Expressions have no side effects.
155 -----------------------------------------------------------------------------
156
157 data CmmExpr
158   = CmmLit CmmLit               -- Literal
159   | CmmLoad CmmExpr MachRep     -- Read memory location
160   | CmmReg CmmReg               -- Contents of register
161   | CmmMachOp MachOp [CmmExpr]  -- Machine operation (+, -, *, etc.)
162   | CmmRegOff CmmReg Int        
163         -- CmmRegOff reg i
164         --        ** is shorthand only, meaning **
165         -- CmmMachOp (MO_S_Add rep (CmmReg reg) (CmmLit (CmmInt i rep)))
166         --      where rep = cmmRegRep reg
167
168 cmmExprRep :: CmmExpr -> MachRep
169 cmmExprRep (CmmLit lit)      = cmmLitRep lit
170 cmmExprRep (CmmLoad _ rep)   = rep
171 cmmExprRep (CmmReg reg)      = cmmRegRep reg
172 cmmExprRep (CmmMachOp op _)  = resultRepOfMachOp op
173 cmmExprRep (CmmRegOff reg _) = cmmRegRep reg
174
175 data CmmReg 
176   = CmmLocal  LocalReg
177   | CmmGlobal GlobalReg
178   deriving( Eq )
179
180 cmmRegRep :: CmmReg -> MachRep
181 cmmRegRep (CmmLocal  reg)       = localRegRep reg
182 cmmRegRep (CmmGlobal reg)       = globalRegRep reg
183
184 data LocalReg
185   = LocalReg !Unique MachRep
186
187 instance Eq LocalReg where
188   (LocalReg u1 _) == (LocalReg u2 _) = u1 == u2
189
190 instance Uniquable LocalReg where
191   getUnique (LocalReg uniq _) = uniq
192
193 localRegRep :: LocalReg -> MachRep
194 localRegRep (LocalReg _ rep) = rep
195
196 data CmmLit
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
206   
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
215
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
222
223 -----------------------------------------------------------------------------
224 -- A local label.
225
226 -- Local labels must be unique within a single compilation unit.
227
228 newtype BlockId = BlockId Unique
229   deriving (Eq,Ord)
230
231 instance Uniquable BlockId where
232   getUnique (BlockId u) = u
233
234 -----------------------------------------------------------------------------
235 --              Static Data
236 -----------------------------------------------------------------------------
237
238 data Section
239   = Text
240   | Data
241   | ReadOnlyData
242   | RelocatableReadOnlyData
243   | UninitialisedData
244   | ReadOnlyData16      -- .rodata.cst16 on x86_64, 16-byte aligned
245   | OtherSection String
246
247 data CmmStatic
248   = CmmStaticLit CmmLit 
249         -- a literal value, size given by cmmLitRep of the literal.
250   | CmmUninitialised Int
251         -- uninitialised data, N bytes long
252   | CmmAlign Int
253         -- align to next N-byte boundary (N must be a power of 2).
254   | CmmDataLabel CLabel
255         -- label the current position in this section.
256   | CmmString [Word8]
257         -- string of 8-bit values only, not zero terminated.
258
259 -----------------------------------------------------------------------------
260 --              Global STG registers
261 -----------------------------------------------------------------------------
262
263 data GlobalReg
264   -- Argument and return registers
265   = VanillaReg                  -- pointers, unboxed ints and chars
266         {-# UNPACK #-} !Int     -- its number
267
268   | FloatReg            -- single-precision floating-point registers
269         {-# UNPACK #-} !Int     -- its number
270
271   | DoubleReg           -- double-precision floating-point registers
272         {-# UNPACK #-} !Int     -- its number
273
274   | LongReg             -- long int registers (64-bit, really)
275         {-# UNPACK #-} !Int     -- its number
276
277   -- STG registers
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
285
286                 -- We keep the address of some commonly-called 
287                 -- functions in the register table, to keep code
288                 -- size down:
289   | GCEnter1            -- stg_gc_enter_1
290   | GCFun               -- stg_gc_fun
291
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.
296   | BaseReg
297
298   -- Base Register for PIC (position-independent code) calculations
299   -- Only used inside the native code generator. It's exact meaning differs
300   -- from platform to platform (see module PositionIndependentCode).
301   | PicBaseReg
302
303   deriving( Eq
304 #ifdef DEBUG
305         , Show
306 #endif
307          )
308
309 -- convenient aliases
310 spReg, hpReg, nodeReg :: CmmReg
311 spReg = CmmGlobal Sp
312 hpReg = CmmGlobal Hp
313 nodeReg = CmmGlobal node
314
315 node :: GlobalReg
316 node = VanillaReg 1
317
318 globalRegRep :: GlobalReg -> MachRep
319 globalRegRep (VanillaReg _)     = wordRep
320 globalRegRep (FloatReg _)       = F32
321 globalRegRep (DoubleReg _)      = F64
322 globalRegRep (LongReg _)        = I64
323 globalRegRep _                  = wordRep