PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/compiler/basicTypes/Unique.lhs

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