(high_{block,function}_linenum): New variables.
[official-gcc.git] / gcc / tree.c
blobdb862e818938db722d2c7e98a7533530a0079247
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 ();
267 static tree stabilize_reference_1 ();
269 /* Init the principal obstacks. */
271 void
272 init_obstacks ()
274 gcc_obstack_init (&obstack_stack_obstack);
275 gcc_obstack_init (&permanent_obstack);
277 gcc_obstack_init (&temporary_obstack);
278 temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
279 gcc_obstack_init (&momentary_obstack);
280 momentary_firstobj = (char *) obstack_alloc (&momentary_obstack, 0);
281 momentary_function_firstobj = momentary_firstobj;
282 gcc_obstack_init (&maybepermanent_obstack);
283 maybepermanent_firstobj
284 = (char *) obstack_alloc (&maybepermanent_obstack, 0);
285 gcc_obstack_init (&temp_decl_obstack);
286 temp_decl_firstobj = (char *) obstack_alloc (&temp_decl_obstack, 0);
288 function_obstack = &temporary_obstack;
289 function_maybepermanent_obstack = &maybepermanent_obstack;
290 current_obstack = &permanent_obstack;
291 expression_obstack = &permanent_obstack;
292 rtl_obstack = saveable_obstack = &permanent_obstack;
294 /* Init the hash table of identifiers. */
295 bzero ((char *) hash_table, sizeof hash_table);
298 void
299 gcc_obstack_init (obstack)
300 struct obstack *obstack;
302 /* Let particular systems override the size of a chunk. */
303 #ifndef OBSTACK_CHUNK_SIZE
304 #define OBSTACK_CHUNK_SIZE 0
305 #endif
306 /* Let them override the alloc and free routines too. */
307 #ifndef OBSTACK_CHUNK_ALLOC
308 #define OBSTACK_CHUNK_ALLOC xmalloc
309 #endif
310 #ifndef OBSTACK_CHUNK_FREE
311 #define OBSTACK_CHUNK_FREE free
312 #endif
313 _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
314 (void *(*) ()) OBSTACK_CHUNK_ALLOC,
315 (void (*) ()) OBSTACK_CHUNK_FREE);
318 /* Save all variables describing the current status into the structure *P.
319 This is used before starting a nested function. */
321 void
322 save_tree_status (p, toplevel)
323 struct function *p;
324 int toplevel;
326 p->all_types_permanent = all_types_permanent;
327 p->momentary_stack = momentary_stack;
328 p->maybepermanent_firstobj = maybepermanent_firstobj;
329 p->momentary_firstobj = momentary_firstobj;
330 p->momentary_function_firstobj = momentary_function_firstobj;
331 p->function_obstack = function_obstack;
332 p->function_maybepermanent_obstack = function_maybepermanent_obstack;
333 p->current_obstack = current_obstack;
334 p->expression_obstack = expression_obstack;
335 p->saveable_obstack = saveable_obstack;
336 p->rtl_obstack = rtl_obstack;
338 if (! toplevel)
340 /* Objects that need to be saved in this function can be in the nonsaved
341 obstack of the enclosing function since they can't possibly be needed
342 once it has returned. */
343 function_maybepermanent_obstack = function_obstack;
344 maybepermanent_firstobj
345 = (char *) obstack_finish (function_maybepermanent_obstack);
348 function_obstack = (struct obstack *) xmalloc (sizeof (struct obstack));
349 gcc_obstack_init (function_obstack);
351 current_obstack = &permanent_obstack;
352 expression_obstack = &permanent_obstack;
353 rtl_obstack = saveable_obstack = &permanent_obstack;
355 momentary_firstobj = (char *) obstack_finish (&momentary_obstack);
356 momentary_function_firstobj = momentary_firstobj;
359 /* Restore all variables describing the current status from the structure *P.
360 This is used after a nested function. */
362 void
363 restore_tree_status (p, toplevel)
364 struct function *p;
365 int toplevel;
367 all_types_permanent = p->all_types_permanent;
368 momentary_stack = p->momentary_stack;
370 obstack_free (&momentary_obstack, momentary_function_firstobj);
372 if (! toplevel)
374 /* Free saveable storage used by the function just compiled and not
375 saved.
377 CAUTION: This is in function_obstack of the containing function.
378 So we must be sure that we never allocate from that obstack during
379 the compilation of a nested function if we expect it to survive
380 past the nested function's end. */
381 obstack_free (function_maybepermanent_obstack, maybepermanent_firstobj);
384 obstack_free (function_obstack, 0);
385 free (function_obstack);
387 momentary_firstobj = p->momentary_firstobj;
388 momentary_function_firstobj = p->momentary_function_firstobj;
389 maybepermanent_firstobj = p->maybepermanent_firstobj;
390 function_obstack = p->function_obstack;
391 function_maybepermanent_obstack = p->function_maybepermanent_obstack;
392 current_obstack = p->current_obstack;
393 expression_obstack = p->expression_obstack;
394 saveable_obstack = p->saveable_obstack;
395 rtl_obstack = p->rtl_obstack;
398 /* Start allocating on the temporary (per function) obstack.
399 This is done in start_function before parsing the function body,
400 and before each initialization at top level, and to go back
401 to temporary allocation after doing permanent_allocation. */
403 void
404 temporary_allocation ()
406 /* Note that function_obstack at top level points to temporary_obstack.
407 But within a nested function context, it is a separate obstack. */
408 current_obstack = function_obstack;
409 expression_obstack = function_obstack;
410 rtl_obstack = saveable_obstack = function_maybepermanent_obstack;
411 momentary_stack = 0;
414 /* Start allocating on the permanent obstack but don't
415 free the temporary data. After calling this, call
416 `permanent_allocation' to fully resume permanent allocation status. */
418 void
419 end_temporary_allocation ()
421 current_obstack = &permanent_obstack;
422 expression_obstack = &permanent_obstack;
423 rtl_obstack = saveable_obstack = &permanent_obstack;
426 /* Resume allocating on the temporary obstack, undoing
427 effects of `end_temporary_allocation'. */
429 void
430 resume_temporary_allocation ()
432 current_obstack = function_obstack;
433 expression_obstack = function_obstack;
434 rtl_obstack = saveable_obstack = function_maybepermanent_obstack;
437 /* While doing temporary allocation, switch to allocating in such a
438 way as to save all nodes if the function is inlined. Call
439 resume_temporary_allocation to go back to ordinary temporary
440 allocation. */
442 void
443 saveable_allocation ()
445 /* Note that function_obstack at top level points to temporary_obstack.
446 But within a nested function context, it is a separate obstack. */
447 expression_obstack = current_obstack = saveable_obstack;
450 /* Switch to current obstack CURRENT and maybepermanent obstack SAVEABLE,
451 recording the previously current obstacks on a stack.
452 This does not free any storage in any obstack. */
454 void
455 push_obstacks (current, saveable)
456 struct obstack *current, *saveable;
458 struct obstack_stack *p
459 = (struct obstack_stack *) obstack_alloc (&obstack_stack_obstack,
460 (sizeof (struct obstack_stack)));
462 p->current = current_obstack;
463 p->saveable = saveable_obstack;
464 p->expression = expression_obstack;
465 p->rtl = rtl_obstack;
466 p->next = obstack_stack;
467 obstack_stack = p;
469 current_obstack = current;
470 expression_obstack = current;
471 rtl_obstack = saveable_obstack = saveable;
474 /* Save the current set of obstacks, but don't change them. */
476 void
477 push_obstacks_nochange ()
479 struct obstack_stack *p
480 = (struct obstack_stack *) obstack_alloc (&obstack_stack_obstack,
481 (sizeof (struct obstack_stack)));
483 p->current = current_obstack;
484 p->saveable = saveable_obstack;
485 p->expression = expression_obstack;
486 p->rtl = rtl_obstack;
487 p->next = obstack_stack;
488 obstack_stack = p;
491 /* Pop the obstack selection stack. */
493 void
494 pop_obstacks ()
496 struct obstack_stack *p = obstack_stack;
497 obstack_stack = p->next;
499 current_obstack = p->current;
500 saveable_obstack = p->saveable;
501 expression_obstack = p->expression;
502 rtl_obstack = p->rtl;
504 obstack_free (&obstack_stack_obstack, p);
507 /* Nonzero if temporary allocation is currently in effect.
508 Zero if currently doing permanent allocation. */
511 allocation_temporary_p ()
513 return current_obstack != &permanent_obstack;
516 /* Go back to allocating on the permanent obstack
517 and free everything in the temporary obstack.
519 FUNCTION_END is true only if we have just finished compiling a function.
520 In that case, we also free preserved initial values on the momentary
521 obstack. */
523 void
524 permanent_allocation (function_end)
525 int function_end;
527 /* Free up previous temporary obstack data */
528 obstack_free (&temporary_obstack, temporary_firstobj);
529 if (function_end)
531 obstack_free (&momentary_obstack, momentary_function_firstobj);
532 momentary_firstobj = momentary_function_firstobj;
534 else
535 obstack_free (&momentary_obstack, momentary_firstobj);
536 obstack_free (&maybepermanent_obstack, maybepermanent_firstobj);
537 obstack_free (&temp_decl_obstack, temp_decl_firstobj);
539 current_obstack = &permanent_obstack;
540 expression_obstack = &permanent_obstack;
541 rtl_obstack = saveable_obstack = &permanent_obstack;
544 /* Save permanently everything on the maybepermanent_obstack. */
546 void
547 preserve_data ()
549 maybepermanent_firstobj
550 = (char *) obstack_alloc (function_maybepermanent_obstack, 0);
553 void
554 preserve_initializer ()
556 struct momentary_level *tem;
557 char *old_momentary;
559 temporary_firstobj
560 = (char *) obstack_alloc (&temporary_obstack, 0);
561 maybepermanent_firstobj
562 = (char *) obstack_alloc (function_maybepermanent_obstack, 0);
564 old_momentary = momentary_firstobj;
565 momentary_firstobj
566 = (char *) obstack_alloc (&momentary_obstack, 0);
567 if (momentary_firstobj != old_momentary)
568 for (tem = momentary_stack; tem; tem = tem->prev)
569 tem->base = momentary_firstobj;
572 /* Start allocating new rtl in current_obstack.
573 Use resume_temporary_allocation
574 to go back to allocating rtl in saveable_obstack. */
576 void
577 rtl_in_current_obstack ()
579 rtl_obstack = current_obstack;
582 /* Start allocating rtl from saveable_obstack. Intended to be used after
583 a call to push_obstacks_nochange. */
585 void
586 rtl_in_saveable_obstack ()
588 rtl_obstack = saveable_obstack;
591 /* Allocate SIZE bytes in the current obstack
592 and return a pointer to them.
593 In practice the current obstack is always the temporary one. */
595 char *
596 oballoc (size)
597 int size;
599 return (char *) obstack_alloc (current_obstack, size);
602 /* Free the object PTR in the current obstack
603 as well as everything allocated since PTR.
604 In practice the current obstack is always the temporary one. */
606 void
607 obfree (ptr)
608 char *ptr;
610 obstack_free (current_obstack, ptr);
613 /* Allocate SIZE bytes in the permanent obstack
614 and return a pointer to them. */
616 char *
617 permalloc (size)
618 int size;
620 return (char *) obstack_alloc (&permanent_obstack, size);
623 /* Allocate NELEM items of SIZE bytes in the permanent obstack
624 and return a pointer to them. The storage is cleared before
625 returning the value. */
627 char *
628 perm_calloc (nelem, size)
629 int nelem;
630 long size;
632 char *rval = (char *) obstack_alloc (&permanent_obstack, nelem * size);
633 bzero (rval, nelem * size);
634 return rval;
637 /* Allocate SIZE bytes in the saveable obstack
638 and return a pointer to them. */
640 char *
641 savealloc (size)
642 int size;
644 return (char *) obstack_alloc (saveable_obstack, size);
647 /* Print out which obstack an object is in. */
649 void
650 print_obstack_name (object, file, prefix)
651 char *object;
652 FILE *file;
653 char *prefix;
655 struct obstack *obstack = NULL;
656 char *obstack_name = NULL;
657 struct function *p;
659 for (p = outer_function_chain; p; p = p->next)
661 if (_obstack_allocated_p (p->function_obstack, object))
663 obstack = p->function_obstack;
664 obstack_name = "containing function obstack";
666 if (_obstack_allocated_p (p->function_maybepermanent_obstack, object))
668 obstack = p->function_maybepermanent_obstack;
669 obstack_name = "containing function maybepermanent obstack";
673 if (_obstack_allocated_p (&obstack_stack_obstack, object))
675 obstack = &obstack_stack_obstack;
676 obstack_name = "obstack_stack_obstack";
678 else if (_obstack_allocated_p (function_obstack, object))
680 obstack = function_obstack;
681 obstack_name = "function obstack";
683 else if (_obstack_allocated_p (&permanent_obstack, object))
685 obstack = &permanent_obstack;
686 obstack_name = "permanent_obstack";
688 else if (_obstack_allocated_p (&momentary_obstack, object))
690 obstack = &momentary_obstack;
691 obstack_name = "momentary_obstack";
693 else if (_obstack_allocated_p (function_maybepermanent_obstack, object))
695 obstack = function_maybepermanent_obstack;
696 obstack_name = "function maybepermanent obstack";
698 else if (_obstack_allocated_p (&temp_decl_obstack, object))
700 obstack = &temp_decl_obstack;
701 obstack_name = "temp_decl_obstack";
704 /* Check to see if the object is in the free area of the obstack. */
705 if (obstack != NULL)
707 if (object >= obstack->next_free
708 && object < obstack->chunk_limit)
709 fprintf (file, "%s in free portion of obstack %s",
710 prefix, obstack_name);
711 else
712 fprintf (file, "%s allocated from %s", prefix, obstack_name);
714 else
715 fprintf (file, "%s not allocated from any obstack", prefix);
718 void
719 debug_obstack (object)
720 char *object;
722 print_obstack_name (object, stderr, "object");
723 fprintf (stderr, ".\n");
726 /* Return 1 if OBJ is in the permanent obstack.
727 This is slow, and should be used only for debugging.
728 Use TREE_PERMANENT for other purposes. */
731 object_permanent_p (obj)
732 tree obj;
734 return _obstack_allocated_p (&permanent_obstack, obj);
737 /* Start a level of momentary allocation.
738 In C, each compound statement has its own level
739 and that level is freed at the end of each statement.
740 All expression nodes are allocated in the momentary allocation level. */
742 void
743 push_momentary ()
745 struct momentary_level *tem
746 = (struct momentary_level *) obstack_alloc (&momentary_obstack,
747 sizeof (struct momentary_level));
748 tem->prev = momentary_stack;
749 tem->base = (char *) obstack_base (&momentary_obstack);
750 tem->obstack = expression_obstack;
751 momentary_stack = tem;
752 expression_obstack = &momentary_obstack;
755 /* Set things up so the next clear_momentary will only clear memory
756 past our present position in momentary_obstack. */
758 void
759 preserve_momentary ()
761 momentary_stack->base = (char *) obstack_base (&momentary_obstack);
764 /* Free all the storage in the current momentary-allocation level.
765 In C, this happens at the end of each statement. */
767 void
768 clear_momentary ()
770 obstack_free (&momentary_obstack, momentary_stack->base);
773 /* Discard a level of momentary allocation.
774 In C, this happens at the end of each compound statement.
775 Restore the status of expression node allocation
776 that was in effect before this level was created. */
778 void
779 pop_momentary ()
781 struct momentary_level *tem = momentary_stack;
782 momentary_stack = tem->prev;
783 expression_obstack = tem->obstack;
784 /* We can't free TEM from the momentary_obstack, because there might
785 be objects above it which have been saved. We can free back to the
786 stack of the level we are popping off though. */
787 obstack_free (&momentary_obstack, tem->base);
790 /* Pop back to the previous level of momentary allocation,
791 but don't free any momentary data just yet. */
793 void
794 pop_momentary_nofree ()
796 struct momentary_level *tem = momentary_stack;
797 momentary_stack = tem->prev;
798 expression_obstack = tem->obstack;
801 /* Call when starting to parse a declaration:
802 make expressions in the declaration last the length of the function.
803 Returns an argument that should be passed to resume_momentary later. */
806 suspend_momentary ()
808 register int tem = expression_obstack == &momentary_obstack;
809 expression_obstack = saveable_obstack;
810 return tem;
813 /* Call when finished parsing a declaration:
814 restore the treatment of node-allocation that was
815 in effect before the suspension.
816 YES should be the value previously returned by suspend_momentary. */
818 void
819 resume_momentary (yes)
820 int yes;
822 if (yes)
823 expression_obstack = &momentary_obstack;
826 /* Init the tables indexed by tree code.
827 Note that languages can add to these tables to define their own codes. */
829 void
830 init_tree_codes ()
832 tree_code_type = (char **) xmalloc (sizeof (standard_tree_code_type));
833 tree_code_length = (int *) xmalloc (sizeof (standard_tree_code_length));
834 tree_code_name = (char **) xmalloc (sizeof (standard_tree_code_name));
835 bcopy ((char *) standard_tree_code_type, (char *) tree_code_type,
836 sizeof (standard_tree_code_type));
837 bcopy ((char *) standard_tree_code_length, (char *) tree_code_length,
838 sizeof (standard_tree_code_length));
839 bcopy ((char *) standard_tree_code_name, (char *) tree_code_name,
840 sizeof (standard_tree_code_name));
843 /* Return a newly allocated node of code CODE.
844 Initialize the node's unique id and its TREE_PERMANENT flag.
845 For decl and type nodes, some other fields are initialized.
846 The rest of the node is initialized to zero.
848 Achoo! I got a code in the node. */
850 tree
851 make_node (code)
852 enum tree_code code;
854 register tree t;
855 register int type = TREE_CODE_CLASS (code);
856 register int length;
857 register struct obstack *obstack = current_obstack;
858 register int i;
859 register tree_node_kind kind;
861 switch (type)
863 case 'd': /* A decl node */
864 #ifdef GATHER_STATISTICS
865 kind = d_kind;
866 #endif
867 length = sizeof (struct tree_decl);
868 /* All decls in an inline function need to be saved. */
869 if (obstack != &permanent_obstack)
870 obstack = saveable_obstack;
872 /* PARM_DECLs go on the context of the parent. If this is a nested
873 function, then we must allocate the PARM_DECL on the parent's
874 obstack, so that they will live to the end of the parent's
875 closing brace. This is neccesary in case we try to inline the
876 function into its parent.
878 PARM_DECLs of top-level functions do not have this problem. However,
879 we allocate them where we put the FUNCTION_DECL for languauges such as
880 Ada that need to consult some flags in the PARM_DECLs of the function
881 when calling it.
883 See comment in restore_tree_status for why we can't put this
884 in function_obstack. */
885 if (code == PARM_DECL && obstack != &permanent_obstack)
887 tree context = 0;
888 if (current_function_decl)
889 context = decl_function_context (current_function_decl);
891 if (context)
892 obstack
893 = find_function_data (context)->function_maybepermanent_obstack;
895 break;
897 case 't': /* a type node */
898 #ifdef GATHER_STATISTICS
899 kind = t_kind;
900 #endif
901 length = sizeof (struct tree_type);
902 /* All data types are put where we can preserve them if nec. */
903 if (obstack != &permanent_obstack)
904 obstack = all_types_permanent ? &permanent_obstack : saveable_obstack;
905 break;
907 case 'b': /* a lexical block */
908 #ifdef GATHER_STATISTICS
909 kind = b_kind;
910 #endif
911 length = sizeof (struct tree_block);
912 /* All BLOCK nodes are put where we can preserve them if nec. */
913 if (obstack != &permanent_obstack)
914 obstack = saveable_obstack;
915 break;
917 case 's': /* an expression with side effects */
918 #ifdef GATHER_STATISTICS
919 kind = s_kind;
920 goto usual_kind;
921 #endif
922 case 'r': /* a reference */
923 #ifdef GATHER_STATISTICS
924 kind = r_kind;
925 goto usual_kind;
926 #endif
927 case 'e': /* an expression */
928 case '<': /* a comparison expression */
929 case '1': /* a unary arithmetic expression */
930 case '2': /* a binary arithmetic expression */
931 #ifdef GATHER_STATISTICS
932 kind = e_kind;
933 usual_kind:
934 #endif
935 obstack = expression_obstack;
936 /* All BIND_EXPR nodes are put where we can preserve them if nec. */
937 if (code == BIND_EXPR && obstack != &permanent_obstack)
938 obstack = saveable_obstack;
939 length = sizeof (struct tree_exp)
940 + (tree_code_length[(int) code] - 1) * sizeof (char *);
941 break;
943 case 'c': /* a constant */
944 #ifdef GATHER_STATISTICS
945 kind = c_kind;
946 #endif
947 obstack = expression_obstack;
949 /* We can't use tree_code_length for INTEGER_CST, since the number of
950 words is machine-dependent due to varying length of HOST_WIDE_INT,
951 which might be wider than a pointer (e.g., long long). Similarly
952 for REAL_CST, since the number of words is machine-dependent due
953 to varying size and alignment of `double'. */
955 if (code == INTEGER_CST)
956 length = sizeof (struct tree_int_cst);
957 else if (code == REAL_CST)
958 length = sizeof (struct tree_real_cst);
959 else
960 length = sizeof (struct tree_common)
961 + tree_code_length[(int) code] * sizeof (char *);
962 break;
964 case 'x': /* something random, like an identifier. */
965 #ifdef GATHER_STATISTICS
966 if (code == IDENTIFIER_NODE)
967 kind = id_kind;
968 else if (code == OP_IDENTIFIER)
969 kind = op_id_kind;
970 else if (code == TREE_VEC)
971 kind = vec_kind;
972 else
973 kind = x_kind;
974 #endif
975 length = sizeof (struct tree_common)
976 + tree_code_length[(int) code] * sizeof (char *);
977 /* Identifier nodes are always permanent since they are
978 unique in a compiler run. */
979 if (code == IDENTIFIER_NODE) obstack = &permanent_obstack;
980 break;
982 default:
983 abort ();
986 t = (tree) obstack_alloc (obstack, length);
988 #ifdef GATHER_STATISTICS
989 tree_node_counts[(int)kind]++;
990 tree_node_sizes[(int)kind] += length;
991 #endif
993 /* Clear a word at a time. */
994 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
995 ((int *) t)[i] = 0;
996 /* Clear any extra bytes. */
997 for (i = length / sizeof (int) * sizeof (int); i < length; i++)
998 ((char *) t)[i] = 0;
1000 TREE_SET_CODE (t, code);
1001 if (obstack == &permanent_obstack)
1002 TREE_PERMANENT (t) = 1;
1004 switch (type)
1006 case 's':
1007 TREE_SIDE_EFFECTS (t) = 1;
1008 TREE_TYPE (t) = void_type_node;
1009 break;
1011 case 'd':
1012 if (code != FUNCTION_DECL)
1013 DECL_ALIGN (t) = 1;
1014 DECL_IN_SYSTEM_HEADER (t)
1015 = in_system_header && (obstack == &permanent_obstack);
1016 DECL_SOURCE_LINE (t) = lineno;
1017 DECL_SOURCE_FILE (t) = (input_filename) ? input_filename : "<built-in>";
1018 DECL_UID (t) = next_decl_uid++;
1019 break;
1021 case 't':
1022 TYPE_UID (t) = next_type_uid++;
1023 TYPE_ALIGN (t) = 1;
1024 TYPE_MAIN_VARIANT (t) = t;
1025 TYPE_OBSTACK (t) = obstack;
1026 TYPE_ATTRIBUTES (t) = NULL_TREE;
1027 #ifdef SET_DEFAULT_TYPE_ATTRIBUTES
1028 SET_DEFAULT_TYPE_ATTRIBUTES (t);
1029 #endif
1030 break;
1032 case 'c':
1033 TREE_CONSTANT (t) = 1;
1034 break;
1037 return t;
1040 /* Return a new node with the same contents as NODE
1041 except that its TREE_CHAIN is zero and it has a fresh uid. */
1043 tree
1044 copy_node (node)
1045 tree node;
1047 register tree t;
1048 register enum tree_code code = TREE_CODE (node);
1049 register int length;
1050 register int i;
1052 switch (TREE_CODE_CLASS (code))
1054 case 'd': /* A decl node */
1055 length = sizeof (struct tree_decl);
1056 break;
1058 case 't': /* a type node */
1059 length = sizeof (struct tree_type);
1060 break;
1062 case 'b': /* a lexical block node */
1063 length = sizeof (struct tree_block);
1064 break;
1066 case 'r': /* a reference */
1067 case 'e': /* an expression */
1068 case 's': /* an expression with side effects */
1069 case '<': /* a comparison expression */
1070 case '1': /* a unary arithmetic expression */
1071 case '2': /* a binary arithmetic expression */
1072 length = sizeof (struct tree_exp)
1073 + (tree_code_length[(int) code] - 1) * sizeof (char *);
1074 break;
1076 case 'c': /* a constant */
1077 /* We can't use tree_code_length for INTEGER_CST, since the number of
1078 words is machine-dependent due to varying length of HOST_WIDE_INT,
1079 which might be wider than a pointer (e.g., long long). Similarly
1080 for REAL_CST, since the number of words is machine-dependent due
1081 to varying size and alignment of `double'. */
1082 if (code == INTEGER_CST)
1084 length = sizeof (struct tree_int_cst);
1085 break;
1087 else if (code == REAL_CST)
1089 length = sizeof (struct tree_real_cst);
1090 break;
1093 case 'x': /* something random, like an identifier. */
1094 length = sizeof (struct tree_common)
1095 + tree_code_length[(int) code] * sizeof (char *);
1096 if (code == TREE_VEC)
1097 length += (TREE_VEC_LENGTH (node) - 1) * sizeof (char *);
1100 t = (tree) obstack_alloc (current_obstack, length);
1102 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
1103 ((int *) t)[i] = ((int *) node)[i];
1104 /* Clear any extra bytes. */
1105 for (i = length / sizeof (int) * sizeof (int); i < length; i++)
1106 ((char *) t)[i] = ((char *) node)[i];
1108 TREE_CHAIN (t) = 0;
1110 if (TREE_CODE_CLASS (code) == 'd')
1111 DECL_UID (t) = next_decl_uid++;
1112 else if (TREE_CODE_CLASS (code) == 't')
1114 TYPE_UID (t) = next_type_uid++;
1115 TYPE_OBSTACK (t) = current_obstack;
1118 TREE_PERMANENT (t) = (current_obstack == &permanent_obstack);
1120 return t;
1123 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1124 For example, this can copy a list made of TREE_LIST nodes. */
1126 tree
1127 copy_list (list)
1128 tree list;
1130 tree head;
1131 register tree prev, next;
1133 if (list == 0)
1134 return 0;
1136 head = prev = copy_node (list);
1137 next = TREE_CHAIN (list);
1138 while (next)
1140 TREE_CHAIN (prev) = copy_node (next);
1141 prev = TREE_CHAIN (prev);
1142 next = TREE_CHAIN (next);
1144 return head;
1147 #define HASHBITS 30
1149 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
1150 If an identifier with that name has previously been referred to,
1151 the same node is returned this time. */
1153 tree
1154 get_identifier (text)
1155 register char *text;
1157 register int hi;
1158 register int i;
1159 register tree idp;
1160 register int len, hash_len;
1162 /* Compute length of text in len. */
1163 for (len = 0; text[len]; len++);
1165 /* Decide how much of that length to hash on */
1166 hash_len = len;
1167 if (warn_id_clash && len > id_clash_len)
1168 hash_len = id_clash_len;
1170 /* Compute hash code */
1171 hi = hash_len * 613 + (unsigned)text[0];
1172 for (i = 1; i < hash_len; i += 2)
1173 hi = ((hi * 613) + (unsigned)(text[i]));
1175 hi &= (1 << HASHBITS) - 1;
1176 hi %= MAX_HASH_TABLE;
1178 /* Search table for identifier */
1179 for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
1180 if (IDENTIFIER_LENGTH (idp) == len
1181 && IDENTIFIER_POINTER (idp)[0] == text[0]
1182 && !bcmp (IDENTIFIER_POINTER (idp), text, len))
1183 return idp; /* <-- return if found */
1185 /* Not found; optionally warn about a similar identifier */
1186 if (warn_id_clash && do_identifier_warnings && len >= id_clash_len)
1187 for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
1188 if (!strncmp (IDENTIFIER_POINTER (idp), text, id_clash_len))
1190 warning ("`%s' and `%s' identical in first %d characters",
1191 IDENTIFIER_POINTER (idp), text, id_clash_len);
1192 break;
1195 if (tree_code_length[(int) IDENTIFIER_NODE] < 0)
1196 abort (); /* set_identifier_size hasn't been called. */
1198 /* Not found, create one, add to chain */
1199 idp = make_node (IDENTIFIER_NODE);
1200 IDENTIFIER_LENGTH (idp) = len;
1201 #ifdef GATHER_STATISTICS
1202 id_string_size += len;
1203 #endif
1205 IDENTIFIER_POINTER (idp) = obstack_copy0 (&permanent_obstack, text, len);
1207 TREE_CHAIN (idp) = hash_table[hi];
1208 hash_table[hi] = idp;
1209 return idp; /* <-- return if created */
1212 /* Enable warnings on similar identifiers (if requested).
1213 Done after the built-in identifiers are created. */
1215 void
1216 start_identifier_warnings ()
1218 do_identifier_warnings = 1;
1221 /* Record the size of an identifier node for the language in use.
1222 SIZE is the total size in bytes.
1223 This is called by the language-specific files. This must be
1224 called before allocating any identifiers. */
1226 void
1227 set_identifier_size (size)
1228 int size;
1230 tree_code_length[(int) IDENTIFIER_NODE]
1231 = (size - sizeof (struct tree_common)) / sizeof (tree);
1234 /* Return a newly constructed INTEGER_CST node whose constant value
1235 is specified by the two ints LOW and HI.
1236 The TREE_TYPE is set to `int'.
1238 This function should be used via the `build_int_2' macro. */
1240 tree
1241 build_int_2_wide (low, hi)
1242 HOST_WIDE_INT low, hi;
1244 register tree t = make_node (INTEGER_CST);
1245 TREE_INT_CST_LOW (t) = low;
1246 TREE_INT_CST_HIGH (t) = hi;
1247 TREE_TYPE (t) = integer_type_node;
1248 return t;
1251 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1253 tree
1254 build_real (type, d)
1255 tree type;
1256 REAL_VALUE_TYPE d;
1258 tree v;
1259 int overflow = 0;
1261 /* Check for valid float value for this type on this target machine;
1262 if not, can print error message and store a valid value in D. */
1263 #ifdef CHECK_FLOAT_VALUE
1264 CHECK_FLOAT_VALUE (TYPE_MODE (type), d, overflow);
1265 #endif
1267 v = make_node (REAL_CST);
1268 TREE_TYPE (v) = type;
1269 TREE_REAL_CST (v) = d;
1270 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
1271 return v;
1274 /* Return a new REAL_CST node whose type is TYPE
1275 and whose value is the integer value of the INTEGER_CST node I. */
1277 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1279 REAL_VALUE_TYPE
1280 real_value_from_int_cst (i)
1281 tree i;
1283 REAL_VALUE_TYPE d;
1284 REAL_VALUE_TYPE e;
1285 /* Some 386 compilers mishandle unsigned int to float conversions,
1286 so introduce a temporary variable E to avoid those bugs. */
1288 #ifdef REAL_ARITHMETIC
1289 if (! TREE_UNSIGNED (TREE_TYPE (i)))
1290 REAL_VALUE_FROM_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i));
1291 else
1292 REAL_VALUE_FROM_UNSIGNED_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i));
1293 #else /* not REAL_ARITHMETIC */
1294 if (TREE_INT_CST_HIGH (i) < 0 && ! TREE_UNSIGNED (TREE_TYPE (i)))
1296 d = (double) (~ TREE_INT_CST_HIGH (i));
1297 e = ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
1298 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
1299 d *= e;
1300 e = (double) (unsigned HOST_WIDE_INT) (~ TREE_INT_CST_LOW (i));
1301 d += e;
1302 d = (- d - 1.0);
1304 else
1306 d = (double) (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (i);
1307 e = ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
1308 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
1309 d *= e;
1310 e = (double) (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (i);
1311 d += e;
1313 #endif /* not REAL_ARITHMETIC */
1314 return d;
1317 /* This function can't be implemented if we can't do arithmetic
1318 on the float representation. */
1320 tree
1321 build_real_from_int_cst (type, i)
1322 tree type;
1323 tree i;
1325 tree v;
1326 int overflow = TREE_OVERFLOW (i);
1327 REAL_VALUE_TYPE d;
1328 jmp_buf float_error;
1330 v = make_node (REAL_CST);
1331 TREE_TYPE (v) = type;
1333 if (setjmp (float_error))
1335 d = dconst0;
1336 overflow = 1;
1337 goto got_it;
1340 set_float_handler (float_error);
1342 d = REAL_VALUE_TRUNCATE (TYPE_MODE (type), real_value_from_int_cst (i));
1344 /* Check for valid float value for this type on this target machine. */
1346 got_it:
1347 set_float_handler (NULL_PTR);
1349 #ifdef CHECK_FLOAT_VALUE
1350 CHECK_FLOAT_VALUE (TYPE_MODE (type), d, overflow);
1351 #endif
1353 TREE_REAL_CST (v) = d;
1354 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
1355 return v;
1358 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1360 /* Return a newly constructed STRING_CST node whose value is
1361 the LEN characters at STR.
1362 The TREE_TYPE is not initialized. */
1364 tree
1365 build_string (len, str)
1366 int len;
1367 char *str;
1369 /* Put the string in saveable_obstack since it will be placed in the RTL
1370 for an "asm" statement and will also be kept around a while if
1371 deferring constant output in varasm.c. */
1373 register tree s = make_node (STRING_CST);
1374 TREE_STRING_LENGTH (s) = len;
1375 TREE_STRING_POINTER (s) = obstack_copy0 (saveable_obstack, str, len);
1376 return s;
1379 /* Return a newly constructed COMPLEX_CST node whose value is
1380 specified by the real and imaginary parts REAL and IMAG.
1381 Both REAL and IMAG should be constant nodes.
1382 The TREE_TYPE is not initialized. */
1384 tree
1385 build_complex (real, imag)
1386 tree real, imag;
1388 register tree t = make_node (COMPLEX_CST);
1390 TREE_REALPART (t) = real;
1391 TREE_IMAGPART (t) = imag;
1392 TREE_TYPE (t) = build_complex_type (TREE_TYPE (real));
1393 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1394 TREE_CONSTANT_OVERFLOW (t)
1395 = TREE_CONSTANT_OVERFLOW (real) | TREE_CONSTANT_OVERFLOW (imag);
1396 return t;
1399 /* Build a newly constructed TREE_VEC node of length LEN. */
1400 tree
1401 make_tree_vec (len)
1402 int len;
1404 register tree t;
1405 register int length = (len-1) * sizeof (tree) + sizeof (struct tree_vec);
1406 register struct obstack *obstack = current_obstack;
1407 register int i;
1409 #ifdef GATHER_STATISTICS
1410 tree_node_counts[(int)vec_kind]++;
1411 tree_node_sizes[(int)vec_kind] += length;
1412 #endif
1414 t = (tree) obstack_alloc (obstack, length);
1416 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
1417 ((int *) t)[i] = 0;
1419 TREE_SET_CODE (t, TREE_VEC);
1420 TREE_VEC_LENGTH (t) = len;
1421 if (obstack == &permanent_obstack)
1422 TREE_PERMANENT (t) = 1;
1424 return t;
1427 /* Return 1 if EXPR is the integer constant zero or a complex constant
1428 of zero. */
1431 integer_zerop (expr)
1432 tree expr;
1434 STRIP_NOPS (expr);
1436 return ((TREE_CODE (expr) == INTEGER_CST
1437 && TREE_INT_CST_LOW (expr) == 0
1438 && TREE_INT_CST_HIGH (expr) == 0)
1439 || (TREE_CODE (expr) == COMPLEX_CST
1440 && integer_zerop (TREE_REALPART (expr))
1441 && integer_zerop (TREE_IMAGPART (expr))));
1444 /* Return 1 if EXPR is the integer constant one or the corresponding
1445 complex constant. */
1448 integer_onep (expr)
1449 tree expr;
1451 STRIP_NOPS (expr);
1453 return ((TREE_CODE (expr) == INTEGER_CST
1454 && TREE_INT_CST_LOW (expr) == 1
1455 && TREE_INT_CST_HIGH (expr) == 0)
1456 || (TREE_CODE (expr) == COMPLEX_CST
1457 && integer_onep (TREE_REALPART (expr))
1458 && integer_zerop (TREE_IMAGPART (expr))));
1461 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
1462 it contains. Likewise for the corresponding complex constant. */
1465 integer_all_onesp (expr)
1466 tree expr;
1468 register int prec;
1469 register int uns;
1471 STRIP_NOPS (expr);
1473 if (TREE_CODE (expr) == COMPLEX_CST
1474 && integer_all_onesp (TREE_REALPART (expr))
1475 && integer_zerop (TREE_IMAGPART (expr)))
1476 return 1;
1478 else if (TREE_CODE (expr) != INTEGER_CST)
1479 return 0;
1481 uns = TREE_UNSIGNED (TREE_TYPE (expr));
1482 if (!uns)
1483 return TREE_INT_CST_LOW (expr) == -1 && TREE_INT_CST_HIGH (expr) == -1;
1485 prec = TYPE_PRECISION (TREE_TYPE (expr));
1486 if (prec >= HOST_BITS_PER_WIDE_INT)
1488 int high_value, shift_amount;
1490 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1492 if (shift_amount > HOST_BITS_PER_WIDE_INT)
1493 /* Can not handle precisions greater than twice the host int size. */
1494 abort ();
1495 else if (shift_amount == HOST_BITS_PER_WIDE_INT)
1496 /* Shifting by the host word size is undefined according to the ANSI
1497 standard, so we must handle this as a special case. */
1498 high_value = -1;
1499 else
1500 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1502 return TREE_INT_CST_LOW (expr) == -1
1503 && TREE_INT_CST_HIGH (expr) == high_value;
1505 else
1506 return TREE_INT_CST_LOW (expr) == ((HOST_WIDE_INT) 1 << prec) - 1;
1509 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1510 one bit on). */
1513 integer_pow2p (expr)
1514 tree expr;
1516 HOST_WIDE_INT high, low;
1518 STRIP_NOPS (expr);
1520 if (TREE_CODE (expr) == COMPLEX_CST
1521 && integer_pow2p (TREE_REALPART (expr))
1522 && integer_zerop (TREE_IMAGPART (expr)))
1523 return 1;
1525 if (TREE_CODE (expr) != INTEGER_CST)
1526 return 0;
1528 high = TREE_INT_CST_HIGH (expr);
1529 low = TREE_INT_CST_LOW (expr);
1531 if (high == 0 && low == 0)
1532 return 0;
1534 return ((high == 0 && (low & (low - 1)) == 0)
1535 || (low == 0 && (high & (high - 1)) == 0));
1538 /* Return 1 if EXPR is the real constant zero. */
1541 real_zerop (expr)
1542 tree expr;
1544 STRIP_NOPS (expr);
1546 return ((TREE_CODE (expr) == REAL_CST
1547 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0))
1548 || (TREE_CODE (expr) == COMPLEX_CST
1549 && real_zerop (TREE_REALPART (expr))
1550 && real_zerop (TREE_IMAGPART (expr))));
1553 /* Return 1 if EXPR is the real constant one in real or complex form. */
1556 real_onep (expr)
1557 tree expr;
1559 STRIP_NOPS (expr);
1561 return ((TREE_CODE (expr) == REAL_CST
1562 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1))
1563 || (TREE_CODE (expr) == COMPLEX_CST
1564 && real_onep (TREE_REALPART (expr))
1565 && real_zerop (TREE_IMAGPART (expr))));
1568 /* Return 1 if EXPR is the real constant two. */
1571 real_twop (expr)
1572 tree expr;
1574 STRIP_NOPS (expr);
1576 return ((TREE_CODE (expr) == REAL_CST
1577 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2))
1578 || (TREE_CODE (expr) == COMPLEX_CST
1579 && real_twop (TREE_REALPART (expr))
1580 && real_zerop (TREE_IMAGPART (expr))));
1583 /* Nonzero if EXP is a constant or a cast of a constant. */
1586 really_constant_p (exp)
1587 tree exp;
1589 /* This is not quite the same as STRIP_NOPS. It does more. */
1590 while (TREE_CODE (exp) == NOP_EXPR
1591 || TREE_CODE (exp) == CONVERT_EXPR
1592 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1593 exp = TREE_OPERAND (exp, 0);
1594 return TREE_CONSTANT (exp);
1597 /* Return first list element whose TREE_VALUE is ELEM.
1598 Return 0 if ELEM is not it LIST. */
1600 tree
1601 value_member (elem, list)
1602 tree elem, list;
1604 while (list)
1606 if (elem == TREE_VALUE (list))
1607 return list;
1608 list = TREE_CHAIN (list);
1610 return NULL_TREE;
1613 /* Return first list element whose TREE_PURPOSE is ELEM.
1614 Return 0 if ELEM is not it LIST. */
1616 tree
1617 purpose_member (elem, list)
1618 tree elem, list;
1620 while (list)
1622 if (elem == TREE_PURPOSE (list))
1623 return list;
1624 list = TREE_CHAIN (list);
1626 return NULL_TREE;
1629 /* Return first list element whose BINFO_TYPE is ELEM.
1630 Return 0 if ELEM is not it LIST. */
1632 tree
1633 binfo_member (elem, list)
1634 tree elem, list;
1636 while (list)
1638 if (elem == BINFO_TYPE (list))
1639 return list;
1640 list = TREE_CHAIN (list);
1642 return NULL_TREE;
1645 /* Return nonzero if ELEM is part of the chain CHAIN. */
1648 chain_member (elem, chain)
1649 tree elem, chain;
1651 while (chain)
1653 if (elem == chain)
1654 return 1;
1655 chain = TREE_CHAIN (chain);
1658 return 0;
1661 /* Return the length of a chain of nodes chained through TREE_CHAIN.
1662 We expect a null pointer to mark the end of the chain.
1663 This is the Lisp primitive `length'. */
1666 list_length (t)
1667 tree t;
1669 register tree tail;
1670 register int len = 0;
1672 for (tail = t; tail; tail = TREE_CHAIN (tail))
1673 len++;
1675 return len;
1678 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
1679 by modifying the last node in chain 1 to point to chain 2.
1680 This is the Lisp primitive `nconc'. */
1682 tree
1683 chainon (op1, op2)
1684 tree op1, op2;
1687 if (op1)
1689 register tree t1;
1690 register tree t2;
1692 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
1694 TREE_CHAIN (t1) = op2;
1695 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
1696 if (t2 == t1)
1697 abort (); /* Circularity created. */
1698 return op1;
1700 else return op2;
1703 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
1705 tree
1706 tree_last (chain)
1707 register tree chain;
1709 register tree next;
1710 if (chain)
1711 while (next = TREE_CHAIN (chain))
1712 chain = next;
1713 return chain;
1716 /* Reverse the order of elements in the chain T,
1717 and return the new head of the chain (old last element). */
1719 tree
1720 nreverse (t)
1721 tree t;
1723 register tree prev = 0, decl, next;
1724 for (decl = t; decl; decl = next)
1726 next = TREE_CHAIN (decl);
1727 TREE_CHAIN (decl) = prev;
1728 prev = decl;
1730 return prev;
1733 /* Given a chain CHAIN of tree nodes,
1734 construct and return a list of those nodes. */
1736 tree
1737 listify (chain)
1738 tree chain;
1740 tree result = NULL_TREE;
1741 tree in_tail = chain;
1742 tree out_tail = NULL_TREE;
1744 while (in_tail)
1746 tree next = tree_cons (NULL_TREE, in_tail, NULL_TREE);
1747 if (out_tail)
1748 TREE_CHAIN (out_tail) = next;
1749 else
1750 result = next;
1751 out_tail = next;
1752 in_tail = TREE_CHAIN (in_tail);
1755 return result;
1758 /* Return a newly created TREE_LIST node whose
1759 purpose and value fields are PARM and VALUE. */
1761 tree
1762 build_tree_list (parm, value)
1763 tree parm, value;
1765 register tree t = make_node (TREE_LIST);
1766 TREE_PURPOSE (t) = parm;
1767 TREE_VALUE (t) = value;
1768 return t;
1771 /* Similar, but build on the temp_decl_obstack. */
1773 tree
1774 build_decl_list (parm, value)
1775 tree parm, value;
1777 register tree node;
1778 register struct obstack *ambient_obstack = current_obstack;
1779 current_obstack = &temp_decl_obstack;
1780 node = build_tree_list (parm, value);
1781 current_obstack = ambient_obstack;
1782 return node;
1785 /* Return a newly created TREE_LIST node whose
1786 purpose and value fields are PARM and VALUE
1787 and whose TREE_CHAIN is CHAIN. */
1789 tree
1790 tree_cons (purpose, value, chain)
1791 tree purpose, value, chain;
1793 #if 0
1794 register tree node = make_node (TREE_LIST);
1795 #else
1796 register int i;
1797 register tree node = (tree) obstack_alloc (current_obstack, sizeof (struct tree_list));
1798 #ifdef GATHER_STATISTICS
1799 tree_node_counts[(int)x_kind]++;
1800 tree_node_sizes[(int)x_kind] += sizeof (struct tree_list);
1801 #endif
1803 for (i = (sizeof (struct tree_common) / sizeof (int)) - 1; i >= 0; i--)
1804 ((int *) node)[i] = 0;
1806 TREE_SET_CODE (node, TREE_LIST);
1807 if (current_obstack == &permanent_obstack)
1808 TREE_PERMANENT (node) = 1;
1809 #endif
1811 TREE_CHAIN (node) = chain;
1812 TREE_PURPOSE (node) = purpose;
1813 TREE_VALUE (node) = value;
1814 return node;
1817 /* Similar, but build on the temp_decl_obstack. */
1819 tree
1820 decl_tree_cons (purpose, value, chain)
1821 tree purpose, value, chain;
1823 register tree node;
1824 register struct obstack *ambient_obstack = current_obstack;
1825 current_obstack = &temp_decl_obstack;
1826 node = tree_cons (purpose, value, chain);
1827 current_obstack = ambient_obstack;
1828 return node;
1831 /* Same as `tree_cons' but make a permanent object. */
1833 tree
1834 perm_tree_cons (purpose, value, chain)
1835 tree purpose, value, chain;
1837 register tree node;
1838 register struct obstack *ambient_obstack = current_obstack;
1839 current_obstack = &permanent_obstack;
1841 node = tree_cons (purpose, value, chain);
1842 current_obstack = ambient_obstack;
1843 return node;
1846 /* Same as `tree_cons', but make this node temporary, regardless. */
1848 tree
1849 temp_tree_cons (purpose, value, chain)
1850 tree purpose, value, chain;
1852 register tree node;
1853 register struct obstack *ambient_obstack = current_obstack;
1854 current_obstack = &temporary_obstack;
1856 node = tree_cons (purpose, value, chain);
1857 current_obstack = ambient_obstack;
1858 return node;
1861 /* Same as `tree_cons', but save this node if the function's RTL is saved. */
1863 tree
1864 saveable_tree_cons (purpose, value, chain)
1865 tree purpose, value, chain;
1867 register tree node;
1868 register struct obstack *ambient_obstack = current_obstack;
1869 current_obstack = saveable_obstack;
1871 node = tree_cons (purpose, value, chain);
1872 current_obstack = ambient_obstack;
1873 return node;
1876 /* Return the size nominally occupied by an object of type TYPE
1877 when it resides in memory. The value is measured in units of bytes,
1878 and its data type is that normally used for type sizes
1879 (which is the first type created by make_signed_type or
1880 make_unsigned_type). */
1882 tree
1883 size_in_bytes (type)
1884 tree type;
1886 tree t;
1888 if (type == error_mark_node)
1889 return integer_zero_node;
1890 type = TYPE_MAIN_VARIANT (type);
1891 if (TYPE_SIZE (type) == 0)
1893 incomplete_type_error (NULL_TREE, type);
1894 return integer_zero_node;
1896 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1897 size_int (BITS_PER_UNIT));
1898 if (TREE_CODE (t) == INTEGER_CST)
1899 force_fit_type (t, 0);
1900 return t;
1903 /* Return the size of TYPE (in bytes) as an integer,
1904 or return -1 if the size can vary. */
1907 int_size_in_bytes (type)
1908 tree type;
1910 unsigned int size;
1911 if (type == error_mark_node)
1912 return 0;
1913 type = TYPE_MAIN_VARIANT (type);
1914 if (TYPE_SIZE (type) == 0)
1915 return -1;
1916 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1917 return -1;
1918 if (TREE_INT_CST_HIGH (TYPE_SIZE (type)) != 0)
1920 tree t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1921 size_int (BITS_PER_UNIT));
1922 return TREE_INT_CST_LOW (t);
1924 size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1925 return (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1928 /* Return, as a tree node, the number of elements for TYPE (which is an
1929 ARRAY_TYPE) minus one. This counts only elements of the top array. */
1931 tree
1932 array_type_nelts (type)
1933 tree type;
1935 tree index_type = TYPE_DOMAIN (type);
1937 return (integer_zerop (TYPE_MIN_VALUE (index_type))
1938 ? TYPE_MAX_VALUE (index_type)
1939 : fold (build (MINUS_EXPR, TREE_TYPE (TYPE_MAX_VALUE (index_type)),
1940 TYPE_MAX_VALUE (index_type),
1941 TYPE_MIN_VALUE (index_type))));
1944 /* Return nonzero if arg is static -- a reference to an object in
1945 static storage. This is not the same as the C meaning of `static'. */
1948 staticp (arg)
1949 tree arg;
1951 switch (TREE_CODE (arg))
1953 case FUNCTION_DECL:
1954 /* Nested functions aren't static, since taking their address
1955 involves a trampoline. */
1956 return decl_function_context (arg) == 0;
1957 case VAR_DECL:
1958 return TREE_STATIC (arg) || DECL_EXTERNAL (arg);
1960 case CONSTRUCTOR:
1961 return TREE_STATIC (arg);
1963 case STRING_CST:
1964 return 1;
1966 case COMPONENT_REF:
1967 case BIT_FIELD_REF:
1968 return staticp (TREE_OPERAND (arg, 0));
1970 case INDIRECT_REF:
1971 return TREE_CONSTANT (TREE_OPERAND (arg, 0));
1973 case ARRAY_REF:
1974 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
1975 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
1976 return staticp (TREE_OPERAND (arg, 0));
1979 return 0;
1982 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
1983 Do this to any expression which may be used in more than one place,
1984 but must be evaluated only once.
1986 Normally, expand_expr would reevaluate the expression each time.
1987 Calling save_expr produces something that is evaluated and recorded
1988 the first time expand_expr is called on it. Subsequent calls to
1989 expand_expr just reuse the recorded value.
1991 The call to expand_expr that generates code that actually computes
1992 the value is the first call *at compile time*. Subsequent calls
1993 *at compile time* generate code to use the saved value.
1994 This produces correct result provided that *at run time* control
1995 always flows through the insns made by the first expand_expr
1996 before reaching the other places where the save_expr was evaluated.
1997 You, the caller of save_expr, must make sure this is so.
1999 Constants, and certain read-only nodes, are returned with no
2000 SAVE_EXPR because that is safe. Expressions containing placeholders
2001 are not touched; see tree.def for an explanation of what these
2002 are used for. */
2004 tree
2005 save_expr (expr)
2006 tree expr;
2008 register tree t = fold (expr);
2010 /* We don't care about whether this can be used as an lvalue in this
2011 context. */
2012 while (TREE_CODE (t) == NON_LVALUE_EXPR)
2013 t = TREE_OPERAND (t, 0);
2015 /* If the tree evaluates to a constant, then we don't want to hide that
2016 fact (i.e. this allows further folding, and direct checks for constants).
2017 However, a read-only object that has side effects cannot be bypassed.
2018 Since it is no problem to reevaluate literals, we just return the
2019 literal node. */
2021 if (TREE_CONSTANT (t) || (TREE_READONLY (t) && ! TREE_SIDE_EFFECTS (t))
2022 || TREE_CODE (t) == SAVE_EXPR)
2023 return t;
2025 /* If T contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
2026 it means that the size or offset of some field of an object depends on
2027 the value within another field.
2029 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
2030 and some variable since it would then need to be both evaluated once and
2031 evaluated more than once. Front-ends must assure this case cannot
2032 happen by surrounding any such subexpressions in their own SAVE_EXPR
2033 and forcing evaluation at the proper time. */
2034 if (contains_placeholder_p (t))
2035 return t;
2037 t = build (SAVE_EXPR, TREE_TYPE (expr), t, current_function_decl, NULL_TREE);
2039 /* This expression might be placed ahead of a jump to ensure that the
2040 value was computed on both sides of the jump. So make sure it isn't
2041 eliminated as dead. */
2042 TREE_SIDE_EFFECTS (t) = 1;
2043 return t;
2046 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
2047 or offset that depends on a field within a record.
2049 Note that we only allow such expressions within simple arithmetic
2050 or a COND_EXPR. */
2053 contains_placeholder_p (exp)
2054 tree exp;
2056 register enum tree_code code = TREE_CODE (exp);
2057 tree inner;
2059 /* If we have a WITH_RECORD_EXPR, it "cancels" any PLACEHOLDER_EXPR
2060 in it since it is supplying a value for it. */
2061 if (code == WITH_RECORD_EXPR)
2062 return 0;
2064 switch (TREE_CODE_CLASS (code))
2066 case 'r':
2067 for (inner = TREE_OPERAND (exp, 0);
2068 TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
2069 inner = TREE_OPERAND (inner, 0))
2071 return TREE_CODE (inner) == PLACEHOLDER_EXPR;
2073 case '1':
2074 case '2': case '<':
2075 case 'e':
2076 switch (tree_code_length[(int) code])
2078 case 1:
2079 return contains_placeholder_p (TREE_OPERAND (exp, 0));
2080 case 2:
2081 return (code != RTL_EXPR
2082 && code != CONSTRUCTOR
2083 && ! (code == SAVE_EXPR && SAVE_EXPR_RTL (exp) != 0)
2084 && code != WITH_RECORD_EXPR
2085 && (contains_placeholder_p (TREE_OPERAND (exp, 0))
2086 || contains_placeholder_p (TREE_OPERAND (exp, 1))));
2087 case 3:
2088 return (code == COND_EXPR
2089 && (contains_placeholder_p (TREE_OPERAND (exp, 0))
2090 || contains_placeholder_p (TREE_OPERAND (exp, 1))
2091 || contains_placeholder_p (TREE_OPERAND (exp, 2))));
2095 return 0;
2098 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
2099 return a tree with all occurrences of references to F in a
2100 PLACEHOLDER_EXPR replaced by R. Note that we assume here that EXP
2101 contains only arithmetic expressions. */
2103 tree
2104 substitute_in_expr (exp, f, r)
2105 tree exp;
2106 tree f;
2107 tree r;
2109 enum tree_code code = TREE_CODE (exp);
2110 tree inner;
2112 switch (TREE_CODE_CLASS (code))
2114 case 'c':
2115 case 'd':
2116 return exp;
2118 case 'x':
2119 if (code == PLACEHOLDER_EXPR)
2120 return exp;
2121 break;
2123 case '1':
2124 case '2':
2125 case '<':
2126 case 'e':
2127 switch (tree_code_length[(int) code])
2129 case 1:
2130 return fold (build1 (code, TREE_TYPE (exp),
2131 substitute_in_expr (TREE_OPERAND (exp, 0),
2132 f, r)));
2134 case 2:
2135 /* An RTL_EXPR cannot contain a PLACEHOLDER_EXPR; a CONSTRUCTOR
2136 could, but we don't support it. */
2137 if (code == RTL_EXPR)
2138 return exp;
2139 else if (code == CONSTRUCTOR)
2140 abort ();
2142 return fold (build (code, TREE_TYPE (exp),
2143 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2144 substitute_in_expr (TREE_OPERAND (exp, 1),
2145 f, r)));
2147 case 3:
2148 /* It cannot be that anything inside a SAVE_EXPR contains a
2149 PLACEHOLDER_EXPR. */
2150 if (code == SAVE_EXPR)
2151 return exp;
2153 if (code != COND_EXPR)
2154 abort ();
2156 return fold (build (code, TREE_TYPE (exp),
2157 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2158 substitute_in_expr (TREE_OPERAND (exp, 1), f, r),
2159 substitute_in_expr (TREE_OPERAND (exp, 2),
2160 f, r)));
2163 break;
2165 case 'r':
2166 switch (code)
2168 case COMPONENT_REF:
2169 /* If this expression is getting a value from a PLACEHOLDER_EXPR
2170 and it is the right field, replace it with R. */
2171 for (inner = TREE_OPERAND (exp, 0);
2172 TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
2173 inner = TREE_OPERAND (inner, 0))
2175 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2176 && TREE_OPERAND (exp, 1) == f)
2177 return r;
2179 return fold (build (code, TREE_TYPE (exp),
2180 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2181 TREE_OPERAND (exp, 1)));
2182 case BIT_FIELD_REF:
2183 return fold (build (code, TREE_TYPE (exp),
2184 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2185 substitute_in_expr (TREE_OPERAND (exp, 1), f, r),
2186 substitute_in_expr (TREE_OPERAND (exp, 2), f, r)));
2187 case INDIRECT_REF:
2188 case BUFFER_REF:
2189 return fold (build1 (code, TREE_TYPE (exp),
2190 substitute_in_expr (TREE_OPERAND (exp, 0),
2191 f, r)));
2192 case OFFSET_REF:
2193 return fold (build (code, TREE_TYPE (exp),
2194 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2195 substitute_in_expr (TREE_OPERAND (exp, 1), f, r)));
2199 /* If it wasn't one of the cases we handle, give up. */
2201 abort ();
2204 /* Given a type T, a FIELD_DECL F, and a replacement value R,
2205 return a new type with all size expressions that contain F
2206 updated by replacing F with R. */
2208 tree
2209 substitute_in_type (t, f, r)
2210 tree t, f, r;
2212 switch (TREE_CODE (t))
2214 case POINTER_TYPE:
2215 case VOID_TYPE:
2216 return t;
2217 case INTEGER_TYPE:
2218 case ENUMERAL_TYPE:
2219 case BOOLEAN_TYPE:
2220 case CHAR_TYPE:
2221 if ((TREE_CODE (TYPE_MIN_VALUE (t)) != INTEGER_CST
2222 && contains_placeholder_p (TYPE_MIN_VALUE (t)))
2223 || (TREE_CODE (TYPE_MAX_VALUE (t)) != INTEGER_CST
2224 && contains_placeholder_p (TYPE_MAX_VALUE (t))))
2225 return build_range_type (t,
2226 substitute_in_expr (TYPE_MIN_VALUE (t), f, r),
2227 substitute_in_expr (TYPE_MAX_VALUE (t), f, r));
2228 return t;
2230 case REAL_TYPE:
2231 if ((TYPE_MIN_VALUE (t) != 0
2232 && TREE_CODE (TYPE_MIN_VALUE (t)) != REAL_CST
2233 && contains_placeholder_p (TYPE_MIN_VALUE (t)))
2234 || (TYPE_MAX_VALUE (t) != 0
2235 && TREE_CODE (TYPE_MAX_VALUE (t)) != REAL_CST
2236 && contains_placeholder_p (TYPE_MAX_VALUE (t))))
2238 t = build_type_copy (t);
2240 if (TYPE_MIN_VALUE (t))
2241 TYPE_MIN_VALUE (t) = substitute_in_expr (TYPE_MIN_VALUE (t), f, r);
2242 if (TYPE_MAX_VALUE (t))
2243 TYPE_MAX_VALUE (t) = substitute_in_expr (TYPE_MAX_VALUE (t), f, r);
2245 return t;
2247 case COMPLEX_TYPE:
2248 return build_complex_type (substitute_in_type (TREE_TYPE (t), f, r));
2250 case OFFSET_TYPE:
2251 case METHOD_TYPE:
2252 case REFERENCE_TYPE:
2253 case FILE_TYPE:
2254 case SET_TYPE:
2255 case FUNCTION_TYPE:
2256 case LANG_TYPE:
2257 /* Don't know how to do these yet. */
2258 abort ();
2260 case ARRAY_TYPE:
2261 t = build_array_type (substitute_in_type (TREE_TYPE (t), f, r),
2262 substitute_in_type (TYPE_DOMAIN (t), f, r));
2263 TYPE_SIZE (t) = 0;
2264 layout_type (t);
2265 return t;
2267 case RECORD_TYPE:
2268 case UNION_TYPE:
2269 case QUAL_UNION_TYPE:
2271 tree new = copy_node (t);
2272 tree field;
2273 tree last_field = 0;
2275 /* Start out with no fields, make new fields, and chain them
2276 in. */
2278 TYPE_FIELDS (new) = 0;
2279 TYPE_SIZE (new) = 0;
2281 for (field = TYPE_FIELDS (t); field;
2282 field = TREE_CHAIN (field))
2284 tree new_field = copy_node (field);
2286 TREE_TYPE (new_field)
2287 = substitute_in_type (TREE_TYPE (new_field), f, r);
2289 /* If this is an anonymous field and the type of this field is
2290 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
2291 the type just has one element, treat that as the field.
2292 But don't do this if we are processing a QUAL_UNION_TYPE. */
2293 if (TREE_CODE (t) != QUAL_UNION_TYPE && DECL_NAME (new_field) == 0
2294 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
2295 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
2297 if (TYPE_FIELDS (TREE_TYPE (new_field)) == 0)
2298 continue;
2300 if (TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))) == 0)
2301 new_field = TYPE_FIELDS (TREE_TYPE (new_field));
2304 DECL_CONTEXT (new_field) = new;
2305 DECL_SIZE (new_field) = 0;
2307 if (TREE_CODE (t) == QUAL_UNION_TYPE)
2309 /* Do the substitution inside the qualifier and if we find
2310 that this field will not be present, omit it. */
2311 DECL_QUALIFIER (new_field)
2312 = substitute_in_expr (DECL_QUALIFIER (field), f, r);
2313 if (integer_zerop (DECL_QUALIFIER (new_field)))
2314 continue;
2317 if (last_field == 0)
2318 TYPE_FIELDS (new) = new_field;
2319 else
2320 TREE_CHAIN (last_field) = new_field;
2322 last_field = new_field;
2324 /* If this is a qualified type and this field will always be
2325 present, we are done. */
2326 if (TREE_CODE (t) == QUAL_UNION_TYPE
2327 && integer_onep (DECL_QUALIFIER (new_field)))
2328 break;
2331 /* If this used to be a qualified union type, but we now know what
2332 field will be present, make this a normal union. */
2333 if (TREE_CODE (new) == QUAL_UNION_TYPE
2334 && (TYPE_FIELDS (new) == 0
2335 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
2336 TREE_SET_CODE (new, UNION_TYPE);
2338 layout_type (new);
2339 return new;
2344 /* Stabilize a reference so that we can use it any number of times
2345 without causing its operands to be evaluated more than once.
2346 Returns the stabilized reference. This works by means of save_expr,
2347 so see the caveats in the comments about save_expr.
2349 Also allows conversion expressions whose operands are references.
2350 Any other kind of expression is returned unchanged. */
2352 tree
2353 stabilize_reference (ref)
2354 tree ref;
2356 register tree result;
2357 register enum tree_code code = TREE_CODE (ref);
2359 switch (code)
2361 case VAR_DECL:
2362 case PARM_DECL:
2363 case RESULT_DECL:
2364 /* No action is needed in this case. */
2365 return ref;
2367 case NOP_EXPR:
2368 case CONVERT_EXPR:
2369 case FLOAT_EXPR:
2370 case FIX_TRUNC_EXPR:
2371 case FIX_FLOOR_EXPR:
2372 case FIX_ROUND_EXPR:
2373 case FIX_CEIL_EXPR:
2374 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2375 break;
2377 case INDIRECT_REF:
2378 result = build_nt (INDIRECT_REF,
2379 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2380 break;
2382 case COMPONENT_REF:
2383 result = build_nt (COMPONENT_REF,
2384 stabilize_reference (TREE_OPERAND (ref, 0)),
2385 TREE_OPERAND (ref, 1));
2386 break;
2388 case BIT_FIELD_REF:
2389 result = build_nt (BIT_FIELD_REF,
2390 stabilize_reference (TREE_OPERAND (ref, 0)),
2391 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2392 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2393 break;
2395 case ARRAY_REF:
2396 result = build_nt (ARRAY_REF,
2397 stabilize_reference (TREE_OPERAND (ref, 0)),
2398 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2399 break;
2401 case COMPOUND_EXPR:
2402 result = build_nt (COMPOUND_EXPR,
2403 stabilize_reference_1 (TREE_OPERAND (ref, 0)),
2404 stabilize_reference (TREE_OPERAND (ref, 1)));
2405 break;
2407 case RTL_EXPR:
2408 result = build1 (INDIRECT_REF, TREE_TYPE (ref),
2409 save_expr (build1 (ADDR_EXPR,
2410 build_pointer_type (TREE_TYPE (ref)),
2411 ref)));
2412 break;
2415 /* If arg isn't a kind of lvalue we recognize, make no change.
2416 Caller should recognize the error for an invalid lvalue. */
2417 default:
2418 return ref;
2420 case ERROR_MARK:
2421 return error_mark_node;
2424 TREE_TYPE (result) = TREE_TYPE (ref);
2425 TREE_READONLY (result) = TREE_READONLY (ref);
2426 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
2427 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
2428 TREE_RAISES (result) = TREE_RAISES (ref);
2430 return result;
2433 /* Subroutine of stabilize_reference; this is called for subtrees of
2434 references. Any expression with side-effects must be put in a SAVE_EXPR
2435 to ensure that it is only evaluated once.
2437 We don't put SAVE_EXPR nodes around everything, because assigning very
2438 simple expressions to temporaries causes us to miss good opportunities
2439 for optimizations. Among other things, the opportunity to fold in the
2440 addition of a constant into an addressing mode often gets lost, e.g.
2441 "y[i+1] += x;". In general, we take the approach that we should not make
2442 an assignment unless we are forced into it - i.e., that any non-side effect
2443 operator should be allowed, and that cse should take care of coalescing
2444 multiple utterances of the same expression should that prove fruitful. */
2446 static tree
2447 stabilize_reference_1 (e)
2448 tree e;
2450 register tree result;
2451 register enum tree_code code = TREE_CODE (e);
2453 /* We cannot ignore const expressions because it might be a reference
2454 to a const array but whose index contains side-effects. But we can
2455 ignore things that are actual constant or that already have been
2456 handled by this function. */
2458 if (TREE_CONSTANT (e) || code == SAVE_EXPR)
2459 return e;
2461 switch (TREE_CODE_CLASS (code))
2463 case 'x':
2464 case 't':
2465 case 'd':
2466 case 'b':
2467 case '<':
2468 case 's':
2469 case 'e':
2470 case 'r':
2471 /* If the expression has side-effects, then encase it in a SAVE_EXPR
2472 so that it will only be evaluated once. */
2473 /* The reference (r) and comparison (<) classes could be handled as
2474 below, but it is generally faster to only evaluate them once. */
2475 if (TREE_SIDE_EFFECTS (e))
2476 return save_expr (e);
2477 return e;
2479 case 'c':
2480 /* Constants need no processing. In fact, we should never reach
2481 here. */
2482 return e;
2484 case '2':
2485 /* Division is slow and tends to be compiled with jumps,
2486 especially the division by powers of 2 that is often
2487 found inside of an array reference. So do it just once. */
2488 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
2489 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
2490 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
2491 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
2492 return save_expr (e);
2493 /* Recursively stabilize each operand. */
2494 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
2495 stabilize_reference_1 (TREE_OPERAND (e, 1)));
2496 break;
2498 case '1':
2499 /* Recursively stabilize each operand. */
2500 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
2501 break;
2503 default:
2504 abort ();
2507 TREE_TYPE (result) = TREE_TYPE (e);
2508 TREE_READONLY (result) = TREE_READONLY (e);
2509 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
2510 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
2511 TREE_RAISES (result) = TREE_RAISES (e);
2513 return result;
2516 /* Low-level constructors for expressions. */
2518 /* Build an expression of code CODE, data type TYPE,
2519 and operands as specified by the arguments ARG1 and following arguments.
2520 Expressions and reference nodes can be created this way.
2521 Constants, decls, types and misc nodes cannot be. */
2523 tree
2524 build VPROTO((enum tree_code code, tree tt, ...))
2526 #ifndef __STDC__
2527 enum tree_code code;
2528 tree tt;
2529 #endif
2530 va_list p;
2531 register tree t;
2532 register int length;
2533 register int i;
2535 VA_START (p, tt);
2537 #ifndef __STDC__
2538 code = va_arg (p, enum tree_code);
2539 tt = va_arg (p, tree);
2540 #endif
2542 t = make_node (code);
2543 length = tree_code_length[(int) code];
2544 TREE_TYPE (t) = tt;
2546 if (length == 2)
2548 /* This is equivalent to the loop below, but faster. */
2549 register tree arg0 = va_arg (p, tree);
2550 register tree arg1 = va_arg (p, tree);
2551 TREE_OPERAND (t, 0) = arg0;
2552 TREE_OPERAND (t, 1) = arg1;
2553 if ((arg0 && TREE_SIDE_EFFECTS (arg0))
2554 || (arg1 && TREE_SIDE_EFFECTS (arg1)))
2555 TREE_SIDE_EFFECTS (t) = 1;
2556 TREE_RAISES (t)
2557 = (arg0 && TREE_RAISES (arg0)) || (arg1 && TREE_RAISES (arg1));
2559 else if (length == 1)
2561 register tree arg0 = va_arg (p, tree);
2563 /* Call build1 for this! */
2564 if (TREE_CODE_CLASS (code) != 's')
2565 abort ();
2566 TREE_OPERAND (t, 0) = arg0;
2567 if (arg0 && TREE_SIDE_EFFECTS (arg0))
2568 TREE_SIDE_EFFECTS (t) = 1;
2569 TREE_RAISES (t) = (arg0 && TREE_RAISES (arg0));
2571 else
2573 for (i = 0; i < length; i++)
2575 register tree operand = va_arg (p, tree);
2576 TREE_OPERAND (t, i) = operand;
2577 if (operand)
2579 if (TREE_SIDE_EFFECTS (operand))
2580 TREE_SIDE_EFFECTS (t) = 1;
2581 if (TREE_RAISES (operand))
2582 TREE_RAISES (t) = 1;
2586 va_end (p);
2587 return t;
2590 /* Same as above, but only builds for unary operators.
2591 Saves lions share of calls to `build'; cuts down use
2592 of varargs, which is expensive for RISC machines. */
2593 tree
2594 build1 (code, type, node)
2595 enum tree_code code;
2596 tree type;
2597 tree node;
2599 register struct obstack *obstack = current_obstack;
2600 register int i, length;
2601 register tree_node_kind kind;
2602 register tree t;
2604 #ifdef GATHER_STATISTICS
2605 if (TREE_CODE_CLASS (code) == 'r')
2606 kind = r_kind;
2607 else
2608 kind = e_kind;
2609 #endif
2611 obstack = expression_obstack;
2612 length = sizeof (struct tree_exp);
2614 t = (tree) obstack_alloc (obstack, length);
2616 #ifdef GATHER_STATISTICS
2617 tree_node_counts[(int)kind]++;
2618 tree_node_sizes[(int)kind] += length;
2619 #endif
2621 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
2622 ((int *) t)[i] = 0;
2624 TREE_TYPE (t) = type;
2625 TREE_SET_CODE (t, code);
2627 if (obstack == &permanent_obstack)
2628 TREE_PERMANENT (t) = 1;
2630 TREE_OPERAND (t, 0) = node;
2631 if (node)
2633 if (TREE_SIDE_EFFECTS (node))
2634 TREE_SIDE_EFFECTS (t) = 1;
2635 if (TREE_RAISES (node))
2636 TREE_RAISES (t) = 1;
2639 return t;
2642 /* Similar except don't specify the TREE_TYPE
2643 and leave the TREE_SIDE_EFFECTS as 0.
2644 It is permissible for arguments to be null,
2645 or even garbage if their values do not matter. */
2647 tree
2648 build_nt VPROTO((enum tree_code code, ...))
2650 #ifndef __STDC__
2651 enum tree_code code;
2652 #endif
2653 va_list p;
2654 register tree t;
2655 register int length;
2656 register int i;
2658 VA_START (p, code);
2660 #ifndef __STDC__
2661 code = va_arg (p, enum tree_code);
2662 #endif
2664 t = make_node (code);
2665 length = tree_code_length[(int) code];
2667 for (i = 0; i < length; i++)
2668 TREE_OPERAND (t, i) = va_arg (p, tree);
2670 va_end (p);
2671 return t;
2674 /* Similar to `build_nt', except we build
2675 on the temp_decl_obstack, regardless. */
2677 tree
2678 build_parse_node VPROTO((enum tree_code code, ...))
2680 #ifndef __STDC__
2681 enum tree_code code;
2682 #endif
2683 register struct obstack *ambient_obstack = expression_obstack;
2684 va_list p;
2685 register tree t;
2686 register int length;
2687 register int i;
2689 VA_START (p, code);
2691 #ifndef __STDC__
2692 code = va_arg (p, enum tree_code);
2693 #endif
2695 expression_obstack = &temp_decl_obstack;
2697 t = make_node (code);
2698 length = tree_code_length[(int) code];
2700 for (i = 0; i < length; i++)
2701 TREE_OPERAND (t, i) = va_arg (p, tree);
2703 va_end (p);
2704 expression_obstack = ambient_obstack;
2705 return t;
2708 #if 0
2709 /* Commented out because this wants to be done very
2710 differently. See cp-lex.c. */
2711 tree
2712 build_op_identifier (op1, op2)
2713 tree op1, op2;
2715 register tree t = make_node (OP_IDENTIFIER);
2716 TREE_PURPOSE (t) = op1;
2717 TREE_VALUE (t) = op2;
2718 return t;
2720 #endif
2722 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
2723 We do NOT enter this node in any sort of symbol table.
2725 layout_decl is used to set up the decl's storage layout.
2726 Other slots are initialized to 0 or null pointers. */
2728 tree
2729 build_decl (code, name, type)
2730 enum tree_code code;
2731 tree name, type;
2733 register tree t;
2735 t = make_node (code);
2737 /* if (type == error_mark_node)
2738 type = integer_type_node; */
2739 /* That is not done, deliberately, so that having error_mark_node
2740 as the type can suppress useless errors in the use of this variable. */
2742 DECL_NAME (t) = name;
2743 DECL_ASSEMBLER_NAME (t) = name;
2744 TREE_TYPE (t) = type;
2746 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
2747 layout_decl (t, 0);
2748 else if (code == FUNCTION_DECL)
2749 DECL_MODE (t) = FUNCTION_MODE;
2751 return t;
2754 /* BLOCK nodes are used to represent the structure of binding contours
2755 and declarations, once those contours have been exited and their contents
2756 compiled. This information is used for outputting debugging info. */
2758 tree
2759 build_block (vars, tags, subblocks, supercontext, chain)
2760 tree vars, tags, subblocks, supercontext, chain;
2762 register tree block = make_node (BLOCK);
2763 BLOCK_VARS (block) = vars;
2764 BLOCK_TYPE_TAGS (block) = tags;
2765 BLOCK_SUBBLOCKS (block) = subblocks;
2766 BLOCK_SUPERCONTEXT (block) = supercontext;
2767 BLOCK_CHAIN (block) = chain;
2768 return block;
2771 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
2772 is ATTRIBUTE.
2774 Such modified types already made are recorded so that duplicates
2775 are not made. */
2777 tree
2778 build_type_attribute_variant (ttype, attribute)
2779 tree ttype, attribute;
2781 if ( ! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
2783 register int hashcode;
2784 register struct obstack *ambient_obstack = current_obstack;
2785 tree ntype;
2787 if (ambient_obstack != &permanent_obstack)
2788 current_obstack = TYPE_OBSTACK (ttype);
2790 ntype = copy_node (ttype);
2791 current_obstack = ambient_obstack;
2793 TYPE_POINTER_TO (ntype) = 0;
2794 TYPE_REFERENCE_TO (ntype) = 0;
2795 TYPE_ATTRIBUTES (ntype) = attribute;
2797 /* Create a new main variant of TYPE. */
2798 TYPE_MAIN_VARIANT (ntype) = ntype;
2799 TYPE_NEXT_VARIANT (ntype) = 0;
2800 TYPE_READONLY (ntype) = TYPE_VOLATILE (ntype) = 0;
2802 hashcode = TYPE_HASH (TREE_CODE (ntype))
2803 + TYPE_HASH (TREE_TYPE (ntype))
2804 + type_hash_list (attribute);
2806 switch (TREE_CODE (ntype))
2808 case FUNCTION_TYPE:
2809 hashcode += TYPE_HASH (TYPE_ARG_TYPES (ntype));
2810 break;
2811 case ARRAY_TYPE:
2812 hashcode += TYPE_HASH (TYPE_DOMAIN (ntype));
2813 break;
2814 case INTEGER_TYPE:
2815 hashcode += TYPE_HASH (TYPE_MAX_VALUE (ntype));
2816 break;
2817 case REAL_TYPE:
2818 hashcode += TYPE_HASH (TYPE_PRECISION (ntype));
2819 break;
2822 ntype = type_hash_canon (hashcode, ntype);
2823 ttype = build_type_variant (ntype, TYPE_READONLY (ttype),
2824 TYPE_VOLATILE (ttype));
2827 return ttype;
2830 /* Return a type like TYPE except that its TYPE_READONLY is CONSTP
2831 and its TYPE_VOLATILE is VOLATILEP.
2833 Such variant types already made are recorded so that duplicates
2834 are not made.
2836 A variant types should never be used as the type of an expression.
2837 Always copy the variant information into the TREE_READONLY
2838 and TREE_THIS_VOLATILE of the expression, and then give the expression
2839 as its type the "main variant", the variant whose TYPE_READONLY
2840 and TYPE_VOLATILE are zero. Use TYPE_MAIN_VARIANT to find the
2841 main variant. */
2843 tree
2844 build_type_variant (type, constp, volatilep)
2845 tree type;
2846 int constp, volatilep;
2848 register tree t;
2850 /* Treat any nonzero argument as 1. */
2851 constp = !!constp;
2852 volatilep = !!volatilep;
2854 /* Search the chain of variants to see if there is already one there just
2855 like the one we need to have. If so, use that existing one. We must
2856 preserve the TYPE_NAME, since there is code that depends on this. */
2858 for (t = TYPE_MAIN_VARIANT(type); t; t = TYPE_NEXT_VARIANT (t))
2859 if (constp == TYPE_READONLY (t) && volatilep == TYPE_VOLATILE (t)
2860 && TYPE_NAME (t) == TYPE_NAME (type))
2861 return t;
2863 /* We need a new one. */
2865 t = build_type_copy (type);
2866 TYPE_READONLY (t) = constp;
2867 TYPE_VOLATILE (t) = volatilep;
2869 return t;
2872 /* Give TYPE a new main variant: NEW_MAIN.
2873 This is the right thing to do only when something else
2874 about TYPE is modified in place. */
2876 void
2877 change_main_variant (type, new_main)
2878 tree type, new_main;
2880 tree t;
2881 tree omain = TYPE_MAIN_VARIANT (type);
2883 /* Remove TYPE from the TYPE_NEXT_VARIANT chain of its main variant. */
2884 if (TYPE_NEXT_VARIANT (omain) == type)
2885 TYPE_NEXT_VARIANT (omain) = TYPE_NEXT_VARIANT (type);
2886 else
2887 for (t = TYPE_NEXT_VARIANT (omain); t && TYPE_NEXT_VARIANT (t);
2888 t = TYPE_NEXT_VARIANT (t))
2889 if (TYPE_NEXT_VARIANT (t) == type)
2891 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (type);
2892 break;
2895 TYPE_MAIN_VARIANT (type) = new_main;
2896 TYPE_NEXT_VARIANT (type) = TYPE_NEXT_VARIANT (new_main);
2897 TYPE_NEXT_VARIANT (new_main) = type;
2900 /* Create a new variant of TYPE, equivalent but distinct.
2901 This is so the caller can modify it. */
2903 tree
2904 build_type_copy (type)
2905 tree type;
2907 register tree t, m = TYPE_MAIN_VARIANT (type);
2908 register struct obstack *ambient_obstack = current_obstack;
2910 current_obstack = TYPE_OBSTACK (type);
2911 t = copy_node (type);
2912 current_obstack = ambient_obstack;
2914 TYPE_POINTER_TO (t) = 0;
2915 TYPE_REFERENCE_TO (t) = 0;
2917 /* Add this type to the chain of variants of TYPE. */
2918 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
2919 TYPE_NEXT_VARIANT (m) = t;
2921 return t;
2924 /* Hashing of types so that we don't make duplicates.
2925 The entry point is `type_hash_canon'. */
2927 /* Each hash table slot is a bucket containing a chain
2928 of these structures. */
2930 struct type_hash
2932 struct type_hash *next; /* Next structure in the bucket. */
2933 int hashcode; /* Hash code of this type. */
2934 tree type; /* The type recorded here. */
2937 /* Now here is the hash table. When recording a type, it is added
2938 to the slot whose index is the hash code mod the table size.
2939 Note that the hash table is used for several kinds of types
2940 (function types, array types and array index range types, for now).
2941 While all these live in the same table, they are completely independent,
2942 and the hash code is computed differently for each of these. */
2944 #define TYPE_HASH_SIZE 59
2945 struct type_hash *type_hash_table[TYPE_HASH_SIZE];
2947 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
2948 with types in the TREE_VALUE slots), by adding the hash codes
2949 of the individual types. */
2952 type_hash_list (list)
2953 tree list;
2955 register int hashcode;
2956 register tree tail;
2957 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
2958 hashcode += TYPE_HASH (TREE_VALUE (tail));
2959 return hashcode;
2962 /* Look in the type hash table for a type isomorphic to TYPE.
2963 If one is found, return it. Otherwise return 0. */
2965 tree
2966 type_hash_lookup (hashcode, type)
2967 int hashcode;
2968 tree type;
2970 register struct type_hash *h;
2971 for (h = type_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
2972 if (h->hashcode == hashcode
2973 && TREE_CODE (h->type) == TREE_CODE (type)
2974 && TREE_TYPE (h->type) == TREE_TYPE (type)
2975 && attribute_list_equal (TYPE_ATTRIBUTES (h->type),
2976 TYPE_ATTRIBUTES (type))
2977 && (TYPE_MAX_VALUE (h->type) == TYPE_MAX_VALUE (type)
2978 || tree_int_cst_equal (TYPE_MAX_VALUE (h->type),
2979 TYPE_MAX_VALUE (type)))
2980 && (TYPE_MIN_VALUE (h->type) == TYPE_MIN_VALUE (type)
2981 || tree_int_cst_equal (TYPE_MIN_VALUE (h->type),
2982 TYPE_MIN_VALUE (type)))
2983 && (TYPE_DOMAIN (h->type) == TYPE_DOMAIN (type)
2984 || (TYPE_DOMAIN (h->type)
2985 && TREE_CODE (TYPE_DOMAIN (h->type)) == TREE_LIST
2986 && TYPE_DOMAIN (type)
2987 && TREE_CODE (TYPE_DOMAIN (type)) == TREE_LIST
2988 && type_list_equal (TYPE_DOMAIN (h->type), TYPE_DOMAIN (type)))))
2989 return h->type;
2990 return 0;
2993 /* Add an entry to the type-hash-table
2994 for a type TYPE whose hash code is HASHCODE. */
2996 void
2997 type_hash_add (hashcode, type)
2998 int hashcode;
2999 tree type;
3001 register struct type_hash *h;
3003 h = (struct type_hash *) oballoc (sizeof (struct type_hash));
3004 h->hashcode = hashcode;
3005 h->type = type;
3006 h->next = type_hash_table[hashcode % TYPE_HASH_SIZE];
3007 type_hash_table[hashcode % TYPE_HASH_SIZE] = h;
3010 /* Given TYPE, and HASHCODE its hash code, return the canonical
3011 object for an identical type if one already exists.
3012 Otherwise, return TYPE, and record it as the canonical object
3013 if it is a permanent object.
3015 To use this function, first create a type of the sort you want.
3016 Then compute its hash code from the fields of the type that
3017 make it different from other similar types.
3018 Then call this function and use the value.
3019 This function frees the type you pass in if it is a duplicate. */
3021 /* Set to 1 to debug without canonicalization. Never set by program. */
3022 int debug_no_type_hash = 0;
3024 tree
3025 type_hash_canon (hashcode, type)
3026 int hashcode;
3027 tree type;
3029 tree t1;
3031 if (debug_no_type_hash)
3032 return type;
3034 t1 = type_hash_lookup (hashcode, type);
3035 if (t1 != 0)
3037 obstack_free (TYPE_OBSTACK (type), type);
3038 #ifdef GATHER_STATISTICS
3039 tree_node_counts[(int)t_kind]--;
3040 tree_node_sizes[(int)t_kind] -= sizeof (struct tree_type);
3041 #endif
3042 return t1;
3045 /* If this is a permanent type, record it for later reuse. */
3046 if (TREE_PERMANENT (type))
3047 type_hash_add (hashcode, type);
3049 return type;
3052 /* Given two lists of attributes, return true if list l2 is
3053 equivalent to l1. */
3056 attribute_list_equal (l1, l2)
3057 tree l1, l2;
3059 return attribute_list_contained (l1, l2)
3060 && attribute_list_contained (l2, l1);
3063 /* Given two lists of attributes, return true if list l2 is
3064 completely contained within l1. */
3067 attribute_list_contained (l1, l2)
3068 tree l1, l2;
3070 register tree t1, t2;
3072 /* First check the obvious, maybe the lists are identical. */
3073 if (l1 == l2)
3074 return 1;
3076 /* Then check the obvious, maybe the lists are similar. */
3077 for (t1 = l1, t2 = l2;
3078 t1 && t2
3079 && TREE_VALUE (t1) == TREE_VALUE (t2);
3080 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
3082 /* Maybe the lists are equal. */
3083 if (t1 == 0 && t2 == 0)
3084 return 1;
3086 for (; t2; t2 = TREE_CHAIN (t2))
3087 if (!value_member (l1, t2))
3088 return 0;
3089 return 1;
3092 /* Given two lists of types
3093 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
3094 return 1 if the lists contain the same types in the same order.
3095 Also, the TREE_PURPOSEs must match. */
3098 type_list_equal (l1, l2)
3099 tree l1, l2;
3101 register tree t1, t2;
3102 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
3104 if (TREE_VALUE (t1) != TREE_VALUE (t2))
3105 return 0;
3106 if (TREE_PURPOSE (t1) != TREE_PURPOSE (t2))
3108 int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
3109 if (cmp < 0)
3110 abort ();
3111 if (cmp == 0
3112 || TREE_TYPE (TREE_PURPOSE (t1))
3113 != TREE_TYPE (TREE_PURPOSE (t2)))
3114 return 0;
3118 return t1 == t2;
3121 /* Nonzero if integer constants T1 and T2
3122 represent the same constant value. */
3125 tree_int_cst_equal (t1, t2)
3126 tree t1, t2;
3128 if (t1 == t2)
3129 return 1;
3130 if (t1 == 0 || t2 == 0)
3131 return 0;
3132 if (TREE_CODE (t1) == INTEGER_CST
3133 && TREE_CODE (t2) == INTEGER_CST
3134 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3135 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
3136 return 1;
3137 return 0;
3140 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
3141 The precise way of comparison depends on their data type. */
3144 tree_int_cst_lt (t1, t2)
3145 tree t1, t2;
3147 if (t1 == t2)
3148 return 0;
3150 if (!TREE_UNSIGNED (TREE_TYPE (t1)))
3151 return INT_CST_LT (t1, t2);
3152 return INT_CST_LT_UNSIGNED (t1, t2);
3155 /* Return an indication of the sign of the integer constant T.
3156 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
3157 Note that -1 will never be returned it T's type is unsigned. */
3160 tree_int_cst_sgn (t)
3161 tree t;
3163 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
3164 return 0;
3165 else if (TREE_UNSIGNED (TREE_TYPE (t)))
3166 return 1;
3167 else if (TREE_INT_CST_HIGH (t) < 0)
3168 return -1;
3169 else
3170 return 1;
3173 /* Compare two constructor-element-type constants. */
3175 simple_cst_list_equal (l1, l2)
3176 tree l1, l2;
3178 while (l1 != NULL_TREE && l2 != NULL_TREE)
3180 int cmp = simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2));
3181 if (cmp < 0)
3182 abort ();
3183 if (cmp == 0)
3184 return 0;
3185 l1 = TREE_CHAIN (l1);
3186 l2 = TREE_CHAIN (l2);
3188 return (l1 == l2);
3191 /* Return truthvalue of whether T1 is the same tree structure as T2.
3192 Return 1 if they are the same.
3193 Return 0 if they are understandably different.
3194 Return -1 if either contains tree structure not understood by
3195 this function. */
3198 simple_cst_equal (t1, t2)
3199 tree t1, t2;
3201 register enum tree_code code1, code2;
3202 int cmp;
3204 if (t1 == t2)
3205 return 1;
3206 if (t1 == 0 || t2 == 0)
3207 return 0;
3209 code1 = TREE_CODE (t1);
3210 code2 = TREE_CODE (t2);
3212 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
3213 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR || code2 == NON_LVALUE_EXPR)
3214 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3215 else
3216 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
3217 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3218 || code2 == NON_LVALUE_EXPR)
3219 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
3221 if (code1 != code2)
3222 return 0;
3224 switch (code1)
3226 case INTEGER_CST:
3227 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3228 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
3230 case REAL_CST:
3231 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3233 case STRING_CST:
3234 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3235 && !bcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3236 TREE_STRING_LENGTH (t1));
3238 case CONSTRUCTOR:
3239 abort ();
3241 case SAVE_EXPR:
3242 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3244 case CALL_EXPR:
3245 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3246 if (cmp <= 0)
3247 return cmp;
3248 return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3250 case TARGET_EXPR:
3251 /* Special case: if either target is an unallocated VAR_DECL,
3252 it means that it's going to be unified with whatever the
3253 TARGET_EXPR is really supposed to initialize, so treat it
3254 as being equivalent to anything. */
3255 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
3256 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
3257 && DECL_RTL (TREE_OPERAND (t1, 0)) == 0)
3258 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
3259 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
3260 && DECL_RTL (TREE_OPERAND (t2, 0)) == 0))
3261 cmp = 1;
3262 else
3263 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3264 if (cmp <= 0)
3265 return cmp;
3266 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3268 case WITH_CLEANUP_EXPR:
3269 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3270 if (cmp <= 0)
3271 return cmp;
3272 return simple_cst_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t1, 2));
3274 case COMPONENT_REF:
3275 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
3276 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3277 return 0;
3279 case VAR_DECL:
3280 case PARM_DECL:
3281 case CONST_DECL:
3282 case FUNCTION_DECL:
3283 return 0;
3286 /* This general rule works for most tree codes.
3287 All exceptions should be handled above. */
3289 switch (TREE_CODE_CLASS (code1))
3291 int i;
3292 case '1':
3293 case '2':
3294 case '<':
3295 case 'e':
3296 case 'r':
3297 case 's':
3298 cmp = 1;
3299 for (i=0; i<tree_code_length[(int) code1]; ++i)
3301 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
3302 if (cmp <= 0)
3303 return cmp;
3305 return cmp;
3308 return -1;
3311 /* Constructors for pointer, array and function types.
3312 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
3313 constructed by language-dependent code, not here.) */
3315 /* Construct, lay out and return the type of pointers to TO_TYPE.
3316 If such a type has already been constructed, reuse it. */
3318 tree
3319 build_pointer_type (to_type)
3320 tree to_type;
3322 register tree t = TYPE_POINTER_TO (to_type);
3324 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3326 if (t)
3327 return t;
3329 /* We need a new one. Put this in the same obstack as TO_TYPE. */
3330 push_obstacks (TYPE_OBSTACK (to_type), TYPE_OBSTACK (to_type));
3331 t = make_node (POINTER_TYPE);
3332 pop_obstacks ();
3334 TREE_TYPE (t) = to_type;
3336 /* Record this type as the pointer to TO_TYPE. */
3337 TYPE_POINTER_TO (to_type) = t;
3339 /* Lay out the type. This function has many callers that are concerned
3340 with expression-construction, and this simplifies them all.
3341 Also, it guarantees the TYPE_SIZE is in the same obstack as the type. */
3342 layout_type (t);
3344 return t;
3347 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
3348 MAXVAL should be the maximum value in the domain
3349 (one less than the length of the array). */
3351 tree
3352 build_index_type (maxval)
3353 tree maxval;
3355 register tree itype = make_node (INTEGER_TYPE);
3356 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
3357 TYPE_MIN_VALUE (itype) = build_int_2 (0, 0);
3358 TREE_TYPE (TYPE_MIN_VALUE (itype)) = sizetype;
3359 TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
3360 TYPE_MODE (itype) = TYPE_MODE (sizetype);
3361 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
3362 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
3363 if (TREE_CODE (maxval) == INTEGER_CST)
3365 int maxint = (int) TREE_INT_CST_LOW (maxval);
3366 /* If the domain should be empty, make sure the maxval
3367 remains -1 and is not spoiled by truncation. */
3368 if (INT_CST_LT (maxval, integer_zero_node))
3370 TYPE_MAX_VALUE (itype) = build_int_2 (-1, -1);
3371 TREE_TYPE (TYPE_MAX_VALUE (itype)) = sizetype;
3373 return type_hash_canon (maxint < 0 ? ~maxint : maxint, itype);
3375 else
3376 return itype;
3379 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
3380 ENUMERAL_TYPE, BOOLEAN_TYPE, or CHAR_TYPE), with
3381 low bound LOWVAL and high bound HIGHVAL.
3382 if TYPE==NULL_TREE, sizetype is used. */
3384 tree
3385 build_range_type (type, lowval, highval)
3386 tree type, lowval, highval;
3388 register tree itype = make_node (INTEGER_TYPE);
3389 TREE_TYPE (itype) = type;
3390 if (type == NULL_TREE)
3391 type = sizetype;
3392 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
3393 TYPE_MIN_VALUE (itype) = convert (type, lowval);
3394 TYPE_MAX_VALUE (itype) = convert (type, highval);
3395 TYPE_MODE (itype) = TYPE_MODE (type);
3396 TYPE_SIZE (itype) = TYPE_SIZE (type);
3397 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
3398 if ((TREE_CODE (lowval) == INTEGER_CST)
3399 && (TREE_CODE (highval) == INTEGER_CST))
3401 HOST_WIDE_INT highint = TREE_INT_CST_LOW (highval);
3402 HOST_WIDE_INT lowint = TREE_INT_CST_LOW (lowval);
3403 int maxint = (int) (highint - lowint);
3404 return type_hash_canon (maxint < 0 ? ~maxint : maxint, itype);
3406 else
3407 return itype;
3410 /* Just like build_index_type, but takes lowval and highval instead
3411 of just highval (maxval). */
3413 tree
3414 build_index_2_type (lowval,highval)
3415 tree lowval, highval;
3417 return build_range_type (NULL_TREE, lowval, highval);
3420 /* Return nonzero iff ITYPE1 and ITYPE2 are equal (in the LISP sense).
3421 Needed because when index types are not hashed, equal index types
3422 built at different times appear distinct, even though structurally,
3423 they are not. */
3426 index_type_equal (itype1, itype2)
3427 tree itype1, itype2;
3429 if (TREE_CODE (itype1) != TREE_CODE (itype2))
3430 return 0;
3431 if (TREE_CODE (itype1) == INTEGER_TYPE)
3433 if (TYPE_PRECISION (itype1) != TYPE_PRECISION (itype2)
3434 || TYPE_MODE (itype1) != TYPE_MODE (itype2)
3435 || ! simple_cst_equal (TYPE_SIZE (itype1), TYPE_SIZE (itype2))
3436 || TYPE_ALIGN (itype1) != TYPE_ALIGN (itype2))
3437 return 0;
3438 if (simple_cst_equal (TYPE_MIN_VALUE (itype1), TYPE_MIN_VALUE (itype2))
3439 && simple_cst_equal (TYPE_MAX_VALUE (itype1), TYPE_MAX_VALUE (itype2)))
3440 return 1;
3442 return 0;
3445 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
3446 and number of elements specified by the range of values of INDEX_TYPE.
3447 If such a type has already been constructed, reuse it. */
3449 tree
3450 build_array_type (elt_type, index_type)
3451 tree elt_type, index_type;
3453 register tree t;
3454 int hashcode;
3456 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
3458 error ("arrays of functions are not meaningful");
3459 elt_type = integer_type_node;
3462 /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
3463 build_pointer_type (elt_type);
3465 /* Allocate the array after the pointer type,
3466 in case we free it in type_hash_canon. */
3467 t = make_node (ARRAY_TYPE);
3468 TREE_TYPE (t) = elt_type;
3469 TYPE_DOMAIN (t) = index_type;
3471 if (index_type == 0)
3473 return t;
3476 hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
3477 t = type_hash_canon (hashcode, t);
3479 #if 0 /* This led to crashes, because it could put a temporary node
3480 on the TYPE_NEXT_VARIANT chain of a permanent one. */
3481 /* The main variant of an array type should always
3482 be an array whose element type is the main variant. */
3483 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
3484 change_main_variant (t, build_array_type (TYPE_MAIN_VARIANT (elt_type),
3485 index_type));
3486 #endif
3488 if (TYPE_SIZE (t) == 0)
3489 layout_type (t);
3490 return t;
3493 /* Construct, lay out and return
3494 the type of functions returning type VALUE_TYPE
3495 given arguments of types ARG_TYPES.
3496 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
3497 are data type nodes for the arguments of the function.
3498 If such a type has already been constructed, reuse it. */
3500 tree
3501 build_function_type (value_type, arg_types)
3502 tree value_type, arg_types;
3504 register tree t;
3505 int hashcode;
3507 if (TREE_CODE (value_type) == FUNCTION_TYPE)
3509 error ("function return type cannot be function");
3510 value_type = integer_type_node;
3513 /* Make a node of the sort we want. */
3514 t = make_node (FUNCTION_TYPE);
3515 TREE_TYPE (t) = value_type;
3516 TYPE_ARG_TYPES (t) = arg_types;
3518 /* If we already have such a type, use the old one and free this one. */
3519 hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
3520 t = type_hash_canon (hashcode, t);
3522 if (TYPE_SIZE (t) == 0)
3523 layout_type (t);
3524 return t;
3527 /* Build the node for the type of references-to-TO_TYPE. */
3529 tree
3530 build_reference_type (to_type)
3531 tree to_type;
3533 register tree t = TYPE_REFERENCE_TO (to_type);
3534 register struct obstack *ambient_obstack = current_obstack;
3535 register struct obstack *ambient_saveable_obstack = saveable_obstack;
3537 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3539 if (t)
3540 return t;
3542 /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
3543 if (TREE_PERMANENT (to_type))
3545 current_obstack = &permanent_obstack;
3546 saveable_obstack = &permanent_obstack;
3549 t = make_node (REFERENCE_TYPE);
3550 TREE_TYPE (t) = to_type;
3552 /* Record this type as the pointer to TO_TYPE. */
3553 TYPE_REFERENCE_TO (to_type) = t;
3555 layout_type (t);
3557 current_obstack = ambient_obstack;
3558 saveable_obstack = ambient_saveable_obstack;
3559 return t;
3562 /* Construct, lay out and return the type of methods belonging to class
3563 BASETYPE and whose arguments and values are described by TYPE.
3564 If that type exists already, reuse it.
3565 TYPE must be a FUNCTION_TYPE node. */
3567 tree
3568 build_method_type (basetype, type)
3569 tree basetype, type;
3571 register tree t;
3572 int hashcode;
3574 /* Make a node of the sort we want. */
3575 t = make_node (METHOD_TYPE);
3577 if (TREE_CODE (type) != FUNCTION_TYPE)
3578 abort ();
3580 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3581 TREE_TYPE (t) = TREE_TYPE (type);
3583 /* The actual arglist for this function includes a "hidden" argument
3584 which is "this". Put it into the list of argument types. */
3586 TYPE_ARG_TYPES (t)
3587 = tree_cons (NULL_TREE,
3588 build_pointer_type (basetype), TYPE_ARG_TYPES (type));
3590 /* If we already have such a type, use the old one and free this one. */
3591 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
3592 t = type_hash_canon (hashcode, t);
3594 if (TYPE_SIZE (t) == 0)
3595 layout_type (t);
3597 return t;
3600 /* Construct, lay out and return the type of offsets to a value
3601 of type TYPE, within an object of type BASETYPE.
3602 If a suitable offset type exists already, reuse it. */
3604 tree
3605 build_offset_type (basetype, type)
3606 tree basetype, type;
3608 register tree t;
3609 int hashcode;
3611 /* Make a node of the sort we want. */
3612 t = make_node (OFFSET_TYPE);
3614 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3615 TREE_TYPE (t) = type;
3617 /* If we already have such a type, use the old one and free this one. */
3618 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
3619 t = type_hash_canon (hashcode, t);
3621 if (TYPE_SIZE (t) == 0)
3622 layout_type (t);
3624 return t;
3627 /* Create a complex type whose components are COMPONENT_TYPE. */
3629 tree
3630 build_complex_type (component_type)
3631 tree component_type;
3633 register tree t;
3634 int hashcode;
3636 /* Make a node of the sort we want. */
3637 t = make_node (COMPLEX_TYPE);
3639 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
3640 TYPE_VOLATILE (t) = TYPE_VOLATILE (component_type);
3641 TYPE_READONLY (t) = TYPE_READONLY (component_type);
3643 /* If we already have such a type, use the old one and free this one. */
3644 hashcode = TYPE_HASH (component_type);
3645 t = type_hash_canon (hashcode, t);
3647 if (TYPE_SIZE (t) == 0)
3648 layout_type (t);
3650 return t;
3653 /* Return OP, stripped of any conversions to wider types as much as is safe.
3654 Converting the value back to OP's type makes a value equivalent to OP.
3656 If FOR_TYPE is nonzero, we return a value which, if converted to
3657 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
3659 If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
3660 narrowest type that can hold the value, even if they don't exactly fit.
3661 Otherwise, bit-field references are changed to a narrower type
3662 only if they can be fetched directly from memory in that type.
3664 OP must have integer, real or enumeral type. Pointers are not allowed!
3666 There are some cases where the obvious value we could return
3667 would regenerate to OP if converted to OP's type,
3668 but would not extend like OP to wider types.
3669 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
3670 For example, if OP is (unsigned short)(signed char)-1,
3671 we avoid returning (signed char)-1 if FOR_TYPE is int,
3672 even though extending that to an unsigned short would regenerate OP,
3673 since the result of extending (signed char)-1 to (int)
3674 is different from (int) OP. */
3676 tree
3677 get_unwidened (op, for_type)
3678 register tree op;
3679 tree for_type;
3681 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
3682 /* TYPE_PRECISION is safe in place of type_precision since
3683 pointer types are not allowed. */
3684 register tree type = TREE_TYPE (op);
3685 register unsigned final_prec
3686 = TYPE_PRECISION (for_type != 0 ? for_type : type);
3687 register int uns
3688 = (for_type != 0 && for_type != type
3689 && final_prec > TYPE_PRECISION (type)
3690 && TREE_UNSIGNED (type));
3691 register tree win = op;
3693 while (TREE_CODE (op) == NOP_EXPR)
3695 register int bitschange
3696 = TYPE_PRECISION (TREE_TYPE (op))
3697 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
3699 /* Truncations are many-one so cannot be removed.
3700 Unless we are later going to truncate down even farther. */
3701 if (bitschange < 0
3702 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
3703 break;
3705 /* See what's inside this conversion. If we decide to strip it,
3706 we will set WIN. */
3707 op = TREE_OPERAND (op, 0);
3709 /* If we have not stripped any zero-extensions (uns is 0),
3710 we can strip any kind of extension.
3711 If we have previously stripped a zero-extension,
3712 only zero-extensions can safely be stripped.
3713 Any extension can be stripped if the bits it would produce
3714 are all going to be discarded later by truncating to FOR_TYPE. */
3716 if (bitschange > 0)
3718 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
3719 win = op;
3720 /* TREE_UNSIGNED says whether this is a zero-extension.
3721 Let's avoid computing it if it does not affect WIN
3722 and if UNS will not be needed again. */
3723 if ((uns || TREE_CODE (op) == NOP_EXPR)
3724 && TREE_UNSIGNED (TREE_TYPE (op)))
3726 uns = 1;
3727 win = op;
3732 if (TREE_CODE (op) == COMPONENT_REF
3733 /* Since type_for_size always gives an integer type. */
3734 && TREE_CODE (type) != REAL_TYPE)
3736 unsigned innerprec = TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)));
3737 type = type_for_size (innerprec, TREE_UNSIGNED (TREE_OPERAND (op, 1)));
3739 /* We can get this structure field in the narrowest type it fits in.
3740 If FOR_TYPE is 0, do this only for a field that matches the
3741 narrower type exactly and is aligned for it
3742 The resulting extension to its nominal type (a fullword type)
3743 must fit the same conditions as for other extensions. */
3745 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
3746 && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
3747 && (! uns || final_prec <= innerprec
3748 || TREE_UNSIGNED (TREE_OPERAND (op, 1)))
3749 && type != 0)
3751 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
3752 TREE_OPERAND (op, 1));
3753 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
3754 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
3755 TREE_RAISES (win) = TREE_RAISES (op);
3758 return win;
3761 /* Return OP or a simpler expression for a narrower value
3762 which can be sign-extended or zero-extended to give back OP.
3763 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
3764 or 0 if the value should be sign-extended. */
3766 tree
3767 get_narrower (op, unsignedp_ptr)
3768 register tree op;
3769 int *unsignedp_ptr;
3771 register int uns = 0;
3772 int first = 1;
3773 register tree win = op;
3775 while (TREE_CODE (op) == NOP_EXPR)
3777 register int bitschange
3778 = TYPE_PRECISION (TREE_TYPE (op))
3779 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
3781 /* Truncations are many-one so cannot be removed. */
3782 if (bitschange < 0)
3783 break;
3785 /* See what's inside this conversion. If we decide to strip it,
3786 we will set WIN. */
3787 op = TREE_OPERAND (op, 0);
3789 if (bitschange > 0)
3791 /* An extension: the outermost one can be stripped,
3792 but remember whether it is zero or sign extension. */
3793 if (first)
3794 uns = TREE_UNSIGNED (TREE_TYPE (op));
3795 /* Otherwise, if a sign extension has been stripped,
3796 only sign extensions can now be stripped;
3797 if a zero extension has been stripped, only zero-extensions. */
3798 else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
3799 break;
3800 first = 0;
3802 else /* bitschange == 0 */
3804 /* A change in nominal type can always be stripped, but we must
3805 preserve the unsignedness. */
3806 if (first)
3807 uns = TREE_UNSIGNED (TREE_TYPE (op));
3808 first = 0;
3811 win = op;
3814 if (TREE_CODE (op) == COMPONENT_REF
3815 /* Since type_for_size always gives an integer type. */
3816 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE)
3818 unsigned innerprec = TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)));
3819 tree type = type_for_size (innerprec, TREE_UNSIGNED (op));
3821 /* We can get this structure field in a narrower type that fits it,
3822 but the resulting extension to its nominal type (a fullword type)
3823 must satisfy the same conditions as for other extensions.
3825 Do this only for fields that are aligned (not bit-fields),
3826 because when bit-field insns will be used there is no
3827 advantage in doing this. */
3829 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
3830 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
3831 && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
3832 && type != 0)
3834 if (first)
3835 uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
3836 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
3837 TREE_OPERAND (op, 1));
3838 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
3839 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
3840 TREE_RAISES (win) = TREE_RAISES (op);
3843 *unsignedp_ptr = uns;
3844 return win;
3847 /* Return the precision of a type, for arithmetic purposes.
3848 Supports all types on which arithmetic is possible
3849 (including pointer types).
3850 It's not clear yet what will be right for complex types. */
3853 type_precision (type)
3854 register tree type;
3856 return ((TREE_CODE (type) == INTEGER_TYPE
3857 || TREE_CODE (type) == ENUMERAL_TYPE
3858 || TREE_CODE (type) == REAL_TYPE)
3859 ? TYPE_PRECISION (type) : POINTER_SIZE);
3862 /* Nonzero if integer constant C has a value that is permissible
3863 for type TYPE (an INTEGER_TYPE). */
3866 int_fits_type_p (c, type)
3867 tree c, type;
3869 if (TREE_UNSIGNED (type))
3870 return (! (TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST
3871 && INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c))
3872 && ! (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
3873 && INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type))));
3874 else
3875 return (! (TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST
3876 && INT_CST_LT (TYPE_MAX_VALUE (type), c))
3877 && ! (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
3878 && INT_CST_LT (c, TYPE_MIN_VALUE (type))));
3881 /* Return the innermost context enclosing DECL that is
3882 a FUNCTION_DECL, or zero if none. */
3884 tree
3885 decl_function_context (decl)
3886 tree decl;
3888 tree context;
3890 if (TREE_CODE (decl) == ERROR_MARK)
3891 return 0;
3893 if (TREE_CODE (decl) == SAVE_EXPR)
3894 context = SAVE_EXPR_CONTEXT (decl);
3895 else
3896 context = DECL_CONTEXT (decl);
3898 while (context && TREE_CODE (context) != FUNCTION_DECL)
3900 if (TREE_CODE (context) == RECORD_TYPE
3901 || TREE_CODE (context) == UNION_TYPE)
3902 context = NULL_TREE;
3903 else if (TREE_CODE (context) == TYPE_DECL)
3904 context = DECL_CONTEXT (context);
3905 else if (TREE_CODE (context) == BLOCK)
3906 context = BLOCK_SUPERCONTEXT (context);
3907 else
3908 /* Unhandled CONTEXT !? */
3909 abort ();
3912 return context;
3915 /* Return the innermost context enclosing DECL that is
3916 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
3917 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
3919 tree
3920 decl_type_context (decl)
3921 tree decl;
3923 tree context = DECL_CONTEXT (decl);
3925 while (context)
3927 if (TREE_CODE (context) == RECORD_TYPE
3928 || TREE_CODE (context) == UNION_TYPE
3929 || TREE_CODE (context) == QUAL_UNION_TYPE)
3930 return context;
3931 if (TREE_CODE (context) == TYPE_DECL
3932 || TREE_CODE (context) == FUNCTION_DECL)
3933 context = DECL_CONTEXT (context);
3934 else if (TREE_CODE (context) == BLOCK)
3935 context = BLOCK_SUPERCONTEXT (context);
3936 else
3937 /* Unhandled CONTEXT!? */
3938 abort ();
3940 return NULL_TREE;
3943 void
3944 print_obstack_statistics (str, o)
3945 char *str;
3946 struct obstack *o;
3948 struct _obstack_chunk *chunk = o->chunk;
3949 int n_chunks = 0;
3950 int n_alloc = 0;
3952 while (chunk)
3954 n_chunks += 1;
3955 n_alloc += chunk->limit - &chunk->contents[0];
3956 chunk = chunk->prev;
3958 fprintf (stderr, "obstack %s: %d bytes, %d chunks\n",
3959 str, n_alloc, n_chunks);
3961 void
3962 dump_tree_statistics ()
3964 int i;
3965 int total_nodes, total_bytes;
3967 fprintf (stderr, "\n??? tree nodes created\n\n");
3968 #ifdef GATHER_STATISTICS
3969 fprintf (stderr, "Kind Nodes Bytes\n");
3970 fprintf (stderr, "-------------------------------------\n");
3971 total_nodes = total_bytes = 0;
3972 for (i = 0; i < (int) all_kinds; i++)
3974 fprintf (stderr, "%-20s %6d %9d\n", tree_node_kind_names[i],
3975 tree_node_counts[i], tree_node_sizes[i]);
3976 total_nodes += tree_node_counts[i];
3977 total_bytes += tree_node_sizes[i];
3979 fprintf (stderr, "%-20s %9d\n", "identifier names", id_string_size);
3980 fprintf (stderr, "-------------------------------------\n");
3981 fprintf (stderr, "%-20s %6d %9d\n", "Total", total_nodes, total_bytes);
3982 fprintf (stderr, "-------------------------------------\n");
3983 #else
3984 fprintf (stderr, "(No per-node statistics)\n");
3985 #endif
3986 print_lang_statistics ();
3989 #define FILE_FUNCTION_PREFIX_LEN 9
3991 #ifndef NO_DOLLAR_IN_LABEL
3992 #define FILE_FUNCTION_FORMAT "_GLOBAL_$D$%s"
3993 #else /* NO_DOLLAR_IN_LABEL */
3994 #ifndef NO_DOT_IN_LABEL
3995 #define FILE_FUNCTION_FORMAT "_GLOBAL_.D.%s"
3996 #else /* NO_DOT_IN_LABEL */
3997 #define FILE_FUNCTION_FORMAT "_GLOBAL__D_%s"
3998 #endif /* NO_DOT_IN_LABEL */
3999 #endif /* NO_DOLLAR_IN_LABEL */
4001 extern char * first_global_object_name;
4003 /* If KIND=='I', return a suitable global initializer (constructor) name.
4004 If KIND=='D', return a suitable global clean-up (destructor) name. */
4006 tree
4007 get_file_function_name (kind)
4008 int kind;
4010 char *buf;
4011 register char *p;
4013 if (first_global_object_name)
4014 p = first_global_object_name;
4015 else if (main_input_filename)
4016 p = main_input_filename;
4017 else
4018 p = input_filename;
4020 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p));
4022 /* Set up the name of the file-level functions we may need. */
4023 /* Use a global object (which is already required to be unique over
4024 the program) rather than the file name (which imposes extra
4025 constraints). -- Raeburn@MIT.EDU, 10 Jan 1990. */
4026 sprintf (buf, FILE_FUNCTION_FORMAT, p);
4028 /* Don't need to pull wierd characters out of global names. */
4029 if (p != first_global_object_name)
4031 for (p = buf+11; *p; p++)
4032 if (! ((*p >= '0' && *p <= '9')
4033 #if 0 /* we always want labels, which are valid C++ identifiers (+ `$') */
4034 #ifndef ASM_IDENTIFY_GCC /* this is required if `.' is invalid -- k. raeburn */
4035 || *p == '.'
4036 #endif
4037 #endif
4038 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
4039 || *p == '$'
4040 #endif
4041 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
4042 || *p == '.'
4043 #endif
4044 || (*p >= 'A' && *p <= 'Z')
4045 || (*p >= 'a' && *p <= 'z')))
4046 *p = '_';
4049 buf[FILE_FUNCTION_PREFIX_LEN] = kind;
4051 return get_identifier (buf);
4054 /* Expand (the constant part of) a SET_TYPE CONTRUCTOR node.
4055 The result is placed in BUFFER (which has length BIT_SIZE),
4056 with one bit in each char ('\000' or '\001').
4058 If the constructor is constant, NULL_TREE is returned.
4059 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4061 tree
4062 get_set_constructor_bits (init, buffer, bit_size)
4063 tree init;
4064 char *buffer;
4065 int bit_size;
4067 int i;
4068 tree vals;
4069 HOST_WIDE_INT domain_min
4070 = TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (init))));
4071 tree non_const_bits = NULL_TREE;
4072 for (i = 0; i < bit_size; i++)
4073 buffer[i] = 0;
4075 for (vals = TREE_OPERAND (init, 1);
4076 vals != NULL_TREE; vals = TREE_CHAIN (vals))
4078 if (TREE_CODE (TREE_VALUE (vals)) != INTEGER_CST
4079 || (TREE_PURPOSE (vals) != NULL_TREE
4080 && TREE_CODE (TREE_PURPOSE (vals)) != INTEGER_CST))
4081 non_const_bits =
4082 tree_cons (TREE_PURPOSE (vals), TREE_VALUE (vals), non_const_bits);
4083 else if (TREE_PURPOSE (vals) != NULL_TREE)
4085 /* Set a range of bits to ones. */
4086 HOST_WIDE_INT lo_index
4087 = TREE_INT_CST_LOW (TREE_PURPOSE (vals)) - domain_min;
4088 HOST_WIDE_INT hi_index
4089 = TREE_INT_CST_LOW (TREE_VALUE (vals)) - domain_min;
4090 if (lo_index < 0 || lo_index >= bit_size
4091 || hi_index < 0 || hi_index >= bit_size)
4092 abort ();
4093 for ( ; lo_index <= hi_index; lo_index++)
4094 buffer[lo_index] = 1;
4096 else
4098 /* Set a single bit to one. */
4099 HOST_WIDE_INT index
4100 = TREE_INT_CST_LOW (TREE_VALUE (vals)) - domain_min;
4101 if (index < 0 || index >= bit_size)
4103 error ("invalid initializer for bit string");
4104 return NULL_TREE;
4106 buffer[index] = 1;
4109 return non_const_bits;
4112 /* Expand (the constant part of) a SET_TYPE CONTRUCTOR node.
4113 The result is placed in BUFFER (which is an array of WD_SIZE
4114 words). TYPE_ALIGN bits are stored in each element of BUFFER.
4115 If the constructor is constant, NULL_TREE is returned.
4116 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4118 tree
4119 get_set_constructor_words (init, buffer, wd_size)
4120 tree init;
4121 HOST_WIDE_INT *buffer;
4122 int wd_size;
4124 int i;
4125 tree vals = TREE_OPERAND (init, 1);
4126 int set_word_size = TYPE_ALIGN (TREE_TYPE (init));
4127 int bit_size = wd_size * set_word_size;
4128 int bit_pos = 0;
4129 HOST_WIDE_INT *wordp = buffer;
4130 char *bit_buffer = (char*)alloca(bit_size);
4131 tree non_const_bits = get_set_constructor_bits (init, bit_buffer, bit_size);
4133 for (i = 0; i < wd_size; i++)
4134 buffer[i] = 0;
4136 for (i = 0; i < bit_size; i++)
4138 if (bit_buffer[i])
4140 if (BITS_BIG_ENDIAN)
4141 *wordp |= (1 << (set_word_size - 1 - bit_pos));
4142 else
4143 *wordp |= 1 << bit_pos;
4145 bit_pos++;
4146 if (bit_pos >= set_word_size)
4147 bit_pos = 0, wordp++;
4149 return non_const_bits;