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