Merge remote-tracking branch 'origin/type-nats' into type-nats-merge
[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, -- XXX: comment?
119 updfr_space :: Maybe ByteOff -- XXX: comment?
120 }
121
122 -- | Info table as a haskell data type
123 data CmmInfoTable
124 = CmmInfoTable {
125 cit_lbl :: CLabel, -- Info table label
126 cit_rep :: SMRep,
127 cit_prof :: ProfilingInfo,
128 cit_srt :: C_SRT
129 }
130 | CmmNonInfoTable -- Procedure doesn't need an info table
131
132 data ProfilingInfo
133 = NoProfilingInfo
134 | ProfilingInfo [Word8] [Word8] -- closure_type, closure_desc
135
136 -- C_SRT is what StgSyn.SRT gets translated to...
137 -- we add a label for the table, and expect only the 'offset/length' form
138
139 data C_SRT = NoC_SRT
140 | C_SRT !CLabel !WordOff !StgHalfWord {-bitmap or escape-}
141 deriving (Eq)
142
143 needsSRT :: C_SRT -> Bool
144 needsSRT NoC_SRT = False
145 needsSRT (C_SRT _ _ _) = True
146
147 -----------------------------------------------------------------------------
148 -- Static Data
149 -----------------------------------------------------------------------------
150
151 data Section
152 = Text
153 | Data
154 | ReadOnlyData
155 | RelocatableReadOnlyData
156 | UninitialisedData
157 | ReadOnlyData16 -- .rodata.cst16 on x86_64, 16-byte aligned
158 | OtherSection String
159
160 data CmmStatic
161 = CmmStaticLit CmmLit
162 -- a literal value, size given by cmmLitRep of the literal.
163 | CmmUninitialised Int
164 -- uninitialised data, N bytes long
165 | CmmString [Word8]
166 -- string of 8-bit values only, not zero terminated.
167
168 data CmmStatics
169 = Statics
170 CLabel -- Label of statics
171 [CmmStatic] -- The static data itself
172