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
19 {-# LANGUAGE BangPatterns #-}
22 Unique, Uniquable(..),
24 -- ** Constructors, desctructors and operations on 'Unique's
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,
50 mkVarOccUnique, mkDataOccUnique, mkTvOccUnique, mkTcOccUnique,
51 mkRegSingleUnique, mkRegPairUnique, mkRegClassUnique, mkRegSubUnique,
60 #include "HsVersions.h"
68 #if defined(__GLASGOW_HASKELL__)
69 --just for implementing a fast [0,61) -> Char function
70 import GHC.Exts (indexCharOffAddr#, Char(..))
74 import Data.Char ( chr, ord )
77 %************************************************************************
79 \subsection[Unique-type]{@Unique@ type and operations}
81 %************************************************************************
83 The @Chars@ are ``tag letters'' that identify the @UniqueSupply@.
84 Fast comparison is everything on @Uniques@:
87 --why not newtype Int?
89 -- | The type of unique identifiers that are used in many places in GHC
90 -- for fast ordering and equality tests. You should generate these with
91 -- the functions from the 'UniqSupply' module
92 data Unique = MkUnique FastInt
95 Now come the functions which construct uniques from their pieces, and vice versa.
96 The stuff about unique *supplies* is handled further down this module.
99 unpkUnique :: Unique -> (Char, Int) -- The reverse
101 mkUniqueGrimily :: Int -> Unique -- A trap-door for UniqSupply
102 getKey :: Unique -> Int -- for Var
103 getKeyFastInt :: Unique -> FastInt -- for Var
105 incrUnique :: Unique -> Unique
106 deriveUnique :: Unique -> Int -> Unique
107 newTagUnique :: Unique -> Char -> Unique
109 isTupleKey :: Unique -> Bool
114 mkUniqueGrimily x = MkUnique (iUnbox x)
116 {-# INLINE getKey #-}
117 getKey (MkUnique x) = iBox x
118 {-# INLINE getKeyFastInt #-}
119 getKeyFastInt (MkUnique x) = x
121 incrUnique (MkUnique i) = MkUnique (i +# _ILIT(1))
123 -- deriveUnique uses an 'X' tag so that it won't clash with
124 -- any of the uniques produced any other way
125 deriveUnique (MkUnique i) delta = mkUnique 'X' (iBox i + delta)
127 -- newTagUnique changes the "domain" of a unique to a different char
128 newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u
130 -- pop the Char in the top 8 bits of the Unique(Supply)
132 -- No 64-bit bugs here, as long as we have at least 32 bits. --JSM
134 -- and as long as the Char fits in 8 bits, which we assume anyway!
136 mkUnique :: Char -> Int -> Unique -- Builds a unique from pieces
137 -- NOT EXPORTED, so that we can see all the Chars that
138 -- are used in this one module
140 = MkUnique (tag `bitOrFastInt` bits)
142 !tag = fastOrd (cUnbox c) `shiftLFastInt` _ILIT(24)
143 !bits = iUnbox i `bitAndFastInt` _ILIT(16777215){-``0x00ffffff''-}
145 unpkUnique (MkUnique u)
147 -- as long as the Char may have its eighth bit set, we
148 -- really do need the logical right-shift here!
149 tag = cBox (fastChr (u `shiftRLFastInt` _ILIT(24)))
150 i = iBox (u `bitAndFastInt` _ILIT(16777215){-``0x00ffffff''-})
157 %************************************************************************
159 \subsection[Uniquable-class]{The @Uniquable@ class}
161 %************************************************************************
164 -- | Class of things that we can obtain a 'Unique' from
165 class Uniquable a where
166 getUnique :: a -> Unique
168 hasKey :: Uniquable a => a -> Unique -> Bool
169 x `hasKey` k = getUnique x == k
171 instance Uniquable FastString where
172 getUnique fs = mkUniqueGrimily (iBox (uniqueOfFS fs))
174 instance Uniquable Int where
175 getUnique i = mkUniqueGrimily i
179 %************************************************************************
181 \subsection[Unique-instances]{Instance declarations for @Unique@}
183 %************************************************************************
185 And the whole point (besides uniqueness) is fast equality. We don't
186 use `deriving' because we want {\em precise} control of ordering
187 (equality on @Uniques@ is v common).
190 eqUnique, ltUnique, leUnique :: Unique -> Unique -> Bool
191 eqUnique (MkUnique u1) (MkUnique u2) = u1 ==# u2
192 ltUnique (MkUnique u1) (MkUnique u2) = u1 <# u2
193 leUnique (MkUnique u1) (MkUnique u2) = u1 <=# u2
195 cmpUnique :: Unique -> Unique -> Ordering
196 cmpUnique (MkUnique u1) (MkUnique u2)
197 = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
199 instance Eq Unique where
200 a == b = eqUnique a b
201 a /= b = not (eqUnique a b)
203 instance Ord Unique where
205 a <= b = leUnique a b
206 a > b = not (leUnique a b)
207 a >= b = not (ltUnique a b)
208 compare a b = cmpUnique a b
211 instance Uniquable Unique where
215 We do sometimes make strings with @Uniques@ in them:
217 pprUnique :: Unique -> SDoc
219 -- | opt_SuppressUniques
220 -- = empty -- Used exclusively to suppress uniques so you
221 -- | otherwise -- can compare output easily
222 = case unpkUnique uniq of
223 (tag, u) -> finish_ppr tag u (text (iToBase62 u))
226 pprUnique10 :: Unique -> SDoc
227 pprUnique10 uniq -- in base-10, dudes
228 = case unpkUnique uniq of
229 (tag, u) -> finish_ppr tag u (int u)
232 finish_ppr :: Char -> Int -> SDoc -> SDoc
233 finish_ppr 't' u _pp_u | u < 26
234 = -- Special case to make v common tyvars, t1, t2, ...
235 -- come out as a, b, ... (shorter, easier to read)
236 char (chr (ord 'a' + u))
237 finish_ppr tag _ pp_u = char tag <> pp_u
239 instance Outputable Unique where
242 instance Show Unique where
243 showsPrec p uniq = showsPrecSDoc p (pprUnique uniq)
246 %************************************************************************
248 \subsection[Utils-base62]{Base-62 numbers}
250 %************************************************************************
252 A character-stingy way to read/write numbers (notably Uniques).
253 The ``62-its'' are \tr{[0-9a-zA-Z]}. We don't handle negative Ints.
254 Code stolen from Lennart.
257 iToBase62 :: Int -> String
259 = ASSERT(n_ >= 0) go (iUnbox n_) ""
261 go n cs | n <# _ILIT(62)
262 = case chooseChar62 n of { c -> c `seq` (c : cs) }
264 = case (quotRem (iBox n) 62) of { (q_, r_) ->
265 case iUnbox q_ of { q -> case iUnbox r_ of { r ->
266 case (chooseChar62 r) of { c -> c `seq`
269 chooseChar62 :: FastInt -> Char
270 {-# INLINE chooseChar62 #-}
271 #if defined(__GLASGOW_HASKELL__)
272 --then FastInt == Int#
273 chooseChar62 n = C# (indexCharOffAddr# chars62 n)
274 !chars62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
276 --Haskell98 arrays are portable
277 chooseChar62 n = (!) chars62 n
278 chars62 = listArray (0,61) "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
282 %************************************************************************
284 \subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
286 %************************************************************************
288 Allocation of unique supply characters:
289 v,t,u : for renumbering value-, type- and usage- vars.
291 C-E: pseudo uniques (used in native-code generator)
292 X: uniques derived by deriveUnique
293 _: unifiable tyvars (above)
294 0-9: prelude things below
295 (no numbers left any more..)
296 :: (prelude) parallel array data constructors
298 other a-z: lower case chars for unique supplies. Used so far:
308 mkAlphaTyVarUnique :: Int -> Unique
309 mkPreludeClassUnique :: Int -> Unique
310 mkPreludeTyConUnique :: Int -> Unique
311 mkTupleTyConUnique :: Boxity -> Int -> Unique
312 mkPreludeDataConUnique :: Int -> Unique
313 mkTupleDataConUnique :: Boxity -> Int -> Unique
314 mkPrimOpIdUnique :: Int -> Unique
315 mkPreludeMiscIdUnique :: Int -> Unique
316 mkPArrDataConUnique :: Int -> Unique
318 mkAlphaTyVarUnique i = mkUnique '1' i
320 mkPreludeClassUnique i = mkUnique '2' i
322 -- Prelude type constructors occupy *three* slots.
323 -- The first is for the tycon itself; the latter two
324 -- are for the generic to/from Ids. See TysWiredIn.mk_tc_gen_info.
326 mkPreludeTyConUnique i = mkUnique '3' (3*i)
327 mkTupleTyConUnique Boxed a = mkUnique '4' (3*a)
328 mkTupleTyConUnique Unboxed a = mkUnique '5' (3*a)
330 -- Data constructor keys occupy *two* slots. The first is used for the
331 -- data constructor itself and its wrapper function (the function that
332 -- evaluates arguments as necessary and calls the worker). The second is
333 -- used for the worker function (the function that builds the constructor
336 mkPreludeDataConUnique i = mkUnique '6' (2*i) -- Must be alphabetic
337 mkTupleDataConUnique Boxed a = mkUnique '7' (2*a) -- ditto (*may* be used in C labels)
338 mkTupleDataConUnique Unboxed a = mkUnique '8' (2*a)
340 -- This one is used for a tiresome reason
341 -- to improve a consistency-checking error check in the renamer
342 isTupleKey u = case unpkUnique u of
343 (tag,_) -> tag == '4' || tag == '5' || tag == '7' || tag == '8'
345 mkPrimOpIdUnique op = mkUnique '9' op
346 mkPreludeMiscIdUnique i = mkUnique '0' i
348 -- No numbers left anymore, so I pick something different for the character tag
349 mkPArrDataConUnique a = mkUnique ':' (2*a)
351 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
352 -- See pprUnique for details
354 initTyVarUnique :: Unique
355 initTyVarUnique = mkUnique 't' 0
357 mkPseudoUniqueC, mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
358 mkBuiltinUnique :: Int -> Unique
360 mkBuiltinUnique i = mkUnique 'B' i
361 mkPseudoUniqueC i = mkUnique 'C' i -- used for getUnique on Regs
362 mkPseudoUniqueD i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
363 mkPseudoUniqueE i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
364 mkPseudoUniqueH i = mkUnique 'H' i -- used in NCG spiller to create spill VirtualRegs
366 mkRegSingleUnique, mkRegPairUnique, mkRegSubUnique, mkRegClassUnique :: Int -> Unique
367 mkRegSingleUnique = mkUnique 'R'
368 mkRegSubUnique = mkUnique 'S'
369 mkRegPairUnique = mkUnique 'P'
370 mkRegClassUnique = mkUnique 'L'
372 mkVarOccUnique, mkDataOccUnique, mkTvOccUnique, mkTcOccUnique :: FastString -> Unique
373 -- See Note [The Unique of an OccName] in OccName
374 mkVarOccUnique fs = mkUnique 'i' (iBox (uniqueOfFS fs))
375 mkDataOccUnique fs = mkUnique 'd' (iBox (uniqueOfFS fs))
376 mkTvOccUnique fs = mkUnique 'v' (iBox (uniqueOfFS fs))
377 mkTcOccUnique fs = mkUnique 'c' (iBox (uniqueOfFS fs))