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