PageRenderTime 106ms CodeModel.GetById 24ms RepoModel.GetById 3ms app.codeStats 1ms

/compiler/basicTypes/Literal.lhs

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