Skip to content

Commit f627be5

Browse files
committed
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.
1 parent ececcbc commit f627be5

6 files changed

+132
-68
lines changed

Diff for: Zend/zend_execute.h

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ void init_executor(TSRMLS_D);
5656
void shutdown_executor(TSRMLS_D);
5757
void shutdown_destructors(TSRMLS_D);
5858
ZEND_API void execute(zend_op_array *op_array TSRMLS_DC);
59+
ZEND_API void execute_ex(zend_execute_data *execute_data TSRMLS_DC);
5960
ZEND_API void execute_internal(zend_execute_data *execute_data_ptr, int return_value_used TSRMLS_DC);
6061
ZEND_API int zend_is_true(zval *op);
6162
#define safe_free_zval_ptr(p) safe_free_zval_ptr_rel(p ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC)

Diff for: Zend/zend_generators.c

+45
Original file line numberDiff line numberDiff line change
@@ -86,53 +86,98 @@ static zend_function *zend_generator_get_constructor(zval *object TSRMLS_DC) /*
8686
}
8787
/* }}} */
8888

89+
static void zend_generator_resume(zend_generator *generator TSRMLS_DC) /* {{{ */
90+
{
91+
generator->execute_data->opline++;
92+
execute_ex(generator->execute_data TSRMLS_CC);
93+
}
94+
/* }}} */
95+
96+
static void zend_generator_ensure_initialized(zend_generator *generator TSRMLS_DC) /* {{{ */
97+
{
98+
if (!generator->value) {
99+
zend_generator_resume(generator TSRMLS_CC);
100+
}
101+
}
102+
/* }}} */
103+
89104
/* {{{ proto void Generator::rewind()
90105
* Rewind the generator */
91106
ZEND_METHOD(Generator, rewind)
92107
{
108+
zend_generator *generator;
109+
93110
if (zend_parse_parameters_none() == FAILURE) {
94111
return;
95112
}
113+
114+
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
115+
116+
zend_generator_ensure_initialized(generator TSRMLS_CC);
96117
}
97118
/* }}} */
98119

99120
/* {{{ proto bool Generator::valid()
100121
* Check whether the generator is valid */
101122
ZEND_METHOD(Generator, valid)
102123
{
124+
zend_generator *generator;
125+
103126
if (zend_parse_parameters_none() == FAILURE) {
104127
return;
105128
}
129+
130+
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
131+
132+
zend_generator_ensure_initialized(generator TSRMLS_CC);
106133
}
107134
/* }}} */
108135

109136
/* {{{ proto mixed Generator::current()
110137
* Get the current value */
111138
ZEND_METHOD(Generator, current)
112139
{
140+
zend_generator *generator;
141+
113142
if (zend_parse_parameters_none() == FAILURE) {
114143
return;
115144
}
145+
146+
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
147+
148+
zend_generator_ensure_initialized(generator TSRMLS_CC);
116149
}
117150
/* }}} */
118151

119152
/* {{{ proto mixed Generator::key()
120153
* Get the current key */
121154
ZEND_METHOD(Generator, key)
122155
{
156+
zend_generator *generator;
157+
123158
if (zend_parse_parameters_none() == FAILURE) {
124159
return;
125160
}
161+
162+
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
163+
164+
zend_generator_ensure_initialized(generator TSRMLS_CC);
126165
}
127166
/* }}} */
128167

129168
/* {{{ proto void Generator::next()
130169
* Advances the generator */
131170
ZEND_METHOD(Generator, next)
132171
{
172+
zend_generator *generator;
173+
133174
if (zend_parse_parameters_none() == FAILURE) {
134175
return;
135176
}
177+
178+
generator = (zend_generator *) zend_object_store_get_object(getThis() TSRMLS_CC);
179+
180+
zend_generator_ensure_initialized(generator TSRMLS_CC);
136181
}
137182
/* }}} */
138183

Diff for: Zend/zend_generators.h

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ typedef struct _zend_generator {
3232

3333
/* The suspended execution context. */
3434
zend_execute_data *execute_data;
35+
/* Current value */
36+
zval *value;
3537
} zend_generator;
3638

3739
END_EXTERN_C()

Diff for: Zend/zend_vm_execute.h

+41-33
Original file line numberDiff line numberDiff line change
@@ -339,25 +339,9 @@ static opcode_handler_t zend_vm_get_opcode_handler(zend_uchar opcode, zend_op* o
339339
#define EX_Ts() EX(Ts)
340340

341341

342-
ZEND_API void execute(zend_op_array *op_array TSRMLS_DC)
343-
{
344-
DCL_OPLINE
345-
342+
static zend_execute_data *zend_create_execute_data_from_op_array(zend_op_array *op_array, zend_bool nested TSRMLS_DC) {
346343
zend_execute_data *execute_data;
347-
size_t execute_data_size;
348-
349-
zend_bool nested = 0;
350-
zend_bool original_in_execution = EG(in_execution);
351-
352-
353-
354-
if (EG(exception)) {
355-
return;
356-
}
357-
358-
EG(in_execution) = 1;
359344

360-
zend_vm_enter:
361345
/*
362346
* When allocating the execute_data, memory for compiled variables and
363347
* temporary variables is also allocated after the actual zend_execute_data
@@ -367,11 +351,10 @@ ZEND_API void execute(zend_op_array *op_array TSRMLS_DC)
367351
* In that case the first half contains zval**s and the second half the
368352
* actual zval*s (which would otherwise be in the symbol table).
369353
*/
370-
execute_data_size =
371-
ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data)) +
372-
ZEND_MM_ALIGNED_SIZE(sizeof(zval **) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2)) +
373-
ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable)) * op_array->T
374-
;
354+
size_t execute_data_size = ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data));
355+
size_t CVs_size = ZEND_MM_ALIGNED_SIZE(sizeof(zval **) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2));
356+
size_t Ts_size = ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable)) * op_array->T;
357+
size_t total_size = execute_data_size + CVs_size + Ts_size;
375358

376359
/*
377360
* Normally the execute_data is allocated on the VM stack (because it does
@@ -383,14 +366,16 @@ ZEND_API void execute(zend_op_array *op_array TSRMLS_DC)
383366
* by replacing a pointer.
384367
*/
385368
if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
386-
execute_data = emalloc(execute_data_size);
369+
execute_data = emalloc(total_size);
387370
} else {
388-
execute_data = zend_vm_stack_alloc(execute_data_size TSRMLS_CC);
371+
execute_data = zend_vm_stack_alloc(total_size TSRMLS_CC);
389372
}
390373

391-
EX(CVs) = (zval***)((char*)execute_data + ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data)));
392-
memset(EX(CVs), 0, sizeof(zval**) * op_array->last_var);
393-
EX(Ts) = (temp_variable *)(((char*)EX(CVs)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval**) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2)));
374+
EX(CVs) = (zval ***) ((char *) execute_data + execute_data_size);
375+
memset(EX(CVs), 0, sizeof(zval **) * op_array->last_var);
376+
377+
EX(Ts) = (temp_variable *) ((char *) EX(CVs) + CVs_size);
378+
394379
EX(fbc) = NULL;
395380
EX(called_scope) = NULL;
396381
EX(object) = NULL;
@@ -400,9 +385,6 @@ ZEND_API void execute(zend_op_array *op_array TSRMLS_DC)
400385
EX(prev_execute_data) = EG(current_execute_data);
401386
EG(current_execute_data) = execute_data;
402387
EX(nested) = nested;
403-
nested = 1;
404-
405-
LOAD_REGS();
406388

407389
if (!op_array->run_time_cache && op_array->last_cache_slot) {
408390
op_array->run_time_cache = ecalloc(op_array->last_cache_slot, sizeof(void*));
@@ -422,11 +404,31 @@ ZEND_API void execute(zend_op_array *op_array TSRMLS_DC)
422404

423405
EX(opline) = UNEXPECTED((op_array->fn_flags & ZEND_ACC_INTERACTIVE) != 0) && EG(start_op) ? EG(start_op) : op_array->opcodes;
424406
EG(opline_ptr) = &EX(opline);
425-
LOAD_OPLINE();
426407

427408
EX(function_state).function = (zend_function *) op_array;
428409
EX(function_state).arguments = NULL;
429410

411+
return execute_data;
412+
}
413+
414+
ZEND_API void execute_ex(zend_execute_data *execute_data TSRMLS_DC)
415+
{
416+
DCL_OPLINE
417+
418+
zend_bool original_in_execution = EG(in_execution);
419+
420+
421+
422+
if (EG(exception)) {
423+
return;
424+
}
425+
426+
EG(in_execution) = 1;
427+
428+
zend_vm_enter:
429+
LOAD_REGS();
430+
LOAD_OPLINE();
431+
430432
while (1) {
431433
int ret;
432434
#ifdef ZEND_WIN32
@@ -441,8 +443,7 @@ ZEND_API void execute(zend_op_array *op_array TSRMLS_DC)
441443
EG(in_execution) = original_in_execution;
442444
return;
443445
case 2:
444-
op_array = EG(active_op_array);
445-
goto zend_vm_enter;
446+
execute_data = zend_create_execute_data_from_op_array(EG(active_op_array), 1 TSRMLS_CC);
446447
case 3:
447448
execute_data = EG(current_execute_data);
448449
default:
@@ -454,6 +455,13 @@ ZEND_API void execute(zend_op_array *op_array TSRMLS_DC)
454455
zend_error_noreturn(E_ERROR, "Arrived at end of main loop which shouldn't happen");
455456
}
456457

458+
ZEND_API void execute(zend_op_array *op_array TSRMLS_DC)
459+
{
460+
zend_execute_data *execute_data = zend_create_execute_data_from_op_array(op_array, 0 TSRMLS_CC);
461+
462+
execute_ex(execute_data TSRMLS_CC);
463+
}
464+
457465
static int ZEND_FASTCALL ZEND_JMP_SPEC_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
458466
{
459467
USE_OPLINE

Diff for: Zend/zend_vm_execute.skl

+42-33
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,8 @@
11
{%DEFINES%}
22

3-
ZEND_API void {%EXECUTOR_NAME%}(zend_op_array *op_array TSRMLS_DC)
4-
{
5-
DCL_OPLINE
6-
3+
static zend_execute_data *zend_create_execute_data_from_op_array(zend_op_array *op_array, zend_bool nested TSRMLS_DC) {
74
zend_execute_data *execute_data;
8-
size_t execute_data_size;
9-
10-
zend_bool nested = 0;
11-
zend_bool original_in_execution = EG(in_execution);
12-
13-
{%HELPER_VARS%}
14-
15-
{%INTERNAL_LABELS%}
16-
17-
if (EG(exception)) {
18-
return;
19-
}
20-
21-
EG(in_execution) = 1;
225

23-
zend_vm_enter:
246
/*
257
* When allocating the execute_data, memory for compiled variables and
268
* temporary variables is also allocated after the actual zend_execute_data
@@ -30,11 +12,10 @@ zend_vm_enter:
3012
* In that case the first half contains zval**s and the second half the
3113
* actual zval*s (which would otherwise be in the symbol table).
3214
*/
33-
execute_data_size =
34-
ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data)) +
35-
ZEND_MM_ALIGNED_SIZE(sizeof(zval **) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2)) +
36-
ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable)) * op_array->T
37-
;
15+
size_t execute_data_size = ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data));
16+
size_t CVs_size = ZEND_MM_ALIGNED_SIZE(sizeof(zval **) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2));
17+
size_t Ts_size = ZEND_MM_ALIGNED_SIZE(sizeof(temp_variable)) * op_array->T;
18+
size_t total_size = execute_data_size + CVs_size + Ts_size;
3819

3920
/*
4021
* Normally the execute_data is allocated on the VM stack (because it does
@@ -46,14 +27,16 @@ zend_vm_enter:
4627
* by replacing a pointer.
4728
*/
4829
if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
49-
execute_data = emalloc(execute_data_size);
30+
execute_data = emalloc(total_size);
5031
} else {
51-
execute_data = zend_vm_stack_alloc(execute_data_size TSRMLS_CC);
32+
execute_data = zend_vm_stack_alloc(total_size TSRMLS_CC);
5233
}
5334

54-
EX(CVs) = (zval***)((char*)execute_data + ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data)));
55-
memset(EX(CVs), 0, sizeof(zval**) * op_array->last_var);
56-
EX(Ts) = (temp_variable *)(((char*)EX(CVs)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval**) * op_array->last_var * (EG(active_symbol_table) ? 1 : 2)));
35+
EX(CVs) = (zval ***) ((char *) execute_data + execute_data_size);
36+
memset(EX(CVs), 0, sizeof(zval **) * op_array->last_var);
37+
38+
EX(Ts) = (temp_variable *) ((char *) EX(CVs) + CVs_size);
39+
5740
EX(fbc) = NULL;
5841
EX(called_scope) = NULL;
5942
EX(object) = NULL;
@@ -63,9 +46,6 @@ zend_vm_enter:
6346
EX(prev_execute_data) = EG(current_execute_data);
6447
EG(current_execute_data) = execute_data;
6548
EX(nested) = nested;
66-
nested = 1;
67-
68-
LOAD_REGS();
6949

7050
if (!op_array->run_time_cache && op_array->last_cache_slot) {
7151
op_array->run_time_cache = ecalloc(op_array->last_cache_slot, sizeof(void*));
@@ -85,11 +65,33 @@ zend_vm_enter:
8565

8666
EX(opline) = UNEXPECTED((op_array->fn_flags & ZEND_ACC_INTERACTIVE) != 0) && EG(start_op) ? EG(start_op) : op_array->opcodes;
8767
EG(opline_ptr) = &EX(opline);
88-
LOAD_OPLINE();
8968

9069
EX(function_state).function = (zend_function *) op_array;
9170
EX(function_state).arguments = NULL;
9271

72+
return execute_data;
73+
}
74+
75+
ZEND_API void {%EXECUTOR_NAME%}_ex(zend_execute_data *execute_data TSRMLS_DC)
76+
{
77+
DCL_OPLINE
78+
79+
zend_bool original_in_execution = EG(in_execution);
80+
81+
{%HELPER_VARS%}
82+
83+
{%INTERNAL_LABELS%}
84+
85+
if (EG(exception)) {
86+
return;
87+
}
88+
89+
EG(in_execution) = 1;
90+
91+
zend_vm_enter:
92+
LOAD_REGS();
93+
LOAD_OPLINE();
94+
9395
while (1) {
9496
{%ZEND_VM_CONTINUE_LABEL%}
9597
#ifdef ZEND_WIN32
@@ -106,6 +108,13 @@ zend_vm_enter:
106108
zend_error_noreturn(E_ERROR, "Arrived at end of main loop which shouldn't happen");
107109
}
108110

111+
ZEND_API void {%EXECUTOR_NAME%}(zend_op_array *op_array TSRMLS_DC)
112+
{
113+
zend_execute_data *execute_data = zend_create_execute_data_from_op_array(op_array, 0 TSRMLS_CC);
114+
115+
{%EXECUTOR_NAME%}_ex(execute_data TSRMLS_CC);
116+
}
117+
109118
{%EXTERNAL_EXECUTOR%}
110119

111120
void {%INITIALIZER_NAME%}(void)

Diff for: Zend/zend_vm_gen.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -976,8 +976,7 @@ function gen_executor($f, $skl, $spec, $kind, $executor_name, $initializer_name,
976976
$m[1]."\t\tEG(in_execution) = original_in_execution;\n".
977977
$m[1]."\t\treturn;\n".
978978
$m[1]."\tcase 2:\n" .
979-
$m[1]."\t\top_array = EG(active_op_array);\n".
980-
$m[1]."\t\tgoto zend_vm_enter;\n".
979+
$m[1]."\t\texecute_data = zend_create_execute_data_from_op_array(EG(active_op_array), 1 TSRMLS_CC);\n".
981980
$m[1]."\tcase 3:\n" .
982981
$m[1]."\t\texecute_data = EG(current_execute_data);\n".
983982
$m[1]."\tdefault:\n".

0 commit comments

Comments
 (0)