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 Unique, Uniquable(..), hasKey,
24 mkUnique, -- Used in UniqSupply
25 mkUniqueGrimily, -- Used in UniqSupply only!
26 getKey, getKey#, -- Used in Var, UniqFM, Name only!
28 incrUnique, -- Used for renumbering
29 deriveUnique, -- Ditto
30 newTagUnique, -- Used in CgCase
35 -- now all the built-in Uniques (and functions to make them)
36 -- [the Oh-So-Wonderful Haskell module system wins again...]
39 mkTupleTyConUnique, mkTupleDataConUnique,
40 mkPreludeMiscIdUnique, mkPreludeDataConUnique,
41 mkPreludeTyConUnique, mkPreludeClassUnique,
51 #include "HsVersions.h"
60 import Data.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 tag = i2w (ord# c) `uncheckedShiftL#` i2w_s 24#
124 bits = i2w i `and#` (i2w 16777215#){-``0x00ffffff''-}
126 unpkUnique (MkUnique u)
128 tag = C# (chr# (w2i ((i2w u) `uncheckedShiftRL#` (i2w_s 24#))))
129 i = I# (w2i ((i2w u) `and#` (i2w 16777215#){-``0x00ffffff''-}))
136 %************************************************************************
138 \subsection[Uniquable-class]{The @Uniquable@ class}
140 %************************************************************************
143 class Uniquable a where
144 getUnique :: a -> Unique
146 hasKey :: Uniquable a => a -> Unique -> Bool
147 x `hasKey` k = getUnique x == k
149 instance Uniquable FastString where
150 getUnique fs = mkUniqueGrimily (I# (uniqueOfFS fs))
152 instance Uniquable PackageId where
153 getUnique pid = getUnique (packageIdFS pid)
155 instance Uniquable Int where
156 getUnique i = mkUniqueGrimily i
160 %************************************************************************
162 \subsection[Unique-instances]{Instance declarations for @Unique@}
164 %************************************************************************
166 And the whole point (besides uniqueness) is fast equality. We don't
167 use `deriving' because we want {\em precise} control of ordering
168 (equality on @Uniques@ is v common).
171 eqUnique (MkUnique u1) (MkUnique u2) = u1 ==# u2
172 ltUnique (MkUnique u1) (MkUnique u2) = u1 <# u2
173 leUnique (MkUnique u1) (MkUnique u2) = u1 <=# u2
175 cmpUnique (MkUnique u1) (MkUnique u2)
176 = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
178 instance Eq Unique where
179 a == b = eqUnique a b
180 a /= b = not (eqUnique a b)
182 instance Ord Unique where
184 a <= b = leUnique a b
185 a > b = not (leUnique a b)
186 a >= b = not (ltUnique a b)
187 compare a b = cmpUnique a b
190 instance Uniquable Unique where
194 We do sometimes make strings with @Uniques@ in them:
196 pprUnique :: Unique -> SDoc
198 = case unpkUnique uniq of
199 (tag, u) -> finish_ppr tag u (text (iToBase62 u))
202 pprUnique10 :: Unique -> SDoc
203 pprUnique10 uniq -- in base-10, dudes
204 = case unpkUnique uniq of
205 (tag, u) -> finish_ppr tag u (int u)
208 finish_ppr 't' u pp_u | u < 26
209 = -- Special case to make v common tyvars, t1, t2, ...
210 -- come out as a, b, ... (shorter, easier to read)
211 char (chr (ord 'a' + u))
212 finish_ppr tag u pp_u = char tag <> pp_u
214 instance Outputable Unique where
217 instance Show Unique where
218 showsPrec p uniq = showsPrecSDoc p (pprUnique uniq)
221 %************************************************************************
223 \subsection[Utils-base62]{Base-62 numbers}
225 %************************************************************************
227 A character-stingy way to read/write numbers (notably Uniques).
228 The ``62-its'' are \tr{[0-9a-zA-Z]}. We don't handle negative Ints.
229 Code stolen from Lennart.
232 iToBase62 :: Int -> String
234 = ASSERT(n >= 0) go n# ""
237 = case (indexCharOffAddr# chars62# n#) of { c# -> C# c# : cs }
239 = case (quotRem (I# n#) 62) of { (I# q#, I# r#) ->
240 case (indexCharOffAddr# chars62# r#) of { c# ->
241 go q# (C# c# : cs) }}
243 chars62# = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
246 %************************************************************************
248 \subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
250 %************************************************************************
252 Allocation of unique supply characters:
253 v,t,u : for renumbering value-, type- and usage- vars.
255 C-E: pseudo uniques (used in native-code generator)
256 X: uniques derived by deriveUnique
257 _: unifiable tyvars (above)
258 0-9: prelude things below
260 other a-z: lower case chars for unique supplies. Used so far:
271 mkAlphaTyVarUnique i = mkUnique '1' i
273 mkPreludeClassUnique i = mkUnique '2' i
275 -- Prelude type constructors occupy *three* slots.
276 -- The first is for the tycon itself; the latter two
277 -- are for the generic to/from Ids. See TysWiredIn.mk_tc_gen_info.
279 mkPreludeTyConUnique i = mkUnique '3' (3*i)
280 mkTupleTyConUnique Boxed a = mkUnique '4' (3*a)
281 mkTupleTyConUnique Unboxed a = mkUnique '5' (3*a)
283 -- Data constructor keys occupy *two* slots. The first is used for the
284 -- data constructor itself and its wrapper function (the function that
285 -- evaluates arguments as necessary and calls the worker). The second is
286 -- used for the worker function (the function that builds the constructor
289 mkPreludeDataConUnique i = mkUnique '6' (2*i) -- Must be alphabetic
290 mkTupleDataConUnique Boxed a = mkUnique '7' (2*a) -- ditto (*may* be used in C labels)
291 mkTupleDataConUnique Unboxed a = mkUnique '8' (2*a)
293 -- This one is used for a tiresome reason
294 -- to improve a consistency-checking error check in the renamer
295 isTupleKey u = case unpkUnique u of
296 (tag,_) -> tag == '4' || tag == '5' || tag == '7' || tag == '8'
298 mkPrimOpIdUnique op = mkUnique '9' op
299 mkPreludeMiscIdUnique i = mkUnique '0' i
301 -- No numbers left anymore, so I pick something different for the character
303 mkPArrDataConUnique a = mkUnique ':' (2*a)
305 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
306 -- See pprUnique for details
308 initTyVarUnique :: Unique
309 initTyVarUnique = mkUnique 't' 0
311 mkPseudoUniqueC, mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
312 mkBuiltinUnique :: Int -> Unique
314 mkBuiltinUnique i = mkUnique 'B' i
315 mkPseudoUniqueC i = mkUnique 'C' i -- used for getUnique on Regs
316 mkPseudoUniqueD i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
317 mkPseudoUniqueE i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
318 mkPseudoUniqueH i = mkUnique 'H' i -- used in NCG spiller to create spill VirtualRegs