xref: /PHP-7.1/Zend/zend_operators.h (revision ccd4716e)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1998-2018 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@zend.com>                                |
16    |          Zeev Suraski <zeev@zend.com>                                |
17    |          Dmitry Stogov <dmitry@zend.com>                             |
18    +----------------------------------------------------------------------+
19 */
20 
21 /* $Id$ */
22 
23 #ifndef ZEND_OPERATORS_H
24 #define ZEND_OPERATORS_H
25 
26 #include <errno.h>
27 #include <math.h>
28 #include <assert.h>
29 
30 #ifdef __GNUC__
31 #include <stddef.h>
32 #endif
33 
34 #ifdef HAVE_IEEEFP_H
35 #include <ieeefp.h>
36 #endif
37 
38 #include "zend_strtod.h"
39 #include "zend_multiply.h"
40 
41 #if 0&&HAVE_BCMATH
42 #include "ext/bcmath/libbcmath/src/bcmath.h"
43 #endif
44 
45 #define LONG_SIGN_MASK (((zend_long)1) << (8*sizeof(zend_long)-1))
46 
47 BEGIN_EXTERN_C()
48 ZEND_API int ZEND_FASTCALL add_function(zval *result, zval *op1, zval *op2);
49 ZEND_API int ZEND_FASTCALL sub_function(zval *result, zval *op1, zval *op2);
50 ZEND_API int ZEND_FASTCALL mul_function(zval *result, zval *op1, zval *op2);
51 ZEND_API int ZEND_FASTCALL pow_function(zval *result, zval *op1, zval *op2);
52 ZEND_API int ZEND_FASTCALL div_function(zval *result, zval *op1, zval *op2);
53 ZEND_API int ZEND_FASTCALL mod_function(zval *result, zval *op1, zval *op2);
54 ZEND_API int ZEND_FASTCALL boolean_xor_function(zval *result, zval *op1, zval *op2);
55 ZEND_API int ZEND_FASTCALL boolean_not_function(zval *result, zval *op1);
56 ZEND_API int ZEND_FASTCALL bitwise_not_function(zval *result, zval *op1);
57 ZEND_API int ZEND_FASTCALL bitwise_or_function(zval *result, zval *op1, zval *op2);
58 ZEND_API int ZEND_FASTCALL bitwise_and_function(zval *result, zval *op1, zval *op2);
59 ZEND_API int ZEND_FASTCALL bitwise_xor_function(zval *result, zval *op1, zval *op2);
60 ZEND_API int ZEND_FASTCALL shift_left_function(zval *result, zval *op1, zval *op2);
61 ZEND_API int ZEND_FASTCALL shift_right_function(zval *result, zval *op1, zval *op2);
62 ZEND_API int ZEND_FASTCALL concat_function(zval *result, zval *op1, zval *op2);
63 
64 ZEND_API int ZEND_FASTCALL zend_is_identical(zval *op1, zval *op2);
65 
66 ZEND_API int ZEND_FASTCALL is_equal_function(zval *result, zval *op1, zval *op2);
67 ZEND_API int ZEND_FASTCALL is_identical_function(zval *result, zval *op1, zval *op2);
68 ZEND_API int ZEND_FASTCALL is_not_identical_function(zval *result, zval *op1, zval *op2);
69 ZEND_API int ZEND_FASTCALL is_not_equal_function(zval *result, zval *op1, zval *op2);
70 ZEND_API int ZEND_FASTCALL is_smaller_function(zval *result, zval *op1, zval *op2);
71 ZEND_API int ZEND_FASTCALL is_smaller_or_equal_function(zval *result, zval *op1, zval *op2);
72 
73 ZEND_API zend_bool ZEND_FASTCALL instanceof_function_ex(const zend_class_entry *instance_ce, const zend_class_entry *ce, zend_bool interfaces_only);
74 ZEND_API zend_bool ZEND_FASTCALL instanceof_function(const zend_class_entry *instance_ce, const zend_class_entry *ce);
75 
76 /**
77  * Checks whether the string "str" with length "length" is numeric. The value
78  * of allow_errors determines whether it's required to be entirely numeric, or
79  * just its prefix. Leading whitespace is allowed.
80  *
81  * The function returns 0 if the string did not contain a valid number; IS_LONG
82  * if it contained a number that fits within the range of a long; or IS_DOUBLE
83  * if the number was out of long range or contained a decimal point/exponent.
84  * The number's value is returned into the respective pointer, *lval or *dval,
85  * if that pointer is not NULL.
86  *
87  * This variant also gives information if a string that represents an integer
88  * could not be represented as such due to overflow. It writes 1 to oflow_info
89  * if the integer is larger than ZEND_LONG_MAX and -1 if it's smaller than ZEND_LONG_MIN.
90  */
91 ZEND_API zend_uchar ZEND_FASTCALL _is_numeric_string_ex(const char *str, size_t length, zend_long *lval, double *dval, int allow_errors, int *oflow_info);
92 
93 ZEND_API const char* ZEND_FASTCALL zend_memnstr_ex(const char *haystack, const char *needle, size_t needle_len, const char *end);
94 ZEND_API const char* ZEND_FASTCALL zend_memnrstr_ex(const char *haystack, const char *needle, size_t needle_len, const char *end);
95 
96 #if SIZEOF_ZEND_LONG == 4
97 #	define ZEND_DOUBLE_FITS_LONG(d) (!((d) > ZEND_LONG_MAX || (d) < ZEND_LONG_MIN))
98 #else
99 	/* >= as (double)ZEND_LONG_MAX is outside signed range */
100 #	define ZEND_DOUBLE_FITS_LONG(d) (!((d) >= ZEND_LONG_MAX || (d) < ZEND_LONG_MIN))
101 #endif
102 
103 #if ZEND_DVAL_TO_LVAL_CAST_OK
zend_dval_to_lval(double d)104 static zend_always_inline zend_long zend_dval_to_lval(double d)
105 {
106     if (EXPECTED(zend_finite(d)) && EXPECTED(!zend_isnan(d))) {
107         return (zend_long)d;
108     } else {
109         return 0;
110     }
111 }
112 #else
113 ZEND_API zend_long ZEND_FASTCALL zend_dval_to_lval_slow(double d);
114 
zend_dval_to_lval(double d)115 static zend_always_inline zend_long zend_dval_to_lval(double d)
116 {
117 	if (UNEXPECTED(!zend_finite(d)) || UNEXPECTED(zend_isnan(d))) {
118 		return 0;
119 	} else if (!ZEND_DOUBLE_FITS_LONG(d)) {
120 		return zend_dval_to_lval_slow(d);
121 	}
122 	return (zend_long)d;
123 }
124 #endif
125 
zend_dval_to_lval_cap(double d)126 static zend_always_inline zend_long zend_dval_to_lval_cap(double d)
127 {
128 	if (UNEXPECTED(!zend_finite(d)) || UNEXPECTED(zend_isnan(d))) {
129 		return 0;
130 	} else if (!ZEND_DOUBLE_FITS_LONG(d)) {
131 		return (d > 0 ? ZEND_LONG_MAX : ZEND_LONG_MIN);
132 	}
133 	return (zend_long)d;
134 }
135 /* }}} */
136 
137 #define ZEND_IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
138 #define ZEND_IS_XDIGIT(c) (((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))
139 
is_numeric_string_ex(const char * str,size_t length,zend_long * lval,double * dval,int allow_errors,int * oflow_info)140 static zend_always_inline zend_uchar is_numeric_string_ex(const char *str, size_t length, zend_long *lval, double *dval, int allow_errors, int *oflow_info)
141 {
142 	if (*str > '9') {
143 		return 0;
144 	}
145 	return _is_numeric_string_ex(str, length, lval, dval, allow_errors, oflow_info);
146 }
147 
is_numeric_string(const char * str,size_t length,zend_long * lval,double * dval,int allow_errors)148 static zend_always_inline zend_uchar is_numeric_string(const char *str, size_t length, zend_long *lval, double *dval, int allow_errors) {
149     return is_numeric_string_ex(str, length, lval, dval, allow_errors, NULL);
150 }
151 
152 ZEND_API zend_uchar ZEND_FASTCALL is_numeric_str_function(const zend_string *str, zend_long *lval, double *dval);
153 
154 static zend_always_inline const char *
zend_memnstr(const char * haystack,const char * needle,size_t needle_len,const char * end)155 zend_memnstr(const char *haystack, const char *needle, size_t needle_len, const char *end)
156 {
157 	const char *p = haystack;
158 	const char ne = needle[needle_len-1];
159 	ptrdiff_t off_p;
160 	size_t off_s;
161 
162 	if (needle_len == 1) {
163 		return (const char *)memchr(p, *needle, (end-p));
164 	}
165 
166 	off_p = end - haystack;
167 	off_s = (off_p > 0) ? (size_t)off_p : 0;
168 
169 	if (needle_len > off_s) {
170 		return NULL;
171 	}
172 
173 	if (EXPECTED(off_s < 1024 || needle_len < 9)) {	/* glibc memchr is faster when needle is too short */
174 		end -= needle_len;
175 
176 		while (p <= end) {
177 			if ((p = (const char *)memchr(p, *needle, (end-p+1))) && ne == p[needle_len-1]) {
178 				if (!memcmp(needle, p, needle_len-1)) {
179 					return p;
180 				}
181 			}
182 
183 			if (p == NULL) {
184 				return NULL;
185 			}
186 
187 			p++;
188 		}
189 
190 		return NULL;
191 	} else {
192 		return zend_memnstr_ex(haystack, needle, needle_len, end);
193 	}
194 }
195 
zend_memrchr(const void * s,int c,size_t n)196 static zend_always_inline const void *zend_memrchr(const void *s, int c, size_t n)
197 {
198 	const unsigned char *e;
199 	if (n <= 0) {
200 		return NULL;
201 	}
202 
203 	for (e = (const unsigned char *)s + n - 1; e >= (const unsigned char *)s; e--) {
204 		if (*e == (const unsigned char)c) {
205 			return (const void *)e;
206 		}
207 	}
208 	return NULL;
209 }
210 
211 
212 static zend_always_inline const char *
zend_memnrstr(const char * haystack,const char * needle,size_t needle_len,char * end)213 zend_memnrstr(const char *haystack, const char *needle, size_t needle_len, char *end)
214 {
215     const char *p = end;
216     const char ne = needle[needle_len-1];
217     ptrdiff_t off_p;
218     size_t off_s;
219 
220     if (needle_len == 1) {
221         return (const char *)zend_memrchr(haystack, *needle, (p - haystack));
222     }
223 
224     off_p = end - haystack;
225     off_s = (off_p > 0) ? (size_t)off_p : 0;
226 
227     if (needle_len > off_s) {
228         return NULL;
229     }
230 
231 	if (EXPECTED(off_s < 1024 || needle_len < 3)) {
232 		p -= needle_len;
233 
234 		do {
235 			if ((p = (const char *)zend_memrchr(haystack, *needle, (p - haystack) + 1)) && ne == p[needle_len-1]) {
236 				if (!memcmp(needle, p, needle_len - 1)) {
237 					return p;
238 				}
239 			}
240 		} while (p-- >= haystack);
241 
242 		return NULL;
243 	} else {
244 		return zend_memnrstr_ex(haystack, needle, needle_len, end);
245 	}
246 }
247 
248 ZEND_API int ZEND_FASTCALL increment_function(zval *op1);
249 ZEND_API int ZEND_FASTCALL decrement_function(zval *op2);
250 
251 ZEND_API void ZEND_FASTCALL convert_scalar_to_number(zval *op);
252 ZEND_API void ZEND_FASTCALL _convert_to_cstring(zval *op ZEND_FILE_LINE_DC);
253 ZEND_API void ZEND_FASTCALL _convert_to_string(zval *op ZEND_FILE_LINE_DC);
254 ZEND_API void ZEND_FASTCALL convert_to_long(zval *op);
255 ZEND_API void ZEND_FASTCALL convert_to_double(zval *op);
256 ZEND_API void ZEND_FASTCALL convert_to_long_base(zval *op, int base);
257 ZEND_API void ZEND_FASTCALL convert_to_null(zval *op);
258 ZEND_API void ZEND_FASTCALL convert_to_boolean(zval *op);
259 ZEND_API void ZEND_FASTCALL convert_to_array(zval *op);
260 ZEND_API void ZEND_FASTCALL convert_to_object(zval *op);
261 ZEND_API void multi_convert_to_long_ex(int argc, ...);
262 ZEND_API void multi_convert_to_double_ex(int argc, ...);
263 ZEND_API void multi_convert_to_string_ex(int argc, ...);
264 
265 ZEND_API zend_long    ZEND_FASTCALL _zval_get_long_func(zval *op);
266 ZEND_API double       ZEND_FASTCALL _zval_get_double_func(zval *op);
267 ZEND_API zend_string* ZEND_FASTCALL _zval_get_string_func(zval *op);
268 
_zval_get_long(zval * op)269 static zend_always_inline zend_long _zval_get_long(zval *op) {
270 	return Z_TYPE_P(op) == IS_LONG ? Z_LVAL_P(op) : _zval_get_long_func(op);
271 }
_zval_get_double(zval * op)272 static zend_always_inline double _zval_get_double(zval *op) {
273 	return Z_TYPE_P(op) == IS_DOUBLE ? Z_DVAL_P(op) : _zval_get_double_func(op);
274 }
_zval_get_string(zval * op)275 static zend_always_inline zend_string *_zval_get_string(zval *op) {
276 	return Z_TYPE_P(op) == IS_STRING ? zend_string_copy(Z_STR_P(op)) : _zval_get_string_func(op);
277 }
278 
279 #define zval_get_long(op) _zval_get_long((op))
280 #define zval_get_double(op) _zval_get_double((op))
281 #define zval_get_string(op) _zval_get_string((op))
282 
283 #define convert_to_cstring(op) if (Z_TYPE_P(op) != IS_STRING) { _convert_to_cstring((op) ZEND_FILE_LINE_CC); }
284 #define convert_to_string(op) if (Z_TYPE_P(op) != IS_STRING) { _convert_to_string((op) ZEND_FILE_LINE_CC); }
285 
286 
287 ZEND_API int ZEND_FASTCALL zend_is_true(zval *op);
288 ZEND_API int ZEND_FASTCALL zend_object_is_true(zval *op);
289 
290 #define zval_is_true(op) \
291 	zend_is_true(op)
292 
i_zend_is_true(zval * op)293 static zend_always_inline int i_zend_is_true(zval *op)
294 {
295 	int result = 0;
296 
297 again:
298 	switch (Z_TYPE_P(op)) {
299 		case IS_TRUE:
300 			result = 1;
301 			break;
302 		case IS_LONG:
303 			if (Z_LVAL_P(op)) {
304 				result = 1;
305 			}
306 			break;
307 		case IS_DOUBLE:
308 			if (Z_DVAL_P(op)) {
309 				result = 1;
310 			}
311 			break;
312 		case IS_STRING:
313 			if (Z_STRLEN_P(op) > 1 || (Z_STRLEN_P(op) && Z_STRVAL_P(op)[0] != '0')) {
314 				result = 1;
315 			}
316 			break;
317 		case IS_ARRAY:
318 			if (zend_hash_num_elements(Z_ARRVAL_P(op))) {
319 				result = 1;
320 			}
321 			break;
322 		case IS_OBJECT:
323 			result = zend_object_is_true(op);
324 			break;
325 		case IS_RESOURCE:
326 			if (EXPECTED(Z_RES_HANDLE_P(op))) {
327 				result = 1;
328 			}
329 			break;
330 		case IS_REFERENCE:
331 			op = Z_REFVAL_P(op);
332 			goto again;
333 			break;
334 		default:
335 			break;
336 	}
337 	return result;
338 }
339 
340 ZEND_API int ZEND_FASTCALL compare_function(zval *result, zval *op1, zval *op2);
341 
342 ZEND_API int ZEND_FASTCALL numeric_compare_function(zval *op1, zval *op2);
343 ZEND_API int ZEND_FASTCALL string_compare_function_ex(zval *op1, zval *op2, zend_bool case_insensitive);
344 ZEND_API int ZEND_FASTCALL string_compare_function(zval *op1, zval *op2);
345 ZEND_API int ZEND_FASTCALL string_case_compare_function(zval *op1, zval *op2);
346 #if HAVE_STRCOLL
347 ZEND_API int ZEND_FASTCALL string_locale_compare_function(zval *op1, zval *op2);
348 #endif
349 
350 ZEND_API void         ZEND_FASTCALL zend_str_tolower(char *str, size_t length);
351 ZEND_API char*        ZEND_FASTCALL zend_str_tolower_copy(char *dest, const char *source, size_t length);
352 ZEND_API char*        ZEND_FASTCALL zend_str_tolower_dup(const char *source, size_t length);
353 ZEND_API char*        ZEND_FASTCALL zend_str_tolower_dup_ex(const char *source, size_t length);
354 ZEND_API zend_string* ZEND_FASTCALL zend_string_tolower(zend_string *str);
355 
356 ZEND_API int ZEND_FASTCALL zend_binary_zval_strcmp(zval *s1, zval *s2);
357 ZEND_API int ZEND_FASTCALL zend_binary_zval_strncmp(zval *s1, zval *s2, zval *s3);
358 ZEND_API int ZEND_FASTCALL zend_binary_zval_strcasecmp(zval *s1, zval *s2);
359 ZEND_API int ZEND_FASTCALL zend_binary_zval_strncasecmp(zval *s1, zval *s2, zval *s3);
360 ZEND_API int ZEND_FASTCALL zend_binary_strcmp(const char *s1, size_t len1, const char *s2, size_t len2);
361 ZEND_API int ZEND_FASTCALL zend_binary_strncmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length);
362 ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp(const char *s1, size_t len1, const char *s2, size_t len2);
363 ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp(const char *s1, size_t len1, const char *s2, size_t len2, size_t length);
364 ZEND_API int ZEND_FASTCALL zend_binary_strcasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2);
365 ZEND_API int ZEND_FASTCALL zend_binary_strncasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2, size_t length);
366 
367 ZEND_API zend_long ZEND_FASTCALL zendi_smart_strcmp(zend_string *s1, zend_string *s2);
368 ZEND_API int ZEND_FASTCALL zend_compare_symbol_tables(HashTable *ht1, HashTable *ht2);
369 ZEND_API int ZEND_FASTCALL zend_compare_arrays(zval *a1, zval *a2);
370 ZEND_API int ZEND_FASTCALL zend_compare_objects(zval *o1, zval *o2);
371 
372 ZEND_API int ZEND_FASTCALL zend_atoi(const char *str, int str_len);
373 ZEND_API zend_long ZEND_FASTCALL zend_atol(const char *str, int str_len);
374 
375 ZEND_API void ZEND_FASTCALL zend_locale_sprintf_double(zval *op ZEND_FILE_LINE_DC);
376 
377 #define convert_to_ex_master(pzv, lower_type, upper_type)	\
378 	if (Z_TYPE_P(pzv)!=upper_type) {					\
379 		convert_to_##lower_type(pzv);						\
380 	}
381 
382 #define convert_to_explicit_type(pzv, type)		\
383 	do {										\
384 		switch (type) {							\
385 			case IS_NULL:						\
386 				convert_to_null(pzv);			\
387 				break;							\
388 			case IS_LONG:						\
389 				convert_to_long(pzv);			\
390 				break;							\
391 			case IS_DOUBLE:						\
392 				convert_to_double(pzv);			\
393 				break;							\
394 			case _IS_BOOL:						\
395 				convert_to_boolean(pzv);		\
396 				break;							\
397 			case IS_ARRAY:						\
398 				convert_to_array(pzv);			\
399 				break;							\
400 			case IS_OBJECT:						\
401 				convert_to_object(pzv);			\
402 				break;							\
403 			case IS_STRING:						\
404 				convert_to_string(pzv);			\
405 				break;							\
406 			default:							\
407 				assert(0);						\
408 				break;							\
409 		}										\
410 	} while (0);
411 
412 #define convert_to_explicit_type_ex(pzv, str_type)	\
413 	if (Z_TYPE_P(pzv) != str_type) {				\
414 		convert_to_explicit_type(pzv, str_type);	\
415 	}
416 
417 #define convert_to_boolean_ex(pzv)	convert_to_ex_master(pzv, boolean, _IS_BOOL)
418 #define convert_to_long_ex(pzv)		convert_to_ex_master(pzv, long, IS_LONG)
419 #define convert_to_double_ex(pzv)	convert_to_ex_master(pzv, double, IS_DOUBLE)
420 #define convert_to_string_ex(pzv)	convert_to_ex_master(pzv, string, IS_STRING)
421 #define convert_to_array_ex(pzv)	convert_to_ex_master(pzv, array, IS_ARRAY)
422 #define convert_to_object_ex(pzv)	convert_to_ex_master(pzv, object, IS_OBJECT)
423 #define convert_to_null_ex(pzv)		convert_to_ex_master(pzv, null, IS_NULL)
424 
425 #define convert_scalar_to_number_ex(pzv)							\
426 	if (Z_TYPE_P(pzv)!=IS_LONG && Z_TYPE_P(pzv)!=IS_DOUBLE) {		\
427 		convert_scalar_to_number(pzv);					\
428 	}
429 
430 #if HAVE_SETLOCALE && defined(ZEND_WIN32) && !defined(ZTS) && defined(_MSC_VER)
431 /* This performance improvement of tolower() on Windows gives 10-18% on bench.php */
432 #define ZEND_USE_TOLOWER_L 1
433 #endif
434 
435 #ifdef ZEND_USE_TOLOWER_L
436 ZEND_API void zend_update_current_locale(void);
437 #else
438 #define zend_update_current_locale()
439 #endif
440 
441 /* The offset in bytes between the value and type fields of a zval */
442 #define ZVAL_OFFSETOF_TYPE	\
443 	(offsetof(zval, u1.type_info) - offsetof(zval, value))
444 
fast_long_increment_function(zval * op1)445 static zend_always_inline void fast_long_increment_function(zval *op1)
446 {
447 #if defined(__GNUC__) && defined(__i386__)
448 	__asm__(
449 		"incl (%0)\n\t"
450 		"jno  0f\n\t"
451 		"movl $0x0, (%0)\n\t"
452 		"movl $0x41e00000, 0x4(%0)\n\t"
453 		"movl %1, %c2(%0)\n"
454 		"0:"
455 		:
456 		: "r"(&op1->value),
457 		  "n"(IS_DOUBLE),
458 		  "n"(ZVAL_OFFSETOF_TYPE)
459 		: "cc", "memory");
460 #elif defined(__GNUC__) && defined(__x86_64__)
461 	__asm__(
462 		"incq (%0)\n\t"
463 		"jno  0f\n\t"
464 		"movl $0x0, (%0)\n\t"
465 		"movl $0x43e00000, 0x4(%0)\n\t"
466 		"movl %1, %c2(%0)\n"
467 		"0:"
468 		:
469 		: "r"(&op1->value),
470 		  "n"(IS_DOUBLE),
471 		  "n"(ZVAL_OFFSETOF_TYPE)
472 		: "cc", "memory");
473 #else
474 	if (UNEXPECTED(Z_LVAL_P(op1) == ZEND_LONG_MAX)) {
475 		/* switch to double */
476 		ZVAL_DOUBLE(op1, (double)ZEND_LONG_MAX + 1.0);
477 	} else {
478 		Z_LVAL_P(op1)++;
479 	}
480 #endif
481 }
482 
fast_long_decrement_function(zval * op1)483 static zend_always_inline void fast_long_decrement_function(zval *op1)
484 {
485 #if defined(__GNUC__) && defined(__i386__)
486 	__asm__(
487 		"decl (%0)\n\t"
488 		"jno  0f\n\t"
489 		"movl $0x00200000, (%0)\n\t"
490 		"movl $0xc1e00000, 0x4(%0)\n\t"
491 		"movl %1,%c2(%0)\n"
492 		"0:"
493 		:
494 		: "r"(&op1->value),
495 		  "n"(IS_DOUBLE),
496 		  "n"(ZVAL_OFFSETOF_TYPE)
497 		: "cc", "memory");
498 #elif defined(__GNUC__) && defined(__x86_64__)
499 	__asm__(
500 		"decq (%0)\n\t"
501 		"jno  0f\n\t"
502 		"movl $0x00000000, (%0)\n\t"
503 		"movl $0xc3e00000, 0x4(%0)\n\t"
504 		"movl %1,%c2(%0)\n"
505 		"0:"
506 		:
507 		: "r"(&op1->value),
508 		  "n"(IS_DOUBLE),
509 		  "n"(ZVAL_OFFSETOF_TYPE)
510 		: "cc", "memory");
511 #else
512 	if (UNEXPECTED(Z_LVAL_P(op1) == ZEND_LONG_MIN)) {
513 		/* switch to double */
514 		ZVAL_DOUBLE(op1, (double)ZEND_LONG_MIN - 1.0);
515 	} else {
516 		Z_LVAL_P(op1)--;
517 	}
518 #endif
519 }
520 
fast_long_add_function(zval * result,zval * op1,zval * op2)521 static zend_always_inline void fast_long_add_function(zval *result, zval *op1, zval *op2)
522 {
523 #if defined(__GNUC__) && defined(__i386__) \
524 	&& !(4 == __GNUC__ && 8 == __GNUC_MINOR__) \
525 	&& !(4 == __GNUC__ && 9 == __GNUC_MINOR__ && (defined(__PIC__) || defined(__PIE__)))
526 	/* Position-independent builds fail with gcc-4.9.x */
527 	__asm__(
528 		"movl	(%1), %%eax\n\t"
529 		"addl   (%2), %%eax\n\t"
530 		"jo     0f\n\t"
531 		"movl   %%eax, (%0)\n\t"
532 		"movl   %3, %c5(%0)\n\t"
533 		"jmp    1f\n"
534 		"0:\n\t"
535 		"fildl	(%1)\n\t"
536 		"fildl	(%2)\n\t"
537 		"faddp	%%st, %%st(1)\n\t"
538 		"movl   %4, %c5(%0)\n\t"
539 		"fstpl	(%0)\n"
540 		"1:"
541 		:
542 		: "r"(&result->value),
543 		  "r"(&op1->value),
544 		  "r"(&op2->value),
545 		  "n"(IS_LONG),
546 		  "n"(IS_DOUBLE),
547 		  "n"(ZVAL_OFFSETOF_TYPE)
548 		: "eax","cc", "memory");
549 #elif defined(__GNUC__) && defined(__x86_64__)
550 	__asm__(
551 		"movq	(%1), %%rax\n\t"
552 		"addq   (%2), %%rax\n\t"
553 		"jo     0f\n\t"
554 		"movq   %%rax, (%0)\n\t"
555 		"movl   %3, %c5(%0)\n\t"
556 		"jmp    1f\n"
557 		"0:\n\t"
558 		"fildq	(%1)\n\t"
559 		"fildq	(%2)\n\t"
560 		"faddp	%%st, %%st(1)\n\t"
561 		"movl   %4, %c5(%0)\n\t"
562 		"fstpl	(%0)\n"
563 		"1:"
564 		:
565 		: "r"(&result->value),
566 		  "r"(&op1->value),
567 		  "r"(&op2->value),
568 		  "n"(IS_LONG),
569 		  "n"(IS_DOUBLE),
570 		  "n"(ZVAL_OFFSETOF_TYPE)
571 		: "rax","cc", "memory");
572 #else
573 	/*
574 	 * 'result' may alias with op1 or op2, so we need to
575 	 * ensure that 'result' is not updated until after we
576 	 * have read the values of op1 and op2.
577 	 */
578 
579 	if (UNEXPECTED((Z_LVAL_P(op1) & LONG_SIGN_MASK) == (Z_LVAL_P(op2) & LONG_SIGN_MASK)
580 		&& (Z_LVAL_P(op1) & LONG_SIGN_MASK) != ((Z_LVAL_P(op1) + Z_LVAL_P(op2)) & LONG_SIGN_MASK))) {
581 		ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2));
582 	} else {
583 		ZVAL_LONG(result, Z_LVAL_P(op1) + Z_LVAL_P(op2));
584 	}
585 #endif
586 }
587 
fast_add_function(zval * result,zval * op1,zval * op2)588 static zend_always_inline int fast_add_function(zval *result, zval *op1, zval *op2)
589 {
590 	if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
591 		if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
592 			fast_long_add_function(result, op1, op2);
593 			return SUCCESS;
594 		} else if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
595 			ZVAL_DOUBLE(result, ((double)Z_LVAL_P(op1)) + Z_DVAL_P(op2));
596 			return SUCCESS;
597 		}
598 	} else if (EXPECTED(Z_TYPE_P(op1) == IS_DOUBLE)) {
599 		if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
600 			ZVAL_DOUBLE(result, Z_DVAL_P(op1) + Z_DVAL_P(op2));
601 			return SUCCESS;
602 		} else if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
603 			ZVAL_DOUBLE(result, Z_DVAL_P(op1) + ((double)Z_LVAL_P(op2)));
604 			return SUCCESS;
605 		}
606 	}
607 	return add_function(result, op1, op2);
608 }
609 
fast_long_sub_function(zval * result,zval * op1,zval * op2)610 static zend_always_inline void fast_long_sub_function(zval *result, zval *op1, zval *op2)
611 {
612 #if defined(__GNUC__) && defined(__i386__) && \
613 	!(4 == __GNUC__ && 8 == __GNUC_MINOR__) && \
614 	!(4 == __GNUC__ && 9 == __GNUC_MINOR__ && (defined(__PIC__) || defined(__PIE__)))
615 	/* Position-independent builds fail with gcc-4.9.x */
616 	__asm__(
617 		"movl	(%1), %%eax\n\t"
618 		"subl   (%2), %%eax\n\t"
619 		"jo     0f\n\t"
620 		"movl   %%eax, (%0)\n\t"
621 		"movl   %3, %c5(%0)\n\t"
622 		"jmp    1f\n"
623 		"0:\n\t"
624 		"fildl	(%2)\n\t"
625 		"fildl	(%1)\n\t"
626 #if defined(__clang__) && (__clang_major__ < 2 || (__clang_major__ == 2 && __clang_minor__ < 10))
627 		"fsubp  %%st(1), %%st\n\t"  /* LLVM bug #9164 */
628 #else
629 		"fsubp	%%st, %%st(1)\n\t"
630 #endif
631 		"movl   %4, %c5(%0)\n\t"
632 		"fstpl	(%0)\n"
633 		"1:"
634 		:
635 		: "r"(&result->value),
636 		  "r"(&op1->value),
637 		  "r"(&op2->value),
638 		  "n"(IS_LONG),
639 		  "n"(IS_DOUBLE),
640 		  "n"(ZVAL_OFFSETOF_TYPE)
641 		: "eax","cc", "memory");
642 #elif defined(__GNUC__) && defined(__x86_64__)
643 	__asm__(
644 		"movq	(%1), %%rax\n\t"
645 		"subq   (%2), %%rax\n\t"
646 		"jo     0f\n\t"
647 		"movq   %%rax, (%0)\n\t"
648 		"movl   %3, %c5(%0)\n\t"
649 		"jmp    1f\n"
650 		"0:\n\t"
651 		"fildq	(%2)\n\t"
652 		"fildq	(%1)\n\t"
653 #if defined(__clang__) && (__clang_major__ < 2 || (__clang_major__ == 2 && __clang_minor__ < 10))
654 		"fsubp  %%st(1), %%st\n\t"  /* LLVM bug #9164 */
655 #else
656 		"fsubp	%%st, %%st(1)\n\t"
657 #endif
658 		"movl   %4, %c5(%0)\n\t"
659 		"fstpl	(%0)\n"
660 		"1:"
661 		:
662 		: "r"(&result->value),
663 		  "r"(&op1->value),
664 		  "r"(&op2->value),
665 		  "n"(IS_LONG),
666 		  "n"(IS_DOUBLE),
667 		  "n"(ZVAL_OFFSETOF_TYPE)
668 		: "rax","cc", "memory");
669 #else
670 	ZVAL_LONG(result, Z_LVAL_P(op1) - Z_LVAL_P(op2));
671 
672 	if (UNEXPECTED((Z_LVAL_P(op1) & LONG_SIGN_MASK) != (Z_LVAL_P(op2) & LONG_SIGN_MASK)
673 		&& (Z_LVAL_P(op1) & LONG_SIGN_MASK) != (Z_LVAL_P(result) & LONG_SIGN_MASK))) {
674 		ZVAL_DOUBLE(result, (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2));
675 	}
676 #endif
677 }
678 
fast_div_function(zval * result,zval * op1,zval * op2)679 static zend_always_inline int fast_div_function(zval *result, zval *op1, zval *op2)
680 {
681 	return div_function(result, op1, op2);
682 }
683 
fast_equal_check_function(zval * op1,zval * op2)684 static zend_always_inline int fast_equal_check_function(zval *op1, zval *op2)
685 {
686 	zval result;
687 	if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
688 		if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
689 			return Z_LVAL_P(op1) == Z_LVAL_P(op2);
690 		} else if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
691 			return ((double)Z_LVAL_P(op1)) == Z_DVAL_P(op2);
692 		}
693 	} else if (EXPECTED(Z_TYPE_P(op1) == IS_DOUBLE)) {
694 		if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
695 			return Z_DVAL_P(op1) == Z_DVAL_P(op2);
696 		} else if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
697 			return Z_DVAL_P(op1) == ((double)Z_LVAL_P(op2));
698 		}
699 	} else if (EXPECTED(Z_TYPE_P(op1) == IS_STRING)) {
700 		if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
701 			if (Z_STR_P(op1) == Z_STR_P(op2)) {
702 				return 1;
703 			} else if (Z_STRVAL_P(op1)[0] > '9' || Z_STRVAL_P(op2)[0] > '9') {
704 				if (Z_STRLEN_P(op1) != Z_STRLEN_P(op2)) {
705 					return 0;
706 				} else {
707 					return memcmp(Z_STRVAL_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op1)) == 0;
708 				}
709 			} else {
710 				return zendi_smart_strcmp(Z_STR_P(op1), Z_STR_P(op2)) == 0;
711 			}
712 		}
713 	}
714 	compare_function(&result, op1, op2);
715 	return Z_LVAL(result) == 0;
716 }
717 
fast_equal_check_long(zval * op1,zval * op2)718 static zend_always_inline int fast_equal_check_long(zval *op1, zval *op2)
719 {
720 	zval result;
721 	if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
722 		return Z_LVAL_P(op1) == Z_LVAL_P(op2);
723 	}
724 	compare_function(&result, op1, op2);
725 	return Z_LVAL(result) == 0;
726 }
727 
fast_equal_check_string(zval * op1,zval * op2)728 static zend_always_inline int fast_equal_check_string(zval *op1, zval *op2)
729 {
730 	zval result;
731 	if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) {
732 		if (Z_STR_P(op1) == Z_STR_P(op2)) {
733 			return 1;
734 		} else if (Z_STRVAL_P(op1)[0] > '9' || Z_STRVAL_P(op2)[0] > '9') {
735 			if (Z_STRLEN_P(op1) != Z_STRLEN_P(op2)) {
736 				return 0;
737 			} else {
738 				return memcmp(Z_STRVAL_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op1)) == 0;
739 			}
740 		} else {
741 			return zendi_smart_strcmp(Z_STR_P(op1), Z_STR_P(op2)) == 0;
742 		}
743 	}
744 	compare_function(&result, op1, op2);
745 	return Z_LVAL(result) == 0;
746 }
747 
fast_is_identical_function(zval * op1,zval * op2)748 static zend_always_inline int fast_is_identical_function(zval *op1, zval *op2)
749 {
750 	if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) {
751 		return 0;
752 	} else if (Z_TYPE_P(op1) <= IS_TRUE) {
753 		return 1;
754 	}
755 	return zend_is_identical(op1, op2);
756 }
757 
fast_is_not_identical_function(zval * op1,zval * op2)758 static zend_always_inline int fast_is_not_identical_function(zval *op1, zval *op2)
759 {
760 	if (Z_TYPE_P(op1) != Z_TYPE_P(op2)) {
761 		return 1;
762 	} else if (Z_TYPE_P(op1) <= IS_TRUE) {
763 		return 0;
764 	}
765 	return !zend_is_identical(op1, op2);
766 }
767 
768 #define ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode, binary_op)                                            \
769 	if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT)                                                             \
770 		&& op1 == result                                                                                   \
771 		&& UNEXPECTED(Z_OBJ_HANDLER_P(op1, get))                                                           \
772 		&& EXPECTED(Z_OBJ_HANDLER_P(op1, set))) {                                                          \
773 		int ret;                                                                                           \
774 		zval rv;                                                                                           \
775 		zval *objval = Z_OBJ_HANDLER_P(op1, get)(op1, &rv);                                      \
776 		Z_TRY_ADDREF_P(objval);                                                                                \
777 		ret = binary_op(objval, objval, op2);                                                    \
778 		Z_OBJ_HANDLER_P(op1, set)(op1, objval);                                                  \
779 		zval_ptr_dtor(objval);                                                                             \
780 		return ret;                                                                                        \
781 	} else if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT)                                                      \
782 		&& UNEXPECTED(Z_OBJ_HANDLER_P(op1, do_operation))) {                                               \
783 		if (EXPECTED(SUCCESS == Z_OBJ_HANDLER_P(op1, do_operation)(opcode, result, op1, op2))) { \
784 			return SUCCESS;                                                                                \
785 		}                                                                                                  \
786 	}
787 
788 #define ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode)                                                       \
789 	if (UNEXPECTED(Z_TYPE_P(op2) == IS_OBJECT)                                                             \
790 		&& UNEXPECTED(Z_OBJ_HANDLER_P(op2, do_operation))                                                  \
791 		&& EXPECTED(SUCCESS == Z_OBJ_HANDLER_P(op2, do_operation)(opcode, result, op1, op2))) {  \
792 		return SUCCESS;                                                                                    \
793 	}
794 
795 #define ZEND_TRY_BINARY_OBJECT_OPERATION(opcode, binary_op)                                                \
796 	ZEND_TRY_BINARY_OP1_OBJECT_OPERATION(opcode, binary_op)                                                \
797 	else                                                                                                   \
798 	ZEND_TRY_BINARY_OP2_OBJECT_OPERATION(opcode)
799 
800 #define ZEND_TRY_UNARY_OBJECT_OPERATION(opcode)                                                            \
801 	if (UNEXPECTED(Z_TYPE_P(op1) == IS_OBJECT)                                                             \
802 		&& UNEXPECTED(Z_OBJ_HANDLER_P(op1, do_operation))                                                  \
803 		&& EXPECTED(SUCCESS == Z_OBJ_HANDLER_P(op1, do_operation)(opcode, result, op1, NULL))) { \
804 		return SUCCESS;                                                                                    \
805 	}
806 
807 /* buf points to the END of the buffer */
zend_print_ulong_to_buf(char * buf,zend_ulong num)808 static zend_always_inline char *zend_print_ulong_to_buf(char *buf, zend_ulong num) {
809 	*buf = '\0';
810 	do {
811 		*--buf = (char) (num % 10) + '0';
812 		num /= 10;
813 	} while (num > 0);
814 	return buf;
815 }
816 
817 /* buf points to the END of the buffer */
zend_print_long_to_buf(char * buf,zend_long num)818 static zend_always_inline char *zend_print_long_to_buf(char *buf, zend_long num) {
819 	if (num < 0) {
820 	    char *result = zend_print_ulong_to_buf(buf, ~((zend_ulong) num) + 1);
821 	    *--result = '-';
822 		return result;
823 	} else {
824 	    return zend_print_ulong_to_buf(buf, num);
825 	}
826 }
827 
828 ZEND_API zend_string* ZEND_FASTCALL zend_long_to_str(zend_long num);
829 
zend_unwrap_reference(zval * op)830 static zend_always_inline void zend_unwrap_reference(zval *op) /* {{{ */
831 {
832 	if (Z_REFCOUNT_P(op) == 1) {
833 		ZVAL_UNREF(op);
834 	} else {
835 		Z_DELREF_P(op);
836 		ZVAL_COPY(op, Z_REFVAL_P(op));
837 	}
838 }
839 /* }}} */
840 
841 
842 END_EXTERN_C()
843 
844 #endif
845 
846 /*
847  * Local variables:
848  * tab-width: 4
849  * c-basic-offset: 4
850  * indent-tabs-mode: t
851  * End:
852  */
853