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