88db742eb6df4329e293b5c15c6886eb8694c740
[m6w6/ext-psi] / src / types / num_exp.c
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 #include "php_psi_stdinc.h"
27
28 #include <assert.h>
29
30 #include "data.h"
31 #include "call.h"
32 #include "calc.h"
33
34 struct psi_num_exp *psi_num_exp_init(token_t t, void *num)
35 {
36 struct psi_num_exp *exp = calloc(1, sizeof(*exp));
37
38 switch (exp->type = t) {
39 case PSI_T_NUMBER:
40 case PSI_T_NSNAME:
41 exp->data.numb = strdup(num);
42 break;
43 case PSI_T_NAME:
44 exp->data.dvar = num;
45 break;
46 default:
47 assert(0);
48 }
49
50 return exp;
51 }
52
53 struct psi_num_exp *psi_num_exp_copy(struct psi_num_exp *exp)
54 {
55 struct psi_num_exp *num = calloc(1, sizeof(*num));
56
57 *num = *exp;
58
59 if (num->token) {
60 num->token = psi_token_copy(num->token);
61 }
62 if (num->operand) {
63 num->operand = psi_num_exp_copy(num->operand);
64 }
65
66 switch (num->type) {
67 case PSI_T_INT64:
68 case PSI_T_DOUBLE:
69 case PSI_T_ENUM:
70 case PSI_T_CONST:
71 break;
72 case PSI_T_NUMBER:
73 case PSI_T_NSNAME:
74 num->data.numb = strdup(num->data.numb);
75 break;
76 case PSI_T_NAME:
77 num->data.dvar = psi_decl_var_copy(num->data.dvar);
78 break;
79 default:
80 assert(0);
81 }
82 return num;
83 }
84
85 void psi_num_exp_free(struct psi_num_exp **exp_ptr)
86 {
87 if (*exp_ptr) {
88 struct psi_num_exp *exp = *exp_ptr;
89
90 *exp_ptr = NULL;
91 if (exp->token) {
92 free(exp->token);
93 }
94 switch (exp->type) {
95 case PSI_T_INT64:
96 case PSI_T_DOUBLE:
97 case PSI_T_ENUM:
98 case PSI_T_CONST:
99 break;
100 case PSI_T_NSNAME:
101 case PSI_T_NUMBER:
102 free(exp->data.numb);
103 break;
104 case PSI_T_NAME:
105 psi_decl_var_free(&exp->data.dvar);
106 break;
107 default:
108 assert(0);
109 }
110 if (exp->operand) {
111 psi_num_exp_free(&exp->operand);
112 }
113 free(exp);
114 }
115 }
116
117 static inline wint_t psi_num_exp_op_tok(token_t op)
118 {
119 switch (op) {
120 case PSI_T_LSHIFT:
121 return L'«';
122 case PSI_T_RSHIFT:
123 return L'»';
124 case PSI_T_PLUS:
125 return L'+';
126 case PSI_T_MINUS:
127 return L'-';
128 case PSI_T_ASTERISK:
129 return L'*';
130 case PSI_T_SLASH:
131 return L'/';
132 case PSI_T_AMPERSAND:
133 return L'&';
134 case PSI_T_CARET:
135 return L'^';
136 case PSI_T_PIPE:
137 return L'|';
138 default:
139 assert(0);
140 }
141 return 0;
142 }
143
144 void psi_num_exp_dump(int fd, struct psi_num_exp *exp)
145 {
146 while (exp) {
147 switch (exp->type) {
148 case PSI_T_INT64:
149 dprintf(fd, "%" PRId64, exp->data.ival.i64);
150 break;
151 case PSI_T_DOUBLE:
152 dprintf(fd, "%F", exp->data.ival.dval);
153 break;
154 case PSI_T_NUMBER:
155 case PSI_T_NSNAME:
156 dprintf(fd, "%s", exp->data.numb);
157 break;
158 case PSI_T_CONST:
159 dprintf(fd, "%s", exp->data.cnst->name);
160 break;
161 case PSI_T_ENUM:
162 dprintf(fd, "%s", exp->data.enm->name);
163 break;
164 case PSI_T_NAME:
165 psi_decl_var_dump(fd, exp->data.dvar);
166 break;
167 default:
168 assert(0);
169 }
170
171 if (exp->operand) {
172 dprintf(fd, " %c ", psi_num_exp_op_tok(exp->op));
173 }
174
175 exp = exp->operand;
176 }
177 }
178
179 static inline bool psi_num_exp_validate_enum(struct psi_data *data, struct psi_num_exp *exp,
180 struct psi_decl_enum *enm)
181 {
182 size_t i = 0;
183 struct psi_decl_enum_item *itm;
184
185 while (psi_plist_get(enm->items, i++, &itm)) {
186 if (!strcmp(itm->name, exp->data.dvar->name)) {
187 psi_decl_var_free(&exp->data.dvar);
188 exp->type = PSI_T_ENUM;
189 exp->data.enm = itm;
190 return psi_num_exp_validate(data, exp, NULL, NULL, NULL, NULL, enm);
191 }
192 }
193
194 return false;
195 }
196
197 bool psi_num_exp_validate(struct psi_data *data, struct psi_num_exp *exp,
198 struct psi_impl *impl, struct psi_decl *cb_decl, struct psi_let_exp *current_let,
199 struct psi_set_exp *current_set, struct psi_decl_enum *current_enum)
200 {
201 size_t i = 0;
202 impl_val tmp = {0};
203 struct psi_const *cnst;
204 struct psi_decl_enum *enm;
205
206 if (exp->operand) {
207 switch (exp->op) {
208 case PSI_T_LSHIFT:
209 exp->calc = psi_calc_bin_lshift;
210 break;
211 case PSI_T_RSHIFT:
212 exp->calc = psi_calc_bin_rshift;
213 break;
214 case PSI_T_PLUS:
215 exp->calc = psi_calc_add;
216 break;
217 case PSI_T_MINUS:
218 exp->calc = psi_calc_sub;
219 break;
220 case PSI_T_ASTERISK:
221 exp->calc = psi_calc_mul;
222 break;
223 case PSI_T_SLASH:
224 exp->calc = psi_calc_div;
225 break;
226 case PSI_T_AMPERSAND:
227 exp->calc = psi_calc_bin_and;
228 break;
229 case PSI_T_CARET:
230 exp->calc = psi_calc_bin_xor;
231 break;
232 case PSI_T_PIPE:
233 exp->calc = psi_calc_bin_or;
234 break;
235 default:
236 data->error(data, exp->token, PSI_WARNING,
237 "Unknown numeric operator (%d)", exp->op);
238 return false;
239 }
240 if (!psi_num_exp_validate(data, exp->operand, impl, cb_decl, current_let,
241 current_set, current_enum)) {
242 return false;
243 }
244 }
245
246 switch (exp->type) {
247 case PSI_T_CONST:
248 case PSI_T_INT64:
249 case PSI_T_DOUBLE:
250 case PSI_T_ENUM:
251 return true;
252
253 case PSI_T_NAME:
254 if (current_enum && psi_num_exp_validate_enum(data, exp, current_enum)) {
255 return true;
256 }
257 while (psi_plist_get(data->enums, i++, &enm)) {
258 if (psi_num_exp_validate_enum(data, exp, enm)) {
259 return true;
260 }
261 }
262 if (exp->data.dvar->arg) {
263 return true;
264 }
265 if (psi_decl_var_validate(data, exp->data.dvar, impl ? impl->decl : NULL,
266 current_let, current_set)) {
267 return true;
268 }
269 if (cb_decl && psi_decl_var_validate(data, exp->data.dvar, cb_decl, NULL, NULL)) {
270 return true;
271 }
272 data->error(data, exp->token, PSI_WARNING,
273 "Unknown variable '%s' in numeric expression",
274 exp->data.dvar->name);
275 return false;
276
277 case PSI_T_NSNAME:
278 while (psi_plist_get(data->consts, i++, &cnst)) {
279 if (!strcmp(cnst->name, exp->data.numb)) {
280 free(exp->data.numb);
281 exp->type = PSI_T_CONST;
282 exp->data.cnst = cnst;
283 return true;
284 }
285 }
286 data->error(data, exp->token, PSI_WARNING,
287 "Unknown constant '%s' in numeric expression",
288 exp->data.numb);
289 return false;
290
291 case PSI_T_NUMBER:
292 switch (is_numeric_string(exp->data.numb, strlen(exp->data.numb), (zend_long *) &tmp, (double *) &tmp, 0)) {
293 case IS_LONG:
294 free(exp->data.numb);
295 exp->type = PSI_T_INT64;
296 exp->data.ival.i64 = tmp.zend.lval;
297 return true;
298
299 case IS_DOUBLE:
300 free(exp->data.numb);
301 exp->type = PSI_T_DOUBLE;
302 exp->data.ival.dval = tmp.dval;
303 return true;
304 }
305 data->error(data, exp->token, PSI_WARNING, "Expected numeric entity (parser error?)");
306 return false;
307
308 default:
309 assert(0);
310 }
311
312 return false;
313 }
314
315 #include "Zend/zend_constants.h"
316
317 static inline token_t psi_num_exp_eval_constant(struct psi_num_exp *exp,
318 impl_val *res, struct psi_call_frame *frame)
319 {
320 switch (exp->data.cnst->type->type) {
321 case PSI_T_INT:
322 res->i64 = zend_get_constant_str(exp->data.cnst->name,
323 strlen(exp->data.cnst->name))->value.lval;
324 if (frame) PSI_DEBUG_PRINT(frame->context, "%" PRIi64, res->i64);
325 return PSI_T_INT64;
326 case PSI_T_FLOAT:
327 res->dval = zend_get_constant_str(exp->data.cnst->name,
328 strlen(exp->data.cnst->name))->value.dval;
329 if (frame) PSI_DEBUG_PRINT(frame->context, "%" PRIdval, res->dval);
330 return PSI_T_DOUBLE;
331 default:
332 if (frame) PSI_DEBUG_PRINT(frame->context, "?(t=%d)", exp->data.cnst->type->type);
333 return 0;
334 }
335 }
336
337 static inline void psi_num_exp_verify_result(token_t t, impl_val *res, struct psi_call_frame *frame)
338 {
339 switch (t) {
340 case PSI_T_INT8:
341 case PSI_T_UINT8:
342 if (frame) PSI_DEBUG_PRINT(frame->context, "%" PRIi8, res->i8);
343 break;
344 case PSI_T_INT16:
345 case PSI_T_UINT16:
346 if (frame) PSI_DEBUG_PRINT(frame->context, "%" PRIi16, res->i16);
347 break;
348 case PSI_T_INT32:
349 case PSI_T_UINT32:
350 if (frame) PSI_DEBUG_PRINT(frame->context, "%" PRIi32, res->i32);
351 break;
352 case PSI_T_INT64:
353 case PSI_T_UINT64:
354 if (frame) PSI_DEBUG_PRINT(frame->context, "%" PRIi64, res->i64);
355 break;
356 case PSI_T_FLOAT:
357 if (frame) PSI_DEBUG_PRINT(frame->context, "%" PRIfval, res->fval);
358 break;
359 case PSI_T_DOUBLE:
360 if (frame) PSI_DEBUG_PRINT(frame->context, "%" PRIdval, res->dval);
361 break;
362 default:
363 assert(0);
364 }
365 }
366
367 static inline token_t psi_num_exp_eval_decl_var(struct psi_num_exp *exp,
368 impl_val *res, struct psi_call_frame *frame)
369 {
370 impl_val *ref;
371 struct psi_call_frame_symbol *sym;
372 struct psi_decl_type *real;
373 size_t size;
374
375 real = psi_decl_type_get_real(exp->data.dvar->arg->type);
376 size = psi_decl_arg_get_size(exp->data.dvar->arg);
377 sym = psi_call_frame_fetch_symbol(frame, exp->data.dvar);
378 ref = deref_impl_val(sym->ptr, exp->data.dvar);
379
380 memcpy(res, ref, size);
381
382 return real->type;
383 }
384
385 static inline token_t psi_num_exp_eval(struct psi_num_exp *exp, impl_val *res,
386 struct psi_call_frame *frame)
387 {
388 switch (exp->type) {
389 case PSI_T_INT64:
390 *res = exp->data.ival;
391 if (frame) PSI_DEBUG_PRINT(frame->context, "%" PRIi64, res->i64);
392 return PSI_T_INT64;
393
394 case PSI_T_DOUBLE:
395 *res = exp->data.ival;
396 if (frame) PSI_DEBUG_PRINT(frame->context, "%" PRIdval, res->dval);
397 return exp->type;
398
399 case PSI_T_ENUM:
400 res->i64 = exp->data.enm->val;
401 if (frame) PSI_DEBUG_PRINT(frame->context, "%" PRIi64, res->i64);
402 return PSI_T_INT64;
403
404 case PSI_T_CONST:
405 return psi_num_exp_eval_constant(exp, res, frame);
406 break;
407
408 case PSI_T_NAME:
409 return psi_num_exp_eval_decl_var(exp, res, frame);
410 break;
411
412 default:
413 assert(0);
414 }
415 return 0;
416 }
417
418 #include "context.h"
419
420 static inline int psi_num_exp_op_cmp(token_t op1, token_t op2)
421 {
422 assert(op1 >= PSI_T_LSHIFT && op2 <= PSI_T_PIPE);
423 assert(op2 >= PSI_T_LSHIFT && op2 <= PSI_T_PIPE);
424
425 switch (op1) {
426 case PSI_T_LSHIFT:
427 case PSI_T_RSHIFT:
428 return op2 > PSI_T_RSHIFT;
429
430 case PSI_T_PLUS:
431 case PSI_T_MINUS:
432 return op2 > PSI_T_MINUS ? 1 : (op2 < PSI_T_PLUS ? -1 : 0);
433
434 case PSI_T_ASTERISK:
435 case PSI_T_SLASH:
436 return op2 > PSI_T_SLASH ? 1 : (op2 < PSI_T_ASTERISK ? -1 : 0);
437
438 case PSI_T_AMPERSAND:
439 case PSI_T_CARET:
440 case PSI_T_PIPE:
441 return -1 * (op2 < PSI_T_AMPERSAND);
442 }
443
444 return 0;
445 }
446
447 token_t psi_num_exp_exec(struct psi_num_exp *exp, impl_val *res,
448 struct psi_call_frame *frame)
449 {
450 impl_val num = {0};
451 token_t num_type;
452
453 num_type = psi_num_exp_eval(exp, &num, frame);
454
455 if (exp->operand) {
456 impl_val rhs;
457 token_t rhs_type, res_type;
458
459 /* only if there's a following op, and we have a higher precedence */
460 if (exp->operand->operand &&
461 psi_num_exp_op_cmp(exp->op, exp->operand->op) < 0) {
462 impl_val tmp, lhs;
463 token_t tmp_type, lhs_type;
464
465 if (frame) PSI_DEBUG_PRINT(frame->context, " %lc ", psi_num_exp_op_tok(exp->op));
466
467 tmp_type = psi_num_exp_eval(exp->operand, &tmp, frame);
468 lhs_type = exp->calc(num_type, &num, tmp_type, &tmp, &lhs);
469
470 if (frame) PSI_DEBUG_PRINT(frame->context, " %c ", '=');
471 psi_num_exp_verify_result(lhs_type, &lhs, frame);
472 if (frame) PSI_DEBUG_PRINT(frame->context, " %c", '\n');
473
474 rhs_type = psi_num_exp_exec(exp->operand->operand, &rhs, frame);
475 res_type = exp->operand->calc(lhs_type, &lhs, rhs_type, &rhs, res);
476 } else {
477 if (frame) PSI_DEBUG_PRINT(frame->context, " %lc ", psi_num_exp_op_tok(exp->op));
478
479 rhs_type = psi_num_exp_exec(exp->operand, &rhs, frame);
480 res_type = exp->calc(num_type, &num, rhs_type, &rhs, res);
481 }
482
483 if (frame) PSI_DEBUG_PRINT(frame->context, " %c ", '=');
484 psi_num_exp_verify_result(res_type, res, frame);
485 if (frame) PSI_DEBUG_PRINT(frame->context, " %c", '\n');
486
487 return res_type;
488 }
489
490 *res = num;
491
492 return num_type;
493 }