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