Back to index

php5  5.3.10
zend_execute.h
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | Zend Engine                                                          |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 2.00 of the Zend license,     |
00008    | that is bundled with this package in the file LICENSE, and is        | 
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.zend.com/license/2_00.txt.                                |
00011    | If you did not receive a copy of the Zend license and are unable to  |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@zend.com so we can mail you a copy immediately.              |
00014    +----------------------------------------------------------------------+
00015    | Authors: Andi Gutmans <andi@zend.com>                                |
00016    |          Zeev Suraski <zeev@zend.com>                                |
00017    +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: zend_execute.h 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 #ifndef ZEND_EXECUTE_H
00023 #define ZEND_EXECUTE_H
00024 
00025 #include "zend_compile.h"
00026 #include "zend_hash.h"
00027 #include "zend_operators.h"
00028 #include "zend_variables.h"
00029 
00030 typedef union _temp_variable {
00031        zval tmp_var;
00032        struct {
00033               zval **ptr_ptr;
00034               zval *ptr;
00035               zend_bool fcall_returned_reference;
00036        } var;
00037        struct {
00038               zval **ptr_ptr;
00039               zval *ptr;
00040               zend_bool fcall_returned_reference;
00041               zval *str;
00042               zend_uint offset;
00043        } str_offset;
00044        struct {
00045               zval **ptr_ptr;
00046               zval *ptr;
00047               zend_bool fcall_returned_reference;
00048               HashPointer fe_pos;
00049        } fe;
00050        zend_class_entry *class_entry;
00051 } temp_variable;
00052 
00053 
00054 BEGIN_EXTERN_C()
00055 ZEND_API extern void (*zend_execute)(zend_op_array *op_array TSRMLS_DC);
00056 ZEND_API extern void (*zend_execute_internal)(zend_execute_data *execute_data_ptr, int return_value_used TSRMLS_DC);
00057 
00058 void init_executor(TSRMLS_D);
00059 void shutdown_executor(TSRMLS_D);
00060 void shutdown_destructors(TSRMLS_D);
00061 ZEND_API void execute(zend_op_array *op_array TSRMLS_DC);
00062 ZEND_API void execute_internal(zend_execute_data *execute_data_ptr, int return_value_used TSRMLS_DC);
00063 ZEND_API int zend_is_true(zval *op);
00064 #define safe_free_zval_ptr(p) safe_free_zval_ptr_rel(p ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)
00065 static inline void safe_free_zval_ptr_rel(zval *p ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
00066 {
00067        TSRMLS_FETCH();
00068 
00069        if (p!=EG(uninitialized_zval_ptr)) {
00070               FREE_ZVAL_REL(p);
00071        }
00072 }
00073 ZEND_API int zend_lookup_class(const char *name, int name_length, zend_class_entry ***ce TSRMLS_DC);
00074 ZEND_API int zend_lookup_class_ex(const char *name, int name_length, int use_autoload, zend_class_entry ***ce TSRMLS_DC);
00075 ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name TSRMLS_DC);
00076 ZEND_API int zend_eval_stringl(char *str, int str_len, zval *retval_ptr, char *string_name TSRMLS_DC);
00077 ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC);
00078 ZEND_API int zend_eval_stringl_ex(char *str, int str_len, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC);
00079 
00080 static inline int i_zend_is_true(zval *op)
00081 {
00082        int result;
00083 
00084        switch (Z_TYPE_P(op)) {
00085               case IS_NULL:
00086                      result = 0;
00087                      break;
00088               case IS_LONG:
00089               case IS_BOOL:
00090               case IS_RESOURCE:
00091                      result = (Z_LVAL_P(op)?1:0);
00092                      break;
00093               case IS_DOUBLE:
00094                      result = (Z_DVAL_P(op) ? 1 : 0);
00095                      break;
00096               case IS_STRING:
00097                      if (Z_STRLEN_P(op) == 0
00098                             || (Z_STRLEN_P(op)==1 && Z_STRVAL_P(op)[0]=='0')) {
00099                             result = 0;
00100                      } else {
00101                             result = 1;
00102                      }
00103                      break;
00104               case IS_ARRAY:
00105                      result = (zend_hash_num_elements(Z_ARRVAL_P(op))?1:0);
00106                      break;
00107               case IS_OBJECT:
00108                      if(IS_ZEND_STD_OBJECT(*op)) {
00109                             TSRMLS_FETCH();
00110 
00111                             if (Z_OBJ_HT_P(op)->cast_object) {
00112                                    zval tmp;
00113                                    if (Z_OBJ_HT_P(op)->cast_object(op, &tmp, IS_BOOL TSRMLS_CC) == SUCCESS) {
00114                                           result = Z_LVAL(tmp);
00115                                           break;
00116                                    }
00117                             } else if (Z_OBJ_HT_P(op)->get) {
00118                                    zval *tmp = Z_OBJ_HT_P(op)->get(op TSRMLS_CC);
00119                                    if(Z_TYPE_P(tmp) != IS_OBJECT) {
00120                                           /* for safety - avoid loop */
00121                                           convert_to_boolean(tmp);
00122                                           result = Z_LVAL_P(tmp);
00123                                           zval_ptr_dtor(&tmp);
00124                                           break;
00125                                    }
00126                             }
00127                      }
00128                      result = 1;
00129                      break;
00130               default:
00131                      result = 0;
00132                      break;
00133        }
00134        return result;
00135 }
00136 
00137 ZEND_API int zval_update_constant(zval **pp, void *arg TSRMLS_DC);
00138 ZEND_API int zval_update_constant_inline_change(zval **pp, void *arg TSRMLS_DC);
00139 ZEND_API int zval_update_constant_no_inline_change(zval **pp, void *arg TSRMLS_DC);
00140 ZEND_API int zval_update_constant_ex(zval **pp, void *arg, zend_class_entry *scope TSRMLS_DC);
00141 
00142 /* dedicated Zend executor functions - do not use! */
00143 #define ZEND_VM_STACK_PAGE_SIZE ((64 * 1024) - 64)
00144 
00145 struct _zend_vm_stack {
00146        void **top;
00147        void **end;
00148        zend_vm_stack prev;
00149 };
00150 
00151 #define ZEND_VM_STACK_ELEMETS(stack) \
00152        ((void**)(((char*)(stack)) + ZEND_MM_ALIGNED_SIZE(sizeof(struct _zend_vm_stack))))
00153 
00154 #define ZEND_VM_STACK_GROW_IF_NEEDED(count)                                                \
00155        do {                                                                                                     \
00156               if (UNEXPECTED((count) >                                                            \
00157                   EG(argument_stack)->end - EG(argument_stack)->top)) {      \
00158                      zend_vm_stack_extend((count) TSRMLS_CC);                       \
00159               }                                                                                                        \
00160        } while (0)
00161 
00162 static inline zend_vm_stack zend_vm_stack_new_page(int count) {
00163        zend_vm_stack page = (zend_vm_stack)emalloc(ZEND_MM_ALIGNED_SIZE(sizeof(*page)) + sizeof(void*) * count);
00164 
00165        page->top = ZEND_VM_STACK_ELEMETS(page);
00166        page->end = page->top + count;
00167        page->prev = NULL;
00168        return page;
00169 }
00170 
00171 static inline void zend_vm_stack_init(TSRMLS_D)
00172 {
00173        EG(argument_stack) = zend_vm_stack_new_page(ZEND_VM_STACK_PAGE_SIZE);
00174 }
00175 
00176 static inline void zend_vm_stack_destroy(TSRMLS_D)
00177 {
00178        zend_vm_stack stack = EG(argument_stack);
00179 
00180        while (stack != NULL) {
00181               zend_vm_stack p = stack->prev;
00182               efree(stack);
00183               stack = p;
00184        }
00185 }
00186 
00187 static inline void zend_vm_stack_extend(int count TSRMLS_DC)
00188 {
00189        zend_vm_stack p = zend_vm_stack_new_page(count >= ZEND_VM_STACK_PAGE_SIZE ? count : ZEND_VM_STACK_PAGE_SIZE);
00190        p->prev = EG(argument_stack);
00191        EG(argument_stack) = p;
00192 }
00193 
00194 static inline void **zend_vm_stack_top(TSRMLS_D)
00195 {
00196        return EG(argument_stack)->top;
00197 }
00198 
00199 static inline void zend_vm_stack_push(void *ptr TSRMLS_DC)
00200 {
00201        ZEND_VM_STACK_GROW_IF_NEEDED(1);
00202        *(EG(argument_stack)->top++) = ptr;
00203 }
00204 
00205 static inline void zend_vm_stack_push_nocheck(void *ptr TSRMLS_DC)
00206 {
00207        *(EG(argument_stack)->top++) = ptr;
00208 }
00209 
00210 static inline void *zend_vm_stack_pop(TSRMLS_D)
00211 {
00212        void *el = *(--EG(argument_stack)->top);
00213 
00214        if (UNEXPECTED(EG(argument_stack)->top == ZEND_VM_STACK_ELEMETS(EG(argument_stack)))) {
00215               zend_vm_stack p = EG(argument_stack);
00216               EG(argument_stack) = p->prev;
00217               efree(p);
00218        }
00219        return el;
00220 }
00221 
00222 static inline void *zend_vm_stack_alloc(size_t size TSRMLS_DC)
00223 {
00224        void *ret;
00225 
00226        size = (size + (sizeof(void*) - 1)) / sizeof(void*);
00227 
00228        /* the following comparison must be optimized out at compile time */
00229        if (ZEND_MM_ALIGNMENT > sizeof(void*)) {
00230               int extra = (ZEND_MM_ALIGNMENT - ((zend_uintptr_t)EG(argument_stack)->top & (ZEND_MM_ALIGNMENT - 1))) / sizeof(void*);
00231 
00232               if (UNEXPECTED(size + extra + ZEND_MM_ALIGNED_SIZE(sizeof(void*)) / sizeof(void*) >
00233                   (zend_uintptr_t)(EG(argument_stack)->end - EG(argument_stack)->top))) {
00234                      zend_vm_stack_extend(size TSRMLS_CC);
00235               } else {
00236                      void **old_top = EG(argument_stack)->top;
00237 
00238                      EG(argument_stack)->top += extra;
00239                      /* store old top on the stack */
00240                      *EG(argument_stack)->top = (void*)old_top;
00241                      EG(argument_stack)->top += ZEND_MM_ALIGNED_SIZE(sizeof(void*)) / sizeof(void*);
00242               }
00243        } else {
00244               ZEND_VM_STACK_GROW_IF_NEEDED((int)size);
00245        }
00246        ret = (void*)EG(argument_stack)->top;
00247        EG(argument_stack)->top += size;
00248        return ret;
00249 }
00250 
00251 static inline void zend_vm_stack_free_int(void *ptr TSRMLS_DC)
00252 {      
00253        if (UNEXPECTED(ZEND_VM_STACK_ELEMETS(EG(argument_stack)) == (void**)ptr)) {
00254               zend_vm_stack p = EG(argument_stack);
00255 
00256               EG(argument_stack) = p->prev;
00257               efree(p);
00258        } else {
00259               EG(argument_stack)->top = (void**)ptr;
00260        }
00261 }
00262 
00263 static inline void zend_vm_stack_free(void *ptr TSRMLS_DC)
00264 {      
00265        if (UNEXPECTED(ZEND_VM_STACK_ELEMETS(EG(argument_stack)) == (void**)ptr)) {
00266               zend_vm_stack p = EG(argument_stack);
00267 
00268               EG(argument_stack) = p->prev;
00269               efree(p);
00270        } else {
00271               /* the following comparison must be optimized out at compile time */
00272               if (ZEND_MM_ALIGNMENT > sizeof(void*)) {
00273                      ptr = (void*)(((char*)ptr) - ZEND_MM_ALIGNED_SIZE(sizeof(void*)));
00274                      EG(argument_stack)->top = *(void***)ptr;
00275               } else {
00276                      EG(argument_stack)->top = (void**)ptr;
00277               }
00278        }
00279 }
00280 
00281 static inline void** zend_vm_stack_push_args(int count TSRMLS_DC)
00282 {
00283 
00284        if (UNEXPECTED(EG(argument_stack)->top - ZEND_VM_STACK_ELEMETS(EG(argument_stack)) < count)  || 
00285               UNEXPECTED(EG(argument_stack)->top == EG(argument_stack)->end)) {
00286               zend_vm_stack p = EG(argument_stack);
00287 
00288               zend_vm_stack_extend(count + 1 TSRMLS_CC);
00289 
00290               EG(argument_stack)->top += count;
00291               *(EG(argument_stack)->top) = (void*)(zend_uintptr_t)count;
00292               while (count-- > 0) {
00293                      void *data = *(--p->top);
00294 
00295                      if (UNEXPECTED(p->top == ZEND_VM_STACK_ELEMETS(p))) {
00296                             zend_vm_stack r = p;
00297 
00298                             EG(argument_stack)->prev = p->prev;
00299                             p = p->prev;
00300                             efree(r);
00301                      }
00302                      *(ZEND_VM_STACK_ELEMETS(EG(argument_stack)) + count) = data;
00303               }
00304               return EG(argument_stack)->top++;
00305        }
00306        *(EG(argument_stack)->top) = (void*)(zend_uintptr_t)count;
00307        return EG(argument_stack)->top++;
00308 }
00309 
00310 static inline void zend_vm_stack_clear_multiple(TSRMLS_D)
00311 {
00312        void **p = EG(argument_stack)->top - 1;
00313        int delete_count = (int)(zend_uintptr_t) *p;
00314 
00315        while (--delete_count>=0) {
00316               zval *q = *(zval **)(--p);
00317               *p = NULL;
00318               zval_ptr_dtor(&q);
00319        }
00320        zend_vm_stack_free_int(p TSRMLS_CC);
00321 }
00322 
00323 static inline zval** zend_vm_stack_get_arg(int requested_arg TSRMLS_DC)
00324 {
00325        void **p = EG(current_execute_data)->prev_execute_data->function_state.arguments;
00326        int arg_count = (int)(zend_uintptr_t) *p;
00327 
00328        if (UNEXPECTED(requested_arg > arg_count)) {
00329               return NULL;
00330        }
00331        return (zval**)p - arg_count + requested_arg - 1;
00332 }
00333 
00334 static inline void zend_arg_types_stack_2_pop(zend_ptr_stack *stack, zval **object, zend_function **fbc)
00335 {
00336        void *a, *b;
00337 
00338        zend_ptr_stack_2_pop(stack, &a, &b);
00339 
00340        *object = (zval *) a;
00341        *fbc = (zend_function *) b;
00342 }
00343 
00344 static inline void zend_arg_types_stack_3_pop(zend_ptr_stack *stack, zend_class_entry **called_scope, zval **object, zend_function **fbc)
00345 {
00346        void *a, *b, *c;
00347 
00348        zend_ptr_stack_3_pop(stack, &a, &b, &c);
00349 
00350        *called_scope = (zend_class_entry *) a;
00351        *object = (zval *) b;
00352        *fbc = (zend_function *) c;
00353 }
00354 
00355 void execute_new_code(TSRMLS_D);
00356 
00357 
00358 /* services */
00359 ZEND_API char *get_active_class_name(char **space TSRMLS_DC);
00360 ZEND_API char *get_active_function_name(TSRMLS_D);
00361 ZEND_API char *zend_get_executed_filename(TSRMLS_D);
00362 ZEND_API uint zend_get_executed_lineno(TSRMLS_D);
00363 ZEND_API zend_bool zend_is_executing(TSRMLS_D);
00364 
00365 ZEND_API void zend_set_timeout(long seconds, int reset_signals);
00366 ZEND_API void zend_unset_timeout(TSRMLS_D);
00367 ZEND_API void zend_timeout(int dummy);
00368 ZEND_API zend_class_entry *zend_fetch_class(const char *class_name, uint class_name_len, int fetch_type TSRMLS_DC);
00369 void zend_verify_abstract_class(zend_class_entry *ce TSRMLS_DC);
00370 
00371 #ifdef ZEND_WIN32
00372 void zend_init_timeout_thread(void);
00373 void zend_shutdown_timeout_thread(void);
00374 #define WM_REGISTER_ZEND_TIMEOUT          (WM_USER+1)
00375 #define WM_UNREGISTER_ZEND_TIMEOUT        (WM_USER+2)
00376 #endif
00377 
00378 #define zendi_zval_copy_ctor(p) zval_copy_ctor(&(p))
00379 #define zendi_zval_dtor(p) zval_dtor(&(p))
00380 
00381 #define active_opline (*EG(opline_ptr))
00382 
00383 /* The following tries to resolve the classname of a zval of type object.
00384  * Since it is slow it should be only used in error messages.
00385  */
00386 #define Z_OBJ_CLASS_NAME_P(zval) ((zval) && Z_TYPE_P(zval) == IS_OBJECT && Z_OBJ_HT_P(zval)->get_class_entry != NULL && Z_OBJ_HT_P(zval)->get_class_entry(zval TSRMLS_CC) ? Z_OBJ_HT_P(zval)->get_class_entry(zval TSRMLS_CC)->name : "")
00387 
00388 ZEND_API zval** zend_get_compiled_variable_value(const zend_execute_data *execute_data_ptr, zend_uint var);
00389 
00390 #define ZEND_USER_OPCODE_CONTINUE   0 /* execute next opcode */
00391 #define ZEND_USER_OPCODE_RETURN     1 /* exit from executor (return from function) */
00392 #define ZEND_USER_OPCODE_DISPATCH   2 /* call original opcode handler */
00393 #define ZEND_USER_OPCODE_ENTER      3 /* enter into new op_array without recursion */
00394 #define ZEND_USER_OPCODE_LEAVE      4 /* return to calling op_array within the same executor */
00395 
00396 #define ZEND_USER_OPCODE_DISPATCH_TO 0x100 /* call original handler of returned opcode */
00397 
00398 ZEND_API int zend_set_user_opcode_handler(zend_uchar opcode, user_opcode_handler_t handler);
00399 ZEND_API user_opcode_handler_t zend_get_user_opcode_handler(zend_uchar opcode);
00400 
00401 /* former zend_execute_locks.h */
00402 typedef struct _zend_free_op {
00403        zval* var;
00404 /*     int   is_var; */
00405 } zend_free_op;
00406 
00407 ZEND_API zval *zend_get_zval_ptr(znode *node, const temp_variable *Ts, zend_free_op *should_free, int type TSRMLS_DC);
00408 ZEND_API zval **zend_get_zval_ptr_ptr(const znode *node, const temp_variable *Ts, zend_free_op *should_free, int type TSRMLS_DC);
00409 
00410 ZEND_API int zend_do_fcall(ZEND_OPCODE_HANDLER_ARGS);
00411 
00412 END_EXTERN_C()
00413 
00414 #endif /* ZEND_EXECUTE_H */
00415 
00416 /*
00417  * Local variables:
00418  * tab-width: 4
00419  * c-basic-offset: 4
00420  * indent-tabs-mode: t
00421  * End:
00422  */