2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
4 \section[Literal]{@Literal@: Machine literals (unboxed, of course)}
8 Literal(..), -- Exported to ParseIface
10 mkMachInt64, mkMachWord64,
12 literalType, literalPrimRep,
15 inIntRange, inWordRange,
17 word2IntLit, int2WordLit, int2CharLit,
18 int2FloatLit, int2DoubleLit, char2IntLit
21 #include "HsVersions.h"
23 import TysPrim ( charPrimTy, addrPrimTy, floatPrimTy, doublePrimTy,
24 intPrimTy, wordPrimTy, int64PrimTy, word64PrimTy
26 import Name ( hashName )
27 import PrimRep ( PrimRep(..) )
28 import TyCon ( isNewTyCon )
29 import Type ( Type, typePrimRep )
30 import PprType ( pprParendType )
31 import Demand ( Demand )
32 import CStrings ( charToC, charToEasyHaskell, pprFSInCStyle )
35 import Util ( thenCmp )
37 import Ratio ( numerator, denominator )
38 import FastString ( uniqueOfFS )
39 import Char ( ord, chr )
41 #if __GLASGOW_HASKELL__ >= 404
42 import GlaExts ( fromInt )
48 %************************************************************************
52 %************************************************************************
54 If we're compiling with GHC (and we're not cross-compiling), then we
55 know that minBound and maxBound :: Int are the right values for the
56 target architecture. Otherwise, we assume -2^31 and 2^31-1
57 respectively (which will be wrong on a 64-bit machine).
60 tARGET_MIN_INT, tARGET_MAX_INT, tARGET_MAX_WORD :: Integer
61 #if __GLASGOW_HASKELL__
62 tARGET_MIN_INT = toInteger (minBound :: Int)
63 tARGET_MAX_INT = toInteger (maxBound :: Int)
65 tARGET_MIN_INT = -2147483648
66 tARGET_MAX_INT = 2147483647
68 tARGET_MAX_WORD = (tARGET_MAX_INT * 2) + 1
72 %************************************************************************
76 %************************************************************************
78 So-called @Literals@ are {\em either}:
81 An unboxed (``machine'') literal (type: @IntPrim@, @FloatPrim@, etc.),
82 which is presumed to be surrounded by appropriate constructors
83 (@mKINT@, etc.), so that the overall thing makes sense.
85 An Integer, Rational, or String literal whose representation we are
86 {\em uncommitted} about; i.e., the surrounding with constructors,
87 function applications, etc., etc., has not yet been done.
93 -- First the primitive guys
97 | MachAddr Integer -- Whatever this machine thinks is a "pointer"
99 | MachInt Integer -- Int# At least 32 bits
100 | MachInt64 Integer -- Int64# At least 64 bits
101 | MachWord Integer -- Word# At least 32 bits
102 | MachWord64 Integer -- Word64# At least 64 bits
105 | MachDouble Rational
107 | MachLitLit FAST_STRING Type -- Type might be Add# or Int# etc
111 instance Outputable Literal where
114 instance Show Literal where
115 showsPrec p lit = showsPrecSDoc p (ppr lit)
117 instance Eq Literal where
118 a == b = case (a `compare` b) of { EQ -> True; _ -> False }
119 a /= b = case (a `compare` b) of { EQ -> False; _ -> True }
121 instance Ord Literal where
122 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
123 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
124 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
125 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
126 compare a b = cmpLit a b
133 mkMachInt, mkMachWord, mkMachInt64, mkMachWord64 :: Integer -> Literal
135 mkMachInt x = ASSERT2( inIntRange x, integer x ) MachInt x
136 mkMachWord x = ASSERT2( inWordRange x, integer x ) MachWord x
137 mkMachInt64 x = MachInt64 x -- Assertions?
138 mkMachWord64 x = MachWord64 x -- Ditto?
140 inIntRange, inWordRange :: Integer -> Bool
141 inIntRange x = x >= tARGET_MIN_INT && x <= tARGET_MAX_INT
142 inWordRange x = x >= 0 && x <= tARGET_MAX_WORD
148 word2IntLit, int2WordLit, int2CharLit, char2IntLit :: Literal -> Literal
149 int2FloatLit, int2DoubleLit :: Literal -> Literal
151 word2IntLit (MachWord w)
152 | w > tARGET_MAX_INT = MachInt ((-1) + tARGET_MAX_WORD - w)
153 | otherwise = MachInt w
155 int2WordLit (MachInt i)
156 | i < 0 = MachWord (1 + tARGET_MAX_WORD + i) -- (-1) ---> tARGET_MAX_WORD
157 | otherwise = MachWord i
159 int2CharLit (MachInt i) = MachChar (chr (fromInteger i))
160 char2IntLit (MachChar c) = MachInt (toInteger (ord c))
162 int2FloatLit (MachInt i) = MachFloat (fromInteger i)
163 int2DoubleLit (MachInt i) = MachDouble (fromInteger i)
169 isLitLitLit (MachLitLit _ _) = True
170 isLitLitLit _ = False
176 literalType :: Literal -> Type
177 literalType (MachChar _) = charPrimTy
178 literalType (MachStr _) = addrPrimTy
179 literalType (MachAddr _) = addrPrimTy
180 literalType (MachInt _) = intPrimTy
181 literalType (MachWord _) = wordPrimTy
182 literalType (MachInt64 _) = int64PrimTy
183 literalType (MachWord64 _) = word64PrimTy
184 literalType (MachFloat _) = floatPrimTy
185 literalType (MachDouble _) = doublePrimTy
186 literalType (MachLitLit _ ty) = ty
190 literalPrimRep :: Literal -> PrimRep
192 literalPrimRep (MachChar _) = CharRep
193 literalPrimRep (MachStr _) = AddrRep -- specifically: "char *"
194 literalPrimRep (MachAddr _) = AddrRep
195 literalPrimRep (MachInt _) = IntRep
196 literalPrimRep (MachWord _) = WordRep
197 literalPrimRep (MachInt64 _) = Int64Rep
198 literalPrimRep (MachWord64 _) = Word64Rep
199 literalPrimRep (MachFloat _) = FloatRep
200 literalPrimRep (MachDouble _) = DoubleRep
201 literalPrimRep (MachLitLit _ ty) = typePrimRep ty
208 cmpLit (MachChar a) (MachChar b) = a `compare` b
209 cmpLit (MachStr a) (MachStr b) = a `compare` b
210 cmpLit (MachAddr a) (MachAddr b) = a `compare` b
211 cmpLit (MachInt a) (MachInt b) = a `compare` b
212 cmpLit (MachWord a) (MachWord b) = a `compare` b
213 cmpLit (MachInt64 a) (MachInt64 b) = a `compare` b
214 cmpLit (MachWord64 a) (MachWord64 b) = a `compare` b
215 cmpLit (MachFloat a) (MachFloat b) = a `compare` b
216 cmpLit (MachDouble a) (MachDouble b) = a `compare` b
217 cmpLit (MachLitLit a b) (MachLitLit c d) = (a `compare` c) `thenCmp` (b `compare` d)
218 cmpLit lit1 lit2 | litTag lit1 _LT_ litTag lit2 = LT
221 litTag (MachChar _) = ILIT(1)
222 litTag (MachStr _) = ILIT(2)
223 litTag (MachAddr _) = ILIT(3)
224 litTag (MachInt _) = ILIT(4)
225 litTag (MachWord _) = ILIT(5)
226 litTag (MachInt64 _) = ILIT(6)
227 litTag (MachWord64 _) = ILIT(7)
228 litTag (MachFloat _) = ILIT(8)
229 litTag (MachDouble _) = ILIT(9)
230 litTag (MachLitLit _ _) = ILIT(10)
235 * MachX (i.e. unboxed) things are printed unadornded (e.g. 3, 'a', "foo")
236 exceptions: MachFloat and MachAddr get an initial keyword prefix
240 = getPprStyle $ \ sty ->
242 code_style = codeStyle sty
245 MachChar ch | code_style -> hcat [ptext SLIT("(C_)"), char '\'',
246 text (charToC ch), char '\'']
247 | ifaceStyle sty -> char '\'' <> text (charToEasyHaskell ch) <> char '\''
248 | otherwise -> text ['\'', ch, '\'']
250 MachStr s | code_style -> pprFSInCStyle s
251 | otherwise -> pprFSAsString s
253 MachInt i | code_style && i == tARGET_MIN_INT -> parens (integer (i+1) <> text "-1")
254 -- Avoid a problem whereby gcc interprets
255 -- the constant minInt as unsigned.
256 | otherwise -> pprIntVal i
258 MachInt64 i | code_style -> pprIntVal i -- Same problem with gcc???
259 | otherwise -> ptext SLIT("__int64") <+> integer i
261 MachWord w | code_style -> pprHexVal w
262 | otherwise -> ptext SLIT("__word") <+> integer w
264 MachWord64 w | code_style -> pprHexVal w
265 | otherwise -> ptext SLIT("__word64") <+> integer w
267 MachFloat f | code_style -> ptext SLIT("(StgFloat)") <> rational f
268 | otherwise -> ptext SLIT("__float") <+> rational f
270 MachDouble d | ifaceStyle sty && d < 0 -> parens (rational d)
271 | otherwise -> rational d
273 MachAddr p | code_style -> ptext SLIT("(void*)") <> integer p
274 | otherwise -> ptext SLIT("__addr") <+> integer p
276 MachLitLit s ty | code_style -> ptext s
277 | otherwise -> parens (hsep [ptext SLIT("__litlit"),
281 pprIntVal :: Integer -> SDoc
282 -- Print negative integers with parens to be sure it's unambiguous
283 pprIntVal i | i < 0 = parens (integer i)
284 | otherwise = integer i
286 pprHexVal :: Integer -> SDoc
287 -- Print in C hex format: 0x13fa
288 pprHexVal 0 = ptext SLIT("0x0")
289 pprHexVal w = ptext SLIT("0x") <> go w
292 go w = go quot <> dig
294 (quot,rem) = w `quotRem` 16
295 dig | rem < 10 = char (chr (fromInteger rem + ord '0'))
296 | otherwise = char (chr (fromInteger rem - 10 + ord 'a'))
300 %************************************************************************
304 %************************************************************************
306 Hash values should be zero or a positive integer. No negatives please.
307 (They mess up the UniqFM for some reason.)
310 hashLiteral :: Literal -> Int
311 hashLiteral (MachChar c) = ord c + 1000 -- Keep it out of range of common ints
312 hashLiteral (MachStr s) = hashFS s
313 hashLiteral (MachAddr i) = hashInteger i
314 hashLiteral (MachInt i) = hashInteger i
315 hashLiteral (MachInt64 i) = hashInteger i
316 hashLiteral (MachWord i) = hashInteger i
317 hashLiteral (MachWord64 i) = hashInteger i
318 hashLiteral (MachFloat r) = hashRational r
319 hashLiteral (MachDouble r) = hashRational r
320 hashLiteral (MachLitLit s _) = hashFS s
322 hashRational :: Rational -> Int
323 hashRational r = hashInteger (numerator r)
325 hashInteger :: Integer -> Int
326 hashInteger i = 1 + abs (fromInteger (i `rem` 10000))
327 -- The 1+ is to avoid zero, which is a Bad Number
328 -- since we use * to combine hash values
330 hashFS :: FAST_STRING -> Int
331 hashFS s = IBOX( uniqueOfFS s )