2 % (c) The GRASP/AQUA Project, Glasgow University, 1998
4 \section[Literal]{@Literal@: Machine literals (unboxed, of course)}
8 ( Literal(..) -- Exported to ParseIface
9 , mkMachInt, mkMachWord
10 , mkMachInt64, mkMachWord64
11 , isLitLitLit, maybeLitLit, litSize, litIsDupable,
12 , literalType, literalPrimRep
15 , inIntRange, inWordRange, tARGET_MAX_INT, inCharRange
18 , word2IntLit, int2WordLit
19 , narrow8IntLit, narrow16IntLit, narrow32IntLit
20 , narrow8WordLit, narrow16WordLit, narrow32WordLit
21 , char2IntLit, int2CharLit
22 , float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit
23 , nullAddrLit, float2DoubleLit, double2FloatLit
26 #include "HsVersions.h"
28 import TysPrim ( charPrimTy, addrPrimTy, floatPrimTy, doublePrimTy,
29 intPrimTy, wordPrimTy, int64PrimTy, word64PrimTy
31 import PrimRep ( PrimRep(..) )
32 import TcType ( Type, tcCmpType )
33 import Type ( typePrimRep )
34 import PprType ( pprParendType )
35 import CStrings ( pprFSInCStyle )
41 import Util ( thenCmp )
43 import Ratio ( numerator )
44 import FastString ( uniqueOfFS, lengthFS )
45 import DATA_INT ( Int8, Int16, Int32 )
46 import DATA_WORD ( Word8, Word16, Word32 )
47 import Char ( ord, chr )
52 %************************************************************************
56 %************************************************************************
58 If we're compiling with GHC (and we're not cross-compiling), then we
59 know that minBound and maxBound :: Int are the right values for the
60 target architecture. Otherwise, we assume -2^31 and 2^31-1
61 respectively (which will be wrong on a 64-bit machine).
64 tARGET_MIN_INT, tARGET_MAX_INT, tARGET_MAX_WORD :: Integer
65 #if __GLASGOW_HASKELL__
66 tARGET_MIN_INT = toInteger (minBound :: Int)
67 tARGET_MAX_INT = toInteger (maxBound :: Int)
69 tARGET_MIN_INT = -2147483648
70 tARGET_MAX_INT = 2147483647
72 tARGET_MAX_WORD = (tARGET_MAX_INT * 2) + 1
74 tARGET_MAX_CHAR :: Int
75 tARGET_MAX_CHAR = 0x10ffff
79 %************************************************************************
83 %************************************************************************
85 So-called @Literals@ are {\em either}:
88 An unboxed (``machine'') literal (type: @IntPrim@, @FloatPrim@, etc.),
89 which is presumed to be surrounded by appropriate constructors
90 (@mKINT@, etc.), so that the overall thing makes sense.
92 An Integer, Rational, or String literal whose representation we are
93 {\em uncommitted} about; i.e., the surrounding with constructors,
94 function applications, etc., etc., has not yet been done.
100 -- First the primitive guys
101 MachChar Int -- Char# At least 31 bits
104 | MachAddr Integer -- Whatever this machine thinks is a "pointer"
106 | MachInt Integer -- Int# At least WORD_SIZE_IN_BITS bits
107 | MachInt64 Integer -- Int64# At least 64 bits
108 | MachWord Integer -- Word# At least WORD_SIZE_IN_BITS bits
109 | MachWord64 Integer -- Word64# At least 64 bits
112 | MachDouble Rational
114 -- MachLabel is used (only) for the literal derived from a
115 -- "foreign label" declaration.
116 -- string argument is the name of a symbol. This literal
117 -- refers to the *address* of the label.
118 | MachLabel FastString -- always an Addr#
120 -- lit-lits only work for via-C compilation, hence they
121 -- are deprecated. The string is emitted verbatim into
122 -- the C file, and can therefore be any C expression,
123 -- macro call, #defined constant etc.
124 | MachLitLit FastString Type -- Type might be Addr# or Int# etc
127 Binary instance: must do this manually, because we don't want the type
128 arg of MachLitLit involved.
131 instance Binary Literal where
132 put_ bh (MachChar aa) = do putByte bh 0; put_ bh aa
133 put_ bh (MachStr ab) = do putByte bh 1; put_ bh ab
134 put_ bh (MachAddr ac) = do putByte bh 2; put_ bh ac
135 put_ bh (MachInt ad) = do putByte bh 3; put_ bh ad
136 put_ bh (MachInt64 ae) = do putByte bh 4; put_ bh ae
137 put_ bh (MachWord af) = do putByte bh 5; put_ bh af
138 put_ bh (MachWord64 ag) = do putByte bh 6; put_ bh ag
139 put_ bh (MachFloat ah) = do putByte bh 7; put_ bh ah
140 put_ bh (MachDouble ai) = do putByte bh 8; put_ bh ai
141 put_ bh (MachLabel aj) = do putByte bh 9; put_ bh aj
142 put_ bh (MachLitLit ak _) = do putByte bh 10; put_ bh ak
160 return (MachInt64 ae)
166 return (MachWord64 ag)
169 return (MachFloat ah)
172 return (MachDouble ai)
175 return (MachLabel aj)
178 return (MachLitLit ak (error "MachLitLit: no type"))
182 instance Outputable Literal where
185 instance Show Literal where
186 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 }
192 instance Ord Literal where
193 a <= b = case (a `compare` b) of { LT -> True; EQ -> True; GT -> False }
194 a < b = case (a `compare` b) of { LT -> True; EQ -> False; GT -> False }
195 a >= b = case (a `compare` b) of { LT -> False; EQ -> True; GT -> True }
196 a > b = case (a `compare` b) of { LT -> False; EQ -> False; GT -> True }
197 compare a b = cmpLit a b
204 mkMachInt, mkMachWord, mkMachInt64, mkMachWord64 :: Integer -> Literal
206 mkMachInt x = -- ASSERT2( inIntRange x, integer x )
207 -- Not true: you can write out of range Int# literals
208 -- For example, one can write (intToWord# 0xffff0000) to
209 -- get a particular Word bit-pattern, and there's no other
210 -- convenient way to write such literals, which is why we allow it.
212 mkMachWord x = -- ASSERT2( inWordRange x, integer x )
214 mkMachInt64 x = MachInt64 x
215 mkMachWord64 x = MachWord64 x
217 inIntRange, inWordRange :: Integer -> Bool
218 inIntRange x = x >= tARGET_MIN_INT && x <= tARGET_MAX_INT
219 inWordRange x = x >= 0 && x <= tARGET_MAX_WORD
221 inCharRange :: Int -> Bool
222 inCharRange c = c >= 0 && c <= tARGET_MAX_CHAR
224 isZeroLit :: Literal -> Bool
225 isZeroLit (MachInt 0) = True
226 isZeroLit (MachInt64 0) = True
227 isZeroLit (MachWord 0) = True
228 isZeroLit (MachWord64 0) = True
229 isZeroLit (MachFloat 0) = True
230 isZeroLit (MachDouble 0) = True
231 isZeroLit other = False
237 word2IntLit, int2WordLit,
238 narrow8IntLit, narrow16IntLit, narrow32IntLit,
239 narrow8WordLit, narrow16WordLit, narrow32WordLit,
240 char2IntLit, int2CharLit,
241 float2IntLit, int2FloatLit, double2IntLit, int2DoubleLit,
242 float2DoubleLit, double2FloatLit
243 :: Literal -> Literal
245 word2IntLit (MachWord w)
246 | w > tARGET_MAX_INT = MachInt (w - tARGET_MAX_WORD - 1)
247 | otherwise = MachInt w
249 int2WordLit (MachInt i)
250 | i < 0 = MachWord (1 + tARGET_MAX_WORD + i) -- (-1) ---> tARGET_MAX_WORD
251 | otherwise = MachWord i
253 narrow8IntLit (MachInt i) = MachInt (toInteger (fromInteger i :: Int8))
254 narrow16IntLit (MachInt i) = MachInt (toInteger (fromInteger i :: Int16))
255 narrow32IntLit (MachInt i) = MachInt (toInteger (fromInteger i :: Int32))
256 narrow8WordLit (MachWord w) = MachWord (toInteger (fromInteger w :: Word8))
257 narrow16WordLit (MachWord w) = MachWord (toInteger (fromInteger w :: Word16))
258 narrow32WordLit (MachWord w) = MachWord (toInteger (fromInteger w :: Word32))
260 char2IntLit (MachChar c) = MachInt (toInteger c)
261 int2CharLit (MachInt i) = MachChar (fromInteger i)
263 float2IntLit (MachFloat f) = MachInt (truncate f)
264 int2FloatLit (MachInt i) = MachFloat (fromInteger i)
266 double2IntLit (MachDouble f) = MachInt (truncate f)
267 int2DoubleLit (MachInt i) = MachDouble (fromInteger i)
269 float2DoubleLit (MachFloat f) = MachDouble f
270 double2FloatLit (MachDouble d) = MachFloat d
272 nullAddrLit :: Literal
273 nullAddrLit = MachAddr 0
279 isLitLitLit (MachLitLit _ _) = True
280 isLitLitLit _ = False
282 maybeLitLit (MachLitLit s t) = Just (s,t)
283 maybeLitLit _ = Nothing
285 litIsDupable :: Literal -> Bool
286 -- True if code space does not go bad if we duplicate this literal
287 -- False principally of strings
288 litIsDupable (MachStr _) = False
289 litIsDupable other = True
291 litSize :: Literal -> Int
292 -- Used by CoreUnfold.sizeExpr
293 litSize (MachStr str) = 1 + (lengthFS str `div` 4)
294 -- Every literal has size at least 1, otherwise
296 -- might be too small
303 literalType :: Literal -> Type
304 literalType (MachChar _) = charPrimTy
305 literalType (MachStr _) = addrPrimTy
306 literalType (MachAddr _) = addrPrimTy
307 literalType (MachInt _) = intPrimTy
308 literalType (MachWord _) = wordPrimTy
309 literalType (MachInt64 _) = int64PrimTy
310 literalType (MachWord64 _) = word64PrimTy
311 literalType (MachFloat _) = floatPrimTy
312 literalType (MachDouble _) = doublePrimTy
313 literalType (MachLabel _) = addrPrimTy
314 literalType (MachLitLit _ ty) = ty
318 literalPrimRep :: Literal -> PrimRep
320 literalPrimRep (MachChar _) = CharRep
321 literalPrimRep (MachStr _) = AddrRep -- specifically: "char *"
322 literalPrimRep (MachAddr _) = AddrRep
323 literalPrimRep (MachInt _) = IntRep
324 literalPrimRep (MachWord _) = WordRep
325 literalPrimRep (MachInt64 _) = Int64Rep
326 literalPrimRep (MachWord64 _) = Word64Rep
327 literalPrimRep (MachFloat _) = FloatRep
328 literalPrimRep (MachDouble _) = DoubleRep
329 literalPrimRep (MachLabel _) = AddrRep
330 literalPrimRep (MachLitLit _ ty) = typePrimRep ty
337 cmpLit (MachChar a) (MachChar b) = a `compare` b
338 cmpLit (MachStr a) (MachStr b) = a `compare` b
339 cmpLit (MachAddr a) (MachAddr b) = a `compare` b
340 cmpLit (MachInt a) (MachInt b) = a `compare` b
341 cmpLit (MachWord a) (MachWord b) = a `compare` b
342 cmpLit (MachInt64 a) (MachInt64 b) = a `compare` b
343 cmpLit (MachWord64 a) (MachWord64 b) = a `compare` b
344 cmpLit (MachFloat a) (MachFloat b) = a `compare` b
345 cmpLit (MachDouble a) (MachDouble b) = a `compare` b
346 cmpLit (MachLabel a) (MachLabel b) = a `compare` b
347 cmpLit (MachLitLit a b) (MachLitLit c d) = (a `compare` c) `thenCmp` (b `tcCmpType` d)
348 cmpLit lit1 lit2 | litTag lit1 <# litTag lit2 = LT
351 litTag (MachChar _) = _ILIT(1)
352 litTag (MachStr _) = _ILIT(2)
353 litTag (MachAddr _) = _ILIT(3)
354 litTag (MachInt _) = _ILIT(4)
355 litTag (MachWord _) = _ILIT(5)
356 litTag (MachInt64 _) = _ILIT(6)
357 litTag (MachWord64 _) = _ILIT(7)
358 litTag (MachFloat _) = _ILIT(8)
359 litTag (MachDouble _) = _ILIT(9)
360 litTag (MachLabel _) = _ILIT(10)
361 litTag (MachLitLit _ _) = _ILIT(11)
366 * MachX (i.e. unboxed) things are printed unadornded (e.g. 3, 'a', "foo")
367 exceptions: MachFloat and MachAddr get an initial keyword prefix
371 = getPprStyle $ \ sty ->
373 code_style = codeStyle sty
376 MachChar ch | code_style -> hcat [ptext SLIT("(C_)"), text (show ch)]
377 | otherwise -> pprHsChar ch
379 MachStr s | code_style -> pprFSInCStyle s
380 | otherwise -> pprHsString s
381 -- Warning: printing MachStr in code_style assumes it contains
382 -- only characters '\0'..'\xFF'!
384 MachInt i | code_style && i == tARGET_MIN_INT -> parens (integer (i+1) <> text "-1")
385 -- Avoid a problem whereby gcc interprets
386 -- the constant minInt as unsigned.
387 | otherwise -> pprIntVal i
389 MachInt64 i | code_style -> pprIntVal i -- Same problem with gcc???
390 | otherwise -> ptext SLIT("__int64") <+> integer i
392 MachWord w | code_style -> pprHexVal w
393 | otherwise -> ptext SLIT("__word") <+> integer w
395 MachWord64 w | code_style -> pprHexVal w
396 | otherwise -> ptext SLIT("__word64") <+> integer w
398 MachFloat f | code_style -> ptext SLIT("(StgFloat)") <> code_rational f
399 | otherwise -> ptext SLIT("__float") <+> rational f
401 MachDouble d | code_style -> code_rational d
402 | otherwise -> rational d
404 MachAddr p | code_style -> ptext SLIT("(void*)") <> integer p
405 | otherwise -> ptext SLIT("__addr") <+> integer p
407 MachLabel l | code_style -> ptext SLIT("(&") <> ftext l <> char ')'
408 | otherwise -> ptext SLIT("__label") <+> pprHsString l
410 MachLitLit s ty | code_style -> ftext s
411 | otherwise -> parens (hsep [ptext SLIT("__litlit"),
415 -- negative floating literals in code style need parentheses to avoid
416 -- interacting with surrounding syntax.
417 code_rational d | d < 0 = parens (rational d)
418 | otherwise = rational d
420 pprIntVal :: Integer -> SDoc
421 -- Print negative integers with parens to be sure it's unambiguous
422 pprIntVal i | i < 0 = parens (integer i)
423 | otherwise = integer i
425 pprHexVal :: Integer -> SDoc
426 -- Print in C hex format: 0x13fa
427 pprHexVal 0 = ptext SLIT("0x0")
428 pprHexVal w = ptext SLIT("0x") <> go w
431 go w = go quot <> dig
433 (quot,rem) = w `quotRem` 16
434 dig | rem < 10 = char (chr (fromInteger rem + ord '0'))
435 | otherwise = char (chr (fromInteger rem - 10 + ord 'a'))
439 %************************************************************************
443 %************************************************************************
445 Hash values should be zero or a positive integer. No negatives please.
446 (They mess up the UniqFM for some reason.)
449 hashLiteral :: Literal -> Int
450 hashLiteral (MachChar c) = c + 1000 -- Keep it out of range of common ints
451 hashLiteral (MachStr s) = hashFS s
452 hashLiteral (MachAddr i) = hashInteger i
453 hashLiteral (MachInt i) = hashInteger i
454 hashLiteral (MachInt64 i) = hashInteger i
455 hashLiteral (MachWord i) = hashInteger i
456 hashLiteral (MachWord64 i) = hashInteger i
457 hashLiteral (MachFloat r) = hashRational r
458 hashLiteral (MachDouble r) = hashRational r
459 hashLiteral (MachLabel s) = hashFS s
460 hashLiteral (MachLitLit s _) = hashFS s
462 hashRational :: Rational -> Int
463 hashRational r = hashInteger (numerator r)
465 hashInteger :: Integer -> Int
466 hashInteger i = 1 + abs (fromInteger (i `rem` 10000))
467 -- The 1+ is to avoid zero, which is a Bad Number
468 -- since we use * to combine hash values
470 hashFS :: FastString -> Int
471 hashFS s = iBox (uniqueOfFS s)