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