Daily bump.
[official-gcc.git] / gcc / tree.c
blobbd8938758fbe7015955d59343532cd9d3f5e4908
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)) != TREE_UNSIGNED (TREE_TYPE (t2)))
3420 int t1_sgn = tree_int_cst_sgn (t1);
3421 int t2_sgn = tree_int_cst_sgn (t2);
3423 if (t1_sgn < t2_sgn)
3424 return 1;
3425 else if (t1_sgn > t2_sgn)
3426 return 0;
3427 /* Otherwise, both are non-negative, so we compare them as
3428 unsigned just in case one of them would overflow a signed
3429 type. */
3431 else if (! TREE_UNSIGNED (TREE_TYPE (t1)))
3432 return INT_CST_LT (t1, t2);
3434 return INT_CST_LT_UNSIGNED (t1, t2);
3437 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
3440 tree_int_cst_compare (t1, t2)
3441 tree t1;
3442 tree t2;
3444 if (tree_int_cst_lt (t1, t2))
3445 return -1;
3446 else if (tree_int_cst_lt (t2, t1))
3447 return 1;
3448 else
3449 return 0;
3452 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
3453 the host. If POS is zero, the value can be represented in a single
3454 HOST_WIDE_INT. If POS is nonzero, the value must be positive and can
3455 be represented in a single unsigned HOST_WIDE_INT. */
3458 host_integerp (t, pos)
3459 tree t;
3460 int pos;
3462 return (TREE_CODE (t) == INTEGER_CST
3463 && ! TREE_OVERFLOW (t)
3464 && ((TREE_INT_CST_HIGH (t) == 0
3465 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
3466 || (! pos && TREE_INT_CST_HIGH (t) == -1
3467 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
3468 && ! TREE_UNSIGNED (TREE_TYPE (t)))
3469 || (pos && TREE_INT_CST_HIGH (t) == 0)));
3472 /* Return the HOST_WIDE_INT least significant bits of T if it is an
3473 INTEGER_CST and there is no overflow. POS is nonzero if the result must
3474 be positive. Abort if we cannot satisfy the above conditions. */
3476 HOST_WIDE_INT
3477 tree_low_cst (t, pos)
3478 tree t;
3479 int pos;
3481 if (host_integerp (t, pos))
3482 return TREE_INT_CST_LOW (t);
3483 else
3484 abort ();
3487 /* Return the most significant bit of the integer constant T. */
3490 tree_int_cst_msb (t)
3491 tree t;
3493 int prec;
3494 HOST_WIDE_INT h;
3495 unsigned HOST_WIDE_INT l;
3497 /* Note that using TYPE_PRECISION here is wrong. We care about the
3498 actual bits, not the (arbitrary) range of the type. */
3499 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
3500 rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
3501 2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
3502 return (l & 1) == 1;
3505 /* Return an indication of the sign of the integer constant T.
3506 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
3507 Note that -1 will never be returned it T's type is unsigned. */
3510 tree_int_cst_sgn (t)
3511 tree t;
3513 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
3514 return 0;
3515 else if (TREE_UNSIGNED (TREE_TYPE (t)))
3516 return 1;
3517 else if (TREE_INT_CST_HIGH (t) < 0)
3518 return -1;
3519 else
3520 return 1;
3523 /* Compare two constructor-element-type constants. Return 1 if the lists
3524 are known to be equal; otherwise return 0. */
3527 simple_cst_list_equal (l1, l2)
3528 tree l1, l2;
3530 while (l1 != NULL_TREE && l2 != NULL_TREE)
3532 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
3533 return 0;
3535 l1 = TREE_CHAIN (l1);
3536 l2 = TREE_CHAIN (l2);
3539 return l1 == l2;
3542 /* Return truthvalue of whether T1 is the same tree structure as T2.
3543 Return 1 if they are the same.
3544 Return 0 if they are understandably different.
3545 Return -1 if either contains tree structure not understood by
3546 this function. */
3549 simple_cst_equal (t1, t2)
3550 tree t1, t2;
3552 enum tree_code code1, code2;
3553 int cmp;
3554 int i;
3556 if (t1 == t2)
3557 return 1;
3558 if (t1 == 0 || t2 == 0)
3559 return 0;
3561 code1 = TREE_CODE (t1);
3562 code2 = TREE_CODE (t2);
3564 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
3566 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3567 || code2 == NON_LVALUE_EXPR)
3568 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3569 else
3570 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
3573 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3574 || code2 == NON_LVALUE_EXPR)
3575 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
3577 if (code1 != code2)
3578 return 0;
3580 switch (code1)
3582 case INTEGER_CST:
3583 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3584 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
3586 case REAL_CST:
3587 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3589 case STRING_CST:
3590 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3591 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3592 TREE_STRING_LENGTH (t1)));
3594 case CONSTRUCTOR:
3595 if (CONSTRUCTOR_ELTS (t1) == CONSTRUCTOR_ELTS (t2))
3596 return 1;
3597 else
3598 abort ();
3600 case SAVE_EXPR:
3601 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3603 case CALL_EXPR:
3604 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3605 if (cmp <= 0)
3606 return cmp;
3607 return
3608 simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3610 case TARGET_EXPR:
3611 /* Special case: if either target is an unallocated VAR_DECL,
3612 it means that it's going to be unified with whatever the
3613 TARGET_EXPR is really supposed to initialize, so treat it
3614 as being equivalent to anything. */
3615 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
3616 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
3617 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
3618 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
3619 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
3620 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
3621 cmp = 1;
3622 else
3623 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3625 if (cmp <= 0)
3626 return cmp;
3628 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3630 case WITH_CLEANUP_EXPR:
3631 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3632 if (cmp <= 0)
3633 return cmp;
3635 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
3637 case COMPONENT_REF:
3638 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
3639 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3641 return 0;
3643 case VAR_DECL:
3644 case PARM_DECL:
3645 case CONST_DECL:
3646 case FUNCTION_DECL:
3647 return 0;
3649 default:
3650 break;
3653 /* This general rule works for most tree codes. All exceptions should be
3654 handled above. If this is a language-specific tree code, we can't
3655 trust what might be in the operand, so say we don't know
3656 the situation. */
3657 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
3658 return -1;
3660 switch (TREE_CODE_CLASS (code1))
3662 case '1':
3663 case '2':
3664 case '<':
3665 case 'e':
3666 case 'r':
3667 case 's':
3668 cmp = 1;
3669 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
3671 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
3672 if (cmp <= 0)
3673 return cmp;
3676 return cmp;
3678 default:
3679 return -1;
3683 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
3684 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
3685 than U, respectively. */
3688 compare_tree_int (t, u)
3689 tree t;
3690 unsigned HOST_WIDE_INT u;
3692 if (tree_int_cst_sgn (t) < 0)
3693 return -1;
3694 else if (TREE_INT_CST_HIGH (t) != 0)
3695 return 1;
3696 else if (TREE_INT_CST_LOW (t) == u)
3697 return 0;
3698 else if (TREE_INT_CST_LOW (t) < u)
3699 return -1;
3700 else
3701 return 1;
3704 /* Constructors for pointer, array and function types.
3705 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
3706 constructed by language-dependent code, not here.) */
3708 /* Construct, lay out and return the type of pointers to TO_TYPE.
3709 If such a type has already been constructed, reuse it. */
3711 tree
3712 build_pointer_type (to_type)
3713 tree to_type;
3715 tree t = TYPE_POINTER_TO (to_type);
3717 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3719 if (t != 0)
3720 return t;
3722 /* We need a new one. */
3723 t = make_node (POINTER_TYPE);
3725 TREE_TYPE (t) = to_type;
3727 /* Record this type as the pointer to TO_TYPE. */
3728 TYPE_POINTER_TO (to_type) = t;
3730 /* Lay out the type. This function has many callers that are concerned
3731 with expression-construction, and this simplifies them all.
3732 Also, it guarantees the TYPE_SIZE is in the same obstack as the type. */
3733 layout_type (t);
3735 return t;
3738 /* Build the node for the type of references-to-TO_TYPE. */
3740 tree
3741 build_reference_type (to_type)
3742 tree to_type;
3744 tree t = TYPE_REFERENCE_TO (to_type);
3746 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3748 if (t)
3749 return t;
3751 /* We need a new one. */
3752 t = make_node (REFERENCE_TYPE);
3754 TREE_TYPE (t) = to_type;
3756 /* Record this type as the pointer to TO_TYPE. */
3757 TYPE_REFERENCE_TO (to_type) = t;
3759 layout_type (t);
3761 return t;
3764 /* Build a type that is compatible with t but has no cv quals anywhere
3765 in its type, thus
3767 const char *const *const * -> char ***. */
3769 tree
3770 build_type_no_quals (t)
3771 tree t;
3773 switch (TREE_CODE (t))
3775 case POINTER_TYPE:
3776 return build_pointer_type (build_type_no_quals (TREE_TYPE (t)));
3777 case REFERENCE_TYPE:
3778 return build_reference_type (build_type_no_quals (TREE_TYPE (t)));
3779 default:
3780 return TYPE_MAIN_VARIANT (t);
3784 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
3785 MAXVAL should be the maximum value in the domain
3786 (one less than the length of the array).
3788 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
3789 We don't enforce this limit, that is up to caller (e.g. language front end).
3790 The limit exists because the result is a signed type and we don't handle
3791 sizes that use more than one HOST_WIDE_INT. */
3793 tree
3794 build_index_type (maxval)
3795 tree maxval;
3797 tree itype = make_node (INTEGER_TYPE);
3799 TREE_TYPE (itype) = sizetype;
3800 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
3801 TYPE_MIN_VALUE (itype) = size_zero_node;
3802 TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
3803 TYPE_MODE (itype) = TYPE_MODE (sizetype);
3804 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
3805 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
3806 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
3807 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
3809 if (host_integerp (maxval, 1))
3810 return type_hash_canon (tree_low_cst (maxval, 1), itype);
3811 else
3812 return itype;
3815 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
3816 ENUMERAL_TYPE, BOOLEAN_TYPE, or CHAR_TYPE), with
3817 low bound LOWVAL and high bound HIGHVAL.
3818 if TYPE==NULL_TREE, sizetype is used. */
3820 tree
3821 build_range_type (type, lowval, highval)
3822 tree type, lowval, highval;
3824 tree itype = make_node (INTEGER_TYPE);
3826 TREE_TYPE (itype) = type;
3827 if (type == NULL_TREE)
3828 type = sizetype;
3830 TYPE_MIN_VALUE (itype) = convert (type, lowval);
3831 TYPE_MAX_VALUE (itype) = highval ? convert (type, highval) : NULL;
3833 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
3834 TYPE_MODE (itype) = TYPE_MODE (type);
3835 TYPE_SIZE (itype) = TYPE_SIZE (type);
3836 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
3837 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
3838 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
3840 if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
3841 return type_hash_canon (tree_low_cst (highval, 0)
3842 - tree_low_cst (lowval, 0),
3843 itype);
3844 else
3845 return itype;
3848 /* Just like build_index_type, but takes lowval and highval instead
3849 of just highval (maxval). */
3851 tree
3852 build_index_2_type (lowval, highval)
3853 tree lowval, highval;
3855 return build_range_type (sizetype, lowval, highval);
3858 /* Return nonzero iff ITYPE1 and ITYPE2 are equal (in the LISP sense).
3859 Needed because when index types are not hashed, equal index types
3860 built at different times appear distinct, even though structurally,
3861 they are not. */
3864 index_type_equal (itype1, itype2)
3865 tree itype1, itype2;
3867 if (TREE_CODE (itype1) != TREE_CODE (itype2))
3868 return 0;
3870 if (TREE_CODE (itype1) == INTEGER_TYPE)
3872 if (TYPE_PRECISION (itype1) != TYPE_PRECISION (itype2)
3873 || TYPE_MODE (itype1) != TYPE_MODE (itype2)
3874 || simple_cst_equal (TYPE_SIZE (itype1), TYPE_SIZE (itype2)) != 1
3875 || TYPE_ALIGN (itype1) != TYPE_ALIGN (itype2))
3876 return 0;
3878 if (1 == simple_cst_equal (TYPE_MIN_VALUE (itype1),
3879 TYPE_MIN_VALUE (itype2))
3880 && 1 == simple_cst_equal (TYPE_MAX_VALUE (itype1),
3881 TYPE_MAX_VALUE (itype2)))
3882 return 1;
3885 return 0;
3888 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
3889 and number of elements specified by the range of values of INDEX_TYPE.
3890 If such a type has already been constructed, reuse it. */
3892 tree
3893 build_array_type (elt_type, index_type)
3894 tree elt_type, index_type;
3896 tree t;
3897 unsigned int hashcode;
3899 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
3901 error ("arrays of functions are not meaningful");
3902 elt_type = integer_type_node;
3905 /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
3906 build_pointer_type (elt_type);
3908 /* Allocate the array after the pointer type,
3909 in case we free it in type_hash_canon. */
3910 t = make_node (ARRAY_TYPE);
3911 TREE_TYPE (t) = elt_type;
3912 TYPE_DOMAIN (t) = index_type;
3914 if (index_type == 0)
3916 return t;
3919 hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
3920 t = type_hash_canon (hashcode, t);
3922 if (!COMPLETE_TYPE_P (t))
3923 layout_type (t);
3924 return t;
3927 /* Return the TYPE of the elements comprising
3928 the innermost dimension of ARRAY. */
3930 tree
3931 get_inner_array_type (array)
3932 tree array;
3934 tree type = TREE_TYPE (array);
3936 while (TREE_CODE (type) == ARRAY_TYPE)
3937 type = TREE_TYPE (type);
3939 return type;
3942 /* Construct, lay out and return
3943 the type of functions returning type VALUE_TYPE
3944 given arguments of types ARG_TYPES.
3945 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
3946 are data type nodes for the arguments of the function.
3947 If such a type has already been constructed, reuse it. */
3949 tree
3950 build_function_type (value_type, arg_types)
3951 tree value_type, arg_types;
3953 tree t;
3954 unsigned int hashcode;
3956 if (TREE_CODE (value_type) == FUNCTION_TYPE)
3958 error ("function return type cannot be function");
3959 value_type = integer_type_node;
3962 /* Make a node of the sort we want. */
3963 t = make_node (FUNCTION_TYPE);
3964 TREE_TYPE (t) = value_type;
3965 TYPE_ARG_TYPES (t) = arg_types;
3967 /* If we already have such a type, use the old one and free this one. */
3968 hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
3969 t = type_hash_canon (hashcode, t);
3971 if (!COMPLETE_TYPE_P (t))
3972 layout_type (t);
3973 return t;
3976 /* Construct, lay out and return the type of methods belonging to class
3977 BASETYPE and whose arguments and values are described by TYPE.
3978 If that type exists already, reuse it.
3979 TYPE must be a FUNCTION_TYPE node. */
3981 tree
3982 build_method_type (basetype, type)
3983 tree basetype, type;
3985 tree t;
3986 unsigned int hashcode;
3988 /* Make a node of the sort we want. */
3989 t = make_node (METHOD_TYPE);
3991 if (TREE_CODE (type) != FUNCTION_TYPE)
3992 abort ();
3994 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3995 TREE_TYPE (t) = TREE_TYPE (type);
3997 /* The actual arglist for this function includes a "hidden" argument
3998 which is "this". Put it into the list of argument types. */
4000 TYPE_ARG_TYPES (t)
4001 = tree_cons (NULL_TREE,
4002 build_pointer_type (basetype), TYPE_ARG_TYPES (type));
4004 /* If we already have such a type, use the old one and free this one. */
4005 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
4006 t = type_hash_canon (hashcode, t);
4008 if (!COMPLETE_TYPE_P (t))
4009 layout_type (t);
4011 return t;
4014 /* Construct, lay out and return the type of offsets to a value
4015 of type TYPE, within an object of type BASETYPE.
4016 If a suitable offset type exists already, reuse it. */
4018 tree
4019 build_offset_type (basetype, type)
4020 tree basetype, type;
4022 tree t;
4023 unsigned int hashcode;
4025 /* Make a node of the sort we want. */
4026 t = make_node (OFFSET_TYPE);
4028 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
4029 TREE_TYPE (t) = type;
4031 /* If we already have such a type, use the old one and free this one. */
4032 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
4033 t = type_hash_canon (hashcode, t);
4035 if (!COMPLETE_TYPE_P (t))
4036 layout_type (t);
4038 return t;
4041 /* Create a complex type whose components are COMPONENT_TYPE. */
4043 tree
4044 build_complex_type (component_type)
4045 tree component_type;
4047 tree t;
4048 unsigned int hashcode;
4050 /* Make a node of the sort we want. */
4051 t = make_node (COMPLEX_TYPE);
4053 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
4054 set_type_quals (t, TYPE_QUALS (component_type));
4056 /* If we already have such a type, use the old one and free this one. */
4057 hashcode = TYPE_HASH (component_type);
4058 t = type_hash_canon (hashcode, t);
4060 if (!COMPLETE_TYPE_P (t))
4061 layout_type (t);
4063 /* If we are writing Dwarf2 output we need to create a name,
4064 since complex is a fundamental type. */
4065 if ((write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
4066 && ! TYPE_NAME (t))
4068 const char *name;
4069 if (component_type == char_type_node)
4070 name = "complex char";
4071 else if (component_type == signed_char_type_node)
4072 name = "complex signed char";
4073 else if (component_type == unsigned_char_type_node)
4074 name = "complex unsigned char";
4075 else if (component_type == short_integer_type_node)
4076 name = "complex short int";
4077 else if (component_type == short_unsigned_type_node)
4078 name = "complex short unsigned int";
4079 else if (component_type == integer_type_node)
4080 name = "complex int";
4081 else if (component_type == unsigned_type_node)
4082 name = "complex unsigned int";
4083 else if (component_type == long_integer_type_node)
4084 name = "complex long int";
4085 else if (component_type == long_unsigned_type_node)
4086 name = "complex long unsigned int";
4087 else if (component_type == long_long_integer_type_node)
4088 name = "complex long long int";
4089 else if (component_type == long_long_unsigned_type_node)
4090 name = "complex long long unsigned int";
4091 else
4092 name = 0;
4094 if (name != 0)
4095 TYPE_NAME (t) = get_identifier (name);
4098 return t;
4101 /* Return OP, stripped of any conversions to wider types as much as is safe.
4102 Converting the value back to OP's type makes a value equivalent to OP.
4104 If FOR_TYPE is nonzero, we return a value which, if converted to
4105 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
4107 If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
4108 narrowest type that can hold the value, even if they don't exactly fit.
4109 Otherwise, bit-field references are changed to a narrower type
4110 only if they can be fetched directly from memory in that type.
4112 OP must have integer, real or enumeral type. Pointers are not allowed!
4114 There are some cases where the obvious value we could return
4115 would regenerate to OP if converted to OP's type,
4116 but would not extend like OP to wider types.
4117 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
4118 For example, if OP is (unsigned short)(signed char)-1,
4119 we avoid returning (signed char)-1 if FOR_TYPE is int,
4120 even though extending that to an unsigned short would regenerate OP,
4121 since the result of extending (signed char)-1 to (int)
4122 is different from (int) OP. */
4124 tree
4125 get_unwidened (op, for_type)
4126 tree op;
4127 tree for_type;
4129 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
4130 tree type = TREE_TYPE (op);
4131 unsigned final_prec
4132 = TYPE_PRECISION (for_type != 0 ? for_type : type);
4133 int uns
4134 = (for_type != 0 && for_type != type
4135 && final_prec > TYPE_PRECISION (type)
4136 && TREE_UNSIGNED (type));
4137 tree win = op;
4139 while (TREE_CODE (op) == NOP_EXPR)
4141 int bitschange
4142 = TYPE_PRECISION (TREE_TYPE (op))
4143 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
4145 /* Truncations are many-one so cannot be removed.
4146 Unless we are later going to truncate down even farther. */
4147 if (bitschange < 0
4148 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
4149 break;
4151 /* See what's inside this conversion. If we decide to strip it,
4152 we will set WIN. */
4153 op = TREE_OPERAND (op, 0);
4155 /* If we have not stripped any zero-extensions (uns is 0),
4156 we can strip any kind of extension.
4157 If we have previously stripped a zero-extension,
4158 only zero-extensions can safely be stripped.
4159 Any extension can be stripped if the bits it would produce
4160 are all going to be discarded later by truncating to FOR_TYPE. */
4162 if (bitschange > 0)
4164 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
4165 win = op;
4166 /* TREE_UNSIGNED says whether this is a zero-extension.
4167 Let's avoid computing it if it does not affect WIN
4168 and if UNS will not be needed again. */
4169 if ((uns || TREE_CODE (op) == NOP_EXPR)
4170 && TREE_UNSIGNED (TREE_TYPE (op)))
4172 uns = 1;
4173 win = op;
4178 if (TREE_CODE (op) == COMPONENT_REF
4179 /* Since type_for_size always gives an integer type. */
4180 && TREE_CODE (type) != REAL_TYPE
4181 /* Don't crash if field not laid out yet. */
4182 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
4183 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
4185 unsigned int innerprec
4186 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4188 type = type_for_size (innerprec, TREE_UNSIGNED (TREE_OPERAND (op, 1)));
4190 /* We can get this structure field in the narrowest type it fits in.
4191 If FOR_TYPE is 0, do this only for a field that matches the
4192 narrower type exactly and is aligned for it
4193 The resulting extension to its nominal type (a fullword type)
4194 must fit the same conditions as for other extensions. */
4196 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
4197 && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
4198 && (! uns || final_prec <= innerprec
4199 || TREE_UNSIGNED (TREE_OPERAND (op, 1)))
4200 && type != 0)
4202 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4203 TREE_OPERAND (op, 1));
4204 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4205 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4209 return win;
4212 /* Return OP or a simpler expression for a narrower value
4213 which can be sign-extended or zero-extended to give back OP.
4214 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
4215 or 0 if the value should be sign-extended. */
4217 tree
4218 get_narrower (op, unsignedp_ptr)
4219 tree op;
4220 int *unsignedp_ptr;
4222 int uns = 0;
4223 int first = 1;
4224 tree win = op;
4226 while (TREE_CODE (op) == NOP_EXPR)
4228 int bitschange
4229 = (TYPE_PRECISION (TREE_TYPE (op))
4230 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
4232 /* Truncations are many-one so cannot be removed. */
4233 if (bitschange < 0)
4234 break;
4236 /* See what's inside this conversion. If we decide to strip it,
4237 we will set WIN. */
4238 op = TREE_OPERAND (op, 0);
4240 if (bitschange > 0)
4242 /* An extension: the outermost one can be stripped,
4243 but remember whether it is zero or sign extension. */
4244 if (first)
4245 uns = TREE_UNSIGNED (TREE_TYPE (op));
4246 /* Otherwise, if a sign extension has been stripped,
4247 only sign extensions can now be stripped;
4248 if a zero extension has been stripped, only zero-extensions. */
4249 else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
4250 break;
4251 first = 0;
4253 else /* bitschange == 0 */
4255 /* A change in nominal type can always be stripped, but we must
4256 preserve the unsignedness. */
4257 if (first)
4258 uns = TREE_UNSIGNED (TREE_TYPE (op));
4259 first = 0;
4262 win = op;
4265 if (TREE_CODE (op) == COMPONENT_REF
4266 /* Since type_for_size always gives an integer type. */
4267 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
4268 /* Ensure field is laid out already. */
4269 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0)
4271 unsigned HOST_WIDE_INT innerprec
4272 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4273 tree type = type_for_size (innerprec, TREE_UNSIGNED (op));
4275 /* We can get this structure field in a narrower type that fits it,
4276 but the resulting extension to its nominal type (a fullword type)
4277 must satisfy the same conditions as for other extensions.
4279 Do this only for fields that are aligned (not bit-fields),
4280 because when bit-field insns will be used there is no
4281 advantage in doing this. */
4283 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
4284 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
4285 && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
4286 && type != 0)
4288 if (first)
4289 uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
4290 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4291 TREE_OPERAND (op, 1));
4292 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4293 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4296 *unsignedp_ptr = uns;
4297 return win;
4300 /* Nonzero if integer constant C has a value that is permissible
4301 for type TYPE (an INTEGER_TYPE). */
4304 int_fits_type_p (c, type)
4305 tree c, type;
4307 /* If the bounds of the type are integers, we can check ourselves.
4308 If not, but this type is a subtype, try checking against that.
4309 Otherwise, use force_fit_type, which checks against the precision. */
4310 if (TYPE_MAX_VALUE (type) != NULL_TREE
4311 && TYPE_MIN_VALUE (type) != NULL_TREE
4312 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST
4313 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
4315 if (TREE_UNSIGNED (type))
4316 return (! INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c)
4317 && ! INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type))
4318 /* Negative ints never fit unsigned types. */
4319 && ! (TREE_INT_CST_HIGH (c) < 0
4320 && ! TREE_UNSIGNED (TREE_TYPE (c))));
4321 else
4322 return (! INT_CST_LT (TYPE_MAX_VALUE (type), c)
4323 && ! INT_CST_LT (c, TYPE_MIN_VALUE (type))
4324 /* Unsigned ints with top bit set never fit signed types. */
4325 && ! (TREE_INT_CST_HIGH (c) < 0
4326 && TREE_UNSIGNED (TREE_TYPE (c))));
4328 else if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != 0)
4329 return int_fits_type_p (c, TREE_TYPE (type));
4330 else
4332 c = copy_node (c);
4333 TREE_TYPE (c) = type;
4334 return !force_fit_type (c, 0);
4338 /* Given a DECL or TYPE, return the scope in which it was declared, or
4339 NULL_TREE if there is no containing scope. */
4341 tree
4342 get_containing_scope (t)
4343 tree t;
4345 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
4348 /* Return the innermost context enclosing DECL that is
4349 a FUNCTION_DECL, or zero if none. */
4351 tree
4352 decl_function_context (decl)
4353 tree decl;
4355 tree context;
4357 if (TREE_CODE (decl) == ERROR_MARK)
4358 return 0;
4360 if (TREE_CODE (decl) == SAVE_EXPR)
4361 context = SAVE_EXPR_CONTEXT (decl);
4363 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
4364 where we look up the function at runtime. Such functions always take
4365 a first argument of type 'pointer to real context'.
4367 C++ should really be fixed to use DECL_CONTEXT for the real context,
4368 and use something else for the "virtual context". */
4369 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
4370 context
4371 = TYPE_MAIN_VARIANT
4372 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4373 else
4374 context = DECL_CONTEXT (decl);
4376 while (context && TREE_CODE (context) != FUNCTION_DECL)
4378 if (TREE_CODE (context) == BLOCK)
4379 context = BLOCK_SUPERCONTEXT (context);
4380 else
4381 context = get_containing_scope (context);
4384 return context;
4387 /* Return the innermost context enclosing DECL that is
4388 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
4389 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
4391 tree
4392 decl_type_context (decl)
4393 tree decl;
4395 tree context = DECL_CONTEXT (decl);
4397 while (context)
4399 if (TREE_CODE (context) == RECORD_TYPE
4400 || TREE_CODE (context) == UNION_TYPE
4401 || TREE_CODE (context) == QUAL_UNION_TYPE)
4402 return context;
4404 if (TREE_CODE (context) == TYPE_DECL
4405 || TREE_CODE (context) == FUNCTION_DECL)
4406 context = DECL_CONTEXT (context);
4408 else if (TREE_CODE (context) == BLOCK)
4409 context = BLOCK_SUPERCONTEXT (context);
4411 else
4412 /* Unhandled CONTEXT!? */
4413 abort ();
4415 return NULL_TREE;
4418 /* CALL is a CALL_EXPR. Return the declaration for the function
4419 called, or NULL_TREE if the called function cannot be
4420 determined. */
4422 tree
4423 get_callee_fndecl (call)
4424 tree call;
4426 tree addr;
4428 /* It's invalid to call this function with anything but a
4429 CALL_EXPR. */
4430 if (TREE_CODE (call) != CALL_EXPR)
4431 abort ();
4433 /* The first operand to the CALL is the address of the function
4434 called. */
4435 addr = TREE_OPERAND (call, 0);
4437 STRIP_NOPS (addr);
4439 /* If this is a readonly function pointer, extract its initial value. */
4440 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
4441 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
4442 && DECL_INITIAL (addr))
4443 addr = DECL_INITIAL (addr);
4445 /* If the address is just `&f' for some function `f', then we know
4446 that `f' is being called. */
4447 if (TREE_CODE (addr) == ADDR_EXPR
4448 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
4449 return TREE_OPERAND (addr, 0);
4451 /* We couldn't figure out what was being called. */
4452 return NULL_TREE;
4455 /* Print debugging information about the obstack O, named STR. */
4457 void
4458 print_obstack_statistics (str, o)
4459 const char *str;
4460 struct obstack *o;
4462 struct _obstack_chunk *chunk = o->chunk;
4463 int n_chunks = 1;
4464 int n_alloc = 0;
4466 n_alloc += o->next_free - chunk->contents;
4467 chunk = chunk->prev;
4468 while (chunk)
4470 n_chunks += 1;
4471 n_alloc += chunk->limit - &chunk->contents[0];
4472 chunk = chunk->prev;
4474 fprintf (stderr, "obstack %s: %u bytes, %d chunks\n",
4475 str, n_alloc, n_chunks);
4478 /* Print debugging information about tree nodes generated during the compile,
4479 and any language-specific information. */
4481 void
4482 dump_tree_statistics ()
4484 #ifdef GATHER_STATISTICS
4485 int i;
4486 int total_nodes, total_bytes;
4487 #endif
4489 fprintf (stderr, "\n??? tree nodes created\n\n");
4490 #ifdef GATHER_STATISTICS
4491 fprintf (stderr, "Kind Nodes Bytes\n");
4492 fprintf (stderr, "-------------------------------------\n");
4493 total_nodes = total_bytes = 0;
4494 for (i = 0; i < (int) all_kinds; i++)
4496 fprintf (stderr, "%-20s %6d %9d\n", tree_node_kind_names[i],
4497 tree_node_counts[i], tree_node_sizes[i]);
4498 total_nodes += tree_node_counts[i];
4499 total_bytes += tree_node_sizes[i];
4501 fprintf (stderr, "-------------------------------------\n");
4502 fprintf (stderr, "%-20s %6d %9d\n", "Total", total_nodes, total_bytes);
4503 fprintf (stderr, "-------------------------------------\n");
4504 #else
4505 fprintf (stderr, "(No per-node statistics)\n");
4506 #endif
4507 print_obstack_statistics ("permanent_obstack", &permanent_obstack);
4508 print_type_hash_statistics ();
4509 (*lang_hooks.print_statistics) ();
4512 #define FILE_FUNCTION_PREFIX_LEN 9
4514 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
4516 /* Appends 6 random characters to TEMPLATE to (hopefully) avoid name
4517 clashes in cases where we can't reliably choose a unique name.
4519 Derived from mkstemp.c in libiberty. */
4521 static void
4522 append_random_chars (template)
4523 char *template;
4525 static const char letters[]
4526 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
4527 static unsigned HOST_WIDE_INT value;
4528 unsigned HOST_WIDE_INT v;
4530 if (! value)
4532 struct stat st;
4534 /* VALUE should be unique for each file and must not change between
4535 compiles since this can cause bootstrap comparison errors. */
4537 if (stat (main_input_filename, &st) < 0)
4539 /* This can happen when preprocessed text is shipped between
4540 machines, e.g. with bug reports. Assume that uniqueness
4541 isn't actually an issue. */
4542 value = 1;
4544 else
4546 /* In VMS, ino is an array, so we have to use both values. We
4547 conditionalize that. */
4548 #ifdef VMS
4549 #define INO_TO_INT(INO) ((int) (INO)[1] << 16 ^ (int) (INO)[2])
4550 #else
4551 #define INO_TO_INT(INO) INO
4552 #endif
4553 value = st.st_dev ^ INO_TO_INT (st.st_ino) ^ st.st_mtime;
4557 template += strlen (template);
4559 v = value;
4561 /* Fill in the random bits. */
4562 template[0] = letters[v % 62];
4563 v /= 62;
4564 template[1] = letters[v % 62];
4565 v /= 62;
4566 template[2] = letters[v % 62];
4567 v /= 62;
4568 template[3] = letters[v % 62];
4569 v /= 62;
4570 template[4] = letters[v % 62];
4571 v /= 62;
4572 template[5] = letters[v % 62];
4574 template[6] = '\0';
4577 /* P is a string that will be used in a symbol. Mask out any characters
4578 that are not valid in that context. */
4580 void
4581 clean_symbol_name (p)
4582 char *p;
4584 for (; *p; p++)
4585 if (! (ISALNUM (*p)
4586 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
4587 || *p == '$'
4588 #endif
4589 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
4590 || *p == '.'
4591 #endif
4593 *p = '_';
4596 /* Generate a name for a function unique to this translation unit.
4597 TYPE is some string to identify the purpose of this function to the
4598 linker or collect2. */
4600 tree
4601 get_file_function_name_long (type)
4602 const char *type;
4604 char *buf;
4605 const char *p;
4606 char *q;
4608 if (first_global_object_name)
4609 p = first_global_object_name;
4610 else
4612 /* We don't have anything that we know to be unique to this translation
4613 unit, so use what we do have and throw in some randomness. */
4615 const char *name = weak_global_object_name;
4616 const char *file = main_input_filename;
4618 if (! name)
4619 name = "";
4620 if (! file)
4621 file = input_filename;
4623 q = (char *) alloca (7 + strlen (name) + strlen (file));
4625 sprintf (q, "%s%s", name, file);
4626 append_random_chars (q);
4627 p = q;
4630 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
4631 + strlen (type));
4633 /* Set up the name of the file-level functions we may need.
4634 Use a global object (which is already required to be unique over
4635 the program) rather than the file name (which imposes extra
4636 constraints). */
4637 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
4639 /* Don't need to pull weird characters out of global names. */
4640 if (p != first_global_object_name)
4641 clean_symbol_name (buf + 11);
4643 return get_identifier (buf);
4646 /* If KIND=='I', return a suitable global initializer (constructor) name.
4647 If KIND=='D', return a suitable global clean-up (destructor) name. */
4649 tree
4650 get_file_function_name (kind)
4651 int kind;
4653 char p[2];
4655 p[0] = kind;
4656 p[1] = 0;
4658 return get_file_function_name_long (p);
4661 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4662 The result is placed in BUFFER (which has length BIT_SIZE),
4663 with one bit in each char ('\000' or '\001').
4665 If the constructor is constant, NULL_TREE is returned.
4666 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4668 tree
4669 get_set_constructor_bits (init, buffer, bit_size)
4670 tree init;
4671 char *buffer;
4672 int bit_size;
4674 int i;
4675 tree vals;
4676 HOST_WIDE_INT domain_min
4677 = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (init))), 0);
4678 tree non_const_bits = NULL_TREE;
4680 for (i = 0; i < bit_size; i++)
4681 buffer[i] = 0;
4683 for (vals = TREE_OPERAND (init, 1);
4684 vals != NULL_TREE; vals = TREE_CHAIN (vals))
4686 if (!host_integerp (TREE_VALUE (vals), 0)
4687 || (TREE_PURPOSE (vals) != NULL_TREE
4688 && !host_integerp (TREE_PURPOSE (vals), 0)))
4689 non_const_bits
4690 = tree_cons (TREE_PURPOSE (vals), TREE_VALUE (vals), non_const_bits);
4691 else if (TREE_PURPOSE (vals) != NULL_TREE)
4693 /* Set a range of bits to ones. */
4694 HOST_WIDE_INT lo_index
4695 = tree_low_cst (TREE_PURPOSE (vals), 0) - domain_min;
4696 HOST_WIDE_INT hi_index
4697 = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
4699 if (lo_index < 0 || lo_index >= bit_size
4700 || hi_index < 0 || hi_index >= bit_size)
4701 abort ();
4702 for (; lo_index <= hi_index; lo_index++)
4703 buffer[lo_index] = 1;
4705 else
4707 /* Set a single bit to one. */
4708 HOST_WIDE_INT index
4709 = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
4710 if (index < 0 || index >= bit_size)
4712 error ("invalid initializer for bit string");
4713 return NULL_TREE;
4715 buffer[index] = 1;
4718 return non_const_bits;
4721 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4722 The result is placed in BUFFER (which is an array of bytes).
4723 If the constructor is constant, NULL_TREE is returned.
4724 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4726 tree
4727 get_set_constructor_bytes (init, buffer, wd_size)
4728 tree init;
4729 unsigned char *buffer;
4730 int wd_size;
4732 int i;
4733 int set_word_size = BITS_PER_UNIT;
4734 int bit_size = wd_size * set_word_size;
4735 int bit_pos = 0;
4736 unsigned char *bytep = buffer;
4737 char *bit_buffer = (char *) alloca (bit_size);
4738 tree non_const_bits = get_set_constructor_bits (init, bit_buffer, bit_size);
4740 for (i = 0; i < wd_size; i++)
4741 buffer[i] = 0;
4743 for (i = 0; i < bit_size; i++)
4745 if (bit_buffer[i])
4747 if (BYTES_BIG_ENDIAN)
4748 *bytep |= (1 << (set_word_size - 1 - bit_pos));
4749 else
4750 *bytep |= 1 << bit_pos;
4752 bit_pos++;
4753 if (bit_pos >= set_word_size)
4754 bit_pos = 0, bytep++;
4756 return non_const_bits;
4759 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4760 /* Complain that the tree code of NODE does not match the expected CODE.
4761 FILE, LINE, and FUNCTION are of the caller. */
4763 void
4764 tree_check_failed (node, code, file, line, function)
4765 const tree node;
4766 enum tree_code code;
4767 const char *file;
4768 int line;
4769 const char *function;
4771 internal_error ("tree check: expected %s, have %s in %s, at %s:%d",
4772 tree_code_name[code], tree_code_name[TREE_CODE (node)],
4773 function, trim_filename (file), line);
4776 /* Similar to above, except that we check for a class of tree
4777 code, given in CL. */
4779 void
4780 tree_class_check_failed (node, cl, file, line, function)
4781 const tree node;
4782 int cl;
4783 const char *file;
4784 int line;
4785 const char *function;
4787 internal_error
4788 ("tree check: expected class '%c', have '%c' (%s) in %s, at %s:%d",
4789 cl, TREE_CODE_CLASS (TREE_CODE (node)),
4790 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
4793 #endif /* ENABLE_TREE_CHECKING */
4795 /* For a new vector type node T, build the information necessary for
4796 debuggint output. */
4798 static void
4799 finish_vector_type (t)
4800 tree t;
4802 layout_type (t);
4805 tree index = build_int_2 (TYPE_VECTOR_SUBPARTS (t) - 1, 0);
4806 tree array = build_array_type (TREE_TYPE (t),
4807 build_index_type (index));
4808 tree rt = make_node (RECORD_TYPE);
4810 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
4811 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
4812 layout_type (rt);
4813 TYPE_DEBUG_REPRESENTATION_TYPE (t) = rt;
4814 /* In dwarfout.c, type lookup uses TYPE_UID numbers. We want to output
4815 the representation type, and we want to find that die when looking up
4816 the vector type. This is most easily achieved by making the TYPE_UID
4817 numbers equal. */
4818 TYPE_UID (rt) = TYPE_UID (t);
4822 /* Create nodes for all integer types (and error_mark_node) using the sizes
4823 of C datatypes. The caller should call set_sizetype soon after calling
4824 this function to select one of the types as sizetype. */
4826 void
4827 build_common_tree_nodes (signed_char)
4828 int signed_char;
4830 error_mark_node = make_node (ERROR_MARK);
4831 TREE_TYPE (error_mark_node) = error_mark_node;
4833 initialize_sizetypes ();
4835 /* Define both `signed char' and `unsigned char'. */
4836 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
4837 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
4839 /* Define `char', which is like either `signed char' or `unsigned char'
4840 but not the same as either. */
4841 char_type_node
4842 = (signed_char
4843 ? make_signed_type (CHAR_TYPE_SIZE)
4844 : make_unsigned_type (CHAR_TYPE_SIZE));
4846 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
4847 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
4848 integer_type_node = make_signed_type (INT_TYPE_SIZE);
4849 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
4850 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
4851 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
4852 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
4853 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
4855 intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
4856 intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
4857 intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
4858 intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
4859 intTI_type_node = make_signed_type (GET_MODE_BITSIZE (TImode));
4861 unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
4862 unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
4863 unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
4864 unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
4865 unsigned_intTI_type_node = make_unsigned_type (GET_MODE_BITSIZE (TImode));
4868 /* Call this function after calling build_common_tree_nodes and set_sizetype.
4869 It will create several other common tree nodes. */
4871 void
4872 build_common_tree_nodes_2 (short_double)
4873 int short_double;
4875 /* Define these next since types below may used them. */
4876 integer_zero_node = build_int_2 (0, 0);
4877 integer_one_node = build_int_2 (1, 0);
4878 integer_minus_one_node = build_int_2 (-1, -1);
4880 size_zero_node = size_int (0);
4881 size_one_node = size_int (1);
4882 bitsize_zero_node = bitsize_int (0);
4883 bitsize_one_node = bitsize_int (1);
4884 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
4886 void_type_node = make_node (VOID_TYPE);
4887 layout_type (void_type_node);
4889 /* We are not going to have real types in C with less than byte alignment,
4890 so we might as well not have any types that claim to have it. */
4891 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
4892 TYPE_USER_ALIGN (void_type_node) = 0;
4894 null_pointer_node = build_int_2 (0, 0);
4895 TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
4896 layout_type (TREE_TYPE (null_pointer_node));
4898 ptr_type_node = build_pointer_type (void_type_node);
4899 const_ptr_type_node
4900 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
4902 float_type_node = make_node (REAL_TYPE);
4903 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
4904 layout_type (float_type_node);
4906 double_type_node = make_node (REAL_TYPE);
4907 if (short_double)
4908 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
4909 else
4910 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
4911 layout_type (double_type_node);
4913 long_double_type_node = make_node (REAL_TYPE);
4914 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
4915 layout_type (long_double_type_node);
4917 complex_integer_type_node = make_node (COMPLEX_TYPE);
4918 TREE_TYPE (complex_integer_type_node) = integer_type_node;
4919 layout_type (complex_integer_type_node);
4921 complex_float_type_node = make_node (COMPLEX_TYPE);
4922 TREE_TYPE (complex_float_type_node) = float_type_node;
4923 layout_type (complex_float_type_node);
4925 complex_double_type_node = make_node (COMPLEX_TYPE);
4926 TREE_TYPE (complex_double_type_node) = double_type_node;
4927 layout_type (complex_double_type_node);
4929 complex_long_double_type_node = make_node (COMPLEX_TYPE);
4930 TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
4931 layout_type (complex_long_double_type_node);
4934 tree t;
4935 BUILD_VA_LIST_TYPE (t);
4937 /* Many back-ends define record types without seting TYPE_NAME.
4938 If we copied the record type here, we'd keep the original
4939 record type without a name. This breaks name mangling. So,
4940 don't copy record types and let c_common_nodes_and_builtins()
4941 declare the type to be __builtin_va_list. */
4942 if (TREE_CODE (t) != RECORD_TYPE)
4943 t = build_type_copy (t);
4945 va_list_type_node = t;
4948 unsigned_V4SI_type_node
4949 = make_vector (V4SImode, unsigned_intSI_type_node, 1);
4950 unsigned_V2SI_type_node
4951 = make_vector (V2SImode, unsigned_intSI_type_node, 1);
4952 unsigned_V4HI_type_node
4953 = make_vector (V4HImode, unsigned_intHI_type_node, 1);
4954 unsigned_V8QI_type_node
4955 = make_vector (V8QImode, unsigned_intQI_type_node, 1);
4956 unsigned_V8HI_type_node
4957 = make_vector (V8HImode, unsigned_intHI_type_node, 1);
4958 unsigned_V16QI_type_node
4959 = make_vector (V16QImode, unsigned_intQI_type_node, 1);
4961 V16SF_type_node = make_vector (V16SFmode, float_type_node, 0);
4962 V4SF_type_node = make_vector (V4SFmode, float_type_node, 0);
4963 V4SI_type_node = make_vector (V4SImode, intSI_type_node, 0);
4964 V2SI_type_node = make_vector (V2SImode, intSI_type_node, 0);
4965 V4HI_type_node = make_vector (V4HImode, intHI_type_node, 0);
4966 V8QI_type_node = make_vector (V8QImode, intQI_type_node, 0);
4967 V8HI_type_node = make_vector (V8HImode, intHI_type_node, 0);
4968 V2SF_type_node = make_vector (V2SFmode, float_type_node, 0);
4969 V16QI_type_node = make_vector (V16QImode, intQI_type_node, 0);
4972 /* Returns a vector tree node given a vector mode, the inner type, and
4973 the signness. */
4975 static tree
4976 make_vector (mode, innertype, unsignedp)
4977 enum machine_mode mode;
4978 tree innertype;
4979 int unsignedp;
4981 tree t;
4983 t = make_node (VECTOR_TYPE);
4984 TREE_TYPE (t) = innertype;
4985 TYPE_MODE (t) = mode;
4986 TREE_UNSIGNED (TREE_TYPE (t)) = unsignedp;
4987 finish_vector_type (t);
4989 return t;