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