• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * builtin evaluation & expansion.
3  *
4  * Copyright (C) 2003 Transmeta Corp.
5  *               2003-2004 Linus Torvalds
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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.
24  */
25 
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>
33 
34 #define dyntype incomplete_ctype
is_dynamic_type(struct symbol * t)35 static bool is_dynamic_type(struct symbol *t)
36 {
37 	if (t->type == SYM_NODE)
38 		t = t->ctype.base_type;
39 	return t == &dyntype;
40 }
41 
evaluate_to_int_const_expr(struct expression * expr)42 static int evaluate_to_int_const_expr(struct expression *expr)
43 {
44 	expr->ctype = &int_ctype;
45 	expr->flags |= CEF_SET_ICE;
46 	return 1;
47 }
48 
evaluate_pure_unop(struct expression * expr)49 static int evaluate_pure_unop(struct expression *expr)
50 {
51 	struct expression *arg = first_expression(expr->args);
52 	int flags = arg->flags;
53 
54 	/*
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 ...:
58 	 */
59 	flags |= (flags & CEF_ICE) ? CEF_SET_INT : 0;
60 	expr->flags = flags;
61 	return 1;
62 }
63 
64 /*
65  * eval_args - check the number of arguments and evaluate them.
66  */
eval_args(struct expression * expr,int n)67 static int eval_args(struct expression *expr, int n)
68 {
69 	struct expression *arg;
70 	struct symbol *sym;
71 	const char *msg;
72 	int rc = 1;
73 
74 	FOR_EACH_PTR(expr->args, arg) {
75 		if (n-- == 0) {
76 			msg = "too many arguments";
77 			goto error;
78 		}
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;
85 	}
86 	return rc;
87 
88 error:
89 	sym = expr->fn->ctype;
90 	expression_error(expr, "%s for %s", msg, show_ident(sym->ident));
91 	return 0;
92 }
93 
args_prototype(struct expression * expr)94 static int args_prototype(struct expression *expr)
95 {
96 	struct symbol *fntype = expr->fn->ctype->ctype.base_type;
97 	int n = symbol_list_size(fntype->arguments);
98 	return eval_args(expr, n);
99 }
100 
args_triadic(struct expression * expr)101 static int args_triadic(struct expression *expr)
102 {
103 	return eval_args(expr, 3);
104 }
105 
evaluate_choose(struct expression * expr)106 static int evaluate_choose(struct expression *expr)
107 {
108 	struct expression_list *list = expr->args;
109 	struct expression *arg, *args[3];
110 	int n = 0;
111 
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);
116 
117 	*expr = get_expression_value(args[0]) ? *args[1] : *args[2];
118 
119 	return 1;
120 }
121 
expand_expect(struct expression * expr,int cost)122 static int expand_expect(struct expression *expr, int cost)
123 {
124 	struct expression *arg = first_ptr_list((struct ptr_list *) expr->args);
125 
126 	if (arg)
127 		*expr = *arg;
128 	return 0;
129 }
130 
131 /*
132  * __builtin_warning() has type "int" and always returns 1,
133  * so that you can use it in conditionals or whatever
134  */
expand_warning(struct expression * expr,int cost)135 static int expand_warning(struct expression *expr, int cost)
136 {
137 	struct expression *arg;
138 	struct expression_list *arglist = expr->args;
139 
140 	FOR_EACH_PTR (arglist, arg) {
141 		/*
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.
146 		 *
147 		 * Just for the heck of it, allow any constant string
148 		 * symbol.
149 		 */
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);
155 			}
156 			continue;
157 		}
158 
159 		/*
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.
163 		 */
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;
174 }
175 
176 /* The arguments are constant if the cost of all of them is zero */
expand_constant_p(struct expression * expr,int cost)177 static int expand_constant_p(struct expression *expr, int cost)
178 {
179 	expr->type = EXPR_VALUE;
180 	expr->value = !cost;
181 	expr->taint = 0;
182 	return 0;
183 }
184 
185 /* The arguments are safe, if their cost is less than SIDE_EFFECTS */
expand_safe_p(struct expression * expr,int cost)186 static int expand_safe_p(struct expression *expr, int cost)
187 {
188 	expr->type = EXPR_VALUE;
189 	expr->value = (cost < SIDE_EFFECTS);
190 	expr->taint = 0;
191 	return 0;
192 }
193 
194 static struct symbol_op constant_p_op = {
195 	.evaluate = evaluate_to_int_const_expr,
196 	.expand = expand_constant_p
197 };
198 
199 static struct symbol_op safe_p_op = {
200 	.evaluate = evaluate_to_int_const_expr,
201 	.expand = expand_safe_p
202 };
203 
204 static struct symbol_op warning_op = {
205 	.evaluate = evaluate_to_int_const_expr,
206 	.expand = expand_warning
207 };
208 
209 static struct symbol_op expect_op = {
210 	.expand = expand_expect
211 };
212 
213 static struct symbol_op choose_op = {
214 	.args = args_triadic,
215 	.evaluate = evaluate_choose,
216 };
217 
218 /* The argument is constant and valid if the cost is zero */
expand_bswap(struct expression * expr,int cost)219 static int expand_bswap(struct expression *expr, int cost)
220 {
221 	struct expression *arg;
222 	long long val;
223 
224 	if (cost)
225 		return cost;
226 
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;
236 	}
237 
238 	expr->type = EXPR_VALUE;
239 	expr->taint = 0;
240 	return 0;
241 }
242 
243 static struct symbol_op bswap_op = {
244 	.evaluate = evaluate_pure_unop,
245 	.expand = expand_bswap,
246 };
247 
248 
249 #define EXPAND_FINDBIT(name)					\
250 static int expand_##name(struct expression *expr, int cost)	\
251 {								\
252 	struct expression *arg;					\
253 	long long val;						\
254 								\
255 	if (cost)						\
256 		return cost;					\
257 								\
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;				\
267 	}							\
268 								\
269 	expr->value = val;					\
270 	expr->type = EXPR_VALUE;				\
271 	expr->taint = 0;					\
272 	return 0;						\
273 }								\
274 								\
275 static struct symbol_op name##_op = {				\
276 	.evaluate = evaluate_pure_unop,				\
277 	.expand = expand_##name,				\
278 }
279 
280 EXPAND_FINDBIT(clz);
281 EXPAND_FINDBIT(ctz);
282 EXPAND_FINDBIT(clrsb);
283 EXPAND_FINDBIT(ffs);
284 EXPAND_FINDBIT(parity);
285 EXPAND_FINDBIT(popcount);
286 
evaluate_fp_unop(struct expression * expr)287 static int evaluate_fp_unop(struct expression *expr)
288 {
289 	struct expression *arg;
290 
291 	if (!eval_args(expr, 1))
292 		return 0;
293 
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;
299 	}
300 	return 1;
301 }
302 
303 static struct symbol_op fp_unop_op = {
304 	.evaluate = evaluate_fp_unop,
305 };
306 
307 
expand_isdigit(struct expression * expr,int cost)308 static int expand_isdigit(struct expression *expr, int cost)
309 {
310 	struct expression *arg = first_expression(expr->args);
311 	long long val = get_expression_value_silent(arg);
312 
313 	if (cost)
314 		return cost;
315 
316 	expr->value = (val >= '0') && (val <= '9');
317 	expr->type = EXPR_VALUE;
318 	expr->taint = 0;
319 	return 0;
320 }
321 
322 static struct symbol_op isdigit_op = {
323 	.evaluate = evaluate_pure_unop,
324 	.expand = expand_isdigit,
325 };
326 
327 
evaluate_overflow_gen(struct expression * expr,int ptr)328 static int evaluate_overflow_gen(struct expression *expr, int ptr)
329 {
330 	struct expression *arg;
331 	int n = 0;
332 
333 	/* there will be exactly 3; we'd already verified that */
334 	FOR_EACH_PTR(expr->args, arg) {
335 		struct symbol *type;
336 
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;
352 		}
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);
360 
361 	// the builtin returns a bool
362 	expr->ctype = &bool_ctype;
363 	return 1;
364 
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;
370 }
371 
evaluate_overflow(struct expression * expr)372 static int evaluate_overflow(struct expression *expr)
373 {
374 	return evaluate_overflow_gen(expr, 1);
375 }
376 
377 static struct symbol_op overflow_op = {
378 	.args = args_triadic,
379 	.evaluate = evaluate_overflow,
380 };
381 
evaluate_overflow_p(struct expression * expr)382 static int evaluate_overflow_p(struct expression *expr)
383 {
384 	return evaluate_overflow_gen(expr, 0);
385 }
386 
387 static struct symbol_op overflow_p_op = {
388 	.args = args_triadic,
389 	.evaluate = evaluate_overflow_p,
390 };
391 
392 
393 ///
394 // Evaluate the arguments of 'generic' integer operators.
395 //
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);
evaluate_generic_int_op(struct expression * expr)404 static int evaluate_generic_int_op(struct expression *expr)
405 {
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;
412 
413 	PREPARE_PTR_LIST(fntype->arguments, t);
414 	FOR_EACH_PTR(expr->args, arg) {
415 		n++;
416 
417 		if (!is_dynamic_type(t)) {
418 			;
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;
430 
431 			// next 'dynamic' arguments will use this type
432 			ctype = t;
433 		} else {
434 			// use the previous 'dynamic' type
435 			t = ctype;
436 		}
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);
442 
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;
448 }
449 
450 struct symbol_op generic_int_op = {
451 	.args = args_prototype,
452 	.evaluate = evaluate_generic_int_op,
453 };
454 
455 
eval_atomic_common(struct expression * expr)456 static int eval_atomic_common(struct expression *expr)
457 {
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;
464 
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;
470 
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;
497 		}
498 		add_ptr_list(&types, t);
499 		NEXT_PTR_LIST(t);
500 	} END_FOR_EACH_PTR(arg);
501 	FINISH_PTR_LIST(t);
502 
503 	if (!expr->ctype)	// set the return type, if needed
504 		expr->ctype = ctype;
505 	return evaluate_arguments(types, expr->args);
506 
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;
512 }
513 
514 static struct symbol_op atomic_op = {
515 	.args = args_prototype,
516 	.evaluate = eval_atomic_common,
517 };
518 
519 
520 ///
521 // expand __builtin_object_size()
522 //
523 // :note: type 1 and type 3 are not supported because the
524 //	needed information isn't available after evaluation.
expand_object_size(struct expression * expr,int cost)525 static int expand_object_size(struct expression *expr, int cost)
526 {
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;
530 
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 				arg = arg->unop;
550 				off = 0;
551 				switch (arg->type) {
552 				case EXPR_SYMBOL:
553 					arg = arg->symbol->initializer;
554 					continue;
555 				default:
556 					break;
557 				}
558 			}
559 			break;
560 		case EXPR_SYMBOL:
561 			// the symbol we're looking after
562 			val = bits_to_bytes(arg->symbol->bit_size);
563 			break;
564 		case EXPR_CALL:
565 			// use alloc_size() attribute but only after linearization.
566 			return UNSAFE;
567 		default:
568 			break;
569 		}
570 		break;
571 	}
572 
573 	if (val == -1)
574 		val = (type & 2) ? 0 : val;
575 	else if (type & 1)
576 		return UNSAFE;
577 	else
578 		val -= off;
579 
580 	expr->flags |= CEF_SET_ICE;
581 	expr->type = EXPR_VALUE;
582 	expr->value = val;
583 	expr->taint = 0;
584 	return 0;
585 }
586 
587 static struct symbol_op object_size_op = {
588 	.expand = expand_object_size,
589 };
590 
591 /*
592  * Builtin functions
593  */
594 static struct symbol size_t_alias;
595 
get_ctype(struct symbol * sym)596 static struct symbol *get_ctype(struct symbol *sym)
597 {
598 	if (sym == &size_t_alias)
599 		return size_t_ctype;
600 	return sym;
601 }
602 
declare_builtin(int stream,const struct builtin_fn * entry)603 static void declare_builtin(int stream, const struct builtin_fn *entry)
604 {
605 	struct symbol *sym = create_symbol(stream, entry->name, SYM_NODE, NS_SYMBOL);
606 	struct symbol *fun = alloc_symbol(sym->pos, SYM_FN);
607 	struct symbol *arg;
608 	int i;
609 
610 	sym->ctype.base_type = fun;
611 	sym->ctype.modifiers = MOD_TOPLEVEL;
612 	sym->builtin = 1;
613 	sym->op = entry->op;
614 
615 	fun->ctype.base_type = get_ctype(entry->ret_type);
616 	fun->variadic = entry->variadic;
617 
618 	for (i = 0; (arg = entry->args[i]); i++) {
619 		struct symbol *anode = alloc_symbol(sym->pos, SYM_NODE);
620 		anode->ctype.base_type = get_ctype(arg);
621 		add_symbol(&fun->arguments, anode);
622 	}
623 }
624 
declare_builtins(int stream,const struct builtin_fn tbl[])625 void declare_builtins(int stream, const struct builtin_fn tbl[])
626 {
627 	if (!tbl)
628 		return;
629 
630 	while (tbl->name)
631 		declare_builtin(stream, tbl++);
632 }
633 
634 static const struct builtin_fn builtins_common[] = {
635 #define size_t_ctype	&size_t_alias
636 #define va_list_ctype	&ptr_ctype
637 #define vol_ptr		&volatile_ptr_ctype
638 	{ "__atomic_add_fetch", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
639 	{ "__atomic_always_lock_free", &bool_ctype, 0, { size_t_ctype, vol_ptr }},
640 	{ "__atomic_and_fetch", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
641 	{ "__atomic_clear", &void_ctype, 0, { &volatile_bool_ptr_ctype, &int_ctype }},
642 	{ "__atomic_compare_exchange", &bool_ctype, 0, { vol_ptr, &ptr_ctype, &ptr_ctype, &bool_ctype, &int_ctype, &int_ctype }, .op = &atomic_op },
643 	{ "__atomic_compare_exchange_n", &bool_ctype, 0, { vol_ptr, &ptr_ctype, &dyntype, &bool_ctype, &int_ctype, &int_ctype }, .op = &atomic_op },
644 	{ "__atomic_exchange", &void_ctype, 0, { vol_ptr, &ptr_ctype, &ptr_ctype, &int_ctype }, .op = &atomic_op },
645 	{ "__atomic_exchange_n", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
646 	{ "__atomic_fetch_add", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
647 	{ "__atomic_fetch_and", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
648 	{ "__atomic_fetch_nand",NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
649 	{ "__atomic_fetch_or",  NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
650 	{ "__atomic_fetch_sub", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
651 	{ "__atomic_fetch_xor", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
652 	{ "__atomic_is_lock_free", &bool_ctype, 0, { size_t_ctype, vol_ptr }},
653 	{ "__atomic_load", &void_ctype, 0, { vol_ptr, &ptr_ctype, &int_ctype }, .op = &atomic_op },
654 	{ "__atomic_load_n", NULL, 0, { vol_ptr, &int_ctype }, .op = &atomic_op },
655 	{ "__atomic_nand_fetch",NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
656 	{ "__atomic_or_fetch",  NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
657 	{ "__atomic_signal_fence", &void_ctype, 0, { &int_ctype }},
658 	{ "__atomic_store", &void_ctype, 0, { vol_ptr, &ptr_ctype, &int_ctype }, .op = &atomic_op },
659 	{ "__atomic_store_n", &void_ctype, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
660 	{ "__atomic_sub_fetch", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
661 	{ "__atomic_test_and_set", &bool_ctype, 0, { vol_ptr, &int_ctype }},
662 	{ "__atomic_thread_fence", &void_ctype, 0, { &int_ctype }},
663 	{ "__atomic_xor_fetch", NULL, 0, { vol_ptr, &dyntype, &int_ctype }, .op = &atomic_op },
664 	{ "__builtin_choose_expr", NULL, 1, .op = &choose_op },
665 	{ "__builtin_constant_p", NULL, 1, .op = &constant_p_op },
666 	{ "__builtin_expect", &long_ctype, 0, { &long_ctype ,&long_ctype }, .op = &expect_op },
667 	{ "__builtin_safe_p", NULL, 1, .op = &safe_p_op },
668 	{ "__builtin_warning", NULL, 1, .op = &warning_op },
669 
670 	{ "__builtin_abort", &void_ctype, 0 },
671 	{ "__builtin_abs", &int_ctype , 0, { &int_ctype }},
672 	{ "__builtin_add_overflow", &bool_ctype, 1, .op = &overflow_op },
673 	{ "__builtin_add_overflow_p", &bool_ctype, 1, .op = &overflow_p_op },
674 	{ "__builtin_alloca", &ptr_ctype, 0, { size_t_ctype }},
675 	{ "__builtin_bcmp", &int_ctype , 0, { &const_ptr_ctype, &const_ptr_ctype, size_t_ctype }},
676 	{ "__builtin_bcopy", &void_ctype, 0, { &const_ptr_ctype, &ptr_ctype, size_t_ctype }},
677 	{ "__builtin_bswap16", &ushort_ctype, 0, { &ushort_ctype }, .op = &bswap_op },
678 	{ "__builtin_bswap32", &uint_ctype, 0, { &uint_ctype }, .op = &bswap_op },
679 	{ "__builtin_bswap64", &ullong_ctype, 0, { &ullong_ctype }, .op = &bswap_op },
680 	{ "__builtin_bzero", &void_ctype, 0, { &ptr_ctype, size_t_ctype }},
681 	{ "__builtin_calloc", &ptr_ctype, 0, { size_t_ctype, size_t_ctype }},
682 	{ "__builtin_clrsb", &int_ctype, 0, { &int_ctype }, .op = &clrsb_op },
683 	{ "__builtin_clrsbl", &int_ctype, 0, { &long_ctype }, .op = &clrsb_op },
684 	{ "__builtin_clrsbll", &int_ctype, 0, { &llong_ctype }, .op = &clrsb_op },
685 	{ "__builtin_clz", &int_ctype, 0, { &int_ctype }, .op = &clz_op },
686 	{ "__builtin_clzl", &int_ctype, 0, { &long_ctype }, .op = &clz_op },
687 	{ "__builtin_clzll", &int_ctype, 0, { &llong_ctype }, .op = &clz_op },
688 	{ "__builtin_ctz", &int_ctype, 0, { &int_ctype }, .op = &ctz_op },
689 	{ "__builtin_ctzl", &int_ctype, 0, { &long_ctype }, .op = &ctz_op },
690 	{ "__builtin_ctzll", &int_ctype, 0, { &llong_ctype }, .op = &ctz_op },
691 	{ "__builtin_exit", &void_ctype, 0, { &int_ctype }},
692 	{ "__builtin_extract_return_addr", &ptr_ctype, 0, { &ptr_ctype }},
693 	{ "__builtin_fabs", &double_ctype, 0, { &double_ctype }},
694 	{ "__builtin_ffs", &int_ctype, 0, { &int_ctype }, .op = &ffs_op },
695 	{ "__builtin_ffsl", &int_ctype, 0, { &long_ctype }, .op = &ffs_op },
696 	{ "__builtin_ffsll", &int_ctype, 0, { &llong_ctype }, .op = &ffs_op },
697 	{ "__builtin_fma", &double_ctype, 0, { &double_ctype, &double_ctype, &double_ctype }},
698 	{ "__builtin_fmaf", &float_ctype, 0, { &float_ctype, &float_ctype, &float_ctype }},
699 	{ "__builtin_fmal", &ldouble_ctype, 0, { &ldouble_ctype, &ldouble_ctype, &ldouble_ctype }},
700 	{ "__builtin_frame_address", &ptr_ctype, 0, { &uint_ctype }},
701 	{ "__builtin_free", &void_ctype, 0, { &ptr_ctype }},
702 	{ "__builtin_huge_val", &double_ctype, 0 },
703 	{ "__builtin_huge_valf", &float_ctype, 0 },
704 	{ "__builtin_huge_vall", &ldouble_ctype, 0 },
705 	{ "__builtin_index", &string_ctype, 0, { &const_string_ctype, &int_ctype }},
706 	{ "__builtin_inf", &double_ctype, 0 },
707 	{ "__builtin_inff", &float_ctype, 0 },
708 	{ "__builtin_infl", &ldouble_ctype, 0 },
709 	{ "__builtin_isdigit", &int_ctype, 0, { &int_ctype }, .op = &isdigit_op },
710 	{ "__builtin_isfinite", &int_ctype, 1, .op = &fp_unop_op },
711 	{ "__builtin_isgreater", &int_ctype, 0, { &float_ctype, &float_ctype }},
712 	{ "__builtin_isgreaterequal", &int_ctype, 0, { &float_ctype, &float_ctype }},
713 	{ "__builtin_isinf", &int_ctype, 1, .op = &fp_unop_op },
714 	{ "__builtin_isinf_sign", &int_ctype, 1, .op = &fp_unop_op },
715 	{ "__builtin_isless", &int_ctype, 0, { &float_ctype, &float_ctype }},
716 	{ "__builtin_islessequal", &int_ctype, 0, { &float_ctype, &float_ctype }},
717 	{ "__builtin_islessgreater", &int_ctype, 0, { &float_ctype, &float_ctype }},
718 	{ "__builtin_isnan", &int_ctype, 1, .op = &fp_unop_op },
719 	{ "__builtin_isnormal", &int_ctype, 1, .op = &fp_unop_op },
720 	{ "__builtin_isunordered", &int_ctype, 0, { &float_ctype, &float_ctype }},
721 	{ "__builtin_labs", &long_ctype, 0, { &long_ctype }},
722 	{ "__builtin_llabs", &llong_ctype, 0, { &llong_ctype }},
723 	{ "__builtin_malloc", &ptr_ctype, 0, { size_t_ctype }},
724 	{ "__builtin_memchr", &ptr_ctype, 0, { &const_ptr_ctype, &int_ctype, size_t_ctype }},
725 	{ "__builtin_memcmp", &int_ctype, 0, { &const_ptr_ctype, &const_ptr_ctype, size_t_ctype }},
726 	{ "__builtin_memcpy", &ptr_ctype, 0, { &ptr_ctype, &const_ptr_ctype, size_t_ctype }},
727 	{ "__builtin_memmove", &ptr_ctype, 0, { &ptr_ctype, &const_ptr_ctype, size_t_ctype }},
728 	{ "__builtin_mempcpy", &ptr_ctype, 0, { &ptr_ctype, &const_ptr_ctype, size_t_ctype }},
729 	{ "__builtin_memset", &ptr_ctype, 0, { &ptr_ctype, &int_ctype, size_t_ctype }},
730 	{ "__builtin_mul_overflow", &bool_ctype, 1, .op = &overflow_op },
731 	{ "__builtin_mul_overflow_p", &bool_ctype, 1, .op = &overflow_p_op },
732 	{ "__builtin_nan", &double_ctype, 0, { &const_string_ctype }},
733 	{ "__builtin_nanf", &float_ctype, 0, { &const_string_ctype }},
734 	{ "__builtin_nanl", &ldouble_ctype, 0, { &const_string_ctype }},
735 	{ "__builtin_object_size", size_t_ctype, 0, { &const_ptr_ctype, &int_ctype }, .op = &object_size_op},
736 	{ "__builtin_parity", &int_ctype, 0, { &uint_ctype }, .op = &parity_op },
737 	{ "__builtin_parityl", &int_ctype, 0, { &ulong_ctype }, .op = &parity_op },
738 	{ "__builtin_parityll", &int_ctype, 0, { &ullong_ctype }, .op = &parity_op },
739 	{ "__builtin_popcount", &int_ctype, 0, { &uint_ctype }, .op = &popcount_op },
740 	{ "__builtin_popcountl", &int_ctype, 0, { &ulong_ctype }, .op = &popcount_op },
741 	{ "__builtin_popcountll", &int_ctype, 0, { &ullong_ctype }, .op = &popcount_op },
742 	{ "__builtin_prefetch", &void_ctype, 1, { &const_ptr_ctype }},
743 	{ "__builtin_printf", &int_ctype, 1, { &const_string_ctype }},
744 	{ "__builtin_puts", &int_ctype, 0, { &const_string_ctype }},
745 	{ "__builtin_realloc", &ptr_ctype, 0, { &ptr_ctype, size_t_ctype }},
746 	{ "__builtin_return_address", &ptr_ctype, 0, { &uint_ctype }},
747 	{ "__builtin_rindex", &string_ctype, 0, { &const_string_ctype, &int_ctype }},
748 	{ "__builtin_sadd_overflow", &bool_ctype, 0, { &int_ctype, &int_ctype, &int_ptr_ctype }},
749 	{ "__builtin_saddl_overflow", &bool_ctype, 0, { &long_ctype, &long_ctype, &long_ptr_ctype }},
750 	{ "__builtin_saddll_overflow", &bool_ctype, 0, { &llong_ctype, &llong_ctype, &llong_ptr_ctype }},
751 	{ "__builtin_signbit", &int_ctype, 1 , .op = &fp_unop_op },
752 	{ "__builtin_smul_overflow", &bool_ctype, 0, { &int_ctype, &int_ctype, &int_ptr_ctype }},
753 	{ "__builtin_smull_overflow", &bool_ctype, 0, { &long_ctype, &long_ctype, &long_ptr_ctype }},
754 	{ "__builtin_smulll_overflow", &bool_ctype, 0, { &llong_ctype, &llong_ctype, &llong_ptr_ctype }},
755 	{ "__builtin_snprintf", &int_ctype, 1, { &string_ctype, size_t_ctype, &const_string_ctype }},
756 	{ "__builtin_sprintf", &int_ctype, 1, { &string_ctype, &const_string_ctype }},
757 	{ "__builtin_ssub_overflow", &bool_ctype, 0, { &int_ctype, &int_ctype, &int_ptr_ctype }},
758 	{ "__builtin_ssubl_overflow", &bool_ctype, 0, { &long_ctype, &long_ctype, &long_ptr_ctype }},
759 	{ "__builtin_ssubll_overflow", &bool_ctype, 0, { &llong_ctype, &llong_ctype, &llong_ptr_ctype }},
760 	{ "__builtin_stpcpy", &string_ctype, 0, { &const_string_ctype, &const_string_ctype }},
761 	{ "__builtin_stpncpy", &string_ctype, 0, { &const_string_ctype, &const_string_ctype, size_t_ctype }},
762 	{ "__builtin_strcasecmp", &int_ctype, 0, { &const_string_ctype, &const_string_ctype }},
763 	{ "__builtin_strcasestr", &string_ctype, 0, { &const_string_ctype, &const_string_ctype }},
764 	{ "__builtin_strcat", &string_ctype, 0, { &string_ctype, &const_string_ctype }},
765 	{ "__builtin_strchr", &string_ctype, 0, { &const_string_ctype, &int_ctype }},
766 	{ "__builtin_strcmp", &int_ctype, 0, { &const_string_ctype, &const_string_ctype }},
767 	{ "__builtin_strcpy", &string_ctype, 0, { &string_ctype, &const_string_ctype }},
768 	{ "__builtin_strcspn", size_t_ctype, 0, { &const_string_ctype, &const_string_ctype }},
769 	{ "__builtin_strdup", &string_ctype, 0, { &const_string_ctype }},
770 	{ "__builtin_strlen", size_t_ctype, 0, { &const_string_ctype }},
771 	{ "__builtin_strncasecmp", &int_ctype, 0, { &const_string_ctype, &const_string_ctype, size_t_ctype }},
772 	{ "__builtin_strncat", &string_ctype, 0, { &string_ctype, &const_string_ctype, size_t_ctype }},
773 	{ "__builtin_strncmp", &int_ctype, 0, { &const_string_ctype, &const_string_ctype, size_t_ctype }},
774 	{ "__builtin_strncpy", &string_ctype, 0, { &string_ctype, &const_string_ctype, size_t_ctype }},
775 	{ "__builtin_strndup", &string_ctype, 0, { &const_string_ctype, size_t_ctype }},
776 	{ "__builtin_strnstr", &string_ctype, 0, { &const_string_ctype, &const_string_ctype, size_t_ctype }},
777 	{ "__builtin_strpbrk", &string_ctype, 0, { &const_string_ctype, &const_string_ctype }},
778 	{ "__builtin_strrchr", &string_ctype, 0, { &const_string_ctype, &int_ctype }},
779 	{ "__builtin_strspn", size_t_ctype, 0, { &const_string_ctype, &const_string_ctype }},
780 	{ "__builtin_strstr", &string_ctype, 0, { &const_string_ctype, &const_string_ctype }},
781 	{ "__builtin_sub_overflow", &bool_ctype, 1, .op = &overflow_op },
782 	{ "__builtin_sub_overflow_p", &bool_ctype, 1, .op = &overflow_p_op },
783 	{ "__builtin_trap", &void_ctype, 0 },
784 	{ "__builtin_uadd_overflow", &bool_ctype, 0, { &uint_ctype, &uint_ctype, &uint_ptr_ctype }},
785 	{ "__builtin_uaddl_overflow", &bool_ctype, 0, { &ulong_ctype, &ulong_ctype, &ulong_ptr_ctype }},
786 	{ "__builtin_uaddll_overflow", &bool_ctype, 0, { &ullong_ctype, &ullong_ctype, &ullong_ptr_ctype }},
787 	{ "__builtin_umul_overflow", &bool_ctype, 0, { &uint_ctype, &uint_ctype, &uint_ptr_ctype }},
788 	{ "__builtin_umull_overflow", &bool_ctype, 0, { &ulong_ctype, &ulong_ctype, &ulong_ptr_ctype }},
789 	{ "__builtin_umulll_overflow", &bool_ctype, 0, { &ullong_ctype, &ullong_ctype, &ullong_ptr_ctype }},
790 	{ "__builtin_unreachable", &void_ctype, 0 },
791 	{ "__builtin_usub_overflow", &bool_ctype, 0, { &uint_ctype, &uint_ctype, &uint_ptr_ctype }},
792 	{ "__builtin_usubl_overflow", &bool_ctype, 0, { &ulong_ctype, &ulong_ctype, &ulong_ptr_ctype }},
793 	{ "__builtin_usubll_overflow", &bool_ctype, 0, { &ullong_ctype, &ullong_ctype, &ullong_ptr_ctype }},
794 	{ "__builtin_va_arg_pack_len", size_t_ctype, 0 },
795 	{ "__builtin_vprintf", &int_ctype, 0, { &const_string_ctype, va_list_ctype }},
796 	{ "__builtin_vsnprintf", &int_ctype, 0, { &string_ctype, size_t_ctype, &const_string_ctype, va_list_ctype }},
797 	{ "__builtin_vsprintf", &int_ctype, 0, { &string_ctype, &const_string_ctype, va_list_ctype }},
798 
799 	{ "__builtin___memcpy_chk", &ptr_ctype, 0, { &ptr_ctype, &const_ptr_ctype, size_t_ctype, size_t_ctype }},
800 	{ "__builtin___memmove_chk", &ptr_ctype, 0, { &ptr_ctype, &const_ptr_ctype, size_t_ctype, size_t_ctype }},
801 	{ "__builtin___mempcpy_chk", &ptr_ctype, 0, { &ptr_ctype, &const_ptr_ctype, size_t_ctype, size_t_ctype }},
802 	{ "__builtin___memset_chk", &ptr_ctype, 0, { &ptr_ctype, &int_ctype, size_t_ctype, size_t_ctype }},
803 	{ "__builtin___snprintf_chk", &int_ctype, 1, { &string_ctype, size_t_ctype, &int_ctype , size_t_ctype, &const_string_ctype }},
804 	{ "__builtin___sprintf_chk", &int_ctype, 1, { &string_ctype, &int_ctype, size_t_ctype, &const_string_ctype }},
805 	{ "__builtin___stpcpy_chk", &string_ctype, 0, { &string_ctype, &const_string_ctype, size_t_ctype }},
806 	{ "__builtin___strcat_chk", &string_ctype, 0, { &string_ctype, &const_string_ctype, size_t_ctype }},
807 	{ "__builtin___strcpy_chk", &string_ctype, 0, { &string_ctype, &const_string_ctype, size_t_ctype }},
808 	{ "__builtin___strncat_chk", &string_ctype, 0, { &string_ctype, &const_string_ctype, size_t_ctype, size_t_ctype }},
809 	{ "__builtin___strncpy_chk", &string_ctype, 0, { &string_ctype, &const_string_ctype, size_t_ctype, size_t_ctype }},
810 	{ "__builtin___vsnprintf_chk", &int_ctype, 0, { &string_ctype, size_t_ctype, &int_ctype, size_t_ctype, &const_string_ctype, va_list_ctype }},
811 	{ "__builtin___vsprintf_chk", &int_ctype, 0, { &string_ctype, &int_ctype, size_t_ctype, &const_string_ctype, va_list_ctype }},
812 
813 	{ "__sync_add_and_fetch", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
814 	{ "__sync_and_and_fetch", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
815 	{ "__sync_bool_compare_and_swap", &bool_ctype, 1, { vol_ptr, &dyntype, &dyntype }, .op = &atomic_op},
816 	{ "__sync_fetch_and_add", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
817 	{ "__sync_fetch_and_and", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
818 	{ "__sync_fetch_and_nand", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
819 	{ "__sync_fetch_and_or", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
820 	{ "__sync_fetch_and_sub", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
821 	{ "__sync_fetch_and_xor", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
822 	{ "__sync_lock_release", &void_ctype, 1, { vol_ptr }, .op = &atomic_op },
823 	{ "__sync_lock_test_and_set", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
824 	{ "__sync_nand_and_fetch", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
825 	{ "__sync_or_and_fetch", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
826 	{ "__sync_sub_and_fetch", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
827 	{ "__sync_synchronize", &void_ctype, 1 },
828 	{ "__sync_val_compare_and_swap", NULL, 1, { vol_ptr, &dyntype, &dyntype }, .op = &atomic_op },
829 	{ "__sync_xor_and_fetch", NULL, 1, { vol_ptr, &dyntype }, .op = &atomic_op },
830 
831 	{ }
832 };
833 
init_builtins(int stream)834 void init_builtins(int stream)
835 {
836 	declare_builtins(stream, builtins_common);
837 	declare_builtins(stream, arch_target->builtins);
838 	init_linearized_builtins(stream);
839 }
840