Merge remote-tracking branch 'origin/master' into newcg
[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 top-level chunk, abstracted over the type of the contents of
82 -- the basic blocks (Cmm or instructions are the likely instantiations).
83 data GenCmmDecl d h g
84 = CmmProc -- A procedure
85 h -- Extra header such as the info table
86 CLabel -- Entry label
87 g -- Control-flow graph for the procedure's code
88
89 | CmmData -- Static data
90 Section
91 d
92
93 type CmmDecl = GenCmmDecl CmmStatics CmmTopInfo CmmGraph
94
95 -----------------------------------------------------------------------------
96 -- Graphs
97 -----------------------------------------------------------------------------
98
99 type CmmGraph = GenCmmGraph CmmNode
100 data GenCmmGraph n = CmmGraph { g_entry :: BlockId, g_graph :: Graph n C C }
101 type CmmBlock = Block CmmNode C C
102
103 type CmmReplGraph e x = GenCmmReplGraph CmmNode e x
104 type GenCmmReplGraph n e x = FuelUniqSM (Maybe (Graph n e x))
105 type CmmFwdRewrite f = FwdRewrite FuelUniqSM CmmNode f
106 type CmmBwdRewrite f = BwdRewrite FuelUniqSM CmmNode f
107
108 -----------------------------------------------------------------------------
109 -- Info Tables
110 -----------------------------------------------------------------------------
111
112 data CmmTopInfo = TopInfo { info_tbl :: CmmInfoTable
113 , stack_info :: CmmStackInfo }
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 -- XXX: comment?
122 }
123
124 -- | Info table as a haskell data type
125 data CmmInfoTable
126 = CmmInfoTable {
127 cit_lbl :: CLabel, -- Info table label
128 cit_rep :: SMRep,
129 cit_prof :: ProfilingInfo,
130 cit_srt :: C_SRT
131 }
132 | CmmNonInfoTable -- Procedure doesn't need an info table
133
134 data ProfilingInfo
135 = NoProfilingInfo
136 | ProfilingInfo [Word8] [Word8] -- closure_type, closure_desc
137
138 -- C_SRT is what StgSyn.SRT gets translated to...
139 -- we add a label for the table, and expect only the 'offset/length' form
140
141 data C_SRT = NoC_SRT
142 | C_SRT !CLabel !WordOff !StgHalfWord {-bitmap or escape-}
143 deriving (Eq)
144
145 needsSRT :: C_SRT -> Bool
146 needsSRT NoC_SRT = False
147 needsSRT (C_SRT _ _ _) = True
148
149 -----------------------------------------------------------------------------
150 -- Static Data
151 -----------------------------------------------------------------------------
152
153 data Section
154 = Text
155 | Data
156 | ReadOnlyData
157 | RelocatableReadOnlyData
158 | UninitialisedData
159 | ReadOnlyData16 -- .rodata.cst16 on x86_64, 16-byte aligned
160 | OtherSection String
161
162 data CmmStatic
163 = CmmStaticLit CmmLit
164 -- a literal value, size given by cmmLitRep of the literal.
165 | CmmUninitialised Int
166 -- uninitialised data, N bytes long
167 | CmmString [Word8]
168 -- string of 8-bit values only, not zero terminated.
169
170 data CmmStatics
171 = Statics
172 CLabel -- Label of statics
173 [CmmStatic] -- The static data itself
174