Add kind equalities to GHC.
[ghc.git] / compiler / basicTypes / Unique.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4
5
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.
9
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
12 directed to that end.
13
14 Some of the other hair in this code is to be able to use a
15 ``splittable @UniqueSupply@'' if requested/possible (not standard
16 Haskell).
17 -}
18
19 {-# LANGUAGE CPP, BangPatterns, MagicHash #-}
20
21 module Unique (
22 -- * Main data types
23 Unique, Uniquable(..),
24
25 -- ** Constructors, desctructors and operations on 'Unique's
26 hasKey, cmpByUnique,
27
28 pprUnique,
29
30 mkUniqueGrimily, -- Used in UniqSupply only!
31 getKey, -- Used in Var, UniqFM, Name only!
32 mkUnique, unpkUnique, -- Used in BinIface only
33
34 incrUnique, -- Used for renumbering
35 deriveUnique, -- Ditto
36 newTagUnique, -- Used in CgCase
37 initTyVarUnique,
38
39 -- ** Making built-in uniques
40
41 -- now all the built-in Uniques (and functions to make them)
42 -- [the Oh-So-Wonderful Haskell module system wins again...]
43 mkAlphaTyVarUnique,
44 mkPrimOpIdUnique,
45 mkTupleTyConUnique, mkTupleDataConUnique,
46 mkCTupleTyConUnique,
47 mkPreludeMiscIdUnique, mkPreludeDataConUnique,
48 mkPreludeTyConUnique, mkPreludeClassUnique,
49 mkPArrDataConUnique, mkCoVarUnique,
50
51 mkVarOccUnique, mkDataOccUnique, mkTvOccUnique, mkTcOccUnique,
52 mkRegSingleUnique, mkRegPairUnique, mkRegClassUnique, mkRegSubUnique,
53 mkCostCentreUnique,
54
55 tyConRepNameUnique,
56 dataConWorkerUnique, dataConRepNameUnique,
57
58 mkBuiltinUnique,
59 mkPseudoUniqueD,
60 mkPseudoUniqueE,
61 mkPseudoUniqueH
62 ) where
63
64 #include "HsVersions.h"
65
66 import BasicTypes
67 import FastString
68 import Outputable
69 import Util
70
71 -- just for implementing a fast [0,61) -> Char function
72 import GHC.Exts (indexCharOffAddr#, Char(..), Int(..))
73
74 import Data.Char ( chr, ord )
75 import Data.Bits
76
77 {-
78 ************************************************************************
79 * *
80 \subsection[Unique-type]{@Unique@ type and operations}
81 * *
82 ************************************************************************
83
84 The @Chars@ are ``tag letters'' that identify the @UniqueSupply@.
85 Fast comparison is everything on @Uniques@:
86 -}
87
88 --why not newtype Int?
89
90 -- | The type of unique identifiers that are used in many places in GHC
91 -- for fast ordering and equality tests. You should generate these with
92 -- the functions from the 'UniqSupply' module
93 data Unique = MkUnique {-# UNPACK #-} !Int
94
95 {-
96 Now come the functions which construct uniques from their pieces, and vice versa.
97 The stuff about unique *supplies* is handled further down this module.
98 -}
99
100 unpkUnique :: Unique -> (Char, Int) -- The reverse
101
102 mkUniqueGrimily :: Int -> Unique -- A trap-door for UniqSupply
103 getKey :: Unique -> Int -- for Var
104
105 incrUnique :: Unique -> Unique
106 stepUnique :: Unique -> Int -> Unique
107 deriveUnique :: Unique -> Int -> Unique
108 newTagUnique :: Unique -> Char -> Unique
109
110 mkUniqueGrimily = MkUnique
111
112 {-# INLINE getKey #-}
113 getKey (MkUnique x) = x
114
115 incrUnique (MkUnique i) = MkUnique (i + 1)
116 stepUnique (MkUnique i) n = MkUnique (i + n)
117
118 -- deriveUnique uses an 'X' tag so that it won't clash with
119 -- any of the uniques produced any other way
120 -- SPJ says: this looks terribly smelly to me!
121 deriveUnique (MkUnique i) delta = mkUnique 'X' (i + delta)
122
123 -- newTagUnique changes the "domain" of a unique to a different char
124 newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u
125
126 -- pop the Char in the top 8 bits of the Unique(Supply)
127
128 -- No 64-bit bugs here, as long as we have at least 32 bits. --JSM
129
130 -- and as long as the Char fits in 8 bits, which we assume anyway!
131
132 mkUnique :: Char -> Int -> Unique -- Builds a unique from pieces
133 -- NOT EXPORTED, so that we can see all the Chars that
134 -- are used in this one module
135 mkUnique c i
136 = MkUnique (tag .|. bits)
137 where
138 tag = ord c `shiftL` 24
139 bits = i .&. 16777215 {-``0x00ffffff''-}
140
141 unpkUnique (MkUnique u)
142 = let
143 -- as long as the Char may have its eighth bit set, we
144 -- really do need the logical right-shift here!
145 tag = chr (u `shiftR` 24)
146 i = u .&. 16777215 {-``0x00ffffff''-}
147 in
148 (tag, i)
149
150 {-
151 ************************************************************************
152 * *
153 \subsection[Uniquable-class]{The @Uniquable@ class}
154 * *
155 ************************************************************************
156 -}
157
158 -- | Class of things that we can obtain a 'Unique' from
159 class Uniquable a where
160 getUnique :: a -> Unique
161
162 hasKey :: Uniquable a => a -> Unique -> Bool
163 x `hasKey` k = getUnique x == k
164
165 instance Uniquable FastString where
166 getUnique fs = mkUniqueGrimily (uniqueOfFS fs)
167
168 instance Uniquable Int where
169 getUnique i = mkUniqueGrimily i
170
171 cmpByUnique :: Uniquable a => a -> a -> Ordering
172 cmpByUnique x y = (getUnique x) `cmpUnique` (getUnique y)
173
174 {-
175 ************************************************************************
176 * *
177 \subsection[Unique-instances]{Instance declarations for @Unique@}
178 * *
179 ************************************************************************
180
181 And the whole point (besides uniqueness) is fast equality. We don't
182 use `deriving' because we want {\em precise} control of ordering
183 (equality on @Uniques@ is v common).
184 -}
185
186 -- Note [Unique Determinism]
187 -- ~~~~~~~~~~~~~~~~~~~~~~~~~
188 -- The order of allocated @Uniques@ is not stable across rebuilds.
189 -- The main reason for that is that typechecking interface files pulls
190 -- @Uniques@ from @UniqSupply@ and the interface file for the module being
191 -- currently compiled can, but doesn't have to exist.
192 --
193 -- It gets more complicated if you take into account that the interface
194 -- files are loaded lazily and that building multiple files at once has to
195 -- work for any subset of interface files present. When you add parallelism
196 -- this makes @Uniques@ hopelessly random.
197 --
198 -- As such, to get deterministic builds, the order of the allocated
199 -- @Uniques@ should not affect the final result.
200 -- see also wiki/DeterministicBuilds
201
202 eqUnique, ltUnique, leUnique :: Unique -> Unique -> Bool
203 eqUnique (MkUnique u1) (MkUnique u2) = u1 == u2
204 ltUnique (MkUnique u1) (MkUnique u2) = u1 < u2
205 leUnique (MkUnique u1) (MkUnique u2) = u1 <= u2
206
207 cmpUnique :: Unique -> Unique -> Ordering
208 cmpUnique (MkUnique u1) (MkUnique u2)
209 = if u1 == u2 then EQ else if u1 < u2 then LT else GT
210
211 instance Eq Unique where
212 a == b = eqUnique a b
213 a /= b = not (eqUnique a b)
214
215 instance Ord Unique where
216 a < b = ltUnique a b
217 a <= b = leUnique a b
218 a > b = not (leUnique a b)
219 a >= b = not (ltUnique a b)
220 compare a b = cmpUnique a b
221
222 -----------------
223 instance Uniquable Unique where
224 getUnique u = u
225
226 -- We do sometimes make strings with @Uniques@ in them:
227
228 showUnique :: Unique -> String
229 showUnique uniq
230 = case unpkUnique uniq of
231 (tag, u) -> finish_show tag u (iToBase62 u)
232
233 finish_show :: Char -> Int -> String -> String
234 finish_show 't' u _pp_u | u < 26
235 = -- Special case to make v common tyvars, t1, t2, ...
236 -- come out as a, b, ... (shorter, easier to read)
237 [chr (ord 'a' + u)]
238 finish_show tag _ pp_u = tag : pp_u
239
240 pprUnique :: Unique -> SDoc
241 pprUnique u = text (showUnique u)
242
243 instance Outputable Unique where
244 ppr = pprUnique
245
246 instance Show Unique where
247 show uniq = showUnique uniq
248
249 {-
250 ************************************************************************
251 * *
252 \subsection[Utils-base62]{Base-62 numbers}
253 * *
254 ************************************************************************
255
256 A character-stingy way to read/write numbers (notably Uniques).
257 The ``62-its'' are \tr{[0-9a-zA-Z]}. We don't handle negative Ints.
258 Code stolen from Lennart.
259 -}
260
261 iToBase62 :: Int -> String
262 iToBase62 n_
263 = ASSERT(n_ >= 0) go n_ ""
264 where
265 go n cs | n < 62
266 = let !c = chooseChar62 n in c : cs
267 | otherwise
268 = go q (c : cs) where (q, r) = quotRem n 62
269 !c = chooseChar62 r
270
271 chooseChar62 :: Int -> Char
272 {-# INLINE chooseChar62 #-}
273 chooseChar62 (I# n) = C# (indexCharOffAddr# chars62 n)
274 chars62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
275
276 {-
277 ************************************************************************
278 * *
279 \subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
280 * *
281 ************************************************************************
282
283 Allocation of unique supply characters:
284 v,t,u : for renumbering value-, type- and usage- vars.
285 B: builtin
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
292
293 other a-z: lower case chars for unique supplies. Used so far:
294
295 d desugarer
296 f AbsC flattener
297 g SimplStg
298 n Native codegen
299 r Hsc name cache
300 s simplifier
301 -}
302
303 mkAlphaTyVarUnique :: Int -> Unique
304 mkPreludeClassUnique :: Int -> Unique
305 mkPreludeTyConUnique :: Int -> Unique
306 mkTupleTyConUnique :: Boxity -> Arity -> Unique
307 mkCTupleTyConUnique :: Arity -> Unique
308 mkPreludeDataConUnique :: Arity -> Unique
309 mkTupleDataConUnique :: Boxity -> Arity -> Unique
310 mkPrimOpIdUnique :: Int -> Unique
311 mkPreludeMiscIdUnique :: Int -> Unique
312 mkPArrDataConUnique :: Int -> Unique
313 mkCoVarUnique :: Int -> Unique
314
315 mkAlphaTyVarUnique i = mkUnique '1' i
316 mkCoVarUnique i = mkUnique 'g' i
317 mkPreludeClassUnique i = mkUnique '2' i
318
319 --------------------------------------------------
320 -- Wired-in data constructor keys occupy *three* slots:
321 -- * u: the DataCon itself
322 -- * u+1: its worker Id
323 -- * u+2: the TyConRepName of the promoted TyCon
324 -- Prelude data constructors are too simple to need wrappers.
325 mkPreludeTyConUnique i = mkUnique '3' (3*i)
326 mkTupleTyConUnique Boxed a = mkUnique '4' (3*a)
327 mkTupleTyConUnique Unboxed a = mkUnique '5' (3*a)
328 mkCTupleTyConUnique a = mkUnique 'k' (3*a)
329
330 tyConRepNameUnique :: Unique -> Unique
331 tyConRepNameUnique u = incrUnique u
332
333 -- Data constructor keys occupy *two* slots. The first is used for the
334 -- data constructor itself and its wrapper function (the function that
335 -- evaluates arguments as necessary and calls the worker). The second is
336 -- used for the worker function (the function that builds the constructor
337 -- representation).
338
339 --------------------------------------------------
340 -- Wired-in data constructor keys occupy *three* slots:
341 -- * u: the DataCon itself
342 -- * u+1: its worker Id
343 -- * u+2: the TyConRepName of the promoted TyCon
344 -- Prelude data constructors are too simple to need wrappers.
345
346 mkPreludeDataConUnique i = mkUnique '6' (3*i) -- Must be alphabetic
347 mkTupleDataConUnique Boxed a = mkUnique '7' (3*a) -- ditto (*may* be used in C labels)
348 mkTupleDataConUnique Unboxed a = mkUnique '8' (3*a)
349
350 dataConRepNameUnique, dataConWorkerUnique :: Unique -> Unique
351 dataConWorkerUnique u = incrUnique u
352 dataConRepNameUnique u = stepUnique u 2
353
354 --------------------------------------------------
355 mkPrimOpIdUnique op = mkUnique '9' op
356 mkPreludeMiscIdUnique i = mkUnique '0' i
357
358 -- No numbers left anymore, so I pick something different for the character tag
359 mkPArrDataConUnique a = mkUnique ':' (2*a)
360
361 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
362 -- See pprUnique for details
363
364 initTyVarUnique :: Unique
365 initTyVarUnique = mkUnique 't' 0
366
367 mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
368 mkBuiltinUnique :: Int -> Unique
369
370 mkBuiltinUnique i = mkUnique 'B' i
371 mkPseudoUniqueD i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
372 mkPseudoUniqueE i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
373 mkPseudoUniqueH i = mkUnique 'H' i -- used in NCG spiller to create spill VirtualRegs
374
375 mkRegSingleUnique, mkRegPairUnique, mkRegSubUnique, mkRegClassUnique :: Int -> Unique
376 mkRegSingleUnique = mkUnique 'R'
377 mkRegSubUnique = mkUnique 'S'
378 mkRegPairUnique = mkUnique 'P'
379 mkRegClassUnique = mkUnique 'L'
380
381 mkCostCentreUnique :: Int -> Unique
382 mkCostCentreUnique = mkUnique 'C'
383
384 mkVarOccUnique, mkDataOccUnique, mkTvOccUnique, mkTcOccUnique :: FastString -> Unique
385 -- See Note [The Unique of an OccName] in OccName
386 mkVarOccUnique fs = mkUnique 'i' (uniqueOfFS fs)
387 mkDataOccUnique fs = mkUnique 'd' (uniqueOfFS fs)
388 mkTvOccUnique fs = mkUnique 'v' (uniqueOfFS fs)
389 mkTcOccUnique fs = mkUnique 'c' (uniqueOfFS fs)