macaw-base
Copyright(c) Galois Inc 2017-2019
Maintainerjhendrix@galois.com
Safe HaskellNone
LanguageHaskell2010

Data.Macaw.CFG.Rewriter

Contents

Description

This provides a rewriter for simplifying blocks by rewriting value computations and performing other rearrangement and adjustments.

The main entry point is calling runRewriter with a constructed RewriteContext to control the rewrite operation and a Rewriter monad computation that rewrites the statements for a particular block.

Note that while the common case is a single block, the Branch TermStmt that can be part of a block may refer to other Blocks (by ID) and therefore the rewriting operation may need to actually modify multiple blocks or even add new blocks for new Branch TermStmt targets.

Synopsis

Basic types

data RewriteContext arch s src tgt Source #

Information needed for rewriting.

mkRewriteContext Source #

Arguments

:: RegisterInfo (ArchReg arch) 
=> NonceGenerator (ST s) tgt 
-> (forall (tp :: Type). ArchFn arch (Value arch src) tp -> Rewriter arch s src tgt (Value arch tgt tp)) 
-> (ArchStmt arch (Value arch src) -> Rewriter arch s src tgt ()) 
-> (TermStmt arch tgt -> Rewriter arch s src tgt (TermStmt arch tgt))

Optional internal terminal statement rewriter; e.g. use is for block jump targets discovered outside normal pattern-matching in Discovery.hs.

-> Map SectionIndex (ArchSegmentOff arch)

Map from loaded section indices to their address.

-> ST s (RewriteContext arch s src tgt) 

data Rewriter arch s src tgt a Source #

Monad for constant propagation within a block.

Instances

Instances details
Applicative (Rewriter arch s src tgt) Source # 
Instance details

Defined in Data.Macaw.CFG.Rewriter

Methods

pure :: a -> Rewriter arch s src tgt a #

(<*>) :: Rewriter arch s src tgt (a -> b) -> Rewriter arch s src tgt a -> Rewriter arch s src tgt b #

liftA2 :: (a -> b -> c) -> Rewriter arch s src tgt a -> Rewriter arch s src tgt b -> Rewriter arch s src tgt c #

(*>) :: Rewriter arch s src tgt a -> Rewriter arch s src tgt b -> Rewriter arch s src tgt b #

(<*) :: Rewriter arch s src tgt a -> Rewriter arch s src tgt b -> Rewriter arch s src tgt a #

Functor (Rewriter arch s src tgt) Source # 
Instance details

Defined in Data.Macaw.CFG.Rewriter

Methods

fmap :: (a -> b) -> Rewriter arch s src tgt a -> Rewriter arch s src tgt b #

(<$) :: a -> Rewriter arch s src tgt b -> Rewriter arch s src tgt a #

Monad (Rewriter arch s src tgt) Source # 
Instance details

Defined in Data.Macaw.CFG.Rewriter

Methods

(>>=) :: Rewriter arch s src tgt a -> (a -> Rewriter arch s src tgt b) -> Rewriter arch s src tgt b #

(>>) :: Rewriter arch s src tgt a -> Rewriter arch s src tgt b -> Rewriter arch s src tgt b #

return :: a -> Rewriter arch s src tgt a #

runRewriter :: RewriteContext arch s src tgt -> Rewriter arch s src tgt (TermStmt arch tgt) -> ST s (RewriteContext arch s src tgt, [Stmt arch tgt], TermStmt arch tgt) Source #

Run the rewriter with the given context and collect the statements.

rewriteStmt :: Stmt arch src -> Rewriter arch s src tgt () Source #

Apply optimizations to a statement.

Since statements may be introduced/deleted during optimization, this should add new statements to the list of target statements rather than return the optimized statement.

rewriteValue :: forall arch src (tp :: Type) s tgt. Value arch src tp -> Rewriter arch s src tgt (Value arch tgt tp) Source #

rewriteApp :: forall arch tgt (tp :: Type) s src. App (Value arch tgt) tp -> Rewriter arch s src tgt (Value arch tgt tp) Source #

evalRewrittenArchFn :: forall arch tgt (tp :: Type) s src. HasRepr (ArchFn arch (Value arch tgt)) TypeRepr => ArchFn arch (Value arch tgt) tp -> Rewriter arch s src tgt (Value arch tgt tp) Source #

Add an assignment statement that evaluates the architecture function.

appendRewrittenArchStmt :: ArchStmt arch (Value arch tgt) -> Rewriter arch s src tgt () Source #

Add a architecture-specific statement to the list