(expand_value_return): Make function static.
[official-gcc.git] / gcc / tree.c
blobe26819f56023686988987411804bd7e6d3481fc1
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 88, 92, 93, 94, 1995 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 ();
268 /* Init the principal obstacks. */
270 void
271 init_obstacks ()
273 gcc_obstack_init (&obstack_stack_obstack);
274 gcc_obstack_init (&permanent_obstack);
276 gcc_obstack_init (&temporary_obstack);
277 temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
278 gcc_obstack_init (&momentary_obstack);
279 momentary_firstobj = (char *) obstack_alloc (&momentary_obstack, 0);
280 momentary_function_firstobj = momentary_firstobj;
281 gcc_obstack_init (&maybepermanent_obstack);
282 maybepermanent_firstobj
283 = (char *) obstack_alloc (&maybepermanent_obstack, 0);
284 gcc_obstack_init (&temp_decl_obstack);
285 temp_decl_firstobj = (char *) obstack_alloc (&temp_decl_obstack, 0);
287 function_obstack = &temporary_obstack;
288 function_maybepermanent_obstack = &maybepermanent_obstack;
289 current_obstack = &permanent_obstack;
290 expression_obstack = &permanent_obstack;
291 rtl_obstack = saveable_obstack = &permanent_obstack;
293 /* Init the hash table of identifiers. */
294 bzero ((char *) hash_table, sizeof hash_table);
297 void
298 gcc_obstack_init (obstack)
299 struct obstack *obstack;
301 /* Let particular systems override the size of a chunk. */
302 #ifndef OBSTACK_CHUNK_SIZE
303 #define OBSTACK_CHUNK_SIZE 0
304 #endif
305 /* Let them override the alloc and free routines too. */
306 #ifndef OBSTACK_CHUNK_ALLOC
307 #define OBSTACK_CHUNK_ALLOC xmalloc
308 #endif
309 #ifndef OBSTACK_CHUNK_FREE
310 #define OBSTACK_CHUNK_FREE free
311 #endif
312 _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
313 (void *(*) ()) OBSTACK_CHUNK_ALLOC,
314 (void (*) ()) OBSTACK_CHUNK_FREE);
317 /* Save all variables describing the current status into the structure *P.
318 This is used before starting a nested function. */
320 void
321 save_tree_status (p, toplevel)
322 struct function *p;
323 int toplevel;
325 p->all_types_permanent = all_types_permanent;
326 p->momentary_stack = momentary_stack;
327 p->maybepermanent_firstobj = maybepermanent_firstobj;
328 p->momentary_firstobj = momentary_firstobj;
329 p->momentary_function_firstobj = momentary_function_firstobj;
330 p->function_obstack = function_obstack;
331 p->function_maybepermanent_obstack = function_maybepermanent_obstack;
332 p->current_obstack = current_obstack;
333 p->expression_obstack = expression_obstack;
334 p->saveable_obstack = saveable_obstack;
335 p->rtl_obstack = rtl_obstack;
337 if (! toplevel)
339 /* Objects that need to be saved in this function can be in the nonsaved
340 obstack of the enclosing function since they can't possibly be needed
341 once it has returned. */
342 function_maybepermanent_obstack = function_obstack;
343 maybepermanent_firstobj
344 = (char *) obstack_finish (function_maybepermanent_obstack);
347 function_obstack = (struct obstack *) xmalloc (sizeof (struct obstack));
348 gcc_obstack_init (function_obstack);
350 current_obstack = &permanent_obstack;
351 expression_obstack = &permanent_obstack;
352 rtl_obstack = saveable_obstack = &permanent_obstack;
354 momentary_firstobj = (char *) obstack_finish (&momentary_obstack);
355 momentary_function_firstobj = momentary_firstobj;
358 /* Restore all variables describing the current status from the structure *P.
359 This is used after a nested function. */
361 void
362 restore_tree_status (p, toplevel)
363 struct function *p;
364 int toplevel;
366 all_types_permanent = p->all_types_permanent;
367 momentary_stack = p->momentary_stack;
369 obstack_free (&momentary_obstack, momentary_function_firstobj);
371 if (! toplevel)
373 /* Free saveable storage used by the function just compiled and not
374 saved.
376 CAUTION: This is in function_obstack of the containing function.
377 So we must be sure that we never allocate from that obstack during
378 the compilation of a nested function if we expect it to survive
379 past the nested function's end. */
380 obstack_free (function_maybepermanent_obstack, maybepermanent_firstobj);
383 obstack_free (function_obstack, 0);
384 free (function_obstack);
386 momentary_firstobj = p->momentary_firstobj;
387 momentary_function_firstobj = p->momentary_function_firstobj;
388 maybepermanent_firstobj = p->maybepermanent_firstobj;
389 function_obstack = p->function_obstack;
390 function_maybepermanent_obstack = p->function_maybepermanent_obstack;
391 current_obstack = p->current_obstack;
392 expression_obstack = p->expression_obstack;
393 saveable_obstack = p->saveable_obstack;
394 rtl_obstack = p->rtl_obstack;
397 /* Start allocating on the temporary (per function) obstack.
398 This is done in start_function before parsing the function body,
399 and before each initialization at top level, and to go back
400 to temporary allocation after doing permanent_allocation. */
402 void
403 temporary_allocation ()
405 /* Note that function_obstack at top level points to temporary_obstack.
406 But within a nested function context, it is a separate obstack. */
407 current_obstack = function_obstack;
408 expression_obstack = function_obstack;
409 rtl_obstack = saveable_obstack = function_maybepermanent_obstack;
410 momentary_stack = 0;
413 /* Start allocating on the permanent obstack but don't
414 free the temporary data. After calling this, call
415 `permanent_allocation' to fully resume permanent allocation status. */
417 void
418 end_temporary_allocation ()
420 current_obstack = &permanent_obstack;
421 expression_obstack = &permanent_obstack;
422 rtl_obstack = saveable_obstack = &permanent_obstack;
425 /* Resume allocating on the temporary obstack, undoing
426 effects of `end_temporary_allocation'. */
428 void
429 resume_temporary_allocation ()
431 current_obstack = function_obstack;
432 expression_obstack = function_obstack;
433 rtl_obstack = saveable_obstack = function_maybepermanent_obstack;
436 /* While doing temporary allocation, switch to allocating in such a
437 way as to save all nodes if the function is inlined. Call
438 resume_temporary_allocation to go back to ordinary temporary
439 allocation. */
441 void
442 saveable_allocation ()
444 /* Note that function_obstack at top level points to temporary_obstack.
445 But within a nested function context, it is a separate obstack. */
446 expression_obstack = current_obstack = saveable_obstack;
449 /* Switch to current obstack CURRENT and maybepermanent obstack SAVEABLE,
450 recording the previously current obstacks on a stack.
451 This does not free any storage in any obstack. */
453 void
454 push_obstacks (current, saveable)
455 struct obstack *current, *saveable;
457 struct obstack_stack *p
458 = (struct obstack_stack *) obstack_alloc (&obstack_stack_obstack,
459 (sizeof (struct obstack_stack)));
461 p->current = current_obstack;
462 p->saveable = saveable_obstack;
463 p->expression = expression_obstack;
464 p->rtl = rtl_obstack;
465 p->next = obstack_stack;
466 obstack_stack = p;
468 current_obstack = current;
469 expression_obstack = current;
470 rtl_obstack = saveable_obstack = saveable;
473 /* Save the current set of obstacks, but don't change them. */
475 void
476 push_obstacks_nochange ()
478 struct obstack_stack *p
479 = (struct obstack_stack *) obstack_alloc (&obstack_stack_obstack,
480 (sizeof (struct obstack_stack)));
482 p->current = current_obstack;
483 p->saveable = saveable_obstack;
484 p->expression = expression_obstack;
485 p->rtl = rtl_obstack;
486 p->next = obstack_stack;
487 obstack_stack = p;
490 /* Pop the obstack selection stack. */
492 void
493 pop_obstacks ()
495 struct obstack_stack *p = obstack_stack;
496 obstack_stack = p->next;
498 current_obstack = p->current;
499 saveable_obstack = p->saveable;
500 expression_obstack = p->expression;
501 rtl_obstack = p->rtl;
503 obstack_free (&obstack_stack_obstack, p);
506 /* Nonzero if temporary allocation is currently in effect.
507 Zero if currently doing permanent allocation. */
510 allocation_temporary_p ()
512 return current_obstack != &permanent_obstack;
515 /* Go back to allocating on the permanent obstack
516 and free everything in the temporary obstack.
518 FUNCTION_END is true only if we have just finished compiling a function.
519 In that case, we also free preserved initial values on the momentary
520 obstack. */
522 void
523 permanent_allocation (function_end)
524 int function_end;
526 /* Free up previous temporary obstack data */
527 obstack_free (&temporary_obstack, temporary_firstobj);
528 if (function_end)
530 obstack_free (&momentary_obstack, momentary_function_firstobj);
531 momentary_firstobj = momentary_function_firstobj;
533 else
534 obstack_free (&momentary_obstack, momentary_firstobj);
535 obstack_free (function_maybepermanent_obstack, maybepermanent_firstobj);
536 obstack_free (&temp_decl_obstack, temp_decl_firstobj);
538 current_obstack = &permanent_obstack;
539 expression_obstack = &permanent_obstack;
540 rtl_obstack = saveable_obstack = &permanent_obstack;
543 /* Save permanently everything on the maybepermanent_obstack. */
545 void
546 preserve_data ()
548 maybepermanent_firstobj
549 = (char *) obstack_alloc (function_maybepermanent_obstack, 0);
552 void
553 preserve_initializer ()
555 struct momentary_level *tem;
556 char *old_momentary;
558 temporary_firstobj
559 = (char *) obstack_alloc (&temporary_obstack, 0);
560 maybepermanent_firstobj
561 = (char *) obstack_alloc (function_maybepermanent_obstack, 0);
563 old_momentary = momentary_firstobj;
564 momentary_firstobj
565 = (char *) obstack_alloc (&momentary_obstack, 0);
566 if (momentary_firstobj != old_momentary)
567 for (tem = momentary_stack; tem; tem = tem->prev)
568 tem->base = momentary_firstobj;
571 /* Start allocating new rtl in current_obstack.
572 Use resume_temporary_allocation
573 to go back to allocating rtl in saveable_obstack. */
575 void
576 rtl_in_current_obstack ()
578 rtl_obstack = current_obstack;
581 /* Start allocating rtl from saveable_obstack. Intended to be used after
582 a call to push_obstacks_nochange. */
584 void
585 rtl_in_saveable_obstack ()
587 rtl_obstack = saveable_obstack;
590 /* Allocate SIZE bytes in the current obstack
591 and return a pointer to them.
592 In practice the current obstack is always the temporary one. */
594 char *
595 oballoc (size)
596 int size;
598 return (char *) obstack_alloc (current_obstack, size);
601 /* Free the object PTR in the current obstack
602 as well as everything allocated since PTR.
603 In practice the current obstack is always the temporary one. */
605 void
606 obfree (ptr)
607 char *ptr;
609 obstack_free (current_obstack, ptr);
612 /* Allocate SIZE bytes in the permanent obstack
613 and return a pointer to them. */
615 char *
616 permalloc (size)
617 int size;
619 return (char *) obstack_alloc (&permanent_obstack, size);
622 /* Allocate NELEM items of SIZE bytes in the permanent obstack
623 and return a pointer to them. The storage is cleared before
624 returning the value. */
626 char *
627 perm_calloc (nelem, size)
628 int nelem;
629 long size;
631 char *rval = (char *) obstack_alloc (&permanent_obstack, nelem * size);
632 bzero (rval, nelem * size);
633 return rval;
636 /* Allocate SIZE bytes in the saveable obstack
637 and return a pointer to them. */
639 char *
640 savealloc (size)
641 int size;
643 return (char *) obstack_alloc (saveable_obstack, size);
646 /* Print out which obstack an object is in. */
648 void
649 print_obstack_name (object, file, prefix)
650 char *object;
651 FILE *file;
652 char *prefix;
654 struct obstack *obstack = NULL;
655 char *obstack_name = NULL;
656 struct function *p;
658 for (p = outer_function_chain; p; p = p->next)
660 if (_obstack_allocated_p (p->function_obstack, object))
662 obstack = p->function_obstack;
663 obstack_name = "containing function obstack";
665 if (_obstack_allocated_p (p->function_maybepermanent_obstack, object))
667 obstack = p->function_maybepermanent_obstack;
668 obstack_name = "containing function maybepermanent obstack";
672 if (_obstack_allocated_p (&obstack_stack_obstack, object))
674 obstack = &obstack_stack_obstack;
675 obstack_name = "obstack_stack_obstack";
677 else if (_obstack_allocated_p (function_obstack, object))
679 obstack = function_obstack;
680 obstack_name = "function obstack";
682 else if (_obstack_allocated_p (&permanent_obstack, object))
684 obstack = &permanent_obstack;
685 obstack_name = "permanent_obstack";
687 else if (_obstack_allocated_p (&momentary_obstack, object))
689 obstack = &momentary_obstack;
690 obstack_name = "momentary_obstack";
692 else if (_obstack_allocated_p (function_maybepermanent_obstack, object))
694 obstack = function_maybepermanent_obstack;
695 obstack_name = "function maybepermanent obstack";
697 else if (_obstack_allocated_p (&temp_decl_obstack, object))
699 obstack = &temp_decl_obstack;
700 obstack_name = "temp_decl_obstack";
703 /* Check to see if the object is in the free area of the obstack. */
704 if (obstack != NULL)
706 if (object >= obstack->next_free
707 && object < obstack->chunk_limit)
708 fprintf (file, "%s in free portion of obstack %s",
709 prefix, obstack_name);
710 else
711 fprintf (file, "%s allocated from %s", prefix, obstack_name);
713 else
714 fprintf (file, "%s not allocated from any obstack", prefix);
717 void
718 debug_obstack (object)
719 char *object;
721 print_obstack_name (object, stderr, "object");
722 fprintf (stderr, ".\n");
725 /* Return 1 if OBJ is in the permanent obstack.
726 This is slow, and should be used only for debugging.
727 Use TREE_PERMANENT for other purposes. */
730 object_permanent_p (obj)
731 tree obj;
733 return _obstack_allocated_p (&permanent_obstack, obj);
736 /* Start a level of momentary allocation.
737 In C, each compound statement has its own level
738 and that level is freed at the end of each statement.
739 All expression nodes are allocated in the momentary allocation level. */
741 void
742 push_momentary ()
744 struct momentary_level *tem
745 = (struct momentary_level *) obstack_alloc (&momentary_obstack,
746 sizeof (struct momentary_level));
747 tem->prev = momentary_stack;
748 tem->base = (char *) obstack_base (&momentary_obstack);
749 tem->obstack = expression_obstack;
750 momentary_stack = tem;
751 expression_obstack = &momentary_obstack;
754 /* Set things up so the next clear_momentary will only clear memory
755 past our present position in momentary_obstack. */
757 void
758 preserve_momentary ()
760 momentary_stack->base = (char *) obstack_base (&momentary_obstack);
763 /* Free all the storage in the current momentary-allocation level.
764 In C, this happens at the end of each statement. */
766 void
767 clear_momentary ()
769 obstack_free (&momentary_obstack, momentary_stack->base);
772 /* Discard a level of momentary allocation.
773 In C, this happens at the end of each compound statement.
774 Restore the status of expression node allocation
775 that was in effect before this level was created. */
777 void
778 pop_momentary ()
780 struct momentary_level *tem = momentary_stack;
781 momentary_stack = tem->prev;
782 expression_obstack = tem->obstack;
783 /* We can't free TEM from the momentary_obstack, because there might
784 be objects above it which have been saved. We can free back to the
785 stack of the level we are popping off though. */
786 obstack_free (&momentary_obstack, tem->base);
789 /* Pop back to the previous level of momentary allocation,
790 but don't free any momentary data just yet. */
792 void
793 pop_momentary_nofree ()
795 struct momentary_level *tem = momentary_stack;
796 momentary_stack = tem->prev;
797 expression_obstack = tem->obstack;
800 /* Call when starting to parse a declaration:
801 make expressions in the declaration last the length of the function.
802 Returns an argument that should be passed to resume_momentary later. */
805 suspend_momentary ()
807 register int tem = expression_obstack == &momentary_obstack;
808 expression_obstack = saveable_obstack;
809 return tem;
812 /* Call when finished parsing a declaration:
813 restore the treatment of node-allocation that was
814 in effect before the suspension.
815 YES should be the value previously returned by suspend_momentary. */
817 void
818 resume_momentary (yes)
819 int yes;
821 if (yes)
822 expression_obstack = &momentary_obstack;
825 /* Init the tables indexed by tree code.
826 Note that languages can add to these tables to define their own codes. */
828 void
829 init_tree_codes ()
831 tree_code_type = (char **) xmalloc (sizeof (standard_tree_code_type));
832 tree_code_length = (int *) xmalloc (sizeof (standard_tree_code_length));
833 tree_code_name = (char **) xmalloc (sizeof (standard_tree_code_name));
834 bcopy ((char *) standard_tree_code_type, (char *) tree_code_type,
835 sizeof (standard_tree_code_type));
836 bcopy ((char *) standard_tree_code_length, (char *) tree_code_length,
837 sizeof (standard_tree_code_length));
838 bcopy ((char *) standard_tree_code_name, (char *) tree_code_name,
839 sizeof (standard_tree_code_name));
842 /* Return a newly allocated node of code CODE.
843 Initialize the node's unique id and its TREE_PERMANENT flag.
844 For decl and type nodes, some other fields are initialized.
845 The rest of the node is initialized to zero.
847 Achoo! I got a code in the node. */
849 tree
850 make_node (code)
851 enum tree_code code;
853 register tree t;
854 register int type = TREE_CODE_CLASS (code);
855 register int length;
856 register struct obstack *obstack = current_obstack;
857 register int i;
858 register tree_node_kind kind;
860 switch (type)
862 case 'd': /* A decl node */
863 #ifdef GATHER_STATISTICS
864 kind = d_kind;
865 #endif
866 length = sizeof (struct tree_decl);
867 /* All decls in an inline function need to be saved. */
868 if (obstack != &permanent_obstack)
869 obstack = saveable_obstack;
871 /* PARM_DECLs go on the context of the parent. If this is a nested
872 function, then we must allocate the PARM_DECL on the parent's
873 obstack, so that they will live to the end of the parent's
874 closing brace. This is neccesary in case we try to inline the
875 function into its parent.
877 PARM_DECLs of top-level functions do not have this problem. However,
878 we allocate them where we put the FUNCTION_DECL for languauges such as
879 Ada that need to consult some flags in the PARM_DECLs of the function
880 when calling it.
882 See comment in restore_tree_status for why we can't put this
883 in function_obstack. */
884 if (code == PARM_DECL && obstack != &permanent_obstack)
886 tree context = 0;
887 if (current_function_decl)
888 context = decl_function_context (current_function_decl);
890 if (context)
891 obstack
892 = find_function_data (context)->function_maybepermanent_obstack;
894 break;
896 case 't': /* a type node */
897 #ifdef GATHER_STATISTICS
898 kind = t_kind;
899 #endif
900 length = sizeof (struct tree_type);
901 /* All data types are put where we can preserve them if nec. */
902 if (obstack != &permanent_obstack)
903 obstack = all_types_permanent ? &permanent_obstack : saveable_obstack;
904 break;
906 case 'b': /* a lexical block */
907 #ifdef GATHER_STATISTICS
908 kind = b_kind;
909 #endif
910 length = sizeof (struct tree_block);
911 /* All BLOCK nodes are put where we can preserve them if nec. */
912 if (obstack != &permanent_obstack)
913 obstack = saveable_obstack;
914 break;
916 case 's': /* an expression with side effects */
917 #ifdef GATHER_STATISTICS
918 kind = s_kind;
919 goto usual_kind;
920 #endif
921 case 'r': /* a reference */
922 #ifdef GATHER_STATISTICS
923 kind = r_kind;
924 goto usual_kind;
925 #endif
926 case 'e': /* an expression */
927 case '<': /* a comparison expression */
928 case '1': /* a unary arithmetic expression */
929 case '2': /* a binary arithmetic expression */
930 #ifdef GATHER_STATISTICS
931 kind = e_kind;
932 usual_kind:
933 #endif
934 obstack = expression_obstack;
935 /* All BIND_EXPR nodes are put where we can preserve them if nec. */
936 if (code == BIND_EXPR && obstack != &permanent_obstack)
937 obstack = saveable_obstack;
938 length = sizeof (struct tree_exp)
939 + (tree_code_length[(int) code] - 1) * sizeof (char *);
940 break;
942 case 'c': /* a constant */
943 #ifdef GATHER_STATISTICS
944 kind = c_kind;
945 #endif
946 obstack = expression_obstack;
948 /* We can't use tree_code_length for INTEGER_CST, since the number of
949 words is machine-dependent due to varying length of HOST_WIDE_INT,
950 which might be wider than a pointer (e.g., long long). Similarly
951 for REAL_CST, since the number of words is machine-dependent due
952 to varying size and alignment of `double'. */
954 if (code == INTEGER_CST)
955 length = sizeof (struct tree_int_cst);
956 else if (code == REAL_CST)
957 length = sizeof (struct tree_real_cst);
958 else
959 length = sizeof (struct tree_common)
960 + tree_code_length[(int) code] * sizeof (char *);
961 break;
963 case 'x': /* something random, like an identifier. */
964 #ifdef GATHER_STATISTICS
965 if (code == IDENTIFIER_NODE)
966 kind = id_kind;
967 else if (code == OP_IDENTIFIER)
968 kind = op_id_kind;
969 else if (code == TREE_VEC)
970 kind = vec_kind;
971 else
972 kind = x_kind;
973 #endif
974 length = sizeof (struct tree_common)
975 + tree_code_length[(int) code] * sizeof (char *);
976 /* Identifier nodes are always permanent since they are
977 unique in a compiler run. */
978 if (code == IDENTIFIER_NODE) obstack = &permanent_obstack;
979 break;
981 default:
982 abort ();
985 t = (tree) obstack_alloc (obstack, length);
987 #ifdef GATHER_STATISTICS
988 tree_node_counts[(int)kind]++;
989 tree_node_sizes[(int)kind] += length;
990 #endif
992 /* Clear a word at a time. */
993 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
994 ((int *) t)[i] = 0;
995 /* Clear any extra bytes. */
996 for (i = length / sizeof (int) * sizeof (int); i < length; i++)
997 ((char *) t)[i] = 0;
999 TREE_SET_CODE (t, code);
1000 if (obstack == &permanent_obstack)
1001 TREE_PERMANENT (t) = 1;
1003 switch (type)
1005 case 's':
1006 TREE_SIDE_EFFECTS (t) = 1;
1007 TREE_TYPE (t) = void_type_node;
1008 break;
1010 case 'd':
1011 if (code != FUNCTION_DECL)
1012 DECL_ALIGN (t) = 1;
1013 DECL_IN_SYSTEM_HEADER (t)
1014 = in_system_header && (obstack == &permanent_obstack);
1015 DECL_SOURCE_LINE (t) = lineno;
1016 DECL_SOURCE_FILE (t) = (input_filename) ? input_filename : "<built-in>";
1017 DECL_UID (t) = next_decl_uid++;
1018 break;
1020 case 't':
1021 TYPE_UID (t) = next_type_uid++;
1022 TYPE_ALIGN (t) = 1;
1023 TYPE_MAIN_VARIANT (t) = t;
1024 TYPE_OBSTACK (t) = obstack;
1025 TYPE_ATTRIBUTES (t) = NULL_TREE;
1026 #ifdef SET_DEFAULT_TYPE_ATTRIBUTES
1027 SET_DEFAULT_TYPE_ATTRIBUTES (t);
1028 #endif
1029 break;
1031 case 'c':
1032 TREE_CONSTANT (t) = 1;
1033 break;
1036 return t;
1039 /* Return a new node with the same contents as NODE
1040 except that its TREE_CHAIN is zero and it has a fresh uid. */
1042 tree
1043 copy_node (node)
1044 tree node;
1046 register tree t;
1047 register enum tree_code code = TREE_CODE (node);
1048 register int length;
1049 register int i;
1051 switch (TREE_CODE_CLASS (code))
1053 case 'd': /* A decl node */
1054 length = sizeof (struct tree_decl);
1055 break;
1057 case 't': /* a type node */
1058 length = sizeof (struct tree_type);
1059 break;
1061 case 'b': /* a lexical block node */
1062 length = sizeof (struct tree_block);
1063 break;
1065 case 'r': /* a reference */
1066 case 'e': /* an expression */
1067 case 's': /* an expression with side effects */
1068 case '<': /* a comparison expression */
1069 case '1': /* a unary arithmetic expression */
1070 case '2': /* a binary arithmetic expression */
1071 length = sizeof (struct tree_exp)
1072 + (tree_code_length[(int) code] - 1) * sizeof (char *);
1073 break;
1075 case 'c': /* a constant */
1076 /* We can't use tree_code_length for INTEGER_CST, since the number of
1077 words is machine-dependent due to varying length of HOST_WIDE_INT,
1078 which might be wider than a pointer (e.g., long long). Similarly
1079 for REAL_CST, since the number of words is machine-dependent due
1080 to varying size and alignment of `double'. */
1081 if (code == INTEGER_CST)
1083 length = sizeof (struct tree_int_cst);
1084 break;
1086 else if (code == REAL_CST)
1088 length = sizeof (struct tree_real_cst);
1089 break;
1092 case 'x': /* something random, like an identifier. */
1093 length = sizeof (struct tree_common)
1094 + tree_code_length[(int) code] * sizeof (char *);
1095 if (code == TREE_VEC)
1096 length += (TREE_VEC_LENGTH (node) - 1) * sizeof (char *);
1099 t = (tree) obstack_alloc (current_obstack, length);
1101 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
1102 ((int *) t)[i] = ((int *) node)[i];
1103 /* Clear any extra bytes. */
1104 for (i = length / sizeof (int) * sizeof (int); i < length; i++)
1105 ((char *) t)[i] = ((char *) node)[i];
1107 TREE_CHAIN (t) = 0;
1109 if (TREE_CODE_CLASS (code) == 'd')
1110 DECL_UID (t) = next_decl_uid++;
1111 else if (TREE_CODE_CLASS (code) == 't')
1113 TYPE_UID (t) = next_type_uid++;
1114 TYPE_OBSTACK (t) = current_obstack;
1117 TREE_PERMANENT (t) = (current_obstack == &permanent_obstack);
1119 return t;
1122 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1123 For example, this can copy a list made of TREE_LIST nodes. */
1125 tree
1126 copy_list (list)
1127 tree list;
1129 tree head;
1130 register tree prev, next;
1132 if (list == 0)
1133 return 0;
1135 head = prev = copy_node (list);
1136 next = TREE_CHAIN (list);
1137 while (next)
1139 TREE_CHAIN (prev) = copy_node (next);
1140 prev = TREE_CHAIN (prev);
1141 next = TREE_CHAIN (next);
1143 return head;
1146 #define HASHBITS 30
1148 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
1149 If an identifier with that name has previously been referred to,
1150 the same node is returned this time. */
1152 tree
1153 get_identifier (text)
1154 register char *text;
1156 register int hi;
1157 register int i;
1158 register tree idp;
1159 register int len, hash_len;
1161 /* Compute length of text in len. */
1162 for (len = 0; text[len]; len++);
1164 /* Decide how much of that length to hash on */
1165 hash_len = len;
1166 if (warn_id_clash && len > id_clash_len)
1167 hash_len = id_clash_len;
1169 /* Compute hash code */
1170 hi = hash_len * 613 + (unsigned)text[0];
1171 for (i = 1; i < hash_len; i += 2)
1172 hi = ((hi * 613) + (unsigned)(text[i]));
1174 hi &= (1 << HASHBITS) - 1;
1175 hi %= MAX_HASH_TABLE;
1177 /* Search table for identifier */
1178 for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
1179 if (IDENTIFIER_LENGTH (idp) == len
1180 && IDENTIFIER_POINTER (idp)[0] == text[0]
1181 && !bcmp (IDENTIFIER_POINTER (idp), text, len))
1182 return idp; /* <-- return if found */
1184 /* Not found; optionally warn about a similar identifier */
1185 if (warn_id_clash && do_identifier_warnings && len >= id_clash_len)
1186 for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
1187 if (!strncmp (IDENTIFIER_POINTER (idp), text, id_clash_len))
1189 warning ("`%s' and `%s' identical in first %d characters",
1190 IDENTIFIER_POINTER (idp), text, id_clash_len);
1191 break;
1194 if (tree_code_length[(int) IDENTIFIER_NODE] < 0)
1195 abort (); /* set_identifier_size hasn't been called. */
1197 /* Not found, create one, add to chain */
1198 idp = make_node (IDENTIFIER_NODE);
1199 IDENTIFIER_LENGTH (idp) = len;
1200 #ifdef GATHER_STATISTICS
1201 id_string_size += len;
1202 #endif
1204 IDENTIFIER_POINTER (idp) = obstack_copy0 (&permanent_obstack, text, len);
1206 TREE_CHAIN (idp) = hash_table[hi];
1207 hash_table[hi] = idp;
1208 return idp; /* <-- return if created */
1211 /* Enable warnings on similar identifiers (if requested).
1212 Done after the built-in identifiers are created. */
1214 void
1215 start_identifier_warnings ()
1217 do_identifier_warnings = 1;
1220 /* Record the size of an identifier node for the language in use.
1221 SIZE is the total size in bytes.
1222 This is called by the language-specific files. This must be
1223 called before allocating any identifiers. */
1225 void
1226 set_identifier_size (size)
1227 int size;
1229 tree_code_length[(int) IDENTIFIER_NODE]
1230 = (size - sizeof (struct tree_common)) / sizeof (tree);
1233 /* Return a newly constructed INTEGER_CST node whose constant value
1234 is specified by the two ints LOW and HI.
1235 The TREE_TYPE is set to `int'.
1237 This function should be used via the `build_int_2' macro. */
1239 tree
1240 build_int_2_wide (low, hi)
1241 HOST_WIDE_INT low, hi;
1243 register tree t = make_node (INTEGER_CST);
1244 TREE_INT_CST_LOW (t) = low;
1245 TREE_INT_CST_HIGH (t) = hi;
1246 TREE_TYPE (t) = integer_type_node;
1247 return t;
1250 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1252 tree
1253 build_real (type, d)
1254 tree type;
1255 REAL_VALUE_TYPE d;
1257 tree v;
1258 int overflow = 0;
1260 /* Check for valid float value for this type on this target machine;
1261 if not, can print error message and store a valid value in D. */
1262 #ifdef CHECK_FLOAT_VALUE
1263 CHECK_FLOAT_VALUE (TYPE_MODE (type), d, overflow);
1264 #endif
1266 v = make_node (REAL_CST);
1267 TREE_TYPE (v) = type;
1268 TREE_REAL_CST (v) = d;
1269 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
1270 return v;
1273 /* Return a new REAL_CST node whose type is TYPE
1274 and whose value is the integer value of the INTEGER_CST node I. */
1276 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1278 REAL_VALUE_TYPE
1279 real_value_from_int_cst (i)
1280 tree i;
1282 REAL_VALUE_TYPE d;
1283 REAL_VALUE_TYPE e;
1284 /* Some 386 compilers mishandle unsigned int to float conversions,
1285 so introduce a temporary variable E to avoid those bugs. */
1287 #ifdef REAL_ARITHMETIC
1288 if (! TREE_UNSIGNED (TREE_TYPE (i)))
1289 REAL_VALUE_FROM_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i));
1290 else
1291 REAL_VALUE_FROM_UNSIGNED_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i));
1292 #else /* not REAL_ARITHMETIC */
1293 if (TREE_INT_CST_HIGH (i) < 0 && ! TREE_UNSIGNED (TREE_TYPE (i)))
1295 d = (double) (~ TREE_INT_CST_HIGH (i));
1296 e = ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
1297 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
1298 d *= e;
1299 e = (double) (unsigned HOST_WIDE_INT) (~ TREE_INT_CST_LOW (i));
1300 d += e;
1301 d = (- d - 1.0);
1303 else
1305 d = (double) (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (i);
1306 e = ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
1307 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
1308 d *= e;
1309 e = (double) (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (i);
1310 d += e;
1312 #endif /* not REAL_ARITHMETIC */
1313 return d;
1316 /* This function can't be implemented if we can't do arithmetic
1317 on the float representation. */
1319 tree
1320 build_real_from_int_cst (type, i)
1321 tree type;
1322 tree i;
1324 tree v;
1325 int overflow = TREE_OVERFLOW (i);
1326 REAL_VALUE_TYPE d;
1327 jmp_buf float_error;
1329 v = make_node (REAL_CST);
1330 TREE_TYPE (v) = type;
1332 if (setjmp (float_error))
1334 d = dconst0;
1335 overflow = 1;
1336 goto got_it;
1339 set_float_handler (float_error);
1341 d = REAL_VALUE_TRUNCATE (TYPE_MODE (type), real_value_from_int_cst (i));
1343 /* Check for valid float value for this type on this target machine. */
1345 got_it:
1346 set_float_handler (NULL_PTR);
1348 #ifdef CHECK_FLOAT_VALUE
1349 CHECK_FLOAT_VALUE (TYPE_MODE (type), d, overflow);
1350 #endif
1352 TREE_REAL_CST (v) = d;
1353 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
1354 return v;
1357 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1359 /* Return a newly constructed STRING_CST node whose value is
1360 the LEN characters at STR.
1361 The TREE_TYPE is not initialized. */
1363 tree
1364 build_string (len, str)
1365 int len;
1366 char *str;
1368 /* Put the string in saveable_obstack since it will be placed in the RTL
1369 for an "asm" statement and will also be kept around a while if
1370 deferring constant output in varasm.c. */
1372 register tree s = make_node (STRING_CST);
1373 TREE_STRING_LENGTH (s) = len;
1374 TREE_STRING_POINTER (s) = obstack_copy0 (saveable_obstack, str, len);
1375 return s;
1378 /* Return a newly constructed COMPLEX_CST node whose value is
1379 specified by the real and imaginary parts REAL and IMAG.
1380 Both REAL and IMAG should be constant nodes.
1381 The TREE_TYPE is not initialized. */
1383 tree
1384 build_complex (real, imag)
1385 tree real, imag;
1387 register tree t = make_node (COMPLEX_CST);
1389 TREE_REALPART (t) = real;
1390 TREE_IMAGPART (t) = imag;
1391 TREE_TYPE (t) = build_complex_type (TREE_TYPE (real));
1392 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1393 TREE_CONSTANT_OVERFLOW (t)
1394 = TREE_CONSTANT_OVERFLOW (real) | TREE_CONSTANT_OVERFLOW (imag);
1395 return t;
1398 /* Build a newly constructed TREE_VEC node of length LEN. */
1399 tree
1400 make_tree_vec (len)
1401 int len;
1403 register tree t;
1404 register int length = (len-1) * sizeof (tree) + sizeof (struct tree_vec);
1405 register struct obstack *obstack = current_obstack;
1406 register int i;
1408 #ifdef GATHER_STATISTICS
1409 tree_node_counts[(int)vec_kind]++;
1410 tree_node_sizes[(int)vec_kind] += length;
1411 #endif
1413 t = (tree) obstack_alloc (obstack, length);
1415 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
1416 ((int *) t)[i] = 0;
1418 TREE_SET_CODE (t, TREE_VEC);
1419 TREE_VEC_LENGTH (t) = len;
1420 if (obstack == &permanent_obstack)
1421 TREE_PERMANENT (t) = 1;
1423 return t;
1426 /* Return 1 if EXPR is the integer constant zero or a complex constant
1427 of zero. */
1430 integer_zerop (expr)
1431 tree expr;
1433 STRIP_NOPS (expr);
1435 return ((TREE_CODE (expr) == INTEGER_CST
1436 && TREE_INT_CST_LOW (expr) == 0
1437 && TREE_INT_CST_HIGH (expr) == 0)
1438 || (TREE_CODE (expr) == COMPLEX_CST
1439 && integer_zerop (TREE_REALPART (expr))
1440 && integer_zerop (TREE_IMAGPART (expr))));
1443 /* Return 1 if EXPR is the integer constant one or the corresponding
1444 complex constant. */
1447 integer_onep (expr)
1448 tree expr;
1450 STRIP_NOPS (expr);
1452 return ((TREE_CODE (expr) == INTEGER_CST
1453 && TREE_INT_CST_LOW (expr) == 1
1454 && TREE_INT_CST_HIGH (expr) == 0)
1455 || (TREE_CODE (expr) == COMPLEX_CST
1456 && integer_onep (TREE_REALPART (expr))
1457 && integer_zerop (TREE_IMAGPART (expr))));
1460 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1461 it contains. Likewise for the corresponding complex constant. */
1464 integer_all_onesp (expr)
1465 tree expr;
1467 register int prec;
1468 register int uns;
1470 STRIP_NOPS (expr);
1472 if (TREE_CODE (expr) == COMPLEX_CST
1473 && integer_all_onesp (TREE_REALPART (expr))
1474 && integer_zerop (TREE_IMAGPART (expr)))
1475 return 1;
1477 else if (TREE_CODE (expr) != INTEGER_CST)
1478 return 0;
1480 uns = TREE_UNSIGNED (TREE_TYPE (expr));
1481 if (!uns)
1482 return TREE_INT_CST_LOW (expr) == -1 && TREE_INT_CST_HIGH (expr) == -1;
1484 prec = TYPE_PRECISION (TREE_TYPE (expr));
1485 if (prec >= HOST_BITS_PER_WIDE_INT)
1487 int high_value, shift_amount;
1489 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1491 if (shift_amount > HOST_BITS_PER_WIDE_INT)
1492 /* Can not handle precisions greater than twice the host int size. */
1493 abort ();
1494 else if (shift_amount == HOST_BITS_PER_WIDE_INT)
1495 /* Shifting by the host word size is undefined according to the ANSI
1496 standard, so we must handle this as a special case. */
1497 high_value = -1;
1498 else
1499 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1501 return TREE_INT_CST_LOW (expr) == -1
1502 && TREE_INT_CST_HIGH (expr) == high_value;
1504 else
1505 return TREE_INT_CST_LOW (expr) == ((HOST_WIDE_INT) 1 << prec) - 1;
1508 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1509 one bit on). */
1512 integer_pow2p (expr)
1513 tree expr;
1515 HOST_WIDE_INT high, low;
1517 STRIP_NOPS (expr);
1519 if (TREE_CODE (expr) == COMPLEX_CST
1520 && integer_pow2p (TREE_REALPART (expr))
1521 && integer_zerop (TREE_IMAGPART (expr)))
1522 return 1;
1524 if (TREE_CODE (expr) != INTEGER_CST)
1525 return 0;
1527 high = TREE_INT_CST_HIGH (expr);
1528 low = TREE_INT_CST_LOW (expr);
1530 if (high == 0 && low == 0)
1531 return 0;
1533 return ((high == 0 && (low & (low - 1)) == 0)
1534 || (low == 0 && (high & (high - 1)) == 0));
1537 /* Return 1 if EXPR is the real constant zero. */
1540 real_zerop (expr)
1541 tree expr;
1543 STRIP_NOPS (expr);
1545 return ((TREE_CODE (expr) == REAL_CST
1546 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0))
1547 || (TREE_CODE (expr) == COMPLEX_CST
1548 && real_zerop (TREE_REALPART (expr))
1549 && real_zerop (TREE_IMAGPART (expr))));
1552 /* Return 1 if EXPR is the real constant one in real or complex form. */
1555 real_onep (expr)
1556 tree expr;
1558 STRIP_NOPS (expr);
1560 return ((TREE_CODE (expr) == REAL_CST
1561 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1))
1562 || (TREE_CODE (expr) == COMPLEX_CST
1563 && real_onep (TREE_REALPART (expr))
1564 && real_zerop (TREE_IMAGPART (expr))));
1567 /* Return 1 if EXPR is the real constant two. */
1570 real_twop (expr)
1571 tree expr;
1573 STRIP_NOPS (expr);
1575 return ((TREE_CODE (expr) == REAL_CST
1576 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2))
1577 || (TREE_CODE (expr) == COMPLEX_CST
1578 && real_twop (TREE_REALPART (expr))
1579 && real_zerop (TREE_IMAGPART (expr))));
1582 /* Nonzero if EXP is a constant or a cast of a constant. */
1585 really_constant_p (exp)
1586 tree exp;
1588 /* This is not quite the same as STRIP_NOPS. It does more. */
1589 while (TREE_CODE (exp) == NOP_EXPR
1590 || TREE_CODE (exp) == CONVERT_EXPR
1591 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1592 exp = TREE_OPERAND (exp, 0);
1593 return TREE_CONSTANT (exp);
1596 /* Return first list element whose TREE_VALUE is ELEM.
1597 Return 0 if ELEM is not it LIST. */
1599 tree
1600 value_member (elem, list)
1601 tree elem, list;
1603 while (list)
1605 if (elem == TREE_VALUE (list))
1606 return list;
1607 list = TREE_CHAIN (list);
1609 return NULL_TREE;
1612 /* Return first list element whose TREE_PURPOSE is ELEM.
1613 Return 0 if ELEM is not it LIST. */
1615 tree
1616 purpose_member (elem, list)
1617 tree elem, list;
1619 while (list)
1621 if (elem == TREE_PURPOSE (list))
1622 return list;
1623 list = TREE_CHAIN (list);
1625 return NULL_TREE;
1628 /* Return first list element whose BINFO_TYPE is ELEM.
1629 Return 0 if ELEM is not it LIST. */
1631 tree
1632 binfo_member (elem, list)
1633 tree elem, list;
1635 while (list)
1637 if (elem == BINFO_TYPE (list))
1638 return list;
1639 list = TREE_CHAIN (list);
1641 return NULL_TREE;
1644 /* Return nonzero if ELEM is part of the chain CHAIN. */
1647 chain_member (elem, chain)
1648 tree elem, chain;
1650 while (chain)
1652 if (elem == chain)
1653 return 1;
1654 chain = TREE_CHAIN (chain);
1657 return 0;
1660 /* Return the length of a chain of nodes chained through TREE_CHAIN.
1661 We expect a null pointer to mark the end of the chain.
1662 This is the Lisp primitive `length'. */
1665 list_length (t)
1666 tree t;
1668 register tree tail;
1669 register int len = 0;
1671 for (tail = t; tail; tail = TREE_CHAIN (tail))
1672 len++;
1674 return len;
1677 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
1678 by modifying the last node in chain 1 to point to chain 2.
1679 This is the Lisp primitive `nconc'. */
1681 tree
1682 chainon (op1, op2)
1683 tree op1, op2;
1686 if (op1)
1688 register tree t1;
1689 register tree t2;
1691 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
1693 TREE_CHAIN (t1) = op2;
1694 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
1695 if (t2 == t1)
1696 abort (); /* Circularity created. */
1697 return op1;
1699 else return op2;
1702 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
1704 tree
1705 tree_last (chain)
1706 register tree chain;
1708 register tree next;
1709 if (chain)
1710 while (next = TREE_CHAIN (chain))
1711 chain = next;
1712 return chain;
1715 /* Reverse the order of elements in the chain T,
1716 and return the new head of the chain (old last element). */
1718 tree
1719 nreverse (t)
1720 tree t;
1722 register tree prev = 0, decl, next;
1723 for (decl = t; decl; decl = next)
1725 next = TREE_CHAIN (decl);
1726 TREE_CHAIN (decl) = prev;
1727 prev = decl;
1729 return prev;
1732 /* Given a chain CHAIN of tree nodes,
1733 construct and return a list of those nodes. */
1735 tree
1736 listify (chain)
1737 tree chain;
1739 tree result = NULL_TREE;
1740 tree in_tail = chain;
1741 tree out_tail = NULL_TREE;
1743 while (in_tail)
1745 tree next = tree_cons (NULL_TREE, in_tail, NULL_TREE);
1746 if (out_tail)
1747 TREE_CHAIN (out_tail) = next;
1748 else
1749 result = next;
1750 out_tail = next;
1751 in_tail = TREE_CHAIN (in_tail);
1754 return result;
1757 /* Return a newly created TREE_LIST node whose
1758 purpose and value fields are PARM and VALUE. */
1760 tree
1761 build_tree_list (parm, value)
1762 tree parm, value;
1764 register tree t = make_node (TREE_LIST);
1765 TREE_PURPOSE (t) = parm;
1766 TREE_VALUE (t) = value;
1767 return t;
1770 /* Similar, but build on the temp_decl_obstack. */
1772 tree
1773 build_decl_list (parm, value)
1774 tree parm, value;
1776 register tree node;
1777 register struct obstack *ambient_obstack = current_obstack;
1778 current_obstack = &temp_decl_obstack;
1779 node = build_tree_list (parm, value);
1780 current_obstack = ambient_obstack;
1781 return node;
1784 /* Return a newly created TREE_LIST node whose
1785 purpose and value fields are PARM and VALUE
1786 and whose TREE_CHAIN is CHAIN. */
1788 tree
1789 tree_cons (purpose, value, chain)
1790 tree purpose, value, chain;
1792 #if 0
1793 register tree node = make_node (TREE_LIST);
1794 #else
1795 register int i;
1796 register tree node = (tree) obstack_alloc (current_obstack, sizeof (struct tree_list));
1797 #ifdef GATHER_STATISTICS
1798 tree_node_counts[(int)x_kind]++;
1799 tree_node_sizes[(int)x_kind] += sizeof (struct tree_list);
1800 #endif
1802 for (i = (sizeof (struct tree_common) / sizeof (int)) - 1; i >= 0; i--)
1803 ((int *) node)[i] = 0;
1805 TREE_SET_CODE (node, TREE_LIST);
1806 if (current_obstack == &permanent_obstack)
1807 TREE_PERMANENT (node) = 1;
1808 #endif
1810 TREE_CHAIN (node) = chain;
1811 TREE_PURPOSE (node) = purpose;
1812 TREE_VALUE (node) = value;
1813 return node;
1816 /* Similar, but build on the temp_decl_obstack. */
1818 tree
1819 decl_tree_cons (purpose, value, chain)
1820 tree purpose, value, chain;
1822 register tree node;
1823 register struct obstack *ambient_obstack = current_obstack;
1824 current_obstack = &temp_decl_obstack;
1825 node = tree_cons (purpose, value, chain);
1826 current_obstack = ambient_obstack;
1827 return node;
1830 /* Same as `tree_cons' but make a permanent object. */
1832 tree
1833 perm_tree_cons (purpose, value, chain)
1834 tree purpose, value, chain;
1836 register tree node;
1837 register struct obstack *ambient_obstack = current_obstack;
1838 current_obstack = &permanent_obstack;
1840 node = tree_cons (purpose, value, chain);
1841 current_obstack = ambient_obstack;
1842 return node;
1845 /* Same as `tree_cons', but make this node temporary, regardless. */
1847 tree
1848 temp_tree_cons (purpose, value, chain)
1849 tree purpose, value, chain;
1851 register tree node;
1852 register struct obstack *ambient_obstack = current_obstack;
1853 current_obstack = &temporary_obstack;
1855 node = tree_cons (purpose, value, chain);
1856 current_obstack = ambient_obstack;
1857 return node;
1860 /* Same as `tree_cons', but save this node if the function's RTL is saved. */
1862 tree
1863 saveable_tree_cons (purpose, value, chain)
1864 tree purpose, value, chain;
1866 register tree node;
1867 register struct obstack *ambient_obstack = current_obstack;
1868 current_obstack = saveable_obstack;
1870 node = tree_cons (purpose, value, chain);
1871 current_obstack = ambient_obstack;
1872 return node;
1875 /* Return the size nominally occupied by an object of type TYPE
1876 when it resides in memory. The value is measured in units of bytes,
1877 and its data type is that normally used for type sizes
1878 (which is the first type created by make_signed_type or
1879 make_unsigned_type). */
1881 tree
1882 size_in_bytes (type)
1883 tree type;
1885 tree t;
1887 if (type == error_mark_node)
1888 return integer_zero_node;
1889 type = TYPE_MAIN_VARIANT (type);
1890 if (TYPE_SIZE (type) == 0)
1892 incomplete_type_error (NULL_TREE, type);
1893 return integer_zero_node;
1895 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1896 size_int (BITS_PER_UNIT));
1897 if (TREE_CODE (t) == INTEGER_CST)
1898 force_fit_type (t, 0);
1899 return t;
1902 /* Return the size of TYPE (in bytes) as an integer,
1903 or return -1 if the size can vary. */
1906 int_size_in_bytes (type)
1907 tree type;
1909 unsigned int size;
1910 if (type == error_mark_node)
1911 return 0;
1912 type = TYPE_MAIN_VARIANT (type);
1913 if (TYPE_SIZE (type) == 0)
1914 return -1;
1915 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1916 return -1;
1917 if (TREE_INT_CST_HIGH (TYPE_SIZE (type)) != 0)
1919 tree t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1920 size_int (BITS_PER_UNIT));
1921 return TREE_INT_CST_LOW (t);
1923 size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1924 return (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1927 /* Return, as a tree node, the number of elements for TYPE (which is an
1928 ARRAY_TYPE) minus one. This counts only elements of the top array. */
1930 tree
1931 array_type_nelts (type)
1932 tree type;
1934 tree index_type = TYPE_DOMAIN (type);
1936 return (integer_zerop (TYPE_MIN_VALUE (index_type))
1937 ? TYPE_MAX_VALUE (index_type)
1938 : fold (build (MINUS_EXPR, TREE_TYPE (TYPE_MAX_VALUE (index_type)),
1939 TYPE_MAX_VALUE (index_type),
1940 TYPE_MIN_VALUE (index_type))));
1943 /* Return nonzero if arg is static -- a reference to an object in
1944 static storage. This is not the same as the C meaning of `static'. */
1947 staticp (arg)
1948 tree arg;
1950 switch (TREE_CODE (arg))
1952 case FUNCTION_DECL:
1953 /* Nested functions aren't static, since taking their address
1954 involves a trampoline. */
1955 return decl_function_context (arg) == 0;
1956 case VAR_DECL:
1957 return TREE_STATIC (arg) || DECL_EXTERNAL (arg);
1959 case CONSTRUCTOR:
1960 return TREE_STATIC (arg);
1962 case STRING_CST:
1963 return 1;
1965 case COMPONENT_REF:
1966 case BIT_FIELD_REF:
1967 return staticp (TREE_OPERAND (arg, 0));
1969 case INDIRECT_REF:
1970 return TREE_CONSTANT (TREE_OPERAND (arg, 0));
1972 case ARRAY_REF:
1973 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
1974 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
1975 return staticp (TREE_OPERAND (arg, 0));
1978 return 0;
1981 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
1982 Do this to any expression which may be used in more than one place,
1983 but must be evaluated only once.
1985 Normally, expand_expr would reevaluate the expression each time.
1986 Calling save_expr produces something that is evaluated and recorded
1987 the first time expand_expr is called on it. Subsequent calls to
1988 expand_expr just reuse the recorded value.
1990 The call to expand_expr that generates code that actually computes
1991 the value is the first call *at compile time*. Subsequent calls
1992 *at compile time* generate code to use the saved value.
1993 This produces correct result provided that *at run time* control
1994 always flows through the insns made by the first expand_expr
1995 before reaching the other places where the save_expr was evaluated.
1996 You, the caller of save_expr, must make sure this is so.
1998 Constants, and certain read-only nodes, are returned with no
1999 SAVE_EXPR because that is safe. Expressions containing placeholders
2000 are not touched; see tree.def for an explanation of what these
2001 are used for. */
2003 tree
2004 save_expr (expr)
2005 tree expr;
2007 register tree t = fold (expr);
2009 /* We don't care about whether this can be used as an lvalue in this
2010 context. */
2011 while (TREE_CODE (t) == NON_LVALUE_EXPR)
2012 t = TREE_OPERAND (t, 0);
2014 /* If the tree evaluates to a constant, then we don't want to hide that
2015 fact (i.e. this allows further folding, and direct checks for constants).
2016 However, a read-only object that has side effects cannot be bypassed.
2017 Since it is no problem to reevaluate literals, we just return the
2018 literal node. */
2020 if (TREE_CONSTANT (t) || (TREE_READONLY (t) && ! TREE_SIDE_EFFECTS (t))
2021 || TREE_CODE (t) == SAVE_EXPR)
2022 return t;
2024 /* If T contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2025 it means that the size or offset of some field of an object depends on
2026 the value within another field.
2028 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2029 and some variable since it would then need to be both evaluated once and
2030 evaluated more than once. Front-ends must assure this case cannot
2031 happen by surrounding any such subexpressions in their own SAVE_EXPR
2032 and forcing evaluation at the proper time. */
2033 if (contains_placeholder_p (t))
2034 return t;
2036 t = build (SAVE_EXPR, TREE_TYPE (expr), t, current_function_decl, NULL_TREE);
2038 /* This expression might be placed ahead of a jump to ensure that the
2039 value was computed on both sides of the jump. So make sure it isn't
2040 eliminated as dead. */
2041 TREE_SIDE_EFFECTS (t) = 1;
2042 return t;
2045 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
2046 or offset that depends on a field within a record.
2048 Note that we only allow such expressions within simple arithmetic
2049 or a COND_EXPR. */
2052 contains_placeholder_p (exp)
2053 tree exp;
2055 register enum tree_code code = TREE_CODE (exp);
2056 tree inner;
2058 /* If we have a WITH_RECORD_EXPR, it "cancels" any PLACEHOLDER_EXPR
2059 in it since it is supplying a value for it. */
2060 if (code == WITH_RECORD_EXPR)
2061 return 0;
2063 switch (TREE_CODE_CLASS (code))
2065 case 'r':
2066 for (inner = TREE_OPERAND (exp, 0);
2067 TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
2068 inner = TREE_OPERAND (inner, 0))
2070 return TREE_CODE (inner) == PLACEHOLDER_EXPR;
2072 case '1':
2073 case '2': case '<':
2074 case 'e':
2075 switch (tree_code_length[(int) code])
2077 case 1:
2078 return contains_placeholder_p (TREE_OPERAND (exp, 0));
2079 case 2:
2080 return (code != RTL_EXPR
2081 && code != CONSTRUCTOR
2082 && ! (code == SAVE_EXPR && SAVE_EXPR_RTL (exp) != 0)
2083 && code != WITH_RECORD_EXPR
2084 && (contains_placeholder_p (TREE_OPERAND (exp, 0))
2085 || contains_placeholder_p (TREE_OPERAND (exp, 1))));
2086 case 3:
2087 return (code == COND_EXPR
2088 && (contains_placeholder_p (TREE_OPERAND (exp, 0))
2089 || contains_placeholder_p (TREE_OPERAND (exp, 1))
2090 || contains_placeholder_p (TREE_OPERAND (exp, 2))));
2094 return 0;
2097 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
2098 return a tree with all occurrences of references to F in a
2099 PLACEHOLDER_EXPR replaced by R. Note that we assume here that EXP
2100 contains only arithmetic expressions. */
2102 tree
2103 substitute_in_expr (exp, f, r)
2104 tree exp;
2105 tree f;
2106 tree r;
2108 enum tree_code code = TREE_CODE (exp);
2109 tree inner;
2111 switch (TREE_CODE_CLASS (code))
2113 case 'c':
2114 case 'd':
2115 return exp;
2117 case 'x':
2118 if (code == PLACEHOLDER_EXPR)
2119 return exp;
2120 break;
2122 case '1':
2123 case '2':
2124 case '<':
2125 case 'e':
2126 switch (tree_code_length[(int) code])
2128 case 1:
2129 return fold (build1 (code, TREE_TYPE (exp),
2130 substitute_in_expr (TREE_OPERAND (exp, 0),
2131 f, r)));
2133 case 2:
2134 /* An RTL_EXPR cannot contain a PLACEHOLDER_EXPR; a CONSTRUCTOR
2135 could, but we don't support it. */
2136 if (code == RTL_EXPR)
2137 return exp;
2138 else if (code == CONSTRUCTOR)
2139 abort ();
2141 return fold (build (code, TREE_TYPE (exp),
2142 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2143 substitute_in_expr (TREE_OPERAND (exp, 1),
2144 f, r)));
2146 case 3:
2147 /* It cannot be that anything inside a SAVE_EXPR contains a
2148 PLACEHOLDER_EXPR. */
2149 if (code == SAVE_EXPR)
2150 return exp;
2152 if (code != COND_EXPR)
2153 abort ();
2155 return fold (build (code, TREE_TYPE (exp),
2156 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2157 substitute_in_expr (TREE_OPERAND (exp, 1), f, r),
2158 substitute_in_expr (TREE_OPERAND (exp, 2),
2159 f, r)));
2162 break;
2164 case 'r':
2165 switch (code)
2167 case COMPONENT_REF:
2168 /* If this expression is getting a value from a PLACEHOLDER_EXPR
2169 and it is the right field, replace it with R. */
2170 for (inner = TREE_OPERAND (exp, 0);
2171 TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
2172 inner = TREE_OPERAND (inner, 0))
2174 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2175 && TREE_OPERAND (exp, 1) == f)
2176 return r;
2178 return fold (build (code, TREE_TYPE (exp),
2179 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2180 TREE_OPERAND (exp, 1)));
2181 case BIT_FIELD_REF:
2182 return fold (build (code, TREE_TYPE (exp),
2183 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2184 substitute_in_expr (TREE_OPERAND (exp, 1), f, r),
2185 substitute_in_expr (TREE_OPERAND (exp, 2), f, r)));
2186 case INDIRECT_REF:
2187 case BUFFER_REF:
2188 return fold (build1 (code, TREE_TYPE (exp),
2189 substitute_in_expr (TREE_OPERAND (exp, 0),
2190 f, r)));
2191 case OFFSET_REF:
2192 return fold (build (code, TREE_TYPE (exp),
2193 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2194 substitute_in_expr (TREE_OPERAND (exp, 1), f, r)));
2198 /* If it wasn't one of the cases we handle, give up. */
2200 abort ();
2203 /* Given a type T, a FIELD_DECL F, and a replacement value R,
2204 return a new type with all size expressions that contain F
2205 updated by replacing F with R. */
2207 tree
2208 substitute_in_type (t, f, r)
2209 tree t, f, r;
2211 switch (TREE_CODE (t))
2213 case POINTER_TYPE:
2214 case VOID_TYPE:
2215 return t;
2216 case INTEGER_TYPE:
2217 case ENUMERAL_TYPE:
2218 case BOOLEAN_TYPE:
2219 case CHAR_TYPE:
2220 if ((TREE_CODE (TYPE_MIN_VALUE (t)) != INTEGER_CST
2221 && contains_placeholder_p (TYPE_MIN_VALUE (t)))
2222 || (TREE_CODE (TYPE_MAX_VALUE (t)) != INTEGER_CST
2223 && contains_placeholder_p (TYPE_MAX_VALUE (t))))
2224 return build_range_type (t,
2225 substitute_in_expr (TYPE_MIN_VALUE (t), f, r),
2226 substitute_in_expr (TYPE_MAX_VALUE (t), f, r));
2227 return t;
2229 case REAL_TYPE:
2230 if ((TYPE_MIN_VALUE (t) != 0
2231 && TREE_CODE (TYPE_MIN_VALUE (t)) != REAL_CST
2232 && contains_placeholder_p (TYPE_MIN_VALUE (t)))
2233 || (TYPE_MAX_VALUE (t) != 0
2234 && TREE_CODE (TYPE_MAX_VALUE (t)) != REAL_CST
2235 && contains_placeholder_p (TYPE_MAX_VALUE (t))))
2237 t = build_type_copy (t);
2239 if (TYPE_MIN_VALUE (t))
2240 TYPE_MIN_VALUE (t) = substitute_in_expr (TYPE_MIN_VALUE (t), f, r);
2241 if (TYPE_MAX_VALUE (t))
2242 TYPE_MAX_VALUE (t) = substitute_in_expr (TYPE_MAX_VALUE (t), f, r);
2244 return t;
2246 case COMPLEX_TYPE:
2247 return build_complex_type (substitute_in_type (TREE_TYPE (t), f, r));
2249 case OFFSET_TYPE:
2250 case METHOD_TYPE:
2251 case REFERENCE_TYPE:
2252 case FILE_TYPE:
2253 case SET_TYPE:
2254 case FUNCTION_TYPE:
2255 case LANG_TYPE:
2256 /* Don't know how to do these yet. */
2257 abort ();
2259 case ARRAY_TYPE:
2260 t = build_array_type (substitute_in_type (TREE_TYPE (t), f, r),
2261 substitute_in_type (TYPE_DOMAIN (t), f, r));
2262 TYPE_SIZE (t) = 0;
2263 layout_type (t);
2264 return t;
2266 case RECORD_TYPE:
2267 case UNION_TYPE:
2268 case QUAL_UNION_TYPE:
2270 tree new = copy_node (t);
2271 tree field;
2272 tree last_field = 0;
2274 /* Start out with no fields, make new fields, and chain them
2275 in. */
2277 TYPE_FIELDS (new) = 0;
2278 TYPE_SIZE (new) = 0;
2280 for (field = TYPE_FIELDS (t); field;
2281 field = TREE_CHAIN (field))
2283 tree new_field = copy_node (field);
2285 TREE_TYPE (new_field)
2286 = substitute_in_type (TREE_TYPE (new_field), f, r);
2288 /* If this is an anonymous field and the type of this field is
2289 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
2290 the type just has one element, treat that as the field.
2291 But don't do this if we are processing a QUAL_UNION_TYPE. */
2292 if (TREE_CODE (t) != QUAL_UNION_TYPE && DECL_NAME (new_field) == 0
2293 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
2294 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
2296 if (TYPE_FIELDS (TREE_TYPE (new_field)) == 0)
2297 continue;
2299 if (TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))) == 0)
2300 new_field = TYPE_FIELDS (TREE_TYPE (new_field));
2303 DECL_CONTEXT (new_field) = new;
2304 DECL_SIZE (new_field) = 0;
2306 if (TREE_CODE (t) == QUAL_UNION_TYPE)
2308 /* Do the substitution inside the qualifier and if we find
2309 that this field will not be present, omit it. */
2310 DECL_QUALIFIER (new_field)
2311 = substitute_in_expr (DECL_QUALIFIER (field), f, r);
2312 if (integer_zerop (DECL_QUALIFIER (new_field)))
2313 continue;
2316 if (last_field == 0)
2317 TYPE_FIELDS (new) = new_field;
2318 else
2319 TREE_CHAIN (last_field) = new_field;
2321 last_field = new_field;
2323 /* If this is a qualified type and this field will always be
2324 present, we are done. */
2325 if (TREE_CODE (t) == QUAL_UNION_TYPE
2326 && integer_onep (DECL_QUALIFIER (new_field)))
2327 break;
2330 /* If this used to be a qualified union type, but we now know what
2331 field will be present, make this a normal union. */
2332 if (TREE_CODE (new) == QUAL_UNION_TYPE
2333 && (TYPE_FIELDS (new) == 0
2334 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
2335 TREE_SET_CODE (new, UNION_TYPE);
2337 layout_type (new);
2338 return new;
2343 /* Stabilize a reference so that we can use it any number of times
2344 without causing its operands to be evaluated more than once.
2345 Returns the stabilized reference. This works by means of save_expr,
2346 so see the caveats in the comments about save_expr.
2348 Also allows conversion expressions whose operands are references.
2349 Any other kind of expression is returned unchanged. */
2351 tree
2352 stabilize_reference (ref)
2353 tree ref;
2355 register tree result;
2356 register enum tree_code code = TREE_CODE (ref);
2358 switch (code)
2360 case VAR_DECL:
2361 case PARM_DECL:
2362 case RESULT_DECL:
2363 /* No action is needed in this case. */
2364 return ref;
2366 case NOP_EXPR:
2367 case CONVERT_EXPR:
2368 case FLOAT_EXPR:
2369 case FIX_TRUNC_EXPR:
2370 case FIX_FLOOR_EXPR:
2371 case FIX_ROUND_EXPR:
2372 case FIX_CEIL_EXPR:
2373 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2374 break;
2376 case INDIRECT_REF:
2377 result = build_nt (INDIRECT_REF,
2378 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2379 break;
2381 case COMPONENT_REF:
2382 result = build_nt (COMPONENT_REF,
2383 stabilize_reference (TREE_OPERAND (ref, 0)),
2384 TREE_OPERAND (ref, 1));
2385 break;
2387 case BIT_FIELD_REF:
2388 result = build_nt (BIT_FIELD_REF,
2389 stabilize_reference (TREE_OPERAND (ref, 0)),
2390 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2391 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2392 break;
2394 case ARRAY_REF:
2395 result = build_nt (ARRAY_REF,
2396 stabilize_reference (TREE_OPERAND (ref, 0)),
2397 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2398 break;
2400 case COMPOUND_EXPR:
2401 result = build_nt (COMPOUND_EXPR,
2402 stabilize_reference_1 (TREE_OPERAND (ref, 0)),
2403 stabilize_reference (TREE_OPERAND (ref, 1)));
2404 break;
2406 case RTL_EXPR:
2407 result = build1 (INDIRECT_REF, TREE_TYPE (ref),
2408 save_expr (build1 (ADDR_EXPR,
2409 build_pointer_type (TREE_TYPE (ref)),
2410 ref)));
2411 break;
2414 /* If arg isn't a kind of lvalue we recognize, make no change.
2415 Caller should recognize the error for an invalid lvalue. */
2416 default:
2417 return ref;
2419 case ERROR_MARK:
2420 return error_mark_node;
2423 TREE_TYPE (result) = TREE_TYPE (ref);
2424 TREE_READONLY (result) = TREE_READONLY (ref);
2425 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
2426 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
2427 TREE_RAISES (result) = TREE_RAISES (ref);
2429 return result;
2432 /* Subroutine of stabilize_reference; this is called for subtrees of
2433 references. Any expression with side-effects must be put in a SAVE_EXPR
2434 to ensure that it is only evaluated once.
2436 We don't put SAVE_EXPR nodes around everything, because assigning very
2437 simple expressions to temporaries causes us to miss good opportunities
2438 for optimizations. Among other things, the opportunity to fold in the
2439 addition of a constant into an addressing mode often gets lost, e.g.
2440 "y[i+1] += x;". In general, we take the approach that we should not make
2441 an assignment unless we are forced into it - i.e., that any non-side effect
2442 operator should be allowed, and that cse should take care of coalescing
2443 multiple utterances of the same expression should that prove fruitful. */
2445 tree
2446 stabilize_reference_1 (e)
2447 tree e;
2449 register tree result;
2450 register enum tree_code code = TREE_CODE (e);
2452 /* We cannot ignore const expressions because it might be a reference
2453 to a const array but whose index contains side-effects. But we can
2454 ignore things that are actual constant or that already have been
2455 handled by this function. */
2457 if (TREE_CONSTANT (e) || code == SAVE_EXPR)
2458 return e;
2460 switch (TREE_CODE_CLASS (code))
2462 case 'x':
2463 case 't':
2464 case 'd':
2465 case 'b':
2466 case '<':
2467 case 's':
2468 case 'e':
2469 case 'r':
2470 /* If the expression has side-effects, then encase it in a SAVE_EXPR
2471 so that it will only be evaluated once. */
2472 /* The reference (r) and comparison (<) classes could be handled as
2473 below, but it is generally faster to only evaluate them once. */
2474 if (TREE_SIDE_EFFECTS (e))
2475 return save_expr (e);
2476 return e;
2478 case 'c':
2479 /* Constants need no processing. In fact, we should never reach
2480 here. */
2481 return e;
2483 case '2':
2484 /* Division is slow and tends to be compiled with jumps,
2485 especially the division by powers of 2 that is often
2486 found inside of an array reference. So do it just once. */
2487 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
2488 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
2489 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
2490 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
2491 return save_expr (e);
2492 /* Recursively stabilize each operand. */
2493 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
2494 stabilize_reference_1 (TREE_OPERAND (e, 1)));
2495 break;
2497 case '1':
2498 /* Recursively stabilize each operand. */
2499 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
2500 break;
2502 default:
2503 abort ();
2506 TREE_TYPE (result) = TREE_TYPE (e);
2507 TREE_READONLY (result) = TREE_READONLY (e);
2508 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
2509 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
2510 TREE_RAISES (result) = TREE_RAISES (e);
2512 return result;
2515 /* Low-level constructors for expressions. */
2517 /* Build an expression of code CODE, data type TYPE,
2518 and operands as specified by the arguments ARG1 and following arguments.
2519 Expressions and reference nodes can be created this way.
2520 Constants, decls, types and misc nodes cannot be. */
2522 tree
2523 build VPROTO((enum tree_code code, tree tt, ...))
2525 #ifndef __STDC__
2526 enum tree_code code;
2527 tree tt;
2528 #endif
2529 va_list p;
2530 register tree t;
2531 register int length;
2532 register int i;
2534 VA_START (p, tt);
2536 #ifndef __STDC__
2537 code = va_arg (p, enum tree_code);
2538 tt = va_arg (p, tree);
2539 #endif
2541 t = make_node (code);
2542 length = tree_code_length[(int) code];
2543 TREE_TYPE (t) = tt;
2545 if (length == 2)
2547 /* This is equivalent to the loop below, but faster. */
2548 register tree arg0 = va_arg (p, tree);
2549 register tree arg1 = va_arg (p, tree);
2550 TREE_OPERAND (t, 0) = arg0;
2551 TREE_OPERAND (t, 1) = arg1;
2552 if ((arg0 && TREE_SIDE_EFFECTS (arg0))
2553 || (arg1 && TREE_SIDE_EFFECTS (arg1)))
2554 TREE_SIDE_EFFECTS (t) = 1;
2555 TREE_RAISES (t)
2556 = (arg0 && TREE_RAISES (arg0)) || (arg1 && TREE_RAISES (arg1));
2558 else if (length == 1)
2560 register tree arg0 = va_arg (p, tree);
2562 /* Call build1 for this! */
2563 if (TREE_CODE_CLASS (code) != 's')
2564 abort ();
2565 TREE_OPERAND (t, 0) = arg0;
2566 if (arg0 && TREE_SIDE_EFFECTS (arg0))
2567 TREE_SIDE_EFFECTS (t) = 1;
2568 TREE_RAISES (t) = (arg0 && TREE_RAISES (arg0));
2570 else
2572 for (i = 0; i < length; i++)
2574 register tree operand = va_arg (p, tree);
2575 TREE_OPERAND (t, i) = operand;
2576 if (operand)
2578 if (TREE_SIDE_EFFECTS (operand))
2579 TREE_SIDE_EFFECTS (t) = 1;
2580 if (TREE_RAISES (operand))
2581 TREE_RAISES (t) = 1;
2585 va_end (p);
2586 return t;
2589 /* Same as above, but only builds for unary operators.
2590 Saves lions share of calls to `build'; cuts down use
2591 of varargs, which is expensive for RISC machines. */
2592 tree
2593 build1 (code, type, node)
2594 enum tree_code code;
2595 tree type;
2596 tree node;
2598 register struct obstack *obstack = current_obstack;
2599 register int i, length;
2600 register tree_node_kind kind;
2601 register tree t;
2603 #ifdef GATHER_STATISTICS
2604 if (TREE_CODE_CLASS (code) == 'r')
2605 kind = r_kind;
2606 else
2607 kind = e_kind;
2608 #endif
2610 obstack = expression_obstack;
2611 length = sizeof (struct tree_exp);
2613 t = (tree) obstack_alloc (obstack, length);
2615 #ifdef GATHER_STATISTICS
2616 tree_node_counts[(int)kind]++;
2617 tree_node_sizes[(int)kind] += length;
2618 #endif
2620 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
2621 ((int *) t)[i] = 0;
2623 TREE_TYPE (t) = type;
2624 TREE_SET_CODE (t, code);
2626 if (obstack == &permanent_obstack)
2627 TREE_PERMANENT (t) = 1;
2629 TREE_OPERAND (t, 0) = node;
2630 if (node)
2632 if (TREE_SIDE_EFFECTS (node))
2633 TREE_SIDE_EFFECTS (t) = 1;
2634 if (TREE_RAISES (node))
2635 TREE_RAISES (t) = 1;
2638 return t;
2641 /* Similar except don't specify the TREE_TYPE
2642 and leave the TREE_SIDE_EFFECTS as 0.
2643 It is permissible for arguments to be null,
2644 or even garbage if their values do not matter. */
2646 tree
2647 build_nt VPROTO((enum tree_code code, ...))
2649 #ifndef __STDC__
2650 enum tree_code code;
2651 #endif
2652 va_list p;
2653 register tree t;
2654 register int length;
2655 register int i;
2657 VA_START (p, code);
2659 #ifndef __STDC__
2660 code = va_arg (p, enum tree_code);
2661 #endif
2663 t = make_node (code);
2664 length = tree_code_length[(int) code];
2666 for (i = 0; i < length; i++)
2667 TREE_OPERAND (t, i) = va_arg (p, tree);
2669 va_end (p);
2670 return t;
2673 /* Similar to `build_nt', except we build
2674 on the temp_decl_obstack, regardless. */
2676 tree
2677 build_parse_node VPROTO((enum tree_code code, ...))
2679 #ifndef __STDC__
2680 enum tree_code code;
2681 #endif
2682 register struct obstack *ambient_obstack = expression_obstack;
2683 va_list p;
2684 register tree t;
2685 register int length;
2686 register int i;
2688 VA_START (p, code);
2690 #ifndef __STDC__
2691 code = va_arg (p, enum tree_code);
2692 #endif
2694 expression_obstack = &temp_decl_obstack;
2696 t = make_node (code);
2697 length = tree_code_length[(int) code];
2699 for (i = 0; i < length; i++)
2700 TREE_OPERAND (t, i) = va_arg (p, tree);
2702 va_end (p);
2703 expression_obstack = ambient_obstack;
2704 return t;
2707 #if 0
2708 /* Commented out because this wants to be done very
2709 differently. See cp-lex.c. */
2710 tree
2711 build_op_identifier (op1, op2)
2712 tree op1, op2;
2714 register tree t = make_node (OP_IDENTIFIER);
2715 TREE_PURPOSE (t) = op1;
2716 TREE_VALUE (t) = op2;
2717 return t;
2719 #endif
2721 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
2722 We do NOT enter this node in any sort of symbol table.
2724 layout_decl is used to set up the decl's storage layout.
2725 Other slots are initialized to 0 or null pointers. */
2727 tree
2728 build_decl (code, name, type)
2729 enum tree_code code;
2730 tree name, type;
2732 register tree t;
2734 t = make_node (code);
2736 /* if (type == error_mark_node)
2737 type = integer_type_node; */
2738 /* That is not done, deliberately, so that having error_mark_node
2739 as the type can suppress useless errors in the use of this variable. */
2741 DECL_NAME (t) = name;
2742 DECL_ASSEMBLER_NAME (t) = name;
2743 TREE_TYPE (t) = type;
2745 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
2746 layout_decl (t, 0);
2747 else if (code == FUNCTION_DECL)
2748 DECL_MODE (t) = FUNCTION_MODE;
2750 return t;
2753 /* BLOCK nodes are used to represent the structure of binding contours
2754 and declarations, once those contours have been exited and their contents
2755 compiled. This information is used for outputting debugging info. */
2757 tree
2758 build_block (vars, tags, subblocks, supercontext, chain)
2759 tree vars, tags, subblocks, supercontext, chain;
2761 register tree block = make_node (BLOCK);
2762 BLOCK_VARS (block) = vars;
2763 BLOCK_TYPE_TAGS (block) = tags;
2764 BLOCK_SUBBLOCKS (block) = subblocks;
2765 BLOCK_SUPERCONTEXT (block) = supercontext;
2766 BLOCK_CHAIN (block) = chain;
2767 return block;
2770 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
2771 is ATTRIBUTE.
2773 Such modified types already made are recorded so that duplicates
2774 are not made. */
2776 tree
2777 build_type_attribute_variant (ttype, attribute)
2778 tree ttype, attribute;
2780 if ( ! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
2782 register int hashcode;
2783 register struct obstack *ambient_obstack = current_obstack;
2784 tree ntype;
2786 if (ambient_obstack != &permanent_obstack)
2787 current_obstack = TYPE_OBSTACK (ttype);
2789 ntype = copy_node (ttype);
2790 current_obstack = ambient_obstack;
2792 TYPE_POINTER_TO (ntype) = 0;
2793 TYPE_REFERENCE_TO (ntype) = 0;
2794 TYPE_ATTRIBUTES (ntype) = attribute;
2796 /* Create a new main variant of TYPE. */
2797 TYPE_MAIN_VARIANT (ntype) = ntype;
2798 TYPE_NEXT_VARIANT (ntype) = 0;
2799 TYPE_READONLY (ntype) = TYPE_VOLATILE (ntype) = 0;
2801 hashcode = TYPE_HASH (TREE_CODE (ntype))
2802 + TYPE_HASH (TREE_TYPE (ntype))
2803 + type_hash_list (attribute);
2805 switch (TREE_CODE (ntype))
2807 case FUNCTION_TYPE:
2808 hashcode += TYPE_HASH (TYPE_ARG_TYPES (ntype));
2809 break;
2810 case ARRAY_TYPE:
2811 hashcode += TYPE_HASH (TYPE_DOMAIN (ntype));
2812 break;
2813 case INTEGER_TYPE:
2814 hashcode += TYPE_HASH (TYPE_MAX_VALUE (ntype));
2815 break;
2816 case REAL_TYPE:
2817 hashcode += TYPE_HASH (TYPE_PRECISION (ntype));
2818 break;
2821 ntype = type_hash_canon (hashcode, ntype);
2822 ttype = build_type_variant (ntype, TYPE_READONLY (ttype),
2823 TYPE_VOLATILE (ttype));
2826 return ttype;
2829 /* Return a type like TYPE except that its TYPE_READONLY is CONSTP
2830 and its TYPE_VOLATILE is VOLATILEP.
2832 Such variant types already made are recorded so that duplicates
2833 are not made.
2835 A variant types should never be used as the type of an expression.
2836 Always copy the variant information into the TREE_READONLY
2837 and TREE_THIS_VOLATILE of the expression, and then give the expression
2838 as its type the "main variant", the variant whose TYPE_READONLY
2839 and TYPE_VOLATILE are zero. Use TYPE_MAIN_VARIANT to find the
2840 main variant. */
2842 tree
2843 build_type_variant (type, constp, volatilep)
2844 tree type;
2845 int constp, volatilep;
2847 register tree t;
2849 /* Treat any nonzero argument as 1. */
2850 constp = !!constp;
2851 volatilep = !!volatilep;
2853 /* Search the chain of variants to see if there is already one there just
2854 like the one we need to have. If so, use that existing one. We must
2855 preserve the TYPE_NAME, since there is code that depends on this. */
2857 for (t = TYPE_MAIN_VARIANT(type); t; t = TYPE_NEXT_VARIANT (t))
2858 if (constp == TYPE_READONLY (t) && volatilep == TYPE_VOLATILE (t)
2859 && TYPE_NAME (t) == TYPE_NAME (type))
2860 return t;
2862 /* We need a new one. */
2864 t = build_type_copy (type);
2865 TYPE_READONLY (t) = constp;
2866 TYPE_VOLATILE (t) = volatilep;
2868 return t;
2871 /* Give TYPE a new main variant: NEW_MAIN.
2872 This is the right thing to do only when something else
2873 about TYPE is modified in place. */
2875 void
2876 change_main_variant (type, new_main)
2877 tree type, new_main;
2879 tree t;
2880 tree omain = TYPE_MAIN_VARIANT (type);
2882 /* Remove TYPE from the TYPE_NEXT_VARIANT chain of its main variant. */
2883 if (TYPE_NEXT_VARIANT (omain) == type)
2884 TYPE_NEXT_VARIANT (omain) = TYPE_NEXT_VARIANT (type);
2885 else
2886 for (t = TYPE_NEXT_VARIANT (omain); t && TYPE_NEXT_VARIANT (t);
2887 t = TYPE_NEXT_VARIANT (t))
2888 if (TYPE_NEXT_VARIANT (t) == type)
2890 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (type);
2891 break;
2894 TYPE_MAIN_VARIANT (type) = new_main;
2895 TYPE_NEXT_VARIANT (type) = TYPE_NEXT_VARIANT (new_main);
2896 TYPE_NEXT_VARIANT (new_main) = type;
2899 /* Create a new variant of TYPE, equivalent but distinct.
2900 This is so the caller can modify it. */
2902 tree
2903 build_type_copy (type)
2904 tree type;
2906 register tree t, m = TYPE_MAIN_VARIANT (type);
2907 register struct obstack *ambient_obstack = current_obstack;
2909 current_obstack = TYPE_OBSTACK (type);
2910 t = copy_node (type);
2911 current_obstack = ambient_obstack;
2913 TYPE_POINTER_TO (t) = 0;
2914 TYPE_REFERENCE_TO (t) = 0;
2916 /* Add this type to the chain of variants of TYPE. */
2917 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
2918 TYPE_NEXT_VARIANT (m) = t;
2920 return t;
2923 /* Hashing of types so that we don't make duplicates.
2924 The entry point is `type_hash_canon'. */
2926 /* Each hash table slot is a bucket containing a chain
2927 of these structures. */
2929 struct type_hash
2931 struct type_hash *next; /* Next structure in the bucket. */
2932 int hashcode; /* Hash code of this type. */
2933 tree type; /* The type recorded here. */
2936 /* Now here is the hash table. When recording a type, it is added
2937 to the slot whose index is the hash code mod the table size.
2938 Note that the hash table is used for several kinds of types
2939 (function types, array types and array index range types, for now).
2940 While all these live in the same table, they are completely independent,
2941 and the hash code is computed differently for each of these. */
2943 #define TYPE_HASH_SIZE 59
2944 struct type_hash *type_hash_table[TYPE_HASH_SIZE];
2946 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
2947 with types in the TREE_VALUE slots), by adding the hash codes
2948 of the individual types. */
2951 type_hash_list (list)
2952 tree list;
2954 register int hashcode;
2955 register tree tail;
2956 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
2957 hashcode += TYPE_HASH (TREE_VALUE (tail));
2958 return hashcode;
2961 /* Look in the type hash table for a type isomorphic to TYPE.
2962 If one is found, return it. Otherwise return 0. */
2964 tree
2965 type_hash_lookup (hashcode, type)
2966 int hashcode;
2967 tree type;
2969 register struct type_hash *h;
2970 for (h = type_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
2971 if (h->hashcode == hashcode
2972 && TREE_CODE (h->type) == TREE_CODE (type)
2973 && TREE_TYPE (h->type) == TREE_TYPE (type)
2974 && attribute_list_equal (TYPE_ATTRIBUTES (h->type),
2975 TYPE_ATTRIBUTES (type))
2976 && (TYPE_MAX_VALUE (h->type) == TYPE_MAX_VALUE (type)
2977 || tree_int_cst_equal (TYPE_MAX_VALUE (h->type),
2978 TYPE_MAX_VALUE (type)))
2979 && (TYPE_MIN_VALUE (h->type) == TYPE_MIN_VALUE (type)
2980 || tree_int_cst_equal (TYPE_MIN_VALUE (h->type),
2981 TYPE_MIN_VALUE (type)))
2982 && (TYPE_DOMAIN (h->type) == TYPE_DOMAIN (type)
2983 || (TYPE_DOMAIN (h->type)
2984 && TREE_CODE (TYPE_DOMAIN (h->type)) == TREE_LIST
2985 && TYPE_DOMAIN (type)
2986 && TREE_CODE (TYPE_DOMAIN (type)) == TREE_LIST
2987 && type_list_equal (TYPE_DOMAIN (h->type), TYPE_DOMAIN (type)))))
2988 return h->type;
2989 return 0;
2992 /* Add an entry to the type-hash-table
2993 for a type TYPE whose hash code is HASHCODE. */
2995 void
2996 type_hash_add (hashcode, type)
2997 int hashcode;
2998 tree type;
3000 register struct type_hash *h;
3002 h = (struct type_hash *) oballoc (sizeof (struct type_hash));
3003 h->hashcode = hashcode;
3004 h->type = type;
3005 h->next = type_hash_table[hashcode % TYPE_HASH_SIZE];
3006 type_hash_table[hashcode % TYPE_HASH_SIZE] = h;
3009 /* Given TYPE, and HASHCODE its hash code, return the canonical
3010 object for an identical type if one already exists.
3011 Otherwise, return TYPE, and record it as the canonical object
3012 if it is a permanent object.
3014 To use this function, first create a type of the sort you want.
3015 Then compute its hash code from the fields of the type that
3016 make it different from other similar types.
3017 Then call this function and use the value.
3018 This function frees the type you pass in if it is a duplicate. */
3020 /* Set to 1 to debug without canonicalization. Never set by program. */
3021 int debug_no_type_hash = 0;
3023 tree
3024 type_hash_canon (hashcode, type)
3025 int hashcode;
3026 tree type;
3028 tree t1;
3030 if (debug_no_type_hash)
3031 return type;
3033 t1 = type_hash_lookup (hashcode, type);
3034 if (t1 != 0)
3036 obstack_free (TYPE_OBSTACK (type), type);
3037 #ifdef GATHER_STATISTICS
3038 tree_node_counts[(int)t_kind]--;
3039 tree_node_sizes[(int)t_kind] -= sizeof (struct tree_type);
3040 #endif
3041 return t1;
3044 /* If this is a permanent type, record it for later reuse. */
3045 if (TREE_PERMANENT (type))
3046 type_hash_add (hashcode, type);
3048 return type;
3051 /* Given two lists of attributes, return true if list l2 is
3052 equivalent to l1. */
3055 attribute_list_equal (l1, l2)
3056 tree l1, l2;
3058 return attribute_list_contained (l1, l2)
3059 && attribute_list_contained (l2, l1);
3062 /* Given two lists of attributes, return true if list l2 is
3063 completely contained within l1. */
3066 attribute_list_contained (l1, l2)
3067 tree l1, l2;
3069 register tree t1, t2;
3071 /* First check the obvious, maybe the lists are identical. */
3072 if (l1 == l2)
3073 return 1;
3075 /* Then check the obvious, maybe the lists are similar. */
3076 for (t1 = l1, t2 = l2;
3077 t1 && t2
3078 && TREE_VALUE (t1) == TREE_VALUE (t2);
3079 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
3081 /* Maybe the lists are equal. */
3082 if (t1 == 0 && t2 == 0)
3083 return 1;
3085 for (; t2; t2 = TREE_CHAIN (t2))
3086 if (!value_member (l1, t2))
3087 return 0;
3088 return 1;
3091 /* Given two lists of types
3092 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
3093 return 1 if the lists contain the same types in the same order.
3094 Also, the TREE_PURPOSEs must match. */
3097 type_list_equal (l1, l2)
3098 tree l1, l2;
3100 register tree t1, t2;
3101 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
3103 if (TREE_VALUE (t1) != TREE_VALUE (t2))
3104 return 0;
3105 if (TREE_PURPOSE (t1) != TREE_PURPOSE (t2))
3107 int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
3108 if (cmp < 0)
3109 abort ();
3110 if (cmp == 0
3111 || TREE_TYPE (TREE_PURPOSE (t1))
3112 != TREE_TYPE (TREE_PURPOSE (t2)))
3113 return 0;
3117 return t1 == t2;
3120 /* Nonzero if integer constants T1 and T2
3121 represent the same constant value. */
3124 tree_int_cst_equal (t1, t2)
3125 tree t1, t2;
3127 if (t1 == t2)
3128 return 1;
3129 if (t1 == 0 || t2 == 0)
3130 return 0;
3131 if (TREE_CODE (t1) == INTEGER_CST
3132 && TREE_CODE (t2) == INTEGER_CST
3133 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3134 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
3135 return 1;
3136 return 0;
3139 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
3140 The precise way of comparison depends on their data type. */
3143 tree_int_cst_lt (t1, t2)
3144 tree t1, t2;
3146 if (t1 == t2)
3147 return 0;
3149 if (!TREE_UNSIGNED (TREE_TYPE (t1)))
3150 return INT_CST_LT (t1, t2);
3151 return INT_CST_LT_UNSIGNED (t1, t2);
3154 /* Return an indication of the sign of the integer constant T.
3155 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
3156 Note that -1 will never be returned it T's type is unsigned. */
3159 tree_int_cst_sgn (t)
3160 tree t;
3162 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
3163 return 0;
3164 else if (TREE_UNSIGNED (TREE_TYPE (t)))
3165 return 1;
3166 else if (TREE_INT_CST_HIGH (t) < 0)
3167 return -1;
3168 else
3169 return 1;
3172 /* Compare two constructor-element-type constants. */
3174 simple_cst_list_equal (l1, l2)
3175 tree l1, l2;
3177 while (l1 != NULL_TREE && l2 != NULL_TREE)
3179 int cmp = simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2));
3180 if (cmp < 0)
3181 abort ();
3182 if (cmp == 0)
3183 return 0;
3184 l1 = TREE_CHAIN (l1);
3185 l2 = TREE_CHAIN (l2);
3187 return (l1 == l2);
3190 /* Return truthvalue of whether T1 is the same tree structure as T2.
3191 Return 1 if they are the same.
3192 Return 0 if they are understandably different.
3193 Return -1 if either contains tree structure not understood by
3194 this function. */
3197 simple_cst_equal (t1, t2)
3198 tree t1, t2;
3200 register enum tree_code code1, code2;
3201 int cmp;
3203 if (t1 == t2)
3204 return 1;
3205 if (t1 == 0 || t2 == 0)
3206 return 0;
3208 code1 = TREE_CODE (t1);
3209 code2 = TREE_CODE (t2);
3211 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
3212 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR || code2 == NON_LVALUE_EXPR)
3213 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3214 else
3215 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
3216 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3217 || code2 == NON_LVALUE_EXPR)
3218 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
3220 if (code1 != code2)
3221 return 0;
3223 switch (code1)
3225 case INTEGER_CST:
3226 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3227 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
3229 case REAL_CST:
3230 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3232 case STRING_CST:
3233 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3234 && !bcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3235 TREE_STRING_LENGTH (t1));
3237 case CONSTRUCTOR:
3238 abort ();
3240 case SAVE_EXPR:
3241 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3243 case CALL_EXPR:
3244 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3245 if (cmp <= 0)
3246 return cmp;
3247 return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3249 case TARGET_EXPR:
3250 /* Special case: if either target is an unallocated VAR_DECL,
3251 it means that it's going to be unified with whatever the
3252 TARGET_EXPR is really supposed to initialize, so treat it
3253 as being equivalent to anything. */
3254 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
3255 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
3256 && DECL_RTL (TREE_OPERAND (t1, 0)) == 0)
3257 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
3258 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
3259 && DECL_RTL (TREE_OPERAND (t2, 0)) == 0))
3260 cmp = 1;
3261 else
3262 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3263 if (cmp <= 0)
3264 return cmp;
3265 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3267 case WITH_CLEANUP_EXPR:
3268 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3269 if (cmp <= 0)
3270 return cmp;
3271 return simple_cst_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t1, 2));
3273 case COMPONENT_REF:
3274 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
3275 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3276 return 0;
3278 case VAR_DECL:
3279 case PARM_DECL:
3280 case CONST_DECL:
3281 case FUNCTION_DECL:
3282 return 0;
3285 /* This general rule works for most tree codes.
3286 All exceptions should be handled above. */
3288 switch (TREE_CODE_CLASS (code1))
3290 int i;
3291 case '1':
3292 case '2':
3293 case '<':
3294 case 'e':
3295 case 'r':
3296 case 's':
3297 cmp = 1;
3298 for (i=0; i<tree_code_length[(int) code1]; ++i)
3300 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
3301 if (cmp <= 0)
3302 return cmp;
3304 return cmp;
3307 return -1;
3310 /* Constructors for pointer, array and function types.
3311 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
3312 constructed by language-dependent code, not here.) */
3314 /* Construct, lay out and return the type of pointers to TO_TYPE.
3315 If such a type has already been constructed, reuse it. */
3317 tree
3318 build_pointer_type (to_type)
3319 tree to_type;
3321 register tree t = TYPE_POINTER_TO (to_type);
3323 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3325 if (t)
3326 return t;
3328 /* We need a new one. Put this in the same obstack as TO_TYPE. */
3329 push_obstacks (TYPE_OBSTACK (to_type), TYPE_OBSTACK (to_type));
3330 t = make_node (POINTER_TYPE);
3331 pop_obstacks ();
3333 TREE_TYPE (t) = to_type;
3335 /* Record this type as the pointer to TO_TYPE. */
3336 TYPE_POINTER_TO (to_type) = t;
3338 /* Lay out the type. This function has many callers that are concerned
3339 with expression-construction, and this simplifies them all.
3340 Also, it guarantees the TYPE_SIZE is in the same obstack as the type. */
3341 layout_type (t);
3343 return t;
3346 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
3347 MAXVAL should be the maximum value in the domain
3348 (one less than the length of the array). */
3350 tree
3351 build_index_type (maxval)
3352 tree maxval;
3354 register tree itype = make_node (INTEGER_TYPE);
3355 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
3356 TYPE_MIN_VALUE (itype) = build_int_2 (0, 0);
3357 TREE_TYPE (TYPE_MIN_VALUE (itype)) = sizetype;
3358 TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
3359 TYPE_MODE (itype) = TYPE_MODE (sizetype);
3360 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
3361 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
3362 if (TREE_CODE (maxval) == INTEGER_CST)
3364 int maxint = (int) TREE_INT_CST_LOW (maxval);
3365 /* If the domain should be empty, make sure the maxval
3366 remains -1 and is not spoiled by truncation. */
3367 if (INT_CST_LT (maxval, integer_zero_node))
3369 TYPE_MAX_VALUE (itype) = build_int_2 (-1, -1);
3370 TREE_TYPE (TYPE_MAX_VALUE (itype)) = sizetype;
3372 return type_hash_canon (maxint < 0 ? ~maxint : maxint, itype);
3374 else
3375 return itype;
3378 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
3379 ENUMERAL_TYPE, BOOLEAN_TYPE, or CHAR_TYPE), with
3380 low bound LOWVAL and high bound HIGHVAL.
3381 if TYPE==NULL_TREE, sizetype is used. */
3383 tree
3384 build_range_type (type, lowval, highval)
3385 tree type, lowval, highval;
3387 register tree itype = make_node (INTEGER_TYPE);
3388 TREE_TYPE (itype) = type;
3389 if (type == NULL_TREE)
3390 type = sizetype;
3391 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
3392 TYPE_MIN_VALUE (itype) = convert (type, lowval);
3393 TYPE_MAX_VALUE (itype) = convert (type, highval);
3394 TYPE_MODE (itype) = TYPE_MODE (type);
3395 TYPE_SIZE (itype) = TYPE_SIZE (type);
3396 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
3397 if ((TREE_CODE (lowval) == INTEGER_CST)
3398 && (TREE_CODE (highval) == INTEGER_CST))
3400 HOST_WIDE_INT highint = TREE_INT_CST_LOW (highval);
3401 HOST_WIDE_INT lowint = TREE_INT_CST_LOW (lowval);
3402 int maxint = (int) (highint - lowint);
3403 return type_hash_canon (maxint < 0 ? ~maxint : maxint, itype);
3405 else
3406 return itype;
3409 /* Just like build_index_type, but takes lowval and highval instead
3410 of just highval (maxval). */
3412 tree
3413 build_index_2_type (lowval,highval)
3414 tree lowval, highval;
3416 return build_range_type (NULL_TREE, lowval, highval);
3419 /* Return nonzero iff ITYPE1 and ITYPE2 are equal (in the LISP sense).
3420 Needed because when index types are not hashed, equal index types
3421 built at different times appear distinct, even though structurally,
3422 they are not. */
3425 index_type_equal (itype1, itype2)
3426 tree itype1, itype2;
3428 if (TREE_CODE (itype1) != TREE_CODE (itype2))
3429 return 0;
3430 if (TREE_CODE (itype1) == INTEGER_TYPE)
3432 if (TYPE_PRECISION (itype1) != TYPE_PRECISION (itype2)
3433 || TYPE_MODE (itype1) != TYPE_MODE (itype2)
3434 || ! simple_cst_equal (TYPE_SIZE (itype1), TYPE_SIZE (itype2))
3435 || TYPE_ALIGN (itype1) != TYPE_ALIGN (itype2))
3436 return 0;
3437 if (simple_cst_equal (TYPE_MIN_VALUE (itype1), TYPE_MIN_VALUE (itype2))
3438 && simple_cst_equal (TYPE_MAX_VALUE (itype1), TYPE_MAX_VALUE (itype2)))
3439 return 1;
3441 return 0;
3444 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
3445 and number of elements specified by the range of values of INDEX_TYPE.
3446 If such a type has already been constructed, reuse it. */
3448 tree
3449 build_array_type (elt_type, index_type)
3450 tree elt_type, index_type;
3452 register tree t;
3453 int hashcode;
3455 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
3457 error ("arrays of functions are not meaningful");
3458 elt_type = integer_type_node;
3461 /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
3462 build_pointer_type (elt_type);
3464 /* Allocate the array after the pointer type,
3465 in case we free it in type_hash_canon. */
3466 t = make_node (ARRAY_TYPE);
3467 TREE_TYPE (t) = elt_type;
3468 TYPE_DOMAIN (t) = index_type;
3470 if (index_type == 0)
3472 return t;
3475 hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
3476 t = type_hash_canon (hashcode, t);
3478 #if 0 /* This led to crashes, because it could put a temporary node
3479 on the TYPE_NEXT_VARIANT chain of a permanent one. */
3480 /* The main variant of an array type should always
3481 be an array whose element type is the main variant. */
3482 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
3483 change_main_variant (t, build_array_type (TYPE_MAIN_VARIANT (elt_type),
3484 index_type));
3485 #endif
3487 if (TYPE_SIZE (t) == 0)
3488 layout_type (t);
3489 return t;
3492 /* Construct, lay out and return
3493 the type of functions returning type VALUE_TYPE
3494 given arguments of types ARG_TYPES.
3495 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
3496 are data type nodes for the arguments of the function.
3497 If such a type has already been constructed, reuse it. */
3499 tree
3500 build_function_type (value_type, arg_types)
3501 tree value_type, arg_types;
3503 register tree t;
3504 int hashcode;
3506 if (TREE_CODE (value_type) == FUNCTION_TYPE)
3508 error ("function return type cannot be function");
3509 value_type = integer_type_node;
3512 /* Make a node of the sort we want. */
3513 t = make_node (FUNCTION_TYPE);
3514 TREE_TYPE (t) = value_type;
3515 TYPE_ARG_TYPES (t) = arg_types;
3517 /* If we already have such a type, use the old one and free this one. */
3518 hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
3519 t = type_hash_canon (hashcode, t);
3521 if (TYPE_SIZE (t) == 0)
3522 layout_type (t);
3523 return t;
3526 /* Build the node for the type of references-to-TO_TYPE. */
3528 tree
3529 build_reference_type (to_type)
3530 tree to_type;
3532 register tree t = TYPE_REFERENCE_TO (to_type);
3533 register struct obstack *ambient_obstack = current_obstack;
3534 register struct obstack *ambient_saveable_obstack = saveable_obstack;
3536 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3538 if (t)
3539 return t;
3541 /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
3542 if (TREE_PERMANENT (to_type))
3544 current_obstack = &permanent_obstack;
3545 saveable_obstack = &permanent_obstack;
3548 t = make_node (REFERENCE_TYPE);
3549 TREE_TYPE (t) = to_type;
3551 /* Record this type as the pointer to TO_TYPE. */
3552 TYPE_REFERENCE_TO (to_type) = t;
3554 layout_type (t);
3556 current_obstack = ambient_obstack;
3557 saveable_obstack = ambient_saveable_obstack;
3558 return t;
3561 /* Construct, lay out and return the type of methods belonging to class
3562 BASETYPE and whose arguments and values are described by TYPE.
3563 If that type exists already, reuse it.
3564 TYPE must be a FUNCTION_TYPE node. */
3566 tree
3567 build_method_type (basetype, type)
3568 tree basetype, type;
3570 register tree t;
3571 int hashcode;
3573 /* Make a node of the sort we want. */
3574 t = make_node (METHOD_TYPE);
3576 if (TREE_CODE (type) != FUNCTION_TYPE)
3577 abort ();
3579 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3580 TREE_TYPE (t) = TREE_TYPE (type);
3582 /* The actual arglist for this function includes a "hidden" argument
3583 which is "this". Put it into the list of argument types. */
3585 TYPE_ARG_TYPES (t)
3586 = tree_cons (NULL_TREE,
3587 build_pointer_type (basetype), TYPE_ARG_TYPES (type));
3589 /* If we already have such a type, use the old one and free this one. */
3590 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
3591 t = type_hash_canon (hashcode, t);
3593 if (TYPE_SIZE (t) == 0)
3594 layout_type (t);
3596 return t;
3599 /* Construct, lay out and return the type of offsets to a value
3600 of type TYPE, within an object of type BASETYPE.
3601 If a suitable offset type exists already, reuse it. */
3603 tree
3604 build_offset_type (basetype, type)
3605 tree basetype, type;
3607 register tree t;
3608 int hashcode;
3610 /* Make a node of the sort we want. */
3611 t = make_node (OFFSET_TYPE);
3613 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3614 TREE_TYPE (t) = type;
3616 /* If we already have such a type, use the old one and free this one. */
3617 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
3618 t = type_hash_canon (hashcode, t);
3620 if (TYPE_SIZE (t) == 0)
3621 layout_type (t);
3623 return t;
3626 /* Create a complex type whose components are COMPONENT_TYPE. */
3628 tree
3629 build_complex_type (component_type)
3630 tree component_type;
3632 register tree t;
3633 int hashcode;
3635 /* Make a node of the sort we want. */
3636 t = make_node (COMPLEX_TYPE);
3638 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
3639 TYPE_VOLATILE (t) = TYPE_VOLATILE (component_type);
3640 TYPE_READONLY (t) = TYPE_READONLY (component_type);
3642 /* If we already have such a type, use the old one and free this one. */
3643 hashcode = TYPE_HASH (component_type);
3644 t = type_hash_canon (hashcode, t);
3646 if (TYPE_SIZE (t) == 0)
3647 layout_type (t);
3649 return t;
3652 /* Return OP, stripped of any conversions to wider types as much as is safe.
3653 Converting the value back to OP's type makes a value equivalent to OP.
3655 If FOR_TYPE is nonzero, we return a value which, if converted to
3656 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
3658 If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
3659 narrowest type that can hold the value, even if they don't exactly fit.
3660 Otherwise, bit-field references are changed to a narrower type
3661 only if they can be fetched directly from memory in that type.
3663 OP must have integer, real or enumeral type. Pointers are not allowed!
3665 There are some cases where the obvious value we could return
3666 would regenerate to OP if converted to OP's type,
3667 but would not extend like OP to wider types.
3668 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
3669 For example, if OP is (unsigned short)(signed char)-1,
3670 we avoid returning (signed char)-1 if FOR_TYPE is int,
3671 even though extending that to an unsigned short would regenerate OP,
3672 since the result of extending (signed char)-1 to (int)
3673 is different from (int) OP. */
3675 tree
3676 get_unwidened (op, for_type)
3677 register tree op;
3678 tree for_type;
3680 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
3681 /* TYPE_PRECISION is safe in place of type_precision since
3682 pointer types are not allowed. */
3683 register tree type = TREE_TYPE (op);
3684 register unsigned final_prec
3685 = TYPE_PRECISION (for_type != 0 ? for_type : type);
3686 register int uns
3687 = (for_type != 0 && for_type != type
3688 && final_prec > TYPE_PRECISION (type)
3689 && TREE_UNSIGNED (type));
3690 register tree win = op;
3692 while (TREE_CODE (op) == NOP_EXPR)
3694 register int bitschange
3695 = TYPE_PRECISION (TREE_TYPE (op))
3696 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
3698 /* Truncations are many-one so cannot be removed.
3699 Unless we are later going to truncate down even farther. */
3700 if (bitschange < 0
3701 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
3702 break;
3704 /* See what's inside this conversion. If we decide to strip it,
3705 we will set WIN. */
3706 op = TREE_OPERAND (op, 0);
3708 /* If we have not stripped any zero-extensions (uns is 0),
3709 we can strip any kind of extension.
3710 If we have previously stripped a zero-extension,
3711 only zero-extensions can safely be stripped.
3712 Any extension can be stripped if the bits it would produce
3713 are all going to be discarded later by truncating to FOR_TYPE. */
3715 if (bitschange > 0)
3717 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
3718 win = op;
3719 /* TREE_UNSIGNED says whether this is a zero-extension.
3720 Let's avoid computing it if it does not affect WIN
3721 and if UNS will not be needed again. */
3722 if ((uns || TREE_CODE (op) == NOP_EXPR)
3723 && TREE_UNSIGNED (TREE_TYPE (op)))
3725 uns = 1;
3726 win = op;
3731 if (TREE_CODE (op) == COMPONENT_REF
3732 /* Since type_for_size always gives an integer type. */
3733 && TREE_CODE (type) != REAL_TYPE)
3735 unsigned innerprec = TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)));
3736 type = type_for_size (innerprec, TREE_UNSIGNED (TREE_OPERAND (op, 1)));
3738 /* We can get this structure field in the narrowest type it fits in.
3739 If FOR_TYPE is 0, do this only for a field that matches the
3740 narrower type exactly and is aligned for it
3741 The resulting extension to its nominal type (a fullword type)
3742 must fit the same conditions as for other extensions. */
3744 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
3745 && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
3746 && (! uns || final_prec <= innerprec
3747 || TREE_UNSIGNED (TREE_OPERAND (op, 1)))
3748 && type != 0)
3750 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
3751 TREE_OPERAND (op, 1));
3752 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
3753 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
3754 TREE_RAISES (win) = TREE_RAISES (op);
3757 return win;
3760 /* Return OP or a simpler expression for a narrower value
3761 which can be sign-extended or zero-extended to give back OP.
3762 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
3763 or 0 if the value should be sign-extended. */
3765 tree
3766 get_narrower (op, unsignedp_ptr)
3767 register tree op;
3768 int *unsignedp_ptr;
3770 register int uns = 0;
3771 int first = 1;
3772 register tree win = op;
3774 while (TREE_CODE (op) == NOP_EXPR)
3776 register int bitschange
3777 = TYPE_PRECISION (TREE_TYPE (op))
3778 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
3780 /* Truncations are many-one so cannot be removed. */
3781 if (bitschange < 0)
3782 break;
3784 /* See what's inside this conversion. If we decide to strip it,
3785 we will set WIN. */
3786 op = TREE_OPERAND (op, 0);
3788 if (bitschange > 0)
3790 /* An extension: the outermost one can be stripped,
3791 but remember whether it is zero or sign extension. */
3792 if (first)
3793 uns = TREE_UNSIGNED (TREE_TYPE (op));
3794 /* Otherwise, if a sign extension has been stripped,
3795 only sign extensions can now be stripped;
3796 if a zero extension has been stripped, only zero-extensions. */
3797 else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
3798 break;
3799 first = 0;
3801 else /* bitschange == 0 */
3803 /* A change in nominal type can always be stripped, but we must
3804 preserve the unsignedness. */
3805 if (first)
3806 uns = TREE_UNSIGNED (TREE_TYPE (op));
3807 first = 0;
3810 win = op;
3813 if (TREE_CODE (op) == COMPONENT_REF
3814 /* Since type_for_size always gives an integer type. */
3815 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE)
3817 unsigned innerprec = TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)));
3818 tree type = type_for_size (innerprec, TREE_UNSIGNED (op));
3820 /* We can get this structure field in a narrower type that fits it,
3821 but the resulting extension to its nominal type (a fullword type)
3822 must satisfy the same conditions as for other extensions.
3824 Do this only for fields that are aligned (not bit-fields),
3825 because when bit-field insns will be used there is no
3826 advantage in doing this. */
3828 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
3829 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
3830 && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
3831 && type != 0)
3833 if (first)
3834 uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
3835 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
3836 TREE_OPERAND (op, 1));
3837 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
3838 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
3839 TREE_RAISES (win) = TREE_RAISES (op);
3842 *unsignedp_ptr = uns;
3843 return win;
3846 /* Return the precision of a type, for arithmetic purposes.
3847 Supports all types on which arithmetic is possible
3848 (including pointer types).
3849 It's not clear yet what will be right for complex types. */
3852 type_precision (type)
3853 register tree type;
3855 return ((TREE_CODE (type) == INTEGER_TYPE
3856 || TREE_CODE (type) == ENUMERAL_TYPE
3857 || TREE_CODE (type) == REAL_TYPE)
3858 ? TYPE_PRECISION (type) : POINTER_SIZE);
3861 /* Nonzero if integer constant C has a value that is permissible
3862 for type TYPE (an INTEGER_TYPE). */
3865 int_fits_type_p (c, type)
3866 tree c, type;
3868 if (TREE_UNSIGNED (type))
3869 return (! (TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST
3870 && INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c))
3871 && ! (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
3872 && INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type))));
3873 else
3874 return (! (TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST
3875 && INT_CST_LT (TYPE_MAX_VALUE (type), c))
3876 && ! (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
3877 && INT_CST_LT (c, TYPE_MIN_VALUE (type))));
3880 /* Return the innermost context enclosing DECL that is
3881 a FUNCTION_DECL, or zero if none. */
3883 tree
3884 decl_function_context (decl)
3885 tree decl;
3887 tree context;
3889 if (TREE_CODE (decl) == ERROR_MARK)
3890 return 0;
3892 if (TREE_CODE (decl) == SAVE_EXPR)
3893 context = SAVE_EXPR_CONTEXT (decl);
3894 else
3895 context = DECL_CONTEXT (decl);
3897 while (context && TREE_CODE (context) != FUNCTION_DECL)
3899 if (TREE_CODE (context) == RECORD_TYPE
3900 || TREE_CODE (context) == UNION_TYPE)
3901 context = NULL_TREE;
3902 else if (TREE_CODE (context) == TYPE_DECL)
3903 context = DECL_CONTEXT (context);
3904 else if (TREE_CODE (context) == BLOCK)
3905 context = BLOCK_SUPERCONTEXT (context);
3906 else
3907 /* Unhandled CONTEXT !? */
3908 abort ();
3911 return context;
3914 /* Return the innermost context enclosing DECL that is
3915 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
3916 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
3918 tree
3919 decl_type_context (decl)
3920 tree decl;
3922 tree context = DECL_CONTEXT (decl);
3924 while (context)
3926 if (TREE_CODE (context) == RECORD_TYPE
3927 || TREE_CODE (context) == UNION_TYPE
3928 || TREE_CODE (context) == QUAL_UNION_TYPE)
3929 return context;
3930 if (TREE_CODE (context) == TYPE_DECL
3931 || TREE_CODE (context) == FUNCTION_DECL)
3932 context = DECL_CONTEXT (context);
3933 else if (TREE_CODE (context) == BLOCK)
3934 context = BLOCK_SUPERCONTEXT (context);
3935 else
3936 /* Unhandled CONTEXT!? */
3937 abort ();
3939 return NULL_TREE;
3942 void
3943 print_obstack_statistics (str, o)
3944 char *str;
3945 struct obstack *o;
3947 struct _obstack_chunk *chunk = o->chunk;
3948 int n_chunks = 0;
3949 int n_alloc = 0;
3951 while (chunk)
3953 n_chunks += 1;
3954 n_alloc += chunk->limit - &chunk->contents[0];
3955 chunk = chunk->prev;
3957 fprintf (stderr, "obstack %s: %d bytes, %d chunks\n",
3958 str, n_alloc, n_chunks);
3960 void
3961 dump_tree_statistics ()
3963 int i;
3964 int total_nodes, total_bytes;
3966 fprintf (stderr, "\n??? tree nodes created\n\n");
3967 #ifdef GATHER_STATISTICS
3968 fprintf (stderr, "Kind Nodes Bytes\n");
3969 fprintf (stderr, "-------------------------------------\n");
3970 total_nodes = total_bytes = 0;
3971 for (i = 0; i < (int) all_kinds; i++)
3973 fprintf (stderr, "%-20s %6d %9d\n", tree_node_kind_names[i],
3974 tree_node_counts[i], tree_node_sizes[i]);
3975 total_nodes += tree_node_counts[i];
3976 total_bytes += tree_node_sizes[i];
3978 fprintf (stderr, "%-20s %9d\n", "identifier names", id_string_size);
3979 fprintf (stderr, "-------------------------------------\n");
3980 fprintf (stderr, "%-20s %6d %9d\n", "Total", total_nodes, total_bytes);
3981 fprintf (stderr, "-------------------------------------\n");
3982 #else
3983 fprintf (stderr, "(No per-node statistics)\n");
3984 #endif
3985 print_lang_statistics ();
3988 #define FILE_FUNCTION_PREFIX_LEN 9
3990 #ifndef NO_DOLLAR_IN_LABEL
3991 #define FILE_FUNCTION_FORMAT "_GLOBAL_$D$%s"
3992 #else /* NO_DOLLAR_IN_LABEL */
3993 #ifndef NO_DOT_IN_LABEL
3994 #define FILE_FUNCTION_FORMAT "_GLOBAL_.D.%s"
3995 #else /* NO_DOT_IN_LABEL */
3996 #define FILE_FUNCTION_FORMAT "_GLOBAL__D_%s"
3997 #endif /* NO_DOT_IN_LABEL */
3998 #endif /* NO_DOLLAR_IN_LABEL */
4000 extern char * first_global_object_name;
4002 /* If KIND=='I', return a suitable global initializer (constructor) name.
4003 If KIND=='D', return a suitable global clean-up (destructor) name. */
4005 tree
4006 get_file_function_name (kind)
4007 int kind;
4009 char *buf;
4010 register char *p;
4012 if (first_global_object_name)
4013 p = first_global_object_name;
4014 else if (main_input_filename)
4015 p = main_input_filename;
4016 else
4017 p = input_filename;
4019 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p));
4021 /* Set up the name of the file-level functions we may need. */
4022 /* Use a global object (which is already required to be unique over
4023 the program) rather than the file name (which imposes extra
4024 constraints). -- Raeburn@MIT.EDU, 10 Jan 1990. */
4025 sprintf (buf, FILE_FUNCTION_FORMAT, p);
4027 /* Don't need to pull wierd characters out of global names. */
4028 if (p != first_global_object_name)
4030 for (p = buf+11; *p; p++)
4031 if (! ((*p >= '0' && *p <= '9')
4032 #if 0 /* we always want labels, which are valid C++ identifiers (+ `$') */
4033 #ifndef ASM_IDENTIFY_GCC /* this is required if `.' is invalid -- k. raeburn */
4034 || *p == '.'
4035 #endif
4036 #endif
4037 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
4038 || *p == '$'
4039 #endif
4040 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
4041 || *p == '.'
4042 #endif
4043 || (*p >= 'A' && *p <= 'Z')
4044 || (*p >= 'a' && *p <= 'z')))
4045 *p = '_';
4048 buf[FILE_FUNCTION_PREFIX_LEN] = kind;
4050 return get_identifier (buf);
4053 /* Expand (the constant part of) a SET_TYPE CONTRUCTOR node.
4054 The result is placed in BUFFER (which has length BIT_SIZE),
4055 with one bit in each char ('\000' or '\001').
4057 If the constructor is constant, NULL_TREE is returned.
4058 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4060 tree
4061 get_set_constructor_bits (init, buffer, bit_size)
4062 tree init;
4063 char *buffer;
4064 int bit_size;
4066 int i;
4067 tree vals;
4068 HOST_WIDE_INT domain_min
4069 = TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (init))));
4070 tree non_const_bits = NULL_TREE;
4071 for (i = 0; i < bit_size; i++)
4072 buffer[i] = 0;
4074 for (vals = TREE_OPERAND (init, 1);
4075 vals != NULL_TREE; vals = TREE_CHAIN (vals))
4077 if (TREE_CODE (TREE_VALUE (vals)) != INTEGER_CST
4078 || (TREE_PURPOSE (vals) != NULL_TREE
4079 && TREE_CODE (TREE_PURPOSE (vals)) != INTEGER_CST))
4080 non_const_bits =
4081 tree_cons (TREE_PURPOSE (vals), TREE_VALUE (vals), non_const_bits);
4082 else if (TREE_PURPOSE (vals) != NULL_TREE)
4084 /* Set a range of bits to ones. */
4085 HOST_WIDE_INT lo_index
4086 = TREE_INT_CST_LOW (TREE_PURPOSE (vals)) - domain_min;
4087 HOST_WIDE_INT hi_index
4088 = TREE_INT_CST_LOW (TREE_VALUE (vals)) - domain_min;
4089 if (lo_index < 0 || lo_index >= bit_size
4090 || hi_index < 0 || hi_index >= bit_size)
4091 abort ();
4092 for ( ; lo_index <= hi_index; lo_index++)
4093 buffer[lo_index] = 1;
4095 else
4097 /* Set a single bit to one. */
4098 HOST_WIDE_INT index
4099 = TREE_INT_CST_LOW (TREE_VALUE (vals)) - domain_min;
4100 if (index < 0 || index >= bit_size)
4102 error ("invalid initializer for bit string");
4103 return NULL_TREE;
4105 buffer[index] = 1;
4108 return non_const_bits;
4111 /* Expand (the constant part of) a SET_TYPE CONTRUCTOR node.
4112 The result is placed in BUFFER (which is an array of bytes).
4113 If the constructor is constant, NULL_TREE is returned.
4114 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4116 tree
4117 get_set_constructor_bytes (init, buffer, wd_size)
4118 tree init;
4119 unsigned char *buffer;
4120 int wd_size;
4122 int i;
4123 tree vals = TREE_OPERAND (init, 1);
4124 int set_word_size = BITS_PER_UNIT;
4125 int bit_size = wd_size * set_word_size;
4126 int bit_pos = 0;
4127 unsigned char *bytep = buffer;
4128 char *bit_buffer = (char*)alloca(bit_size);
4129 tree non_const_bits = get_set_constructor_bits (init, bit_buffer, bit_size);
4131 for (i = 0; i < wd_size; i++)
4132 buffer[i] = 0;
4134 for (i = 0; i < bit_size; i++)
4136 if (bit_buffer[i])
4138 if (BITS_BIG_ENDIAN)
4139 *bytep |= (1 << (set_word_size - 1 - bit_pos));
4140 else
4141 *bytep |= 1 << bit_pos;
4143 bit_pos++;
4144 if (bit_pos >= set_word_size)
4145 bit_pos = 0, bytep++;
4147 return non_const_bits;