Define ASM_OUTPUT_MAX_SKIP_ALIGN if the assembler supports .p2align.
[official-gcc.git] / gcc / c-common.c
blob872750320ed4df0715a7f25c0a6cc4cd218a1212
1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "toplev.h"
27 #include "output.h"
28 #include "c-pragma.h"
29 #include "rtl.h"
30 #include "ggc.h"
31 #include "expr.h"
32 #include "c-common.h"
33 #include "tm_p.h"
34 #include "obstack.h"
35 #include "cpplib.h"
36 cpp_reader *parse_in; /* Declared in c-lex.h. */
38 #undef WCHAR_TYPE_SIZE
39 #define WCHAR_TYPE_SIZE TYPE_PRECISION (wchar_type_node)
41 /* We let tm.h override the types used here, to handle trivial differences
42 such as the choice of unsigned int or long unsigned int for size_t.
43 When machines start needing nontrivial differences in the size type,
44 it would be best to do something here to figure out automatically
45 from other information what type to use. */
47 #ifndef SIZE_TYPE
48 #define SIZE_TYPE "long unsigned int"
49 #endif
51 #ifndef WCHAR_TYPE
52 #define WCHAR_TYPE "int"
53 #endif
55 #ifndef PTRDIFF_TYPE
56 #define PTRDIFF_TYPE "long int"
57 #endif
59 #ifndef WINT_TYPE
60 #define WINT_TYPE "unsigned int"
61 #endif
63 #ifndef INTMAX_TYPE
64 #define INTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
65 ? "int" \
66 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
67 ? "long int" \
68 : "long long int"))
69 #endif
71 #ifndef UINTMAX_TYPE
72 #define UINTMAX_TYPE ((INT_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
73 ? "unsigned int" \
74 : ((LONG_TYPE_SIZE == LONG_LONG_TYPE_SIZE) \
75 ? "long unsigned int" \
76 : "long long unsigned int"))
77 #endif
79 /* The following symbols are subsumed in the c_global_trees array, and
80 listed here individually for documentation purposes.
82 INTEGER_TYPE and REAL_TYPE nodes for the standard data types.
84 tree short_integer_type_node;
85 tree long_integer_type_node;
86 tree long_long_integer_type_node;
88 tree short_unsigned_type_node;
89 tree long_unsigned_type_node;
90 tree long_long_unsigned_type_node;
92 tree boolean_type_node;
93 tree boolean_false_node;
94 tree boolean_true_node;
96 tree ptrdiff_type_node;
98 tree unsigned_char_type_node;
99 tree signed_char_type_node;
100 tree wchar_type_node;
101 tree signed_wchar_type_node;
102 tree unsigned_wchar_type_node;
104 tree float_type_node;
105 tree double_type_node;
106 tree long_double_type_node;
108 tree complex_integer_type_node;
109 tree complex_float_type_node;
110 tree complex_double_type_node;
111 tree complex_long_double_type_node;
113 tree intQI_type_node;
114 tree intHI_type_node;
115 tree intSI_type_node;
116 tree intDI_type_node;
117 tree intTI_type_node;
119 tree unsigned_intQI_type_node;
120 tree unsigned_intHI_type_node;
121 tree unsigned_intSI_type_node;
122 tree unsigned_intDI_type_node;
123 tree unsigned_intTI_type_node;
125 tree widest_integer_literal_type_node;
126 tree widest_unsigned_literal_type_node;
128 Nodes for types `void *' and `const void *'.
130 tree ptr_type_node, const_ptr_type_node;
132 Nodes for types `char *' and `const char *'.
134 tree string_type_node, const_string_type_node;
136 Type `char[SOMENUMBER]'.
137 Used when an array of char is needed and the size is irrelevant.
139 tree char_array_type_node;
141 Type `int[SOMENUMBER]' or something like it.
142 Used when an array of int needed and the size is irrelevant.
144 tree int_array_type_node;
146 Type `wchar_t[SOMENUMBER]' or something like it.
147 Used when a wide string literal is created.
149 tree wchar_array_type_node;
151 Type `int ()' -- used for implicit declaration of functions.
153 tree default_function_type;
155 Function types `int (int)', etc.
157 tree int_ftype_int;
158 tree void_ftype;
159 tree void_ftype_ptr;
160 tree int_ftype_int;
161 tree ptr_ftype_sizetype;
163 A VOID_TYPE node, packaged in a TREE_LIST.
165 tree void_list_node;
167 The lazily created VAR_DECLS for __FUNCTION__, __PRETTY_FUNCTION__,
168 and __func__. (C doesn't generate __FUNCTION__ and__PRETTY_FUNCTION__
169 VAR_DECLS, but C++ does.)
171 tree function_name_decl_node;
172 tree pretty_function_name_declnode;
173 tree c99_function_name_decl_node;
175 Stack of nested function name VAR_DECLs.
177 tree saved_function_name_decls;
181 tree c_global_trees[CTI_MAX];
183 /* Nonzero means don't recognize the non-ANSI builtin functions. */
185 int flag_no_builtin;
187 /* Nonzero means don't recognize the non-ANSI builtin functions.
188 -ansi sets this. */
190 int flag_no_nonansi_builtin;
192 /* Nonzero means give `double' the same size as `float'. */
194 int flag_short_double;
196 /* Nonzero means give `wchar_t' the same size as `short'. */
198 int flag_short_wchar;
200 /* If non-NULL, dump the tree structure for the entire translation
201 unit to this file. */
203 const char *flag_dump_translation_unit;
205 /* Nonzero means warn about possible violations of sequence point rules. */
207 int warn_sequence_point;
209 /* The elements of `ridpointers' are identifier nodes for the reserved
210 type names and storage classes. It is indexed by a RID_... value. */
211 tree *ridpointers;
213 tree (*make_fname_decl) PARAMS ((tree, int));
215 /* If non-NULL, the address of a language-specific function that
216 returns 1 for language-specific statement codes. */
217 int (*lang_statement_code_p) PARAMS ((enum tree_code));
219 /* If non-NULL, the address of a language-specific function that takes
220 any action required right before expand_function_end is called. */
221 void (*lang_expand_function_end) PARAMS ((void));
223 /* If this variable is defined to a non-NULL value, it will be called
224 after the file has been completely parsed. */
225 void (*back_end_hook) PARAMS ((tree));
227 /* Nonzero means the expression being parsed will never be evaluated.
228 This is a count, since unevaluated expressions can nest. */
229 int skip_evaluation;
231 enum attrs {A_PACKED, A_NOCOMMON, A_COMMON, A_NORETURN, A_CONST, A_T_UNION,
232 A_NO_CHECK_MEMORY_USAGE, A_NO_INSTRUMENT_FUNCTION,
233 A_CONSTRUCTOR, A_DESTRUCTOR, A_MODE, A_SECTION, A_ALIGNED,
234 A_UNUSED, A_FORMAT, A_FORMAT_ARG, A_WEAK, A_ALIAS, A_MALLOC,
235 A_NO_LIMIT_STACK, A_PURE};
237 /* Information about how a function name is generated. */
238 struct fname_var_t
240 tree *decl; /* pointer to the VAR_DECL. */
241 unsigned rid; /* RID number for the identifier. */
242 int pretty; /* How pretty is it? */
245 /* The three ways of getting then name of the current function. */
247 const struct fname_var_t fname_vars[] =
249 /* C99 compliant __func__, must be first. */
250 {&c99_function_name_decl_node, RID_C99_FUNCTION_NAME, 0},
251 /* GCC __FUNCTION__ compliant. */
252 {&function_name_decl_node, RID_FUNCTION_NAME, 0},
253 /* GCC __PRETTY_FUNCTION__ compliant. */
254 {&pretty_function_name_decl_node, RID_PRETTY_FUNCTION_NAME, 1},
255 {NULL, 0, 0},
258 static void add_attribute PARAMS ((enum attrs, const char *,
259 int, int, int));
260 static void init_attributes PARAMS ((void));
261 static int default_valid_lang_attribute PARAMS ((tree, tree, tree, tree));
262 static int constant_fits_type_p PARAMS ((tree, tree));
264 /* Keep a stack of if statements. We record the number of compound
265 statements seen up to the if keyword, as well as the line number
266 and file of the if. If a potentially ambiguous else is seen, that
267 fact is recorded; the warning is issued when we can be sure that
268 the enclosing if statement does not have an else branch. */
269 typedef struct
271 int compstmt_count;
272 int line;
273 const char *file;
274 int needs_warning;
275 tree if_stmt;
276 } if_elt;
278 static if_elt *if_stack;
280 /* Amount of space in the if statement stack. */
281 static int if_stack_space = 0;
283 /* Stack pointer. */
284 static int if_stack_pointer = 0;
286 /* Record the start of an if-then, and record the start of it
287 for ambiguous else detection. */
289 void
290 c_expand_start_cond (cond, compstmt_count)
291 tree cond;
292 int compstmt_count;
294 tree if_stmt;
296 /* Make sure there is enough space on the stack. */
297 if (if_stack_space == 0)
299 if_stack_space = 10;
300 if_stack = (if_elt *)xmalloc (10 * sizeof (if_elt));
302 else if (if_stack_space == if_stack_pointer)
304 if_stack_space += 10;
305 if_stack = (if_elt *)xrealloc (if_stack, if_stack_space * sizeof (if_elt));
308 if_stmt = build_stmt (IF_STMT, NULL_TREE, NULL_TREE, NULL_TREE);
309 IF_COND (if_stmt) = cond;
310 add_stmt (if_stmt);
312 /* Record this if statement. */
313 if_stack[if_stack_pointer].compstmt_count = compstmt_count;
314 if_stack[if_stack_pointer].file = input_filename;
315 if_stack[if_stack_pointer].line = lineno;
316 if_stack[if_stack_pointer].needs_warning = 0;
317 if_stack[if_stack_pointer].if_stmt = if_stmt;
318 if_stack_pointer++;
321 /* Called after the then-clause for an if-statement is processed. */
323 void
324 c_finish_then ()
326 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
327 RECHAIN_STMTS (if_stmt, THEN_CLAUSE (if_stmt));
330 /* Record the end of an if-then. Optionally warn if a nested
331 if statement had an ambiguous else clause. */
333 void
334 c_expand_end_cond ()
336 if_stack_pointer--;
337 if (if_stack[if_stack_pointer].needs_warning)
338 warning_with_file_and_line (if_stack[if_stack_pointer].file,
339 if_stack[if_stack_pointer].line,
340 "suggest explicit braces to avoid ambiguous `else'");
341 last_expr_type = NULL_TREE;
344 /* Called between the then-clause and the else-clause
345 of an if-then-else. */
347 void
348 c_expand_start_else ()
350 /* An ambiguous else warning must be generated for the enclosing if
351 statement, unless we see an else branch for that one, too. */
352 if (warn_parentheses
353 && if_stack_pointer > 1
354 && (if_stack[if_stack_pointer - 1].compstmt_count
355 == if_stack[if_stack_pointer - 2].compstmt_count))
356 if_stack[if_stack_pointer - 2].needs_warning = 1;
358 /* Even if a nested if statement had an else branch, it can't be
359 ambiguous if this one also has an else. So don't warn in that
360 case. Also don't warn for any if statements nested in this else. */
361 if_stack[if_stack_pointer - 1].needs_warning = 0;
362 if_stack[if_stack_pointer - 1].compstmt_count--;
365 /* Called after the else-clause for an if-statement is processed. */
367 void
368 c_finish_else ()
370 tree if_stmt = if_stack[if_stack_pointer - 1].if_stmt;
371 RECHAIN_STMTS (if_stmt, ELSE_CLAUSE (if_stmt));
374 /* Push current bindings for the function name VAR_DECLS. */
376 void
377 start_fname_decls ()
379 unsigned ix;
380 tree saved = NULL_TREE;
382 for (ix = 0; fname_vars[ix].decl; ix++)
384 tree decl = *fname_vars[ix].decl;
386 if (decl)
388 saved = tree_cons (decl, build_int_2 (ix, 0), saved);
389 *fname_vars[ix].decl = NULL_TREE;
392 if (saved || saved_function_name_decls)
393 /* Normally they'll have been NULL, so only push if we've got a
394 stack, or they are non-NULL. */
395 saved_function_name_decls = tree_cons (saved, NULL_TREE,
396 saved_function_name_decls);
399 /* Finish up the current bindings, adding them into the
400 current function's statement tree. This is done by wrapping the
401 function's body in a COMPOUND_STMT containing these decls too. This
402 must be done _before_ finish_stmt_tree is called. If there is no
403 current function, we must be at file scope and no statements are
404 involved. Pop the previous bindings. */
406 void
407 finish_fname_decls ()
409 unsigned ix;
410 tree body = NULL_TREE;
411 tree stack = saved_function_name_decls;
413 for (; stack && TREE_VALUE (stack); stack = TREE_CHAIN (stack))
414 body = chainon (TREE_VALUE (stack), body);
416 if (body)
418 /* They were called into existance, so add to statement tree. */
419 body = chainon (body,
420 TREE_CHAIN (DECL_SAVED_TREE (current_function_decl)));
421 body = build_stmt (COMPOUND_STMT, body);
423 COMPOUND_STMT_NO_SCOPE (body) = 1;
424 TREE_CHAIN (DECL_SAVED_TREE (current_function_decl)) = body;
427 for (ix = 0; fname_vars[ix].decl; ix++)
428 *fname_vars[ix].decl = NULL_TREE;
430 if (stack)
432 /* We had saved values, restore them. */
433 tree saved;
435 for (saved = TREE_PURPOSE (stack); saved; saved = TREE_CHAIN (saved))
437 tree decl = TREE_PURPOSE (saved);
438 unsigned ix = TREE_INT_CST_LOW (TREE_VALUE (saved));
440 *fname_vars[ix].decl = decl;
442 stack = TREE_CHAIN (stack);
444 saved_function_name_decls = stack;
447 /* Return the text name of the current function, suitable prettified
448 by PRETTY_P. */
450 const char *
451 fname_as_string (pretty_p)
452 int pretty_p;
454 const char *name = NULL;
456 if (pretty_p)
457 name = (current_function_decl
458 ? (*decl_printable_name) (current_function_decl, 2)
459 : "top level");
460 else if (current_function_decl && DECL_NAME (current_function_decl))
461 name = IDENTIFIER_POINTER (DECL_NAME (current_function_decl));
462 else
463 name = "";
464 return name;
467 /* Return the text name of the current function, formatted as
468 required by the supplied RID value. */
470 const char *
471 fname_string (rid)
472 unsigned rid;
474 unsigned ix;
476 for (ix = 0; fname_vars[ix].decl; ix++)
477 if (fname_vars[ix].rid == rid)
478 break;
479 return fname_as_string (fname_vars[ix].pretty);
482 /* Return the VAR_DECL for a const char array naming the current
483 function. If the VAR_DECL has not yet been created, create it
484 now. RID indicates how it should be formatted and IDENTIFIER_NODE
485 ID is its name (unfortunately C and C++ hold the RID values of
486 keywords in different places, so we can't derive RID from ID in
487 this language independant code. */
489 tree
490 fname_decl (rid, id)
491 unsigned rid;
492 tree id;
494 unsigned ix;
495 tree decl = NULL_TREE;
497 for (ix = 0; fname_vars[ix].decl; ix++)
498 if (fname_vars[ix].rid == rid)
499 break;
501 decl = *fname_vars[ix].decl;
502 if (!decl)
504 tree saved_last_tree = last_tree;
506 decl = (*make_fname_decl) (id, fname_vars[ix].pretty);
507 if (last_tree != saved_last_tree)
509 /* We created some statement tree for the decl. This belongs
510 at the start of the function, so remove it now and reinsert
511 it after the function is complete. */
512 tree stmts = TREE_CHAIN (saved_last_tree);
514 TREE_CHAIN (saved_last_tree) = NULL_TREE;
515 last_tree = saved_last_tree;
516 saved_function_name_decls = tree_cons (decl, stmts,
517 saved_function_name_decls);
519 *fname_vars[ix].decl = decl;
521 if (!ix && !current_function_decl)
522 pedwarn_with_decl (decl, "`%s' is not defined outside of function scope");
524 return decl;
527 /* Given a chain of STRING_CST nodes,
528 concatenate them into one STRING_CST
529 and give it a suitable array-of-chars data type. */
531 tree
532 combine_strings (strings)
533 tree strings;
535 register tree value, t;
536 register int length = 1;
537 int wide_length = 0;
538 int wide_flag = 0;
539 int wchar_bytes = TYPE_PRECISION (wchar_type_node) / BITS_PER_UNIT;
540 int nchars;
541 const int nchars_max = flag_isoc99 ? 4095 : 509;
543 if (TREE_CHAIN (strings))
545 /* More than one in the chain, so concatenate. */
546 register char *p, *q;
548 /* Don't include the \0 at the end of each substring,
549 except for the last one.
550 Count wide strings and ordinary strings separately. */
551 for (t = strings; t; t = TREE_CHAIN (t))
553 if (TREE_TYPE (t) == wchar_array_type_node)
555 wide_length += (TREE_STRING_LENGTH (t) - wchar_bytes);
556 wide_flag = 1;
558 else
559 length += (TREE_STRING_LENGTH (t) - 1);
562 /* If anything is wide, the non-wides will be converted,
563 which makes them take more space. */
564 if (wide_flag)
565 length = length * wchar_bytes + wide_length;
567 p = alloca (length);
569 /* Copy the individual strings into the new combined string.
570 If the combined string is wide, convert the chars to ints
571 for any individual strings that are not wide. */
573 q = p;
574 for (t = strings; t; t = TREE_CHAIN (t))
576 int len = (TREE_STRING_LENGTH (t)
577 - ((TREE_TYPE (t) == wchar_array_type_node)
578 ? wchar_bytes : 1));
579 if ((TREE_TYPE (t) == wchar_array_type_node) == wide_flag)
581 memcpy (q, TREE_STRING_POINTER (t), len);
582 q += len;
584 else
586 int i;
587 for (i = 0; i < len; i++)
589 if (WCHAR_TYPE_SIZE == HOST_BITS_PER_SHORT)
590 ((short *) q)[i] = TREE_STRING_POINTER (t)[i];
591 else
592 ((int *) q)[i] = TREE_STRING_POINTER (t)[i];
594 q += len * wchar_bytes;
597 if (wide_flag)
599 int i;
600 for (i = 0; i < wchar_bytes; i++)
601 *q++ = 0;
603 else
604 *q = 0;
606 value = build_string (length, p);
608 else
610 value = strings;
611 length = TREE_STRING_LENGTH (value);
612 if (TREE_TYPE (value) == wchar_array_type_node)
613 wide_flag = 1;
616 /* Compute the number of elements, for the array type. */
617 nchars = wide_flag ? length / wchar_bytes : length;
619 if (pedantic && nchars - 1 > nchars_max && c_language == clk_c)
620 pedwarn ("string length `%d' is greater than the length `%d' ISO C%d compilers are required to support",
621 nchars - 1, nchars_max, flag_isoc99 ? 99 : 89);
623 /* Create the array type for the string constant.
624 -Wwrite-strings says make the string constant an array of const char
625 so that copying it to a non-const pointer will get a warning.
626 For C++, this is the standard behavior. */
627 if (flag_const_strings
628 && (! flag_traditional && ! flag_writable_strings))
630 tree elements
631 = build_type_variant (wide_flag ? wchar_type_node : char_type_node,
632 1, 0);
633 TREE_TYPE (value)
634 = build_array_type (elements,
635 build_index_type (build_int_2 (nchars - 1, 0)));
637 else
638 TREE_TYPE (value)
639 = build_array_type (wide_flag ? wchar_type_node : char_type_node,
640 build_index_type (build_int_2 (nchars - 1, 0)));
642 TREE_CONSTANT (value) = 1;
643 TREE_READONLY (value) = ! flag_writable_strings;
644 TREE_STATIC (value) = 1;
645 return value;
648 /* To speed up processing of attributes, we maintain an array of
649 IDENTIFIER_NODES and the corresponding attribute types. */
651 /* Array to hold attribute information. */
653 static struct {enum attrs id; tree name; int min, max, decl_req;} attrtab[50];
655 static int attrtab_idx = 0;
657 /* Add an entry to the attribute table above. */
659 static void
660 add_attribute (id, string, min_len, max_len, decl_req)
661 enum attrs id;
662 const char *string;
663 int min_len, max_len;
664 int decl_req;
666 char buf[100];
668 attrtab[attrtab_idx].id = id;
669 attrtab[attrtab_idx].name = get_identifier (string);
670 attrtab[attrtab_idx].min = min_len;
671 attrtab[attrtab_idx].max = max_len;
672 attrtab[attrtab_idx++].decl_req = decl_req;
674 sprintf (buf, "__%s__", string);
676 attrtab[attrtab_idx].id = id;
677 attrtab[attrtab_idx].name = get_identifier (buf);
678 attrtab[attrtab_idx].min = min_len;
679 attrtab[attrtab_idx].max = max_len;
680 attrtab[attrtab_idx++].decl_req = decl_req;
683 /* Initialize attribute table. */
685 static void
686 init_attributes ()
688 add_attribute (A_PACKED, "packed", 0, 0, 0);
689 add_attribute (A_NOCOMMON, "nocommon", 0, 0, 1);
690 add_attribute (A_COMMON, "common", 0, 0, 1);
691 add_attribute (A_NORETURN, "noreturn", 0, 0, 1);
692 add_attribute (A_NORETURN, "volatile", 0, 0, 1);
693 add_attribute (A_UNUSED, "unused", 0, 0, 0);
694 add_attribute (A_CONST, "const", 0, 0, 1);
695 add_attribute (A_T_UNION, "transparent_union", 0, 0, 0);
696 add_attribute (A_CONSTRUCTOR, "constructor", 0, 0, 1);
697 add_attribute (A_DESTRUCTOR, "destructor", 0, 0, 1);
698 add_attribute (A_MODE, "mode", 1, 1, 1);
699 add_attribute (A_SECTION, "section", 1, 1, 1);
700 add_attribute (A_ALIGNED, "aligned", 0, 1, 0);
701 add_attribute (A_FORMAT, "format", 3, 3, 1);
702 add_attribute (A_FORMAT_ARG, "format_arg", 1, 1, 1);
703 add_attribute (A_WEAK, "weak", 0, 0, 1);
704 add_attribute (A_ALIAS, "alias", 1, 1, 1);
705 add_attribute (A_NO_INSTRUMENT_FUNCTION, "no_instrument_function", 0, 0, 1);
706 add_attribute (A_NO_CHECK_MEMORY_USAGE, "no_check_memory_usage", 0, 0, 1);
707 add_attribute (A_MALLOC, "malloc", 0, 0, 1);
708 add_attribute (A_NO_LIMIT_STACK, "no_stack_limit", 0, 0, 1);
709 add_attribute (A_PURE, "pure", 0, 0, 1);
712 /* Default implementation of valid_lang_attribute, below. By default, there
713 are no language-specific attributes. */
715 static int
716 default_valid_lang_attribute (attr_name, attr_args, decl, type)
717 tree attr_name ATTRIBUTE_UNUSED;
718 tree attr_args ATTRIBUTE_UNUSED;
719 tree decl ATTRIBUTE_UNUSED;
720 tree type ATTRIBUTE_UNUSED;
722 return 0;
725 /* Return a 1 if ATTR_NAME and ATTR_ARGS denote a valid language-specific
726 attribute for either declaration DECL or type TYPE and 0 otherwise. */
728 int (*valid_lang_attribute) PARAMS ((tree, tree, tree, tree))
729 = default_valid_lang_attribute;
731 /* Process the attributes listed in ATTRIBUTES and PREFIX_ATTRIBUTES
732 and install them in NODE, which is either a DECL (including a TYPE_DECL)
733 or a TYPE. PREFIX_ATTRIBUTES can appear after the declaration specifiers
734 and declaration modifiers but before the declaration proper. */
736 void
737 decl_attributes (node, attributes, prefix_attributes)
738 tree node, attributes, prefix_attributes;
740 tree decl = 0, type = 0;
741 int is_type = 0;
742 tree a;
744 if (attrtab_idx == 0)
745 init_attributes ();
747 if (DECL_P (node))
749 decl = node;
750 type = TREE_TYPE (decl);
751 is_type = TREE_CODE (node) == TYPE_DECL;
753 else if (TYPE_P (node))
754 type = node, is_type = 1;
756 #ifdef PRAGMA_INSERT_ATTRIBUTES
757 /* If the code in c-pragma.c wants to insert some attributes then
758 allow it to do so. Do this before allowing machine back ends to
759 insert attributes, so that they have the opportunity to override
760 anything done here. */
761 PRAGMA_INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
762 #endif
764 #ifdef INSERT_ATTRIBUTES
765 INSERT_ATTRIBUTES (node, & attributes, & prefix_attributes);
766 #endif
768 attributes = chainon (prefix_attributes, attributes);
770 for (a = attributes; a; a = TREE_CHAIN (a))
772 tree name = TREE_PURPOSE (a);
773 tree args = TREE_VALUE (a);
774 int i;
775 enum attrs id;
777 for (i = 0; i < attrtab_idx; i++)
778 if (attrtab[i].name == name)
779 break;
781 if (i == attrtab_idx)
783 if (! valid_machine_attribute (name, args, decl, type)
784 && ! (* valid_lang_attribute) (name, args, decl, type))
785 warning ("`%s' attribute directive ignored",
786 IDENTIFIER_POINTER (name));
787 else if (decl != 0)
788 type = TREE_TYPE (decl);
789 continue;
791 else if (attrtab[i].decl_req && decl == 0)
793 warning ("`%s' attribute does not apply to types",
794 IDENTIFIER_POINTER (name));
795 continue;
797 else if (list_length (args) < attrtab[i].min
798 || list_length (args) > attrtab[i].max)
800 error ("wrong number of arguments specified for `%s' attribute",
801 IDENTIFIER_POINTER (name));
802 continue;
805 id = attrtab[i].id;
806 switch (id)
808 case A_PACKED:
809 if (is_type)
810 TYPE_PACKED (type) = 1;
811 else if (TREE_CODE (decl) == FIELD_DECL)
812 DECL_PACKED (decl) = 1;
813 /* We can't set DECL_PACKED for a VAR_DECL, because the bit is
814 used for DECL_REGISTER. It wouldn't mean anything anyway. */
815 else
816 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
817 break;
819 case A_NOCOMMON:
820 if (TREE_CODE (decl) == VAR_DECL)
821 DECL_COMMON (decl) = 0;
822 else
823 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
824 break;
826 case A_COMMON:
827 if (TREE_CODE (decl) == VAR_DECL)
828 DECL_COMMON (decl) = 1;
829 else
830 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
831 break;
833 case A_NORETURN:
834 if (TREE_CODE (decl) == FUNCTION_DECL)
835 TREE_THIS_VOLATILE (decl) = 1;
836 else if (TREE_CODE (type) == POINTER_TYPE
837 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
838 TREE_TYPE (decl) = type
839 = build_pointer_type
840 (build_type_variant (TREE_TYPE (type),
841 TREE_READONLY (TREE_TYPE (type)), 1));
842 else
843 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
844 break;
846 case A_MALLOC:
847 if (TREE_CODE (decl) == FUNCTION_DECL)
848 DECL_IS_MALLOC (decl) = 1;
849 /* ??? TODO: Support types. */
850 else
851 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
852 break;
854 case A_UNUSED:
855 if (is_type)
856 if (decl)
857 TREE_USED (decl) = 1;
858 else
859 TREE_USED (type) = 1;
860 else if (TREE_CODE (decl) == PARM_DECL
861 || TREE_CODE (decl) == VAR_DECL
862 || TREE_CODE (decl) == FUNCTION_DECL
863 || TREE_CODE (decl) == LABEL_DECL)
864 TREE_USED (decl) = 1;
865 else
866 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
867 break;
869 case A_CONST:
870 if (TREE_CODE (decl) == FUNCTION_DECL)
871 TREE_READONLY (decl) = 1;
872 else if (TREE_CODE (type) == POINTER_TYPE
873 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE)
874 TREE_TYPE (decl) = type
875 = build_pointer_type
876 (build_type_variant (TREE_TYPE (type), 1,
877 TREE_THIS_VOLATILE (TREE_TYPE (type))));
878 else
879 warning ( "`%s' attribute ignored", IDENTIFIER_POINTER (name));
880 break;
882 case A_PURE:
883 if (TREE_CODE (decl) == FUNCTION_DECL)
884 DECL_IS_PURE (decl) = 1;
885 /* ??? TODO: Support types. */
886 else
887 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
888 break;
891 case A_T_UNION:
892 if (is_type
893 && TREE_CODE (type) == UNION_TYPE
894 && (decl == 0
895 || (TYPE_FIELDS (type) != 0
896 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))))
897 TYPE_TRANSPARENT_UNION (type) = 1;
898 else if (decl != 0 && TREE_CODE (decl) == PARM_DECL
899 && TREE_CODE (type) == UNION_TYPE
900 && TYPE_MODE (type) == DECL_MODE (TYPE_FIELDS (type)))
901 DECL_TRANSPARENT_UNION (decl) = 1;
902 else
903 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
904 break;
906 case A_CONSTRUCTOR:
907 if (TREE_CODE (decl) == FUNCTION_DECL
908 && TREE_CODE (type) == FUNCTION_TYPE
909 && decl_function_context (decl) == 0)
911 DECL_STATIC_CONSTRUCTOR (decl) = 1;
912 TREE_USED (decl) = 1;
914 else
915 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
916 break;
918 case A_DESTRUCTOR:
919 if (TREE_CODE (decl) == FUNCTION_DECL
920 && TREE_CODE (type) == FUNCTION_TYPE
921 && decl_function_context (decl) == 0)
923 DECL_STATIC_DESTRUCTOR (decl) = 1;
924 TREE_USED (decl) = 1;
926 else
927 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
928 break;
930 case A_MODE:
931 if (TREE_CODE (TREE_VALUE (args)) != IDENTIFIER_NODE)
932 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
933 else
935 int j;
936 const char *p = IDENTIFIER_POINTER (TREE_VALUE (args));
937 int len = strlen (p);
938 enum machine_mode mode = VOIDmode;
939 tree typefm;
941 if (len > 4 && p[0] == '_' && p[1] == '_'
942 && p[len - 1] == '_' && p[len - 2] == '_')
944 char *newp = (char *) alloca (len - 1);
946 strcpy (newp, &p[2]);
947 newp[len - 4] = '\0';
948 p = newp;
951 /* Give this decl a type with the specified mode.
952 First check for the special modes. */
953 if (! strcmp (p, "byte"))
954 mode = byte_mode;
955 else if (!strcmp (p, "word"))
956 mode = word_mode;
957 else if (! strcmp (p, "pointer"))
958 mode = ptr_mode;
959 else
960 for (j = 0; j < NUM_MACHINE_MODES; j++)
961 if (!strcmp (p, GET_MODE_NAME (j)))
962 mode = (enum machine_mode) j;
964 if (mode == VOIDmode)
965 error ("unknown machine mode `%s'", p);
966 else if (0 == (typefm = type_for_mode (mode,
967 TREE_UNSIGNED (type))))
968 error ("no data type for mode `%s'", p);
969 else
971 if (TYPE_PRECISION (typefm) > (TREE_UNSIGNED (type)
972 ? TYPE_PRECISION(uintmax_type_node)
973 : TYPE_PRECISION(intmax_type_node))
974 && pedantic)
975 pedwarn ("type with more precision than %s",
976 TREE_UNSIGNED (type) ? "uintmax_t" : "intmax_t");
977 TREE_TYPE (decl) = type = typefm;
978 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
979 layout_decl (decl, 0);
982 break;
984 case A_SECTION:
985 #ifdef ASM_OUTPUT_SECTION_NAME
986 if ((TREE_CODE (decl) == FUNCTION_DECL
987 || TREE_CODE (decl) == VAR_DECL)
988 && TREE_CODE (TREE_VALUE (args)) == STRING_CST)
990 if (TREE_CODE (decl) == VAR_DECL
991 && current_function_decl != NULL_TREE
992 && ! TREE_STATIC (decl))
993 error_with_decl (decl,
994 "section attribute cannot be specified for local variables");
995 /* The decl may have already been given a section attribute from
996 a previous declaration. Ensure they match. */
997 else if (DECL_SECTION_NAME (decl) != NULL_TREE
998 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
999 TREE_STRING_POINTER (TREE_VALUE (args))) != 0)
1000 error_with_decl (node,
1001 "section of `%s' conflicts with previous declaration");
1002 else
1003 DECL_SECTION_NAME (decl) = TREE_VALUE (args);
1005 else
1006 error_with_decl (node,
1007 "section attribute not allowed for `%s'");
1008 #else
1009 error_with_decl (node,
1010 "section attributes are not supported for this target");
1011 #endif
1012 break;
1014 case A_ALIGNED:
1016 tree align_expr
1017 = (args ? TREE_VALUE (args)
1018 : size_int (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1019 int i;
1021 /* Strip any NOPs of any kind. */
1022 while (TREE_CODE (align_expr) == NOP_EXPR
1023 || TREE_CODE (align_expr) == CONVERT_EXPR
1024 || TREE_CODE (align_expr) == NON_LVALUE_EXPR)
1025 align_expr = TREE_OPERAND (align_expr, 0);
1027 if (TREE_CODE (align_expr) != INTEGER_CST)
1029 error ("requested alignment is not a constant");
1030 continue;
1033 if ((i = tree_log2 (align_expr)) == -1)
1034 error ("requested alignment is not a power of 2");
1035 else if (i > HOST_BITS_PER_INT - 2)
1036 error ("requested alignment is too large");
1037 else if (is_type)
1039 /* If we have a TYPE_DECL, then copy the type, so that we
1040 don't accidentally modify a builtin type. See pushdecl. */
1041 if (decl && TREE_TYPE (decl) != error_mark_node
1042 && DECL_ORIGINAL_TYPE (decl) == NULL_TREE)
1044 tree tt = TREE_TYPE (decl);
1045 DECL_ORIGINAL_TYPE (decl) = tt;
1046 tt = build_type_copy (tt);
1047 TYPE_NAME (tt) = decl;
1048 TREE_USED (tt) = TREE_USED (decl);
1049 TREE_TYPE (decl) = tt;
1050 type = tt;
1053 TYPE_ALIGN (type) = (1 << i) * BITS_PER_UNIT;
1054 TYPE_USER_ALIGN (type) = 1;
1056 else if (TREE_CODE (decl) != VAR_DECL
1057 && TREE_CODE (decl) != FIELD_DECL)
1058 error_with_decl (decl,
1059 "alignment may not be specified for `%s'");
1060 else
1062 DECL_ALIGN (decl) = (1 << i) * BITS_PER_UNIT;
1063 DECL_USER_ALIGN (decl) = 1;
1066 break;
1068 case A_FORMAT:
1069 decl_handle_format_attribute (decl, args);
1070 break;
1072 case A_FORMAT_ARG:
1073 decl_handle_format_arg_attribute (decl, args);
1074 break;
1076 case A_WEAK:
1077 declare_weak (decl);
1078 break;
1080 case A_ALIAS:
1081 if ((TREE_CODE (decl) == FUNCTION_DECL && DECL_INITIAL (decl))
1082 || (TREE_CODE (decl) != FUNCTION_DECL && ! DECL_EXTERNAL (decl)))
1083 error_with_decl (decl,
1084 "`%s' defined both normally and as an alias");
1085 else if (decl_function_context (decl) == 0)
1087 tree id;
1089 id = TREE_VALUE (args);
1090 if (TREE_CODE (id) != STRING_CST)
1092 error ("alias arg not a string");
1093 break;
1095 id = get_identifier (TREE_STRING_POINTER (id));
1096 /* This counts as a use of the object pointed to. */
1097 TREE_USED (id) = 1;
1099 if (TREE_CODE (decl) == FUNCTION_DECL)
1100 DECL_INITIAL (decl) = error_mark_node;
1101 else
1102 DECL_EXTERNAL (decl) = 0;
1103 assemble_alias (decl, id);
1105 else
1106 warning ("`%s' attribute ignored", IDENTIFIER_POINTER (name));
1107 break;
1109 case A_NO_CHECK_MEMORY_USAGE:
1110 if (TREE_CODE (decl) != FUNCTION_DECL)
1112 error_with_decl (decl,
1113 "`%s' attribute applies only to functions",
1114 IDENTIFIER_POINTER (name));
1116 else if (DECL_INITIAL (decl))
1118 error_with_decl (decl,
1119 "can't set `%s' attribute after definition",
1120 IDENTIFIER_POINTER (name));
1122 else
1123 DECL_NO_CHECK_MEMORY_USAGE (decl) = 1;
1124 break;
1126 case A_NO_INSTRUMENT_FUNCTION:
1127 if (TREE_CODE (decl) != FUNCTION_DECL)
1129 error_with_decl (decl,
1130 "`%s' attribute applies only to functions",
1131 IDENTIFIER_POINTER (name));
1133 else if (DECL_INITIAL (decl))
1135 error_with_decl (decl,
1136 "can't set `%s' attribute after definition",
1137 IDENTIFIER_POINTER (name));
1139 else
1140 DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (decl) = 1;
1141 break;
1143 case A_NO_LIMIT_STACK:
1144 if (TREE_CODE (decl) != FUNCTION_DECL)
1146 error_with_decl (decl,
1147 "`%s' attribute applies only to functions",
1148 IDENTIFIER_POINTER (name));
1150 else if (DECL_INITIAL (decl))
1152 error_with_decl (decl,
1153 "can't set `%s' attribute after definition",
1154 IDENTIFIER_POINTER (name));
1156 else
1157 DECL_NO_LIMIT_STACK (decl) = 1;
1158 break;
1163 /* Split SPECS_ATTRS, a list of declspecs and prefix attributes, into two
1164 lists. SPECS_ATTRS may also be just a typespec (eg: RECORD_TYPE).
1166 The head of the declspec list is stored in DECLSPECS.
1167 The head of the attribute list is stored in PREFIX_ATTRIBUTES.
1169 Note that attributes in SPECS_ATTRS are stored in the TREE_PURPOSE of
1170 the list elements. We drop the containing TREE_LIST nodes and link the
1171 resulting attributes together the way decl_attributes expects them. */
1173 void
1174 split_specs_attrs (specs_attrs, declspecs, prefix_attributes)
1175 tree specs_attrs;
1176 tree *declspecs, *prefix_attributes;
1178 tree t, s, a, next, specs, attrs;
1180 /* This can happen after an __extension__ in pedantic mode. */
1181 if (specs_attrs != NULL_TREE
1182 && TREE_CODE (specs_attrs) == INTEGER_CST)
1184 *declspecs = NULL_TREE;
1185 *prefix_attributes = NULL_TREE;
1186 return;
1189 /* This can happen in c++ (eg: decl: typespec initdecls ';'). */
1190 if (specs_attrs != NULL_TREE
1191 && TREE_CODE (specs_attrs) != TREE_LIST)
1193 *declspecs = specs_attrs;
1194 *prefix_attributes = NULL_TREE;
1195 return;
1198 /* Remember to keep the lists in the same order, element-wise. */
1200 specs = s = NULL_TREE;
1201 attrs = a = NULL_TREE;
1202 for (t = specs_attrs; t; t = next)
1204 next = TREE_CHAIN (t);
1205 /* Declspecs have a non-NULL TREE_VALUE. */
1206 if (TREE_VALUE (t) != NULL_TREE)
1208 if (specs == NULL_TREE)
1209 specs = s = t;
1210 else
1212 TREE_CHAIN (s) = t;
1213 s = t;
1216 else
1218 if (attrs == NULL_TREE)
1219 attrs = a = TREE_PURPOSE (t);
1220 else
1222 TREE_CHAIN (a) = TREE_PURPOSE (t);
1223 a = TREE_PURPOSE (t);
1225 /* More attrs can be linked here, move A to the end. */
1226 while (TREE_CHAIN (a) != NULL_TREE)
1227 a = TREE_CHAIN (a);
1231 /* Terminate the lists. */
1232 if (s != NULL_TREE)
1233 TREE_CHAIN (s) = NULL_TREE;
1234 if (a != NULL_TREE)
1235 TREE_CHAIN (a) = NULL_TREE;
1237 /* All done. */
1238 *declspecs = specs;
1239 *prefix_attributes = attrs;
1242 /* Strip attributes from SPECS_ATTRS, a list of declspecs and attributes.
1243 This function is used by the parser when a rule will accept attributes
1244 in a particular position, but we don't want to support that just yet.
1246 A warning is issued for every ignored attribute. */
1248 tree
1249 strip_attrs (specs_attrs)
1250 tree specs_attrs;
1252 tree specs, attrs;
1254 split_specs_attrs (specs_attrs, &specs, &attrs);
1256 while (attrs)
1258 warning ("`%s' attribute ignored",
1259 IDENTIFIER_POINTER (TREE_PURPOSE (attrs)));
1260 attrs = TREE_CHAIN (attrs);
1263 return specs;
1266 static int is_valid_printf_arglist PARAMS ((tree));
1267 static rtx c_expand_builtin PARAMS ((tree, rtx, enum machine_mode, enum expand_modifier));
1268 static rtx c_expand_builtin_printf PARAMS ((tree, rtx, enum machine_mode,
1269 enum expand_modifier, int));
1270 static rtx c_expand_builtin_fprintf PARAMS ((tree, rtx, enum machine_mode,
1271 enum expand_modifier, int));
1273 /* Print a warning if a constant expression had overflow in folding.
1274 Invoke this function on every expression that the language
1275 requires to be a constant expression.
1276 Note the ANSI C standard says it is erroneous for a
1277 constant expression to overflow. */
1279 void
1280 constant_expression_warning (value)
1281 tree value;
1283 if ((TREE_CODE (value) == INTEGER_CST || TREE_CODE (value) == REAL_CST
1284 || TREE_CODE (value) == COMPLEX_CST)
1285 && TREE_CONSTANT_OVERFLOW (value) && pedantic)
1286 pedwarn ("overflow in constant expression");
1289 /* Print a warning if an expression had overflow in folding.
1290 Invoke this function on every expression that
1291 (1) appears in the source code, and
1292 (2) might be a constant expression that overflowed, and
1293 (3) is not already checked by convert_and_check;
1294 however, do not invoke this function on operands of explicit casts. */
1296 void
1297 overflow_warning (value)
1298 tree value;
1300 if ((TREE_CODE (value) == INTEGER_CST
1301 || (TREE_CODE (value) == COMPLEX_CST
1302 && TREE_CODE (TREE_REALPART (value)) == INTEGER_CST))
1303 && TREE_OVERFLOW (value))
1305 TREE_OVERFLOW (value) = 0;
1306 if (skip_evaluation == 0)
1307 warning ("integer overflow in expression");
1309 else if ((TREE_CODE (value) == REAL_CST
1310 || (TREE_CODE (value) == COMPLEX_CST
1311 && TREE_CODE (TREE_REALPART (value)) == REAL_CST))
1312 && TREE_OVERFLOW (value))
1314 TREE_OVERFLOW (value) = 0;
1315 if (skip_evaluation == 0)
1316 warning ("floating point overflow in expression");
1320 /* Print a warning if a large constant is truncated to unsigned,
1321 or if -Wconversion is used and a constant < 0 is converted to unsigned.
1322 Invoke this function on every expression that might be implicitly
1323 converted to an unsigned type. */
1325 void
1326 unsigned_conversion_warning (result, operand)
1327 tree result, operand;
1329 if (TREE_CODE (operand) == INTEGER_CST
1330 && TREE_CODE (TREE_TYPE (result)) == INTEGER_TYPE
1331 && TREE_UNSIGNED (TREE_TYPE (result))
1332 && skip_evaluation == 0
1333 && !int_fits_type_p (operand, TREE_TYPE (result)))
1335 if (!int_fits_type_p (operand, signed_type (TREE_TYPE (result))))
1336 /* This detects cases like converting -129 or 256 to unsigned char. */
1337 warning ("large integer implicitly truncated to unsigned type");
1338 else if (warn_conversion)
1339 warning ("negative integer implicitly converted to unsigned type");
1343 /* Nonzero if constant C has a value that is permissible
1344 for type TYPE (an INTEGER_TYPE). */
1346 static int
1347 constant_fits_type_p (c, type)
1348 tree c, type;
1350 if (TREE_CODE (c) == INTEGER_CST)
1351 return int_fits_type_p (c, type);
1353 c = convert (type, c);
1354 return !TREE_OVERFLOW (c);
1357 /* Convert EXPR to TYPE, warning about conversion problems with constants.
1358 Invoke this function on every expression that is converted implicitly,
1359 i.e. because of language rules and not because of an explicit cast. */
1361 tree
1362 convert_and_check (type, expr)
1363 tree type, expr;
1365 tree t = convert (type, expr);
1366 if (TREE_CODE (t) == INTEGER_CST)
1368 if (TREE_OVERFLOW (t))
1370 TREE_OVERFLOW (t) = 0;
1372 /* Do not diagnose overflow in a constant expression merely
1373 because a conversion overflowed. */
1374 TREE_CONSTANT_OVERFLOW (t) = TREE_CONSTANT_OVERFLOW (expr);
1376 /* No warning for converting 0x80000000 to int. */
1377 if (!(TREE_UNSIGNED (type) < TREE_UNSIGNED (TREE_TYPE (expr))
1378 && TREE_CODE (TREE_TYPE (expr)) == INTEGER_TYPE
1379 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (expr))))
1380 /* If EXPR fits in the unsigned version of TYPE,
1381 don't warn unless pedantic. */
1382 if ((pedantic
1383 || TREE_UNSIGNED (type)
1384 || ! constant_fits_type_p (expr, unsigned_type (type)))
1385 && skip_evaluation == 0)
1386 warning ("overflow in implicit constant conversion");
1388 else
1389 unsigned_conversion_warning (t, expr);
1391 return t;
1394 /* A node in a list that describes references to variables (EXPR), which are
1395 either read accesses if WRITER is zero, or write accesses, in which case
1396 WRITER is the parent of EXPR. */
1397 struct tlist
1399 struct tlist *next;
1400 tree expr, writer;
1403 /* Used to implement a cache the results of a call to verify_tree. We only
1404 use this for SAVE_EXPRs. */
1405 struct tlist_cache
1407 struct tlist_cache *next;
1408 struct tlist *cache_before_sp;
1409 struct tlist *cache_after_sp;
1410 tree expr;
1413 /* Obstack to use when allocating tlist structures, and corresponding
1414 firstobj. */
1415 static struct obstack tlist_obstack;
1416 static char *tlist_firstobj = 0;
1418 /* Keep track of the identifiers we've warned about, so we can avoid duplicate
1419 warnings. */
1420 static struct tlist *warned_ids;
1421 /* SAVE_EXPRs need special treatment. We process them only once and then
1422 cache the results. */
1423 static struct tlist_cache *save_expr_cache;
1425 static void add_tlist PARAMS ((struct tlist **, struct tlist *, tree, int));
1426 static void merge_tlist PARAMS ((struct tlist **, struct tlist *, int));
1427 static void verify_tree PARAMS ((tree, struct tlist **, struct tlist **, tree));
1428 static int warning_candidate_p PARAMS ((tree));
1429 static void warn_for_collisions PARAMS ((struct tlist *));
1430 static void warn_for_collisions_1 PARAMS ((tree, tree, struct tlist *, int));
1431 static struct tlist *new_tlist PARAMS ((struct tlist *, tree, tree));
1432 static void verify_sequence_points PARAMS ((tree));
1434 /* Create a new struct tlist and fill in its fields. */
1435 static struct tlist *
1436 new_tlist (next, t, writer)
1437 struct tlist *next;
1438 tree t;
1439 tree writer;
1441 struct tlist *l;
1442 l = (struct tlist *) obstack_alloc (&tlist_obstack, sizeof *l);
1443 l->next = next;
1444 l->expr = t;
1445 l->writer = writer;
1446 return l;
1449 /* Add duplicates of the nodes found in ADD to the list *TO. If EXCLUDE_WRITER
1450 is nonnull, we ignore any node we find which has a writer equal to it. */
1452 static void
1453 add_tlist (to, add, exclude_writer, copy)
1454 struct tlist **to;
1455 struct tlist *add;
1456 tree exclude_writer;
1457 int copy;
1459 while (add)
1461 struct tlist *next = add->next;
1462 if (! copy)
1463 add->next = *to;
1464 if (! exclude_writer || add->writer != exclude_writer)
1465 *to = copy ? new_tlist (*to, add->expr, add->writer) : add;
1466 add = next;
1470 /* Merge the nodes of ADD into TO. This merging process is done so that for
1471 each variable that already exists in TO, no new node is added; however if
1472 there is a write access recorded in ADD, and an occurrence on TO is only
1473 a read access, then the occurrence in TO will be modified to record the
1474 write. */
1476 static void
1477 merge_tlist (to, add, copy)
1478 struct tlist **to;
1479 struct tlist *add;
1480 int copy;
1482 struct tlist **end = to;
1484 while (*end)
1485 end = &(*end)->next;
1487 while (add)
1489 int found = 0;
1490 struct tlist *tmp2;
1491 struct tlist *next = add->next;
1493 for (tmp2 = *to; tmp2; tmp2 = tmp2->next)
1494 if (tmp2->expr == add->expr)
1496 found = 1;
1497 if (! tmp2->writer)
1498 tmp2->writer = add->writer;
1500 if (! found)
1502 *end = copy ? add : new_tlist (NULL, add->expr, add->writer);
1503 end = &(*end)->next;
1504 *end = 0;
1506 add = next;
1510 /* WRITTEN is a variable, WRITER is its parent. Warn if any of the variable
1511 references in list LIST conflict with it, excluding reads if ONLY writers
1512 is nonzero. */
1514 static void
1515 warn_for_collisions_1 (written, writer, list, only_writes)
1516 tree written, writer;
1517 struct tlist *list;
1518 int only_writes;
1520 struct tlist *tmp;
1522 /* Avoid duplicate warnings. */
1523 for (tmp = warned_ids; tmp; tmp = tmp->next)
1524 if (tmp->expr == written)
1525 return;
1527 while (list)
1529 if (list->expr == written
1530 && list->writer != writer
1531 && (! only_writes || list->writer))
1533 warned_ids = new_tlist (warned_ids, written, NULL_TREE);
1534 warning ("operation on `%s' may be undefined",
1535 IDENTIFIER_POINTER (DECL_NAME (list->expr)));
1537 list = list->next;
1541 /* Given a list LIST of references to variables, find whether any of these
1542 can cause conflicts due to missing sequence points. */
1544 static void
1545 warn_for_collisions (list)
1546 struct tlist *list;
1548 struct tlist *tmp;
1550 for (tmp = list; tmp; tmp = tmp->next)
1552 if (tmp->writer)
1553 warn_for_collisions_1 (tmp->expr, tmp->writer, list, 0);
1557 /* Return nonzero if X is a tree that can be verified by the sequence poitn
1558 warnings. */
1559 static int
1560 warning_candidate_p (x)
1561 tree x;
1563 return TREE_CODE (x) == VAR_DECL || TREE_CODE (x) == PARM_DECL;
1566 /* Walk the tree X, and record accesses to variables. If X is written by the
1567 parent tree, WRITER is the parent.
1568 We store accesses in one of the two lists: PBEFORE_SP, and PNO_SP. If this
1569 expression or its only operand forces a sequence point, then everything up
1570 to the sequence point is stored in PBEFORE_SP. Everything else gets stored
1571 in PNO_SP.
1572 Once we return, we will have emitted warnings if any subexpression before
1573 such a sequence point could be undefined. On a higher level, however, the
1574 sequence point may not be relevant, and we'll merge the two lists.
1576 Example: (b++, a) + b;
1577 The call that processes the COMPOUND_EXPR will store the increment of B
1578 in PBEFORE_SP, and the use of A in PNO_SP. The higher-level call that
1579 processes the PLUS_EXPR will need to merge the two lists so that
1580 eventually, all accesses end up on the same list (and we'll warn about the
1581 unordered subexpressions b++ and b.
1583 A note on merging. If we modify the former example so that our expression
1584 becomes
1585 (b++, b) + a
1586 care must be taken not simply to add all three expressions into the final
1587 PNO_SP list. The function merge_tlist takes care of that by merging the
1588 before-SP list of the COMPOUND_EXPR into its after-SP list in a special
1589 way, so that no more than one access to B is recorded. */
1591 static void
1592 verify_tree (x, pbefore_sp, pno_sp, writer)
1593 tree x;
1594 struct tlist **pbefore_sp, **pno_sp;
1595 tree writer;
1597 struct tlist *tmp_before, *tmp_nosp, *tmp_list2, *tmp_list3;
1598 enum tree_code code;
1599 char class;
1601 restart:
1602 code = TREE_CODE (x);
1603 class = TREE_CODE_CLASS (code);
1605 if (warning_candidate_p (x))
1607 *pno_sp = new_tlist (*pno_sp, x, writer);
1608 return;
1611 switch (code)
1613 case CONSTRUCTOR:
1614 return;
1616 case COMPOUND_EXPR:
1617 case TRUTH_ANDIF_EXPR:
1618 case TRUTH_ORIF_EXPR:
1619 tmp_before = tmp_nosp = tmp_list3 = 0;
1620 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1621 warn_for_collisions (tmp_nosp);
1622 merge_tlist (pbefore_sp, tmp_before, 0);
1623 merge_tlist (pbefore_sp, tmp_nosp, 0);
1624 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, pno_sp, NULL_TREE);
1625 merge_tlist (pbefore_sp, tmp_list3, 0);
1626 return;
1628 case COND_EXPR:
1629 tmp_before = tmp_list2 = 0;
1630 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_list2, NULL_TREE);
1631 warn_for_collisions (tmp_list2);
1632 merge_tlist (pbefore_sp, tmp_before, 0);
1633 merge_tlist (pbefore_sp, tmp_list2, 1);
1635 tmp_list3 = tmp_nosp = 0;
1636 verify_tree (TREE_OPERAND (x, 1), &tmp_list3, &tmp_nosp, NULL_TREE);
1637 warn_for_collisions (tmp_nosp);
1638 merge_tlist (pbefore_sp, tmp_list3, 0);
1640 tmp_list3 = tmp_list2 = 0;
1641 verify_tree (TREE_OPERAND (x, 2), &tmp_list3, &tmp_list2, NULL_TREE);
1642 warn_for_collisions (tmp_list2);
1643 merge_tlist (pbefore_sp, tmp_list3, 0);
1644 /* Rather than add both tmp_nosp and tmp_list2, we have to merge the
1645 two first, to avoid warning for (a ? b++ : b++). */
1646 merge_tlist (&tmp_nosp, tmp_list2, 0);
1647 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1648 return;
1650 case PREDECREMENT_EXPR:
1651 case PREINCREMENT_EXPR:
1652 case POSTDECREMENT_EXPR:
1653 case POSTINCREMENT_EXPR:
1654 verify_tree (TREE_OPERAND (x, 0), pno_sp, pno_sp, x);
1655 return;
1657 case MODIFY_EXPR:
1658 tmp_before = tmp_nosp = tmp_list3 = 0;
1659 verify_tree (TREE_OPERAND (x, 1), &tmp_before, &tmp_nosp, NULL_TREE);
1660 verify_tree (TREE_OPERAND (x, 0), &tmp_list3, &tmp_list3, x);
1661 /* Expressions inside the LHS are not ordered wrt. the sequence points
1662 in the RHS. Example:
1663 *a = (a++, 2)
1664 Despite the fact that the modification of "a" is in the before_sp
1665 list (tmp_before), it conflicts with the use of "a" in the LHS.
1666 We can handle this by adding the contents of tmp_list3
1667 to those of tmp_before, and redoing the collision warnings for that
1668 list. */
1669 add_tlist (&tmp_before, tmp_list3, x, 1);
1670 warn_for_collisions (tmp_before);
1671 /* Exclude the LHS itself here; we first have to merge it into the
1672 tmp_nosp list. This is done to avoid warning for "a = a"; if we
1673 didn't exclude the LHS, we'd get it twice, once as a read and once
1674 as a write. */
1675 add_tlist (pno_sp, tmp_list3, x, 0);
1676 warn_for_collisions_1 (TREE_OPERAND (x, 0), x, tmp_nosp, 1);
1678 merge_tlist (pbefore_sp, tmp_before, 0);
1679 if (warning_candidate_p (TREE_OPERAND (x, 0)))
1680 merge_tlist (&tmp_nosp, new_tlist (NULL, TREE_OPERAND (x, 0), x), 0);
1681 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 1);
1682 return;
1684 case CALL_EXPR:
1685 /* We need to warn about conflicts among arguments and conflicts between
1686 args and the function address. Side effects of the function address,
1687 however, are not ordered by the sequence point of the call. */
1688 tmp_before = tmp_nosp = tmp_list2 = tmp_list3 = 0;
1689 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1690 if (TREE_OPERAND (x, 1))
1691 verify_tree (TREE_OPERAND (x, 1), &tmp_list2, &tmp_list3, NULL_TREE);
1692 merge_tlist (&tmp_list3, tmp_list2, 0);
1693 add_tlist (&tmp_before, tmp_list3, NULL_TREE, 0);
1694 add_tlist (&tmp_before, tmp_nosp, NULL_TREE, 0);
1695 warn_for_collisions (tmp_before);
1696 add_tlist (pbefore_sp, tmp_before, NULL_TREE, 0);
1697 return;
1699 case TREE_LIST:
1700 /* Scan all the list, e.g. indices of multi dimensional array. */
1701 while (x)
1703 tmp_before = tmp_nosp = 0;
1704 verify_tree (TREE_VALUE (x), &tmp_before, &tmp_nosp, NULL_TREE);
1705 merge_tlist (&tmp_nosp, tmp_before, 0);
1706 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1707 x = TREE_CHAIN (x);
1709 return;
1711 case SAVE_EXPR:
1713 struct tlist_cache *t;
1714 for (t = save_expr_cache; t; t = t->next)
1715 if (t->expr == x)
1716 break;
1718 if (! t)
1720 t = (struct tlist_cache *) obstack_alloc (&tlist_obstack,
1721 sizeof *t);
1722 t->next = save_expr_cache;
1723 t->expr = x;
1724 save_expr_cache = t;
1726 tmp_before = tmp_nosp = 0;
1727 verify_tree (TREE_OPERAND (x, 0), &tmp_before, &tmp_nosp, NULL_TREE);
1728 warn_for_collisions (tmp_nosp);
1730 tmp_list3 = 0;
1731 while (tmp_nosp)
1733 struct tlist *t = tmp_nosp;
1734 tmp_nosp = t->next;
1735 merge_tlist (&tmp_list3, t, 0);
1737 t->cache_before_sp = tmp_before;
1738 t->cache_after_sp = tmp_list3;
1740 merge_tlist (pbefore_sp, t->cache_before_sp, 1);
1741 add_tlist (pno_sp, t->cache_after_sp, NULL_TREE, 1);
1742 return;
1744 default:
1745 break;
1748 if (class == '1')
1750 if (first_rtl_op (code) == 0)
1751 return;
1752 x = TREE_OPERAND (x, 0);
1753 writer = 0;
1754 goto restart;
1757 switch (class)
1759 case 'r':
1760 case '<':
1761 case '2':
1762 case 'b':
1763 case 'e':
1764 case 's':
1765 case 'x':
1767 int lp;
1768 int max = first_rtl_op (TREE_CODE (x));
1769 for (lp = 0; lp < max; lp++)
1771 tmp_before = tmp_nosp = 0;
1772 verify_tree (TREE_OPERAND (x, lp), &tmp_before, &tmp_nosp, NULL_TREE);
1773 merge_tlist (&tmp_nosp, tmp_before, 0);
1774 add_tlist (pno_sp, tmp_nosp, NULL_TREE, 0);
1776 break;
1781 /* Try to warn for undefined behaviour in EXPR due to missing sequence
1782 points. */
1784 static void
1785 verify_sequence_points (expr)
1786 tree expr;
1788 struct tlist *before_sp = 0, *after_sp = 0;
1790 warned_ids = 0;
1791 save_expr_cache = 0;
1792 if (tlist_firstobj == 0)
1794 gcc_obstack_init (&tlist_obstack);
1795 tlist_firstobj = obstack_alloc (&tlist_obstack, 0);
1798 verify_tree (expr, &before_sp, &after_sp, 0);
1799 warn_for_collisions (after_sp);
1800 obstack_free (&tlist_obstack, tlist_firstobj);
1803 tree
1804 c_expand_expr_stmt (expr)
1805 tree expr;
1807 /* Do default conversion if safe and possibly important,
1808 in case within ({...}). */
1809 if ((TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE && lvalue_p (expr))
1810 || TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE)
1811 expr = default_conversion (expr);
1813 if (warn_sequence_point)
1814 verify_sequence_points (expr);
1816 if (TREE_TYPE (expr) != error_mark_node
1817 && !COMPLETE_OR_VOID_TYPE_P (TREE_TYPE (expr))
1818 && TREE_CODE (TREE_TYPE (expr)) != ARRAY_TYPE)
1819 error ("expression statement has incomplete type");
1821 last_expr_type = TREE_TYPE (expr);
1822 return add_stmt (build_stmt (EXPR_STMT, expr));
1825 /* Validate the expression after `case' and apply default promotions. */
1827 tree
1828 check_case_value (value)
1829 tree value;
1831 if (value == NULL_TREE)
1832 return value;
1834 /* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue. */
1835 STRIP_TYPE_NOPS (value);
1836 /* In C++, the following is allowed:
1838 const int i = 3;
1839 switch (...) { case i: ... }
1841 So, we try to reduce the VALUE to a constant that way. */
1842 if (c_language == clk_cplusplus)
1844 value = decl_constant_value (value);
1845 STRIP_TYPE_NOPS (value);
1846 value = fold (value);
1849 if (TREE_CODE (value) != INTEGER_CST
1850 && value != error_mark_node)
1852 error ("case label does not reduce to an integer constant");
1853 value = error_mark_node;
1855 else
1856 /* Promote char or short to int. */
1857 value = default_conversion (value);
1859 constant_expression_warning (value);
1861 return value;
1864 /* Return an integer type with BITS bits of precision,
1865 that is unsigned if UNSIGNEDP is nonzero, otherwise signed. */
1867 tree
1868 type_for_size (bits, unsignedp)
1869 unsigned bits;
1870 int unsignedp;
1872 if (bits == TYPE_PRECISION (integer_type_node))
1873 return unsignedp ? unsigned_type_node : integer_type_node;
1875 if (bits == TYPE_PRECISION (signed_char_type_node))
1876 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1878 if (bits == TYPE_PRECISION (short_integer_type_node))
1879 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1881 if (bits == TYPE_PRECISION (long_integer_type_node))
1882 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1884 if (bits == TYPE_PRECISION (long_long_integer_type_node))
1885 return (unsignedp ? long_long_unsigned_type_node
1886 : long_long_integer_type_node);
1888 if (bits == TYPE_PRECISION (widest_integer_literal_type_node))
1889 return (unsignedp ? widest_unsigned_literal_type_node
1890 : widest_integer_literal_type_node);
1892 if (bits <= TYPE_PRECISION (intQI_type_node))
1893 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1895 if (bits <= TYPE_PRECISION (intHI_type_node))
1896 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1898 if (bits <= TYPE_PRECISION (intSI_type_node))
1899 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1901 if (bits <= TYPE_PRECISION (intDI_type_node))
1902 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1904 return 0;
1907 /* Return a data type that has machine mode MODE.
1908 If the mode is an integer,
1909 then UNSIGNEDP selects between signed and unsigned types. */
1911 tree
1912 type_for_mode (mode, unsignedp)
1913 enum machine_mode mode;
1914 int unsignedp;
1916 if (mode == TYPE_MODE (integer_type_node))
1917 return unsignedp ? unsigned_type_node : integer_type_node;
1919 if (mode == TYPE_MODE (signed_char_type_node))
1920 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
1922 if (mode == TYPE_MODE (short_integer_type_node))
1923 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
1925 if (mode == TYPE_MODE (long_integer_type_node))
1926 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
1928 if (mode == TYPE_MODE (long_long_integer_type_node))
1929 return unsignedp ? long_long_unsigned_type_node : long_long_integer_type_node;
1931 if (mode == TYPE_MODE (widest_integer_literal_type_node))
1932 return unsignedp ? widest_unsigned_literal_type_node
1933 : widest_integer_literal_type_node;
1935 if (mode == TYPE_MODE (intQI_type_node))
1936 return unsignedp ? unsigned_intQI_type_node : intQI_type_node;
1938 if (mode == TYPE_MODE (intHI_type_node))
1939 return unsignedp ? unsigned_intHI_type_node : intHI_type_node;
1941 if (mode == TYPE_MODE (intSI_type_node))
1942 return unsignedp ? unsigned_intSI_type_node : intSI_type_node;
1944 if (mode == TYPE_MODE (intDI_type_node))
1945 return unsignedp ? unsigned_intDI_type_node : intDI_type_node;
1947 #if HOST_BITS_PER_WIDE_INT >= 64
1948 if (mode == TYPE_MODE (intTI_type_node))
1949 return unsignedp ? unsigned_intTI_type_node : intTI_type_node;
1950 #endif
1952 if (mode == TYPE_MODE (float_type_node))
1953 return float_type_node;
1955 if (mode == TYPE_MODE (double_type_node))
1956 return double_type_node;
1958 if (mode == TYPE_MODE (long_double_type_node))
1959 return long_double_type_node;
1961 if (mode == TYPE_MODE (build_pointer_type (char_type_node)))
1962 return build_pointer_type (char_type_node);
1964 if (mode == TYPE_MODE (build_pointer_type (integer_type_node)))
1965 return build_pointer_type (integer_type_node);
1967 #ifdef VECTOR_MODE_SUPPORTED_P
1968 if (mode == TYPE_MODE (V4SF_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1969 return V4SF_type_node;
1970 if (mode == TYPE_MODE (V4SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1971 return V4SI_type_node;
1972 if (mode == TYPE_MODE (V2SI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1973 return V2SI_type_node;
1974 if (mode == TYPE_MODE (V4HI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1975 return V4HI_type_node;
1976 if (mode == TYPE_MODE (V8QI_type_node) && VECTOR_MODE_SUPPORTED_P (mode))
1977 return V8QI_type_node;
1978 #endif
1980 return 0;
1983 /* Return an unsigned type the same as TYPE in other respects. */
1984 tree
1985 unsigned_type (type)
1986 tree type;
1988 tree type1 = TYPE_MAIN_VARIANT (type);
1989 if (type1 == signed_char_type_node || type1 == char_type_node)
1990 return unsigned_char_type_node;
1991 if (type1 == integer_type_node)
1992 return unsigned_type_node;
1993 if (type1 == short_integer_type_node)
1994 return short_unsigned_type_node;
1995 if (type1 == long_integer_type_node)
1996 return long_unsigned_type_node;
1997 if (type1 == long_long_integer_type_node)
1998 return long_long_unsigned_type_node;
1999 if (type1 == widest_integer_literal_type_node)
2000 return widest_unsigned_literal_type_node;
2001 #if HOST_BITS_PER_WIDE_INT >= 64
2002 if (type1 == intTI_type_node)
2003 return unsigned_intTI_type_node;
2004 #endif
2005 if (type1 == intDI_type_node)
2006 return unsigned_intDI_type_node;
2007 if (type1 == intSI_type_node)
2008 return unsigned_intSI_type_node;
2009 if (type1 == intHI_type_node)
2010 return unsigned_intHI_type_node;
2011 if (type1 == intQI_type_node)
2012 return unsigned_intQI_type_node;
2014 return signed_or_unsigned_type (1, type);
2017 /* Return a signed type the same as TYPE in other respects. */
2019 tree
2020 signed_type (type)
2021 tree type;
2023 tree type1 = TYPE_MAIN_VARIANT (type);
2024 if (type1 == unsigned_char_type_node || type1 == char_type_node)
2025 return signed_char_type_node;
2026 if (type1 == unsigned_type_node)
2027 return integer_type_node;
2028 if (type1 == short_unsigned_type_node)
2029 return short_integer_type_node;
2030 if (type1 == long_unsigned_type_node)
2031 return long_integer_type_node;
2032 if (type1 == long_long_unsigned_type_node)
2033 return long_long_integer_type_node;
2034 if (type1 == widest_unsigned_literal_type_node)
2035 return widest_integer_literal_type_node;
2036 #if HOST_BITS_PER_WIDE_INT >= 64
2037 if (type1 == unsigned_intTI_type_node)
2038 return intTI_type_node;
2039 #endif
2040 if (type1 == unsigned_intDI_type_node)
2041 return intDI_type_node;
2042 if (type1 == unsigned_intSI_type_node)
2043 return intSI_type_node;
2044 if (type1 == unsigned_intHI_type_node)
2045 return intHI_type_node;
2046 if (type1 == unsigned_intQI_type_node)
2047 return intQI_type_node;
2049 return signed_or_unsigned_type (0, type);
2052 /* Return a type the same as TYPE except unsigned or
2053 signed according to UNSIGNEDP. */
2055 tree
2056 signed_or_unsigned_type (unsignedp, type)
2057 int unsignedp;
2058 tree type;
2060 if (! INTEGRAL_TYPE_P (type)
2061 || TREE_UNSIGNED (type) == unsignedp)
2062 return type;
2064 if (TYPE_PRECISION (type) == TYPE_PRECISION (signed_char_type_node))
2065 return unsignedp ? unsigned_char_type_node : signed_char_type_node;
2066 if (TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node))
2067 return unsignedp ? unsigned_type_node : integer_type_node;
2068 if (TYPE_PRECISION (type) == TYPE_PRECISION (short_integer_type_node))
2069 return unsignedp ? short_unsigned_type_node : short_integer_type_node;
2070 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_integer_type_node))
2071 return unsignedp ? long_unsigned_type_node : long_integer_type_node;
2072 if (TYPE_PRECISION (type) == TYPE_PRECISION (long_long_integer_type_node))
2073 return (unsignedp ? long_long_unsigned_type_node
2074 : long_long_integer_type_node);
2075 if (TYPE_PRECISION (type) == TYPE_PRECISION (widest_integer_literal_type_node))
2076 return (unsignedp ? widest_unsigned_literal_type_node
2077 : widest_integer_literal_type_node);
2078 return type;
2081 /* Return the minimum number of bits needed to represent VALUE in a
2082 signed or unsigned type, UNSIGNEDP says which. */
2084 unsigned int
2085 min_precision (value, unsignedp)
2086 tree value;
2087 int unsignedp;
2089 int log;
2091 /* If the value is negative, compute its negative minus 1. The latter
2092 adjustment is because the absolute value of the largest negative value
2093 is one larger than the largest positive value. This is equivalent to
2094 a bit-wise negation, so use that operation instead. */
2096 if (tree_int_cst_sgn (value) < 0)
2097 value = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (value), value));
2099 /* Return the number of bits needed, taking into account the fact
2100 that we need one more bit for a signed than unsigned type. */
2102 if (integer_zerop (value))
2103 log = 0;
2104 else
2105 log = tree_floor_log2 (value);
2107 return log + 1 + ! unsignedp;
2110 /* Print an error message for invalid operands to arith operation CODE.
2111 NOP_EXPR is used as a special case (see truthvalue_conversion). */
2113 void
2114 binary_op_error (code)
2115 enum tree_code code;
2117 register const char *opname;
2119 switch (code)
2121 case NOP_EXPR:
2122 error ("invalid truth-value expression");
2123 return;
2125 case PLUS_EXPR:
2126 opname = "+"; break;
2127 case MINUS_EXPR:
2128 opname = "-"; break;
2129 case MULT_EXPR:
2130 opname = "*"; break;
2131 case MAX_EXPR:
2132 opname = "max"; break;
2133 case MIN_EXPR:
2134 opname = "min"; break;
2135 case EQ_EXPR:
2136 opname = "=="; break;
2137 case NE_EXPR:
2138 opname = "!="; break;
2139 case LE_EXPR:
2140 opname = "<="; break;
2141 case GE_EXPR:
2142 opname = ">="; break;
2143 case LT_EXPR:
2144 opname = "<"; break;
2145 case GT_EXPR:
2146 opname = ">"; break;
2147 case LSHIFT_EXPR:
2148 opname = "<<"; break;
2149 case RSHIFT_EXPR:
2150 opname = ">>"; break;
2151 case TRUNC_MOD_EXPR:
2152 case FLOOR_MOD_EXPR:
2153 opname = "%"; break;
2154 case TRUNC_DIV_EXPR:
2155 case FLOOR_DIV_EXPR:
2156 opname = "/"; break;
2157 case BIT_AND_EXPR:
2158 opname = "&"; break;
2159 case BIT_IOR_EXPR:
2160 opname = "|"; break;
2161 case TRUTH_ANDIF_EXPR:
2162 opname = "&&"; break;
2163 case TRUTH_ORIF_EXPR:
2164 opname = "||"; break;
2165 case BIT_XOR_EXPR:
2166 opname = "^"; break;
2167 case LROTATE_EXPR:
2168 case RROTATE_EXPR:
2169 opname = "rotate"; break;
2170 default:
2171 opname = "unknown"; break;
2173 error ("invalid operands to binary %s", opname);
2176 /* Subroutine of build_binary_op, used for comparison operations.
2177 See if the operands have both been converted from subword integer types
2178 and, if so, perhaps change them both back to their original type.
2179 This function is also responsible for converting the two operands
2180 to the proper common type for comparison.
2182 The arguments of this function are all pointers to local variables
2183 of build_binary_op: OP0_PTR is &OP0, OP1_PTR is &OP1,
2184 RESTYPE_PTR is &RESULT_TYPE and RESCODE_PTR is &RESULTCODE.
2186 If this function returns nonzero, it means that the comparison has
2187 a constant value. What this function returns is an expression for
2188 that value. */
2190 tree
2191 shorten_compare (op0_ptr, op1_ptr, restype_ptr, rescode_ptr)
2192 tree *op0_ptr, *op1_ptr;
2193 tree *restype_ptr;
2194 enum tree_code *rescode_ptr;
2196 register tree type;
2197 tree op0 = *op0_ptr;
2198 tree op1 = *op1_ptr;
2199 int unsignedp0, unsignedp1;
2200 int real1, real2;
2201 tree primop0, primop1;
2202 enum tree_code code = *rescode_ptr;
2204 /* Throw away any conversions to wider types
2205 already present in the operands. */
2207 primop0 = get_narrower (op0, &unsignedp0);
2208 primop1 = get_narrower (op1, &unsignedp1);
2210 /* Handle the case that OP0 does not *contain* a conversion
2211 but it *requires* conversion to FINAL_TYPE. */
2213 if (op0 == primop0 && TREE_TYPE (op0) != *restype_ptr)
2214 unsignedp0 = TREE_UNSIGNED (TREE_TYPE (op0));
2215 if (op1 == primop1 && TREE_TYPE (op1) != *restype_ptr)
2216 unsignedp1 = TREE_UNSIGNED (TREE_TYPE (op1));
2218 /* If one of the operands must be floated, we cannot optimize. */
2219 real1 = TREE_CODE (TREE_TYPE (primop0)) == REAL_TYPE;
2220 real2 = TREE_CODE (TREE_TYPE (primop1)) == REAL_TYPE;
2222 /* If first arg is constant, swap the args (changing operation
2223 so value is preserved), for canonicalization. Don't do this if
2224 the second arg is 0. */
2226 if (TREE_CONSTANT (primop0)
2227 && ! integer_zerop (primop1) && ! real_zerop (primop1))
2229 register tree tem = primop0;
2230 register int temi = unsignedp0;
2231 primop0 = primop1;
2232 primop1 = tem;
2233 tem = op0;
2234 op0 = op1;
2235 op1 = tem;
2236 *op0_ptr = op0;
2237 *op1_ptr = op1;
2238 unsignedp0 = unsignedp1;
2239 unsignedp1 = temi;
2240 temi = real1;
2241 real1 = real2;
2242 real2 = temi;
2244 switch (code)
2246 case LT_EXPR:
2247 code = GT_EXPR;
2248 break;
2249 case GT_EXPR:
2250 code = LT_EXPR;
2251 break;
2252 case LE_EXPR:
2253 code = GE_EXPR;
2254 break;
2255 case GE_EXPR:
2256 code = LE_EXPR;
2257 break;
2258 default:
2259 break;
2261 *rescode_ptr = code;
2264 /* If comparing an integer against a constant more bits wide,
2265 maybe we can deduce a value of 1 or 0 independent of the data.
2266 Or else truncate the constant now
2267 rather than extend the variable at run time.
2269 This is only interesting if the constant is the wider arg.
2270 Also, it is not safe if the constant is unsigned and the
2271 variable arg is signed, since in this case the variable
2272 would be sign-extended and then regarded as unsigned.
2273 Our technique fails in this case because the lowest/highest
2274 possible unsigned results don't follow naturally from the
2275 lowest/highest possible values of the variable operand.
2276 For just EQ_EXPR and NE_EXPR there is another technique that
2277 could be used: see if the constant can be faithfully represented
2278 in the other operand's type, by truncating it and reextending it
2279 and see if that preserves the constant's value. */
2281 if (!real1 && !real2
2282 && TREE_CODE (primop1) == INTEGER_CST
2283 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr))
2285 int min_gt, max_gt, min_lt, max_lt;
2286 tree maxval, minval;
2287 /* 1 if comparison is nominally unsigned. */
2288 int unsignedp = TREE_UNSIGNED (*restype_ptr);
2289 tree val;
2291 type = signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0));
2293 /* If TYPE is an enumeration, then we need to get its min/max
2294 values from it's underlying integral type, not the enumerated
2295 type itself. */
2296 if (TREE_CODE (type) == ENUMERAL_TYPE)
2297 type = type_for_size (TYPE_PRECISION (type), unsignedp0);
2299 maxval = TYPE_MAX_VALUE (type);
2300 minval = TYPE_MIN_VALUE (type);
2302 if (unsignedp && !unsignedp0)
2303 *restype_ptr = signed_type (*restype_ptr);
2305 if (TREE_TYPE (primop1) != *restype_ptr)
2306 primop1 = convert (*restype_ptr, primop1);
2307 if (type != *restype_ptr)
2309 minval = convert (*restype_ptr, minval);
2310 maxval = convert (*restype_ptr, maxval);
2313 if (unsignedp && unsignedp0)
2315 min_gt = INT_CST_LT_UNSIGNED (primop1, minval);
2316 max_gt = INT_CST_LT_UNSIGNED (primop1, maxval);
2317 min_lt = INT_CST_LT_UNSIGNED (minval, primop1);
2318 max_lt = INT_CST_LT_UNSIGNED (maxval, primop1);
2320 else
2322 min_gt = INT_CST_LT (primop1, minval);
2323 max_gt = INT_CST_LT (primop1, maxval);
2324 min_lt = INT_CST_LT (minval, primop1);
2325 max_lt = INT_CST_LT (maxval, primop1);
2328 val = 0;
2329 /* This used to be a switch, but Genix compiler can't handle that. */
2330 if (code == NE_EXPR)
2332 if (max_lt || min_gt)
2333 val = boolean_true_node;
2335 else if (code == EQ_EXPR)
2337 if (max_lt || min_gt)
2338 val = boolean_false_node;
2340 else if (code == LT_EXPR)
2342 if (max_lt)
2343 val = boolean_true_node;
2344 if (!min_lt)
2345 val = boolean_false_node;
2347 else if (code == GT_EXPR)
2349 if (min_gt)
2350 val = boolean_true_node;
2351 if (!max_gt)
2352 val = boolean_false_node;
2354 else if (code == LE_EXPR)
2356 if (!max_gt)
2357 val = boolean_true_node;
2358 if (min_gt)
2359 val = boolean_false_node;
2361 else if (code == GE_EXPR)
2363 if (!min_lt)
2364 val = boolean_true_node;
2365 if (max_lt)
2366 val = boolean_false_node;
2369 /* If primop0 was sign-extended and unsigned comparison specd,
2370 we did a signed comparison above using the signed type bounds.
2371 But the comparison we output must be unsigned.
2373 Also, for inequalities, VAL is no good; but if the signed
2374 comparison had *any* fixed result, it follows that the
2375 unsigned comparison just tests the sign in reverse
2376 (positive values are LE, negative ones GE).
2377 So we can generate an unsigned comparison
2378 against an extreme value of the signed type. */
2380 if (unsignedp && !unsignedp0)
2382 if (val != 0)
2383 switch (code)
2385 case LT_EXPR:
2386 case GE_EXPR:
2387 primop1 = TYPE_MIN_VALUE (type);
2388 val = 0;
2389 break;
2391 case LE_EXPR:
2392 case GT_EXPR:
2393 primop1 = TYPE_MAX_VALUE (type);
2394 val = 0;
2395 break;
2397 default:
2398 break;
2400 type = unsigned_type (type);
2403 if (!max_gt && !unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2405 /* This is the case of (char)x >?< 0x80, which people used to use
2406 expecting old C compilers to change the 0x80 into -0x80. */
2407 if (val == boolean_false_node)
2408 warning ("comparison is always false due to limited range of data type");
2409 if (val == boolean_true_node)
2410 warning ("comparison is always true due to limited range of data type");
2413 if (!min_lt && unsignedp0 && TREE_CODE (primop0) != INTEGER_CST)
2415 /* This is the case of (unsigned char)x >?< -1 or < 0. */
2416 if (val == boolean_false_node)
2417 warning ("comparison is always false due to limited range of data type");
2418 if (val == boolean_true_node)
2419 warning ("comparison is always true due to limited range of data type");
2422 if (val != 0)
2424 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2425 if (TREE_SIDE_EFFECTS (primop0))
2426 return build (COMPOUND_EXPR, TREE_TYPE (val), primop0, val);
2427 return val;
2430 /* Value is not predetermined, but do the comparison
2431 in the type of the operand that is not constant.
2432 TYPE is already properly set. */
2434 else if (real1 && real2
2435 && (TYPE_PRECISION (TREE_TYPE (primop0))
2436 == TYPE_PRECISION (TREE_TYPE (primop1))))
2437 type = TREE_TYPE (primop0);
2439 /* If args' natural types are both narrower than nominal type
2440 and both extend in the same manner, compare them
2441 in the type of the wider arg.
2442 Otherwise must actually extend both to the nominal
2443 common type lest different ways of extending
2444 alter the result.
2445 (eg, (short)-1 == (unsigned short)-1 should be 0.) */
2447 else if (unsignedp0 == unsignedp1 && real1 == real2
2448 && TYPE_PRECISION (TREE_TYPE (primop0)) < TYPE_PRECISION (*restype_ptr)
2449 && TYPE_PRECISION (TREE_TYPE (primop1)) < TYPE_PRECISION (*restype_ptr))
2451 type = common_type (TREE_TYPE (primop0), TREE_TYPE (primop1));
2452 type = signed_or_unsigned_type (unsignedp0
2453 || TREE_UNSIGNED (*restype_ptr),
2454 type);
2455 /* Make sure shorter operand is extended the right way
2456 to match the longer operand. */
2457 primop0 = convert (signed_or_unsigned_type (unsignedp0, TREE_TYPE (primop0)),
2458 primop0);
2459 primop1 = convert (signed_or_unsigned_type (unsignedp1, TREE_TYPE (primop1)),
2460 primop1);
2462 else
2464 /* Here we must do the comparison on the nominal type
2465 using the args exactly as we received them. */
2466 type = *restype_ptr;
2467 primop0 = op0;
2468 primop1 = op1;
2470 if (!real1 && !real2 && integer_zerop (primop1)
2471 && TREE_UNSIGNED (*restype_ptr))
2473 tree value = 0;
2474 switch (code)
2476 case GE_EXPR:
2477 /* All unsigned values are >= 0, so we warn if extra warnings
2478 are requested. However, if OP0 is a constant that is
2479 >= 0, the signedness of the comparison isn't an issue,
2480 so suppress the warning. */
2481 if (extra_warnings && !in_system_header
2482 && ! (TREE_CODE (primop0) == INTEGER_CST
2483 && ! TREE_OVERFLOW (convert (signed_type (type),
2484 primop0))))
2485 warning ("comparison of unsigned expression >= 0 is always true");
2486 value = boolean_true_node;
2487 break;
2489 case LT_EXPR:
2490 if (extra_warnings && !in_system_header
2491 && ! (TREE_CODE (primop0) == INTEGER_CST
2492 && ! TREE_OVERFLOW (convert (signed_type (type),
2493 primop0))))
2494 warning ("comparison of unsigned expression < 0 is always false");
2495 value = boolean_false_node;
2496 break;
2498 default:
2499 break;
2502 if (value != 0)
2504 /* Don't forget to evaluate PRIMOP0 if it has side effects. */
2505 if (TREE_SIDE_EFFECTS (primop0))
2506 return build (COMPOUND_EXPR, TREE_TYPE (value),
2507 primop0, value);
2508 return value;
2513 *op0_ptr = convert (type, primop0);
2514 *op1_ptr = convert (type, primop1);
2516 *restype_ptr = boolean_type_node;
2518 return 0;
2521 /* Prepare expr to be an argument of a TRUTH_NOT_EXPR,
2522 or validate its data type for an `if' or `while' statement or ?..: exp.
2524 This preparation consists of taking the ordinary
2525 representation of an expression expr and producing a valid tree
2526 boolean expression describing whether expr is nonzero. We could
2527 simply always do build_binary_op (NE_EXPR, expr, boolean_false_node, 1),
2528 but we optimize comparisons, &&, ||, and !.
2530 The resulting type should always be `boolean_type_node'. */
2532 tree
2533 truthvalue_conversion (expr)
2534 tree expr;
2536 if (TREE_CODE (expr) == ERROR_MARK)
2537 return expr;
2539 #if 0 /* This appears to be wrong for C++. */
2540 /* These really should return error_mark_node after 2.4 is stable.
2541 But not all callers handle ERROR_MARK properly. */
2542 switch (TREE_CODE (TREE_TYPE (expr)))
2544 case RECORD_TYPE:
2545 error ("struct type value used where scalar is required");
2546 return boolean_false_node;
2548 case UNION_TYPE:
2549 error ("union type value used where scalar is required");
2550 return boolean_false_node;
2552 case ARRAY_TYPE:
2553 error ("array type value used where scalar is required");
2554 return boolean_false_node;
2556 default:
2557 break;
2559 #endif /* 0 */
2561 switch (TREE_CODE (expr))
2563 case EQ_EXPR:
2564 case NE_EXPR: case LE_EXPR: case GE_EXPR: case LT_EXPR: case GT_EXPR:
2565 case TRUTH_ANDIF_EXPR:
2566 case TRUTH_ORIF_EXPR:
2567 case TRUTH_AND_EXPR:
2568 case TRUTH_OR_EXPR:
2569 case TRUTH_XOR_EXPR:
2570 case TRUTH_NOT_EXPR:
2571 TREE_TYPE (expr) = boolean_type_node;
2572 return expr;
2574 case ERROR_MARK:
2575 return expr;
2577 case INTEGER_CST:
2578 return integer_zerop (expr) ? boolean_false_node : boolean_true_node;
2580 case REAL_CST:
2581 return real_zerop (expr) ? boolean_false_node : boolean_true_node;
2583 case ADDR_EXPR:
2584 /* If we are taking the address of a external decl, it might be zero
2585 if it is weak, so we cannot optimize. */
2586 if (DECL_P (TREE_OPERAND (expr, 0))
2587 && DECL_EXTERNAL (TREE_OPERAND (expr, 0)))
2588 break;
2590 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)))
2591 return build (COMPOUND_EXPR, boolean_type_node,
2592 TREE_OPERAND (expr, 0), boolean_true_node);
2593 else
2594 return boolean_true_node;
2596 case COMPLEX_EXPR:
2597 return build_binary_op ((TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
2598 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2599 truthvalue_conversion (TREE_OPERAND (expr, 0)),
2600 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2603 case NEGATE_EXPR:
2604 case ABS_EXPR:
2605 case FLOAT_EXPR:
2606 case FFS_EXPR:
2607 /* These don't change whether an object is non-zero or zero. */
2608 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2610 case LROTATE_EXPR:
2611 case RROTATE_EXPR:
2612 /* These don't change whether an object is zero or non-zero, but
2613 we can't ignore them if their second arg has side-effects. */
2614 if (TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)))
2615 return build (COMPOUND_EXPR, boolean_type_node, TREE_OPERAND (expr, 1),
2616 truthvalue_conversion (TREE_OPERAND (expr, 0)));
2617 else
2618 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2620 case COND_EXPR:
2621 /* Distribute the conversion into the arms of a COND_EXPR. */
2622 return fold (build (COND_EXPR, boolean_type_node, TREE_OPERAND (expr, 0),
2623 truthvalue_conversion (TREE_OPERAND (expr, 1)),
2624 truthvalue_conversion (TREE_OPERAND (expr, 2))));
2626 case CONVERT_EXPR:
2627 /* Don't cancel the effect of a CONVERT_EXPR from a REFERENCE_TYPE,
2628 since that affects how `default_conversion' will behave. */
2629 if (TREE_CODE (TREE_TYPE (expr)) == REFERENCE_TYPE
2630 || TREE_CODE (TREE_TYPE (TREE_OPERAND (expr, 0))) == REFERENCE_TYPE)
2631 break;
2632 /* fall through... */
2633 case NOP_EXPR:
2634 /* If this is widening the argument, we can ignore it. */
2635 if (TYPE_PRECISION (TREE_TYPE (expr))
2636 >= TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (expr, 0))))
2637 return truthvalue_conversion (TREE_OPERAND (expr, 0));
2638 break;
2640 case MINUS_EXPR:
2641 /* With IEEE arithmetic, x - x may not equal 0, so we can't optimize
2642 this case. */
2643 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT
2644 && TREE_CODE (TREE_TYPE (expr)) == REAL_TYPE)
2645 break;
2646 /* fall through... */
2647 case BIT_XOR_EXPR:
2648 /* This and MINUS_EXPR can be changed into a comparison of the
2649 two objects. */
2650 if (TREE_TYPE (TREE_OPERAND (expr, 0))
2651 == TREE_TYPE (TREE_OPERAND (expr, 1)))
2652 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2653 TREE_OPERAND (expr, 1), 1);
2654 return build_binary_op (NE_EXPR, TREE_OPERAND (expr, 0),
2655 fold (build1 (NOP_EXPR,
2656 TREE_TYPE (TREE_OPERAND (expr, 0)),
2657 TREE_OPERAND (expr, 1))), 1);
2659 case BIT_AND_EXPR:
2660 if (integer_onep (TREE_OPERAND (expr, 1))
2661 && TREE_TYPE (expr) != boolean_type_node)
2662 /* Using convert here would cause infinite recursion. */
2663 return build1 (NOP_EXPR, boolean_type_node, expr);
2664 break;
2666 case MODIFY_EXPR:
2667 if (warn_parentheses && C_EXP_ORIGINAL_CODE (expr) == MODIFY_EXPR)
2668 warning ("suggest parentheses around assignment used as truth value");
2669 break;
2671 default:
2672 break;
2675 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
2677 tree tem = save_expr (expr);
2678 return (build_binary_op
2679 ((TREE_SIDE_EFFECTS (expr)
2680 ? TRUTH_OR_EXPR : TRUTH_ORIF_EXPR),
2681 truthvalue_conversion (build_unary_op (REALPART_EXPR, tem, 0)),
2682 truthvalue_conversion (build_unary_op (IMAGPART_EXPR, tem, 0)),
2683 0));
2686 return build_binary_op (NE_EXPR, expr, integer_zero_node, 1);
2689 static tree builtin_function_2 PARAMS ((const char *, const char *, tree, tree,
2690 int, enum built_in_class, int, int,
2691 int));
2693 /* Make a variant type in the proper way for C/C++, propagating qualifiers
2694 down to the element type of an array. */
2696 tree
2697 c_build_qualified_type (type, type_quals)
2698 tree type;
2699 int type_quals;
2701 /* A restrict-qualified pointer type must be a pointer to object or
2702 incomplete type. Note that the use of POINTER_TYPE_P also allows
2703 REFERENCE_TYPEs, which is appropriate for C++. Unfortunately,
2704 the C++ front-end also use POINTER_TYPE for pointer-to-member
2705 values, so even though it should be illegal to use `restrict'
2706 with such an entity we don't flag that here. Thus, special case
2707 code for that case is required in the C++ front-end. */
2708 if ((type_quals & TYPE_QUAL_RESTRICT)
2709 && (!POINTER_TYPE_P (type)
2710 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (type))))
2712 error ("invalid use of `restrict'");
2713 type_quals &= ~TYPE_QUAL_RESTRICT;
2716 if (TREE_CODE (type) == ARRAY_TYPE)
2717 return build_array_type (c_build_qualified_type (TREE_TYPE (type),
2718 type_quals),
2719 TYPE_DOMAIN (type));
2720 return build_qualified_type (type, type_quals);
2723 /* Apply the TYPE_QUALS to the new DECL. */
2725 void
2726 c_apply_type_quals_to_decl (type_quals, decl)
2727 int type_quals;
2728 tree decl;
2730 if ((type_quals & TYPE_QUAL_CONST)
2731 || (TREE_TYPE (decl)
2732 && TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE))
2733 TREE_READONLY (decl) = 1;
2734 if (type_quals & TYPE_QUAL_VOLATILE)
2736 TREE_SIDE_EFFECTS (decl) = 1;
2737 TREE_THIS_VOLATILE (decl) = 1;
2739 if (type_quals & TYPE_QUAL_RESTRICT)
2741 if (!TREE_TYPE (decl)
2742 || !POINTER_TYPE_P (TREE_TYPE (decl))
2743 || !C_TYPE_OBJECT_OR_INCOMPLETE_P (TREE_TYPE (TREE_TYPE (decl))))
2744 error ("invalid use of `restrict'");
2745 else if (flag_strict_aliasing)
2747 /* No two restricted pointers can point at the same thing.
2748 However, a restricted pointer can point at the same thing
2749 as an unrestricted pointer, if that unrestricted pointer
2750 is based on the restricted pointer. So, we make the
2751 alias set for the restricted pointer a subset of the
2752 alias set for the type pointed to by the type of the
2753 decl. */
2755 HOST_WIDE_INT pointed_to_alias_set
2756 = get_alias_set (TREE_TYPE (TREE_TYPE (decl)));
2758 if (pointed_to_alias_set == 0)
2759 /* It's not legal to make a subset of alias set zero. */
2761 else
2763 DECL_POINTER_ALIAS_SET (decl) = new_alias_set ();
2764 record_alias_subset (pointed_to_alias_set,
2765 DECL_POINTER_ALIAS_SET (decl));
2772 /* Return the typed-based alias set for T, which may be an expression
2773 or a type. Return -1 if we don't do anything special. */
2775 HOST_WIDE_INT
2776 lang_get_alias_set (t)
2777 tree t;
2779 tree u;
2781 /* We know nothing about vector types */
2782 if (TREE_CODE (t) == VECTOR_TYPE)
2783 return 0;
2785 /* Permit type-punning when accessing a union, provided the access
2786 is directly through the union. For example, this code does not
2787 permit taking the address of a union member and then storing
2788 through it. Even the type-punning allowed here is a GCC
2789 extension, albeit a common and useful one; the C standard says
2790 that such accesses have implementation-defined behavior. */
2791 for (u = t;
2792 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
2793 u = TREE_OPERAND (u, 0))
2794 if (TREE_CODE (u) == COMPONENT_REF
2795 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
2796 return 0;
2798 /* If this is a char *, the ANSI C standard says it can alias
2799 anything. Note that all references need do this. */
2800 if (TREE_CODE_CLASS (TREE_CODE (t)) == 'r'
2801 && TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
2802 && TYPE_PRECISION (TREE_TYPE (t)) == TYPE_PRECISION (char_type_node))
2803 return 0;
2805 /* That's all the expressions we handle specially. */
2806 if (! TYPE_P (t))
2807 return -1;
2809 /* The C standard specifically allows aliasing between signed and
2810 unsigned variants of the same type. We treat the signed
2811 variant as canonical. */
2812 if (TREE_CODE (t) == INTEGER_TYPE && TREE_UNSIGNED (t))
2814 tree t1 = signed_type (t);
2816 /* t1 == t can happen for boolean nodes which are always unsigned. */
2817 if (t1 != t)
2818 return get_alias_set (t1);
2820 else if (POINTER_TYPE_P (t))
2822 tree t1;
2824 /* Unfortunately, there is no canonical form of a pointer type.
2825 In particular, if we have `typedef int I', then `int *', and
2826 `I *' are different types. So, we have to pick a canonical
2827 representative. We do this below.
2829 Technically, this approach is actually more conservative that
2830 it needs to be. In particular, `const int *' and `int *'
2831 chould be in different alias sets, according to the C and C++
2832 standard, since their types are not the same, and so,
2833 technically, an `int **' and `const int **' cannot point at
2834 the same thing.
2836 But, the standard is wrong. In particular, this code is
2837 legal C++:
2839 int *ip;
2840 int **ipp = &ip;
2841 const int* const* cipp = &ip;
2843 And, it doesn't make sense for that to be legal unless you
2844 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
2845 the pointed-to types. This issue has been reported to the
2846 C++ committee. */
2847 t1 = build_type_no_quals (t);
2848 if (t1 != t)
2849 return get_alias_set (t1);
2851 /* It's not yet safe to use alias sets for classes in C++ because
2852 the TYPE_FIELDs list for a class doesn't mention base classes. */
2853 else if (c_language == clk_cplusplus && AGGREGATE_TYPE_P (t))
2854 return 0;
2856 return -1;
2859 /* Build tree nodes and builtin functions common to both C and C++ language
2860 frontends. */
2862 void
2863 c_common_nodes_and_builtins ()
2865 int wchar_type_size;
2866 tree array_domain_type;
2867 tree temp;
2868 tree memcpy_ftype, memset_ftype, strlen_ftype;
2869 tree bzero_ftype, bcmp_ftype, puts_ftype, printf_ftype;
2870 tree fputs_ftype, fputc_ftype, fwrite_ftype, fprintf_ftype;
2871 tree endlink, int_endlink, double_endlink, unsigned_endlink;
2872 tree cstring_endlink, sizetype_endlink;
2873 tree ptr_ftype, ptr_ftype_unsigned;
2874 tree void_ftype_any, void_ftype_int, int_ftype_any;
2875 tree double_ftype_double, double_ftype_double_double;
2876 tree float_ftype_float, ldouble_ftype_ldouble;
2877 tree cfloat_ftype_cfloat, cdouble_ftype_cdouble, cldouble_ftype_cldouble;
2878 tree float_ftype_cfloat, double_ftype_cdouble, ldouble_ftype_cldouble;
2879 tree int_ftype_cptr_cptr_sizet, sizet_ftype_cstring_cstring;
2880 tree int_ftype_cstring_cstring, string_ftype_string_cstring;
2881 tree string_ftype_cstring_int, string_ftype_cstring_cstring;
2882 tree string_ftype_string_cstring_sizet, int_ftype_cstring_cstring_sizet;
2883 tree long_ftype_long;
2884 tree longlong_ftype_longlong;
2885 tree intmax_ftype_intmax;
2886 /* Either char* or void*. */
2887 tree traditional_ptr_type_node;
2888 /* Either const char* or const void*. */
2889 tree traditional_cptr_type_node;
2890 tree traditional_len_type_node;
2891 tree traditional_len_endlink;
2892 tree va_list_ref_type_node;
2893 tree va_list_arg_type_node;
2895 /* Define `int' and `char' first so that dbx will output them first. */
2896 record_builtin_type (RID_INT, NULL_PTR, integer_type_node);
2897 record_builtin_type (RID_CHAR, "char", char_type_node);
2899 /* `signed' is the same as `int'. FIXME: the declarations of "signed",
2900 "unsigned long", "long long unsigned" and "unsigned short" were in C++
2901 but not C. Are the conditionals here needed? */
2902 if (c_language == clk_cplusplus)
2903 record_builtin_type (RID_SIGNED, NULL_PTR, integer_type_node);
2904 record_builtin_type (RID_LONG, "long int", long_integer_type_node);
2905 record_builtin_type (RID_UNSIGNED, "unsigned int", unsigned_type_node);
2906 record_builtin_type (RID_MAX, "long unsigned int",
2907 long_unsigned_type_node);
2908 if (c_language == clk_cplusplus)
2909 record_builtin_type (RID_MAX, "unsigned long", long_unsigned_type_node);
2910 record_builtin_type (RID_MAX, "long long int",
2911 long_long_integer_type_node);
2912 record_builtin_type (RID_MAX, "long long unsigned int",
2913 long_long_unsigned_type_node);
2914 if (c_language == clk_cplusplus)
2915 record_builtin_type (RID_MAX, "long long unsigned",
2916 long_long_unsigned_type_node);
2917 record_builtin_type (RID_SHORT, "short int", short_integer_type_node);
2918 record_builtin_type (RID_MAX, "short unsigned int",
2919 short_unsigned_type_node);
2920 if (c_language == clk_cplusplus)
2921 record_builtin_type (RID_MAX, "unsigned short",
2922 short_unsigned_type_node);
2924 /* Define both `signed char' and `unsigned char'. */
2925 record_builtin_type (RID_MAX, "signed char", signed_char_type_node);
2926 record_builtin_type (RID_MAX, "unsigned char", unsigned_char_type_node);
2928 /* These are types that type_for_size and type_for_mode use. */
2929 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intQI_type_node));
2930 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intHI_type_node));
2931 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intSI_type_node));
2932 pushdecl (build_decl (TYPE_DECL, NULL_TREE, intDI_type_node));
2933 #if HOST_BITS_PER_WIDE_INT >= 64
2934 pushdecl (build_decl (TYPE_DECL, get_identifier ("__int128_t"), intTI_type_node));
2935 #endif
2936 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intQI_type_node));
2937 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intHI_type_node));
2938 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intSI_type_node));
2939 pushdecl (build_decl (TYPE_DECL, NULL_TREE, unsigned_intDI_type_node));
2940 #if HOST_BITS_PER_WIDE_INT >= 64
2941 pushdecl (build_decl (TYPE_DECL, get_identifier ("__uint128_t"), unsigned_intTI_type_node));
2942 #endif
2944 /* Create the widest literal types. */
2945 widest_integer_literal_type_node
2946 = make_signed_type (HOST_BITS_PER_WIDE_INT * 2);
2947 pushdecl (build_decl (TYPE_DECL, NULL_TREE,
2948 widest_integer_literal_type_node));
2950 widest_unsigned_literal_type_node
2951 = make_unsigned_type (HOST_BITS_PER_WIDE_INT * 2);
2952 pushdecl (build_decl (TYPE_DECL, NULL_TREE,
2953 widest_unsigned_literal_type_node));
2955 /* `unsigned long' is the standard type for sizeof.
2956 Note that stddef.h uses `unsigned long',
2957 and this must agree, even if long and int are the same size. */
2958 c_size_type_node =
2959 TREE_TYPE (identifier_global_value (get_identifier (SIZE_TYPE)));
2960 signed_size_type_node = signed_type (c_size_type_node);
2961 if (flag_traditional)
2962 c_size_type_node = signed_size_type_node;
2963 set_sizetype (c_size_type_node);
2965 build_common_tree_nodes_2 (flag_short_double);
2967 record_builtin_type (RID_FLOAT, NULL_PTR, float_type_node);
2968 record_builtin_type (RID_DOUBLE, NULL_PTR, double_type_node);
2969 record_builtin_type (RID_MAX, "long double", long_double_type_node);
2971 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex int"),
2972 complex_integer_type_node));
2973 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex float"),
2974 complex_float_type_node));
2975 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex double"),
2976 complex_double_type_node));
2977 pushdecl (build_decl (TYPE_DECL, get_identifier ("complex long double"),
2978 complex_long_double_type_node));
2980 record_builtin_type (RID_VOID, NULL_PTR, void_type_node);
2982 void_list_node = build_void_list_node ();
2984 /* Make a type to be the domain of a few array types
2985 whose domains don't really matter.
2986 200 is small enough that it always fits in size_t
2987 and large enough that it can hold most function names for the
2988 initializations of __FUNCTION__ and __PRETTY_FUNCTION__. */
2989 array_domain_type = build_index_type (size_int (200));
2991 /* Make a type for arrays of characters.
2992 With luck nothing will ever really depend on the length of this
2993 array type. */
2994 char_array_type_node
2995 = build_array_type (char_type_node, array_domain_type);
2997 /* Likewise for arrays of ints. */
2998 int_array_type_node
2999 = build_array_type (integer_type_node, array_domain_type);
3001 #ifdef MD_INIT_BUILTINS
3002 MD_INIT_BUILTINS;
3003 #endif
3005 /* This is special for C++ so functions can be overloaded. */
3006 wchar_type_node = get_identifier (flag_short_wchar
3007 ? "short unsigned int"
3008 : WCHAR_TYPE);
3009 wchar_type_node = TREE_TYPE (identifier_global_value (wchar_type_node));
3010 wchar_type_size = TYPE_PRECISION (wchar_type_node);
3011 if (c_language == clk_cplusplus)
3013 if (TREE_UNSIGNED (wchar_type_node))
3014 wchar_type_node = make_unsigned_type (wchar_type_size);
3015 else
3016 wchar_type_node = make_signed_type (wchar_type_size);
3017 record_builtin_type (RID_WCHAR, "wchar_t", wchar_type_node);
3019 else
3021 signed_wchar_type_node = signed_type (wchar_type_node);
3022 unsigned_wchar_type_node = unsigned_type (wchar_type_node);
3025 /* This is for wide string constants. */
3026 wchar_array_type_node
3027 = build_array_type (wchar_type_node, array_domain_type);
3029 string_type_node = build_pointer_type (char_type_node);
3030 const_string_type_node
3031 = build_pointer_type (build_type_variant (char_type_node, 1, 0));
3033 wint_type_node =
3034 TREE_TYPE (identifier_global_value (get_identifier (WINT_TYPE)));
3036 intmax_type_node =
3037 TREE_TYPE (identifier_global_value (get_identifier (INTMAX_TYPE)));
3038 uintmax_type_node =
3039 TREE_TYPE (identifier_global_value (get_identifier (UINTMAX_TYPE)));
3041 default_function_type = build_function_type (integer_type_node, NULL_TREE);
3042 ptrdiff_type_node
3043 = TREE_TYPE (identifier_global_value (get_identifier (PTRDIFF_TYPE)));
3044 unsigned_ptrdiff_type_node = unsigned_type (ptrdiff_type_node);
3046 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_va_list"),
3047 va_list_type_node));
3049 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_ptrdiff_t"),
3050 ptrdiff_type_node));
3052 pushdecl (build_decl (TYPE_DECL, get_identifier ("__builtin_size_t"),
3053 sizetype));
3055 if (TREE_CODE (va_list_type_node) == ARRAY_TYPE)
3057 va_list_arg_type_node = va_list_ref_type_node =
3058 build_pointer_type (TREE_TYPE (va_list_type_node));
3060 else
3062 va_list_arg_type_node = va_list_type_node;
3063 va_list_ref_type_node = build_reference_type (va_list_type_node);
3066 endlink = void_list_node;
3067 int_endlink = tree_cons (NULL_TREE, integer_type_node, endlink);
3068 double_endlink = tree_cons (NULL_TREE, double_type_node, endlink);
3069 unsigned_endlink = tree_cons (NULL_TREE, unsigned_type_node, endlink);
3070 cstring_endlink = tree_cons (NULL_TREE, const_string_type_node, endlink);
3072 ptr_ftype = build_function_type (ptr_type_node, NULL_TREE);
3073 ptr_ftype_unsigned = build_function_type (ptr_type_node, unsigned_endlink);
3074 sizetype_endlink = tree_cons (NULL_TREE, TYPE_DOMAIN (sizetype), endlink);
3075 /* We realloc here because sizetype could be int or unsigned. S'ok. */
3076 ptr_ftype_sizetype = build_function_type (ptr_type_node, sizetype_endlink);
3078 int_ftype_any = build_function_type (integer_type_node, NULL_TREE);
3079 void_ftype_any = build_function_type (void_type_node, NULL_TREE);
3080 void_ftype = build_function_type (void_type_node, endlink);
3081 void_ftype_int = build_function_type (void_type_node, int_endlink);
3082 void_ftype_ptr
3083 = build_function_type (void_type_node,
3084 tree_cons (NULL_TREE, ptr_type_node, endlink));
3086 float_ftype_float
3087 = build_function_type (float_type_node,
3088 tree_cons (NULL_TREE, float_type_node, endlink));
3090 double_ftype_double
3091 = build_function_type (double_type_node, double_endlink);
3093 ldouble_ftype_ldouble
3094 = build_function_type (long_double_type_node,
3095 tree_cons (NULL_TREE, long_double_type_node,
3096 endlink));
3098 double_ftype_double_double
3099 = build_function_type (double_type_node,
3100 tree_cons (NULL_TREE, double_type_node,
3101 double_endlink));
3103 cfloat_ftype_cfloat
3104 = build_function_type (complex_float_type_node,
3105 tree_cons (NULL_TREE, complex_float_type_node,
3106 endlink));
3107 cdouble_ftype_cdouble
3108 = build_function_type (complex_double_type_node,
3109 tree_cons (NULL_TREE, complex_double_type_node,
3110 endlink));
3111 cldouble_ftype_cldouble
3112 = build_function_type (complex_long_double_type_node,
3113 tree_cons (NULL_TREE, complex_long_double_type_node,
3114 endlink));
3116 float_ftype_cfloat
3117 = build_function_type (float_type_node,
3118 tree_cons (NULL_TREE, complex_float_type_node,
3119 endlink));
3120 double_ftype_cdouble
3121 = build_function_type (double_type_node,
3122 tree_cons (NULL_TREE, complex_double_type_node,
3123 endlink));
3124 ldouble_ftype_cldouble
3125 = build_function_type (long_double_type_node,
3126 tree_cons (NULL_TREE, complex_long_double_type_node,
3127 endlink));
3129 int_ftype_int
3130 = build_function_type (integer_type_node, int_endlink);
3132 long_ftype_long
3133 = build_function_type (long_integer_type_node,
3134 tree_cons (NULL_TREE, long_integer_type_node,
3135 endlink));
3137 longlong_ftype_longlong
3138 = build_function_type (long_long_integer_type_node,
3139 tree_cons (NULL_TREE, long_long_integer_type_node,
3140 endlink));
3142 intmax_ftype_intmax
3143 = build_function_type (intmax_type_node,
3144 tree_cons (NULL_TREE, intmax_type_node,
3145 endlink));
3147 int_ftype_cptr_cptr_sizet
3148 = build_function_type (integer_type_node,
3149 tree_cons (NULL_TREE, const_ptr_type_node,
3150 tree_cons (NULL_TREE,
3151 const_ptr_type_node,
3152 sizetype_endlink)));
3154 void_zero_node = build_int_2 (0, 0);
3155 TREE_TYPE (void_zero_node) = void_type_node;
3157 /* Prototype for strcpy/strcat. */
3158 string_ftype_string_cstring
3159 = build_function_type (string_type_node,
3160 tree_cons (NULL_TREE, string_type_node,
3161 cstring_endlink));
3163 /* Prototype for strncpy/strncat. */
3164 string_ftype_string_cstring_sizet
3165 = build_function_type (string_type_node,
3166 tree_cons (NULL_TREE, string_type_node,
3167 tree_cons (NULL_TREE,
3168 const_string_type_node,
3169 sizetype_endlink)));
3171 traditional_len_type_node = ((flag_traditional &&
3172 c_language != clk_cplusplus)
3173 ? integer_type_node : sizetype);
3174 traditional_len_endlink = tree_cons (NULL_TREE, traditional_len_type_node,
3175 endlink);
3177 /* Prototype for strcmp. */
3178 int_ftype_cstring_cstring
3179 = build_function_type (integer_type_node,
3180 tree_cons (NULL_TREE, const_string_type_node,
3181 cstring_endlink));
3183 /* Prototype for strspn/strcspn. */
3184 sizet_ftype_cstring_cstring
3185 = build_function_type (c_size_type_node,
3186 tree_cons (NULL_TREE, const_string_type_node,
3187 cstring_endlink));
3189 /* Prototype for strncmp. */
3190 int_ftype_cstring_cstring_sizet
3191 = build_function_type (integer_type_node,
3192 tree_cons (NULL_TREE, const_string_type_node,
3193 tree_cons (NULL_TREE,
3194 const_string_type_node,
3195 sizetype_endlink)));
3197 /* Prototype for strstr, strpbrk, etc. */
3198 string_ftype_cstring_cstring
3199 = build_function_type (string_type_node,
3200 tree_cons (NULL_TREE, const_string_type_node,
3201 cstring_endlink));
3203 /* Prototype for strchr. */
3204 string_ftype_cstring_int
3205 = build_function_type (string_type_node,
3206 tree_cons (NULL_TREE, const_string_type_node,
3207 int_endlink));
3209 /* Prototype for strlen. */
3210 strlen_ftype
3211 = build_function_type (traditional_len_type_node, cstring_endlink);
3213 traditional_ptr_type_node = ((flag_traditional &&
3214 c_language != clk_cplusplus)
3215 ? string_type_node : ptr_type_node);
3216 traditional_cptr_type_node = ((flag_traditional &&
3217 c_language != clk_cplusplus)
3218 ? const_string_type_node : const_ptr_type_node);
3220 /* Prototype for memcpy. */
3221 memcpy_ftype
3222 = build_function_type (traditional_ptr_type_node,
3223 tree_cons (NULL_TREE, ptr_type_node,
3224 tree_cons (NULL_TREE, const_ptr_type_node,
3225 sizetype_endlink)));
3227 /* Prototype for memset. */
3228 memset_ftype
3229 = build_function_type (traditional_ptr_type_node,
3230 tree_cons (NULL_TREE, ptr_type_node,
3231 tree_cons (NULL_TREE, integer_type_node,
3232 sizetype_endlink)));
3234 /* Prototype for bzero. */
3235 bzero_ftype
3236 = build_function_type (void_type_node,
3237 tree_cons (NULL_TREE, traditional_ptr_type_node,
3238 traditional_len_endlink));
3240 /* Prototype for bcmp. */
3241 bcmp_ftype
3242 = build_function_type (integer_type_node,
3243 tree_cons (NULL_TREE, traditional_cptr_type_node,
3244 tree_cons (NULL_TREE,
3245 traditional_cptr_type_node,
3246 traditional_len_endlink)));
3248 /* Prototype for puts. */
3249 puts_ftype
3250 = build_function_type (integer_type_node, cstring_endlink);
3252 /* Prototype for printf. */
3253 printf_ftype
3254 = build_function_type (integer_type_node,
3255 tree_cons (NULL_TREE, const_string_type_node,
3256 NULL_TREE));
3258 /* These stdio prototypes are declared using void* in place of
3259 FILE*. They are only used for __builtin_ style calls, regular
3260 style builtin prototypes omit the arguments and merge those
3261 provided by stdio.h. */
3262 /* Prototype for fwrite. */
3263 fwrite_ftype
3264 = build_function_type (c_size_type_node,
3265 tree_cons (NULL_TREE, const_ptr_type_node,
3266 tree_cons (NULL_TREE, c_size_type_node,
3267 tree_cons (NULL_TREE, c_size_type_node,
3268 tree_cons (NULL_TREE, ptr_type_node, endlink)))));
3270 /* Prototype for fputc. */
3271 fputc_ftype
3272 = build_function_type (integer_type_node,
3273 tree_cons (NULL_TREE, integer_type_node,
3274 tree_cons (NULL_TREE, ptr_type_node, endlink)));
3276 /* Prototype for fputs. */
3277 fputs_ftype
3278 = build_function_type (integer_type_node,
3279 tree_cons (NULL_TREE, const_string_type_node,
3280 tree_cons (NULL_TREE, ptr_type_node, endlink)));
3282 /* Prototype for fprintf. */
3283 fprintf_ftype
3284 = build_function_type (integer_type_node,
3285 tree_cons (NULL_TREE, ptr_type_node,
3286 tree_cons (NULL_TREE,
3287 const_string_type_node,
3288 NULL_TREE)));
3290 builtin_function ("__builtin_constant_p", default_function_type,
3291 BUILT_IN_CONSTANT_P, BUILT_IN_NORMAL, NULL_PTR);
3293 builtin_function ("__builtin_return_address", ptr_ftype_unsigned,
3294 BUILT_IN_RETURN_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
3296 builtin_function ("__builtin_frame_address", ptr_ftype_unsigned,
3297 BUILT_IN_FRAME_ADDRESS, BUILT_IN_NORMAL, NULL_PTR);
3299 #ifdef EH_RETURN_DATA_REGNO
3300 builtin_function ("__builtin_eh_return_data_regno", int_ftype_int,
3301 BUILT_IN_EH_RETURN_DATA_REGNO, BUILT_IN_NORMAL, NULL_PTR);
3302 #endif
3304 builtin_function ("__builtin_alloca", ptr_ftype_sizetype,
3305 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, "alloca");
3306 builtin_function_2 ("__builtin_ffs", "ffs",
3307 int_ftype_int, int_ftype_int,
3308 BUILT_IN_FFS, BUILT_IN_NORMAL, 0, 1, 0);
3309 /* Define alloca as builtin, unless SMALL_STACK. */
3310 #ifndef SMALL_STACK
3311 builtin_function_2 (NULL_PTR, "alloca", NULL_TREE, ptr_ftype_sizetype,
3312 BUILT_IN_ALLOCA, BUILT_IN_NORMAL, 0, 1, 0);
3313 #endif
3314 /* Declare _exit and _Exit just to mark them as non-returning. */
3315 builtin_function_2 (NULL_PTR, "_exit", NULL_TREE, void_ftype_int,
3316 0, NOT_BUILT_IN, 0, 1, 1);
3317 builtin_function_2 (NULL_PTR, "_Exit", NULL_TREE, void_ftype_int,
3318 0, NOT_BUILT_IN, 0, !flag_isoc99, 1);
3320 builtin_function_2 ("__builtin_index", "index",
3321 string_ftype_cstring_int, string_ftype_cstring_int,
3322 BUILT_IN_INDEX, BUILT_IN_NORMAL, 1, 1, 0);
3323 builtin_function_2 ("__builtin_rindex", "rindex",
3324 string_ftype_cstring_int, string_ftype_cstring_int,
3325 BUILT_IN_RINDEX, BUILT_IN_NORMAL, 1, 1, 0);
3327 /* The system prototypes for these functions have many
3328 variations, so don't specify parameters to avoid conflicts.
3329 The expand_* functions check the argument types anyway. */
3330 builtin_function_2 ("__builtin_bzero", "bzero",
3331 bzero_ftype, void_ftype_any,
3332 BUILT_IN_BZERO, BUILT_IN_NORMAL, 1, 1, 0);
3333 builtin_function_2 ("__builtin_bcmp", "bcmp",
3334 bcmp_ftype, int_ftype_any,
3335 BUILT_IN_BCMP, BUILT_IN_NORMAL, 1, 1, 0);
3337 builtin_function_2 ("__builtin_abs", "abs",
3338 int_ftype_int, int_ftype_int,
3339 BUILT_IN_ABS, BUILT_IN_NORMAL, 0, 0, 0);
3340 builtin_function_2 ("__builtin_fabsf", "fabsf",
3341 float_ftype_float, float_ftype_float,
3342 BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0);
3343 builtin_function_2 ("__builtin_fabs", "fabs",
3344 double_ftype_double, double_ftype_double,
3345 BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0);
3346 builtin_function_2 ("__builtin_fabsl", "fabsl",
3347 ldouble_ftype_ldouble, ldouble_ftype_ldouble,
3348 BUILT_IN_FABS, BUILT_IN_NORMAL, 0, 0, 0);
3349 builtin_function_2 ("__builtin_labs", "labs",
3350 long_ftype_long, long_ftype_long,
3351 BUILT_IN_ABS, BUILT_IN_NORMAL, 0, 0, 0);
3352 builtin_function_2 ("__builtin_llabs", "llabs",
3353 longlong_ftype_longlong, longlong_ftype_longlong,
3354 BUILT_IN_ABS, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3355 builtin_function_2 ("__builtin_imaxabs", "imaxabs",
3356 intmax_ftype_intmax, intmax_ftype_intmax,
3357 BUILT_IN_ABS, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3359 builtin_function ("__builtin_saveregs", ptr_ftype, BUILT_IN_SAVEREGS,
3360 BUILT_IN_NORMAL, NULL_PTR);
3361 builtin_function ("__builtin_classify_type", default_function_type,
3362 BUILT_IN_CLASSIFY_TYPE, BUILT_IN_NORMAL, NULL_PTR);
3363 builtin_function ("__builtin_next_arg", ptr_ftype, BUILT_IN_NEXT_ARG,
3364 BUILT_IN_NORMAL, NULL_PTR);
3365 builtin_function ("__builtin_args_info", int_ftype_int, BUILT_IN_ARGS_INFO,
3366 BUILT_IN_NORMAL, NULL_PTR);
3367 builtin_function ("__builtin_setjmp",
3368 build_function_type (integer_type_node,
3369 tree_cons (NULL_TREE, ptr_type_node,
3370 endlink)),
3371 BUILT_IN_SETJMP, BUILT_IN_NORMAL, NULL_PTR);
3372 builtin_function ("__builtin_longjmp",
3373 build_function_type (void_type_node,
3374 tree_cons (NULL_TREE, ptr_type_node,
3375 int_endlink)),
3376 BUILT_IN_LONGJMP, BUILT_IN_NORMAL, NULL_PTR);
3377 builtin_function ("__builtin_trap", void_ftype, BUILT_IN_TRAP,
3378 BUILT_IN_NORMAL, NULL_PTR);
3380 /* ISO C99 IEEE Unordered compares. */
3381 builtin_function ("__builtin_isgreater", default_function_type,
3382 BUILT_IN_ISGREATER, BUILT_IN_NORMAL, NULL_PTR);
3383 builtin_function ("__builtin_isgreaterequal", default_function_type,
3384 BUILT_IN_ISGREATEREQUAL, BUILT_IN_NORMAL, NULL_PTR);
3385 builtin_function ("__builtin_isless", default_function_type,
3386 BUILT_IN_ISLESS, BUILT_IN_NORMAL, NULL_PTR);
3387 builtin_function ("__builtin_islessequal", default_function_type,
3388 BUILT_IN_ISLESSEQUAL, BUILT_IN_NORMAL, NULL_PTR);
3389 builtin_function ("__builtin_islessgreater", default_function_type,
3390 BUILT_IN_ISLESSGREATER, BUILT_IN_NORMAL, NULL_PTR);
3391 builtin_function ("__builtin_isunordered", default_function_type,
3392 BUILT_IN_ISUNORDERED, BUILT_IN_NORMAL, NULL_PTR);
3394 /* Untyped call and return. */
3395 builtin_function ("__builtin_apply_args", ptr_ftype,
3396 BUILT_IN_APPLY_ARGS, BUILT_IN_NORMAL, NULL_PTR);
3398 temp = tree_cons (NULL_TREE,
3399 build_pointer_type (build_function_type (void_type_node,
3400 NULL_TREE)),
3401 tree_cons (NULL_TREE, ptr_type_node, sizetype_endlink));
3402 builtin_function ("__builtin_apply",
3403 build_function_type (ptr_type_node, temp),
3404 BUILT_IN_APPLY, BUILT_IN_NORMAL, NULL_PTR);
3405 builtin_function ("__builtin_return", void_ftype_ptr,
3406 BUILT_IN_RETURN, BUILT_IN_NORMAL, NULL_PTR);
3408 /* Support for varargs.h and stdarg.h. */
3409 builtin_function ("__builtin_varargs_start",
3410 build_function_type (void_type_node,
3411 tree_cons (NULL_TREE,
3412 va_list_ref_type_node,
3413 endlink)),
3414 BUILT_IN_VARARGS_START, BUILT_IN_NORMAL, NULL_PTR);
3416 builtin_function ("__builtin_stdarg_start",
3417 build_function_type (void_type_node,
3418 tree_cons (NULL_TREE,
3419 va_list_ref_type_node,
3420 NULL_TREE)),
3421 BUILT_IN_STDARG_START, BUILT_IN_NORMAL, NULL_PTR);
3423 builtin_function ("__builtin_va_end",
3424 build_function_type (void_type_node,
3425 tree_cons (NULL_TREE,
3426 va_list_ref_type_node,
3427 endlink)),
3428 BUILT_IN_VA_END, BUILT_IN_NORMAL, NULL_PTR);
3430 builtin_function ("__builtin_va_copy",
3431 build_function_type (void_type_node,
3432 tree_cons (NULL_TREE,
3433 va_list_ref_type_node,
3434 tree_cons (NULL_TREE,
3435 va_list_arg_type_node,
3436 endlink))),
3437 BUILT_IN_VA_COPY, BUILT_IN_NORMAL, NULL_PTR);
3439 /* ??? Ought to be `T __builtin_expect(T, T)' for any type T. */
3440 builtin_function ("__builtin_expect",
3441 build_function_type (long_integer_type_node,
3442 tree_cons (NULL_TREE,
3443 long_integer_type_node,
3444 tree_cons (NULL_TREE,
3445 long_integer_type_node,
3446 endlink))),
3447 BUILT_IN_EXPECT, BUILT_IN_NORMAL, NULL_PTR);
3449 /* Currently under experimentation. */
3450 builtin_function_2 ("__builtin_memcpy", "memcpy",
3451 memcpy_ftype, memcpy_ftype,
3452 BUILT_IN_MEMCPY, BUILT_IN_NORMAL, 1, 0, 0);
3453 builtin_function_2 ("__builtin_memcmp", "memcmp",
3454 int_ftype_cptr_cptr_sizet, int_ftype_cptr_cptr_sizet,
3455 BUILT_IN_MEMCMP, BUILT_IN_NORMAL, 1, 0, 0);
3456 builtin_function_2 ("__builtin_memset", "memset",
3457 memset_ftype, memset_ftype,
3458 BUILT_IN_MEMSET, BUILT_IN_NORMAL, 1, 0, 0);
3459 built_in_decls[BUILT_IN_STRCMP] =
3460 builtin_function_2 ("__builtin_strcmp", "strcmp",
3461 int_ftype_cstring_cstring, int_ftype_cstring_cstring,
3462 BUILT_IN_STRCMP, BUILT_IN_NORMAL, 1, 0, 0);
3463 builtin_function_2 ("__builtin_strncmp", "strncmp",
3464 int_ftype_cstring_cstring_sizet,
3465 int_ftype_cstring_cstring_sizet,
3466 BUILT_IN_STRNCMP, BUILT_IN_NORMAL, 1, 0, 0);
3467 builtin_function_2 ("__builtin_strstr", "strstr",
3468 string_ftype_cstring_cstring, string_ftype_cstring_cstring,
3469 BUILT_IN_STRSTR, BUILT_IN_NORMAL, 1, 0, 0);
3470 builtin_function_2 ("__builtin_strpbrk", "strpbrk",
3471 string_ftype_cstring_cstring, string_ftype_cstring_cstring,
3472 BUILT_IN_STRPBRK, BUILT_IN_NORMAL, 1, 0, 0);
3473 built_in_decls[BUILT_IN_STRCHR] =
3474 builtin_function_2 ("__builtin_strchr", "strchr",
3475 string_ftype_cstring_int, string_ftype_cstring_int,
3476 BUILT_IN_STRCHR, BUILT_IN_NORMAL, 1, 0, 0);
3477 builtin_function_2 ("__builtin_strrchr", "strrchr",
3478 string_ftype_cstring_int, string_ftype_cstring_int,
3479 BUILT_IN_STRRCHR, BUILT_IN_NORMAL, 1, 0, 0);
3480 builtin_function_2 ("__builtin_strcpy", "strcpy",
3481 string_ftype_string_cstring, string_ftype_string_cstring,
3482 BUILT_IN_STRCPY, BUILT_IN_NORMAL, 1, 0, 0);
3483 builtin_function_2 ("__builtin_strncpy", "strncpy",
3484 string_ftype_string_cstring_sizet,
3485 string_ftype_string_cstring_sizet,
3486 BUILT_IN_STRNCPY, BUILT_IN_NORMAL, 1, 0, 0);
3487 built_in_decls[BUILT_IN_STRCAT] =
3488 builtin_function_2 ("__builtin_strcat", "strcat",
3489 string_ftype_string_cstring,
3490 string_ftype_string_cstring,
3491 BUILT_IN_STRCAT, BUILT_IN_NORMAL, 1, 0, 0);
3492 builtin_function_2 ("__builtin_strncat", "strncat",
3493 string_ftype_string_cstring_sizet,
3494 string_ftype_string_cstring_sizet,
3495 BUILT_IN_STRNCAT, BUILT_IN_NORMAL, 1, 0, 0);
3496 builtin_function_2 ("__builtin_strspn", "strspn",
3497 sizet_ftype_cstring_cstring, sizet_ftype_cstring_cstring,
3498 BUILT_IN_STRSPN, BUILT_IN_NORMAL, 1, 0, 0);
3499 builtin_function_2 ("__builtin_strcspn", "strcspn",
3500 sizet_ftype_cstring_cstring, sizet_ftype_cstring_cstring,
3501 BUILT_IN_STRCSPN, BUILT_IN_NORMAL, 1, 0, 0);
3502 built_in_decls[BUILT_IN_STRLEN] =
3503 builtin_function_2 ("__builtin_strlen", "strlen",
3504 strlen_ftype, strlen_ftype,
3505 BUILT_IN_STRLEN, BUILT_IN_NORMAL, 1, 0, 0);
3507 builtin_function_2 ("__builtin_sqrtf", "sqrtf",
3508 float_ftype_float, float_ftype_float,
3509 BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0);
3510 builtin_function_2 ("__builtin_fsqrt", "sqrt",
3511 double_ftype_double, double_ftype_double,
3512 BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0);
3513 builtin_function_2 ("__builtin_sqrtl", "sqrtl",
3514 ldouble_ftype_ldouble, ldouble_ftype_ldouble,
3515 BUILT_IN_FSQRT, BUILT_IN_NORMAL, 1, 0, 0);
3516 builtin_function_2 ("__builtin_sinf", "sinf",
3517 float_ftype_float, float_ftype_float,
3518 BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0);
3519 builtin_function_2 ("__builtin_sin", "sin",
3520 double_ftype_double, double_ftype_double,
3521 BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0);
3522 builtin_function_2 ("__builtin_sinl", "sinl",
3523 ldouble_ftype_ldouble, ldouble_ftype_ldouble,
3524 BUILT_IN_SIN, BUILT_IN_NORMAL, 1, 0, 0);
3525 builtin_function_2 ("__builtin_cosf", "cosf",
3526 float_ftype_float, float_ftype_float,
3527 BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0);
3528 builtin_function_2 ("__builtin_cos", "cos",
3529 double_ftype_double, double_ftype_double,
3530 BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0);
3531 builtin_function_2 ("__builtin_cosl", "cosl",
3532 ldouble_ftype_ldouble, ldouble_ftype_ldouble,
3533 BUILT_IN_COS, BUILT_IN_NORMAL, 1, 0, 0);
3535 /* ISO C99 complex arithmetic functions. */
3536 builtin_function_2 ("__builtin_conjf", "conjf",
3537 cfloat_ftype_cfloat, cfloat_ftype_cfloat,
3538 BUILT_IN_CONJ, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3539 builtin_function_2 ("__builtin_conj", "conj",
3540 cdouble_ftype_cdouble, cdouble_ftype_cdouble,
3541 BUILT_IN_CONJ, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3542 builtin_function_2 ("__builtin_conjl", "conjl",
3543 cldouble_ftype_cldouble, cldouble_ftype_cldouble,
3544 BUILT_IN_CONJ, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3545 builtin_function_2 ("__builtin_crealf", "crealf",
3546 float_ftype_cfloat, float_ftype_cfloat,
3547 BUILT_IN_CREAL, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3548 builtin_function_2 ("__builtin_creal", "creal",
3549 double_ftype_cdouble, double_ftype_cdouble,
3550 BUILT_IN_CREAL, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3551 builtin_function_2 ("__builtin_creall", "creall",
3552 ldouble_ftype_cldouble, ldouble_ftype_cldouble,
3553 BUILT_IN_CREAL, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3554 builtin_function_2 ("__builtin_cimagf", "cimagf",
3555 float_ftype_cfloat, float_ftype_cfloat,
3556 BUILT_IN_CIMAG, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3557 builtin_function_2 ("__builtin_cimag", "cimag",
3558 double_ftype_cdouble, double_ftype_cdouble,
3559 BUILT_IN_CIMAG, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3560 builtin_function_2 ("__builtin_cimagl", "cimagl",
3561 ldouble_ftype_cldouble, ldouble_ftype_cldouble,
3562 BUILT_IN_CIMAG, BUILT_IN_NORMAL, 0, !flag_isoc99, 0);
3564 built_in_decls[BUILT_IN_PUTCHAR] =
3565 builtin_function ("__builtin_putchar", int_ftype_int,
3566 BUILT_IN_PUTCHAR, BUILT_IN_NORMAL, "putchar");
3567 built_in_decls[BUILT_IN_PUTS] =
3568 builtin_function ("__builtin_puts", puts_ftype,
3569 BUILT_IN_PUTS, BUILT_IN_NORMAL, "puts");
3570 builtin_function_2 ("__builtin_printf", "printf",
3571 printf_ftype, printf_ftype,
3572 BUILT_IN_PRINTF, BUILT_IN_FRONTEND, 1, 0, 0);
3573 builtin_function_2 ("__builtin_fprintf", "fprintf",
3574 fprintf_ftype, fprintf_ftype,
3575 BUILT_IN_FPRINTF, BUILT_IN_FRONTEND, 1, 0, 0);
3576 built_in_decls[BUILT_IN_FWRITE] =
3577 builtin_function ("__builtin_fwrite", fwrite_ftype,
3578 BUILT_IN_FWRITE, BUILT_IN_NORMAL, "fwrite");
3579 built_in_decls[BUILT_IN_FPUTC] =
3580 builtin_function ("__builtin_fputc", fputc_ftype,
3581 BUILT_IN_FPUTC, BUILT_IN_NORMAL, "fputc");
3582 /* Declare the __builtin_ style with arguments and the regular style
3583 without them. We rely on stdio.h to supply the arguments for the
3584 regular style declaration since we had to use void* instead of
3585 FILE* in the __builtin_ prototype supplied here. */
3586 built_in_decls[BUILT_IN_FPUTS] =
3587 builtin_function_2 ("__builtin_fputs", "fputs",
3588 fputs_ftype, int_ftype_any,
3589 BUILT_IN_FPUTS, BUILT_IN_NORMAL, 1, 0, 0);
3591 /* Declare these functions non-returning
3592 to avoid spurious "control drops through" warnings. */
3593 builtin_function_2 (NULL_PTR, "abort",
3594 NULL_TREE, ((c_language == clk_cplusplus)
3595 ? void_ftype : void_ftype_any),
3596 0, NOT_BUILT_IN, 0, 0, 1);
3598 builtin_function_2 (NULL_PTR, "exit",
3599 NULL_TREE, ((c_language == clk_cplusplus)
3600 ? void_ftype_int : void_ftype_any),
3601 0, NOT_BUILT_IN, 0, 0, 1);
3603 #if 0
3604 /* Support for these has not been written in either expand_builtin
3605 or build_function_call. */
3606 builtin_function ("__builtin_div", default_ftype, BUILT_IN_DIV,
3607 BUILT_IN_NORMAL, NULL_PTR);
3608 builtin_function ("__builtin_ldiv", default_ftype, BUILT_IN_LDIV,
3609 BUILT_IN_NORMAL, NULL_PTR);
3610 builtin_function ("__builtin_ffloor", double_ftype_double, BUILT_IN_FFLOOR,
3611 BUILT_IN_NORMAL, NULL_PTR);
3612 builtin_function ("__builtin_fceil", double_ftype_double, BUILT_IN_FCEIL,
3613 BUILT_IN_NORMAL, NULL_PTR);
3614 builtin_function ("__builtin_fmod", double_ftype_double_double,
3615 BUILT_IN_FMOD, BUILT_IN_NORMAL, NULL_PTR);
3616 builtin_function ("__builtin_frem", double_ftype_double_double,
3617 BUILT_IN_FREM, BUILT_IN_NORMAL, NULL_PTR);
3618 builtin_function ("__builtin_getexp", double_ftype_double, BUILT_IN_GETEXP,
3619 BUILT_IN_NORMAL, NULL_PTR);
3620 builtin_function ("__builtin_getman", double_ftype_double, BUILT_IN_GETMAN,
3621 BUILT_IN_NORMAL, NULL_PTR);
3622 #endif
3624 main_identifier_node = get_identifier ("main");
3626 /* ??? Perhaps there's a better place to do this. But it is related
3627 to __builtin_va_arg, so it isn't that off-the-wall. */
3628 lang_type_promotes_to = simple_type_promotes_to;
3631 tree
3632 build_va_arg (expr, type)
3633 tree expr, type;
3635 return build1 (VA_ARG_EXPR, type, expr);
3639 /* Possibly define a builtin function with one or two names. BUILTIN_NAME
3640 is an __builtin_-prefixed name; NAME is the ordinary name; one or both
3641 of these may be NULL (though both being NULL is useless).
3642 BUILTIN_TYPE is the type of the __builtin_-prefixed function;
3643 TYPE is the type of the function with the ordinary name. These
3644 may differ if the ordinary name is declared with a looser type to avoid
3645 conflicts with headers. FUNCTION_CODE and CLASS are as for
3646 builtin_function. If LIBRARY_NAME_P is nonzero, NAME is passed as
3647 the LIBRARY_NAME parameter to builtin_function when declaring BUILTIN_NAME.
3648 If NONANSI_P is nonzero, the name NAME is treated as a non-ANSI name; if
3649 NORETURN_P is nonzero, the function is marked as non-returning.
3650 Returns the declaration of BUILTIN_NAME, if any, otherwise
3651 the declaration of NAME. Does not declare NAME if flag_no_builtin,
3652 or if NONANSI_P and flag_no_nonansi_builtin. */
3654 static tree
3655 builtin_function_2 (builtin_name, name, builtin_type, type, function_code,
3656 class, library_name_p, nonansi_p, noreturn_p)
3657 const char *builtin_name;
3658 const char *name;
3659 tree builtin_type;
3660 tree type;
3661 int function_code;
3662 enum built_in_class class;
3663 int library_name_p;
3664 int nonansi_p;
3665 int noreturn_p;
3667 tree bdecl = NULL_TREE;
3668 tree decl = NULL_TREE;
3669 if (builtin_name != 0)
3671 bdecl = builtin_function (builtin_name, builtin_type, function_code,
3672 class, library_name_p ? name : NULL_PTR);
3673 if (noreturn_p)
3675 TREE_THIS_VOLATILE (bdecl) = 1;
3676 TREE_SIDE_EFFECTS (bdecl) = 1;
3679 if (name != 0 && !flag_no_builtin && !(nonansi_p && flag_no_nonansi_builtin))
3681 decl = builtin_function (name, type, function_code, class, NULL_PTR);
3682 if (nonansi_p)
3683 DECL_BUILT_IN_NONANSI (decl) = 1;
3684 if (noreturn_p)
3686 TREE_THIS_VOLATILE (decl) = 1;
3687 TREE_SIDE_EFFECTS (decl) = 1;
3690 return (bdecl != 0 ? bdecl : decl);
3693 /* Given a type, apply default promotions wrt unnamed function arguments
3694 and return the new type. Return NULL_TREE if no change. */
3695 /* ??? There is a function of the same name in the C++ front end that
3696 does something similar, but is more thorough and does not return NULL
3697 if no change. We could perhaps share code, but it would make the
3698 self_promoting_type property harder to identify. */
3700 tree
3701 simple_type_promotes_to (type)
3702 tree type;
3704 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3705 return double_type_node;
3707 if (C_PROMOTING_INTEGER_TYPE_P (type))
3709 /* Traditionally, unsignedness is preserved in default promotions.
3710 Also preserve unsignedness if not really getting any wider. */
3711 if (TREE_UNSIGNED (type)
3712 && (flag_traditional
3713 || TYPE_PRECISION (type) == TYPE_PRECISION (integer_type_node)))
3714 return unsigned_type_node;
3715 return integer_type_node;
3718 return NULL_TREE;
3721 /* Return 1 if PARMS specifies a fixed number of parameters
3722 and none of their types is affected by default promotions. */
3725 self_promoting_args_p (parms)
3726 tree parms;
3728 register tree t;
3729 for (t = parms; t; t = TREE_CHAIN (t))
3731 register tree type = TREE_VALUE (t);
3733 if (TREE_CHAIN (t) == 0 && type != void_type_node)
3734 return 0;
3736 if (type == 0)
3737 return 0;
3739 if (TYPE_MAIN_VARIANT (type) == float_type_node)
3740 return 0;
3742 if (C_PROMOTING_INTEGER_TYPE_P (type))
3743 return 0;
3745 return 1;
3748 /* Recursively examines the array elements of TYPE, until a non-array
3749 element type is found. */
3751 tree
3752 strip_array_types (type)
3753 tree type;
3755 while (TREE_CODE (type) == ARRAY_TYPE)
3756 type = TREE_TYPE (type);
3758 return type;
3761 /* Recognize certain built-in functions so we can make tree-codes
3762 other than CALL_EXPR. We do this when it enables fold-const.c
3763 to do something useful. */
3764 /* ??? By rights this should go in builtins.c, but only C and C++
3765 implement build_{binary,unary}_op. Not exactly sure what bits
3766 of functionality are actually needed from those functions, or
3767 where the similar functionality exists in the other front ends. */
3769 tree
3770 expand_tree_builtin (function, params, coerced_params)
3771 tree function, params, coerced_params;
3773 enum tree_code code;
3775 if (DECL_BUILT_IN_CLASS (function) != BUILT_IN_NORMAL)
3776 return NULL_TREE;
3778 switch (DECL_FUNCTION_CODE (function))
3780 case BUILT_IN_ABS:
3781 case BUILT_IN_FABS:
3782 if (coerced_params == 0)
3783 return integer_zero_node;
3784 return build_unary_op (ABS_EXPR, TREE_VALUE (coerced_params), 0);
3786 case BUILT_IN_CONJ:
3787 if (coerced_params == 0)
3788 return integer_zero_node;
3789 return build_unary_op (CONJ_EXPR, TREE_VALUE (coerced_params), 0);
3791 case BUILT_IN_CREAL:
3792 if (coerced_params == 0)
3793 return integer_zero_node;
3794 return build_unary_op (REALPART_EXPR, TREE_VALUE (coerced_params), 0);
3796 case BUILT_IN_CIMAG:
3797 if (coerced_params == 0)
3798 return integer_zero_node;
3799 return build_unary_op (IMAGPART_EXPR, TREE_VALUE (coerced_params), 0);
3801 case BUILT_IN_ISGREATER:
3802 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3803 code = UNLE_EXPR;
3804 else
3805 code = LE_EXPR;
3806 goto unordered_cmp;
3808 case BUILT_IN_ISGREATEREQUAL:
3809 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3810 code = UNLT_EXPR;
3811 else
3812 code = LT_EXPR;
3813 goto unordered_cmp;
3815 case BUILT_IN_ISLESS:
3816 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3817 code = UNGE_EXPR;
3818 else
3819 code = GE_EXPR;
3820 goto unordered_cmp;
3822 case BUILT_IN_ISLESSEQUAL:
3823 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3824 code = UNGT_EXPR;
3825 else
3826 code = GT_EXPR;
3827 goto unordered_cmp;
3829 case BUILT_IN_ISLESSGREATER:
3830 if (TARGET_FLOAT_FORMAT == IEEE_FLOAT_FORMAT)
3831 code = UNEQ_EXPR;
3832 else
3833 code = EQ_EXPR;
3834 goto unordered_cmp;
3836 case BUILT_IN_ISUNORDERED:
3837 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT)
3838 return integer_zero_node;
3839 code = UNORDERED_EXPR;
3840 goto unordered_cmp;
3842 unordered_cmp:
3844 tree arg0, arg1;
3846 if (params == 0
3847 || TREE_CHAIN (params) == 0)
3849 error ("too few arguments to function `%s'",
3850 IDENTIFIER_POINTER (DECL_NAME (function)));
3851 return error_mark_node;
3853 else if (TREE_CHAIN (TREE_CHAIN (params)) != 0)
3855 error ("too many arguments to function `%s'",
3856 IDENTIFIER_POINTER (DECL_NAME (function)));
3857 return error_mark_node;
3860 arg0 = TREE_VALUE (params);
3861 arg1 = TREE_VALUE (TREE_CHAIN (params));
3862 arg0 = build_binary_op (code, arg0, arg1, 0);
3863 if (code != UNORDERED_EXPR)
3864 arg0 = build_unary_op (TRUTH_NOT_EXPR, arg0, 0);
3865 return arg0;
3867 break;
3869 default:
3870 break;
3873 return NULL_TREE;
3876 /* Returns non-zero if CODE is the code for a statement. */
3879 statement_code_p (code)
3880 enum tree_code code;
3882 switch (code)
3884 case EXPR_STMT:
3885 case COMPOUND_STMT:
3886 case DECL_STMT:
3887 case IF_STMT:
3888 case FOR_STMT:
3889 case WHILE_STMT:
3890 case DO_STMT:
3891 case RETURN_STMT:
3892 case BREAK_STMT:
3893 case CONTINUE_STMT:
3894 case SCOPE_STMT:
3895 case SWITCH_STMT:
3896 case GOTO_STMT:
3897 case LABEL_STMT:
3898 case ASM_STMT:
3899 case CASE_LABEL:
3900 return 1;
3902 default:
3903 if (lang_statement_code_p)
3904 return (*lang_statement_code_p) (code);
3905 return 0;
3909 /* Walk the statement tree, rooted at *tp. Apply FUNC to all the
3910 sub-trees of *TP in a pre-order traversal. FUNC is called with the
3911 DATA and the address of each sub-tree. If FUNC returns a non-NULL
3912 value, the traversal is aborted, and the value returned by FUNC is
3913 returned. If FUNC sets WALK_SUBTREES to zero, then the subtrees of
3914 the node being visited are not walked.
3916 We don't need a without_duplicates variant of this one because the
3917 statement tree is a tree, not a graph. */
3919 tree
3920 walk_stmt_tree (tp, func, data)
3921 tree *tp;
3922 walk_tree_fn func;
3923 void *data;
3925 enum tree_code code;
3926 int walk_subtrees;
3927 tree result;
3928 int i, len;
3930 #define WALK_SUBTREE(NODE) \
3931 do \
3933 result = walk_stmt_tree (&(NODE), func, data); \
3934 if (result) \
3935 return result; \
3937 while (0)
3939 /* Skip empty subtrees. */
3940 if (!*tp)
3941 return NULL_TREE;
3943 /* Skip subtrees below non-statement nodes. */
3944 if (!statement_code_p (TREE_CODE (*tp)))
3945 return NULL_TREE;
3947 /* Call the function. */
3948 walk_subtrees = 1;
3949 result = (*func) (tp, &walk_subtrees, data);
3951 /* If we found something, return it. */
3952 if (result)
3953 return result;
3955 /* FUNC may have modified the tree, recheck that we're looking at a
3956 statement node. */
3957 code = TREE_CODE (*tp);
3958 if (!statement_code_p (code))
3959 return NULL_TREE;
3961 /* Visit the subtrees unless FUNC decided that there was nothing
3962 interesting below this point in the tree. */
3963 if (walk_subtrees)
3965 /* Walk over all the sub-trees of this operand. Statement nodes
3966 never contain RTL, and we needn't worry about TARGET_EXPRs. */
3967 len = TREE_CODE_LENGTH (code);
3969 /* Go through the subtrees. We need to do this in forward order so
3970 that the scope of a FOR_EXPR is handled properly. */
3971 for (i = 0; i < len; ++i)
3972 WALK_SUBTREE (TREE_OPERAND (*tp, i));
3975 /* Finally visit the chain. This can be tail-recursion optimized if
3976 we write it this way. */
3977 return walk_stmt_tree (&TREE_CHAIN (*tp), func, data);
3979 #undef WALK_SUBTREE
3982 /* Used to compare case labels. K1 and K2 are actually tree nodes
3983 representing case labels, or NULL_TREE for a `default' label.
3984 Returns -1 if K1 is ordered before K2, -1 if K1 is ordered after
3985 K2, and 0 if K1 and K2 are equal. */
3988 case_compare (k1, k2)
3989 splay_tree_key k1;
3990 splay_tree_key k2;
3992 /* Consider a NULL key (such as arises with a `default' label) to be
3993 smaller than anything else. */
3994 if (!k1)
3995 return k2 ? -1 : 0;
3996 else if (!k2)
3997 return k1 ? 1 : 0;
3999 return tree_int_cst_compare ((tree) k1, (tree) k2);
4002 /* Process a case label for the range LOW_VALUE ... HIGH_VALUE. If
4003 LOW_VALUE and HIGH_VALUE are both NULL_TREE then this case label is
4004 actually a `default' label. If only HIGH_VALUE is NULL_TREE, then
4005 case label was declared using the usual C/C++ syntax, rather than
4006 the GNU case range extension. CASES is a tree containing all the
4007 case ranges processed so far; COND is the condition for the
4008 switch-statement itself. Returns the CASE_LABEL created, or
4009 ERROR_MARK_NODE if no CASE_LABEL is created. */
4011 tree
4012 c_add_case_label (cases, cond, low_value, high_value)
4013 splay_tree cases;
4014 tree cond;
4015 tree low_value;
4016 tree high_value;
4018 tree type;
4019 tree label;
4020 tree case_label;
4021 splay_tree_node node;
4023 /* Create the LABEL_DECL itself. */
4024 label = build_decl (LABEL_DECL, NULL_TREE, NULL_TREE);
4025 DECL_CONTEXT (label) = current_function_decl;
4027 /* If there was an error processing the switch condition, bail now
4028 before we get more confused. */
4029 if (!cond || cond == error_mark_node)
4031 /* Add a label anyhow so that the back-end doesn't think that
4032 the beginning of the switch is unreachable. */
4033 if (!cases->root)
4034 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4035 return error_mark_node;
4038 if ((low_value && TREE_TYPE (low_value)
4039 && POINTER_TYPE_P (TREE_TYPE (low_value)))
4040 || (high_value && TREE_TYPE (high_value)
4041 && POINTER_TYPE_P (TREE_TYPE (high_value))))
4042 error ("pointers are not permitted as case values");
4044 /* Case ranges are a GNU extension. */
4045 if (high_value && pedantic)
4047 if (c_language == clk_cplusplus)
4048 pedwarn ("ISO C++ forbids range expressions in switch statements");
4049 else
4050 pedwarn ("ISO C forbids range expressions in switch statements");
4053 type = TREE_TYPE (cond);
4054 if (low_value)
4056 low_value = check_case_value (low_value);
4057 low_value = convert_and_check (type, low_value);
4059 if (high_value)
4061 high_value = check_case_value (high_value);
4062 high_value = convert_and_check (type, high_value);
4065 /* If an error has occurred, bail out now. */
4066 if (low_value == error_mark_node || high_value == error_mark_node)
4068 if (!cases->root)
4069 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4070 return error_mark_node;
4073 /* If the LOW_VALUE and HIGH_VALUE are the same, then this isn't
4074 really a case range, even though it was written that way. Remove
4075 the HIGH_VALUE to simplify later processing. */
4076 if (tree_int_cst_equal (low_value, high_value))
4077 high_value = NULL_TREE;
4078 if (low_value && high_value
4079 && !tree_int_cst_lt (low_value, high_value))
4080 warning ("empty range specified");
4082 /* Look up the LOW_VALUE in the table of case labels we already
4083 have. */
4084 node = splay_tree_lookup (cases, (splay_tree_key) low_value);
4085 /* If there was not an exact match, check for overlapping ranges.
4086 There's no need to do this if there's no LOW_VALUE or HIGH_VALUE;
4087 that's a `default' label and the only overlap is an exact match. */
4088 if (!node && (low_value || high_value))
4090 splay_tree_node low_bound;
4091 splay_tree_node high_bound;
4093 /* Even though there wasn't an exact match, there might be an
4094 overlap between this case range and another case range.
4095 Since we've (inductively) not allowed any overlapping case
4096 ranges, we simply need to find the greatest low case label
4097 that is smaller that LOW_VALUE, and the smallest low case
4098 label that is greater than LOW_VALUE. If there is an overlap
4099 it will occur in one of these two ranges. */
4100 low_bound = splay_tree_predecessor (cases,
4101 (splay_tree_key) low_value);
4102 high_bound = splay_tree_successor (cases,
4103 (splay_tree_key) low_value);
4105 /* Check to see if the LOW_BOUND overlaps. It is smaller than
4106 the LOW_VALUE, so there is no need to check unless the
4107 LOW_BOUND is in fact itself a case range. */
4108 if (low_bound
4109 && CASE_HIGH ((tree) low_bound->value)
4110 && tree_int_cst_compare (CASE_HIGH ((tree) low_bound->value),
4111 low_value) >= 0)
4112 node = low_bound;
4113 /* Check to see if the HIGH_BOUND overlaps. The low end of that
4114 range is bigger than the low end of the current range, so we
4115 are only interested if the current range is a real range, and
4116 not an ordinary case label. */
4117 else if (high_bound
4118 && high_value
4119 && (tree_int_cst_compare ((tree) high_bound->key,
4120 high_value)
4121 <= 0))
4122 node = high_bound;
4124 /* If there was an overlap, issue an error. */
4125 if (node)
4127 tree duplicate = CASE_LABEL_DECL ((tree) node->value);
4129 if (high_value)
4131 error ("duplicate (or overlapping) case value");
4132 error_with_decl (duplicate,
4133 "this is the first entry overlapping that value");
4135 else if (low_value)
4137 error ("duplicate case value") ;
4138 error_with_decl (duplicate, "previously used here");
4140 else
4142 error ("multiple default labels in one switch");
4143 error_with_decl (duplicate, "this is the first default label");
4145 if (!cases->root)
4146 add_stmt (build_case_label (NULL_TREE, NULL_TREE, label));
4149 /* Add a CASE_LABEL to the statement-tree. */
4150 case_label = add_stmt (build_case_label (low_value, high_value, label));
4151 /* Register this case label in the splay tree. */
4152 splay_tree_insert (cases,
4153 (splay_tree_key) low_value,
4154 (splay_tree_value) case_label);
4156 return case_label;
4159 /* Mark P (a stmt_tree) for GC. The use of a `void *' for the
4160 parameter allows this function to be used as a GC-marking
4161 function. */
4163 void
4164 mark_stmt_tree (p)
4165 void *p;
4167 stmt_tree st = (stmt_tree) p;
4169 ggc_mark_tree (st->x_last_stmt);
4170 ggc_mark_tree (st->x_last_expr_type);
4173 /* Mark LD for GC. */
4175 void
4176 c_mark_lang_decl (c)
4177 struct c_lang_decl *c;
4179 ggc_mark_tree (c->saved_tree);
4182 /* Mark F for GC. */
4184 void
4185 mark_c_language_function (f)
4186 struct language_function *f;
4188 if (!f)
4189 return;
4191 mark_stmt_tree (&f->x_stmt_tree);
4192 ggc_mark_tree (f->x_scope_stmt_stack);
4195 /* Hook used by expand_expr to expand language-specific tree codes. */
4198 c_expand_expr (exp, target, tmode, modifier)
4199 tree exp;
4200 rtx target;
4201 enum machine_mode tmode;
4202 enum expand_modifier modifier;
4204 switch (TREE_CODE (exp))
4206 case STMT_EXPR:
4208 tree rtl_expr;
4209 rtx result;
4211 /* Since expand_expr_stmt calls free_temp_slots after every
4212 expression statement, we must call push_temp_slots here.
4213 Otherwise, any temporaries in use now would be considered
4214 out-of-scope after the first EXPR_STMT from within the
4215 STMT_EXPR. */
4216 push_temp_slots ();
4217 rtl_expr = expand_start_stmt_expr ();
4218 expand_stmt (STMT_EXPR_STMT (exp));
4219 expand_end_stmt_expr (rtl_expr);
4220 result = expand_expr (rtl_expr, target, tmode, modifier);
4221 pop_temp_slots ();
4222 return result;
4224 break;
4226 case CALL_EXPR:
4228 if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
4229 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4230 == FUNCTION_DECL)
4231 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4232 && (DECL_BUILT_IN_CLASS (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
4233 == BUILT_IN_FRONTEND))
4234 return c_expand_builtin (exp, target, tmode, modifier);
4235 else
4236 abort();
4238 break;
4240 default:
4241 abort ();
4244 abort ();
4245 return NULL;
4248 /* Hook used by safe_from_p to handle language-specific tree codes. */
4251 c_safe_from_p (target, exp)
4252 rtx target;
4253 tree exp;
4255 /* We can see statements here when processing the body of a
4256 statement-expression. For a declaration statement declaring a
4257 variable, look at the variable's initializer. */
4258 if (TREE_CODE (exp) == DECL_STMT)
4260 tree decl = DECL_STMT_DECL (exp);
4262 if (TREE_CODE (decl) == VAR_DECL
4263 && DECL_INITIAL (decl)
4264 && !safe_from_p (target, DECL_INITIAL (decl), /*top_p=*/0))
4265 return 0;
4268 /* For any statement, we must follow the statement-chain. */
4269 if (statement_code_p (TREE_CODE (exp)) && TREE_CHAIN (exp))
4270 return safe_from_p (target, TREE_CHAIN (exp), /*top_p=*/0);
4272 /* Assume everything else is safe. */
4273 return 1;
4276 /* Hook used by unsafe_for_reeval to handle language-specific tree codes. */
4279 c_unsafe_for_reeval (exp)
4280 tree exp;
4282 /* Statement expressions may not be reevaluated. */
4283 if (TREE_CODE (exp) == STMT_EXPR)
4284 return 2;
4286 /* Walk all other expressions. */
4287 return -1;
4290 /* Tree code classes. */
4292 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
4294 static char c_tree_code_type[] = {
4295 'x',
4296 #include "c-common.def"
4298 #undef DEFTREECODE
4300 /* Table indexed by tree code giving number of expression
4301 operands beyond the fixed part of the node structure.
4302 Not used for types or decls. */
4304 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
4306 static int c_tree_code_length[] = {
4308 #include "c-common.def"
4310 #undef DEFTREECODE
4312 /* Names of tree components.
4313 Used for printing out the tree and error messages. */
4314 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
4316 static const char *c_tree_code_name[] = {
4317 "@@dummy",
4318 #include "c-common.def"
4320 #undef DEFTREECODE
4322 /* Adds the tree codes specific to the C front end to the list of all
4323 tree codes. */
4325 void
4326 add_c_tree_codes ()
4328 memcpy (tree_code_type + (int) LAST_AND_UNUSED_TREE_CODE,
4329 c_tree_code_type,
4330 (int)LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE);
4331 memcpy (tree_code_length + (int) LAST_AND_UNUSED_TREE_CODE,
4332 c_tree_code_length,
4333 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (int));
4334 memcpy (tree_code_name + (int) LAST_AND_UNUSED_TREE_CODE,
4335 c_tree_code_name,
4336 (LAST_C_TREE_CODE - (int)LAST_AND_UNUSED_TREE_CODE) * sizeof (char *));
4337 lang_unsafe_for_reeval = c_unsafe_for_reeval;
4340 #define CALLED_AS_BUILT_IN(NODE) \
4341 (!strncmp (IDENTIFIER_POINTER (DECL_NAME (NODE)), "__builtin_", 10))
4343 static rtx
4344 c_expand_builtin (exp, target, tmode, modifier)
4345 tree exp;
4346 rtx target;
4347 enum machine_mode tmode;
4348 enum expand_modifier modifier;
4350 tree type = TREE_TYPE (exp);
4351 tree fndecl = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
4352 tree arglist = TREE_OPERAND (exp, 1);
4353 enum built_in_function fcode = DECL_FUNCTION_CODE (fndecl);
4354 enum tree_code code = TREE_CODE (exp);
4355 const int ignore = (target == const0_rtx
4356 || ((code == NON_LVALUE_EXPR || code == NOP_EXPR
4357 || code == CONVERT_EXPR || code == REFERENCE_EXPR
4358 || code == COND_EXPR)
4359 && TREE_CODE (type) == VOID_TYPE));
4361 if (! optimize && ! CALLED_AS_BUILT_IN (fndecl))
4362 return expand_call (exp, target, ignore);
4364 switch (fcode)
4366 case BUILT_IN_PRINTF:
4367 target = c_expand_builtin_printf (arglist, target, tmode,
4368 modifier, ignore);
4369 if (target)
4370 return target;
4371 break;
4373 case BUILT_IN_FPRINTF:
4374 target = c_expand_builtin_fprintf (arglist, target, tmode,
4375 modifier, ignore);
4376 if (target)
4377 return target;
4378 break;
4380 default: /* just do library call, if unknown builtin */
4381 error ("built-in function `%s' not currently supported",
4382 IDENTIFIER_POINTER (DECL_NAME (fndecl)));
4385 /* The switch statement above can drop through to cause the function
4386 to be called normally. */
4387 return expand_call (exp, target, ignore);
4390 /* Check an arglist to *printf for problems. The arglist should start
4391 at the format specifier, with the remaining arguments immediately
4392 following it. */
4393 static int
4394 is_valid_printf_arglist (arglist)
4395 tree arglist;
4397 /* Save this value so we can restore it later. */
4398 const int SAVE_pedantic = pedantic;
4399 int diagnostic_occurred = 0;
4401 /* Set this to a known value so the user setting won't affect code
4402 generation. */
4403 pedantic = 1;
4404 /* Check to make sure there are no format specifier errors. */
4405 check_function_format (&diagnostic_occurred,
4406 maybe_get_identifier("printf"),
4407 NULL_TREE, arglist);
4409 /* Restore the value of `pedantic'. */
4410 pedantic = SAVE_pedantic;
4412 /* If calling `check_function_format_ptr' produces a warning, we
4413 return false, otherwise we return true. */
4414 return ! diagnostic_occurred;
4417 /* If the arguments passed to printf are suitable for optimizations,
4418 we attempt to transform the call. */
4419 static rtx
4420 c_expand_builtin_printf (arglist, target, tmode, modifier, ignore)
4421 tree arglist;
4422 rtx target;
4423 enum machine_mode tmode;
4424 enum expand_modifier modifier;
4425 int ignore;
4427 tree fn_putchar = built_in_decls[BUILT_IN_PUTCHAR],
4428 fn_puts = built_in_decls[BUILT_IN_PUTS];
4429 tree fn, format_arg, stripped_string;
4431 /* If the return value is used, or the replacement _DECL isn't
4432 initialized, don't do the transformation. */
4433 if (!ignore || !fn_putchar || !fn_puts)
4434 return 0;
4436 /* Verify the required arguments in the original call. */
4437 if (arglist == 0
4438 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE))
4439 return 0;
4441 /* Check the specifier vs. the parameters. */
4442 if (!is_valid_printf_arglist (arglist))
4443 return 0;
4445 format_arg = TREE_VALUE (arglist);
4446 stripped_string = format_arg;
4447 STRIP_NOPS (stripped_string);
4448 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
4449 stripped_string = TREE_OPERAND (stripped_string, 0);
4451 /* If the format specifier isn't a STRING_CST, punt. */
4452 if (TREE_CODE (stripped_string) != STRING_CST)
4453 return 0;
4455 /* OK! We can attempt optimization. */
4457 /* If the format specifier was "%s\n", call __builtin_puts(arg2). */
4458 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s\n") == 0)
4460 arglist = TREE_CHAIN (arglist);
4461 fn = fn_puts;
4463 /* If the format specifier was "%c", call __builtin_putchar (arg2). */
4464 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
4466 arglist = TREE_CHAIN (arglist);
4467 fn = fn_putchar;
4469 else
4471 /* We can't handle anything else with % args or %% ... yet. */
4472 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
4473 return 0;
4475 /* If the resulting constant string has a length of 1, call
4476 putchar. Note, TREE_STRING_LENGTH includes the terminating
4477 NULL in its count. */
4478 if (TREE_STRING_LENGTH (stripped_string) == 2)
4480 /* Given printf("c"), (where c is any one character,)
4481 convert "c"[0] to an int and pass that to the replacement
4482 function. */
4483 arglist = build_int_2 (TREE_STRING_POINTER (stripped_string)[0], 0);
4484 arglist = build_tree_list (NULL_TREE, arglist);
4486 fn = fn_putchar;
4488 /* If the resulting constant was "string\n", call
4489 __builtin_puts("string"). Ensure "string" has at least one
4490 character besides the trailing \n. Note, TREE_STRING_LENGTH
4491 includes the terminating NULL in its count. */
4492 else if (TREE_STRING_LENGTH (stripped_string) > 2
4493 && TREE_STRING_POINTER (stripped_string)
4494 [TREE_STRING_LENGTH (stripped_string) - 2] == '\n')
4496 /* Create a NULL-terminated string that's one char shorter
4497 than the original, stripping off the trailing '\n'. */
4498 const int newlen = TREE_STRING_LENGTH (stripped_string) - 1;
4499 char *newstr = (char *) alloca (newlen);
4500 memcpy (newstr, TREE_STRING_POINTER (stripped_string), newlen - 1);
4501 newstr[newlen - 1] = 0;
4503 arglist = combine_strings (build_string (newlen, newstr));
4504 arglist = build_tree_list (NULL_TREE, arglist);
4505 fn = fn_puts;
4507 else
4508 /* We'd like to arrange to call fputs(string) here, but we
4509 need stdout and don't have a way to get it ... yet. */
4510 return 0;
4513 return expand_expr (build_function_call (fn, arglist),
4514 (ignore ? const0_rtx : target),
4515 tmode, modifier);
4518 /* If the arguments passed to fprintf are suitable for optimizations,
4519 we attempt to transform the call. */
4520 static rtx
4521 c_expand_builtin_fprintf (arglist, target, tmode, modifier, ignore)
4522 tree arglist;
4523 rtx target;
4524 enum machine_mode tmode;
4525 enum expand_modifier modifier;
4526 int ignore;
4528 tree fn_fputc = built_in_decls[BUILT_IN_FPUTC],
4529 fn_fputs = built_in_decls[BUILT_IN_FPUTS];
4530 tree fn, format_arg, stripped_string;
4532 /* If the return value is used, or the replacement _DECL isn't
4533 initialized, don't do the transformation. */
4534 if (!ignore || !fn_fputc || !fn_fputs)
4535 return 0;
4537 /* Verify the required arguments in the original call. */
4538 if (arglist == 0
4539 || (TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) != POINTER_TYPE)
4540 || (TREE_CHAIN (arglist) == 0)
4541 || (TREE_CODE (TREE_TYPE (TREE_VALUE (TREE_CHAIN (arglist)))) !=
4542 POINTER_TYPE))
4543 return 0;
4545 /* Check the specifier vs. the parameters. */
4546 if (!is_valid_printf_arglist (TREE_CHAIN (arglist)))
4547 return 0;
4549 format_arg = TREE_VALUE (TREE_CHAIN (arglist));
4550 stripped_string = format_arg;
4551 STRIP_NOPS (stripped_string);
4552 if (stripped_string && TREE_CODE (stripped_string) == ADDR_EXPR)
4553 stripped_string = TREE_OPERAND (stripped_string, 0);
4555 /* If the format specifier isn't a STRING_CST, punt. */
4556 if (TREE_CODE (stripped_string) != STRING_CST)
4557 return 0;
4559 /* OK! We can attempt optimization. */
4561 /* If the format specifier was "%s", call __builtin_fputs(arg3, arg1). */
4562 if (strcmp (TREE_STRING_POINTER (stripped_string), "%s") == 0)
4564 tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist));
4565 arglist = tree_cons (NULL_TREE,
4566 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
4567 newarglist);
4568 fn = fn_fputs;
4570 /* If the format specifier was "%c", call __builtin_fputc (arg3, arg1). */
4571 else if (strcmp (TREE_STRING_POINTER (stripped_string), "%c") == 0)
4573 tree newarglist = build_tree_list (NULL_TREE, TREE_VALUE (arglist));
4574 arglist = tree_cons (NULL_TREE,
4575 TREE_VALUE (TREE_CHAIN (TREE_CHAIN (arglist))),
4576 newarglist);
4577 fn = fn_fputc;
4579 else
4581 /* We can't handle anything else with % args or %% ... yet. */
4582 if (strchr (TREE_STRING_POINTER (stripped_string), '%'))
4583 return 0;
4585 /* When "string" doesn't contain %, replace all cases of
4586 fprintf(stream,string) with fputs(string,stream). The fputs
4587 builtin will take take of special cases like length==1. */
4588 arglist = tree_cons (NULL_TREE, TREE_VALUE (TREE_CHAIN (arglist)),
4589 build_tree_list (NULL_TREE, TREE_VALUE (arglist)));
4590 fn = fn_fputs;
4593 return expand_expr (build_function_call (fn, arglist),
4594 (ignore ? const0_rtx : target),
4595 tmode, modifier);
4599 /* Given a boolean expression ARG, return a tree representing an increment
4600 or decrement (as indicated by CODE) of ARG. The front end must check for
4601 invalid cases (e.g., decrement in C++). */
4602 tree
4603 boolean_increment (code, arg)
4604 enum tree_code code;
4605 tree arg;
4607 tree val;
4608 tree true_res = (c_language == clk_cplusplus
4609 ? boolean_true_node
4610 : c_bool_true_node);
4611 arg = stabilize_reference (arg);
4612 switch (code)
4614 case PREINCREMENT_EXPR:
4615 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4616 break;
4617 case POSTINCREMENT_EXPR:
4618 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, true_res);
4619 arg = save_expr (arg);
4620 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4621 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4622 break;
4623 case PREDECREMENT_EXPR:
4624 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
4625 break;
4626 case POSTDECREMENT_EXPR:
4627 val = build (MODIFY_EXPR, TREE_TYPE (arg), arg, invert_truthvalue (arg));
4628 arg = save_expr (arg);
4629 val = build (COMPOUND_EXPR, TREE_TYPE (arg), val, arg);
4630 val = build (COMPOUND_EXPR, TREE_TYPE (arg), arg, val);
4631 break;
4632 default:
4633 abort ();
4635 TREE_SIDE_EFFECTS (val) = 1;
4636 return val;
4640 /* Do the parts of lang_init common to C and C++. */
4641 void
4642 c_common_lang_init ()
4644 /* If still "unspecified", make it match -fbounded-pointers. */
4645 if (flag_bounds_check < 0)
4646 flag_bounds_check = flag_bounded_pointers;
4648 /* Special format checking options don't work without -Wformat; warn if
4649 they are used. */
4650 if (warn_format_y2k && !warn_format)
4651 warning ("-Wformat-y2k ignored without -Wformat");
4652 if (warn_format_extra_args && !warn_format)
4653 warning ("-Wformat-extra-args ignored without -Wformat");
4654 if (warn_format_nonliteral && !warn_format)
4655 warning ("-Wformat-nonliteral ignored without -Wformat");
4656 if (warn_format_security && !warn_format)
4657 warning ("-Wformat-security ignored without -Wformat");
4658 if (warn_missing_format_attribute && !warn_format)
4659 warning ("-Wmissing-format-attribute ignored without -Wformat");