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