File: basic_block.h

package info (click to toggle)
exult 1.12.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 43,608 kB
  • sloc: cpp: 169,917; xml: 7,400; yacc: 2,850; makefile: 2,419; java: 1,901; ansic: 1,654; lex: 673; sh: 539; objc: 416
file content (675 lines) | stat: -rw-r--r-- 15,213 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
/**
 ** basic_block.cc - Basic block analysis for usecode compiler.
 **
 ** Written: 2/7/08
 **/

/*
Copyright (C) 2008-2022 The Exult Team

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

#ifndef INCL_BASICBLOCK
#define INCL_BASICBLOCK 1

#include "common_types.h"
#include "opcodes.h"
#include "ucexpr.h"

#include <iomanip>
#include <iostream>
#include <set>
#include <vector>

class Basic_block;

/*
 *  Class representing a single usecode instruction and its
 *  parameters, if any; for jump instructions, this does NOT
 *  include the instruction offset.
 */

class Opcode {
protected:
	UsecodeOps        opcode;
	std::vector<char> params;
	bool              is_jump;
	bool              op32bit = false;

public:
	Opcode(UsecodeOps op) : opcode(op) {
		switch (opcode) {
		case UC_LOOPTOP:
		case UC_LOOPTOPS:
		case UC_LOOPTOPTHV:
		case UC_LOOPTOP32:
		case UC_LOOPTOPS32:
		case UC_LOOPTOPTHV32:
			is_jump = true;
			params.reserve(8);
			break;
		case UC_CMPS:
		case UC_DEFAULT:
		case UC_CMPS32:
		case UC_DEFAULT32:
			is_jump = true;
			params.reserve(2);
			break;
		case UC_CONVERSE:
		case UC_JMP:
		case UC_JNE:
		case UC_TRYSTART:
		case UC_CONVERSE32:
		case UC_JMP32:
		case UC_JNE32:
		case UC_TRYSTART32:
			is_jump = true;
			break;
		case UC_CALLINDEX:
		case UC_PUSHB:
			is_jump = false;
			params.reserve(1);
			break;
		case UC_ADDSI:
		case UC_ADDSV:
		case UC_AIDX:
		case UC_AIDXS:
		case UC_AIDXTHV:
		case UC_ARRC:
		case UC_CALL:
		case UC_CALLE:
		case UC_CALLM:
		case UC_CALLO:
		case UC_CLSCREATE:
		case UC_DBGLINE:
		case UC_POP:
		case UC_POPARR:
		case UC_POPARRS:
		case UC_POPARRTHV:
		case UC_POPF:
		case UC_POPSTATIC:
		case UC_POPTHV:
		case UC_PUSH:
		case UC_PUSHF:
		case UC_PUSHI:
		case UC_PUSHS:
		case UC_PUSHSTATIC:
		case UC_PUSHTHV:
			is_jump = false;
			params.reserve(2);
			break;
		case UC_CALLI:
		case UC_CALLIS:
			is_jump = false;
			params.reserve(3);
			break;
		case UC_CALLMS:
		case UC_DBGFUNC:
		case UC_ADDSI32:
		case UC_CALL32:
		case UC_CALLE32:
		case UC_PUSHI32:
		case UC_PUSHS32:
			is_jump = false;
			params.reserve(4);
			break;
		case UC_DBGFUNC32:
			is_jump = false;
			params.reserve(6);
			break;
		default:
			is_jump = false;
			break;
		}
	}

	UsecodeOps get_opcode() const {
		return opcode;
	}

	void WriteParam1(unsigned short val) {
		Write1(params, val);
	}

	void WriteParam2(unsigned short val) {
		Write2(params, val);
	}

	void WriteParam4(unsigned int val) {
		Write4(params, val);
	}

private:
	uint32 get_real_opcode() const {
		if (op32bit) {
			return static_cast<uint32>(opcode) | 0x80u;
		}
		return opcode;
	}

public:
	void write(std::vector<char>& out) {
		Write1(out, get_real_opcode());
		out.insert(out.end(), params.begin(), params.end());
	}
#ifdef DEBUG    // For debugging.
	void write_text() const {
		std::cout << std::setw(2) << std::setfill('0') << get_real_opcode()
				  << ' ';
		for (const char param : params) {
			std::cout << std::setw(2) << std::setfill('0')
					  << static_cast<int>(static_cast<unsigned char>(param))
					  << ' ';
		}
		if (is_jump) {
			std::cout << "<offset>";
		}
	}
#endif
	size_t get_size() const {    // Total size, including offset.
		const size_t size = 1 + params.size();
		if (is_32bit()) {
			return size + 4;    // +4 for 32-bit offset.
		}
		if (is_jump) {
			return size + 2;    // +2 for 16-bit offset.
		}
		return size;
	}

	bool is_32bit() const {    // Only matters for jumps.
		return op32bit;
	}

	void set_32bit() {    // Only matters for jumps.
		if (is_jump) {
			op32bit = true;
		}
	}

	bool is_return() const {
		return opcode == UC_RET || opcode == UC_RET2 || opcode == UC_RETV
			   || opcode == UC_RETZ;
	}

	bool is_abort() const {
		return opcode == UC_ABRT || opcode == UC_THROW;
	}
};

/*
 *  A class representing a basic block. A basic block is a group of
 *  statements/instructions with the following properties:
 *      (1) There is only one entrance to the block;
 *      (2) There is only one exit from the block.
 *  Thus, when a basic block is being executed, whenever the first
 *  instruction of the block is executed, all the instructions of
 *  of that block will also be executed.
 *
 *  A basic block must be created when
 *      (1) The given instruction is a jump target;
 *      (2) the previous block reached its end by means of a
 *          branching instruction or a return.
 */

class Basic_block {
	friend void PopOpcode(Basic_block* dest);
	friend void WriteOp(Basic_block* dest, UsecodeOps val);
	friend void WriteOpParam1(Basic_block* dest, unsigned short val);
	friend void WriteOpParam2(Basic_block* dest, unsigned short val);
	friend void WriteOpParam4(Basic_block* dest, unsigned int val);
	friend void WriteJumpParam2(Basic_block* dest, unsigned short val);
	friend void WriteJumpParam4(Basic_block* dest, unsigned int val);

protected:
	std::set<Basic_block*> predecessors;    // Blocks that came before this.
	int index = 0;    // Block index in the fun_blocks array *or*
	// -1 for the starting ("phantom") block.

	Basic_block* taken
			= nullptr;    // For conditional jumps, block taken to by true
	// condition (fall-through); for unconditional jumps, block
	// to jump to; for all others, the next (fall-through) block.
	int taken_index = -1;    // Index in the fun_blocks array, filled after
	// cleaning up unreachable blocks.
	Basic_block* ntaken
			= nullptr;    // For conditional jumps, block taken to by false
	// condition (jump); for all others, this is zero.
	int ntaken_index = -1;    // Index in the fun_blocks array, filled after
	// cleaning up unreachable blocks.

	Opcode* jmp_op = nullptr;    // 0 for no instruction (fall-through) to taken
	// block; otherwise, one of:
	//  conditional jumps:
	//      UC_JNE, UC_CMPS, UC_CONVERSE, UC_LOOPTOP, UC_LOOPTOPS,
	//      UC_LOOPTOPTHV, UC_TRYSTART, UC_DEFAULT
	//  unconditional jumps:
	//      UC_JMP
	// or the 32-bit versions of these instructions.

	std::vector<Opcode*> instructions;    // The instructions of the block
	bool                 reachable = false;

public:
	Basic_block() {
		instructions.reserve(100);
	}

	Basic_block(
			int ind, Basic_block* t = nullptr, Basic_block* n = nullptr,
			UsecodeOps ins = UC_INVALID)
			: index(ind), taken(t), ntaken(n), jmp_op(new Opcode(ins)) {
		if (index != -1) {
			instructions.reserve(100);
		}
	}

	~Basic_block() {
		predecessors.clear();
		for (auto* op : instructions) {
			delete op;
		}
		instructions.clear();
		delete jmp_op;
	}

	int get_index() const {
		return index;
	}

	void set_index(int ind) {
		index = ind;
	}

	UsecodeOps get_opcode() const {
		return jmp_op != nullptr ? jmp_op->get_opcode() : UC_INVALID;
	}

	void clear_jump() {
		delete jmp_op;
		jmp_op = nullptr;
	}

	UsecodeOps get_last_instruction() const {
		return !instructions.empty() ? instructions.back()->get_opcode()
									 : UC_INVALID;
	}

	void set_32bit_jump() {
		if (jmp_op != nullptr) {
			jmp_op->set_32bit();
		}
	}

	bool is_32bit_jump() {
		return jmp_op != nullptr ? jmp_op->is_32bit() : false;
	}

	bool does_not_jump() const {
		return jmp_op == nullptr;
	}

	bool ends_in_return() const {
		return !instructions.empty() && instructions.back()->is_return();
	}

	bool ends_in_abort() const {
		return !instructions.empty() && instructions.back()->is_abort();
	}

	size_t get_jump_size() const {
		if (jmp_op == nullptr) {
			return 0;    // Fall-through block or terminating block
		}
		return jmp_op->get_size();
	}

	size_t get_block_size() const {
		size_t size = get_jump_size();
		for (auto* op : instructions) {
			size += op->get_size();
		}
		return size;
	}

	bool is_jump_block() const {
		return jmp_op != nullptr ? (jmp_op->get_opcode() == UC_JMP) : false;
	}

	bool is_simple_jump_block() const {
		return is_jump_block() && instructions.empty();
	}

	bool is_conditionaljump_block() const {
		// instructions shouldn't be empty in this case.
		return jmp_op != nullptr ? (jmp_op->get_opcode() == UC_JNE) : false;
	}

	bool is_array_loop_block() const {
		if (jmp_op == nullptr) {
			return false;
		}
		const auto opcode = jmp_op->get_opcode();
		return opcode == UC_LOOPTOP || opcode == UC_LOOPTOPS
			   || opcode == UC_LOOPTOPTHV;
	}

	bool is_converse_case_block() const {
		if (jmp_op == nullptr) {
			return false;
		}
		const auto opcode = jmp_op->get_opcode();
		return opcode == UC_CMPS || opcode == UC_DEFAULT;
	}

	bool is_fallthrough_block() const {
		return jmp_op == nullptr && taken != nullptr;
	}

	bool is_empty_block() const {
		return jmp_op == nullptr && (instructions.empty());
	}

	bool is_end_block() const {
		return jmp_op == nullptr && taken != nullptr && (taken->index == -1);
	}

	bool is_forced_target() const {
		for (auto* block : predecessors) {
			if (!block->is_jump_block() && !block->is_fallthrough_block()) {
				return false;
			}
		}
		return true;
	}

	UsecodeOps get_return_opcode() const {
		return ends_in_return() ? instructions.back()->get_opcode()
								: UC_INVALID;
	}

	bool is_simple_return_block() const {
		return instructions.size() == 1 && instructions.back()->is_return();
	}

	bool is_simple_abort_block() const {
		return is_end_block() && instructions.size() == 1
			   && instructions.back()->is_abort();
	}

	bool is_childless() const {
		return taken == nullptr && ntaken == nullptr;
	}

	bool no_parents() const {
		return predecessors.empty();
	}

	bool is_orphan() const {
		return index >= 0 && no_parents();
	}

	bool is_reachable() const {
		return reachable;
	}

	void mark_reachable() {
		if (reachable) {
			return;
		}
		reachable = true;
		if (taken != nullptr) {
			taken->mark_reachable();
		}
		if (ntaken != nullptr) {
			ntaken->mark_reachable();
		}
	}

	Basic_block* get_taken() {
		return taken;
	}

	Basic_block* get_ntaken() {
		return ntaken;
	}

	int get_taken_index() {
		return taken_index;
	}

	int get_ntaken_index() {
		return ntaken_index;
	}

	bool has_single_predecessor() const {
		return predecessors.size() == 1;
	}

	size_t predecessor_count() const {
		return predecessors.size();
	}

	void set_taken(Basic_block* dest) {
		if (dest != nullptr) {
			dest->predecessors.insert(this);
		}
		if (taken != nullptr) {
			taken->predecessors.erase(this);
		}
		taken = dest;
	}

	void set_ntaken(Basic_block* dest) {
		if (dest != nullptr) {
			dest->predecessors.insert(this);
		}
		if (ntaken != nullptr) {
			ntaken->predecessors.erase(this);
		}
		ntaken = dest;
	}

	void set_targets(
			UsecodeOps op, Basic_block* t = nullptr, Basic_block* n = nullptr) {
		clear_jump();
		if (op != UC_INVALID) {
			jmp_op = new Opcode(op);
		}
		set_taken(t);
		set_ntaken(n);
	}

	void unlink_descendants() {
		if (taken != nullptr) {
			taken->predecessors.erase(this);
			taken = nullptr;
		}
		if (ntaken != nullptr) {
			ntaken->predecessors.erase(this);
			ntaken = nullptr;
		}
	}

	void link_predecessors() {
		for (auto* block : predecessors) {
			if (block->taken == this) {
				block->taken_index = index;
			}
			if (block->ntaken == this) {
				block->ntaken_index = index;
			}
		}
	}

	void unlink_predecessors() {
		for (auto* block : predecessors) {
			if (block->taken == this) {
				block->taken       = nullptr;
				block->taken_index = -1;
			}
			if (block->ntaken == this) {
				block->ntaken       = nullptr;
				block->ntaken_index = -1;
			}
		}
		predecessors.clear();
	}

	void link_through_block() {
		for (auto* pred : predecessors) {
			// Do NOT use set_taken, set_ntaken!
			if (pred->taken == this) {
				if (taken != nullptr) {    // Check almost unneeded.
					taken->predecessors.insert(pred);
				}
				pred->taken = taken;
			}
			if (pred->ntaken == this) {
				if (ntaken != nullptr) {
					ntaken->predecessors.insert(pred);
					pred->ntaken = ntaken;
				} else if (taken != nullptr) {
					taken->predecessors.insert(pred);
					pred->ntaken = taken;
				} else {
					pred->ntaken = nullptr;
				}
			}
		}
		predecessors.clear();
		unlink_descendants();
	}

	void merge_taken() {
		Basic_block* safetaken = taken;
		instructions.insert(
				instructions.end(), safetaken->instructions.begin(),
				safetaken->instructions.end());
		delete jmp_op;
		jmp_op = safetaken->jmp_op;
		set_taken(safetaken->taken);
		set_ntaken(safetaken->ntaken);
		// Prevent these from being deleted.
		safetaken->instructions.clear();
		safetaken->jmp_op = nullptr;
	}

	void write(std::vector<char>& out) {
		for (auto* op : instructions) {
			op->write(out);
		}
		if (jmp_op != nullptr) {
			jmp_op->write(out);
		}
	}
#ifdef DEBUG    // For debugging.
	void check() const {
		std::cout << std::hex << std::setw(8) << std::setfill('0') << this
				  << '\t' << std::setw(8) << std::setfill('0') << taken << '\t'
				  << std::setw(8) << std::setfill('0') << ntaken << '\t';
		for (const auto* instruction : instructions) {
			instruction->write_text();
		}
		if (jmp_op != nullptr) {
			jmp_op->write_text();
		} else {
			std::cout << "<no jump>";
		}
		std::cout << std::endl;
	}
#endif
};

/*
 *  Removes last opcode from the instruction block.
 */

inline void PopOpcode(Basic_block* dest) {
	if (dest->instructions.empty()) {
		return;
	}
	Opcode* op = dest->instructions.back();
	dest->instructions.pop_back();
	delete op;
}

/*
 *  Write an opcode value to the end of the instruction block
 *  and marks it as the last opcode.
 */

inline void WriteOp(Basic_block* dest, UsecodeOps val) {
	auto* op = new Opcode(val);
	dest->instructions.push_back(op);
}

/*
 *  Write a 1-byte value to the end of the jump parameters.
 */

inline void WriteOpParam1(Basic_block* dest, unsigned short val) {
	if (dest->instructions.empty()) {
		return;
	}
	dest->instructions.back()->WriteParam1(val);
}

/*
 *  Write a 2-byte value to the end of the jump parameters.
 */

inline void WriteOpParam2(Basic_block* dest, unsigned short val) {
	if (dest->instructions.empty()) {
		return;
	}
	dest->instructions.back()->WriteParam2(val);
}

/*
 *  Write a 4-byte value to the end of the jump parameters.
 */

inline void WriteOpParam4(Basic_block* dest, unsigned int val) {
	if (dest->instructions.empty()) {
		return;
	}
	dest->instructions.back()->WriteParam4(val);
}

/*
 *  Write a 2-byte value to the end of the jump parameters.
 */

inline void WriteJumpParam2(Basic_block* dest, unsigned short val) {
	if (dest->jmp_op == nullptr) {
		return;
	}
	dest->jmp_op->WriteParam2(val);
}

/*
 *  Write a 4-byte value to the end of the jump parameters.
 */

inline void WriteJumpParam4(Basic_block* dest, unsigned int val) {
	if (dest->jmp_op == nullptr) {
		return;
	}
	dest->jmp_op->WriteParam4(val);
}

#endif