OSDN Git Service

* gcc.c (LIBGCC_SPEC): If REAL_LIBGCC_SPEC is defined, and
[pf3gnuchains/gcc-fork.git] / gcc / tree.c
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.
4
5 This file is part of GCC.
6
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.
11
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.
16
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.  */
21
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.
28
29    It is intended to be language-independent, but occasionally
30    calls language-dependent routines defined (for C) in typecheck.c.  */
31
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"
48
49 /* obstack.[ch] explicitly declined to prototype this.  */
50 extern int _obstack_allocated_p (struct obstack *h, void *obj);
51
52 #ifdef GATHER_STATISTICS
53 /* Statistics-gathering stuff.  */
54
55 int tree_node_counts[(int) all_kinds];
56 int tree_node_sizes[(int) all_kinds];
57
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"
74 };
75 #endif /* GATHER_STATISTICS */
76
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;
81
82 /* Since we cannot rehash a type after it is in the table, we have to
83    keep the hash code.  */
84
85 struct type_hash GTY(())
86 {
87   unsigned long hash;
88   tree type;
89 };
90
91 /* Initial size of the hash table (rounded to next prime).  */
92 #define TYPE_HASH_INITIAL_SIZE 1000
93
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.  */
100
101 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
102      htab_t type_hash_table;
103
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
111 tree global_trees[TI_MAX];
112 tree integer_types[itk_none];
113 \f
114 /* Init tree.c.  */
115
116 void
117 init_ttree (void)
118 {
119   /* Initialize the hash table of types.  */
120   type_hash_table = htab_create_ggc (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
121                                      type_hash_eq, 0);
122 }
123
124 \f
125 /* The name of the object as the assembler will see it (but before any
126    translations made by ASM_OUTPUT_LABELREF).  Often this is the same
127    as DECL_NAME.  It is an IDENTIFIER_NODE.  */
128 tree
129 decl_assembler_name (tree decl)
130 {
131   if (!DECL_ASSEMBLER_NAME_SET_P (decl))
132     (*lang_hooks.set_decl_assembler_name) (decl);
133   return DECL_CHECK (decl)->decl.assembler_name;
134 }
135
136 /* Compute the number of bytes occupied by 'node'.  This routine only
137    looks at TREE_CODE and, if the code is TREE_VEC, TREE_VEC_LENGTH.  */
138 size_t
139 tree_size (tree node)
140 {
141   enum tree_code code = TREE_CODE (node);
142
143   switch (TREE_CODE_CLASS (code))
144     {
145     case 'd':  /* A decl node */
146       return sizeof (struct tree_decl);
147
148     case 't':  /* a type node */
149       return sizeof (struct tree_type);
150
151     case 'b':  /* a lexical block node */
152       return sizeof (struct tree_block);
153
154     case 'r':  /* a reference */
155     case 'e':  /* an expression */
156     case 's':  /* an expression with side effects */
157     case '<':  /* a comparison expression */
158     case '1':  /* a unary arithmetic expression */
159     case '2':  /* a binary arithmetic expression */
160       return (sizeof (struct tree_exp)
161               + TREE_CODE_LENGTH (code) * sizeof (char *) - sizeof (char *));
162
163     case 'c':  /* a constant */
164       switch (code)
165         {
166         case INTEGER_CST:       return sizeof (struct tree_int_cst);
167         case REAL_CST:          return sizeof (struct tree_real_cst);
168         case COMPLEX_CST:       return sizeof (struct tree_complex);
169         case VECTOR_CST:        return sizeof (struct tree_vector);
170         case STRING_CST:        return sizeof (struct tree_string);
171         default:
172           return (*lang_hooks.tree_size) (code);
173         }
174
175     case 'x':  /* something random, like an identifier.  */
176       switch (code)
177         {
178         case IDENTIFIER_NODE:   return lang_hooks.identifier_size;
179         case TREE_LIST:         return sizeof (struct tree_list);
180         case TREE_VEC:          return (sizeof (struct tree_vec)
181                                         + TREE_VEC_LENGTH(node) * sizeof(char *)
182                                         - sizeof (char *));
183
184         case ERROR_MARK:
185         case PLACEHOLDER_EXPR:  return sizeof (struct tree_common);
186
187         default:
188           return (*lang_hooks.tree_size) (code);
189         }
190
191     default:
192       abort ();
193     }
194 }
195
196 /* Return a newly allocated node of code CODE.
197    For decl and type nodes, some other fields are initialized.
198    The rest of the node is initialized to zero.
199
200    Achoo!  I got a code in the node.  */
201
202 tree
203 make_node (enum tree_code code)
204 {
205   tree t;
206   int type = TREE_CODE_CLASS (code);
207   size_t length;
208 #ifdef GATHER_STATISTICS
209   tree_node_kind kind;
210 #endif
211   struct tree_common ttmp;
212
213   /* We can't allocate a TREE_VEC without knowing how many elements
214      it will have.  */
215   if (code == TREE_VEC)
216     abort ();
217
218   TREE_SET_CODE ((tree)&ttmp, code);
219   length = tree_size ((tree)&ttmp);
220
221 #ifdef GATHER_STATISTICS
222   switch (type)
223     {
224     case 'd':  /* A decl node */
225       kind = d_kind;
226       break;
227
228     case 't':  /* a type node */
229       kind = t_kind;
230       break;
231
232     case 'b':  /* a lexical block */
233       kind = b_kind;
234       break;
235
236     case 's':  /* an expression with side effects */
237       kind = s_kind;
238       break;
239
240     case 'r':  /* a reference */
241       kind = r_kind;
242       break;
243
244     case 'e':  /* an expression */
245     case '<':  /* a comparison expression */
246     case '1':  /* a unary arithmetic expression */
247     case '2':  /* a binary arithmetic expression */
248       kind = e_kind;
249       break;
250
251     case 'c':  /* a constant */
252       kind = c_kind;
253       break;
254
255     case 'x':  /* something random, like an identifier.  */
256       if (code == IDENTIFIER_NODE)
257         kind = id_kind;
258       else if (code == TREE_VEC)
259         kind = vec_kind;
260       else
261         kind = x_kind;
262       break;
263
264     default:
265       abort ();
266     }
267
268   tree_node_counts[(int) kind]++;
269   tree_node_sizes[(int) kind] += length;
270 #endif
271
272   t = ggc_alloc_tree (length);
273
274   memset (t, 0, length);
275
276   TREE_SET_CODE (t, code);
277
278   switch (type)
279     {
280     case 's':
281       TREE_SIDE_EFFECTS (t) = 1;
282       break;
283
284     case 'd':
285       if (code != FUNCTION_DECL)
286         DECL_ALIGN (t) = 1;
287       DECL_USER_ALIGN (t) = 0;
288       DECL_IN_SYSTEM_HEADER (t) = in_system_header;
289       DECL_SOURCE_LOCATION (t) = input_location;
290       DECL_UID (t) = next_decl_uid++;
291
292       /* We have not yet computed the alias set for this declaration.  */
293       DECL_POINTER_ALIAS_SET (t) = -1;
294       break;
295
296     case 't':
297       TYPE_UID (t) = next_type_uid++;
298       TYPE_ALIGN (t) = char_type_node ? TYPE_ALIGN (char_type_node) : 0;
299       TYPE_USER_ALIGN (t) = 0;
300       TYPE_MAIN_VARIANT (t) = t;
301
302       /* Default to no attributes for type, but let target change that.  */
303       TYPE_ATTRIBUTES (t) = NULL_TREE;
304       (*targetm.set_default_type_attributes) (t);
305
306       /* We have not yet computed the alias set for this type.  */
307       TYPE_ALIAS_SET (t) = -1;
308       break;
309
310     case 'c':
311       TREE_CONSTANT (t) = 1;
312       break;
313
314     case 'e':
315       switch (code)
316         {
317         case INIT_EXPR:
318         case MODIFY_EXPR:
319         case VA_ARG_EXPR:
320         case RTL_EXPR:
321         case PREDECREMENT_EXPR:
322         case PREINCREMENT_EXPR:
323         case POSTDECREMENT_EXPR:
324         case POSTINCREMENT_EXPR:
325           /* All of these have side-effects, no matter what their
326              operands are.  */
327           TREE_SIDE_EFFECTS (t) = 1;
328           break;
329
330         default:
331           break;
332         }
333       break;
334     }
335
336   return t;
337 }
338 \f
339 /* Return a new node with the same contents as NODE except that its
340    TREE_CHAIN is zero and it has a fresh uid.  */
341
342 tree
343 copy_node (tree node)
344 {
345   tree t;
346   enum tree_code code = TREE_CODE (node);
347   size_t length;
348
349   length = tree_size (node);
350   t = ggc_alloc_tree (length);
351   memcpy (t, node, length);
352
353   TREE_CHAIN (t) = 0;
354   TREE_ASM_WRITTEN (t) = 0;
355
356   if (TREE_CODE_CLASS (code) == 'd')
357     DECL_UID (t) = next_decl_uid++;
358   else if (TREE_CODE_CLASS (code) == 't')
359     {
360       TYPE_UID (t) = next_type_uid++;
361       /* The following is so that the debug code for
362          the copy is different from the original type.
363          The two statements usually duplicate each other
364          (because they clear fields of the same union),
365          but the optimizer should catch that.  */
366       TYPE_SYMTAB_POINTER (t) = 0;
367       TYPE_SYMTAB_ADDRESS (t) = 0;
368     }
369
370   return t;
371 }
372
373 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
374    For example, this can copy a list made of TREE_LIST nodes.  */
375
376 tree
377 copy_list (tree list)
378 {
379   tree head;
380   tree prev, next;
381
382   if (list == 0)
383     return 0;
384
385   head = prev = copy_node (list);
386   next = TREE_CHAIN (list);
387   while (next)
388     {
389       TREE_CHAIN (prev) = copy_node (next);
390       prev = TREE_CHAIN (prev);
391       next = TREE_CHAIN (next);
392     }
393   return head;
394 }
395
396 \f
397 /* Return a newly constructed INTEGER_CST node whose constant value
398    is specified by the two ints LOW and HI.
399    The TREE_TYPE is set to `int'.
400
401    This function should be used via the `build_int_2' macro.  */
402
403 tree
404 build_int_2_wide (unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
405 {
406   tree t = make_node (INTEGER_CST);
407
408   TREE_INT_CST_LOW (t) = low;
409   TREE_INT_CST_HIGH (t) = hi;
410   TREE_TYPE (t) = integer_type_node;
411   return t;
412 }
413
414 /* Return a new VECTOR_CST node whose type is TYPE and whose values
415    are in a list pointed by VALS.  */
416
417 tree
418 build_vector (tree type, tree vals)
419 {
420   tree v = make_node (VECTOR_CST);
421   int over1 = 0, over2 = 0;
422   tree link;
423
424   TREE_VECTOR_CST_ELTS (v) = vals;
425   TREE_TYPE (v) = type;
426
427   /* Iterate through elements and check for overflow.  */
428   for (link = vals; link; link = TREE_CHAIN (link))
429     {
430       tree value = TREE_VALUE (link);
431
432       over1 |= TREE_OVERFLOW (value);
433       over2 |= TREE_CONSTANT_OVERFLOW (value);
434     }
435
436   TREE_OVERFLOW (v) = over1;
437   TREE_CONSTANT_OVERFLOW (v) = over2;
438
439   return v;
440 }
441
442 /* Return a new CONSTRUCTOR node whose type is TYPE and whose values
443    are in a list pointed to by VALS.  */
444 tree
445 build_constructor (tree type, tree vals)
446 {
447   tree c = make_node (CONSTRUCTOR);
448   TREE_TYPE (c) = type;
449   CONSTRUCTOR_ELTS (c) = vals;
450
451   /* ??? May not be necessary.  Mirrors what build does.  */
452   if (vals)
453     {
454       TREE_SIDE_EFFECTS (c) = TREE_SIDE_EFFECTS (vals);
455       TREE_READONLY (c) = TREE_READONLY (vals);
456       TREE_CONSTANT (c) = TREE_CONSTANT (vals);
457     }
458   else
459     TREE_CONSTANT (c) = 0;  /* safe side */
460
461   return c;
462 }
463
464 /* Return a new REAL_CST node whose type is TYPE and value is D.  */
465
466 tree
467 build_real (tree type, REAL_VALUE_TYPE d)
468 {
469   tree v;
470   REAL_VALUE_TYPE *dp;
471   int overflow = 0;
472
473   /* ??? Used to check for overflow here via CHECK_FLOAT_TYPE.
474      Consider doing it via real_convert now.  */
475
476   v = make_node (REAL_CST);
477   dp = ggc_alloc (sizeof (REAL_VALUE_TYPE));
478   memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
479
480   TREE_TYPE (v) = type;
481   TREE_REAL_CST_PTR (v) = dp;
482   TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
483   return v;
484 }
485
486 /* Return a new REAL_CST node whose type is TYPE
487    and whose value is the integer value of the INTEGER_CST node I.  */
488
489 REAL_VALUE_TYPE
490 real_value_from_int_cst (tree type, tree i)
491 {
492   REAL_VALUE_TYPE d;
493
494   /* Clear all bits of the real value type so that we can later do
495      bitwise comparisons to see if two values are the same.  */
496   memset (&d, 0, sizeof d);
497
498   real_from_integer (&d, type ? TYPE_MODE (type) : VOIDmode,
499                      TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
500                      TREE_UNSIGNED (TREE_TYPE (i)));
501   return d;
502 }
503
504 /* Given a tree representing an integer constant I, return a tree
505    representing the same value as a floating-point constant of type TYPE.  */
506
507 tree
508 build_real_from_int_cst (tree type, tree i)
509 {
510   tree v;
511   int overflow = TREE_OVERFLOW (i);
512
513   v = build_real (type, real_value_from_int_cst (type, i));
514
515   TREE_OVERFLOW (v) |= overflow;
516   TREE_CONSTANT_OVERFLOW (v) |= overflow;
517   return v;
518 }
519
520 /* Return a newly constructed STRING_CST node whose value is
521    the LEN characters at STR.
522    The TREE_TYPE is not initialized.  */
523
524 tree
525 build_string (int len, const char *str)
526 {
527   tree s = make_node (STRING_CST);
528
529   TREE_STRING_LENGTH (s) = len;
530   TREE_STRING_POINTER (s) = ggc_alloc_string (str, len);
531
532   return s;
533 }
534
535 /* Return a newly constructed COMPLEX_CST node whose value is
536    specified by the real and imaginary parts REAL and IMAG.
537    Both REAL and IMAG should be constant nodes.  TYPE, if specified,
538    will be the type of the COMPLEX_CST; otherwise a new type will be made.  */
539
540 tree
541 build_complex (tree type, tree real, tree imag)
542 {
543   tree t = make_node (COMPLEX_CST);
544
545   TREE_REALPART (t) = real;
546   TREE_IMAGPART (t) = imag;
547   TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
548   TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
549   TREE_CONSTANT_OVERFLOW (t)
550     = TREE_CONSTANT_OVERFLOW (real) | TREE_CONSTANT_OVERFLOW (imag);
551   return t;
552 }
553
554 /* Build a newly constructed TREE_VEC node of length LEN.  */
555
556 tree
557 make_tree_vec (int len)
558 {
559   tree t;
560   int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
561
562 #ifdef GATHER_STATISTICS
563   tree_node_counts[(int) vec_kind]++;
564   tree_node_sizes[(int) vec_kind] += length;
565 #endif
566
567   t = ggc_alloc_tree (length);
568
569   memset (t, 0, length);
570   TREE_SET_CODE (t, TREE_VEC);
571   TREE_VEC_LENGTH (t) = len;
572
573   return t;
574 }
575 \f
576 /* Return 1 if EXPR is the integer constant zero or a complex constant
577    of zero.  */
578
579 int
580 integer_zerop (tree expr)
581 {
582   STRIP_NOPS (expr);
583
584   return ((TREE_CODE (expr) == INTEGER_CST
585            && ! TREE_CONSTANT_OVERFLOW (expr)
586            && TREE_INT_CST_LOW (expr) == 0
587            && TREE_INT_CST_HIGH (expr) == 0)
588           || (TREE_CODE (expr) == COMPLEX_CST
589               && integer_zerop (TREE_REALPART (expr))
590               && integer_zerop (TREE_IMAGPART (expr))));
591 }
592
593 /* Return 1 if EXPR is the integer constant one or the corresponding
594    complex constant.  */
595
596 int
597 integer_onep (tree expr)
598 {
599   STRIP_NOPS (expr);
600
601   return ((TREE_CODE (expr) == INTEGER_CST
602            && ! TREE_CONSTANT_OVERFLOW (expr)
603            && TREE_INT_CST_LOW (expr) == 1
604            && TREE_INT_CST_HIGH (expr) == 0)
605           || (TREE_CODE (expr) == COMPLEX_CST
606               && integer_onep (TREE_REALPART (expr))
607               && integer_zerop (TREE_IMAGPART (expr))));
608 }
609
610 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
611    it contains.  Likewise for the corresponding complex constant.  */
612
613 int
614 integer_all_onesp (tree expr)
615 {
616   int prec;
617   int uns;
618
619   STRIP_NOPS (expr);
620
621   if (TREE_CODE (expr) == COMPLEX_CST
622       && integer_all_onesp (TREE_REALPART (expr))
623       && integer_zerop (TREE_IMAGPART (expr)))
624     return 1;
625
626   else if (TREE_CODE (expr) != INTEGER_CST
627            || TREE_CONSTANT_OVERFLOW (expr))
628     return 0;
629
630   uns = TREE_UNSIGNED (TREE_TYPE (expr));
631   if (!uns)
632     return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
633             && TREE_INT_CST_HIGH (expr) == -1);
634
635   /* Note that using TYPE_PRECISION here is wrong.  We care about the
636      actual bits, not the (arbitrary) range of the type.  */
637   prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)));
638   if (prec >= HOST_BITS_PER_WIDE_INT)
639     {
640       HOST_WIDE_INT high_value;
641       int shift_amount;
642
643       shift_amount = prec - HOST_BITS_PER_WIDE_INT;
644
645       if (shift_amount > HOST_BITS_PER_WIDE_INT)
646         /* Can not handle precisions greater than twice the host int size.  */
647         abort ();
648       else if (shift_amount == HOST_BITS_PER_WIDE_INT)
649         /* Shifting by the host word size is undefined according to the ANSI
650            standard, so we must handle this as a special case.  */
651         high_value = -1;
652       else
653         high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
654
655       return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
656               && TREE_INT_CST_HIGH (expr) == high_value);
657     }
658   else
659     return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
660 }
661
662 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
663    one bit on).  */
664
665 int
666 integer_pow2p (tree expr)
667 {
668   int prec;
669   HOST_WIDE_INT high, low;
670
671   STRIP_NOPS (expr);
672
673   if (TREE_CODE (expr) == COMPLEX_CST
674       && integer_pow2p (TREE_REALPART (expr))
675       && integer_zerop (TREE_IMAGPART (expr)))
676     return 1;
677
678   if (TREE_CODE (expr) != INTEGER_CST || TREE_CONSTANT_OVERFLOW (expr))
679     return 0;
680
681   prec = (POINTER_TYPE_P (TREE_TYPE (expr))
682           ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
683   high = TREE_INT_CST_HIGH (expr);
684   low = TREE_INT_CST_LOW (expr);
685
686   /* First clear all bits that are beyond the type's precision in case
687      we've been sign extended.  */
688
689   if (prec == 2 * HOST_BITS_PER_WIDE_INT)
690     ;
691   else if (prec > HOST_BITS_PER_WIDE_INT)
692     high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
693   else
694     {
695       high = 0;
696       if (prec < HOST_BITS_PER_WIDE_INT)
697         low &= ~((HOST_WIDE_INT) (-1) << prec);
698     }
699
700   if (high == 0 && low == 0)
701     return 0;
702
703   return ((high == 0 && (low & (low - 1)) == 0)
704           || (low == 0 && (high & (high - 1)) == 0));
705 }
706
707 /* Return 1 if EXPR is an integer constant other than zero or a
708    complex constant other than zero.  */
709
710 int
711 integer_nonzerop (tree expr)
712 {
713   STRIP_NOPS (expr);
714
715   return ((TREE_CODE (expr) == INTEGER_CST
716            && ! TREE_CONSTANT_OVERFLOW (expr)
717            && (TREE_INT_CST_LOW (expr) != 0
718                || TREE_INT_CST_HIGH (expr) != 0))
719           || (TREE_CODE (expr) == COMPLEX_CST
720               && (integer_nonzerop (TREE_REALPART (expr))
721                   || integer_nonzerop (TREE_IMAGPART (expr)))));
722 }
723
724 /* Return the power of two represented by a tree node known to be a
725    power of two.  */
726
727 int
728 tree_log2 (tree expr)
729 {
730   int prec;
731   HOST_WIDE_INT high, low;
732
733   STRIP_NOPS (expr);
734
735   if (TREE_CODE (expr) == COMPLEX_CST)
736     return tree_log2 (TREE_REALPART (expr));
737
738   prec = (POINTER_TYPE_P (TREE_TYPE (expr))
739           ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
740
741   high = TREE_INT_CST_HIGH (expr);
742   low = TREE_INT_CST_LOW (expr);
743
744   /* First clear all bits that are beyond the type's precision in case
745      we've been sign extended.  */
746
747   if (prec == 2 * HOST_BITS_PER_WIDE_INT)
748     ;
749   else if (prec > HOST_BITS_PER_WIDE_INT)
750     high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
751   else
752     {
753       high = 0;
754       if (prec < HOST_BITS_PER_WIDE_INT)
755         low &= ~((HOST_WIDE_INT) (-1) << prec);
756     }
757
758   return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
759           : exact_log2 (low));
760 }
761
762 /* Similar, but return the largest integer Y such that 2 ** Y is less
763    than or equal to EXPR.  */
764
765 int
766 tree_floor_log2 (tree expr)
767 {
768   int prec;
769   HOST_WIDE_INT high, low;
770
771   STRIP_NOPS (expr);
772
773   if (TREE_CODE (expr) == COMPLEX_CST)
774     return tree_log2 (TREE_REALPART (expr));
775
776   prec = (POINTER_TYPE_P (TREE_TYPE (expr))
777           ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
778
779   high = TREE_INT_CST_HIGH (expr);
780   low = TREE_INT_CST_LOW (expr);
781
782   /* First clear all bits that are beyond the type's precision in case
783      we've been sign extended.  Ignore if type's precision hasn't been set
784      since what we are doing is setting it.  */
785
786   if (prec == 2 * HOST_BITS_PER_WIDE_INT || prec == 0)
787     ;
788   else if (prec > HOST_BITS_PER_WIDE_INT)
789     high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
790   else
791     {
792       high = 0;
793       if (prec < HOST_BITS_PER_WIDE_INT)
794         low &= ~((HOST_WIDE_INT) (-1) << prec);
795     }
796
797   return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
798           : floor_log2 (low));
799 }
800
801 /* Return 1 if EXPR is the real constant zero.  */
802
803 int
804 real_zerop (tree expr)
805 {
806   STRIP_NOPS (expr);
807
808   return ((TREE_CODE (expr) == REAL_CST
809            && ! TREE_CONSTANT_OVERFLOW (expr)
810            && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0))
811           || (TREE_CODE (expr) == COMPLEX_CST
812               && real_zerop (TREE_REALPART (expr))
813               && real_zerop (TREE_IMAGPART (expr))));
814 }
815
816 /* Return 1 if EXPR is the real constant one in real or complex form.  */
817
818 int
819 real_onep (tree expr)
820 {
821   STRIP_NOPS (expr);
822
823   return ((TREE_CODE (expr) == REAL_CST
824            && ! TREE_CONSTANT_OVERFLOW (expr)
825            && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1))
826           || (TREE_CODE (expr) == COMPLEX_CST
827               && real_onep (TREE_REALPART (expr))
828               && real_zerop (TREE_IMAGPART (expr))));
829 }
830
831 /* Return 1 if EXPR is the real constant two.  */
832
833 int
834 real_twop (tree expr)
835 {
836   STRIP_NOPS (expr);
837
838   return ((TREE_CODE (expr) == REAL_CST
839            && ! TREE_CONSTANT_OVERFLOW (expr)
840            && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2))
841           || (TREE_CODE (expr) == COMPLEX_CST
842               && real_twop (TREE_REALPART (expr))
843               && real_zerop (TREE_IMAGPART (expr))));
844 }
845
846 /* Return 1 if EXPR is the real constant minus one.  */
847
848 int
849 real_minus_onep (tree expr)
850 {
851   STRIP_NOPS (expr);
852
853   return ((TREE_CODE (expr) == REAL_CST
854            && ! TREE_CONSTANT_OVERFLOW (expr)
855            && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1))
856           || (TREE_CODE (expr) == COMPLEX_CST
857               && real_minus_onep (TREE_REALPART (expr))
858               && real_zerop (TREE_IMAGPART (expr))));
859 }
860
861 /* Nonzero if EXP is a constant or a cast of a constant.  */
862
863 int
864 really_constant_p (tree exp)
865 {
866   /* This is not quite the same as STRIP_NOPS.  It does more.  */
867   while (TREE_CODE (exp) == NOP_EXPR
868          || TREE_CODE (exp) == CONVERT_EXPR
869          || TREE_CODE (exp) == NON_LVALUE_EXPR)
870     exp = TREE_OPERAND (exp, 0);
871   return TREE_CONSTANT (exp);
872 }
873 \f
874 /* Return first list element whose TREE_VALUE is ELEM.
875    Return 0 if ELEM is not in LIST.  */
876
877 tree
878 value_member (tree elem, tree list)
879 {
880   while (list)
881     {
882       if (elem == TREE_VALUE (list))
883         return list;
884       list = TREE_CHAIN (list);
885     }
886   return NULL_TREE;
887 }
888
889 /* Return first list element whose TREE_PURPOSE is ELEM.
890    Return 0 if ELEM is not in LIST.  */
891
892 tree
893 purpose_member (tree elem, tree list)
894 {
895   while (list)
896     {
897       if (elem == TREE_PURPOSE (list))
898         return list;
899       list = TREE_CHAIN (list);
900     }
901   return NULL_TREE;
902 }
903
904 /* Return first list element whose BINFO_TYPE is ELEM.
905    Return 0 if ELEM is not in LIST.  */
906
907 tree
908 binfo_member (tree elem, tree list)
909 {
910   while (list)
911     {
912       if (elem == BINFO_TYPE (list))
913         return list;
914       list = TREE_CHAIN (list);
915     }
916   return NULL_TREE;
917 }
918
919 /* Return nonzero if ELEM is part of the chain CHAIN.  */
920
921 int
922 chain_member (tree elem, tree chain)
923 {
924   while (chain)
925     {
926       if (elem == chain)
927         return 1;
928       chain = TREE_CHAIN (chain);
929     }
930
931   return 0;
932 }
933
934 /* Return the length of a chain of nodes chained through TREE_CHAIN.
935    We expect a null pointer to mark the end of the chain.
936    This is the Lisp primitive `length'.  */
937
938 int
939 list_length (tree t)
940 {
941   tree tail;
942   int len = 0;
943
944   for (tail = t; tail; tail = TREE_CHAIN (tail))
945     len++;
946
947   return len;
948 }
949
950 /* Returns the number of FIELD_DECLs in TYPE.  */
951
952 int
953 fields_length (tree type)
954 {
955   tree t = TYPE_FIELDS (type);
956   int count = 0;
957
958   for (; t; t = TREE_CHAIN (t))
959     if (TREE_CODE (t) == FIELD_DECL)
960       ++count;
961
962   return count;
963 }
964
965 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
966    by modifying the last node in chain 1 to point to chain 2.
967    This is the Lisp primitive `nconc'.  */
968
969 tree
970 chainon (tree op1, tree op2)
971 {
972   tree t1;
973
974   if (!op1)
975     return op2;
976   if (!op2)
977     return op1;
978
979   for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
980     continue;
981   TREE_CHAIN (t1) = op2;
982
983 #ifdef ENABLE_TREE_CHECKING
984   {
985     tree t2;
986     for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
987       if (t2 == t1)
988         abort ();  /* Circularity created.  */
989   }
990 #endif
991
992   return op1;
993 }
994
995 /* Return the last node in a chain of nodes (chained through TREE_CHAIN).  */
996
997 tree
998 tree_last (tree chain)
999 {
1000   tree next;
1001   if (chain)
1002     while ((next = TREE_CHAIN (chain)))
1003       chain = next;
1004   return chain;
1005 }
1006
1007 /* Reverse the order of elements in the chain T,
1008    and return the new head of the chain (old last element).  */
1009
1010 tree
1011 nreverse (tree t)
1012 {
1013   tree prev = 0, decl, next;
1014   for (decl = t; decl; decl = next)
1015     {
1016       next = TREE_CHAIN (decl);
1017       TREE_CHAIN (decl) = prev;
1018       prev = decl;
1019     }
1020   return prev;
1021 }
1022 \f
1023 /* Return a newly created TREE_LIST node whose
1024    purpose and value fields are PARM and VALUE.  */
1025
1026 tree
1027 build_tree_list (tree parm, tree value)
1028 {
1029   tree t = make_node (TREE_LIST);
1030   TREE_PURPOSE (t) = parm;
1031   TREE_VALUE (t) = value;
1032   return t;
1033 }
1034
1035 /* Return a newly created TREE_LIST node whose
1036    purpose and value fields are PURPOSE and VALUE
1037    and whose TREE_CHAIN is CHAIN.  */
1038
1039 tree
1040 tree_cons (tree purpose, tree value, tree chain)
1041 {
1042   tree node;
1043
1044   node = ggc_alloc_tree (sizeof (struct tree_list));
1045
1046   memset (node, 0, sizeof (struct tree_common));
1047
1048 #ifdef GATHER_STATISTICS
1049   tree_node_counts[(int) x_kind]++;
1050   tree_node_sizes[(int) x_kind] += sizeof (struct tree_list);
1051 #endif
1052
1053   TREE_SET_CODE (node, TREE_LIST);
1054   TREE_CHAIN (node) = chain;
1055   TREE_PURPOSE (node) = purpose;
1056   TREE_VALUE (node) = value;
1057   return node;
1058 }
1059
1060 /* Return the first expression in a sequence of COMPOUND_EXPRs.  */
1061
1062 tree
1063 expr_first (tree expr)
1064 {
1065   if (expr == NULL_TREE)
1066     return expr;
1067   while (TREE_CODE (expr) == COMPOUND_EXPR)
1068     expr = TREE_OPERAND (expr, 0);
1069   return expr;
1070 }
1071
1072 /* Return the last expression in a sequence of COMPOUND_EXPRs.  */
1073
1074 tree
1075 expr_last (tree expr)
1076 {
1077   if (expr == NULL_TREE)
1078     return expr;
1079   while (TREE_CODE (expr) == COMPOUND_EXPR)
1080     expr = TREE_OPERAND (expr, 1);
1081   return expr;
1082 }
1083
1084 /* Return the number of subexpressions in a sequence of COMPOUND_EXPRs.  */
1085
1086 int
1087 expr_length (tree expr)
1088 {
1089   int len = 0;
1090
1091   if (expr == NULL_TREE)
1092     return 0;
1093   for (; TREE_CODE (expr) == COMPOUND_EXPR; expr = TREE_OPERAND (expr, 1))
1094     len += expr_length (TREE_OPERAND (expr, 0));
1095   ++len;
1096   return len;
1097 }
1098 \f
1099 /* Return the size nominally occupied by an object of type TYPE
1100    when it resides in memory.  The value is measured in units of bytes,
1101    and its data type is that normally used for type sizes
1102    (which is the first type created by make_signed_type or
1103    make_unsigned_type).  */
1104
1105 tree
1106 size_in_bytes (tree type)
1107 {
1108   tree t;
1109
1110   if (type == error_mark_node)
1111     return integer_zero_node;
1112
1113   type = TYPE_MAIN_VARIANT (type);
1114   t = TYPE_SIZE_UNIT (type);
1115
1116   if (t == 0)
1117     {
1118       (*lang_hooks.types.incomplete_type_error) (NULL_TREE, type);
1119       return size_zero_node;
1120     }
1121
1122   if (TREE_CODE (t) == INTEGER_CST)
1123     force_fit_type (t, 0);
1124
1125   return t;
1126 }
1127
1128 /* Return the size of TYPE (in bytes) as a wide integer
1129    or return -1 if the size can vary or is larger than an integer.  */
1130
1131 HOST_WIDE_INT
1132 int_size_in_bytes (tree type)
1133 {
1134   tree t;
1135
1136   if (type == error_mark_node)
1137     return 0;
1138
1139   type = TYPE_MAIN_VARIANT (type);
1140   t = TYPE_SIZE_UNIT (type);
1141   if (t == 0
1142       || TREE_CODE (t) != INTEGER_CST
1143       || TREE_OVERFLOW (t)
1144       || TREE_INT_CST_HIGH (t) != 0
1145       /* If the result would appear negative, it's too big to represent.  */
1146       || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
1147     return -1;
1148
1149   return TREE_INT_CST_LOW (t);
1150 }
1151 \f
1152 /* Return the bit position of FIELD, in bits from the start of the record.
1153    This is a tree of type bitsizetype.  */
1154
1155 tree
1156 bit_position (tree field)
1157 {
1158   return bit_from_pos (DECL_FIELD_OFFSET (field),
1159                        DECL_FIELD_BIT_OFFSET (field));
1160 }
1161
1162 /* Likewise, but return as an integer.  Abort if it cannot be represented
1163    in that way (since it could be a signed value, we don't have the option
1164    of returning -1 like int_size_in_byte can.  */
1165
1166 HOST_WIDE_INT
1167 int_bit_position (tree field)
1168 {
1169   return tree_low_cst (bit_position (field), 0);
1170 }
1171 \f
1172 /* Return the byte position of FIELD, in bytes from the start of the record.
1173    This is a tree of type sizetype.  */
1174
1175 tree
1176 byte_position (tree field)
1177 {
1178   return byte_from_pos (DECL_FIELD_OFFSET (field),
1179                         DECL_FIELD_BIT_OFFSET (field));
1180 }
1181
1182 /* Likewise, but return as an integer.  Abort if it cannot be represented
1183    in that way (since it could be a signed value, we don't have the option
1184    of returning -1 like int_size_in_byte can.  */
1185
1186 HOST_WIDE_INT
1187 int_byte_position (tree field)
1188 {
1189   return tree_low_cst (byte_position (field), 0);
1190 }
1191 \f
1192 /* Return the strictest alignment, in bits, that T is known to have.  */
1193
1194 unsigned int
1195 expr_align (tree t)
1196 {
1197   unsigned int align0, align1;
1198
1199   switch (TREE_CODE (t))
1200     {
1201     case NOP_EXPR:  case CONVERT_EXPR:  case NON_LVALUE_EXPR:
1202       /* If we have conversions, we know that the alignment of the
1203          object must meet each of the alignments of the types.  */
1204       align0 = expr_align (TREE_OPERAND (t, 0));
1205       align1 = TYPE_ALIGN (TREE_TYPE (t));
1206       return MAX (align0, align1);
1207
1208     case SAVE_EXPR:         case COMPOUND_EXPR:       case MODIFY_EXPR:
1209     case INIT_EXPR:         case TARGET_EXPR:         case WITH_CLEANUP_EXPR:
1210     case WITH_RECORD_EXPR:  case CLEANUP_POINT_EXPR:  case UNSAVE_EXPR:
1211       /* These don't change the alignment of an object.  */
1212       return expr_align (TREE_OPERAND (t, 0));
1213
1214     case COND_EXPR:
1215       /* The best we can do is say that the alignment is the least aligned
1216          of the two arms.  */
1217       align0 = expr_align (TREE_OPERAND (t, 1));
1218       align1 = expr_align (TREE_OPERAND (t, 2));
1219       return MIN (align0, align1);
1220
1221     case LABEL_DECL:     case CONST_DECL:
1222     case VAR_DECL:       case PARM_DECL:   case RESULT_DECL:
1223       if (DECL_ALIGN (t) != 0)
1224         return DECL_ALIGN (t);
1225       break;
1226
1227     case FUNCTION_DECL:
1228       return FUNCTION_BOUNDARY;
1229
1230     default:
1231       break;
1232     }
1233
1234   /* Otherwise take the alignment from that of the type.  */
1235   return TYPE_ALIGN (TREE_TYPE (t));
1236 }
1237 \f
1238 /* Return, as a tree node, the number of elements for TYPE (which is an
1239    ARRAY_TYPE) minus one. This counts only elements of the top array.  */
1240
1241 tree
1242 array_type_nelts (tree type)
1243 {
1244   tree index_type, min, max;
1245
1246   /* If they did it with unspecified bounds, then we should have already
1247      given an error about it before we got here.  */
1248   if (! TYPE_DOMAIN (type))
1249     return error_mark_node;
1250
1251   index_type = TYPE_DOMAIN (type);
1252   min = TYPE_MIN_VALUE (index_type);
1253   max = TYPE_MAX_VALUE (index_type);
1254
1255   return (integer_zerop (min)
1256           ? max
1257           : fold (build (MINUS_EXPR, TREE_TYPE (max), max, min)));
1258 }
1259 \f
1260 /* Return nonzero if arg is static -- a reference to an object in
1261    static storage.  This is not the same as the C meaning of `static'.  */
1262
1263 int
1264 staticp (tree arg)
1265 {
1266   switch (TREE_CODE (arg))
1267     {
1268     case FUNCTION_DECL:
1269       /* Nested functions aren't static, since taking their address
1270          involves a trampoline.  */
1271       return ((decl_function_context (arg) == 0 || DECL_NO_STATIC_CHAIN (arg))
1272               && ! DECL_NON_ADDR_CONST_P (arg));
1273
1274     case VAR_DECL:
1275       return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
1276               && ! DECL_THREAD_LOCAL (arg)
1277               && ! DECL_NON_ADDR_CONST_P (arg));
1278
1279     case CONSTRUCTOR:
1280       return TREE_STATIC (arg);
1281
1282     case LABEL_DECL:
1283     case STRING_CST:
1284       return 1;
1285
1286       /* If we are referencing a bitfield, we can't evaluate an
1287          ADDR_EXPR at compile time and so it isn't a constant.  */
1288     case COMPONENT_REF:
1289       return (! DECL_BIT_FIELD (TREE_OPERAND (arg, 1))
1290               && staticp (TREE_OPERAND (arg, 0)));
1291
1292     case BIT_FIELD_REF:
1293       return 0;
1294
1295 #if 0
1296        /* This case is technically correct, but results in setting
1297           TREE_CONSTANT on ADDR_EXPRs that cannot be evaluated at
1298           compile time.  */
1299     case INDIRECT_REF:
1300       return TREE_CONSTANT (TREE_OPERAND (arg, 0));
1301 #endif
1302
1303     case ARRAY_REF:
1304     case ARRAY_RANGE_REF:
1305       if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
1306           && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
1307         return staticp (TREE_OPERAND (arg, 0));
1308
1309     default:
1310       if ((unsigned int) TREE_CODE (arg)
1311           >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
1312         return (*lang_hooks.staticp) (arg);
1313       else
1314         return 0;
1315     }
1316 }
1317 \f
1318 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
1319    Do this to any expression which may be used in more than one place,
1320    but must be evaluated only once.
1321
1322    Normally, expand_expr would reevaluate the expression each time.
1323    Calling save_expr produces something that is evaluated and recorded
1324    the first time expand_expr is called on it.  Subsequent calls to
1325    expand_expr just reuse the recorded value.
1326
1327    The call to expand_expr that generates code that actually computes
1328    the value is the first call *at compile time*.  Subsequent calls
1329    *at compile time* generate code to use the saved value.
1330    This produces correct result provided that *at run time* control
1331    always flows through the insns made by the first expand_expr
1332    before reaching the other places where the save_expr was evaluated.
1333    You, the caller of save_expr, must make sure this is so.
1334
1335    Constants, and certain read-only nodes, are returned with no
1336    SAVE_EXPR because that is safe.  Expressions containing placeholders
1337    are not touched; see tree.def for an explanation of what these
1338    are used for.  */
1339
1340 tree
1341 save_expr (tree expr)
1342 {
1343   tree t = fold (expr);
1344   tree inner;
1345
1346   /* If the tree evaluates to a constant, then we don't want to hide that
1347      fact (i.e. this allows further folding, and direct checks for constants).
1348      However, a read-only object that has side effects cannot be bypassed.
1349      Since it is no problem to reevaluate literals, we just return the
1350      literal node.  */
1351   inner = skip_simple_arithmetic (t);
1352   if (TREE_CONSTANT (inner)
1353       || (TREE_READONLY (inner) && ! TREE_SIDE_EFFECTS (inner))
1354       || TREE_CODE (inner) == SAVE_EXPR
1355       || TREE_CODE (inner) == ERROR_MARK)
1356     return t;
1357
1358   /* If INNER contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
1359      it means that the size or offset of some field of an object depends on
1360      the value within another field.
1361
1362      Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
1363      and some variable since it would then need to be both evaluated once and
1364      evaluated more than once.  Front-ends must assure this case cannot
1365      happen by surrounding any such subexpressions in their own SAVE_EXPR
1366      and forcing evaluation at the proper time.  */
1367   if (contains_placeholder_p (inner))
1368     return t;
1369
1370   t = build (SAVE_EXPR, TREE_TYPE (expr), t, current_function_decl, NULL_TREE);
1371
1372   /* This expression might be placed ahead of a jump to ensure that the
1373      value was computed on both sides of the jump.  So make sure it isn't
1374      eliminated as dead.  */
1375   TREE_SIDE_EFFECTS (t) = 1;
1376   TREE_READONLY (t) = 1;
1377   return t;
1378 }
1379
1380 /* Look inside EXPR and into any simple arithmetic operations.  Return
1381    the innermost non-arithmetic node.  */
1382
1383 tree
1384 skip_simple_arithmetic (tree expr)
1385 {
1386   tree inner;
1387
1388   /* We don't care about whether this can be used as an lvalue in this
1389      context.  */
1390   while (TREE_CODE (expr) == NON_LVALUE_EXPR)
1391     expr = TREE_OPERAND (expr, 0);
1392
1393   /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
1394      a constant, it will be more efficient to not make another SAVE_EXPR since
1395      it will allow better simplification and GCSE will be able to merge the
1396      computations if they actually occur.  */
1397   inner = expr;
1398   while (1)
1399     {
1400       if (TREE_CODE_CLASS (TREE_CODE (inner)) == '1')
1401         inner = TREE_OPERAND (inner, 0);
1402       else if (TREE_CODE_CLASS (TREE_CODE (inner)) == '2')
1403         {
1404           if (TREE_CONSTANT (TREE_OPERAND (inner, 1)))
1405             inner = TREE_OPERAND (inner, 0);
1406           else if (TREE_CONSTANT (TREE_OPERAND (inner, 0)))
1407             inner = TREE_OPERAND (inner, 1);
1408           else
1409             break;
1410         }
1411       else
1412         break;
1413     }
1414
1415   return inner;
1416 }
1417
1418 /* Return TRUE if EXPR is a SAVE_EXPR or wraps simple arithmetic around a
1419    SAVE_EXPR.  Return FALSE otherwise.  */
1420
1421 bool
1422 saved_expr_p (tree expr)
1423 {
1424   return TREE_CODE (skip_simple_arithmetic (expr)) == SAVE_EXPR;
1425 }
1426
1427 /* Arrange for an expression to be expanded multiple independent
1428    times.  This is useful for cleanup actions, as the backend can
1429    expand them multiple times in different places.  */
1430
1431 tree
1432 unsave_expr (tree expr)
1433 {
1434   tree t;
1435
1436   /* If this is already protected, no sense in protecting it again.  */
1437   if (TREE_CODE (expr) == UNSAVE_EXPR)
1438     return expr;
1439
1440   t = build1 (UNSAVE_EXPR, TREE_TYPE (expr), expr);
1441   TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (expr);
1442   return t;
1443 }
1444
1445 /* Returns the index of the first non-tree operand for CODE, or the number
1446    of operands if all are trees.  */
1447
1448 int
1449 first_rtl_op (enum tree_code code)
1450 {
1451   switch (code)
1452     {
1453     case SAVE_EXPR:
1454       return 2;
1455     case GOTO_SUBROUTINE_EXPR:
1456     case RTL_EXPR:
1457       return 0;
1458     case WITH_CLEANUP_EXPR:
1459       return 2;
1460     default:
1461       return TREE_CODE_LENGTH (code);
1462     }
1463 }
1464
1465 /* Return which tree structure is used by T.  */
1466
1467 enum tree_node_structure_enum
1468 tree_node_structure (tree t)
1469 {
1470   enum tree_code code = TREE_CODE (t);
1471
1472   switch (TREE_CODE_CLASS (code))
1473     {
1474     case 'd':   return TS_DECL;
1475     case 't':   return TS_TYPE;
1476     case 'b':   return TS_BLOCK;
1477     case 'r': case '<': case '1': case '2': case 'e': case 's':
1478       return TS_EXP;
1479     default:  /* 'c' and 'x' */
1480       break;
1481     }
1482   switch (code)
1483     {
1484       /* 'c' cases.  */
1485     case INTEGER_CST:           return TS_INT_CST;
1486     case REAL_CST:              return TS_REAL_CST;
1487     case COMPLEX_CST:           return TS_COMPLEX;
1488     case VECTOR_CST:            return TS_VECTOR;
1489     case STRING_CST:            return TS_STRING;
1490       /* 'x' cases.  */
1491     case ERROR_MARK:            return TS_COMMON;
1492     case IDENTIFIER_NODE:       return TS_IDENTIFIER;
1493     case TREE_LIST:             return TS_LIST;
1494     case TREE_VEC:              return TS_VEC;
1495     case PLACEHOLDER_EXPR:      return TS_COMMON;
1496
1497     default:
1498       abort ();
1499     }
1500 }
1501
1502 /* Perform any modifications to EXPR required when it is unsaved.  Does
1503    not recurse into EXPR's subtrees.  */
1504
1505 void
1506 unsave_expr_1 (tree expr)
1507 {
1508   switch (TREE_CODE (expr))
1509     {
1510     case SAVE_EXPR:
1511       if (! SAVE_EXPR_PERSISTENT_P (expr))
1512         SAVE_EXPR_RTL (expr) = 0;
1513       break;
1514
1515     case TARGET_EXPR:
1516       /* Don't mess with a TARGET_EXPR that hasn't been expanded.
1517          It's OK for this to happen if it was part of a subtree that
1518          isn't immediately expanded, such as operand 2 of another
1519          TARGET_EXPR.  */
1520       if (TREE_OPERAND (expr, 1))
1521         break;
1522
1523       TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
1524       TREE_OPERAND (expr, 3) = NULL_TREE;
1525       break;
1526
1527     case RTL_EXPR:
1528       /* I don't yet know how to emit a sequence multiple times.  */
1529       if (RTL_EXPR_SEQUENCE (expr) != 0)
1530         abort ();
1531       break;
1532
1533     default:
1534       break;
1535     }
1536 }
1537
1538 /* Default lang hook for "unsave_expr_now".  */
1539
1540 tree
1541 lhd_unsave_expr_now (tree expr)
1542 {
1543   enum tree_code code;
1544
1545   /* There's nothing to do for NULL_TREE.  */
1546   if (expr == 0)
1547     return expr;
1548
1549   unsave_expr_1 (expr);
1550
1551   code = TREE_CODE (expr);
1552   switch (TREE_CODE_CLASS (code))
1553     {
1554     case 'c':  /* a constant */
1555     case 't':  /* a type node */
1556     case 'd':  /* A decl node */
1557     case 'b':  /* A block node */
1558       break;
1559
1560     case 'x':  /* miscellaneous: e.g., identifier, TREE_LIST or ERROR_MARK.  */
1561       if (code == TREE_LIST)
1562         {
1563           lhd_unsave_expr_now (TREE_VALUE (expr));
1564           lhd_unsave_expr_now (TREE_CHAIN (expr));
1565         }
1566       break;
1567
1568     case 'e':  /* an expression */
1569     case 'r':  /* a reference */
1570     case 's':  /* an expression with side effects */
1571     case '<':  /* a comparison expression */
1572     case '2':  /* a binary arithmetic expression */
1573     case '1':  /* a unary arithmetic expression */
1574       {
1575         int i;
1576
1577         for (i = first_rtl_op (code) - 1; i >= 0; i--)
1578           lhd_unsave_expr_now (TREE_OPERAND (expr, i));
1579       }
1580       break;
1581
1582     default:
1583       abort ();
1584     }
1585
1586   return expr;
1587 }
1588
1589 /* Return 0 if it is safe to evaluate EXPR multiple times,
1590    return 1 if it is safe if EXPR is unsaved afterward, or
1591    return 2 if it is completely unsafe.
1592
1593    This assumes that CALL_EXPRs and TARGET_EXPRs are never replicated in
1594    an expression tree, so that it safe to unsave them and the surrounding
1595    context will be correct.
1596
1597    SAVE_EXPRs basically *only* appear replicated in an expression tree,
1598    occasionally across the whole of a function.  It is therefore only
1599    safe to unsave a SAVE_EXPR if you know that all occurrences appear
1600    below the UNSAVE_EXPR.
1601
1602    RTL_EXPRs consume their rtl during evaluation.  It is therefore
1603    never possible to unsave them.  */
1604
1605 int
1606 unsafe_for_reeval (tree expr)
1607 {
1608   int unsafeness = 0;
1609   enum tree_code code;
1610   int i, tmp, tmp2;
1611   tree exp;
1612   int first_rtl;
1613
1614   if (expr == NULL_TREE)
1615     return 1;
1616
1617   code = TREE_CODE (expr);
1618   first_rtl = first_rtl_op (code);
1619
1620   switch (code)
1621     {
1622     case SAVE_EXPR:
1623     case RTL_EXPR:
1624       return 2;
1625
1626     case TREE_LIST:
1627       for (exp = expr; exp != 0; exp = TREE_CHAIN (exp))
1628         {
1629           tmp = unsafe_for_reeval (TREE_VALUE (exp));
1630           unsafeness = MAX (tmp, unsafeness);
1631         }
1632
1633       return unsafeness;
1634
1635     case CALL_EXPR:
1636       tmp2 = unsafe_for_reeval (TREE_OPERAND (expr, 0));
1637       tmp = unsafe_for_reeval (TREE_OPERAND (expr, 1));
1638       return MAX (MAX (tmp, 1), tmp2);
1639
1640     case TARGET_EXPR:
1641       unsafeness = 1;
1642       break;
1643
1644     case EXIT_BLOCK_EXPR:
1645       /* EXIT_BLOCK_LABELED_BLOCK, a.k.a. TREE_OPERAND (expr, 0), holds
1646          a reference to an ancestor LABELED_BLOCK, so we need to avoid
1647          unbounded recursion in the 'e' traversal code below.  */
1648       exp = EXIT_BLOCK_RETURN (expr);
1649       return exp ? unsafe_for_reeval (exp) : 0;
1650
1651     default:
1652       tmp = (*lang_hooks.unsafe_for_reeval) (expr);
1653       if (tmp >= 0)
1654         return tmp;
1655       break;
1656     }
1657
1658   switch (TREE_CODE_CLASS (code))
1659     {
1660     case 'c':  /* a constant */
1661     case 't':  /* a type node */
1662     case 'x':  /* something random, like an identifier or an ERROR_MARK.  */
1663     case 'd':  /* A decl node */
1664     case 'b':  /* A block node */
1665       return 0;
1666
1667     case 'e':  /* an expression */
1668     case 'r':  /* a reference */
1669     case 's':  /* an expression with side effects */
1670     case '<':  /* a comparison expression */
1671     case '2':  /* a binary arithmetic expression */
1672     case '1':  /* a unary arithmetic expression */
1673       for (i = first_rtl - 1; i >= 0; i--)
1674         {
1675           tmp = unsafe_for_reeval (TREE_OPERAND (expr, i));
1676           unsafeness = MAX (tmp, unsafeness);
1677         }
1678
1679       return unsafeness;
1680
1681     default:
1682       return 2;
1683     }
1684 }
1685 \f
1686 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
1687    or offset that depends on a field within a record.  */
1688
1689 bool
1690 contains_placeholder_p (tree exp)
1691 {
1692   enum tree_code code;
1693   int result;
1694
1695   if (!exp)
1696     return 0;
1697
1698   /* If we have a WITH_RECORD_EXPR, it "cancels" any PLACEHOLDER_EXPR
1699      in it since it is supplying a value for it.  */
1700   code = TREE_CODE (exp);
1701   if (code == WITH_RECORD_EXPR)
1702     return 0;
1703   else if (code == PLACEHOLDER_EXPR)
1704     return 1;
1705
1706   switch (TREE_CODE_CLASS (code))
1707     {
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));
1714
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;
1720
1721     case '1':
1722     case '2':  case '<':
1723     case 'e':
1724       switch (code)
1725         {
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));
1729
1730         case RTL_EXPR:
1731         case CONSTRUCTOR:
1732           return 0;
1733
1734         case COND_EXPR:
1735           return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
1736                   || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1))
1737                   || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 2)));
1738
1739         case SAVE_EXPR:
1740           /* If we already know this doesn't have a placeholder, don't
1741              check again.  */
1742           if (SAVE_EXPR_NOPLACEHOLDER (exp) || SAVE_EXPR_RTL (exp) != 0)
1743             return 0;
1744
1745           SAVE_EXPR_NOPLACEHOLDER (exp) = 1;
1746           result = CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
1747           if (result)
1748             SAVE_EXPR_NOPLACEHOLDER (exp) = 0;
1749
1750           return result;
1751
1752         case CALL_EXPR:
1753           return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1));
1754
1755         default:
1756           break;
1757         }
1758
1759       switch (TREE_CODE_LENGTH (code))
1760         {
1761         case 1:
1762           return CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0));
1763         case 2:
1764           return (CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 0))
1765                   || CONTAINS_PLACEHOLDER_P (TREE_OPERAND (exp, 1)));
1766         default:
1767           return 0;
1768         }
1769
1770     default:
1771       return 0;
1772     }
1773   return 0;
1774 }
1775
1776 /* Return 1 if any part of the computation of TYPE involves a PLACEHOLDER_EXPR.
1777    This includes size, bounds, qualifiers (for QUAL_UNION_TYPE) and field
1778    positions.  */
1779
1780 bool
1781 type_contains_placeholder_p (tree type)
1782 {
1783   /* If the size contains a placeholder or the parent type (component type in
1784      the case of arrays) type involves a placeholder, this type does.  */
1785   if (CONTAINS_PLACEHOLDER_P (TYPE_SIZE (type))
1786       || CONTAINS_PLACEHOLDER_P (TYPE_SIZE_UNIT (type))
1787       || (TREE_TYPE (type) != 0
1788           && type_contains_placeholder_p (TREE_TYPE (type))))
1789     return 1;
1790
1791   /* Now do type-specific checks.  Note that the last part of the check above
1792      greatly limits what we have to do below.  */
1793   switch (TREE_CODE (type))
1794     {
1795     case VOID_TYPE:
1796     case COMPLEX_TYPE:
1797     case VECTOR_TYPE:
1798     case ENUMERAL_TYPE:
1799     case BOOLEAN_TYPE:
1800     case CHAR_TYPE:
1801     case POINTER_TYPE:
1802     case OFFSET_TYPE:
1803     case REFERENCE_TYPE:
1804     case METHOD_TYPE:
1805     case FILE_TYPE:
1806     case FUNCTION_TYPE:
1807       return 0;
1808
1809     case INTEGER_TYPE:
1810     case REAL_TYPE:
1811       /* Here we just check the bounds.  */
1812       return (CONTAINS_PLACEHOLDER_P (TYPE_MIN_VALUE (type))
1813               || CONTAINS_PLACEHOLDER_P (TYPE_MAX_VALUE (type)));
1814
1815     case ARRAY_TYPE:
1816     case SET_TYPE:
1817       /* We're already checked the component type (TREE_TYPE), so just check
1818          the index type.  */
1819       return type_contains_placeholder_p (TYPE_DOMAIN (type));
1820
1821     case RECORD_TYPE:
1822     case UNION_TYPE:
1823     case QUAL_UNION_TYPE:
1824       {
1825         static tree seen_types = 0;
1826         tree field;
1827         bool ret = 0;
1828
1829         /* We have to be careful here that we don't end up in infinite
1830            recursions due to a field of a type being a pointer to that type
1831            or to a mutually-recursive type.  So we store a list of record
1832            types that we've seen and see if this type is in them.  To save
1833            memory, we don't use a list for just one type.  Here we check
1834            whether we've seen this type before and store it if not.  */
1835         if (seen_types == 0)
1836           seen_types = type;
1837         else if (TREE_CODE (seen_types) != TREE_LIST)
1838           {
1839             if (seen_types == type)
1840               return 0;
1841
1842             seen_types = tree_cons (NULL_TREE, type,
1843                                     build_tree_list (NULL_TREE, seen_types));
1844           }
1845         else
1846           {
1847             if (value_member (type, seen_types) != 0)
1848               return 0;
1849
1850             seen_types = tree_cons (NULL_TREE, type, seen_types);
1851           }
1852
1853         for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1854           if (TREE_CODE (field) == FIELD_DECL
1855               && (CONTAINS_PLACEHOLDER_P (DECL_FIELD_OFFSET (field))
1856                   || (TREE_CODE (type) == QUAL_UNION_TYPE
1857                       && CONTAINS_PLACEHOLDER_P (DECL_QUALIFIER (field)))
1858                   || type_contains_placeholder_p (TREE_TYPE (field))))
1859             {
1860               ret = true;
1861               break;
1862             }
1863
1864         /* Now remove us from seen_types and return the result.  */
1865         if (seen_types == type)
1866           seen_types = 0;
1867         else
1868           seen_types = TREE_CHAIN (seen_types);
1869
1870         return ret;
1871       }
1872
1873     default:
1874       abort ();
1875     }
1876 }
1877
1878 /* Return 1 if EXP contains any expressions that produce cleanups for an
1879    outer scope to deal with.  Used by fold.  */
1880
1881 int
1882 has_cleanups (tree exp)
1883 {
1884   int i, nops, cmp;
1885
1886   if (! TREE_SIDE_EFFECTS (exp))
1887     return 0;
1888
1889   switch (TREE_CODE (exp))
1890     {
1891     case TARGET_EXPR:
1892     case GOTO_SUBROUTINE_EXPR:
1893     case WITH_CLEANUP_EXPR:
1894       return 1;
1895
1896     case CLEANUP_POINT_EXPR:
1897       return 0;
1898
1899     case CALL_EXPR:
1900       for (exp = TREE_OPERAND (exp, 1); exp; exp = TREE_CHAIN (exp))
1901         {
1902           cmp = has_cleanups (TREE_VALUE (exp));
1903           if (cmp)
1904             return cmp;
1905         }
1906       return 0;
1907
1908     default:
1909       break;
1910     }
1911
1912   /* This general rule works for most tree codes.  All exceptions should be
1913      handled above.  If this is a language-specific tree code, we can't
1914      trust what might be in the operand, so say we don't know
1915      the situation.  */
1916   if ((int) TREE_CODE (exp) >= (int) LAST_AND_UNUSED_TREE_CODE)
1917     return -1;
1918
1919   nops = first_rtl_op (TREE_CODE (exp));
1920   for (i = 0; i < nops; i++)
1921     if (TREE_OPERAND (exp, i) != 0)
1922       {
1923         int type = TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (exp, i)));
1924         if (type == 'e' || type == '<' || type == '1' || type == '2'
1925             || type == 'r' || type == 's')
1926           {
1927             cmp = has_cleanups (TREE_OPERAND (exp, i));
1928             if (cmp)
1929               return cmp;
1930           }
1931       }
1932
1933   return 0;
1934 }
1935 \f
1936 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
1937    return a tree with all occurrences of references to F in a
1938    PLACEHOLDER_EXPR replaced by R.   Note that we assume here that EXP
1939    contains only arithmetic expressions or a CALL_EXPR with a
1940    PLACEHOLDER_EXPR occurring only in its arglist.  */
1941
1942 tree
1943 substitute_in_expr (tree exp, tree f, tree r)
1944 {
1945   enum tree_code code = TREE_CODE (exp);
1946   tree op0, op1, op2;
1947   tree new;
1948   tree inner;
1949
1950   switch (TREE_CODE_CLASS (code))
1951     {
1952     case 'c':
1953     case 'd':
1954       return exp;
1955
1956     case 'x':
1957       if (code == PLACEHOLDER_EXPR)
1958         return exp;
1959       else if (code == TREE_LIST)
1960         {
1961           op0 = (TREE_CHAIN (exp) == 0
1962                  ? 0 : substitute_in_expr (TREE_CHAIN (exp), f, r));
1963           op1 = substitute_in_expr (TREE_VALUE (exp), f, r);
1964           if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
1965             return exp;
1966
1967           return tree_cons (TREE_PURPOSE (exp), op1, op0);
1968         }
1969
1970       abort ();
1971
1972     case '1':
1973     case '2':
1974     case '<':
1975     case 'e':
1976       switch (TREE_CODE_LENGTH (code))
1977         {
1978         case 1:
1979           op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
1980           if (op0 == TREE_OPERAND (exp, 0))
1981             return exp;
1982
1983           if (code == NON_LVALUE_EXPR)
1984             return op0;
1985
1986           new = fold (build1 (code, TREE_TYPE (exp), op0));
1987           break;
1988
1989         case 2:
1990           /* An RTL_EXPR cannot contain a PLACEHOLDER_EXPR; a CONSTRUCTOR
1991              could, but we don't support it.  */
1992           if (code == RTL_EXPR)
1993             return exp;
1994           else if (code == CONSTRUCTOR)
1995             abort ();
1996
1997           op0 = TREE_OPERAND (exp, 0);
1998           op1 = TREE_OPERAND (exp, 1);
1999           if (CONTAINS_PLACEHOLDER_P (op0))
2000             op0 = substitute_in_expr (op0, f, r);
2001           if (CONTAINS_PLACEHOLDER_P (op1))
2002             op1 = substitute_in_expr (op1, f, r);
2003
2004           if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2005             return exp;
2006
2007           new = fold (build (code, TREE_TYPE (exp), op0, op1));
2008           break;
2009
2010         case 3:
2011           /* It cannot be that anything inside a SAVE_EXPR contains a
2012              PLACEHOLDER_EXPR.  */
2013           if (code == SAVE_EXPR)
2014             return exp;
2015
2016           else if (code == CALL_EXPR)
2017             {
2018               op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
2019               if (op1 == TREE_OPERAND (exp, 1))
2020                 return exp;
2021
2022               return build (code, TREE_TYPE (exp),
2023                             TREE_OPERAND (exp, 0), op1, NULL_TREE);
2024             }
2025
2026           else if (code != COND_EXPR)
2027             abort ();
2028
2029           op0 = TREE_OPERAND (exp, 0);
2030           op1 = TREE_OPERAND (exp, 1);
2031           op2 = TREE_OPERAND (exp, 2);
2032
2033           if (CONTAINS_PLACEHOLDER_P (op0))
2034             op0 = substitute_in_expr (op0, f, r);
2035           if (CONTAINS_PLACEHOLDER_P (op1))
2036             op1 = substitute_in_expr (op1, f, r);
2037           if (CONTAINS_PLACEHOLDER_P (op2))
2038             op2 = substitute_in_expr (op2, f, r);
2039
2040           if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2041               && op2 == TREE_OPERAND (exp, 2))
2042             return exp;
2043
2044           new = fold (build (code, TREE_TYPE (exp), op0, op1, op2));
2045           break;
2046
2047         default:
2048           abort ();
2049         }
2050
2051       break;
2052
2053     case 'r':
2054       switch (code)
2055         {
2056         case COMPONENT_REF:
2057           /* If this expression is getting a value from a PLACEHOLDER_EXPR
2058              and it is the right field, replace it with R.  */
2059           for (inner = TREE_OPERAND (exp, 0);
2060                TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
2061                inner = TREE_OPERAND (inner, 0))
2062             ;
2063           if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2064               && TREE_OPERAND (exp, 1) == f)
2065             return r;
2066
2067           /* If this expression hasn't been completed let, leave it
2068              alone.  */
2069           if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2070               && TREE_TYPE (inner) == 0)
2071             return exp;
2072
2073           op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2074           if (op0 == TREE_OPERAND (exp, 0))
2075             return exp;
2076
2077           new = fold (build (code, TREE_TYPE (exp), op0,
2078                              TREE_OPERAND (exp, 1)));
2079           break;
2080
2081         case BIT_FIELD_REF:
2082           op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2083           op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
2084           op2 = substitute_in_expr (TREE_OPERAND (exp, 2), f, r);
2085           if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2086               && op2 == TREE_OPERAND (exp, 2))
2087             return exp;
2088
2089           new = fold (build (code, TREE_TYPE (exp), op0, op1, op2));
2090           break;
2091
2092         case INDIRECT_REF:
2093         case BUFFER_REF:
2094           op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2095           if (op0 == TREE_OPERAND (exp, 0))
2096             return exp;
2097
2098           new = fold (build1 (code, TREE_TYPE (exp), op0));
2099           break;
2100
2101         default:
2102           abort ();
2103         }
2104       break;
2105
2106     default:
2107       abort ();
2108     }
2109
2110   TREE_READONLY (new) = TREE_READONLY (exp);
2111   return new;
2112 }
2113 \f
2114 /* Stabilize a reference so that we can use it any number of times
2115    without causing its operands to be evaluated more than once.
2116    Returns the stabilized reference.  This works by means of save_expr,
2117    so see the caveats in the comments about save_expr.
2118
2119    Also allows conversion expressions whose operands are references.
2120    Any other kind of expression is returned unchanged.  */
2121
2122 tree
2123 stabilize_reference (tree ref)
2124 {
2125   tree result;
2126   enum tree_code code = TREE_CODE (ref);
2127
2128   switch (code)
2129     {
2130     case VAR_DECL:
2131     case PARM_DECL:
2132     case RESULT_DECL:
2133       /* No action is needed in this case.  */
2134       return ref;
2135
2136     case NOP_EXPR:
2137     case CONVERT_EXPR:
2138     case FLOAT_EXPR:
2139     case FIX_TRUNC_EXPR:
2140     case FIX_FLOOR_EXPR:
2141     case FIX_ROUND_EXPR:
2142     case FIX_CEIL_EXPR:
2143       result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2144       break;
2145
2146     case INDIRECT_REF:
2147       result = build_nt (INDIRECT_REF,
2148                          stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2149       break;
2150
2151     case COMPONENT_REF:
2152       result = build_nt (COMPONENT_REF,
2153                          stabilize_reference (TREE_OPERAND (ref, 0)),
2154                          TREE_OPERAND (ref, 1));
2155       break;
2156
2157     case BIT_FIELD_REF:
2158       result = build_nt (BIT_FIELD_REF,
2159                          stabilize_reference (TREE_OPERAND (ref, 0)),
2160                          stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2161                          stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2162       break;
2163
2164     case ARRAY_REF:
2165       result = build_nt (ARRAY_REF,
2166                          stabilize_reference (TREE_OPERAND (ref, 0)),
2167                          stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2168       break;
2169
2170     case ARRAY_RANGE_REF:
2171       result = build_nt (ARRAY_RANGE_REF,
2172                          stabilize_reference (TREE_OPERAND (ref, 0)),
2173                          stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2174       break;
2175
2176     case COMPOUND_EXPR:
2177       /* We cannot wrap the first expression in a SAVE_EXPR, as then
2178          it wouldn't be ignored.  This matters when dealing with
2179          volatiles.  */
2180       return stabilize_reference_1 (ref);
2181
2182     case RTL_EXPR:
2183       result = build1 (INDIRECT_REF, TREE_TYPE (ref),
2184                        save_expr (build1 (ADDR_EXPR,
2185                                           build_pointer_type (TREE_TYPE (ref)),
2186                                           ref)));
2187       break;
2188
2189       /* If arg isn't a kind of lvalue we recognize, make no change.
2190          Caller should recognize the error for an invalid lvalue.  */
2191     default:
2192       return ref;
2193
2194     case ERROR_MARK:
2195       return error_mark_node;
2196     }
2197
2198   TREE_TYPE (result) = TREE_TYPE (ref);
2199   TREE_READONLY (result) = TREE_READONLY (ref);
2200   TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
2201   TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
2202
2203   return result;
2204 }
2205
2206 /* Subroutine of stabilize_reference; this is called for subtrees of
2207    references.  Any expression with side-effects must be put in a SAVE_EXPR
2208    to ensure that it is only evaluated once.
2209
2210    We don't put SAVE_EXPR nodes around everything, because assigning very
2211    simple expressions to temporaries causes us to miss good opportunities
2212    for optimizations.  Among other things, the opportunity to fold in the
2213    addition of a constant into an addressing mode often gets lost, e.g.
2214    "y[i+1] += x;".  In general, we take the approach that we should not make
2215    an assignment unless we are forced into it - i.e., that any non-side effect
2216    operator should be allowed, and that cse should take care of coalescing
2217    multiple utterances of the same expression should that prove fruitful.  */
2218
2219 tree
2220 stabilize_reference_1 (tree e)
2221 {
2222   tree result;
2223   enum tree_code code = TREE_CODE (e);
2224
2225   /* We cannot ignore const expressions because it might be a reference
2226      to a const array but whose index contains side-effects.  But we can
2227      ignore things that are actual constant or that already have been
2228      handled by this function.  */
2229
2230   if (TREE_CONSTANT (e) || code == SAVE_EXPR)
2231     return e;
2232
2233   switch (TREE_CODE_CLASS (code))
2234     {
2235     case 'x':
2236     case 't':
2237     case 'd':
2238     case 'b':
2239     case '<':
2240     case 's':
2241     case 'e':
2242     case 'r':
2243       /* If the expression has side-effects, then encase it in a SAVE_EXPR
2244          so that it will only be evaluated once.  */
2245       /* The reference (r) and comparison (<) classes could be handled as
2246          below, but it is generally faster to only evaluate them once.  */
2247       if (TREE_SIDE_EFFECTS (e))
2248         return save_expr (e);
2249       return e;
2250
2251     case 'c':
2252       /* Constants need no processing.  In fact, we should never reach
2253          here.  */
2254       return e;
2255
2256     case '2':
2257       /* Division is slow and tends to be compiled with jumps,
2258          especially the division by powers of 2 that is often
2259          found inside of an array reference.  So do it just once.  */
2260       if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
2261           || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
2262           || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
2263           || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
2264         return save_expr (e);
2265       /* Recursively stabilize each operand.  */
2266       result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
2267                          stabilize_reference_1 (TREE_OPERAND (e, 1)));
2268       break;
2269
2270     case '1':
2271       /* Recursively stabilize each operand.  */
2272       result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
2273       break;
2274
2275     default:
2276       abort ();
2277     }
2278
2279   TREE_TYPE (result) = TREE_TYPE (e);
2280   TREE_READONLY (result) = TREE_READONLY (e);
2281   TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
2282   TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
2283
2284   return result;
2285 }
2286 \f
2287 /* Low-level constructors for expressions.  */
2288
2289 /* Build an expression of code CODE, data type TYPE,
2290    and operands as specified by the arguments ARG1 and following arguments.
2291    Expressions and reference nodes can be created this way.
2292    Constants, decls, types and misc nodes cannot be.  */
2293
2294 tree
2295 build (enum tree_code code, tree tt, ...)
2296 {
2297   tree t;
2298   int length;
2299   int i;
2300   int fro;
2301   int constant;
2302   va_list p;
2303   tree node;
2304
2305   va_start (p, tt);
2306
2307   t = make_node (code);
2308   length = TREE_CODE_LENGTH (code);
2309   TREE_TYPE (t) = tt;
2310
2311   /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
2312      result based on those same flags for the arguments.  But if the
2313      arguments aren't really even `tree' expressions, we shouldn't be trying
2314      to do this.  */
2315   fro = first_rtl_op (code);
2316
2317   /* Expressions without side effects may be constant if their
2318      arguments are as well.  */
2319   constant = (TREE_CODE_CLASS (code) == '<'
2320               || TREE_CODE_CLASS (code) == '1'
2321               || TREE_CODE_CLASS (code) == '2'
2322               || TREE_CODE_CLASS (code) == 'c');
2323
2324   if (length == 2)
2325     {
2326       /* This is equivalent to the loop below, but faster.  */
2327       tree arg0 = va_arg (p, tree);
2328       tree arg1 = va_arg (p, tree);
2329
2330       TREE_OPERAND (t, 0) = arg0;
2331       TREE_OPERAND (t, 1) = arg1;
2332       TREE_READONLY (t) = 1;
2333       if (arg0 && fro > 0)
2334         {
2335           if (TREE_SIDE_EFFECTS (arg0))
2336             TREE_SIDE_EFFECTS (t) = 1;
2337           if (!TREE_READONLY (arg0))
2338             TREE_READONLY (t) = 0;
2339           if (!TREE_CONSTANT (arg0))
2340             constant = 0;
2341         }
2342
2343       if (arg1 && fro > 1)
2344         {
2345           if (TREE_SIDE_EFFECTS (arg1))
2346             TREE_SIDE_EFFECTS (t) = 1;
2347           if (!TREE_READONLY (arg1))
2348             TREE_READONLY (t) = 0;
2349           if (!TREE_CONSTANT (arg1))
2350             constant = 0;
2351         }
2352     }
2353   else if (length == 1)
2354     {
2355       tree arg0 = va_arg (p, tree);
2356
2357       /* The only one-operand cases we handle here are those with side-effects.
2358          Others are handled with build1.  So don't bother checked if the
2359          arg has side-effects since we'll already have set it.
2360
2361          ??? This really should use build1 too.  */
2362       if (TREE_CODE_CLASS (code) != 's')
2363         abort ();
2364       TREE_OPERAND (t, 0) = arg0;
2365     }
2366   else
2367     {
2368       for (i = 0; i < length; i++)
2369         {
2370           tree operand = va_arg (p, tree);
2371
2372           TREE_OPERAND (t, i) = operand;
2373           if (operand && fro > i)
2374             {
2375               if (TREE_SIDE_EFFECTS (operand))
2376                 TREE_SIDE_EFFECTS (t) = 1;
2377               if (!TREE_CONSTANT (operand))
2378                 constant = 0;
2379             }
2380         }
2381     }
2382   va_end (p);
2383
2384   TREE_CONSTANT (t) = constant;
2385   
2386   if (code == CALL_EXPR && !TREE_SIDE_EFFECTS (t))
2387     {
2388       /* Calls have side-effects, except those to const or
2389          pure functions.  */
2390       i = call_expr_flags (t);
2391       if (!(i & (ECF_CONST | ECF_PURE)))
2392         TREE_SIDE_EFFECTS (t) = 1;
2393
2394       /* And even those have side-effects if their arguments do.  */
2395       else for (node = TREE_OPERAND (t, 1); node; node = TREE_CHAIN (node))
2396         if (TREE_SIDE_EFFECTS (TREE_VALUE (node)))
2397           {
2398             TREE_SIDE_EFFECTS (t) = 1;
2399             break;
2400           }
2401     }
2402
2403   return t;
2404 }
2405
2406 /* Same as above, but only builds for unary operators.
2407    Saves lions share of calls to `build'; cuts down use
2408    of varargs, which is expensive for RISC machines.  */
2409
2410 tree
2411 build1 (enum tree_code code, tree type, tree node)
2412 {
2413   int length = sizeof (struct tree_exp);
2414 #ifdef GATHER_STATISTICS
2415   tree_node_kind kind;
2416 #endif
2417   tree t;
2418
2419 #ifdef GATHER_STATISTICS
2420   switch (TREE_CODE_CLASS (code))
2421     {
2422     case 's':  /* an expression with side effects */
2423       kind = s_kind;
2424       break;
2425     case 'r':  /* a reference */
2426       kind = r_kind;
2427       break;
2428     default:
2429       kind = e_kind;
2430       break;
2431     }
2432
2433   tree_node_counts[(int) kind]++;
2434   tree_node_sizes[(int) kind] += length;
2435 #endif
2436
2437 #ifdef ENABLE_CHECKING
2438   if (TREE_CODE_CLASS (code) == '2'
2439       || TREE_CODE_CLASS (code) == '<'
2440       || TREE_CODE_LENGTH (code) != 1)
2441     abort ();
2442 #endif /* ENABLE_CHECKING */
2443
2444   t = ggc_alloc_tree (length);
2445
2446   memset (t, 0, sizeof (struct tree_common));
2447
2448   TREE_SET_CODE (t, code);
2449
2450   TREE_TYPE (t) = type;
2451   TREE_COMPLEXITY (t) = 0;
2452   TREE_OPERAND (t, 0) = node;
2453   if (node && first_rtl_op (code) != 0)
2454     {
2455       TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
2456       TREE_READONLY (t) = TREE_READONLY (node);
2457     }
2458
2459   if (TREE_CODE_CLASS (code) == 's')
2460     TREE_SIDE_EFFECTS (t) = 1;
2461   else switch (code)
2462     {
2463     case INIT_EXPR:
2464     case MODIFY_EXPR:
2465     case VA_ARG_EXPR:
2466     case RTL_EXPR:
2467     case PREDECREMENT_EXPR:
2468     case PREINCREMENT_EXPR:
2469     case POSTDECREMENT_EXPR:
2470     case POSTINCREMENT_EXPR:
2471       /* All of these have side-effects, no matter what their
2472          operands are.  */
2473       TREE_SIDE_EFFECTS (t) = 1;
2474       TREE_READONLY (t) = 0;
2475       break;
2476
2477     case INDIRECT_REF:
2478       /* Whether a dereference is readonly has nothing to do with whether
2479          its operand is readonly.  */
2480       TREE_READONLY (t) = 0;
2481       break;
2482
2483     case ADDR_EXPR:
2484       if (node)
2485         {
2486           /* The address of a volatile decl or reference does not have
2487              side-effects.  But be careful not to ignore side-effects from
2488              other sources deeper in the expression--if node is a _REF and
2489              one of its operands has side-effects, so do we.  */
2490           if (TREE_THIS_VOLATILE (node))
2491             {
2492               TREE_SIDE_EFFECTS (t) = 0;
2493               if (!DECL_P (node))
2494                 {
2495                   int i = first_rtl_op (TREE_CODE (node)) - 1;
2496                   for (; i >= 0; --i)
2497                     {
2498                       if (TREE_SIDE_EFFECTS (TREE_OPERAND (node, i)))
2499                         TREE_SIDE_EFFECTS (t) = 1;
2500                     }
2501                 }
2502             }
2503         }
2504       break;
2505
2506     default:
2507       if (TREE_CODE_CLASS (code) == '1' && node && TREE_CONSTANT (node))
2508         TREE_CONSTANT (t) = 1;
2509       break;
2510     }
2511
2512   return t;
2513 }
2514
2515 /* Similar except don't specify the TREE_TYPE
2516    and leave the TREE_SIDE_EFFECTS as 0.
2517    It is permissible for arguments to be null,
2518    or even garbage if their values do not matter.  */
2519
2520 tree
2521 build_nt (enum tree_code code, ...)
2522 {
2523   tree t;
2524   int length;
2525   int i;
2526   va_list p;
2527
2528   va_start (p, code);
2529
2530   t = make_node (code);
2531   length = TREE_CODE_LENGTH (code);
2532
2533   for (i = 0; i < length; i++)
2534     TREE_OPERAND (t, i) = va_arg (p, tree);
2535
2536   va_end (p);
2537   return t;
2538 }
2539 \f
2540 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
2541    We do NOT enter this node in any sort of symbol table.
2542
2543    layout_decl is used to set up the decl's storage layout.
2544    Other slots are initialized to 0 or null pointers.  */
2545
2546 tree
2547 build_decl (enum tree_code code, tree name, tree type)
2548 {
2549   tree t;
2550
2551   t = make_node (code);
2552
2553 /*  if (type == error_mark_node)
2554     type = integer_type_node; */
2555 /* That is not done, deliberately, so that having error_mark_node
2556    as the type can suppress useless errors in the use of this variable.  */
2557
2558   DECL_NAME (t) = name;
2559   TREE_TYPE (t) = type;
2560
2561   if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
2562     layout_decl (t, 0);
2563   else if (code == FUNCTION_DECL)
2564     DECL_MODE (t) = FUNCTION_MODE;
2565
2566   return t;
2567 }
2568 \f
2569 /* BLOCK nodes are used to represent the structure of binding contours
2570    and declarations, once those contours have been exited and their contents
2571    compiled.  This information is used for outputting debugging info.  */
2572
2573 tree
2574 build_block (tree vars, tree tags ATTRIBUTE_UNUSED, tree subblocks,
2575              tree supercontext, tree chain)
2576 {
2577   tree block = make_node (BLOCK);
2578
2579   BLOCK_VARS (block) = vars;
2580   BLOCK_SUBBLOCKS (block) = subblocks;
2581   BLOCK_SUPERCONTEXT (block) = supercontext;
2582   BLOCK_CHAIN (block) = chain;
2583   return block;
2584 }
2585
2586 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
2587    location where an expression or an identifier were encountered. It
2588    is necessary for languages where the frontend parser will handle
2589    recursively more than one file (Java is one of them).  */
2590
2591 tree
2592 build_expr_wfl (tree node, const char *file, int line, int col)
2593 {
2594   static const char *last_file = 0;
2595   static tree last_filenode = NULL_TREE;
2596   tree wfl = make_node (EXPR_WITH_FILE_LOCATION);
2597
2598   EXPR_WFL_NODE (wfl) = node;
2599   EXPR_WFL_SET_LINECOL (wfl, line, col);
2600   if (file != last_file)
2601     {
2602       last_file = file;
2603       last_filenode = file ? get_identifier (file) : NULL_TREE;
2604     }
2605
2606   EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
2607   if (node)
2608     {
2609       TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
2610       TREE_TYPE (wfl) = TREE_TYPE (node);
2611     }
2612
2613   return wfl;
2614 }
2615 \f
2616 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
2617    is ATTRIBUTE.  */
2618
2619 tree
2620 build_decl_attribute_variant (tree ddecl, tree attribute)
2621 {
2622   DECL_ATTRIBUTES (ddecl) = attribute;
2623   return ddecl;
2624 }
2625
2626 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
2627    is ATTRIBUTE.
2628
2629    Record such modified types already made so we don't make duplicates.  */
2630
2631 tree
2632 build_type_attribute_variant (tree ttype, tree attribute)
2633 {
2634   if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
2635     {
2636       unsigned int hashcode;
2637       tree ntype;
2638
2639       ntype = copy_node (ttype);
2640
2641       TYPE_POINTER_TO (ntype) = 0;
2642       TYPE_REFERENCE_TO (ntype) = 0;
2643       TYPE_ATTRIBUTES (ntype) = attribute;
2644
2645       /* Create a new main variant of TYPE.  */
2646       TYPE_MAIN_VARIANT (ntype) = ntype;
2647       TYPE_NEXT_VARIANT (ntype) = 0;
2648       set_type_quals (ntype, TYPE_UNQUALIFIED);
2649
2650       hashcode = (TYPE_HASH (TREE_CODE (ntype))
2651                   + TYPE_HASH (TREE_TYPE (ntype))
2652                   + attribute_hash_list (attribute));
2653
2654       switch (TREE_CODE (ntype))
2655         {
2656         case FUNCTION_TYPE:
2657           hashcode += TYPE_HASH (TYPE_ARG_TYPES (ntype));
2658           break;
2659         case ARRAY_TYPE:
2660           hashcode += TYPE_HASH (TYPE_DOMAIN (ntype));
2661           break;
2662         case INTEGER_TYPE:
2663           hashcode += TYPE_HASH (TYPE_MAX_VALUE (ntype));
2664           break;
2665         case REAL_TYPE:
2666           hashcode += TYPE_HASH (TYPE_PRECISION (ntype));
2667           break;
2668         default:
2669           break;
2670         }
2671
2672       ntype = type_hash_canon (hashcode, ntype);
2673       ttype = build_qualified_type (ntype, TYPE_QUALS (ttype));
2674     }
2675
2676   return ttype;
2677 }
2678
2679 /* Return nonzero if IDENT is a valid name for attribute ATTR,
2680    or zero if not.
2681
2682    We try both `text' and `__text__', ATTR may be either one.  */
2683 /* ??? It might be a reasonable simplification to require ATTR to be only
2684    `text'.  One might then also require attribute lists to be stored in
2685    their canonicalized form.  */
2686
2687 int
2688 is_attribute_p (const char *attr, tree ident)
2689 {
2690   int ident_len, attr_len;
2691   const char *p;
2692
2693   if (TREE_CODE (ident) != IDENTIFIER_NODE)
2694     return 0;
2695
2696   if (strcmp (attr, IDENTIFIER_POINTER (ident)) == 0)
2697     return 1;
2698
2699   p = IDENTIFIER_POINTER (ident);
2700   ident_len = strlen (p);
2701   attr_len = strlen (attr);
2702
2703   /* If ATTR is `__text__', IDENT must be `text'; and vice versa.  */
2704   if (attr[0] == '_')
2705     {
2706       if (attr[1] != '_'
2707           || attr[attr_len - 2] != '_'
2708           || attr[attr_len - 1] != '_')
2709         abort ();
2710       if (ident_len == attr_len - 4
2711           && strncmp (attr + 2, p, attr_len - 4) == 0)
2712         return 1;
2713     }
2714   else
2715     {
2716       if (ident_len == attr_len + 4
2717           && p[0] == '_' && p[1] == '_'
2718           && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
2719           && strncmp (attr, p + 2, attr_len) == 0)
2720         return 1;
2721     }
2722
2723   return 0;
2724 }
2725
2726 /* Given an attribute name and a list of attributes, return a pointer to the
2727    attribute's list element if the attribute is part of the list, or NULL_TREE
2728    if not found.  If the attribute appears more than once, this only
2729    returns the first occurrence; the TREE_CHAIN of the return value should
2730    be passed back in if further occurrences are wanted.  */
2731
2732 tree
2733 lookup_attribute (const char *attr_name, tree list)
2734 {
2735   tree l;
2736
2737   for (l = list; l; l = TREE_CHAIN (l))
2738     {
2739       if (TREE_CODE (TREE_PURPOSE (l)) != IDENTIFIER_NODE)
2740         abort ();
2741       if (is_attribute_p (attr_name, TREE_PURPOSE (l)))
2742         return l;
2743     }
2744
2745   return NULL_TREE;
2746 }
2747
2748 /* Return an attribute list that is the union of a1 and a2.  */
2749
2750 tree
2751 merge_attributes (tree a1, tree a2)
2752 {
2753   tree attributes;
2754
2755   /* Either one unset?  Take the set one.  */
2756
2757   if ((attributes = a1) == 0)
2758     attributes = a2;
2759
2760   /* One that completely contains the other?  Take it.  */
2761
2762   else if (a2 != 0 && ! attribute_list_contained (a1, a2))
2763     {
2764       if (attribute_list_contained (a2, a1))
2765         attributes = a2;
2766       else
2767         {
2768           /* Pick the longest list, and hang on the other list.  */
2769
2770           if (list_length (a1) < list_length (a2))
2771             attributes = a2, a2 = a1;
2772
2773           for (; a2 != 0; a2 = TREE_CHAIN (a2))
2774             {
2775               tree a;
2776               for (a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
2777                                          attributes);
2778                    a != NULL_TREE;
2779                    a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
2780                                          TREE_CHAIN (a)))
2781                 {
2782                   if (simple_cst_equal (TREE_VALUE (a), TREE_VALUE (a2)) == 1)
2783                     break;
2784                 }
2785               if (a == NULL_TREE)
2786                 {
2787                   a1 = copy_node (a2);
2788                   TREE_CHAIN (a1) = attributes;
2789                   attributes = a1;
2790                 }
2791             }
2792         }
2793     }
2794   return attributes;
2795 }
2796
2797 /* Given types T1 and T2, merge their attributes and return
2798   the result.  */
2799
2800 tree
2801 merge_type_attributes (tree t1, tree t2)
2802 {
2803   return merge_attributes (TYPE_ATTRIBUTES (t1),
2804                            TYPE_ATTRIBUTES (t2));
2805 }
2806
2807 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
2808    the result.  */
2809
2810 tree
2811 merge_decl_attributes (tree olddecl, tree newdecl)
2812 {
2813   return merge_attributes (DECL_ATTRIBUTES (olddecl),
2814                            DECL_ATTRIBUTES (newdecl));
2815 }
2816
2817 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
2818
2819 /* Specialization of merge_decl_attributes for various Windows targets.
2820
2821    This handles the following situation:
2822
2823      __declspec (dllimport) int foo;
2824      int foo;
2825
2826    The second instance of `foo' nullifies the dllimport.  */
2827
2828 tree
2829 merge_dllimport_decl_attributes (tree old, tree new)
2830 {
2831   tree a;
2832   int delete_dllimport_p;
2833
2834   old = DECL_ATTRIBUTES (old);
2835   new = DECL_ATTRIBUTES (new);
2836
2837   /* What we need to do here is remove from `old' dllimport if it doesn't
2838      appear in `new'.  dllimport behaves like extern: if a declaration is
2839      marked dllimport and a definition appears later, then the object
2840      is not dllimport'd.  */
2841   if (lookup_attribute ("dllimport", old) != NULL_TREE
2842       && lookup_attribute ("dllimport", new) == NULL_TREE)
2843     delete_dllimport_p = 1;
2844   else
2845     delete_dllimport_p = 0;
2846
2847   a = merge_attributes (old, new);
2848
2849   if (delete_dllimport_p)
2850     {
2851       tree prev, t;
2852
2853       /* Scan the list for dllimport and delete it.  */
2854       for (prev = NULL_TREE, t = a; t; prev = t, t = TREE_CHAIN (t))
2855         {
2856           if (is_attribute_p ("dllimport", TREE_PURPOSE (t)))
2857             {
2858               if (prev == NULL_TREE)
2859                 a = TREE_CHAIN (a);
2860               else
2861                 TREE_CHAIN (prev) = TREE_CHAIN (t);
2862               break;
2863             }
2864         }
2865     }
2866
2867   return a;
2868 }
2869
2870 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES  */
2871 \f
2872 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
2873    of the various TYPE_QUAL values.  */
2874
2875 static void
2876 set_type_quals (tree type, int type_quals)
2877 {
2878   TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
2879   TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
2880   TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
2881 }
2882
2883 /* Return a version of the TYPE, qualified as indicated by the
2884    TYPE_QUALS, if one exists.  If no qualified version exists yet,
2885    return NULL_TREE.  */
2886
2887 tree
2888 get_qualified_type (tree type, int type_quals)
2889 {
2890   tree t;
2891
2892   /* Search the chain of variants to see if there is already one there just
2893      like the one we need to have.  If so, use that existing one.  We must
2894      preserve the TYPE_NAME, since there is code that depends on this.  */
2895   for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2896     if (TYPE_QUALS (t) == type_quals && TYPE_NAME (t) == TYPE_NAME (type)
2897         && TYPE_CONTEXT (t) == TYPE_CONTEXT (type)
2898         && attribute_list_equal (TYPE_ATTRIBUTES (t), TYPE_ATTRIBUTES (type)))
2899       return t;
2900
2901   return NULL_TREE;
2902 }
2903
2904 /* Like get_qualified_type, but creates the type if it does not
2905    exist.  This function never returns NULL_TREE.  */
2906
2907 tree
2908 build_qualified_type (tree type, int type_quals)
2909 {
2910   tree t;
2911
2912   /* See if we already have the appropriate qualified variant.  */
2913   t = get_qualified_type (type, type_quals);
2914
2915   /* If not, build it.  */
2916   if (!t)
2917     {
2918       t = build_type_copy (type);
2919       set_type_quals (t, type_quals);
2920     }
2921
2922   return t;
2923 }
2924
2925 /* Create a new variant of TYPE, equivalent but distinct.
2926    This is so the caller can modify it.  */
2927
2928 tree
2929 build_type_copy (tree type)
2930 {
2931   tree t, m = TYPE_MAIN_VARIANT (type);
2932
2933   t = copy_node (type);
2934
2935   TYPE_POINTER_TO (t) = 0;
2936   TYPE_REFERENCE_TO (t) = 0;
2937
2938   /* Add this type to the chain of variants of TYPE.  */
2939   TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
2940   TYPE_NEXT_VARIANT (m) = t;
2941
2942   return t;
2943 }
2944 \f
2945 /* Hashing of types so that we don't make duplicates.
2946    The entry point is `type_hash_canon'.  */
2947
2948 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
2949    with types in the TREE_VALUE slots), by adding the hash codes
2950    of the individual types.  */
2951
2952 unsigned int
2953 type_hash_list (tree list)
2954 {
2955   unsigned int hashcode;
2956   tree tail;
2957
2958   for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
2959     hashcode += TYPE_HASH (TREE_VALUE (tail));
2960
2961   return hashcode;
2962 }
2963
2964 /* These are the Hashtable callback functions.  */
2965
2966 /* Returns true if the types are equal.  */
2967
2968 static int
2969 type_hash_eq (const void *va, const void *vb)
2970 {
2971   const struct type_hash *a = va, *b = vb;
2972   if (a->hash == b->hash
2973       && TREE_CODE (a->type) == TREE_CODE (b->type)
2974       && TREE_TYPE (a->type) == TREE_TYPE (b->type)
2975       && attribute_list_equal (TYPE_ATTRIBUTES (a->type),
2976                                TYPE_ATTRIBUTES (b->type))
2977       && TYPE_ALIGN (a->type) == TYPE_ALIGN (b->type)
2978       && (TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
2979           || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
2980                                  TYPE_MAX_VALUE (b->type)))
2981       && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
2982           || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
2983                                  TYPE_MIN_VALUE (b->type)))
2984       /* Note that TYPE_DOMAIN is TYPE_ARG_TYPES for FUNCTION_TYPE.  */
2985       && (TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type)
2986           || (TYPE_DOMAIN (a->type)
2987               && TREE_CODE (TYPE_DOMAIN (a->type)) == TREE_LIST
2988               && TYPE_DOMAIN (b->type)
2989               && TREE_CODE (TYPE_DOMAIN (b->type)) == TREE_LIST
2990               && type_list_equal (TYPE_DOMAIN (a->type),
2991                                   TYPE_DOMAIN (b->type)))))
2992     return 1;
2993   return 0;
2994 }
2995
2996 /* Return the cached hash value.  */
2997
2998 static hashval_t
2999 type_hash_hash (const void *item)
3000 {
3001   return ((const struct type_hash *) item)->hash;
3002 }
3003
3004 /* Look in the type hash table for a type isomorphic to TYPE.
3005    If one is found, return it.  Otherwise return 0.  */
3006
3007 tree
3008 type_hash_lookup (unsigned int hashcode, tree type)
3009 {
3010   struct type_hash *h, in;
3011
3012   /* The TYPE_ALIGN field of a type is set by layout_type(), so we
3013      must call that routine before comparing TYPE_ALIGNs.  */
3014   layout_type (type);
3015
3016   in.hash = hashcode;
3017   in.type = type;
3018
3019   h = htab_find_with_hash (type_hash_table, &in, hashcode);
3020   if (h)
3021     return h->type;
3022   return NULL_TREE;
3023 }
3024
3025 /* Add an entry to the type-hash-table
3026    for a type TYPE whose hash code is HASHCODE.  */
3027
3028 void
3029 type_hash_add (unsigned int hashcode, tree type)
3030 {
3031   struct type_hash *h;
3032   void **loc;
3033
3034   h = ggc_alloc (sizeof (struct type_hash));
3035   h->hash = hashcode;
3036   h->type = type;
3037   loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
3038   *(struct type_hash **) loc = h;
3039 }
3040
3041 /* Given TYPE, and HASHCODE its hash code, return the canonical
3042    object for an identical type if one already exists.
3043    Otherwise, return TYPE, and record it as the canonical object
3044    if it is a permanent object.
3045
3046    To use this function, first create a type of the sort you want.
3047    Then compute its hash code from the fields of the type that
3048    make it different from other similar types.
3049    Then call this function and use the value.
3050    This function frees the type you pass in if it is a duplicate.  */
3051
3052 /* Set to 1 to debug without canonicalization.  Never set by program.  */
3053 int debug_no_type_hash = 0;
3054
3055 tree
3056 type_hash_canon (unsigned int hashcode, tree type)
3057 {
3058   tree t1;
3059
3060   if (debug_no_type_hash)
3061     return type;
3062
3063   /* See if the type is in the hash table already.  If so, return it.
3064      Otherwise, add the type.  */
3065   t1 = type_hash_lookup (hashcode, type);
3066   if (t1 != 0)
3067     {
3068 #ifdef GATHER_STATISTICS
3069       tree_node_counts[(int) t_kind]--;
3070       tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
3071 #endif
3072       return t1;
3073     }
3074   else
3075     {
3076       type_hash_add (hashcode, type);
3077       return type;
3078     }
3079 }
3080
3081 /* See if the data pointed to by the type hash table is marked.  We consider
3082    it marked if the type is marked or if a debug type number or symbol
3083    table entry has been made for the type.  This reduces the amount of
3084    debugging output and eliminates that dependency of the debug output on
3085    the number of garbage collections.  */
3086
3087 static int
3088 type_hash_marked_p (const void *p)
3089 {
3090   tree type = ((struct type_hash *) p)->type;
3091
3092   return ggc_marked_p (type) || TYPE_SYMTAB_POINTER (type);
3093 }
3094
3095 static void
3096 print_type_hash_statistics (void)
3097 {
3098   fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
3099            (long) htab_size (type_hash_table),
3100            (long) htab_elements (type_hash_table),
3101            htab_collisions (type_hash_table));
3102 }
3103
3104 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
3105    with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
3106    by adding the hash codes of the individual attributes.  */
3107
3108 unsigned int
3109 attribute_hash_list (tree list)
3110 {
3111   unsigned int hashcode;
3112   tree tail;
3113
3114   for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
3115     /* ??? Do we want to add in TREE_VALUE too? */
3116     hashcode += TYPE_HASH (TREE_PURPOSE (tail));
3117   return hashcode;
3118 }
3119
3120 /* Given two lists of attributes, return true if list l2 is
3121    equivalent to l1.  */
3122
3123 int
3124 attribute_list_equal (tree l1, tree l2)
3125 {
3126   return attribute_list_contained (l1, l2)
3127          && attribute_list_contained (l2, l1);
3128 }
3129
3130 /* Given two lists of attributes, return true if list L2 is
3131    completely contained within L1.  */
3132 /* ??? This would be faster if attribute names were stored in a canonicalized
3133    form.  Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
3134    must be used to show these elements are equivalent (which they are).  */
3135 /* ??? It's not clear that attributes with arguments will always be handled
3136    correctly.  */
3137
3138 int
3139 attribute_list_contained (tree l1, tree l2)
3140 {
3141   tree t1, t2;
3142
3143   /* First check the obvious, maybe the lists are identical.  */
3144   if (l1 == l2)
3145     return 1;
3146
3147   /* Maybe the lists are similar.  */
3148   for (t1 = l1, t2 = l2;
3149        t1 != 0 && t2 != 0
3150         && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
3151         && TREE_VALUE (t1) == TREE_VALUE (t2);
3152        t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
3153
3154   /* Maybe the lists are equal.  */
3155   if (t1 == 0 && t2 == 0)
3156     return 1;
3157
3158   for (; t2 != 0; t2 = TREE_CHAIN (t2))
3159     {
3160       tree attr;
3161       for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)), l1);
3162            attr != NULL_TREE;
3163            attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
3164                                     TREE_CHAIN (attr)))
3165         {
3166           if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) == 1)
3167             break;
3168         }
3169
3170       if (attr == 0)
3171         return 0;
3172
3173       if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) != 1)
3174         return 0;
3175     }
3176
3177   return 1;
3178 }
3179
3180 /* Given two lists of types
3181    (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
3182    return 1 if the lists contain the same types in the same order.
3183    Also, the TREE_PURPOSEs must match.  */
3184
3185 int
3186 type_list_equal (tree l1, tree l2)
3187 {
3188   tree t1, t2;
3189
3190   for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
3191     if (TREE_VALUE (t1) != TREE_VALUE (t2)
3192         || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
3193             && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
3194                   && (TREE_TYPE (TREE_PURPOSE (t1))
3195                       == TREE_TYPE (TREE_PURPOSE (t2))))))
3196       return 0;
3197
3198   return t1 == t2;
3199 }
3200
3201 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
3202    given by TYPE.  If the argument list accepts variable arguments,
3203    then this function counts only the ordinary arguments.  */
3204
3205 int
3206 type_num_arguments (tree type)
3207 {
3208   int i = 0;
3209   tree t;
3210
3211   for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
3212     /* If the function does not take a variable number of arguments,
3213        the last element in the list will have type `void'.  */
3214     if (VOID_TYPE_P (TREE_VALUE (t)))
3215       break;
3216     else
3217       ++i;
3218
3219   return i;
3220 }
3221
3222 /* Nonzero if integer constants T1 and T2
3223    represent the same constant value.  */
3224
3225 int
3226 tree_int_cst_equal (tree t1, tree t2)
3227 {
3228   if (t1 == t2)
3229     return 1;
3230
3231   if (t1 == 0 || t2 == 0)
3232     return 0;
3233
3234   if (TREE_CODE (t1) == INTEGER_CST
3235       && TREE_CODE (t2) == INTEGER_CST
3236       && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3237       && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
3238     return 1;
3239
3240   return 0;
3241 }
3242
3243 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
3244    The precise way of comparison depends on their data type.  */
3245
3246 int
3247 tree_int_cst_lt (tree t1, tree t2)
3248 {
3249   if (t1 == t2)
3250     return 0;
3251
3252   if (TREE_UNSIGNED (TREE_TYPE (t1)) != TREE_UNSIGNED (TREE_TYPE (t2)))
3253     {
3254       int t1_sgn = tree_int_cst_sgn (t1);
3255       int t2_sgn = tree_int_cst_sgn (t2);
3256
3257       if (t1_sgn < t2_sgn)
3258         return 1;
3259       else if (t1_sgn > t2_sgn)
3260         return 0;
3261       /* Otherwise, both are non-negative, so we compare them as
3262          unsigned just in case one of them would overflow a signed
3263          type.  */
3264     }
3265   else if (! TREE_UNSIGNED (TREE_TYPE (t1)))
3266     return INT_CST_LT (t1, t2);
3267
3268   return INT_CST_LT_UNSIGNED (t1, t2);
3269 }
3270
3271 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2.  */
3272
3273 int
3274 tree_int_cst_compare (tree t1, tree t2)
3275 {
3276   if (tree_int_cst_lt (t1, t2))
3277     return -1;
3278   else if (tree_int_cst_lt (t2, t1))
3279     return 1;
3280   else
3281     return 0;
3282 }
3283
3284 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
3285    the host.  If POS is zero, the value can be represented in a single
3286    HOST_WIDE_INT.  If POS is nonzero, the value must be positive and can
3287    be represented in a single unsigned HOST_WIDE_INT.  */
3288
3289 int
3290 host_integerp (tree t, int pos)
3291 {
3292   return (TREE_CODE (t) == INTEGER_CST
3293           && ! TREE_OVERFLOW (t)
3294           && ((TREE_INT_CST_HIGH (t) == 0
3295                && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
3296               || (! pos && TREE_INT_CST_HIGH (t) == -1
3297                   && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
3298                   && ! TREE_UNSIGNED (TREE_TYPE (t)))
3299               || (pos && TREE_INT_CST_HIGH (t) == 0)));
3300 }
3301
3302 /* Return the HOST_WIDE_INT least significant bits of T if it is an
3303    INTEGER_CST and there is no overflow.  POS is nonzero if the result must
3304    be positive.  Abort if we cannot satisfy the above conditions.  */
3305
3306 HOST_WIDE_INT
3307 tree_low_cst (tree t, int pos)
3308 {
3309   if (host_integerp (t, pos))
3310     return TREE_INT_CST_LOW (t);
3311   else
3312     abort ();
3313 }
3314
3315 /* Return the most significant bit of the integer constant T.  */
3316
3317 int
3318 tree_int_cst_msb (tree t)
3319 {
3320   int prec;
3321   HOST_WIDE_INT h;
3322   unsigned HOST_WIDE_INT l;
3323
3324   /* Note that using TYPE_PRECISION here is wrong.  We care about the
3325      actual bits, not the (arbitrary) range of the type.  */
3326   prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
3327   rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
3328                  2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
3329   return (l & 1) == 1;
3330 }
3331
3332 /* Return an indication of the sign of the integer constant T.
3333    The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
3334    Note that -1 will never be returned it T's type is unsigned.  */
3335
3336 int
3337 tree_int_cst_sgn (tree t)
3338 {
3339   if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
3340     return 0;
3341   else if (TREE_UNSIGNED (TREE_TYPE (t)))
3342     return 1;
3343   else if (TREE_INT_CST_HIGH (t) < 0)
3344     return -1;
3345   else
3346     return 1;
3347 }
3348
3349 /* Compare two constructor-element-type constants.  Return 1 if the lists
3350    are known to be equal; otherwise return 0.  */
3351
3352 int
3353 simple_cst_list_equal (tree l1, tree l2)
3354 {
3355   while (l1 != NULL_TREE && l2 != NULL_TREE)
3356     {
3357       if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
3358         return 0;
3359
3360       l1 = TREE_CHAIN (l1);
3361       l2 = TREE_CHAIN (l2);
3362     }
3363
3364   return l1 == l2;
3365 }
3366
3367 /* Return truthvalue of whether T1 is the same tree structure as T2.
3368    Return 1 if they are the same.
3369    Return 0 if they are understandably different.
3370    Return -1 if either contains tree structure not understood by
3371    this function.  */
3372
3373 int
3374 simple_cst_equal (tree t1, tree t2)
3375 {
3376   enum tree_code code1, code2;
3377   int cmp;
3378   int i;
3379
3380   if (t1 == t2)
3381     return 1;
3382   if (t1 == 0 || t2 == 0)
3383     return 0;
3384
3385   code1 = TREE_CODE (t1);
3386   code2 = TREE_CODE (t2);
3387
3388   if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
3389     {
3390       if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3391           || code2 == NON_LVALUE_EXPR)
3392         return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3393       else
3394         return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
3395     }
3396
3397   else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3398            || code2 == NON_LVALUE_EXPR)
3399     return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
3400
3401   if (code1 != code2)
3402     return 0;
3403
3404   switch (code1)
3405     {
3406     case INTEGER_CST:
3407       return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3408               && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
3409
3410     case REAL_CST:
3411       return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3412
3413     case STRING_CST:
3414       return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3415               && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3416                          TREE_STRING_LENGTH (t1)));
3417
3418     case CONSTRUCTOR:
3419       if (CONSTRUCTOR_ELTS (t1) == CONSTRUCTOR_ELTS (t2))
3420         return 1;
3421       else
3422         abort ();
3423
3424     case SAVE_EXPR:
3425       return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3426
3427     case CALL_EXPR:
3428       cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3429       if (cmp <= 0)
3430         return cmp;
3431       return
3432         simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3433
3434     case TARGET_EXPR:
3435       /* Special case: if either target is an unallocated VAR_DECL,
3436          it means that it's going to be unified with whatever the
3437          TARGET_EXPR is really supposed to initialize, so treat it
3438          as being equivalent to anything.  */
3439       if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
3440            && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
3441            && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
3442           || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
3443               && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
3444               && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
3445         cmp = 1;
3446       else
3447         cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3448
3449       if (cmp <= 0)
3450         return cmp;
3451
3452       return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3453
3454     case WITH_CLEANUP_EXPR:
3455       cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3456       if (cmp <= 0)
3457         return cmp;
3458
3459       return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
3460
3461     case COMPONENT_REF:
3462       if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
3463         return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3464
3465       return 0;
3466
3467     case VAR_DECL:
3468     case PARM_DECL:
3469     case CONST_DECL:
3470     case FUNCTION_DECL:
3471       return 0;
3472
3473     default:
3474       break;
3475     }
3476
3477   /* This general rule works for most tree codes.  All exceptions should be
3478      handled above.  If this is a language-specific tree code, we can't
3479      trust what might be in the operand, so say we don't know
3480      the situation.  */
3481   if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
3482     return -1;
3483
3484   switch (TREE_CODE_CLASS (code1))
3485     {
3486     case '1':
3487     case '2':
3488     case '<':
3489     case 'e':
3490     case 'r':
3491     case 's':
3492       cmp = 1;
3493       for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
3494         {
3495           cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
3496           if (cmp <= 0)
3497             return cmp;
3498         }
3499
3500       return cmp;
3501
3502     default:
3503       return -1;
3504     }
3505 }
3506
3507 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
3508    Return -1, 0, or 1 if the value of T is less than, equal to, or greater
3509    than U, respectively.  */
3510
3511 int
3512 compare_tree_int (tree t, unsigned HOST_WIDE_INT u)
3513 {
3514   if (tree_int_cst_sgn (t) < 0)
3515     return -1;
3516   else if (TREE_INT_CST_HIGH (t) != 0)
3517     return 1;
3518   else if (TREE_INT_CST_LOW (t) == u)
3519     return 0;
3520   else if (TREE_INT_CST_LOW (t) < u)
3521     return -1;
3522   else
3523     return 1;
3524 }
3525
3526 /* Return true if CODE represents an associative tree code.  Otherwise
3527    return false.  */
3528 bool
3529 associative_tree_code (enum tree_code code)
3530 {
3531   switch (code)
3532     {
3533     case BIT_IOR_EXPR:
3534     case BIT_AND_EXPR:
3535     case BIT_XOR_EXPR:
3536     case PLUS_EXPR:
3537     case MINUS_EXPR:
3538     case MULT_EXPR:
3539     case LSHIFT_EXPR:
3540     case RSHIFT_EXPR:
3541     case MIN_EXPR:
3542     case MAX_EXPR:
3543       return true;
3544
3545     default:
3546       break;
3547     }
3548   return false;
3549 }
3550
3551 /* Return true if CODE represents an commutative tree code.  Otherwise
3552    return false.  */
3553 bool
3554 commutative_tree_code (enum tree_code code)
3555 {
3556   switch (code)
3557     {
3558     case PLUS_EXPR:
3559     case MULT_EXPR:
3560     case MIN_EXPR:
3561     case MAX_EXPR:
3562     case BIT_IOR_EXPR:
3563     case BIT_XOR_EXPR:
3564     case BIT_AND_EXPR:
3565     case NE_EXPR:
3566     case EQ_EXPR:
3567       return true;
3568
3569     default:
3570       break;
3571     }
3572   return false;
3573 }
3574
3575 /* Generate a hash value for an expression.  This can be used iteratively
3576    by passing a previous result as the "val" argument.
3577
3578    This function is intended to produce the same hash for expressions which
3579    would compare equal using operand_equal_p.  */
3580
3581 hashval_t
3582 iterative_hash_expr (tree t, hashval_t val)
3583 {
3584   int i;
3585   enum tree_code code;
3586   char class;
3587
3588   if (t == NULL_TREE)
3589     return iterative_hash_object (t, val);
3590
3591   code = TREE_CODE (t);
3592   class = TREE_CODE_CLASS (code);
3593
3594   if (class == 'd')
3595     {
3596       /* Decls we can just compare by pointer.  */
3597       val = iterative_hash_object (t, val);
3598     }
3599   else if (class == 'c')
3600     {
3601       /* Alas, constants aren't shared, so we can't rely on pointer
3602          identity.  */
3603       if (code == INTEGER_CST)
3604         {
3605           val = iterative_hash_object (TREE_INT_CST_LOW (t), val);
3606           val = iterative_hash_object (TREE_INT_CST_HIGH (t), val);
3607         }
3608       else if (code == REAL_CST)
3609         val = iterative_hash (TREE_REAL_CST_PTR (t),
3610                               sizeof (REAL_VALUE_TYPE), val);
3611       else if (code == STRING_CST)
3612         val = iterative_hash (TREE_STRING_POINTER (t),
3613                               TREE_STRING_LENGTH (t), val);
3614       else if (code == COMPLEX_CST)
3615         {
3616           val = iterative_hash_expr (TREE_REALPART (t), val);
3617           val = iterative_hash_expr (TREE_IMAGPART (t), val);
3618         }
3619       else if (code == VECTOR_CST)
3620         val = iterative_hash_expr (TREE_VECTOR_CST_ELTS (t), val);
3621       else
3622         abort ();
3623     }
3624   else if (IS_EXPR_CODE_CLASS (class))
3625     {
3626       val = iterative_hash_object (code, val);
3627
3628       if (code == NOP_EXPR || code == CONVERT_EXPR
3629           || code == NON_LVALUE_EXPR)
3630         val = iterative_hash_object (TREE_TYPE (t), val);
3631
3632       if (commutative_tree_code (code))
3633         {
3634           /* It's a commutative expression.  We want to hash it the same
3635              however it appears.  We do this by first hashing both operands
3636              and then rehashing based on the order of their independent
3637              hashes.  */
3638           hashval_t one = iterative_hash_expr (TREE_OPERAND (t, 0), 0);
3639           hashval_t two = iterative_hash_expr (TREE_OPERAND (t, 1), 0);
3640           hashval_t t;
3641
3642           if (one > two)
3643             t = one, one = two, two = t;
3644
3645           val = iterative_hash_object (one, val);
3646           val = iterative_hash_object (two, val);
3647         }
3648       else
3649         for (i = first_rtl_op (code) - 1; i >= 0; --i)
3650           val = iterative_hash_expr (TREE_OPERAND (t, i), val);
3651     }
3652   else if (code == TREE_LIST)
3653     {
3654       /* A list of expressions, for a CALL_EXPR or as the elements of a
3655          VECTOR_CST.  */
3656       for (; t; t = TREE_CHAIN (t))
3657         val = iterative_hash_expr (TREE_VALUE (t), val);
3658     }
3659   else
3660     abort ();
3661
3662   return val;
3663 }
3664 \f
3665 /* Constructors for pointer, array and function types.
3666    (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
3667    constructed by language-dependent code, not here.)  */
3668
3669 /* Construct, lay out and return the type of pointers to TO_TYPE
3670    with mode MODE. If such a type has already been constructed,
3671    reuse it.  */
3672
3673 tree
3674 build_pointer_type_for_mode (tree to_type, enum machine_mode mode)
3675 {
3676   tree t = TYPE_POINTER_TO (to_type);
3677
3678   /* First, if we already have a type for pointers to TO_TYPE, use it.  */
3679   if (t != 0 && mode == ptr_mode)
3680     return t;
3681
3682   t = make_node (POINTER_TYPE);
3683
3684   TREE_TYPE (t) = to_type;
3685   TYPE_MODE (t) = mode;
3686
3687   /* Record this type as the pointer to TO_TYPE.  */
3688   if (mode == ptr_mode)
3689   TYPE_POINTER_TO (to_type) = t;
3690
3691   /* Lay out the type.  This function has many callers that are concerned
3692      with expression-construction, and this simplifies them all.
3693      Also, it guarantees the TYPE_SIZE is in the same obstack as the type.  */
3694   layout_type (t);
3695
3696   return t;
3697 }
3698
3699 /* By default build pointers in ptr_mode.  */
3700
3701 tree
3702 build_pointer_type (tree to_type)
3703 {
3704   return build_pointer_type_for_mode (to_type, ptr_mode);
3705 }
3706
3707 /* Construct, lay out and return the type of references to TO_TYPE
3708    with mode MODE. If such a type has already been constructed,
3709    reuse it.  */
3710
3711 tree
3712 build_reference_type_for_mode (tree to_type, enum machine_mode mode)
3713 {
3714   tree t = TYPE_REFERENCE_TO (to_type);
3715
3716   /* First, if we already have a type for pointers to TO_TYPE, use it.  */
3717   if (t != 0 && mode == ptr_mode)
3718     return t;
3719
3720   t = make_node (REFERENCE_TYPE);
3721
3722   TREE_TYPE (t) = to_type;
3723   TYPE_MODE (t) = mode;
3724
3725   /* Record this type as the pointer to TO_TYPE.  */
3726   if (mode == ptr_mode)
3727   TYPE_REFERENCE_TO (to_type) = t;
3728
3729   layout_type (t);
3730
3731   return t;
3732 }
3733
3734
3735 /* Build the node for the type of references-to-TO_TYPE by default
3736    in ptr_mode.  */
3737
3738 tree
3739 build_reference_type (tree to_type)
3740 {
3741   return build_reference_type_for_mode (to_type, ptr_mode);
3742 }
3743
3744 /* Build a type that is compatible with t but has no cv quals anywhere
3745    in its type, thus
3746
3747    const char *const *const *  ->  char ***.  */
3748
3749 tree
3750 build_type_no_quals (tree t)
3751 {
3752   switch (TREE_CODE (t))
3753     {
3754     case POINTER_TYPE:
3755       return build_pointer_type (build_type_no_quals (TREE_TYPE (t)));
3756     case REFERENCE_TYPE:
3757       return build_reference_type (build_type_no_quals (TREE_TYPE (t)));
3758     default:
3759       return TYPE_MAIN_VARIANT (t);
3760     }
3761 }
3762
3763 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
3764    MAXVAL should be the maximum value in the domain
3765    (one less than the length of the array).
3766
3767    The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
3768    We don't enforce this limit, that is up to caller (e.g. language front end).
3769    The limit exists because the result is a signed type and we don't handle
3770    sizes that use more than one HOST_WIDE_INT.  */
3771
3772 tree
3773 build_index_type (tree maxval)
3774 {
3775   tree itype = make_node (INTEGER_TYPE);
3776
3777   TREE_TYPE (itype) = sizetype;
3778   TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
3779   TYPE_MIN_VALUE (itype) = size_zero_node;
3780   TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
3781   TYPE_MODE (itype) = TYPE_MODE (sizetype);
3782   TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
3783   TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
3784   TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
3785   TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
3786
3787   if (host_integerp (maxval, 1))
3788     return type_hash_canon (tree_low_cst (maxval, 1), itype);
3789   else
3790     return itype;
3791 }
3792
3793 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
3794    ENUMERAL_TYPE, BOOLEAN_TYPE, or CHAR_TYPE), with
3795    low bound LOWVAL and high bound HIGHVAL.
3796    if TYPE==NULL_TREE, sizetype is used.  */
3797
3798 tree
3799 build_range_type (tree type, tree lowval, tree highval)
3800 {
3801   tree itype = make_node (INTEGER_TYPE);
3802
3803   TREE_TYPE (itype) = type;
3804   if (type == NULL_TREE)
3805     type = sizetype;
3806
3807   TYPE_MIN_VALUE (itype) = convert (type, lowval);
3808   TYPE_MAX_VALUE (itype) = highval ? convert (type, highval) : NULL;
3809
3810   TYPE_PRECISION (itype) = TYPE_PRECISION (type);
3811   TYPE_MODE (itype) = TYPE_MODE (type);
3812   TYPE_SIZE (itype) = TYPE_SIZE (type);
3813   TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
3814   TYPE_ALIGN (itype) = TYPE_ALIGN (type);
3815   TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
3816
3817   if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
3818     return type_hash_canon (tree_low_cst (highval, 0)
3819                             - tree_low_cst (lowval, 0),
3820                             itype);
3821   else
3822     return itype;
3823 }
3824
3825 /* Just like build_index_type, but takes lowval and highval instead
3826    of just highval (maxval).  */
3827
3828 tree
3829 build_index_2_type (tree lowval, tree highval)
3830 {
3831   return build_range_type (sizetype, lowval, highval);
3832 }
3833
3834 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
3835    and number of elements specified by the range of values of INDEX_TYPE.
3836    If such a type has already been constructed, reuse it.  */
3837
3838 tree
3839 build_array_type (tree elt_type, tree index_type)
3840 {
3841   tree t;
3842   unsigned int hashcode;
3843
3844   if (TREE_CODE (elt_type) == FUNCTION_TYPE)
3845     {
3846       error ("arrays of functions are not meaningful");
3847       elt_type = integer_type_node;
3848     }
3849
3850   /* Make sure TYPE_POINTER_TO (elt_type) is filled in.  */
3851   build_pointer_type (elt_type);
3852
3853   /* Allocate the array after the pointer type,
3854      in case we free it in type_hash_canon.  */
3855   t = make_node (ARRAY_TYPE);
3856   TREE_TYPE (t) = elt_type;
3857   TYPE_DOMAIN (t) = index_type;
3858
3859   if (index_type == 0)
3860     {
3861       return t;
3862     }
3863
3864   hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
3865   t = type_hash_canon (hashcode, t);
3866
3867   if (!COMPLETE_TYPE_P (t))
3868     layout_type (t);
3869   return t;
3870 }
3871
3872 /* Return the TYPE of the elements comprising
3873    the innermost dimension of ARRAY.  */
3874
3875 tree
3876 get_inner_array_type (tree array)
3877 {
3878   tree type = TREE_TYPE (array);
3879
3880   while (TREE_CODE (type) == ARRAY_TYPE)
3881     type = TREE_TYPE (type);
3882
3883   return type;
3884 }
3885
3886 /* Construct, lay out and return
3887    the type of functions returning type VALUE_TYPE
3888    given arguments of types ARG_TYPES.
3889    ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
3890    are data type nodes for the arguments of the function.
3891    If such a type has already been constructed, reuse it.  */
3892
3893 tree
3894 build_function_type (tree value_type, tree arg_types)
3895 {
3896   tree t;
3897   unsigned int hashcode;
3898
3899   if (TREE_CODE (value_type) == FUNCTION_TYPE)
3900     {
3901       error ("function return type cannot be function");
3902       value_type = integer_type_node;
3903     }
3904
3905   /* Make a node of the sort we want.  */
3906   t = make_node (FUNCTION_TYPE);
3907   TREE_TYPE (t) = value_type;
3908   TYPE_ARG_TYPES (t) = arg_types;
3909
3910   /* If we already have such a type, use the old one and free this one.  */
3911   hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
3912   t = type_hash_canon (hashcode, t);
3913
3914   if (!COMPLETE_TYPE_P (t))
3915     layout_type (t);
3916   return t;
3917 }
3918
3919 /* Build a function type.  The RETURN_TYPE is the type returned by the
3920    function.  If additional arguments are provided, they are
3921    additional argument types.  The list of argument types must always
3922    be terminated by NULL_TREE.  */
3923
3924 tree
3925 build_function_type_list (tree return_type, ...)
3926 {
3927   tree t, args, last;
3928   va_list p;
3929
3930   va_start (p, return_type);
3931
3932   t = va_arg (p, tree);
3933   for (args = NULL_TREE; t != NULL_TREE; t = va_arg (p, tree))
3934     args = tree_cons (NULL_TREE, t, args);
3935
3936   last = args;
3937   args = nreverse (args);
3938   TREE_CHAIN (last) = void_list_node;
3939   args = build_function_type (return_type, args);
3940
3941   va_end (p);
3942   return args;
3943 }
3944
3945 /* Build a METHOD_TYPE for a member of BASETYPE.  The RETTYPE (a TYPE)
3946    and ARGTYPES (a TREE_LIST) are the return type and arguments types
3947    for the method.  An implicit additional parameter (of type
3948    pointer-to-BASETYPE) is added to the ARGTYPES.  */
3949
3950 tree
3951 build_method_type_directly (tree basetype,
3952                             tree rettype,
3953                             tree argtypes)
3954 {
3955   tree t;
3956   tree ptype;
3957   int hashcode;
3958
3959   /* Make a node of the sort we want.  */
3960   t = make_node (METHOD_TYPE);
3961
3962   TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3963   TREE_TYPE (t) = rettype;
3964   ptype = build_pointer_type (basetype);
3965
3966   /* The actual arglist for this function includes a "hidden" argument
3967      which is "this".  Put it into the list of argument types.  */
3968   argtypes = tree_cons (NULL_TREE, ptype, argtypes);
3969   TYPE_ARG_TYPES (t) = argtypes;
3970
3971   /* If we already have such a type, use the old one and free this one.
3972      Note that it also frees up the above cons cell if found.  */
3973   hashcode = TYPE_HASH (basetype) + TYPE_HASH (rettype) +
3974     type_hash_list (argtypes);
3975
3976   t = type_hash_canon (hashcode, t);
3977
3978   if (!COMPLETE_TYPE_P (t))
3979     layout_type (t);
3980
3981   return t;
3982 }
3983
3984 /* Construct, lay out and return the type of methods belonging to class
3985    BASETYPE and whose arguments and values are described by TYPE.
3986    If that type exists already, reuse it.
3987    TYPE must be a FUNCTION_TYPE node.  */
3988
3989 tree
3990 build_method_type (tree basetype, tree type)
3991 {
3992   if (TREE_CODE (type) != FUNCTION_TYPE)
3993     abort ();
3994
3995   return build_method_type_directly (basetype, 
3996                                      TREE_TYPE (type),
3997                                      TYPE_ARG_TYPES (type));
3998 }
3999
4000 /* Construct, lay out and return the type of offsets to a value
4001    of type TYPE, within an object of type BASETYPE.
4002    If a suitable offset type exists already, reuse it.  */
4003
4004 tree
4005 build_offset_type (tree basetype, tree type)
4006 {
4007   tree t;
4008   unsigned int hashcode;
4009
4010   /* Make a node of the sort we want.  */
4011   t = make_node (OFFSET_TYPE);
4012
4013   TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
4014   TREE_TYPE (t) = type;
4015
4016   /* If we already have such a type, use the old one and free this one.  */
4017   hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
4018   t = type_hash_canon (hashcode, t);
4019
4020   if (!COMPLETE_TYPE_P (t))
4021     layout_type (t);
4022
4023   return t;
4024 }
4025
4026 /* Create a complex type whose components are COMPONENT_TYPE.  */
4027
4028 tree
4029 build_complex_type (tree component_type)
4030 {
4031   tree t;
4032   unsigned int hashcode;
4033
4034   /* Make a node of the sort we want.  */
4035   t = make_node (COMPLEX_TYPE);
4036
4037   TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
4038   set_type_quals (t, TYPE_QUALS (component_type));
4039
4040   /* If we already have such a type, use the old one and free this one.  */
4041   hashcode = TYPE_HASH (component_type);
4042   t = type_hash_canon (hashcode, t);
4043
4044   if (!COMPLETE_TYPE_P (t))
4045     layout_type (t);
4046
4047   /* If we are writing Dwarf2 output we need to create a name,
4048      since complex is a fundamental type.  */
4049   if ((write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
4050       && ! TYPE_NAME (t))
4051     {
4052       const char *name;
4053       if (component_type == char_type_node)
4054         name = "complex char";
4055       else if (component_type == signed_char_type_node)
4056         name = "complex signed char";
4057       else if (component_type == unsigned_char_type_node)
4058         name = "complex unsigned char";
4059       else if (component_type == short_integer_type_node)
4060         name = "complex short int";
4061       else if (component_type == short_unsigned_type_node)
4062         name = "complex short unsigned int";
4063       else if (component_type == integer_type_node)
4064         name = "complex int";
4065       else if (component_type == unsigned_type_node)
4066         name = "complex unsigned int";
4067       else if (component_type == long_integer_type_node)
4068         name = "complex long int";
4069       else if (component_type == long_unsigned_type_node)
4070         name = "complex long unsigned int";
4071       else if (component_type == long_long_integer_type_node)
4072         name = "complex long long int";
4073       else if (component_type == long_long_unsigned_type_node)
4074         name = "complex long long unsigned int";
4075       else
4076         name = 0;
4077
4078       if (name != 0)
4079         TYPE_NAME (t) = get_identifier (name);
4080     }
4081
4082   return t;
4083 }
4084 \f
4085 /* Return OP, stripped of any conversions to wider types as much as is safe.
4086    Converting the value back to OP's type makes a value equivalent to OP.
4087
4088    If FOR_TYPE is nonzero, we return a value which, if converted to
4089    type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
4090
4091    If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
4092    narrowest type that can hold the value, even if they don't exactly fit.
4093    Otherwise, bit-field references are changed to a narrower type
4094    only if they can be fetched directly from memory in that type.
4095
4096    OP must have integer, real or enumeral type.  Pointers are not allowed!
4097
4098    There are some cases where the obvious value we could return
4099    would regenerate to OP if converted to OP's type,
4100    but would not extend like OP to wider types.
4101    If FOR_TYPE indicates such extension is contemplated, we eschew such values.
4102    For example, if OP is (unsigned short)(signed char)-1,
4103    we avoid returning (signed char)-1 if FOR_TYPE is int,
4104    even though extending that to an unsigned short would regenerate OP,
4105    since the result of extending (signed char)-1 to (int)
4106    is different from (int) OP.  */
4107
4108 tree
4109 get_unwidened (tree op, tree for_type)
4110 {
4111   /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension.  */
4112   tree type = TREE_TYPE (op);
4113   unsigned final_prec
4114     = TYPE_PRECISION (for_type != 0 ? for_type : type);
4115   int uns
4116     = (for_type != 0 && for_type != type
4117        && final_prec > TYPE_PRECISION (type)
4118        && TREE_UNSIGNED (type));
4119   tree win = op;
4120
4121   while (TREE_CODE (op) == NOP_EXPR)
4122     {
4123       int bitschange
4124         = TYPE_PRECISION (TREE_TYPE (op))
4125           - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
4126
4127       /* Truncations are many-one so cannot be removed.
4128          Unless we are later going to truncate down even farther.  */
4129       if (bitschange < 0
4130           && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
4131         break;
4132
4133       /* See what's inside this conversion.  If we decide to strip it,
4134          we will set WIN.  */
4135       op = TREE_OPERAND (op, 0);
4136
4137       /* If we have not stripped any zero-extensions (uns is 0),
4138          we can strip any kind of extension.
4139          If we have previously stripped a zero-extension,
4140          only zero-extensions can safely be stripped.
4141          Any extension can be stripped if the bits it would produce
4142          are all going to be discarded later by truncating to FOR_TYPE.  */
4143
4144       if (bitschange > 0)
4145         {
4146           if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
4147             win = op;
4148           /* TREE_UNSIGNED says whether this is a zero-extension.
4149              Let's avoid computing it if it does not affect WIN
4150              and if UNS will not be needed again.  */
4151           if ((uns || TREE_CODE (op) == NOP_EXPR)
4152               && TREE_UNSIGNED (TREE_TYPE (op)))
4153             {
4154               uns = 1;
4155               win = op;
4156             }
4157         }
4158     }
4159
4160   if (TREE_CODE (op) == COMPONENT_REF
4161       /* Since type_for_size always gives an integer type.  */
4162       && TREE_CODE (type) != REAL_TYPE
4163       /* Don't crash if field not laid out yet.  */
4164       && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
4165       && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
4166     {
4167       unsigned int innerprec
4168         = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4169       int unsignedp = (TREE_UNSIGNED (TREE_OPERAND (op, 1))
4170                        || TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
4171       type = (*lang_hooks.types.type_for_size) (innerprec, unsignedp);
4172
4173       /* We can get this structure field in the narrowest type it fits in.
4174          If FOR_TYPE is 0, do this only for a field that matches the
4175          narrower type exactly and is aligned for it
4176          The resulting extension to its nominal type (a fullword type)
4177          must fit the same conditions as for other extensions.  */
4178
4179       if (type != 0
4180           && INT_CST_LT_UNSIGNED (TYPE_SIZE (type), TYPE_SIZE (TREE_TYPE (op)))
4181           && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
4182           && (! uns || final_prec <= innerprec || unsignedp))
4183         {
4184           win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4185                        TREE_OPERAND (op, 1));
4186           TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4187           TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4188         }
4189     }
4190
4191   return win;
4192 }
4193 \f
4194 /* Return OP or a simpler expression for a narrower value
4195    which can be sign-extended or zero-extended to give back OP.
4196    Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
4197    or 0 if the value should be sign-extended.  */
4198
4199 tree
4200 get_narrower (tree op, int *unsignedp_ptr)
4201 {
4202   int uns = 0;
4203   int first = 1;
4204   tree win = op;
4205
4206   while (TREE_CODE (op) == NOP_EXPR)
4207     {
4208       int bitschange
4209         = (TYPE_PRECISION (TREE_TYPE (op))
4210            - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
4211
4212       /* Truncations are many-one so cannot be removed.  */
4213       if (bitschange < 0)
4214         break;
4215
4216       /* See what's inside this conversion.  If we decide to strip it,
4217          we will set WIN.  */
4218
4219       if (bitschange > 0)
4220         {
4221           op = TREE_OPERAND (op, 0);
4222           /* An extension: the outermost one can be stripped,
4223              but remember whether it is zero or sign extension.  */
4224           if (first)
4225             uns = TREE_UNSIGNED (TREE_TYPE (op));
4226           /* Otherwise, if a sign extension has been stripped,
4227              only sign extensions can now be stripped;
4228              if a zero extension has been stripped, only zero-extensions.  */
4229           else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
4230             break;
4231           first = 0;
4232         }
4233       else /* bitschange == 0 */
4234         {
4235           /* A change in nominal type can always be stripped, but we must
4236              preserve the unsignedness.  */
4237           if (first)
4238             uns = TREE_UNSIGNED (TREE_TYPE (op));
4239           first = 0;
4240           op = TREE_OPERAND (op, 0);
4241         }
4242
4243       win = op;
4244     }
4245
4246   if (TREE_CODE (op) == COMPONENT_REF
4247       /* Since type_for_size always gives an integer type.  */
4248       && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
4249       /* Ensure field is laid out already.  */
4250       && DECL_SIZE (TREE_OPERAND (op, 1)) != 0)
4251     {
4252       unsigned HOST_WIDE_INT innerprec
4253         = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4254       int unsignedp = (TREE_UNSIGNED (TREE_OPERAND (op, 1))
4255                        || TREE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 1))));
4256       tree type = (*lang_hooks.types.type_for_size) (innerprec, unsignedp);
4257
4258       /* We can get this structure field in a narrower type that fits it,
4259          but the resulting extension to its nominal type (a fullword type)
4260          must satisfy the same conditions as for other extensions.
4261
4262          Do this only for fields that are aligned (not bit-fields),
4263          because when bit-field insns will be used there is no
4264          advantage in doing this.  */
4265
4266       if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
4267           && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
4268           && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
4269           && type != 0)
4270         {
4271           if (first)
4272             uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
4273           win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4274                        TREE_OPERAND (op, 1));
4275           TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4276           TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4277         }
4278     }
4279   *unsignedp_ptr = uns;
4280   return win;
4281 }
4282 \f
4283 /* Nonzero if integer constant C has a value that is permissible
4284    for type TYPE (an INTEGER_TYPE).  */
4285
4286 int
4287 int_fits_type_p (tree c, tree type)
4288 {
4289   tree type_low_bound = TYPE_MIN_VALUE (type);
4290   tree type_high_bound = TYPE_MAX_VALUE (type);
4291   int ok_for_low_bound, ok_for_high_bound;
4292
4293   /* Perform some generic filtering first, which may allow making a decision
4294      even if the bounds are not constant.  First, negative integers never fit
4295      in unsigned types, */
4296   if ((TREE_UNSIGNED (type) && tree_int_cst_sgn (c) < 0)
4297       /* Also, unsigned integers with top bit set never fit signed types.  */
4298       || (! TREE_UNSIGNED (type)
4299           && TREE_UNSIGNED (TREE_TYPE (c)) && tree_int_cst_msb (c)))
4300     return 0;
4301
4302   /* If at least one bound of the type is a constant integer, we can check
4303      ourselves and maybe make a decision. If no such decision is possible, but
4304      this type is a subtype, try checking against that.  Otherwise, use
4305      force_fit_type, which checks against the precision.
4306
4307      Compute the status for each possibly constant bound, and return if we see
4308      one does not match. Use ok_for_xxx_bound for this purpose, assigning -1
4309      for "unknown if constant fits", 0 for "constant known *not* to fit" and 1
4310      for "constant known to fit".  */
4311
4312   ok_for_low_bound = -1;
4313   ok_for_high_bound = -1;
4314
4315   /* Check if C >= type_low_bound.  */
4316   if (type_low_bound && TREE_CODE (type_low_bound) == INTEGER_CST)
4317     {
4318       ok_for_low_bound = ! tree_int_cst_lt (c, type_low_bound);
4319       if (! ok_for_low_bound)
4320         return 0;
4321     }
4322
4323   /* Check if c <= type_high_bound.  */
4324   if (type_high_bound && TREE_CODE (type_high_bound) == INTEGER_CST)
4325     {
4326       ok_for_high_bound = ! tree_int_cst_lt (type_high_bound, c);
4327       if (! ok_for_high_bound)
4328         return 0;
4329     }
4330
4331   /* If the constant fits both bounds, the result is known.  */
4332   if (ok_for_low_bound == 1 && ok_for_high_bound == 1)
4333     return 1;
4334
4335   /* If we haven't been able to decide at this point, there nothing more we
4336      can check ourselves here. Look at the base type if we have one.  */
4337   else if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != 0)
4338     return int_fits_type_p (c, TREE_TYPE (type));
4339
4340   /* Or to force_fit_type, if nothing else.  */
4341   else
4342     {
4343       c = copy_node (c);
4344       TREE_TYPE (c) = type;
4345       return !force_fit_type (c, 0);
4346     }
4347 }
4348
4349 /* Returns true if T is, contains, or refers to a type with variable
4350    size.  This concept is more general than that of C99 'variably
4351    modified types': in C99, a struct type is never variably modified
4352    because a VLA may not appear as a structure member.  However, in
4353    GNU C code like:
4354
4355      struct S { int i[f()]; };
4356
4357    is valid, and other languages may define similar constructs.  */
4358
4359 bool
4360 variably_modified_type_p (tree type)
4361 {
4362   tree t;
4363
4364   if (type == error_mark_node)
4365     return false;
4366
4367   /* If TYPE itself has variable size, it is variably modified.
4368
4369      We do not yet have a representation of the C99 '[*]' syntax.
4370      When a representation is chosen, this function should be modified
4371      to test for that case as well.  */
4372   t = TYPE_SIZE (type);
4373   if (t && t != error_mark_node && TREE_CODE (t) != INTEGER_CST)
4374     return true;
4375
4376   switch (TREE_CODE (type))
4377     {
4378     case POINTER_TYPE:
4379     case REFERENCE_TYPE:
4380     case ARRAY_TYPE:
4381       /* If TYPE is a pointer or reference, it is variably modified if
4382          the type pointed to is variably modified.  Similarly for arrays;
4383          note that VLAs are handled by the TYPE_SIZE check above.  */
4384       return variably_modified_type_p (TREE_TYPE (type));
4385
4386     case FUNCTION_TYPE:
4387     case METHOD_TYPE:
4388       /* If TYPE is a function type, it is variably modified if any of the
4389          parameters or the return type are variably modified.  */
4390       {
4391         tree parm;
4392
4393         if (variably_modified_type_p (TREE_TYPE (type)))
4394           return true;
4395         for (parm = TYPE_ARG_TYPES (type);
4396              parm && parm != void_list_node;
4397              parm = TREE_CHAIN (parm))
4398           if (variably_modified_type_p (TREE_VALUE (parm)))
4399             return true;
4400       }
4401       break;
4402
4403     case INTEGER_TYPE:
4404       /* Scalar types are variably modified if their end points
4405          aren't constant.  */
4406       t = TYPE_MIN_VALUE (type);
4407       if (t && t != error_mark_node && TREE_CODE (t) != INTEGER_CST)
4408         return true;
4409       t = TYPE_MAX_VALUE (type);
4410       if (t && t != error_mark_node && TREE_CODE (t) != INTEGER_CST)
4411         return true;
4412       return false;
4413
4414     default:
4415       break;
4416     }
4417
4418   /* The current language may have other cases to check, but in general,
4419      all other types are not variably modified.  */
4420   return (*lang_hooks.tree_inlining.var_mod_type_p) (type);
4421 }
4422
4423 /* Given a DECL or TYPE, return the scope in which it was declared, or
4424    NULL_TREE if there is no containing scope.  */
4425
4426 tree
4427 get_containing_scope (tree t)
4428 {
4429   return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
4430 }
4431
4432 /* Return the innermost context enclosing DECL that is
4433    a FUNCTION_DECL, or zero if none.  */
4434
4435 tree
4436 decl_function_context (tree decl)
4437 {
4438   tree context;
4439
4440   if (TREE_CODE (decl) == ERROR_MARK)
4441     return 0;
4442
4443   if (TREE_CODE (decl) == SAVE_EXPR)
4444     context = SAVE_EXPR_CONTEXT (decl);
4445
4446   /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
4447      where we look up the function at runtime.  Such functions always take
4448      a first argument of type 'pointer to real context'.
4449
4450      C++ should really be fixed to use DECL_CONTEXT for the real context,
4451      and use something else for the "virtual context".  */
4452   else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
4453     context
4454       = TYPE_MAIN_VARIANT
4455         (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4456   else
4457     context = DECL_CONTEXT (decl);
4458
4459   while (context && TREE_CODE (context) != FUNCTION_DECL)
4460     {
4461       if (TREE_CODE (context) == BLOCK)
4462         context = BLOCK_SUPERCONTEXT (context);
4463       else
4464         context = get_containing_scope (context);
4465     }
4466
4467   return context;
4468 }
4469
4470 /* Return the innermost context enclosing DECL that is
4471    a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
4472    TYPE_DECLs and FUNCTION_DECLs are transparent to this function.  */
4473
4474 tree
4475 decl_type_context (tree decl)
4476 {
4477   tree context = DECL_CONTEXT (decl);
4478
4479   while (context)
4480     switch (TREE_CODE (context))
4481       {
4482       case NAMESPACE_DECL:
4483       case TRANSLATION_UNIT_DECL:
4484         return NULL_TREE;
4485
4486       case RECORD_TYPE:
4487       case UNION_TYPE:
4488       case QUAL_UNION_TYPE:
4489         return context;
4490         
4491       case TYPE_DECL:
4492       case FUNCTION_DECL:
4493         context = DECL_CONTEXT (context);
4494         break;
4495         
4496       case BLOCK:
4497         context = BLOCK_SUPERCONTEXT (context);
4498         break;
4499         
4500       default:
4501         abort ();
4502       }
4503
4504   return NULL_TREE;
4505 }
4506
4507 /* CALL is a CALL_EXPR.  Return the declaration for the function
4508    called, or NULL_TREE if the called function cannot be
4509    determined.  */
4510
4511 tree
4512 get_callee_fndecl (tree call)
4513 {
4514   tree addr;
4515
4516   /* It's invalid to call this function with anything but a
4517      CALL_EXPR.  */
4518   if (TREE_CODE (call) != CALL_EXPR)
4519     abort ();
4520
4521   /* The first operand to the CALL is the address of the function
4522      called.  */
4523   addr = TREE_OPERAND (call, 0);
4524
4525   STRIP_NOPS (addr);
4526
4527   /* If this is a readonly function pointer, extract its initial value.  */
4528   if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
4529       && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
4530       && DECL_INITIAL (addr))
4531     addr = DECL_INITIAL (addr);
4532
4533   /* If the address is just `&f' for some function `f', then we know
4534      that `f' is being called.  */
4535   if (TREE_CODE (addr) == ADDR_EXPR
4536       && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
4537     return TREE_OPERAND (addr, 0);
4538   
4539   /* We couldn't figure out what was being called.  Maybe the front
4540      end has some idea.  */
4541   return (*lang_hooks.lang_get_callee_fndecl) (call);
4542 }
4543
4544 /* Print debugging information about tree nodes generated during the compile,
4545    and any language-specific information.  */
4546
4547 void
4548 dump_tree_statistics (void)
4549 {
4550 #ifdef GATHER_STATISTICS
4551   int i;
4552   int total_nodes, total_bytes;
4553 #endif
4554
4555   fprintf (stderr, "\n??? tree nodes created\n\n");
4556 #ifdef GATHER_STATISTICS
4557   fprintf (stderr, "Kind                   Nodes      Bytes\n");
4558   fprintf (stderr, "---------------------------------------\n");
4559   total_nodes = total_bytes = 0;
4560   for (i = 0; i < (int) all_kinds; i++)
4561     {
4562       fprintf (stderr, "%-20s %7d %10d\n", tree_node_kind_names[i],
4563                tree_node_counts[i], tree_node_sizes[i]);
4564       total_nodes += tree_node_counts[i];
4565       total_bytes += tree_node_sizes[i];
4566     }
4567   fprintf (stderr, "---------------------------------------\n");
4568   fprintf (stderr, "%-20s %7d %10d\n", "Total", total_nodes, total_bytes);
4569   fprintf (stderr, "---------------------------------------\n");
4570 #else
4571   fprintf (stderr, "(No per-node statistics)\n");
4572 #endif
4573   print_type_hash_statistics ();
4574   (*lang_hooks.print_statistics) ();
4575 }
4576 \f
4577 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
4578
4579 /* Generate a crc32 of a string.  */
4580
4581 unsigned
4582 crc32_string (unsigned chksum, const char *string)
4583 {
4584   do
4585     {
4586       unsigned value = *string << 24;
4587       unsigned ix;
4588       
4589       for (ix = 8; ix--; value <<= 1)
4590         {
4591           unsigned feedback;
4592           
4593           feedback = (value ^ chksum) & 0x80000000 ? 0x04c11db7 : 0;
4594           chksum <<= 1;
4595           chksum ^= feedback;
4596         }
4597     }
4598   while (*string++);
4599   return chksum;
4600 }
4601
4602 /* P is a string that will be used in a symbol.  Mask out any characters
4603    that are not valid in that context.  */
4604
4605 void
4606 clean_symbol_name (char *p)
4607 {
4608   for (; *p; p++)
4609     if (! (ISALNUM (*p)
4610 #ifndef NO_DOLLAR_IN_LABEL      /* this for `$'; unlikely, but... -- kr */
4611             || *p == '$'
4612 #endif
4613 #ifndef NO_DOT_IN_LABEL         /* this for `.'; unlikely, but...  */
4614             || *p == '.'
4615 #endif
4616            ))
4617       *p = '_';
4618 }
4619
4620 /* Generate a name for a function unique to this translation unit.
4621    TYPE is some string to identify the purpose of this function to the
4622    linker or collect2.  */
4623
4624 tree
4625 get_file_function_name_long (const char *type)
4626 {
4627   char *buf;
4628   const char *p;
4629   char *q;
4630
4631   if (first_global_object_name)
4632     p = first_global_object_name;
4633   else
4634     {
4635       /* We don't have anything that we know to be unique to this translation
4636          unit, so use what we do have and throw in some randomness.  */
4637       unsigned len;
4638       const char *name = weak_global_object_name;
4639       const char *file = main_input_filename;
4640
4641       if (! name)
4642         name = "";
4643       if (! file)
4644         file = input_filename;
4645
4646       len = strlen (file);
4647       q = alloca (9 * 2 + len + 1);
4648       memcpy (q, file, len + 1);
4649       clean_symbol_name (q);
4650
4651       sprintf (q + len, "_%08X_%08X", crc32_string (0, name),
4652                crc32_string (0, flag_random_seed));
4653
4654       p = q;
4655     }
4656
4657   buf = alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p) + strlen (type));
4658
4659   /* Set up the name of the file-level functions we may need.
4660      Use a global object (which is already required to be unique over
4661      the program) rather than the file name (which imposes extra
4662      constraints).  */
4663   sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
4664
4665   return get_identifier (buf);
4666 }
4667
4668 /* If KIND=='I', return a suitable global initializer (constructor) name.
4669    If KIND=='D', return a suitable global clean-up (destructor) name.  */
4670
4671 tree
4672 get_file_function_name (int kind)
4673 {
4674   char p[2];
4675
4676   p[0] = kind;
4677   p[1] = 0;
4678
4679   return get_file_function_name_long (p);
4680 }
4681 \f
4682 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4683    The result is placed in BUFFER (which has length BIT_SIZE),
4684    with one bit in each char ('\000' or '\001').
4685
4686    If the constructor is constant, NULL_TREE is returned.
4687    Otherwise, a TREE_LIST of the non-constant elements is emitted.  */
4688
4689 tree
4690 get_set_constructor_bits (tree init, char *buffer, int bit_size)
4691 {
4692   int i;
4693   tree vals;
4694   HOST_WIDE_INT domain_min
4695     = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (init))), 0);
4696   tree non_const_bits = NULL_TREE;
4697
4698   for (i = 0; i < bit_size; i++)
4699     buffer[i] = 0;
4700
4701   for (vals = TREE_OPERAND (init, 1);
4702        vals != NULL_TREE; vals = TREE_CHAIN (vals))
4703     {
4704       if (!host_integerp (TREE_VALUE (vals), 0)
4705           || (TREE_PURPOSE (vals) != NULL_TREE
4706               && !host_integerp (TREE_PURPOSE (vals), 0)))
4707         non_const_bits
4708           = tree_cons (TREE_PURPOSE (vals), TREE_VALUE (vals), non_const_bits);
4709       else if (TREE_PURPOSE (vals) != NULL_TREE)
4710         {
4711           /* Set a range of bits to ones.  */
4712           HOST_WIDE_INT lo_index
4713             = tree_low_cst (TREE_PURPOSE (vals), 0) - domain_min;
4714           HOST_WIDE_INT hi_index
4715             = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
4716
4717           if (lo_index < 0 || lo_index >= bit_size
4718               || hi_index < 0 || hi_index >= bit_size)
4719             abort ();
4720           for (; lo_index <= hi_index; lo_index++)
4721             buffer[lo_index] = 1;
4722         }
4723       else
4724         {
4725           /* Set a single bit to one.  */
4726           HOST_WIDE_INT index
4727             = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
4728           if (index < 0 || index >= bit_size)
4729             {
4730               error ("invalid initializer for bit string");
4731               return NULL_TREE;
4732             }
4733           buffer[index] = 1;
4734         }
4735     }
4736   return non_const_bits;
4737 }
4738
4739 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4740    The result is placed in BUFFER (which is an array of bytes).
4741    If the constructor is constant, NULL_TREE is returned.
4742    Otherwise, a TREE_LIST of the non-constant elements is emitted.  */
4743
4744 tree
4745 get_set_constructor_bytes (tree init, unsigned char *buffer, int wd_size)
4746 {
4747   int i;
4748   int set_word_size = BITS_PER_UNIT;
4749   int bit_size = wd_size * set_word_size;
4750   int bit_pos = 0;
4751   unsigned char *bytep = buffer;
4752   char *bit_buffer = alloca (bit_size);
4753   tree non_const_bits = get_set_constructor_bits (init, bit_buffer, bit_size);
4754
4755   for (i = 0; i < wd_size; i++)
4756     buffer[i] = 0;
4757
4758   for (i = 0; i < bit_size; i++)
4759     {
4760       if (bit_buffer[i])
4761         {
4762           if (BYTES_BIG_ENDIAN)
4763             *bytep |= (1 << (set_word_size - 1 - bit_pos));
4764           else
4765             *bytep |= 1 << bit_pos;
4766         }
4767       bit_pos++;
4768       if (bit_pos >= set_word_size)
4769         bit_pos = 0, bytep++;
4770     }
4771   return non_const_bits;
4772 }
4773 \f
4774 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4775 /* Complain that the tree code of NODE does not match the expected CODE.
4776    FILE, LINE, and FUNCTION are of the caller.  */
4777
4778 void
4779 tree_check_failed (const tree node, enum tree_code code, const char *file,
4780                    int line, const char *function)
4781 {
4782   internal_error ("tree check: expected %s, have %s in %s, at %s:%d",
4783                   tree_code_name[code], tree_code_name[TREE_CODE (node)],
4784                   function, trim_filename (file), line);
4785 }
4786
4787 /* Similar to above, except that we check for a class of tree
4788    code, given in CL.  */
4789
4790 void
4791 tree_class_check_failed (const tree node, int cl, const char *file,
4792                          int line, const char *function)
4793 {
4794   internal_error
4795     ("tree check: expected class '%c', have '%c' (%s) in %s, at %s:%d",
4796      cl, TREE_CODE_CLASS (TREE_CODE (node)),
4797      tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
4798 }
4799
4800 /* Similar to above, except that the check is for the bounds of a TREE_VEC's
4801    (dynamically sized) vector.  */
4802
4803 void
4804 tree_vec_elt_check_failed (int idx, int len, const char *file, int line,
4805                            const char *function)
4806 {
4807   internal_error
4808     ("tree check: accessed elt %d of tree_vec with %d elts in %s, at %s:%d",
4809      idx + 1, len, function, trim_filename (file), line);
4810 }
4811
4812 /* Similar to above, except that the check is for the bounds of the operand
4813    vector of an expression node.  */
4814
4815 void
4816 tree_operand_check_failed (int idx, enum tree_code code, const char *file,
4817                            int line, const char *function)
4818 {
4819   internal_error
4820     ("tree check: accessed operand %d of %s with %d operands in %s, at %s:%d",
4821      idx + 1, tree_code_name[code], TREE_CODE_LENGTH (code),
4822      function, trim_filename (file), line);
4823 }
4824 #endif /* ENABLE_TREE_CHECKING */
4825 \f
4826 /* For a new vector type node T, build the information necessary for
4827    debugging output.  */
4828
4829 static void
4830 finish_vector_type (tree t)
4831 {
4832   layout_type (t);
4833
4834   {
4835     tree index = build_int_2 (TYPE_VECTOR_SUBPARTS (t) - 1, 0);
4836     tree array = build_array_type (TREE_TYPE (t),
4837                                    build_index_type (index));
4838     tree rt = make_node (RECORD_TYPE);
4839
4840     TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
4841     DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
4842     layout_type (rt);
4843     TYPE_DEBUG_REPRESENTATION_TYPE (t) = rt;
4844     /* In dwarfout.c, type lookup uses TYPE_UID numbers.  We want to output
4845        the representation type, and we want to find that die when looking up
4846        the vector type.  This is most easily achieved by making the TYPE_UID
4847        numbers equal.  */
4848     TYPE_UID (rt) = TYPE_UID (t);
4849   }
4850 }
4851
4852 /* Create nodes for all integer types (and error_mark_node) using the sizes
4853    of C datatypes.  The caller should call set_sizetype soon after calling
4854    this function to select one of the types as sizetype.  */
4855
4856 void
4857 build_common_tree_nodes (int signed_char)
4858 {
4859   error_mark_node = make_node (ERROR_MARK);
4860   TREE_TYPE (error_mark_node) = error_mark_node;
4861
4862   initialize_sizetypes ();
4863
4864   /* Define both `signed char' and `unsigned char'.  */
4865   signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
4866   unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
4867
4868   /* Define `char', which is like either `signed char' or `unsigned char'
4869      but not the same as either.  */
4870   char_type_node
4871     = (signed_char
4872        ? make_signed_type (CHAR_TYPE_SIZE)
4873        : make_unsigned_type (CHAR_TYPE_SIZE));
4874
4875   short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
4876   short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
4877   integer_type_node = make_signed_type (INT_TYPE_SIZE);
4878   unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
4879   long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
4880   long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
4881   long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
4882   long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
4883
4884   /* Define a boolean type.  This type only represents boolean values but
4885      may be larger than char depending on the value of BOOL_TYPE_SIZE.
4886      Front ends which want to override this size (i.e. Java) can redefine
4887      boolean_type_node before calling build_common_tree_nodes_2.  */
4888   boolean_type_node = make_unsigned_type (BOOL_TYPE_SIZE);
4889   TREE_SET_CODE (boolean_type_node, BOOLEAN_TYPE);
4890   TYPE_MAX_VALUE (boolean_type_node) = build_int_2 (1, 0);
4891   TREE_TYPE (TYPE_MAX_VALUE (boolean_type_node)) = boolean_type_node;
4892   TYPE_PRECISION (boolean_type_node) = 1;
4893
4894   intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
4895   intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
4896   intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
4897   intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
4898   intTI_type_node = make_signed_type (GET_MODE_BITSIZE (TImode));
4899
4900   unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
4901   unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
4902   unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
4903   unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
4904   unsigned_intTI_type_node = make_unsigned_type (GET_MODE_BITSIZE (TImode));
4905   
4906   access_public_node = get_identifier ("public");
4907   access_protected_node = get_identifier ("protected");
4908   access_private_node = get_identifier ("private");
4909 }
4910
4911 /* Call this function after calling build_common_tree_nodes and set_sizetype.
4912    It will create several other common tree nodes.  */
4913
4914 void
4915 build_common_tree_nodes_2 (int short_double)
4916 {
4917   /* Define these next since types below may used them.  */
4918   integer_zero_node = build_int_2 (0, 0);
4919   integer_one_node = build_int_2 (1, 0);
4920   integer_minus_one_node = build_int_2 (-1, -1);
4921
4922   size_zero_node = size_int (0);
4923   size_one_node = size_int (1);
4924   bitsize_zero_node = bitsize_int (0);
4925   bitsize_one_node = bitsize_int (1);
4926   bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
4927
4928   boolean_false_node = TYPE_MIN_VALUE (boolean_type_node);
4929   boolean_true_node = TYPE_MAX_VALUE (boolean_type_node);
4930
4931   void_type_node = make_node (VOID_TYPE);
4932   layout_type (void_type_node);
4933
4934   /* We are not going to have real types in C with less than byte alignment,
4935      so we might as well not have any types that claim to have it.  */
4936   TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
4937   TYPE_USER_ALIGN (void_type_node) = 0;
4938
4939   null_pointer_node = build_int_2 (0, 0);
4940   TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
4941   layout_type (TREE_TYPE (null_pointer_node));
4942
4943   ptr_type_node = build_pointer_type (void_type_node);
4944   const_ptr_type_node
4945     = build_pointer_type (build_type_variant (void_type_node, 1, 0));
4946
4947   float_type_node = make_node (REAL_TYPE);
4948   TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
4949   layout_type (float_type_node);
4950
4951   double_type_node = make_node (REAL_TYPE);
4952   if (short_double)
4953     TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
4954   else
4955     TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
4956   layout_type (double_type_node);
4957
4958   long_double_type_node = make_node (REAL_TYPE);
4959   TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
4960   layout_type (long_double_type_node);
4961
4962   float_ptr_type_node = build_pointer_type (float_type_node);
4963   double_ptr_type_node = build_pointer_type (double_type_node);
4964   long_double_ptr_type_node = build_pointer_type (long_double_type_node);
4965   integer_ptr_type_node = build_pointer_type (integer_type_node);
4966
4967   complex_integer_type_node = make_node (COMPLEX_TYPE);
4968   TREE_TYPE (complex_integer_type_node) = integer_type_node;
4969   layout_type (complex_integer_type_node);
4970
4971   complex_float_type_node = make_node (COMPLEX_TYPE);
4972   TREE_TYPE (complex_float_type_node) = float_type_node;
4973   layout_type (complex_float_type_node);
4974
4975   complex_double_type_node = make_node (COMPLEX_TYPE);
4976   TREE_TYPE (complex_double_type_node) = double_type_node;
4977   layout_type (complex_double_type_node);
4978
4979   complex_long_double_type_node = make_node (COMPLEX_TYPE);
4980   TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
4981   layout_type (complex_long_double_type_node);
4982
4983   {
4984     tree t = (*targetm.build_builtin_va_list) ();
4985
4986     /* Many back-ends define record types without setting TYPE_NAME.
4987        If we copied the record type here, we'd keep the original
4988        record type without a name.  This breaks name mangling.  So,
4989        don't copy record types and let c_common_nodes_and_builtins()
4990        declare the type to be __builtin_va_list.  */
4991     if (TREE_CODE (t) != RECORD_TYPE)
4992       t = build_type_copy (t);
4993
4994     va_list_type_node = t;
4995   }
4996
4997   unsigned_V4SI_type_node
4998     = make_vector (V4SImode, unsigned_intSI_type_node, 1);
4999   unsigned_V2HI_type_node
5000     = make_vector (V2HImode, unsigned_intHI_type_node, 1);
5001   unsigned_V2SI_type_node
5002     = make_vector (V2SImode, unsigned_intSI_type_node, 1);
5003   unsigned_V2DI_type_node
5004     = make_vector (V2DImode, unsigned_intDI_type_node, 1);
5005   unsigned_V4HI_type_node
5006     = make_vector (V4HImode, unsigned_intHI_type_node, 1);
5007   unsigned_V8QI_type_node
5008     = make_vector (V8QImode, unsigned_intQI_type_node, 1);
5009   unsigned_V8HI_type_node
5010     = make_vector (V8HImode, unsigned_intHI_type_node, 1);
5011   unsigned_V16QI_type_node
5012     = make_vector (V16QImode, unsigned_intQI_type_node, 1);
5013   unsigned_V1DI_type_node
5014     = make_vector (V1DImode, unsigned_intDI_type_node, 1);
5015
5016   V16SF_type_node = make_vector (V16SFmode, float_type_node, 0);
5017   V4SF_type_node = make_vector (V4SFmode, float_type_node, 0);
5018   V4SI_type_node = make_vector (V4SImode, intSI_type_node, 0);
5019   V2HI_type_node = make_vector (V2HImode, intHI_type_node, 0);
5020   V2SI_type_node = make_vector (V2SImode, intSI_type_node, 0);
5021   V2DI_type_node = make_vector (V2DImode, intDI_type_node, 0);
5022   V4HI_type_node = make_vector (V4HImode, intHI_type_node, 0);
5023   V8QI_type_node = make_vector (V8QImode, intQI_type_node, 0);
5024   V8HI_type_node = make_vector (V8HImode, intHI_type_node, 0);
5025   V2SF_type_node = make_vector (V2SFmode, float_type_node, 0);
5026   V2DF_type_node = make_vector (V2DFmode, double_type_node, 0);
5027   V16QI_type_node = make_vector (V16QImode, intQI_type_node, 0);
5028   V1DI_type_node = make_vector (V1DImode, intDI_type_node, 0);
5029   V4DF_type_node = make_vector (V4DFmode, double_type_node, 0);
5030 }
5031
5032 /* HACK.  GROSS.  This is absolutely disgusting.  I wish there was a
5033    better way.
5034
5035    If we requested a pointer to a vector, build up the pointers that
5036    we stripped off while looking for the inner type.  Similarly for
5037    return values from functions.
5038
5039    The argument TYPE is the top of the chain, and BOTTOM is the
5040    new type which we will point to.  */
5041
5042 tree
5043 reconstruct_complex_type (tree type, tree bottom)
5044 {
5045   tree inner, outer;
5046
5047   if (POINTER_TYPE_P (type))
5048     {
5049       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
5050       outer = build_pointer_type (inner);
5051     }
5052   else if (TREE_CODE (type) == ARRAY_TYPE)
5053     {
5054       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
5055       outer = build_array_type (inner, TYPE_DOMAIN (type));
5056     }
5057   else if (TREE_CODE (type) == FUNCTION_TYPE)
5058     {
5059       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
5060       outer = build_function_type (inner, TYPE_ARG_TYPES (type));
5061     }
5062   else if (TREE_CODE (type) == METHOD_TYPE)
5063     {
5064       inner = reconstruct_complex_type (TREE_TYPE (type), bottom);
5065       outer = build_method_type_directly (TYPE_METHOD_BASETYPE (type),
5066                                           inner, 
5067                                           TYPE_ARG_TYPES (type));
5068     }
5069   else
5070     return bottom;
5071
5072   TREE_READONLY (outer) = TREE_READONLY (type);
5073   TREE_THIS_VOLATILE (outer) = TREE_THIS_VOLATILE (type);
5074
5075   return outer;
5076 }
5077
5078 /* Returns a vector tree node given a vector mode, the inner type, and
5079    the signness.  */
5080
5081 tree
5082 make_vector (enum machine_mode mode, tree innertype, int unsignedp)
5083 {
5084   tree t;
5085
5086   t = make_node (VECTOR_TYPE);
5087   TREE_TYPE (t) = innertype;
5088   TYPE_MODE (t) = mode;
5089   TREE_UNSIGNED (TREE_TYPE (t)) = unsignedp;
5090   finish_vector_type (t);
5091
5092   return t;
5093 }
5094
5095 /* Given an initializer INIT, return TRUE if INIT is zero or some
5096    aggregate of zeros.  Otherwise return FALSE.  */
5097
5098 bool
5099 initializer_zerop (tree init)
5100 {
5101   STRIP_NOPS (init);
5102
5103   switch (TREE_CODE (init))
5104     {
5105     case INTEGER_CST:
5106       return integer_zerop (init);
5107     case REAL_CST:
5108       return real_zerop (init)
5109         && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
5110     case COMPLEX_CST:
5111       return integer_zerop (init)
5112         || (real_zerop (init)
5113             && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
5114             && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
5115     case CONSTRUCTOR:
5116       {
5117         /* Set is empty if it has no elements.  */
5118         if ((TREE_CODE (TREE_TYPE (init)) == SET_TYPE)
5119              && CONSTRUCTOR_ELTS (init))
5120           return false;
5121
5122         if (AGGREGATE_TYPE_P (TREE_TYPE (init)))
5123           {
5124             tree aggr_init = CONSTRUCTOR_ELTS (init);
5125
5126             while (aggr_init)
5127               {
5128                 if (! initializer_zerop (TREE_VALUE (aggr_init)))
5129                   return false;
5130                 aggr_init = TREE_CHAIN (aggr_init);
5131               }
5132             return true;
5133           }
5134         return false;
5135       }
5136     default:
5137       return false;
5138     }
5139 }
5140
5141 #include "gt-tree.h"