Unique.lhs 9.31 KB
Newer Older
1
%
Simon Marlow's avatar
Simon Marlow committed
2
% (c) The University of Glasgow 2006
3
4
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
%
5
6
7
8
9
10
11
12
13
14
15
16
17
18

@Uniques@ are used to distinguish entities in the compiler (@Ids@,
@Classes@, etc.) from each other.  Thus, @Uniques@ are the basic
comparison key in the compiler.

If there is any single operation that needs to be fast, it is @Unique@
comparison.  Unsurprisingly, there is quite a bit of huff-and-puff
directed to that end.

Some of the other hair in this code is to be able to use a
``splittable @UniqueSupply@'' if requested/possible (not standard
Haskell).

\begin{code}
19
{-# OPTIONS -w #-}
20
21
22
-- The above warning supression flag is a temporary kludge.
-- While working on this module you are encouraged to remove it and fix
-- any warnings in the module. See
Ian Lynagh's avatar
Ian Lynagh committed
23
--     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
24
25
-- for details

26
module Unique (
27
	Unique, Uniquable(..), hasKey,
28

29
	pprUnique, 
30
31
32

	mkUnique,			-- Used in UniqSupply
	mkUniqueGrimily,		-- Used in UniqSupply only!
33
	getKey, getKey#,		-- Used in Var, UniqFM, Name only!
34

35
	incrUnique,			-- Used for renumbering
36
	deriveUnique,			-- Ditto
37
	newTagUnique,			-- Used in CgCase
38
	initTyVarUnique,
39

40
	isTupleKey, 
41

42
43
	-- now all the built-in Uniques (and functions to make them)
	-- [the Oh-So-Wonderful Haskell module system wins again...]
44
	mkAlphaTyVarUnique,
45
	mkPrimOpIdUnique,
46
47
48
	mkTupleTyConUnique, mkTupleDataConUnique,
	mkPreludeMiscIdUnique, mkPreludeDataConUnique,
	mkPreludeTyConUnique, mkPreludeClassUnique,
chak's avatar
chak committed
49
	mkPArrDataConUnique,
50

51
	mkBuiltinUnique,
52
53
54
55
	mkPseudoUniqueC,
	mkPseudoUniqueD,
	mkPseudoUniqueE,
	mkPseudoUniqueH
56
57
    ) where

58
59
#include "HsVersions.h"

Simon Marlow's avatar
Simon Marlow committed
60
61
import BasicTypes
import FastString
62
import Outputable
63

Simon Marlow's avatar
Simon Marlow committed
64
65
import GHC.Exts
import Data.Char	( chr, ord )
66
67
68
69
70
71
72
73
74
75
76
77
78
\end{code}

%************************************************************************
%*									*
\subsection[Unique-type]{@Unique@ type and operations}
%*									*
%************************************************************************

The @Chars@ are ``tag letters'' that identify the @UniqueSupply@.
Fast comparison is everything on @Uniques@:

\begin{code}
data Unique = MkUnique Int#
79
80
\end{code}

81
82
83
84
Now come the functions which construct uniques from their pieces, and vice versa.
The stuff about unique *supplies* is handled further down this module.

\begin{code}
85
86
mkUnique	:: Char -> Int -> Unique	-- Builds a unique from pieces
unpkUnique	:: Unique -> (Char, Int)	-- The reverse
87

88
89
90
mkUniqueGrimily :: Int -> Unique		-- A trap-door for UniqSupply
getKey		:: Unique -> Int		-- for Var
getKey#		:: Unique -> Int#		-- for Var
91

92
incrUnique	:: Unique -> Unique
93
deriveUnique	:: Unique -> Int -> Unique
94
newTagUnique	:: Unique -> Char -> Unique
95
96

isTupleKey	:: Unique -> Bool
97
98
99
100
\end{code}


\begin{code}
101
mkUniqueGrimily (I# x) = MkUnique x
102

103
{-# INLINE getKey #-}
104
105
106
getKey (MkUnique x) = I# x
{-# INLINE getKey# #-}
getKey# (MkUnique x) = x
107

108
109
110
111
112
incrUnique (MkUnique i) = MkUnique (i +# 1#)

-- deriveUnique uses an 'X' tag so that it won't clash with
-- any of the uniques produced any other way
deriveUnique (MkUnique i) delta = mkUnique 'X' (I# i + delta)
113

114
115
116
-- newTagUnique changes the "domain" of a unique to a different char
newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u

117
118
119
120
121
122
123
124
-- pop the Char in the top 8 bits of the Unique(Supply)

-- No 64-bit bugs here, as long as we have at least 32 bits. --JSM

w2i x = word2Int# x
i2w x = int2Word# x
i2w_s x = (x::Int#)

125
mkUnique (C# c) (I# i)
126
127
  = MkUnique (w2i (tag `or#` bits))
  where
128
    tag  = i2w (ord# c) `uncheckedShiftL#` i2w_s 24#
129
    bits = i2w i `and#` (i2w 16777215#){-``0x00ffffff''-}
130
131
132

unpkUnique (MkUnique u)
  = let
133
	tag = C# (chr# (w2i ((i2w u) `uncheckedShiftRL#` (i2w_s 24#))))
134
	i   = I# (w2i ((i2w u) `and#` (i2w 16777215#){-``0x00ffffff''-}))
135
136
137
138
    in
    (tag, i)
\end{code}

139
140
141
142
143
144
145
146
147
148


%************************************************************************
%*									*
\subsection[Uniquable-class]{The @Uniquable@ class}
%*									*
%************************************************************************

\begin{code}
class Uniquable a where
149
    getUnique :: a -> Unique
150

151
152
153
hasKey		:: Uniquable a => a -> Unique -> Bool
x `hasKey` k	= getUnique x == k

154
instance Uniquable FastString where
155
 getUnique fs = mkUniqueGrimily (I# (uniqueOfFS fs))
156
157

instance Uniquable Int where
158
 getUnique i = mkUniqueGrimily i
159
160
161
\end{code}


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
%************************************************************************
%*									*
\subsection[Unique-instances]{Instance declarations for @Unique@}
%*									*
%************************************************************************

And the whole point (besides uniqueness) is fast equality.  We don't
use `deriving' because we want {\em precise} control of ordering
(equality on @Uniques@ is v common).

\begin{code}
eqUnique (MkUnique u1) (MkUnique u2) = u1 ==# u2
ltUnique (MkUnique u1) (MkUnique u2) = u1 <#  u2
leUnique (MkUnique u1) (MkUnique u2) = u1 <=# u2

cmpUnique (MkUnique u1) (MkUnique u2)
178
  = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
179
180
181
182
183
184
185
186
187
188

instance Eq Unique where
    a == b = eqUnique a b
    a /= b = not (eqUnique a b)

instance Ord Unique where
    a  < b = ltUnique a b
    a <= b = leUnique a b
    a  > b = not (leUnique a b)
    a >= b = not (ltUnique a b)
189
    compare a b = cmpUnique a b
190

191
192
-----------------
instance Uniquable Unique where
193
    getUnique u = u
194
195
196
197
\end{code}

We do sometimes make strings with @Uniques@ in them:
\begin{code}
198
pprUnique :: Unique -> SDoc
199
200
pprUnique uniq
  = case unpkUnique uniq of
201
      (tag, u) -> finish_ppr tag u (text (iToBase62 u))
202

203
204
#ifdef UNUSED
pprUnique10 :: Unique -> SDoc
205
206
pprUnique10 uniq	-- in base-10, dudes
  = case unpkUnique uniq of
sof's avatar
sof committed
207
      (tag, u) -> finish_ppr tag u (int u)
208
#endif
209

sof's avatar
sof committed
210
211
212
213
214
finish_ppr 't' u pp_u | u < 26
  =	-- Special case to make v common tyvars, t1, t2, ...
	-- come out as a, b, ... (shorter, easier to read)
    char (chr (ord 'a' + u))
finish_ppr tag u pp_u = char tag <> pp_u
215

216
instance Outputable Unique where
217
    ppr u = pprUnique u
218

219
220
instance Show Unique where
    showsPrec p uniq = showsPrecSDoc p (pprUnique uniq)
221
222
223
224
225
226
227
228
229
230
231
\end{code}

%************************************************************************
%*									*
\subsection[Utils-base62]{Base-62 numbers}
%*									*
%************************************************************************

A character-stingy way to read/write numbers (notably Uniques).
The ``62-its'' are \tr{[0-9a-zA-Z]}.  We don't handle negative Ints.
Code stolen from Lennart.
232

233
\begin{code}
234
235
236
iToBase62 :: Int -> String
iToBase62 n@(I# n#) 
  = ASSERT(n >= 0) go n# ""
237
  where
238
239
240
241
242
243
244
245
    go n# cs | n# <# 62# 
	     = case (indexCharOffAddr# chars62# n#) of { c# -> C# c# : cs }
	     | otherwise
	     =	case (quotRem (I# n#) 62)	     of { (I# q#, I# r#) ->
		case (indexCharOffAddr# chars62# r#) of { c#  ->
		go q# (C# c# : cs) }}

    chars62# = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"#
246
247
248
249
250
251
252
253
\end{code}

%************************************************************************
%*									*
\subsection[Uniques-prelude]{@Uniques@ for wired-in Prelude things}
%*									*
%************************************************************************

254
Allocation of unique supply characters:
255
256
257
	v,t,u : for renumbering value-, type- and usage- vars.
	B:   builtin
	C-E: pseudo uniques	(used in native-code generator)
258
	X:   uniques derived by deriveUnique
259
	_:   unifiable tyvars   (above)
260
	0-9: prelude things below
261

262
263
264
265
266
267
268
269
270
271
	other a-z: lower case chars for unique supplies.  Used so far:

	d	desugarer
	f	AbsC flattener
	g	SimplStg
	l	ndpFlatten
	n	Native codegen
	r	Hsc name cache
	s	simplifier

272
\begin{code}
273
mkAlphaTyVarUnique i            = mkUnique '1' i
274
275

mkPreludeClassUnique i		= mkUnique '2' i
276
277
278
279
280

-- Prelude type constructors occupy *three* slots.
-- The first is for the tycon itself; the latter two
-- are for the generic to/from Ids.  See TysWiredIn.mk_tc_gen_info.

281
mkPreludeTyConUnique i		= mkUnique '3' (3*i)
282
283
mkTupleTyConUnique Boxed   a	= mkUnique '4' (3*a)
mkTupleTyConUnique Unboxed a	= mkUnique '5' (3*a)
284

285
286
287
288
289
290
291
-- Data constructor keys occupy *two* slots.  The first is used for the
-- data constructor itself and its wrapper function (the function that
-- evaluates arguments as necessary and calls the worker). The second is
-- used for the worker function (the function that builds the constructor
-- representation).

mkPreludeDataConUnique i	= mkUnique '6' (2*i)	-- Must be alphabetic
292
293
mkTupleDataConUnique Boxed a	= mkUnique '7' (2*a)	-- ditto (*may* be used in C labels)
mkTupleDataConUnique Unboxed a	= mkUnique '8' (2*a)
294
295
296
297
298

-- This one is used for a tiresome reason
-- to improve a consistency-checking error check in the renamer
isTupleKey u = case unpkUnique u of
		(tag,_) -> tag == '4' || tag == '5' || tag == '7' || tag == '8'
299

300
301
mkPrimOpIdUnique op		= mkUnique '9' op
mkPreludeMiscIdUnique i		= mkUnique '0' i
302

chak's avatar
chak committed
303
304
305
306
-- No numbers left anymore, so I pick something different for the character
-- tag 
mkPArrDataConUnique a	        = mkUnique ':' (2*a)

sof's avatar
sof committed
307
308
309
310
311
312
-- The "tyvar uniques" print specially nicely: a, b, c, etc.
-- See pprUnique for details

initTyVarUnique :: Unique
initTyVarUnique = mkUnique 't' 0

313
mkPseudoUniqueC, mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
314
   mkBuiltinUnique :: Int -> Unique
315
316

mkBuiltinUnique i = mkUnique 'B' i
317
318
319
320
mkPseudoUniqueC i = mkUnique 'C' i -- used for getUnique on Regs
mkPseudoUniqueD i = mkUnique 'D' i -- used in NCG for getUnique on RealRegs
mkPseudoUniqueE i = mkUnique 'E' i -- used in NCG spiller to create spill VirtualRegs
mkPseudoUniqueH i = mkUnique 'H' i -- used in NCG spiller to create spill VirtualRegs
321
322
\end{code}