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"
59 import Data.Char ( chr, ord )
62 %************************************************************************
64 \subsection[Unique-type]{@Unique@ type and operations}
66 %************************************************************************
68 The @Chars@ are ``tag letters'' that identify the @UniqueSupply@.
69 Fast comparison is everything on @Uniques@:
72 data Unique = MkUnique Int#
75 Now come the functions which construct uniques from their pieces, and vice versa.
76 The stuff about unique *supplies* is handled further down this module.
79 mkUnique :: Char -> Int -> Unique -- Builds a unique from pieces
80 unpkUnique :: Unique -> (Char, Int) -- The reverse
82 mkUniqueGrimily :: Int -> Unique -- A trap-door for UniqSupply
83 getKey :: Unique -> Int -- for Var
84 getKey# :: Unique -> Int# -- for Var
86 incrUnique :: Unique -> Unique
87 deriveUnique :: Unique -> Int -> Unique
88 newTagUnique :: Unique -> Char -> Unique
90 isTupleKey :: Unique -> Bool
95 mkUniqueGrimily (I# x) = MkUnique x
98 getKey (MkUnique x) = I# x
99 {-# INLINE getKey# #-}
100 getKey# (MkUnique x) = x
102 incrUnique (MkUnique i) = MkUnique (i +# 1#)
104 -- deriveUnique uses an 'X' tag so that it won't clash with
105 -- any of the uniques produced any other way
106 deriveUnique (MkUnique i) delta = mkUnique 'X' (I# i + delta)
108 -- newTagUnique changes the "domain" of a unique to a different char
109 newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u
111 -- pop the Char in the top 8 bits of the Unique(Supply)
113 -- No 64-bit bugs here, as long as we have at least 32 bits. --JSM
119 mkUnique (C# c) (I# i)
120 = MkUnique (w2i (tag `or#` bits))
122 tag = i2w (ord# c) `uncheckedShiftL#` i2w_s 24#
123 bits = i2w i `and#` (i2w 16777215#){-``0x00ffffff''-}
125 unpkUnique (MkUnique u)
127 tag = C# (chr# (w2i ((i2w u) `uncheckedShiftRL#` (i2w_s 24#))))
128 i = I# (w2i ((i2w u) `and#` (i2w 16777215#){-``0x00ffffff''-}))
135 %************************************************************************
137 \subsection[Uniquable-class]{The @Uniquable@ class}
139 %************************************************************************
142 class Uniquable a where
143 getUnique :: a -> Unique
145 hasKey :: Uniquable a => a -> Unique -> Bool
146 x `hasKey` k = getUnique x == k
148 instance Uniquable FastString where
149 getUnique fs = mkUniqueGrimily (I# (uniqueOfFS fs))
151 instance Uniquable PackageId where
152 getUnique pid = getUnique (packageIdFS pid)
154 instance Uniquable Int where
155 getUnique i = mkUniqueGrimily i
159 %************************************************************************
161 \subsection[Unique-instances]{Instance declarations for @Unique@}
163 %************************************************************************
165 And the whole point (besides uniqueness) is fast equality. We don't
166 use `deriving' because we want {\em precise} control of ordering
167 (equality on @Uniques@ is v common).
170 eqUnique (MkUnique u1) (MkUnique u2) = u1 ==# u2
171 ltUnique (MkUnique u1) (MkUnique u2) = u1 <# u2
172 leUnique (MkUnique u1) (MkUnique u2) = u1 <=# u2
174 cmpUnique (MkUnique u1) (MkUnique u2)
175 = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
177 instance Eq Unique where
178 a == b = eqUnique a b
179 a /= b = not (eqUnique a b)
181 instance Ord Unique where
183 a <= b = leUnique a b
184 a > b = not (leUnique a b)
185 a >= b = not (ltUnique a b)
186 compare a b = cmpUnique a b
189 instance Uniquable Unique where
193 We do sometimes make strings with @Uniques@ in them:
195 pprUnique :: Unique -> SDoc
197 = case unpkUnique uniq of
198 (tag, u) -> finish_ppr tag u (text (iToBase62 u))
201 pprUnique10 :: Unique -> SDoc
202 pprUnique10 uniq -- in base-10, dudes
203 = case unpkUnique uniq of
204 (tag, u) -> finish_ppr tag u (int u)
207 finish_ppr 't' u pp_u | u < 26
208 = -- Special case to make v common tyvars, t1, t2, ...
209 -- come out as a, b, ... (shorter, easier to read)
210 char (chr (ord 'a' + u))
211 finish_ppr tag u pp_u = char tag <> pp_u
213 instance Outputable Unique where
216 instance Show Unique where
217 showsPrec p uniq = showsPrecSDoc p (pprUnique uniq)
220 %************************************************************************
222 \subsection[Utils-base62]{Base-62 numbers}
224 %************************************************************************
226 A character-stingy way to read/write numbers (notably Uniques).
227 The ``62-its'' are \tr{[0-9a-zA-Z]}. We don't handle negative Ints.
228 Code stolen from Lennart.
231 iToBase62 :: Int -> String
233 = ASSERT(n >= 0) go n# ""
236 = case (indexCharOffAddr# chars62# n#) of { c# -> C# c# : cs }
238 = case (quotRem (I# n#) 62) of { (I# q#, I# r#) ->
239 case (indexCharOffAddr# chars62# r#) of { c# ->
240 go q# (C# c# : cs) }}
242 chars62# = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
245 %************************************************************************
247 \subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
249 %************************************************************************
251 Allocation of unique supply characters:
252 v,t,u : for renumbering value-, type- and usage- vars.
254 C-E: pseudo uniques (used in native-code generator)
255 X: uniques derived by deriveUnique
256 _: unifiable tyvars (above)
257 0-9: prelude things below
259 other a-z: lower case chars for unique supplies. Used so far:
270 mkAlphaTyVarUnique i = mkUnique '1' i
272 mkPreludeClassUnique i = mkUnique '2' i
274 -- Prelude type constructors occupy *three* slots.
275 -- The first is for the tycon itself; the latter two
276 -- are for the generic to/from Ids. See TysWiredIn.mk_tc_gen_info.
278 mkPreludeTyConUnique i = mkUnique '3' (3*i)
279 mkTupleTyConUnique Boxed a = mkUnique '4' (3*a)
280 mkTupleTyConUnique Unboxed a = mkUnique '5' (3*a)
282 -- Data constructor keys occupy *two* slots. The first is used for the
283 -- data constructor itself and its wrapper function (the function that
284 -- evaluates arguments as necessary and calls the worker). The second is
285 -- used for the worker function (the function that builds the constructor
288 mkPreludeDataConUnique i = mkUnique '6' (2*i) -- Must be alphabetic
289 mkTupleDataConUnique Boxed a = mkUnique '7' (2*a) -- ditto (*may* be used in C labels)
290 mkTupleDataConUnique Unboxed a = mkUnique '8' (2*a)
292 -- This one is used for a tiresome reason
293 -- to improve a consistency-checking error check in the renamer
294 isTupleKey u = case unpkUnique u of
295 (tag,_) -> tag == '4' || tag == '5' || tag == '7' || tag == '8'
297 mkPrimOpIdUnique op = mkUnique '9' op
298 mkPreludeMiscIdUnique i = mkUnique '0' i
300 -- No numbers left anymore, so I pick something different for the character
302 mkPArrDataConUnique a = mkUnique ':' (2*a)
304 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
305 -- See pprUnique for details
307 initTyVarUnique :: Unique
308 initTyVarUnique = mkUnique 't' 0
310 mkPseudoUniqueC, mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
311 mkBuiltinUnique :: Int -> Unique
313 mkBuiltinUnique i = mkUnique 'B' i
314 mkPseudoUniqueC i = mkUnique 'C' i -- used for getUnique on Regs
315 mkPseudoUniqueD i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
316 mkPseudoUniqueE i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
317 mkPseudoUniqueH i = mkUnique 'H' i -- used in NCG spiller to create spill VirtualRegs