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