Merge branch 'master' into type-nats
[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 {-# OPTIONS_GHC -fno-warn-incomplete-patterns #-}
5 #if __GLASGOW_HASKELL__ >= 703
6 -- GHC 7.0.1 improved incomplete pattern warnings with GADTs
7 {-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
8 #endif
9
10 module Cmm (
11 -- * Cmm top-level datatypes
12 CmmProgram, CmmGroup, GenCmmGroup,
13 CmmDecl, GenCmmDecl(..),
14 CmmGraph, GenCmmGraph(..),
15 CmmBlock,
16 Section(..), CmmStatics(..), CmmStatic(..),
17
18 -- * Cmm graphs
19 CmmReplGraph, GenCmmReplGraph, CmmFwdRewrite, CmmBwdRewrite,
20
21 -- * Info Tables
22 CmmTopInfo(..), CmmStackInfo(..), CmmInfoTable(..),
23 ClosureTypeInfo(..),
24 C_SRT(..), needsSRT,
25 ProfilingInfo(..), ConstrDescription,
26
27 -- * Statements, expressions and types
28 module CmmNode,
29 module CmmExpr,
30 ) where
31
32 import CLabel
33 import BlockId
34 import CmmNode
35 import OptimizationFuel as F
36 import SMRep
37 import CmmExpr
38 import Compiler.Hoopl
39
40 import Data.Word ( Word8 )
41
42 #include "HsVersions.h"
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
54 type CmmProgram = [CmmGroup]
55
56 type GenCmmGroup d h g = [GenCmmDecl d h g]
57 type CmmGroup = GenCmmGroup CmmStatics CmmTopInfo 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 -- (Cmm and RawCmm in OldCmm.hs)
71 -- (b) Native code, populated with data/instructions
72 --
73 -- A second family of instances based on Hoopl is in Cmm.hs.
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 g -- Control-flow graph for the procedure's code
82
83 | CmmData -- Static data
84 Section
85 d
86
87 type CmmDecl = GenCmmDecl CmmStatics CmmTopInfo CmmGraph
88
89 -----------------------------------------------------------------------------
90 -- Graphs
91 -----------------------------------------------------------------------------
92
93 type CmmGraph = GenCmmGraph CmmNode
94 data GenCmmGraph n = CmmGraph { g_entry :: BlockId, g_graph :: Graph n C C }
95 type CmmBlock = Block CmmNode C C
96
97 type CmmReplGraph e x = GenCmmReplGraph CmmNode e x
98 type GenCmmReplGraph n e x = FuelUniqSM (Maybe (Graph n e x))
99 type CmmFwdRewrite f = FwdRewrite FuelUniqSM CmmNode f
100 type CmmBwdRewrite f = BwdRewrite FuelUniqSM CmmNode f
101
102 -----------------------------------------------------------------------------
103 -- Info Tables
104 -----------------------------------------------------------------------------
105
106 data CmmTopInfo = TopInfo {info_tbl :: CmmInfoTable, stack_info :: CmmStackInfo}
107
108 data CmmStackInfo
109 = StackInfo {
110 arg_space :: ByteOff, -- XXX: comment?
111 updfr_space :: Maybe ByteOff -- XXX: comment?
112 }
113
114 -- | Info table as a haskell data type
115 data CmmInfoTable
116 = CmmInfoTable {
117 cit_lbl :: CLabel, -- Info table label
118 cit_rep :: SMRep,
119 cit_prof :: ProfilingInfo,
120 cit_srt :: C_SRT
121 }
122 | CmmNonInfoTable -- Procedure doesn't need an info table
123
124 data ProfilingInfo
125 = NoProfilingInfo
126 | ProfilingInfo [Word8] [Word8] -- closure_type, closure_desc
127
128 -- C_SRT is what StgSyn.SRT gets translated to...
129 -- we add a label for the table, and expect only the 'offset/length' form
130
131 data C_SRT = NoC_SRT
132 | C_SRT !CLabel !WordOff !StgHalfWord {-bitmap or escape-}
133 deriving (Eq)
134
135 needsSRT :: C_SRT -> Bool
136 needsSRT NoC_SRT = False
137 needsSRT (C_SRT _ _ _) = True
138
139 -----------------------------------------------------------------------------
140 -- Static Data
141 -----------------------------------------------------------------------------
142
143 data Section
144 = Text
145 | Data
146 | ReadOnlyData
147 | RelocatableReadOnlyData
148 | UninitialisedData
149 | ReadOnlyData16 -- .rodata.cst16 on x86_64, 16-byte aligned
150 | OtherSection String
151
152 data CmmStatic
153 = CmmStaticLit CmmLit
154 -- a literal value, size given by cmmLitRep of the literal.
155 | CmmUninitialised Int
156 -- uninitialised data, N bytes long
157 | CmmString [Word8]
158 -- string of 8-bit values only, not zero terminated.
159
160 data CmmStatics
161 = Statics
162 CLabel -- Label of statics
163 [CmmStatic] -- The static data itself
164