2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
6 @Uniques@ are used to distinguish entities in the compiler (@Ids@,
7 @Classes@, etc.) from each other. Thus, @Uniques@ are the basic
8 comparison key in the compiler.
10 If there is any single operation that needs to be fast, it is @Unique@
11 comparison. Unsurprisingly, there is quite a bit of huff-and-puff
14 Some of the other hair in this code is to be able to use a
15 ``splittable @UniqueSupply@'' if requested/possible (not standard
20 -- The above warning supression flag is a temporary kludge.
21 -- While working on this module you are encouraged to remove it and fix
22 -- any warnings in the module. See
23 -- http://hackage.haskell.org/trac/ghc/wiki/CodingStyle#Warnings
27 Unique, Uniquable(..), hasKey,
31 mkUnique, -- Used in UniqSupply
32 mkUniqueGrimily, -- Used in UniqSupply only!
33 getKey, getKey#, -- Used in Var, UniqFM, Name only!
35 incrUnique, -- Used for renumbering
36 deriveUnique, -- Ditto
37 newTagUnique, -- Used in CgCase
42 -- now all the built-in Uniques (and functions to make them)
43 -- [the Oh-So-Wonderful Haskell module system wins again...]
46 mkTupleTyConUnique, mkTupleDataConUnique,
47 mkPreludeMiscIdUnique, mkPreludeDataConUnique,
48 mkPreludeTyConUnique, mkPreludeClassUnique,
58 #include "HsVersions.h"
66 import Data.Char ( chr, ord )
69 %************************************************************************
71 \subsection[Unique-type]{@Unique@ type and operations}
73 %************************************************************************
75 The @Chars@ are ``tag letters'' that identify the @UniqueSupply@.
76 Fast comparison is everything on @Uniques@:
79 data Unique = MkUnique Int#
82 Now come the functions which construct uniques from their pieces, and vice versa.
83 The stuff about unique *supplies* is handled further down this module.
86 mkUnique :: Char -> Int -> Unique -- Builds a unique from pieces
87 unpkUnique :: Unique -> (Char, Int) -- The reverse
89 mkUniqueGrimily :: Int -> Unique -- A trap-door for UniqSupply
90 getKey :: Unique -> Int -- for Var
91 getKey# :: Unique -> Int# -- for Var
93 incrUnique :: Unique -> Unique
94 deriveUnique :: Unique -> Int -> Unique
95 newTagUnique :: Unique -> Char -> Unique
97 isTupleKey :: Unique -> Bool
102 mkUniqueGrimily (I# x) = MkUnique x
104 {-# INLINE getKey #-}
105 getKey (MkUnique x) = I# x
106 {-# INLINE getKey# #-}
107 getKey# (MkUnique x) = x
109 incrUnique (MkUnique i) = MkUnique (i +# 1#)
111 -- deriveUnique uses an 'X' tag so that it won't clash with
112 -- any of the uniques produced any other way
113 deriveUnique (MkUnique i) delta = mkUnique 'X' (I# i + delta)
115 -- newTagUnique changes the "domain" of a unique to a different char
116 newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u
118 -- pop the Char in the top 8 bits of the Unique(Supply)
120 -- No 64-bit bugs here, as long as we have at least 32 bits. --JSM
126 mkUnique (C# c) (I# i)
127 = MkUnique (w2i (tag `or#` bits))
129 tag = i2w (ord# c) `uncheckedShiftL#` i2w_s 24#
130 bits = i2w i `and#` (i2w 16777215#){-``0x00ffffff''-}
132 unpkUnique (MkUnique u)
134 tag = C# (chr# (w2i ((i2w u) `uncheckedShiftRL#` (i2w_s 24#))))
135 i = I# (w2i ((i2w u) `and#` (i2w 16777215#){-``0x00ffffff''-}))
142 %************************************************************************
144 \subsection[Uniquable-class]{The @Uniquable@ class}
146 %************************************************************************
149 class Uniquable a where
150 getUnique :: a -> Unique
152 hasKey :: Uniquable a => a -> Unique -> Bool
153 x `hasKey` k = getUnique x == k
155 instance Uniquable FastString where
156 getUnique fs = mkUniqueGrimily (I# (uniqueOfFS fs))
158 instance Uniquable PackageId where
159 getUnique pid = getUnique (packageIdFS pid)
161 instance Uniquable Int where
162 getUnique i = mkUniqueGrimily i
166 %************************************************************************
168 \subsection[Unique-instances]{Instance declarations for @Unique@}
170 %************************************************************************
172 And the whole point (besides uniqueness) is fast equality. We don't
173 use `deriving' because we want {\em precise} control of ordering
174 (equality on @Uniques@ is v common).
177 eqUnique (MkUnique u1) (MkUnique u2) = u1 ==# u2
178 ltUnique (MkUnique u1) (MkUnique u2) = u1 <# u2
179 leUnique (MkUnique u1) (MkUnique u2) = u1 <=# u2
181 cmpUnique (MkUnique u1) (MkUnique u2)
182 = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
184 instance Eq Unique where
185 a == b = eqUnique a b
186 a /= b = not (eqUnique a b)
188 instance Ord Unique where
190 a <= b = leUnique a b
191 a > b = not (leUnique a b)
192 a >= b = not (ltUnique a b)
193 compare a b = cmpUnique a b
196 instance Uniquable Unique where
200 We do sometimes make strings with @Uniques@ in them:
202 pprUnique :: Unique -> SDoc
204 = case unpkUnique uniq of
205 (tag, u) -> finish_ppr tag u (text (iToBase62 u))
208 pprUnique10 :: Unique -> SDoc
209 pprUnique10 uniq -- in base-10, dudes
210 = case unpkUnique uniq of
211 (tag, u) -> finish_ppr tag u (int u)
214 finish_ppr 't' u pp_u | u < 26
215 = -- Special case to make v common tyvars, t1, t2, ...
216 -- come out as a, b, ... (shorter, easier to read)
217 char (chr (ord 'a' + u))
218 finish_ppr tag u pp_u = char tag <> pp_u
220 instance Outputable Unique where
223 instance Show Unique where
224 showsPrec p uniq = showsPrecSDoc p (pprUnique uniq)
227 %************************************************************************
229 \subsection[Utils-base62]{Base-62 numbers}
231 %************************************************************************
233 A character-stingy way to read/write numbers (notably Uniques).
234 The ``62-its'' are \tr{[0-9a-zA-Z]}. We don't handle negative Ints.
235 Code stolen from Lennart.
238 iToBase62 :: Int -> String
240 = ASSERT(n >= 0) go n# ""
243 = case (indexCharOffAddr# chars62# n#) of { c# -> C# c# : cs }
245 = case (quotRem (I# n#) 62) of { (I# q#, I# r#) ->
246 case (indexCharOffAddr# chars62# r#) of { c# ->
247 go q# (C# c# : cs) }}
249 chars62# = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
252 %************************************************************************
254 \subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
256 %************************************************************************
258 Allocation of unique supply characters:
259 v,t,u : for renumbering value-, type- and usage- vars.
261 C-E: pseudo uniques (used in native-code generator)
262 X: uniques derived by deriveUnique
263 _: unifiable tyvars (above)
264 0-9: prelude things below
266 other a-z: lower case chars for unique supplies. Used so far:
277 mkAlphaTyVarUnique i = mkUnique '1' i
279 mkPreludeClassUnique i = mkUnique '2' i
281 -- Prelude type constructors occupy *three* slots.
282 -- The first is for the tycon itself; the latter two
283 -- are for the generic to/from Ids. See TysWiredIn.mk_tc_gen_info.
285 mkPreludeTyConUnique i = mkUnique '3' (3*i)
286 mkTupleTyConUnique Boxed a = mkUnique '4' (3*a)
287 mkTupleTyConUnique Unboxed a = mkUnique '5' (3*a)
289 -- Data constructor keys occupy *two* slots. The first is used for the
290 -- data constructor itself and its wrapper function (the function that
291 -- evaluates arguments as necessary and calls the worker). The second is
292 -- used for the worker function (the function that builds the constructor
295 mkPreludeDataConUnique i = mkUnique '6' (2*i) -- Must be alphabetic
296 mkTupleDataConUnique Boxed a = mkUnique '7' (2*a) -- ditto (*may* be used in C labels)
297 mkTupleDataConUnique Unboxed a = mkUnique '8' (2*a)
299 -- This one is used for a tiresome reason
300 -- to improve a consistency-checking error check in the renamer
301 isTupleKey u = case unpkUnique u of
302 (tag,_) -> tag == '4' || tag == '5' || tag == '7' || tag == '8'
304 mkPrimOpIdUnique op = mkUnique '9' op
305 mkPreludeMiscIdUnique i = mkUnique '0' i
307 -- No numbers left anymore, so I pick something different for the character
309 mkPArrDataConUnique a = mkUnique ':' (2*a)
311 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
312 -- See pprUnique for details
314 initTyVarUnique :: Unique
315 initTyVarUnique = mkUnique 't' 0
317 mkPseudoUniqueC, mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
318 mkBuiltinUnique :: Int -> Unique
320 mkBuiltinUnique i = mkUnique 'B' i
321 mkPseudoUniqueC i = mkUnique 'C' i -- used for getUnique on Regs
322 mkPseudoUniqueD i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
323 mkPseudoUniqueE i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
324 mkPseudoUniqueH i = mkUnique 'H' i -- used in NCG spiller to create spill VirtualRegs