build cleanup
[m6w6/ext-psi] / src / builtin.c
1 /*******************************************************************************
2 Copyright (c) 2018, 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 #include "php_psi_stdinc.h"
27
28 #include "php_psi.h"
29 #include "builtin.h"
30 #include "parser.h"
31 #include "cpp.h"
32
33 #include <stdarg.h>
34
35 HashTable psi_builtins;
36
37 static bool has_include(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
38 static bool has_include_next(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
39 static bool has_feature(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
40 static bool builtin_constant_p(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
41 static bool COUNTER__(struct psi_cpp *cpp, struct psi_token *target, struct psi_plist **args, struct psi_plist **res);
42
43 static inline struct psi_plist *builtin_sig(token_t typ, ...)
44 {
45 struct psi_plist *sig;
46 size_t n = 0;
47 va_list args;
48
49 if (typ == (token_t) - 1) {
50 return NULL;
51 }
52
53 sig = psi_plist_init((psi_plist_dtor) psi_token_free);
54 va_start(args, typ);
55 while (typ) {
56 char a = 'a' + n++;
57 struct psi_token *arg;
58
59 arg = psi_token_init(typ, &a, 1, 0, 0, zend_empty_string);
60 sig = psi_plist_add(sig, &arg);
61 typ = va_arg(args, token_t);
62 }
63 va_end(args);
64
65 return sig;
66 }
67
68 static void free_builtin(zval *p)
69 {
70 struct psi_builtin *b = Z_PTR_P(p);
71
72 if (b) {
73 zend_string_release(b->name);
74 psi_cpp_macro_decl_free(&b->decl);
75 pefree(b, 1);
76 }
77 }
78
79 PHP_MINIT_FUNCTION(psi_builtin)
80 {
81 #define PSI_BUILTIN(builtin, ...) do { \
82 struct psi_builtin entry; \
83 struct psi_plist *sig = builtin_sig(__VA_ARGS__, 0); \
84 struct psi_cpp_macro_decl *decl = psi_cpp_macro_decl_init(sig, NULL, NULL); \
85 decl->token = psi_token_init(PSI_T_NAME, "__" #builtin, sizeof("__" #builtin)-1, \
86 0, 0, zend_empty_string); \
87 entry.name = zend_string_copy(decl->token->text); \
88 entry.func = &builtin; \
89 entry.decl = decl; \
90 zend_hash_add_mem(&psi_builtins, entry.name, &entry, sizeof(entry)); \
91 } while(0)
92
93 zend_hash_init(&psi_builtins, 0, NULL, free_builtin, 1);
94 PSI_BUILTIN(has_include, PSI_T_CPP_HEADER);
95 PSI_BUILTIN(has_include_next, PSI_T_CPP_HEADER);
96 PSI_BUILTIN(has_feature, PSI_T_NAME);
97 PSI_BUILTIN(builtin_constant_p, PSI_T_NAME);
98
99 PSI_BUILTIN(COUNTER__, -1);
100
101 return SUCCESS;
102 }
103
104 PHP_MSHUTDOWN_FUNCTION(psi_builtin)
105 {
106 zend_hash_destroy(&psi_builtins);
107 return SUCCESS;
108 }
109
110 bool psi_builtin_exists(zend_string *name)
111 {
112 return zend_hash_exists(&psi_builtins, name);
113 }
114
115 struct psi_builtin *psi_builtin_get(zend_string *name)
116 {
117 return zend_hash_find_ptr(&psi_builtins, name);
118 }
119
120 static bool has_include(struct psi_cpp *cpp, struct psi_token *target,
121 struct psi_plist **args, struct psi_plist **res)
122 {
123 struct psi_plist *arg = args[0];
124 struct psi_token *tok;
125
126 switch (psi_plist_count(arg)) {
127 case 1:
128 if (psi_plist_get(arg, 0, &tok)) {
129 const char *cpp_search = cpp->search;
130 bool has = psi_cpp_has_include(cpp, tok, 0, NULL);
131 cpp->search = cpp_search;
132 return has;
133 }
134 /* no break */
135 default:
136 cpp->parser->error(PSI_DATA(cpp->parser), target, PSI_WARNING,
137 "Erroneous usage of builtin __%s", __FUNCTION__);
138 }
139 return false;
140 }
141
142 static bool has_include_next(struct psi_cpp *cpp, struct psi_token *target,
143 struct psi_plist **args, struct psi_plist **res)
144 {
145 struct psi_plist *arg = args[0];
146 struct psi_token *tok;
147
148 switch (psi_plist_count(arg)) {
149 case 1:
150 if (psi_plist_get(arg, 0, &tok)) {
151 const char *cpp_search = cpp->search;
152 bool has = psi_cpp_has_include(cpp, tok, PSI_CPP_INCLUDE_NEXT, NULL);
153 cpp->search = cpp_search;
154 return has;
155 }
156 /* no break */
157 default:
158 cpp->parser->error(PSI_DATA(cpp->parser), target, PSI_WARNING,
159 "Erroneous usage of builtin __%s", __FUNCTION__);
160 }
161 return false;
162 }
163
164 static bool has_feature(struct psi_cpp *cpp, struct psi_token *target,
165 struct psi_plist **args, struct psi_plist **res_ptr)
166 {
167 return false;
168 }
169
170 static bool builtin_constant_p(struct psi_cpp *cpp, struct psi_token *target,
171 struct psi_plist **args, struct psi_plist **res_ptr)
172 {
173 /* we want functions, not macros for e.g. htonl() */
174 return false;
175 }
176
177 static bool COUNTER__(struct psi_cpp *cpp, struct psi_token *target,
178 struct psi_plist **args, struct psi_plist **res)
179 {
180 struct psi_token *tok;
181 char buf[0x20];
182 size_t len = sprintf(buf, "%u", cpp->counter++);
183
184 tok = psi_token_init(PSI_T_NUMBER, buf, len, target->col, target->line, target->file);
185 *res = psi_plist_init((psi_plist_dtor) psi_token_free);
186 *res = psi_plist_add(*res, &tok);
187
188 return true;
189 }
190
191 #ifdef __APPLE__
192 #include <libkern/OSByteOrder.h>
193 # define bswap_16(u) _OSSwapInt16(u)
194 # define bswap_32(u) _OSSwapInt32(u)
195 # define bswap_64(u) _OSSwapInt64(u)
196 #elif defined(__FreeBSD__)
197 # include <sys/endian.h>
198 # define bswap_16(u) bswap16(u)
199 # define bswap_32(u) bswap32(u)
200 # define bswap_64(u) bswap64(u)
201 #elif defined(__OpenBSD__)
202 # include <sys/types.h>
203 # define bswap_16(u) swap16(u)
204 # define bswap_32(u) swap32(u)
205 # define bswap_64(u) swap64(u)
206 #elif defined(__NetBSD__)
207 # include <sys/types.h>
208 # include <machine/bswap.h>
209 # define bswap_16(u) bswap16(u)
210 # define bswap_32(u) bswap32(u)
211 # define bswap_64(u) bswap64(u)
212 #else
213 # include <byteswap.h>
214 #endif
215
216 uint16_t psi_swap16(uint16_t u)
217 {
218 return bswap_16(u);
219 }
220
221 uint32_t psi_swap32(uint32_t u)
222 {
223 return bswap_32(u);
224 }
225
226 uint64_t psi_swap64(uint64_t u)
227 {
228 return bswap_64(u);
229 }