OSDN Git Service

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