Use ByteString to represent Cmm string literals (#16198)
[ghc.git] / compiler / cmm / Cmm.hs
1 -- Cmm representations using Hoopl's Graph CmmNode e x.
2 {-# LANGUAGE 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 ProfilingInfo(..), ConstrDescription,
22
23 -- * Statements, expressions and types
24 module CmmNode,
25 module CmmExpr,
26 ) where
27
28 import GhcPrelude
29
30 import Id
31 import CostCentre
32 import CLabel
33 import BlockId
34 import CmmNode
35 import SMRep
36 import CmmExpr
37 import Hoopl.Block
38 import Hoopl.Collections
39 import Hoopl.Graph
40 import Hoopl.Label
41 import Outputable
42 import Data.ByteString (ByteString)
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 -- Section-splitting follows suit and makes one .text subsection for each
54 -- CmmGroup.
55
56 type CmmProgram = [CmmGroup]
57
58 type GenCmmGroup d h g = [GenCmmDecl d h g]
59 type CmmGroup = GenCmmGroup CmmStatics CmmTopInfo CmmGraph
60 type RawCmmGroup = GenCmmGroup CmmStatics (LabelMap CmmStatics) CmmGraph
61
62 -----------------------------------------------------------------------------
63 -- CmmDecl, GenCmmDecl
64 -----------------------------------------------------------------------------
65
66 -- GenCmmDecl is abstracted over
67 -- d, the type of static data elements in CmmData
68 -- h, the static info preceding the code of a CmmProc
69 -- g, the control-flow graph of a CmmProc
70 --
71 -- We expect there to be two main instances of this type:
72 -- (a) C--, i.e. populated with various C-- constructs
73 -- (b) Native code, populated with data/instructions
74
75 -- | A top-level chunk, abstracted over the type of the contents of
76 -- the basic blocks (Cmm or instructions are the likely instantiations).
77 data GenCmmDecl d h g
78 = CmmProc -- A procedure
79 h -- Extra header such as the info table
80 CLabel -- Entry label
81 [GlobalReg] -- Registers live on entry. Note that the set of live
82 -- registers will be correct in generated C-- code, but
83 -- not in hand-written C-- code. However,
84 -- splitAtProcPoints calculates correct liveness
85 -- information for CmmProcs.
86 g -- Control-flow graph for the procedure's code
87
88 | CmmData -- Static data
89 Section
90 d
91
92 type CmmDecl = GenCmmDecl CmmStatics CmmTopInfo CmmGraph
93
94 type RawCmmDecl
95 = GenCmmDecl
96 CmmStatics
97 (LabelMap CmmStatics)
98 CmmGraph
99
100 -----------------------------------------------------------------------------
101 -- Graphs
102 -----------------------------------------------------------------------------
103
104 type CmmGraph = GenCmmGraph CmmNode
105 data GenCmmGraph n = CmmGraph { g_entry :: BlockId, g_graph :: Graph n C C }
106 type CmmBlock = Block CmmNode C C
107
108 -----------------------------------------------------------------------------
109 -- Info Tables
110 -----------------------------------------------------------------------------
111
112 data CmmTopInfo = TopInfo { info_tbls :: LabelMap CmmInfoTable
113 , stack_info :: CmmStackInfo }
114
115 topInfoTable :: GenCmmDecl a CmmTopInfo (GenCmmGraph n) -> Maybe CmmInfoTable
116 topInfoTable (CmmProc infos _ _ g) = mapLookup (g_entry g) (info_tbls infos)
117 topInfoTable _ = Nothing
118
119 data CmmStackInfo
120 = StackInfo {
121 arg_space :: ByteOff,
122 -- number of bytes of arguments on the stack on entry to the
123 -- the proc. This is filled in by StgCmm.codeGen, and used
124 -- by the stack allocator later.
125 updfr_space :: Maybe ByteOff,
126 -- XXX: this never contains anything useful, but it should.
127 -- See comment in CmmLayoutStack.
128 do_layout :: Bool
129 -- Do automatic stack layout for this proc. This is
130 -- True for all code generated by the code generator,
131 -- but is occasionally False for hand-written Cmm where
132 -- we want to do the stack manipulation manually.
133 }
134
135 -- | Info table as a haskell data type
136 data CmmInfoTable
137 = CmmInfoTable {
138 cit_lbl :: CLabel, -- Info table label
139 cit_rep :: SMRep,
140 cit_prof :: ProfilingInfo,
141 cit_srt :: Maybe CLabel, -- empty, or a closure address
142 cit_clo :: Maybe (Id, CostCentreStack)
143 -- Just (id,ccs) <=> build a static closure later
144 -- Nothing <=> don't build a static closure
145 --
146 -- Static closures for FUNs and THUNKs are *not* generated by
147 -- the code generator, because we might want to add SRT
148 -- entries to them later (for FUNs at least; THUNKs are
149 -- treated the same for consistency). See Note [SRTs] in
150 -- CmmBuildInfoTables, in particular the [FUN] optimisation.
151 --
152 -- This is strictly speaking not a part of the info table that
153 -- will be finally generated, but it's the only convenient
154 -- place to convey this information from the code generator to
155 -- where we build the static closures in
156 -- CmmBuildInfoTables.doSRTs.
157 }
158
159 data ProfilingInfo
160 = NoProfilingInfo
161 | ProfilingInfo ByteString ByteString -- closure_type, closure_desc
162
163 -----------------------------------------------------------------------------
164 -- Static Data
165 -----------------------------------------------------------------------------
166
167 data SectionType
168 = Text
169 | Data
170 | ReadOnlyData
171 | RelocatableReadOnlyData
172 | UninitialisedData
173 | ReadOnlyData16 -- .rodata.cst16 on x86_64, 16-byte aligned
174 | CString
175 | OtherSection String
176 deriving (Show)
177
178 -- | Should a data in this section be considered constant
179 isSecConstant :: Section -> Bool
180 isSecConstant (Section t _) = case t of
181 Text -> True
182 ReadOnlyData -> True
183 RelocatableReadOnlyData -> True
184 ReadOnlyData16 -> True
185 CString -> True
186 Data -> False
187 UninitialisedData -> False
188 (OtherSection _) -> False
189
190 data Section = Section SectionType CLabel
191
192 data CmmStatic
193 = CmmStaticLit CmmLit
194 -- a literal value, size given by cmmLitRep of the literal.
195 | CmmUninitialised Int
196 -- uninitialised data, N bytes long
197 | CmmString ByteString
198 -- string of 8-bit values only, not zero terminated.
199
200 data CmmStatics
201 = Statics
202 CLabel -- Label of statics
203 [CmmStatic] -- The static data itself
204
205 -- -----------------------------------------------------------------------------
206 -- Basic blocks consisting of lists
207
208 -- These are used by the LLVM and NCG backends, when populating Cmm
209 -- with lists of instructions.
210
211 data GenBasicBlock i = BasicBlock BlockId [i]
212
213 -- | The branch block id is that of the first block in
214 -- the branch, which is that branch's entry point
215 blockId :: GenBasicBlock i -> BlockId
216 blockId (BasicBlock blk_id _ ) = blk_id
217
218 newtype ListGraph i = ListGraph [GenBasicBlock i]
219
220 instance Outputable instr => Outputable (ListGraph instr) where
221 ppr (ListGraph blocks) = vcat (map ppr blocks)
222
223 instance Outputable instr => Outputable (GenBasicBlock instr) where
224 ppr = pprBBlock
225
226 pprBBlock :: Outputable stmt => GenBasicBlock stmt -> SDoc
227 pprBBlock (BasicBlock ident stmts) =
228 hang (ppr ident <> colon) 4 (vcat (map ppr stmts))
229