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