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