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