unwind: use the shared success_fail_return() function
[smatch.git] / builtin.c
blob3a29c3aec8a639a0a49631caef05156bd9e34348
1 /*
2 * builtin evaluation & expansion.
4 * Copyright (C) 2003 Transmeta Corp.
5 * 2003-2004 Linus Torvalds
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
26 #include "builtin.h"
27 #include "expression.h"
28 #include "evaluate.h"
29 #include "expand.h"
30 #include "symbol.h"
31 #include "compat/bswap.h"
32 #include <stdarg.h>
34 #define dyntype incomplete_ctype
35 static bool is_dynamic_type(struct symbol *t)
37 if (t->type == SYM_NODE)
38 t = t->ctype.base_type;
39 return t == &dyntype;
42 static int evaluate_to_int_const_expr(struct expression *expr)
44 expr->ctype = &int_ctype;
45 expr->flags |= CEF_SET_ICE;
46 return 1;
49 static int evaluate_pure_unop(struct expression *expr)
51 struct expression *arg = first_expression(expr->args);
52 int flags = arg->flags;
55 * Allow such functions with a constant integer expression
56 * argument to be treated as a *constant* integer.
57 * This allow us to use them in switch() { case ...:
59 flags |= (flags & CEF_ICE) ? CEF_SET_INT : 0;
60 expr->flags = flags;
61 return 1;
65 * eval_args - check the number of arguments and evaluate them.
67 static int eval_args(struct expression *expr, int n)
69 struct expression *arg;
70 struct symbol *sym;
71 const char *msg;
72 int rc = 1;
74 FOR_EACH_PTR(expr->args, arg) {
75 if (n-- == 0) {
76 msg = "too many arguments";
77 goto error;
79 if (!evaluate_expression(arg))
80 rc = 0;
81 } END_FOR_EACH_PTR(arg);
82 if (n > 0) {
83 msg = "not enough arguments";
84 goto error;
86 return rc;
88 error:
89 sym = expr->fn->ctype;
90 expression_error(expr, "%s for %s", msg, show_ident(sym->ident));
91 return 0;
94 static int args_prototype(struct expression *expr)
96 struct symbol *fntype = expr->fn->ctype->ctype.base_type;
97 int n = symbol_list_size(fntype->arguments);
98 return eval_args(expr, n);
101 static int args_triadic(struct expression *expr)
103 return eval_args(expr, 3);
106 static int evaluate_choose(struct expression *expr)
108 struct expression_list *list = expr->args;
109 struct expression *arg, *args[3];
110 int n = 0;
112 /* there will be exactly 3; we'd already verified that */
113 FOR_EACH_PTR(list, arg) {
114 args[n++] = arg;
115 } END_FOR_EACH_PTR(arg);
117 *expr = get_expression_value(args[0]) ? *args[1] : *args[2];
119 return 1;
122 static int expand_expect(struct expression *expr, int cost)
124 struct expression *arg = first_ptr_list((struct ptr_list *) expr->args);
126 if (arg)
127 *expr = *arg;
128 return 0;
132 * __builtin_warning() has type "int" and always returns 1,
133 * so that you can use it in conditionals or whatever
135 static int expand_warning(struct expression *expr, int cost)
137 struct expression *arg;
138 struct expression_list *arglist = expr->args;
140 FOR_EACH_PTR (arglist, arg) {
142 * Constant strings get printed out as a warning. By the
143 * time we get here, the EXPR_STRING has been fully
144 * evaluated, so by now it's an anonymous symbol with a
145 * string initializer.
147 * Just for the heck of it, allow any constant string
148 * symbol.
150 if (arg->type == EXPR_SYMBOL) {
151 struct symbol *sym = arg->symbol;
152 if (sym->initializer && sym->initializer->type == EXPR_STRING) {
153 struct string *string = sym->initializer->string;
154 warning(expr->pos, "%*s", string->length-1, string->data);
156 continue;
160 * Any other argument is a conditional. If it's
161 * non-constant, or it is false, we exit and do
162 * not print any warning.
164 if (arg->type != EXPR_VALUE)
165 goto out;
166 if (!arg->value)
167 goto out;
168 } END_FOR_EACH_PTR(arg);
169 out:
170 expr->type = EXPR_VALUE;
171 expr->value = 1;
172 expr->taint = 0;
173 return 0;
176 /* The arguments are constant if the cost of all of them is zero */
177 static int expand_constant_p(struct expression *expr, int cost)
179 expr->type = EXPR_VALUE;
180 expr->value = !cost;
181 expr->taint = 0;
182 return 0;
185 /* The arguments are safe, if their cost is less than SIDE_EFFECTS */
186 static int expand_safe_p(struct expression *expr, int cost)
188 expr->type = EXPR_VALUE;
189 expr->value = (cost < SIDE_EFFECTS);
190 expr->taint = 0;
191 return 0;
194 static struct symbol_op constant_p_op = {
195 .evaluate = evaluate_to_int_const_expr,
196 .expand = expand_constant_p
199 static struct symbol_op safe_p_op = {
200 .evaluate = evaluate_to_int_const_expr,
201 .expand = expand_safe_p
204 static struct symbol_op warning_op = {
205 .evaluate = evaluate_to_int_const_expr,
206 .expand = expand_warning
209 static struct symbol_op expect_op = {
210 .expand = expand_expect
213 static struct symbol_op choose_op = {
214 .args = args_triadic,
215 .evaluate = evaluate_choose,
218 /* The argument is constant and valid if the cost is zero */
219 static int expand_bswap(struct expression *expr, int cost)
221 struct expression *arg;
222 long long val;
224 if (cost)
225 return cost;
227 /* the arguments number & type have already been checked */
228 arg = first_expression(expr->args);
229 val = get_expression_value_silent(arg);
230 switch (expr->ctype->bit_size) {
231 case 16: expr->value = bswap16(val); break;
232 case 32: expr->value = bswap32(val); break;
233 case 64: expr->value = bswap64(val); break;
234 default: /* impossible error */
235 return SIDE_EFFECTS;
238 expr->type = EXPR_VALUE;
239 expr->taint = 0;
240 return 0;
243 static struct symbol_op bswap_op = {
244 .evaluate = evaluate_pure_unop,
245 .expand = expand_bswap,
249 #define EXPAND_FINDBIT(name) \
250 static int expand_##name(struct expression *expr, int cost) \
252 struct expression *arg; \
253 long long val; \
255 if (cost) \
256 return cost; \
258 arg = first_expression(expr->args); \
259 val = get_expression_value_silent(arg); \
260 switch (arg->ctype->bit_size) { \
261 case sizeof(int) * 8: \
262 val = __builtin_##name(val); break; \
263 case sizeof(long long) * 8: \
264 val = __builtin_##name##ll(val); break; \
265 default: /* impossible error */ \
266 return SIDE_EFFECTS; \
269 expr->value = val; \
270 expr->type = EXPR_VALUE; \
271 expr->taint = 0; \
272 return 0; \
275 static struct symbol_op name##_op = { \
276 .evaluate = evaluate_pure_unop, \
277 .expand = expand_##name, \
280 EXPAND_FINDBIT(clz);
281 EXPAND_FINDBIT(ctz);
282 EXPAND_FINDBIT(clrsb);
283 EXPAND_FINDBIT(ffs);
284 EXPAND_FINDBIT(parity);
285 EXPAND_FINDBIT(popcount);
287 static int evaluate_fp_unop(struct expression *expr)
289 struct expression *arg;
291 if (!eval_args(expr, 1))
292 return 0;
294 arg = first_expression(expr->args);
295 if (!is_float_type(arg->ctype)) {
296 expression_error(expr, "non-floating-point argument in call to %s()",
297 show_ident(expr->fn->ctype->ident));
298 return 0;
300 return 1;
303 static struct symbol_op fp_unop_op = {
304 .evaluate = evaluate_fp_unop,
308 static int expand_isdigit(struct expression *expr, int cost)
310 struct expression *arg = first_expression(expr->args);
311 long long val = get_expression_value_silent(arg);
313 if (cost)
314 return cost;
316 expr->value = (val >= '0') && (val <= '9');
317 expr->type = EXPR_VALUE;
318 expr->taint = 0;
319 return 0;
322 static struct symbol_op isdigit_op = {
323 .evaluate = evaluate_pure_unop,
324 .expand = expand_isdigit,
328 static int evaluate_overflow_gen(struct expression *expr, int ptr)
330 struct expression *arg;
331 int n = 0;
333 /* there will be exactly 3; we'd already verified that */
334 FOR_EACH_PTR(expr->args, arg) {
335 struct symbol *type;
337 n++;
338 if (!arg || !(type = arg->ctype))
339 return 0;
340 // 1st & 2nd args must be a basic integer type
341 // 3rd arg must be a pointer to such a type.
342 if (n == 3 && ptr) {
343 if (type->type == SYM_NODE)
344 type = type->ctype.base_type;
345 if (!type)
346 return 0;
347 if (type->type != SYM_PTR)
348 goto err;
349 type = type->ctype.base_type;
350 if (!type)
351 return 0;
353 if (type->type == SYM_NODE)
354 type = type->ctype.base_type;
355 if (!type)
356 return 0;
357 if (type->ctype.base_type != &int_type || type == &bool_ctype)
358 goto err;
359 } END_FOR_EACH_PTR(arg);
361 // the builtin returns a bool
362 expr->ctype = &bool_ctype;
363 return 1;
365 err:
366 sparse_error(arg->pos, "invalid type for argument %d:", n);
367 info(arg->pos, " %s", show_typename(arg->ctype));
368 expr->ctype = &bad_ctype;
369 return 0;
372 static int evaluate_overflow(struct expression *expr)
374 return evaluate_overflow_gen(expr, 1);
377 static struct symbol_op overflow_op = {
378 .args = args_triadic,
379 .evaluate = evaluate_overflow,
382 static int evaluate_overflow_p(struct expression *expr)
384 return evaluate_overflow_gen(expr, 0);
387 static struct symbol_op overflow_p_op = {
388 .args = args_triadic,
389 .evaluate = evaluate_overflow_p,
394 // Evaluate the arguments of 'generic' integer operators.
396 // Parameters with a complete type are used like in a normal prototype.
397 // The first parameter with a 'dynamic' type will be consider
398 // as polymorphic and for each calls will be instancied with the type
399 // of its effective argument.
400 // The next dynamic parameters will the use this polymorphic type.
401 // This allows to declare functions with some parameters having
402 // a type variably defined at call time:
403 // int foo(int, T, T);
404 static int evaluate_generic_int_op(struct expression *expr)
406 struct symbol *fntype = expr->fn->ctype->ctype.base_type;
407 struct symbol_list *types = NULL;
408 struct symbol *ctype = NULL;
409 struct expression *arg;
410 struct symbol *t;
411 int n = 0;
413 PREPARE_PTR_LIST(fntype->arguments, t);
414 FOR_EACH_PTR(expr->args, arg) {
415 n++;
417 if (!is_dynamic_type(t)) {
419 } else if (!ctype) {
420 // first 'dynamic' type, check that it's an integer
421 t = arg->ctype;
422 if (!t)
423 return 0;
424 if (t->type == SYM_NODE)
425 t = t->ctype.base_type;
426 if (!t)
427 return 0;
428 if (t->ctype.base_type != &int_type)
429 goto err;
431 // next 'dynamic' arguments will use this type
432 ctype = t;
433 } else {
434 // use the previous 'dynamic' type
435 t = ctype;
437 add_ptr_list(&types, t);
438 NEXT_PTR_LIST(t);
439 } END_FOR_EACH_PTR(arg);
440 FINISH_PTR_LIST(t);
441 return evaluate_arguments(types, expr->args);
443 err:
444 sparse_error(arg->pos, "non-integer type for argument %d:", n);
445 info(arg->pos, " %s", show_typename(arg->ctype));
446 expr->ctype = &bad_ctype;
447 return 0;
450 struct symbol_op generic_int_op = {
451 .args = args_prototype,
452 .evaluate = evaluate_generic_int_op,
456 static int eval_atomic_common(struct expression *expr)
458 struct symbol *fntype = expr->fn->ctype->ctype.base_type;
459 struct symbol_list *types = NULL;
460 struct symbol *ctype = NULL;
461 struct symbol *t;
462 struct expression *arg;
463 int n = 0;
465 // The number of arguments has already be verified.
466 // The first arg must be a pointer to an integral type.
467 PREPARE_PTR_LIST(fntype->arguments, t);
468 FOR_EACH_PTR(expr->args, arg) {
469 struct symbol *ptrtype = NULL;
471 if (++n == 1) {
472 t = arg->ctype;
473 if (!t)
474 return 0;
475 if (t->type == SYM_NODE)
476 t = t->ctype.base_type;
477 if (!t)
478 return 0;
479 if (t->type != SYM_PTR)
480 goto err;
481 ptrtype = t;
482 t = t->ctype.base_type;
483 if (!t)
484 return 0;
485 if (t->type == SYM_NODE)
486 t = t->ctype.base_type;
487 if (!t)
488 return 0;
489 if (t->type != SYM_PTR && t->ctype.base_type != &int_type)
490 goto err;
491 ctype = t;
492 t = ptrtype;
493 } else if (is_dynamic_type(t)) {
494 t = ctype;
495 } else if (t == &ptr_ctype) {
496 t = ptrtype;
498 add_ptr_list(&types, t);
499 NEXT_PTR_LIST(t);
500 } END_FOR_EACH_PTR(arg);
501 FINISH_PTR_LIST(t);
503 if (!expr->ctype) // set the return type, if needed
504 expr->ctype = ctype;
505 return evaluate_arguments(types, expr->args);
507 err:
508 sparse_error(arg->pos, "invalid type for argument %d:", n);
509 info(arg->pos, " %s", show_typename(arg->ctype));
510 expr->ctype = &bad_ctype;
511 return 0;
514 static struct symbol_op atomic_op = {
515 .args = args_prototype,
516 .evaluate = eval_atomic_common,
521 // expand __builtin_object_size()
523 // :note: type 1 and type 3 are not supported because the
524 // needed information isn't available after evaluation.
525 static int expand_object_size(struct expression *expr, int cost)
527 struct expression *arg = first_expression(expr->args);
528 int type = get_expression_value_silent(ptr_list_nth(expr->args, 1));
529 unsigned long val = -1, off = 0;
531 while (arg) {
532 switch (arg->type) {
533 case EXPR_IMPLIED_CAST:
534 case EXPR_CAST:
535 // ignore those
536 arg = arg->cast_expression;
537 continue;
538 case EXPR_BINOP:
539 // a constant add is (maybe) an offset
540 if (!arg->right || arg->op != '+' || arg->right->type != EXPR_VALUE)
541 break;
542 off += arg->right->value;
543 arg = arg->left;
544 continue;
545 case EXPR_PREOP:
546 // a deref is just intermediate variable
547 // and so the offset needs to be zeroed.
548 if (arg->op == '*') {
549 struct expression *parent = arg;
550 arg = arg->unop;
551 off = 0;
552 switch (arg->type) {
553 case EXPR_SYMBOL:
554 arg = arg->symbol->initializer;
555 if (arg == parent) {
556 // stop at self-initialized vars
557 // and do not expand them.
558 arg = NULL;
559 val = -1;
560 break;
562 continue;
563 default:
564 break;
567 break;
568 case EXPR_SYMBOL:
569 // the symbol we're looking after
570 val = bits_to_bytes(arg->symbol->bit_size);
571 break;
572 case EXPR_CALL:
573 // use alloc_size() attribute but only after linearization.
574 return UNSAFE;
575 default:
576 break;
578 break;
581 if (val == -1)
582 val = (type & 2) ? 0 : val;
583 else if (type & 1)
584 return UNSAFE;
585 else
586 val -= off;
588 expr->flags |= CEF_SET_ICE;
589 expr->type = EXPR_VALUE;
590 expr->value = val;
591 expr->taint = 0;
592 return 0;
595 static struct symbol_op object_size_op = {
596 .expand = expand_object_size,
600 * Builtin functions
602 static struct symbol size_t_alias;
604 static struct symbol *get_ctype(struct symbol *sym)
606 if (sym == &size_t_alias)
607 return size_t_ctype;
608 return sym;
611 static void declare_builtin(int stream, const struct builtin_fn *entry)
613 struct symbol *sym = create_symbol(stream, entry->name, SYM_NODE, NS_SYMBOL);
614 struct symbol *fun = alloc_symbol(sym->pos, SYM_FN);
615 struct symbol *arg;
616 int i;
618 sym->ctype.base_type = fun;
619 sym->ctype.modifiers = MOD_TOPLEVEL;
620 sym->builtin = 1;
621 sym->op = entry->op;
623 fun->ctype.base_type = get_ctype(entry->ret_type);
624 fun->variadic = entry->variadic;
626 for (i = 0; (arg = entry->args[i]); i++) {
627 struct symbol *anode = alloc_symbol(sym->pos, SYM_NODE);
628 anode->ctype.base_type = get_ctype(arg);
629 add_symbol(&fun->arguments, anode);
633 void declare_builtins(int stream, const struct builtin_fn tbl[])
635 if (!tbl)
636 return;
638 while (tbl->name)
639 declare_builtin(stream, tbl++);
642 static const struct builtin_fn builtins_common[] = {
643 #define size_t_ctype &size_t_alias
644 #define va_list_ctype &ptr_ctype
645 #define vol_ptr &volatile_ptr_ctype
646 { "__atomic_add_fetch", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
647 { "__atomic_always_lock_free", &bool_ctype, 0, { size_t_ctype, vol_ptr }},
648 { "__atomic_and_fetch", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
649 { "__atomic_clear", &void_ctype, 0, { &volatile_bool_ptr_ctype, &int_ctype }},
650 { "__atomic_compare_exchange", &bool_ctype, 0, { vol_ptr, &ptr_ctype, &ptr_ctype, &bool_ctype, &int_ctype, &int_ctype }, .op = &atomic_op },
651 { "__atomic_compare_exchange_n", &bool_ctype, 0, { vol_ptr, &ptr_ctype, &dyntype, &bool_ctype, &int_ctype, &int_ctype }, .op = &atomic_op },
652 { "__atomic_exchange", &void_ctype, 0, { vol_ptr, &ptr_ctype, &ptr_ctype, &int_ctype }, .op = &atomic_op },
653 { "__atomic_exchange_n", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
654 { "__atomic_fetch_add", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
655 { "__atomic_fetch_and", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
656 { "__atomic_fetch_nand",NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
657 { "__atomic_fetch_or", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
658 { "__atomic_fetch_sub", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
659 { "__atomic_fetch_xor", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
660 { "__atomic_is_lock_free", &bool_ctype, 0, { size_t_ctype, vol_ptr }},
661 { "__atomic_load", &void_ctype, 0, { vol_ptr, &ptr_ctype, &int_ctype }, .op = &atomic_op },
662 { "__atomic_load_n", NULL, 0, { vol_ptr, &int_ctype }, .op = &atomic_op },
663 { "__atomic_nand_fetch",NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
664 { "__atomic_or_fetch", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
665 { "__atomic_signal_fence", &void_ctype, 0, { &int_ctype }},
666 { "__atomic_store", &void_ctype, 0, { vol_ptr, &ptr_ctype, &int_ctype }, .op = &atomic_op },
667 { "__atomic_store_n", &void_ctype, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
668 { "__atomic_sub_fetch", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
669 { "__atomic_test_and_set", &bool_ctype, 0, { vol_ptr, &int_ctype }},
670 { "__atomic_thread_fence", &void_ctype, 0, { &int_ctype }},
671 { "__atomic_xor_fetch", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
672 { "__builtin_choose_expr", NULL, 1, .op = &choose_op },
673 { "__builtin_constant_p", NULL, 1, .op = &constant_p_op },
674 { "__builtin_expect", &long_ctype, 0, { &long_ctype ,&long_ctype }, .op = &expect_op },
675 { "__builtin_safe_p", NULL, 1, .op = &safe_p_op },
676 { "__builtin_warning", NULL, 1, .op = &warning_op },
678 { "__builtin_abort", &void_ctype, 0 },
679 { "__builtin_abs", &int_ctype , 0, { &int_ctype }},
680 { "__builtin_add_overflow", &bool_ctype, 1, .op = &overflow_op },
681 { "__builtin_add_overflow_p", &bool_ctype, 1, .op = &overflow_p_op },
682 { "__builtin_alloca", &ptr_ctype, 0, { size_t_ctype }},
683 { "__builtin_bcmp", &int_ctype , 0, { &const_ptr_ctype, &const_ptr_ctype, size_t_ctype }},
684 { "__builtin_bcopy", &void_ctype, 0, { &const_ptr_ctype, &ptr_ctype, size_t_ctype }},
685 { "__builtin_bswap16", &ushort_ctype, 0, { &ushort_ctype }, .op = &bswap_op },
686 { "__builtin_bswap32", &uint_ctype, 0, { &uint_ctype }, .op = &bswap_op },
687 { "__builtin_bswap64", &ullong_ctype, 0, { &ullong_ctype }, .op = &bswap_op },
688 { "__builtin_bzero", &void_ctype, 0, { &ptr_ctype, size_t_ctype }},
689 { "__builtin_calloc", &ptr_ctype, 0, { size_t_ctype, size_t_ctype }},
690 { "__builtin_clrsb", &int_ctype, 0, { &int_ctype }, .op = &clrsb_op },
691 { "__builtin_clrsbl", &int_ctype, 0, { &long_ctype }, .op = &clrsb_op },
692 { "__builtin_clrsbll", &int_ctype, 0, { &llong_ctype }, .op = &clrsb_op },
693 { "__builtin_clz", &int_ctype, 0, { &int_ctype }, .op = &clz_op },
694 { "__builtin_clzl", &int_ctype, 0, { &long_ctype }, .op = &clz_op },
695 { "__builtin_clzll", &int_ctype, 0, { &llong_ctype }, .op = &clz_op },
696 { "__builtin_ctz", &int_ctype, 0, { &int_ctype }, .op = &ctz_op },
697 { "__builtin_ctzl", &int_ctype, 0, { &long_ctype }, .op = &ctz_op },
698 { "__builtin_ctzll", &int_ctype, 0, { &llong_ctype }, .op = &ctz_op },
699 { "__builtin_exit", &void_ctype, 0, { &int_ctype }},
700 { "__builtin_extract_return_addr", &ptr_ctype, 0, { &ptr_ctype }},
701 { "__builtin_fabs", &double_ctype, 0, { &double_ctype }},
702 { "__builtin_ffs", &int_ctype, 0, { &int_ctype }, .op = &ffs_op },
703 { "__builtin_ffsl", &int_ctype, 0, { &long_ctype }, .op = &ffs_op },
704 { "__builtin_ffsll", &int_ctype, 0, { &llong_ctype }, .op = &ffs_op },
705 { "__builtin_fma", &double_ctype, 0, { &double_ctype, &double_ctype, &double_ctype }},
706 { "__builtin_fmaf", &float_ctype, 0, { &float_ctype, &float_ctype, &float_ctype }},
707 { "__builtin_fmal", &ldouble_ctype, 0, { &ldouble_ctype, &ldouble_ctype, &ldouble_ctype }},
708 { "__builtin_frame_address", &ptr_ctype, 0, { &uint_ctype }},
709 { "__builtin_free", &void_ctype, 0, { &ptr_ctype }},
710 { "__builtin_huge_val", &double_ctype, 0 },
711 { "__builtin_huge_valf", &float_ctype, 0 },
712 { "__builtin_huge_vall", &ldouble_ctype, 0 },
713 { "__builtin_index", &string_ctype, 0, { &const_string_ctype, &int_ctype }},
714 { "__builtin_inf", &double_ctype, 0 },
715 { "__builtin_inff", &float_ctype, 0 },
716 { "__builtin_infl", &ldouble_ctype, 0 },
717 { "__builtin_isdigit", &int_ctype, 0, { &int_ctype }, .op = &isdigit_op },
718 { "__builtin_isfinite", &int_ctype, 1, .op = &fp_unop_op },
719 { "__builtin_isgreater", &int_ctype, 0, { &float_ctype, &float_ctype }},
720 { "__builtin_isgreaterequal", &int_ctype, 0, { &float_ctype, &float_ctype }},
721 { "__builtin_isinf", &int_ctype, 1, .op = &fp_unop_op },
722 { "__builtin_isinf_sign", &int_ctype, 1, .op = &fp_unop_op },
723 { "__builtin_isless", &int_ctype, 0, { &float_ctype, &float_ctype }},
724 { "__builtin_islessequal", &int_ctype, 0, { &float_ctype, &float_ctype }},
725 { "__builtin_islessgreater", &int_ctype, 0, { &float_ctype, &float_ctype }},
726 { "__builtin_isnan", &int_ctype, 1, .op = &fp_unop_op },
727 { "__builtin_isnormal", &int_ctype, 1, .op = &fp_unop_op },
728 { "__builtin_isunordered", &int_ctype, 0, { &float_ctype, &float_ctype }},
729 { "__builtin_labs", &long_ctype, 0, { &long_ctype }},
730 { "__builtin_llabs", &llong_ctype, 0, { &llong_ctype }},
731 { "__builtin_malloc", &ptr_ctype, 0, { size_t_ctype }},
732 { "__builtin_memchr", &ptr_ctype, 0, { &const_ptr_ctype, &int_ctype, size_t_ctype }},
733 { "__builtin_memcmp", &int_ctype, 0, { &const_ptr_ctype, &const_ptr_ctype, size_t_ctype }},
734 { "__builtin_memcpy", &ptr_ctype, 0, { &ptr_ctype, &const_ptr_ctype, size_t_ctype }},
735 { "__builtin_memmove", &ptr_ctype, 0, { &ptr_ctype, &const_ptr_ctype, size_t_ctype }},
736 { "__builtin_mempcpy", &ptr_ctype, 0, { &ptr_ctype, &const_ptr_ctype, size_t_ctype }},
737 { "__builtin_memset", &ptr_ctype, 0, { &ptr_ctype, &int_ctype, size_t_ctype }},
738 { "__builtin_mul_overflow", &bool_ctype, 1, .op = &overflow_op },
739 { "__builtin_mul_overflow_p", &bool_ctype, 1, .op = &overflow_p_op },
740 { "__builtin_nan", &double_ctype, 0, { &const_string_ctype }},
741 { "__builtin_nanf", &float_ctype, 0, { &const_string_ctype }},
742 { "__builtin_nanl", &ldouble_ctype, 0, { &const_string_ctype }},
743 { "__builtin_object_size", size_t_ctype, 0, { &const_ptr_ctype, &int_ctype }, .op = &object_size_op},
744 { "__builtin_parity", &int_ctype, 0, { &uint_ctype }, .op = &parity_op },
745 { "__builtin_parityl", &int_ctype, 0, { &ulong_ctype }, .op = &parity_op },
746 { "__builtin_parityll", &int_ctype, 0, { &ullong_ctype }, .op = &parity_op },
747 { "__builtin_popcount", &int_ctype, 0, { &uint_ctype }, .op = &popcount_op },
748 { "__builtin_popcountl", &int_ctype, 0, { &ulong_ctype }, .op = &popcount_op },
749 { "__builtin_popcountll", &int_ctype, 0, { &ullong_ctype }, .op = &popcount_op },
750 { "__builtin_prefetch", &void_ctype, 1, { &const_ptr_ctype }},
751 { "__builtin_printf", &int_ctype, 1, { &const_string_ctype }},
752 { "__builtin_puts", &int_ctype, 0, { &const_string_ctype }},
753 { "__builtin_realloc", &ptr_ctype, 0, { &ptr_ctype, size_t_ctype }},
754 { "__builtin_return_address", &ptr_ctype, 0, { &uint_ctype }},
755 { "__builtin_rindex", &string_ctype, 0, { &const_string_ctype, &int_ctype }},
756 { "__builtin_sadd_overflow", &bool_ctype, 0, { &int_ctype, &int_ctype, &int_ptr_ctype }},
757 { "__builtin_saddl_overflow", &bool_ctype, 0, { &long_ctype, &long_ctype, &long_ptr_ctype }},
758 { "__builtin_saddll_overflow", &bool_ctype, 0, { &llong_ctype, &llong_ctype, &llong_ptr_ctype }},
759 { "__builtin_signbit", &int_ctype, 1 , .op = &fp_unop_op },
760 { "__builtin_smul_overflow", &bool_ctype, 0, { &int_ctype, &int_ctype, &int_ptr_ctype }},
761 { "__builtin_smull_overflow", &bool_ctype, 0, { &long_ctype, &long_ctype, &long_ptr_ctype }},
762 { "__builtin_smulll_overflow", &bool_ctype, 0, { &llong_ctype, &llong_ctype, &llong_ptr_ctype }},
763 { "__builtin_snprintf", &int_ctype, 1, { &string_ctype, size_t_ctype, &const_string_ctype }},
764 { "__builtin_sprintf", &int_ctype, 1, { &string_ctype, &const_string_ctype }},
765 { "__builtin_ssub_overflow", &bool_ctype, 0, { &int_ctype, &int_ctype, &int_ptr_ctype }},
766 { "__builtin_ssubl_overflow", &bool_ctype, 0, { &long_ctype, &long_ctype, &long_ptr_ctype }},
767 { "__builtin_ssubll_overflow", &bool_ctype, 0, { &llong_ctype, &llong_ctype, &llong_ptr_ctype }},
768 { "__builtin_stpcpy", &string_ctype, 0, { &const_string_ctype, &const_string_ctype }},
769 { "__builtin_stpncpy", &string_ctype, 0, { &const_string_ctype, &const_string_ctype, size_t_ctype }},
770 { "__builtin_strcasecmp", &int_ctype, 0, { &const_string_ctype, &const_string_ctype }},
771 { "__builtin_strcasestr", &string_ctype, 0, { &const_string_ctype, &const_string_ctype }},
772 { "__builtin_strcat", &string_ctype, 0, { &string_ctype, &const_string_ctype }},
773 { "__builtin_strchr", &string_ctype, 0, { &const_string_ctype, &int_ctype }},
774 { "__builtin_strcmp", &int_ctype, 0, { &const_string_ctype, &const_string_ctype }},
775 { "__builtin_strcpy", &string_ctype, 0, { &string_ctype, &const_string_ctype }},
776 { "__builtin_strcspn", size_t_ctype, 0, { &const_string_ctype, &const_string_ctype }},
777 { "__builtin_strdup", &string_ctype, 0, { &const_string_ctype }},
778 { "__builtin_strlen", size_t_ctype, 0, { &const_string_ctype }},
779 { "__builtin_strncasecmp", &int_ctype, 0, { &const_string_ctype, &const_string_ctype, size_t_ctype }},
780 { "__builtin_strncat", &string_ctype, 0, { &string_ctype, &const_string_ctype, size_t_ctype }},
781 { "__builtin_strncmp", &int_ctype, 0, { &const_string_ctype, &const_string_ctype, size_t_ctype }},
782 { "__builtin_strncpy", &string_ctype, 0, { &string_ctype, &const_string_ctype, size_t_ctype }},
783 { "__builtin_strndup", &string_ctype, 0, { &const_string_ctype, size_t_ctype }},
784 { "__builtin_strnstr", &string_ctype, 0, { &const_string_ctype, &const_string_ctype, size_t_ctype }},
785 { "__builtin_strpbrk", &string_ctype, 0, { &const_string_ctype, &const_string_ctype }},
786 { "__builtin_strrchr", &string_ctype, 0, { &const_string_ctype, &int_ctype }},
787 { "__builtin_strspn", size_t_ctype, 0, { &const_string_ctype, &const_string_ctype }},
788 { "__builtin_strstr", &string_ctype, 0, { &const_string_ctype, &const_string_ctype }},
789 { "__builtin_sub_overflow", &bool_ctype, 1, .op = &overflow_op },
790 { "__builtin_sub_overflow_p", &bool_ctype, 1, .op = &overflow_p_op },
791 { "__builtin_trap", &void_ctype, 0 },
792 { "__builtin_uadd_overflow", &bool_ctype, 0, { &uint_ctype, &uint_ctype, &uint_ptr_ctype }},
793 { "__builtin_uaddl_overflow", &bool_ctype, 0, { &ulong_ctype, &ulong_ctype, &ulong_ptr_ctype }},
794 { "__builtin_uaddll_overflow", &bool_ctype, 0, { &ullong_ctype, &ullong_ctype, &ullong_ptr_ctype }},
795 { "__builtin_umul_overflow", &bool_ctype, 0, { &uint_ctype, &uint_ctype, &uint_ptr_ctype }},
796 { "__builtin_umull_overflow", &bool_ctype, 0, { &ulong_ctype, &ulong_ctype, &ulong_ptr_ctype }},
797 { "__builtin_umulll_overflow", &bool_ctype, 0, { &ullong_ctype, &ullong_ctype, &ullong_ptr_ctype }},
798 { "__builtin_unreachable", &void_ctype, 0 },
799 { "__builtin_usub_overflow", &bool_ctype, 0, { &uint_ctype, &uint_ctype, &uint_ptr_ctype }},
800 { "__builtin_usubl_overflow", &bool_ctype, 0, { &ulong_ctype, &ulong_ctype, &ulong_ptr_ctype }},
801 { "__builtin_usubll_overflow", &bool_ctype, 0, { &ullong_ctype, &ullong_ctype, &ullong_ptr_ctype }},
802 { "__builtin_va_arg_pack_len", size_t_ctype, 0 },
803 { "__builtin_vprintf", &int_ctype, 0, { &const_string_ctype, va_list_ctype }},
804 { "__builtin_vsnprintf", &int_ctype, 0, { &string_ctype, size_t_ctype, &const_string_ctype, va_list_ctype }},
805 { "__builtin_vsprintf", &int_ctype, 0, { &string_ctype, &const_string_ctype, va_list_ctype }},
807 { "__builtin___memcpy_chk", &ptr_ctype, 0, { &ptr_ctype, &const_ptr_ctype, size_t_ctype, size_t_ctype }},
808 { "__builtin___memmove_chk", &ptr_ctype, 0, { &ptr_ctype, &const_ptr_ctype, size_t_ctype, size_t_ctype }},
809 { "__builtin___mempcpy_chk", &ptr_ctype, 0, { &ptr_ctype, &const_ptr_ctype, size_t_ctype, size_t_ctype }},
810 { "__builtin___memset_chk", &ptr_ctype, 0, { &ptr_ctype, &int_ctype, size_t_ctype, size_t_ctype }},
811 { "__builtin___snprintf_chk", &int_ctype, 1, { &string_ctype, size_t_ctype, &int_ctype , size_t_ctype, &const_string_ctype }},
812 { "__builtin___sprintf_chk", &int_ctype, 1, { &string_ctype, &int_ctype, size_t_ctype, &const_string_ctype }},
813 { "__builtin___stpcpy_chk", &string_ctype, 0, { &string_ctype, &const_string_ctype, size_t_ctype }},
814 { "__builtin___strcat_chk", &string_ctype, 0, { &string_ctype, &const_string_ctype, size_t_ctype }},
815 { "__builtin___strcpy_chk", &string_ctype, 0, { &string_ctype, &const_string_ctype, size_t_ctype }},
816 { "__builtin___strncat_chk", &string_ctype, 0, { &string_ctype, &const_string_ctype, size_t_ctype, size_t_ctype }},
817 { "__builtin___strncpy_chk", &string_ctype, 0, { &string_ctype, &const_string_ctype, size_t_ctype, size_t_ctype }},
818 { "__builtin___vsnprintf_chk", &int_ctype, 0, { &string_ctype, size_t_ctype, &int_ctype, size_t_ctype, &const_string_ctype, va_list_ctype }},
819 { "__builtin___vsprintf_chk", &int_ctype, 0, { &string_ctype, &int_ctype, size_t_ctype, &const_string_ctype, va_list_ctype }},
821 { "__sync_add_and_fetch", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
822 { "__sync_and_and_fetch", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
823 { "__sync_bool_compare_and_swap", &bool_ctype, 1, { vol_ptr, &dyntype, &dyntype }, .op = &atomic_op},
824 { "__sync_fetch_and_add", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
825 { "__sync_fetch_and_and", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
826 { "__sync_fetch_and_nand", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
827 { "__sync_fetch_and_or", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
828 { "__sync_fetch_and_sub", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
829 { "__sync_fetch_and_xor", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
830 { "__sync_lock_release", &void_ctype, 1, { vol_ptr }, .op = &atomic_op },
831 { "__sync_lock_test_and_set", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
832 { "__sync_nand_and_fetch", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
833 { "__sync_or_and_fetch", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
834 { "__sync_sub_and_fetch", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
835 { "__sync_synchronize", &void_ctype, 1 },
836 { "__sync_val_compare_and_swap", NULL, 1, { vol_ptr, &dyntype, &dyntype }, .op = &atomic_op },
837 { "__sync_xor_and_fetch", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
842 void init_builtins(int stream)
844 declare_builtins(stream, builtins_common);
845 declare_builtins(stream, arch_target->builtins);
846 init_linearized_builtins(stream);