{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE RecordWildCards            #-}
{-# LANGUAGE TypeApplications           #-}
--
--  (c) The University of Glasgow 2002-2006
--

-- | Bytecode assembler types
module GHC.ByteCode.Types
  ( CompiledByteCode(..), seqCompiledByteCode
  , FFIInfo(..)
  , RegBitmap(..)
  , NativeCallType(..), NativeCallInfo(..), voidTupleReturnInfo, voidPrimCallInfo
  , ByteOff(..), WordOff(..)
  , UnlinkedBCO(..), BCOPtr(..), BCONPtr(..)
  , ItblEnv, ItblPtr(..)
  , AddrEnv, AddrPtr(..)
  , CgBreakInfo(..)
  , ModBreaks (..), BreakIndex, emptyModBreaks
  , CCostCentre
  ) where

import GHC.Prelude

import GHC.Data.FastString
import GHC.Data.SizedSeq
import GHC.Types.Name
import GHC.Types.Name.Env
import GHC.Utils.Outputable
import GHC.Builtin.PrimOps
import GHC.Types.SrcLoc
import GHCi.BreakArray
import GHCi.RemoteTypes
import GHCi.FFI
import Control.DeepSeq

import Foreign
import Data.Array
import Data.Array.Base  ( UArray(..) )
import Data.ByteString (ByteString)
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import qualified GHC.Exts.Heap as Heap
import GHC.Stack.CCS
import GHC.Cmm.Expr ( GlobalRegSet, emptyRegSet, regSetToList )
import GHC.Iface.Syntax

-- -----------------------------------------------------------------------------
-- Compiled Byte Code

data CompiledByteCode = CompiledByteCode
  { CompiledByteCode -> [UnlinkedBCO]
bc_bcos   :: [UnlinkedBCO]  -- Bunch of interpretable bindings
  , CompiledByteCode -> ItblEnv
bc_itbls  :: ItblEnv        -- A mapping from DataCons to their itbls
  , CompiledByteCode -> [FFIInfo]
bc_ffis   :: [FFIInfo]      -- ffi blocks we allocated
  , CompiledByteCode -> AddrEnv
bc_strs   :: AddrEnv        -- malloc'd top-level strings
  , CompiledByteCode -> Maybe ModBreaks
bc_breaks :: Maybe ModBreaks -- breakpoint info (Nothing if we're not
                                 -- creating breakpoints, for some reason)
  }
                -- ToDo: we're not tracking strings that we malloc'd
newtype FFIInfo = FFIInfo (RemotePtr C_ffi_cif)
  deriving (BreakIndex -> FFIInfo -> ShowS
[FFIInfo] -> ShowS
FFIInfo -> String
forall a.
(BreakIndex -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FFIInfo] -> ShowS
$cshowList :: [FFIInfo] -> ShowS
show :: FFIInfo -> String
$cshow :: FFIInfo -> String
showsPrec :: BreakIndex -> FFIInfo -> ShowS
$cshowsPrec :: BreakIndex -> FFIInfo -> ShowS
Show, FFIInfo -> ()
forall a. (a -> ()) -> NFData a
rnf :: FFIInfo -> ()
$crnf :: FFIInfo -> ()
NFData)

instance Outputable CompiledByteCode where
  ppr :: CompiledByteCode -> SDoc
ppr CompiledByteCode{[UnlinkedBCO]
[FFIInfo]
Maybe ModBreaks
AddrEnv
ItblEnv
bc_breaks :: Maybe ModBreaks
bc_strs :: AddrEnv
bc_ffis :: [FFIInfo]
bc_itbls :: ItblEnv
bc_bcos :: [UnlinkedBCO]
bc_breaks :: CompiledByteCode -> Maybe ModBreaks
bc_strs :: CompiledByteCode -> AddrEnv
bc_ffis :: CompiledByteCode -> [FFIInfo]
bc_itbls :: CompiledByteCode -> ItblEnv
bc_bcos :: CompiledByteCode -> [UnlinkedBCO]
..} = forall a. Outputable a => a -> SDoc
ppr [UnlinkedBCO]
bc_bcos

-- Not a real NFData instance, because ModBreaks contains some things
-- we can't rnf
seqCompiledByteCode :: CompiledByteCode -> ()
seqCompiledByteCode :: CompiledByteCode -> ()
seqCompiledByteCode CompiledByteCode{[UnlinkedBCO]
[FFIInfo]
Maybe ModBreaks
AddrEnv
ItblEnv
bc_breaks :: Maybe ModBreaks
bc_strs :: AddrEnv
bc_ffis :: [FFIInfo]
bc_itbls :: ItblEnv
bc_bcos :: [UnlinkedBCO]
bc_breaks :: CompiledByteCode -> Maybe ModBreaks
bc_strs :: CompiledByteCode -> AddrEnv
bc_ffis :: CompiledByteCode -> [FFIInfo]
bc_itbls :: CompiledByteCode -> ItblEnv
bc_bcos :: CompiledByteCode -> [UnlinkedBCO]
..} =
  forall a. NFData a => a -> ()
rnf [UnlinkedBCO]
bc_bcos seq :: forall a b. a -> b -> b
`seq`
  forall elt. (elt -> ()) -> NameEnv elt -> ()
seqEltsNameEnv forall a. NFData a => a -> ()
rnf ItblEnv
bc_itbls seq :: forall a b. a -> b -> b
`seq`
  forall a. NFData a => a -> ()
rnf [FFIInfo]
bc_ffis seq :: forall a b. a -> b -> b
`seq`
  forall elt. (elt -> ()) -> NameEnv elt -> ()
seqEltsNameEnv forall a. NFData a => a -> ()
rnf AddrEnv
bc_strs seq :: forall a b. a -> b -> b
`seq`
  forall a. NFData a => a -> ()
rnf (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ModBreaks -> ()
seqModBreaks Maybe ModBreaks
bc_breaks)

newtype ByteOff = ByteOff Int
    deriving (BreakIndex -> ByteOff
ByteOff -> BreakIndex
ByteOff -> [ByteOff]
ByteOff -> ByteOff
ByteOff -> ByteOff -> [ByteOff]
ByteOff -> ByteOff -> ByteOff -> [ByteOff]
forall a.
(a -> a)
-> (a -> a)
-> (BreakIndex -> a)
-> (a -> BreakIndex)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: ByteOff -> ByteOff -> ByteOff -> [ByteOff]
$cenumFromThenTo :: ByteOff -> ByteOff -> ByteOff -> [ByteOff]
enumFromTo :: ByteOff -> ByteOff -> [ByteOff]
$cenumFromTo :: ByteOff -> ByteOff -> [ByteOff]
enumFromThen :: ByteOff -> ByteOff -> [ByteOff]
$cenumFromThen :: ByteOff -> ByteOff -> [ByteOff]
enumFrom :: ByteOff -> [ByteOff]
$cenumFrom :: ByteOff -> [ByteOff]
fromEnum :: ByteOff -> BreakIndex
$cfromEnum :: ByteOff -> BreakIndex
toEnum :: BreakIndex -> ByteOff
$ctoEnum :: BreakIndex -> ByteOff
pred :: ByteOff -> ByteOff
$cpred :: ByteOff -> ByteOff
succ :: ByteOff -> ByteOff
$csucc :: ByteOff -> ByteOff
Enum, ByteOff -> ByteOff -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ByteOff -> ByteOff -> Bool
$c/= :: ByteOff -> ByteOff -> Bool
== :: ByteOff -> ByteOff -> Bool
$c== :: ByteOff -> ByteOff -> Bool
Eq, BreakIndex -> ByteOff -> ShowS
[ByteOff] -> ShowS
ByteOff -> String
forall a.
(BreakIndex -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ByteOff] -> ShowS
$cshowList :: [ByteOff] -> ShowS
show :: ByteOff -> String
$cshow :: ByteOff -> String
showsPrec :: BreakIndex -> ByteOff -> ShowS
$cshowsPrec :: BreakIndex -> ByteOff -> ShowS
Show, Enum ByteOff
Real ByteOff
ByteOff -> Integer
ByteOff -> ByteOff -> (ByteOff, ByteOff)
ByteOff -> ByteOff -> ByteOff
forall a.
Real a
-> Enum a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: ByteOff -> Integer
$ctoInteger :: ByteOff -> Integer
divMod :: ByteOff -> ByteOff -> (ByteOff, ByteOff)
$cdivMod :: ByteOff -> ByteOff -> (ByteOff, ByteOff)
quotRem :: ByteOff -> ByteOff -> (ByteOff, ByteOff)
$cquotRem :: ByteOff -> ByteOff -> (ByteOff, ByteOff)
mod :: ByteOff -> ByteOff -> ByteOff
$cmod :: ByteOff -> ByteOff -> ByteOff
div :: ByteOff -> ByteOff -> ByteOff
$cdiv :: ByteOff -> ByteOff -> ByteOff
rem :: ByteOff -> ByteOff -> ByteOff
$crem :: ByteOff -> ByteOff -> ByteOff
quot :: ByteOff -> ByteOff -> ByteOff
$cquot :: ByteOff -> ByteOff -> ByteOff
Integral, Integer -> ByteOff
ByteOff -> ByteOff
ByteOff -> ByteOff -> ByteOff
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> ByteOff
$cfromInteger :: Integer -> ByteOff
signum :: ByteOff -> ByteOff
$csignum :: ByteOff -> ByteOff
abs :: ByteOff -> ByteOff
$cabs :: ByteOff -> ByteOff
negate :: ByteOff -> ByteOff
$cnegate :: ByteOff -> ByteOff
* :: ByteOff -> ByteOff -> ByteOff
$c* :: ByteOff -> ByteOff -> ByteOff
- :: ByteOff -> ByteOff -> ByteOff
$c- :: ByteOff -> ByteOff -> ByteOff
+ :: ByteOff -> ByteOff -> ByteOff
$c+ :: ByteOff -> ByteOff -> ByteOff
Num, Eq ByteOff
ByteOff -> ByteOff -> Bool
ByteOff -> ByteOff -> Ordering
ByteOff -> ByteOff -> ByteOff
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ByteOff -> ByteOff -> ByteOff
$cmin :: ByteOff -> ByteOff -> ByteOff
max :: ByteOff -> ByteOff -> ByteOff
$cmax :: ByteOff -> ByteOff -> ByteOff
>= :: ByteOff -> ByteOff -> Bool
$c>= :: ByteOff -> ByteOff -> Bool
> :: ByteOff -> ByteOff -> Bool
$c> :: ByteOff -> ByteOff -> Bool
<= :: ByteOff -> ByteOff -> Bool
$c<= :: ByteOff -> ByteOff -> Bool
< :: ByteOff -> ByteOff -> Bool
$c< :: ByteOff -> ByteOff -> Bool
compare :: ByteOff -> ByteOff -> Ordering
$ccompare :: ByteOff -> ByteOff -> Ordering
Ord, Num ByteOff
Ord ByteOff
ByteOff -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
toRational :: ByteOff -> Rational
$ctoRational :: ByteOff -> Rational
Real, ByteOff -> SDoc
forall a. (a -> SDoc) -> Outputable a
ppr :: ByteOff -> SDoc
$cppr :: ByteOff -> SDoc
Outputable)

newtype WordOff = WordOff Int
    deriving (BreakIndex -> WordOff
WordOff -> BreakIndex
WordOff -> [WordOff]
WordOff -> WordOff
WordOff -> WordOff -> [WordOff]
WordOff -> WordOff -> WordOff -> [WordOff]
forall a.
(a -> a)
-> (a -> a)
-> (BreakIndex -> a)
-> (a -> BreakIndex)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: WordOff -> WordOff -> WordOff -> [WordOff]
$cenumFromThenTo :: WordOff -> WordOff -> WordOff -> [WordOff]
enumFromTo :: WordOff -> WordOff -> [WordOff]
$cenumFromTo :: WordOff -> WordOff -> [WordOff]
enumFromThen :: WordOff -> WordOff -> [WordOff]
$cenumFromThen :: WordOff -> WordOff -> [WordOff]
enumFrom :: WordOff -> [WordOff]
$cenumFrom :: WordOff -> [WordOff]
fromEnum :: WordOff -> BreakIndex
$cfromEnum :: WordOff -> BreakIndex
toEnum :: BreakIndex -> WordOff
$ctoEnum :: BreakIndex -> WordOff
pred :: WordOff -> WordOff
$cpred :: WordOff -> WordOff
succ :: WordOff -> WordOff
$csucc :: WordOff -> WordOff
Enum, WordOff -> WordOff -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: WordOff -> WordOff -> Bool
$c/= :: WordOff -> WordOff -> Bool
== :: WordOff -> WordOff -> Bool
$c== :: WordOff -> WordOff -> Bool
Eq, BreakIndex -> WordOff -> ShowS
[WordOff] -> ShowS
WordOff -> String
forall a.
(BreakIndex -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WordOff] -> ShowS
$cshowList :: [WordOff] -> ShowS
show :: WordOff -> String
$cshow :: WordOff -> String
showsPrec :: BreakIndex -> WordOff -> ShowS
$cshowsPrec :: BreakIndex -> WordOff -> ShowS
Show, Enum WordOff
Real WordOff
WordOff -> Integer
WordOff -> WordOff -> (WordOff, WordOff)
WordOff -> WordOff -> WordOff
forall a.
Real a
-> Enum a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: WordOff -> Integer
$ctoInteger :: WordOff -> Integer
divMod :: WordOff -> WordOff -> (WordOff, WordOff)
$cdivMod :: WordOff -> WordOff -> (WordOff, WordOff)
quotRem :: WordOff -> WordOff -> (WordOff, WordOff)
$cquotRem :: WordOff -> WordOff -> (WordOff, WordOff)
mod :: WordOff -> WordOff -> WordOff
$cmod :: WordOff -> WordOff -> WordOff
div :: WordOff -> WordOff -> WordOff
$cdiv :: WordOff -> WordOff -> WordOff
rem :: WordOff -> WordOff -> WordOff
$crem :: WordOff -> WordOff -> WordOff
quot :: WordOff -> WordOff -> WordOff
$cquot :: WordOff -> WordOff -> WordOff
Integral, Integer -> WordOff
WordOff -> WordOff
WordOff -> WordOff -> WordOff
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> WordOff
$cfromInteger :: Integer -> WordOff
signum :: WordOff -> WordOff
$csignum :: WordOff -> WordOff
abs :: WordOff -> WordOff
$cabs :: WordOff -> WordOff
negate :: WordOff -> WordOff
$cnegate :: WordOff -> WordOff
* :: WordOff -> WordOff -> WordOff
$c* :: WordOff -> WordOff -> WordOff
- :: WordOff -> WordOff -> WordOff
$c- :: WordOff -> WordOff -> WordOff
+ :: WordOff -> WordOff -> WordOff
$c+ :: WordOff -> WordOff -> WordOff
Num, Eq WordOff
WordOff -> WordOff -> Bool
WordOff -> WordOff -> Ordering
WordOff -> WordOff -> WordOff
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: WordOff -> WordOff -> WordOff
$cmin :: WordOff -> WordOff -> WordOff
max :: WordOff -> WordOff -> WordOff
$cmax :: WordOff -> WordOff -> WordOff
>= :: WordOff -> WordOff -> Bool
$c>= :: WordOff -> WordOff -> Bool
> :: WordOff -> WordOff -> Bool
$c> :: WordOff -> WordOff -> Bool
<= :: WordOff -> WordOff -> Bool
$c<= :: WordOff -> WordOff -> Bool
< :: WordOff -> WordOff -> Bool
$c< :: WordOff -> WordOff -> Bool
compare :: WordOff -> WordOff -> Ordering
$ccompare :: WordOff -> WordOff -> Ordering
Ord, Num WordOff
Ord WordOff
WordOff -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
toRational :: WordOff -> Rational
$ctoRational :: WordOff -> Rational
Real, WordOff -> SDoc
forall a. (a -> SDoc) -> Outputable a
ppr :: WordOff -> SDoc
$cppr :: WordOff -> SDoc
Outputable)

newtype RegBitmap = RegBitmap { RegBitmap -> Word32
unRegBitmap :: Word32 }
    deriving (BreakIndex -> RegBitmap
RegBitmap -> BreakIndex
RegBitmap -> [RegBitmap]
RegBitmap -> RegBitmap
RegBitmap -> RegBitmap -> [RegBitmap]
RegBitmap -> RegBitmap -> RegBitmap -> [RegBitmap]
forall a.
(a -> a)
-> (a -> a)
-> (BreakIndex -> a)
-> (a -> BreakIndex)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: RegBitmap -> RegBitmap -> RegBitmap -> [RegBitmap]
$cenumFromThenTo :: RegBitmap -> RegBitmap -> RegBitmap -> [RegBitmap]
enumFromTo :: RegBitmap -> RegBitmap -> [RegBitmap]
$cenumFromTo :: RegBitmap -> RegBitmap -> [RegBitmap]
enumFromThen :: RegBitmap -> RegBitmap -> [RegBitmap]
$cenumFromThen :: RegBitmap -> RegBitmap -> [RegBitmap]
enumFrom :: RegBitmap -> [RegBitmap]
$cenumFrom :: RegBitmap -> [RegBitmap]
fromEnum :: RegBitmap -> BreakIndex
$cfromEnum :: RegBitmap -> BreakIndex
toEnum :: BreakIndex -> RegBitmap
$ctoEnum :: BreakIndex -> RegBitmap
pred :: RegBitmap -> RegBitmap
$cpred :: RegBitmap -> RegBitmap
succ :: RegBitmap -> RegBitmap
$csucc :: RegBitmap -> RegBitmap
Enum, RegBitmap -> RegBitmap -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RegBitmap -> RegBitmap -> Bool
$c/= :: RegBitmap -> RegBitmap -> Bool
== :: RegBitmap -> RegBitmap -> Bool
$c== :: RegBitmap -> RegBitmap -> Bool
Eq, BreakIndex -> RegBitmap -> ShowS
[RegBitmap] -> ShowS
RegBitmap -> String
forall a.
(BreakIndex -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RegBitmap] -> ShowS
$cshowList :: [RegBitmap] -> ShowS
show :: RegBitmap -> String
$cshow :: RegBitmap -> String
showsPrec :: BreakIndex -> RegBitmap -> ShowS
$cshowsPrec :: BreakIndex -> RegBitmap -> ShowS
Show, Enum RegBitmap
Real RegBitmap
RegBitmap -> Integer
RegBitmap -> RegBitmap -> (RegBitmap, RegBitmap)
RegBitmap -> RegBitmap -> RegBitmap
forall a.
Real a
-> Enum a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
toInteger :: RegBitmap -> Integer
$ctoInteger :: RegBitmap -> Integer
divMod :: RegBitmap -> RegBitmap -> (RegBitmap, RegBitmap)
$cdivMod :: RegBitmap -> RegBitmap -> (RegBitmap, RegBitmap)
quotRem :: RegBitmap -> RegBitmap -> (RegBitmap, RegBitmap)
$cquotRem :: RegBitmap -> RegBitmap -> (RegBitmap, RegBitmap)
mod :: RegBitmap -> RegBitmap -> RegBitmap
$cmod :: RegBitmap -> RegBitmap -> RegBitmap
div :: RegBitmap -> RegBitmap -> RegBitmap
$cdiv :: RegBitmap -> RegBitmap -> RegBitmap
rem :: RegBitmap -> RegBitmap -> RegBitmap
$crem :: RegBitmap -> RegBitmap -> RegBitmap
quot :: RegBitmap -> RegBitmap -> RegBitmap
$cquot :: RegBitmap -> RegBitmap -> RegBitmap
Integral, Integer -> RegBitmap
RegBitmap -> RegBitmap
RegBitmap -> RegBitmap -> RegBitmap
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> RegBitmap
$cfromInteger :: Integer -> RegBitmap
signum :: RegBitmap -> RegBitmap
$csignum :: RegBitmap -> RegBitmap
abs :: RegBitmap -> RegBitmap
$cabs :: RegBitmap -> RegBitmap
negate :: RegBitmap -> RegBitmap
$cnegate :: RegBitmap -> RegBitmap
* :: RegBitmap -> RegBitmap -> RegBitmap
$c* :: RegBitmap -> RegBitmap -> RegBitmap
- :: RegBitmap -> RegBitmap -> RegBitmap
$c- :: RegBitmap -> RegBitmap -> RegBitmap
+ :: RegBitmap -> RegBitmap -> RegBitmap
$c+ :: RegBitmap -> RegBitmap -> RegBitmap
Num, Eq RegBitmap
RegBitmap -> RegBitmap -> Bool
RegBitmap -> RegBitmap -> Ordering
RegBitmap -> RegBitmap -> RegBitmap
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: RegBitmap -> RegBitmap -> RegBitmap
$cmin :: RegBitmap -> RegBitmap -> RegBitmap
max :: RegBitmap -> RegBitmap -> RegBitmap
$cmax :: RegBitmap -> RegBitmap -> RegBitmap
>= :: RegBitmap -> RegBitmap -> Bool
$c>= :: RegBitmap -> RegBitmap -> Bool
> :: RegBitmap -> RegBitmap -> Bool
$c> :: RegBitmap -> RegBitmap -> Bool
<= :: RegBitmap -> RegBitmap -> Bool
$c<= :: RegBitmap -> RegBitmap -> Bool
< :: RegBitmap -> RegBitmap -> Bool
$c< :: RegBitmap -> RegBitmap -> Bool
compare :: RegBitmap -> RegBitmap -> Ordering
$ccompare :: RegBitmap -> RegBitmap -> Ordering
Ord, Num RegBitmap
Ord RegBitmap
RegBitmap -> Rational
forall a. Num a -> Ord a -> (a -> Rational) -> Real a
toRational :: RegBitmap -> Rational
$ctoRational :: RegBitmap -> Rational
Real, Eq RegBitmap
RegBitmap
BreakIndex -> RegBitmap
RegBitmap -> Bool
RegBitmap -> BreakIndex
RegBitmap -> Maybe BreakIndex
RegBitmap -> RegBitmap
RegBitmap -> BreakIndex -> Bool
RegBitmap -> BreakIndex -> RegBitmap
RegBitmap -> RegBitmap -> RegBitmap
forall a.
Eq a
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> BreakIndex -> a)
-> (a -> BreakIndex -> a)
-> a
-> (BreakIndex -> a)
-> (a -> BreakIndex -> a)
-> (a -> BreakIndex -> a)
-> (a -> BreakIndex -> a)
-> (a -> BreakIndex -> Bool)
-> (a -> Maybe BreakIndex)
-> (a -> BreakIndex)
-> (a -> Bool)
-> (a -> BreakIndex -> a)
-> (a -> BreakIndex -> a)
-> (a -> BreakIndex -> a)
-> (a -> BreakIndex -> a)
-> (a -> BreakIndex -> a)
-> (a -> BreakIndex -> a)
-> (a -> BreakIndex)
-> Bits a
popCount :: RegBitmap -> BreakIndex
$cpopCount :: RegBitmap -> BreakIndex
rotateR :: RegBitmap -> BreakIndex -> RegBitmap
$crotateR :: RegBitmap -> BreakIndex -> RegBitmap
rotateL :: RegBitmap -> BreakIndex -> RegBitmap
$crotateL :: RegBitmap -> BreakIndex -> RegBitmap
unsafeShiftR :: RegBitmap -> BreakIndex -> RegBitmap
$cunsafeShiftR :: RegBitmap -> BreakIndex -> RegBitmap
shiftR :: RegBitmap -> BreakIndex -> RegBitmap
$cshiftR :: RegBitmap -> BreakIndex -> RegBitmap
unsafeShiftL :: RegBitmap -> BreakIndex -> RegBitmap
$cunsafeShiftL :: RegBitmap -> BreakIndex -> RegBitmap
shiftL :: RegBitmap -> BreakIndex -> RegBitmap
$cshiftL :: RegBitmap -> BreakIndex -> RegBitmap
isSigned :: RegBitmap -> Bool
$cisSigned :: RegBitmap -> Bool
bitSize :: RegBitmap -> BreakIndex
$cbitSize :: RegBitmap -> BreakIndex
bitSizeMaybe :: RegBitmap -> Maybe BreakIndex
$cbitSizeMaybe :: RegBitmap -> Maybe BreakIndex
testBit :: RegBitmap -> BreakIndex -> Bool
$ctestBit :: RegBitmap -> BreakIndex -> Bool
complementBit :: RegBitmap -> BreakIndex -> RegBitmap
$ccomplementBit :: RegBitmap -> BreakIndex -> RegBitmap
clearBit :: RegBitmap -> BreakIndex -> RegBitmap
$cclearBit :: RegBitmap -> BreakIndex -> RegBitmap
setBit :: RegBitmap -> BreakIndex -> RegBitmap
$csetBit :: RegBitmap -> BreakIndex -> RegBitmap
bit :: BreakIndex -> RegBitmap
$cbit :: BreakIndex -> RegBitmap
zeroBits :: RegBitmap
$czeroBits :: RegBitmap
rotate :: RegBitmap -> BreakIndex -> RegBitmap
$crotate :: RegBitmap -> BreakIndex -> RegBitmap
shift :: RegBitmap -> BreakIndex -> RegBitmap
$cshift :: RegBitmap -> BreakIndex -> RegBitmap
complement :: RegBitmap -> RegBitmap
$ccomplement :: RegBitmap -> RegBitmap
xor :: RegBitmap -> RegBitmap -> RegBitmap
$cxor :: RegBitmap -> RegBitmap -> RegBitmap
.|. :: RegBitmap -> RegBitmap -> RegBitmap
$c.|. :: RegBitmap -> RegBitmap -> RegBitmap
.&. :: RegBitmap -> RegBitmap -> RegBitmap
$c.&. :: RegBitmap -> RegBitmap -> RegBitmap
Bits, Bits RegBitmap
RegBitmap -> BreakIndex
forall b.
Bits b
-> (b -> BreakIndex)
-> (b -> BreakIndex)
-> (b -> BreakIndex)
-> FiniteBits b
countTrailingZeros :: RegBitmap -> BreakIndex
$ccountTrailingZeros :: RegBitmap -> BreakIndex
countLeadingZeros :: RegBitmap -> BreakIndex
$ccountLeadingZeros :: RegBitmap -> BreakIndex
finiteBitSize :: RegBitmap -> BreakIndex
$cfiniteBitSize :: RegBitmap -> BreakIndex
FiniteBits, RegBitmap -> SDoc
forall a. (a -> SDoc) -> Outputable a
ppr :: RegBitmap -> SDoc
$cppr :: RegBitmap -> SDoc
Outputable)

{- Note [GHCi TupleInfo]
~~~~~~~~~~~~~~~~~~~~~~~~
   This contains the data we need for passing unboxed tuples between
   bytecode and native code

   In general we closely follow the native calling convention that
   GHC uses for unboxed tuples, but we don't use any registers in
   bytecode. All tuple elements are expanded to use a full register
   or a full word on the stack.

   The position of tuple elements that are returned on the stack in
   the native calling convention is unchanged when returning the same
   tuple in bytecode.

   The order of the remaining elements is determined by the register in
   which they would have been returned, rather than by their position in
   the tuple in the Haskell source code. This makes jumping between bytecode
   and native code easier: A map of live registers is enough to convert the
   tuple.

   See GHC.StgToByteCode.layoutTuple for more details.
-}

data NativeCallType = NativePrimCall
                    | NativeTupleReturn
  deriving (NativeCallType -> NativeCallType -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NativeCallType -> NativeCallType -> Bool
$c/= :: NativeCallType -> NativeCallType -> Bool
== :: NativeCallType -> NativeCallType -> Bool
$c== :: NativeCallType -> NativeCallType -> Bool
Eq)

data NativeCallInfo = NativeCallInfo
  { NativeCallInfo -> NativeCallType
nativeCallType           :: !NativeCallType
  , NativeCallInfo -> WordOff
nativeCallSize           :: !WordOff   -- total size of arguments in words
  , NativeCallInfo -> GlobalRegSet
nativeCallRegs           :: !GlobalRegSet
  , NativeCallInfo -> WordOff
nativeCallStackSpillSize :: !WordOff {- words spilled on the stack by
                                            GHCs native calling convention -}
  }

instance Outputable NativeCallInfo where
  ppr :: NativeCallInfo -> SDoc
ppr NativeCallInfo{GlobalRegSet
NativeCallType
WordOff
nativeCallStackSpillSize :: WordOff
nativeCallRegs :: GlobalRegSet
nativeCallSize :: WordOff
nativeCallType :: NativeCallType
nativeCallStackSpillSize :: NativeCallInfo -> WordOff
nativeCallRegs :: NativeCallInfo -> GlobalRegSet
nativeCallSize :: NativeCallInfo -> WordOff
nativeCallType :: NativeCallInfo -> NativeCallType
..} = forall doc. IsLine doc => String -> doc
text String
"<arg_size" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr WordOff
nativeCallSize forall doc. IsLine doc => doc -> doc -> doc
<+>
                           forall doc. IsLine doc => String -> doc
text String
"stack" forall doc. IsLine doc => doc -> doc -> doc
<+> forall a. Outputable a => a -> SDoc
ppr WordOff
nativeCallStackSpillSize forall doc. IsLine doc => doc -> doc -> doc
<+>
                           forall doc. IsLine doc => String -> doc
text String
"regs"  forall doc. IsLine doc => doc -> doc -> doc
<+>
                           forall a. Outputable a => a -> SDoc
ppr (forall a b. (a -> b) -> [a] -> [b]
map (forall doc. IsLine doc => String -> doc
text @SDoc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Show a => a -> String
show) forall a b. (a -> b) -> a -> b
$ forall r. RegSet r -> [r]
regSetToList GlobalRegSet
nativeCallRegs) forall doc. IsLine doc => doc -> doc -> doc
<>
                           forall doc. IsLine doc => Char -> doc
char Char
'>'


voidTupleReturnInfo :: NativeCallInfo
voidTupleReturnInfo :: NativeCallInfo
voidTupleReturnInfo = NativeCallType
-> WordOff -> GlobalRegSet -> WordOff -> NativeCallInfo
NativeCallInfo NativeCallType
NativeTupleReturn WordOff
0 forall r. RegSet r
emptyRegSet WordOff
0

voidPrimCallInfo :: NativeCallInfo
voidPrimCallInfo :: NativeCallInfo
voidPrimCallInfo = NativeCallType
-> WordOff -> GlobalRegSet -> WordOff -> NativeCallInfo
NativeCallInfo NativeCallType
NativePrimCall WordOff
0 forall r. RegSet r
emptyRegSet WordOff
0

type ItblEnv = NameEnv (Name, ItblPtr)
type AddrEnv = NameEnv (Name, AddrPtr)
        -- We need the Name in the range so we know which
        -- elements to filter out when unloading a module

newtype ItblPtr = ItblPtr (RemotePtr Heap.StgInfoTable)
  deriving (BreakIndex -> ItblPtr -> ShowS
[ItblPtr] -> ShowS
ItblPtr -> String
forall a.
(BreakIndex -> a -> ShowS)
-> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ItblPtr] -> ShowS
$cshowList :: [ItblPtr] -> ShowS
show :: ItblPtr -> String
$cshow :: ItblPtr -> String
showsPrec :: BreakIndex -> ItblPtr -> ShowS
$cshowsPrec :: BreakIndex -> ItblPtr -> ShowS
Show, ItblPtr -> ()
forall a. (a -> ()) -> NFData a
rnf :: ItblPtr -> ()
$crnf :: ItblPtr -> ()
NFData)
newtype AddrPtr = AddrPtr (RemotePtr ())
  deriving (AddrPtr -> ()
forall a. (a -> ()) -> NFData a
rnf :: AddrPtr -> ()
$crnf :: AddrPtr -> ()
NFData)

data UnlinkedBCO
   = UnlinkedBCO {
        UnlinkedBCO -> Name
unlinkedBCOName   :: !Name,
        UnlinkedBCO -> BreakIndex
unlinkedBCOArity  :: {-# UNPACK #-} !Int,
        UnlinkedBCO -> UArray BreakIndex Word16
unlinkedBCOInstrs :: !(UArray Int Word16),      -- insns
        UnlinkedBCO -> UArray BreakIndex Word64
unlinkedBCOBitmap :: !(UArray Int Word64),      -- bitmap
        UnlinkedBCO -> SizedSeq BCONPtr
unlinkedBCOLits   :: !(SizedSeq BCONPtr),       -- non-ptrs
        UnlinkedBCO -> SizedSeq BCOPtr
unlinkedBCOPtrs   :: !(SizedSeq BCOPtr)         -- ptrs
   }

instance NFData UnlinkedBCO where
  rnf :: UnlinkedBCO -> ()
rnf UnlinkedBCO{BreakIndex
UArray BreakIndex Word16
UArray BreakIndex Word64
SizedSeq BCONPtr
SizedSeq BCOPtr
Name
unlinkedBCOPtrs :: SizedSeq BCOPtr
unlinkedBCOLits :: SizedSeq BCONPtr
unlinkedBCOBitmap :: UArray BreakIndex Word64
unlinkedBCOInstrs :: UArray BreakIndex Word16
unlinkedBCOArity :: BreakIndex
unlinkedBCOName :: Name
unlinkedBCOPtrs :: UnlinkedBCO -> SizedSeq BCOPtr
unlinkedBCOLits :: UnlinkedBCO -> SizedSeq BCONPtr
unlinkedBCOBitmap :: UnlinkedBCO -> UArray BreakIndex Word64
unlinkedBCOInstrs :: UnlinkedBCO -> UArray BreakIndex Word16
unlinkedBCOArity :: UnlinkedBCO -> BreakIndex
unlinkedBCOName :: UnlinkedBCO -> Name
..} =
    forall a. NFData a => a -> ()
rnf SizedSeq BCONPtr
unlinkedBCOLits seq :: forall a b. a -> b -> b
`seq`
    forall a. NFData a => a -> ()
rnf SizedSeq BCOPtr
unlinkedBCOPtrs

data BCOPtr
  = BCOPtrName   !Name
  | BCOPtrPrimOp !PrimOp
  | BCOPtrBCO    !UnlinkedBCO
  | BCOPtrBreakArray  -- a pointer to this module's BreakArray

instance NFData BCOPtr where
  rnf :: BCOPtr -> ()
rnf (BCOPtrBCO UnlinkedBCO
bco) = forall a. NFData a => a -> ()
rnf UnlinkedBCO
bco
  rnf BCOPtr
x = BCOPtr
x seq :: forall a b. a -> b -> b
`seq` ()

data BCONPtr
  = BCONPtrWord  {-# UNPACK #-} !Word
  | BCONPtrLbl   !FastString
  | BCONPtrItbl  !Name
  -- | A reference to a top-level string literal; see
  -- Note [Generating code for top-level string literal bindings] in GHC.StgToByteCode.
  | BCONPtrAddr  !Name
  -- | Only used internally in the assembler in an intermediate representation;
  -- should never appear in a fully-assembled UnlinkedBCO.
  -- Also see Note [Allocating string literals] in GHC.ByteCode.Asm.
  | BCONPtrStr   !ByteString

instance NFData BCONPtr where
  rnf :: BCONPtr -> ()
rnf BCONPtr
x = BCONPtr
x seq :: forall a b. a -> b -> b
`seq` ()

-- | Information about a breakpoint that we know at code-generation time
-- In order to be used, this needs to be hydrated relative to the current HscEnv by
-- 'hydrateCgBreakInfo'. Everything here can be fully forced and that's critical for
-- preventing space leaks (see #22530)
data CgBreakInfo
   = CgBreakInfo
   { CgBreakInfo -> [IfaceTvBndr]
cgb_tyvars :: ![IfaceTvBndr] -- ^ Type variables in scope at the breakpoint
   , CgBreakInfo -> [Maybe (IfaceIdBndr, Word16)]
cgb_vars   :: ![Maybe (IfaceIdBndr, Word16)]
   , CgBreakInfo -> IfaceType
cgb_resty  :: !IfaceType
   }
-- See Note [Syncing breakpoint info] in GHC.Runtime.Eval

seqCgBreakInfo :: CgBreakInfo -> ()
seqCgBreakInfo :: CgBreakInfo -> ()
seqCgBreakInfo CgBreakInfo{[Maybe (IfaceIdBndr, Word16)]
[IfaceTvBndr]
IfaceType
cgb_resty :: IfaceType
cgb_vars :: [Maybe (IfaceIdBndr, Word16)]
cgb_tyvars :: [IfaceTvBndr]
cgb_resty :: CgBreakInfo -> IfaceType
cgb_vars :: CgBreakInfo -> [Maybe (IfaceIdBndr, Word16)]
cgb_tyvars :: CgBreakInfo -> [IfaceTvBndr]
..} =
    forall a. NFData a => a -> ()
rnf [IfaceTvBndr]
cgb_tyvars seq :: forall a b. a -> b -> b
`seq`
    forall a. NFData a => a -> ()
rnf [Maybe (IfaceIdBndr, Word16)]
cgb_vars seq :: forall a b. a -> b -> b
`seq`
    forall a. NFData a => a -> ()
rnf IfaceType
cgb_resty

instance Outputable UnlinkedBCO where
   ppr :: UnlinkedBCO -> SDoc
ppr (UnlinkedBCO Name
nm BreakIndex
_arity UArray BreakIndex Word16
_insns UArray BreakIndex Word64
_bitmap SizedSeq BCONPtr
lits SizedSeq BCOPtr
ptrs)
      = forall doc. IsLine doc => [doc] -> doc
sep [forall doc. IsLine doc => String -> doc
text String
"BCO", forall a. Outputable a => a -> SDoc
ppr Name
nm, forall doc. IsLine doc => String -> doc
text String
"with",
             forall a. Outputable a => a -> SDoc
ppr (forall a. SizedSeq a -> Word
sizeSS SizedSeq BCONPtr
lits), forall doc. IsLine doc => String -> doc
text String
"lits",
             forall a. Outputable a => a -> SDoc
ppr (forall a. SizedSeq a -> Word
sizeSS SizedSeq BCOPtr
ptrs), forall doc. IsLine doc => String -> doc
text String
"ptrs" ]

instance Outputable CgBreakInfo where
   ppr :: CgBreakInfo -> SDoc
ppr CgBreakInfo
info = forall doc. IsLine doc => String -> doc
text String
"CgBreakInfo" forall doc. IsLine doc => doc -> doc -> doc
<+>
              forall doc. IsLine doc => doc -> doc
parens (forall a. Outputable a => a -> SDoc
ppr (CgBreakInfo -> [Maybe (IfaceIdBndr, Word16)]
cgb_vars CgBreakInfo
info) forall doc. IsLine doc => doc -> doc -> doc
<+>
                      forall a. Outputable a => a -> SDoc
ppr (CgBreakInfo -> IfaceType
cgb_resty CgBreakInfo
info))

-- -----------------------------------------------------------------------------
-- Breakpoints

-- | Breakpoint index
type BreakIndex = Int

-- | C CostCentre type
data CCostCentre

-- | All the information about the breakpoints for a module
data ModBreaks
   = ModBreaks
   { ModBreaks -> ForeignRef BreakArray
modBreaks_flags :: ForeignRef BreakArray
        -- ^ The array of flags, one per breakpoint,
        -- indicating which breakpoints are enabled.
   , ModBreaks -> Array BreakIndex SrcSpan
modBreaks_locs :: !(Array BreakIndex SrcSpan)
        -- ^ An array giving the source span of each breakpoint.
   , ModBreaks -> Array BreakIndex [OccName]
modBreaks_vars :: !(Array BreakIndex [OccName])
        -- ^ An array giving the names of the free variables at each breakpoint.
   , ModBreaks -> Array BreakIndex [String]
modBreaks_decls :: !(Array BreakIndex [String])
        -- ^ An array giving the names of the declarations enclosing each breakpoint.
        -- See Note [Field modBreaks_decls]
   , ModBreaks -> Array BreakIndex (RemotePtr CostCentre)
modBreaks_ccs :: !(Array BreakIndex (RemotePtr CostCentre))
        -- ^ Array pointing to cost centre for each breakpoint
   , ModBreaks -> IntMap CgBreakInfo
modBreaks_breakInfo :: IntMap CgBreakInfo
        -- ^ info about each breakpoint from the bytecode generator
   }

seqModBreaks :: ModBreaks -> ()
seqModBreaks :: ModBreaks -> ()
seqModBreaks ModBreaks{Array BreakIndex [String]
Array BreakIndex [OccName]
Array BreakIndex (RemotePtr CostCentre)
Array BreakIndex SrcSpan
IntMap CgBreakInfo
ForeignRef BreakArray
modBreaks_breakInfo :: IntMap CgBreakInfo
modBreaks_ccs :: Array BreakIndex (RemotePtr CostCentre)
modBreaks_decls :: Array BreakIndex [String]
modBreaks_vars :: Array BreakIndex [OccName]
modBreaks_locs :: Array BreakIndex SrcSpan
modBreaks_flags :: ForeignRef BreakArray
modBreaks_breakInfo :: ModBreaks -> IntMap CgBreakInfo
modBreaks_ccs :: ModBreaks -> Array BreakIndex (RemotePtr CostCentre)
modBreaks_decls :: ModBreaks -> Array BreakIndex [String]
modBreaks_vars :: ModBreaks -> Array BreakIndex [OccName]
modBreaks_locs :: ModBreaks -> Array BreakIndex SrcSpan
modBreaks_flags :: ModBreaks -> ForeignRef BreakArray
..} =
  forall a. NFData a => a -> ()
rnf ForeignRef BreakArray
modBreaks_flags seq :: forall a b. a -> b -> b
`seq`
  forall a. NFData a => a -> ()
rnf Array BreakIndex SrcSpan
modBreaks_locs seq :: forall a b. a -> b -> b
`seq`
  forall a. NFData a => a -> ()
rnf Array BreakIndex [OccName]
modBreaks_vars seq :: forall a b. a -> b -> b
`seq`
  forall a. NFData a => a -> ()
rnf Array BreakIndex [String]
modBreaks_decls seq :: forall a b. a -> b -> b
`seq`
  forall a. NFData a => a -> ()
rnf Array BreakIndex (RemotePtr CostCentre)
modBreaks_ccs seq :: forall a b. a -> b -> b
`seq`
  forall a. NFData a => a -> ()
rnf (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CgBreakInfo -> ()
seqCgBreakInfo IntMap CgBreakInfo
modBreaks_breakInfo)

-- | Construct an empty ModBreaks
emptyModBreaks :: ModBreaks
emptyModBreaks :: ModBreaks
emptyModBreaks = ModBreaks
   { modBreaks_flags :: ForeignRef BreakArray
modBreaks_flags = forall a. HasCallStack => String -> a
error String
"ModBreaks.modBreaks_array not initialised"
         -- ToDo: can we avoid this?
   , modBreaks_locs :: Array BreakIndex SrcSpan
modBreaks_locs  = forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
array (BreakIndex
0,-BreakIndex
1) []
   , modBreaks_vars :: Array BreakIndex [OccName]
modBreaks_vars  = forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
array (BreakIndex
0,-BreakIndex
1) []
   , modBreaks_decls :: Array BreakIndex [String]
modBreaks_decls = forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
array (BreakIndex
0,-BreakIndex
1) []
   , modBreaks_ccs :: Array BreakIndex (RemotePtr CostCentre)
modBreaks_ccs = forall i e. Ix i => (i, i) -> [(i, e)] -> Array i e
array (BreakIndex
0,-BreakIndex
1) []
   , modBreaks_breakInfo :: IntMap CgBreakInfo
modBreaks_breakInfo = forall a. IntMap a
IntMap.empty
   }

{-
Note [Field modBreaks_decls]
~~~~~~~~~~~~~~~~~~~~~~
A value of eg ["foo", "bar", "baz"] in a `modBreaks_decls` field means:
The breakpoint is in the function called "baz" that is declared in a `let`
or `where` clause of a declaration called "bar", which itself is declared
in a `let` or `where` clause of the top-level function called "foo".
-}