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