php-src/Zend/zend_opcode.c

894 lines
26 KiB
C
Raw Normal View History

1999-04-07 18:10:10 +00:00
/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
2015-01-15 15:27:30 +00:00
| Copyright (c) 1998-2015 Zend Technologies Ltd. (http://www.zend.com) |
1999-04-07 18:10:10 +00:00
+----------------------------------------------------------------------+
2001-12-11 15:16:21 +00:00
| This source file is subject to version 2.00 of the Zend license, |
2015-01-03 09:22:58 +00:00
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
2001-12-11 15:16:21 +00:00
| http://www.zend.com/license/2_00.txt. |
1999-07-16 14:58:16 +00:00
| If you did not receive a copy of the Zend license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@zend.com so we can mail you a copy immediately. |
1999-04-07 18:10:10 +00:00
+----------------------------------------------------------------------+
| Authors: Andi Gutmans <andi@zend.com> |
| Zeev Suraski <zeev@zend.com> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
1999-07-16 14:58:16 +00:00
1999-04-07 18:10:10 +00:00
#include <stdio.h>
#include "zend.h"
#include "zend_alloc.h"
#include "zend_compile.h"
#include "zend_extensions.h"
#include "zend_API.h"
#include "zend_vm.h"
2014-12-13 22:06:14 +00:00
static void zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array)
1999-04-07 18:10:10 +00:00
{
if (extension->op_array_ctor) {
extension->op_array_ctor(op_array);
1999-04-07 18:10:10 +00:00
}
}
2014-12-13 22:06:14 +00:00
static void zend_extension_op_array_dtor_handler(zend_extension *extension, zend_op_array *op_array)
1999-04-07 18:10:10 +00:00
{
if (extension->op_array_dtor) {
extension->op_array_dtor(op_array);
1999-04-07 18:10:10 +00:00
}
}
2014-08-25 17:28:33 +00:00
static void op_array_alloc_ops(zend_op_array *op_array, uint32_t size)
1999-04-07 18:10:10 +00:00
{
op_array->opcodes = erealloc(op_array->opcodes, size * sizeof(zend_op));
1999-04-07 18:10:10 +00:00
}
2014-12-13 22:06:14 +00:00
void init_op_array(zend_op_array *op_array, zend_uchar type, int initial_ops_size)
1999-04-07 18:10:10 +00:00
{
2000-04-29 01:30:17 +00:00
op_array->type = type;
2014-08-25 17:28:33 +00:00
op_array->refcount = (uint32_t *) emalloc(sizeof(uint32_t));
1999-04-07 18:10:10 +00:00
*op_array->refcount = 1;
op_array->last = 0;
op_array->opcodes = NULL;
op_array_alloc_ops(op_array, initial_ops_size);
1999-04-07 18:10:10 +00:00
op_array->last_var = 0;
op_array->vars = NULL;
1999-04-07 18:10:10 +00:00
op_array->T = 0;
op_array->function_name = NULL;
2014-12-13 22:06:14 +00:00
op_array->filename = zend_get_compiled_filename();
2004-02-20 06:59:37 +00:00
op_array->doc_comment = NULL;
1999-04-07 18:10:10 +00:00
op_array->arg_info = NULL;
op_array->num_args = 0;
op_array->required_num_args = 0;
1999-04-07 18:10:10 +00:00
op_array->scope = NULL;
op_array->prototype = NULL;
1999-04-07 18:10:10 +00:00
op_array->brk_cont_array = NULL;
op_array->try_catch_array = NULL;
1999-04-07 18:10:10 +00:00
op_array->last_brk_cont = 0;
op_array->static_variables = NULL;
op_array->last_try_catch = 0;
1999-04-07 18:10:10 +00:00
op_array->this_var = -1;
op_array->fn_flags = 0;
op_array->early_binding = -1;
op_array->last_literal = 0;
op_array->literals = NULL;
op_array->run_time_cache = NULL;
op_array->last_cache_slot = 0;
memset(op_array->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
2014-12-13 22:06:14 +00:00
zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_ctor_handler, op_array);
1999-04-07 18:10:10 +00:00
}
2014-12-13 22:06:14 +00:00
ZEND_API void destroy_zend_function(zend_function *function)
1999-04-07 18:10:10 +00:00
{
if (function->type == ZEND_USER_FUNCTION) {
2014-12-13 22:06:14 +00:00
destroy_op_array(&function->op_array);
} else {
ZEND_ASSERT(function->type == ZEND_INTERNAL_FUNCTION);
ZEND_ASSERT(function->common.function_name);
zend_string_release(function->common.function_name);
1999-04-07 18:10:10 +00:00
}
}
ZEND_API void zend_function_dtor(zval *zv)
{
zend_function *function = Z_PTR_P(zv);
if (function->type == ZEND_USER_FUNCTION) {
ZEND_ASSERT(function->common.function_name);
2014-12-13 22:06:14 +00:00
destroy_op_array(&function->op_array);
/* op_arrays are allocated on arena, so we don't have to free them */
//??? efree_size(function, sizeof(zend_op_array));
} else {
ZEND_ASSERT(function->type == ZEND_INTERNAL_FUNCTION);
ZEND_ASSERT(function->common.function_name);
zend_string_release(function->common.function_name);
if (!(function->common.fn_flags & ZEND_ACC_ARENA_ALLOCATED)) {
pefree(function, 1);
}
}
}
ZEND_API void zend_cleanup_op_array_data(zend_op_array *op_array)
{
if (op_array->static_variables &&
!(GC_FLAGS(op_array->static_variables) & IS_ARRAY_IMMUTABLE)) {
zend_hash_clean(op_array->static_variables);
}
}
2014-12-13 22:06:14 +00:00
ZEND_API void zend_cleanup_user_class_data(zend_class_entry *ce)
{
/* Clean all parts that can contain run-time data */
/* Note that only run-time accessed data need to be cleaned up, pre-defined data can
not contain objects and thus are not probelmatic */
if (ce->ce_flags & ZEND_HAS_STATIC_IN_METHODS) {
zend_function *func;
ZEND_HASH_FOREACH_PTR(&ce->function_table, func) {
if (func->type == ZEND_USER_FUNCTION) {
zend_cleanup_op_array_data((zend_op_array *) func);
}
} ZEND_HASH_FOREACH_END();
}
if (ce->static_members_table) {
zval *static_members = ce->static_members_table;
int count = ce->default_static_members_count;
int i;
ce->default_static_members_count = 0;
ce->default_static_members_table = ce->static_members_table = NULL;
for (i = 0; i < count; i++) {
zval_ptr_dtor(&static_members[i]);
}
efree(static_members);
}
}
2014-12-13 22:06:14 +00:00
ZEND_API void zend_cleanup_internal_class_data(zend_class_entry *ce)
{
if (CE_STATIC_MEMBERS(ce)) {
zval *static_members = CE_STATIC_MEMBERS(ce);
int i;
2015-01-03 09:22:58 +00:00
#ifdef ZTS
2010-07-06 15:52:39 +00:00
CG(static_members_table)[(zend_intptr_t)(ce->static_members_table)] = NULL;
#else
ce->static_members_table = NULL;
#endif
for (i = 0; i < ce->default_static_members_count; i++) {
zval_ptr_dtor(&static_members[i]);
}
efree(static_members);
}
}
void _destroy_zend_class_traits_info(zend_class_entry *ce)
{
if (ce->num_traits > 0 && ce->traits) {
efree(ce->traits);
}
2015-01-03 09:22:58 +00:00
if (ce->trait_aliases) {
size_t i = 0;
while (ce->trait_aliases[i]) {
if (ce->trait_aliases[i]->trait_method) {
if (ce->trait_aliases[i]->trait_method->method_name) {
2014-08-25 17:24:55 +00:00
zend_string_release(ce->trait_aliases[i]->trait_method->method_name);
}
if (ce->trait_aliases[i]->trait_method->class_name) {
2014-08-25 17:24:55 +00:00
zend_string_release(ce->trait_aliases[i]->trait_method->class_name);
}
efree(ce->trait_aliases[i]->trait_method);
}
2015-01-03 09:22:58 +00:00
if (ce->trait_aliases[i]->alias) {
2014-08-25 17:24:55 +00:00
zend_string_release(ce->trait_aliases[i]->alias);
}
2015-01-03 09:22:58 +00:00
efree(ce->trait_aliases[i]);
i++;
}
2015-01-03 09:22:58 +00:00
efree(ce->trait_aliases);
}
if (ce->trait_precedences) {
size_t i = 0;
2015-01-03 09:22:58 +00:00
while (ce->trait_precedences[i]) {
2014-08-25 17:24:55 +00:00
zend_string_release(ce->trait_precedences[i]->trait_method->method_name);
zend_string_release(ce->trait_precedences[i]->trait_method->class_name);
efree(ce->trait_precedences[i]->trait_method);
if (ce->trait_precedences[i]->exclude_from_classes) {
efree(ce->trait_precedences[i]->exclude_from_classes);
}
efree(ce->trait_precedences[i]);
i++;
}
efree(ce->trait_precedences);
}
}
ZEND_API void destroy_zend_class(zval *zv)
1999-04-07 18:10:10 +00:00
{
zend_class_entry *ce = Z_PTR_P(zv);
2015-01-03 09:22:58 +00:00
if (--ce->refcount > 0) {
return;
}
1999-04-07 18:10:10 +00:00
switch (ce->type) {
case ZEND_USER_CLASS:
if (ce->default_properties_table) {
int i;
for (i = 0; i < ce->default_properties_count; i++) {
if (Z_TYPE(ce->default_properties_table[i]) != IS_UNDEF) {
zval_ptr_dtor(&ce->default_properties_table[i]);
}
}
efree(ce->default_properties_table);
}
if (ce->default_static_members_table) {
int i;
for (i = 0; i < ce->default_static_members_count; i++) {
if (Z_TYPE(ce->default_static_members_table[i]) != IS_UNDEF) {
zval_ptr_dtor(&ce->default_static_members_table[i]);
}
}
efree(ce->default_static_members_table);
}
zend_hash_destroy(&ce->properties_info);
2014-08-25 17:24:55 +00:00
zend_string_release(ce->name);
1999-04-07 18:10:10 +00:00
zend_hash_destroy(&ce->function_table);
zend_hash_destroy(&ce->constants_table);
if (ce->num_interfaces > 0 && ce->interfaces) {
2003-03-05 11:14:44 +00:00
efree(ce->interfaces);
}
if (ce->info.user.doc_comment) {
2014-08-25 17:24:55 +00:00
zend_string_release(ce->info.user.doc_comment);
}
2015-01-03 09:22:58 +00:00
_destroy_zend_class_traits_info(ce);
2015-01-03 09:22:58 +00:00
1999-04-07 18:10:10 +00:00
break;
case ZEND_INTERNAL_CLASS:
if (ce->default_properties_table) {
int i;
for (i = 0; i < ce->default_properties_count; i++) {
if (Z_TYPE(ce->default_properties_table[i]) != IS_UNDEF) {
zval_internal_ptr_dtor(&ce->default_properties_table[i]);
}
}
free(ce->default_properties_table);
}
if (ce->default_static_members_table) {
int i;
for (i = 0; i < ce->default_static_members_count; i++) {
zval_internal_ptr_dtor(&ce->default_static_members_table[i]);
}
free(ce->default_static_members_table);
}
zend_hash_destroy(&ce->properties_info);
2014-08-25 17:24:55 +00:00
zend_string_release(ce->name);
1999-04-07 18:10:10 +00:00
zend_hash_destroy(&ce->function_table);
zend_hash_destroy(&ce->constants_table);
if (ce->num_interfaces > 0) {
free(ce->interfaces);
}
free(ce);
1999-04-07 18:10:10 +00:00
break;
}
}
1999-04-07 18:10:10 +00:00
void zend_class_add_ref(zval *zv)
{
zend_class_entry *ce = Z_PTR_P(zv);
ce->refcount++;
}
2014-12-13 22:06:14 +00:00
ZEND_API void destroy_op_array(zend_op_array *op_array)
1999-04-07 18:10:10 +00:00
{
zval *literal = op_array->literals;
zval *end;
2014-08-25 17:28:33 +00:00
uint32_t i;
1999-04-07 18:10:10 +00:00
if (op_array->static_variables &&
!(GC_FLAGS(op_array->static_variables) & IS_ARRAY_IMMUTABLE)) {
if (--GC_REFCOUNT(op_array->static_variables) == 0) {
zend_array_destroy(op_array->static_variables);
}
}
if (op_array->run_time_cache && !op_array->function_name) {
efree(op_array->run_time_cache);
}
if (!op_array->refcount || --(*op_array->refcount)>0) {
1999-04-07 18:10:10 +00:00
return;
}
efree_size(op_array->refcount, sizeof(*(op_array->refcount)));
1999-04-07 18:10:10 +00:00
if (op_array->vars) {
i = op_array->last_var;
while (i > 0) {
i--;
2014-08-25 17:24:55 +00:00
zend_string_release(op_array->vars[i]);
}
efree(op_array->vars);
}
if (literal) {
end = literal + op_array->last_literal;
while (literal < end) {
zval_ptr_dtor_nogc(literal);
literal++;
1999-04-07 18:10:10 +00:00
}
efree(op_array->literals);
1999-04-07 18:10:10 +00:00
}
efree(op_array->opcodes);
2004-07-29 15:23:47 +00:00
2012-12-25 06:23:08 +00:00
if (op_array->function_name) {
2014-08-25 17:24:55 +00:00
zend_string_release(op_array->function_name);
1999-04-07 18:10:10 +00:00
}
2004-02-20 06:59:37 +00:00
if (op_array->doc_comment) {
2014-08-25 17:24:55 +00:00
zend_string_release(op_array->doc_comment);
2004-02-20 06:59:37 +00:00
}
1999-04-07 18:10:10 +00:00
if (op_array->brk_cont_array) {
efree(op_array->brk_cont_array);
}
if (op_array->try_catch_array) {
efree(op_array->try_catch_array);
}
if (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) {
2014-12-13 22:06:14 +00:00
zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_dtor_handler, op_array);
}
if (op_array->arg_info) {
int32_t num_args = op_array->num_args;
zend_arg_info *arg_info = op_array->arg_info;
int32_t i;
if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
arg_info--;
num_args++;
}
if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
num_args++;
}
for (i = 0 ; i < num_args; i++) {
if (arg_info[i].name) {
zend_string_release(arg_info[i].name);
}
if (arg_info[i].class_name) {
zend_string_release(arg_info[i].class_name);
2003-08-03 22:28:14 +00:00
}
}
efree(arg_info);
}
1999-04-07 18:10:10 +00:00
}
2014-12-13 22:06:14 +00:00
void init_op(zend_op *op)
{
2004-02-04 15:51:07 +00:00
memset(op, 0, sizeof(zend_op));
op->lineno = CG(zend_lineno);
SET_UNUSED(op->result);
}
2014-12-13 22:06:14 +00:00
zend_op *get_next_op(zend_op_array *op_array)
1999-04-07 18:10:10 +00:00
{
2014-08-25 17:28:33 +00:00
uint32_t next_op_num = op_array->last++;
1999-04-07 18:10:10 +00:00
zend_op *next_op;
if (next_op_num >= CG(context).opcodes_size) {
CG(context).opcodes_size *= 4;
op_array_alloc_ops(op_array, CG(context).opcodes_size);
1999-04-07 18:10:10 +00:00
}
2015-01-03 09:22:58 +00:00
1999-04-07 18:10:10 +00:00
next_op = &(op_array->opcodes[next_op_num]);
2015-01-03 09:22:58 +00:00
2014-12-13 22:06:14 +00:00
init_op(next_op);
1999-04-07 18:10:10 +00:00
return next_op;
}
int get_next_op_number(zend_op_array *op_array)
{
return op_array->last;
}
zend_brk_cont_element *get_next_brk_cont_element(zend_op_array *op_array)
{
op_array->last_brk_cont++;
op_array->brk_cont_array = erealloc(op_array->brk_cont_array, sizeof(zend_brk_cont_element)*op_array->last_brk_cont);
return &op_array->brk_cont_array[op_array->last_brk_cont-1];
}
2014-12-13 22:06:14 +00:00
static void zend_update_extended_info(zend_op_array *op_array)
1999-04-07 18:10:10 +00:00
{
zend_op *opline = op_array->opcodes, *end=opline+op_array->last;
while (opline<end) {
if (opline->opcode == ZEND_EXT_STMT) {
if (opline+1<end) {
if ((opline+1)->opcode == ZEND_EXT_STMT) {
opline->opcode = ZEND_NOP;
opline++;
continue;
}
if (opline+1<end) {
opline->lineno = (opline+1)->lineno;
}
1999-04-07 18:10:10 +00:00
} else {
opline->opcode = ZEND_NOP;
}
}
opline++;
}
}
2014-12-13 22:06:14 +00:00
static void zend_extension_op_array_handler(zend_extension *extension, zend_op_array *op_array)
1999-04-07 18:10:10 +00:00
{
if (extension->op_array_handler) {
extension->op_array_handler(op_array);
}
}
2014-12-13 22:06:14 +00:00
static void zend_check_finally_breakout(zend_op_array *op_array, uint32_t op_num, uint32_t dst_num)
2012-11-22 11:17:05 +00:00
{
2014-09-15 14:52:19 +00:00
int i;
2012-11-22 11:17:05 +00:00
for (i = 0; i < op_array->last_try_catch; i++) {
if ((op_num < op_array->try_catch_array[i].finally_op ||
op_num >= op_array->try_catch_array[i].finally_end)
&& (dst_num >= op_array->try_catch_array[i].finally_op &&
dst_num <= op_array->try_catch_array[i].finally_end)) {
CG(in_compilation) = 1;
CG(active_op_array) = op_array;
CG(zend_lineno) = op_array->opcodes[op_num].lineno;
2013-12-13 02:56:35 +00:00
zend_error_noreturn(E_COMPILE_ERROR, "jump into a finally block is disallowed");
2015-01-03 09:22:58 +00:00
} else if ((op_num >= op_array->try_catch_array[i].finally_op
2012-11-22 11:17:05 +00:00
&& op_num <= op_array->try_catch_array[i].finally_end)
2015-01-03 09:22:58 +00:00
&& (dst_num > op_array->try_catch_array[i].finally_end
|| dst_num < op_array->try_catch_array[i].finally_op)) {
CG(in_compilation) = 1;
CG(active_op_array) = op_array;
2012-11-22 11:17:05 +00:00
CG(zend_lineno) = op_array->opcodes[op_num].lineno;
zend_error_noreturn(E_COMPILE_ERROR, "jump out of a finally block is disallowed");
}
2015-01-03 09:22:58 +00:00
}
}
2014-12-13 22:06:14 +00:00
static void zend_adjust_fast_call(zend_op_array *op_array, uint32_t fast_call, uint32_t start, uint32_t end)
2014-07-19 09:19:01 +00:00
{
int i;
2014-08-25 17:28:33 +00:00
uint32_t op_num = 0;
2014-07-19 09:19:01 +00:00
for (i = 0; i < op_array->last_try_catch; i++) {
2015-01-03 09:22:58 +00:00
if (op_array->try_catch_array[i].finally_op > start
2014-07-19 09:19:01 +00:00
&& op_array->try_catch_array[i].finally_end < end) {
op_num = op_array->try_catch_array[i].finally_op;
start = op_array->try_catch_array[i].finally_end;
}
}
if (op_num) {
/* Must be ZEND_FAST_CALL */
ZEND_ASSERT(op_array->opcodes[op_num - 2].opcode == ZEND_FAST_CALL);
op_array->opcodes[op_num - 2].extended_value = ZEND_FAST_CALL_FROM_FINALLY;
op_array->opcodes[op_num - 2].op2.opline_num = fast_call;
}
}
2014-12-13 22:06:14 +00:00
static void zend_resolve_fast_call(zend_op_array *op_array, uint32_t fast_call, uint32_t op_num)
2014-07-19 09:19:01 +00:00
{
int i;
2014-08-25 17:28:33 +00:00
uint32_t finally_op_num = 0;
2014-07-19 09:19:01 +00:00
for (i = 0; i < op_array->last_try_catch; i++) {
if (op_num >= op_array->try_catch_array[i].finally_op
&& op_num < op_array->try_catch_array[i].finally_end) {
finally_op_num = op_array->try_catch_array[i].finally_op;
}
}
if (finally_op_num) {
/* Must be ZEND_FAST_CALL */
ZEND_ASSERT(op_array->opcodes[finally_op_num - 2].opcode == ZEND_FAST_CALL);
if (op_array->opcodes[fast_call].extended_value == 0) {
op_array->opcodes[fast_call].extended_value = ZEND_FAST_CALL_FROM_FINALLY;
op_array->opcodes[fast_call].op2.opline_num = finally_op_num - 2;
}
2015-01-03 09:22:58 +00:00
}
2014-07-19 09:19:01 +00:00
}
2014-12-13 22:06:14 +00:00
static void zend_resolve_finally_call(zend_op_array *op_array, uint32_t op_num, uint32_t dst_num)
2012-11-22 11:17:05 +00:00
{
2014-08-25 17:28:33 +00:00
uint32_t start_op;
2012-11-22 11:17:05 +00:00
zend_op *opline;
2014-08-25 17:28:33 +00:00
uint32_t i = op_array->last_try_catch;
2012-11-22 11:17:05 +00:00
2014-08-25 17:28:33 +00:00
if (dst_num != (uint32_t)-1) {
2014-12-13 22:06:14 +00:00
zend_check_finally_breakout(op_array, op_num, dst_num);
2012-11-22 11:17:05 +00:00
}
/* the backward order is mater */
while (i > 0) {
i--;
if (op_array->try_catch_array[i].finally_op &&
op_num >= op_array->try_catch_array[i].try_op &&
op_num < op_array->try_catch_array[i].finally_op - 1 &&
(dst_num < op_array->try_catch_array[i].try_op ||
dst_num > op_array->try_catch_array[i].finally_end)) {
/* we have a jump out of try block that needs executing finally */
uint32_t fast_call_var;
2015-01-03 09:22:58 +00:00
/* Must be ZEND_FAST_RET */
ZEND_ASSERT(op_array->opcodes[op_array->try_catch_array[i].finally_end].opcode == ZEND_FAST_RET);
fast_call_var = op_array->opcodes[op_array->try_catch_array[i].finally_end].op1.var;
2012-11-22 11:17:05 +00:00
/* generate a FAST_CALL to finally block */
2012-11-22 11:17:05 +00:00
start_op = get_next_op_number(op_array);
2014-12-13 22:06:14 +00:00
opline = get_next_op(op_array);
2012-11-22 11:17:05 +00:00
opline->opcode = ZEND_FAST_CALL;
opline->result_type = IS_TMP_VAR;
opline->result.var = fast_call_var;
2012-11-22 11:17:05 +00:00
SET_UNUSED(opline->op1);
SET_UNUSED(opline->op2);
2014-07-19 09:19:01 +00:00
zend_adjust_fast_call(op_array, start_op,
op_array->try_catch_array[i].finally_op,
2014-12-13 22:06:14 +00:00
op_array->try_catch_array[i].finally_end);
2012-11-22 11:17:05 +00:00
if (op_array->try_catch_array[i].catch_op) {
2014-07-19 09:19:01 +00:00
opline->extended_value = ZEND_FAST_CALL_FROM_CATCH;
2012-11-22 11:17:05 +00:00
opline->op2.opline_num = op_array->try_catch_array[i].catch_op;
2014-07-19 09:19:01 +00:00
opline->op1.opline_num = get_next_op_number(op_array);
/* generate a FAST_CALL to hole CALL_FROM_FINALLY */
2014-12-13 22:06:14 +00:00
opline = get_next_op(op_array);
2014-07-19 09:19:01 +00:00
opline->opcode = ZEND_FAST_CALL;
opline->result_type = IS_TMP_VAR;
opline->result.var = fast_call_var;
2014-07-19 09:19:01 +00:00
SET_UNUSED(opline->op1);
SET_UNUSED(opline->op2);
2014-12-13 22:06:14 +00:00
zend_resolve_fast_call(op_array, start_op + 1, op_array->try_catch_array[i].finally_op - 2);
2014-07-19 09:19:01 +00:00
} else {
2014-12-13 22:06:14 +00:00
zend_resolve_fast_call(op_array, start_op, op_array->try_catch_array[i].finally_op - 2);
2012-11-22 11:17:05 +00:00
}
2014-07-19 09:19:01 +00:00
opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
2012-11-22 11:17:05 +00:00
/* generate a sequence of FAST_CALL to upward finally block */
2012-11-22 11:17:05 +00:00
while (i > 0) {
i--;
if (op_array->try_catch_array[i].finally_op &&
op_num >= op_array->try_catch_array[i].try_op &&
op_num < op_array->try_catch_array[i].finally_op - 1 &&
(dst_num < op_array->try_catch_array[i].try_op ||
dst_num > op_array->try_catch_array[i].finally_end)) {
2014-12-13 22:06:14 +00:00
opline = get_next_op(op_array);
2012-11-22 11:17:05 +00:00
opline->opcode = ZEND_FAST_CALL;
opline->result_type = IS_TMP_VAR;
opline->result.var = fast_call_var;
2012-11-22 11:17:05 +00:00
SET_UNUSED(opline->op1);
SET_UNUSED(opline->op2);
opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
}
}
/* Finish the sequence with original opcode */
2014-12-13 22:06:14 +00:00
opline = get_next_op(op_array);
2012-11-22 11:17:05 +00:00
*opline = op_array->opcodes[op_num];
/* Replace original opcode with jump to this sequence */
opline = op_array->opcodes + op_num;
opline->opcode = ZEND_JMP;
SET_UNUSED(opline->op1);
SET_UNUSED(opline->op2);
opline->op1.opline_num = start_op;
break;
2012-11-22 11:17:05 +00:00
}
2015-01-03 09:22:58 +00:00
}
2012-11-22 11:17:05 +00:00
}
2014-12-13 22:06:14 +00:00
static void zend_resolve_finally_ret(zend_op_array *op_array, uint32_t op_num)
2012-11-22 11:17:05 +00:00
{
int i;
2014-08-25 17:28:33 +00:00
uint32_t catch_op_num = 0, finally_op_num = 0;
2012-11-22 11:17:05 +00:00
for (i = 0; i < op_array->last_try_catch; i++) {
if (op_array->try_catch_array[i].try_op > op_num) {
break;
}
if (op_num < op_array->try_catch_array[i].finally_op) {
finally_op_num = op_array->try_catch_array[i].finally_op;
}
if (op_num < op_array->try_catch_array[i].catch_op) {
catch_op_num = op_array->try_catch_array[i].catch_op;
}
}
if (finally_op_num && (!catch_op_num || catch_op_num >= finally_op_num)) {
/* in case of unhandled exception return to upward finally block */
op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_FINALLY;
op_array->opcodes[op_num].op2.opline_num = finally_op_num;
} else if (catch_op_num) {
/* in case of unhandled exception return to upward catch block */
op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_CATCH;
op_array->opcodes[op_num].op2.opline_num = catch_op_num;
}
}
2014-12-13 22:06:14 +00:00
static void zend_resolve_finally_calls(zend_op_array *op_array)
2012-11-22 11:17:05 +00:00
{
2014-08-25 17:28:33 +00:00
uint32_t i, j;
2012-11-22 11:17:05 +00:00
zend_op *opline;
for (i = 0, j = op_array->last; i < j; i++) {
2012-11-22 11:17:05 +00:00
opline = op_array->opcodes + i;
switch (opline->opcode) {
case ZEND_RETURN:
case ZEND_RETURN_BY_REF:
case ZEND_GENERATOR_RETURN:
2014-12-13 22:06:14 +00:00
zend_resolve_finally_call(op_array, i, (uint32_t)-1);
2012-11-22 11:17:05 +00:00
break;
case ZEND_BRK:
case ZEND_CONT:
{
int nest_levels, array_offset;
zend_brk_cont_element *jmp_to;
nest_levels = Z_LVAL(op_array->literals[opline->op2.constant]);
2014-07-19 07:30:50 +00:00
if ((array_offset = opline->op1.opline_num) != -1) {
do {
jmp_to = &op_array->brk_cont_array[array_offset];
if (nest_levels > 1) {
array_offset = jmp_to->parent;
}
} while (--nest_levels > 0);
2014-12-13 22:06:14 +00:00
zend_resolve_finally_call(op_array, i, opline->opcode == ZEND_BRK ? jmp_to->brk : jmp_to->cont);
2014-07-19 07:30:50 +00:00
break;
}
2012-11-22 11:17:05 +00:00
}
case ZEND_GOTO:
if (Z_TYPE_P(CT_CONSTANT_EX(op_array, opline->op2.constant)) != IS_LONG) {
2014-08-25 17:28:33 +00:00
uint32_t num = opline->op2.constant;
ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op2);
2014-12-13 22:06:14 +00:00
zend_resolve_goto_label(op_array, opline, 1);
2015-01-03 09:22:58 +00:00
opline->op2.constant = num;
2012-11-22 11:17:05 +00:00
}
/* break omitted intentionally */
case ZEND_JMP:
2014-12-13 22:06:14 +00:00
zend_resolve_finally_call(op_array, i, opline->op1.opline_num);
2012-11-22 11:17:05 +00:00
break;
case ZEND_FAST_CALL:
2014-12-13 22:06:14 +00:00
zend_resolve_fast_call(op_array, i, i);
break;
2012-11-22 11:17:05 +00:00
case ZEND_FAST_RET:
2014-12-13 22:06:14 +00:00
zend_resolve_finally_ret(op_array, i);
2012-11-22 11:17:05 +00:00
break;
default:
break;
}
}
}
2014-12-13 22:06:14 +00:00
ZEND_API int pass_two(zend_op_array *op_array)
1999-04-07 18:10:10 +00:00
{
zend_op *opline, *end;
1999-04-07 18:10:10 +00:00
if (!ZEND_USER_CODE(op_array->type)) {
1999-04-07 18:10:10 +00:00
return 0;
}
if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
2014-12-13 22:06:14 +00:00
zend_resolve_finally_calls(op_array);
2012-11-22 11:17:05 +00:00
}
if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) {
2014-12-13 22:06:14 +00:00
zend_update_extended_info(op_array);
1999-04-07 18:10:10 +00:00
}
if (CG(compiler_options) & ZEND_COMPILE_HANDLE_OP_ARRAY) {
2014-12-13 22:06:14 +00:00
zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_handler, op_array);
1999-04-07 18:10:10 +00:00
}
if (CG(context).vars_size != op_array->last_var) {
op_array->vars = (zend_string**) erealloc(op_array->vars, sizeof(zend_string*)*op_array->last_var);
CG(context).vars_size = op_array->last_var;
}
if (CG(context).opcodes_size != op_array->last) {
op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last);
CG(context).opcodes_size = op_array->last;
}
if (CG(context).literals_size != op_array->last_literal) {
op_array->literals = (zval*)erealloc(op_array->literals, sizeof(zval) * op_array->last_literal);
CG(context).literals_size = op_array->last_literal;
}
opline = op_array->opcodes;
end = opline + op_array->last;
while (opline < end) {
if (opline->op1_type == IS_CONST) {
ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op1);
} else if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
opline->op1.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->op1.var);
1999-04-07 18:10:10 +00:00
}
if (opline->op2_type == IS_CONST) {
ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op2);
} else if (opline->op2_type & (IS_VAR|IS_TMP_VAR)) {
opline->op2.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->op2.var);
}
if (opline->result_type & (IS_VAR|IS_TMP_VAR)) {
opline->result.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->result.var);
1999-04-07 18:10:10 +00:00
}
2002-10-24 18:24:55 +00:00
switch (opline->opcode) {
case ZEND_DECLARE_INHERITED_CLASS:
case ZEND_DECLARE_INHERITED_CLASS_DELAYED:
opline->extended_value = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->extended_value);
break;
case ZEND_GOTO:
if (Z_TYPE_P(RT_CONSTANT(op_array, opline->op2)) != IS_LONG) {
2014-12-13 22:06:14 +00:00
zend_resolve_goto_label(op_array, opline, 1);
}
/* break omitted intentionally */
2002-10-24 18:24:55 +00:00
case ZEND_JMP:
2012-11-22 11:17:05 +00:00
case ZEND_FAST_CALL:
ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op1);
2002-10-24 18:24:55 +00:00
break;
case ZEND_JMPZNZ:
/* absolute index to relative offset */
opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline->extended_value);
/* break omitted intentionally */
2002-10-24 18:24:55 +00:00
case ZEND_JMPZ:
case ZEND_JMPNZ:
case ZEND_JMPZ_EX:
case ZEND_JMPNZ_EX:
case ZEND_JMP_SET:
case ZEND_COALESCE:
case ZEND_NEW:
Fix "forech" statemt behaviour according to https://wiki.php.net/rfc/php7_foreach Squashed commit of the following: commit 1e41295097576dbce6c197ddb7507c07ccae3cbe Author: Dmitry Stogov <dmitry@zend.com> Date: Sat Jan 31 07:28:58 2015 +0300 Generalize HashTableIterator API to allows its usage without involvement of HashTable.nInternalPonter commit 5406f21b11e563069d64045e599693b51c444b63 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:43 2015 +0300 Reduced alghorithms complexity commit b37f1d58d2a141b6e1d980a461ccb588d4317d2e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:30 2015 +0300 Fixed test name commit fb2d079645829b12ed4e55a461034df6400bc430 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 18:08:05 2015 +0300 API cleanup commit 08302c0d6d1cab279b9f2129df03a057baddf2ff Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 14:20:46 2015 +0300 Make array_splice() to preserve foreach hash position commit cc4b7be41e2e2b9b0d7a3c8e98466b8886692e6e Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 12:24:31 2015 +0300 Make internal function, operation on array passed by reference, to preserve foreach hash position commit 5aa9712b0a30303aadfe3bdd8ae1f072ca3e6ba1 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 09:49:35 2015 +0300 Implement consistent behavior for foreach by value over plain object commit 4c5b385ff53ae9f0b52572e98c4db801f56603b0 Author: Dmitry Stogov <dmitry@zend.com> Date: Fri Jan 30 07:56:37 2015 +0300 More careful iterators update. commit 721fc9e80d2ee8f2cd79c8c3cdceffae2c72de92 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:43:28 2015 +0300 Added new test commit 15a23b1218b3e38630d677751a975907daa2cd54 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:05:02 2015 +0300 Reimplement iteration magic with HashTableIterators (see https://wiki.php.net/rfc/php7_foreach#implementation_details) commit 10a3260b1f16b6075fd8140f673dfef4d5efea91 Author: Dmitry Stogov <dmitry@zend.com> Date: Thu Jan 29 21:04:44 2015 +0300 New test commit eef80c583762d1e98d177cdbb27e3a8a6b0c4539 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 16:52:21 2015 +0300 Fixed foreach by reference iteration over constant array commit 61e739187391661e2d541947bec25d7dcc4479f3 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 14:59:54 2015 +0300 Fixed temporary variable re-allocation pass commit 92e90c09f085c22707ff4a59201f016f56e0ef8b Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 12:44:57 2015 +0300 Fixed operand destruction in case of exceptions in iterator commit dd2a36a2074bbb0cb31de00b66dcf2812d6d753f Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 10:02:34 2015 +0300 Use GET_OP1_ZVAL_PTR_DEREF() (IS_TMP_VAR and IS_CONST can't be IS_REFERENCE) commit 4638f7b91407c48710007af82a68da0007c820f2 Author: Dmitry Stogov <dmitry@zend.com> Date: Wed Jan 28 07:43:28 2015 +0300 Change "foreach" statement behavior (this is just a PoC yet) - "foreach by value" don't relay on internal array/object pointer and doesnt perform array duplication. It just locks it incrementing reference counter. If the original array is modified by some code, the copy on write is performed and "foreach" still work with the old copy. - it makes no difference if array given to "foreach by value" is reference itself - "foreach by reference" still use internal array/object pointer and should work similar to PHP-5. (This id not completely implemented)
2015-02-12 10:57:12 +00:00
case ZEND_FE_RESET_R:
case ZEND_FE_RESET_RW:
case ZEND_FE_FETCH_R:
case ZEND_FE_FETCH_RW:
ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2);
2002-10-24 18:24:55 +00:00
break;
case ZEND_VERIFY_RETURN_TYPE:
if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
MAKE_NOP(opline);
}
break;
case ZEND_RETURN:
case ZEND_RETURN_BY_REF:
if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
if (opline->op1_type != IS_CONST || Z_TYPE_P(RT_CONSTANT(op_array, opline->op1)) != IS_NULL) {
CG(zend_lineno) = opline->lineno;
zend_error_noreturn(E_COMPILE_ERROR, "Generators cannot return values using \"return\"");
}
opline->opcode = ZEND_GENERATOR_RETURN;
}
break;
}
ZEND_VM_SET_OPCODE_HANDLER(opline);
1999-04-07 18:10:10 +00:00
opline++;
}
op_array->fn_flags |= ZEND_ACC_DONE_PASS_TWO;
return 0;
1999-04-07 18:10:10 +00:00
}
2014-12-13 22:06:14 +00:00
int pass_two_wrapper(zval *el)
{
2014-12-13 22:06:14 +00:00
return pass_two((zend_op_array *) Z_PTR_P(el));
}
2014-12-13 22:06:14 +00:00
int print_class(zend_class_entry *class_entry)
1999-04-07 18:10:10 +00:00
{
printf("Class %s:\n", class_entry->name->val);
2014-12-13 22:06:14 +00:00
zend_hash_apply(&class_entry->function_table, pass_two_wrapper);
printf("End of class %s.\n\n", class_entry->name->val);
1999-04-07 18:10:10 +00:00
return 0;
}
ZEND_API unary_op_type get_unary_op(int opcode)
1999-04-07 18:10:10 +00:00
{
2002-11-30 11:20:25 +00:00
switch (opcode) {
1999-04-07 18:10:10 +00:00
case ZEND_BW_NOT:
return (unary_op_type) bitwise_not_function;
1999-04-07 18:10:10 +00:00
case ZEND_BOOL_NOT:
return (unary_op_type) boolean_not_function;
1999-04-07 18:10:10 +00:00
default:
return (unary_op_type) NULL;
1999-04-07 18:10:10 +00:00
}
}
ZEND_API binary_op_type get_binary_op(int opcode)
1999-04-07 18:10:10 +00:00
{
switch (opcode) {
case ZEND_ADD:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_ADD:
return (binary_op_type) add_function;
1999-04-07 18:10:10 +00:00
case ZEND_SUB:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_SUB:
return (binary_op_type) sub_function;
1999-04-07 18:10:10 +00:00
case ZEND_MUL:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_MUL:
return (binary_op_type) mul_function;
case ZEND_POW:
return (binary_op_type) pow_function;
1999-04-07 18:10:10 +00:00
case ZEND_DIV:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_DIV:
return (binary_op_type) div_function;
1999-04-07 18:10:10 +00:00
case ZEND_MOD:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_MOD:
return (binary_op_type) mod_function;
1999-04-07 18:10:10 +00:00
case ZEND_SL:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_SL:
return (binary_op_type) shift_left_function;
1999-04-07 18:10:10 +00:00
case ZEND_SR:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_SR:
return (binary_op_type) shift_right_function;
1999-04-07 18:10:10 +00:00
case ZEND_CONCAT:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_CONCAT:
return (binary_op_type) concat_function;
case ZEND_IS_IDENTICAL:
return (binary_op_type) is_identical_function;
2000-03-29 22:05:19 +00:00
case ZEND_IS_NOT_IDENTICAL:
return (binary_op_type) is_not_identical_function;
1999-04-24 00:12:55 +00:00
case ZEND_IS_EQUAL:
return (binary_op_type) is_equal_function;
1999-04-24 00:12:55 +00:00
case ZEND_IS_NOT_EQUAL:
return (binary_op_type) is_not_equal_function;
1999-04-07 18:10:10 +00:00
case ZEND_IS_SMALLER:
return (binary_op_type) is_smaller_function;
1999-04-24 00:12:55 +00:00
case ZEND_IS_SMALLER_OR_EQUAL:
return (binary_op_type) is_smaller_or_equal_function;
2015-01-19 07:12:39 +00:00
case ZEND_SPACESHIP:
return (binary_op_type) compare_function;
1999-04-07 18:10:10 +00:00
case ZEND_BW_OR:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_BW_OR:
return (binary_op_type) bitwise_or_function;
1999-04-07 18:10:10 +00:00
case ZEND_BW_AND:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_BW_AND:
return (binary_op_type) bitwise_and_function;
1999-04-07 18:10:10 +00:00
case ZEND_BW_XOR:
1999-04-24 00:12:55 +00:00
case ZEND_ASSIGN_BW_XOR:
return (binary_op_type) bitwise_xor_function;
case ZEND_BOOL_XOR:
return (binary_op_type) boolean_xor_function;
1999-04-07 18:10:10 +00:00
default:
return (binary_op_type) NULL;
1999-04-07 18:10:10 +00:00
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
*/