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