OSDN Git Service

(stabilize_reference_1): Remove decl of unused variable.
[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 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20
21 /* This file contains the low level primitives for operating on tree nodes,
22    including allocation, list operations, interning of identifiers,
23    construction of data type nodes and statement nodes,
24    and construction of type conversion nodes.  It also contains
25    tables index by tree code that describe how to take apart
26    nodes of that code.
27
28    It is intended to be language-independent, but occasionally
29    calls language-dependent routines defined (for C) in typecheck.c.
30
31    The low-level allocation routines oballoc and permalloc
32    are used also for allocating many other kinds of objects
33    by all passes of the compiler.  */
34
35 #include <setjmp.h>
36 #include "config.h"
37 #include "flags.h"
38 #include "tree.h"
39 #include "function.h"
40 #include "obstack.h"
41 #ifdef __STDC__
42 #include "gstdarg.h"
43 #else
44 #include "gvarargs.h"
45 #endif
46 #include <stdio.h>
47
48 #define obstack_chunk_alloc xmalloc
49 #define obstack_chunk_free free
50
51 /* Tree nodes of permanent duration are allocated in this obstack.
52    They are the identifier nodes, and everything outside of
53    the bodies and parameters of function definitions.  */
54
55 struct obstack permanent_obstack;
56
57 /* The initial RTL, and all ..._TYPE nodes, in a function
58    are allocated in this obstack.  Usually they are freed at the
59    end of the function, but if the function is inline they are saved.
60    For top-level functions, this is maybepermanent_obstack.
61    Separate obstacks are made for nested functions.  */
62
63 struct obstack *function_maybepermanent_obstack;
64
65 /* This is the function_maybepermanent_obstack for top-level functions.  */
66
67 struct obstack maybepermanent_obstack;
68
69 /* The contents of the current function definition are allocated
70    in this obstack, and all are freed at the end of the function.
71    For top-level functions, this is temporary_obstack.
72    Separate obstacks are made for nested functions.  */
73
74 struct obstack *function_obstack;
75
76 /* This is used for reading initializers of global variables.  */
77
78 struct obstack temporary_obstack;
79
80 /* The tree nodes of an expression are allocated
81    in this obstack, and all are freed at the end of the expression.  */
82
83 struct obstack momentary_obstack;
84
85 /* The tree nodes of a declarator are allocated
86    in this obstack, and all are freed when the declarator
87    has been parsed.  */
88
89 static struct obstack temp_decl_obstack;
90
91 /* This points at either permanent_obstack
92    or the current function_maybepermanent_obstack.  */
93
94 struct obstack *saveable_obstack;
95
96 /* This is same as saveable_obstack during parse and expansion phase;
97    it points to the current function's obstack during optimization.
98    This is the obstack to be used for creating rtl objects.  */
99
100 struct obstack *rtl_obstack;
101
102 /* This points at either permanent_obstack or the current function_obstack.  */
103
104 struct obstack *current_obstack;
105
106 /* This points at either permanent_obstack or the current function_obstack
107    or momentary_obstack.  */
108
109 struct obstack *expression_obstack;
110
111 /* Stack of obstack selections for push_obstacks and pop_obstacks.  */
112
113 struct obstack_stack
114 {
115   struct obstack_stack *next;
116   struct obstack *current;
117   struct obstack *saveable;
118   struct obstack *expression;
119   struct obstack *rtl;
120 };
121
122 struct obstack_stack *obstack_stack;
123
124 /* Obstack for allocating struct obstack_stack entries.  */
125
126 static struct obstack obstack_stack_obstack;
127
128 /* Addresses of first objects in some obstacks.
129    This is for freeing their entire contents.  */
130 char *maybepermanent_firstobj;
131 char *temporary_firstobj;
132 char *momentary_firstobj;
133 char *temp_decl_firstobj;
134
135 /* This is used to preserve objects (mainly array initializers) that need to
136    live until the end of the current function, but no further.  */
137 char *momentary_function_firstobj;
138
139 /* Nonzero means all ..._TYPE nodes should be allocated permanently.  */
140
141 int all_types_permanent;
142
143 /* Stack of places to restore the momentary obstack back to.  */
144    
145 struct momentary_level
146 {
147   /* Pointer back to previous such level.  */
148   struct momentary_level *prev;
149   /* First object allocated within this level.  */
150   char *base;
151   /* Value of expression_obstack saved at entry to this level.  */
152   struct obstack *obstack;
153 };
154
155 struct momentary_level *momentary_stack;
156
157 /* Table indexed by tree code giving a string containing a character
158    classifying the tree code.  Possibilities are
159    t, d, s, c, r, <, 1, 2 and e.  See tree.def for details.  */
160
161 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
162
163 char *standard_tree_code_type[] = {
164 #include "tree.def"
165 };
166 #undef DEFTREECODE
167
168 /* Table indexed by tree code giving number of expression
169    operands beyond the fixed part of the node structure.
170    Not used for types or decls.  */
171
172 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
173
174 int standard_tree_code_length[] = {
175 #include "tree.def"
176 };
177 #undef DEFTREECODE
178
179 /* Names of tree components.
180    Used for printing out the tree and error messages.  */
181 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
182
183 char *standard_tree_code_name[] = {
184 #include "tree.def"
185 };
186 #undef DEFTREECODE
187
188 /* Table indexed by tree code giving a string containing a character
189    classifying the tree code.  Possibilities are
190    t, d, s, c, r, e, <, 1 and 2.  See tree.def for details.  */
191
192 char **tree_code_type;
193
194 /* Table indexed by tree code giving number of expression
195    operands beyond the fixed part of the node structure.
196    Not used for types or decls.  */
197
198 int *tree_code_length;
199
200 /* Table indexed by tree code giving name of tree code, as a string.  */
201
202 char **tree_code_name;
203
204 /* Statistics-gathering stuff.  */
205 typedef enum
206 {
207   d_kind,
208   t_kind,
209   b_kind,
210   s_kind,
211   r_kind,
212   e_kind,
213   c_kind,
214   id_kind,
215   op_id_kind,
216   perm_list_kind,
217   temp_list_kind,
218   vec_kind,
219   x_kind,
220   lang_decl,
221   lang_type,
222   all_kinds
223 } tree_node_kind;
224
225 int tree_node_counts[(int)all_kinds];
226 int tree_node_sizes[(int)all_kinds];
227 int id_string_size = 0;
228
229 char *tree_node_kind_names[] = {
230   "decls",
231   "types",
232   "blocks",
233   "stmts",
234   "refs",
235   "exprs",
236   "constants",
237   "identifiers",
238   "op_identifiers",
239   "perm_tree_lists",
240   "temp_tree_lists",
241   "vecs",
242   "random kinds",
243   "lang_decl kinds",
244   "lang_type kinds"
245 };
246
247 /* Hash table for uniquizing IDENTIFIER_NODEs by name.  */
248
249 #define MAX_HASH_TABLE 1009
250 static tree hash_table[MAX_HASH_TABLE]; /* id hash buckets */
251
252 /* 0 while creating built-in identifiers.  */
253 static int do_identifier_warnings;
254
255 /* Unique id for next decl created.  */
256 static int next_decl_uid;
257 /* Unique id for next type created.  */
258 static int next_type_uid = 1;
259
260 extern char *mode_name[];
261
262 void gcc_obstack_init ();
263 static tree stabilize_reference_1 ();
264 \f
265 /* Init the principal obstacks.  */
266
267 void
268 init_obstacks ()
269 {
270   gcc_obstack_init (&obstack_stack_obstack);
271   gcc_obstack_init (&permanent_obstack);
272
273   gcc_obstack_init (&temporary_obstack);
274   temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
275   gcc_obstack_init (&momentary_obstack);
276   momentary_firstobj = (char *) obstack_alloc (&momentary_obstack, 0);
277   momentary_function_firstobj = momentary_firstobj;
278   gcc_obstack_init (&maybepermanent_obstack);
279   maybepermanent_firstobj
280     = (char *) obstack_alloc (&maybepermanent_obstack, 0);
281   gcc_obstack_init (&temp_decl_obstack);
282   temp_decl_firstobj = (char *) obstack_alloc (&temp_decl_obstack, 0);
283
284   function_obstack = &temporary_obstack;
285   function_maybepermanent_obstack = &maybepermanent_obstack;
286   current_obstack = &permanent_obstack;
287   expression_obstack = &permanent_obstack;
288   rtl_obstack = saveable_obstack = &permanent_obstack;
289
290   /* Init the hash table of identifiers.  */
291   bzero (hash_table, sizeof hash_table);
292 }
293
294 void
295 gcc_obstack_init (obstack)
296      struct obstack *obstack;
297 {
298   /* Let particular systems override the size of a chunk.  */
299 #ifndef OBSTACK_CHUNK_SIZE
300 #define OBSTACK_CHUNK_SIZE 0
301 #endif
302   /* Let them override the alloc and free routines too.  */
303 #ifndef OBSTACK_CHUNK_ALLOC
304 #define OBSTACK_CHUNK_ALLOC xmalloc
305 #endif
306 #ifndef OBSTACK_CHUNK_FREE
307 #define OBSTACK_CHUNK_FREE free
308 #endif
309   _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
310                   (void *(*) ()) OBSTACK_CHUNK_ALLOC,
311                   (void (*) ()) OBSTACK_CHUNK_FREE);
312 }
313
314 /* Save all variables describing the current status into the structure *P.
315    This is used before starting a nested function.  */
316
317 void
318 save_tree_status (p)
319      struct function *p;
320 {
321   p->all_types_permanent = all_types_permanent;
322   p->momentary_stack = momentary_stack;
323   p->maybepermanent_firstobj = maybepermanent_firstobj;
324   p->momentary_firstobj = momentary_firstobj;
325   p->momentary_function_firstobj = momentary_function_firstobj;
326   p->function_obstack = function_obstack;
327   p->function_maybepermanent_obstack = function_maybepermanent_obstack;
328   p->current_obstack = current_obstack;
329   p->expression_obstack = expression_obstack;
330   p->saveable_obstack = saveable_obstack;
331   p->rtl_obstack = rtl_obstack;
332
333   /* Objects that need to be saved in this function can be in the nonsaved
334      obstack of the enclosing function since they can't possibly be needed
335      once it has returned.  */
336   function_maybepermanent_obstack = function_obstack;
337
338   function_obstack = (struct obstack *) xmalloc (sizeof (struct obstack));
339   gcc_obstack_init (function_obstack);
340
341   current_obstack = &permanent_obstack;
342   expression_obstack = &permanent_obstack;
343   rtl_obstack = saveable_obstack = &permanent_obstack;
344
345   momentary_firstobj = (char *) obstack_finish (&momentary_obstack);
346   momentary_function_firstobj = momentary_firstobj;
347   maybepermanent_firstobj
348     = (char *) obstack_finish (function_maybepermanent_obstack);
349 }
350
351 /* Restore all variables describing the current status from the structure *P.
352    This is used after a nested function.  */
353
354 void
355 restore_tree_status (p)
356      struct function *p;
357 {
358   all_types_permanent = p->all_types_permanent;
359   momentary_stack = p->momentary_stack;
360
361   obstack_free (&momentary_obstack, momentary_function_firstobj);
362
363   /* Free saveable storage used by the function just compiled and not
364      saved.
365
366      CAUTION: This is in function_obstack of the containing function.  So
367      we must be sure that we never allocate from that obstack during
368      the compilation of a nested function if we expect it to survive past the
369      nested function's end.  */
370   obstack_free (function_maybepermanent_obstack, maybepermanent_firstobj);
371
372   obstack_free (function_obstack, 0);
373   free (function_obstack);
374
375   momentary_firstobj = p->momentary_firstobj;
376   momentary_function_firstobj = p->momentary_function_firstobj;
377   maybepermanent_firstobj = p->maybepermanent_firstobj;
378   function_obstack = p->function_obstack;
379   function_maybepermanent_obstack = p->function_maybepermanent_obstack;
380   current_obstack = p->current_obstack;
381   expression_obstack = p->expression_obstack;
382   saveable_obstack = p->saveable_obstack;
383   rtl_obstack = p->rtl_obstack;
384 }
385 \f
386 /* Start allocating on the temporary (per function) obstack.
387    This is done in start_function before parsing the function body,
388    and before each initialization at top level, and to go back
389    to temporary allocation after doing permanent_allocation.  */
390
391 void
392 temporary_allocation ()
393 {
394   /* Note that function_obstack at top level points to temporary_obstack.
395      But within a nested function context, it is a separate obstack.  */
396   current_obstack = function_obstack;
397   expression_obstack = function_obstack;
398   rtl_obstack = saveable_obstack = function_maybepermanent_obstack;
399   momentary_stack = 0;
400 }
401
402 /* Start allocating on the permanent obstack but don't
403    free the temporary data.  After calling this, call
404    `permanent_allocation' to fully resume permanent allocation status.  */
405
406 void
407 end_temporary_allocation ()
408 {
409   current_obstack = &permanent_obstack;
410   expression_obstack = &permanent_obstack;
411   rtl_obstack = saveable_obstack = &permanent_obstack;
412 }
413
414 /* Resume allocating on the temporary obstack, undoing
415    effects of `end_temporary_allocation'.  */
416
417 void
418 resume_temporary_allocation ()
419 {
420   current_obstack = function_obstack;
421   expression_obstack = function_obstack;
422   rtl_obstack = saveable_obstack = function_maybepermanent_obstack;
423 }
424
425 /* While doing temporary allocation, switch to allocating in such a
426    way as to save all nodes if the function is inlined.  Call
427    resume_temporary_allocation to go back to ordinary temporary
428    allocation.  */
429
430 void
431 saveable_allocation ()
432 {
433   /* Note that function_obstack at top level points to temporary_obstack.
434      But within a nested function context, it is a separate obstack.  */
435   expression_obstack = current_obstack = saveable_obstack;
436 }
437
438 /* Switch to current obstack CURRENT and maybepermanent obstack SAVEABLE,
439    recording the previously current obstacks on a stack.
440    This does not free any storage in any obstack.  */
441
442 void
443 push_obstacks (current, saveable)
444      struct obstack *current, *saveable;
445 {
446   struct obstack_stack *p
447     = (struct obstack_stack *) obstack_alloc (&obstack_stack_obstack,
448                                               (sizeof (struct obstack_stack)));
449
450   p->current = current_obstack;
451   p->saveable = saveable_obstack;
452   p->expression = expression_obstack;
453   p->rtl = rtl_obstack;
454   p->next = obstack_stack;
455   obstack_stack = p;
456
457   current_obstack = current;
458   expression_obstack = current;
459   rtl_obstack = saveable_obstack = saveable;
460 }
461
462 /* Save the current set of obstacks, but don't change them.  */
463
464 void
465 push_obstacks_nochange ()
466 {
467   struct obstack_stack *p
468     = (struct obstack_stack *) obstack_alloc (&obstack_stack_obstack,
469                                               (sizeof (struct obstack_stack)));
470
471   p->current = current_obstack;
472   p->saveable = saveable_obstack;
473   p->expression = expression_obstack;
474   p->rtl = rtl_obstack;
475   p->next = obstack_stack;
476   obstack_stack = p;
477 }
478
479 /* Pop the obstack selection stack.  */
480
481 void
482 pop_obstacks ()
483 {
484   struct obstack_stack *p = obstack_stack;
485   obstack_stack = p->next;
486
487   current_obstack = p->current;
488   saveable_obstack = p->saveable;
489   expression_obstack = p->expression;
490   rtl_obstack = p->rtl;
491
492   obstack_free (&obstack_stack_obstack, p);
493 }
494
495 /* Nonzero if temporary allocation is currently in effect.
496    Zero if currently doing permanent allocation.  */
497
498 int
499 allocation_temporary_p ()
500 {
501   return current_obstack != &permanent_obstack;
502 }
503
504 /* Go back to allocating on the permanent obstack
505    and free everything in the temporary obstack.
506
507    FUNCTION_END is true only if we have just finished compiling a function.
508    In that case, we also free preserved initial values on the momentary
509    obstack.  */
510
511 void
512 permanent_allocation (function_end)
513      int function_end;
514 {
515   /* Free up previous temporary obstack data */
516   obstack_free (&temporary_obstack, temporary_firstobj);
517   if (function_end)
518     obstack_free (&momentary_obstack, momentary_function_firstobj);
519   else
520     obstack_free (&momentary_obstack, momentary_firstobj);
521   obstack_free (&maybepermanent_obstack, maybepermanent_firstobj);
522   obstack_free (&temp_decl_obstack, temp_decl_firstobj);
523
524   current_obstack = &permanent_obstack;
525   expression_obstack = &permanent_obstack;
526   rtl_obstack = saveable_obstack = &permanent_obstack;
527 }
528
529 /* Save permanently everything on the maybepermanent_obstack.  */
530
531 void
532 preserve_data ()
533 {
534   maybepermanent_firstobj
535     = (char *) obstack_alloc (function_maybepermanent_obstack, 0);
536 }
537
538 void
539 preserve_initializer ()
540 {
541   struct momentary_level *tem;
542   char *old_momentary;
543
544   temporary_firstobj
545     = (char *) obstack_alloc (&temporary_obstack, 0);
546   maybepermanent_firstobj
547     = (char *) obstack_alloc (function_maybepermanent_obstack, 0);
548
549   old_momentary = momentary_firstobj;
550   momentary_firstobj
551     = (char *) obstack_alloc (&momentary_obstack, 0);
552   if (momentary_firstobj != old_momentary)
553     for (tem = momentary_stack; tem; tem = tem->prev)
554       tem->base = momentary_firstobj;
555 }
556
557 /* Start allocating new rtl in current_obstack.
558    Use resume_temporary_allocation
559    to go back to allocating rtl in saveable_obstack.  */
560
561 void
562 rtl_in_current_obstack ()
563 {
564   rtl_obstack = current_obstack;
565 }
566
567 /* Start allocating rtl from saveable_obstack.  Intended to be used after
568    a call to push_obstacks_nochange.  */
569
570 void
571 rtl_in_saveable_obstack ()
572 {
573   rtl_obstack = saveable_obstack;
574 }
575 \f
576 /* Allocate SIZE bytes in the current obstack
577    and return a pointer to them.
578    In practice the current obstack is always the temporary one.  */
579
580 char *
581 oballoc (size)
582      int size;
583 {
584   return (char *) obstack_alloc (current_obstack, size);
585 }
586
587 /* Free the object PTR in the current obstack
588    as well as everything allocated since PTR.
589    In practice the current obstack is always the temporary one.  */
590
591 void
592 obfree (ptr)
593      char *ptr;
594 {
595   obstack_free (current_obstack, ptr);
596 }
597
598 /* Allocate SIZE bytes in the permanent obstack
599    and return a pointer to them.  */
600
601 char *
602 permalloc (size)
603      int size;
604 {
605   return (char *) obstack_alloc (&permanent_obstack, size);
606 }
607
608 /* Allocate NELEM items of SIZE bytes in the permanent obstack
609    and return a pointer to them.  The storage is cleared before
610    returning the value.  */
611
612 char *
613 perm_calloc (nelem, size)
614      int nelem;
615      long size;
616 {
617   char *rval = (char *) obstack_alloc (&permanent_obstack, nelem * size);
618   bzero (rval, nelem * size);
619   return rval;
620 }
621
622 /* Allocate SIZE bytes in the saveable obstack
623    and return a pointer to them.  */
624
625 char *
626 savealloc (size)
627      int size;
628 {
629   return (char *) obstack_alloc (saveable_obstack, size);
630 }
631 \f
632 /* Print out which obstack an object is in.  */
633
634 void
635 print_obstack_name (object, file, prefix)
636      char *object;
637      FILE *file;
638      char *prefix;
639 {
640   struct obstack *obstack = NULL;
641   char *obstack_name = NULL;
642   struct function *p;
643
644   for (p = outer_function_chain; p; p = p->next)
645     {
646       if (_obstack_allocated_p (p->function_obstack, object))
647         {
648           obstack = p->function_obstack;
649           obstack_name = "containing function obstack";
650         }
651       if (_obstack_allocated_p (p->function_maybepermanent_obstack, object))
652         {
653           obstack = p->function_maybepermanent_obstack;
654           obstack_name = "containing function maybepermanent obstack";
655         }
656     }
657
658   if (_obstack_allocated_p (&obstack_stack_obstack, object))
659     {
660       obstack = &obstack_stack_obstack;
661       obstack_name = "obstack_stack_obstack";
662     }
663   else if (_obstack_allocated_p (function_obstack, object))
664     {
665       obstack = function_obstack;
666       obstack_name = "function obstack";
667     }
668   else if (_obstack_allocated_p (&permanent_obstack, object))
669     {
670       obstack = &permanent_obstack;
671       obstack_name = "permanent_obstack";
672     }
673   else if (_obstack_allocated_p (&momentary_obstack, object))
674     {
675       obstack = &momentary_obstack;
676       obstack_name = "momentary_obstack";
677     }
678   else if (_obstack_allocated_p (function_maybepermanent_obstack, object))
679     {
680       obstack = function_maybepermanent_obstack;
681       obstack_name = "function maybepermanent obstack";
682     }
683   else if (_obstack_allocated_p (&temp_decl_obstack, object))
684     {
685       obstack = &temp_decl_obstack;
686       obstack_name = "temp_decl_obstack";
687     }
688
689   /* Check to see if the object is in the free area of the obstack. */
690   if (obstack != NULL)
691     {
692       if (object >= obstack->next_free
693           && object < obstack->chunk_limit)
694         fprintf (file, "%s in free portion of obstack %s",
695                  prefix, obstack_name);
696       else
697         fprintf (file, "%s allocated from %s", prefix, obstack_name);
698     }
699   else
700     fprintf (file, "%s not allocated from any obstack", prefix);
701 }
702
703 void
704 debug_obstack (object)
705      char *object;
706 {
707   print_obstack_name (object, stderr, "object");
708   fprintf (stderr, ".\n");
709 }
710
711 /* Return 1 if OBJ is in the permanent obstack.
712    This is slow, and should be used only for debugging.
713    Use TREE_PERMANENT for other purposes.  */
714
715 int
716 object_permanent_p (obj)
717      tree obj;
718 {
719   return _obstack_allocated_p (&permanent_obstack, obj);
720 }
721 \f
722 /* Start a level of momentary allocation.
723    In C, each compound statement has its own level
724    and that level is freed at the end of each statement.
725    All expression nodes are allocated in the momentary allocation level.  */
726
727 void
728 push_momentary ()
729 {
730   struct momentary_level *tem
731     = (struct momentary_level *) obstack_alloc (&momentary_obstack,
732                                                 sizeof (struct momentary_level));
733   tem->prev = momentary_stack;
734   tem->base = (char *) obstack_base (&momentary_obstack);
735   tem->obstack = expression_obstack;
736   momentary_stack = tem;
737   expression_obstack = &momentary_obstack;
738 }
739
740 /* Free all the storage in the current momentary-allocation level.
741    In C, this happens at the end of each statement.  */
742
743 void
744 clear_momentary ()
745 {
746   obstack_free (&momentary_obstack, momentary_stack->base);
747 }
748
749 /* Discard a level of momentary allocation.
750    In C, this happens at the end of each compound statement.
751    Restore the status of expression node allocation
752    that was in effect before this level was created.  */
753
754 void
755 pop_momentary ()
756 {
757   struct momentary_level *tem = momentary_stack;
758   momentary_stack = tem->prev;
759   expression_obstack = tem->obstack;
760   /* We can't free TEM from the momentary_obstack, because there might
761      be objects above it which have been saved.  We can free back to the
762      stack of the level we are popping off though.  */
763   obstack_free (&momentary_obstack, tem->base);
764 }
765
766 /* Pop back to the previous level of momentary allocation,
767    but don't free any momentary data just yet.  */
768
769 void
770 pop_momentary_nofree ()
771 {
772   struct momentary_level *tem = momentary_stack;
773   momentary_stack = tem->prev;
774   expression_obstack = tem->obstack;
775 }
776
777 /* Call when starting to parse a declaration:
778    make expressions in the declaration last the length of the function.
779    Returns an argument that should be passed to resume_momentary later.  */
780
781 int
782 suspend_momentary ()
783 {
784   register int tem = expression_obstack == &momentary_obstack;
785   expression_obstack = saveable_obstack;
786   return tem;
787 }
788
789 /* Call when finished parsing a declaration:
790    restore the treatment of node-allocation that was
791    in effect before the suspension.
792    YES should be the value previously returned by suspend_momentary.  */
793
794 void
795 resume_momentary (yes)
796      int yes;
797 {
798   if (yes)
799     expression_obstack = &momentary_obstack;
800 }
801 \f
802 /* Init the tables indexed by tree code.
803    Note that languages can add to these tables to define their own codes.  */
804
805 void
806 init_tree_codes ()
807 {
808   tree_code_type = (char **) xmalloc (sizeof (standard_tree_code_type));
809   tree_code_length = (int *) xmalloc (sizeof (standard_tree_code_length));
810   tree_code_name = (char **) xmalloc (sizeof (standard_tree_code_name));
811   bcopy (standard_tree_code_type, tree_code_type,
812          sizeof (standard_tree_code_type));
813   bcopy (standard_tree_code_length, tree_code_length,
814          sizeof (standard_tree_code_length));
815   bcopy (standard_tree_code_name, tree_code_name,
816          sizeof (standard_tree_code_name));
817 }
818
819 /* Return a newly allocated node of code CODE.
820    Initialize the node's unique id and its TREE_PERMANENT flag.
821    For decl and type nodes, some other fields are initialized.
822    The rest of the node is initialized to zero.
823
824    Achoo!  I got a code in the node.  */
825
826 tree
827 make_node (code)
828      enum tree_code code;
829 {
830   register tree t;
831   register int type = TREE_CODE_CLASS (code);
832   register int length;
833   register struct obstack *obstack = current_obstack;
834   register int i;
835   register tree_node_kind kind;
836
837   switch (type)
838     {
839     case 'd':  /* A decl node */
840 #ifdef GATHER_STATISTICS
841       kind = d_kind;
842 #endif
843       length = sizeof (struct tree_decl);
844       /* All decls in an inline function need to be saved.  */
845       if (obstack != &permanent_obstack)
846         obstack = saveable_obstack;
847
848       /* PARM_DECLs go on the context of the parent. If this is a nested
849          function, then we must allocate the PARM_DECL on the parent's
850          obstack, so that they will live to the end of the parent's
851          closing brace.  This is neccesary in case we try to inline the
852          function into its parent.
853
854          PARM_DECLs of top-level functions do not have this problem.  However,
855          we allocate them where we put the FUNCTION_DECL for languauges such as
856          Ada that need to consult some flags in the PARM_DECLs of the function
857          when calling it. 
858
859          See comment in restore_tree_status for why we can't put this
860          in function_obstack.  */
861       if (code == PARM_DECL && obstack != &permanent_obstack)
862         {
863           tree context = 0;
864           if (current_function_decl)
865             context = decl_function_context (current_function_decl);
866
867           if (context)
868             obstack
869               = find_function_data (context)->function_maybepermanent_obstack;
870         }
871       break;
872
873     case 't':  /* a type node */
874 #ifdef GATHER_STATISTICS
875       kind = t_kind;
876 #endif
877       length = sizeof (struct tree_type);
878       /* All data types are put where we can preserve them if nec.  */
879       if (obstack != &permanent_obstack)
880         obstack = all_types_permanent ? &permanent_obstack : saveable_obstack;
881       break;
882
883     case 'b':  /* a lexical block */
884 #ifdef GATHER_STATISTICS
885       kind = b_kind;
886 #endif
887       length = sizeof (struct tree_block);
888       /* All BLOCK nodes are put where we can preserve them if nec.  */
889       if (obstack != &permanent_obstack)
890         obstack = saveable_obstack;
891       break;
892
893     case 's':  /* an expression with side effects */
894 #ifdef GATHER_STATISTICS
895       kind = s_kind;
896       goto usual_kind;
897 #endif
898     case 'r':  /* a reference */
899 #ifdef GATHER_STATISTICS
900       kind = r_kind;
901       goto usual_kind;
902 #endif
903     case 'e':  /* an expression */
904     case '<':  /* a comparison expression */
905     case '1':  /* a unary arithmetic expression */
906     case '2':  /* a binary arithmetic expression */
907 #ifdef GATHER_STATISTICS
908       kind = e_kind;
909     usual_kind:
910 #endif
911       obstack = expression_obstack;
912       /* All BIND_EXPR nodes are put where we can preserve them if nec.  */
913       if (code == BIND_EXPR && obstack != &permanent_obstack)
914         obstack = saveable_obstack;
915       length = sizeof (struct tree_exp)
916         + (tree_code_length[(int) code] - 1) * sizeof (char *);
917       break;
918
919     case 'c':  /* a constant */
920 #ifdef GATHER_STATISTICS
921       kind = c_kind;
922 #endif
923       obstack = expression_obstack;
924
925       /* We can't use tree_code_length for INTEGER_CST, since the number of
926          words is machine-dependent due to varying length of HOST_WIDE_INT,
927          which might be wider than a pointer (e.g., long long).  Similarly
928          for REAL_CST, since the number of words is machine-dependent due
929          to varying size and alignment of `double'.  */
930
931       if (code == INTEGER_CST)
932         length = sizeof (struct tree_int_cst);
933       else if (code == REAL_CST)
934         length = sizeof (struct tree_real_cst);
935       else
936         length = sizeof (struct tree_common)
937           + tree_code_length[(int) code] * sizeof (char *);
938       break;
939
940     case 'x':  /* something random, like an identifier.  */
941 #ifdef GATHER_STATISTICS
942       if (code == IDENTIFIER_NODE)
943         kind = id_kind;
944       else if (code == OP_IDENTIFIER)
945         kind = op_id_kind;
946       else if (code == TREE_VEC)
947         kind = vec_kind;
948       else
949         kind = x_kind;
950 #endif
951       length = sizeof (struct tree_common)
952         + tree_code_length[(int) code] * sizeof (char *);
953       /* Identifier nodes are always permanent since they are
954          unique in a compiler run.  */
955       if (code == IDENTIFIER_NODE) obstack = &permanent_obstack;
956       break;
957
958     default:
959       abort ();
960     }
961
962   t = (tree) obstack_alloc (obstack, length);
963
964 #ifdef GATHER_STATISTICS
965   tree_node_counts[(int)kind]++;
966   tree_node_sizes[(int)kind] += length;
967 #endif
968
969   /* Clear a word at a time.  */
970   for (i = (length / sizeof (int)) - 1; i >= 0; i--)
971     ((int *) t)[i] = 0;
972   /* Clear any extra bytes.  */
973   for (i = length / sizeof (int) * sizeof (int); i < length; i++)
974     ((char *) t)[i] = 0;
975
976   TREE_SET_CODE (t, code);
977   if (obstack == &permanent_obstack)
978     TREE_PERMANENT (t) = 1;
979
980   switch (type)
981     {
982     case 's':
983       TREE_SIDE_EFFECTS (t) = 1;
984       TREE_TYPE (t) = void_type_node;
985       break;
986
987     case 'd':
988       if (code != FUNCTION_DECL)
989         DECL_ALIGN (t) = 1;
990       DECL_IN_SYSTEM_HEADER (t)
991         = in_system_header && (obstack == &permanent_obstack);
992       DECL_SOURCE_LINE (t) = lineno;
993       DECL_SOURCE_FILE (t) = (input_filename) ? input_filename : "<built-in>";
994       DECL_UID (t) = next_decl_uid++;
995       break;
996
997     case 't':
998       TYPE_UID (t) = next_type_uid++;
999       TYPE_ALIGN (t) = 1;
1000       TYPE_MAIN_VARIANT (t) = t;
1001       TYPE_OBSTACK (t) = obstack;
1002       break;
1003
1004     case 'c':
1005       TREE_CONSTANT (t) = 1;
1006       break;
1007     }
1008
1009   return t;
1010 }
1011 \f
1012 /* Return a new node with the same contents as NODE
1013    except that its TREE_CHAIN is zero and it has a fresh uid.  */
1014
1015 tree
1016 copy_node (node)
1017      tree node;
1018 {
1019   register tree t;
1020   register enum tree_code code = TREE_CODE (node);
1021   register int length;
1022   register int i;
1023
1024   switch (TREE_CODE_CLASS (code))
1025     {
1026     case 'd':  /* A decl node */
1027       length = sizeof (struct tree_decl);
1028       break;
1029
1030     case 't':  /* a type node */
1031       length = sizeof (struct tree_type);
1032       break;
1033
1034     case 'b':  /* a lexical block node */
1035       length = sizeof (struct tree_block);
1036       break;
1037
1038     case 'r':  /* a reference */
1039     case 'e':  /* an expression */
1040     case 's':  /* an expression with side effects */
1041     case '<':  /* a comparison expression */
1042     case '1':  /* a unary arithmetic expression */
1043     case '2':  /* a binary arithmetic expression */
1044       length = sizeof (struct tree_exp)
1045         + (tree_code_length[(int) code] - 1) * sizeof (char *);
1046       break;
1047
1048     case 'c':  /* a constant */
1049       /* We can't use tree_code_length for INTEGER_CST, since the number of
1050          words is machine-dependent due to varying length of HOST_WIDE_INT,
1051          which might be wider than a pointer (e.g., long long).  Similarly
1052          for REAL_CST, since the number of words is machine-dependent due
1053          to varying size and alignment of `double'.  */
1054       if (code == INTEGER_CST)
1055         {
1056           length = sizeof (struct tree_int_cst);
1057           break;
1058         }
1059       else if (code == REAL_CST)
1060         {
1061           length = sizeof (struct tree_real_cst);
1062           break;
1063         }
1064
1065     case 'x':  /* something random, like an identifier.  */
1066       length = sizeof (struct tree_common)
1067         + tree_code_length[(int) code] * sizeof (char *);
1068       if (code == TREE_VEC)
1069         length += (TREE_VEC_LENGTH (node) - 1) * sizeof (char *);
1070     }
1071
1072   t = (tree) obstack_alloc (current_obstack, length);
1073
1074   for (i = (length / sizeof (int)) - 1; i >= 0; i--)
1075     ((int *) t)[i] = ((int *) node)[i];
1076   /* Clear any extra bytes.  */
1077   for (i = length / sizeof (int) * sizeof (int); i < length; i++)
1078     ((char *) t)[i] = ((char *) node)[i];
1079
1080   TREE_CHAIN (t) = 0;
1081
1082   if (TREE_CODE_CLASS (code) == 'd')
1083     DECL_UID (t) = next_decl_uid++;
1084   else if (TREE_CODE_CLASS (code) == 't')
1085     {
1086       TYPE_UID (t) = next_type_uid++;
1087       TYPE_OBSTACK (t) = current_obstack;
1088     }
1089
1090   TREE_PERMANENT (t) = (current_obstack == &permanent_obstack);
1091
1092   return t;
1093 }
1094
1095 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1096    For example, this can copy a list made of TREE_LIST nodes.  */
1097
1098 tree
1099 copy_list (list)
1100      tree list;
1101 {
1102   tree head;
1103   register tree prev, next;
1104
1105   if (list == 0)
1106     return 0;
1107
1108   head = prev = copy_node (list);
1109   next = TREE_CHAIN (list);
1110   while (next)
1111     {
1112       TREE_CHAIN (prev) = copy_node (next);
1113       prev = TREE_CHAIN (prev);
1114       next = TREE_CHAIN (next);
1115     }
1116   return head;
1117 }
1118 \f
1119 #define HASHBITS 30
1120
1121 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
1122    If an identifier with that name has previously been referred to,
1123    the same node is returned this time.  */
1124
1125 tree
1126 get_identifier (text)
1127      register char *text;
1128 {
1129   register int hi;
1130   register int i;
1131   register tree idp;
1132   register int len, hash_len;
1133
1134   /* Compute length of text in len.  */
1135   for (len = 0; text[len]; len++);
1136
1137   /* Decide how much of that length to hash on */
1138   hash_len = len;
1139   if (warn_id_clash && len > id_clash_len)
1140     hash_len = id_clash_len;
1141
1142   /* Compute hash code */
1143   hi = hash_len * 613 + (unsigned)text[0];
1144   for (i = 1; i < hash_len; i += 2)
1145     hi = ((hi * 613) + (unsigned)(text[i]));
1146
1147   hi &= (1 << HASHBITS) - 1;
1148   hi %= MAX_HASH_TABLE;
1149   
1150   /* Search table for identifier */
1151   for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
1152     if (IDENTIFIER_LENGTH (idp) == len
1153         && IDENTIFIER_POINTER (idp)[0] == text[0]
1154         && !bcmp (IDENTIFIER_POINTER (idp), text, len))
1155       return idp;               /* <-- return if found */
1156
1157   /* Not found; optionally warn about a similar identifier */
1158   if (warn_id_clash && do_identifier_warnings && len >= id_clash_len)
1159     for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
1160       if (!strncmp (IDENTIFIER_POINTER (idp), text, id_clash_len))
1161         {
1162           warning ("`%s' and `%s' identical in first %d characters",
1163                    IDENTIFIER_POINTER (idp), text, id_clash_len);
1164           break;
1165         }
1166
1167   if (tree_code_length[(int) IDENTIFIER_NODE] < 0)
1168     abort ();                   /* set_identifier_size hasn't been called.  */
1169
1170   /* Not found, create one, add to chain */
1171   idp = make_node (IDENTIFIER_NODE);
1172   IDENTIFIER_LENGTH (idp) = len;
1173 #ifdef GATHER_STATISTICS
1174   id_string_size += len;
1175 #endif
1176
1177   IDENTIFIER_POINTER (idp) = obstack_copy0 (&permanent_obstack, text, len);
1178
1179   TREE_CHAIN (idp) = hash_table[hi];
1180   hash_table[hi] = idp;
1181   return idp;                   /* <-- return if created */
1182 }
1183
1184 /* Enable warnings on similar identifiers (if requested).
1185    Done after the built-in identifiers are created.  */
1186
1187 void
1188 start_identifier_warnings ()
1189 {
1190   do_identifier_warnings = 1;
1191 }
1192
1193 /* Record the size of an identifier node for the language in use.
1194    SIZE is the total size in bytes.
1195    This is called by the language-specific files.  This must be
1196    called before allocating any identifiers.  */
1197
1198 void
1199 set_identifier_size (size)
1200      int size;
1201 {
1202   tree_code_length[(int) IDENTIFIER_NODE]
1203     = (size - sizeof (struct tree_common)) / sizeof (tree);
1204 }
1205 \f
1206 /* Return a newly constructed INTEGER_CST node whose constant value
1207    is specified by the two ints LOW and HI.
1208    The TREE_TYPE is set to `int'. 
1209
1210    This function should be used via the `build_int_2' macro.  */
1211
1212 tree
1213 build_int_2_wide (low, hi)
1214      HOST_WIDE_INT low, hi;
1215 {
1216   register tree t = make_node (INTEGER_CST);
1217   TREE_INT_CST_LOW (t) = low;
1218   TREE_INT_CST_HIGH (t) = hi;
1219   TREE_TYPE (t) = integer_type_node;
1220   return t;
1221 }
1222
1223 /* Return a new REAL_CST node whose type is TYPE and value is D.  */
1224
1225 tree
1226 build_real (type, d)
1227      tree type;
1228      REAL_VALUE_TYPE d;
1229 {
1230   tree v;
1231   int overflow = 0;
1232
1233   /* Check for valid float value for this type on this target machine;
1234      if not, can print error message and store a valid value in D.  */
1235 #ifdef CHECK_FLOAT_VALUE
1236   CHECK_FLOAT_VALUE (TYPE_MODE (type), d, overflow);
1237 #endif
1238
1239   v = make_node (REAL_CST);
1240   TREE_TYPE (v) = type;
1241   TREE_REAL_CST (v) = d;
1242   TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
1243   return v;
1244 }
1245
1246 /* Return a new REAL_CST node whose type is TYPE
1247    and whose value is the integer value of the INTEGER_CST node I.  */
1248
1249 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1250
1251 REAL_VALUE_TYPE
1252 real_value_from_int_cst (i)
1253      tree i;
1254 {
1255   REAL_VALUE_TYPE d;
1256   REAL_VALUE_TYPE e;
1257   /* Some 386 compilers mishandle unsigned int to float conversions,
1258      so introduce a temporary variable E to avoid those bugs.  */
1259
1260 #ifdef REAL_ARITHMETIC
1261   if (! TREE_UNSIGNED (TREE_TYPE (i)))
1262     REAL_VALUE_FROM_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i));
1263   else
1264     REAL_VALUE_FROM_UNSIGNED_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i));
1265 #else /* not REAL_ARITHMETIC */
1266   if (TREE_INT_CST_HIGH (i) < 0 && ! TREE_UNSIGNED (TREE_TYPE (i)))
1267     {
1268       d = (double) (~ TREE_INT_CST_HIGH (i));
1269       e = ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
1270             * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
1271       d *= e;
1272       e = (double) (unsigned HOST_WIDE_INT) (~ TREE_INT_CST_LOW (i));
1273       d += e;
1274       d = (- d - 1.0);
1275     }
1276   else
1277     {
1278       d = (double) (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (i);
1279       e = ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
1280             * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
1281       d *= e;
1282       e = (double) (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (i);
1283       d += e;
1284     }
1285 #endif /* not REAL_ARITHMETIC */
1286   return d;
1287 }
1288
1289 /* This function can't be implemented if we can't do arithmetic
1290    on the float representation.  */
1291
1292 tree
1293 build_real_from_int_cst (type, i)
1294      tree type;
1295      tree i;
1296 {
1297   tree v;
1298   int overflow = TREE_OVERFLOW (i);
1299   REAL_VALUE_TYPE d;
1300   jmp_buf float_error;
1301
1302   v = make_node (REAL_CST);
1303   TREE_TYPE (v) = type;
1304
1305   if (setjmp (float_error))
1306     {
1307       d = dconst0;
1308       overflow = 1;
1309       goto got_it;
1310     }
1311
1312   set_float_handler (float_error);
1313
1314   d = REAL_VALUE_TRUNCATE (TYPE_MODE (type), real_value_from_int_cst (i));
1315
1316   /* Check for valid float value for this type on this target machine.  */
1317
1318  got_it:
1319   set_float_handler (NULL_PTR);
1320
1321 #ifdef CHECK_FLOAT_VALUE
1322   CHECK_FLOAT_VALUE (TYPE_MODE (type), d, overflow);
1323 #endif
1324
1325   TREE_REAL_CST (v) = d;
1326   TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
1327   return v;
1328 }
1329
1330 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1331
1332 /* Return a newly constructed STRING_CST node whose value is
1333    the LEN characters at STR.
1334    The TREE_TYPE is not initialized.  */
1335
1336 tree
1337 build_string (len, str)
1338      int len;
1339      char *str;
1340 {
1341   /* Put the string in saveable_obstack since it will be placed in the RTL
1342      for an "asm" statement and will also be kept around a while if
1343      deferring constant output in varasm.c.  */
1344
1345   register tree s = make_node (STRING_CST);
1346   TREE_STRING_LENGTH (s) = len;
1347   TREE_STRING_POINTER (s) = obstack_copy0 (saveable_obstack, str, len);
1348   return s;
1349 }
1350
1351 /* Return a newly constructed COMPLEX_CST node whose value is
1352    specified by the real and imaginary parts REAL and IMAG.
1353    Both REAL and IMAG should be constant nodes.
1354    The TREE_TYPE is not initialized.  */
1355
1356 tree
1357 build_complex (real, imag)
1358      tree real, imag;
1359 {
1360   register tree t = make_node (COMPLEX_CST);
1361
1362   TREE_REALPART (t) = real;
1363   TREE_IMAGPART (t) = imag;
1364   TREE_TYPE (t) = build_complex_type (TREE_TYPE (real));
1365   TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1366   TREE_CONSTANT_OVERFLOW (t)
1367     = TREE_CONSTANT_OVERFLOW (real) | TREE_CONSTANT_OVERFLOW (imag);
1368   return t;
1369 }
1370
1371 /* Build a newly constructed TREE_VEC node of length LEN.  */
1372 tree
1373 make_tree_vec (len)
1374      int len;
1375 {
1376   register tree t;
1377   register int length = (len-1) * sizeof (tree) + sizeof (struct tree_vec);
1378   register struct obstack *obstack = current_obstack;
1379   register int i;
1380
1381 #ifdef GATHER_STATISTICS
1382   tree_node_counts[(int)vec_kind]++;
1383   tree_node_sizes[(int)vec_kind] += length;
1384 #endif
1385
1386   t = (tree) obstack_alloc (obstack, length);
1387
1388   for (i = (length / sizeof (int)) - 1; i >= 0; i--)
1389     ((int *) t)[i] = 0;
1390
1391   TREE_SET_CODE (t, TREE_VEC);
1392   TREE_VEC_LENGTH (t) = len;
1393   if (obstack == &permanent_obstack)
1394     TREE_PERMANENT (t) = 1;
1395
1396   return t;
1397 }
1398 \f
1399 /* Return 1 if EXPR is the integer constant zero.  */
1400
1401 int
1402 integer_zerop (expr)
1403      tree expr;
1404 {
1405   STRIP_NOPS (expr);
1406
1407   return (TREE_CODE (expr) == INTEGER_CST
1408           && TREE_INT_CST_LOW (expr) == 0
1409           && TREE_INT_CST_HIGH (expr) == 0);
1410 }
1411
1412 /* Return 1 if EXPR is the integer constant one.  */
1413
1414 int
1415 integer_onep (expr)
1416      tree expr;
1417 {
1418   STRIP_NOPS (expr);
1419
1420   return (TREE_CODE (expr) == INTEGER_CST
1421           && TREE_INT_CST_LOW (expr) == 1
1422           && TREE_INT_CST_HIGH (expr) == 0);
1423 }
1424
1425 /* Return 1 if EXPR is an integer containing all 1's
1426    in as much precision as it contains.  */
1427
1428 int
1429 integer_all_onesp (expr)
1430      tree expr;
1431 {
1432   register int prec;
1433   register int uns;
1434
1435   STRIP_NOPS (expr);
1436
1437   if (TREE_CODE (expr) != INTEGER_CST)
1438     return 0;
1439
1440   uns = TREE_UNSIGNED (TREE_TYPE (expr));
1441   if (!uns)
1442     return TREE_INT_CST_LOW (expr) == -1 && TREE_INT_CST_HIGH (expr) == -1;
1443
1444   prec = TYPE_PRECISION (TREE_TYPE (expr));
1445   if (prec >= HOST_BITS_PER_WIDE_INT)
1446     {
1447       int high_value, shift_amount;
1448
1449       shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1450
1451       if (shift_amount > HOST_BITS_PER_WIDE_INT)
1452         /* Can not handle precisions greater than twice the host int size.  */
1453         abort ();
1454       else if (shift_amount == HOST_BITS_PER_WIDE_INT)
1455         /* Shifting by the host word size is undefined according to the ANSI
1456            standard, so we must handle this as a special case.  */
1457         high_value = -1;
1458       else
1459         high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1460
1461       return TREE_INT_CST_LOW (expr) == -1
1462         && TREE_INT_CST_HIGH (expr) == high_value;
1463     }
1464   else
1465     return TREE_INT_CST_LOW (expr) == ((HOST_WIDE_INT) 1 << prec) - 1;
1466 }
1467
1468 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1469    one bit on).  */
1470
1471 int
1472 integer_pow2p (expr)
1473      tree expr;
1474 {
1475   HOST_WIDE_INT high, low;
1476
1477   STRIP_NOPS (expr);
1478
1479   if (TREE_CODE (expr) != INTEGER_CST)
1480     return 0;
1481
1482   high = TREE_INT_CST_HIGH (expr);
1483   low = TREE_INT_CST_LOW (expr);
1484
1485   if (high == 0 && low == 0)
1486     return 0;
1487
1488   return ((high == 0 && (low & (low - 1)) == 0)
1489           || (low == 0 && (high & (high - 1)) == 0));
1490 }
1491
1492 /* Return 1 if EXPR is the real constant zero.  */
1493
1494 int
1495 real_zerop (expr)
1496      tree expr;
1497 {
1498   STRIP_NOPS (expr);
1499
1500   return (TREE_CODE (expr) == REAL_CST
1501           && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0));
1502 }
1503
1504 /* Return 1 if EXPR is the real constant one.  */
1505
1506 int
1507 real_onep (expr)
1508      tree expr;
1509 {
1510   STRIP_NOPS (expr);
1511
1512   return (TREE_CODE (expr) == REAL_CST
1513           && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1));
1514 }
1515
1516 /* Return 1 if EXPR is the real constant two.  */
1517
1518 int
1519 real_twop (expr)
1520      tree expr;
1521 {
1522   STRIP_NOPS (expr);
1523
1524   return (TREE_CODE (expr) == REAL_CST
1525           && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2));
1526 }
1527
1528 /* Nonzero if EXP is a constant or a cast of a constant.  */
1529  
1530 int
1531 really_constant_p (exp)
1532      tree exp;
1533 {
1534   /* This is not quite the same as STRIP_NOPS.  It does more.  */
1535   while (TREE_CODE (exp) == NOP_EXPR
1536          || TREE_CODE (exp) == CONVERT_EXPR
1537          || TREE_CODE (exp) == NON_LVALUE_EXPR)
1538     exp = TREE_OPERAND (exp, 0);
1539   return TREE_CONSTANT (exp);
1540 }
1541 \f
1542 /* Return first list element whose TREE_VALUE is ELEM.
1543    Return 0 if ELEM is not it LIST.  */
1544
1545 tree
1546 value_member (elem, list)
1547      tree elem, list;
1548 {
1549   while (list)
1550     {
1551       if (elem == TREE_VALUE (list))
1552         return list;
1553       list = TREE_CHAIN (list);
1554     }
1555   return NULL_TREE;
1556 }
1557
1558 /* Return first list element whose TREE_PURPOSE is ELEM.
1559    Return 0 if ELEM is not it LIST.  */
1560
1561 tree
1562 purpose_member (elem, list)
1563      tree elem, list;
1564 {
1565   while (list)
1566     {
1567       if (elem == TREE_PURPOSE (list))
1568         return list;
1569       list = TREE_CHAIN (list);
1570     }
1571   return NULL_TREE;
1572 }
1573
1574 /* Return first list element whose BINFO_TYPE is ELEM.
1575    Return 0 if ELEM is not it LIST.  */
1576
1577 tree
1578 binfo_member (elem, list)
1579      tree elem, list;
1580 {
1581   while (list)
1582     {
1583       if (elem == BINFO_TYPE (list))
1584         return list;
1585       list = TREE_CHAIN (list);
1586     }
1587   return NULL_TREE;
1588 }
1589
1590 /* Return nonzero if ELEM is part of the chain CHAIN.  */
1591
1592 int
1593 chain_member (elem, chain)
1594      tree elem, chain;
1595 {
1596   while (chain)
1597     {
1598       if (elem == chain)
1599         return 1;
1600       chain = TREE_CHAIN (chain);
1601     }
1602
1603   return 0;
1604 }
1605
1606 /* Return the length of a chain of nodes chained through TREE_CHAIN.
1607    We expect a null pointer to mark the end of the chain.
1608    This is the Lisp primitive `length'.  */
1609
1610 int
1611 list_length (t)
1612      tree t;
1613 {
1614   register tree tail;
1615   register int len = 0;
1616
1617   for (tail = t; tail; tail = TREE_CHAIN (tail))
1618     len++;
1619
1620   return len;
1621 }
1622
1623 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
1624    by modifying the last node in chain 1 to point to chain 2.
1625    This is the Lisp primitive `nconc'.  */
1626
1627 tree
1628 chainon (op1, op2)
1629      tree op1, op2;
1630 {
1631
1632   if (op1)
1633     {
1634       register tree t1;
1635       register tree t2;
1636
1637       for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
1638         ;
1639       TREE_CHAIN (t1) = op2;
1640       for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
1641         if (t2 == t1)
1642           abort ();  /* Circularity created.  */
1643       return op1;
1644     }
1645   else return op2;
1646 }
1647
1648 /* Return the last node in a chain of nodes (chained through TREE_CHAIN).  */
1649
1650 tree
1651 tree_last (chain)
1652      register tree chain;
1653 {
1654   register tree next;
1655   if (chain)
1656     while (next = TREE_CHAIN (chain))
1657       chain = next;
1658   return chain;
1659 }
1660
1661 /* Reverse the order of elements in the chain T,
1662    and return the new head of the chain (old last element).  */
1663
1664 tree
1665 nreverse (t)
1666      tree t;
1667 {
1668   register tree prev = 0, decl, next;
1669   for (decl = t; decl; decl = next)
1670     {
1671       next = TREE_CHAIN (decl);
1672       TREE_CHAIN (decl) = prev;
1673       prev = decl;
1674     }
1675   return prev;
1676 }
1677
1678 /* Given a chain CHAIN of tree nodes,
1679    construct and return a list of those nodes.  */
1680
1681 tree
1682 listify (chain)
1683      tree chain;
1684 {
1685   tree result = NULL_TREE;
1686   tree in_tail = chain;
1687   tree out_tail = NULL_TREE;
1688
1689   while (in_tail)
1690     {
1691       tree next = tree_cons (NULL_TREE, in_tail, NULL_TREE);
1692       if (out_tail)
1693         TREE_CHAIN (out_tail) = next;
1694       else
1695         result = next;
1696       out_tail = next;
1697       in_tail = TREE_CHAIN (in_tail);
1698     }
1699
1700   return result;
1701 }
1702 \f
1703 /* Return a newly created TREE_LIST node whose
1704    purpose and value fields are PARM and VALUE.  */
1705
1706 tree
1707 build_tree_list (parm, value)
1708      tree parm, value;
1709 {
1710   register tree t = make_node (TREE_LIST);
1711   TREE_PURPOSE (t) = parm;
1712   TREE_VALUE (t) = value;
1713   return t;
1714 }
1715
1716 /* Similar, but build on the temp_decl_obstack.  */
1717
1718 tree
1719 build_decl_list (parm, value)
1720      tree parm, value;
1721 {
1722   register tree node;
1723   register struct obstack *ambient_obstack = current_obstack;
1724   current_obstack = &temp_decl_obstack;
1725   node = build_tree_list (parm, value);
1726   current_obstack = ambient_obstack;
1727   return node;
1728 }
1729
1730 /* Return a newly created TREE_LIST node whose
1731    purpose and value fields are PARM and VALUE
1732    and whose TREE_CHAIN is CHAIN.  */
1733
1734 tree
1735 tree_cons (purpose, value, chain)
1736      tree purpose, value, chain;
1737 {
1738 #if 0
1739   register tree node = make_node (TREE_LIST);
1740 #else
1741   register int i;
1742   register tree node = (tree) obstack_alloc (current_obstack, sizeof (struct tree_list));
1743 #ifdef GATHER_STATISTICS
1744   tree_node_counts[(int)x_kind]++;
1745   tree_node_sizes[(int)x_kind] += sizeof (struct tree_list);
1746 #endif
1747
1748   for (i = (sizeof (struct tree_common) / sizeof (int)) - 1; i >= 0; i--)
1749     ((int *) node)[i] = 0;
1750
1751   TREE_SET_CODE (node, TREE_LIST);
1752   if (current_obstack == &permanent_obstack)
1753     TREE_PERMANENT (node) = 1;
1754 #endif
1755
1756   TREE_CHAIN (node) = chain;
1757   TREE_PURPOSE (node) = purpose;
1758   TREE_VALUE (node) = value;
1759   return node;
1760 }
1761
1762 /* Similar, but build on the temp_decl_obstack.  */
1763
1764 tree
1765 decl_tree_cons (purpose, value, chain)
1766      tree purpose, value, chain;
1767 {
1768   register tree node;
1769   register struct obstack *ambient_obstack = current_obstack;
1770   current_obstack = &temp_decl_obstack;
1771   node = tree_cons (purpose, value, chain);
1772   current_obstack = ambient_obstack;
1773   return node;
1774 }
1775
1776 /* Same as `tree_cons' but make a permanent object.  */
1777
1778 tree
1779 perm_tree_cons (purpose, value, chain)
1780      tree purpose, value, chain;
1781 {
1782   register tree node;
1783   register struct obstack *ambient_obstack = current_obstack;
1784   current_obstack = &permanent_obstack;
1785
1786   node = tree_cons (purpose, value, chain);
1787   current_obstack = ambient_obstack;
1788   return node;
1789 }
1790
1791 /* Same as `tree_cons', but make this node temporary, regardless.  */
1792
1793 tree
1794 temp_tree_cons (purpose, value, chain)
1795      tree purpose, value, chain;
1796 {
1797   register tree node;
1798   register struct obstack *ambient_obstack = current_obstack;
1799   current_obstack = &temporary_obstack;
1800
1801   node = tree_cons (purpose, value, chain);
1802   current_obstack = ambient_obstack;
1803   return node;
1804 }
1805
1806 /* Same as `tree_cons', but save this node if the function's RTL is saved.  */
1807
1808 tree
1809 saveable_tree_cons (purpose, value, chain)
1810      tree purpose, value, chain;
1811 {
1812   register tree node;
1813   register struct obstack *ambient_obstack = current_obstack;
1814   current_obstack = saveable_obstack;
1815
1816   node = tree_cons (purpose, value, chain);
1817   current_obstack = ambient_obstack;
1818   return node;
1819 }
1820 \f
1821 /* Return the size nominally occupied by an object of type TYPE
1822    when it resides in memory.  The value is measured in units of bytes,
1823    and its data type is that normally used for type sizes
1824    (which is the first type created by make_signed_type or
1825    make_unsigned_type).  */
1826
1827 tree
1828 size_in_bytes (type)
1829      tree type;
1830 {
1831   tree t;
1832
1833   if (type == error_mark_node)
1834     return integer_zero_node;
1835   type = TYPE_MAIN_VARIANT (type);
1836   if (TYPE_SIZE (type) == 0)
1837     {
1838       incomplete_type_error (NULL_TREE, type);
1839       return integer_zero_node;
1840     }
1841   t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1842                   size_int (BITS_PER_UNIT));
1843   if (TREE_CODE (t) == INTEGER_CST)
1844     force_fit_type (t, 0);
1845   return t;
1846 }
1847
1848 /* Return the size of TYPE (in bytes) as an integer,
1849    or return -1 if the size can vary.  */
1850
1851 int
1852 int_size_in_bytes (type)
1853      tree type;
1854 {
1855   unsigned int size;
1856   if (type == error_mark_node)
1857     return 0;
1858   type = TYPE_MAIN_VARIANT (type);
1859   if (TYPE_SIZE (type) == 0)
1860     return -1;
1861   if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1862     return -1;
1863   if (TREE_INT_CST_HIGH (TYPE_SIZE (type)) != 0)
1864     {
1865       tree t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1866                            size_int (BITS_PER_UNIT));
1867       return TREE_INT_CST_LOW (t);
1868     }
1869   size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1870   return (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1871 }
1872 \f
1873 /* Return, as a tree node, the number of elements for TYPE (which is an
1874    ARRAY_TYPE) minus one. This counts only elements of the top array.  */
1875
1876 tree
1877 array_type_nelts (type)
1878      tree type;
1879 {
1880   tree index_type = TYPE_DOMAIN (type);
1881
1882   return (integer_zerop (TYPE_MIN_VALUE (index_type))
1883           ? TYPE_MAX_VALUE (index_type)
1884           : fold (build (MINUS_EXPR, TREE_TYPE (TYPE_MAX_VALUE (index_type)),
1885                          TYPE_MAX_VALUE (index_type),
1886                          TYPE_MIN_VALUE (index_type))));
1887 }
1888 \f
1889 /* Return nonzero if arg is static -- a reference to an object in
1890    static storage.  This is not the same as the C meaning of `static'.  */
1891
1892 int
1893 staticp (arg)
1894      tree arg;
1895 {
1896   switch (TREE_CODE (arg))
1897     {
1898     case VAR_DECL:
1899     case FUNCTION_DECL:
1900       return TREE_STATIC (arg) || DECL_EXTERNAL (arg);
1901
1902     case CONSTRUCTOR:
1903       return TREE_STATIC (arg);
1904
1905     case STRING_CST:
1906       return 1;
1907
1908     case COMPONENT_REF:
1909     case BIT_FIELD_REF:
1910       return staticp (TREE_OPERAND (arg, 0));
1911
1912     case INDIRECT_REF:
1913       return TREE_CONSTANT (TREE_OPERAND (arg, 0));
1914
1915     case ARRAY_REF:
1916       if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
1917           && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
1918         return staticp (TREE_OPERAND (arg, 0));
1919     }
1920
1921   return 0;
1922 }
1923 \f
1924 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
1925    Do this to any expression which may be used in more than one place,
1926    but must be evaluated only once.
1927
1928    Normally, expand_expr would reevaluate the expression each time.
1929    Calling save_expr produces something that is evaluated and recorded
1930    the first time expand_expr is called on it.  Subsequent calls to
1931    expand_expr just reuse the recorded value.
1932
1933    The call to expand_expr that generates code that actually computes
1934    the value is the first call *at compile time*.  Subsequent calls
1935    *at compile time* generate code to use the saved value.
1936    This produces correct result provided that *at run time* control
1937    always flows through the insns made by the first expand_expr
1938    before reaching the other places where the save_expr was evaluated.
1939    You, the caller of save_expr, must make sure this is so.
1940
1941    Constants, and certain read-only nodes, are returned with no
1942    SAVE_EXPR because that is safe.  Expressions containing placeholders
1943    are not touched; see tree.def for an explanation of what these
1944    are used for.  */
1945
1946 tree
1947 save_expr (expr)
1948      tree expr;
1949 {
1950   register tree t = fold (expr);
1951
1952   /* We don't care about whether this can be used as an lvalue in this
1953      context.  */
1954   while (TREE_CODE (t) == NON_LVALUE_EXPR)
1955     t = TREE_OPERAND (t, 0);
1956
1957   /* If the tree evaluates to a constant, then we don't want to hide that
1958      fact (i.e. this allows further folding, and direct checks for constants).
1959      However, a read-only object that has side effects cannot be bypassed.
1960      Since it is no problem to reevaluate literals, we just return the 
1961      literal node. */
1962
1963   if (TREE_CONSTANT (t) || (TREE_READONLY (t) && ! TREE_SIDE_EFFECTS (t))
1964       || TREE_CODE (t) == SAVE_EXPR)
1965     return t;
1966
1967   /* If T contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
1968      it means that the size or offset of some field of an object depends on
1969      the value within another field.
1970
1971      Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
1972      and some variable since it would then need to be both evaluated once and
1973      evaluated more than once.  Front-ends must assure this case cannot
1974      happen by surrounding any such subexpressions in their own SAVE_EXPR
1975      and forcing evaluation at the proper time.  */
1976   if (contains_placeholder_p (t))
1977     return t;
1978
1979   t = build (SAVE_EXPR, TREE_TYPE (expr), t, current_function_decl, NULL_TREE);
1980
1981   /* This expression might be placed ahead of a jump to ensure that the
1982      value was computed on both sides of the jump.  So make sure it isn't
1983      eliminated as dead.  */
1984   TREE_SIDE_EFFECTS (t) = 1;
1985   return t;
1986 }
1987 \f
1988 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
1989    or offset that depends on a field within a record.
1990
1991    Note that we only allow such expressions within simple arithmetic
1992    or a COND_EXPR.  */
1993
1994 int
1995 contains_placeholder_p (exp)
1996      tree exp;
1997 {
1998   register enum tree_code code = TREE_CODE (exp);
1999   tree inner;
2000
2001   /* If we have a WITH_RECORD_EXPR, it "cancels" any PLACEHOLDER_EXPR
2002      in it since it is supplying a value for it.  */
2003   if (code == WITH_RECORD_EXPR)
2004     return 0;
2005
2006   switch (TREE_CODE_CLASS (code))
2007     {
2008     case 'r':
2009       for (inner = TREE_OPERAND (exp, 0);
2010            TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
2011            inner = TREE_OPERAND (inner, 0))
2012         ;
2013       return TREE_CODE (inner) == PLACEHOLDER_EXPR;
2014
2015     case '1':
2016     case '2':  case '<':
2017     case 'e':
2018       switch (tree_code_length[(int) code])
2019         {
2020         case 1:
2021           return contains_placeholder_p (TREE_OPERAND (exp, 0));
2022         case 2:
2023           return (code != RTL_EXPR
2024                   && code != CONSTRUCTOR
2025                   && ! (code == SAVE_EXPR && SAVE_EXPR_RTL (exp) != 0)
2026                   && code != WITH_RECORD_EXPR
2027                   && (contains_placeholder_p (TREE_OPERAND (exp, 0))
2028                       || contains_placeholder_p (TREE_OPERAND (exp, 1))));
2029         case 3:
2030           return (code == COND_EXPR
2031                   && (contains_placeholder_p (TREE_OPERAND (exp, 0))
2032                       || contains_placeholder_p (TREE_OPERAND (exp, 1))
2033                       || contains_placeholder_p (TREE_OPERAND (exp, 2))));
2034         }
2035     }
2036
2037   return 0;
2038 }
2039 \f
2040 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
2041    return a tree with all occurrences of references to F in a
2042    PLACEHOLDER_EXPR replaced by R.   Note that we assume here that EXP
2043    contains only arithmetic expressions.  */
2044
2045 tree
2046 substitute_in_expr (exp, f, r)
2047      tree exp;
2048      tree f;
2049      tree r;
2050 {
2051   enum tree_code code = TREE_CODE (exp);
2052   tree inner;
2053
2054   switch (TREE_CODE_CLASS (code))
2055     {
2056     case 'c':
2057     case 'd':
2058       return exp;
2059
2060     case 'x':
2061       if (code == PLACEHOLDER_EXPR)
2062         return exp;
2063       break;
2064
2065     case '1':
2066     case '2':
2067     case '<':
2068     case 'e':
2069       switch (tree_code_length[(int) code])
2070         {
2071         case 1:
2072           return fold (build1 (code, TREE_TYPE (exp),
2073                                substitute_in_expr (TREE_OPERAND (exp, 0),
2074                                                    f, r)));
2075
2076         case 2:
2077           /* An RTL_EXPR cannot contain a PLACEHOLDER_EXPR; a CONSTRUCTOR
2078              could, but we don't support it.  */
2079           if (code == RTL_EXPR)
2080             return exp;
2081           else if (code == CONSTRUCTOR)
2082             abort ();
2083
2084           return fold (build (code, TREE_TYPE (exp),
2085                               substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2086                               substitute_in_expr (TREE_OPERAND (exp, 1),
2087                                                   f, r)));
2088
2089         case 3:
2090           /* It cannot be that anything inside a SAVE_EXPR contains a
2091              PLACEHOLDER_EXPR.  */
2092           if (code == SAVE_EXPR)
2093             return exp;
2094
2095           if (code != COND_EXPR)
2096             abort ();
2097
2098           return fold (build (code, TREE_TYPE (exp),
2099                               substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2100                               substitute_in_expr (TREE_OPERAND (exp, 1), f, r),
2101                               substitute_in_expr (TREE_OPERAND (exp, 2),
2102                                                   f, r)));
2103         }
2104
2105       break;
2106
2107     case 'r':
2108       switch (code)
2109         {
2110         case COMPONENT_REF:
2111           /* If this expression is getting a value from a PLACEHOLDER_EXPR
2112              and it is the right field, replace it with R.  */
2113           for (inner = TREE_OPERAND (exp, 0);
2114                TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
2115                inner = TREE_OPERAND (inner, 0))
2116             ;
2117           if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2118               && TREE_OPERAND (exp, 1) == f)
2119             return r;
2120
2121           return fold (build (code, TREE_TYPE (exp),
2122                               substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2123                               TREE_OPERAND (exp, 1)));
2124         case BIT_FIELD_REF:
2125           return fold (build (code, TREE_TYPE (exp),
2126                               substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2127                               substitute_in_expr (TREE_OPERAND (exp, 1), f, r),
2128                               substitute_in_expr (TREE_OPERAND (exp, 2), f, r)));
2129         case INDIRECT_REF:
2130         case BUFFER_REF:
2131           return fold (build1 (code, TREE_TYPE (exp),
2132                                substitute_in_expr (TREE_OPERAND (exp, 0),
2133                                                  f, r)));
2134         case OFFSET_REF:
2135           return fold (build (code, TREE_TYPE (exp),
2136                               substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2137                               substitute_in_expr (TREE_OPERAND (exp, 1), f, r)));
2138         }
2139     }
2140
2141   /* If it wasn't one of the cases we handle, give up.  */
2142
2143   abort ();
2144 }
2145 \f
2146 /* Given a type T, a FIELD_DECL F, and a replacement value R,
2147    return a new type with all size expressions that contain F
2148    updated by replacing F with R.  */
2149
2150 tree
2151 substitute_in_type (t, f, r)
2152      tree t, f, r;
2153 {
2154   switch (TREE_CODE (t))
2155     {
2156     case POINTER_TYPE:
2157     case VOID_TYPE:
2158       return t;
2159     case INTEGER_TYPE:
2160     case ENUMERAL_TYPE:
2161     case BOOLEAN_TYPE:
2162     case CHAR_TYPE:
2163       if ((TREE_CODE (TYPE_MIN_VALUE (t)) != INTEGER_CST
2164            && contains_placeholder_p (TYPE_MIN_VALUE (t)))
2165           || (TREE_CODE (TYPE_MAX_VALUE (t)) != INTEGER_CST
2166               && contains_placeholder_p (TYPE_MAX_VALUE (t))))
2167         return build_range_type (t,
2168                                  substitute_in_expr (TYPE_MIN_VALUE (t), f, r),
2169                                  substitute_in_expr (TYPE_MAX_VALUE (t), f, r));
2170       return t;
2171
2172     case REAL_TYPE:
2173       if ((TYPE_MIN_VALUE (t) != 0
2174            && TREE_CODE (TYPE_MIN_VALUE (t)) != REAL_CST
2175            && contains_placeholder_p (TYPE_MIN_VALUE (t)))
2176           || (TYPE_MAX_VALUE (t) != 0
2177               && TREE_CODE (TYPE_MAX_VALUE (t)) != REAL_CST
2178               && contains_placeholder_p (TYPE_MAX_VALUE (t))))
2179         {
2180           t = build_type_copy (t);
2181
2182           if (TYPE_MIN_VALUE (t))
2183             TYPE_MIN_VALUE (t) = substitute_in_expr (TYPE_MIN_VALUE (t), f, r);
2184           if (TYPE_MAX_VALUE (t))
2185             TYPE_MAX_VALUE (t) = substitute_in_expr (TYPE_MAX_VALUE (t), f, r);
2186         }
2187       return t;
2188
2189     case COMPLEX_TYPE:
2190       return build_complex_type (substitute_in_type (TREE_TYPE (t), f, r));
2191
2192     case OFFSET_TYPE:
2193     case METHOD_TYPE:
2194     case REFERENCE_TYPE:
2195     case FILE_TYPE:
2196     case SET_TYPE:
2197     case FUNCTION_TYPE:
2198     case LANG_TYPE:
2199       /* Don't know how to do these yet.  */
2200       abort ();
2201
2202     case ARRAY_TYPE:
2203       t = build_array_type (substitute_in_type (TREE_TYPE (t), f, r),
2204                             substitute_in_type (TYPE_DOMAIN (t), f, r));
2205       TYPE_SIZE (t) = 0;
2206       layout_type (t);
2207       return t;
2208
2209     case RECORD_TYPE:
2210     case UNION_TYPE:
2211     case QUAL_UNION_TYPE:
2212       {
2213         tree new = copy_node (t);
2214         tree field;
2215         tree last_field = 0;
2216
2217         /* Start out with no fields, make new fields, and chain them
2218            in.  */
2219
2220         TYPE_FIELDS (new) = 0;
2221         TYPE_SIZE (new) = 0;
2222
2223         for (field = TYPE_FIELDS (t); field;
2224              field = TREE_CHAIN (field))
2225           {
2226             tree new_field = copy_node (field);
2227
2228             TREE_TYPE (new_field)
2229               = substitute_in_type (TREE_TYPE (new_field), f, r);
2230
2231             /* If this is an anonymous field and the type of this field is
2232                a UNION_TYPE or RECORD_TYPE with no elements, ignore it.  If
2233                the type just has one element, treat that as the field. 
2234                But don't do this if we are processing a QUAL_UNION_TYPE.  */
2235             if (TREE_CODE (t) != QUAL_UNION_TYPE && DECL_NAME (new_field) == 0
2236                 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
2237                     || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
2238               {
2239                 if (TYPE_FIELDS (TREE_TYPE (new_field)) == 0)
2240                   continue;
2241
2242                 if (TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))) == 0)
2243                   new_field = TYPE_FIELDS (TREE_TYPE (new_field));
2244               }
2245
2246             DECL_CONTEXT (new_field) = new;
2247             DECL_SIZE (new_field) = 0;
2248
2249             if (TREE_CODE (t) == QUAL_UNION_TYPE)
2250               {
2251                 /* Do the substitution inside the qualifier and if we find
2252                    that this field will not be present, omit it.  */
2253                 DECL_QUALIFIER (new_field)
2254                   = substitute_in_expr (DECL_QUALIFIER (field), f, r);
2255                 if (integer_zerop (DECL_QUALIFIER (new_field)))
2256                   continue;
2257               }
2258
2259             if (last_field == 0)
2260               TYPE_FIELDS (new) = new_field;
2261             else
2262               TREE_CHAIN (last_field) = new_field;
2263
2264             last_field = new_field;
2265
2266             /* If this is a qualified type and this field will always be
2267                present, we are done.  */
2268             if (TREE_CODE (t) == QUAL_UNION_TYPE
2269                 && integer_onep (DECL_QUALIFIER (new_field)))
2270               break;
2271           }
2272
2273         /* If this used to be a qualified union type, but we now know what
2274            field will be present, make this a normal union.  */
2275         if (TREE_CODE (new) == QUAL_UNION_TYPE
2276             && (TYPE_FIELDS (new) == 0
2277                 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
2278           TREE_SET_CODE (new, UNION_TYPE);
2279
2280         layout_type (new);
2281         return new;
2282       }
2283     }
2284 }
2285 \f
2286 /* Stabilize a reference so that we can use it any number of times
2287    without causing its operands to be evaluated more than once.
2288    Returns the stabilized reference.  This works by means of save_expr,
2289    so see the caveats in the comments about save_expr.
2290
2291    Also allows conversion expressions whose operands are references.
2292    Any other kind of expression is returned unchanged.  */
2293
2294 tree
2295 stabilize_reference (ref)
2296      tree ref;
2297 {
2298   register tree result;
2299   register enum tree_code code = TREE_CODE (ref);
2300
2301   switch (code)
2302     {
2303     case VAR_DECL:
2304     case PARM_DECL:
2305     case RESULT_DECL:
2306       /* No action is needed in this case.  */
2307       return ref;
2308
2309     case NOP_EXPR:
2310     case CONVERT_EXPR:
2311     case FLOAT_EXPR:
2312     case FIX_TRUNC_EXPR:
2313     case FIX_FLOOR_EXPR:
2314     case FIX_ROUND_EXPR:
2315     case FIX_CEIL_EXPR:
2316       result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2317       break;
2318
2319     case INDIRECT_REF:
2320       result = build_nt (INDIRECT_REF,
2321                          stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2322       break;
2323
2324     case COMPONENT_REF:
2325       result = build_nt (COMPONENT_REF,
2326                          stabilize_reference (TREE_OPERAND (ref, 0)),
2327                          TREE_OPERAND (ref, 1));
2328       break;
2329
2330     case BIT_FIELD_REF:
2331       result = build_nt (BIT_FIELD_REF,
2332                          stabilize_reference (TREE_OPERAND (ref, 0)),
2333                          stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2334                          stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2335       break;
2336
2337     case ARRAY_REF:
2338       result = build_nt (ARRAY_REF,
2339                          stabilize_reference (TREE_OPERAND (ref, 0)),
2340                          stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2341       break;
2342
2343       /* If arg isn't a kind of lvalue we recognize, make no change.
2344          Caller should recognize the error for an invalid lvalue.  */
2345     default:
2346       return ref;
2347
2348     case ERROR_MARK:
2349       return error_mark_node;
2350     }
2351
2352   TREE_TYPE (result) = TREE_TYPE (ref);
2353   TREE_READONLY (result) = TREE_READONLY (ref);
2354   TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
2355   TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
2356   TREE_RAISES (result) = TREE_RAISES (ref);
2357
2358   return result;
2359 }
2360
2361 /* Subroutine of stabilize_reference; this is called for subtrees of
2362    references.  Any expression with side-effects must be put in a SAVE_EXPR
2363    to ensure that it is only evaluated once.
2364
2365    We don't put SAVE_EXPR nodes around everything, because assigning very
2366    simple expressions to temporaries causes us to miss good opportunities
2367    for optimizations.  Among other things, the opportunity to fold in the
2368    addition of a constant into an addressing mode often gets lost, e.g.
2369    "y[i+1] += x;".  In general, we take the approach that we should not make
2370    an assignment unless we are forced into it - i.e., that any non-side effect
2371    operator should be allowed, and that cse should take care of coalescing
2372    multiple utterances of the same expression should that prove fruitful.  */
2373
2374 static tree
2375 stabilize_reference_1 (e)
2376      tree e;
2377 {
2378   register tree result;
2379   register enum tree_code code = TREE_CODE (e);
2380
2381   /* We cannot ignore const expressions because it might be a reference
2382      to a const array but whose index contains side-effects.  But we can
2383      ignore things that are actual constant or that already have been
2384      handled by this function.  */
2385
2386   if (TREE_CONSTANT (e) || code == SAVE_EXPR)
2387     return e;
2388
2389   switch (TREE_CODE_CLASS (code))
2390     {
2391     case 'x':
2392     case 't':
2393     case 'd':
2394     case 'b':
2395     case '<':
2396     case 's':
2397     case 'e':
2398     case 'r':
2399       /* If the expression has side-effects, then encase it in a SAVE_EXPR
2400          so that it will only be evaluated once.  */
2401       /* The reference (r) and comparison (<) classes could be handled as
2402          below, but it is generally faster to only evaluate them once.  */
2403       if (TREE_SIDE_EFFECTS (e))
2404         return save_expr (e);
2405       return e;
2406
2407     case 'c':
2408       /* Constants need no processing.  In fact, we should never reach
2409          here.  */
2410       return e;
2411       
2412     case '2':
2413       /* Division is slow and tends to be compiled with jumps,
2414          especially the division by powers of 2 that is often
2415          found inside of an array reference.  So do it just once.  */
2416       if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
2417           || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
2418           || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
2419           || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
2420         return save_expr (e);
2421       /* Recursively stabilize each operand.  */
2422       result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
2423                          stabilize_reference_1 (TREE_OPERAND (e, 1)));
2424       break;
2425
2426     case '1':
2427       /* Recursively stabilize each operand.  */
2428       result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
2429       break;
2430
2431     default:
2432       abort ();
2433     }
2434   
2435   TREE_TYPE (result) = TREE_TYPE (e);
2436   TREE_READONLY (result) = TREE_READONLY (e);
2437   TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
2438   TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
2439   TREE_RAISES (result) = TREE_RAISES (e);
2440
2441   return result;
2442 }
2443 \f
2444 /* Low-level constructors for expressions.  */
2445
2446 /* Build an expression of code CODE, data type TYPE,
2447    and operands as specified by the arguments ARG1 and following arguments.
2448    Expressions and reference nodes can be created this way.
2449    Constants, decls, types and misc nodes cannot be.  */
2450
2451 tree
2452 build VPROTO((enum tree_code code, tree tt, ...))
2453 {
2454 #ifndef __STDC__
2455   enum tree_code code;
2456   tree tt;
2457 #endif
2458   va_list p;
2459   register tree t;
2460   register int length;
2461   register int i;
2462
2463   VA_START (p, tt);
2464
2465 #ifndef __STDC__
2466   code = va_arg (p, enum tree_code);
2467   tt = va_arg (p, tree);
2468 #endif
2469
2470   t = make_node (code);
2471   length = tree_code_length[(int) code];
2472   TREE_TYPE (t) = tt;
2473
2474   if (length == 2)
2475     {
2476       /* This is equivalent to the loop below, but faster.  */
2477       register tree arg0 = va_arg (p, tree);
2478       register tree arg1 = va_arg (p, tree);
2479       TREE_OPERAND (t, 0) = arg0;
2480       TREE_OPERAND (t, 1) = arg1;
2481       if ((arg0 && TREE_SIDE_EFFECTS (arg0))
2482           || (arg1 && TREE_SIDE_EFFECTS (arg1)))
2483         TREE_SIDE_EFFECTS (t) = 1;
2484       TREE_RAISES (t)
2485         = (arg0 && TREE_RAISES (arg0)) || (arg1 && TREE_RAISES (arg1));
2486     }
2487   else if (length == 1)
2488     {
2489       register tree arg0 = va_arg (p, tree);
2490
2491       /* Call build1 for this!  */
2492       if (TREE_CODE_CLASS (code) != 's')
2493         abort ();
2494       TREE_OPERAND (t, 0) = arg0;
2495       if (arg0 && TREE_SIDE_EFFECTS (arg0))
2496         TREE_SIDE_EFFECTS (t) = 1;
2497       TREE_RAISES (t) = (arg0 && TREE_RAISES (arg0));
2498     }
2499   else
2500     {
2501       for (i = 0; i < length; i++)
2502         {
2503           register tree operand = va_arg (p, tree);
2504           TREE_OPERAND (t, i) = operand;
2505           if (operand)
2506             {
2507               if (TREE_SIDE_EFFECTS (operand))
2508                 TREE_SIDE_EFFECTS (t) = 1;
2509               if (TREE_RAISES (operand))
2510                 TREE_RAISES (t) = 1;
2511             }
2512         }
2513     }
2514   va_end (p);
2515   return t;
2516 }
2517
2518 /* Same as above, but only builds for unary operators.
2519    Saves lions share of calls to `build'; cuts down use
2520    of varargs, which is expensive for RISC machines.  */
2521 tree
2522 build1 (code, type, node)
2523      enum tree_code code;
2524      tree type;
2525      tree node;
2526 {
2527   register struct obstack *obstack = current_obstack;
2528   register int i, length;
2529   register tree_node_kind kind;
2530   register tree t;
2531
2532 #ifdef GATHER_STATISTICS
2533   if (TREE_CODE_CLASS (code) == 'r')
2534     kind = r_kind;
2535   else
2536     kind = e_kind;
2537 #endif
2538
2539   obstack = expression_obstack;
2540   length = sizeof (struct tree_exp);
2541
2542   t = (tree) obstack_alloc (obstack, length);
2543
2544 #ifdef GATHER_STATISTICS
2545   tree_node_counts[(int)kind]++;
2546   tree_node_sizes[(int)kind] += length;
2547 #endif
2548
2549   for (i = (length / sizeof (int)) - 1; i >= 0; i--)
2550     ((int *) t)[i] = 0;
2551
2552   TREE_TYPE (t) = type;
2553   TREE_SET_CODE (t, code);
2554
2555   if (obstack == &permanent_obstack)
2556     TREE_PERMANENT (t) = 1;
2557
2558   TREE_OPERAND (t, 0) = node;
2559   if (node)
2560     {
2561       if (TREE_SIDE_EFFECTS (node))
2562         TREE_SIDE_EFFECTS (t) = 1;
2563       if (TREE_RAISES (node))
2564         TREE_RAISES (t) = 1;
2565     }
2566
2567   return t;
2568 }
2569
2570 /* Similar except don't specify the TREE_TYPE
2571    and leave the TREE_SIDE_EFFECTS as 0.
2572    It is permissible for arguments to be null,
2573    or even garbage if their values do not matter.  */
2574
2575 tree
2576 build_nt VPROTO((enum tree_code code, ...))
2577 {
2578 #ifndef __STDC__
2579   enum tree_code code;
2580 #endif
2581   va_list p;
2582   register tree t;
2583   register int length;
2584   register int i;
2585
2586   VA_START (p, code);
2587
2588 #ifndef __STDC__
2589   code = va_arg (p, enum tree_code);
2590 #endif
2591
2592   t = make_node (code);
2593   length = tree_code_length[(int) code];
2594
2595   for (i = 0; i < length; i++)
2596     TREE_OPERAND (t, i) = va_arg (p, tree);
2597
2598   va_end (p);
2599   return t;
2600 }
2601
2602 /* Similar to `build_nt', except we build
2603    on the temp_decl_obstack, regardless.  */
2604
2605 tree
2606 build_parse_node VPROTO((enum tree_code code, ...))
2607 {
2608 #ifndef __STDC__
2609   enum tree_code code;
2610 #endif
2611   register struct obstack *ambient_obstack = expression_obstack;
2612   va_list p;
2613   register tree t;
2614   register int length;
2615   register int i;
2616
2617   VA_START (p, code);
2618
2619 #ifndef __STDC__
2620   code = va_arg (p, enum tree_code);
2621 #endif
2622
2623   expression_obstack = &temp_decl_obstack;
2624
2625   t = make_node (code);
2626   length = tree_code_length[(int) code];
2627
2628   for (i = 0; i < length; i++)
2629     TREE_OPERAND (t, i) = va_arg (p, tree);
2630
2631   va_end (p);
2632   expression_obstack = ambient_obstack;
2633   return t;
2634 }
2635
2636 #if 0
2637 /* Commented out because this wants to be done very
2638    differently.  See cp-lex.c.  */
2639 tree
2640 build_op_identifier (op1, op2)
2641      tree op1, op2;
2642 {
2643   register tree t = make_node (OP_IDENTIFIER);
2644   TREE_PURPOSE (t) = op1;
2645   TREE_VALUE (t) = op2;
2646   return t;
2647 }
2648 #endif
2649 \f
2650 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
2651    We do NOT enter this node in any sort of symbol table.
2652
2653    layout_decl is used to set up the decl's storage layout.
2654    Other slots are initialized to 0 or null pointers.  */
2655
2656 tree
2657 build_decl (code, name, type)
2658      enum tree_code code;
2659      tree name, type;
2660 {
2661   register tree t;
2662
2663   t = make_node (code);
2664
2665 /*  if (type == error_mark_node)
2666     type = integer_type_node; */
2667 /* That is not done, deliberately, so that having error_mark_node
2668    as the type can suppress useless errors in the use of this variable.  */
2669
2670   DECL_NAME (t) = name;
2671   DECL_ASSEMBLER_NAME (t) = name;
2672   TREE_TYPE (t) = type;
2673
2674   if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
2675     layout_decl (t, 0);
2676   else if (code == FUNCTION_DECL)
2677     DECL_MODE (t) = FUNCTION_MODE;
2678
2679   return t;
2680 }
2681 \f
2682 /* BLOCK nodes are used to represent the structure of binding contours
2683    and declarations, once those contours have been exited and their contents
2684    compiled.  This information is used for outputting debugging info.  */
2685
2686 tree
2687 build_block (vars, tags, subblocks, supercontext, chain)
2688      tree vars, tags, subblocks, supercontext, chain;
2689 {
2690   register tree block = make_node (BLOCK);
2691   BLOCK_VARS (block) = vars;
2692   BLOCK_TYPE_TAGS (block) = tags;
2693   BLOCK_SUBBLOCKS (block) = subblocks;
2694   BLOCK_SUPERCONTEXT (block) = supercontext;
2695   BLOCK_CHAIN (block) = chain;
2696   return block;
2697 }
2698 \f
2699 /* Return a type like TYPE except that its TYPE_READONLY is CONSTP
2700    and its TYPE_VOLATILE is VOLATILEP.
2701
2702    Such variant types already made are recorded so that duplicates
2703    are not made.
2704
2705    A variant types should never be used as the type of an expression.
2706    Always copy the variant information into the TREE_READONLY
2707    and TREE_THIS_VOLATILE of the expression, and then give the expression
2708    as its type the "main variant", the variant whose TYPE_READONLY
2709    and TYPE_VOLATILE are zero.  Use TYPE_MAIN_VARIANT to find the
2710    main variant.  */
2711
2712 tree
2713 build_type_variant (type, constp, volatilep)
2714      tree type;
2715      int constp, volatilep;
2716 {
2717   register tree t;
2718
2719   /* Treat any nonzero argument as 1.  */
2720   constp = !!constp;
2721   volatilep = !!volatilep;
2722
2723   /* If not generating auxiliary info, search the chain of variants to see
2724      if there is already one there just like the one we need to have.  If so,
2725      use that existing one.
2726
2727      We don't do this in the case where we are generating aux info because
2728      in that case we want each typedef names to get it's own distinct type
2729      node, even if the type of this new typedef is the same as some other
2730      (existing) type.  */
2731
2732   if (!flag_gen_aux_info)
2733     for (t = TYPE_MAIN_VARIANT(type); t; t = TYPE_NEXT_VARIANT (t))
2734       if (constp == TYPE_READONLY (t) && volatilep == TYPE_VOLATILE (t))
2735         return t;
2736
2737   /* We need a new one.  */
2738
2739   t = build_type_copy (type);
2740   TYPE_READONLY (t) = constp;
2741   TYPE_VOLATILE (t) = volatilep;
2742
2743   return t;
2744 }
2745
2746 /* Give TYPE a new main variant: NEW_MAIN.
2747    This is the right thing to do only when something else
2748    about TYPE is modified in place.  */
2749
2750 tree
2751 change_main_variant (type, new_main)
2752      tree type, new_main;
2753 {
2754   tree t;
2755   tree omain = TYPE_MAIN_VARIANT (type);
2756
2757   /* Remove TYPE from the TYPE_NEXT_VARIANT chain of its main variant.  */
2758   if (TYPE_NEXT_VARIANT (omain) == type)
2759     TYPE_NEXT_VARIANT (omain) = TYPE_NEXT_VARIANT (type);
2760   else
2761     for (t = TYPE_NEXT_VARIANT (omain); t && TYPE_NEXT_VARIANT (t);
2762          t = TYPE_NEXT_VARIANT (t))
2763       if (TYPE_NEXT_VARIANT (t) == type)
2764         {
2765           TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (type);
2766           break;
2767         }
2768
2769   TYPE_MAIN_VARIANT (type) = new_main;
2770   TYPE_NEXT_VARIANT (type) = TYPE_NEXT_VARIANT (new_main);
2771   TYPE_NEXT_VARIANT (new_main) = type;
2772 }
2773
2774 /* Create a new variant of TYPE, equivalent but distinct.
2775    This is so the caller can modify it.  */
2776
2777 tree
2778 build_type_copy (type)
2779      tree type;
2780 {
2781   register tree t, m = TYPE_MAIN_VARIANT (type);
2782   register struct obstack *ambient_obstack = current_obstack;
2783
2784   current_obstack = TYPE_OBSTACK (type);
2785   t = copy_node (type);
2786   current_obstack = ambient_obstack;
2787
2788   TYPE_POINTER_TO (t) = 0;
2789   TYPE_REFERENCE_TO (t) = 0;
2790
2791   /* Add this type to the chain of variants of TYPE.  */
2792   TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
2793   TYPE_NEXT_VARIANT (m) = t;
2794
2795   return t;
2796 }
2797 \f
2798 /* Hashing of types so that we don't make duplicates.
2799    The entry point is `type_hash_canon'.  */
2800
2801 /* Each hash table slot is a bucket containing a chain
2802    of these structures.  */
2803
2804 struct type_hash
2805 {
2806   struct type_hash *next;       /* Next structure in the bucket.  */
2807   int hashcode;                 /* Hash code of this type.  */
2808   tree type;                    /* The type recorded here.  */
2809 };
2810
2811 /* Now here is the hash table.  When recording a type, it is added
2812    to the slot whose index is the hash code mod the table size.
2813    Note that the hash table is used for several kinds of types
2814    (function types, array types and array index range types, for now).
2815    While all these live in the same table, they are completely independent,
2816    and the hash code is computed differently for each of these.  */
2817
2818 #define TYPE_HASH_SIZE 59
2819 struct type_hash *type_hash_table[TYPE_HASH_SIZE];
2820
2821 /* Here is how primitive or already-canonicalized types' hash
2822    codes are made.  */
2823 #define TYPE_HASH(TYPE) ((HOST_WIDE_INT) (TYPE) & 0777777)
2824
2825 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
2826    with types in the TREE_VALUE slots), by adding the hash codes
2827    of the individual types.  */
2828
2829 int
2830 type_hash_list (list)
2831      tree list;
2832 {
2833   register int hashcode;
2834   register tree tail;
2835   for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
2836     hashcode += TYPE_HASH (TREE_VALUE (tail));
2837   return hashcode;
2838 }
2839
2840 /* Look in the type hash table for a type isomorphic to TYPE.
2841    If one is found, return it.  Otherwise return 0.  */
2842
2843 tree
2844 type_hash_lookup (hashcode, type)
2845      int hashcode;
2846      tree type;
2847 {
2848   register struct type_hash *h;
2849   for (h = type_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
2850     if (h->hashcode == hashcode
2851         && TREE_CODE (h->type) == TREE_CODE (type)
2852         && TREE_TYPE (h->type) == TREE_TYPE (type)
2853         && (TYPE_MAX_VALUE (h->type) == TYPE_MAX_VALUE (type)
2854             || tree_int_cst_equal (TYPE_MAX_VALUE (h->type),
2855                                    TYPE_MAX_VALUE (type)))
2856         && (TYPE_MIN_VALUE (h->type) == TYPE_MIN_VALUE (type)
2857             || tree_int_cst_equal (TYPE_MIN_VALUE (h->type),
2858                                    TYPE_MIN_VALUE (type)))
2859         && (TYPE_DOMAIN (h->type) == TYPE_DOMAIN (type)
2860             || (TYPE_DOMAIN (h->type)
2861                 && TREE_CODE (TYPE_DOMAIN (h->type)) == TREE_LIST
2862                 && TYPE_DOMAIN (type)
2863                 && TREE_CODE (TYPE_DOMAIN (type)) == TREE_LIST
2864                 && type_list_equal (TYPE_DOMAIN (h->type), TYPE_DOMAIN (type)))))
2865       return h->type;
2866   return 0;
2867 }
2868
2869 /* Add an entry to the type-hash-table
2870    for a type TYPE whose hash code is HASHCODE.  */
2871
2872 void
2873 type_hash_add (hashcode, type)
2874      int hashcode;
2875      tree type;
2876 {
2877   register struct type_hash *h;
2878
2879   h = (struct type_hash *) oballoc (sizeof (struct type_hash));
2880   h->hashcode = hashcode;
2881   h->type = type;
2882   h->next = type_hash_table[hashcode % TYPE_HASH_SIZE];
2883   type_hash_table[hashcode % TYPE_HASH_SIZE] = h;
2884 }
2885
2886 /* Given TYPE, and HASHCODE its hash code, return the canonical
2887    object for an identical type if one already exists.
2888    Otherwise, return TYPE, and record it as the canonical object
2889    if it is a permanent object.
2890
2891    To use this function, first create a type of the sort you want.
2892    Then compute its hash code from the fields of the type that
2893    make it different from other similar types.
2894    Then call this function and use the value.
2895    This function frees the type you pass in if it is a duplicate.  */
2896
2897 /* Set to 1 to debug without canonicalization.  Never set by program.  */
2898 int debug_no_type_hash = 0;
2899
2900 tree
2901 type_hash_canon (hashcode, type)
2902      int hashcode;
2903      tree type;
2904 {
2905   tree t1;
2906
2907   if (debug_no_type_hash)
2908     return type;
2909
2910   t1 = type_hash_lookup (hashcode, type);
2911   if (t1 != 0)
2912     {
2913       obstack_free (TYPE_OBSTACK (type), type);
2914 #ifdef GATHER_STATISTICS
2915       tree_node_counts[(int)t_kind]--;
2916       tree_node_sizes[(int)t_kind] -= sizeof (struct tree_type);
2917 #endif
2918       return t1;
2919     }
2920
2921   /* If this is a permanent type, record it for later reuse.  */
2922   if (TREE_PERMANENT (type))
2923     type_hash_add (hashcode, type);
2924
2925   return type;
2926 }
2927
2928 /* Given two lists of types
2929    (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
2930    return 1 if the lists contain the same types in the same order.
2931    Also, the TREE_PURPOSEs must match.  */
2932
2933 int
2934 type_list_equal (l1, l2)
2935      tree l1, l2;
2936 {
2937   register tree t1, t2;
2938   for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2939     {
2940       if (TREE_VALUE (t1) != TREE_VALUE (t2))
2941         return 0;
2942       if (TREE_PURPOSE (t1) != TREE_PURPOSE (t2))
2943         {
2944           int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
2945           if (cmp < 0)
2946             abort ();
2947           if (cmp == 0)
2948             return 0;
2949         }
2950     }
2951
2952   return t1 == t2;
2953 }
2954
2955 /* Nonzero if integer constants T1 and T2
2956    represent the same constant value.  */
2957
2958 int
2959 tree_int_cst_equal (t1, t2)
2960      tree t1, t2;
2961 {
2962   if (t1 == t2)
2963     return 1;
2964   if (t1 == 0 || t2 == 0)
2965     return 0;
2966   if (TREE_CODE (t1) == INTEGER_CST
2967       && TREE_CODE (t2) == INTEGER_CST
2968       && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
2969       && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
2970     return 1;
2971   return 0;
2972 }
2973
2974 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
2975    The precise way of comparison depends on their data type.  */
2976
2977 int
2978 tree_int_cst_lt (t1, t2)
2979      tree t1, t2;
2980 {
2981   if (t1 == t2)
2982     return 0;
2983
2984   if (!TREE_UNSIGNED (TREE_TYPE (t1)))
2985     return INT_CST_LT (t1, t2);
2986   return INT_CST_LT_UNSIGNED (t1, t2);
2987 }
2988
2989 /* Return an indication of the sign of the integer constant T.
2990    The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
2991    Note that -1 will never be returned it T's type is unsigned.  */
2992
2993 int
2994 tree_int_cst_sgn (t)
2995      tree t;
2996 {
2997   if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
2998     return 0;
2999   else if (TREE_UNSIGNED (TREE_TYPE (t)))
3000     return 1;
3001   else if (TREE_INT_CST_HIGH (t) < 0)
3002     return -1;
3003   else
3004     return 1;
3005 }
3006
3007 /* Compare two constructor-element-type constants.  */
3008 int
3009 simple_cst_list_equal (l1, l2)
3010      tree l1, l2;
3011 {
3012   while (l1 != NULL_TREE && l2 != NULL_TREE)
3013     {
3014       int cmp = simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2));
3015       if (cmp < 0)
3016         abort ();
3017       if (cmp == 0)
3018         return 0;
3019       l1 = TREE_CHAIN (l1);
3020       l2 = TREE_CHAIN (l2);
3021     }
3022   return (l1 == l2);
3023 }
3024
3025 /* Return truthvalue of whether T1 is the same tree structure as T2.
3026    Return 1 if they are the same.
3027    Return 0 if they are understandably different.
3028    Return -1 if either contains tree structure not understood by
3029    this function.  */
3030
3031 int
3032 simple_cst_equal (t1, t2)
3033      tree t1, t2;
3034 {
3035   register enum tree_code code1, code2;
3036   int cmp;
3037
3038   if (t1 == t2)
3039     return 1;
3040   if (t1 == 0 || t2 == 0)
3041     return 0;
3042
3043   code1 = TREE_CODE (t1);
3044   code2 = TREE_CODE (t2);
3045
3046   if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
3047     if (code2 == NOP_EXPR || code2 == CONVERT_EXPR || code2 == NON_LVALUE_EXPR)
3048       return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3049     else
3050       return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
3051   else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3052            || code2 == NON_LVALUE_EXPR)
3053     return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
3054
3055   if (code1 != code2)
3056     return 0;
3057
3058   switch (code1)
3059     {
3060     case INTEGER_CST:
3061       return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3062         && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
3063
3064     case REAL_CST:
3065       return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3066
3067     case STRING_CST:
3068       return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3069         && !bcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3070                   TREE_STRING_LENGTH (t1));
3071
3072     case CONSTRUCTOR:
3073       abort ();
3074
3075     case SAVE_EXPR:
3076       return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3077
3078     case CALL_EXPR:
3079       cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3080       if (cmp <= 0)
3081         return cmp;
3082       return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3083
3084     case TARGET_EXPR:
3085       /* Special case: if either target is an unallocated VAR_DECL,
3086          it means that it's going to be unified with whatever the
3087          TARGET_EXPR is really supposed to initialize, so treat it
3088          as being equivalent to anything.  */
3089       if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
3090            && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
3091            && DECL_RTL (TREE_OPERAND (t1, 0)) == 0)
3092           || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
3093               && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
3094               && DECL_RTL (TREE_OPERAND (t2, 0)) == 0))
3095         cmp = 1;
3096       else
3097         cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3098       if (cmp <= 0)
3099         return cmp;
3100       return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3101
3102     case WITH_CLEANUP_EXPR:
3103       cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3104       if (cmp <= 0)
3105         return cmp;
3106       return simple_cst_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t1, 2));
3107
3108     case COMPONENT_REF:
3109       if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
3110         return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3111       return 0;
3112
3113     case VAR_DECL:
3114     case PARM_DECL:
3115     case CONST_DECL:
3116     case FUNCTION_DECL:
3117       return 0;
3118     }
3119
3120   /* This general rule works for most tree codes.
3121      All exceptions should be handled above.  */
3122
3123   switch (TREE_CODE_CLASS (code1))
3124     {
3125       int i;
3126     case '1':
3127     case '2':
3128     case '<':
3129     case 'e':
3130     case 'r':
3131     case 's':
3132       cmp = 1;
3133       for (i=0; i<tree_code_length[(int) code1]; ++i)
3134         {
3135           cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
3136           if (cmp <= 0)
3137             return cmp;
3138         }
3139       return cmp;
3140     }
3141
3142   return -1;
3143 }
3144 \f
3145 /* Constructors for pointer, array and function types.
3146    (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
3147    constructed by language-dependent code, not here.)  */
3148
3149 /* Construct, lay out and return the type of pointers to TO_TYPE.
3150    If such a type has already been constructed, reuse it.  */
3151
3152 tree
3153 build_pointer_type (to_type)
3154      tree to_type;
3155 {
3156   register tree t = TYPE_POINTER_TO (to_type);
3157
3158   /* First, if we already have a type for pointers to TO_TYPE, use it.  */
3159
3160   if (t)
3161     return t;
3162
3163   /* We need a new one.  Put this in the same obstack as TO_TYPE.   */
3164   push_obstacks (TYPE_OBSTACK (to_type), TYPE_OBSTACK (to_type));
3165   t = make_node (POINTER_TYPE);
3166   pop_obstacks ();
3167
3168   TREE_TYPE (t) = to_type;
3169
3170   /* Record this type as the pointer to TO_TYPE.  */
3171   TYPE_POINTER_TO (to_type) = t;
3172
3173   /* Lay out the type.  This function has many callers that are concerned
3174      with expression-construction, and this simplifies them all.
3175      Also, it guarantees the TYPE_SIZE is in the same obstack as the type.  */
3176   layout_type (t);
3177
3178   return t;
3179 }
3180
3181 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
3182    MAXVAL should be the maximum value in the domain
3183    (one less than the length of the array).  */
3184
3185 tree
3186 build_index_type (maxval)
3187      tree maxval;
3188 {
3189   register tree itype = make_node (INTEGER_TYPE);
3190   TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
3191   TYPE_MIN_VALUE (itype) = build_int_2 (0, 0);
3192   TREE_TYPE (TYPE_MIN_VALUE (itype)) = sizetype;
3193   TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
3194   TYPE_MODE (itype) = TYPE_MODE (sizetype);
3195   TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
3196   TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
3197   if (TREE_CODE (maxval) == INTEGER_CST)
3198     {
3199       int maxint = (int) TREE_INT_CST_LOW (maxval);
3200       /* If the domain should be empty, make sure the maxval
3201          remains -1 and is not spoiled by truncation.  */
3202       if (INT_CST_LT (maxval, integer_zero_node))
3203         {
3204           TYPE_MAX_VALUE (itype) = build_int_2 (-1, -1);
3205           TREE_TYPE (TYPE_MAX_VALUE (itype)) = sizetype;
3206         }
3207       return type_hash_canon (maxint < 0 ? ~maxint : maxint, itype);
3208     }
3209   else
3210     return itype;
3211 }
3212
3213 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
3214    ENUMERAL_TYPE, BOOLEAN_TYPE, or CHAR_TYPE), with
3215    low bound LOWVAL and high bound HIGHVAL.
3216    if TYPE==NULL_TREE, sizetype is used. */
3217
3218 tree
3219 build_range_type (type, lowval, highval)
3220      tree type, lowval, highval;
3221 {
3222   register tree itype = make_node (INTEGER_TYPE);
3223   TREE_TYPE (itype) = type;
3224   if (type == NULL_TREE)
3225     type = sizetype;
3226   TYPE_PRECISION (itype) = TYPE_PRECISION (type);
3227   TYPE_MIN_VALUE (itype) = convert (type, lowval);
3228   TYPE_MAX_VALUE (itype) = convert (type, highval);
3229   TYPE_MODE (itype) = TYPE_MODE (type);
3230   TYPE_SIZE (itype) = TYPE_SIZE (type);
3231   TYPE_ALIGN (itype) = TYPE_ALIGN (type);
3232   if ((TREE_CODE (lowval) == INTEGER_CST)
3233       && (TREE_CODE (highval) == INTEGER_CST))
3234     {
3235       HOST_WIDE_INT highint = TREE_INT_CST_LOW (highval);
3236       HOST_WIDE_INT lowint = TREE_INT_CST_LOW (lowval);
3237       int maxint = (int) (highint - lowint);
3238       return type_hash_canon (maxint < 0 ? ~maxint : maxint, itype);
3239     }
3240   else
3241     return itype;
3242 }
3243
3244 /* Just like build_index_type, but takes lowval and highval instead
3245    of just highval (maxval). */
3246
3247 tree
3248 build_index_2_type (lowval,highval)
3249      tree lowval, highval;
3250 {
3251   return build_range_type (NULL_TREE, lowval, highval);
3252 }
3253
3254 /* Return nonzero iff ITYPE1 and ITYPE2 are equal (in the LISP sense).
3255    Needed because when index types are not hashed, equal index types
3256    built at different times appear distinct, even though structurally,
3257    they are not.  */
3258
3259 int
3260 index_type_equal (itype1, itype2)
3261      tree itype1, itype2;
3262 {
3263   if (TREE_CODE (itype1) != TREE_CODE (itype2))
3264     return 0;
3265   if (TREE_CODE (itype1) == INTEGER_TYPE)
3266     {
3267       if (TYPE_PRECISION (itype1) != TYPE_PRECISION (itype2)
3268           || TYPE_MODE (itype1) != TYPE_MODE (itype2)
3269           || ! simple_cst_equal (TYPE_SIZE (itype1), TYPE_SIZE (itype2))
3270           || TYPE_ALIGN (itype1) != TYPE_ALIGN (itype2))
3271         return 0;
3272       if (simple_cst_equal (TYPE_MIN_VALUE (itype1), TYPE_MIN_VALUE (itype2))
3273           && simple_cst_equal (TYPE_MAX_VALUE (itype1), TYPE_MAX_VALUE (itype2)))
3274         return 1;
3275     }
3276   return 0;
3277 }
3278
3279 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
3280    and number of elements specified by the range of values of INDEX_TYPE.
3281    If such a type has already been constructed, reuse it.  */
3282
3283 tree
3284 build_array_type (elt_type, index_type)
3285      tree elt_type, index_type;
3286 {
3287   register tree t;
3288   int hashcode;
3289
3290   if (TREE_CODE (elt_type) == FUNCTION_TYPE)
3291     {
3292       error ("arrays of functions are not meaningful");
3293       elt_type = integer_type_node;
3294     }
3295
3296   /* Make sure TYPE_POINTER_TO (elt_type) is filled in.  */
3297   build_pointer_type (elt_type);
3298
3299   /* Allocate the array after the pointer type,
3300      in case we free it in type_hash_canon.  */
3301   t = make_node (ARRAY_TYPE);
3302   TREE_TYPE (t) = elt_type;
3303   TYPE_DOMAIN (t) = index_type;
3304
3305   if (index_type == 0)
3306     {
3307       return t;
3308     }
3309
3310   hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
3311   t = type_hash_canon (hashcode, t);
3312
3313 #if 0 /* This led to crashes, because it could put a temporary node
3314          on the TYPE_NEXT_VARIANT chain of a permanent one.  */
3315   /* The main variant of an array type should always
3316      be an array whose element type is the main variant.  */
3317   if (elt_type != TYPE_MAIN_VARIANT (elt_type))
3318     change_main_variant (t, build_array_type (TYPE_MAIN_VARIANT (elt_type),
3319                                               index_type));
3320 #endif
3321
3322   if (TYPE_SIZE (t) == 0)
3323     layout_type (t);
3324   return t;
3325 }
3326
3327 /* Construct, lay out and return
3328    the type of functions returning type VALUE_TYPE
3329    given arguments of types ARG_TYPES.
3330    ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
3331    are data type nodes for the arguments of the function.
3332    If such a type has already been constructed, reuse it.  */
3333
3334 tree
3335 build_function_type (value_type, arg_types)
3336      tree value_type, arg_types;
3337 {
3338   register tree t;
3339   int hashcode;
3340
3341   if (TREE_CODE (value_type) == FUNCTION_TYPE)
3342     {
3343       error ("function return type cannot be function");
3344       value_type = integer_type_node;
3345     }
3346
3347   /* Make a node of the sort we want.  */
3348   t = make_node (FUNCTION_TYPE);
3349   TREE_TYPE (t) = value_type;
3350   TYPE_ARG_TYPES (t) = arg_types;
3351
3352   /* If we already have such a type, use the old one and free this one.  */
3353   hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
3354   t = type_hash_canon (hashcode, t);
3355
3356   if (TYPE_SIZE (t) == 0)
3357     layout_type (t);
3358   return t;
3359 }
3360
3361 /* Build the node for the type of references-to-TO_TYPE.  */
3362
3363 tree
3364 build_reference_type (to_type)
3365      tree to_type;
3366 {
3367   register tree t = TYPE_REFERENCE_TO (to_type);
3368   register struct obstack *ambient_obstack = current_obstack;
3369   register struct obstack *ambient_saveable_obstack = saveable_obstack;
3370
3371   /* First, if we already have a type for pointers to TO_TYPE, use it.  */
3372
3373   if (t)
3374     return t;
3375
3376   /* We need a new one.  If TO_TYPE is permanent, make this permanent too.  */
3377   if (TREE_PERMANENT (to_type))
3378     {
3379       current_obstack = &permanent_obstack;
3380       saveable_obstack = &permanent_obstack;
3381     }
3382
3383   t = make_node (REFERENCE_TYPE);
3384   TREE_TYPE (t) = to_type;
3385
3386   /* Record this type as the pointer to TO_TYPE.  */
3387   TYPE_REFERENCE_TO (to_type) = t;
3388
3389   layout_type (t);
3390
3391   current_obstack = ambient_obstack;
3392   saveable_obstack = ambient_saveable_obstack;
3393   return t;
3394 }
3395
3396 /* Construct, lay out and return the type of methods belonging to class
3397    BASETYPE and whose arguments and values are described by TYPE.
3398    If that type exists already, reuse it.
3399    TYPE must be a FUNCTION_TYPE node.  */
3400
3401 tree
3402 build_method_type (basetype, type)
3403      tree basetype, type;
3404 {
3405   register tree t;
3406   int hashcode;
3407
3408   /* Make a node of the sort we want.  */
3409   t = make_node (METHOD_TYPE);
3410
3411   if (TREE_CODE (type) != FUNCTION_TYPE)
3412     abort ();
3413
3414   TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3415   TREE_TYPE (t) = TREE_TYPE (type);
3416
3417   /* The actual arglist for this function includes a "hidden" argument
3418      which is "this".  Put it into the list of argument types.  */
3419
3420   TYPE_ARG_TYPES (t)
3421     = tree_cons (NULL_TREE,
3422                  build_pointer_type (basetype), TYPE_ARG_TYPES (type));
3423
3424   /* If we already have such a type, use the old one and free this one.  */
3425   hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
3426   t = type_hash_canon (hashcode, t);
3427
3428   if (TYPE_SIZE (t) == 0)
3429     layout_type (t);
3430
3431   return t;
3432 }
3433
3434 /* Construct, lay out and return the type of offsets to a value
3435    of type TYPE, within an object of type BASETYPE.
3436    If a suitable offset type exists already, reuse it.  */
3437
3438 tree
3439 build_offset_type (basetype, type)
3440      tree basetype, type;
3441 {
3442   register tree t;
3443   int hashcode;
3444
3445   /* Make a node of the sort we want.  */
3446   t = make_node (OFFSET_TYPE);
3447
3448   TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3449   TREE_TYPE (t) = type;
3450
3451   /* If we already have such a type, use the old one and free this one.  */
3452   hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
3453   t = type_hash_canon (hashcode, t);
3454
3455   if (TYPE_SIZE (t) == 0)
3456     layout_type (t);
3457
3458   return t;
3459 }
3460
3461 /* Create a complex type whose components are COMPONENT_TYPE.  */
3462
3463 tree
3464 build_complex_type (component_type)
3465      tree component_type;
3466 {
3467   register tree t;
3468   int hashcode;
3469
3470   /* Make a node of the sort we want.  */
3471   t = make_node (COMPLEX_TYPE);
3472
3473   TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
3474   TYPE_VOLATILE (t) = TYPE_VOLATILE (component_type);
3475   TYPE_READONLY (t) = TYPE_READONLY (component_type);
3476
3477   /* If we already have such a type, use the old one and free this one.  */
3478   hashcode = TYPE_HASH (component_type);
3479   t = type_hash_canon (hashcode, t);
3480
3481   if (TYPE_SIZE (t) == 0)
3482     layout_type (t);
3483
3484   return t;
3485 }
3486 \f
3487 /* Return OP, stripped of any conversions to wider types as much as is safe.
3488    Converting the value back to OP's type makes a value equivalent to OP.
3489
3490    If FOR_TYPE is nonzero, we return a value which, if converted to
3491    type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
3492
3493    If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
3494    narrowest type that can hold the value, even if they don't exactly fit.
3495    Otherwise, bit-field references are changed to a narrower type
3496    only if they can be fetched directly from memory in that type.
3497
3498    OP must have integer, real or enumeral type.  Pointers are not allowed!
3499
3500    There are some cases where the obvious value we could return
3501    would regenerate to OP if converted to OP's type, 
3502    but would not extend like OP to wider types.
3503    If FOR_TYPE indicates such extension is contemplated, we eschew such values.
3504    For example, if OP is (unsigned short)(signed char)-1,
3505    we avoid returning (signed char)-1 if FOR_TYPE is int,
3506    even though extending that to an unsigned short would regenerate OP,
3507    since the result of extending (signed char)-1 to (int)
3508    is different from (int) OP.  */
3509
3510 tree
3511 get_unwidened (op, for_type)
3512      register tree op;
3513      tree for_type;
3514 {
3515   /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension.  */
3516   /* TYPE_PRECISION is safe in place of type_precision since
3517      pointer types are not allowed.  */
3518   register tree type = TREE_TYPE (op);
3519   register unsigned final_prec
3520     = TYPE_PRECISION (for_type != 0 ? for_type : type);
3521   register int uns
3522     = (for_type != 0 && for_type != type
3523        && final_prec > TYPE_PRECISION (type)
3524        && TREE_UNSIGNED (type));
3525   register tree win = op;
3526
3527   while (TREE_CODE (op) == NOP_EXPR)
3528     {
3529       register int bitschange
3530         = TYPE_PRECISION (TREE_TYPE (op))
3531           - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
3532
3533       /* Truncations are many-one so cannot be removed.
3534          Unless we are later going to truncate down even farther.  */
3535       if (bitschange < 0
3536           && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
3537         break;
3538
3539       /* See what's inside this conversion.  If we decide to strip it,
3540          we will set WIN.  */
3541       op = TREE_OPERAND (op, 0);
3542
3543       /* If we have not stripped any zero-extensions (uns is 0),
3544          we can strip any kind of extension.
3545          If we have previously stripped a zero-extension,
3546          only zero-extensions can safely be stripped.
3547          Any extension can be stripped if the bits it would produce
3548          are all going to be discarded later by truncating to FOR_TYPE.  */
3549
3550       if (bitschange > 0)
3551         {
3552           if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
3553             win = op;
3554           /* TREE_UNSIGNED says whether this is a zero-extension.
3555              Let's avoid computing it if it does not affect WIN
3556              and if UNS will not be needed again.  */
3557           if ((uns || TREE_CODE (op) == NOP_EXPR)
3558               && TREE_UNSIGNED (TREE_TYPE (op)))
3559             {
3560               uns = 1;
3561               win = op;
3562             }
3563         }
3564     }
3565
3566   if (TREE_CODE (op) == COMPONENT_REF
3567       /* Since type_for_size always gives an integer type.  */
3568       && TREE_CODE (type) != REAL_TYPE)
3569     {
3570       unsigned innerprec = TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)));
3571       type = type_for_size (innerprec, TREE_UNSIGNED (TREE_OPERAND (op, 1)));
3572
3573       /* We can get this structure field in the narrowest type it fits in.
3574          If FOR_TYPE is 0, do this only for a field that matches the
3575          narrower type exactly and is aligned for it
3576          The resulting extension to its nominal type (a fullword type)
3577          must fit the same conditions as for other extensions.  */
3578
3579       if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
3580           && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
3581           && (! uns || final_prec <= innerprec
3582               || TREE_UNSIGNED (TREE_OPERAND (op, 1)))
3583           && type != 0)
3584         {
3585           win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
3586                        TREE_OPERAND (op, 1));
3587           TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
3588           TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
3589           TREE_RAISES (win) = TREE_RAISES (op);
3590         }
3591     }
3592   return win;
3593 }
3594 \f
3595 /* Return OP or a simpler expression for a narrower value
3596    which can be sign-extended or zero-extended to give back OP.
3597    Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
3598    or 0 if the value should be sign-extended.  */
3599
3600 tree
3601 get_narrower (op, unsignedp_ptr)
3602      register tree op;
3603      int *unsignedp_ptr;
3604 {
3605   register int uns = 0;
3606   int first = 1;
3607   register tree win = op;
3608
3609   while (TREE_CODE (op) == NOP_EXPR)
3610     {
3611       register int bitschange
3612         = TYPE_PRECISION (TREE_TYPE (op))
3613           - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
3614
3615       /* Truncations are many-one so cannot be removed.  */
3616       if (bitschange < 0)
3617         break;
3618
3619       /* See what's inside this conversion.  If we decide to strip it,
3620          we will set WIN.  */
3621       op = TREE_OPERAND (op, 0);
3622
3623       if (bitschange > 0)
3624         {
3625           /* An extension: the outermost one can be stripped,
3626              but remember whether it is zero or sign extension.  */
3627           if (first)
3628             uns = TREE_UNSIGNED (TREE_TYPE (op));
3629           /* Otherwise, if a sign extension has been stripped,
3630              only sign extensions can now be stripped;
3631              if a zero extension has been stripped, only zero-extensions.  */
3632           else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
3633             break;
3634           first = 0;
3635         }
3636       else /* bitschange == 0 */
3637         {
3638           /* A change in nominal type can always be stripped, but we must
3639              preserve the unsignedness.  */
3640           if (first)
3641             uns = TREE_UNSIGNED (TREE_TYPE (op));
3642           first = 0;
3643         }
3644
3645       win = op;
3646     }
3647
3648   if (TREE_CODE (op) == COMPONENT_REF
3649       /* Since type_for_size always gives an integer type.  */
3650       && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE)
3651     {
3652       unsigned innerprec = TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)));
3653       tree type = type_for_size (innerprec, TREE_UNSIGNED (op));
3654
3655       /* We can get this structure field in a narrower type that fits it,
3656          but the resulting extension to its nominal type (a fullword type)
3657          must satisfy the same conditions as for other extensions.
3658
3659          Do this only for fields that are aligned (not bit-fields),
3660          because when bit-field insns will be used there is no
3661          advantage in doing this.  */
3662
3663       if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
3664           && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
3665           && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
3666           && type != 0)
3667         {
3668           if (first)
3669             uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
3670           win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
3671                        TREE_OPERAND (op, 1));
3672           TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
3673           TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
3674           TREE_RAISES (win) = TREE_RAISES (op);
3675         }
3676     }
3677   *unsignedp_ptr = uns;
3678   return win;
3679 }
3680 \f
3681 /* Return the precision of a type, for arithmetic purposes.
3682    Supports all types on which arithmetic is possible
3683    (including pointer types).
3684    It's not clear yet what will be right for complex types.  */
3685
3686 int
3687 type_precision (type)
3688      register tree type;
3689 {
3690   return ((TREE_CODE (type) == INTEGER_TYPE
3691            || TREE_CODE (type) == ENUMERAL_TYPE
3692            || TREE_CODE (type) == REAL_TYPE)
3693           ? TYPE_PRECISION (type) : POINTER_SIZE);
3694 }
3695
3696 /* Nonzero if integer constant C has a value that is permissible
3697    for type TYPE (an INTEGER_TYPE).  */
3698
3699 int
3700 int_fits_type_p (c, type)
3701      tree c, type;
3702 {
3703   if (TREE_UNSIGNED (type))
3704     return (! (TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST
3705                && INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c))
3706             && ! (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
3707                   && INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type))));
3708   else
3709     return (! (TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST
3710                && INT_CST_LT (TYPE_MAX_VALUE (type), c))
3711             && ! (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
3712                   && INT_CST_LT (c, TYPE_MIN_VALUE (type))));
3713 }
3714
3715 /* Return the innermost context enclosing DECL that is
3716    a FUNCTION_DECL, or zero if none.  */
3717
3718 tree
3719 decl_function_context (decl)
3720      tree decl;
3721 {
3722   tree context;
3723
3724   if (TREE_CODE (decl) == ERROR_MARK)
3725     return 0;
3726
3727   if (TREE_CODE (decl) == SAVE_EXPR)
3728     context = SAVE_EXPR_CONTEXT (decl);
3729   else
3730     context = DECL_CONTEXT (decl);
3731
3732   while (context && TREE_CODE (context) != FUNCTION_DECL)
3733     {
3734       if (TREE_CODE (context) == RECORD_TYPE
3735           || TREE_CODE (context) == UNION_TYPE)
3736         context = TYPE_CONTEXT (context);
3737       else if (TREE_CODE (context) == TYPE_DECL)
3738         context = DECL_CONTEXT (context);
3739       else if (TREE_CODE (context) == BLOCK)
3740         context = BLOCK_SUPERCONTEXT (context);
3741       else
3742         /* Unhandled CONTEXT !?  */
3743         abort ();
3744     }
3745
3746   return context;
3747 }
3748
3749 /* Return the innermost context enclosing DECL that is
3750    a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
3751    TYPE_DECLs and FUNCTION_DECLs are transparent to this function.  */
3752
3753 tree
3754 decl_type_context (decl)
3755      tree decl;
3756 {
3757   tree context = DECL_CONTEXT (decl);
3758
3759   while (context)
3760     {
3761       if (TREE_CODE (context) == RECORD_TYPE
3762           || TREE_CODE (context) == UNION_TYPE
3763           || TREE_CODE (context) == QUAL_UNION_TYPE)
3764         return context;
3765       if (TREE_CODE (context) == TYPE_DECL
3766           || TREE_CODE (context) == FUNCTION_DECL)
3767         context = DECL_CONTEXT (context);
3768       else if (TREE_CODE (context) == BLOCK)
3769         context = BLOCK_SUPERCONTEXT (context);
3770       else
3771         /* Unhandled CONTEXT!?  */
3772         abort ();
3773     }
3774   return NULL_TREE;
3775 }
3776
3777 void
3778 print_obstack_statistics (str, o)
3779      char *str;
3780      struct obstack *o;
3781 {
3782   struct _obstack_chunk *chunk = o->chunk;
3783   int n_chunks = 0;
3784   int n_alloc = 0;
3785
3786   while (chunk)
3787     {
3788       n_chunks += 1;
3789       n_alloc += chunk->limit - &chunk->contents[0];
3790       chunk = chunk->prev;
3791     }
3792   fprintf (stderr, "obstack %s: %d bytes, %d chunks\n",
3793            str, n_alloc, n_chunks);
3794 }
3795 void
3796 dump_tree_statistics ()
3797 {
3798   int i;
3799   int total_nodes, total_bytes;
3800
3801   fprintf (stderr, "\n??? tree nodes created\n\n");
3802 #ifdef GATHER_STATISTICS
3803   fprintf (stderr, "Kind                  Nodes     Bytes\n");
3804   fprintf (stderr, "-------------------------------------\n");
3805   total_nodes = total_bytes = 0;
3806   for (i = 0; i < (int) all_kinds; i++)
3807     {
3808       fprintf (stderr, "%-20s %6d %9d\n", tree_node_kind_names[i],
3809                tree_node_counts[i], tree_node_sizes[i]);
3810       total_nodes += tree_node_counts[i];
3811       total_bytes += tree_node_sizes[i];
3812     }
3813   fprintf (stderr, "%-20s        %9d\n", "identifier names", id_string_size);
3814   fprintf (stderr, "-------------------------------------\n");
3815   fprintf (stderr, "%-20s %6d %9d\n", "Total", total_nodes, total_bytes);
3816   fprintf (stderr, "-------------------------------------\n");
3817 #else
3818   fprintf (stderr, "(No per-node statistics)\n");
3819 #endif
3820   print_lang_statistics ();
3821 }
3822 \f
3823 #define FILE_FUNCTION_PREFIX_LEN 9
3824
3825 #ifndef NO_DOLLAR_IN_LABEL
3826 #define FILE_FUNCTION_FORMAT "_GLOBAL_$D$%s"
3827 #else /* NO_DOLLAR_IN_LABEL */
3828 #ifndef NO_DOT_IN_LABEL
3829 #define FILE_FUNCTION_FORMAT "_GLOBAL_.D.%s"
3830 #else /* NO_DOT_IN_LABEL */
3831 #define FILE_FUNCTION_FORMAT "_GLOBAL__D_%s"
3832 #endif  /* NO_DOT_IN_LABEL */
3833 #endif  /* NO_DOLLAR_IN_LABEL */
3834
3835 extern char * first_global_object_name;
3836
3837 /* If KIND=='I', return a suitable global initializer (constructor) name.
3838    If KIND=='D', return a suitable global clean-up (destructor) name. */
3839
3840 tree
3841 get_file_function_name (kind)
3842      int kind;
3843 {
3844   char *buf;
3845   register char *p;
3846
3847   if (first_global_object_name)
3848     p = first_global_object_name;
3849   else if (main_input_filename)
3850     p = main_input_filename;
3851   else
3852     p = input_filename;
3853
3854   buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p));
3855
3856   /* Set up the name of the file-level functions we may need.  */
3857   /* Use a global object (which is already required to be unique over
3858      the program) rather than the file name (which imposes extra
3859      constraints).  -- Raeburn@MIT.EDU, 10 Jan 1990.  */
3860   sprintf (buf, FILE_FUNCTION_FORMAT, p);
3861
3862   /* Don't need to pull wierd characters out of global names.  */
3863   if (p != first_global_object_name)
3864     {
3865       for (p = buf+11; *p; p++)
3866         if (! ((*p >= '0' && *p <= '9')
3867 #if 0 /* we always want labels, which are valid C++ identifiers (+ `$') */
3868 #ifndef ASM_IDENTIFY_GCC        /* this is required if `.' is invalid -- k. raeburn */
3869                || *p == '.'
3870 #endif
3871 #endif
3872 #ifndef NO_DOLLAR_IN_LABEL      /* this for `$'; unlikely, but... -- kr */
3873                || *p == '$'
3874 #endif
3875 #ifndef NO_DOT_IN_LABEL         /* this for `.'; unlikely, but... */
3876                || *p == '.'
3877 #endif
3878                || (*p >= 'A' && *p <= 'Z')
3879                || (*p >= 'a' && *p <= 'z')))
3880           *p = '_';
3881     }
3882
3883   buf[FILE_FUNCTION_PREFIX_LEN] = kind;
3884
3885   return get_identifier (buf);
3886 }