Make -dsuppress-uniques apply regardless of -ppr-debug
[ghc-hetmet.git] / compiler / basicTypes / Unique.lhs
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 \begin{code}
19 module Unique (
20         -- * Main data types
21         Unique, Uniquable(..), 
22         
23         -- ** Constructors, desctructors and operations on 'Unique's
24         hasKey,
25
26         pprUnique, 
27
28         mkUnique,                       -- Used in UniqSupply
29         mkUniqueGrimily,                -- Used in UniqSupply only!
30         getKey, getKeyFastInt,          -- Used in Var, UniqFM, Name only!
31
32         incrUnique,                     -- Used for renumbering
33         deriveUnique,                   -- Ditto
34         newTagUnique,                   -- Used in CgCase
35         initTyVarUnique,
36
37         isTupleKey, 
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         mkPreludeMiscIdUnique, mkPreludeDataConUnique,
47         mkPreludeTyConUnique, mkPreludeClassUnique,
48         mkPArrDataConUnique,
49
50         mkBuiltinUnique,
51         mkPseudoUniqueC,
52         mkPseudoUniqueD,
53         mkPseudoUniqueE,
54         mkPseudoUniqueH
55     ) where
56
57 #include "HsVersions.h"
58
59 import BasicTypes
60 import FastTypes
61 import FastString
62 import Outputable
63 import StaticFlags
64 import Util
65
66 #if defined(__GLASGOW_HASKELL__)
67 --just for implementing a fast [0,61) -> Char function
68 import GHC.Exts (indexCharOffAddr#, Char(..))
69 #else
70 import Data.Array
71 #endif
72 import Data.Char        ( chr, ord )
73 \end{code}
74
75 %************************************************************************
76 %*                                                                      *
77 \subsection[Unique-type]{@Unique@ type and operations}
78 %*                                                                      *
79 %************************************************************************
80
81 The @Chars@ are ``tag letters'' that identify the @UniqueSupply@.
82 Fast comparison is everything on @Uniques@:
83
84 \begin{code}
85 --why not newtype Int?
86
87 -- | The type of unique identifiers that are used in many places in GHC
88 -- for fast ordering and equality tests. You should generate these with
89 -- the functions from the 'UniqSupply' module
90 data Unique = MkUnique FastInt
91 \end{code}
92
93 Now come the functions which construct uniques from their pieces, and vice versa.
94 The stuff about unique *supplies* is handled further down this module.
95
96 \begin{code}
97 mkUnique        :: Char -> Int -> Unique        -- Builds a unique from pieces
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 isTupleKey      :: Unique -> Bool
109 \end{code}
110
111
112 \begin{code}
113 mkUniqueGrimily x = MkUnique (iUnbox x)
114
115 {-# INLINE getKey #-}
116 getKey (MkUnique x) = iBox x
117 {-# INLINE getKeyFastInt #-}
118 getKeyFastInt (MkUnique x) = x
119
120 incrUnique (MkUnique i) = MkUnique (i +# _ILIT(1))
121
122 -- deriveUnique uses an 'X' tag so that it won't clash with
123 -- any of the uniques produced any other way
124 deriveUnique (MkUnique i) delta = mkUnique 'X' (iBox i + delta)
125
126 -- newTagUnique changes the "domain" of a unique to a different char
127 newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u
128
129 -- pop the Char in the top 8 bits of the Unique(Supply)
130
131 -- No 64-bit bugs here, as long as we have at least 32 bits. --JSM
132
133 -- and as long as the Char fits in 8 bits, which we assume anyway!
134
135 mkUnique c i
136   = MkUnique (tag `bitOrFastInt` bits)
137   where
138     tag  = fastOrd (cUnbox c) `shiftLFastInt` _ILIT(24)
139     bits = iUnbox i `bitAndFastInt` _ILIT(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 = cBox (fastChr (u `shiftRLFastInt` _ILIT(24)))
146         i   = iBox (u `bitAndFastInt` _ILIT(16777215){-``0x00ffffff''-})
147     in
148     (tag, i)
149 \end{code}
150
151
152
153 %************************************************************************
154 %*                                                                      *
155 \subsection[Uniquable-class]{The @Uniquable@ class}
156 %*                                                                      *
157 %************************************************************************
158
159 \begin{code}
160 -- | Class of things that we can obtain a 'Unique' from
161 class Uniquable a where
162     getUnique :: a -> Unique
163
164 hasKey          :: Uniquable a => a -> Unique -> Bool
165 x `hasKey` k    = getUnique x == k
166
167 instance Uniquable FastString where
168  getUnique fs = mkUniqueGrimily (iBox (uniqueOfFS fs))
169
170 instance Uniquable Int where
171  getUnique i = mkUniqueGrimily i
172 \end{code}
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 \begin{code}
186 eqUnique, ltUnique, leUnique :: Unique -> Unique -> Bool
187 eqUnique (MkUnique u1) (MkUnique u2) = u1 ==# u2
188 ltUnique (MkUnique u1) (MkUnique u2) = u1 <#  u2
189 leUnique (MkUnique u1) (MkUnique u2) = u1 <=# u2
190
191 cmpUnique :: Unique -> Unique -> Ordering
192 cmpUnique (MkUnique u1) (MkUnique u2)
193   = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
194
195 instance Eq Unique where
196     a == b = eqUnique a b
197     a /= b = not (eqUnique a b)
198
199 instance Ord Unique where
200     a  < b = ltUnique a b
201     a <= b = leUnique a b
202     a  > b = not (leUnique a b)
203     a >= b = not (ltUnique a b)
204     compare a b = cmpUnique a b
205
206 -----------------
207 instance Uniquable Unique where
208     getUnique u = u
209 \end{code}
210
211 We do sometimes make strings with @Uniques@ in them:
212 \begin{code}
213 pprUnique :: Unique -> SDoc
214 pprUnique uniq
215   | debugIsOn || opt_SuppressUniques
216   = empty       -- Used exclusively to suppress uniques so you 
217   | otherwise   -- can compare output easily
218   = case unpkUnique uniq of
219       (tag, u) -> finish_ppr tag u (text (iToBase62 u))
220
221 #ifdef UNUSED
222 pprUnique10 :: Unique -> SDoc
223 pprUnique10 uniq        -- in base-10, dudes
224   = case unpkUnique uniq of
225       (tag, u) -> finish_ppr tag u (int u)
226 #endif
227
228 finish_ppr :: Char -> Int -> SDoc -> SDoc
229 finish_ppr 't' u _pp_u | u < 26
230   =     -- Special case to make v common tyvars, t1, t2, ...
231         -- come out as a, b, ... (shorter, easier to read)
232     char (chr (ord 'a' + u))
233 finish_ppr tag _ pp_u = char tag <> pp_u
234
235 instance Outputable Unique where
236     ppr u = pprUnique u
237
238 instance Show Unique where
239     showsPrec p uniq = showsPrecSDoc p (pprUnique uniq)
240 \end{code}
241
242 %************************************************************************
243 %*                                                                      *
244 \subsection[Utils-base62]{Base-62 numbers}
245 %*                                                                      *
246 %************************************************************************
247
248 A character-stingy way to read/write numbers (notably Uniques).
249 The ``62-its'' are \tr{[0-9a-zA-Z]}.  We don't handle negative Ints.
250 Code stolen from Lennart.
251
252 \begin{code}
253 iToBase62 :: Int -> String
254 iToBase62 n_
255   = ASSERT(n_ >= 0) go (iUnbox n_) ""
256   where
257     go n cs | n <# _ILIT(62)
258              = case chooseChar62 n of { c -> c `seq` (c : cs) }
259              | otherwise
260              =  case (quotRem (iBox n) 62) of { (q_, r_) ->
261                 case iUnbox q_ of { q -> case iUnbox r_ of { r ->
262                 case (chooseChar62 r) of { c -> c `seq`
263                 (go q (c : cs)) }}}}
264
265     chooseChar62 :: FastInt -> Char
266     {-# INLINE chooseChar62 #-}
267 #if defined(__GLASGOW_HASKELL__)
268     --then FastInt == Int#
269     chooseChar62 n = C# (indexCharOffAddr# chars62 n)
270     chars62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
271 #else
272     --Haskell98 arrays are portable
273     chooseChar62 n = (!) chars62 n
274     chars62 = listArray (0,61) "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
275 #endif
276 \end{code}
277
278 %************************************************************************
279 %*                                                                      *
280 \subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
281 %*                                                                      *
282 %************************************************************************
283
284 Allocation of unique supply characters:
285         v,t,u : for renumbering value-, type- and usage- vars.
286         B:   builtin
287         C-E: pseudo uniques     (used in native-code generator)
288         X:   uniques derived by deriveUnique
289         _:   unifiable tyvars   (above)
290         0-9: prelude things below
291              (no numbers left any more..)
292         ::   (prelude) parallel array data constructors
293
294         other a-z: lower case chars for unique supplies.  Used so far:
295
296         d       desugarer
297         f       AbsC flattener
298         g       SimplStg
299         n       Native codegen
300         r       Hsc name cache
301         s       simplifier
302
303 \begin{code}
304 mkAlphaTyVarUnique     :: Int -> Unique
305 mkPreludeClassUnique   :: Int -> Unique
306 mkPreludeTyConUnique   :: Int -> Unique
307 mkTupleTyConUnique     :: Boxity -> Int -> Unique
308 mkPreludeDataConUnique :: Int -> Unique
309 mkTupleDataConUnique   :: Boxity -> Int -> Unique
310 mkPrimOpIdUnique       :: Int -> Unique
311 mkPreludeMiscIdUnique  :: Int -> Unique
312 mkPArrDataConUnique    :: Int -> Unique
313
314 mkAlphaTyVarUnique i            = mkUnique '1' i
315
316 mkPreludeClassUnique i          = mkUnique '2' i
317
318 -- Prelude type constructors occupy *three* slots.
319 -- The first is for the tycon itself; the latter two
320 -- are for the generic to/from Ids.  See TysWiredIn.mk_tc_gen_info.
321
322 mkPreludeTyConUnique i          = mkUnique '3' (3*i)
323 mkTupleTyConUnique Boxed   a    = mkUnique '4' (3*a)
324 mkTupleTyConUnique Unboxed a    = mkUnique '5' (3*a)
325
326 -- Data constructor keys occupy *two* slots.  The first is used for the
327 -- data constructor itself and its wrapper function (the function that
328 -- evaluates arguments as necessary and calls the worker). The second is
329 -- used for the worker function (the function that builds the constructor
330 -- representation).
331
332 mkPreludeDataConUnique i        = mkUnique '6' (2*i)    -- Must be alphabetic
333 mkTupleDataConUnique Boxed a    = mkUnique '7' (2*a)    -- ditto (*may* be used in C labels)
334 mkTupleDataConUnique Unboxed a  = mkUnique '8' (2*a)
335
336 -- This one is used for a tiresome reason
337 -- to improve a consistency-checking error check in the renamer
338 isTupleKey u = case unpkUnique u of
339                 (tag,_) -> tag == '4' || tag == '5' || tag == '7' || tag == '8'
340
341 mkPrimOpIdUnique op         = mkUnique '9' op
342 mkPreludeMiscIdUnique  i    = mkUnique '0' i
343
344 -- No numbers left anymore, so I pick something different for the character
345 -- tag 
346 mkPArrDataConUnique a           = mkUnique ':' (2*a)
347
348 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
349 -- See pprUnique for details
350
351 initTyVarUnique :: Unique
352 initTyVarUnique = mkUnique 't' 0
353
354 mkPseudoUniqueC, mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
355    mkBuiltinUnique :: Int -> Unique
356
357 mkBuiltinUnique i = mkUnique 'B' i
358 mkPseudoUniqueC i = mkUnique 'C' i -- used for getUnique on Regs
359 mkPseudoUniqueD i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
360 mkPseudoUniqueE i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
361 mkPseudoUniqueH i = mkUnique 'H' i -- used in NCG spiller to create spill VirtualRegs
362 \end{code}
363