commit after reset fuckup
[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 ZVAL_STRING(z, psi_i128_to_buf(&buf[sizeof(buf) - 1], V)); \
76 } else { \
77 ZVAL_STRING(z, zend_print_long_to_buf(&buf[sizeof(buf) - 1], V)); \
78 } \
79 } else { \
80 if (V <= ZEND_LONG_MAX) { \
81 ZVAL_LONG(z, V); \
82 } else if (V <= (1L<<53)) { \
83 ZVAL_DOUBLE(z, V); \
84 } else if (V > ZEND_ULONG_MAX) { \
85 ZVAL_STRING(z, psi_u128_to_buf(&buf[sizeof(buf) - 1], V)); \
86 } else { \
87 ZVAL_STRING(z, zend_print_ulong_to_buf(&buf[sizeof(buf) - 1], V)); \
88 } \
89 } \
90 } while (0)
91
92
93 zend_long psi_zval_count(zval *zvalue);
94 zend_internal_arg_info *psi_internal_arginfo(struct psi_impl *impl);
95 int psi_internal_type(struct psi_impl_type *type);
96
97 typedef void (*psi_marshal_set)(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
98 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);
99
100 void psi_set_void(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
101 void psi_set_to_bool(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
102 void psi_set_to_int(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
103 void psi_set_to_float(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
104 void psi_set_to_string(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
105 void psi_set_to_stringl(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
106 void psi_set_to_recursive(zval *return_value, struct psi_set_exp *set, impl_val *r_val, struct psi_call_frame *frame);
107 void psi_set_to_array_simple(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
108 void psi_set_to_array_counted(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
109 void psi_set_to_array(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
110 void psi_set_to_object(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
111 void psi_set_zval(zval *return_value, struct psi_set_exp *set, impl_val *ret_val, struct psi_call_frame *frame);
112
113 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);
114 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);
115 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);
116 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);
117 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);
118 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);
119 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);
120 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);
121 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);
122 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);
123
124 #endif