Snapshot of codegen refactoring to share with simonpj
[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 {-# OPTIONS_GHC -fno-warn-incomplete-patterns #-}
6 #if __GLASGOW_HASKELL__ >= 703
7 -- GHC 7.0.1 improved incomplete pattern warnings with GADTs
8 {-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
9 #endif
10
11 module Cmm (
12 -- * Cmm top-level datatypes
13 CmmPgm, GenCmmPgm,
14 CmmTop, GenCmmTop(..),
15 CmmGraph, GenCmmGraph(..),
16 CmmBlock,
17 Section(..), CmmStatics(..), CmmStatic(..),
18
19 -- * Cmm graphs
20 CmmReplGraph, GenCmmReplGraph, CmmFwdRewrite, CmmBwdRewrite,
21
22 -- * Info Tables
23 CmmTopInfo(..), CmmStackInfo(..), CmmInfoTable(..),
24 ClosureTypeInfo(..),
25 C_SRT(..), needsSRT,
26 ProfilingInfo(..), ConstrDescription,
27
28 -- * Statements, expressions and types
29 module CmmNode,
30 module CmmExpr,
31 ) where
32
33 import CLabel
34 import BlockId
35 import CmmNode
36 import OptimizationFuel as F
37 import SMRep
38 import CmmExpr
39 import Compiler.Hoopl
40
41 import Data.Word ( Word8 )
42
43 #include "HsVersions.h"
44
45 -----------------------------------------------------------------------------
46 -- Cmm, GenCmm
47 -----------------------------------------------------------------------------
48
49 -- A file is a list of top-level chunks. These may be arbitrarily
50 -- re-orderd during code generation.
51
52 -- GenCmm is abstracted over
53 -- d, the type of static data elements in CmmData
54 -- h, the static info preceding the code of a CmmProc
55 -- g, the control-flow graph of a CmmProc
56 --
57 -- We expect there to be two main instances of this type:
58 -- (a) C--, i.e. populated with various C-- constructs
59 -- (Cmm and RawCmm in OldCmm.hs)
60 -- (b) Native code, populated with data/instructions
61 --
62 -- A second family of instances based on Hoopl is in Cmm.hs.
63 --
64 type GenCmmPgm d h g = [GenCmmTop d h g]
65
66 type CmmPgm = GenCmmPgm CmmStatics CmmTopInfo CmmGraph
67
68 -----------------------------------------------------------------------------
69 -- CmmTop, GenCmmTop
70 -----------------------------------------------------------------------------
71
72 -- | A top-level chunk, abstracted over the type of the contents of
73 -- the basic blocks (Cmm or instructions are the likely instantiations).
74 data GenCmmTop d h g
75 = CmmProc -- A procedure
76 h -- Extra header such as the info table
77 CLabel -- Entry label
78 g -- Control-flow graph for the procedure's code
79
80 | CmmData -- Static data
81 Section
82 d
83
84 type CmmTop = GenCmmTop CmmStatics CmmTopInfo CmmGraph
85
86 -----------------------------------------------------------------------------
87 -- Graphs
88 -----------------------------------------------------------------------------
89
90 type CmmGraph = GenCmmGraph CmmNode
91 data GenCmmGraph n = CmmGraph { g_entry :: BlockId, g_graph :: Graph n C C }
92 type CmmBlock = Block CmmNode C C
93
94 type CmmReplGraph e x = GenCmmReplGraph CmmNode e x
95 type GenCmmReplGraph n e x = FuelUniqSM (Maybe (Graph n e x))
96 type CmmFwdRewrite f = FwdRewrite FuelUniqSM CmmNode f
97 type CmmBwdRewrite f = BwdRewrite FuelUniqSM CmmNode f
98
99 -----------------------------------------------------------------------------
100 -- Info Tables
101 -----------------------------------------------------------------------------
102
103 data CmmTopInfo = TopInfo {info_tbl :: CmmInfoTable, stack_info :: CmmStackInfo}
104
105 data CmmStackInfo
106 = StackInfo {
107 arg_space :: ByteOff, -- XXX: comment?
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