Add selectors for common fields (DataCon/PatSyn) to ConLike
[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, getKeyFastInt, -- 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 mkBuiltinUnique,
56 mkPseudoUniqueD,
57 mkPseudoUniqueE,
58 mkPseudoUniqueH
59 ) where
60
61 #include "HsVersions.h"
62
63 import BasicTypes
64 import FastTypes
65 import FastString
66 import Outputable
67 -- import StaticFlags
68 import Util
69
70 --just for implementing a fast [0,61) -> Char function
71 import GHC.Exts (indexCharOffAddr#, Char(..))
72
73 import Data.Char ( chr, ord )
74
75 {-
76 ************************************************************************
77 * *
78 \subsection[Unique-type]{@Unique@ type and operations}
79 * *
80 ************************************************************************
81
82 The @Chars@ are ``tag letters'' that identify the @UniqueSupply@.
83 Fast comparison is everything on @Uniques@:
84 -}
85
86 --why not newtype Int?
87
88 -- | The type of unique identifiers that are used in many places in GHC
89 -- for fast ordering and equality tests. You should generate these with
90 -- the functions from the 'UniqSupply' module
91 data Unique = MkUnique FastInt
92
93 {-
94 Now come the functions which construct uniques from their pieces, and vice versa.
95 The stuff about unique *supplies* is handled further down this module.
96 -}
97
98 unpkUnique :: Unique -> (Char, Int) -- The reverse
99
100 mkUniqueGrimily :: Int -> Unique -- A trap-door for UniqSupply
101 getKey :: Unique -> Int -- for Var
102 getKeyFastInt :: Unique -> FastInt -- for Var
103
104 incrUnique :: Unique -> Unique
105 deriveUnique :: Unique -> Int -> Unique
106 newTagUnique :: Unique -> Char -> Unique
107
108 mkUniqueGrimily x = MkUnique (iUnbox x)
109
110 {-# INLINE getKey #-}
111 getKey (MkUnique x) = iBox x
112 {-# INLINE getKeyFastInt #-}
113 getKeyFastInt (MkUnique x) = x
114
115 incrUnique (MkUnique i) = MkUnique (i +# _ILIT(1))
116
117 -- deriveUnique uses an 'X' tag so that it won't clash with
118 -- any of the uniques produced any other way
119 deriveUnique (MkUnique i) delta = mkUnique 'X' (iBox i + delta)
120
121 -- newTagUnique changes the "domain" of a unique to a different char
122 newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u
123
124 -- pop the Char in the top 8 bits of the Unique(Supply)
125
126 -- No 64-bit bugs here, as long as we have at least 32 bits. --JSM
127
128 -- and as long as the Char fits in 8 bits, which we assume anyway!
129
130 mkUnique :: Char -> Int -> Unique -- Builds a unique from pieces
131 -- NOT EXPORTED, so that we can see all the Chars that
132 -- are used in this one module
133 mkUnique c i
134 = MkUnique (tag `bitOrFastInt` bits)
135 where
136 !tag = fastOrd (cUnbox c) `shiftLFastInt` _ILIT(24)
137 !bits = iUnbox i `bitAndFastInt` _ILIT(16777215){-``0x00ffffff''-}
138
139 unpkUnique (MkUnique u)
140 = let
141 -- as long as the Char may have its eighth bit set, we
142 -- really do need the logical right-shift here!
143 tag = cBox (fastChr (u `shiftRLFastInt` _ILIT(24)))
144 i = iBox (u `bitAndFastInt` _ILIT(16777215){-``0x00ffffff''-})
145 in
146 (tag, i)
147
148 {-
149 ************************************************************************
150 * *
151 \subsection[Uniquable-class]{The @Uniquable@ class}
152 * *
153 ************************************************************************
154 -}
155
156 -- | Class of things that we can obtain a 'Unique' from
157 class Uniquable a where
158 getUnique :: a -> Unique
159
160 hasKey :: Uniquable a => a -> Unique -> Bool
161 x `hasKey` k = getUnique x == k
162
163 instance Uniquable FastString where
164 getUnique fs = mkUniqueGrimily (iBox (uniqueOfFS fs))
165
166 instance Uniquable Int where
167 getUnique i = mkUniqueGrimily i
168
169 {-
170 ************************************************************************
171 * *
172 \subsection[Unique-instances]{Instance declarations for @Unique@}
173 * *
174 ************************************************************************
175
176 And the whole point (besides uniqueness) is fast equality. We don't
177 use `deriving' because we want {\em precise} control of ordering
178 (equality on @Uniques@ is v common).
179 -}
180
181 eqUnique, ltUnique, leUnique :: Unique -> Unique -> Bool
182 eqUnique (MkUnique u1) (MkUnique u2) = u1 ==# u2
183 ltUnique (MkUnique u1) (MkUnique u2) = u1 <# u2
184 leUnique (MkUnique u1) (MkUnique u2) = u1 <=# u2
185
186 cmpUnique :: Unique -> Unique -> Ordering
187 cmpUnique (MkUnique u1) (MkUnique u2)
188 = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
189
190 instance Eq Unique where
191 a == b = eqUnique a b
192 a /= b = not (eqUnique a b)
193
194 instance Ord Unique where
195 a < b = ltUnique a b
196 a <= b = leUnique a b
197 a > b = not (leUnique a b)
198 a >= b = not (ltUnique a b)
199 compare a b = cmpUnique a b
200
201 -----------------
202 instance Uniquable Unique where
203 getUnique u = u
204
205 -- We do sometimes make strings with @Uniques@ in them:
206
207 showUnique :: Unique -> String
208 showUnique uniq
209 = case unpkUnique uniq of
210 (tag, u) -> finish_show tag u (iToBase62 u)
211
212 finish_show :: Char -> Int -> String -> String
213 finish_show 't' u _pp_u | u < 26
214 = -- Special case to make v common tyvars, t1, t2, ...
215 -- come out as a, b, ... (shorter, easier to read)
216 [chr (ord 'a' + u)]
217 finish_show tag _ pp_u = tag : pp_u
218
219 pprUnique :: Unique -> SDoc
220 pprUnique u = text (showUnique u)
221
222 instance Outputable Unique where
223 ppr = pprUnique
224
225 instance Show Unique where
226 show uniq = showUnique uniq
227
228 {-
229 ************************************************************************
230 * *
231 \subsection[Utils-base62]{Base-62 numbers}
232 * *
233 ************************************************************************
234
235 A character-stingy way to read/write numbers (notably Uniques).
236 The ``62-its'' are \tr{[0-9a-zA-Z]}. We don't handle negative Ints.
237 Code stolen from Lennart.
238 -}
239
240 iToBase62 :: Int -> String
241 iToBase62 n_
242 = ASSERT(n_ >= 0) go (iUnbox n_) ""
243 where
244 go n cs | n <# _ILIT(62)
245 = case chooseChar62 n of { c -> c `seq` (c : cs) }
246 | otherwise
247 = case (quotRem (iBox n) 62) of { (q_, r_) ->
248 case iUnbox q_ of { q -> case iUnbox r_ of { r ->
249 case (chooseChar62 r) of { c -> c `seq`
250 (go q (c : cs)) }}}}
251
252 chooseChar62 :: FastInt -> Char
253 {-# INLINE chooseChar62 #-}
254 chooseChar62 n = C# (indexCharOffAddr# chars62 n)
255 !chars62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
256
257 {-
258 ************************************************************************
259 * *
260 \subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
261 * *
262 ************************************************************************
263
264 Allocation of unique supply characters:
265 v,t,u : for renumbering value-, type- and usage- vars.
266 B: builtin
267 C-E: pseudo uniques (used in native-code generator)
268 X: uniques derived by deriveUnique
269 _: unifiable tyvars (above)
270 0-9: prelude things below
271 (no numbers left any more..)
272 :: (prelude) parallel array data constructors
273
274 other a-z: lower case chars for unique supplies. Used so far:
275
276 d desugarer
277 f AbsC flattener
278 g SimplStg
279 n Native codegen
280 r Hsc name cache
281 s simplifier
282 -}
283
284 mkAlphaTyVarUnique :: Int -> Unique
285 mkPreludeClassUnique :: Int -> Unique
286 mkPreludeTyConUnique :: Int -> Unique
287 mkTupleTyConUnique :: Boxity -> Arity -> Unique
288 mkCTupleTyConUnique :: Arity -> Unique
289 mkPreludeDataConUnique :: Arity -> Unique
290 mkTupleDataConUnique :: Boxity -> Arity -> Unique
291 mkPrimOpIdUnique :: Int -> Unique
292 mkPreludeMiscIdUnique :: Int -> Unique
293 mkPArrDataConUnique :: Int -> Unique
294
295 mkAlphaTyVarUnique i = mkUnique '1' i
296 mkPreludeClassUnique i = mkUnique '2' i
297
298 -- Prelude type constructors occupy *three* slots.
299 -- The first is for the tycon itself; the latter two
300 -- are for the generic to/from Ids. See TysWiredIn.mk_tc_gen_info.
301
302 mkPreludeTyConUnique i = mkUnique '3' (3*i)
303 mkTupleTyConUnique Boxed a = mkUnique '4' (3*a)
304 mkTupleTyConUnique Unboxed a = mkUnique '5' (3*a)
305 mkCTupleTyConUnique a = mkUnique 'k' (3*a)
306
307 -- Data constructor keys occupy *two* slots. The first is used for the
308 -- data constructor itself and its wrapper function (the function that
309 -- evaluates arguments as necessary and calls the worker). The second is
310 -- used for the worker function (the function that builds the constructor
311 -- representation).
312
313 mkPreludeDataConUnique i = mkUnique '6' (2*i) -- Must be alphabetic
314 mkTupleDataConUnique Boxed a = mkUnique '7' (2*a) -- ditto (*may* be used in C labels)
315 mkTupleDataConUnique Unboxed a = mkUnique '8' (2*a)
316
317 mkPrimOpIdUnique op = mkUnique '9' op
318 mkPreludeMiscIdUnique i = mkUnique '0' i
319
320 -- No numbers left anymore, so I pick something different for the character tag
321 mkPArrDataConUnique a = mkUnique ':' (2*a)
322
323 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
324 -- See pprUnique for details
325
326 initTyVarUnique :: Unique
327 initTyVarUnique = mkUnique 't' 0
328
329 mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
330 mkBuiltinUnique :: Int -> Unique
331
332 mkBuiltinUnique i = mkUnique 'B' i
333 mkPseudoUniqueD i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
334 mkPseudoUniqueE i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
335 mkPseudoUniqueH i = mkUnique 'H' i -- used in NCG spiller to create spill VirtualRegs
336
337 mkRegSingleUnique, mkRegPairUnique, mkRegSubUnique, mkRegClassUnique :: Int -> Unique
338 mkRegSingleUnique = mkUnique 'R'
339 mkRegSubUnique = mkUnique 'S'
340 mkRegPairUnique = mkUnique 'P'
341 mkRegClassUnique = mkUnique 'L'
342
343 mkCostCentreUnique :: Int -> Unique
344 mkCostCentreUnique = mkUnique 'C'
345
346 mkVarOccUnique, mkDataOccUnique, mkTvOccUnique, mkTcOccUnique :: FastString -> Unique
347 -- See Note [The Unique of an OccName] in OccName
348 mkVarOccUnique fs = mkUnique 'i' (iBox (uniqueOfFS fs))
349 mkDataOccUnique fs = mkUnique 'd' (iBox (uniqueOfFS fs))
350 mkTvOccUnique fs = mkUnique 'v' (iBox (uniqueOfFS fs))
351 mkTcOccUnique fs = mkUnique 'c' (iBox (uniqueOfFS fs))