Unique.lhs 9.37 KB
Newer Older
1
2
3
%
% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
%
4
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}
module Unique (
19
	Unique, Uniquable(..), hasKey,
20

21
	pprUnique, 
22
23
24

	mkUnique,			-- Used in UniqSupply
	mkUniqueGrimily,		-- Used in UniqSupply only!
25
	getKey, getKey#,		-- Used in Var, UniqFM, Name only!
26

27
	incrUnique,			-- Used for renumbering
28
	deriveUnique,			-- Ditto
29
	newTagUnique,			-- Used in CgCase
30
	initTyVarUnique,
31

32
	isTupleKey, 
33

34
35
	-- now all the built-in Uniques (and functions to make them)
	-- [the Oh-So-Wonderful Haskell module system wins again...]
36
	mkAlphaTyVarUnique,
37
	mkPrimOpIdUnique,
38
39
40
	mkTupleTyConUnique, mkTupleDataConUnique,
	mkPreludeMiscIdUnique, mkPreludeDataConUnique,
	mkPreludeTyConUnique, mkPreludeClassUnique,
chak's avatar
chak committed
41
	mkPArrDataConUnique,
42

43
	mkBuiltinUnique,
44
45
46
47
	mkPseudoUniqueC,
	mkPseudoUniqueD,
	mkPseudoUniqueE,
	mkPseudoUniqueH
48
49
    ) where

50
51
#include "HsVersions.h"

52
import BasicTypes	( Boxity(..) )
53
import PackageConfig	( PackageId, packageIdFS )
54
import FastString	( FastString, uniqueOfFS )
55
import Outputable
56
import FastTypes
57

58
59
60
import GLAEXTS

import Char		( chr, ord )
61
62
63
64
65
66
67
68
69
70
71
72
73
\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#
74
75
\end{code}

76
77
78
79
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}
80
81
mkUnique	:: Char -> Int -> Unique	-- Builds a unique from pieces
unpkUnique	:: Unique -> (Char, Int)	-- The reverse
82

83
84
85
mkUniqueGrimily :: Int -> Unique		-- A trap-door for UniqSupply
getKey		:: Unique -> Int		-- for Var
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
93
94
95
\end{code}


\begin{code}
96
mkUniqueGrimily (I# x) = MkUnique x
97

98
{-# INLINE getKey #-}
99
100
101
getKey (MkUnique x) = I# x
{-# INLINE getKey# #-}
getKey# (MkUnique x) = x
102

103
104
105
106
107
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)
108

109
110
111
-- newTagUnique changes the "domain" of a unique to a different char
newTagUnique u c = mkUnique c i where (_,i) = unpkUnique u

112
113
114
115
116
117
118
119
-- 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#)

120
mkUnique (C# c) (I# i)
121
122
  = MkUnique (w2i (tag `or#` bits))
  where
123
124
125
#if __GLASGOW_HASKELL__ >= 503
    tag  = i2w (ord# c) `uncheckedShiftL#` i2w_s 24#
#else
126
    tag  = i2w (ord# c) `shiftL#` i2w_s 24#
127
#endif
128
    bits = i2w i `and#` (i2w 16777215#){-``0x00ffffff''-}
129
130
131

unpkUnique (MkUnique u)
  = let
132
	tag = C# (chr# (w2i ((i2w u) `shiftr` (i2w_s 24#))))
133
	i   = I# (w2i ((i2w u) `and#` (i2w 16777215#){-``0x00ffffff''-}))
134
135
136
    in
    (tag, i)
  where
137
138
139
#if __GLASGOW_HASKELL__ >= 503
    shiftr x y = uncheckedShiftRL# x y
#else
sof's avatar
sof committed
140
    shiftr x y = shiftRL# x y
141
#endif
142
143
\end{code}

144
145
146
147
148
149
150
151
152
153


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

\begin{code}
class Uniquable a where
154
    getUnique :: a -> Unique
155

156
157
158
hasKey		:: Uniquable a => a -> Unique -> Bool
x `hasKey` k	= getUnique x == k

159
instance Uniquable FastString where
160
 getUnique fs = mkUniqueGrimily (I# (uniqueOfFS fs))
161

162
163
164
instance Uniquable PackageId where
 getUnique pid = getUnique (packageIdFS pid)

165
instance Uniquable Int where
166
 getUnique i = mkUniqueGrimily i
167
168
169
\end{code}


170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
%************************************************************************
%*									*
\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)
186
  = if u1 ==# u2 then EQ else if u1 <# u2 then LT else GT
187
188
189
190
191
192
193
194
195
196

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)
197
    compare a b = cmpUnique a b
198

199
200
-----------------
instance Uniquable Unique where
201
    getUnique u = u
202
203
204
205
\end{code}

We do sometimes make strings with @Uniques@ in them:
\begin{code}
206
pprUnique :: Unique -> SDoc
207
208
pprUnique uniq
  = case unpkUnique uniq of
209
      (tag, u) -> finish_ppr tag u (text (iToBase62 u))
210

211
212
#ifdef UNUSED
pprUnique10 :: Unique -> SDoc
213
214
pprUnique10 uniq	-- in base-10, dudes
  = case unpkUnique uniq of
sof's avatar
sof committed
215
      (tag, u) -> finish_ppr tag u (int u)
216
#endif
217

sof's avatar
sof committed
218
219
220
221
222
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
223

224
instance Outputable Unique where
225
    ppr u = pprUnique u
226

227
228
instance Show Unique where
    showsPrec p uniq = showsPrecSDoc p (pprUnique uniq)
229
230
231
232
233
234
235
236
237
238
239
\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.
240

241
\begin{code}
242
243
244
iToBase62 :: Int -> String
iToBase62 n@(I# n#) 
  = ASSERT(n >= 0) go n# ""
245
  where
246
247
248
249
250
251
252
253
    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"#
254
255
256
257
258
259
260
261
\end{code}

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

262
Allocation of unique supply characters:
263
264
265
	v,t,u : for renumbering value-, type- and usage- vars.
	B:   builtin
	C-E: pseudo uniques	(used in native-code generator)
266
	X:   uniques derived by deriveUnique
267
	_:   unifiable tyvars   (above)
268
	0-9: prelude things below
269

270
271
272
273
274
275
276
277
278
279
	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

280
\begin{code}
281
mkAlphaTyVarUnique i            = mkUnique '1' i
282
283

mkPreludeClassUnique i		= mkUnique '2' i
284
285
286
287
288

-- 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.

289
mkPreludeTyConUnique i		= mkUnique '3' (3*i)
290
291
mkTupleTyConUnique Boxed   a	= mkUnique '4' (3*a)
mkTupleTyConUnique Unboxed a	= mkUnique '5' (3*a)
292

293
294
295
296
297
298
299
-- 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
300
301
mkTupleDataConUnique Boxed a	= mkUnique '7' (2*a)	-- ditto (*may* be used in C labels)
mkTupleDataConUnique Unboxed a	= mkUnique '8' (2*a)
302
303
304
305
306

-- 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'
307

308
309
mkPrimOpIdUnique op		= mkUnique '9' op
mkPreludeMiscIdUnique i		= mkUnique '0' i
310

chak's avatar
chak committed
311
312
313
314
-- No numbers left anymore, so I pick something different for the character
-- tag 
mkPArrDataConUnique a	        = mkUnique ':' (2*a)

sof's avatar
sof committed
315
316
317
318
319
320
-- The "tyvar uniques" print specially nicely: a, b, c, etc.
-- See pprUnique for details

initTyVarUnique :: Unique
initTyVarUnique = mkUnique 't' 0

321
mkPseudoUniqueC, mkPseudoUniqueD, mkPseudoUniqueE, mkPseudoUniqueH,
322
   mkBuiltinUnique :: Int -> Unique
323
324

mkBuiltinUnique i = mkUnique 'B' i
325
326
327
328
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
329
330
\end{code}