xref: /php-src/Zend/zend_compile.h (revision 600d591c)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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@php.net>                                 |
16    |          Zeev Suraski <zeev@php.net>                                 |
17    +----------------------------------------------------------------------+
18 */
19 
20 #ifndef ZEND_COMPILE_H
21 #define ZEND_COMPILE_H
22 
23 #include "zend_ast.h"
24 #include "zend_types.h"
25 #include "zend_map_ptr.h"
26 #include "zend_alloc.h"
27 
28 #include <stdarg.h>
29 #include <stdint.h>
30 
31 #include "zend_llist.h"
32 #include "zend_frameless_function.h"
33 
34 #define SET_UNUSED(op) do { \
35 	op ## _type = IS_UNUSED; \
36 	op.num = (uint32_t) -1; \
37 } while (0)
38 
39 #define MAKE_NOP(opline) do { \
40 	(opline)->opcode = ZEND_NOP; \
41 	SET_UNUSED((opline)->op1); \
42 	SET_UNUSED((opline)->op2); \
43 	SET_UNUSED((opline)->result); \
44 } while (0)
45 
46 #define RESET_DOC_COMMENT() do { \
47 	if (CG(doc_comment)) { \
48 		zend_string_release_ex(CG(doc_comment), 0); \
49 		CG(doc_comment) = NULL; \
50 	} \
51 } while (0)
52 
53 typedef struct _zend_op_array zend_op_array;
54 typedef 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 relative 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 #else
63 # define ZEND_USE_ABS_JMP_ADDR      0
64 # define ZEND_USE_ABS_CONST_ADDR    0
65 #endif
66 
67 typedef union _znode_op {
68 	uint32_t      constant;
69 	uint32_t      var;
70 	uint32_t      num;
71 	uint32_t      opline_num; /*  Needs to be signed */
72 #if ZEND_USE_ABS_JMP_ADDR
73 	zend_op       *jmp_addr;
74 #else
75 	uint32_t      jmp_offset;
76 #endif
77 #if ZEND_USE_ABS_CONST_ADDR
78 	zval          *zv;
79 #endif
80 } znode_op;
81 
82 typedef struct _znode { /* used only during compilation */
83 	uint8_t op_type;
84 	uint8_t flag;
85 	union {
86 		znode_op op;
87 		zval constant; /* replaced by literal/zv */
88 	} u;
89 } znode;
90 
91 typedef struct _zend_ast_znode {
92 	zend_ast_kind kind;
93 	zend_ast_attr attr;
94 	uint32_t lineno;
95 	znode node;
96 } zend_ast_znode;
97 
98 ZEND_API zend_ast * ZEND_FASTCALL zend_ast_create_znode(znode *node);
99 
zend_ast_get_znode(zend_ast * ast)100 static zend_always_inline znode *zend_ast_get_znode(zend_ast *ast) {
101 	return &((zend_ast_znode *) ast)->node;
102 }
103 
104 typedef struct _zend_declarables {
105 	zend_long ticks;
106 } zend_declarables;
107 
108 /* Compilation context that is different for each file, but shared between op arrays. */
109 typedef struct _zend_file_context {
110 	zend_declarables declarables;
111 
112 	zend_string *current_namespace;
113 	bool in_namespace;
114 	bool has_bracketed_namespaces;
115 
116 	HashTable *imports;
117 	HashTable *imports_function;
118 	HashTable *imports_const;
119 
120 	HashTable seen_symbols;
121 } zend_file_context;
122 
123 typedef union _zend_parser_stack_elem {
124 	zend_ast *ast;
125 	zend_string *str;
126 	zend_ulong num;
127 	unsigned char *ptr;
128 	unsigned char *ident;
129 } zend_parser_stack_elem;
130 
131 void zend_compile_top_stmt(zend_ast *ast);
132 void zend_const_expr_to_zval(zval *result, zend_ast **ast_ptr, bool allow_dynamic);
133 
134 typedef int (*user_opcode_handler_t) (zend_execute_data *execute_data);
135 
136 struct _zend_op {
137 	const void *handler;
138 	znode_op op1;
139 	znode_op op2;
140 	znode_op result;
141 	uint32_t extended_value;
142 	uint32_t lineno;
143 	uint8_t opcode;       /* Opcodes defined in Zend/zend_vm_opcodes.h */
144 	uint8_t op1_type;     /* IS_UNUSED, IS_CONST, IS_TMP_VAR, IS_VAR, IS_CV */
145 	uint8_t op2_type;     /* IS_UNUSED, IS_CONST, IS_TMP_VAR, IS_VAR, IS_CV */
146 	uint8_t result_type;  /* IS_UNUSED, IS_CONST, IS_TMP_VAR, IS_VAR, IS_CV */
147 #ifdef ZEND_VERIFY_TYPE_INFERENCE
148 	uint32_t op1_use_type;
149 	uint32_t op2_use_type;
150 	uint32_t result_use_type;
151 	uint32_t op1_def_type;
152 	uint32_t op2_def_type;
153 	uint32_t result_def_type;
154 #endif
155 };
156 
157 
158 typedef struct _zend_brk_cont_element {
159 	int start;
160 	int cont;
161 	int brk;
162 	int parent;
163 	bool is_switch;
164 } zend_brk_cont_element;
165 
166 typedef struct _zend_label {
167 	int brk_cont;
168 	uint32_t opline_num;
169 } zend_label;
170 
171 typedef struct _zend_try_catch_element {
172 	uint32_t try_op;
173 	uint32_t catch_op;  /* ketchup! */
174 	uint32_t finally_op;
175 	uint32_t finally_end;
176 } zend_try_catch_element;
177 
178 #define ZEND_LIVE_TMPVAR  0
179 #define ZEND_LIVE_LOOP    1
180 #define ZEND_LIVE_SILENCE 2
181 #define ZEND_LIVE_ROPE    3
182 #define ZEND_LIVE_NEW     4
183 #define ZEND_LIVE_MASK    7
184 
185 typedef struct _zend_live_range {
186 	uint32_t var; /* low bits are used for variable type (ZEND_LIVE_* macros) */
187 	uint32_t start;
188 	uint32_t end;
189 } zend_live_range;
190 
191 /* Compilation context that is different for each op array. */
192 typedef struct _zend_oparray_context {
193 	struct _zend_oparray_context *prev;
194 	zend_op_array *op_array;
195 	uint32_t   opcodes_size;
196 	int        vars_size;
197 	int        literals_size;
198 	uint32_t   fast_call_var;
199 	uint32_t   try_catch_offset;
200 	int        current_brk_cont;
201 	int        last_brk_cont;
202 	zend_brk_cont_element *brk_cont_array;
203 	HashTable *labels;
204 	bool       in_jmp_frameless_branch;
205 } zend_oparray_context;
206 
207 /* Class, property and method flags                  class|meth.|prop.|const*/
208 /*                                                        |     |     |     */
209 /* Common flags                                           |     |     |     */
210 /* ============                                           |     |     |     */
211 /*                                                        |     |     |     */
212 /* Visibility flags (public < protected < private)        |     |     |     */
213 #define ZEND_ACC_PUBLIC                  (1 <<  0) /*     |  X  |  X  |  X  */
214 #define ZEND_ACC_PROTECTED               (1 <<  1) /*     |  X  |  X  |  X  */
215 #define ZEND_ACC_PRIVATE                 (1 <<  2) /*     |  X  |  X  |  X  */
216 /*                                                        |     |     |     */
217 /* Property or method overrides private one               |     |     |     */
218 #define ZEND_ACC_CHANGED                 (1 <<  3) /*     |  X  |  X  |     */
219 /*                                                        |     |     |     */
220 /* Static method or property                              |     |     |     */
221 #define ZEND_ACC_STATIC                  (1 <<  4) /*     |  X  |  X  |     */
222 /*                                                        |     |     |     */
223 /* Promoted property / parameter                          |     |     |     */
224 #define ZEND_ACC_PROMOTED                (1 <<  5) /*     |     |  X  |  X  */
225 /*                                                        |     |     |     */
226 /* Final class or method                                  |     |     |     */
227 #define ZEND_ACC_FINAL                   (1 <<  5) /*  X  |  X  |     |     */
228 /*                                                        |     |     |     */
229 /* Abstract method                                        |     |     |     */
230 #define ZEND_ACC_ABSTRACT                (1 <<  6) /*  X  |  X  |     |     */
231 #define ZEND_ACC_EXPLICIT_ABSTRACT_CLASS (1 <<  6) /*  X  |     |     |     */
232 /*                                                        |     |     |     */
233 /* Readonly property                                      |     |     |     */
234 #define ZEND_ACC_READONLY                (1 <<  7) /*     |     |  X  |     */
235 /*                                                        |     |     |     */
236 /* Immutable op_array and class_entries                   |     |     |     */
237 /* (implemented only for lazy loading of op_arrays)       |     |     |     */
238 #define ZEND_ACC_IMMUTABLE               (1 <<  7) /*  X  |  X  |     |     */
239 /*                                                        |     |     |     */
240 /* Function has typed arguments / class has typed props   |     |     |     */
241 #define ZEND_ACC_HAS_TYPE_HINTS          (1 <<  8) /*  X  |  X  |     |     */
242 /*                                                        |     |     |     */
243 /* Top-level class or function declaration                |     |     |     */
244 #define ZEND_ACC_TOP_LEVEL               (1 <<  9) /*  X  |  X  |     |     */
245 /*                                                        |     |     |     */
246 /* op_array or class is preloaded                         |     |     |     */
247 #define ZEND_ACC_PRELOADED               (1 << 10) /*  X  |  X  |     |     */
248 /*                                                        |     |     |     */
249 /* Flag to differentiate cases from constants.            |     |     |     */
250 /* Must not conflict with ZEND_ACC_ visibility flags      |     |     |     */
251 /* or IS_CONSTANT_VISITED_MARK                            |     |     |     */
252 #define ZEND_CLASS_CONST_IS_CASE         (1 << 6)  /*     |     |     |  X  */
253 /*                                                        |     |     |     */
254 /* Class Flags (unused: 30,31)                            |     |     |     */
255 /* ===========                                            |     |     |     */
256 /*                                                        |     |     |     */
257 /* Special class types                                    |     |     |     */
258 #define ZEND_ACC_INTERFACE               (1 <<  0) /*  X  |     |     |     */
259 #define ZEND_ACC_TRAIT                   (1 <<  1) /*  X  |     |     |     */
260 #define ZEND_ACC_ANON_CLASS              (1 <<  2) /*  X  |     |     |     */
261 #define ZEND_ACC_ENUM                    (1 << 28) /*  X  |     |     |     */
262 /*                                                        |     |     |     */
263 /* Class linked with parent, interfaces and traits        |     |     |     */
264 #define ZEND_ACC_LINKED                  (1 <<  3) /*  X  |     |     |     */
265 /*                                                        |     |     |     */
266 /* Class is abstract, since it is set by any              |     |     |     */
267 /* abstract method                                        |     |     |     */
268 #define ZEND_ACC_IMPLICIT_ABSTRACT_CLASS (1 <<  4) /*  X  |     |     |     */
269 /*                                                        |     |     |     */
270 /* Class has magic methods __get/__set/__unset/           |     |     |     */
271 /* __isset that use guards                                |     |     |     */
272 #define ZEND_ACC_USE_GUARDS              (1 << 11) /*  X  |     |     |     */
273 /*                                                        |     |     |     */
274 /* Class constants updated                                |     |     |     */
275 #define ZEND_ACC_CONSTANTS_UPDATED       (1 << 12) /*  X  |     |     |     */
276 /*                                                        |     |     |     */
277 /* Objects of this class may not have dynamic properties  |     |     |     */
278 #define ZEND_ACC_NO_DYNAMIC_PROPERTIES   (1 << 13) /*  X  |     |     |     */
279 /*                                                        |     |     |     */
280 /* User class has methods with static variables           |     |     |     */
281 #define ZEND_HAS_STATIC_IN_METHODS       (1 << 14) /*  X  |     |     |     */
282 /*                                                        |     |     |     */
283 /* Objects of this class may have dynamic properties      |     |     |     */
284 /* without triggering a deprecation warning               |     |     |     */
285 #define ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES (1 << 15) /* X  |     |     |     */
286 /*                                                        |     |     |     */
287 /* Readonly class                                         |     |     |     */
288 #define ZEND_ACC_READONLY_CLASS          (1 << 16) /*  X  |     |     |     */
289 /*                                                        |     |     |     */
290 /* Parent class is resolved (CE).                         |     |     |     */
291 #define ZEND_ACC_RESOLVED_PARENT         (1 << 17) /*  X  |     |     |     */
292 /*                                                        |     |     |     */
293 /* Interfaces are resolved (CEs).                         |     |     |     */
294 #define ZEND_ACC_RESOLVED_INTERFACES     (1 << 18) /*  X  |     |     |     */
295 /*                                                        |     |     |     */
296 /* Class has unresolved variance obligations.             |     |     |     */
297 #define ZEND_ACC_UNRESOLVED_VARIANCE     (1 << 19) /*  X  |     |     |     */
298 /*                                                        |     |     |     */
299 /* Class is linked apart from variance obligations.       |     |     |     */
300 #define ZEND_ACC_NEARLY_LINKED           (1 << 20) /*  X  |     |     |     */
301 /* Class has readonly props                               |     |     |     */
302 #define ZEND_ACC_HAS_READONLY_PROPS      (1 << 21) /*  X  |     |     |     */
303 /*                                                        |     |     |     */
304 /* stored in opcache (may be partially)                   |     |     |     */
305 #define ZEND_ACC_CACHED                  (1 << 22) /*  X  |     |     |     */
306 /*                                                        |     |     |     */
307 /* temporary flag used during delayed variance checks     |     |     |     */
308 #define ZEND_ACC_CACHEABLE               (1 << 23) /*  X  |     |     |     */
309 /*                                                        |     |     |     */
310 #define ZEND_ACC_HAS_AST_CONSTANTS       (1 << 24) /*  X  |     |     |     */
311 #define ZEND_ACC_HAS_AST_PROPERTIES      (1 << 25) /*  X  |     |     |     */
312 #define ZEND_ACC_HAS_AST_STATICS         (1 << 26) /*  X  |     |     |     */
313 /*                                                        |     |     |     */
314 /* loaded from file cache to process memory               |     |     |     */
315 #define ZEND_ACC_FILE_CACHED             (1 << 27) /*  X  |     |     |     */
316 /*                                                        |     |     |     */
317 /* Class cannot be serialized or unserialized             |     |     |     */
318 #define ZEND_ACC_NOT_SERIALIZABLE        (1 << 29) /*  X  |     |     |     */
319 /*                                                        |     |     |     */
320 /* Function Flags (unused: 29-30)                         |     |     |     */
321 /* ==============                                         |     |     |     */
322 /*                                                        |     |     |     */
323 /* deprecation flag                                       |     |     |     */
324 #define ZEND_ACC_DEPRECATED              (1 << 11) /*     |  X  |     |     */
325 /*                                                        |     |     |     */
326 /* Function returning by reference                        |     |     |     */
327 #define ZEND_ACC_RETURN_REFERENCE        (1 << 12) /*     |  X  |     |     */
328 /*                                                        |     |     |     */
329 /* Function has a return type                             |     |     |     */
330 #define ZEND_ACC_HAS_RETURN_TYPE         (1 << 13) /*     |  X  |     |     */
331 /*                                                        |     |     |     */
332 /* Function with variable number of arguments             |     |     |     */
333 #define ZEND_ACC_VARIADIC                (1 << 14) /*     |  X  |     |     */
334 /*                                                        |     |     |     */
335 /* op_array has finally blocks (user only)                |     |     |     */
336 #define ZEND_ACC_HAS_FINALLY_BLOCK       (1 << 15) /*     |  X  |     |     */
337 /*                                                        |     |     |     */
338 /* "main" op_array with                                   |     |     |     */
339 /* ZEND_DECLARE_CLASS_DELAYED opcodes                     |     |     |     */
340 #define ZEND_ACC_EARLY_BINDING           (1 << 16) /*     |  X  |     |     */
341 /*                                                        |     |     |     */
342 /* closure uses $this                                     |     |     |     */
343 #define ZEND_ACC_USES_THIS               (1 << 17) /*     |  X  |     |     */
344 /*                                                        |     |     |     */
345 /* call through user function trampoline. e.g.            |     |     |     */
346 /* __call, __callstatic                                   |     |     |     */
347 #define ZEND_ACC_CALL_VIA_TRAMPOLINE     (1 << 18) /*     |  X  |     |     */
348 /*                                                        |     |     |     */
349 /* disable inline caching                                 |     |     |     */
350 #define ZEND_ACC_NEVER_CACHE             (1 << 19) /*     |  X  |     |     */
351 /*                                                        |     |     |     */
352 /* op_array is a clone of trait method                    |     |     |     */
353 #define ZEND_ACC_TRAIT_CLONE             (1 << 20) /*     |  X  |     |     */
354 /*                                                        |     |     |     */
355 /* functions is a constructor                             |     |     |     */
356 #define ZEND_ACC_CTOR                    (1 << 21) /*     |  X  |     |     */
357 /*                                                        |     |     |     */
358 /* Closure related                                        |     |     |     */
359 #define ZEND_ACC_CLOSURE                 (1 << 22) /*     |  X  |     |     */
360 #define ZEND_ACC_FAKE_CLOSURE            (1 << 23) /*     |  X  |     |     */ /* Same as ZEND_CALL_FAKE_CLOSURE */
361 /*                                                        |     |     |     */
362 #define ZEND_ACC_GENERATOR               (1 << 24) /*     |  X  |     |     */
363 /*                                                        |     |     |     */
364 /* function was processed by pass two (user only)         |     |     |     */
365 #define ZEND_ACC_DONE_PASS_TWO           (1 << 25) /*     |  X  |     |     */
366 /*                                                        |     |     |     */
367 /* internal function is allocated at arena (int only)     |     |     |     */
368 #define ZEND_ACC_ARENA_ALLOCATED         (1 << 25) /*     |  X  |     |     */
369 /*                                                        |     |     |     */
370 /* run_time_cache allocated on heap (user only)           |     |     |     */
371 #define ZEND_ACC_HEAP_RT_CACHE           (1 << 26) /*     |  X  |     |     */
372 /*                                                        |     |     |     */
373 /* method flag used by Closure::__invoke() (int only)     |     |     |     */
374 #define ZEND_ACC_USER_ARG_INFO           (1 << 26) /*     |  X  |     |     */
375 /*                                                        |     |     |     */
376 /* supports opcache compile-time evaluation (funcs)       |     |     |     */
377 #define ZEND_ACC_COMPILE_TIME_EVAL       (1 << 27) /*     |  X  |     |     */
378 /*                                                        |     |     |     */
379 /* has #[\Override] attribute                             |     |     |     */
380 #define ZEND_ACC_OVERRIDE                (1 << 28) /*     |  X  |     |     */
381 /*                                                        |     |     |     */
382 /* op_array uses strict mode types                        |     |     |     */
383 #define ZEND_ACC_STRICT_TYPES            (1U << 31) /*    |  X  |     |     */
384 
385 
386 #define ZEND_ACC_PPP_MASK  (ZEND_ACC_PUBLIC | ZEND_ACC_PROTECTED | ZEND_ACC_PRIVATE)
387 
388 /* call through internal function handler. e.g. Closure::invoke() */
389 #define ZEND_ACC_CALL_VIA_HANDLER     ZEND_ACC_CALL_VIA_TRAMPOLINE
390 
391 #define ZEND_SHORT_CIRCUITING_CHAIN_MASK 0x3
392 #define ZEND_SHORT_CIRCUITING_CHAIN_EXPR 0
393 #define ZEND_SHORT_CIRCUITING_CHAIN_ISSET 1
394 #define ZEND_SHORT_CIRCUITING_CHAIN_EMPTY 2
395 
396 // Must not clash with ZEND_SHORT_CIRCUITING_CHAIN_MASK
397 #define ZEND_JMP_NULL_BP_VAR_IS 4
398 
399 char *zend_visibility_string(uint32_t fn_flags);
400 
401 typedef struct _zend_property_info {
402 	uint32_t offset; /* property offset for object properties or
403 	                      property index for static properties */
404 	uint32_t flags;
405 	zend_string *name;
406 	zend_string *doc_comment;
407 	HashTable *attributes;
408 	zend_class_entry *ce;
409 	zend_type type;
410 } zend_property_info;
411 
412 #define OBJ_PROP(obj, offset) \
413 	((zval*)((char*)(obj) + offset))
414 #define OBJ_PROP_NUM(obj, num) \
415 	(&(obj)->properties_table[(num)])
416 #define OBJ_PROP_TO_OFFSET(num) \
417 	((uint32_t)(XtOffsetOf(zend_object, properties_table) + sizeof(zval) * (num)))
418 #define OBJ_PROP_TO_NUM(offset) \
419 	((offset - OBJ_PROP_TO_OFFSET(0)) / sizeof(zval))
420 
421 typedef struct _zend_class_constant {
422 	zval value; /* flags are stored in u2 */
423 	zend_string *doc_comment;
424 	HashTable *attributes;
425 	zend_class_entry *ce;
426 	zend_type type;
427 } zend_class_constant;
428 
429 #define ZEND_CLASS_CONST_FLAGS(c) Z_CONSTANT_FLAGS((c)->value)
430 
431 /* arg_info for internal functions */
432 typedef struct _zend_internal_arg_info {
433 	const char *name;
434 	zend_type type;
435 	const char *default_value;
436 } zend_internal_arg_info;
437 
438 /* arg_info for user functions */
439 typedef struct _zend_arg_info {
440 	zend_string *name;
441 	zend_type type;
442 	zend_string *default_value;
443 } zend_arg_info;
444 
445 /* the following structure repeats the layout of zend_internal_arg_info,
446  * but its fields have different meaning. It's used as the first element of
447  * arg_info array to define properties of internal functions.
448  * It's also used for the return type.
449  */
450 typedef struct _zend_internal_function_info {
451 	uintptr_t required_num_args;
452 	zend_type type;
453 	const char *default_value;
454 } zend_internal_function_info;
455 
456 struct _zend_op_array {
457 	/* Common elements */
458 	uint8_t type;
459 	uint8_t arg_flags[3]; /* bitset of arg_info.pass_by_reference */
460 	uint32_t fn_flags;
461 	zend_string *function_name;
462 	zend_class_entry *scope;
463 	zend_function *prototype;
464 	uint32_t num_args;
465 	uint32_t required_num_args;
466 	zend_arg_info *arg_info;
467 	HashTable *attributes;
468 	ZEND_MAP_PTR_DEF(void **, run_time_cache);
469 	zend_string *doc_comment;
470 	uint32_t T;         /* number of temporary variables */
471 	/* END of common elements */
472 
473 	int cache_size;     /* number of run_time_cache_slots * sizeof(void*) */
474 	int last_var;       /* number of CV variables */
475 	uint32_t last;      /* number of opcodes */
476 
477 	zend_op *opcodes;
478 	ZEND_MAP_PTR_DEF(HashTable *, static_variables_ptr);
479 	HashTable *static_variables;
480 	zend_string **vars; /* names of CV variables */
481 
482 	uint32_t *refcount;
483 
484 	int last_live_range;
485 	int last_try_catch;
486 	zend_live_range *live_range;
487 	zend_try_catch_element *try_catch_array;
488 
489 	zend_string *filename;
490 	uint32_t line_start;
491 	uint32_t line_end;
492 
493 	int last_literal;
494 	uint32_t num_dynamic_func_defs;
495 	zval *literals;
496 
497 	/* Functions that are declared dynamically are stored here and
498 	 * referenced by index from opcodes. */
499 	zend_op_array **dynamic_func_defs;
500 
501 	void *reserved[ZEND_MAX_RESERVED_RESOURCES];
502 };
503 
504 
505 #define ZEND_RETURN_VALUE				0
506 #define ZEND_RETURN_REFERENCE			1
507 
508 #define INTERNAL_FUNCTION_PARAMETERS zend_execute_data *execute_data, zval *return_value
509 #define INTERNAL_FUNCTION_PARAM_PASSTHRU execute_data, return_value
510 
511 /* zend_internal_function_handler */
512 typedef void (ZEND_FASTCALL *zif_handler)(INTERNAL_FUNCTION_PARAMETERS);
513 
514 typedef struct _zend_internal_function {
515 	/* Common elements */
516 	uint8_t type;
517 	uint8_t arg_flags[3]; /* bitset of arg_info.pass_by_reference */
518 	uint32_t fn_flags;
519 	zend_string* function_name;
520 	zend_class_entry *scope;
521 	zend_function *prototype;
522 	uint32_t num_args;
523 	uint32_t required_num_args;
524 	zend_internal_arg_info *arg_info;
525 	HashTable *attributes;
526 	ZEND_MAP_PTR_DEF(void **, run_time_cache);
527 	zend_string *doc_comment;
528 	uint32_t T;         /* number of temporary variables */
529 	/* END of common elements */
530 
531 	zif_handler handler;
532 	struct _zend_module_entry *module;
533 	const zend_frameless_function_info *frameless_function_infos;
534 	void *reserved[ZEND_MAX_RESERVED_RESOURCES];
535 } zend_internal_function;
536 
537 #define ZEND_FN_SCOPE_NAME(function)  ((function) && (function)->common.scope ? ZSTR_VAL((function)->common.scope->name) : "")
538 
539 union _zend_function {
540 	uint8_t type;	/* MUST be the first element of this struct! */
541 	uint32_t   quick_arg_flags;
542 
543 	struct {
544 		uint8_t type;  /* never used */
545 		uint8_t arg_flags[3]; /* bitset of arg_info.pass_by_reference */
546 		uint32_t fn_flags;
547 		zend_string *function_name;
548 		zend_class_entry *scope;
549 		zend_function *prototype;
550 		uint32_t num_args;
551 		uint32_t required_num_args;
552 		zend_arg_info *arg_info;  /* index -1 represents the return value info, if any */
553 		HashTable   *attributes;
554 		ZEND_MAP_PTR_DEF(void **, run_time_cache);
555 		zend_string *doc_comment;
556 		uint32_t T;         /* number of temporary variables */
557 	} common;
558 
559 	zend_op_array op_array;
560 	zend_internal_function internal_function;
561 };
562 
563 struct _zend_execute_data {
564 	const zend_op       *opline;           /* executed opline                */
565 	zend_execute_data   *call;             /* current call                   */
566 	zval                *return_value;
567 	zend_function       *func;             /* executed function              */
568 	zval                 This;             /* this + call_info + num_args    */
569 	zend_execute_data   *prev_execute_data;
570 	zend_array          *symbol_table;
571 	void               **run_time_cache;   /* cache op_array->run_time_cache */
572 	zend_array          *extra_named_params;
573 };
574 
575 #define ZEND_CALL_HAS_THIS           IS_OBJECT_EX
576 
577 /* Top 16 bits of Z_TYPE_INFO(EX(This)) are used as call_info flags */
578 #define ZEND_CALL_FUNCTION           (0 << 16)
579 #define ZEND_CALL_CODE               (1 << 16)
580 #define ZEND_CALL_NESTED             (0 << 17)
581 #define ZEND_CALL_TOP                (1 << 17)
582 #define ZEND_CALL_ALLOCATED          (1 << 18)
583 #define ZEND_CALL_FREE_EXTRA_ARGS    (1 << 19)
584 #define ZEND_CALL_HAS_SYMBOL_TABLE   (1 << 20)
585 #define ZEND_CALL_RELEASE_THIS       (1 << 21)
586 #define ZEND_CALL_CLOSURE            (1 << 22)
587 #define ZEND_CALL_FAKE_CLOSURE       (1 << 23) /* Same as ZEND_ACC_FAKE_CLOSURE */
588 #define ZEND_CALL_GENERATOR          (1 << 24)
589 #define ZEND_CALL_DYNAMIC            (1 << 25)
590 #define ZEND_CALL_MAY_HAVE_UNDEF     (1 << 26)
591 #define ZEND_CALL_HAS_EXTRA_NAMED_PARAMS (1 << 27)
592 #define ZEND_CALL_OBSERVED           (1 << 28) /* "fcall_begin" observer handler may set this flag */
593                                                /* to prevent optimization in RETURN handler and    */
594                                                /* keep all local variables for "fcall_end" handler */
595 #define ZEND_CALL_JIT_RESERVED       (1 << 29) /* reserved for tracing JIT */
596 #define ZEND_CALL_NEEDS_REATTACH     (1 << 30)
597 #define ZEND_CALL_SEND_ARG_BY_REF    (1u << 31)
598 
599 #define ZEND_CALL_NESTED_FUNCTION    (ZEND_CALL_FUNCTION | ZEND_CALL_NESTED)
600 #define ZEND_CALL_NESTED_CODE        (ZEND_CALL_CODE | ZEND_CALL_NESTED)
601 #define ZEND_CALL_TOP_FUNCTION       (ZEND_CALL_TOP | ZEND_CALL_FUNCTION)
602 #define ZEND_CALL_TOP_CODE           (ZEND_CALL_CODE | ZEND_CALL_TOP)
603 
604 #define ZEND_CALL_INFO(call) \
605 	Z_TYPE_INFO((call)->This)
606 
607 #define ZEND_CALL_KIND_EX(call_info) \
608 	(call_info & (ZEND_CALL_CODE | ZEND_CALL_TOP))
609 
610 #define ZEND_CALL_KIND(call) \
611 	ZEND_CALL_KIND_EX(ZEND_CALL_INFO(call))
612 
613 #define ZEND_ADD_CALL_FLAG_EX(call_info, flag) do { \
614 		call_info |= (flag); \
615 	} while (0)
616 
617 #define ZEND_DEL_CALL_FLAG_EX(call_info, flag) do { \
618 		call_info &= ~(flag); \
619 	} while (0)
620 
621 #define ZEND_ADD_CALL_FLAG(call, flag) do { \
622 		ZEND_ADD_CALL_FLAG_EX(Z_TYPE_INFO((call)->This), flag); \
623 	} while (0)
624 
625 #define ZEND_DEL_CALL_FLAG(call, flag) do { \
626 		ZEND_DEL_CALL_FLAG_EX(Z_TYPE_INFO((call)->This), flag); \
627 	} while (0)
628 
629 #define ZEND_CALL_NUM_ARGS(call) \
630 	(call)->This.u2.num_args
631 
632 /* Ensure the correct alignment before slots calculation */
633 ZEND_STATIC_ASSERT(ZEND_MM_ALIGNED_SIZE(sizeof(zval)) == sizeof(zval),
634                    "zval must be aligned by ZEND_MM_ALIGNMENT");
635 /* A number of call frame slots (zvals) reserved for zend_execute_data. */
636 #define ZEND_CALL_FRAME_SLOT \
637 	((int)((sizeof(zend_execute_data) + sizeof(zval) - 1) / sizeof(zval)))
638 
639 #define ZEND_CALL_VAR(call, n) \
640 	((zval*)(((char*)(call)) + ((int)(n))))
641 
642 #define ZEND_CALL_VAR_NUM(call, n) \
643 	(((zval*)(call)) + (ZEND_CALL_FRAME_SLOT + ((int)(n))))
644 
645 #define ZEND_CALL_ARG(call, n) \
646 	ZEND_CALL_VAR_NUM(call, ((int)(n)) - 1)
647 
648 #define EX(element) 			((execute_data)->element)
649 
650 #define EX_CALL_INFO()			ZEND_CALL_INFO(execute_data)
651 #define EX_CALL_KIND()			ZEND_CALL_KIND(execute_data)
652 #define EX_NUM_ARGS()			ZEND_CALL_NUM_ARGS(execute_data)
653 
654 #define ZEND_CALL_USES_STRICT_TYPES(call) \
655 	(((call)->func->common.fn_flags & ZEND_ACC_STRICT_TYPES) != 0)
656 
657 #define EX_USES_STRICT_TYPES() \
658 	ZEND_CALL_USES_STRICT_TYPES(execute_data)
659 
660 #define ZEND_ARG_USES_STRICT_TYPES() \
661 	(EG(current_execute_data)->prev_execute_data && \
662 	 EG(current_execute_data)->prev_execute_data->func && \
663 	 ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data)->prev_execute_data))
664 
665 #define ZEND_FLF_ARG_USES_STRICT_TYPES() \
666 	(EG(current_execute_data) && \
667 	 EG(current_execute_data)->func && \
668 	 ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data)))
669 
670 #define ZEND_RET_USES_STRICT_TYPES() \
671 	ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data))
672 
673 #define EX_VAR(n)				ZEND_CALL_VAR(execute_data, n)
674 #define EX_VAR_NUM(n)			ZEND_CALL_VAR_NUM(execute_data, n)
675 
676 #define EX_VAR_TO_NUM(n)		((uint32_t)((n) / sizeof(zval) - ZEND_CALL_FRAME_SLOT))
677 #define EX_NUM_TO_VAR(n)		((uint32_t)(((n) + ZEND_CALL_FRAME_SLOT) * sizeof(zval)))
678 
679 #define ZEND_OPLINE_TO_OFFSET(opline, target) \
680 	((char*)(target) - (char*)(opline))
681 
682 #define ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline_num) \
683 	((char*)&(op_array)->opcodes[opline_num] - (char*)(opline))
684 
685 #define ZEND_OFFSET_TO_OPLINE(base, offset) \
686 	((zend_op*)(((char*)(base)) + (int)offset))
687 
688 #define ZEND_OFFSET_TO_OPLINE_NUM(op_array, base, offset) \
689 	(ZEND_OFFSET_TO_OPLINE(base, offset) - op_array->opcodes)
690 
691 #if ZEND_USE_ABS_JMP_ADDR
692 
693 /* run-time jump target */
694 # define OP_JMP_ADDR(opline, node) \
695 	(node).jmp_addr
696 
697 # define ZEND_SET_OP_JMP_ADDR(opline, node, val) do { \
698 		(node).jmp_addr = (val); \
699 	} while (0)
700 
701 /* convert jump target from compile-time to run-time */
702 # define ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, node) do { \
703 		(node).jmp_addr = (op_array)->opcodes + (node).opline_num; \
704 	} while (0)
705 
706 /* convert jump target back from run-time to compile-time */
707 # define ZEND_PASS_TWO_UNDO_JMP_TARGET(op_array, opline, node) do { \
708 		(node).opline_num = (node).jmp_addr - (op_array)->opcodes; \
709 	} while (0)
710 
711 #else
712 
713 /* run-time jump target */
714 # define OP_JMP_ADDR(opline, node) \
715 	ZEND_OFFSET_TO_OPLINE(opline, (node).jmp_offset)
716 
717 # define ZEND_SET_OP_JMP_ADDR(opline, node, val) do { \
718 		(node).jmp_offset = ZEND_OPLINE_TO_OFFSET(opline, val); \
719 	} while (0)
720 
721 /* convert jump target from compile-time to run-time */
722 # define ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, node) do { \
723 		(node).jmp_offset = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, (node).opline_num); \
724 	} while (0)
725 
726 /* convert jump target back from run-time to compile-time */
727 # define ZEND_PASS_TWO_UNDO_JMP_TARGET(op_array, opline, node) do { \
728 		(node).opline_num = ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, (node).jmp_offset); \
729 	} while (0)
730 
731 #endif
732 
733 /* constant-time constant */
734 # define CT_CONSTANT_EX(op_array, num) \
735 	((op_array)->literals + (num))
736 
737 # define CT_CONSTANT(node) \
738 	CT_CONSTANT_EX(CG(active_op_array), (node).constant)
739 
740 #if ZEND_USE_ABS_CONST_ADDR
741 
742 /* run-time constant */
743 # define RT_CONSTANT(opline, node) \
744 	(node).zv
745 
746 /* convert constant from compile-time to run-time */
747 # define ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, node) do { \
748 		(node).zv = CT_CONSTANT_EX(op_array, (node).constant); \
749 	} while (0)
750 
751 #else
752 
753 /* At run-time, constants are allocated together with op_array->opcodes
754  * and addressed relatively to current opline.
755  */
756 
757 /* run-time constant */
758 # define RT_CONSTANT(opline, node) \
759 	((zval*)(((char*)(opline)) + (int32_t)(node).constant))
760 
761 /* convert constant from compile-time to run-time */
762 # define ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, node) do { \
763 		(node).constant = \
764 			(((char*)CT_CONSTANT_EX(op_array, (node).constant)) - \
765 			((char*)opline)); \
766 	} while (0)
767 
768 #endif
769 
770 /* convert constant back from run-time to compile-time */
771 #define ZEND_PASS_TWO_UNDO_CONSTANT(op_array, opline, node) do { \
772 		(node).constant = RT_CONSTANT(opline, node) - (op_array)->literals; \
773 	} while (0)
774 
775 #define RUN_TIME_CACHE(op_array) \
776 	ZEND_MAP_PTR_GET((op_array)->run_time_cache)
777 
778 #define ZEND_OP_ARRAY_EXTENSION(op_array, handle) \
779 	((void**)RUN_TIME_CACHE(op_array))[handle]
780 
781 #define IS_UNUSED	0		/* Unused operand */
782 #define IS_CONST	(1<<0)
783 #define IS_TMP_VAR	(1<<1)
784 #define IS_VAR		(1<<2)
785 #define IS_CV		(1<<3)	/* Compiled variable */
786 
787 /* Used for result.type of smart branch instructions */
788 #define IS_SMART_BRANCH_JMPZ  (1<<4)
789 #define IS_SMART_BRANCH_JMPNZ (1<<5)
790 
791 #define ZEND_EXTRA_VALUE 1
792 
793 #include "zend_globals.h"
794 
795 typedef enum _zend_compile_position {
796 	ZEND_COMPILE_POSITION_AT_SHEBANG = 0,
797 	ZEND_COMPILE_POSITION_AT_OPEN_TAG,
798 	ZEND_COMPILE_POSITION_AFTER_OPEN_TAG
799 } zend_compile_position;
800 
801 BEGIN_EXTERN_C()
802 
803 void init_compiler(void);
804 void shutdown_compiler(void);
805 void zend_init_compiler_data_structures(void);
806 
807 void zend_oparray_context_begin(zend_oparray_context *prev_context, zend_op_array *op_array);
808 void zend_oparray_context_end(zend_oparray_context *prev_context);
809 void zend_file_context_begin(zend_file_context *prev_context);
810 void zend_file_context_end(zend_file_context *prev_context);
811 
812 extern ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
813 extern ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename, zend_compile_position position);
814 
815 ZEND_API int ZEND_FASTCALL lex_scan(zval *zendlval, zend_parser_stack_elem *elem);
816 void startup_scanner(void);
817 void shutdown_scanner(void);
818 
819 ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename);
820 ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename);
821 ZEND_API zend_string *zend_get_compiled_filename(void);
822 ZEND_API int zend_get_compiled_lineno(void);
823 ZEND_API size_t zend_get_scanned_file_offset(void);
824 
825 ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var);
826 
827 #ifdef ZTS
828 const char *zend_get_zendtext(void);
829 int zend_get_zendleng(void);
830 #endif
831 
832 typedef zend_result (ZEND_FASTCALL *unary_op_type)(zval *, zval *);
833 typedef zend_result (ZEND_FASTCALL *binary_op_type)(zval *, zval *, zval *);
834 
835 ZEND_API unary_op_type get_unary_op(int opcode);
836 ZEND_API binary_op_type get_binary_op(int opcode);
837 
838 void zend_stop_lexing(void);
839 void zend_emit_final_return(bool return_one);
840 
841 typedef enum {
842 	ZEND_MODIFIER_TARGET_PROPERTY = 0,
843 	ZEND_MODIFIER_TARGET_METHOD,
844 	ZEND_MODIFIER_TARGET_CONSTANT,
845 	ZEND_MODIFIER_TARGET_CPP,
846 } zend_modifier_target;
847 
848 /* Used during AST construction */
849 zend_ast *zend_ast_append_str(zend_ast *left, zend_ast *right);
850 zend_ast *zend_negate_num_string(zend_ast *ast);
851 uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag);
852 uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag);
853 uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target);
854 
855 uint32_t zend_modifier_token_to_flag(zend_modifier_target target, uint32_t flags);
856 uint32_t zend_modifier_list_to_flags(zend_modifier_target target, zend_ast *modifiers);
857 
858 bool zend_handle_encoding_declaration(zend_ast *ast);
859 
860 ZEND_API zend_class_entry *zend_bind_class_in_slot(
861 		zval *class_table_slot, zval *lcname, zend_string *lc_parent_name);
862 ZEND_API zend_result do_bind_function(zend_function *func, zval *lcname);
863 ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name);
864 
865 void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline);
866 
867 ZEND_API void function_add_ref(zend_function *function);
868 zend_string *zval_make_interned_string(zval *zv);
869 
870 #define INITIAL_OP_ARRAY_SIZE 64
871 
872 
873 /* helper functions in zend_language_scanner.l */
874 struct _zend_arena;
875 
876 ZEND_API zend_op_array *compile_file(zend_file_handle *file_handle, int type);
877 ZEND_API zend_op_array *compile_string(zend_string *source_string, const char *filename, zend_compile_position position);
878 ZEND_API zend_op_array *compile_filename(int type, zend_string *filename);
879 ZEND_API zend_ast *zend_compile_string_to_ast(
880 		zend_string *code, struct _zend_arena **ast_arena, zend_string *filename);
881 ZEND_API zend_result zend_execute_scripts(int type, zval *retval, int file_count, ...);
882 ZEND_API zend_result zend_execute_script(int type, zval *retval, zend_file_handle *file_handle);
883 ZEND_API zend_result open_file_for_scanning(zend_file_handle *file_handle);
884 ZEND_API void init_op_array(zend_op_array *op_array, uint8_t type, int initial_ops_size);
885 ZEND_API void destroy_op_array(zend_op_array *op_array);
886 ZEND_API void zend_destroy_static_vars(zend_op_array *op_array);
887 ZEND_API void zend_destroy_file_handle(zend_file_handle *file_handle);
888 ZEND_API void zend_cleanup_mutable_class_data(zend_class_entry *ce);
889 ZEND_API void zend_cleanup_internal_class_data(zend_class_entry *ce);
890 ZEND_API void zend_type_release(zend_type type, bool persistent);
891 ZEND_API zend_string *zend_create_member_string(zend_string *class_name, zend_string *member_name);
892 
893 
894 ZEND_API ZEND_COLD void zend_user_exception_handler(void);
895 
896 #define zend_try_exception_handler() do { \
897 		if (UNEXPECTED(EG(exception))) { \
898 			if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) { \
899 				zend_user_exception_handler(); \
900 			} \
901 		} \
902 	} while (0)
903 
904 void zend_free_internal_arg_info(zend_internal_function *function);
905 ZEND_API void destroy_zend_function(zend_function *function);
906 ZEND_API void zend_function_dtor(zval *zv);
907 ZEND_API void destroy_zend_class(zval *zv);
908 void zend_class_add_ref(zval *zv);
909 
910 ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal);
911 #define zend_unmangle_property_name(mangled_property, class_name, prop_name) \
912         zend_unmangle_property_name_ex(mangled_property, class_name, prop_name, NULL)
913 ZEND_API zend_result zend_unmangle_property_name_ex(const zend_string *name, const char **class_name, const char **prop_name, size_t *prop_len);
914 
zend_get_unmangled_property_name(const zend_string * mangled_prop)915 static zend_always_inline const char *zend_get_unmangled_property_name(const zend_string *mangled_prop) {
916 	const char *class_name, *prop_name;
917 	zend_unmangle_property_name(mangled_prop, &class_name, &prop_name);
918 	return prop_name;
919 }
920 
921 #define ZEND_FUNCTION_DTOR zend_function_dtor
922 #define ZEND_CLASS_DTOR destroy_zend_class
923 
924 typedef bool (*zend_needs_live_range_cb)(zend_op_array *op_array, zend_op *opline);
925 ZEND_API void zend_recalc_live_ranges(
926 	zend_op_array *op_array, zend_needs_live_range_cb needs_live_range);
927 
928 ZEND_API void pass_two(zend_op_array *op_array);
929 ZEND_API bool zend_is_compiling(void);
930 ZEND_API char *zend_make_compiled_string_description(const char *name);
931 ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_handlers);
932 uint32_t zend_get_class_fetch_type(const zend_string *name);
933 ZEND_API uint8_t zend_get_call_op(const zend_op *init_op, zend_function *fbc);
934 ZEND_API bool zend_is_smart_branch(const zend_op *opline);
935 
936 typedef bool (*zend_auto_global_callback)(zend_string *name);
937 typedef struct _zend_auto_global {
938 	zend_string *name;
939 	zend_auto_global_callback auto_global_callback;
940 	bool jit;
941 	bool armed;
942 } zend_auto_global;
943 
944 ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback);
945 ZEND_API void zend_activate_auto_globals(void);
946 ZEND_API bool zend_is_auto_global(zend_string *name);
947 ZEND_API bool zend_is_auto_global_str(const char *name, size_t len);
948 ZEND_API size_t zend_dirname(char *path, size_t len);
949 ZEND_API void zend_set_function_arg_flags(zend_function *func);
950 
951 int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem);
952 
953 void zend_assert_valid_class_name(const zend_string *const_name);
954 
955 zend_string *zend_type_to_string_resolved(zend_type type, zend_class_entry *scope);
956 ZEND_API zend_string *zend_type_to_string(zend_type type);
957 
958 /* BEGIN: OPCODES */
959 
960 #include "zend_vm_opcodes.h"
961 
962 /* END: OPCODES */
963 
964 /* class fetches */
965 #define ZEND_FETCH_CLASS_DEFAULT	0
966 #define ZEND_FETCH_CLASS_SELF		1
967 #define ZEND_FETCH_CLASS_PARENT		2
968 #define ZEND_FETCH_CLASS_STATIC		3
969 #define ZEND_FETCH_CLASS_AUTO		4
970 #define ZEND_FETCH_CLASS_INTERFACE	5
971 #define ZEND_FETCH_CLASS_TRAIT		6
972 #define ZEND_FETCH_CLASS_MASK        0x0f
973 #define ZEND_FETCH_CLASS_NO_AUTOLOAD 0x80
974 #define ZEND_FETCH_CLASS_SILENT      0x0100
975 #define ZEND_FETCH_CLASS_EXCEPTION   0x0200
976 #define ZEND_FETCH_CLASS_ALLOW_UNLINKED 0x0400
977 #define ZEND_FETCH_CLASS_ALLOW_NEARLY_LINKED 0x0800
978 
979 /* These should not clash with ZEND_ACC_(PUBLIC|PROTECTED|PRIVATE) */
980 #define ZEND_PARAM_REF      (1<<3)
981 #define ZEND_PARAM_VARIADIC (1<<4)
982 
983 #define ZEND_NAME_FQ       0
984 #define ZEND_NAME_NOT_FQ   1
985 #define ZEND_NAME_RELATIVE 2
986 
987 /* ZEND_FETCH_ flags in class name AST of new const expression must not clash with ZEND_NAME_ flags */
988 #define ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT 2
989 
990 #define ZEND_TYPE_NULLABLE (1<<8)
991 
992 #define ZEND_ARRAY_SYNTAX_LIST 1  /* list() */
993 #define ZEND_ARRAY_SYNTAX_LONG 2  /* array() */
994 #define ZEND_ARRAY_SYNTAX_SHORT 3 /* [] */
995 
996 /* var status for backpatching */
997 #define BP_VAR_R			0
998 #define BP_VAR_W			1
999 #define BP_VAR_RW			2
1000 #define BP_VAR_IS			3
1001 #define BP_VAR_FUNC_ARG		4
1002 #define BP_VAR_UNSET		5
1003 
1004 #define ZEND_INTERNAL_FUNCTION		1
1005 #define ZEND_USER_FUNCTION			2
1006 #define ZEND_EVAL_CODE				4
1007 
1008 #define ZEND_USER_CODE(type)		((type) != ZEND_INTERNAL_FUNCTION)
1009 
1010 #define ZEND_INTERNAL_CLASS         1
1011 #define ZEND_USER_CLASS             2
1012 
1013 #define ZEND_EVAL				(1<<0)
1014 #define ZEND_INCLUDE			(1<<1)
1015 #define ZEND_INCLUDE_ONCE		(1<<2)
1016 #define ZEND_REQUIRE			(1<<3)
1017 #define ZEND_REQUIRE_ONCE		(1<<4)
1018 
1019 /* global/local fetches */
1020 #define ZEND_FETCH_GLOBAL		(1<<1)
1021 #define ZEND_FETCH_LOCAL		(1<<2)
1022 #define ZEND_FETCH_GLOBAL_LOCK	(1<<3)
1023 
1024 #define ZEND_FETCH_TYPE_MASK	0xe
1025 
1026 /* Only one of these can ever be in use */
1027 #define ZEND_FETCH_REF			1
1028 #define ZEND_FETCH_DIM_WRITE	2
1029 #define ZEND_FETCH_OBJ_FLAGS	3
1030 
1031 /* Used to mark what kind of operation a writing FETCH_DIM is used in,
1032  * to produce a more precise error on incorrect string offset use. */
1033 #define ZEND_FETCH_DIM_REF 1
1034 #define ZEND_FETCH_DIM_DIM 2
1035 #define ZEND_FETCH_DIM_OBJ 3
1036 #define ZEND_FETCH_DIM_INCDEC 4
1037 
1038 #define ZEND_ISEMPTY			(1<<0)
1039 
1040 #define ZEND_LAST_CATCH			(1<<0)
1041 
1042 #define ZEND_FREE_ON_RETURN     (1<<0)
1043 #define ZEND_FREE_SWITCH        (1<<1)
1044 
1045 #define ZEND_SEND_BY_VAL     0u
1046 #define ZEND_SEND_BY_REF     1u
1047 #define ZEND_SEND_PREFER_REF 2u
1048 
1049 #define ZEND_THROW_IS_EXPR 1u
1050 
1051 #define ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS 1
1052 
1053 /* The send mode, the is_variadic, the is_promoted, and the is_tentative flags are stored as part of zend_type */
1054 #define _ZEND_SEND_MODE_SHIFT _ZEND_TYPE_EXTRA_FLAGS_SHIFT
1055 #define _ZEND_IS_VARIADIC_BIT (1 << (_ZEND_TYPE_EXTRA_FLAGS_SHIFT + 2))
1056 #define _ZEND_IS_PROMOTED_BIT (1 << (_ZEND_TYPE_EXTRA_FLAGS_SHIFT + 3))
1057 #define _ZEND_IS_TENTATIVE_BIT (1 << (_ZEND_TYPE_EXTRA_FLAGS_SHIFT + 4))
1058 #define ZEND_ARG_SEND_MODE(arg_info) \
1059 	((ZEND_TYPE_FULL_MASK((arg_info)->type) >> _ZEND_SEND_MODE_SHIFT) & 3)
1060 #define ZEND_ARG_IS_VARIADIC(arg_info) \
1061 	((ZEND_TYPE_FULL_MASK((arg_info)->type) & _ZEND_IS_VARIADIC_BIT) != 0)
1062 #define ZEND_ARG_IS_PROMOTED(arg_info) \
1063 	((ZEND_TYPE_FULL_MASK((arg_info)->type) & _ZEND_IS_PROMOTED_BIT) != 0)
1064 #define ZEND_ARG_TYPE_IS_TENTATIVE(arg_info) \
1065 	((ZEND_TYPE_FULL_MASK((arg_info)->type) & _ZEND_IS_TENTATIVE_BIT) != 0)
1066 
1067 #define ZEND_DIM_IS					(1 << 0) /* isset fetch needed for null coalesce. Set in zend_compile.c for ZEND_AST_DIM nested within ZEND_AST_COALESCE. */
1068 #define ZEND_DIM_ALTERNATIVE_SYNTAX	(1 << 1) /* deprecated curly brace usage */
1069 
1070 /* Attributes for ${} encaps var in strings (ZEND_AST_DIM or ZEND_AST_VAR node) */
1071 /* ZEND_AST_VAR nodes can have any of the ZEND_ENCAPS_VAR_* flags */
1072 /* ZEND_AST_DIM flags can have ZEND_DIM_ALTERNATIVE_SYNTAX or ZEND_ENCAPS_VAR_DOLLAR_CURLY during the parse phase (ZEND_DIM_ALTERNATIVE_SYNTAX is a thrown fatal error). */
1073 #define ZEND_ENCAPS_VAR_DOLLAR_CURLY (1 << 0)
1074 #define ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR (1 << 1)
1075 
1076 /* Make sure these don't clash with ZEND_FETCH_CLASS_* flags. */
1077 #define IS_CONSTANT_CLASS                    0x400 /* __CLASS__ in trait */
1078 #define IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE 0x800
1079 
zend_check_arg_send_type(const zend_function * zf,uint32_t arg_num,uint32_t mask)1080 static zend_always_inline bool zend_check_arg_send_type(const zend_function *zf, uint32_t arg_num, uint32_t mask)
1081 {
1082 	arg_num--;
1083 	if (UNEXPECTED(arg_num >= zf->common.num_args)) {
1084 		if (EXPECTED((zf->common.fn_flags & ZEND_ACC_VARIADIC) == 0)) {
1085 			return 0;
1086 		}
1087 		arg_num = zf->common.num_args;
1088 	}
1089 	return UNEXPECTED((ZEND_ARG_SEND_MODE(&zf->common.arg_info[arg_num]) & mask) != 0);
1090 }
1091 
1092 #define ARG_MUST_BE_SENT_BY_REF(zf, arg_num) \
1093 	zend_check_arg_send_type(zf, arg_num, ZEND_SEND_BY_REF)
1094 
1095 #define ARG_SHOULD_BE_SENT_BY_REF(zf, arg_num) \
1096 	zend_check_arg_send_type(zf, arg_num, ZEND_SEND_BY_REF|ZEND_SEND_PREFER_REF)
1097 
1098 #define ARG_MAY_BE_SENT_BY_REF(zf, arg_num) \
1099 	zend_check_arg_send_type(zf, arg_num, ZEND_SEND_PREFER_REF)
1100 
1101 /* Quick API to check first 12 arguments */
1102 #define MAX_ARG_FLAG_NUM 12
1103 
1104 #ifdef WORDS_BIGENDIAN
1105 # define ZEND_SET_ARG_FLAG(zf, arg_num, mask) do { \
1106 		(zf)->quick_arg_flags |= ((mask) << ((arg_num) - 1) * 2); \
1107 	} while (0)
1108 # define ZEND_CHECK_ARG_FLAG(zf, arg_num, mask) \
1109 	(((zf)->quick_arg_flags >> (((arg_num) - 1) * 2)) & (mask))
1110 #else
1111 # define ZEND_SET_ARG_FLAG(zf, arg_num, mask) do { \
1112 		(zf)->quick_arg_flags |= (((mask) << 6) << (arg_num) * 2); \
1113 	} while (0)
1114 # define ZEND_CHECK_ARG_FLAG(zf, arg_num, mask) \
1115 	(((zf)->quick_arg_flags >> (((arg_num) + 3) * 2)) & (mask))
1116 #endif
1117 
1118 #define QUICK_ARG_MUST_BE_SENT_BY_REF(zf, arg_num) \
1119 	ZEND_CHECK_ARG_FLAG(zf, arg_num, ZEND_SEND_BY_REF)
1120 
1121 #define QUICK_ARG_SHOULD_BE_SENT_BY_REF(zf, arg_num) \
1122 	ZEND_CHECK_ARG_FLAG(zf, arg_num, ZEND_SEND_BY_REF|ZEND_SEND_PREFER_REF)
1123 
1124 #define QUICK_ARG_MAY_BE_SENT_BY_REF(zf, arg_num) \
1125 	ZEND_CHECK_ARG_FLAG(zf, arg_num, ZEND_SEND_PREFER_REF)
1126 
1127 #define ZEND_RETURN_VAL 0
1128 #define ZEND_RETURN_REF 1
1129 
1130 #define ZEND_BIND_VAL      0
1131 #define ZEND_BIND_REF      1
1132 #define ZEND_BIND_IMPLICIT 2
1133 #define ZEND_BIND_EXPLICIT 4
1134 
1135 #define ZEND_RETURNS_FUNCTION (1<<0)
1136 #define ZEND_RETURNS_VALUE    (1<<1)
1137 
1138 #define ZEND_ARRAY_ELEMENT_REF		(1<<0)
1139 #define ZEND_ARRAY_NOT_PACKED		(1<<1)
1140 #define ZEND_ARRAY_SIZE_SHIFT		2
1141 
1142 /* Attribute for ternary inside parentheses */
1143 #define ZEND_PARENTHESIZED_CONDITIONAL 1
1144 
1145 /* For "use" AST nodes and the seen symbol table */
1146 #define ZEND_SYMBOL_CLASS    (1<<0)
1147 #define ZEND_SYMBOL_FUNCTION (1<<1)
1148 #define ZEND_SYMBOL_CONST    (1<<2)
1149 
1150 /* All increment opcodes are even (decrement are odd) */
1151 #define ZEND_IS_INCREMENT(opcode) (((opcode) & 1) == 0)
1152 
1153 #define ZEND_IS_BINARY_ASSIGN_OP_OPCODE(opcode) \
1154 	(((opcode) >= ZEND_ADD) && ((opcode) <= ZEND_POW))
1155 
1156 /* Pseudo-opcodes that are used only temporarily during compilation */
1157 #define ZEND_GOTO  253
1158 #define ZEND_BRK   254
1159 #define ZEND_CONT  255
1160 
1161 
1162 END_EXTERN_C()
1163 
1164 #define ZEND_CLONE_FUNC_NAME		"__clone"
1165 #define ZEND_CONSTRUCTOR_FUNC_NAME	"__construct"
1166 #define ZEND_DESTRUCTOR_FUNC_NAME	"__destruct"
1167 #define ZEND_GET_FUNC_NAME          "__get"
1168 #define ZEND_SET_FUNC_NAME          "__set"
1169 #define ZEND_UNSET_FUNC_NAME        "__unset"
1170 #define ZEND_ISSET_FUNC_NAME        "__isset"
1171 #define ZEND_CALL_FUNC_NAME         "__call"
1172 #define ZEND_CALLSTATIC_FUNC_NAME   "__callstatic"
1173 #define ZEND_TOSTRING_FUNC_NAME     "__tostring"
1174 #define ZEND_INVOKE_FUNC_NAME       "__invoke"
1175 #define ZEND_DEBUGINFO_FUNC_NAME    "__debuginfo"
1176 
1177 /* The following constants may be combined in CG(compiler_options)
1178  * to change the default compiler behavior */
1179 
1180 /* generate extended debug information */
1181 #define ZEND_COMPILE_EXTENDED_STMT              (1<<0)
1182 #define ZEND_COMPILE_EXTENDED_FCALL             (1<<1)
1183 #define ZEND_COMPILE_EXTENDED_INFO              (ZEND_COMPILE_EXTENDED_STMT|ZEND_COMPILE_EXTENDED_FCALL)
1184 
1185 /* call op_array handler of extensions */
1186 #define ZEND_COMPILE_HANDLE_OP_ARRAY            (1<<2)
1187 
1188 /* generate ZEND_INIT_FCALL_BY_NAME for internal functions instead of ZEND_INIT_FCALL */
1189 #define ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS  (1<<3)
1190 
1191 /* don't perform early binding for classes inherited form internal ones;
1192  * in namespaces assume that internal class that doesn't exist at compile-time
1193  * may appear in run-time */
1194 #define ZEND_COMPILE_IGNORE_INTERNAL_CLASSES    (1<<4)
1195 
1196 /* generate ZEND_DECLARE_CLASS_DELAYED opcode to delay early binding */
1197 #define ZEND_COMPILE_DELAYED_BINDING            (1<<5)
1198 
1199 /* disable constant substitution at compile-time */
1200 #define ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION   (1<<6)
1201 
1202 /* disable substitution of persistent constants at compile-time */
1203 #define ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION	(1<<8)
1204 
1205 /* generate ZEND_INIT_FCALL_BY_NAME for userland functions instead of ZEND_INIT_FCALL */
1206 #define ZEND_COMPILE_IGNORE_USER_FUNCTIONS      (1<<9)
1207 
1208 /* force ZEND_ACC_USE_GUARDS for all classes */
1209 #define ZEND_COMPILE_GUARDS						(1<<10)
1210 
1211 /* disable builtin special case function calls */
1212 #define ZEND_COMPILE_NO_BUILTINS				(1<<11)
1213 
1214 /* result of compilation may be stored in file cache */
1215 #define ZEND_COMPILE_WITH_FILE_CACHE			(1<<12)
1216 
1217 /* ignore functions and classes declared in other files */
1218 #define ZEND_COMPILE_IGNORE_OTHER_FILES			(1<<13)
1219 
1220 /* this flag is set when compiler invoked by opcache_compile_file() */
1221 #define ZEND_COMPILE_WITHOUT_EXECUTION          (1<<14)
1222 
1223 /* this flag is set when compiler invoked during preloading */
1224 #define ZEND_COMPILE_PRELOAD                    (1<<15)
1225 
1226 /* disable jumptable optimization for switch statements */
1227 #define ZEND_COMPILE_NO_JUMPTABLES				(1<<16)
1228 
1229 /* this flag is set when compiler invoked during preloading in separate process */
1230 #define ZEND_COMPILE_PRELOAD_IN_CHILD           (1<<17)
1231 
1232 /* ignore observer notifications, e.g. to manually notify afterwards in a post-processing step after compilation */
1233 #define ZEND_COMPILE_IGNORE_OBSERVER			(1<<18)
1234 
1235 /* The default value for CG(compiler_options) */
1236 #define ZEND_COMPILE_DEFAULT					ZEND_COMPILE_HANDLE_OP_ARRAY
1237 
1238 /* The default value for CG(compiler_options) during eval() */
1239 #define ZEND_COMPILE_DEFAULT_FOR_EVAL			0
1240 
1241 ZEND_API bool zend_is_op_long_compatible(const zval *op);
1242 ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, const zval *op2);
1243 ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, const zval *op);
1244 
1245 #endif /* ZEND_COMPILE_H */
1246