xref: /illumos-gate/usr/src/tools/smatch/src/builtin.c (revision 63f91fbc3c024870d86dc3332a4a0080fb29bc40)
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 "expression.h"
27 #include "evaluate.h"
28 #include "expand.h"
29 #include "symbol.h"
30 #include "compat/bswap.h"
31 #include <stdarg.h>
32 
33 static int evaluate_to_int_const_expr(struct expression *expr)
34 {
35 	expr->ctype = &int_ctype;
36 	expr->flags |= CEF_SET_ICE;
37 	return 1;
38 }
39 
40 static int evaluate_pure_unop(struct expression *expr)
41 {
42 	struct expression *arg = first_expression(expr->args);
43 	int flags = arg->flags;
44 
45 	/*
46 	 * Allow such functions with a constant integer expression
47 	 * argument to be treated as a *constant* integer.
48 	 * This allow us to use them in switch() { case ...:
49 	 */
50 	flags |= (flags & CEF_ICE) ? CEF_SET_INT : 0;
51 	expr->flags = flags;
52 	return 1;
53 }
54 
55 /*
56  * eval_args - check the number of arguments and evaluate them.
57  */
58 static int eval_args(struct expression *expr, int n)
59 {
60 	struct expression *arg;
61 	struct symbol *sym;
62 	const char *msg;
63 	int rc = 1;
64 
65 	FOR_EACH_PTR(expr->args, arg) {
66 		if (n-- == 0) {
67 			msg = "too many arguments";
68 			goto error;
69 		}
70 		if (!evaluate_expression(arg))
71 			rc = 0;
72 	} END_FOR_EACH_PTR(arg);
73 	if (n > 0) {
74 		msg = "not enough arguments";
75 		goto error;
76 	}
77 	return rc;
78 
79 error:
80 	sym = expr->fn->ctype;
81 	expression_error(expr, "%s for %s", msg, show_ident(sym->ident));
82 	return 0;
83 }
84 
85 static int args_triadic(struct expression *expr)
86 {
87 	return eval_args(expr, 3);
88 }
89 
90 static int evaluate_choose(struct expression *expr)
91 {
92 	struct expression_list *list = expr->args;
93 	struct expression *arg, *args[3];
94 	int n = 0;
95 
96 	/* there will be exactly 3; we'd already verified that */
97 	FOR_EACH_PTR(list, arg) {
98 		args[n++] = arg;
99 	} END_FOR_EACH_PTR(arg);
100 
101 	*expr = get_expression_value(args[0]) ? *args[1] : *args[2];
102 
103 	return 1;
104 }
105 
106 static int expand_expect(struct expression *expr, int cost)
107 {
108 	struct expression *arg = first_ptr_list((struct ptr_list *) expr->args);
109 
110 	if (arg)
111 		*expr = *arg;
112 	return 0;
113 }
114 
115 /*
116  * __builtin_warning() has type "int" and always returns 1,
117  * so that you can use it in conditionals or whatever
118  */
119 static int expand_warning(struct expression *expr, int cost)
120 {
121 	struct expression *arg;
122 	struct expression_list *arglist = expr->args;
123 
124 	FOR_EACH_PTR (arglist, arg) {
125 		/*
126 		 * Constant strings get printed out as a warning. By the
127 		 * time we get here, the EXPR_STRING has been fully
128 		 * evaluated, so by now it's an anonymous symbol with a
129 		 * string initializer.
130 		 *
131 		 * Just for the heck of it, allow any constant string
132 		 * symbol.
133 		 */
134 		if (arg->type == EXPR_SYMBOL) {
135 			struct symbol *sym = arg->symbol;
136 			if (sym->initializer && sym->initializer->type == EXPR_STRING) {
137 				struct string *string = sym->initializer->string;
138 				warning(expr->pos, "%*s", string->length-1, string->data);
139 			}
140 			continue;
141 		}
142 
143 		/*
144 		 * Any other argument is a conditional. If it's
145 		 * non-constant, or it is false, we exit and do
146 		 * not print any warning.
147 		 */
148 		if (arg->type != EXPR_VALUE)
149 			goto out;
150 		if (!arg->value)
151 			goto out;
152 	} END_FOR_EACH_PTR(arg);
153 out:
154 	expr->type = EXPR_VALUE;
155 	expr->value = 1;
156 	expr->taint = 0;
157 	return 0;
158 }
159 
160 /* The arguments are constant if the cost of all of them is zero */
161 static int expand_constant_p(struct expression *expr, int cost)
162 {
163 	expr->type = EXPR_VALUE;
164 	expr->value = !cost;
165 	expr->taint = 0;
166 	return 0;
167 }
168 
169 /* The arguments are safe, if their cost is less than SIDE_EFFECTS */
170 static int expand_safe_p(struct expression *expr, int cost)
171 {
172 	expr->type = EXPR_VALUE;
173 	expr->value = (cost < SIDE_EFFECTS);
174 	expr->taint = 0;
175 	return 0;
176 }
177 
178 static struct symbol_op constant_p_op = {
179 	.evaluate = evaluate_to_int_const_expr,
180 	.expand = expand_constant_p
181 };
182 
183 static struct symbol_op safe_p_op = {
184 	.evaluate = evaluate_to_int_const_expr,
185 	.expand = expand_safe_p
186 };
187 
188 static struct symbol_op warning_op = {
189 	.evaluate = evaluate_to_int_const_expr,
190 	.expand = expand_warning
191 };
192 
193 static struct symbol_op expect_op = {
194 	.expand = expand_expect
195 };
196 
197 static struct symbol_op choose_op = {
198 	.args = args_triadic,
199 	.evaluate = evaluate_choose,
200 };
201 
202 /* The argument is constant and valid if the cost is zero */
203 static int expand_bswap(struct expression *expr, int cost)
204 {
205 	struct expression *arg;
206 	long long val;
207 
208 	if (cost)
209 		return cost;
210 
211 	/* the arguments number & type have already been checked */
212 	arg = first_expression(expr->args);
213 	val = get_expression_value_silent(arg);
214 	switch (expr->ctype->bit_size) {
215 	case 16: expr->value = bswap16(val); break;
216 	case 32: expr->value = bswap32(val); break;
217 	case 64: expr->value = bswap64(val); break;
218 	default: /* impossible error */
219 		return SIDE_EFFECTS;
220 	}
221 
222 	expr->type = EXPR_VALUE;
223 	expr->taint = 0;
224 	return 0;
225 }
226 
227 static struct symbol_op bswap_op = {
228 	.evaluate = evaluate_pure_unop,
229 	.expand = expand_bswap,
230 };
231 
232 
233 static int evaluate_fp_unop(struct expression *expr)
234 {
235 	struct expression *arg;
236 
237 	if (!eval_args(expr, 1))
238 		return 0;
239 
240 	arg = first_expression(expr->args);
241 	if (!is_float_type(arg->ctype)) {
242 		expression_error(expr, "non-floating-point argument in call to %s()",
243 			show_ident(expr->fn->ctype->ident));
244 		return 0;
245 	}
246 	return 1;
247 }
248 
249 static struct symbol_op fp_unop_op = {
250 	.evaluate = evaluate_fp_unop,
251 };
252 
253 
254 static int evaluate_overflow_gen(struct expression *expr, int ptr)
255 {
256 	struct expression *arg;
257 	int n = 0;
258 
259 	/* there will be exactly 3; we'd already verified that */
260 	FOR_EACH_PTR(expr->args, arg) {
261 		struct symbol *type;
262 
263 		n++;
264 		if (!arg || !(type = arg->ctype))
265 			return 0;
266 		// 1st & 2nd args must be a basic integer type
267 		// 3rd arg must be a pointer to such a type.
268 		if (n == 3 && ptr) {
269 			if (type->type == SYM_NODE)
270 				type = type->ctype.base_type;
271 			if (!type)
272 				return 0;
273 			if (type->type != SYM_PTR)
274 				goto err;
275 			type = type->ctype.base_type;
276 			if (!type)
277 				return 0;
278 		}
279 		if (type->type == SYM_NODE)
280 			type = type->ctype.base_type;
281 		if (!type)
282 			return 0;
283 		if (type->ctype.base_type != &int_type || type == &bool_ctype)
284 			goto err;
285 	} END_FOR_EACH_PTR(arg);
286 
287 	// the builtin returns a bool
288 	expr->ctype = &bool_ctype;
289 	return 1;
290 
291 err:
292 	sparse_error(arg->pos, "invalid type for argument %d:", n);
293 	info(arg->pos, "        %s", show_typename(arg->ctype));
294 	expr->ctype = &bad_ctype;
295 	return 0;
296 }
297 
298 static int evaluate_overflow(struct expression *expr)
299 {
300 	return evaluate_overflow_gen(expr, 1);
301 }
302 
303 static struct symbol_op overflow_op = {
304 	.args = args_triadic,
305 	.evaluate = evaluate_overflow,
306 };
307 
308 static int evaluate_overflow_p(struct expression *expr)
309 {
310 	return evaluate_overflow_gen(expr, 0);
311 }
312 
313 static struct symbol_op overflow_p_op = {
314 	.args = args_triadic,
315 	.evaluate = evaluate_overflow_p,
316 };
317 
318 
319 /*
320  * Builtin functions
321  */
322 static struct symbol builtin_fn_type = { .type = SYM_FN /* , .variadic =1 */ };
323 static struct sym_init {
324 	const char *name;
325 	struct symbol *base_type;
326 	unsigned int modifiers;
327 	struct symbol_op *op;
328 } builtins_table[] = {
329 	{ "__builtin_constant_p", &builtin_fn_type, MOD_TOPLEVEL, &constant_p_op },
330 	{ "__builtin_safe_p", &builtin_fn_type, MOD_TOPLEVEL, &safe_p_op },
331 	{ "__builtin_warning", &builtin_fn_type, MOD_TOPLEVEL, &warning_op },
332 	{ "__builtin_expect", &builtin_fn_type, MOD_TOPLEVEL, &expect_op },
333 	{ "__builtin_choose_expr", &builtin_fn_type, MOD_TOPLEVEL, &choose_op },
334 	{ "__builtin_bswap16", &builtin_fn_type, MOD_TOPLEVEL, &bswap_op },
335 	{ "__builtin_bswap32", &builtin_fn_type, MOD_TOPLEVEL, &bswap_op },
336 	{ "__builtin_bswap64", &builtin_fn_type, MOD_TOPLEVEL, &bswap_op },
337 	{ "__builtin_isfinite", &builtin_fn_type, MOD_TOPLEVEL, &fp_unop_op },
338 	{ "__builtin_isinf", &builtin_fn_type, MOD_TOPLEVEL, &fp_unop_op },
339 	{ "__builtin_isinf_sign", &builtin_fn_type, MOD_TOPLEVEL, &fp_unop_op },
340 	{ "__builtin_isnan", &builtin_fn_type, MOD_TOPLEVEL, &fp_unop_op },
341 	{ "__builtin_isnormal", &builtin_fn_type, MOD_TOPLEVEL, &fp_unop_op },
342 	{ "__builtin_signbit", &builtin_fn_type, MOD_TOPLEVEL, &fp_unop_op },
343 	{ "__builtin_add_overflow", &builtin_fn_type, MOD_TOPLEVEL, &overflow_op },
344 	{ "__builtin_sub_overflow", &builtin_fn_type, MOD_TOPLEVEL, &overflow_op },
345 	{ "__builtin_mul_overflow", &builtin_fn_type, MOD_TOPLEVEL, &overflow_op },
346 	{ "__builtin_add_overflow_p", &builtin_fn_type, MOD_TOPLEVEL, &overflow_p_op },
347 	{ "__builtin_sub_overflow_p", &builtin_fn_type, MOD_TOPLEVEL, &overflow_p_op },
348 	{ "__builtin_mul_overflow_p", &builtin_fn_type, MOD_TOPLEVEL, &overflow_p_op },
349 	{ NULL,		NULL,		0 }
350 };
351 
352 void init_builtins(int stream)
353 {
354 	struct sym_init *ptr;
355 
356 	builtin_fn_type.variadic = 1;
357 	for (ptr = builtins_table; ptr->name; ptr++) {
358 		struct symbol *sym;
359 		sym = create_symbol(stream, ptr->name, SYM_NODE, NS_SYMBOL);
360 		sym->ctype.base_type = ptr->base_type;
361 		sym->ctype.modifiers = ptr->modifiers;
362 		sym->op = ptr->op;
363 		sym->builtin = 1;
364 	}
365 }
366 
367 static void declare_builtin(const char *name, struct symbol *rtype, int variadic, ...)
368 {
369 	int stream = 0;			// FIXME
370 	struct symbol *sym = create_symbol(stream, name, SYM_NODE, NS_SYMBOL);
371 	struct symbol *fun = alloc_symbol(sym->pos, SYM_FN);
372 	struct symbol *arg;
373 	va_list args;
374 
375 	sym->ctype.base_type = fun;
376 	sym->ctype.modifiers = MOD_TOPLEVEL;
377 	sym->builtin = 1;
378 
379 	fun->ctype.base_type = rtype;
380 	fun->variadic = variadic;
381 
382 	va_start(args, variadic);
383 	while ((arg = va_arg(args, struct symbol *))) {
384 		struct symbol *anode = alloc_symbol(sym->pos, SYM_NODE);
385 		anode->ctype.base_type = arg;
386 		add_symbol(&fun->arguments, anode);
387 	}
388 	va_end(args);
389 }
390 
391 void declare_builtins(void)
392 {
393 	struct symbol *va_list_ctype = &ptr_ctype;
394 
395 	declare_builtin("__builtin_abort", &void_ctype, 0, NULL);
396 	declare_builtin("__builtin_abs", &int_ctype , 0, &int_ctype, NULL);
397 	declare_builtin("__builtin_alloca", &ptr_ctype, 0, size_t_ctype, NULL);
398 	declare_builtin("__builtin_alpha_cmpbge", &long_ctype, 0, &long_ctype, &long_ctype, NULL);
399 	declare_builtin("__builtin_alpha_extbl", &long_ctype, 0, &long_ctype, &long_ctype, NULL);
400 	declare_builtin("__builtin_alpha_extwl", &long_ctype, 0, &long_ctype, &long_ctype, NULL);
401 	declare_builtin("__builtin_alpha_insbl", &long_ctype, 0, &long_ctype, &long_ctype, NULL);
402 	declare_builtin("__builtin_alpha_inslh", &long_ctype, 0, &long_ctype, &long_ctype, NULL);
403 	declare_builtin("__builtin_alpha_insql", &long_ctype, 0, &long_ctype, &long_ctype, NULL);
404 	declare_builtin("__builtin_alpha_inswl", &long_ctype, 0, &long_ctype, &long_ctype, NULL);
405 	declare_builtin("__builtin_bcmp", &int_ctype , 0, &const_ptr_ctype, &const_ptr_ctype, size_t_ctype, NULL);
406 	declare_builtin("__builtin_bcopy", &void_ctype, 0, &const_ptr_ctype, &ptr_ctype, size_t_ctype, NULL);
407 	declare_builtin("__builtin_bswap16", &ushort_ctype, 0, &ushort_ctype, NULL);
408 	declare_builtin("__builtin_bswap32", &uint_ctype, 0, &uint_ctype, NULL);
409 	declare_builtin("__builtin_bswap64", &ullong_ctype, 0, &ullong_ctype, NULL);
410 	declare_builtin("__builtin_bzero", &void_ctype, 0, &ptr_ctype, size_t_ctype, NULL);
411 	declare_builtin("__builtin_calloc", &ptr_ctype, 0, size_t_ctype, size_t_ctype, NULL);
412 	declare_builtin("__builtin_clrsb", &int_ctype, 0, &int_ctype, NULL);
413 	declare_builtin("__builtin_clrsbl", &int_ctype, 0, &long_ctype, NULL);
414 	declare_builtin("__builtin_clrsbll", &int_ctype, 0, &llong_ctype, NULL);
415 	declare_builtin("__builtin_clz", &int_ctype, 0, &int_ctype, NULL);
416 	declare_builtin("__builtin_clzl", &int_ctype, 0, &long_ctype, NULL);
417 	declare_builtin("__builtin_clzll", &int_ctype, 0, &llong_ctype, NULL);
418 	declare_builtin("__builtin_ctz", &int_ctype, 0, &int_ctype, NULL);
419 	declare_builtin("__builtin_ctzl", &int_ctype, 0, &long_ctype, NULL);
420 	declare_builtin("__builtin_ctzll", &int_ctype, 0, &llong_ctype, NULL);
421 	declare_builtin("__builtin_exit", &void_ctype, 0, &int_ctype, NULL);
422 	declare_builtin("__builtin_expect", &long_ctype, 0, &long_ctype ,&long_ctype, NULL);
423 	declare_builtin("__builtin_extract_return_addr", &ptr_ctype, 0, &ptr_ctype, NULL);
424 	declare_builtin("__builtin_fabs", &double_ctype, 0, &double_ctype, NULL);
425 	declare_builtin("__builtin_ffs", &int_ctype, 0, &int_ctype, NULL);
426 	declare_builtin("__builtin_ffsl", &int_ctype, 0, &long_ctype, NULL);
427 	declare_builtin("__builtin_ffsll", &int_ctype, 0, &llong_ctype, NULL);
428 	declare_builtin("__builtin_frame_address", &ptr_ctype, 0, &uint_ctype, NULL);
429 	declare_builtin("__builtin_free", &void_ctype, 0, &ptr_ctype, NULL);
430 	declare_builtin("__builtin_huge_val", &double_ctype, 0, NULL);
431 	declare_builtin("__builtin_huge_valf", &float_ctype, 0, NULL);
432 	declare_builtin("__builtin_huge_vall", &ldouble_ctype, 0, NULL);
433 	declare_builtin("__builtin_index", &string_ctype, 0, &const_string_ctype, &int_ctype, NULL);
434 	declare_builtin("__builtin_inf", &double_ctype, 0, NULL);
435 	declare_builtin("__builtin_inff", &float_ctype, 0, NULL);
436 	declare_builtin("__builtin_infl", &ldouble_ctype, 0, NULL);
437 	declare_builtin("__builtin_isfinite", &int_ctype, 1, NULL);
438 	declare_builtin("__builtin_isgreater", &int_ctype, 0, &float_ctype, &float_ctype, NULL);
439 	declare_builtin("__builtin_isgreaterequal", &int_ctype, 0, &float_ctype, &float_ctype, NULL);
440 	declare_builtin("__builtin_isinf", &int_ctype, 1, NULL);
441 	declare_builtin("__builtin_isinf_sign", &int_ctype, 1, NULL);
442 	declare_builtin("__builtin_isless", &int_ctype, 0, &float_ctype, &float_ctype, NULL);
443 	declare_builtin("__builtin_islessequal", &int_ctype, 0, &float_ctype, &float_ctype, NULL);
444 	declare_builtin("__builtin_islessgreater", &int_ctype, 0, &float_ctype, &float_ctype, NULL);
445 	declare_builtin("__builtin_isnan", &int_ctype, 1, NULL);
446 	declare_builtin("__builtin_isnormal", &int_ctype, 1, NULL);
447 	declare_builtin("__builtin_isunordered", &int_ctype, 0, &float_ctype, &float_ctype, NULL);
448 	declare_builtin("__builtin_labs", &long_ctype, 0, &long_ctype, NULL);
449 	declare_builtin("__builtin_llabs", &llong_ctype, 0, &llong_ctype, NULL);
450 	declare_builtin("__builtin_malloc", &ptr_ctype, 0, size_t_ctype, NULL);
451 	declare_builtin("__builtin_memchr", &ptr_ctype, 0, &const_ptr_ctype, &int_ctype, size_t_ctype, NULL);
452 	declare_builtin("__builtin_memcmp", &int_ctype, 0, &const_ptr_ctype, &const_ptr_ctype, size_t_ctype, NULL);
453 	declare_builtin("__builtin_memcpy", &ptr_ctype, 0, &ptr_ctype, &const_ptr_ctype, size_t_ctype, NULL);
454 	declare_builtin("__builtin_memmove", &ptr_ctype, 0, &ptr_ctype, &const_ptr_ctype, size_t_ctype, NULL);
455 	declare_builtin("__builtin_mempcpy", &ptr_ctype, 0, &ptr_ctype, &const_ptr_ctype, size_t_ctype, NULL);
456 	declare_builtin("__builtin_memset", &ptr_ctype, 0, &ptr_ctype, &int_ctype, size_t_ctype, NULL);
457 	declare_builtin("__builtin_nan", &double_ctype, 0, &const_string_ctype, NULL);
458 	declare_builtin("__builtin_nanf", &float_ctype, 0, &const_string_ctype, NULL);
459 	declare_builtin("__builtin_nanl", &ldouble_ctype, 0, &const_string_ctype, NULL);
460 	declare_builtin("__builtin_object_size", size_t_ctype, 0, &const_ptr_ctype, &int_ctype, NULL);
461 	declare_builtin("__builtin_parity", &int_ctype, 0, &uint_ctype, NULL);
462 	declare_builtin("__builtin_parityl", &int_ctype, 0, &ulong_ctype, NULL);
463 	declare_builtin("__builtin_parityll", &int_ctype, 0, &ullong_ctype, NULL);
464 	declare_builtin("__builtin_popcount", &int_ctype, 0, &uint_ctype, NULL);
465 	declare_builtin("__builtin_popcountl", &int_ctype, 0, &ulong_ctype, NULL);
466 	declare_builtin("__builtin_popcountll", &int_ctype, 0, &ullong_ctype, NULL);
467 	declare_builtin("__builtin_prefetch", &void_ctype, 1, &const_ptr_ctype, NULL);
468 	declare_builtin("__builtin_printf", &int_ctype, 1, &const_string_ctype, NULL);
469 	declare_builtin("__builtin_puts", &int_ctype, 0, &const_string_ctype, NULL);
470 	declare_builtin("__builtin_realloc", &ptr_ctype, 0, &ptr_ctype, size_t_ctype, NULL);
471 	declare_builtin("__builtin_return_address", &ptr_ctype, 0, &uint_ctype, NULL);
472 	declare_builtin("__builtin_rindex", &string_ctype, 0, &const_string_ctype, &int_ctype, NULL);
473 	declare_builtin("__builtin_sadd_overflow", &bool_ctype, 0, &int_ctype, &int_ctype, &int_ptr_ctype, NULL);
474 	declare_builtin("__builtin_saddl_overflow", &bool_ctype, 0, &long_ctype, &long_ctype, &long_ptr_ctype, NULL);
475 	declare_builtin("__builtin_saddll_overflow", &bool_ctype, 0, &llong_ctype, &llong_ctype, &llong_ptr_ctype, NULL);
476 	declare_builtin("__builtin_signbit", &int_ctype, 1, NULL);
477 	declare_builtin("__builtin_smul_overflow", &bool_ctype, 0, &int_ctype, &int_ctype, &int_ptr_ctype, NULL);
478 	declare_builtin("__builtin_smull_overflow", &bool_ctype, 0, &long_ctype, &long_ctype, &long_ptr_ctype, NULL);
479 	declare_builtin("__builtin_smulll_overflow", &bool_ctype, 0, &llong_ctype, &llong_ctype, &llong_ptr_ctype, NULL);
480 	declare_builtin("__builtin_snprintf", &int_ctype, 1, &string_ctype, size_t_ctype, &const_string_ctype, NULL);
481 	declare_builtin("__builtin_sprintf", &int_ctype, 1, &string_ctype, &const_string_ctype, NULL);
482 	declare_builtin("__builtin_ssub_overflow", &bool_ctype, 0, &int_ctype, &int_ctype, &int_ptr_ctype, NULL);
483 	declare_builtin("__builtin_ssubl_overflow", &bool_ctype, 0, &long_ctype, &long_ctype, &long_ptr_ctype, NULL);
484 	declare_builtin("__builtin_ssubll_overflow", &bool_ctype, 0, &llong_ctype, &llong_ctype, &llong_ptr_ctype, NULL);
485 	declare_builtin("__builtin_stpcpy", &string_ctype, 0, &const_string_ctype, &const_string_ctype, NULL);
486 	declare_builtin("__builtin_stpncpy", &string_ctype, 0, &const_string_ctype, &const_string_ctype, size_t_ctype, NULL);
487 	declare_builtin("__builtin_strcasecmp", &int_ctype, 0, &const_string_ctype, &const_string_ctype, NULL);
488 	declare_builtin("__builtin_strcasestr", &string_ctype, 0, &const_string_ctype, &const_string_ctype, NULL);
489 	declare_builtin("__builtin_strcat", &string_ctype, 0, &string_ctype, &const_string_ctype, NULL);
490 	declare_builtin("__builtin_strchr", &string_ctype, 0, &const_string_ctype, &int_ctype, NULL);
491 	declare_builtin("__builtin_strcmp", &int_ctype, 0, &const_string_ctype, &const_string_ctype, NULL);
492 	declare_builtin("__builtin_strcpy", &string_ctype, 0, &string_ctype, &const_string_ctype, NULL);
493 	declare_builtin("__builtin_strcspn", size_t_ctype, 0, &const_string_ctype, &const_string_ctype, NULL);
494 	declare_builtin("__builtin_strdup", &string_ctype, 0, &const_string_ctype, NULL);
495 	declare_builtin("__builtin_strlen", size_t_ctype, 0, &const_string_ctype, NULL);
496 	declare_builtin("__builtin_strncasecmp", &int_ctype, 0, &const_string_ctype, &const_string_ctype, size_t_ctype, NULL);
497 	declare_builtin("__builtin_strncat", &string_ctype, 0, &string_ctype, &const_string_ctype, size_t_ctype, NULL);
498 	declare_builtin("__builtin_strncmp", &int_ctype, 0, &const_string_ctype, &const_string_ctype, size_t_ctype, NULL);
499 	declare_builtin("__builtin_strncpy", &string_ctype, 0, &string_ctype, &const_string_ctype, size_t_ctype, NULL);
500 	declare_builtin("__builtin_strndup", &string_ctype, 0, &const_string_ctype, size_t_ctype, NULL);
501 	declare_builtin("__builtin_strnstr", &string_ctype, 0, &const_string_ctype, &const_string_ctype, size_t_ctype, NULL);
502 	declare_builtin("__builtin_strpbrk", &string_ctype, 0, &const_string_ctype, &const_string_ctype, NULL);
503 	declare_builtin("__builtin_strrchr", &string_ctype, 0, &const_string_ctype, &int_ctype, NULL);
504 	declare_builtin("__builtin_strspn", size_t_ctype, 0, &const_string_ctype, &const_string_ctype, NULL);
505 	declare_builtin("__builtin_strstr", &string_ctype, 0, &const_string_ctype, &const_string_ctype, NULL);
506 	declare_builtin("__builtin_trap", &void_ctype, 0, NULL);
507 	declare_builtin("__builtin_uadd_overflow", &bool_ctype, 0, &uint_ctype, &uint_ctype, &uint_ptr_ctype, NULL);
508 	declare_builtin("__builtin_uaddl_overflow", &bool_ctype, 0, &ulong_ctype, &ulong_ctype, &ulong_ptr_ctype, NULL);
509 	declare_builtin("__builtin_uaddll_overflow", &bool_ctype, 0, &ullong_ctype, &ullong_ctype, &ullong_ptr_ctype, NULL);
510 	declare_builtin("__builtin_umul_overflow", &bool_ctype, 0, &uint_ctype, &uint_ctype, &uint_ptr_ctype, NULL);
511 	declare_builtin("__builtin_umull_overflow", &bool_ctype, 0, &ulong_ctype, &ulong_ctype, &ulong_ptr_ctype, NULL);
512 	declare_builtin("__builtin_umulll_overflow", &bool_ctype, 0, &ullong_ctype, &ullong_ctype, &ullong_ptr_ctype, NULL);
513 	declare_builtin("__builtin_unreachable", &void_ctype, 0, NULL);
514 	declare_builtin("__builtin_usub_overflow", &bool_ctype, 0, &uint_ctype, &uint_ctype, &uint_ptr_ctype, NULL);
515 	declare_builtin("__builtin_usubl_overflow", &bool_ctype, 0, &ulong_ctype, &ulong_ctype, &ulong_ptr_ctype, NULL);
516 	declare_builtin("__builtin_usubll_overflow", &bool_ctype, 0, &ullong_ctype, &ullong_ctype, &ullong_ptr_ctype, NULL);
517 	declare_builtin("__builtin_va_arg_pack_len", size_t_ctype, 0, NULL);
518 	declare_builtin("__builtin_vprintf", &int_ctype, 0, &const_string_ctype, va_list_ctype, NULL);
519 	declare_builtin("__builtin_vsnprintf", &int_ctype, 0, &string_ctype, size_t_ctype, &const_string_ctype, va_list_ctype, NULL);
520 	declare_builtin("__builtin_vsprintf", &int_ctype, 0, &string_ctype, &const_string_ctype, va_list_ctype, NULL);
521 
522 	declare_builtin("__builtin___memcpy_chk", &ptr_ctype, 0, &ptr_ctype, &const_ptr_ctype, size_t_ctype, size_t_ctype, NULL);
523 	declare_builtin("__builtin___memmove_chk", &ptr_ctype, 0, &ptr_ctype, &const_ptr_ctype, size_t_ctype, size_t_ctype, NULL);
524 	declare_builtin("__builtin___mempcpy_chk", &ptr_ctype, 0, &ptr_ctype, &const_ptr_ctype, size_t_ctype, size_t_ctype, NULL);
525 	declare_builtin("__builtin___memset_chk", &ptr_ctype, 0, &ptr_ctype, &int_ctype, size_t_ctype, size_t_ctype, NULL);
526 	declare_builtin("__builtin___snprintf_chk", &int_ctype, 1, &string_ctype, size_t_ctype, &int_ctype , size_t_ctype, &const_string_ctype, NULL);
527 	declare_builtin("__builtin___sprintf_chk", &int_ctype, 1, &string_ctype, &int_ctype, size_t_ctype, &const_string_ctype, NULL);
528 	declare_builtin("__builtin___stpcpy_chk", &string_ctype, 0, &string_ctype, &const_string_ctype, size_t_ctype, NULL);
529 	declare_builtin("__builtin___strcat_chk", &string_ctype, 0, &string_ctype, &const_string_ctype, size_t_ctype, NULL);
530 	declare_builtin("__builtin___strcpy_chk", &string_ctype, 0, &string_ctype, &const_string_ctype, size_t_ctype, NULL);
531 	declare_builtin("__builtin___strncat_chk", &string_ctype, 0, &string_ctype, &const_string_ctype, size_t_ctype, size_t_ctype, NULL);
532 	declare_builtin("__builtin___strncpy_chk", &string_ctype, 0, &string_ctype, &const_string_ctype, size_t_ctype, size_t_ctype, NULL);
533 	declare_builtin("__builtin___vsnprintf_chk", &int_ctype, 0, &string_ctype, size_t_ctype, &int_ctype, size_t_ctype, &const_string_ctype, va_list_ctype, NULL);
534 	declare_builtin("__builtin___vsprintf_chk", &int_ctype, 0, &string_ctype, &int_ctype, size_t_ctype, &const_string_ctype, va_list_ctype, NULL);
535 
536 	declare_builtin("__sync_add_and_fetch", &int_ctype, 1, &ptr_ctype, NULL);
537 	declare_builtin("__sync_and_and_fetch", &int_ctype, 1, &ptr_ctype, NULL);
538 	declare_builtin("__sync_bool_compare_and_swap", &int_ctype, 1, &ptr_ctype, NULL);
539 	declare_builtin("__sync_fetch_and_add", &int_ctype, 1, &ptr_ctype, NULL);
540 	declare_builtin("__sync_fetch_and_and", &int_ctype, 1, &ptr_ctype, NULL);
541 	declare_builtin("__sync_fetch_and_nand", &int_ctype, 1, &ptr_ctype, NULL);
542 	declare_builtin("__sync_fetch_and_or", &int_ctype, 1, &ptr_ctype, NULL);
543 	declare_builtin("__sync_fetch_and_sub", &int_ctype, 1, &ptr_ctype, NULL);
544 	declare_builtin("__sync_fetch_and_xor", &int_ctype, 1, &ptr_ctype, NULL);
545 	declare_builtin("__sync_lock_release", &void_ctype, 1, &ptr_ctype, NULL);
546 	declare_builtin("__sync_lock_test_and_set", &int_ctype, 1, &ptr_ctype, NULL);
547 	declare_builtin("__sync_nand_and_fetch", &int_ctype, 1, &ptr_ctype, NULL);
548 	declare_builtin("__sync_or_and_fetch", &int_ctype, 1, &ptr_ctype, NULL);
549 	declare_builtin("__sync_sub_and_fetch", &int_ctype, 1, &ptr_ctype, NULL);
550 	declare_builtin("__sync_synchronize", &void_ctype, 0, NULL);
551 	declare_builtin("__sync_val_compare_and_swap", &int_ctype, 1, &ptr_ctype, NULL);
552 	declare_builtin("__sync_xor_and_fetch", &int_ctype, 1, &ptr_ctype, NULL);
553 
554 	// Blackfin-specific stuff
555 	declare_builtin("__builtin_bfin_csync", &void_ctype, 0, NULL);
556 	declare_builtin("__builtin_bfin_ssync", &void_ctype, 0, NULL);
557 	declare_builtin("__builtin_bfin_norm_fr1x32", &int_ctype, 0, &int_ctype, NULL);
558 }
559