1 | #if 0 |
---|
2 | # define XCACHE_DEBUG |
---|
3 | #endif |
---|
4 | |
---|
5 | #include "utils.h" |
---|
6 | #include "optimizer.h" |
---|
7 | /* the "vector" stack */ |
---|
8 | #include "stack.h" |
---|
9 | #include "xcache_globals.h" |
---|
10 | |
---|
11 | #ifdef XCACHE_DEBUG |
---|
12 | # include "processor.h" |
---|
13 | # include "const_string.h" |
---|
14 | # include "ext/standard/php_var.h" |
---|
15 | #endif |
---|
16 | |
---|
17 | #ifdef IS_CV |
---|
18 | # define XCACHE_IS_CV IS_CV |
---|
19 | #else |
---|
20 | # define XCACHE_IS_CV 16 |
---|
21 | #endif |
---|
22 | |
---|
23 | typedef int bbid_t; |
---|
24 | enum { |
---|
25 | BBID_INVALID = -1, |
---|
26 | }; |
---|
27 | /* {{{ basic block */ |
---|
28 | typedef struct _bb_t { |
---|
29 | bbid_t id; |
---|
30 | zend_bool used; |
---|
31 | |
---|
32 | zend_bool alloc; |
---|
33 | zend_op *opcodes; |
---|
34 | int count; |
---|
35 | int size; |
---|
36 | |
---|
37 | bbid_t fall; |
---|
38 | #ifdef ZEND_ENGINE_2 |
---|
39 | bbid_t catch; |
---|
40 | #endif |
---|
41 | |
---|
42 | int opnum; /* opnum after joining basic block */ |
---|
43 | } bb_t; |
---|
44 | /* }}} */ |
---|
45 | |
---|
46 | /* basic blocks */ |
---|
47 | typedef xc_stack_t bbs_t; |
---|
48 | |
---|
49 | /* op array helper functions */ |
---|
50 | static int op_array_convert_switch(zend_op_array *op_array) /* {{{ */ |
---|
51 | { |
---|
52 | int i; |
---|
53 | |
---|
54 | if (op_array->brk_cont_array == NULL) { |
---|
55 | return SUCCESS; |
---|
56 | } |
---|
57 | |
---|
58 | for (i = 0; i < op_array->last; i ++) { |
---|
59 | zend_op *opline = &op_array->opcodes[i]; |
---|
60 | zend_brk_cont_element *jmp_to; |
---|
61 | int array_offset, nest_levels, original_nest_levels; |
---|
62 | |
---|
63 | if (opline->opcode != ZEND_BRK && opline->opcode != ZEND_CONT) { |
---|
64 | continue; |
---|
65 | } |
---|
66 | if (Z_OP_TYPE(opline->op2) != IS_CONST |
---|
67 | || Z_OP_CONSTANT(opline->op2).type != IS_LONG) { |
---|
68 | return FAILURE; |
---|
69 | } |
---|
70 | |
---|
71 | nest_levels = Z_OP_CONSTANT(opline->op2).value.lval; |
---|
72 | original_nest_levels = nest_levels; |
---|
73 | |
---|
74 | array_offset = Z_OP(opline->op1).opline_num; |
---|
75 | do { |
---|
76 | if (array_offset == -1) { |
---|
77 | /* this is a runtime error in ZE |
---|
78 | zend_error(E_ERROR, "Cannot break/continue %d level%s", original_nest_levels, (original_nest_levels == 1) ? "" : "s"); |
---|
79 | */ |
---|
80 | return FAILURE; |
---|
81 | } |
---|
82 | jmp_to = &op_array->brk_cont_array[array_offset]; |
---|
83 | if (nest_levels > 1) { |
---|
84 | zend_op *brk_opline = &op_array->opcodes[jmp_to->brk]; |
---|
85 | |
---|
86 | switch (brk_opline->opcode) { |
---|
87 | case ZEND_SWITCH_FREE: |
---|
88 | break; |
---|
89 | case ZEND_FREE: |
---|
90 | break; |
---|
91 | } |
---|
92 | } |
---|
93 | array_offset = jmp_to->parent; |
---|
94 | } while (--nest_levels > 0); |
---|
95 | |
---|
96 | /* rewrite to jmp */ |
---|
97 | if (opline->opcode == ZEND_BRK) { |
---|
98 | Z_OP(opline->op1).opline_num = jmp_to->brk; |
---|
99 | } |
---|
100 | else { |
---|
101 | Z_OP(opline->op1).opline_num = jmp_to->cont; |
---|
102 | } |
---|
103 | Z_OP_TYPE(opline->op2) = IS_UNUSED; |
---|
104 | opline->opcode = ZEND_JMP; |
---|
105 | } |
---|
106 | |
---|
107 | if (op_array->brk_cont_array != NULL) { |
---|
108 | efree(op_array->brk_cont_array); |
---|
109 | op_array->brk_cont_array = NULL; |
---|
110 | } |
---|
111 | op_array->last_brk_cont = 0; |
---|
112 | return SUCCESS; |
---|
113 | } |
---|
114 | /* }}} */ |
---|
115 | /* {{{ op_flowinfo helper func */ |
---|
116 | enum { |
---|
117 | XC_OPNUM_INVALID = -1, |
---|
118 | }; |
---|
119 | typedef struct { |
---|
120 | int jmpout_op1; |
---|
121 | int jmpout_op2; |
---|
122 | int jmpout_ext; |
---|
123 | zend_bool fall; |
---|
124 | } op_flowinfo_t; |
---|
125 | static void op_flowinfo_init(op_flowinfo_t *fi) |
---|
126 | { |
---|
127 | fi->jmpout_op1 = fi->jmpout_op2 = fi->jmpout_ext = XC_OPNUM_INVALID; |
---|
128 | fi->fall = 0; |
---|
129 | } |
---|
130 | /* }}} */ |
---|
131 | static int op_get_flowinfo(op_flowinfo_t *fi, zend_op *opline) /* {{{ */ |
---|
132 | { |
---|
133 | op_flowinfo_init(fi); |
---|
134 | |
---|
135 | /* break=will fall */ |
---|
136 | switch (opline->opcode) { |
---|
137 | #ifdef ZEND_HANDLE_EXCEPTION |
---|
138 | case ZEND_HANDLE_EXCEPTION: |
---|
139 | #endif |
---|
140 | case ZEND_RETURN: |
---|
141 | case ZEND_EXIT: |
---|
142 | return SUCCESS; /* no fall */ |
---|
143 | |
---|
144 | case ZEND_JMP: |
---|
145 | fi->jmpout_op1 = Z_OP(opline->op1).opline_num; |
---|
146 | return SUCCESS; /* no fall */ |
---|
147 | |
---|
148 | case ZEND_JMPZNZ: |
---|
149 | fi->jmpout_op2 = Z_OP(opline->op2).opline_num; |
---|
150 | fi->jmpout_ext = (int) opline->extended_value; |
---|
151 | return SUCCESS; /* no fall */ |
---|
152 | |
---|
153 | case ZEND_JMPZ: |
---|
154 | case ZEND_JMPNZ: |
---|
155 | case ZEND_JMPZ_EX: |
---|
156 | case ZEND_JMPNZ_EX: |
---|
157 | #ifdef ZEND_JMP_SET |
---|
158 | case ZEND_JMP_SET: |
---|
159 | #endif |
---|
160 | #ifdef ZEND_JMP_NO_CTOR |
---|
161 | case ZEND_JMP_NO_CTOR: |
---|
162 | #endif |
---|
163 | #ifdef ZEND_NEW |
---|
164 | case ZEND_NEW: |
---|
165 | #endif |
---|
166 | #ifdef ZEND_FE_RESET |
---|
167 | case ZEND_FE_RESET: |
---|
168 | #endif |
---|
169 | case ZEND_FE_FETCH: |
---|
170 | fi->jmpout_op2 = Z_OP(opline->op2).opline_num; |
---|
171 | break; |
---|
172 | |
---|
173 | #ifdef ZEND_CATCH |
---|
174 | case ZEND_CATCH: |
---|
175 | fi->jmpout_ext = (int) opline->extended_value; |
---|
176 | break; |
---|
177 | #endif |
---|
178 | |
---|
179 | default: |
---|
180 | return FAILURE; |
---|
181 | } |
---|
182 | |
---|
183 | fi->fall = 1; |
---|
184 | return SUCCESS; |
---|
185 | } |
---|
186 | /* }}} */ |
---|
187 | #ifdef XCACHE_DEBUG |
---|
188 | static void op_snprint(char *buf, int size, zend_uchar op_type, znode_op *op) /* {{{ */ |
---|
189 | { |
---|
190 | switch (op_type) { |
---|
191 | case IS_CONST: |
---|
192 | { |
---|
193 | zval result; |
---|
194 | zval *zv = &Z_OP_CONSTANT(*op); |
---|
195 | TSRMLS_FETCH(); |
---|
196 | |
---|
197 | /* TODO: update for PHP 6 */ |
---|
198 | php_start_ob_buffer(NULL, 0, 1 TSRMLS_CC); |
---|
199 | php_var_export(&zv, 1 TSRMLS_CC); |
---|
200 | |
---|
201 | php_ob_get_buffer(&result TSRMLS_CC); |
---|
202 | php_end_ob_buffer(0, 0 TSRMLS_CC); |
---|
203 | snprintf(buf, size, Z_STRVAL(result)); |
---|
204 | zval_dtor(&result); |
---|
205 | } |
---|
206 | break; |
---|
207 | |
---|
208 | case IS_TMP_VAR: |
---|
209 | snprintf(buf, size, "t@%d", Z_OP(*op).var); |
---|
210 | break; |
---|
211 | |
---|
212 | case XCACHE_IS_CV: |
---|
213 | case IS_VAR: |
---|
214 | snprintf(buf, size, "v@%d", Z_OP(*op).var); |
---|
215 | break; |
---|
216 | |
---|
217 | case IS_UNUSED: |
---|
218 | if (Z_OP(*op).opline_num) { |
---|
219 | snprintf(buf, size, "u#%d", Z_OP(op).opline_num); |
---|
220 | } |
---|
221 | else { |
---|
222 | snprintf(buf, size, "-"); |
---|
223 | } |
---|
224 | break; |
---|
225 | |
---|
226 | default: |
---|
227 | snprintf(buf, size, "%d %d", op->op_type, Z_OP(op).var); |
---|
228 | } |
---|
229 | } |
---|
230 | /* }}} */ |
---|
231 | static void op_print(int line, zend_op *first, zend_op *end) /* {{{ */ |
---|
232 | { |
---|
233 | zend_op *opline; |
---|
234 | for (opline = first; opline < end; opline ++) { |
---|
235 | char buf_r[20]; |
---|
236 | char buf_1[20]; |
---|
237 | char buf_2[20]; |
---|
238 | op_snprint(buf_r, sizeof(buf_r), Z_OP_TYPE(opline->result), &opline->result); |
---|
239 | op_snprint(buf_1, sizeof(buf_1), Z_OP_TYPE(opline->op1), &opline->op1); |
---|
240 | op_snprint(buf_2, sizeof(buf_2), Z_OP_TYPE(opline->op2), &opline->op2); |
---|
241 | fprintf(stderr, |
---|
242 | "%3d %3d" |
---|
243 | " %-25s%-5s%-20s%-20s%5lu\r\n" |
---|
244 | , opline->lineno, opline - first + line |
---|
245 | , xc_get_opcode(opline->opcode), buf_r, buf_1, buf_2, opline->extended_value); |
---|
246 | } |
---|
247 | } |
---|
248 | /* }}} */ |
---|
249 | #endif |
---|
250 | |
---|
251 | /* |
---|
252 | * basic block functions |
---|
253 | */ |
---|
254 | |
---|
255 | static bb_t *bb_new_ex(zend_op *opcodes, int count) /* {{{ */ |
---|
256 | { |
---|
257 | bb_t *bb = (bb_t *) ecalloc(sizeof(bb_t), 1); |
---|
258 | |
---|
259 | bb->fall = BBID_INVALID; |
---|
260 | #ifdef ZEND_ENGINE_2 |
---|
261 | bb->catch = BBID_INVALID; |
---|
262 | #endif |
---|
263 | |
---|
264 | if (opcodes) { |
---|
265 | bb->alloc = 0; |
---|
266 | bb->size = bb->count = count; |
---|
267 | bb->opcodes = opcodes; |
---|
268 | } |
---|
269 | else { |
---|
270 | bb->alloc = 1; |
---|
271 | bb->size = bb->count = 8; |
---|
272 | bb->opcodes = ecalloc(sizeof(zend_op), bb->size); |
---|
273 | } |
---|
274 | |
---|
275 | return bb; |
---|
276 | } |
---|
277 | /* }}} */ |
---|
278 | #define bb_new() bb_new_ex(NULL, 0) |
---|
279 | static void bb_destroy(bb_t *bb) /* {{{ */ |
---|
280 | { |
---|
281 | if (bb->alloc) { |
---|
282 | efree(bb->opcodes); |
---|
283 | } |
---|
284 | efree(bb); |
---|
285 | } |
---|
286 | /* }}} */ |
---|
287 | #ifdef XCACHE_DEBUG |
---|
288 | static void bb_print(bb_t *bb, zend_op *opcodes) /* {{{ */ |
---|
289 | { |
---|
290 | int line = bb->opcodes - opcodes; |
---|
291 | op_flowinfo_t fi; |
---|
292 | zend_op *last = bb->opcodes + bb->count - 1; |
---|
293 | bbid_t catchbbid; |
---|
294 | #ifdef ZEND_ENGINE_2 |
---|
295 | catchbbid = BBID_INVALID; |
---|
296 | #else |
---|
297 | catchbbid = bb->catch; |
---|
298 | #endif |
---|
299 | |
---|
300 | op_get_flowinfo(&fi, last); |
---|
301 | |
---|
302 | fprintf(stderr, |
---|
303 | "\r\n==== #%-3d cnt:%-3d lno:%-3d" |
---|
304 | " %c%c" |
---|
305 | " op1:%-3d op2:%-3d ext:%-3d fal:%-3d cat:%-3d %s ====\r\n" |
---|
306 | , bb->id, bb->count, bb->alloc ? -1 : line |
---|
307 | , bb->used ? 'U' : ' ', bb->alloc ? 'A' : ' ' |
---|
308 | , fi.jmpout_op1, fi.jmpout_op2, fi.jmpout_ext, bb->fall, catchbbid, xc_get_opcode(last->opcode) |
---|
309 | ); |
---|
310 | op_print(line, bb->opcodes, last + 1); |
---|
311 | } |
---|
312 | /* }}} */ |
---|
313 | #endif |
---|
314 | |
---|
315 | static bb_t *bbs_get(bbs_t *bbs, int n) /* {{{ */ |
---|
316 | { |
---|
317 | return (bb_t *) xc_stack_get(bbs, n); |
---|
318 | } |
---|
319 | /* }}} */ |
---|
320 | static int bbs_count(bbs_t *bbs) /* {{{ */ |
---|
321 | { |
---|
322 | return xc_stack_count(bbs); |
---|
323 | } |
---|
324 | /* }}} */ |
---|
325 | static void bbs_destroy(bbs_t *bbs) /* {{{ */ |
---|
326 | { |
---|
327 | bb_t *bb; |
---|
328 | while (bbs_count(bbs)) { |
---|
329 | bb = (bb_t *) xc_stack_pop(bbs); |
---|
330 | bb_destroy(bb); |
---|
331 | } |
---|
332 | xc_stack_destroy(bbs); |
---|
333 | } |
---|
334 | /* }}} */ |
---|
335 | #ifdef XCACHE_DEBUG |
---|
336 | static void bbs_print(bbs_t *bbs, zend_op *opcodes) /* {{{ */ |
---|
337 | { |
---|
338 | int i; |
---|
339 | for (i = 0; i < xc_stack_count(bbs); i ++) { |
---|
340 | bb_print(bbs_get(bbs, i), opcodes); |
---|
341 | } |
---|
342 | } |
---|
343 | /* }}} */ |
---|
344 | #endif |
---|
345 | #define bbs_init(bbs) xc_stack_init_ex(bbs, 16) |
---|
346 | static bb_t *bbs_add_bb(bbs_t *bbs, bb_t *bb) /* {{{ */ |
---|
347 | { |
---|
348 | bb->id = (bbid_t) xc_stack_count(bbs); |
---|
349 | xc_stack_push(bbs, (void *) bb); |
---|
350 | return bb; |
---|
351 | } |
---|
352 | /* }}} */ |
---|
353 | static bb_t *bbs_new_bb_ex(bbs_t *bbs, zend_op *opcodes, int count) /* {{{ */ |
---|
354 | { |
---|
355 | return bbs_add_bb(bbs, bb_new_ex(opcodes, count)); |
---|
356 | } |
---|
357 | /* }}} */ |
---|
358 | static int bbs_build_from(bbs_t *bbs, zend_op_array *op_array, int count) /* {{{ */ |
---|
359 | { |
---|
360 | int i, start; |
---|
361 | bb_t *pbb; |
---|
362 | bbid_t id; |
---|
363 | op_flowinfo_t fi; |
---|
364 | zend_op *opline; |
---|
365 | ALLOCA_FLAG(use_heap_bbids) |
---|
366 | ALLOCA_FLAG(use_heap_catchbbids) |
---|
367 | ALLOCA_FLAG(use_heap_markbbhead) |
---|
368 | bbid_t *bbids = my_do_alloca(count * sizeof(bbid_t), use_heap_bbids); |
---|
369 | #ifdef ZEND_ENGINE_2 |
---|
370 | bbid_t *catchbbids = my_do_alloca(count * sizeof(bbid_t), use_heap_catchbbids); |
---|
371 | #endif |
---|
372 | zend_bool *markbbhead = my_do_alloca(count * sizeof(zend_bool), use_heap_markbbhead); |
---|
373 | |
---|
374 | /* {{{ mark jmpin/jumpout */ |
---|
375 | memset(markbbhead, 0, count * sizeof(zend_bool)); |
---|
376 | |
---|
377 | markbbhead[0] = 1; |
---|
378 | for (i = 0; i < count; i ++) { |
---|
379 | if (op_get_flowinfo(&fi, &op_array->opcodes[i]) == SUCCESS) { |
---|
380 | if (fi.jmpout_op1 != XC_OPNUM_INVALID) { |
---|
381 | markbbhead[fi.jmpout_op1] = 1; |
---|
382 | } |
---|
383 | if (fi.jmpout_op2 != XC_OPNUM_INVALID) { |
---|
384 | markbbhead[fi.jmpout_op2] = 1; |
---|
385 | } |
---|
386 | if (fi.jmpout_ext != XC_OPNUM_INVALID) { |
---|
387 | markbbhead[fi.jmpout_ext] = 1; |
---|
388 | } |
---|
389 | if (i + 1 < count) { |
---|
390 | markbbhead[i + 1] = 1; |
---|
391 | } |
---|
392 | } |
---|
393 | } |
---|
394 | #ifdef ZEND_ENGINE_2 |
---|
395 | /* mark try start */ |
---|
396 | for (i = 0; i < op_array->last_try_catch; i ++) { |
---|
397 | markbbhead[op_array->try_catch_array[i].try_op] = 1; |
---|
398 | } |
---|
399 | #endif |
---|
400 | /* }}} */ |
---|
401 | /* {{{ fill op lines with newly allocated id */ |
---|
402 | for (i = 0; i < count; i ++) { |
---|
403 | bbids[i] = BBID_INVALID; |
---|
404 | } |
---|
405 | |
---|
406 | id = -1; |
---|
407 | for (i = 0; i < count; i ++) { |
---|
408 | if (markbbhead[i]) { |
---|
409 | id ++; |
---|
410 | } |
---|
411 | bbids[i] = id; |
---|
412 | TRACE("bbids[%d] = %d", i, id); |
---|
413 | } |
---|
414 | /* }}} */ |
---|
415 | #ifdef ZEND_ENGINE_2 |
---|
416 | /* {{{ fill op lines with catch id */ |
---|
417 | for (i = 0; i < count; i ++) { |
---|
418 | catchbbids[i] = BBID_INVALID; |
---|
419 | } |
---|
420 | |
---|
421 | for (i = 0; i < op_array->last_try_catch; i ++) { |
---|
422 | int j; |
---|
423 | zend_try_catch_element *e = &op_array->try_catch_array[i]; |
---|
424 | for (j = e->try_op; j < e->catch_op; j ++) { |
---|
425 | catchbbids[j] = bbids[e->catch_op]; |
---|
426 | } |
---|
427 | } |
---|
428 | #ifdef XCACHE_DEBUG |
---|
429 | for (i = 0; i < count; i ++) { |
---|
430 | TRACE("catchbbids[%d] = %d", i, catchbbids[i]); |
---|
431 | } |
---|
432 | #endif |
---|
433 | /* }}} */ |
---|
434 | #endif |
---|
435 | /* {{{ create basic blocks */ |
---|
436 | start = 0; |
---|
437 | id = 0; |
---|
438 | /* loop over to deal with the last block */ |
---|
439 | for (i = 1; i <= count; i ++) { |
---|
440 | if (i < count && id == bbids[i]) { |
---|
441 | continue; |
---|
442 | } |
---|
443 | |
---|
444 | opline = op_array->opcodes + start; |
---|
445 | pbb = bbs_new_bb_ex(bbs, opline, i - start); |
---|
446 | #ifdef ZEND_ENGINE_2 |
---|
447 | pbb->catch = catchbbids[start]; |
---|
448 | #endif |
---|
449 | |
---|
450 | /* last */ |
---|
451 | opline = pbb->opcodes + pbb->count - 1; |
---|
452 | if (op_get_flowinfo(&fi, opline) == SUCCESS) { |
---|
453 | if (fi.jmpout_op1 != XC_OPNUM_INVALID) { |
---|
454 | Z_OP(opline->op1).opline_num = bbids[fi.jmpout_op1]; |
---|
455 | assert(Z_OP(opline->op1).opline_num != BBID_INVALID); |
---|
456 | } |
---|
457 | if (fi.jmpout_op2 != XC_OPNUM_INVALID) { |
---|
458 | Z_OP(opline->op2).opline_num = bbids[fi.jmpout_op2]; |
---|
459 | assert(Z_OP(opline->op2).opline_num != BBID_INVALID); |
---|
460 | } |
---|
461 | if (fi.jmpout_ext != XC_OPNUM_INVALID) { |
---|
462 | opline->extended_value = bbids[fi.jmpout_ext]; |
---|
463 | assert(opline->extended_value != BBID_INVALID); |
---|
464 | } |
---|
465 | if (fi.fall && i + 1 < count) { |
---|
466 | pbb->fall = bbids[i + 1]; |
---|
467 | TRACE("fall %d", pbb->fall); |
---|
468 | assert(pbb->fall != BBID_INVALID); |
---|
469 | } |
---|
470 | } |
---|
471 | if (i >= count) { |
---|
472 | break; |
---|
473 | } |
---|
474 | start = i; |
---|
475 | id = bbids[i]; |
---|
476 | } |
---|
477 | /* }}} */ |
---|
478 | |
---|
479 | my_free_alloca(markbbhead, use_heap_markbbhead); |
---|
480 | #ifdef ZEND_ENGINE_2 |
---|
481 | my_free_alloca(catchbbids, use_heap_catchbbids); |
---|
482 | #endif |
---|
483 | my_free_alloca(bbids, use_heap_bbids); |
---|
484 | return SUCCESS; |
---|
485 | } |
---|
486 | /* }}} */ |
---|
487 | static void bbs_restore_opnum(bbs_t *bbs, zend_op_array *op_array) /* {{{ */ |
---|
488 | { |
---|
489 | int i; |
---|
490 | #ifdef ZEND_ENGINE_2 |
---|
491 | bbid_t lasttrybbid; |
---|
492 | bbid_t lastcatchbbid; |
---|
493 | #endif |
---|
494 | |
---|
495 | for (i = 0; i < bbs_count(bbs); i ++) { |
---|
496 | op_flowinfo_t fi; |
---|
497 | bb_t *bb = bbs_get(bbs, i); |
---|
498 | zend_op *last = bb->opcodes + bb->count - 1; |
---|
499 | |
---|
500 | if (op_get_flowinfo(&fi, last) == SUCCESS) { |
---|
501 | if (fi.jmpout_op1 != XC_OPNUM_INVALID) { |
---|
502 | Z_OP(last->op1).opline_num = bbs_get(bbs, fi.jmpout_op1)->opnum; |
---|
503 | assert(Z_OP(last->op1).opline_num != BBID_INVALID); |
---|
504 | } |
---|
505 | if (fi.jmpout_op2 != XC_OPNUM_INVALID) { |
---|
506 | Z_OP(last->op2).opline_num = bbs_get(bbs, fi.jmpout_op2)->opnum; |
---|
507 | assert(Z_OP(last->op2).opline_num != BBID_INVALID); |
---|
508 | } |
---|
509 | if (fi.jmpout_ext != XC_OPNUM_INVALID) { |
---|
510 | last->extended_value = bbs_get(bbs, fi.jmpout_ext)->opnum; |
---|
511 | assert(last->extended_value != BBID_INVALID); |
---|
512 | } |
---|
513 | } |
---|
514 | } |
---|
515 | |
---|
516 | #ifdef ZEND_ENGINE_2 |
---|
517 | lasttrybbid = BBID_INVALID; |
---|
518 | lastcatchbbid = BBID_INVALID; |
---|
519 | op_array->last_try_catch = 0; |
---|
520 | for (i = 0; i < bbs_count(bbs); i ++) { |
---|
521 | bb_t *bb = bbs_get(bbs, i); |
---|
522 | |
---|
523 | if (lastcatchbbid != bb->catch) { |
---|
524 | if (lasttrybbid != BBID_INVALID && lastcatchbbid != BBID_INVALID) { |
---|
525 | int try_catch_offset = op_array->last_try_catch ++; |
---|
526 | |
---|
527 | op_array->try_catch_array = erealloc(op_array->try_catch_array, sizeof(zend_try_catch_element) * op_array->last_try_catch); |
---|
528 | op_array->try_catch_array[try_catch_offset].try_op = bbs_get(bbs, lasttrybbid)->opnum; |
---|
529 | op_array->try_catch_array[try_catch_offset].catch_op = bbs_get(bbs, lastcatchbbid)->opnum; |
---|
530 | } |
---|
531 | lasttrybbid = i; |
---|
532 | lastcatchbbid = bb->catch; |
---|
533 | } |
---|
534 | } |
---|
535 | /* it is impossible to have last bb catched */ |
---|
536 | #endif |
---|
537 | } |
---|
538 | /* }}} */ |
---|
539 | |
---|
540 | /* |
---|
541 | * optimize |
---|
542 | */ |
---|
543 | static int xc_optimize_op_array(zend_op_array *op_array TSRMLS_DC) /* {{{ */ |
---|
544 | { |
---|
545 | bbs_t bbs; |
---|
546 | |
---|
547 | if (op_array->type != ZEND_USER_FUNCTION) { |
---|
548 | return 0; |
---|
549 | } |
---|
550 | |
---|
551 | #ifdef XCACHE_DEBUG |
---|
552 | # if 0 |
---|
553 | TRACE("optimize file: %s", op_array->filename); |
---|
554 | xc_dprint_zend_op_array(op_array, 0 TSRMLS_CC); |
---|
555 | # endif |
---|
556 | op_print(0, op_array->opcodes, op_array->opcodes + op_array->last); |
---|
557 | #endif |
---|
558 | |
---|
559 | if (op_array_convert_switch(op_array) == SUCCESS) { |
---|
560 | bbs_init(&bbs); |
---|
561 | if (bbs_build_from(&bbs, op_array, op_array->last) == SUCCESS) { |
---|
562 | int i; |
---|
563 | #ifdef XCACHE_DEBUG |
---|
564 | bbs_print(&bbs, op_array->opcodes); |
---|
565 | #endif |
---|
566 | /* TODO: calc opnum after basic block move */ |
---|
567 | for (i = 0; i < bbs_count(&bbs); i ++) { |
---|
568 | bb_t *bb = bbs_get(&bbs, i); |
---|
569 | bb->opnum = bb->opcodes - op_array->opcodes; |
---|
570 | } |
---|
571 | bbs_restore_opnum(&bbs, op_array); |
---|
572 | } |
---|
573 | bbs_destroy(&bbs); |
---|
574 | } |
---|
575 | |
---|
576 | #ifdef XCACHE_DEBUG |
---|
577 | # if 0 |
---|
578 | TRACE("%s", "after compiles"); |
---|
579 | xc_dprint_zend_op_array(op_array, 0 TSRMLS_CC); |
---|
580 | # endif |
---|
581 | op_print(0, op_array->opcodes, op_array->opcodes + op_array->last); |
---|
582 | #endif |
---|
583 | return 0; |
---|
584 | } |
---|
585 | /* }}} */ |
---|
586 | void xc_optimizer_op_array_handler(zend_op_array *op_array) /* {{{ */ |
---|
587 | { |
---|
588 | TSRMLS_FETCH(); |
---|
589 | if (XG(optimizer)) { |
---|
590 | xc_optimize_op_array(op_array TSRMLS_CC); |
---|
591 | } |
---|
592 | } |
---|
593 | /* }}} */ |
---|