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