refactoring only: use the parameterised InstalledPackageInfo
[ghc-hetmet.git] / compiler / basicTypes / Unique.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5
6 @Uniques@ are used to distinguish entities in the compiler (@Ids@,
7 @Classes@, etc.) from each other.  Thus, @Uniques@ are the basic
8 comparison key in the compiler.
9
10 If there is any single operation that needs to be fast, it is @Unique@
11 comparison.  Unsurprisingly, there is quite a bit of huff-and-puff
12 directed to that end.
13
14 Some of the other hair in this code is to be able to use a
15 ``splittable @UniqueSupply@'' if requested/possible (not standard
16 Haskell).
17
18 \begin{code}
19 {-# OPTIONS -w #-}
20 -- The above warning supression flag is a temporary kludge.
21 -- While working on this module you are encouraged to remove it and fix
22 -- any warnings in the module. See
23 --     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
24 -- for details
25
26 module Unique (
27         Unique, Uniquable(..), hasKey,
28
29         pprUnique, 
30
31         mkUnique,                       -- Used in UniqSupply
32         mkUniqueGrimily,                -- Used in UniqSupply only!
33         getKey, getKey#,                -- Used in Var, UniqFM, Name only!
34
35         incrUnique,                     -- Used for renumbering
36         deriveUnique,                   -- Ditto
37         newTagUnique,                   -- Used in CgCase
38         initTyVarUnique,
39
40         isTupleKey, 
41
42         -- now all the built-in Uniques (and functions to make them)
43         -- [the Oh-So-Wonderful Haskell module system wins again...]
44         mkAlphaTyVarUnique,
45         mkPrimOpIdUnique,
46         mkTupleTyConUnique, mkTupleDataConUnique,
47         mkPreludeMiscIdUnique, mkPreludeDataConUnique,
48         mkPreludeTyConUnique, mkPreludeClassUnique,
49         mkPArrDataConUnique,
50
51         mkBuiltinUnique,
52         mkPseudoUniqueC,
53         mkPseudoUniqueD,
54         mkPseudoUniqueE,
55         mkPseudoUniqueH
56     ) where
57
58 #include "HsVersions.h"
59
60 import BasicTypes
61 import FastString
62 import Outputable
63
64 import GHC.Exts
65 import Data.Char        ( chr, ord )
66 \end{code}
67
68 %************************************************************************
69 %*                                                                      *
70 \subsection[Unique-type]{@Unique@ type and operations}
71 %*                                                                      *
72 %************************************************************************
73
74 The @Chars@ are ``tag letters'' that identify the @UniqueSupply@.
75 Fast comparison is everything on @Uniques@:
76
77 \begin{code}
78 data Unique = MkUnique Int#
79 \end{code}
80
81 Now come the functions which construct uniques from their pieces, and vice versa.
82 The stuff about unique *supplies* is handled further down this module.
83
84 \begin{code}
85 mkUnique        :: Char -> Int -> Unique        -- Builds a unique from pieces
86 unpkUnique      :: Unique -> (Char, Int)        -- The reverse
87
88 mkUniqueGrimily :: Int -> Unique                -- A trap-door for UniqSupply
89 getKey          :: Unique -> Int                -- for Var
90 getKey#         :: Unique -> Int#               -- for Var
91
92 incrUnique      :: Unique -> Unique
93 deriveUnique    :: Unique -> Int -> Unique
94 newTagUnique    :: Unique -> Char -> Unique
95
96 isTupleKey      :: Unique -> Bool
97 \end{code}
98
99
100 \begin{code}
101 mkUniqueGrimily (I# x) = MkUnique x
102
103 {-# INLINE getKey #-}
104 getKey (MkUnique x) = I# x
105 {-# INLINE getKey# #-}
106 getKey# (MkUnique x) = x
107
108 incrUnique (MkUnique i) = MkUnique (i +# 1#)
109
110 -- deriveUnique uses an 'X' tag so that it won't clash with
111 -- any of the uniques produced any other way
112 deriveUnique (MkUnique i) delta = mkUnique 'X' (I# i + delta)
113
114 -- newTagUnique changes the "domain" of a unique to a different char
115 newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u
116
117 -- pop the Char in the top 8 bits of the Unique(Supply)
118
119 -- No 64-bit bugs here, as long as we have at least 32 bits. --JSM
120
121 w2i x = word2Int# x
122 i2w x = int2Word# x
123 i2w_s x = (x::Int#)
124
125 mkUnique (C# c) (I# i)
126   = MkUnique (w2i (tag `or#` bits))
127   where
128     tag  = i2w (ord# c) `uncheckedShiftL#` i2w_s 24#
129     bits = i2w i `and#` (i2w 16777215#){-``0x00ffffff''-}
130
131 unpkUnique (MkUnique u)
132   = let
133         tag = C# (chr# (w2i ((i2w u) `uncheckedShiftRL#` (i2w_s 24#))))
134         i   = I# (w2i ((i2w u) `and#` (i2w 16777215#){-``0x00ffffff''-}))
135     in
136     (tag, i)
137 \end{code}
138
139
140
141 %************************************************************************
142 %*                                                                      *
143 \subsection[Uniquable-class]{The @Uniquable@ class}
144 %*                                                                      *
145 %************************************************************************
146
147 \begin{code}
148 class Uniquable a where
149     getUnique :: a -> Unique
150
151 hasKey          :: Uniquable a => a -> Unique -> Bool
152 x `hasKey` k    = getUnique x == k
153
154 instance Uniquable FastString where
155  getUnique fs = mkUniqueGrimily (I# (uniqueOfFS fs))
156
157 instance Uniquable Int where
158  getUnique i = mkUniqueGrimily i
159 \end{code}
160
161
162 %************************************************************************
163 %*                                                                      *
164 \subsection[Unique-instances]{Instance declarations for @Unique@}
165 %*                                                                      *
166 %************************************************************************
167
168 And the whole point (besides uniqueness) is fast equality.  We don't
169 use `deriving' because we want {\em precise} control of ordering
170 (equality on @Uniques@ is v common).
171
172 \begin{code}
173 eqUnique (MkUnique u1) (MkUnique u2) = u1 ==# u2
174 ltUnique (MkUnique u1) (MkUnique u2) = u1 <#  u2
175 leUnique (MkUnique u1) (MkUnique u2) = u1 <=# u2
176
177 cmpUnique (MkUnique u1) (MkUnique u2)
178   = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
179
180 instance Eq Unique where
181     a == b = eqUnique a b
182     a /= b = not (eqUnique a b)
183
184 instance Ord Unique where
185     a  < b = ltUnique a b
186     a <= b = leUnique a b
187     a  > b = not (leUnique a b)
188     a >= b = not (ltUnique a b)
189     compare a b = cmpUnique a b
190
191 -----------------
192 instance Uniquable Unique where
193     getUnique u = u
194 \end{code}
195
196 We do sometimes make strings with @Uniques@ in them:
197 \begin{code}
198 pprUnique :: Unique -> SDoc
199 pprUnique uniq
200   = case unpkUnique uniq of
201       (tag, u) -> finish_ppr tag u (text (iToBase62 u))
202
203 #ifdef UNUSED
204 pprUnique10 :: Unique -> SDoc
205 pprUnique10 uniq        -- in base-10, dudes
206   = case unpkUnique uniq of
207       (tag, u) -> finish_ppr tag u (int u)
208 #endif
209
210 finish_ppr 't' u pp_u | u < 26
211   =     -- Special case to make v common tyvars, t1, t2, ...
212         -- come out as a, b, ... (shorter, easier to read)
213     char (chr (ord 'a' + u))
214 finish_ppr tag u pp_u = char tag <> pp_u
215
216 instance Outputable Unique where
217     ppr u = pprUnique u
218
219 instance Show Unique where
220     showsPrec p uniq = showsPrecSDoc p (pprUnique uniq)
221 \end{code}
222
223 %************************************************************************
224 %*                                                                      *
225 \subsection[Utils-base62]{Base-62 numbers}
226 %*                                                                      *
227 %************************************************************************
228
229 A character-stingy way to read/write numbers (notably Uniques).
230 The ``62-its'' are \tr{[0-9a-zA-Z]}.  We don't handle negative Ints.
231 Code stolen from Lennart.
232
233 \begin{code}
234 iToBase62 :: Int -> String
235 iToBase62 n@(I# n#) 
236   = ASSERT(n >= 0) go n# ""
237   where
238     go n# cs | n# <# 62# 
239              = case (indexCharOffAddr# chars62# n#) of { c# -> C# c# : cs }
240              | otherwise
241              =  case (quotRem (I# n#) 62)            of { (I# q#, I# r#) ->
242                 case (indexCharOffAddr# chars62# r#) of { c#  ->
243                 go q# (C# c# : cs) }}
244
245     chars62# = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
246 \end{code}
247
248 %************************************************************************
249 %*                                                                      *
250 \subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
251 %*                                                                      *
252 %************************************************************************
253
254 Allocation of unique supply characters:
255         v,t,u : for renumbering value-, type- and usage- vars.
256         B:   builtin
257         C-E: pseudo uniques     (used in native-code generator)
258         X:   uniques derived by deriveUnique
259         _:   unifiable tyvars   (above)
260         0-9: prelude things below
261
262         other a-z: lower case chars for unique supplies.  Used so far:
263
264         d       desugarer
265         f       AbsC flattener
266         g       SimplStg
267         l       ndpFlatten
268         n       Native codegen
269         r       Hsc name cache
270         s       simplifier
271
272 \begin{code}
273 mkAlphaTyVarUnique i            = mkUnique '1' i
274
275 mkPreludeClassUnique i          = mkUnique '2' i
276
277 -- Prelude type constructors occupy *three* slots.
278 -- The first is for the tycon itself; the latter two
279 -- are for the generic to/from Ids.  See TysWiredIn.mk_tc_gen_info.
280
281 mkPreludeTyConUnique i          = mkUnique '3' (3*i)
282 mkTupleTyConUnique Boxed   a    = mkUnique '4' (3*a)
283 mkTupleTyConUnique Unboxed a    = mkUnique '5' (3*a)
284
285 -- Data constructor keys occupy *two* slots.  The first is used for the
286 -- data constructor itself and its wrapper function (the function that
287 -- evaluates arguments as necessary and calls the worker). The second is
288 -- used for the worker function (the function that builds the constructor
289 -- representation).
290
291 mkPreludeDataConUnique i        = mkUnique '6' (2*i)    -- Must be alphabetic
292 mkTupleDataConUnique Boxed a    = mkUnique '7' (2*a)    -- ditto (*may* be used in C labels)
293 mkTupleDataConUnique Unboxed a  = mkUnique '8' (2*a)
294
295 -- This one is used for a tiresome reason
296 -- to improve a consistency-checking error check in the renamer
297 isTupleKey u = case unpkUnique u of
298                 (tag,_) -> tag == '4' || tag == '5' || tag == '7' || tag == '8'
299
300 mkPrimOpIdUnique op             = mkUnique '9' op
301 mkPreludeMiscIdUnique i         = mkUnique '0' i
302
303 -- No numbers left anymore, so I pick something different for the character
304 -- tag 
305 mkPArrDataConUnique a           = mkUnique ':' (2*a)
306
307 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
308 -- See pprUnique for details
309
310 initTyVarUnique :: Unique
311 initTyVarUnique = mkUnique 't' 0
312
313 mkPseudoUniqueC, mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
314    mkBuiltinUnique :: Int -> Unique
315
316 mkBuiltinUnique i = mkUnique 'B' i
317 mkPseudoUniqueC i = mkUnique 'C' i -- used for getUnique on Regs
318 mkPseudoUniqueD i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
319 mkPseudoUniqueE i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
320 mkPseudoUniqueH i = mkUnique 'H' i -- used in NCG spiller to create spill VirtualRegs
321 \end{code}
322