1 /*
2 +----------------------------------------------------------------------+
3 | Zend OPcache |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998-2018 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Andi Gutmans <andi@zend.com> |
16 | Zeev Suraski <zeev@zend.com> |
17 | Stanislav Malyshev <stas@zend.com> |
18 | Dmitry Stogov <dmitry@zend.com> |
19 +----------------------------------------------------------------------+
20 */
21
22 /* pass 10:
23 * - remove NOPs
24 */
25
26 #include "php.h"
27 #include "Optimizer/zend_optimizer.h"
28 #include "Optimizer/zend_optimizer_internal.h"
29 #include "zend_API.h"
30 #include "zend_constants.h"
31 #include "zend_execute.h"
32 #include "zend_vm.h"
33
zend_optimizer_nop_removal(zend_op_array * op_array)34 void zend_optimizer_nop_removal(zend_op_array *op_array)
35 {
36 zend_op *end, *opline;
37 uint32_t new_count, i, shift;
38 int j;
39 uint32_t *shiftlist;
40 ALLOCA_FLAG(use_heap);
41
42 shiftlist = (uint32_t *)do_alloca(sizeof(uint32_t) * op_array->last, use_heap);
43 i = new_count = shift = 0;
44 end = op_array->opcodes + op_array->last;
45 for (opline = op_array->opcodes; opline < end; opline++) {
46
47 /* Kill JMP-over-NOP-s */
48 if (opline->opcode == ZEND_JMP && ZEND_OP1_JMP_ADDR(opline) > op_array->opcodes + i) {
49 /* check if there are only NOPs under the branch */
50 zend_op *target = ZEND_OP1_JMP_ADDR(opline) - 1;
51
52 while (target->opcode == ZEND_NOP) {
53 target--;
54 }
55 if (target == opline) {
56 /* only NOPs */
57 opline->opcode = ZEND_NOP;
58 }
59 }
60
61 shiftlist[i++] = shift;
62 if (opline->opcode == ZEND_NOP) {
63 shift++;
64 } else {
65 if (shift) {
66 zend_op *new_opline = op_array->opcodes + new_count;
67
68 *new_opline = *opline;
69 zend_optimizer_migrate_jump(op_array, new_opline, opline);
70 }
71 new_count++;
72 }
73 }
74
75 if (shift) {
76 op_array->last = new_count;
77 end = op_array->opcodes + op_array->last;
78
79 /* update JMPs */
80 for (opline = op_array->opcodes; opline<end; opline++) {
81 zend_optimizer_shift_jump(op_array, opline, shiftlist);
82 }
83
84 /* update brk/cont array */
85 for (j = 0; j < op_array->last_live_range; j++) {
86 op_array->live_range[j].start -= shiftlist[op_array->live_range[j].start];
87 op_array->live_range[j].end -= shiftlist[op_array->live_range[j].end];
88 }
89
90 /* update try/catch array */
91 for (j = 0; j < op_array->last_try_catch; j++) {
92 op_array->try_catch_array[j].try_op -= shiftlist[op_array->try_catch_array[j].try_op];
93 op_array->try_catch_array[j].catch_op -= shiftlist[op_array->try_catch_array[j].catch_op];
94 if (op_array->try_catch_array[j].finally_op) {
95 op_array->try_catch_array[j].finally_op -= shiftlist[op_array->try_catch_array[j].finally_op];
96 op_array->try_catch_array[j].finally_end -= shiftlist[op_array->try_catch_array[j].finally_end];
97 }
98 }
99
100 /* update early binding list */
101 if (op_array->early_binding != (uint32_t)-1) {
102 uint32_t *opline_num = &op_array->early_binding;
103
104 do {
105 *opline_num -= shiftlist[*opline_num];
106 opline_num = &op_array->opcodes[*opline_num].result.opline_num;
107 } while (*opline_num != (uint32_t)-1);
108 }
109 }
110 free_alloca(shiftlist, use_heap);
111 }
112