xref: /php-src/Zend/zend_types.h (revision fd1d5ec2)
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    |          Dmitry Stogov <dmitry@php.net>                              |
18    |          Xinchen Hui <laruence@php.net>                              |
19    +----------------------------------------------------------------------+
20 */
21 
22 #ifndef ZEND_TYPES_H
23 #define ZEND_TYPES_H
24 
25 #include "zend_portability.h"
26 #include "zend_long.h"
27 #include <stdbool.h>
28 
29 #ifdef __SSE2__
30 # include <mmintrin.h>
31 # include <emmintrin.h>
32 #endif
33 
34 #ifdef WORDS_BIGENDIAN
35 # define ZEND_ENDIAN_LOHI(lo, hi)          hi; lo;
36 # define ZEND_ENDIAN_LOHI_3(lo, mi, hi)    hi; mi; lo;
37 # define ZEND_ENDIAN_LOHI_4(a, b, c, d)    d; c; b; a;
38 # define ZEND_ENDIAN_LOHI_C(lo, hi)        hi, lo
39 # define ZEND_ENDIAN_LOHI_C_3(lo, mi, hi)  hi, mi, lo,
40 # define ZEND_ENDIAN_LOHI_C_4(a, b, c, d)  d, c, b, a
41 #else
42 # define ZEND_ENDIAN_LOHI(lo, hi)          lo; hi;
43 # define ZEND_ENDIAN_LOHI_3(lo, mi, hi)    lo; mi; hi;
44 # define ZEND_ENDIAN_LOHI_4(a, b, c, d)    a; b; c; d;
45 # define ZEND_ENDIAN_LOHI_C(lo, hi)        lo, hi
46 # define ZEND_ENDIAN_LOHI_C_3(lo, mi, hi)  lo, mi, hi,
47 # define ZEND_ENDIAN_LOHI_C_4(a, b, c, d)  a, b, c, d
48 #endif
49 
50 typedef bool zend_bool;
51 typedef unsigned char zend_uchar;
52 
53 typedef enum {
54   SUCCESS =  0,
55   FAILURE = -1,		/* this MUST stay a negative number, or it may affect functions! */
56 } ZEND_RESULT_CODE;
57 
58 typedef ZEND_RESULT_CODE zend_result;
59 
60 #ifdef ZEND_ENABLE_ZVAL_LONG64
61 # ifdef ZEND_WIN32
62 #  define ZEND_SIZE_MAX  _UI64_MAX
63 # else
64 #  define ZEND_SIZE_MAX  SIZE_MAX
65 # endif
66 #else
67 # if defined(ZEND_WIN32)
68 #  define ZEND_SIZE_MAX  _UI32_MAX
69 # else
70 #  define ZEND_SIZE_MAX SIZE_MAX
71 # endif
72 #endif
73 
74 typedef intptr_t zend_intptr_t;
75 typedef uintptr_t zend_uintptr_t;
76 
77 #ifdef ZTS
78 #define ZEND_TLS static TSRM_TLS
79 #define ZEND_EXT_TLS TSRM_TLS
80 #else
81 #define ZEND_TLS static
82 #define ZEND_EXT_TLS
83 #endif
84 
85 typedef struct _zend_object_handlers zend_object_handlers;
86 typedef struct _zend_class_entry     zend_class_entry;
87 typedef union  _zend_function        zend_function;
88 typedef struct _zend_execute_data    zend_execute_data;
89 
90 typedef struct _zval_struct     zval;
91 
92 typedef struct _zend_refcounted zend_refcounted;
93 typedef struct _zend_string     zend_string;
94 typedef struct _zend_array      zend_array;
95 typedef struct _zend_object     zend_object;
96 typedef struct _zend_resource   zend_resource;
97 typedef struct _zend_reference  zend_reference;
98 typedef struct _zend_ast_ref    zend_ast_ref;
99 typedef struct _zend_ast        zend_ast;
100 
101 typedef int  (*compare_func_t)(const void *, const void *);
102 typedef void (*swap_func_t)(void *, void *);
103 typedef void (*sort_func_t)(void *, size_t, size_t, compare_func_t, swap_func_t);
104 typedef void (*dtor_func_t)(zval *pDest);
105 typedef void (*copy_ctor_func_t)(zval *pElement);
106 
107 /*
108  * zend_type - is an abstraction layer to represent information about type hint.
109  * It shouldn't be used directly. Only through ZEND_TYPE_* macros.
110  *
111  * ZEND_TYPE_IS_SET()        - checks if there is a type-hint
112  * ZEND_TYPE_HAS_ONLY_MASK() - checks if type-hint refer to standard type only
113  * ZEND_TYPE_HAS_CLASS()     - checks if type-hint contains some class
114  * ZEND_TYPE_HAS_CE()        - checks if type-hint contains some class as zend_class_entry *
115  * ZEND_TYPE_HAS_NAME()      - checks if type-hint contains some class as zend_string *
116  *
117  * ZEND_TYPE_NAME()       - returns referenced class name
118  * ZEND_TYPE_CE()         - returns referenced class entry
119  * ZEND_TYPE_PURE_MASK()  - returns MAY_BE_* type mask
120  * ZEND_TYPE_FULL_MASK()  - returns MAY_BE_* type mask together with other flags
121  *
122  * ZEND_TYPE_ALLOW_NULL() - checks if NULL is allowed
123  *
124  * ZEND_TYPE_INIT_*() should be used for construction.
125  */
126 
127 typedef struct {
128 	/* Not using a union here, because there's no good way to initialize them
129 	 * in a way that is supported in both C and C++ (designated initializers
130 	 * are only supported since C++20). */
131 	void *ptr;
132 	uint32_t type_mask;
133 	/* TODO: We could use the extra 32-bit of padding on 64-bit systems. */
134 } zend_type;
135 
136 typedef struct {
137 	uint32_t num_types;
138 	zend_type types[1];
139 } zend_type_list;
140 
141 #define _ZEND_TYPE_EXTRA_FLAGS_SHIFT 25
142 #define _ZEND_TYPE_MASK ((1u << 25) - 1)
143 /* Only one of these bits may be set. */
144 #define _ZEND_TYPE_NAME_BIT (1u << 24)
145 #define _ZEND_TYPE_CE_BIT   (1u << 23)
146 #define _ZEND_TYPE_LIST_BIT (1u << 22)
147 #define _ZEND_TYPE_KIND_MASK (_ZEND_TYPE_LIST_BIT|_ZEND_TYPE_CE_BIT|_ZEND_TYPE_NAME_BIT)
148 /* TODO: bit 21 is not used */
149 /* Whether the type list is arena allocated */
150 #define _ZEND_TYPE_ARENA_BIT (1u << 20)
151 /* Type mask excluding the flags above. */
152 #define _ZEND_TYPE_MAY_BE_MASK ((1u << 20) - 1)
153 /* Must have same value as MAY_BE_NULL */
154 #define _ZEND_TYPE_NULLABLE_BIT 0x2u
155 
156 #define ZEND_TYPE_IS_SET(t) \
157 	(((t).type_mask & _ZEND_TYPE_MASK) != 0)
158 
159 #define ZEND_TYPE_HAS_CLASS(t) \
160 	((((t).type_mask) & _ZEND_TYPE_KIND_MASK) != 0)
161 
162 #define ZEND_TYPE_HAS_CE(t) \
163 	((((t).type_mask) & _ZEND_TYPE_CE_BIT) != 0)
164 
165 #define ZEND_TYPE_HAS_NAME(t) \
166 	((((t).type_mask) & _ZEND_TYPE_NAME_BIT) != 0)
167 
168 #define ZEND_TYPE_HAS_LIST(t) \
169 	((((t).type_mask) & _ZEND_TYPE_LIST_BIT) != 0)
170 
171 #define ZEND_TYPE_USES_ARENA(t) \
172 	((((t).type_mask) & _ZEND_TYPE_ARENA_BIT) != 0)
173 
174 #define ZEND_TYPE_IS_ONLY_MASK(t) \
175 	(ZEND_TYPE_IS_SET(t) && (t).ptr == NULL)
176 
177 #define ZEND_TYPE_NAME(t) \
178 	((zend_string *) (t).ptr)
179 
180 #define ZEND_TYPE_LITERAL_NAME(t) \
181 	((const char *) (t).ptr)
182 
183 #define ZEND_TYPE_CE(t) \
184 	((zend_class_entry *) (t).ptr)
185 
186 #define ZEND_TYPE_LIST(t) \
187 	((zend_type_list *) (t).ptr)
188 
189 #define ZEND_TYPE_LIST_SIZE(num_types) \
190 	(sizeof(zend_type_list) + ((num_types) - 1) * sizeof(zend_type))
191 
192 /* This iterates over a zend_type_list. */
193 #define ZEND_TYPE_LIST_FOREACH(list, type_ptr) do { \
194 	zend_type *_list = (list)->types; \
195 	zend_type *_end = _list + (list)->num_types; \
196 	for (; _list < _end; _list++) { \
197 		type_ptr = _list;
198 
199 #define ZEND_TYPE_LIST_FOREACH_END() \
200 	} \
201 } while (0)
202 
203 /* This iterates over any zend_type. If it's a type list, all list elements will
204  * be visited. If it's a single type, only the single type is visited. */
205 #define ZEND_TYPE_FOREACH(type, type_ptr) do { \
206 	zend_type *_cur, *_end; \
207 	if (ZEND_TYPE_HAS_LIST(type)) { \
208 		zend_type_list *_list = ZEND_TYPE_LIST(type); \
209 		_cur = _list->types; \
210 		_end = _cur + _list->num_types; \
211 	} else { \
212 		_cur = &(type); \
213 		_end = _cur + 1; \
214 	} \
215 	do { \
216 		type_ptr = _cur;
217 
218 #define ZEND_TYPE_FOREACH_END() \
219 	} while (++_cur < _end); \
220 } while (0)
221 
222 #define ZEND_TYPE_SET_PTR(t, _ptr) \
223 	((t).ptr = (_ptr))
224 
225 #define ZEND_TYPE_SET_PTR_AND_KIND(t, _ptr, kind_bit) do { \
226 	(t).ptr = (_ptr); \
227 	(t).type_mask &= ~_ZEND_TYPE_KIND_MASK; \
228 	(t).type_mask |= (kind_bit); \
229 } while (0)
230 
231 #define ZEND_TYPE_SET_CE(t, ce) \
232 	ZEND_TYPE_SET_PTR_AND_KIND(t, ce, _ZEND_TYPE_CE_BIT)
233 
234 #define ZEND_TYPE_SET_LIST(t, list) \
235 	ZEND_TYPE_SET_PTR_AND_KIND(t, list, _ZEND_TYPE_LIST_BIT)
236 
237 /* FULL_MASK() includes the MAY_BE_* type mask, the CE/NAME bits, as well as extra reserved bits.
238  * The PURE_MASK() only includes the MAY_BE_* type mask. */
239 #define ZEND_TYPE_FULL_MASK(t) \
240 	((t).type_mask)
241 
242 #define ZEND_TYPE_PURE_MASK(t) \
243 	((t).type_mask & _ZEND_TYPE_MAY_BE_MASK)
244 
245 #define ZEND_TYPE_FULL_MASK_WITHOUT_NULL(t) \
246 	((t).type_mask & ~_ZEND_TYPE_NULLABLE_BIT)
247 
248 #define ZEND_TYPE_PURE_MASK_WITHOUT_NULL(t) \
249 	((t).type_mask & _ZEND_TYPE_MAY_BE_MASK & ~_ZEND_TYPE_NULLABLE_BIT)
250 
251 #define ZEND_TYPE_CONTAINS_CODE(t, code) \
252 	(((t).type_mask & (1u << (code))) != 0)
253 
254 #define ZEND_TYPE_ALLOW_NULL(t) \
255 	(((t).type_mask & _ZEND_TYPE_NULLABLE_BIT) != 0)
256 
257 #define ZEND_TYPE_INIT_NONE(extra_flags) \
258 	{ NULL, (extra_flags) }
259 
260 #define ZEND_TYPE_INIT_MASK(_type_mask) \
261 	{ NULL, (_type_mask) }
262 
263 #define ZEND_TYPE_INIT_CODE(code, allow_null, extra_flags) \
264 	ZEND_TYPE_INIT_MASK(((code) == _IS_BOOL ? MAY_BE_BOOL : ((code) == IS_MIXED ? MAY_BE_ANY : (1 << (code)))) \
265 		| ((allow_null) ? _ZEND_TYPE_NULLABLE_BIT : 0) | (extra_flags))
266 
267 #define ZEND_TYPE_INIT_PTR(ptr, type_kind, allow_null, extra_flags) \
268 	{ (void *) (ptr), \
269 		(type_kind) | ((allow_null) ? _ZEND_TYPE_NULLABLE_BIT : 0) | (extra_flags) }
270 
271 #define ZEND_TYPE_INIT_PTR_MASK(ptr, type_mask) \
272 	{ (void *) (ptr), (type_mask) }
273 
274 #define ZEND_TYPE_INIT_CE(_ce, allow_null, extra_flags) \
275 	ZEND_TYPE_INIT_PTR(_ce, _ZEND_TYPE_CE_BIT, allow_null, extra_flags)
276 
277 #define ZEND_TYPE_INIT_CLASS(class_name, allow_null, extra_flags) \
278 	ZEND_TYPE_INIT_PTR(class_name, _ZEND_TYPE_NAME_BIT, allow_null, extra_flags)
279 
280 #define ZEND_TYPE_INIT_CLASS_CONST(class_name, allow_null, extra_flags) \
281 	ZEND_TYPE_INIT_PTR(class_name, _ZEND_TYPE_NAME_BIT, allow_null, extra_flags)
282 
283 #define ZEND_TYPE_INIT_CLASS_CONST_MASK(class_name, type_mask) \
284 	ZEND_TYPE_INIT_PTR_MASK(class_name, _ZEND_TYPE_NAME_BIT | (type_mask))
285 
286 typedef union _zend_value {
287 	zend_long         lval;				/* long value */
288 	double            dval;				/* double value */
289 	zend_refcounted  *counted;
290 	zend_string      *str;
291 	zend_array       *arr;
292 	zend_object      *obj;
293 	zend_resource    *res;
294 	zend_reference   *ref;
295 	zend_ast_ref     *ast;
296 	zval             *zv;
297 	void             *ptr;
298 	zend_class_entry *ce;
299 	zend_function    *func;
300 	struct {
301 		uint32_t w1;
302 		uint32_t w2;
303 	} ww;
304 } zend_value;
305 
306 struct _zval_struct {
307 	zend_value        value;			/* value */
308 	union {
309 		uint32_t type_info;
310 		struct {
311 			ZEND_ENDIAN_LOHI_3(
312 				zend_uchar    type,			/* active type */
313 				zend_uchar    type_flags,
314 				union {
315 					uint16_t  extra;        /* not further specified */
316 				} u)
317 		} v;
318 	} u1;
319 	union {
320 		uint32_t     next;                 /* hash collision chain */
321 		uint32_t     cache_slot;           /* cache slot (for RECV_INIT) */
322 		uint32_t     opline_num;           /* opline number (for FAST_CALL) */
323 		uint32_t     lineno;               /* line number (for ast nodes) */
324 		uint32_t     num_args;             /* arguments number for EX(This) */
325 		uint32_t     fe_pos;               /* foreach position */
326 		uint32_t     fe_iter_idx;          /* foreach iterator index */
327 		uint32_t     property_guard;       /* single property guard */
328 		uint32_t     constant_flags;       /* constant flags */
329 		uint32_t     extra;                /* not further specified */
330 	} u2;
331 };
332 
333 typedef struct _zend_refcounted_h {
334 	uint32_t         refcount;			/* reference counter 32-bit */
335 	union {
336 		uint32_t type_info;
337 	} u;
338 } zend_refcounted_h;
339 
340 struct _zend_refcounted {
341 	zend_refcounted_h gc;
342 };
343 
344 struct _zend_string {
345 	zend_refcounted_h gc;
346 	zend_ulong        h;                /* hash value */
347 	size_t            len;
348 	char              val[1];
349 };
350 
351 typedef struct _Bucket {
352 	zval              val;
353 	zend_ulong        h;                /* hash value (or numeric index)   */
354 	zend_string      *key;              /* string key or NULL for numerics */
355 } Bucket;
356 
357 typedef struct _zend_array HashTable;
358 
359 struct _zend_array {
360 	zend_refcounted_h gc;
361 	union {
362 		struct {
363 			ZEND_ENDIAN_LOHI_4(
364 				zend_uchar    flags,
365 				zend_uchar    _unused,
366 				zend_uchar    nIteratorsCount,
367 				zend_uchar    _unused2)
368 		} v;
369 		uint32_t flags;
370 	} u;
371 	uint32_t          nTableMask;
372 	Bucket           *arData;
373 	uint32_t          nNumUsed;
374 	uint32_t          nNumOfElements;
375 	uint32_t          nTableSize;
376 	uint32_t          nInternalPointer;
377 	zend_long         nNextFreeElement;
378 	dtor_func_t       pDestructor;
379 };
380 
381 /*
382  * HashTable Data Layout
383  * =====================
384  *
385  *                 +=============================+
386  *                 | HT_HASH(ht, ht->nTableMask) |
387  *                 | ...                         |
388  *                 | HT_HASH(ht, -1)             |
389  *                 +-----------------------------+
390  * ht->arData ---> | Bucket[0]                   |
391  *                 | ...                         |
392  *                 | Bucket[ht->nTableSize-1]    |
393  *                 +=============================+
394  */
395 
396 #define HT_INVALID_IDX ((uint32_t) -1)
397 
398 #define HT_MIN_MASK ((uint32_t) -2)
399 #define HT_MIN_SIZE 8
400 
401 #if SIZEOF_SIZE_T == 4
402 # define HT_MAX_SIZE 0x04000000 /* small enough to avoid overflow checks */
403 # define HT_HASH_TO_BUCKET_EX(data, idx) \
404 	((Bucket*)((char*)(data) + (idx)))
405 # define HT_IDX_TO_HASH(idx) \
406 	((idx) * sizeof(Bucket))
407 # define HT_HASH_TO_IDX(idx) \
408 	((idx) / sizeof(Bucket))
409 #elif SIZEOF_SIZE_T == 8
410 # define HT_MAX_SIZE 0x80000000
411 # define HT_HASH_TO_BUCKET_EX(data, idx) \
412 	((data) + (idx))
413 # define HT_IDX_TO_HASH(idx) \
414 	(idx)
415 # define HT_HASH_TO_IDX(idx) \
416 	(idx)
417 #else
418 # error "Unknown SIZEOF_SIZE_T"
419 #endif
420 
421 #define HT_HASH_EX(data, idx) \
422 	((uint32_t*)(data))[(int32_t)(idx)]
423 #define HT_HASH(ht, idx) \
424 	HT_HASH_EX((ht)->arData, idx)
425 
426 #define HT_SIZE_TO_MASK(nTableSize) \
427 	((uint32_t)(-((nTableSize) + (nTableSize))))
428 #define HT_HASH_SIZE(nTableMask) \
429 	(((size_t)(uint32_t)-(int32_t)(nTableMask)) * sizeof(uint32_t))
430 #define HT_DATA_SIZE(nTableSize) \
431 	((size_t)(nTableSize) * sizeof(Bucket))
432 #define HT_SIZE_EX(nTableSize, nTableMask) \
433 	(HT_DATA_SIZE((nTableSize)) + HT_HASH_SIZE((nTableMask)))
434 #define HT_SIZE(ht) \
435 	HT_SIZE_EX((ht)->nTableSize, (ht)->nTableMask)
436 #define HT_USED_SIZE(ht) \
437 	(HT_HASH_SIZE((ht)->nTableMask) + ((size_t)(ht)->nNumUsed * sizeof(Bucket)))
438 #ifdef __SSE2__
439 # define HT_HASH_RESET(ht) do { \
440 		char *p = (char*)&HT_HASH(ht, (ht)->nTableMask); \
441 		size_t size = HT_HASH_SIZE((ht)->nTableMask); \
442 		__m128i xmm0 = _mm_setzero_si128(); \
443 		xmm0 = _mm_cmpeq_epi8(xmm0, xmm0); \
444 		ZEND_ASSERT(size >= 64 && ((size & 0x3f) == 0)); \
445 		do { \
446 			_mm_storeu_si128((__m128i*)p, xmm0); \
447 			_mm_storeu_si128((__m128i*)(p+16), xmm0); \
448 			_mm_storeu_si128((__m128i*)(p+32), xmm0); \
449 			_mm_storeu_si128((__m128i*)(p+48), xmm0); \
450 			p += 64; \
451 			size -= 64; \
452 		} while (size != 0); \
453 	} while (0)
454 #else
455 # define HT_HASH_RESET(ht) \
456 	memset(&HT_HASH(ht, (ht)->nTableMask), HT_INVALID_IDX, HT_HASH_SIZE((ht)->nTableMask))
457 #endif
458 #define HT_HASH_RESET_PACKED(ht) do { \
459 		HT_HASH(ht, -2) = HT_INVALID_IDX; \
460 		HT_HASH(ht, -1) = HT_INVALID_IDX; \
461 	} while (0)
462 #define HT_HASH_TO_BUCKET(ht, idx) \
463 	HT_HASH_TO_BUCKET_EX((ht)->arData, idx)
464 
465 #define HT_SET_DATA_ADDR(ht, ptr) do { \
466 		(ht)->arData = (Bucket*)(((char*)(ptr)) + HT_HASH_SIZE((ht)->nTableMask)); \
467 	} while (0)
468 #define HT_GET_DATA_ADDR(ht) \
469 	((char*)((ht)->arData) - HT_HASH_SIZE((ht)->nTableMask))
470 
471 typedef uint32_t HashPosition;
472 
473 typedef struct _HashTableIterator {
474 	HashTable    *ht;
475 	HashPosition  pos;
476 } HashTableIterator;
477 
478 struct _zend_object {
479 	zend_refcounted_h gc;
480 	uint32_t          handle; // TODO: may be removed ???
481 	zend_class_entry *ce;
482 	const zend_object_handlers *handlers;
483 	HashTable        *properties;
484 	zval              properties_table[1];
485 };
486 
487 struct _zend_resource {
488 	zend_refcounted_h gc;
489 	int               handle; // TODO: may be removed ???
490 	int               type;
491 	void             *ptr;
492 };
493 
494 typedef struct {
495 	size_t num;
496 	size_t num_allocated;
497 	struct _zend_property_info *ptr[1];
498 } zend_property_info_list;
499 
500 typedef union {
501 	struct _zend_property_info *ptr;
502 	uintptr_t list;
503 } zend_property_info_source_list;
504 
505 #define ZEND_PROPERTY_INFO_SOURCE_FROM_LIST(list) (0x1 | (uintptr_t) (list))
506 #define ZEND_PROPERTY_INFO_SOURCE_TO_LIST(list) ((zend_property_info_list *) ((list) & ~0x1))
507 #define ZEND_PROPERTY_INFO_SOURCE_IS_LIST(list) ((list) & 0x1)
508 
509 struct _zend_reference {
510 	zend_refcounted_h              gc;
511 	zval                           val;
512 	zend_property_info_source_list sources;
513 };
514 
515 struct _zend_ast_ref {
516 	zend_refcounted_h gc;
517 	/*zend_ast        ast; zend_ast follows the zend_ast_ref structure */
518 };
519 
520 /* Regular data types: Must be in sync with zend_variables.c. */
521 #define IS_UNDEF					0
522 #define IS_NULL						1
523 #define IS_FALSE					2
524 #define IS_TRUE						3
525 #define IS_LONG						4
526 #define IS_DOUBLE					5
527 #define IS_STRING					6
528 #define IS_ARRAY					7
529 #define IS_OBJECT					8
530 #define IS_RESOURCE					9
531 #define IS_REFERENCE				10
532 #define IS_CONSTANT_AST				11 /* Constant expressions */
533 
534 /* Fake types used only for type hinting.
535  * These are allowed to overlap with the types below. */
536 #define IS_CALLABLE					12
537 #define IS_ITERABLE					13
538 #define IS_VOID						14
539 #define IS_STATIC					15
540 #define IS_MIXED					16
541 #define IS_NEVER					17
542 
543 /* internal types */
544 #define IS_INDIRECT             	12
545 #define IS_PTR						13
546 #define IS_ALIAS_PTR				14
547 #define _IS_ERROR					15
548 
549 /* used for casts */
550 #define _IS_BOOL					18
551 #define _IS_NUMBER					19
552 
553 static zend_always_inline zend_uchar zval_get_type(const zval* pz) {
554 	return pz->u1.v.type;
555 }
556 
557 #define ZEND_SAME_FAKE_TYPE(faketype, realtype) ( \
558 	(faketype) == (realtype) \
559 	|| ((faketype) == _IS_BOOL && ((realtype) == IS_TRUE || (realtype) == IS_FALSE)) \
560 )
561 
562 /* we should never set just Z_TYPE, we should set Z_TYPE_INFO */
563 #define Z_TYPE(zval)				zval_get_type(&(zval))
564 #define Z_TYPE_P(zval_p)			Z_TYPE(*(zval_p))
565 
566 #define Z_TYPE_FLAGS(zval)			(zval).u1.v.type_flags
567 #define Z_TYPE_FLAGS_P(zval_p)		Z_TYPE_FLAGS(*(zval_p))
568 
569 #define Z_TYPE_INFO(zval)			(zval).u1.type_info
570 #define Z_TYPE_INFO_P(zval_p)		Z_TYPE_INFO(*(zval_p))
571 
572 #define Z_NEXT(zval)				(zval).u2.next
573 #define Z_NEXT_P(zval_p)			Z_NEXT(*(zval_p))
574 
575 #define Z_CACHE_SLOT(zval)			(zval).u2.cache_slot
576 #define Z_CACHE_SLOT_P(zval_p)		Z_CACHE_SLOT(*(zval_p))
577 
578 #define Z_LINENO(zval)				(zval).u2.lineno
579 #define Z_LINENO_P(zval_p)			Z_LINENO(*(zval_p))
580 
581 #define Z_OPLINE_NUM(zval)			(zval).u2.opline_num
582 #define Z_OPLINE_NUM_P(zval_p)		Z_OPLINE_NUM(*(zval_p))
583 
584 #define Z_FE_POS(zval)				(zval).u2.fe_pos
585 #define Z_FE_POS_P(zval_p)			Z_FE_POS(*(zval_p))
586 
587 #define Z_FE_ITER(zval)				(zval).u2.fe_iter_idx
588 #define Z_FE_ITER_P(zval_p)			Z_FE_ITER(*(zval_p))
589 
590 #define Z_PROPERTY_GUARD(zval)		(zval).u2.property_guard
591 #define Z_PROPERTY_GUARD_P(zval_p)	Z_PROPERTY_GUARD(*(zval_p))
592 
593 #define Z_CONSTANT_FLAGS(zval)		(zval).u2.constant_flags
594 #define Z_CONSTANT_FLAGS_P(zval_p)	Z_CONSTANT_FLAGS(*(zval_p))
595 
596 #define Z_EXTRA(zval)				(zval).u2.extra
597 #define Z_EXTRA_P(zval_p)			Z_EXTRA(*(zval_p))
598 
599 #define Z_COUNTED(zval)				(zval).value.counted
600 #define Z_COUNTED_P(zval_p)			Z_COUNTED(*(zval_p))
601 
602 #define Z_TYPE_MASK					0xff
603 #define Z_TYPE_FLAGS_MASK			0xff00
604 
605 #define Z_TYPE_FLAGS_SHIFT			8
606 
607 #define GC_REFCOUNT(p)				zend_gc_refcount(&(p)->gc)
608 #define GC_SET_REFCOUNT(p, rc)		zend_gc_set_refcount(&(p)->gc, rc)
609 #define GC_ADDREF(p)				zend_gc_addref(&(p)->gc)
610 #define GC_DELREF(p)				zend_gc_delref(&(p)->gc)
611 #define GC_ADDREF_EX(p, rc)			zend_gc_addref_ex(&(p)->gc, rc)
612 #define GC_DELREF_EX(p, rc)			zend_gc_delref_ex(&(p)->gc, rc)
613 #define GC_TRY_ADDREF(p)			zend_gc_try_addref(&(p)->gc)
614 #define GC_TRY_DELREF(p)			zend_gc_try_delref(&(p)->gc)
615 
616 #define GC_TYPE_MASK				0x0000000f
617 #define GC_FLAGS_MASK				0x000003f0
618 #define GC_INFO_MASK				0xfffffc00
619 #define GC_FLAGS_SHIFT				0
620 #define GC_INFO_SHIFT				10
621 
622 static zend_always_inline zend_uchar zval_gc_type(uint32_t gc_type_info) {
623 	return (gc_type_info & GC_TYPE_MASK);
624 }
625 
626 static zend_always_inline uint32_t zval_gc_flags(uint32_t gc_type_info) {
627 	return (gc_type_info >> GC_FLAGS_SHIFT) & (GC_FLAGS_MASK >> GC_FLAGS_SHIFT);
628 }
629 
630 static zend_always_inline uint32_t zval_gc_info(uint32_t gc_type_info) {
631 	return (gc_type_info >> GC_INFO_SHIFT);
632 }
633 
634 #define GC_TYPE_INFO(p)				(p)->gc.u.type_info
635 #define GC_TYPE(p)					zval_gc_type(GC_TYPE_INFO(p))
636 #define GC_FLAGS(p)					zval_gc_flags(GC_TYPE_INFO(p))
637 #define GC_INFO(p)					zval_gc_info(GC_TYPE_INFO(p))
638 
639 #define GC_ADD_FLAGS(p, flags) do { \
640 		GC_TYPE_INFO(p) |= (flags) << GC_FLAGS_SHIFT; \
641 	} while (0)
642 #define GC_DEL_FLAGS(p, flags) do { \
643 		GC_TYPE_INFO(p) &= ~((flags) << GC_FLAGS_SHIFT); \
644 	} while (0)
645 
646 #define Z_GC_TYPE(zval)				GC_TYPE(Z_COUNTED(zval))
647 #define Z_GC_TYPE_P(zval_p)			Z_GC_TYPE(*(zval_p))
648 
649 #define Z_GC_FLAGS(zval)			GC_FLAGS(Z_COUNTED(zval))
650 #define Z_GC_FLAGS_P(zval_p)		Z_GC_FLAGS(*(zval_p))
651 
652 #define Z_GC_INFO(zval)				GC_INFO(Z_COUNTED(zval))
653 #define Z_GC_INFO_P(zval_p)			Z_GC_INFO(*(zval_p))
654 #define Z_GC_TYPE_INFO(zval)		GC_TYPE_INFO(Z_COUNTED(zval))
655 #define Z_GC_TYPE_INFO_P(zval_p)	Z_GC_TYPE_INFO(*(zval_p))
656 
657 /* zval_gc_flags(zval.value->gc.u.type_info) (common flags) */
658 #define GC_NOT_COLLECTABLE			(1<<4)
659 #define GC_PROTECTED                (1<<5) /* used for recursion detection */
660 #define GC_IMMUTABLE                (1<<6) /* can't be changed in place */
661 #define GC_PERSISTENT               (1<<7) /* allocated using malloc */
662 #define GC_PERSISTENT_LOCAL         (1<<8) /* persistent, but thread-local */
663 
664 #define GC_NULL						(IS_NULL         | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT))
665 #define GC_STRING					(IS_STRING       | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT))
666 #define GC_ARRAY					IS_ARRAY
667 #define GC_OBJECT					IS_OBJECT
668 #define GC_RESOURCE					(IS_RESOURCE     | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT))
669 #define GC_REFERENCE				(IS_REFERENCE    | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT))
670 #define GC_CONSTANT_AST				(IS_CONSTANT_AST | (GC_NOT_COLLECTABLE << GC_FLAGS_SHIFT))
671 
672 /* zval.u1.v.type_flags */
673 #define IS_TYPE_REFCOUNTED			(1<<0)
674 #define IS_TYPE_COLLECTABLE			(1<<1)
675 
676 #if 1
677 /* This optimized version assumes that we have a single "type_flag" */
678 /* IS_TYPE_COLLECTABLE may be used only with IS_TYPE_REFCOUNTED */
679 # define Z_TYPE_INFO_REFCOUNTED(t)	(((t) & Z_TYPE_FLAGS_MASK) != 0)
680 #else
681 # define Z_TYPE_INFO_REFCOUNTED(t)	(((t) & (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT)) != 0)
682 #endif
683 
684 /* extended types */
685 #define IS_INTERNED_STRING_EX		IS_STRING
686 
687 #define IS_STRING_EX				(IS_STRING         | (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT))
688 #define IS_ARRAY_EX					(IS_ARRAY          | (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT) | (IS_TYPE_COLLECTABLE << Z_TYPE_FLAGS_SHIFT))
689 #define IS_OBJECT_EX				(IS_OBJECT         | (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT) | (IS_TYPE_COLLECTABLE << Z_TYPE_FLAGS_SHIFT))
690 #define IS_RESOURCE_EX				(IS_RESOURCE       | (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT))
691 #define IS_REFERENCE_EX				(IS_REFERENCE      | (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT))
692 
693 #define IS_CONSTANT_AST_EX			(IS_CONSTANT_AST   | (IS_TYPE_REFCOUNTED << Z_TYPE_FLAGS_SHIFT))
694 
695 /* string flags (zval.value->gc.u.flags) */
696 #define IS_STR_CLASS_NAME_MAP_PTR   GC_PROTECTED  /* refcount is a map_ptr offset of class_entry */
697 #define IS_STR_INTERNED				GC_IMMUTABLE  /* interned string */
698 #define IS_STR_PERSISTENT			GC_PERSISTENT /* allocated using malloc */
699 #define IS_STR_PERMANENT        	(1<<8)        /* relives request boundary */
700 #define IS_STR_VALID_UTF8           (1<<9)        /* valid UTF-8 according to PCRE */
701 
702 /* array flags */
703 #define IS_ARRAY_IMMUTABLE			GC_IMMUTABLE
704 #define IS_ARRAY_PERSISTENT			GC_PERSISTENT
705 
706 /* object flags (zval.value->gc.u.flags) */
707 #define IS_OBJ_WEAKLY_REFERENCED	GC_PERSISTENT
708 #define IS_OBJ_DESTRUCTOR_CALLED	(1<<8)
709 #define IS_OBJ_FREE_CALLED			(1<<9)
710 
711 #define OBJ_FLAGS(obj)              GC_FLAGS(obj)
712 
713 /* Fast class cache */
714 #define ZSTR_HAS_CE_CACHE(s)		(GC_FLAGS(s) & IS_STR_CLASS_NAME_MAP_PTR)
715 #define ZSTR_GET_CE_CACHE(s) \
716 	(*(zend_class_entry **)ZEND_MAP_PTR_OFFSET2PTR(GC_REFCOUNT(s)))
717 #define ZSTR_SET_CE_CACHE(s, ce) do { \
718 		*((zend_class_entry **)ZEND_MAP_PTR_OFFSET2PTR(GC_REFCOUNT(s))) = ce; \
719 	} while (0)
720 
721 /* Recursion protection macros must be used only for arrays and objects */
722 #define GC_IS_RECURSIVE(p) \
723 	(GC_FLAGS(p) & GC_PROTECTED)
724 
725 #define GC_PROTECT_RECURSION(p) do { \
726 		GC_ADD_FLAGS(p, GC_PROTECTED); \
727 	} while (0)
728 
729 #define GC_UNPROTECT_RECURSION(p) do { \
730 		GC_DEL_FLAGS(p, GC_PROTECTED); \
731 	} while (0)
732 
733 #define GC_TRY_PROTECT_RECURSION(p) do { \
734 		if (!(GC_FLAGS(p) & GC_IMMUTABLE)) GC_PROTECT_RECURSION(p); \
735 	} while (0)
736 
737 #define GC_TRY_UNPROTECT_RECURSION(p) do { \
738 		if (!(GC_FLAGS(p) & GC_IMMUTABLE)) GC_UNPROTECT_RECURSION(p); \
739 	} while (0)
740 
741 #define Z_IS_RECURSIVE(zval)        GC_IS_RECURSIVE(Z_COUNTED(zval))
742 #define Z_PROTECT_RECURSION(zval)   GC_PROTECT_RECURSION(Z_COUNTED(zval))
743 #define Z_UNPROTECT_RECURSION(zval) GC_UNPROTECT_RECURSION(Z_COUNTED(zval))
744 #define Z_IS_RECURSIVE_P(zv)        Z_IS_RECURSIVE(*(zv))
745 #define Z_PROTECT_RECURSION_P(zv)   Z_PROTECT_RECURSION(*(zv))
746 #define Z_UNPROTECT_RECURSION_P(zv) Z_UNPROTECT_RECURSION(*(zv))
747 
748 /* All data types < IS_STRING have their constructor/destructors skipped */
749 #define Z_CONSTANT(zval)			(Z_TYPE(zval) == IS_CONSTANT_AST)
750 #define Z_CONSTANT_P(zval_p)		Z_CONSTANT(*(zval_p))
751 
752 #if 1
753 /* This optimized version assumes that we have a single "type_flag" */
754 /* IS_TYPE_COLLECTABLE may be used only with IS_TYPE_REFCOUNTED */
755 #define Z_REFCOUNTED(zval)			(Z_TYPE_FLAGS(zval) != 0)
756 #else
757 #define Z_REFCOUNTED(zval)			((Z_TYPE_FLAGS(zval) & IS_TYPE_REFCOUNTED) != 0)
758 #endif
759 #define Z_REFCOUNTED_P(zval_p)		Z_REFCOUNTED(*(zval_p))
760 
761 #define Z_COLLECTABLE(zval)			((Z_TYPE_FLAGS(zval) & IS_TYPE_COLLECTABLE) != 0)
762 #define Z_COLLECTABLE_P(zval_p)		Z_COLLECTABLE(*(zval_p))
763 
764 /* deprecated: (COPYABLE is the same as IS_ARRAY) */
765 #define Z_COPYABLE(zval)			(Z_TYPE(zval) == IS_ARRAY)
766 #define Z_COPYABLE_P(zval_p)		Z_COPYABLE(*(zval_p))
767 
768 /* deprecated: (IMMUTABLE is the same as IS_ARRAY && !REFCOUNTED) */
769 #define Z_IMMUTABLE(zval)			(Z_TYPE_INFO(zval) == IS_ARRAY)
770 #define Z_IMMUTABLE_P(zval_p)		Z_IMMUTABLE(*(zval_p))
771 #define Z_OPT_IMMUTABLE(zval)		Z_IMMUTABLE(zval_p)
772 #define Z_OPT_IMMUTABLE_P(zval_p)	Z_IMMUTABLE(*(zval_p))
773 
774 /* the following Z_OPT_* macros make better code when Z_TYPE_INFO accessed before */
775 #define Z_OPT_TYPE(zval)			(Z_TYPE_INFO(zval) & Z_TYPE_MASK)
776 #define Z_OPT_TYPE_P(zval_p)		Z_OPT_TYPE(*(zval_p))
777 
778 #define Z_OPT_CONSTANT(zval)		(Z_OPT_TYPE(zval) == IS_CONSTANT_AST)
779 #define Z_OPT_CONSTANT_P(zval_p)	Z_OPT_CONSTANT(*(zval_p))
780 
781 #define Z_OPT_REFCOUNTED(zval)		Z_TYPE_INFO_REFCOUNTED(Z_TYPE_INFO(zval))
782 #define Z_OPT_REFCOUNTED_P(zval_p)	Z_OPT_REFCOUNTED(*(zval_p))
783 
784 /* deprecated: (COPYABLE is the same as IS_ARRAY) */
785 #define Z_OPT_COPYABLE(zval)		(Z_OPT_TYPE(zval) == IS_ARRAY)
786 #define Z_OPT_COPYABLE_P(zval_p)	Z_OPT_COPYABLE(*(zval_p))
787 
788 #define Z_OPT_ISREF(zval)			(Z_OPT_TYPE(zval) == IS_REFERENCE)
789 #define Z_OPT_ISREF_P(zval_p)		Z_OPT_ISREF(*(zval_p))
790 
791 #define Z_ISREF(zval)				(Z_TYPE(zval) == IS_REFERENCE)
792 #define Z_ISREF_P(zval_p)			Z_ISREF(*(zval_p))
793 
794 #define Z_ISUNDEF(zval)				(Z_TYPE(zval) == IS_UNDEF)
795 #define Z_ISUNDEF_P(zval_p)			Z_ISUNDEF(*(zval_p))
796 
797 #define Z_ISNULL(zval)				(Z_TYPE(zval) == IS_NULL)
798 #define Z_ISNULL_P(zval_p)			Z_ISNULL(*(zval_p))
799 
800 #define Z_ISERROR(zval)				(Z_TYPE(zval) == _IS_ERROR)
801 #define Z_ISERROR_P(zval_p)			Z_ISERROR(*(zval_p))
802 
803 #define Z_LVAL(zval)				(zval).value.lval
804 #define Z_LVAL_P(zval_p)			Z_LVAL(*(zval_p))
805 
806 #define Z_DVAL(zval)				(zval).value.dval
807 #define Z_DVAL_P(zval_p)			Z_DVAL(*(zval_p))
808 
809 #define Z_STR(zval)					(zval).value.str
810 #define Z_STR_P(zval_p)				Z_STR(*(zval_p))
811 
812 #define Z_STRVAL(zval)				ZSTR_VAL(Z_STR(zval))
813 #define Z_STRVAL_P(zval_p)			Z_STRVAL(*(zval_p))
814 
815 #define Z_STRLEN(zval)				ZSTR_LEN(Z_STR(zval))
816 #define Z_STRLEN_P(zval_p)			Z_STRLEN(*(zval_p))
817 
818 #define Z_STRHASH(zval)				ZSTR_HASH(Z_STR(zval))
819 #define Z_STRHASH_P(zval_p)			Z_STRHASH(*(zval_p))
820 
821 #define Z_ARR(zval)					(zval).value.arr
822 #define Z_ARR_P(zval_p)				Z_ARR(*(zval_p))
823 
824 #define Z_ARRVAL(zval)				Z_ARR(zval)
825 #define Z_ARRVAL_P(zval_p)			Z_ARRVAL(*(zval_p))
826 
827 #define Z_OBJ(zval)					(zval).value.obj
828 #define Z_OBJ_P(zval_p)				Z_OBJ(*(zval_p))
829 
830 #define Z_OBJ_HT(zval)				Z_OBJ(zval)->handlers
831 #define Z_OBJ_HT_P(zval_p)			Z_OBJ_HT(*(zval_p))
832 
833 #define Z_OBJ_HANDLER(zval, hf)		Z_OBJ_HT((zval))->hf
834 #define Z_OBJ_HANDLER_P(zv_p, hf)	Z_OBJ_HANDLER(*(zv_p), hf)
835 
836 #define Z_OBJ_HANDLE(zval)          (Z_OBJ((zval)))->handle
837 #define Z_OBJ_HANDLE_P(zval_p)      Z_OBJ_HANDLE(*(zval_p))
838 
839 #define Z_OBJCE(zval)				(Z_OBJ(zval)->ce)
840 #define Z_OBJCE_P(zval_p)			Z_OBJCE(*(zval_p))
841 
842 #define Z_OBJPROP(zval)				Z_OBJ_HT((zval))->get_properties(Z_OBJ(zval))
843 #define Z_OBJPROP_P(zval_p)			Z_OBJPROP(*(zval_p))
844 
845 #define Z_RES(zval)					(zval).value.res
846 #define Z_RES_P(zval_p)				Z_RES(*zval_p)
847 
848 #define Z_RES_HANDLE(zval)			Z_RES(zval)->handle
849 #define Z_RES_HANDLE_P(zval_p)		Z_RES_HANDLE(*zval_p)
850 
851 #define Z_RES_TYPE(zval)			Z_RES(zval)->type
852 #define Z_RES_TYPE_P(zval_p)		Z_RES_TYPE(*zval_p)
853 
854 #define Z_RES_VAL(zval)				Z_RES(zval)->ptr
855 #define Z_RES_VAL_P(zval_p)			Z_RES_VAL(*zval_p)
856 
857 #define Z_REF(zval)					(zval).value.ref
858 #define Z_REF_P(zval_p)				Z_REF(*(zval_p))
859 
860 #define Z_REFVAL(zval)				&Z_REF(zval)->val
861 #define Z_REFVAL_P(zval_p)			Z_REFVAL(*(zval_p))
862 
863 #define Z_AST(zval)					(zval).value.ast
864 #define Z_AST_P(zval_p)				Z_AST(*(zval_p))
865 
866 #define GC_AST(p)					((zend_ast*)(((char*)p) + sizeof(zend_ast_ref)))
867 
868 #define Z_ASTVAL(zval)				GC_AST(Z_AST(zval))
869 #define Z_ASTVAL_P(zval_p)			Z_ASTVAL(*(zval_p))
870 
871 #define Z_INDIRECT(zval)			(zval).value.zv
872 #define Z_INDIRECT_P(zval_p)		Z_INDIRECT(*(zval_p))
873 
874 #define Z_CE(zval)					(zval).value.ce
875 #define Z_CE_P(zval_p)				Z_CE(*(zval_p))
876 
877 #define Z_FUNC(zval)				(zval).value.func
878 #define Z_FUNC_P(zval_p)			Z_FUNC(*(zval_p))
879 
880 #define Z_PTR(zval)					(zval).value.ptr
881 #define Z_PTR_P(zval_p)				Z_PTR(*(zval_p))
882 
883 #define ZVAL_UNDEF(z) do {				\
884 		Z_TYPE_INFO_P(z) = IS_UNDEF;	\
885 	} while (0)
886 
887 #define ZVAL_NULL(z) do {				\
888 		Z_TYPE_INFO_P(z) = IS_NULL;		\
889 	} while (0)
890 
891 #define ZVAL_FALSE(z) do {				\
892 		Z_TYPE_INFO_P(z) = IS_FALSE;	\
893 	} while (0)
894 
895 #define ZVAL_TRUE(z) do {				\
896 		Z_TYPE_INFO_P(z) = IS_TRUE;		\
897 	} while (0)
898 
899 #define ZVAL_BOOL(z, b) do {			\
900 		Z_TYPE_INFO_P(z) =				\
901 			(b) ? IS_TRUE : IS_FALSE;	\
902 	} while (0)
903 
904 #define ZVAL_LONG(z, l) do {			\
905 		zval *__z = (z);				\
906 		Z_LVAL_P(__z) = l;				\
907 		Z_TYPE_INFO_P(__z) = IS_LONG;	\
908 	} while (0)
909 
910 #define ZVAL_DOUBLE(z, d) do {			\
911 		zval *__z = (z);				\
912 		Z_DVAL_P(__z) = d;				\
913 		Z_TYPE_INFO_P(__z) = IS_DOUBLE;	\
914 	} while (0)
915 
916 #define ZVAL_STR(z, s) do {						\
917 		zval *__z = (z);						\
918 		zend_string *__s = (s);					\
919 		Z_STR_P(__z) = __s;						\
920 		/* interned strings support */			\
921 		Z_TYPE_INFO_P(__z) = ZSTR_IS_INTERNED(__s) ? \
922 			IS_INTERNED_STRING_EX : 			\
923 			IS_STRING_EX;						\
924 	} while (0)
925 
926 #define ZVAL_INTERNED_STR(z, s) do {				\
927 		zval *__z = (z);							\
928 		zend_string *__s = (s);						\
929 		Z_STR_P(__z) = __s;							\
930 		Z_TYPE_INFO_P(__z) = IS_INTERNED_STRING_EX;	\
931 	} while (0)
932 
933 #define ZVAL_NEW_STR(z, s) do {					\
934 		zval *__z = (z);						\
935 		zend_string *__s = (s);					\
936 		Z_STR_P(__z) = __s;						\
937 		Z_TYPE_INFO_P(__z) = IS_STRING_EX;		\
938 	} while (0)
939 
940 #define ZVAL_STR_COPY(z, s) do {						\
941 		zval *__z = (z);								\
942 		zend_string *__s = (s);							\
943 		Z_STR_P(__z) = __s;								\
944 		/* interned strings support */					\
945 		if (ZSTR_IS_INTERNED(__s)) {					\
946 			Z_TYPE_INFO_P(__z) = IS_INTERNED_STRING_EX;	\
947 		} else {										\
948 			GC_ADDREF(__s);								\
949 			Z_TYPE_INFO_P(__z) = IS_STRING_EX;			\
950 		}												\
951 	} while (0)
952 
953 #define ZVAL_ARR(z, a) do {						\
954 		zend_array *__arr = (a);				\
955 		zval *__z = (z);						\
956 		Z_ARR_P(__z) = __arr;					\
957 		Z_TYPE_INFO_P(__z) = IS_ARRAY_EX;		\
958 	} while (0)
959 
960 #define ZVAL_NEW_ARR(z) do {									\
961 		zval *__z = (z);										\
962 		zend_array *_arr =										\
963 		(zend_array *) emalloc(sizeof(zend_array));				\
964 		Z_ARR_P(__z) = _arr;									\
965 		Z_TYPE_INFO_P(__z) = IS_ARRAY_EX;						\
966 	} while (0)
967 
968 #define ZVAL_NEW_PERSISTENT_ARR(z) do {							\
969 		zval *__z = (z);										\
970 		zend_array *_arr =										\
971 		(zend_array *) malloc(sizeof(zend_array));				\
972 		Z_ARR_P(__z) = _arr;									\
973 		Z_TYPE_INFO_P(__z) = IS_ARRAY_EX;						\
974 	} while (0)
975 
976 #define ZVAL_OBJ(z, o) do {						\
977 		zval *__z = (z);						\
978 		Z_OBJ_P(__z) = (o);						\
979 		Z_TYPE_INFO_P(__z) = IS_OBJECT_EX;		\
980 	} while (0)
981 
982 #define ZVAL_OBJ_COPY(z, o) do {				\
983 		zval *__z = (z);						\
984 		zend_object *__o = (o);					\
985 		GC_ADDREF(__o);							\
986 		Z_OBJ_P(__z) = __o;						\
987 		Z_TYPE_INFO_P(__z) = IS_OBJECT_EX;		\
988 	} while (0)
989 
990 #define ZVAL_RES(z, r) do {						\
991 		zval *__z = (z);						\
992 		Z_RES_P(__z) = (r);						\
993 		Z_TYPE_INFO_P(__z) = IS_RESOURCE_EX;	\
994 	} while (0)
995 
996 #define ZVAL_NEW_RES(z, h, p, t) do {							\
997 		zend_resource *_res =									\
998 		(zend_resource *) emalloc(sizeof(zend_resource));		\
999 		zval *__z;												\
1000 		GC_SET_REFCOUNT(_res, 1);								\
1001 		GC_TYPE_INFO(_res) = GC_RESOURCE;						\
1002 		_res->handle = (h);										\
1003 		_res->type = (t);										\
1004 		_res->ptr = (p);										\
1005 		__z = (z);												\
1006 		Z_RES_P(__z) = _res;									\
1007 		Z_TYPE_INFO_P(__z) = IS_RESOURCE_EX;					\
1008 	} while (0)
1009 
1010 #define ZVAL_NEW_PERSISTENT_RES(z, h, p, t) do {				\
1011 		zend_resource *_res =									\
1012 		(zend_resource *) malloc(sizeof(zend_resource));		\
1013 		zval *__z;												\
1014 		GC_SET_REFCOUNT(_res, 1);								\
1015 		GC_TYPE_INFO(_res) = GC_RESOURCE |						\
1016 			(GC_PERSISTENT << GC_FLAGS_SHIFT);					\
1017 		_res->handle = (h);										\
1018 		_res->type = (t);										\
1019 		_res->ptr = (p);										\
1020 		__z = (z);												\
1021 		Z_RES_P(__z) = _res;									\
1022 		Z_TYPE_INFO_P(__z) = IS_RESOURCE_EX;					\
1023 	} while (0)
1024 
1025 #define ZVAL_REF(z, r) do {										\
1026 		zval *__z = (z);										\
1027 		Z_REF_P(__z) = (r);										\
1028 		Z_TYPE_INFO_P(__z) = IS_REFERENCE_EX;					\
1029 	} while (0)
1030 
1031 #define ZVAL_NEW_EMPTY_REF(z) do {								\
1032 		zend_reference *_ref =									\
1033 		(zend_reference *) emalloc(sizeof(zend_reference));		\
1034 		GC_SET_REFCOUNT(_ref, 1);								\
1035 		GC_TYPE_INFO(_ref) = GC_REFERENCE;						\
1036 		_ref->sources.ptr = NULL;									\
1037 		Z_REF_P(z) = _ref;										\
1038 		Z_TYPE_INFO_P(z) = IS_REFERENCE_EX;						\
1039 	} while (0)
1040 
1041 #define ZVAL_NEW_REF(z, r) do {									\
1042 		zend_reference *_ref =									\
1043 		(zend_reference *) emalloc(sizeof(zend_reference));		\
1044 		GC_SET_REFCOUNT(_ref, 1);								\
1045 		GC_TYPE_INFO(_ref) = GC_REFERENCE;						\
1046 		ZVAL_COPY_VALUE(&_ref->val, r);							\
1047 		_ref->sources.ptr = NULL;									\
1048 		Z_REF_P(z) = _ref;										\
1049 		Z_TYPE_INFO_P(z) = IS_REFERENCE_EX;						\
1050 	} while (0)
1051 
1052 #define ZVAL_MAKE_REF_EX(z, refcount) do {						\
1053 		zval *_z = (z);											\
1054 		zend_reference *_ref =									\
1055 			(zend_reference *) emalloc(sizeof(zend_reference));	\
1056 		GC_SET_REFCOUNT(_ref, (refcount));						\
1057 		GC_TYPE_INFO(_ref) = GC_REFERENCE;						\
1058 		ZVAL_COPY_VALUE(&_ref->val, _z);						\
1059 		_ref->sources.ptr = NULL;									\
1060 		Z_REF_P(_z) = _ref;										\
1061 		Z_TYPE_INFO_P(_z) = IS_REFERENCE_EX;					\
1062 	} while (0)
1063 
1064 #define ZVAL_NEW_PERSISTENT_REF(z, r) do {						\
1065 		zend_reference *_ref =									\
1066 		(zend_reference *) malloc(sizeof(zend_reference));		\
1067 		GC_SET_REFCOUNT(_ref, 1);								\
1068 		GC_TYPE_INFO(_ref) = GC_REFERENCE |						\
1069 			(GC_PERSISTENT << GC_FLAGS_SHIFT);					\
1070 		ZVAL_COPY_VALUE(&_ref->val, r);							\
1071 		_ref->sources.ptr = NULL;									\
1072 		Z_REF_P(z) = _ref;										\
1073 		Z_TYPE_INFO_P(z) = IS_REFERENCE_EX;						\
1074 	} while (0)
1075 
1076 #define ZVAL_AST(z, ast) do {									\
1077 		zval *__z = (z);										\
1078 		Z_AST_P(__z) = ast;										\
1079 		Z_TYPE_INFO_P(__z) = IS_CONSTANT_AST_EX;				\
1080 	} while (0)
1081 
1082 #define ZVAL_INDIRECT(z, v) do {								\
1083 		Z_INDIRECT_P(z) = (v);									\
1084 		Z_TYPE_INFO_P(z) = IS_INDIRECT;							\
1085 	} while (0)
1086 
1087 #define ZVAL_PTR(z, p) do {										\
1088 		Z_PTR_P(z) = (p);										\
1089 		Z_TYPE_INFO_P(z) = IS_PTR;								\
1090 	} while (0)
1091 
1092 #define ZVAL_FUNC(z, f) do {									\
1093 		Z_FUNC_P(z) = (f);										\
1094 		Z_TYPE_INFO_P(z) = IS_PTR;								\
1095 	} while (0)
1096 
1097 #define ZVAL_CE(z, c) do {										\
1098 		Z_CE_P(z) = (c);										\
1099 		Z_TYPE_INFO_P(z) = IS_PTR;								\
1100 	} while (0)
1101 
1102 #define ZVAL_ALIAS_PTR(z, p) do {								\
1103 		Z_PTR_P(z) = (p);										\
1104 		Z_TYPE_INFO_P(z) = IS_ALIAS_PTR;						\
1105 	} while (0)
1106 
1107 #define ZVAL_ERROR(z) do {				\
1108 		Z_TYPE_INFO_P(z) = _IS_ERROR;	\
1109 	} while (0)
1110 
1111 #define Z_REFCOUNT_P(pz)			zval_refcount_p(pz)
1112 #define Z_SET_REFCOUNT_P(pz, rc)	zval_set_refcount_p(pz, rc)
1113 #define Z_ADDREF_P(pz)				zval_addref_p(pz)
1114 #define Z_DELREF_P(pz)				zval_delref_p(pz)
1115 
1116 #define Z_REFCOUNT(z)				Z_REFCOUNT_P(&(z))
1117 #define Z_SET_REFCOUNT(z, rc)		Z_SET_REFCOUNT_P(&(z), rc)
1118 #define Z_ADDREF(z)					Z_ADDREF_P(&(z))
1119 #define Z_DELREF(z)					Z_DELREF_P(&(z))
1120 
1121 #define Z_TRY_ADDREF_P(pz) do {		\
1122 	if (Z_REFCOUNTED_P((pz))) {		\
1123 		Z_ADDREF_P((pz));			\
1124 	}								\
1125 } while (0)
1126 
1127 #define Z_TRY_DELREF_P(pz) do {		\
1128 	if (Z_REFCOUNTED_P((pz))) {		\
1129 		Z_DELREF_P((pz));			\
1130 	}								\
1131 } while (0)
1132 
1133 #define Z_TRY_ADDREF(z)				Z_TRY_ADDREF_P(&(z))
1134 #define Z_TRY_DELREF(z)				Z_TRY_DELREF_P(&(z))
1135 
1136 #ifndef ZEND_RC_DEBUG
1137 # define ZEND_RC_DEBUG 0
1138 #endif
1139 
1140 #if ZEND_RC_DEBUG
1141 extern ZEND_API bool zend_rc_debug;
1142 # define ZEND_RC_MOD_CHECK(p) do { \
1143 		if (zend_rc_debug && zval_gc_type((p)->u.type_info) != IS_OBJECT) { \
1144 			ZEND_ASSERT(!(zval_gc_flags((p)->u.type_info) & GC_IMMUTABLE)); \
1145 			ZEND_ASSERT((zval_gc_flags((p)->u.type_info) & (GC_PERSISTENT|GC_PERSISTENT_LOCAL)) != GC_PERSISTENT); \
1146 		} \
1147 	} while (0)
1148 # define GC_MAKE_PERSISTENT_LOCAL(p) do { \
1149 		GC_ADD_FLAGS(p, GC_PERSISTENT_LOCAL); \
1150 	} while (0)
1151 #else
1152 # define ZEND_RC_MOD_CHECK(p) \
1153 	do { } while (0)
1154 # define GC_MAKE_PERSISTENT_LOCAL(p) \
1155 	do { } while (0)
1156 #endif
1157 
1158 static zend_always_inline uint32_t zend_gc_refcount(const zend_refcounted_h *p) {
1159 	return p->refcount;
1160 }
1161 
1162 static zend_always_inline uint32_t zend_gc_set_refcount(zend_refcounted_h *p, uint32_t rc) {
1163 	p->refcount = rc;
1164 	return p->refcount;
1165 }
1166 
1167 static zend_always_inline uint32_t zend_gc_addref(zend_refcounted_h *p) {
1168 	ZEND_RC_MOD_CHECK(p);
1169 	return ++(p->refcount);
1170 }
1171 
1172 static zend_always_inline void zend_gc_try_addref(zend_refcounted_h *p) {
1173 	if (!(p->u.type_info & GC_IMMUTABLE)) {
1174 		ZEND_RC_MOD_CHECK(p);
1175 		++p->refcount;
1176 	}
1177 }
1178 
1179 static zend_always_inline void zend_gc_try_delref(zend_refcounted_h *p) {
1180 	if (!(p->u.type_info & GC_IMMUTABLE)) {
1181 		ZEND_RC_MOD_CHECK(p);
1182 		--p->refcount;
1183 	}
1184 }
1185 
1186 static zend_always_inline uint32_t zend_gc_delref(zend_refcounted_h *p) {
1187 	ZEND_ASSERT(p->refcount > 0);
1188 	ZEND_RC_MOD_CHECK(p);
1189 	return --(p->refcount);
1190 }
1191 
1192 static zend_always_inline uint32_t zend_gc_addref_ex(zend_refcounted_h *p, uint32_t rc) {
1193 	ZEND_RC_MOD_CHECK(p);
1194 	p->refcount += rc;
1195 	return p->refcount;
1196 }
1197 
1198 static zend_always_inline uint32_t zend_gc_delref_ex(zend_refcounted_h *p, uint32_t rc) {
1199 	ZEND_RC_MOD_CHECK(p);
1200 	p->refcount -= rc;
1201 	return p->refcount;
1202 }
1203 
1204 static zend_always_inline uint32_t zval_refcount_p(const zval* pz) {
1205 #if ZEND_DEBUG
1206 	ZEND_ASSERT(Z_REFCOUNTED_P(pz) || Z_TYPE_P(pz) == IS_ARRAY);
1207 #endif
1208 	return GC_REFCOUNT(Z_COUNTED_P(pz));
1209 }
1210 
1211 static zend_always_inline uint32_t zval_set_refcount_p(zval* pz, uint32_t rc) {
1212 	ZEND_ASSERT(Z_REFCOUNTED_P(pz));
1213 	return GC_SET_REFCOUNT(Z_COUNTED_P(pz), rc);
1214 }
1215 
1216 static zend_always_inline uint32_t zval_addref_p(zval* pz) {
1217 	ZEND_ASSERT(Z_REFCOUNTED_P(pz));
1218 	return GC_ADDREF(Z_COUNTED_P(pz));
1219 }
1220 
1221 static zend_always_inline uint32_t zval_delref_p(zval* pz) {
1222 	ZEND_ASSERT(Z_REFCOUNTED_P(pz));
1223 	return GC_DELREF(Z_COUNTED_P(pz));
1224 }
1225 
1226 #if SIZEOF_SIZE_T == 4
1227 # define ZVAL_COPY_VALUE_EX(z, v, gc, t)				\
1228 	do {												\
1229 		uint32_t _w2 = v->value.ww.w2;					\
1230 		Z_COUNTED_P(z) = gc;							\
1231 		z->value.ww.w2 = _w2;							\
1232 		Z_TYPE_INFO_P(z) = t;							\
1233 	} while (0)
1234 #elif SIZEOF_SIZE_T == 8
1235 # define ZVAL_COPY_VALUE_EX(z, v, gc, t)				\
1236 	do {												\
1237 		Z_COUNTED_P(z) = gc;							\
1238 		Z_TYPE_INFO_P(z) = t;							\
1239 	} while (0)
1240 #else
1241 # error "Unknown SIZEOF_SIZE_T"
1242 #endif
1243 
1244 #define ZVAL_COPY_VALUE(z, v)							\
1245 	do {												\
1246 		zval *_z1 = (z);								\
1247 		const zval *_z2 = (v);							\
1248 		zend_refcounted *_gc = Z_COUNTED_P(_z2);		\
1249 		uint32_t _t = Z_TYPE_INFO_P(_z2);				\
1250 		ZVAL_COPY_VALUE_EX(_z1, _z2, _gc, _t);			\
1251 	} while (0)
1252 
1253 #define ZVAL_COPY(z, v)									\
1254 	do {												\
1255 		zval *_z1 = (z);								\
1256 		const zval *_z2 = (v);							\
1257 		zend_refcounted *_gc = Z_COUNTED_P(_z2);		\
1258 		uint32_t _t = Z_TYPE_INFO_P(_z2);				\
1259 		ZVAL_COPY_VALUE_EX(_z1, _z2, _gc, _t);			\
1260 		if (Z_TYPE_INFO_REFCOUNTED(_t)) {				\
1261 			GC_ADDREF(_gc);								\
1262 		}												\
1263 	} while (0)
1264 
1265 #define ZVAL_DUP(z, v)									\
1266 	do {												\
1267 		zval *_z1 = (z);								\
1268 		const zval *_z2 = (v);							\
1269 		zend_refcounted *_gc = Z_COUNTED_P(_z2);		\
1270 		uint32_t _t = Z_TYPE_INFO_P(_z2);				\
1271 		if ((_t & Z_TYPE_MASK) == IS_ARRAY) {			\
1272 			ZVAL_ARR(_z1, zend_array_dup((zend_array*)_gc));\
1273 		} else {										\
1274 			if (Z_TYPE_INFO_REFCOUNTED(_t)) {			\
1275 				GC_ADDREF(_gc);							\
1276 			}											\
1277 			ZVAL_COPY_VALUE_EX(_z1, _z2, _gc, _t);		\
1278 		}												\
1279 	} while (0)
1280 
1281 
1282 /* ZVAL_COPY_OR_DUP() should be used instead of ZVAL_COPY() and ZVAL_DUP()
1283  * in all places where the source may be a persistent zval.
1284  */
1285 #define ZVAL_COPY_OR_DUP(z, v)											\
1286 	do {																\
1287 		zval *_z1 = (z);												\
1288 		const zval *_z2 = (v);											\
1289 		zend_refcounted *_gc = Z_COUNTED_P(_z2);						\
1290 		uint32_t _t = Z_TYPE_INFO_P(_z2);								\
1291 		ZVAL_COPY_VALUE_EX(_z1, _z2, _gc, _t);							\
1292 		if (Z_TYPE_INFO_REFCOUNTED(_t)) {								\
1293 			if (EXPECTED(!(GC_FLAGS(_gc) & GC_PERSISTENT))) {			\
1294 				GC_ADDREF(_gc);											\
1295 			} else {													\
1296 				zval_copy_ctor_func(_z1);								\
1297 			}															\
1298 		}																\
1299 	} while (0)
1300 
1301 #define ZVAL_DEREF(z) do {								\
1302 		if (UNEXPECTED(Z_ISREF_P(z))) {					\
1303 			(z) = Z_REFVAL_P(z);						\
1304 		}												\
1305 	} while (0)
1306 
1307 #define ZVAL_DEINDIRECT(z) do {							\
1308 		if (Z_TYPE_P(z) == IS_INDIRECT) {				\
1309 			(z) = Z_INDIRECT_P(z);						\
1310 		}												\
1311 	} while (0)
1312 
1313 #define ZVAL_OPT_DEREF(z) do {							\
1314 		if (UNEXPECTED(Z_OPT_ISREF_P(z))) {				\
1315 			(z) = Z_REFVAL_P(z);						\
1316 		}												\
1317 	} while (0)
1318 
1319 #define ZVAL_MAKE_REF(zv) do {							\
1320 		zval *__zv = (zv);								\
1321 		if (!Z_ISREF_P(__zv)) {							\
1322 			ZVAL_NEW_REF(__zv, __zv);					\
1323 		}												\
1324 	} while (0)
1325 
1326 #define ZVAL_UNREF(z) do {								\
1327 		zval *_z = (z);									\
1328 		zend_reference *ref;							\
1329 		ZEND_ASSERT(Z_ISREF_P(_z));						\
1330 		ref = Z_REF_P(_z);								\
1331 		ZVAL_COPY_VALUE(_z, &ref->val);					\
1332 		efree_size(ref, sizeof(zend_reference));		\
1333 	} while (0)
1334 
1335 #define ZVAL_COPY_DEREF(z, v) do {						\
1336 		zval *_z3 = (v);								\
1337 		if (Z_OPT_REFCOUNTED_P(_z3)) {					\
1338 			if (UNEXPECTED(Z_OPT_ISREF_P(_z3))) {		\
1339 				_z3 = Z_REFVAL_P(_z3);					\
1340 				if (Z_OPT_REFCOUNTED_P(_z3)) {			\
1341 					Z_ADDREF_P(_z3);					\
1342 				}										\
1343 			} else {									\
1344 				Z_ADDREF_P(_z3);						\
1345 			}											\
1346 		}												\
1347 		ZVAL_COPY_VALUE(z, _z3);						\
1348 	} while (0)
1349 
1350 
1351 #define SEPARATE_STRING(zv) do {						\
1352 		zval *_zv = (zv);								\
1353 		if (Z_REFCOUNT_P(_zv) > 1) {					\
1354 			zend_string *_str = Z_STR_P(_zv);			\
1355 			ZEND_ASSERT(Z_REFCOUNTED_P(_zv));			\
1356 			ZEND_ASSERT(!ZSTR_IS_INTERNED(_str));		\
1357 			ZVAL_NEW_STR(_zv, zend_string_init(			\
1358 				ZSTR_VAL(_str),	ZSTR_LEN(_str), 0));	\
1359 			GC_DELREF(_str);							\
1360 		}												\
1361 	} while (0)
1362 
1363 #define SEPARATE_ARRAY(zv) do {							\
1364 		zval *__zv = (zv);								\
1365 		zend_array *_arr = Z_ARR_P(__zv);				\
1366 		if (UNEXPECTED(GC_REFCOUNT(_arr) > 1)) {		\
1367 			ZVAL_ARR(__zv, zend_array_dup(_arr));		\
1368 			GC_TRY_DELREF(_arr);						\
1369 		}												\
1370 	} while (0)
1371 
1372 #define SEPARATE_ZVAL_NOREF(zv) do {					\
1373 		zval *_zv = (zv);								\
1374 		ZEND_ASSERT(Z_TYPE_P(_zv) != IS_REFERENCE);		\
1375 		if (Z_TYPE_P(_zv) == IS_ARRAY) {				\
1376 			SEPARATE_ARRAY(_zv);						\
1377 		}												\
1378 	} while (0)
1379 
1380 #define SEPARATE_ZVAL(zv) do {							\
1381 		zval *_zv = (zv);								\
1382 		if (Z_ISREF_P(_zv)) {							\
1383 			zend_reference *_r = Z_REF_P(_zv);			\
1384 			ZVAL_COPY_VALUE(_zv, &_r->val);				\
1385 			if (GC_DELREF(_r) == 0) {					\
1386 				efree_size(_r, sizeof(zend_reference));	\
1387 			} else if (Z_OPT_TYPE_P(_zv) == IS_ARRAY) {	\
1388 				ZVAL_ARR(_zv, zend_array_dup(Z_ARR_P(_zv)));\
1389 				break;									\
1390 			} else if (Z_OPT_REFCOUNTED_P(_zv)) {		\
1391 				Z_ADDREF_P(_zv);						\
1392 				break;									\
1393 			}											\
1394 		}												\
1395 		if (Z_TYPE_P(_zv) == IS_ARRAY) {				\
1396 			SEPARATE_ARRAY(_zv);						\
1397 		}												\
1398 	} while (0)
1399 
1400 /* Properties store a flag distinguishing unset and uninitialized properties
1401  * (both use IS_UNDEF type) in the Z_EXTRA space. As such we also need to copy
1402  * the Z_EXTRA space when copying property default values etc. We define separate
1403  * macros for this purpose, so this workaround is easier to remove in the future. */
1404 #define IS_PROP_UNINIT 1
1405 #define Z_PROP_FLAG_P(z) Z_EXTRA_P(z)
1406 #define ZVAL_COPY_VALUE_PROP(z, v) \
1407 	do { *(z) = *(v); } while (0)
1408 #define ZVAL_COPY_PROP(z, v) \
1409 	do { ZVAL_COPY(z, v); Z_PROP_FLAG_P(z) = Z_PROP_FLAG_P(v); } while (0)
1410 #define ZVAL_COPY_OR_DUP_PROP(z, v) \
1411 	do { ZVAL_COPY_OR_DUP(z, v); Z_PROP_FLAG_P(z) = Z_PROP_FLAG_P(v); } while (0)
1412 
1413 
1414 #endif /* ZEND_TYPES_H */
1415