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
21 Unique, Uniquable(..),
23 -- ** Constructors, desctructors and operations on 'Unique's
28 mkUnique, -- Used in UniqSupply
29 mkUniqueGrimily, -- Used in UniqSupply only!
30 getKey, getKeyFastInt, -- Used in Var, UniqFM, Name only!
32 incrUnique, -- Used for renumbering
33 deriveUnique, -- Ditto
34 newTagUnique, -- Used in CgCase
39 -- ** Making built-in uniques
41 -- now all the built-in Uniques (and functions to make them)
42 -- [the Oh-So-Wonderful Haskell module system wins again...]
45 mkTupleTyConUnique, mkTupleDataConUnique,
46 mkPreludeMiscIdUnique, mkPreludeDataConUnique,
47 mkPreludeTyConUnique, mkPreludeClassUnique,
57 #include "HsVersions.h"
66 #if defined(__GLASGOW_HASKELL__)
67 --just for implementing a fast [0,61) -> Char function
68 import GHC.Exts (indexCharOffAddr#, Char(..))
72 import Data.Char ( chr, ord )
75 %************************************************************************
77 \subsection[Unique-type]{@Unique@ type and operations}
79 %************************************************************************
81 The @Chars@ are ``tag letters'' that identify the @UniqueSupply@.
82 Fast comparison is everything on @Uniques@:
85 --why not newtype Int?
87 -- | The type of unique identifiers that are used in many places in GHC
88 -- for fast ordering and equality tests. You should generate these with
89 -- the functions from the 'UniqSupply' module
90 data Unique = MkUnique FastInt
93 Now come the functions which construct uniques from their pieces, and vice versa.
94 The stuff about unique *supplies* is handled further down this module.
97 mkUnique :: Char -> Int -> Unique -- Builds a unique from pieces
98 unpkUnique :: Unique -> (Char, Int) -- The reverse
100 mkUniqueGrimily :: Int -> Unique -- A trap-door for UniqSupply
101 getKey :: Unique -> Int -- for Var
102 getKeyFastInt :: Unique -> FastInt -- for Var
104 incrUnique :: Unique -> Unique
105 deriveUnique :: Unique -> Int -> Unique
106 newTagUnique :: Unique -> Char -> Unique
108 isTupleKey :: Unique -> Bool
113 mkUniqueGrimily x = MkUnique (iUnbox x)
115 {-# INLINE getKey #-}
116 getKey (MkUnique x) = iBox x
117 {-# INLINE getKeyFastInt #-}
118 getKeyFastInt (MkUnique x) = x
120 incrUnique (MkUnique i) = MkUnique (i +# _ILIT(1))
122 -- deriveUnique uses an 'X' tag so that it won't clash with
123 -- any of the uniques produced any other way
124 deriveUnique (MkUnique i) delta = mkUnique 'X' (iBox i + delta)
126 -- newTagUnique changes the "domain" of a unique to a different char
127 newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u
129 -- pop the Char in the top 8 bits of the Unique(Supply)
131 -- No 64-bit bugs here, as long as we have at least 32 bits. --JSM
133 -- and as long as the Char fits in 8 bits, which we assume anyway!
136 = MkUnique (tag `bitOrFastInt` bits)
138 tag = fastOrd (cUnbox c) `shiftLFastInt` _ILIT(24)
139 bits = iUnbox i `bitAndFastInt` _ILIT(16777215){-``0x00ffffff''-}
141 unpkUnique (MkUnique u)
143 -- as long as the Char may have its eighth bit set, we
144 -- really do need the logical right-shift here!
145 tag = cBox (fastChr (u `shiftRLFastInt` _ILIT(24)))
146 i = iBox (u `bitAndFastInt` _ILIT(16777215){-``0x00ffffff''-})
153 %************************************************************************
155 \subsection[Uniquable-class]{The @Uniquable@ class}
157 %************************************************************************
160 -- | Class of things that we can obtain a 'Unique' from
161 class Uniquable a where
162 getUnique :: a -> Unique
164 hasKey :: Uniquable a => a -> Unique -> Bool
165 x `hasKey` k = getUnique x == k
167 instance Uniquable FastString where
168 getUnique fs = mkUniqueGrimily (iBox (uniqueOfFS fs))
170 instance Uniquable Int where
171 getUnique i = mkUniqueGrimily i
175 %************************************************************************
177 \subsection[Unique-instances]{Instance declarations for @Unique@}
179 %************************************************************************
181 And the whole point (besides uniqueness) is fast equality. We don't
182 use `deriving' because we want {\em precise} control of ordering
183 (equality on @Uniques@ is v common).
186 eqUnique, ltUnique, leUnique :: Unique -> Unique -> Bool
187 eqUnique (MkUnique u1) (MkUnique u2) = u1 ==# u2
188 ltUnique (MkUnique u1) (MkUnique u2) = u1 <# u2
189 leUnique (MkUnique u1) (MkUnique u2) = u1 <=# u2
191 cmpUnique :: Unique -> Unique -> Ordering
192 cmpUnique (MkUnique u1) (MkUnique u2)
193 = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
195 instance Eq Unique where
196 a == b = eqUnique a b
197 a /= b = not (eqUnique a b)
199 instance Ord Unique where
201 a <= b = leUnique a b
202 a > b = not (leUnique a b)
203 a >= b = not (ltUnique a b)
204 compare a b = cmpUnique a b
207 instance Uniquable Unique where
211 We do sometimes make strings with @Uniques@ in them:
213 pprUnique :: Unique -> SDoc
215 | debugIsOn && opt_SuppressUniques
216 = empty -- Used exclusively to suppress uniques so you
217 | otherwise -- can compare output easily
218 = case unpkUnique uniq of
219 (tag, u) -> finish_ppr tag u (text (iToBase62 u))
222 pprUnique10 :: Unique -> SDoc
223 pprUnique10 uniq -- in base-10, dudes
224 = case unpkUnique uniq of
225 (tag, u) -> finish_ppr tag u (int u)
228 finish_ppr :: Char -> Int -> SDoc -> SDoc
229 finish_ppr 't' u _pp_u | u < 26
230 = -- Special case to make v common tyvars, t1, t2, ...
231 -- come out as a, b, ... (shorter, easier to read)
232 char (chr (ord 'a' + u))
233 finish_ppr tag _ pp_u = char tag <> pp_u
235 instance Outputable Unique where
238 instance Show Unique where
239 showsPrec p uniq = showsPrecSDoc p (pprUnique uniq)
242 %************************************************************************
244 \subsection[Utils-base62]{Base-62 numbers}
246 %************************************************************************
248 A character-stingy way to read/write numbers (notably Uniques).
249 The ``62-its'' are \tr{[0-9a-zA-Z]}. We don't handle negative Ints.
250 Code stolen from Lennart.
253 iToBase62 :: Int -> String
255 = ASSERT(n_ >= 0) go (iUnbox n_) ""
257 go n cs | n <# _ILIT(62)
258 = case chooseChar62 n of { c -> c `seq` (c : cs) }
260 = case (quotRem (iBox n) 62) of { (q_, r_) ->
261 case iUnbox q_ of { q -> case iUnbox r_ of { r ->
262 case (chooseChar62 r) of { c -> c `seq`
265 chooseChar62 :: FastInt -> Char
266 {-# INLINE chooseChar62 #-}
267 #if defined(__GLASGOW_HASKELL__)
268 --then FastInt == Int#
269 chooseChar62 n = C# (indexCharOffAddr# chars62 n)
270 chars62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
272 --Haskell98 arrays are portable
273 chooseChar62 n = (!) chars62 n
274 chars62 = listArray (0,61) "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
278 %************************************************************************
280 \subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
282 %************************************************************************
284 Allocation of unique supply characters:
285 v,t,u : for renumbering value-, type- and usage- vars.
287 C-E: pseudo uniques (used in native-code generator)
288 X: uniques derived by deriveUnique
289 _: unifiable tyvars (above)
290 0-9: prelude things below
291 (no numbers left any more..)
292 :: (prelude) parallel array data constructors
294 other a-z: lower case chars for unique supplies. Used so far:
304 mkAlphaTyVarUnique :: Int -> Unique
305 mkPreludeClassUnique :: Int -> Unique
306 mkPreludeTyConUnique :: Int -> Unique
307 mkTupleTyConUnique :: Boxity -> Int -> Unique
308 mkPreludeDataConUnique :: Int -> Unique
309 mkTupleDataConUnique :: Boxity -> Int -> Unique
310 mkPrimOpIdUnique :: Int -> Unique
311 mkPreludeMiscIdUnique :: Int -> Unique
312 mkPArrDataConUnique :: Int -> Unique
314 mkAlphaTyVarUnique i = mkUnique '1' i
316 mkPreludeClassUnique i = mkUnique '2' i
318 -- Prelude type constructors occupy *three* slots.
319 -- The first is for the tycon itself; the latter two
320 -- are for the generic to/from Ids. See TysWiredIn.mk_tc_gen_info.
322 mkPreludeTyConUnique i = mkUnique '3' (3*i)
323 mkTupleTyConUnique Boxed a = mkUnique '4' (3*a)
324 mkTupleTyConUnique Unboxed a = mkUnique '5' (3*a)
326 -- Data constructor keys occupy *two* slots. The first is used for the
327 -- data constructor itself and its wrapper function (the function that
328 -- evaluates arguments as necessary and calls the worker). The second is
329 -- used for the worker function (the function that builds the constructor
332 mkPreludeDataConUnique i = mkUnique '6' (2*i) -- Must be alphabetic
333 mkTupleDataConUnique Boxed a = mkUnique '7' (2*a) -- ditto (*may* be used in C labels)
334 mkTupleDataConUnique Unboxed a = mkUnique '8' (2*a)
336 -- This one is used for a tiresome reason
337 -- to improve a consistency-checking error check in the renamer
338 isTupleKey u = case unpkUnique u of
339 (tag,_) -> tag == '4' || tag == '5' || tag == '7' || tag == '8'
341 mkPrimOpIdUnique op = mkUnique '9' op
342 mkPreludeMiscIdUnique i = mkUnique '0' i
344 -- No numbers left anymore, so I pick something different for the character
346 mkPArrDataConUnique a = mkUnique ':' (2*a)
348 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
349 -- See pprUnique for details
351 initTyVarUnique :: Unique
352 initTyVarUnique = mkUnique 't' 0
354 mkPseudoUniqueC, mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
355 mkBuiltinUnique :: Int -> Unique
357 mkBuiltinUnique i = mkUnique 'B' i
358 mkPseudoUniqueC i = mkUnique 'C' i -- used for getUnique on Regs
359 mkPseudoUniqueD i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
360 mkPseudoUniqueE i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
361 mkPseudoUniqueH i = mkUnique 'H' i -- used in NCG spiller to create spill VirtualRegs