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