Generate Typeable info at definition sites
[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,
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,
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 {-
172 ************************************************************************
173 * *
174 \subsection[Unique-instances]{Instance declarations for @Unique@}
175 * *
176 ************************************************************************
177
178 And the whole point (besides uniqueness) is fast equality. We don't
179 use `deriving' because we want {\em precise} control of ordering
180 (equality on @Uniques@ is v common).
181 -}
182
183 -- Note [Unique Determinism]
184 -- ~~~~~~~~~~~~~~~~~~~~~~~~~
185 -- The order of allocated @Uniques@ is not stable across rebuilds.
186 -- The main reason for that is that typechecking interface files pulls
187 -- @Uniques@ from @UniqSupply@ and the interface file for the module being
188 -- currently compiled can, but doesn't have to exist.
189 --
190 -- It gets more complicated if you take into account that the interface
191 -- files are loaded lazily and that building multiple files at once has to
192 -- work for any subset of interface files present. When you add parallelism
193 -- this makes @Uniques@ hopelessly random.
194 --
195 -- As such, to get deterministic builds, the order of the allocated
196 -- @Uniques@ should not affect the final result.
197 -- see also wiki/DeterministicBuilds
198
199 eqUnique, ltUnique, leUnique :: Unique -> Unique -> Bool
200 eqUnique (MkUnique u1) (MkUnique u2) = u1 == u2
201 ltUnique (MkUnique u1) (MkUnique u2) = u1 < u2
202 leUnique (MkUnique u1) (MkUnique u2) = u1 <= u2
203
204 cmpUnique :: Unique -> Unique -> Ordering
205 cmpUnique (MkUnique u1) (MkUnique u2)
206 = if u1 == u2 then EQ else if u1 < u2 then LT else GT
207
208 instance Eq Unique where
209 a == b = eqUnique a b
210 a /= b = not (eqUnique a b)
211
212 instance Ord Unique where
213 a < b = ltUnique a b
214 a <= b = leUnique a b
215 a > b = not (leUnique a b)
216 a >= b = not (ltUnique a b)
217 compare a b = cmpUnique a b
218
219 -----------------
220 instance Uniquable Unique where
221 getUnique u = u
222
223 -- We do sometimes make strings with @Uniques@ in them:
224
225 showUnique :: Unique -> String
226 showUnique uniq
227 = case unpkUnique uniq of
228 (tag, u) -> finish_show tag u (iToBase62 u)
229
230 finish_show :: Char -> Int -> String -> String
231 finish_show 't' u _pp_u | u < 26
232 = -- Special case to make v common tyvars, t1, t2, ...
233 -- come out as a, b, ... (shorter, easier to read)
234 [chr (ord 'a' + u)]
235 finish_show tag _ pp_u = tag : pp_u
236
237 pprUnique :: Unique -> SDoc
238 pprUnique u = text (showUnique u)
239
240 instance Outputable Unique where
241 ppr = pprUnique
242
243 instance Show Unique where
244 show uniq = showUnique uniq
245
246 {-
247 ************************************************************************
248 * *
249 \subsection[Utils-base62]{Base-62 numbers}
250 * *
251 ************************************************************************
252
253 A character-stingy way to read/write numbers (notably Uniques).
254 The ``62-its'' are \tr{[0-9a-zA-Z]}. We don't handle negative Ints.
255 Code stolen from Lennart.
256 -}
257
258 iToBase62 :: Int -> String
259 iToBase62 n_
260 = ASSERT(n_ >= 0) go n_ ""
261 where
262 go n cs | n < 62
263 = let !c = chooseChar62 n in c : cs
264 | otherwise
265 = go q (c : cs) where (q, r) = quotRem n 62
266 !c = chooseChar62 r
267
268 chooseChar62 :: Int -> Char
269 {-# INLINE chooseChar62 #-}
270 chooseChar62 (I# n) = C# (indexCharOffAddr# chars62 n)
271 chars62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
272
273 {-
274 ************************************************************************
275 * *
276 \subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
277 * *
278 ************************************************************************
279
280 Allocation of unique supply characters:
281 v,t,u : for renumbering value-, type- and usage- vars.
282 B: builtin
283 C-E: pseudo uniques (used in native-code generator)
284 X: uniques derived by deriveUnique
285 _: unifiable tyvars (above)
286 0-9: prelude things below
287 (no numbers left any more..)
288 :: (prelude) parallel array data constructors
289
290 other a-z: lower case chars for unique supplies. Used so far:
291
292 d desugarer
293 f AbsC flattener
294 g SimplStg
295 n Native codegen
296 r Hsc name cache
297 s simplifier
298 -}
299
300 mkAlphaTyVarUnique :: Int -> Unique
301 mkPreludeClassUnique :: Int -> Unique
302 mkPreludeTyConUnique :: Int -> Unique
303 mkTupleTyConUnique :: Boxity -> Arity -> Unique
304 mkCTupleTyConUnique :: Arity -> Unique
305 mkPreludeDataConUnique :: Arity -> Unique
306 mkTupleDataConUnique :: Boxity -> Arity -> Unique
307 mkPrimOpIdUnique :: Int -> Unique
308 mkPreludeMiscIdUnique :: Int -> Unique
309 mkPArrDataConUnique :: Int -> Unique
310
311 mkAlphaTyVarUnique i = mkUnique '1' i
312 mkPreludeClassUnique i = mkUnique '2' i
313
314 --------------------------------------------------
315 -- Wired-in data constructor keys occupy *three* slots:
316 -- * u: the DataCon itself
317 -- * u+1: its worker Id
318 -- * u+2: the TyConRepName of the promoted TyCon
319 -- Prelude data constructors are too simple to need wrappers.
320 mkPreludeTyConUnique i = mkUnique '3' (3*i)
321 mkTupleTyConUnique Boxed a = mkUnique '4' (3*a)
322 mkTupleTyConUnique Unboxed a = mkUnique '5' (3*a)
323 mkCTupleTyConUnique a = mkUnique 'k' (3*a)
324
325 tyConRepNameUnique :: Unique -> Unique
326 tyConRepNameUnique u = incrUnique u
327
328 -- Data constructor keys occupy *two* slots. The first is used for the
329 -- data constructor itself and its wrapper function (the function that
330 -- evaluates arguments as necessary and calls the worker). The second is
331 -- used for the worker function (the function that builds the constructor
332 -- representation).
333
334 --------------------------------------------------
335 -- Wired-in data constructor keys occupy *three* slots:
336 -- * u: the DataCon itself
337 -- * u+1: its worker Id
338 -- * u+2: the TyConRepName of the promoted TyCon
339 -- Prelude data constructors are too simple to need wrappers.
340
341 mkPreludeDataConUnique i = mkUnique '6' (3*i) -- Must be alphabetic
342 mkTupleDataConUnique Boxed a = mkUnique '7' (3*a) -- ditto (*may* be used in C labels)
343 mkTupleDataConUnique Unboxed a = mkUnique '8' (3*a)
344
345 dataConRepNameUnique, dataConWorkerUnique :: Unique -> Unique
346 dataConWorkerUnique u = incrUnique u
347 dataConRepNameUnique u = stepUnique u 2
348
349 --------------------------------------------------
350 mkPrimOpIdUnique op = mkUnique '9' op
351 mkPreludeMiscIdUnique i = mkUnique '0' i
352
353 -- No numbers left anymore, so I pick something different for the character tag
354 mkPArrDataConUnique a = mkUnique ':' (2*a)
355
356 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
357 -- See pprUnique for details
358
359 initTyVarUnique :: Unique
360 initTyVarUnique = mkUnique 't' 0
361
362 mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
363 mkBuiltinUnique :: Int -> Unique
364
365 mkBuiltinUnique i = mkUnique 'B' i
366 mkPseudoUniqueD i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
367 mkPseudoUniqueE i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
368 mkPseudoUniqueH i = mkUnique 'H' i -- used in NCG spiller to create spill VirtualRegs
369
370 mkRegSingleUnique, mkRegPairUnique, mkRegSubUnique, mkRegClassUnique :: Int -> Unique
371 mkRegSingleUnique = mkUnique 'R'
372 mkRegSubUnique = mkUnique 'S'
373 mkRegPairUnique = mkUnique 'P'
374 mkRegClassUnique = mkUnique 'L'
375
376 mkCostCentreUnique :: Int -> Unique
377 mkCostCentreUnique = mkUnique 'C'
378
379 mkVarOccUnique, mkDataOccUnique, mkTvOccUnique, mkTcOccUnique :: FastString -> Unique
380 -- See Note [The Unique of an OccName] in OccName
381 mkVarOccUnique fs = mkUnique 'i' (uniqueOfFS fs)
382 mkDataOccUnique fs = mkUnique 'd' (uniqueOfFS fs)
383 mkTvOccUnique fs = mkUnique 'v' (uniqueOfFS fs)
384 mkTcOccUnique fs = mkUnique 'c' (uniqueOfFS fs)