V-Gears 0
Free Final Fantasy VII engine.
ControlFlow Class Reference

Class for doing code flow analysis. More...

#include <ControlFlow.h>

Collaboration diagram for ControlFlow:

Public Member Functions

 ControlFlow (InstVec &insts, Engine &engine)
 Constructor for the control flow graph. More...
 
 ControlFlow (const ControlFlow &control_flow)=delete
 Copy constructor disabled. More...
 
ControlFlowoperator= (const ControlFlow &control_flow)=delete
 Copy constructor disabled. More...
 
const GraphGetGraph () const
 Retrieves the current control flow graph. More...
 
void CreateGroups ()
 Creates groups suitable for a stack-based machine. More...
 
const GraphAnalyze ()
 Performs control flow analysis. More...
 

Private Member Functions

GraphVertex Find (const InstPtr inst)
 Finds a graph vertex through an instruction. More...
 
GraphVertex Find (ConstInstIterator it)
 Finds a graph vertex through an instruction iterator. More...
 
GraphVertex Find (uint32 address)
 Finds a graph vertex through an address. More...
 
void Merge (GraphVertex graph_1, GraphVertex graph_2)
 Merges two graph vertices. More...
 
void SetStackLevel (GraphVertex graph, int level)
 Sets the stack level for all instructions, using depth-first search. More...
 
void DetectShortCircuit ()
 Merged groups that are part of the same short-circuited condition. More...
 
void DetectWhile ()
 Detects while blocks. More...
 
void DetectDoWhile ()
 Detects do-while blocks. More...
 
void DetectBreak ()
 Detects break statements. More...
 
void DetectContinue ()
 Detects continue statements. More...
 
bool ValidateBreakOrContinue (GroupPtr group, GroupPtr condition_group)
 Checks if a candidate break/continue goes to the closest loop. More...
 
void DetectIf ()
 Detects if blocks. More...
 
void DetectElse ()
 Detects else blocks. More...
 
bool ValidateElseBlock (GroupPtr if_group, GroupPtr start, GroupPtr end)
 Checks if a candidate else block will cross block boundaries. More...
 

Private Attributes

Graph graph_
 The control flow graph. More...
 
Engineengine_
 The engine used for the script. More...
 
InstVecinsts_
 The instructions being analyzed. More...
 
std::map< uint32, GraphVertexaddr_map_
 Map of addresses and vertices. More...
 

Detailed Description

Class for doing code flow analysis.

Constructor & Destructor Documentation

◆ ControlFlow() [1/2]

ControlFlow::ControlFlow ( InstVec insts,
Engine engine 
)

Constructor for the control flow graph.

Parameters
[in]instsThe instructions to analyze control flow for.
[in]enginePointer to the Engine used for the script.

◆ ControlFlow() [2/2]

ControlFlow::ControlFlow ( const ControlFlow control_flow)
delete

Copy constructor disabled.

Parameters
[in]control_flowThe control flow to copy.

Member Function Documentation

◆ Analyze()

const Graph & ControlFlow::Analyze ( )

Performs control flow analysis.

The constructs are detected in the following order: do-while, while, break, continue, if/else.

Returns
The control flow graph after analysis.

◆ CreateGroups()

void ControlFlow::CreateGroups ( )

Creates groups suitable for a stack-based machine.

Before group creation, the expected stack level for each instruction is determined. After group creation, short-circuit detection is applied to the groups.

◆ DetectBreak()

void ControlFlow::DetectBreak ( )
private

Detects break statements.

Do-while and while detection must be completed before running this method.

◆ DetectContinue()

void ControlFlow::DetectContinue ( )
private

Detects continue statements.

Do-while and while detection must be completed before running this method.

◆ DetectDoWhile()

void ControlFlow::DetectDoWhile ( )
private

Detects do-while blocks.

◆ DetectElse()

void ControlFlow::DetectElse ( )
private

Detects else blocks.

Must be performed after if detection.

◆ DetectIf()

void ControlFlow::DetectIf ( )
private

Detects if blocks.

Must be performed after break and continue detection.

◆ DetectShortCircuit()

void ControlFlow::DetectShortCircuit ( )
private

Merged groups that are part of the same short-circuited condition.

◆ DetectWhile()

void ControlFlow::DetectWhile ( )
private

Detects while blocks.

Do-while detection must be completed before running this method.

◆ Find() [1/3]

GraphVertex ControlFlow::Find ( const InstPtr  inst)
private

Finds a graph vertex through an instruction.

Parameters
[in]instThe instruction to find the vertex for.

◆ Find() [2/3]

GraphVertex ControlFlow::Find ( ConstInstIterator  it)
private

Finds a graph vertex through an instruction iterator.

Parameters
[in]itThe iterator to find the vertex for.

◆ Find() [3/3]

GraphVertex ControlFlow::Find ( uint32  address)
private

Finds a graph vertex through an address.

Parameters
[in]addressThe address to find the vertex for.

◆ GetGraph()

const Graph & ControlFlow::GetGraph ( ) const

Retrieves the current control flow graph.

Returns
The current control flow graph.

◆ Merge()

void ControlFlow::Merge ( GraphVertex  graph_1,
GraphVertex  graph_2 
)
private

Merges two graph vertices.

graph_2 will be merged into graph_1.

Parameters
[in,out]graph_1The first vertex to merge.
[in]graph_2The second vertex to merge.

◆ operator=()

ControlFlow & ControlFlow::operator= ( const ControlFlow control_flow)
delete

Copy constructor disabled.

Parameters
[in]control_flowThe control flow to copy.

◆ SetStackLevel()

void ControlFlow::SetStackLevel ( GraphVertex  graph,
int  level 
)
private

Sets the stack level for all instructions, using depth-first search.

Parameters
[in]graphThe GraphVertex to search from.
[in]levelThe stack level when g is reached.

◆ ValidateBreakOrContinue()

bool ControlFlow::ValidateBreakOrContinue ( GroupPtr  group,
GroupPtr  condition_group 
)
private

Checks if a candidate break/continue goes to the closest loop.

Parameters
[in]groupThe group containing the candidate break/continue.
[in]condition_groupThe group containing the respective loop condition.
Returns
True if the validation succeeded, false if it did not.

◆ ValidateElseBlock()

bool ControlFlow::ValidateElseBlock ( GroupPtr  if_group,
GroupPtr  start,
GroupPtr  end 
)
private

Checks if a candidate else block will cross block boundaries.

Parameters
[in]if_groupThe group containing the if this else candidate is associated with.
[in]startThe group containing the start of the else.
[in]endThe group immediately after the group ending the else.
Returns
True if the validation succeeded, false if it did not.

Member Data Documentation

◆ addr_map_

std::map<uint32, GraphVertex> ControlFlow::addr_map_
private

Map of addresses and vertices.

◆ engine_

Engine& ControlFlow::engine_
private

The engine used for the script.

◆ graph_

Graph ControlFlow::graph_
private

The control flow graph.

◆ insts_

InstVec& ControlFlow::insts_
private

The instructions being analyzed.


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