2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1998
5 \section[Literal]{@Literal@: Machine literals (unboxed, of course)}
8 {-# LANGUAGE CPP, DeriveDataTypeable #-}
13 Literal
(..) -- Exported to ParseIface
15 -- ** Creating Literals
16 , mkMachInt
, mkMachWord
17 , mkMachInt64
, mkMachWord64
18 , mkMachFloat
, mkMachDouble
19 , mkMachChar
, mkMachString
22 -- ** Operations on Literals
27 -- ** Predicates on Literals and their contents
28 , litIsDupable
, litIsTrivial
, litIsLifted
29 , inIntRange
, inWordRange
, tARGET_MAX_INT
, inCharRange
35 , word2IntLit
, int2WordLit
36 , narrow8IntLit
, narrow16IntLit
, narrow32IntLit
37 , narrow8WordLit
, narrow16WordLit
, narrow32WordLit
38 , char2IntLit
, int2CharLit
39 , float2IntLit
, int2FloatLit
, double2IntLit
, int2DoubleLit
40 , nullAddrLit
, float2DoubleLit
, double2FloatLit
43 #include
"HsVersions.h"
58 import Data
.ByteString
(ByteString
)
62 import Data
.Data
( Data
)
63 import Numeric
( fromRat )
66 ************************************************************************
70 ************************************************************************
73 -- | So-called 'Literal's are one of:
75 -- * An unboxed (/machine/) literal ('MachInt', 'MachFloat', etc.),
76 -- which is presumed to be surrounded by appropriate constructors
77 -- (@Int#@, etc.), so that the overall thing makes sense.
79 -- * The literal derived from the label mentioned in a \"foreign label\"
80 -- declaration ('MachLabel')
83 -- First the primitive guys
84 MachChar
Char -- ^ @Char#@ - at least 31 bits. Create with 'mkMachChar'
86 | MachStr ByteString
-- ^ A string-literal: stored and emitted
87 -- UTF-8 encoded, we'll arrange to decode it
88 -- at runtime. Also emitted with a @'\0'@
89 -- terminator. Create with 'mkMachString'
91 | MachNullAddr
-- ^ The @NULL@ pointer, the only pointer value
92 -- that can be represented as a Literal. Create
95 | MachInt
Integer -- ^ @Int#@ - at least @WORD_SIZE_IN_BITS@ bits. Create with 'mkMachInt'
96 | MachInt64
Integer -- ^ @Int64#@ - at least 64 bits. Create with 'mkMachInt64'
97 | MachWord
Integer -- ^ @Word#@ - at least @WORD_SIZE_IN_BITS@ bits. Create with 'mkMachWord'
98 | MachWord64
Integer -- ^ @Word64#@ - at least 64 bits. Create with 'mkMachWord64'
100 | MachFloat
Rational -- ^ @Float#@. Create with 'mkMachFloat'
101 | MachDouble
Rational -- ^ @Double#@. Create with 'mkMachDouble'
103 | MachLabel FastString
106 -- ^ A label literal. Parameters:
108 -- 1) The name of the symbol mentioned in the declaration
110 -- 2) The size (in bytes) of the arguments
111 -- the label expects. Only applicable with
112 -- @stdcall@ labels. @Just x@ => @\<x\>@ will
113 -- be appended to label name when emitting assembly.
115 | LitInteger
Integer Type
-- ^ Integer literals
116 -- See Note [Integer literals]
120 Note [Integer literals]
121 ~~~~~~~~~~~~~~~~~~~~~~~
122 An Integer literal is represented using, well, an Integer, to make it
123 easier to write RULEs for them. They also contain the Integer type, so
124 that e.g. literalType can return the right Type for them.
126 They only get converted into real Core,
127 mkInteger [c1, c2, .., cn]
128 during the CorePrep phase, although TidyPgm looks ahead at what the
129 core will be, so that it can see whether it involves CAFs.
131 When we initally build an Integer literal, notably when
132 deserialising it from an interface file (see the Binary instance
133 below), we don't have convenient access to the mkInteger Id. So we
134 just use an error thunk, and fill in the real Id when we do tcIfaceLit
141 instance Binary Literal
where
142 put_ bh
(MachChar aa
) = do putByte bh
0; put_ bh aa
143 put_ bh
(MachStr ab
) = do putByte bh
1; put_ bh ab
144 put_ bh
(MachNullAddr
) = do putByte bh
2
145 put_ bh
(MachInt ad
) = do putByte bh
3; put_ bh ad
146 put_ bh
(MachInt64 ae
) = do putByte bh
4; put_ bh ae
147 put_ bh
(MachWord af
) = do putByte bh
5; put_ bh af
148 put_ bh
(MachWord64 ag
) = do putByte bh
6; put_ bh ag
149 put_ bh
(MachFloat ah
) = do putByte bh
7; put_ bh ah
150 put_ bh
(MachDouble ai
) = do putByte bh
8; put_ bh ai
151 put_ bh
(MachLabel aj mb fod
)
156 put_ bh
(LitInteger i _
) = do putByte bh
10; put_ bh i
167 return (MachNullAddr
)
173 return (MachInt64 ae
)
179 return (MachWord64 ag
)
182 return (MachFloat ah
)
185 return (MachDouble ai
)
190 return (MachLabel aj mb fod
)
193 -- See Note [Integer literals]
194 return $ mkLitInteger i
(panic
"Evaluated the place holder for mkInteger")
196 instance Outputable Literal
where
197 ppr lit
= pprLiteral
(\d
-> d
) lit
199 instance Eq Literal
where
200 a
== b
= case (a `
compare` b
) of { EQ
-> True; _
-> False }
201 a
/= b
= case (a `
compare` b
) of { EQ
-> False; _
-> True }
203 instance Ord Literal
where
204 a
<= b
= case (a `
compare` b
) of { LT
-> True; EQ
-> True; GT
-> False }
205 a
< b
= case (a `
compare` b
) of { LT
-> True; EQ
-> False; GT
-> False }
206 a
>= b
= case (a `
compare` b
) of { LT
-> False; EQ
-> True; GT
-> True }
207 a
> b
= case (a `
compare` b
) of { LT
-> False; EQ
-> False; GT
-> True }
208 compare a b
= cmpLit a b
215 -- | Creates a 'Literal' of type @Int#@
216 mkMachInt
:: DynFlags
-> Integer -> Literal
217 mkMachInt dflags x
= ASSERT2
( inIntRange dflags x
, integer x
)
220 -- | Creates a 'Literal' of type @Word#@
221 mkMachWord
:: DynFlags
-> Integer -> Literal
222 mkMachWord dflags x
= ASSERT2
( inWordRange dflags x
, integer x
)
225 -- | Creates a 'Literal' of type @Int64#@
226 mkMachInt64
:: Integer -> Literal
227 mkMachInt64 x
= MachInt64 x
229 -- | Creates a 'Literal' of type @Word64#@
230 mkMachWord64
:: Integer -> Literal
231 mkMachWord64 x
= MachWord64 x
233 -- | Creates a 'Literal' of type @Float#@
234 mkMachFloat
:: Rational -> Literal
235 mkMachFloat
= MachFloat
237 -- | Creates a 'Literal' of type @Double#@
238 mkMachDouble
:: Rational -> Literal
239 mkMachDouble
= MachDouble
241 -- | Creates a 'Literal' of type @Char#@
242 mkMachChar
:: Char -> Literal
243 mkMachChar
= MachChar
245 -- | Creates a 'Literal' of type @Addr#@, which is appropriate for passing to
246 -- e.g. some of the \"error\" functions in GHC.Err such as @GHC.Err.runtimeError@
247 mkMachString
:: String -> Literal
248 -- stored UTF-8 encoded
249 mkMachString s
= MachStr
(fastStringToByteString
$ mkFastString s
)
251 mkLitInteger
:: Integer -> Type
-> Literal
252 mkLitInteger
= LitInteger
254 inIntRange
, inWordRange
:: DynFlags
-> Integer -> Bool
255 inIntRange dflags x
= x
>= tARGET_MIN_INT dflags
&& x
<= tARGET_MAX_INT dflags
256 inWordRange dflags x
= x
>= 0 && x
<= tARGET_MAX_WORD dflags
258 inCharRange
:: Char -> Bool
259 inCharRange c
= c
>= '\0' && c
<= chr tARGET_MAX_CHAR
261 -- | Tests whether the literal represents a zero of whatever type it is
262 isZeroLit
:: Literal
-> Bool
263 isZeroLit
(MachInt
0) = True
264 isZeroLit
(MachInt64
0) = True
265 isZeroLit
(MachWord
0) = True
266 isZeroLit
(MachWord64
0) = True
267 isZeroLit
(MachFloat
0) = True
268 isZeroLit
(MachDouble
0) = True
271 -- | Returns the 'Integer' contained in the 'Literal', for when that makes
272 -- sense, i.e. for 'Char', 'Int', 'Word' and 'LitInteger'.
273 litValue
:: Literal
-> Integer
274 litValue
(MachChar c
) = toInteger $ ord c
275 litValue
(MachInt i
) = i
276 litValue
(MachInt64 i
) = i
277 litValue
(MachWord i
) = i
278 litValue
(MachWord64 i
) = i
279 litValue
(LitInteger i _
) = i
280 litValue l
= pprPanic
"litValue" (ppr l
)
287 narrow8IntLit
, narrow16IntLit
, narrow32IntLit
,
288 narrow8WordLit
, narrow16WordLit
, narrow32WordLit
,
289 char2IntLit
, int2CharLit
,
290 float2IntLit
, int2FloatLit
, double2IntLit
, int2DoubleLit
,
291 float2DoubleLit
, double2FloatLit
292 :: Literal
-> Literal
294 word2IntLit
, int2WordLit
:: DynFlags
-> Literal
-> Literal
295 word2IntLit dflags
(MachWord w
)
296 | w
> tARGET_MAX_INT dflags
= MachInt
(w
- tARGET_MAX_WORD dflags
- 1)
297 |
otherwise = MachInt w
298 word2IntLit _ l
= pprPanic
"word2IntLit" (ppr l
)
300 int2WordLit dflags
(MachInt i
)
301 | i
< 0 = MachWord
(1 + tARGET_MAX_WORD dflags
+ i
) -- (-1) ---> tARGET_MAX_WORD
302 |
otherwise = MachWord i
303 int2WordLit _ l
= pprPanic
"int2WordLit" (ppr l
)
305 narrow8IntLit
(MachInt i
) = MachInt
(toInteger (fromInteger i
:: Int8
))
306 narrow8IntLit l
= pprPanic
"narrow8IntLit" (ppr l
)
307 narrow16IntLit
(MachInt i
) = MachInt
(toInteger (fromInteger i
:: Int16
))
308 narrow16IntLit l
= pprPanic
"narrow16IntLit" (ppr l
)
309 narrow32IntLit
(MachInt i
) = MachInt
(toInteger (fromInteger i
:: Int32
))
310 narrow32IntLit l
= pprPanic
"narrow32IntLit" (ppr l
)
311 narrow8WordLit
(MachWord w
) = MachWord
(toInteger (fromInteger w
:: Word8
))
312 narrow8WordLit l
= pprPanic
"narrow8WordLit" (ppr l
)
313 narrow16WordLit
(MachWord w
) = MachWord
(toInteger (fromInteger w
:: Word16
))
314 narrow16WordLit l
= pprPanic
"narrow16WordLit" (ppr l
)
315 narrow32WordLit
(MachWord w
) = MachWord
(toInteger (fromInteger w
:: Word32
))
316 narrow32WordLit l
= pprPanic
"narrow32WordLit" (ppr l
)
318 char2IntLit
(MachChar c
) = MachInt
(toInteger (ord c
))
319 char2IntLit l
= pprPanic
"char2IntLit" (ppr l
)
320 int2CharLit
(MachInt i
) = MachChar
(chr (fromInteger i
))
321 int2CharLit l
= pprPanic
"int2CharLit" (ppr l
)
323 float2IntLit
(MachFloat f
) = MachInt
(truncate f
)
324 float2IntLit l
= pprPanic
"float2IntLit" (ppr l
)
325 int2FloatLit
(MachInt i
) = MachFloat
(fromInteger i
)
326 int2FloatLit l
= pprPanic
"int2FloatLit" (ppr l
)
328 double2IntLit
(MachDouble f
) = MachInt
(truncate f
)
329 double2IntLit l
= pprPanic
"double2IntLit" (ppr l
)
330 int2DoubleLit
(MachInt i
) = MachDouble
(fromInteger i
)
331 int2DoubleLit l
= pprPanic
"int2DoubleLit" (ppr l
)
333 float2DoubleLit
(MachFloat f
) = MachDouble f
334 float2DoubleLit l
= pprPanic
"float2DoubleLit" (ppr l
)
335 double2FloatLit
(MachDouble d
) = MachFloat d
336 double2FloatLit l
= pprPanic
"double2FloatLit" (ppr l
)
338 nullAddrLit
:: Literal
339 nullAddrLit
= MachNullAddr
346 -- | True if there is absolutely no penalty to duplicating the literal.
347 -- False principally of strings
348 litIsTrivial
:: Literal
-> Bool
349 -- c.f. CoreUtils.exprIsTrivial
350 litIsTrivial
(MachStr _
) = False
351 litIsTrivial
(LitInteger
{}) = False
352 litIsTrivial _
= True
354 -- | True if code space does not go bad if we duplicate this literal
355 -- Currently we treat it just like 'litIsTrivial'
356 litIsDupable
:: DynFlags
-> Literal
-> Bool
357 -- c.f. CoreUtils.exprIsDupable
358 litIsDupable _
(MachStr _
) = False
359 litIsDupable dflags
(LitInteger i _
) = inIntRange dflags i
360 litIsDupable _ _
= True
362 litFitsInChar
:: Literal
-> Bool
363 litFitsInChar
(MachInt i
) = i
>= toInteger (ord minBound)
364 && i
<= toInteger (ord maxBound)
365 litFitsInChar _
= False
367 litIsLifted
:: Literal
-> Bool
368 litIsLifted
(LitInteger
{}) = True
369 litIsLifted _
= False
376 -- | Find the Haskell 'Type' the literal occupies
377 literalType
:: Literal
-> Type
378 literalType MachNullAddr
= addrPrimTy
379 literalType
(MachChar _
) = charPrimTy
380 literalType
(MachStr _
) = addrPrimTy
381 literalType
(MachInt _
) = intPrimTy
382 literalType
(MachWord _
) = wordPrimTy
383 literalType
(MachInt64 _
) = int64PrimTy
384 literalType
(MachWord64 _
) = word64PrimTy
385 literalType
(MachFloat _
) = floatPrimTy
386 literalType
(MachDouble _
) = doublePrimTy
387 literalType
(MachLabel _ _ _
) = addrPrimTy
388 literalType
(LitInteger _ t
) = t
390 absentLiteralOf
:: TyCon
-> Maybe Literal
391 -- Return a literal of the appropriate primtive
392 -- TyCon, to use as a placeholder when it doesn't matter
393 absentLiteralOf tc
= lookupUFM absent_lits
(tyConName tc
)
395 absent_lits
:: UniqFM Literal
396 absent_lits
= listToUFM
[ (addrPrimTyConKey
, MachNullAddr
)
397 , (charPrimTyConKey
, MachChar
'x
')
398 , (intPrimTyConKey
, MachInt
0)
399 , (int64PrimTyConKey
, MachInt64
0)
400 , (floatPrimTyConKey
, MachFloat
0)
401 , (doublePrimTyConKey
, MachDouble
0)
402 , (wordPrimTyConKey
, MachWord
0)
403 , (word64PrimTyConKey
, MachWord64
0) ]
410 cmpLit
:: Literal
-> Literal
-> Ordering
411 cmpLit
(MachChar a
) (MachChar b
) = a `
compare` b
412 cmpLit
(MachStr a
) (MachStr b
) = a `
compare` b
413 cmpLit
(MachNullAddr
) (MachNullAddr
) = EQ
414 cmpLit
(MachInt a
) (MachInt b
) = a `
compare` b
415 cmpLit
(MachWord a
) (MachWord b
) = a `
compare` b
416 cmpLit
(MachInt64 a
) (MachInt64 b
) = a `
compare` b
417 cmpLit
(MachWord64 a
) (MachWord64 b
) = a `
compare` b
418 cmpLit
(MachFloat a
) (MachFloat b
) = a `
compare` b
419 cmpLit
(MachDouble a
) (MachDouble b
) = a `
compare` b
420 cmpLit
(MachLabel a _ _
) (MachLabel b _ _
) = a `
compare` b
421 cmpLit
(LitInteger a _
) (LitInteger b _
) = a `
compare` b
422 cmpLit lit1 lit2 | litTag lit1
< litTag lit2
= LT
425 litTag
:: Literal
-> Int
426 litTag
(MachChar _
) = 1
427 litTag
(MachStr _
) = 2
428 litTag
(MachNullAddr
) = 3
429 litTag
(MachInt _
) = 4
430 litTag
(MachWord _
) = 5
431 litTag
(MachInt64 _
) = 6
432 litTag
(MachWord64 _
) = 7
433 litTag
(MachFloat _
) = 8
434 litTag
(MachDouble _
) = 9
435 litTag
(MachLabel _ _ _
) = 10
436 litTag
(LitInteger
{}) = 11
441 * See Note [Printing of literals in Core]
444 pprLiteral
:: (SDoc
-> SDoc
) -> Literal
-> SDoc
445 pprLiteral _
(MachChar c
) = pprPrimChar c
446 pprLiteral _
(MachStr s
) = pprHsBytes s
447 pprLiteral _
(MachNullAddr
) = text
"__NULL"
448 pprLiteral _
(MachInt i
) = pprPrimInt i
449 pprLiteral _
(MachInt64 i
) = pprPrimInt64 i
450 pprLiteral _
(MachWord w
) = pprPrimWord w
451 pprLiteral _
(MachWord64 w
) = pprPrimWord64 w
452 pprLiteral _
(MachFloat f
) = float
(fromRat f
) <> primFloatSuffix
453 pprLiteral _
(MachDouble d
) = double
(fromRat d
) <> primDoubleSuffix
454 pprLiteral add_par
(LitInteger i _
) = pprIntegerVal add_par i
455 pprLiteral add_par
(MachLabel l mb fod
) = add_par
(text
"__label" <+> b
<+> ppr fod
)
457 Nothing
-> pprHsString l
458 Just x
-> doubleQuotes
(text
(unpackFS l
++ '@':show x
))
460 pprIntegerVal
:: (SDoc
-> SDoc
) -> Integer -> SDoc
461 -- See Note [Printing of literals in Core].
462 pprIntegerVal add_par i | i
< 0 = add_par
(integer i
)
463 |
otherwise = integer i
466 Note [Printing of literals in Core]
467 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
468 The function `add_par` is used to wrap parenthesis around negative integers
469 (`LitInteger`) and labels (`MachLabel`), if they occur in a context requiring
470 an atomic thing (for example function application).
472 Although not all Core literals would be valid Haskell, we are trying to stay
473 as close as possible to Haskell syntax in the printing of Core, to make it
474 easier for a Haskell user to read Core.
477 * We do print parenthesis around negative `LitInteger`, because we print
478 `LitInteger` using plain number literals (no prefix or suffix), and plain
479 number literals in Haskell require parenthesis in contexts like function
480 application (i.e. `1 - -1` is not valid Haskell).
482 * We don't print parenthesis around other (negative) literals, because they
483 aren't needed in GHC/Haskell either (i.e. `1# -# -1#` is accepted by GHC's
486 Literal Output Output if context requires
487 an atom (if different)
488 ------- ------- ----------------------
491 MachNullAddr "__NULL"
499 MachLabel "__label" ... ("__label" ...)