OSDN Git Service

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