(expand_inline_function): If called function calls alloca, save and
[official-gcc.git] / gcc / tree.c
blob24a815ac76e4a6774a07cb1e6b758395cf4c4973
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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.
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.
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. */
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.
28 It is intended to be language-independent, but occasionally
29 calls language-dependent routines defined (for C) in typecheck.c.
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. */
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>
48 #define obstack_chunk_alloc xmalloc
49 #define obstack_chunk_free free
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. */
55 struct obstack permanent_obstack;
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. */
63 struct obstack *function_maybepermanent_obstack;
65 /* This is the function_maybepermanent_obstack for top-level functions. */
67 struct obstack maybepermanent_obstack;
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. */
74 struct obstack *function_obstack;
76 /* This is used for reading initializers of global variables. */
78 struct obstack temporary_obstack;
80 /* The tree nodes of an expression are allocated
81 in this obstack, and all are freed at the end of the expression. */
83 struct obstack momentary_obstack;
85 /* The tree nodes of a declarator are allocated
86 in this obstack, and all are freed when the declarator
87 has been parsed. */
89 static struct obstack temp_decl_obstack;
91 /* This points at either permanent_obstack
92 or the current function_maybepermanent_obstack. */
94 struct obstack *saveable_obstack;
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. */
100 struct obstack *rtl_obstack;
102 /* This points at either permanent_obstack or the current function_obstack. */
104 struct obstack *current_obstack;
106 /* This points at either permanent_obstack or the current function_obstack
107 or momentary_obstack. */
109 struct obstack *expression_obstack;
111 /* Stack of obstack selections for push_obstacks and pop_obstacks. */
113 struct obstack_stack
115 struct obstack_stack *next;
116 struct obstack *current;
117 struct obstack *saveable;
118 struct obstack *expression;
119 struct obstack *rtl;
122 struct obstack_stack *obstack_stack;
124 /* Obstack for allocating struct obstack_stack entries. */
126 static struct obstack obstack_stack_obstack;
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;
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;
139 /* Nonzero means all ..._TYPE nodes should be allocated permanently. */
141 int all_types_permanent;
143 /* Stack of places to restore the momentary obstack back to. */
145 struct momentary_level
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;
155 struct momentary_level *momentary_stack;
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. */
161 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
163 char *standard_tree_code_type[] = {
164 #include "tree.def"
166 #undef DEFTREECODE
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. */
172 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
174 int standard_tree_code_length[] = {
175 #include "tree.def"
177 #undef DEFTREECODE
179 /* Names of tree components.
180 Used for printing out the tree and error messages. */
181 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
183 char *standard_tree_code_name[] = {
184 #include "tree.def"
186 #undef DEFTREECODE
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. */
192 char **tree_code_type;
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. */
198 int *tree_code_length;
200 /* Table indexed by tree code giving name of tree code, as a string. */
202 char **tree_code_name;
204 /* Statistics-gathering stuff. */
205 typedef enum
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;
225 int tree_node_counts[(int)all_kinds];
226 int tree_node_sizes[(int)all_kinds];
227 int id_string_size = 0;
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"
247 /* Hash table for uniquizing IDENTIFIER_NODEs by name. */
249 #define MAX_HASH_TABLE 1009
250 static tree hash_table[MAX_HASH_TABLE]; /* id hash buckets */
252 /* 0 while creating built-in identifiers. */
253 static int do_identifier_warnings;
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;
260 /* Here is how primitive or already-canonicalized types' hash
261 codes are made. */
262 #define TYPE_HASH(TYPE) ((HOST_WIDE_INT) (TYPE) & 0777777)
264 extern char *mode_name[];
266 void gcc_obstack_init ();
267 static tree stabilize_reference_1 ();
269 /* Init the principal obstacks. */
271 void
272 init_obstacks ()
274 gcc_obstack_init (&obstack_stack_obstack);
275 gcc_obstack_init (&permanent_obstack);
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);
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;
294 /* Init the hash table of identifiers. */
295 bzero ((char *) hash_table, sizeof hash_table);
298 void
299 gcc_obstack_init (obstack)
300 struct obstack *obstack;
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);
318 /* Save all variables describing the current status into the structure *P.
319 This is used before starting a nested function. */
321 void
322 save_tree_status (p, toplevel)
323 struct function *p;
324 int toplevel;
326 p->all_types_permanent = all_types_permanent;
327 p->momentary_stack = momentary_stack;
328 p->maybepermanent_firstobj = maybepermanent_firstobj;
329 p->momentary_firstobj = momentary_firstobj;
330 p->momentary_function_firstobj = momentary_function_firstobj;
331 p->function_obstack = function_obstack;
332 p->function_maybepermanent_obstack = function_maybepermanent_obstack;
333 p->current_obstack = current_obstack;
334 p->expression_obstack = expression_obstack;
335 p->saveable_obstack = saveable_obstack;
336 p->rtl_obstack = rtl_obstack;
338 if (! toplevel)
340 /* Objects that need to be saved in this function can be in the nonsaved
341 obstack of the enclosing function since they can't possibly be needed
342 once it has returned. */
343 function_maybepermanent_obstack = function_obstack;
344 maybepermanent_firstobj
345 = (char *) obstack_finish (function_maybepermanent_obstack);
348 function_obstack = (struct obstack *) xmalloc (sizeof (struct obstack));
349 gcc_obstack_init (function_obstack);
351 current_obstack = &permanent_obstack;
352 expression_obstack = &permanent_obstack;
353 rtl_obstack = saveable_obstack = &permanent_obstack;
355 momentary_firstobj = (char *) obstack_finish (&momentary_obstack);
356 momentary_function_firstobj = momentary_firstobj;
359 /* Restore all variables describing the current status from the structure *P.
360 This is used after a nested function. */
362 void
363 restore_tree_status (p, toplevel)
364 struct function *p;
365 int toplevel;
367 all_types_permanent = p->all_types_permanent;
368 momentary_stack = p->momentary_stack;
370 obstack_free (&momentary_obstack, momentary_function_firstobj);
372 if (! toplevel)
374 /* Free saveable storage used by the function just compiled and not
375 saved.
377 CAUTION: This is in function_obstack of the containing function.
378 So we must be sure that we never allocate from that obstack during
379 the compilation of a nested function if we expect it to survive
380 past the nested function's end. */
381 obstack_free (function_maybepermanent_obstack, maybepermanent_firstobj);
384 obstack_free (function_obstack, 0);
385 free (function_obstack);
387 momentary_firstobj = p->momentary_firstobj;
388 momentary_function_firstobj = p->momentary_function_firstobj;
389 maybepermanent_firstobj = p->maybepermanent_firstobj;
390 function_obstack = p->function_obstack;
391 function_maybepermanent_obstack = p->function_maybepermanent_obstack;
392 current_obstack = p->current_obstack;
393 expression_obstack = p->expression_obstack;
394 saveable_obstack = p->saveable_obstack;
395 rtl_obstack = p->rtl_obstack;
398 /* Start allocating on the temporary (per function) obstack.
399 This is done in start_function before parsing the function body,
400 and before each initialization at top level, and to go back
401 to temporary allocation after doing permanent_allocation. */
403 void
404 temporary_allocation ()
406 /* Note that function_obstack at top level points to temporary_obstack.
407 But within a nested function context, it is a separate obstack. */
408 current_obstack = function_obstack;
409 expression_obstack = function_obstack;
410 rtl_obstack = saveable_obstack = function_maybepermanent_obstack;
411 momentary_stack = 0;
414 /* Start allocating on the permanent obstack but don't
415 free the temporary data. After calling this, call
416 `permanent_allocation' to fully resume permanent allocation status. */
418 void
419 end_temporary_allocation ()
421 current_obstack = &permanent_obstack;
422 expression_obstack = &permanent_obstack;
423 rtl_obstack = saveable_obstack = &permanent_obstack;
426 /* Resume allocating on the temporary obstack, undoing
427 effects of `end_temporary_allocation'. */
429 void
430 resume_temporary_allocation ()
432 current_obstack = function_obstack;
433 expression_obstack = function_obstack;
434 rtl_obstack = saveable_obstack = function_maybepermanent_obstack;
437 /* While doing temporary allocation, switch to allocating in such a
438 way as to save all nodes if the function is inlined. Call
439 resume_temporary_allocation to go back to ordinary temporary
440 allocation. */
442 void
443 saveable_allocation ()
445 /* Note that function_obstack at top level points to temporary_obstack.
446 But within a nested function context, it is a separate obstack. */
447 expression_obstack = current_obstack = saveable_obstack;
450 /* Switch to current obstack CURRENT and maybepermanent obstack SAVEABLE,
451 recording the previously current obstacks on a stack.
452 This does not free any storage in any obstack. */
454 void
455 push_obstacks (current, saveable)
456 struct obstack *current, *saveable;
458 struct obstack_stack *p
459 = (struct obstack_stack *) obstack_alloc (&obstack_stack_obstack,
460 (sizeof (struct obstack_stack)));
462 p->current = current_obstack;
463 p->saveable = saveable_obstack;
464 p->expression = expression_obstack;
465 p->rtl = rtl_obstack;
466 p->next = obstack_stack;
467 obstack_stack = p;
469 current_obstack = current;
470 expression_obstack = current;
471 rtl_obstack = saveable_obstack = saveable;
474 /* Save the current set of obstacks, but don't change them. */
476 void
477 push_obstacks_nochange ()
479 struct obstack_stack *p
480 = (struct obstack_stack *) obstack_alloc (&obstack_stack_obstack,
481 (sizeof (struct obstack_stack)));
483 p->current = current_obstack;
484 p->saveable = saveable_obstack;
485 p->expression = expression_obstack;
486 p->rtl = rtl_obstack;
487 p->next = obstack_stack;
488 obstack_stack = p;
491 /* Pop the obstack selection stack. */
493 void
494 pop_obstacks ()
496 struct obstack_stack *p = obstack_stack;
497 obstack_stack = p->next;
499 current_obstack = p->current;
500 saveable_obstack = p->saveable;
501 expression_obstack = p->expression;
502 rtl_obstack = p->rtl;
504 obstack_free (&obstack_stack_obstack, p);
507 /* Nonzero if temporary allocation is currently in effect.
508 Zero if currently doing permanent allocation. */
511 allocation_temporary_p ()
513 return current_obstack != &permanent_obstack;
516 /* Go back to allocating on the permanent obstack
517 and free everything in the temporary obstack.
519 FUNCTION_END is true only if we have just finished compiling a function.
520 In that case, we also free preserved initial values on the momentary
521 obstack. */
523 void
524 permanent_allocation (function_end)
525 int function_end;
527 /* Free up previous temporary obstack data */
528 obstack_free (&temporary_obstack, temporary_firstobj);
529 if (function_end)
531 obstack_free (&momentary_obstack, momentary_function_firstobj);
532 momentary_firstobj = momentary_function_firstobj;
534 else
535 obstack_free (&momentary_obstack, momentary_firstobj);
536 obstack_free (&maybepermanent_obstack, maybepermanent_firstobj);
537 obstack_free (&temp_decl_obstack, temp_decl_firstobj);
539 current_obstack = &permanent_obstack;
540 expression_obstack = &permanent_obstack;
541 rtl_obstack = saveable_obstack = &permanent_obstack;
544 /* Save permanently everything on the maybepermanent_obstack. */
546 void
547 preserve_data ()
549 maybepermanent_firstobj
550 = (char *) obstack_alloc (function_maybepermanent_obstack, 0);
553 void
554 preserve_initializer ()
556 struct momentary_level *tem;
557 char *old_momentary;
559 temporary_firstobj
560 = (char *) obstack_alloc (&temporary_obstack, 0);
561 maybepermanent_firstobj
562 = (char *) obstack_alloc (function_maybepermanent_obstack, 0);
564 old_momentary = momentary_firstobj;
565 momentary_firstobj
566 = (char *) obstack_alloc (&momentary_obstack, 0);
567 if (momentary_firstobj != old_momentary)
568 for (tem = momentary_stack; tem; tem = tem->prev)
569 tem->base = momentary_firstobj;
572 /* Start allocating new rtl in current_obstack.
573 Use resume_temporary_allocation
574 to go back to allocating rtl in saveable_obstack. */
576 void
577 rtl_in_current_obstack ()
579 rtl_obstack = current_obstack;
582 /* Start allocating rtl from saveable_obstack. Intended to be used after
583 a call to push_obstacks_nochange. */
585 void
586 rtl_in_saveable_obstack ()
588 rtl_obstack = saveable_obstack;
591 /* Allocate SIZE bytes in the current obstack
592 and return a pointer to them.
593 In practice the current obstack is always the temporary one. */
595 char *
596 oballoc (size)
597 int size;
599 return (char *) obstack_alloc (current_obstack, size);
602 /* Free the object PTR in the current obstack
603 as well as everything allocated since PTR.
604 In practice the current obstack is always the temporary one. */
606 void
607 obfree (ptr)
608 char *ptr;
610 obstack_free (current_obstack, ptr);
613 /* Allocate SIZE bytes in the permanent obstack
614 and return a pointer to them. */
616 char *
617 permalloc (size)
618 int size;
620 return (char *) obstack_alloc (&permanent_obstack, size);
623 /* Allocate NELEM items of SIZE bytes in the permanent obstack
624 and return a pointer to them. The storage is cleared before
625 returning the value. */
627 char *
628 perm_calloc (nelem, size)
629 int nelem;
630 long size;
632 char *rval = (char *) obstack_alloc (&permanent_obstack, nelem * size);
633 bzero (rval, nelem * size);
634 return rval;
637 /* Allocate SIZE bytes in the saveable obstack
638 and return a pointer to them. */
640 char *
641 savealloc (size)
642 int size;
644 return (char *) obstack_alloc (saveable_obstack, size);
647 /* Print out which obstack an object is in. */
649 void
650 print_obstack_name (object, file, prefix)
651 char *object;
652 FILE *file;
653 char *prefix;
655 struct obstack *obstack = NULL;
656 char *obstack_name = NULL;
657 struct function *p;
659 for (p = outer_function_chain; p; p = p->next)
661 if (_obstack_allocated_p (p->function_obstack, object))
663 obstack = p->function_obstack;
664 obstack_name = "containing function obstack";
666 if (_obstack_allocated_p (p->function_maybepermanent_obstack, object))
668 obstack = p->function_maybepermanent_obstack;
669 obstack_name = "containing function maybepermanent obstack";
673 if (_obstack_allocated_p (&obstack_stack_obstack, object))
675 obstack = &obstack_stack_obstack;
676 obstack_name = "obstack_stack_obstack";
678 else if (_obstack_allocated_p (function_obstack, object))
680 obstack = function_obstack;
681 obstack_name = "function obstack";
683 else if (_obstack_allocated_p (&permanent_obstack, object))
685 obstack = &permanent_obstack;
686 obstack_name = "permanent_obstack";
688 else if (_obstack_allocated_p (&momentary_obstack, object))
690 obstack = &momentary_obstack;
691 obstack_name = "momentary_obstack";
693 else if (_obstack_allocated_p (function_maybepermanent_obstack, object))
695 obstack = function_maybepermanent_obstack;
696 obstack_name = "function maybepermanent obstack";
698 else if (_obstack_allocated_p (&temp_decl_obstack, object))
700 obstack = &temp_decl_obstack;
701 obstack_name = "temp_decl_obstack";
704 /* Check to see if the object is in the free area of the obstack. */
705 if (obstack != NULL)
707 if (object >= obstack->next_free
708 && object < obstack->chunk_limit)
709 fprintf (file, "%s in free portion of obstack %s",
710 prefix, obstack_name);
711 else
712 fprintf (file, "%s allocated from %s", prefix, obstack_name);
714 else
715 fprintf (file, "%s not allocated from any obstack", prefix);
718 void
719 debug_obstack (object)
720 char *object;
722 print_obstack_name (object, stderr, "object");
723 fprintf (stderr, ".\n");
726 /* Return 1 if OBJ is in the permanent obstack.
727 This is slow, and should be used only for debugging.
728 Use TREE_PERMANENT for other purposes. */
731 object_permanent_p (obj)
732 tree obj;
734 return _obstack_allocated_p (&permanent_obstack, obj);
737 /* Start a level of momentary allocation.
738 In C, each compound statement has its own level
739 and that level is freed at the end of each statement.
740 All expression nodes are allocated in the momentary allocation level. */
742 void
743 push_momentary ()
745 struct momentary_level *tem
746 = (struct momentary_level *) obstack_alloc (&momentary_obstack,
747 sizeof (struct momentary_level));
748 tem->prev = momentary_stack;
749 tem->base = (char *) obstack_base (&momentary_obstack);
750 tem->obstack = expression_obstack;
751 momentary_stack = tem;
752 expression_obstack = &momentary_obstack;
755 /* Free all the storage in the current momentary-allocation level.
756 In C, this happens at the end of each statement. */
758 void
759 clear_momentary ()
761 obstack_free (&momentary_obstack, momentary_stack->base);
764 /* Discard a level of momentary allocation.
765 In C, this happens at the end of each compound statement.
766 Restore the status of expression node allocation
767 that was in effect before this level was created. */
769 void
770 pop_momentary ()
772 struct momentary_level *tem = momentary_stack;
773 momentary_stack = tem->prev;
774 expression_obstack = tem->obstack;
775 /* We can't free TEM from the momentary_obstack, because there might
776 be objects above it which have been saved. We can free back to the
777 stack of the level we are popping off though. */
778 obstack_free (&momentary_obstack, tem->base);
781 /* Pop back to the previous level of momentary allocation,
782 but don't free any momentary data just yet. */
784 void
785 pop_momentary_nofree ()
787 struct momentary_level *tem = momentary_stack;
788 momentary_stack = tem->prev;
789 expression_obstack = tem->obstack;
792 /* Call when starting to parse a declaration:
793 make expressions in the declaration last the length of the function.
794 Returns an argument that should be passed to resume_momentary later. */
797 suspend_momentary ()
799 register int tem = expression_obstack == &momentary_obstack;
800 expression_obstack = saveable_obstack;
801 return tem;
804 /* Call when finished parsing a declaration:
805 restore the treatment of node-allocation that was
806 in effect before the suspension.
807 YES should be the value previously returned by suspend_momentary. */
809 void
810 resume_momentary (yes)
811 int yes;
813 if (yes)
814 expression_obstack = &momentary_obstack;
817 /* Init the tables indexed by tree code.
818 Note that languages can add to these tables to define their own codes. */
820 void
821 init_tree_codes ()
823 tree_code_type = (char **) xmalloc (sizeof (standard_tree_code_type));
824 tree_code_length = (int *) xmalloc (sizeof (standard_tree_code_length));
825 tree_code_name = (char **) xmalloc (sizeof (standard_tree_code_name));
826 bcopy ((char *) standard_tree_code_type, (char *) tree_code_type,
827 sizeof (standard_tree_code_type));
828 bcopy ((char *) standard_tree_code_length, (char *) tree_code_length,
829 sizeof (standard_tree_code_length));
830 bcopy ((char *) standard_tree_code_name, (char *) tree_code_name,
831 sizeof (standard_tree_code_name));
834 /* Return a newly allocated node of code CODE.
835 Initialize the node's unique id and its TREE_PERMANENT flag.
836 For decl and type nodes, some other fields are initialized.
837 The rest of the node is initialized to zero.
839 Achoo! I got a code in the node. */
841 tree
842 make_node (code)
843 enum tree_code code;
845 register tree t;
846 register int type = TREE_CODE_CLASS (code);
847 register int length;
848 register struct obstack *obstack = current_obstack;
849 register int i;
850 register tree_node_kind kind;
852 switch (type)
854 case 'd': /* A decl node */
855 #ifdef GATHER_STATISTICS
856 kind = d_kind;
857 #endif
858 length = sizeof (struct tree_decl);
859 /* All decls in an inline function need to be saved. */
860 if (obstack != &permanent_obstack)
861 obstack = saveable_obstack;
863 /* PARM_DECLs go on the context of the parent. If this is a nested
864 function, then we must allocate the PARM_DECL on the parent's
865 obstack, so that they will live to the end of the parent's
866 closing brace. This is neccesary in case we try to inline the
867 function into its parent.
869 PARM_DECLs of top-level functions do not have this problem. However,
870 we allocate them where we put the FUNCTION_DECL for languauges such as
871 Ada that need to consult some flags in the PARM_DECLs of the function
872 when calling it.
874 See comment in restore_tree_status for why we can't put this
875 in function_obstack. */
876 if (code == PARM_DECL && obstack != &permanent_obstack)
878 tree context = 0;
879 if (current_function_decl)
880 context = decl_function_context (current_function_decl);
882 if (context)
883 obstack
884 = find_function_data (context)->function_maybepermanent_obstack;
886 break;
888 case 't': /* a type node */
889 #ifdef GATHER_STATISTICS
890 kind = t_kind;
891 #endif
892 length = sizeof (struct tree_type);
893 /* All data types are put where we can preserve them if nec. */
894 if (obstack != &permanent_obstack)
895 obstack = all_types_permanent ? &permanent_obstack : saveable_obstack;
896 break;
898 case 'b': /* a lexical block */
899 #ifdef GATHER_STATISTICS
900 kind = b_kind;
901 #endif
902 length = sizeof (struct tree_block);
903 /* All BLOCK nodes are put where we can preserve them if nec. */
904 if (obstack != &permanent_obstack)
905 obstack = saveable_obstack;
906 break;
908 case 's': /* an expression with side effects */
909 #ifdef GATHER_STATISTICS
910 kind = s_kind;
911 goto usual_kind;
912 #endif
913 case 'r': /* a reference */
914 #ifdef GATHER_STATISTICS
915 kind = r_kind;
916 goto usual_kind;
917 #endif
918 case 'e': /* an expression */
919 case '<': /* a comparison expression */
920 case '1': /* a unary arithmetic expression */
921 case '2': /* a binary arithmetic expression */
922 #ifdef GATHER_STATISTICS
923 kind = e_kind;
924 usual_kind:
925 #endif
926 obstack = expression_obstack;
927 /* All BIND_EXPR nodes are put where we can preserve them if nec. */
928 if (code == BIND_EXPR && obstack != &permanent_obstack)
929 obstack = saveable_obstack;
930 length = sizeof (struct tree_exp)
931 + (tree_code_length[(int) code] - 1) * sizeof (char *);
932 break;
934 case 'c': /* a constant */
935 #ifdef GATHER_STATISTICS
936 kind = c_kind;
937 #endif
938 obstack = expression_obstack;
940 /* We can't use tree_code_length for INTEGER_CST, since the number of
941 words is machine-dependent due to varying length of HOST_WIDE_INT,
942 which might be wider than a pointer (e.g., long long). Similarly
943 for REAL_CST, since the number of words is machine-dependent due
944 to varying size and alignment of `double'. */
946 if (code == INTEGER_CST)
947 length = sizeof (struct tree_int_cst);
948 else if (code == REAL_CST)
949 length = sizeof (struct tree_real_cst);
950 else
951 length = sizeof (struct tree_common)
952 + tree_code_length[(int) code] * sizeof (char *);
953 break;
955 case 'x': /* something random, like an identifier. */
956 #ifdef GATHER_STATISTICS
957 if (code == IDENTIFIER_NODE)
958 kind = id_kind;
959 else if (code == OP_IDENTIFIER)
960 kind = op_id_kind;
961 else if (code == TREE_VEC)
962 kind = vec_kind;
963 else
964 kind = x_kind;
965 #endif
966 length = sizeof (struct tree_common)
967 + tree_code_length[(int) code] * sizeof (char *);
968 /* Identifier nodes are always permanent since they are
969 unique in a compiler run. */
970 if (code == IDENTIFIER_NODE) obstack = &permanent_obstack;
971 break;
973 default:
974 abort ();
977 t = (tree) obstack_alloc (obstack, length);
979 #ifdef GATHER_STATISTICS
980 tree_node_counts[(int)kind]++;
981 tree_node_sizes[(int)kind] += length;
982 #endif
984 /* Clear a word at a time. */
985 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
986 ((int *) t)[i] = 0;
987 /* Clear any extra bytes. */
988 for (i = length / sizeof (int) * sizeof (int); i < length; i++)
989 ((char *) t)[i] = 0;
991 TREE_SET_CODE (t, code);
992 if (obstack == &permanent_obstack)
993 TREE_PERMANENT (t) = 1;
995 switch (type)
997 case 's':
998 TREE_SIDE_EFFECTS (t) = 1;
999 TREE_TYPE (t) = void_type_node;
1000 break;
1002 case 'd':
1003 if (code != FUNCTION_DECL)
1004 DECL_ALIGN (t) = 1;
1005 DECL_IN_SYSTEM_HEADER (t)
1006 = in_system_header && (obstack == &permanent_obstack);
1007 DECL_SOURCE_LINE (t) = lineno;
1008 DECL_SOURCE_FILE (t) = (input_filename) ? input_filename : "<built-in>";
1009 DECL_UID (t) = next_decl_uid++;
1010 break;
1012 case 't':
1013 TYPE_UID (t) = next_type_uid++;
1014 TYPE_ALIGN (t) = 1;
1015 TYPE_MAIN_VARIANT (t) = t;
1016 TYPE_OBSTACK (t) = obstack;
1017 TYPE_ATTRIBUTES (t) = NULL_TREE;
1018 #ifdef SET_DEFAULT_TYPE_ATTRIBUTES
1019 SET_DEFAULT_TYPE_ATTRIBUTES (t);
1020 #endif
1021 break;
1023 case 'c':
1024 TREE_CONSTANT (t) = 1;
1025 break;
1028 return t;
1031 /* Return a new node with the same contents as NODE
1032 except that its TREE_CHAIN is zero and it has a fresh uid. */
1034 tree
1035 copy_node (node)
1036 tree node;
1038 register tree t;
1039 register enum tree_code code = TREE_CODE (node);
1040 register int length;
1041 register int i;
1043 switch (TREE_CODE_CLASS (code))
1045 case 'd': /* A decl node */
1046 length = sizeof (struct tree_decl);
1047 break;
1049 case 't': /* a type node */
1050 length = sizeof (struct tree_type);
1051 break;
1053 case 'b': /* a lexical block node */
1054 length = sizeof (struct tree_block);
1055 break;
1057 case 'r': /* a reference */
1058 case 'e': /* an expression */
1059 case 's': /* an expression with side effects */
1060 case '<': /* a comparison expression */
1061 case '1': /* a unary arithmetic expression */
1062 case '2': /* a binary arithmetic expression */
1063 length = sizeof (struct tree_exp)
1064 + (tree_code_length[(int) code] - 1) * sizeof (char *);
1065 break;
1067 case 'c': /* a constant */
1068 /* We can't use tree_code_length for INTEGER_CST, since the number of
1069 words is machine-dependent due to varying length of HOST_WIDE_INT,
1070 which might be wider than a pointer (e.g., long long). Similarly
1071 for REAL_CST, since the number of words is machine-dependent due
1072 to varying size and alignment of `double'. */
1073 if (code == INTEGER_CST)
1075 length = sizeof (struct tree_int_cst);
1076 break;
1078 else if (code == REAL_CST)
1080 length = sizeof (struct tree_real_cst);
1081 break;
1084 case 'x': /* something random, like an identifier. */
1085 length = sizeof (struct tree_common)
1086 + tree_code_length[(int) code] * sizeof (char *);
1087 if (code == TREE_VEC)
1088 length += (TREE_VEC_LENGTH (node) - 1) * sizeof (char *);
1091 t = (tree) obstack_alloc (current_obstack, length);
1093 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
1094 ((int *) t)[i] = ((int *) node)[i];
1095 /* Clear any extra bytes. */
1096 for (i = length / sizeof (int) * sizeof (int); i < length; i++)
1097 ((char *) t)[i] = ((char *) node)[i];
1099 TREE_CHAIN (t) = 0;
1101 if (TREE_CODE_CLASS (code) == 'd')
1102 DECL_UID (t) = next_decl_uid++;
1103 else if (TREE_CODE_CLASS (code) == 't')
1105 TYPE_UID (t) = next_type_uid++;
1106 TYPE_OBSTACK (t) = current_obstack;
1109 TREE_PERMANENT (t) = (current_obstack == &permanent_obstack);
1111 return t;
1114 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1115 For example, this can copy a list made of TREE_LIST nodes. */
1117 tree
1118 copy_list (list)
1119 tree list;
1121 tree head;
1122 register tree prev, next;
1124 if (list == 0)
1125 return 0;
1127 head = prev = copy_node (list);
1128 next = TREE_CHAIN (list);
1129 while (next)
1131 TREE_CHAIN (prev) = copy_node (next);
1132 prev = TREE_CHAIN (prev);
1133 next = TREE_CHAIN (next);
1135 return head;
1138 #define HASHBITS 30
1140 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
1141 If an identifier with that name has previously been referred to,
1142 the same node is returned this time. */
1144 tree
1145 get_identifier (text)
1146 register char *text;
1148 register int hi;
1149 register int i;
1150 register tree idp;
1151 register int len, hash_len;
1153 /* Compute length of text in len. */
1154 for (len = 0; text[len]; len++);
1156 /* Decide how much of that length to hash on */
1157 hash_len = len;
1158 if (warn_id_clash && len > id_clash_len)
1159 hash_len = id_clash_len;
1161 /* Compute hash code */
1162 hi = hash_len * 613 + (unsigned)text[0];
1163 for (i = 1; i < hash_len; i += 2)
1164 hi = ((hi * 613) + (unsigned)(text[i]));
1166 hi &= (1 << HASHBITS) - 1;
1167 hi %= MAX_HASH_TABLE;
1169 /* Search table for identifier */
1170 for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
1171 if (IDENTIFIER_LENGTH (idp) == len
1172 && IDENTIFIER_POINTER (idp)[0] == text[0]
1173 && !bcmp (IDENTIFIER_POINTER (idp), text, len))
1174 return idp; /* <-- return if found */
1176 /* Not found; optionally warn about a similar identifier */
1177 if (warn_id_clash && do_identifier_warnings && len >= id_clash_len)
1178 for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
1179 if (!strncmp (IDENTIFIER_POINTER (idp), text, id_clash_len))
1181 warning ("`%s' and `%s' identical in first %d characters",
1182 IDENTIFIER_POINTER (idp), text, id_clash_len);
1183 break;
1186 if (tree_code_length[(int) IDENTIFIER_NODE] < 0)
1187 abort (); /* set_identifier_size hasn't been called. */
1189 /* Not found, create one, add to chain */
1190 idp = make_node (IDENTIFIER_NODE);
1191 IDENTIFIER_LENGTH (idp) = len;
1192 #ifdef GATHER_STATISTICS
1193 id_string_size += len;
1194 #endif
1196 IDENTIFIER_POINTER (idp) = obstack_copy0 (&permanent_obstack, text, len);
1198 TREE_CHAIN (idp) = hash_table[hi];
1199 hash_table[hi] = idp;
1200 return idp; /* <-- return if created */
1203 /* Enable warnings on similar identifiers (if requested).
1204 Done after the built-in identifiers are created. */
1206 void
1207 start_identifier_warnings ()
1209 do_identifier_warnings = 1;
1212 /* Record the size of an identifier node for the language in use.
1213 SIZE is the total size in bytes.
1214 This is called by the language-specific files. This must be
1215 called before allocating any identifiers. */
1217 void
1218 set_identifier_size (size)
1219 int size;
1221 tree_code_length[(int) IDENTIFIER_NODE]
1222 = (size - sizeof (struct tree_common)) / sizeof (tree);
1225 /* Return a newly constructed INTEGER_CST node whose constant value
1226 is specified by the two ints LOW and HI.
1227 The TREE_TYPE is set to `int'.
1229 This function should be used via the `build_int_2' macro. */
1231 tree
1232 build_int_2_wide (low, hi)
1233 HOST_WIDE_INT low, hi;
1235 register tree t = make_node (INTEGER_CST);
1236 TREE_INT_CST_LOW (t) = low;
1237 TREE_INT_CST_HIGH (t) = hi;
1238 TREE_TYPE (t) = integer_type_node;
1239 return t;
1242 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1244 tree
1245 build_real (type, d)
1246 tree type;
1247 REAL_VALUE_TYPE d;
1249 tree v;
1250 int overflow = 0;
1252 /* Check for valid float value for this type on this target machine;
1253 if not, can print error message and store a valid value in D. */
1254 #ifdef CHECK_FLOAT_VALUE
1255 CHECK_FLOAT_VALUE (TYPE_MODE (type), d, overflow);
1256 #endif
1258 v = make_node (REAL_CST);
1259 TREE_TYPE (v) = type;
1260 TREE_REAL_CST (v) = d;
1261 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
1262 return v;
1265 /* Return a new REAL_CST node whose type is TYPE
1266 and whose value is the integer value of the INTEGER_CST node I. */
1268 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1270 REAL_VALUE_TYPE
1271 real_value_from_int_cst (i)
1272 tree i;
1274 REAL_VALUE_TYPE d;
1275 REAL_VALUE_TYPE e;
1276 /* Some 386 compilers mishandle unsigned int to float conversions,
1277 so introduce a temporary variable E to avoid those bugs. */
1279 #ifdef REAL_ARITHMETIC
1280 if (! TREE_UNSIGNED (TREE_TYPE (i)))
1281 REAL_VALUE_FROM_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i));
1282 else
1283 REAL_VALUE_FROM_UNSIGNED_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i));
1284 #else /* not REAL_ARITHMETIC */
1285 if (TREE_INT_CST_HIGH (i) < 0 && ! TREE_UNSIGNED (TREE_TYPE (i)))
1287 d = (double) (~ TREE_INT_CST_HIGH (i));
1288 e = ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
1289 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
1290 d *= e;
1291 e = (double) (unsigned HOST_WIDE_INT) (~ TREE_INT_CST_LOW (i));
1292 d += e;
1293 d = (- d - 1.0);
1295 else
1297 d = (double) (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (i);
1298 e = ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
1299 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
1300 d *= e;
1301 e = (double) (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (i);
1302 d += e;
1304 #endif /* not REAL_ARITHMETIC */
1305 return d;
1308 /* This function can't be implemented if we can't do arithmetic
1309 on the float representation. */
1311 tree
1312 build_real_from_int_cst (type, i)
1313 tree type;
1314 tree i;
1316 tree v;
1317 int overflow = TREE_OVERFLOW (i);
1318 REAL_VALUE_TYPE d;
1319 jmp_buf float_error;
1321 v = make_node (REAL_CST);
1322 TREE_TYPE (v) = type;
1324 if (setjmp (float_error))
1326 d = dconst0;
1327 overflow = 1;
1328 goto got_it;
1331 set_float_handler (float_error);
1333 d = REAL_VALUE_TRUNCATE (TYPE_MODE (type), real_value_from_int_cst (i));
1335 /* Check for valid float value for this type on this target machine. */
1337 got_it:
1338 set_float_handler (NULL_PTR);
1340 #ifdef CHECK_FLOAT_VALUE
1341 CHECK_FLOAT_VALUE (TYPE_MODE (type), d, overflow);
1342 #endif
1344 TREE_REAL_CST (v) = d;
1345 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
1346 return v;
1349 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1351 /* Return a newly constructed STRING_CST node whose value is
1352 the LEN characters at STR.
1353 The TREE_TYPE is not initialized. */
1355 tree
1356 build_string (len, str)
1357 int len;
1358 char *str;
1360 /* Put the string in saveable_obstack since it will be placed in the RTL
1361 for an "asm" statement and will also be kept around a while if
1362 deferring constant output in varasm.c. */
1364 register tree s = make_node (STRING_CST);
1365 TREE_STRING_LENGTH (s) = len;
1366 TREE_STRING_POINTER (s) = obstack_copy0 (saveable_obstack, str, len);
1367 return s;
1370 /* Return a newly constructed COMPLEX_CST node whose value is
1371 specified by the real and imaginary parts REAL and IMAG.
1372 Both REAL and IMAG should be constant nodes.
1373 The TREE_TYPE is not initialized. */
1375 tree
1376 build_complex (real, imag)
1377 tree real, imag;
1379 register tree t = make_node (COMPLEX_CST);
1381 TREE_REALPART (t) = real;
1382 TREE_IMAGPART (t) = imag;
1383 TREE_TYPE (t) = build_complex_type (TREE_TYPE (real));
1384 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1385 TREE_CONSTANT_OVERFLOW (t)
1386 = TREE_CONSTANT_OVERFLOW (real) | TREE_CONSTANT_OVERFLOW (imag);
1387 return t;
1390 /* Build a newly constructed TREE_VEC node of length LEN. */
1391 tree
1392 make_tree_vec (len)
1393 int len;
1395 register tree t;
1396 register int length = (len-1) * sizeof (tree) + sizeof (struct tree_vec);
1397 register struct obstack *obstack = current_obstack;
1398 register int i;
1400 #ifdef GATHER_STATISTICS
1401 tree_node_counts[(int)vec_kind]++;
1402 tree_node_sizes[(int)vec_kind] += length;
1403 #endif
1405 t = (tree) obstack_alloc (obstack, length);
1407 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
1408 ((int *) t)[i] = 0;
1410 TREE_SET_CODE (t, TREE_VEC);
1411 TREE_VEC_LENGTH (t) = len;
1412 if (obstack == &permanent_obstack)
1413 TREE_PERMANENT (t) = 1;
1415 return t;
1418 /* Return 1 if EXPR is the integer constant zero or a complex constant
1419 of zero. */
1422 integer_zerop (expr)
1423 tree expr;
1425 STRIP_NOPS (expr);
1427 return ((TREE_CODE (expr) == INTEGER_CST
1428 && TREE_INT_CST_LOW (expr) == 0
1429 && TREE_INT_CST_HIGH (expr) == 0)
1430 || (TREE_CODE (expr) == COMPLEX_CST
1431 && integer_zerop (TREE_REALPART (expr))
1432 && integer_zerop (TREE_IMAGPART (expr))));
1435 /* Return 1 if EXPR is the integer constant one or the corresponding
1436 complex constant. */
1439 integer_onep (expr)
1440 tree expr;
1442 STRIP_NOPS (expr);
1444 return ((TREE_CODE (expr) == INTEGER_CST
1445 && TREE_INT_CST_LOW (expr) == 1
1446 && TREE_INT_CST_HIGH (expr) == 0)
1447 || (TREE_CODE (expr) == COMPLEX_CST
1448 && integer_onep (TREE_REALPART (expr))
1449 && integer_zerop (TREE_IMAGPART (expr))));
1452 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1453 it contains. Likewise for the corresponding complex constant. */
1456 integer_all_onesp (expr)
1457 tree expr;
1459 register int prec;
1460 register int uns;
1462 STRIP_NOPS (expr);
1464 if (TREE_CODE (expr) == COMPLEX_CST
1465 && integer_all_onesp (TREE_REALPART (expr))
1466 && integer_zerop (TREE_IMAGPART (expr)))
1467 return 1;
1469 else if (TREE_CODE (expr) != INTEGER_CST)
1470 return 0;
1472 uns = TREE_UNSIGNED (TREE_TYPE (expr));
1473 if (!uns)
1474 return TREE_INT_CST_LOW (expr) == -1 && TREE_INT_CST_HIGH (expr) == -1;
1476 prec = TYPE_PRECISION (TREE_TYPE (expr));
1477 if (prec >= HOST_BITS_PER_WIDE_INT)
1479 int high_value, shift_amount;
1481 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1483 if (shift_amount > HOST_BITS_PER_WIDE_INT)
1484 /* Can not handle precisions greater than twice the host int size. */
1485 abort ();
1486 else if (shift_amount == HOST_BITS_PER_WIDE_INT)
1487 /* Shifting by the host word size is undefined according to the ANSI
1488 standard, so we must handle this as a special case. */
1489 high_value = -1;
1490 else
1491 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1493 return TREE_INT_CST_LOW (expr) == -1
1494 && TREE_INT_CST_HIGH (expr) == high_value;
1496 else
1497 return TREE_INT_CST_LOW (expr) == ((HOST_WIDE_INT) 1 << prec) - 1;
1500 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1501 one bit on). */
1504 integer_pow2p (expr)
1505 tree expr;
1507 HOST_WIDE_INT high, low;
1509 STRIP_NOPS (expr);
1511 if (TREE_CODE (expr) == COMPLEX_CST
1512 && integer_pow2p (TREE_REALPART (expr))
1513 && integer_zerop (TREE_IMAGPART (expr)))
1514 return 1;
1516 if (TREE_CODE (expr) != INTEGER_CST)
1517 return 0;
1519 high = TREE_INT_CST_HIGH (expr);
1520 low = TREE_INT_CST_LOW (expr);
1522 if (high == 0 && low == 0)
1523 return 0;
1525 return ((high == 0 && (low & (low - 1)) == 0)
1526 || (low == 0 && (high & (high - 1)) == 0));
1529 /* Return 1 if EXPR is the real constant zero. */
1532 real_zerop (expr)
1533 tree expr;
1535 STRIP_NOPS (expr);
1537 return ((TREE_CODE (expr) == REAL_CST
1538 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0))
1539 || (TREE_CODE (expr) == COMPLEX_CST
1540 && real_zerop (TREE_REALPART (expr))
1541 && real_zerop (TREE_IMAGPART (expr))));
1544 /* Return 1 if EXPR is the real constant one in real or complex form. */
1547 real_onep (expr)
1548 tree expr;
1550 STRIP_NOPS (expr);
1552 return ((TREE_CODE (expr) == REAL_CST
1553 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1))
1554 || (TREE_CODE (expr) == COMPLEX_CST
1555 && real_onep (TREE_REALPART (expr))
1556 && real_zerop (TREE_IMAGPART (expr))));
1559 /* Return 1 if EXPR is the real constant two. */
1562 real_twop (expr)
1563 tree expr;
1565 STRIP_NOPS (expr);
1567 return ((TREE_CODE (expr) == REAL_CST
1568 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2))
1569 || (TREE_CODE (expr) == COMPLEX_CST
1570 && real_twop (TREE_REALPART (expr))
1571 && real_zerop (TREE_IMAGPART (expr))));
1574 /* Nonzero if EXP is a constant or a cast of a constant. */
1577 really_constant_p (exp)
1578 tree exp;
1580 /* This is not quite the same as STRIP_NOPS. It does more. */
1581 while (TREE_CODE (exp) == NOP_EXPR
1582 || TREE_CODE (exp) == CONVERT_EXPR
1583 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1584 exp = TREE_OPERAND (exp, 0);
1585 return TREE_CONSTANT (exp);
1588 /* Return first list element whose TREE_VALUE is ELEM.
1589 Return 0 if ELEM is not it LIST. */
1591 tree
1592 value_member (elem, list)
1593 tree elem, list;
1595 while (list)
1597 if (elem == TREE_VALUE (list))
1598 return list;
1599 list = TREE_CHAIN (list);
1601 return NULL_TREE;
1604 /* Return first list element whose TREE_PURPOSE is ELEM.
1605 Return 0 if ELEM is not it LIST. */
1607 tree
1608 purpose_member (elem, list)
1609 tree elem, list;
1611 while (list)
1613 if (elem == TREE_PURPOSE (list))
1614 return list;
1615 list = TREE_CHAIN (list);
1617 return NULL_TREE;
1620 /* Return first list element whose BINFO_TYPE is ELEM.
1621 Return 0 if ELEM is not it LIST. */
1623 tree
1624 binfo_member (elem, list)
1625 tree elem, list;
1627 while (list)
1629 if (elem == BINFO_TYPE (list))
1630 return list;
1631 list = TREE_CHAIN (list);
1633 return NULL_TREE;
1636 /* Return nonzero if ELEM is part of the chain CHAIN. */
1639 chain_member (elem, chain)
1640 tree elem, chain;
1642 while (chain)
1644 if (elem == chain)
1645 return 1;
1646 chain = TREE_CHAIN (chain);
1649 return 0;
1652 /* Return the length of a chain of nodes chained through TREE_CHAIN.
1653 We expect a null pointer to mark the end of the chain.
1654 This is the Lisp primitive `length'. */
1657 list_length (t)
1658 tree t;
1660 register tree tail;
1661 register int len = 0;
1663 for (tail = t; tail; tail = TREE_CHAIN (tail))
1664 len++;
1666 return len;
1669 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
1670 by modifying the last node in chain 1 to point to chain 2.
1671 This is the Lisp primitive `nconc'. */
1673 tree
1674 chainon (op1, op2)
1675 tree op1, op2;
1678 if (op1)
1680 register tree t1;
1681 register tree t2;
1683 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
1685 TREE_CHAIN (t1) = op2;
1686 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
1687 if (t2 == t1)
1688 abort (); /* Circularity created. */
1689 return op1;
1691 else return op2;
1694 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
1696 tree
1697 tree_last (chain)
1698 register tree chain;
1700 register tree next;
1701 if (chain)
1702 while (next = TREE_CHAIN (chain))
1703 chain = next;
1704 return chain;
1707 /* Reverse the order of elements in the chain T,
1708 and return the new head of the chain (old last element). */
1710 tree
1711 nreverse (t)
1712 tree t;
1714 register tree prev = 0, decl, next;
1715 for (decl = t; decl; decl = next)
1717 next = TREE_CHAIN (decl);
1718 TREE_CHAIN (decl) = prev;
1719 prev = decl;
1721 return prev;
1724 /* Given a chain CHAIN of tree nodes,
1725 construct and return a list of those nodes. */
1727 tree
1728 listify (chain)
1729 tree chain;
1731 tree result = NULL_TREE;
1732 tree in_tail = chain;
1733 tree out_tail = NULL_TREE;
1735 while (in_tail)
1737 tree next = tree_cons (NULL_TREE, in_tail, NULL_TREE);
1738 if (out_tail)
1739 TREE_CHAIN (out_tail) = next;
1740 else
1741 result = next;
1742 out_tail = next;
1743 in_tail = TREE_CHAIN (in_tail);
1746 return result;
1749 /* Return a newly created TREE_LIST node whose
1750 purpose and value fields are PARM and VALUE. */
1752 tree
1753 build_tree_list (parm, value)
1754 tree parm, value;
1756 register tree t = make_node (TREE_LIST);
1757 TREE_PURPOSE (t) = parm;
1758 TREE_VALUE (t) = value;
1759 return t;
1762 /* Similar, but build on the temp_decl_obstack. */
1764 tree
1765 build_decl_list (parm, value)
1766 tree parm, value;
1768 register tree node;
1769 register struct obstack *ambient_obstack = current_obstack;
1770 current_obstack = &temp_decl_obstack;
1771 node = build_tree_list (parm, value);
1772 current_obstack = ambient_obstack;
1773 return node;
1776 /* Return a newly created TREE_LIST node whose
1777 purpose and value fields are PARM and VALUE
1778 and whose TREE_CHAIN is CHAIN. */
1780 tree
1781 tree_cons (purpose, value, chain)
1782 tree purpose, value, chain;
1784 #if 0
1785 register tree node = make_node (TREE_LIST);
1786 #else
1787 register int i;
1788 register tree node = (tree) obstack_alloc (current_obstack, sizeof (struct tree_list));
1789 #ifdef GATHER_STATISTICS
1790 tree_node_counts[(int)x_kind]++;
1791 tree_node_sizes[(int)x_kind] += sizeof (struct tree_list);
1792 #endif
1794 for (i = (sizeof (struct tree_common) / sizeof (int)) - 1; i >= 0; i--)
1795 ((int *) node)[i] = 0;
1797 TREE_SET_CODE (node, TREE_LIST);
1798 if (current_obstack == &permanent_obstack)
1799 TREE_PERMANENT (node) = 1;
1800 #endif
1802 TREE_CHAIN (node) = chain;
1803 TREE_PURPOSE (node) = purpose;
1804 TREE_VALUE (node) = value;
1805 return node;
1808 /* Similar, but build on the temp_decl_obstack. */
1810 tree
1811 decl_tree_cons (purpose, value, chain)
1812 tree purpose, value, chain;
1814 register tree node;
1815 register struct obstack *ambient_obstack = current_obstack;
1816 current_obstack = &temp_decl_obstack;
1817 node = tree_cons (purpose, value, chain);
1818 current_obstack = ambient_obstack;
1819 return node;
1822 /* Same as `tree_cons' but make a permanent object. */
1824 tree
1825 perm_tree_cons (purpose, value, chain)
1826 tree purpose, value, chain;
1828 register tree node;
1829 register struct obstack *ambient_obstack = current_obstack;
1830 current_obstack = &permanent_obstack;
1832 node = tree_cons (purpose, value, chain);
1833 current_obstack = ambient_obstack;
1834 return node;
1837 /* Same as `tree_cons', but make this node temporary, regardless. */
1839 tree
1840 temp_tree_cons (purpose, value, chain)
1841 tree purpose, value, chain;
1843 register tree node;
1844 register struct obstack *ambient_obstack = current_obstack;
1845 current_obstack = &temporary_obstack;
1847 node = tree_cons (purpose, value, chain);
1848 current_obstack = ambient_obstack;
1849 return node;
1852 /* Same as `tree_cons', but save this node if the function's RTL is saved. */
1854 tree
1855 saveable_tree_cons (purpose, value, chain)
1856 tree purpose, value, chain;
1858 register tree node;
1859 register struct obstack *ambient_obstack = current_obstack;
1860 current_obstack = saveable_obstack;
1862 node = tree_cons (purpose, value, chain);
1863 current_obstack = ambient_obstack;
1864 return node;
1867 /* Return the size nominally occupied by an object of type TYPE
1868 when it resides in memory. The value is measured in units of bytes,
1869 and its data type is that normally used for type sizes
1870 (which is the first type created by make_signed_type or
1871 make_unsigned_type). */
1873 tree
1874 size_in_bytes (type)
1875 tree type;
1877 tree t;
1879 if (type == error_mark_node)
1880 return integer_zero_node;
1881 type = TYPE_MAIN_VARIANT (type);
1882 if (TYPE_SIZE (type) == 0)
1884 incomplete_type_error (NULL_TREE, type);
1885 return integer_zero_node;
1887 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1888 size_int (BITS_PER_UNIT));
1889 if (TREE_CODE (t) == INTEGER_CST)
1890 force_fit_type (t, 0);
1891 return t;
1894 /* Return the size of TYPE (in bytes) as an integer,
1895 or return -1 if the size can vary. */
1898 int_size_in_bytes (type)
1899 tree type;
1901 unsigned int size;
1902 if (type == error_mark_node)
1903 return 0;
1904 type = TYPE_MAIN_VARIANT (type);
1905 if (TYPE_SIZE (type) == 0)
1906 return -1;
1907 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1908 return -1;
1909 if (TREE_INT_CST_HIGH (TYPE_SIZE (type)) != 0)
1911 tree t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1912 size_int (BITS_PER_UNIT));
1913 return TREE_INT_CST_LOW (t);
1915 size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1916 return (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1919 /* Return, as a tree node, the number of elements for TYPE (which is an
1920 ARRAY_TYPE) minus one. This counts only elements of the top array. */
1922 tree
1923 array_type_nelts (type)
1924 tree type;
1926 tree index_type = TYPE_DOMAIN (type);
1928 return (integer_zerop (TYPE_MIN_VALUE (index_type))
1929 ? TYPE_MAX_VALUE (index_type)
1930 : fold (build (MINUS_EXPR, TREE_TYPE (TYPE_MAX_VALUE (index_type)),
1931 TYPE_MAX_VALUE (index_type),
1932 TYPE_MIN_VALUE (index_type))));
1935 /* Return nonzero if arg is static -- a reference to an object in
1936 static storage. This is not the same as the C meaning of `static'. */
1939 staticp (arg)
1940 tree arg;
1942 switch (TREE_CODE (arg))
1944 case FUNCTION_DECL:
1945 /* Nested functions aren't static, since taking their address
1946 involves a trampoline. */
1947 return decl_function_context (arg) == 0;
1948 case VAR_DECL:
1949 return TREE_STATIC (arg) || DECL_EXTERNAL (arg);
1951 case CONSTRUCTOR:
1952 return TREE_STATIC (arg);
1954 case STRING_CST:
1955 return 1;
1957 case COMPONENT_REF:
1958 case BIT_FIELD_REF:
1959 return staticp (TREE_OPERAND (arg, 0));
1961 case INDIRECT_REF:
1962 return TREE_CONSTANT (TREE_OPERAND (arg, 0));
1964 case ARRAY_REF:
1965 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
1966 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
1967 return staticp (TREE_OPERAND (arg, 0));
1970 return 0;
1973 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
1974 Do this to any expression which may be used in more than one place,
1975 but must be evaluated only once.
1977 Normally, expand_expr would reevaluate the expression each time.
1978 Calling save_expr produces something that is evaluated and recorded
1979 the first time expand_expr is called on it. Subsequent calls to
1980 expand_expr just reuse the recorded value.
1982 The call to expand_expr that generates code that actually computes
1983 the value is the first call *at compile time*. Subsequent calls
1984 *at compile time* generate code to use the saved value.
1985 This produces correct result provided that *at run time* control
1986 always flows through the insns made by the first expand_expr
1987 before reaching the other places where the save_expr was evaluated.
1988 You, the caller of save_expr, must make sure this is so.
1990 Constants, and certain read-only nodes, are returned with no
1991 SAVE_EXPR because that is safe. Expressions containing placeholders
1992 are not touched; see tree.def for an explanation of what these
1993 are used for. */
1995 tree
1996 save_expr (expr)
1997 tree expr;
1999 register tree t = fold (expr);
2001 /* We don't care about whether this can be used as an lvalue in this
2002 context. */
2003 while (TREE_CODE (t) == NON_LVALUE_EXPR)
2004 t = TREE_OPERAND (t, 0);
2006 /* If the tree evaluates to a constant, then we don't want to hide that
2007 fact (i.e. this allows further folding, and direct checks for constants).
2008 However, a read-only object that has side effects cannot be bypassed.
2009 Since it is no problem to reevaluate literals, we just return the
2010 literal node. */
2012 if (TREE_CONSTANT (t) || (TREE_READONLY (t) && ! TREE_SIDE_EFFECTS (t))
2013 || TREE_CODE (t) == SAVE_EXPR)
2014 return t;
2016 /* If T contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2017 it means that the size or offset of some field of an object depends on
2018 the value within another field.
2020 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2021 and some variable since it would then need to be both evaluated once and
2022 evaluated more than once. Front-ends must assure this case cannot
2023 happen by surrounding any such subexpressions in their own SAVE_EXPR
2024 and forcing evaluation at the proper time. */
2025 if (contains_placeholder_p (t))
2026 return t;
2028 t = build (SAVE_EXPR, TREE_TYPE (expr), t, current_function_decl, NULL_TREE);
2030 /* This expression might be placed ahead of a jump to ensure that the
2031 value was computed on both sides of the jump. So make sure it isn't
2032 eliminated as dead. */
2033 TREE_SIDE_EFFECTS (t) = 1;
2034 return t;
2037 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
2038 or offset that depends on a field within a record.
2040 Note that we only allow such expressions within simple arithmetic
2041 or a COND_EXPR. */
2044 contains_placeholder_p (exp)
2045 tree exp;
2047 register enum tree_code code = TREE_CODE (exp);
2048 tree inner;
2050 /* If we have a WITH_RECORD_EXPR, it "cancels" any PLACEHOLDER_EXPR
2051 in it since it is supplying a value for it. */
2052 if (code == WITH_RECORD_EXPR)
2053 return 0;
2055 switch (TREE_CODE_CLASS (code))
2057 case 'r':
2058 for (inner = TREE_OPERAND (exp, 0);
2059 TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
2060 inner = TREE_OPERAND (inner, 0))
2062 return TREE_CODE (inner) == PLACEHOLDER_EXPR;
2064 case '1':
2065 case '2': case '<':
2066 case 'e':
2067 switch (tree_code_length[(int) code])
2069 case 1:
2070 return contains_placeholder_p (TREE_OPERAND (exp, 0));
2071 case 2:
2072 return (code != RTL_EXPR
2073 && code != CONSTRUCTOR
2074 && ! (code == SAVE_EXPR && SAVE_EXPR_RTL (exp) != 0)
2075 && code != WITH_RECORD_EXPR
2076 && (contains_placeholder_p (TREE_OPERAND (exp, 0))
2077 || contains_placeholder_p (TREE_OPERAND (exp, 1))));
2078 case 3:
2079 return (code == COND_EXPR
2080 && (contains_placeholder_p (TREE_OPERAND (exp, 0))
2081 || contains_placeholder_p (TREE_OPERAND (exp, 1))
2082 || contains_placeholder_p (TREE_OPERAND (exp, 2))));
2086 return 0;
2089 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
2090 return a tree with all occurrences of references to F in a
2091 PLACEHOLDER_EXPR replaced by R. Note that we assume here that EXP
2092 contains only arithmetic expressions. */
2094 tree
2095 substitute_in_expr (exp, f, r)
2096 tree exp;
2097 tree f;
2098 tree r;
2100 enum tree_code code = TREE_CODE (exp);
2101 tree inner;
2103 switch (TREE_CODE_CLASS (code))
2105 case 'c':
2106 case 'd':
2107 return exp;
2109 case 'x':
2110 if (code == PLACEHOLDER_EXPR)
2111 return exp;
2112 break;
2114 case '1':
2115 case '2':
2116 case '<':
2117 case 'e':
2118 switch (tree_code_length[(int) code])
2120 case 1:
2121 return fold (build1 (code, TREE_TYPE (exp),
2122 substitute_in_expr (TREE_OPERAND (exp, 0),
2123 f, r)));
2125 case 2:
2126 /* An RTL_EXPR cannot contain a PLACEHOLDER_EXPR; a CONSTRUCTOR
2127 could, but we don't support it. */
2128 if (code == RTL_EXPR)
2129 return exp;
2130 else if (code == CONSTRUCTOR)
2131 abort ();
2133 return fold (build (code, TREE_TYPE (exp),
2134 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2135 substitute_in_expr (TREE_OPERAND (exp, 1),
2136 f, r)));
2138 case 3:
2139 /* It cannot be that anything inside a SAVE_EXPR contains a
2140 PLACEHOLDER_EXPR. */
2141 if (code == SAVE_EXPR)
2142 return exp;
2144 if (code != COND_EXPR)
2145 abort ();
2147 return fold (build (code, TREE_TYPE (exp),
2148 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2149 substitute_in_expr (TREE_OPERAND (exp, 1), f, r),
2150 substitute_in_expr (TREE_OPERAND (exp, 2),
2151 f, r)));
2154 break;
2156 case 'r':
2157 switch (code)
2159 case COMPONENT_REF:
2160 /* If this expression is getting a value from a PLACEHOLDER_EXPR
2161 and it is the right field, replace it with R. */
2162 for (inner = TREE_OPERAND (exp, 0);
2163 TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
2164 inner = TREE_OPERAND (inner, 0))
2166 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2167 && TREE_OPERAND (exp, 1) == f)
2168 return r;
2170 return fold (build (code, TREE_TYPE (exp),
2171 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2172 TREE_OPERAND (exp, 1)));
2173 case BIT_FIELD_REF:
2174 return fold (build (code, TREE_TYPE (exp),
2175 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2176 substitute_in_expr (TREE_OPERAND (exp, 1), f, r),
2177 substitute_in_expr (TREE_OPERAND (exp, 2), f, r)));
2178 case INDIRECT_REF:
2179 case BUFFER_REF:
2180 return fold (build1 (code, TREE_TYPE (exp),
2181 substitute_in_expr (TREE_OPERAND (exp, 0),
2182 f, r)));
2183 case OFFSET_REF:
2184 return fold (build (code, TREE_TYPE (exp),
2185 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2186 substitute_in_expr (TREE_OPERAND (exp, 1), f, r)));
2190 /* If it wasn't one of the cases we handle, give up. */
2192 abort ();
2195 /* Given a type T, a FIELD_DECL F, and a replacement value R,
2196 return a new type with all size expressions that contain F
2197 updated by replacing F with R. */
2199 tree
2200 substitute_in_type (t, f, r)
2201 tree t, f, r;
2203 switch (TREE_CODE (t))
2205 case POINTER_TYPE:
2206 case VOID_TYPE:
2207 return t;
2208 case INTEGER_TYPE:
2209 case ENUMERAL_TYPE:
2210 case BOOLEAN_TYPE:
2211 case CHAR_TYPE:
2212 if ((TREE_CODE (TYPE_MIN_VALUE (t)) != INTEGER_CST
2213 && contains_placeholder_p (TYPE_MIN_VALUE (t)))
2214 || (TREE_CODE (TYPE_MAX_VALUE (t)) != INTEGER_CST
2215 && contains_placeholder_p (TYPE_MAX_VALUE (t))))
2216 return build_range_type (t,
2217 substitute_in_expr (TYPE_MIN_VALUE (t), f, r),
2218 substitute_in_expr (TYPE_MAX_VALUE (t), f, r));
2219 return t;
2221 case REAL_TYPE:
2222 if ((TYPE_MIN_VALUE (t) != 0
2223 && TREE_CODE (TYPE_MIN_VALUE (t)) != REAL_CST
2224 && contains_placeholder_p (TYPE_MIN_VALUE (t)))
2225 || (TYPE_MAX_VALUE (t) != 0
2226 && TREE_CODE (TYPE_MAX_VALUE (t)) != REAL_CST
2227 && contains_placeholder_p (TYPE_MAX_VALUE (t))))
2229 t = build_type_copy (t);
2231 if (TYPE_MIN_VALUE (t))
2232 TYPE_MIN_VALUE (t) = substitute_in_expr (TYPE_MIN_VALUE (t), f, r);
2233 if (TYPE_MAX_VALUE (t))
2234 TYPE_MAX_VALUE (t) = substitute_in_expr (TYPE_MAX_VALUE (t), f, r);
2236 return t;
2238 case COMPLEX_TYPE:
2239 return build_complex_type (substitute_in_type (TREE_TYPE (t), f, r));
2241 case OFFSET_TYPE:
2242 case METHOD_TYPE:
2243 case REFERENCE_TYPE:
2244 case FILE_TYPE:
2245 case SET_TYPE:
2246 case FUNCTION_TYPE:
2247 case LANG_TYPE:
2248 /* Don't know how to do these yet. */
2249 abort ();
2251 case ARRAY_TYPE:
2252 t = build_array_type (substitute_in_type (TREE_TYPE (t), f, r),
2253 substitute_in_type (TYPE_DOMAIN (t), f, r));
2254 TYPE_SIZE (t) = 0;
2255 layout_type (t);
2256 return t;
2258 case RECORD_TYPE:
2259 case UNION_TYPE:
2260 case QUAL_UNION_TYPE:
2262 tree new = copy_node (t);
2263 tree field;
2264 tree last_field = 0;
2266 /* Start out with no fields, make new fields, and chain them
2267 in. */
2269 TYPE_FIELDS (new) = 0;
2270 TYPE_SIZE (new) = 0;
2272 for (field = TYPE_FIELDS (t); field;
2273 field = TREE_CHAIN (field))
2275 tree new_field = copy_node (field);
2277 TREE_TYPE (new_field)
2278 = substitute_in_type (TREE_TYPE (new_field), f, r);
2280 /* If this is an anonymous field and the type of this field is
2281 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
2282 the type just has one element, treat that as the field.
2283 But don't do this if we are processing a QUAL_UNION_TYPE. */
2284 if (TREE_CODE (t) != QUAL_UNION_TYPE && DECL_NAME (new_field) == 0
2285 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
2286 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
2288 if (TYPE_FIELDS (TREE_TYPE (new_field)) == 0)
2289 continue;
2291 if (TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))) == 0)
2292 new_field = TYPE_FIELDS (TREE_TYPE (new_field));
2295 DECL_CONTEXT (new_field) = new;
2296 DECL_SIZE (new_field) = 0;
2298 if (TREE_CODE (t) == QUAL_UNION_TYPE)
2300 /* Do the substitution inside the qualifier and if we find
2301 that this field will not be present, omit it. */
2302 DECL_QUALIFIER (new_field)
2303 = substitute_in_expr (DECL_QUALIFIER (field), f, r);
2304 if (integer_zerop (DECL_QUALIFIER (new_field)))
2305 continue;
2308 if (last_field == 0)
2309 TYPE_FIELDS (new) = new_field;
2310 else
2311 TREE_CHAIN (last_field) = new_field;
2313 last_field = new_field;
2315 /* If this is a qualified type and this field will always be
2316 present, we are done. */
2317 if (TREE_CODE (t) == QUAL_UNION_TYPE
2318 && integer_onep (DECL_QUALIFIER (new_field)))
2319 break;
2322 /* If this used to be a qualified union type, but we now know what
2323 field will be present, make this a normal union. */
2324 if (TREE_CODE (new) == QUAL_UNION_TYPE
2325 && (TYPE_FIELDS (new) == 0
2326 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
2327 TREE_SET_CODE (new, UNION_TYPE);
2329 layout_type (new);
2330 return new;
2335 /* Stabilize a reference so that we can use it any number of times
2336 without causing its operands to be evaluated more than once.
2337 Returns the stabilized reference. This works by means of save_expr,
2338 so see the caveats in the comments about save_expr.
2340 Also allows conversion expressions whose operands are references.
2341 Any other kind of expression is returned unchanged. */
2343 tree
2344 stabilize_reference (ref)
2345 tree ref;
2347 register tree result;
2348 register enum tree_code code = TREE_CODE (ref);
2350 switch (code)
2352 case VAR_DECL:
2353 case PARM_DECL:
2354 case RESULT_DECL:
2355 /* No action is needed in this case. */
2356 return ref;
2358 case NOP_EXPR:
2359 case CONVERT_EXPR:
2360 case FLOAT_EXPR:
2361 case FIX_TRUNC_EXPR:
2362 case FIX_FLOOR_EXPR:
2363 case FIX_ROUND_EXPR:
2364 case FIX_CEIL_EXPR:
2365 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2366 break;
2368 case INDIRECT_REF:
2369 result = build_nt (INDIRECT_REF,
2370 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2371 break;
2373 case COMPONENT_REF:
2374 result = build_nt (COMPONENT_REF,
2375 stabilize_reference (TREE_OPERAND (ref, 0)),
2376 TREE_OPERAND (ref, 1));
2377 break;
2379 case BIT_FIELD_REF:
2380 result = build_nt (BIT_FIELD_REF,
2381 stabilize_reference (TREE_OPERAND (ref, 0)),
2382 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2383 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2384 break;
2386 case ARRAY_REF:
2387 result = build_nt (ARRAY_REF,
2388 stabilize_reference (TREE_OPERAND (ref, 0)),
2389 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2390 break;
2392 case COMPOUND_EXPR:
2393 result = build_nt (COMPOUND_EXPR,
2394 stabilize_reference_1 (TREE_OPERAND (ref, 0)),
2395 stabilize_reference (TREE_OPERAND (ref, 1)));
2396 break;
2399 /* If arg isn't a kind of lvalue we recognize, make no change.
2400 Caller should recognize the error for an invalid lvalue. */
2401 default:
2402 return ref;
2404 case ERROR_MARK:
2405 return error_mark_node;
2408 TREE_TYPE (result) = TREE_TYPE (ref);
2409 TREE_READONLY (result) = TREE_READONLY (ref);
2410 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
2411 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
2412 TREE_RAISES (result) = TREE_RAISES (ref);
2414 return result;
2417 /* Subroutine of stabilize_reference; this is called for subtrees of
2418 references. Any expression with side-effects must be put in a SAVE_EXPR
2419 to ensure that it is only evaluated once.
2421 We don't put SAVE_EXPR nodes around everything, because assigning very
2422 simple expressions to temporaries causes us to miss good opportunities
2423 for optimizations. Among other things, the opportunity to fold in the
2424 addition of a constant into an addressing mode often gets lost, e.g.
2425 "y[i+1] += x;". In general, we take the approach that we should not make
2426 an assignment unless we are forced into it - i.e., that any non-side effect
2427 operator should be allowed, and that cse should take care of coalescing
2428 multiple utterances of the same expression should that prove fruitful. */
2430 static tree
2431 stabilize_reference_1 (e)
2432 tree e;
2434 register tree result;
2435 register enum tree_code code = TREE_CODE (e);
2437 /* We cannot ignore const expressions because it might be a reference
2438 to a const array but whose index contains side-effects. But we can
2439 ignore things that are actual constant or that already have been
2440 handled by this function. */
2442 if (TREE_CONSTANT (e) || code == SAVE_EXPR)
2443 return e;
2445 switch (TREE_CODE_CLASS (code))
2447 case 'x':
2448 case 't':
2449 case 'd':
2450 case 'b':
2451 case '<':
2452 case 's':
2453 case 'e':
2454 case 'r':
2455 /* If the expression has side-effects, then encase it in a SAVE_EXPR
2456 so that it will only be evaluated once. */
2457 /* The reference (r) and comparison (<) classes could be handled as
2458 below, but it is generally faster to only evaluate them once. */
2459 if (TREE_SIDE_EFFECTS (e))
2460 return save_expr (e);
2461 return e;
2463 case 'c':
2464 /* Constants need no processing. In fact, we should never reach
2465 here. */
2466 return e;
2468 case '2':
2469 /* Division is slow and tends to be compiled with jumps,
2470 especially the division by powers of 2 that is often
2471 found inside of an array reference. So do it just once. */
2472 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
2473 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
2474 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
2475 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
2476 return save_expr (e);
2477 /* Recursively stabilize each operand. */
2478 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
2479 stabilize_reference_1 (TREE_OPERAND (e, 1)));
2480 break;
2482 case '1':
2483 /* Recursively stabilize each operand. */
2484 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
2485 break;
2487 default:
2488 abort ();
2491 TREE_TYPE (result) = TREE_TYPE (e);
2492 TREE_READONLY (result) = TREE_READONLY (e);
2493 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
2494 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
2495 TREE_RAISES (result) = TREE_RAISES (e);
2497 return result;
2500 /* Low-level constructors for expressions. */
2502 /* Build an expression of code CODE, data type TYPE,
2503 and operands as specified by the arguments ARG1 and following arguments.
2504 Expressions and reference nodes can be created this way.
2505 Constants, decls, types and misc nodes cannot be. */
2507 tree
2508 build VPROTO((enum tree_code code, tree tt, ...))
2510 #ifndef __STDC__
2511 enum tree_code code;
2512 tree tt;
2513 #endif
2514 va_list p;
2515 register tree t;
2516 register int length;
2517 register int i;
2519 VA_START (p, tt);
2521 #ifndef __STDC__
2522 code = va_arg (p, enum tree_code);
2523 tt = va_arg (p, tree);
2524 #endif
2526 t = make_node (code);
2527 length = tree_code_length[(int) code];
2528 TREE_TYPE (t) = tt;
2530 if (length == 2)
2532 /* This is equivalent to the loop below, but faster. */
2533 register tree arg0 = va_arg (p, tree);
2534 register tree arg1 = va_arg (p, tree);
2535 TREE_OPERAND (t, 0) = arg0;
2536 TREE_OPERAND (t, 1) = arg1;
2537 if ((arg0 && TREE_SIDE_EFFECTS (arg0))
2538 || (arg1 && TREE_SIDE_EFFECTS (arg1)))
2539 TREE_SIDE_EFFECTS (t) = 1;
2540 TREE_RAISES (t)
2541 = (arg0 && TREE_RAISES (arg0)) || (arg1 && TREE_RAISES (arg1));
2543 else if (length == 1)
2545 register tree arg0 = va_arg (p, tree);
2547 /* Call build1 for this! */
2548 if (TREE_CODE_CLASS (code) != 's')
2549 abort ();
2550 TREE_OPERAND (t, 0) = arg0;
2551 if (arg0 && TREE_SIDE_EFFECTS (arg0))
2552 TREE_SIDE_EFFECTS (t) = 1;
2553 TREE_RAISES (t) = (arg0 && TREE_RAISES (arg0));
2555 else
2557 for (i = 0; i < length; i++)
2559 register tree operand = va_arg (p, tree);
2560 TREE_OPERAND (t, i) = operand;
2561 if (operand)
2563 if (TREE_SIDE_EFFECTS (operand))
2564 TREE_SIDE_EFFECTS (t) = 1;
2565 if (TREE_RAISES (operand))
2566 TREE_RAISES (t) = 1;
2570 va_end (p);
2571 return t;
2574 /* Same as above, but only builds for unary operators.
2575 Saves lions share of calls to `build'; cuts down use
2576 of varargs, which is expensive for RISC machines. */
2577 tree
2578 build1 (code, type, node)
2579 enum tree_code code;
2580 tree type;
2581 tree node;
2583 register struct obstack *obstack = current_obstack;
2584 register int i, length;
2585 register tree_node_kind kind;
2586 register tree t;
2588 #ifdef GATHER_STATISTICS
2589 if (TREE_CODE_CLASS (code) == 'r')
2590 kind = r_kind;
2591 else
2592 kind = e_kind;
2593 #endif
2595 obstack = expression_obstack;
2596 length = sizeof (struct tree_exp);
2598 t = (tree) obstack_alloc (obstack, length);
2600 #ifdef GATHER_STATISTICS
2601 tree_node_counts[(int)kind]++;
2602 tree_node_sizes[(int)kind] += length;
2603 #endif
2605 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
2606 ((int *) t)[i] = 0;
2608 TREE_TYPE (t) = type;
2609 TREE_SET_CODE (t, code);
2611 if (obstack == &permanent_obstack)
2612 TREE_PERMANENT (t) = 1;
2614 TREE_OPERAND (t, 0) = node;
2615 if (node)
2617 if (TREE_SIDE_EFFECTS (node))
2618 TREE_SIDE_EFFECTS (t) = 1;
2619 if (TREE_RAISES (node))
2620 TREE_RAISES (t) = 1;
2623 return t;
2626 /* Similar except don't specify the TREE_TYPE
2627 and leave the TREE_SIDE_EFFECTS as 0.
2628 It is permissible for arguments to be null,
2629 or even garbage if their values do not matter. */
2631 tree
2632 build_nt VPROTO((enum tree_code code, ...))
2634 #ifndef __STDC__
2635 enum tree_code code;
2636 #endif
2637 va_list p;
2638 register tree t;
2639 register int length;
2640 register int i;
2642 VA_START (p, code);
2644 #ifndef __STDC__
2645 code = va_arg (p, enum tree_code);
2646 #endif
2648 t = make_node (code);
2649 length = tree_code_length[(int) code];
2651 for (i = 0; i < length; i++)
2652 TREE_OPERAND (t, i) = va_arg (p, tree);
2654 va_end (p);
2655 return t;
2658 /* Similar to `build_nt', except we build
2659 on the temp_decl_obstack, regardless. */
2661 tree
2662 build_parse_node VPROTO((enum tree_code code, ...))
2664 #ifndef __STDC__
2665 enum tree_code code;
2666 #endif
2667 register struct obstack *ambient_obstack = expression_obstack;
2668 va_list p;
2669 register tree t;
2670 register int length;
2671 register int i;
2673 VA_START (p, code);
2675 #ifndef __STDC__
2676 code = va_arg (p, enum tree_code);
2677 #endif
2679 expression_obstack = &temp_decl_obstack;
2681 t = make_node (code);
2682 length = tree_code_length[(int) code];
2684 for (i = 0; i < length; i++)
2685 TREE_OPERAND (t, i) = va_arg (p, tree);
2687 va_end (p);
2688 expression_obstack = ambient_obstack;
2689 return t;
2692 #if 0
2693 /* Commented out because this wants to be done very
2694 differently. See cp-lex.c. */
2695 tree
2696 build_op_identifier (op1, op2)
2697 tree op1, op2;
2699 register tree t = make_node (OP_IDENTIFIER);
2700 TREE_PURPOSE (t) = op1;
2701 TREE_VALUE (t) = op2;
2702 return t;
2704 #endif
2706 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
2707 We do NOT enter this node in any sort of symbol table.
2709 layout_decl is used to set up the decl's storage layout.
2710 Other slots are initialized to 0 or null pointers. */
2712 tree
2713 build_decl (code, name, type)
2714 enum tree_code code;
2715 tree name, type;
2717 register tree t;
2719 t = make_node (code);
2721 /* if (type == error_mark_node)
2722 type = integer_type_node; */
2723 /* That is not done, deliberately, so that having error_mark_node
2724 as the type can suppress useless errors in the use of this variable. */
2726 DECL_NAME (t) = name;
2727 DECL_ASSEMBLER_NAME (t) = name;
2728 TREE_TYPE (t) = type;
2730 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
2731 layout_decl (t, 0);
2732 else if (code == FUNCTION_DECL)
2733 DECL_MODE (t) = FUNCTION_MODE;
2735 return t;
2738 /* BLOCK nodes are used to represent the structure of binding contours
2739 and declarations, once those contours have been exited and their contents
2740 compiled. This information is used for outputting debugging info. */
2742 tree
2743 build_block (vars, tags, subblocks, supercontext, chain)
2744 tree vars, tags, subblocks, supercontext, chain;
2746 register tree block = make_node (BLOCK);
2747 BLOCK_VARS (block) = vars;
2748 BLOCK_TYPE_TAGS (block) = tags;
2749 BLOCK_SUBBLOCKS (block) = subblocks;
2750 BLOCK_SUPERCONTEXT (block) = supercontext;
2751 BLOCK_CHAIN (block) = chain;
2752 return block;
2755 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
2756 is ATTRIBUTE.
2758 Such modified types already made are recorded so that duplicates
2759 are not made. */
2761 tree
2762 build_type_attribute_variant (ttype, attribute)
2763 tree ttype, attribute;
2765 if ( ! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
2767 register int hashcode;
2768 register struct obstack *ambient_obstack = current_obstack;
2769 tree ntype;
2771 if (ambient_obstack != &permanent_obstack)
2772 current_obstack = TYPE_OBSTACK (ttype);
2774 ntype = copy_node (ttype);
2775 current_obstack = ambient_obstack;
2777 TYPE_POINTER_TO (ntype) = 0;
2778 TYPE_REFERENCE_TO (ntype) = 0;
2779 TYPE_ATTRIBUTES (ntype) = attribute;
2781 /* Create a new main variant of TYPE. */
2782 TYPE_MAIN_VARIANT (ntype) = ntype;
2783 TYPE_NEXT_VARIANT (ntype) = 0;
2784 TYPE_READONLY (ntype) = TYPE_VOLATILE (ntype) = 0;
2786 hashcode = TYPE_HASH (TREE_CODE (ntype))
2787 + TYPE_HASH (TREE_TYPE (ntype))
2788 + type_hash_list (attribute);
2790 switch (TREE_CODE (ntype))
2792 case FUNCTION_TYPE:
2793 hashcode += TYPE_HASH (TYPE_ARG_TYPES (ntype));
2794 break;
2795 case ARRAY_TYPE:
2796 hashcode += TYPE_HASH (TYPE_DOMAIN (ntype));
2797 break;
2798 case INTEGER_TYPE:
2799 hashcode += TYPE_HASH (TYPE_MAX_VALUE (ntype));
2800 break;
2801 case REAL_TYPE:
2802 hashcode += TYPE_HASH (TYPE_PRECISION (ntype));
2803 break;
2806 ntype = type_hash_canon (hashcode, ntype);
2807 ttype = build_type_variant (ntype, TYPE_READONLY (ttype),
2808 TYPE_VOLATILE (ttype));
2811 return ttype;
2814 /* Return a type like TYPE except that its TYPE_READONLY is CONSTP
2815 and its TYPE_VOLATILE is VOLATILEP.
2817 Such variant types already made are recorded so that duplicates
2818 are not made.
2820 A variant types should never be used as the type of an expression.
2821 Always copy the variant information into the TREE_READONLY
2822 and TREE_THIS_VOLATILE of the expression, and then give the expression
2823 as its type the "main variant", the variant whose TYPE_READONLY
2824 and TYPE_VOLATILE are zero. Use TYPE_MAIN_VARIANT to find the
2825 main variant. */
2827 tree
2828 build_type_variant (type, constp, volatilep)
2829 tree type;
2830 int constp, volatilep;
2832 register tree t;
2834 /* Treat any nonzero argument as 1. */
2835 constp = !!constp;
2836 volatilep = !!volatilep;
2838 /* Search the chain of variants to see if there is already one there just
2839 like the one we need to have. If so, use that existing one. We must
2840 preserve the TYPE_NAME, since there is code that depends on this. */
2842 for (t = TYPE_MAIN_VARIANT(type); t; t = TYPE_NEXT_VARIANT (t))
2843 if (constp == TYPE_READONLY (t) && volatilep == TYPE_VOLATILE (t)
2844 && TYPE_NAME (t) == TYPE_NAME (type))
2845 return t;
2847 /* We need a new one. */
2849 t = build_type_copy (type);
2850 TYPE_READONLY (t) = constp;
2851 TYPE_VOLATILE (t) = volatilep;
2853 return t;
2856 /* Give TYPE a new main variant: NEW_MAIN.
2857 This is the right thing to do only when something else
2858 about TYPE is modified in place. */
2860 void
2861 change_main_variant (type, new_main)
2862 tree type, new_main;
2864 tree t;
2865 tree omain = TYPE_MAIN_VARIANT (type);
2867 /* Remove TYPE from the TYPE_NEXT_VARIANT chain of its main variant. */
2868 if (TYPE_NEXT_VARIANT (omain) == type)
2869 TYPE_NEXT_VARIANT (omain) = TYPE_NEXT_VARIANT (type);
2870 else
2871 for (t = TYPE_NEXT_VARIANT (omain); t && TYPE_NEXT_VARIANT (t);
2872 t = TYPE_NEXT_VARIANT (t))
2873 if (TYPE_NEXT_VARIANT (t) == type)
2875 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (type);
2876 break;
2879 TYPE_MAIN_VARIANT (type) = new_main;
2880 TYPE_NEXT_VARIANT (type) = TYPE_NEXT_VARIANT (new_main);
2881 TYPE_NEXT_VARIANT (new_main) = type;
2884 /* Create a new variant of TYPE, equivalent but distinct.
2885 This is so the caller can modify it. */
2887 tree
2888 build_type_copy (type)
2889 tree type;
2891 register tree t, m = TYPE_MAIN_VARIANT (type);
2892 register struct obstack *ambient_obstack = current_obstack;
2894 current_obstack = TYPE_OBSTACK (type);
2895 t = copy_node (type);
2896 current_obstack = ambient_obstack;
2898 TYPE_POINTER_TO (t) = 0;
2899 TYPE_REFERENCE_TO (t) = 0;
2901 /* Add this type to the chain of variants of TYPE. */
2902 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
2903 TYPE_NEXT_VARIANT (m) = t;
2905 return t;
2908 /* Hashing of types so that we don't make duplicates.
2909 The entry point is `type_hash_canon'. */
2911 /* Each hash table slot is a bucket containing a chain
2912 of these structures. */
2914 struct type_hash
2916 struct type_hash *next; /* Next structure in the bucket. */
2917 int hashcode; /* Hash code of this type. */
2918 tree type; /* The type recorded here. */
2921 /* Now here is the hash table. When recording a type, it is added
2922 to the slot whose index is the hash code mod the table size.
2923 Note that the hash table is used for several kinds of types
2924 (function types, array types and array index range types, for now).
2925 While all these live in the same table, they are completely independent,
2926 and the hash code is computed differently for each of these. */
2928 #define TYPE_HASH_SIZE 59
2929 struct type_hash *type_hash_table[TYPE_HASH_SIZE];
2931 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
2932 with types in the TREE_VALUE slots), by adding the hash codes
2933 of the individual types. */
2936 type_hash_list (list)
2937 tree list;
2939 register int hashcode;
2940 register tree tail;
2941 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
2942 hashcode += TYPE_HASH (TREE_VALUE (tail));
2943 return hashcode;
2946 /* Look in the type hash table for a type isomorphic to TYPE.
2947 If one is found, return it. Otherwise return 0. */
2949 tree
2950 type_hash_lookup (hashcode, type)
2951 int hashcode;
2952 tree type;
2954 register struct type_hash *h;
2955 for (h = type_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
2956 if (h->hashcode == hashcode
2957 && TREE_CODE (h->type) == TREE_CODE (type)
2958 && TREE_TYPE (h->type) == TREE_TYPE (type)
2959 && attribute_list_equal (TYPE_ATTRIBUTES (h->type),
2960 TYPE_ATTRIBUTES (type))
2961 && (TYPE_MAX_VALUE (h->type) == TYPE_MAX_VALUE (type)
2962 || tree_int_cst_equal (TYPE_MAX_VALUE (h->type),
2963 TYPE_MAX_VALUE (type)))
2964 && (TYPE_MIN_VALUE (h->type) == TYPE_MIN_VALUE (type)
2965 || tree_int_cst_equal (TYPE_MIN_VALUE (h->type),
2966 TYPE_MIN_VALUE (type)))
2967 && (TYPE_DOMAIN (h->type) == TYPE_DOMAIN (type)
2968 || (TYPE_DOMAIN (h->type)
2969 && TREE_CODE (TYPE_DOMAIN (h->type)) == TREE_LIST
2970 && TYPE_DOMAIN (type)
2971 && TREE_CODE (TYPE_DOMAIN (type)) == TREE_LIST
2972 && type_list_equal (TYPE_DOMAIN (h->type), TYPE_DOMAIN (type)))))
2973 return h->type;
2974 return 0;
2977 /* Add an entry to the type-hash-table
2978 for a type TYPE whose hash code is HASHCODE. */
2980 void
2981 type_hash_add (hashcode, type)
2982 int hashcode;
2983 tree type;
2985 register struct type_hash *h;
2987 h = (struct type_hash *) oballoc (sizeof (struct type_hash));
2988 h->hashcode = hashcode;
2989 h->type = type;
2990 h->next = type_hash_table[hashcode % TYPE_HASH_SIZE];
2991 type_hash_table[hashcode % TYPE_HASH_SIZE] = h;
2994 /* Given TYPE, and HASHCODE its hash code, return the canonical
2995 object for an identical type if one already exists.
2996 Otherwise, return TYPE, and record it as the canonical object
2997 if it is a permanent object.
2999 To use this function, first create a type of the sort you want.
3000 Then compute its hash code from the fields of the type that
3001 make it different from other similar types.
3002 Then call this function and use the value.
3003 This function frees the type you pass in if it is a duplicate. */
3005 /* Set to 1 to debug without canonicalization. Never set by program. */
3006 int debug_no_type_hash = 0;
3008 tree
3009 type_hash_canon (hashcode, type)
3010 int hashcode;
3011 tree type;
3013 tree t1;
3015 if (debug_no_type_hash)
3016 return type;
3018 t1 = type_hash_lookup (hashcode, type);
3019 if (t1 != 0)
3021 obstack_free (TYPE_OBSTACK (type), type);
3022 #ifdef GATHER_STATISTICS
3023 tree_node_counts[(int)t_kind]--;
3024 tree_node_sizes[(int)t_kind] -= sizeof (struct tree_type);
3025 #endif
3026 return t1;
3029 /* If this is a permanent type, record it for later reuse. */
3030 if (TREE_PERMANENT (type))
3031 type_hash_add (hashcode, type);
3033 return type;
3036 /* Given two lists of attributes, return true if list l2 is
3037 equivalent to l1. */
3040 attribute_list_equal (l1, l2)
3041 tree l1, l2;
3043 return attribute_list_contained (l1, l2)
3044 && attribute_list_contained (l2, l1);
3047 /* Given two lists of attributes, return true if list l2 is
3048 completely contained within l1. */
3051 attribute_list_contained (l1, l2)
3052 tree l1, l2;
3054 register tree t1, t2;
3056 /* First check the obvious, maybe the lists are identical. */
3057 if (l1 == l2)
3058 return 1;
3060 /* Then check the obvious, maybe the lists are similar. */
3061 for (t1 = l1, t2 = l2;
3062 t1 && t2
3063 && TREE_VALUE (t1) == TREE_VALUE (t2);
3064 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
3066 /* Maybe the lists are equal. */
3067 if (t1 == 0 && t2 == 0)
3068 return 1;
3070 for (; t2; t2 = TREE_CHAIN (t2))
3071 if (!value_member (l1, t2))
3072 return 0;
3073 return 1;
3076 /* Given two lists of types
3077 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
3078 return 1 if the lists contain the same types in the same order.
3079 Also, the TREE_PURPOSEs must match. */
3082 type_list_equal (l1, l2)
3083 tree l1, l2;
3085 register tree t1, t2;
3086 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
3088 if (TREE_VALUE (t1) != TREE_VALUE (t2))
3089 return 0;
3090 if (TREE_PURPOSE (t1) != TREE_PURPOSE (t2))
3092 int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
3093 if (cmp < 0)
3094 abort ();
3095 if (cmp == 0
3096 || TREE_TYPE (TREE_PURPOSE (t1))
3097 != TREE_TYPE (TREE_PURPOSE (t2)))
3098 return 0;
3102 return t1 == t2;
3105 /* Nonzero if integer constants T1 and T2
3106 represent the same constant value. */
3109 tree_int_cst_equal (t1, t2)
3110 tree t1, t2;
3112 if (t1 == t2)
3113 return 1;
3114 if (t1 == 0 || t2 == 0)
3115 return 0;
3116 if (TREE_CODE (t1) == INTEGER_CST
3117 && TREE_CODE (t2) == INTEGER_CST
3118 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3119 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
3120 return 1;
3121 return 0;
3124 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
3125 The precise way of comparison depends on their data type. */
3128 tree_int_cst_lt (t1, t2)
3129 tree t1, t2;
3131 if (t1 == t2)
3132 return 0;
3134 if (!TREE_UNSIGNED (TREE_TYPE (t1)))
3135 return INT_CST_LT (t1, t2);
3136 return INT_CST_LT_UNSIGNED (t1, t2);
3139 /* Return an indication of the sign of the integer constant T.
3140 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
3141 Note that -1 will never be returned it T's type is unsigned. */
3144 tree_int_cst_sgn (t)
3145 tree t;
3147 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
3148 return 0;
3149 else if (TREE_UNSIGNED (TREE_TYPE (t)))
3150 return 1;
3151 else if (TREE_INT_CST_HIGH (t) < 0)
3152 return -1;
3153 else
3154 return 1;
3157 /* Compare two constructor-element-type constants. */
3159 simple_cst_list_equal (l1, l2)
3160 tree l1, l2;
3162 while (l1 != NULL_TREE && l2 != NULL_TREE)
3164 int cmp = simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2));
3165 if (cmp < 0)
3166 abort ();
3167 if (cmp == 0)
3168 return 0;
3169 l1 = TREE_CHAIN (l1);
3170 l2 = TREE_CHAIN (l2);
3172 return (l1 == l2);
3175 /* Return truthvalue of whether T1 is the same tree structure as T2.
3176 Return 1 if they are the same.
3177 Return 0 if they are understandably different.
3178 Return -1 if either contains tree structure not understood by
3179 this function. */
3182 simple_cst_equal (t1, t2)
3183 tree t1, t2;
3185 register enum tree_code code1, code2;
3186 int cmp;
3188 if (t1 == t2)
3189 return 1;
3190 if (t1 == 0 || t2 == 0)
3191 return 0;
3193 code1 = TREE_CODE (t1);
3194 code2 = TREE_CODE (t2);
3196 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
3197 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR || code2 == NON_LVALUE_EXPR)
3198 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3199 else
3200 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
3201 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3202 || code2 == NON_LVALUE_EXPR)
3203 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
3205 if (code1 != code2)
3206 return 0;
3208 switch (code1)
3210 case INTEGER_CST:
3211 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3212 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
3214 case REAL_CST:
3215 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3217 case STRING_CST:
3218 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3219 && !bcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3220 TREE_STRING_LENGTH (t1));
3222 case CONSTRUCTOR:
3223 abort ();
3225 case SAVE_EXPR:
3226 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3228 case CALL_EXPR:
3229 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3230 if (cmp <= 0)
3231 return cmp;
3232 return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3234 case TARGET_EXPR:
3235 /* Special case: if either target is an unallocated VAR_DECL,
3236 it means that it's going to be unified with whatever the
3237 TARGET_EXPR is really supposed to initialize, so treat it
3238 as being equivalent to anything. */
3239 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
3240 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
3241 && DECL_RTL (TREE_OPERAND (t1, 0)) == 0)
3242 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
3243 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
3244 && DECL_RTL (TREE_OPERAND (t2, 0)) == 0))
3245 cmp = 1;
3246 else
3247 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3248 if (cmp <= 0)
3249 return cmp;
3250 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3252 case WITH_CLEANUP_EXPR:
3253 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3254 if (cmp <= 0)
3255 return cmp;
3256 return simple_cst_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t1, 2));
3258 case COMPONENT_REF:
3259 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
3260 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3261 return 0;
3263 case VAR_DECL:
3264 case PARM_DECL:
3265 case CONST_DECL:
3266 case FUNCTION_DECL:
3267 return 0;
3270 /* This general rule works for most tree codes.
3271 All exceptions should be handled above. */
3273 switch (TREE_CODE_CLASS (code1))
3275 int i;
3276 case '1':
3277 case '2':
3278 case '<':
3279 case 'e':
3280 case 'r':
3281 case 's':
3282 cmp = 1;
3283 for (i=0; i<tree_code_length[(int) code1]; ++i)
3285 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
3286 if (cmp <= 0)
3287 return cmp;
3289 return cmp;
3292 return -1;
3295 /* Constructors for pointer, array and function types.
3296 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
3297 constructed by language-dependent code, not here.) */
3299 /* Construct, lay out and return the type of pointers to TO_TYPE.
3300 If such a type has already been constructed, reuse it. */
3302 tree
3303 build_pointer_type (to_type)
3304 tree to_type;
3306 register tree t = TYPE_POINTER_TO (to_type);
3308 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3310 if (t)
3311 return t;
3313 /* We need a new one. Put this in the same obstack as TO_TYPE. */
3314 push_obstacks (TYPE_OBSTACK (to_type), TYPE_OBSTACK (to_type));
3315 t = make_node (POINTER_TYPE);
3316 pop_obstacks ();
3318 TREE_TYPE (t) = to_type;
3320 /* Record this type as the pointer to TO_TYPE. */
3321 TYPE_POINTER_TO (to_type) = t;
3323 /* Lay out the type. This function has many callers that are concerned
3324 with expression-construction, and this simplifies them all.
3325 Also, it guarantees the TYPE_SIZE is in the same obstack as the type. */
3326 layout_type (t);
3328 return t;
3331 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
3332 MAXVAL should be the maximum value in the domain
3333 (one less than the length of the array). */
3335 tree
3336 build_index_type (maxval)
3337 tree maxval;
3339 register tree itype = make_node (INTEGER_TYPE);
3340 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
3341 TYPE_MIN_VALUE (itype) = build_int_2 (0, 0);
3342 TREE_TYPE (TYPE_MIN_VALUE (itype)) = sizetype;
3343 TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
3344 TYPE_MODE (itype) = TYPE_MODE (sizetype);
3345 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
3346 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
3347 if (TREE_CODE (maxval) == INTEGER_CST)
3349 int maxint = (int) TREE_INT_CST_LOW (maxval);
3350 /* If the domain should be empty, make sure the maxval
3351 remains -1 and is not spoiled by truncation. */
3352 if (INT_CST_LT (maxval, integer_zero_node))
3354 TYPE_MAX_VALUE (itype) = build_int_2 (-1, -1);
3355 TREE_TYPE (TYPE_MAX_VALUE (itype)) = sizetype;
3357 return type_hash_canon (maxint < 0 ? ~maxint : maxint, itype);
3359 else
3360 return itype;
3363 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
3364 ENUMERAL_TYPE, BOOLEAN_TYPE, or CHAR_TYPE), with
3365 low bound LOWVAL and high bound HIGHVAL.
3366 if TYPE==NULL_TREE, sizetype is used. */
3368 tree
3369 build_range_type (type, lowval, highval)
3370 tree type, lowval, highval;
3372 register tree itype = make_node (INTEGER_TYPE);
3373 TREE_TYPE (itype) = type;
3374 if (type == NULL_TREE)
3375 type = sizetype;
3376 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
3377 TYPE_MIN_VALUE (itype) = convert (type, lowval);
3378 TYPE_MAX_VALUE (itype) = convert (type, highval);
3379 TYPE_MODE (itype) = TYPE_MODE (type);
3380 TYPE_SIZE (itype) = TYPE_SIZE (type);
3381 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
3382 if ((TREE_CODE (lowval) == INTEGER_CST)
3383 && (TREE_CODE (highval) == INTEGER_CST))
3385 HOST_WIDE_INT highint = TREE_INT_CST_LOW (highval);
3386 HOST_WIDE_INT lowint = TREE_INT_CST_LOW (lowval);
3387 int maxint = (int) (highint - lowint);
3388 return type_hash_canon (maxint < 0 ? ~maxint : maxint, itype);
3390 else
3391 return itype;
3394 /* Just like build_index_type, but takes lowval and highval instead
3395 of just highval (maxval). */
3397 tree
3398 build_index_2_type (lowval,highval)
3399 tree lowval, highval;
3401 return build_range_type (NULL_TREE, lowval, highval);
3404 /* Return nonzero iff ITYPE1 and ITYPE2 are equal (in the LISP sense).
3405 Needed because when index types are not hashed, equal index types
3406 built at different times appear distinct, even though structurally,
3407 they are not. */
3410 index_type_equal (itype1, itype2)
3411 tree itype1, itype2;
3413 if (TREE_CODE (itype1) != TREE_CODE (itype2))
3414 return 0;
3415 if (TREE_CODE (itype1) == INTEGER_TYPE)
3417 if (TYPE_PRECISION (itype1) != TYPE_PRECISION (itype2)
3418 || TYPE_MODE (itype1) != TYPE_MODE (itype2)
3419 || ! simple_cst_equal (TYPE_SIZE (itype1), TYPE_SIZE (itype2))
3420 || TYPE_ALIGN (itype1) != TYPE_ALIGN (itype2))
3421 return 0;
3422 if (simple_cst_equal (TYPE_MIN_VALUE (itype1), TYPE_MIN_VALUE (itype2))
3423 && simple_cst_equal (TYPE_MAX_VALUE (itype1), TYPE_MAX_VALUE (itype2)))
3424 return 1;
3426 return 0;
3429 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
3430 and number of elements specified by the range of values of INDEX_TYPE.
3431 If such a type has already been constructed, reuse it. */
3433 tree
3434 build_array_type (elt_type, index_type)
3435 tree elt_type, index_type;
3437 register tree t;
3438 int hashcode;
3440 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
3442 error ("arrays of functions are not meaningful");
3443 elt_type = integer_type_node;
3446 /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
3447 build_pointer_type (elt_type);
3449 /* Allocate the array after the pointer type,
3450 in case we free it in type_hash_canon. */
3451 t = make_node (ARRAY_TYPE);
3452 TREE_TYPE (t) = elt_type;
3453 TYPE_DOMAIN (t) = index_type;
3455 if (index_type == 0)
3457 return t;
3460 hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
3461 t = type_hash_canon (hashcode, t);
3463 #if 0 /* This led to crashes, because it could put a temporary node
3464 on the TYPE_NEXT_VARIANT chain of a permanent one. */
3465 /* The main variant of an array type should always
3466 be an array whose element type is the main variant. */
3467 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
3468 change_main_variant (t, build_array_type (TYPE_MAIN_VARIANT (elt_type),
3469 index_type));
3470 #endif
3472 if (TYPE_SIZE (t) == 0)
3473 layout_type (t);
3474 return t;
3477 /* Construct, lay out and return
3478 the type of functions returning type VALUE_TYPE
3479 given arguments of types ARG_TYPES.
3480 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
3481 are data type nodes for the arguments of the function.
3482 If such a type has already been constructed, reuse it. */
3484 tree
3485 build_function_type (value_type, arg_types)
3486 tree value_type, arg_types;
3488 register tree t;
3489 int hashcode;
3491 if (TREE_CODE (value_type) == FUNCTION_TYPE)
3493 error ("function return type cannot be function");
3494 value_type = integer_type_node;
3497 /* Make a node of the sort we want. */
3498 t = make_node (FUNCTION_TYPE);
3499 TREE_TYPE (t) = value_type;
3500 TYPE_ARG_TYPES (t) = arg_types;
3502 /* If we already have such a type, use the old one and free this one. */
3503 hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
3504 t = type_hash_canon (hashcode, t);
3506 if (TYPE_SIZE (t) == 0)
3507 layout_type (t);
3508 return t;
3511 /* Build the node for the type of references-to-TO_TYPE. */
3513 tree
3514 build_reference_type (to_type)
3515 tree to_type;
3517 register tree t = TYPE_REFERENCE_TO (to_type);
3518 register struct obstack *ambient_obstack = current_obstack;
3519 register struct obstack *ambient_saveable_obstack = saveable_obstack;
3521 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3523 if (t)
3524 return t;
3526 /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
3527 if (TREE_PERMANENT (to_type))
3529 current_obstack = &permanent_obstack;
3530 saveable_obstack = &permanent_obstack;
3533 t = make_node (REFERENCE_TYPE);
3534 TREE_TYPE (t) = to_type;
3536 /* Record this type as the pointer to TO_TYPE. */
3537 TYPE_REFERENCE_TO (to_type) = t;
3539 layout_type (t);
3541 current_obstack = ambient_obstack;
3542 saveable_obstack = ambient_saveable_obstack;
3543 return t;
3546 /* Construct, lay out and return the type of methods belonging to class
3547 BASETYPE and whose arguments and values are described by TYPE.
3548 If that type exists already, reuse it.
3549 TYPE must be a FUNCTION_TYPE node. */
3551 tree
3552 build_method_type (basetype, type)
3553 tree basetype, type;
3555 register tree t;
3556 int hashcode;
3558 /* Make a node of the sort we want. */
3559 t = make_node (METHOD_TYPE);
3561 if (TREE_CODE (type) != FUNCTION_TYPE)
3562 abort ();
3564 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3565 TREE_TYPE (t) = TREE_TYPE (type);
3567 /* The actual arglist for this function includes a "hidden" argument
3568 which is "this". Put it into the list of argument types. */
3570 TYPE_ARG_TYPES (t)
3571 = tree_cons (NULL_TREE,
3572 build_pointer_type (basetype), TYPE_ARG_TYPES (type));
3574 /* If we already have such a type, use the old one and free this one. */
3575 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
3576 t = type_hash_canon (hashcode, t);
3578 if (TYPE_SIZE (t) == 0)
3579 layout_type (t);
3581 return t;
3584 /* Construct, lay out and return the type of offsets to a value
3585 of type TYPE, within an object of type BASETYPE.
3586 If a suitable offset type exists already, reuse it. */
3588 tree
3589 build_offset_type (basetype, type)
3590 tree basetype, type;
3592 register tree t;
3593 int hashcode;
3595 /* Make a node of the sort we want. */
3596 t = make_node (OFFSET_TYPE);
3598 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3599 TREE_TYPE (t) = type;
3601 /* If we already have such a type, use the old one and free this one. */
3602 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
3603 t = type_hash_canon (hashcode, t);
3605 if (TYPE_SIZE (t) == 0)
3606 layout_type (t);
3608 return t;
3611 /* Create a complex type whose components are COMPONENT_TYPE. */
3613 tree
3614 build_complex_type (component_type)
3615 tree component_type;
3617 register tree t;
3618 int hashcode;
3620 /* Make a node of the sort we want. */
3621 t = make_node (COMPLEX_TYPE);
3623 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
3624 TYPE_VOLATILE (t) = TYPE_VOLATILE (component_type);
3625 TYPE_READONLY (t) = TYPE_READONLY (component_type);
3627 /* If we already have such a type, use the old one and free this one. */
3628 hashcode = TYPE_HASH (component_type);
3629 t = type_hash_canon (hashcode, t);
3631 if (TYPE_SIZE (t) == 0)
3632 layout_type (t);
3634 return t;
3637 /* Return OP, stripped of any conversions to wider types as much as is safe.
3638 Converting the value back to OP's type makes a value equivalent to OP.
3640 If FOR_TYPE is nonzero, we return a value which, if converted to
3641 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
3643 If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
3644 narrowest type that can hold the value, even if they don't exactly fit.
3645 Otherwise, bit-field references are changed to a narrower type
3646 only if they can be fetched directly from memory in that type.
3648 OP must have integer, real or enumeral type. Pointers are not allowed!
3650 There are some cases where the obvious value we could return
3651 would regenerate to OP if converted to OP's type,
3652 but would not extend like OP to wider types.
3653 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
3654 For example, if OP is (unsigned short)(signed char)-1,
3655 we avoid returning (signed char)-1 if FOR_TYPE is int,
3656 even though extending that to an unsigned short would regenerate OP,
3657 since the result of extending (signed char)-1 to (int)
3658 is different from (int) OP. */
3660 tree
3661 get_unwidened (op, for_type)
3662 register tree op;
3663 tree for_type;
3665 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
3666 /* TYPE_PRECISION is safe in place of type_precision since
3667 pointer types are not allowed. */
3668 register tree type = TREE_TYPE (op);
3669 register unsigned final_prec
3670 = TYPE_PRECISION (for_type != 0 ? for_type : type);
3671 register int uns
3672 = (for_type != 0 && for_type != type
3673 && final_prec > TYPE_PRECISION (type)
3674 && TREE_UNSIGNED (type));
3675 register tree win = op;
3677 while (TREE_CODE (op) == NOP_EXPR)
3679 register int bitschange
3680 = TYPE_PRECISION (TREE_TYPE (op))
3681 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
3683 /* Truncations are many-one so cannot be removed.
3684 Unless we are later going to truncate down even farther. */
3685 if (bitschange < 0
3686 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
3687 break;
3689 /* See what's inside this conversion. If we decide to strip it,
3690 we will set WIN. */
3691 op = TREE_OPERAND (op, 0);
3693 /* If we have not stripped any zero-extensions (uns is 0),
3694 we can strip any kind of extension.
3695 If we have previously stripped a zero-extension,
3696 only zero-extensions can safely be stripped.
3697 Any extension can be stripped if the bits it would produce
3698 are all going to be discarded later by truncating to FOR_TYPE. */
3700 if (bitschange > 0)
3702 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
3703 win = op;
3704 /* TREE_UNSIGNED says whether this is a zero-extension.
3705 Let's avoid computing it if it does not affect WIN
3706 and if UNS will not be needed again. */
3707 if ((uns || TREE_CODE (op) == NOP_EXPR)
3708 && TREE_UNSIGNED (TREE_TYPE (op)))
3710 uns = 1;
3711 win = op;
3716 if (TREE_CODE (op) == COMPONENT_REF
3717 /* Since type_for_size always gives an integer type. */
3718 && TREE_CODE (type) != REAL_TYPE)
3720 unsigned innerprec = TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)));
3721 type = type_for_size (innerprec, TREE_UNSIGNED (TREE_OPERAND (op, 1)));
3723 /* We can get this structure field in the narrowest type it fits in.
3724 If FOR_TYPE is 0, do this only for a field that matches the
3725 narrower type exactly and is aligned for it
3726 The resulting extension to its nominal type (a fullword type)
3727 must fit the same conditions as for other extensions. */
3729 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
3730 && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
3731 && (! uns || final_prec <= innerprec
3732 || TREE_UNSIGNED (TREE_OPERAND (op, 1)))
3733 && type != 0)
3735 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
3736 TREE_OPERAND (op, 1));
3737 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
3738 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
3739 TREE_RAISES (win) = TREE_RAISES (op);
3742 return win;
3745 /* Return OP or a simpler expression for a narrower value
3746 which can be sign-extended or zero-extended to give back OP.
3747 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
3748 or 0 if the value should be sign-extended. */
3750 tree
3751 get_narrower (op, unsignedp_ptr)
3752 register tree op;
3753 int *unsignedp_ptr;
3755 register int uns = 0;
3756 int first = 1;
3757 register tree win = op;
3759 while (TREE_CODE (op) == NOP_EXPR)
3761 register int bitschange
3762 = TYPE_PRECISION (TREE_TYPE (op))
3763 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
3765 /* Truncations are many-one so cannot be removed. */
3766 if (bitschange < 0)
3767 break;
3769 /* See what's inside this conversion. If we decide to strip it,
3770 we will set WIN. */
3771 op = TREE_OPERAND (op, 0);
3773 if (bitschange > 0)
3775 /* An extension: the outermost one can be stripped,
3776 but remember whether it is zero or sign extension. */
3777 if (first)
3778 uns = TREE_UNSIGNED (TREE_TYPE (op));
3779 /* Otherwise, if a sign extension has been stripped,
3780 only sign extensions can now be stripped;
3781 if a zero extension has been stripped, only zero-extensions. */
3782 else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
3783 break;
3784 first = 0;
3786 else /* bitschange == 0 */
3788 /* A change in nominal type can always be stripped, but we must
3789 preserve the unsignedness. */
3790 if (first)
3791 uns = TREE_UNSIGNED (TREE_TYPE (op));
3792 first = 0;
3795 win = op;
3798 if (TREE_CODE (op) == COMPONENT_REF
3799 /* Since type_for_size always gives an integer type. */
3800 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE)
3802 unsigned innerprec = TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)));
3803 tree type = type_for_size (innerprec, TREE_UNSIGNED (op));
3805 /* We can get this structure field in a narrower type that fits it,
3806 but the resulting extension to its nominal type (a fullword type)
3807 must satisfy the same conditions as for other extensions.
3809 Do this only for fields that are aligned (not bit-fields),
3810 because when bit-field insns will be used there is no
3811 advantage in doing this. */
3813 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
3814 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
3815 && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
3816 && type != 0)
3818 if (first)
3819 uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
3820 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
3821 TREE_OPERAND (op, 1));
3822 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
3823 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
3824 TREE_RAISES (win) = TREE_RAISES (op);
3827 *unsignedp_ptr = uns;
3828 return win;
3831 /* Return the precision of a type, for arithmetic purposes.
3832 Supports all types on which arithmetic is possible
3833 (including pointer types).
3834 It's not clear yet what will be right for complex types. */
3837 type_precision (type)
3838 register tree type;
3840 return ((TREE_CODE (type) == INTEGER_TYPE
3841 || TREE_CODE (type) == ENUMERAL_TYPE
3842 || TREE_CODE (type) == REAL_TYPE)
3843 ? TYPE_PRECISION (type) : POINTER_SIZE);
3846 /* Nonzero if integer constant C has a value that is permissible
3847 for type TYPE (an INTEGER_TYPE). */
3850 int_fits_type_p (c, type)
3851 tree c, type;
3853 if (TREE_UNSIGNED (type))
3854 return (! (TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST
3855 && INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c))
3856 && ! (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
3857 && INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type))));
3858 else
3859 return (! (TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST
3860 && INT_CST_LT (TYPE_MAX_VALUE (type), c))
3861 && ! (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
3862 && INT_CST_LT (c, TYPE_MIN_VALUE (type))));
3865 /* Return the innermost context enclosing DECL that is
3866 a FUNCTION_DECL, or zero if none. */
3868 tree
3869 decl_function_context (decl)
3870 tree decl;
3872 tree context;
3874 if (TREE_CODE (decl) == ERROR_MARK)
3875 return 0;
3877 if (TREE_CODE (decl) == SAVE_EXPR)
3878 context = SAVE_EXPR_CONTEXT (decl);
3879 else
3880 context = DECL_CONTEXT (decl);
3882 while (context && TREE_CODE (context) != FUNCTION_DECL)
3884 if (TREE_CODE (context) == RECORD_TYPE
3885 || TREE_CODE (context) == UNION_TYPE)
3886 context = NULL_TREE;
3887 else if (TREE_CODE (context) == TYPE_DECL)
3888 context = DECL_CONTEXT (context);
3889 else if (TREE_CODE (context) == BLOCK)
3890 context = BLOCK_SUPERCONTEXT (context);
3891 else
3892 /* Unhandled CONTEXT !? */
3893 abort ();
3896 return context;
3899 /* Return the innermost context enclosing DECL that is
3900 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
3901 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
3903 tree
3904 decl_type_context (decl)
3905 tree decl;
3907 tree context = DECL_CONTEXT (decl);
3909 while (context)
3911 if (TREE_CODE (context) == RECORD_TYPE
3912 || TREE_CODE (context) == UNION_TYPE
3913 || TREE_CODE (context) == QUAL_UNION_TYPE)
3914 return context;
3915 if (TREE_CODE (context) == TYPE_DECL
3916 || TREE_CODE (context) == FUNCTION_DECL)
3917 context = DECL_CONTEXT (context);
3918 else if (TREE_CODE (context) == BLOCK)
3919 context = BLOCK_SUPERCONTEXT (context);
3920 else
3921 /* Unhandled CONTEXT!? */
3922 abort ();
3924 return NULL_TREE;
3927 void
3928 print_obstack_statistics (str, o)
3929 char *str;
3930 struct obstack *o;
3932 struct _obstack_chunk *chunk = o->chunk;
3933 int n_chunks = 0;
3934 int n_alloc = 0;
3936 while (chunk)
3938 n_chunks += 1;
3939 n_alloc += chunk->limit - &chunk->contents[0];
3940 chunk = chunk->prev;
3942 fprintf (stderr, "obstack %s: %d bytes, %d chunks\n",
3943 str, n_alloc, n_chunks);
3945 void
3946 dump_tree_statistics ()
3948 int i;
3949 int total_nodes, total_bytes;
3951 fprintf (stderr, "\n??? tree nodes created\n\n");
3952 #ifdef GATHER_STATISTICS
3953 fprintf (stderr, "Kind Nodes Bytes\n");
3954 fprintf (stderr, "-------------------------------------\n");
3955 total_nodes = total_bytes = 0;
3956 for (i = 0; i < (int) all_kinds; i++)
3958 fprintf (stderr, "%-20s %6d %9d\n", tree_node_kind_names[i],
3959 tree_node_counts[i], tree_node_sizes[i]);
3960 total_nodes += tree_node_counts[i];
3961 total_bytes += tree_node_sizes[i];
3963 fprintf (stderr, "%-20s %9d\n", "identifier names", id_string_size);
3964 fprintf (stderr, "-------------------------------------\n");
3965 fprintf (stderr, "%-20s %6d %9d\n", "Total", total_nodes, total_bytes);
3966 fprintf (stderr, "-------------------------------------\n");
3967 #else
3968 fprintf (stderr, "(No per-node statistics)\n");
3969 #endif
3970 print_lang_statistics ();
3973 #define FILE_FUNCTION_PREFIX_LEN 9
3975 #ifndef NO_DOLLAR_IN_LABEL
3976 #define FILE_FUNCTION_FORMAT "_GLOBAL_$D$%s"
3977 #else /* NO_DOLLAR_IN_LABEL */
3978 #ifndef NO_DOT_IN_LABEL
3979 #define FILE_FUNCTION_FORMAT "_GLOBAL_.D.%s"
3980 #else /* NO_DOT_IN_LABEL */
3981 #define FILE_FUNCTION_FORMAT "_GLOBAL__D_%s"
3982 #endif /* NO_DOT_IN_LABEL */
3983 #endif /* NO_DOLLAR_IN_LABEL */
3985 extern char * first_global_object_name;
3987 /* If KIND=='I', return a suitable global initializer (constructor) name.
3988 If KIND=='D', return a suitable global clean-up (destructor) name. */
3990 tree
3991 get_file_function_name (kind)
3992 int kind;
3994 char *buf;
3995 register char *p;
3997 if (first_global_object_name)
3998 p = first_global_object_name;
3999 else if (main_input_filename)
4000 p = main_input_filename;
4001 else
4002 p = input_filename;
4004 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p));
4006 /* Set up the name of the file-level functions we may need. */
4007 /* Use a global object (which is already required to be unique over
4008 the program) rather than the file name (which imposes extra
4009 constraints). -- Raeburn@MIT.EDU, 10 Jan 1990. */
4010 sprintf (buf, FILE_FUNCTION_FORMAT, p);
4012 /* Don't need to pull wierd characters out of global names. */
4013 if (p != first_global_object_name)
4015 for (p = buf+11; *p; p++)
4016 if (! ((*p >= '0' && *p <= '9')
4017 #if 0 /* we always want labels, which are valid C++ identifiers (+ `$') */
4018 #ifndef ASM_IDENTIFY_GCC /* this is required if `.' is invalid -- k. raeburn */
4019 || *p == '.'
4020 #endif
4021 #endif
4022 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
4023 || *p == '$'
4024 #endif
4025 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
4026 || *p == '.'
4027 #endif
4028 || (*p >= 'A' && *p <= 'Z')
4029 || (*p >= 'a' && *p <= 'z')))
4030 *p = '_';
4033 buf[FILE_FUNCTION_PREFIX_LEN] = kind;
4035 return get_identifier (buf);
4038 /* Expand (the constant part of) a SET_TYPE CONTRUCTOR node.
4039 The result is placed in BUFFER (which has length BIT_SIZE),
4040 with one bit in each char ('\000' or '\001').
4042 If the constructor is constant, NULL_TREE is returned.
4043 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4045 tree
4046 get_set_constructor_bits (init, buffer, bit_size)
4047 tree init;
4048 char *buffer;
4049 int bit_size;
4051 int i;
4052 tree vals;
4053 HOST_WIDE_INT domain_min
4054 = TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (init))));
4055 tree non_const_bits = NULL_TREE;
4056 for (i = 0; i < bit_size; i++)
4057 buffer[i] = 0;
4059 for (vals = TREE_OPERAND (init, 1);
4060 vals != NULL_TREE; vals = TREE_CHAIN (vals))
4062 if (TREE_CODE (TREE_VALUE (vals)) != INTEGER_CST
4063 || (TREE_PURPOSE (vals) != NULL_TREE
4064 && TREE_CODE (TREE_PURPOSE (vals)) != INTEGER_CST))
4065 non_const_bits =
4066 tree_cons (TREE_PURPOSE (vals), TREE_VALUE (vals), non_const_bits);
4067 else if (TREE_PURPOSE (vals) != NULL_TREE)
4069 /* Set a range of bits to ones. */
4070 HOST_WIDE_INT lo_index
4071 = TREE_INT_CST_LOW (TREE_PURPOSE (vals)) - domain_min;
4072 HOST_WIDE_INT hi_index
4073 = TREE_INT_CST_LOW (TREE_VALUE (vals)) - domain_min;
4074 if (lo_index < 0 || lo_index >= bit_size
4075 || hi_index < 0 || hi_index >= bit_size)
4076 abort ();
4077 for ( ; lo_index <= hi_index; lo_index++)
4078 buffer[lo_index] = 1;
4080 else
4082 /* Set a single bit to one. */
4083 HOST_WIDE_INT index
4084 = TREE_INT_CST_LOW (TREE_VALUE (vals)) - domain_min;
4085 if (index < 0 || index >= bit_size)
4087 error ("invalid initializer for bit string");
4088 return NULL_TREE;
4090 buffer[index] = 1;
4093 return non_const_bits;
4096 /* Expand (the constant part of) a SET_TYPE CONTRUCTOR node.
4097 The result is placed in BUFFER (which is an array of WD_SIZE
4098 words). TYPE_ALIGN bits are stored in each element of BUFFER.
4099 If the constructor is constant, NULL_TREE is returned.
4100 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4102 tree
4103 get_set_constructor_words (init, buffer, wd_size)
4104 tree init;
4105 HOST_WIDE_INT *buffer;
4106 int wd_size;
4108 int i;
4109 tree vals = TREE_OPERAND (init, 1);
4110 int set_word_size = TYPE_ALIGN (TREE_TYPE (init));
4111 int bit_size = wd_size * set_word_size;
4112 int bit_pos = 0;
4113 HOST_WIDE_INT *wordp = buffer;
4114 char *bit_buffer = (char*)alloca(bit_size);
4115 tree non_const_bits = get_set_constructor_bits (init, bit_buffer, bit_size);
4117 for (i = 0; i < wd_size; i++)
4118 buffer[i] = 0;
4120 for (i = 0; i < bit_size; i++)
4122 if (bit_buffer[i])
4124 if (BITS_BIG_ENDIAN)
4125 *wordp |= (1 << (set_word_size - 1 - bit_pos));
4126 else
4127 *wordp |= 1 << bit_pos;
4129 bit_pos++;
4130 if (bit_pos >= set_word_size)
4131 bit_pos = 0, wordp++;
4133 return non_const_bits;