travis: update
[m6w6/ext-psi] / src / marshal.h
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #ifndef PSI_MARSHAL_H
27 #define PSI_MARSHAL_H
28
29 #include "types/impl_val.h"
30 #include "Zend/zend_types.h"
31
32 struct psi_let_exp;
33 struct psi_set_exp;
34 struct psi_decl_arg;
35 struct psi_call_frame;
36 struct psi_impl;
37 struct psi_impl_type;
38
39 #if HAVE_INT128
40 static inline char *psi_u128_to_buf(char *buf, unsigned __int128 u128)
41 {
42 for (*buf = 0; u128 > 0; u128 /= 10) {
43 *--buf = ((u128 % 10) + '0') & 0xff;
44 }
45 return buf;
46 }
47
48 static inline char *psi_i128_to_buf(char *buf, __int128 i128)
49 {
50 if (i128 < 0) {
51 char *res = psi_u128_to_buf(buf, ~((unsigned __int128) i128) + 1);
52
53 *--res = '-';
54 return res;
55 }
56 return psi_u128_to_buf(buf, i128);
57 }
58 #else
59 # define psi_u128_to_buf(b,u) zend_print_ulong_to_buf(b,u)
60 # define psi_i128_to_buf(b,i) zend_print_long_to_buf(b,i)
61 #endif
62
63
64 #define RETVAL_LONG_DOUBLE_STR(V, flags) ZVAL_LONG_DOUBLE_STR(return_value, V, flags)
65 # define ZVAL_LONG_DOUBLE_STR(z, V, flags) do { \
66 char buf[0x20] = {0}; \
67 bool is_signed = false, persistent = false; \
68 flags; \
69 if (is_signed) { \
70 if (V >= ZEND_LONG_MIN && V <= ZEND_LONG_MAX) { \
71 ZVAL_LONG(z, V); \
72 } else if (V >= -(1L<<52) && V <= (1L<<53)) { \
73 ZVAL_DOUBLE(z, V); \
74 } else if (V < ZEND_LONG_MIN || V > ZEND_LONG_MAX) { \
75 char *str = psi_i128_to_buf(&buf[sizeof(buf) - 1], V); \
76 if (persistent) { \
77 ZVAL_PSTRING(z, str); \
78 } else { \
79 ZVAL_STRING(z, str); \
80 } \
81 } else { \
82 char *str = zend_print_long_to_buf(&buf[sizeof(buf) - 1], V); \
83 if (persistent) { \
84 ZVAL_PSTRING(z, str); \
85 } else { \
86 ZVAL_STRING(z, str); \
87 } \
88 } \
89 } else { \
90 if (V <= ZEND_LONG_MAX) { \
91 ZVAL_LONG(z, V); \
92 } else if (V <= (1L<<53)) { \
93 ZVAL_DOUBLE(z, V); \
94 } else if (V > ZEND_ULONG_MAX) { \
95 char *str = psi_u128_to_buf(&buf[sizeof(buf) - 1], V); \
96 if (persistent) { \
97 ZVAL_PSTRING(z, str); \
98 } else { \
99 ZVAL_STRING(z, str); \
100 } \
101 } else { \
102 char *str = zend_print_ulong_to_buf(&buf[sizeof(buf) - 1], V); \
103 if (persistent) { \
104 ZVAL_PSTRING(z, str); \
105 } else { \
106 ZVAL_STRING(z, str); \
107 } \
108 } \
109 } \
110 } while (0)
111
112
113 zend_long psi_zval_count(zval *zvalue);
114 zend_internal_arg_info *psi_internal_arginfo(struct psi_impl *impl);
115 int psi_internal_type(struct psi_impl_type *type);
116
117 typedef void (*psi_marshal_set)(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
118 typedef impl_val *(*psi_marshal_let)(impl_val *tmp, struct psi_decl_arg *type_spec, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free);
119
120 void psi_set_void(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
121 void psi_set_to_bool(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
122 void psi_set_to_int(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
123 void psi_set_to_float(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
124 void psi_set_to_string(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
125 void psi_set_to_stringl(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
126 void psi_set_to_recursive(zval *return_value, struct psi_set_exp *set, impl_val *r_val, struct psi_call_frame *frame);
127 void psi_set_to_array_simple(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
128 void psi_set_to_array_counted(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
129 void psi_set_to_array(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
130 void psi_set_to_object(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
131 void psi_set_zval(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
132
133 impl_val *psi_let_void(impl_val *tmp, struct psi_decl_arg *spec, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free);
134 impl_val *psi_let_boolval(impl_val *tmp, struct psi_decl_arg *spec, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free);
135 impl_val *psi_let_intval(impl_val *tmp, struct psi_decl_arg *spec, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free);
136 impl_val *psi_let_floatval(impl_val *tmp, struct psi_decl_arg *spec, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free);
137 impl_val *psi_let_strval(impl_val *tmp, struct psi_decl_arg *spec, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free);
138 impl_val *psi_let_pathval(impl_val *tmp, struct psi_decl_arg *spec, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free);
139 impl_val *psi_let_strlen(impl_val *tmp, struct psi_decl_arg *spec, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free);
140 impl_val *psi_let_objval(impl_val *tmp, struct psi_decl_arg *spec, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free);
141 impl_val *psi_let_zval(impl_val *tmp, struct psi_decl_arg *spec, token_t impl_type, impl_val *ivalue, zval *zvalue, void **to_free);
142 impl_val *psi_let_count(impl_val *tmp, struct psi_decl_arg *spec, token_t impl_type, impl_val *ival, zval *zvalue, void **to_free);
143
144 #endif