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