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,
23 mkUnique, -- Used in UniqSupply
24 mkUniqueGrimily, -- Used in UniqSupply only!
25 getKey, getKey#, -- Used in Var, UniqFM, Name only!
27 incrUnique, -- Used for renumbering
28 deriveUnique, -- Ditto
29 newTagUnique, -- Used in CgCase
34 -- now all the built-in Uniques (and functions to make them)
35 -- [the Oh-So-Wonderful Haskell module system wins again...]
38 mkTupleTyConUnique, mkTupleDataConUnique,
39 mkPreludeMiscIdUnique, mkPreludeDataConUnique,
40 mkPreludeTyConUnique, mkPreludeClassUnique,
50 #include "HsVersions.h"
52 import BasicTypes ( Boxity(..) )
53 import PackageConfig ( PackageId, packageIdFS )
54 import FastString ( FastString, uniqueOfFS )
60 import Char ( chr, ord )
63 %************************************************************************
65 \subsection[Unique-type]{@Unique@ type and operations}
67 %************************************************************************
69 The @Chars@ are ``tag letters'' that identify the @UniqueSupply@.
70 Fast comparison is everything on @Uniques@:
73 data Unique = MkUnique Int#
76 Now come the functions which construct uniques from their pieces, and vice versa.
77 The stuff about unique *supplies* is handled further down this module.
80 mkUnique :: Char -> Int -> Unique -- Builds a unique from pieces
81 unpkUnique :: Unique -> (Char, Int) -- The reverse
83 mkUniqueGrimily :: Int -> Unique -- A trap-door for UniqSupply
84 getKey :: Unique -> Int -- for Var
85 getKey# :: Unique -> Int# -- for Var
87 incrUnique :: Unique -> Unique
88 deriveUnique :: Unique -> Int -> Unique
89 newTagUnique :: Unique -> Char -> Unique
91 isTupleKey :: Unique -> Bool
96 mkUniqueGrimily (I# x) = MkUnique x
99 getKey (MkUnique x) = I# x
100 {-# INLINE getKey# #-}
101 getKey# (MkUnique x) = x
103 incrUnique (MkUnique i) = MkUnique (i +# 1#)
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)
109 -- newTagUnique changes the "domain" of a unique to a different char
110 newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u
112 -- pop the Char in the top 8 bits of the Unique(Supply)
114 -- No 64-bit bugs here, as long as we have at least 32 bits. --JSM
120 mkUnique (C# c) (I# i)
121 = MkUnique (w2i (tag `or#` bits))
123 #if __GLASGOW_HASKELL__ >= 503
124 tag = i2w (ord# c) `uncheckedShiftL#` i2w_s 24#
126 tag = i2w (ord# c) `shiftL#` i2w_s 24#
128 bits = i2w i `and#` (i2w 16777215#){-``0x00ffffff''-}
130 unpkUnique (MkUnique u)
132 tag = C# (chr# (w2i ((i2w u) `shiftr` (i2w_s 24#))))
133 i = I# (w2i ((i2w u) `and#` (i2w 16777215#){-``0x00ffffff''-}))
137 #if __GLASGOW_HASKELL__ >= 503
138 shiftr x y = uncheckedShiftRL# x y
140 shiftr x y = shiftRL# x y
146 %************************************************************************
148 \subsection[Uniquable-class]{The @Uniquable@ class}
150 %************************************************************************
153 class Uniquable a where
154 getUnique :: a -> Unique
156 hasKey :: Uniquable a => a -> Unique -> Bool
157 x `hasKey` k = getUnique x == k
159 instance Uniquable FastString where
160 getUnique fs = mkUniqueGrimily (I# (uniqueOfFS fs))
162 instance Uniquable PackageId where
163 getUnique pid = getUnique (packageIdFS pid)
165 instance Uniquable Int where
166 getUnique i = mkUniqueGrimily i
170 %************************************************************************
172 \subsection[Unique-instances]{Instance declarations for @Unique@}
174 %************************************************************************
176 And the whole point (besides uniqueness) is fast equality. We don't
177 use `deriving' because we want {\em precise} control of ordering
178 (equality on @Uniques@ is v common).
181 eqUnique (MkUnique u1) (MkUnique u2) = u1 ==# u2
182 ltUnique (MkUnique u1) (MkUnique u2) = u1 <# u2
183 leUnique (MkUnique u1) (MkUnique u2) = u1 <=# u2
185 cmpUnique (MkUnique u1) (MkUnique u2)
186 = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
188 instance Eq Unique where
189 a == b = eqUnique a b
190 a /= b = not (eqUnique a b)
192 instance Ord Unique where
194 a <= b = leUnique a b
195 a > b = not (leUnique a b)
196 a >= b = not (ltUnique a b)
197 compare a b = cmpUnique a b
200 instance Uniquable Unique where
204 We do sometimes make strings with @Uniques@ in them:
206 pprUnique :: Unique -> SDoc
208 = case unpkUnique uniq of
209 (tag, u) -> finish_ppr tag u (text (iToBase62 u))
212 pprUnique10 :: Unique -> SDoc
213 pprUnique10 uniq -- in base-10, dudes
214 = case unpkUnique uniq of
215 (tag, u) -> finish_ppr tag u (int u)
218 finish_ppr 't' u pp_u | u < 26
219 = -- Special case to make v common tyvars, t1, t2, ...
220 -- come out as a, b, ... (shorter, easier to read)
221 char (chr (ord 'a' + u))
222 finish_ppr tag u pp_u = char tag <> pp_u
224 instance Outputable Unique where
227 instance Show Unique where
228 showsPrec p uniq = showsPrecSDoc p (pprUnique uniq)
231 %************************************************************************
233 \subsection[Utils-base62]{Base-62 numbers}
235 %************************************************************************
237 A character-stingy way to read/write numbers (notably Uniques).
238 The ``62-its'' are \tr{[0-9a-zA-Z]}. We don't handle negative Ints.
239 Code stolen from Lennart.
242 iToBase62 :: Int -> String
244 = ASSERT(n >= 0) go n# ""
247 = case (indexCharOffAddr# chars62# n#) of { c# -> C# c# : cs }
249 = case (quotRem (I# n#) 62) of { (I# q#, I# r#) ->
250 case (indexCharOffAddr# chars62# r#) of { c# ->
251 go q# (C# c# : cs) }}
253 chars62# = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
256 %************************************************************************
258 \subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
260 %************************************************************************
262 Allocation of unique supply characters:
263 v,t,u : for renumbering value-, type- and usage- vars.
265 C-E: pseudo uniques (used in native-code generator)
266 X: uniques derived by deriveUnique
267 _: unifiable tyvars (above)
268 0-9: prelude things below
270 other a-z: lower case chars for unique supplies. Used so far:
281 mkAlphaTyVarUnique i = mkUnique '1' i
283 mkPreludeClassUnique i = mkUnique '2' i
285 -- Prelude type constructors occupy *three* slots.
286 -- The first is for the tycon itself; the latter two
287 -- are for the generic to/from Ids. See TysWiredIn.mk_tc_gen_info.
289 mkPreludeTyConUnique i = mkUnique '3' (3*i)
290 mkTupleTyConUnique Boxed a = mkUnique '4' (3*a)
291 mkTupleTyConUnique Unboxed a = mkUnique '5' (3*a)
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
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)
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'
308 mkPrimOpIdUnique op = mkUnique '9' op
309 mkPreludeMiscIdUnique i = mkUnique '0' i
311 -- No numbers left anymore, so I pick something different for the character
313 mkPArrDataConUnique a = mkUnique ':' (2*a)
315 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
316 -- See pprUnique for details
318 initTyVarUnique :: Unique
319 initTyVarUnique = mkUnique 't' 0
321 mkPseudoUniqueC, mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
322 mkBuiltinUnique :: Int -> Unique
324 mkBuiltinUnique i = mkUnique 'B' i
325 mkPseudoUniqueC i = mkUnique 'C' i -- used for getUnique on Regs
326 mkPseudoUniqueD i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
327 mkPseudoUniqueE i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
328 mkPseudoUniqueH i = mkUnique 'H' i -- used in NCG spiller to create spill VirtualRegs