{-# LANGUAGE CPP #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE EmptyCase #-}
module GHC.Cmm.DebugBlock (
DebugBlock(..),
cmmDebugGen,
cmmDebugLabels,
cmmDebugLink,
debugToMap,
UnwindTable, UnwindPoint(..),
UnwindExpr(..), toUnwindExpr,
pprUnwindTable
) where
import GHC.Prelude
import GHC.Platform
import GHC.Cmm.BlockId
import GHC.Cmm.CLabel
import GHC.Cmm
import GHC.Cmm.Reg ( pprGlobalReg, pprGlobalRegUse )
import GHC.Cmm.Utils
import GHC.Data.FastString ( LexicalFastString, nilFS, mkFastString )
import GHC.Unit.Module
import GHC.Utils.Outputable
import GHC.Utils.Panic
import GHC.Types.SrcLoc
import GHC.Types.Tickish
import GHC.Utils.Misc ( seqList )
import GHC.Cmm.Dataflow.Block
import GHC.Cmm.Dataflow.Graph
import GHC.Cmm.Dataflow.Label
import Data.Maybe
import Data.List ( minimumBy, nubBy )
import Data.List.NonEmpty ( NonEmpty (..), nonEmpty )
import qualified Data.List.NonEmpty as NE
import Data.Ord ( comparing )
import qualified Data.Map as Map
import Data.Foldable ( toList )
import Data.Either ( partitionEithers )
import Data.Void
data DebugBlock =
DebugBlock
{ DebugBlock -> Label
dblProcedure :: !Label
, DebugBlock -> Label
dblLabel :: !Label
, DebugBlock -> CLabel
dblCLabel :: !CLabel
, DebugBlock -> Bool
dblHasInfoTbl :: !Bool
, DebugBlock -> Maybe DebugBlock
dblParent :: !(Maybe DebugBlock)
, DebugBlock -> [GenTickish 'TickishPassCmm]
dblTicks :: ![CmmTickish]
, DebugBlock -> Maybe (GenTickish 'TickishPassCmm)
dblSourceTick :: !(Maybe CmmTickish)
, DebugBlock -> Maybe Int
dblPosition :: !(Maybe Int)
, DebugBlock -> [UnwindPoint]
dblUnwind :: [UnwindPoint]
, DebugBlock -> [DebugBlock]
dblBlocks :: ![DebugBlock]
}
instance OutputableP Platform DebugBlock where
pdoc :: Platform -> DebugBlock -> SDoc
pdoc Platform
env DebugBlock
blk =
(if | DebugBlock -> Label
dblProcedure DebugBlock
blk Label -> Label -> Bool
forall a. Eq a => a -> a -> Bool
== DebugBlock -> Label
dblLabel DebugBlock
blk
-> String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"proc"
| DebugBlock -> Bool
dblHasInfoTbl DebugBlock
blk
-> String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"pp-blk"
| Bool
otherwise
-> String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"blk") SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+>
Label -> SDoc
forall a. Outputable a => a -> SDoc
ppr (DebugBlock -> Label
dblLabel DebugBlock
blk) SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc
parens (Platform -> CLabel -> SDoc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
env (DebugBlock -> CLabel
dblCLabel DebugBlock
blk)) SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+>
(SDoc
-> (GenTickish 'TickishPassCmm -> SDoc)
-> Maybe (GenTickish 'TickishPassCmm)
-> SDoc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe SDoc
forall doc. IsOutput doc => doc
empty GenTickish 'TickishPassCmm -> SDoc
forall a. Outputable a => a -> SDoc
ppr (DebugBlock -> Maybe (GenTickish 'TickishPassCmm)
dblSourceTick DebugBlock
blk)) SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+>
(SDoc -> (Int -> SDoc) -> Maybe Int -> SDoc
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"removed") ((String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"pos " SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<>) (SDoc -> SDoc) -> (Int -> SDoc) -> Int -> SDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> SDoc
forall a. Outputable a => a -> SDoc
ppr)
(DebugBlock -> Maybe Int
dblPosition DebugBlock
blk)) SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+>
(Platform -> [UnwindPoint] -> SDoc
forall env a. OutputableP env a => env -> a -> SDoc
pdoc Platform
env (DebugBlock -> [UnwindPoint]
dblUnwind DebugBlock
blk)) SDoc -> SDoc -> SDoc
$+$
(if [DebugBlock] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null (DebugBlock -> [DebugBlock]
dblBlocks DebugBlock
blk) then SDoc
forall doc. IsOutput doc => doc
empty else Int -> SDoc -> SDoc
nest Int
4 (Platform -> [DebugBlock] -> SDoc
forall env a. OutputableP env a => env -> a -> SDoc
pdoc Platform
env (DebugBlock -> [DebugBlock]
dblBlocks DebugBlock
blk)))
type BlockContext = (CmmBlock, RawCmmDeclNoStatics)
type RawCmmDeclNoStatics
= GenCmmDecl
Void
(LabelMap RawCmmStatics)
CmmGraph
cmmDebugGen :: ModLocation -> [RawCmmDecl] -> [DebugBlock]
cmmDebugGen :: ModLocation -> [RawCmmDecl] -> [DebugBlock]
cmmDebugGen ModLocation
modLoc [RawCmmDecl]
decls = ((CmmTickScope, NonEmpty BlockContext) -> DebugBlock)
-> [(CmmTickScope, NonEmpty BlockContext)] -> [DebugBlock]
forall a b. (a -> b) -> [a] -> [b]
map (Maybe (RealSrcSpan, LexicalFastString)
-> (CmmTickScope, NonEmpty BlockContext) -> DebugBlock
blocksForScope Maybe (RealSrcSpan, LexicalFastString)
forall a. Maybe a
Nothing) [(CmmTickScope, NonEmpty BlockContext)]
topScopes
where
blockCtxs :: Map.Map CmmTickScope (NonEmpty BlockContext)
blockCtxs :: Map CmmTickScope (NonEmpty BlockContext)
blockCtxs = [RawCmmDecl] -> Map CmmTickScope (NonEmpty BlockContext)
forall a.
[GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph]
-> Map CmmTickScope (NonEmpty BlockContext)
blockContexts [RawCmmDecl]
decls
([(CmmTickScope, NonEmpty BlockContext)]
topScopes, [(CmmTickScope, (CmmTickScope, NonEmpty BlockContext),
NonEmpty BlockContext)]
childScopes)
= [Either
(CmmTickScope, NonEmpty BlockContext)
(CmmTickScope, (CmmTickScope, NonEmpty BlockContext),
NonEmpty BlockContext)]
-> ([(CmmTickScope, NonEmpty BlockContext)],
[(CmmTickScope, (CmmTickScope, NonEmpty BlockContext),
NonEmpty BlockContext)])
forall a b. [Either a b] -> ([a], [b])
partitionEithers ([Either
(CmmTickScope, NonEmpty BlockContext)
(CmmTickScope, (CmmTickScope, NonEmpty BlockContext),
NonEmpty BlockContext)]
-> ([(CmmTickScope, NonEmpty BlockContext)],
[(CmmTickScope, (CmmTickScope, NonEmpty BlockContext),
NonEmpty BlockContext)]))
-> [Either
(CmmTickScope, NonEmpty BlockContext)
(CmmTickScope, (CmmTickScope, NonEmpty BlockContext),
NonEmpty BlockContext)]
-> ([(CmmTickScope, NonEmpty BlockContext)],
[(CmmTickScope, (CmmTickScope, NonEmpty BlockContext),
NonEmpty BlockContext)])
forall a b. (a -> b) -> a -> b
$ ((CmmTickScope, NonEmpty BlockContext)
-> Either
(CmmTickScope, NonEmpty BlockContext)
(CmmTickScope, (CmmTickScope, NonEmpty BlockContext),
NonEmpty BlockContext))
-> [(CmmTickScope, NonEmpty BlockContext)]
-> [Either
(CmmTickScope, NonEmpty BlockContext)
(CmmTickScope, (CmmTickScope, NonEmpty BlockContext),
NonEmpty BlockContext)]
forall a b. (a -> b) -> [a] -> [b]
map (\(CmmTickScope
k, NonEmpty BlockContext
a) -> (CmmTickScope, NonEmpty BlockContext)
-> CmmTickScope
-> Either
(CmmTickScope, NonEmpty BlockContext)
(CmmTickScope, (CmmTickScope, NonEmpty BlockContext),
NonEmpty BlockContext)
forall {t}.
t
-> CmmTickScope
-> Either t (CmmTickScope, t, NonEmpty BlockContext)
findP (CmmTickScope
k, NonEmpty BlockContext
a) CmmTickScope
k) ([(CmmTickScope, NonEmpty BlockContext)]
-> [Either
(CmmTickScope, NonEmpty BlockContext)
(CmmTickScope, (CmmTickScope, NonEmpty BlockContext),
NonEmpty BlockContext)])
-> [(CmmTickScope, NonEmpty BlockContext)]
-> [Either
(CmmTickScope, NonEmpty BlockContext)
(CmmTickScope, (CmmTickScope, NonEmpty BlockContext),
NonEmpty BlockContext)]
forall a b. (a -> b) -> a -> b
$ Map CmmTickScope (NonEmpty BlockContext)
-> [(CmmTickScope, NonEmpty BlockContext)]
forall k a. Map k a -> [(k, a)]
Map.toList Map CmmTickScope (NonEmpty BlockContext)
blockCtxs
findP :: t
-> CmmTickScope
-> Either t (CmmTickScope, t, NonEmpty BlockContext)
findP t
tsc CmmTickScope
GlobalScope = t -> Either t (CmmTickScope, t, NonEmpty BlockContext)
forall a b. a -> Either a b
Left t
tsc
findP t
tsc CmmTickScope
scp | Just NonEmpty BlockContext
x <- CmmTickScope
-> Map CmmTickScope (NonEmpty BlockContext)
-> Maybe (NonEmpty BlockContext)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup CmmTickScope
scp' Map CmmTickScope (NonEmpty BlockContext)
blockCtxs = (CmmTickScope, t, NonEmpty BlockContext)
-> Either t (CmmTickScope, t, NonEmpty BlockContext)
forall a b. b -> Either a b
Right (CmmTickScope
scp', t
tsc, NonEmpty BlockContext
x)
| Bool
otherwise = t
-> CmmTickScope
-> Either t (CmmTickScope, t, NonEmpty BlockContext)
findP t
tsc CmmTickScope
scp'
where
scp' :: CmmTickScope
scp' | SubScope Unique
_ CmmTickScope
scp' <- CmmTickScope
scp = CmmTickScope
scp'
| CombinedScope CmmTickScope
scp' CmmTickScope
_ <- CmmTickScope
scp = CmmTickScope
scp'
scopeMap :: Map CmmTickScope [(CmmTickScope, NonEmpty BlockContext)]
scopeMap = (Map CmmTickScope [(CmmTickScope, NonEmpty BlockContext)]
-> (CmmTickScope, (CmmTickScope, NonEmpty BlockContext),
NonEmpty BlockContext)
-> Map CmmTickScope [(CmmTickScope, NonEmpty BlockContext)])
-> Map CmmTickScope [(CmmTickScope, NonEmpty BlockContext)]
-> [(CmmTickScope, (CmmTickScope, NonEmpty BlockContext),
NonEmpty BlockContext)]
-> Map CmmTickScope [(CmmTickScope, NonEmpty BlockContext)]
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (\ Map CmmTickScope [(CmmTickScope, NonEmpty BlockContext)]
acc (CmmTickScope
k, (CmmTickScope
k', NonEmpty BlockContext
a'), NonEmpty BlockContext
_) -> CmmTickScope
-> (CmmTickScope, NonEmpty BlockContext)
-> Map CmmTickScope [(CmmTickScope, NonEmpty BlockContext)]
-> Map CmmTickScope [(CmmTickScope, NonEmpty BlockContext)]
forall k a. Ord k => k -> a -> Map k [a] -> Map k [a]
insertMulti CmmTickScope
k (CmmTickScope
k', NonEmpty BlockContext
a') Map CmmTickScope [(CmmTickScope, NonEmpty BlockContext)]
acc) Map CmmTickScope [(CmmTickScope, NonEmpty BlockContext)]
forall k a. Map k a
Map.empty [(CmmTickScope, (CmmTickScope, NonEmpty BlockContext),
NonEmpty BlockContext)]
childScopes
ticksToCopy :: CmmTickScope -> [CmmTickish]
ticksToCopy :: CmmTickScope -> [GenTickish 'TickishPassCmm]
ticksToCopy (CombinedScope CmmTickScope
scp CmmTickScope
s) = CmmTickScope -> [GenTickish 'TickishPassCmm]
go CmmTickScope
s
where go :: CmmTickScope -> [GenTickish 'TickishPassCmm]
go CmmTickScope
s | CmmTickScope
scp CmmTickScope -> CmmTickScope -> Bool
`isTickSubScope` CmmTickScope
s = []
| SubScope Unique
_ CmmTickScope
s' <- CmmTickScope
s = [GenTickish 'TickishPassCmm]
ticks [GenTickish 'TickishPassCmm]
-> [GenTickish 'TickishPassCmm] -> [GenTickish 'TickishPassCmm]
forall a. [a] -> [a] -> [a]
++ CmmTickScope -> [GenTickish 'TickishPassCmm]
go CmmTickScope
s'
| CombinedScope CmmTickScope
s1 CmmTickScope
s2 <- CmmTickScope
s = [GenTickish 'TickishPassCmm]
ticks [GenTickish 'TickishPassCmm]
-> [GenTickish 'TickishPassCmm] -> [GenTickish 'TickishPassCmm]
forall a. [a] -> [a] -> [a]
++ CmmTickScope -> [GenTickish 'TickishPassCmm]
go CmmTickScope
s1 [GenTickish 'TickishPassCmm]
-> [GenTickish 'TickishPassCmm] -> [GenTickish 'TickishPassCmm]
forall a. [a] -> [a] -> [a]
++ CmmTickScope -> [GenTickish 'TickishPassCmm]
go CmmTickScope
s2
| Bool
otherwise = String -> [GenTickish 'TickishPassCmm]
forall a. HasCallStack => String -> a
panic String
"ticksToCopy impossible"
where ticks :: [GenTickish 'TickishPassCmm]
ticks = [BlockContext] -> [GenTickish 'TickishPassCmm]
forall {b}.
[(Block CmmNode C C, b)] -> [GenTickish 'TickishPassCmm]
bCtxsTicks ([BlockContext] -> [GenTickish 'TickishPassCmm])
-> [BlockContext] -> [GenTickish 'TickishPassCmm]
forall a b. (a -> b) -> a -> b
$ [BlockContext]
-> (NonEmpty BlockContext -> [BlockContext])
-> Maybe (NonEmpty BlockContext)
-> [BlockContext]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] NonEmpty BlockContext -> [BlockContext]
forall a. NonEmpty a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (Maybe (NonEmpty BlockContext) -> [BlockContext])
-> Maybe (NonEmpty BlockContext) -> [BlockContext]
forall a b. (a -> b) -> a -> b
$ CmmTickScope
-> Map CmmTickScope (NonEmpty BlockContext)
-> Maybe (NonEmpty BlockContext)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup CmmTickScope
s Map CmmTickScope (NonEmpty BlockContext)
blockCtxs
ticksToCopy CmmTickScope
_ = []
bCtxsTicks :: [(Block CmmNode C C, b)] -> [GenTickish 'TickishPassCmm]
bCtxsTicks = ((Block CmmNode C C, b) -> [GenTickish 'TickishPassCmm])
-> [(Block CmmNode C C, b)] -> [GenTickish 'TickishPassCmm]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap (Block CmmNode C C -> [GenTickish 'TickishPassCmm]
blockTicks (Block CmmNode C C -> [GenTickish 'TickishPassCmm])
-> ((Block CmmNode C C, b) -> Block CmmNode C C)
-> (Block CmmNode C C, b)
-> [GenTickish 'TickishPassCmm]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Block CmmNode C C, b) -> Block CmmNode C C
forall a b. (a, b) -> a
fst)
bestSrcTick :: NonEmpty (RealSrcSpan, LexicalFastString)
-> (RealSrcSpan, LexicalFastString)
bestSrcTick = ((RealSrcSpan, LexicalFastString)
-> (RealSrcSpan, LexicalFastString) -> Ordering)
-> NonEmpty (RealSrcSpan, LexicalFastString)
-> (RealSrcSpan, LexicalFastString)
forall (t :: * -> *) a.
Foldable t =>
(a -> a -> Ordering) -> t a -> a
minimumBy (((RealSrcSpan, LexicalFastString) -> Int)
-> (RealSrcSpan, LexicalFastString)
-> (RealSrcSpan, LexicalFastString)
-> Ordering
forall a b. Ord a => (b -> a) -> b -> b -> Ordering
comparing (RealSrcSpan, LexicalFastString) -> Int
rangeRating)
rangeRating :: (RealSrcSpan, LexicalFastString) -> Int
rangeRating (RealSrcSpan
span, LexicalFastString
_)
| RealSrcSpan -> FastString
srcSpanFile RealSrcSpan
span FastString -> FastString -> Bool
forall a. Eq a => a -> a -> Bool
== FastString
thisFile = Int
1
| Bool
otherwise = Int
2 :: Int
thisFile :: FastString
thisFile = FastString -> (String -> FastString) -> Maybe String -> FastString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe FastString
nilFS String -> FastString
mkFastString (Maybe String -> FastString) -> Maybe String -> FastString
forall a b. (a -> b) -> a -> b
$ ModLocation -> Maybe String
ml_hs_file ModLocation
modLoc
blocksForScope :: Maybe (RealSrcSpan, LexicalFastString) -> (CmmTickScope, NonEmpty BlockContext) -> DebugBlock
blocksForScope :: Maybe (RealSrcSpan, LexicalFastString)
-> (CmmTickScope, NonEmpty BlockContext) -> DebugBlock
blocksForScope Maybe (RealSrcSpan, LexicalFastString)
cstick (CmmTickScope
scope, BlockContext
bctx:|[BlockContext]
bctxs) = Bool -> BlockContext -> DebugBlock
mkBlock Bool
True BlockContext
bctx
where nested :: [(CmmTickScope, NonEmpty BlockContext)]
nested = [(CmmTickScope, NonEmpty BlockContext)]
-> Maybe [(CmmTickScope, NonEmpty BlockContext)]
-> [(CmmTickScope, NonEmpty BlockContext)]
forall a. a -> Maybe a -> a
fromMaybe [] (Maybe [(CmmTickScope, NonEmpty BlockContext)]
-> [(CmmTickScope, NonEmpty BlockContext)])
-> Maybe [(CmmTickScope, NonEmpty BlockContext)]
-> [(CmmTickScope, NonEmpty BlockContext)]
forall a b. (a -> b) -> a -> b
$ CmmTickScope
-> Map CmmTickScope [(CmmTickScope, NonEmpty BlockContext)]
-> Maybe [(CmmTickScope, NonEmpty BlockContext)]
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup CmmTickScope
scope Map CmmTickScope [(CmmTickScope, NonEmpty BlockContext)]
scopeMap
childs :: [DebugBlock]
childs = (BlockContext -> DebugBlock) -> [BlockContext] -> [DebugBlock]
forall a b. (a -> b) -> [a] -> [b]
map (Bool -> BlockContext -> DebugBlock
mkBlock Bool
False) [BlockContext]
bctxs [DebugBlock] -> [DebugBlock] -> [DebugBlock]
forall a. [a] -> [a] -> [a]
++
((CmmTickScope, NonEmpty BlockContext) -> DebugBlock)
-> [(CmmTickScope, NonEmpty BlockContext)] -> [DebugBlock]
forall a b. (a -> b) -> [a] -> [b]
map (Maybe (RealSrcSpan, LexicalFastString)
-> (CmmTickScope, NonEmpty BlockContext) -> DebugBlock
blocksForScope Maybe (RealSrcSpan, LexicalFastString)
stick) [(CmmTickScope, NonEmpty BlockContext)]
nested
mkBlock :: Bool -> BlockContext -> DebugBlock
mkBlock :: Bool -> BlockContext -> DebugBlock
mkBlock Bool
top (Block CmmNode C C
block, RawCmmDeclNoStatics
prc)
= DebugBlock { dblProcedure :: Label
dblProcedure = CmmGraph -> Label
forall (s :: * -> *) (n :: Extensibility -> Extensibility -> *).
GenGenCmmGraph s n -> Label
g_entry CmmGraph
graph
, dblLabel :: Label
dblLabel = Label
label
, dblCLabel :: CLabel
dblCLabel = Label -> CLabel
blockLbl Label
label
, dblHasInfoTbl :: Bool
dblHasInfoTbl = Maybe RawCmmStatics -> Bool
forall a. Maybe a -> Bool
isJust Maybe RawCmmStatics
info
, dblParent :: Maybe DebugBlock
dblParent = Maybe DebugBlock
forall a. Maybe a
Nothing
, dblTicks :: [GenTickish 'TickishPassCmm]
dblTicks = [GenTickish 'TickishPassCmm]
ticks
, dblPosition :: Maybe Int
dblPosition = Maybe Int
forall a. Maybe a
Nothing
, dblSourceTick :: Maybe (GenTickish 'TickishPassCmm)
dblSourceTick = (RealSrcSpan -> LexicalFastString -> GenTickish 'TickishPassCmm)
-> (RealSrcSpan, LexicalFastString) -> GenTickish 'TickishPassCmm
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry RealSrcSpan -> LexicalFastString -> GenTickish 'TickishPassCmm
forall (pass :: TickishPass).
RealSrcSpan -> LexicalFastString -> GenTickish pass
SourceNote ((RealSrcSpan, LexicalFastString) -> GenTickish 'TickishPassCmm)
-> Maybe (RealSrcSpan, LexicalFastString)
-> Maybe (GenTickish 'TickishPassCmm)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe (RealSrcSpan, LexicalFastString)
stick
, dblBlocks :: [DebugBlock]
dblBlocks = [DebugBlock]
blocks
, dblUnwind :: [UnwindPoint]
dblUnwind = []
}
where (LabelMap RawCmmStatics
infos, CmmGraph
graph) = case RawCmmDeclNoStatics
prc of
CmmProc LabelMap RawCmmStatics
infos CLabel
_ [GlobalRegUse]
_ CmmGraph
graph -> (LabelMap RawCmmStatics
infos, CmmGraph
graph)
CmmData Section
_ Void
v -> case Void
v of
label :: Label
label = Block CmmNode C C -> Label
forall (x :: Extensibility). Block CmmNode C x -> Label
forall (thing :: Extensibility -> Extensibility -> *)
(x :: Extensibility).
NonLocal thing =>
thing C x -> Label
entryLabel Block CmmNode C C
block
info :: Maybe RawCmmStatics
info = Label -> LabelMap RawCmmStatics -> Maybe RawCmmStatics
forall a. Label -> LabelMap a -> Maybe a
mapLookup Label
label LabelMap RawCmmStatics
infos
blocks :: [DebugBlock]
blocks | Bool
top = [DebugBlock] -> [DebugBlock] -> [DebugBlock]
forall a b. [a] -> b -> b
seqList [DebugBlock]
childs [DebugBlock]
childs
| Bool
otherwise = []
isSourceTick :: GenTickish pass -> Maybe (RealSrcSpan, LexicalFastString)
isSourceTick (SourceNote RealSrcSpan
span LexicalFastString
a) = (RealSrcSpan, LexicalFastString)
-> Maybe (RealSrcSpan, LexicalFastString)
forall a. a -> Maybe a
Just (RealSrcSpan
span, LexicalFastString
a)
isSourceTick GenTickish pass
_ = Maybe (RealSrcSpan, LexicalFastString)
forall a. Maybe a
Nothing
ticks :: [GenTickish 'TickishPassCmm]
ticks = (GenTickish 'TickishPassCmm -> GenTickish 'TickishPassCmm -> Bool)
-> [GenTickish 'TickishPassCmm] -> [GenTickish 'TickishPassCmm]
forall a. (a -> a -> Bool) -> [a] -> [a]
nubBy ((GenTickish 'TickishPassCmm -> GenTickish 'TickishPassCmm -> Bool)
-> GenTickish 'TickishPassCmm -> GenTickish 'TickishPassCmm -> Bool
forall a b c. (a -> b -> c) -> b -> a -> c
flip GenTickish 'TickishPassCmm -> GenTickish 'TickishPassCmm -> Bool
forall (pass :: TickishPass).
Eq (GenTickish pass) =>
GenTickish pass -> GenTickish pass -> Bool
tickishContains) ([GenTickish 'TickishPassCmm] -> [GenTickish 'TickishPassCmm])
-> [GenTickish 'TickishPassCmm] -> [GenTickish 'TickishPassCmm]
forall a b. (a -> b) -> a -> b
$
[BlockContext] -> [GenTickish 'TickishPassCmm]
forall {b}.
[(Block CmmNode C C, b)] -> [GenTickish 'TickishPassCmm]
bCtxsTicks [BlockContext]
bctxs [GenTickish 'TickishPassCmm]
-> [GenTickish 'TickishPassCmm] -> [GenTickish 'TickishPassCmm]
forall a. [a] -> [a] -> [a]
++ CmmTickScope -> [GenTickish 'TickishPassCmm]
ticksToCopy CmmTickScope
scope
stick :: Maybe (RealSrcSpan, LexicalFastString)
stick = case [(RealSrcSpan, LexicalFastString)]
-> Maybe (NonEmpty (RealSrcSpan, LexicalFastString))
forall a. [a] -> Maybe (NonEmpty a)
nonEmpty ([(RealSrcSpan, LexicalFastString)]
-> Maybe (NonEmpty (RealSrcSpan, LexicalFastString)))
-> [(RealSrcSpan, LexicalFastString)]
-> Maybe (NonEmpty (RealSrcSpan, LexicalFastString))
forall a b. (a -> b) -> a -> b
$ (GenTickish 'TickishPassCmm
-> Maybe (RealSrcSpan, LexicalFastString))
-> [GenTickish 'TickishPassCmm]
-> [(RealSrcSpan, LexicalFastString)]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe GenTickish 'TickishPassCmm
-> Maybe (RealSrcSpan, LexicalFastString)
forall {pass :: TickishPass}.
GenTickish pass -> Maybe (RealSrcSpan, LexicalFastString)
isSourceTick [GenTickish 'TickishPassCmm]
ticks of
Maybe (NonEmpty (RealSrcSpan, LexicalFastString))
Nothing -> Maybe (RealSrcSpan, LexicalFastString)
cstick
Just NonEmpty (RealSrcSpan, LexicalFastString)
sticks -> (RealSrcSpan, LexicalFastString)
-> Maybe (RealSrcSpan, LexicalFastString)
forall a. a -> Maybe a
Just ((RealSrcSpan, LexicalFastString)
-> Maybe (RealSrcSpan, LexicalFastString))
-> (RealSrcSpan, LexicalFastString)
-> Maybe (RealSrcSpan, LexicalFastString)
forall a b. (a -> b) -> a -> b
$! NonEmpty (RealSrcSpan, LexicalFastString)
-> (RealSrcSpan, LexicalFastString)
bestSrcTick (NonEmpty (RealSrcSpan, LexicalFastString)
sticks NonEmpty (RealSrcSpan, LexicalFastString)
-> [(RealSrcSpan, LexicalFastString)]
-> NonEmpty (RealSrcSpan, LexicalFastString)
forall a. NonEmpty a -> [a] -> NonEmpty a
`NE.appendList` Maybe (RealSrcSpan, LexicalFastString)
-> [(RealSrcSpan, LexicalFastString)]
forall a. Maybe a -> [a]
maybeToList Maybe (RealSrcSpan, LexicalFastString)
cstick)
blockContexts :: [GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph] -> Map.Map CmmTickScope (NonEmpty BlockContext)
blockContexts :: forall a.
[GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph]
-> Map CmmTickScope (NonEmpty BlockContext)
blockContexts = (NonEmpty BlockContext -> NonEmpty BlockContext)
-> Map CmmTickScope (NonEmpty BlockContext)
-> Map CmmTickScope (NonEmpty BlockContext)
forall a b k. (a -> b) -> Map k a -> Map k b
Map.map NonEmpty BlockContext -> NonEmpty BlockContext
forall a. NonEmpty a -> NonEmpty a
NE.reverse (Map CmmTickScope (NonEmpty BlockContext)
-> Map CmmTickScope (NonEmpty BlockContext))
-> ([GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph]
-> Map CmmTickScope (NonEmpty BlockContext))
-> [GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph]
-> Map CmmTickScope (NonEmpty BlockContext)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
-> Map CmmTickScope (NonEmpty BlockContext)
-> Map CmmTickScope (NonEmpty BlockContext))
-> Map CmmTickScope (NonEmpty BlockContext)
-> [GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph]
-> Map CmmTickScope (NonEmpty BlockContext)
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
-> Map CmmTickScope (NonEmpty BlockContext)
-> Map CmmTickScope (NonEmpty BlockContext)
forall a.
GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
-> Map CmmTickScope (NonEmpty BlockContext)
-> Map CmmTickScope (NonEmpty BlockContext)
walkProc Map CmmTickScope (NonEmpty BlockContext)
forall k a. Map k a
Map.empty
where walkProc :: GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
-> Map.Map CmmTickScope (NonEmpty BlockContext)
-> Map.Map CmmTickScope (NonEmpty BlockContext)
walkProc :: forall a.
GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
-> Map CmmTickScope (NonEmpty BlockContext)
-> Map CmmTickScope (NonEmpty BlockContext)
walkProc CmmData{} Map CmmTickScope (NonEmpty BlockContext)
m = Map CmmTickScope (NonEmpty BlockContext)
m
walkProc prc :: GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
prc@(CmmProc LabelMap RawCmmStatics
_ CLabel
_ [GlobalRegUse]
_ CmmGraph
graph) Map CmmTickScope (NonEmpty BlockContext)
m
| LabelMap (Block CmmNode C C) -> Bool
forall a. LabelMap a -> Bool
mapNull LabelMap (Block CmmNode C C)
blocks = Map CmmTickScope (NonEmpty BlockContext)
m
| Bool
otherwise = (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
-> Map CmmTickScope (NonEmpty BlockContext)
forall a b. (a, b) -> b
snd ((LabelSet, Map CmmTickScope (NonEmpty BlockContext))
-> Map CmmTickScope (NonEmpty BlockContext))
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
-> Map CmmTickScope (NonEmpty BlockContext)
forall a b. (a -> b) -> a -> b
$ GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
-> [Block CmmNode C C]
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
forall a.
GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
-> [Block CmmNode C C]
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
walkBlock GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
prc [Block CmmNode C C]
entry (LabelSet
emptyLbls, Map CmmTickScope (NonEmpty BlockContext)
m)
where blocks :: LabelMap (Block CmmNode C C)
blocks = CmmGraph -> LabelMap (Block CmmNode C C)
toBlockMap CmmGraph
graph
entry :: [Block CmmNode C C]
entry = [Label -> LabelMap (Block CmmNode C C) -> Block CmmNode C C
forall {a}. Label -> LabelMap a -> a
mapFind (CmmGraph -> Label
forall (s :: * -> *) (n :: Extensibility -> Extensibility -> *).
GenGenCmmGraph s n -> Label
g_entry CmmGraph
graph) LabelMap (Block CmmNode C C)
blocks]
emptyLbls :: LabelSet
emptyLbls = LabelSet
setEmpty :: LabelSet
walkBlock :: GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph -> [Block CmmNode C C]
-> (LabelSet, Map.Map CmmTickScope (NonEmpty BlockContext))
-> (LabelSet, Map.Map CmmTickScope (NonEmpty BlockContext))
walkBlock :: forall a.
GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
-> [Block CmmNode C C]
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
walkBlock GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
_ [] (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
c = (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
c
walkBlock GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
prc (Block CmmNode C C
block:[Block CmmNode C C]
blocks) (LabelSet
visited, Map CmmTickScope (NonEmpty BlockContext)
m) = case (GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
prc, Label -> LabelSet -> Bool
setMember Label
lbl LabelSet
visited) of
(CmmProc LabelMap RawCmmStatics
x CLabel
y [GlobalRegUse]
z CmmGraph
graph, Bool
False) ->
let succs :: [Block CmmNode C C]
succs = (Label -> LabelMap (Block CmmNode C C) -> Block CmmNode C C)
-> LabelMap (Block CmmNode C C) -> Label -> Block CmmNode C C
forall a b c. (a -> b -> c) -> b -> a -> c
flip Label -> LabelMap (Block CmmNode C C) -> Block CmmNode C C
forall {a}. Label -> LabelMap a -> a
mapFind (CmmGraph -> LabelMap (Block CmmNode C C)
toBlockMap CmmGraph
graph) (Label -> Block CmmNode C C) -> [Label] -> [Block CmmNode C C]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
CmmNode O C -> [Label]
forall (e :: Extensibility). CmmNode e C -> [Label]
forall (thing :: Extensibility -> Extensibility -> *)
(e :: Extensibility).
NonLocal thing =>
thing e C -> [Label]
successors (Block CmmNode C C -> CmmNode O C
forall (n :: Extensibility -> Extensibility -> *)
(x :: Extensibility).
Block n x C -> n O C
lastNode Block CmmNode C C
block) in
GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
-> [Block CmmNode C C]
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
forall a.
GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
-> [Block CmmNode C C]
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
walkBlock GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
prc [Block CmmNode C C]
blocks ((LabelSet, Map CmmTickScope (NonEmpty BlockContext))
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext)))
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
forall a b. (a -> b) -> a -> b
$
GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
-> [Block CmmNode C C]
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
forall a.
GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
-> [Block CmmNode C C]
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
walkBlock GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
prc [Block CmmNode C C]
succs
( Label
lbl Label -> LabelSet -> LabelSet
`setInsert` LabelSet
visited
, CmmTickScope
-> BlockContext
-> Map CmmTickScope (NonEmpty BlockContext)
-> Map CmmTickScope (NonEmpty BlockContext)
forall k a.
Ord k =>
k -> a -> Map k (NonEmpty a) -> Map k (NonEmpty a)
insertMultiNE CmmTickScope
scope (Block CmmNode C C
block, LabelMap RawCmmStatics
-> CLabel -> [GlobalRegUse] -> CmmGraph -> RawCmmDeclNoStatics
forall d h g.
h -> CLabel -> [GlobalRegUse] -> g -> GenCmmDecl d h g
CmmProc LabelMap RawCmmStatics
x CLabel
y [GlobalRegUse]
z CmmGraph
graph) Map CmmTickScope (NonEmpty BlockContext)
m )
(GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph, Bool)
_ -> GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
-> [Block CmmNode C C]
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
forall a.
GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
-> [Block CmmNode C C]
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
-> (LabelSet, Map CmmTickScope (NonEmpty BlockContext))
walkBlock GenCmmDecl a (LabelMap RawCmmStatics) CmmGraph
prc [Block CmmNode C C]
blocks (LabelSet
visited, Map CmmTickScope (NonEmpty BlockContext)
m)
where CmmEntry Label
lbl CmmTickScope
scope = Block CmmNode C C -> CmmNode C O
forall (n :: Extensibility -> Extensibility -> *)
(x :: Extensibility).
Block n C x -> n C O
firstNode Block CmmNode C C
block
mapFind :: Label -> LabelMap a -> a
mapFind = a -> Label -> LabelMap a -> a
forall a. a -> Label -> LabelMap a -> a
mapFindWithDefault (String -> a
forall a. HasCallStack => String -> a
error String
"contextTree: block not found!")
insertMulti :: Ord k => k -> a -> Map.Map k [a] -> Map.Map k [a]
insertMulti :: forall k a. Ord k => k -> a -> Map k [a] -> Map k [a]
insertMulti k
k a
v = ([a] -> [a] -> [a]) -> k -> [a] -> Map k [a] -> Map k [a]
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
Map.insertWith (([a] -> [a]) -> [a] -> [a] -> [a]
forall a b. a -> b -> a
const (a
va -> [a] -> [a]
forall a. a -> [a] -> [a]
:)) k
k [a
v]
insertMultiNE :: Ord k => k -> a -> Map.Map k (NonEmpty a) -> Map.Map k (NonEmpty a)
insertMultiNE :: forall k a.
Ord k =>
k -> a -> Map k (NonEmpty a) -> Map k (NonEmpty a)
insertMultiNE k
k a
v = (NonEmpty a -> NonEmpty a -> NonEmpty a)
-> k -> NonEmpty a -> Map k (NonEmpty a) -> Map k (NonEmpty a)
forall k a. Ord k => (a -> a -> a) -> k -> a -> Map k a -> Map k a
Map.insertWith ((NonEmpty a -> NonEmpty a)
-> NonEmpty a -> NonEmpty a -> NonEmpty a
forall a b. a -> b -> a
const (a
v a -> NonEmpty a -> NonEmpty a
forall a. a -> NonEmpty a -> NonEmpty a
NE.<|)) k
k (a -> NonEmpty a
forall a. a -> NonEmpty a
NE.singleton a
v)
cmmDebugLabels :: (BlockId -> Bool) -> (i -> Bool) -> GenCmmGroup d g (ListGraph i) -> [Label]
cmmDebugLabels :: forall i d g.
(Label -> Bool)
-> (i -> Bool) -> GenCmmGroup d g (ListGraph i) -> [Label]
cmmDebugLabels Label -> Bool
is_valid_label i -> Bool
isMeta GenCmmGroup d g (ListGraph i)
nats = [Label] -> [Label] -> [Label]
forall a b. [a] -> b -> b
seqList [Label]
lbls [Label]
lbls
where
lbls :: [Label]
lbls = (Label -> Bool) -> [Label] -> [Label]
forall a. (a -> Bool) -> [a] -> [a]
filter Label -> Bool
is_valid_label ([Label] -> [Label]) -> [Label] -> [Label]
forall a b. (a -> b) -> a -> b
$ (GenBasicBlock i -> Label) -> [GenBasicBlock i] -> [Label]
forall a b. (a -> b) -> [a] -> [b]
map GenBasicBlock i -> Label
forall i. GenBasicBlock i -> Label
blockId ([GenBasicBlock i] -> [Label]) -> [GenBasicBlock i] -> [Label]
forall a b. (a -> b) -> a -> b
$ (GenBasicBlock i -> Bool) -> [GenBasicBlock i] -> [GenBasicBlock i]
forall a. (a -> Bool) -> [a] -> [a]
filter (Bool -> Bool
not (Bool -> Bool)
-> (GenBasicBlock i -> Bool) -> GenBasicBlock i -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GenBasicBlock i -> Bool
allMeta) ([GenBasicBlock i] -> [GenBasicBlock i])
-> [GenBasicBlock i] -> [GenBasicBlock i]
forall a b. (a -> b) -> a -> b
$ (GenCmmDecl d g (ListGraph i) -> [GenBasicBlock i])
-> GenCmmGroup d g (ListGraph i) -> [GenBasicBlock i]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap GenCmmDecl d g (ListGraph i) -> [GenBasicBlock i]
forall {d} {h} {i}.
GenCmmDecl d h (ListGraph i) -> [GenBasicBlock i]
getBlocks GenCmmGroup d g (ListGraph i)
nats
getBlocks :: GenCmmDecl d h (ListGraph i) -> [GenBasicBlock i]
getBlocks (CmmProc h
_ CLabel
_ [GlobalRegUse]
_ (ListGraph [GenBasicBlock i]
bs)) = [GenBasicBlock i]
bs
getBlocks GenCmmDecl d h (ListGraph i)
_other = []
allMeta :: GenBasicBlock i -> Bool
allMeta (BasicBlock Label
_ [i]
instrs) = (i -> Bool) -> [i] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all i -> Bool
isMeta [i]
instrs
cmmDebugLink :: [Label] -> LabelMap [UnwindPoint]
-> [DebugBlock] -> [DebugBlock]
cmmDebugLink :: [Label] -> LabelMap [UnwindPoint] -> [DebugBlock] -> [DebugBlock]
cmmDebugLink [Label]
labels LabelMap [UnwindPoint]
unwindPts [DebugBlock]
blocks = (DebugBlock -> Maybe DebugBlock) -> [DebugBlock] -> [DebugBlock]
forall a b. (a -> Maybe b) -> [a] -> [b]
mapMaybe DebugBlock -> Maybe DebugBlock
link [DebugBlock]
blocks
where blockPos :: LabelMap Int
blockPos :: LabelMap Int
blockPos = [(Label, Int)] -> LabelMap Int
forall v. [(Label, v)] -> LabelMap v
mapFromList ([(Label, Int)] -> LabelMap Int) -> [(Label, Int)] -> LabelMap Int
forall a b. (a -> b) -> a -> b
$ ([Label] -> [Int] -> [(Label, Int)])
-> [Int] -> [Label] -> [(Label, Int)]
forall a b c. (a -> b -> c) -> b -> a -> c
flip [Label] -> [Int] -> [(Label, Int)]
forall a b. [a] -> [b] -> [(a, b)]
zip [Int
0..] [Label]
labels
link :: DebugBlock -> Maybe DebugBlock
link DebugBlock
block = case Label -> LabelMap Int -> Maybe Int
forall a. Label -> LabelMap a -> Maybe a
mapLookup (DebugBlock -> Label
dblLabel DebugBlock
block) LabelMap Int
blockPos of
Maybe Int
Nothing -> Maybe DebugBlock
forall a. Maybe a
Nothing
Maybe Int
pos -> DebugBlock -> Maybe DebugBlock
forall a. a -> Maybe a
Just (DebugBlock -> Maybe DebugBlock) -> DebugBlock -> Maybe DebugBlock
forall a b. (a -> b) -> a -> b
$ DebugBlock
block
{ dblPosition = pos
, dblBlocks = mapMaybe link (dblBlocks block)
, dblUnwind = fromMaybe mempty $ mapLookup (dblLabel block) unwindPts
}
debugToMap :: [DebugBlock] -> LabelMap DebugBlock
debugToMap :: [DebugBlock] -> LabelMap DebugBlock
debugToMap = [LabelMap DebugBlock] -> LabelMap DebugBlock
forall a. [LabelMap a] -> LabelMap a
mapUnions ([LabelMap DebugBlock] -> LabelMap DebugBlock)
-> ([DebugBlock] -> [LabelMap DebugBlock])
-> [DebugBlock]
-> LabelMap DebugBlock
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (DebugBlock -> LabelMap DebugBlock)
-> [DebugBlock] -> [LabelMap DebugBlock]
forall a b. (a -> b) -> [a] -> [b]
map DebugBlock -> LabelMap DebugBlock
go
where go :: DebugBlock -> LabelMap DebugBlock
go DebugBlock
b = Label -> DebugBlock -> LabelMap DebugBlock -> LabelMap DebugBlock
forall v. Label -> v -> LabelMap v -> LabelMap v
mapInsert (DebugBlock -> Label
dblLabel DebugBlock
b) DebugBlock
b (LabelMap DebugBlock -> LabelMap DebugBlock)
-> LabelMap DebugBlock -> LabelMap DebugBlock
forall a b. (a -> b) -> a -> b
$ [LabelMap DebugBlock] -> LabelMap DebugBlock
forall a. [LabelMap a] -> LabelMap a
mapUnions ([LabelMap DebugBlock] -> LabelMap DebugBlock)
-> [LabelMap DebugBlock] -> LabelMap DebugBlock
forall a b. (a -> b) -> a -> b
$ (DebugBlock -> LabelMap DebugBlock)
-> [DebugBlock] -> [LabelMap DebugBlock]
forall a b. (a -> b) -> [a] -> [b]
map DebugBlock -> LabelMap DebugBlock
go (DebugBlock -> [DebugBlock]
dblBlocks DebugBlock
b)
data UnwindPoint = UnwindPoint !CLabel !UnwindTable
instance OutputableP Platform UnwindPoint where
pdoc :: Platform -> UnwindPoint -> SDoc
pdoc Platform
env (UnwindPoint CLabel
lbl UnwindTable
uws) =
SDoc -> SDoc
forall doc. IsLine doc => doc -> doc
braces (SDoc -> SDoc) -> SDoc -> SDoc
forall a b. (a -> b) -> a -> b
$ Platform -> CLabel -> SDoc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
env CLabel
lbl SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> SDoc
forall doc. IsLine doc => doc
colon
SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<+> [SDoc] -> SDoc
forall doc. IsLine doc => [doc] -> doc
hsep (SDoc -> [SDoc] -> [SDoc]
forall doc. IsLine doc => doc -> [doc] -> [doc]
punctuate SDoc
forall doc. IsLine doc => doc
comma ([SDoc] -> [SDoc]) -> [SDoc] -> [SDoc]
forall a b. (a -> b) -> a -> b
$ ((GlobalReg, Maybe UnwindExpr) -> SDoc)
-> [(GlobalReg, Maybe UnwindExpr)] -> [SDoc]
forall a b. (a -> b) -> [a] -> [b]
map (GlobalReg, Maybe UnwindExpr) -> SDoc
pprUw ([(GlobalReg, Maybe UnwindExpr)] -> [SDoc])
-> [(GlobalReg, Maybe UnwindExpr)] -> [SDoc]
forall a b. (a -> b) -> a -> b
$ UnwindTable -> [(GlobalReg, Maybe UnwindExpr)]
forall k a. Map k a -> [(k, a)]
Map.toList UnwindTable
uws)
where
pprUw :: (GlobalReg, Maybe UnwindExpr) -> SDoc
pprUw (GlobalReg
g, Maybe UnwindExpr
expr) = GlobalReg -> SDoc
forall a. Outputable a => a -> SDoc
ppr GlobalReg
g SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> Char -> SDoc
forall doc. IsLine doc => Char -> doc
char Char
'=' SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> Platform -> Maybe UnwindExpr -> SDoc
forall env a. OutputableP env a => env -> a -> SDoc
pdoc Platform
env Maybe UnwindExpr
expr
type UnwindTable = Map.Map GlobalReg (Maybe UnwindExpr)
data UnwindExpr = UwConst !Int
| UwReg !GlobalRegUse !Int
| UwDeref UnwindExpr
| UwLabel CLabel
| UwPlus UnwindExpr UnwindExpr
| UwMinus UnwindExpr UnwindExpr
| UwTimes UnwindExpr UnwindExpr
deriving (UnwindExpr -> UnwindExpr -> Bool
(UnwindExpr -> UnwindExpr -> Bool)
-> (UnwindExpr -> UnwindExpr -> Bool) -> Eq UnwindExpr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: UnwindExpr -> UnwindExpr -> Bool
== :: UnwindExpr -> UnwindExpr -> Bool
$c/= :: UnwindExpr -> UnwindExpr -> Bool
/= :: UnwindExpr -> UnwindExpr -> Bool
Eq)
instance OutputableP Platform UnwindExpr where
pdoc :: Platform -> UnwindExpr -> SDoc
pdoc = Rational -> Platform -> UnwindExpr -> SDoc
forall doc. IsLine doc => Rational -> Platform -> UnwindExpr -> doc
pprUnwindExpr Rational
0
pprUnwindTable :: IsLine doc => Platform -> UnwindTable -> doc
pprUnwindTable :: forall doc. IsLine doc => Platform -> UnwindTable -> doc
pprUnwindTable Platform
platform UnwindTable
u = doc -> doc
forall doc. IsLine doc => doc -> doc
brackets ([doc] -> doc
forall doc. IsLine doc => [doc] -> doc
fsep (doc -> [doc] -> [doc]
forall doc. IsLine doc => doc -> [doc] -> [doc]
punctuate doc
forall doc. IsLine doc => doc
comma (((GlobalReg, Maybe UnwindExpr) -> doc)
-> [(GlobalReg, Maybe UnwindExpr)] -> [doc]
forall a b. (a -> b) -> [a] -> [b]
map (GlobalReg, Maybe UnwindExpr) -> doc
print_entry (UnwindTable -> [(GlobalReg, Maybe UnwindExpr)]
forall k a. Map k a -> [(k, a)]
Map.toList UnwindTable
u))))
where print_entry :: (GlobalReg, Maybe UnwindExpr) -> doc
print_entry (GlobalReg
reg, Maybe UnwindExpr
Nothing) =
doc -> doc
forall doc. IsLine doc => doc -> doc
parens ([doc] -> doc
forall doc. IsLine doc => [doc] -> doc
sep [GlobalReg -> doc
forall doc. IsLine doc => GlobalReg -> doc
pprGlobalReg GlobalReg
reg, String -> doc
forall doc. IsLine doc => String -> doc
text String
"Nothing"])
print_entry (GlobalReg
reg, Just UnwindExpr
x) =
doc -> doc
forall doc. IsLine doc => doc -> doc
parens ([doc] -> doc
forall doc. IsLine doc => [doc] -> doc
sep [GlobalReg -> doc
forall doc. IsLine doc => GlobalReg -> doc
pprGlobalReg GlobalReg
reg, String -> doc
forall doc. IsLine doc => String -> doc
text String
"Just" doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<+> Rational -> Platform -> UnwindExpr -> doc
forall doc. IsLine doc => Rational -> Platform -> UnwindExpr -> doc
pprUnwindExpr Rational
0 Platform
platform UnwindExpr
x])
pprUnwindExpr :: IsLine doc => Rational -> Platform -> UnwindExpr -> doc
pprUnwindExpr :: forall doc. IsLine doc => Rational -> Platform -> UnwindExpr -> doc
pprUnwindExpr Rational
p Platform
env = \case
UwConst Int
i -> Int -> doc
forall doc. IsLine doc => Int -> doc
int Int
i
UwReg GlobalRegUse
g Int
0 -> GlobalRegUse -> doc
forall doc. IsLine doc => GlobalRegUse -> doc
pprGlobalRegUse GlobalRegUse
g
UwReg GlobalRegUse
g Int
x -> Rational -> Platform -> UnwindExpr -> doc
forall doc. IsLine doc => Rational -> Platform -> UnwindExpr -> doc
pprUnwindExpr Rational
p Platform
env (UnwindExpr -> UnwindExpr -> UnwindExpr
UwPlus (GlobalRegUse -> Int -> UnwindExpr
UwReg GlobalRegUse
g Int
0) (Int -> UnwindExpr
UwConst Int
x))
UwDeref UnwindExpr
e -> Char -> doc
forall doc. IsLine doc => Char -> doc
char Char
'*' doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Rational -> Platform -> UnwindExpr -> doc
forall doc. IsLine doc => Rational -> Platform -> UnwindExpr -> doc
pprUnwindExpr Rational
3 Platform
env UnwindExpr
e
UwLabel CLabel
l -> Platform -> CLabel -> doc
forall doc. IsLine doc => Platform -> CLabel -> doc
pprAsmLabel Platform
env CLabel
l
UwPlus UnwindExpr
e0 UnwindExpr
e1
| Rational
p Rational -> Rational -> Bool
forall a. Ord a => a -> a -> Bool
<= Rational
0 -> Rational -> Platform -> UnwindExpr -> doc
forall doc. IsLine doc => Rational -> Platform -> UnwindExpr -> doc
pprUnwindExpr Rational
0 Platform
env UnwindExpr
e0 doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Char -> doc
forall doc. IsLine doc => Char -> doc
char Char
'+' doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Rational -> Platform -> UnwindExpr -> doc
forall doc. IsLine doc => Rational -> Platform -> UnwindExpr -> doc
pprUnwindExpr Rational
0 Platform
env UnwindExpr
e1
UwMinus UnwindExpr
e0 UnwindExpr
e1
| Rational
p Rational -> Rational -> Bool
forall a. Ord a => a -> a -> Bool
<= Rational
0 -> Rational -> Platform -> UnwindExpr -> doc
forall doc. IsLine doc => Rational -> Platform -> UnwindExpr -> doc
pprUnwindExpr Rational
1 Platform
env UnwindExpr
e0 doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Char -> doc
forall doc. IsLine doc => Char -> doc
char Char
'-' doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Rational -> Platform -> UnwindExpr -> doc
forall doc. IsLine doc => Rational -> Platform -> UnwindExpr -> doc
pprUnwindExpr Rational
1 Platform
env UnwindExpr
e1
UwTimes UnwindExpr
e0 UnwindExpr
e1
| Rational
p Rational -> Rational -> Bool
forall a. Ord a => a -> a -> Bool
<= Rational
1 -> Rational -> Platform -> UnwindExpr -> doc
forall doc. IsLine doc => Rational -> Platform -> UnwindExpr -> doc
pprUnwindExpr Rational
2 Platform
env UnwindExpr
e0 doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Char -> doc
forall doc. IsLine doc => Char -> doc
char Char
'*' doc -> doc -> doc
forall doc. IsLine doc => doc -> doc -> doc
<> Rational -> Platform -> UnwindExpr -> doc
forall doc. IsLine doc => Rational -> Platform -> UnwindExpr -> doc
pprUnwindExpr Rational
2 Platform
env UnwindExpr
e1
UnwindExpr
other -> doc -> doc
forall doc. IsLine doc => doc -> doc
parens (Rational -> Platform -> UnwindExpr -> doc
forall doc. IsLine doc => Rational -> Platform -> UnwindExpr -> doc
pprUnwindExpr Rational
0 Platform
env UnwindExpr
other)
{-# SPECIALIZE pprUnwindExpr :: Rational -> Platform -> UnwindExpr -> SDoc #-}
{-# SPECIALIZE pprUnwindExpr :: Rational -> Platform -> UnwindExpr -> HLine #-}
toUnwindExpr :: Platform -> CmmExpr -> UnwindExpr
toUnwindExpr :: Platform -> CmmExpr -> UnwindExpr
toUnwindExpr Platform
_ (CmmLit (CmmInt Integer
i Width
_)) = Int -> UnwindExpr
UwConst (Integer -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
i)
toUnwindExpr Platform
_ (CmmLit (CmmLabel CLabel
l)) = CLabel -> UnwindExpr
UwLabel CLabel
l
toUnwindExpr Platform
_ (CmmRegOff (CmmGlobal GlobalRegUse
g) Int
i) = GlobalRegUse -> Int -> UnwindExpr
UwReg GlobalRegUse
g Int
i
toUnwindExpr Platform
_ (CmmReg (CmmGlobal GlobalRegUse
g)) = GlobalRegUse -> Int -> UnwindExpr
UwReg GlobalRegUse
g Int
0
toUnwindExpr Platform
platform (CmmLoad CmmExpr
e CmmType
_ AlignmentSpec
_) = UnwindExpr -> UnwindExpr
UwDeref (Platform -> CmmExpr -> UnwindExpr
toUnwindExpr Platform
platform CmmExpr
e)
toUnwindExpr Platform
platform e :: CmmExpr
e@(CmmMachOp MachOp
op [CmmExpr
e1, CmmExpr
e2]) =
case (MachOp
op, Platform -> CmmExpr -> UnwindExpr
toUnwindExpr Platform
platform CmmExpr
e1, Platform -> CmmExpr -> UnwindExpr
toUnwindExpr Platform
platform CmmExpr
e2) of
(MO_Add{}, UwReg GlobalRegUse
r Int
x, UwConst Int
y) -> GlobalRegUse -> Int -> UnwindExpr
UwReg GlobalRegUse
r (Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
y)
(MO_Sub{}, UwReg GlobalRegUse
r Int
x, UwConst Int
y) -> GlobalRegUse -> Int -> UnwindExpr
UwReg GlobalRegUse
r (Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
y)
(MO_Add{}, UwConst Int
x, UwReg GlobalRegUse
r Int
y) -> GlobalRegUse -> Int -> UnwindExpr
UwReg GlobalRegUse
r (Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
y)
(MO_Add{}, UwConst Int
x, UwConst Int
y) -> Int -> UnwindExpr
UwConst (Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
y)
(MO_Sub{}, UwConst Int
x, UwConst Int
y) -> Int -> UnwindExpr
UwConst (Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
y)
(MO_Mul{}, UwConst Int
x, UwConst Int
y) -> Int -> UnwindExpr
UwConst (Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
y)
(MO_Add{}, UnwindExpr
u1, UnwindExpr
u2 ) -> UnwindExpr -> UnwindExpr -> UnwindExpr
UwPlus UnwindExpr
u1 UnwindExpr
u2
(MO_Sub{}, UnwindExpr
u1, UnwindExpr
u2 ) -> UnwindExpr -> UnwindExpr -> UnwindExpr
UwMinus UnwindExpr
u1 UnwindExpr
u2
(MO_Mul{}, UnwindExpr
u1, UnwindExpr
u2 ) -> UnwindExpr -> UnwindExpr -> UnwindExpr
UwTimes UnwindExpr
u1 UnwindExpr
u2
(MachOp, UnwindExpr, UnwindExpr)
_otherwise -> String -> SDoc -> UnwindExpr
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"Unsupported operator in unwind expression!"
(Platform -> CmmExpr -> SDoc
forall env a. OutputableP env a => env -> a -> SDoc
pdoc Platform
platform CmmExpr
e)
toUnwindExpr Platform
platform CmmExpr
e
= String -> SDoc -> UnwindExpr
forall a. HasCallStack => String -> SDoc -> a
pprPanic String
"Unsupported unwind expression!" (Platform -> CmmExpr -> SDoc
forall env a. OutputableP env a => env -> a -> SDoc
pdoc Platform
platform CmmExpr
e)