PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - Zend - zend_opcode.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 388 459 84.5 %
Date: 2014-08-04 Functions: 28 34 82.4 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | Zend Engine                                                          |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1998-2014 Zend Technologies Ltd. (http://www.zend.com) |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 2.00 of the Zend 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.zend.com/license/2_00.txt.                                |
      11             :    | If you did not receive a copy of the Zend license and are unable to  |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@zend.com so we can mail you a copy immediately.              |
      14             :    +----------------------------------------------------------------------+
      15             :    | Authors: Andi Gutmans <andi@zend.com>                                |
      16             :    |          Zeev Suraski <zeev@zend.com>                                |
      17             :    +----------------------------------------------------------------------+
      18             : */
      19             : 
      20             : /* $Id$ */
      21             : 
      22             : #include <stdio.h>
      23             : 
      24             : #include "zend.h"
      25             : #include "zend_alloc.h"
      26             : #include "zend_compile.h"
      27             : #include "zend_extensions.h"
      28             : #include "zend_API.h"
      29             : 
      30             : #include "zend_vm.h"
      31             : 
      32       68969 : static void zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
      33             : {
      34       68969 :         if (extension->op_array_ctor) {
      35           0 :                 extension->op_array_ctor(op_array);
      36             :         }
      37       68969 : }
      38             : 
      39       66866 : static void zend_extension_op_array_dtor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
      40             : {
      41       66866 :         if (extension->op_array_dtor) {
      42           0 :                 extension->op_array_dtor(op_array);
      43             :         }
      44       66866 : }
      45             : 
      46       78374 : static void op_array_alloc_ops(zend_op_array *op_array, zend_uint size)
      47             : {
      48       78374 :         op_array->opcodes = erealloc(op_array->opcodes, size * sizeof(zend_op));
      49       78374 : }
      50             : 
      51       69398 : void init_op_array(zend_op_array *op_array, zend_uchar type, int initial_ops_size TSRMLS_DC)
      52             : {
      53       69398 :         op_array->type = type;
      54             : 
      55       69398 :         if (CG(interactive)) {
      56             :                 /* We must avoid a realloc() on the op_array in interactive mode, since pointers to constants
      57             :                  * will become invalid
      58             :                  */
      59           0 :                 initial_ops_size = INITIAL_INTERACTIVE_OP_ARRAY_SIZE;
      60             :         }
      61             : 
      62       69398 :         op_array->refcount = (zend_uint *) emalloc(sizeof(zend_uint));
      63       69398 :         *op_array->refcount = 1;
      64       69398 :         op_array->last = 0;
      65       69398 :         op_array->opcodes = NULL;
      66       69398 :         op_array_alloc_ops(op_array, initial_ops_size);
      67             : 
      68       69398 :         op_array->last_var = 0;
      69       69398 :         op_array->vars = NULL;
      70             : 
      71       69398 :         op_array->T = 0;
      72             : 
      73       69398 :         op_array->nested_calls = 0;
      74       69398 :         op_array->used_stack = 0;
      75             : 
      76       69398 :         op_array->function_name = NULL;
      77       69398 :         op_array->filename = zend_get_compiled_filename(TSRMLS_C);
      78       69398 :         op_array->doc_comment = NULL;
      79       69398 :         op_array->doc_comment_len = 0;
      80             : 
      81       69398 :         op_array->arg_info = NULL;
      82       69398 :         op_array->num_args = 0;
      83       69398 :         op_array->required_num_args = 0;
      84             : 
      85       69398 :         op_array->scope = NULL;
      86             : 
      87       69398 :         op_array->brk_cont_array = NULL;
      88       69398 :         op_array->try_catch_array = NULL;
      89       69398 :         op_array->last_brk_cont = 0;
      90             : 
      91       69398 :         op_array->static_variables = NULL;
      92       69398 :         op_array->last_try_catch = 0;
      93       69398 :         op_array->has_finally_block = 0;
      94             : 
      95       69398 :         op_array->this_var = -1;
      96             : 
      97       69398 :         op_array->fn_flags = CG(interactive)?ZEND_ACC_INTERACTIVE:0;
      98             : 
      99       69398 :         op_array->early_binding = -1;
     100             : 
     101       69398 :         op_array->last_literal = 0;
     102       69398 :         op_array->literals = NULL;
     103             : 
     104       69398 :         op_array->run_time_cache = NULL;
     105       69398 :         op_array->last_cache_slot = 0;
     106             : 
     107       69398 :         memset(op_array->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
     108             : 
     109       69398 :         zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_ctor_handler, op_array TSRMLS_CC);
     110       69398 : }
     111             : 
     112   110107588 : ZEND_API void destroy_zend_function(zend_function *function TSRMLS_DC)
     113             : {
     114   110107588 :         switch (function->type) {
     115             :                 case ZEND_USER_FUNCTION:
     116       56816 :                         destroy_op_array((zend_op_array *) function TSRMLS_CC);
     117             :                         break;
     118             :                 case ZEND_INTERNAL_FUNCTION:
     119             :                         /* do nothing */
     120             :                         break;
     121             :         }
     122   110107588 : }
     123             : 
     124   110107574 : ZEND_API void zend_function_dtor(zend_function *function)
     125             : {
     126             :         TSRMLS_FETCH();
     127             : 
     128   110107574 :         destroy_zend_function(function TSRMLS_CC);
     129   110107574 : }
     130             : 
     131       33597 : static void zend_cleanup_op_array_data(zend_op_array *op_array)
     132             : {
     133       33597 :         if (op_array->static_variables) {
     134        2126 :                 zend_hash_clean(op_array->static_variables);
     135             :         }
     136       33597 : }
     137             : 
     138       44426 : ZEND_API int zend_cleanup_function_data(zend_function *function TSRMLS_DC)
     139             : {
     140       44426 :         if (function->type == ZEND_USER_FUNCTION) {
     141       23169 :                 zend_cleanup_op_array_data((zend_op_array *) function);
     142       23169 :                 return ZEND_HASH_APPLY_KEEP;
     143             :         } else {
     144       21257 :                 return ZEND_HASH_APPLY_STOP;
     145             :         }
     146             : }
     147             : 
     148       10449 : ZEND_API int zend_cleanup_function_data_full(zend_function *function TSRMLS_DC)
     149             : {
     150       10449 :         if (function->type == ZEND_USER_FUNCTION) {
     151       10428 :                 zend_cleanup_op_array_data((zend_op_array *) function);
     152             :         }
     153       10449 :         return 0;
     154             : }
     155             : 
     156        9171 : static inline void cleanup_user_class_data(zend_class_entry *ce TSRMLS_DC)
     157             : {
     158             :         /* Clean all parts that can contain run-time data */
     159             :         /* Note that only run-time accessed data need to be cleaned up, pre-defined data can
     160             :            not contain objects and thus are not probelmatic */
     161        9171 :         if (ce->ce_flags & ZEND_HAS_STATIC_IN_METHODS) {
     162        1489 :                 zend_hash_apply(&ce->function_table, (apply_func_t) zend_cleanup_function_data_full TSRMLS_CC);
     163             :         }
     164        9171 :         if (ce->static_members_table) {
     165             :                 int i;
     166             : 
     167         858 :                 for (i = 0; i < ce->default_static_members_count; i++) {
     168         535 :                         if (ce->static_members_table[i]) {
     169         535 :                                 zval *p = ce->static_members_table[i];
     170         535 :                                 ce->static_members_table[i] = NULL;
     171         535 :                                 zval_ptr_dtor(&p);
     172             :                         }
     173             :                 }
     174         323 :                 ce->static_members_table = NULL;
     175             :         }
     176        9170 : }
     177             : 
     178           0 : static inline void cleanup_internal_class_data(zend_class_entry *ce TSRMLS_DC)
     179             : {
     180           0 :         if (CE_STATIC_MEMBERS(ce)) {
     181             :                 int i;
     182             :                 
     183           0 :                 for (i = 0; i < ce->default_static_members_count; i++) {
     184           0 :                         zval_ptr_dtor(&CE_STATIC_MEMBERS(ce)[i]);
     185             :                 }
     186           0 :                 efree(CE_STATIC_MEMBERS(ce));
     187             : #ifdef ZTS
     188             :                 CG(static_members_table)[(zend_intptr_t)(ce->static_members_table)] = NULL;
     189             : #else
     190           0 :                 ce->static_members_table = NULL;
     191             : #endif
     192             :         }
     193           0 : }
     194             : 
     195           0 : ZEND_API void zend_cleanup_internal_class_data(zend_class_entry *ce TSRMLS_DC)
     196             : {
     197           0 :         cleanup_internal_class_data(ce TSRMLS_CC);
     198           0 : }
     199             : 
     200       30427 : ZEND_API int zend_cleanup_user_class_data(zend_class_entry **pce TSRMLS_DC)
     201             : {
     202       30427 :         if ((*pce)->type == ZEND_USER_CLASS) {
     203        9171 :                 cleanup_user_class_data(*pce TSRMLS_CC);
     204        9170 :                 return ZEND_HASH_APPLY_KEEP;
     205             :         } else {
     206       21256 :                 return ZEND_HASH_APPLY_STOP;
     207             :         }
     208             : }
     209             : 
     210           0 : ZEND_API int zend_cleanup_class_data(zend_class_entry **pce TSRMLS_DC)
     211             : {
     212           0 :         if ((*pce)->type == ZEND_USER_CLASS) {
     213           0 :                 cleanup_user_class_data(*pce TSRMLS_CC);
     214             :         } else {
     215           0 :                 cleanup_internal_class_data(*pce TSRMLS_CC);
     216             :         }
     217           0 :         return 0;
     218             : }
     219             : 
     220        7462 : void _destroy_zend_class_traits_info(zend_class_entry *ce)
     221             : {
     222        7462 :         if (ce->num_traits > 0 && ce->traits) {
     223         168 :                 efree(ce->traits);
     224             :         }
     225             :         
     226        7462 :         if (ce->trait_aliases) {
     227          44 :                 size_t i = 0;
     228         144 :                 while (ce->trait_aliases[i]) {
     229          56 :                         if (ce->trait_aliases[i]->trait_method) {
     230          56 :                                 if (ce->trait_aliases[i]->trait_method->method_name) {
     231          56 :                                         efree((char*)ce->trait_aliases[i]->trait_method->method_name);
     232             :                                 }
     233          56 :                                 if (ce->trait_aliases[i]->trait_method->class_name) {
     234          21 :                                         efree((char*)ce->trait_aliases[i]->trait_method->class_name);
     235             :                                 }
     236          56 :                                 efree(ce->trait_aliases[i]->trait_method);
     237             :                         }
     238             :                         
     239          56 :                         if (ce->trait_aliases[i]->alias) {
     240          48 :                                 efree((char*)ce->trait_aliases[i]->alias);
     241             :                         }
     242             :                         
     243          56 :                         efree(ce->trait_aliases[i]);
     244          56 :                         i++;
     245             :                 }
     246             :                 
     247          44 :                 efree(ce->trait_aliases);
     248             :         }
     249             : 
     250        7462 :         if (ce->trait_precedences) {
     251          13 :                 size_t i = 0;
     252             :                 
     253          43 :                 while (ce->trait_precedences[i]) {
     254          17 :                         efree((char*)ce->trait_precedences[i]->trait_method->method_name);
     255          17 :                         efree((char*)ce->trait_precedences[i]->trait_method->class_name);
     256          17 :                         efree(ce->trait_precedences[i]->trait_method);
     257             : 
     258          17 :                         if (ce->trait_precedences[i]->exclude_from_classes) {
     259          17 :                                 efree(ce->trait_precedences[i]->exclude_from_classes);
     260             :                         }
     261             : 
     262          17 :                         efree(ce->trait_precedences[i]);
     263          17 :                         i++;
     264             :                 }
     265          13 :                 efree(ce->trait_precedences);
     266             :         }
     267        7462 : }
     268             : 
     269     3848458 : ZEND_API void destroy_zend_class(zend_class_entry **pce)
     270             : {
     271     3848458 :         zend_class_entry *ce = *pce;
     272             :         
     273     3848458 :         if (--ce->refcount > 0) {
     274        7356 :                 return;
     275             :         }
     276     3841102 :         switch (ce->type) {
     277             :                 case ZEND_USER_CLASS:
     278        7462 :                         if (ce->default_properties_table) {
     279             :                                 int i;
     280             : 
     281       22340 :                                 for (i = 0; i < ce->default_properties_count; i++) {
     282       20119 :                                         if (ce->default_properties_table[i]) {
     283       20056 :                                                 zval_ptr_dtor(&ce->default_properties_table[i]);
     284             :                                     }
     285             :                                 }
     286        2221 :                                 efree(ce->default_properties_table);
     287             :                         }
     288        7462 :                         if (ce->default_static_members_table) {
     289             :                                 int i;
     290             : 
     291         859 :                                 for (i = 0; i < ce->default_static_members_count; i++) {
     292         535 :                                         if (ce->default_static_members_table[i]) {
     293           0 :                                                 zval_ptr_dtor(&ce->default_static_members_table[i]);
     294             :                                         }
     295             :                                 }
     296         324 :                                 efree(ce->default_static_members_table);
     297             :                         }
     298        7462 :                         zend_hash_destroy(&ce->properties_info);
     299        7462 :                         str_efree(ce->name);
     300        7462 :                         zend_hash_destroy(&ce->function_table);
     301        7462 :                         zend_hash_destroy(&ce->constants_table);
     302        7462 :                         if (ce->num_interfaces > 0 && ce->interfaces) {
     303         618 :                                 efree(ce->interfaces);
     304             :                         }
     305        7462 :                         if (ce->info.user.doc_comment) {
     306          64 :                                 efree((char*)ce->info.user.doc_comment);
     307             :                         }
     308             :                         
     309        7462 :                         _destroy_zend_class_traits_info(ce);
     310             :                         
     311        7462 :                         efree(ce);
     312        7462 :                         break;
     313             :                 case ZEND_INTERNAL_CLASS:
     314     3833640 :                         if (ce->default_properties_table) {
     315             :                                 int i;
     316             : 
     317     5750460 :                                 for (i = 0; i < ce->default_properties_count; i++) {
     318     4834646 :                                         if (ce->default_properties_table[i]) {
     319     4834646 :                                                 zval_internal_ptr_dtor(&ce->default_properties_table[i]);
     320             :                                         }
     321             :                                 }
     322      915814 :                                 free(ce->default_properties_table);
     323             :                         }
     324     3833640 :                         if (ce->default_static_members_table) {
     325             :                                 int i;
     326             : 
     327           0 :                                 for (i = 0; i < ce->default_static_members_count; i++) {
     328           0 :                                         zval_internal_ptr_dtor(&ce->default_static_members_table[i]);
     329             :                                 }
     330           0 :                                 free(ce->default_static_members_table);
     331             :                         }
     332     3833640 :                         zend_hash_destroy(&ce->properties_info);
     333     3833640 :                         str_free(ce->name);
     334     3833640 :                         zend_hash_destroy(&ce->function_table);
     335     3833640 :                         zend_hash_destroy(&ce->constants_table);
     336     3833640 :                         if (ce->num_interfaces > 0) {
     337     1469562 :                                 free(ce->interfaces);
     338             :                         }
     339     3833640 :                         free(ce);
     340             :                         break;
     341             :         }
     342             : }
     343             : 
     344           0 : void zend_class_add_ref(zend_class_entry **ce)
     345             : {
     346           0 :         (*ce)->refcount++;
     347           0 : }
     348             : 
     349       87453 : ZEND_API void destroy_op_array(zend_op_array *op_array TSRMLS_DC)
     350             : {
     351       87453 :         zend_literal *literal = op_array->literals;
     352             :         zend_literal *end;
     353             :         zend_uint i;
     354             : 
     355       87453 :         if (op_array->static_variables) {
     356        2239 :                 zend_hash_destroy(op_array->static_variables);
     357        2239 :                 FREE_HASHTABLE(op_array->static_variables);
     358             :         }
     359             : 
     360       87453 :         if (op_array->run_time_cache) {
     361       35806 :                 efree(op_array->run_time_cache);
     362             :         }
     363             : 
     364       87453 :         if (--(*op_array->refcount)>0) {
     365       20126 :                 return;
     366             :         }
     367             : 
     368       67327 :         efree(op_array->refcount);
     369             : 
     370       67327 :         if (op_array->vars) {
     371       50829 :                 i = op_array->last_var;
     372      328039 :                 while (i > 0) {
     373      226381 :                         i--;
     374      226381 :                         str_efree(op_array->vars[i].name);
     375             :                 }
     376       50829 :                 efree(op_array->vars);
     377             :         }
     378             : 
     379       67327 :         if (literal) {
     380       67310 :                 end = literal + op_array->last_literal;
     381     1449799 :                 while (literal < end) {
     382     1315179 :                         zval_dtor(&literal->constant);
     383     1315179 :                         literal++;
     384             :                 }
     385       67310 :                 efree(op_array->literals);
     386             :         }
     387       67327 :         efree(op_array->opcodes);
     388             : 
     389       67327 :         if (op_array->function_name) {
     390       37368 :                 efree((char*)op_array->function_name);
     391             :         }
     392       67327 :         if (op_array->doc_comment) {
     393        2384 :                 efree((char*)op_array->doc_comment);
     394             :         }
     395       67327 :         if (op_array->brk_cont_array) {
     396       11722 :                 efree(op_array->brk_cont_array);
     397             :         }
     398       67327 :         if (op_array->try_catch_array) {
     399        1637 :                 efree(op_array->try_catch_array);
     400             :         }
     401       67327 :         if (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) {
     402       67286 :                 zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_dtor_handler, op_array TSRMLS_CC);
     403             :         }
     404       67327 :         if (op_array->arg_info) {
     405      114607 :                 for (i=0; i<op_array->num_args; i++) {
     406       86497 :                         str_efree(op_array->arg_info[i].name);
     407       86497 :                         if (op_array->arg_info[i].class_name) {
     408         177 :                                 str_efree(op_array->arg_info[i].class_name);
     409             :                         }
     410             :                 }
     411       28110 :                 efree(op_array->arg_info);
     412             :         }
     413             : }
     414             : 
     415     2595428 : void init_op(zend_op *op TSRMLS_DC)
     416             : {
     417     2595428 :         memset(op, 0, sizeof(zend_op));
     418     2595428 :         op->lineno = CG(zend_lineno);
     419     2595428 :         SET_UNUSED(op->result);
     420     2595428 : }
     421             : 
     422     2394500 : zend_op *get_next_op(zend_op_array *op_array TSRMLS_DC)
     423             : {
     424     2394500 :         zend_uint next_op_num = op_array->last++;
     425             :         zend_op *next_op;
     426             : 
     427     2394500 :         if (next_op_num >= CG(context).opcodes_size) {
     428        8976 :                 if (op_array->fn_flags & ZEND_ACC_INTERACTIVE) {
     429             :                         /* we messed up */
     430           0 :                         zend_printf("Ran out of opcode space!\n"
     431             :                                                 "You should probably consider writing this huge script into a file!\n");
     432           0 :                         zend_bailout();
     433             :                 }
     434        8976 :                 CG(context).opcodes_size *= 4;
     435        8976 :                 op_array_alloc_ops(op_array, CG(context).opcodes_size);
     436             :         }
     437             :         
     438     2394500 :         next_op = &(op_array->opcodes[next_op_num]);
     439             :         
     440     2394500 :         init_op(next_op TSRMLS_CC);
     441             : 
     442     2394500 :         return next_op;
     443             : }
     444             : 
     445     1004287 : int get_next_op_number(zend_op_array *op_array)
     446             : {
     447     1004287 :         return op_array->last;
     448             : }
     449             : 
     450       16107 : zend_brk_cont_element *get_next_brk_cont_element(zend_op_array *op_array)
     451             : {
     452       16107 :         op_array->last_brk_cont++;
     453       16107 :         op_array->brk_cont_array = erealloc(op_array->brk_cont_array, sizeof(zend_brk_cont_element)*op_array->last_brk_cont);
     454       16107 :         return &op_array->brk_cont_array[op_array->last_brk_cont-1];
     455             : }
     456             : 
     457           0 : static void zend_update_extended_info(zend_op_array *op_array TSRMLS_DC)
     458             : {
     459           0 :         zend_op *opline = op_array->opcodes, *end=opline+op_array->last;
     460             : 
     461           0 :         while (opline<end) {
     462           0 :                 if (opline->opcode == ZEND_EXT_STMT) {
     463           0 :                         if (opline+1<end) {
     464           0 :                                 if ((opline+1)->opcode == ZEND_EXT_STMT) {
     465           0 :                                         opline->opcode = ZEND_NOP;
     466           0 :                                         opline++;
     467           0 :                                         continue;
     468             :                                 }
     469           0 :                                 if (opline+1<end) {
     470           0 :                                         opline->lineno = (opline+1)->lineno;
     471             :                                 }
     472             :                         } else {
     473           0 :                                 opline->opcode = ZEND_NOP;
     474             :                         }
     475             :                 }
     476           0 :                 opline++;
     477             :         }
     478           0 : }
     479             : 
     480       68537 : static void zend_extension_op_array_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
     481             : {
     482       68537 :         if (extension->op_array_handler) {
     483           0 :                 extension->op_array_handler(op_array);
     484             :         }
     485       68537 : }
     486             : 
     487         141 : static void zend_check_finally_breakout(zend_op_array *op_array, zend_uint op_num, zend_uint dst_num TSRMLS_DC)
     488             : {
     489             :         zend_uint i;
     490             : 
     491         534 :         for (i = 0; i < op_array->last_try_catch; i++) {
     492        1158 :                 if ((op_num < op_array->try_catch_array[i].finally_op ||
     493         176 :                                         op_num >= op_array->try_catch_array[i].finally_end)
     494         364 :                                 && (dst_num >= op_array->try_catch_array[i].finally_op &&
     495         218 :                                          dst_num <= op_array->try_catch_array[i].finally_end)) {
     496           3 :                         CG(in_compilation) = 1;
     497           3 :                         CG(active_op_array) = op_array;
     498           3 :                         CG(zend_lineno) = op_array->opcodes[op_num].lineno;
     499           3 :                         zend_error_noreturn(E_COMPILE_ERROR, "jump into a finally block is disallowed");
     500        1038 :                 } else if ((op_num >= op_array->try_catch_array[i].finally_op 
     501         572 :                                         && op_num <= op_array->try_catch_array[i].finally_end)
     502          36 :                                 && (dst_num > op_array->try_catch_array[i].finally_end 
     503          69 :                                         || dst_num < op_array->try_catch_array[i].finally_op)) {
     504           4 :                         CG(in_compilation) = 1;
     505           4 :                         CG(active_op_array) = op_array;
     506           4 :                         CG(zend_lineno) = op_array->opcodes[op_num].lineno;
     507           4 :                         zend_error_noreturn(E_COMPILE_ERROR, "jump out of a finally block is disallowed");
     508             :                 }
     509             :         } 
     510         134 : }
     511             : 
     512          21 : static void zend_adjust_fast_call(zend_op_array *op_array, zend_uint fast_call, zend_uint start, zend_uint end TSRMLS_DC)
     513             : {
     514             :         int i;
     515          21 :         zend_uint op_num = 0;
     516             : 
     517          78 :         for (i = 0; i < op_array->last_try_catch; i++) {
     518         125 :                 if (op_array->try_catch_array[i].finally_op > start 
     519          68 :                                 && op_array->try_catch_array[i].finally_end < end) {
     520           0 :                         op_num = op_array->try_catch_array[i].finally_op;
     521           0 :                         start = op_array->try_catch_array[i].finally_end;
     522             :                 }
     523             :         }
     524             : 
     525          21 :         if (op_num) {
     526             :                 /* Must be ZEND_FAST_CALL */
     527             :                 ZEND_ASSERT(op_array->opcodes[op_num - 2].opcode == ZEND_FAST_CALL);
     528           0 :                 op_array->opcodes[op_num - 2].extended_value = ZEND_FAST_CALL_FROM_FINALLY;
     529           0 :                 op_array->opcodes[op_num - 2].op2.opline_num = fast_call;
     530             :         }
     531          21 : }
     532             : 
     533          87 : static void zend_resolve_fast_call(zend_op_array *op_array, zend_uint fast_call, zend_uint op_num TSRMLS_DC)
     534             : {
     535             :         int i;
     536          87 :         zend_uint finally_op_num = 0;
     537             : 
     538         340 :         for (i = 0; i < op_array->last_try_catch; i++) {
     539         597 :                 if (op_num >= op_array->try_catch_array[i].finally_op
     540         344 :                                 && op_num < op_array->try_catch_array[i].finally_end) {
     541          16 :                         finally_op_num = op_array->try_catch_array[i].finally_op;
     542             :                 }
     543             :         }
     544             : 
     545          87 :         if (finally_op_num) {
     546             :                 /* Must be ZEND_FAST_CALL */
     547             :                 ZEND_ASSERT(op_array->opcodes[finally_op_num - 2].opcode == ZEND_FAST_CALL);
     548           9 :                 if (op_array->opcodes[fast_call].extended_value == 0) {
     549           9 :                         op_array->opcodes[fast_call].extended_value = ZEND_FAST_CALL_FROM_FINALLY;
     550           9 :                         op_array->opcodes[fast_call].op2.opline_num = finally_op_num - 2;
     551             :                 }
     552             :         } 
     553          87 : }
     554             : 
     555         206 : static void zend_resolve_finally_call(zend_op_array *op_array, zend_uint op_num, zend_uint dst_num TSRMLS_DC)
     556             : {
     557             :         zend_uint start_op;
     558             :         zend_op *opline;
     559         206 :         zend_uint i = op_array->last_try_catch;
     560             : 
     561         206 :         if (dst_num != (zend_uint)-1) {
     562         141 :                 zend_check_finally_breakout(op_array, op_num, dst_num TSRMLS_CC);
     563             :         }
     564             : 
     565             :         /* the backward order is mater */
     566         858 :         while (i > 0) {
     567         481 :                 i--;
     568        1524 :                 if (op_array->try_catch_array[i].finally_op &&
     569         464 :                     op_num >= op_array->try_catch_array[i].try_op &&
     570         393 :                     op_num < op_array->try_catch_array[i].finally_op - 1 &&
     571          94 :                     (dst_num < op_array->try_catch_array[i].try_op ||
     572          92 :                      dst_num > op_array->try_catch_array[i].finally_end)) {
     573             :                         /* we have a jump out of try block that needs executing finally */
     574             : 
     575             :                         /* generate a FAST_CALL to finally block */
     576          21 :                     start_op = get_next_op_number(op_array);
     577             : 
     578          21 :                         opline = get_next_op(op_array TSRMLS_CC);
     579          21 :                         opline->opcode = ZEND_FAST_CALL;
     580          21 :                         SET_UNUSED(opline->op1);
     581          21 :                         SET_UNUSED(opline->op2);
     582          42 :                         zend_adjust_fast_call(op_array, start_op,
     583          21 :                                         op_array->try_catch_array[i].finally_op,
     584          21 :                                         op_array->try_catch_array[i].finally_end TSRMLS_CC);
     585          21 :                         if (op_array->try_catch_array[i].catch_op) {
     586           7 :                                 opline->extended_value = ZEND_FAST_CALL_FROM_CATCH;
     587           7 :                                 opline->op2.opline_num = op_array->try_catch_array[i].catch_op;
     588           7 :                                 opline->op1.opline_num = get_next_op_number(op_array);
     589             :                                 /* generate a FAST_CALL to hole CALL_FROM_FINALLY */
     590           7 :                                 opline = get_next_op(op_array TSRMLS_CC);
     591           7 :                                 opline->opcode = ZEND_FAST_CALL;
     592           7 :                                 SET_UNUSED(opline->op1);
     593           7 :                                 SET_UNUSED(opline->op2);
     594           7 :                                 zend_resolve_fast_call(op_array, start_op + 1, op_array->try_catch_array[i].finally_op - 2 TSRMLS_CC);
     595             :                         } else {
     596          14 :                                 zend_resolve_fast_call(op_array, start_op, op_array->try_catch_array[i].finally_op - 2 TSRMLS_CC);
     597             :                         }
     598          21 :                         opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
     599             : 
     600             :                         /* generate a sequence of FAST_CALL to upward finally block */
     601          66 :                         while (i > 0) {
     602          24 :                                 i--;
     603          79 :                                 if (op_array->try_catch_array[i].finally_op &&
     604          23 :                                         op_num >= op_array->try_catch_array[i].try_op &&
     605          23 :                                         op_num < op_array->try_catch_array[i].finally_op - 1 &&
     606           5 :                                         (dst_num < op_array->try_catch_array[i].try_op ||
     607           4 :                                          dst_num > op_array->try_catch_array[i].finally_end)) {
     608             : 
     609           5 :                                         opline = get_next_op(op_array TSRMLS_CC);
     610           5 :                                         opline->opcode = ZEND_FAST_CALL;
     611           5 :                                         SET_UNUSED(opline->op1);
     612           5 :                                         SET_UNUSED(opline->op2);
     613           5 :                                         opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
     614             :                                 }
     615             :                         }
     616             : 
     617             :                         /* Finish the sequence with original opcode */
     618          21 :                         opline = get_next_op(op_array TSRMLS_CC);
     619          21 :                         *opline = op_array->opcodes[op_num];
     620             : 
     621             :                         /* Replace original opcode with jump to this sequence */
     622          21 :                         opline = op_array->opcodes + op_num;
     623          21 :                         opline->opcode = ZEND_JMP;
     624          21 :                         SET_UNUSED(opline->op1);
     625          21 :                         SET_UNUSED(opline->op2);
     626          21 :                         opline->op1.opline_num = start_op;
     627             : 
     628          21 :                         break;
     629             :                 }
     630             :         }       
     631         199 : }
     632             : 
     633          62 : static void zend_resolve_finally_ret(zend_op_array *op_array, zend_uint op_num TSRMLS_DC)
     634             : {
     635             :         int i;
     636          62 :         zend_uint catch_op_num = 0, finally_op_num = 0;
     637             : 
     638         224 :         for (i = 0; i < op_array->last_try_catch; i++) {
     639         170 :                 if (op_array->try_catch_array[i].try_op > op_num) {
     640           8 :                         break;
     641             :                 }
     642         162 :                 if (op_num < op_array->try_catch_array[i].finally_op) {
     643          23 :                         finally_op_num = op_array->try_catch_array[i].finally_op;
     644             :                 }
     645         162 :                 if (op_num < op_array->try_catch_array[i].catch_op) {
     646          16 :                         catch_op_num = op_array->try_catch_array[i].catch_op;
     647             :                 }
     648             :         }
     649             : 
     650          70 :         if (finally_op_num && (!catch_op_num || catch_op_num >= finally_op_num)) {
     651             :                 /* in case of unhandled exception return to upward finally block */
     652           8 :                 op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_FINALLY;
     653           8 :                 op_array->opcodes[op_num].op2.opline_num = finally_op_num;
     654          54 :         } else if (catch_op_num) {
     655             :                 /* in case of unhandled exception return to upward catch block */
     656          11 :                 op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_CATCH;
     657          11 :                 op_array->opcodes[op_num].op2.opline_num = catch_op_num;
     658             :         }
     659          62 : }
     660             : 
     661          42 : static void zend_resolve_finally_calls(zend_op_array *op_array TSRMLS_DC)
     662             : {
     663             :         zend_uint i, j;
     664             :         zend_op *opline;
     665             : 
     666         767 :         for (i = 0, j = op_array->last; i < j; i++) {
     667         732 :                 opline = op_array->opcodes + i;
     668         732 :                 switch (opline->opcode) {
     669             :                         case ZEND_RETURN:
     670             :                         case ZEND_RETURN_BY_REF:
     671             :                         case ZEND_GENERATOR_RETURN:
     672          64 :                                 zend_resolve_finally_call(op_array, i, (zend_uint)-1 TSRMLS_CC);
     673          64 :                                 break;
     674             :                         case ZEND_BRK:
     675             :                         case ZEND_CONT:
     676             :                         {
     677             :                                 int nest_levels, array_offset;
     678             :                                 zend_brk_cont_element *jmp_to;
     679             : 
     680           9 :                                 nest_levels = Z_LVAL(op_array->literals[opline->op2.constant].constant);
     681           9 :                                 if ((array_offset = opline->op1.opline_num) != -1) {
     682             :                                         do {
     683           8 :                                                 jmp_to = &op_array->brk_cont_array[array_offset];
     684           8 :                                                 if (nest_levels > 1) {
     685           0 :                                                         array_offset = jmp_to->parent;
     686             :                                                 }
     687           8 :                                         } while (--nest_levels > 0);
     688           8 :                                         zend_resolve_finally_call(op_array, i, opline->opcode == ZEND_BRK ? jmp_to->brk : jmp_to->cont TSRMLS_CC);
     689           6 :                                         break;
     690             :                                 }
     691             :                         }
     692             :                         case ZEND_GOTO:
     693           9 :                                 if (Z_TYPE(op_array->literals[opline->op2.constant].constant) != IS_LONG) {
     694           8 :                                         zend_uint num = opline->op2.constant;
     695           8 :                                         opline->op2.zv = &op_array->literals[opline->op2.constant].constant;
     696           8 :                                         zend_resolve_goto_label(op_array, opline, 1 TSRMLS_CC);
     697           8 :                                         opline->op2.constant = num;                                  
     698             :                                 }
     699             :                                 /* break omitted intentionally */
     700             :                         case ZEND_JMP:
     701         134 :                                 zend_resolve_finally_call(op_array, i, opline->op1.opline_num TSRMLS_CC);
     702         129 :                                 break;
     703             :                         case ZEND_FAST_CALL:
     704          66 :                                 zend_resolve_fast_call(op_array, i, i TSRMLS_CC);
     705          66 :                                 break;
     706             :                         case ZEND_FAST_RET:
     707          62 :                                 zend_resolve_finally_ret(op_array, i TSRMLS_CC);
     708             :                                 break;
     709             :                         default:
     710             :                                 break;
     711             :                 }
     712             :         }
     713          35 : }
     714             : 
     715       69156 : ZEND_API int pass_two(zend_op_array *op_array TSRMLS_DC)
     716             : {
     717             :         zend_op *opline, *end;
     718             : 
     719       69156 :         if (op_array->type!=ZEND_USER_FUNCTION && op_array->type!=ZEND_EVAL_CODE) {
     720           0 :                 return 0;
     721             :         }
     722       69156 :         if (op_array->has_finally_block) {
     723          42 :                 zend_resolve_finally_calls(op_array TSRMLS_CC);
     724             :         }
     725       69149 :         if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) {
     726           0 :                 zend_update_extended_info(op_array TSRMLS_CC);
     727             :         }
     728       69149 :         if (CG(compiler_options) & ZEND_COMPILE_HANDLE_OP_ARRAY) {
     729       68933 :                 zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_handler, op_array TSRMLS_CC);
     730             :         }
     731             : 
     732       69149 :         if (!(op_array->fn_flags & ZEND_ACC_INTERACTIVE) && CG(context).vars_size != op_array->last_var) {
     733       51378 :                 op_array->vars = (zend_compiled_variable *) erealloc(op_array->vars, sizeof(zend_compiled_variable)*op_array->last_var);
     734       51378 :                 CG(context).vars_size = op_array->last_var;
     735             :         }
     736       69149 :         if (!(op_array->fn_flags & ZEND_ACC_INTERACTIVE) && CG(context).opcodes_size != op_array->last) {
     737       68751 :                 op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last);
     738       68751 :                 CG(context).opcodes_size = op_array->last;
     739             :         }
     740       69149 :         if (!(op_array->fn_flags & ZEND_ACC_INTERACTIVE) && CG(context).literals_size != op_array->last_literal) {
     741       67743 :                 op_array->literals = (zend_literal*)erealloc(op_array->literals, sizeof(zend_literal) * op_array->last_literal);
     742       67743 :                 CG(context).literals_size = op_array->last_literal;
     743             :         }
     744             : 
     745       69149 :         opline = op_array->opcodes;
     746       69149 :         end = opline + op_array->last;
     747     2530065 :         while (opline < end) {
     748     2391772 :                 if (opline->op1_type == IS_CONST) {
     749      803125 :                         opline->op1.zv = &op_array->literals[opline->op1.constant].constant;
     750             :                 }
     751     2391772 :                 if (opline->op2_type == IS_CONST) {
     752      382936 :                         opline->op2.zv = &op_array->literals[opline->op2.constant].constant;
     753             :                 }
     754     2391772 :                 switch (opline->opcode) {
     755             :                         case ZEND_GOTO:
     756          17 :                                 if (Z_TYPE_P(opline->op2.zv) != IS_LONG) {
     757          15 :                                         zend_resolve_goto_label(op_array, opline, 1 TSRMLS_CC);
     758             :                                 }
     759             :                                 /* break omitted intentionally */
     760             :                         case ZEND_JMP:
     761             :                         case ZEND_FAST_CALL:
     762      128376 :                                 opline->op1.jmp_addr = &op_array->opcodes[opline->op1.opline_num];
     763      128376 :                                 break;
     764             :                         case ZEND_JMPZ:
     765             :                         case ZEND_JMPNZ:
     766             :                         case ZEND_JMPZ_EX:
     767             :                         case ZEND_JMPNZ_EX:
     768             :                         case ZEND_JMP_SET:
     769             :                         case ZEND_JMP_SET_VAR:
     770      123198 :                                 opline->op2.jmp_addr = &op_array->opcodes[opline->op2.opline_num];
     771      123198 :                                 break;
     772             :                         case ZEND_RETURN:
     773             :                         case ZEND_RETURN_BY_REF:
     774      114048 :                                 if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
     775          83 :                                         if (opline->op1_type != IS_CONST || Z_TYPE_P(opline->op1.zv) != IS_NULL) {
     776           2 :                                                 CG(zend_lineno) = opline->lineno;
     777           2 :                                                 zend_error_noreturn(E_COMPILE_ERROR, "Generators cannot return values using \"return\"");
     778             :                                         }
     779             : 
     780          81 :                                         opline->opcode = ZEND_GENERATOR_RETURN;
     781             :                                 }
     782             :                                 break;
     783             :                 }
     784     2391767 :                 ZEND_VM_SET_OPCODE_HANDLER(opline);
     785     2391767 :                 opline++;
     786             :         }
     787             : 
     788       69144 :         op_array->fn_flags |= ZEND_ACC_DONE_PASS_TWO;
     789       69144 :         return 0;
     790             : }
     791             : 
     792           0 : int print_class(zend_class_entry *class_entry TSRMLS_DC)
     793             : {
     794           0 :         printf("Class %s:\n", class_entry->name);
     795           0 :         zend_hash_apply(&class_entry->function_table, (apply_func_t) pass_two TSRMLS_CC);
     796           0 :         printf("End of class %s.\n\n", class_entry->name);
     797           0 :         return 0;
     798             : }
     799             : 
     800         173 : ZEND_API unary_op_type get_unary_op(int opcode)
     801             : {
     802         173 :         switch (opcode) {
     803             :                 case ZEND_BW_NOT:
     804           0 :                         return (unary_op_type) bitwise_not_function;
     805             :                         break;
     806             :                 case ZEND_BOOL_NOT:
     807         173 :                         return (unary_op_type) boolean_not_function;
     808             :                         break;
     809             :                 default:
     810           0 :                         return (unary_op_type) NULL;
     811             :                         break;
     812             :         }
     813             : }
     814             : 
     815          22 : ZEND_API binary_op_type get_binary_op(int opcode)
     816             : {
     817          22 :         switch (opcode) {
     818             :                 case ZEND_ADD:
     819             :                 case ZEND_ASSIGN_ADD:
     820           0 :                         return (binary_op_type) add_function;
     821             :                         break;
     822             :                 case ZEND_SUB:
     823             :                 case ZEND_ASSIGN_SUB:
     824           1 :                         return (binary_op_type) sub_function;
     825             :                         break;
     826             :                 case ZEND_MUL:
     827             :                 case ZEND_ASSIGN_MUL:
     828           0 :                         return (binary_op_type) mul_function;
     829             :                         break;
     830             :                 case ZEND_POW:
     831           0 :                         return (binary_op_type) pow_function;
     832             :                         break;
     833             :                 case ZEND_DIV:
     834             :                 case ZEND_ASSIGN_DIV:
     835           0 :                         return (binary_op_type) div_function;
     836             :                         break;
     837             :                 case ZEND_MOD:
     838             :                 case ZEND_ASSIGN_MOD:
     839           0 :                         return (binary_op_type) mod_function;
     840             :                         break;
     841             :                 case ZEND_SL:
     842             :                 case ZEND_ASSIGN_SL:
     843           0 :                         return (binary_op_type) shift_left_function;
     844             :                         break;
     845             :                 case ZEND_SR:
     846             :                 case ZEND_ASSIGN_SR:
     847           0 :                         return (binary_op_type) shift_right_function;
     848             :                         break;
     849             :                 case ZEND_CONCAT:
     850             :                 case ZEND_ASSIGN_CONCAT:
     851          14 :                         return (binary_op_type) concat_function;
     852             :                         break;
     853             :                 case ZEND_IS_IDENTICAL:
     854           0 :                         return (binary_op_type) is_identical_function;
     855             :                         break;
     856             :                 case ZEND_IS_NOT_IDENTICAL:
     857           0 :                         return (binary_op_type) is_not_identical_function;
     858             :                         break;
     859             :                 case ZEND_IS_EQUAL:
     860           1 :                         return (binary_op_type) is_equal_function;
     861             :                         break;
     862             :                 case ZEND_IS_NOT_EQUAL:
     863           0 :                         return (binary_op_type) is_not_equal_function;
     864             :                         break;
     865             :                 case ZEND_IS_SMALLER:
     866           0 :                         return (binary_op_type) is_smaller_function;
     867             :                         break;
     868             :                 case ZEND_IS_SMALLER_OR_EQUAL:
     869           5 :                         return (binary_op_type) is_smaller_or_equal_function;
     870             :                         break;
     871             :                 case ZEND_BW_OR:
     872             :                 case ZEND_ASSIGN_BW_OR:
     873           1 :                         return (binary_op_type) bitwise_or_function;
     874             :                         break;
     875             :                 case ZEND_BW_AND:
     876             :                 case ZEND_ASSIGN_BW_AND:
     877           0 :                         return (binary_op_type) bitwise_and_function;
     878             :                         break;
     879             :                 case ZEND_BW_XOR:
     880             :                 case ZEND_ASSIGN_BW_XOR:
     881           0 :                         return (binary_op_type) bitwise_xor_function;
     882             :                         break;
     883             :                 case ZEND_BOOL_XOR:
     884           0 :                         return (binary_op_type) boolean_xor_function;
     885             :                         break;
     886             :                 default:
     887           0 :                         return (binary_op_type) NULL;
     888             :                         break;
     889             :         }
     890             : }
     891             : 
     892             : /*
     893             :  * Local variables:
     894             :  * tab-width: 4
     895             :  * c-basic-offset: 4
     896             :  * indent-tabs-mode: t
     897             :  * End:
     898             :  */

Generated by: LCOV version 1.10

Generated at Mon, 04 Aug 2014 15:49:00 +0000 (23 days ago)

Copyright © 2005-2014 The PHP Group
All rights reserved.