1 /* pass 4
2 * - optimize INIT_FCALL_BY_NAME to DO_FCALL
3 */
4 #if ZEND_EXTENSION_API_NO > PHP_5_3_X_API_NO
5
6 typedef struct _optimizer_call_info {
7 zend_function *func;
8 zend_op *opline;
9 } optimizer_call_info;
10
optimize_func_calls(zend_op_array * op_array,zend_persistent_script * script TSRMLS_DC)11 static void optimize_func_calls(zend_op_array *op_array, zend_persistent_script *script TSRMLS_DC) {
12 zend_op *opline = op_array->opcodes;
13 zend_op *end = opline + op_array->last;
14 int call = 0;
15 #if ZEND_EXTENSION_API_NO > PHP_5_4_X_API_NO
16 optimizer_call_info *call_stack = ecalloc(op_array->nested_calls + 1, sizeof(optimizer_call_info));
17 #else
18 int stack_size = 4;
19 optimizer_call_info *call_stack = ecalloc(stack_size, sizeof(optimizer_call_info));
20 #endif
21
22 while (opline < end) {
23 switch (opline->opcode) {
24 case ZEND_INIT_FCALL_BY_NAME:
25 case ZEND_INIT_NS_FCALL_BY_NAME:
26 if (ZEND_OP2_TYPE(opline) == IS_CONST) {
27 zend_function *func;
28 zval *function_name = &op_array->literals[opline->op2.constant + 1].constant;
29 if ((zend_hash_quick_find(&script->function_table,
30 Z_STRVAL_P(function_name), Z_STRLEN_P(function_name) + 1,
31 Z_HASH_P(function_name), (void **)&func) == SUCCESS)) {
32 call_stack[call].func = func;
33 }
34 }
35 /* break missing intentionally */
36 case ZEND_NEW:
37 case ZEND_INIT_METHOD_CALL:
38 case ZEND_INIT_STATIC_METHOD_CALL:
39 call_stack[call].opline = opline;
40 call++;
41 #if ZEND_EXTENSION_API_NO < PHP_5_5_X_API_NO
42 if (call == stack_size) {
43 stack_size += 4;
44 call_stack = erealloc(call_stack, sizeof(optimizer_call_info) * stack_size);
45 memset(call_stack + 4, 0, 4 * sizeof(optimizer_call_info));
46 }
47 #endif
48 break;
49 case ZEND_DO_FCALL_BY_NAME:
50 call--;
51 if (call_stack[call].func && call_stack[call].opline) {
52 zend_op *fcall = call_stack[call].opline;
53
54 opline->opcode = ZEND_DO_FCALL;
55 ZEND_OP1_TYPE(opline) = IS_CONST;
56 opline->op1.constant = fcall->op2.constant + 1;
57 op_array->literals[fcall->op2.constant + 1].cache_slot = op_array->literals[fcall->op2.constant].cache_slot;
58 literal_dtor(&ZEND_OP2_LITERAL(fcall));
59 if (fcall->opcode == ZEND_INIT_NS_FCALL_BY_NAME) {
60 literal_dtor(&op_array->literals[fcall->op2.constant + 2].constant);
61 }
62 MAKE_NOP(fcall);
63 } else if (opline->extended_value == 0 &&
64 call_stack[call].opline &&
65 call_stack[call].opline->opcode == ZEND_INIT_FCALL_BY_NAME &&
66 ZEND_OP2_TYPE(call_stack[call].opline) == IS_CONST) {
67
68 zend_op *fcall = call_stack[call].opline;
69
70 opline->opcode = ZEND_DO_FCALL;
71 ZEND_OP1_TYPE(opline) = IS_CONST;
72 opline->op1.constant = fcall->op2.constant + 1;
73 op_array->literals[fcall->op2.constant + 1].cache_slot = op_array->literals[fcall->op2.constant].cache_slot;
74 literal_dtor(&ZEND_OP2_LITERAL(fcall));
75 MAKE_NOP(fcall);
76 }
77 call_stack[call].func = NULL;
78 call_stack[call].opline = NULL;
79 break;
80 case ZEND_FETCH_FUNC_ARG:
81 case ZEND_FETCH_OBJ_FUNC_ARG:
82 case ZEND_FETCH_DIM_FUNC_ARG:
83 if (call_stack[call - 1].func) {
84 if (ARG_SHOULD_BE_SENT_BY_REF(call_stack[call - 1].func, (opline->extended_value & ZEND_FETCH_ARG_MASK))) {
85 opline->extended_value &= ZEND_FETCH_TYPE_MASK;
86 opline->opcode -= 9;
87 } else {
88 opline->extended_value &= ZEND_FETCH_TYPE_MASK;
89 opline->opcode -= 12;
90 }
91 }
92 break;
93 case ZEND_SEND_VAL:
94 if (opline->extended_value == ZEND_DO_FCALL_BY_NAME && call_stack[call - 1].func) {
95 if (ARG_MUST_BE_SENT_BY_REF(call_stack[call - 1].func, opline->op2.num)) {
96 /* We won't convert it into_DO_FCALL to emit error at run-time */
97 call_stack[call - 1].opline = NULL;
98 } else {
99 opline->extended_value = ZEND_DO_FCALL;
100 }
101 }
102 break;
103 case ZEND_SEND_VAR:
104 if (opline->extended_value == ZEND_DO_FCALL_BY_NAME && call_stack[call - 1].func) {
105 if (ARG_SHOULD_BE_SENT_BY_REF(call_stack[call - 1].func, opline->op2.num)) {
106 opline->opcode = ZEND_SEND_REF;
107 }
108 opline->extended_value = ZEND_DO_FCALL;
109 }
110 break;
111 case ZEND_SEND_VAR_NO_REF:
112 if (!(opline->extended_value & ZEND_ARG_COMPILE_TIME_BOUND) && call_stack[call - 1].func) {
113 if (ARG_SHOULD_BE_SENT_BY_REF(call_stack[call - 1].func, opline->op2.num)) {
114 opline->extended_value |= ZEND_ARG_COMPILE_TIME_BOUND | ZEND_ARG_SEND_BY_REF;
115 } else if (opline->extended_value) {
116 opline->extended_value |= ZEND_ARG_COMPILE_TIME_BOUND;
117 } else {
118 opline->opcode = ZEND_SEND_VAR;
119 opline->extended_value = ZEND_DO_FCALL;
120 }
121 }
122 break;
123 case ZEND_SEND_REF:
124 if (opline->extended_value == ZEND_DO_FCALL_BY_NAME && call_stack[call - 1].func) {
125 /* We won't handle run-time pass by reference */
126 call_stack[call - 1].opline = NULL;
127 }
128 break;
129 #if ZEND_EXTENSION_API_NO > PHP_5_5_X_API_NO
130 case ZEND_SEND_UNPACK:
131 call_stack[call - 1].func = NULL;
132 call_stack[call - 1].opline = NULL;
133 break;
134 #endif
135 default:
136 break;
137 }
138 opline++;
139 }
140
141 efree(call_stack);
142 }
143 #endif
144