OSDN Git Service

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