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, getKeyFastInt, -- 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"
58 import StaticFlags ( opt_SuppressUniques )
61 #if defined(__GLASGOW_HASKELL__)
62 --just for implementing a fast [0,61) -> Char function
63 import GHC.Exts (indexCharOffAddr#, Char(..))
67 import Data.Char ( chr, ord )
70 %************************************************************************
72 \subsection[Unique-type]{@Unique@ type and operations}
74 %************************************************************************
76 The @Chars@ are ``tag letters'' that identify the @UniqueSupply@.
77 Fast comparison is everything on @Uniques@:
80 --why not newtype Int?
81 data Unique = MkUnique FastInt
84 Now come the functions which construct uniques from their pieces, and vice versa.
85 The stuff about unique *supplies* is handled further down this module.
88 mkUnique :: Char -> Int -> Unique -- Builds a unique from pieces
89 unpkUnique :: Unique -> (Char, Int) -- The reverse
91 mkUniqueGrimily :: Int -> Unique -- A trap-door for UniqSupply
92 getKey :: Unique -> Int -- for Var
93 getKeyFastInt :: Unique -> FastInt -- for Var
95 incrUnique :: Unique -> Unique
96 deriveUnique :: Unique -> Int -> Unique
97 newTagUnique :: Unique -> Char -> Unique
99 isTupleKey :: Unique -> Bool
104 mkUniqueGrimily x = MkUnique (iUnbox x)
106 {-# INLINE getKey #-}
107 getKey (MkUnique x) = iBox x
108 {-# INLINE getKeyFastInt #-}
109 getKeyFastInt (MkUnique x) = x
111 incrUnique (MkUnique i) = MkUnique (i +# _ILIT(1))
113 -- deriveUnique uses an 'X' tag so that it won't clash with
114 -- any of the uniques produced any other way
115 deriveUnique (MkUnique i) delta = mkUnique 'X' (iBox i + delta)
117 -- newTagUnique changes the "domain" of a unique to a different char
118 newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u
120 -- pop the Char in the top 8 bits of the Unique(Supply)
122 -- No 64-bit bugs here, as long as we have at least 32 bits. --JSM
124 -- and as long as the Char fits in 8 bits, which we assume anyway!
127 = MkUnique (tag `bitOrFastInt` bits)
129 tag = fastOrd (cUnbox c) `shiftLFastInt` _ILIT(24)
130 bits = iUnbox i `bitAndFastInt` _ILIT(16777215){-``0x00ffffff''-}
132 unpkUnique (MkUnique u)
134 -- as long as the Char may have its eighth bit set, we
135 -- really do need the logical right-shift here!
136 tag = cBox (fastChr (u `shiftRLFastInt` _ILIT(24)))
137 i = iBox (u `bitAndFastInt` _ILIT(16777215){-``0x00ffffff''-})
144 %************************************************************************
146 \subsection[Uniquable-class]{The @Uniquable@ class}
148 %************************************************************************
151 class Uniquable a where
152 getUnique :: a -> Unique
154 hasKey :: Uniquable a => a -> Unique -> Bool
155 x `hasKey` k = getUnique x == k
157 instance Uniquable FastString where
158 getUnique fs = mkUniqueGrimily (iBox (uniqueOfFS fs))
160 instance Uniquable Int where
161 getUnique i = mkUniqueGrimily i
165 %************************************************************************
167 \subsection[Unique-instances]{Instance declarations for @Unique@}
169 %************************************************************************
171 And the whole point (besides uniqueness) is fast equality. We don't
172 use `deriving' because we want {\em precise} control of ordering
173 (equality on @Uniques@ is v common).
176 eqUnique, ltUnique, leUnique :: Unique -> Unique -> Bool
177 eqUnique (MkUnique u1) (MkUnique u2) = u1 ==# u2
178 ltUnique (MkUnique u1) (MkUnique u2) = u1 <# u2
179 leUnique (MkUnique u1) (MkUnique u2) = u1 <=# u2
181 cmpUnique :: Unique -> Unique -> Ordering
182 cmpUnique (MkUnique u1) (MkUnique u2)
183 = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
185 instance Eq Unique where
186 a == b = eqUnique a b
187 a /= b = not (eqUnique a b)
189 instance Ord Unique where
191 a <= b = leUnique a b
192 a > b = not (leUnique a b)
193 a >= b = not (ltUnique a b)
194 compare a b = cmpUnique a b
197 instance Uniquable Unique where
201 We do sometimes make strings with @Uniques@ in them:
203 pprUnique :: Unique -> SDoc
206 | opt_SuppressUniques
207 = empty -- Used exclusively to suppress uniques so you
208 | otherwise -- can compare output easily
210 = case unpkUnique uniq of
211 (tag, u) -> finish_ppr tag u (text (iToBase62 u))
214 pprUnique10 :: Unique -> SDoc
215 pprUnique10 uniq -- in base-10, dudes
216 = case unpkUnique uniq of
217 (tag, u) -> finish_ppr tag u (int u)
220 finish_ppr :: Char -> Int -> SDoc -> SDoc
221 finish_ppr 't' u _pp_u | u < 26
222 = -- Special case to make v common tyvars, t1, t2, ...
223 -- come out as a, b, ... (shorter, easier to read)
224 char (chr (ord 'a' + u))
225 finish_ppr tag _ pp_u = char tag <> pp_u
227 instance Outputable Unique where
230 instance Show Unique where
231 showsPrec p uniq = showsPrecSDoc p (pprUnique uniq)
234 %************************************************************************
236 \subsection[Utils-base62]{Base-62 numbers}
238 %************************************************************************
240 A character-stingy way to read/write numbers (notably Uniques).
241 The ``62-its'' are \tr{[0-9a-zA-Z]}. We don't handle negative Ints.
242 Code stolen from Lennart.
245 iToBase62 :: Int -> String
247 = ASSERT(n_ >= 0) go (iUnbox n_) ""
249 go n cs | n <# _ILIT(62)
250 = case chooseChar62 n of { c -> c `seq` (c : cs) }
252 = case (quotRem (iBox n) 62) of { (q_, r_) ->
253 case iUnbox q_ of { q -> case iUnbox r_ of { r ->
254 case (chooseChar62 r) of { c -> c `seq`
257 chooseChar62 :: FastInt -> Char
258 {-# INLINE chooseChar62 #-}
259 #if defined(__GLASGOW_HASKELL__)
260 --then FastInt == Int#
261 chooseChar62 n = C# (indexCharOffAddr# chars62 n)
262 chars62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
264 --Haskell98 arrays are portable
265 chooseChar62 n = (!) chars62 n
266 chars62 = listArray (0,61) "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
270 %************************************************************************
272 \subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
274 %************************************************************************
276 Allocation of unique supply characters:
277 v,t,u : for renumbering value-, type- and usage- vars.
279 C-E: pseudo uniques (used in native-code generator)
280 X: uniques derived by deriveUnique
281 _: unifiable tyvars (above)
282 0-9: prelude things below
284 other a-z: lower case chars for unique supplies. Used so far:
295 mkAlphaTyVarUnique :: Int -> Unique
296 mkPreludeClassUnique :: Int -> Unique
297 mkPreludeTyConUnique :: Int -> Unique
298 mkTupleTyConUnique :: Boxity -> Int -> Unique
299 mkPreludeDataConUnique :: Int -> Unique
300 mkTupleDataConUnique :: Boxity -> Int -> Unique
301 mkPrimOpIdUnique :: Int -> Unique
302 mkPreludeMiscIdUnique :: Int -> Unique
303 mkPArrDataConUnique :: Int -> Unique
305 mkAlphaTyVarUnique i = mkUnique '1' i
307 mkPreludeClassUnique i = mkUnique '2' i
309 -- Prelude type constructors occupy *three* slots.
310 -- The first is for the tycon itself; the latter two
311 -- are for the generic to/from Ids. See TysWiredIn.mk_tc_gen_info.
313 mkPreludeTyConUnique i = mkUnique '3' (3*i)
314 mkTupleTyConUnique Boxed a = mkUnique '4' (3*a)
315 mkTupleTyConUnique Unboxed a = mkUnique '5' (3*a)
317 -- Data constructor keys occupy *two* slots. The first is used for the
318 -- data constructor itself and its wrapper function (the function that
319 -- evaluates arguments as necessary and calls the worker). The second is
320 -- used for the worker function (the function that builds the constructor
323 mkPreludeDataConUnique i = mkUnique '6' (2*i) -- Must be alphabetic
324 mkTupleDataConUnique Boxed a = mkUnique '7' (2*a) -- ditto (*may* be used in C labels)
325 mkTupleDataConUnique Unboxed a = mkUnique '8' (2*a)
327 -- This one is used for a tiresome reason
328 -- to improve a consistency-checking error check in the renamer
329 isTupleKey u = case unpkUnique u of
330 (tag,_) -> tag == '4' || tag == '5' || tag == '7' || tag == '8'
332 mkPrimOpIdUnique op = mkUnique '9' op
333 mkPreludeMiscIdUnique i = mkUnique '0' i
335 -- No numbers left anymore, so I pick something different for the character
337 mkPArrDataConUnique a = mkUnique ':' (2*a)
339 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
340 -- See pprUnique for details
342 initTyVarUnique :: Unique
343 initTyVarUnique = mkUnique 't' 0
345 mkPseudoUniqueC, mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
346 mkBuiltinUnique :: Int -> Unique
348 mkBuiltinUnique i = mkUnique 'B' i
349 mkPseudoUniqueC i = mkUnique 'C' i -- used for getUnique on Regs
350 mkPseudoUniqueD i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
351 mkPseudoUniqueE i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
352 mkPseudoUniqueH i = mkUnique 'H' i -- used in NCG spiller to create spill VirtualRegs