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