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