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