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: 0 425 0.0 %
Date: 2014-04-16 Functions: 0 32 0.0 %
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           0 : static void zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
      33             : {
      34           0 :         if (extension->op_array_ctor) {
      35           0 :                 extension->op_array_ctor(op_array);
      36             :         }
      37           0 : }
      38             : 
      39           0 : static void zend_extension_op_array_dtor_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
      40             : {
      41           0 :         if (extension->op_array_dtor) {
      42           0 :                 extension->op_array_dtor(op_array);
      43             :         }
      44           0 : }
      45             : 
      46           0 : static void op_array_alloc_ops(zend_op_array *op_array, zend_uint size)
      47             : {
      48           0 :         op_array->opcodes = erealloc(op_array->opcodes, size * sizeof(zend_op));
      49           0 : }
      50             : 
      51           0 : void init_op_array(zend_op_array *op_array, zend_uchar type, int initial_ops_size TSRMLS_DC)
      52             : {
      53           0 :         op_array->type = type;
      54             : 
      55           0 :         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           0 :         op_array->refcount = (zend_uint *) emalloc(sizeof(zend_uint));
      63           0 :         *op_array->refcount = 1;
      64           0 :         op_array->last = 0;
      65           0 :         op_array->opcodes = NULL;
      66           0 :         op_array_alloc_ops(op_array, initial_ops_size);
      67             : 
      68           0 :         op_array->last_var = 0;
      69           0 :         op_array->vars = NULL;
      70             : 
      71           0 :         op_array->T = 0;
      72             : 
      73           0 :         op_array->nested_calls = 0;
      74           0 :         op_array->used_stack = 0;
      75             : 
      76           0 :         op_array->function_name = NULL;
      77           0 :         op_array->filename = zend_get_compiled_filename(TSRMLS_C);
      78           0 :         op_array->doc_comment = NULL;
      79           0 :         op_array->doc_comment_len = 0;
      80             : 
      81           0 :         op_array->arg_info = NULL;
      82           0 :         op_array->num_args = 0;
      83           0 :         op_array->required_num_args = 0;
      84             : 
      85           0 :         op_array->scope = NULL;
      86             : 
      87           0 :         op_array->brk_cont_array = NULL;
      88           0 :         op_array->try_catch_array = NULL;
      89           0 :         op_array->last_brk_cont = 0;
      90             : 
      91           0 :         op_array->static_variables = NULL;
      92           0 :         op_array->last_try_catch = 0;
      93           0 :         op_array->has_finally_block = 0;
      94             : 
      95           0 :         op_array->this_var = -1;
      96             : 
      97           0 :         op_array->fn_flags = CG(interactive)?ZEND_ACC_INTERACTIVE:0;
      98             : 
      99           0 :         op_array->early_binding = -1;
     100             : 
     101           0 :         op_array->last_literal = 0;
     102           0 :         op_array->literals = NULL;
     103             : 
     104           0 :         op_array->run_time_cache = NULL;
     105           0 :         op_array->last_cache_slot = 0;
     106             : 
     107           0 :         memset(op_array->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
     108             : 
     109           0 :         zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_ctor_handler, op_array TSRMLS_CC);
     110           0 : }
     111             : 
     112           0 : ZEND_API void destroy_zend_function(zend_function *function TSRMLS_DC)
     113             : {
     114           0 :         switch (function->type) {
     115             :                 case ZEND_USER_FUNCTION:
     116           0 :                         destroy_op_array((zend_op_array *) function TSRMLS_CC);
     117             :                         break;
     118             :                 case ZEND_INTERNAL_FUNCTION:
     119             :                         /* do nothing */
     120             :                         break;
     121             :         }
     122           0 : }
     123             : 
     124           0 : ZEND_API void zend_function_dtor(zend_function *function)
     125             : {
     126             :         TSRMLS_FETCH();
     127             : 
     128           0 :         destroy_zend_function(function TSRMLS_CC);
     129           0 : }
     130             : 
     131           0 : static void zend_cleanup_op_array_data(zend_op_array *op_array)
     132             : {
     133           0 :         if (op_array->static_variables) {
     134           0 :                 zend_hash_clean(op_array->static_variables);
     135             :         }
     136           0 : }
     137             : 
     138           0 : ZEND_API int zend_cleanup_function_data(zend_function *function TSRMLS_DC)
     139             : {
     140           0 :         if (function->type == ZEND_USER_FUNCTION) {
     141           0 :                 zend_cleanup_op_array_data((zend_op_array *) function);
     142           0 :                 return ZEND_HASH_APPLY_KEEP;
     143             :         } else {
     144           0 :                 return ZEND_HASH_APPLY_STOP;
     145             :         }
     146             : }
     147             : 
     148           0 : ZEND_API int zend_cleanup_function_data_full(zend_function *function TSRMLS_DC)
     149             : {
     150           0 :         if (function->type == ZEND_USER_FUNCTION) {
     151           0 :                 zend_cleanup_op_array_data((zend_op_array *) function);
     152             :         }
     153           0 :         return 0;
     154             : }
     155             : 
     156           0 : 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           0 :         if (ce->ce_flags & ZEND_HAS_STATIC_IN_METHODS) {
     162           0 :                 zend_hash_apply(&ce->function_table, (apply_func_t) zend_cleanup_function_data_full TSRMLS_CC);
     163             :         }
     164           0 :         if (ce->static_members_table) {
     165             :                 int i;
     166             : 
     167           0 :                 for (i = 0; i < ce->default_static_members_count; i++) {
     168           0 :                         if (ce->static_members_table[i]) {
     169           0 :                                 zval *p = ce->static_members_table[i];
     170           0 :                                 ce->static_members_table[i] = NULL;
     171           0 :                                 zval_ptr_dtor(&p);
     172             :                         }
     173             :                 }
     174           0 :                 ce->static_members_table = NULL;
     175             :         }
     176           0 : }
     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           0 : ZEND_API int zend_cleanup_user_class_data(zend_class_entry **pce TSRMLS_DC)
     201             : {
     202           0 :         if ((*pce)->type == ZEND_USER_CLASS) {
     203           0 :                 cleanup_user_class_data(*pce TSRMLS_CC);
     204           0 :                 return ZEND_HASH_APPLY_KEEP;
     205             :         } else {
     206           0 :                 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           0 : void _destroy_zend_class_traits_info(zend_class_entry *ce)
     221             : {
     222           0 :         if (ce->num_traits > 0 && ce->traits) {
     223           0 :                 efree(ce->traits);
     224             :         }
     225             :         
     226           0 :         if (ce->trait_aliases) {
     227           0 :                 size_t i = 0;
     228           0 :                 while (ce->trait_aliases[i]) {
     229           0 :                         if (ce->trait_aliases[i]->trait_method) {
     230           0 :                                 if (ce->trait_aliases[i]->trait_method->method_name) {
     231           0 :                                         efree((char*)ce->trait_aliases[i]->trait_method->method_name);
     232             :                                 }
     233           0 :                                 if (ce->trait_aliases[i]->trait_method->class_name) {
     234           0 :                                         efree((char*)ce->trait_aliases[i]->trait_method->class_name);
     235             :                                 }
     236           0 :                                 efree(ce->trait_aliases[i]->trait_method);
     237             :                         }
     238             :                         
     239           0 :                         if (ce->trait_aliases[i]->alias) {
     240           0 :                                 efree((char*)ce->trait_aliases[i]->alias);
     241             :                         }
     242             :                         
     243           0 :                         efree(ce->trait_aliases[i]);
     244           0 :                         i++;
     245             :                 }
     246             :                 
     247           0 :                 efree(ce->trait_aliases);
     248             :         }
     249             : 
     250           0 :         if (ce->trait_precedences) {
     251           0 :                 size_t i = 0;
     252             :                 
     253           0 :                 while (ce->trait_precedences[i]) {
     254           0 :                         efree((char*)ce->trait_precedences[i]->trait_method->method_name);
     255           0 :                         efree((char*)ce->trait_precedences[i]->trait_method->class_name);
     256           0 :                         efree(ce->trait_precedences[i]->trait_method);
     257             : 
     258           0 :                         if (ce->trait_precedences[i]->exclude_from_classes) {
     259           0 :                                 efree(ce->trait_precedences[i]->exclude_from_classes);
     260             :                         }
     261             : 
     262           0 :                         efree(ce->trait_precedences[i]);
     263           0 :                         i++;
     264             :                 }
     265           0 :                 efree(ce->trait_precedences);
     266             :         }
     267           0 : }
     268             : 
     269           0 : ZEND_API void destroy_zend_class(zend_class_entry **pce)
     270             : {
     271           0 :         zend_class_entry *ce = *pce;
     272             :         
     273           0 :         if (--ce->refcount > 0) {
     274           0 :                 return;
     275             :         }
     276           0 :         switch (ce->type) {
     277             :                 case ZEND_USER_CLASS:
     278           0 :                         if (ce->default_properties_table) {
     279             :                                 int i;
     280             : 
     281           0 :                                 for (i = 0; i < ce->default_properties_count; i++) {
     282           0 :                                         if (ce->default_properties_table[i]) {
     283           0 :                                                 zval_ptr_dtor(&ce->default_properties_table[i]);
     284             :                                     }
     285             :                                 }
     286           0 :                                 efree(ce->default_properties_table);
     287             :                         }
     288           0 :                         if (ce->default_static_members_table) {
     289             :                                 int i;
     290             : 
     291           0 :                                 for (i = 0; i < ce->default_static_members_count; i++) {
     292           0 :                                         if (ce->default_static_members_table[i]) {
     293           0 :                                                 zval_ptr_dtor(&ce->default_static_members_table[i]);
     294             :                                         }
     295             :                                 }
     296           0 :                                 efree(ce->default_static_members_table);
     297             :                         }
     298           0 :                         zend_hash_destroy(&ce->properties_info);
     299           0 :                         str_efree(ce->name);
     300           0 :                         zend_hash_destroy(&ce->function_table);
     301           0 :                         zend_hash_destroy(&ce->constants_table);
     302           0 :                         if (ce->num_interfaces > 0 && ce->interfaces) {
     303           0 :                                 efree(ce->interfaces);
     304             :                         }
     305           0 :                         if (ce->info.user.doc_comment) {
     306           0 :                                 efree((char*)ce->info.user.doc_comment);
     307             :                         }
     308             :                         
     309           0 :                         _destroy_zend_class_traits_info(ce);
     310             :                         
     311           0 :                         efree(ce);
     312           0 :                         break;
     313             :                 case ZEND_INTERNAL_CLASS:
     314           0 :                         if (ce->default_properties_table) {
     315             :                                 int i;
     316             : 
     317           0 :                                 for (i = 0; i < ce->default_properties_count; i++) {
     318           0 :                                         if (ce->default_properties_table[i]) {
     319           0 :                                                 zval_internal_ptr_dtor(&ce->default_properties_table[i]);
     320             :                                         }
     321             :                                 }
     322           0 :                                 free(ce->default_properties_table);
     323             :                         }
     324           0 :                         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           0 :                         zend_hash_destroy(&ce->properties_info);
     333           0 :                         str_free(ce->name);
     334           0 :                         zend_hash_destroy(&ce->function_table);
     335           0 :                         zend_hash_destroy(&ce->constants_table);
     336           0 :                         if (ce->num_interfaces > 0) {
     337           0 :                                 free(ce->interfaces);
     338             :                         }
     339           0 :                         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           0 : ZEND_API void destroy_op_array(zend_op_array *op_array TSRMLS_DC)
     350             : {
     351           0 :         zend_literal *literal = op_array->literals;
     352             :         zend_literal *end;
     353             :         zend_uint i;
     354             : 
     355           0 :         if (op_array->static_variables) {
     356           0 :                 zend_hash_destroy(op_array->static_variables);
     357           0 :                 FREE_HASHTABLE(op_array->static_variables);
     358             :         }
     359             : 
     360           0 :         if (op_array->run_time_cache) {
     361           0 :                 efree(op_array->run_time_cache);
     362             :         }
     363             : 
     364           0 :         if (--(*op_array->refcount)>0) {
     365           0 :                 return;
     366             :         }
     367             : 
     368           0 :         efree(op_array->refcount);
     369             : 
     370           0 :         if (op_array->vars) {
     371           0 :                 i = op_array->last_var;
     372           0 :                 while (i > 0) {
     373           0 :                         i--;
     374           0 :                         str_efree(op_array->vars[i].name);
     375             :                 }
     376           0 :                 efree(op_array->vars);
     377             :         }
     378             : 
     379           0 :         if (literal) {
     380           0 :                 end = literal + op_array->last_literal;
     381           0 :                 while (literal < end) {
     382           0 :                         zval_dtor(&literal->constant);
     383           0 :                         literal++;
     384             :                 }
     385           0 :                 efree(op_array->literals);
     386             :         }
     387           0 :         efree(op_array->opcodes);
     388             : 
     389           0 :         if (op_array->function_name) {
     390           0 :                 efree((char*)op_array->function_name);
     391             :         }
     392           0 :         if (op_array->doc_comment) {
     393           0 :                 efree((char*)op_array->doc_comment);
     394             :         }
     395           0 :         if (op_array->brk_cont_array) {
     396           0 :                 efree(op_array->brk_cont_array);
     397             :         }
     398           0 :         if (op_array->try_catch_array) {
     399           0 :                 efree(op_array->try_catch_array);
     400             :         }
     401           0 :         if (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) {
     402           0 :                 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           0 :         if (op_array->arg_info) {
     405           0 :                 for (i=0; i<op_array->num_args; i++) {
     406           0 :                         str_efree(op_array->arg_info[i].name);
     407           0 :                         if (op_array->arg_info[i].class_name) {
     408           0 :                                 str_efree(op_array->arg_info[i].class_name);
     409             :                         }
     410             :                 }
     411           0 :                 efree(op_array->arg_info);
     412             :         }
     413             : }
     414             : 
     415           0 : void init_op(zend_op *op TSRMLS_DC)
     416             : {
     417           0 :         memset(op, 0, sizeof(zend_op));
     418           0 :         op->lineno = CG(zend_lineno);
     419           0 :         SET_UNUSED(op->result);
     420           0 : }
     421             : 
     422           0 : zend_op *get_next_op(zend_op_array *op_array TSRMLS_DC)
     423             : {
     424           0 :         zend_uint next_op_num = op_array->last++;
     425             :         zend_op *next_op;
     426             : 
     427           0 :         if (next_op_num >= CG(context).opcodes_size) {
     428           0 :                 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           0 :                 CG(context).opcodes_size *= 4;
     435           0 :                 op_array_alloc_ops(op_array, CG(context).opcodes_size);
     436             :         }
     437             :         
     438           0 :         next_op = &(op_array->opcodes[next_op_num]);
     439             :         
     440           0 :         init_op(next_op TSRMLS_CC);
     441             : 
     442           0 :         return next_op;
     443             : }
     444             : 
     445           0 : int get_next_op_number(zend_op_array *op_array)
     446             : {
     447           0 :         return op_array->last;
     448             : }
     449             : 
     450           0 : zend_brk_cont_element *get_next_brk_cont_element(zend_op_array *op_array)
     451             : {
     452           0 :         op_array->last_brk_cont++;
     453           0 :         op_array->brk_cont_array = erealloc(op_array->brk_cont_array, sizeof(zend_brk_cont_element)*op_array->last_brk_cont);
     454           0 :         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           0 : static void zend_extension_op_array_handler(zend_extension *extension, zend_op_array *op_array TSRMLS_DC)
     481             : {
     482           0 :         if (extension->op_array_handler) {
     483           0 :                 extension->op_array_handler(op_array);
     484             :         }
     485           0 : }
     486             : 
     487           0 : 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           0 :         for (i = 0; i < op_array->last_try_catch; i++) {
     492           0 :                 if ((op_num < op_array->try_catch_array[i].finally_op ||
     493           0 :                                         op_num >= op_array->try_catch_array[i].finally_end)
     494           0 :                                 && (dst_num >= op_array->try_catch_array[i].finally_op &&
     495           0 :                                          dst_num <= op_array->try_catch_array[i].finally_end)) {
     496           0 :                         CG(in_compilation) = 1;
     497           0 :                         CG(active_op_array) = op_array;
     498           0 :                         CG(zend_lineno) = op_array->opcodes[op_num].lineno;
     499           0 :                         zend_error_noreturn(E_COMPILE_ERROR, "jump into a finally block is disallowed");
     500           0 :                 } else if ((op_num >= op_array->try_catch_array[i].finally_op 
     501           0 :                                         && op_num <= op_array->try_catch_array[i].finally_end)
     502           0 :                                 && (dst_num > op_array->try_catch_array[i].finally_end 
     503           0 :                                         || dst_num < op_array->try_catch_array[i].finally_op)) {
     504           0 :                         CG(in_compilation) = 1;
     505           0 :                         CG(active_op_array) = op_array;
     506           0 :                         CG(zend_lineno) = op_array->opcodes[op_num].lineno;
     507           0 :                         zend_error_noreturn(E_COMPILE_ERROR, "jump out of a finally block is disallowed");
     508             :                 }
     509             :         } 
     510           0 : }
     511             : 
     512           0 : static void zend_resolve_finally_call(zend_op_array *op_array, zend_uint op_num, zend_uint dst_num TSRMLS_DC)
     513             : {
     514             :         zend_uint start_op;
     515             :         zend_op *opline;
     516           0 :         zend_uint i = op_array->last_try_catch;
     517             : 
     518           0 :         if (dst_num != (zend_uint)-1) {
     519           0 :                 zend_check_finally_breakout(op_array, op_num, dst_num TSRMLS_CC);
     520             :         }
     521             : 
     522             :         /* the backward order is mater */
     523           0 :         while (i > 0) {
     524           0 :                 i--;
     525           0 :                 if (op_array->try_catch_array[i].finally_op &&
     526           0 :                     op_num >= op_array->try_catch_array[i].try_op &&
     527           0 :                     op_num < op_array->try_catch_array[i].finally_op - 1 &&
     528           0 :                     (dst_num < op_array->try_catch_array[i].try_op ||
     529           0 :                      dst_num > op_array->try_catch_array[i].finally_end)) {
     530             :                         /* we have a jump out of try block that needs executing finally */
     531             : 
     532             :                         /* generate a FAST_CALL to finally block */
     533           0 :                     start_op = get_next_op_number(op_array);
     534             : 
     535           0 :                         opline = get_next_op(op_array TSRMLS_CC);
     536           0 :                         opline->opcode = ZEND_FAST_CALL;
     537           0 :                         SET_UNUSED(opline->op1);
     538           0 :                         SET_UNUSED(opline->op2);
     539           0 :                         opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
     540           0 :                         if (op_array->try_catch_array[i].catch_op) {
     541           0 :                                 opline->extended_value = 1;
     542           0 :                                 opline->op2.opline_num = op_array->try_catch_array[i].catch_op;
     543             :                         }
     544             : 
     545             :                         /* generate a sequence of FAST_CALL to upward finally block */
     546           0 :                         while (i > 0) {
     547           0 :                                 i--;
     548           0 :                                 if (op_array->try_catch_array[i].finally_op &&
     549           0 :                                         op_num >= op_array->try_catch_array[i].try_op &&
     550           0 :                                         op_num < op_array->try_catch_array[i].finally_op - 1 &&
     551           0 :                                         (dst_num < op_array->try_catch_array[i].try_op ||
     552           0 :                                          dst_num > op_array->try_catch_array[i].finally_end)) {
     553             : 
     554           0 :                                         opline = get_next_op(op_array TSRMLS_CC);
     555           0 :                                         opline->opcode = ZEND_FAST_CALL;
     556           0 :                                         SET_UNUSED(opline->op1);
     557           0 :                                         SET_UNUSED(opline->op2);
     558           0 :                                         opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
     559             :                                 }
     560             :                         }
     561             : 
     562             :                         /* Finish the sequence with original opcode */
     563           0 :                         opline = get_next_op(op_array TSRMLS_CC);
     564           0 :                         *opline = op_array->opcodes[op_num];
     565             : 
     566             :                         /* Replace original opcode with jump to this sequence */
     567           0 :                         opline = op_array->opcodes + op_num;
     568           0 :                         opline->opcode = ZEND_JMP;
     569           0 :                         SET_UNUSED(opline->op1);
     570           0 :                         SET_UNUSED(opline->op2);
     571           0 :                         opline->op1.opline_num = start_op;
     572             : 
     573           0 :                         break;
     574             :                 }
     575             :         }       
     576           0 : }
     577             : 
     578           0 : static void zend_resolve_finally_ret(zend_op_array *op_array, zend_uint op_num TSRMLS_DC)
     579             : {
     580             :         int i;
     581           0 :         zend_uint catch_op_num = 0, finally_op_num = 0;
     582             : 
     583           0 :         for (i = 0; i < op_array->last_try_catch; i++) {
     584           0 :                 if (op_array->try_catch_array[i].try_op > op_num) {
     585           0 :                         break;
     586             :                 }
     587           0 :                 if (op_num < op_array->try_catch_array[i].finally_op) {
     588           0 :                         finally_op_num = op_array->try_catch_array[i].finally_op;
     589             :                 }
     590           0 :                 if (op_num < op_array->try_catch_array[i].catch_op) {
     591           0 :                         catch_op_num = op_array->try_catch_array[i].catch_op;
     592             :                 }
     593             :         }
     594             : 
     595           0 :         if (finally_op_num && (!catch_op_num || catch_op_num >= finally_op_num)) {
     596             :                 /* in case of unhandled exception return to upward finally block */
     597           0 :                 op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_FINALLY;
     598           0 :                 op_array->opcodes[op_num].op2.opline_num = finally_op_num;
     599           0 :         } else if (catch_op_num) {
     600             :                 /* in case of unhandled exception return to upward catch block */
     601           0 :                 op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_CATCH;
     602           0 :                 op_array->opcodes[op_num].op2.opline_num = catch_op_num;
     603             :         }
     604           0 : }
     605             : 
     606           0 : static void zend_resolve_finally_calls(zend_op_array *op_array TSRMLS_DC)
     607             : {
     608             :         zend_uint i;
     609             :         zend_op *opline;
     610             : 
     611           0 :         for (i = 0; i < op_array->last; i++) {
     612           0 :                 opline = op_array->opcodes + i;
     613           0 :                 switch (opline->opcode) {
     614             :                         case ZEND_RETURN:
     615             :                         case ZEND_RETURN_BY_REF:
     616             :                         case ZEND_GENERATOR_RETURN:
     617           0 :                                 zend_resolve_finally_call(op_array, i, (zend_uint)-1 TSRMLS_CC);
     618           0 :                                 break;
     619             :                         case ZEND_BRK:
     620             :                         case ZEND_CONT:
     621             :                         {
     622             :                                 int nest_levels, array_offset;
     623             :                                 zend_brk_cont_element *jmp_to;
     624             : 
     625           0 :                                 nest_levels = Z_LVAL(op_array->literals[opline->op2.constant].constant);
     626           0 :                                 array_offset = opline->op1.opline_num;
     627             :                                 do {
     628           0 :                                         jmp_to = &op_array->brk_cont_array[array_offset];
     629           0 :                                         if (nest_levels > 1) {
     630           0 :                                                 array_offset = jmp_to->parent;
     631             :                                         }
     632           0 :                                 } while (--nest_levels > 0);
     633           0 :                                 zend_resolve_finally_call(op_array, i, opline->opcode == ZEND_BRK ? jmp_to->brk : jmp_to->cont TSRMLS_CC);
     634           0 :                                 break;
     635             :                         }
     636             :                         case ZEND_GOTO:
     637           0 :                                 if (Z_TYPE(op_array->literals[opline->op2.constant].constant) != IS_LONG) {
     638           0 :                                         zend_uint num = opline->op2.constant;
     639           0 :                                         opline->op2.zv = &op_array->literals[opline->op2.constant].constant;
     640           0 :                                         zend_resolve_goto_label(op_array, opline, 1 TSRMLS_CC);
     641           0 :                                         opline->op2.constant = num;                                  
     642             :                                 }
     643             :                                 /* break omitted intentionally */
     644             :                         case ZEND_JMP:
     645           0 :                                 zend_resolve_finally_call(op_array, i, opline->op1.opline_num TSRMLS_CC);
     646           0 :                                 break;
     647             :                         case ZEND_FAST_RET:
     648           0 :                                 zend_resolve_finally_ret(op_array, i TSRMLS_CC);
     649             :                                 break;
     650             :                         default:
     651             :                                 break;
     652             :                 }
     653             :         }
     654           0 : }
     655             : 
     656           0 : ZEND_API int pass_two(zend_op_array *op_array TSRMLS_DC)
     657             : {
     658             :         zend_op *opline, *end;
     659             : 
     660           0 :         if (op_array->type!=ZEND_USER_FUNCTION && op_array->type!=ZEND_EVAL_CODE) {
     661           0 :                 return 0;
     662             :         }
     663           0 :         if (op_array->has_finally_block) {
     664           0 :                 zend_resolve_finally_calls(op_array TSRMLS_CC);
     665             :         }
     666           0 :         if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) {
     667           0 :                 zend_update_extended_info(op_array TSRMLS_CC);
     668             :         }
     669           0 :         if (CG(compiler_options) & ZEND_COMPILE_HANDLE_OP_ARRAY) {
     670           0 :                 zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_handler, op_array TSRMLS_CC);
     671             :         }
     672             : 
     673           0 :         if (!(op_array->fn_flags & ZEND_ACC_INTERACTIVE) && CG(context).vars_size != op_array->last_var) {
     674           0 :                 op_array->vars = (zend_compiled_variable *) erealloc(op_array->vars, sizeof(zend_compiled_variable)*op_array->last_var);
     675           0 :                 CG(context).vars_size = op_array->last_var;
     676             :         }
     677           0 :         if (!(op_array->fn_flags & ZEND_ACC_INTERACTIVE) && CG(context).opcodes_size != op_array->last) {
     678           0 :                 op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last);
     679           0 :                 CG(context).opcodes_size = op_array->last;
     680             :         }
     681           0 :         if (!(op_array->fn_flags & ZEND_ACC_INTERACTIVE) && CG(context).literals_size != op_array->last_literal) {
     682           0 :                 op_array->literals = (zend_literal*)erealloc(op_array->literals, sizeof(zend_literal) * op_array->last_literal);
     683           0 :                 CG(context).literals_size = op_array->last_literal;
     684             :         }
     685             : 
     686           0 :         opline = op_array->opcodes;
     687           0 :         end = opline + op_array->last;
     688           0 :         while (opline < end) {
     689           0 :                 if (opline->op1_type == IS_CONST) {
     690           0 :                         opline->op1.zv = &op_array->literals[opline->op1.constant].constant;
     691             :                 }
     692           0 :                 if (opline->op2_type == IS_CONST) {
     693           0 :                         opline->op2.zv = &op_array->literals[opline->op2.constant].constant;
     694             :                 }
     695           0 :                 switch (opline->opcode) {
     696             :                         case ZEND_GOTO:
     697           0 :                                 if (Z_TYPE_P(opline->op2.zv) != IS_LONG) {
     698           0 :                                         zend_resolve_goto_label(op_array, opline, 1 TSRMLS_CC);
     699             :                                 }
     700             :                                 /* break omitted intentionally */
     701             :                         case ZEND_JMP:
     702             :                         case ZEND_FAST_CALL:
     703           0 :                                 opline->op1.jmp_addr = &op_array->opcodes[opline->op1.opline_num];
     704           0 :                                 break;
     705             :                         case ZEND_JMPZ:
     706             :                         case ZEND_JMPNZ:
     707             :                         case ZEND_JMPZ_EX:
     708             :                         case ZEND_JMPNZ_EX:
     709             :                         case ZEND_JMP_SET:
     710             :                         case ZEND_JMP_SET_VAR:
     711           0 :                                 opline->op2.jmp_addr = &op_array->opcodes[opline->op2.opline_num];
     712           0 :                                 break;
     713             :                         case ZEND_RETURN:
     714             :                         case ZEND_RETURN_BY_REF:
     715           0 :                                 if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
     716           0 :                                         if (opline->op1_type != IS_CONST || Z_TYPE_P(opline->op1.zv) != IS_NULL) {
     717           0 :                                                 CG(zend_lineno) = opline->lineno;
     718           0 :                                                 zend_error_noreturn(E_COMPILE_ERROR, "Generators cannot return values using \"return\"");
     719             :                                         }
     720             : 
     721           0 :                                         opline->opcode = ZEND_GENERATOR_RETURN;
     722             :                                 }
     723             :                                 break;
     724             :                 }
     725           0 :                 ZEND_VM_SET_OPCODE_HANDLER(opline);
     726           0 :                 opline++;
     727             :         }
     728             : 
     729           0 :         op_array->fn_flags |= ZEND_ACC_DONE_PASS_TWO;
     730           0 :         return 0;
     731             : }
     732             : 
     733           0 : int print_class(zend_class_entry *class_entry TSRMLS_DC)
     734             : {
     735           0 :         printf("Class %s:\n", class_entry->name);
     736           0 :         zend_hash_apply(&class_entry->function_table, (apply_func_t) pass_two TSRMLS_CC);
     737           0 :         printf("End of class %s.\n\n", class_entry->name);
     738           0 :         return 0;
     739             : }
     740             : 
     741           0 : ZEND_API unary_op_type get_unary_op(int opcode)
     742             : {
     743           0 :         switch (opcode) {
     744             :                 case ZEND_BW_NOT:
     745           0 :                         return (unary_op_type) bitwise_not_function;
     746             :                         break;
     747             :                 case ZEND_BOOL_NOT:
     748           0 :                         return (unary_op_type) boolean_not_function;
     749             :                         break;
     750             :                 default:
     751           0 :                         return (unary_op_type) NULL;
     752             :                         break;
     753             :         }
     754             : }
     755             : 
     756           0 : ZEND_API binary_op_type get_binary_op(int opcode)
     757             : {
     758           0 :         switch (opcode) {
     759             :                 case ZEND_ADD:
     760             :                 case ZEND_ASSIGN_ADD:
     761           0 :                         return (binary_op_type) add_function;
     762             :                         break;
     763             :                 case ZEND_SUB:
     764             :                 case ZEND_ASSIGN_SUB:
     765           0 :                         return (binary_op_type) sub_function;
     766             :                         break;
     767             :                 case ZEND_MUL:
     768             :                 case ZEND_ASSIGN_MUL:
     769           0 :                         return (binary_op_type) mul_function;
     770             :                         break;
     771             :                 case ZEND_POW:
     772           0 :                         return (binary_op_type) pow_function;
     773             :                         break;
     774             :                 case ZEND_DIV:
     775             :                 case ZEND_ASSIGN_DIV:
     776           0 :                         return (binary_op_type) div_function;
     777             :                         break;
     778             :                 case ZEND_MOD:
     779             :                 case ZEND_ASSIGN_MOD:
     780           0 :                         return (binary_op_type) mod_function;
     781             :                         break;
     782             :                 case ZEND_SL:
     783             :                 case ZEND_ASSIGN_SL:
     784           0 :                         return (binary_op_type) shift_left_function;
     785             :                         break;
     786             :                 case ZEND_SR:
     787             :                 case ZEND_ASSIGN_SR:
     788           0 :                         return (binary_op_type) shift_right_function;
     789             :                         break;
     790             :                 case ZEND_CONCAT:
     791             :                 case ZEND_ASSIGN_CONCAT:
     792           0 :                         return (binary_op_type) concat_function;
     793             :                         break;
     794             :                 case ZEND_IS_IDENTICAL:
     795           0 :                         return (binary_op_type) is_identical_function;
     796             :                         break;
     797             :                 case ZEND_IS_NOT_IDENTICAL:
     798           0 :                         return (binary_op_type) is_not_identical_function;
     799             :                         break;
     800             :                 case ZEND_IS_EQUAL:
     801           0 :                         return (binary_op_type) is_equal_function;
     802             :                         break;
     803             :                 case ZEND_IS_NOT_EQUAL:
     804           0 :                         return (binary_op_type) is_not_equal_function;
     805             :                         break;
     806             :                 case ZEND_IS_SMALLER:
     807           0 :                         return (binary_op_type) is_smaller_function;
     808             :                         break;
     809             :                 case ZEND_IS_SMALLER_OR_EQUAL:
     810           0 :                         return (binary_op_type) is_smaller_or_equal_function;
     811             :                         break;
     812             :                 case ZEND_BW_OR:
     813             :                 case ZEND_ASSIGN_BW_OR:
     814           0 :                         return (binary_op_type) bitwise_or_function;
     815             :                         break;
     816             :                 case ZEND_BW_AND:
     817             :                 case ZEND_ASSIGN_BW_AND:
     818           0 :                         return (binary_op_type) bitwise_and_function;
     819             :                         break;
     820             :                 case ZEND_BW_XOR:
     821             :                 case ZEND_ASSIGN_BW_XOR:
     822           0 :                         return (binary_op_type) bitwise_xor_function;
     823             :                         break;
     824             :                 case ZEND_BOOL_XOR:
     825           0 :                         return (binary_op_type) boolean_xor_function;
     826             :                         break;
     827             :                 default:
     828           0 :                         return (binary_op_type) NULL;
     829             :                         break;
     830             :         }
     831             : }
     832             : 
     833             : /*
     834             :  * Local variables:
     835             :  * tab-width: 4
     836             :  * c-basic-offset: 4
     837             :  * indent-tabs-mode: t
     838             :  * End:
     839             :  */

Generated by: LCOV version 1.10

Generated at Wed, 16 Apr 2014 12:47:43 +0000 (7 days ago)

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