Fixed warnings in basicTypes/Unique
[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
58 #if defined(__GLASGOW_HASKELL__)
59 --just for implementing a fast [0,61) -> Char function
60 import GHC.Exts (indexCharOffAddr#, Char(..))
61 #else
62 import Data.Array
63 #endif
64 import Data.Char        ( chr, ord )
65 \end{code}
66
67 %************************************************************************
68 %*                                                                      *
69 \subsection[Unique-type]{@Unique@ type and operations}
70 %*                                                                      *
71 %************************************************************************
72
73 The @Chars@ are ``tag letters'' that identify the @UniqueSupply@.
74 Fast comparison is everything on @Uniques@:
75
76 \begin{code}
77 --why not newtype Int?
78 data Unique = MkUnique FastInt
79 \end{code}
80
81 Now come the functions which construct uniques from their pieces, and vice versa.
82 The stuff about unique *supplies* is handled further down this module.
83
84 \begin{code}
85 mkUnique        :: Char -> Int -> Unique        -- Builds a unique from pieces
86 unpkUnique      :: Unique -> (Char, Int)        -- The reverse
87
88 mkUniqueGrimily :: Int -> Unique                -- A trap-door for UniqSupply
89 getKey          :: Unique -> Int                -- for Var
90 getKeyFastInt   :: Unique -> FastInt            -- for Var
91
92 incrUnique      :: Unique -> Unique
93 deriveUnique    :: Unique -> Int -> Unique
94 newTagUnique    :: Unique -> Char -> Unique
95
96 isTupleKey      :: Unique -> Bool
97 \end{code}
98
99
100 \begin{code}
101 mkUniqueGrimily x = MkUnique (iUnbox x)
102
103 {-# INLINE getKey #-}
104 getKey (MkUnique x) = iBox x
105 {-# INLINE getKeyFastInt #-}
106 getKeyFastInt (MkUnique x) = x
107
108 incrUnique (MkUnique i) = MkUnique (i +# _ILIT(1))
109
110 -- deriveUnique uses an 'X' tag so that it won't clash with
111 -- any of the uniques produced any other way
112 deriveUnique (MkUnique i) delta = mkUnique 'X' (iBox i + delta)
113
114 -- newTagUnique changes the "domain" of a unique to a different char
115 newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u
116
117 -- pop the Char in the top 8 bits of the Unique(Supply)
118
119 -- No 64-bit bugs here, as long as we have at least 32 bits. --JSM
120
121 -- and as long as the Char fits in 8 bits, which we assume anyway!
122
123 mkUnique c i
124   = MkUnique (tag `bitOrFastInt` bits)
125   where
126     tag  = fastOrd (cUnbox c) `shiftLFastInt` _ILIT(24)
127     bits = iUnbox i `bitAndFastInt` _ILIT(16777215){-``0x00ffffff''-}
128
129 unpkUnique (MkUnique u)
130   = let
131         -- as long as the Char may have its eighth bit set, we
132         -- really do need the logical right-shift here!
133         tag = cBox (fastChr (u `shiftRLFastInt` _ILIT(24)))
134         i   = iBox (u `bitAndFastInt` _ILIT(16777215){-``0x00ffffff''-})
135     in
136     (tag, i)
137 \end{code}
138
139
140
141 %************************************************************************
142 %*                                                                      *
143 \subsection[Uniquable-class]{The @Uniquable@ class}
144 %*                                                                      *
145 %************************************************************************
146
147 \begin{code}
148 class Uniquable a where
149     getUnique :: a -> Unique
150
151 hasKey          :: Uniquable a => a -> Unique -> Bool
152 x `hasKey` k    = getUnique x == k
153
154 instance Uniquable FastString where
155  getUnique fs = mkUniqueGrimily (iBox (uniqueOfFS fs))
156
157 instance Uniquable Int where
158  getUnique i = mkUniqueGrimily i
159 \end{code}
160
161
162 %************************************************************************
163 %*                                                                      *
164 \subsection[Unique-instances]{Instance declarations for @Unique@}
165 %*                                                                      *
166 %************************************************************************
167
168 And the whole point (besides uniqueness) is fast equality.  We don't
169 use `deriving' because we want {\em precise} control of ordering
170 (equality on @Uniques@ is v common).
171
172 \begin{code}
173 eqUnique, ltUnique, leUnique :: Unique -> Unique -> Bool
174 eqUnique (MkUnique u1) (MkUnique u2) = u1 ==# u2
175 ltUnique (MkUnique u1) (MkUnique u2) = u1 <#  u2
176 leUnique (MkUnique u1) (MkUnique u2) = u1 <=# u2
177
178 cmpUnique :: Unique -> Unique -> Ordering
179 cmpUnique (MkUnique u1) (MkUnique u2)
180   = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
181
182 instance Eq Unique where
183     a == b = eqUnique a b
184     a /= b = not (eqUnique a b)
185
186 instance Ord Unique where
187     a  < b = ltUnique a b
188     a <= b = leUnique a b
189     a  > b = not (leUnique a b)
190     a >= b = not (ltUnique a b)
191     compare a b = cmpUnique a b
192
193 -----------------
194 instance Uniquable Unique where
195     getUnique u = u
196 \end{code}
197
198 We do sometimes make strings with @Uniques@ in them:
199 \begin{code}
200 pprUnique :: Unique -> SDoc
201 pprUnique uniq
202 #ifdef DEBUG
203   | opt_SuppressUniques
204   = empty       -- Used exclusively to suppress uniques so you 
205   | otherwise   -- can compare output easily
206 #endif
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         l       ndpFlatten
287         n       Native codegen
288         r       Hsc name cache
289         s       simplifier
290
291 \begin{code}
292 mkAlphaTyVarUnique     :: Int -> Unique
293 mkPreludeClassUnique   :: Int -> Unique
294 mkPreludeTyConUnique   :: Int -> Unique
295 mkTupleTyConUnique     :: Boxity -> Int -> Unique
296 mkPreludeDataConUnique :: Int -> Unique
297 mkTupleDataConUnique   :: Boxity -> Int -> Unique
298 mkPrimOpIdUnique       :: Int -> Unique
299 mkPreludeMiscIdUnique  :: Int -> Unique
300 mkPArrDataConUnique    :: Int -> Unique
301
302 mkAlphaTyVarUnique i            = mkUnique '1' i
303
304 mkPreludeClassUnique i          = mkUnique '2' i
305
306 -- Prelude type constructors occupy *three* slots.
307 -- The first is for the tycon itself; the latter two
308 -- are for the generic to/from Ids.  See TysWiredIn.mk_tc_gen_info.
309
310 mkPreludeTyConUnique i          = mkUnique '3' (3*i)
311 mkTupleTyConUnique Boxed   a    = mkUnique '4' (3*a)
312 mkTupleTyConUnique Unboxed a    = mkUnique '5' (3*a)
313
314 -- Data constructor keys occupy *two* slots.  The first is used for the
315 -- data constructor itself and its wrapper function (the function that
316 -- evaluates arguments as necessary and calls the worker). The second is
317 -- used for the worker function (the function that builds the constructor
318 -- representation).
319
320 mkPreludeDataConUnique i        = mkUnique '6' (2*i)    -- Must be alphabetic
321 mkTupleDataConUnique Boxed a    = mkUnique '7' (2*a)    -- ditto (*may* be used in C labels)
322 mkTupleDataConUnique Unboxed a  = mkUnique '8' (2*a)
323
324 -- This one is used for a tiresome reason
325 -- to improve a consistency-checking error check in the renamer
326 isTupleKey u = case unpkUnique u of
327                 (tag,_) -> tag == '4' || tag == '5' || tag == '7' || tag == '8'
328
329 mkPrimOpIdUnique op             = mkUnique '9' op
330 mkPreludeMiscIdUnique i         = mkUnique '0' i
331
332 -- No numbers left anymore, so I pick something different for the character
333 -- tag 
334 mkPArrDataConUnique a           = mkUnique ':' (2*a)
335
336 -- The "tyvar uniques" print specially nicely: a, b, c, etc.
337 -- See pprUnique for details
338
339 initTyVarUnique :: Unique
340 initTyVarUnique = mkUnique 't' 0
341
342 mkPseudoUniqueC, mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
343    mkBuiltinUnique :: Int -> Unique
344
345 mkBuiltinUnique i = mkUnique 'B' i
346 mkPseudoUniqueC i = mkUnique 'C' i -- used for getUnique on Regs
347 mkPseudoUniqueD i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
348 mkPseudoUniqueE i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
349 mkPseudoUniqueH i = mkUnique 'H' i -- used in NCG spiller to create spill VirtualRegs
350 \end{code}
351