OSDN Git Service

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