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