OSDN Git Service

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