PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/compiler/basicTypes/Literal.lhs

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