1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
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
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. */
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
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. */
115 struct obstack_stack *next;
116 struct obstack *current;
117 struct obstack *saveable;
118 struct obstack *expression;
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. */
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[] = {
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[] = {
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[] = {
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. */
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[] = {
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
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. */
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);
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
306 /* Let them override the alloc and free routines too. */
307 #ifndef OBSTACK_CHUNK_ALLOC
308 #define OBSTACK_CHUNK_ALLOC xmalloc
310 #ifndef OBSTACK_CHUNK_FREE
311 #define OBSTACK_CHUNK_FREE free
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. */
325 p->all_types_permanent = all_types_permanent;
326 p->momentary_stack = momentary_stack;
327 p->maybepermanent_firstobj = maybepermanent_firstobj;
328 p->momentary_firstobj = momentary_firstobj;
329 p->momentary_function_firstobj = momentary_function_firstobj;
330 p->function_obstack = function_obstack;
331 p->function_maybepermanent_obstack = function_maybepermanent_obstack;
332 p->current_obstack = current_obstack;
333 p->expression_obstack = expression_obstack;
334 p->saveable_obstack = saveable_obstack;
335 p->rtl_obstack = rtl_obstack;
337 /* Objects that need to be saved in this function can be in the nonsaved
338 obstack of the enclosing function since they can't possibly be needed
339 once it has returned. */
340 function_maybepermanent_obstack = function_obstack;
342 function_obstack = (struct obstack *) xmalloc (sizeof (struct obstack));
343 gcc_obstack_init (function_obstack);
345 current_obstack = &permanent_obstack;
346 expression_obstack = &permanent_obstack;
347 rtl_obstack = saveable_obstack = &permanent_obstack;
349 momentary_firstobj = (char *) obstack_finish (&momentary_obstack);
350 momentary_function_firstobj = momentary_firstobj;
351 maybepermanent_firstobj
352 = (char *) obstack_finish (function_maybepermanent_obstack);
355 /* Restore all variables describing the current status from the structure *P.
356 This is used after a nested function. */
359 restore_tree_status (p)
362 all_types_permanent = p->all_types_permanent;
363 momentary_stack = p->momentary_stack;
365 obstack_free (&momentary_obstack, momentary_function_firstobj);
367 /* Free saveable storage used by the function just compiled and not
370 CAUTION: This is in function_obstack of the containing function. So
371 we must be sure that we never allocate from that obstack during
372 the compilation of a nested function if we expect it to survive past the
373 nested function's end. */
374 obstack_free (function_maybepermanent_obstack, maybepermanent_firstobj);
376 obstack_free (function_obstack, 0);
377 free (function_obstack);
379 momentary_firstobj = p->momentary_firstobj;
380 momentary_function_firstobj = p->momentary_function_firstobj;
381 maybepermanent_firstobj = p->maybepermanent_firstobj;
382 function_obstack = p->function_obstack;
383 function_maybepermanent_obstack = p->function_maybepermanent_obstack;
384 current_obstack = p->current_obstack;
385 expression_obstack = p->expression_obstack;
386 saveable_obstack = p->saveable_obstack;
387 rtl_obstack = p->rtl_obstack;
390 /* Start allocating on the temporary (per function) obstack.
391 This is done in start_function before parsing the function body,
392 and before each initialization at top level, and to go back
393 to temporary allocation after doing permanent_allocation. */
396 temporary_allocation ()
398 /* Note that function_obstack at top level points to temporary_obstack.
399 But within a nested function context, it is a separate obstack. */
400 current_obstack = function_obstack;
401 expression_obstack = function_obstack;
402 rtl_obstack = saveable_obstack = function_maybepermanent_obstack;
406 /* Start allocating on the permanent obstack but don't
407 free the temporary data. After calling this, call
408 `permanent_allocation' to fully resume permanent allocation status. */
411 end_temporary_allocation ()
413 current_obstack = &permanent_obstack;
414 expression_obstack = &permanent_obstack;
415 rtl_obstack = saveable_obstack = &permanent_obstack;
418 /* Resume allocating on the temporary obstack, undoing
419 effects of `end_temporary_allocation'. */
422 resume_temporary_allocation ()
424 current_obstack = function_obstack;
425 expression_obstack = function_obstack;
426 rtl_obstack = saveable_obstack = function_maybepermanent_obstack;
429 /* While doing temporary allocation, switch to allocating in such a
430 way as to save all nodes if the function is inlined. Call
431 resume_temporary_allocation to go back to ordinary temporary
435 saveable_allocation ()
437 /* Note that function_obstack at top level points to temporary_obstack.
438 But within a nested function context, it is a separate obstack. */
439 expression_obstack = current_obstack = saveable_obstack;
442 /* Switch to current obstack CURRENT and maybepermanent obstack SAVEABLE,
443 recording the previously current obstacks on a stack.
444 This does not free any storage in any obstack. */
447 push_obstacks (current, saveable)
448 struct obstack *current, *saveable;
450 struct obstack_stack *p
451 = (struct obstack_stack *) obstack_alloc (&obstack_stack_obstack,
452 (sizeof (struct obstack_stack)));
454 p->current = current_obstack;
455 p->saveable = saveable_obstack;
456 p->expression = expression_obstack;
457 p->rtl = rtl_obstack;
458 p->next = obstack_stack;
461 current_obstack = current;
462 expression_obstack = current;
463 rtl_obstack = saveable_obstack = saveable;
466 /* Save the current set of obstacks, but don't change them. */
469 push_obstacks_nochange ()
471 struct obstack_stack *p
472 = (struct obstack_stack *) obstack_alloc (&obstack_stack_obstack,
473 (sizeof (struct obstack_stack)));
475 p->current = current_obstack;
476 p->saveable = saveable_obstack;
477 p->expression = expression_obstack;
478 p->rtl = rtl_obstack;
479 p->next = obstack_stack;
483 /* Pop the obstack selection stack. */
488 struct obstack_stack *p = obstack_stack;
489 obstack_stack = p->next;
491 current_obstack = p->current;
492 saveable_obstack = p->saveable;
493 expression_obstack = p->expression;
494 rtl_obstack = p->rtl;
496 obstack_free (&obstack_stack_obstack, p);
499 /* Nonzero if temporary allocation is currently in effect.
500 Zero if currently doing permanent allocation. */
503 allocation_temporary_p ()
505 return current_obstack != &permanent_obstack;
508 /* Go back to allocating on the permanent obstack
509 and free everything in the temporary obstack.
511 FUNCTION_END is true only if we have just finished compiling a function.
512 In that case, we also free preserved initial values on the momentary
516 permanent_allocation (function_end)
519 /* Free up previous temporary obstack data */
520 obstack_free (&temporary_obstack, temporary_firstobj);
523 obstack_free (&momentary_obstack, momentary_function_firstobj);
524 momentary_firstobj = momentary_function_firstobj;
527 obstack_free (&momentary_obstack, momentary_firstobj);
528 obstack_free (&maybepermanent_obstack, maybepermanent_firstobj);
529 obstack_free (&temp_decl_obstack, temp_decl_firstobj);
531 current_obstack = &permanent_obstack;
532 expression_obstack = &permanent_obstack;
533 rtl_obstack = saveable_obstack = &permanent_obstack;
536 /* Save permanently everything on the maybepermanent_obstack. */
541 maybepermanent_firstobj
542 = (char *) obstack_alloc (function_maybepermanent_obstack, 0);
546 preserve_initializer ()
548 struct momentary_level *tem;
552 = (char *) obstack_alloc (&temporary_obstack, 0);
553 maybepermanent_firstobj
554 = (char *) obstack_alloc (function_maybepermanent_obstack, 0);
556 old_momentary = momentary_firstobj;
558 = (char *) obstack_alloc (&momentary_obstack, 0);
559 if (momentary_firstobj != old_momentary)
560 for (tem = momentary_stack; tem; tem = tem->prev)
561 tem->base = momentary_firstobj;
564 /* Start allocating new rtl in current_obstack.
565 Use resume_temporary_allocation
566 to go back to allocating rtl in saveable_obstack. */
569 rtl_in_current_obstack ()
571 rtl_obstack = current_obstack;
574 /* Start allocating rtl from saveable_obstack. Intended to be used after
575 a call to push_obstacks_nochange. */
578 rtl_in_saveable_obstack ()
580 rtl_obstack = saveable_obstack;
583 /* Allocate SIZE bytes in the current obstack
584 and return a pointer to them.
585 In practice the current obstack is always the temporary one. */
591 return (char *) obstack_alloc (current_obstack, size);
594 /* Free the object PTR in the current obstack
595 as well as everything allocated since PTR.
596 In practice the current obstack is always the temporary one. */
602 obstack_free (current_obstack, ptr);
605 /* Allocate SIZE bytes in the permanent obstack
606 and return a pointer to them. */
612 return (char *) obstack_alloc (&permanent_obstack, size);
615 /* Allocate NELEM items of SIZE bytes in the permanent obstack
616 and return a pointer to them. The storage is cleared before
617 returning the value. */
620 perm_calloc (nelem, size)
624 char *rval = (char *) obstack_alloc (&permanent_obstack, nelem * size);
625 bzero (rval, nelem * size);
629 /* Allocate SIZE bytes in the saveable obstack
630 and return a pointer to them. */
636 return (char *) obstack_alloc (saveable_obstack, size);
639 /* Print out which obstack an object is in. */
642 print_obstack_name (object, file, prefix)
647 struct obstack *obstack = NULL;
648 char *obstack_name = NULL;
651 for (p = outer_function_chain; p; p = p->next)
653 if (_obstack_allocated_p (p->function_obstack, object))
655 obstack = p->function_obstack;
656 obstack_name = "containing function obstack";
658 if (_obstack_allocated_p (p->function_maybepermanent_obstack, object))
660 obstack = p->function_maybepermanent_obstack;
661 obstack_name = "containing function maybepermanent obstack";
665 if (_obstack_allocated_p (&obstack_stack_obstack, object))
667 obstack = &obstack_stack_obstack;
668 obstack_name = "obstack_stack_obstack";
670 else if (_obstack_allocated_p (function_obstack, object))
672 obstack = function_obstack;
673 obstack_name = "function obstack";
675 else if (_obstack_allocated_p (&permanent_obstack, object))
677 obstack = &permanent_obstack;
678 obstack_name = "permanent_obstack";
680 else if (_obstack_allocated_p (&momentary_obstack, object))
682 obstack = &momentary_obstack;
683 obstack_name = "momentary_obstack";
685 else if (_obstack_allocated_p (function_maybepermanent_obstack, object))
687 obstack = function_maybepermanent_obstack;
688 obstack_name = "function maybepermanent obstack";
690 else if (_obstack_allocated_p (&temp_decl_obstack, object))
692 obstack = &temp_decl_obstack;
693 obstack_name = "temp_decl_obstack";
696 /* Check to see if the object is in the free area of the obstack. */
699 if (object >= obstack->next_free
700 && object < obstack->chunk_limit)
701 fprintf (file, "%s in free portion of obstack %s",
702 prefix, obstack_name);
704 fprintf (file, "%s allocated from %s", prefix, obstack_name);
707 fprintf (file, "%s not allocated from any obstack", prefix);
711 debug_obstack (object)
714 print_obstack_name (object, stderr, "object");
715 fprintf (stderr, ".\n");
718 /* Return 1 if OBJ is in the permanent obstack.
719 This is slow, and should be used only for debugging.
720 Use TREE_PERMANENT for other purposes. */
723 object_permanent_p (obj)
726 return _obstack_allocated_p (&permanent_obstack, obj);
729 /* Start a level of momentary allocation.
730 In C, each compound statement has its own level
731 and that level is freed at the end of each statement.
732 All expression nodes are allocated in the momentary allocation level. */
737 struct momentary_level *tem
738 = (struct momentary_level *) obstack_alloc (&momentary_obstack,
739 sizeof (struct momentary_level));
740 tem->prev = momentary_stack;
741 tem->base = (char *) obstack_base (&momentary_obstack);
742 tem->obstack = expression_obstack;
743 momentary_stack = tem;
744 expression_obstack = &momentary_obstack;
747 /* Free all the storage in the current momentary-allocation level.
748 In C, this happens at the end of each statement. */
753 obstack_free (&momentary_obstack, momentary_stack->base);
756 /* Discard a level of momentary allocation.
757 In C, this happens at the end of each compound statement.
758 Restore the status of expression node allocation
759 that was in effect before this level was created. */
764 struct momentary_level *tem = momentary_stack;
765 momentary_stack = tem->prev;
766 expression_obstack = tem->obstack;
767 /* We can't free TEM from the momentary_obstack, because there might
768 be objects above it which have been saved. We can free back to the
769 stack of the level we are popping off though. */
770 obstack_free (&momentary_obstack, tem->base);
773 /* Pop back to the previous level of momentary allocation,
774 but don't free any momentary data just yet. */
777 pop_momentary_nofree ()
779 struct momentary_level *tem = momentary_stack;
780 momentary_stack = tem->prev;
781 expression_obstack = tem->obstack;
784 /* Call when starting to parse a declaration:
785 make expressions in the declaration last the length of the function.
786 Returns an argument that should be passed to resume_momentary later. */
791 register int tem = expression_obstack == &momentary_obstack;
792 expression_obstack = saveable_obstack;
796 /* Call when finished parsing a declaration:
797 restore the treatment of node-allocation that was
798 in effect before the suspension.
799 YES should be the value previously returned by suspend_momentary. */
802 resume_momentary (yes)
806 expression_obstack = &momentary_obstack;
809 /* Init the tables indexed by tree code.
810 Note that languages can add to these tables to define their own codes. */
815 tree_code_type = (char **) xmalloc (sizeof (standard_tree_code_type));
816 tree_code_length = (int *) xmalloc (sizeof (standard_tree_code_length));
817 tree_code_name = (char **) xmalloc (sizeof (standard_tree_code_name));
818 bcopy ((char *) standard_tree_code_type, (char *) tree_code_type,
819 sizeof (standard_tree_code_type));
820 bcopy ((char *) standard_tree_code_length, (char *) tree_code_length,
821 sizeof (standard_tree_code_length));
822 bcopy ((char *) standard_tree_code_name, (char *) tree_code_name,
823 sizeof (standard_tree_code_name));
826 /* Return a newly allocated node of code CODE.
827 Initialize the node's unique id and its TREE_PERMANENT flag.
828 For decl and type nodes, some other fields are initialized.
829 The rest of the node is initialized to zero.
831 Achoo! I got a code in the node. */
838 register int type = TREE_CODE_CLASS (code);
840 register struct obstack *obstack = current_obstack;
842 register tree_node_kind kind;
846 case 'd': /* A decl node */
847 #ifdef GATHER_STATISTICS
850 length = sizeof (struct tree_decl);
851 /* All decls in an inline function need to be saved. */
852 if (obstack != &permanent_obstack)
853 obstack = saveable_obstack;
855 /* PARM_DECLs go on the context of the parent. If this is a nested
856 function, then we must allocate the PARM_DECL on the parent's
857 obstack, so that they will live to the end of the parent's
858 closing brace. This is neccesary in case we try to inline the
859 function into its parent.
861 PARM_DECLs of top-level functions do not have this problem. However,
862 we allocate them where we put the FUNCTION_DECL for languauges such as
863 Ada that need to consult some flags in the PARM_DECLs of the function
866 See comment in restore_tree_status for why we can't put this
867 in function_obstack. */
868 if (code == PARM_DECL && obstack != &permanent_obstack)
871 if (current_function_decl)
872 context = decl_function_context (current_function_decl);
876 = find_function_data (context)->function_maybepermanent_obstack;
880 case 't': /* a type node */
881 #ifdef GATHER_STATISTICS
884 length = sizeof (struct tree_type);
885 /* All data types are put where we can preserve them if nec. */
886 if (obstack != &permanent_obstack)
887 obstack = all_types_permanent ? &permanent_obstack : saveable_obstack;
890 case 'b': /* a lexical block */
891 #ifdef GATHER_STATISTICS
894 length = sizeof (struct tree_block);
895 /* All BLOCK nodes are put where we can preserve them if nec. */
896 if (obstack != &permanent_obstack)
897 obstack = saveable_obstack;
900 case 's': /* an expression with side effects */
901 #ifdef GATHER_STATISTICS
905 case 'r': /* a reference */
906 #ifdef GATHER_STATISTICS
910 case 'e': /* an expression */
911 case '<': /* a comparison expression */
912 case '1': /* a unary arithmetic expression */
913 case '2': /* a binary arithmetic expression */
914 #ifdef GATHER_STATISTICS
918 obstack = expression_obstack;
919 /* All BIND_EXPR nodes are put where we can preserve them if nec. */
920 if (code == BIND_EXPR && obstack != &permanent_obstack)
921 obstack = saveable_obstack;
922 length = sizeof (struct tree_exp)
923 + (tree_code_length[(int) code] - 1) * sizeof (char *);
926 case 'c': /* a constant */
927 #ifdef GATHER_STATISTICS
930 obstack = expression_obstack;
932 /* We can't use tree_code_length for INTEGER_CST, since the number of
933 words is machine-dependent due to varying length of HOST_WIDE_INT,
934 which might be wider than a pointer (e.g., long long). Similarly
935 for REAL_CST, since the number of words is machine-dependent due
936 to varying size and alignment of `double'. */
938 if (code == INTEGER_CST)
939 length = sizeof (struct tree_int_cst);
940 else if (code == REAL_CST)
941 length = sizeof (struct tree_real_cst);
943 length = sizeof (struct tree_common)
944 + tree_code_length[(int) code] * sizeof (char *);
947 case 'x': /* something random, like an identifier. */
948 #ifdef GATHER_STATISTICS
949 if (code == IDENTIFIER_NODE)
951 else if (code == OP_IDENTIFIER)
953 else if (code == TREE_VEC)
958 length = sizeof (struct tree_common)
959 + tree_code_length[(int) code] * sizeof (char *);
960 /* Identifier nodes are always permanent since they are
961 unique in a compiler run. */
962 if (code == IDENTIFIER_NODE) obstack = &permanent_obstack;
969 t = (tree) obstack_alloc (obstack, length);
971 #ifdef GATHER_STATISTICS
972 tree_node_counts[(int)kind]++;
973 tree_node_sizes[(int)kind] += length;
976 /* Clear a word at a time. */
977 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
979 /* Clear any extra bytes. */
980 for (i = length / sizeof (int) * sizeof (int); i < length; i++)
983 TREE_SET_CODE (t, code);
984 if (obstack == &permanent_obstack)
985 TREE_PERMANENT (t) = 1;
990 TREE_SIDE_EFFECTS (t) = 1;
991 TREE_TYPE (t) = void_type_node;
995 if (code != FUNCTION_DECL)
997 DECL_IN_SYSTEM_HEADER (t)
998 = in_system_header && (obstack == &permanent_obstack);
999 DECL_SOURCE_LINE (t) = lineno;
1000 DECL_SOURCE_FILE (t) = (input_filename) ? input_filename : "<built-in>";
1001 DECL_UID (t) = next_decl_uid++;
1005 TYPE_UID (t) = next_type_uid++;
1007 TYPE_MAIN_VARIANT (t) = t;
1008 TYPE_OBSTACK (t) = obstack;
1009 TYPE_ATTRIBUTES (t) = NULL_TREE;
1010 #ifdef SET_DEFAULT_TYPE_ATTRIBUTES
1011 SET_DEFAULT_TYPE_ATTRIBUTES (t);
1016 TREE_CONSTANT (t) = 1;
1023 /* Return a new node with the same contents as NODE
1024 except that its TREE_CHAIN is zero and it has a fresh uid. */
1031 register enum tree_code code = TREE_CODE (node);
1032 register int length;
1035 switch (TREE_CODE_CLASS (code))
1037 case 'd': /* A decl node */
1038 length = sizeof (struct tree_decl);
1041 case 't': /* a type node */
1042 length = sizeof (struct tree_type);
1045 case 'b': /* a lexical block node */
1046 length = sizeof (struct tree_block);
1049 case 'r': /* a reference */
1050 case 'e': /* an expression */
1051 case 's': /* an expression with side effects */
1052 case '<': /* a comparison expression */
1053 case '1': /* a unary arithmetic expression */
1054 case '2': /* a binary arithmetic expression */
1055 length = sizeof (struct tree_exp)
1056 + (tree_code_length[(int) code] - 1) * sizeof (char *);
1059 case 'c': /* a constant */
1060 /* We can't use tree_code_length for INTEGER_CST, since the number of
1061 words is machine-dependent due to varying length of HOST_WIDE_INT,
1062 which might be wider than a pointer (e.g., long long). Similarly
1063 for REAL_CST, since the number of words is machine-dependent due
1064 to varying size and alignment of `double'. */
1065 if (code == INTEGER_CST)
1067 length = sizeof (struct tree_int_cst);
1070 else if (code == REAL_CST)
1072 length = sizeof (struct tree_real_cst);
1076 case 'x': /* something random, like an identifier. */
1077 length = sizeof (struct tree_common)
1078 + tree_code_length[(int) code] * sizeof (char *);
1079 if (code == TREE_VEC)
1080 length += (TREE_VEC_LENGTH (node) - 1) * sizeof (char *);
1083 t = (tree) obstack_alloc (current_obstack, length);
1085 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
1086 ((int *) t)[i] = ((int *) node)[i];
1087 /* Clear any extra bytes. */
1088 for (i = length / sizeof (int) * sizeof (int); i < length; i++)
1089 ((char *) t)[i] = ((char *) node)[i];
1093 if (TREE_CODE_CLASS (code) == 'd')
1094 DECL_UID (t) = next_decl_uid++;
1095 else if (TREE_CODE_CLASS (code) == 't')
1097 TYPE_UID (t) = next_type_uid++;
1098 TYPE_OBSTACK (t) = current_obstack;
1101 TREE_PERMANENT (t) = (current_obstack == &permanent_obstack);
1106 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1107 For example, this can copy a list made of TREE_LIST nodes. */
1114 register tree prev, next;
1119 head = prev = copy_node (list);
1120 next = TREE_CHAIN (list);
1123 TREE_CHAIN (prev) = copy_node (next);
1124 prev = TREE_CHAIN (prev);
1125 next = TREE_CHAIN (next);
1132 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
1133 If an identifier with that name has previously been referred to,
1134 the same node is returned this time. */
1137 get_identifier (text)
1138 register char *text;
1143 register int len, hash_len;
1145 /* Compute length of text in len. */
1146 for (len = 0; text[len]; len++);
1148 /* Decide how much of that length to hash on */
1150 if (warn_id_clash && len > id_clash_len)
1151 hash_len = id_clash_len;
1153 /* Compute hash code */
1154 hi = hash_len * 613 + (unsigned)text[0];
1155 for (i = 1; i < hash_len; i += 2)
1156 hi = ((hi * 613) + (unsigned)(text[i]));
1158 hi &= (1 << HASHBITS) - 1;
1159 hi %= MAX_HASH_TABLE;
1161 /* Search table for identifier */
1162 for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
1163 if (IDENTIFIER_LENGTH (idp) == len
1164 && IDENTIFIER_POINTER (idp)[0] == text[0]
1165 && !bcmp (IDENTIFIER_POINTER (idp), text, len))
1166 return idp; /* <-- return if found */
1168 /* Not found; optionally warn about a similar identifier */
1169 if (warn_id_clash && do_identifier_warnings && len >= id_clash_len)
1170 for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
1171 if (!strncmp (IDENTIFIER_POINTER (idp), text, id_clash_len))
1173 warning ("`%s' and `%s' identical in first %d characters",
1174 IDENTIFIER_POINTER (idp), text, id_clash_len);
1178 if (tree_code_length[(int) IDENTIFIER_NODE] < 0)
1179 abort (); /* set_identifier_size hasn't been called. */
1181 /* Not found, create one, add to chain */
1182 idp = make_node (IDENTIFIER_NODE);
1183 IDENTIFIER_LENGTH (idp) = len;
1184 #ifdef GATHER_STATISTICS
1185 id_string_size += len;
1188 IDENTIFIER_POINTER (idp) = obstack_copy0 (&permanent_obstack, text, len);
1190 TREE_CHAIN (idp) = hash_table[hi];
1191 hash_table[hi] = idp;
1192 return idp; /* <-- return if created */
1195 /* Enable warnings on similar identifiers (if requested).
1196 Done after the built-in identifiers are created. */
1199 start_identifier_warnings ()
1201 do_identifier_warnings = 1;
1204 /* Record the size of an identifier node for the language in use.
1205 SIZE is the total size in bytes.
1206 This is called by the language-specific files. This must be
1207 called before allocating any identifiers. */
1210 set_identifier_size (size)
1213 tree_code_length[(int) IDENTIFIER_NODE]
1214 = (size - sizeof (struct tree_common)) / sizeof (tree);
1217 /* Return a newly constructed INTEGER_CST node whose constant value
1218 is specified by the two ints LOW and HI.
1219 The TREE_TYPE is set to `int'.
1221 This function should be used via the `build_int_2' macro. */
1224 build_int_2_wide (low, hi)
1225 HOST_WIDE_INT low, hi;
1227 register tree t = make_node (INTEGER_CST);
1228 TREE_INT_CST_LOW (t) = low;
1229 TREE_INT_CST_HIGH (t) = hi;
1230 TREE_TYPE (t) = integer_type_node;
1234 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1237 build_real (type, d)
1244 /* Check for valid float value for this type on this target machine;
1245 if not, can print error message and store a valid value in D. */
1246 #ifdef CHECK_FLOAT_VALUE
1247 CHECK_FLOAT_VALUE (TYPE_MODE (type), d, overflow);
1250 v = make_node (REAL_CST);
1251 TREE_TYPE (v) = type;
1252 TREE_REAL_CST (v) = d;
1253 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
1257 /* Return a new REAL_CST node whose type is TYPE
1258 and whose value is the integer value of the INTEGER_CST node I. */
1260 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1263 real_value_from_int_cst (i)
1268 /* Some 386 compilers mishandle unsigned int to float conversions,
1269 so introduce a temporary variable E to avoid those bugs. */
1271 #ifdef REAL_ARITHMETIC
1272 if (! TREE_UNSIGNED (TREE_TYPE (i)))
1273 REAL_VALUE_FROM_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i));
1275 REAL_VALUE_FROM_UNSIGNED_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i));
1276 #else /* not REAL_ARITHMETIC */
1277 if (TREE_INT_CST_HIGH (i) < 0 && ! TREE_UNSIGNED (TREE_TYPE (i)))
1279 d = (double) (~ TREE_INT_CST_HIGH (i));
1280 e = ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
1281 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
1283 e = (double) (unsigned HOST_WIDE_INT) (~ TREE_INT_CST_LOW (i));
1289 d = (double) (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (i);
1290 e = ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
1291 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
1293 e = (double) (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (i);
1296 #endif /* not REAL_ARITHMETIC */
1300 /* This function can't be implemented if we can't do arithmetic
1301 on the float representation. */
1304 build_real_from_int_cst (type, i)
1309 int overflow = TREE_OVERFLOW (i);
1311 jmp_buf float_error;
1313 v = make_node (REAL_CST);
1314 TREE_TYPE (v) = type;
1316 if (setjmp (float_error))
1323 set_float_handler (float_error);
1325 d = REAL_VALUE_TRUNCATE (TYPE_MODE (type), real_value_from_int_cst (i));
1327 /* Check for valid float value for this type on this target machine. */
1330 set_float_handler (NULL_PTR);
1332 #ifdef CHECK_FLOAT_VALUE
1333 CHECK_FLOAT_VALUE (TYPE_MODE (type), d, overflow);
1336 TREE_REAL_CST (v) = d;
1337 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
1341 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1343 /* Return a newly constructed STRING_CST node whose value is
1344 the LEN characters at STR.
1345 The TREE_TYPE is not initialized. */
1348 build_string (len, str)
1352 /* Put the string in saveable_obstack since it will be placed in the RTL
1353 for an "asm" statement and will also be kept around a while if
1354 deferring constant output in varasm.c. */
1356 register tree s = make_node (STRING_CST);
1357 TREE_STRING_LENGTH (s) = len;
1358 TREE_STRING_POINTER (s) = obstack_copy0 (saveable_obstack, str, len);
1362 /* Return a newly constructed COMPLEX_CST node whose value is
1363 specified by the real and imaginary parts REAL and IMAG.
1364 Both REAL and IMAG should be constant nodes.
1365 The TREE_TYPE is not initialized. */
1368 build_complex (real, imag)
1371 register tree t = make_node (COMPLEX_CST);
1373 TREE_REALPART (t) = real;
1374 TREE_IMAGPART (t) = imag;
1375 TREE_TYPE (t) = build_complex_type (TREE_TYPE (real));
1376 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
1377 TREE_CONSTANT_OVERFLOW (t)
1378 = TREE_CONSTANT_OVERFLOW (real) | TREE_CONSTANT_OVERFLOW (imag);
1382 /* Build a newly constructed TREE_VEC node of length LEN. */
1388 register int length = (len-1) * sizeof (tree) + sizeof (struct tree_vec);
1389 register struct obstack *obstack = current_obstack;
1392 #ifdef GATHER_STATISTICS
1393 tree_node_counts[(int)vec_kind]++;
1394 tree_node_sizes[(int)vec_kind] += length;
1397 t = (tree) obstack_alloc (obstack, length);
1399 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
1402 TREE_SET_CODE (t, TREE_VEC);
1403 TREE_VEC_LENGTH (t) = len;
1404 if (obstack == &permanent_obstack)
1405 TREE_PERMANENT (t) = 1;
1410 /* Return 1 if EXPR is the integer constant zero. */
1413 integer_zerop (expr)
1418 return (TREE_CODE (expr) == INTEGER_CST
1419 && TREE_INT_CST_LOW (expr) == 0
1420 && TREE_INT_CST_HIGH (expr) == 0);
1423 /* Return 1 if EXPR is the integer constant one. */
1431 return (TREE_CODE (expr) == INTEGER_CST
1432 && TREE_INT_CST_LOW (expr) == 1
1433 && TREE_INT_CST_HIGH (expr) == 0);
1436 /* Return 1 if EXPR is an integer containing all 1's
1437 in as much precision as it contains. */
1440 integer_all_onesp (expr)
1448 if (TREE_CODE (expr) != INTEGER_CST)
1451 uns = TREE_UNSIGNED (TREE_TYPE (expr));
1453 return TREE_INT_CST_LOW (expr) == -1 && TREE_INT_CST_HIGH (expr) == -1;
1455 prec = TYPE_PRECISION (TREE_TYPE (expr));
1456 if (prec >= HOST_BITS_PER_WIDE_INT)
1458 int high_value, shift_amount;
1460 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1462 if (shift_amount > HOST_BITS_PER_WIDE_INT)
1463 /* Can not handle precisions greater than twice the host int size. */
1465 else if (shift_amount == HOST_BITS_PER_WIDE_INT)
1466 /* Shifting by the host word size is undefined according to the ANSI
1467 standard, so we must handle this as a special case. */
1470 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1472 return TREE_INT_CST_LOW (expr) == -1
1473 && TREE_INT_CST_HIGH (expr) == high_value;
1476 return TREE_INT_CST_LOW (expr) == ((HOST_WIDE_INT) 1 << prec) - 1;
1479 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1483 integer_pow2p (expr)
1486 HOST_WIDE_INT high, low;
1490 if (TREE_CODE (expr) != INTEGER_CST)
1493 high = TREE_INT_CST_HIGH (expr);
1494 low = TREE_INT_CST_LOW (expr);
1496 if (high == 0 && low == 0)
1499 return ((high == 0 && (low & (low - 1)) == 0)
1500 || (low == 0 && (high & (high - 1)) == 0));
1503 /* Return 1 if EXPR is the real constant zero. */
1511 return (TREE_CODE (expr) == REAL_CST
1512 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0));
1515 /* Return 1 if EXPR is the real constant one. */
1523 return (TREE_CODE (expr) == REAL_CST
1524 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1));
1527 /* Return 1 if EXPR is the real constant two. */
1535 return (TREE_CODE (expr) == REAL_CST
1536 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2));
1539 /* Nonzero if EXP is a constant or a cast of a constant. */
1542 really_constant_p (exp)
1545 /* This is not quite the same as STRIP_NOPS. It does more. */
1546 while (TREE_CODE (exp) == NOP_EXPR
1547 || TREE_CODE (exp) == CONVERT_EXPR
1548 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1549 exp = TREE_OPERAND (exp, 0);
1550 return TREE_CONSTANT (exp);
1553 /* Return first list element whose TREE_VALUE is ELEM.
1554 Return 0 if ELEM is not it LIST. */
1557 value_member (elem, list)
1562 if (elem == TREE_VALUE (list))
1564 list = TREE_CHAIN (list);
1569 /* Return first list element whose TREE_PURPOSE is ELEM.
1570 Return 0 if ELEM is not it LIST. */
1573 purpose_member (elem, list)
1578 if (elem == TREE_PURPOSE (list))
1580 list = TREE_CHAIN (list);
1585 /* Return first list element whose BINFO_TYPE is ELEM.
1586 Return 0 if ELEM is not it LIST. */
1589 binfo_member (elem, list)
1594 if (elem == BINFO_TYPE (list))
1596 list = TREE_CHAIN (list);
1601 /* Return nonzero if ELEM is part of the chain CHAIN. */
1604 chain_member (elem, chain)
1611 chain = TREE_CHAIN (chain);
1617 /* Return the length of a chain of nodes chained through TREE_CHAIN.
1618 We expect a null pointer to mark the end of the chain.
1619 This is the Lisp primitive `length'. */
1626 register int len = 0;
1628 for (tail = t; tail; tail = TREE_CHAIN (tail))
1634 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
1635 by modifying the last node in chain 1 to point to chain 2.
1636 This is the Lisp primitive `nconc'. */
1648 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
1650 TREE_CHAIN (t1) = op2;
1651 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
1653 abort (); /* Circularity created. */
1659 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
1663 register tree chain;
1667 while (next = TREE_CHAIN (chain))
1672 /* Reverse the order of elements in the chain T,
1673 and return the new head of the chain (old last element). */
1679 register tree prev = 0, decl, next;
1680 for (decl = t; decl; decl = next)
1682 next = TREE_CHAIN (decl);
1683 TREE_CHAIN (decl) = prev;
1689 /* Given a chain CHAIN of tree nodes,
1690 construct and return a list of those nodes. */
1696 tree result = NULL_TREE;
1697 tree in_tail = chain;
1698 tree out_tail = NULL_TREE;
1702 tree next = tree_cons (NULL_TREE, in_tail, NULL_TREE);
1704 TREE_CHAIN (out_tail) = next;
1708 in_tail = TREE_CHAIN (in_tail);
1714 /* Return a newly created TREE_LIST node whose
1715 purpose and value fields are PARM and VALUE. */
1718 build_tree_list (parm, value)
1721 register tree t = make_node (TREE_LIST);
1722 TREE_PURPOSE (t) = parm;
1723 TREE_VALUE (t) = value;
1727 /* Similar, but build on the temp_decl_obstack. */
1730 build_decl_list (parm, value)
1734 register struct obstack *ambient_obstack = current_obstack;
1735 current_obstack = &temp_decl_obstack;
1736 node = build_tree_list (parm, value);
1737 current_obstack = ambient_obstack;
1741 /* Return a newly created TREE_LIST node whose
1742 purpose and value fields are PARM and VALUE
1743 and whose TREE_CHAIN is CHAIN. */
1746 tree_cons (purpose, value, chain)
1747 tree purpose, value, chain;
1750 register tree node = make_node (TREE_LIST);
1753 register tree node = (tree) obstack_alloc (current_obstack, sizeof (struct tree_list));
1754 #ifdef GATHER_STATISTICS
1755 tree_node_counts[(int)x_kind]++;
1756 tree_node_sizes[(int)x_kind] += sizeof (struct tree_list);
1759 for (i = (sizeof (struct tree_common) / sizeof (int)) - 1; i >= 0; i--)
1760 ((int *) node)[i] = 0;
1762 TREE_SET_CODE (node, TREE_LIST);
1763 if (current_obstack == &permanent_obstack)
1764 TREE_PERMANENT (node) = 1;
1767 TREE_CHAIN (node) = chain;
1768 TREE_PURPOSE (node) = purpose;
1769 TREE_VALUE (node) = value;
1773 /* Similar, but build on the temp_decl_obstack. */
1776 decl_tree_cons (purpose, value, chain)
1777 tree purpose, value, chain;
1780 register struct obstack *ambient_obstack = current_obstack;
1781 current_obstack = &temp_decl_obstack;
1782 node = tree_cons (purpose, value, chain);
1783 current_obstack = ambient_obstack;
1787 /* Same as `tree_cons' but make a permanent object. */
1790 perm_tree_cons (purpose, value, chain)
1791 tree purpose, value, chain;
1794 register struct obstack *ambient_obstack = current_obstack;
1795 current_obstack = &permanent_obstack;
1797 node = tree_cons (purpose, value, chain);
1798 current_obstack = ambient_obstack;
1802 /* Same as `tree_cons', but make this node temporary, regardless. */
1805 temp_tree_cons (purpose, value, chain)
1806 tree purpose, value, chain;
1809 register struct obstack *ambient_obstack = current_obstack;
1810 current_obstack = &temporary_obstack;
1812 node = tree_cons (purpose, value, chain);
1813 current_obstack = ambient_obstack;
1817 /* Same as `tree_cons', but save this node if the function's RTL is saved. */
1820 saveable_tree_cons (purpose, value, chain)
1821 tree purpose, value, chain;
1824 register struct obstack *ambient_obstack = current_obstack;
1825 current_obstack = saveable_obstack;
1827 node = tree_cons (purpose, value, chain);
1828 current_obstack = ambient_obstack;
1832 /* Return the size nominally occupied by an object of type TYPE
1833 when it resides in memory. The value is measured in units of bytes,
1834 and its data type is that normally used for type sizes
1835 (which is the first type created by make_signed_type or
1836 make_unsigned_type). */
1839 size_in_bytes (type)
1844 if (type == error_mark_node)
1845 return integer_zero_node;
1846 type = TYPE_MAIN_VARIANT (type);
1847 if (TYPE_SIZE (type) == 0)
1849 incomplete_type_error (NULL_TREE, type);
1850 return integer_zero_node;
1852 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1853 size_int (BITS_PER_UNIT));
1854 if (TREE_CODE (t) == INTEGER_CST)
1855 force_fit_type (t, 0);
1859 /* Return the size of TYPE (in bytes) as an integer,
1860 or return -1 if the size can vary. */
1863 int_size_in_bytes (type)
1867 if (type == error_mark_node)
1869 type = TYPE_MAIN_VARIANT (type);
1870 if (TYPE_SIZE (type) == 0)
1872 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1874 if (TREE_INT_CST_HIGH (TYPE_SIZE (type)) != 0)
1876 tree t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1877 size_int (BITS_PER_UNIT));
1878 return TREE_INT_CST_LOW (t);
1880 size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1881 return (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1884 /* Return, as a tree node, the number of elements for TYPE (which is an
1885 ARRAY_TYPE) minus one. This counts only elements of the top array. */
1888 array_type_nelts (type)
1891 tree index_type = TYPE_DOMAIN (type);
1893 return (integer_zerop (TYPE_MIN_VALUE (index_type))
1894 ? TYPE_MAX_VALUE (index_type)
1895 : fold (build (MINUS_EXPR, TREE_TYPE (TYPE_MAX_VALUE (index_type)),
1896 TYPE_MAX_VALUE (index_type),
1897 TYPE_MIN_VALUE (index_type))));
1900 /* Return nonzero if arg is static -- a reference to an object in
1901 static storage. This is not the same as the C meaning of `static'. */
1907 switch (TREE_CODE (arg))
1910 /* Nested functions aren't static. Since taking their address
1911 involves a trampoline. */
1912 if (decl_function_context (arg) != 0)
1914 /* ... fall through ... */
1916 return TREE_STATIC (arg) || DECL_EXTERNAL (arg);
1919 return TREE_STATIC (arg);
1926 return staticp (TREE_OPERAND (arg, 0));
1929 return TREE_CONSTANT (TREE_OPERAND (arg, 0));
1932 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
1933 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
1934 return staticp (TREE_OPERAND (arg, 0));
1940 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
1941 Do this to any expression which may be used in more than one place,
1942 but must be evaluated only once.
1944 Normally, expand_expr would reevaluate the expression each time.
1945 Calling save_expr produces something that is evaluated and recorded
1946 the first time expand_expr is called on it. Subsequent calls to
1947 expand_expr just reuse the recorded value.
1949 The call to expand_expr that generates code that actually computes
1950 the value is the first call *at compile time*. Subsequent calls
1951 *at compile time* generate code to use the saved value.
1952 This produces correct result provided that *at run time* control
1953 always flows through the insns made by the first expand_expr
1954 before reaching the other places where the save_expr was evaluated.
1955 You, the caller of save_expr, must make sure this is so.
1957 Constants, and certain read-only nodes, are returned with no
1958 SAVE_EXPR because that is safe. Expressions containing placeholders
1959 are not touched; see tree.def for an explanation of what these
1966 register tree t = fold (expr);
1968 /* We don't care about whether this can be used as an lvalue in this
1970 while (TREE_CODE (t) == NON_LVALUE_EXPR)
1971 t = TREE_OPERAND (t, 0);
1973 /* If the tree evaluates to a constant, then we don't want to hide that
1974 fact (i.e. this allows further folding, and direct checks for constants).
1975 However, a read-only object that has side effects cannot be bypassed.
1976 Since it is no problem to reevaluate literals, we just return the
1979 if (TREE_CONSTANT (t) || (TREE_READONLY (t) && ! TREE_SIDE_EFFECTS (t))
1980 || TREE_CODE (t) == SAVE_EXPR)
1983 /* If T contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
1984 it means that the size or offset of some field of an object depends on
1985 the value within another field.
1987 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
1988 and some variable since it would then need to be both evaluated once and
1989 evaluated more than once. Front-ends must assure this case cannot
1990 happen by surrounding any such subexpressions in their own SAVE_EXPR
1991 and forcing evaluation at the proper time. */
1992 if (contains_placeholder_p (t))
1995 t = build (SAVE_EXPR, TREE_TYPE (expr), t, current_function_decl, NULL_TREE);
1997 /* This expression might be placed ahead of a jump to ensure that the
1998 value was computed on both sides of the jump. So make sure it isn't
1999 eliminated as dead. */
2000 TREE_SIDE_EFFECTS (t) = 1;
2004 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
2005 or offset that depends on a field within a record.
2007 Note that we only allow such expressions within simple arithmetic
2011 contains_placeholder_p (exp)
2014 register enum tree_code code = TREE_CODE (exp);
2017 /* If we have a WITH_RECORD_EXPR, it "cancels" any PLACEHOLDER_EXPR
2018 in it since it is supplying a value for it. */
2019 if (code == WITH_RECORD_EXPR)
2022 switch (TREE_CODE_CLASS (code))
2025 for (inner = TREE_OPERAND (exp, 0);
2026 TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
2027 inner = TREE_OPERAND (inner, 0))
2029 return TREE_CODE (inner) == PLACEHOLDER_EXPR;
2034 switch (tree_code_length[(int) code])
2037 return contains_placeholder_p (TREE_OPERAND (exp, 0));
2039 return (code != RTL_EXPR
2040 && code != CONSTRUCTOR
2041 && ! (code == SAVE_EXPR && SAVE_EXPR_RTL (exp) != 0)
2042 && code != WITH_RECORD_EXPR
2043 && (contains_placeholder_p (TREE_OPERAND (exp, 0))
2044 || contains_placeholder_p (TREE_OPERAND (exp, 1))));
2046 return (code == COND_EXPR
2047 && (contains_placeholder_p (TREE_OPERAND (exp, 0))
2048 || contains_placeholder_p (TREE_OPERAND (exp, 1))
2049 || contains_placeholder_p (TREE_OPERAND (exp, 2))));
2056 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
2057 return a tree with all occurrences of references to F in a
2058 PLACEHOLDER_EXPR replaced by R. Note that we assume here that EXP
2059 contains only arithmetic expressions. */
2062 substitute_in_expr (exp, f, r)
2067 enum tree_code code = TREE_CODE (exp);
2070 switch (TREE_CODE_CLASS (code))
2077 if (code == PLACEHOLDER_EXPR)
2085 switch (tree_code_length[(int) code])
2088 return fold (build1 (code, TREE_TYPE (exp),
2089 substitute_in_expr (TREE_OPERAND (exp, 0),
2093 /* An RTL_EXPR cannot contain a PLACEHOLDER_EXPR; a CONSTRUCTOR
2094 could, but we don't support it. */
2095 if (code == RTL_EXPR)
2097 else if (code == CONSTRUCTOR)
2100 return fold (build (code, TREE_TYPE (exp),
2101 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2102 substitute_in_expr (TREE_OPERAND (exp, 1),
2106 /* It cannot be that anything inside a SAVE_EXPR contains a
2107 PLACEHOLDER_EXPR. */
2108 if (code == SAVE_EXPR)
2111 if (code != COND_EXPR)
2114 return fold (build (code, TREE_TYPE (exp),
2115 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2116 substitute_in_expr (TREE_OPERAND (exp, 1), f, r),
2117 substitute_in_expr (TREE_OPERAND (exp, 2),
2127 /* If this expression is getting a value from a PLACEHOLDER_EXPR
2128 and it is the right field, replace it with R. */
2129 for (inner = TREE_OPERAND (exp, 0);
2130 TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
2131 inner = TREE_OPERAND (inner, 0))
2133 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2134 && TREE_OPERAND (exp, 1) == f)
2137 return fold (build (code, TREE_TYPE (exp),
2138 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2139 TREE_OPERAND (exp, 1)));
2141 return fold (build (code, TREE_TYPE (exp),
2142 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2143 substitute_in_expr (TREE_OPERAND (exp, 1), f, r),
2144 substitute_in_expr (TREE_OPERAND (exp, 2), f, r)));
2147 return fold (build1 (code, TREE_TYPE (exp),
2148 substitute_in_expr (TREE_OPERAND (exp, 0),
2151 return fold (build (code, TREE_TYPE (exp),
2152 substitute_in_expr (TREE_OPERAND (exp, 0), f, r),
2153 substitute_in_expr (TREE_OPERAND (exp, 1), f, r)));
2157 /* If it wasn't one of the cases we handle, give up. */
2162 /* Given a type T, a FIELD_DECL F, and a replacement value R,
2163 return a new type with all size expressions that contain F
2164 updated by replacing F with R. */
2167 substitute_in_type (t, f, r)
2170 switch (TREE_CODE (t))
2179 if ((TREE_CODE (TYPE_MIN_VALUE (t)) != INTEGER_CST
2180 && contains_placeholder_p (TYPE_MIN_VALUE (t)))
2181 || (TREE_CODE (TYPE_MAX_VALUE (t)) != INTEGER_CST
2182 && contains_placeholder_p (TYPE_MAX_VALUE (t))))
2183 return build_range_type (t,
2184 substitute_in_expr (TYPE_MIN_VALUE (t), f, r),
2185 substitute_in_expr (TYPE_MAX_VALUE (t), f, r));
2189 if ((TYPE_MIN_VALUE (t) != 0
2190 && TREE_CODE (TYPE_MIN_VALUE (t)) != REAL_CST
2191 && contains_placeholder_p (TYPE_MIN_VALUE (t)))
2192 || (TYPE_MAX_VALUE (t) != 0
2193 && TREE_CODE (TYPE_MAX_VALUE (t)) != REAL_CST
2194 && contains_placeholder_p (TYPE_MAX_VALUE (t))))
2196 t = build_type_copy (t);
2198 if (TYPE_MIN_VALUE (t))
2199 TYPE_MIN_VALUE (t) = substitute_in_expr (TYPE_MIN_VALUE (t), f, r);
2200 if (TYPE_MAX_VALUE (t))
2201 TYPE_MAX_VALUE (t) = substitute_in_expr (TYPE_MAX_VALUE (t), f, r);
2206 return build_complex_type (substitute_in_type (TREE_TYPE (t), f, r));
2210 case REFERENCE_TYPE:
2215 /* Don't know how to do these yet. */
2219 t = build_array_type (substitute_in_type (TREE_TYPE (t), f, r),
2220 substitute_in_type (TYPE_DOMAIN (t), f, r));
2227 case QUAL_UNION_TYPE:
2229 tree new = copy_node (t);
2231 tree last_field = 0;
2233 /* Start out with no fields, make new fields, and chain them
2236 TYPE_FIELDS (new) = 0;
2237 TYPE_SIZE (new) = 0;
2239 for (field = TYPE_FIELDS (t); field;
2240 field = TREE_CHAIN (field))
2242 tree new_field = copy_node (field);
2244 TREE_TYPE (new_field)
2245 = substitute_in_type (TREE_TYPE (new_field), f, r);
2247 /* If this is an anonymous field and the type of this field is
2248 a UNION_TYPE or RECORD_TYPE with no elements, ignore it. If
2249 the type just has one element, treat that as the field.
2250 But don't do this if we are processing a QUAL_UNION_TYPE. */
2251 if (TREE_CODE (t) != QUAL_UNION_TYPE && DECL_NAME (new_field) == 0
2252 && (TREE_CODE (TREE_TYPE (new_field)) == UNION_TYPE
2253 || TREE_CODE (TREE_TYPE (new_field)) == RECORD_TYPE))
2255 if (TYPE_FIELDS (TREE_TYPE (new_field)) == 0)
2258 if (TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (new_field))) == 0)
2259 new_field = TYPE_FIELDS (TREE_TYPE (new_field));
2262 DECL_CONTEXT (new_field) = new;
2263 DECL_SIZE (new_field) = 0;
2265 if (TREE_CODE (t) == QUAL_UNION_TYPE)
2267 /* Do the substitution inside the qualifier and if we find
2268 that this field will not be present, omit it. */
2269 DECL_QUALIFIER (new_field)
2270 = substitute_in_expr (DECL_QUALIFIER (field), f, r);
2271 if (integer_zerop (DECL_QUALIFIER (new_field)))
2275 if (last_field == 0)
2276 TYPE_FIELDS (new) = new_field;
2278 TREE_CHAIN (last_field) = new_field;
2280 last_field = new_field;
2282 /* If this is a qualified type and this field will always be
2283 present, we are done. */
2284 if (TREE_CODE (t) == QUAL_UNION_TYPE
2285 && integer_onep (DECL_QUALIFIER (new_field)))
2289 /* If this used to be a qualified union type, but we now know what
2290 field will be present, make this a normal union. */
2291 if (TREE_CODE (new) == QUAL_UNION_TYPE
2292 && (TYPE_FIELDS (new) == 0
2293 || integer_onep (DECL_QUALIFIER (TYPE_FIELDS (new)))))
2294 TREE_SET_CODE (new, UNION_TYPE);
2302 /* Stabilize a reference so that we can use it any number of times
2303 without causing its operands to be evaluated more than once.
2304 Returns the stabilized reference. This works by means of save_expr,
2305 so see the caveats in the comments about save_expr.
2307 Also allows conversion expressions whose operands are references.
2308 Any other kind of expression is returned unchanged. */
2311 stabilize_reference (ref)
2314 register tree result;
2315 register enum tree_code code = TREE_CODE (ref);
2322 /* No action is needed in this case. */
2328 case FIX_TRUNC_EXPR:
2329 case FIX_FLOOR_EXPR:
2330 case FIX_ROUND_EXPR:
2332 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2336 result = build_nt (INDIRECT_REF,
2337 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2341 result = build_nt (COMPONENT_REF,
2342 stabilize_reference (TREE_OPERAND (ref, 0)),
2343 TREE_OPERAND (ref, 1));
2347 result = build_nt (BIT_FIELD_REF,
2348 stabilize_reference (TREE_OPERAND (ref, 0)),
2349 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2350 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2354 result = build_nt (ARRAY_REF,
2355 stabilize_reference (TREE_OPERAND (ref, 0)),
2356 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2360 result = build_nt (COMPOUND_EXPR,
2361 stabilize_reference_1 (TREE_OPERAND (ref, 0)),
2362 stabilize_reference (TREE_OPERAND (ref, 1)));
2366 /* If arg isn't a kind of lvalue we recognize, make no change.
2367 Caller should recognize the error for an invalid lvalue. */
2372 return error_mark_node;
2375 TREE_TYPE (result) = TREE_TYPE (ref);
2376 TREE_READONLY (result) = TREE_READONLY (ref);
2377 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
2378 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
2379 TREE_RAISES (result) = TREE_RAISES (ref);
2384 /* Subroutine of stabilize_reference; this is called for subtrees of
2385 references. Any expression with side-effects must be put in a SAVE_EXPR
2386 to ensure that it is only evaluated once.
2388 We don't put SAVE_EXPR nodes around everything, because assigning very
2389 simple expressions to temporaries causes us to miss good opportunities
2390 for optimizations. Among other things, the opportunity to fold in the
2391 addition of a constant into an addressing mode often gets lost, e.g.
2392 "y[i+1] += x;". In general, we take the approach that we should not make
2393 an assignment unless we are forced into it - i.e., that any non-side effect
2394 operator should be allowed, and that cse should take care of coalescing
2395 multiple utterances of the same expression should that prove fruitful. */
2398 stabilize_reference_1 (e)
2401 register tree result;
2402 register enum tree_code code = TREE_CODE (e);
2404 /* We cannot ignore const expressions because it might be a reference
2405 to a const array but whose index contains side-effects. But we can
2406 ignore things that are actual constant or that already have been
2407 handled by this function. */
2409 if (TREE_CONSTANT (e) || code == SAVE_EXPR)
2412 switch (TREE_CODE_CLASS (code))
2422 /* If the expression has side-effects, then encase it in a SAVE_EXPR
2423 so that it will only be evaluated once. */
2424 /* The reference (r) and comparison (<) classes could be handled as
2425 below, but it is generally faster to only evaluate them once. */
2426 if (TREE_SIDE_EFFECTS (e))
2427 return save_expr (e);
2431 /* Constants need no processing. In fact, we should never reach
2436 /* Division is slow and tends to be compiled with jumps,
2437 especially the division by powers of 2 that is often
2438 found inside of an array reference. So do it just once. */
2439 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
2440 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
2441 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
2442 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
2443 return save_expr (e);
2444 /* Recursively stabilize each operand. */
2445 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
2446 stabilize_reference_1 (TREE_OPERAND (e, 1)));
2450 /* Recursively stabilize each operand. */
2451 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
2458 TREE_TYPE (result) = TREE_TYPE (e);
2459 TREE_READONLY (result) = TREE_READONLY (e);
2460 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
2461 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
2462 TREE_RAISES (result) = TREE_RAISES (e);
2467 /* Low-level constructors for expressions. */
2469 /* Build an expression of code CODE, data type TYPE,
2470 and operands as specified by the arguments ARG1 and following arguments.
2471 Expressions and reference nodes can be created this way.
2472 Constants, decls, types and misc nodes cannot be. */
2475 build VPROTO((enum tree_code code, tree tt, ...))
2478 enum tree_code code;
2483 register int length;
2489 code = va_arg (p, enum tree_code);
2490 tt = va_arg (p, tree);
2493 t = make_node (code);
2494 length = tree_code_length[(int) code];
2499 /* This is equivalent to the loop below, but faster. */
2500 register tree arg0 = va_arg (p, tree);
2501 register tree arg1 = va_arg (p, tree);
2502 TREE_OPERAND (t, 0) = arg0;
2503 TREE_OPERAND (t, 1) = arg1;
2504 if ((arg0 && TREE_SIDE_EFFECTS (arg0))
2505 || (arg1 && TREE_SIDE_EFFECTS (arg1)))
2506 TREE_SIDE_EFFECTS (t) = 1;
2508 = (arg0 && TREE_RAISES (arg0)) || (arg1 && TREE_RAISES (arg1));
2510 else if (length == 1)
2512 register tree arg0 = va_arg (p, tree);
2514 /* Call build1 for this! */
2515 if (TREE_CODE_CLASS (code) != 's')
2517 TREE_OPERAND (t, 0) = arg0;
2518 if (arg0 && TREE_SIDE_EFFECTS (arg0))
2519 TREE_SIDE_EFFECTS (t) = 1;
2520 TREE_RAISES (t) = (arg0 && TREE_RAISES (arg0));
2524 for (i = 0; i < length; i++)
2526 register tree operand = va_arg (p, tree);
2527 TREE_OPERAND (t, i) = operand;
2530 if (TREE_SIDE_EFFECTS (operand))
2531 TREE_SIDE_EFFECTS (t) = 1;
2532 if (TREE_RAISES (operand))
2533 TREE_RAISES (t) = 1;
2541 /* Same as above, but only builds for unary operators.
2542 Saves lions share of calls to `build'; cuts down use
2543 of varargs, which is expensive for RISC machines. */
2545 build1 (code, type, node)
2546 enum tree_code code;
2550 register struct obstack *obstack = current_obstack;
2551 register int i, length;
2552 register tree_node_kind kind;
2555 #ifdef GATHER_STATISTICS
2556 if (TREE_CODE_CLASS (code) == 'r')
2562 obstack = expression_obstack;
2563 length = sizeof (struct tree_exp);
2565 t = (tree) obstack_alloc (obstack, length);
2567 #ifdef GATHER_STATISTICS
2568 tree_node_counts[(int)kind]++;
2569 tree_node_sizes[(int)kind] += length;
2572 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
2575 TREE_TYPE (t) = type;
2576 TREE_SET_CODE (t, code);
2578 if (obstack == &permanent_obstack)
2579 TREE_PERMANENT (t) = 1;
2581 TREE_OPERAND (t, 0) = node;
2584 if (TREE_SIDE_EFFECTS (node))
2585 TREE_SIDE_EFFECTS (t) = 1;
2586 if (TREE_RAISES (node))
2587 TREE_RAISES (t) = 1;
2593 /* Similar except don't specify the TREE_TYPE
2594 and leave the TREE_SIDE_EFFECTS as 0.
2595 It is permissible for arguments to be null,
2596 or even garbage if their values do not matter. */
2599 build_nt VPROTO((enum tree_code code, ...))
2602 enum tree_code code;
2606 register int length;
2612 code = va_arg (p, enum tree_code);
2615 t = make_node (code);
2616 length = tree_code_length[(int) code];
2618 for (i = 0; i < length; i++)
2619 TREE_OPERAND (t, i) = va_arg (p, tree);
2625 /* Similar to `build_nt', except we build
2626 on the temp_decl_obstack, regardless. */
2629 build_parse_node VPROTO((enum tree_code code, ...))
2632 enum tree_code code;
2634 register struct obstack *ambient_obstack = expression_obstack;
2637 register int length;
2643 code = va_arg (p, enum tree_code);
2646 expression_obstack = &temp_decl_obstack;
2648 t = make_node (code);
2649 length = tree_code_length[(int) code];
2651 for (i = 0; i < length; i++)
2652 TREE_OPERAND (t, i) = va_arg (p, tree);
2655 expression_obstack = ambient_obstack;
2660 /* Commented out because this wants to be done very
2661 differently. See cp-lex.c. */
2663 build_op_identifier (op1, op2)
2666 register tree t = make_node (OP_IDENTIFIER);
2667 TREE_PURPOSE (t) = op1;
2668 TREE_VALUE (t) = op2;
2673 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
2674 We do NOT enter this node in any sort of symbol table.
2676 layout_decl is used to set up the decl's storage layout.
2677 Other slots are initialized to 0 or null pointers. */
2680 build_decl (code, name, type)
2681 enum tree_code code;
2686 t = make_node (code);
2688 /* if (type == error_mark_node)
2689 type = integer_type_node; */
2690 /* That is not done, deliberately, so that having error_mark_node
2691 as the type can suppress useless errors in the use of this variable. */
2693 DECL_NAME (t) = name;
2694 DECL_ASSEMBLER_NAME (t) = name;
2695 TREE_TYPE (t) = type;
2697 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
2699 else if (code == FUNCTION_DECL)
2700 DECL_MODE (t) = FUNCTION_MODE;
2705 /* BLOCK nodes are used to represent the structure of binding contours
2706 and declarations, once those contours have been exited and their contents
2707 compiled. This information is used for outputting debugging info. */
2710 build_block (vars, tags, subblocks, supercontext, chain)
2711 tree vars, tags, subblocks, supercontext, chain;
2713 register tree block = make_node (BLOCK);
2714 BLOCK_VARS (block) = vars;
2715 BLOCK_TYPE_TAGS (block) = tags;
2716 BLOCK_SUBBLOCKS (block) = subblocks;
2717 BLOCK_SUPERCONTEXT (block) = supercontext;
2718 BLOCK_CHAIN (block) = chain;
2722 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
2725 Such modified types already made are recorded so that duplicates
2729 build_type_attribute_variant (ttype, attribute)
2730 tree ttype, attribute;
2732 if ( ! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
2734 register int hashcode;
2735 register struct obstack *ambient_obstack = current_obstack;
2738 if (ambient_obstack != &permanent_obstack)
2739 current_obstack = TYPE_OBSTACK (ttype);
2741 ntype = copy_node (ttype);
2742 current_obstack = ambient_obstack;
2744 TYPE_POINTER_TO (ntype) = 0;
2745 TYPE_REFERENCE_TO (ntype) = 0;
2746 TYPE_ATTRIBUTES (ntype) = attribute;
2748 /* Create a new main variant of TYPE. */
2749 TYPE_MAIN_VARIANT (ntype) = ntype;
2750 TYPE_NEXT_VARIANT (ntype) = 0;
2751 TYPE_READONLY (ntype) = TYPE_VOLATILE (ntype) = 0;
2753 hashcode = TYPE_HASH (TREE_CODE (ntype))
2754 + TYPE_HASH (TREE_TYPE (ntype))
2755 + type_hash_list (attribute);
2757 switch (TREE_CODE (ntype))
2760 hashcode += TYPE_HASH (TYPE_ARG_TYPES (ntype));
2763 hashcode += TYPE_HASH (TYPE_DOMAIN (ntype));
2766 hashcode += TYPE_HASH (TYPE_MAX_VALUE (ntype));
2769 hashcode += TYPE_HASH (TYPE_PRECISION (ntype));
2773 ntype = type_hash_canon (hashcode, ntype);
2774 ttype = build_type_variant (ntype, TYPE_READONLY (ttype),
2775 TYPE_VOLATILE (ttype));
2781 /* Return a type like TYPE except that its TYPE_READONLY is CONSTP
2782 and its TYPE_VOLATILE is VOLATILEP.
2784 Such variant types already made are recorded so that duplicates
2787 A variant types should never be used as the type of an expression.
2788 Always copy the variant information into the TREE_READONLY
2789 and TREE_THIS_VOLATILE of the expression, and then give the expression
2790 as its type the "main variant", the variant whose TYPE_READONLY
2791 and TYPE_VOLATILE are zero. Use TYPE_MAIN_VARIANT to find the
2795 build_type_variant (type, constp, volatilep)
2797 int constp, volatilep;
2801 /* Treat any nonzero argument as 1. */
2803 volatilep = !!volatilep;
2805 /* If not generating auxiliary info, search the chain of variants to see
2806 if there is already one there just like the one we need to have. If so,
2807 use that existing one.
2809 We don't do this in the case where we are generating aux info because
2810 in that case we want each typedef names to get it's own distinct type
2811 node, even if the type of this new typedef is the same as some other
2814 if (!flag_gen_aux_info)
2815 for (t = TYPE_MAIN_VARIANT(type); t; t = TYPE_NEXT_VARIANT (t))
2816 if (constp == TYPE_READONLY (t) && volatilep == TYPE_VOLATILE (t))
2819 /* We need a new one. */
2821 t = build_type_copy (type);
2822 TYPE_READONLY (t) = constp;
2823 TYPE_VOLATILE (t) = volatilep;
2828 /* Give TYPE a new main variant: NEW_MAIN.
2829 This is the right thing to do only when something else
2830 about TYPE is modified in place. */
2833 change_main_variant (type, new_main)
2834 tree type, new_main;
2837 tree omain = TYPE_MAIN_VARIANT (type);
2839 /* Remove TYPE from the TYPE_NEXT_VARIANT chain of its main variant. */
2840 if (TYPE_NEXT_VARIANT (omain) == type)
2841 TYPE_NEXT_VARIANT (omain) = TYPE_NEXT_VARIANT (type);
2843 for (t = TYPE_NEXT_VARIANT (omain); t && TYPE_NEXT_VARIANT (t);
2844 t = TYPE_NEXT_VARIANT (t))
2845 if (TYPE_NEXT_VARIANT (t) == type)
2847 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (type);
2851 TYPE_MAIN_VARIANT (type) = new_main;
2852 TYPE_NEXT_VARIANT (type) = TYPE_NEXT_VARIANT (new_main);
2853 TYPE_NEXT_VARIANT (new_main) = type;
2856 /* Create a new variant of TYPE, equivalent but distinct.
2857 This is so the caller can modify it. */
2860 build_type_copy (type)
2863 register tree t, m = TYPE_MAIN_VARIANT (type);
2864 register struct obstack *ambient_obstack = current_obstack;
2866 current_obstack = TYPE_OBSTACK (type);
2867 t = copy_node (type);
2868 current_obstack = ambient_obstack;
2870 TYPE_POINTER_TO (t) = 0;
2871 TYPE_REFERENCE_TO (t) = 0;
2873 /* Add this type to the chain of variants of TYPE. */
2874 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
2875 TYPE_NEXT_VARIANT (m) = t;
2880 /* Hashing of types so that we don't make duplicates.
2881 The entry point is `type_hash_canon'. */
2883 /* Each hash table slot is a bucket containing a chain
2884 of these structures. */
2888 struct type_hash *next; /* Next structure in the bucket. */
2889 int hashcode; /* Hash code of this type. */
2890 tree type; /* The type recorded here. */
2893 /* Now here is the hash table. When recording a type, it is added
2894 to the slot whose index is the hash code mod the table size.
2895 Note that the hash table is used for several kinds of types
2896 (function types, array types and array index range types, for now).
2897 While all these live in the same table, they are completely independent,
2898 and the hash code is computed differently for each of these. */
2900 #define TYPE_HASH_SIZE 59
2901 struct type_hash *type_hash_table[TYPE_HASH_SIZE];
2903 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
2904 with types in the TREE_VALUE slots), by adding the hash codes
2905 of the individual types. */
2908 type_hash_list (list)
2911 register int hashcode;
2913 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
2914 hashcode += TYPE_HASH (TREE_VALUE (tail));
2918 /* Look in the type hash table for a type isomorphic to TYPE.
2919 If one is found, return it. Otherwise return 0. */
2922 type_hash_lookup (hashcode, type)
2926 register struct type_hash *h;
2927 for (h = type_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
2928 if (h->hashcode == hashcode
2929 && TREE_CODE (h->type) == TREE_CODE (type)
2930 && TREE_TYPE (h->type) == TREE_TYPE (type)
2931 && attribute_list_equal (TYPE_ATTRIBUTES (h->type),
2932 TYPE_ATTRIBUTES (type))
2933 && (TYPE_MAX_VALUE (h->type) == TYPE_MAX_VALUE (type)
2934 || tree_int_cst_equal (TYPE_MAX_VALUE (h->type),
2935 TYPE_MAX_VALUE (type)))
2936 && (TYPE_MIN_VALUE (h->type) == TYPE_MIN_VALUE (type)
2937 || tree_int_cst_equal (TYPE_MIN_VALUE (h->type),
2938 TYPE_MIN_VALUE (type)))
2939 && (TYPE_DOMAIN (h->type) == TYPE_DOMAIN (type)
2940 || (TYPE_DOMAIN (h->type)
2941 && TREE_CODE (TYPE_DOMAIN (h->type)) == TREE_LIST
2942 && TYPE_DOMAIN (type)
2943 && TREE_CODE (TYPE_DOMAIN (type)) == TREE_LIST
2944 && type_list_equal (TYPE_DOMAIN (h->type), TYPE_DOMAIN (type)))))
2949 /* Add an entry to the type-hash-table
2950 for a type TYPE whose hash code is HASHCODE. */
2953 type_hash_add (hashcode, type)
2957 register struct type_hash *h;
2959 h = (struct type_hash *) oballoc (sizeof (struct type_hash));
2960 h->hashcode = hashcode;
2962 h->next = type_hash_table[hashcode % TYPE_HASH_SIZE];
2963 type_hash_table[hashcode % TYPE_HASH_SIZE] = h;
2966 /* Given TYPE, and HASHCODE its hash code, return the canonical
2967 object for an identical type if one already exists.
2968 Otherwise, return TYPE, and record it as the canonical object
2969 if it is a permanent object.
2971 To use this function, first create a type of the sort you want.
2972 Then compute its hash code from the fields of the type that
2973 make it different from other similar types.
2974 Then call this function and use the value.
2975 This function frees the type you pass in if it is a duplicate. */
2977 /* Set to 1 to debug without canonicalization. Never set by program. */
2978 int debug_no_type_hash = 0;
2981 type_hash_canon (hashcode, type)
2987 if (debug_no_type_hash)
2990 t1 = type_hash_lookup (hashcode, type);
2993 obstack_free (TYPE_OBSTACK (type), type);
2994 #ifdef GATHER_STATISTICS
2995 tree_node_counts[(int)t_kind]--;
2996 tree_node_sizes[(int)t_kind] -= sizeof (struct tree_type);
3001 /* If this is a permanent type, record it for later reuse. */
3002 if (TREE_PERMANENT (type))
3003 type_hash_add (hashcode, type);
3008 /* Given two lists of attributes, return true if list l2 is
3009 equivalent to l1. */
3012 attribute_list_equal (l1, l2)
3015 return attribute_list_contained (l1, l2)
3016 && attribute_list_contained (l2, l1);
3019 /* Given two lists of attributes, return true if list l2 is
3020 completely contained within l1. */
3023 attribute_list_contained (l1, l2)
3026 register tree t1, t2;
3028 /* First check the obvious, maybe the lists are identical. */
3032 /* Then check the obvious, maybe the lists are similar. */
3033 for (t1 = l1, t2 = l2;
3035 && TREE_VALUE (t1) == TREE_VALUE (t2);
3036 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
3038 /* Maybe the lists are equal. */
3039 if (t1 == 0 && t2 == 0)
3042 for (; t2; t2 = TREE_CHAIN (t2))
3043 if (!value_member (l1, t2))
3048 /* Given two lists of types
3049 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
3050 return 1 if the lists contain the same types in the same order.
3051 Also, the TREE_PURPOSEs must match. */
3054 type_list_equal (l1, l2)
3057 register tree t1, t2;
3058 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
3060 if (TREE_VALUE (t1) != TREE_VALUE (t2))
3062 if (TREE_PURPOSE (t1) != TREE_PURPOSE (t2))
3064 int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
3075 /* Nonzero if integer constants T1 and T2
3076 represent the same constant value. */
3079 tree_int_cst_equal (t1, t2)
3084 if (t1 == 0 || t2 == 0)
3086 if (TREE_CODE (t1) == INTEGER_CST
3087 && TREE_CODE (t2) == INTEGER_CST
3088 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3089 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
3094 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
3095 The precise way of comparison depends on their data type. */
3098 tree_int_cst_lt (t1, t2)
3104 if (!TREE_UNSIGNED (TREE_TYPE (t1)))
3105 return INT_CST_LT (t1, t2);
3106 return INT_CST_LT_UNSIGNED (t1, t2);
3109 /* Return an indication of the sign of the integer constant T.
3110 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
3111 Note that -1 will never be returned it T's type is unsigned. */
3114 tree_int_cst_sgn (t)
3117 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
3119 else if (TREE_UNSIGNED (TREE_TYPE (t)))
3121 else if (TREE_INT_CST_HIGH (t) < 0)
3127 /* Compare two constructor-element-type constants. */
3129 simple_cst_list_equal (l1, l2)
3132 while (l1 != NULL_TREE && l2 != NULL_TREE)
3134 int cmp = simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2));
3139 l1 = TREE_CHAIN (l1);
3140 l2 = TREE_CHAIN (l2);
3145 /* Return truthvalue of whether T1 is the same tree structure as T2.
3146 Return 1 if they are the same.
3147 Return 0 if they are understandably different.
3148 Return -1 if either contains tree structure not understood by
3152 simple_cst_equal (t1, t2)
3155 register enum tree_code code1, code2;
3160 if (t1 == 0 || t2 == 0)
3163 code1 = TREE_CODE (t1);
3164 code2 = TREE_CODE (t2);
3166 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
3167 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR || code2 == NON_LVALUE_EXPR)
3168 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3170 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
3171 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3172 || code2 == NON_LVALUE_EXPR)
3173 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
3181 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3182 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
3185 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3188 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3189 && !bcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3190 TREE_STRING_LENGTH (t1));
3196 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3199 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3202 return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3205 /* Special case: if either target is an unallocated VAR_DECL,
3206 it means that it's going to be unified with whatever the
3207 TARGET_EXPR is really supposed to initialize, so treat it
3208 as being equivalent to anything. */
3209 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
3210 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
3211 && DECL_RTL (TREE_OPERAND (t1, 0)) == 0)
3212 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
3213 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
3214 && DECL_RTL (TREE_OPERAND (t2, 0)) == 0))
3217 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3220 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3222 case WITH_CLEANUP_EXPR:
3223 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3226 return simple_cst_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t1, 2));
3229 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
3230 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3240 /* This general rule works for most tree codes.
3241 All exceptions should be handled above. */
3243 switch (TREE_CODE_CLASS (code1))
3253 for (i=0; i<tree_code_length[(int) code1]; ++i)
3255 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
3265 /* Constructors for pointer, array and function types.
3266 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
3267 constructed by language-dependent code, not here.) */
3269 /* Construct, lay out and return the type of pointers to TO_TYPE.
3270 If such a type has already been constructed, reuse it. */
3273 build_pointer_type (to_type)
3276 register tree t = TYPE_POINTER_TO (to_type);
3278 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3283 /* We need a new one. Put this in the same obstack as TO_TYPE. */
3284 push_obstacks (TYPE_OBSTACK (to_type), TYPE_OBSTACK (to_type));
3285 t = make_node (POINTER_TYPE);
3288 TREE_TYPE (t) = to_type;
3290 /* Record this type as the pointer to TO_TYPE. */
3291 TYPE_POINTER_TO (to_type) = t;
3293 /* Lay out the type. This function has many callers that are concerned
3294 with expression-construction, and this simplifies them all.
3295 Also, it guarantees the TYPE_SIZE is in the same obstack as the type. */
3301 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
3302 MAXVAL should be the maximum value in the domain
3303 (one less than the length of the array). */
3306 build_index_type (maxval)
3309 register tree itype = make_node (INTEGER_TYPE);
3310 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
3311 TYPE_MIN_VALUE (itype) = build_int_2 (0, 0);
3312 TREE_TYPE (TYPE_MIN_VALUE (itype)) = sizetype;
3313 TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
3314 TYPE_MODE (itype) = TYPE_MODE (sizetype);
3315 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
3316 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
3317 if (TREE_CODE (maxval) == INTEGER_CST)
3319 int maxint = (int) TREE_INT_CST_LOW (maxval);
3320 /* If the domain should be empty, make sure the maxval
3321 remains -1 and is not spoiled by truncation. */
3322 if (INT_CST_LT (maxval, integer_zero_node))
3324 TYPE_MAX_VALUE (itype) = build_int_2 (-1, -1);
3325 TREE_TYPE (TYPE_MAX_VALUE (itype)) = sizetype;
3327 return type_hash_canon (maxint < 0 ? ~maxint : maxint, itype);
3333 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
3334 ENUMERAL_TYPE, BOOLEAN_TYPE, or CHAR_TYPE), with
3335 low bound LOWVAL and high bound HIGHVAL.
3336 if TYPE==NULL_TREE, sizetype is used. */
3339 build_range_type (type, lowval, highval)
3340 tree type, lowval, highval;
3342 register tree itype = make_node (INTEGER_TYPE);
3343 TREE_TYPE (itype) = type;
3344 if (type == NULL_TREE)
3346 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
3347 TYPE_MIN_VALUE (itype) = convert (type, lowval);
3348 TYPE_MAX_VALUE (itype) = convert (type, highval);
3349 TYPE_MODE (itype) = TYPE_MODE (type);
3350 TYPE_SIZE (itype) = TYPE_SIZE (type);
3351 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
3352 if ((TREE_CODE (lowval) == INTEGER_CST)
3353 && (TREE_CODE (highval) == INTEGER_CST))
3355 HOST_WIDE_INT highint = TREE_INT_CST_LOW (highval);
3356 HOST_WIDE_INT lowint = TREE_INT_CST_LOW (lowval);
3357 int maxint = (int) (highint - lowint);
3358 return type_hash_canon (maxint < 0 ? ~maxint : maxint, itype);
3364 /* Just like build_index_type, but takes lowval and highval instead
3365 of just highval (maxval). */
3368 build_index_2_type (lowval,highval)
3369 tree lowval, highval;
3371 return build_range_type (NULL_TREE, lowval, highval);
3374 /* Return nonzero iff ITYPE1 and ITYPE2 are equal (in the LISP sense).
3375 Needed because when index types are not hashed, equal index types
3376 built at different times appear distinct, even though structurally,
3380 index_type_equal (itype1, itype2)
3381 tree itype1, itype2;
3383 if (TREE_CODE (itype1) != TREE_CODE (itype2))
3385 if (TREE_CODE (itype1) == INTEGER_TYPE)
3387 if (TYPE_PRECISION (itype1) != TYPE_PRECISION (itype2)
3388 || TYPE_MODE (itype1) != TYPE_MODE (itype2)
3389 || ! simple_cst_equal (TYPE_SIZE (itype1), TYPE_SIZE (itype2))
3390 || TYPE_ALIGN (itype1) != TYPE_ALIGN (itype2))
3392 if (simple_cst_equal (TYPE_MIN_VALUE (itype1), TYPE_MIN_VALUE (itype2))
3393 && simple_cst_equal (TYPE_MAX_VALUE (itype1), TYPE_MAX_VALUE (itype2)))
3399 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
3400 and number of elements specified by the range of values of INDEX_TYPE.
3401 If such a type has already been constructed, reuse it. */
3404 build_array_type (elt_type, index_type)
3405 tree elt_type, index_type;
3410 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
3412 error ("arrays of functions are not meaningful");
3413 elt_type = integer_type_node;
3416 /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
3417 build_pointer_type (elt_type);
3419 /* Allocate the array after the pointer type,
3420 in case we free it in type_hash_canon. */
3421 t = make_node (ARRAY_TYPE);
3422 TREE_TYPE (t) = elt_type;
3423 TYPE_DOMAIN (t) = index_type;
3425 if (index_type == 0)
3430 hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
3431 t = type_hash_canon (hashcode, t);
3433 #if 0 /* This led to crashes, because it could put a temporary node
3434 on the TYPE_NEXT_VARIANT chain of a permanent one. */
3435 /* The main variant of an array type should always
3436 be an array whose element type is the main variant. */
3437 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
3438 change_main_variant (t, build_array_type (TYPE_MAIN_VARIANT (elt_type),
3442 if (TYPE_SIZE (t) == 0)
3447 /* Construct, lay out and return
3448 the type of functions returning type VALUE_TYPE
3449 given arguments of types ARG_TYPES.
3450 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
3451 are data type nodes for the arguments of the function.
3452 If such a type has already been constructed, reuse it. */
3455 build_function_type (value_type, arg_types)
3456 tree value_type, arg_types;
3461 if (TREE_CODE (value_type) == FUNCTION_TYPE)
3463 error ("function return type cannot be function");
3464 value_type = integer_type_node;
3467 /* Make a node of the sort we want. */
3468 t = make_node (FUNCTION_TYPE);
3469 TREE_TYPE (t) = value_type;
3470 TYPE_ARG_TYPES (t) = arg_types;
3472 /* If we already have such a type, use the old one and free this one. */
3473 hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
3474 t = type_hash_canon (hashcode, t);
3476 if (TYPE_SIZE (t) == 0)
3481 /* Build the node for the type of references-to-TO_TYPE. */
3484 build_reference_type (to_type)
3487 register tree t = TYPE_REFERENCE_TO (to_type);
3488 register struct obstack *ambient_obstack = current_obstack;
3489 register struct obstack *ambient_saveable_obstack = saveable_obstack;
3491 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3496 /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
3497 if (TREE_PERMANENT (to_type))
3499 current_obstack = &permanent_obstack;
3500 saveable_obstack = &permanent_obstack;
3503 t = make_node (REFERENCE_TYPE);
3504 TREE_TYPE (t) = to_type;
3506 /* Record this type as the pointer to TO_TYPE. */
3507 TYPE_REFERENCE_TO (to_type) = t;
3511 current_obstack = ambient_obstack;
3512 saveable_obstack = ambient_saveable_obstack;
3516 /* Construct, lay out and return the type of methods belonging to class
3517 BASETYPE and whose arguments and values are described by TYPE.
3518 If that type exists already, reuse it.
3519 TYPE must be a FUNCTION_TYPE node. */
3522 build_method_type (basetype, type)
3523 tree basetype, type;
3528 /* Make a node of the sort we want. */
3529 t = make_node (METHOD_TYPE);
3531 if (TREE_CODE (type) != FUNCTION_TYPE)
3534 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3535 TREE_TYPE (t) = TREE_TYPE (type);
3537 /* The actual arglist for this function includes a "hidden" argument
3538 which is "this". Put it into the list of argument types. */
3541 = tree_cons (NULL_TREE,
3542 build_pointer_type (basetype), TYPE_ARG_TYPES (type));
3544 /* If we already have such a type, use the old one and free this one. */
3545 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
3546 t = type_hash_canon (hashcode, t);
3548 if (TYPE_SIZE (t) == 0)
3554 /* Construct, lay out and return the type of offsets to a value
3555 of type TYPE, within an object of type BASETYPE.
3556 If a suitable offset type exists already, reuse it. */
3559 build_offset_type (basetype, type)
3560 tree basetype, type;
3565 /* Make a node of the sort we want. */
3566 t = make_node (OFFSET_TYPE);
3568 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3569 TREE_TYPE (t) = type;
3571 /* If we already have such a type, use the old one and free this one. */
3572 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
3573 t = type_hash_canon (hashcode, t);
3575 if (TYPE_SIZE (t) == 0)
3581 /* Create a complex type whose components are COMPONENT_TYPE. */
3584 build_complex_type (component_type)
3585 tree component_type;
3590 /* Make a node of the sort we want. */
3591 t = make_node (COMPLEX_TYPE);
3593 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
3594 TYPE_VOLATILE (t) = TYPE_VOLATILE (component_type);
3595 TYPE_READONLY (t) = TYPE_READONLY (component_type);
3597 /* If we already have such a type, use the old one and free this one. */
3598 hashcode = TYPE_HASH (component_type);
3599 t = type_hash_canon (hashcode, t);
3601 if (TYPE_SIZE (t) == 0)
3607 /* Return OP, stripped of any conversions to wider types as much as is safe.
3608 Converting the value back to OP's type makes a value equivalent to OP.
3610 If FOR_TYPE is nonzero, we return a value which, if converted to
3611 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
3613 If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
3614 narrowest type that can hold the value, even if they don't exactly fit.
3615 Otherwise, bit-field references are changed to a narrower type
3616 only if they can be fetched directly from memory in that type.
3618 OP must have integer, real or enumeral type. Pointers are not allowed!
3620 There are some cases where the obvious value we could return
3621 would regenerate to OP if converted to OP's type,
3622 but would not extend like OP to wider types.
3623 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
3624 For example, if OP is (unsigned short)(signed char)-1,
3625 we avoid returning (signed char)-1 if FOR_TYPE is int,
3626 even though extending that to an unsigned short would regenerate OP,
3627 since the result of extending (signed char)-1 to (int)
3628 is different from (int) OP. */
3631 get_unwidened (op, for_type)
3635 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
3636 /* TYPE_PRECISION is safe in place of type_precision since
3637 pointer types are not allowed. */
3638 register tree type = TREE_TYPE (op);
3639 register unsigned final_prec
3640 = TYPE_PRECISION (for_type != 0 ? for_type : type);
3642 = (for_type != 0 && for_type != type
3643 && final_prec > TYPE_PRECISION (type)
3644 && TREE_UNSIGNED (type));
3645 register tree win = op;
3647 while (TREE_CODE (op) == NOP_EXPR)
3649 register int bitschange
3650 = TYPE_PRECISION (TREE_TYPE (op))
3651 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
3653 /* Truncations are many-one so cannot be removed.
3654 Unless we are later going to truncate down even farther. */
3656 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
3659 /* See what's inside this conversion. If we decide to strip it,
3661 op = TREE_OPERAND (op, 0);
3663 /* If we have not stripped any zero-extensions (uns is 0),
3664 we can strip any kind of extension.
3665 If we have previously stripped a zero-extension,
3666 only zero-extensions can safely be stripped.
3667 Any extension can be stripped if the bits it would produce
3668 are all going to be discarded later by truncating to FOR_TYPE. */
3672 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
3674 /* TREE_UNSIGNED says whether this is a zero-extension.
3675 Let's avoid computing it if it does not affect WIN
3676 and if UNS will not be needed again. */
3677 if ((uns || TREE_CODE (op) == NOP_EXPR)
3678 && TREE_UNSIGNED (TREE_TYPE (op)))
3686 if (TREE_CODE (op) == COMPONENT_REF
3687 /* Since type_for_size always gives an integer type. */
3688 && TREE_CODE (type) != REAL_TYPE)
3690 unsigned innerprec = TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)));
3691 type = type_for_size (innerprec, TREE_UNSIGNED (TREE_OPERAND (op, 1)));
3693 /* We can get this structure field in the narrowest type it fits in.
3694 If FOR_TYPE is 0, do this only for a field that matches the
3695 narrower type exactly and is aligned for it
3696 The resulting extension to its nominal type (a fullword type)
3697 must fit the same conditions as for other extensions. */
3699 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
3700 && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
3701 && (! uns || final_prec <= innerprec
3702 || TREE_UNSIGNED (TREE_OPERAND (op, 1)))
3705 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
3706 TREE_OPERAND (op, 1));
3707 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
3708 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
3709 TREE_RAISES (win) = TREE_RAISES (op);
3715 /* Return OP or a simpler expression for a narrower value
3716 which can be sign-extended or zero-extended to give back OP.
3717 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
3718 or 0 if the value should be sign-extended. */
3721 get_narrower (op, unsignedp_ptr)
3725 register int uns = 0;
3727 register tree win = op;
3729 while (TREE_CODE (op) == NOP_EXPR)
3731 register int bitschange
3732 = TYPE_PRECISION (TREE_TYPE (op))
3733 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
3735 /* Truncations are many-one so cannot be removed. */
3739 /* See what's inside this conversion. If we decide to strip it,
3741 op = TREE_OPERAND (op, 0);
3745 /* An extension: the outermost one can be stripped,
3746 but remember whether it is zero or sign extension. */
3748 uns = TREE_UNSIGNED (TREE_TYPE (op));
3749 /* Otherwise, if a sign extension has been stripped,
3750 only sign extensions can now be stripped;
3751 if a zero extension has been stripped, only zero-extensions. */
3752 else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
3756 else /* bitschange == 0 */
3758 /* A change in nominal type can always be stripped, but we must
3759 preserve the unsignedness. */
3761 uns = TREE_UNSIGNED (TREE_TYPE (op));
3768 if (TREE_CODE (op) == COMPONENT_REF
3769 /* Since type_for_size always gives an integer type. */
3770 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE)
3772 unsigned innerprec = TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)));
3773 tree type = type_for_size (innerprec, TREE_UNSIGNED (op));
3775 /* We can get this structure field in a narrower type that fits it,
3776 but the resulting extension to its nominal type (a fullword type)
3777 must satisfy the same conditions as for other extensions.
3779 Do this only for fields that are aligned (not bit-fields),
3780 because when bit-field insns will be used there is no
3781 advantage in doing this. */
3783 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
3784 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
3785 && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
3789 uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
3790 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
3791 TREE_OPERAND (op, 1));
3792 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
3793 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
3794 TREE_RAISES (win) = TREE_RAISES (op);
3797 *unsignedp_ptr = uns;
3801 /* Return the precision of a type, for arithmetic purposes.
3802 Supports all types on which arithmetic is possible
3803 (including pointer types).
3804 It's not clear yet what will be right for complex types. */
3807 type_precision (type)
3810 return ((TREE_CODE (type) == INTEGER_TYPE
3811 || TREE_CODE (type) == ENUMERAL_TYPE
3812 || TREE_CODE (type) == REAL_TYPE)
3813 ? TYPE_PRECISION (type) : POINTER_SIZE);
3816 /* Nonzero if integer constant C has a value that is permissible
3817 for type TYPE (an INTEGER_TYPE). */
3820 int_fits_type_p (c, type)
3823 if (TREE_UNSIGNED (type))
3824 return (! (TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST
3825 && INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c))
3826 && ! (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
3827 && INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type))));
3829 return (! (TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST
3830 && INT_CST_LT (TYPE_MAX_VALUE (type), c))
3831 && ! (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
3832 && INT_CST_LT (c, TYPE_MIN_VALUE (type))));
3835 /* Return the innermost context enclosing DECL that is
3836 a FUNCTION_DECL, or zero if none. */
3839 decl_function_context (decl)
3844 if (TREE_CODE (decl) == ERROR_MARK)
3847 if (TREE_CODE (decl) == SAVE_EXPR)
3848 context = SAVE_EXPR_CONTEXT (decl);
3850 context = DECL_CONTEXT (decl);
3852 while (context && TREE_CODE (context) != FUNCTION_DECL)
3854 if (TREE_CODE (context) == RECORD_TYPE
3855 || TREE_CODE (context) == UNION_TYPE)
3856 context = TYPE_CONTEXT (context);
3857 else if (TREE_CODE (context) == TYPE_DECL)
3858 context = DECL_CONTEXT (context);
3859 else if (TREE_CODE (context) == BLOCK)
3860 context = BLOCK_SUPERCONTEXT (context);
3862 /* Unhandled CONTEXT !? */
3869 /* Return the innermost context enclosing DECL that is
3870 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
3871 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
3874 decl_type_context (decl)
3877 tree context = DECL_CONTEXT (decl);
3881 if (TREE_CODE (context) == RECORD_TYPE
3882 || TREE_CODE (context) == UNION_TYPE
3883 || TREE_CODE (context) == QUAL_UNION_TYPE)
3885 if (TREE_CODE (context) == TYPE_DECL
3886 || TREE_CODE (context) == FUNCTION_DECL)
3887 context = DECL_CONTEXT (context);
3888 else if (TREE_CODE (context) == BLOCK)
3889 context = BLOCK_SUPERCONTEXT (context);
3891 /* Unhandled CONTEXT!? */
3898 print_obstack_statistics (str, o)
3902 struct _obstack_chunk *chunk = o->chunk;
3909 n_alloc += chunk->limit - &chunk->contents[0];
3910 chunk = chunk->prev;
3912 fprintf (stderr, "obstack %s: %d bytes, %d chunks\n",
3913 str, n_alloc, n_chunks);
3916 dump_tree_statistics ()
3919 int total_nodes, total_bytes;
3921 fprintf (stderr, "\n??? tree nodes created\n\n");
3922 #ifdef GATHER_STATISTICS
3923 fprintf (stderr, "Kind Nodes Bytes\n");
3924 fprintf (stderr, "-------------------------------------\n");
3925 total_nodes = total_bytes = 0;
3926 for (i = 0; i < (int) all_kinds; i++)
3928 fprintf (stderr, "%-20s %6d %9d\n", tree_node_kind_names[i],
3929 tree_node_counts[i], tree_node_sizes[i]);
3930 total_nodes += tree_node_counts[i];
3931 total_bytes += tree_node_sizes[i];
3933 fprintf (stderr, "%-20s %9d\n", "identifier names", id_string_size);
3934 fprintf (stderr, "-------------------------------------\n");
3935 fprintf (stderr, "%-20s %6d %9d\n", "Total", total_nodes, total_bytes);
3936 fprintf (stderr, "-------------------------------------\n");
3938 fprintf (stderr, "(No per-node statistics)\n");
3940 print_lang_statistics ();
3943 #define FILE_FUNCTION_PREFIX_LEN 9
3945 #ifndef NO_DOLLAR_IN_LABEL
3946 #define FILE_FUNCTION_FORMAT "_GLOBAL_$D$%s"
3947 #else /* NO_DOLLAR_IN_LABEL */
3948 #ifndef NO_DOT_IN_LABEL
3949 #define FILE_FUNCTION_FORMAT "_GLOBAL_.D.%s"
3950 #else /* NO_DOT_IN_LABEL */
3951 #define FILE_FUNCTION_FORMAT "_GLOBAL__D_%s"
3952 #endif /* NO_DOT_IN_LABEL */
3953 #endif /* NO_DOLLAR_IN_LABEL */
3955 extern char * first_global_object_name;
3957 /* If KIND=='I', return a suitable global initializer (constructor) name.
3958 If KIND=='D', return a suitable global clean-up (destructor) name. */
3961 get_file_function_name (kind)
3967 if (first_global_object_name)
3968 p = first_global_object_name;
3969 else if (main_input_filename)
3970 p = main_input_filename;
3974 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p));
3976 /* Set up the name of the file-level functions we may need. */
3977 /* Use a global object (which is already required to be unique over
3978 the program) rather than the file name (which imposes extra
3979 constraints). -- Raeburn@MIT.EDU, 10 Jan 1990. */
3980 sprintf (buf, FILE_FUNCTION_FORMAT, p);
3982 /* Don't need to pull wierd characters out of global names. */
3983 if (p != first_global_object_name)
3985 for (p = buf+11; *p; p++)
3986 if (! ((*p >= '0' && *p <= '9')
3987 #if 0 /* we always want labels, which are valid C++ identifiers (+ `$') */
3988 #ifndef ASM_IDENTIFY_GCC /* this is required if `.' is invalid -- k. raeburn */
3992 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
3995 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
3998 || (*p >= 'A' && *p <= 'Z')
3999 || (*p >= 'a' && *p <= 'z')))
4003 buf[FILE_FUNCTION_PREFIX_LEN] = kind;
4005 return get_identifier (buf);