comments
[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 {-# OPTIONS -fno-warn-tabs #-}
6 -- The above warning supression flag is a temporary kludge.
7 -- While working on this module you are encouraged to remove it and
8 -- detab the module (please do the detabbing in a separate patch). See
9 -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
10 -- for details
11
12 {-# OPTIONS_GHC -fno-warn-incomplete-patterns #-}
13 #if __GLASGOW_HASKELL__ >= 703
14 -- GHC 7.0.1 improved incomplete pattern warnings with GADTs
15 {-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
16 #endif
17
18 module Cmm (
19 -- * Cmm top-level datatypes
20 CmmProgram, CmmGroup, GenCmmGroup,
21 CmmDecl, GenCmmDecl(..),
22 CmmGraph, GenCmmGraph(..),
23 CmmBlock,
24 Section(..), CmmStatics(..), CmmStatic(..),
25
26 -- * Cmm graphs
27 CmmReplGraph, GenCmmReplGraph, CmmFwdRewrite, CmmBwdRewrite,
28
29 -- * Info Tables
30 CmmTopInfo(..), CmmStackInfo(..), CmmInfoTable(..),
31 ClosureTypeInfo(..),
32 C_SRT(..), needsSRT,
33 ProfilingInfo(..), ConstrDescription,
34
35 -- * Statements, expressions and types
36 module CmmNode,
37 module CmmExpr,
38 ) where
39
40 import CLabel
41 import BlockId
42 import CmmNode
43 import OptimizationFuel as F
44 import SMRep
45 import CmmExpr
46 import Compiler.Hoopl
47
48 import Data.Word ( Word8 )
49
50 #include "HsVersions.h"
51
52 -----------------------------------------------------------------------------
53 -- Cmm, GenCmm
54 -----------------------------------------------------------------------------
55
56 -- A CmmProgram is a list of CmmGroups
57 -- A CmmGroup is a list of top-level declarations
58
59 -- When object-splitting is on,each group is compiled into a separate
60 -- .o file. So typically we put closely related stuff in a CmmGroup.
61
62 type CmmProgram = [CmmGroup]
63
64 type GenCmmGroup d h g = [GenCmmDecl d h g]
65 type CmmGroup = GenCmmGroup CmmStatics CmmTopInfo CmmGraph
66
67 -----------------------------------------------------------------------------
68 -- CmmDecl, GenCmmDecl
69 -----------------------------------------------------------------------------
70
71 -- GenCmmDecl is abstracted over
72 -- d, the type of static data elements in CmmData
73 -- h, the static info preceding the code of a CmmProc
74 -- g, the control-flow graph of a CmmProc
75 --
76 -- We expect there to be two main instances of this type:
77 -- (a) C--, i.e. populated with various C-- constructs
78 -- (Cmm and RawCmm in OldCmm.hs)
79 -- (b) Native code, populated with data/instructions
80 --
81 -- A second family of instances based on Hoopl is in Cmm.hs.
82
83 -- | A top-level chunk, abstracted over the type of the contents of
84 -- the basic blocks (Cmm or instructions are the likely instantiations).
85 data GenCmmDecl d h g
86 = CmmProc -- A procedure
87 h -- Extra header such as the info table
88 CLabel -- Entry label
89 g -- Control-flow graph for the procedure's code
90
91 | CmmData -- Static data
92 Section
93 d
94
95 type CmmDecl = GenCmmDecl CmmStatics CmmTopInfo 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 type CmmReplGraph e x = GenCmmReplGraph CmmNode e x
106 type GenCmmReplGraph n e x = FuelUniqSM (Maybe (Graph n e x))
107 type CmmFwdRewrite f = FwdRewrite FuelUniqSM CmmNode f
108 type CmmBwdRewrite f = BwdRewrite FuelUniqSM CmmNode f
109
110 -----------------------------------------------------------------------------
111 -- Info Tables
112 -----------------------------------------------------------------------------
113
114 data CmmTopInfo = TopInfo {info_tbl :: CmmInfoTable, stack_info :: CmmStackInfo}
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 -- XXX: comment?
123 }
124
125 -- | Info table as a haskell data type
126 data CmmInfoTable
127 = CmmInfoTable {
128 cit_lbl :: CLabel, -- Info table label
129 cit_rep :: SMRep,
130 cit_prof :: ProfilingInfo,
131 cit_srt :: C_SRT
132 }
133 | CmmNonInfoTable -- Procedure doesn't need an info table
134
135 data ProfilingInfo
136 = NoProfilingInfo
137 | ProfilingInfo [Word8] [Word8] -- closure_type, closure_desc
138
139 -- C_SRT is what StgSyn.SRT gets translated to...
140 -- we add a label for the table, and expect only the 'offset/length' form
141
142 data C_SRT = NoC_SRT
143 | C_SRT !CLabel !WordOff !StgHalfWord {-bitmap or escape-}
144 deriving (Eq)
145
146 needsSRT :: C_SRT -> Bool
147 needsSRT NoC_SRT = False
148 needsSRT (C_SRT _ _ _) = True
149
150 -----------------------------------------------------------------------------
151 -- Static Data
152 -----------------------------------------------------------------------------
153
154 data Section
155 = Text
156 | Data
157 | ReadOnlyData
158 | RelocatableReadOnlyData
159 | UninitialisedData
160 | ReadOnlyData16 -- .rodata.cst16 on x86_64, 16-byte aligned
161 | OtherSection String
162
163 data CmmStatic
164 = CmmStaticLit CmmLit
165 -- a literal value, size given by cmmLitRep of the literal.
166 | CmmUninitialised Int
167 -- uninitialised data, N bytes long
168 | CmmString [Word8]
169 -- string of 8-bit values only, not zero terminated.
170
171 data CmmStatics
172 = Statics
173 CLabel -- Label of statics
174 [CmmStatic] -- The static data itself
175