1 /*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1998-2015 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 +----------------------------------------------------------------------+
18 */
19
20 /* $Id$ */
21
22 #ifndef ZEND_OPERATORS_H
23 #define ZEND_OPERATORS_H
24
25 #include <errno.h>
26 #include <math.h>
27 #include <assert.h>
28
29 #ifdef __GNUC__
30 #include <stddef.h>
31 #endif
32
33 #ifdef HAVE_IEEEFP_H
34 #include <ieeefp.h>
35 #endif
36
37 #include "zend_strtod.h"
38 #include "zend_multiply.h"
39
40 #if 0&&HAVE_BCMATH
41 #include "ext/bcmath/libbcmath/src/bcmath.h"
42 #endif
43
44 #define LONG_SIGN_MASK (1L << (8*sizeof(long)-1))
45
46 BEGIN_EXTERN_C()
47 ZEND_API int add_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
48 ZEND_API int sub_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
49 ZEND_API int mul_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
50 ZEND_API int div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
51 ZEND_API int mod_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
52 ZEND_API int boolean_xor_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
53 ZEND_API int boolean_not_function(zval *result, zval *op1 TSRMLS_DC);
54 ZEND_API int bitwise_not_function(zval *result, zval *op1 TSRMLS_DC);
55 ZEND_API int bitwise_or_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
56 ZEND_API int bitwise_and_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
57 ZEND_API int bitwise_xor_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
58 ZEND_API int shift_left_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
59 ZEND_API int shift_right_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
60 ZEND_API int concat_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
61
62 ZEND_API int is_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
63 ZEND_API int is_identical_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
64 ZEND_API int is_not_identical_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
65 ZEND_API int is_not_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
66 ZEND_API int is_smaller_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
67 ZEND_API int is_smaller_or_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
68
69 ZEND_API zend_bool instanceof_function_ex(const zend_class_entry *instance_ce, const zend_class_entry *ce, zend_bool interfaces_only TSRMLS_DC);
70 ZEND_API zend_bool instanceof_function(const zend_class_entry *instance_ce, const zend_class_entry *ce TSRMLS_DC);
END_EXTERN_C()71 END_EXTERN_C()
72
73 #if ZEND_DVAL_TO_LVAL_CAST_OK
74 # define zend_dval_to_lval(d) ((long) (d))
75 #elif SIZEOF_LONG == 4
76 static zend_always_inline long zend_dval_to_lval(double d)
77 {
78 if (d > LONG_MAX || d < LONG_MIN) {
79 double two_pow_32 = pow(2., 32.),
80 dmod;
81
82 dmod = fmod(d, two_pow_32);
83 if (dmod < 0) {
84 /* we're going to make this number positive; call ceil()
85 * to simulate rounding towards 0 of the negative number */
86 dmod = ceil(dmod) + two_pow_32;
87 }
88 return (long)(unsigned long)dmod;
89 }
90 return (long)d;
91 }
92 #else
93 static zend_always_inline long zend_dval_to_lval(double d)
94 {
95 /* >= as (double)LONG_MAX is outside signed range */
96 if (d >= LONG_MAX || d < LONG_MIN) {
97 double two_pow_64 = pow(2., 64.),
98 dmod;
99
100 dmod = fmod(d, two_pow_64);
101 if (dmod < 0) {
102 /* no need to call ceil; original double must have had no
103 * fractional part, hence dmod does not have one either */
104 dmod += two_pow_64;
105 }
106 return (long)(unsigned long)dmod;
107 }
108 return (long)d;
109 }
110 #endif
111 /* }}} */
112
113 #define ZEND_IS_DIGIT(c) ((c) >= '0' && (c) <= '9')
114 #define ZEND_IS_XDIGIT(c) (((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))
115
116 /**
117 * Checks whether the string "str" with length "length" is numeric. The value
118 * of allow_errors determines whether it's required to be entirely numeric, or
119 * just its prefix. Leading whitespace is allowed.
120 *
121 * The function returns 0 if the string did not contain a valid number; IS_LONG
122 * if it contained a number that fits within the range of a long; or IS_DOUBLE
123 * if the number was out of long range or contained a decimal point/exponent.
124 * The number's value is returned into the respective pointer, *lval or *dval,
125 * if that pointer is not NULL.
126 *
127 * This variant also gives information if a string that represents an integer
128 * could not be represented as such due to overflow. It writes 1 to oflow_info
129 * if the integer is larger than LONG_MAX and -1 if it's smaller than LONG_MIN.
130 */
131 static inline zend_uchar is_numeric_string_ex(const char *str, int length, long *lval, double *dval, int allow_errors, int *oflow_info)
132 {
133 const char *ptr;
134 int base = 10, digits = 0, dp_or_e = 0;
135 double local_dval = 0.0;
136 zend_uchar type;
137
138 if (!length) {
139 return 0;
140 }
141
142 if (oflow_info != NULL) {
143 *oflow_info = 0;
144 }
145
146 /* Skip any whitespace
147 * This is much faster than the isspace() function */
148 while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r' || *str == '\v' || *str == '\f') {
149 str++;
150 length--;
151 }
152 ptr = str;
153
154 if (*ptr == '-' || *ptr == '+') {
155 ptr++;
156 }
157
158 if (ZEND_IS_DIGIT(*ptr)) {
159 /* Handle hex numbers
160 * str is used instead of ptr to disallow signs and keep old behavior */
161 if (length > 2 && *str == '0' && (str[1] == 'x' || str[1] == 'X')) {
162 base = 16;
163 ptr += 2;
164 }
165
166 /* Skip any leading 0s */
167 while (*ptr == '0') {
168 ptr++;
169 }
170
171 /* Count the number of digits. If a decimal point/exponent is found,
172 * it's a double. Otherwise, if there's a dval or no need to check for
173 * a full match, stop when there are too many digits for a long */
174 for (type = IS_LONG; !(digits >= MAX_LENGTH_OF_LONG && (dval || allow_errors == 1)); digits++, ptr++) {
175 check_digits:
176 if (ZEND_IS_DIGIT(*ptr) || (base == 16 && ZEND_IS_XDIGIT(*ptr))) {
177 continue;
178 } else if (base == 10) {
179 if (*ptr == '.' && dp_or_e < 1) {
180 goto process_double;
181 } else if ((*ptr == 'e' || *ptr == 'E') && dp_or_e < 2) {
182 const char *e = ptr + 1;
183
184 if (*e == '-' || *e == '+') {
185 ptr = e++;
186 }
187 if (ZEND_IS_DIGIT(*e)) {
188 goto process_double;
189 }
190 }
191 }
192
193 break;
194 }
195
196 if (base == 10) {
197 if (digits >= MAX_LENGTH_OF_LONG) {
198 if (oflow_info != NULL) {
199 *oflow_info = *str == '-' ? -1 : 1;
200 }
201 dp_or_e = -1;
202 goto process_double;
203 }
204 } else if (!(digits < SIZEOF_LONG * 2 || (digits == SIZEOF_LONG * 2 && ptr[-digits] <= '7'))) {
205 if (dval) {
206 local_dval = zend_hex_strtod(str, &ptr);
207 }
208 if (oflow_info != NULL) {
209 *oflow_info = 1;
210 }
211 type = IS_DOUBLE;
212 }
213 } else if (*ptr == '.' && ZEND_IS_DIGIT(ptr[1])) {
214 process_double:
215 type = IS_DOUBLE;
216
217 /* If there's a dval, do the conversion; else continue checking
218 * the digits if we need to check for a full match */
219 if (dval) {
220 local_dval = zend_strtod(str, &ptr);
221 } else if (allow_errors != 1 && dp_or_e != -1) {
222 dp_or_e = (*ptr++ == '.') ? 1 : 2;
223 goto check_digits;
224 }
225 } else {
226 return 0;
227 }
228
229 if (ptr != str + length) {
230 if (!allow_errors) {
231 return 0;
232 }
233 if (allow_errors == -1) {
234 zend_error(E_NOTICE, "A non well formed numeric value encountered");
235 }
236 }
237
238 if (type == IS_LONG) {
239 if (digits == MAX_LENGTH_OF_LONG - 1) {
240 int cmp = strcmp(&ptr[-digits], long_min_digits);
241
242 if (!(cmp < 0 || (cmp == 0 && *str == '-'))) {
243 if (dval) {
244 *dval = zend_strtod(str, NULL);
245 }
246 if (oflow_info != NULL) {
247 *oflow_info = *str == '-' ? -1 : 1;
248 }
249
250 return IS_DOUBLE;
251 }
252 }
253
254 if (lval) {
255 *lval = strtol(str, NULL, base);
256 }
257
258 return IS_LONG;
259 } else {
260 if (dval) {
261 *dval = local_dval;
262 }
263
264 return IS_DOUBLE;
265 }
266 }
267
is_numeric_string(const char * str,int length,long * lval,double * dval,int allow_errors)268 static inline zend_uchar is_numeric_string(const char *str, int length, long *lval, double *dval, int allow_errors) {
269 return is_numeric_string_ex(str, length, lval, dval, allow_errors, NULL);
270 }
271
272 static inline char *
zend_memnstr(char * haystack,char * needle,int needle_len,char * end)273 zend_memnstr(char *haystack, char *needle, int needle_len, char *end)
274 {
275 char *p = haystack;
276 char ne = needle[needle_len-1];
277
278 if (needle_len == 1) {
279 return (char *)memchr(p, *needle, (end-p));
280 }
281
282 if (needle_len > end-haystack) {
283 return NULL;
284 }
285
286 end -= needle_len;
287
288 while (p <= end) {
289 if ((p = (char *)memchr(p, *needle, (end-p+1))) && ne == p[needle_len-1]) {
290 if (!memcmp(needle, p, needle_len-1)) {
291 return p;
292 }
293 }
294
295 if (p == NULL) {
296 return NULL;
297 }
298
299 p++;
300 }
301
302 return NULL;
303 }
304
zend_memrchr(const void * s,int c,size_t n)305 static inline const void *zend_memrchr(const void *s, int c, size_t n)
306 {
307 register const unsigned char *e;
308
309 if (n <= 0) {
310 return NULL;
311 }
312
313 for (e = (const unsigned char *)s + n - 1; e >= (const unsigned char *)s; e--) {
314 if (*e == (const unsigned char)c) {
315 return (const void *)e;
316 }
317 }
318
319 return NULL;
320 }
321
322 BEGIN_EXTERN_C()
323 ZEND_API int increment_function(zval *op1);
324 ZEND_API int decrement_function(zval *op2);
325
326 ZEND_API void convert_scalar_to_number(zval *op TSRMLS_DC);
327 ZEND_API void _convert_to_cstring(zval *op ZEND_FILE_LINE_DC);
328 ZEND_API void _convert_to_string(zval *op ZEND_FILE_LINE_DC);
329 ZEND_API void convert_to_long(zval *op);
330 ZEND_API void convert_to_double(zval *op);
331 ZEND_API void convert_to_long_base(zval *op, int base);
332 ZEND_API void convert_to_null(zval *op);
333 ZEND_API void convert_to_boolean(zval *op);
334 ZEND_API void convert_to_array(zval *op);
335 ZEND_API void convert_to_object(zval *op);
336 ZEND_API void multi_convert_to_long_ex(int argc, ...);
337 ZEND_API void multi_convert_to_double_ex(int argc, ...);
338 ZEND_API void multi_convert_to_string_ex(int argc, ...);
339 ZEND_API int add_char_to_string(zval *result, const zval *op1, const zval *op2);
340 ZEND_API int add_string_to_string(zval *result, const zval *op1, const zval *op2);
341 #define convert_to_cstring(op) if ((op)->type != IS_STRING) { _convert_to_cstring((op) ZEND_FILE_LINE_CC); }
342 #define convert_to_string(op) if ((op)->type != IS_STRING) { _convert_to_string((op) ZEND_FILE_LINE_CC); }
343
344 ZEND_API double zend_string_to_double(const char *number, zend_uint length);
345
346 ZEND_API int zval_is_true(zval *op);
347 ZEND_API int compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
348 ZEND_API int numeric_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
349 ZEND_API int string_compare_function_ex(zval *result, zval *op1, zval *op2, zend_bool case_insensitive TSRMLS_DC);
350 ZEND_API int string_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
351 ZEND_API int string_case_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
352 #if HAVE_STRCOLL
353 ZEND_API int string_locale_compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC);
354 #endif
355
356 ZEND_API void zend_str_tolower(char *str, unsigned int length);
357 ZEND_API char *zend_str_tolower_copy(char *dest, const char *source, unsigned int length);
358 ZEND_API char *zend_str_tolower_dup(const char *source, unsigned int length);
359
360 ZEND_API int zend_binary_zval_strcmp(zval *s1, zval *s2);
361 ZEND_API int zend_binary_zval_strncmp(zval *s1, zval *s2, zval *s3);
362 ZEND_API int zend_binary_zval_strcasecmp(zval *s1, zval *s2);
363 ZEND_API int zend_binary_zval_strncasecmp(zval *s1, zval *s2, zval *s3);
364 ZEND_API int zend_binary_strcmp(const char *s1, uint len1, const char *s2, uint len2);
365 ZEND_API int zend_binary_strncmp(const char *s1, uint len1, const char *s2, uint len2, uint length);
366 ZEND_API int zend_binary_strcasecmp(const char *s1, uint len1, const char *s2, uint len2);
367 ZEND_API int zend_binary_strncasecmp(const char *s1, uint len1, const char *s2, uint len2, uint length);
368 ZEND_API int zend_binary_strncasecmp_l(const char *s1, uint len1, const char *s2, uint len2, uint length);
369
370 ZEND_API void zendi_smart_strcmp(zval *result, zval *s1, zval *s2);
371 ZEND_API void zend_compare_symbol_tables(zval *result, HashTable *ht1, HashTable *ht2 TSRMLS_DC);
372 ZEND_API void zend_compare_arrays(zval *result, zval *a1, zval *a2 TSRMLS_DC);
373 ZEND_API void zend_compare_objects(zval *result, zval *o1, zval *o2 TSRMLS_DC);
374
375 ZEND_API int zend_atoi(const char *str, int str_len);
376 ZEND_API long zend_atol(const char *str, int str_len);
377
378 ZEND_API void zend_locale_sprintf_double(zval *op ZEND_FILE_LINE_DC);
379 END_EXTERN_C()
380
381 #define convert_to_ex_master(ppzv, lower_type, upper_type) \
382 if (Z_TYPE_PP(ppzv)!=IS_##upper_type) { \
383 SEPARATE_ZVAL_IF_NOT_REF(ppzv); \
384 convert_to_##lower_type(*ppzv); \
385 }
386
387 #define convert_to_explicit_type(pzv, type) \
388 do { \
389 switch (type) { \
390 case IS_NULL: \
391 convert_to_null(pzv); \
392 break; \
393 case IS_LONG: \
394 convert_to_long(pzv); \
395 break; \
396 case IS_DOUBLE: \
397 convert_to_double(pzv); \
398 break; \
399 case IS_BOOL: \
400 convert_to_boolean(pzv); \
401 break; \
402 case IS_ARRAY: \
403 convert_to_array(pzv); \
404 break; \
405 case IS_OBJECT: \
406 convert_to_object(pzv); \
407 break; \
408 case IS_STRING: \
409 convert_to_string(pzv); \
410 break; \
411 default: \
412 assert(0); \
413 break; \
414 } \
415 } while (0);
416
417 #define convert_to_explicit_type_ex(ppzv, str_type) \
418 if (Z_TYPE_PP(ppzv) != str_type) { \
419 SEPARATE_ZVAL_IF_NOT_REF(ppzv); \
420 convert_to_explicit_type(*ppzv, str_type); \
421 }
422
423 #define convert_to_boolean_ex(ppzv) convert_to_ex_master(ppzv, boolean, BOOL)
424 #define convert_to_long_ex(ppzv) convert_to_ex_master(ppzv, long, LONG)
425 #define convert_to_double_ex(ppzv) convert_to_ex_master(ppzv, double, DOUBLE)
426 #define convert_to_string_ex(ppzv) convert_to_ex_master(ppzv, string, STRING)
427 #define convert_to_array_ex(ppzv) convert_to_ex_master(ppzv, array, ARRAY)
428 #define convert_to_object_ex(ppzv) convert_to_ex_master(ppzv, object, OBJECT)
429 #define convert_to_null_ex(ppzv) convert_to_ex_master(ppzv, null, NULL)
430
431 #define convert_scalar_to_number_ex(ppzv) \
432 if (Z_TYPE_PP(ppzv)!=IS_LONG && Z_TYPE_PP(ppzv)!=IS_DOUBLE) { \
433 if (!Z_ISREF_PP(ppzv)) { \
434 SEPARATE_ZVAL(ppzv); \
435 } \
436 convert_scalar_to_number(*ppzv TSRMLS_CC); \
437 }
438
439
440 #define Z_LVAL(zval) (zval).value.lval
441 #define Z_BVAL(zval) ((zend_bool)(zval).value.lval)
442 #define Z_DVAL(zval) (zval).value.dval
443 #define Z_STRVAL(zval) (zval).value.str.val
444 #define Z_STRLEN(zval) (zval).value.str.len
445 #define Z_ARRVAL(zval) (zval).value.ht
446 #define Z_OBJVAL(zval) (zval).value.obj
447 #define Z_OBJ_HANDLE(zval) Z_OBJVAL(zval).handle
448 #define Z_OBJ_HT(zval) Z_OBJVAL(zval).handlers
449 #define Z_OBJCE(zval) zend_get_class_entry(&(zval) TSRMLS_CC)
450 #define Z_OBJPROP(zval) Z_OBJ_HT((zval))->get_properties(&(zval) TSRMLS_CC)
451 #define Z_OBJ_HANDLER(zval, hf) Z_OBJ_HT((zval))->hf
452 #define Z_RESVAL(zval) (zval).value.lval
453 #define Z_OBJDEBUG(zval,is_tmp) (Z_OBJ_HANDLER((zval),get_debug_info)?Z_OBJ_HANDLER((zval),get_debug_info)(&(zval),&is_tmp TSRMLS_CC):(is_tmp=0,Z_OBJ_HANDLER((zval),get_properties)?Z_OBJPROP(zval):NULL))
454
455 #define Z_LVAL_P(zval_p) Z_LVAL(*zval_p)
456 #define Z_BVAL_P(zval_p) Z_BVAL(*zval_p)
457 #define Z_DVAL_P(zval_p) Z_DVAL(*zval_p)
458 #define Z_STRVAL_P(zval_p) Z_STRVAL(*zval_p)
459 #define Z_STRLEN_P(zval_p) Z_STRLEN(*zval_p)
460 #define Z_ARRVAL_P(zval_p) Z_ARRVAL(*zval_p)
461 #define Z_OBJPROP_P(zval_p) Z_OBJPROP(*zval_p)
462 #define Z_OBJCE_P(zval_p) Z_OBJCE(*zval_p)
463 #define Z_RESVAL_P(zval_p) Z_RESVAL(*zval_p)
464 #define Z_OBJVAL_P(zval_p) Z_OBJVAL(*zval_p)
465 #define Z_OBJ_HANDLE_P(zval_p) Z_OBJ_HANDLE(*zval_p)
466 #define Z_OBJ_HT_P(zval_p) Z_OBJ_HT(*zval_p)
467 #define Z_OBJ_HANDLER_P(zval_p, h) Z_OBJ_HANDLER(*zval_p, h)
468 #define Z_OBJDEBUG_P(zval_p,is_tmp) Z_OBJDEBUG(*zval_p,is_tmp)
469
470 #define Z_LVAL_PP(zval_pp) Z_LVAL(**zval_pp)
471 #define Z_BVAL_PP(zval_pp) Z_BVAL(**zval_pp)
472 #define Z_DVAL_PP(zval_pp) Z_DVAL(**zval_pp)
473 #define Z_STRVAL_PP(zval_pp) Z_STRVAL(**zval_pp)
474 #define Z_STRLEN_PP(zval_pp) Z_STRLEN(**zval_pp)
475 #define Z_ARRVAL_PP(zval_pp) Z_ARRVAL(**zval_pp)
476 #define Z_OBJPROP_PP(zval_pp) Z_OBJPROP(**zval_pp)
477 #define Z_OBJCE_PP(zval_pp) Z_OBJCE(**zval_pp)
478 #define Z_RESVAL_PP(zval_pp) Z_RESVAL(**zval_pp)
479 #define Z_OBJVAL_PP(zval_pp) Z_OBJVAL(**zval_pp)
480 #define Z_OBJ_HANDLE_PP(zval_p) Z_OBJ_HANDLE(**zval_p)
481 #define Z_OBJ_HT_PP(zval_p) Z_OBJ_HT(**zval_p)
482 #define Z_OBJ_HANDLER_PP(zval_p, h) Z_OBJ_HANDLER(**zval_p, h)
483 #define Z_OBJDEBUG_PP(zval_pp,is_tmp) Z_OBJDEBUG(**zval_pp,is_tmp)
484
485 #define Z_TYPE(zval) (zval).type
486 #define Z_TYPE_P(zval_p) Z_TYPE(*zval_p)
487 #define Z_TYPE_PP(zval_pp) Z_TYPE(**zval_pp)
488
489 #if HAVE_SETLOCALE && defined(ZEND_WIN32) && !defined(ZTS) && defined(_MSC_VER) && (_MSC_VER >= 1400)
490 /* This is performance improvement of tolower() on Windows and VC2005
491 * Gives 10-18% on bench.php
492 */
493 #define ZEND_USE_TOLOWER_L 1
494 #endif
495
496 #ifdef ZEND_USE_TOLOWER_L
497 ZEND_API void zend_update_current_locale(void);
498 #else
499 #define zend_update_current_locale()
500 #endif
501
502 /* The offset in bytes between the value and type fields of a zval */
503 #define ZVAL_OFFSETOF_TYPE \
504 (offsetof(zval,type) - offsetof(zval,value))
505
fast_increment_function(zval * op1)506 static zend_always_inline int fast_increment_function(zval *op1)
507 {
508 if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
509 #if defined(__GNUC__) && defined(__i386__)
510 __asm__(
511 "incl (%0)\n\t"
512 "jno 0f\n\t"
513 "movl $0x0, (%0)\n\t"
514 "movl $0x41e00000, 0x4(%0)\n\t"
515 "movb %1, %c2(%0)\n"
516 "0:"
517 :
518 : "r"(&op1->value),
519 "n"(IS_DOUBLE),
520 "n"(ZVAL_OFFSETOF_TYPE)
521 : "cc");
522 #elif defined(__GNUC__) && defined(__x86_64__)
523 __asm__(
524 "incq (%0)\n\t"
525 "jno 0f\n\t"
526 "movl $0x0, (%0)\n\t"
527 "movl $0x43e00000, 0x4(%0)\n\t"
528 "movb %1, %c2(%0)\n"
529 "0:"
530 :
531 : "r"(&op1->value),
532 "n"(IS_DOUBLE),
533 "n"(ZVAL_OFFSETOF_TYPE)
534 : "cc");
535 #else
536 if (UNEXPECTED(Z_LVAL_P(op1) == LONG_MAX)) {
537 /* switch to double */
538 Z_DVAL_P(op1) = (double)LONG_MAX + 1.0;
539 Z_TYPE_P(op1) = IS_DOUBLE;
540 } else {
541 Z_LVAL_P(op1)++;
542 }
543 #endif
544 return SUCCESS;
545 }
546 return increment_function(op1);
547 }
548
fast_decrement_function(zval * op1)549 static zend_always_inline int fast_decrement_function(zval *op1)
550 {
551 if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
552 #if defined(__GNUC__) && defined(__i386__)
553 __asm__(
554 "decl (%0)\n\t"
555 "jno 0f\n\t"
556 "movl $0x00200000, (%0)\n\t"
557 "movl $0xc1e00000, 0x4(%0)\n\t"
558 "movb %1,%c2(%0)\n"
559 "0:"
560 :
561 : "r"(&op1->value),
562 "n"(IS_DOUBLE),
563 "n"(ZVAL_OFFSETOF_TYPE)
564 : "cc");
565 #elif defined(__GNUC__) && defined(__x86_64__)
566 __asm__(
567 "decq (%0)\n\t"
568 "jno 0f\n\t"
569 "movl $0x00000000, (%0)\n\t"
570 "movl $0xc3e00000, 0x4(%0)\n\t"
571 "movb %1,%c2(%0)\n"
572 "0:"
573 :
574 : "r"(&op1->value),
575 "n"(IS_DOUBLE),
576 "n"(ZVAL_OFFSETOF_TYPE)
577 : "cc");
578 #else
579 if (UNEXPECTED(Z_LVAL_P(op1) == LONG_MIN)) {
580 /* switch to double */
581 Z_DVAL_P(op1) = (double)LONG_MIN - 1.0;
582 Z_TYPE_P(op1) = IS_DOUBLE;
583 } else {
584 Z_LVAL_P(op1)--;
585 }
586 #endif
587 return SUCCESS;
588 }
589 return decrement_function(op1);
590 }
591
fast_add_function(zval * result,zval * op1,zval * op2 TSRMLS_DC)592 static zend_always_inline int fast_add_function(zval *result, zval *op1, zval *op2 TSRMLS_DC)
593 {
594 if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
595 if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
596 #if defined(__GNUC__) && defined(__i386__)
597 __asm__(
598 "movl (%1), %%eax\n\t"
599 "addl (%2), %%eax\n\t"
600 "jo 0f\n\t"
601 "movl %%eax, (%0)\n\t"
602 "movb %3, %c5(%0)\n\t"
603 "jmp 1f\n"
604 "0:\n\t"
605 "fildl (%1)\n\t"
606 "fildl (%2)\n\t"
607 "faddp %%st, %%st(1)\n\t"
608 "movb %4, %c5(%0)\n\t"
609 "fstpl (%0)\n"
610 "1:"
611 :
612 : "r"(&result->value),
613 "r"(&op1->value),
614 "r"(&op2->value),
615 "n"(IS_LONG),
616 "n"(IS_DOUBLE),
617 "n"(ZVAL_OFFSETOF_TYPE)
618 : "eax","cc");
619 #elif defined(__GNUC__) && defined(__x86_64__)
620 __asm__(
621 "movq (%1), %%rax\n\t"
622 "addq (%2), %%rax\n\t"
623 "jo 0f\n\t"
624 "movq %%rax, (%0)\n\t"
625 "movb %3, %c5(%0)\n\t"
626 "jmp 1f\n"
627 "0:\n\t"
628 "fildq (%1)\n\t"
629 "fildq (%2)\n\t"
630 "faddp %%st, %%st(1)\n\t"
631 "movb %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 : "rax","cc");
642 #else
643 /*
644 * 'result' may alias with op1 or op2, so we need to
645 * ensure that 'result' is not updated until after we
646 * have read the values of op1 and op2.
647 */
648
649 if (UNEXPECTED((Z_LVAL_P(op1) & LONG_SIGN_MASK) == (Z_LVAL_P(op2) & LONG_SIGN_MASK)
650 && (Z_LVAL_P(op1) & LONG_SIGN_MASK) != ((Z_LVAL_P(op1) + Z_LVAL_P(op2)) & LONG_SIGN_MASK))) {
651 Z_DVAL_P(result) = (double) Z_LVAL_P(op1) + (double) Z_LVAL_P(op2);
652 Z_TYPE_P(result) = IS_DOUBLE;
653 } else {
654 Z_LVAL_P(result) = Z_LVAL_P(op1) + Z_LVAL_P(op2);
655 Z_TYPE_P(result) = IS_LONG;
656 }
657 #endif
658 return SUCCESS;
659 } else if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
660 Z_DVAL_P(result) = ((double)Z_LVAL_P(op1)) + Z_DVAL_P(op2);
661 Z_TYPE_P(result) = IS_DOUBLE;
662 return SUCCESS;
663 }
664 } else if (EXPECTED(Z_TYPE_P(op1) == IS_DOUBLE)) {
665 if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
666 Z_DVAL_P(result) = Z_DVAL_P(op1) + Z_DVAL_P(op2);
667 Z_TYPE_P(result) = IS_DOUBLE;
668 return SUCCESS;
669 } else if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
670 Z_DVAL_P(result) = Z_DVAL_P(op1) + ((double)Z_LVAL_P(op2));
671 Z_TYPE_P(result) = IS_DOUBLE;
672 return SUCCESS;
673 }
674 }
675 return add_function(result, op1, op2 TSRMLS_CC);
676 }
677
fast_sub_function(zval * result,zval * op1,zval * op2 TSRMLS_DC)678 static zend_always_inline int fast_sub_function(zval *result, zval *op1, zval *op2 TSRMLS_DC)
679 {
680 if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
681 if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
682 #if defined(__GNUC__) && defined(__i386__)
683 __asm__(
684 "movl (%1), %%eax\n\t"
685 "subl (%2), %%eax\n\t"
686 "jo 0f\n\t"
687 "movl %%eax, (%0)\n\t"
688 "movb %3, %c5(%0)\n\t"
689 "jmp 1f\n"
690 "0:\n\t"
691 "fildl (%2)\n\t"
692 "fildl (%1)\n\t"
693 #if defined(__clang__) && (__clang_major__ < 2 || (__clang_major__ == 2 && __clang_minor__ < 10))
694 "fsubp %%st(1), %%st\n\t" /* LLVM bug #9164 */
695 #else
696 "fsubp %%st, %%st(1)\n\t"
697 #endif
698 "movb %4, %c5(%0)\n\t"
699 "fstpl (%0)\n"
700 "1:"
701 :
702 : "r"(&result->value),
703 "r"(&op1->value),
704 "r"(&op2->value),
705 "n"(IS_LONG),
706 "n"(IS_DOUBLE),
707 "n"(ZVAL_OFFSETOF_TYPE)
708 : "eax","cc");
709 #elif defined(__GNUC__) && defined(__x86_64__)
710 __asm__(
711 "movq (%1), %%rax\n\t"
712 "subq (%2), %%rax\n\t"
713 "jo 0f\n\t"
714 "movq %%rax, (%0)\n\t"
715 "movb %3, %c5(%0)\n\t"
716 "jmp 1f\n"
717 "0:\n\t"
718 "fildq (%2)\n\t"
719 "fildq (%1)\n\t"
720 #if defined(__clang__) && (__clang_major__ < 2 || (__clang_major__ == 2 && __clang_minor__ < 10))
721 "fsubp %%st(1), %%st\n\t" /* LLVM bug #9164 */
722 #else
723 "fsubp %%st, %%st(1)\n\t"
724 #endif
725 "movb %4, %c5(%0)\n\t"
726 "fstpl (%0)\n"
727 "1:"
728 :
729 : "r"(&result->value),
730 "r"(&op1->value),
731 "r"(&op2->value),
732 "n"(IS_LONG),
733 "n"(IS_DOUBLE),
734 "n"(ZVAL_OFFSETOF_TYPE)
735 : "rax","cc");
736 #else
737 Z_LVAL_P(result) = Z_LVAL_P(op1) - Z_LVAL_P(op2);
738
739 if (UNEXPECTED((Z_LVAL_P(op1) & LONG_SIGN_MASK) != (Z_LVAL_P(op2) & LONG_SIGN_MASK)
740 && (Z_LVAL_P(op1) & LONG_SIGN_MASK) != (Z_LVAL_P(result) & LONG_SIGN_MASK))) {
741 Z_DVAL_P(result) = (double) Z_LVAL_P(op1) - (double) Z_LVAL_P(op2);
742 Z_TYPE_P(result) = IS_DOUBLE;
743 } else {
744 Z_TYPE_P(result) = IS_LONG;
745 }
746 #endif
747 return SUCCESS;
748 } else if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
749 Z_DVAL_P(result) = ((double)Z_LVAL_P(op1)) - Z_DVAL_P(op2);
750 Z_TYPE_P(result) = IS_DOUBLE;
751 return SUCCESS;
752 }
753 } else if (EXPECTED(Z_TYPE_P(op1) == IS_DOUBLE)) {
754 if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
755 Z_DVAL_P(result) = Z_DVAL_P(op1) - Z_DVAL_P(op2);
756 Z_TYPE_P(result) = IS_DOUBLE;
757 return SUCCESS;
758 } else if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
759 Z_DVAL_P(result) = Z_DVAL_P(op1) - ((double)Z_LVAL_P(op2));
760 Z_TYPE_P(result) = IS_DOUBLE;
761 return SUCCESS;
762 }
763 }
764 return sub_function(result, op1, op2 TSRMLS_CC);
765 }
766
fast_mul_function(zval * result,zval * op1,zval * op2 TSRMLS_DC)767 static zend_always_inline int fast_mul_function(zval *result, zval *op1, zval *op2 TSRMLS_DC)
768 {
769 if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
770 if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
771 long overflow;
772
773 ZEND_SIGNED_MULTIPLY_LONG(Z_LVAL_P(op1), Z_LVAL_P(op2), Z_LVAL_P(result), Z_DVAL_P(result), overflow);
774 Z_TYPE_P(result) = overflow ? IS_DOUBLE : IS_LONG;
775 return SUCCESS;
776 } else if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
777 Z_DVAL_P(result) = ((double)Z_LVAL_P(op1)) * Z_DVAL_P(op2);
778 Z_TYPE_P(result) = IS_DOUBLE;
779 return SUCCESS;
780 }
781 } else if (EXPECTED(Z_TYPE_P(op1) == IS_DOUBLE)) {
782 if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
783 Z_DVAL_P(result) = Z_DVAL_P(op1) * Z_DVAL_P(op2);
784 Z_TYPE_P(result) = IS_DOUBLE;
785 return SUCCESS;
786 } else if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
787 Z_DVAL_P(result) = Z_DVAL_P(op1) * ((double)Z_LVAL_P(op2));
788 Z_TYPE_P(result) = IS_DOUBLE;
789 return SUCCESS;
790 }
791 }
792 return mul_function(result, op1, op2 TSRMLS_CC);
793 }
794
fast_div_function(zval * result,zval * op1,zval * op2 TSRMLS_DC)795 static zend_always_inline int fast_div_function(zval *result, zval *op1, zval *op2 TSRMLS_DC)
796 {
797 #if 0
798 if (EXPECTED(Z_TYPE_P(op1) == IS_LONG) && 0) {
799 if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
800 if (UNEXPECTED(Z_LVAL_P(op2) == 0)) {
801 zend_error(E_WARNING, "Division by zero");
802 Z_LVAL_P(result) = 0;
803 Z_TYPE_P(result) = IS_BOOL;
804 return FAILURE;
805 } else if (UNEXPECTED(Z_LVAL_P(op2) == -1 && Z_LVAL_P(op1) == LONG_MIN)) {
806 /* Prevent overflow error/crash */
807 Z_DVAL_P(result) = (double) LONG_MIN / -1;
808 Z_TYPE_P(result) = IS_DOUBLE;
809 } else if (EXPECTED(Z_LVAL_P(op1) % Z_LVAL_P(op2) == 0)) {
810 /* integer */
811 Z_LVAL_P(result) = Z_LVAL_P(op1) / Z_LVAL_P(op2);
812 Z_TYPE_P(result) = IS_LONG;
813 } else {
814 Z_DVAL_P(result) = ((double) Z_LVAL_P(op1)) / ((double)Z_LVAL_P(op2));
815 Z_TYPE_P(result) = IS_DOUBLE;
816 }
817 return SUCCESS;
818 } else if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
819 if (UNEXPECTED(Z_DVAL_P(op2) == 0)) {
820 zend_error(E_WARNING, "Division by zero");
821 Z_LVAL_P(result) = 0;
822 Z_TYPE_P(result) = IS_BOOL;
823 return FAILURE;
824 }
825 Z_DVAL_P(result) = ((double)Z_LVAL_P(op1)) / Z_DVAL_P(op2);
826 Z_TYPE_P(result) = IS_DOUBLE;
827 return SUCCESS;
828 }
829 } else if (EXPECTED(Z_TYPE_P(op1) == IS_DOUBLE) && 0) {
830 if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
831 if (UNEXPECTED(Z_DVAL_P(op2) == 0)) {
832 zend_error(E_WARNING, "Division by zero");
833 Z_LVAL_P(result) = 0;
834 Z_TYPE_P(result) = IS_BOOL;
835 return FAILURE;
836 }
837 Z_DVAL_P(result) = Z_DVAL_P(op1) / Z_DVAL_P(op2);
838 Z_TYPE_P(result) = IS_DOUBLE;
839 return SUCCESS;
840 } else if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
841 if (UNEXPECTED(Z_LVAL_P(op2) == 0)) {
842 zend_error(E_WARNING, "Division by zero");
843 Z_LVAL_P(result) = 0;
844 Z_TYPE_P(result) = IS_BOOL;
845 return FAILURE;
846 }
847 Z_DVAL_P(result) = Z_DVAL_P(op1) / ((double)Z_LVAL_P(op2));
848 Z_TYPE_P(result) = IS_DOUBLE;
849 return SUCCESS;
850 }
851 }
852 #endif
853 return div_function(result, op1, op2 TSRMLS_CC);
854 }
855
fast_mod_function(zval * result,zval * op1,zval * op2 TSRMLS_DC)856 static zend_always_inline int fast_mod_function(zval *result, zval *op1, zval *op2 TSRMLS_DC)
857 {
858 if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
859 if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
860 if (UNEXPECTED(Z_LVAL_P(op2) == 0)) {
861 zend_error(E_WARNING, "Division by zero");
862 Z_LVAL_P(result) = 0;
863 Z_TYPE_P(result) = IS_BOOL;
864 return FAILURE;
865 } else if (UNEXPECTED(Z_LVAL_P(op2) == -1)) {
866 /* Prevent overflow error/crash if op1==LONG_MIN */
867 Z_LVAL_P(result) = 0;
868 Z_TYPE_P(result) = IS_LONG;
869 return SUCCESS;
870 }
871 Z_LVAL_P(result) = Z_LVAL_P(op1) % Z_LVAL_P(op2);
872 Z_TYPE_P(result) = IS_LONG;
873 return SUCCESS;
874 }
875 }
876 return mod_function(result, op1, op2 TSRMLS_CC);
877 }
878
fast_equal_function(zval * result,zval * op1,zval * op2 TSRMLS_DC)879 static zend_always_inline int fast_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC)
880 {
881 if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
882 if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
883 return Z_LVAL_P(op1) == Z_LVAL_P(op2);
884 } else if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
885 return ((double)Z_LVAL_P(op1)) == Z_DVAL_P(op2);
886 }
887 } else if (EXPECTED(Z_TYPE_P(op1) == IS_DOUBLE)) {
888 if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
889 return Z_DVAL_P(op1) == Z_DVAL_P(op2);
890 } else if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
891 return Z_DVAL_P(op1) == ((double)Z_LVAL_P(op2));
892 }
893 }
894 compare_function(result, op1, op2 TSRMLS_CC);
895 return Z_LVAL_P(result) == 0;
896 }
897
fast_not_equal_function(zval * result,zval * op1,zval * op2 TSRMLS_DC)898 static zend_always_inline int fast_not_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC)
899 {
900 if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
901 if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
902 return Z_LVAL_P(op1) != Z_LVAL_P(op2);
903 } else if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
904 return ((double)Z_LVAL_P(op1)) != Z_DVAL_P(op2);
905 }
906 } else if (EXPECTED(Z_TYPE_P(op1) == IS_DOUBLE)) {
907 if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
908 return Z_DVAL_P(op1) != Z_DVAL_P(op2);
909 } else if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
910 return Z_DVAL_P(op1) != ((double)Z_LVAL_P(op2));
911 }
912 }
913 compare_function(result, op1, op2 TSRMLS_CC);
914 return Z_LVAL_P(result) != 0;
915 }
916
fast_is_smaller_function(zval * result,zval * op1,zval * op2 TSRMLS_DC)917 static zend_always_inline int fast_is_smaller_function(zval *result, zval *op1, zval *op2 TSRMLS_DC)
918 {
919 if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
920 if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
921 return Z_LVAL_P(op1) < Z_LVAL_P(op2);
922 } else if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
923 return ((double)Z_LVAL_P(op1)) < Z_DVAL_P(op2);
924 }
925 } else if (EXPECTED(Z_TYPE_P(op1) == IS_DOUBLE)) {
926 if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
927 return Z_DVAL_P(op1) < Z_DVAL_P(op2);
928 } else if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
929 return Z_DVAL_P(op1) < ((double)Z_LVAL_P(op2));
930 }
931 }
932 compare_function(result, op1, op2 TSRMLS_CC);
933 return Z_LVAL_P(result) < 0;
934 }
935
fast_is_smaller_or_equal_function(zval * result,zval * op1,zval * op2 TSRMLS_DC)936 static zend_always_inline int fast_is_smaller_or_equal_function(zval *result, zval *op1, zval *op2 TSRMLS_DC)
937 {
938 if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
939 if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
940 return Z_LVAL_P(op1) <= Z_LVAL_P(op2);
941 } else if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
942 return ((double)Z_LVAL_P(op1)) <= Z_DVAL_P(op2);
943 }
944 } else if (EXPECTED(Z_TYPE_P(op1) == IS_DOUBLE)) {
945 if (EXPECTED(Z_TYPE_P(op2) == IS_DOUBLE)) {
946 return Z_DVAL_P(op1) <= Z_DVAL_P(op2);
947 } else if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
948 return Z_DVAL_P(op1) <= ((double)Z_LVAL_P(op2));
949 }
950 }
951 compare_function(result, op1, op2 TSRMLS_CC);
952 return Z_LVAL_P(result) <= 0;
953 }
954
955 #endif
956
957 /*
958 * Local variables:
959 * tab-width: 4
960 * c-basic-offset: 4
961 * indent-tabs-mode: t
962 * End:
963 */
964