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