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