never executed always true always false
    1 {-
    2 (c) The University of Glasgow 2006
    3 (c) The GRASP/AQUA Project, Glasgow University, 1998
    4 
    5 -}
    6 
    7 {-# LANGUAGE DeriveDataTypeable, ScopedTypeVariables #-}
    8 {-# LANGUAGE TypeApplications #-}
    9 {-# LANGUAGE MagicHash #-}
   10 {-# LANGUAGE AllowAmbiguousTypes #-}
   11 
   12 {-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
   13 
   14 -- | Core literals
   15 module GHC.Types.Literal
   16         (
   17         -- * Main data type
   18           Literal(..)           -- Exported to ParseIface
   19         , LitNumType(..)
   20 
   21         -- ** Creating Literals
   22         , mkLitInt, mkLitIntWrap, mkLitIntWrapC, mkLitIntUnchecked
   23         , mkLitWord, mkLitWordWrap, mkLitWordWrapC, mkLitWordUnchecked
   24         , mkLitInt8, mkLitInt8Wrap, mkLitInt8Unchecked
   25         , mkLitWord8, mkLitWord8Wrap, mkLitWord8Unchecked
   26         , mkLitInt16, mkLitInt16Wrap, mkLitInt16Unchecked
   27         , mkLitWord16, mkLitWord16Wrap, mkLitWord16Unchecked
   28         , mkLitInt32, mkLitInt32Wrap, mkLitInt32Unchecked
   29         , mkLitWord32, mkLitWord32Wrap, mkLitWord32Unchecked
   30         , mkLitInt64, mkLitInt64Wrap, mkLitInt64Unchecked
   31         , mkLitWord64, mkLitWord64Wrap, mkLitWord64Unchecked
   32         , mkLitFloat, mkLitDouble
   33         , mkLitChar, mkLitString
   34         , mkLitBigNat
   35         , mkLitNumber, mkLitNumberWrap
   36 
   37         -- ** Operations on Literals
   38         , literalType
   39         , pprLiteral
   40         , litNumIsSigned
   41         , litNumRange
   42         , litNumCheckRange
   43         , litNumWrap
   44         , litNumCoerce
   45         , litNumNarrow
   46         , litNumBitSize
   47         , isMinBound
   48         , isMaxBound
   49 
   50         -- ** Predicates on Literals and their contents
   51         , litIsDupable, litIsTrivial, litIsLifted
   52         , inCharRange
   53         , isZeroLit, isOneLit
   54         , litFitsInChar
   55         , litValue, mapLitValue
   56         , isLitValue_maybe, isLitRubbish
   57 
   58         -- ** Coercions
   59         , narrowInt8Lit, narrowInt16Lit, narrowInt32Lit, narrowInt64Lit
   60         , narrowWord8Lit, narrowWord16Lit, narrowWord32Lit, narrowWord64Lit
   61         , extendIntLit, extendWordLit
   62         , charToIntLit, intToCharLit
   63         , floatToIntLit, intToFloatLit, doubleToIntLit, intToDoubleLit
   64         , nullAddrLit, floatToDoubleLit, doubleToFloatLit
   65         ) where
   66 
   67 import GHC.Prelude
   68 
   69 import GHC.Builtin.Types.Prim
   70 import GHC.Core.Type
   71 import GHC.Utils.Outputable
   72 import GHC.Data.FastString
   73 import GHC.Types.Basic
   74 import GHC.Utils.Binary
   75 import GHC.Settings.Constants
   76 import GHC.Platform
   77 import GHC.Utils.Panic
   78 import GHC.Utils.Encoding
   79 
   80 import Data.ByteString (ByteString)
   81 import Data.Int
   82 import Data.Word
   83 import Data.Char
   84 import Data.Data ( Data )
   85 import GHC.Exts
   86 import Numeric ( fromRat )
   87 
   88 {-
   89 ************************************************************************
   90 *                                                                      *
   91 \subsection{Literals}
   92 *                                                                      *
   93 ************************************************************************
   94 -}
   95 
   96 -- | So-called 'Literal's are one of:
   97 --
   98 -- * An unboxed numeric literal or floating-point literal which is presumed
   99 --   to be surrounded by appropriate constructors (@Int#@, etc.), so that
  100 --   the overall thing makes sense.
  101 --
  102 --   We maintain the invariant that the 'Integer' in the 'LitNumber'
  103 --   constructor is actually in the (possibly target-dependent) range.
  104 --   The mkLit{Int,Word}*Wrap smart constructors ensure this by applying
  105 --   the target machine's wrapping semantics. Use these in situations
  106 --   where you know the wrapping semantics are correct.
  107 --
  108 -- * The literal derived from the label mentioned in a \"foreign label\"
  109 --   declaration ('LitLabel')
  110 --
  111 -- * A 'LitRubbish' to be used in place of values that are never used.
  112 --
  113 -- * A character
  114 -- * A string
  115 -- * The NULL pointer
  116 --
  117 data Literal
  118   = LitChar    Char             -- ^ @Char#@ - at least 31 bits. Create with
  119                                 -- 'mkLitChar'
  120 
  121   | LitNumber !LitNumType !Integer
  122                                 -- ^ Any numeric literal that can be
  123                                 -- internally represented with an Integer.
  124 
  125   | LitString !ByteString       -- ^ A string-literal: stored and emitted
  126                                 -- UTF-8 encoded, we'll arrange to decode it
  127                                 -- at runtime.  Also emitted with a @\'\\0\'@
  128                                 -- terminator. Create with 'mkLitString'
  129 
  130   | LitNullAddr                 -- ^ The @NULL@ pointer, the only pointer value
  131                                 -- that can be represented as a Literal. Create
  132                                 -- with 'nullAddrLit'
  133 
  134   | LitRubbish Type             -- ^ A nonsense value of the given
  135                                 -- representation. See Note [Rubbish literals].
  136                                 --
  137                                 -- The Type argument, rr, is of kind RuntimeRep.
  138                                 -- The type of the literal is forall (a:TYPE rr). a
  139                                 --
  140                                 -- INVARIANT: the Type has no free variables
  141                                 --    and so substitution etc can ignore it
  142                                 --
  143 
  144   | LitFloat   Rational         -- ^ @Float#@. Create with 'mkLitFloat'
  145   | LitDouble  Rational         -- ^ @Double#@. Create with 'mkLitDouble'
  146 
  147   | LitLabel   FastString (Maybe Int) FunctionOrData
  148                                 -- ^ A label literal. Parameters:
  149                                 --
  150                                 -- 1) The name of the symbol mentioned in the
  151                                 --    declaration
  152                                 --
  153                                 -- 2) The size (in bytes) of the arguments
  154                                 --    the label expects. Only applicable with
  155                                 --    @stdcall@ labels. @Just x@ => @\<x\>@ will
  156                                 --    be appended to label name when emitting
  157                                 --    assembly.
  158                                 --
  159                                 -- 3) Flag indicating whether the symbol
  160                                 --    references a function or a data
  161   deriving Data
  162 
  163 -- | Numeric literal type
  164 data LitNumType
  165   = LitNumBigNat  -- ^ @Bignat@ (see Note [BigNum literals])
  166   | LitNumInt     -- ^ @Int#@ - according to target machine
  167   | LitNumInt8    -- ^ @Int8#@ - exactly 8 bits
  168   | LitNumInt16   -- ^ @Int16#@ - exactly 16 bits
  169   | LitNumInt32   -- ^ @Int32#@ - exactly 32 bits
  170   | LitNumInt64   -- ^ @Int64#@ - exactly 64 bits
  171   | LitNumWord    -- ^ @Word#@ - according to target machine
  172   | LitNumWord8   -- ^ @Word8#@ - exactly 8 bits
  173   | LitNumWord16  -- ^ @Word16#@ - exactly 16 bits
  174   | LitNumWord32  -- ^ @Word32#@ - exactly 32 bits
  175   | LitNumWord64  -- ^ @Word64#@ - exactly 64 bits
  176   deriving (Data,Enum,Eq,Ord)
  177 
  178 -- | Indicate if a numeric literal type supports negative numbers
  179 litNumIsSigned :: LitNumType -> Bool
  180 litNumIsSigned nt = case nt of
  181   LitNumBigNat  -> False
  182   LitNumInt     -> True
  183   LitNumInt8    -> True
  184   LitNumInt16   -> True
  185   LitNumInt32   -> True
  186   LitNumInt64   -> True
  187   LitNumWord    -> False
  188   LitNumWord8   -> False
  189   LitNumWord16  -> False
  190   LitNumWord32  -> False
  191   LitNumWord64  -> False
  192 
  193 -- | Number of bits
  194 litNumBitSize :: Platform -> LitNumType -> Maybe Word
  195 litNumBitSize platform nt = case nt of
  196   LitNumBigNat  -> Nothing
  197   LitNumInt     -> Just (fromIntegral (platformWordSizeInBits platform))
  198   LitNumInt8    -> Just 8
  199   LitNumInt16   -> Just 16
  200   LitNumInt32   -> Just 32
  201   LitNumInt64   -> Just 64
  202   LitNumWord    -> Just (fromIntegral (platformWordSizeInBits platform))
  203   LitNumWord8   -> Just 8
  204   LitNumWord16  -> Just 16
  205   LitNumWord32  -> Just 32
  206   LitNumWord64  -> Just 64
  207 
  208 instance Binary LitNumType where
  209    put_ bh numTyp = putByte bh (fromIntegral (fromEnum numTyp))
  210    get bh = do
  211       h <- getByte bh
  212       return (toEnum (fromIntegral h))
  213 
  214 {-
  215 Note [BigNum literals]
  216 ~~~~~~~~~~~~~~~~~~~~~~
  217 GHC supports 2 kinds of arbitrary precision numbers (a.k.a BigNum):
  218 
  219    * data Natural = NS Word# | NB BigNat#
  220 
  221    * data Integer = IS Int# | IN BigNat# | IP BigNat#
  222 
  223 In the past, we had Core constructors to represent Integer and Natural literals.
  224 These literals were then lowered into their real Core representation only in
  225 Core prep. The issue with this approach is that literals have two
  226 representations and we have to ensure that we handle them the same everywhere
  227 (in every optimisation, etc.).
  228 
  229 For example (0 :: Integer) was representable in Core with both:
  230 
  231     Lit (LitNumber LitNumInteger 0)                          -- literal
  232     App (Var integerISDataCon) (Lit (LitNumber LitNumInt 0)) -- real representation
  233 
  234 Nowadays we always use the real representation for Integer and Natural literals.
  235 However we still have two representations for BigNat# literals. BigNat# literals
  236 are still lowered in Core prep into a call to a constructor function (BigNat# is
  237 ByteArray# and we don't have ByteArray# literals yet so we have to build them at
  238 runtime).
  239 
  240 Note [String literals]
  241 ~~~~~~~~~~~~~~~~~~~~~~
  242 String literals are UTF-8 encoded and stored into ByteStrings in the following
  243 ASTs: Haskell, Core, Stg, Cmm. TH can also emit ByteString based string literals
  244 with the BytesPrimL constructor (see #14741).
  245 
  246 It wasn't true before as [Word8] was used in Cmm AST and in TH which was quite
  247 bad for performance with large strings (see #16198 and #14741).
  248 
  249 To include string literals into output objects, the assembler code generator has
  250 to embed the UTF-8 encoded binary blob. See Note [Embedding large binary blobs]
  251 for more details.
  252 
  253 -}
  254 
  255 instance Binary Literal where
  256     put_ bh (LitChar aa)     = do putByte bh 0; put_ bh aa
  257     put_ bh (LitString ab)   = do putByte bh 1; put_ bh ab
  258     put_ bh (LitNullAddr)    = putByte bh 2
  259     put_ bh (LitFloat ah)    = do putByte bh 3; put_ bh ah
  260     put_ bh (LitDouble ai)   = do putByte bh 4; put_ bh ai
  261     put_ bh (LitLabel aj mb fod)
  262         = do putByte bh 5
  263              put_ bh aj
  264              put_ bh mb
  265              put_ bh fod
  266     put_ bh (LitNumber nt i)
  267         = do putByte bh 6
  268              put_ bh nt
  269              put_ bh i
  270     put_ _ (LitRubbish b) = pprPanic "Binary LitRubbish" (ppr b)
  271      -- We use IfaceLitRubbish; see Note [Rubbish literals], item (6)
  272 
  273     get bh = do
  274             h <- getByte bh
  275             case h of
  276               0 -> do
  277                     aa <- get bh
  278                     return (LitChar aa)
  279               1 -> do
  280                     ab <- get bh
  281                     return (LitString ab)
  282               2 -> return (LitNullAddr)
  283               3 -> do
  284                     ah <- get bh
  285                     return (LitFloat ah)
  286               4 -> do
  287                     ai <- get bh
  288                     return (LitDouble ai)
  289               5 -> do
  290                     aj <- get bh
  291                     mb <- get bh
  292                     fod <- get bh
  293                     return (LitLabel aj mb fod)
  294               6 -> do
  295                     nt <- get bh
  296                     i  <- get bh
  297                     return (LitNumber nt i)
  298               _ -> pprPanic "Binary:Literal" (int (fromIntegral h))
  299 
  300 instance Outputable Literal where
  301     ppr = pprLiteral id
  302 
  303 instance Eq Literal where
  304     a == b = compare a b == EQ
  305 
  306 -- | Needed for the @Ord@ instance of 'AltCon', which in turn is needed in
  307 -- 'GHC.Data.TrieMap.CoreMap'.
  308 instance Ord Literal where
  309     compare = cmpLit
  310 
  311 {-
  312         Construction
  313         ~~~~~~~~~~~~
  314 -}
  315 
  316 {- Note [Word/Int underflow/overflow]
  317 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  318 According to the Haskell Report 2010 (Sections 18.1 and 23.1 about signed and
  319 unsigned integral types): "All arithmetic is performed modulo 2^n, where n is
  320 the number of bits in the type."
  321 
  322 GHC stores Word# and Int# constant values as Integer. Core optimizations such
  323 as constant folding must ensure that the Integer value remains in the valid
  324 target Word/Int range (see #13172). The following functions are used to
  325 ensure this.
  326 
  327 Note that we *don't* warn the user about overflow. It's not done at runtime
  328 either, and compilation of completely harmless things like
  329    ((124076834 :: Word32) + (2147483647 :: Word32))
  330 doesn't yield a warning. Instead we simply squash the value into the *target*
  331 Int/Word range.
  332 -}
  333 
  334 -- | Make a literal number using wrapping semantics if the value is out of
  335 -- bound.
  336 mkLitNumberWrap :: Platform -> LitNumType -> Integer -> Literal
  337 mkLitNumberWrap platform nt i = case nt of
  338   LitNumInt -> case platformWordSize platform of
  339     PW4 -> wrap @Int32
  340     PW8 -> wrap @Int64
  341   LitNumWord -> case platformWordSize platform of
  342     PW4 -> wrap @Word32
  343     PW8 -> wrap @Word64
  344   LitNumInt8    -> wrap @Int8
  345   LitNumInt16   -> wrap @Int16
  346   LitNumInt32   -> wrap @Int32
  347   LitNumInt64   -> wrap @Int64
  348   LitNumWord8   -> wrap @Word8
  349   LitNumWord16  -> wrap @Word16
  350   LitNumWord32  -> wrap @Word32
  351   LitNumWord64  -> wrap @Word64
  352   LitNumBigNat
  353     | i < 0     -> panic "mkLitNumberWrap: trying to create a negative BigNat"
  354     | otherwise -> LitNumber nt i
  355   where
  356     wrap :: forall a. (Integral a, Num a) => Literal
  357     wrap = LitNumber nt (toInteger (fromIntegral i :: a))
  358 
  359 -- | Wrap a literal number according to its type using wrapping semantics.
  360 litNumWrap :: Platform -> Literal -> Literal
  361 litNumWrap platform (LitNumber nt i) = mkLitNumberWrap platform nt i
  362 litNumWrap _        l                = pprPanic "litNumWrap" (ppr l)
  363 
  364 -- | Coerce a literal number into another using wrapping semantics.
  365 litNumCoerce :: LitNumType -> Platform -> Literal -> Literal
  366 litNumCoerce pt platform (LitNumber _nt i) = mkLitNumberWrap platform pt i
  367 litNumCoerce _  _        l                 = pprPanic "litNumWrapCoerce: not a number" (ppr l)
  368 
  369 -- | Narrow a literal number by converting it into another number type and then
  370 -- converting it back to its original type.
  371 litNumNarrow :: LitNumType -> Platform -> Literal -> Literal
  372 litNumNarrow pt platform (LitNumber nt i)
  373    = case mkLitNumberWrap platform pt i of
  374       LitNumber _ j -> mkLitNumberWrap platform nt j
  375       l             -> pprPanic "litNumNarrow: got invalid literal" (ppr l)
  376 litNumNarrow _ _ l = pprPanic "litNumNarrow: invalid literal" (ppr l)
  377 
  378 
  379 -- | Check that a given number is in the range of a numeric literal
  380 litNumCheckRange :: Platform -> LitNumType -> Integer -> Bool
  381 litNumCheckRange platform nt i =
  382     maybe True (i >=) m_lower &&
  383     maybe True (i <=) m_upper
  384   where
  385     (m_lower, m_upper) = litNumRange platform nt
  386 
  387 -- | Get the literal range
  388 litNumRange :: Platform -> LitNumType -> (Maybe Integer, Maybe Integer)
  389 litNumRange platform nt = case nt of
  390      LitNumInt     -> (Just (platformMinInt platform), Just (platformMaxInt platform))
  391      LitNumWord    -> (Just 0, Just (platformMaxWord platform))
  392      LitNumInt8    -> bounded_range @Int8
  393      LitNumInt16   -> bounded_range @Int16
  394      LitNumInt32   -> bounded_range @Int32
  395      LitNumInt64   -> bounded_range @Int64
  396      LitNumWord8   -> bounded_range @Word8
  397      LitNumWord16  -> bounded_range @Word16
  398      LitNumWord32  -> bounded_range @Word32
  399      LitNumWord64  -> bounded_range @Word64
  400      LitNumBigNat  -> (Just 0, Nothing)
  401   where
  402     bounded_range :: forall a . (Integral a, Bounded a) => (Maybe Integer,Maybe Integer)
  403     bounded_range = case boundedRange @a of
  404       (mi,ma) -> (Just mi, Just ma)
  405 
  406 -- | Create a numeric 'Literal' of the given type
  407 mkLitNumber :: Platform -> LitNumType -> Integer -> Literal
  408 mkLitNumber platform nt i =
  409   assertPpr (litNumCheckRange platform nt i) (integer i)
  410   (LitNumber nt i)
  411 
  412 -- | Creates a 'Literal' of type @Int#@
  413 mkLitInt :: Platform -> Integer -> Literal
  414 mkLitInt platform x = assertPpr (platformInIntRange platform x) (integer x)
  415                        (mkLitIntUnchecked x)
  416 
  417 -- | Creates a 'Literal' of type @Int#@.
  418 --   If the argument is out of the (target-dependent) range, it is wrapped.
  419 --   See Note [Word/Int underflow/overflow]
  420 mkLitIntWrap :: Platform -> Integer -> Literal
  421 mkLitIntWrap platform i = mkLitNumberWrap platform LitNumInt i
  422 
  423 -- | Creates a 'Literal' of type @Int#@ without checking its range.
  424 mkLitIntUnchecked :: Integer -> Literal
  425 mkLitIntUnchecked i = LitNumber LitNumInt i
  426 
  427 -- | Creates a 'Literal' of type @Int#@, as well as a 'Bool'ean flag indicating
  428 --   overflow. That is, if the argument is out of the (target-dependent) range
  429 --   the argument is wrapped and the overflow flag will be set.
  430 --   See Note [Word/Int underflow/overflow]
  431 mkLitIntWrapC :: Platform -> Integer -> (Literal, Bool)
  432 mkLitIntWrapC platform i = (n, i /= i')
  433   where
  434     n@(LitNumber _ i') = mkLitIntWrap platform i
  435 
  436 -- | Creates a 'Literal' of type @Word#@
  437 mkLitWord :: Platform -> Integer -> Literal
  438 mkLitWord platform x = assertPpr (platformInWordRange platform x) (integer x)
  439                         (mkLitWordUnchecked x)
  440 
  441 -- | Creates a 'Literal' of type @Word#@.
  442 --   If the argument is out of the (target-dependent) range, it is wrapped.
  443 --   See Note [Word/Int underflow/overflow]
  444 mkLitWordWrap :: Platform -> Integer -> Literal
  445 mkLitWordWrap platform i = mkLitNumberWrap platform LitNumWord i
  446 
  447 -- | Creates a 'Literal' of type @Word#@ without checking its range.
  448 mkLitWordUnchecked :: Integer -> Literal
  449 mkLitWordUnchecked i = LitNumber LitNumWord i
  450 
  451 -- | Creates a 'Literal' of type @Word#@, as well as a 'Bool'ean flag indicating
  452 --   carry. That is, if the argument is out of the (target-dependent) range
  453 --   the argument is wrapped and the carry flag will be set.
  454 --   See Note [Word/Int underflow/overflow]
  455 mkLitWordWrapC :: Platform -> Integer -> (Literal, Bool)
  456 mkLitWordWrapC platform i = (n, i /= i')
  457   where
  458     n@(LitNumber _ i') = mkLitWordWrap platform i
  459 
  460 -- | Creates a 'Literal' of type @Int8#@
  461 mkLitInt8 :: Integer -> Literal
  462 mkLitInt8  x = assertPpr (inBoundedRange @Int8 x) (integer x) (mkLitInt8Unchecked x)
  463 
  464 -- | Creates a 'Literal' of type @Int8#@.
  465 --   If the argument is out of the range, it is wrapped.
  466 mkLitInt8Wrap :: Integer -> Literal
  467 mkLitInt8Wrap i = mkLitInt8Unchecked (toInteger (fromIntegral i :: Int8))
  468 
  469 -- | Creates a 'Literal' of type @Int8#@ without checking its range.
  470 mkLitInt8Unchecked :: Integer -> Literal
  471 mkLitInt8Unchecked i = LitNumber LitNumInt8 i
  472 
  473 -- | Creates a 'Literal' of type @Word8#@
  474 mkLitWord8 :: Integer -> Literal
  475 mkLitWord8 x = assertPpr (inBoundedRange @Word8 x) (integer x) (mkLitWord8Unchecked x)
  476 
  477 -- | Creates a 'Literal' of type @Word8#@.
  478 --   If the argument is out of the range, it is wrapped.
  479 mkLitWord8Wrap :: Integer -> Literal
  480 mkLitWord8Wrap i = mkLitWord8Unchecked (toInteger (fromIntegral i :: Word8))
  481 
  482 -- | Creates a 'Literal' of type @Word8#@ without checking its range.
  483 mkLitWord8Unchecked :: Integer -> Literal
  484 mkLitWord8Unchecked i = LitNumber LitNumWord8 i
  485 
  486 -- | Creates a 'Literal' of type @Int16#@
  487 mkLitInt16 :: Integer -> Literal
  488 mkLitInt16  x = assertPpr (inBoundedRange @Int16 x) (integer x) (mkLitInt16Unchecked x)
  489 
  490 -- | Creates a 'Literal' of type @Int16#@.
  491 --   If the argument is out of the range, it is wrapped.
  492 mkLitInt16Wrap :: Integer -> Literal
  493 mkLitInt16Wrap i = mkLitInt16Unchecked (toInteger (fromIntegral i :: Int16))
  494 
  495 -- | Creates a 'Literal' of type @Int16#@ without checking its range.
  496 mkLitInt16Unchecked :: Integer -> Literal
  497 mkLitInt16Unchecked i = LitNumber LitNumInt16 i
  498 
  499 -- | Creates a 'Literal' of type @Word16#@
  500 mkLitWord16 :: Integer -> Literal
  501 mkLitWord16 x = assertPpr (inBoundedRange @Word16 x) (integer x) (mkLitWord16Unchecked x)
  502 
  503 -- | Creates a 'Literal' of type @Word16#@.
  504 --   If the argument is out of the range, it is wrapped.
  505 mkLitWord16Wrap :: Integer -> Literal
  506 mkLitWord16Wrap i = mkLitWord16Unchecked (toInteger (fromIntegral i :: Word16))
  507 
  508 -- | Creates a 'Literal' of type @Word16#@ without checking its range.
  509 mkLitWord16Unchecked :: Integer -> Literal
  510 mkLitWord16Unchecked i = LitNumber LitNumWord16 i
  511 
  512 -- | Creates a 'Literal' of type @Int32#@
  513 mkLitInt32 :: Integer -> Literal
  514 mkLitInt32  x = assertPpr (inBoundedRange @Int32 x) (integer x) (mkLitInt32Unchecked x)
  515 
  516 -- | Creates a 'Literal' of type @Int32#@.
  517 --   If the argument is out of the range, it is wrapped.
  518 mkLitInt32Wrap :: Integer -> Literal
  519 mkLitInt32Wrap i = mkLitInt32Unchecked (toInteger (fromIntegral i :: Int32))
  520 
  521 -- | Creates a 'Literal' of type @Int32#@ without checking its range.
  522 mkLitInt32Unchecked :: Integer -> Literal
  523 mkLitInt32Unchecked i = LitNumber LitNumInt32 i
  524 
  525 -- | Creates a 'Literal' of type @Word32#@
  526 mkLitWord32 :: Integer -> Literal
  527 mkLitWord32 x = assertPpr (inBoundedRange @Word32 x) (integer x) (mkLitWord32Unchecked x)
  528 
  529 -- | Creates a 'Literal' of type @Word32#@.
  530 --   If the argument is out of the range, it is wrapped.
  531 mkLitWord32Wrap :: Integer -> Literal
  532 mkLitWord32Wrap i = mkLitWord32Unchecked (toInteger (fromIntegral i :: Word32))
  533 
  534 -- | Creates a 'Literal' of type @Word32#@ without checking its range.
  535 mkLitWord32Unchecked :: Integer -> Literal
  536 mkLitWord32Unchecked i = LitNumber LitNumWord32 i
  537 
  538 -- | Creates a 'Literal' of type @Int64#@
  539 mkLitInt64 :: Integer -> Literal
  540 mkLitInt64  x = assertPpr (inBoundedRange @Int64 x) (integer x) (mkLitInt64Unchecked x)
  541 
  542 -- | Creates a 'Literal' of type @Int64#@.
  543 --   If the argument is out of the range, it is wrapped.
  544 mkLitInt64Wrap :: Integer -> Literal
  545 mkLitInt64Wrap i = mkLitInt64Unchecked (toInteger (fromIntegral i :: Int64))
  546 
  547 -- | Creates a 'Literal' of type @Int64#@ without checking its range.
  548 mkLitInt64Unchecked :: Integer -> Literal
  549 mkLitInt64Unchecked i = LitNumber LitNumInt64 i
  550 
  551 -- | Creates a 'Literal' of type @Word64#@
  552 mkLitWord64 :: Integer -> Literal
  553 mkLitWord64 x = assertPpr (inBoundedRange @Word64 x) (integer x) (mkLitWord64Unchecked x)
  554 
  555 -- | Creates a 'Literal' of type @Word64#@.
  556 --   If the argument is out of the range, it is wrapped.
  557 mkLitWord64Wrap :: Integer -> Literal
  558 mkLitWord64Wrap i = mkLitWord64Unchecked (toInteger (fromIntegral i :: Word64))
  559 
  560 -- | Creates a 'Literal' of type @Word64#@ without checking its range.
  561 mkLitWord64Unchecked :: Integer -> Literal
  562 mkLitWord64Unchecked i = LitNumber LitNumWord64 i
  563 
  564 -- | Creates a 'Literal' of type @Float#@
  565 mkLitFloat :: Rational -> Literal
  566 mkLitFloat = LitFloat
  567 
  568 -- | Creates a 'Literal' of type @Double#@
  569 mkLitDouble :: Rational -> Literal
  570 mkLitDouble = LitDouble
  571 
  572 -- | Creates a 'Literal' of type @Char#@
  573 mkLitChar :: Char -> Literal
  574 mkLitChar = LitChar
  575 
  576 -- | Creates a 'Literal' of type @Addr#@, which is appropriate for passing to
  577 -- e.g. some of the \"error\" functions in GHC.Err such as @GHC.Err.runtimeError@
  578 mkLitString :: String -> Literal
  579 -- stored UTF-8 encoded
  580 mkLitString [] = LitString mempty
  581 mkLitString s  = LitString (utf8EncodeString s)
  582 
  583 mkLitBigNat :: Integer -> Literal
  584 mkLitBigNat x = assertPpr (x >= 0) (integer x)
  585                     (LitNumber LitNumBigNat x)
  586 
  587 isLitRubbish :: Literal -> Bool
  588 isLitRubbish (LitRubbish {}) = True
  589 isLitRubbish _               = False
  590 
  591 inBoundedRange :: forall a. (Bounded a, Integral a) => Integer -> Bool
  592 inBoundedRange x  = x >= toInteger (minBound :: a) &&
  593                     x <= toInteger (maxBound :: a)
  594 
  595 boundedRange :: forall a. (Bounded a, Integral a) => (Integer,Integer)
  596 boundedRange = (toInteger (minBound :: a), toInteger (maxBound :: a))
  597 
  598 isMinBound :: Platform -> Literal -> Bool
  599 isMinBound _        (LitChar c)        = c == minBound
  600 isMinBound platform (LitNumber nt i)   = case nt of
  601    LitNumInt     -> i == platformMinInt platform
  602    LitNumInt8    -> i == toInteger (minBound :: Int8)
  603    LitNumInt16   -> i == toInteger (minBound :: Int16)
  604    LitNumInt32   -> i == toInteger (minBound :: Int32)
  605    LitNumInt64   -> i == toInteger (minBound :: Int64)
  606    LitNumWord    -> i == 0
  607    LitNumWord8   -> i == 0
  608    LitNumWord16  -> i == 0
  609    LitNumWord32  -> i == 0
  610    LitNumWord64  -> i == 0
  611    LitNumBigNat  -> i == 0
  612 isMinBound _        _                  = False
  613 
  614 isMaxBound :: Platform -> Literal -> Bool
  615 isMaxBound _        (LitChar c)        = c == maxBound
  616 isMaxBound platform (LitNumber nt i)   = case nt of
  617    LitNumInt     -> i == platformMaxInt platform
  618    LitNumInt8    -> i == toInteger (maxBound :: Int8)
  619    LitNumInt16   -> i == toInteger (maxBound :: Int16)
  620    LitNumInt32   -> i == toInteger (maxBound :: Int32)
  621    LitNumInt64   -> i == toInteger (maxBound :: Int64)
  622    LitNumWord    -> i == platformMaxWord platform
  623    LitNumWord8   -> i == toInteger (maxBound :: Word8)
  624    LitNumWord16  -> i == toInteger (maxBound :: Word16)
  625    LitNumWord32  -> i == toInteger (maxBound :: Word32)
  626    LitNumWord64  -> i == toInteger (maxBound :: Word64)
  627    LitNumBigNat  -> False
  628 isMaxBound _        _                  = False
  629 
  630 inCharRange :: Char -> Bool
  631 inCharRange c =  c >= '\0' && c <= chr tARGET_MAX_CHAR
  632 
  633 -- | Tests whether the literal represents a zero of whatever type it is
  634 isZeroLit :: Literal -> Bool
  635 isZeroLit (LitNumber _ 0) = True
  636 isZeroLit (LitFloat  0)   = True
  637 isZeroLit (LitDouble 0)   = True
  638 isZeroLit _               = False
  639 
  640 -- | Tests whether the literal represents a one of whatever type it is
  641 isOneLit :: Literal -> Bool
  642 isOneLit (LitNumber _ 1) = True
  643 isOneLit (LitFloat  1)   = True
  644 isOneLit (LitDouble 1)   = True
  645 isOneLit _               = False
  646 
  647 -- | Returns the 'Integer' contained in the 'Literal', for when that makes
  648 -- sense, i.e. for 'Char' and numbers.
  649 litValue  :: Literal -> Integer
  650 litValue l = case isLitValue_maybe l of
  651    Just x  -> x
  652    Nothing -> pprPanic "litValue" (ppr l)
  653 
  654 -- | Returns the 'Integer' contained in the 'Literal', for when that makes
  655 -- sense, i.e. for 'Char' and numbers.
  656 isLitValue_maybe  :: Literal -> Maybe Integer
  657 isLitValue_maybe (LitChar   c)     = Just $ toInteger $ ord c
  658 isLitValue_maybe (LitNumber _ i)   = Just i
  659 isLitValue_maybe _                 = Nothing
  660 
  661 -- | Apply a function to the 'Integer' contained in the 'Literal', for when that
  662 -- makes sense, e.g. for 'Char' and numbers.
  663 -- For fixed-size integral literals, the result will be wrapped in accordance
  664 -- with the semantics of the target type.
  665 -- See Note [Word/Int underflow/overflow]
  666 mapLitValue  :: Platform -> (Integer -> Integer) -> Literal -> Literal
  667 mapLitValue _        f (LitChar   c)      = mkLitChar (fchar c)
  668    where fchar = chr . fromInteger . f . toInteger . ord
  669 mapLitValue platform f (LitNumber nt i)   = mkLitNumberWrap platform nt (f i)
  670 mapLitValue _        _ l                  = pprPanic "mapLitValue" (ppr l)
  671 
  672 {-
  673         Coercions
  674         ~~~~~~~~~
  675 -}
  676 
  677 charToIntLit, intToCharLit,
  678   floatToIntLit, intToFloatLit,
  679   doubleToIntLit, intToDoubleLit,
  680   floatToDoubleLit, doubleToFloatLit
  681   :: Literal -> Literal
  682 
  683 -- | Narrow a literal number (unchecked result range)
  684 narrowLit' :: forall a. Integral a => LitNumType -> Literal -> Literal
  685 narrowLit' nt' (LitNumber _ i)  = LitNumber nt' (toInteger (fromInteger i :: a))
  686 narrowLit' _   l                = pprPanic "narrowLit" (ppr l)
  687 
  688 narrowInt8Lit, narrowInt16Lit, narrowInt32Lit, narrowInt64Lit,
  689   narrowWord8Lit, narrowWord16Lit, narrowWord32Lit, narrowWord64Lit :: Literal -> Literal
  690 narrowInt8Lit   = narrowLit' @Int8   LitNumInt8
  691 narrowInt16Lit  = narrowLit' @Int16  LitNumInt16
  692 narrowInt32Lit  = narrowLit' @Int32  LitNumInt32
  693 narrowInt64Lit  = narrowLit' @Int64  LitNumInt64
  694 narrowWord8Lit  = narrowLit' @Word8  LitNumWord8
  695 narrowWord16Lit = narrowLit' @Word16 LitNumWord16
  696 narrowWord32Lit = narrowLit' @Word32 LitNumWord32
  697 narrowWord64Lit = narrowLit' @Word64 LitNumWord64
  698 
  699 -- | Extend a fixed-width literal (e.g. 'Int16#') to a word-sized literal (e.g.
  700 -- 'Int#').
  701 extendWordLit, extendIntLit :: Platform -> Literal -> Literal
  702 extendWordLit platform (LitNumber _nt i)  = mkLitWord platform i
  703 extendWordLit _platform l                 = pprPanic "extendWordLit" (ppr l)
  704 extendIntLit  platform (LitNumber _nt i)  = mkLitInt platform i
  705 extendIntLit  _platform l                 = pprPanic "extendIntLit" (ppr l)
  706 
  707 charToIntLit (LitChar c)       = mkLitIntUnchecked (toInteger (ord c))
  708 charToIntLit l                 = pprPanic "charToIntLit" (ppr l)
  709 intToCharLit (LitNumber _ i)   = LitChar (chr (fromInteger i))
  710 intToCharLit l                 = pprPanic "intToCharLit" (ppr l)
  711 
  712 floatToIntLit (LitFloat f)      = mkLitIntUnchecked (truncate f)
  713 floatToIntLit l                 = pprPanic "floatToIntLit" (ppr l)
  714 intToFloatLit (LitNumber _ i)   = LitFloat (fromInteger i)
  715 intToFloatLit l                 = pprPanic "intToFloatLit" (ppr l)
  716 
  717 doubleToIntLit (LitDouble f)     = mkLitIntUnchecked (truncate f)
  718 doubleToIntLit l                 = pprPanic "doubleToIntLit" (ppr l)
  719 intToDoubleLit (LitNumber _ i)   = LitDouble (fromInteger i)
  720 intToDoubleLit l                 = pprPanic "intToDoubleLit" (ppr l)
  721 
  722 floatToDoubleLit (LitFloat  f) = LitDouble f
  723 floatToDoubleLit l             = pprPanic "floatToDoubleLit" (ppr l)
  724 doubleToFloatLit (LitDouble d) = LitFloat  d
  725 doubleToFloatLit l             = pprPanic "doubleToFloatLit" (ppr l)
  726 
  727 nullAddrLit :: Literal
  728 nullAddrLit = LitNullAddr
  729 
  730 {-
  731         Predicates
  732         ~~~~~~~~~~
  733 -}
  734 
  735 -- | True if there is absolutely no penalty to duplicating the literal.
  736 -- False principally of strings.
  737 --
  738 -- "Why?", you say? I'm glad you asked. Well, for one duplicating strings would
  739 -- blow up code sizes. Not only this, it's also unsafe.
  740 --
  741 -- Consider a program that wants to traverse a string. One way it might do this
  742 -- is to first compute the Addr# pointing to the end of the string, and then,
  743 -- starting from the beginning, bump a pointer using eqAddr# to determine the
  744 -- end. For instance,
  745 --
  746 -- @
  747 -- -- Given pointers to the start and end of a string, count how many zeros
  748 -- -- the string contains.
  749 -- countZeros :: Addr# -> Addr# -> -> Int
  750 -- countZeros start end = go start 0
  751 --   where
  752 --     go off n
  753 --       | off `addrEq#` end = n
  754 --       | otherwise         = go (off `plusAddr#` 1) n'
  755 --       where n' | isTrue# (indexInt8OffAddr# off 0# ==# 0#) = n + 1
  756 --                | otherwise                                 = n
  757 -- @
  758 --
  759 -- Consider what happens if we considered strings to be trivial (and therefore
  760 -- duplicable) and emitted a call like @countZeros "hello"# ("hello"#
  761 -- `plusAddr`# 5)@. The beginning and end pointers do not belong to the same
  762 -- string, meaning that an iteration like the above would blow up terribly.
  763 -- This is what happened in #12757.
  764 --
  765 -- Ultimately the solution here is to make primitive strings a bit more
  766 -- structured, ensuring that the compiler can't inline in ways that will break
  767 -- user code. One approach to this is described in #8472.
  768 litIsTrivial :: Literal -> Bool
  769 --      c.f. GHC.Core.Utils.exprIsTrivial
  770 litIsTrivial (LitString _)    = False
  771 litIsTrivial (LitNumber nt _) = case nt of
  772   LitNumBigNat  -> False
  773   LitNumInt     -> True
  774   LitNumInt8    -> True
  775   LitNumInt16   -> True
  776   LitNumInt32   -> True
  777   LitNumInt64   -> True
  778   LitNumWord    -> True
  779   LitNumWord8   -> True
  780   LitNumWord16  -> True
  781   LitNumWord32  -> True
  782   LitNumWord64  -> True
  783 litIsTrivial _                  = True
  784 
  785 -- | True if code space does not go bad if we duplicate this literal
  786 litIsDupable :: Platform -> Literal -> Bool
  787 --      c.f. GHC.Core.Utils.exprIsDupable
  788 litIsDupable platform x = case x of
  789    LitNumber nt i -> case nt of
  790       LitNumBigNat  -> i <= platformMaxWord platform * 8 -- arbitrary, reasonable
  791       LitNumInt     -> True
  792       LitNumInt8    -> True
  793       LitNumInt16   -> True
  794       LitNumInt32   -> True
  795       LitNumInt64   -> True
  796       LitNumWord    -> True
  797       LitNumWord8   -> True
  798       LitNumWord16  -> True
  799       LitNumWord32  -> True
  800       LitNumWord64  -> True
  801    LitString _ -> False
  802    _           -> True
  803 
  804 litFitsInChar :: Literal -> Bool
  805 litFitsInChar (LitNumber _ i) = i >= toInteger (ord minBound)
  806                               && i <= toInteger (ord maxBound)
  807 litFitsInChar _               = False
  808 
  809 litIsLifted :: Literal -> Bool
  810 litIsLifted (LitNumber nt _) = case nt of
  811   LitNumBigNat  -> True
  812   LitNumInt     -> False
  813   LitNumInt8    -> False
  814   LitNumInt16   -> False
  815   LitNumInt32   -> False
  816   LitNumInt64   -> False
  817   LitNumWord    -> False
  818   LitNumWord8   -> False
  819   LitNumWord16  -> False
  820   LitNumWord32  -> False
  821   LitNumWord64  -> False
  822 litIsLifted _                        = False
  823   -- Even RUBBISH[LiftedRep] is unlifted, as rubbish values are always evaluated.
  824 
  825 {-
  826         Types
  827         ~~~~~
  828 -}
  829 
  830 -- | Find the Haskell 'Type' the literal occupies
  831 literalType :: Literal -> Type
  832 literalType LitNullAddr       = addrPrimTy
  833 literalType (LitChar _)       = charPrimTy
  834 literalType (LitString  _)    = addrPrimTy
  835 literalType (LitFloat _)      = floatPrimTy
  836 literalType (LitDouble _)     = doublePrimTy
  837 literalType (LitLabel _ _ _)  = addrPrimTy
  838 literalType (LitNumber lt _)  = case lt of
  839    LitNumBigNat  -> byteArrayPrimTy
  840    LitNumInt     -> intPrimTy
  841    LitNumInt8    -> int8PrimTy
  842    LitNumInt16   -> int16PrimTy
  843    LitNumInt32   -> int32PrimTy
  844    LitNumInt64   -> int64PrimTy
  845    LitNumWord    -> wordPrimTy
  846    LitNumWord8   -> word8PrimTy
  847    LitNumWord16  -> word16PrimTy
  848    LitNumWord32  -> word32PrimTy
  849    LitNumWord64  -> word64PrimTy
  850 
  851 -- LitRubbish: see Note [Rubbish literals]
  852 literalType (LitRubbish rep)
  853   = mkForAllTy a Inferred (mkTyVarTy a)
  854   where
  855     a = mkTemplateKindVar (tYPE rep)
  856 
  857 {-
  858         Comparison
  859         ~~~~~~~~~~
  860 -}
  861 
  862 cmpLit :: Literal -> Literal -> Ordering
  863 cmpLit (LitChar      a)     (LitChar       b)     = a `compare` b
  864 cmpLit (LitString    a)     (LitString     b)     = a `compare` b
  865 cmpLit (LitNullAddr)        (LitNullAddr)         = EQ
  866 cmpLit (LitFloat     a)     (LitFloat      b)     = a `compare` b
  867 cmpLit (LitDouble    a)     (LitDouble     b)     = a `compare` b
  868 cmpLit (LitLabel     a _ _) (LitLabel      b _ _) = a `lexicalCompareFS` b
  869 cmpLit (LitNumber nt1 a)    (LitNumber nt2  b)
  870   = (nt1 `compare` nt2) `mappend` (a `compare` b)
  871 cmpLit (LitRubbish b1)      (LitRubbish b2)       = b1 `nonDetCmpType` b2
  872 cmpLit lit1 lit2
  873   | isTrue# (dataToTag# lit1 <# dataToTag# lit2) = LT
  874   | otherwise                                    = GT
  875 
  876 {-
  877         Printing
  878         ~~~~~~~~
  879 * See Note [Printing of literals in Core]
  880 -}
  881 
  882 pprLiteral :: (SDoc -> SDoc) -> Literal -> SDoc
  883 pprLiteral _       (LitChar c)     = pprPrimChar c
  884 pprLiteral _       (LitString s)   = pprHsBytes s
  885 pprLiteral _       (LitNullAddr)   = text "__NULL"
  886 pprLiteral _       (LitFloat f)    = float (fromRat f) <> primFloatSuffix
  887 pprLiteral _       (LitDouble d)   = double (fromRat d) <> primDoubleSuffix
  888 pprLiteral _       (LitNumber nt i)
  889    = case nt of
  890        LitNumBigNat  -> integer i
  891        LitNumInt     -> pprPrimInt i
  892        LitNumInt8    -> pprPrimInt8 i
  893        LitNumInt16   -> pprPrimInt16 i
  894        LitNumInt32   -> pprPrimInt32 i
  895        LitNumInt64   -> pprPrimInt64 i
  896        LitNumWord    -> pprPrimWord i
  897        LitNumWord8   -> pprPrimWord8 i
  898        LitNumWord16  -> pprPrimWord16 i
  899        LitNumWord32  -> pprPrimWord32 i
  900        LitNumWord64  -> pprPrimWord64 i
  901 pprLiteral add_par (LitLabel l mb fod) =
  902     add_par (text "__label" <+> b <+> ppr fod)
  903     where b = case mb of
  904               Nothing -> pprHsString l
  905               Just x  -> doubleQuotes (text (unpackFS l ++ '@':show x))
  906 pprLiteral _       (LitRubbish rep)
  907   = text "RUBBISH" <> parens (ppr rep)
  908 
  909 {-
  910 Note [Printing of literals in Core]
  911 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  912 The function `add_par` is used to wrap parenthesis around labels (`LitLabel`),
  913 if they occur in a context requiring an atomic thing (for example function
  914 application).
  915 
  916 Although not all Core literals would be valid Haskell, we are trying to stay
  917 as close as possible to Haskell syntax in the printing of Core, to make it
  918 easier for a Haskell user to read Core.
  919 
  920 To that end:
  921   * We do print parenthesis around negative `LitInteger`, because we print
  922   `LitInteger` using plain number literals (no prefix or suffix), and plain
  923   number literals in Haskell require parenthesis in contexts like function
  924   application (i.e. `1 - -1` is not valid Haskell).
  925 
  926   * We don't print parenthesis around other (negative) literals, because they
  927   aren't needed in GHC/Haskell either (i.e. `1# -# -1#` is accepted by GHC's
  928   parser).
  929 
  930 Literal         Output             Output if context requires
  931                                    an atom (if different)
  932 -------         -------            ----------------------
  933 LitChar         'a'#
  934 LitString       "aaa"#
  935 LitNullAddr     "__NULL"
  936 LitInt          -1#
  937 LitIntN         -1#N
  938 LitWord          1##
  939 LitWordN         1##N
  940 LitFloat        -1.0#
  941 LitDouble       -1.0##
  942 LitBigNat       1
  943 LitLabel        "__label" ...      ("__label" ...)
  944 LitRubbish      "RUBBISH[...]"
  945 
  946 Note [Rubbish literals]
  947 ~~~~~~~~~~~~~~~~~~~~~~~
  948 Sometimes, we need to cough up a rubbish value of a certain type that is used
  949 in place of dead code we thus aim to eliminate. The value of a dead occurrence
  950 has no effect on the dynamic semantics of the program, so we can pick any value
  951 of the same representation.
  952 
  953 Exploiting the results of absence analysis in worker/wrapper is a scenario where
  954 we need such a rubbish value, see examples in Note [Absent fillers] in
  955 GHC.Core.Opt.WorkWrap.Utils.
  956 
  957 It's completely undefined what the *value* of a rubbish value is, e.g., we could
  958 pick @0#@ for @Int#@ or @42#@; it mustn't matter where it's inserted into a Core
  959 program. We embed these rubbish values in the 'LitRubbish' case of the 'Literal'
  960 data type. Here are the moving parts:
  961 
  962 1. Source Haskell: No way to produce rubbish lits in source syntax. Purely
  963    an IR feature.
  964 
  965 2. Core: 'LitRubbish' carries a `Type` of kind RuntimeRep,
  966    describing the runtime representaion of the literal (is it a
  967    pointer, an unboxed Double#, or whatever).
  968 
  969    We have it that `RUBBISH[rr]` has type `forall (a :: TYPE rr). a`.
  970    See the `LitRubbish` case of `literalType`.
  971 
  972    The function GHC.Core.Make.mkLitRubbish makes a Core rubbish literal of
  973    a given type.  It obeys the following invariants:
  974 
  975    INVARIANT 1: 'rr' has no free variables. Main reason: we don't need to run
  976    substitutions and free variable finders over Literal. The rules around
  977    levity/runtime-rep polymorphism naturally uphold this invariant.
  978 
  979    INVARIANT 2: we never make a rubbish literal of type (a ~# b). Reason:
  980    see Note [Core type and coercion invariant] in GHC.Core.  We can't substitute
  981    a LitRubbish inside a coercion, so it's best not to make one. They are zero
  982    width anyway, so passing absent ones around costs nothing.  If we wanted
  983    an absent filler of type (a ~# b) we should use (Coercion (UnivCo ...)),
  984    but it doesn't seem worth making a new UnivCoProvenance for this purpose.
  985 
  986    This is sad, though: see #18983.
  987 
  988 3. STG: The type app in `RUBBISH[IntRep] @Int# :: Int#` is erased and we get
  989    the (untyped) 'StgLit' `RUBBISH[IntRep] :: Int#` in STG.
  990 
  991    It's treated mostly opaque, with the exception of the Unariser, where we
  992    take apart a case scrutinisation on, or arg occurrence of, e.g.,
  993    `RUBBISH[TupleRep[IntRep,DoubleRep]]` (which may stand in for `(# Int#, Double# #)`)
  994    into its sub-parts `RUBBISH[IntRep]` and `RUBBISH[DoubleRep]`, similar to
  995    unboxed tuples. `RUBBISH[VoidRep]` is erased.
  996    See 'unariseRubbish_maybe' and also Note [Post-unarisation invariants].
  997 
  998 4. Cmm: We translate 'LitRubbish' to their actual rubbish value in 'cgLit'.
  999    The particulars are boring, and only matter when debugging illicit use of
 1000    a rubbish value; see Modes of failure below.
 1001 
 1002 5. Bytecode: In GHC.ByteCode.Asm we just lower it as a 0 literal, because it's
 1003    all boxed to the host GC anyway.
 1004 
 1005 6. IfaceSyn: `Literal` is part of `IfaceSyn`, but `Type` really isn't.  So in
 1006    the passage from Core to Iface I put LitRubbish into its owns IfaceExpr data
 1007    constructor, IfaceLitRubbish. The remaining constructors of Literal are
 1008    fine as IfaceSyn.
 1009 
 1010 Wrinkles
 1011 
 1012 a) Why do we put the `Type` (of kind RuntimeRep) inside the literal?  Could
 1013    we not instead /apply/ the literal to that RuntimeRep?  Alas no, becuase
 1014    then LitRubbish :: forall (rr::RuntimeRep) (a::TYPE rr). a
 1015    and that's am ill-formed type because its kind is `TYPE rr`, which escapes
 1016    the binding site of `rr`. Annoying.
 1017 
 1018 b) A rubbish literal is not bottom, and replies True to exprOkForSpeculation.
 1019    For unboxed types there is no bottom anyway.  If we have
 1020        let (x::Int#) = RUBBISH[IntRep] @Int#
 1021    we want to convert that to a case!  We want to leave it as a let, and
 1022    probably discard it as dead code soon after because x is unused.
 1023 
 1024 c) We can see a rubbish literal at the head of an application chain.
 1025    Most obviously, pretty much every rubbish literal is the head of a
 1026    type application e.g. `RUBBISH[IntRep] @Int#`.  But see also
 1027    Note [How a rubbish literal can be the head of an application]
 1028 
 1029 c) Literal is in Ord, because (and only because) we use Ord on AltCon when
 1030    building a TypeMap. Annoying.  We use `nonDetCmpType` here; the
 1031    non-determinism won't matter because it's only used in TrieMap.
 1032    Moreover, rubbish literals should not appear in patterns anyway.
 1033 
 1034 d) Why not lower LitRubbish in CoreToStg? Because it enables us to use
 1035    LitRubbish when unarising unboxed sums in the future, and it allows
 1036    rubbish values of e.g.  VecRep, for which we can't cough up dummy
 1037    values in STG.
 1038 
 1039 Modes of failure
 1040 ----------------
 1041 Suppose there is a bug in GHC, and a rubbish value is used after all. That is
 1042 undefined behavior, of course, but let us list a few examples for failure modes:
 1043 
 1044  a) For an value of unboxed numeric type like `Int#`, we just use a silly
 1045     value like 42#. The error might propoagate indefinitely, hence we better
 1046     pick a rather unique literal. Same for Word, Floats, Char and VecRep.
 1047  b) For AddrRep (like String lits), we mit a null pointer, resulting in a
 1048     definitive segfault when accessed.
 1049  c) For boxed values, unlifted or not, we use a pointer to a fixed closure,
 1050     like `()`, so that the GC has a pointer to follow.
 1051     If we use that pointer as an 'Array#', we will likely access fields of the
 1052     array that don't exist, and a seg-fault is likely, but not guaranteed.
 1053     If we use that pointer as `Either Int Bool`, we might try to access the
 1054     'Int' field of the 'Left' constructor (which has the same ConTag as '()'),
 1055     which doesn't exists. In the best case, we'll find an invalid pointer in its
 1056     position and get a seg-fault, in the worst case the error manifests only one
 1057     or two indirections later.
 1058 
 1059 Note [How a rubbish literal can be the head of an application]
 1060 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1061 Consider this (#19824):
 1062 
 1063     h :: T3 -> Int -> blah
 1064     h _ (I# n) = ...
 1065 
 1066     f :: (T1 -> T2 -> T3) -> T4 -> blah
 1067     f g x = ....(h (g n s) x)...
 1068 
 1069 Demand analysis finds that h does not use its first argument, and w/w's h to
 1070 
 1071     {-# INLINE h #-}
 1072     h a b = case b of I# n -> $wh n
 1073 
 1074 Demand analysis also finds that f does not use its first arg,
 1075 so the worker for f look like
 1076 
 1077     $wf x = let g = RUBBISH in
 1078             ....(h (g n s) x)...
 1079 
 1080 Now we inline g to get:
 1081 
 1082     $wf x = ....(h (RUBBISH n s) x)...
 1083 
 1084 And lo, until we inline `h`, we have that application of
 1085 RUBBISH in $wf's RHS.  But surely `h` will inline? Not if the
 1086 arguments look boring.  Well, RUBBISH doesn't look boring.  But it
 1087 could be a bit more complicated like
 1088    f g x = let t = ...(g n s)...
 1089            in ...(h t x)...
 1090 
 1091 and now the call looks more boring.  Anyway, the point is that we
 1092 might reasonably see RUBBISH at the head of an application chain.
 1093 
 1094 It would be fine to rewrite
 1095   RUBBISH @(ta->tb->tr) a b  --->   RUBBISH @tr
 1096 but we don't currently do so.
 1097 
 1098 It is NOT ok to discard the entire continuation:
 1099   case RUBBISH @ty of DEFAULT -> blah
 1100 does not return RUBBISH!
 1101 -}