2004-02-24 Aldy Hernandez <aldyh@redhat.com>
[official-gcc.git] / gcc / tree.c
blob3913b55f33e3dd66bb9408b40c9267645578bdcc
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, 2003, 2004 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 #include "config.h"
33 #include "system.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "flags.h"
37 #include "tree.h"
38 #include "real.h"
39 #include "tm_p.h"
40 #include "function.h"
41 #include "obstack.h"
42 #include "toplev.h"
43 #include "ggc.h"
44 #include "hashtab.h"
45 #include "output.h"
46 #include "target.h"
47 #include "langhooks.h"
49 /* obstack.[ch] explicitly declined to prototype this. */
50 extern int _obstack_allocated_p (struct obstack *h, void *obj);
52 #ifdef GATHER_STATISTICS
53 /* Statistics-gathering stuff. */
55 int tree_node_counts[(int) all_kinds];
56 int tree_node_sizes[(int) all_kinds];
58 /* Keep in sync with tree.h:enum tree_node_kind. */
59 static const char * const tree_node_kind_names[] = {
60 "decls",
61 "types",
62 "blocks",
63 "stmts",
64 "refs",
65 "exprs",
66 "constants",
67 "identifiers",
68 "perm_tree_lists",
69 "temp_tree_lists",
70 "vecs",
71 "random kinds",
72 "lang_decl kinds",
73 "lang_type kinds"
75 #endif /* GATHER_STATISTICS */
77 /* Unique id for next decl created. */
78 static GTY(()) int next_decl_uid;
79 /* Unique id for next type created. */
80 static GTY(()) int next_type_uid = 1;
82 /* Since we cannot rehash a type after it is in the table, we have to
83 keep the hash code. */
85 struct type_hash GTY(())
87 unsigned long hash;
88 tree type;
91 /* Initial size of the hash table (rounded to next prime). */
92 #define TYPE_HASH_INITIAL_SIZE 1000
94 /* Now here is the hash table. When recording a type, it is added to
95 the slot whose index is the hash code. Note that the hash table is
96 used for several kinds of types (function types, array types and
97 array index range types, for now). While all these live in the
98 same table, they are completely independent, and the hash code is
99 computed differently for each of these. */
101 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
102 htab_t type_hash_table;
104 static void set_type_quals (tree, int);
105 static int type_hash_eq (const void *, const void *);
106 static hashval_t type_hash_hash (const void *);
107 static void print_type_hash_statistics (void);
108 static void finish_vector_type (tree);
109 static int type_hash_marked_p (const void *);
111 tree global_trees[TI_MAX];
112 tree integer_types[itk_none];
114 /* Init tree.c. */
116 void
117 init_ttree (void)
119 /* Initialize the hash table of types. */
120 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
121 type_hash_eq, 0);
125 /* The name of the object as the assembler will see it (but before any
126 translations made by ASM_OUTPUT_LABELREF). Often this is the same
127 as DECL_NAME. It is an IDENTIFIER_NODE. */
128 tree
129 decl_assembler_name (tree decl)
131 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
132 (*lang_hooks.set_decl_assembler_name) (decl);
133 return DECL_CHECK (decl)->decl.assembler_name;
136 /* Compute the number of bytes occupied by 'node'. This routine only
137 looks at TREE_CODE and, if the code is TREE_VEC, TREE_VEC_LENGTH. */
138 size_t
139 tree_size (tree node)
141 enum tree_code code = TREE_CODE (node);
143 switch (TREE_CODE_CLASS (code))
145 case 'd': /* A decl node */
146 return sizeof (struct tree_decl);
148 case 't': /* a type node */
149 return sizeof (struct tree_type);
151 case 'b': /* a lexical block node */
152 return sizeof (struct tree_block);
154 case 'r': /* a reference */
155 case 'e': /* an expression */
156 case 's': /* an expression with side effects */
157 case '<': /* a comparison expression */
158 case '1': /* a unary arithmetic expression */
159 case '2': /* a binary arithmetic expression */
160 return (sizeof (struct tree_exp)
161 + TREE_CODE_LENGTH (code) * sizeof (char *) - sizeof (char *));
163 case 'c': /* a constant */
164 switch (code)
166 case INTEGER_CST: return sizeof (struct tree_int_cst);
167 case REAL_CST: return sizeof (struct tree_real_cst);
168 case COMPLEX_CST: return sizeof (struct tree_complex);
169 case VECTOR_CST: return sizeof (struct tree_vector);
170 case STRING_CST: return sizeof (struct tree_string);
171 default:
172 return (*lang_hooks.tree_size) (code);
175 case 'x': /* something random, like an identifier. */
176 switch (code)
178 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
179 case TREE_LIST: return sizeof (struct tree_list);
180 case TREE_VEC: return (sizeof (struct tree_vec)
181 + TREE_VEC_LENGTH(node) * sizeof(char *)
182 - sizeof (char *));
184 case ERROR_MARK:
185 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
187 default:
188 return (*lang_hooks.tree_size) (code);
191 default:
192 abort ();
196 /* Return a newly allocated node of code CODE.
197 For decl and type nodes, some other fields are initialized.
198 The rest of the node is initialized to zero.
200 Achoo! I got a code in the node. */
202 tree
203 make_node (enum tree_code code)
205 tree t;
206 int type = TREE_CODE_CLASS (code);
207 size_t length;
208 #ifdef GATHER_STATISTICS
209 tree_node_kind kind;
210 #endif
211 struct tree_common ttmp;
213 /* We can't allocate a TREE_VEC without knowing how many elements
214 it will have. */
215 if (code == TREE_VEC)
216 abort ();
218 TREE_SET_CODE ((tree)&ttmp, code);
219 length = tree_size ((tree)&ttmp);
221 #ifdef GATHER_STATISTICS
222 switch (type)
224 case 'd': /* A decl node */
225 kind = d_kind;
226 break;
228 case 't': /* a type node */
229 kind = t_kind;
230 break;
232 case 'b': /* a lexical block */
233 kind = b_kind;
234 break;
236 case 's': /* an expression with side effects */
237 kind = s_kind;
238 break;
240 case 'r': /* a reference */
241 kind = r_kind;
242 break;
244 case 'e': /* an expression */
245 case '<': /* a comparison expression */
246 case '1': /* a unary arithmetic expression */
247 case '2': /* a binary arithmetic expression */
248 kind = e_kind;
249 break;
251 case 'c': /* a constant */
252 kind = c_kind;
253 break;
255 case 'x': /* something random, like an identifier. */
256 if (code == IDENTIFIER_NODE)
257 kind = id_kind;
258 else if (code == TREE_VEC)
259 kind = vec_kind;
260 else
261 kind = x_kind;
262 break;
264 default:
265 abort ();
268 tree_node_counts[(int) kind]++;
269 tree_node_sizes[(int) kind] += length;
270 #endif
272 t = ggc_alloc_tree (length);
274 memset (t, 0, length);
276 TREE_SET_CODE (t, code);
278 switch (type)
280 case 's':
281 TREE_SIDE_EFFECTS (t) = 1;
282 break;
284 case 'd':
285 if (code != FUNCTION_DECL)
286 DECL_ALIGN (t) = 1;
287 DECL_USER_ALIGN (t) = 0;
288 DECL_IN_SYSTEM_HEADER (t) = in_system_header;
289 DECL_SOURCE_LOCATION (t) = input_location;
290 DECL_UID (t) = next_decl_uid++;
292 /* We have not yet computed the alias set for this declaration. */
293 DECL_POINTER_ALIAS_SET (t) = -1;
294 break;
296 case 't':
297 TYPE_UID (t) = next_type_uid++;
298 TYPE_ALIGN (t) = char_type_node ? TYPE_ALIGN (char_type_node) : 0;
299 TYPE_USER_ALIGN (t) = 0;
300 TYPE_MAIN_VARIANT (t) = t;
302 /* Default to no attributes for type, but let target change that. */
303 TYPE_ATTRIBUTES (t) = NULL_TREE;
304 (*targetm.set_default_type_attributes) (t);
306 /* We have not yet computed the alias set for this type. */
307 TYPE_ALIAS_SET (t) = -1;
308 break;
310 case 'c':
311 TREE_CONSTANT (t) = 1;
312 break;
314 case 'e':
315 switch (code)
317 case INIT_EXPR:
318 case MODIFY_EXPR:
319 case VA_ARG_EXPR:
320 case RTL_EXPR:
321 case PREDECREMENT_EXPR:
322 case PREINCREMENT_EXPR:
323 case POSTDECREMENT_EXPR:
324 case POSTINCREMENT_EXPR:
325 /* All of these have side-effects, no matter what their
326 operands are. */
327 TREE_SIDE_EFFECTS (t) = 1;
328 break;
330 default:
331 break;
333 break;
336 return t;
339 /* Return a new node with the same contents as NODE except that its
340 TREE_CHAIN is zero and it has a fresh uid. */
342 tree
343 copy_node (tree node)
345 tree t;
346 enum tree_code code = TREE_CODE (node);
347 size_t length;
349 length = tree_size (node);
350 t = ggc_alloc_tree (length);
351 memcpy (t, node, length);
353 TREE_CHAIN (t) = 0;
354 TREE_ASM_WRITTEN (t) = 0;
356 if (TREE_CODE_CLASS (code) == 'd')
357 DECL_UID (t) = next_decl_uid++;
358 else if (TREE_CODE_CLASS (code) == 't')
360 TYPE_UID (t) = next_type_uid++;
361 /* The following is so that the debug code for
362 the copy is different from the original type.
363 The two statements usually duplicate each other
364 (because they clear fields of the same union),
365 but the optimizer should catch that. */
366 TYPE_SYMTAB_POINTER (t) = 0;
367 TYPE_SYMTAB_ADDRESS (t) = 0;
370 return t;
373 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
374 For example, this can copy a list made of TREE_LIST nodes. */
376 tree
377 copy_list (tree list)
379 tree head;
380 tree prev, next;
382 if (list == 0)
383 return 0;
385 head = prev = copy_node (list);
386 next = TREE_CHAIN (list);
387 while (next)
389 TREE_CHAIN (prev) = copy_node (next);
390 prev = TREE_CHAIN (prev);
391 next = TREE_CHAIN (next);
393 return head;
397 /* Return a newly constructed INTEGER_CST node whose constant value
398 is specified by the two ints LOW and HI.
399 The TREE_TYPE is set to `int'.
401 This function should be used via the `build_int_2' macro. */
403 tree
404 build_int_2_wide (unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
406 tree t = make_node (INTEGER_CST);
408 TREE_INT_CST_LOW (t) = low;
409 TREE_INT_CST_HIGH (t) = hi;
410 TREE_TYPE (t) = integer_type_node;
411 return t;
414 /* Return a new VECTOR_CST node whose type is TYPE and whose values
415 are in a list pointed by VALS. */
417 tree
418 build_vector (tree type, tree vals)
420 tree v = make_node (VECTOR_CST);
421 int over1 = 0, over2 = 0;
422 tree link;
424 TREE_VECTOR_CST_ELTS (v) = vals;
425 TREE_TYPE (v) = type;
427 /* Iterate through elements and check for overflow. */
428 for (link = vals; link; link = TREE_CHAIN (link))
430 tree value = TREE_VALUE (link);
432 over1 |= TREE_OVERFLOW (value);
433 over2 |= TREE_CONSTANT_OVERFLOW (value);
436 TREE_OVERFLOW (v) = over1;
437 TREE_CONSTANT_OVERFLOW (v) = over2;
439 return v;
442 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
443 are in a list pointed to by VALS. */
444 tree
445 build_constructor (tree type, tree vals)
447 tree c = make_node (CONSTRUCTOR);
448 TREE_TYPE (c) = type;
449 CONSTRUCTOR_ELTS (c) = vals;
451 /* ??? May not be necessary. Mirrors what build does. */
452 if (vals)
454 TREE_SIDE_EFFECTS (c) = TREE_SIDE_EFFECTS (vals);
455 TREE_READONLY (c) = TREE_READONLY (vals);
456 TREE_CONSTANT (c) = TREE_CONSTANT (vals);
458 else
459 TREE_CONSTANT (c) = 0; /* safe side */
461 return c;
464 /* Return a new REAL_CST node whose type is TYPE and value is D. */
466 tree
467 build_real (tree type, REAL_VALUE_TYPE d)
469 tree v;
470 REAL_VALUE_TYPE *dp;
471 int overflow = 0;
473 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
474 Consider doing it via real_convert now. */
476 v = make_node (REAL_CST);
477 dp = ggc_alloc (sizeof (REAL_VALUE_TYPE));
478 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
480 TREE_TYPE (v) = type;
481 TREE_REAL_CST_PTR (v) = dp;
482 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
483 return v;
486 /* Return a new REAL_CST node whose type is TYPE
487 and whose value is the integer value of the INTEGER_CST node I. */
489 REAL_VALUE_TYPE
490 real_value_from_int_cst (tree type, tree i)
492 REAL_VALUE_TYPE d;
494 /* Clear all bits of the real value type so that we can later do
495 bitwise comparisons to see if two values are the same. */
496 memset (&d, 0, sizeof d);
498 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
499 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
500 TREE_UNSIGNED (TREE_TYPE (i)));
501 return d;
504 /* Given a tree representing an integer constant I, return a tree
505 representing the same value as a floating-point constant of type TYPE. */
507 tree
508 build_real_from_int_cst (tree type, tree i)
510 tree v;
511 int overflow = TREE_OVERFLOW (i);
513 v = build_real (type, real_value_from_int_cst (type, i));
515 TREE_OVERFLOW (v) |= overflow;
516 TREE_CONSTANT_OVERFLOW (v) |= overflow;
517 return v;
520 /* Return a newly constructed STRING_CST node whose value is
521 the LEN characters at STR.
522 The TREE_TYPE is not initialized. */
524 tree
525 build_string (int len, const char *str)
527 tree s = make_node (STRING_CST);
529 TREE_STRING_LENGTH (s) = len;
530 TREE_STRING_POINTER (s) = ggc_alloc_string (str, len);
532 return s;
535 /* Return a newly constructed COMPLEX_CST node whose value is
536 specified by the real and imaginary parts REAL and IMAG.
537 Both REAL and IMAG should be constant nodes. TYPE, if specified,
538 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
540 tree
541 build_complex (tree type, tree real, tree imag)
543 tree t = make_node (COMPLEX_CST);
545 TREE_REALPART (t) = real;
546 TREE_IMAGPART (t) = imag;
547 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
548 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
549 TREE_CONSTANT_OVERFLOW (t)
550 = TREE_CONSTANT_OVERFLOW (real) | TREE_CONSTANT_OVERFLOW (imag);
551 return t;
554 /* Build a newly constructed TREE_VEC node of length LEN. */
556 tree
557 make_tree_vec (int len)
559 tree t;
560 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
562 #ifdef GATHER_STATISTICS
563 tree_node_counts[(int) vec_kind]++;
564 tree_node_sizes[(int) vec_kind] += length;
565 #endif
567 t = ggc_alloc_tree (length);
569 memset (t, 0, length);
570 TREE_SET_CODE (t, TREE_VEC);
571 TREE_VEC_LENGTH (t) = len;
573 return t;
576 /* Return 1 if EXPR is the integer constant zero or a complex constant
577 of zero. */
580 integer_zerop (tree expr)
582 STRIP_NOPS (expr);
584 return ((TREE_CODE (expr) == INTEGER_CST
585 && ! TREE_CONSTANT_OVERFLOW (expr)
586 && TREE_INT_CST_LOW (expr) == 0
587 && TREE_INT_CST_HIGH (expr) == 0)
588 || (TREE_CODE (expr) == COMPLEX_CST
589 && integer_zerop (TREE_REALPART (expr))
590 && integer_zerop (TREE_IMAGPART (expr))));
593 /* Return 1 if EXPR is the integer constant one or the corresponding
594 complex constant. */
597 integer_onep (tree expr)
599 STRIP_NOPS (expr);
601 return ((TREE_CODE (expr) == INTEGER_CST
602 && ! TREE_CONSTANT_OVERFLOW (expr)
603 && TREE_INT_CST_LOW (expr) == 1
604 && TREE_INT_CST_HIGH (expr) == 0)
605 || (TREE_CODE (expr) == COMPLEX_CST
606 && integer_onep (TREE_REALPART (expr))
607 && integer_zerop (TREE_IMAGPART (expr))));
610 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
611 it contains. Likewise for the corresponding complex constant. */
614 integer_all_onesp (tree expr)
616 int prec;
617 int uns;
619 STRIP_NOPS (expr);
621 if (TREE_CODE (expr) == COMPLEX_CST
622 && integer_all_onesp (TREE_REALPART (expr))
623 && integer_zerop (TREE_IMAGPART (expr)))
624 return 1;
626 else if (TREE_CODE (expr) != INTEGER_CST
627 || TREE_CONSTANT_OVERFLOW (expr))
628 return 0;
630 uns = TREE_UNSIGNED (TREE_TYPE (expr));
631 if (!uns)
632 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
633 && TREE_INT_CST_HIGH (expr) == -1);
635 /* Note that using TYPE_PRECISION here is wrong. We care about the
636 actual bits, not the (arbitrary) range of the type. */
637 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)));
638 if (prec >= HOST_BITS_PER_WIDE_INT)
640 HOST_WIDE_INT high_value;
641 int shift_amount;
643 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
645 if (shift_amount > HOST_BITS_PER_WIDE_INT)
646 /* Can not handle precisions greater than twice the host int size. */
647 abort ();
648 else if (shift_amount == HOST_BITS_PER_WIDE_INT)
649 /* Shifting by the host word size is undefined according to the ANSI
650 standard, so we must handle this as a special case. */
651 high_value = -1;
652 else
653 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
655 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
656 && TREE_INT_CST_HIGH (expr) == high_value);
658 else
659 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
662 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
663 one bit on). */
666 integer_pow2p (tree expr)
668 int prec;
669 HOST_WIDE_INT high, low;
671 STRIP_NOPS (expr);
673 if (TREE_CODE (expr) == COMPLEX_CST
674 && integer_pow2p (TREE_REALPART (expr))
675 && integer_zerop (TREE_IMAGPART (expr)))
676 return 1;
678 if (TREE_CODE (expr) != INTEGER_CST || TREE_CONSTANT_OVERFLOW (expr))
679 return 0;
681 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
682 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
683 high = TREE_INT_CST_HIGH (expr);
684 low = TREE_INT_CST_LOW (expr);
686 /* First clear all bits that are beyond the type's precision in case
687 we've been sign extended. */
689 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
691 else if (prec > HOST_BITS_PER_WIDE_INT)
692 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
693 else
695 high = 0;
696 if (prec < HOST_BITS_PER_WIDE_INT)
697 low &= ~((HOST_WIDE_INT) (-1) << prec);
700 if (high == 0 && low == 0)
701 return 0;
703 return ((high == 0 && (low & (low - 1)) == 0)
704 || (low == 0 && (high & (high - 1)) == 0));
707 /* Return 1 if EXPR is an integer constant other than zero or a
708 complex constant other than zero. */
711 integer_nonzerop (tree expr)
713 STRIP_NOPS (expr);
715 return ((TREE_CODE (expr) == INTEGER_CST
716 && ! TREE_CONSTANT_OVERFLOW (expr)
717 && (TREE_INT_CST_LOW (expr) != 0
718 || TREE_INT_CST_HIGH (expr) != 0))
719 || (TREE_CODE (expr) == COMPLEX_CST
720 && (integer_nonzerop (TREE_REALPART (expr))
721 || integer_nonzerop (TREE_IMAGPART (expr)))));
724 /* Return the power of two represented by a tree node known to be a
725 power of two. */
728 tree_log2 (tree expr)
730 int prec;
731 HOST_WIDE_INT high, low;
733 STRIP_NOPS (expr);
735 if (TREE_CODE (expr) == COMPLEX_CST)
736 return tree_log2 (TREE_REALPART (expr));
738 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
739 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
741 high = TREE_INT_CST_HIGH (expr);
742 low = TREE_INT_CST_LOW (expr);
744 /* First clear all bits that are beyond the type's precision in case
745 we've been sign extended. */
747 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
749 else if (prec > HOST_BITS_PER_WIDE_INT)
750 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
751 else
753 high = 0;
754 if (prec < HOST_BITS_PER_WIDE_INT)
755 low &= ~((HOST_WIDE_INT) (-1) << prec);
758 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
759 : exact_log2 (low));
762 /* Similar, but return the largest integer Y such that 2 ** Y is less
763 than or equal to EXPR. */
766 tree_floor_log2 (tree expr)
768 int prec;
769 HOST_WIDE_INT high, low;
771 STRIP_NOPS (expr);
773 if (TREE_CODE (expr) == COMPLEX_CST)
774 return tree_log2 (TREE_REALPART (expr));
776 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
777 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
779 high = TREE_INT_CST_HIGH (expr);
780 low = TREE_INT_CST_LOW (expr);
782 /* First clear all bits that are beyond the type's precision in case
783 we've been sign extended. Ignore if type's precision hasn't been set
784 since what we are doing is setting it. */
786 if (prec == 2 * HOST_BITS_PER_WIDE_INT || prec == 0)
788 else if (prec > HOST_BITS_PER_WIDE_INT)
789 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
790 else
792 high = 0;
793 if (prec < HOST_BITS_PER_WIDE_INT)
794 low &= ~((HOST_WIDE_INT) (-1) << prec);
797 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
798 : floor_log2 (low));
801 /* Return 1 if EXPR is the real constant zero. */
804 real_zerop (tree expr)
806 STRIP_NOPS (expr);
808 return ((TREE_CODE (expr) == REAL_CST
809 && ! TREE_CONSTANT_OVERFLOW (expr)
810 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0))
811 || (TREE_CODE (expr) == COMPLEX_CST
812 && real_zerop (TREE_REALPART (expr))
813 && real_zerop (TREE_IMAGPART (expr))));
816 /* Return 1 if EXPR is the real constant one in real or complex form. */
819 real_onep (tree expr)
821 STRIP_NOPS (expr);
823 return ((TREE_CODE (expr) == REAL_CST
824 && ! TREE_CONSTANT_OVERFLOW (expr)
825 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1))
826 || (TREE_CODE (expr) == COMPLEX_CST
827 && real_onep (TREE_REALPART (expr))
828 && real_zerop (TREE_IMAGPART (expr))));
831 /* Return 1 if EXPR is the real constant two. */
834 real_twop (tree expr)
836 STRIP_NOPS (expr);
838 return ((TREE_CODE (expr) == REAL_CST
839 && ! TREE_CONSTANT_OVERFLOW (expr)
840 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2))
841 || (TREE_CODE (expr) == COMPLEX_CST
842 && real_twop (TREE_REALPART (expr))
843 && real_zerop (TREE_IMAGPART (expr))));
846 /* Return 1 if EXPR is the real constant minus one. */
849 real_minus_onep (tree expr)
851 STRIP_NOPS (expr);
853 return ((TREE_CODE (expr) == REAL_CST
854 && ! TREE_CONSTANT_OVERFLOW (expr)
855 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1))
856 || (TREE_CODE (expr) == COMPLEX_CST
857 && real_minus_onep (TREE_REALPART (expr))
858 && real_zerop (TREE_IMAGPART (expr))));
861 /* Nonzero if EXP is a constant or a cast of a constant. */
864 really_constant_p (tree exp)
866 /* This is not quite the same as STRIP_NOPS. It does more. */
867 while (TREE_CODE (exp) == NOP_EXPR
868 || TREE_CODE (exp) == CONVERT_EXPR
869 || TREE_CODE (exp) == NON_LVALUE_EXPR)
870 exp = TREE_OPERAND (exp, 0);
871 return TREE_CONSTANT (exp);
874 /* Return first list element whose TREE_VALUE is ELEM.
875 Return 0 if ELEM is not in LIST. */
877 tree
878 value_member (tree elem, tree list)
880 while (list)
882 if (elem == TREE_VALUE (list))
883 return list;
884 list = TREE_CHAIN (list);
886 return NULL_TREE;
889 /* Return first list element whose TREE_PURPOSE is ELEM.
890 Return 0 if ELEM is not in LIST. */
892 tree
893 purpose_member (tree elem, tree list)
895 while (list)
897 if (elem == TREE_PURPOSE (list))
898 return list;
899 list = TREE_CHAIN (list);
901 return NULL_TREE;
904 /* Return first list element whose BINFO_TYPE is ELEM.
905 Return 0 if ELEM is not in LIST. */
907 tree
908 binfo_member (tree elem, tree list)
910 while (list)
912 if (elem == BINFO_TYPE (list))
913 return list;
914 list = TREE_CHAIN (list);
916 return NULL_TREE;
919 /* Return nonzero if ELEM is part of the chain CHAIN. */
922 chain_member (tree elem, tree chain)
924 while (chain)
926 if (elem == chain)
927 return 1;
928 chain = TREE_CHAIN (chain);
931 return 0;
934 /* Return the length of a chain of nodes chained through TREE_CHAIN.
935 We expect a null pointer to mark the end of the chain.
936 This is the Lisp primitive `length'. */
939 list_length (tree t)
941 tree tail;
942 int len = 0;
944 for (tail = t; tail; tail = TREE_CHAIN (tail))
945 len++;
947 return len;
950 /* Returns the number of FIELD_DECLs in TYPE. */
953 fields_length (tree type)
955 tree t = TYPE_FIELDS (type);
956 int count = 0;
958 for (; t; t = TREE_CHAIN (t))
959 if (TREE_CODE (t) == FIELD_DECL)
960 ++count;
962 return count;
965 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
966 by modifying the last node in chain 1 to point to chain 2.
967 This is the Lisp primitive `nconc'. */
969 tree
970 chainon (tree op1, tree op2)
972 tree t1;
974 if (!op1)
975 return op2;
976 if (!op2)
977 return op1;
979 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
980 continue;
981 TREE_CHAIN (t1) = op2;
983 #ifdef ENABLE_TREE_CHECKING
985 tree t2;
986 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
987 if (t2 == t1)
988 abort (); /* Circularity created. */
990 #endif
992 return op1;
995 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
997 tree
998 tree_last (tree chain)
1000 tree next;
1001 if (chain)
1002 while ((next = TREE_CHAIN (chain)))
1003 chain = next;
1004 return chain;
1007 /* Reverse the order of elements in the chain T,
1008 and return the new head of the chain (old last element). */
1010 tree
1011 nreverse (tree t)
1013 tree prev = 0, decl, next;
1014 for (decl = t; decl; decl = next)
1016 next = TREE_CHAIN (decl);
1017 TREE_CHAIN (decl) = prev;
1018 prev = decl;
1020 return prev;
1023 /* Return a newly created TREE_LIST node whose
1024 purpose and value fields are PARM and VALUE. */
1026 tree
1027 build_tree_list (tree parm, tree value)
1029 tree t = make_node (TREE_LIST);
1030 TREE_PURPOSE (t) = parm;
1031 TREE_VALUE (t) = value;
1032 return t;
1035 /* Return a newly created TREE_LIST node whose
1036 purpose and value fields are PURPOSE and VALUE
1037 and whose TREE_CHAIN is CHAIN. */
1039 tree
1040 tree_cons (tree purpose, tree value, tree chain)
1042 tree node;
1044 node = ggc_alloc_tree (sizeof (struct tree_list));
1046 memset (node, 0, sizeof (struct tree_common));
1048 #ifdef GATHER_STATISTICS
1049 tree_node_counts[(int) x_kind]++;
1050 tree_node_sizes[(int) x_kind] += sizeof (struct tree_list);
1051 #endif
1053 TREE_SET_CODE (node, TREE_LIST);
1054 TREE_CHAIN (node) = chain;
1055 TREE_PURPOSE (node) = purpose;
1056 TREE_VALUE (node) = value;
1057 return node;
1060 /* Return the first expression in a sequence of COMPOUND_EXPRs. */
1062 tree
1063 expr_first (tree expr)
1065 if (expr == NULL_TREE)
1066 return expr;
1067 while (TREE_CODE (expr) == COMPOUND_EXPR)
1068 expr = TREE_OPERAND (expr, 0);
1069 return expr;
1072 /* Return the last expression in a sequence of COMPOUND_EXPRs. */
1074 tree
1075 expr_last (tree expr)
1077 if (expr == NULL_TREE)
1078 return expr;
1079 while (TREE_CODE (expr) == COMPOUND_EXPR)
1080 expr = TREE_OPERAND (expr, 1);
1081 return expr;
1084 /* Return the number of subexpressions in a sequence of COMPOUND_EXPRs. */
1087 expr_length (tree expr)
1089 int len = 0;
1091 if (expr == NULL_TREE)
1092 return 0;
1093 for (; TREE_CODE (expr) == COMPOUND_EXPR; expr = TREE_OPERAND (expr, 1))
1094 len += expr_length (TREE_OPERAND (expr, 0));
1095 ++len;
1096 return len;
1099 /* Return the size nominally occupied by an object of type TYPE
1100 when it resides in memory. The value is measured in units of bytes,
1101 and its data type is that normally used for type sizes
1102 (which is the first type created by make_signed_type or
1103 make_unsigned_type). */
1105 tree
1106 size_in_bytes (tree type)
1108 tree t;
1110 if (type == error_mark_node)
1111 return integer_zero_node;
1113 type = TYPE_MAIN_VARIANT (type);
1114 t = TYPE_SIZE_UNIT (type);
1116 if (t == 0)
1118 (*lang_hooks.types.incomplete_type_error) (NULL_TREE, type);
1119 return size_zero_node;
1122 if (TREE_CODE (t) == INTEGER_CST)
1123 force_fit_type (t, 0);
1125 return t;
1128 /* Return the size of TYPE (in bytes) as a wide integer
1129 or return -1 if the size can vary or is larger than an integer. */
1131 HOST_WIDE_INT
1132 int_size_in_bytes (tree type)
1134 tree t;
1136 if (type == error_mark_node)
1137 return 0;
1139 type = TYPE_MAIN_VARIANT (type);
1140 t = TYPE_SIZE_UNIT (type);
1141 if (t == 0
1142 || TREE_CODE (t) != INTEGER_CST
1143 || TREE_OVERFLOW (t)
1144 || TREE_INT_CST_HIGH (t) != 0
1145 /* If the result would appear negative, it's too big to represent. */
1146 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
1147 return -1;
1149 return TREE_INT_CST_LOW (t);
1152 /* Return the bit position of FIELD, in bits from the start of the record.
1153 This is a tree of type bitsizetype. */
1155 tree
1156 bit_position (tree field)
1158 return bit_from_pos (DECL_FIELD_OFFSET (field),
1159 DECL_FIELD_BIT_OFFSET (field));
1162 /* Likewise, but return as an integer. Abort if it cannot be represented
1163 in that way (since it could be a signed value, we don't have the option
1164 of returning -1 like int_size_in_byte can. */
1166 HOST_WIDE_INT
1167 int_bit_position (tree field)
1169 return tree_low_cst (bit_position (field), 0);
1172 /* Return the byte position of FIELD, in bytes from the start of the record.
1173 This is a tree of type sizetype. */
1175 tree
1176 byte_position (tree field)
1178 return byte_from_pos (DECL_FIELD_OFFSET (field),
1179 DECL_FIELD_BIT_OFFSET (field));
1182 /* Likewise, but return as an integer. Abort if it cannot be represented
1183 in that way (since it could be a signed value, we don't have the option
1184 of returning -1 like int_size_in_byte can. */
1186 HOST_WIDE_INT
1187 int_byte_position (tree field)
1189 return tree_low_cst (byte_position (field), 0);
1192 /* Return the strictest alignment, in bits, that T is known to have. */
1194 unsigned int
1195 expr_align (tree t)
1197 unsigned int align0, align1;
1199 switch (TREE_CODE (t))
1201 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
1202 /* If we have conversions, we know that the alignment of the
1203 object must meet each of the alignments of the types. */
1204 align0 = expr_align (TREE_OPERAND (t, 0));
1205 align1 = TYPE_ALIGN (TREE_TYPE (t));
1206 return MAX (align0, align1);
1208 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
1209 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
1210 case WITH_RECORD_EXPR: case CLEANUP_POINT_EXPR: case UNSAVE_EXPR:
1211 /* These don't change the alignment of an object. */
1212 return expr_align (TREE_OPERAND (t, 0));
1214 case COND_EXPR:
1215 /* The best we can do is say that the alignment is the least aligned
1216 of the two arms. */
1217 align0 = expr_align (TREE_OPERAND (t, 1));
1218 align1 = expr_align (TREE_OPERAND (t, 2));
1219 return MIN (align0, align1);
1221 case LABEL_DECL: case CONST_DECL:
1222 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
1223 if (DECL_ALIGN (t) != 0)
1224 return DECL_ALIGN (t);
1225 break;
1227 case FUNCTION_DECL:
1228 return FUNCTION_BOUNDARY;
1230 default:
1231 break;
1234 /* Otherwise take the alignment from that of the type. */
1235 return TYPE_ALIGN (TREE_TYPE (t));
1238 /* Return, as a tree node, the number of elements for TYPE (which is an
1239 ARRAY_TYPE) minus one. This counts only elements of the top array. */
1241 tree
1242 array_type_nelts (tree type)
1244 tree index_type, min, max;
1246 /* If they did it with unspecified bounds, then we should have already
1247 given an error about it before we got here. */
1248 if (! TYPE_DOMAIN (type))
1249 return error_mark_node;
1251 index_type = TYPE_DOMAIN (type);
1252 min = TYPE_MIN_VALUE (index_type);
1253 max = TYPE_MAX_VALUE (index_type);
1255 return (integer_zerop (min)
1256 ? max
1257 : fold (build (MINUS_EXPR, TREE_TYPE (max), max, min)));
1260 /* Return nonzero if arg is static -- a reference to an object in
1261 static storage. This is not the same as the C meaning of `static'. */
1264 staticp (tree arg)
1266 switch (TREE_CODE (arg))
1268 case FUNCTION_DECL:
1269 /* Nested functions aren't static, since taking their address
1270 involves a trampoline. */
1271 return ((decl_function_context (arg) == 0 || DECL_NO_STATIC_CHAIN (arg))
1272 && ! DECL_NON_ADDR_CONST_P (arg));
1274 case VAR_DECL:
1275 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
1276 && ! DECL_THREAD_LOCAL (arg)
1277 && ! DECL_NON_ADDR_CONST_P (arg));
1279 case CONSTRUCTOR:
1280 return TREE_STATIC (arg);
1282 case LABEL_DECL:
1283 case STRING_CST:
1284 return 1;
1286 /* If we are referencing a bitfield, we can't evaluate an
1287 ADDR_EXPR at compile time and so it isn't a constant. */
1288 case COMPONENT_REF:
1289 return (! DECL_BIT_FIELD (TREE_OPERAND (arg, 1))
1290 && staticp (TREE_OPERAND (arg, 0)));
1292 case BIT_FIELD_REF:
1293 return 0;
1295 #if 0
1296 /* This case is technically correct, but results in setting
1297 TREE_CONSTANT on ADDR_EXPRs that cannot be evaluated at
1298 compile time. */
1299 case INDIRECT_REF:
1300 return TREE_CONSTANT (TREE_OPERAND (arg, 0));
1301 #endif
1303 case ARRAY_REF:
1304 case ARRAY_RANGE_REF:
1305 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
1306 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
1307 return staticp (TREE_OPERAND (arg, 0));
1309 default:
1310 if ((unsigned int) TREE_CODE (arg)
1311 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
1312 return (*lang_hooks.staticp) (arg);
1313 else
1314 return 0;
1318 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
1319 Do this to any expression which may be used in more than one place,
1320 but must be evaluated only once.
1322 Normally, expand_expr would reevaluate the expression each time.
1323 Calling save_expr produces something that is evaluated and recorded
1324 the first time expand_expr is called on it. Subsequent calls to
1325 expand_expr just reuse the recorded value.
1327 The call to expand_expr that generates code that actually computes
1328 the value is the first call *at compile time*. Subsequent calls
1329 *at compile time* generate code to use the saved value.
1330 This produces correct result provided that *at run time* control
1331 always flows through the insns made by the first expand_expr
1332 before reaching the other places where the save_expr was evaluated.
1333 You, the caller of save_expr, must make sure this is so.
1335 Constants, and certain read-only nodes, are returned with no
1336 SAVE_EXPR because that is safe. Expressions containing placeholders
1337 are not touched; see tree.def for an explanation of what these
1338 are used for. */
1340 tree
1341 save_expr (tree expr)
1343 tree t = fold (expr);
1344 tree inner;
1346 /* If the tree evaluates to a constant, then we don't want to hide that
1347 fact (i.e. this allows further folding, and direct checks for constants).
1348 However, a read-only object that has side effects cannot be bypassed.
1349 Since it is no problem to reevaluate literals, we just return the
1350 literal node. */
1351 inner = skip_simple_arithmetic (t);
1352 if (TREE_CONSTANT (inner)
1353 || (TREE_READONLY (inner) && ! TREE_SIDE_EFFECTS (inner))
1354 || TREE_CODE (inner) == SAVE_EXPR
1355 || TREE_CODE (inner) == ERROR_MARK)
1356 return t;
1358 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
1359 it means that the size or offset of some field of an object depends on
1360 the value within another field.
1362 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
1363 and some variable since it would then need to be both evaluated once and
1364 evaluated more than once. Front-ends must assure this case cannot
1365 happen by surrounding any such subexpressions in their own SAVE_EXPR
1366 and forcing evaluation at the proper time. */
1367 if (contains_placeholder_p (inner))
1368 return t;
1370 t = build (SAVE_EXPR, TREE_TYPE (expr), t, current_function_decl, NULL_TREE);
1372 /* This expression might be placed ahead of a jump to ensure that the
1373 value was computed on both sides of the jump. So make sure it isn't
1374 eliminated as dead. */
1375 TREE_SIDE_EFFECTS (t) = 1;
1376 TREE_READONLY (t) = 1;
1377 return t;
1380 /* Look inside EXPR and into any simple arithmetic operations. Return
1381 the innermost non-arithmetic node. */
1383 tree
1384 skip_simple_arithmetic (tree expr)
1386 tree inner;
1388 /* We don't care about whether this can be used as an lvalue in this
1389 context. */
1390 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
1391 expr = TREE_OPERAND (expr, 0);
1393 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
1394 a constant, it will be more efficient to not make another SAVE_EXPR since
1395 it will allow better simplification and GCSE will be able to merge the
1396 computations if they actually occur. */
1397 inner = expr;
1398 while (1)
1400 if (TREE_CODE_CLASS (TREE_CODE (inner)) == '1')
1401 inner = TREE_OPERAND (inner, 0);
1402 else if (TREE_CODE_CLASS (TREE_CODE (inner)) == '2')
1404 if (TREE_CONSTANT (TREE_OPERAND (inner, 1)))
1405 inner = TREE_OPERAND (inner, 0);
1406 else if (TREE_CONSTANT (TREE_OPERAND (inner, 0)))
1407 inner = TREE_OPERAND (inner, 1);
1408 else
1409 break;
1411 else
1412 break;
1415 return inner;
1418 /* Return TRUE if EXPR is a SAVE_EXPR or wraps simple arithmetic around a
1419 SAVE_EXPR. Return FALSE otherwise. */
1421 bool
1422 saved_expr_p (tree expr)
1424 return TREE_CODE (skip_simple_arithmetic (expr)) == SAVE_EXPR;
1427 /* Arrange for an expression to be expanded multiple independent
1428 times. This is useful for cleanup actions, as the backend can
1429 expand them multiple times in different places. */
1431 tree
1432 unsave_expr (tree expr)
1434 tree t;
1436 /* If this is already protected, no sense in protecting it again. */
1437 if (TREE_CODE (expr) == UNSAVE_EXPR)
1438 return expr;
1440 t = build1 (UNSAVE_EXPR, TREE_TYPE (expr), expr);
1441 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (expr);
1442 return t;
1445 /* Returns the index of the first non-tree operand for CODE, or the number
1446 of operands if all are trees. */
1449 first_rtl_op (enum tree_code code)
1451 switch (code)
1453 case SAVE_EXPR:
1454 return 2;
1455 case GOTO_SUBROUTINE_EXPR:
1456 case RTL_EXPR:
1457 return 0;
1458 case WITH_CLEANUP_EXPR:
1459 return 2;
1460 default:
1461 return TREE_CODE_LENGTH (code);
1465 /* Return which tree structure is used by T. */
1467 enum tree_node_structure_enum
1468 tree_node_structure (tree t)
1470 enum tree_code code = TREE_CODE (t);
1472 switch (TREE_CODE_CLASS (code))
1474 case 'd': return TS_DECL;
1475 case 't': return TS_TYPE;
1476 case 'b': return TS_BLOCK;
1477 case 'r': case '<': case '1': case '2': case 'e': case 's':
1478 return TS_EXP;
1479 default: /* 'c' and 'x' */
1480 break;
1482 switch (code)
1484 /* 'c' cases. */
1485 case INTEGER_CST: return TS_INT_CST;
1486 case REAL_CST: return TS_REAL_CST;
1487 case COMPLEX_CST: return TS_COMPLEX;
1488 case VECTOR_CST: return TS_VECTOR;
1489 case STRING_CST: return TS_STRING;
1490 /* 'x' cases. */
1491 case ERROR_MARK: return TS_COMMON;
1492 case IDENTIFIER_NODE: return TS_IDENTIFIER;
1493 case TREE_LIST: return TS_LIST;
1494 case TREE_VEC: return TS_VEC;
1495 case PLACEHOLDER_EXPR: return TS_COMMON;
1497 default:
1498 abort ();
1502 /* Perform any modifications to EXPR required when it is unsaved. Does
1503 not recurse into EXPR's subtrees. */
1505 void
1506 unsave_expr_1 (tree expr)
1508 switch (TREE_CODE (expr))
1510 case SAVE_EXPR:
1511 if (! SAVE_EXPR_PERSISTENT_P (expr))
1512 SAVE_EXPR_RTL (expr) = 0;
1513 break;
1515 case TARGET_EXPR:
1516 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
1517 It's OK for this to happen if it was part of a subtree that
1518 isn't immediately expanded, such as operand 2 of another
1519 TARGET_EXPR. */
1520 if (TREE_OPERAND (expr, 1))
1521 break;
1523 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
1524 TREE_OPERAND (expr, 3) = NULL_TREE;
1525 break;
1527 case RTL_EXPR:
1528 /* I don't yet know how to emit a sequence multiple times. */
1529 if (RTL_EXPR_SEQUENCE (expr) != 0)
1530 abort ();
1531 break;
1533 default:
1534 break;
1538 /* Default lang hook for "unsave_expr_now". */
1540 tree
1541 lhd_unsave_expr_now (tree expr)
1543 enum tree_code code;
1545 /* There's nothing to do for NULL_TREE. */
1546 if (expr == 0)
1547 return expr;
1549 unsave_expr_1 (expr);
1551 code = TREE_CODE (expr);
1552 switch (TREE_CODE_CLASS (code))
1554 case 'c': /* a constant */
1555 case 't': /* a type node */
1556 case 'd': /* A decl node */
1557 case 'b': /* A block node */
1558 break;
1560 case 'x': /* miscellaneous: e.g., identifier, TREE_LIST or ERROR_MARK. */
1561 if (code == TREE_LIST)
1563 lhd_unsave_expr_now (TREE_VALUE (expr));
1564 lhd_unsave_expr_now (TREE_CHAIN (expr));
1566 break;
1568 case 'e': /* an expression */
1569 case 'r': /* a reference */
1570 case 's': /* an expression with side effects */
1571 case '<': /* a comparison expression */
1572 case '2': /* a binary arithmetic expression */
1573 case '1': /* a unary arithmetic expression */
1575 int i;
1577 for (i = first_rtl_op (code) - 1; i >= 0; i--)
1578 lhd_unsave_expr_now (TREE_OPERAND (expr, i));
1580 break;
1582 default:
1583 abort ();
1586 return expr;
1589 /* Return 0 if it is safe to evaluate EXPR multiple times,
1590 return 1 if it is safe if EXPR is unsaved afterward, or
1591 return 2 if it is completely unsafe.
1593 This assumes that CALL_EXPRs and TARGET_EXPRs are never replicated in
1594 an expression tree, so that it safe to unsave them and the surrounding
1595 context will be correct.
1597 SAVE_EXPRs basically *only* appear replicated in an expression tree,
1598 occasionally across the whole of a function. It is therefore only
1599 safe to unsave a SAVE_EXPR if you know that all occurrences appear
1600 below the UNSAVE_EXPR.
1602 RTL_EXPRs consume their rtl during evaluation. It is therefore
1603 never possible to unsave them. */
1606 unsafe_for_reeval (tree expr)
1608 int unsafeness = 0;
1609 enum tree_code code;
1610 int i, tmp, tmp2;
1611 tree exp;
1612 int first_rtl;
1614 if (expr == NULL_TREE)
1615 return 1;
1617 code = TREE_CODE (expr);
1618 first_rtl = first_rtl_op (code);
1620 switch (code)
1622 case SAVE_EXPR:
1623 case RTL_EXPR:
1624 return 2;
1626 case TREE_LIST:
1627 for (exp = expr; exp != 0; exp = TREE_CHAIN (exp))
1629 tmp = unsafe_for_reeval (TREE_VALUE (exp));
1630 unsafeness = MAX (tmp, unsafeness);
1633 return unsafeness;
1635 case CALL_EXPR:
1636 tmp2 = unsafe_for_reeval (TREE_OPERAND (expr, 0));
1637 tmp = unsafe_for_reeval (TREE_OPERAND (expr, 1));
1638 return MAX (MAX (tmp, 1), tmp2);
1640 case TARGET_EXPR:
1641 unsafeness = 1;
1642 break;
1644 case EXIT_BLOCK_EXPR:
1645 /* EXIT_BLOCK_LABELED_BLOCK, a.k.a. TREE_OPERAND (expr, 0), holds
1646 a reference to an ancestor LABELED_BLOCK, so we need to avoid
1647 unbounded recursion in the 'e' traversal code below. */
1648 exp = EXIT_BLOCK_RETURN (expr);
1649 return exp ? unsafe_for_reeval (exp) : 0;
1651 default:
1652 tmp = (*lang_hooks.unsafe_for_reeval) (expr);
1653 if (tmp >= 0)
1654 return tmp;
1655 break;
1658 switch (TREE_CODE_CLASS (code))
1660 case 'c': /* a constant */
1661 case 't': /* a type node */
1662 case 'x': /* something random, like an identifier or an ERROR_MARK. */
1663 case 'd': /* A decl node */
1664 case 'b': /* A block node */
1665 return 0;
1667 case 'e': /* an expression */
1668 case 'r': /* a reference */
1669 case 's': /* an expression with side effects */
1670 case '<': /* a comparison expression */
1671 case '2': /* a binary arithmetic expression */
1672 case '1': /* a unary arithmetic expression */
1673 for (i = first_rtl - 1; i >= 0; i--)
1675 tmp = unsafe_for_reeval (TREE_OPERAND (expr, i));
1676 unsafeness = MAX (tmp, unsafeness);
1679 return unsafeness;
1681 default:
1682 return 2;
1686 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
1687 or offset that depends on a field within a record. */
1689 bool
1690 contains_placeholder_p (tree exp)
1692 enum tree_code code;
1693 int result;
1695 if (!exp)
1696 return 0;
1698 /* If we have a WITH_RECORD_EXPR, it "cancels" any PLACEHOLDER_EXPR
1699 in it since it is supplying a value for it. */
1700 code = TREE_CODE (exp);
1701 if (code == WITH_RECORD_EXPR)
1702 return 0;
1703 else if (code == PLACEHOLDER_EXPR)
1704 return 1;
1706 switch (TREE_CODE_CLASS (code))
1708 case 'r':
1709 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
1710 position computations since they will be converted into a
1711 WITH_RECORD_EXPR involving the reference, which will assume
1712 here will be valid. */
1713 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
1715 case 'x':
1716 if (code == TREE_LIST)
1717 return (CONTAINS_PLACEHOLDER_P (TREE_VALUE (exp))
1718 || CONTAINS_PLACEHOLDER_P (TREE_CHAIN (exp)));
1719 break;
1721 case '1':
1722 case '2': case '<':
1723 case 'e':
1724 switch (code)
1726 case COMPOUND_EXPR:
1727 /* Ignoring the first operand isn't quite right, but works best. */
1728 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
1730 case RTL_EXPR:
1731 case CONSTRUCTOR:
1732 return 0;
1734 case COND_EXPR:
1735 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
1736 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
1737 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
1739 case SAVE_EXPR:
1740 /* If we already know this doesn't have a placeholder, don't
1741 check again. */
1742 if (SAVE_EXPR_NOPLACEHOLDER (exp) || SAVE_EXPR_RTL (exp) != 0)
1743 return 0;
1745 SAVE_EXPR_NOPLACEHOLDER (exp) = 1;
1746 result = CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
1747 if (result)
1748 SAVE_EXPR_NOPLACEHOLDER (exp) = 0;
1750 return result;
1752 case CALL_EXPR:
1753 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
1755 default:
1756 break;
1759 switch (TREE_CODE_LENGTH (code))
1761 case 1:
1762 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
1763 case 2:
1764 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
1765 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
1766 default:
1767 return 0;
1770 default:
1771 return 0;
1773 return 0;
1776 /* Return 1 if any part of the computation of TYPE involves a PLACEHOLDER_EXPR.
1777 This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and field
1778 positions. */
1780 bool
1781 type_contains_placeholder_p (tree type)
1783 /* If the size contains a placeholder or the parent type (component type in
1784 the case of arrays) type involves a placeholder, this type does. */
1785 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
1786 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
1787 || (TREE_TYPE (type) != 0
1788 && type_contains_placeholder_p (TREE_TYPE (type))))
1789 return 1;
1791 /* Now do type-specific checks. Note that the last part of the check above
1792 greatly limits what we have to do below. */
1793 switch (TREE_CODE (type))
1795 case VOID_TYPE:
1796 case COMPLEX_TYPE:
1797 case VECTOR_TYPE:
1798 case ENUMERAL_TYPE:
1799 case BOOLEAN_TYPE:
1800 case CHAR_TYPE:
1801 case POINTER_TYPE:
1802 case OFFSET_TYPE:
1803 case REFERENCE_TYPE:
1804 case METHOD_TYPE:
1805 case FILE_TYPE:
1806 case FUNCTION_TYPE:
1807 return 0;
1809 case INTEGER_TYPE:
1810 case REAL_TYPE:
1811 /* Here we just check the bounds. */
1812 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
1813 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
1815 case ARRAY_TYPE:
1816 case SET_TYPE:
1817 /* We're already checked the component type (TREE_TYPE), so just check
1818 the index type. */
1819 return type_contains_placeholder_p (TYPE_DOMAIN (type));
1821 case RECORD_TYPE:
1822 case UNION_TYPE:
1823 case QUAL_UNION_TYPE:
1825 static tree seen_types = 0;
1826 tree field;
1827 bool ret = 0;
1829 /* We have to be careful here that we don't end up in infinite
1830 recursions due to a field of a type being a pointer to that type
1831 or to a mutually-recursive type. So we store a list of record
1832 types that we've seen and see if this type is in them. To save
1833 memory, we don't use a list for just one type. Here we check
1834 whether we've seen this type before and store it if not. */
1835 if (seen_types == 0)
1836 seen_types = type;
1837 else if (TREE_CODE (seen_types) != TREE_LIST)
1839 if (seen_types == type)
1840 return 0;
1842 seen_types = tree_cons (NULL_TREE, type,
1843 build_tree_list (NULL_TREE, seen_types));
1845 else
1847 if (value_member (type, seen_types) != 0)
1848 return 0;
1850 seen_types = tree_cons (NULL_TREE, type, seen_types);
1853 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1854 if (TREE_CODE (field) == FIELD_DECL
1855 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
1856 || (TREE_CODE (type) == QUAL_UNION_TYPE
1857 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
1858 || type_contains_placeholder_p (TREE_TYPE (field))))
1860 ret = true;
1861 break;
1864 /* Now remove us from seen_types and return the result. */
1865 if (seen_types == type)
1866 seen_types = 0;
1867 else
1868 seen_types = TREE_CHAIN (seen_types);
1870 return ret;
1873 default:
1874 abort ();
1878 /* Return 1 if EXP contains any expressions that produce cleanups for an
1879 outer scope to deal with. Used by fold. */
1882 has_cleanups (tree exp)
1884 int i, nops, cmp;
1886 if (! TREE_SIDE_EFFECTS (exp))
1887 return 0;
1889 switch (TREE_CODE (exp))
1891 case TARGET_EXPR:
1892 case GOTO_SUBROUTINE_EXPR:
1893 case WITH_CLEANUP_EXPR:
1894 return 1;
1896 case CLEANUP_POINT_EXPR:
1897 return 0;
1899 case CALL_EXPR:
1900 for (exp = TREE_OPERAND (exp, 1); exp; exp = TREE_CHAIN (exp))
1902 cmp = has_cleanups (TREE_VALUE (exp));
1903 if (cmp)
1904 return cmp;
1906 return 0;
1908 default:
1909 break;
1912 /* This general rule works for most tree codes. All exceptions should be
1913 handled above. If this is a language-specific tree code, we can't
1914 trust what might be in the operand, so say we don't know
1915 the situation. */
1916 if ((int) TREE_CODE (exp) >= (int) LAST_AND_UNUSED_TREE_CODE)
1917 return -1;
1919 nops = first_rtl_op (TREE_CODE (exp));
1920 for (i = 0; i < nops; i++)
1921 if (TREE_OPERAND (exp, i) != 0)
1923 int type = TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (exp, i)));
1924 if (type == 'e' || type == '<' || type == '1' || type == '2'
1925 || type == 'r' || type == 's')
1927 cmp = has_cleanups (TREE_OPERAND (exp, i));
1928 if (cmp)
1929 return cmp;
1933 return 0;
1936 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
1937 return a tree with all occurrences of references to F in a
1938 PLACEHOLDER_EXPR replaced by R. Note that we assume here that EXP
1939 contains only arithmetic expressions or a CALL_EXPR with a
1940 PLACEHOLDER_EXPR occurring only in its arglist. */
1942 tree
1943 substitute_in_expr (tree exp, tree f, tree r)
1945 enum tree_code code = TREE_CODE (exp);
1946 tree op0, op1, op2;
1947 tree new;
1948 tree inner;
1950 switch (TREE_CODE_CLASS (code))
1952 case 'c':
1953 case 'd':
1954 return exp;
1956 case 'x':
1957 if (code == PLACEHOLDER_EXPR)
1958 return exp;
1959 else if (code == TREE_LIST)
1961 op0 = (TREE_CHAIN (exp) == 0
1962 ? 0 : substitute_in_expr (TREE_CHAIN (exp), f, r));
1963 op1 = substitute_in_expr (TREE_VALUE (exp), f, r);
1964 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
1965 return exp;
1967 return tree_cons (TREE_PURPOSE (exp), op1, op0);
1970 abort ();
1972 case '1':
1973 case '2':
1974 case '<':
1975 case 'e':
1976 switch (TREE_CODE_LENGTH (code))
1978 case 1:
1979 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
1980 if (op0 == TREE_OPERAND (exp, 0))
1981 return exp;
1983 if (code == NON_LVALUE_EXPR)
1984 return op0;
1986 new = fold (build1 (code, TREE_TYPE (exp), op0));
1987 break;
1989 case 2:
1990 /* An RTL_EXPR cannot contain a PLACEHOLDER_EXPR; a CONSTRUCTOR
1991 could, but we don't support it. */
1992 if (code == RTL_EXPR)
1993 return exp;
1994 else if (code == CONSTRUCTOR)
1995 abort ();
1997 op0 = TREE_OPERAND (exp, 0);
1998 op1 = TREE_OPERAND (exp, 1);
1999 if (CONTAINS_PLACEHOLDER_P (op0))
2000 op0 = substitute_in_expr (op0, f, r);
2001 if (CONTAINS_PLACEHOLDER_P (op1))
2002 op1 = substitute_in_expr (op1, f, r);
2004 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2005 return exp;
2007 new = fold (build (code, TREE_TYPE (exp), op0, op1));
2008 break;
2010 case 3:
2011 /* It cannot be that anything inside a SAVE_EXPR contains a
2012 PLACEHOLDER_EXPR. */
2013 if (code == SAVE_EXPR)
2014 return exp;
2016 else if (code == CALL_EXPR)
2018 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
2019 if (op1 == TREE_OPERAND (exp, 1))
2020 return exp;
2022 return build (code, TREE_TYPE (exp),
2023 TREE_OPERAND (exp, 0), op1, NULL_TREE);
2026 else if (code != COND_EXPR)
2027 abort ();
2029 op0 = TREE_OPERAND (exp, 0);
2030 op1 = TREE_OPERAND (exp, 1);
2031 op2 = TREE_OPERAND (exp, 2);
2033 if (CONTAINS_PLACEHOLDER_P (op0))
2034 op0 = substitute_in_expr (op0, f, r);
2035 if (CONTAINS_PLACEHOLDER_P (op1))
2036 op1 = substitute_in_expr (op1, f, r);
2037 if (CONTAINS_PLACEHOLDER_P (op2))
2038 op2 = substitute_in_expr (op2, f, r);
2040 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2041 && op2 == TREE_OPERAND (exp, 2))
2042 return exp;
2044 new = fold (build (code, TREE_TYPE (exp), op0, op1, op2));
2045 break;
2047 default:
2048 abort ();
2051 break;
2053 case 'r':
2054 switch (code)
2056 case COMPONENT_REF:
2057 /* If this expression is getting a value from a PLACEHOLDER_EXPR
2058 and it is the right field, replace it with R. */
2059 for (inner = TREE_OPERAND (exp, 0);
2060 TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
2061 inner = TREE_OPERAND (inner, 0))
2063 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2064 && TREE_OPERAND (exp, 1) == f)
2065 return r;
2067 /* If this expression hasn't been completed let, leave it
2068 alone. */
2069 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2070 && TREE_TYPE (inner) == 0)
2071 return exp;
2073 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2074 if (op0 == TREE_OPERAND (exp, 0))
2075 return exp;
2077 new = fold (build (code, TREE_TYPE (exp), op0,
2078 TREE_OPERAND (exp, 1)));
2079 break;
2081 case BIT_FIELD_REF:
2082 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2083 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
2084 op2 = substitute_in_expr (TREE_OPERAND (exp, 2), f, r);
2085 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2086 && op2 == TREE_OPERAND (exp, 2))
2087 return exp;
2089 new = fold (build (code, TREE_TYPE (exp), op0, op1, op2));
2090 break;
2092 case INDIRECT_REF:
2093 case BUFFER_REF:
2094 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2095 if (op0 == TREE_OPERAND (exp, 0))
2096 return exp;
2098 new = fold (build1 (code, TREE_TYPE (exp), op0));
2099 break;
2101 default:
2102 abort ();
2104 break;
2106 default:
2107 abort ();
2110 TREE_READONLY (new) = TREE_READONLY (exp);
2111 return new;
2114 /* Stabilize a reference so that we can use it any number of times
2115 without causing its operands to be evaluated more than once.
2116 Returns the stabilized reference. This works by means of save_expr,
2117 so see the caveats in the comments about save_expr.
2119 Also allows conversion expressions whose operands are references.
2120 Any other kind of expression is returned unchanged. */
2122 tree
2123 stabilize_reference (tree ref)
2125 tree result;
2126 enum tree_code code = TREE_CODE (ref);
2128 switch (code)
2130 case VAR_DECL:
2131 case PARM_DECL:
2132 case RESULT_DECL:
2133 /* No action is needed in this case. */
2134 return ref;
2136 case NOP_EXPR:
2137 case CONVERT_EXPR:
2138 case FLOAT_EXPR:
2139 case FIX_TRUNC_EXPR:
2140 case FIX_FLOOR_EXPR:
2141 case FIX_ROUND_EXPR:
2142 case FIX_CEIL_EXPR:
2143 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2144 break;
2146 case INDIRECT_REF:
2147 result = build_nt (INDIRECT_REF,
2148 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2149 break;
2151 case COMPONENT_REF:
2152 result = build_nt (COMPONENT_REF,
2153 stabilize_reference (TREE_OPERAND (ref, 0)),
2154 TREE_OPERAND (ref, 1));
2155 break;
2157 case BIT_FIELD_REF:
2158 result = build_nt (BIT_FIELD_REF,
2159 stabilize_reference (TREE_OPERAND (ref, 0)),
2160 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2161 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2162 break;
2164 case ARRAY_REF:
2165 result = build_nt (ARRAY_REF,
2166 stabilize_reference (TREE_OPERAND (ref, 0)),
2167 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2168 break;
2170 case ARRAY_RANGE_REF:
2171 result = build_nt (ARRAY_RANGE_REF,
2172 stabilize_reference (TREE_OPERAND (ref, 0)),
2173 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2174 break;
2176 case COMPOUND_EXPR:
2177 /* We cannot wrap the first expression in a SAVE_EXPR, as then
2178 it wouldn't be ignored. This matters when dealing with
2179 volatiles. */
2180 return stabilize_reference_1 (ref);
2182 case RTL_EXPR:
2183 result = build1 (INDIRECT_REF, TREE_TYPE (ref),
2184 save_expr (build1 (ADDR_EXPR,
2185 build_pointer_type (TREE_TYPE (ref)),
2186 ref)));
2187 break;
2189 /* If arg isn't a kind of lvalue we recognize, make no change.
2190 Caller should recognize the error for an invalid lvalue. */
2191 default:
2192 return ref;
2194 case ERROR_MARK:
2195 return error_mark_node;
2198 TREE_TYPE (result) = TREE_TYPE (ref);
2199 TREE_READONLY (result) = TREE_READONLY (ref);
2200 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
2201 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
2203 return result;
2206 /* Subroutine of stabilize_reference; this is called for subtrees of
2207 references. Any expression with side-effects must be put in a SAVE_EXPR
2208 to ensure that it is only evaluated once.
2210 We don't put SAVE_EXPR nodes around everything, because assigning very
2211 simple expressions to temporaries causes us to miss good opportunities
2212 for optimizations. Among other things, the opportunity to fold in the
2213 addition of a constant into an addressing mode often gets lost, e.g.
2214 "y[i+1] += x;". In general, we take the approach that we should not make
2215 an assignment unless we are forced into it - i.e., that any non-side effect
2216 operator should be allowed, and that cse should take care of coalescing
2217 multiple utterances of the same expression should that prove fruitful. */
2219 tree
2220 stabilize_reference_1 (tree e)
2222 tree result;
2223 enum tree_code code = TREE_CODE (e);
2225 /* We cannot ignore const expressions because it might be a reference
2226 to a const array but whose index contains side-effects. But we can
2227 ignore things that are actual constant or that already have been
2228 handled by this function. */
2230 if (TREE_CONSTANT (e) || code == SAVE_EXPR)
2231 return e;
2233 switch (TREE_CODE_CLASS (code))
2235 case 'x':
2236 case 't':
2237 case 'd':
2238 case 'b':
2239 case '<':
2240 case 's':
2241 case 'e':
2242 case 'r':
2243 /* If the expression has side-effects, then encase it in a SAVE_EXPR
2244 so that it will only be evaluated once. */
2245 /* The reference (r) and comparison (<) classes could be handled as
2246 below, but it is generally faster to only evaluate them once. */
2247 if (TREE_SIDE_EFFECTS (e))
2248 return save_expr (e);
2249 return e;
2251 case 'c':
2252 /* Constants need no processing. In fact, we should never reach
2253 here. */
2254 return e;
2256 case '2':
2257 /* Division is slow and tends to be compiled with jumps,
2258 especially the division by powers of 2 that is often
2259 found inside of an array reference. So do it just once. */
2260 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
2261 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
2262 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
2263 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
2264 return save_expr (e);
2265 /* Recursively stabilize each operand. */
2266 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
2267 stabilize_reference_1 (TREE_OPERAND (e, 1)));
2268 break;
2270 case '1':
2271 /* Recursively stabilize each operand. */
2272 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
2273 break;
2275 default:
2276 abort ();
2279 TREE_TYPE (result) = TREE_TYPE (e);
2280 TREE_READONLY (result) = TREE_READONLY (e);
2281 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
2282 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
2284 return result;
2287 /* Low-level constructors for expressions. */
2289 /* Build an expression of code CODE, data type TYPE, and operands as
2290 specified. Expressions and reference nodes can be created this way.
2291 Constants, decls, types and misc nodes cannot be.
2293 We define 5 non-variadic functions, from 0 to 4 arguments. This is
2294 enough for all extant tree codes. These functions can be called
2295 directly (preferably!), but can also be obtained via GCC preprocessor
2296 magic within the build macro. */
2298 tree
2299 build0 (enum tree_code code, tree tt)
2301 tree t;
2303 #ifdef ENABLE_CHECKING
2304 if (TREE_CODE_LENGTH (code) != 0)
2305 abort ();
2306 #endif
2308 t = make_node (code);
2309 TREE_TYPE (t) = tt;
2311 return t;
2314 tree
2315 build1 (enum tree_code code, tree type, tree node)
2317 int length = sizeof (struct tree_exp);
2318 #ifdef GATHER_STATISTICS
2319 tree_node_kind kind;
2320 #endif
2321 tree t;
2323 #ifdef GATHER_STATISTICS
2324 switch (TREE_CODE_CLASS (code))
2326 case 's': /* an expression with side effects */
2327 kind = s_kind;
2328 break;
2329 case 'r': /* a reference */
2330 kind = r_kind;
2331 break;
2332 default:
2333 kind = e_kind;
2334 break;
2337 tree_node_counts[(int) kind]++;
2338 tree_node_sizes[(int) kind] += length;
2339 #endif
2341 #ifdef ENABLE_CHECKING
2342 if (TREE_CODE_LENGTH (code) != 1)
2343 abort ();
2344 #endif /* ENABLE_CHECKING */
2346 t = ggc_alloc_tree (length);
2348 memset (t, 0, sizeof (struct tree_common));
2350 TREE_SET_CODE (t, code);
2352 TREE_TYPE (t) = type;
2353 TREE_COMPLEXITY (t) = 0;
2354 TREE_OPERAND (t, 0) = node;
2355 if (node && first_rtl_op (code) != 0)
2357 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
2358 TREE_READONLY (t) = TREE_READONLY (node);
2361 if (TREE_CODE_CLASS (code) == 's')
2362 TREE_SIDE_EFFECTS (t) = 1;
2363 else switch (code)
2365 case INIT_EXPR:
2366 case MODIFY_EXPR:
2367 case VA_ARG_EXPR:
2368 case RTL_EXPR:
2369 case PREDECREMENT_EXPR:
2370 case PREINCREMENT_EXPR:
2371 case POSTDECREMENT_EXPR:
2372 case POSTINCREMENT_EXPR:
2373 /* All of these have side-effects, no matter what their
2374 operands are. */
2375 TREE_SIDE_EFFECTS (t) = 1;
2376 TREE_READONLY (t) = 0;
2377 break;
2379 case INDIRECT_REF:
2380 /* Whether a dereference is readonly has nothing to do with whether
2381 its operand is readonly. */
2382 TREE_READONLY (t) = 0;
2383 break;
2385 case ADDR_EXPR:
2386 if (node)
2388 /* The address of a volatile decl or reference does not have
2389 side-effects. But be careful not to ignore side-effects from
2390 other sources deeper in the expression--if node is a _REF and
2391 one of its operands has side-effects, so do we. */
2392 if (TREE_THIS_VOLATILE (node))
2394 TREE_SIDE_EFFECTS (t) = 0;
2395 if (!DECL_P (node))
2397 int i = first_rtl_op (TREE_CODE (node)) - 1;
2398 for (; i >= 0; --i)
2400 if (TREE_SIDE_EFFECTS (TREE_OPERAND (node, i)))
2401 TREE_SIDE_EFFECTS (t) = 1;
2406 break;
2408 default:
2409 if (TREE_CODE_CLASS (code) == '1' && node && TREE_CONSTANT (node))
2410 TREE_CONSTANT (t) = 1;
2411 break;
2414 return t;
2417 #define PROCESS_ARG(N) \
2418 do { \
2419 TREE_OPERAND (t, N) = arg##N; \
2420 if (arg##N && fro > N) \
2422 if (TREE_SIDE_EFFECTS (arg##N)) \
2423 side_effects = 1; \
2424 if (!TREE_READONLY (arg##N)) \
2425 read_only = 0; \
2426 if (!TREE_CONSTANT (arg##N)) \
2427 constant = 0; \
2429 } while (0)
2431 tree
2432 build2 (enum tree_code code, tree tt, tree arg0, tree arg1)
2434 bool constant, read_only, side_effects;
2435 tree t;
2436 int fro;
2438 #ifdef ENABLE_CHECKING
2439 if (TREE_CODE_LENGTH (code) != 2)
2440 abort ();
2441 #endif
2443 t = make_node (code);
2444 TREE_TYPE (t) = tt;
2446 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
2447 result based on those same flags for the arguments. But if the
2448 arguments aren't really even `tree' expressions, we shouldn't be trying
2449 to do this. */
2450 fro = first_rtl_op (code);
2452 /* Expressions without side effects may be constant if their
2453 arguments are as well. */
2454 constant = (TREE_CODE_CLASS (code) == '<'
2455 || TREE_CODE_CLASS (code) == '2');
2456 read_only = 1;
2457 side_effects = TREE_SIDE_EFFECTS (t);
2459 PROCESS_ARG(0);
2460 PROCESS_ARG(1);
2462 if (code == CALL_EXPR && !side_effects)
2464 tree node;
2465 int i;
2467 /* Calls have side-effects, except those to const or
2468 pure functions. */
2469 i = call_expr_flags (t);
2470 if (!(i & (ECF_CONST | ECF_PURE)))
2471 side_effects = 1;
2473 /* And even those have side-effects if their arguments do. */
2474 else for (node = TREE_OPERAND (t, 1); node; node = TREE_CHAIN (node))
2475 if (TREE_SIDE_EFFECTS (TREE_VALUE (node)))
2477 side_effects = 1;
2478 break;
2482 TREE_READONLY (t) = read_only;
2483 TREE_CONSTANT (t) = constant;
2484 TREE_SIDE_EFFECTS (t) = side_effects;
2486 return t;
2489 tree
2490 build3 (enum tree_code code, tree tt, tree arg0, tree arg1, tree arg2)
2492 bool constant, read_only, side_effects;
2493 tree t;
2494 int fro;
2496 /* ??? Quite a lot of existing code passes one too many arguments to
2497 CALL_EXPR. Not going to fix them, because CALL_EXPR is about to
2498 grow a new argument, so it would just mean changing them back. */
2499 if (code == CALL_EXPR)
2501 if (arg2 != NULL_TREE)
2502 abort ();
2503 return build2 (code, tt, arg0, arg1);
2506 #ifdef ENABLE_CHECKING
2507 if (TREE_CODE_LENGTH (code) != 3)
2508 abort ();
2509 #endif
2511 t = make_node (code);
2512 TREE_TYPE (t) = tt;
2514 fro = first_rtl_op (code);
2516 side_effects = TREE_SIDE_EFFECTS (t);
2518 PROCESS_ARG(0);
2519 PROCESS_ARG(1);
2520 PROCESS_ARG(2);
2522 TREE_SIDE_EFFECTS (t) = side_effects;
2524 return t;
2527 tree
2528 build4 (enum tree_code code, tree tt, tree arg0, tree arg1,
2529 tree arg2, tree arg3)
2531 bool constant, read_only, side_effects;
2532 tree t;
2533 int fro;
2535 #ifdef ENABLE_CHECKING
2536 if (TREE_CODE_LENGTH (code) != 4)
2537 abort ();
2538 #endif
2540 t = make_node (code);
2541 TREE_TYPE (t) = tt;
2543 fro = first_rtl_op (code);
2545 side_effects = TREE_SIDE_EFFECTS (t);
2547 PROCESS_ARG(0);
2548 PROCESS_ARG(1);
2549 PROCESS_ARG(2);
2550 PROCESS_ARG(3);
2552 TREE_SIDE_EFFECTS (t) = side_effects;
2554 return t;
2557 /* Backup definition for non-gcc build compilers. */
2559 tree
2560 (build) (enum tree_code code, tree tt, ...)
2562 tree t, arg0, arg1, arg2, arg3;
2563 int length = TREE_CODE_LENGTH (code);
2564 va_list p;
2566 va_start (p, tt);
2567 switch (length)
2569 case 0:
2570 t = build0 (code, tt);
2571 break;
2572 case 1:
2573 arg0 = va_arg (p, tree);
2574 t = build1 (code, tt, arg0);
2575 break;
2576 case 2:
2577 arg0 = va_arg (p, tree);
2578 arg1 = va_arg (p, tree);
2579 t = build2 (code, tt, arg0, arg1);
2580 break;
2581 case 3:
2582 arg0 = va_arg (p, tree);
2583 arg1 = va_arg (p, tree);
2584 arg2 = va_arg (p, tree);
2585 t = build3 (code, tt, arg0, arg1, arg2);
2586 break;
2587 case 4:
2588 arg0 = va_arg (p, tree);
2589 arg1 = va_arg (p, tree);
2590 arg2 = va_arg (p, tree);
2591 arg3 = va_arg (p, tree);
2592 t = build4 (code, tt, arg0, arg1, arg2, arg3);
2593 break;
2594 default:
2595 abort ();
2597 va_end (p);
2599 return t;
2602 /* Similar except don't specify the TREE_TYPE
2603 and leave the TREE_SIDE_EFFECTS as 0.
2604 It is permissible for arguments to be null,
2605 or even garbage if their values do not matter. */
2607 tree
2608 build_nt (enum tree_code code, ...)
2610 tree t;
2611 int length;
2612 int i;
2613 va_list p;
2615 va_start (p, code);
2617 t = make_node (code);
2618 length = TREE_CODE_LENGTH (code);
2620 for (i = 0; i < length; i++)
2621 TREE_OPERAND (t, i) = va_arg (p, tree);
2623 va_end (p);
2624 return t;
2627 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
2628 We do NOT enter this node in any sort of symbol table.
2630 layout_decl is used to set up the decl's storage layout.
2631 Other slots are initialized to 0 or null pointers. */
2633 tree
2634 build_decl (enum tree_code code, tree name, tree type)
2636 tree t;
2638 t = make_node (code);
2640 /* if (type == error_mark_node)
2641 type = integer_type_node; */
2642 /* That is not done, deliberately, so that having error_mark_node
2643 as the type can suppress useless errors in the use of this variable. */
2645 DECL_NAME (t) = name;
2646 TREE_TYPE (t) = type;
2648 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
2649 layout_decl (t, 0);
2650 else if (code == FUNCTION_DECL)
2651 DECL_MODE (t) = FUNCTION_MODE;
2653 return t;
2656 /* BLOCK nodes are used to represent the structure of binding contours
2657 and declarations, once those contours have been exited and their contents
2658 compiled. This information is used for outputting debugging info. */
2660 tree
2661 build_block (tree vars, tree tags ATTRIBUTE_UNUSED, tree subblocks,
2662 tree supercontext, tree chain)
2664 tree block = make_node (BLOCK);
2666 BLOCK_VARS (block) = vars;
2667 BLOCK_SUBBLOCKS (block) = subblocks;
2668 BLOCK_SUPERCONTEXT (block) = supercontext;
2669 BLOCK_CHAIN (block) = chain;
2670 return block;
2673 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
2674 location where an expression or an identifier were encountered. It
2675 is necessary for languages where the frontend parser will handle
2676 recursively more than one file (Java is one of them). */
2678 tree
2679 build_expr_wfl (tree node, const char *file, int line, int col)
2681 static const char *last_file = 0;
2682 static tree last_filenode = NULL_TREE;
2683 tree wfl = make_node (EXPR_WITH_FILE_LOCATION);
2685 EXPR_WFL_NODE (wfl) = node;
2686 EXPR_WFL_SET_LINECOL (wfl, line, col);
2687 if (file != last_file)
2689 last_file = file;
2690 last_filenode = file ? get_identifier (file) : NULL_TREE;
2693 EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
2694 if (node)
2696 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
2697 TREE_TYPE (wfl) = TREE_TYPE (node);
2700 return wfl;
2703 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
2704 is ATTRIBUTE. */
2706 tree
2707 build_decl_attribute_variant (tree ddecl, tree attribute)
2709 DECL_ATTRIBUTES (ddecl) = attribute;
2710 return ddecl;
2713 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
2714 is ATTRIBUTE.
2716 Record such modified types already made so we don't make duplicates. */
2718 tree
2719 build_type_attribute_variant (tree ttype, tree attribute)
2721 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
2723 unsigned int hashcode;
2724 tree ntype;
2726 ntype = copy_node (ttype);
2728 TYPE_POINTER_TO (ntype) = 0;
2729 TYPE_REFERENCE_TO (ntype) = 0;
2730 TYPE_ATTRIBUTES (ntype) = attribute;
2732 /* Create a new main variant of TYPE. */
2733 TYPE_MAIN_VARIANT (ntype) = ntype;
2734 TYPE_NEXT_VARIANT (ntype) = 0;
2735 set_type_quals (ntype, TYPE_UNQUALIFIED);
2737 hashcode = (TYPE_HASH (TREE_CODE (ntype))
2738 + TYPE_HASH (TREE_TYPE (ntype))
2739 + attribute_hash_list (attribute));
2741 switch (TREE_CODE (ntype))
2743 case FUNCTION_TYPE:
2744 hashcode += TYPE_HASH (TYPE_ARG_TYPES (ntype));
2745 break;
2746 case ARRAY_TYPE:
2747 hashcode += TYPE_HASH (TYPE_DOMAIN (ntype));
2748 break;
2749 case INTEGER_TYPE:
2750 hashcode += TYPE_HASH (TYPE_MAX_VALUE (ntype));
2751 break;
2752 case REAL_TYPE:
2753 hashcode += TYPE_HASH (TYPE_PRECISION (ntype));
2754 break;
2755 default:
2756 break;
2759 ntype = type_hash_canon (hashcode, ntype);
2760 ttype = build_qualified_type (ntype, TYPE_QUALS (ttype));
2763 return ttype;
2766 /* Return nonzero if IDENT is a valid name for attribute ATTR,
2767 or zero if not.
2769 We try both `text' and `__text__', ATTR may be either one. */
2770 /* ??? It might be a reasonable simplification to require ATTR to be only
2771 `text'. One might then also require attribute lists to be stored in
2772 their canonicalized form. */
2775 is_attribute_p (const char *attr, tree ident)
2777 int ident_len, attr_len;
2778 const char *p;
2780 if (TREE_CODE (ident) != IDENTIFIER_NODE)
2781 return 0;
2783 if (strcmp (attr, IDENTIFIER_POINTER (ident)) == 0)
2784 return 1;
2786 p = IDENTIFIER_POINTER (ident);
2787 ident_len = strlen (p);
2788 attr_len = strlen (attr);
2790 /* If ATTR is `__text__', IDENT must be `text'; and vice versa. */
2791 if (attr[0] == '_')
2793 if (attr[1] != '_'
2794 || attr[attr_len - 2] != '_'
2795 || attr[attr_len - 1] != '_')
2796 abort ();
2797 if (ident_len == attr_len - 4
2798 && strncmp (attr + 2, p, attr_len - 4) == 0)
2799 return 1;
2801 else
2803 if (ident_len == attr_len + 4
2804 && p[0] == '_' && p[1] == '_'
2805 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
2806 && strncmp (attr, p + 2, attr_len) == 0)
2807 return 1;
2810 return 0;
2813 /* Given an attribute name and a list of attributes, return a pointer to the
2814 attribute's list element if the attribute is part of the list, or NULL_TREE
2815 if not found. If the attribute appears more than once, this only
2816 returns the first occurrence; the TREE_CHAIN of the return value should
2817 be passed back in if further occurrences are wanted. */
2819 tree
2820 lookup_attribute (const char *attr_name, tree list)
2822 tree l;
2824 for (l = list; l; l = TREE_CHAIN (l))
2826 if (TREE_CODE (TREE_PURPOSE (l)) != IDENTIFIER_NODE)
2827 abort ();
2828 if (is_attribute_p (attr_name, TREE_PURPOSE (l)))
2829 return l;
2832 return NULL_TREE;
2835 /* Return an attribute list that is the union of a1 and a2. */
2837 tree
2838 merge_attributes (tree a1, tree a2)
2840 tree attributes;
2842 /* Either one unset? Take the set one. */
2844 if ((attributes = a1) == 0)
2845 attributes = a2;
2847 /* One that completely contains the other? Take it. */
2849 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
2851 if (attribute_list_contained (a2, a1))
2852 attributes = a2;
2853 else
2855 /* Pick the longest list, and hang on the other list. */
2857 if (list_length (a1) < list_length (a2))
2858 attributes = a2, a2 = a1;
2860 for (; a2 != 0; a2 = TREE_CHAIN (a2))
2862 tree a;
2863 for (a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
2864 attributes);
2865 a != NULL_TREE;
2866 a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
2867 TREE_CHAIN (a)))
2869 if (simple_cst_equal (TREE_VALUE (a), TREE_VALUE (a2)) == 1)
2870 break;
2872 if (a == NULL_TREE)
2874 a1 = copy_node (a2);
2875 TREE_CHAIN (a1) = attributes;
2876 attributes = a1;
2881 return attributes;
2884 /* Given types T1 and T2, merge their attributes and return
2885 the result. */
2887 tree
2888 merge_type_attributes (tree t1, tree t2)
2890 return merge_attributes (TYPE_ATTRIBUTES (t1),
2891 TYPE_ATTRIBUTES (t2));
2894 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
2895 the result. */
2897 tree
2898 merge_decl_attributes (tree olddecl, tree newdecl)
2900 return merge_attributes (DECL_ATTRIBUTES (olddecl),
2901 DECL_ATTRIBUTES (newdecl));
2904 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
2906 /* Specialization of merge_decl_attributes for various Windows targets.
2908 This handles the following situation:
2910 __declspec (dllimport) int foo;
2911 int foo;
2913 The second instance of `foo' nullifies the dllimport. */
2915 tree
2916 merge_dllimport_decl_attributes (tree old, tree new)
2918 tree a;
2919 int delete_dllimport_p;
2921 old = DECL_ATTRIBUTES (old);
2922 new = DECL_ATTRIBUTES (new);
2924 /* What we need to do here is remove from `old' dllimport if it doesn't
2925 appear in `new'. dllimport behaves like extern: if a declaration is
2926 marked dllimport and a definition appears later, then the object
2927 is not dllimport'd. */
2928 if (lookup_attribute ("dllimport", old) != NULL_TREE
2929 && lookup_attribute ("dllimport", new) == NULL_TREE)
2930 delete_dllimport_p = 1;
2931 else
2932 delete_dllimport_p = 0;
2934 a = merge_attributes (old, new);
2936 if (delete_dllimport_p)
2938 tree prev, t;
2940 /* Scan the list for dllimport and delete it. */
2941 for (prev = NULL_TREE, t = a; t; prev = t, t = TREE_CHAIN (t))
2943 if (is_attribute_p ("dllimport", TREE_PURPOSE (t)))
2945 if (prev == NULL_TREE)
2946 a = TREE_CHAIN (a);
2947 else
2948 TREE_CHAIN (prev) = TREE_CHAIN (t);
2949 break;
2954 return a;
2957 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
2959 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
2960 of the various TYPE_QUAL values. */
2962 static void
2963 set_type_quals (tree type, int type_quals)
2965 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
2966 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
2967 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
2970 /* Returns true iff cand is equivalent to base with type_quals. */
2972 bool
2973 check_qualified_type (tree cand, tree base, int type_quals)
2975 return (TYPE_QUALS (cand) == type_quals
2976 && TYPE_NAME (cand) == TYPE_NAME (base)
2977 /* Apparently this is needed for Objective-C. */
2978 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
2979 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
2980 TYPE_ATTRIBUTES (base)));
2983 /* Return a version of the TYPE, qualified as indicated by the
2984 TYPE_QUALS, if one exists. If no qualified version exists yet,
2985 return NULL_TREE. */
2987 tree
2988 get_qualified_type (tree type, int type_quals)
2990 tree t;
2992 if (TYPE_QUALS (type) == type_quals)
2993 return type;
2995 /* Search the chain of variants to see if there is already one there just
2996 like the one we need to have. If so, use that existing one. We must
2997 preserve the TYPE_NAME, since there is code that depends on this. */
2998 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2999 if (check_qualified_type (t, type, type_quals))
3000 return t;
3002 return NULL_TREE;
3005 /* Like get_qualified_type, but creates the type if it does not
3006 exist. This function never returns NULL_TREE. */
3008 tree
3009 build_qualified_type (tree type, int type_quals)
3011 tree t;
3013 /* See if we already have the appropriate qualified variant. */
3014 t = get_qualified_type (type, type_quals);
3016 /* If not, build it. */
3017 if (!t)
3019 t = build_type_copy (type);
3020 set_type_quals (t, type_quals);
3023 return t;
3026 /* Create a new variant of TYPE, equivalent but distinct.
3027 This is so the caller can modify it. */
3029 tree
3030 build_type_copy (tree type)
3032 tree t, m = TYPE_MAIN_VARIANT (type);
3034 t = copy_node (type);
3036 TYPE_POINTER_TO (t) = 0;
3037 TYPE_REFERENCE_TO (t) = 0;
3039 /* Add this type to the chain of variants of TYPE. */
3040 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
3041 TYPE_NEXT_VARIANT (m) = t;
3043 return t;
3046 /* Hashing of types so that we don't make duplicates.
3047 The entry point is `type_hash_canon'. */
3049 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
3050 with types in the TREE_VALUE slots), by adding the hash codes
3051 of the individual types. */
3053 unsigned int
3054 type_hash_list (tree list)
3056 unsigned int hashcode;
3057 tree tail;
3059 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
3060 hashcode += TYPE_HASH (TREE_VALUE (tail));
3062 return hashcode;
3065 /* These are the Hashtable callback functions. */
3067 /* Returns true if the types are equal. */
3069 static int
3070 type_hash_eq (const void *va, const void *vb)
3072 const struct type_hash *a = va, *b = vb;
3073 if (a->hash == b->hash
3074 && TREE_CODE (a->type) == TREE_CODE (b->type)
3075 && TREE_TYPE (a->type) == TREE_TYPE (b->type)
3076 && attribute_list_equal (TYPE_ATTRIBUTES (a->type),
3077 TYPE_ATTRIBUTES (b->type))
3078 && TYPE_ALIGN (a->type) == TYPE_ALIGN (b->type)
3079 && (TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
3080 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
3081 TYPE_MAX_VALUE (b->type)))
3082 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
3083 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
3084 TYPE_MIN_VALUE (b->type)))
3085 /* Note that TYPE_DOMAIN is TYPE_ARG_TYPES for FUNCTION_TYPE. */
3086 && (TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type)
3087 || (TYPE_DOMAIN (a->type)
3088 && TREE_CODE (TYPE_DOMAIN (a->type)) == TREE_LIST
3089 && TYPE_DOMAIN (b->type)
3090 && TREE_CODE (TYPE_DOMAIN (b->type)) == TREE_LIST
3091 && type_list_equal (TYPE_DOMAIN (a->type),
3092 TYPE_DOMAIN (b->type)))))
3093 return 1;
3094 return 0;
3097 /* Return the cached hash value. */
3099 static hashval_t
3100 type_hash_hash (const void *item)
3102 return ((const struct type_hash *) item)->hash;
3105 /* Look in the type hash table for a type isomorphic to TYPE.
3106 If one is found, return it. Otherwise return 0. */
3108 tree
3109 type_hash_lookup (unsigned int hashcode, tree type)
3111 struct type_hash *h, in;
3113 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
3114 must call that routine before comparing TYPE_ALIGNs. */
3115 layout_type (type);
3117 in.hash = hashcode;
3118 in.type = type;
3120 h = htab_find_with_hash (type_hash_table, &in, hashcode);
3121 if (h)
3122 return h->type;
3123 return NULL_TREE;
3126 /* Add an entry to the type-hash-table
3127 for a type TYPE whose hash code is HASHCODE. */
3129 void
3130 type_hash_add (unsigned int hashcode, tree type)
3132 struct type_hash *h;
3133 void **loc;
3135 h = ggc_alloc (sizeof (struct type_hash));
3136 h->hash = hashcode;
3137 h->type = type;
3138 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
3139 *(struct type_hash **) loc = h;
3142 /* Given TYPE, and HASHCODE its hash code, return the canonical
3143 object for an identical type if one already exists.
3144 Otherwise, return TYPE, and record it as the canonical object
3145 if it is a permanent object.
3147 To use this function, first create a type of the sort you want.
3148 Then compute its hash code from the fields of the type that
3149 make it different from other similar types.
3150 Then call this function and use the value.
3151 This function frees the type you pass in if it is a duplicate. */
3153 /* Set to 1 to debug without canonicalization. Never set by program. */
3154 int debug_no_type_hash = 0;
3156 tree
3157 type_hash_canon (unsigned int hashcode, tree type)
3159 tree t1;
3161 if (debug_no_type_hash)
3162 return type;
3164 /* See if the type is in the hash table already. If so, return it.
3165 Otherwise, add the type. */
3166 t1 = type_hash_lookup (hashcode, type);
3167 if (t1 != 0)
3169 #ifdef GATHER_STATISTICS
3170 tree_node_counts[(int) t_kind]--;
3171 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
3172 #endif
3173 return t1;
3175 else
3177 type_hash_add (hashcode, type);
3178 return type;
3182 /* See if the data pointed to by the type hash table is marked. We consider
3183 it marked if the type is marked or if a debug type number or symbol
3184 table entry has been made for the type. This reduces the amount of
3185 debugging output and eliminates that dependency of the debug output on
3186 the number of garbage collections. */
3188 static int
3189 type_hash_marked_p (const void *p)
3191 tree type = ((struct type_hash *) p)->type;
3193 return ggc_marked_p (type) || TYPE_SYMTAB_POINTER (type);
3196 static void
3197 print_type_hash_statistics (void)
3199 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
3200 (long) htab_size (type_hash_table),
3201 (long) htab_elements (type_hash_table),
3202 htab_collisions (type_hash_table));
3205 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
3206 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
3207 by adding the hash codes of the individual attributes. */
3209 unsigned int
3210 attribute_hash_list (tree list)
3212 unsigned int hashcode;
3213 tree tail;
3215 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
3216 /* ??? Do we want to add in TREE_VALUE too? */
3217 hashcode += TYPE_HASH (TREE_PURPOSE (tail));
3218 return hashcode;
3221 /* Given two lists of attributes, return true if list l2 is
3222 equivalent to l1. */
3225 attribute_list_equal (tree l1, tree l2)
3227 return attribute_list_contained (l1, l2)
3228 && attribute_list_contained (l2, l1);
3231 /* Given two lists of attributes, return true if list L2 is
3232 completely contained within L1. */
3233 /* ??? This would be faster if attribute names were stored in a canonicalized
3234 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
3235 must be used to show these elements are equivalent (which they are). */
3236 /* ??? It's not clear that attributes with arguments will always be handled
3237 correctly. */
3240 attribute_list_contained (tree l1, tree l2)
3242 tree t1, t2;
3244 /* First check the obvious, maybe the lists are identical. */
3245 if (l1 == l2)
3246 return 1;
3248 /* Maybe the lists are similar. */
3249 for (t1 = l1, t2 = l2;
3250 t1 != 0 && t2 != 0
3251 && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
3252 && TREE_VALUE (t1) == TREE_VALUE (t2);
3253 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
3255 /* Maybe the lists are equal. */
3256 if (t1 == 0 && t2 == 0)
3257 return 1;
3259 for (; t2 != 0; t2 = TREE_CHAIN (t2))
3261 tree attr;
3262 for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)), l1);
3263 attr != NULL_TREE;
3264 attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
3265 TREE_CHAIN (attr)))
3267 if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) == 1)
3268 break;
3271 if (attr == 0)
3272 return 0;
3274 if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) != 1)
3275 return 0;
3278 return 1;
3281 /* Given two lists of types
3282 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
3283 return 1 if the lists contain the same types in the same order.
3284 Also, the TREE_PURPOSEs must match. */
3287 type_list_equal (tree l1, tree l2)
3289 tree t1, t2;
3291 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
3292 if (TREE_VALUE (t1) != TREE_VALUE (t2)
3293 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
3294 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
3295 && (TREE_TYPE (TREE_PURPOSE (t1))
3296 == TREE_TYPE (TREE_PURPOSE (t2))))))
3297 return 0;
3299 return t1 == t2;
3302 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
3303 given by TYPE. If the argument list accepts variable arguments,
3304 then this function counts only the ordinary arguments. */
3307 type_num_arguments (tree type)
3309 int i = 0;
3310 tree t;
3312 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
3313 /* If the function does not take a variable number of arguments,
3314 the last element in the list will have type `void'. */
3315 if (VOID_TYPE_P (TREE_VALUE (t)))
3316 break;
3317 else
3318 ++i;
3320 return i;
3323 /* Nonzero if integer constants T1 and T2
3324 represent the same constant value. */
3327 tree_int_cst_equal (tree t1, tree t2)
3329 if (t1 == t2)
3330 return 1;
3332 if (t1 == 0 || t2 == 0)
3333 return 0;
3335 if (TREE_CODE (t1) == INTEGER_CST
3336 && TREE_CODE (t2) == INTEGER_CST
3337 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3338 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
3339 return 1;
3341 return 0;
3344 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
3345 The precise way of comparison depends on their data type. */
3348 tree_int_cst_lt (tree t1, tree t2)
3350 if (t1 == t2)
3351 return 0;
3353 if (TREE_UNSIGNED (TREE_TYPE (t1)) != TREE_UNSIGNED (TREE_TYPE (t2)))
3355 int t1_sgn = tree_int_cst_sgn (t1);
3356 int t2_sgn = tree_int_cst_sgn (t2);
3358 if (t1_sgn < t2_sgn)
3359 return 1;
3360 else if (t1_sgn > t2_sgn)
3361 return 0;
3362 /* Otherwise, both are non-negative, so we compare them as
3363 unsigned just in case one of them would overflow a signed
3364 type. */
3366 else if (! TREE_UNSIGNED (TREE_TYPE (t1)))
3367 return INT_CST_LT (t1, t2);
3369 return INT_CST_LT_UNSIGNED (t1, t2);
3372 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
3375 tree_int_cst_compare (tree t1, tree t2)
3377 if (tree_int_cst_lt (t1, t2))
3378 return -1;
3379 else if (tree_int_cst_lt (t2, t1))
3380 return 1;
3381 else
3382 return 0;
3385 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
3386 the host. If POS is zero, the value can be represented in a single
3387 HOST_WIDE_INT. If POS is nonzero, the value must be positive and can
3388 be represented in a single unsigned HOST_WIDE_INT. */
3391 host_integerp (tree t, int pos)
3393 return (TREE_CODE (t) == INTEGER_CST
3394 && ! TREE_OVERFLOW (t)
3395 && ((TREE_INT_CST_HIGH (t) == 0
3396 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
3397 || (! pos && TREE_INT_CST_HIGH (t) == -1
3398 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
3399 && ! TREE_UNSIGNED (TREE_TYPE (t)))
3400 || (pos && TREE_INT_CST_HIGH (t) == 0)));
3403 /* Return the HOST_WIDE_INT least significant bits of T if it is an
3404 INTEGER_CST and there is no overflow. POS is nonzero if the result must
3405 be positive. Abort if we cannot satisfy the above conditions. */
3407 HOST_WIDE_INT
3408 tree_low_cst (tree t, int pos)
3410 if (host_integerp (t, pos))
3411 return TREE_INT_CST_LOW (t);
3412 else
3413 abort ();
3416 /* Return the most significant bit of the integer constant T. */
3419 tree_int_cst_msb (tree t)
3421 int prec;
3422 HOST_WIDE_INT h;
3423 unsigned HOST_WIDE_INT l;
3425 /* Note that using TYPE_PRECISION here is wrong. We care about the
3426 actual bits, not the (arbitrary) range of the type. */
3427 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
3428 rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
3429 2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
3430 return (l & 1) == 1;
3433 /* Return an indication of the sign of the integer constant T.
3434 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
3435 Note that -1 will never be returned it T's type is unsigned. */
3438 tree_int_cst_sgn (tree t)
3440 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
3441 return 0;
3442 else if (TREE_UNSIGNED (TREE_TYPE (t)))
3443 return 1;
3444 else if (TREE_INT_CST_HIGH (t) < 0)
3445 return -1;
3446 else
3447 return 1;
3450 /* Compare two constructor-element-type constants. Return 1 if the lists
3451 are known to be equal; otherwise return 0. */
3454 simple_cst_list_equal (tree l1, tree l2)
3456 while (l1 != NULL_TREE && l2 != NULL_TREE)
3458 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
3459 return 0;
3461 l1 = TREE_CHAIN (l1);
3462 l2 = TREE_CHAIN (l2);
3465 return l1 == l2;
3468 /* Return truthvalue of whether T1 is the same tree structure as T2.
3469 Return 1 if they are the same.
3470 Return 0 if they are understandably different.
3471 Return -1 if either contains tree structure not understood by
3472 this function. */
3475 simple_cst_equal (tree t1, tree t2)
3477 enum tree_code code1, code2;
3478 int cmp;
3479 int i;
3481 if (t1 == t2)
3482 return 1;
3483 if (t1 == 0 || t2 == 0)
3484 return 0;
3486 code1 = TREE_CODE (t1);
3487 code2 = TREE_CODE (t2);
3489 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
3491 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3492 || code2 == NON_LVALUE_EXPR)
3493 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3494 else
3495 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
3498 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3499 || code2 == NON_LVALUE_EXPR)
3500 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
3502 if (code1 != code2)
3503 return 0;
3505 switch (code1)
3507 case INTEGER_CST:
3508 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3509 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
3511 case REAL_CST:
3512 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3514 case STRING_CST:
3515 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3516 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3517 TREE_STRING_LENGTH (t1)));
3519 case CONSTRUCTOR:
3520 if (CONSTRUCTOR_ELTS (t1) == CONSTRUCTOR_ELTS (t2))
3521 return 1;
3522 else
3523 abort ();
3525 case SAVE_EXPR:
3526 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3528 case CALL_EXPR:
3529 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3530 if (cmp <= 0)
3531 return cmp;
3532 return
3533 simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3535 case TARGET_EXPR:
3536 /* Special case: if either target is an unallocated VAR_DECL,
3537 it means that it's going to be unified with whatever the
3538 TARGET_EXPR is really supposed to initialize, so treat it
3539 as being equivalent to anything. */
3540 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
3541 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
3542 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
3543 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
3544 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
3545 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
3546 cmp = 1;
3547 else
3548 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3550 if (cmp <= 0)
3551 return cmp;
3553 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3555 case WITH_CLEANUP_EXPR:
3556 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3557 if (cmp <= 0)
3558 return cmp;
3560 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
3562 case COMPONENT_REF:
3563 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
3564 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3566 return 0;
3568 case VAR_DECL:
3569 case PARM_DECL:
3570 case CONST_DECL:
3571 case FUNCTION_DECL:
3572 return 0;
3574 default:
3575 break;
3578 /* This general rule works for most tree codes. All exceptions should be
3579 handled above. If this is a language-specific tree code, we can't
3580 trust what might be in the operand, so say we don't know
3581 the situation. */
3582 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
3583 return -1;
3585 switch (TREE_CODE_CLASS (code1))
3587 case '1':
3588 case '2':
3589 case '<':
3590 case 'e':
3591 case 'r':
3592 case 's':
3593 cmp = 1;
3594 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
3596 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
3597 if (cmp <= 0)
3598 return cmp;
3601 return cmp;
3603 default:
3604 return -1;
3608 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
3609 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
3610 than U, respectively. */
3613 compare_tree_int (tree t, unsigned HOST_WIDE_INT u)
3615 if (tree_int_cst_sgn (t) < 0)
3616 return -1;
3617 else if (TREE_INT_CST_HIGH (t) != 0)
3618 return 1;
3619 else if (TREE_INT_CST_LOW (t) == u)
3620 return 0;
3621 else if (TREE_INT_CST_LOW (t) < u)
3622 return -1;
3623 else
3624 return 1;
3627 /* Return true if CODE represents an associative tree code. Otherwise
3628 return false. */
3629 bool
3630 associative_tree_code (enum tree_code code)
3632 switch (code)
3634 case BIT_IOR_EXPR:
3635 case BIT_AND_EXPR:
3636 case BIT_XOR_EXPR:
3637 case PLUS_EXPR:
3638 case MINUS_EXPR:
3639 case MULT_EXPR:
3640 case LSHIFT_EXPR:
3641 case RSHIFT_EXPR:
3642 case MIN_EXPR:
3643 case MAX_EXPR:
3644 return true;
3646 default:
3647 break;
3649 return false;
3652 /* Return true if CODE represents an commutative tree code. Otherwise
3653 return false. */
3654 bool
3655 commutative_tree_code (enum tree_code code)
3657 switch (code)
3659 case PLUS_EXPR:
3660 case MULT_EXPR:
3661 case MIN_EXPR:
3662 case MAX_EXPR:
3663 case BIT_IOR_EXPR:
3664 case BIT_XOR_EXPR:
3665 case BIT_AND_EXPR:
3666 case NE_EXPR:
3667 case EQ_EXPR:
3668 return true;
3670 default:
3671 break;
3673 return false;
3676 /* Generate a hash value for an expression. This can be used iteratively
3677 by passing a previous result as the "val" argument.
3679 This function is intended to produce the same hash for expressions which
3680 would compare equal using operand_equal_p. */
3682 hashval_t
3683 iterative_hash_expr (tree t, hashval_t val)
3685 int i;
3686 enum tree_code code;
3687 char class;
3689 if (t == NULL_TREE)
3690 return iterative_hash_object (t, val);
3692 code = TREE_CODE (t);
3693 class = TREE_CODE_CLASS (code);
3695 if (class == 'd')
3697 /* Decls we can just compare by pointer. */
3698 val = iterative_hash_object (t, val);
3700 else if (class == 'c')
3702 /* Alas, constants aren't shared, so we can't rely on pointer
3703 identity. */
3704 if (code == INTEGER_CST)
3706 val = iterative_hash_object (TREE_INT_CST_LOW (t), val);
3707 val = iterative_hash_object (TREE_INT_CST_HIGH (t), val);
3709 else if (code == REAL_CST)
3710 val = iterative_hash (TREE_REAL_CST_PTR (t),
3711 sizeof (REAL_VALUE_TYPE), val);
3712 else if (code == STRING_CST)
3713 val = iterative_hash (TREE_STRING_POINTER (t),
3714 TREE_STRING_LENGTH (t), val);
3715 else if (code == COMPLEX_CST)
3717 val = iterative_hash_expr (TREE_REALPART (t), val);
3718 val = iterative_hash_expr (TREE_IMAGPART (t), val);
3720 else if (code == VECTOR_CST)
3721 val = iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
3722 else
3723 abort ();
3725 else if (IS_EXPR_CODE_CLASS (class))
3727 val = iterative_hash_object (code, val);
3729 if (code == NOP_EXPR || code == CONVERT_EXPR
3730 || code == NON_LVALUE_EXPR)
3731 val = iterative_hash_object (TREE_TYPE (t), val);
3733 if (commutative_tree_code (code))
3735 /* It's a commutative expression. We want to hash it the same
3736 however it appears. We do this by first hashing both operands
3737 and then rehashing based on the order of their independent
3738 hashes. */
3739 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
3740 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
3741 hashval_t t;
3743 if (one > two)
3744 t = one, one = two, two = t;
3746 val = iterative_hash_object (one, val);
3747 val = iterative_hash_object (two, val);
3749 else
3750 for (i = first_rtl_op (code) - 1; i >= 0; --i)
3751 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
3753 else if (code == TREE_LIST)
3755 /* A list of expressions, for a CALL_EXPR or as the elements of a
3756 VECTOR_CST. */
3757 for (; t; t = TREE_CHAIN (t))
3758 val = iterative_hash_expr (TREE_VALUE (t), val);
3760 else
3761 abort ();
3763 return val;
3766 /* Constructors for pointer, array and function types.
3767 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
3768 constructed by language-dependent code, not here.) */
3770 /* Construct, lay out and return the type of pointers to TO_TYPE
3771 with mode MODE. If such a type has already been constructed,
3772 reuse it. */
3774 tree
3775 build_pointer_type_for_mode (tree to_type, enum machine_mode mode)
3777 tree t = TYPE_POINTER_TO (to_type);
3779 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3780 if (t != 0 && mode == ptr_mode)
3781 return t;
3783 t = make_node (POINTER_TYPE);
3785 TREE_TYPE (t) = to_type;
3786 TYPE_MODE (t) = mode;
3788 /* Record this type as the pointer to TO_TYPE. */
3789 if (mode == ptr_mode)
3790 TYPE_POINTER_TO (to_type) = t;
3792 /* Lay out the type. This function has many callers that are concerned
3793 with expression-construction, and this simplifies them all.
3794 Also, it guarantees the TYPE_SIZE is in the same obstack as the type. */
3795 layout_type (t);
3797 return t;
3800 /* By default build pointers in ptr_mode. */
3802 tree
3803 build_pointer_type (tree to_type)
3805 return build_pointer_type_for_mode (to_type, ptr_mode);
3808 /* Construct, lay out and return the type of references to TO_TYPE
3809 with mode MODE. If such a type has already been constructed,
3810 reuse it. */
3812 tree
3813 build_reference_type_for_mode (tree to_type, enum machine_mode mode)
3815 tree t = TYPE_REFERENCE_TO (to_type);
3817 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3818 if (t != 0 && mode == ptr_mode)
3819 return t;
3821 t = make_node (REFERENCE_TYPE);
3823 TREE_TYPE (t) = to_type;
3824 TYPE_MODE (t) = mode;
3826 /* Record this type as the pointer to TO_TYPE. */
3827 if (mode == ptr_mode)
3828 TYPE_REFERENCE_TO (to_type) = t;
3830 layout_type (t);
3832 return t;
3836 /* Build the node for the type of references-to-TO_TYPE by default
3837 in ptr_mode. */
3839 tree
3840 build_reference_type (tree to_type)
3842 return build_reference_type_for_mode (to_type, ptr_mode);
3845 /* Build a type that is compatible with t but has no cv quals anywhere
3846 in its type, thus
3848 const char *const *const * -> char ***. */
3850 tree
3851 build_type_no_quals (tree t)
3853 switch (TREE_CODE (t))
3855 case POINTER_TYPE:
3856 return build_pointer_type (build_type_no_quals (TREE_TYPE (t)));
3857 case REFERENCE_TYPE:
3858 return build_reference_type (build_type_no_quals (TREE_TYPE (t)));
3859 default:
3860 return TYPE_MAIN_VARIANT (t);
3864 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
3865 MAXVAL should be the maximum value in the domain
3866 (one less than the length of the array).
3868 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
3869 We don't enforce this limit, that is up to caller (e.g. language front end).
3870 The limit exists because the result is a signed type and we don't handle
3871 sizes that use more than one HOST_WIDE_INT. */
3873 tree
3874 build_index_type (tree maxval)
3876 tree itype = make_node (INTEGER_TYPE);
3878 TREE_TYPE (itype) = sizetype;
3879 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
3880 TYPE_MIN_VALUE (itype) = size_zero_node;
3881 TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
3882 TYPE_MODE (itype) = TYPE_MODE (sizetype);
3883 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
3884 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
3885 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
3886 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
3888 if (host_integerp (maxval, 1))
3889 return type_hash_canon (tree_low_cst (maxval, 1), itype);
3890 else
3891 return itype;
3894 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
3895 ENUMERAL_TYPE, BOOLEAN_TYPE, or CHAR_TYPE), with
3896 low bound LOWVAL and high bound HIGHVAL.
3897 if TYPE==NULL_TREE, sizetype is used. */
3899 tree
3900 build_range_type (tree type, tree lowval, tree highval)
3902 tree itype = make_node (INTEGER_TYPE);
3904 TREE_TYPE (itype) = type;
3905 if (type == NULL_TREE)
3906 type = sizetype;
3908 TYPE_MIN_VALUE (itype) = convert (type, lowval);
3909 TYPE_MAX_VALUE (itype) = highval ? convert (type, highval) : NULL;
3911 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
3912 TYPE_MODE (itype) = TYPE_MODE (type);
3913 TYPE_SIZE (itype) = TYPE_SIZE (type);
3914 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
3915 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
3916 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
3918 if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
3919 return type_hash_canon (tree_low_cst (highval, 0)
3920 - tree_low_cst (lowval, 0),
3921 itype);
3922 else
3923 return itype;
3926 /* Just like build_index_type, but takes lowval and highval instead
3927 of just highval (maxval). */
3929 tree
3930 build_index_2_type (tree lowval, tree highval)
3932 return build_range_type (sizetype, lowval, highval);
3935 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
3936 and number of elements specified by the range of values of INDEX_TYPE.
3937 If such a type has already been constructed, reuse it. */
3939 tree
3940 build_array_type (tree elt_type, tree index_type)
3942 tree t;
3943 unsigned int hashcode;
3945 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
3947 error ("arrays of functions are not meaningful");
3948 elt_type = integer_type_node;
3951 /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
3952 build_pointer_type (elt_type);
3954 /* Allocate the array after the pointer type,
3955 in case we free it in type_hash_canon. */
3956 t = make_node (ARRAY_TYPE);
3957 TREE_TYPE (t) = elt_type;
3958 TYPE_DOMAIN (t) = index_type;
3960 if (index_type == 0)
3962 return t;
3965 hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
3966 t = type_hash_canon (hashcode, t);
3968 if (!COMPLETE_TYPE_P (t))
3969 layout_type (t);
3970 return t;
3973 /* Return the TYPE of the elements comprising
3974 the innermost dimension of ARRAY. */
3976 tree
3977 get_inner_array_type (tree array)
3979 tree type = TREE_TYPE (array);
3981 while (TREE_CODE (type) == ARRAY_TYPE)
3982 type = TREE_TYPE (type);
3984 return type;
3987 /* Construct, lay out and return
3988 the type of functions returning type VALUE_TYPE
3989 given arguments of types ARG_TYPES.
3990 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
3991 are data type nodes for the arguments of the function.
3992 If such a type has already been constructed, reuse it. */
3994 tree
3995 build_function_type (tree value_type, tree arg_types)
3997 tree t;
3998 unsigned int hashcode;
4000 if (TREE_CODE (value_type) == FUNCTION_TYPE)
4002 error ("function return type cannot be function");
4003 value_type = integer_type_node;
4006 /* Make a node of the sort we want. */
4007 t = make_node (FUNCTION_TYPE);
4008 TREE_TYPE (t) = value_type;
4009 TYPE_ARG_TYPES (t) = arg_types;
4011 /* If we already have such a type, use the old one and free this one. */
4012 hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
4013 t = type_hash_canon (hashcode, t);
4015 if (!COMPLETE_TYPE_P (t))
4016 layout_type (t);
4017 return t;
4020 /* Build a function type. The RETURN_TYPE is the type returned by the
4021 function. If additional arguments are provided, they are
4022 additional argument types. The list of argument types must always
4023 be terminated by NULL_TREE. */
4025 tree
4026 build_function_type_list (tree return_type, ...)
4028 tree t, args, last;
4029 va_list p;
4031 va_start (p, return_type);
4033 t = va_arg (p, tree);
4034 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (p, tree))
4035 args = tree_cons (NULL_TREE, t, args);
4037 last = args;
4038 args = nreverse (args);
4039 TREE_CHAIN (last) = void_list_node;
4040 args = build_function_type (return_type, args);
4042 va_end (p);
4043 return args;
4046 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
4047 and ARGTYPES (a TREE_LIST) are the return type and arguments types
4048 for the method. An implicit additional parameter (of type
4049 pointer-to-BASETYPE) is added to the ARGTYPES. */
4051 tree
4052 build_method_type_directly (tree basetype,
4053 tree rettype,
4054 tree argtypes)
4056 tree t;
4057 tree ptype;
4058 int hashcode;
4060 /* Make a node of the sort we want. */
4061 t = make_node (METHOD_TYPE);
4063 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
4064 TREE_TYPE (t) = rettype;
4065 ptype = build_pointer_type (basetype);
4067 /* The actual arglist for this function includes a "hidden" argument
4068 which is "this". Put it into the list of argument types. */
4069 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
4070 TYPE_ARG_TYPES (t) = argtypes;
4072 /* If we already have such a type, use the old one and free this one.
4073 Note that it also frees up the above cons cell if found. */
4074 hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) +
4075 type_hash_list (argtypes);
4077 t = type_hash_canon (hashcode, t);
4079 if (!COMPLETE_TYPE_P (t))
4080 layout_type (t);
4082 return t;
4085 /* Construct, lay out and return the type of methods belonging to class
4086 BASETYPE and whose arguments and values are described by TYPE.
4087 If that type exists already, reuse it.
4088 TYPE must be a FUNCTION_TYPE node. */
4090 tree
4091 build_method_type (tree basetype, tree type)
4093 if (TREE_CODE (type) != FUNCTION_TYPE)
4094 abort ();
4096 return build_method_type_directly (basetype,
4097 TREE_TYPE (type),
4098 TYPE_ARG_TYPES (type));
4101 /* Construct, lay out and return the type of offsets to a value
4102 of type TYPE, within an object of type BASETYPE.
4103 If a suitable offset type exists already, reuse it. */
4105 tree
4106 build_offset_type (tree basetype, tree type)
4108 tree t;
4109 unsigned int hashcode;
4111 /* Make a node of the sort we want. */
4112 t = make_node (OFFSET_TYPE);
4114 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
4115 TREE_TYPE (t) = type;
4117 /* If we already have such a type, use the old one and free this one. */
4118 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
4119 t = type_hash_canon (hashcode, t);
4121 if (!COMPLETE_TYPE_P (t))
4122 layout_type (t);
4124 return t;
4127 /* Create a complex type whose components are COMPONENT_TYPE. */
4129 tree
4130 build_complex_type (tree component_type)
4132 tree t;
4133 unsigned int hashcode;
4135 /* Make a node of the sort we want. */
4136 t = make_node (COMPLEX_TYPE);
4138 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
4139 set_type_quals (t, TYPE_QUALS (component_type));
4141 /* If we already have such a type, use the old one and free this one. */
4142 hashcode = TYPE_HASH (component_type);
4143 t = type_hash_canon (hashcode, t);
4145 if (!COMPLETE_TYPE_P (t))
4146 layout_type (t);
4148 /* If we are writing Dwarf2 output we need to create a name,
4149 since complex is a fundamental type. */
4150 if ((write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
4151 && ! TYPE_NAME (t))
4153 const char *name;
4154 if (component_type == char_type_node)
4155 name = "complex char";
4156 else if (component_type == signed_char_type_node)
4157 name = "complex signed char";
4158 else if (component_type == unsigned_char_type_node)
4159 name = "complex unsigned char";
4160 else if (component_type == short_integer_type_node)
4161 name = "complex short int";
4162 else if (component_type == short_unsigned_type_node)
4163 name = "complex short unsigned int";
4164 else if (component_type == integer_type_node)
4165 name = "complex int";
4166 else if (component_type == unsigned_type_node)
4167 name = "complex unsigned int";
4168 else if (component_type == long_integer_type_node)
4169 name = "complex long int";
4170 else if (component_type == long_unsigned_type_node)
4171 name = "complex long unsigned int";
4172 else if (component_type == long_long_integer_type_node)
4173 name = "complex long long int";
4174 else if (component_type == long_long_unsigned_type_node)
4175 name = "complex long long unsigned int";
4176 else
4177 name = 0;
4179 if (name != 0)
4180 TYPE_NAME (t) = get_identifier (name);
4183 return t;
4186 /* Return OP, stripped of any conversions to wider types as much as is safe.
4187 Converting the value back to OP's type makes a value equivalent to OP.
4189 If FOR_TYPE is nonzero, we return a value which, if converted to
4190 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
4192 If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
4193 narrowest type that can hold the value, even if they don't exactly fit.
4194 Otherwise, bit-field references are changed to a narrower type
4195 only if they can be fetched directly from memory in that type.
4197 OP must have integer, real or enumeral type. Pointers are not allowed!
4199 There are some cases where the obvious value we could return
4200 would regenerate to OP if converted to OP's type,
4201 but would not extend like OP to wider types.
4202 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
4203 For example, if OP is (unsigned short)(signed char)-1,
4204 we avoid returning (signed char)-1 if FOR_TYPE is int,
4205 even though extending that to an unsigned short would regenerate OP,
4206 since the result of extending (signed char)-1 to (int)
4207 is different from (int) OP. */
4209 tree
4210 get_unwidened (tree op, tree for_type)
4212 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
4213 tree type = TREE_TYPE (op);
4214 unsigned final_prec
4215 = TYPE_PRECISION (for_type != 0 ? for_type : type);
4216 int uns
4217 = (for_type != 0 && for_type != type
4218 && final_prec > TYPE_PRECISION (type)
4219 && TREE_UNSIGNED (type));
4220 tree win = op;
4222 while (TREE_CODE (op) == NOP_EXPR)
4224 int bitschange
4225 = TYPE_PRECISION (TREE_TYPE (op))
4226 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
4228 /* Truncations are many-one so cannot be removed.
4229 Unless we are later going to truncate down even farther. */
4230 if (bitschange < 0
4231 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
4232 break;
4234 /* See what's inside this conversion. If we decide to strip it,
4235 we will set WIN. */
4236 op = TREE_OPERAND (op, 0);
4238 /* If we have not stripped any zero-extensions (uns is 0),
4239 we can strip any kind of extension.
4240 If we have previously stripped a zero-extension,
4241 only zero-extensions can safely be stripped.
4242 Any extension can be stripped if the bits it would produce
4243 are all going to be discarded later by truncating to FOR_TYPE. */
4245 if (bitschange > 0)
4247 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
4248 win = op;
4249 /* TREE_UNSIGNED says whether this is a zero-extension.
4250 Let's avoid computing it if it does not affect WIN
4251 and if UNS will not be needed again. */
4252 if ((uns || TREE_CODE (op) == NOP_EXPR)
4253 && TREE_UNSIGNED (TREE_TYPE (op)))
4255 uns = 1;
4256 win = op;
4261 if (TREE_CODE (op) == COMPONENT_REF
4262 /* Since type_for_size always gives an integer type. */
4263 && TREE_CODE (type) != REAL_TYPE
4264 /* Don't crash if field not laid out yet. */
4265 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
4266 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
4268 unsigned int innerprec
4269 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4270 int unsignedp = (TREE_UNSIGNED (TREE_OPERAND (op, 1))
4271 || TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
4272 type = (*lang_hooks.types.type_for_size) (innerprec, unsignedp);
4274 /* We can get this structure field in the narrowest type it fits in.
4275 If FOR_TYPE is 0, do this only for a field that matches the
4276 narrower type exactly and is aligned for it
4277 The resulting extension to its nominal type (a fullword type)
4278 must fit the same conditions as for other extensions. */
4280 if (type != 0
4281 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (op)))
4282 && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
4283 && (! uns || final_prec <= innerprec || unsignedp))
4285 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4286 TREE_OPERAND (op, 1));
4287 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4288 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4292 return win;
4295 /* Return OP or a simpler expression for a narrower value
4296 which can be sign-extended or zero-extended to give back OP.
4297 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
4298 or 0 if the value should be sign-extended. */
4300 tree
4301 get_narrower (tree op, int *unsignedp_ptr)
4303 int uns = 0;
4304 int first = 1;
4305 tree win = op;
4307 while (TREE_CODE (op) == NOP_EXPR)
4309 int bitschange
4310 = (TYPE_PRECISION (TREE_TYPE (op))
4311 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
4313 /* Truncations are many-one so cannot be removed. */
4314 if (bitschange < 0)
4315 break;
4317 /* See what's inside this conversion. If we decide to strip it,
4318 we will set WIN. */
4320 if (bitschange > 0)
4322 op = TREE_OPERAND (op, 0);
4323 /* An extension: the outermost one can be stripped,
4324 but remember whether it is zero or sign extension. */
4325 if (first)
4326 uns = TREE_UNSIGNED (TREE_TYPE (op));
4327 /* Otherwise, if a sign extension has been stripped,
4328 only sign extensions can now be stripped;
4329 if a zero extension has been stripped, only zero-extensions. */
4330 else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
4331 break;
4332 first = 0;
4334 else /* bitschange == 0 */
4336 /* A change in nominal type can always be stripped, but we must
4337 preserve the unsignedness. */
4338 if (first)
4339 uns = TREE_UNSIGNED (TREE_TYPE (op));
4340 first = 0;
4341 op = TREE_OPERAND (op, 0);
4344 win = op;
4347 if (TREE_CODE (op) == COMPONENT_REF
4348 /* Since type_for_size always gives an integer type. */
4349 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
4350 /* Ensure field is laid out already. */
4351 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0)
4353 unsigned HOST_WIDE_INT innerprec
4354 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4355 int unsignedp = (TREE_UNSIGNED (TREE_OPERAND (op, 1))
4356 || TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
4357 tree type = (*lang_hooks.types.type_for_size) (innerprec, unsignedp);
4359 /* We can get this structure field in a narrower type that fits it,
4360 but the resulting extension to its nominal type (a fullword type)
4361 must satisfy the same conditions as for other extensions.
4363 Do this only for fields that are aligned (not bit-fields),
4364 because when bit-field insns will be used there is no
4365 advantage in doing this. */
4367 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
4368 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
4369 && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
4370 && type != 0)
4372 if (first)
4373 uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
4374 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4375 TREE_OPERAND (op, 1));
4376 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4377 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4380 *unsignedp_ptr = uns;
4381 return win;
4384 /* Nonzero if integer constant C has a value that is permissible
4385 for type TYPE (an INTEGER_TYPE). */
4388 int_fits_type_p (tree c, tree type)
4390 tree type_low_bound = TYPE_MIN_VALUE (type);
4391 tree type_high_bound = TYPE_MAX_VALUE (type);
4392 int ok_for_low_bound, ok_for_high_bound;
4394 /* Perform some generic filtering first, which may allow making a decision
4395 even if the bounds are not constant. First, negative integers never fit
4396 in unsigned types, */
4397 if ((TREE_UNSIGNED (type) && tree_int_cst_sgn (c) < 0)
4398 /* Also, unsigned integers with top bit set never fit signed types. */
4399 || (! TREE_UNSIGNED (type)
4400 && TREE_UNSIGNED (TREE_TYPE (c)) && tree_int_cst_msb (c)))
4401 return 0;
4403 /* If at least one bound of the type is a constant integer, we can check
4404 ourselves and maybe make a decision. If no such decision is possible, but
4405 this type is a subtype, try checking against that. Otherwise, use
4406 force_fit_type, which checks against the precision.
4408 Compute the status for each possibly constant bound, and return if we see
4409 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
4410 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
4411 for "constant known to fit". */
4413 ok_for_low_bound = -1;
4414 ok_for_high_bound = -1;
4416 /* Check if C >= type_low_bound. */
4417 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
4419 ok_for_low_bound = ! tree_int_cst_lt (c, type_low_bound);
4420 if (! ok_for_low_bound)
4421 return 0;
4424 /* Check if c <= type_high_bound. */
4425 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
4427 ok_for_high_bound = ! tree_int_cst_lt (type_high_bound, c);
4428 if (! ok_for_high_bound)
4429 return 0;
4432 /* If the constant fits both bounds, the result is known. */
4433 if (ok_for_low_bound == 1 && ok_for_high_bound == 1)
4434 return 1;
4436 /* If we haven't been able to decide at this point, there nothing more we
4437 can check ourselves here. Look at the base type if we have one. */
4438 else if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != 0)
4439 return int_fits_type_p (c, TREE_TYPE (type));
4441 /* Or to force_fit_type, if nothing else. */
4442 else
4444 c = copy_node (c);
4445 TREE_TYPE (c) = type;
4446 return !force_fit_type (c, 0);
4450 /* Returns true if T is, contains, or refers to a type with variable
4451 size. This concept is more general than that of C99 'variably
4452 modified types': in C99, a struct type is never variably modified
4453 because a VLA may not appear as a structure member. However, in
4454 GNU C code like:
4456 struct S { int i[f()]; };
4458 is valid, and other languages may define similar constructs. */
4460 bool
4461 variably_modified_type_p (tree type)
4463 tree t;
4465 if (type == error_mark_node)
4466 return false;
4468 /* If TYPE itself has variable size, it is variably modified.
4470 We do not yet have a representation of the C99 '[*]' syntax.
4471 When a representation is chosen, this function should be modified
4472 to test for that case as well. */
4473 t = TYPE_SIZE (type);
4474 if (t && t != error_mark_node && TREE_CODE (t) != INTEGER_CST)
4475 return true;
4477 switch (TREE_CODE (type))
4479 case POINTER_TYPE:
4480 case REFERENCE_TYPE:
4481 case ARRAY_TYPE:
4482 /* If TYPE is a pointer or reference, it is variably modified if
4483 the type pointed to is variably modified. Similarly for arrays;
4484 note that VLAs are handled by the TYPE_SIZE check above. */
4485 return variably_modified_type_p (TREE_TYPE (type));
4487 case FUNCTION_TYPE:
4488 case METHOD_TYPE:
4489 /* If TYPE is a function type, it is variably modified if any of the
4490 parameters or the return type are variably modified. */
4492 tree parm;
4494 if (variably_modified_type_p (TREE_TYPE (type)))
4495 return true;
4496 for (parm = TYPE_ARG_TYPES (type);
4497 parm && parm != void_list_node;
4498 parm = TREE_CHAIN (parm))
4499 if (variably_modified_type_p (TREE_VALUE (parm)))
4500 return true;
4502 break;
4504 case INTEGER_TYPE:
4505 /* Scalar types are variably modified if their end points
4506 aren't constant. */
4507 t = TYPE_MIN_VALUE (type);
4508 if (t && t != error_mark_node && TREE_CODE (t) != INTEGER_CST)
4509 return true;
4510 t = TYPE_MAX_VALUE (type);
4511 if (t && t != error_mark_node && TREE_CODE (t) != INTEGER_CST)
4512 return true;
4513 return false;
4515 default:
4516 break;
4519 /* The current language may have other cases to check, but in general,
4520 all other types are not variably modified. */
4521 return (*lang_hooks.tree_inlining.var_mod_type_p) (type);
4524 /* Given a DECL or TYPE, return the scope in which it was declared, or
4525 NULL_TREE if there is no containing scope. */
4527 tree
4528 get_containing_scope (tree t)
4530 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
4533 /* Return the innermost context enclosing DECL that is
4534 a FUNCTION_DECL, or zero if none. */
4536 tree
4537 decl_function_context (tree decl)
4539 tree context;
4541 if (TREE_CODE (decl) == ERROR_MARK)
4542 return 0;
4544 if (TREE_CODE (decl) == SAVE_EXPR)
4545 context = SAVE_EXPR_CONTEXT (decl);
4547 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
4548 where we look up the function at runtime. Such functions always take
4549 a first argument of type 'pointer to real context'.
4551 C++ should really be fixed to use DECL_CONTEXT for the real context,
4552 and use something else for the "virtual context". */
4553 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
4554 context
4555 = TYPE_MAIN_VARIANT
4556 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4557 else
4558 context = DECL_CONTEXT (decl);
4560 while (context && TREE_CODE (context) != FUNCTION_DECL)
4562 if (TREE_CODE (context) == BLOCK)
4563 context = BLOCK_SUPERCONTEXT (context);
4564 else
4565 context = get_containing_scope (context);
4568 return context;
4571 /* Return the innermost context enclosing DECL that is
4572 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
4573 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
4575 tree
4576 decl_type_context (tree decl)
4578 tree context = DECL_CONTEXT (decl);
4580 while (context)
4581 switch (TREE_CODE (context))
4583 case NAMESPACE_DECL:
4584 case TRANSLATION_UNIT_DECL:
4585 return NULL_TREE;
4587 case RECORD_TYPE:
4588 case UNION_TYPE:
4589 case QUAL_UNION_TYPE:
4590 return context;
4592 case TYPE_DECL:
4593 case FUNCTION_DECL:
4594 context = DECL_CONTEXT (context);
4595 break;
4597 case BLOCK:
4598 context = BLOCK_SUPERCONTEXT (context);
4599 break;
4601 default:
4602 abort ();
4605 return NULL_TREE;
4608 /* CALL is a CALL_EXPR. Return the declaration for the function
4609 called, or NULL_TREE if the called function cannot be
4610 determined. */
4612 tree
4613 get_callee_fndecl (tree call)
4615 tree addr;
4617 /* It's invalid to call this function with anything but a
4618 CALL_EXPR. */
4619 if (TREE_CODE (call) != CALL_EXPR)
4620 abort ();
4622 /* The first operand to the CALL is the address of the function
4623 called. */
4624 addr = TREE_OPERAND (call, 0);
4626 STRIP_NOPS (addr);
4628 /* If this is a readonly function pointer, extract its initial value. */
4629 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
4630 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
4631 && DECL_INITIAL (addr))
4632 addr = DECL_INITIAL (addr);
4634 /* If the address is just `&f' for some function `f', then we know
4635 that `f' is being called. */
4636 if (TREE_CODE (addr) == ADDR_EXPR
4637 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
4638 return TREE_OPERAND (addr, 0);
4640 /* We couldn't figure out what was being called. Maybe the front
4641 end has some idea. */
4642 return (*lang_hooks.lang_get_callee_fndecl) (call);
4645 /* Print debugging information about tree nodes generated during the compile,
4646 and any language-specific information. */
4648 void
4649 dump_tree_statistics (void)
4651 #ifdef GATHER_STATISTICS
4652 int i;
4653 int total_nodes, total_bytes;
4654 #endif
4656 fprintf (stderr, "\n??? tree nodes created\n\n");
4657 #ifdef GATHER_STATISTICS
4658 fprintf (stderr, "Kind Nodes Bytes\n");
4659 fprintf (stderr, "---------------------------------------\n");
4660 total_nodes = total_bytes = 0;
4661 for (i = 0; i < (int) all_kinds; i++)
4663 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
4664 tree_node_counts[i], tree_node_sizes[i]);
4665 total_nodes += tree_node_counts[i];
4666 total_bytes += tree_node_sizes[i];
4668 fprintf (stderr, "---------------------------------------\n");
4669 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
4670 fprintf (stderr, "---------------------------------------\n");
4671 #else
4672 fprintf (stderr, "(No per-node statistics)\n");
4673 #endif
4674 print_type_hash_statistics ();
4675 (*lang_hooks.print_statistics) ();
4678 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
4680 /* Generate a crc32 of a string. */
4682 unsigned
4683 crc32_string (unsigned chksum, const char *string)
4687 unsigned value = *string << 24;
4688 unsigned ix;
4690 for (ix = 8; ix--; value <<= 1)
4692 unsigned feedback;
4694 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
4695 chksum <<= 1;
4696 chksum ^= feedback;
4699 while (*string++);
4700 return chksum;
4703 /* P is a string that will be used in a symbol. Mask out any characters
4704 that are not valid in that context. */
4706 void
4707 clean_symbol_name (char *p)
4709 for (; *p; p++)
4710 if (! (ISALNUM (*p)
4711 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
4712 || *p == '$'
4713 #endif
4714 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
4715 || *p == '.'
4716 #endif
4718 *p = '_';
4721 /* Generate a name for a function unique to this translation unit.
4722 TYPE is some string to identify the purpose of this function to the
4723 linker or collect2. */
4725 tree
4726 get_file_function_name_long (const char *type)
4728 char *buf;
4729 const char *p;
4730 char *q;
4732 if (first_global_object_name)
4733 p = first_global_object_name;
4734 else
4736 /* We don't have anything that we know to be unique to this translation
4737 unit, so use what we do have and throw in some randomness. */
4738 unsigned len;
4739 const char *name = weak_global_object_name;
4740 const char *file = main_input_filename;
4742 if (! name)
4743 name = "";
4744 if (! file)
4745 file = input_filename;
4747 len = strlen (file);
4748 q = alloca (9 * 2 + len + 1);
4749 memcpy (q, file, len + 1);
4750 clean_symbol_name (q);
4752 sprintf (q + len, "_%08X_%08X", crc32_string (0, name),
4753 crc32_string (0, flag_random_seed));
4755 p = q;
4758 buf = alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p) + strlen (type));
4760 /* Set up the name of the file-level functions we may need.
4761 Use a global object (which is already required to be unique over
4762 the program) rather than the file name (which imposes extra
4763 constraints). */
4764 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
4766 return get_identifier (buf);
4769 /* If KIND=='I', return a suitable global initializer (constructor) name.
4770 If KIND=='D', return a suitable global clean-up (destructor) name. */
4772 tree
4773 get_file_function_name (int kind)
4775 char p[2];
4777 p[0] = kind;
4778 p[1] = 0;
4780 return get_file_function_name_long (p);
4783 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4784 The result is placed in BUFFER (which has length BIT_SIZE),
4785 with one bit in each char ('\000' or '\001').
4787 If the constructor is constant, NULL_TREE is returned.
4788 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4790 tree
4791 get_set_constructor_bits (tree init, char *buffer, int bit_size)
4793 int i;
4794 tree vals;
4795 HOST_WIDE_INT domain_min
4796 = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (init))), 0);
4797 tree non_const_bits = NULL_TREE;
4799 for (i = 0; i < bit_size; i++)
4800 buffer[i] = 0;
4802 for (vals = TREE_OPERAND (init, 1);
4803 vals != NULL_TREE; vals = TREE_CHAIN (vals))
4805 if (!host_integerp (TREE_VALUE (vals), 0)
4806 || (TREE_PURPOSE (vals) != NULL_TREE
4807 && !host_integerp (TREE_PURPOSE (vals), 0)))
4808 non_const_bits
4809 = tree_cons (TREE_PURPOSE (vals), TREE_VALUE (vals), non_const_bits);
4810 else if (TREE_PURPOSE (vals) != NULL_TREE)
4812 /* Set a range of bits to ones. */
4813 HOST_WIDE_INT lo_index
4814 = tree_low_cst (TREE_PURPOSE (vals), 0) - domain_min;
4815 HOST_WIDE_INT hi_index
4816 = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
4818 if (lo_index < 0 || lo_index >= bit_size
4819 || hi_index < 0 || hi_index >= bit_size)
4820 abort ();
4821 for (; lo_index <= hi_index; lo_index++)
4822 buffer[lo_index] = 1;
4824 else
4826 /* Set a single bit to one. */
4827 HOST_WIDE_INT index
4828 = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
4829 if (index < 0 || index >= bit_size)
4831 error ("invalid initializer for bit string");
4832 return NULL_TREE;
4834 buffer[index] = 1;
4837 return non_const_bits;
4840 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4841 The result is placed in BUFFER (which is an array of bytes).
4842 If the constructor is constant, NULL_TREE is returned.
4843 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4845 tree
4846 get_set_constructor_bytes (tree init, unsigned char *buffer, int wd_size)
4848 int i;
4849 int set_word_size = BITS_PER_UNIT;
4850 int bit_size = wd_size * set_word_size;
4851 int bit_pos = 0;
4852 unsigned char *bytep = buffer;
4853 char *bit_buffer = alloca (bit_size);
4854 tree non_const_bits = get_set_constructor_bits (init, bit_buffer, bit_size);
4856 for (i = 0; i < wd_size; i++)
4857 buffer[i] = 0;
4859 for (i = 0; i < bit_size; i++)
4861 if (bit_buffer[i])
4863 if (BYTES_BIG_ENDIAN)
4864 *bytep |= (1 << (set_word_size - 1 - bit_pos));
4865 else
4866 *bytep |= 1 << bit_pos;
4868 bit_pos++;
4869 if (bit_pos >= set_word_size)
4870 bit_pos = 0, bytep++;
4872 return non_const_bits;
4875 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4876 /* Complain that the tree code of NODE does not match the expected CODE.
4877 FILE, LINE, and FUNCTION are of the caller. */
4879 void
4880 tree_check_failed (const tree node, enum tree_code code, const char *file,
4881 int line, const char *function)
4883 internal_error ("tree check: expected %s, have %s in %s, at %s:%d",
4884 tree_code_name[code], tree_code_name[TREE_CODE (node)],
4885 function, trim_filename (file), line);
4888 /* Similar to above, except that we check for a class of tree
4889 code, given in CL. */
4891 void
4892 tree_class_check_failed (const tree node, int cl, const char *file,
4893 int line, const char *function)
4895 internal_error
4896 ("tree check: expected class '%c', have '%c' (%s) in %s, at %s:%d",
4897 cl, TREE_CODE_CLASS (TREE_CODE (node)),
4898 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
4901 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
4902 (dynamically sized) vector. */
4904 void
4905 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
4906 const char *function)
4908 internal_error
4909 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
4910 idx + 1, len, function, trim_filename (file), line);
4913 /* Similar to above, except that the check is for the bounds of the operand
4914 vector of an expression node. */
4916 void
4917 tree_operand_check_failed (int idx, enum tree_code code, const char *file,
4918 int line, const char *function)
4920 internal_error
4921 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
4922 idx + 1, tree_code_name[code], TREE_CODE_LENGTH (code),
4923 function, trim_filename (file), line);
4925 #endif /* ENABLE_TREE_CHECKING */
4927 /* For a new vector type node T, build the information necessary for
4928 debugging output. */
4930 static void
4931 finish_vector_type (tree t)
4933 layout_type (t);
4936 tree index = build_int_2 (TYPE_VECTOR_SUBPARTS (t) - 1, 0);
4937 tree array = build_array_type (TREE_TYPE (t),
4938 build_index_type (index));
4939 tree rt = make_node (RECORD_TYPE);
4941 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
4942 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
4943 layout_type (rt);
4944 TYPE_DEBUG_REPRESENTATION_TYPE (t) = rt;
4945 /* In dwarfout.c, type lookup uses TYPE_UID numbers. We want to output
4946 the representation type, and we want to find that die when looking up
4947 the vector type. This is most easily achieved by making the TYPE_UID
4948 numbers equal. */
4949 TYPE_UID (rt) = TYPE_UID (t);
4953 /* Create nodes for all integer types (and error_mark_node) using the sizes
4954 of C datatypes. The caller should call set_sizetype soon after calling
4955 this function to select one of the types as sizetype. */
4957 void
4958 build_common_tree_nodes (int signed_char)
4960 error_mark_node = make_node (ERROR_MARK);
4961 TREE_TYPE (error_mark_node) = error_mark_node;
4963 initialize_sizetypes ();
4965 /* Define both `signed char' and `unsigned char'. */
4966 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
4967 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
4969 /* Define `char', which is like either `signed char' or `unsigned char'
4970 but not the same as either. */
4971 char_type_node
4972 = (signed_char
4973 ? make_signed_type (CHAR_TYPE_SIZE)
4974 : make_unsigned_type (CHAR_TYPE_SIZE));
4976 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
4977 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
4978 integer_type_node = make_signed_type (INT_TYPE_SIZE);
4979 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
4980 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
4981 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
4982 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
4983 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
4985 /* Define a boolean type. This type only represents boolean values but
4986 may be larger than char depending on the value of BOOL_TYPE_SIZE.
4987 Front ends which want to override this size (i.e. Java) can redefine
4988 boolean_type_node before calling build_common_tree_nodes_2. */
4989 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
4990 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
4991 TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
4992 TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
4993 TYPE_PRECISION (boolean_type_node) = 1;
4995 intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
4996 intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
4997 intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
4998 intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
4999 intTI_type_node = make_signed_type (GET_MODE_BITSIZE (TImode));
5001 unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
5002 unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
5003 unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
5004 unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
5005 unsigned_intTI_type_node = make_unsigned_type (GET_MODE_BITSIZE (TImode));
5007 access_public_node = get_identifier ("public");
5008 access_protected_node = get_identifier ("protected");
5009 access_private_node = get_identifier ("private");
5012 /* Call this function after calling build_common_tree_nodes and set_sizetype.
5013 It will create several other common tree nodes. */
5015 void
5016 build_common_tree_nodes_2 (int short_double)
5018 /* Define these next since types below may used them. */
5019 integer_zero_node = build_int_2 (0, 0);
5020 integer_one_node = build_int_2 (1, 0);
5021 integer_minus_one_node = build_int_2 (-1, -1);
5023 size_zero_node = size_int (0);
5024 size_one_node = size_int (1);
5025 bitsize_zero_node = bitsize_int (0);
5026 bitsize_one_node = bitsize_int (1);
5027 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
5029 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
5030 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
5032 void_type_node = make_node (VOID_TYPE);
5033 layout_type (void_type_node);
5035 /* We are not going to have real types in C with less than byte alignment,
5036 so we might as well not have any types that claim to have it. */
5037 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
5038 TYPE_USER_ALIGN (void_type_node) = 0;
5040 null_pointer_node = build_int_2 (0, 0);
5041 TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
5042 layout_type (TREE_TYPE (null_pointer_node));
5044 ptr_type_node = build_pointer_type (void_type_node);
5045 const_ptr_type_node
5046 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
5048 float_type_node = make_node (REAL_TYPE);
5049 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
5050 layout_type (float_type_node);
5052 double_type_node = make_node (REAL_TYPE);
5053 if (short_double)
5054 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
5055 else
5056 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
5057 layout_type (double_type_node);
5059 long_double_type_node = make_node (REAL_TYPE);
5060 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
5061 layout_type (long_double_type_node);
5063 float_ptr_type_node = build_pointer_type (float_type_node);
5064 double_ptr_type_node = build_pointer_type (double_type_node);
5065 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
5066 integer_ptr_type_node = build_pointer_type (integer_type_node);
5068 complex_integer_type_node = make_node (COMPLEX_TYPE);
5069 TREE_TYPE (complex_integer_type_node) = integer_type_node;
5070 layout_type (complex_integer_type_node);
5072 complex_float_type_node = make_node (COMPLEX_TYPE);
5073 TREE_TYPE (complex_float_type_node) = float_type_node;
5074 layout_type (complex_float_type_node);
5076 complex_double_type_node = make_node (COMPLEX_TYPE);
5077 TREE_TYPE (complex_double_type_node) = double_type_node;
5078 layout_type (complex_double_type_node);
5080 complex_long_double_type_node = make_node (COMPLEX_TYPE);
5081 TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
5082 layout_type (complex_long_double_type_node);
5085 tree t = (*targetm.build_builtin_va_list) ();
5087 /* Many back-ends define record types without setting TYPE_NAME.
5088 If we copied the record type here, we'd keep the original
5089 record type without a name. This breaks name mangling. So,
5090 don't copy record types and let c_common_nodes_and_builtins()
5091 declare the type to be __builtin_va_list. */
5092 if (TREE_CODE (t) != RECORD_TYPE)
5093 t = build_type_copy (t);
5095 va_list_type_node = t;
5098 unsigned_V4SI_type_node
5099 = make_vector (V4SImode, unsigned_intSI_type_node, 1);
5100 unsigned_V2HI_type_node
5101 = make_vector (V2HImode, unsigned_intHI_type_node, 1);
5102 unsigned_V2SI_type_node
5103 = make_vector (V2SImode, unsigned_intSI_type_node, 1);
5104 unsigned_V2DI_type_node
5105 = make_vector (V2DImode, unsigned_intDI_type_node, 1);
5106 unsigned_V4HI_type_node
5107 = make_vector (V4HImode, unsigned_intHI_type_node, 1);
5108 unsigned_V8QI_type_node
5109 = make_vector (V8QImode, unsigned_intQI_type_node, 1);
5110 unsigned_V8HI_type_node
5111 = make_vector (V8HImode, unsigned_intHI_type_node, 1);
5112 unsigned_V16QI_type_node
5113 = make_vector (V16QImode, unsigned_intQI_type_node, 1);
5114 unsigned_V1DI_type_node
5115 = make_vector (V1DImode, unsigned_intDI_type_node, 1);
5117 V16SF_type_node = make_vector (V16SFmode, float_type_node, 0);
5118 V4SF_type_node = make_vector (V4SFmode, float_type_node, 0);
5119 V4SI_type_node = make_vector (V4SImode, intSI_type_node, 0);
5120 V2HI_type_node = make_vector (V2HImode, intHI_type_node, 0);
5121 V2SI_type_node = make_vector (V2SImode, intSI_type_node, 0);
5122 V2DI_type_node = make_vector (V2DImode, intDI_type_node, 0);
5123 V4HI_type_node = make_vector (V4HImode, intHI_type_node, 0);
5124 V8QI_type_node = make_vector (V8QImode, intQI_type_node, 0);
5125 V8HI_type_node = make_vector (V8HImode, intHI_type_node, 0);
5126 V2SF_type_node = make_vector (V2SFmode, float_type_node, 0);
5127 V2DF_type_node = make_vector (V2DFmode, double_type_node, 0);
5128 V16QI_type_node = make_vector (V16QImode, intQI_type_node, 0);
5129 V1DI_type_node = make_vector (V1DImode, intDI_type_node, 0);
5130 V4DF_type_node = make_vector (V4DFmode, double_type_node, 0);
5133 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
5134 better way.
5136 If we requested a pointer to a vector, build up the pointers that
5137 we stripped off while looking for the inner type. Similarly for
5138 return values from functions.
5140 The argument TYPE is the top of the chain, and BOTTOM is the
5141 new type which we will point to. */
5143 tree
5144 reconstruct_complex_type (tree type, tree bottom)
5146 tree inner, outer;
5148 if (POINTER_TYPE_P (type))
5150 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
5151 outer = build_pointer_type (inner);
5153 else if (TREE_CODE (type) == ARRAY_TYPE)
5155 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
5156 outer = build_array_type (inner, TYPE_DOMAIN (type));
5158 else if (TREE_CODE (type) == FUNCTION_TYPE)
5160 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
5161 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
5163 else if (TREE_CODE (type) == METHOD_TYPE)
5165 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
5166 outer = build_method_type_directly (TYPE_METHOD_BASETYPE (type),
5167 inner,
5168 TYPE_ARG_TYPES (type));
5170 else
5171 return bottom;
5173 TREE_READONLY (outer) = TREE_READONLY (type);
5174 TREE_THIS_VOLATILE (outer) = TREE_THIS_VOLATILE (type);
5176 return outer;
5179 /* Returns a vector tree node given a vector mode, the inner type, and
5180 the signness. */
5182 tree
5183 make_vector (enum machine_mode mode, tree innertype, int unsignedp)
5185 tree t;
5187 t = make_node (VECTOR_TYPE);
5188 TREE_TYPE (t) = innertype;
5189 TYPE_MODE (t) = mode;
5190 TREE_UNSIGNED (TREE_TYPE (t)) = unsignedp;
5191 finish_vector_type (t);
5193 return t;
5196 /* Given an initializer INIT, return TRUE if INIT is zero or some
5197 aggregate of zeros. Otherwise return FALSE. */
5199 bool
5200 initializer_zerop (tree init)
5202 STRIP_NOPS (init);
5204 switch (TREE_CODE (init))
5206 case INTEGER_CST:
5207 return integer_zerop (init);
5208 case REAL_CST:
5209 return real_zerop (init)
5210 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
5211 case COMPLEX_CST:
5212 return integer_zerop (init)
5213 || (real_zerop (init)
5214 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
5215 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
5216 case CONSTRUCTOR:
5218 /* Set is empty if it has no elements. */
5219 if ((TREE_CODE (TREE_TYPE (init)) == SET_TYPE)
5220 && CONSTRUCTOR_ELTS (init))
5221 return false;
5223 if (AGGREGATE_TYPE_P (TREE_TYPE (init)))
5225 tree aggr_init = CONSTRUCTOR_ELTS (init);
5227 while (aggr_init)
5229 if (! initializer_zerop (TREE_VALUE (aggr_init)))
5230 return false;
5231 aggr_init = TREE_CHAIN (aggr_init);
5233 return true;
5235 return false;
5237 default:
5238 return false;
5242 #include "gt-tree.h"