{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
module CmmNode (
CmmNode(..), CmmFormal, CmmActual, CmmTickish,
UpdFrameOffset, Convention(..),
ForeignConvention(..), ForeignTarget(..), foreignTargetHints,
CmmReturnInfo(..),
mapExp, mapExpDeep, wrapRecExp, foldExp, foldExpDeep, wrapRecExpf,
mapExpM, mapExpDeepM, wrapRecExpM, mapSuccessors, mapCollectSuccessors,
CmmTickScope(..), isTickSubScope, combineTickScopes,
) where
import GhcPrelude hiding (succ)
import CodeGen.Platform
import CmmExpr
import CmmSwitch
import DynFlags
import FastString
import ForeignCall
import Outputable
import SMRep
import CoreSyn (Tickish)
import qualified Unique as U
import Hoopl.Block
import Hoopl.Graph
import Hoopl.Collections
import Hoopl.Label
import Data.Maybe
import Data.List (tails,sortBy)
import Unique (nonDetCmpUnique)
import Util
#define ULabel {-# UNPACK #-} !Label
data CmmNode e x where
CmmEntry :: ULabel -> CmmTickScope -> CmmNode C O
:: FastString -> CmmNode O O
CmmTick :: !CmmTickish -> CmmNode O O
CmmUnwind :: [(GlobalReg, Maybe CmmExpr)] -> CmmNode O O
CmmAssign :: !CmmReg -> !CmmExpr -> CmmNode O O
CmmStore :: !CmmExpr -> !CmmExpr -> CmmNode O O
CmmUnsafeForeignCall ::
ForeignTarget ->
[CmmFormal] ->
[CmmActual] ->
CmmNode O O
CmmBranch :: ULabel -> CmmNode O C
CmmCondBranch :: {
CmmNode O C -> CmmExpr
cml_pred :: CmmExpr,
CmmNode O C -> Label
cml_true, CmmNode O C -> Label
cml_false :: ULabel,
CmmNode O C -> Maybe Bool
cml_likely :: Maybe Bool
} -> CmmNode O C
CmmSwitch
:: CmmExpr
-> SwitchTargets
-> CmmNode O C
CmmCall :: {
CmmNode O C -> CmmExpr
cml_target :: CmmExpr,
CmmNode O C -> Maybe Label
cml_cont :: Maybe Label,
CmmNode O C -> [GlobalReg]
cml_args_regs :: [GlobalReg],
CmmNode O C -> ByteOff
cml_args :: ByteOff,
CmmNode O C -> ByteOff
cml_ret_args :: ByteOff,
CmmNode O C -> ByteOff
cml_ret_off :: ByteOff
} -> CmmNode O C
CmmForeignCall :: {
CmmNode O C -> ForeignTarget
tgt :: ForeignTarget,
CmmNode O C -> [CmmFormal]
res :: [CmmFormal],
CmmNode O C -> [CmmExpr]
args :: [CmmActual],
CmmNode O C -> Label
succ :: ULabel,
CmmNode O C -> ByteOff
ret_args :: ByteOff,
CmmNode O C -> ByteOff
ret_off :: ByteOff,
CmmNode O C -> Bool
intrbl:: Bool
} -> CmmNode O C
deriving instance Eq (CmmNode e x)
instance NonLocal CmmNode where
entryLabel :: CmmNode C x -> Label
entryLabel (CmmEntry l :: Label
l _) = Label
l
successors :: CmmNode e C -> [Label]
successors (CmmBranch l :: Label
l) = [Label
l]
successors (CmmCondBranch {cml_true :: CmmNode O C -> Label
cml_true=Label
t, cml_false :: CmmNode O C -> Label
cml_false=Label
f}) = [Label
f, Label
t]
successors (CmmSwitch _ ids :: SwitchTargets
ids) = SwitchTargets -> [Label]
switchTargetsToList SwitchTargets
ids
successors (CmmCall {cml_cont :: CmmNode O C -> Maybe Label
cml_cont=Maybe Label
l}) = Maybe Label -> [Label]
forall a. Maybe a -> [a]
maybeToList Maybe Label
l
successors (CmmForeignCall {succ :: CmmNode O C -> Label
succ=Label
l}) = [Label
l]
type CmmActual = CmmExpr
type CmmFormal = LocalReg
type UpdFrameOffset = ByteOff
data Convention
= NativeDirectCall
| NativeNodeCall
| NativeReturn
| Slow
| GC
deriving( Convention -> Convention -> Bool
(Convention -> Convention -> Bool)
-> (Convention -> Convention -> Bool) -> Eq Convention
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Convention -> Convention -> Bool
$c/= :: Convention -> Convention -> Bool
== :: Convention -> Convention -> Bool
$c== :: Convention -> Convention -> Bool
Eq )
data ForeignConvention
= ForeignConvention
CCallConv
[ForeignHint]
[ForeignHint]
CmmReturnInfo
deriving ForeignConvention -> ForeignConvention -> Bool
(ForeignConvention -> ForeignConvention -> Bool)
-> (ForeignConvention -> ForeignConvention -> Bool)
-> Eq ForeignConvention
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ForeignConvention -> ForeignConvention -> Bool
$c/= :: ForeignConvention -> ForeignConvention -> Bool
== :: ForeignConvention -> ForeignConvention -> Bool
$c== :: ForeignConvention -> ForeignConvention -> Bool
Eq
data CmmReturnInfo
= CmmMayReturn
| CmmNeverReturns
deriving ( CmmReturnInfo -> CmmReturnInfo -> Bool
(CmmReturnInfo -> CmmReturnInfo -> Bool)
-> (CmmReturnInfo -> CmmReturnInfo -> Bool) -> Eq CmmReturnInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CmmReturnInfo -> CmmReturnInfo -> Bool
$c/= :: CmmReturnInfo -> CmmReturnInfo -> Bool
== :: CmmReturnInfo -> CmmReturnInfo -> Bool
$c== :: CmmReturnInfo -> CmmReturnInfo -> Bool
Eq )
data ForeignTarget
= ForeignTarget
CmmExpr
ForeignConvention
| PrimTarget
CallishMachOp
deriving ForeignTarget -> ForeignTarget -> Bool
(ForeignTarget -> ForeignTarget -> Bool)
-> (ForeignTarget -> ForeignTarget -> Bool) -> Eq ForeignTarget
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ForeignTarget -> ForeignTarget -> Bool
$c/= :: ForeignTarget -> ForeignTarget -> Bool
== :: ForeignTarget -> ForeignTarget -> Bool
$c== :: ForeignTarget -> ForeignTarget -> Bool
Eq
foreignTargetHints :: ForeignTarget -> ([ForeignHint], [ForeignHint])
foreignTargetHints :: ForeignTarget -> ([ForeignHint], [ForeignHint])
foreignTargetHints target :: ForeignTarget
target
= ( [ForeignHint]
res_hints [ForeignHint] -> [ForeignHint] -> [ForeignHint]
forall a. [a] -> [a] -> [a]
++ ForeignHint -> [ForeignHint]
forall a. a -> [a]
repeat ForeignHint
NoHint
, [ForeignHint]
arg_hints [ForeignHint] -> [ForeignHint] -> [ForeignHint]
forall a. [a] -> [a] -> [a]
++ ForeignHint -> [ForeignHint]
forall a. a -> [a]
repeat ForeignHint
NoHint )
where
(res_hints :: [ForeignHint]
res_hints, arg_hints :: [ForeignHint]
arg_hints) =
case ForeignTarget
target of
PrimTarget op :: CallishMachOp
op -> CallishMachOp -> ([ForeignHint], [ForeignHint])
callishMachOpHints CallishMachOp
op
ForeignTarget _ (ForeignConvention _ arg_hints :: [ForeignHint]
arg_hints res_hints :: [ForeignHint]
res_hints _) ->
([ForeignHint]
res_hints, [ForeignHint]
arg_hints)
instance UserOfRegs LocalReg (CmmNode e x) where
foldRegsUsed :: DynFlags -> (b -> CmmFormal -> b) -> b -> CmmNode e x -> b
foldRegsUsed dflags :: DynFlags
dflags f :: b -> CmmFormal -> b
f !b
z n :: CmmNode e x
n = case CmmNode e x
n of
CmmAssign _ expr :: CmmExpr
expr -> (b -> CmmFormal -> b) -> b -> CmmExpr -> b
forall a b.
UserOfRegs CmmFormal a =>
(b -> CmmFormal -> b) -> b -> a -> b
fold b -> CmmFormal -> b
f b
z CmmExpr
expr
CmmStore addr :: CmmExpr
addr rval :: CmmExpr
rval -> (b -> CmmFormal -> b) -> b -> CmmExpr -> b
forall a b.
UserOfRegs CmmFormal a =>
(b -> CmmFormal -> b) -> b -> a -> b
fold b -> CmmFormal -> b
f ((b -> CmmFormal -> b) -> b -> CmmExpr -> b
forall a b.
UserOfRegs CmmFormal a =>
(b -> CmmFormal -> b) -> b -> a -> b
fold b -> CmmFormal -> b
f b
z CmmExpr
addr) CmmExpr
rval
CmmUnsafeForeignCall t :: ForeignTarget
t _ args :: [CmmExpr]
args -> (b -> CmmFormal -> b) -> b -> [CmmExpr] -> b
forall a b.
UserOfRegs CmmFormal a =>
(b -> CmmFormal -> b) -> b -> a -> b
fold b -> CmmFormal -> b
f ((b -> CmmFormal -> b) -> b -> ForeignTarget -> b
forall a b.
UserOfRegs CmmFormal a =>
(b -> CmmFormal -> b) -> b -> a -> b
fold b -> CmmFormal -> b
f b
z ForeignTarget
t) [CmmExpr]
args
CmmCondBranch expr :: CmmExpr
expr _ _ _ -> (b -> CmmFormal -> b) -> b -> CmmExpr -> b
forall a b.
UserOfRegs CmmFormal a =>
(b -> CmmFormal -> b) -> b -> a -> b
fold b -> CmmFormal -> b
f b
z CmmExpr
expr
CmmSwitch expr :: CmmExpr
expr _ -> (b -> CmmFormal -> b) -> b -> CmmExpr -> b
forall a b.
UserOfRegs CmmFormal a =>
(b -> CmmFormal -> b) -> b -> a -> b
fold b -> CmmFormal -> b
f b
z CmmExpr
expr
CmmCall {cml_target :: CmmNode O C -> CmmExpr
cml_target=CmmExpr
tgt} -> (b -> CmmFormal -> b) -> b -> CmmExpr -> b
forall a b.
UserOfRegs CmmFormal a =>
(b -> CmmFormal -> b) -> b -> a -> b
fold b -> CmmFormal -> b
f b
z CmmExpr
tgt
CmmForeignCall {tgt :: CmmNode O C -> ForeignTarget
tgt=ForeignTarget
tgt, args :: CmmNode O C -> [CmmExpr]
args=[CmmExpr]
args} -> (b -> CmmFormal -> b) -> b -> [CmmExpr] -> b
forall a b.
UserOfRegs CmmFormal a =>
(b -> CmmFormal -> b) -> b -> a -> b
fold b -> CmmFormal -> b
f ((b -> CmmFormal -> b) -> b -> ForeignTarget -> b
forall a b.
UserOfRegs CmmFormal a =>
(b -> CmmFormal -> b) -> b -> a -> b
fold b -> CmmFormal -> b
f b
z ForeignTarget
tgt) [CmmExpr]
args
_ -> b
z
where fold :: forall a b. UserOfRegs LocalReg a
=> (b -> LocalReg -> b) -> b -> a -> b
fold :: (b -> CmmFormal -> b) -> b -> a -> b
fold f :: b -> CmmFormal -> b
f z :: b
z n :: a
n = DynFlags -> (b -> CmmFormal -> b) -> b -> a -> b
forall r a b.
UserOfRegs r a =>
DynFlags -> (b -> r -> b) -> b -> a -> b
foldRegsUsed DynFlags
dflags b -> CmmFormal -> b
f b
z a
n
instance UserOfRegs GlobalReg (CmmNode e x) where
foldRegsUsed :: DynFlags -> (b -> GlobalReg -> b) -> b -> CmmNode e x -> b
foldRegsUsed dflags :: DynFlags
dflags f :: b -> GlobalReg -> b
f !b
z n :: CmmNode e x
n = case CmmNode e x
n of
CmmAssign _ expr :: CmmExpr
expr -> (b -> GlobalReg -> b) -> b -> CmmExpr -> b
forall a b.
UserOfRegs GlobalReg a =>
(b -> GlobalReg -> b) -> b -> a -> b
fold b -> GlobalReg -> b
f b
z CmmExpr
expr
CmmStore addr :: CmmExpr
addr rval :: CmmExpr
rval -> (b -> GlobalReg -> b) -> b -> CmmExpr -> b
forall a b.
UserOfRegs GlobalReg a =>
(b -> GlobalReg -> b) -> b -> a -> b
fold b -> GlobalReg -> b
f ((b -> GlobalReg -> b) -> b -> CmmExpr -> b
forall a b.
UserOfRegs GlobalReg a =>
(b -> GlobalReg -> b) -> b -> a -> b
fold b -> GlobalReg -> b
f b
z CmmExpr
addr) CmmExpr
rval
CmmUnsafeForeignCall t :: ForeignTarget
t _ args :: [CmmExpr]
args -> (b -> GlobalReg -> b) -> b -> [CmmExpr] -> b
forall a b.
UserOfRegs GlobalReg a =>
(b -> GlobalReg -> b) -> b -> a -> b
fold b -> GlobalReg -> b
f ((b -> GlobalReg -> b) -> b -> ForeignTarget -> b
forall a b.
UserOfRegs GlobalReg a =>
(b -> GlobalReg -> b) -> b -> a -> b
fold b -> GlobalReg -> b
f b
z ForeignTarget
t) [CmmExpr]
args
CmmCondBranch expr :: CmmExpr
expr _ _ _ -> (b -> GlobalReg -> b) -> b -> CmmExpr -> b
forall a b.
UserOfRegs GlobalReg a =>
(b -> GlobalReg -> b) -> b -> a -> b
fold b -> GlobalReg -> b
f b
z CmmExpr
expr
CmmSwitch expr :: CmmExpr
expr _ -> (b -> GlobalReg -> b) -> b -> CmmExpr -> b
forall a b.
UserOfRegs GlobalReg a =>
(b -> GlobalReg -> b) -> b -> a -> b
fold b -> GlobalReg -> b
f b
z CmmExpr
expr
CmmCall {cml_target :: CmmNode O C -> CmmExpr
cml_target=CmmExpr
tgt, cml_args_regs :: CmmNode O C -> [GlobalReg]
cml_args_regs=[GlobalReg]
args} -> (b -> GlobalReg -> b) -> b -> CmmExpr -> b
forall a b.
UserOfRegs GlobalReg a =>
(b -> GlobalReg -> b) -> b -> a -> b
fold b -> GlobalReg -> b
f ((b -> GlobalReg -> b) -> b -> [GlobalReg] -> b
forall a b.
UserOfRegs GlobalReg a =>
(b -> GlobalReg -> b) -> b -> a -> b
fold b -> GlobalReg -> b
f b
z [GlobalReg]
args) CmmExpr
tgt
CmmForeignCall {tgt :: CmmNode O C -> ForeignTarget
tgt=ForeignTarget
tgt, args :: CmmNode O C -> [CmmExpr]
args=[CmmExpr]
args} -> (b -> GlobalReg -> b) -> b -> [CmmExpr] -> b
forall a b.
UserOfRegs GlobalReg a =>
(b -> GlobalReg -> b) -> b -> a -> b
fold b -> GlobalReg -> b
f ((b -> GlobalReg -> b) -> b -> ForeignTarget -> b
forall a b.
UserOfRegs GlobalReg a =>
(b -> GlobalReg -> b) -> b -> a -> b
fold b -> GlobalReg -> b
f b
z ForeignTarget
tgt) [CmmExpr]
args
_ -> b
z
where fold :: forall a b. UserOfRegs GlobalReg a
=> (b -> GlobalReg -> b) -> b -> a -> b
fold :: (b -> GlobalReg -> b) -> b -> a -> b
fold f :: b -> GlobalReg -> b
f z :: b
z n :: a
n = DynFlags -> (b -> GlobalReg -> b) -> b -> a -> b
forall r a b.
UserOfRegs r a =>
DynFlags -> (b -> r -> b) -> b -> a -> b
foldRegsUsed DynFlags
dflags b -> GlobalReg -> b
f b
z a
n
instance (Ord r, UserOfRegs r CmmReg) => UserOfRegs r ForeignTarget where
foldRegsUsed :: DynFlags -> (b -> r -> b) -> b -> ForeignTarget -> b
foldRegsUsed _ _ !b
z (PrimTarget _) = b
z
foldRegsUsed dflags :: DynFlags
dflags f :: b -> r -> b
f !b
z (ForeignTarget e :: CmmExpr
e _) = DynFlags -> (b -> r -> b) -> b -> CmmExpr -> b
forall r a b.
UserOfRegs r a =>
DynFlags -> (b -> r -> b) -> b -> a -> b
foldRegsUsed DynFlags
dflags b -> r -> b
f b
z CmmExpr
e
instance DefinerOfRegs LocalReg (CmmNode e x) where
foldRegsDefd :: DynFlags -> (b -> CmmFormal -> b) -> b -> CmmNode e x -> b
foldRegsDefd dflags :: DynFlags
dflags f :: b -> CmmFormal -> b
f !b
z n :: CmmNode e x
n = case CmmNode e x
n of
CmmAssign lhs :: CmmReg
lhs _ -> (b -> CmmFormal -> b) -> b -> CmmReg -> b
forall a b.
DefinerOfRegs CmmFormal a =>
(b -> CmmFormal -> b) -> b -> a -> b
fold b -> CmmFormal -> b
f b
z CmmReg
lhs
CmmUnsafeForeignCall _ fs :: [CmmFormal]
fs _ -> (b -> CmmFormal -> b) -> b -> [CmmFormal] -> b
forall a b.
DefinerOfRegs CmmFormal a =>
(b -> CmmFormal -> b) -> b -> a -> b
fold b -> CmmFormal -> b
f b
z [CmmFormal]
fs
CmmForeignCall {res :: CmmNode O C -> [CmmFormal]
res=[CmmFormal]
res} -> (b -> CmmFormal -> b) -> b -> [CmmFormal] -> b
forall a b.
DefinerOfRegs CmmFormal a =>
(b -> CmmFormal -> b) -> b -> a -> b
fold b -> CmmFormal -> b
f b
z [CmmFormal]
res
_ -> b
z
where fold :: forall a b. DefinerOfRegs LocalReg a
=> (b -> LocalReg -> b) -> b -> a -> b
fold :: (b -> CmmFormal -> b) -> b -> a -> b
fold f :: b -> CmmFormal -> b
f z :: b
z n :: a
n = DynFlags -> (b -> CmmFormal -> b) -> b -> a -> b
forall r a b.
DefinerOfRegs r a =>
DynFlags -> (b -> r -> b) -> b -> a -> b
foldRegsDefd DynFlags
dflags b -> CmmFormal -> b
f b
z a
n
instance DefinerOfRegs GlobalReg (CmmNode e x) where
foldRegsDefd :: DynFlags -> (b -> GlobalReg -> b) -> b -> CmmNode e x -> b
foldRegsDefd dflags :: DynFlags
dflags f :: b -> GlobalReg -> b
f !b
z n :: CmmNode e x
n = case CmmNode e x
n of
CmmAssign lhs :: CmmReg
lhs _ -> (b -> GlobalReg -> b) -> b -> CmmReg -> b
forall a b.
DefinerOfRegs GlobalReg a =>
(b -> GlobalReg -> b) -> b -> a -> b
fold b -> GlobalReg -> b
f b
z CmmReg
lhs
CmmUnsafeForeignCall tgt :: ForeignTarget
tgt _ _ -> (b -> GlobalReg -> b) -> b -> [GlobalReg] -> b
forall a b.
DefinerOfRegs GlobalReg a =>
(b -> GlobalReg -> b) -> b -> a -> b
fold b -> GlobalReg -> b
f b
z (ForeignTarget -> [GlobalReg]
foreignTargetRegs ForeignTarget
tgt)
CmmCall {} -> (b -> GlobalReg -> b) -> b -> [GlobalReg] -> b
forall a b.
DefinerOfRegs GlobalReg a =>
(b -> GlobalReg -> b) -> b -> a -> b
fold b -> GlobalReg -> b
f b
z [GlobalReg]
activeRegs
CmmForeignCall {} -> (b -> GlobalReg -> b) -> b -> [GlobalReg] -> b
forall a b.
DefinerOfRegs GlobalReg a =>
(b -> GlobalReg -> b) -> b -> a -> b
fold b -> GlobalReg -> b
f b
z [GlobalReg]
activeRegs
_ -> b
z
where fold :: forall a b. DefinerOfRegs GlobalReg a
=> (b -> GlobalReg -> b) -> b -> a -> b
fold :: (b -> GlobalReg -> b) -> b -> a -> b
fold f :: b -> GlobalReg -> b
f z :: b
z n :: a
n = DynFlags -> (b -> GlobalReg -> b) -> b -> a -> b
forall r a b.
DefinerOfRegs r a =>
DynFlags -> (b -> r -> b) -> b -> a -> b
foldRegsDefd DynFlags
dflags b -> GlobalReg -> b
f b
z a
n
platform :: Platform
platform = DynFlags -> Platform
targetPlatform DynFlags
dflags
activeRegs :: [GlobalReg]
activeRegs = Platform -> [GlobalReg]
activeStgRegs Platform
platform
activeCallerSavesRegs :: [GlobalReg]
activeCallerSavesRegs = (GlobalReg -> Bool) -> [GlobalReg] -> [GlobalReg]
forall a. (a -> Bool) -> [a] -> [a]
filter (Platform -> GlobalReg -> Bool
callerSaves Platform
platform) [GlobalReg]
activeRegs
foreignTargetRegs :: ForeignTarget -> [GlobalReg]
foreignTargetRegs (ForeignTarget _ (ForeignConvention _ _ _ CmmNeverReturns)) = []
foreignTargetRegs _ = [GlobalReg]
activeCallerSavesRegs
mapForeignTarget :: (CmmExpr -> CmmExpr) -> ForeignTarget -> ForeignTarget
mapForeignTarget :: (CmmExpr -> CmmExpr) -> ForeignTarget -> ForeignTarget
mapForeignTarget exp :: CmmExpr -> CmmExpr
exp (ForeignTarget e :: CmmExpr
e c :: ForeignConvention
c) = CmmExpr -> ForeignConvention -> ForeignTarget
ForeignTarget (CmmExpr -> CmmExpr
exp CmmExpr
e) ForeignConvention
c
mapForeignTarget _ m :: ForeignTarget
m@(PrimTarget _) = ForeignTarget
m
wrapRecExp :: (CmmExpr -> CmmExpr) -> CmmExpr -> CmmExpr
wrapRecExp :: (CmmExpr -> CmmExpr) -> CmmExpr -> CmmExpr
wrapRecExp f :: CmmExpr -> CmmExpr
f (CmmMachOp op :: MachOp
op es :: [CmmExpr]
es) = CmmExpr -> CmmExpr
f (MachOp -> [CmmExpr] -> CmmExpr
CmmMachOp MachOp
op ([CmmExpr] -> CmmExpr) -> [CmmExpr] -> CmmExpr
forall a b. (a -> b) -> a -> b
$ (CmmExpr -> CmmExpr) -> [CmmExpr] -> [CmmExpr]
forall a b. (a -> b) -> [a] -> [b]
map ((CmmExpr -> CmmExpr) -> CmmExpr -> CmmExpr
wrapRecExp CmmExpr -> CmmExpr
f) [CmmExpr]
es)
wrapRecExp f :: CmmExpr -> CmmExpr
f (CmmLoad addr :: CmmExpr
addr ty :: CmmType
ty) = CmmExpr -> CmmExpr
f (CmmExpr -> CmmType -> CmmExpr
CmmLoad ((CmmExpr -> CmmExpr) -> CmmExpr -> CmmExpr
wrapRecExp CmmExpr -> CmmExpr
f CmmExpr
addr) CmmType
ty)
wrapRecExp f :: CmmExpr -> CmmExpr
f e :: CmmExpr
e = CmmExpr -> CmmExpr
f CmmExpr
e
mapExp :: (CmmExpr -> CmmExpr) -> CmmNode e x -> CmmNode e x
mapExp :: (CmmExpr -> CmmExpr) -> CmmNode e x -> CmmNode e x
mapExp _ f :: CmmNode e x
f@(CmmEntry{}) = CmmNode e x
f
mapExp _ m :: CmmNode e x
m@(CmmComment _) = CmmNode e x
m
mapExp _ m :: CmmNode e x
m@(CmmTick _) = CmmNode e x
m
mapExp f :: CmmExpr -> CmmExpr
f (CmmUnwind regs :: [(GlobalReg, Maybe CmmExpr)]
regs) = [(GlobalReg, Maybe CmmExpr)] -> CmmNode O O
CmmUnwind (((GlobalReg, Maybe CmmExpr) -> (GlobalReg, Maybe CmmExpr))
-> [(GlobalReg, Maybe CmmExpr)] -> [(GlobalReg, Maybe CmmExpr)]
forall a b. (a -> b) -> [a] -> [b]
map ((Maybe CmmExpr -> Maybe CmmExpr)
-> (GlobalReg, Maybe CmmExpr) -> (GlobalReg, Maybe CmmExpr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((CmmExpr -> CmmExpr) -> Maybe CmmExpr -> Maybe CmmExpr
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap CmmExpr -> CmmExpr
f)) [(GlobalReg, Maybe CmmExpr)]
regs)
mapExp f :: CmmExpr -> CmmExpr
f (CmmAssign r :: CmmReg
r e :: CmmExpr
e) = CmmReg -> CmmExpr -> CmmNode O O
CmmAssign CmmReg
r (CmmExpr -> CmmExpr
f CmmExpr
e)
mapExp f :: CmmExpr -> CmmExpr
f (CmmStore addr :: CmmExpr
addr e :: CmmExpr
e) = CmmExpr -> CmmExpr -> CmmNode O O
CmmStore (CmmExpr -> CmmExpr
f CmmExpr
addr) (CmmExpr -> CmmExpr
f CmmExpr
e)
mapExp f :: CmmExpr -> CmmExpr
f (CmmUnsafeForeignCall tgt :: ForeignTarget
tgt fs :: [CmmFormal]
fs as :: [CmmExpr]
as) = ForeignTarget -> [CmmFormal] -> [CmmExpr] -> CmmNode O O
CmmUnsafeForeignCall ((CmmExpr -> CmmExpr) -> ForeignTarget -> ForeignTarget
mapForeignTarget CmmExpr -> CmmExpr
f ForeignTarget
tgt) [CmmFormal]
fs ((CmmExpr -> CmmExpr) -> [CmmExpr] -> [CmmExpr]
forall a b. (a -> b) -> [a] -> [b]
map CmmExpr -> CmmExpr
f [CmmExpr]
as)
mapExp _ l :: CmmNode e x
l@(CmmBranch _) = CmmNode e x
l
mapExp f :: CmmExpr -> CmmExpr
f (CmmCondBranch e :: CmmExpr
e ti :: Label
ti fi :: Label
fi l :: Maybe Bool
l) = CmmExpr -> Label -> Label -> Maybe Bool -> CmmNode O C
CmmCondBranch (CmmExpr -> CmmExpr
f CmmExpr
e) Label
ti Label
fi Maybe Bool
l
mapExp f :: CmmExpr -> CmmExpr
f (CmmSwitch e :: CmmExpr
e ids :: SwitchTargets
ids) = CmmExpr -> SwitchTargets -> CmmNode O C
CmmSwitch (CmmExpr -> CmmExpr
f CmmExpr
e) SwitchTargets
ids
mapExp f :: CmmExpr -> CmmExpr
f n :: CmmNode e x
n@CmmCall {cml_target :: CmmNode O C -> CmmExpr
cml_target=CmmExpr
tgt} = CmmNode e x
n{cml_target :: CmmExpr
cml_target = CmmExpr -> CmmExpr
f CmmExpr
tgt}
mapExp f :: CmmExpr -> CmmExpr
f (CmmForeignCall tgt :: ForeignTarget
tgt fs :: [CmmFormal]
fs as :: [CmmExpr]
as succ :: Label
succ ret_args :: ByteOff
ret_args updfr :: ByteOff
updfr intrbl :: Bool
intrbl) = ForeignTarget
-> [CmmFormal]
-> [CmmExpr]
-> Label
-> ByteOff
-> ByteOff
-> Bool
-> CmmNode O C
CmmForeignCall ((CmmExpr -> CmmExpr) -> ForeignTarget -> ForeignTarget
mapForeignTarget CmmExpr -> CmmExpr
f ForeignTarget
tgt) [CmmFormal]
fs ((CmmExpr -> CmmExpr) -> [CmmExpr] -> [CmmExpr]
forall a b. (a -> b) -> [a] -> [b]
map CmmExpr -> CmmExpr
f [CmmExpr]
as) Label
succ ByteOff
ret_args ByteOff
updfr Bool
intrbl
mapExpDeep :: (CmmExpr -> CmmExpr) -> CmmNode e x -> CmmNode e x
mapExpDeep :: (CmmExpr -> CmmExpr) -> CmmNode e x -> CmmNode e x
mapExpDeep f :: CmmExpr -> CmmExpr
f = (CmmExpr -> CmmExpr) -> CmmNode e x -> CmmNode e x
forall e x. (CmmExpr -> CmmExpr) -> CmmNode e x -> CmmNode e x
mapExp ((CmmExpr -> CmmExpr) -> CmmNode e x -> CmmNode e x)
-> (CmmExpr -> CmmExpr) -> CmmNode e x -> CmmNode e x
forall a b. (a -> b) -> a -> b
$ (CmmExpr -> CmmExpr) -> CmmExpr -> CmmExpr
wrapRecExp CmmExpr -> CmmExpr
f
mapForeignTargetM :: (CmmExpr -> Maybe CmmExpr) -> ForeignTarget -> Maybe ForeignTarget
mapForeignTargetM :: (CmmExpr -> Maybe CmmExpr) -> ForeignTarget -> Maybe ForeignTarget
mapForeignTargetM f :: CmmExpr -> Maybe CmmExpr
f (ForeignTarget e :: CmmExpr
e c :: ForeignConvention
c) = (\x :: CmmExpr
x -> CmmExpr -> ForeignConvention -> ForeignTarget
ForeignTarget CmmExpr
x ForeignConvention
c) (CmmExpr -> ForeignTarget) -> Maybe CmmExpr -> Maybe ForeignTarget
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` CmmExpr -> Maybe CmmExpr
f CmmExpr
e
mapForeignTargetM _ (PrimTarget _) = Maybe ForeignTarget
forall a. Maybe a
Nothing
wrapRecExpM :: (CmmExpr -> Maybe CmmExpr) -> (CmmExpr -> Maybe CmmExpr)
wrapRecExpM :: (CmmExpr -> Maybe CmmExpr) -> CmmExpr -> Maybe CmmExpr
wrapRecExpM f :: CmmExpr -> Maybe CmmExpr
f n :: CmmExpr
n@(CmmMachOp op :: MachOp
op es :: [CmmExpr]
es) = Maybe CmmExpr
-> ([CmmExpr] -> Maybe CmmExpr) -> Maybe [CmmExpr] -> Maybe CmmExpr
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (CmmExpr -> Maybe CmmExpr
f CmmExpr
n) (CmmExpr -> Maybe CmmExpr
f (CmmExpr -> Maybe CmmExpr)
-> ([CmmExpr] -> CmmExpr) -> [CmmExpr] -> Maybe CmmExpr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MachOp -> [CmmExpr] -> CmmExpr
CmmMachOp MachOp
op) ((CmmExpr -> Maybe CmmExpr) -> [CmmExpr] -> Maybe [CmmExpr]
forall a. (a -> Maybe a) -> [a] -> Maybe [a]
mapListM ((CmmExpr -> Maybe CmmExpr) -> CmmExpr -> Maybe CmmExpr
wrapRecExpM CmmExpr -> Maybe CmmExpr
f) [CmmExpr]
es)
wrapRecExpM f :: CmmExpr -> Maybe CmmExpr
f n :: CmmExpr
n@(CmmLoad addr :: CmmExpr
addr ty :: CmmType
ty) = Maybe CmmExpr
-> (CmmExpr -> Maybe CmmExpr) -> Maybe CmmExpr -> Maybe CmmExpr
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (CmmExpr -> Maybe CmmExpr
f CmmExpr
n) (CmmExpr -> Maybe CmmExpr
f (CmmExpr -> Maybe CmmExpr)
-> (CmmExpr -> CmmExpr) -> CmmExpr -> Maybe CmmExpr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (CmmExpr -> CmmType -> CmmExpr) -> CmmType -> CmmExpr -> CmmExpr
forall a b c. (a -> b -> c) -> b -> a -> c
flip CmmExpr -> CmmType -> CmmExpr
CmmLoad CmmType
ty) ((CmmExpr -> Maybe CmmExpr) -> CmmExpr -> Maybe CmmExpr
wrapRecExpM CmmExpr -> Maybe CmmExpr
f CmmExpr
addr)
wrapRecExpM f :: CmmExpr -> Maybe CmmExpr
f e :: CmmExpr
e = CmmExpr -> Maybe CmmExpr
f CmmExpr
e
mapExpM :: (CmmExpr -> Maybe CmmExpr) -> CmmNode e x -> Maybe (CmmNode e x)
mapExpM :: (CmmExpr -> Maybe CmmExpr) -> CmmNode e x -> Maybe (CmmNode e x)
mapExpM _ (CmmEntry{}) = Maybe (CmmNode e x)
forall a. Maybe a
Nothing
mapExpM _ (CmmComment _) = Maybe (CmmNode e x)
forall a. Maybe a
Nothing
mapExpM _ (CmmTick _) = Maybe (CmmNode e x)
forall a. Maybe a
Nothing
mapExpM f :: CmmExpr -> Maybe CmmExpr
f (CmmUnwind regs :: [(GlobalReg, Maybe CmmExpr)]
regs) = [(GlobalReg, Maybe CmmExpr)] -> CmmNode O O
CmmUnwind ([(GlobalReg, Maybe CmmExpr)] -> CmmNode O O)
-> Maybe [(GlobalReg, Maybe CmmExpr)] -> Maybe (CmmNode O O)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` ((GlobalReg, Maybe CmmExpr) -> Maybe (GlobalReg, Maybe CmmExpr))
-> [(GlobalReg, Maybe CmmExpr)]
-> Maybe [(GlobalReg, Maybe CmmExpr)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (\(r :: GlobalReg
r,e :: Maybe CmmExpr
e) -> (CmmExpr -> Maybe CmmExpr)
-> Maybe CmmExpr -> Maybe (Maybe CmmExpr)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM CmmExpr -> Maybe CmmExpr
f Maybe CmmExpr
e Maybe (Maybe CmmExpr)
-> (Maybe CmmExpr -> Maybe (GlobalReg, Maybe CmmExpr))
-> Maybe (GlobalReg, Maybe CmmExpr)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \e' :: Maybe CmmExpr
e' -> (GlobalReg, Maybe CmmExpr) -> Maybe (GlobalReg, Maybe CmmExpr)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (GlobalReg
r,Maybe CmmExpr
e')) [(GlobalReg, Maybe CmmExpr)]
regs
mapExpM f :: CmmExpr -> Maybe CmmExpr
f (CmmAssign r :: CmmReg
r e :: CmmExpr
e) = CmmReg -> CmmExpr -> CmmNode O O
CmmAssign CmmReg
r (CmmExpr -> CmmNode O O) -> Maybe CmmExpr -> Maybe (CmmNode O O)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` CmmExpr -> Maybe CmmExpr
f CmmExpr
e
mapExpM f :: CmmExpr -> Maybe CmmExpr
f (CmmStore addr :: CmmExpr
addr e :: CmmExpr
e) = (\[addr' :: CmmExpr
addr', e' :: CmmExpr
e'] -> CmmExpr -> CmmExpr -> CmmNode O O
CmmStore CmmExpr
addr' CmmExpr
e') ([CmmExpr] -> CmmNode O O)
-> Maybe [CmmExpr] -> Maybe (CmmNode O O)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` (CmmExpr -> Maybe CmmExpr) -> [CmmExpr] -> Maybe [CmmExpr]
forall a. (a -> Maybe a) -> [a] -> Maybe [a]
mapListM CmmExpr -> Maybe CmmExpr
f [CmmExpr
addr, CmmExpr
e]
mapExpM _ (CmmBranch _) = Maybe (CmmNode e x)
forall a. Maybe a
Nothing
mapExpM f :: CmmExpr -> Maybe CmmExpr
f (CmmCondBranch e :: CmmExpr
e ti :: Label
ti fi :: Label
fi l :: Maybe Bool
l) = (\x :: CmmExpr
x -> CmmExpr -> Label -> Label -> Maybe Bool -> CmmNode O C
CmmCondBranch CmmExpr
x Label
ti Label
fi Maybe Bool
l) (CmmExpr -> CmmNode O C) -> Maybe CmmExpr -> Maybe (CmmNode O C)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` CmmExpr -> Maybe CmmExpr
f CmmExpr
e
mapExpM f :: CmmExpr -> Maybe CmmExpr
f (CmmSwitch e :: CmmExpr
e tbl :: SwitchTargets
tbl) = (\x :: CmmExpr
x -> CmmExpr -> SwitchTargets -> CmmNode O C
CmmSwitch CmmExpr
x SwitchTargets
tbl) (CmmExpr -> CmmNode O C) -> Maybe CmmExpr -> Maybe (CmmNode O C)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` CmmExpr -> Maybe CmmExpr
f CmmExpr
e
mapExpM f :: CmmExpr -> Maybe CmmExpr
f (CmmCall tgt :: CmmExpr
tgt mb_id :: Maybe Label
mb_id r :: [GlobalReg]
r o :: ByteOff
o i :: ByteOff
i s :: ByteOff
s) = (\x :: CmmExpr
x -> CmmExpr
-> Maybe Label
-> [GlobalReg]
-> ByteOff
-> ByteOff
-> ByteOff
-> CmmNode O C
CmmCall CmmExpr
x Maybe Label
mb_id [GlobalReg]
r ByteOff
o ByteOff
i ByteOff
s) (CmmExpr -> CmmNode O C) -> Maybe CmmExpr -> Maybe (CmmNode O C)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` CmmExpr -> Maybe CmmExpr
f CmmExpr
tgt
mapExpM f :: CmmExpr -> Maybe CmmExpr
f (CmmUnsafeForeignCall tgt :: ForeignTarget
tgt fs :: [CmmFormal]
fs as :: [CmmExpr]
as)
= case (CmmExpr -> Maybe CmmExpr) -> ForeignTarget -> Maybe ForeignTarget
mapForeignTargetM CmmExpr -> Maybe CmmExpr
f ForeignTarget
tgt of
Just tgt' :: ForeignTarget
tgt' -> CmmNode O O -> Maybe (CmmNode O O)
forall a. a -> Maybe a
Just (ForeignTarget -> [CmmFormal] -> [CmmExpr] -> CmmNode O O
CmmUnsafeForeignCall ForeignTarget
tgt' [CmmFormal]
fs ((CmmExpr -> Maybe CmmExpr) -> [CmmExpr] -> [CmmExpr]
forall a. (a -> Maybe a) -> [a] -> [a]
mapListJ CmmExpr -> Maybe CmmExpr
f [CmmExpr]
as))
Nothing -> (\xs :: [CmmExpr]
xs -> ForeignTarget -> [CmmFormal] -> [CmmExpr] -> CmmNode O O
CmmUnsafeForeignCall ForeignTarget
tgt [CmmFormal]
fs [CmmExpr]
xs) ([CmmExpr] -> CmmNode O O)
-> Maybe [CmmExpr] -> Maybe (CmmNode O O)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` (CmmExpr -> Maybe CmmExpr) -> [CmmExpr] -> Maybe [CmmExpr]
forall a. (a -> Maybe a) -> [a] -> Maybe [a]
mapListM CmmExpr -> Maybe CmmExpr
f [CmmExpr]
as
mapExpM f :: CmmExpr -> Maybe CmmExpr
f (CmmForeignCall tgt :: ForeignTarget
tgt fs :: [CmmFormal]
fs as :: [CmmExpr]
as succ :: Label
succ ret_args :: ByteOff
ret_args updfr :: ByteOff
updfr intrbl :: Bool
intrbl)
= case (CmmExpr -> Maybe CmmExpr) -> ForeignTarget -> Maybe ForeignTarget
mapForeignTargetM CmmExpr -> Maybe CmmExpr
f ForeignTarget
tgt of
Just tgt' :: ForeignTarget
tgt' -> CmmNode O C -> Maybe (CmmNode O C)
forall a. a -> Maybe a
Just (ForeignTarget
-> [CmmFormal]
-> [CmmExpr]
-> Label
-> ByteOff
-> ByteOff
-> Bool
-> CmmNode O C
CmmForeignCall ForeignTarget
tgt' [CmmFormal]
fs ((CmmExpr -> Maybe CmmExpr) -> [CmmExpr] -> [CmmExpr]
forall a. (a -> Maybe a) -> [a] -> [a]
mapListJ CmmExpr -> Maybe CmmExpr
f [CmmExpr]
as) Label
succ ByteOff
ret_args ByteOff
updfr Bool
intrbl)
Nothing -> (\xs :: [CmmExpr]
xs -> ForeignTarget
-> [CmmFormal]
-> [CmmExpr]
-> Label
-> ByteOff
-> ByteOff
-> Bool
-> CmmNode O C
CmmForeignCall ForeignTarget
tgt [CmmFormal]
fs [CmmExpr]
xs Label
succ ByteOff
ret_args ByteOff
updfr Bool
intrbl) ([CmmExpr] -> CmmNode O C)
-> Maybe [CmmExpr] -> Maybe (CmmNode O C)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` (CmmExpr -> Maybe CmmExpr) -> [CmmExpr] -> Maybe [CmmExpr]
forall a. (a -> Maybe a) -> [a] -> Maybe [a]
mapListM CmmExpr -> Maybe CmmExpr
f [CmmExpr]
as
mapListM :: (a -> Maybe a) -> [a] -> Maybe [a]
mapListM :: (a -> Maybe a) -> [a] -> Maybe [a]
mapListM f :: a -> Maybe a
f xs :: [a]
xs = let (b :: Bool
b, r :: [a]
r) = (a -> Maybe a) -> [a] -> (Bool, [a])
forall a. (a -> Maybe a) -> [a] -> (Bool, [a])
mapListT a -> Maybe a
f [a]
xs
in if Bool
b then [a] -> Maybe [a]
forall a. a -> Maybe a
Just [a]
r else Maybe [a]
forall a. Maybe a
Nothing
mapListJ :: (a -> Maybe a) -> [a] -> [a]
mapListJ :: (a -> Maybe a) -> [a] -> [a]
mapListJ f :: a -> Maybe a
f xs :: [a]
xs = (Bool, [a]) -> [a]
forall a b. (a, b) -> b
snd ((a -> Maybe a) -> [a] -> (Bool, [a])
forall a. (a -> Maybe a) -> [a] -> (Bool, [a])
mapListT a -> Maybe a
f [a]
xs)
mapListT :: (a -> Maybe a) -> [a] -> (Bool, [a])
mapListT :: (a -> Maybe a) -> [a] -> (Bool, [a])
mapListT f :: a -> Maybe a
f xs :: [a]
xs = (([a], a, Maybe a) -> (Bool, [a]) -> (Bool, [a]))
-> (Bool, [a]) -> [([a], a, Maybe a)] -> (Bool, [a])
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ([a], a, Maybe a) -> (Bool, [a]) -> (Bool, [a])
forall a. ([a], a, Maybe a) -> (Bool, [a]) -> (Bool, [a])
g (Bool
False, []) ([[a]] -> [a] -> [Maybe a] -> [([a], a, Maybe a)]
forall a b c. [a] -> [b] -> [c] -> [(a, b, c)]
zip3 ([a] -> [[a]]
forall a. [a] -> [[a]]
tails [a]
xs) [a]
xs ((a -> Maybe a) -> [a] -> [Maybe a]
forall a b. (a -> b) -> [a] -> [b]
map a -> Maybe a
f [a]
xs))
where g :: ([a], a, Maybe a) -> (Bool, [a]) -> (Bool, [a])
g (_, y :: a
y, Nothing) (True, ys :: [a]
ys) = (Bool
True, a
ya -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
ys)
g (_, _, Just y :: a
y) (True, ys :: [a]
ys) = (Bool
True, a
ya -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
ys)
g (ys' :: [a]
ys', _, Nothing) (False, _) = (Bool
False, [a]
ys')
g (_, _, Just y :: a
y) (False, ys :: [a]
ys) = (Bool
True, a
ya -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
ys)
mapExpDeepM :: (CmmExpr -> Maybe CmmExpr) -> CmmNode e x -> Maybe (CmmNode e x)
mapExpDeepM :: (CmmExpr -> Maybe CmmExpr) -> CmmNode e x -> Maybe (CmmNode e x)
mapExpDeepM f :: CmmExpr -> Maybe CmmExpr
f = (CmmExpr -> Maybe CmmExpr) -> CmmNode e x -> Maybe (CmmNode e x)
forall e x.
(CmmExpr -> Maybe CmmExpr) -> CmmNode e x -> Maybe (CmmNode e x)
mapExpM ((CmmExpr -> Maybe CmmExpr) -> CmmNode e x -> Maybe (CmmNode e x))
-> (CmmExpr -> Maybe CmmExpr) -> CmmNode e x -> Maybe (CmmNode e x)
forall a b. (a -> b) -> a -> b
$ (CmmExpr -> Maybe CmmExpr) -> CmmExpr -> Maybe CmmExpr
wrapRecExpM CmmExpr -> Maybe CmmExpr
f
foldExpForeignTarget :: (CmmExpr -> z -> z) -> ForeignTarget -> z -> z
foldExpForeignTarget :: (CmmExpr -> z -> z) -> ForeignTarget -> z -> z
foldExpForeignTarget exp :: CmmExpr -> z -> z
exp (ForeignTarget e :: CmmExpr
e _) z :: z
z = CmmExpr -> z -> z
exp CmmExpr
e z
z
foldExpForeignTarget _ (PrimTarget _) z :: z
z = z
z
wrapRecExpf :: (CmmExpr -> z -> z) -> CmmExpr -> z -> z
wrapRecExpf :: (CmmExpr -> z -> z) -> CmmExpr -> z -> z
wrapRecExpf f :: CmmExpr -> z -> z
f e :: CmmExpr
e@(CmmMachOp _ es :: [CmmExpr]
es) z :: z
z = (CmmExpr -> z -> z) -> z -> [CmmExpr] -> z
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((CmmExpr -> z -> z) -> CmmExpr -> z -> z
forall z. (CmmExpr -> z -> z) -> CmmExpr -> z -> z
wrapRecExpf CmmExpr -> z -> z
f) (CmmExpr -> z -> z
f CmmExpr
e z
z) [CmmExpr]
es
wrapRecExpf f :: CmmExpr -> z -> z
f e :: CmmExpr
e@(CmmLoad addr :: CmmExpr
addr _) z :: z
z = (CmmExpr -> z -> z) -> CmmExpr -> z -> z
forall z. (CmmExpr -> z -> z) -> CmmExpr -> z -> z
wrapRecExpf CmmExpr -> z -> z
f CmmExpr
addr (CmmExpr -> z -> z
f CmmExpr
e z
z)
wrapRecExpf f :: CmmExpr -> z -> z
f e :: CmmExpr
e z :: z
z = CmmExpr -> z -> z
f CmmExpr
e z
z
foldExp :: (CmmExpr -> z -> z) -> CmmNode e x -> z -> z
foldExp :: (CmmExpr -> z -> z) -> CmmNode e x -> z -> z
foldExp _ (CmmEntry {}) z :: z
z = z
z
foldExp _ (CmmComment {}) z :: z
z = z
z
foldExp _ (CmmTick {}) z :: z
z = z
z
foldExp f :: CmmExpr -> z -> z
f (CmmUnwind xs :: [(GlobalReg, Maybe CmmExpr)]
xs) z :: z
z = (Maybe CmmExpr -> z -> z) -> z -> [Maybe CmmExpr] -> z
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr ((z -> z) -> (CmmExpr -> z -> z) -> Maybe CmmExpr -> z -> z
forall b a. b -> (a -> b) -> Maybe a -> b
maybe z -> z
forall a. a -> a
id CmmExpr -> z -> z
f) z
z (((GlobalReg, Maybe CmmExpr) -> Maybe CmmExpr)
-> [(GlobalReg, Maybe CmmExpr)] -> [Maybe CmmExpr]
forall a b. (a -> b) -> [a] -> [b]
map (GlobalReg, Maybe CmmExpr) -> Maybe CmmExpr
forall a b. (a, b) -> b
snd [(GlobalReg, Maybe CmmExpr)]
xs)
foldExp f :: CmmExpr -> z -> z
f (CmmAssign _ e :: CmmExpr
e) z :: z
z = CmmExpr -> z -> z
f CmmExpr
e z
z
foldExp f :: CmmExpr -> z -> z
f (CmmStore addr :: CmmExpr
addr e :: CmmExpr
e) z :: z
z = CmmExpr -> z -> z
f CmmExpr
addr (z -> z) -> z -> z
forall a b. (a -> b) -> a -> b
$ CmmExpr -> z -> z
f CmmExpr
e z
z
foldExp f :: CmmExpr -> z -> z
f (CmmUnsafeForeignCall t :: ForeignTarget
t _ as :: [CmmExpr]
as) z :: z
z = (CmmExpr -> z -> z) -> z -> [CmmExpr] -> z
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr CmmExpr -> z -> z
f ((CmmExpr -> z -> z) -> ForeignTarget -> z -> z
forall z. (CmmExpr -> z -> z) -> ForeignTarget -> z -> z
foldExpForeignTarget CmmExpr -> z -> z
f ForeignTarget
t z
z) [CmmExpr]
as
foldExp _ (CmmBranch _) z :: z
z = z
z
foldExp f :: CmmExpr -> z -> z
f (CmmCondBranch e :: CmmExpr
e _ _ _) z :: z
z = CmmExpr -> z -> z
f CmmExpr
e z
z
foldExp f :: CmmExpr -> z -> z
f (CmmSwitch e :: CmmExpr
e _) z :: z
z = CmmExpr -> z -> z
f CmmExpr
e z
z
foldExp f :: CmmExpr -> z -> z
f (CmmCall {cml_target :: CmmNode O C -> CmmExpr
cml_target=CmmExpr
tgt}) z :: z
z = CmmExpr -> z -> z
f CmmExpr
tgt z
z
foldExp f :: CmmExpr -> z -> z
f (CmmForeignCall {tgt :: CmmNode O C -> ForeignTarget
tgt=ForeignTarget
tgt, args :: CmmNode O C -> [CmmExpr]
args=[CmmExpr]
args}) z :: z
z = (CmmExpr -> z -> z) -> z -> [CmmExpr] -> z
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr CmmExpr -> z -> z
f ((CmmExpr -> z -> z) -> ForeignTarget -> z -> z
forall z. (CmmExpr -> z -> z) -> ForeignTarget -> z -> z
foldExpForeignTarget CmmExpr -> z -> z
f ForeignTarget
tgt z
z) [CmmExpr]
args
foldExpDeep :: (CmmExpr -> z -> z) -> CmmNode e x -> z -> z
foldExpDeep :: (CmmExpr -> z -> z) -> CmmNode e x -> z -> z
foldExpDeep f :: CmmExpr -> z -> z
f = (CmmExpr -> z -> z) -> CmmNode e x -> z -> z
forall z e x. (CmmExpr -> z -> z) -> CmmNode e x -> z -> z
foldExp ((CmmExpr -> z -> z) -> CmmExpr -> z -> z
forall z. (CmmExpr -> z -> z) -> CmmExpr -> z -> z
wrapRecExpf CmmExpr -> z -> z
f)
mapSuccessors :: (Label -> Label) -> CmmNode O C -> CmmNode O C
mapSuccessors :: (Label -> Label) -> CmmNode O C -> CmmNode O C
mapSuccessors f :: Label -> Label
f (CmmBranch bid :: Label
bid) = Label -> CmmNode O C
CmmBranch (Label -> Label
f Label
bid)
mapSuccessors f :: Label -> Label
f (CmmCondBranch p :: CmmExpr
p y :: Label
y n :: Label
n l :: Maybe Bool
l) = CmmExpr -> Label -> Label -> Maybe Bool -> CmmNode O C
CmmCondBranch CmmExpr
p (Label -> Label
f Label
y) (Label -> Label
f Label
n) Maybe Bool
l
mapSuccessors f :: Label -> Label
f (CmmSwitch e :: CmmExpr
e ids :: SwitchTargets
ids) = CmmExpr -> SwitchTargets -> CmmNode O C
CmmSwitch CmmExpr
e ((Label -> Label) -> SwitchTargets -> SwitchTargets
mapSwitchTargets Label -> Label
f SwitchTargets
ids)
mapSuccessors _ n :: CmmNode O C
n = CmmNode O C
n
mapCollectSuccessors :: forall a. (Label -> (Label,a)) -> CmmNode O C
-> (CmmNode O C, [a])
mapCollectSuccessors :: (Label -> (Label, a)) -> CmmNode O C -> (CmmNode O C, [a])
mapCollectSuccessors f :: Label -> (Label, a)
f (CmmBranch bid :: Label
bid)
= let (bid' :: Label
bid', acc :: a
acc) = Label -> (Label, a)
f Label
bid in (Label -> CmmNode O C
CmmBranch Label
bid', [a
acc])
mapCollectSuccessors f :: Label -> (Label, a)
f (CmmCondBranch p :: CmmExpr
p y :: Label
y n :: Label
n l :: Maybe Bool
l)
= let (bidt :: Label
bidt, acct :: a
acct) = Label -> (Label, a)
f Label
y
(bidf :: Label
bidf, accf :: a
accf) = Label -> (Label, a)
f Label
n
in (CmmExpr -> Label -> Label -> Maybe Bool -> CmmNode O C
CmmCondBranch CmmExpr
p Label
bidt Label
bidf Maybe Bool
l, [a
accf, a
acct])
mapCollectSuccessors f :: Label -> (Label, a)
f (CmmSwitch e :: CmmExpr
e ids :: SwitchTargets
ids)
= let lbls :: [Label]
lbls = SwitchTargets -> [Label]
switchTargetsToList SwitchTargets
ids :: [Label]
lblMap :: LabelMap (Label, a)
lblMap = [(KeyOf LabelMap, (Label, a))] -> LabelMap (Label, a)
forall (map :: * -> *) a. IsMap map => [(KeyOf map, a)] -> map a
mapFromList ([(KeyOf LabelMap, (Label, a))] -> LabelMap (Label, a))
-> [(KeyOf LabelMap, (Label, a))] -> LabelMap (Label, a)
forall a b. (a -> b) -> a -> b
$ [Label] -> [(Label, a)] -> [(Label, (Label, a))]
forall a b. [a] -> [b] -> [(a, b)]
zip [Label]
lbls ((Label -> (Label, a)) -> [Label] -> [(Label, a)]
forall a b. (a -> b) -> [a] -> [b]
map Label -> (Label, a)
f [Label]
lbls) :: LabelMap (Label, a)
in ( CmmExpr -> SwitchTargets -> CmmNode O C
CmmSwitch CmmExpr
e
((Label -> Label) -> SwitchTargets -> SwitchTargets
mapSwitchTargets
(\l :: Label
l -> (Label, a) -> Label
forall a b. (a, b) -> a
fst ((Label, a) -> Label) -> (Label, a) -> Label
forall a b. (a -> b) -> a -> b
$ (Label, a) -> KeyOf LabelMap -> LabelMap (Label, a) -> (Label, a)
forall (map :: * -> *) a. IsMap map => a -> KeyOf map -> map a -> a
mapFindWithDefault ([Char] -> (Label, a)
forall a. HasCallStack => [Char] -> a
error "impossible") KeyOf LabelMap
Label
l LabelMap (Label, a)
lblMap) SwitchTargets
ids)
, ((Label, a) -> a) -> [(Label, a)] -> [a]
forall a b. (a -> b) -> [a] -> [b]
map (Label, a) -> a
forall a b. (a, b) -> b
snd (LabelMap (Label, a) -> [(Label, a)]
forall (map :: * -> *) a. IsMap map => map a -> [a]
mapElems LabelMap (Label, a)
lblMap)
)
mapCollectSuccessors _ n :: CmmNode O C
n = (CmmNode O C
n, [])
type CmmTickish = Tickish ()
data CmmTickScope
= GlobalScope
| SubScope !U.Unique CmmTickScope
| CombinedScope CmmTickScope CmmTickScope
scopeToPaths :: CmmTickScope -> [[U.Unique]]
scopeToPaths :: CmmTickScope -> [[Unique]]
scopeToPaths GlobalScope = [[]]
scopeToPaths (SubScope u :: Unique
u s :: CmmTickScope
s) = ([Unique] -> [Unique]) -> [[Unique]] -> [[Unique]]
forall a b. (a -> b) -> [a] -> [b]
map (Unique
uUnique -> [Unique] -> [Unique]
forall a. a -> [a] -> [a]
:) (CmmTickScope -> [[Unique]]
scopeToPaths CmmTickScope
s)
scopeToPaths (CombinedScope s1 :: CmmTickScope
s1 s2 :: CmmTickScope
s2) = CmmTickScope -> [[Unique]]
scopeToPaths CmmTickScope
s1 [[Unique]] -> [[Unique]] -> [[Unique]]
forall a. [a] -> [a] -> [a]
++ CmmTickScope -> [[Unique]]
scopeToPaths CmmTickScope
s2
scopeUniques :: CmmTickScope -> [U.Unique]
scopeUniques :: CmmTickScope -> [Unique]
scopeUniques GlobalScope = []
scopeUniques (SubScope u :: Unique
u _) = [Unique
u]
scopeUniques (CombinedScope s1 :: CmmTickScope
s1 s2 :: CmmTickScope
s2) = CmmTickScope -> [Unique]
scopeUniques CmmTickScope
s1 [Unique] -> [Unique] -> [Unique]
forall a. [a] -> [a] -> [a]
++ CmmTickScope -> [Unique]
scopeUniques CmmTickScope
s2
instance Eq CmmTickScope where
GlobalScope == :: CmmTickScope -> CmmTickScope -> Bool
== GlobalScope = Bool
True
GlobalScope == _ = Bool
False
_ == GlobalScope = Bool
False
(SubScope u :: Unique
u _) == (SubScope u' :: Unique
u' _) = Unique
u Unique -> Unique -> Bool
forall a. Eq a => a -> a -> Bool
== Unique
u'
(SubScope _ _) == _ = Bool
False
_ == (SubScope _ _) = Bool
False
scope :: CmmTickScope
scope == scope' :: CmmTickScope
scope' =
(Unique -> Unique -> Ordering) -> [Unique] -> [Unique]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy Unique -> Unique -> Ordering
nonDetCmpUnique (CmmTickScope -> [Unique]
scopeUniques CmmTickScope
scope) [Unique] -> [Unique] -> Bool
forall a. Eq a => a -> a -> Bool
==
(Unique -> Unique -> Ordering) -> [Unique] -> [Unique]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy Unique -> Unique -> Ordering
nonDetCmpUnique (CmmTickScope -> [Unique]
scopeUniques CmmTickScope
scope')
instance Ord CmmTickScope where
compare :: CmmTickScope -> CmmTickScope -> Ordering
compare GlobalScope GlobalScope = Ordering
EQ
compare GlobalScope _ = Ordering
LT
compare _ GlobalScope = Ordering
GT
compare (SubScope u :: Unique
u _) (SubScope u' :: Unique
u' _) = Unique -> Unique -> Ordering
nonDetCmpUnique Unique
u Unique
u'
compare scope :: CmmTickScope
scope scope' :: CmmTickScope
scope' = (Unique -> Unique -> Ordering) -> [Unique] -> [Unique] -> Ordering
forall a. (a -> a -> Ordering) -> [a] -> [a] -> Ordering
cmpList Unique -> Unique -> Ordering
nonDetCmpUnique
((Unique -> Unique -> Ordering) -> [Unique] -> [Unique]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy Unique -> Unique -> Ordering
nonDetCmpUnique ([Unique] -> [Unique]) -> [Unique] -> [Unique]
forall a b. (a -> b) -> a -> b
$ CmmTickScope -> [Unique]
scopeUniques CmmTickScope
scope)
((Unique -> Unique -> Ordering) -> [Unique] -> [Unique]
forall a. (a -> a -> Ordering) -> [a] -> [a]
sortBy Unique -> Unique -> Ordering
nonDetCmpUnique ([Unique] -> [Unique]) -> [Unique] -> [Unique]
forall a b. (a -> b) -> a -> b
$ CmmTickScope -> [Unique]
scopeUniques CmmTickScope
scope')
instance Outputable CmmTickScope where
ppr :: CmmTickScope -> SDoc
ppr GlobalScope = [Char] -> SDoc
text "global"
ppr (SubScope us :: Unique
us GlobalScope)
= Unique -> SDoc
forall a. Outputable a => a -> SDoc
ppr Unique
us
ppr (SubScope us :: Unique
us s :: CmmTickScope
s) = CmmTickScope -> SDoc
forall a. Outputable a => a -> SDoc
ppr CmmTickScope
s SDoc -> SDoc -> SDoc
<> Char -> SDoc
char '/' SDoc -> SDoc -> SDoc
<> Unique -> SDoc
forall a. Outputable a => a -> SDoc
ppr Unique
us
ppr combined :: CmmTickScope
combined = SDoc -> SDoc
parens (SDoc -> SDoc) -> SDoc -> SDoc
forall a b. (a -> b) -> a -> b
$ [SDoc] -> SDoc
hcat ([SDoc] -> SDoc) -> [SDoc] -> SDoc
forall a b. (a -> b) -> a -> b
$ SDoc -> [SDoc] -> [SDoc]
punctuate (Char -> SDoc
char '+') ([SDoc] -> [SDoc]) -> [SDoc] -> [SDoc]
forall a b. (a -> b) -> a -> b
$
([Unique] -> SDoc) -> [[Unique]] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map ([SDoc] -> SDoc
hcat ([SDoc] -> SDoc) -> ([Unique] -> [SDoc]) -> [Unique] -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SDoc -> [SDoc] -> [SDoc]
punctuate (Char -> SDoc
char '/') ([SDoc] -> [SDoc]) -> ([Unique] -> [SDoc]) -> [Unique] -> [SDoc]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Unique -> SDoc) -> [Unique] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map Unique -> SDoc
forall a. Outputable a => a -> SDoc
ppr ([Unique] -> [SDoc])
-> ([Unique] -> [Unique]) -> [Unique] -> [SDoc]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Unique] -> [Unique]
forall a. [a] -> [a]
reverse) ([[Unique]] -> [SDoc]) -> [[Unique]] -> [SDoc]
forall a b. (a -> b) -> a -> b
$
CmmTickScope -> [[Unique]]
scopeToPaths CmmTickScope
combined
isTickSubScope :: CmmTickScope -> CmmTickScope -> Bool
isTickSubScope :: CmmTickScope -> CmmTickScope -> Bool
isTickSubScope = CmmTickScope -> CmmTickScope -> Bool
cmp
where cmp :: CmmTickScope -> CmmTickScope -> Bool
cmp _ GlobalScope = Bool
True
cmp GlobalScope _ = Bool
False
cmp (CombinedScope s1 :: CmmTickScope
s1 s2 :: CmmTickScope
s2) s' :: CmmTickScope
s' = CmmTickScope -> CmmTickScope -> Bool
cmp CmmTickScope
s1 CmmTickScope
s' Bool -> Bool -> Bool
&& CmmTickScope -> CmmTickScope -> Bool
cmp CmmTickScope
s2 CmmTickScope
s'
cmp s :: CmmTickScope
s (CombinedScope s1' :: CmmTickScope
s1' s2' :: CmmTickScope
s2') = CmmTickScope -> CmmTickScope -> Bool
cmp CmmTickScope
s CmmTickScope
s1' Bool -> Bool -> Bool
|| CmmTickScope -> CmmTickScope -> Bool
cmp CmmTickScope
s CmmTickScope
s2'
cmp (SubScope u :: Unique
u s :: CmmTickScope
s) s' :: CmmTickScope
s'@(SubScope u' :: Unique
u' _) = Unique
u Unique -> Unique -> Bool
forall a. Eq a => a -> a -> Bool
== Unique
u' Bool -> Bool -> Bool
|| CmmTickScope -> CmmTickScope -> Bool
cmp CmmTickScope
s CmmTickScope
s'
combineTickScopes :: CmmTickScope -> CmmTickScope -> CmmTickScope
combineTickScopes :: CmmTickScope -> CmmTickScope -> CmmTickScope
combineTickScopes s1 :: CmmTickScope
s1 s2 :: CmmTickScope
s2
| CmmTickScope
s1 CmmTickScope -> CmmTickScope -> Bool
`isTickSubScope` CmmTickScope
s2 = CmmTickScope
s1
| CmmTickScope
s2 CmmTickScope -> CmmTickScope -> Bool
`isTickSubScope` CmmTickScope
s1 = CmmTickScope
s2
| Bool
otherwise = CmmTickScope -> CmmTickScope -> CmmTickScope
CombinedScope CmmTickScope
s1 CmmTickScope
s2