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