1/*
2   +----------------------------------------------------------------------+
3   | Zend Engine                                                          |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1998-2014 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->last_cache_slot = 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        zend_hash_clean(op_array->static_variables);
135    }
136}
137
138ZEND_API void zend_cleanup_user_class_data(zend_class_entry *ce)
139{
140    /* Clean all parts that can contain run-time data */
141    /* Note that only run-time accessed data need to be cleaned up, pre-defined data can
142       not contain objects and thus are not probelmatic */
143    if (ce->ce_flags & ZEND_HAS_STATIC_IN_METHODS) {
144        zend_function *func;
145
146        ZEND_HASH_FOREACH_PTR(&ce->function_table, func) {
147            if (func->type == ZEND_USER_FUNCTION) {
148                zend_cleanup_op_array_data((zend_op_array *) func);
149            }
150        } ZEND_HASH_FOREACH_END();
151    }
152    if (ce->static_members_table) {
153        int i;
154
155        for (i = 0; i < ce->default_static_members_count; i++) {
156            if (Z_TYPE(ce->static_members_table[i]) != IS_UNDEF) {
157                zval tmp;
158
159                ZVAL_COPY_VALUE(&tmp, &ce->static_members_table[i]);
160                ZVAL_UNDEF(&ce->static_members_table[i]);
161                zval_ptr_dtor(&tmp);
162            }
163        }
164        ce->static_members_table = NULL;
165    }
166}
167
168ZEND_API void zend_cleanup_internal_class_data(zend_class_entry *ce)
169{
170    if (CE_STATIC_MEMBERS(ce)) {
171        int i;
172
173        for (i = 0; i < ce->default_static_members_count; i++) {
174            zval_ptr_dtor(&CE_STATIC_MEMBERS(ce)[i]);
175        }
176        efree(CE_STATIC_MEMBERS(ce));
177#ifdef ZTS
178        CG(static_members_table)[(zend_intptr_t)(ce->static_members_table)] = NULL;
179#else
180        ce->static_members_table = NULL;
181#endif
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        zend_hash_destroy(op_array->static_variables);
323        FREE_HASHTABLE(op_array->static_variables);
324    }
325
326    if (op_array->run_time_cache && !op_array->function_name) {
327        efree(op_array->run_time_cache);
328    }
329
330    if (--(*op_array->refcount)>0) {
331        return;
332    }
333
334    efree_size(op_array->refcount, sizeof(*(op_array->refcount)));
335
336    if (op_array->vars) {
337        i = op_array->last_var;
338        while (i > 0) {
339            i--;
340            zend_string_release(op_array->vars[i]);
341        }
342        efree(op_array->vars);
343    }
344
345    if (literal) {
346        end = literal + op_array->last_literal;
347        while (literal < end) {
348            zval_ptr_dtor_nogc(literal);
349            literal++;
350        }
351        efree(op_array->literals);
352    }
353    efree(op_array->opcodes);
354
355    if (op_array->function_name) {
356        zend_string_release(op_array->function_name);
357    }
358    if (op_array->doc_comment) {
359        zend_string_release(op_array->doc_comment);
360    }
361    if (op_array->brk_cont_array) {
362        efree(op_array->brk_cont_array);
363    }
364    if (op_array->try_catch_array) {
365        efree(op_array->try_catch_array);
366    }
367    if (op_array->fn_flags & ZEND_ACC_DONE_PASS_TWO) {
368        zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_dtor_handler, op_array);
369    }
370    if (op_array->arg_info) {
371        for (i=0; i<op_array->num_args; i++) {
372            zend_string_release(op_array->arg_info[i].name);
373            if (op_array->arg_info[i].class_name) {
374                zend_string_release(op_array->arg_info[i].class_name);
375            }
376        }
377        efree(op_array->arg_info);
378    }
379}
380
381void init_op(zend_op *op)
382{
383    memset(op, 0, sizeof(zend_op));
384    op->lineno = CG(zend_lineno);
385    SET_UNUSED(op->result);
386}
387
388zend_op *get_next_op(zend_op_array *op_array)
389{
390    uint32_t next_op_num = op_array->last++;
391    zend_op *next_op;
392
393    if (next_op_num >= CG(context).opcodes_size) {
394        CG(context).opcodes_size *= 4;
395        op_array_alloc_ops(op_array, CG(context).opcodes_size);
396    }
397
398    next_op = &(op_array->opcodes[next_op_num]);
399
400    init_op(next_op);
401
402    return next_op;
403}
404
405int get_next_op_number(zend_op_array *op_array)
406{
407    return op_array->last;
408}
409
410zend_brk_cont_element *get_next_brk_cont_element(zend_op_array *op_array)
411{
412    op_array->last_brk_cont++;
413    op_array->brk_cont_array = erealloc(op_array->brk_cont_array, sizeof(zend_brk_cont_element)*op_array->last_brk_cont);
414    return &op_array->brk_cont_array[op_array->last_brk_cont-1];
415}
416
417static void zend_update_extended_info(zend_op_array *op_array)
418{
419    zend_op *opline = op_array->opcodes, *end=opline+op_array->last;
420
421    while (opline<end) {
422        if (opline->opcode == ZEND_EXT_STMT) {
423            if (opline+1<end) {
424                if ((opline+1)->opcode == ZEND_EXT_STMT) {
425                    opline->opcode = ZEND_NOP;
426                    opline++;
427                    continue;
428                }
429                if (opline+1<end) {
430                    opline->lineno = (opline+1)->lineno;
431                }
432            } else {
433                opline->opcode = ZEND_NOP;
434            }
435        }
436        opline++;
437    }
438}
439
440static void zend_extension_op_array_handler(zend_extension *extension, zend_op_array *op_array)
441{
442    if (extension->op_array_handler) {
443        extension->op_array_handler(op_array);
444    }
445}
446
447static void zend_check_finally_breakout(zend_op_array *op_array, uint32_t op_num, uint32_t dst_num)
448{
449    int i;
450
451    for (i = 0; i < op_array->last_try_catch; i++) {
452        if ((op_num < op_array->try_catch_array[i].finally_op ||
453                    op_num >= op_array->try_catch_array[i].finally_end)
454                && (dst_num >= op_array->try_catch_array[i].finally_op &&
455                     dst_num <= op_array->try_catch_array[i].finally_end)) {
456            CG(in_compilation) = 1;
457            CG(active_op_array) = op_array;
458            CG(zend_lineno) = op_array->opcodes[op_num].lineno;
459            zend_error_noreturn(E_COMPILE_ERROR, "jump into a finally block is disallowed");
460        } else if ((op_num >= op_array->try_catch_array[i].finally_op
461                    && op_num <= op_array->try_catch_array[i].finally_end)
462                && (dst_num > op_array->try_catch_array[i].finally_end
463                    || dst_num < op_array->try_catch_array[i].finally_op)) {
464            CG(in_compilation) = 1;
465            CG(active_op_array) = op_array;
466            CG(zend_lineno) = op_array->opcodes[op_num].lineno;
467            zend_error_noreturn(E_COMPILE_ERROR, "jump out of a finally block is disallowed");
468        }
469    }
470}
471
472static void zend_adjust_fast_call(zend_op_array *op_array, uint32_t fast_call, uint32_t start, uint32_t end)
473{
474    int i;
475    uint32_t op_num = 0;
476
477    for (i = 0; i < op_array->last_try_catch; i++) {
478        if (op_array->try_catch_array[i].finally_op > start
479                && op_array->try_catch_array[i].finally_end < end) {
480            op_num = op_array->try_catch_array[i].finally_op;
481            start = op_array->try_catch_array[i].finally_end;
482        }
483    }
484
485    if (op_num) {
486        /* Must be ZEND_FAST_CALL */
487        ZEND_ASSERT(op_array->opcodes[op_num - 2].opcode == ZEND_FAST_CALL);
488        op_array->opcodes[op_num - 2].extended_value = ZEND_FAST_CALL_FROM_FINALLY;
489        op_array->opcodes[op_num - 2].op2.opline_num = fast_call;
490    }
491}
492
493static void zend_resolve_fast_call(zend_op_array *op_array, uint32_t fast_call, uint32_t op_num)
494{
495    int i;
496    uint32_t finally_op_num = 0;
497
498    for (i = 0; i < op_array->last_try_catch; i++) {
499        if (op_num >= op_array->try_catch_array[i].finally_op
500                && op_num < op_array->try_catch_array[i].finally_end) {
501            finally_op_num = op_array->try_catch_array[i].finally_op;
502        }
503    }
504
505    if (finally_op_num) {
506        /* Must be ZEND_FAST_CALL */
507        ZEND_ASSERT(op_array->opcodes[finally_op_num - 2].opcode == ZEND_FAST_CALL);
508        if (op_array->opcodes[fast_call].extended_value == 0) {
509            op_array->opcodes[fast_call].extended_value = ZEND_FAST_CALL_FROM_FINALLY;
510            op_array->opcodes[fast_call].op2.opline_num = finally_op_num - 2;
511        }
512    }
513}
514
515static void zend_resolve_finally_call(zend_op_array *op_array, uint32_t op_num, uint32_t dst_num)
516{
517    uint32_t start_op;
518    zend_op *opline;
519    uint32_t i = op_array->last_try_catch;
520
521    if (dst_num != (uint32_t)-1) {
522        zend_check_finally_breakout(op_array, op_num, dst_num);
523    }
524
525    /* the backward order is mater */
526    while (i > 0) {
527        i--;
528        if (op_array->try_catch_array[i].finally_op &&
529            op_num >= op_array->try_catch_array[i].try_op &&
530            op_num < op_array->try_catch_array[i].finally_op - 1 &&
531            (dst_num < op_array->try_catch_array[i].try_op ||
532             dst_num > op_array->try_catch_array[i].finally_end)) {
533            /* we have a jump out of try block that needs executing finally */
534            uint32_t fast_call_var;
535
536            /* Must be ZEND_FAST_RET */
537            ZEND_ASSERT(op_array->opcodes[op_array->try_catch_array[i].finally_end].opcode == ZEND_FAST_RET);
538            fast_call_var = op_array->opcodes[op_array->try_catch_array[i].finally_end].op1.var;
539
540            /* generate a FAST_CALL to finally block */
541            start_op = get_next_op_number(op_array);
542
543            opline = get_next_op(op_array);
544            opline->opcode = ZEND_FAST_CALL;
545            opline->result_type = IS_TMP_VAR;
546            opline->result.var = fast_call_var;
547            SET_UNUSED(opline->op1);
548            SET_UNUSED(opline->op2);
549            zend_adjust_fast_call(op_array, start_op,
550                    op_array->try_catch_array[i].finally_op,
551                    op_array->try_catch_array[i].finally_end);
552            if (op_array->try_catch_array[i].catch_op) {
553                opline->extended_value = ZEND_FAST_CALL_FROM_CATCH;
554                opline->op2.opline_num = op_array->try_catch_array[i].catch_op;
555                opline->op1.opline_num = get_next_op_number(op_array);
556                /* generate a FAST_CALL to hole CALL_FROM_FINALLY */
557                opline = get_next_op(op_array);
558                opline->opcode = ZEND_FAST_CALL;
559                opline->result_type = IS_TMP_VAR;
560                opline->result.var = fast_call_var;
561                SET_UNUSED(opline->op1);
562                SET_UNUSED(opline->op2);
563                zend_resolve_fast_call(op_array, start_op + 1, op_array->try_catch_array[i].finally_op - 2);
564            } else {
565                zend_resolve_fast_call(op_array, start_op, op_array->try_catch_array[i].finally_op - 2);
566            }
567            opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
568
569            /* generate a sequence of FAST_CALL to upward finally block */
570            while (i > 0) {
571                i--;
572                if (op_array->try_catch_array[i].finally_op &&
573                    op_num >= op_array->try_catch_array[i].try_op &&
574                    op_num < op_array->try_catch_array[i].finally_op - 1 &&
575                    (dst_num < op_array->try_catch_array[i].try_op ||
576                     dst_num > op_array->try_catch_array[i].finally_end)) {
577
578                    opline = get_next_op(op_array);
579                    opline->opcode = ZEND_FAST_CALL;
580                    opline->result_type = IS_TMP_VAR;
581                    opline->result.var = fast_call_var;
582                    SET_UNUSED(opline->op1);
583                    SET_UNUSED(opline->op2);
584                    opline->op1.opline_num = op_array->try_catch_array[i].finally_op;
585                }
586            }
587
588            /* Finish the sequence with original opcode */
589            opline = get_next_op(op_array);
590            *opline = op_array->opcodes[op_num];
591
592            /* Replace original opcode with jump to this sequence */
593            opline = op_array->opcodes + op_num;
594            opline->opcode = ZEND_JMP;
595            SET_UNUSED(opline->op1);
596            SET_UNUSED(opline->op2);
597            opline->op1.opline_num = start_op;
598
599            break;
600        }
601    }
602}
603
604static void zend_resolve_finally_ret(zend_op_array *op_array, uint32_t op_num)
605{
606    int i;
607    uint32_t catch_op_num = 0, finally_op_num = 0;
608
609    for (i = 0; i < op_array->last_try_catch; i++) {
610        if (op_array->try_catch_array[i].try_op > op_num) {
611            break;
612        }
613        if (op_num < op_array->try_catch_array[i].finally_op) {
614            finally_op_num = op_array->try_catch_array[i].finally_op;
615        }
616        if (op_num < op_array->try_catch_array[i].catch_op) {
617            catch_op_num = op_array->try_catch_array[i].catch_op;
618        }
619    }
620
621    if (finally_op_num && (!catch_op_num || catch_op_num >= finally_op_num)) {
622        /* in case of unhandled exception return to upward finally block */
623        op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_FINALLY;
624        op_array->opcodes[op_num].op2.opline_num = finally_op_num;
625    } else if (catch_op_num) {
626        /* in case of unhandled exception return to upward catch block */
627        op_array->opcodes[op_num].extended_value = ZEND_FAST_RET_TO_CATCH;
628        op_array->opcodes[op_num].op2.opline_num = catch_op_num;
629    }
630}
631
632static void zend_resolve_finally_calls(zend_op_array *op_array)
633{
634    uint32_t i, j;
635    zend_op *opline;
636
637    for (i = 0, j = op_array->last; i < j; i++) {
638        opline = op_array->opcodes + i;
639        switch (opline->opcode) {
640            case ZEND_RETURN:
641            case ZEND_RETURN_BY_REF:
642            case ZEND_GENERATOR_RETURN:
643                zend_resolve_finally_call(op_array, i, (uint32_t)-1);
644                break;
645            case ZEND_BRK:
646            case ZEND_CONT:
647            {
648                int nest_levels, array_offset;
649                zend_brk_cont_element *jmp_to;
650
651                nest_levels = Z_LVAL(op_array->literals[opline->op2.constant]);
652                if ((array_offset = opline->op1.opline_num) != -1) {
653                    do {
654                        jmp_to = &op_array->brk_cont_array[array_offset];
655                        if (nest_levels > 1) {
656                            array_offset = jmp_to->parent;
657                        }
658                    } while (--nest_levels > 0);
659                    zend_resolve_finally_call(op_array, i, opline->opcode == ZEND_BRK ? jmp_to->brk : jmp_to->cont);
660                    break;
661                }
662            }
663            case ZEND_GOTO:
664                if (Z_TYPE_P(CT_CONSTANT_EX(op_array, opline->op2.constant)) != IS_LONG) {
665                    uint32_t num = opline->op2.constant;
666
667                    ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op2);
668                    zend_resolve_goto_label(op_array, opline, 1);
669                    opline->op2.constant = num;
670                }
671                /* break omitted intentionally */
672            case ZEND_JMP:
673                zend_resolve_finally_call(op_array, i, opline->op1.opline_num);
674                break;
675            case ZEND_FAST_CALL:
676                zend_resolve_fast_call(op_array, i, i);
677                break;
678            case ZEND_FAST_RET:
679                zend_resolve_finally_ret(op_array, i);
680                break;
681            default:
682                break;
683        }
684    }
685}
686
687ZEND_API int pass_two(zend_op_array *op_array)
688{
689    zend_op *opline, *end;
690
691    if (!ZEND_USER_CODE(op_array->type)) {
692        return 0;
693    }
694    if (op_array->fn_flags & ZEND_ACC_HAS_FINALLY_BLOCK) {
695        zend_resolve_finally_calls(op_array);
696    }
697    if (CG(compiler_options) & ZEND_COMPILE_EXTENDED_INFO) {
698        zend_update_extended_info(op_array);
699    }
700    if (CG(compiler_options) & ZEND_COMPILE_HANDLE_OP_ARRAY) {
701        zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) zend_extension_op_array_handler, op_array);
702    }
703
704    if (CG(context).vars_size != op_array->last_var) {
705        op_array->vars = (zend_string**) erealloc(op_array->vars, sizeof(zend_string*)*op_array->last_var);
706        CG(context).vars_size = op_array->last_var;
707    }
708    if (CG(context).opcodes_size != op_array->last) {
709        op_array->opcodes = (zend_op *) erealloc(op_array->opcodes, sizeof(zend_op)*op_array->last);
710        CG(context).opcodes_size = op_array->last;
711    }
712    if (CG(context).literals_size != op_array->last_literal) {
713        op_array->literals = (zval*)erealloc(op_array->literals, sizeof(zval) * op_array->last_literal);
714        CG(context).literals_size = op_array->last_literal;
715    }
716    opline = op_array->opcodes;
717    end = opline + op_array->last;
718    while (opline < end) {
719        if (opline->op1_type == IS_CONST) {
720            ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op1);
721        } else if (opline->op1_type & (IS_VAR|IS_TMP_VAR)) {
722            opline->op1.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->op1.var);
723        }
724        if (opline->op2_type == IS_CONST) {
725            ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline->op2);
726        } else if (opline->op2_type & (IS_VAR|IS_TMP_VAR)) {
727            opline->op2.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->op2.var);
728        }
729        if (opline->result_type & (IS_VAR|IS_TMP_VAR)) {
730            opline->result.var = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->result.var);
731        }
732        switch (opline->opcode) {
733            case ZEND_DECLARE_INHERITED_CLASS:
734            case ZEND_DECLARE_INHERITED_CLASS_DELAYED:
735                opline->extended_value = (uint32_t)(zend_intptr_t)ZEND_CALL_VAR_NUM(NULL, op_array->last_var + opline->extended_value);
736                break;
737            case ZEND_GOTO:
738                if (Z_TYPE_P(RT_CONSTANT(op_array, opline->op2)) != IS_LONG) {
739                    zend_resolve_goto_label(op_array, opline, 1);
740                }
741                /* break omitted intentionally */
742            case ZEND_JMP:
743            case ZEND_FAST_CALL:
744                ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op1);
745                break;
746            case ZEND_JMPZNZ:
747                /* absolute index to relative offset */
748                opline->extended_value = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline->extended_value);
749                /* break omitted intentionally */
750            case ZEND_JMPZ:
751            case ZEND_JMPNZ:
752            case ZEND_JMPZ_EX:
753            case ZEND_JMPNZ_EX:
754            case ZEND_JMP_SET:
755            case ZEND_COALESCE:
756            case ZEND_NEW:
757            case ZEND_FE_RESET:
758            case ZEND_FE_FETCH:
759                ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, opline->op2);
760                break;
761            case ZEND_RETURN:
762            case ZEND_RETURN_BY_REF:
763                if (op_array->fn_flags & ZEND_ACC_GENERATOR) {
764                    if (opline->op1_type != IS_CONST || Z_TYPE_P(RT_CONSTANT(op_array, opline->op1)) != IS_NULL) {
765                        CG(zend_lineno) = opline->lineno;
766                        zend_error_noreturn(E_COMPILE_ERROR, "Generators cannot return values using \"return\"");
767                    }
768
769                    opline->opcode = ZEND_GENERATOR_RETURN;
770                }
771                break;
772        }
773        ZEND_VM_SET_OPCODE_HANDLER(opline);
774        opline++;
775    }
776
777    op_array->fn_flags |= ZEND_ACC_DONE_PASS_TWO;
778    return 0;
779}
780
781int pass_two_wrapper(zval *el)
782{
783    return pass_two((zend_op_array *) Z_PTR_P(el));
784}
785
786int print_class(zend_class_entry *class_entry)
787{
788    printf("Class %s:\n", class_entry->name->val);
789    zend_hash_apply(&class_entry->function_table, pass_two_wrapper);
790    printf("End of class %s.\n\n", class_entry->name->val);
791    return 0;
792}
793
794ZEND_API unary_op_type get_unary_op(int opcode)
795{
796    switch (opcode) {
797        case ZEND_BW_NOT:
798            return (unary_op_type) bitwise_not_function;
799        case ZEND_BOOL_NOT:
800            return (unary_op_type) boolean_not_function;
801        default:
802            return (unary_op_type) NULL;
803    }
804}
805
806ZEND_API binary_op_type get_binary_op(int opcode)
807{
808    switch (opcode) {
809        case ZEND_ADD:
810        case ZEND_ASSIGN_ADD:
811            return (binary_op_type) add_function;
812        case ZEND_SUB:
813        case ZEND_ASSIGN_SUB:
814            return (binary_op_type) sub_function;
815        case ZEND_MUL:
816        case ZEND_ASSIGN_MUL:
817            return (binary_op_type) mul_function;
818        case ZEND_POW:
819            return (binary_op_type) pow_function;
820        case ZEND_DIV:
821        case ZEND_ASSIGN_DIV:
822            return (binary_op_type) div_function;
823        case ZEND_MOD:
824        case ZEND_ASSIGN_MOD:
825            return (binary_op_type) mod_function;
826        case ZEND_SL:
827        case ZEND_ASSIGN_SL:
828            return (binary_op_type) shift_left_function;
829        case ZEND_SR:
830        case ZEND_ASSIGN_SR:
831            return (binary_op_type) shift_right_function;
832        case ZEND_CONCAT:
833        case ZEND_ASSIGN_CONCAT:
834            return (binary_op_type) concat_function;
835        case ZEND_IS_IDENTICAL:
836            return (binary_op_type) is_identical_function;
837        case ZEND_IS_NOT_IDENTICAL:
838            return (binary_op_type) is_not_identical_function;
839        case ZEND_IS_EQUAL:
840            return (binary_op_type) is_equal_function;
841        case ZEND_IS_NOT_EQUAL:
842            return (binary_op_type) is_not_equal_function;
843        case ZEND_IS_SMALLER:
844            return (binary_op_type) is_smaller_function;
845        case ZEND_IS_SMALLER_OR_EQUAL:
846            return (binary_op_type) is_smaller_or_equal_function;
847        case ZEND_BW_OR:
848        case ZEND_ASSIGN_BW_OR:
849            return (binary_op_type) bitwise_or_function;
850        case ZEND_BW_AND:
851        case ZEND_ASSIGN_BW_AND:
852            return (binary_op_type) bitwise_and_function;
853        case ZEND_BW_XOR:
854        case ZEND_ASSIGN_BW_XOR:
855            return (binary_op_type) bitwise_xor_function;
856        case ZEND_BOOL_XOR:
857            return (binary_op_type) boolean_xor_function;
858        default:
859            return (binary_op_type) NULL;
860    }
861}
862
863/*
864 * Local variables:
865 * tab-width: 4
866 * c-basic-offset: 4
867 * indent-tabs-mode: t
868 * End:
869 */
870