Merge branch 'master' of darcs.haskell.org:/srv/darcs//ghc
[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 g -- Control-flow graph for the procedure's code
75
76 | CmmData -- Static data
77 Section
78 d
79
80 type CmmDecl = GenCmmDecl CmmStatics CmmTopInfo CmmGraph
81
82 -----------------------------------------------------------------------------
83 -- Graphs
84 -----------------------------------------------------------------------------
85
86 type CmmGraph = GenCmmGraph CmmNode
87 data GenCmmGraph n = CmmGraph { g_entry :: BlockId, g_graph :: Graph n C C }
88 type CmmBlock = Block CmmNode C C
89
90 type CmmReplGraph e x = GenCmmReplGraph CmmNode e x
91 type GenCmmReplGraph n e x = UniqSM (Maybe (Graph n e x))
92 type CmmFwdRewrite f = FwdRewrite UniqSM CmmNode f
93 type CmmBwdRewrite f = BwdRewrite UniqSM CmmNode f
94
95 -----------------------------------------------------------------------------
96 -- Info Tables
97 -----------------------------------------------------------------------------
98
99 data CmmTopInfo = TopInfo { info_tbls :: BlockEnv CmmInfoTable
100 , stack_info :: CmmStackInfo }
101
102 topInfoTable :: GenCmmDecl a CmmTopInfo (GenCmmGraph n) -> Maybe CmmInfoTable
103 topInfoTable (CmmProc infos _ g) = mapLookup (g_entry g) (info_tbls infos)
104 topInfoTable _ = Nothing
105
106 data CmmStackInfo
107 = StackInfo {
108 arg_space :: ByteOff,
109 -- number of bytes of arguments on the stack on entry to the
110 -- the proc. This is filled in by StgCmm.codeGen, and used
111 -- by the stack allocator later.
112 updfr_space :: Maybe ByteOff
113 -- XXX: this never contains anything useful, but it should.
114 -- See comment in CmmLayoutStack.
115 }
116
117 -- | Info table as a haskell data type
118 data CmmInfoTable
119 = CmmInfoTable {
120 cit_lbl :: CLabel, -- Info table label
121 cit_rep :: SMRep,
122 cit_prof :: ProfilingInfo,
123 cit_srt :: C_SRT
124 }
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