PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/ghc-7.0.4/compiler/basicTypes/Unique.lhs

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