* pretty-print.c (pp_base_maybe_space): New function.
[official-gcc.git] / gcc / tree.c
blob240d3357454e2219c8be61e04d0661abe2765101
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 *);
110 static unsigned int type_hash_list (tree, hashval_t);
111 static unsigned int attribute_hash_list (tree, hashval_t);
113 tree global_trees[TI_MAX];
114 tree integer_types[itk_none];
116 /* Init tree.c. */
118 void
119 init_ttree (void)
121 /* Initialize the hash table of types. */
122 type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
123 type_hash_eq, 0);
127 /* The name of the object as the assembler will see it (but before any
128 translations made by ASM_OUTPUT_LABELREF). Often this is the same
129 as DECL_NAME. It is an IDENTIFIER_NODE. */
130 tree
131 decl_assembler_name (tree decl)
133 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
134 lang_hooks.set_decl_assembler_name (decl);
135 return DECL_CHECK (decl)->decl.assembler_name;
138 /* Compute the number of bytes occupied by 'node'. This routine only
139 looks at TREE_CODE and, if the code is TREE_VEC, TREE_VEC_LENGTH. */
140 size_t
141 tree_size (tree node)
143 enum tree_code code = TREE_CODE (node);
145 switch (TREE_CODE_CLASS (code))
147 case 'd': /* A decl node */
148 return sizeof (struct tree_decl);
150 case 't': /* a type node */
151 return sizeof (struct tree_type);
153 case 'b': /* a lexical block node */
154 return sizeof (struct tree_block);
156 case 'r': /* a reference */
157 case 'e': /* an expression */
158 case 's': /* an expression with side effects */
159 case '<': /* a comparison expression */
160 case '1': /* a unary arithmetic expression */
161 case '2': /* a binary arithmetic expression */
162 return (sizeof (struct tree_exp)
163 + TREE_CODE_LENGTH (code) * sizeof (char *) - sizeof (char *));
165 case 'c': /* a constant */
166 switch (code)
168 case INTEGER_CST: return sizeof (struct tree_int_cst);
169 case REAL_CST: return sizeof (struct tree_real_cst);
170 case COMPLEX_CST: return sizeof (struct tree_complex);
171 case VECTOR_CST: return sizeof (struct tree_vector);
172 case STRING_CST: return sizeof (struct tree_string);
173 default:
174 return lang_hooks.tree_size (code);
177 case 'x': /* something random, like an identifier. */
178 switch (code)
180 case IDENTIFIER_NODE: return lang_hooks.identifier_size;
181 case TREE_LIST: return sizeof (struct tree_list);
182 case TREE_VEC: return (sizeof (struct tree_vec)
183 + TREE_VEC_LENGTH(node) * sizeof(char *)
184 - sizeof (char *));
186 case ERROR_MARK:
187 case PLACEHOLDER_EXPR: return sizeof (struct tree_common);
189 default:
190 return lang_hooks.tree_size (code);
193 default:
194 abort ();
198 /* Return a newly allocated node of code CODE.
199 For decl and type nodes, some other fields are initialized.
200 The rest of the node is initialized to zero.
202 Achoo! I got a code in the node. */
204 tree
205 make_node_stat (enum tree_code code MEM_STAT_DECL)
207 tree t;
208 int type = TREE_CODE_CLASS (code);
209 size_t length;
210 #ifdef GATHER_STATISTICS
211 tree_node_kind kind;
212 #endif
213 struct tree_common ttmp;
215 /* We can't allocate a TREE_VEC without knowing how many elements
216 it will have. */
217 if (code == TREE_VEC)
218 abort ();
220 TREE_SET_CODE ((tree)&ttmp, code);
221 length = tree_size ((tree)&ttmp);
223 #ifdef GATHER_STATISTICS
224 switch (type)
226 case 'd': /* A decl node */
227 kind = d_kind;
228 break;
230 case 't': /* a type node */
231 kind = t_kind;
232 break;
234 case 'b': /* a lexical block */
235 kind = b_kind;
236 break;
238 case 's': /* an expression with side effects */
239 kind = s_kind;
240 break;
242 case 'r': /* a reference */
243 kind = r_kind;
244 break;
246 case 'e': /* an expression */
247 case '<': /* a comparison expression */
248 case '1': /* a unary arithmetic expression */
249 case '2': /* a binary arithmetic expression */
250 kind = e_kind;
251 break;
253 case 'c': /* a constant */
254 kind = c_kind;
255 break;
257 case 'x': /* something random, like an identifier. */
258 if (code == IDENTIFIER_NODE)
259 kind = id_kind;
260 else if (code == TREE_VEC)
261 kind = vec_kind;
262 else
263 kind = x_kind;
264 break;
266 default:
267 abort ();
270 tree_node_counts[(int) kind]++;
271 tree_node_sizes[(int) kind] += length;
272 #endif
274 t = ggc_alloc_zone_stat (length, tree_zone PASS_MEM_STAT);
276 memset (t, 0, length);
278 TREE_SET_CODE (t, code);
280 switch (type)
282 case 's':
283 TREE_SIDE_EFFECTS (t) = 1;
284 break;
286 case 'd':
287 if (code != FUNCTION_DECL)
288 DECL_ALIGN (t) = 1;
289 DECL_USER_ALIGN (t) = 0;
290 DECL_IN_SYSTEM_HEADER (t) = in_system_header;
291 DECL_SOURCE_LOCATION (t) = input_location;
292 DECL_UID (t) = next_decl_uid++;
294 /* We have not yet computed the alias set for this declaration. */
295 DECL_POINTER_ALIAS_SET (t) = -1;
296 break;
298 case 't':
299 TYPE_UID (t) = next_type_uid++;
300 TYPE_ALIGN (t) = char_type_node ? TYPE_ALIGN (char_type_node) : 0;
301 TYPE_USER_ALIGN (t) = 0;
302 TYPE_MAIN_VARIANT (t) = t;
304 /* Default to no attributes for type, but let target change that. */
305 TYPE_ATTRIBUTES (t) = NULL_TREE;
306 targetm.set_default_type_attributes (t);
308 /* We have not yet computed the alias set for this type. */
309 TYPE_ALIAS_SET (t) = -1;
310 break;
312 case 'c':
313 TREE_CONSTANT (t) = 1;
314 break;
316 case 'e':
317 switch (code)
319 case INIT_EXPR:
320 case MODIFY_EXPR:
321 case VA_ARG_EXPR:
322 case RTL_EXPR:
323 case PREDECREMENT_EXPR:
324 case PREINCREMENT_EXPR:
325 case POSTDECREMENT_EXPR:
326 case POSTINCREMENT_EXPR:
327 /* All of these have side-effects, no matter what their
328 operands are. */
329 TREE_SIDE_EFFECTS (t) = 1;
330 break;
332 default:
333 break;
335 break;
338 return t;
341 /* Return a new node with the same contents as NODE except that its
342 TREE_CHAIN is zero and it has a fresh uid. */
344 tree
345 copy_node_stat (tree node MEM_STAT_DECL)
347 tree t;
348 enum tree_code code = TREE_CODE (node);
349 size_t length;
351 length = tree_size (node);
352 t = ggc_alloc_zone_stat (length, tree_zone PASS_MEM_STAT);
353 memcpy (t, node, length);
355 TREE_CHAIN (t) = 0;
356 TREE_ASM_WRITTEN (t) = 0;
358 if (TREE_CODE_CLASS (code) == 'd')
359 DECL_UID (t) = next_decl_uid++;
360 else if (TREE_CODE_CLASS (code) == 't')
362 TYPE_UID (t) = next_type_uid++;
363 /* The following is so that the debug code for
364 the copy is different from the original type.
365 The two statements usually duplicate each other
366 (because they clear fields of the same union),
367 but the optimizer should catch that. */
368 TYPE_SYMTAB_POINTER (t) = 0;
369 TYPE_SYMTAB_ADDRESS (t) = 0;
372 return t;
375 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
376 For example, this can copy a list made of TREE_LIST nodes. */
378 tree
379 copy_list (tree list)
381 tree head;
382 tree prev, next;
384 if (list == 0)
385 return 0;
387 head = prev = copy_node (list);
388 next = TREE_CHAIN (list);
389 while (next)
391 TREE_CHAIN (prev) = copy_node (next);
392 prev = TREE_CHAIN (prev);
393 next = TREE_CHAIN (next);
395 return head;
399 /* Return a newly constructed INTEGER_CST node whose constant value
400 is specified by the two ints LOW and HI.
401 The TREE_TYPE is set to `int'.
403 This function should be used via the `build_int_2' macro. */
405 tree
406 build_int_2_wide (unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
408 tree t = make_node (INTEGER_CST);
410 TREE_INT_CST_LOW (t) = low;
411 TREE_INT_CST_HIGH (t) = hi;
412 TREE_TYPE (t) = integer_type_node;
413 return t;
416 /* Return a new VECTOR_CST node whose type is TYPE and whose values
417 are in a list pointed by VALS. */
419 tree
420 build_vector (tree type, tree vals)
422 tree v = make_node (VECTOR_CST);
423 int over1 = 0, over2 = 0;
424 tree link;
426 TREE_VECTOR_CST_ELTS (v) = vals;
427 TREE_TYPE (v) = type;
429 /* Iterate through elements and check for overflow. */
430 for (link = vals; link; link = TREE_CHAIN (link))
432 tree value = TREE_VALUE (link);
434 over1 |= TREE_OVERFLOW (value);
435 over2 |= TREE_CONSTANT_OVERFLOW (value);
438 TREE_OVERFLOW (v) = over1;
439 TREE_CONSTANT_OVERFLOW (v) = over2;
441 return v;
444 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
445 are in a list pointed to by VALS. */
446 tree
447 build_constructor (tree type, tree vals)
449 tree c = make_node (CONSTRUCTOR);
450 TREE_TYPE (c) = type;
451 CONSTRUCTOR_ELTS (c) = vals;
453 /* ??? May not be necessary. Mirrors what build does. */
454 if (vals)
456 TREE_SIDE_EFFECTS (c) = TREE_SIDE_EFFECTS (vals);
457 TREE_READONLY (c) = TREE_READONLY (vals);
458 TREE_CONSTANT (c) = TREE_CONSTANT (vals);
460 else
461 TREE_CONSTANT (c) = 0; /* safe side */
463 return c;
466 /* Return a new REAL_CST node whose type is TYPE and value is D. */
468 tree
469 build_real (tree type, REAL_VALUE_TYPE d)
471 tree v;
472 REAL_VALUE_TYPE *dp;
473 int overflow = 0;
475 /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
476 Consider doing it via real_convert now. */
478 v = make_node (REAL_CST);
479 dp = ggc_alloc (sizeof (REAL_VALUE_TYPE));
480 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
482 TREE_TYPE (v) = type;
483 TREE_REAL_CST_PTR (v) = dp;
484 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
485 return v;
488 /* Return a new REAL_CST node whose type is TYPE
489 and whose value is the integer value of the INTEGER_CST node I. */
491 REAL_VALUE_TYPE
492 real_value_from_int_cst (tree type, tree i)
494 REAL_VALUE_TYPE d;
496 /* Clear all bits of the real value type so that we can later do
497 bitwise comparisons to see if two values are the same. */
498 memset (&d, 0, sizeof d);
500 real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
501 TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
502 TREE_UNSIGNED (TREE_TYPE (i)));
503 return d;
506 /* Given a tree representing an integer constant I, return a tree
507 representing the same value as a floating-point constant of type TYPE. */
509 tree
510 build_real_from_int_cst (tree type, tree i)
512 tree v;
513 int overflow = TREE_OVERFLOW (i);
515 v = build_real (type, real_value_from_int_cst (type, i));
517 TREE_OVERFLOW (v) |= overflow;
518 TREE_CONSTANT_OVERFLOW (v) |= overflow;
519 return v;
522 /* Return a newly constructed STRING_CST node whose value is
523 the LEN characters at STR.
524 The TREE_TYPE is not initialized. */
526 tree
527 build_string (int len, const char *str)
529 tree s = make_node (STRING_CST);
531 TREE_STRING_LENGTH (s) = len;
532 TREE_STRING_POINTER (s) = ggc_alloc_string (str, len);
534 return s;
537 /* Return a newly constructed COMPLEX_CST node whose value is
538 specified by the real and imaginary parts REAL and IMAG.
539 Both REAL and IMAG should be constant nodes. TYPE, if specified,
540 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
542 tree
543 build_complex (tree type, tree real, tree imag)
545 tree t = make_node (COMPLEX_CST);
547 TREE_REALPART (t) = real;
548 TREE_IMAGPART (t) = imag;
549 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
550 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
551 TREE_CONSTANT_OVERFLOW (t)
552 = TREE_CONSTANT_OVERFLOW (real) | TREE_CONSTANT_OVERFLOW (imag);
553 return t;
556 /* Build a newly constructed TREE_VEC node of length LEN. */
558 tree
559 make_tree_vec_stat (int len MEM_STAT_DECL)
561 tree t;
562 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
564 #ifdef GATHER_STATISTICS
565 tree_node_counts[(int) vec_kind]++;
566 tree_node_sizes[(int) vec_kind] += length;
567 #endif
569 t = ggc_alloc_zone_stat (length, tree_zone PASS_MEM_STAT);
571 memset (t, 0, length);
573 TREE_SET_CODE (t, TREE_VEC);
574 TREE_VEC_LENGTH (t) = len;
576 return t;
579 /* Return 1 if EXPR is the integer constant zero or a complex constant
580 of zero. */
583 integer_zerop (tree expr)
585 STRIP_NOPS (expr);
587 return ((TREE_CODE (expr) == INTEGER_CST
588 && ! TREE_CONSTANT_OVERFLOW (expr)
589 && TREE_INT_CST_LOW (expr) == 0
590 && TREE_INT_CST_HIGH (expr) == 0)
591 || (TREE_CODE (expr) == COMPLEX_CST
592 && integer_zerop (TREE_REALPART (expr))
593 && integer_zerop (TREE_IMAGPART (expr))));
596 /* Return 1 if EXPR is the integer constant one or the corresponding
597 complex constant. */
600 integer_onep (tree expr)
602 STRIP_NOPS (expr);
604 return ((TREE_CODE (expr) == INTEGER_CST
605 && ! TREE_CONSTANT_OVERFLOW (expr)
606 && TREE_INT_CST_LOW (expr) == 1
607 && TREE_INT_CST_HIGH (expr) == 0)
608 || (TREE_CODE (expr) == COMPLEX_CST
609 && integer_onep (TREE_REALPART (expr))
610 && integer_zerop (TREE_IMAGPART (expr))));
613 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
614 it contains. Likewise for the corresponding complex constant. */
617 integer_all_onesp (tree expr)
619 int prec;
620 int uns;
622 STRIP_NOPS (expr);
624 if (TREE_CODE (expr) == COMPLEX_CST
625 && integer_all_onesp (TREE_REALPART (expr))
626 && integer_zerop (TREE_IMAGPART (expr)))
627 return 1;
629 else if (TREE_CODE (expr) != INTEGER_CST
630 || TREE_CONSTANT_OVERFLOW (expr))
631 return 0;
633 uns = TREE_UNSIGNED (TREE_TYPE (expr));
634 if (!uns)
635 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
636 && TREE_INT_CST_HIGH (expr) == -1);
638 /* Note that using TYPE_PRECISION here is wrong. We care about the
639 actual bits, not the (arbitrary) range of the type. */
640 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)));
641 if (prec >= HOST_BITS_PER_WIDE_INT)
643 HOST_WIDE_INT high_value;
644 int shift_amount;
646 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
648 if (shift_amount > HOST_BITS_PER_WIDE_INT)
649 /* Can not handle precisions greater than twice the host int size. */
650 abort ();
651 else if (shift_amount == HOST_BITS_PER_WIDE_INT)
652 /* Shifting by the host word size is undefined according to the ANSI
653 standard, so we must handle this as a special case. */
654 high_value = -1;
655 else
656 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
658 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
659 && TREE_INT_CST_HIGH (expr) == high_value);
661 else
662 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
665 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
666 one bit on). */
669 integer_pow2p (tree expr)
671 int prec;
672 HOST_WIDE_INT high, low;
674 STRIP_NOPS (expr);
676 if (TREE_CODE (expr) == COMPLEX_CST
677 && integer_pow2p (TREE_REALPART (expr))
678 && integer_zerop (TREE_IMAGPART (expr)))
679 return 1;
681 if (TREE_CODE (expr) != INTEGER_CST || TREE_CONSTANT_OVERFLOW (expr))
682 return 0;
684 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
685 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
686 high = TREE_INT_CST_HIGH (expr);
687 low = TREE_INT_CST_LOW (expr);
689 /* First clear all bits that are beyond the type's precision in case
690 we've been sign extended. */
692 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
694 else if (prec > HOST_BITS_PER_WIDE_INT)
695 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
696 else
698 high = 0;
699 if (prec < HOST_BITS_PER_WIDE_INT)
700 low &= ~((HOST_WIDE_INT) (-1) << prec);
703 if (high == 0 && low == 0)
704 return 0;
706 return ((high == 0 && (low & (low - 1)) == 0)
707 || (low == 0 && (high & (high - 1)) == 0));
710 /* Return 1 if EXPR is an integer constant other than zero or a
711 complex constant other than zero. */
714 integer_nonzerop (tree expr)
716 STRIP_NOPS (expr);
718 return ((TREE_CODE (expr) == INTEGER_CST
719 && ! TREE_CONSTANT_OVERFLOW (expr)
720 && (TREE_INT_CST_LOW (expr) != 0
721 || TREE_INT_CST_HIGH (expr) != 0))
722 || (TREE_CODE (expr) == COMPLEX_CST
723 && (integer_nonzerop (TREE_REALPART (expr))
724 || integer_nonzerop (TREE_IMAGPART (expr)))));
727 /* Return the power of two represented by a tree node known to be a
728 power of two. */
731 tree_log2 (tree expr)
733 int prec;
734 HOST_WIDE_INT high, low;
736 STRIP_NOPS (expr);
738 if (TREE_CODE (expr) == COMPLEX_CST)
739 return tree_log2 (TREE_REALPART (expr));
741 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
742 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
744 high = TREE_INT_CST_HIGH (expr);
745 low = TREE_INT_CST_LOW (expr);
747 /* First clear all bits that are beyond the type's precision in case
748 we've been sign extended. */
750 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
752 else if (prec > HOST_BITS_PER_WIDE_INT)
753 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
754 else
756 high = 0;
757 if (prec < HOST_BITS_PER_WIDE_INT)
758 low &= ~((HOST_WIDE_INT) (-1) << prec);
761 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
762 : exact_log2 (low));
765 /* Similar, but return the largest integer Y such that 2 ** Y is less
766 than or equal to EXPR. */
769 tree_floor_log2 (tree expr)
771 int prec;
772 HOST_WIDE_INT high, low;
774 STRIP_NOPS (expr);
776 if (TREE_CODE (expr) == COMPLEX_CST)
777 return tree_log2 (TREE_REALPART (expr));
779 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
780 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
782 high = TREE_INT_CST_HIGH (expr);
783 low = TREE_INT_CST_LOW (expr);
785 /* First clear all bits that are beyond the type's precision in case
786 we've been sign extended. Ignore if type's precision hasn't been set
787 since what we are doing is setting it. */
789 if (prec == 2 * HOST_BITS_PER_WIDE_INT || prec == 0)
791 else if (prec > HOST_BITS_PER_WIDE_INT)
792 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
793 else
795 high = 0;
796 if (prec < HOST_BITS_PER_WIDE_INT)
797 low &= ~((HOST_WIDE_INT) (-1) << prec);
800 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
801 : floor_log2 (low));
804 /* Return 1 if EXPR is the real constant zero. */
807 real_zerop (tree expr)
809 STRIP_NOPS (expr);
811 return ((TREE_CODE (expr) == REAL_CST
812 && ! TREE_CONSTANT_OVERFLOW (expr)
813 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0))
814 || (TREE_CODE (expr) == COMPLEX_CST
815 && real_zerop (TREE_REALPART (expr))
816 && real_zerop (TREE_IMAGPART (expr))));
819 /* Return 1 if EXPR is the real constant one in real or complex form. */
822 real_onep (tree expr)
824 STRIP_NOPS (expr);
826 return ((TREE_CODE (expr) == REAL_CST
827 && ! TREE_CONSTANT_OVERFLOW (expr)
828 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1))
829 || (TREE_CODE (expr) == COMPLEX_CST
830 && real_onep (TREE_REALPART (expr))
831 && real_zerop (TREE_IMAGPART (expr))));
834 /* Return 1 if EXPR is the real constant two. */
837 real_twop (tree expr)
839 STRIP_NOPS (expr);
841 return ((TREE_CODE (expr) == REAL_CST
842 && ! TREE_CONSTANT_OVERFLOW (expr)
843 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2))
844 || (TREE_CODE (expr) == COMPLEX_CST
845 && real_twop (TREE_REALPART (expr))
846 && real_zerop (TREE_IMAGPART (expr))));
849 /* Return 1 if EXPR is the real constant minus one. */
852 real_minus_onep (tree expr)
854 STRIP_NOPS (expr);
856 return ((TREE_CODE (expr) == REAL_CST
857 && ! TREE_CONSTANT_OVERFLOW (expr)
858 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1))
859 || (TREE_CODE (expr) == COMPLEX_CST
860 && real_minus_onep (TREE_REALPART (expr))
861 && real_zerop (TREE_IMAGPART (expr))));
864 /* Nonzero if EXP is a constant or a cast of a constant. */
867 really_constant_p (tree exp)
869 /* This is not quite the same as STRIP_NOPS. It does more. */
870 while (TREE_CODE (exp) == NOP_EXPR
871 || TREE_CODE (exp) == CONVERT_EXPR
872 || TREE_CODE (exp) == NON_LVALUE_EXPR)
873 exp = TREE_OPERAND (exp, 0);
874 return TREE_CONSTANT (exp);
877 /* Return first list element whose TREE_VALUE is ELEM.
878 Return 0 if ELEM is not in LIST. */
880 tree
881 value_member (tree elem, tree list)
883 while (list)
885 if (elem == TREE_VALUE (list))
886 return list;
887 list = TREE_CHAIN (list);
889 return NULL_TREE;
892 /* Return first list element whose TREE_PURPOSE is ELEM.
893 Return 0 if ELEM is not in LIST. */
895 tree
896 purpose_member (tree elem, tree list)
898 while (list)
900 if (elem == TREE_PURPOSE (list))
901 return list;
902 list = TREE_CHAIN (list);
904 return NULL_TREE;
907 /* Return first list element whose BINFO_TYPE is ELEM.
908 Return 0 if ELEM is not in LIST. */
910 tree
911 binfo_member (tree elem, tree list)
913 while (list)
915 if (elem == BINFO_TYPE (list))
916 return list;
917 list = TREE_CHAIN (list);
919 return NULL_TREE;
922 /* Return nonzero if ELEM is part of the chain CHAIN. */
925 chain_member (tree elem, tree chain)
927 while (chain)
929 if (elem == chain)
930 return 1;
931 chain = TREE_CHAIN (chain);
934 return 0;
937 /* Return the length of a chain of nodes chained through TREE_CHAIN.
938 We expect a null pointer to mark the end of the chain.
939 This is the Lisp primitive `length'. */
942 list_length (tree t)
944 tree tail;
945 int len = 0;
947 for (tail = t; tail; tail = TREE_CHAIN (tail))
948 len++;
950 return len;
953 /* Returns the number of FIELD_DECLs in TYPE. */
956 fields_length (tree type)
958 tree t = TYPE_FIELDS (type);
959 int count = 0;
961 for (; t; t = TREE_CHAIN (t))
962 if (TREE_CODE (t) == FIELD_DECL)
963 ++count;
965 return count;
968 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
969 by modifying the last node in chain 1 to point to chain 2.
970 This is the Lisp primitive `nconc'. */
972 tree
973 chainon (tree op1, tree op2)
975 tree t1;
977 if (!op1)
978 return op2;
979 if (!op2)
980 return op1;
982 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
983 continue;
984 TREE_CHAIN (t1) = op2;
986 #ifdef ENABLE_TREE_CHECKING
988 tree t2;
989 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
990 if (t2 == t1)
991 abort (); /* Circularity created. */
993 #endif
995 return op1;
998 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
1000 tree
1001 tree_last (tree chain)
1003 tree next;
1004 if (chain)
1005 while ((next = TREE_CHAIN (chain)))
1006 chain = next;
1007 return chain;
1010 /* Reverse the order of elements in the chain T,
1011 and return the new head of the chain (old last element). */
1013 tree
1014 nreverse (tree t)
1016 tree prev = 0, decl, next;
1017 for (decl = t; decl; decl = next)
1019 next = TREE_CHAIN (decl);
1020 TREE_CHAIN (decl) = prev;
1021 prev = decl;
1023 return prev;
1026 /* Return a newly created TREE_LIST node whose
1027 purpose and value fields are PARM and VALUE. */
1029 tree
1030 build_tree_list_stat (tree parm, tree value MEM_STAT_DECL)
1032 tree t = make_node_stat (TREE_LIST PASS_MEM_STAT);
1033 TREE_PURPOSE (t) = parm;
1034 TREE_VALUE (t) = value;
1035 return t;
1038 /* Return a newly created TREE_LIST node whose
1039 purpose and value fields are PURPOSE and VALUE
1040 and whose TREE_CHAIN is CHAIN. */
1042 tree
1043 tree_cons_stat (tree purpose, tree value, tree chain MEM_STAT_DECL)
1045 tree node;
1047 node = ggc_alloc_zone_stat (sizeof (struct tree_list),
1048 tree_zone PASS_MEM_STAT);
1050 memset (node, 0, sizeof (struct tree_common));
1052 #ifdef GATHER_STATISTICS
1053 tree_node_counts[(int) x_kind]++;
1054 tree_node_sizes[(int) x_kind] += sizeof (struct tree_list);
1055 #endif
1057 TREE_SET_CODE (node, TREE_LIST);
1058 TREE_CHAIN (node) = chain;
1059 TREE_PURPOSE (node) = purpose;
1060 TREE_VALUE (node) = value;
1061 return node;
1064 /* Return the first expression in a sequence of COMPOUND_EXPRs. */
1066 tree
1067 expr_first (tree expr)
1069 if (expr == NULL_TREE)
1070 return expr;
1071 while (TREE_CODE (expr) == COMPOUND_EXPR)
1072 expr = TREE_OPERAND (expr, 0);
1073 return expr;
1076 /* Return the last expression in a sequence of COMPOUND_EXPRs. */
1078 tree
1079 expr_last (tree expr)
1081 if (expr == NULL_TREE)
1082 return expr;
1083 while (TREE_CODE (expr) == COMPOUND_EXPR)
1084 expr = TREE_OPERAND (expr, 1);
1085 return expr;
1088 /* Return the number of subexpressions in a sequence of COMPOUND_EXPRs. */
1091 expr_length (tree expr)
1093 int len = 0;
1095 if (expr == NULL_TREE)
1096 return 0;
1097 for (; TREE_CODE (expr) == COMPOUND_EXPR; expr = TREE_OPERAND (expr, 1))
1098 len += expr_length (TREE_OPERAND (expr, 0));
1099 ++len;
1100 return len;
1103 /* Return the size nominally occupied by an object of type TYPE
1104 when it resides in memory. The value is measured in units of bytes,
1105 and its data type is that normally used for type sizes
1106 (which is the first type created by make_signed_type or
1107 make_unsigned_type). */
1109 tree
1110 size_in_bytes (tree type)
1112 tree t;
1114 if (type == error_mark_node)
1115 return integer_zero_node;
1117 type = TYPE_MAIN_VARIANT (type);
1118 t = TYPE_SIZE_UNIT (type);
1120 if (t == 0)
1122 lang_hooks.types.incomplete_type_error (NULL_TREE, type);
1123 return size_zero_node;
1126 if (TREE_CODE (t) == INTEGER_CST)
1127 force_fit_type (t, 0);
1129 return t;
1132 /* Return the size of TYPE (in bytes) as a wide integer
1133 or return -1 if the size can vary or is larger than an integer. */
1135 HOST_WIDE_INT
1136 int_size_in_bytes (tree type)
1138 tree t;
1140 if (type == error_mark_node)
1141 return 0;
1143 type = TYPE_MAIN_VARIANT (type);
1144 t = TYPE_SIZE_UNIT (type);
1145 if (t == 0
1146 || TREE_CODE (t) != INTEGER_CST
1147 || TREE_OVERFLOW (t)
1148 || TREE_INT_CST_HIGH (t) != 0
1149 /* If the result would appear negative, it's too big to represent. */
1150 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
1151 return -1;
1153 return TREE_INT_CST_LOW (t);
1156 /* Return the bit position of FIELD, in bits from the start of the record.
1157 This is a tree of type bitsizetype. */
1159 tree
1160 bit_position (tree field)
1162 return bit_from_pos (DECL_FIELD_OFFSET (field),
1163 DECL_FIELD_BIT_OFFSET (field));
1166 /* Likewise, but return as an integer. Abort if it cannot be represented
1167 in that way (since it could be a signed value, we don't have the option
1168 of returning -1 like int_size_in_byte can. */
1170 HOST_WIDE_INT
1171 int_bit_position (tree field)
1173 return tree_low_cst (bit_position (field), 0);
1176 /* Return the byte position of FIELD, in bytes from the start of the record.
1177 This is a tree of type sizetype. */
1179 tree
1180 byte_position (tree field)
1182 return byte_from_pos (DECL_FIELD_OFFSET (field),
1183 DECL_FIELD_BIT_OFFSET (field));
1186 /* Likewise, but return as an integer. Abort if it cannot be represented
1187 in that way (since it could be a signed value, we don't have the option
1188 of returning -1 like int_size_in_byte can. */
1190 HOST_WIDE_INT
1191 int_byte_position (tree field)
1193 return tree_low_cst (byte_position (field), 0);
1196 /* Return the strictest alignment, in bits, that T is known to have. */
1198 unsigned int
1199 expr_align (tree t)
1201 unsigned int align0, align1;
1203 switch (TREE_CODE (t))
1205 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
1206 /* If we have conversions, we know that the alignment of the
1207 object must meet each of the alignments of the types. */
1208 align0 = expr_align (TREE_OPERAND (t, 0));
1209 align1 = TYPE_ALIGN (TREE_TYPE (t));
1210 return MAX (align0, align1);
1212 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
1213 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
1214 case CLEANUP_POINT_EXPR: case UNSAVE_EXPR:
1215 /* These don't change the alignment of an object. */
1216 return expr_align (TREE_OPERAND (t, 0));
1218 case COND_EXPR:
1219 /* The best we can do is say that the alignment is the least aligned
1220 of the two arms. */
1221 align0 = expr_align (TREE_OPERAND (t, 1));
1222 align1 = expr_align (TREE_OPERAND (t, 2));
1223 return MIN (align0, align1);
1225 case LABEL_DECL: case CONST_DECL:
1226 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
1227 if (DECL_ALIGN (t) != 0)
1228 return DECL_ALIGN (t);
1229 break;
1231 case FUNCTION_DECL:
1232 return FUNCTION_BOUNDARY;
1234 default:
1235 break;
1238 /* Otherwise take the alignment from that of the type. */
1239 return TYPE_ALIGN (TREE_TYPE (t));
1242 /* Return, as a tree node, the number of elements for TYPE (which is an
1243 ARRAY_TYPE) minus one. This counts only elements of the top array. */
1245 tree
1246 array_type_nelts (tree type)
1248 tree index_type, min, max;
1250 /* If they did it with unspecified bounds, then we should have already
1251 given an error about it before we got here. */
1252 if (! TYPE_DOMAIN (type))
1253 return error_mark_node;
1255 index_type = TYPE_DOMAIN (type);
1256 min = TYPE_MIN_VALUE (index_type);
1257 max = TYPE_MAX_VALUE (index_type);
1259 return (integer_zerop (min)
1260 ? max
1261 : fold (build (MINUS_EXPR, TREE_TYPE (max), max, min)));
1264 /* Return nonzero if arg is static -- a reference to an object in
1265 static storage. This is not the same as the C meaning of `static'. */
1268 staticp (tree arg)
1270 switch (TREE_CODE (arg))
1272 case FUNCTION_DECL:
1273 /* Nested functions aren't static, since taking their address
1274 involves a trampoline. */
1275 return ((decl_function_context (arg) == 0 || DECL_NO_STATIC_CHAIN (arg))
1276 && ! DECL_NON_ADDR_CONST_P (arg));
1278 case VAR_DECL:
1279 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
1280 && ! DECL_THREAD_LOCAL (arg)
1281 && ! DECL_NON_ADDR_CONST_P (arg));
1283 case CONSTRUCTOR:
1284 return TREE_STATIC (arg);
1286 case LABEL_DECL:
1287 case STRING_CST:
1288 return 1;
1290 /* If we are referencing a bitfield, we can't evaluate an
1291 ADDR_EXPR at compile time and so it isn't a constant. */
1292 case COMPONENT_REF:
1293 return (! DECL_BIT_FIELD (TREE_OPERAND (arg, 1))
1294 && staticp (TREE_OPERAND (arg, 0)));
1296 case BIT_FIELD_REF:
1297 return 0;
1299 #if 0
1300 /* This case is technically correct, but results in setting
1301 TREE_CONSTANT on ADDR_EXPRs that cannot be evaluated at
1302 compile time. */
1303 case INDIRECT_REF:
1304 return TREE_CONSTANT (TREE_OPERAND (arg, 0));
1305 #endif
1307 case ARRAY_REF:
1308 case ARRAY_RANGE_REF:
1309 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
1310 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
1311 return staticp (TREE_OPERAND (arg, 0));
1313 default:
1314 if ((unsigned int) TREE_CODE (arg)
1315 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
1316 return lang_hooks.staticp (arg);
1317 else
1318 return 0;
1322 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
1323 Do this to any expression which may be used in more than one place,
1324 but must be evaluated only once.
1326 Normally, expand_expr would reevaluate the expression each time.
1327 Calling save_expr produces something that is evaluated and recorded
1328 the first time expand_expr is called on it. Subsequent calls to
1329 expand_expr just reuse the recorded value.
1331 The call to expand_expr that generates code that actually computes
1332 the value is the first call *at compile time*. Subsequent calls
1333 *at compile time* generate code to use the saved value.
1334 This produces correct result provided that *at run time* control
1335 always flows through the insns made by the first expand_expr
1336 before reaching the other places where the save_expr was evaluated.
1337 You, the caller of save_expr, must make sure this is so.
1339 Constants, and certain read-only nodes, are returned with no
1340 SAVE_EXPR because that is safe. Expressions containing placeholders
1341 are not touched; see tree.def for an explanation of what these
1342 are used for. */
1344 tree
1345 save_expr (tree expr)
1347 tree t = fold (expr);
1348 tree inner;
1350 /* If the tree evaluates to a constant, then we don't want to hide that
1351 fact (i.e. this allows further folding, and direct checks for constants).
1352 However, a read-only object that has side effects cannot be bypassed.
1353 Since it is no problem to reevaluate literals, we just return the
1354 literal node. */
1355 inner = skip_simple_arithmetic (t);
1356 if (TREE_CONSTANT (inner)
1357 || (TREE_READONLY (inner) && ! TREE_SIDE_EFFECTS (inner))
1358 || TREE_CODE (inner) == SAVE_EXPR
1359 || TREE_CODE (inner) == ERROR_MARK)
1360 return t;
1362 /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
1363 it means that the size or offset of some field of an object depends on
1364 the value within another field.
1366 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
1367 and some variable since it would then need to be both evaluated once and
1368 evaluated more than once. Front-ends must assure this case cannot
1369 happen by surrounding any such subexpressions in their own SAVE_EXPR
1370 and forcing evaluation at the proper time. */
1371 if (contains_placeholder_p (inner))
1372 return t;
1374 t = build (SAVE_EXPR, TREE_TYPE (expr), t, current_function_decl, NULL_TREE);
1376 /* This expression might be placed ahead of a jump to ensure that the
1377 value was computed on both sides of the jump. So make sure it isn't
1378 eliminated as dead. */
1379 TREE_SIDE_EFFECTS (t) = 1;
1380 TREE_READONLY (t) = 1;
1381 return t;
1384 /* Look inside EXPR and into any simple arithmetic operations. Return
1385 the innermost non-arithmetic node. */
1387 tree
1388 skip_simple_arithmetic (tree expr)
1390 tree inner;
1392 /* We don't care about whether this can be used as an lvalue in this
1393 context. */
1394 while (TREE_CODE (expr) == NON_LVALUE_EXPR)
1395 expr = TREE_OPERAND (expr, 0);
1397 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
1398 a constant, it will be more efficient to not make another SAVE_EXPR since
1399 it will allow better simplification and GCSE will be able to merge the
1400 computations if they actually occur. */
1401 inner = expr;
1402 while (1)
1404 if (TREE_CODE_CLASS (TREE_CODE (inner)) == '1')
1405 inner = TREE_OPERAND (inner, 0);
1406 else if (TREE_CODE_CLASS (TREE_CODE (inner)) == '2')
1408 if (TREE_CONSTANT (TREE_OPERAND (inner, 1)))
1409 inner = TREE_OPERAND (inner, 0);
1410 else if (TREE_CONSTANT (TREE_OPERAND (inner, 0)))
1411 inner = TREE_OPERAND (inner, 1);
1412 else
1413 break;
1415 else
1416 break;
1419 return inner;
1422 /* Return TRUE if EXPR is a SAVE_EXPR or wraps simple arithmetic around a
1423 SAVE_EXPR. Return FALSE otherwise. */
1425 bool
1426 saved_expr_p (tree expr)
1428 return TREE_CODE (skip_simple_arithmetic (expr)) == SAVE_EXPR;
1431 /* Arrange for an expression to be expanded multiple independent
1432 times. This is useful for cleanup actions, as the backend can
1433 expand them multiple times in different places. */
1435 tree
1436 unsave_expr (tree expr)
1438 tree t;
1440 /* If this is already protected, no sense in protecting it again. */
1441 if (TREE_CODE (expr) == UNSAVE_EXPR)
1442 return expr;
1444 t = build1 (UNSAVE_EXPR, TREE_TYPE (expr), expr);
1445 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (expr);
1446 return t;
1449 /* Returns the index of the first non-tree operand for CODE, or the number
1450 of operands if all are trees. */
1453 first_rtl_op (enum tree_code code)
1455 switch (code)
1457 case SAVE_EXPR:
1458 return 2;
1459 case GOTO_SUBROUTINE_EXPR:
1460 case RTL_EXPR:
1461 return 0;
1462 case WITH_CLEANUP_EXPR:
1463 return 2;
1464 default:
1465 return TREE_CODE_LENGTH (code);
1469 /* Return which tree structure is used by T. */
1471 enum tree_node_structure_enum
1472 tree_node_structure (tree t)
1474 enum tree_code code = TREE_CODE (t);
1476 switch (TREE_CODE_CLASS (code))
1478 case 'd': return TS_DECL;
1479 case 't': return TS_TYPE;
1480 case 'b': return TS_BLOCK;
1481 case 'r': case '<': case '1': case '2': case 'e': case 's':
1482 return TS_EXP;
1483 default: /* 'c' and 'x' */
1484 break;
1486 switch (code)
1488 /* 'c' cases. */
1489 case INTEGER_CST: return TS_INT_CST;
1490 case REAL_CST: return TS_REAL_CST;
1491 case COMPLEX_CST: return TS_COMPLEX;
1492 case VECTOR_CST: return TS_VECTOR;
1493 case STRING_CST: return TS_STRING;
1494 /* 'x' cases. */
1495 case ERROR_MARK: return TS_COMMON;
1496 case IDENTIFIER_NODE: return TS_IDENTIFIER;
1497 case TREE_LIST: return TS_LIST;
1498 case TREE_VEC: return TS_VEC;
1499 case PLACEHOLDER_EXPR: return TS_COMMON;
1501 default:
1502 abort ();
1506 /* Perform any modifications to EXPR required when it is unsaved. Does
1507 not recurse into EXPR's subtrees. */
1509 void
1510 unsave_expr_1 (tree expr)
1512 switch (TREE_CODE (expr))
1514 case SAVE_EXPR:
1515 if (! SAVE_EXPR_PERSISTENT_P (expr))
1516 SAVE_EXPR_RTL (expr) = 0;
1517 break;
1519 case TARGET_EXPR:
1520 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
1521 It's OK for this to happen if it was part of a subtree that
1522 isn't immediately expanded, such as operand 2 of another
1523 TARGET_EXPR. */
1524 if (TREE_OPERAND (expr, 1))
1525 break;
1527 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
1528 TREE_OPERAND (expr, 3) = NULL_TREE;
1529 break;
1531 case RTL_EXPR:
1532 /* I don't yet know how to emit a sequence multiple times. */
1533 if (RTL_EXPR_SEQUENCE (expr) != 0)
1534 abort ();
1535 break;
1537 default:
1538 break;
1542 /* Default lang hook for "unsave_expr_now". */
1544 tree
1545 lhd_unsave_expr_now (tree expr)
1547 enum tree_code code;
1549 /* There's nothing to do for NULL_TREE. */
1550 if (expr == 0)
1551 return expr;
1553 unsave_expr_1 (expr);
1555 code = TREE_CODE (expr);
1556 switch (TREE_CODE_CLASS (code))
1558 case 'c': /* a constant */
1559 case 't': /* a type node */
1560 case 'd': /* A decl node */
1561 case 'b': /* A block node */
1562 break;
1564 case 'x': /* miscellaneous: e.g., identifier, TREE_LIST or ERROR_MARK. */
1565 if (code == TREE_LIST)
1567 lhd_unsave_expr_now (TREE_VALUE (expr));
1568 lhd_unsave_expr_now (TREE_CHAIN (expr));
1570 break;
1572 case 'e': /* an expression */
1573 case 'r': /* a reference */
1574 case 's': /* an expression with side effects */
1575 case '<': /* a comparison expression */
1576 case '2': /* a binary arithmetic expression */
1577 case '1': /* a unary arithmetic expression */
1579 int i;
1581 for (i = first_rtl_op (code) - 1; i >= 0; i--)
1582 lhd_unsave_expr_now (TREE_OPERAND (expr, i));
1584 break;
1586 default:
1587 abort ();
1590 return expr;
1593 /* Return 0 if it is safe to evaluate EXPR multiple times,
1594 return 1 if it is safe if EXPR is unsaved afterward, or
1595 return 2 if it is completely unsafe.
1597 This assumes that CALL_EXPRs and TARGET_EXPRs are never replicated in
1598 an expression tree, so that it safe to unsave them and the surrounding
1599 context will be correct.
1601 SAVE_EXPRs basically *only* appear replicated in an expression tree,
1602 occasionally across the whole of a function. It is therefore only
1603 safe to unsave a SAVE_EXPR if you know that all occurrences appear
1604 below the UNSAVE_EXPR.
1606 RTL_EXPRs consume their rtl during evaluation. It is therefore
1607 never possible to unsave them. */
1610 unsafe_for_reeval (tree expr)
1612 int unsafeness = 0;
1613 enum tree_code code;
1614 int i, tmp, tmp2;
1615 tree exp;
1616 int first_rtl;
1618 if (expr == NULL_TREE)
1619 return 1;
1621 code = TREE_CODE (expr);
1622 first_rtl = first_rtl_op (code);
1624 switch (code)
1626 case SAVE_EXPR:
1627 case RTL_EXPR:
1628 return 2;
1630 case TREE_LIST:
1631 for (exp = expr; exp != 0; exp = TREE_CHAIN (exp))
1633 tmp = unsafe_for_reeval (TREE_VALUE (exp));
1634 unsafeness = MAX (tmp, unsafeness);
1637 return unsafeness;
1639 case CALL_EXPR:
1640 tmp2 = unsafe_for_reeval (TREE_OPERAND (expr, 0));
1641 tmp = unsafe_for_reeval (TREE_OPERAND (expr, 1));
1642 return MAX (MAX (tmp, 1), tmp2);
1644 case TARGET_EXPR:
1645 unsafeness = 1;
1646 break;
1648 case EXIT_BLOCK_EXPR:
1649 /* EXIT_BLOCK_LABELED_BLOCK, a.k.a. TREE_OPERAND (expr, 0), holds
1650 a reference to an ancestor LABELED_BLOCK, so we need to avoid
1651 unbounded recursion in the 'e' traversal code below. */
1652 exp = EXIT_BLOCK_RETURN (expr);
1653 return exp ? unsafe_for_reeval (exp) : 0;
1655 default:
1656 tmp = lang_hooks.unsafe_for_reeval (expr);
1657 if (tmp >= 0)
1658 return tmp;
1659 break;
1662 switch (TREE_CODE_CLASS (code))
1664 case 'c': /* a constant */
1665 case 't': /* a type node */
1666 case 'x': /* something random, like an identifier or an ERROR_MARK. */
1667 case 'd': /* A decl node */
1668 case 'b': /* A block node */
1669 return 0;
1671 case 'e': /* an expression */
1672 case 'r': /* a reference */
1673 case 's': /* an expression with side effects */
1674 case '<': /* a comparison expression */
1675 case '2': /* a binary arithmetic expression */
1676 case '1': /* a unary arithmetic expression */
1677 for (i = first_rtl - 1; i >= 0; i--)
1679 tmp = unsafe_for_reeval (TREE_OPERAND (expr, i));
1680 unsafeness = MAX (tmp, unsafeness);
1683 return unsafeness;
1685 default:
1686 return 2;
1690 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
1691 or offset that depends on a field within a record. */
1693 bool
1694 contains_placeholder_p (tree exp)
1696 enum tree_code code;
1697 int result;
1699 if (!exp)
1700 return 0;
1702 code = TREE_CODE (exp);
1703 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 COND_EXPR:
1731 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
1732 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
1733 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
1735 case SAVE_EXPR:
1736 /* If we already know this doesn't have a placeholder, don't
1737 check again. */
1738 if (SAVE_EXPR_NOPLACEHOLDER (exp) || SAVE_EXPR_RTL (exp) != 0)
1739 return 0;
1741 SAVE_EXPR_NOPLACEHOLDER (exp) = 1;
1742 result = CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
1743 if (result)
1744 SAVE_EXPR_NOPLACEHOLDER (exp) = 0;
1746 return result;
1748 default:
1749 break;
1752 switch (first_rtl_op (code))
1754 case 1:
1755 return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
1756 case 2:
1757 return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
1758 || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
1759 default:
1760 return 0;
1763 default:
1764 return 0;
1766 return 0;
1769 /* Return 1 if any part of the computation of TYPE involves a PLACEHOLDER_EXPR.
1770 This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and field
1771 positions. */
1773 bool
1774 type_contains_placeholder_p (tree type)
1776 /* If the size contains a placeholder or the parent type (component type in
1777 the case of arrays) type involves a placeholder, this type does. */
1778 if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
1779 || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
1780 || (TREE_TYPE (type) != 0
1781 && type_contains_placeholder_p (TREE_TYPE (type))))
1782 return 1;
1784 /* Now do type-specific checks. Note that the last part of the check above
1785 greatly limits what we have to do below. */
1786 switch (TREE_CODE (type))
1788 case VOID_TYPE:
1789 case COMPLEX_TYPE:
1790 case ENUMERAL_TYPE:
1791 case BOOLEAN_TYPE:
1792 case CHAR_TYPE:
1793 case POINTER_TYPE:
1794 case OFFSET_TYPE:
1795 case REFERENCE_TYPE:
1796 case METHOD_TYPE:
1797 case FILE_TYPE:
1798 case FUNCTION_TYPE:
1799 return 0;
1801 case INTEGER_TYPE:
1802 case REAL_TYPE:
1803 /* Here we just check the bounds. */
1804 return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
1805 || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
1807 case ARRAY_TYPE:
1808 case SET_TYPE:
1809 case VECTOR_TYPE:
1810 /* We're already checked the component type (TREE_TYPE), so just check
1811 the index type. */
1812 return type_contains_placeholder_p (TYPE_DOMAIN (type));
1814 case RECORD_TYPE:
1815 case UNION_TYPE:
1816 case QUAL_UNION_TYPE:
1818 static tree seen_types = 0;
1819 tree field;
1820 bool ret = 0;
1822 /* We have to be careful here that we don't end up in infinite
1823 recursions due to a field of a type being a pointer to that type
1824 or to a mutually-recursive type. So we store a list of record
1825 types that we've seen and see if this type is in them. To save
1826 memory, we don't use a list for just one type. Here we check
1827 whether we've seen this type before and store it if not. */
1828 if (seen_types == 0)
1829 seen_types = type;
1830 else if (TREE_CODE (seen_types) != TREE_LIST)
1832 if (seen_types == type)
1833 return 0;
1835 seen_types = tree_cons (NULL_TREE, type,
1836 build_tree_list (NULL_TREE, seen_types));
1838 else
1840 if (value_member (type, seen_types) != 0)
1841 return 0;
1843 seen_types = tree_cons (NULL_TREE, type, seen_types);
1846 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1847 if (TREE_CODE (field) == FIELD_DECL
1848 && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
1849 || (TREE_CODE (type) == QUAL_UNION_TYPE
1850 && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
1851 || type_contains_placeholder_p (TREE_TYPE (field))))
1853 ret = true;
1854 break;
1857 /* Now remove us from seen_types and return the result. */
1858 if (seen_types == type)
1859 seen_types = 0;
1860 else
1861 seen_types = TREE_CHAIN (seen_types);
1863 return ret;
1866 default:
1867 abort ();
1871 /* Return 1 if EXP contains any expressions that produce cleanups for an
1872 outer scope to deal with. Used by fold. */
1875 has_cleanups (tree exp)
1877 int i, nops, cmp;
1879 if (! TREE_SIDE_EFFECTS (exp))
1880 return 0;
1882 switch (TREE_CODE (exp))
1884 case TARGET_EXPR:
1885 case GOTO_SUBROUTINE_EXPR:
1886 case WITH_CLEANUP_EXPR:
1887 return 1;
1889 case CLEANUP_POINT_EXPR:
1890 return 0;
1892 case CALL_EXPR:
1893 for (exp = TREE_OPERAND (exp, 1); exp; exp = TREE_CHAIN (exp))
1895 cmp = has_cleanups (TREE_VALUE (exp));
1896 if (cmp)
1897 return cmp;
1899 return 0;
1901 default:
1902 break;
1905 /* This general rule works for most tree codes. All exceptions should be
1906 handled above. If this is a language-specific tree code, we can't
1907 trust what might be in the operand, so say we don't know
1908 the situation. */
1909 if ((int) TREE_CODE (exp) >= (int) LAST_AND_UNUSED_TREE_CODE)
1910 return -1;
1912 nops = first_rtl_op (TREE_CODE (exp));
1913 for (i = 0; i < nops; i++)
1914 if (TREE_OPERAND (exp, i) != 0)
1916 int type = TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (exp, i)));
1917 if (type == 'e' || type == '<' || type == '1' || type == '2'
1918 || type == 'r' || type == 's')
1920 cmp = has_cleanups (TREE_OPERAND (exp, i));
1921 if (cmp)
1922 return cmp;
1926 return 0;
1929 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
1930 return a tree with all occurrences of references to F in a
1931 PLACEHOLDER_EXPR replaced by R. Note that we assume here that EXP
1932 contains only arithmetic expressions or a CALL_EXPR with a
1933 PLACEHOLDER_EXPR occurring only in its arglist. */
1935 tree
1936 substitute_in_expr (tree exp, tree f, tree r)
1938 enum tree_code code = TREE_CODE (exp);
1939 tree op0, op1, op2;
1940 tree new;
1941 tree inner;
1943 /* We handle TREE_LIST and COMPONENT_REF separately. */
1944 if (code == TREE_LIST)
1946 op0 = SUBSTITUTE_IN_EXPR (TREE_CHAIN (exp), f, r);
1947 op1 = SUBSTITUTE_IN_EXPR (TREE_VALUE (exp), f, r);
1948 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
1949 return exp;
1951 return tree_cons (TREE_PURPOSE (exp), op1, op0);
1953 else if (code == COMPONENT_REF)
1955 /* If this expression is getting a value from a PLACEHOLDER_EXPR
1956 and it is the right field, replace it with R. */
1957 for (inner = TREE_OPERAND (exp, 0);
1958 TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
1959 inner = TREE_OPERAND (inner, 0))
1961 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
1962 && TREE_OPERAND (exp, 1) == f)
1963 return r;
1965 /* If this expression hasn't been completed let, leave it
1966 alone. */
1967 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && TREE_TYPE (inner) == 0)
1968 return exp;
1970 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
1971 if (op0 == TREE_OPERAND (exp, 0))
1972 return exp;
1974 new = fold (build (code, TREE_TYPE (exp), op0, TREE_OPERAND (exp, 1)));
1976 else
1977 switch (TREE_CODE_CLASS (code))
1979 case 'c':
1980 case 'd':
1981 return exp;
1983 case 'x':
1984 case '1':
1985 case '2':
1986 case '<':
1987 case 'e':
1988 case 'r':
1989 switch (first_rtl_op (code))
1991 case 0:
1992 return exp;
1994 case 1:
1995 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
1996 if (op0 == TREE_OPERAND (exp, 0))
1997 return exp;
1999 new = fold (build1 (code, TREE_TYPE (exp), op0));
2000 break;
2002 case 2:
2003 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2004 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2006 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2007 return exp;
2009 new = fold (build2 (code, TREE_TYPE (exp), op0, op1));
2010 break;
2012 case 3:
2013 op0 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 0), f, r);
2014 op1 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 1), f, r);
2015 op2 = SUBSTITUTE_IN_EXPR (TREE_OPERAND (exp, 2), f, r);
2017 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2018 && op2 == TREE_OPERAND (exp, 2))
2019 return exp;
2021 new = fold (build3 (code, TREE_TYPE (exp), op0, op1, op2));
2022 break;
2024 default:
2025 abort ();
2027 break;
2029 default:
2030 abort ();
2033 TREE_READONLY (new) = TREE_READONLY (exp);
2034 return new;
2037 /* Similar, but look for a PLACEHOLDER_EXPR in EXP and find a replacement
2038 for it within OBJ, a tree that is an object or a chain of references. */
2040 tree
2041 substitute_placeholder_in_expr (tree exp, tree obj)
2043 enum tree_code code = TREE_CODE (exp);
2044 tree op0, op1, op2;
2046 /* If this is a PLACEHOLDER_EXPR, see if we find a corresponding type
2047 in the chain of OBJ. */
2048 if (code == PLACEHOLDER_EXPR)
2050 tree need_type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
2051 tree elt;
2053 for (elt = obj; elt != 0;
2054 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
2055 || TREE_CODE (elt) == COND_EXPR)
2056 ? TREE_OPERAND (elt, 1)
2057 : (TREE_CODE_CLASS (TREE_CODE (elt)) == 'r'
2058 || TREE_CODE_CLASS (TREE_CODE (elt)) == '1'
2059 || TREE_CODE_CLASS (TREE_CODE (elt)) == '2'
2060 || TREE_CODE_CLASS (TREE_CODE (elt)) == 'e')
2061 ? TREE_OPERAND (elt, 0) : 0))
2062 if (TYPE_MAIN_VARIANT (TREE_TYPE (elt)) == need_type)
2063 return elt;
2065 for (elt = obj; elt != 0;
2066 elt = ((TREE_CODE (elt) == COMPOUND_EXPR
2067 || TREE_CODE (elt) == COND_EXPR)
2068 ? TREE_OPERAND (elt, 1)
2069 : (TREE_CODE_CLASS (TREE_CODE (elt)) == 'r'
2070 || TREE_CODE_CLASS (TREE_CODE (elt)) == '1'
2071 || TREE_CODE_CLASS (TREE_CODE (elt)) == '2'
2072 || TREE_CODE_CLASS (TREE_CODE (elt)) == 'e')
2073 ? TREE_OPERAND (elt, 0) : 0))
2074 if (POINTER_TYPE_P (TREE_TYPE (elt))
2075 && (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (elt)))
2076 == need_type))
2077 return fold (build1 (INDIRECT_REF, need_type, elt));
2079 /* If we didn't find it, return the original PLACEHOLDER_EXPR. If it
2080 survives until RTL generation, there will be an error. */
2081 return exp;
2084 /* TREE_LIST is special because we need to look at TREE_VALUE
2085 and TREE_CHAIN, not TREE_OPERANDS. */
2086 else if (code == TREE_LIST)
2088 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_CHAIN (exp), obj);
2089 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_VALUE (exp), obj);
2090 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2091 return exp;
2093 return tree_cons (TREE_PURPOSE (exp), op1, op0);
2095 else
2096 switch (TREE_CODE_CLASS (code))
2098 case 'c':
2099 case 'd':
2100 case 'b':
2101 return exp;
2103 case 'x':
2104 case '1':
2105 case '2':
2106 case '<':
2107 case 'e':
2108 case 'r':
2109 case 's':
2110 switch (first_rtl_op (code))
2112 case 0:
2113 return exp;
2115 case 1:
2116 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2117 if (op0 == TREE_OPERAND (exp, 0))
2118 return exp;
2119 else
2120 return fold (build1 (code, TREE_TYPE (exp), op0));
2122 case 2:
2123 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2124 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2126 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2127 return exp;
2128 else
2129 return fold (build2 (code, TREE_TYPE (exp), op0, op1));
2131 case 3:
2132 op0 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 0), obj);
2133 op1 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 1), obj);
2134 op2 = SUBSTITUTE_PLACEHOLDER_IN_EXPR (TREE_OPERAND (exp, 2), obj);
2136 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2137 && op2 == TREE_OPERAND (exp, 2))
2138 return exp;
2139 else
2140 return fold (build3 (code, TREE_TYPE (exp), op0, op1, op2));
2142 default:
2143 abort ();
2145 break;
2147 default:
2148 abort ();
2152 /* Stabilize a reference so that we can use it any number of times
2153 without causing its operands to be evaluated more than once.
2154 Returns the stabilized reference. This works by means of save_expr,
2155 so see the caveats in the comments about save_expr.
2157 Also allows conversion expressions whose operands are references.
2158 Any other kind of expression is returned unchanged. */
2160 tree
2161 stabilize_reference (tree ref)
2163 tree result;
2164 enum tree_code code = TREE_CODE (ref);
2166 switch (code)
2168 case VAR_DECL:
2169 case PARM_DECL:
2170 case RESULT_DECL:
2171 /* No action is needed in this case. */
2172 return ref;
2174 case NOP_EXPR:
2175 case CONVERT_EXPR:
2176 case FLOAT_EXPR:
2177 case FIX_TRUNC_EXPR:
2178 case FIX_FLOOR_EXPR:
2179 case FIX_ROUND_EXPR:
2180 case FIX_CEIL_EXPR:
2181 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2182 break;
2184 case INDIRECT_REF:
2185 result = build_nt (INDIRECT_REF,
2186 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2187 break;
2189 case COMPONENT_REF:
2190 result = build_nt (COMPONENT_REF,
2191 stabilize_reference (TREE_OPERAND (ref, 0)),
2192 TREE_OPERAND (ref, 1));
2193 break;
2195 case BIT_FIELD_REF:
2196 result = build_nt (BIT_FIELD_REF,
2197 stabilize_reference (TREE_OPERAND (ref, 0)),
2198 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2199 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2200 break;
2202 case ARRAY_REF:
2203 result = build_nt (ARRAY_REF,
2204 stabilize_reference (TREE_OPERAND (ref, 0)),
2205 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2206 break;
2208 case ARRAY_RANGE_REF:
2209 result = build_nt (ARRAY_RANGE_REF,
2210 stabilize_reference (TREE_OPERAND (ref, 0)),
2211 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2212 break;
2214 case COMPOUND_EXPR:
2215 /* We cannot wrap the first expression in a SAVE_EXPR, as then
2216 it wouldn't be ignored. This matters when dealing with
2217 volatiles. */
2218 return stabilize_reference_1 (ref);
2220 case RTL_EXPR:
2221 result = build1 (INDIRECT_REF, TREE_TYPE (ref),
2222 save_expr (build1 (ADDR_EXPR,
2223 build_pointer_type (TREE_TYPE (ref)),
2224 ref)));
2225 break;
2227 /* If arg isn't a kind of lvalue we recognize, make no change.
2228 Caller should recognize the error for an invalid lvalue. */
2229 default:
2230 return ref;
2232 case ERROR_MARK:
2233 return error_mark_node;
2236 TREE_TYPE (result) = TREE_TYPE (ref);
2237 TREE_READONLY (result) = TREE_READONLY (ref);
2238 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
2239 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
2241 return result;
2244 /* Subroutine of stabilize_reference; this is called for subtrees of
2245 references. Any expression with side-effects must be put in a SAVE_EXPR
2246 to ensure that it is only evaluated once.
2248 We don't put SAVE_EXPR nodes around everything, because assigning very
2249 simple expressions to temporaries causes us to miss good opportunities
2250 for optimizations. Among other things, the opportunity to fold in the
2251 addition of a constant into an addressing mode often gets lost, e.g.
2252 "y[i+1] += x;". In general, we take the approach that we should not make
2253 an assignment unless we are forced into it - i.e., that any non-side effect
2254 operator should be allowed, and that cse should take care of coalescing
2255 multiple utterances of the same expression should that prove fruitful. */
2257 tree
2258 stabilize_reference_1 (tree e)
2260 tree result;
2261 enum tree_code code = TREE_CODE (e);
2263 /* We cannot ignore const expressions because it might be a reference
2264 to a const array but whose index contains side-effects. But we can
2265 ignore things that are actual constant or that already have been
2266 handled by this function. */
2268 if (TREE_CONSTANT (e) || code == SAVE_EXPR)
2269 return e;
2271 switch (TREE_CODE_CLASS (code))
2273 case 'x':
2274 case 't':
2275 case 'd':
2276 case 'b':
2277 case '<':
2278 case 's':
2279 case 'e':
2280 case 'r':
2281 /* If the expression has side-effects, then encase it in a SAVE_EXPR
2282 so that it will only be evaluated once. */
2283 /* The reference (r) and comparison (<) classes could be handled as
2284 below, but it is generally faster to only evaluate them once. */
2285 if (TREE_SIDE_EFFECTS (e))
2286 return save_expr (e);
2287 return e;
2289 case 'c':
2290 /* Constants need no processing. In fact, we should never reach
2291 here. */
2292 return e;
2294 case '2':
2295 /* Division is slow and tends to be compiled with jumps,
2296 especially the division by powers of 2 that is often
2297 found inside of an array reference. So do it just once. */
2298 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
2299 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
2300 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
2301 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
2302 return save_expr (e);
2303 /* Recursively stabilize each operand. */
2304 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
2305 stabilize_reference_1 (TREE_OPERAND (e, 1)));
2306 break;
2308 case '1':
2309 /* Recursively stabilize each operand. */
2310 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
2311 break;
2313 default:
2314 abort ();
2317 TREE_TYPE (result) = TREE_TYPE (e);
2318 TREE_READONLY (result) = TREE_READONLY (e);
2319 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
2320 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
2322 return result;
2325 /* Low-level constructors for expressions. */
2327 /* Build an expression of code CODE, data type TYPE, and operands as
2328 specified. Expressions and reference nodes can be created this way.
2329 Constants, decls, types and misc nodes cannot be.
2331 We define 5 non-variadic functions, from 0 to 4 arguments. This is
2332 enough for all extant tree codes. These functions can be called
2333 directly (preferably!), but can also be obtained via GCC preprocessor
2334 magic within the build macro. */
2336 tree
2337 build0_stat (enum tree_code code, tree tt MEM_STAT_DECL)
2339 tree t;
2341 #ifdef ENABLE_CHECKING
2342 if (TREE_CODE_LENGTH (code) != 0)
2343 abort ();
2344 #endif
2346 t = make_node_stat (code PASS_MEM_STAT);
2347 TREE_TYPE (t) = tt;
2349 return t;
2352 tree
2353 build1_stat (enum tree_code code, tree type, tree node MEM_STAT_DECL)
2355 int length = sizeof (struct tree_exp);
2356 #ifdef GATHER_STATISTICS
2357 tree_node_kind kind;
2358 #endif
2359 tree t;
2361 #ifdef GATHER_STATISTICS
2362 switch (TREE_CODE_CLASS (code))
2364 case 's': /* an expression with side effects */
2365 kind = s_kind;
2366 break;
2367 case 'r': /* a reference */
2368 kind = r_kind;
2369 break;
2370 default:
2371 kind = e_kind;
2372 break;
2375 tree_node_counts[(int) kind]++;
2376 tree_node_sizes[(int) kind] += length;
2377 #endif
2379 #ifdef ENABLE_CHECKING
2380 if (TREE_CODE_LENGTH (code) != 1)
2381 abort ();
2382 #endif /* ENABLE_CHECKING */
2384 t = ggc_alloc_zone_stat (length, tree_zone PASS_MEM_STAT);
2386 memset (t, 0, sizeof (struct tree_common));
2388 TREE_SET_CODE (t, code);
2390 TREE_TYPE (t) = type;
2391 TREE_COMPLEXITY (t) = 0;
2392 TREE_OPERAND (t, 0) = node;
2393 if (node && first_rtl_op (code) != 0)
2395 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
2396 TREE_READONLY (t) = TREE_READONLY (node);
2399 if (TREE_CODE_CLASS (code) == 's')
2400 TREE_SIDE_EFFECTS (t) = 1;
2401 else switch (code)
2403 case INIT_EXPR:
2404 case MODIFY_EXPR:
2405 case VA_ARG_EXPR:
2406 case RTL_EXPR:
2407 case PREDECREMENT_EXPR:
2408 case PREINCREMENT_EXPR:
2409 case POSTDECREMENT_EXPR:
2410 case POSTINCREMENT_EXPR:
2411 /* All of these have side-effects, no matter what their
2412 operands are. */
2413 TREE_SIDE_EFFECTS (t) = 1;
2414 TREE_READONLY (t) = 0;
2415 break;
2417 case INDIRECT_REF:
2418 /* Whether a dereference is readonly has nothing to do with whether
2419 its operand is readonly. */
2420 TREE_READONLY (t) = 0;
2421 break;
2423 case ADDR_EXPR:
2424 if (node)
2426 /* The address of a volatile decl or reference does not have
2427 side-effects. But be careful not to ignore side-effects from
2428 other sources deeper in the expression--if node is a _REF and
2429 one of its operands has side-effects, so do we. */
2430 if (TREE_THIS_VOLATILE (node))
2432 TREE_SIDE_EFFECTS (t) = 0;
2433 if (!DECL_P (node))
2435 int i = first_rtl_op (TREE_CODE (node)) - 1;
2436 for (; i >= 0; --i)
2438 if (TREE_SIDE_EFFECTS (TREE_OPERAND (node, i)))
2439 TREE_SIDE_EFFECTS (t) = 1;
2444 break;
2446 default:
2447 if (TREE_CODE_CLASS (code) == '1' && node && TREE_CONSTANT (node))
2448 TREE_CONSTANT (t) = 1;
2449 break;
2452 return t;
2455 #define PROCESS_ARG(N) \
2456 do { \
2457 TREE_OPERAND (t, N) = arg##N; \
2458 if (arg##N && fro > N) \
2460 if (TREE_SIDE_EFFECTS (arg##N)) \
2461 side_effects = 1; \
2462 if (!TREE_READONLY (arg##N)) \
2463 read_only = 0; \
2464 if (!TREE_CONSTANT (arg##N)) \
2465 constant = 0; \
2467 } while (0)
2469 tree
2470 build2_stat (enum tree_code code, tree tt, tree arg0, tree arg1 MEM_STAT_DECL)
2472 bool constant, read_only, side_effects;
2473 tree t;
2474 int fro;
2476 #ifdef ENABLE_CHECKING
2477 if (TREE_CODE_LENGTH (code) != 2)
2478 abort ();
2479 #endif
2481 t = make_node_stat (code PASS_MEM_STAT);
2482 TREE_TYPE (t) = tt;
2484 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
2485 result based on those same flags for the arguments. But if the
2486 arguments aren't really even `tree' expressions, we shouldn't be trying
2487 to do this. */
2488 fro = first_rtl_op (code);
2490 /* Expressions without side effects may be constant if their
2491 arguments are as well. */
2492 constant = (TREE_CODE_CLASS (code) == '<'
2493 || TREE_CODE_CLASS (code) == '2');
2494 read_only = 1;
2495 side_effects = TREE_SIDE_EFFECTS (t);
2497 PROCESS_ARG(0);
2498 PROCESS_ARG(1);
2500 if (code == CALL_EXPR && !side_effects)
2502 tree node;
2503 int i;
2505 /* Calls have side-effects, except those to const or
2506 pure functions. */
2507 i = call_expr_flags (t);
2508 if (!(i & (ECF_CONST | ECF_PURE)))
2509 side_effects = 1;
2511 /* And even those have side-effects if their arguments do. */
2512 else for (node = TREE_OPERAND (t, 1); node; node = TREE_CHAIN (node))
2513 if (TREE_SIDE_EFFECTS (TREE_VALUE (node)))
2515 side_effects = 1;
2516 break;
2520 TREE_READONLY (t) = read_only;
2521 TREE_CONSTANT (t) = constant;
2522 TREE_SIDE_EFFECTS (t) = side_effects;
2524 return t;
2527 tree
2528 build3_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
2529 tree arg2 MEM_STAT_DECL)
2531 bool constant, read_only, side_effects;
2532 tree t;
2533 int fro;
2535 /* ??? Quite a lot of existing code passes one too many arguments to
2536 CALL_EXPR. Not going to fix them, because CALL_EXPR is about to
2537 grow a new argument, so it would just mean changing them back. */
2538 if (code == CALL_EXPR)
2540 if (arg2 != NULL_TREE)
2541 abort ();
2542 return build2 (code, tt, arg0, arg1);
2545 #ifdef ENABLE_CHECKING
2546 if (TREE_CODE_LENGTH (code) != 3)
2547 abort ();
2548 #endif
2550 t = make_node_stat (code PASS_MEM_STAT);
2551 TREE_TYPE (t) = tt;
2553 fro = first_rtl_op (code);
2555 side_effects = TREE_SIDE_EFFECTS (t);
2557 PROCESS_ARG(0);
2558 PROCESS_ARG(1);
2559 PROCESS_ARG(2);
2561 TREE_SIDE_EFFECTS (t) = side_effects;
2563 return t;
2566 tree
2567 build4_stat (enum tree_code code, tree tt, tree arg0, tree arg1,
2568 tree arg2, tree arg3 MEM_STAT_DECL)
2570 bool constant, read_only, side_effects;
2571 tree t;
2572 int fro;
2574 #ifdef ENABLE_CHECKING
2575 if (TREE_CODE_LENGTH (code) != 4)
2576 abort ();
2577 #endif
2579 t = make_node_stat (code PASS_MEM_STAT);
2580 TREE_TYPE (t) = tt;
2582 fro = first_rtl_op (code);
2584 side_effects = TREE_SIDE_EFFECTS (t);
2586 PROCESS_ARG(0);
2587 PROCESS_ARG(1);
2588 PROCESS_ARG(2);
2589 PROCESS_ARG(3);
2591 TREE_SIDE_EFFECTS (t) = side_effects;
2593 return t;
2596 /* Backup definition for non-gcc build compilers. */
2598 tree
2599 (build) (enum tree_code code, tree tt, ...)
2601 tree t, arg0, arg1, arg2, arg3;
2602 int length = TREE_CODE_LENGTH (code);
2603 va_list p;
2605 va_start (p, tt);
2606 switch (length)
2608 case 0:
2609 t = build0 (code, tt);
2610 break;
2611 case 1:
2612 arg0 = va_arg (p, tree);
2613 t = build1 (code, tt, arg0);
2614 break;
2615 case 2:
2616 arg0 = va_arg (p, tree);
2617 arg1 = va_arg (p, tree);
2618 t = build2 (code, tt, arg0, arg1);
2619 break;
2620 case 3:
2621 arg0 = va_arg (p, tree);
2622 arg1 = va_arg (p, tree);
2623 arg2 = va_arg (p, tree);
2624 t = build3 (code, tt, arg0, arg1, arg2);
2625 break;
2626 case 4:
2627 arg0 = va_arg (p, tree);
2628 arg1 = va_arg (p, tree);
2629 arg2 = va_arg (p, tree);
2630 arg3 = va_arg (p, tree);
2631 t = build4 (code, tt, arg0, arg1, arg2, arg3);
2632 break;
2633 default:
2634 abort ();
2636 va_end (p);
2638 return t;
2641 /* Similar except don't specify the TREE_TYPE
2642 and leave the TREE_SIDE_EFFECTS as 0.
2643 It is permissible for arguments to be null,
2644 or even garbage if their values do not matter. */
2646 tree
2647 build_nt (enum tree_code code, ...)
2649 tree t;
2650 int length;
2651 int i;
2652 va_list p;
2654 va_start (p, code);
2656 t = make_node (code);
2657 length = TREE_CODE_LENGTH (code);
2659 for (i = 0; i < length; i++)
2660 TREE_OPERAND (t, i) = va_arg (p, tree);
2662 va_end (p);
2663 return t;
2666 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
2667 We do NOT enter this node in any sort of symbol table.
2669 layout_decl is used to set up the decl's storage layout.
2670 Other slots are initialized to 0 or null pointers. */
2672 tree
2673 build_decl_stat (enum tree_code code, tree name, tree type MEM_STAT_DECL)
2675 tree t;
2677 t = make_node_stat (code PASS_MEM_STAT);
2679 /* if (type == error_mark_node)
2680 type = integer_type_node; */
2681 /* That is not done, deliberately, so that having error_mark_node
2682 as the type can suppress useless errors in the use of this variable. */
2684 DECL_NAME (t) = name;
2685 TREE_TYPE (t) = type;
2687 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
2688 layout_decl (t, 0);
2689 else if (code == FUNCTION_DECL)
2690 DECL_MODE (t) = FUNCTION_MODE;
2692 return t;
2695 /* BLOCK nodes are used to represent the structure of binding contours
2696 and declarations, once those contours have been exited and their contents
2697 compiled. This information is used for outputting debugging info. */
2699 tree
2700 build_block (tree vars, tree tags ATTRIBUTE_UNUSED, tree subblocks,
2701 tree supercontext, tree chain)
2703 tree block = make_node (BLOCK);
2705 BLOCK_VARS (block) = vars;
2706 BLOCK_SUBBLOCKS (block) = subblocks;
2707 BLOCK_SUPERCONTEXT (block) = supercontext;
2708 BLOCK_CHAIN (block) = chain;
2709 return block;
2712 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
2713 location where an expression or an identifier were encountered. It
2714 is necessary for languages where the frontend parser will handle
2715 recursively more than one file (Java is one of them). */
2717 tree
2718 build_expr_wfl (tree node, const char *file, int line, int col)
2720 static const char *last_file = 0;
2721 static tree last_filenode = NULL_TREE;
2722 tree wfl = make_node (EXPR_WITH_FILE_LOCATION);
2724 EXPR_WFL_NODE (wfl) = node;
2725 EXPR_WFL_SET_LINECOL (wfl, line, col);
2726 if (file != last_file)
2728 last_file = file;
2729 last_filenode = file ? get_identifier (file) : NULL_TREE;
2732 EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
2733 if (node)
2735 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
2736 TREE_TYPE (wfl) = TREE_TYPE (node);
2739 return wfl;
2742 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
2743 is ATTRIBUTE. */
2745 tree
2746 build_decl_attribute_variant (tree ddecl, tree attribute)
2748 DECL_ATTRIBUTES (ddecl) = attribute;
2749 return ddecl;
2752 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
2753 is ATTRIBUTE.
2755 Record such modified types already made so we don't make duplicates. */
2757 tree
2758 build_type_attribute_variant (tree ttype, tree attribute)
2760 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
2762 hashval_t hashcode = 0;
2763 tree ntype;
2764 enum tree_code code = TREE_CODE (ttype);
2766 ntype = copy_node (ttype);
2768 TYPE_POINTER_TO (ntype) = 0;
2769 TYPE_REFERENCE_TO (ntype) = 0;
2770 TYPE_ATTRIBUTES (ntype) = attribute;
2772 /* Create a new main variant of TYPE. */
2773 TYPE_MAIN_VARIANT (ntype) = ntype;
2774 TYPE_NEXT_VARIANT (ntype) = 0;
2775 set_type_quals (ntype, TYPE_UNQUALIFIED);
2777 hashcode = iterative_hash_object (code, hashcode);
2778 if (TREE_TYPE (ntype))
2779 hashcode = iterative_hash_object (TYPE_HASH (TREE_TYPE (ntype)),
2780 hashcode);
2781 hashcode = attribute_hash_list (attribute, hashcode);
2783 switch (TREE_CODE (ntype))
2785 case FUNCTION_TYPE:
2786 hashcode = type_hash_list (TYPE_ARG_TYPES (ntype), hashcode);
2787 break;
2788 case ARRAY_TYPE:
2789 hashcode = iterative_hash_object (TYPE_HASH (TYPE_DOMAIN (ntype)),
2790 hashcode);
2791 break;
2792 case INTEGER_TYPE:
2793 hashcode = iterative_hash_object
2794 (TREE_INT_CST_LOW (TYPE_MAX_VALUE (ntype)), hashcode);
2795 hashcode = iterative_hash_object
2796 (TREE_INT_CST_HIGH (TYPE_MAX_VALUE (ntype)), hashcode);
2797 break;
2798 case REAL_TYPE:
2800 unsigned int precision = TYPE_PRECISION (ntype);
2801 hashcode = iterative_hash_object (precision, hashcode);
2803 break;
2804 default:
2805 break;
2808 ntype = type_hash_canon (hashcode, ntype);
2809 ttype = build_qualified_type (ntype, TYPE_QUALS (ttype));
2812 return ttype;
2815 /* Return nonzero if IDENT is a valid name for attribute ATTR,
2816 or zero if not.
2818 We try both `text' and `__text__', ATTR may be either one. */
2819 /* ??? It might be a reasonable simplification to require ATTR to be only
2820 `text'. One might then also require attribute lists to be stored in
2821 their canonicalized form. */
2824 is_attribute_p (const char *attr, tree ident)
2826 int ident_len, attr_len;
2827 const char *p;
2829 if (TREE_CODE (ident) != IDENTIFIER_NODE)
2830 return 0;
2832 if (strcmp (attr, IDENTIFIER_POINTER (ident)) == 0)
2833 return 1;
2835 p = IDENTIFIER_POINTER (ident);
2836 ident_len = strlen (p);
2837 attr_len = strlen (attr);
2839 /* If ATTR is `__text__', IDENT must be `text'; and vice versa. */
2840 if (attr[0] == '_')
2842 if (attr[1] != '_'
2843 || attr[attr_len - 2] != '_'
2844 || attr[attr_len - 1] != '_')
2845 abort ();
2846 if (ident_len == attr_len - 4
2847 && strncmp (attr + 2, p, attr_len - 4) == 0)
2848 return 1;
2850 else
2852 if (ident_len == attr_len + 4
2853 && p[0] == '_' && p[1] == '_'
2854 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
2855 && strncmp (attr, p + 2, attr_len) == 0)
2856 return 1;
2859 return 0;
2862 /* Given an attribute name and a list of attributes, return a pointer to the
2863 attribute's list element if the attribute is part of the list, or NULL_TREE
2864 if not found. If the attribute appears more than once, this only
2865 returns the first occurrence; the TREE_CHAIN of the return value should
2866 be passed back in if further occurrences are wanted. */
2868 tree
2869 lookup_attribute (const char *attr_name, tree list)
2871 tree l;
2873 for (l = list; l; l = TREE_CHAIN (l))
2875 if (TREE_CODE (TREE_PURPOSE (l)) != IDENTIFIER_NODE)
2876 abort ();
2877 if (is_attribute_p (attr_name, TREE_PURPOSE (l)))
2878 return l;
2881 return NULL_TREE;
2884 /* Return an attribute list that is the union of a1 and a2. */
2886 tree
2887 merge_attributes (tree a1, tree a2)
2889 tree attributes;
2891 /* Either one unset? Take the set one. */
2893 if ((attributes = a1) == 0)
2894 attributes = a2;
2896 /* One that completely contains the other? Take it. */
2898 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
2900 if (attribute_list_contained (a2, a1))
2901 attributes = a2;
2902 else
2904 /* Pick the longest list, and hang on the other list. */
2906 if (list_length (a1) < list_length (a2))
2907 attributes = a2, a2 = a1;
2909 for (; a2 != 0; a2 = TREE_CHAIN (a2))
2911 tree a;
2912 for (a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
2913 attributes);
2914 a != NULL_TREE;
2915 a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
2916 TREE_CHAIN (a)))
2918 if (simple_cst_equal (TREE_VALUE (a), TREE_VALUE (a2)) == 1)
2919 break;
2921 if (a == NULL_TREE)
2923 a1 = copy_node (a2);
2924 TREE_CHAIN (a1) = attributes;
2925 attributes = a1;
2930 return attributes;
2933 /* Given types T1 and T2, merge their attributes and return
2934 the result. */
2936 tree
2937 merge_type_attributes (tree t1, tree t2)
2939 return merge_attributes (TYPE_ATTRIBUTES (t1),
2940 TYPE_ATTRIBUTES (t2));
2943 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
2944 the result. */
2946 tree
2947 merge_decl_attributes (tree olddecl, tree newdecl)
2949 return merge_attributes (DECL_ATTRIBUTES (olddecl),
2950 DECL_ATTRIBUTES (newdecl));
2953 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
2955 /* Specialization of merge_decl_attributes for various Windows targets.
2957 This handles the following situation:
2959 __declspec (dllimport) int foo;
2960 int foo;
2962 The second instance of `foo' nullifies the dllimport. */
2964 tree
2965 merge_dllimport_decl_attributes (tree old, tree new)
2967 tree a;
2968 int delete_dllimport_p;
2970 old = DECL_ATTRIBUTES (old);
2971 new = DECL_ATTRIBUTES (new);
2973 /* What we need to do here is remove from `old' dllimport if it doesn't
2974 appear in `new'. dllimport behaves like extern: if a declaration is
2975 marked dllimport and a definition appears later, then the object
2976 is not dllimport'd. */
2977 if (lookup_attribute ("dllimport", old) != NULL_TREE
2978 && lookup_attribute ("dllimport", new) == NULL_TREE)
2979 delete_dllimport_p = 1;
2980 else
2981 delete_dllimport_p = 0;
2983 a = merge_attributes (old, new);
2985 if (delete_dllimport_p)
2987 tree prev, t;
2989 /* Scan the list for dllimport and delete it. */
2990 for (prev = NULL_TREE, t = a; t; prev = t, t = TREE_CHAIN (t))
2992 if (is_attribute_p ("dllimport", TREE_PURPOSE (t)))
2994 if (prev == NULL_TREE)
2995 a = TREE_CHAIN (a);
2996 else
2997 TREE_CHAIN (prev) = TREE_CHAIN (t);
2998 break;
3003 return a;
3006 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
3008 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
3009 of the various TYPE_QUAL values. */
3011 static void
3012 set_type_quals (tree type, int type_quals)
3014 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
3015 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
3016 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
3019 /* Returns true iff cand is equivalent to base with type_quals. */
3021 bool
3022 check_qualified_type (tree cand, tree base, int type_quals)
3024 return (TYPE_QUALS (cand) == type_quals
3025 && TYPE_NAME (cand) == TYPE_NAME (base)
3026 /* Apparently this is needed for Objective-C. */
3027 && TYPE_CONTEXT (cand) == TYPE_CONTEXT (base)
3028 && attribute_list_equal (TYPE_ATTRIBUTES (cand),
3029 TYPE_ATTRIBUTES (base)));
3032 /* Return a version of the TYPE, qualified as indicated by the
3033 TYPE_QUALS, if one exists. If no qualified version exists yet,
3034 return NULL_TREE. */
3036 tree
3037 get_qualified_type (tree type, int type_quals)
3039 tree t;
3041 if (TYPE_QUALS (type) == type_quals)
3042 return type;
3044 /* Search the chain of variants to see if there is already one there just
3045 like the one we need to have. If so, use that existing one. We must
3046 preserve the TYPE_NAME, since there is code that depends on this. */
3047 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
3048 if (check_qualified_type (t, type, type_quals))
3049 return t;
3051 return NULL_TREE;
3054 /* Like get_qualified_type, but creates the type if it does not
3055 exist. This function never returns NULL_TREE. */
3057 tree
3058 build_qualified_type (tree type, int type_quals)
3060 tree t;
3062 /* See if we already have the appropriate qualified variant. */
3063 t = get_qualified_type (type, type_quals);
3065 /* If not, build it. */
3066 if (!t)
3068 t = build_type_copy (type);
3069 set_type_quals (t, type_quals);
3072 return t;
3075 /* Create a new variant of TYPE, equivalent but distinct.
3076 This is so the caller can modify it. */
3078 tree
3079 build_type_copy (tree type)
3081 tree t, m = TYPE_MAIN_VARIANT (type);
3083 t = copy_node (type);
3085 TYPE_POINTER_TO (t) = 0;
3086 TYPE_REFERENCE_TO (t) = 0;
3088 /* Add this type to the chain of variants of TYPE. */
3089 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
3090 TYPE_NEXT_VARIANT (m) = t;
3092 return t;
3095 /* Hashing of types so that we don't make duplicates.
3096 The entry point is `type_hash_canon'. */
3098 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
3099 with types in the TREE_VALUE slots), by adding the hash codes
3100 of the individual types. */
3102 unsigned int
3103 type_hash_list (tree list, hashval_t hashcode)
3105 tree tail;
3107 for (tail = list; tail; tail = TREE_CHAIN (tail))
3108 if (TREE_VALUE (tail) != error_mark_node)
3109 hashcode = iterative_hash_object (TYPE_HASH (TREE_VALUE (tail)),
3110 hashcode);
3112 return hashcode;
3115 /* These are the Hashtable callback functions. */
3117 /* Returns true iff the types are equivalent. */
3119 static int
3120 type_hash_eq (const void *va, const void *vb)
3122 const struct type_hash *a = va, *b = vb;
3124 /* First test the things that are the same for all types. */
3125 if (a->hash != b->hash
3126 || TREE_CODE (a->type) != TREE_CODE (b->type)
3127 || TREE_TYPE (a->type) != TREE_TYPE (b->type)
3128 || !attribute_list_equal (TYPE_ATTRIBUTES (a->type),
3129 TYPE_ATTRIBUTES (b->type))
3130 || TYPE_ALIGN (a->type) != TYPE_ALIGN (b->type)
3131 || TYPE_MODE (a->type) != TYPE_MODE (b->type))
3132 return 0;
3134 switch (TREE_CODE (a->type))
3136 case VOID_TYPE:
3137 case COMPLEX_TYPE:
3138 case VECTOR_TYPE:
3139 case POINTER_TYPE:
3140 case REFERENCE_TYPE:
3141 return 1;
3143 case ENUMERAL_TYPE:
3144 if (TYPE_VALUES (a->type) != TYPE_VALUES (b->type)
3145 && !(TYPE_VALUES (a->type)
3146 && TREE_CODE (TYPE_VALUES (a->type)) == TREE_LIST
3147 && TYPE_VALUES (b->type)
3148 && TREE_CODE (TYPE_VALUES (b->type)) == TREE_LIST
3149 && type_list_equal (TYPE_VALUES (a->type),
3150 TYPE_VALUES (b->type))))
3151 return 0;
3153 /* ... fall through ... */
3155 case INTEGER_TYPE:
3156 case REAL_TYPE:
3157 case BOOLEAN_TYPE:
3158 case CHAR_TYPE:
3159 return ((TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
3160 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
3161 TYPE_MAX_VALUE (b->type)))
3162 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
3163 && tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
3164 TYPE_MIN_VALUE (b->type))));
3166 case OFFSET_TYPE:
3167 return TYPE_OFFSET_BASETYPE (a->type) == TYPE_OFFSET_BASETYPE (b->type);
3169 case METHOD_TYPE:
3170 return (TYPE_METHOD_BASETYPE (a->type) == TYPE_METHOD_BASETYPE (b->type)
3171 && (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
3172 || (TYPE_ARG_TYPES (a->type)
3173 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
3174 && TYPE_ARG_TYPES (b->type)
3175 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
3176 && type_list_equal (TYPE_ARG_TYPES (a->type),
3177 TYPE_ARG_TYPES (b->type)))));
3179 case ARRAY_TYPE:
3180 case SET_TYPE:
3181 return TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type);
3183 case RECORD_TYPE:
3184 case UNION_TYPE:
3185 case QUAL_UNION_TYPE:
3186 return (TYPE_FIELDS (a->type) == TYPE_FIELDS (b->type)
3187 || (TYPE_FIELDS (a->type)
3188 && TREE_CODE (TYPE_FIELDS (a->type)) == TREE_LIST
3189 && TYPE_FIELDS (b->type)
3190 && TREE_CODE (TYPE_FIELDS (b->type)) == TREE_LIST
3191 && type_list_equal (TYPE_FIELDS (a->type),
3192 TYPE_FIELDS (b->type))));
3194 case FUNCTION_TYPE:
3195 return (TYPE_ARG_TYPES (a->type) == TYPE_ARG_TYPES (b->type)
3196 || (TYPE_ARG_TYPES (a->type)
3197 && TREE_CODE (TYPE_ARG_TYPES (a->type)) == TREE_LIST
3198 && TYPE_ARG_TYPES (b->type)
3199 && TREE_CODE (TYPE_ARG_TYPES (b->type)) == TREE_LIST
3200 && type_list_equal (TYPE_ARG_TYPES (a->type),
3201 TYPE_ARG_TYPES (b->type))));
3203 default:
3204 return 0;
3208 /* Return the cached hash value. */
3210 static hashval_t
3211 type_hash_hash (const void *item)
3213 return ((const struct type_hash *) item)->hash;
3216 /* Look in the type hash table for a type isomorphic to TYPE.
3217 If one is found, return it. Otherwise return 0. */
3219 tree
3220 type_hash_lookup (hashval_t hashcode, tree type)
3222 struct type_hash *h, in;
3224 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
3225 must call that routine before comparing TYPE_ALIGNs. */
3226 layout_type (type);
3228 in.hash = hashcode;
3229 in.type = type;
3231 h = htab_find_with_hash (type_hash_table, &in, hashcode);
3232 if (h)
3233 return h->type;
3234 return NULL_TREE;
3237 /* Add an entry to the type-hash-table
3238 for a type TYPE whose hash code is HASHCODE. */
3240 void
3241 type_hash_add (hashval_t hashcode, tree type)
3243 struct type_hash *h;
3244 void **loc;
3246 h = ggc_alloc (sizeof (struct type_hash));
3247 h->hash = hashcode;
3248 h->type = type;
3249 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
3250 *(struct type_hash **) loc = h;
3253 /* Given TYPE, and HASHCODE its hash code, return the canonical
3254 object for an identical type if one already exists.
3255 Otherwise, return TYPE, and record it as the canonical object.
3257 To use this function, first create a type of the sort you want.
3258 Then compute its hash code from the fields of the type that
3259 make it different from other similar types.
3260 Then call this function and use the value. */
3262 tree
3263 type_hash_canon (unsigned int hashcode, tree type)
3265 tree t1;
3267 /* The hash table only contains main variants, so ensure that's what we're
3268 being passed. */
3269 if (TYPE_MAIN_VARIANT (type) != type)
3270 abort ();
3272 if (!lang_hooks.types.hash_types)
3273 return type;
3275 /* See if the type is in the hash table already. If so, return it.
3276 Otherwise, add the type. */
3277 t1 = type_hash_lookup (hashcode, type);
3278 if (t1 != 0)
3280 #ifdef GATHER_STATISTICS
3281 tree_node_counts[(int) t_kind]--;
3282 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
3283 #endif
3284 return t1;
3286 else
3288 type_hash_add (hashcode, type);
3289 return type;
3293 /* See if the data pointed to by the type hash table is marked. We consider
3294 it marked if the type is marked or if a debug type number or symbol
3295 table entry has been made for the type. This reduces the amount of
3296 debugging output and eliminates that dependency of the debug output on
3297 the number of garbage collections. */
3299 static int
3300 type_hash_marked_p (const void *p)
3302 tree type = ((struct type_hash *) p)->type;
3304 return ggc_marked_p (type) || TYPE_SYMTAB_POINTER (type);
3307 static void
3308 print_type_hash_statistics (void)
3310 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
3311 (long) htab_size (type_hash_table),
3312 (long) htab_elements (type_hash_table),
3313 htab_collisions (type_hash_table));
3316 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
3317 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
3318 by adding the hash codes of the individual attributes. */
3320 unsigned int
3321 attribute_hash_list (tree list, hashval_t hashcode)
3323 tree tail;
3325 for (tail = list; tail; tail = TREE_CHAIN (tail))
3326 /* ??? Do we want to add in TREE_VALUE too? */
3327 hashcode = iterative_hash_object
3328 (IDENTIFIER_HASH_VALUE (TREE_PURPOSE (tail)), hashcode);
3329 return hashcode;
3332 /* Given two lists of attributes, return true if list l2 is
3333 equivalent to l1. */
3336 attribute_list_equal (tree l1, tree l2)
3338 return attribute_list_contained (l1, l2)
3339 && attribute_list_contained (l2, l1);
3342 /* Given two lists of attributes, return true if list L2 is
3343 completely contained within L1. */
3344 /* ??? This would be faster if attribute names were stored in a canonicalized
3345 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
3346 must be used to show these elements are equivalent (which they are). */
3347 /* ??? It's not clear that attributes with arguments will always be handled
3348 correctly. */
3351 attribute_list_contained (tree l1, tree l2)
3353 tree t1, t2;
3355 /* First check the obvious, maybe the lists are identical. */
3356 if (l1 == l2)
3357 return 1;
3359 /* Maybe the lists are similar. */
3360 for (t1 = l1, t2 = l2;
3361 t1 != 0 && t2 != 0
3362 && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
3363 && TREE_VALUE (t1) == TREE_VALUE (t2);
3364 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
3366 /* Maybe the lists are equal. */
3367 if (t1 == 0 && t2 == 0)
3368 return 1;
3370 for (; t2 != 0; t2 = TREE_CHAIN (t2))
3372 tree attr;
3373 for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)), l1);
3374 attr != NULL_TREE;
3375 attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
3376 TREE_CHAIN (attr)))
3378 if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) == 1)
3379 break;
3382 if (attr == 0)
3383 return 0;
3385 if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) != 1)
3386 return 0;
3389 return 1;
3392 /* Given two lists of types
3393 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
3394 return 1 if the lists contain the same types in the same order.
3395 Also, the TREE_PURPOSEs must match. */
3398 type_list_equal (tree l1, tree l2)
3400 tree t1, t2;
3402 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
3403 if (TREE_VALUE (t1) != TREE_VALUE (t2)
3404 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
3405 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
3406 && (TREE_TYPE (TREE_PURPOSE (t1))
3407 == TREE_TYPE (TREE_PURPOSE (t2))))))
3408 return 0;
3410 return t1 == t2;
3413 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
3414 given by TYPE. If the argument list accepts variable arguments,
3415 then this function counts only the ordinary arguments. */
3418 type_num_arguments (tree type)
3420 int i = 0;
3421 tree t;
3423 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
3424 /* If the function does not take a variable number of arguments,
3425 the last element in the list will have type `void'. */
3426 if (VOID_TYPE_P (TREE_VALUE (t)))
3427 break;
3428 else
3429 ++i;
3431 return i;
3434 /* Nonzero if integer constants T1 and T2
3435 represent the same constant value. */
3438 tree_int_cst_equal (tree t1, tree t2)
3440 if (t1 == t2)
3441 return 1;
3443 if (t1 == 0 || t2 == 0)
3444 return 0;
3446 if (TREE_CODE (t1) == INTEGER_CST
3447 && TREE_CODE (t2) == INTEGER_CST
3448 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3449 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
3450 return 1;
3452 return 0;
3455 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
3456 The precise way of comparison depends on their data type. */
3459 tree_int_cst_lt (tree t1, tree t2)
3461 if (t1 == t2)
3462 return 0;
3464 if (TREE_UNSIGNED (TREE_TYPE (t1)) != TREE_UNSIGNED (TREE_TYPE (t2)))
3466 int t1_sgn = tree_int_cst_sgn (t1);
3467 int t2_sgn = tree_int_cst_sgn (t2);
3469 if (t1_sgn < t2_sgn)
3470 return 1;
3471 else if (t1_sgn > t2_sgn)
3472 return 0;
3473 /* Otherwise, both are non-negative, so we compare them as
3474 unsigned just in case one of them would overflow a signed
3475 type. */
3477 else if (! TREE_UNSIGNED (TREE_TYPE (t1)))
3478 return INT_CST_LT (t1, t2);
3480 return INT_CST_LT_UNSIGNED (t1, t2);
3483 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
3486 tree_int_cst_compare (tree t1, tree t2)
3488 if (tree_int_cst_lt (t1, t2))
3489 return -1;
3490 else if (tree_int_cst_lt (t2, t1))
3491 return 1;
3492 else
3493 return 0;
3496 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
3497 the host. If POS is zero, the value can be represented in a single
3498 HOST_WIDE_INT. If POS is nonzero, the value must be positive and can
3499 be represented in a single unsigned HOST_WIDE_INT. */
3502 host_integerp (tree t, int pos)
3504 return (TREE_CODE (t) == INTEGER_CST
3505 && ! TREE_OVERFLOW (t)
3506 && ((TREE_INT_CST_HIGH (t) == 0
3507 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
3508 || (! pos && TREE_INT_CST_HIGH (t) == -1
3509 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
3510 && ! TREE_UNSIGNED (TREE_TYPE (t)))
3511 || (pos && TREE_INT_CST_HIGH (t) == 0)));
3514 /* Return the HOST_WIDE_INT least significant bits of T if it is an
3515 INTEGER_CST and there is no overflow. POS is nonzero if the result must
3516 be positive. Abort if we cannot satisfy the above conditions. */
3518 HOST_WIDE_INT
3519 tree_low_cst (tree t, int pos)
3521 if (host_integerp (t, pos))
3522 return TREE_INT_CST_LOW (t);
3523 else
3524 abort ();
3527 /* Return the most significant bit of the integer constant T. */
3530 tree_int_cst_msb (tree t)
3532 int prec;
3533 HOST_WIDE_INT h;
3534 unsigned HOST_WIDE_INT l;
3536 /* Note that using TYPE_PRECISION here is wrong. We care about the
3537 actual bits, not the (arbitrary) range of the type. */
3538 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
3539 rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
3540 2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
3541 return (l & 1) == 1;
3544 /* Return an indication of the sign of the integer constant T.
3545 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
3546 Note that -1 will never be returned it T's type is unsigned. */
3549 tree_int_cst_sgn (tree t)
3551 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
3552 return 0;
3553 else if (TREE_UNSIGNED (TREE_TYPE (t)))
3554 return 1;
3555 else if (TREE_INT_CST_HIGH (t) < 0)
3556 return -1;
3557 else
3558 return 1;
3561 /* Compare two constructor-element-type constants. Return 1 if the lists
3562 are known to be equal; otherwise return 0. */
3565 simple_cst_list_equal (tree l1, tree l2)
3567 while (l1 != NULL_TREE && l2 != NULL_TREE)
3569 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
3570 return 0;
3572 l1 = TREE_CHAIN (l1);
3573 l2 = TREE_CHAIN (l2);
3576 return l1 == l2;
3579 /* Return truthvalue of whether T1 is the same tree structure as T2.
3580 Return 1 if they are the same.
3581 Return 0 if they are understandably different.
3582 Return -1 if either contains tree structure not understood by
3583 this function. */
3586 simple_cst_equal (tree t1, tree t2)
3588 enum tree_code code1, code2;
3589 int cmp;
3590 int i;
3592 if (t1 == t2)
3593 return 1;
3594 if (t1 == 0 || t2 == 0)
3595 return 0;
3597 code1 = TREE_CODE (t1);
3598 code2 = TREE_CODE (t2);
3600 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
3602 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3603 || code2 == NON_LVALUE_EXPR)
3604 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3605 else
3606 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
3609 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3610 || code2 == NON_LVALUE_EXPR)
3611 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
3613 if (code1 != code2)
3614 return 0;
3616 switch (code1)
3618 case INTEGER_CST:
3619 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3620 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
3622 case REAL_CST:
3623 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3625 case STRING_CST:
3626 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3627 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3628 TREE_STRING_LENGTH (t1)));
3630 case CONSTRUCTOR:
3631 if (CONSTRUCTOR_ELTS (t1) == CONSTRUCTOR_ELTS (t2))
3632 return 1;
3633 else
3634 abort ();
3636 case SAVE_EXPR:
3637 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3639 case CALL_EXPR:
3640 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3641 if (cmp <= 0)
3642 return cmp;
3643 return
3644 simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3646 case TARGET_EXPR:
3647 /* Special case: if either target is an unallocated VAR_DECL,
3648 it means that it's going to be unified with whatever the
3649 TARGET_EXPR is really supposed to initialize, so treat it
3650 as being equivalent to anything. */
3651 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
3652 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
3653 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
3654 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
3655 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
3656 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
3657 cmp = 1;
3658 else
3659 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3661 if (cmp <= 0)
3662 return cmp;
3664 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3666 case WITH_CLEANUP_EXPR:
3667 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3668 if (cmp <= 0)
3669 return cmp;
3671 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
3673 case COMPONENT_REF:
3674 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
3675 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3677 return 0;
3679 case VAR_DECL:
3680 case PARM_DECL:
3681 case CONST_DECL:
3682 case FUNCTION_DECL:
3683 return 0;
3685 default:
3686 break;
3689 /* This general rule works for most tree codes. All exceptions should be
3690 handled above. If this is a language-specific tree code, we can't
3691 trust what might be in the operand, so say we don't know
3692 the situation. */
3693 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
3694 return -1;
3696 switch (TREE_CODE_CLASS (code1))
3698 case '1':
3699 case '2':
3700 case '<':
3701 case 'e':
3702 case 'r':
3703 case 's':
3704 cmp = 1;
3705 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
3707 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
3708 if (cmp <= 0)
3709 return cmp;
3712 return cmp;
3714 default:
3715 return -1;
3719 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
3720 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
3721 than U, respectively. */
3724 compare_tree_int (tree t, unsigned HOST_WIDE_INT u)
3726 if (tree_int_cst_sgn (t) < 0)
3727 return -1;
3728 else if (TREE_INT_CST_HIGH (t) != 0)
3729 return 1;
3730 else if (TREE_INT_CST_LOW (t) == u)
3731 return 0;
3732 else if (TREE_INT_CST_LOW (t) < u)
3733 return -1;
3734 else
3735 return 1;
3738 /* Return true if CODE represents an associative tree code. Otherwise
3739 return false. */
3740 bool
3741 associative_tree_code (enum tree_code code)
3743 switch (code)
3745 case BIT_IOR_EXPR:
3746 case BIT_AND_EXPR:
3747 case BIT_XOR_EXPR:
3748 case PLUS_EXPR:
3749 case MINUS_EXPR:
3750 case MULT_EXPR:
3751 case LSHIFT_EXPR:
3752 case RSHIFT_EXPR:
3753 case MIN_EXPR:
3754 case MAX_EXPR:
3755 return true;
3757 default:
3758 break;
3760 return false;
3763 /* Return true if CODE represents an commutative tree code. Otherwise
3764 return false. */
3765 bool
3766 commutative_tree_code (enum tree_code code)
3768 switch (code)
3770 case PLUS_EXPR:
3771 case MULT_EXPR:
3772 case MIN_EXPR:
3773 case MAX_EXPR:
3774 case BIT_IOR_EXPR:
3775 case BIT_XOR_EXPR:
3776 case BIT_AND_EXPR:
3777 case NE_EXPR:
3778 case EQ_EXPR:
3779 return true;
3781 default:
3782 break;
3784 return false;
3787 /* Generate a hash value for an expression. This can be used iteratively
3788 by passing a previous result as the "val" argument.
3790 This function is intended to produce the same hash for expressions which
3791 would compare equal using operand_equal_p. */
3793 hashval_t
3794 iterative_hash_expr (tree t, hashval_t val)
3796 int i;
3797 enum tree_code code;
3798 char class;
3800 if (t == NULL_TREE)
3801 return iterative_hash_object (t, val);
3803 code = TREE_CODE (t);
3804 class = TREE_CODE_CLASS (code);
3806 if (class == 'd')
3808 /* Decls we can just compare by pointer. */
3809 val = iterative_hash_object (t, val);
3811 else if (class == 'c')
3813 /* Alas, constants aren't shared, so we can't rely on pointer
3814 identity. */
3815 if (code == INTEGER_CST)
3817 val = iterative_hash_object (TREE_INT_CST_LOW (t), val);
3818 val = iterative_hash_object (TREE_INT_CST_HIGH (t), val);
3820 else if (code == REAL_CST)
3821 val = iterative_hash (TREE_REAL_CST_PTR (t),
3822 sizeof (REAL_VALUE_TYPE), val);
3823 else if (code == STRING_CST)
3824 val = iterative_hash (TREE_STRING_POINTER (t),
3825 TREE_STRING_LENGTH (t), val);
3826 else if (code == COMPLEX_CST)
3828 val = iterative_hash_expr (TREE_REALPART (t), val);
3829 val = iterative_hash_expr (TREE_IMAGPART (t), val);
3831 else if (code == VECTOR_CST)
3832 val = iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
3833 else
3834 abort ();
3836 else if (IS_EXPR_CODE_CLASS (class))
3838 val = iterative_hash_object (code, val);
3840 if (code == NOP_EXPR || code == CONVERT_EXPR
3841 || code == NON_LVALUE_EXPR)
3842 val = iterative_hash_object (TREE_TYPE (t), val);
3844 if (commutative_tree_code (code))
3846 /* It's a commutative expression. We want to hash it the same
3847 however it appears. We do this by first hashing both operands
3848 and then rehashing based on the order of their independent
3849 hashes. */
3850 hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
3851 hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
3852 hashval_t t;
3854 if (one > two)
3855 t = one, one = two, two = t;
3857 val = iterative_hash_object (one, val);
3858 val = iterative_hash_object (two, val);
3860 else
3861 for (i = first_rtl_op (code) - 1; i >= 0; --i)
3862 val = iterative_hash_expr (TREE_OPERAND (t, i), val);
3864 else if (code == TREE_LIST)
3866 /* A list of expressions, for a CALL_EXPR or as the elements of a
3867 VECTOR_CST. */
3868 for (; t; t = TREE_CHAIN (t))
3869 val = iterative_hash_expr (TREE_VALUE (t), val);
3871 else
3872 abort ();
3874 return val;
3877 /* Constructors for pointer, array and function types.
3878 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
3879 constructed by language-dependent code, not here.) */
3881 /* Construct, lay out and return the type of pointers to TO_TYPE
3882 with mode MODE. If such a type has already been constructed,
3883 reuse it. */
3885 tree
3886 build_pointer_type_for_mode (tree to_type, enum machine_mode mode)
3888 tree t = TYPE_POINTER_TO (to_type);
3890 /* First, if we already have a type for pointers to TO_TYPE and it's
3891 the proper mode, use it. */
3892 if (t != 0 && mode == ptr_mode)
3893 return t;
3895 t = make_node (POINTER_TYPE);
3897 TREE_TYPE (t) = to_type;
3898 TYPE_MODE (t) = mode;
3900 /* We can only record one type as "the" pointer to TO_TYPE. We choose to
3901 record the pointer whose mode is ptr_mode. */
3902 if (mode == ptr_mode)
3903 TYPE_POINTER_TO (to_type) = t;
3905 /* Lay out the type. This function has many callers that are concerned
3906 with expression-construction, and this simplifies them all. */
3907 layout_type (t);
3909 return t;
3912 /* By default build pointers in ptr_mode. */
3914 tree
3915 build_pointer_type (tree to_type)
3917 return build_pointer_type_for_mode (to_type, ptr_mode);
3920 /* Construct, lay out and return the type of references to TO_TYPE
3921 with mode MODE. If such a type has already been constructed,
3922 reuse it. */
3924 tree
3925 build_reference_type_for_mode (tree to_type, enum machine_mode mode)
3927 tree t = TYPE_REFERENCE_TO (to_type);
3929 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3930 if (t != 0 && mode == ptr_mode)
3931 return t;
3933 t = make_node (REFERENCE_TYPE);
3935 TREE_TYPE (t) = to_type;
3936 TYPE_MODE (t) = mode;
3938 /* Record this type as the pointer to TO_TYPE. */
3939 if (mode == ptr_mode)
3940 TYPE_REFERENCE_TO (to_type) = t;
3942 layout_type (t);
3944 return t;
3948 /* Build the node for the type of references-to-TO_TYPE by default
3949 in ptr_mode. */
3951 tree
3952 build_reference_type (tree to_type)
3954 return build_reference_type_for_mode (to_type, ptr_mode);
3957 /* Build a type that is compatible with t but has no cv quals anywhere
3958 in its type, thus
3960 const char *const *const * -> char ***. */
3962 tree
3963 build_type_no_quals (tree t)
3965 switch (TREE_CODE (t))
3967 case POINTER_TYPE:
3968 return build_pointer_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
3969 TYPE_MODE (t));
3970 case REFERENCE_TYPE:
3971 return
3972 build_reference_type_for_mode (build_type_no_quals (TREE_TYPE (t)),
3973 TYPE_MODE (t));
3974 default:
3975 return TYPE_MAIN_VARIANT (t);
3979 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
3980 MAXVAL should be the maximum value in the domain
3981 (one less than the length of the array).
3983 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
3984 We don't enforce this limit, that is up to caller (e.g. language front end).
3985 The limit exists because the result is a signed type and we don't handle
3986 sizes that use more than one HOST_WIDE_INT. */
3988 tree
3989 build_index_type (tree maxval)
3991 tree itype = make_node (INTEGER_TYPE);
3993 TREE_TYPE (itype) = sizetype;
3994 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
3995 TYPE_MIN_VALUE (itype) = size_zero_node;
3996 TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
3997 TYPE_MODE (itype) = TYPE_MODE (sizetype);
3998 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
3999 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
4000 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
4001 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
4003 if (host_integerp (maxval, 1))
4004 return type_hash_canon (tree_low_cst (maxval, 1), itype);
4005 else
4006 return itype;
4009 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
4010 ENUMERAL_TYPE, BOOLEAN_TYPE, or CHAR_TYPE), with
4011 low bound LOWVAL and high bound HIGHVAL.
4012 if TYPE==NULL_TREE, sizetype is used. */
4014 tree
4015 build_range_type (tree type, tree lowval, tree highval)
4017 tree itype = make_node (INTEGER_TYPE);
4019 TREE_TYPE (itype) = type;
4020 if (type == NULL_TREE)
4021 type = sizetype;
4023 TYPE_MIN_VALUE (itype) = convert (type, lowval);
4024 TYPE_MAX_VALUE (itype) = highval ? convert (type, highval) : NULL;
4026 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
4027 TYPE_MODE (itype) = TYPE_MODE (type);
4028 TYPE_SIZE (itype) = TYPE_SIZE (type);
4029 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
4030 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
4031 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
4033 if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
4034 return type_hash_canon (tree_low_cst (highval, 0)
4035 - tree_low_cst (lowval, 0),
4036 itype);
4037 else
4038 return itype;
4041 /* Just like build_index_type, but takes lowval and highval instead
4042 of just highval (maxval). */
4044 tree
4045 build_index_2_type (tree lowval, tree highval)
4047 return build_range_type (sizetype, lowval, highval);
4050 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
4051 and number of elements specified by the range of values of INDEX_TYPE.
4052 If such a type has already been constructed, reuse it. */
4054 tree
4055 build_array_type (tree elt_type, tree index_type)
4057 tree t;
4058 hashval_t hashcode = 0;
4060 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
4062 error ("arrays of functions are not meaningful");
4063 elt_type = integer_type_node;
4066 t = make_node (ARRAY_TYPE);
4067 TREE_TYPE (t) = elt_type;
4068 TYPE_DOMAIN (t) = index_type;
4070 if (index_type == 0)
4071 return t;
4073 hashcode = iterative_hash_object (TYPE_HASH (elt_type), hashcode);
4074 hashcode = iterative_hash_object (TYPE_HASH (index_type), hashcode);
4075 t = type_hash_canon (hashcode, t);
4077 if (!COMPLETE_TYPE_P (t))
4078 layout_type (t);
4079 return t;
4082 /* Return the TYPE of the elements comprising
4083 the innermost dimension of ARRAY. */
4085 tree
4086 get_inner_array_type (tree array)
4088 tree type = TREE_TYPE (array);
4090 while (TREE_CODE (type) == ARRAY_TYPE)
4091 type = TREE_TYPE (type);
4093 return type;
4096 /* Construct, lay out and return
4097 the type of functions returning type VALUE_TYPE
4098 given arguments of types ARG_TYPES.
4099 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
4100 are data type nodes for the arguments of the function.
4101 If such a type has already been constructed, reuse it. */
4103 tree
4104 build_function_type (tree value_type, tree arg_types)
4106 tree t;
4107 hashval_t hashcode = 0;
4109 if (TREE_CODE (value_type) == FUNCTION_TYPE)
4111 error ("function return type cannot be function");
4112 value_type = integer_type_node;
4115 /* Make a node of the sort we want. */
4116 t = make_node (FUNCTION_TYPE);
4117 TREE_TYPE (t) = value_type;
4118 TYPE_ARG_TYPES (t) = arg_types;
4120 /* If we already have such a type, use the old one. */
4121 hashcode = iterative_hash_object (TYPE_HASH (value_type), hashcode);
4122 hashcode = type_hash_list (arg_types, hashcode);
4123 t = type_hash_canon (hashcode, t);
4125 if (!COMPLETE_TYPE_P (t))
4126 layout_type (t);
4127 return t;
4130 /* Build a function type. The RETURN_TYPE is the type returned by the
4131 function. If additional arguments are provided, they are
4132 additional argument types. The list of argument types must always
4133 be terminated by NULL_TREE. */
4135 tree
4136 build_function_type_list (tree return_type, ...)
4138 tree t, args, last;
4139 va_list p;
4141 va_start (p, return_type);
4143 t = va_arg (p, tree);
4144 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (p, tree))
4145 args = tree_cons (NULL_TREE, t, args);
4147 last = args;
4148 args = nreverse (args);
4149 TREE_CHAIN (last) = void_list_node;
4150 args = build_function_type (return_type, args);
4152 va_end (p);
4153 return args;
4156 /* Build a METHOD_TYPE for a member of BASETYPE. The RETTYPE (a TYPE)
4157 and ARGTYPES (a TREE_LIST) are the return type and arguments types
4158 for the method. An implicit additional parameter (of type
4159 pointer-to-BASETYPE) is added to the ARGTYPES. */
4161 tree
4162 build_method_type_directly (tree basetype,
4163 tree rettype,
4164 tree argtypes)
4166 tree t;
4167 tree ptype;
4168 int hashcode = 0;
4170 /* Make a node of the sort we want. */
4171 t = make_node (METHOD_TYPE);
4173 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
4174 TREE_TYPE (t) = rettype;
4175 ptype = build_pointer_type (basetype);
4177 /* The actual arglist for this function includes a "hidden" argument
4178 which is "this". Put it into the list of argument types. */
4179 argtypes = tree_cons (NULL_TREE, ptype, argtypes);
4180 TYPE_ARG_TYPES (t) = argtypes;
4182 /* If we already have such a type, use the old one. */
4183 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
4184 hashcode = iterative_hash_object (TYPE_HASH (rettype), hashcode);
4185 hashcode = type_hash_list (argtypes, hashcode);
4186 t = type_hash_canon (hashcode, t);
4188 if (!COMPLETE_TYPE_P (t))
4189 layout_type (t);
4191 return t;
4194 /* Construct, lay out and return the type of methods belonging to class
4195 BASETYPE and whose arguments and values are described by TYPE.
4196 If that type exists already, reuse it.
4197 TYPE must be a FUNCTION_TYPE node. */
4199 tree
4200 build_method_type (tree basetype, tree type)
4202 if (TREE_CODE (type) != FUNCTION_TYPE)
4203 abort ();
4205 return build_method_type_directly (basetype,
4206 TREE_TYPE (type),
4207 TYPE_ARG_TYPES (type));
4210 /* Construct, lay out and return the type of offsets to a value
4211 of type TYPE, within an object of type BASETYPE.
4212 If a suitable offset type exists already, reuse it. */
4214 tree
4215 build_offset_type (tree basetype, tree type)
4217 tree t;
4218 hashval_t hashcode = 0;
4220 /* Make a node of the sort we want. */
4221 t = make_node (OFFSET_TYPE);
4223 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
4224 TREE_TYPE (t) = type;
4226 /* If we already have such a type, use the old one. */
4227 hashcode = iterative_hash_object (TYPE_HASH (basetype), hashcode);
4228 hashcode = iterative_hash_object (TYPE_HASH (type), hashcode);
4229 t = type_hash_canon (hashcode, t);
4231 if (!COMPLETE_TYPE_P (t))
4232 layout_type (t);
4234 return t;
4237 /* Create a complex type whose components are COMPONENT_TYPE. */
4239 tree
4240 build_complex_type (tree component_type)
4242 tree t;
4243 hashval_t hashcode;
4245 /* Make a node of the sort we want. */
4246 t = make_node (COMPLEX_TYPE);
4248 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
4250 /* If we already have such a type, use the old one. */
4251 hashcode = iterative_hash_object (TYPE_HASH (component_type), 0);
4252 t = type_hash_canon (hashcode, t);
4254 if (!COMPLETE_TYPE_P (t))
4255 layout_type (t);
4257 /* If we are writing Dwarf2 output we need to create a name,
4258 since complex is a fundamental type. */
4259 if ((write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
4260 && ! TYPE_NAME (t))
4262 const char *name;
4263 if (component_type == char_type_node)
4264 name = "complex char";
4265 else if (component_type == signed_char_type_node)
4266 name = "complex signed char";
4267 else if (component_type == unsigned_char_type_node)
4268 name = "complex unsigned char";
4269 else if (component_type == short_integer_type_node)
4270 name = "complex short int";
4271 else if (component_type == short_unsigned_type_node)
4272 name = "complex short unsigned int";
4273 else if (component_type == integer_type_node)
4274 name = "complex int";
4275 else if (component_type == unsigned_type_node)
4276 name = "complex unsigned int";
4277 else if (component_type == long_integer_type_node)
4278 name = "complex long int";
4279 else if (component_type == long_unsigned_type_node)
4280 name = "complex long unsigned int";
4281 else if (component_type == long_long_integer_type_node)
4282 name = "complex long long int";
4283 else if (component_type == long_long_unsigned_type_node)
4284 name = "complex long long unsigned int";
4285 else
4286 name = 0;
4288 if (name != 0)
4289 TYPE_NAME (t) = get_identifier (name);
4292 return build_qualified_type (t, TYPE_QUALS (component_type));
4295 /* Return OP, stripped of any conversions to wider types as much as is safe.
4296 Converting the value back to OP's type makes a value equivalent to OP.
4298 If FOR_TYPE is nonzero, we return a value which, if converted to
4299 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
4301 If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
4302 narrowest type that can hold the value, even if they don't exactly fit.
4303 Otherwise, bit-field references are changed to a narrower type
4304 only if they can be fetched directly from memory in that type.
4306 OP must have integer, real or enumeral type. Pointers are not allowed!
4308 There are some cases where the obvious value we could return
4309 would regenerate to OP if converted to OP's type,
4310 but would not extend like OP to wider types.
4311 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
4312 For example, if OP is (unsigned short)(signed char)-1,
4313 we avoid returning (signed char)-1 if FOR_TYPE is int,
4314 even though extending that to an unsigned short would regenerate OP,
4315 since the result of extending (signed char)-1 to (int)
4316 is different from (int) OP. */
4318 tree
4319 get_unwidened (tree op, tree for_type)
4321 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
4322 tree type = TREE_TYPE (op);
4323 unsigned final_prec
4324 = TYPE_PRECISION (for_type != 0 ? for_type : type);
4325 int uns
4326 = (for_type != 0 && for_type != type
4327 && final_prec > TYPE_PRECISION (type)
4328 && TREE_UNSIGNED (type));
4329 tree win = op;
4331 while (TREE_CODE (op) == NOP_EXPR)
4333 int bitschange
4334 = TYPE_PRECISION (TREE_TYPE (op))
4335 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
4337 /* Truncations are many-one so cannot be removed.
4338 Unless we are later going to truncate down even farther. */
4339 if (bitschange < 0
4340 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
4341 break;
4343 /* See what's inside this conversion. If we decide to strip it,
4344 we will set WIN. */
4345 op = TREE_OPERAND (op, 0);
4347 /* If we have not stripped any zero-extensions (uns is 0),
4348 we can strip any kind of extension.
4349 If we have previously stripped a zero-extension,
4350 only zero-extensions can safely be stripped.
4351 Any extension can be stripped if the bits it would produce
4352 are all going to be discarded later by truncating to FOR_TYPE. */
4354 if (bitschange > 0)
4356 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
4357 win = op;
4358 /* TREE_UNSIGNED says whether this is a zero-extension.
4359 Let's avoid computing it if it does not affect WIN
4360 and if UNS will not be needed again. */
4361 if ((uns || TREE_CODE (op) == NOP_EXPR)
4362 && TREE_UNSIGNED (TREE_TYPE (op)))
4364 uns = 1;
4365 win = op;
4370 if (TREE_CODE (op) == COMPONENT_REF
4371 /* Since type_for_size always gives an integer type. */
4372 && TREE_CODE (type) != REAL_TYPE
4373 /* Don't crash if field not laid out yet. */
4374 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
4375 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
4377 unsigned int innerprec
4378 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4379 int unsignedp = (TREE_UNSIGNED (TREE_OPERAND (op, 1))
4380 || TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
4381 type = lang_hooks.types.type_for_size (innerprec, unsignedp);
4383 /* We can get this structure field in the narrowest type it fits in.
4384 If FOR_TYPE is 0, do this only for a field that matches the
4385 narrower type exactly and is aligned for it
4386 The resulting extension to its nominal type (a fullword type)
4387 must fit the same conditions as for other extensions. */
4389 if (type != 0
4390 && INT_CST_LT_UNSIGNED (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (op)))
4391 && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
4392 && (! uns || final_prec <= innerprec || unsignedp))
4394 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4395 TREE_OPERAND (op, 1));
4396 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4397 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4401 return win;
4404 /* Return OP or a simpler expression for a narrower value
4405 which can be sign-extended or zero-extended to give back OP.
4406 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
4407 or 0 if the value should be sign-extended. */
4409 tree
4410 get_narrower (tree op, int *unsignedp_ptr)
4412 int uns = 0;
4413 int first = 1;
4414 tree win = op;
4416 while (TREE_CODE (op) == NOP_EXPR)
4418 int bitschange
4419 = (TYPE_PRECISION (TREE_TYPE (op))
4420 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
4422 /* Truncations are many-one so cannot be removed. */
4423 if (bitschange < 0)
4424 break;
4426 /* See what's inside this conversion. If we decide to strip it,
4427 we will set WIN. */
4429 if (bitschange > 0)
4431 op = TREE_OPERAND (op, 0);
4432 /* An extension: the outermost one can be stripped,
4433 but remember whether it is zero or sign extension. */
4434 if (first)
4435 uns = TREE_UNSIGNED (TREE_TYPE (op));
4436 /* Otherwise, if a sign extension has been stripped,
4437 only sign extensions can now be stripped;
4438 if a zero extension has been stripped, only zero-extensions. */
4439 else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
4440 break;
4441 first = 0;
4443 else /* bitschange == 0 */
4445 /* A change in nominal type can always be stripped, but we must
4446 preserve the unsignedness. */
4447 if (first)
4448 uns = TREE_UNSIGNED (TREE_TYPE (op));
4449 first = 0;
4450 op = TREE_OPERAND (op, 0);
4453 win = op;
4456 if (TREE_CODE (op) == COMPONENT_REF
4457 /* Since type_for_size always gives an integer type. */
4458 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
4459 /* Ensure field is laid out already. */
4460 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0)
4462 unsigned HOST_WIDE_INT innerprec
4463 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4464 int unsignedp = (TREE_UNSIGNED (TREE_OPERAND (op, 1))
4465 || TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
4466 tree type = lang_hooks.types.type_for_size (innerprec, unsignedp);
4468 /* We can get this structure field in a narrower type that fits it,
4469 but the resulting extension to its nominal type (a fullword type)
4470 must satisfy the same conditions as for other extensions.
4472 Do this only for fields that are aligned (not bit-fields),
4473 because when bit-field insns will be used there is no
4474 advantage in doing this. */
4476 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
4477 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
4478 && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
4479 && type != 0)
4481 if (first)
4482 uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
4483 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4484 TREE_OPERAND (op, 1));
4485 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4486 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4489 *unsignedp_ptr = uns;
4490 return win;
4493 /* Nonzero if integer constant C has a value that is permissible
4494 for type TYPE (an INTEGER_TYPE). */
4497 int_fits_type_p (tree c, tree type)
4499 tree type_low_bound = TYPE_MIN_VALUE (type);
4500 tree type_high_bound = TYPE_MAX_VALUE (type);
4501 int ok_for_low_bound, ok_for_high_bound;
4503 /* Perform some generic filtering first, which may allow making a decision
4504 even if the bounds are not constant. First, negative integers never fit
4505 in unsigned types, */
4506 if ((TREE_UNSIGNED (type) && tree_int_cst_sgn (c) < 0)
4507 /* Also, unsigned integers with top bit set never fit signed types. */
4508 || (! TREE_UNSIGNED (type)
4509 && TREE_UNSIGNED (TREE_TYPE (c)) && tree_int_cst_msb (c)))
4510 return 0;
4512 /* If at least one bound of the type is a constant integer, we can check
4513 ourselves and maybe make a decision. If no such decision is possible, but
4514 this type is a subtype, try checking against that. Otherwise, use
4515 force_fit_type, which checks against the precision.
4517 Compute the status for each possibly constant bound, and return if we see
4518 one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
4519 for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
4520 for "constant known to fit". */
4522 ok_for_low_bound = -1;
4523 ok_for_high_bound = -1;
4525 /* Check if C >= type_low_bound. */
4526 if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
4528 ok_for_low_bound = ! tree_int_cst_lt (c, type_low_bound);
4529 if (! ok_for_low_bound)
4530 return 0;
4533 /* Check if c <= type_high_bound. */
4534 if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
4536 ok_for_high_bound = ! tree_int_cst_lt (type_high_bound, c);
4537 if (! ok_for_high_bound)
4538 return 0;
4541 /* If the constant fits both bounds, the result is known. */
4542 if (ok_for_low_bound == 1 && ok_for_high_bound == 1)
4543 return 1;
4545 /* If we haven't been able to decide at this point, there nothing more we
4546 can check ourselves here. Look at the base type if we have one. */
4547 else if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != 0)
4548 return int_fits_type_p (c, TREE_TYPE (type));
4550 /* Or to force_fit_type, if nothing else. */
4551 else
4553 c = copy_node (c);
4554 TREE_TYPE (c) = type;
4555 return !force_fit_type (c, 0);
4559 /* Returns true if T is, contains, or refers to a type with variable
4560 size. This concept is more general than that of C99 'variably
4561 modified types': in C99, a struct type is never variably modified
4562 because a VLA may not appear as a structure member. However, in
4563 GNU C code like:
4565 struct S { int i[f()]; };
4567 is valid, and other languages may define similar constructs. */
4569 bool
4570 variably_modified_type_p (tree type)
4572 tree t;
4574 if (type == error_mark_node)
4575 return false;
4577 /* If TYPE itself has variable size, it is variably modified.
4579 We do not yet have a representation of the C99 '[*]' syntax.
4580 When a representation is chosen, this function should be modified
4581 to test for that case as well. */
4582 t = TYPE_SIZE (type);
4583 if (t && t != error_mark_node && TREE_CODE (t) != INTEGER_CST)
4584 return true;
4586 switch (TREE_CODE (type))
4588 case POINTER_TYPE:
4589 case REFERENCE_TYPE:
4590 case ARRAY_TYPE:
4591 /* If TYPE is a pointer or reference, it is variably modified if
4592 the type pointed to is variably modified. Similarly for arrays;
4593 note that VLAs are handled by the TYPE_SIZE check above. */
4594 return variably_modified_type_p (TREE_TYPE (type));
4596 case FUNCTION_TYPE:
4597 case METHOD_TYPE:
4598 /* If TYPE is a function type, it is variably modified if any of the
4599 parameters or the return type are variably modified. */
4601 tree parm;
4603 if (variably_modified_type_p (TREE_TYPE (type)))
4604 return true;
4605 for (parm = TYPE_ARG_TYPES (type);
4606 parm && parm != void_list_node;
4607 parm = TREE_CHAIN (parm))
4608 if (variably_modified_type_p (TREE_VALUE (parm)))
4609 return true;
4611 break;
4613 case INTEGER_TYPE:
4614 /* Scalar types are variably modified if their end points
4615 aren't constant. */
4616 t = TYPE_MIN_VALUE (type);
4617 if (t && t != error_mark_node && TREE_CODE (t) != INTEGER_CST)
4618 return true;
4619 t = TYPE_MAX_VALUE (type);
4620 if (t && t != error_mark_node && TREE_CODE (t) != INTEGER_CST)
4621 return true;
4622 return false;
4624 default:
4625 break;
4628 /* The current language may have other cases to check, but in general,
4629 all other types are not variably modified. */
4630 return lang_hooks.tree_inlining.var_mod_type_p (type);
4633 /* Given a DECL or TYPE, return the scope in which it was declared, or
4634 NULL_TREE if there is no containing scope. */
4636 tree
4637 get_containing_scope (tree t)
4639 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
4642 /* Return the innermost context enclosing DECL that is
4643 a FUNCTION_DECL, or zero if none. */
4645 tree
4646 decl_function_context (tree decl)
4648 tree context;
4650 if (TREE_CODE (decl) == ERROR_MARK)
4651 return 0;
4653 if (TREE_CODE (decl) == SAVE_EXPR)
4654 context = SAVE_EXPR_CONTEXT (decl);
4656 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
4657 where we look up the function at runtime. Such functions always take
4658 a first argument of type 'pointer to real context'.
4660 C++ should really be fixed to use DECL_CONTEXT for the real context,
4661 and use something else for the "virtual context". */
4662 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
4663 context
4664 = TYPE_MAIN_VARIANT
4665 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4666 else
4667 context = DECL_CONTEXT (decl);
4669 while (context && TREE_CODE (context) != FUNCTION_DECL)
4671 if (TREE_CODE (context) == BLOCK)
4672 context = BLOCK_SUPERCONTEXT (context);
4673 else
4674 context = get_containing_scope (context);
4677 return context;
4680 /* Return the innermost context enclosing DECL that is
4681 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
4682 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
4684 tree
4685 decl_type_context (tree decl)
4687 tree context = DECL_CONTEXT (decl);
4689 while (context)
4690 switch (TREE_CODE (context))
4692 case NAMESPACE_DECL:
4693 case TRANSLATION_UNIT_DECL:
4694 return NULL_TREE;
4696 case RECORD_TYPE:
4697 case UNION_TYPE:
4698 case QUAL_UNION_TYPE:
4699 return context;
4701 case TYPE_DECL:
4702 case FUNCTION_DECL:
4703 context = DECL_CONTEXT (context);
4704 break;
4706 case BLOCK:
4707 context = BLOCK_SUPERCONTEXT (context);
4708 break;
4710 default:
4711 abort ();
4714 return NULL_TREE;
4717 /* CALL is a CALL_EXPR. Return the declaration for the function
4718 called, or NULL_TREE if the called function cannot be
4719 determined. */
4721 tree
4722 get_callee_fndecl (tree call)
4724 tree addr;
4726 /* It's invalid to call this function with anything but a
4727 CALL_EXPR. */
4728 if (TREE_CODE (call) != CALL_EXPR)
4729 abort ();
4731 /* The first operand to the CALL is the address of the function
4732 called. */
4733 addr = TREE_OPERAND (call, 0);
4735 STRIP_NOPS (addr);
4737 /* If this is a readonly function pointer, extract its initial value. */
4738 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
4739 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
4740 && DECL_INITIAL (addr))
4741 addr = DECL_INITIAL (addr);
4743 /* If the address is just `&f' for some function `f', then we know
4744 that `f' is being called. */
4745 if (TREE_CODE (addr) == ADDR_EXPR
4746 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
4747 return TREE_OPERAND (addr, 0);
4749 /* We couldn't figure out what was being called. Maybe the front
4750 end has some idea. */
4751 return lang_hooks.lang_get_callee_fndecl (call);
4754 /* Print debugging information about tree nodes generated during the compile,
4755 and any language-specific information. */
4757 void
4758 dump_tree_statistics (void)
4760 #ifdef GATHER_STATISTICS
4761 int i;
4762 int total_nodes, total_bytes;
4763 #endif
4765 fprintf (stderr, "\n??? tree nodes created\n\n");
4766 #ifdef GATHER_STATISTICS
4767 fprintf (stderr, "Kind Nodes Bytes\n");
4768 fprintf (stderr, "---------------------------------------\n");
4769 total_nodes = total_bytes = 0;
4770 for (i = 0; i < (int) all_kinds; i++)
4772 fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
4773 tree_node_counts[i], tree_node_sizes[i]);
4774 total_nodes += tree_node_counts[i];
4775 total_bytes += tree_node_sizes[i];
4777 fprintf (stderr, "---------------------------------------\n");
4778 fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
4779 fprintf (stderr, "---------------------------------------\n");
4780 #else
4781 fprintf (stderr, "(No per-node statistics)\n");
4782 #endif
4783 print_type_hash_statistics ();
4784 lang_hooks.print_statistics ();
4787 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
4789 /* Generate a crc32 of a string. */
4791 unsigned
4792 crc32_string (unsigned chksum, const char *string)
4796 unsigned value = *string << 24;
4797 unsigned ix;
4799 for (ix = 8; ix--; value <<= 1)
4801 unsigned feedback;
4803 feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
4804 chksum <<= 1;
4805 chksum ^= feedback;
4808 while (*string++);
4809 return chksum;
4812 /* P is a string that will be used in a symbol. Mask out any characters
4813 that are not valid in that context. */
4815 void
4816 clean_symbol_name (char *p)
4818 for (; *p; p++)
4819 if (! (ISALNUM (*p)
4820 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
4821 || *p == '$'
4822 #endif
4823 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
4824 || *p == '.'
4825 #endif
4827 *p = '_';
4830 /* Generate a name for a function unique to this translation unit.
4831 TYPE is some string to identify the purpose of this function to the
4832 linker or collect2. */
4834 tree
4835 get_file_function_name_long (const char *type)
4837 char *buf;
4838 const char *p;
4839 char *q;
4841 if (first_global_object_name)
4842 p = first_global_object_name;
4843 else
4845 /* We don't have anything that we know to be unique to this translation
4846 unit, so use what we do have and throw in some randomness. */
4847 unsigned len;
4848 const char *name = weak_global_object_name;
4849 const char *file = main_input_filename;
4851 if (! name)
4852 name = "";
4853 if (! file)
4854 file = input_filename;
4856 len = strlen (file);
4857 q = alloca (9 * 2 + len + 1);
4858 memcpy (q, file, len + 1);
4859 clean_symbol_name (q);
4861 sprintf (q + len, "_%08X_%08X", crc32_string (0, name),
4862 crc32_string (0, flag_random_seed));
4864 p = q;
4867 buf = alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p) + strlen (type));
4869 /* Set up the name of the file-level functions we may need.
4870 Use a global object (which is already required to be unique over
4871 the program) rather than the file name (which imposes extra
4872 constraints). */
4873 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
4875 return get_identifier (buf);
4878 /* If KIND=='I', return a suitable global initializer (constructor) name.
4879 If KIND=='D', return a suitable global clean-up (destructor) name. */
4881 tree
4882 get_file_function_name (int kind)
4884 char p[2];
4886 p[0] = kind;
4887 p[1] = 0;
4889 return get_file_function_name_long (p);
4892 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4893 The result is placed in BUFFER (which has length BIT_SIZE),
4894 with one bit in each char ('\000' or '\001').
4896 If the constructor is constant, NULL_TREE is returned.
4897 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4899 tree
4900 get_set_constructor_bits (tree init, char *buffer, int bit_size)
4902 int i;
4903 tree vals;
4904 HOST_WIDE_INT domain_min
4905 = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (init))), 0);
4906 tree non_const_bits = NULL_TREE;
4908 for (i = 0; i < bit_size; i++)
4909 buffer[i] = 0;
4911 for (vals = TREE_OPERAND (init, 1);
4912 vals != NULL_TREE; vals = TREE_CHAIN (vals))
4914 if (!host_integerp (TREE_VALUE (vals), 0)
4915 || (TREE_PURPOSE (vals) != NULL_TREE
4916 && !host_integerp (TREE_PURPOSE (vals), 0)))
4917 non_const_bits
4918 = tree_cons (TREE_PURPOSE (vals), TREE_VALUE (vals), non_const_bits);
4919 else if (TREE_PURPOSE (vals) != NULL_TREE)
4921 /* Set a range of bits to ones. */
4922 HOST_WIDE_INT lo_index
4923 = tree_low_cst (TREE_PURPOSE (vals), 0) - domain_min;
4924 HOST_WIDE_INT hi_index
4925 = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
4927 if (lo_index < 0 || lo_index >= bit_size
4928 || hi_index < 0 || hi_index >= bit_size)
4929 abort ();
4930 for (; lo_index <= hi_index; lo_index++)
4931 buffer[lo_index] = 1;
4933 else
4935 /* Set a single bit to one. */
4936 HOST_WIDE_INT index
4937 = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
4938 if (index < 0 || index >= bit_size)
4940 error ("invalid initializer for bit string");
4941 return NULL_TREE;
4943 buffer[index] = 1;
4946 return non_const_bits;
4949 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4950 The result is placed in BUFFER (which is an array of bytes).
4951 If the constructor is constant, NULL_TREE is returned.
4952 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4954 tree
4955 get_set_constructor_bytes (tree init, unsigned char *buffer, int wd_size)
4957 int i;
4958 int set_word_size = BITS_PER_UNIT;
4959 int bit_size = wd_size * set_word_size;
4960 int bit_pos = 0;
4961 unsigned char *bytep = buffer;
4962 char *bit_buffer = alloca (bit_size);
4963 tree non_const_bits = get_set_constructor_bits (init, bit_buffer, bit_size);
4965 for (i = 0; i < wd_size; i++)
4966 buffer[i] = 0;
4968 for (i = 0; i < bit_size; i++)
4970 if (bit_buffer[i])
4972 if (BYTES_BIG_ENDIAN)
4973 *bytep |= (1 << (set_word_size - 1 - bit_pos));
4974 else
4975 *bytep |= 1 << bit_pos;
4977 bit_pos++;
4978 if (bit_pos >= set_word_size)
4979 bit_pos = 0, bytep++;
4981 return non_const_bits;
4984 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4986 /* Complain that the tree code of NODE does not match the expected CODE.
4987 FILE, LINE, and FUNCTION are of the caller. */
4989 void
4990 tree_check_failed (const tree node, enum tree_code code, const char *file,
4991 int line, const char *function)
4993 internal_error ("tree check: expected %s, have %s in %s, at %s:%d",
4994 tree_code_name[code], tree_code_name[TREE_CODE (node)],
4995 function, trim_filename (file), line);
4998 /* Similar to above except that we allowed the code to be one of two
4999 different codes. */
5001 void
5002 tree_check2_failed (const tree node, enum tree_code code1,
5003 enum tree_code code2, const char *file,
5004 int line, const char *function)
5006 internal_error ("tree check: expected %s or %s, have %s in %s, at %s:%d",
5007 tree_code_name[code1], tree_code_name[code2],
5008 tree_code_name[TREE_CODE (node)],
5009 function, trim_filename (file), line);
5012 /* Likewise for three different codes. */
5014 void
5015 tree_check3_failed (const tree node, enum tree_code code1,
5016 enum tree_code code2, enum tree_code code3,
5017 const char *file, int line, const char *function)
5019 internal_error ("tree check: expected %s, %s or %s; have %s in %s, at %s:%d",
5020 tree_code_name[code1], tree_code_name[code2],
5021 tree_code_name[code3], tree_code_name[TREE_CODE (node)],
5022 function, trim_filename (file), line);
5025 /* ... and for five different codes. */
5027 void
5028 tree_check5_failed (const tree node, enum tree_code code1,
5029 enum tree_code code2, enum tree_code code3,
5030 enum tree_code code4, enum tree_code code5,
5031 const char *file, int line, const char *function)
5033 internal_error
5034 ("tree check: expected %s, %s, %s, %s or %s; have %s in %s, at %s:%d",
5035 tree_code_name[code1], tree_code_name[code2], tree_code_name[code3],
5036 tree_code_name[code4], tree_code_name[code5],
5037 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
5040 /* Similar to tree_check_failed, except that we check for a class of tree
5041 code, given in CL. */
5043 void
5044 tree_class_check_failed (const tree node, int cl, const char *file,
5045 int line, const char *function)
5047 internal_error
5048 ("tree check: expected class '%c', have '%c' (%s) in %s, at %s:%d",
5049 cl, TREE_CODE_CLASS (TREE_CODE (node)),
5050 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
5053 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
5054 (dynamically sized) vector. */
5056 void
5057 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
5058 const char *function)
5060 internal_error
5061 ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
5062 idx + 1, len, function, trim_filename (file), line);
5065 /* Similar to above, except that the check is for the bounds of the operand
5066 vector of an expression node. */
5068 void
5069 tree_operand_check_failed (int idx, enum tree_code code, const char *file,
5070 int line, const char *function)
5072 internal_error
5073 ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
5074 idx + 1, tree_code_name[code], TREE_CODE_LENGTH (code),
5075 function, trim_filename (file), line);
5077 #endif /* ENABLE_TREE_CHECKING */
5079 /* For a new vector type node T, build the information necessary for
5080 debugging output. */
5082 static void
5083 finish_vector_type (tree t)
5085 layout_type (t);
5088 tree index = build_int_2 (TYPE_VECTOR_SUBPARTS (t) - 1, 0);
5089 tree array = build_array_type (TREE_TYPE (t),
5090 build_index_type (index));
5091 tree rt = make_node (RECORD_TYPE);
5093 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
5094 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
5095 layout_type (rt);
5096 TYPE_DEBUG_REPRESENTATION_TYPE (t) = rt;
5097 /* In dwarfout.c, type lookup uses TYPE_UID numbers. We want to output
5098 the representation type, and we want to find that die when looking up
5099 the vector type. This is most easily achieved by making the TYPE_UID
5100 numbers equal. */
5101 TYPE_UID (rt) = TYPE_UID (t);
5105 /* Create nodes for all integer types (and error_mark_node) using the sizes
5106 of C datatypes. The caller should call set_sizetype soon after calling
5107 this function to select one of the types as sizetype. */
5109 void
5110 build_common_tree_nodes (int signed_char)
5112 error_mark_node = make_node (ERROR_MARK);
5113 TREE_TYPE (error_mark_node) = error_mark_node;
5115 initialize_sizetypes ();
5117 /* Define both `signed char' and `unsigned char'. */
5118 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
5119 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
5121 /* Define `char', which is like either `signed char' or `unsigned char'
5122 but not the same as either. */
5123 char_type_node
5124 = (signed_char
5125 ? make_signed_type (CHAR_TYPE_SIZE)
5126 : make_unsigned_type (CHAR_TYPE_SIZE));
5128 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
5129 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
5130 integer_type_node = make_signed_type (INT_TYPE_SIZE);
5131 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
5132 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
5133 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
5134 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
5135 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
5137 /* Define a boolean type. This type only represents boolean values but
5138 may be larger than char depending on the value of BOOL_TYPE_SIZE.
5139 Front ends which want to override this size (i.e. Java) can redefine
5140 boolean_type_node before calling build_common_tree_nodes_2. */
5141 boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
5142 TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
5143 TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
5144 TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
5145 TYPE_PRECISION (boolean_type_node) = 1;
5147 intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
5148 intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
5149 intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
5150 intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
5151 intTI_type_node = make_signed_type (GET_MODE_BITSIZE (TImode));
5153 unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
5154 unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
5155 unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
5156 unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
5157 unsigned_intTI_type_node = make_unsigned_type (GET_MODE_BITSIZE (TImode));
5159 access_public_node = get_identifier ("public");
5160 access_protected_node = get_identifier ("protected");
5161 access_private_node = get_identifier ("private");
5164 /* Call this function after calling build_common_tree_nodes and set_sizetype.
5165 It will create several other common tree nodes. */
5167 void
5168 build_common_tree_nodes_2 (int short_double)
5170 /* Define these next since types below may used them. */
5171 integer_zero_node = build_int_2 (0, 0);
5172 integer_one_node = build_int_2 (1, 0);
5173 integer_minus_one_node = build_int_2 (-1, -1);
5175 size_zero_node = size_int (0);
5176 size_one_node = size_int (1);
5177 bitsize_zero_node = bitsize_int (0);
5178 bitsize_one_node = bitsize_int (1);
5179 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
5181 boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
5182 boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
5184 void_type_node = make_node (VOID_TYPE);
5185 layout_type (void_type_node);
5187 /* We are not going to have real types in C with less than byte alignment,
5188 so we might as well not have any types that claim to have it. */
5189 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
5190 TYPE_USER_ALIGN (void_type_node) = 0;
5192 null_pointer_node = build_int_2 (0, 0);
5193 TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
5194 layout_type (TREE_TYPE (null_pointer_node));
5196 ptr_type_node = build_pointer_type (void_type_node);
5197 const_ptr_type_node
5198 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
5200 float_type_node = make_node (REAL_TYPE);
5201 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
5202 layout_type (float_type_node);
5204 double_type_node = make_node (REAL_TYPE);
5205 if (short_double)
5206 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
5207 else
5208 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
5209 layout_type (double_type_node);
5211 long_double_type_node = make_node (REAL_TYPE);
5212 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
5213 layout_type (long_double_type_node);
5215 float_ptr_type_node = build_pointer_type (float_type_node);
5216 double_ptr_type_node = build_pointer_type (double_type_node);
5217 long_double_ptr_type_node = build_pointer_type (long_double_type_node);
5218 integer_ptr_type_node = build_pointer_type (integer_type_node);
5220 complex_integer_type_node = make_node (COMPLEX_TYPE);
5221 TREE_TYPE (complex_integer_type_node) = integer_type_node;
5222 layout_type (complex_integer_type_node);
5224 complex_float_type_node = make_node (COMPLEX_TYPE);
5225 TREE_TYPE (complex_float_type_node) = float_type_node;
5226 layout_type (complex_float_type_node);
5228 complex_double_type_node = make_node (COMPLEX_TYPE);
5229 TREE_TYPE (complex_double_type_node) = double_type_node;
5230 layout_type (complex_double_type_node);
5232 complex_long_double_type_node = make_node (COMPLEX_TYPE);
5233 TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
5234 layout_type (complex_long_double_type_node);
5237 tree t = targetm.build_builtin_va_list ();
5239 /* Many back-ends define record types without setting TYPE_NAME.
5240 If we copied the record type here, we'd keep the original
5241 record type without a name. This breaks name mangling. So,
5242 don't copy record types and let c_common_nodes_and_builtins()
5243 declare the type to be __builtin_va_list. */
5244 if (TREE_CODE (t) != RECORD_TYPE)
5245 t = build_type_copy (t);
5247 va_list_type_node = t;
5251 /* HACK. GROSS. This is absolutely disgusting. I wish there was a
5252 better way.
5254 If we requested a pointer to a vector, build up the pointers that
5255 we stripped off while looking for the inner type. Similarly for
5256 return values from functions.
5258 The argument TYPE is the top of the chain, and BOTTOM is the
5259 new type which we will point to. */
5261 tree
5262 reconstruct_complex_type (tree type, tree bottom)
5264 tree inner, outer;
5266 if (POINTER_TYPE_P (type))
5268 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
5269 outer = build_pointer_type (inner);
5271 else if (TREE_CODE (type) == ARRAY_TYPE)
5273 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
5274 outer = build_array_type (inner, TYPE_DOMAIN (type));
5276 else if (TREE_CODE (type) == FUNCTION_TYPE)
5278 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
5279 outer = build_function_type (inner, TYPE_ARG_TYPES (type));
5281 else if (TREE_CODE (type) == METHOD_TYPE)
5283 inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
5284 outer = build_method_type_directly (TYPE_METHOD_BASETYPE (type),
5285 inner,
5286 TYPE_ARG_TYPES (type));
5288 else
5289 return bottom;
5291 TREE_READONLY (outer) = TREE_READONLY (type);
5292 TREE_THIS_VOLATILE (outer) = TREE_THIS_VOLATILE (type);
5294 return outer;
5297 /* Returns a vector tree node given a vector mode and inner type. */
5298 tree
5299 build_vector_type_for_mode (tree innertype, enum machine_mode mode)
5301 tree t;
5302 t = make_node (VECTOR_TYPE);
5303 TREE_TYPE (t) = innertype;
5304 TYPE_MODE (t) = mode;
5305 TREE_UNSIGNED (t) = TREE_UNSIGNED (innertype);
5306 finish_vector_type (t);
5307 return t;
5310 /* Similarly, but takes inner type and units. */
5312 tree
5313 build_vector_type (tree innertype, int nunits)
5315 enum machine_mode innermode = TYPE_MODE (innertype);
5316 enum machine_mode mode;
5318 if (GET_MODE_CLASS (innermode) == MODE_FLOAT)
5319 mode = MIN_MODE_VECTOR_FLOAT;
5320 else
5321 mode = MIN_MODE_VECTOR_INT;
5323 for (; mode != VOIDmode ; mode = GET_MODE_WIDER_MODE (mode))
5324 if (GET_MODE_NUNITS (mode) == nunits && GET_MODE_INNER (mode) == innermode)
5325 return build_vector_type_for_mode (innertype, mode);
5327 return NULL_TREE;
5330 /* Given an initializer INIT, return TRUE if INIT is zero or some
5331 aggregate of zeros. Otherwise return FALSE. */
5332 bool
5333 initializer_zerop (tree init)
5335 STRIP_NOPS (init);
5337 switch (TREE_CODE (init))
5339 case INTEGER_CST:
5340 return integer_zerop (init);
5341 case REAL_CST:
5342 return real_zerop (init)
5343 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
5344 case COMPLEX_CST:
5345 return integer_zerop (init)
5346 || (real_zerop (init)
5347 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
5348 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
5349 case CONSTRUCTOR:
5351 /* Set is empty if it has no elements. */
5352 if ((TREE_CODE (TREE_TYPE (init)) == SET_TYPE)
5353 && CONSTRUCTOR_ELTS (init))
5354 return false;
5356 if (AGGREGATE_TYPE_P (TREE_TYPE (init)))
5358 tree aggr_init = CONSTRUCTOR_ELTS (init);
5360 while (aggr_init)
5362 if (! initializer_zerop (TREE_VALUE (aggr_init)))
5363 return false;
5364 aggr_init = TREE_CHAIN (aggr_init);
5366 return true;
5368 return false;
5370 default:
5371 return false;
5375 #include "gt-tree.h"