xref: /PHP-8.2/Zend/zend_compile.h (revision c149b4f5)
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 	uint32_t T;         /* number of temporary variables */
468 	/* END of common elements */
469 
470 	int cache_size;     /* number of run_time_cache_slots * sizeof(void*) */
471 	int last_var;       /* number of CV variables */
472 	uint32_t last;      /* number of opcodes */
473 
474 	zend_op *opcodes;
475 	ZEND_MAP_PTR_DEF(HashTable *, static_variables_ptr);
476 	HashTable *static_variables;
477 	zend_string **vars; /* names of CV variables */
478 
479 	uint32_t *refcount;
480 
481 	int last_live_range;
482 	int last_try_catch;
483 	zend_live_range *live_range;
484 	zend_try_catch_element *try_catch_array;
485 
486 	zend_string *filename;
487 	uint32_t line_start;
488 	uint32_t line_end;
489 	zend_string *doc_comment;
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 	uint32_t T;         /* number of temporary variables */
526 	/* END of common elements */
527 
528 	zif_handler handler;
529 	struct _zend_module_entry *module;
530 	const zend_frameless_function_info *frameless_function_infos;
531 	void *reserved[ZEND_MAX_RESERVED_RESOURCES];
532 } zend_internal_function;
533 
534 #define ZEND_FN_SCOPE_NAME(function)  ((function) && (function)->common.scope ? ZSTR_VAL((function)->common.scope->name) : "")
535 
536 union _zend_function {
537 	uint8_t type;	/* MUST be the first element of this struct! */
538 	uint32_t   quick_arg_flags;
539 
540 	struct {
541 		uint8_t type;  /* never used */
542 		uint8_t arg_flags[3]; /* bitset of arg_info.pass_by_reference */
543 		uint32_t fn_flags;
544 		zend_string *function_name;
545 		zend_class_entry *scope;
546 		zend_function *prototype;
547 		uint32_t num_args;
548 		uint32_t required_num_args;
549 		zend_arg_info *arg_info;  /* index -1 represents the return value info, if any */
550 		HashTable   *attributes;
551 		ZEND_MAP_PTR_DEF(void **, run_time_cache);
552 		uint32_t T;         /* number of temporary variables */
553 	} common;
554 
555 	zend_op_array op_array;
556 	zend_internal_function internal_function;
557 };
558 
559 struct _zend_execute_data {
560 	const zend_op       *opline;           /* executed opline                */
561 	zend_execute_data   *call;             /* current call                   */
562 	zval                *return_value;
563 	zend_function       *func;             /* executed function              */
564 	zval                 This;             /* this + call_info + num_args    */
565 	zend_execute_data   *prev_execute_data;
566 	zend_array          *symbol_table;
567 	void               **run_time_cache;   /* cache op_array->run_time_cache */
568 	zend_array          *extra_named_params;
569 };
570 
571 #define ZEND_CALL_HAS_THIS           IS_OBJECT_EX
572 
573 /* Top 16 bits of Z_TYPE_INFO(EX(This)) are used as call_info flags */
574 #define ZEND_CALL_FUNCTION           (0 << 16)
575 #define ZEND_CALL_CODE               (1 << 16)
576 #define ZEND_CALL_NESTED             (0 << 17)
577 #define ZEND_CALL_TOP                (1 << 17)
578 #define ZEND_CALL_ALLOCATED          (1 << 18)
579 #define ZEND_CALL_FREE_EXTRA_ARGS    (1 << 19)
580 #define ZEND_CALL_HAS_SYMBOL_TABLE   (1 << 20)
581 #define ZEND_CALL_RELEASE_THIS       (1 << 21)
582 #define ZEND_CALL_CLOSURE            (1 << 22)
583 #define ZEND_CALL_FAKE_CLOSURE       (1 << 23) /* Same as ZEND_ACC_FAKE_CLOSURE */
584 #define ZEND_CALL_GENERATOR          (1 << 24)
585 #define ZEND_CALL_DYNAMIC            (1 << 25)
586 #define ZEND_CALL_MAY_HAVE_UNDEF     (1 << 26)
587 #define ZEND_CALL_HAS_EXTRA_NAMED_PARAMS (1 << 27)
588 #define ZEND_CALL_OBSERVED           (1 << 28) /* "fcall_begin" observer handler may set this flag */
589                                                /* to prevent optimization in RETURN handler and    */
590                                                /* keep all local variables for "fcall_end" handler */
591 #define ZEND_CALL_JIT_RESERVED       (1 << 29) /* reserved for tracing JIT */
592 #define ZEND_CALL_NEEDS_REATTACH     (1 << 30)
593 #define ZEND_CALL_SEND_ARG_BY_REF    (1u << 31)
594 
595 #define ZEND_CALL_NESTED_FUNCTION    (ZEND_CALL_FUNCTION | ZEND_CALL_NESTED)
596 #define ZEND_CALL_NESTED_CODE        (ZEND_CALL_CODE | ZEND_CALL_NESTED)
597 #define ZEND_CALL_TOP_FUNCTION       (ZEND_CALL_TOP | ZEND_CALL_FUNCTION)
598 #define ZEND_CALL_TOP_CODE           (ZEND_CALL_CODE | ZEND_CALL_TOP)
599 
600 #define ZEND_CALL_INFO(call) \
601 	Z_TYPE_INFO((call)->This)
602 
603 #define ZEND_CALL_KIND_EX(call_info) \
604 	(call_info & (ZEND_CALL_CODE | ZEND_CALL_TOP))
605 
606 #define ZEND_CALL_KIND(call) \
607 	ZEND_CALL_KIND_EX(ZEND_CALL_INFO(call))
608 
609 #define ZEND_ADD_CALL_FLAG_EX(call_info, flag) do { \
610 		call_info |= (flag); \
611 	} while (0)
612 
613 #define ZEND_DEL_CALL_FLAG_EX(call_info, flag) do { \
614 		call_info &= ~(flag); \
615 	} while (0)
616 
617 #define ZEND_ADD_CALL_FLAG(call, flag) do { \
618 		ZEND_ADD_CALL_FLAG_EX(Z_TYPE_INFO((call)->This), flag); \
619 	} while (0)
620 
621 #define ZEND_DEL_CALL_FLAG(call, flag) do { \
622 		ZEND_DEL_CALL_FLAG_EX(Z_TYPE_INFO((call)->This), flag); \
623 	} while (0)
624 
625 #define ZEND_CALL_NUM_ARGS(call) \
626 	(call)->This.u2.num_args
627 
628 /* Ensure the correct alignment before slots calculation */
629 ZEND_STATIC_ASSERT(ZEND_MM_ALIGNED_SIZE(sizeof(zval)) == sizeof(zval),
630                    "zval must be aligned by ZEND_MM_ALIGNMENT");
631 /* A number of call frame slots (zvals) reserved for zend_execute_data. */
632 #define ZEND_CALL_FRAME_SLOT \
633 	((int)((sizeof(zend_execute_data) + sizeof(zval) - 1) / sizeof(zval)))
634 
635 #define ZEND_CALL_VAR(call, n) \
636 	((zval*)(((char*)(call)) + ((int)(n))))
637 
638 #define ZEND_CALL_VAR_NUM(call, n) \
639 	(((zval*)(call)) + (ZEND_CALL_FRAME_SLOT + ((int)(n))))
640 
641 #define ZEND_CALL_ARG(call, n) \
642 	ZEND_CALL_VAR_NUM(call, ((int)(n)) - 1)
643 
644 #define EX(element) 			((execute_data)->element)
645 
646 #define EX_CALL_INFO()			ZEND_CALL_INFO(execute_data)
647 #define EX_CALL_KIND()			ZEND_CALL_KIND(execute_data)
648 #define EX_NUM_ARGS()			ZEND_CALL_NUM_ARGS(execute_data)
649 
650 #define ZEND_CALL_USES_STRICT_TYPES(call) \
651 	(((call)->func->common.fn_flags & ZEND_ACC_STRICT_TYPES) != 0)
652 
653 #define EX_USES_STRICT_TYPES() \
654 	ZEND_CALL_USES_STRICT_TYPES(execute_data)
655 
656 #define ZEND_ARG_USES_STRICT_TYPES() \
657 	(EG(current_execute_data)->prev_execute_data && \
658 	 EG(current_execute_data)->prev_execute_data->func && \
659 	 ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data)->prev_execute_data))
660 
661 #define ZEND_FLF_ARG_USES_STRICT_TYPES() \
662 	(EG(current_execute_data) && \
663 	 EG(current_execute_data)->func && \
664 	 ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data)))
665 
666 #define ZEND_RET_USES_STRICT_TYPES() \
667 	ZEND_CALL_USES_STRICT_TYPES(EG(current_execute_data))
668 
669 #define EX_VAR(n)				ZEND_CALL_VAR(execute_data, n)
670 #define EX_VAR_NUM(n)			ZEND_CALL_VAR_NUM(execute_data, n)
671 
672 #define EX_VAR_TO_NUM(n)		((uint32_t)((n) / sizeof(zval) - ZEND_CALL_FRAME_SLOT))
673 #define EX_NUM_TO_VAR(n)		((uint32_t)(((n) + ZEND_CALL_FRAME_SLOT) * sizeof(zval)))
674 
675 #define ZEND_OPLINE_TO_OFFSET(opline, target) \
676 	((char*)(target) - (char*)(opline))
677 
678 #define ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, opline_num) \
679 	((char*)&(op_array)->opcodes[opline_num] - (char*)(opline))
680 
681 #define ZEND_OFFSET_TO_OPLINE(base, offset) \
682 	((zend_op*)(((char*)(base)) + (int)offset))
683 
684 #define ZEND_OFFSET_TO_OPLINE_NUM(op_array, base, offset) \
685 	(ZEND_OFFSET_TO_OPLINE(base, offset) - op_array->opcodes)
686 
687 #if ZEND_USE_ABS_JMP_ADDR
688 
689 /* run-time jump target */
690 # define OP_JMP_ADDR(opline, node) \
691 	(node).jmp_addr
692 
693 # define ZEND_SET_OP_JMP_ADDR(opline, node, val) do { \
694 		(node).jmp_addr = (val); \
695 	} while (0)
696 
697 /* convert jump target from compile-time to run-time */
698 # define ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, node) do { \
699 		(node).jmp_addr = (op_array)->opcodes + (node).opline_num; \
700 	} while (0)
701 
702 /* convert jump target back from run-time to compile-time */
703 # define ZEND_PASS_TWO_UNDO_JMP_TARGET(op_array, opline, node) do { \
704 		(node).opline_num = (node).jmp_addr - (op_array)->opcodes; \
705 	} while (0)
706 
707 #else
708 
709 /* run-time jump target */
710 # define OP_JMP_ADDR(opline, node) \
711 	ZEND_OFFSET_TO_OPLINE(opline, (node).jmp_offset)
712 
713 # define ZEND_SET_OP_JMP_ADDR(opline, node, val) do { \
714 		(node).jmp_offset = ZEND_OPLINE_TO_OFFSET(opline, val); \
715 	} while (0)
716 
717 /* convert jump target from compile-time to run-time */
718 # define ZEND_PASS_TWO_UPDATE_JMP_TARGET(op_array, opline, node) do { \
719 		(node).jmp_offset = ZEND_OPLINE_NUM_TO_OFFSET(op_array, opline, (node).opline_num); \
720 	} while (0)
721 
722 /* convert jump target back from run-time to compile-time */
723 # define ZEND_PASS_TWO_UNDO_JMP_TARGET(op_array, opline, node) do { \
724 		(node).opline_num = ZEND_OFFSET_TO_OPLINE_NUM(op_array, opline, (node).jmp_offset); \
725 	} while (0)
726 
727 #endif
728 
729 /* constant-time constant */
730 # define CT_CONSTANT_EX(op_array, num) \
731 	((op_array)->literals + (num))
732 
733 # define CT_CONSTANT(node) \
734 	CT_CONSTANT_EX(CG(active_op_array), (node).constant)
735 
736 #if ZEND_USE_ABS_CONST_ADDR
737 
738 /* run-time constant */
739 # define RT_CONSTANT(opline, node) \
740 	(node).zv
741 
742 /* convert constant from compile-time to run-time */
743 # define ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, node) do { \
744 		(node).zv = CT_CONSTANT_EX(op_array, (node).constant); \
745 	} while (0)
746 
747 #else
748 
749 /* At run-time, constants are allocated together with op_array->opcodes
750  * and addressed relatively to current opline.
751  */
752 
753 /* run-time constant */
754 # define RT_CONSTANT(opline, node) \
755 	((zval*)(((char*)(opline)) + (int32_t)(node).constant))
756 
757 /* convert constant from compile-time to run-time */
758 # define ZEND_PASS_TWO_UPDATE_CONSTANT(op_array, opline, node) do { \
759 		(node).constant = \
760 			(((char*)CT_CONSTANT_EX(op_array, (node).constant)) - \
761 			((char*)opline)); \
762 	} while (0)
763 
764 #endif
765 
766 /* convert constant back from run-time to compile-time */
767 #define ZEND_PASS_TWO_UNDO_CONSTANT(op_array, opline, node) do { \
768 		(node).constant = RT_CONSTANT(opline, node) - (op_array)->literals; \
769 	} while (0)
770 
771 #define RUN_TIME_CACHE(op_array) \
772 	ZEND_MAP_PTR_GET((op_array)->run_time_cache)
773 
774 #define ZEND_OP_ARRAY_EXTENSION(op_array, handle) \
775 	((void**)RUN_TIME_CACHE(op_array))[handle]
776 
777 #define IS_UNUSED	0		/* Unused operand */
778 #define IS_CONST	(1<<0)
779 #define IS_TMP_VAR	(1<<1)
780 #define IS_VAR		(1<<2)
781 #define IS_CV		(1<<3)	/* Compiled variable */
782 
783 /* Used for result.type of smart branch instructions */
784 #define IS_SMART_BRANCH_JMPZ  (1<<4)
785 #define IS_SMART_BRANCH_JMPNZ (1<<5)
786 
787 #define ZEND_EXTRA_VALUE 1
788 
789 #include "zend_globals.h"
790 
791 typedef enum _zend_compile_position {
792 	ZEND_COMPILE_POSITION_AT_SHEBANG = 0,
793 	ZEND_COMPILE_POSITION_AT_OPEN_TAG,
794 	ZEND_COMPILE_POSITION_AFTER_OPEN_TAG
795 } zend_compile_position;
796 
797 BEGIN_EXTERN_C()
798 
799 void init_compiler(void);
800 void shutdown_compiler(void);
801 void zend_init_compiler_data_structures(void);
802 
803 void zend_oparray_context_begin(zend_oparray_context *prev_context);
804 void zend_oparray_context_end(zend_oparray_context *prev_context);
805 void zend_file_context_begin(zend_file_context *prev_context);
806 void zend_file_context_end(zend_file_context *prev_context);
807 
808 extern ZEND_API zend_op_array *(*zend_compile_file)(zend_file_handle *file_handle, int type);
809 extern ZEND_API zend_op_array *(*zend_compile_string)(zend_string *source_string, const char *filename, zend_compile_position position);
810 
811 ZEND_API int ZEND_FASTCALL lex_scan(zval *zendlval, zend_parser_stack_elem *elem);
812 void startup_scanner(void);
813 void shutdown_scanner(void);
814 
815 ZEND_API zend_string *zend_set_compiled_filename(zend_string *new_compiled_filename);
816 ZEND_API void zend_restore_compiled_filename(zend_string *original_compiled_filename);
817 ZEND_API zend_string *zend_get_compiled_filename(void);
818 ZEND_API int zend_get_compiled_lineno(void);
819 ZEND_API size_t zend_get_scanned_file_offset(void);
820 
821 ZEND_API zend_string *zend_get_compiled_variable_name(const zend_op_array *op_array, uint32_t var);
822 
823 #ifdef ZTS
824 const char *zend_get_zendtext(void);
825 int zend_get_zendleng(void);
826 #endif
827 
828 typedef zend_result (ZEND_FASTCALL *unary_op_type)(zval *, zval *);
829 typedef zend_result (ZEND_FASTCALL *binary_op_type)(zval *, zval *, zval *);
830 
831 ZEND_API unary_op_type get_unary_op(int opcode);
832 ZEND_API binary_op_type get_binary_op(int opcode);
833 
834 void zend_stop_lexing(void);
835 void zend_emit_final_return(bool return_one);
836 
837 typedef enum {
838 	ZEND_MODIFIER_TARGET_PROPERTY = 0,
839 	ZEND_MODIFIER_TARGET_METHOD,
840 	ZEND_MODIFIER_TARGET_CONSTANT,
841 	ZEND_MODIFIER_TARGET_CPP,
842 } zend_modifier_target;
843 
844 /* Used during AST construction */
845 zend_ast *zend_ast_append_str(zend_ast *left, zend_ast *right);
846 zend_ast *zend_negate_num_string(zend_ast *ast);
847 uint32_t zend_add_class_modifier(uint32_t flags, uint32_t new_flag);
848 uint32_t zend_add_anonymous_class_modifier(uint32_t flags, uint32_t new_flag);
849 uint32_t zend_add_member_modifier(uint32_t flags, uint32_t new_flag, zend_modifier_target target);
850 
851 uint32_t zend_modifier_token_to_flag(zend_modifier_target target, uint32_t flags);
852 uint32_t zend_modifier_list_to_flags(zend_modifier_target target, zend_ast *modifiers);
853 
854 bool zend_handle_encoding_declaration(zend_ast *ast);
855 
856 ZEND_API zend_class_entry *zend_bind_class_in_slot(
857 		zval *class_table_slot, zval *lcname, zend_string *lc_parent_name);
858 ZEND_API zend_result do_bind_function(zend_function *func, zval *lcname);
859 ZEND_API zend_result do_bind_class(zval *lcname, zend_string *lc_parent_name);
860 
861 void zend_resolve_goto_label(zend_op_array *op_array, zend_op *opline);
862 
863 ZEND_API void function_add_ref(zend_function *function);
864 zend_string *zval_make_interned_string(zval *zv);
865 
866 #define INITIAL_OP_ARRAY_SIZE 64
867 
868 
869 /* helper functions in zend_language_scanner.l */
870 struct _zend_arena;
871 
872 ZEND_API zend_op_array *compile_file(zend_file_handle *file_handle, int type);
873 ZEND_API zend_op_array *compile_string(zend_string *source_string, const char *filename, zend_compile_position position);
874 ZEND_API zend_op_array *compile_filename(int type, zend_string *filename);
875 ZEND_API zend_ast *zend_compile_string_to_ast(
876 		zend_string *code, struct _zend_arena **ast_arena, zend_string *filename);
877 ZEND_API zend_result zend_execute_scripts(int type, zval *retval, int file_count, ...);
878 ZEND_API zend_result zend_execute_script(int type, zval *retval, zend_file_handle *file_handle);
879 ZEND_API zend_result open_file_for_scanning(zend_file_handle *file_handle);
880 ZEND_API void init_op_array(zend_op_array *op_array, uint8_t type, int initial_ops_size);
881 ZEND_API void destroy_op_array(zend_op_array *op_array);
882 ZEND_API void zend_destroy_static_vars(zend_op_array *op_array);
883 ZEND_API void zend_destroy_file_handle(zend_file_handle *file_handle);
884 ZEND_API void zend_cleanup_mutable_class_data(zend_class_entry *ce);
885 ZEND_API void zend_cleanup_internal_class_data(zend_class_entry *ce);
886 ZEND_API void zend_type_release(zend_type type, bool persistent);
887 ZEND_API zend_string *zend_create_member_string(zend_string *class_name, zend_string *member_name);
888 
889 
890 ZEND_API ZEND_COLD void zend_user_exception_handler(void);
891 
892 #define zend_try_exception_handler() do { \
893 		if (UNEXPECTED(EG(exception))) { \
894 			if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) { \
895 				zend_user_exception_handler(); \
896 			} \
897 		} \
898 	} while (0)
899 
900 void zend_free_internal_arg_info(zend_internal_function *function);
901 ZEND_API void destroy_zend_function(zend_function *function);
902 ZEND_API void zend_function_dtor(zval *zv);
903 ZEND_API void destroy_zend_class(zval *zv);
904 void zend_class_add_ref(zval *zv);
905 
906 ZEND_API zend_string *zend_mangle_property_name(const char *src1, size_t src1_length, const char *src2, size_t src2_length, bool internal);
907 #define zend_unmangle_property_name(mangled_property, class_name, prop_name) \
908         zend_unmangle_property_name_ex(mangled_property, class_name, prop_name, NULL)
909 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);
910 
zend_get_unmangled_property_name(const zend_string * mangled_prop)911 static zend_always_inline const char *zend_get_unmangled_property_name(const zend_string *mangled_prop) {
912 	const char *class_name, *prop_name;
913 	zend_unmangle_property_name(mangled_prop, &class_name, &prop_name);
914 	return prop_name;
915 }
916 
917 #define ZEND_FUNCTION_DTOR zend_function_dtor
918 #define ZEND_CLASS_DTOR destroy_zend_class
919 
920 typedef bool (*zend_needs_live_range_cb)(zend_op_array *op_array, zend_op *opline);
921 ZEND_API void zend_recalc_live_ranges(
922 	zend_op_array *op_array, zend_needs_live_range_cb needs_live_range);
923 
924 ZEND_API void pass_two(zend_op_array *op_array);
925 ZEND_API bool zend_is_compiling(void);
926 ZEND_API char *zend_make_compiled_string_description(const char *name);
927 ZEND_API void zend_initialize_class_data(zend_class_entry *ce, bool nullify_handlers);
928 uint32_t zend_get_class_fetch_type(const zend_string *name);
929 ZEND_API uint8_t zend_get_call_op(const zend_op *init_op, zend_function *fbc);
930 ZEND_API bool zend_is_smart_branch(const zend_op *opline);
931 
932 typedef bool (*zend_auto_global_callback)(zend_string *name);
933 typedef struct _zend_auto_global {
934 	zend_string *name;
935 	zend_auto_global_callback auto_global_callback;
936 	bool jit;
937 	bool armed;
938 } zend_auto_global;
939 
940 ZEND_API zend_result zend_register_auto_global(zend_string *name, bool jit, zend_auto_global_callback auto_global_callback);
941 ZEND_API void zend_activate_auto_globals(void);
942 ZEND_API bool zend_is_auto_global(zend_string *name);
943 ZEND_API bool zend_is_auto_global_str(const char *name, size_t len);
944 ZEND_API size_t zend_dirname(char *path, size_t len);
945 ZEND_API void zend_set_function_arg_flags(zend_function *func);
946 
947 int ZEND_FASTCALL zendlex(zend_parser_stack_elem *elem);
948 
949 void zend_assert_valid_class_name(const zend_string *const_name);
950 
951 zend_string *zend_type_to_string_resolved(zend_type type, zend_class_entry *scope);
952 ZEND_API zend_string *zend_type_to_string(zend_type type);
953 
954 /* BEGIN: OPCODES */
955 
956 #include "zend_vm_opcodes.h"
957 
958 /* END: OPCODES */
959 
960 /* class fetches */
961 #define ZEND_FETCH_CLASS_DEFAULT	0
962 #define ZEND_FETCH_CLASS_SELF		1
963 #define ZEND_FETCH_CLASS_PARENT		2
964 #define ZEND_FETCH_CLASS_STATIC		3
965 #define ZEND_FETCH_CLASS_AUTO		4
966 #define ZEND_FETCH_CLASS_INTERFACE	5
967 #define ZEND_FETCH_CLASS_TRAIT		6
968 #define ZEND_FETCH_CLASS_MASK        0x0f
969 #define ZEND_FETCH_CLASS_NO_AUTOLOAD 0x80
970 #define ZEND_FETCH_CLASS_SILENT      0x0100
971 #define ZEND_FETCH_CLASS_EXCEPTION   0x0200
972 #define ZEND_FETCH_CLASS_ALLOW_UNLINKED 0x0400
973 #define ZEND_FETCH_CLASS_ALLOW_NEARLY_LINKED 0x0800
974 
975 /* These should not clash with ZEND_ACC_(PUBLIC|PROTECTED|PRIVATE) */
976 #define ZEND_PARAM_REF      (1<<3)
977 #define ZEND_PARAM_VARIADIC (1<<4)
978 
979 #define ZEND_NAME_FQ       0
980 #define ZEND_NAME_NOT_FQ   1
981 #define ZEND_NAME_RELATIVE 2
982 
983 /* ZEND_FETCH_ flags in class name AST of new const expression must not clash with ZEND_NAME_ flags */
984 #define ZEND_CONST_EXPR_NEW_FETCH_TYPE_SHIFT 2
985 
986 #define ZEND_TYPE_NULLABLE (1<<8)
987 
988 #define ZEND_ARRAY_SYNTAX_LIST 1  /* list() */
989 #define ZEND_ARRAY_SYNTAX_LONG 2  /* array() */
990 #define ZEND_ARRAY_SYNTAX_SHORT 3 /* [] */
991 
992 /* var status for backpatching */
993 #define BP_VAR_R			0
994 #define BP_VAR_W			1
995 #define BP_VAR_RW			2
996 #define BP_VAR_IS			3
997 #define BP_VAR_FUNC_ARG		4
998 #define BP_VAR_UNSET		5
999 
1000 #define ZEND_INTERNAL_FUNCTION		1
1001 #define ZEND_USER_FUNCTION			2
1002 #define ZEND_EVAL_CODE				4
1003 
1004 #define ZEND_USER_CODE(type)		((type) != ZEND_INTERNAL_FUNCTION)
1005 
1006 #define ZEND_INTERNAL_CLASS         1
1007 #define ZEND_USER_CLASS             2
1008 
1009 #define ZEND_EVAL				(1<<0)
1010 #define ZEND_INCLUDE			(1<<1)
1011 #define ZEND_INCLUDE_ONCE		(1<<2)
1012 #define ZEND_REQUIRE			(1<<3)
1013 #define ZEND_REQUIRE_ONCE		(1<<4)
1014 
1015 /* global/local fetches */
1016 #define ZEND_FETCH_GLOBAL		(1<<1)
1017 #define ZEND_FETCH_LOCAL		(1<<2)
1018 #define ZEND_FETCH_GLOBAL_LOCK	(1<<3)
1019 
1020 #define ZEND_FETCH_TYPE_MASK	0xe
1021 
1022 /* Only one of these can ever be in use */
1023 #define ZEND_FETCH_REF			1
1024 #define ZEND_FETCH_DIM_WRITE	2
1025 #define ZEND_FETCH_OBJ_FLAGS	3
1026 
1027 /* Used to mark what kind of operation a writing FETCH_DIM is used in,
1028  * to produce a more precise error on incorrect string offset use. */
1029 #define ZEND_FETCH_DIM_REF 1
1030 #define ZEND_FETCH_DIM_DIM 2
1031 #define ZEND_FETCH_DIM_OBJ 3
1032 #define ZEND_FETCH_DIM_INCDEC 4
1033 
1034 #define ZEND_ISEMPTY			(1<<0)
1035 
1036 #define ZEND_LAST_CATCH			(1<<0)
1037 
1038 #define ZEND_FREE_ON_RETURN     (1<<0)
1039 #define ZEND_FREE_SWITCH        (1<<1)
1040 
1041 #define ZEND_SEND_BY_VAL     0u
1042 #define ZEND_SEND_BY_REF     1u
1043 #define ZEND_SEND_PREFER_REF 2u
1044 
1045 #define ZEND_THROW_IS_EXPR 1u
1046 
1047 #define ZEND_FCALL_MAY_HAVE_EXTRA_NAMED_PARAMS 1
1048 
1049 /* The send mode, the is_variadic, the is_promoted, and the is_tentative flags are stored as part of zend_type */
1050 #define _ZEND_SEND_MODE_SHIFT _ZEND_TYPE_EXTRA_FLAGS_SHIFT
1051 #define _ZEND_IS_VARIADIC_BIT (1 << (_ZEND_TYPE_EXTRA_FLAGS_SHIFT + 2))
1052 #define _ZEND_IS_PROMOTED_BIT (1 << (_ZEND_TYPE_EXTRA_FLAGS_SHIFT + 3))
1053 #define _ZEND_IS_TENTATIVE_BIT (1 << (_ZEND_TYPE_EXTRA_FLAGS_SHIFT + 4))
1054 #define ZEND_ARG_SEND_MODE(arg_info) \
1055 	((ZEND_TYPE_FULL_MASK((arg_info)->type) >> _ZEND_SEND_MODE_SHIFT) & 3)
1056 #define ZEND_ARG_IS_VARIADIC(arg_info) \
1057 	((ZEND_TYPE_FULL_MASK((arg_info)->type) & _ZEND_IS_VARIADIC_BIT) != 0)
1058 #define ZEND_ARG_IS_PROMOTED(arg_info) \
1059 	((ZEND_TYPE_FULL_MASK((arg_info)->type) & _ZEND_IS_PROMOTED_BIT) != 0)
1060 #define ZEND_ARG_TYPE_IS_TENTATIVE(arg_info) \
1061 	((ZEND_TYPE_FULL_MASK((arg_info)->type) & _ZEND_IS_TENTATIVE_BIT) != 0)
1062 
1063 #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. */
1064 #define ZEND_DIM_ALTERNATIVE_SYNTAX	(1 << 1) /* deprecated curly brace usage */
1065 
1066 /* Attributes for ${} encaps var in strings (ZEND_AST_DIM or ZEND_AST_VAR node) */
1067 /* ZEND_AST_VAR nodes can have any of the ZEND_ENCAPS_VAR_* flags */
1068 /* 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). */
1069 #define ZEND_ENCAPS_VAR_DOLLAR_CURLY (1 << 0)
1070 #define ZEND_ENCAPS_VAR_DOLLAR_CURLY_VAR_VAR (1 << 1)
1071 
1072 /* Make sure these don't clash with ZEND_FETCH_CLASS_* flags. */
1073 #define IS_CONSTANT_CLASS                    0x400 /* __CLASS__ in trait */
1074 #define IS_CONSTANT_UNQUALIFIED_IN_NAMESPACE 0x800
1075 
zend_check_arg_send_type(const zend_function * zf,uint32_t arg_num,uint32_t mask)1076 static zend_always_inline bool zend_check_arg_send_type(const zend_function *zf, uint32_t arg_num, uint32_t mask)
1077 {
1078 	arg_num--;
1079 	if (UNEXPECTED(arg_num >= zf->common.num_args)) {
1080 		if (EXPECTED((zf->common.fn_flags & ZEND_ACC_VARIADIC) == 0)) {
1081 			return 0;
1082 		}
1083 		arg_num = zf->common.num_args;
1084 	}
1085 	return UNEXPECTED((ZEND_ARG_SEND_MODE(&zf->common.arg_info[arg_num]) & mask) != 0);
1086 }
1087 
1088 #define ARG_MUST_BE_SENT_BY_REF(zf, arg_num) \
1089 	zend_check_arg_send_type(zf, arg_num, ZEND_SEND_BY_REF)
1090 
1091 #define ARG_SHOULD_BE_SENT_BY_REF(zf, arg_num) \
1092 	zend_check_arg_send_type(zf, arg_num, ZEND_SEND_BY_REF|ZEND_SEND_PREFER_REF)
1093 
1094 #define ARG_MAY_BE_SENT_BY_REF(zf, arg_num) \
1095 	zend_check_arg_send_type(zf, arg_num, ZEND_SEND_PREFER_REF)
1096 
1097 /* Quick API to check first 12 arguments */
1098 #define MAX_ARG_FLAG_NUM 12
1099 
1100 #ifdef WORDS_BIGENDIAN
1101 # define ZEND_SET_ARG_FLAG(zf, arg_num, mask) do { \
1102 		(zf)->quick_arg_flags |= ((mask) << ((arg_num) - 1) * 2); \
1103 	} while (0)
1104 # define ZEND_CHECK_ARG_FLAG(zf, arg_num, mask) \
1105 	(((zf)->quick_arg_flags >> (((arg_num) - 1) * 2)) & (mask))
1106 #else
1107 # define ZEND_SET_ARG_FLAG(zf, arg_num, mask) do { \
1108 		(zf)->quick_arg_flags |= (((mask) << 6) << (arg_num) * 2); \
1109 	} while (0)
1110 # define ZEND_CHECK_ARG_FLAG(zf, arg_num, mask) \
1111 	(((zf)->quick_arg_flags >> (((arg_num) + 3) * 2)) & (mask))
1112 #endif
1113 
1114 #define QUICK_ARG_MUST_BE_SENT_BY_REF(zf, arg_num) \
1115 	ZEND_CHECK_ARG_FLAG(zf, arg_num, ZEND_SEND_BY_REF)
1116 
1117 #define QUICK_ARG_SHOULD_BE_SENT_BY_REF(zf, arg_num) \
1118 	ZEND_CHECK_ARG_FLAG(zf, arg_num, ZEND_SEND_BY_REF|ZEND_SEND_PREFER_REF)
1119 
1120 #define QUICK_ARG_MAY_BE_SENT_BY_REF(zf, arg_num) \
1121 	ZEND_CHECK_ARG_FLAG(zf, arg_num, ZEND_SEND_PREFER_REF)
1122 
1123 #define ZEND_RETURN_VAL 0
1124 #define ZEND_RETURN_REF 1
1125 
1126 #define ZEND_BIND_VAL      0
1127 #define ZEND_BIND_REF      1
1128 #define ZEND_BIND_IMPLICIT 2
1129 #define ZEND_BIND_EXPLICIT 4
1130 
1131 #define ZEND_RETURNS_FUNCTION (1<<0)
1132 #define ZEND_RETURNS_VALUE    (1<<1)
1133 
1134 #define ZEND_ARRAY_ELEMENT_REF		(1<<0)
1135 #define ZEND_ARRAY_NOT_PACKED		(1<<1)
1136 #define ZEND_ARRAY_SIZE_SHIFT		2
1137 
1138 /* Attribute for ternary inside parentheses */
1139 #define ZEND_PARENTHESIZED_CONDITIONAL 1
1140 
1141 /* For "use" AST nodes and the seen symbol table */
1142 #define ZEND_SYMBOL_CLASS    (1<<0)
1143 #define ZEND_SYMBOL_FUNCTION (1<<1)
1144 #define ZEND_SYMBOL_CONST    (1<<2)
1145 
1146 /* All increment opcodes are even (decrement are odd) */
1147 #define ZEND_IS_INCREMENT(opcode) (((opcode) & 1) == 0)
1148 
1149 #define ZEND_IS_BINARY_ASSIGN_OP_OPCODE(opcode) \
1150 	(((opcode) >= ZEND_ADD) && ((opcode) <= ZEND_POW))
1151 
1152 /* Pseudo-opcodes that are used only temporarily during compilation */
1153 #define ZEND_GOTO  253
1154 #define ZEND_BRK   254
1155 #define ZEND_CONT  255
1156 
1157 
1158 END_EXTERN_C()
1159 
1160 #define ZEND_CLONE_FUNC_NAME		"__clone"
1161 #define ZEND_CONSTRUCTOR_FUNC_NAME	"__construct"
1162 #define ZEND_DESTRUCTOR_FUNC_NAME	"__destruct"
1163 #define ZEND_GET_FUNC_NAME          "__get"
1164 #define ZEND_SET_FUNC_NAME          "__set"
1165 #define ZEND_UNSET_FUNC_NAME        "__unset"
1166 #define ZEND_ISSET_FUNC_NAME        "__isset"
1167 #define ZEND_CALL_FUNC_NAME         "__call"
1168 #define ZEND_CALLSTATIC_FUNC_NAME   "__callstatic"
1169 #define ZEND_TOSTRING_FUNC_NAME     "__tostring"
1170 #define ZEND_INVOKE_FUNC_NAME       "__invoke"
1171 #define ZEND_DEBUGINFO_FUNC_NAME    "__debuginfo"
1172 
1173 /* The following constants may be combined in CG(compiler_options)
1174  * to change the default compiler behavior */
1175 
1176 /* generate extended debug information */
1177 #define ZEND_COMPILE_EXTENDED_STMT              (1<<0)
1178 #define ZEND_COMPILE_EXTENDED_FCALL             (1<<1)
1179 #define ZEND_COMPILE_EXTENDED_INFO              (ZEND_COMPILE_EXTENDED_STMT|ZEND_COMPILE_EXTENDED_FCALL)
1180 
1181 /* call op_array handler of extensions */
1182 #define ZEND_COMPILE_HANDLE_OP_ARRAY            (1<<2)
1183 
1184 /* generate ZEND_INIT_FCALL_BY_NAME for internal functions instead of ZEND_INIT_FCALL */
1185 #define ZEND_COMPILE_IGNORE_INTERNAL_FUNCTIONS  (1<<3)
1186 
1187 /* don't perform early binding for classes inherited form internal ones;
1188  * in namespaces assume that internal class that doesn't exist at compile-time
1189  * may appear in run-time */
1190 #define ZEND_COMPILE_IGNORE_INTERNAL_CLASSES    (1<<4)
1191 
1192 /* generate ZEND_DECLARE_CLASS_DELAYED opcode to delay early binding */
1193 #define ZEND_COMPILE_DELAYED_BINDING            (1<<5)
1194 
1195 /* disable constant substitution at compile-time */
1196 #define ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION   (1<<6)
1197 
1198 /* disable substitution of persistent constants at compile-time */
1199 #define ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION	(1<<8)
1200 
1201 /* generate ZEND_INIT_FCALL_BY_NAME for userland functions instead of ZEND_INIT_FCALL */
1202 #define ZEND_COMPILE_IGNORE_USER_FUNCTIONS      (1<<9)
1203 
1204 /* force ZEND_ACC_USE_GUARDS for all classes */
1205 #define ZEND_COMPILE_GUARDS						(1<<10)
1206 
1207 /* disable builtin special case function calls */
1208 #define ZEND_COMPILE_NO_BUILTINS				(1<<11)
1209 
1210 /* result of compilation may be stored in file cache */
1211 #define ZEND_COMPILE_WITH_FILE_CACHE			(1<<12)
1212 
1213 /* ignore functions and classes declared in other files */
1214 #define ZEND_COMPILE_IGNORE_OTHER_FILES			(1<<13)
1215 
1216 /* this flag is set when compiler invoked by opcache_compile_file() */
1217 #define ZEND_COMPILE_WITHOUT_EXECUTION          (1<<14)
1218 
1219 /* this flag is set when compiler invoked during preloading */
1220 #define ZEND_COMPILE_PRELOAD                    (1<<15)
1221 
1222 /* disable jumptable optimization for switch statements */
1223 #define ZEND_COMPILE_NO_JUMPTABLES				(1<<16)
1224 
1225 /* this flag is set when compiler invoked during preloading in separate process */
1226 #define ZEND_COMPILE_PRELOAD_IN_CHILD           (1<<17)
1227 
1228 /* ignore observer notifications, e.g. to manually notify afterwards in a post-processing step after compilation */
1229 #define ZEND_COMPILE_IGNORE_OBSERVER			(1<<18)
1230 
1231 /* The default value for CG(compiler_options) */
1232 #define ZEND_COMPILE_DEFAULT					ZEND_COMPILE_HANDLE_OP_ARRAY
1233 
1234 /* The default value for CG(compiler_options) during eval() */
1235 #define ZEND_COMPILE_DEFAULT_FOR_EVAL			0
1236 
1237 ZEND_API bool zend_is_op_long_compatible(const zval *op);
1238 ZEND_API bool zend_binary_op_produces_error(uint32_t opcode, const zval *op1, const zval *op2);
1239 ZEND_API bool zend_unary_op_produces_error(uint32_t opcode, const zval *op);
1240 
1241 #endif /* ZEND_COMPILE_H */
1242