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/Commentary/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"
65 import Data.Char ( chr, ord )
68 %************************************************************************
70 \subsection[Unique-type]{@Unique@ type and operations}
72 %************************************************************************
74 The @Chars@ are ``tag letters'' that identify the @UniqueSupply@.
75 Fast comparison is everything on @Uniques@:
78 data Unique = MkUnique Int#
81 Now come the functions which construct uniques from their pieces, and vice versa.
82 The stuff about unique *supplies* is handled further down this module.
85 mkUnique :: Char -> Int -> Unique -- Builds a unique from pieces
86 unpkUnique :: Unique -> (Char, Int) -- The reverse
88 mkUniqueGrimily :: Int -> Unique -- A trap-door for UniqSupply
89 getKey :: Unique -> Int -- for Var
90 getKey# :: Unique -> Int# -- for Var
92 incrUnique :: Unique -> Unique
93 deriveUnique :: Unique -> Int -> Unique
94 newTagUnique :: Unique -> Char -> Unique
96 isTupleKey :: Unique -> Bool
101 mkUniqueGrimily (I# x) = MkUnique x
103 {-# INLINE getKey #-}
104 getKey (MkUnique x) = I# x
105 {-# INLINE getKey# #-}
106 getKey# (MkUnique x) = x
108 incrUnique (MkUnique i) = MkUnique (i +# 1#)
110 -- deriveUnique uses an 'X' tag so that it won't clash with
111 -- any of the uniques produced any other way
112 deriveUnique (MkUnique i) delta = mkUnique 'X' (I# i + delta)
114 -- newTagUnique changes the "domain" of a unique to a different char
115 newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u
117 -- pop the Char in the top 8 bits of the Unique(Supply)
119 -- No 64-bit bugs here, as long as we have at least 32 bits. --JSM
125 mkUnique (C# c) (I# i)
126 = MkUnique (w2i (tag `or#` bits))
128 tag = i2w (ord# c) `uncheckedShiftL#` i2w_s 24#
129 bits = i2w i `and#` (i2w 16777215#){-``0x00ffffff''-}
131 unpkUnique (MkUnique u)
133 tag = C# (chr# (w2i ((i2w u) `uncheckedShiftRL#` (i2w_s 24#))))
134 i = I# (w2i ((i2w u) `and#` (i2w 16777215#){-``0x00ffffff''-}))
141 %************************************************************************
143 \subsection[Uniquable-class]{The @Uniquable@ class}
145 %************************************************************************
148 class Uniquable a where
149 getUnique :: a -> Unique
151 hasKey :: Uniquable a => a -> Unique -> Bool
152 x `hasKey` k = getUnique x == k
154 instance Uniquable FastString where
155 getUnique fs = mkUniqueGrimily (I# (uniqueOfFS fs))
157 instance Uniquable Int where
158 getUnique i = mkUniqueGrimily i
162 %************************************************************************
164 \subsection[Unique-instances]{Instance declarations for @Unique@}
166 %************************************************************************
168 And the whole point (besides uniqueness) is fast equality. We don't
169 use `deriving' because we want {\em precise} control of ordering
170 (equality on @Uniques@ is v common).
173 eqUnique (MkUnique u1) (MkUnique u2) = u1 ==# u2
174 ltUnique (MkUnique u1) (MkUnique u2) = u1 <# u2
175 leUnique (MkUnique u1) (MkUnique u2) = u1 <=# u2
177 cmpUnique (MkUnique u1) (MkUnique u2)
178 = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
180 instance Eq Unique where
181 a == b = eqUnique a b
182 a /= b = not (eqUnique a b)
184 instance Ord Unique where
186 a <= b = leUnique a b
187 a > b = not (leUnique a b)
188 a >= b = not (ltUnique a b)
189 compare a b = cmpUnique a b
192 instance Uniquable Unique where
196 We do sometimes make strings with @Uniques@ in them:
198 pprUnique :: Unique -> SDoc
200 = case unpkUnique uniq of
201 (tag, u) -> finish_ppr tag u (text (iToBase62 u))
204 pprUnique10 :: Unique -> SDoc
205 pprUnique10 uniq -- in base-10, dudes
206 = case unpkUnique uniq of
207 (tag, u) -> finish_ppr tag u (int u)
210 finish_ppr 't' u pp_u | u < 26
211 = -- Special case to make v common tyvars, t1, t2, ...
212 -- come out as a, b, ... (shorter, easier to read)
213 char (chr (ord 'a' + u))
214 finish_ppr tag u pp_u = char tag <> pp_u
216 instance Outputable Unique where
219 instance Show Unique where
220 showsPrec p uniq = showsPrecSDoc p (pprUnique uniq)
223 %************************************************************************
225 \subsection[Utils-base62]{Base-62 numbers}
227 %************************************************************************
229 A character-stingy way to read/write numbers (notably Uniques).
230 The ``62-its'' are \tr{[0-9a-zA-Z]}. We don't handle negative Ints.
231 Code stolen from Lennart.
234 iToBase62 :: Int -> String
236 = ASSERT(n >= 0) go n# ""
239 = case (indexCharOffAddr# chars62# n#) of { c# -> C# c# : cs }
241 = case (quotRem (I# n#) 62) of { (I# q#, I# r#) ->
242 case (indexCharOffAddr# chars62# r#) of { c# ->
243 go q# (C# c# : cs) }}
245 chars62# = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
248 %************************************************************************
250 \subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
252 %************************************************************************
254 Allocation of unique supply characters:
255 v,t,u : for renumbering value-, type- and usage- vars.
257 C-E: pseudo uniques (used in native-code generator)
258 X: uniques derived by deriveUnique
259 _: unifiable tyvars (above)
260 0-9: prelude things below
262 other a-z: lower case chars for unique supplies. Used so far:
273 mkAlphaTyVarUnique i = mkUnique '1' i
275 mkPreludeClassUnique i = mkUnique '2' i
277 -- Prelude type constructors occupy *three* slots.
278 -- The first is for the tycon itself; the latter two
279 -- are for the generic to/from Ids. See TysWiredIn.mk_tc_gen_info.
281 mkPreludeTyConUnique i = mkUnique '3' (3*i)
282 mkTupleTyConUnique Boxed a = mkUnique '4' (3*a)
283 mkTupleTyConUnique Unboxed a = mkUnique '5' (3*a)
285 -- Data constructor keys occupy *two* slots. The first is used for the
286 -- data constructor itself and its wrapper function (the function that
287 -- evaluates arguments as necessary and calls the worker). The second is
288 -- used for the worker function (the function that builds the constructor
291 mkPreludeDataConUnique i = mkUnique '6' (2*i) -- Must be alphabetic
292 mkTupleDataConUnique Boxed a = mkUnique '7' (2*a) -- ditto (*may* be used in C labels)
293 mkTupleDataConUnique Unboxed a = mkUnique '8' (2*a)
295 -- This one is used for a tiresome reason
296 -- to improve a consistency-checking error check in the renamer
297 isTupleKey u = case unpkUnique u of
298 (tag,_) -> tag == '4' || tag == '5' || tag == '7' || tag == '8'
300 mkPrimOpIdUnique op = mkUnique '9' op
301 mkPreludeMiscIdUnique i = mkUnique '0' i
303 -- No numbers left anymore, so I pick something different for the character
305 mkPArrDataConUnique a = mkUnique ':' (2*a)
307 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
308 -- See pprUnique for details
310 initTyVarUnique :: Unique
311 initTyVarUnique = mkUnique 't' 0
313 mkPseudoUniqueC, mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
314 mkBuiltinUnique :: Int -> Unique
316 mkBuiltinUnique i = mkUnique 'B' i
317 mkPseudoUniqueC i = mkUnique 'C' i -- used for getUnique on Regs
318 mkPseudoUniqueD i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
319 mkPseudoUniqueE i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
320 mkPseudoUniqueH i = mkUnique 'H' i -- used in NCG spiller to create spill VirtualRegs