V-Gears 0
Free Final Fantasy VII engine.
ControlFlow.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2022 The V-Gears Team
3 *
4 * This file is part of V-Gears
5 *
6 * V-Gears is free software: you can redistribute it and/or modify it under
7 * terms of the GNU General Public License as published by the Free Software
8 * Foundation, version 3.0 (GPLv3) of the License.
9 *
10 * V-Gears is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16#pragma once
17
18#include "Engine.h"
19#include "Graph.h"
20
25
26 public:
27
34 ControlFlow(InstVec& insts, Engine& engine);
35
41 ControlFlow(const ControlFlow& control_flow) = delete;
42
48 ControlFlow& operator = (const ControlFlow& control_flow) = delete;
49
55 const Graph& GetGraph() const;
56
64 void CreateGroups();
65
74 const Graph& Analyze();
75
76 private:
77
82
87
92
96 std::map<uint32, GraphVertex> addr_map_;
97
103 GraphVertex Find(const InstPtr inst);
104
111
117 GraphVertex Find(uint32 address);
118
127 void Merge(GraphVertex graph_1, GraphVertex graph_2);
128
135 void SetStackLevel(GraphVertex graph, int level);
136
140 void DetectShortCircuit();
141
147 void DetectWhile();
148
152 void DetectDoWhile();
153
160 void DetectBreak();
161
168 void DetectContinue();
169
178 bool ValidateBreakOrContinue(GroupPtr group, GroupPtr condition_group);
179
185 void DetectIf();
186
192 void DetectElse();
193
204 bool ValidateElseBlock(GroupPtr if_group, GroupPtr start, GroupPtr end);
205
206};
207
boost::intrusive_ptr< Group > GroupPtr
Definition: Graph.h:76
Graph::vertex_descriptor GraphVertex
Definition: Graph.h:171
boost::adjacency_list< boost::setS, boost::listS, boost::bidirectionalS, GraphProperty, EdgeProperty > Graph
Definition: Graph.h:169
boost::intrusive_ptr< Instruction > InstPtr
Pointer to an Instruction.
Definition: Instruction.h:43
InstVec::const_iterator ConstInstIterator
Type representing a const_iterator over InstPtrs.
Definition: Instruction.h:392
std::vector< InstPtr > InstVec
Type representing a vector of InstPtrs.
Definition: Instruction.h:382
Class for doing code flow analysis.
Definition: ControlFlow.h:24
void DetectDoWhile()
Detects do-while blocks.
Definition: ControlFlow.cpp:347
std::map< uint32, GraphVertex > addr_map_
Map of addresses and vertices.
Definition: ControlFlow.h:96
bool ValidateElseBlock(GroupPtr if_group, GroupPtr start, GroupPtr end)
Checks if a candidate else block will cross block boundaries.
Definition: ControlFlow.cpp:551
void DetectShortCircuit()
Merged groups that are part of the same short-circuited condition.
Definition: ControlFlow.cpp:277
void CreateGroups()
Creates groups suitable for a stack-based machine.
Definition: ControlFlow.cpp:188
Engine & engine_
The engine used for the script.
Definition: ControlFlow.h:86
ControlFlow(InstVec &insts, Engine &engine)
Constructor for the control flow graph.
Definition: ControlFlow.cpp:64
const Graph & Analyze()
Performs control flow analysis.
Definition: ControlFlow.cpp:313
void DetectBreak()
Detects break statements.
Definition: ControlFlow.cpp:364
void DetectIf()
Detects if blocks.
Definition: ControlFlow.cpp:499
void SetStackLevel(GraphVertex graph, int level)
Sets the stack level for all instructions, using depth-first search.
Definition: ControlFlow.cpp:160
void DetectContinue()
Detects continue statements.
Definition: ControlFlow.cpp:395
void DetectWhile()
Detects while blocks.
Definition: ControlFlow.cpp:323
bool ValidateBreakOrContinue(GroupPtr group, GroupPtr condition_group)
Checks if a candidate break/continue goes to the closest loop.
Definition: ControlFlow.cpp:441
void DetectElse()
Detects else blocks.
Definition: ControlFlow.cpp:509
InstVec & insts_
The instructions being analyzed.
Definition: ControlFlow.h:91
Graph graph_
The control flow graph.
Definition: ControlFlow.h:81
ControlFlow & operator=(const ControlFlow &control_flow)=delete
Copy constructor disabled.
ControlFlow(const ControlFlow &control_flow)=delete
Copy constructor disabled.
GraphVertex Find(const InstPtr inst)
Finds a graph vertex through an instruction.
Definition: ControlFlow.cpp:118
const Graph & GetGraph() const
Retrieves the current control flow graph.
Definition: ControlFlow.cpp:116
void Merge(GraphVertex graph_1, GraphVertex graph_2)
Merges two graph vertices.
Definition: ControlFlow.cpp:131
Base class for engines.
Definition: Engine.h:34
unsigned int uint32
Definition: scummsys.h:435