Attach global register liveness info to Cmm procedures.
[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 module Cmm (
6 -- * Cmm top-level datatypes
7 CmmProgram, CmmGroup, GenCmmGroup,
8 CmmDecl, GenCmmDecl(..),
9 CmmGraph, GenCmmGraph(..),
10 CmmBlock,
11 Section(..), CmmStatics(..), CmmStatic(..),
12
13 -- * Cmm graphs
14 CmmReplGraph, GenCmmReplGraph, CmmFwdRewrite, CmmBwdRewrite,
15
16 -- * Info Tables
17 CmmTopInfo(..), CmmStackInfo(..), CmmInfoTable(..), topInfoTable,
18 ClosureTypeInfo(..),
19 C_SRT(..), needsSRT,
20 ProfilingInfo(..), ConstrDescription,
21
22 -- * Statements, expressions and types
23 module CmmNode,
24 module CmmExpr,
25 ) where
26
27 import CLabel
28 import BlockId
29 import CmmNode
30 import SMRep
31 import CmmExpr
32 import UniqSupply
33 import Compiler.Hoopl
34
35 import Data.Word ( Word8 )
36
37 #include "HsVersions.h"
38
39 -----------------------------------------------------------------------------
40 -- Cmm, GenCmm
41 -----------------------------------------------------------------------------
42
43 -- A CmmProgram is a list of CmmGroups
44 -- A CmmGroup is a list of top-level declarations
45
46 -- When object-splitting is on,each group is compiled into a separate
47 -- .o file. So typically we put closely related stuff in a CmmGroup.
48
49 type CmmProgram = [CmmGroup]
50
51 type GenCmmGroup d h g = [GenCmmDecl d h g]
52 type CmmGroup = GenCmmGroup CmmStatics CmmTopInfo CmmGraph
53
54 -----------------------------------------------------------------------------
55 -- CmmDecl, GenCmmDecl
56 -----------------------------------------------------------------------------
57
58 -- GenCmmDecl is abstracted over
59 -- d, the type of static data elements in CmmData
60 -- h, the static info preceding the code of a CmmProc
61 -- g, the control-flow graph of a CmmProc
62 --
63 -- We expect there to be two main instances of this type:
64 -- (a) C--, i.e. populated with various C-- constructs
65 -- (Cmm and RawCmm in OldCmm.hs)
66 -- (b) Native code, populated with data/instructions
67
68 -- | A top-level chunk, abstracted over the type of the contents of
69 -- the basic blocks (Cmm or instructions are the likely instantiations).
70 data GenCmmDecl d h g
71 = CmmProc -- A procedure
72 h -- Extra header such as the info table
73 CLabel -- Entry label
74 [GlobalReg] -- Registers live on entry. Note that the set of live
75 -- registers will be correct in generated C-- code, but
76 -- not in hand-written C-- code. However,
77 -- splitAtProcPoints calculates correct liveness
78 -- information for CmmProc's. Right now only the LLVM
79 -- back-end relies on correct liveness information and
80 -- for that back-end we always call splitAtProcPoints, so
81 -- all is good.
82 g -- Control-flow graph for the procedure's code
83
84 | CmmData -- Static data
85 Section
86 d
87
88 type CmmDecl = GenCmmDecl CmmStatics CmmTopInfo CmmGraph
89
90 -----------------------------------------------------------------------------
91 -- Graphs
92 -----------------------------------------------------------------------------
93
94 type CmmGraph = GenCmmGraph CmmNode
95 data GenCmmGraph n = CmmGraph { g_entry :: BlockId, g_graph :: Graph n C C }
96 type CmmBlock = Block CmmNode C C
97
98 type CmmReplGraph e x = GenCmmReplGraph CmmNode e x
99 type GenCmmReplGraph n e x = UniqSM (Maybe (Graph n e x))
100 type CmmFwdRewrite f = FwdRewrite UniqSM CmmNode f
101 type CmmBwdRewrite f = BwdRewrite UniqSM CmmNode f
102
103 -----------------------------------------------------------------------------
104 -- Info Tables
105 -----------------------------------------------------------------------------
106
107 data CmmTopInfo = TopInfo { info_tbls :: BlockEnv CmmInfoTable
108 , stack_info :: CmmStackInfo }
109
110 topInfoTable :: GenCmmDecl a CmmTopInfo (GenCmmGraph n) -> Maybe CmmInfoTable
111 topInfoTable (CmmProc infos _ _ g) = mapLookup (g_entry g) (info_tbls infos)
112 topInfoTable _ = Nothing
113
114 data CmmStackInfo
115 = StackInfo {
116 arg_space :: ByteOff,
117 -- number of bytes of arguments on the stack on entry to the
118 -- the proc. This is filled in by StgCmm.codeGen, and used
119 -- by the stack allocator later.
120 updfr_space :: Maybe ByteOff,
121 -- XXX: this never contains anything useful, but it should.
122 -- See comment in CmmLayoutStack.
123 do_layout :: Bool
124 -- Do automatic stack layout for this proc. This is
125 -- True for all code generated by the code generator,
126 -- but is occasionally False for hand-written Cmm where
127 -- we want to do the stack manipulation manually.
128 }
129
130 -- | Info table as a haskell data type
131 data CmmInfoTable
132 = CmmInfoTable {
133 cit_lbl :: CLabel, -- Info table label
134 cit_rep :: SMRep,
135 cit_prof :: ProfilingInfo,
136 cit_srt :: C_SRT
137 }
138
139 data ProfilingInfo
140 = NoProfilingInfo
141 | ProfilingInfo [Word8] [Word8] -- closure_type, closure_desc
142
143 -- C_SRT is what StgSyn.SRT gets translated to...
144 -- we add a label for the table, and expect only the 'offset/length' form
145
146 data C_SRT = NoC_SRT
147 | C_SRT !CLabel !WordOff !StgHalfWord {-bitmap or escape-}
148 deriving (Eq)
149
150 needsSRT :: C_SRT -> Bool
151 needsSRT NoC_SRT = False
152 needsSRT (C_SRT _ _ _) = True
153
154 -----------------------------------------------------------------------------
155 -- Static Data
156 -----------------------------------------------------------------------------
157
158 data Section
159 = Text
160 | Data
161 | ReadOnlyData
162 | RelocatableReadOnlyData
163 | UninitialisedData
164 | ReadOnlyData16 -- .rodata.cst16 on x86_64, 16-byte aligned
165 | OtherSection String
166
167 data CmmStatic
168 = CmmStaticLit CmmLit
169 -- a literal value, size given by cmmLitRep of the literal.
170 | CmmUninitialised Int
171 -- uninitialised data, N bytes long
172 | CmmString [Word8]
173 -- string of 8-bit values only, not zero terminated.
174
175 data CmmStatics
176 = Statics
177 CLabel -- Label of statics
178 [CmmStatic] -- The static data itself
179