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,
21 pprUnique, pprUnique10,
23 mkUnique, -- Used in UniqSupply
24 mkUniqueGrimily, -- Used in UniqSupply only!
25 getKey, -- Used in Var, UniqFM, Name only!
27 incrUnique, -- Used for renumbering
28 deriveUnique, -- Ditto
29 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,
44 mkBuiltinUnique, builtinUniques,
45 mkPseudoUnique1, mkPseudoUnique2, mkPseudoUnique3
48 #include "HsVersions.h"
50 import BasicTypes ( Boxity(..) )
51 import FastString ( FastString, uniqueOfFS )
57 import 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#
73 Now come the functions which construct uniques from their pieces, and vice versa.
74 The stuff about unique *supplies* is handled further down this module.
77 mkUnique :: Char -> Int -> Unique -- Builds a unique from pieces
78 unpkUnique :: Unique -> (Char, Int) -- The reverse
80 mkUniqueGrimily :: Int# -> Unique -- A trap-door for UniqSupply
82 getKey :: Unique -> Int# -- for Var
84 incrUnique :: Unique -> Unique
85 deriveUnique :: Unique -> Int -> Unique
86 newTagUnique :: Unique -> Char -> Unique
88 isTupleKey :: Unique -> Bool
93 mkUniqueGrimily x = MkUnique x
96 getKey (MkUnique x) = x
98 incrUnique (MkUnique i) = MkUnique (i +# 1#)
100 -- deriveUnique uses an 'X' tag so that it won't clash with
101 -- any of the uniques produced any other way
102 deriveUnique (MkUnique i) delta = mkUnique 'X' (I# i + delta)
104 -- newTagUnique changes the "domain" of a unique to a different char
105 newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u
107 -- pop the Char in the top 8 bits of the Unique(Supply)
109 -- No 64-bit bugs here, as long as we have at least 32 bits. --JSM
115 mkUnique (C# c) (I# i)
116 = MkUnique (w2i (tag `or#` bits))
118 #if __GLASGOW_HASKELL__ >= 503
119 tag = i2w (ord# c) `uncheckedShiftL#` i2w_s 24#
121 tag = i2w (ord# c) `shiftL#` i2w_s 24#
123 bits = i2w i `and#` (i2w 16777215#){-``0x00ffffff''-}
125 unpkUnique (MkUnique u)
127 tag = C# (chr# (w2i ((i2w u) `shiftr` (i2w_s 24#))))
128 i = I# (w2i ((i2w u) `and#` (i2w 16777215#){-``0x00ffffff''-}))
132 #if __GLASGOW_HASKELL__ >= 503
133 shiftr x y = uncheckedShiftRL# x y
135 shiftr x y = shiftRL# x y
141 %************************************************************************
143 \subsection[Uniquable-class]{The @Uniquable@ class}
145 %************************************************************************
148 class Uniquable a where
149 getUnique :: a -> Unique
151 hasKey :: Uniquable a => a -> Unique -> Bool
152 x `hasKey` k = getUnique x == k
154 instance Uniquable FastString where
155 getUnique fs = mkUniqueGrimily (uniqueOfFS fs)
157 instance Uniquable Int where
158 getUnique (I# i#) = mkUniqueGrimily i#
162 %************************************************************************
164 \subsection[Unique-instances]{Instance declarations for @Unique@}
166 %************************************************************************
168 And the whole point (besides uniqueness) is fast equality. We don't
169 use `deriving' because we want {\em precise} control of ordering
170 (equality on @Uniques@ is v common).
173 eqUnique (MkUnique u1) (MkUnique u2) = u1 ==# u2
174 ltUnique (MkUnique u1) (MkUnique u2) = u1 <# u2
175 leUnique (MkUnique u1) (MkUnique u2) = u1 <=# u2
177 cmpUnique (MkUnique u1) (MkUnique u2)
178 = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
180 instance Eq Unique where
181 a == b = eqUnique a b
182 a /= b = not (eqUnique a b)
184 instance Ord Unique where
186 a <= b = leUnique a b
187 a > b = not (leUnique a b)
188 a >= b = not (ltUnique a b)
189 compare a b = cmpUnique a b
192 instance Uniquable Unique where
196 We do sometimes make strings with @Uniques@ in them:
198 pprUnique, pprUnique10 :: Unique -> SDoc
201 = case unpkUnique uniq of
202 (tag, u) -> finish_ppr tag u (iToBase62 u)
204 pprUnique10 uniq -- in base-10, dudes
205 = case unpkUnique uniq of
206 (tag, u) -> finish_ppr tag u (int u)
208 finish_ppr 't' u pp_u | u < 26
209 = -- Special case to make v common tyvars, t1, t2, ...
210 -- come out as a, b, ... (shorter, easier to read)
211 char (chr (ord 'a' + u))
212 finish_ppr tag u pp_u = char tag <> pp_u
214 instance Outputable Unique where
217 instance Show Unique where
218 showsPrec p uniq = showsPrecSDoc p (pprUnique uniq)
221 %************************************************************************
223 \subsection[Utils-base62]{Base-62 numbers}
225 %************************************************************************
227 A character-stingy way to read/write numbers (notably Uniques).
228 The ``62-its'' are \tr{[0-9a-zA-Z]}. We don't handle negative Ints.
229 Code stolen from Lennart.
232 iToBase62 :: Int -> SDoc
237 case (indexCharOffAddr# chars62# n#) of { c ->
240 case (quotRem n 62) of { (q, I# r#) ->
241 case (indexCharOffAddr# chars62# r#) of { c ->
242 (<>) (iToBase62 q) (char (C# c)) }}
244 chars62# = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
247 %************************************************************************
249 \subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
251 %************************************************************************
253 Allocation of unique supply characters:
254 v,t,u : for renumbering value-, type- and usage- vars.
255 other a-z: lower case chars for unique supplies (see Main.lhs)
257 C-E: pseudo uniques (used in native-code generator)
258 X: uniques derived by deriveUnique
259 _: unifiable tyvars (above)
260 0-9: prelude things below
263 mkAlphaTyVarUnique i = mkUnique '1' i
265 mkPreludeClassUnique i = mkUnique '2' i
267 -- Prelude type constructors occupy *three* slots.
268 -- The first is for the tycon itself; the latter two
269 -- are for the generic to/from Ids. See TysWiredIn.mk_tc_gen_info.
271 mkPreludeTyConUnique i = mkUnique '3' (3*i)
272 mkTupleTyConUnique Boxed a = mkUnique '4' (3*a)
273 mkTupleTyConUnique Unboxed a = mkUnique '5' (3*a)
275 -- Data constructor keys occupy *two* slots. The first is used for the
276 -- data constructor itself and its wrapper function (the function that
277 -- evaluates arguments as necessary and calls the worker). The second is
278 -- used for the worker function (the function that builds the constructor
281 mkPreludeDataConUnique i = mkUnique '6' (2*i) -- Must be alphabetic
282 mkTupleDataConUnique Boxed a = mkUnique '7' (2*a) -- ditto (*may* be used in C labels)
283 mkTupleDataConUnique Unboxed a = mkUnique '8' (2*a)
285 -- This one is used for a tiresome reason
286 -- to improve a consistency-checking error check in the renamer
287 isTupleKey u = case unpkUnique u of
288 (tag,_) -> tag == '4' || tag == '5' || tag == '7' || tag == '8'
290 mkPrimOpIdUnique op = mkUnique '9' op
291 mkPreludeMiscIdUnique i = mkUnique '0' i
293 -- No numbers left anymore, so I pick something different for the character
295 mkPArrDataConUnique a = mkUnique ':' (2*a)
297 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
298 -- See pprUnique for details
300 initTyVarUnique :: Unique
301 initTyVarUnique = mkUnique 't' 0
303 initTidyUniques :: (Unique, Unique) -- Global and local
304 initTidyUniques = (mkUnique 'g' 0, mkUnique 'x' 0)
306 mkPseudoUnique1, mkPseudoUnique2, mkPseudoUnique3,
307 mkBuiltinUnique :: Int -> Unique
309 builtinUniques :: [Unique]
310 builtinUniques = map mkBuiltinUnique [1..]
312 mkBuiltinUnique i = mkUnique 'B' i
313 mkPseudoUnique1 i = mkUnique 'C' i -- used for getUnique on Regs
314 mkPseudoUnique2 i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
315 mkPseudoUnique3 i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs