2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
5 @Uniques@ are used to distinguish entities in the compiler (@Ids@,
6 @Classes@, etc.) from each other. Thus, @Uniques@ are the basic
7 comparison key in the compiler.
9 If there is any single operation that needs to be fast, it is @Unique@
10 comparison. Unsurprisingly, there is quite a bit of huff-and-puff
13 Some of the other hair in this code is to be able to use a
14 ``splittable @UniqueSupply@'' if requested/possible (not standard
19 Unique, Uniquable(..), hasKey,
20 u2i, -- hack: used in UniqFM
22 pprUnique, pprUnique10,
24 mkUnique, -- Used in UniqSupply
25 mkUniqueGrimily, -- Used in UniqSupply only!
26 getKey, -- Used in Var only!
28 incrUnique, -- Used for renumbering
29 deriveUnique, -- Ditto
30 newTagUnique, -- Used in CgCase
36 -- now all the built-in Uniques (and functions to make them)
37 -- [the Oh-So-Wonderful Haskell module system wins again...]
40 mkTupleTyConUnique, mkTupleDataConUnique,
41 mkPreludeMiscIdUnique, mkPreludeDataConUnique,
42 mkPreludeTyConUnique, mkPreludeClassUnique,
44 getNumBuiltinUniques, getBuiltinUniques, mkBuiltinUnique,
45 mkPseudoUnique1, mkPseudoUnique2, mkPseudoUnique3
48 #include "HsVersions.h"
50 import BasicTypes ( Boxity(..) )
51 import FastString ( FastString, uniqueOfFS )
54 import PrelBase ( Char(..), chr, ord )
60 %************************************************************************
62 \subsection[Unique-type]{@Unique@ type and operations}
64 %************************************************************************
66 The @Chars@ are ``tag letters'' that identify the @UniqueSupply@.
67 Fast comparison is everything on @Uniques@:
70 data Unique = MkUnique Int#
74 u2i :: Unique -> FastInt
78 Now come the functions which construct uniques from their pieces, and vice versa.
79 The stuff about unique *supplies* is handled further down this module.
82 mkUnique :: Char -> Int -> Unique -- Builds a unique from pieces
83 unpkUnique :: Unique -> (Char, Int) -- The reverse
85 mkUniqueGrimily :: Int# -> Unique -- A trap-door for UniqSupply
87 getKey :: Unique -> Int# -- for Var
89 incrUnique :: Unique -> Unique
90 deriveUnique :: Unique -> Int -> Unique
91 newTagUnique :: Unique -> Char -> Unique
93 isTupleKey :: Unique -> Bool
98 mkUniqueGrimily x = MkUnique x
100 {-# INLINE getKey #-}
101 getKey (MkUnique x) = x
103 incrUnique (MkUnique i) = MkUnique (i +# 1#)
105 -- deriveUnique uses an 'X' tag so that it won't clash with
106 -- any of the uniques produced any other way
107 deriveUnique (MkUnique i) delta = mkUnique 'X' (I# i + delta)
109 -- newTagUnique changes the "domain" of a unique to a different char
110 newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u
112 -- pop the Char in the top 8 bits of the Unique(Supply)
114 -- No 64-bit bugs here, as long as we have at least 32 bits. --JSM
120 mkUnique (C# c) (I# i)
121 = MkUnique (w2i (tag `or#` bits))
123 tag = i2w (ord# c) `shiftL#` i2w_s 24#
124 bits = i2w i `and#` (i2w 16777215#){-``0x00ffffff''-}
126 unpkUnique (MkUnique u)
128 tag = C# (chr# (w2i ((i2w u) `shiftr` (i2w_s 24#))))
129 i = I# (w2i ((i2w u) `and#` (i2w 16777215#){-``0x00ffffff''-}))
133 shiftr x y = shiftRL# x y
138 %************************************************************************
140 \subsection[Uniquable-class]{The @Uniquable@ class}
142 %************************************************************************
145 class Uniquable a where
146 getUnique :: a -> Unique
148 hasKey :: Uniquable a => a -> Unique -> Bool
149 x `hasKey` k = getUnique x == k
151 instance Uniquable FastString where
152 getUnique fs = mkUniqueGrimily (uniqueOfFS fs)
154 instance Uniquable Int where
155 getUnique (I# 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, pprUnique10 :: Unique -> SDoc
198 = case unpkUnique uniq of
199 (tag, u) -> finish_ppr tag u (iToBase62 u)
201 pprUnique10 uniq -- in base-10, dudes
202 = case unpkUnique uniq of
203 (tag, u) -> finish_ppr tag u (int u)
205 finish_ppr 't' u pp_u | u < 26
206 = -- Special case to make v common tyvars, t1, t2, ...
207 -- come out as a, b, ... (shorter, easier to read)
208 char (chr (ord 'a' + u))
209 finish_ppr tag u pp_u = char tag <> pp_u
211 instance Outputable Unique where
214 instance Show Unique where
215 showsPrec p uniq = showsPrecSDoc p (pprUnique uniq)
218 %************************************************************************
220 \subsection[Utils-base62]{Base-62 numbers}
222 %************************************************************************
224 A character-stingy way to read/write numbers (notably Uniques).
225 The ``62-its'' are \tr{[0-9a-zA-Z]}. We don't handle negative Ints.
226 Code stolen from Lennart.
228 # define BYTE_ARRAY GlaExts.ByteArray
229 # define RUN_ST ST.runST
230 # define AND_THEN >>=
231 # define AND_THEN_ >>
232 # define RETURN return
234 iToBase62 :: Int -> SDoc
239 #if __GLASGOW_HASKELL__ < 405
240 bytes = case chars62 of { BYTE_ARRAY bounds_who_needs_'em bytes -> bytes }
242 bytes = case chars62 of { BYTE_ARRAY _ _ bytes -> bytes }
246 case (indexCharArray# bytes n#) of { c ->
249 case (quotRem n 62) of { (q, I# r#) ->
250 case (indexCharArray# bytes r#) of { c ->
251 (<>) (iToBase62 q) (char (C# c)) }}
253 -- keep this at top level! (bug on 94/10/24 WDP)
254 chars62 :: BYTE_ARRAY Int
257 newCharArray (0, 61) AND_THEN \ ch_array ->
258 fill_in ch_array 0 62 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
260 unsafeFreezeByteArray ch_array
263 fill_in ch_array i lim str
267 = writeCharArray ch_array i (str !! i) AND_THEN_
268 fill_in ch_array (i+1) lim str
271 %************************************************************************
273 \subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
275 %************************************************************************
277 Allocation of unique supply characters:
278 v,t,u : for renumbering value-, type- and usage- vars.
279 other a-z: lower case chars for unique supplies (see Main.lhs)
281 C-E: pseudo uniques (used in native-code generator)
282 X: uniques derived by deriveUnique
283 _: unifiable tyvars (above)
284 0-9: prelude things below
287 mkAlphaTyVarUnique i = mkUnique '1' i
289 mkPreludeClassUnique i = mkUnique '2' i
291 -- Prelude type constructors occupy *three* slots.
292 -- The first is for the tycon itself; the latter two
293 -- are for the generic to/from Ids. See TysWiredIn.mk_tc_gen_info.
295 mkPreludeTyConUnique i = mkUnique '3' (3*i)
296 mkTupleTyConUnique Boxed a = mkUnique '4' (3*a)
297 mkTupleTyConUnique Unboxed a = mkUnique '5' (3*a)
299 -- Data constructor keys occupy *two* slots. The first is used for the
300 -- data constructor itself and its wrapper function (the function that
301 -- evaluates arguments as necessary and calls the worker). The second is
302 -- used for the worker function (the function that builds the constructor
305 mkPreludeDataConUnique i = mkUnique '6' (2*i) -- Must be alphabetic
306 mkTupleDataConUnique Boxed a = mkUnique '7' (2*a) -- ditto (*may* be used in C labels)
307 mkTupleDataConUnique Unboxed a = mkUnique '8' (2*a)
309 -- This one is used for a tiresome reason
310 -- to improve a consistency-checking error check in the renamer
311 isTupleKey u = case unpkUnique u of
312 (tag,_) -> tag == '4' || tag == '5' || tag == '7' || tag == '8'
314 mkPrimOpIdUnique op = mkUnique '9' op
315 mkPreludeMiscIdUnique i = mkUnique '0' i
317 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
318 -- See pprUnique for details
320 initTyVarUnique :: Unique
321 initTyVarUnique = mkUnique 't' 0
323 initTidyUniques :: (Unique, Unique) -- Global and local
324 initTidyUniques = (mkUnique 'g' 0, mkUnique 'x' 0)
326 mkPseudoUnique1, mkPseudoUnique2, mkPseudoUnique3,
327 mkBuiltinUnique :: Int -> Unique
329 mkBuiltinUnique i = mkUnique 'B' i
330 mkPseudoUnique1 i = mkUnique 'C' i -- used for getUnique on Regs
331 mkPseudoUnique2 i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
332 mkPseudoUnique3 i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
336 getBuiltinUniques :: Int -> [Unique]
337 getBuiltinUniques n = map (mkUnique 'B') [1 .. n]
339 getNumBuiltinUniques :: Int -- First unique
340 -> Int -- Number required
342 getNumBuiltinUniques base n = map (mkUnique 'B') [base .. base+n-1]