{-# LANGUAGE CPP #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module GHC.Stg.Syntax (
StgArg(..),
GenStgTopBinding(..), GenStgBinding(..), GenStgExpr(..), GenStgRhs(..),
GenStgAlt, AltType(..),
StgPass(..), BinderP, XRhsClosure, XLet, XLetNoEscape, XConApp,
NoExtFieldSilent, noExtFieldSilent,
OutputablePass,
UpdateFlag(..), isUpdatable,
ConstructorNumber(..),
StgTopBinding, StgBinding, StgExpr, StgRhs, StgAlt,
CgStgTopBinding, CgStgBinding, CgStgExpr, CgStgRhs, CgStgAlt,
LlStgTopBinding, LlStgBinding, LlStgExpr, LlStgRhs, LlStgAlt,
InStgArg, InStgTopBinding, InStgBinding, InStgExpr, InStgRhs, InStgAlt,
OutStgArg, OutStgTopBinding, OutStgBinding, OutStgExpr, OutStgRhs, OutStgAlt,
StgOp(..),
stgRhsArity, freeVarsOfRhs,
isDllConApp,
stgArgType,
stripStgTicksTop, stripStgTicksTopE,
stgCaseBndrInScope,
bindersOf, bindersOfTop, bindersOfTopBinds,
StgPprOpts(..), initStgPprOpts,
panicStgPprOpts, shortStgPprOpts,
pprStgArg, pprStgExpr, pprStgRhs, pprStgBinding,
pprGenStgTopBinding, pprStgTopBinding,
pprGenStgTopBindings, pprStgTopBindings
) where
#include "HsVersions.h"
import GHC.Prelude
import GHC.Core ( AltCon )
import GHC.Types.CostCentre ( CostCentreStack )
import Data.ByteString ( ByteString )
import Data.Data ( Data )
import Data.List ( intersperse )
import GHC.Core.DataCon
import GHC.Driver.Session
import GHC.Types.ForeignCall ( ForeignCall )
import GHC.Types.Id
import GHC.Types.Name ( isDynLinkName )
import GHC.Types.Tickish ( StgTickish )
import GHC.Types.Var.Set
import GHC.Types.Literal ( Literal, literalType )
import GHC.Unit.Module ( Module )
import GHC.Utils.Outputable
import GHC.Platform
import GHC.Core.Ppr( )
import GHC.Builtin.PrimOps ( PrimOp, PrimCall )
import GHC.Core.TyCon ( PrimRep(..), TyCon )
import GHC.Core.Type ( Type )
import GHC.Types.RepType ( typePrimRep1 )
import GHC.Utils.Misc
import GHC.Utils.Panic
data GenStgTopBinding pass
= StgTopLifted (GenStgBinding pass)
| StgTopStringLit Id ByteString
data GenStgBinding pass
= StgNonRec (BinderP pass) (GenStgRhs pass)
| StgRec [(BinderP pass, GenStgRhs pass)]
data StgArg
= StgVarArg Id
| StgLitArg Literal
isDllConApp :: DynFlags -> Module -> DataCon -> [StgArg] -> Bool
isDllConApp :: DynFlags -> Module -> DataCon -> [StgArg] -> Bool
isDllConApp DynFlags
dflags Module
this_mod DataCon
con [StgArg]
args
| Bool -> Bool
not (GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_ExternalDynamicRefs DynFlags
dflags) = Bool
False
| Platform -> OS
platformOS Platform
platform forall a. Eq a => a -> a -> Bool
== OS
OSMinGW32
= Platform -> Module -> Name -> Bool
isDynLinkName Platform
platform Module
this_mod (DataCon -> Name
dataConName DataCon
con) Bool -> Bool -> Bool
|| forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any StgArg -> Bool
is_dll_arg [StgArg]
args
| Bool
otherwise = Bool
False
where
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
is_dll_arg :: StgArg -> Bool
is_dll_arg :: StgArg -> Bool
is_dll_arg (StgVarArg Id
v) = PrimRep -> Bool
isAddrRep (HasDebugCallStack => UnaryType -> PrimRep
typePrimRep1 (Id -> UnaryType
idType Id
v))
Bool -> Bool -> Bool
&& Platform -> Module -> Name -> Bool
isDynLinkName Platform
platform Module
this_mod (Id -> Name
idName Id
v)
is_dll_arg StgArg
_ = Bool
False
isAddrRep :: PrimRep -> Bool
isAddrRep :: PrimRep -> Bool
isAddrRep PrimRep
AddrRep = Bool
True
isAddrRep PrimRep
LiftedRep = Bool
True
isAddrRep PrimRep
UnliftedRep = Bool
True
isAddrRep PrimRep
_ = Bool
False
stgArgType :: StgArg -> Type
stgArgType :: StgArg -> UnaryType
stgArgType (StgVarArg Id
v) = Id -> UnaryType
idType Id
v
stgArgType (StgLitArg Literal
lit) = Literal -> UnaryType
literalType Literal
lit
stripStgTicksTop :: (StgTickish -> Bool) -> GenStgExpr p -> ([StgTickish], GenStgExpr p)
stripStgTicksTop :: forall (p :: StgPass).
(StgTickish -> Bool)
-> GenStgExpr p -> ([StgTickish], GenStgExpr p)
stripStgTicksTop StgTickish -> Bool
p = [StgTickish] -> GenStgExpr p -> ([StgTickish], GenStgExpr p)
go []
where go :: [StgTickish] -> GenStgExpr p -> ([StgTickish], GenStgExpr p)
go [StgTickish]
ts (StgTick StgTickish
t GenStgExpr p
e) | StgTickish -> Bool
p StgTickish
t = [StgTickish] -> GenStgExpr p -> ([StgTickish], GenStgExpr p)
go (StgTickish
tforall a. a -> [a] -> [a]
:[StgTickish]
ts) GenStgExpr p
e
go [StgTickish]
ts GenStgExpr p
other = (forall a. [a] -> [a]
reverse [StgTickish]
ts, GenStgExpr p
other)
stripStgTicksTopE :: (StgTickish -> Bool) -> GenStgExpr p -> GenStgExpr p
stripStgTicksTopE :: forall (p :: StgPass).
(StgTickish -> Bool) -> GenStgExpr p -> GenStgExpr p
stripStgTicksTopE StgTickish -> Bool
p = GenStgExpr p -> GenStgExpr p
go
where go :: GenStgExpr p -> GenStgExpr p
go (StgTick StgTickish
t GenStgExpr p
e) | StgTickish -> Bool
p StgTickish
t = GenStgExpr p -> GenStgExpr p
go GenStgExpr p
e
go GenStgExpr p
other = GenStgExpr p
other
stgCaseBndrInScope :: AltType -> Bool -> Bool
stgCaseBndrInScope :: AltType -> Bool -> Bool
stgCaseBndrInScope AltType
alt_ty Bool
unarised =
case AltType
alt_ty of
AlgAlt TyCon
_ -> Bool
True
PrimAlt PrimRep
_ -> Bool
True
MultiValAlt Int
_ -> Bool -> Bool
not Bool
unarised
AltType
PolyAlt -> Bool
True
data GenStgExpr pass
= StgApp
Id
[StgArg]
| StgLit Literal
| StgConApp DataCon
(XConApp pass)
[StgArg]
[Type]
| StgOpApp StgOp
[StgArg]
Type
| StgCase
(GenStgExpr pass)
(BinderP pass)
AltType
[GenStgAlt pass]
| StgLet
(XLet pass)
(GenStgBinding pass)
(GenStgExpr pass)
| StgLetNoEscape
(XLetNoEscape pass)
(GenStgBinding pass)
(GenStgExpr pass)
| StgTick
StgTickish
(GenStgExpr pass)
data GenStgRhs pass
= StgRhsClosure
(XRhsClosure pass)
CostCentreStack
!UpdateFlag
[BinderP pass]
(GenStgExpr pass)
| StgRhsCon
CostCentreStack
DataCon
ConstructorNumber
[StgTickish]
[StgArg]
data StgPass
= Vanilla
| LiftLams
| CodeGen
data NoExtFieldSilent = NoExtFieldSilent
deriving (Typeable NoExtFieldSilent
NoExtFieldSilent -> DataType
NoExtFieldSilent -> Constr
(forall b. Data b => b -> b)
-> NoExtFieldSilent -> NoExtFieldSilent
forall a.
Typeable a
-> (forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
(r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
(r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int -> (forall d. Data d => d -> u) -> NoExtFieldSilent -> u
forall u. (forall d. Data d => d -> u) -> NoExtFieldSilent -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> NoExtFieldSilent -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> NoExtFieldSilent -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> NoExtFieldSilent -> m NoExtFieldSilent
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> NoExtFieldSilent -> m NoExtFieldSilent
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c NoExtFieldSilent
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> NoExtFieldSilent -> c NoExtFieldSilent
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c NoExtFieldSilent)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c NoExtFieldSilent)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> NoExtFieldSilent -> m NoExtFieldSilent
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> NoExtFieldSilent -> m NoExtFieldSilent
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> NoExtFieldSilent -> m NoExtFieldSilent
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> NoExtFieldSilent -> m NoExtFieldSilent
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> NoExtFieldSilent -> m NoExtFieldSilent
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> NoExtFieldSilent -> m NoExtFieldSilent
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> NoExtFieldSilent -> u
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> NoExtFieldSilent -> u
gmapQ :: forall u. (forall d. Data d => d -> u) -> NoExtFieldSilent -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> NoExtFieldSilent -> [u]
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> NoExtFieldSilent -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> NoExtFieldSilent -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> NoExtFieldSilent -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> NoExtFieldSilent -> r
gmapT :: (forall b. Data b => b -> b)
-> NoExtFieldSilent -> NoExtFieldSilent
$cgmapT :: (forall b. Data b => b -> b)
-> NoExtFieldSilent -> NoExtFieldSilent
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c NoExtFieldSilent)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c NoExtFieldSilent)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c NoExtFieldSilent)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c NoExtFieldSilent)
dataTypeOf :: NoExtFieldSilent -> DataType
$cdataTypeOf :: NoExtFieldSilent -> DataType
toConstr :: NoExtFieldSilent -> Constr
$ctoConstr :: NoExtFieldSilent -> Constr
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c NoExtFieldSilent
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c NoExtFieldSilent
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> NoExtFieldSilent -> c NoExtFieldSilent
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> NoExtFieldSilent -> c NoExtFieldSilent
Data, NoExtFieldSilent -> NoExtFieldSilent -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
$c/= :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
== :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
$c== :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
Eq, Eq NoExtFieldSilent
NoExtFieldSilent -> NoExtFieldSilent -> Bool
NoExtFieldSilent -> NoExtFieldSilent -> Ordering
NoExtFieldSilent -> NoExtFieldSilent -> NoExtFieldSilent
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 :: NoExtFieldSilent -> NoExtFieldSilent -> NoExtFieldSilent
$cmin :: NoExtFieldSilent -> NoExtFieldSilent -> NoExtFieldSilent
max :: NoExtFieldSilent -> NoExtFieldSilent -> NoExtFieldSilent
$cmax :: NoExtFieldSilent -> NoExtFieldSilent -> NoExtFieldSilent
>= :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
$c>= :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
> :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
$c> :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
<= :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
$c<= :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
< :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
$c< :: NoExtFieldSilent -> NoExtFieldSilent -> Bool
compare :: NoExtFieldSilent -> NoExtFieldSilent -> Ordering
$ccompare :: NoExtFieldSilent -> NoExtFieldSilent -> Ordering
Ord)
instance Outputable NoExtFieldSilent where
ppr :: NoExtFieldSilent -> SDoc
ppr NoExtFieldSilent
_ = SDoc
empty
noExtFieldSilent :: NoExtFieldSilent
noExtFieldSilent :: NoExtFieldSilent
noExtFieldSilent = NoExtFieldSilent
NoExtFieldSilent
type family BinderP (pass :: StgPass)
type instance BinderP 'Vanilla = Id
type instance BinderP 'CodeGen = Id
type family XRhsClosure (pass :: StgPass)
type instance XRhsClosure 'Vanilla = NoExtFieldSilent
type instance XRhsClosure 'CodeGen = DIdSet
type family XLet (pass :: StgPass)
type instance XLet 'Vanilla = NoExtFieldSilent
type instance XLet 'CodeGen = NoExtFieldSilent
type family XConApp (pass :: StgPass)
type instance XConApp 'Vanilla = ConstructorNumber
type instance XConApp 'CodeGen = ConstructorNumber
data ConstructorNumber =
NoNumber | Numbered Int
instance Outputable ConstructorNumber where
ppr :: ConstructorNumber -> SDoc
ppr ConstructorNumber
NoNumber = SDoc
empty
ppr (Numbered Int
n) = String -> SDoc
text String
"#" SDoc -> SDoc -> SDoc
<> forall a. Outputable a => a -> SDoc
ppr Int
n
type family XLetNoEscape (pass :: StgPass)
type instance XLetNoEscape 'Vanilla = NoExtFieldSilent
type instance XLetNoEscape 'CodeGen = NoExtFieldSilent
stgRhsArity :: StgRhs -> Int
stgRhsArity :: StgRhs -> Int
stgRhsArity (StgRhsClosure XRhsClosure 'Vanilla
_ CostCentreStack
_ UpdateFlag
_ [BinderP 'Vanilla]
bndrs GenStgExpr 'Vanilla
_)
= ASSERT( all isId bndrs ) length bndrs
stgRhsArity (StgRhsCon CostCentreStack
_ DataCon
_ ConstructorNumber
_ [StgTickish]
_ [StgArg]
_) = Int
0
freeVarsOfRhs :: (XRhsClosure pass ~ DIdSet) => GenStgRhs pass -> DIdSet
freeVarsOfRhs :: forall (pass :: StgPass).
(XRhsClosure pass ~ DIdSet) =>
GenStgRhs pass -> DIdSet
freeVarsOfRhs (StgRhsCon CostCentreStack
_ DataCon
_ ConstructorNumber
_ [StgTickish]
_ [StgArg]
args) = [Id] -> DIdSet
mkDVarSet [ Id
id | StgVarArg Id
id <- [StgArg]
args ]
freeVarsOfRhs (StgRhsClosure XRhsClosure pass
fvs CostCentreStack
_ UpdateFlag
_ [BinderP pass]
_ GenStgExpr pass
_) = XRhsClosure pass
fvs
type GenStgAlt pass
= (AltCon,
[BinderP pass],
GenStgExpr pass)
data AltType
= PolyAlt
| MultiValAlt Int
| AlgAlt TyCon
| PrimAlt PrimRep
type StgTopBinding = GenStgTopBinding 'Vanilla
type StgBinding = GenStgBinding 'Vanilla
type StgExpr = GenStgExpr 'Vanilla
type StgRhs = GenStgRhs 'Vanilla
type StgAlt = GenStgAlt 'Vanilla
type LlStgTopBinding = GenStgTopBinding 'LiftLams
type LlStgBinding = GenStgBinding 'LiftLams
type LlStgExpr = GenStgExpr 'LiftLams
type LlStgRhs = GenStgRhs 'LiftLams
type LlStgAlt = GenStgAlt 'LiftLams
type CgStgTopBinding = GenStgTopBinding 'CodeGen
type CgStgBinding = GenStgBinding 'CodeGen
type CgStgExpr = GenStgExpr 'CodeGen
type CgStgRhs = GenStgRhs 'CodeGen
type CgStgAlt = GenStgAlt 'CodeGen
type InStgTopBinding = StgTopBinding
type InStgBinding = StgBinding
type InStgArg = StgArg
type InStgExpr = StgExpr
type InStgRhs = StgRhs
type InStgAlt = StgAlt
type OutStgTopBinding = StgTopBinding
type OutStgBinding = StgBinding
type OutStgArg = StgArg
type OutStgExpr = StgExpr
type OutStgRhs = StgRhs
type OutStgAlt = StgAlt
data UpdateFlag = ReEntrant | Updatable | SingleEntry
instance Outputable UpdateFlag where
ppr :: UpdateFlag -> SDoc
ppr UpdateFlag
u = Char -> SDoc
char forall a b. (a -> b) -> a -> b
$ case UpdateFlag
u of
UpdateFlag
ReEntrant -> Char
'r'
UpdateFlag
Updatable -> Char
'u'
UpdateFlag
SingleEntry -> Char
's'
isUpdatable :: UpdateFlag -> Bool
isUpdatable :: UpdateFlag -> Bool
isUpdatable UpdateFlag
ReEntrant = Bool
False
isUpdatable UpdateFlag
SingleEntry = Bool
False
isUpdatable UpdateFlag
Updatable = Bool
True
data StgOp
= StgPrimOp PrimOp
| StgPrimCallOp PrimCall
| StgFCallOp ForeignCall Type
bindersOf :: BinderP a ~ Id => GenStgBinding a -> [Id]
bindersOf :: forall (a :: StgPass). (BinderP a ~ Id) => GenStgBinding a -> [Id]
bindersOf (StgNonRec BinderP a
binder GenStgRhs a
_) = [BinderP a
binder]
bindersOf (StgRec [(BinderP a, GenStgRhs a)]
pairs) = [Id
binder | (Id
binder, GenStgRhs a
_) <- [(BinderP a, GenStgRhs a)]
pairs]
bindersOfTop :: BinderP a ~ Id => GenStgTopBinding a -> [Id]
bindersOfTop :: forall (a :: StgPass).
(BinderP a ~ Id) =>
GenStgTopBinding a -> [Id]
bindersOfTop (StgTopLifted GenStgBinding a
bind) = forall (a :: StgPass). (BinderP a ~ Id) => GenStgBinding a -> [Id]
bindersOf GenStgBinding a
bind
bindersOfTop (StgTopStringLit Id
binder ByteString
_) = [Id
binder]
bindersOfTopBinds :: BinderP a ~ Id => [GenStgTopBinding a] -> [Id]
bindersOfTopBinds :: forall (a :: StgPass).
(BinderP a ~ Id) =>
[GenStgTopBinding a] -> [Id]
bindersOfTopBinds = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (forall a. [a] -> [a] -> [a]
(++) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: StgPass).
(BinderP a ~ Id) =>
GenStgTopBinding a -> [Id]
bindersOfTop) []
type OutputablePass pass =
( Outputable (XLet pass)
, Outputable (XConApp pass)
, Outputable (XLetNoEscape pass)
, Outputable (XRhsClosure pass)
, OutputableBndr (BinderP pass)
)
data StgPprOpts = StgPprOpts
{ StgPprOpts -> Bool
stgSccEnabled :: !Bool
}
initStgPprOpts :: DynFlags -> StgPprOpts
initStgPprOpts :: DynFlags -> StgPprOpts
initStgPprOpts DynFlags
dflags = StgPprOpts
{ stgSccEnabled :: Bool
stgSccEnabled = DynFlags -> Bool
sccProfilingEnabled DynFlags
dflags
}
panicStgPprOpts :: StgPprOpts
panicStgPprOpts :: StgPprOpts
panicStgPprOpts = StgPprOpts
{ stgSccEnabled :: Bool
stgSccEnabled = Bool
True
}
shortStgPprOpts :: StgPprOpts
shortStgPprOpts :: StgPprOpts
shortStgPprOpts = StgPprOpts
{ stgSccEnabled :: Bool
stgSccEnabled = Bool
False
}
pprGenStgTopBinding
:: OutputablePass pass => StgPprOpts -> GenStgTopBinding pass -> SDoc
pprGenStgTopBinding :: forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgTopBinding pass -> SDoc
pprGenStgTopBinding StgPprOpts
opts GenStgTopBinding pass
b = case GenStgTopBinding pass
b of
StgTopStringLit Id
bndr ByteString
str -> SDoc -> Int -> SDoc -> SDoc
hang ([SDoc] -> SDoc
hsep [forall a. OutputableBndr a => BindingSite -> a -> SDoc
pprBndr BindingSite
LetBind Id
bndr, SDoc
equals]) Int
4 (ByteString -> SDoc
pprHsBytes ByteString
str SDoc -> SDoc -> SDoc
<> SDoc
semi)
StgTopLifted GenStgBinding pass
bind -> forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgBinding pass -> SDoc
pprGenStgBinding StgPprOpts
opts GenStgBinding pass
bind
pprGenStgBinding :: OutputablePass pass => StgPprOpts -> GenStgBinding pass -> SDoc
pprGenStgBinding :: forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgBinding pass -> SDoc
pprGenStgBinding StgPprOpts
opts GenStgBinding pass
b = case GenStgBinding pass
b of
StgNonRec BinderP pass
bndr GenStgRhs pass
rhs -> SDoc -> Int -> SDoc -> SDoc
hang ([SDoc] -> SDoc
hsep [forall a. OutputableBndr a => BindingSite -> a -> SDoc
pprBndr BindingSite
LetBind BinderP pass
bndr, SDoc
equals]) Int
4 (forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgRhs pass -> SDoc
pprStgRhs StgPprOpts
opts GenStgRhs pass
rhs SDoc -> SDoc -> SDoc
<> SDoc
semi)
StgRec [(BinderP pass, GenStgRhs pass)]
pairs -> [SDoc] -> SDoc
vcat [ String -> SDoc
text String
"Rec {"
, [SDoc] -> SDoc
vcat (forall a. a -> [a] -> [a]
intersperse SDoc
blankLine (forall a b. (a -> b) -> [a] -> [b]
map (BinderP pass, GenStgRhs pass) -> SDoc
ppr_bind [(BinderP pass, GenStgRhs pass)]
pairs))
, String -> SDoc
text String
"end Rec }" ]
where
ppr_bind :: (BinderP pass, GenStgRhs pass) -> SDoc
ppr_bind (BinderP pass
bndr, GenStgRhs pass
expr)
= SDoc -> Int -> SDoc -> SDoc
hang ([SDoc] -> SDoc
hsep [forall a. OutputableBndr a => BindingSite -> a -> SDoc
pprBndr BindingSite
LetBind BinderP pass
bndr, SDoc
equals])
Int
4 (forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgRhs pass -> SDoc
pprStgRhs StgPprOpts
opts GenStgRhs pass
expr SDoc -> SDoc -> SDoc
<> SDoc
semi)
pprGenStgTopBindings :: (OutputablePass pass) => StgPprOpts -> [GenStgTopBinding pass] -> SDoc
pprGenStgTopBindings :: forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> [GenStgTopBinding pass] -> SDoc
pprGenStgTopBindings StgPprOpts
opts [GenStgTopBinding pass]
binds
= [SDoc] -> SDoc
vcat forall a b. (a -> b) -> a -> b
$ forall a. a -> [a] -> [a]
intersperse SDoc
blankLine (forall a b. (a -> b) -> [a] -> [b]
map (forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgTopBinding pass -> SDoc
pprGenStgTopBinding StgPprOpts
opts) [GenStgTopBinding pass]
binds)
pprStgBinding :: StgPprOpts -> StgBinding -> SDoc
pprStgBinding :: StgPprOpts -> StgBinding -> SDoc
pprStgBinding = forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgBinding pass -> SDoc
pprGenStgBinding
pprStgTopBinding :: StgPprOpts -> StgTopBinding -> SDoc
pprStgTopBinding :: StgPprOpts -> StgTopBinding -> SDoc
pprStgTopBinding = forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgTopBinding pass -> SDoc
pprGenStgTopBinding
pprStgTopBindings :: StgPprOpts -> [StgTopBinding] -> SDoc
pprStgTopBindings :: StgPprOpts -> [StgTopBinding] -> SDoc
pprStgTopBindings = forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> [GenStgTopBinding pass] -> SDoc
pprGenStgTopBindings
instance Outputable StgArg where
ppr :: StgArg -> SDoc
ppr = StgArg -> SDoc
pprStgArg
pprStgArg :: StgArg -> SDoc
pprStgArg :: StgArg -> SDoc
pprStgArg (StgVarArg Id
var) = forall a. Outputable a => a -> SDoc
ppr Id
var
pprStgArg (StgLitArg Literal
con) = forall a. Outputable a => a -> SDoc
ppr Literal
con
pprStgExpr :: OutputablePass pass => StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr :: forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
e = case GenStgExpr pass
e of
StgLit Literal
lit -> forall a. Outputable a => a -> SDoc
ppr Literal
lit
StgApp Id
func [StgArg]
args -> SDoc -> Int -> SDoc -> SDoc
hang (forall a. Outputable a => a -> SDoc
ppr Id
func) Int
4 (forall a. Outputable a => [a] -> SDoc
interppSP [StgArg]
args)
StgConApp DataCon
con XConApp pass
n [StgArg]
args [UnaryType]
_ -> [SDoc] -> SDoc
hsep [ forall a. Outputable a => a -> SDoc
ppr DataCon
con, forall a. Outputable a => a -> SDoc
ppr XConApp pass
n, SDoc -> SDoc
brackets (forall a. Outputable a => [a] -> SDoc
interppSP [StgArg]
args) ]
StgOpApp StgOp
op [StgArg]
args UnaryType
_ -> [SDoc] -> SDoc
hsep [ StgOp -> SDoc
pprStgOp StgOp
op, SDoc -> SDoc
brackets (forall a. Outputable a => [a] -> SDoc
interppSP [StgArg]
args)]
StgLet XLet pass
ext GenStgBinding pass
bind expr :: GenStgExpr pass
expr@StgLet{} -> SDoc -> SDoc -> SDoc
($$)
([SDoc] -> SDoc
sep [SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"let" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr XLet pass
ext SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"{")
Int
2 ([SDoc] -> SDoc
hsep [forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgBinding pass -> SDoc
pprGenStgBinding StgPprOpts
opts GenStgBinding pass
bind, String -> SDoc
text String
"} in"])])
(forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
expr)
StgLet XLet pass
ext GenStgBinding pass
bind GenStgExpr pass
expr
-> [SDoc] -> SDoc
sep [ SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"let" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr XLet pass
ext SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"{")
Int
2 (forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgBinding pass -> SDoc
pprGenStgBinding StgPprOpts
opts GenStgBinding pass
bind)
, SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"} in ") Int
2 (forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
expr)
]
StgLetNoEscape XLetNoEscape pass
ext GenStgBinding pass
bind GenStgExpr pass
expr
-> [SDoc] -> SDoc
sep [ SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"let-no-escape" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr XLetNoEscape pass
ext SDoc -> SDoc -> SDoc
<+> String -> SDoc
text String
"{")
Int
2 (forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgBinding pass -> SDoc
pprGenStgBinding StgPprOpts
opts GenStgBinding pass
bind)
, SDoc -> Int -> SDoc -> SDoc
hang (String -> SDoc
text String
"} in ") Int
2 (forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
expr)
]
StgTick StgTickish
_tickish GenStgExpr pass
expr -> forall a. (SDocContext -> a) -> (a -> SDoc) -> SDoc
sdocOption SDocContext -> Bool
sdocSuppressTicks forall a b. (a -> b) -> a -> b
$ \case
Bool
True -> forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
expr
Bool
False -> forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
expr
StgCase GenStgExpr pass
expr BinderP pass
bndr AltType
alt_type [GenStgAlt pass
alt]
-> [SDoc] -> SDoc
sep [ [SDoc] -> SDoc
sep [ String -> SDoc
text String
"case"
, Int -> SDoc -> SDoc
nest Int
4 ([SDoc] -> SDoc
hsep [ forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
expr
, SDoc -> SDoc
whenPprDebug (SDoc
dcolon SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr AltType
alt_type)
])
, String -> SDoc
text String
"of"
, forall a. OutputableBndr a => BindingSite -> a -> SDoc
pprBndr BindingSite
CaseBind BinderP pass
bndr
, Char -> SDoc
char Char
'{'
]
, forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> Bool -> GenStgAlt pass -> SDoc
pprStgAlt StgPprOpts
opts Bool
False GenStgAlt pass
alt
, Char -> SDoc
char Char
'}'
]
StgCase GenStgExpr pass
expr BinderP pass
bndr AltType
alt_type [GenStgAlt pass]
alts
-> [SDoc] -> SDoc
sep [ [SDoc] -> SDoc
sep [ String -> SDoc
text String
"case"
, Int -> SDoc -> SDoc
nest Int
4 ([SDoc] -> SDoc
hsep [ forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
expr
, SDoc -> SDoc
whenPprDebug (SDoc
dcolon SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr AltType
alt_type)
])
, String -> SDoc
text String
"of"
, forall a. OutputableBndr a => BindingSite -> a -> SDoc
pprBndr BindingSite
CaseBind BinderP pass
bndr, Char -> SDoc
char Char
'{'
]
, Int -> SDoc -> SDoc
nest Int
2 ([SDoc] -> SDoc
vcat (forall a b. (a -> b) -> [a] -> [b]
map (forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> Bool -> GenStgAlt pass -> SDoc
pprStgAlt StgPprOpts
opts Bool
True) [GenStgAlt pass]
alts))
, Char -> SDoc
char Char
'}'
]
pprStgAlt :: OutputablePass pass => StgPprOpts -> Bool -> GenStgAlt pass -> SDoc
pprStgAlt :: forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> Bool -> GenStgAlt pass -> SDoc
pprStgAlt StgPprOpts
opts Bool
indent (AltCon
con, [BinderP pass]
params, GenStgExpr pass
expr)
| Bool
indent = SDoc -> Int -> SDoc -> SDoc
hang SDoc
altPattern Int
4 (forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
expr SDoc -> SDoc -> SDoc
<> SDoc
semi)
| Bool
otherwise = [SDoc] -> SDoc
sep [SDoc
altPattern, forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
expr SDoc -> SDoc -> SDoc
<> SDoc
semi]
where
altPattern :: SDoc
altPattern = ([SDoc] -> SDoc
hsep [forall a. Outputable a => a -> SDoc
ppr AltCon
con, [SDoc] -> SDoc
sep (forall a b. (a -> b) -> [a] -> [b]
map (forall a. OutputableBndr a => BindingSite -> a -> SDoc
pprBndr BindingSite
CasePatBind) [BinderP pass]
params), String -> SDoc
text String
"->"])
pprStgOp :: StgOp -> SDoc
pprStgOp :: StgOp -> SDoc
pprStgOp (StgPrimOp PrimOp
op) = forall a. Outputable a => a -> SDoc
ppr PrimOp
op
pprStgOp (StgPrimCallOp PrimCall
op)= forall a. Outputable a => a -> SDoc
ppr PrimCall
op
pprStgOp (StgFCallOp ForeignCall
op UnaryType
_) = forall a. Outputable a => a -> SDoc
ppr ForeignCall
op
instance Outputable AltType where
ppr :: AltType -> SDoc
ppr AltType
PolyAlt = String -> SDoc
text String
"Polymorphic"
ppr (MultiValAlt Int
n) = String -> SDoc
text String
"MultiAlt" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr Int
n
ppr (AlgAlt TyCon
tc) = String -> SDoc
text String
"Alg" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr TyCon
tc
ppr (PrimAlt PrimRep
tc) = String -> SDoc
text String
"Prim" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr PrimRep
tc
pprStgRhs :: OutputablePass pass => StgPprOpts -> GenStgRhs pass -> SDoc
pprStgRhs :: forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgRhs pass -> SDoc
pprStgRhs StgPprOpts
opts GenStgRhs pass
rhs = case GenStgRhs pass
rhs of
StgRhsClosure XRhsClosure pass
ext CostCentreStack
cc UpdateFlag
upd_flag [BinderP pass]
args GenStgExpr pass
body
-> SDoc -> Int -> SDoc -> SDoc
hang ([SDoc] -> SDoc
hsep [ if StgPprOpts -> Bool
stgSccEnabled StgPprOpts
opts then forall a. Outputable a => a -> SDoc
ppr CostCentreStack
cc else SDoc
empty
, (SDocContext -> Bool) -> SDoc -> SDoc
ppUnlessOption SDocContext -> Bool
sdocSuppressStgExts (forall a. Outputable a => a -> SDoc
ppr XRhsClosure pass
ext)
, Char -> SDoc
char Char
'\\' SDoc -> SDoc -> SDoc
<> forall a. Outputable a => a -> SDoc
ppr UpdateFlag
upd_flag, SDoc -> SDoc
brackets (forall a. Outputable a => [a] -> SDoc
interppSP [BinderP pass]
args)
])
Int
4 (forall (pass :: StgPass).
OutputablePass pass =>
StgPprOpts -> GenStgExpr pass -> SDoc
pprStgExpr StgPprOpts
opts GenStgExpr pass
body)
StgRhsCon CostCentreStack
cc DataCon
con ConstructorNumber
mid [StgTickish]
_ticks [StgArg]
args
-> [SDoc] -> SDoc
hcat [ forall a. Outputable a => a -> SDoc
ppr CostCentreStack
cc, SDoc
space
, case ConstructorNumber
mid of
ConstructorNumber
NoNumber -> SDoc
empty
Numbered Int
n -> [SDoc] -> SDoc
hcat [forall a. Outputable a => a -> SDoc
ppr Int
n, SDoc
space]
, forall a. Outputable a => a -> SDoc
ppr DataCon
con, String -> SDoc
text String
"! ", SDoc -> SDoc
brackets ([SDoc] -> SDoc
sep (forall a b. (a -> b) -> [a] -> [b]
map StgArg -> SDoc
pprStgArg [StgArg]
args))]