Ghidra Decompiler Analysis Engine
Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
FlowBlock Class Reference

Description of a control-flow block containing PcodeOps. More...

#include <block.hh>

Inheritance diagram for FlowBlock:
Inheritance graph
[legend]

Public Types

enum  block_type {
  t_plain, t_basic, t_graph, t_copy,
  t_goto, t_multigoto, t_ls, t_condition,
  t_if, t_whiledo, t_dowhile, t_switch,
  t_infloop
}
 The possible block types.
 
enum  block_flags {
  f_goto_goto = 1, f_break_goto = 2, f_continue_goto = 4, f_switch_out = 0x10,
  f_unstructured_targ = 0x20, f_mark = 0x80, f_mark2 = 0x100, f_entry_point = 0x200,
  f_interior_gotoout = 0x400, f_interior_gotoin = 0x800, f_label_bumpup = 0x1000, f_donothing_loop = 0x2000,
  f_dead = 0x4000, f_whiledo_overflow = 0x8000, f_flip_path = 0x10000, f_joined_block = 0x20000,
  f_duplicate_block = 0x40000
}
 Boolean properties of blocks. More...
 
enum  edge_flags {
  f_goto_edge = 1, f_loop_edge = 2, f_defaultswitch_edge = 4, f_irreducible = 8,
  f_tree_edge = 0x10, f_forward_edge = 0x20, f_cross_edge = 0x40, f_back_edge = 0x80,
  f_loop_exit_edge = 0x100
}
 Boolean properties on edges. More...
 

Public Member Functions

 FlowBlock (void)
 Construct a block with no edges.
 
virtual void markLabelBumpUp (bool bump)
 Let hierarchical blocks steal labels of their (first) components. More...
 
virtual void printHeader (ostream &s) const
 Print a simple description of this to stream. More...
 
virtual void printTree (ostream &s, int4 level) const
 Print tree structure of any blocks owned by this. More...
 
virtual void emit (PrintLanguage *lng) const
 Emit the instructions in this FlowBlock as structured code. More...
 
virtual bool negateCondition (bool toporbottom)
 Flip the condition computed by this. More...
 
virtual bool preferComplement (Funcdata &data)
 Rearrange this hierarchy to simplify boolean expressions. More...
 
virtual FlowBlockgetSplitPoint (void)
 Get the leaf splitting block. More...
 
virtual int4 flipInPlaceTest (vector< PcodeOp * > &fliplist) const
 Test normalizing the conditional branch in this. More...
 
virtual void flipInPlaceExecute (void)
 Perform the flip to normalize conditional branch executed by this block. More...
 
virtual FlowBlocknextFlowAfter (const FlowBlock *bl) const
 Get the leaf FlowBlock that will execute after the given FlowBlock. More...
 
virtual void saveXmlHeader (ostream &s) const
 Save basic information as XML attributes. More...
 
virtual void restoreXmlHeader (const Element *el)
 Restore basic information for XML attributes. More...
 
virtual void restoreXmlBody (List::const_iterator &iter, List::const_iterator enditer, BlockMap &resolver)
 Restore details about this FlowBlock from an XML stream. More...
 
void saveXmlEdges (ostream &s) const
 Save edge information to an XML stream. More...
 
void restoreXmlEdges (List::const_iterator &iter, List::const_iterator enditer, BlockMap &resolver)
 Restore edges from an XML stream. More...
 
void saveXml (ostream &s) const
 Write out this to an XML stream. More...
 
void restoreXml (const Element *el, BlockMap &resolver)
 Restore this from an XML stream. More...
 
const FlowBlocknextInFlow (void) const
 Return next block to be executed in flow. More...
 
void setGotoBranch (int4 i)
 Mark a goto branch. More...
 
bool isJumpTarget (void) const
 Return true if non-fallthru jump flows into this. More...
 
const FlowBlockgetFrontLeaf (void) const
 Get the first leaf FlowBlock. More...
 
FlowBlockgetFrontLeaf (void)
 Get the first leaf FlowBlock. More...
 
int4 calcDepth (const FlowBlock *leaf) const
 Get the depth of the given component FlowBlock. More...
 
bool dominates (const FlowBlock *subBlock) const
 Does this block dominate the given block. More...
 
bool restrictedByConditional (const FlowBlock *cond) const
 Check if the condition from the given block holds for this block. More...
 
bool hasLoopIn (void) const
 Is there a looping edge coming into this block. More...
 
bool hasLoopOut (void) const
 Is there a looping edge going out of this block. More...
 
int4 getInIndex (const FlowBlock *bl) const
 Get the incoming edge index for the given FlowBlock. More...
 
int4 getOutIndex (const FlowBlock *bl) const
 Get the outgoing edge index for the given FlowBlock. More...
 
bool isDecisionOut (int4 i) const
 Can this and the i-th output be merged into a BlockIf or BlockList.
 
bool isDecisionIn (int4 i) const
 Can this and the i-th input be merged into a BlockIf or BlockList.
 
bool isLoopDAGOut (int4 i) const
 Is the i-th outgoing edge part of the DAG sub-graph.
 
bool isLoopDAGIn (int4 i) const
 Is the i-th incoming edge part of the DAG sub-graph.
 
JumpTablegetJumptable (void) const
 Get the JumpTable associated this block. More...
 

Static Public Member Functions

static block_type nameToType (const string &name)
 Get the block_type associated with a name string. More...
 
static string typeToName (block_type bt)
 Get the name string associated with a block_type. More...
 
static bool compareBlockIndex (const FlowBlock *bl1, const FlowBlock *bl2)
 Compare FlowBlock by index. More...
 
static bool compareFinalOrder (const FlowBlock *bl1, const FlowBlock *bl2)
 Final FlowBlock comparison. More...
 
static FlowBlockfindCommonBlock (FlowBlock *bl1, FlowBlock *bl2)
 Find the common dominator of two FlowBlocks. More...
 
static FlowBlockfindCommonBlock (const vector< FlowBlock * > &blockSet)
 Find common dominator of multiple FlowBlocks. More...
 

Friends

class BlockGraph
 

Detailed Description

Description of a control-flow block containing PcodeOps.

This is the base class for basic blocks (BlockBasic) and the hierarchical description of structured code. At all levels, these can be viewed as a block of code (PcodeOp objects) with other blocks flowing into and out of it.

Member Enumeration Documentation

◆ block_flags

Boolean properties of blocks.

The first four flags describe attributes of the blocks primary exiting edges The f_interior_* flags do not necessarily apply to these edges. They are used with the block structure and hierarchy algorithms where unstructured jumps are removed from the list of primary edges. These flags keep track only of the existence of unstructured edges, even though they aren't listed

Enumerator
f_goto_goto 

(Block ends in) non-structured branch

f_break_goto 

Block ends with a break;.

f_continue_goto 

Block ends with a continue;.

f_switch_out 

Output is decided by switch.

f_unstructured_targ 

Block is destination of unstructured goto.

f_mark 

Generic way to mark a block.

f_mark2 

A secondary mark.

f_entry_point 

Official entry point of the function.

f_interior_gotoout 

The block has an unstructured jump out of interior.

f_interior_gotoin 

Block is target of unstructured jump to its interior.

f_label_bumpup 

Any label printed higher up in hierarchy.

f_donothing_loop 

Block does nothing in infinite loop (halt)

f_dead 

Block is in process of being deleted.

f_whiledo_overflow 

Set if the conditional block of a whiledo is too big to print as while(cond) { ...

f_flip_path 

If true, out edges have been flipped since last time path was traced.

f_joined_block 

Block is a merged form of original basic blocks.

f_duplicate_block 

Block is a duplicated version of an original basic block.

◆ edge_flags

Boolean properties on edges.

Enumerator
f_goto_edge 

Edge is unstructured.

f_loop_edge 

Edge completes a loop, removing these edges gives you a DAG.

f_defaultswitch_edge 

This is default edge from switchblock.

f_irreducible 

Edge which must be removed to make graph reducible.

f_tree_edge 

An edge in the spanning tree.

f_forward_edge 

An edge that jumps forward in the spanning tree.

f_cross_edge 

An edge that crosses subtrees in the spanning tree.

f_back_edge 

Within (reducible) graph, a back edge defining a loop.

f_loop_exit_edge 

Edge exits the body of a loop.

Member Function Documentation

◆ calcDepth()

int4 FlowBlock::calcDepth ( const FlowBlock leaf) const

Get the depth of the given component FlowBlock.

How many getParent() calls from the leaf to this

Parameters
leafis the component FlowBlock
Returns
the depth count

◆ compareBlockIndex()

bool FlowBlock::compareBlockIndex ( const FlowBlock bl1,
const FlowBlock bl2 
)
inlinestatic

Compare FlowBlock by index.

Parameters
bl1is the first FlowBlock to compare
bl2is the second FlowBlock to compare
Returns
true if the first comes before the second

◆ compareFinalOrder()

bool FlowBlock::compareFinalOrder ( const FlowBlock bl1,
const FlowBlock bl2 
)
static

Final FlowBlock comparison.

Comparator for ordering the final 0-exit blocks

Parameters
bl1is the first FlowBlock to compare
bl2is the second FlowBlock
Returns
true if the first comes before the second

◆ dominates()

bool FlowBlock::dominates ( const FlowBlock subBlock) const

Does this block dominate the given block.

Return true if this block dominates the given block (or is equal to it). This assumes that block indices have been set with a reverse post order so that having a smaller index is a necessary condition for dominance.

Parameters
subBlockis the given block to test against this for dominance
Returns
true if this dominates

◆ emit()

void FlowBlock::emit ( PrintLanguage lng) const
inlinevirtual

Emit the instructions in this FlowBlock as structured code.

This is the main entry point, at the control-flow level, for printing structured code.

Parameters
lngis the PrintLanguage that provides details of the high-level language being printed

Reimplemented in BlockSwitch, BlockInfLoop, BlockDoWhile, BlockWhileDo, BlockIf, BlockCondition, BlockList, BlockMultiGoto, BlockGoto, BlockCopy, BlockBasic, and BlockGraph.

◆ findCommonBlock() [1/2]

FlowBlock * FlowBlock::findCommonBlock ( const vector< FlowBlock * > &  blockSet)
static

Find common dominator of multiple FlowBlocks.

Find the most immediate dominating FlowBlock of all blocks in the given set. The container must not be empty.

Parameters
blockSetis the given set of blocks
Returns
the most immediate dominating FlowBlock

◆ findCommonBlock() [2/2]

FlowBlock * FlowBlock::findCommonBlock ( FlowBlock bl1,
FlowBlock bl2 
)
static

Find the common dominator of two FlowBlocks.

Within the dominator tree, find the earliest common ancestor of two FlowBlocks

Parameters
bl1is the first FlowBlock
bl2is the second
Returns
the common ancestor which dominates both

◆ flipInPlaceExecute()

void FlowBlock::flipInPlaceExecute ( void  )
inlinevirtual

Perform the flip to normalize conditional branch executed by this block.

This reverses the outgoing edge order in the right basic blocks, but does not modify the instructions directly.

Reimplemented in BlockCondition, and BlockBasic.

◆ flipInPlaceTest()

int4 FlowBlock::flipInPlaceTest ( vector< PcodeOp * > &  fliplist) const
inlinevirtual

Test normalizing the conditional branch in this.

Find the set of PcodeOp objects that need to be adjusted to flip the condition this FlowBlock calculates.

Return:

  • 0 if the flip would normalize the condition
  • 1 if the flip doesn't affect normalization of the condition
  • 2 if the flip produces an unnormalized condition
    Parameters
    fliplistwill contain the PcodeOps that need to be adjusted
    Returns
    0 if the condition will be normalized, 1 or 2 otherwise

Reimplemented in BlockCondition, and BlockBasic.

◆ getFrontLeaf() [1/2]

FlowBlock * FlowBlock::getFrontLeaf ( void  )

Get the first leaf FlowBlock.

Keep descending tree hierarchy, taking the front block, until we get to the bottom copy block

Returns
the first leaf FlowBlock to execute

◆ getFrontLeaf() [2/2]

const FlowBlock * FlowBlock::getFrontLeaf ( void  ) const

Get the first leaf FlowBlock.

Keep descending tree hierarchy, taking the front block, until we get to the bottom copy block

Returns
the first leaf FlowBlock to execute

◆ getInIndex()

int4 FlowBlock::getInIndex ( const FlowBlock bl) const

Get the incoming edge index for the given FlowBlock.

Search through incoming blocks in edge order for the given block.

Parameters
blis the given FlowBlock
Returns
the matching edge index or -1 if bl doesn't flow into this

◆ getJumptable()

JumpTable * FlowBlock::getJumptable ( void  ) const

Get the JumpTable associated this block.

If this FlowBlock was ends with a computed jump, retrieve the associated JumpTable object

Returns
the JumpTable object or NULL

◆ getOutIndex()

int4 FlowBlock::getOutIndex ( const FlowBlock bl) const

Get the outgoing edge index for the given FlowBlock.

Search through outgoing blocks in edge order for the given block.

Parameters
blis the given FlowBlock
Returns
the matching edge index or -1 if bl doesn't flow out of this

◆ getSplitPoint()

FlowBlock * FlowBlock::getSplitPoint ( void  )
inlinevirtual

Get the leaf splitting block.

If this block ends with a conditional branch, return the deepest component block that performs the split. This component needs to be able to perform flipInPlaceTest() and flipInPlaceExecute()

Returns
the component FlowBlock or NULL if this doesn't end in a conditional branch

Reimplemented in BlockCondition, BlockList, BlockCopy, and BlockBasic.

◆ hasLoopIn()

bool FlowBlock::hasLoopIn ( void  ) const

Is there a looping edge coming into this block.

Returns
true if this is the top of a loop

◆ hasLoopOut()

bool FlowBlock::hasLoopOut ( void  ) const

Is there a looping edge going out of this block.

Returns
true if this is the bottom of a loop

◆ isJumpTarget()

bool FlowBlock::isJumpTarget ( void  ) const

Return true if non-fallthru jump flows into this.

return true if block is the target of a jump

◆ markLabelBumpUp()

void FlowBlock::markLabelBumpUp ( bool  bump)
virtual

Let hierarchical blocks steal labels of their (first) components.

Parameters
bumpif true, mark that labels for this block are printed by somebody higher in hierarchy

Reimplemented in BlockInfLoop, BlockDoWhile, BlockWhileDo, and BlockGraph.

◆ nameToType()

FlowBlock::block_type FlowBlock::nameToType ( const string &  nm)
static

Get the block_type associated with a name string.

Given a string describing a FlowBlock type, return the block_type. This is currently only used by the restoreXml() process. TODO: Fill in the remaining names and types

Parameters
nmis the name string
Returns
the corresponding block_type

◆ negateCondition()

bool FlowBlock::negateCondition ( bool  toporbottom)
virtual

Flip the condition computed by this.

Flip the order of outgoing edges (at least). This should also affect the original op causing the condition. Note: we don't have to flip at all levels of the hierarchy only at the top and at the bottom

Parameters
toporbottomis true if this is the top outermost block of the hierarchy getting negated
Returns
true if a change was made to data-flow

Reimplemented in BlockCondition, BlockList, BlockCopy, and BlockBasic.

◆ nextFlowAfter()

FlowBlock * FlowBlock::nextFlowAfter ( const FlowBlock bl) const
inlinevirtual

Get the leaf FlowBlock that will execute after the given FlowBlock.

Within the hierarchy of this FlowBlock, assume the given FlowBlock will fall-thru in its execution at some point. Return the first leaf block (BlockBasic or BlockCopy) that will execute after the given FlowBlock completes, assuming this is a unique block.

Parameters
blis the given FlowBlock
Returns
the next FlowBlock to execute or NULL

Reimplemented in BlockSwitch, BlockInfLoop, BlockDoWhile, BlockWhileDo, BlockIf, BlockCondition, BlockMultiGoto, BlockGoto, and BlockGraph.

◆ nextInFlow()

const FlowBlock * FlowBlock::nextInFlow ( void  ) const

Return next block to be executed in flow.

If there are two branches, pick the fall-thru branch

Returns
the next block in flow, or NULL otherwise

◆ preferComplement()

bool FlowBlock::preferComplement ( Funcdata data)
inlinevirtual

Rearrange this hierarchy to simplify boolean expressions.

For the instructions in this block, decide if the control-flow structure can be rearranged so that boolean expressions come out more naturally.

Parameters
datais the function to analyze
Returns
true if a change was made

Reimplemented in BlockIf.

◆ printHeader()

void FlowBlock::printHeader ( ostream &  s) const
virtual

Print a simple description of this to stream.

Only print a header for this single block

Parameters
sis the output stream

Reimplemented in BlockSwitch, BlockInfLoop, BlockDoWhile, BlockWhileDo, BlockIf, BlockCondition, BlockList, BlockMultiGoto, BlockGoto, BlockCopy, and BlockBasic.

◆ printTree()

void FlowBlock::printTree ( ostream &  s,
int4  level 
) const
virtual

Print tree structure of any blocks owned by this.

Recursively print out the hierarchical structure of this FlowBlock.

Parameters
sis the output stream
levelis the current level of indentation

Reimplemented in BlockCopy, and BlockGraph.

◆ restoreXml()

void FlowBlock::restoreXml ( const Element el,
BlockMap resolver 
)

Restore this from an XML stream.

Recover this and all it sub-components from an XML <block> tag.

This will construct all the sub-components using resolver as a factory.

Parameters
elis the root XML element
resolveracts as a factory and resolves cross-references

◆ restoreXmlBody()

virtual void FlowBlock::restoreXmlBody ( List::const_iterator &  iter,
List::const_iterator  enditer,
BlockMap resolver 
)
inlinevirtual

Restore details about this FlowBlock from an XML stream.

Parameters
iteris an iterator to XML elements containing component tags etc.
enditermarks the end of the XML tags
resolveris used to recover FlowBlock objects based on XML references

Reimplemented in BlockBasic, and BlockGraph.

◆ restoreXmlEdges()

void FlowBlock::restoreXmlEdges ( List::const_iterator &  iter,
List::const_iterator  enditer,
BlockMap resolver 
)

Restore edges from an XML stream.

Parameters
iteris an iterator to the <edge> tags
enditermarks the end of the list of tags
resolveris used to recover FlowBlock cross-references

◆ restoreXmlHeader()

void FlowBlock::restoreXmlHeader ( const Element el)
virtual

Restore basic information for XML attributes.

Parameters
elis the XML element to pull attributes from

◆ restrictedByConditional()

bool FlowBlock::restrictedByConditional ( const FlowBlock cond) const

Check if the condition from the given block holds for this block.

We assume the given block has 2 out-edges and that this block is immediately reached by one of these two edges. Some condition holds when traversing the out-edge to this, and the complement of the condition holds for traversing the other out-edge. We verify that the condition holds for this entire block. More specifically, we check that that there is no path to this through the sibling edge, where the complement of the condition holds (unless we loop back through the conditional block).

Parameters
condis the conditional block with 2 out-edges
Returns
true if the condition holds for this block

◆ saveXml()

void FlowBlock::saveXml ( ostream &  s) const

Write out this to an XML stream.

Serialize this and all its sub-components as an XML <block> tag.

Parameters
sis the output stream

◆ saveXmlEdges()

void FlowBlock::saveXmlEdges ( ostream &  s) const

Save edge information to an XML stream.

Write <edge> tags to stream

Parameters
sis the output stream

◆ saveXmlHeader()

void FlowBlock::saveXmlHeader ( ostream &  s) const
virtual

Save basic information as XML attributes.

Parameters
sis the output stream

Reimplemented in BlockCondition, and BlockCopy.

◆ setGotoBranch()

void FlowBlock::setGotoBranch ( int4  i)

Mark a goto branch.

This is the main entry point for marking a branch from one block to another as unstructured.

Parameters
iis the index of the outgoing edge to mark

◆ typeToName()

string FlowBlock::typeToName ( FlowBlock::block_type  bt)
static

Get the name string associated with a block_type.

For use in serializng FlowBlocks to XML.

Parameters
btis the block_type
Returns
the corresponding name string

The documentation for this class was generated from the following files: