2002-02-19 Philip Blundell <philb@gnu.org>
[official-gcc.git] / gcc / tree.c
blob764aa798ed8cb27d8f6371e0c71f5387b652700c
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* This file contains the low level primitives for operating on tree nodes,
23 including allocation, list operations, interning of identifiers,
24 construction of data type nodes and statement nodes,
25 and construction of type conversion nodes. It also contains
26 tables index by tree code that describe how to take apart
27 nodes of that code.
29 It is intended to be language-independent, but occasionally
30 calls language-dependent routines defined (for C) in typecheck.c.
32 The low-level allocation routines oballoc and permalloc
33 are used also for allocating many other kinds of objects
34 by all passes of the compiler. */
36 #include "config.h"
37 #include "system.h"
38 #include "flags.h"
39 #include "tree.h"
40 #include "tm_p.h"
41 #include "function.h"
42 #include "obstack.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "hashtab.h"
46 #include "output.h"
47 #include "target.h"
48 #include "langhooks.h"
50 #define obstack_chunk_alloc xmalloc
51 #define obstack_chunk_free free
52 /* obstack.[ch] explicitly declined to prototype this. */
53 extern int _obstack_allocated_p PARAMS ((struct obstack *h, PTR obj));
55 static void unsave_expr_now_r PARAMS ((tree));
57 /* Objects allocated on this obstack last forever. */
59 struct obstack permanent_obstack;
61 /* Table indexed by tree code giving a string containing a character
62 classifying the tree code. Possibilities are
63 t, d, s, c, r, <, 1, 2 and e. See tree.def for details. */
65 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
67 char tree_code_type[MAX_TREE_CODES] = {
68 #include "tree.def"
70 #undef DEFTREECODE
72 /* Table indexed by tree code giving number of expression
73 operands beyond the fixed part of the node structure.
74 Not used for types or decls. */
76 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
78 int tree_code_length[MAX_TREE_CODES] = {
79 #include "tree.def"
81 #undef DEFTREECODE
83 /* Names of tree components.
84 Used for printing out the tree and error messages. */
85 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
87 const char *tree_code_name[MAX_TREE_CODES] = {
88 #include "tree.def"
90 #undef DEFTREECODE
92 /* Statistics-gathering stuff. */
93 typedef enum
95 d_kind,
96 t_kind,
97 b_kind,
98 s_kind,
99 r_kind,
100 e_kind,
101 c_kind,
102 id_kind,
103 perm_list_kind,
104 temp_list_kind,
105 vec_kind,
106 x_kind,
107 lang_decl,
108 lang_type,
109 all_kinds
110 } tree_node_kind;
112 int tree_node_counts[(int) all_kinds];
113 int tree_node_sizes[(int) all_kinds];
115 static const char * const tree_node_kind_names[] = {
116 "decls",
117 "types",
118 "blocks",
119 "stmts",
120 "refs",
121 "exprs",
122 "constants",
123 "identifiers",
124 "perm_tree_lists",
125 "temp_tree_lists",
126 "vecs",
127 "random kinds",
128 "lang_decl kinds",
129 "lang_type kinds"
132 /* Unique id for next decl created. */
133 static int next_decl_uid;
134 /* Unique id for next type created. */
135 static int next_type_uid = 1;
137 /* Since we cannot rehash a type after it is in the table, we have to
138 keep the hash code. */
140 struct type_hash
142 unsigned long hash;
143 tree type;
146 /* Initial size of the hash table (rounded to next prime). */
147 #define TYPE_HASH_INITIAL_SIZE 1000
149 /* Now here is the hash table. When recording a type, it is added to
150 the slot whose index is the hash code. Note that the hash table is
151 used for several kinds of types (function types, array types and
152 array index range types, for now). While all these live in the
153 same table, they are completely independent, and the hash code is
154 computed differently for each of these. */
156 htab_t type_hash_table;
158 static void build_real_from_int_cst_1 PARAMS ((PTR));
159 static void set_type_quals PARAMS ((tree, int));
160 static void append_random_chars PARAMS ((char *));
161 static int type_hash_eq PARAMS ((const void*, const void*));
162 static unsigned int type_hash_hash PARAMS ((const void*));
163 static void print_type_hash_statistics PARAMS((void));
164 static void finish_vector_type PARAMS((tree));
165 static tree make_vector PARAMS ((enum machine_mode, tree, int));
166 static int type_hash_marked_p PARAMS ((const void *));
167 static void type_hash_mark PARAMS ((const void *));
168 static int mark_tree_hashtable_entry PARAMS((void **, void *));
170 /* If non-null, these are language-specific helper functions for
171 unsave_expr_now. If present, LANG_UNSAVE is called before its
172 argument (an UNSAVE_EXPR) is to be unsaved, and all other
173 processing in unsave_expr_now is aborted. LANG_UNSAVE_EXPR_NOW is
174 called from unsave_expr_1 for language-specific tree codes. */
175 void (*lang_unsave) PARAMS ((tree *));
176 void (*lang_unsave_expr_now) PARAMS ((tree));
178 /* If non-null, these are language-specific helper functions for
179 unsafe_for_reeval. Return negative to not handle some tree. */
180 int (*lang_unsafe_for_reeval) PARAMS ((tree));
182 /* Set the DECL_ASSEMBLER_NAME for a node. If it is the sort of thing
183 that the assembler should talk about, set DECL_ASSEMBLER_NAME to an
184 appropriate IDENTIFIER_NODE. Otherwise, set it to the
185 ERROR_MARK_NODE to ensure that the assembler does not talk about
186 it. */
187 void (*lang_set_decl_assembler_name) PARAMS ((tree));
189 tree global_trees[TI_MAX];
190 tree integer_types[itk_none];
192 /* Set the DECL_ASSEMBLER_NAME for DECL. */
193 void
194 set_decl_assembler_name (decl)
195 tree decl;
197 /* The language-independent code should never use the
198 DECL_ASSEMBLER_NAME for lots of DECLs. Only FUNCTION_DECLs and
199 VAR_DECLs for variables with static storage duration need a real
200 DECL_ASSEMBLER_NAME. */
201 if (TREE_CODE (decl) == FUNCTION_DECL
202 || (TREE_CODE (decl) == VAR_DECL
203 && (TREE_STATIC (decl)
204 || DECL_EXTERNAL (decl)
205 || TREE_PUBLIC (decl))))
206 /* By default, assume the name to use in assembly code is the
207 same as that used in the source language. (That's correct
208 for C, and GCC used to set DECL_ASSEMBLER_NAME to the same
209 value as DECL_NAME in build_decl, so this choice provides
210 backwards compatibility with existing front-ends. */
211 SET_DECL_ASSEMBLER_NAME (decl, DECL_NAME (decl));
212 else
213 /* Nobody should ever be asking for the DECL_ASSEMBLER_NAME of
214 these DECLs -- unless they're in language-dependent code, in
215 which case lang_set_decl_assembler_name should handle things. */
216 abort ();
219 /* Init the principal obstacks. */
221 void
222 init_obstacks ()
224 gcc_obstack_init (&permanent_obstack);
226 /* Initialize the hash table of types. */
227 type_hash_table = htab_create (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
228 type_hash_eq, 0);
229 ggc_add_deletable_htab (type_hash_table, type_hash_marked_p,
230 type_hash_mark);
231 ggc_add_tree_root (global_trees, TI_MAX);
232 ggc_add_tree_root (integer_types, itk_none);
234 /* Set lang_set_decl_set_assembler_name to a default value. */
235 lang_set_decl_assembler_name = set_decl_assembler_name;
239 /* Allocate SIZE bytes in the permanent obstack
240 and return a pointer to them. */
242 char *
243 permalloc (size)
244 int size;
246 return (char *) obstack_alloc (&permanent_obstack, size);
249 /* Allocate NELEM items of SIZE bytes in the permanent obstack
250 and return a pointer to them. The storage is cleared before
251 returning the value. */
253 char *
254 perm_calloc (nelem, size)
255 int nelem;
256 long size;
258 char *rval = (char *) obstack_alloc (&permanent_obstack, nelem * size);
259 memset (rval, 0, nelem * size);
260 return rval;
263 /* Compute the number of bytes occupied by 'node'. This routine only
264 looks at TREE_CODE and, if the code is TREE_VEC, TREE_VEC_LENGTH. */
265 size_t
266 tree_size (node)
267 tree node;
269 enum tree_code code = TREE_CODE (node);
271 switch (TREE_CODE_CLASS (code))
273 case 'd': /* A decl node */
274 return sizeof (struct tree_decl);
276 case 't': /* a type node */
277 return sizeof (struct tree_type);
279 case 'b': /* a lexical block node */
280 return sizeof (struct tree_block);
282 case 'r': /* a reference */
283 case 'e': /* an expression */
284 case 's': /* an expression with side effects */
285 case '<': /* a comparison expression */
286 case '1': /* a unary arithmetic expression */
287 case '2': /* a binary arithmetic expression */
288 return (sizeof (struct tree_exp)
289 + (TREE_CODE_LENGTH (code) - 1) * sizeof (char *));
291 case 'c': /* a constant */
292 /* We can't use TREE_CODE_LENGTH for INTEGER_CST, since the number of
293 words is machine-dependent due to varying length of HOST_WIDE_INT,
294 which might be wider than a pointer (e.g., long long). Similarly
295 for REAL_CST, since the number of words is machine-dependent due
296 to varying size and alignment of `double'. */
297 if (code == INTEGER_CST)
298 return sizeof (struct tree_int_cst);
299 else if (code == REAL_CST)
300 return sizeof (struct tree_real_cst);
301 else
302 return (sizeof (struct tree_common)
303 + TREE_CODE_LENGTH (code) * sizeof (char *));
305 case 'x': /* something random, like an identifier. */
307 size_t length;
308 length = (sizeof (struct tree_common)
309 + TREE_CODE_LENGTH (code) * sizeof (char *));
310 if (code == TREE_VEC)
311 length += (TREE_VEC_LENGTH (node) - 1) * sizeof (char *);
312 return length;
315 default:
316 abort ();
320 /* Return a newly allocated node of code CODE.
321 For decl and type nodes, some other fields are initialized.
322 The rest of the node is initialized to zero.
324 Achoo! I got a code in the node. */
326 tree
327 make_node (code)
328 enum tree_code code;
330 tree t;
331 int type = TREE_CODE_CLASS (code);
332 size_t length;
333 #ifdef GATHER_STATISTICS
334 tree_node_kind kind;
335 #endif
336 struct tree_common ttmp;
338 /* We can't allocate a TREE_VEC without knowing how many elements
339 it will have. */
340 if (code == TREE_VEC)
341 abort ();
343 TREE_SET_CODE ((tree)&ttmp, code);
344 length = tree_size ((tree)&ttmp);
346 #ifdef GATHER_STATISTICS
347 switch (type)
349 case 'd': /* A decl node */
350 kind = d_kind;
351 break;
353 case 't': /* a type node */
354 kind = t_kind;
355 break;
357 case 'b': /* a lexical block */
358 kind = b_kind;
359 break;
361 case 's': /* an expression with side effects */
362 kind = s_kind;
363 break;
365 case 'r': /* a reference */
366 kind = r_kind;
367 break;
369 case 'e': /* an expression */
370 case '<': /* a comparison expression */
371 case '1': /* a unary arithmetic expression */
372 case '2': /* a binary arithmetic expression */
373 kind = e_kind;
374 break;
376 case 'c': /* a constant */
377 kind = c_kind;
378 break;
380 case 'x': /* something random, like an identifier. */
381 if (code == IDENTIFIER_NODE)
382 kind = id_kind;
383 else if (code == TREE_VEC)
384 kind = vec_kind;
385 else
386 kind = x_kind;
387 break;
389 default:
390 abort ();
393 tree_node_counts[(int) kind]++;
394 tree_node_sizes[(int) kind] += length;
395 #endif
397 t = ggc_alloc_tree (length);
399 memset ((PTR) t, 0, length);
401 TREE_SET_CODE (t, code);
403 switch (type)
405 case 's':
406 TREE_SIDE_EFFECTS (t) = 1;
407 TREE_TYPE (t) = void_type_node;
408 break;
410 case 'd':
411 if (code != FUNCTION_DECL)
412 DECL_ALIGN (t) = 1;
413 DECL_USER_ALIGN (t) = 0;
414 DECL_IN_SYSTEM_HEADER (t) = in_system_header;
415 DECL_SOURCE_LINE (t) = lineno;
416 DECL_SOURCE_FILE (t) =
417 (input_filename) ? input_filename : "<built-in>";
418 DECL_UID (t) = next_decl_uid++;
420 /* We have not yet computed the alias set for this declaration. */
421 DECL_POINTER_ALIAS_SET (t) = -1;
422 break;
424 case 't':
425 TYPE_UID (t) = next_type_uid++;
426 TYPE_ALIGN (t) = char_type_node ? TYPE_ALIGN (char_type_node) : 0;
427 TYPE_USER_ALIGN (t) = 0;
428 TYPE_MAIN_VARIANT (t) = t;
430 /* Default to no attributes for type, but let target change that. */
431 TYPE_ATTRIBUTES (t) = NULL_TREE;
432 (*targetm.set_default_type_attributes) (t);
434 /* We have not yet computed the alias set for this type. */
435 TYPE_ALIAS_SET (t) = -1;
436 break;
438 case 'c':
439 TREE_CONSTANT (t) = 1;
440 break;
442 case 'e':
443 switch (code)
445 case INIT_EXPR:
446 case MODIFY_EXPR:
447 case VA_ARG_EXPR:
448 case RTL_EXPR:
449 case PREDECREMENT_EXPR:
450 case PREINCREMENT_EXPR:
451 case POSTDECREMENT_EXPR:
452 case POSTINCREMENT_EXPR:
453 /* All of these have side-effects, no matter what their
454 operands are. */
455 TREE_SIDE_EFFECTS (t) = 1;
456 break;
458 default:
459 break;
461 break;
464 return t;
467 /* A front-end can reset this to an appropriate function if types need
468 special handling. */
470 tree (*make_lang_type_fn) PARAMS ((enum tree_code)) = make_node;
472 /* Return a new type (with the indicated CODE), doing whatever
473 language-specific processing is required. */
475 tree
476 make_lang_type (code)
477 enum tree_code code;
479 return (*make_lang_type_fn) (code);
482 /* Return a new node with the same contents as NODE except that its
483 TREE_CHAIN is zero and it has a fresh uid. */
485 tree
486 copy_node (node)
487 tree node;
489 tree t;
490 enum tree_code code = TREE_CODE (node);
491 size_t length;
493 length = tree_size (node);
494 t = ggc_alloc_tree (length);
495 memcpy (t, node, length);
497 TREE_CHAIN (t) = 0;
498 TREE_ASM_WRITTEN (t) = 0;
500 if (TREE_CODE_CLASS (code) == 'd')
501 DECL_UID (t) = next_decl_uid++;
502 else if (TREE_CODE_CLASS (code) == 't')
504 TYPE_UID (t) = next_type_uid++;
505 /* The following is so that the debug code for
506 the copy is different from the original type.
507 The two statements usually duplicate each other
508 (because they clear fields of the same union),
509 but the optimizer should catch that. */
510 TYPE_SYMTAB_POINTER (t) = 0;
511 TYPE_SYMTAB_ADDRESS (t) = 0;
514 return t;
517 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
518 For example, this can copy a list made of TREE_LIST nodes. */
520 tree
521 copy_list (list)
522 tree list;
524 tree head;
525 tree prev, next;
527 if (list == 0)
528 return 0;
530 head = prev = copy_node (list);
531 next = TREE_CHAIN (list);
532 while (next)
534 TREE_CHAIN (prev) = copy_node (next);
535 prev = TREE_CHAIN (prev);
536 next = TREE_CHAIN (next);
538 return head;
542 /* Return a newly constructed INTEGER_CST node whose constant value
543 is specified by the two ints LOW and HI.
544 The TREE_TYPE is set to `int'.
546 This function should be used via the `build_int_2' macro. */
548 tree
549 build_int_2_wide (low, hi)
550 unsigned HOST_WIDE_INT low;
551 HOST_WIDE_INT hi;
553 tree t = make_node (INTEGER_CST);
555 TREE_INT_CST_LOW (t) = low;
556 TREE_INT_CST_HIGH (t) = hi;
557 TREE_TYPE (t) = integer_type_node;
558 return t;
561 /* Return a new VECTOR_CST node whose type is TYPE and whose values
562 are in a list pointed by VALS. */
564 tree
565 build_vector (type, vals)
566 tree type, vals;
568 tree v = make_node (VECTOR_CST);
569 int over1 = 0, over2 = 0;
570 tree link;
572 TREE_VECTOR_CST_ELTS (v) = vals;
573 TREE_TYPE (v) = type;
575 /* Iterate through elements and check for overflow. */
576 for (link = vals; link; link = TREE_CHAIN (link))
578 tree value = TREE_VALUE (link);
580 over1 |= TREE_OVERFLOW (value);
581 over2 |= TREE_CONSTANT_OVERFLOW (value);
584 TREE_OVERFLOW (v) = over1;
585 TREE_CONSTANT_OVERFLOW (v) = over2;
587 return v;
590 /* Return a new REAL_CST node whose type is TYPE and value is D. */
592 tree
593 build_real (type, d)
594 tree type;
595 REAL_VALUE_TYPE d;
597 tree v;
598 int overflow = 0;
600 /* Check for valid float value for this type on this target machine;
601 if not, can print error message and store a valid value in D. */
602 #ifdef CHECK_FLOAT_VALUE
603 CHECK_FLOAT_VALUE (TYPE_MODE (type), d, overflow);
604 #endif
606 v = make_node (REAL_CST);
607 TREE_TYPE (v) = type;
608 TREE_REAL_CST (v) = d;
609 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
610 return v;
613 /* Return a new REAL_CST node whose type is TYPE
614 and whose value is the integer value of the INTEGER_CST node I. */
616 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
618 REAL_VALUE_TYPE
619 real_value_from_int_cst (type, i)
620 tree type ATTRIBUTE_UNUSED, i;
622 REAL_VALUE_TYPE d;
624 #ifdef REAL_ARITHMETIC
625 /* Clear all bits of the real value type so that we can later do
626 bitwise comparisons to see if two values are the same. */
627 memset ((char *) &d, 0, sizeof d);
629 if (! TREE_UNSIGNED (TREE_TYPE (i)))
630 REAL_VALUE_FROM_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
631 TYPE_MODE (type));
632 else
633 REAL_VALUE_FROM_UNSIGNED_INT (d, TREE_INT_CST_LOW (i),
634 TREE_INT_CST_HIGH (i), TYPE_MODE (type));
635 #else /* not REAL_ARITHMETIC */
636 /* Some 386 compilers mishandle unsigned int to float conversions,
637 so introduce a temporary variable E to avoid those bugs. */
638 if (TREE_INT_CST_HIGH (i) < 0 && ! TREE_UNSIGNED (TREE_TYPE (i)))
640 REAL_VALUE_TYPE e;
642 d = (double) (~TREE_INT_CST_HIGH (i));
643 e = ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
644 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
645 d *= e;
646 e = (double) (~TREE_INT_CST_LOW (i));
647 d += e;
648 d = (- d - 1.0);
650 else
652 REAL_VALUE_TYPE e;
654 d = (double) (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (i);
655 e = ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
656 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
657 d *= e;
658 e = (double) TREE_INT_CST_LOW (i);
659 d += e;
661 #endif /* not REAL_ARITHMETIC */
662 return d;
665 /* Args to pass to and from build_real_from_int_cst_1. */
667 struct brfic_args
669 tree type; /* Input: type to conver to. */
670 tree i; /* Input: operand to convert. */
671 REAL_VALUE_TYPE d; /* Output: floating point value. */
674 /* Convert an integer to a floating point value while protected by a floating
675 point exception handler. */
677 static void
678 build_real_from_int_cst_1 (data)
679 PTR data;
681 struct brfic_args *args = (struct brfic_args *) data;
683 #ifdef REAL_ARITHMETIC
684 args->d = real_value_from_int_cst (args->type, args->i);
685 #else
686 args->d
687 = REAL_VALUE_TRUNCATE (TYPE_MODE (args->type),
688 real_value_from_int_cst (args->type, args->i));
689 #endif
692 /* Given a tree representing an integer constant I, return a tree
693 representing the same value as a floating-point constant of type TYPE.
694 We cannot perform this operation if there is no way of doing arithmetic
695 on floating-point values. */
697 tree
698 build_real_from_int_cst (type, i)
699 tree type;
700 tree i;
702 tree v;
703 int overflow = TREE_OVERFLOW (i);
704 REAL_VALUE_TYPE d;
705 struct brfic_args args;
707 v = make_node (REAL_CST);
708 TREE_TYPE (v) = type;
710 /* Setup input for build_real_from_int_cst_1() */
711 args.type = type;
712 args.i = i;
714 if (do_float_handler (build_real_from_int_cst_1, (PTR) &args))
715 /* Receive output from build_real_from_int_cst_1() */
716 d = args.d;
717 else
719 /* We got an exception from build_real_from_int_cst_1() */
720 d = dconst0;
721 overflow = 1;
724 /* Check for valid float value for this type on this target machine. */
726 #ifdef CHECK_FLOAT_VALUE
727 CHECK_FLOAT_VALUE (TYPE_MODE (type), d, overflow);
728 #endif
730 TREE_REAL_CST (v) = d;
731 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
732 return v;
735 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
737 /* Return a newly constructed STRING_CST node whose value is
738 the LEN characters at STR.
739 The TREE_TYPE is not initialized. */
741 tree
742 build_string (len, str)
743 int len;
744 const char *str;
746 tree s = make_node (STRING_CST);
748 TREE_STRING_LENGTH (s) = len;
749 TREE_STRING_POINTER (s) = ggc_alloc_string (str, len);
751 return s;
754 /* Return a newly constructed COMPLEX_CST node whose value is
755 specified by the real and imaginary parts REAL and IMAG.
756 Both REAL and IMAG should be constant nodes. TYPE, if specified,
757 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
759 tree
760 build_complex (type, real, imag)
761 tree type;
762 tree real, imag;
764 tree t = make_node (COMPLEX_CST);
766 TREE_REALPART (t) = real;
767 TREE_IMAGPART (t) = imag;
768 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
769 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
770 TREE_CONSTANT_OVERFLOW (t)
771 = TREE_CONSTANT_OVERFLOW (real) | TREE_CONSTANT_OVERFLOW (imag);
772 return t;
775 /* Build a newly constructed TREE_VEC node of length LEN. */
777 tree
778 make_tree_vec (len)
779 int len;
781 tree t;
782 int length = (len-1) * sizeof (tree) + sizeof (struct tree_vec);
784 #ifdef GATHER_STATISTICS
785 tree_node_counts[(int)vec_kind]++;
786 tree_node_sizes[(int)vec_kind] += length;
787 #endif
789 t = ggc_alloc_tree (length);
791 memset ((PTR) t, 0, length);
792 TREE_SET_CODE (t, TREE_VEC);
793 TREE_VEC_LENGTH (t) = len;
795 return t;
798 /* Return 1 if EXPR is the integer constant zero or a complex constant
799 of zero. */
802 integer_zerop (expr)
803 tree expr;
805 STRIP_NOPS (expr);
807 return ((TREE_CODE (expr) == INTEGER_CST
808 && ! TREE_CONSTANT_OVERFLOW (expr)
809 && TREE_INT_CST_LOW (expr) == 0
810 && TREE_INT_CST_HIGH (expr) == 0)
811 || (TREE_CODE (expr) == COMPLEX_CST
812 && integer_zerop (TREE_REALPART (expr))
813 && integer_zerop (TREE_IMAGPART (expr))));
816 /* Return 1 if EXPR is the integer constant one or the corresponding
817 complex constant. */
820 integer_onep (expr)
821 tree expr;
823 STRIP_NOPS (expr);
825 return ((TREE_CODE (expr) == INTEGER_CST
826 && ! TREE_CONSTANT_OVERFLOW (expr)
827 && TREE_INT_CST_LOW (expr) == 1
828 && TREE_INT_CST_HIGH (expr) == 0)
829 || (TREE_CODE (expr) == COMPLEX_CST
830 && integer_onep (TREE_REALPART (expr))
831 && integer_zerop (TREE_IMAGPART (expr))));
834 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
835 it contains. Likewise for the corresponding complex constant. */
838 integer_all_onesp (expr)
839 tree expr;
841 int prec;
842 int uns;
844 STRIP_NOPS (expr);
846 if (TREE_CODE (expr) == COMPLEX_CST
847 && integer_all_onesp (TREE_REALPART (expr))
848 && integer_zerop (TREE_IMAGPART (expr)))
849 return 1;
851 else if (TREE_CODE (expr) != INTEGER_CST
852 || TREE_CONSTANT_OVERFLOW (expr))
853 return 0;
855 uns = TREE_UNSIGNED (TREE_TYPE (expr));
856 if (!uns)
857 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
858 && TREE_INT_CST_HIGH (expr) == -1);
860 /* Note that using TYPE_PRECISION here is wrong. We care about the
861 actual bits, not the (arbitrary) range of the type. */
862 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)));
863 if (prec >= HOST_BITS_PER_WIDE_INT)
865 HOST_WIDE_INT high_value;
866 int shift_amount;
868 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
870 if (shift_amount > HOST_BITS_PER_WIDE_INT)
871 /* Can not handle precisions greater than twice the host int size. */
872 abort ();
873 else if (shift_amount == HOST_BITS_PER_WIDE_INT)
874 /* Shifting by the host word size is undefined according to the ANSI
875 standard, so we must handle this as a special case. */
876 high_value = -1;
877 else
878 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
880 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
881 && TREE_INT_CST_HIGH (expr) == high_value);
883 else
884 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
887 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
888 one bit on). */
891 integer_pow2p (expr)
892 tree expr;
894 int prec;
895 HOST_WIDE_INT high, low;
897 STRIP_NOPS (expr);
899 if (TREE_CODE (expr) == COMPLEX_CST
900 && integer_pow2p (TREE_REALPART (expr))
901 && integer_zerop (TREE_IMAGPART (expr)))
902 return 1;
904 if (TREE_CODE (expr) != INTEGER_CST || TREE_CONSTANT_OVERFLOW (expr))
905 return 0;
907 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
908 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
909 high = TREE_INT_CST_HIGH (expr);
910 low = TREE_INT_CST_LOW (expr);
912 /* First clear all bits that are beyond the type's precision in case
913 we've been sign extended. */
915 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
917 else if (prec > HOST_BITS_PER_WIDE_INT)
918 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
919 else
921 high = 0;
922 if (prec < HOST_BITS_PER_WIDE_INT)
923 low &= ~((HOST_WIDE_INT) (-1) << prec);
926 if (high == 0 && low == 0)
927 return 0;
929 return ((high == 0 && (low & (low - 1)) == 0)
930 || (low == 0 && (high & (high - 1)) == 0));
933 /* Return the power of two represented by a tree node known to be a
934 power of two. */
937 tree_log2 (expr)
938 tree expr;
940 int prec;
941 HOST_WIDE_INT high, low;
943 STRIP_NOPS (expr);
945 if (TREE_CODE (expr) == COMPLEX_CST)
946 return tree_log2 (TREE_REALPART (expr));
948 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
949 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
951 high = TREE_INT_CST_HIGH (expr);
952 low = TREE_INT_CST_LOW (expr);
954 /* First clear all bits that are beyond the type's precision in case
955 we've been sign extended. */
957 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
959 else if (prec > HOST_BITS_PER_WIDE_INT)
960 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
961 else
963 high = 0;
964 if (prec < HOST_BITS_PER_WIDE_INT)
965 low &= ~((HOST_WIDE_INT) (-1) << prec);
968 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
969 : exact_log2 (low));
972 /* Similar, but return the largest integer Y such that 2 ** Y is less
973 than or equal to EXPR. */
976 tree_floor_log2 (expr)
977 tree expr;
979 int prec;
980 HOST_WIDE_INT high, low;
982 STRIP_NOPS (expr);
984 if (TREE_CODE (expr) == COMPLEX_CST)
985 return tree_log2 (TREE_REALPART (expr));
987 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
988 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
990 high = TREE_INT_CST_HIGH (expr);
991 low = TREE_INT_CST_LOW (expr);
993 /* First clear all bits that are beyond the type's precision in case
994 we've been sign extended. Ignore if type's precision hasn't been set
995 since what we are doing is setting it. */
997 if (prec == 2 * HOST_BITS_PER_WIDE_INT || prec == 0)
999 else if (prec > HOST_BITS_PER_WIDE_INT)
1000 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
1001 else
1003 high = 0;
1004 if (prec < HOST_BITS_PER_WIDE_INT)
1005 low &= ~((HOST_WIDE_INT) (-1) << prec);
1008 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
1009 : floor_log2 (low));
1012 /* Return 1 if EXPR is the real constant zero. */
1015 real_zerop (expr)
1016 tree expr;
1018 STRIP_NOPS (expr);
1020 return ((TREE_CODE (expr) == REAL_CST
1021 && ! TREE_CONSTANT_OVERFLOW (expr)
1022 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0))
1023 || (TREE_CODE (expr) == COMPLEX_CST
1024 && real_zerop (TREE_REALPART (expr))
1025 && real_zerop (TREE_IMAGPART (expr))));
1028 /* Return 1 if EXPR is the real constant one in real or complex form. */
1031 real_onep (expr)
1032 tree expr;
1034 STRIP_NOPS (expr);
1036 return ((TREE_CODE (expr) == REAL_CST
1037 && ! TREE_CONSTANT_OVERFLOW (expr)
1038 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1))
1039 || (TREE_CODE (expr) == COMPLEX_CST
1040 && real_onep (TREE_REALPART (expr))
1041 && real_zerop (TREE_IMAGPART (expr))));
1044 /* Return 1 if EXPR is the real constant two. */
1047 real_twop (expr)
1048 tree expr;
1050 STRIP_NOPS (expr);
1052 return ((TREE_CODE (expr) == REAL_CST
1053 && ! TREE_CONSTANT_OVERFLOW (expr)
1054 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2))
1055 || (TREE_CODE (expr) == COMPLEX_CST
1056 && real_twop (TREE_REALPART (expr))
1057 && real_zerop (TREE_IMAGPART (expr))));
1060 /* Nonzero if EXP is a constant or a cast of a constant. */
1063 really_constant_p (exp)
1064 tree exp;
1066 /* This is not quite the same as STRIP_NOPS. It does more. */
1067 while (TREE_CODE (exp) == NOP_EXPR
1068 || TREE_CODE (exp) == CONVERT_EXPR
1069 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1070 exp = TREE_OPERAND (exp, 0);
1071 return TREE_CONSTANT (exp);
1074 /* Return first list element whose TREE_VALUE is ELEM.
1075 Return 0 if ELEM is not in LIST. */
1077 tree
1078 value_member (elem, list)
1079 tree elem, list;
1081 while (list)
1083 if (elem == TREE_VALUE (list))
1084 return list;
1085 list = TREE_CHAIN (list);
1087 return NULL_TREE;
1090 /* Return first list element whose TREE_PURPOSE is ELEM.
1091 Return 0 if ELEM is not in LIST. */
1093 tree
1094 purpose_member (elem, list)
1095 tree elem, list;
1097 while (list)
1099 if (elem == TREE_PURPOSE (list))
1100 return list;
1101 list = TREE_CHAIN (list);
1103 return NULL_TREE;
1106 /* Return first list element whose BINFO_TYPE is ELEM.
1107 Return 0 if ELEM is not in LIST. */
1109 tree
1110 binfo_member (elem, list)
1111 tree elem, list;
1113 while (list)
1115 if (elem == BINFO_TYPE (list))
1116 return list;
1117 list = TREE_CHAIN (list);
1119 return NULL_TREE;
1122 /* Return nonzero if ELEM is part of the chain CHAIN. */
1125 chain_member (elem, chain)
1126 tree elem, chain;
1128 while (chain)
1130 if (elem == chain)
1131 return 1;
1132 chain = TREE_CHAIN (chain);
1135 return 0;
1138 /* Return nonzero if ELEM is equal to TREE_VALUE (CHAIN) for any piece of
1139 chain CHAIN. This and the next function are currently unused, but
1140 are retained for completeness. */
1143 chain_member_value (elem, chain)
1144 tree elem, chain;
1146 while (chain)
1148 if (elem == TREE_VALUE (chain))
1149 return 1;
1150 chain = TREE_CHAIN (chain);
1153 return 0;
1156 /* Return nonzero if ELEM is equal to TREE_PURPOSE (CHAIN)
1157 for any piece of chain CHAIN. */
1160 chain_member_purpose (elem, chain)
1161 tree elem, chain;
1163 while (chain)
1165 if (elem == TREE_PURPOSE (chain))
1166 return 1;
1167 chain = TREE_CHAIN (chain);
1170 return 0;
1173 /* Return the length of a chain of nodes chained through TREE_CHAIN.
1174 We expect a null pointer to mark the end of the chain.
1175 This is the Lisp primitive `length'. */
1178 list_length (t)
1179 tree t;
1181 tree tail;
1182 int len = 0;
1184 for (tail = t; tail; tail = TREE_CHAIN (tail))
1185 len++;
1187 return len;
1190 /* Returns the number of FIELD_DECLs in TYPE. */
1193 fields_length (type)
1194 tree type;
1196 tree t = TYPE_FIELDS (type);
1197 int count = 0;
1199 for (; t; t = TREE_CHAIN (t))
1200 if (TREE_CODE (t) == FIELD_DECL)
1201 ++count;
1203 return count;
1206 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
1207 by modifying the last node in chain 1 to point to chain 2.
1208 This is the Lisp primitive `nconc'. */
1210 tree
1211 chainon (op1, op2)
1212 tree op1, op2;
1215 if (op1)
1217 tree t1;
1218 #ifdef ENABLE_TREE_CHECKING
1219 tree t2;
1220 #endif
1222 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
1224 TREE_CHAIN (t1) = op2;
1225 #ifdef ENABLE_TREE_CHECKING
1226 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
1227 if (t2 == t1)
1228 abort (); /* Circularity created. */
1229 #endif
1230 return op1;
1232 else
1233 return op2;
1236 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
1238 tree
1239 tree_last (chain)
1240 tree chain;
1242 tree next;
1243 if (chain)
1244 while ((next = TREE_CHAIN (chain)))
1245 chain = next;
1246 return chain;
1249 /* Reverse the order of elements in the chain T,
1250 and return the new head of the chain (old last element). */
1252 tree
1253 nreverse (t)
1254 tree t;
1256 tree prev = 0, decl, next;
1257 for (decl = t; decl; decl = next)
1259 next = TREE_CHAIN (decl);
1260 TREE_CHAIN (decl) = prev;
1261 prev = decl;
1263 return prev;
1266 /* Given a chain CHAIN of tree nodes,
1267 construct and return a list of those nodes. */
1269 tree
1270 listify (chain)
1271 tree chain;
1273 tree result = NULL_TREE;
1274 tree in_tail = chain;
1275 tree out_tail = NULL_TREE;
1277 while (in_tail)
1279 tree next = tree_cons (NULL_TREE, in_tail, NULL_TREE);
1280 if (out_tail)
1281 TREE_CHAIN (out_tail) = next;
1282 else
1283 result = next;
1284 out_tail = next;
1285 in_tail = TREE_CHAIN (in_tail);
1288 return result;
1291 /* Return a newly created TREE_LIST node whose
1292 purpose and value fields are PARM and VALUE. */
1294 tree
1295 build_tree_list (parm, value)
1296 tree parm, value;
1298 tree t = make_node (TREE_LIST);
1299 TREE_PURPOSE (t) = parm;
1300 TREE_VALUE (t) = value;
1301 return t;
1304 /* Return a newly created TREE_LIST node whose
1305 purpose and value fields are PARM and VALUE
1306 and whose TREE_CHAIN is CHAIN. */
1308 tree
1309 tree_cons (purpose, value, chain)
1310 tree purpose, value, chain;
1312 tree node;
1314 node = ggc_alloc_tree (sizeof (struct tree_list));
1316 memset (node, 0, sizeof (struct tree_common));
1318 #ifdef GATHER_STATISTICS
1319 tree_node_counts[(int) x_kind]++;
1320 tree_node_sizes[(int) x_kind] += sizeof (struct tree_list);
1321 #endif
1323 TREE_SET_CODE (node, TREE_LIST);
1324 TREE_CHAIN (node) = chain;
1325 TREE_PURPOSE (node) = purpose;
1326 TREE_VALUE (node) = value;
1327 return node;
1331 /* Return the size nominally occupied by an object of type TYPE
1332 when it resides in memory. The value is measured in units of bytes,
1333 and its data type is that normally used for type sizes
1334 (which is the first type created by make_signed_type or
1335 make_unsigned_type). */
1337 tree
1338 size_in_bytes (type)
1339 tree type;
1341 tree t;
1343 if (type == error_mark_node)
1344 return integer_zero_node;
1346 type = TYPE_MAIN_VARIANT (type);
1347 t = TYPE_SIZE_UNIT (type);
1349 if (t == 0)
1351 incomplete_type_error (NULL_TREE, type);
1352 return size_zero_node;
1355 if (TREE_CODE (t) == INTEGER_CST)
1356 force_fit_type (t, 0);
1358 return t;
1361 /* Return the size of TYPE (in bytes) as a wide integer
1362 or return -1 if the size can vary or is larger than an integer. */
1364 HOST_WIDE_INT
1365 int_size_in_bytes (type)
1366 tree type;
1368 tree t;
1370 if (type == error_mark_node)
1371 return 0;
1373 type = TYPE_MAIN_VARIANT (type);
1374 t = TYPE_SIZE_UNIT (type);
1375 if (t == 0
1376 || TREE_CODE (t) != INTEGER_CST
1377 || TREE_OVERFLOW (t)
1378 || TREE_INT_CST_HIGH (t) != 0
1379 /* If the result would appear negative, it's too big to represent. */
1380 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
1381 return -1;
1383 return TREE_INT_CST_LOW (t);
1386 /* Return the bit position of FIELD, in bits from the start of the record.
1387 This is a tree of type bitsizetype. */
1389 tree
1390 bit_position (field)
1391 tree field;
1394 return bit_from_pos (DECL_FIELD_OFFSET (field),
1395 DECL_FIELD_BIT_OFFSET (field));
1398 /* Likewise, but return as an integer. Abort if it cannot be represented
1399 in that way (since it could be a signed value, we don't have the option
1400 of returning -1 like int_size_in_byte can. */
1402 HOST_WIDE_INT
1403 int_bit_position (field)
1404 tree field;
1406 return tree_low_cst (bit_position (field), 0);
1409 /* Return the byte position of FIELD, in bytes from the start of the record.
1410 This is a tree of type sizetype. */
1412 tree
1413 byte_position (field)
1414 tree field;
1416 return byte_from_pos (DECL_FIELD_OFFSET (field),
1417 DECL_FIELD_BIT_OFFSET (field));
1420 /* Likewise, but return as an integer. Abort if it cannot be represented
1421 in that way (since it could be a signed value, we don't have the option
1422 of returning -1 like int_size_in_byte can. */
1424 HOST_WIDE_INT
1425 int_byte_position (field)
1426 tree field;
1428 return tree_low_cst (byte_position (field), 0);
1431 /* Return the strictest alignment, in bits, that T is known to have. */
1433 unsigned int
1434 expr_align (t)
1435 tree t;
1437 unsigned int align0, align1;
1439 switch (TREE_CODE (t))
1441 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
1442 /* If we have conversions, we know that the alignment of the
1443 object must meet each of the alignments of the types. */
1444 align0 = expr_align (TREE_OPERAND (t, 0));
1445 align1 = TYPE_ALIGN (TREE_TYPE (t));
1446 return MAX (align0, align1);
1448 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
1449 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
1450 case WITH_RECORD_EXPR: case CLEANUP_POINT_EXPR: case UNSAVE_EXPR:
1451 /* These don't change the alignment of an object. */
1452 return expr_align (TREE_OPERAND (t, 0));
1454 case COND_EXPR:
1455 /* The best we can do is say that the alignment is the least aligned
1456 of the two arms. */
1457 align0 = expr_align (TREE_OPERAND (t, 1));
1458 align1 = expr_align (TREE_OPERAND (t, 2));
1459 return MIN (align0, align1);
1461 case LABEL_DECL: case CONST_DECL:
1462 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
1463 if (DECL_ALIGN (t) != 0)
1464 return DECL_ALIGN (t);
1465 break;
1467 case FUNCTION_DECL:
1468 return FUNCTION_BOUNDARY;
1470 default:
1471 break;
1474 /* Otherwise take the alignment from that of the type. */
1475 return TYPE_ALIGN (TREE_TYPE (t));
1478 /* Return, as a tree node, the number of elements for TYPE (which is an
1479 ARRAY_TYPE) minus one. This counts only elements of the top array. */
1481 tree
1482 array_type_nelts (type)
1483 tree type;
1485 tree index_type, min, max;
1487 /* If they did it with unspecified bounds, then we should have already
1488 given an error about it before we got here. */
1489 if (! TYPE_DOMAIN (type))
1490 return error_mark_node;
1492 index_type = TYPE_DOMAIN (type);
1493 min = TYPE_MIN_VALUE (index_type);
1494 max = TYPE_MAX_VALUE (index_type);
1496 return (integer_zerop (min)
1497 ? max
1498 : fold (build (MINUS_EXPR, TREE_TYPE (max), max, min)));
1501 /* Return nonzero if arg is static -- a reference to an object in
1502 static storage. This is not the same as the C meaning of `static'. */
1505 staticp (arg)
1506 tree arg;
1508 switch (TREE_CODE (arg))
1510 case FUNCTION_DECL:
1511 /* Nested functions aren't static, since taking their address
1512 involves a trampoline. */
1513 return (decl_function_context (arg) == 0 || DECL_NO_STATIC_CHAIN (arg))
1514 && ! DECL_NON_ADDR_CONST_P (arg);
1516 case VAR_DECL:
1517 return (TREE_STATIC (arg) || DECL_EXTERNAL (arg))
1518 && ! DECL_NON_ADDR_CONST_P (arg);
1520 case CONSTRUCTOR:
1521 return TREE_STATIC (arg);
1523 case LABEL_DECL:
1524 case STRING_CST:
1525 return 1;
1527 /* If we are referencing a bitfield, we can't evaluate an
1528 ADDR_EXPR at compile time and so it isn't a constant. */
1529 case COMPONENT_REF:
1530 return (! DECL_BIT_FIELD (TREE_OPERAND (arg, 1))
1531 && staticp (TREE_OPERAND (arg, 0)));
1533 case BIT_FIELD_REF:
1534 return 0;
1536 #if 0
1537 /* This case is technically correct, but results in setting
1538 TREE_CONSTANT on ADDR_EXPRs that cannot be evaluated at
1539 compile time. */
1540 case INDIRECT_REF:
1541 return TREE_CONSTANT (TREE_OPERAND (arg, 0));
1542 #endif
1544 case ARRAY_REF:
1545 case ARRAY_RANGE_REF:
1546 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
1547 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
1548 return staticp (TREE_OPERAND (arg, 0));
1550 default:
1551 if ((unsigned int) TREE_CODE (arg)
1552 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
1553 return (*lang_hooks.staticp) (arg);
1554 else
1555 return 0;
1559 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
1560 Do this to any expression which may be used in more than one place,
1561 but must be evaluated only once.
1563 Normally, expand_expr would reevaluate the expression each time.
1564 Calling save_expr produces something that is evaluated and recorded
1565 the first time expand_expr is called on it. Subsequent calls to
1566 expand_expr just reuse the recorded value.
1568 The call to expand_expr that generates code that actually computes
1569 the value is the first call *at compile time*. Subsequent calls
1570 *at compile time* generate code to use the saved value.
1571 This produces correct result provided that *at run time* control
1572 always flows through the insns made by the first expand_expr
1573 before reaching the other places where the save_expr was evaluated.
1574 You, the caller of save_expr, must make sure this is so.
1576 Constants, and certain read-only nodes, are returned with no
1577 SAVE_EXPR because that is safe. Expressions containing placeholders
1578 are not touched; see tree.def for an explanation of what these
1579 are used for. */
1581 tree
1582 save_expr (expr)
1583 tree expr;
1585 tree t = fold (expr);
1586 tree inner;
1588 /* We don't care about whether this can be used as an lvalue in this
1589 context. */
1590 while (TREE_CODE (t) == NON_LVALUE_EXPR)
1591 t = TREE_OPERAND (t, 0);
1593 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
1594 a constant, it will be more efficient to not make another SAVE_EXPR since
1595 it will allow better simplification and GCSE will be able to merge the
1596 computations if they actualy occur. */
1597 for (inner = t;
1598 (TREE_CODE_CLASS (TREE_CODE (inner)) == '1'
1599 || (TREE_CODE_CLASS (TREE_CODE (inner)) == '2'
1600 && TREE_CONSTANT (TREE_OPERAND (inner, 1))));
1601 inner = TREE_OPERAND (inner, 0))
1604 /* If the tree evaluates to a constant, then we don't want to hide that
1605 fact (i.e. this allows further folding, and direct checks for constants).
1606 However, a read-only object that has side effects cannot be bypassed.
1607 Since it is no problem to reevaluate literals, we just return the
1608 literal node. */
1609 if (TREE_CONSTANT (inner)
1610 || (TREE_READONLY (inner) && ! TREE_SIDE_EFFECTS (inner))
1611 || TREE_CODE (inner) == SAVE_EXPR || TREE_CODE (inner) == ERROR_MARK)
1612 return t;
1614 /* If T contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
1615 it means that the size or offset of some field of an object depends on
1616 the value within another field.
1618 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
1619 and some variable since it would then need to be both evaluated once and
1620 evaluated more than once. Front-ends must assure this case cannot
1621 happen by surrounding any such subexpressions in their own SAVE_EXPR
1622 and forcing evaluation at the proper time. */
1623 if (contains_placeholder_p (t))
1624 return t;
1626 t = build (SAVE_EXPR, TREE_TYPE (expr), t, current_function_decl, NULL_TREE);
1628 /* This expression might be placed ahead of a jump to ensure that the
1629 value was computed on both sides of the jump. So make sure it isn't
1630 eliminated as dead. */
1631 TREE_SIDE_EFFECTS (t) = 1;
1632 TREE_READONLY (t) = 1;
1633 return t;
1636 /* Arrange for an expression to be expanded multiple independent
1637 times. This is useful for cleanup actions, as the backend can
1638 expand them multiple times in different places. */
1640 tree
1641 unsave_expr (expr)
1642 tree expr;
1644 tree t;
1646 /* If this is already protected, no sense in protecting it again. */
1647 if (TREE_CODE (expr) == UNSAVE_EXPR)
1648 return expr;
1650 t = build1 (UNSAVE_EXPR, TREE_TYPE (expr), expr);
1651 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (expr);
1652 return t;
1655 /* Returns the index of the first non-tree operand for CODE, or the number
1656 of operands if all are trees. */
1659 first_rtl_op (code)
1660 enum tree_code code;
1662 switch (code)
1664 case SAVE_EXPR:
1665 return 2;
1666 case GOTO_SUBROUTINE_EXPR:
1667 case RTL_EXPR:
1668 return 0;
1669 case WITH_CLEANUP_EXPR:
1670 return 2;
1671 case METHOD_CALL_EXPR:
1672 return 3;
1673 default:
1674 return TREE_CODE_LENGTH (code);
1678 /* Perform any modifications to EXPR required when it is unsaved. Does
1679 not recurse into EXPR's subtrees. */
1681 void
1682 unsave_expr_1 (expr)
1683 tree expr;
1685 switch (TREE_CODE (expr))
1687 case SAVE_EXPR:
1688 if (! SAVE_EXPR_PERSISTENT_P (expr))
1689 SAVE_EXPR_RTL (expr) = 0;
1690 break;
1692 case TARGET_EXPR:
1693 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
1694 It's OK for this to happen if it was part of a subtree that
1695 isn't immediately expanded, such as operand 2 of another
1696 TARGET_EXPR. */
1697 if (TREE_OPERAND (expr, 1))
1698 break;
1700 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
1701 TREE_OPERAND (expr, 3) = NULL_TREE;
1702 break;
1704 case RTL_EXPR:
1705 /* I don't yet know how to emit a sequence multiple times. */
1706 if (RTL_EXPR_SEQUENCE (expr) != 0)
1707 abort ();
1708 break;
1710 default:
1711 if (lang_unsave_expr_now != 0)
1712 (*lang_unsave_expr_now) (expr);
1713 break;
1717 /* Helper function for unsave_expr_now. */
1719 static void
1720 unsave_expr_now_r (expr)
1721 tree expr;
1723 enum tree_code code;
1725 /* There's nothing to do for NULL_TREE. */
1726 if (expr == 0)
1727 return;
1729 unsave_expr_1 (expr);
1731 code = TREE_CODE (expr);
1732 switch (TREE_CODE_CLASS (code))
1734 case 'c': /* a constant */
1735 case 't': /* a type node */
1736 case 'd': /* A decl node */
1737 case 'b': /* A block node */
1738 break;
1740 case 'x': /* miscellaneous: e.g., identifier, TREE_LIST or ERROR_MARK. */
1741 if (code == TREE_LIST)
1743 unsave_expr_now_r (TREE_VALUE (expr));
1744 unsave_expr_now_r (TREE_CHAIN (expr));
1746 break;
1748 case 'e': /* an expression */
1749 case 'r': /* a reference */
1750 case 's': /* an expression with side effects */
1751 case '<': /* a comparison expression */
1752 case '2': /* a binary arithmetic expression */
1753 case '1': /* a unary arithmetic expression */
1755 int i;
1757 for (i = first_rtl_op (code) - 1; i >= 0; i--)
1758 unsave_expr_now_r (TREE_OPERAND (expr, i));
1760 break;
1762 default:
1763 abort ();
1767 /* Modify a tree in place so that all the evaluate only once things
1768 are cleared out. Return the EXPR given. */
1770 tree
1771 unsave_expr_now (expr)
1772 tree expr;
1774 if (lang_unsave!= 0)
1775 (*lang_unsave) (&expr);
1776 else
1777 unsave_expr_now_r (expr);
1779 return expr;
1782 /* Return 0 if it is safe to evaluate EXPR multiple times,
1783 return 1 if it is safe if EXPR is unsaved afterward, or
1784 return 2 if it is completely unsafe.
1786 This assumes that CALL_EXPRs and TARGET_EXPRs are never replicated in
1787 an expression tree, so that it safe to unsave them and the surrounding
1788 context will be correct.
1790 SAVE_EXPRs basically *only* appear replicated in an expression tree,
1791 occasionally across the whole of a function. It is therefore only
1792 safe to unsave a SAVE_EXPR if you know that all occurrences appear
1793 below the UNSAVE_EXPR.
1795 RTL_EXPRs consume their rtl during evaluation. It is therefore
1796 never possible to unsave them. */
1799 unsafe_for_reeval (expr)
1800 tree expr;
1802 int unsafeness = 0;
1803 enum tree_code code;
1804 int i, tmp;
1805 tree exp;
1806 int first_rtl;
1808 if (expr == NULL_TREE)
1809 return 1;
1811 code = TREE_CODE (expr);
1812 first_rtl = first_rtl_op (code);
1814 switch (code)
1816 case SAVE_EXPR:
1817 case RTL_EXPR:
1818 return 2;
1820 case TREE_LIST:
1821 for (exp = expr; exp != 0; exp = TREE_CHAIN (exp))
1823 tmp = unsafe_for_reeval (TREE_VALUE (exp));
1824 unsafeness = MAX (tmp, unsafeness);
1827 return unsafeness;
1829 case CALL_EXPR:
1830 tmp = unsafe_for_reeval (TREE_OPERAND (expr, 1));
1831 return MAX (tmp, 1);
1833 case TARGET_EXPR:
1834 unsafeness = 1;
1835 break;
1837 default:
1838 if (lang_unsafe_for_reeval != 0)
1840 tmp = (*lang_unsafe_for_reeval) (expr);
1841 if (tmp >= 0)
1842 return tmp;
1844 break;
1847 switch (TREE_CODE_CLASS (code))
1849 case 'c': /* a constant */
1850 case 't': /* a type node */
1851 case 'x': /* something random, like an identifier or an ERROR_MARK. */
1852 case 'd': /* A decl node */
1853 case 'b': /* A block node */
1854 return 0;
1856 case 'e': /* an expression */
1857 case 'r': /* a reference */
1858 case 's': /* an expression with side effects */
1859 case '<': /* a comparison expression */
1860 case '2': /* a binary arithmetic expression */
1861 case '1': /* a unary arithmetic expression */
1862 for (i = first_rtl - 1; i >= 0; i--)
1864 tmp = unsafe_for_reeval (TREE_OPERAND (expr, i));
1865 unsafeness = MAX (tmp, unsafeness);
1868 return unsafeness;
1870 default:
1871 return 2;
1875 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
1876 or offset that depends on a field within a record. */
1879 contains_placeholder_p (exp)
1880 tree exp;
1882 enum tree_code code;
1883 int result;
1885 if (!exp)
1886 return 0;
1888 /* If we have a WITH_RECORD_EXPR, it "cancels" any PLACEHOLDER_EXPR
1889 in it since it is supplying a value for it. */
1890 code = TREE_CODE (exp);
1891 if (code == WITH_RECORD_EXPR)
1892 return 0;
1893 else if (code == PLACEHOLDER_EXPR)
1894 return 1;
1896 switch (TREE_CODE_CLASS (code))
1898 case 'r':
1899 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
1900 position computations since they will be converted into a
1901 WITH_RECORD_EXPR involving the reference, which will assume
1902 here will be valid. */
1903 return contains_placeholder_p (TREE_OPERAND (exp, 0));
1905 case 'x':
1906 if (code == TREE_LIST)
1907 return (contains_placeholder_p (TREE_VALUE (exp))
1908 || (TREE_CHAIN (exp) != 0
1909 && contains_placeholder_p (TREE_CHAIN (exp))));
1910 break;
1912 case '1':
1913 case '2': case '<':
1914 case 'e':
1915 switch (code)
1917 case COMPOUND_EXPR:
1918 /* Ignoring the first operand isn't quite right, but works best. */
1919 return contains_placeholder_p (TREE_OPERAND (exp, 1));
1921 case RTL_EXPR:
1922 case CONSTRUCTOR:
1923 return 0;
1925 case COND_EXPR:
1926 return (contains_placeholder_p (TREE_OPERAND (exp, 0))
1927 || contains_placeholder_p (TREE_OPERAND (exp, 1))
1928 || contains_placeholder_p (TREE_OPERAND (exp, 2)));
1930 case SAVE_EXPR:
1931 /* If we already know this doesn't have a placeholder, don't
1932 check again. */
1933 if (SAVE_EXPR_NOPLACEHOLDER (exp) || SAVE_EXPR_RTL (exp) != 0)
1934 return 0;
1936 SAVE_EXPR_NOPLACEHOLDER (exp) = 1;
1937 result = contains_placeholder_p (TREE_OPERAND (exp, 0));
1938 if (result)
1939 SAVE_EXPR_NOPLACEHOLDER (exp) = 0;
1941 return result;
1943 case CALL_EXPR:
1944 return (TREE_OPERAND (exp, 1) != 0
1945 && contains_placeholder_p (TREE_OPERAND (exp, 1)));
1947 default:
1948 break;
1951 switch (TREE_CODE_LENGTH (code))
1953 case 1:
1954 return contains_placeholder_p (TREE_OPERAND (exp, 0));
1955 case 2:
1956 return (contains_placeholder_p (TREE_OPERAND (exp, 0))
1957 || contains_placeholder_p (TREE_OPERAND (exp, 1)));
1958 default:
1959 return 0;
1962 default:
1963 return 0;
1965 return 0;
1968 /* Return 1 if EXP contains any expressions that produce cleanups for an
1969 outer scope to deal with. Used by fold. */
1972 has_cleanups (exp)
1973 tree exp;
1975 int i, nops, cmp;
1977 if (! TREE_SIDE_EFFECTS (exp))
1978 return 0;
1980 switch (TREE_CODE (exp))
1982 case TARGET_EXPR:
1983 case GOTO_SUBROUTINE_EXPR:
1984 case WITH_CLEANUP_EXPR:
1985 return 1;
1987 case CLEANUP_POINT_EXPR:
1988 return 0;
1990 case CALL_EXPR:
1991 for (exp = TREE_OPERAND (exp, 1); exp; exp = TREE_CHAIN (exp))
1993 cmp = has_cleanups (TREE_VALUE (exp));
1994 if (cmp)
1995 return cmp;
1997 return 0;
1999 default:
2000 break;
2003 /* This general rule works for most tree codes. All exceptions should be
2004 handled above. If this is a language-specific tree code, we can't
2005 trust what might be in the operand, so say we don't know
2006 the situation. */
2007 if ((int) TREE_CODE (exp) >= (int) LAST_AND_UNUSED_TREE_CODE)
2008 return -1;
2010 nops = first_rtl_op (TREE_CODE (exp));
2011 for (i = 0; i < nops; i++)
2012 if (TREE_OPERAND (exp, i) != 0)
2014 int type = TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (exp, i)));
2015 if (type == 'e' || type == '<' || type == '1' || type == '2'
2016 || type == 'r' || type == 's')
2018 cmp = has_cleanups (TREE_OPERAND (exp, i));
2019 if (cmp)
2020 return cmp;
2024 return 0;
2027 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
2028 return a tree with all occurrences of references to F in a
2029 PLACEHOLDER_EXPR replaced by R. Note that we assume here that EXP
2030 contains only arithmetic expressions or a CALL_EXPR with a
2031 PLACEHOLDER_EXPR occurring only in its arglist. */
2033 tree
2034 substitute_in_expr (exp, f, r)
2035 tree exp;
2036 tree f;
2037 tree r;
2039 enum tree_code code = TREE_CODE (exp);
2040 tree op0, op1, op2;
2041 tree new;
2042 tree inner;
2044 switch (TREE_CODE_CLASS (code))
2046 case 'c':
2047 case 'd':
2048 return exp;
2050 case 'x':
2051 if (code == PLACEHOLDER_EXPR)
2052 return exp;
2053 else if (code == TREE_LIST)
2055 op0 = (TREE_CHAIN (exp) == 0
2056 ? 0 : substitute_in_expr (TREE_CHAIN (exp), f, r));
2057 op1 = substitute_in_expr (TREE_VALUE (exp), f, r);
2058 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2059 return exp;
2061 return tree_cons (TREE_PURPOSE (exp), op1, op0);
2064 abort ();
2066 case '1':
2067 case '2':
2068 case '<':
2069 case 'e':
2070 switch (TREE_CODE_LENGTH (code))
2072 case 1:
2073 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2074 if (op0 == TREE_OPERAND (exp, 0))
2075 return exp;
2077 if (code == NON_LVALUE_EXPR)
2078 return op0;
2080 new = fold (build1 (code, TREE_TYPE (exp), op0));
2081 break;
2083 case 2:
2084 /* An RTL_EXPR cannot contain a PLACEHOLDER_EXPR; a CONSTRUCTOR
2085 could, but we don't support it. */
2086 if (code == RTL_EXPR)
2087 return exp;
2088 else if (code == CONSTRUCTOR)
2089 abort ();
2091 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2092 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
2093 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2094 return exp;
2096 new = fold (build (code, TREE_TYPE (exp), op0, op1));
2097 break;
2099 case 3:
2100 /* It cannot be that anything inside a SAVE_EXPR contains a
2101 PLACEHOLDER_EXPR. */
2102 if (code == SAVE_EXPR)
2103 return exp;
2105 else if (code == CALL_EXPR)
2107 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
2108 if (op1 == TREE_OPERAND (exp, 1))
2109 return exp;
2111 return build (code, TREE_TYPE (exp),
2112 TREE_OPERAND (exp, 0), op1, NULL_TREE);
2115 else if (code != COND_EXPR)
2116 abort ();
2118 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2119 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
2120 op2 = substitute_in_expr (TREE_OPERAND (exp, 2), f, r);
2121 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2122 && op2 == TREE_OPERAND (exp, 2))
2123 return exp;
2125 new = fold (build (code, TREE_TYPE (exp), op0, op1, op2));
2126 break;
2128 default:
2129 abort ();
2132 break;
2134 case 'r':
2135 switch (code)
2137 case COMPONENT_REF:
2138 /* If this expression is getting a value from a PLACEHOLDER_EXPR
2139 and it is the right field, replace it with R. */
2140 for (inner = TREE_OPERAND (exp, 0);
2141 TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
2142 inner = TREE_OPERAND (inner, 0))
2144 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2145 && TREE_OPERAND (exp, 1) == f)
2146 return r;
2148 /* If this expression hasn't been completed let, leave it
2149 alone. */
2150 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2151 && TREE_TYPE (inner) == 0)
2152 return exp;
2154 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2155 if (op0 == TREE_OPERAND (exp, 0))
2156 return exp;
2158 new = fold (build (code, TREE_TYPE (exp), op0,
2159 TREE_OPERAND (exp, 1)));
2160 break;
2162 case BIT_FIELD_REF:
2163 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2164 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
2165 op2 = substitute_in_expr (TREE_OPERAND (exp, 2), f, r);
2166 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2167 && op2 == TREE_OPERAND (exp, 2))
2168 return exp;
2170 new = fold (build (code, TREE_TYPE (exp), op0, op1, op2));
2171 break;
2173 case INDIRECT_REF:
2174 case BUFFER_REF:
2175 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2176 if (op0 == TREE_OPERAND (exp, 0))
2177 return exp;
2179 new = fold (build1 (code, TREE_TYPE (exp), op0));
2180 break;
2182 default:
2183 abort ();
2185 break;
2187 default:
2188 abort ();
2191 TREE_READONLY (new) = TREE_READONLY (exp);
2192 return new;
2195 /* Stabilize a reference so that we can use it any number of times
2196 without causing its operands to be evaluated more than once.
2197 Returns the stabilized reference. This works by means of save_expr,
2198 so see the caveats in the comments about save_expr.
2200 Also allows conversion expressions whose operands are references.
2201 Any other kind of expression is returned unchanged. */
2203 tree
2204 stabilize_reference (ref)
2205 tree ref;
2207 tree result;
2208 enum tree_code code = TREE_CODE (ref);
2210 switch (code)
2212 case VAR_DECL:
2213 case PARM_DECL:
2214 case RESULT_DECL:
2215 /* No action is needed in this case. */
2216 return ref;
2218 case NOP_EXPR:
2219 case CONVERT_EXPR:
2220 case FLOAT_EXPR:
2221 case FIX_TRUNC_EXPR:
2222 case FIX_FLOOR_EXPR:
2223 case FIX_ROUND_EXPR:
2224 case FIX_CEIL_EXPR:
2225 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2226 break;
2228 case INDIRECT_REF:
2229 result = build_nt (INDIRECT_REF,
2230 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2231 break;
2233 case COMPONENT_REF:
2234 result = build_nt (COMPONENT_REF,
2235 stabilize_reference (TREE_OPERAND (ref, 0)),
2236 TREE_OPERAND (ref, 1));
2237 break;
2239 case BIT_FIELD_REF:
2240 result = build_nt (BIT_FIELD_REF,
2241 stabilize_reference (TREE_OPERAND (ref, 0)),
2242 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2243 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2244 break;
2246 case ARRAY_REF:
2247 result = build_nt (ARRAY_REF,
2248 stabilize_reference (TREE_OPERAND (ref, 0)),
2249 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2250 break;
2252 case ARRAY_RANGE_REF:
2253 result = build_nt (ARRAY_RANGE_REF,
2254 stabilize_reference (TREE_OPERAND (ref, 0)),
2255 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2256 break;
2258 case COMPOUND_EXPR:
2259 /* We cannot wrap the first expression in a SAVE_EXPR, as then
2260 it wouldn't be ignored. This matters when dealing with
2261 volatiles. */
2262 return stabilize_reference_1 (ref);
2264 case RTL_EXPR:
2265 result = build1 (INDIRECT_REF, TREE_TYPE (ref),
2266 save_expr (build1 (ADDR_EXPR,
2267 build_pointer_type (TREE_TYPE (ref)),
2268 ref)));
2269 break;
2271 /* If arg isn't a kind of lvalue we recognize, make no change.
2272 Caller should recognize the error for an invalid lvalue. */
2273 default:
2274 return ref;
2276 case ERROR_MARK:
2277 return error_mark_node;
2280 TREE_TYPE (result) = TREE_TYPE (ref);
2281 TREE_READONLY (result) = TREE_READONLY (ref);
2282 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
2283 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
2285 return result;
2288 /* Subroutine of stabilize_reference; this is called for subtrees of
2289 references. Any expression with side-effects must be put in a SAVE_EXPR
2290 to ensure that it is only evaluated once.
2292 We don't put SAVE_EXPR nodes around everything, because assigning very
2293 simple expressions to temporaries causes us to miss good opportunities
2294 for optimizations. Among other things, the opportunity to fold in the
2295 addition of a constant into an addressing mode often gets lost, e.g.
2296 "y[i+1] += x;". In general, we take the approach that we should not make
2297 an assignment unless we are forced into it - i.e., that any non-side effect
2298 operator should be allowed, and that cse should take care of coalescing
2299 multiple utterances of the same expression should that prove fruitful. */
2301 tree
2302 stabilize_reference_1 (e)
2303 tree e;
2305 tree result;
2306 enum tree_code code = TREE_CODE (e);
2308 /* We cannot ignore const expressions because it might be a reference
2309 to a const array but whose index contains side-effects. But we can
2310 ignore things that are actual constant or that already have been
2311 handled by this function. */
2313 if (TREE_CONSTANT (e) || code == SAVE_EXPR)
2314 return e;
2316 switch (TREE_CODE_CLASS (code))
2318 case 'x':
2319 case 't':
2320 case 'd':
2321 case 'b':
2322 case '<':
2323 case 's':
2324 case 'e':
2325 case 'r':
2326 /* If the expression has side-effects, then encase it in a SAVE_EXPR
2327 so that it will only be evaluated once. */
2328 /* The reference (r) and comparison (<) classes could be handled as
2329 below, but it is generally faster to only evaluate them once. */
2330 if (TREE_SIDE_EFFECTS (e))
2331 return save_expr (e);
2332 return e;
2334 case 'c':
2335 /* Constants need no processing. In fact, we should never reach
2336 here. */
2337 return e;
2339 case '2':
2340 /* Division is slow and tends to be compiled with jumps,
2341 especially the division by powers of 2 that is often
2342 found inside of an array reference. So do it just once. */
2343 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
2344 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
2345 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
2346 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
2347 return save_expr (e);
2348 /* Recursively stabilize each operand. */
2349 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
2350 stabilize_reference_1 (TREE_OPERAND (e, 1)));
2351 break;
2353 case '1':
2354 /* Recursively stabilize each operand. */
2355 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
2356 break;
2358 default:
2359 abort ();
2362 TREE_TYPE (result) = TREE_TYPE (e);
2363 TREE_READONLY (result) = TREE_READONLY (e);
2364 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
2365 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
2367 return result;
2370 /* Low-level constructors for expressions. */
2372 /* Build an expression of code CODE, data type TYPE,
2373 and operands as specified by the arguments ARG1 and following arguments.
2374 Expressions and reference nodes can be created this way.
2375 Constants, decls, types and misc nodes cannot be. */
2377 tree
2378 build VPARAMS ((enum tree_code code, tree tt, ...))
2380 tree t;
2381 int length;
2382 int i;
2383 int fro;
2384 int constant;
2386 VA_OPEN (p, tt);
2387 VA_FIXEDARG (p, enum tree_code, code);
2388 VA_FIXEDARG (p, tree, tt);
2390 t = make_node (code);
2391 length = TREE_CODE_LENGTH (code);
2392 TREE_TYPE (t) = tt;
2394 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
2395 result based on those same flags for the arguments. But if the
2396 arguments aren't really even `tree' expressions, we shouldn't be trying
2397 to do this. */
2398 fro = first_rtl_op (code);
2400 /* Expressions without side effects may be constant if their
2401 arguments are as well. */
2402 constant = (TREE_CODE_CLASS (code) == '<'
2403 || TREE_CODE_CLASS (code) == '1'
2404 || TREE_CODE_CLASS (code) == '2'
2405 || TREE_CODE_CLASS (code) == 'c');
2407 if (length == 2)
2409 /* This is equivalent to the loop below, but faster. */
2410 tree arg0 = va_arg (p, tree);
2411 tree arg1 = va_arg (p, tree);
2413 TREE_OPERAND (t, 0) = arg0;
2414 TREE_OPERAND (t, 1) = arg1;
2415 TREE_READONLY (t) = 1;
2416 if (arg0 && fro > 0)
2418 if (TREE_SIDE_EFFECTS (arg0))
2419 TREE_SIDE_EFFECTS (t) = 1;
2420 if (!TREE_READONLY (arg0))
2421 TREE_READONLY (t) = 0;
2422 if (!TREE_CONSTANT (arg0))
2423 constant = 0;
2426 if (arg1 && fro > 1)
2428 if (TREE_SIDE_EFFECTS (arg1))
2429 TREE_SIDE_EFFECTS (t) = 1;
2430 if (!TREE_READONLY (arg1))
2431 TREE_READONLY (t) = 0;
2432 if (!TREE_CONSTANT (arg1))
2433 constant = 0;
2436 else if (length == 1)
2438 tree arg0 = va_arg (p, tree);
2440 /* The only one-operand cases we handle here are those with side-effects.
2441 Others are handled with build1. So don't bother checked if the
2442 arg has side-effects since we'll already have set it.
2444 ??? This really should use build1 too. */
2445 if (TREE_CODE_CLASS (code) != 's')
2446 abort ();
2447 TREE_OPERAND (t, 0) = arg0;
2449 else
2451 for (i = 0; i < length; i++)
2453 tree operand = va_arg (p, tree);
2455 TREE_OPERAND (t, i) = operand;
2456 if (operand && fro > i)
2458 if (TREE_SIDE_EFFECTS (operand))
2459 TREE_SIDE_EFFECTS (t) = 1;
2460 if (!TREE_CONSTANT (operand))
2461 constant = 0;
2465 VA_CLOSE (p);
2467 TREE_CONSTANT (t) = constant;
2468 return t;
2471 /* Same as above, but only builds for unary operators.
2472 Saves lions share of calls to `build'; cuts down use
2473 of varargs, which is expensive for RISC machines. */
2475 tree
2476 build1 (code, type, node)
2477 enum tree_code code;
2478 tree type;
2479 tree node;
2481 int length;
2482 #ifdef GATHER_STATISTICS
2483 tree_node_kind kind;
2484 #endif
2485 tree t;
2487 #ifdef GATHER_STATISTICS
2488 if (TREE_CODE_CLASS (code) == 'r')
2489 kind = r_kind;
2490 else
2491 kind = e_kind;
2492 #endif
2494 #ifdef ENABLE_CHECKING
2495 if (TREE_CODE_CLASS (code) == '2'
2496 || TREE_CODE_CLASS (code) == '<'
2497 || TREE_CODE_LENGTH (code) != 1)
2498 abort ();
2499 #endif /* ENABLE_CHECKING */
2501 length = sizeof (struct tree_exp);
2503 t = ggc_alloc_tree (length);
2505 memset ((PTR) t, 0, sizeof (struct tree_common));
2507 #ifdef GATHER_STATISTICS
2508 tree_node_counts[(int) kind]++;
2509 tree_node_sizes[(int) kind] += length;
2510 #endif
2512 TREE_SET_CODE (t, code);
2514 TREE_TYPE (t) = type;
2515 TREE_COMPLEXITY (t) = 0;
2516 TREE_OPERAND (t, 0) = node;
2517 if (node && first_rtl_op (code) != 0)
2519 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
2520 TREE_READONLY (t) = TREE_READONLY (node);
2523 switch (code)
2525 case INIT_EXPR:
2526 case MODIFY_EXPR:
2527 case VA_ARG_EXPR:
2528 case RTL_EXPR:
2529 case PREDECREMENT_EXPR:
2530 case PREINCREMENT_EXPR:
2531 case POSTDECREMENT_EXPR:
2532 case POSTINCREMENT_EXPR:
2533 /* All of these have side-effects, no matter what their
2534 operands are. */
2535 TREE_SIDE_EFFECTS (t) = 1;
2536 TREE_READONLY (t) = 0;
2537 break;
2539 case INDIRECT_REF:
2540 /* Whether a dereference is readonly has nothing to do with whether
2541 its operand is readonly. */
2542 TREE_READONLY (t) = 0;
2543 break;
2545 default:
2546 if (TREE_CODE_CLASS (code) == '1' && node && TREE_CONSTANT (node))
2547 TREE_CONSTANT (t) = 1;
2548 break;
2551 return t;
2554 /* Similar except don't specify the TREE_TYPE
2555 and leave the TREE_SIDE_EFFECTS as 0.
2556 It is permissible for arguments to be null,
2557 or even garbage if their values do not matter. */
2559 tree
2560 build_nt VPARAMS ((enum tree_code code, ...))
2562 tree t;
2563 int length;
2564 int i;
2566 VA_OPEN (p, code);
2567 VA_FIXEDARG (p, enum tree_code, code);
2569 t = make_node (code);
2570 length = TREE_CODE_LENGTH (code);
2572 for (i = 0; i < length; i++)
2573 TREE_OPERAND (t, i) = va_arg (p, tree);
2575 VA_CLOSE (p);
2576 return t;
2579 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
2580 We do NOT enter this node in any sort of symbol table.
2582 layout_decl is used to set up the decl's storage layout.
2583 Other slots are initialized to 0 or null pointers. */
2585 tree
2586 build_decl (code, name, type)
2587 enum tree_code code;
2588 tree name, type;
2590 tree t;
2592 t = make_node (code);
2594 /* if (type == error_mark_node)
2595 type = integer_type_node; */
2596 /* That is not done, deliberately, so that having error_mark_node
2597 as the type can suppress useless errors in the use of this variable. */
2599 DECL_NAME (t) = name;
2600 TREE_TYPE (t) = type;
2602 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
2603 layout_decl (t, 0);
2604 else if (code == FUNCTION_DECL)
2605 DECL_MODE (t) = FUNCTION_MODE;
2607 return t;
2610 /* BLOCK nodes are used to represent the structure of binding contours
2611 and declarations, once those contours have been exited and their contents
2612 compiled. This information is used for outputting debugging info. */
2614 tree
2615 build_block (vars, tags, subblocks, supercontext, chain)
2616 tree vars, tags ATTRIBUTE_UNUSED, subblocks, supercontext, chain;
2618 tree block = make_node (BLOCK);
2620 BLOCK_VARS (block) = vars;
2621 BLOCK_SUBBLOCKS (block) = subblocks;
2622 BLOCK_SUPERCONTEXT (block) = supercontext;
2623 BLOCK_CHAIN (block) = chain;
2624 return block;
2627 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
2628 location where an expression or an identifier were encountered. It
2629 is necessary for languages where the frontend parser will handle
2630 recursively more than one file (Java is one of them). */
2632 tree
2633 build_expr_wfl (node, file, line, col)
2634 tree node;
2635 const char *file;
2636 int line, col;
2638 static const char *last_file = 0;
2639 static tree last_filenode = NULL_TREE;
2640 tree wfl = make_node (EXPR_WITH_FILE_LOCATION);
2642 EXPR_WFL_NODE (wfl) = node;
2643 EXPR_WFL_SET_LINECOL (wfl, line, col);
2644 if (file != last_file)
2646 last_file = file;
2647 last_filenode = file ? get_identifier (file) : NULL_TREE;
2650 EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
2651 if (node)
2653 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
2654 TREE_TYPE (wfl) = TREE_TYPE (node);
2657 return wfl;
2660 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
2661 is ATTRIBUTE. */
2663 tree
2664 build_decl_attribute_variant (ddecl, attribute)
2665 tree ddecl, attribute;
2667 DECL_ATTRIBUTES (ddecl) = attribute;
2668 return ddecl;
2671 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
2672 is ATTRIBUTE.
2674 Record such modified types already made so we don't make duplicates. */
2676 tree
2677 build_type_attribute_variant (ttype, attribute)
2678 tree ttype, attribute;
2680 if ( ! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
2682 unsigned int hashcode;
2683 tree ntype;
2685 ntype = copy_node (ttype);
2687 TYPE_POINTER_TO (ntype) = 0;
2688 TYPE_REFERENCE_TO (ntype) = 0;
2689 TYPE_ATTRIBUTES (ntype) = attribute;
2691 /* Create a new main variant of TYPE. */
2692 TYPE_MAIN_VARIANT (ntype) = ntype;
2693 TYPE_NEXT_VARIANT (ntype) = 0;
2694 set_type_quals (ntype, TYPE_UNQUALIFIED);
2696 hashcode = (TYPE_HASH (TREE_CODE (ntype))
2697 + TYPE_HASH (TREE_TYPE (ntype))
2698 + attribute_hash_list (attribute));
2700 switch (TREE_CODE (ntype))
2702 case FUNCTION_TYPE:
2703 hashcode += TYPE_HASH (TYPE_ARG_TYPES (ntype));
2704 break;
2705 case ARRAY_TYPE:
2706 hashcode += TYPE_HASH (TYPE_DOMAIN (ntype));
2707 break;
2708 case INTEGER_TYPE:
2709 hashcode += TYPE_HASH (TYPE_MAX_VALUE (ntype));
2710 break;
2711 case REAL_TYPE:
2712 hashcode += TYPE_HASH (TYPE_PRECISION (ntype));
2713 break;
2714 default:
2715 break;
2718 ntype = type_hash_canon (hashcode, ntype);
2719 ttype = build_qualified_type (ntype, TYPE_QUALS (ttype));
2722 return ttype;
2725 /* Default value of targetm.comp_type_attributes that always returns 1. */
2728 default_comp_type_attributes (type1, type2)
2729 tree type1 ATTRIBUTE_UNUSED;
2730 tree type2 ATTRIBUTE_UNUSED;
2732 return 1;
2735 /* Default version of targetm.set_default_type_attributes that always does
2736 nothing. */
2738 void
2739 default_set_default_type_attributes (type)
2740 tree type ATTRIBUTE_UNUSED;
2744 /* Default version of targetm.insert_attributes that always does nothing. */
2745 void
2746 default_insert_attributes (decl, attr_ptr)
2747 tree decl ATTRIBUTE_UNUSED;
2748 tree *attr_ptr ATTRIBUTE_UNUSED;
2752 /* Default value of targetm.attribute_table that is empty. */
2753 const struct attribute_spec default_target_attribute_table[] =
2755 { NULL, 0, 0, false, false, false, NULL }
2758 /* Default value of targetm.function_attribute_inlinable_p that always
2759 returns false. */
2760 bool
2761 default_function_attribute_inlinable_p (fndecl)
2762 tree fndecl ATTRIBUTE_UNUSED;
2764 /* By default, functions with machine attributes cannot be inlined. */
2765 return false;
2768 /* Default value of targetm.ms_bitfield_layout_p that always returns
2769 false. */
2770 bool
2771 default_ms_bitfield_layout_p (record)
2772 tree record ATTRIBUTE_UNUSED;
2774 /* By default, GCC does not use the MS VC++ bitfield layout rules. */
2775 return false;
2778 /* Return non-zero if IDENT is a valid name for attribute ATTR,
2779 or zero if not.
2781 We try both `text' and `__text__', ATTR may be either one. */
2782 /* ??? It might be a reasonable simplification to require ATTR to be only
2783 `text'. One might then also require attribute lists to be stored in
2784 their canonicalized form. */
2787 is_attribute_p (attr, ident)
2788 const char *attr;
2789 tree ident;
2791 int ident_len, attr_len;
2792 const char *p;
2794 if (TREE_CODE (ident) != IDENTIFIER_NODE)
2795 return 0;
2797 if (strcmp (attr, IDENTIFIER_POINTER (ident)) == 0)
2798 return 1;
2800 p = IDENTIFIER_POINTER (ident);
2801 ident_len = strlen (p);
2802 attr_len = strlen (attr);
2804 /* If ATTR is `__text__', IDENT must be `text'; and vice versa. */
2805 if (attr[0] == '_')
2807 if (attr[1] != '_'
2808 || attr[attr_len - 2] != '_'
2809 || attr[attr_len - 1] != '_')
2810 abort ();
2811 if (ident_len == attr_len - 4
2812 && strncmp (attr + 2, p, attr_len - 4) == 0)
2813 return 1;
2815 else
2817 if (ident_len == attr_len + 4
2818 && p[0] == '_' && p[1] == '_'
2819 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
2820 && strncmp (attr, p + 2, attr_len) == 0)
2821 return 1;
2824 return 0;
2827 /* Given an attribute name and a list of attributes, return a pointer to the
2828 attribute's list element if the attribute is part of the list, or NULL_TREE
2829 if not found. If the attribute appears more than once, this only
2830 returns the first occurrence; the TREE_CHAIN of the return value should
2831 be passed back in if further occurrences are wanted. */
2833 tree
2834 lookup_attribute (attr_name, list)
2835 const char *attr_name;
2836 tree list;
2838 tree l;
2840 for (l = list; l; l = TREE_CHAIN (l))
2842 if (TREE_CODE (TREE_PURPOSE (l)) != IDENTIFIER_NODE)
2843 abort ();
2844 if (is_attribute_p (attr_name, TREE_PURPOSE (l)))
2845 return l;
2848 return NULL_TREE;
2851 /* Return an attribute list that is the union of a1 and a2. */
2853 tree
2854 merge_attributes (a1, a2)
2855 tree a1, a2;
2857 tree attributes;
2859 /* Either one unset? Take the set one. */
2861 if ((attributes = a1) == 0)
2862 attributes = a2;
2864 /* One that completely contains the other? Take it. */
2866 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
2868 if (attribute_list_contained (a2, a1))
2869 attributes = a2;
2870 else
2872 /* Pick the longest list, and hang on the other list. */
2874 if (list_length (a1) < list_length (a2))
2875 attributes = a2, a2 = a1;
2877 for (; a2 != 0; a2 = TREE_CHAIN (a2))
2879 tree a;
2880 for (a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
2881 attributes);
2882 a != NULL_TREE;
2883 a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
2884 TREE_CHAIN (a)))
2886 if (simple_cst_equal (TREE_VALUE (a), TREE_VALUE (a2)) == 1)
2887 break;
2889 if (a == NULL_TREE)
2891 a1 = copy_node (a2);
2892 TREE_CHAIN (a1) = attributes;
2893 attributes = a1;
2898 return attributes;
2901 /* Given types T1 and T2, merge their attributes and return
2902 the result. */
2904 tree
2905 merge_type_attributes (t1, t2)
2906 tree t1, t2;
2908 return merge_attributes (TYPE_ATTRIBUTES (t1),
2909 TYPE_ATTRIBUTES (t2));
2912 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
2913 the result. */
2915 tree
2916 merge_decl_attributes (olddecl, newdecl)
2917 tree olddecl, newdecl;
2919 return merge_attributes (DECL_ATTRIBUTES (olddecl),
2920 DECL_ATTRIBUTES (newdecl));
2923 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
2925 /* Specialization of merge_decl_attributes for various Windows targets.
2927 This handles the following situation:
2929 __declspec (dllimport) int foo;
2930 int foo;
2932 The second instance of `foo' nullifies the dllimport. */
2934 tree
2935 merge_dllimport_decl_attributes (old, new)
2936 tree old;
2937 tree new;
2939 tree a;
2940 int delete_dllimport_p;
2942 old = DECL_ATTRIBUTES (old);
2943 new = DECL_ATTRIBUTES (new);
2945 /* What we need to do here is remove from `old' dllimport if it doesn't
2946 appear in `new'. dllimport behaves like extern: if a declaration is
2947 marked dllimport and a definition appears later, then the object
2948 is not dllimport'd. */
2949 if (lookup_attribute ("dllimport", old) != NULL_TREE
2950 && lookup_attribute ("dllimport", new) == NULL_TREE)
2951 delete_dllimport_p = 1;
2952 else
2953 delete_dllimport_p = 0;
2955 a = merge_attributes (old, new);
2957 if (delete_dllimport_p)
2959 tree prev, t;
2961 /* Scan the list for dllimport and delete it. */
2962 for (prev = NULL_TREE, t = a; t; prev = t, t = TREE_CHAIN (t))
2964 if (is_attribute_p ("dllimport", TREE_PURPOSE (t)))
2966 if (prev == NULL_TREE)
2967 a = TREE_CHAIN (a);
2968 else
2969 TREE_CHAIN (prev) = TREE_CHAIN (t);
2970 break;
2975 return a;
2978 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
2980 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
2981 of the various TYPE_QUAL values. */
2983 static void
2984 set_type_quals (type, type_quals)
2985 tree type;
2986 int type_quals;
2988 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
2989 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
2990 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
2993 /* Return a version of the TYPE, qualified as indicated by the
2994 TYPE_QUALS, if one exists. If no qualified version exists yet,
2995 return NULL_TREE. */
2997 tree
2998 get_qualified_type (type, type_quals)
2999 tree type;
3000 int type_quals;
3002 tree t;
3004 /* Search the chain of variants to see if there is already one there just
3005 like the one we need to have. If so, use that existing one. We must
3006 preserve the TYPE_NAME, since there is code that depends on this. */
3007 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
3008 if (TYPE_QUALS (t) == type_quals && TYPE_NAME (t) == TYPE_NAME (type))
3009 return t;
3011 return NULL_TREE;
3014 /* Like get_qualified_type, but creates the type if it does not
3015 exist. This function never returns NULL_TREE. */
3017 tree
3018 build_qualified_type (type, type_quals)
3019 tree type;
3020 int type_quals;
3022 tree t;
3024 /* See if we already have the appropriate qualified variant. */
3025 t = get_qualified_type (type, type_quals);
3027 /* If not, build it. */
3028 if (!t)
3030 t = build_type_copy (type);
3031 set_type_quals (t, type_quals);
3034 return t;
3037 /* Create a new variant of TYPE, equivalent but distinct.
3038 This is so the caller can modify it. */
3040 tree
3041 build_type_copy (type)
3042 tree type;
3044 tree t, m = TYPE_MAIN_VARIANT (type);
3046 t = copy_node (type);
3048 TYPE_POINTER_TO (t) = 0;
3049 TYPE_REFERENCE_TO (t) = 0;
3051 /* Add this type to the chain of variants of TYPE. */
3052 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
3053 TYPE_NEXT_VARIANT (m) = t;
3055 return t;
3058 /* Hashing of types so that we don't make duplicates.
3059 The entry point is `type_hash_canon'. */
3061 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
3062 with types in the TREE_VALUE slots), by adding the hash codes
3063 of the individual types. */
3065 unsigned int
3066 type_hash_list (list)
3067 tree list;
3069 unsigned int hashcode;
3070 tree tail;
3072 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
3073 hashcode += TYPE_HASH (TREE_VALUE (tail));
3075 return hashcode;
3078 /* These are the Hashtable callback functions. */
3080 /* Returns true if the types are equal. */
3082 static int
3083 type_hash_eq (va, vb)
3084 const void *va;
3085 const void *vb;
3087 const struct type_hash *a = va, *b = vb;
3088 if (a->hash == b->hash
3089 && TREE_CODE (a->type) == TREE_CODE (b->type)
3090 && TREE_TYPE (a->type) == TREE_TYPE (b->type)
3091 && attribute_list_equal (TYPE_ATTRIBUTES (a->type),
3092 TYPE_ATTRIBUTES (b->type))
3093 && TYPE_ALIGN (a->type) == TYPE_ALIGN (b->type)
3094 && (TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
3095 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
3096 TYPE_MAX_VALUE (b->type)))
3097 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
3098 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
3099 TYPE_MIN_VALUE (b->type)))
3100 /* Note that TYPE_DOMAIN is TYPE_ARG_TYPES for FUNCTION_TYPE. */
3101 && (TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type)
3102 || (TYPE_DOMAIN (a->type)
3103 && TREE_CODE (TYPE_DOMAIN (a->type)) == TREE_LIST
3104 && TYPE_DOMAIN (b->type)
3105 && TREE_CODE (TYPE_DOMAIN (b->type)) == TREE_LIST
3106 && type_list_equal (TYPE_DOMAIN (a->type),
3107 TYPE_DOMAIN (b->type)))))
3108 return 1;
3109 return 0;
3112 /* Return the cached hash value. */
3114 static unsigned int
3115 type_hash_hash (item)
3116 const void *item;
3118 return ((const struct type_hash *) item)->hash;
3121 /* Look in the type hash table for a type isomorphic to TYPE.
3122 If one is found, return it. Otherwise return 0. */
3124 tree
3125 type_hash_lookup (hashcode, type)
3126 unsigned int hashcode;
3127 tree type;
3129 struct type_hash *h, in;
3131 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
3132 must call that routine before comparing TYPE_ALIGNs. */
3133 layout_type (type);
3135 in.hash = hashcode;
3136 in.type = type;
3138 h = htab_find_with_hash (type_hash_table, &in, hashcode);
3139 if (h)
3140 return h->type;
3141 return NULL_TREE;
3144 /* Add an entry to the type-hash-table
3145 for a type TYPE whose hash code is HASHCODE. */
3147 void
3148 type_hash_add (hashcode, type)
3149 unsigned int hashcode;
3150 tree type;
3152 struct type_hash *h;
3153 void **loc;
3155 h = (struct type_hash *) ggc_alloc (sizeof (struct type_hash));
3156 h->hash = hashcode;
3157 h->type = type;
3158 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
3159 *(struct type_hash **) loc = h;
3162 /* Given TYPE, and HASHCODE its hash code, return the canonical
3163 object for an identical type if one already exists.
3164 Otherwise, return TYPE, and record it as the canonical object
3165 if it is a permanent object.
3167 To use this function, first create a type of the sort you want.
3168 Then compute its hash code from the fields of the type that
3169 make it different from other similar types.
3170 Then call this function and use the value.
3171 This function frees the type you pass in if it is a duplicate. */
3173 /* Set to 1 to debug without canonicalization. Never set by program. */
3174 int debug_no_type_hash = 0;
3176 tree
3177 type_hash_canon (hashcode, type)
3178 unsigned int hashcode;
3179 tree type;
3181 tree t1;
3183 if (debug_no_type_hash)
3184 return type;
3186 /* See if the type is in the hash table already. If so, return it.
3187 Otherwise, add the type. */
3188 t1 = type_hash_lookup (hashcode, type);
3189 if (t1 != 0)
3191 #ifdef GATHER_STATISTICS
3192 tree_node_counts[(int) t_kind]--;
3193 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
3194 #endif
3195 return t1;
3197 else
3199 type_hash_add (hashcode, type);
3200 return type;
3204 /* See if the data pointed to by the type hash table is marked. We consider
3205 it marked if the type is marked or if a debug type number or symbol
3206 table entry has been made for the type. This reduces the amount of
3207 debugging output and eliminates that dependency of the debug output on
3208 the number of garbage collections. */
3210 static int
3211 type_hash_marked_p (p)
3212 const void *p;
3214 tree type = ((struct type_hash *) p)->type;
3216 return ggc_marked_p (type) || TYPE_SYMTAB_POINTER (type);
3219 /* Mark the entry in the type hash table the type it points to is marked.
3220 Also mark the type in case we are considering this entry "marked" by
3221 virtue of TYPE_SYMTAB_POINTER being set. */
3223 static void
3224 type_hash_mark (p)
3225 const void *p;
3227 ggc_mark (p);
3228 ggc_mark_tree (((struct type_hash *) p)->type);
3231 /* Mark the hashtable slot pointed to by ENTRY (which is really a
3232 `tree**') for GC. */
3234 static int
3235 mark_tree_hashtable_entry (entry, data)
3236 void **entry;
3237 void *data ATTRIBUTE_UNUSED;
3239 ggc_mark_tree ((tree) *entry);
3240 return 1;
3243 /* Mark ARG (which is really a htab_t whose slots are trees) for
3244 GC. */
3246 void
3247 mark_tree_hashtable (arg)
3248 void *arg;
3250 htab_t t = *(htab_t *) arg;
3251 htab_traverse (t, mark_tree_hashtable_entry, 0);
3254 static void
3255 print_type_hash_statistics ()
3257 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
3258 (long) htab_size (type_hash_table),
3259 (long) htab_elements (type_hash_table),
3260 htab_collisions (type_hash_table));
3263 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
3264 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
3265 by adding the hash codes of the individual attributes. */
3267 unsigned int
3268 attribute_hash_list (list)
3269 tree list;
3271 unsigned int hashcode;
3272 tree tail;
3274 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
3275 /* ??? Do we want to add in TREE_VALUE too? */
3276 hashcode += TYPE_HASH (TREE_PURPOSE (tail));
3277 return hashcode;
3280 /* Given two lists of attributes, return true if list l2 is
3281 equivalent to l1. */
3284 attribute_list_equal (l1, l2)
3285 tree l1, l2;
3287 return attribute_list_contained (l1, l2)
3288 && attribute_list_contained (l2, l1);
3291 /* Given two lists of attributes, return true if list L2 is
3292 completely contained within L1. */
3293 /* ??? This would be faster if attribute names were stored in a canonicalized
3294 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
3295 must be used to show these elements are equivalent (which they are). */
3296 /* ??? It's not clear that attributes with arguments will always be handled
3297 correctly. */
3300 attribute_list_contained (l1, l2)
3301 tree l1, l2;
3303 tree t1, t2;
3305 /* First check the obvious, maybe the lists are identical. */
3306 if (l1 == l2)
3307 return 1;
3309 /* Maybe the lists are similar. */
3310 for (t1 = l1, t2 = l2;
3311 t1 != 0 && t2 != 0
3312 && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
3313 && TREE_VALUE (t1) == TREE_VALUE (t2);
3314 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
3316 /* Maybe the lists are equal. */
3317 if (t1 == 0 && t2 == 0)
3318 return 1;
3320 for (; t2 != 0; t2 = TREE_CHAIN (t2))
3322 tree attr;
3323 for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)), l1);
3324 attr != NULL_TREE;
3325 attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
3326 TREE_CHAIN (attr)))
3328 if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) == 1)
3329 break;
3332 if (attr == 0)
3333 return 0;
3335 if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) != 1)
3336 return 0;
3339 return 1;
3342 /* Given two lists of types
3343 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
3344 return 1 if the lists contain the same types in the same order.
3345 Also, the TREE_PURPOSEs must match. */
3348 type_list_equal (l1, l2)
3349 tree l1, l2;
3351 tree t1, t2;
3353 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
3354 if (TREE_VALUE (t1) != TREE_VALUE (t2)
3355 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
3356 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
3357 && (TREE_TYPE (TREE_PURPOSE (t1))
3358 == TREE_TYPE (TREE_PURPOSE (t2))))))
3359 return 0;
3361 return t1 == t2;
3364 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
3365 given by TYPE. If the argument list accepts variable arguments,
3366 then this function counts only the ordinary arguments. */
3369 type_num_arguments (type)
3370 tree type;
3372 int i = 0;
3373 tree t;
3375 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
3376 /* If the function does not take a variable number of arguments,
3377 the last element in the list will have type `void'. */
3378 if (VOID_TYPE_P (TREE_VALUE (t)))
3379 break;
3380 else
3381 ++i;
3383 return i;
3386 /* Nonzero if integer constants T1 and T2
3387 represent the same constant value. */
3390 tree_int_cst_equal (t1, t2)
3391 tree t1, t2;
3393 if (t1 == t2)
3394 return 1;
3396 if (t1 == 0 || t2 == 0)
3397 return 0;
3399 if (TREE_CODE (t1) == INTEGER_CST
3400 && TREE_CODE (t2) == INTEGER_CST
3401 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3402 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
3403 return 1;
3405 return 0;
3408 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
3409 The precise way of comparison depends on their data type. */
3412 tree_int_cst_lt (t1, t2)
3413 tree t1, t2;
3415 if (t1 == t2)
3416 return 0;
3418 if (! TREE_UNSIGNED (TREE_TYPE (t1)))
3419 return INT_CST_LT (t1, t2);
3421 return INT_CST_LT_UNSIGNED (t1, t2);
3424 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
3427 tree_int_cst_compare (t1, t2)
3428 tree t1;
3429 tree t2;
3431 if (tree_int_cst_lt (t1, t2))
3432 return -1;
3433 else if (tree_int_cst_lt (t2, t1))
3434 return 1;
3435 else
3436 return 0;
3439 /* Return 1 if T is an INTEGER_CST that can be represented in a single
3440 HOST_WIDE_INT value. If POS is nonzero, the result must be positive. */
3443 host_integerp (t, pos)
3444 tree t;
3445 int pos;
3447 return (TREE_CODE (t) == INTEGER_CST
3448 && ! TREE_OVERFLOW (t)
3449 && ((TREE_INT_CST_HIGH (t) == 0
3450 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
3451 || (! pos && TREE_INT_CST_HIGH (t) == -1
3452 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
3453 || (! pos && TREE_INT_CST_HIGH (t) == 0
3454 && TREE_UNSIGNED (TREE_TYPE (t)))));
3457 /* Return the HOST_WIDE_INT least significant bits of T if it is an
3458 INTEGER_CST and there is no overflow. POS is nonzero if the result must
3459 be positive. Abort if we cannot satisfy the above conditions. */
3461 HOST_WIDE_INT
3462 tree_low_cst (t, pos)
3463 tree t;
3464 int pos;
3466 if (host_integerp (t, pos))
3467 return TREE_INT_CST_LOW (t);
3468 else
3469 abort ();
3472 /* Return the most significant bit of the integer constant T. */
3475 tree_int_cst_msb (t)
3476 tree t;
3478 int prec;
3479 HOST_WIDE_INT h;
3480 unsigned HOST_WIDE_INT l;
3482 /* Note that using TYPE_PRECISION here is wrong. We care about the
3483 actual bits, not the (arbitrary) range of the type. */
3484 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
3485 rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
3486 2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
3487 return (l & 1) == 1;
3490 /* Return an indication of the sign of the integer constant T.
3491 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
3492 Note that -1 will never be returned it T's type is unsigned. */
3495 tree_int_cst_sgn (t)
3496 tree t;
3498 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
3499 return 0;
3500 else if (TREE_UNSIGNED (TREE_TYPE (t)))
3501 return 1;
3502 else if (TREE_INT_CST_HIGH (t) < 0)
3503 return -1;
3504 else
3505 return 1;
3508 /* Compare two constructor-element-type constants. Return 1 if the lists
3509 are known to be equal; otherwise return 0. */
3512 simple_cst_list_equal (l1, l2)
3513 tree l1, l2;
3515 while (l1 != NULL_TREE && l2 != NULL_TREE)
3517 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
3518 return 0;
3520 l1 = TREE_CHAIN (l1);
3521 l2 = TREE_CHAIN (l2);
3524 return l1 == l2;
3527 /* Return truthvalue of whether T1 is the same tree structure as T2.
3528 Return 1 if they are the same.
3529 Return 0 if they are understandably different.
3530 Return -1 if either contains tree structure not understood by
3531 this function. */
3534 simple_cst_equal (t1, t2)
3535 tree t1, t2;
3537 enum tree_code code1, code2;
3538 int cmp;
3539 int i;
3541 if (t1 == t2)
3542 return 1;
3543 if (t1 == 0 || t2 == 0)
3544 return 0;
3546 code1 = TREE_CODE (t1);
3547 code2 = TREE_CODE (t2);
3549 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
3551 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3552 || code2 == NON_LVALUE_EXPR)
3553 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3554 else
3555 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
3558 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3559 || code2 == NON_LVALUE_EXPR)
3560 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
3562 if (code1 != code2)
3563 return 0;
3565 switch (code1)
3567 case INTEGER_CST:
3568 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3569 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
3571 case REAL_CST:
3572 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3574 case STRING_CST:
3575 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3576 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3577 TREE_STRING_LENGTH (t1)));
3579 case CONSTRUCTOR:
3580 if (CONSTRUCTOR_ELTS (t1) == CONSTRUCTOR_ELTS (t2))
3581 return 1;
3582 else
3583 abort ();
3585 case SAVE_EXPR:
3586 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3588 case CALL_EXPR:
3589 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3590 if (cmp <= 0)
3591 return cmp;
3592 return
3593 simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3595 case TARGET_EXPR:
3596 /* Special case: if either target is an unallocated VAR_DECL,
3597 it means that it's going to be unified with whatever the
3598 TARGET_EXPR is really supposed to initialize, so treat it
3599 as being equivalent to anything. */
3600 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
3601 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
3602 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
3603 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
3604 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
3605 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
3606 cmp = 1;
3607 else
3608 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3610 if (cmp <= 0)
3611 return cmp;
3613 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3615 case WITH_CLEANUP_EXPR:
3616 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3617 if (cmp <= 0)
3618 return cmp;
3620 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
3622 case COMPONENT_REF:
3623 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
3624 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3626 return 0;
3628 case VAR_DECL:
3629 case PARM_DECL:
3630 case CONST_DECL:
3631 case FUNCTION_DECL:
3632 return 0;
3634 default:
3635 break;
3638 /* This general rule works for most tree codes. All exceptions should be
3639 handled above. If this is a language-specific tree code, we can't
3640 trust what might be in the operand, so say we don't know
3641 the situation. */
3642 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
3643 return -1;
3645 switch (TREE_CODE_CLASS (code1))
3647 case '1':
3648 case '2':
3649 case '<':
3650 case 'e':
3651 case 'r':
3652 case 's':
3653 cmp = 1;
3654 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
3656 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
3657 if (cmp <= 0)
3658 return cmp;
3661 return cmp;
3663 default:
3664 return -1;
3668 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
3669 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
3670 than U, respectively. */
3673 compare_tree_int (t, u)
3674 tree t;
3675 unsigned HOST_WIDE_INT u;
3677 if (tree_int_cst_sgn (t) < 0)
3678 return -1;
3679 else if (TREE_INT_CST_HIGH (t) != 0)
3680 return 1;
3681 else if (TREE_INT_CST_LOW (t) == u)
3682 return 0;
3683 else if (TREE_INT_CST_LOW (t) < u)
3684 return -1;
3685 else
3686 return 1;
3689 /* Constructors for pointer, array and function types.
3690 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
3691 constructed by language-dependent code, not here.) */
3693 /* Construct, lay out and return the type of pointers to TO_TYPE.
3694 If such a type has already been constructed, reuse it. */
3696 tree
3697 build_pointer_type (to_type)
3698 tree to_type;
3700 tree t = TYPE_POINTER_TO (to_type);
3702 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3704 if (t != 0)
3705 return t;
3707 /* We need a new one. */
3708 t = make_node (POINTER_TYPE);
3710 TREE_TYPE (t) = to_type;
3712 /* Record this type as the pointer to TO_TYPE. */
3713 TYPE_POINTER_TO (to_type) = t;
3715 /* Lay out the type. This function has many callers that are concerned
3716 with expression-construction, and this simplifies them all.
3717 Also, it guarantees the TYPE_SIZE is in the same obstack as the type. */
3718 layout_type (t);
3720 return t;
3723 /* Build the node for the type of references-to-TO_TYPE. */
3725 tree
3726 build_reference_type (to_type)
3727 tree to_type;
3729 tree t = TYPE_REFERENCE_TO (to_type);
3731 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3733 if (t)
3734 return t;
3736 /* We need a new one. */
3737 t = make_node (REFERENCE_TYPE);
3739 TREE_TYPE (t) = to_type;
3741 /* Record this type as the pointer to TO_TYPE. */
3742 TYPE_REFERENCE_TO (to_type) = t;
3744 layout_type (t);
3746 return t;
3749 /* Build a type that is compatible with t but has no cv quals anywhere
3750 in its type, thus
3752 const char *const *const * -> char ***. */
3754 tree
3755 build_type_no_quals (t)
3756 tree t;
3758 switch (TREE_CODE (t))
3760 case POINTER_TYPE:
3761 return build_pointer_type (build_type_no_quals (TREE_TYPE (t)));
3762 case REFERENCE_TYPE:
3763 return build_reference_type (build_type_no_quals (TREE_TYPE (t)));
3764 default:
3765 return TYPE_MAIN_VARIANT (t);
3769 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
3770 MAXVAL should be the maximum value in the domain
3771 (one less than the length of the array).
3773 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
3774 We don't enforce this limit, that is up to caller (e.g. language front end).
3775 The limit exists because the result is a signed type and we don't handle
3776 sizes that use more than one HOST_WIDE_INT. */
3778 tree
3779 build_index_type (maxval)
3780 tree maxval;
3782 tree itype = make_node (INTEGER_TYPE);
3784 TREE_TYPE (itype) = sizetype;
3785 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
3786 TYPE_MIN_VALUE (itype) = size_zero_node;
3787 TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
3788 TYPE_MODE (itype) = TYPE_MODE (sizetype);
3789 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
3790 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
3791 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
3792 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
3794 if (host_integerp (maxval, 1))
3795 return type_hash_canon (tree_low_cst (maxval, 1), itype);
3796 else
3797 return itype;
3800 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
3801 ENUMERAL_TYPE, BOOLEAN_TYPE, or CHAR_TYPE), with
3802 low bound LOWVAL and high bound HIGHVAL.
3803 if TYPE==NULL_TREE, sizetype is used. */
3805 tree
3806 build_range_type (type, lowval, highval)
3807 tree type, lowval, highval;
3809 tree itype = make_node (INTEGER_TYPE);
3811 TREE_TYPE (itype) = type;
3812 if (type == NULL_TREE)
3813 type = sizetype;
3815 TYPE_MIN_VALUE (itype) = convert (type, lowval);
3816 TYPE_MAX_VALUE (itype) = highval ? convert (type, highval) : NULL;
3818 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
3819 TYPE_MODE (itype) = TYPE_MODE (type);
3820 TYPE_SIZE (itype) = TYPE_SIZE (type);
3821 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
3822 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
3823 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
3825 if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
3826 return type_hash_canon (tree_low_cst (highval, 0)
3827 - tree_low_cst (lowval, 0),
3828 itype);
3829 else
3830 return itype;
3833 /* Just like build_index_type, but takes lowval and highval instead
3834 of just highval (maxval). */
3836 tree
3837 build_index_2_type (lowval, highval)
3838 tree lowval, highval;
3840 return build_range_type (sizetype, lowval, highval);
3843 /* Return nonzero iff ITYPE1 and ITYPE2 are equal (in the LISP sense).
3844 Needed because when index types are not hashed, equal index types
3845 built at different times appear distinct, even though structurally,
3846 they are not. */
3849 index_type_equal (itype1, itype2)
3850 tree itype1, itype2;
3852 if (TREE_CODE (itype1) != TREE_CODE (itype2))
3853 return 0;
3855 if (TREE_CODE (itype1) == INTEGER_TYPE)
3857 if (TYPE_PRECISION (itype1) != TYPE_PRECISION (itype2)
3858 || TYPE_MODE (itype1) != TYPE_MODE (itype2)
3859 || simple_cst_equal (TYPE_SIZE (itype1), TYPE_SIZE (itype2)) != 1
3860 || TYPE_ALIGN (itype1) != TYPE_ALIGN (itype2))
3861 return 0;
3863 if (1 == simple_cst_equal (TYPE_MIN_VALUE (itype1),
3864 TYPE_MIN_VALUE (itype2))
3865 && 1 == simple_cst_equal (TYPE_MAX_VALUE (itype1),
3866 TYPE_MAX_VALUE (itype2)))
3867 return 1;
3870 return 0;
3873 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
3874 and number of elements specified by the range of values of INDEX_TYPE.
3875 If such a type has already been constructed, reuse it. */
3877 tree
3878 build_array_type (elt_type, index_type)
3879 tree elt_type, index_type;
3881 tree t;
3882 unsigned int hashcode;
3884 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
3886 error ("arrays of functions are not meaningful");
3887 elt_type = integer_type_node;
3890 /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
3891 build_pointer_type (elt_type);
3893 /* Allocate the array after the pointer type,
3894 in case we free it in type_hash_canon. */
3895 t = make_node (ARRAY_TYPE);
3896 TREE_TYPE (t) = elt_type;
3897 TYPE_DOMAIN (t) = index_type;
3899 if (index_type == 0)
3901 return t;
3904 hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
3905 t = type_hash_canon (hashcode, t);
3907 if (!COMPLETE_TYPE_P (t))
3908 layout_type (t);
3909 return t;
3912 /* Return the TYPE of the elements comprising
3913 the innermost dimension of ARRAY. */
3915 tree
3916 get_inner_array_type (array)
3917 tree array;
3919 tree type = TREE_TYPE (array);
3921 while (TREE_CODE (type) == ARRAY_TYPE)
3922 type = TREE_TYPE (type);
3924 return type;
3927 /* Construct, lay out and return
3928 the type of functions returning type VALUE_TYPE
3929 given arguments of types ARG_TYPES.
3930 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
3931 are data type nodes for the arguments of the function.
3932 If such a type has already been constructed, reuse it. */
3934 tree
3935 build_function_type (value_type, arg_types)
3936 tree value_type, arg_types;
3938 tree t;
3939 unsigned int hashcode;
3941 if (TREE_CODE (value_type) == FUNCTION_TYPE)
3943 error ("function return type cannot be function");
3944 value_type = integer_type_node;
3947 /* Make a node of the sort we want. */
3948 t = make_node (FUNCTION_TYPE);
3949 TREE_TYPE (t) = value_type;
3950 TYPE_ARG_TYPES (t) = arg_types;
3952 /* If we already have such a type, use the old one and free this one. */
3953 hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
3954 t = type_hash_canon (hashcode, t);
3956 if (!COMPLETE_TYPE_P (t))
3957 layout_type (t);
3958 return t;
3961 /* Construct, lay out and return the type of methods belonging to class
3962 BASETYPE and whose arguments and values are described by TYPE.
3963 If that type exists already, reuse it.
3964 TYPE must be a FUNCTION_TYPE node. */
3966 tree
3967 build_method_type (basetype, type)
3968 tree basetype, type;
3970 tree t;
3971 unsigned int hashcode;
3973 /* Make a node of the sort we want. */
3974 t = make_node (METHOD_TYPE);
3976 if (TREE_CODE (type) != FUNCTION_TYPE)
3977 abort ();
3979 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3980 TREE_TYPE (t) = TREE_TYPE (type);
3982 /* The actual arglist for this function includes a "hidden" argument
3983 which is "this". Put it into the list of argument types. */
3985 TYPE_ARG_TYPES (t)
3986 = tree_cons (NULL_TREE,
3987 build_pointer_type (basetype), TYPE_ARG_TYPES (type));
3989 /* If we already have such a type, use the old one and free this one. */
3990 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
3991 t = type_hash_canon (hashcode, t);
3993 if (!COMPLETE_TYPE_P (t))
3994 layout_type (t);
3996 return t;
3999 /* Construct, lay out and return the type of offsets to a value
4000 of type TYPE, within an object of type BASETYPE.
4001 If a suitable offset type exists already, reuse it. */
4003 tree
4004 build_offset_type (basetype, type)
4005 tree basetype, type;
4007 tree t;
4008 unsigned int hashcode;
4010 /* Make a node of the sort we want. */
4011 t = make_node (OFFSET_TYPE);
4013 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
4014 TREE_TYPE (t) = type;
4016 /* If we already have such a type, use the old one and free this one. */
4017 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
4018 t = type_hash_canon (hashcode, t);
4020 if (!COMPLETE_TYPE_P (t))
4021 layout_type (t);
4023 return t;
4026 /* Create a complex type whose components are COMPONENT_TYPE. */
4028 tree
4029 build_complex_type (component_type)
4030 tree component_type;
4032 tree t;
4033 unsigned int hashcode;
4035 /* Make a node of the sort we want. */
4036 t = make_node (COMPLEX_TYPE);
4038 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
4039 set_type_quals (t, TYPE_QUALS (component_type));
4041 /* If we already have such a type, use the old one and free this one. */
4042 hashcode = TYPE_HASH (component_type);
4043 t = type_hash_canon (hashcode, t);
4045 if (!COMPLETE_TYPE_P (t))
4046 layout_type (t);
4048 /* If we are writing Dwarf2 output we need to create a name,
4049 since complex is a fundamental type. */
4050 if ((write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
4051 && ! TYPE_NAME (t))
4053 const char *name;
4054 if (component_type == char_type_node)
4055 name = "complex char";
4056 else if (component_type == signed_char_type_node)
4057 name = "complex signed char";
4058 else if (component_type == unsigned_char_type_node)
4059 name = "complex unsigned char";
4060 else if (component_type == short_integer_type_node)
4061 name = "complex short int";
4062 else if (component_type == short_unsigned_type_node)
4063 name = "complex short unsigned int";
4064 else if (component_type == integer_type_node)
4065 name = "complex int";
4066 else if (component_type == unsigned_type_node)
4067 name = "complex unsigned int";
4068 else if (component_type == long_integer_type_node)
4069 name = "complex long int";
4070 else if (component_type == long_unsigned_type_node)
4071 name = "complex long unsigned int";
4072 else if (component_type == long_long_integer_type_node)
4073 name = "complex long long int";
4074 else if (component_type == long_long_unsigned_type_node)
4075 name = "complex long long unsigned int";
4076 else
4077 name = 0;
4079 if (name != 0)
4080 TYPE_NAME (t) = get_identifier (name);
4083 return t;
4086 /* Return OP, stripped of any conversions to wider types as much as is safe.
4087 Converting the value back to OP's type makes a value equivalent to OP.
4089 If FOR_TYPE is nonzero, we return a value which, if converted to
4090 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
4092 If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
4093 narrowest type that can hold the value, even if they don't exactly fit.
4094 Otherwise, bit-field references are changed to a narrower type
4095 only if they can be fetched directly from memory in that type.
4097 OP must have integer, real or enumeral type. Pointers are not allowed!
4099 There are some cases where the obvious value we could return
4100 would regenerate to OP if converted to OP's type,
4101 but would not extend like OP to wider types.
4102 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
4103 For example, if OP is (unsigned short)(signed char)-1,
4104 we avoid returning (signed char)-1 if FOR_TYPE is int,
4105 even though extending that to an unsigned short would regenerate OP,
4106 since the result of extending (signed char)-1 to (int)
4107 is different from (int) OP. */
4109 tree
4110 get_unwidened (op, for_type)
4111 tree op;
4112 tree for_type;
4114 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
4115 tree type = TREE_TYPE (op);
4116 unsigned final_prec
4117 = TYPE_PRECISION (for_type != 0 ? for_type : type);
4118 int uns
4119 = (for_type != 0 && for_type != type
4120 && final_prec > TYPE_PRECISION (type)
4121 && TREE_UNSIGNED (type));
4122 tree win = op;
4124 while (TREE_CODE (op) == NOP_EXPR)
4126 int bitschange
4127 = TYPE_PRECISION (TREE_TYPE (op))
4128 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
4130 /* Truncations are many-one so cannot be removed.
4131 Unless we are later going to truncate down even farther. */
4132 if (bitschange < 0
4133 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
4134 break;
4136 /* See what's inside this conversion. If we decide to strip it,
4137 we will set WIN. */
4138 op = TREE_OPERAND (op, 0);
4140 /* If we have not stripped any zero-extensions (uns is 0),
4141 we can strip any kind of extension.
4142 If we have previously stripped a zero-extension,
4143 only zero-extensions can safely be stripped.
4144 Any extension can be stripped if the bits it would produce
4145 are all going to be discarded later by truncating to FOR_TYPE. */
4147 if (bitschange > 0)
4149 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
4150 win = op;
4151 /* TREE_UNSIGNED says whether this is a zero-extension.
4152 Let's avoid computing it if it does not affect WIN
4153 and if UNS will not be needed again. */
4154 if ((uns || TREE_CODE (op) == NOP_EXPR)
4155 && TREE_UNSIGNED (TREE_TYPE (op)))
4157 uns = 1;
4158 win = op;
4163 if (TREE_CODE (op) == COMPONENT_REF
4164 /* Since type_for_size always gives an integer type. */
4165 && TREE_CODE (type) != REAL_TYPE
4166 /* Don't crash if field not laid out yet. */
4167 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
4168 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
4170 unsigned int innerprec
4171 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4173 type = type_for_size (innerprec, TREE_UNSIGNED (TREE_OPERAND (op, 1)));
4175 /* We can get this structure field in the narrowest type it fits in.
4176 If FOR_TYPE is 0, do this only for a field that matches the
4177 narrower type exactly and is aligned for it
4178 The resulting extension to its nominal type (a fullword type)
4179 must fit the same conditions as for other extensions. */
4181 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
4182 && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
4183 && (! uns || final_prec <= innerprec
4184 || TREE_UNSIGNED (TREE_OPERAND (op, 1)))
4185 && type != 0)
4187 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4188 TREE_OPERAND (op, 1));
4189 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4190 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4194 return win;
4197 /* Return OP or a simpler expression for a narrower value
4198 which can be sign-extended or zero-extended to give back OP.
4199 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
4200 or 0 if the value should be sign-extended. */
4202 tree
4203 get_narrower (op, unsignedp_ptr)
4204 tree op;
4205 int *unsignedp_ptr;
4207 int uns = 0;
4208 int first = 1;
4209 tree win = op;
4211 while (TREE_CODE (op) == NOP_EXPR)
4213 int bitschange
4214 = (TYPE_PRECISION (TREE_TYPE (op))
4215 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
4217 /* Truncations are many-one so cannot be removed. */
4218 if (bitschange < 0)
4219 break;
4221 /* See what's inside this conversion. If we decide to strip it,
4222 we will set WIN. */
4223 op = TREE_OPERAND (op, 0);
4225 if (bitschange > 0)
4227 /* An extension: the outermost one can be stripped,
4228 but remember whether it is zero or sign extension. */
4229 if (first)
4230 uns = TREE_UNSIGNED (TREE_TYPE (op));
4231 /* Otherwise, if a sign extension has been stripped,
4232 only sign extensions can now be stripped;
4233 if a zero extension has been stripped, only zero-extensions. */
4234 else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
4235 break;
4236 first = 0;
4238 else /* bitschange == 0 */
4240 /* A change in nominal type can always be stripped, but we must
4241 preserve the unsignedness. */
4242 if (first)
4243 uns = TREE_UNSIGNED (TREE_TYPE (op));
4244 first = 0;
4247 win = op;
4250 if (TREE_CODE (op) == COMPONENT_REF
4251 /* Since type_for_size always gives an integer type. */
4252 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
4253 /* Ensure field is laid out already. */
4254 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0)
4256 unsigned HOST_WIDE_INT innerprec
4257 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4258 tree type = type_for_size (innerprec, TREE_UNSIGNED (op));
4260 /* We can get this structure field in a narrower type that fits it,
4261 but the resulting extension to its nominal type (a fullword type)
4262 must satisfy the same conditions as for other extensions.
4264 Do this only for fields that are aligned (not bit-fields),
4265 because when bit-field insns will be used there is no
4266 advantage in doing this. */
4268 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
4269 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
4270 && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
4271 && type != 0)
4273 if (first)
4274 uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
4275 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4276 TREE_OPERAND (op, 1));
4277 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4278 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4281 *unsignedp_ptr = uns;
4282 return win;
4285 /* Nonzero if integer constant C has a value that is permissible
4286 for type TYPE (an INTEGER_TYPE). */
4289 int_fits_type_p (c, type)
4290 tree c, type;
4292 /* If the bounds of the type are integers, we can check ourselves.
4293 If not, but this type is a subtype, try checking against that.
4294 Otherwise, use force_fit_type, which checks against the precision. */
4295 if (TYPE_MAX_VALUE (type) != NULL_TREE
4296 && TYPE_MIN_VALUE (type) != NULL_TREE
4297 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST
4298 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
4300 if (TREE_UNSIGNED (type))
4301 return (! INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c)
4302 && ! INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type))
4303 /* Negative ints never fit unsigned types. */
4304 && ! (TREE_INT_CST_HIGH (c) < 0
4305 && ! TREE_UNSIGNED (TREE_TYPE (c))));
4306 else
4307 return (! INT_CST_LT (TYPE_MAX_VALUE (type), c)
4308 && ! INT_CST_LT (c, TYPE_MIN_VALUE (type))
4309 /* Unsigned ints with top bit set never fit signed types. */
4310 && ! (TREE_INT_CST_HIGH (c) < 0
4311 && TREE_UNSIGNED (TREE_TYPE (c))));
4313 else if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != 0)
4314 return int_fits_type_p (c, TREE_TYPE (type));
4315 else
4317 c = copy_node (c);
4318 TREE_TYPE (c) = type;
4319 return !force_fit_type (c, 0);
4323 /* Given a DECL or TYPE, return the scope in which it was declared, or
4324 NULL_TREE if there is no containing scope. */
4326 tree
4327 get_containing_scope (t)
4328 tree t;
4330 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
4333 /* Return the innermost context enclosing DECL that is
4334 a FUNCTION_DECL, or zero if none. */
4336 tree
4337 decl_function_context (decl)
4338 tree decl;
4340 tree context;
4342 if (TREE_CODE (decl) == ERROR_MARK)
4343 return 0;
4345 if (TREE_CODE (decl) == SAVE_EXPR)
4346 context = SAVE_EXPR_CONTEXT (decl);
4348 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
4349 where we look up the function at runtime. Such functions always take
4350 a first argument of type 'pointer to real context'.
4352 C++ should really be fixed to use DECL_CONTEXT for the real context,
4353 and use something else for the "virtual context". */
4354 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
4355 context
4356 = TYPE_MAIN_VARIANT
4357 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4358 else
4359 context = DECL_CONTEXT (decl);
4361 while (context && TREE_CODE (context) != FUNCTION_DECL)
4363 if (TREE_CODE (context) == BLOCK)
4364 context = BLOCK_SUPERCONTEXT (context);
4365 else
4366 context = get_containing_scope (context);
4369 return context;
4372 /* Return the innermost context enclosing DECL that is
4373 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
4374 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
4376 tree
4377 decl_type_context (decl)
4378 tree decl;
4380 tree context = DECL_CONTEXT (decl);
4382 while (context)
4384 if (TREE_CODE (context) == RECORD_TYPE
4385 || TREE_CODE (context) == UNION_TYPE
4386 || TREE_CODE (context) == QUAL_UNION_TYPE)
4387 return context;
4389 if (TREE_CODE (context) == TYPE_DECL
4390 || TREE_CODE (context) == FUNCTION_DECL)
4391 context = DECL_CONTEXT (context);
4393 else if (TREE_CODE (context) == BLOCK)
4394 context = BLOCK_SUPERCONTEXT (context);
4396 else
4397 /* Unhandled CONTEXT!? */
4398 abort ();
4400 return NULL_TREE;
4403 /* CALL is a CALL_EXPR. Return the declaration for the function
4404 called, or NULL_TREE if the called function cannot be
4405 determined. */
4407 tree
4408 get_callee_fndecl (call)
4409 tree call;
4411 tree addr;
4413 /* It's invalid to call this function with anything but a
4414 CALL_EXPR. */
4415 if (TREE_CODE (call) != CALL_EXPR)
4416 abort ();
4418 /* The first operand to the CALL is the address of the function
4419 called. */
4420 addr = TREE_OPERAND (call, 0);
4422 STRIP_NOPS (addr);
4424 /* If this is a readonly function pointer, extract its initial value. */
4425 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
4426 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
4427 && DECL_INITIAL (addr))
4428 addr = DECL_INITIAL (addr);
4430 /* If the address is just `&f' for some function `f', then we know
4431 that `f' is being called. */
4432 if (TREE_CODE (addr) == ADDR_EXPR
4433 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
4434 return TREE_OPERAND (addr, 0);
4436 /* We couldn't figure out what was being called. */
4437 return NULL_TREE;
4440 /* Print debugging information about the obstack O, named STR. */
4442 void
4443 print_obstack_statistics (str, o)
4444 const char *str;
4445 struct obstack *o;
4447 struct _obstack_chunk *chunk = o->chunk;
4448 int n_chunks = 1;
4449 int n_alloc = 0;
4451 n_alloc += o->next_free - chunk->contents;
4452 chunk = chunk->prev;
4453 while (chunk)
4455 n_chunks += 1;
4456 n_alloc += chunk->limit - &chunk->contents[0];
4457 chunk = chunk->prev;
4459 fprintf (stderr, "obstack %s: %u bytes, %d chunks\n",
4460 str, n_alloc, n_chunks);
4463 /* Print debugging information about tree nodes generated during the compile,
4464 and any language-specific information. */
4466 void
4467 dump_tree_statistics ()
4469 #ifdef GATHER_STATISTICS
4470 int i;
4471 int total_nodes, total_bytes;
4472 #endif
4474 fprintf (stderr, "\n??? tree nodes created\n\n");
4475 #ifdef GATHER_STATISTICS
4476 fprintf (stderr, "Kind Nodes Bytes\n");
4477 fprintf (stderr, "-------------------------------------\n");
4478 total_nodes = total_bytes = 0;
4479 for (i = 0; i < (int) all_kinds; i++)
4481 fprintf (stderr, "%-20s %6d %9d\n", tree_node_kind_names[i],
4482 tree_node_counts[i], tree_node_sizes[i]);
4483 total_nodes += tree_node_counts[i];
4484 total_bytes += tree_node_sizes[i];
4486 fprintf (stderr, "-------------------------------------\n");
4487 fprintf (stderr, "%-20s %6d %9d\n", "Total", total_nodes, total_bytes);
4488 fprintf (stderr, "-------------------------------------\n");
4489 #else
4490 fprintf (stderr, "(No per-node statistics)\n");
4491 #endif
4492 print_obstack_statistics ("permanent_obstack", &permanent_obstack);
4493 print_type_hash_statistics ();
4494 (*lang_hooks.print_statistics) ();
4497 #define FILE_FUNCTION_PREFIX_LEN 9
4499 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
4501 /* Appends 6 random characters to TEMPLATE to (hopefully) avoid name
4502 clashes in cases where we can't reliably choose a unique name.
4504 Derived from mkstemp.c in libiberty. */
4506 static void
4507 append_random_chars (template)
4508 char *template;
4510 static const char letters[]
4511 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
4512 static unsigned HOST_WIDE_INT value;
4513 unsigned HOST_WIDE_INT v;
4515 if (! value)
4517 struct stat st;
4519 /* VALUE should be unique for each file and must not change between
4520 compiles since this can cause bootstrap comparison errors. */
4522 if (stat (main_input_filename, &st) < 0)
4524 /* This can happen when preprocessed text is shipped between
4525 machines, e.g. with bug reports. Assume that uniqueness
4526 isn't actually an issue. */
4527 value = 1;
4529 else
4531 /* In VMS, ino is an array, so we have to use both values. We
4532 conditionalize that. */
4533 #ifdef VMS
4534 #define INO_TO_INT(INO) ((int) (INO)[1] << 16 ^ (int) (INO)[2])
4535 #else
4536 #define INO_TO_INT(INO) INO
4537 #endif
4538 value = st.st_dev ^ INO_TO_INT (st.st_ino) ^ st.st_mtime;
4542 template += strlen (template);
4544 v = value;
4546 /* Fill in the random bits. */
4547 template[0] = letters[v % 62];
4548 v /= 62;
4549 template[1] = letters[v % 62];
4550 v /= 62;
4551 template[2] = letters[v % 62];
4552 v /= 62;
4553 template[3] = letters[v % 62];
4554 v /= 62;
4555 template[4] = letters[v % 62];
4556 v /= 62;
4557 template[5] = letters[v % 62];
4559 template[6] = '\0';
4562 /* P is a string that will be used in a symbol. Mask out any characters
4563 that are not valid in that context. */
4565 void
4566 clean_symbol_name (p)
4567 char *p;
4569 for (; *p; p++)
4570 if (! (ISALNUM (*p)
4571 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
4572 || *p == '$'
4573 #endif
4574 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
4575 || *p == '.'
4576 #endif
4578 *p = '_';
4581 /* Generate a name for a function unique to this translation unit.
4582 TYPE is some string to identify the purpose of this function to the
4583 linker or collect2. */
4585 tree
4586 get_file_function_name_long (type)
4587 const char *type;
4589 char *buf;
4590 const char *p;
4591 char *q;
4593 if (first_global_object_name)
4594 p = first_global_object_name;
4595 else
4597 /* We don't have anything that we know to be unique to this translation
4598 unit, so use what we do have and throw in some randomness. */
4600 const char *name = weak_global_object_name;
4601 const char *file = main_input_filename;
4603 if (! name)
4604 name = "";
4605 if (! file)
4606 file = input_filename;
4608 q = (char *) alloca (7 + strlen (name) + strlen (file));
4610 sprintf (q, "%s%s", name, file);
4611 append_random_chars (q);
4612 p = q;
4615 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
4616 + strlen (type));
4618 /* Set up the name of the file-level functions we may need.
4619 Use a global object (which is already required to be unique over
4620 the program) rather than the file name (which imposes extra
4621 constraints). */
4622 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
4624 /* Don't need to pull weird characters out of global names. */
4625 if (p != first_global_object_name)
4626 clean_symbol_name (buf + 11);
4628 return get_identifier (buf);
4631 /* If KIND=='I', return a suitable global initializer (constructor) name.
4632 If KIND=='D', return a suitable global clean-up (destructor) name. */
4634 tree
4635 get_file_function_name (kind)
4636 int kind;
4638 char p[2];
4640 p[0] = kind;
4641 p[1] = 0;
4643 return get_file_function_name_long (p);
4646 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4647 The result is placed in BUFFER (which has length BIT_SIZE),
4648 with one bit in each char ('\000' or '\001').
4650 If the constructor is constant, NULL_TREE is returned.
4651 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4653 tree
4654 get_set_constructor_bits (init, buffer, bit_size)
4655 tree init;
4656 char *buffer;
4657 int bit_size;
4659 int i;
4660 tree vals;
4661 HOST_WIDE_INT domain_min
4662 = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (init))), 0);
4663 tree non_const_bits = NULL_TREE;
4665 for (i = 0; i < bit_size; i++)
4666 buffer[i] = 0;
4668 for (vals = TREE_OPERAND (init, 1);
4669 vals != NULL_TREE; vals = TREE_CHAIN (vals))
4671 if (!host_integerp (TREE_VALUE (vals), 0)
4672 || (TREE_PURPOSE (vals) != NULL_TREE
4673 && !host_integerp (TREE_PURPOSE (vals), 0)))
4674 non_const_bits
4675 = tree_cons (TREE_PURPOSE (vals), TREE_VALUE (vals), non_const_bits);
4676 else if (TREE_PURPOSE (vals) != NULL_TREE)
4678 /* Set a range of bits to ones. */
4679 HOST_WIDE_INT lo_index
4680 = tree_low_cst (TREE_PURPOSE (vals), 0) - domain_min;
4681 HOST_WIDE_INT hi_index
4682 = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
4684 if (lo_index < 0 || lo_index >= bit_size
4685 || hi_index < 0 || hi_index >= bit_size)
4686 abort ();
4687 for (; lo_index <= hi_index; lo_index++)
4688 buffer[lo_index] = 1;
4690 else
4692 /* Set a single bit to one. */
4693 HOST_WIDE_INT index
4694 = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
4695 if (index < 0 || index >= bit_size)
4697 error ("invalid initializer for bit string");
4698 return NULL_TREE;
4700 buffer[index] = 1;
4703 return non_const_bits;
4706 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4707 The result is placed in BUFFER (which is an array of bytes).
4708 If the constructor is constant, NULL_TREE is returned.
4709 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4711 tree
4712 get_set_constructor_bytes (init, buffer, wd_size)
4713 tree init;
4714 unsigned char *buffer;
4715 int wd_size;
4717 int i;
4718 int set_word_size = BITS_PER_UNIT;
4719 int bit_size = wd_size * set_word_size;
4720 int bit_pos = 0;
4721 unsigned char *bytep = buffer;
4722 char *bit_buffer = (char *) alloca (bit_size);
4723 tree non_const_bits = get_set_constructor_bits (init, bit_buffer, bit_size);
4725 for (i = 0; i < wd_size; i++)
4726 buffer[i] = 0;
4728 for (i = 0; i < bit_size; i++)
4730 if (bit_buffer[i])
4732 if (BYTES_BIG_ENDIAN)
4733 *bytep |= (1 << (set_word_size - 1 - bit_pos));
4734 else
4735 *bytep |= 1 << bit_pos;
4737 bit_pos++;
4738 if (bit_pos >= set_word_size)
4739 bit_pos = 0, bytep++;
4741 return non_const_bits;
4744 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4745 /* Complain that the tree code of NODE does not match the expected CODE.
4746 FILE, LINE, and FUNCTION are of the caller. */
4748 void
4749 tree_check_failed (node, code, file, line, function)
4750 const tree node;
4751 enum tree_code code;
4752 const char *file;
4753 int line;
4754 const char *function;
4756 internal_error ("tree check: expected %s, have %s in %s, at %s:%d",
4757 tree_code_name[code], tree_code_name[TREE_CODE (node)],
4758 function, trim_filename (file), line);
4761 /* Similar to above, except that we check for a class of tree
4762 code, given in CL. */
4764 void
4765 tree_class_check_failed (node, cl, file, line, function)
4766 const tree node;
4767 int cl;
4768 const char *file;
4769 int line;
4770 const char *function;
4772 internal_error
4773 ("tree check: expected class '%c', have '%c' (%s) in %s, at %s:%d",
4774 cl, TREE_CODE_CLASS (TREE_CODE (node)),
4775 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
4778 #endif /* ENABLE_TREE_CHECKING */
4780 /* For a new vector type node T, build the information necessary for
4781 debuggint output. */
4783 static void
4784 finish_vector_type (t)
4785 tree t;
4787 layout_type (t);
4790 tree index = build_int_2 (TYPE_VECTOR_SUBPARTS (t) - 1, 0);
4791 tree array = build_array_type (TREE_TYPE (t),
4792 build_index_type (index));
4793 tree rt = make_node (RECORD_TYPE);
4795 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
4796 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
4797 layout_type (rt);
4798 TYPE_DEBUG_REPRESENTATION_TYPE (t) = rt;
4799 /* In dwarfout.c, type lookup uses TYPE_UID numbers. We want to output
4800 the representation type, and we want to find that die when looking up
4801 the vector type. This is most easily achieved by making the TYPE_UID
4802 numbers equal. */
4803 TYPE_UID (rt) = TYPE_UID (t);
4807 /* Create nodes for all integer types (and error_mark_node) using the sizes
4808 of C datatypes. The caller should call set_sizetype soon after calling
4809 this function to select one of the types as sizetype. */
4811 void
4812 build_common_tree_nodes (signed_char)
4813 int signed_char;
4815 error_mark_node = make_node (ERROR_MARK);
4816 TREE_TYPE (error_mark_node) = error_mark_node;
4818 initialize_sizetypes ();
4820 /* Define both `signed char' and `unsigned char'. */
4821 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
4822 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
4824 /* Define `char', which is like either `signed char' or `unsigned char'
4825 but not the same as either. */
4826 char_type_node
4827 = (signed_char
4828 ? make_signed_type (CHAR_TYPE_SIZE)
4829 : make_unsigned_type (CHAR_TYPE_SIZE));
4831 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
4832 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
4833 integer_type_node = make_signed_type (INT_TYPE_SIZE);
4834 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
4835 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
4836 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
4837 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
4838 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
4840 intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
4841 intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
4842 intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
4843 intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
4844 intTI_type_node = make_signed_type (GET_MODE_BITSIZE (TImode));
4846 unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
4847 unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
4848 unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
4849 unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
4850 unsigned_intTI_type_node = make_unsigned_type (GET_MODE_BITSIZE (TImode));
4853 /* Call this function after calling build_common_tree_nodes and set_sizetype.
4854 It will create several other common tree nodes. */
4856 void
4857 build_common_tree_nodes_2 (short_double)
4858 int short_double;
4860 /* Define these next since types below may used them. */
4861 integer_zero_node = build_int_2 (0, 0);
4862 integer_one_node = build_int_2 (1, 0);
4863 integer_minus_one_node = build_int_2 (-1, -1);
4865 size_zero_node = size_int (0);
4866 size_one_node = size_int (1);
4867 bitsize_zero_node = bitsize_int (0);
4868 bitsize_one_node = bitsize_int (1);
4869 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
4871 void_type_node = make_node (VOID_TYPE);
4872 layout_type (void_type_node);
4874 /* We are not going to have real types in C with less than byte alignment,
4875 so we might as well not have any types that claim to have it. */
4876 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
4877 TYPE_USER_ALIGN (void_type_node) = 0;
4879 null_pointer_node = build_int_2 (0, 0);
4880 TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
4881 layout_type (TREE_TYPE (null_pointer_node));
4883 ptr_type_node = build_pointer_type (void_type_node);
4884 const_ptr_type_node
4885 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
4887 float_type_node = make_node (REAL_TYPE);
4888 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
4889 layout_type (float_type_node);
4891 double_type_node = make_node (REAL_TYPE);
4892 if (short_double)
4893 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
4894 else
4895 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
4896 layout_type (double_type_node);
4898 long_double_type_node = make_node (REAL_TYPE);
4899 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
4900 layout_type (long_double_type_node);
4902 complex_integer_type_node = make_node (COMPLEX_TYPE);
4903 TREE_TYPE (complex_integer_type_node) = integer_type_node;
4904 layout_type (complex_integer_type_node);
4906 complex_float_type_node = make_node (COMPLEX_TYPE);
4907 TREE_TYPE (complex_float_type_node) = float_type_node;
4908 layout_type (complex_float_type_node);
4910 complex_double_type_node = make_node (COMPLEX_TYPE);
4911 TREE_TYPE (complex_double_type_node) = double_type_node;
4912 layout_type (complex_double_type_node);
4914 complex_long_double_type_node = make_node (COMPLEX_TYPE);
4915 TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
4916 layout_type (complex_long_double_type_node);
4919 tree t;
4920 BUILD_VA_LIST_TYPE (t);
4922 /* Many back-ends define record types without seting TYPE_NAME.
4923 If we copied the record type here, we'd keep the original
4924 record type without a name. This breaks name mangling. So,
4925 don't copy record types and let c_common_nodes_and_builtins()
4926 declare the type to be __builtin_va_list. */
4927 if (TREE_CODE (t) != RECORD_TYPE)
4928 t = build_type_copy (t);
4930 va_list_type_node = t;
4933 unsigned_V4SI_type_node
4934 = make_vector (V4SImode, unsigned_intSI_type_node, 1);
4935 unsigned_V2SI_type_node
4936 = make_vector (V2SImode, unsigned_intSI_type_node, 1);
4937 unsigned_V4HI_type_node
4938 = make_vector (V4HImode, unsigned_intHI_type_node, 1);
4939 unsigned_V8QI_type_node
4940 = make_vector (V8QImode, unsigned_intQI_type_node, 1);
4941 unsigned_V8HI_type_node
4942 = make_vector (V8HImode, unsigned_intHI_type_node, 1);
4943 unsigned_V16QI_type_node
4944 = make_vector (V16QImode, unsigned_intQI_type_node, 1);
4946 V16SF_type_node = make_vector (V16SFmode, float_type_node, 0);
4947 V4SF_type_node = make_vector (V4SFmode, float_type_node, 0);
4948 V4SI_type_node = make_vector (V4SImode, intSI_type_node, 0);
4949 V2SI_type_node = make_vector (V2SImode, intSI_type_node, 0);
4950 V4HI_type_node = make_vector (V4HImode, intHI_type_node, 0);
4951 V8QI_type_node = make_vector (V8QImode, intQI_type_node, 0);
4952 V8HI_type_node = make_vector (V8HImode, intHI_type_node, 0);
4953 V2SF_type_node = make_vector (V2SFmode, float_type_node, 0);
4954 V16QI_type_node = make_vector (V16QImode, intQI_type_node, 0);
4957 /* Returns a vector tree node given a vector mode, the inner type, and
4958 the signness. */
4960 static tree
4961 make_vector (mode, innertype, unsignedp)
4962 enum machine_mode mode;
4963 tree innertype;
4964 int unsignedp;
4966 tree t;
4968 t = make_node (VECTOR_TYPE);
4969 TREE_TYPE (t) = innertype;
4970 TYPE_MODE (t) = mode;
4971 TREE_UNSIGNED (TREE_TYPE (t)) = unsignedp;
4972 finish_vector_type (t);
4974 return t;