PageRenderTime 55ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/compiler/basicTypes/Literal.lhs

http://github.com/ilyasergey/GHC-XAppFix
Haskell | 513 lines | 378 code | 72 blank | 63 comment | 10 complexity | a201a92c9c99f527a04d8d7a5e3d68fc MD5 | raw file
  1. %
  2. % (c) The University of Glasgow 2006
  3. % (c) The GRASP/AQUA Project, Glasgow University, 1998
  4. %
  5. \section[Literal]{@Literal@: Machine literals (unboxed, of course)}
  6. \begin{code}
  7. {-# LANGUAGE DeriveDataTypeable #-}
  8. {-# OPTIONS -fno-warn-tabs #-}
  9. -- The above warning supression flag is a temporary kludge.
  10. -- While working on this module you are encouraged to remove it and
  11. -- detab the module (please do the detabbing in a separate patch). See
  12. -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#TabsvsSpaces
  13. -- for details
  14. module Literal
  15. (
  16. -- * Main data type
  17. Literal(..) -- Exported to ParseIface
  18. -- ** Creating Literals
  19. , mkMachInt, mkMachWord
  20. , mkMachInt64, mkMachWord64
  21. , mkMachFloat, mkMachDouble
  22. , mkMachChar, mkMachString
  23. , mkLitInteger
  24. -- ** Operations on Literals
  25. , literalType
  26. , hashLiteral
  27. , absentLiteralOf
  28. , pprLiteral
  29. -- ** Predicates on Literals and their contents
  30. , litIsDupable, litIsTrivial, litIsLifted
  31. , inIntRange, inWordRange, tARGET_MAX_INT, inCharRange
  32. , isZeroLit
  33. , litFitsInChar
  34. -- ** Coercions
  35. , word2IntLit, int2WordLit
  36. , narrow8IntLit, narrow16IntLit, narrow32IntLit
  37. , narrow8WordLit, narrow16WordLit, narrow32WordLit
  38. , char2IntLit, int2CharLit
  39. , float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit
  40. , nullAddrLit, float2DoubleLit, double2FloatLit
  41. ) where
  42. #include "HsVersions.h"
  43. import TysPrim
  44. import PrelNames
  45. import Type
  46. import TypeRep
  47. import TyCon
  48. import Var
  49. import Outputable
  50. import FastTypes
  51. import FastString
  52. import BasicTypes
  53. import Binary
  54. import Constants
  55. import UniqFM
  56. import Data.Int
  57. import Data.Ratio
  58. import Data.Word
  59. import Data.Char
  60. import Data.Data ( Data, Typeable )
  61. import Numeric ( fromRat )
  62. \end{code}
  63. %************************************************************************
  64. %* *
  65. \subsection{Literals}
  66. %* *
  67. %************************************************************************
  68. \begin{code}
  69. -- | So-called 'Literal's are one of:
  70. --
  71. -- * An unboxed (/machine/) literal ('MachInt', 'MachFloat', etc.),
  72. -- which is presumed to be surrounded by appropriate constructors
  73. -- (@Int#@, etc.), so that the overall thing makes sense.
  74. --
  75. -- * The literal derived from the label mentioned in a \"foreign label\"
  76. -- declaration ('MachLabel')
  77. data Literal
  78. = ------------------
  79. -- First the primitive guys
  80. MachChar Char -- ^ @Char#@ - at least 31 bits. Create with 'mkMachChar'
  81. | MachStr FastString -- ^ A string-literal: stored and emitted
  82. -- UTF-8 encoded, we'll arrange to decode it
  83. -- at runtime. Also emitted with a @'\0'@
  84. -- terminator. Create with 'mkMachString'
  85. | MachNullAddr -- ^ The @NULL@ pointer, the only pointer value
  86. -- that can be represented as a Literal. Create
  87. -- with 'nullAddrLit'
  88. | MachInt Integer -- ^ @Int#@ - at least @WORD_SIZE_IN_BITS@ bits. Create with 'mkMachInt'
  89. | MachInt64 Integer -- ^ @Int64#@ - at least 64 bits. Create with 'mkMachInt64'
  90. | MachWord Integer -- ^ @Word#@ - at least @WORD_SIZE_IN_BITS@ bits. Create with 'mkMachWord'
  91. | MachWord64 Integer -- ^ @Word64#@ - at least 64 bits. Create with 'mkMachWord64'
  92. | MachFloat Rational -- ^ @Float#@. Create with 'mkMachFloat'
  93. | MachDouble Rational -- ^ @Double#@. Create with 'mkMachDouble'
  94. | MachLabel FastString
  95. (Maybe Int)
  96. FunctionOrData
  97. -- ^ A label literal. Parameters:
  98. --
  99. -- 1) The name of the symbol mentioned in the declaration
  100. --
  101. -- 2) The size (in bytes) of the arguments
  102. -- the label expects. Only applicable with
  103. -- @stdcall@ labels. @Just x@ => @\<x\>@ will
  104. -- be appended to label name when emitting assembly.
  105. | LitInteger Integer Id -- ^ Integer literals
  106. -- See Note [Integer literals]
  107. deriving (Data, Typeable)
  108. \end{code}
  109. Note [Integer literals]
  110. ~~~~~~~~~~~~~~~~~~~~~~~
  111. An Integer literal is represented using, well, an Integer, to make it
  112. easier to write RULEs for them.
  113. * The Id is for mkInteger, which we use when finally creating the core.
  114. * They only get converted into real Core,
  115. mkInteger [c1, c2, .., cn]
  116. during the CorePrep phase.
  117. * When we initally build an Integer literal, notably when
  118. deserialising it from an interface file (see the Binary instance
  119. below), we don't have convenient access to the mkInteger Id. So we
  120. just use an error thunk, and fill in the real Id when we do tcIfaceLit
  121. in TcIface.
  122. * When looking for CAF-hood (in TidyPgm), we must take account of the
  123. CAF-hood of the mk_integer field in LitInteger; see TidyPgm.cafRefsL.
  124. Indeed this is the only reason we put the mk_integer field in the
  125. literal -- otherwise we could just look it up in CorePrep.
  126. Binary instance
  127. \begin{code}
  128. instance Binary Literal where
  129. put_ bh (MachChar aa) = do putByte bh 0; put_ bh aa
  130. put_ bh (MachStr ab) = do putByte bh 1; put_ bh ab
  131. put_ bh (MachNullAddr) = do putByte bh 2
  132. put_ bh (MachInt ad) = do putByte bh 3; put_ bh ad
  133. put_ bh (MachInt64 ae) = do putByte bh 4; put_ bh ae
  134. put_ bh (MachWord af) = do putByte bh 5; put_ bh af
  135. put_ bh (MachWord64 ag) = do putByte bh 6; put_ bh ag
  136. put_ bh (MachFloat ah) = do putByte bh 7; put_ bh ah
  137. put_ bh (MachDouble ai) = do putByte bh 8; put_ bh ai
  138. put_ bh (MachLabel aj mb fod)
  139. = do putByte bh 9
  140. put_ bh aj
  141. put_ bh mb
  142. put_ bh fod
  143. put_ bh (LitInteger i _) = do putByte bh 10; put_ bh i
  144. get bh = do
  145. h <- getByte bh
  146. case h of
  147. 0 -> do
  148. aa <- get bh
  149. return (MachChar aa)
  150. 1 -> do
  151. ab <- get bh
  152. return (MachStr ab)
  153. 2 -> do
  154. return (MachNullAddr)
  155. 3 -> do
  156. ad <- get bh
  157. return (MachInt ad)
  158. 4 -> do
  159. ae <- get bh
  160. return (MachInt64 ae)
  161. 5 -> do
  162. af <- get bh
  163. return (MachWord af)
  164. 6 -> do
  165. ag <- get bh
  166. return (MachWord64 ag)
  167. 7 -> do
  168. ah <- get bh
  169. return (MachFloat ah)
  170. 8 -> do
  171. ai <- get bh
  172. return (MachDouble ai)
  173. 9 -> do
  174. aj <- get bh
  175. mb <- get bh
  176. fod <- get bh
  177. return (MachLabel aj mb fod)
  178. _ -> do
  179. i <- get bh
  180. return $ mkLitInteger i (panic "Evaluated the place holder for mkInteger")
  181. -- See Note [Integer literals] in Literal
  182. \end{code}
  183. \begin{code}
  184. instance Outputable Literal where
  185. ppr lit = pprLiteral (\d -> d) lit
  186. instance Show Literal where
  187. showsPrec p lit = showsPrecSDoc p (ppr lit)
  188. instance Eq Literal where
  189. a == b = case (a `compare` b) of { EQ -> True; _ -> False }
  190. a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
  191. instance Ord Literal where
  192. a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
  193. a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
  194. a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
  195. a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
  196. compare a b = cmpLit a b
  197. \end{code}
  198. Construction
  199. ~~~~~~~~~~~~
  200. \begin{code}
  201. -- | Creates a 'Literal' of type @Int#@
  202. mkMachInt :: Integer -> Literal
  203. mkMachInt x = ASSERT2( inIntRange x, integer x )
  204. MachInt x
  205. -- | Creates a 'Literal' of type @Word#@
  206. mkMachWord :: Integer -> Literal
  207. mkMachWord x = ASSERT2( inWordRange x, integer x )
  208. MachWord x
  209. -- | Creates a 'Literal' of type @Int64#@
  210. mkMachInt64 :: Integer -> Literal
  211. mkMachInt64 x = MachInt64 x
  212. -- | Creates a 'Literal' of type @Word64#@
  213. mkMachWord64 :: Integer -> Literal
  214. mkMachWord64 x = MachWord64 x
  215. -- | Creates a 'Literal' of type @Float#@
  216. mkMachFloat :: Rational -> Literal
  217. mkMachFloat = MachFloat
  218. -- | Creates a 'Literal' of type @Double#@
  219. mkMachDouble :: Rational -> Literal
  220. mkMachDouble = MachDouble
  221. -- | Creates a 'Literal' of type @Char#@
  222. mkMachChar :: Char -> Literal
  223. mkMachChar = MachChar
  224. -- | Creates a 'Literal' of type @Addr#@, which is appropriate for passing to
  225. -- e.g. some of the \"error\" functions in GHC.Err such as @GHC.Err.runtimeError@
  226. mkMachString :: String -> Literal
  227. mkMachString s = MachStr (mkFastString s) -- stored UTF-8 encoded
  228. mkLitInteger :: Integer -> Id -> Literal
  229. mkLitInteger = LitInteger
  230. inIntRange, inWordRange :: Integer -> Bool
  231. inIntRange x = x >= tARGET_MIN_INT && x <= tARGET_MAX_INT
  232. inWordRange x = x >= 0 && x <= tARGET_MAX_WORD
  233. inCharRange :: Char -> Bool
  234. inCharRange c = c >= '\0' && c <= chr tARGET_MAX_CHAR
  235. -- | Tests whether the literal represents a zero of whatever type it is
  236. isZeroLit :: Literal -> Bool
  237. isZeroLit (MachInt 0) = True
  238. isZeroLit (MachInt64 0) = True
  239. isZeroLit (MachWord 0) = True
  240. isZeroLit (MachWord64 0) = True
  241. isZeroLit (MachFloat 0) = True
  242. isZeroLit (MachDouble 0) = True
  243. isZeroLit _ = False
  244. \end{code}
  245. Coercions
  246. ~~~~~~~~~
  247. \begin{code}
  248. word2IntLit, int2WordLit,
  249. narrow8IntLit, narrow16IntLit, narrow32IntLit,
  250. narrow8WordLit, narrow16WordLit, narrow32WordLit,
  251. char2IntLit, int2CharLit,
  252. float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit,
  253. float2DoubleLit, double2FloatLit
  254. :: Literal -> Literal
  255. word2IntLit (MachWord w)
  256. | w > tARGET_MAX_INT = MachInt (w - tARGET_MAX_WORD - 1)
  257. | otherwise = MachInt w
  258. word2IntLit l = pprPanic "word2IntLit" (ppr l)
  259. int2WordLit (MachInt i)
  260. | i < 0 = MachWord (1 + tARGET_MAX_WORD + i) -- (-1) ---> tARGET_MAX_WORD
  261. | otherwise = MachWord i
  262. int2WordLit l = pprPanic "int2WordLit" (ppr l)
  263. narrow8IntLit (MachInt i) = MachInt (toInteger (fromInteger i :: Int8))
  264. narrow8IntLit l = pprPanic "narrow8IntLit" (ppr l)
  265. narrow16IntLit (MachInt i) = MachInt (toInteger (fromInteger i :: Int16))
  266. narrow16IntLit l = pprPanic "narrow16IntLit" (ppr l)
  267. narrow32IntLit (MachInt i) = MachInt (toInteger (fromInteger i :: Int32))
  268. narrow32IntLit l = pprPanic "narrow32IntLit" (ppr l)
  269. narrow8WordLit (MachWord w) = MachWord (toInteger (fromInteger w :: Word8))
  270. narrow8WordLit l = pprPanic "narrow8WordLit" (ppr l)
  271. narrow16WordLit (MachWord w) = MachWord (toInteger (fromInteger w :: Word16))
  272. narrow16WordLit l = pprPanic "narrow16WordLit" (ppr l)
  273. narrow32WordLit (MachWord w) = MachWord (toInteger (fromInteger w :: Word32))
  274. narrow32WordLit l = pprPanic "narrow32WordLit" (ppr l)
  275. char2IntLit (MachChar c) = MachInt (toInteger (ord c))
  276. char2IntLit l = pprPanic "char2IntLit" (ppr l)
  277. int2CharLit (MachInt i) = MachChar (chr (fromInteger i))
  278. int2CharLit l = pprPanic "int2CharLit" (ppr l)
  279. float2IntLit (MachFloat f) = MachInt (truncate f)
  280. float2IntLit l = pprPanic "float2IntLit" (ppr l)
  281. int2FloatLit (MachInt i) = MachFloat (fromInteger i)
  282. int2FloatLit l = pprPanic "int2FloatLit" (ppr l)
  283. double2IntLit (MachDouble f) = MachInt (truncate f)
  284. double2IntLit l = pprPanic "double2IntLit" (ppr l)
  285. int2DoubleLit (MachInt i) = MachDouble (fromInteger i)
  286. int2DoubleLit l = pprPanic "int2DoubleLit" (ppr l)
  287. float2DoubleLit (MachFloat f) = MachDouble f
  288. float2DoubleLit l = pprPanic "float2DoubleLit" (ppr l)
  289. double2FloatLit (MachDouble d) = MachFloat d
  290. double2FloatLit l = pprPanic "double2FloatLit" (ppr l)
  291. nullAddrLit :: Literal
  292. nullAddrLit = MachNullAddr
  293. \end{code}
  294. Predicates
  295. ~~~~~~~~~~
  296. \begin{code}
  297. -- | True if there is absolutely no penalty to duplicating the literal.
  298. -- False principally of strings
  299. litIsTrivial :: Literal -> Bool
  300. -- c.f. CoreUtils.exprIsTrivial
  301. litIsTrivial (MachStr _) = False
  302. litIsTrivial (LitInteger {}) = False
  303. litIsTrivial _ = True
  304. -- | True if code space does not go bad if we duplicate this literal
  305. -- Currently we treat it just like 'litIsTrivial'
  306. litIsDupable :: Literal -> Bool
  307. -- c.f. CoreUtils.exprIsDupable
  308. litIsDupable (MachStr _) = False
  309. litIsDupable (LitInteger i _) = inIntRange i
  310. litIsDupable _ = True
  311. litFitsInChar :: Literal -> Bool
  312. litFitsInChar (MachInt i)
  313. = fromInteger i <= ord minBound
  314. && fromInteger i >= ord maxBound
  315. litFitsInChar _ = False
  316. litIsLifted :: Literal -> Bool
  317. litIsLifted (LitInteger {}) = True
  318. litIsLifted _ = False
  319. \end{code}
  320. Types
  321. ~~~~~
  322. \begin{code}
  323. -- | Find the Haskell 'Type' the literal occupies
  324. literalType :: Literal -> Type
  325. literalType MachNullAddr = addrPrimTy
  326. literalType (MachChar _) = charPrimTy
  327. literalType (MachStr _) = addrPrimTy
  328. literalType (MachInt _) = intPrimTy
  329. literalType (MachWord _) = wordPrimTy
  330. literalType (MachInt64 _) = int64PrimTy
  331. literalType (MachWord64 _) = word64PrimTy
  332. literalType (MachFloat _) = floatPrimTy
  333. literalType (MachDouble _) = doublePrimTy
  334. literalType (MachLabel _ _ _) = addrPrimTy
  335. literalType (LitInteger _ mk_integer_id)
  336. -- We really mean idType, rather than varType, but importing Id
  337. -- causes a module import loop
  338. = case varType mk_integer_id of
  339. FunTy _ (FunTy _ integerTy) -> integerTy
  340. _ -> panic "literalType: mkIntegerId has the wrong type"
  341. absentLiteralOf :: TyCon -> Maybe Literal
  342. -- Return a literal of the appropriate primtive
  343. -- TyCon, to use as a placeholder when it doesn't matter
  344. absentLiteralOf tc = lookupUFM absent_lits (tyConName tc)
  345. absent_lits :: UniqFM Literal
  346. absent_lits = listToUFM [ (addrPrimTyConKey, MachNullAddr)
  347. , (charPrimTyConKey, MachChar 'x')
  348. , (intPrimTyConKey, MachInt 0)
  349. , (int64PrimTyConKey, MachInt64 0)
  350. , (floatPrimTyConKey, MachFloat 0)
  351. , (doublePrimTyConKey, MachDouble 0)
  352. , (wordPrimTyConKey, MachWord 0)
  353. , (word64PrimTyConKey, MachWord64 0) ]
  354. \end{code}
  355. Comparison
  356. ~~~~~~~~~~
  357. \begin{code}
  358. cmpLit :: Literal -> Literal -> Ordering
  359. cmpLit (MachChar a) (MachChar b) = a `compare` b
  360. cmpLit (MachStr a) (MachStr b) = a `compare` b
  361. cmpLit (MachNullAddr) (MachNullAddr) = EQ
  362. cmpLit (MachInt a) (MachInt b) = a `compare` b
  363. cmpLit (MachWord a) (MachWord b) = a `compare` b
  364. cmpLit (MachInt64 a) (MachInt64 b) = a `compare` b
  365. cmpLit (MachWord64 a) (MachWord64 b) = a `compare` b
  366. cmpLit (MachFloat a) (MachFloat b) = a `compare` b
  367. cmpLit (MachDouble a) (MachDouble b) = a `compare` b
  368. cmpLit (MachLabel a _ _) (MachLabel b _ _) = a `compare` b
  369. cmpLit (LitInteger a _) (LitInteger b _) = a `compare` b
  370. cmpLit lit1 lit2 | litTag lit1 <# litTag lit2 = LT
  371. | otherwise = GT
  372. litTag :: Literal -> FastInt
  373. litTag (MachChar _) = _ILIT(1)
  374. litTag (MachStr _) = _ILIT(2)
  375. litTag (MachNullAddr) = _ILIT(3)
  376. litTag (MachInt _) = _ILIT(4)
  377. litTag (MachWord _) = _ILIT(5)
  378. litTag (MachInt64 _) = _ILIT(6)
  379. litTag (MachWord64 _) = _ILIT(7)
  380. litTag (MachFloat _) = _ILIT(8)
  381. litTag (MachDouble _) = _ILIT(9)
  382. litTag (MachLabel _ _ _) = _ILIT(10)
  383. litTag (LitInteger {}) = _ILIT(11)
  384. \end{code}
  385. Printing
  386. ~~~~~~~~
  387. * MachX (i.e. unboxed) things are printed unadornded (e.g. 3, 'a', "foo")
  388. exceptions: MachFloat gets an initial keyword prefix.
  389. \begin{code}
  390. pprLiteral :: (SDoc -> SDoc) -> Literal -> SDoc
  391. -- The function is used on non-atomic literals
  392. -- to wrap parens around literals that occur in
  393. -- a context requiring an atomic thing
  394. pprLiteral _ (MachChar ch) = pprHsChar ch
  395. pprLiteral _ (MachStr s) = pprHsString s
  396. pprLiteral _ (MachInt i) = pprIntVal i
  397. pprLiteral _ (MachDouble d) = double (fromRat d)
  398. pprLiteral _ (MachNullAddr) = ptext (sLit "__NULL")
  399. pprLiteral add_par (LitInteger i _) = add_par (ptext (sLit "__integer") <+> integer i)
  400. pprLiteral add_par (MachInt64 i) = add_par (ptext (sLit "__int64") <+> integer i)
  401. pprLiteral add_par (MachWord w) = add_par (ptext (sLit "__word") <+> integer w)
  402. pprLiteral add_par (MachWord64 w) = add_par (ptext (sLit "__word64") <+> integer w)
  403. pprLiteral add_par (MachFloat f) = add_par (ptext (sLit "__float") <+> float (fromRat f))
  404. pprLiteral add_par (MachLabel l mb fod) = add_par (ptext (sLit "__label") <+> b <+> ppr fod)
  405. where b = case mb of
  406. Nothing -> pprHsString l
  407. Just x -> doubleQuotes (text (unpackFS l ++ '@':show x))
  408. pprIntVal :: Integer -> SDoc
  409. -- ^ Print negative integers with parens to be sure it's unambiguous
  410. pprIntVal i | i < 0 = parens (integer i)
  411. | otherwise = integer i
  412. \end{code}
  413. %************************************************************************
  414. %* *
  415. \subsection{Hashing}
  416. %* *
  417. %************************************************************************
  418. Hash values should be zero or a positive integer. No negatives please.
  419. (They mess up the UniqFM for some reason.)
  420. \begin{code}
  421. hashLiteral :: Literal -> Int
  422. hashLiteral (MachChar c) = ord c + 1000 -- Keep it out of range of common ints
  423. hashLiteral (MachStr s) = hashFS s
  424. hashLiteral (MachNullAddr) = 0
  425. hashLiteral (MachInt i) = hashInteger i
  426. hashLiteral (MachInt64 i) = hashInteger i
  427. hashLiteral (MachWord i) = hashInteger i
  428. hashLiteral (MachWord64 i) = hashInteger i
  429. hashLiteral (MachFloat r) = hashRational r
  430. hashLiteral (MachDouble r) = hashRational r
  431. hashLiteral (MachLabel s _ _) = hashFS s
  432. hashLiteral (LitInteger i _) = hashInteger i
  433. hashRational :: Rational -> Int
  434. hashRational r = hashInteger (numerator r)
  435. hashInteger :: Integer -> Int
  436. hashInteger i = 1 + abs (fromInteger (i `rem` 10000))
  437. -- The 1+ is to avoid zero, which is a Bad Number
  438. -- since we use * to combine hash values
  439. hashFS :: FastString -> Int
  440. hashFS s = iBox (uniqueOfFS s)
  441. \end{code}