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