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