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(..),
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_tbl :: CmmInfoTable
100 , stack_info :: CmmStackInfo }
101
102 data CmmStackInfo
103 = StackInfo {
104 arg_space :: ByteOff,
105 -- number of bytes of arguments on the stack on entry to the
106 -- the proc. This is filled in by StgCmm.codeGen, and used
107 -- by the stack allocator later.
108 updfr_space :: Maybe ByteOff -- XXX: comment?
109 }
110
111 -- | Info table as a haskell data type
112 data CmmInfoTable
113 = CmmInfoTable {
114 cit_lbl :: CLabel, -- Info table label
115 cit_rep :: SMRep,
116 cit_prof :: ProfilingInfo,
117 cit_srt :: C_SRT
118 }
119 | CmmNonInfoTable -- Procedure doesn't need an info table
120
121 data ProfilingInfo
122 = NoProfilingInfo
123 | ProfilingInfo [Word8] [Word8] -- closure_type, closure_desc
124
125 -- C_SRT is what StgSyn.SRT gets translated to...
126 -- we add a label for the table, and expect only the 'offset/length' form
127
128 data C_SRT = NoC_SRT
129 | C_SRT !CLabel !WordOff !StgHalfWord {-bitmap or escape-}
130 deriving (Eq)
131
132 needsSRT :: C_SRT -> Bool
133 needsSRT NoC_SRT = False
134 needsSRT (C_SRT _ _ _) = True
135
136 -----------------------------------------------------------------------------
137 -- Static Data
138 -----------------------------------------------------------------------------
139
140 data Section
141 = Text
142 | Data
143 | ReadOnlyData
144 | RelocatableReadOnlyData
145 | UninitialisedData
146 | ReadOnlyData16 -- .rodata.cst16 on x86_64, 16-byte aligned
147 | OtherSection String
148
149 data CmmStatic
150 = CmmStaticLit CmmLit
151 -- a literal value, size given by cmmLitRep of the literal.
152 | CmmUninitialised Int
153 -- uninitialised data, N bytes long
154 | CmmString [Word8]
155 -- string of 8-bit values only, not zero terminated.
156
157 data CmmStatics
158 = Statics
159 CLabel -- Label of statics
160 [CmmStatic] -- The static data itself
161