[project @ 1996-01-11 14:06:51 by partain]
[ghc-hetmet.git] / ghc / compiler / codeGen / SMRep.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
3 %
4 \section[SMRep]{Storage manager representations of closure}
5
6 This is here, rather than in ClosureInfo, just to keep nhc happy.
7 Other modules should access this info through ClosureInfo.
8
9 \begin{code}
10 #include "HsVersions.h"
11
12 module SMRep (
13         SMRep(..), SMSpecRepKind(..), SMUpdateKind(..),
14         getSMInfoStr, getSMInitHdrStr, getSMUpdInplaceHdrStr,
15         ltSMRepHdr -- UNUSED, equivSMRepHdr
16     ) where
17
18 import Outputable
19 import Pretty
20 import Util
21 \end{code}
22
23 %************************************************************************
24 %*                                                                      *
25 \subsubsection[SMRep-datatype]{@SMRep@---storage manager representation}
26 %*                                                                      *
27 %************************************************************************
28
29 Ways in which a closure may be represented by the storage manager;
30 this list slavishly follows the storage-manager interface document.
31
32 \begin{code}
33 data SMSpecRepKind
34   = SpecRep             -- Normal Spec representation
35
36   | ConstantRep         -- Common me up with single global copy
37                         -- Used for nullary constructors
38
39   | CharLikeRep         -- Common me up with entry from global table
40
41   | IntLikeRep          -- Common me up with entry from global table,
42                         -- if the intlike field is in range.
43
44 data SMUpdateKind
45   = SMNormalForm        -- Normal form, no update
46   | SMSingleEntry       -- Single entry thunk, non-updatable
47   | SMUpdatable         -- Shared thunk, updatable
48
49 data SMRep
50   = StaticRep           -- Don't move me, Oh garbage collector!
51                         -- Used for all statically-allocated closures.
52         Int             -- # ptr words (useful for interpreter, debugger, etc)
53         Int             -- # non-ptr words
54
55   | SpecialisedRep      -- GC routines know size etc
56                         -- All have same _HS = SPEC_HS and no _VHS
57         SMSpecRepKind   -- Which kind of specialised representation
58         Int             -- # ptr words
59         Int             -- # non-ptr words
60         SMUpdateKind    -- Updatable?
61
62   | GenericRep          -- GC routines consult sizes in info tbl
63         Int             -- # ptr words
64         Int             -- # non-ptr words
65         SMUpdateKind    -- Updatable?
66
67   | BigTupleRep         -- All ptrs, size in var-hdr field
68                         -- Used for big tuples
69         Int             -- # ptr words
70
71   | DataRep             -- All non-ptrs, size in var-hdr field
72                         -- Used for arbitrary-precision integers, strings
73         Int             -- # non-ptr words
74
75   | DynamicRep          -- Size and # ptrs in var-hdr field
76                         -- Used by RTS for partial applications
77
78   | BlackHoleRep        -- for black hole closures
79
80   | PhantomRep          -- for "phantom" closures that only exist in registers
81
82   | MuTupleRep          -- All ptrs, size in var-hdr field
83                         -- Used for mutable tuples
84         Int             -- # ptr words
85
86 {- Mattson review:
87
88 To: simonpj@dcs.gla.ac.uk, partain@dcs.gla.ac.uk
89 Cc: kh@dcs.gla.ac.uk, trinder@dcs.gla.ac.uk, areid@dcs.gla.ac.uk
90 Subject: Correct me if I'm wrong...
91 Date: Fri, 17 Feb 1995 18:09:00 +0000
92 From: Jim Mattson <mattson@dcs.gla.ac.uk>
93
94 BigTupleRep == TUPLE
95
96     Never generated by the compiler, and only used in the RTS when
97     mutuples don't require special attention at GC time (e.g. 2s)
98     When it is used, it is a primitive object (never entered).
99     May be mutable...probably should never be used in the parallel 
100     system, since we need to distinguish mutables from immutables when
101     deciding whether to copy or move closures across processors.
102
103 DataRep == DATA (aka MutableByteArray & ByteArray)
104     Never generated by the compiler, and only used in the RTS for
105     ArrayOfData.  Always a primitive object (never entered).  May
106     be mutable...though we don't distinguish between mutable and
107     immutable data arrays in the sequential world, it would probably
108     be useful in the parallel world to know when it is safe to just
109     copy one of these.  I believe the hooks are in place for changing
110     the InfoPtr on a MutableByteArray when it's frozen to a ByteArray
111     if we want to do so.
112
113 DynamicRep == DYN
114     Never generated by the compiler, and only used in the RTS for
115     PAPs and the Stable Pointer table.  PAPs are non-primitive,
116     non-updatable, normal-form objects, but the SPT is a primitive,
117     mutable object.  At the moment, there is no SPT in the parallel
118     world.  Presumably, it would be possible to have an SPT on each
119     processor, and we could identify a stable pointer as a (processor,
120     SPT-entry) pair, but would it be worth it?
121
122 MuTupleRep == MUTUPLE
123     Never generated by the compiler, and only used in the RTS when
124     mutuples *do* require special attention at GC time.
125     When it is used, it is a primitive object (never entered).
126     Always mutable...there is an IMMUTUPLE in the RTS, but no
127     corresponding type in the compiler.
128
129 --jim
130 -}
131
132 instance Eq SMRep where
133     (SpecialisedRep k1 a1 b1 _) == (SpecialisedRep k2 a2 b2 _) = (tagOf_SMSpecRepKind k1) _EQ_ (tagOf_SMSpecRepKind k2)
134                                                                && a1 == a2 && b1 == b2
135     (GenericRep a1 b1 _)      == (GenericRep a2 b2 _)      = a1 == a2 && b1 == b2
136     (BigTupleRep a1)          == (BigTupleRep a2)          = a1 == a2
137     (MuTupleRep a1)           == (MuTupleRep a2)           = a1 == a2
138     (DataRep a1)              == (DataRep a2)              = a1 == a2
139     a                         == b                         = (tagOf_SMRep a) _EQ_ (tagOf_SMRep b)
140
141 {- UNUSED:
142 equivSMRepHdr :: SMRep -> SMRep -> Bool
143 a `equivSMRepHdr` b = (tagOf_SMRep a) _EQ_ (tagOf_SMRep b)
144 -}
145
146 ltSMRepHdr :: SMRep -> SMRep -> Bool
147 a `ltSMRepHdr` b = (tagOf_SMRep a) _LT_ (tagOf_SMRep b)
148
149 instance Ord SMRep where
150     -- ToDo: cmp-ify?  This instance seems a bit weird (WDP 94/10)
151     rep1 <= rep2 = rep1 < rep2 || rep1 == rep2
152     rep1 < rep2
153       = let tag1 = tagOf_SMRep rep1
154             tag2 = tagOf_SMRep rep2
155         in
156         if      tag1 _LT_ tag2 then True
157         else if tag1 _GT_ tag2 then False
158         else {- tags equal -}    rep1 `lt` rep2
159       where
160         (SpecialisedRep k1 a1 b1 _) `lt` (SpecialisedRep k2 a2 b2 _) =
161                 t1 _LT_ t2 || (t1 _EQ_ t2 && (a1 < a2 || (a1 == a2 && b1 < b2)))
162                 where t1 = tagOf_SMSpecRepKind k1
163                       t2 = tagOf_SMSpecRepKind k2
164         (GenericRep a1 b1 _)      `lt` (GenericRep a2 b2 _)      = a1 < a2 || (a1 == a2 && b1 < b2)
165         (BigTupleRep a1)          `lt` (BigTupleRep a2)          = a1 < a2
166         (MuTupleRep a1)           `lt` (MuTupleRep a2)           = a1 < a2
167         (DataRep a1)              `lt` (DataRep a2)              = a1 < a2
168         a                         `lt` b                         = True
169
170 tagOf_SMSpecRepKind SpecRep     = (ILIT(1) :: FAST_INT)
171 tagOf_SMSpecRepKind ConstantRep = ILIT(2)
172 tagOf_SMSpecRepKind CharLikeRep = ILIT(3)
173 tagOf_SMSpecRepKind IntLikeRep  = ILIT(4)
174
175 tagOf_SMRep (StaticRep _ _)          = (ILIT(1) :: FAST_INT)
176 tagOf_SMRep (SpecialisedRep k _ _ _) = ILIT(2)
177 tagOf_SMRep (GenericRep _ _ _)       = ILIT(3)
178 tagOf_SMRep (BigTupleRep _)          = ILIT(4)
179 tagOf_SMRep (DataRep _)              = ILIT(5)
180 tagOf_SMRep DynamicRep               = ILIT(6)
181 tagOf_SMRep BlackHoleRep             = ILIT(7)
182 tagOf_SMRep PhantomRep               = ILIT(8)
183 tagOf_SMRep (MuTupleRep _)           = ILIT(9)
184
185 instance Text SMRep where
186     showsPrec d rep
187       = showString (case rep of
188            StaticRep _ _                         -> "STATIC"
189            SpecialisedRep kind _ _ SMNormalForm  -> "SPEC_N"
190            SpecialisedRep kind _ _ SMSingleEntry -> "SPEC_S"
191            SpecialisedRep kind _ _ SMUpdatable   -> "SPEC_U"
192            GenericRep _ _ SMNormalForm           -> "GEN_N"
193            GenericRep _ _ SMSingleEntry          -> "GEN_S"
194            GenericRep _ _ SMUpdatable            -> "GEN_U"
195            BigTupleRep _                         -> "TUPLE"
196            DataRep       _                       -> "DATA"
197            DynamicRep                            -> "DYN"
198            BlackHoleRep                          -> "BH"
199            PhantomRep                            -> "INREGS"
200            MuTupleRep _                          -> "MUTUPLE")
201
202 instance Outputable SMRep where
203     ppr sty rep = ppStr (show rep)
204
205 getSMInfoStr :: SMRep -> String
206 getSMInfoStr (StaticRep _ _)                            = "STATIC"
207 getSMInfoStr (SpecialisedRep ConstantRep _ _ _)         = "CONST"
208 getSMInfoStr (SpecialisedRep CharLikeRep _ _ _)         = "CHARLIKE"
209 getSMInfoStr (SpecialisedRep IntLikeRep _ _ _)          = "INTLIKE"
210 getSMInfoStr (SpecialisedRep SpecRep _ _ SMNormalForm)  = "SPEC_N"
211 getSMInfoStr (SpecialisedRep SpecRep _ _ SMSingleEntry) = "SPEC_S"
212 getSMInfoStr (SpecialisedRep SpecRep _ _ SMUpdatable)   = "SPEC_U"
213 getSMInfoStr (GenericRep _ _ SMNormalForm)              = "GEN_N"
214 getSMInfoStr (GenericRep _ _ SMSingleEntry)             = "GEN_S"
215 getSMInfoStr (GenericRep _ _ SMUpdatable)               = "GEN_U"
216 getSMInfoStr (BigTupleRep _)                            = "TUPLE"
217 getSMInfoStr (DataRep _ )                               = "DATA"
218 getSMInfoStr DynamicRep                                 = "DYN"
219 getSMInfoStr BlackHoleRep                               = panic "getSMInfoStr.BlackHole"
220 getSMInfoStr PhantomRep                                 = "INREGS"
221 getSMInfoStr (MuTupleRep _)                             = "MUTUPLE"
222
223 getSMInitHdrStr :: SMRep -> String
224 getSMInitHdrStr (SpecialisedRep IntLikeRep _ _ _)  = "SET_INTLIKE"
225 getSMInitHdrStr (SpecialisedRep SpecRep _ _ _)     = "SET_SPEC"
226 getSMInitHdrStr (GenericRep _ _ _)                 = "SET_GEN"
227 getSMInitHdrStr (BigTupleRep _)                    = "SET_TUPLE"
228 getSMInitHdrStr (DataRep _ )                       = "SET_DATA"
229 getSMInitHdrStr DynamicRep                         = "SET_DYN"
230 getSMInitHdrStr BlackHoleRep                       = "SET_BH"
231 #ifdef DEBUG
232 getSMInitHdrStr (StaticRep _ _)                    = panic "getSMInitHdrStr.Static"
233 getSMInitHdrStr PhantomRep                         = panic "getSMInitHdrStr.Phantom"
234 getSMInitHdrStr (MuTupleRep _)                     = panic "getSMInitHdrStr.Mutuple"
235 getSMInitHdrStr (SpecialisedRep ConstantRep _ _ _) = panic "getSMInitHdrStr.Constant"
236 getSMInitHdrStr (SpecialisedRep CharLikeRep _ _ _) = panic "getSMInitHdrStr.CharLike"
237 #endif
238
239 getSMUpdInplaceHdrStr :: SMRep -> String
240 getSMUpdInplaceHdrStr (SpecialisedRep ConstantRep _ _ _) = "INPLACE_UPD"
241 getSMUpdInplaceHdrStr (SpecialisedRep CharLikeRep _ _ _) = "INPLACE_UPD"
242 getSMUpdInplaceHdrStr (SpecialisedRep IntLikeRep _ _ _)  = "INPLACE_UPD"
243 getSMUpdInplaceHdrStr (SpecialisedRep SpecRep _ _ _)     = "INPLACE_UPD"
244 #ifdef DEBUG
245 getSMUpdInplaceHdrStr (StaticRep _ _)                    = panic "getSMUpdInplaceHdrStr.Static"
246 getSMUpdInplaceHdrStr (GenericRep _ _ _)                 = panic "getSMUpdInplaceHdrStr.Generic"
247 getSMUpdInplaceHdrStr (BigTupleRep _ )                   = panic "getSMUpdInplaceHdrStr.BigTuple"
248 getSMUpdInplaceHdrStr (DataRep _ )                       = panic "getSMUpdInplaceHdrStr.Data"
249 getSMUpdInplaceHdrStr DynamicRep                         = panic "getSMUpdInplaceHdrStr.Dynamic"
250 getSMUpdInplaceHdrStr BlackHoleRep                       = panic "getSMUpdInplaceHdrStr.BlackHole"
251 getSMUpdInplaceHdrStr PhantomRep                         = panic "getSMUpdInplaceHdrStr.Phantom"
252 getSMUpdInplaceHdrStr (MuTupleRep _ )                    = panic "getSMUpdInplaceHdrStr.MuTuple"
253 #endif
254 \end{code}