Merge branch 'master' of darcs.haskell.org:/home/darcs/ghc
[ghc.git] / compiler / cmm / Cmm.hs
1 -- Cmm representations using Hoopl's Graph CmmNode e x.
2 {-# LANGUAGE GADTs #-}
3 {-# OPTIONS_GHC -fno-warn-warnings-deprecations #-}
4
5 module Cmm (
6 -- * Cmm top-level datatypes
7 CmmProgram, CmmGroup, GenCmmGroup,
8 CmmDecl, GenCmmDecl(..),
9 CmmGraph, GenCmmGraph(..),
10 CmmBlock,
11 RawCmmDecl, RawCmmGroup,
12 Section(..), CmmStatics(..), CmmStatic(..),
13
14 -- ** Blocks containing lists
15 GenBasicBlock(..), blockId,
16 ListGraph(..), pprBBlock,
17
18 -- * Cmm graphs
19 CmmReplGraph, GenCmmReplGraph, CmmFwdRewrite, CmmBwdRewrite,
20
21 -- * Info Tables
22 CmmTopInfo(..), CmmStackInfo(..), CmmInfoTable(..), topInfoTable,
23 ClosureTypeInfo(..),
24 C_SRT(..), needsSRT,
25 ProfilingInfo(..), ConstrDescription,
26
27 -- * Statements, expressions and types
28 module CmmNode,
29 module CmmExpr,
30 ) where
31
32 import CLabel
33 import BlockId
34 import CmmNode
35 import SMRep
36 import CmmExpr
37 import UniqSupply
38 import Compiler.Hoopl
39 import Outputable
40
41 import Data.Word ( Word8 )
42
43 #include "HsVersions.h"
44
45 -----------------------------------------------------------------------------
46 -- Cmm, GenCmm
47 -----------------------------------------------------------------------------
48
49 -- A CmmProgram is a list of CmmGroups
50 -- A CmmGroup is a list of top-level declarations
51
52 -- When object-splitting is on,each group is compiled into a separate
53 -- .o file. So typically we put closely related stuff in a CmmGroup.
54
55 type CmmProgram = [CmmGroup]
56
57 type GenCmmGroup d h g = [GenCmmDecl d h g]
58 type CmmGroup = GenCmmGroup CmmStatics CmmTopInfo CmmGraph
59 type RawCmmGroup = GenCmmGroup CmmStatics (BlockEnv CmmStatics) CmmGraph
60
61 -----------------------------------------------------------------------------
62 -- CmmDecl, GenCmmDecl
63 -----------------------------------------------------------------------------
64
65 -- GenCmmDecl is abstracted over
66 -- d, the type of static data elements in CmmData
67 -- h, the static info preceding the code of a CmmProc
68 -- g, the control-flow graph of a CmmProc
69 --
70 -- We expect there to be two main instances of this type:
71 -- (a) C--, i.e. populated with various C-- constructs
72 -- (b) Native code, populated with data/instructions
73
74 -- | A top-level chunk, abstracted over the type of the contents of
75 -- the basic blocks (Cmm or instructions are the likely instantiations).
76 data GenCmmDecl d h g
77 = CmmProc -- A procedure
78 h -- Extra header such as the info table
79 CLabel -- Entry label
80 [GlobalReg] -- Registers live on entry. Note that the set of live
81 -- registers will be correct in generated C-- code, but
82 -- not in hand-written C-- code. However,
83 -- splitAtProcPoints calculates correct liveness
84 -- information for CmmProc's. Right now only the LLVM
85 -- back-end relies on correct liveness information and
86 -- for that back-end we always call splitAtProcPoints, so
87 -- all is good.
88 g -- Control-flow graph for the procedure's code
89
90 | CmmData -- Static data
91 Section
92 d
93
94 type CmmDecl = GenCmmDecl CmmStatics CmmTopInfo CmmGraph
95
96 type RawCmmDecl
97 = GenCmmDecl
98 CmmStatics
99 (BlockEnv CmmStatics)
100 CmmGraph
101
102 -----------------------------------------------------------------------------
103 -- Graphs
104 -----------------------------------------------------------------------------
105
106 type CmmGraph = GenCmmGraph CmmNode
107 data GenCmmGraph n = CmmGraph { g_entry :: BlockId, g_graph :: Graph n C C }
108 type CmmBlock = Block CmmNode C C
109
110 type CmmReplGraph e x = GenCmmReplGraph CmmNode e x
111 type GenCmmReplGraph n e x = UniqSM (Maybe (Graph n e x))
112 type CmmFwdRewrite f = FwdRewrite UniqSM CmmNode f
113 type CmmBwdRewrite f = BwdRewrite UniqSM CmmNode f
114
115 -----------------------------------------------------------------------------
116 -- Info Tables
117 -----------------------------------------------------------------------------
118
119 data CmmTopInfo = TopInfo { info_tbls :: BlockEnv CmmInfoTable
120 , stack_info :: CmmStackInfo }
121
122 topInfoTable :: GenCmmDecl a CmmTopInfo (GenCmmGraph n) -> Maybe CmmInfoTable
123 topInfoTable (CmmProc infos _ _ g) = mapLookup (g_entry g) (info_tbls infos)
124 topInfoTable _ = Nothing
125
126 data CmmStackInfo
127 = StackInfo {
128 arg_space :: ByteOff,
129 -- number of bytes of arguments on the stack on entry to the
130 -- the proc. This is filled in by StgCmm.codeGen, and used
131 -- by the stack allocator later.
132 updfr_space :: Maybe ByteOff,
133 -- XXX: this never contains anything useful, but it should.
134 -- See comment in CmmLayoutStack.
135 do_layout :: Bool
136 -- Do automatic stack layout for this proc. This is
137 -- True for all code generated by the code generator,
138 -- but is occasionally False for hand-written Cmm where
139 -- we want to do the stack manipulation manually.
140 }
141
142 -- | Info table as a haskell data type
143 data CmmInfoTable
144 = CmmInfoTable {
145 cit_lbl :: CLabel, -- Info table label
146 cit_rep :: SMRep,
147 cit_prof :: ProfilingInfo,
148 cit_srt :: C_SRT
149 }
150
151 data ProfilingInfo
152 = NoProfilingInfo
153 | ProfilingInfo [Word8] [Word8] -- closure_type, closure_desc
154
155 -- C_SRT is what StgSyn.SRT gets translated to...
156 -- we add a label for the table, and expect only the 'offset/length' form
157
158 data C_SRT = NoC_SRT
159 | C_SRT !CLabel !WordOff !StgHalfWord {-bitmap or escape-}
160 deriving (Eq)
161
162 needsSRT :: C_SRT -> Bool
163 needsSRT NoC_SRT = False
164 needsSRT (C_SRT _ _ _) = True
165
166 -----------------------------------------------------------------------------
167 -- Static Data
168 -----------------------------------------------------------------------------
169
170 data Section
171 = Text
172 | Data
173 | ReadOnlyData
174 | RelocatableReadOnlyData
175 | UninitialisedData
176 | ReadOnlyData16 -- .rodata.cst16 on x86_64, 16-byte aligned
177 | OtherSection String
178
179 data CmmStatic
180 = CmmStaticLit CmmLit
181 -- a literal value, size given by cmmLitRep of the literal.
182 | CmmUninitialised Int
183 -- uninitialised data, N bytes long
184 | CmmString [Word8]
185 -- string of 8-bit values only, not zero terminated.
186
187 data CmmStatics
188 = Statics
189 CLabel -- Label of statics
190 [CmmStatic] -- The static data itself
191
192 -- -----------------------------------------------------------------------------
193 -- Basic blocks consisting of lists
194
195 -- These are used by the LLVM and NCG backends, when populating Cmm
196 -- with lists of instructions.
197
198 data GenBasicBlock i = BasicBlock BlockId [i]
199
200 -- | The branch block id is that of the first block in
201 -- the branch, which is that branch's entry point
202 blockId :: GenBasicBlock i -> BlockId
203 blockId (BasicBlock blk_id _ ) = blk_id
204
205 newtype ListGraph i = ListGraph [GenBasicBlock i]
206
207 instance Outputable instr => Outputable (ListGraph instr) where
208 ppr (ListGraph blocks) = vcat (map ppr blocks)
209
210 instance Outputable instr => Outputable (GenBasicBlock instr) where
211 ppr = pprBBlock
212
213 pprBBlock :: Outputable stmt => GenBasicBlock stmt -> SDoc
214 pprBBlock (BasicBlock ident stmts) =
215 hang (ppr ident <> colon) 4 (vcat (map ppr stmts))
216