PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://picorec.googlecode.com/
Haskell | 440 lines | 319 code | 61 blank | 60 comment | 6 complexity | 5fd7a1b4ff7e58af0e9dd059813e0d15 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, 1998
  4. %
  5. \section[Literal]{@Literal@: Machine literals (unboxed, of course)}
  6. \begin{code}
  7. {-# OPTIONS -fno-warn-incomplete-patterns #-}
  8. -- The above warning supression flag is a temporary kludge.
  9. -- While working on this module you are encouraged to remove it and fix
  10. -- any warnings in the module. See
  11. -- http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
  12. -- for details
  13. {-# LANGUAGE DeriveDataTypeable #-}
  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. -- ** Operations on Literals
  24. , literalType
  25. , hashLiteral
  26. , absentLiteralOf
  27. -- ** Predicates on Literals and their contents
  28. , litIsDupable, litIsTrivial
  29. , inIntRange, inWordRange, tARGET_MAX_INT, inCharRange
  30. , isZeroLit
  31. , litFitsInChar
  32. -- ** Coercions
  33. , word2IntLit, int2WordLit
  34. , narrow8IntLit, narrow16IntLit, narrow32IntLit
  35. , narrow8WordLit, narrow16WordLit, narrow32WordLit
  36. , char2IntLit, int2CharLit
  37. , float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit
  38. , nullAddrLit, float2DoubleLit, double2FloatLit
  39. ) where
  40. import TysPrim
  41. import PrelNames
  42. import Type
  43. import TyCon
  44. import Outputable
  45. import FastTypes
  46. import FastString
  47. import BasicTypes
  48. import Binary
  49. import Constants
  50. import UniqFM
  51. import Data.Int
  52. import Data.Ratio
  53. import Data.Word
  54. import Data.Char
  55. import Data.Data( Data, Typeable )
  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 FastString -- ^ 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. deriving (Data, Typeable)
  100. \end{code}
  101. Binary instance
  102. \begin{code}
  103. instance Binary Literal where
  104. put_ bh (MachChar aa) = do putByte bh 0; put_ bh aa
  105. put_ bh (MachStr ab) = do putByte bh 1; put_ bh ab
  106. put_ bh (MachNullAddr) = do putByte bh 2
  107. put_ bh (MachInt ad) = do putByte bh 3; put_ bh ad
  108. put_ bh (MachInt64 ae) = do putByte bh 4; put_ bh ae
  109. put_ bh (MachWord af) = do putByte bh 5; put_ bh af
  110. put_ bh (MachWord64 ag) = do putByte bh 6; put_ bh ag
  111. put_ bh (MachFloat ah) = do putByte bh 7; put_ bh ah
  112. put_ bh (MachDouble ai) = do putByte bh 8; put_ bh ai
  113. put_ bh (MachLabel aj mb fod)
  114. = do putByte bh 9
  115. put_ bh aj
  116. put_ bh mb
  117. put_ bh fod
  118. get bh = do
  119. h <- getByte bh
  120. case h of
  121. 0 -> do
  122. aa <- get bh
  123. return (MachChar aa)
  124. 1 -> do
  125. ab <- get bh
  126. return (MachStr ab)
  127. 2 -> do
  128. return (MachNullAddr)
  129. 3 -> do
  130. ad <- get bh
  131. return (MachInt ad)
  132. 4 -> do
  133. ae <- get bh
  134. return (MachInt64 ae)
  135. 5 -> do
  136. af <- get bh
  137. return (MachWord af)
  138. 6 -> do
  139. ag <- get bh
  140. return (MachWord64 ag)
  141. 7 -> do
  142. ah <- get bh
  143. return (MachFloat ah)
  144. 8 -> do
  145. ai <- get bh
  146. return (MachDouble ai)
  147. 9 -> do
  148. aj <- get bh
  149. mb <- get bh
  150. fod <- get bh
  151. return (MachLabel aj mb fod)
  152. \end{code}
  153. \begin{code}
  154. instance Outputable Literal where
  155. ppr lit = pprLit lit
  156. instance Show Literal where
  157. showsPrec p lit = showsPrecSDoc p (ppr lit)
  158. instance Eq Literal where
  159. a == b = case (a `compare` b) of { EQ -> True; _ -> False }
  160. a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
  161. instance Ord Literal where
  162. a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
  163. a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
  164. a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
  165. a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
  166. compare a b = cmpLit a b
  167. \end{code}
  168. Construction
  169. ~~~~~~~~~~~~
  170. \begin{code}
  171. -- | Creates a 'Literal' of type @Int#@
  172. mkMachInt :: Integer -> Literal
  173. mkMachInt x = -- ASSERT2( inIntRange x, integer x )
  174. -- Not true: you can write out of range Int# literals
  175. -- For example, one can write (intToWord# 0xffff0000) to
  176. -- get a particular Word bit-pattern, and there's no other
  177. -- convenient way to write such literals, which is why we allow it.
  178. MachInt x
  179. -- | Creates a 'Literal' of type @Word#@
  180. mkMachWord :: Integer -> Literal
  181. mkMachWord x = -- ASSERT2( inWordRange x, integer x )
  182. MachWord x
  183. -- | Creates a 'Literal' of type @Int64#@
  184. mkMachInt64 :: Integer -> Literal
  185. mkMachInt64 x = MachInt64 x
  186. -- | Creates a 'Literal' of type @Word64#@
  187. mkMachWord64 :: Integer -> Literal
  188. mkMachWord64 x = MachWord64 x
  189. -- | Creates a 'Literal' of type @Float#@
  190. mkMachFloat :: Rational -> Literal
  191. mkMachFloat = MachFloat
  192. -- | Creates a 'Literal' of type @Double#@
  193. mkMachDouble :: Rational -> Literal
  194. mkMachDouble = MachDouble
  195. -- | Creates a 'Literal' of type @Char#@
  196. mkMachChar :: Char -> Literal
  197. mkMachChar = MachChar
  198. -- | Creates a 'Literal' of type @Addr#@, which is appropriate for passing to
  199. -- e.g. some of the \"error\" functions in GHC.Err such as @GHC.Err.runtimeError@
  200. mkMachString :: String -> Literal
  201. mkMachString s = MachStr (mkFastString s) -- stored UTF-8 encoded
  202. inIntRange, inWordRange :: Integer -> Bool
  203. inIntRange x = x >= tARGET_MIN_INT && x <= tARGET_MAX_INT
  204. inWordRange x = x >= 0 && x <= tARGET_MAX_WORD
  205. inCharRange :: Char -> Bool
  206. inCharRange c = c >= '\0' && c <= chr tARGET_MAX_CHAR
  207. -- | Tests whether the literal represents a zero of whatever type it is
  208. isZeroLit :: Literal -> Bool
  209. isZeroLit (MachInt 0) = True
  210. isZeroLit (MachInt64 0) = True
  211. isZeroLit (MachWord 0) = True
  212. isZeroLit (MachWord64 0) = True
  213. isZeroLit (MachFloat 0) = True
  214. isZeroLit (MachDouble 0) = True
  215. isZeroLit _ = False
  216. \end{code}
  217. Coercions
  218. ~~~~~~~~~
  219. \begin{code}
  220. word2IntLit, int2WordLit,
  221. narrow8IntLit, narrow16IntLit, narrow32IntLit,
  222. narrow8WordLit, narrow16WordLit, narrow32WordLit,
  223. char2IntLit, int2CharLit,
  224. float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit,
  225. float2DoubleLit, double2FloatLit
  226. :: Literal -> Literal
  227. word2IntLit (MachWord w)
  228. | w > tARGET_MAX_INT = MachInt (w - tARGET_MAX_WORD - 1)
  229. | otherwise = MachInt w
  230. int2WordLit (MachInt i)
  231. | i < 0 = MachWord (1 + tARGET_MAX_WORD + i) -- (-1) ---> tARGET_MAX_WORD
  232. | otherwise = MachWord i
  233. narrow8IntLit (MachInt i) = MachInt (toInteger (fromInteger i :: Int8))
  234. narrow16IntLit (MachInt i) = MachInt (toInteger (fromInteger i :: Int16))
  235. narrow32IntLit (MachInt i) = MachInt (toInteger (fromInteger i :: Int32))
  236. narrow8WordLit (MachWord w) = MachWord (toInteger (fromInteger w :: Word8))
  237. narrow16WordLit (MachWord w) = MachWord (toInteger (fromInteger w :: Word16))
  238. narrow32WordLit (MachWord w) = MachWord (toInteger (fromInteger w :: Word32))
  239. char2IntLit (MachChar c) = MachInt (toInteger (ord c))
  240. int2CharLit (MachInt i) = MachChar (chr (fromInteger i))
  241. float2IntLit (MachFloat f) = MachInt (truncate f)
  242. int2FloatLit (MachInt i) = MachFloat (fromInteger i)
  243. double2IntLit (MachDouble f) = MachInt (truncate f)
  244. int2DoubleLit (MachInt i) = MachDouble (fromInteger i)
  245. float2DoubleLit (MachFloat f) = MachDouble f
  246. double2FloatLit (MachDouble d) = MachFloat d
  247. nullAddrLit :: Literal
  248. nullAddrLit = MachNullAddr
  249. \end{code}
  250. Predicates
  251. ~~~~~~~~~~
  252. \begin{code}
  253. -- | True if there is absolutely no penalty to duplicating the literal.
  254. -- False principally of strings
  255. litIsTrivial :: Literal -> Bool
  256. -- c.f. CoreUtils.exprIsTrivial
  257. litIsTrivial (MachStr _) = False
  258. litIsTrivial _ = True
  259. -- | True if code space does not go bad if we duplicate this literal
  260. -- Currently we treat it just like 'litIsTrivial'
  261. litIsDupable :: Literal -> Bool
  262. -- c.f. CoreUtils.exprIsDupable
  263. litIsDupable (MachStr _) = False
  264. litIsDupable _ = True
  265. litFitsInChar :: Literal -> Bool
  266. litFitsInChar (MachInt i)
  267. = fromInteger i <= ord minBound
  268. && fromInteger i >= ord maxBound
  269. litFitsInChar _ = False
  270. \end{code}
  271. Types
  272. ~~~~~
  273. \begin{code}
  274. -- | Find the Haskell 'Type' the literal occupies
  275. literalType :: Literal -> Type
  276. literalType MachNullAddr = addrPrimTy
  277. literalType (MachChar _) = charPrimTy
  278. literalType (MachStr _) = addrPrimTy
  279. literalType (MachInt _) = intPrimTy
  280. literalType (MachWord _) = wordPrimTy
  281. literalType (MachInt64 _) = int64PrimTy
  282. literalType (MachWord64 _) = word64PrimTy
  283. literalType (MachFloat _) = floatPrimTy
  284. literalType (MachDouble _) = doublePrimTy
  285. literalType (MachLabel _ _ _) = addrPrimTy
  286. absentLiteralOf :: TyCon -> Maybe Literal
  287. -- Return a literal of the appropriate primtive
  288. -- TyCon, to use as a placeholder when it doesn't matter
  289. absentLiteralOf tc = lookupUFM absent_lits (tyConName tc)
  290. absent_lits :: UniqFM Literal
  291. absent_lits = listToUFM [ (addrPrimTyConKey, MachNullAddr)
  292. , (charPrimTyConKey, MachChar 'x')
  293. , (intPrimTyConKey, MachInt 0)
  294. , (int64PrimTyConKey, MachInt64 0)
  295. , (floatPrimTyConKey, MachFloat 0)
  296. , (doublePrimTyConKey, MachDouble 0)
  297. , (wordPrimTyConKey, MachWord 0)
  298. , (word64PrimTyConKey, MachWord64 0) ]
  299. \end{code}
  300. Comparison
  301. ~~~~~~~~~~
  302. \begin{code}
  303. cmpLit :: Literal -> Literal -> Ordering
  304. cmpLit (MachChar a) (MachChar b) = a `compare` b
  305. cmpLit (MachStr a) (MachStr b) = a `compare` b
  306. cmpLit (MachNullAddr) (MachNullAddr) = EQ
  307. cmpLit (MachInt a) (MachInt b) = a `compare` b
  308. cmpLit (MachWord a) (MachWord b) = a `compare` b
  309. cmpLit (MachInt64 a) (MachInt64 b) = a `compare` b
  310. cmpLit (MachWord64 a) (MachWord64 b) = a `compare` b
  311. cmpLit (MachFloat a) (MachFloat b) = a `compare` b
  312. cmpLit (MachDouble a) (MachDouble b) = a `compare` b
  313. cmpLit (MachLabel a _ _) (MachLabel b _ _) = a `compare` b
  314. cmpLit lit1 lit2 | litTag lit1 <# litTag lit2 = LT
  315. | otherwise = GT
  316. litTag :: Literal -> FastInt
  317. litTag (MachChar _) = _ILIT(1)
  318. litTag (MachStr _) = _ILIT(2)
  319. litTag (MachNullAddr) = _ILIT(3)
  320. litTag (MachInt _) = _ILIT(4)
  321. litTag (MachWord _) = _ILIT(5)
  322. litTag (MachInt64 _) = _ILIT(6)
  323. litTag (MachWord64 _) = _ILIT(7)
  324. litTag (MachFloat _) = _ILIT(8)
  325. litTag (MachDouble _) = _ILIT(9)
  326. litTag (MachLabel _ _ _) = _ILIT(10)
  327. \end{code}
  328. Printing
  329. ~~~~~~~~
  330. * MachX (i.e. unboxed) things are printed unadornded (e.g. 3, 'a', "foo")
  331. exceptions: MachFloat gets an initial keyword prefix.
  332. \begin{code}
  333. pprLit :: Literal -> SDoc
  334. pprLit (MachChar ch) = pprHsChar ch
  335. pprLit (MachStr s) = pprHsString s
  336. pprLit (MachInt i) = pprIntVal i
  337. pprLit (MachInt64 i) = ptext (sLit "__int64") <+> integer i
  338. pprLit (MachWord w) = ptext (sLit "__word") <+> integer w
  339. pprLit (MachWord64 w) = ptext (sLit "__word64") <+> integer w
  340. pprLit (MachFloat f) = ptext (sLit "__float") <+> rational f
  341. pprLit (MachDouble d) = rational d
  342. pprLit (MachNullAddr) = ptext (sLit "__NULL")
  343. pprLit (MachLabel l mb fod) = ptext (sLit "__label") <+> b <+> ppr fod
  344. where b = case mb of
  345. Nothing -> pprHsString l
  346. Just x -> doubleQuotes (text (unpackFS l ++ '@':show x))
  347. pprIntVal :: Integer -> SDoc
  348. -- ^ Print negative integers with parens to be sure it's unambiguous
  349. pprIntVal i | i < 0 = parens (integer i)
  350. | otherwise = integer i
  351. \end{code}
  352. %************************************************************************
  353. %* *
  354. \subsection{Hashing}
  355. %* *
  356. %************************************************************************
  357. Hash values should be zero or a positive integer. No negatives please.
  358. (They mess up the UniqFM for some reason.)
  359. \begin{code}
  360. hashLiteral :: Literal -> Int
  361. hashLiteral (MachChar c) = ord c + 1000 -- Keep it out of range of common ints
  362. hashLiteral (MachStr s) = hashFS s
  363. hashLiteral (MachNullAddr) = 0
  364. hashLiteral (MachInt i) = hashInteger i
  365. hashLiteral (MachInt64 i) = hashInteger i
  366. hashLiteral (MachWord i) = hashInteger i
  367. hashLiteral (MachWord64 i) = hashInteger i
  368. hashLiteral (MachFloat r) = hashRational r
  369. hashLiteral (MachDouble r) = hashRational r
  370. hashLiteral (MachLabel s _ _) = hashFS s
  371. hashRational :: Rational -> Int
  372. hashRational r = hashInteger (numerator r)
  373. hashInteger :: Integer -> Int
  374. hashInteger i = 1 + abs (fromInteger (i `rem` 10000))
  375. -- The 1+ is to avoid zero, which is a Bad Number
  376. -- since we use * to combine hash values
  377. hashFS :: FastString -> Int
  378. hashFS s = iBox (uniqueOfFS s)
  379. \end{code}