Add support for executing a zend_execute_data

This adds another function execute_ex(), which accepts a zend_execute_data
struct to run (contrary to execute(), which accepts a zend_op_array from
which it initialized the execute_data).

This needs a bit more cleanup.
This commit is contained in:
Nikita Popov 2012-05-26 17:53:13 +02:00
parent ececcbce0e
commit f627be5254
6 changed files with 132 additions and 68 deletions

View file

@ -56,6 +56,7 @@ void init_executor(TSRMLS_D);
void shutdown_executor(TSRMLS_D); void shutdown_executor(TSRMLS_D);
void shutdown_destructors(TSRMLS_D); void shutdown_destructors(TSRMLS_D);
ZEND_API void execute(zend_op_array *op_array TSRMLS_DC); ZEND_API void execute(zend_op_array *op_array TSRMLS_DC);
ZEND_API void execute_ex(zend_execute_data *execute_data TSRMLS_DC);
ZEND_API void execute_internal(zend_execute_data *execute_data_ptr, int return_value_used TSRMLS_DC); ZEND_API void execute_internal(zend_execute_data *execute_data_ptr, int return_value_used TSRMLS_DC);
ZEND_API int zend_is_true(zval *op); ZEND_API int zend_is_true(zval *op);
#define safe_free_zval_ptr(p) safe_free_zval_ptr_rel(p ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC) #define safe_free_zval_ptr(p) safe_free_zval_ptr_rel(p ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)

View file

@ -86,13 +86,34 @@ static zend_function *zend_generator_get_constructor(zval *object TSRMLS_DC) /*
} }
/* }}} */ /* }}} */
static void zend_generator_resume(zend_generator *generator TSRMLS_DC) /* {{{ */
{
generator->execute_data->opline++;
execute_ex(generator->execute_data TSRMLS_CC);
}
/* }}} */
static void zend_generator_ensure_initialized(zend_generator *generator TSRMLS_DC) /* {{{ */
{
if (!generator->value) {
zend_generator_resume(generator TSRMLS_CC);
}
}
/* }}} */
/* {{{ proto void Generator::rewind() /* {{{ proto void Generator::rewind()
* Rewind the generator */ * Rewind the generator */
ZEND_METHOD(Generator, rewind) ZEND_METHOD(Generator, rewind)
{ {
zend_generator *generator;
if (zend_parse_parameters_none() == FAILURE) { if (zend_parse_parameters_none() == FAILURE) {
return; return;
} }
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
zend_generator_ensure_initialized(generator TSRMLS_CC);
} }
/* }}} */ /* }}} */
@ -100,9 +121,15 @@ ZEND_METHOD(Generator, rewind)
* Check whether the generator is valid */ * Check whether the generator is valid */
ZEND_METHOD(Generator, valid) ZEND_METHOD(Generator, valid)
{ {
zend_generator *generator;
if (zend_parse_parameters_none() == FAILURE) { if (zend_parse_parameters_none() == FAILURE) {
return; return;
} }
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
zend_generator_ensure_initialized(generator TSRMLS_CC);
} }
/* }}} */ /* }}} */
@ -110,9 +137,15 @@ ZEND_METHOD(Generator, valid)
* Get the current value */ * Get the current value */
ZEND_METHOD(Generator, current) ZEND_METHOD(Generator, current)
{ {
zend_generator *generator;
if (zend_parse_parameters_none() == FAILURE) { if (zend_parse_parameters_none() == FAILURE) {
return; return;
} }
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
zend_generator_ensure_initialized(generator TSRMLS_CC);
} }
/* }}} */ /* }}} */
@ -120,9 +153,15 @@ ZEND_METHOD(Generator, current)
* Get the current key */ * Get the current key */
ZEND_METHOD(Generator, key) ZEND_METHOD(Generator, key)
{ {
zend_generator *generator;
if (zend_parse_parameters_none() == FAILURE) { if (zend_parse_parameters_none() == FAILURE) {
return; return;
} }
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
zend_generator_ensure_initialized(generator TSRMLS_CC);
} }
/* }}} */ /* }}} */
@ -130,9 +169,15 @@ ZEND_METHOD(Generator, key)
* Advances the generator */ * Advances the generator */
ZEND_METHOD(Generator, next) ZEND_METHOD(Generator, next)
{ {
zend_generator *generator;
if (zend_parse_parameters_none() == FAILURE) { if (zend_parse_parameters_none() == FAILURE) {
return; return;
} }
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
zend_generator_ensure_initialized(generator TSRMLS_CC);
} }
/* }}} */ /* }}} */

View file

@ -32,6 +32,8 @@ typedef struct _zend_generator {
/* The suspended execution context. */ /* The suspended execution context. */
zend_execute_data *execute_data; zend_execute_data *execute_data;
/* Current value */
zval *value;
} zend_generator; } zend_generator;
END_EXTERN_C() END_EXTERN_C()

View file

@ -339,25 +339,9 @@ static opcode_handler_t zend_vm_get_opcode_handler(zend_uchar opcode, zend_op* o
#define EX_Ts() EX(Ts) #define EX_Ts() EX(Ts)
ZEND_API void execute(zend_op_array *op_array TSRMLS_DC) static zend_execute_data *zend_create_execute_data_from_op_array(zend_op_array *op_array, zend_bool nested TSRMLS_DC) {
{
DCL_OPLINE
zend_execute_data *execute_data; zend_execute_data *execute_data;
size_t execute_data_size;
zend_bool nested = 0;
zend_bool original_in_execution = EG(in_execution);
if (EG(exception)) {
return;
}
EG(in_execution) = 1;
zend_vm_enter:
/* /*
* When allocating the execute_data, memory for compiled variables and * When allocating the execute_data, memory for compiled variables and
* temporary variables is also allocated after the actual zend_execute_data * temporary variables is also allocated after the actual zend_execute_data
@ -367,11 +351,10 @@ zend_vm_enter:
* In that case the first half contains zval**s and the second half the * In that case the first half contains zval**s and the second half the
* actual zval*s (which would otherwise be in the symbol table). * actual zval*s (which would otherwise be in the symbol table).
*/ */
execute_data_size = size_t execute_data_size = ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data));
ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data)) + size_t CVs_size = ZEND_MM_ALIGNED_SIZE(sizeof(zval **) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2));
ZEND_MM_ALIGNED_SIZE(sizeof(zval **) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2)) + size_t Ts_size = ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable)) * op_array->T;
ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable)) * op_array->T size_t total_size = execute_data_size + CVs_size + Ts_size;
;
/* /*
* Normally the execute_data is allocated on the VM stack (because it does * Normally the execute_data is allocated on the VM stack (because it does
@ -383,14 +366,16 @@ zend_vm_enter:
* by replacing a pointer. * by replacing a pointer.
*/ */
if (op_array->fn_flags & ZEND_ACC_GENERATOR) { if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
execute_data = emalloc(execute_data_size); execute_data = emalloc(total_size);
} else { } else {
execute_data = zend_vm_stack_alloc(execute_data_size TSRMLS_CC); execute_data = zend_vm_stack_alloc(total_size TSRMLS_CC);
} }
EX(CVs) = (zval***)((char*)execute_data + ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data))); EX(CVs) = (zval ***) ((char *) execute_data + execute_data_size);
memset(EX(CVs), 0, sizeof(zval**) * op_array->last_var); memset(EX(CVs), 0, sizeof(zval **) * op_array->last_var);
EX(Ts) = (temp_variable *)(((char*)EX(CVs)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval**) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2)));
EX(Ts) = (temp_variable *) ((char *) EX(CVs) + CVs_size);
EX(fbc) = NULL; EX(fbc) = NULL;
EX(called_scope) = NULL; EX(called_scope) = NULL;
EX(object) = NULL; EX(object) = NULL;
@ -400,9 +385,6 @@ zend_vm_enter:
EX(prev_execute_data) = EG(current_execute_data); EX(prev_execute_data) = EG(current_execute_data);
EG(current_execute_data) = execute_data; EG(current_execute_data) = execute_data;
EX(nested) = nested; EX(nested) = nested;
nested = 1;
LOAD_REGS();
if (!op_array->run_time_cache && op_array->last_cache_slot) { if (!op_array->run_time_cache && op_array->last_cache_slot) {
op_array->run_time_cache = ecalloc(op_array->last_cache_slot, sizeof(void*)); op_array->run_time_cache = ecalloc(op_array->last_cache_slot, sizeof(void*));
@ -422,11 +404,31 @@ zend_vm_enter:
EX(opline) = UNEXPECTED((op_array->fn_flags & ZEND_ACC_INTERACTIVE) != 0) && EG(start_op) ? EG(start_op) : op_array->opcodes; EX(opline) = UNEXPECTED((op_array->fn_flags & ZEND_ACC_INTERACTIVE) != 0) && EG(start_op) ? EG(start_op) : op_array->opcodes;
EG(opline_ptr) = &EX(opline); EG(opline_ptr) = &EX(opline);
LOAD_OPLINE();
EX(function_state).function = (zend_function *) op_array; EX(function_state).function = (zend_function *) op_array;
EX(function_state).arguments = NULL; EX(function_state).arguments = NULL;
return execute_data;
}
ZEND_API void execute_ex(zend_execute_data *execute_data TSRMLS_DC)
{
DCL_OPLINE
zend_bool original_in_execution = EG(in_execution);
if (EG(exception)) {
return;
}
EG(in_execution) = 1;
zend_vm_enter:
LOAD_REGS();
LOAD_OPLINE();
while (1) { while (1) {
int ret; int ret;
#ifdef ZEND_WIN32 #ifdef ZEND_WIN32
@ -441,8 +443,7 @@ zend_vm_enter:
EG(in_execution) = original_in_execution; EG(in_execution) = original_in_execution;
return; return;
case 2: case 2:
op_array = EG(active_op_array); execute_data = zend_create_execute_data_from_op_array(EG(active_op_array), 1 TSRMLS_CC);
goto zend_vm_enter;
case 3: case 3:
execute_data = EG(current_execute_data); execute_data = EG(current_execute_data);
default: default:
@ -454,6 +455,13 @@ zend_vm_enter:
zend_error_noreturn(E_ERROR, "Arrived at end of main loop which shouldn't happen"); zend_error_noreturn(E_ERROR, "Arrived at end of main loop which shouldn't happen");
} }
ZEND_API void execute(zend_op_array *op_array TSRMLS_DC)
{
zend_execute_data *execute_data = zend_create_execute_data_from_op_array(op_array, 0 TSRMLS_CC);
execute_ex(execute_data TSRMLS_CC);
}
static int ZEND_FASTCALL ZEND_JMP_SPEC_HANDLER(ZEND_OPCODE_HANDLER_ARGS) static int ZEND_FASTCALL ZEND_JMP_SPEC_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
{ {
USE_OPLINE USE_OPLINE

View file

@ -1,26 +1,8 @@
{%DEFINES%} {%DEFINES%}
ZEND_API void {%EXECUTOR_NAME%}(zend_op_array *op_array TSRMLS_DC) static zend_execute_data *zend_create_execute_data_from_op_array(zend_op_array *op_array, zend_bool nested TSRMLS_DC) {
{
DCL_OPLINE
zend_execute_data *execute_data; zend_execute_data *execute_data;
size_t execute_data_size;
zend_bool nested = 0;
zend_bool original_in_execution = EG(in_execution);
{%HELPER_VARS%}
{%INTERNAL_LABELS%}
if (EG(exception)) {
return;
}
EG(in_execution) = 1;
zend_vm_enter:
/* /*
* When allocating the execute_data, memory for compiled variables and * When allocating the execute_data, memory for compiled variables and
* temporary variables is also allocated after the actual zend_execute_data * temporary variables is also allocated after the actual zend_execute_data
@ -30,11 +12,10 @@ zend_vm_enter:
* In that case the first half contains zval**s and the second half the * In that case the first half contains zval**s and the second half the
* actual zval*s (which would otherwise be in the symbol table). * actual zval*s (which would otherwise be in the symbol table).
*/ */
execute_data_size = size_t execute_data_size = ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data));
ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data)) + size_t CVs_size = ZEND_MM_ALIGNED_SIZE(sizeof(zval **) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2));
ZEND_MM_ALIGNED_SIZE(sizeof(zval **) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2)) + size_t Ts_size = ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable)) * op_array->T;
ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable)) * op_array->T size_t total_size = execute_data_size + CVs_size + Ts_size;
;
/* /*
* Normally the execute_data is allocated on the VM stack (because it does * Normally the execute_data is allocated on the VM stack (because it does
@ -46,14 +27,16 @@ zend_vm_enter:
* by replacing a pointer. * by replacing a pointer.
*/ */
if (op_array->fn_flags & ZEND_ACC_GENERATOR) { if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
execute_data = emalloc(execute_data_size); execute_data = emalloc(total_size);
} else { } else {
execute_data = zend_vm_stack_alloc(execute_data_size TSRMLS_CC); execute_data = zend_vm_stack_alloc(total_size TSRMLS_CC);
} }
EX(CVs) = (zval***)((char*)execute_data + ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data))); EX(CVs) = (zval ***) ((char *) execute_data + execute_data_size);
memset(EX(CVs), 0, sizeof(zval**) * op_array->last_var); memset(EX(CVs), 0, sizeof(zval **) * op_array->last_var);
EX(Ts) = (temp_variable *)(((char*)EX(CVs)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval**) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2)));
EX(Ts) = (temp_variable *) ((char *) EX(CVs) + CVs_size);
EX(fbc) = NULL; EX(fbc) = NULL;
EX(called_scope) = NULL; EX(called_scope) = NULL;
EX(object) = NULL; EX(object) = NULL;
@ -63,9 +46,6 @@ zend_vm_enter:
EX(prev_execute_data) = EG(current_execute_data); EX(prev_execute_data) = EG(current_execute_data);
EG(current_execute_data) = execute_data; EG(current_execute_data) = execute_data;
EX(nested) = nested; EX(nested) = nested;
nested = 1;
LOAD_REGS();
if (!op_array->run_time_cache && op_array->last_cache_slot) { if (!op_array->run_time_cache && op_array->last_cache_slot) {
op_array->run_time_cache = ecalloc(op_array->last_cache_slot, sizeof(void*)); op_array->run_time_cache = ecalloc(op_array->last_cache_slot, sizeof(void*));
@ -85,11 +65,33 @@ zend_vm_enter:
EX(opline) = UNEXPECTED((op_array->fn_flags & ZEND_ACC_INTERACTIVE) != 0) && EG(start_op) ? EG(start_op) : op_array->opcodes; EX(opline) = UNEXPECTED((op_array->fn_flags & ZEND_ACC_INTERACTIVE) != 0) && EG(start_op) ? EG(start_op) : op_array->opcodes;
EG(opline_ptr) = &EX(opline); EG(opline_ptr) = &EX(opline);
LOAD_OPLINE();
EX(function_state).function = (zend_function *) op_array; EX(function_state).function = (zend_function *) op_array;
EX(function_state).arguments = NULL; EX(function_state).arguments = NULL;
return execute_data;
}
ZEND_API void {%EXECUTOR_NAME%}_ex(zend_execute_data *execute_data TSRMLS_DC)
{
DCL_OPLINE
zend_bool original_in_execution = EG(in_execution);
{%HELPER_VARS%}
{%INTERNAL_LABELS%}
if (EG(exception)) {
return;
}
EG(in_execution) = 1;
zend_vm_enter:
LOAD_REGS();
LOAD_OPLINE();
while (1) { while (1) {
{%ZEND_VM_CONTINUE_LABEL%} {%ZEND_VM_CONTINUE_LABEL%}
#ifdef ZEND_WIN32 #ifdef ZEND_WIN32
@ -106,6 +108,13 @@ zend_vm_enter:
zend_error_noreturn(E_ERROR, "Arrived at end of main loop which shouldn't happen"); zend_error_noreturn(E_ERROR, "Arrived at end of main loop which shouldn't happen");
} }
ZEND_API void {%EXECUTOR_NAME%}(zend_op_array *op_array TSRMLS_DC)
{
zend_execute_data *execute_data = zend_create_execute_data_from_op_array(op_array, 0 TSRMLS_CC);
{%EXECUTOR_NAME%}_ex(execute_data TSRMLS_CC);
}
{%EXTERNAL_EXECUTOR%} {%EXTERNAL_EXECUTOR%}
void {%INITIALIZER_NAME%}(void) void {%INITIALIZER_NAME%}(void)

View file

@ -976,8 +976,7 @@ function gen_executor($f, $skl, $spec, $kind, $executor_name, $initializer_name,
$m[1]."\t\tEG(in_execution) = original_in_execution;\n". $m[1]."\t\tEG(in_execution) = original_in_execution;\n".
$m[1]."\t\treturn;\n". $m[1]."\t\treturn;\n".
$m[1]."\tcase 2:\n" . $m[1]."\tcase 2:\n" .
$m[1]."\t\top_array = EG(active_op_array);\n". $m[1]."\t\texecute_data = zend_create_execute_data_from_op_array(EG(active_op_array), 1 TSRMLS_CC);\n".
$m[1]."\t\tgoto zend_vm_enter;\n".
$m[1]."\tcase 3:\n" . $m[1]."\tcase 3:\n" .
$m[1]."\t\texecute_data = EG(current_execute_data);\n". $m[1]."\t\texecute_data = EG(current_execute_data);\n".
$m[1]."\tdefault:\n". $m[1]."\tdefault:\n".