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#ifndef ZEND_COMPILE_H
23#define ZEND_COMPILE_H
24
25#include "zend.h"
26#include "zend_ast.h"
27
28#ifdef HAVE_STDARG_H
29# include <stdarg.h>
30#endif
31
32#include "zend_llist.h"
33
34#define DEBUG_ZEND 0
35
36#define SET_UNUSED(op)  op ## _type = IS_UNUSED
37
38#define MAKE_NOP(opline) do { \
39    opline->opcode = ZEND_NOP; \
40    memset(&opline->result, 0, sizeof(opline->result)); \
41    memset(&opline->op1, 0, sizeof(opline->op1)); \
42    memset(&opline->op2, 0, sizeof(opline->op2)); \
43    opline->result_type = opline->op1_type = opline->op2_type = IS_UNUSED; \
44} while (0)
45
46#define RESET_DOC_COMMENT() do { \
47    if (CG(doc_comment)) { \
48        zend_string_release(CG(doc_comment)); \
49        CG(doc_comment) = NULL; \
50    } \
51} while (0)
52
53typedef struct _zend_op_array zend_op_array;
54typedef struct _zend_op zend_op;
55
56typedef struct _zend_compiler_context {
57    uint32_t  opcodes_size;
58    int        vars_size;
59    int        literals_size;
60    int        current_brk_cont;
61    int        backpatch_count;
62    int        in_finally;
63    uint32_t   fast_call_var;
64    HashTable *labels;
65} zend_compiler_context;
66
67/* On 64-bi systems less optimal, but more compact VM code leads to better
68 * performance. So on 32-bit systems we use absolute addresses for jump
69 * targets and constants, but on 64-bit systems realtive 32-bit offsets */
70#if SIZEOF_SIZE_T == 4
71# define ZEND_USE_ABS_JMP_ADDR      1
72# define ZEND_USE_ABS_CONST_ADDR    1
73# define ZEND_EX_USE_LITERALS       0
74# define ZEND_EX_USE_RUN_TIME_CACHE 1
75#else
76# define ZEND_USE_ABS_JMP_ADDR      0
77# define ZEND_USE_ABS_CONST_ADDR    0
78# define ZEND_EX_USE_LITERALS       1
79# define ZEND_EX_USE_RUN_TIME_CACHE 1
80#endif
81
82typedef union _znode_op {
83    uint32_t      constant;
84    uint32_t      var;
85    uint32_t      num;
86    uint32_t      opline_num; /*  Needs to be signed */
87#if ZEND_USE_ABS_JMP_ADDR
88    zend_op       *jmp_addr;
89#else
90    uint32_t      jmp_offset;
91#endif
92#if ZEND_USE_ABS_CONST_ADDR
93    zval          *zv;
94#endif
95} znode_op;
96
97typedef struct _znode { /* used only during compilation */
98    int op_type;
99    union {
100        znode_op op;
101        zval constant; /* replaced by literal/zv */
102    } u;
103} znode;
104
105/* Temporarily defined here, to avoid header ordering issues */
106typedef struct _zend_ast_znode {
107    zend_ast_kind kind;
108    zend_ast_attr attr;
109    uint32_t lineno;
110    znode node;
111} zend_ast_znode;
112ZEND_API zend_ast *zend_ast_create_znode(znode *node);
113
114static zend_always_inline znode *zend_ast_get_znode(zend_ast *ast) {
115    return &((zend_ast_znode *) ast)->node;
116}
117
118typedef union _zend_parser_stack_elem {
119    zend_ast *ast;
120    zend_string *str;
121    zend_ulong num;
122} zend_parser_stack_elem;
123
124void zend_compile_top_stmt(zend_ast *ast);
125void zend_compile_stmt(zend_ast *ast);
126void zend_compile_expr(znode *node, zend_ast *ast);
127void zend_compile_var(znode *node, zend_ast *ast, uint32_t type);
128void zend_eval_const_expr(zend_ast **ast_ptr);
129void zend_const_expr_to_zval(zval *result, zend_ast *ast);
130
131#define ZEND_OPCODE_HANDLER_ARGS zend_execute_data *execute_data
132#define ZEND_OPCODE_HANDLER_ARGS_PASSTHRU execute_data
133
134typedef int (*user_opcode_handler_t) (ZEND_OPCODE_HANDLER_ARGS);
135typedef int (ZEND_FASTCALL *opcode_handler_t) (ZEND_OPCODE_HANDLER_ARGS);
136
137extern ZEND_API opcode_handler_t *zend_opcode_handlers;
138
139struct _zend_op {
140    opcode_handler_t handler;
141    znode_op op1;
142    znode_op op2;
143    znode_op result;
144    uint32_t extended_value;
145    uint32_t lineno;
146    zend_uchar opcode;
147    zend_uchar op1_type;
148    zend_uchar op2_type;
149    zend_uchar result_type;
150};
151
152
153typedef struct _zend_brk_cont_element {
154    int start;
155    int cont;
156    int brk;
157    int parent;
158} zend_brk_cont_element;
159
160typedef struct _zend_label {
161    int brk_cont;
162    uint32_t opline_num;
163} zend_label;
164
165typedef struct _zend_try_catch_element {
166    uint32_t try_op;
167    uint32_t catch_op;  /* ketchup! */
168    uint32_t finally_op;
169    uint32_t finally_end;
170} zend_try_catch_element;
171
172#if SIZEOF_ZEND_LONG == 8
173# ifdef _WIN32
174#  define THIS_HASHVAL 6385726429Ui64
175# else
176#  define THIS_HASHVAL 6385726429ULL
177# endif
178#else
179#define THIS_HASHVAL 2090759133UL
180#endif
181
182/* method flags (types) */
183#define ZEND_ACC_STATIC         0x01
184#define ZEND_ACC_ABSTRACT       0x02
185#define ZEND_ACC_FINAL          0x04
186#define ZEND_ACC_IMPLEMENTED_ABSTRACT       0x08
187
188/* class flags (types) */
189/* ZEND_ACC_IMPLICIT_ABSTRACT_CLASS is used for abstract classes (since it is set by any abstract method even interfaces MAY have it set, too). */
190/* ZEND_ACC_EXPLICIT_ABSTRACT_CLASS denotes that a class was explicitly defined as abstract by using the keyword. */
191#define ZEND_ACC_IMPLICIT_ABSTRACT_CLASS    0x10
192#define ZEND_ACC_EXPLICIT_ABSTRACT_CLASS    0x20
193#define ZEND_ACC_INTERFACE                  0x80
194#define ZEND_ACC_TRAIT                      0x120
195
196/* method flags (visibility) */
197/* The order of those must be kept - public < protected < private */
198#define ZEND_ACC_PUBLIC     0x100
199#define ZEND_ACC_PROTECTED  0x200
200#define ZEND_ACC_PRIVATE    0x400
201#define ZEND_ACC_PPP_MASK  (ZEND_ACC_PUBLIC | ZEND_ACC_PROTECTED | ZEND_ACC_PRIVATE)
202
203#define ZEND_ACC_CHANGED    0x800
204#define ZEND_ACC_IMPLICIT_PUBLIC    0x1000
205
206/* method flags (special method detection) */
207#define ZEND_ACC_CTOR       0x2000
208#define ZEND_ACC_DTOR       0x4000
209#define ZEND_ACC_CLONE      0x8000
210
211/* method flag (bc only), any method that has this flag can be used statically and non statically. */
212#define ZEND_ACC_ALLOW_STATIC   0x10000
213
214/* shadow of parent's private method/property */
215#define ZEND_ACC_SHADOW 0x20000
216
217/* deprecation flag */
218#define ZEND_ACC_DEPRECATED 0x40000
219
220/* class implement interface(s) flag */
221#define ZEND_ACC_IMPLEMENT_INTERFACES 0x80000
222#define ZEND_ACC_IMPLEMENT_TRAITS     0x400000
223
224/* class constants updated */
225#define ZEND_ACC_CONSTANTS_UPDATED    0x100000
226
227/* user class has methods with static variables */
228#define ZEND_HAS_STATIC_IN_METHODS    0x800000
229
230
231#define ZEND_ACC_CLOSURE              0x100000
232#define ZEND_ACC_GENERATOR            0x800000
233
234/* function flag for internal user call handlers __call, __callstatic */
235#define ZEND_ACC_CALL_VIA_HANDLER     0x200000
236
237/* disable inline caching */
238#define ZEND_ACC_NEVER_CACHE          0x400000
239
240#define ZEND_ACC_VARIADIC               0x1000000
241
242#define ZEND_ACC_RETURN_REFERENCE       0x4000000
243#define ZEND_ACC_DONE_PASS_TWO          0x8000000
244
245/* function has arguments with type hinting */
246#define ZEND_ACC_HAS_TYPE_HINTS         0x10000000
247
248/* op_array has finally blocks */
249#define ZEND_ACC_HAS_FINALLY_BLOCK      0x20000000
250
251/* internal function is allocated at arena */
252#define ZEND_ACC_ARENA_ALLOCATED        0x20000000
253
254/* Function has a return type hint (or class has such non-private function) */
255#define ZEND_ACC_HAS_RETURN_TYPE        0x40000000
256
257#define ZEND_CE_IS_TRAIT(ce) (((ce)->ce_flags & ZEND_ACC_TRAIT) == ZEND_ACC_TRAIT)
258
259char *zend_visibility_string(uint32_t fn_flags);
260
261typedef struct _zend_property_info {
262    uint32_t offset; /* property offset for object properties or
263                          property index for static properties */
264    uint32_t flags;
265    zend_string *name;
266    zend_string *doc_comment;
267    zend_class_entry *ce;
268} zend_property_info;
269
270#define OBJ_PROP(obj, offset) \
271    ((zval*)((char*)(obj) + offset))
272#define OBJ_PROP_NUM(obj, num) \
273    (&(obj)->properties_table[(num)])
274#define OBJ_PROP_TO_OFFSET(num) \
275    ((uint32_t)(zend_uintptr_t)OBJ_PROP_NUM(((zend_object*)NULL), num))
276#define OBJ_PROP_TO_NUM(offset) \
277    ((offset - OBJ_PROP_TO_OFFSET(0)) / sizeof(zval))
278
279/* arg_info for internal functions */
280typedef struct _zend_internal_arg_info {
281    const char *name;
282    const char *class_name;
283    zend_uchar type_hint;
284    zend_uchar pass_by_reference;
285    zend_bool allow_null;
286    zend_bool is_variadic;
287} zend_internal_arg_info;
288
289/* arg_info for user functions */
290typedef struct _zend_arg_info {
291    zend_string *name;
292    zend_string *class_name;
293    zend_uchar type_hint;
294    zend_uchar pass_by_reference;
295    zend_bool allow_null;
296    zend_bool is_variadic;
297} zend_arg_info;
298
299/* the following structure repeats the layout of zend_internal_arg_info,
300 * but its fields have different meaning. It's used as the first element of
301 * arg_info array to define properties of internal functions.
302 * It's also used for return type hinting.
303 */
304typedef struct _zend_internal_function_info {
305    zend_uintptr_t required_num_args;
306    const char *class_name;
307    zend_uchar type_hint;
308    zend_bool return_reference;
309    zend_bool allow_null;
310    zend_bool _is_variadic;
311} zend_internal_function_info;
312
313struct _zend_op_array {
314    /* Common elements */
315    zend_uchar type;
316    uint32_t fn_flags;
317    zend_string *function_name;
318    zend_class_entry *scope;
319    zend_function *prototype;
320    uint32_t num_args;
321    uint32_t required_num_args;
322    zend_arg_info *arg_info;
323    /* END of common elements */
324
325    uint32_t *refcount;
326
327    uint32_t this_var;
328
329    uint32_t last;
330    zend_op *opcodes;
331
332    int last_var;
333    uint32_t T;
334    zend_string **vars;
335
336    int last_brk_cont;
337    int last_try_catch;
338    zend_brk_cont_element *brk_cont_array;
339    zend_try_catch_element *try_catch_array;
340
341    /* static variables support */
342    HashTable *static_variables;
343
344    zend_string *filename;
345    uint32_t line_start;
346    uint32_t line_end;
347    zend_string *doc_comment;
348    uint32_t early_binding; /* the linked list of delayed declarations */
349
350    int last_literal;
351    zval *literals;
352
353    int  last_cache_slot;
354    void **run_time_cache;
355
356    void *reserved[ZEND_MAX_RESERVED_RESOURCES];
357};
358
359
360#define ZEND_RETURN_VALUE               0
361#define ZEND_RETURN_REFERENCE           1
362
363typedef struct _zend_internal_function {
364    /* Common elements */
365    zend_uchar type;
366    uint32_t fn_flags;
367    zend_string* function_name;
368    zend_class_entry *scope;
369    zend_function *prototype;
370    uint32_t num_args;
371    uint32_t required_num_args;
372    zend_internal_arg_info *arg_info;
373    /* END of common elements */
374
375    void (*handler)(INTERNAL_FUNCTION_PARAMETERS);
376    struct _zend_module_entry *module;
377} zend_internal_function;
378
379#define ZEND_FN_SCOPE_NAME(function)  ((function) && (function)->common.scope ? (function)->common.scope->name->val : "")
380
381union _zend_function {
382    zend_uchar type;    /* MUST be the first element of this struct! */
383
384    struct {
385        zend_uchar type;  /* never used */
386        uint32_t fn_flags;
387        zend_string *function_name;
388        zend_class_entry *scope;
389        union _zend_function *prototype;
390        uint32_t num_args;
391        uint32_t required_num_args;
392        zend_arg_info *arg_info;
393    } common;
394
395    zend_op_array op_array;
396    zend_internal_function internal_function;
397};
398
399typedef enum _zend_call_kind {
400    ZEND_CALL_NESTED_FUNCTION,  /* stackless VM call to function */
401    ZEND_CALL_NESTED_CODE,      /* stackless VM call to include/require/eval */
402    ZEND_CALL_TOP_FUNCTION,     /* direct VM call to function from external C code */
403    ZEND_CALL_TOP_CODE          /* direct VM call to "main" code from external C code */
404} zend_call_kind;
405
406struct _zend_execute_data {
407    const zend_op       *opline;           /* executed opline                */
408    zend_execute_data   *call;             /* current call                   */
409    zval                *return_value;
410    zend_function       *func;             /* executed op_array              */
411    zval                 This;
412#if ZEND_EX_USE_RUN_TIME_CACHE
413    void               **run_time_cache;
414#endif
415#if ZEND_EX_USE_LITERALS
416    zval                *literals;
417#endif
418    zend_class_entry    *called_scope;
419    zend_execute_data   *prev_execute_data;
420    zend_array          *symbol_table;
421};
422
423#define ZEND_CALL_FUNCTION           (0 << 0)
424#define ZEND_CALL_CODE               (1 << 0)
425#define ZEND_CALL_NESTED             (0 << 1)
426#define ZEND_CALL_TOP                (1 << 1)
427#define ZEND_CALL_FREE_EXTRA_ARGS    (1 << 2) /* equal to IS_TYPE_REFCOUNTED */
428#define ZEND_CALL_CTOR               (1 << 3)
429#define ZEND_CALL_CTOR_RESULT_UNUSED (1 << 4)
430
431#define ZEND_CALL_INFO(call) \
432    (Z_TYPE_INFO((call)->This) >> 24)
433
434#define ZEND_CALL_KIND(call) \
435    (ZEND_CALL_INFO(call) & (ZEND_CALL_CODE | ZEND_CALL_TOP))
436
437#define ZEND_SET_CALL_INFO(call, info) do { \
438        Z_TYPE_INFO((call)->This) = IS_OBJECT_EX | ((info) << 24); \
439    } while (0)
440
441#define ZEND_ADD_CALL_FLAG(call, info) do { \
442        Z_TYPE_INFO((call)->This) |= ((info) << 24); \
443    } while (0)
444
445#define ZEND_CALL_NUM_ARGS(call) \
446    (call)->This.u2.num_args
447
448#define ZEND_CALL_FRAME_SLOT \
449    ((int)((ZEND_MM_ALIGNED_SIZE(sizeof(zend_execute_data)) + ZEND_MM_ALIGNED_SIZE(sizeof(zval)) - 1) / ZEND_MM_ALIGNED_SIZE(sizeof(zval))))
450
451#define ZEND_CALL_VAR(call, n) \
452    ((zval*)(((char*)(call)) + ((int)(n))))
453
454#define ZEND_CALL_VAR_NUM(call, n) \
455    (((zval*)(call)) + (ZEND_CALL_FRAME_SLOT + ((int)(n))))
456
457#define ZEND_CALL_ARG(call, n) \
458    ZEND_CALL_VAR_NUM(call, ((int)(n)) - 1)
459
460#define EX(element)             ((execute_data)->element)
461
462#define EX_CALL_INFO()          ZEND_CALL_INFO(execute_data)
463#define EX_CALL_KIND()          ZEND_CALL_KIND(execute_data)
464#define EX_NUM_ARGS()           ZEND_CALL_NUM_ARGS(execute_data)
465
466#define EX_VAR(n)               ZEND_CALL_VAR(execute_data, n)
467#define EX_VAR_NUM(n)           ZEND_CALL_VAR_NUM(execute_data, n)
468
469#define EX_VAR_TO_NUM(n)        (ZEND_CALL_VAR(NULL, n) - ZEND_CALL_VAR_NUM(NULL, 0))
470
471#define ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline_num) \
472    ((char*)&(op_array)->opcodes[opline_num] - (char*)(opline))
473
474#define ZEND_OFFSET_TO_OPLINE(base, offset) \
475    ((zend_op*)(((char*)(base)) + (int)offset))
476
477#define ZEND_OFFSET_TO_OPLINE_NUM(op_array, base, offset) \
478    (ZEND_OFFSET_TO_OPLINE(base, offset) - op_array->opcodes)
479
480#if ZEND_USE_ABS_JMP_ADDR
481
482/* run-time jump target */
483# define OP_JMP_ADDR(opline, node) \
484    (node).jmp_addr
485
486/* convert jump target from compile-time to run-time */
487# define ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, node) do { \
488        (node).jmp_addr = (op_array)->opcodes + (node).opline_num; \
489    } while (0)
490
491/* convert jump target back from run-time to compile-time */
492# define ZEND_PASS_TWO_UNDO_JMP_TARGET(op_array, opline, node) do { \
493        (node).opline_num = (node).jmp_addr - (op_array)->opcodes; \
494    } while (0)
495
496#else
497
498/* run-time jump target */
499# define OP_JMP_ADDR(opline, node) \
500    ZEND_OFFSET_TO_OPLINE(opline, (node).jmp_offset)
501
502/* convert jump target from compile-time to run-time */
503# define ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, node) do { \
504        (node).jmp_offset = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, (node).opline_num); \
505    } while (0)
506
507/* convert jump target back from run-time to compile-time */
508# define ZEND_PASS_TWO_UNDO_JMP_TARGET(op_array, opline, node) do { \
509        (node).opline_num = ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, (node).jmp_offset); \
510    } while (0)
511
512#endif
513
514/* constant-time constant */
515# define CT_CONSTANT_EX(op_array, num) \
516    ((op_array)->literals + (num))
517
518# define CT_CONSTANT(node) \
519    CT_CONSTANT_EX(CG(active_op_array), (node).constant)
520
521#if ZEND_USE_ABS_CONST_ADDR
522
523/* run-time constant */
524# define RT_CONSTANT_EX(base, node) \
525    (node).zv
526
527/* convert constant from compile-time to run-time */
528# define ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, node) do { \
529        (node).zv = CT_CONSTANT_EX(op_array, (node).constant); \
530    } while (0)
531
532/* convert constant back from run-time to compile-time */
533# define ZEND_PASS_TWO_UNDO_CONSTANT(op_array, node) do { \
534        (node).constant = (node).zv - (op_array)->literals; \
535    } while (0)
536
537#else
538
539/* run-time constant */
540# define RT_CONSTANT_EX(base, node) \
541    ((zval*)(((char*)(base)) + (node).constant))
542
543/* convert constant from compile-time to run-time */
544# define ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, node) do { \
545        (node).constant *= sizeof(zval); \
546    } while (0)
547
548/* convert constant back from run-time to compile-time (do nothing) */
549# define ZEND_PASS_TWO_UNDO_CONSTANT(op_array, node) do { \
550        (node).constant /= sizeof(zval); \
551    } while (0)
552
553#endif
554
555#if ZEND_EX_USE_LITERALS
556
557# define EX_LITERALS() \
558    EX(literals)
559
560# define EX_LOAD_LITERALS(op_array) do { \
561        EX(literals) = (op_array)->literals; \
562    } while (0)
563
564#else
565
566# define EX_LITERALS() \
567    EX(func)->op_array.literals
568
569# define EX_LOAD_LITERALS(op_array) do { \
570    } while (0)
571
572#endif
573
574/* run-time constant */
575#define RT_CONSTANT(op_array, node) \
576    RT_CONSTANT_EX((op_array)->literals, node)
577
578/* constant in currently executed function */
579#define EX_CONSTANT(node) \
580    RT_CONSTANT_EX(EX_LITERALS(), node)
581
582#if ZEND_EX_USE_RUN_TIME_CACHE
583
584# define EX_RUN_TIME_CACHE() \
585    EX(run_time_cache)
586
587# define EX_LOAD_RUN_TIME_CACHE(op_array) do { \
588        EX(run_time_cache) = (op_array)->run_time_cache; \
589    } while (0)
590
591#else
592
593# define EX_RUN_TIME_CACHE() \
594    EX(func)->op_array.run_time_cache
595
596# define EX_LOAD_RUN_TIME_CACHE(op_array) do { \
597    } while (0)
598
599#endif
600
601#define IS_CONST    (1<<0)
602#define IS_TMP_VAR  (1<<1)
603#define IS_VAR      (1<<2)
604#define IS_UNUSED   (1<<3)  /* Unused variable */
605#define IS_CV       (1<<4)  /* Compiled variable */
606
607#define EXT_TYPE_UNUSED (1<<5)
608
609#include "zend_globals.h"
610
611BEGIN_EXTERN_C()
612
613void init_compiler(void);
614void shutdown_compiler(void);
615void zend_init_compiler_data_structures(void);
616void zend_init_compiler_context(void);
617
618extern ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
619extern ZEND_API zend_op_array *(*zend_compile_string)(zval *source_string, char *filename);
620
621ZEND_API int lex_scan(zval *zendlval);
622void startup_scanner(void);
623void shutdown_scanner(void);
624
625ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename);
626ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename);
627ZEND_API zend_string *zend_get_compiled_filename(void);
628ZEND_API int zend_get_compiled_lineno(void);
629ZEND_API size_t zend_get_scanned_file_offset(void);
630
631ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var);
632
633#ifdef ZTS
634const char *zend_get_zendtext(void);
635int zend_get_zendleng(void);
636#endif
637
638
639typedef int (*unary_op_type)(zval *, zval *);
640typedef int (*binary_op_type)(zval *, zval *, zval *);
641ZEND_API unary_op_type get_unary_op(int opcode);
642ZEND_API binary_op_type get_binary_op(int opcode);
643
644void zend_stop_lexing(void);
645void zend_emit_final_return(zval *zv);
646zend_ast *zend_ast_append_str(zend_ast *left, zend_ast *right);
647uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag);
648uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag);
649zend_ast *zend_ast_append_doc_comment(zend_ast *list);
650void zend_handle_encoding_declaration(zend_ast *ast);
651
652/* parser-driven code generators */
653void zend_do_free(znode *op1);
654
655ZEND_API int do_bind_function(const zend_op_array *op_array, const zend_op *opline, HashTable *function_table, zend_bool compile_time);
656ZEND_API zend_class_entry *do_bind_class(const zend_op_array *op_array, const zend_op *opline, HashTable *class_table, zend_bool compile_time);
657ZEND_API zend_class_entry *do_bind_inherited_class(const zend_op_array *op_array, const zend_op *opline, HashTable *class_table, zend_class_entry *parent_ce, zend_bool compile_time);
658ZEND_API void zend_do_delayed_early_binding(const zend_op_array *op_array);
659
660/* Functions for a null terminated pointer list, used for traits parsing and compilation */
661void zend_init_list(void *result, void *item);
662void zend_add_to_list(void *result, void *item);
663
664void zend_do_extended_info(void);
665void zend_do_extended_fcall_begin(void);
666void zend_do_extended_fcall_end(void);
667
668void zend_verify_namespace(void);
669void zend_do_end_compilation(void);
670
671void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline, int pass2);
672void zend_release_labels(int temporary);
673
674ZEND_API void function_add_ref(zend_function *function);
675
676#define INITIAL_OP_ARRAY_SIZE 64
677
678
679/* helper functions in zend_language_scanner.l */
680ZEND_API zend_op_array *compile_file(zend_file_handle *file_handle, int type);
681ZEND_API zend_op_array *compile_string(zval *source_string, char *filename);
682ZEND_API zend_op_array *compile_filename(int type, zval *filename);
683ZEND_API int zend_execute_scripts(int type, zval *retval, int file_count, ...);
684ZEND_API int open_file_for_scanning(zend_file_handle *file_handle);
685ZEND_API void init_op_array(zend_op_array *op_array, zend_uchar type, int initial_ops_size);
686ZEND_API void destroy_op_array(zend_op_array *op_array);
687ZEND_API void zend_destroy_file_handle(zend_file_handle *file_handle);
688ZEND_API void zend_cleanup_user_class_data(zend_class_entry *ce);
689ZEND_API void zend_cleanup_internal_class_data(zend_class_entry *ce);
690ZEND_API void zend_cleanup_internal_classes(void);
691ZEND_API void zend_cleanup_op_array_data(zend_op_array *op_array);
692ZEND_API int clean_non_persistent_function_full(zval *zv);
693ZEND_API int clean_non_persistent_class_full(zval *zv);
694
695ZEND_API void destroy_zend_function(zend_function *function);
696ZEND_API void zend_function_dtor(zval *zv);
697ZEND_API void destroy_zend_class(zval *zv);
698void zend_class_add_ref(zval *zv);
699
700ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, int internal);
701#define zend_unmangle_property_name(mangled_property, class_name, prop_name) \
702        zend_unmangle_property_name_ex(mangled_property, class_name, prop_name, NULL)
703ZEND_API int zend_unmangle_property_name_ex(const zend_string *name, const char **class_name, const char **prop_name, size_t *prop_len);
704
705#define ZEND_FUNCTION_DTOR zend_function_dtor
706#define ZEND_CLASS_DTOR destroy_zend_class
707
708zend_op *get_next_op(zend_op_array *op_array);
709void init_op(zend_op *op);
710int get_next_op_number(zend_op_array *op_array);
711int print_class(zend_class_entry *class_entry);
712void print_op_array(zend_op_array *op_array, int optimizations);
713ZEND_API int pass_two(zend_op_array *op_array);
714zend_brk_cont_element *get_next_brk_cont_element(zend_op_array *op_array);
715ZEND_API zend_bool zend_is_compiling(void);
716ZEND_API char *zend_make_compiled_string_description(const char *name);
717ZEND_API void zend_initialize_class_data(zend_class_entry *ce, zend_bool nullify_handlers);
718uint32_t zend_get_class_fetch_type(zend_string *name);
719
720typedef zend_bool (*zend_auto_global_callback)(zend_string *name);
721typedef struct _zend_auto_global {
722    zend_string *name;
723    zend_auto_global_callback auto_global_callback;
724    zend_bool jit;
725    zend_bool armed;
726} zend_auto_global;
727
728ZEND_API int zend_register_auto_global(zend_string *name, zend_bool jit, zend_auto_global_callback auto_global_callback);
729ZEND_API void zend_activate_auto_globals(void);
730ZEND_API zend_bool zend_is_auto_global(zend_string *name);
731ZEND_API size_t zend_dirname(char *path, size_t len);
732
733int zendlex(zend_parser_stack_elem *elem);
734
735int zend_add_literal(zend_op_array *op_array, zval *zv);
736
737/* BEGIN: OPCODES */
738
739#include "zend_vm_opcodes.h"
740
741#define ZEND_OP_DATA                137
742
743/* END: OPCODES */
744
745/* class fetches */
746#define ZEND_FETCH_CLASS_DEFAULT    0
747#define ZEND_FETCH_CLASS_SELF       1
748#define ZEND_FETCH_CLASS_PARENT     2
749#define ZEND_FETCH_CLASS_STATIC     3
750#define ZEND_FETCH_CLASS_AUTO       4
751#define ZEND_FETCH_CLASS_INTERFACE  5
752#define ZEND_FETCH_CLASS_TRAIT      6
753#define ZEND_FETCH_CLASS_MASK        0x0f
754#define ZEND_FETCH_CLASS_NO_AUTOLOAD 0x80
755#define ZEND_FETCH_CLASS_SILENT      0x0100
756
757/* variable parsing type (compile-time) */
758#define ZEND_PARSED_MEMBER              (1<<0)
759#define ZEND_PARSED_METHOD_CALL         (1<<1)
760#define ZEND_PARSED_STATIC_MEMBER       (1<<2)
761#define ZEND_PARSED_FUNCTION_CALL       (1<<3)
762#define ZEND_PARSED_VARIABLE            (1<<4)
763#define ZEND_PARSED_REFERENCE_VARIABLE  (1<<5)
764#define ZEND_PARSED_NEW                 (1<<6)
765#define ZEND_PARSED_LIST_EXPR           (1<<7)
766
767#define ZEND_PARAM_REF      (1<<0)
768#define ZEND_PARAM_VARIADIC (1<<1)
769
770#define ZEND_NAME_FQ       0
771#define ZEND_NAME_NOT_FQ   1
772#define ZEND_NAME_RELATIVE 2
773
774/* unset types */
775#define ZEND_UNSET_REG 0
776
777/* var status for backpatching */
778#define BP_VAR_R            0
779#define BP_VAR_W            1
780#define BP_VAR_RW           2
781#define BP_VAR_IS           3
782#define BP_VAR_FUNC_ARG     4
783#define BP_VAR_UNSET        5
784#define BP_VAR_REF          6   /* right-hand side of by-ref assignment */
785
786/* Bottom 3 bits are the type, top bits are arg num for BP_VAR_FUNC_ARG */
787#define BP_VAR_SHIFT 3
788#define BP_VAR_MASK  7
789
790
791#define ZEND_INTERNAL_FUNCTION              1
792#define ZEND_USER_FUNCTION                  2
793#define ZEND_OVERLOADED_FUNCTION            3
794#define ZEND_EVAL_CODE                      4
795#define ZEND_OVERLOADED_FUNCTION_TEMPORARY  5
796
797/* A quick check (type == ZEND_USER_FUNCTION || type == ZEND_EVAL_CODE) */
798#define ZEND_USER_CODE(type) ((type & 1) == 0)
799
800#define ZEND_INTERNAL_CLASS         1
801#define ZEND_USER_CLASS             2
802
803#define ZEND_EVAL               (1<<0)
804#define ZEND_INCLUDE            (1<<1)
805#define ZEND_INCLUDE_ONCE       (1<<2)
806#define ZEND_REQUIRE            (1<<3)
807#define ZEND_REQUIRE_ONCE       (1<<4)
808
809#define ZEND_CT (1<<0)
810#define ZEND_RT (1<<1)
811
812/* global/local fetches */
813#define ZEND_FETCH_GLOBAL           0x00000000
814#define ZEND_FETCH_LOCAL            0x10000000
815#define ZEND_FETCH_STATIC           0x20000000
816#define ZEND_FETCH_STATIC_MEMBER    0x30000000
817#define ZEND_FETCH_GLOBAL_LOCK      0x40000000
818#define ZEND_FETCH_LEXICAL          0x50000000
819
820#define ZEND_FETCH_TYPE_MASK        0x70000000
821
822#define ZEND_FETCH_STANDARD         0x00000000
823
824#define ZEND_ISSET                  0x02000000
825#define ZEND_ISEMPTY                0x01000000
826#define ZEND_ISSET_ISEMPTY_MASK     (ZEND_ISSET | ZEND_ISEMPTY)
827#define ZEND_QUICK_SET              0x00800000
828
829#define ZEND_FETCH_ARG_MASK         0x000fffff
830
831#define ZEND_FE_FETCH_BYREF 1
832#define ZEND_FE_FETCH_WITH_KEY  2
833
834#define EXT_TYPE_FREE_ON_RETURN     (1<<2)
835
836#define ZEND_MEMBER_FUNC_CALL   1<<0
837
838#define ZEND_ARG_SEND_BY_REF (1<<0)
839#define ZEND_ARG_COMPILE_TIME_BOUND (1<<1)
840#define ZEND_ARG_SEND_FUNCTION (1<<2)
841#define ZEND_ARG_SEND_SILENT   (1<<3)
842
843#define ZEND_SEND_BY_VAL     0
844#define ZEND_SEND_BY_REF     1
845#define ZEND_SEND_PREFER_REF 2
846
847static zend_always_inline int zend_check_arg_send_type(const zend_function *zf, uint32_t arg_num, uint32_t mask)
848{
849    arg_num--;
850    if (UNEXPECTED(arg_num >= zf->common.num_args)) {
851        if (EXPECTED((zf->common.fn_flags & ZEND_ACC_VARIADIC) == 0)) {
852            return 0;
853        }
854        arg_num = zf->common.num_args;
855    }
856    return UNEXPECTED((zf->common.arg_info[arg_num].pass_by_reference & mask) != 0);
857}
858
859#define ARG_MUST_BE_SENT_BY_REF(zf, arg_num) \
860    zend_check_arg_send_type(zf, arg_num, ZEND_SEND_BY_REF)
861
862#define ARG_SHOULD_BE_SENT_BY_REF(zf, arg_num) \
863    zend_check_arg_send_type(zf, arg_num, ZEND_SEND_BY_REF|ZEND_SEND_PREFER_REF)
864
865#define ARG_MAY_BE_SENT_BY_REF(zf, arg_num) \
866    zend_check_arg_send_type(zf, arg_num, ZEND_SEND_PREFER_REF)
867
868#define ZEND_RETURN_VAL 0
869#define ZEND_RETURN_REF 1
870
871
872#define ZEND_RETURNS_FUNCTION 1<<0
873#define ZEND_RETURNS_VALUE    1<<1
874
875#define ZEND_FAST_RET_TO_CATCH      1
876#define ZEND_FAST_RET_TO_FINALLY    2
877
878#define ZEND_FAST_CALL_FROM_CATCH   1
879#define ZEND_FAST_CALL_FROM_FINALLY 2
880
881#define ZEND_ARRAY_ELEMENT_REF      (1<<0)
882#define ZEND_ARRAY_NOT_PACKED       (1<<1)
883#define ZEND_ARRAY_SIZE_SHIFT       2
884
885END_EXTERN_C()
886
887#define ZEND_CLONE_FUNC_NAME        "__clone"
888#define ZEND_CONSTRUCTOR_FUNC_NAME  "__construct"
889#define ZEND_DESTRUCTOR_FUNC_NAME   "__destruct"
890#define ZEND_GET_FUNC_NAME          "__get"
891#define ZEND_SET_FUNC_NAME          "__set"
892#define ZEND_UNSET_FUNC_NAME        "__unset"
893#define ZEND_ISSET_FUNC_NAME        "__isset"
894#define ZEND_CALL_FUNC_NAME         "__call"
895#define ZEND_CALLSTATIC_FUNC_NAME   "__callstatic"
896#define ZEND_TOSTRING_FUNC_NAME     "__tostring"
897#define ZEND_AUTOLOAD_FUNC_NAME     "__autoload"
898#define ZEND_INVOKE_FUNC_NAME       "__invoke"
899#define ZEND_DEBUGINFO_FUNC_NAME    "__debuginfo"
900
901/* The following constants may be combined in CG(compiler_options)
902 * to change the default compiler behavior */
903
904/* generate extended debug information */
905#define ZEND_COMPILE_EXTENDED_INFO              (1<<0)
906
907/* call op_array handler of extendions */
908#define ZEND_COMPILE_HANDLE_OP_ARRAY            (1<<1)
909
910/* generate ZEND_INIT_FCALL_BY_NAME for internal functions instead of ZEND_INIT_FCALL */
911#define ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS  (1<<2)
912
913/* don't perform early binding for classes inherited form internal ones;
914 * in namespaces assume that internal class that doesn't exist at compile-time
915 * may apper in run-time */
916#define ZEND_COMPILE_IGNORE_INTERNAL_CLASSES    (1<<3)
917
918/* generate ZEND_DECLARE_INHERITED_CLASS_DELAYED opcode to delay early binding */
919#define ZEND_COMPILE_DELAYED_BINDING            (1<<4)
920
921/* disable constant substitution at compile-time */
922#define ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION   (1<<5)
923
924/* disable usage of builtin instruction for strlen() */
925#define ZEND_COMPILE_NO_BUILTIN_STRLEN          (1<<6)
926
927/* The default value for CG(compiler_options) */
928#define ZEND_COMPILE_DEFAULT                    ZEND_COMPILE_HANDLE_OP_ARRAY
929
930/* The default value for CG(compiler_options) during eval() */
931#define ZEND_COMPILE_DEFAULT_FOR_EVAL           0
932
933#endif /* ZEND_COMPILE_H */
934
935/*
936 * Local variables:
937 * tab-width: 4
938 * c-basic-offset: 4
939 * indent-tabs-mode: t
940 * End:
941 */
942