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