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"
65 #if defined(__GLASGOW_HASKELL__)
66 --just for implementing a fast [0,61) -> Char function
67 import GHC.Exts (indexCharOffAddr#, Char(..))
71 import Data.Char ( chr, ord )
74 %************************************************************************
76 \subsection[Unique-type]{@Unique@ type and operations}
78 %************************************************************************
80 The @Chars@ are ``tag letters'' that identify the @UniqueSupply@.
81 Fast comparison is everything on @Uniques@:
84 --why not newtype Int?
86 -- | The type of unique identifiers that are used in many places in GHC
87 -- for fast ordering and equality tests. You should generate these with
88 -- the functions from the 'UniqSupply' module
89 data Unique = MkUnique FastInt
92 Now come the functions which construct uniques from their pieces, and vice versa.
93 The stuff about unique *supplies* is handled further down this module.
96 mkUnique :: Char -> Int -> Unique -- Builds a unique from pieces
97 unpkUnique :: Unique -> (Char, Int) -- The reverse
99 mkUniqueGrimily :: Int -> Unique -- A trap-door for UniqSupply
100 getKey :: Unique -> Int -- for Var
101 getKeyFastInt :: Unique -> FastInt -- for Var
103 incrUnique :: Unique -> Unique
104 deriveUnique :: Unique -> Int -> Unique
105 newTagUnique :: Unique -> Char -> Unique
107 isTupleKey :: Unique -> Bool
112 mkUniqueGrimily x = MkUnique (iUnbox x)
114 {-# INLINE getKey #-}
115 getKey (MkUnique x) = iBox x
116 {-# INLINE getKeyFastInt #-}
117 getKeyFastInt (MkUnique x) = x
119 incrUnique (MkUnique i) = MkUnique (i +# _ILIT(1))
121 -- deriveUnique uses an 'X' tag so that it won't clash with
122 -- any of the uniques produced any other way
123 deriveUnique (MkUnique i) delta = mkUnique 'X' (iBox i + delta)
125 -- newTagUnique changes the "domain" of a unique to a different char
126 newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u
128 -- pop the Char in the top 8 bits of the Unique(Supply)
130 -- No 64-bit bugs here, as long as we have at least 32 bits. --JSM
132 -- and as long as the Char fits in 8 bits, which we assume anyway!
135 = MkUnique (tag `bitOrFastInt` bits)
137 tag = fastOrd (cUnbox c) `shiftLFastInt` _ILIT(24)
138 bits = iUnbox i `bitAndFastInt` _ILIT(16777215){-``0x00ffffff''-}
140 unpkUnique (MkUnique u)
142 -- as long as the Char may have its eighth bit set, we
143 -- really do need the logical right-shift here!
144 tag = cBox (fastChr (u `shiftRLFastInt` _ILIT(24)))
145 i = iBox (u `bitAndFastInt` _ILIT(16777215){-``0x00ffffff''-})
152 %************************************************************************
154 \subsection[Uniquable-class]{The @Uniquable@ class}
156 %************************************************************************
159 -- | Class of things that we can obtain a 'Unique' from
160 class Uniquable a where
161 getUnique :: a -> Unique
163 hasKey :: Uniquable a => a -> Unique -> Bool
164 x `hasKey` k = getUnique x == k
166 instance Uniquable FastString where
167 getUnique fs = mkUniqueGrimily (iBox (uniqueOfFS fs))
169 instance Uniquable Int where
170 getUnique i = mkUniqueGrimily i
174 %************************************************************************
176 \subsection[Unique-instances]{Instance declarations for @Unique@}
178 %************************************************************************
180 And the whole point (besides uniqueness) is fast equality. We don't
181 use `deriving' because we want {\em precise} control of ordering
182 (equality on @Uniques@ is v common).
185 eqUnique, ltUnique, leUnique :: Unique -> Unique -> Bool
186 eqUnique (MkUnique u1) (MkUnique u2) = u1 ==# u2
187 ltUnique (MkUnique u1) (MkUnique u2) = u1 <# u2
188 leUnique (MkUnique u1) (MkUnique u2) = u1 <=# u2
190 cmpUnique :: Unique -> Unique -> Ordering
191 cmpUnique (MkUnique u1) (MkUnique u2)
192 = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
194 instance Eq Unique where
195 a == b = eqUnique a b
196 a /= b = not (eqUnique a b)
198 instance Ord Unique where
200 a <= b = leUnique a b
201 a > b = not (leUnique a b)
202 a >= b = not (ltUnique a b)
203 compare a b = cmpUnique a b
206 instance Uniquable Unique where
210 We do sometimes make strings with @Uniques@ in them:
212 pprUnique :: Unique -> SDoc
214 | opt_SuppressUniques
215 = empty -- Used exclusively to suppress uniques so you
216 | otherwise -- can compare output easily
217 = case unpkUnique uniq of
218 (tag, u) -> finish_ppr tag u (text (iToBase62 u))
221 pprUnique10 :: Unique -> SDoc
222 pprUnique10 uniq -- in base-10, dudes
223 = case unpkUnique uniq of
224 (tag, u) -> finish_ppr tag u (int u)
227 finish_ppr :: Char -> Int -> SDoc -> SDoc
228 finish_ppr 't' u _pp_u | u < 26
229 = -- Special case to make v common tyvars, t1, t2, ...
230 -- come out as a, b, ... (shorter, easier to read)
231 char (chr (ord 'a' + u))
232 finish_ppr tag _ pp_u = char tag <> pp_u
234 instance Outputable Unique where
237 instance Show Unique where
238 showsPrec p uniq = showsPrecSDoc p (pprUnique uniq)
241 %************************************************************************
243 \subsection[Utils-base62]{Base-62 numbers}
245 %************************************************************************
247 A character-stingy way to read/write numbers (notably Uniques).
248 The ``62-its'' are \tr{[0-9a-zA-Z]}. We don't handle negative Ints.
249 Code stolen from Lennart.
252 iToBase62 :: Int -> String
254 = ASSERT(n_ >= 0) go (iUnbox n_) ""
256 go n cs | n <# _ILIT(62)
257 = case chooseChar62 n of { c -> c `seq` (c : cs) }
259 = case (quotRem (iBox n) 62) of { (q_, r_) ->
260 case iUnbox q_ of { q -> case iUnbox r_ of { r ->
261 case (chooseChar62 r) of { c -> c `seq`
264 chooseChar62 :: FastInt -> Char
265 {-# INLINE chooseChar62 #-}
266 #if defined(__GLASGOW_HASKELL__)
267 --then FastInt == Int#
268 chooseChar62 n = C# (indexCharOffAddr# chars62 n)
269 chars62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
271 --Haskell98 arrays are portable
272 chooseChar62 n = (!) chars62 n
273 chars62 = listArray (0,61) "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
277 %************************************************************************
279 \subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
281 %************************************************************************
283 Allocation of unique supply characters:
284 v,t,u : for renumbering value-, type- and usage- vars.
286 C-E: pseudo uniques (used in native-code generator)
287 X: uniques derived by deriveUnique
288 _: unifiable tyvars (above)
289 0-9: prelude things below
290 (no numbers left any more..)
291 :: (prelude) parallel array data constructors
293 other a-z: lower case chars for unique supplies. Used so far:
303 mkAlphaTyVarUnique :: Int -> Unique
304 mkPreludeClassUnique :: Int -> Unique
305 mkPreludeTyConUnique :: Int -> Unique
306 mkTupleTyConUnique :: Boxity -> Int -> Unique
307 mkPreludeDataConUnique :: Int -> Unique
308 mkTupleDataConUnique :: Boxity -> Int -> Unique
309 mkPrimOpIdUnique :: Int -> Unique
310 mkPreludeMiscIdUnique :: Int -> Unique
311 mkPArrDataConUnique :: Int -> Unique
313 mkAlphaTyVarUnique i = mkUnique '1' i
315 mkPreludeClassUnique i = mkUnique '2' i
317 -- Prelude type constructors occupy *three* slots.
318 -- The first is for the tycon itself; the latter two
319 -- are for the generic to/from Ids. See TysWiredIn.mk_tc_gen_info.
321 mkPreludeTyConUnique i = mkUnique '3' (3*i)
322 mkTupleTyConUnique Boxed a = mkUnique '4' (3*a)
323 mkTupleTyConUnique Unboxed a = mkUnique '5' (3*a)
325 -- Data constructor keys occupy *two* slots. The first is used for the
326 -- data constructor itself and its wrapper function (the function that
327 -- evaluates arguments as necessary and calls the worker). The second is
328 -- used for the worker function (the function that builds the constructor
331 mkPreludeDataConUnique i = mkUnique '6' (2*i) -- Must be alphabetic
332 mkTupleDataConUnique Boxed a = mkUnique '7' (2*a) -- ditto (*may* be used in C labels)
333 mkTupleDataConUnique Unboxed a = mkUnique '8' (2*a)
335 -- This one is used for a tiresome reason
336 -- to improve a consistency-checking error check in the renamer
337 isTupleKey u = case unpkUnique u of
338 (tag,_) -> tag == '4' || tag == '5' || tag == '7' || tag == '8'
340 mkPrimOpIdUnique op = mkUnique '9' op
341 mkPreludeMiscIdUnique i = mkUnique '0' i
343 -- No numbers left anymore, so I pick something different for the character
345 mkPArrDataConUnique a = mkUnique ':' (2*a)
347 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
348 -- See pprUnique for details
350 initTyVarUnique :: Unique
351 initTyVarUnique = mkUnique 't' 0
353 mkPseudoUniqueC, mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
354 mkBuiltinUnique :: Int -> Unique
356 mkBuiltinUnique i = mkUnique 'B' i
357 mkPseudoUniqueC i = mkUnique 'C' i -- used for getUnique on Regs
358 mkPseudoUniqueD i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
359 mkPseudoUniqueE i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
360 mkPseudoUniqueH i = mkUnique 'H' i -- used in NCG spiller to create spill VirtualRegs