2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
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.
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
13 Some of the other hair in this code is to be able to use a
14 ``splittable @UniqueSupply@'' if requested/possible (not standard
19 Unique, Uniquable(..), hasKey,
21 pprUnique, pprUnique10,
23 mkUnique, -- Used in UniqSupply
24 mkUniqueGrimily, -- Used in UniqSupply only!
25 getKey, getKey#, -- Used in Var, UniqFM, Name only!
28 incrUnique, -- Used for renumbering
29 deriveUnique, -- Ditto
30 newTagUnique, -- Used in CgCase
36 -- now all the built-in Uniques (and functions to make them)
37 -- [the Oh-So-Wonderful Haskell module system wins again...]
40 mkTupleTyConUnique, mkTupleDataConUnique,
41 mkPreludeMiscIdUnique, mkPreludeDataConUnique,
42 mkPreludeTyConUnique, mkPreludeClassUnique,
45 mkBuiltinUnique, builtinUniques,
46 mkPseudoUnique1, mkPseudoUnique2, mkPseudoUnique3
49 #include "HsVersions.h"
51 import BasicTypes ( Boxity(..) )
52 import FastString ( FastString, uniqueOfFS )
58 import Char ( chr, ord )
61 %************************************************************************
63 \subsection[Unique-type]{@Unique@ type and operations}
65 %************************************************************************
67 The @Chars@ are ``tag letters'' that identify the @UniqueSupply@.
68 Fast comparison is everything on @Uniques@:
71 data Unique = MkUnique Int#
74 Now come the functions which construct uniques from their pieces, and vice versa.
75 The stuff about unique *supplies* is handled further down this module.
78 mkUnique :: Char -> Int -> Unique -- Builds a unique from pieces
79 unpkUnique :: Unique -> (Char, Int) -- The reverse
81 mkUniqueGrimily :: Int -> Unique -- A trap-door for UniqSupply
82 getKey :: Unique -> Int -- for Var
83 getKey# :: Unique -> Int# -- for Var
85 incrUnique :: Unique -> Unique
86 deriveUnique :: Unique -> Int -> Unique
87 newTagUnique :: Unique -> Char -> Unique
89 isTupleKey :: Unique -> Bool
94 mkUniqueGrimily (I# x) = MkUnique x
97 getKey (MkUnique x) = I# x
98 {-# INLINE getKey# #-}
99 getKey# (MkUnique x) = x
101 incrUnique (MkUnique i) = MkUnique (i +# 1#)
103 -- deriveUnique uses an 'X' tag so that it won't clash with
104 -- any of the uniques produced any other way
105 deriveUnique (MkUnique i) delta = mkUnique 'X' (I# i + delta)
107 -- newTagUnique changes the "domain" of a unique to a different char
108 newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u
110 -- pop the Char in the top 8 bits of the Unique(Supply)
112 -- No 64-bit bugs here, as long as we have at least 32 bits. --JSM
118 mkUnique (C# c) (I# i)
119 = MkUnique (w2i (tag `or#` bits))
121 #if __GLASGOW_HASKELL__ >= 503
122 tag = i2w (ord# c) `uncheckedShiftL#` i2w_s 24#
124 tag = i2w (ord# c) `shiftL#` i2w_s 24#
126 bits = i2w i `and#` (i2w 16777215#){-``0x00ffffff''-}
128 unpkUnique (MkUnique u)
130 tag = C# (chr# (w2i ((i2w u) `shiftr` (i2w_s 24#))))
131 i = I# (w2i ((i2w u) `and#` (i2w 16777215#){-``0x00ffffff''-}))
135 #if __GLASGOW_HASKELL__ >= 503
136 shiftr x y = uncheckedShiftRL# x y
138 shiftr x y = shiftRL# x y
144 %************************************************************************
146 \subsection[Uniquable-class]{The @Uniquable@ class}
148 %************************************************************************
151 class Uniquable a where
152 getUnique :: a -> Unique
154 hasKey :: Uniquable a => a -> Unique -> Bool
155 x `hasKey` k = getUnique x == k
157 instance Uniquable FastString where
158 getUnique fs = mkUniqueGrimily (I# (uniqueOfFS fs))
160 instance Uniquable Int where
161 getUnique i = mkUniqueGrimily i
165 %************************************************************************
167 \subsection[Unique-instances]{Instance declarations for @Unique@}
169 %************************************************************************
171 And the whole point (besides uniqueness) is fast equality. We don't
172 use `deriving' because we want {\em precise} control of ordering
173 (equality on @Uniques@ is v common).
176 eqUnique (MkUnique u1) (MkUnique u2) = u1 ==# u2
177 ltUnique (MkUnique u1) (MkUnique u2) = u1 <# u2
178 leUnique (MkUnique u1) (MkUnique u2) = u1 <=# u2
180 cmpUnique (MkUnique u1) (MkUnique u2)
181 = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
183 instance Eq Unique where
184 a == b = eqUnique a b
185 a /= b = not (eqUnique a b)
187 instance Ord Unique where
189 a <= b = leUnique a b
190 a > b = not (leUnique a b)
191 a >= b = not (ltUnique a b)
192 compare a b = cmpUnique a b
195 instance Uniquable Unique where
199 We do sometimes make strings with @Uniques@ in them:
201 pprUnique, pprUnique10 :: Unique -> SDoc
204 = case unpkUnique uniq of
205 (tag, u) -> finish_ppr tag u (iToBase62 u)
207 pprUnique10 uniq -- in base-10, dudes
208 = case unpkUnique uniq of
209 (tag, u) -> finish_ppr tag u (int u)
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
217 instance Outputable Unique where
220 instance Show Unique where
221 showsPrec p uniq = showsPrecSDoc p (pprUnique uniq)
224 %************************************************************************
226 \subsection[Utils-base62]{Base-62 numbers}
228 %************************************************************************
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.
235 iToBase62 :: Int -> SDoc
240 case (indexCharOffAddr# chars62# n#) of { c ->
243 case (quotRem n 62) of { (q, I# r#) ->
244 case (indexCharOffAddr# chars62# r#) of { c ->
245 (<>) (iToBase62 q) (char (C# c)) }}
247 chars62# = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
250 %************************************************************************
252 \subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
254 %************************************************************************
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)
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
266 mkAlphaTyVarUnique i = mkUnique '1' i
268 mkPreludeClassUnique i = mkUnique '2' i
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.
274 mkPreludeTyConUnique i = mkUnique '3' (3*i)
275 mkTupleTyConUnique Boxed a = mkUnique '4' (3*a)
276 mkTupleTyConUnique Unboxed a = mkUnique '5' (3*a)
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
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)
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'
293 mkPrimOpIdUnique op = mkUnique '9' op
294 mkPreludeMiscIdUnique i = mkUnique '0' i
296 -- No numbers left anymore, so I pick something different for the character
298 mkPArrDataConUnique a = mkUnique ':' (2*a)
300 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
301 -- See pprUnique for details
303 initTyVarUnique :: Unique
304 initTyVarUnique = mkUnique 't' 0
306 initTidyUniques :: (Unique, Unique) -- Global and local
307 initTidyUniques = (mkUnique 'g' 0, mkUnique 'x' 0)
309 mkPseudoUnique1, mkPseudoUnique2, mkPseudoUnique3,
310 mkBuiltinUnique :: Int -> Unique
312 builtinUniques :: [Unique]
313 builtinUniques = map mkBuiltinUnique [1..]
315 mkBuiltinUnique i = mkUnique 'B' i
316 mkPseudoUnique1 i = mkUnique 'C' i -- used for getUnique on Regs
317 mkPseudoUnique2 i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
318 mkPseudoUnique3 i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs