File: block.cpp

package info (click to toggle)
residualvm 0.3.1%2Bdfsg-2
  • links: PTS, VCS
  • area: contrib
  • in suites: bullseye
  • size: 31,292 kB
  • sloc: cpp: 227,029; sh: 7,256; xml: 1,731; perl: 1,067; java: 861; asm: 738; python: 691; ansic: 272; makefile: 139; objc: 81; sed: 22; php: 1
file content (329 lines) | stat: -rw-r--r-- 8,866 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* ResidualVM - A 3D game interpreter
 *
 * ResidualVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the AUTHORS
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "engines/stark/tools/block.h"

#include "engines/stark/tools/command.h"

#include "common/debug.h"

namespace Stark {
namespace Tools {

Block::Block() :
		_follower(nullptr),
		_trueBranch(nullptr),
		_falseBranch(nullptr),
		_controlStructure(nullptr),
		_infiniteLoopStart(false) {

}

void Block::appendCommand(CFGCommand *command) {
	_commands.push_back(command);
	command->setBlock(this);
}

bool Block::isEmpty() const {
	return _commands.empty();
}

void Block::print() const {
	for (uint i = 0; i < _commands.size(); i++) {
		_commands[i]->printCall();
	}

	if (_controlStructure) {
		switch (_controlStructure->type) {
			case ControlStructure::kTypeIf:
				debug("if%s: %d else: %d next: %d",
				      _controlStructure->invertedCondition ? " not" : "",
				      _controlStructure->thenHead->getFirstCommandIndex(),
				      _controlStructure->elseHead ? _controlStructure->elseHead->getFirstCommandIndex() : -1,
				      _controlStructure->next ? _controlStructure->next->getFirstCommandIndex() : -1);
				break;
			case ControlStructure::kTypeWhile:
				debug("while%s: %d next: %d",
				      _controlStructure->invertedCondition ? " not" : "",
				      _controlStructure->loopHead->getFirstCommandIndex(),
				      _controlStructure->next->getFirstCommandIndex());
				break;
		}
	}

	if (_infiniteLoopStart) {
		debug("infinite loop start: %d", getFirstCommandIndex());
	}

	if (isCondition() && !_controlStructure) {
		debug("unrecognized control flow");
	}
}

void Block::setBranches(Block *trueBranch, Block *falseBranch) {
	_trueBranch = trueBranch;
	_falseBranch = falseBranch;

	trueBranch->addPredecessor(this);
	falseBranch->addPredecessor(this);
}

void Block::setFollower(Block *follower) {
	_follower = follower;
	follower->addPredecessor(this);
}

void Block::addPredecessor(Block *predecessor) {
	_predecessors.push_back(predecessor);
}

bool Block::isCondition() const {
	return _trueBranch != nullptr && _falseBranch != nullptr;
}

bool Block::hasPredecessor(Block *predecessor) const {
	Common::Array<const Block *> visited;
	return hasPredecessorIntern(visited, predecessor);
}

bool Block::hasPredecessorIntern(Common::Array<const Block *> &visited, Block *predecessor) const {
	visited.push_back(this);

	if (isInfiniteLoopStart()) {
		// Don't follow infinite loops
		return false;
	}

	for (uint i = 0; i < _predecessors.size(); i++) {
		if (_predecessors[i] == predecessor) {
			return true;
		}

		bool alreadyVisited = Common::find(visited.begin(), visited.end(), _predecessors[i]) != visited.end();
		if (!alreadyVisited && _predecessors[i]->hasPredecessorIntern(visited, predecessor)) {
			return true;
		}
	}

	return false;
}

bool Block::hasSuccessor(Block *successor) const {
	Common::Array<const Block *> visited;
	return hasSuccessorIntern(visited, successor);
}

bool Block::hasSuccessorIntern(Common::Array<const Block *> &visited, Block *successor) const {
	visited.push_back(this);

	if (this == successor) {
		return true;
	}

	bool followerHasSuccessor = hasChildSuccessorIntern(visited, _follower, successor);
	bool trueBranchHasSuccessor = hasChildSuccessorIntern(visited, _trueBranch, successor);
	bool falseBranchHasSuccessor = hasChildSuccessorIntern(visited, _falseBranch, successor);

	return followerHasSuccessor || trueBranchHasSuccessor || falseBranchHasSuccessor;
}

bool Block::hasChildSuccessorIntern(Common::Array<const Block *> &visited, Block *child, Block *successor) const {
	if (!child) {
		return false;
	}

	bool alreadyVisited = Common::find(visited.begin(), visited.end(), child) != visited.end();
	return !alreadyVisited && child->hasSuccessorIntern(visited, successor);
}

Block *Block::findMergePoint(Block *other) {
	// TODO: Find the closest merge point? How to define that notion?
	Common::Array<const Block *> visited;
	return findMergePointIntern(visited, other);
}

Block *Block::findMergePointIntern(Common::Array<const Block *> &visited, Block *other) {
	visited.push_back(this);

	if (other == this) {
		return this;
	}

	if (hasPredecessor(other)) {
		return this;
	}

	Block *mergePoint = findChildMergePoint(visited, _follower, other);
	if (mergePoint) {
		return mergePoint;
	}

	mergePoint = findChildMergePoint(visited, _trueBranch, other);
	if (mergePoint) {
		return mergePoint;
	}

	mergePoint = findChildMergePoint(visited, _falseBranch, other);
	if (mergePoint) {
		return mergePoint;
	}

	return nullptr;
}

Block *Block::findChildMergePoint(Common::Array<const Block *> &visited, Block *child, Block *other) const {
	if (child) {
		bool alreadyVisited = Common::find(visited.begin(), visited.end(), child) != visited.end();
		if (!alreadyVisited) {
			return child->findMergePointIntern(visited, other);
		}
	}

	return nullptr;
}

bool Block::checkAllBranchesConverge(Block *junction) const {
	// Check if there is at least one path to the junction node
	if (!hasSuccessor(junction)) {
		return false;
	}

	// Check all the successors go to the junction point or loop infinitely
	Common::Array<const Block *> visited;
	return checkAllBranchesConvergeIntern(visited, junction);
}

bool Block::checkAllBranchesConvergeIntern(Common::Array<const Block *> &visited, Block *junction) const {
	visited.push_back(this);

	if (this == junction) {
		return true;
	}

	if (!_follower && !_trueBranch && !_falseBranch) {
		return false;
	}

	if (isInfiniteLoopStart()) {
		// Don't follow infinite loops
		return false;
	}

	bool followerConverges = checkChildConvergeIntern(visited, _follower, junction);
	bool trueBranchConverges = checkChildConvergeIntern(visited, _trueBranch, junction);
	bool falseBranchConverges = checkChildConvergeIntern(visited, _falseBranch, junction);

	return followerConverges && trueBranchConverges && falseBranchConverges;
}

bool Block::checkChildConvergeIntern(Common::Array<const Block *> &visited, Block *child, Block *junction) const {
	if (child) {
		bool alreadyVisited = Common::find(visited.begin(), visited.end(), child) != visited.end();
		if (!alreadyVisited) {
			return child->checkAllBranchesConvergeIntern(visited, junction);
		}
	}

	return true;
}

Block *Block::getFollower() const {
	return _follower;
}

Block *Block::getTrueBranch() const {
	return _trueBranch;
}

Block *Block::getFalseBranch() const {
	return _falseBranch;
}

uint16 Block::getFirstCommandIndex() const {
	return _commands[0]->getIndex();
}

bool Block::hasControlStructure() const {
	return _controlStructure != nullptr;
}

void Block::setControlStructure(ControlStructure *controlStructure) {
	_controlStructure = controlStructure;
}

ControlStructure *Block::getControlStructure() const {
	return _controlStructure;
}

void Block::setInfiniteLoopStart(bool infiniteLoopStart) {
	_infiniteLoopStart = infiniteLoopStart;
}

bool Block::isInfiniteLoopStart() const {
	return _infiniteLoopStart;
}

Common::Array<CFGCommand *> Block::getLinearCommands() const {
	if (!hasControlStructure()) {
		return _commands;
	} else {
		Common::Array<CFGCommand *> commands;

		// The last command in the block is the condition.
		// Exclude it from the linear commands.
		for (uint i = 0; i < _commands.size() - 1; i ++) {
			commands.push_back(_commands[i]);
		}

		return commands;
	}
}

CFGCommand *Block::getConditionCommand() const {
	if (hasControlStructure()) {
		// The condition command is always the last one
		return _commands.back();
	} else {
		return nullptr;
	}
}

bool Block::allowDuplication() const {
	// Allow simple termination blocks to be duplicated in the decompiled output
	bool isScriptEnd = !_follower && !_trueBranch && !_falseBranch;
	bool isShort = _commands.size() < 5;

	return isScriptEnd && isShort;
}

ControlStructure::ControlStructure(ControlStructureType t) :
		type(t),
		condition(nullptr),
		invertedCondition(false),
		loopHead(nullptr),
		thenHead(nullptr),
		elseHead(nullptr),
		next(nullptr) {
}

} // End of namespace Tools
} // End of namespace Stark