{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE TypeFamilies #-}
module GHC.CoreToStg ( coreToStg ) where
#include "HsVersions.h"
import GHC.Prelude
import GHC.Core
import GHC.Core.Utils ( exprType, findDefault, isJoinBind
, exprIsTickedString_maybe )
import GHC.Core.Opt.Arity ( manifestArity )
import GHC.Stg.Syntax
import GHC.Stg.Debug
import GHC.Core.Type
import GHC.Types.RepType
import GHC.Core.TyCon
import GHC.Types.Id.Make ( coercionTokenId )
import GHC.Types.Id
import GHC.Types.Id.Info
import GHC.Core.DataCon
import GHC.Types.CostCentre
import GHC.Types.Tickish
import GHC.Types.Var.Env
import GHC.Unit.Module
import GHC.Types.Name ( isExternalName, nameModule_maybe )
import GHC.Types.Basic ( Arity )
import GHC.Builtin.Types ( unboxedUnitDataCon )
import GHC.Types.Literal
import GHC.Utils.Outputable
import GHC.Utils.Monad
import GHC.Data.FastString
import GHC.Utils.Misc
import GHC.Utils.Panic
import GHC.Driver.Session
import GHC.Platform.Ways
import GHC.Driver.Ppr
import GHC.Types.ForeignCall
import GHC.Types.IPE
import GHC.Types.Demand ( isUsedOnceDmd )
import GHC.Builtin.PrimOps ( PrimCall(..) )
import GHC.Types.SrcLoc ( mkGeneralSrcSpan )
import Control.Monad (ap)
import Data.Maybe (fromMaybe)
import Data.Tuple (swap)
import qualified Data.Set as Set
coreToStg :: DynFlags -> Module -> ModLocation -> CoreProgram
-> ([StgTopBinding], InfoTableProvMap, CollectedCCs)
coreToStg :: DynFlags
-> Module
-> ModLocation
-> CoreProgram
-> ([StgTopBinding], InfoTableProvMap, CollectedCCs)
coreToStg DynFlags
dflags Module
this_mod ModLocation
ml CoreProgram
pgm
= ([StgTopBinding]
pgm'', InfoTableProvMap
denv, CollectedCCs
final_ccs)
where
(IdEnv HowBound
_, ([CostCentre]
local_ccs, [CostCentreStack]
local_cc_stacks), [StgTopBinding]
pgm')
= DynFlags
-> Module
-> IdEnv HowBound
-> CollectedCCs
-> CoreProgram
-> (IdEnv HowBound, CollectedCCs, [StgTopBinding])
coreTopBindsToStg DynFlags
dflags Module
this_mod forall a. VarEnv a
emptyVarEnv CollectedCCs
emptyCollectedCCs CoreProgram
pgm
(![StgTopBinding]
pgm'', !InfoTableProvMap
denv) =
if GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_InfoTableMap DynFlags
dflags
then DynFlags
-> ModLocation
-> [StgTopBinding]
-> ([StgTopBinding], InfoTableProvMap)
collectDebugInformation DynFlags
dflags ModLocation
ml [StgTopBinding]
pgm'
else ([StgTopBinding]
pgm', InfoTableProvMap
emptyInfoTableProvMap)
prof :: Bool
prof = Way
WayProf forall a. Ord a => a -> Set a -> Bool
`Set.member` DynFlags -> Ways
ways DynFlags
dflags
final_ccs :: CollectedCCs
final_ccs
| Bool
prof Bool -> Bool -> Bool
&& GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_AutoSccsOnIndividualCafs DynFlags
dflags
= ([CostCentre]
local_ccs,[CostCentreStack]
local_cc_stacks)
| Bool
prof
= (CostCentre
all_cafs_ccforall a. a -> [a] -> [a]
:[CostCentre]
local_ccs, CostCentreStack
all_cafs_ccsforall a. a -> [a] -> [a]
:[CostCentreStack]
local_cc_stacks)
| Bool
otherwise
= CollectedCCs
emptyCollectedCCs
(CostCentre
all_cafs_cc, CostCentreStack
all_cafs_ccs) = Module -> (CostCentre, CostCentreStack)
getAllCAFsCC Module
this_mod
coreTopBindsToStg
:: DynFlags
-> Module
-> IdEnv HowBound
-> CollectedCCs
-> CoreProgram
-> (IdEnv HowBound, CollectedCCs, [StgTopBinding])
coreTopBindsToStg :: DynFlags
-> Module
-> IdEnv HowBound
-> CollectedCCs
-> CoreProgram
-> (IdEnv HowBound, CollectedCCs, [StgTopBinding])
coreTopBindsToStg DynFlags
_ Module
_ IdEnv HowBound
env CollectedCCs
ccs []
= (IdEnv HowBound
env, CollectedCCs
ccs, [])
coreTopBindsToStg DynFlags
dflags Module
this_mod IdEnv HowBound
env CollectedCCs
ccs (CoreBind
b:CoreProgram
bs)
| NonRec Id
_ CoreArg
rhs <- CoreBind
b, forall b. Expr b -> Bool
isTyCoArg CoreArg
rhs
= DynFlags
-> Module
-> IdEnv HowBound
-> CollectedCCs
-> CoreProgram
-> (IdEnv HowBound, CollectedCCs, [StgTopBinding])
coreTopBindsToStg DynFlags
dflags Module
this_mod IdEnv HowBound
env1 CollectedCCs
ccs1 CoreProgram
bs
| Bool
otherwise
= (IdEnv HowBound
env2, CollectedCCs
ccs2, StgTopBinding
b'forall a. a -> [a] -> [a]
:[StgTopBinding]
bs')
where
(IdEnv HowBound
env1, CollectedCCs
ccs1, StgTopBinding
b' ) = DynFlags
-> Module
-> IdEnv HowBound
-> CollectedCCs
-> CoreBind
-> (IdEnv HowBound, CollectedCCs, StgTopBinding)
coreTopBindToStg DynFlags
dflags Module
this_mod IdEnv HowBound
env CollectedCCs
ccs CoreBind
b
(IdEnv HowBound
env2, CollectedCCs
ccs2, [StgTopBinding]
bs') = DynFlags
-> Module
-> IdEnv HowBound
-> CollectedCCs
-> CoreProgram
-> (IdEnv HowBound, CollectedCCs, [StgTopBinding])
coreTopBindsToStg DynFlags
dflags Module
this_mod IdEnv HowBound
env1 CollectedCCs
ccs1 CoreProgram
bs
coreTopBindToStg
:: DynFlags
-> Module
-> IdEnv HowBound
-> CollectedCCs
-> CoreBind
-> (IdEnv HowBound, CollectedCCs, StgTopBinding)
coreTopBindToStg :: DynFlags
-> Module
-> IdEnv HowBound
-> CollectedCCs
-> CoreBind
-> (IdEnv HowBound, CollectedCCs, StgTopBinding)
coreTopBindToStg DynFlags
_ Module
_ IdEnv HowBound
env CollectedCCs
ccs (NonRec Id
id CoreArg
e)
| Just ByteString
str <- CoreArg -> Maybe ByteString
exprIsTickedString_maybe CoreArg
e
= let
env' :: IdEnv HowBound
env' = forall a. VarEnv a -> Id -> a -> VarEnv a
extendVarEnv IdEnv HowBound
env Id
id HowBound
how_bound
how_bound :: HowBound
how_bound = LetInfo -> JoinArity -> HowBound
LetBound LetInfo
TopLet JoinArity
0
in (IdEnv HowBound
env', CollectedCCs
ccs, forall (pass :: StgPass). Id -> ByteString -> GenStgTopBinding pass
StgTopStringLit Id
id ByteString
str)
coreTopBindToStg DynFlags
dflags Module
this_mod IdEnv HowBound
env CollectedCCs
ccs (NonRec Id
id CoreArg
rhs)
= let
env' :: IdEnv HowBound
env' = forall a. VarEnv a -> Id -> a -> VarEnv a
extendVarEnv IdEnv HowBound
env Id
id HowBound
how_bound
how_bound :: HowBound
how_bound = LetInfo -> JoinArity -> HowBound
LetBound LetInfo
TopLet forall a b. (a -> b) -> a -> b
$! CoreArg -> JoinArity
manifestArity CoreArg
rhs
(StgRhs
stg_rhs, CollectedCCs
ccs') =
forall a. DynFlags -> IdEnv HowBound -> CtsM a -> a
initCts DynFlags
dflags IdEnv HowBound
env forall a b. (a -> b) -> a -> b
$
DynFlags
-> CollectedCCs
-> Module
-> (Id, CoreArg)
-> CtsM (StgRhs, CollectedCCs)
coreToTopStgRhs DynFlags
dflags CollectedCCs
ccs Module
this_mod (Id
id,CoreArg
rhs)
bind :: StgTopBinding
bind = forall (pass :: StgPass).
GenStgBinding pass -> GenStgTopBinding pass
StgTopLifted forall a b. (a -> b) -> a -> b
$ forall (pass :: StgPass).
BinderP pass -> GenStgRhs pass -> GenStgBinding pass
StgNonRec Id
id StgRhs
stg_rhs
in
(IdEnv HowBound
env', CollectedCCs
ccs', StgTopBinding
bind)
coreTopBindToStg DynFlags
dflags Module
this_mod IdEnv HowBound
env CollectedCCs
ccs (Rec [(Id, CoreArg)]
pairs)
= ASSERT( not (null pairs) )
let
binders :: [Id]
binders = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(Id, CoreArg)]
pairs
extra_env' :: [(Id, HowBound)]
extra_env' = [ (Id
b, LetInfo -> JoinArity -> HowBound
LetBound LetInfo
TopLet forall a b. (a -> b) -> a -> b
$! CoreArg -> JoinArity
manifestArity CoreArg
rhs)
| (Id
b, CoreArg
rhs) <- [(Id, CoreArg)]
pairs ]
env' :: IdEnv HowBound
env' = forall a. VarEnv a -> [(Id, a)] -> VarEnv a
extendVarEnvList IdEnv HowBound
env [(Id, HowBound)]
extra_env'
(CollectedCCs
ccs', [StgRhs]
stg_rhss)
= forall a. DynFlags -> IdEnv HowBound -> CtsM a -> a
initCts DynFlags
dflags IdEnv HowBound
env' forall a b. (a -> b) -> a -> b
$
forall (m :: * -> *) acc x y.
Monad m =>
(acc -> x -> m (acc, y)) -> acc -> [x] -> m (acc, [y])
mapAccumLM (\CollectedCCs
ccs (Id, CoreArg)
rhs -> forall a b. (a, b) -> (b, a)
swap forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DynFlags
-> CollectedCCs
-> Module
-> (Id, CoreArg)
-> CtsM (StgRhs, CollectedCCs)
coreToTopStgRhs DynFlags
dflags CollectedCCs
ccs Module
this_mod (Id, CoreArg)
rhs)
CollectedCCs
ccs
[(Id, CoreArg)]
pairs
bind :: StgTopBinding
bind = forall (pass :: StgPass).
GenStgBinding pass -> GenStgTopBinding pass
StgTopLifted forall a b. (a -> b) -> a -> b
$ forall (pass :: StgPass).
[(BinderP pass, GenStgRhs pass)] -> GenStgBinding pass
StgRec (forall a b. [a] -> [b] -> [(a, b)]
zip [Id]
binders [StgRhs]
stg_rhss)
in
(IdEnv HowBound
env', CollectedCCs
ccs', StgTopBinding
bind)
coreToTopStgRhs
:: DynFlags
-> CollectedCCs
-> Module
-> (Id,CoreExpr)
-> CtsM (StgRhs, CollectedCCs)
coreToTopStgRhs :: DynFlags
-> CollectedCCs
-> Module
-> (Id, CoreArg)
-> CtsM (StgRhs, CollectedCCs)
coreToTopStgRhs DynFlags
dflags CollectedCCs
ccs Module
this_mod (Id
bndr, CoreArg
rhs)
= do { PreStgRhs
new_rhs <- HasDebugCallStack => CoreArg -> CtsM PreStgRhs
coreToPreStgRhs CoreArg
rhs
; let (StgRhs
stg_rhs, CollectedCCs
ccs') =
DynFlags
-> Module
-> CollectedCCs
-> Id
-> PreStgRhs
-> (StgRhs, CollectedCCs)
mkTopStgRhs DynFlags
dflags Module
this_mod CollectedCCs
ccs Id
bndr PreStgRhs
new_rhs
stg_arity :: JoinArity
stg_arity =
StgRhs -> JoinArity
stgRhsArity StgRhs
stg_rhs
; forall (m :: * -> *) a. Monad m => a -> m a
return (ASSERT2( arity_ok stg_arity, mk_arity_msg stg_arity) stg_rhs,
CollectedCCs
ccs') }
where
arity_ok :: JoinArity -> Bool
arity_ok JoinArity
stg_arity
| Name -> Bool
isExternalName (Id -> Name
idName Id
bndr) = JoinArity
id_arity forall a. Eq a => a -> a -> Bool
== JoinArity
stg_arity
| Bool
otherwise = Bool
True
id_arity :: JoinArity
id_arity = Id -> JoinArity
idArity Id
bndr
mk_arity_msg :: JoinArity -> SDoc
mk_arity_msg JoinArity
stg_arity
= [SDoc] -> SDoc
vcat [forall a. Outputable a => a -> SDoc
ppr Id
bndr,
String -> SDoc
text String
"Id arity:" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr JoinArity
id_arity,
String -> SDoc
text String
"STG arity:" SDoc -> SDoc -> SDoc
<+> forall a. Outputable a => a -> SDoc
ppr JoinArity
stg_arity]
coreToStgExpr
:: HasDebugCallStack => CoreExpr
-> CtsM StgExpr
coreToStgExpr :: HasDebugCallStack => CoreArg -> CtsM StgExpr
coreToStgExpr (Lit (LitNumber LitNumType
LitNumInteger Integer
_)) = forall a. String -> a
panic String
"coreToStgExpr: LitInteger"
coreToStgExpr (Lit (LitNumber LitNumType
LitNumNatural Integer
_)) = forall a. String -> a
panic String
"coreToStgExpr: LitNatural"
coreToStgExpr (Lit Literal
l) = forall (m :: * -> *) a. Monad m => a -> m a
return (forall (pass :: StgPass). Literal -> GenStgExpr pass
StgLit Literal
l)
coreToStgExpr (Var Id
v) = Id -> [CoreArg] -> [CoreTickish] -> CtsM StgExpr
coreToStgApp Id
v [] []
coreToStgExpr (Coercion Coercion
_)
= Id -> [CoreArg] -> [CoreTickish] -> CtsM StgExpr
coreToStgApp Id
coercionTokenId [] []
coreToStgExpr expr :: CoreArg
expr@(App CoreArg
_ CoreArg
_)
= case CoreArg
app_head of
Var Id
f -> Id -> [CoreArg] -> [CoreTickish] -> CtsM StgExpr
coreToStgApp Id
f [CoreArg]
args [CoreTickish]
ticks
Lit l :: Literal
l@LitRubbish{} -> forall (m :: * -> *) a. Monad m => a -> m a
return (forall (pass :: StgPass). Literal -> GenStgExpr pass
StgLit Literal
l)
CoreArg
_ -> forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"coreToStgExpr - Invalid app head:" (forall a. Outputable a => a -> SDoc
ppr CoreArg
expr)
where
(CoreArg
app_head, [CoreArg]
args, [CoreTickish]
ticks) = HasDebugCallStack => CoreArg -> (CoreArg, [CoreArg], [CoreTickish])
myCollectArgs CoreArg
expr
coreToStgExpr expr :: CoreArg
expr@(Lam Id
_ CoreArg
_)
= let
([Id]
args, CoreArg
body) = CoreArg -> ([Id], CoreArg)
myCollectBinders CoreArg
expr
in
case [Id] -> [Id]
filterStgBinders [Id]
args of
[] -> HasDebugCallStack => CoreArg -> CtsM StgExpr
coreToStgExpr CoreArg
body
[Id]
_ -> forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"coretoStgExpr" forall a b. (a -> b) -> a -> b
$
String -> SDoc
text String
"Unexpected value lambda:" SDoc -> SDoc -> SDoc
$$ forall a. Outputable a => a -> SDoc
ppr CoreArg
expr
coreToStgExpr (Tick CoreTickish
tick CoreArg
expr)
= do
let !stg_tick :: StgTickish
stg_tick = Type -> CoreTickish -> StgTickish
coreToStgTick (CoreArg -> Type
exprType CoreArg
expr) CoreTickish
tick
!StgExpr
expr2 <- HasDebugCallStack => CoreArg -> CtsM StgExpr
coreToStgExpr CoreArg
expr
forall (m :: * -> *) a. Monad m => a -> m a
return (forall (pass :: StgPass).
StgTickish -> GenStgExpr pass -> GenStgExpr pass
StgTick StgTickish
stg_tick StgExpr
expr2)
coreToStgExpr (Cast CoreArg
expr Coercion
_)
= HasDebugCallStack => CoreArg -> CtsM StgExpr
coreToStgExpr CoreArg
expr
coreToStgExpr (Case CoreArg
scrut Id
bndr Type
_ [Alt Id]
alts)
= do { StgExpr
scrut2 <- HasDebugCallStack => CoreArg -> CtsM StgExpr
coreToStgExpr CoreArg
scrut
; [(AltCon, [Id], StgExpr)]
alts2 <- forall a. [(Id, HowBound)] -> CtsM a -> CtsM a
extendVarEnvCts [(Id
bndr, HowBound
LambdaBound)] (forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Alt Id -> CtsM (AltCon, [Id], StgExpr)
vars_alt [Alt Id]
alts)
; forall (m :: * -> *) a. Monad m => a -> m a
return (forall (pass :: StgPass).
GenStgExpr pass
-> BinderP pass -> AltType -> [GenStgAlt pass] -> GenStgExpr pass
StgCase StgExpr
scrut2 Id
bndr (Id -> [Alt Id] -> AltType
mkStgAltType Id
bndr [Alt Id]
alts) [(AltCon, [Id], StgExpr)]
alts2) }
where
vars_alt :: CoreAlt -> CtsM (AltCon, [Var], StgExpr)
vars_alt :: Alt Id -> CtsM (AltCon, [Id], StgExpr)
vars_alt (Alt AltCon
con [Id]
binders CoreArg
rhs)
| DataAlt DataCon
c <- AltCon
con, DataCon
c forall a. Eq a => a -> a -> Bool
== DataCon
unboxedUnitDataCon
=
ASSERT( null binders )
do { StgExpr
rhs2 <- HasDebugCallStack => CoreArg -> CtsM StgExpr
coreToStgExpr CoreArg
rhs
; forall (m :: * -> *) a. Monad m => a -> m a
return (AltCon
DEFAULT, [], StgExpr
rhs2) }
| Bool
otherwise
= let
binders' :: [Id]
binders' = [Id] -> [Id]
filterStgBinders [Id]
binders
in
forall a. [(Id, HowBound)] -> CtsM a -> CtsM a
extendVarEnvCts [(Id
b, HowBound
LambdaBound) | Id
b <- [Id]
binders'] forall a b. (a -> b) -> a -> b
$ do
StgExpr
rhs2 <- HasDebugCallStack => CoreArg -> CtsM StgExpr
coreToStgExpr CoreArg
rhs
forall (m :: * -> *) a. Monad m => a -> m a
return (AltCon
con, [Id]
binders', StgExpr
rhs2)
coreToStgExpr (Let CoreBind
bind CoreArg
body) = CoreBind -> CoreArg -> CtsM StgExpr
coreToStgLet CoreBind
bind CoreArg
body
coreToStgExpr CoreArg
e = forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"coreToStgExpr" (forall a. Outputable a => a -> SDoc
ppr CoreArg
e)
mkStgAltType :: Id -> [CoreAlt] -> AltType
mkStgAltType :: Id -> [Alt Id] -> AltType
mkStgAltType Id
bndr [Alt Id]
alts
| Type -> Bool
isUnboxedTupleType Type
bndr_ty Bool -> Bool -> Bool
|| Type -> Bool
isUnboxedSumType Type
bndr_ty
= JoinArity -> AltType
MultiValAlt (forall (t :: * -> *) a. Foldable t => t a -> JoinArity
length [PrimRep]
prim_reps)
| Bool
otherwise
= case [PrimRep]
prim_reps of
[PrimRep
rep] | PrimRep -> Bool
isGcPtrRep PrimRep
rep ->
case Type -> Maybe TyCon
tyConAppTyCon_maybe (Type -> Type
unwrapType Type
bndr_ty) of
Just TyCon
tc
| TyCon -> Bool
isAbstractTyCon TyCon
tc -> AltType
look_for_better_tycon
| TyCon -> Bool
isAlgTyCon TyCon
tc -> TyCon -> AltType
AlgAlt TyCon
tc
| Bool
otherwise -> ASSERT2( _is_poly_alt_tycon tc, ppr tc )
AltType
PolyAlt
Maybe TyCon
Nothing -> AltType
PolyAlt
[PrimRep
non_gcd] -> PrimRep -> AltType
PrimAlt PrimRep
non_gcd
[PrimRep]
not_unary -> JoinArity -> AltType
MultiValAlt (forall (t :: * -> *) a. Foldable t => t a -> JoinArity
length [PrimRep]
not_unary)
where
bndr_ty :: Type
bndr_ty = Id -> Type
idType Id
bndr
prim_reps :: [PrimRep]
prim_reps = HasDebugCallStack => Type -> [PrimRep]
typePrimRep Type
bndr_ty
_is_poly_alt_tycon :: TyCon -> Bool
_is_poly_alt_tycon TyCon
tc
= TyCon -> Bool
isFunTyCon TyCon
tc
Bool -> Bool -> Bool
|| TyCon -> Bool
isPrimTyCon TyCon
tc
Bool -> Bool -> Bool
|| TyCon -> Bool
isFamilyTyCon TyCon
tc
look_for_better_tycon :: AltType
look_for_better_tycon
| ((Alt (DataAlt DataCon
con) [Id]
_ CoreArg
_) : [Alt Id]
_) <- [Alt Id]
data_alts =
TyCon -> AltType
AlgAlt (DataCon -> TyCon
dataConTyCon DataCon
con)
| Bool
otherwise =
ASSERT(null data_alts)
AltType
PolyAlt
where
([Alt Id]
data_alts, Maybe CoreArg
_deflt) = forall b. [Alt b] -> ([Alt b], Maybe (Expr b))
findDefault [Alt Id]
alts
coreToStgApp :: Id
-> [CoreArg]
-> [CoreTickish]
-> CtsM StgExpr
coreToStgApp :: Id -> [CoreArg] -> [CoreTickish] -> CtsM StgExpr
coreToStgApp Id
f [CoreArg]
args [CoreTickish]
ticks = do
([StgArg]
args', [StgTickish]
ticks') <- [CoreArg] -> CtsM ([StgArg], [StgTickish])
coreToStgArgs [CoreArg]
args
HowBound
how_bound <- Id -> CtsM HowBound
lookupVarCts Id
f
let
n_val_args :: JoinArity
n_val_args = forall b. [Arg b] -> JoinArity
valArgCount [CoreArg]
args
f_arity :: JoinArity
f_arity = Id -> HowBound -> JoinArity
stgArity Id
f HowBound
how_bound
saturated :: Bool
saturated = JoinArity
f_arity forall a. Ord a => a -> a -> Bool
<= JoinArity
n_val_args
res_ty :: Type
res_ty = CoreArg -> Type
exprType (forall b. Expr b -> [Expr b] -> Expr b
mkApps (forall b. Id -> Expr b
Var Id
f) [CoreArg]
args)
app :: StgExpr
app = case Id -> IdDetails
idDetails Id
f of
DataConWorkId DataCon
dc
| Bool
saturated -> forall (pass :: StgPass).
DataCon -> XConApp pass -> [StgArg] -> [Type] -> GenStgExpr pass
StgConApp DataCon
dc ConstructorNumber
NoNumber [StgArg]
args'
([Type] -> [Type]
dropRuntimeRepArgs (forall a. a -> Maybe a -> a
fromMaybe [] (Type -> Maybe [Type]
tyConAppArgs_maybe Type
res_ty)))
PrimOpId PrimOp
op -> ASSERT( saturated )
forall (pass :: StgPass).
StgOp -> [StgArg] -> Type -> GenStgExpr pass
StgOpApp (PrimOp -> StgOp
StgPrimOp PrimOp
op) [StgArg]
args' Type
res_ty
FCallId (CCall (CCallSpec (StaticTarget SourceText
_ CLabelString
lbl (Just Unit
pkgId) Bool
True)
CCallConv
PrimCallConv Safety
_))
-> ASSERT( saturated )
forall (pass :: StgPass).
StgOp -> [StgArg] -> Type -> GenStgExpr pass
StgOpApp (PrimCall -> StgOp
StgPrimCallOp (CLabelString -> Unit -> PrimCall
PrimCall CLabelString
lbl Unit
pkgId)) [StgArg]
args' Type
res_ty
FCallId ForeignCall
call -> ASSERT( saturated )
forall (pass :: StgPass).
StgOp -> [StgArg] -> Type -> GenStgExpr pass
StgOpApp (ForeignCall -> Type -> StgOp
StgFCallOp ForeignCall
call (Id -> Type
idType Id
f)) [StgArg]
args' Type
res_ty
TickBoxOpId {} -> forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"coreToStg TickBox" forall a b. (a -> b) -> a -> b
$ forall a. Outputable a => a -> SDoc
ppr (Id
f,[StgArg]
args')
IdDetails
_other -> forall (pass :: StgPass). Id -> [StgArg] -> GenStgExpr pass
StgApp Id
f [StgArg]
args'
add_tick :: StgTickish -> GenStgExpr pass -> GenStgExpr pass
add_tick !StgTickish
t !GenStgExpr pass
e = forall (pass :: StgPass).
StgTickish -> GenStgExpr pass -> GenStgExpr pass
StgTick StgTickish
t GenStgExpr pass
e
tapp :: StgExpr
tapp = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall (pass :: StgPass).
StgTickish -> GenStgExpr pass -> GenStgExpr pass
add_tick StgExpr
app (forall a b. (a -> b) -> [a] -> [b]
map (Type -> CoreTickish -> StgTickish
coreToStgTick Type
res_ty) [CoreTickish]
ticks forall a. [a] -> [a] -> [a]
++ [StgTickish]
ticks')
StgExpr
app seq :: forall a b. a -> b -> b
`seq` forall (m :: * -> *) a. Monad m => a -> m a
return StgExpr
tapp
coreToStgArgs :: [CoreArg] -> CtsM ([StgArg], [StgTickish])
coreToStgArgs :: [CoreArg] -> CtsM ([StgArg], [StgTickish])
coreToStgArgs []
= forall (m :: * -> *) a. Monad m => a -> m a
return ([], [])
coreToStgArgs (Type Type
_ : [CoreArg]
args) = do
([StgArg]
args', [StgTickish]
ts) <- [CoreArg] -> CtsM ([StgArg], [StgTickish])
coreToStgArgs [CoreArg]
args
forall (m :: * -> *) a. Monad m => a -> m a
return ([StgArg]
args', [StgTickish]
ts)
coreToStgArgs (Coercion Coercion
_ : [CoreArg]
args)
= do { ([StgArg]
args', [StgTickish]
ts) <- [CoreArg] -> CtsM ([StgArg], [StgTickish])
coreToStgArgs [CoreArg]
args
; forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> StgArg
StgVarArg Id
coercionTokenId forall a. a -> [a] -> [a]
: [StgArg]
args', [StgTickish]
ts) }
coreToStgArgs (Tick CoreTickish
t CoreArg
e : [CoreArg]
args)
= ASSERT( not (tickishIsCode t) )
do { ([StgArg]
args', [StgTickish]
ts) <- [CoreArg] -> CtsM ([StgArg], [StgTickish])
coreToStgArgs (CoreArg
e forall a. a -> [a] -> [a]
: [CoreArg]
args)
; let !t' :: StgTickish
t' = Type -> CoreTickish -> StgTickish
coreToStgTick (CoreArg -> Type
exprType CoreArg
e) CoreTickish
t
; forall (m :: * -> *) a. Monad m => a -> m a
return ([StgArg]
args', StgTickish
t'forall a. a -> [a] -> [a]
:[StgTickish]
ts) }
coreToStgArgs (CoreArg
arg : [CoreArg]
args) = do
([StgArg]
stg_args, [StgTickish]
ticks) <- [CoreArg] -> CtsM ([StgArg], [StgTickish])
coreToStgArgs [CoreArg]
args
StgExpr
arg' <- HasDebugCallStack => CoreArg -> CtsM StgExpr
coreToStgExpr CoreArg
arg
let
([StgTickish]
aticks, StgExpr
arg'') = forall (p :: StgPass).
(StgTickish -> Bool)
-> GenStgExpr p -> ([StgTickish], GenStgExpr p)
stripStgTicksTop forall (pass :: TickishPass). GenTickish pass -> Bool
tickishFloatable StgExpr
arg'
stg_arg :: StgArg
stg_arg = case StgExpr
arg'' of
StgApp Id
v [] -> Id -> StgArg
StgVarArg Id
v
StgConApp DataCon
con XConApp 'Vanilla
_ [] [Type]
_ -> Id -> StgArg
StgVarArg (DataCon -> Id
dataConWorkId DataCon
con)
StgLit Literal
lit -> Literal -> StgArg
StgLitArg Literal
lit
StgExpr
_ -> forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"coreToStgArgs" (forall a. Outputable a => a -> SDoc
ppr CoreArg
arg)
Platform
platform <- DynFlags -> Platform
targetPlatform forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: * -> *). HasDynFlags m => m DynFlags
getDynFlags
let
arg_rep :: [PrimRep]
arg_rep = HasDebugCallStack => Type -> [PrimRep]
typePrimRep (CoreArg -> Type
exprType CoreArg
arg)
stg_arg_rep :: [PrimRep]
stg_arg_rep = HasDebugCallStack => Type -> [PrimRep]
typePrimRep (StgArg -> Type
stgArgType StgArg
stg_arg)
bad_args :: Bool
bad_args = Bool -> Bool
not (Platform -> [PrimRep] -> [PrimRep] -> Bool
primRepsCompatible Platform
platform [PrimRep]
arg_rep [PrimRep]
stg_arg_rep)
WARN( bad_args, text "Dangerous-looking argument. Probable cause: bad unsafeCoerce#" $$ ppr arg )
forall (m :: * -> *) a. Monad m => a -> m a
return (StgArg
stg_arg forall a. a -> [a] -> [a]
: [StgArg]
stg_args, [StgTickish]
ticks forall a. [a] -> [a] -> [a]
++ [StgTickish]
aticks)
coreToStgTick :: Type
-> CoreTickish
-> StgTickish
coreToStgTick :: Type -> CoreTickish -> StgTickish
coreToStgTick Type
_ty (HpcTick Module
m JoinArity
i) = forall (pass :: TickishPass).
Module -> JoinArity -> GenTickish pass
HpcTick Module
m JoinArity
i
coreToStgTick Type
_ty (SourceNote RealSrcSpan
span String
nm) = forall (pass :: TickishPass).
RealSrcSpan -> String -> GenTickish pass
SourceNote RealSrcSpan
span String
nm
coreToStgTick Type
_ty (ProfNote CostCentre
cc Bool
cnt Bool
scope) = forall (pass :: TickishPass).
CostCentre -> Bool -> Bool -> GenTickish pass
ProfNote CostCentre
cc Bool
cnt Bool
scope
coreToStgTick !Type
ty (Breakpoint XBreakpoint 'TickishPassCore
_ JoinArity
bid [XTickishId 'TickishPassCore]
fvs) = forall (pass :: TickishPass).
XBreakpoint pass
-> JoinArity -> [XTickishId pass] -> GenTickish pass
Breakpoint Type
ty JoinArity
bid [XTickishId 'TickishPassCore]
fvs
coreToStgLet
:: CoreBind
-> CoreExpr
-> CtsM StgExpr
coreToStgLet :: CoreBind -> CoreArg -> CtsM StgExpr
coreToStgLet CoreBind
bind CoreArg
body
| NonRec Id
_ CoreArg
rhs <- CoreBind
bind, forall b. Expr b -> Bool
isTyCoArg CoreArg
rhs
= HasDebugCallStack => CoreArg -> CtsM StgExpr
coreToStgExpr CoreArg
body
| Bool
otherwise
= do { (StgBinding
bind2, [(Id, HowBound)]
env_ext) <- CoreBind -> CtsM (StgBinding, [(Id, HowBound)])
vars_bind CoreBind
bind
; StgExpr
body2 <- forall a. [(Id, HowBound)] -> CtsM a -> CtsM a
extendVarEnvCts [(Id, HowBound)]
env_ext forall a b. (a -> b) -> a -> b
$
HasDebugCallStack => CoreArg -> CtsM StgExpr
coreToStgExpr CoreArg
body
; let new_let :: StgExpr
new_let | CoreBind -> Bool
isJoinBind CoreBind
bind
= forall (pass :: StgPass).
XLetNoEscape pass
-> GenStgBinding pass -> GenStgExpr pass -> GenStgExpr pass
StgLetNoEscape NoExtFieldSilent
noExtFieldSilent StgBinding
bind2 StgExpr
body2
| Bool
otherwise
= forall (pass :: StgPass).
XLet pass
-> GenStgBinding pass -> GenStgExpr pass -> GenStgExpr pass
StgLet NoExtFieldSilent
noExtFieldSilent StgBinding
bind2 StgExpr
body2
; forall (m :: * -> *) a. Monad m => a -> m a
return StgExpr
new_let }
where
mk_binding :: a -> CoreArg -> (a, HowBound)
mk_binding a
binder CoreArg
rhs
= (a
binder, LetInfo -> JoinArity -> HowBound
LetBound LetInfo
NestedLet (CoreArg -> JoinArity
manifestArity CoreArg
rhs))
vars_bind :: CoreBind
-> CtsM (StgBinding,
[(Id, HowBound)])
vars_bind :: CoreBind -> CtsM (StgBinding, [(Id, HowBound)])
vars_bind (NonRec Id
binder CoreArg
rhs) = do
StgRhs
rhs2 <- (Id, CoreArg) -> CtsM StgRhs
coreToStgRhs (Id
binder,CoreArg
rhs)
let
env_ext_item :: (Id, HowBound)
env_ext_item = forall {a}. a -> CoreArg -> (a, HowBound)
mk_binding Id
binder CoreArg
rhs
forall (m :: * -> *) a. Monad m => a -> m a
return (forall (pass :: StgPass).
BinderP pass -> GenStgRhs pass -> GenStgBinding pass
StgNonRec Id
binder StgRhs
rhs2, [(Id, HowBound)
env_ext_item])
vars_bind (Rec [(Id, CoreArg)]
pairs)
= let
binders :: [Id]
binders = forall a b. (a -> b) -> [a] -> [b]
map forall a b. (a, b) -> a
fst [(Id, CoreArg)]
pairs
env_ext :: [(Id, HowBound)]
env_ext = [ forall {a}. a -> CoreArg -> (a, HowBound)
mk_binding Id
b CoreArg
rhs
| (Id
b,CoreArg
rhs) <- [(Id, CoreArg)]
pairs ]
in
forall a. [(Id, HowBound)] -> CtsM a -> CtsM a
extendVarEnvCts [(Id, HowBound)]
env_ext forall a b. (a -> b) -> a -> b
$ do
[StgRhs]
rhss2 <- forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Id, CoreArg) -> CtsM StgRhs
coreToStgRhs [(Id, CoreArg)]
pairs
forall (m :: * -> *) a. Monad m => a -> m a
return (forall (pass :: StgPass).
[(BinderP pass, GenStgRhs pass)] -> GenStgBinding pass
StgRec ([Id]
binders forall a b. [a] -> [b] -> [(a, b)]
`zip` [StgRhs]
rhss2), [(Id, HowBound)]
env_ext)
coreToStgRhs :: (Id,CoreExpr)
-> CtsM StgRhs
coreToStgRhs :: (Id, CoreArg) -> CtsM StgRhs
coreToStgRhs (Id
bndr, CoreArg
rhs) = do
PreStgRhs
new_rhs <- HasDebugCallStack => CoreArg -> CtsM PreStgRhs
coreToPreStgRhs CoreArg
rhs
forall (m :: * -> *) a. Monad m => a -> m a
return (Id -> PreStgRhs -> StgRhs
mkStgRhs Id
bndr PreStgRhs
new_rhs)
data PreStgRhs = PreStgRhs [Id] StgExpr
coreToPreStgRhs :: HasDebugCallStack => CoreExpr -> CtsM PreStgRhs
coreToPreStgRhs :: HasDebugCallStack => CoreArg -> CtsM PreStgRhs
coreToPreStgRhs (Cast CoreArg
expr Coercion
_) = HasDebugCallStack => CoreArg -> CtsM PreStgRhs
coreToPreStgRhs CoreArg
expr
coreToPreStgRhs expr :: CoreArg
expr@(Lam Id
_ CoreArg
_) =
let
([Id]
args, CoreArg
body) = CoreArg -> ([Id], CoreArg)
myCollectBinders CoreArg
expr
args' :: [Id]
args' = [Id] -> [Id]
filterStgBinders [Id]
args
in
forall a. [(Id, HowBound)] -> CtsM a -> CtsM a
extendVarEnvCts [ (Id
a, HowBound
LambdaBound) | Id
a <- [Id]
args' ] forall a b. (a -> b) -> a -> b
$ do
StgExpr
body' <- HasDebugCallStack => CoreArg -> CtsM StgExpr
coreToStgExpr CoreArg
body
forall (m :: * -> *) a. Monad m => a -> m a
return ([Id] -> StgExpr -> PreStgRhs
PreStgRhs [Id]
args' StgExpr
body')
coreToPreStgRhs CoreArg
expr = [Id] -> StgExpr -> PreStgRhs
PreStgRhs [] forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HasDebugCallStack => CoreArg -> CtsM StgExpr
coreToStgExpr CoreArg
expr
mkTopStgRhs :: DynFlags -> Module -> CollectedCCs
-> Id -> PreStgRhs -> (StgRhs, CollectedCCs)
mkTopStgRhs :: DynFlags
-> Module
-> CollectedCCs
-> Id
-> PreStgRhs
-> (StgRhs, CollectedCCs)
mkTopStgRhs DynFlags
dflags Module
this_mod CollectedCCs
ccs Id
bndr (PreStgRhs [Id]
bndrs StgExpr
rhs)
| Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Id]
bndrs)
=
( forall (pass :: StgPass).
XRhsClosure pass
-> CostCentreStack
-> UpdateFlag
-> [BinderP pass]
-> GenStgExpr pass
-> GenStgRhs pass
StgRhsClosure NoExtFieldSilent
noExtFieldSilent
CostCentreStack
dontCareCCS
UpdateFlag
ReEntrant
[Id]
bndrs StgExpr
rhs
, CollectedCCs
ccs )
| StgConApp DataCon
con XConApp 'Vanilla
mn [StgArg]
args [Type]
_ <- StgExpr
unticked_rhs
,
Bool -> Bool
not (DynFlags -> Module -> DataCon -> [StgArg] -> Bool
isDllConApp DynFlags
dflags Module
this_mod DataCon
con [StgArg]
args)
=
ASSERT2( not (isUnboxedTupleDataCon con || isUnboxedSumDataCon con)
, ppr bndr $$ ppr con $$ ppr args)
( forall (pass :: StgPass).
CostCentreStack
-> DataCon
-> ConstructorNumber
-> [StgTickish]
-> [StgArg]
-> GenStgRhs pass
StgRhsCon CostCentreStack
dontCareCCS DataCon
con XConApp 'Vanilla
mn [StgTickish]
ticks [StgArg]
args, CollectedCCs
ccs )
| GeneralFlag -> DynFlags -> Bool
gopt GeneralFlag
Opt_AutoSccsOnIndividualCafs DynFlags
dflags
= ( forall (pass :: StgPass).
XRhsClosure pass
-> CostCentreStack
-> UpdateFlag
-> [BinderP pass]
-> GenStgExpr pass
-> GenStgRhs pass
StgRhsClosure NoExtFieldSilent
noExtFieldSilent
CostCentreStack
caf_ccs
UpdateFlag
upd_flag [] StgExpr
rhs
, CostCentre -> CostCentreStack -> CollectedCCs -> CollectedCCs
collectCC CostCentre
caf_cc CostCentreStack
caf_ccs CollectedCCs
ccs )
| Bool
otherwise
= ( forall (pass :: StgPass).
XRhsClosure pass
-> CostCentreStack
-> UpdateFlag
-> [BinderP pass]
-> GenStgExpr pass
-> GenStgRhs pass
StgRhsClosure NoExtFieldSilent
noExtFieldSilent
CostCentreStack
all_cafs_ccs
UpdateFlag
upd_flag [] StgExpr
rhs
, CollectedCCs
ccs )
where
([StgTickish]
ticks, StgExpr
unticked_rhs) = forall (p :: StgPass).
(StgTickish -> Bool)
-> GenStgExpr p -> ([StgTickish], GenStgExpr p)
stripStgTicksTop (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (pass :: TickishPass). GenTickish pass -> Bool
tickishIsCode) StgExpr
rhs
upd_flag :: UpdateFlag
upd_flag | Demand -> Bool
isUsedOnceDmd (Id -> Demand
idDemandInfo Id
bndr) = UpdateFlag
SingleEntry
| Bool
otherwise = UpdateFlag
Updatable
caf_cc :: CostCentre
caf_cc = Id -> Module -> CostCentre
mkAutoCC Id
bndr Module
modl
caf_ccs :: CostCentreStack
caf_ccs = CostCentre -> CostCentreStack
mkSingletonCCS CostCentre
caf_cc
modl :: Module
modl | Just Module
m <- Name -> Maybe Module
nameModule_maybe (Id -> Name
idName Id
bndr) = Module
m
| Bool
otherwise = Module
this_mod
(CostCentre
_, CostCentreStack
all_cafs_ccs) = Module -> (CostCentre, CostCentreStack)
getAllCAFsCC Module
this_mod
mkStgRhs :: Id -> PreStgRhs -> StgRhs
mkStgRhs :: Id -> PreStgRhs -> StgRhs
mkStgRhs Id
bndr (PreStgRhs [Id]
bndrs StgExpr
rhs)
| Bool -> Bool
not (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Id]
bndrs)
= forall (pass :: StgPass).
XRhsClosure pass
-> CostCentreStack
-> UpdateFlag
-> [BinderP pass]
-> GenStgExpr pass
-> GenStgRhs pass
StgRhsClosure NoExtFieldSilent
noExtFieldSilent
CostCentreStack
currentCCS
UpdateFlag
ReEntrant
[Id]
bndrs StgExpr
rhs
| Id -> Bool
isJoinId Id
bndr
= ASSERT(idJoinArity bndr == 0)
forall (pass :: StgPass).
XRhsClosure pass
-> CostCentreStack
-> UpdateFlag
-> [BinderP pass]
-> GenStgExpr pass
-> GenStgRhs pass
StgRhsClosure NoExtFieldSilent
noExtFieldSilent
CostCentreStack
currentCCS
UpdateFlag
ReEntrant
[] StgExpr
rhs
| StgConApp DataCon
con XConApp 'Vanilla
mn [StgArg]
args [Type]
_ <- StgExpr
unticked_rhs
= forall (pass :: StgPass).
CostCentreStack
-> DataCon
-> ConstructorNumber
-> [StgTickish]
-> [StgArg]
-> GenStgRhs pass
StgRhsCon CostCentreStack
currentCCS DataCon
con XConApp 'Vanilla
mn [StgTickish]
ticks [StgArg]
args
| Bool
otherwise
= forall (pass :: StgPass).
XRhsClosure pass
-> CostCentreStack
-> UpdateFlag
-> [BinderP pass]
-> GenStgExpr pass
-> GenStgRhs pass
StgRhsClosure NoExtFieldSilent
noExtFieldSilent
CostCentreStack
currentCCS
UpdateFlag
upd_flag [] StgExpr
rhs
where
([StgTickish]
ticks, StgExpr
unticked_rhs) = forall (p :: StgPass).
(StgTickish -> Bool)
-> GenStgExpr p -> ([StgTickish], GenStgExpr p)
stripStgTicksTop (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (pass :: TickishPass). GenTickish pass -> Bool
tickishIsCode) StgExpr
rhs
upd_flag :: UpdateFlag
upd_flag | Demand -> Bool
isUsedOnceDmd (Id -> Demand
idDemandInfo Id
bndr) = UpdateFlag
SingleEntry
| Bool
otherwise = UpdateFlag
Updatable
newtype CtsM a = CtsM
{ forall a. CtsM a -> DynFlags -> IdEnv HowBound -> a
unCtsM :: DynFlags
-> IdEnv HowBound
-> a
}
deriving (forall a b. a -> CtsM b -> CtsM a
forall a b. (a -> b) -> CtsM a -> CtsM b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: forall a b. a -> CtsM b -> CtsM a
$c<$ :: forall a b. a -> CtsM b -> CtsM a
fmap :: forall a b. (a -> b) -> CtsM a -> CtsM b
$cfmap :: forall a b. (a -> b) -> CtsM a -> CtsM b
Functor)
data HowBound
= ImportBound
| LetBound
LetInfo
Arity
| LambdaBound
deriving (HowBound -> HowBound -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: HowBound -> HowBound -> Bool
$c/= :: HowBound -> HowBound -> Bool
== :: HowBound -> HowBound -> Bool
$c== :: HowBound -> HowBound -> Bool
Eq)
data LetInfo
= TopLet
| NestedLet
deriving (LetInfo -> LetInfo -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LetInfo -> LetInfo -> Bool
$c/= :: LetInfo -> LetInfo -> Bool
== :: LetInfo -> LetInfo -> Bool
$c== :: LetInfo -> LetInfo -> Bool
Eq)
initCts :: DynFlags -> IdEnv HowBound -> CtsM a -> a
initCts :: forall a. DynFlags -> IdEnv HowBound -> CtsM a -> a
initCts DynFlags
dflags IdEnv HowBound
env CtsM a
m = forall a. CtsM a -> DynFlags -> IdEnv HowBound -> a
unCtsM CtsM a
m DynFlags
dflags IdEnv HowBound
env
{-# INLINE thenCts #-}
{-# INLINE returnCts #-}
returnCts :: a -> CtsM a
returnCts :: forall a. a -> CtsM a
returnCts a
e = forall a. (DynFlags -> IdEnv HowBound -> a) -> CtsM a
CtsM forall a b. (a -> b) -> a -> b
$ \DynFlags
_ IdEnv HowBound
_ -> a
e
thenCts :: CtsM a -> (a -> CtsM b) -> CtsM b
thenCts :: forall a b. CtsM a -> (a -> CtsM b) -> CtsM b
thenCts CtsM a
m a -> CtsM b
k = forall a. (DynFlags -> IdEnv HowBound -> a) -> CtsM a
CtsM forall a b. (a -> b) -> a -> b
$ \DynFlags
dflags IdEnv HowBound
env
-> forall a. CtsM a -> DynFlags -> IdEnv HowBound -> a
unCtsM (a -> CtsM b
k (forall a. CtsM a -> DynFlags -> IdEnv HowBound -> a
unCtsM CtsM a
m DynFlags
dflags IdEnv HowBound
env)) DynFlags
dflags IdEnv HowBound
env
instance Applicative CtsM where
pure :: forall a. a -> CtsM a
pure = forall a. a -> CtsM a
returnCts
<*> :: forall a b. CtsM (a -> b) -> CtsM a -> CtsM b
(<*>) = forall (m :: * -> *) a b. Monad m => m (a -> b) -> m a -> m b
ap
instance Monad CtsM where
>>= :: forall a b. CtsM a -> (a -> CtsM b) -> CtsM b
(>>=) = forall a b. CtsM a -> (a -> CtsM b) -> CtsM b
thenCts
instance HasDynFlags CtsM where
getDynFlags :: CtsM DynFlags
getDynFlags = forall a. (DynFlags -> IdEnv HowBound -> a) -> CtsM a
CtsM forall a b. (a -> b) -> a -> b
$ \DynFlags
dflags IdEnv HowBound
_ -> DynFlags
dflags
extendVarEnvCts :: [(Id, HowBound)] -> CtsM a -> CtsM a
extendVarEnvCts :: forall a. [(Id, HowBound)] -> CtsM a -> CtsM a
extendVarEnvCts [(Id, HowBound)]
ids_w_howbound CtsM a
expr
= forall a. (DynFlags -> IdEnv HowBound -> a) -> CtsM a
CtsM forall a b. (a -> b) -> a -> b
$ \DynFlags
dflags IdEnv HowBound
env
-> forall a. CtsM a -> DynFlags -> IdEnv HowBound -> a
unCtsM CtsM a
expr DynFlags
dflags (forall a. VarEnv a -> [(Id, a)] -> VarEnv a
extendVarEnvList IdEnv HowBound
env [(Id, HowBound)]
ids_w_howbound)
lookupVarCts :: Id -> CtsM HowBound
lookupVarCts :: Id -> CtsM HowBound
lookupVarCts Id
v = forall a. (DynFlags -> IdEnv HowBound -> a) -> CtsM a
CtsM forall a b. (a -> b) -> a -> b
$ \DynFlags
_ IdEnv HowBound
env -> IdEnv HowBound -> Id -> HowBound
lookupBinding IdEnv HowBound
env Id
v
lookupBinding :: IdEnv HowBound -> Id -> HowBound
lookupBinding :: IdEnv HowBound -> Id -> HowBound
lookupBinding IdEnv HowBound
env Id
v = case forall a. VarEnv a -> Id -> Maybe a
lookupVarEnv IdEnv HowBound
env Id
v of
Just HowBound
xx -> HowBound
xx
Maybe HowBound
Nothing -> ASSERT2( isGlobalId v, ppr v ) ImportBound
getAllCAFsCC :: Module -> (CostCentre, CostCentreStack)
getAllCAFsCC :: Module -> (CostCentre, CostCentreStack)
getAllCAFsCC Module
this_mod =
let
span :: SrcSpan
span = CLabelString -> SrcSpan
mkGeneralSrcSpan (String -> CLabelString
mkFastString String
"<entire-module>")
all_cafs_cc :: CostCentre
all_cafs_cc = Module -> SrcSpan -> CostCentre
mkAllCafsCC Module
this_mod SrcSpan
span
all_cafs_ccs :: CostCentreStack
all_cafs_ccs = CostCentre -> CostCentreStack
mkSingletonCCS CostCentre
all_cafs_cc
in
(CostCentre
all_cafs_cc, CostCentreStack
all_cafs_ccs)
filterStgBinders :: [Var] -> [Var]
filterStgBinders :: [Id] -> [Id]
filterStgBinders [Id]
bndrs = forall a. (a -> Bool) -> [a] -> [a]
filter Id -> Bool
isId [Id]
bndrs
myCollectBinders :: Expr Var -> ([Var], Expr Var)
myCollectBinders :: CoreArg -> ([Id], CoreArg)
myCollectBinders CoreArg
expr
= forall {a}. [a] -> Expr a -> ([a], Expr a)
go [] CoreArg
expr
where
go :: [a] -> Expr a -> ([a], Expr a)
go [a]
bs (Lam a
b Expr a
e) = [a] -> Expr a -> ([a], Expr a)
go (a
bforall a. a -> [a] -> [a]
:[a]
bs) Expr a
e
go [a]
bs (Cast Expr a
e Coercion
_) = [a] -> Expr a -> ([a], Expr a)
go [a]
bs Expr a
e
go [a]
bs Expr a
e = (forall a. [a] -> [a]
reverse [a]
bs, Expr a
e)
myCollectArgs :: HasDebugCallStack => CoreExpr -> (CoreExpr, [CoreArg], [CoreTickish])
myCollectArgs :: HasDebugCallStack => CoreArg -> (CoreArg, [CoreArg], [CoreTickish])
myCollectArgs CoreArg
expr
= CoreArg
-> [CoreArg]
-> [CoreTickish]
-> (CoreArg, [CoreArg], [CoreTickish])
go CoreArg
expr [] []
where
go :: CoreArg
-> [CoreArg]
-> [CoreTickish]
-> (CoreArg, [CoreArg], [CoreTickish])
go h :: CoreArg
h@(Var Id
_v) [CoreArg]
as [CoreTickish]
ts = (CoreArg
h, [CoreArg]
as, [CoreTickish]
ts)
go (App CoreArg
f CoreArg
a) [CoreArg]
as [CoreTickish]
ts = CoreArg
-> [CoreArg]
-> [CoreTickish]
-> (CoreArg, [CoreArg], [CoreTickish])
go CoreArg
f (CoreArg
aforall a. a -> [a] -> [a]
:[CoreArg]
as) [CoreTickish]
ts
go (Tick CoreTickish
t CoreArg
e) [CoreArg]
as [CoreTickish]
ts = ASSERT2( not (tickishIsCode t) || all isTypeArg as
, ppr e $$ ppr as $$ ppr ts )
CoreArg
-> [CoreArg]
-> [CoreTickish]
-> (CoreArg, [CoreArg], [CoreTickish])
go CoreArg
e [CoreArg]
as (CoreTickish
tforall a. a -> [a] -> [a]
:[CoreTickish]
ts)
go (Cast CoreArg
e Coercion
_) [CoreArg]
as [CoreTickish]
ts = CoreArg
-> [CoreArg]
-> [CoreTickish]
-> (CoreArg, [CoreArg], [CoreTickish])
go CoreArg
e [CoreArg]
as [CoreTickish]
ts
go (Lam Id
b CoreArg
e) [CoreArg]
as [CoreTickish]
ts
| Id -> Bool
isTyVar Id
b = CoreArg
-> [CoreArg]
-> [CoreTickish]
-> (CoreArg, [CoreArg], [CoreTickish])
go CoreArg
e [CoreArg]
as [CoreTickish]
ts
go CoreArg
e [CoreArg]
as [CoreTickish]
ts = (CoreArg
e, [CoreArg]
as, [CoreTickish]
ts)
stgArity :: Id -> HowBound -> Arity
stgArity :: Id -> HowBound -> JoinArity
stgArity Id
_ (LetBound LetInfo
_ JoinArity
arity) = JoinArity
arity
stgArity Id
f HowBound
ImportBound = Id -> JoinArity
idArity Id
f
stgArity Id
_ HowBound
LambdaBound = JoinArity
0