1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2015 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
32static void zend_extension_op_array_ctor_handler(zend_extension *extension, zend_op_array *op_array)
33{
34    if (extension->op_array_ctor) {
35        extension->op_array_ctor(op_array);
36    }
37}
38
39static void zend_extension_op_array_dtor_handler(zend_extension *extension, zend_op_array *op_array)
40{
41    if (extension->op_array_dtor) {
42        extension->op_array_dtor(op_array);
43    }
44}
45
46static void op_array_alloc_ops(zend_op_array *op_array, uint32_t size)
47{
48    op_array->opcodes = erealloc(op_array->opcodes, size * sizeof(zend_op));
49}
50
51void init_op_array(zend_op_array *op_array, zend_uchar type, int initial_ops_size)
52{
53    op_array->type = type;
54
55    op_array->refcount = (uint32_t *) emalloc(sizeof(uint32_t));
56    *op_array->refcount = 1;
57    op_array->last = 0;
58    op_array->opcodes = NULL;
59    op_array_alloc_ops(op_array, initial_ops_size);
60
61    op_array->last_var = 0;
62    op_array->vars = NULL;
63
64    op_array->T = 0;
65
66    op_array->function_name = NULL;
67    op_array->filename = zend_get_compiled_filename();
68    op_array->doc_comment = NULL;
69
70    op_array->arg_info = NULL;
71    op_array->num_args = 0;
72    op_array->required_num_args = 0;
73
74    op_array->scope = NULL;
75    op_array->prototype = NULL;
76
77    op_array->brk_cont_array = NULL;
78    op_array->try_catch_array = NULL;
79    op_array->last_brk_cont = 0;
80
81    op_array->static_variables = NULL;
82    op_array->last_try_catch = 0;
83
84    op_array->this_var = -1;
85
86    op_array->fn_flags = 0;
87
88    op_array->early_binding = -1;
89
90    op_array->last_literal = 0;
91    op_array->literals = NULL;
92
93    op_array->run_time_cache = NULL;
94    op_array->cache_size = 0;
95
96    memset(op_array->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
97
98    zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_ctor_handler, op_array);
99}
100
101ZEND_API void destroy_zend_function(zend_function *function)
102{
103    if (function->type == ZEND_USER_FUNCTION) {
104        destroy_op_array(&function->op_array);
105    } else {
106        ZEND_ASSERT(function->type == ZEND_INTERNAL_FUNCTION);
107        ZEND_ASSERT(function->common.function_name);
108        zend_string_release(function->common.function_name);
109    }
110}
111
112ZEND_API void zend_function_dtor(zval *zv)
113{
114    zend_function *function = Z_PTR_P(zv);
115
116    if (function->type == ZEND_USER_FUNCTION) {
117        ZEND_ASSERT(function->common.function_name);
118        destroy_op_array(&function->op_array);
119        /* op_arrays are allocated on arena, so we don't have to free them */
120//???       efree_size(function, sizeof(zend_op_array));
121    } else {
122        ZEND_ASSERT(function->type == ZEND_INTERNAL_FUNCTION);
123        ZEND_ASSERT(function->common.function_name);
124        zend_string_release(function->common.function_name);
125        if (!(function->common.fn_flags & ZEND_ACC_ARENA_ALLOCATED)) {
126            pefree(function, 1);
127        }
128    }
129}
130
131ZEND_API void zend_cleanup_op_array_data(zend_op_array *op_array)
132{
133    if (op_array->static_variables &&
134        !(GC_FLAGS(op_array->static_variables) & IS_ARRAY_IMMUTABLE)) {
135        zend_hash_clean(op_array->static_variables);
136    }
137}
138
139ZEND_API void zend_cleanup_user_class_data(zend_class_entry *ce)
140{
141    /* Clean all parts that can contain run-time data */
142    /* Note that only run-time accessed data need to be cleaned up, pre-defined data can
143       not contain objects and thus are not probelmatic */
144    if (ce->ce_flags & ZEND_HAS_STATIC_IN_METHODS) {
145        zend_function *func;
146
147        ZEND_HASH_FOREACH_PTR(&ce->function_table, func) {
148            if (func->type == ZEND_USER_FUNCTION) {
149                zend_cleanup_op_array_data((zend_op_array *) func);
150            }
151        } ZEND_HASH_FOREACH_END();
152    }
153    if (ce->static_members_table) {
154        zval *static_members = ce->static_members_table;
155        int count = ce->default_static_members_count;
156        int i;
157
158        ce->default_static_members_count = 0;
159        ce->default_static_members_table = ce->static_members_table = NULL;
160        for (i = 0; i < count; i++) {
161            zval_ptr_dtor(&static_members[i]);
162        }
163        efree(static_members);
164    }
165}
166
167ZEND_API void zend_cleanup_internal_class_data(zend_class_entry *ce)
168{
169    if (CE_STATIC_MEMBERS(ce)) {
170        zval *static_members = CE_STATIC_MEMBERS(ce);
171        int i;
172
173#ifdef ZTS
174        CG(static_members_table)[(zend_intptr_t)(ce->static_members_table)] = NULL;
175#else
176        ce->static_members_table = NULL;
177#endif
178        for (i = 0; i < ce->default_static_members_count; i++) {
179            zval_ptr_dtor(&static_members[i]);
180        }
181        efree(static_members);
182    }
183}
184
185void _destroy_zend_class_traits_info(zend_class_entry *ce)
186{
187    if (ce->num_traits > 0 && ce->traits) {
188        efree(ce->traits);
189    }
190
191    if (ce->trait_aliases) {
192        size_t i = 0;
193        while (ce->trait_aliases[i]) {
194            if (ce->trait_aliases[i]->trait_method) {
195                if (ce->trait_aliases[i]->trait_method->method_name) {
196                    zend_string_release(ce->trait_aliases[i]->trait_method->method_name);
197                }
198                if (ce->trait_aliases[i]->trait_method->class_name) {
199                    zend_string_release(ce->trait_aliases[i]->trait_method->class_name);
200                }
201                efree(ce->trait_aliases[i]->trait_method);
202            }
203
204            if (ce->trait_aliases[i]->alias) {
205                zend_string_release(ce->trait_aliases[i]->alias);
206            }
207
208            efree(ce->trait_aliases[i]);
209            i++;
210        }
211
212        efree(ce->trait_aliases);
213    }
214
215    if (ce->trait_precedences) {
216        size_t i = 0;
217
218        while (ce->trait_precedences[i]) {
219            zend_string_release(ce->trait_precedences[i]->trait_method->method_name);
220            zend_string_release(ce->trait_precedences[i]->trait_method->class_name);
221            efree(ce->trait_precedences[i]->trait_method);
222
223            if (ce->trait_precedences[i]->exclude_from_classes) {
224                efree(ce->trait_precedences[i]->exclude_from_classes);
225            }
226
227            efree(ce->trait_precedences[i]);
228            i++;
229        }
230        efree(ce->trait_precedences);
231    }
232}
233
234ZEND_API void destroy_zend_class(zval *zv)
235{
236    zend_class_entry *ce = Z_PTR_P(zv);
237
238    if (--ce->refcount > 0) {
239        return;
240    }
241    switch (ce->type) {
242        case ZEND_USER_CLASS:
243            if (ce->default_properties_table) {
244                int i;
245
246                for (i = 0; i < ce->default_properties_count; i++) {
247                    if (Z_TYPE(ce->default_properties_table[i]) != IS_UNDEF) {
248                        zval_ptr_dtor(&ce->default_properties_table[i]);
249                    }
250                }
251                efree(ce->default_properties_table);
252            }
253            if (ce->default_static_members_table) {
254                int i;
255
256                for (i = 0; i < ce->default_static_members_count; i++) {
257                    if (Z_TYPE(ce->default_static_members_table[i]) != IS_UNDEF) {
258                        zval_ptr_dtor(&ce->default_static_members_table[i]);
259                    }
260                }
261                efree(ce->default_static_members_table);
262            }
263            zend_hash_destroy(&ce->properties_info);
264            zend_string_release(ce->name);
265            zend_hash_destroy(&ce->function_table);
266            zend_hash_destroy(&ce->constants_table);
267            if (ce->num_interfaces > 0 && ce->interfaces) {
268                efree(ce->interfaces);
269            }
270            if (ce->info.user.doc_comment) {
271                zend_string_release(ce->info.user.doc_comment);
272            }
273
274            _destroy_zend_class_traits_info(ce);
275
276            break;
277        case ZEND_INTERNAL_CLASS:
278            if (ce->default_properties_table) {
279                int i;
280
281                for (i = 0; i < ce->default_properties_count; i++) {
282                    if (Z_TYPE(ce->default_properties_table[i]) != IS_UNDEF) {
283                        zval_internal_ptr_dtor(&ce->default_properties_table[i]);
284                    }
285                }
286                free(ce->default_properties_table);
287            }
288            if (ce->default_static_members_table) {
289                int i;
290
291                for (i = 0; i < ce->default_static_members_count; i++) {
292                    zval_internal_ptr_dtor(&ce->default_static_members_table[i]);
293                }
294                free(ce->default_static_members_table);
295            }
296            zend_hash_destroy(&ce->properties_info);
297            zend_string_release(ce->name);
298            zend_hash_destroy(&ce->function_table);
299            zend_hash_destroy(&ce->constants_table);
300            if (ce->num_interfaces > 0) {
301                free(ce->interfaces);
302            }
303            free(ce);
304            break;
305    }
306}
307
308void zend_class_add_ref(zval *zv)
309{
310    zend_class_entry *ce = Z_PTR_P(zv);
311
312    ce->refcount++;
313}
314
315ZEND_API void destroy_op_array(zend_op_array *op_array)
316{
317    zval *literal = op_array->literals;
318    zval *end;
319    uint32_t i;
320
321    if (op_array->static_variables &&
322        !(GC_FLAGS(op_array->static_variables) & IS_ARRAY_IMMUTABLE)) {
323        if (--GC_REFCOUNT(op_array->static_variables) == 0) {
324            zend_array_destroy(op_array->static_variables);
325        }
326    }
327
328    if (op_array->run_time_cache && !op_array->function_name) {
329        efree(op_array->run_time_cache);
330    }
331
332    if (!op_array->refcount || --(*op_array->refcount)>0) {
333        return;
334    }
335
336    efree_size(op_array->refcount, sizeof(*(op_array->refcount)));
337
338    if (op_array->vars) {
339        i = op_array->last_var;
340        while (i > 0) {
341            i--;
342            zend_string_release(op_array->vars[i]);
343        }
344        efree(op_array->vars);
345    }
346
347    if (literal) {
348        end = literal + op_array->last_literal;
349        while (literal < end) {
350            zval_ptr_dtor_nogc(literal);
351            literal++;
352        }
353        efree(op_array->literals);
354    }
355    efree(op_array->opcodes);
356
357    if (op_array->function_name) {
358        zend_string_release(op_array->function_name);
359    }
360    if (op_array->doc_comment) {
361        zend_string_release(op_array->doc_comment);
362    }
363    if (op_array->brk_cont_array) {
364        efree(op_array->brk_cont_array);
365    }
366    if (op_array->try_catch_array) {
367        efree(op_array->try_catch_array);
368    }
369    if (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) {
370        zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_dtor_handler, op_array);
371    }
372    if (op_array->arg_info) {
373        int32_t num_args = op_array->num_args;
374        zend_arg_info *arg_info = op_array->arg_info;
375        int32_t i;
376
377        if (op_array->fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
378            arg_info--;
379            num_args++;
380        }
381        if (op_array->fn_flags & ZEND_ACC_VARIADIC) {
382            num_args++;
383        }
384        for (i = 0 ; i < num_args; i++) {
385            if (arg_info[i].name) {
386                zend_string_release(arg_info[i].name);
387            }
388            if (arg_info[i].class_name) {
389                zend_string_release(arg_info[i].class_name);
390            }
391        }
392        efree(arg_info);
393    }
394}
395
396void init_op(zend_op *op)
397{
398    memset(op, 0, sizeof(zend_op));
399    op->lineno = CG(zend_lineno);
400    SET_UNUSED(op->result);
401}
402
403zend_op *get_next_op(zend_op_array *op_array)
404{
405    uint32_t next_op_num = op_array->last++;
406    zend_op *next_op;
407
408    if (next_op_num >= CG(context).opcodes_size) {
409        CG(context).opcodes_size *= 4;
410        op_array_alloc_ops(op_array, CG(context).opcodes_size);
411    }
412
413    next_op = &(op_array->opcodes[next_op_num]);
414
415    init_op(next_op);
416
417    return next_op;
418}
419
420int get_next_op_number(zend_op_array *op_array)
421{
422    return op_array->last;
423}
424
425zend_brk_cont_element *get_next_brk_cont_element(zend_op_array *op_array)
426{
427    op_array->last_brk_cont++;
428    op_array->brk_cont_array = erealloc(op_array->brk_cont_array, sizeof(zend_brk_cont_element)*op_array->last_brk_cont);
429    return &op_array->brk_cont_array[op_array->last_brk_cont-1];
430}
431
432static void zend_update_extended_info(zend_op_array *op_array)
433{
434    zend_op *opline = op_array->opcodes, *end=opline+op_array->last;
435
436    while (opline<end) {
437        if (opline->opcode == ZEND_EXT_STMT) {
438            if (opline+1<end) {
439                if ((opline+1)->opcode == ZEND_EXT_STMT) {
440                    opline->opcode = ZEND_NOP;
441                    opline++;
442                    continue;
443                }
444                if (opline+1<end) {
445                    opline->lineno = (opline+1)->lineno;
446                }
447            } else {
448                opline->opcode = ZEND_NOP;
449            }
450        }
451        opline++;
452    }
453}
454
455static void zend_extension_op_array_handler(zend_extension *extension, zend_op_array *op_array)
456{
457    if (extension->op_array_handler) {
458        extension->op_array_handler(op_array);
459    }
460}
461
462static void zend_check_finally_breakout(zend_op_array *op_array, uint32_t op_num, uint32_t dst_num)
463{
464    int i;
465
466    for (i = 0; i < op_array->last_try_catch; i++) {
467        if ((op_num < op_array->try_catch_array[i].finally_op ||
468                    op_num >= op_array->try_catch_array[i].finally_end)
469                && (dst_num >= op_array->try_catch_array[i].finally_op &&
470                     dst_num <= op_array->try_catch_array[i].finally_end)) {
471            CG(in_compilation) = 1;
472            CG(active_op_array) = op_array;
473            CG(zend_lineno) = op_array->opcodes[op_num].lineno;
474            zend_error_noreturn(E_COMPILE_ERROR, "jump into a finally block is disallowed");
475        } else if ((op_num >= op_array->try_catch_array[i].finally_op
476                    && op_num <= op_array->try_catch_array[i].finally_end)
477                && (dst_num > op_array->try_catch_array[i].finally_end
478                    || dst_num < op_array->try_catch_array[i].finally_op)) {
479            CG(in_compilation) = 1;
480            CG(active_op_array) = op_array;
481            CG(zend_lineno) = op_array->opcodes[op_num].lineno;
482            zend_error_noreturn(E_COMPILE_ERROR, "jump out of a finally block is disallowed");
483        }
484    }
485}
486
487static void zend_adjust_fast_call(zend_op_array *op_array, uint32_t fast_call, uint32_t start, uint32_t end)
488{
489    int i;
490    uint32_t op_num = 0;
491
492    for (i = 0; i < op_array->last_try_catch; i++) {
493        if (op_array->try_catch_array[i].finally_op > start
494                && op_array->try_catch_array[i].finally_end < end) {
495            op_num = op_array->try_catch_array[i].finally_op;
496            start = op_array->try_catch_array[i].finally_end;
497        }
498    }
499
500    if (op_num) {
501        /* Must be ZEND_FAST_CALL */
502        ZEND_ASSERT(op_array->opcodes[op_num - 2].opcode == ZEND_FAST_CALL);
503        op_array->opcodes[op_num - 2].extended_value = ZEND_FAST_CALL_FROM_FINALLY;
504        op_array->opcodes[op_num - 2].op2.opline_num = fast_call;
505    }
506}
507
508static void zend_resolve_fast_call(zend_op_array *op_array, uint32_t fast_call, uint32_t op_num)
509{
510    int i;
511    uint32_t finally_op_num = 0;
512
513    for (i = 0; i < op_array->last_try_catch; i++) {
514        if (op_num >= op_array->try_catch_array[i].finally_op
515                && op_num < op_array->try_catch_array[i].finally_end) {
516            finally_op_num = op_array->try_catch_array[i].finally_op;
517        }
518    }
519
520    if (finally_op_num) {
521        /* Must be ZEND_FAST_CALL */
522        ZEND_ASSERT(op_array->opcodes[finally_op_num - 2].opcode == ZEND_FAST_CALL);
523        if (op_array->opcodes[fast_call].extended_value == 0) {
524            op_array->opcodes[fast_call].extended_value = ZEND_FAST_CALL_FROM_FINALLY;
525            op_array->opcodes[fast_call].op2.opline_num = finally_op_num - 2;
526        }
527    }
528}
529
530static void zend_resolve_finally_call(zend_op_array *op_array, uint32_t op_num, uint32_t dst_num)
531{
532    uint32_t start_op;
533    zend_op *opline;
534    uint32_t i = op_array->last_try_catch;
535
536    if (dst_num != (uint32_t)-1) {
537        zend_check_finally_breakout(op_array, op_num, dst_num);
538    }
539
540    /* the backward order is mater */
541    while (i > 0) {
542        i--;
543        if (op_array->try_catch_array[i].finally_op &&
544            op_num >= op_array->try_catch_array[i].try_op &&
545            op_num < op_array->try_catch_array[i].finally_op - 1 &&
546            (dst_num < op_array->try_catch_array[i].try_op ||
547             dst_num > op_array->try_catch_array[i].finally_end)) {
548            /* we have a jump out of try block that needs executing finally */
549            uint32_t fast_call_var;
550
551            /* Must be ZEND_FAST_RET */
552            ZEND_ASSERT(op_array->opcodes[op_array->try_catch_array[i].finally_end].opcode == ZEND_FAST_RET);
553            fast_call_var = op_array->opcodes[op_array->try_catch_array[i].finally_end].op1.var;
554
555            /* generate a FAST_CALL to finally block */
556            start_op = get_next_op_number(op_array);
557
558            opline = get_next_op(op_array);
559            opline->opcode = ZEND_FAST_CALL;
560            opline->result_type = IS_TMP_VAR;
561            opline->result.var = fast_call_var;
562            SET_UNUSED(opline->op1);
563            SET_UNUSED(opline->op2);
564            zend_adjust_fast_call(op_array, start_op,
565                    op_array->try_catch_array[i].finally_op,
566                    op_array->try_catch_array[i].finally_end);
567            if (op_array->try_catch_array[i].catch_op) {
568                opline->extended_value = ZEND_FAST_CALL_FROM_CATCH;
569                opline->op2.opline_num = op_array->try_catch_array[i].catch_op;
570                opline->op1.opline_num = get_next_op_number(op_array);
571                /* generate a FAST_CALL to hole CALL_FROM_FINALLY */
572                opline = get_next_op(op_array);
573                opline->opcode = ZEND_FAST_CALL;
574                opline->result_type = IS_TMP_VAR;
575                opline->result.var = fast_call_var;
576                SET_UNUSED(opline->op1);
577                SET_UNUSED(opline->op2);
578                zend_resolve_fast_call(op_array, start_op + 1, op_array->try_catch_array[i].finally_op - 2);
579            } else {
580                zend_resolve_fast_call(op_array, start_op, op_array->try_catch_array[i].finally_op - 2);
581            }
582            opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
583
584            /* generate a sequence of FAST_CALL to upward finally block */
585            while (i > 0) {
586                i--;
587                if (op_array->try_catch_array[i].finally_op &&
588                    op_num >= op_array->try_catch_array[i].try_op &&
589                    op_num < op_array->try_catch_array[i].finally_op - 1 &&
590                    (dst_num < op_array->try_catch_array[i].try_op ||
591                     dst_num > op_array->try_catch_array[i].finally_end)) {
592
593                    opline = get_next_op(op_array);
594                    opline->opcode = ZEND_FAST_CALL;
595                    opline->result_type = IS_TMP_VAR;
596                    opline->result.var = fast_call_var;
597                    SET_UNUSED(opline->op1);
598                    SET_UNUSED(opline->op2);
599                    opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
600                }
601            }
602
603            /* Finish the sequence with original opcode */
604            opline = get_next_op(op_array);
605            *opline = op_array->opcodes[op_num];
606
607            /* Replace original opcode with jump to this sequence */
608            opline = op_array->opcodes + op_num;
609            opline->opcode = ZEND_JMP;
610            SET_UNUSED(opline->op1);
611            SET_UNUSED(opline->op2);
612            opline->op1.opline_num = start_op;
613
614            break;
615        }
616    }
617}
618
619static void zend_resolve_finally_ret(zend_op_array *op_array, uint32_t op_num)
620{
621    int i;
622    uint32_t catch_op_num = 0, finally_op_num = 0;
623
624    for (i = 0; i < op_array->last_try_catch; i++) {
625        if (op_array->try_catch_array[i].try_op > op_num) {
626            break;
627        }
628        if (op_num < op_array->try_catch_array[i].finally_op) {
629            finally_op_num = op_array->try_catch_array[i].finally_op;
630        }
631        if (op_num < op_array->try_catch_array[i].catch_op) {
632            catch_op_num = op_array->try_catch_array[i].catch_op;
633        }
634    }
635
636    if (finally_op_num && (!catch_op_num || catch_op_num >= finally_op_num)) {
637        /* in case of unhandled exception return to upward finally block */
638        op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_FINALLY;
639        op_array->opcodes[op_num].op2.opline_num = finally_op_num;
640    } else if (catch_op_num) {
641        /* in case of unhandled exception return to upward catch block */
642        op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_CATCH;
643        op_array->opcodes[op_num].op2.opline_num = catch_op_num;
644    }
645}
646
647static void zend_resolve_finally_calls(zend_op_array *op_array)
648{
649    uint32_t i, j;
650    zend_op *opline;
651
652    for (i = 0, j = op_array->last; i < j; i++) {
653        opline = op_array->opcodes + i;
654        switch (opline->opcode) {
655            case ZEND_RETURN:
656            case ZEND_RETURN_BY_REF:
657            case ZEND_GENERATOR_RETURN:
658                zend_resolve_finally_call(op_array, i, (uint32_t)-1);
659                break;
660            case ZEND_BRK:
661            case ZEND_CONT:
662            {
663                int nest_levels, array_offset;
664                zend_brk_cont_element *jmp_to;
665
666                nest_levels = Z_LVAL(op_array->literals[opline->op2.constant]);
667                if ((array_offset = opline->op1.opline_num) != -1) {
668                    do {
669                        jmp_to = &op_array->brk_cont_array[array_offset];
670                        if (nest_levels > 1) {
671                            array_offset = jmp_to->parent;
672                        }
673                    } while (--nest_levels > 0);
674                    zend_resolve_finally_call(op_array, i, opline->opcode == ZEND_BRK ? jmp_to->brk : jmp_to->cont);
675                    break;
676                }
677            }
678            case ZEND_GOTO:
679                if (Z_TYPE_P(CT_CONSTANT_EX(op_array, opline->op2.constant)) != IS_LONG) {
680                    uint32_t num = opline->op2.constant;
681
682                    ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op2);
683                    zend_resolve_goto_label(op_array, opline, 1);
684                    opline->op2.constant = num;
685                }
686                /* break omitted intentionally */
687            case ZEND_JMP:
688                zend_resolve_finally_call(op_array, i, opline->op1.opline_num);
689                break;
690            case ZEND_FAST_CALL:
691                zend_resolve_fast_call(op_array, i, i);
692                break;
693            case ZEND_FAST_RET:
694                zend_resolve_finally_ret(op_array, i);
695                break;
696            default:
697                break;
698        }
699    }
700}
701
702ZEND_API int pass_two(zend_op_array *op_array)
703{
704    zend_op *opline, *end;
705
706    if (!ZEND_USER_CODE(op_array->type)) {
707        return 0;
708    }
709    if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
710        zend_resolve_finally_calls(op_array);
711    }
712    if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) {
713        zend_update_extended_info(op_array);
714    }
715    if (CG(compiler_options) & ZEND_COMPILE_HANDLE_OP_ARRAY) {
716        zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_handler, op_array);
717    }
718
719    if (CG(context).vars_size != op_array->last_var) {
720        op_array->vars = (zend_string**) erealloc(op_array->vars, sizeof(zend_string*)*op_array->last_var);
721        CG(context).vars_size = op_array->last_var;
722    }
723    if (CG(context).opcodes_size != op_array->last) {
724        op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last);
725        CG(context).opcodes_size = op_array->last;
726    }
727    if (CG(context).literals_size != op_array->last_literal) {
728        op_array->literals = (zval*)erealloc(op_array->literals, sizeof(zval) * op_array->last_literal);
729        CG(context).literals_size = op_array->last_literal;
730    }
731    opline = op_array->opcodes;
732    end = opline + op_array->last;
733    while (opline < end) {
734        if (opline->op1_type == IS_CONST) {
735            ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op1);
736        } else if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
737            opline->op1.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->op1.var);
738        }
739        if (opline->op2_type == IS_CONST) {
740            ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op2);
741        } else if (opline->op2_type & (IS_VAR|IS_TMP_VAR)) {
742            opline->op2.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->op2.var);
743        }
744        if (opline->result_type & (IS_VAR|IS_TMP_VAR)) {
745            opline->result.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->result.var);
746        }
747        switch (opline->opcode) {
748            case ZEND_DECLARE_INHERITED_CLASS:
749            case ZEND_DECLARE_INHERITED_CLASS_DELAYED:
750                opline->extended_value = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->extended_value);
751                break;
752            case ZEND_GOTO:
753                if (Z_TYPE_P(RT_CONSTANT(op_array, opline->op2)) != IS_LONG) {
754                    zend_resolve_goto_label(op_array, opline, 1);
755                }
756                /* break omitted intentionally */
757            case ZEND_JMP:
758            case ZEND_FAST_CALL:
759                ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op1);
760                break;
761            case ZEND_JMPZNZ:
762                /* absolute index to relative offset */
763                opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline->extended_value);
764                /* break omitted intentionally */
765            case ZEND_JMPZ:
766            case ZEND_JMPNZ:
767            case ZEND_JMPZ_EX:
768            case ZEND_JMPNZ_EX:
769            case ZEND_JMP_SET:
770            case ZEND_COALESCE:
771            case ZEND_NEW:
772            case ZEND_FE_RESET_R:
773            case ZEND_FE_RESET_RW:
774            case ZEND_FE_FETCH_R:
775            case ZEND_FE_FETCH_RW:
776                ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2);
777                break;
778            case ZEND_VERIFY_RETURN_TYPE:
779                if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
780                    MAKE_NOP(opline);
781                }
782                break;
783            case ZEND_RETURN:
784            case ZEND_RETURN_BY_REF:
785                if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
786                    if (opline->op1_type != IS_CONST || Z_TYPE_P(RT_CONSTANT(op_array, opline->op1)) != IS_NULL) {
787                        CG(zend_lineno) = opline->lineno;
788                        zend_error_noreturn(E_COMPILE_ERROR, "Generators cannot return values using \"return\"");
789                    }
790
791                    opline->opcode = ZEND_GENERATOR_RETURN;
792                }
793                break;
794        }
795        ZEND_VM_SET_OPCODE_HANDLER(opline);
796        opline++;
797    }
798
799    op_array->fn_flags |= ZEND_ACC_DONE_PASS_TWO;
800    return 0;
801}
802
803int pass_two_wrapper(zval *el)
804{
805    return pass_two((zend_op_array *) Z_PTR_P(el));
806}
807
808int print_class(zend_class_entry *class_entry)
809{
810    printf("Class %s:\n", class_entry->name->val);
811    zend_hash_apply(&class_entry->function_table, pass_two_wrapper);
812    printf("End of class %s.\n\n", class_entry->name->val);
813    return 0;
814}
815
816ZEND_API unary_op_type get_unary_op(int opcode)
817{
818    switch (opcode) {
819        case ZEND_BW_NOT:
820            return (unary_op_type) bitwise_not_function;
821        case ZEND_BOOL_NOT:
822            return (unary_op_type) boolean_not_function;
823        default:
824            return (unary_op_type) NULL;
825    }
826}
827
828ZEND_API binary_op_type get_binary_op(int opcode)
829{
830    switch (opcode) {
831        case ZEND_ADD:
832        case ZEND_ASSIGN_ADD:
833            return (binary_op_type) add_function;
834        case ZEND_SUB:
835        case ZEND_ASSIGN_SUB:
836            return (binary_op_type) sub_function;
837        case ZEND_MUL:
838        case ZEND_ASSIGN_MUL:
839            return (binary_op_type) mul_function;
840        case ZEND_POW:
841            return (binary_op_type) pow_function;
842        case ZEND_DIV:
843        case ZEND_ASSIGN_DIV:
844            return (binary_op_type) div_function;
845        case ZEND_MOD:
846        case ZEND_ASSIGN_MOD:
847            return (binary_op_type) mod_function;
848        case ZEND_SL:
849        case ZEND_ASSIGN_SL:
850            return (binary_op_type) shift_left_function;
851        case ZEND_SR:
852        case ZEND_ASSIGN_SR:
853            return (binary_op_type) shift_right_function;
854        case ZEND_CONCAT:
855        case ZEND_ASSIGN_CONCAT:
856            return (binary_op_type) concat_function;
857        case ZEND_IS_IDENTICAL:
858            return (binary_op_type) is_identical_function;
859        case ZEND_IS_NOT_IDENTICAL:
860            return (binary_op_type) is_not_identical_function;
861        case ZEND_IS_EQUAL:
862            return (binary_op_type) is_equal_function;
863        case ZEND_IS_NOT_EQUAL:
864            return (binary_op_type) is_not_equal_function;
865        case ZEND_IS_SMALLER:
866            return (binary_op_type) is_smaller_function;
867        case ZEND_IS_SMALLER_OR_EQUAL:
868            return (binary_op_type) is_smaller_or_equal_function;
869        case ZEND_SPACESHIP:
870            return (binary_op_type) compare_function;
871        case ZEND_BW_OR:
872        case ZEND_ASSIGN_BW_OR:
873            return (binary_op_type) bitwise_or_function;
874        case ZEND_BW_AND:
875        case ZEND_ASSIGN_BW_AND:
876            return (binary_op_type) bitwise_and_function;
877        case ZEND_BW_XOR:
878        case ZEND_ASSIGN_BW_XOR:
879            return (binary_op_type) bitwise_xor_function;
880        case ZEND_BOOL_XOR:
881            return (binary_op_type) boolean_xor_function;
882        default:
883            return (binary_op_type) NULL;
884    }
885}
886
887/*
888 * Local variables:
889 * tab-width: 4
890 * c-basic-offset: 4
891 * indent-tabs-mode: t
892 * End:
893 */
894