Initial revision
[official-gcc.git] / gcc / tree.c
blobc348bf8af22885ca8d06b987b94ad5a1bd418e4d
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
21 /* This file contains the low level primitives for operating on tree nodes,
22 including allocation, list operations, interning of identifiers,
23 construction of data type nodes and statement nodes,
24 and construction of type conversion nodes. It also contains
25 tables index by tree code that describe how to take apart
26 nodes of that code.
28 It is intended to be language-independent, but occasionally
29 calls language-dependent routines defined (for C) in typecheck.c.
31 The low-level allocation routines oballoc and permalloc
32 are used also for allocating many other kinds of objects
33 by all passes of the compiler. */
35 #include "config.h"
36 #include "flags.h"
37 #include "tree.h"
38 #include "function.h"
39 #include "obstack.h"
40 #include "gvarargs.h"
41 #include <stdio.h>
43 #define obstack_chunk_alloc xmalloc
44 #define obstack_chunk_free free
46 /* Tree nodes of permanent duration are allocated in this obstack.
47 They are the identifier nodes, and everything outside of
48 the bodies and parameters of function definitions. */
50 struct obstack permanent_obstack;
52 /* The initial RTL, and all ..._TYPE nodes, in a function
53 are allocated in this obstack. Usually they are freed at the
54 end of the function, but if the function is inline they are saved.
55 For top-level functions, this is maybepermanent_obstack.
56 Separate obstacks are made for nested functions. */
58 struct obstack *function_maybepermanent_obstack;
60 /* This is the function_maybepermanent_obstack for top-level functions. */
62 struct obstack maybepermanent_obstack;
64 /* The contents of the current function definition are allocated
65 in this obstack, and all are freed at the end of the function.
66 For top-level functions, this is temporary_obstack.
67 Separate obstacks are made for nested functions. */
69 struct obstack *function_obstack;
71 /* This is used for reading initializers of global variables. */
73 struct obstack temporary_obstack;
75 /* The tree nodes of an expression are allocated
76 in this obstack, and all are freed at the end of the expression. */
78 struct obstack momentary_obstack;
80 /* The tree nodes of a declarator are allocated
81 in this obstack, and all are freed when the declarator
82 has been parsed. */
84 static struct obstack temp_decl_obstack;
86 /* This points at either permanent_obstack
87 or the current function_maybepermanent_obstack. */
89 struct obstack *saveable_obstack;
91 /* This is same as saveable_obstack during parse and expansion phase;
92 it points to the current function's obstack during optimization.
93 This is the obstack to be used for creating rtl objects. */
95 struct obstack *rtl_obstack;
97 /* This points at either permanent_obstack or the current function_obstack. */
99 struct obstack *current_obstack;
101 /* This points at either permanent_obstack or the current function_obstack
102 or momentary_obstack. */
104 struct obstack *expression_obstack;
106 /* Stack of obstack selections for push_obstacks and pop_obstacks. */
108 struct obstack_stack
110 struct obstack_stack *next;
111 struct obstack *current;
112 struct obstack *saveable;
113 struct obstack *expression;
114 struct obstack *rtl;
117 struct obstack_stack *obstack_stack;
119 /* Obstack for allocating struct obstack_stack entries. */
121 static struct obstack obstack_stack_obstack;
123 /* Addresses of first objects in some obstacks.
124 This is for freeing their entire contents. */
125 char *maybepermanent_firstobj;
126 char *temporary_firstobj;
127 char *momentary_firstobj;
128 char *temp_decl_firstobj;
130 /* Nonzero means all ..._TYPE nodes should be allocated permanently. */
132 int all_types_permanent;
134 /* Stack of places to restore the momentary obstack back to. */
136 struct momentary_level
138 /* Pointer back to previous such level. */
139 struct momentary_level *prev;
140 /* First object allocated within this level. */
141 char *base;
142 /* Value of expression_obstack saved at entry to this level. */
143 struct obstack *obstack;
146 struct momentary_level *momentary_stack;
148 /* Table indexed by tree code giving a string containing a character
149 classifying the tree code. Possibilities are
150 t, d, s, c, r, <, 1, 2 and e. See tree.def for details. */
152 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
154 char *standard_tree_code_type[] = {
155 #include "tree.def"
157 #undef DEFTREECODE
159 /* Table indexed by tree code giving number of expression
160 operands beyond the fixed part of the node structure.
161 Not used for types or decls. */
163 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
165 int standard_tree_code_length[] = {
166 #include "tree.def"
168 #undef DEFTREECODE
170 /* Names of tree components.
171 Used for printing out the tree and error messages. */
172 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
174 char *standard_tree_code_name[] = {
175 #include "tree.def"
177 #undef DEFTREECODE
179 /* Table indexed by tree code giving a string containing a character
180 classifying the tree code. Possibilities are
181 t, d, s, c, r, e, <, 1 and 2. See tree.def for details. */
183 char **tree_code_type;
185 /* Table indexed by tree code giving number of expression
186 operands beyond the fixed part of the node structure.
187 Not used for types or decls. */
189 int *tree_code_length;
191 /* Table indexed by tree code giving name of tree code, as a string. */
193 char **tree_code_name;
195 /* Statistics-gathering stuff. */
196 typedef enum
198 d_kind,
199 t_kind,
200 b_kind,
201 s_kind,
202 r_kind,
203 e_kind,
204 c_kind,
205 id_kind,
206 op_id_kind,
207 perm_list_kind,
208 temp_list_kind,
209 vec_kind,
210 x_kind,
211 lang_decl,
212 lang_type,
213 all_kinds
214 } tree_node_kind;
216 int tree_node_counts[(int)all_kinds];
217 int tree_node_sizes[(int)all_kinds];
218 int id_string_size = 0;
220 char *tree_node_kind_names[] = {
221 "decls",
222 "types",
223 "blocks",
224 "stmts",
225 "refs",
226 "exprs",
227 "constants",
228 "identifiers",
229 "op_identifiers",
230 "perm_tree_lists",
231 "temp_tree_lists",
232 "vecs",
233 "random kinds",
234 "lang_decl kinds",
235 "lang_type kinds"
238 /* Hash table for uniquizing IDENTIFIER_NODEs by name. */
240 #define MAX_HASH_TABLE 1009
241 static tree hash_table[MAX_HASH_TABLE]; /* id hash buckets */
243 /* 0 while creating built-in identifiers. */
244 static int do_identifier_warnings;
246 /* Unique id for next decl created. */
247 static int next_decl_uid;
248 /* Unique id for next type created. */
249 static int next_type_uid = 1;
251 extern char *mode_name[];
253 void gcc_obstack_init ();
254 static tree stabilize_reference_1 ();
256 /* Init the principal obstacks. */
258 void
259 init_obstacks ()
261 gcc_obstack_init (&obstack_stack_obstack);
262 gcc_obstack_init (&permanent_obstack);
264 gcc_obstack_init (&temporary_obstack);
265 temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
266 gcc_obstack_init (&momentary_obstack);
267 momentary_firstobj = (char *) obstack_alloc (&momentary_obstack, 0);
268 gcc_obstack_init (&maybepermanent_obstack);
269 maybepermanent_firstobj
270 = (char *) obstack_alloc (&maybepermanent_obstack, 0);
271 gcc_obstack_init (&temp_decl_obstack);
272 temp_decl_firstobj = (char *) obstack_alloc (&temp_decl_obstack, 0);
274 function_obstack = &temporary_obstack;
275 function_maybepermanent_obstack = &maybepermanent_obstack;
276 current_obstack = &permanent_obstack;
277 expression_obstack = &permanent_obstack;
278 rtl_obstack = saveable_obstack = &permanent_obstack;
280 /* Init the hash table of identifiers. */
281 bzero (hash_table, sizeof hash_table);
284 void
285 gcc_obstack_init (obstack)
286 struct obstack *obstack;
288 /* Let particular systems override the size of a chunk. */
289 #ifndef OBSTACK_CHUNK_SIZE
290 #define OBSTACK_CHUNK_SIZE 0
291 #endif
292 /* Let them override the alloc and free routines too. */
293 #ifndef OBSTACK_CHUNK_ALLOC
294 #define OBSTACK_CHUNK_ALLOC xmalloc
295 #endif
296 #ifndef OBSTACK_CHUNK_FREE
297 #define OBSTACK_CHUNK_FREE free
298 #endif
299 _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
300 (void *(*) ()) OBSTACK_CHUNK_ALLOC,
301 (void (*) ()) OBSTACK_CHUNK_FREE);
304 /* Save all variables describing the current status into the structure *P.
305 This is used before starting a nested function. */
307 void
308 save_tree_status (p)
309 struct function *p;
311 p->all_types_permanent = all_types_permanent;
312 p->momentary_stack = momentary_stack;
313 p->maybepermanent_firstobj = maybepermanent_firstobj;
314 p->momentary_firstobj = momentary_firstobj;
315 p->function_obstack = function_obstack;
316 p->function_maybepermanent_obstack = function_maybepermanent_obstack;
317 p->current_obstack = current_obstack;
318 p->expression_obstack = expression_obstack;
319 p->saveable_obstack = saveable_obstack;
320 p->rtl_obstack = rtl_obstack;
322 function_obstack = (struct obstack *) xmalloc (sizeof (struct obstack));
323 gcc_obstack_init (function_obstack);
325 function_maybepermanent_obstack
326 = (struct obstack *) xmalloc (sizeof (struct obstack));
327 gcc_obstack_init (function_maybepermanent_obstack);
329 current_obstack = &permanent_obstack;
330 expression_obstack = &permanent_obstack;
331 rtl_obstack = saveable_obstack = &permanent_obstack;
333 momentary_firstobj = (char *) obstack_finish (&momentary_obstack);
334 maybepermanent_firstobj
335 = (char *) obstack_finish (function_maybepermanent_obstack);
338 /* Restore all variables describing the current status from the structure *P.
339 This is used after a nested function. */
341 void
342 restore_tree_status (p)
343 struct function *p;
345 all_types_permanent = p->all_types_permanent;
346 momentary_stack = p->momentary_stack;
348 obstack_free (&momentary_obstack, momentary_firstobj);
349 obstack_free (function_obstack, 0);
350 obstack_free (function_maybepermanent_obstack, 0);
351 free (function_obstack);
353 momentary_firstobj = p->momentary_firstobj;
354 maybepermanent_firstobj = p->maybepermanent_firstobj;
355 function_obstack = p->function_obstack;
356 function_maybepermanent_obstack = p->function_maybepermanent_obstack;
357 current_obstack = p->current_obstack;
358 expression_obstack = p->expression_obstack;
359 saveable_obstack = p->saveable_obstack;
360 rtl_obstack = p->rtl_obstack;
363 /* Start allocating on the temporary (per function) obstack.
364 This is done in start_function before parsing the function body,
365 and before each initialization at top level, and to go back
366 to temporary allocation after doing permanent_allocation. */
368 void
369 temporary_allocation ()
371 /* Note that function_obstack at top level points to temporary_obstack.
372 But within a nested function context, it is a separate obstack. */
373 current_obstack = function_obstack;
374 expression_obstack = function_obstack;
375 rtl_obstack = saveable_obstack = function_maybepermanent_obstack;
376 momentary_stack = 0;
379 /* Start allocating on the permanent obstack but don't
380 free the temporary data. After calling this, call
381 `permanent_allocation' to fully resume permanent allocation status. */
383 void
384 end_temporary_allocation ()
386 current_obstack = &permanent_obstack;
387 expression_obstack = &permanent_obstack;
388 rtl_obstack = saveable_obstack = &permanent_obstack;
391 /* Resume allocating on the temporary obstack, undoing
392 effects of `end_temporary_allocation'. */
394 void
395 resume_temporary_allocation ()
397 current_obstack = function_obstack;
398 expression_obstack = function_obstack;
399 rtl_obstack = saveable_obstack = function_maybepermanent_obstack;
402 /* While doing temporary allocation, switch to allocating in such a
403 way as to save all nodes if the function is inlined. Call
404 resume_temporary_allocation to go back to ordinary temporary
405 allocation. */
407 void
408 saveable_allocation ()
410 /* Note that function_obstack at top level points to temporary_obstack.
411 But within a nested function context, it is a separate obstack. */
412 expression_obstack = current_obstack = saveable_obstack;
415 /* Switch to current obstack CURRENT and maybepermanent obstack SAVEABLE,
416 recording the previously current obstacks on a stack.
417 This does not free any storage in any obstack. */
419 void
420 push_obstacks (current, saveable)
421 struct obstack *current, *saveable;
423 struct obstack_stack *p
424 = (struct obstack_stack *) obstack_alloc (&obstack_stack_obstack,
425 (sizeof (struct obstack_stack)));
427 p->current = current_obstack;
428 p->saveable = saveable_obstack;
429 p->expression = expression_obstack;
430 p->rtl = rtl_obstack;
431 p->next = obstack_stack;
432 obstack_stack = p;
434 current_obstack = current;
435 expression_obstack = current;
436 rtl_obstack = saveable_obstack = saveable;
439 /* Save the current set of obstacks, but don't change them. */
441 void
442 push_obstacks_nochange ()
444 struct obstack_stack *p
445 = (struct obstack_stack *) obstack_alloc (&obstack_stack_obstack,
446 (sizeof (struct obstack_stack)));
448 p->current = current_obstack;
449 p->saveable = saveable_obstack;
450 p->expression = expression_obstack;
451 p->rtl = rtl_obstack;
452 p->next = obstack_stack;
453 obstack_stack = p;
456 /* Pop the obstack selection stack. */
458 void
459 pop_obstacks ()
461 struct obstack_stack *p = obstack_stack;
462 obstack_stack = p->next;
464 current_obstack = p->current;
465 saveable_obstack = p->saveable;
466 expression_obstack = p->expression;
467 rtl_obstack = p->rtl;
469 obstack_free (&obstack_stack_obstack, p);
472 /* Nonzero if temporary allocation is currently in effect.
473 Zero if currently doing permanent allocation. */
476 allocation_temporary_p ()
478 return current_obstack != &permanent_obstack;
481 /* Go back to allocating on the permanent obstack
482 and free everything in the temporary obstack.
483 This is done in finish_function after fully compiling a function. */
485 void
486 permanent_allocation ()
488 /* Free up previous temporary obstack data */
489 obstack_free (&temporary_obstack, temporary_firstobj);
490 obstack_free (&momentary_obstack, momentary_firstobj);
491 obstack_free (&maybepermanent_obstack, maybepermanent_firstobj);
492 obstack_free (&temp_decl_obstack, temp_decl_firstobj);
494 current_obstack = &permanent_obstack;
495 expression_obstack = &permanent_obstack;
496 rtl_obstack = saveable_obstack = &permanent_obstack;
499 /* Save permanently everything on the maybepermanent_obstack. */
501 void
502 preserve_data ()
504 maybepermanent_firstobj
505 = (char *) obstack_alloc (function_maybepermanent_obstack, 0);
508 void
509 preserve_initializer ()
511 temporary_firstobj
512 = (char *) obstack_alloc (&temporary_obstack, 0);
513 momentary_firstobj
514 = (char *) obstack_alloc (&momentary_obstack, 0);
515 maybepermanent_firstobj
516 = (char *) obstack_alloc (function_maybepermanent_obstack, 0);
519 /* Start allocating new rtl in current_obstack.
520 Use resume_temporary_allocation
521 to go back to allocating rtl in saveable_obstack. */
523 void
524 rtl_in_current_obstack ()
526 rtl_obstack = current_obstack;
529 /* Start allocating rtl from saveable_obstack. Intended to be used after
530 a call to push_obstacks_nochange. */
532 void
533 rtl_in_saveable_obstack ()
535 rtl_obstack = saveable_obstack;
538 /* Allocate SIZE bytes in the current obstack
539 and return a pointer to them.
540 In practice the current obstack is always the temporary one. */
542 char *
543 oballoc (size)
544 int size;
546 return (char *) obstack_alloc (current_obstack, size);
549 /* Free the object PTR in the current obstack
550 as well as everything allocated since PTR.
551 In practice the current obstack is always the temporary one. */
553 void
554 obfree (ptr)
555 char *ptr;
557 obstack_free (current_obstack, ptr);
560 /* Allocate SIZE bytes in the permanent obstack
561 and return a pointer to them. */
563 char *
564 permalloc (size)
565 int size;
567 return (char *) obstack_alloc (&permanent_obstack, size);
570 /* Allocate NELEM items of SIZE bytes in the permanent obstack
571 and return a pointer to them. The storage is cleared before
572 returning the value. */
574 char *
575 perm_calloc (nelem, size)
576 int nelem;
577 long size;
579 char *rval = (char *) obstack_alloc (&permanent_obstack, nelem * size);
580 bzero (rval, nelem * size);
581 return rval;
584 /* Allocate SIZE bytes in the saveable obstack
585 and return a pointer to them. */
587 char *
588 savealloc (size)
589 int size;
591 return (char *) obstack_alloc (saveable_obstack, size);
594 /* Print out which obstack an object is in. */
596 void
597 print_obstack_name (object, file, prefix)
598 char *object;
599 FILE *file;
600 char *prefix;
602 struct obstack *obstack = NULL;
603 char *obstack_name = NULL;
604 struct function *p;
606 for (p = outer_function_chain; p; p = p->next)
608 if (_obstack_allocated_p (p->function_obstack, object))
610 obstack = p->function_obstack;
611 obstack_name = "containing function obstack";
613 if (_obstack_allocated_p (p->function_maybepermanent_obstack, object))
615 obstack = p->function_maybepermanent_obstack;
616 obstack_name = "containing function maybepermanent obstack";
620 if (_obstack_allocated_p (&obstack_stack_obstack, object))
622 obstack = &obstack_stack_obstack;
623 obstack_name = "obstack_stack_obstack";
625 else if (_obstack_allocated_p (function_obstack, object))
627 obstack = function_obstack;
628 obstack_name = "function obstack";
630 else if (_obstack_allocated_p (&permanent_obstack, object))
632 obstack = &permanent_obstack;
633 obstack_name = "permanent_obstack";
635 else if (_obstack_allocated_p (&momentary_obstack, object))
637 obstack = &momentary_obstack;
638 obstack_name = "momentary_obstack";
640 else if (_obstack_allocated_p (function_maybepermanent_obstack, object))
642 obstack = function_maybepermanent_obstack;
643 obstack_name = "function maybepermanent obstack";
645 else if (_obstack_allocated_p (&temp_decl_obstack, object))
647 obstack = &temp_decl_obstack;
648 obstack_name = "temp_decl_obstack";
651 /* Check to see if the object is in the free area of the obstack. */
652 if (obstack != NULL)
654 if (object >= obstack->next_free
655 && object < obstack->chunk_limit)
656 fprintf (file, "%s in free portion of obstack %s",
657 prefix, obstack_name);
658 else
659 fprintf (file, "%s allocated from %s", prefix, obstack_name);
661 else
662 fprintf (file, "%s not allocated from any obstack", prefix);
665 void
666 debug_obstack (object)
667 char *object;
669 print_obstack_name (object, stderr, "object");
670 fprintf (stderr, ".\n");
673 /* Return 1 if OBJ is in the permanent obstack.
674 This is slow, and should be used only for debugging.
675 Use TREE_PERMANENT for other purposes. */
678 object_permanent_p (obj)
679 tree obj;
681 return _obstack_allocated_p (&permanent_obstack, obj);
684 /* Start a level of momentary allocation.
685 In C, each compound statement has its own level
686 and that level is freed at the end of each statement.
687 All expression nodes are allocated in the momentary allocation level. */
689 void
690 push_momentary ()
692 struct momentary_level *tem
693 = (struct momentary_level *) obstack_alloc (&momentary_obstack,
694 sizeof (struct momentary_level));
695 tem->prev = momentary_stack;
696 tem->base = (char *) obstack_base (&momentary_obstack);
697 tem->obstack = expression_obstack;
698 momentary_stack = tem;
699 expression_obstack = &momentary_obstack;
702 /* Free all the storage in the current momentary-allocation level.
703 In C, this happens at the end of each statement. */
705 void
706 clear_momentary ()
708 obstack_free (&momentary_obstack, momentary_stack->base);
711 /* Discard a level of momentary allocation.
712 In C, this happens at the end of each compound statement.
713 Restore the status of expression node allocation
714 that was in effect before this level was created. */
716 void
717 pop_momentary ()
719 struct momentary_level *tem = momentary_stack;
720 momentary_stack = tem->prev;
721 expression_obstack = tem->obstack;
722 obstack_free (&momentary_obstack, tem);
725 /* Call when starting to parse a declaration:
726 make expressions in the declaration last the length of the function.
727 Returns an argument that should be passed to resume_momentary later. */
730 suspend_momentary ()
732 register int tem = expression_obstack == &momentary_obstack;
733 expression_obstack = saveable_obstack;
734 return tem;
737 /* Call when finished parsing a declaration:
738 restore the treatment of node-allocation that was
739 in effect before the suspension.
740 YES should be the value previously returned by suspend_momentary. */
742 void
743 resume_momentary (yes)
744 int yes;
746 if (yes)
747 expression_obstack = &momentary_obstack;
750 /* Init the tables indexed by tree code.
751 Note that languages can add to these tables to define their own codes. */
753 void
754 init_tree_codes ()
756 tree_code_type = (char **) xmalloc (sizeof (standard_tree_code_type));
757 tree_code_length = (int *) xmalloc (sizeof (standard_tree_code_length));
758 tree_code_name = (char **) xmalloc (sizeof (standard_tree_code_name));
759 bcopy (standard_tree_code_type, tree_code_type,
760 sizeof (standard_tree_code_type));
761 bcopy (standard_tree_code_length, tree_code_length,
762 sizeof (standard_tree_code_length));
763 bcopy (standard_tree_code_name, tree_code_name,
764 sizeof (standard_tree_code_name));
767 /* Return a newly allocated node of code CODE.
768 Initialize the node's unique id and its TREE_PERMANENT flag.
769 For decl and type nodes, some other fields are initialized.
770 The rest of the node is initialized to zero.
772 Achoo! I got a code in the node. */
774 tree
775 make_node (code)
776 enum tree_code code;
778 register tree t;
779 register int type = TREE_CODE_CLASS (code);
780 register int length;
781 register struct obstack *obstack = current_obstack;
782 register int i;
783 register tree_node_kind kind;
785 switch (type)
787 case 'd': /* A decl node */
788 #ifdef GATHER_STATISTICS
789 kind = d_kind;
790 #endif
791 length = sizeof (struct tree_decl);
792 /* All decls in an inline function need to be saved. */
793 if (obstack != &permanent_obstack)
794 obstack = saveable_obstack;
795 /* PARM_DECLs always go on saveable_obstack, not permanent,
796 even though we may make them before the function turns
797 on temporary allocation. */
798 else if (code == PARM_DECL)
800 tree context = 0;
801 if (current_function_decl)
802 context = decl_function_context (current_function_decl);
803 /* If this is a nested function, then we must allocate the PARM_DECL
804 on the parent's saveable_obstack, so that they will live to the
805 end of the parent's closing brace. This is neccesary in case we
806 try to inline the function into its parent. */
807 if (context)
809 struct function *p = find_function_data (context);
810 obstack = p->function_maybepermanent_obstack;
812 else
813 obstack = function_maybepermanent_obstack;
815 break;
817 case 't': /* a type node */
818 #ifdef GATHER_STATISTICS
819 kind = t_kind;
820 #endif
821 length = sizeof (struct tree_type);
822 /* All data types are put where we can preserve them if nec. */
823 if (obstack != &permanent_obstack)
824 obstack = all_types_permanent ? &permanent_obstack : saveable_obstack;
825 break;
827 case 'b': /* a lexical block */
828 #ifdef GATHER_STATISTICS
829 kind = b_kind;
830 #endif
831 length = sizeof (struct tree_block);
832 /* All BLOCK nodes are put where we can preserve them if nec. */
833 if (obstack != &permanent_obstack)
834 obstack = saveable_obstack;
835 break;
837 case 's': /* an expression with side effects */
838 #ifdef GATHER_STATISTICS
839 kind = s_kind;
840 goto usual_kind;
841 #endif
842 case 'r': /* a reference */
843 #ifdef GATHER_STATISTICS
844 kind = r_kind;
845 goto usual_kind;
846 #endif
847 case 'e': /* an expression */
848 case '<': /* a comparison expression */
849 case '1': /* a unary arithmetic expression */
850 case '2': /* a binary arithmetic expression */
851 #ifdef GATHER_STATISTICS
852 kind = e_kind;
853 usual_kind:
854 #endif
855 obstack = expression_obstack;
856 /* All BIND_EXPR nodes are put where we can preserve them if nec. */
857 if (code == BIND_EXPR && obstack != &permanent_obstack)
858 obstack = saveable_obstack;
859 length = sizeof (struct tree_exp)
860 + (tree_code_length[(int) code] - 1) * sizeof (char *);
861 break;
863 case 'c': /* a constant */
864 #ifdef GATHER_STATISTICS
865 kind = c_kind;
866 #endif
867 obstack = expression_obstack;
869 /* We can't use tree_code_length for INTEGER_CST, since the number of
870 words is machine-dependent due to varying length of HOST_WIDE_INT,
871 which might be wider than a pointer (e.g., long long). Similarly
872 for REAL_CST, since the number of words is machine-dependent due
873 to varying size and alignment of `double'. */
875 if (code == INTEGER_CST)
876 length = sizeof (struct tree_int_cst);
877 else if (code == REAL_CST)
878 length = sizeof (struct tree_real_cst);
879 else
880 length = sizeof (struct tree_common)
881 + tree_code_length[(int) code] * sizeof (char *);
882 break;
884 case 'x': /* something random, like an identifier. */
885 #ifdef GATHER_STATISTICS
886 if (code == IDENTIFIER_NODE)
887 kind = id_kind;
888 else if (code == OP_IDENTIFIER)
889 kind = op_id_kind;
890 else if (code == TREE_VEC)
891 kind = vec_kind;
892 else
893 kind = x_kind;
894 #endif
895 length = sizeof (struct tree_common)
896 + tree_code_length[(int) code] * sizeof (char *);
897 /* Identifier nodes are always permanent since they are
898 unique in a compiler run. */
899 if (code == IDENTIFIER_NODE) obstack = &permanent_obstack;
902 t = (tree) obstack_alloc (obstack, length);
904 #ifdef GATHER_STATISTICS
905 tree_node_counts[(int)kind]++;
906 tree_node_sizes[(int)kind] += length;
907 #endif
909 /* Clear a word at a time. */
910 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
911 ((int *) t)[i] = 0;
912 /* Clear any extra bytes. */
913 for (i = length / sizeof (int) * sizeof (int); i < length; i++)
914 ((char *) t)[i] = 0;
916 TREE_SET_CODE (t, code);
917 if (obstack == &permanent_obstack)
918 TREE_PERMANENT (t) = 1;
920 switch (type)
922 case 's':
923 TREE_SIDE_EFFECTS (t) = 1;
924 TREE_TYPE (t) = void_type_node;
925 break;
927 case 'd':
928 if (code != FUNCTION_DECL)
929 DECL_ALIGN (t) = 1;
930 DECL_IN_SYSTEM_HEADER (t)
931 = in_system_header && (obstack == &permanent_obstack);
932 DECL_SOURCE_LINE (t) = lineno;
933 DECL_SOURCE_FILE (t) = (input_filename) ? input_filename : "<built-in>";
934 DECL_UID (t) = next_decl_uid++;
935 break;
937 case 't':
938 TYPE_UID (t) = next_type_uid++;
939 TYPE_ALIGN (t) = 1;
940 TYPE_MAIN_VARIANT (t) = t;
941 TYPE_OBSTACK (t) = obstack;
942 break;
944 case 'c':
945 TREE_CONSTANT (t) = 1;
946 break;
949 return t;
952 /* Return a new node with the same contents as NODE
953 except that its TREE_CHAIN is zero and it has a fresh uid. */
955 tree
956 copy_node (node)
957 tree node;
959 register tree t;
960 register enum tree_code code = TREE_CODE (node);
961 register int length;
962 register int i;
964 switch (TREE_CODE_CLASS (code))
966 case 'd': /* A decl node */
967 length = sizeof (struct tree_decl);
968 break;
970 case 't': /* a type node */
971 length = sizeof (struct tree_type);
972 break;
974 case 'b': /* a lexical block node */
975 length = sizeof (struct tree_block);
976 break;
978 case 'r': /* a reference */
979 case 'e': /* an expression */
980 case 's': /* an expression with side effects */
981 case '<': /* a comparison expression */
982 case '1': /* a unary arithmetic expression */
983 case '2': /* a binary arithmetic expression */
984 length = sizeof (struct tree_exp)
985 + (tree_code_length[(int) code] - 1) * sizeof (char *);
986 break;
988 case 'c': /* a constant */
989 /* We can't use tree_code_length for this, since the number of words
990 is machine-dependent due to varying alignment of `double'. */
991 if (code == REAL_CST)
993 length = sizeof (struct tree_real_cst);
994 break;
997 case 'x': /* something random, like an identifier. */
998 length = sizeof (struct tree_common)
999 + tree_code_length[(int) code] * sizeof (char *);
1000 if (code == TREE_VEC)
1001 length += (TREE_VEC_LENGTH (node) - 1) * sizeof (char *);
1004 t = (tree) obstack_alloc (current_obstack, length);
1006 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
1007 ((int *) t)[i] = ((int *) node)[i];
1008 /* Clear any extra bytes. */
1009 for (i = length / sizeof (int) * sizeof (int); i < length; i++)
1010 ((char *) t)[i] = ((char *) node)[i];
1012 TREE_CHAIN (t) = 0;
1014 if (TREE_CODE_CLASS (code) == 'd')
1015 DECL_UID (t) = next_decl_uid++;
1016 else if (TREE_CODE_CLASS (code) == 't')
1018 TYPE_UID (t) = next_type_uid++;
1019 TYPE_OBSTACK (t) = current_obstack;
1022 TREE_PERMANENT (t) = (current_obstack == &permanent_obstack);
1024 return t;
1027 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
1028 For example, this can copy a list made of TREE_LIST nodes. */
1030 tree
1031 copy_list (list)
1032 tree list;
1034 tree head;
1035 register tree prev, next;
1037 if (list == 0)
1038 return 0;
1040 head = prev = copy_node (list);
1041 next = TREE_CHAIN (list);
1042 while (next)
1044 TREE_CHAIN (prev) = copy_node (next);
1045 prev = TREE_CHAIN (prev);
1046 next = TREE_CHAIN (next);
1048 return head;
1051 #define HASHBITS 30
1053 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
1054 If an identifier with that name has previously been referred to,
1055 the same node is returned this time. */
1057 tree
1058 get_identifier (text)
1059 register char *text;
1061 register int hi;
1062 register int i;
1063 register tree idp;
1064 register int len, hash_len;
1066 /* Compute length of text in len. */
1067 for (len = 0; text[len]; len++);
1069 /* Decide how much of that length to hash on */
1070 hash_len = len;
1071 if (warn_id_clash && len > id_clash_len)
1072 hash_len = id_clash_len;
1074 /* Compute hash code */
1075 hi = hash_len * 613 + (unsigned)text[0];
1076 for (i = 1; i < hash_len; i += 2)
1077 hi = ((hi * 613) + (unsigned)(text[i]));
1079 hi &= (1 << HASHBITS) - 1;
1080 hi %= MAX_HASH_TABLE;
1082 /* Search table for identifier */
1083 for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
1084 if (IDENTIFIER_LENGTH (idp) == len
1085 && IDENTIFIER_POINTER (idp)[0] == text[0]
1086 && !bcmp (IDENTIFIER_POINTER (idp), text, len))
1087 return idp; /* <-- return if found */
1089 /* Not found; optionally warn about a similar identifier */
1090 if (warn_id_clash && do_identifier_warnings && len >= id_clash_len)
1091 for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
1092 if (!strncmp (IDENTIFIER_POINTER (idp), text, id_clash_len))
1094 warning ("`%s' and `%s' identical in first %d characters",
1095 IDENTIFIER_POINTER (idp), text, id_clash_len);
1096 break;
1099 if (tree_code_length[(int) IDENTIFIER_NODE] < 0)
1100 abort (); /* set_identifier_size hasn't been called. */
1102 /* Not found, create one, add to chain */
1103 idp = make_node (IDENTIFIER_NODE);
1104 IDENTIFIER_LENGTH (idp) = len;
1105 #ifdef GATHER_STATISTICS
1106 id_string_size += len;
1107 #endif
1109 IDENTIFIER_POINTER (idp) = obstack_copy0 (&permanent_obstack, text, len);
1111 TREE_CHAIN (idp) = hash_table[hi];
1112 hash_table[hi] = idp;
1113 return idp; /* <-- return if created */
1116 /* Enable warnings on similar identifiers (if requested).
1117 Done after the built-in identifiers are created. */
1119 void
1120 start_identifier_warnings ()
1122 do_identifier_warnings = 1;
1125 /* Record the size of an identifier node for the language in use.
1126 SIZE is the total size in bytes.
1127 This is called by the language-specific files. This must be
1128 called before allocating any identifiers. */
1130 void
1131 set_identifier_size (size)
1132 int size;
1134 tree_code_length[(int) IDENTIFIER_NODE]
1135 = (size - sizeof (struct tree_common)) / sizeof (tree);
1138 /* Return a newly constructed INTEGER_CST node whose constant value
1139 is specified by the two ints LOW and HI.
1140 The TREE_TYPE is set to `int'.
1142 This function should be used via the `build_int_2' macro. */
1144 tree
1145 build_int_2_wide (low, hi)
1146 HOST_WIDE_INT low, hi;
1148 register tree t = make_node (INTEGER_CST);
1149 TREE_INT_CST_LOW (t) = low;
1150 TREE_INT_CST_HIGH (t) = hi;
1151 TREE_TYPE (t) = integer_type_node;
1152 return t;
1155 /* Return a new REAL_CST node whose type is TYPE and value is D. */
1157 tree
1158 build_real (type, d)
1159 tree type;
1160 REAL_VALUE_TYPE d;
1162 tree v;
1164 /* Check for valid float value for this type on this target machine;
1165 if not, can print error message and store a valid value in D. */
1166 #ifdef CHECK_FLOAT_VALUE
1167 CHECK_FLOAT_VALUE (TYPE_MODE (type), d);
1168 #endif
1170 v = make_node (REAL_CST);
1171 TREE_TYPE (v) = type;
1172 TREE_REAL_CST (v) = d;
1173 return v;
1176 /* Return a new REAL_CST node whose type is TYPE
1177 and whose value is the integer value of the INTEGER_CST node I. */
1179 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
1181 REAL_VALUE_TYPE
1182 real_value_from_int_cst (i)
1183 tree i;
1185 REAL_VALUE_TYPE d;
1186 REAL_VALUE_TYPE e;
1187 /* Some 386 compilers mishandle unsigned int to float conversions,
1188 so introduce a temporary variable E to avoid those bugs. */
1190 #ifdef REAL_ARITHMETIC
1191 if (! TREE_UNSIGNED (TREE_TYPE (i)))
1192 REAL_VALUE_FROM_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i));
1193 else
1194 REAL_VALUE_FROM_UNSIGNED_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i));
1195 #else /* not REAL_ARITHMETIC */
1196 if (TREE_INT_CST_HIGH (i) < 0 && ! TREE_UNSIGNED (TREE_TYPE (i)))
1198 d = (double) (~ TREE_INT_CST_HIGH (i));
1199 e = ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
1200 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
1201 d *= e;
1202 e = (double) (unsigned HOST_WIDE_INT) (~ TREE_INT_CST_LOW (i));
1203 d += e;
1204 d = (- d - 1.0);
1206 else
1208 d = (double) (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (i);
1209 e = ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
1210 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
1211 d *= e;
1212 e = (double) (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (i);
1213 d += e;
1215 #endif /* not REAL_ARITHMETIC */
1216 return d;
1219 /* This function can't be implemented if we can't do arithmetic
1220 on the float representation. */
1222 tree
1223 build_real_from_int_cst (type, i)
1224 tree type;
1225 tree i;
1227 tree v;
1228 REAL_VALUE_TYPE d;
1230 v = make_node (REAL_CST);
1231 TREE_TYPE (v) = type;
1233 d = REAL_VALUE_TRUNCATE (TYPE_MODE (type), real_value_from_int_cst (i));
1234 /* Check for valid float value for this type on this target machine;
1235 if not, can print error message and store a valid value in D. */
1236 #ifdef CHECK_FLOAT_VALUE
1237 CHECK_FLOAT_VALUE (TYPE_MODE (type), d);
1238 #endif
1240 TREE_REAL_CST (v) = d;
1241 return v;
1244 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
1246 /* Return a newly constructed STRING_CST node whose value is
1247 the LEN characters at STR.
1248 The TREE_TYPE is not initialized. */
1250 tree
1251 build_string (len, str)
1252 int len;
1253 char *str;
1255 register tree s = make_node (STRING_CST);
1256 TREE_STRING_LENGTH (s) = len;
1257 TREE_STRING_POINTER (s) = obstack_copy0 (expression_obstack, str, len);
1258 return s;
1261 /* Return a newly constructed COMPLEX_CST node whose value is
1262 specified by the real and imaginary parts REAL and IMAG.
1263 Both REAL and IMAG should be constant nodes.
1264 The TREE_TYPE is not initialized. */
1266 tree
1267 build_complex (real, imag)
1268 tree real, imag;
1270 register tree t = make_node (COMPLEX_CST);
1271 TREE_REALPART (t) = real;
1272 TREE_IMAGPART (t) = imag;
1273 TREE_TYPE (t) = build_complex_type (TREE_TYPE (real));
1274 return t;
1277 /* Build a newly constructed TREE_VEC node of length LEN. */
1278 tree
1279 make_tree_vec (len)
1280 int len;
1282 register tree t;
1283 register int length = (len-1) * sizeof (tree) + sizeof (struct tree_vec);
1284 register struct obstack *obstack = current_obstack;
1285 register int i;
1287 #ifdef GATHER_STATISTICS
1288 tree_node_counts[(int)vec_kind]++;
1289 tree_node_sizes[(int)vec_kind] += length;
1290 #endif
1292 t = (tree) obstack_alloc (obstack, length);
1294 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
1295 ((int *) t)[i] = 0;
1297 TREE_SET_CODE (t, TREE_VEC);
1298 TREE_VEC_LENGTH (t) = len;
1299 if (obstack == &permanent_obstack)
1300 TREE_PERMANENT (t) = 1;
1302 return t;
1305 /* Return 1 if EXPR is the integer constant zero. */
1308 integer_zerop (expr)
1309 tree expr;
1311 STRIP_NOPS (expr);
1313 return (TREE_CODE (expr) == INTEGER_CST
1314 && TREE_INT_CST_LOW (expr) == 0
1315 && TREE_INT_CST_HIGH (expr) == 0);
1318 /* Return 1 if EXPR is the integer constant one. */
1321 integer_onep (expr)
1322 tree expr;
1324 STRIP_NOPS (expr);
1326 return (TREE_CODE (expr) == INTEGER_CST
1327 && TREE_INT_CST_LOW (expr) == 1
1328 && TREE_INT_CST_HIGH (expr) == 0);
1331 /* Return 1 if EXPR is an integer containing all 1's
1332 in as much precision as it contains. */
1335 integer_all_onesp (expr)
1336 tree expr;
1338 register int prec;
1339 register int uns;
1341 STRIP_NOPS (expr);
1343 if (TREE_CODE (expr) != INTEGER_CST)
1344 return 0;
1346 uns = TREE_UNSIGNED (TREE_TYPE (expr));
1347 if (!uns)
1348 return TREE_INT_CST_LOW (expr) == -1 && TREE_INT_CST_HIGH (expr) == -1;
1350 prec = TYPE_PRECISION (TREE_TYPE (expr));
1351 if (prec >= HOST_BITS_PER_WIDE_INT)
1353 int high_value, shift_amount;
1355 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
1357 if (shift_amount > HOST_BITS_PER_WIDE_INT)
1358 /* Can not handle precisions greater than twice the host int size. */
1359 abort ();
1360 else if (shift_amount == HOST_BITS_PER_WIDE_INT)
1361 /* Shifting by the host word size is undefined according to the ANSI
1362 standard, so we must handle this as a special case. */
1363 high_value = -1;
1364 else
1365 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
1367 return TREE_INT_CST_LOW (expr) == -1
1368 && TREE_INT_CST_HIGH (expr) == high_value;
1370 else
1371 return TREE_INT_CST_LOW (expr) == ((HOST_WIDE_INT) 1 << prec) - 1;
1374 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
1375 one bit on). */
1378 integer_pow2p (expr)
1379 tree expr;
1381 HOST_WIDE_INT high, low;
1383 STRIP_NOPS (expr);
1385 if (TREE_CODE (expr) != INTEGER_CST)
1386 return 0;
1388 high = TREE_INT_CST_HIGH (expr);
1389 low = TREE_INT_CST_LOW (expr);
1391 if (high == 0 && low == 0)
1392 return 0;
1394 return ((high == 0 && (low & (low - 1)) == 0)
1395 || (low == 0 && (high & (high - 1)) == 0));
1398 /* Return 1 if EXPR is the real constant zero. */
1401 real_zerop (expr)
1402 tree expr;
1404 STRIP_NOPS (expr);
1406 return (TREE_CODE (expr) == REAL_CST
1407 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0));
1410 /* Return 1 if EXPR is the real constant one. */
1413 real_onep (expr)
1414 tree expr;
1416 STRIP_NOPS (expr);
1418 return (TREE_CODE (expr) == REAL_CST
1419 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1));
1422 /* Return 1 if EXPR is the real constant two. */
1425 real_twop (expr)
1426 tree expr;
1428 STRIP_NOPS (expr);
1430 return (TREE_CODE (expr) == REAL_CST
1431 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2));
1434 /* Nonzero if EXP is a constant or a cast of a constant. */
1437 really_constant_p (exp)
1438 tree exp;
1440 /* This is not quite the same as STRIP_NOPS. It does more. */
1441 while (TREE_CODE (exp) == NOP_EXPR
1442 || TREE_CODE (exp) == CONVERT_EXPR
1443 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1444 exp = TREE_OPERAND (exp, 0);
1445 return TREE_CONSTANT (exp);
1448 /* Return first list element whose TREE_VALUE is ELEM.
1449 Return 0 if ELEM is not it LIST. */
1451 tree
1452 value_member (elem, list)
1453 tree elem, list;
1455 while (list)
1457 if (elem == TREE_VALUE (list))
1458 return list;
1459 list = TREE_CHAIN (list);
1461 return NULL_TREE;
1464 /* Return first list element whose TREE_PURPOSE is ELEM.
1465 Return 0 if ELEM is not it LIST. */
1467 tree
1468 purpose_member (elem, list)
1469 tree elem, list;
1471 while (list)
1473 if (elem == TREE_PURPOSE (list))
1474 return list;
1475 list = TREE_CHAIN (list);
1477 return NULL_TREE;
1480 /* Return first list element whose BINFO_TYPE is ELEM.
1481 Return 0 if ELEM is not it LIST. */
1483 tree
1484 binfo_member (elem, list)
1485 tree elem, list;
1487 while (list)
1489 if (elem == BINFO_TYPE (list))
1490 return list;
1491 list = TREE_CHAIN (list);
1493 return NULL_TREE;
1496 /* Return nonzero if ELEM is part of the chain CHAIN. */
1499 chain_member (elem, chain)
1500 tree elem, chain;
1502 while (chain)
1504 if (elem == chain)
1505 return 1;
1506 chain = TREE_CHAIN (chain);
1509 return 0;
1512 /* Return the length of a chain of nodes chained through TREE_CHAIN.
1513 We expect a null pointer to mark the end of the chain.
1514 This is the Lisp primitive `length'. */
1517 list_length (t)
1518 tree t;
1520 register tree tail;
1521 register int len = 0;
1523 for (tail = t; tail; tail = TREE_CHAIN (tail))
1524 len++;
1526 return len;
1529 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
1530 by modifying the last node in chain 1 to point to chain 2.
1531 This is the Lisp primitive `nconc'. */
1533 tree
1534 chainon (op1, op2)
1535 tree op1, op2;
1537 tree t;
1539 if (op1)
1541 for (t = op1; TREE_CHAIN (t); t = TREE_CHAIN (t))
1542 if (t == op2) abort (); /* Circularity being created */
1543 if (t == op2) abort (); /* Circularity being created */
1544 TREE_CHAIN (t) = op2;
1545 return op1;
1547 else return op2;
1550 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
1552 tree
1553 tree_last (chain)
1554 register tree chain;
1556 register tree next;
1557 if (chain)
1558 while (next = TREE_CHAIN (chain))
1559 chain = next;
1560 return chain;
1563 /* Reverse the order of elements in the chain T,
1564 and return the new head of the chain (old last element). */
1566 tree
1567 nreverse (t)
1568 tree t;
1570 register tree prev = 0, decl, next;
1571 for (decl = t; decl; decl = next)
1573 next = TREE_CHAIN (decl);
1574 TREE_CHAIN (decl) = prev;
1575 prev = decl;
1577 return prev;
1580 /* Given a chain CHAIN of tree nodes,
1581 construct and return a list of those nodes. */
1583 tree
1584 listify (chain)
1585 tree chain;
1587 tree result = NULL_TREE;
1588 tree in_tail = chain;
1589 tree out_tail = NULL_TREE;
1591 while (in_tail)
1593 tree next = tree_cons (NULL_TREE, in_tail, NULL_TREE);
1594 if (out_tail)
1595 TREE_CHAIN (out_tail) = next;
1596 else
1597 result = next;
1598 out_tail = next;
1599 in_tail = TREE_CHAIN (in_tail);
1602 return result;
1605 /* Return a newly created TREE_LIST node whose
1606 purpose and value fields are PARM and VALUE. */
1608 tree
1609 build_tree_list (parm, value)
1610 tree parm, value;
1612 register tree t = make_node (TREE_LIST);
1613 TREE_PURPOSE (t) = parm;
1614 TREE_VALUE (t) = value;
1615 return t;
1618 /* Similar, but build on the temp_decl_obstack. */
1620 tree
1621 build_decl_list (parm, value)
1622 tree parm, value;
1624 register tree node;
1625 register struct obstack *ambient_obstack = current_obstack;
1626 current_obstack = &temp_decl_obstack;
1627 node = build_tree_list (parm, value);
1628 current_obstack = ambient_obstack;
1629 return node;
1632 /* Return a newly created TREE_LIST node whose
1633 purpose and value fields are PARM and VALUE
1634 and whose TREE_CHAIN is CHAIN. */
1636 tree
1637 tree_cons (purpose, value, chain)
1638 tree purpose, value, chain;
1640 #if 0
1641 register tree node = make_node (TREE_LIST);
1642 #else
1643 register int i;
1644 register tree node = (tree) obstack_alloc (current_obstack, sizeof (struct tree_list));
1645 #ifdef GATHER_STATISTICS
1646 tree_node_counts[(int)x_kind]++;
1647 tree_node_sizes[(int)x_kind] += sizeof (struct tree_list);
1648 #endif
1650 for (i = (sizeof (struct tree_common) / sizeof (int)) - 1; i >= 0; i--)
1651 ((int *) node)[i] = 0;
1653 TREE_SET_CODE (node, TREE_LIST);
1654 if (current_obstack == &permanent_obstack)
1655 TREE_PERMANENT (node) = 1;
1656 #endif
1658 TREE_CHAIN (node) = chain;
1659 TREE_PURPOSE (node) = purpose;
1660 TREE_VALUE (node) = value;
1661 return node;
1664 /* Similar, but build on the temp_decl_obstack. */
1666 tree
1667 decl_tree_cons (purpose, value, chain)
1668 tree purpose, value, chain;
1670 register tree node;
1671 register struct obstack *ambient_obstack = current_obstack;
1672 current_obstack = &temp_decl_obstack;
1673 node = tree_cons (purpose, value, chain);
1674 current_obstack = ambient_obstack;
1675 return node;
1678 /* Same as `tree_cons' but make a permanent object. */
1680 tree
1681 perm_tree_cons (purpose, value, chain)
1682 tree purpose, value, chain;
1684 register tree node;
1685 register struct obstack *ambient_obstack = current_obstack;
1686 current_obstack = &permanent_obstack;
1688 node = tree_cons (purpose, value, chain);
1689 current_obstack = ambient_obstack;
1690 return node;
1693 /* Same as `tree_cons', but make this node temporary, regardless. */
1695 tree
1696 temp_tree_cons (purpose, value, chain)
1697 tree purpose, value, chain;
1699 register tree node;
1700 register struct obstack *ambient_obstack = current_obstack;
1701 current_obstack = &temporary_obstack;
1703 node = tree_cons (purpose, value, chain);
1704 current_obstack = ambient_obstack;
1705 return node;
1708 /* Same as `tree_cons', but save this node if the function's RTL is saved. */
1710 tree
1711 saveable_tree_cons (purpose, value, chain)
1712 tree purpose, value, chain;
1714 register tree node;
1715 register struct obstack *ambient_obstack = current_obstack;
1716 current_obstack = saveable_obstack;
1718 node = tree_cons (purpose, value, chain);
1719 current_obstack = ambient_obstack;
1720 return node;
1723 /* Return the size nominally occupied by an object of type TYPE
1724 when it resides in memory. The value is measured in units of bytes,
1725 and its data type is that normally used for type sizes
1726 (which is the first type created by make_signed_type or
1727 make_unsigned_type). */
1729 tree
1730 size_in_bytes (type)
1731 tree type;
1733 tree t;
1735 if (type == error_mark_node)
1736 return integer_zero_node;
1737 type = TYPE_MAIN_VARIANT (type);
1738 if (TYPE_SIZE (type) == 0)
1740 incomplete_type_error (NULL_TREE, type);
1741 return integer_zero_node;
1743 t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1744 size_int (BITS_PER_UNIT));
1745 if (TREE_CODE (t) == INTEGER_CST)
1746 force_fit_type (t, 0);
1747 return t;
1750 /* Return the size of TYPE (in bytes) as an integer,
1751 or return -1 if the size can vary. */
1754 int_size_in_bytes (type)
1755 tree type;
1757 unsigned int size;
1758 if (type == error_mark_node)
1759 return 0;
1760 type = TYPE_MAIN_VARIANT (type);
1761 if (TYPE_SIZE (type) == 0)
1762 return -1;
1763 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1764 return -1;
1765 if (TREE_INT_CST_HIGH (TYPE_SIZE (type)) != 0)
1767 tree t = size_binop (CEIL_DIV_EXPR, TYPE_SIZE (type),
1768 size_int (BITS_PER_UNIT));
1769 return TREE_INT_CST_LOW (t);
1771 size = TREE_INT_CST_LOW (TYPE_SIZE (type));
1772 return (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1775 /* Return, as a tree node, the number of elements for TYPE (which is an
1776 ARRAY_TYPE) minus one. This counts only elements of the top array. */
1778 tree
1779 array_type_nelts (type)
1780 tree type;
1782 tree index_type = TYPE_DOMAIN (type);
1784 return (integer_zerop (TYPE_MIN_VALUE (index_type))
1785 ? TYPE_MAX_VALUE (index_type)
1786 : fold (build (MINUS_EXPR, TREE_TYPE (TYPE_MAX_VALUE (index_type)),
1787 TYPE_MAX_VALUE (index_type),
1788 TYPE_MIN_VALUE (index_type))));
1791 /* Return nonzero if arg is static -- a reference to an object in
1792 static storage. This is not the same as the C meaning of `static'. */
1795 staticp (arg)
1796 tree arg;
1798 switch (TREE_CODE (arg))
1800 case VAR_DECL:
1801 case FUNCTION_DECL:
1802 case CONSTRUCTOR:
1803 return TREE_STATIC (arg) || DECL_EXTERNAL (arg);
1805 case STRING_CST:
1806 return 1;
1808 case COMPONENT_REF:
1809 case BIT_FIELD_REF:
1810 return staticp (TREE_OPERAND (arg, 0));
1812 case INDIRECT_REF:
1813 return TREE_CONSTANT (TREE_OPERAND (arg, 0));
1815 case ARRAY_REF:
1816 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
1817 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
1818 return staticp (TREE_OPERAND (arg, 0));
1821 return 0;
1824 /* This should be applied to any node which may be used in more than one place,
1825 but must be evaluated only once. Normally, the code generator would
1826 reevaluate the node each time; this forces it to compute it once and save
1827 the result. This is done by encapsulating the node in a SAVE_EXPR. */
1829 tree
1830 save_expr (expr)
1831 tree expr;
1833 register tree t = fold (expr);
1835 /* We don't care about whether this can be used as an lvalue in this
1836 context. */
1837 while (TREE_CODE (t) == NON_LVALUE_EXPR)
1838 t = TREE_OPERAND (t, 0);
1840 /* If the tree evaluates to a constant, then we don't want to hide that
1841 fact (i.e. this allows further folding, and direct checks for constants).
1842 However, a read-only object that has side effects cannot be bypassed.
1843 Since it is no problem to reevaluate literals, we just return the
1844 literal node. */
1846 if (TREE_CONSTANT (t) || (TREE_READONLY (t) && ! TREE_SIDE_EFFECTS (t))
1847 || TREE_CODE (t) == SAVE_EXPR)
1848 return t;
1850 t = build (SAVE_EXPR, TREE_TYPE (expr), t, current_function_decl, NULL_TREE);
1852 /* This expression might be placed ahead of a jump to ensure that the
1853 value was computed on both sides of the jump. So make sure it isn't
1854 eliminated as dead. */
1855 TREE_SIDE_EFFECTS (t) = 1;
1856 return t;
1859 /* Stabilize a reference so that we can use it any number of times
1860 without causing its operands to be evaluated more than once.
1861 Returns the stabilized reference.
1863 Also allows conversion expressions whose operands are references.
1864 Any other kind of expression is returned unchanged. */
1866 tree
1867 stabilize_reference (ref)
1868 tree ref;
1870 register tree result;
1871 register enum tree_code code = TREE_CODE (ref);
1873 switch (code)
1875 case VAR_DECL:
1876 case PARM_DECL:
1877 case RESULT_DECL:
1878 /* No action is needed in this case. */
1879 return ref;
1881 case NOP_EXPR:
1882 case CONVERT_EXPR:
1883 case FLOAT_EXPR:
1884 case FIX_TRUNC_EXPR:
1885 case FIX_FLOOR_EXPR:
1886 case FIX_ROUND_EXPR:
1887 case FIX_CEIL_EXPR:
1888 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
1889 break;
1891 case INDIRECT_REF:
1892 result = build_nt (INDIRECT_REF,
1893 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
1894 break;
1896 case COMPONENT_REF:
1897 result = build_nt (COMPONENT_REF,
1898 stabilize_reference (TREE_OPERAND (ref, 0)),
1899 TREE_OPERAND (ref, 1));
1900 break;
1902 case BIT_FIELD_REF:
1903 result = build_nt (BIT_FIELD_REF,
1904 stabilize_reference (TREE_OPERAND (ref, 0)),
1905 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
1906 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
1907 break;
1909 case ARRAY_REF:
1910 result = build_nt (ARRAY_REF,
1911 stabilize_reference (TREE_OPERAND (ref, 0)),
1912 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
1913 break;
1915 /* If arg isn't a kind of lvalue we recognize, make no change.
1916 Caller should recognize the error for an invalid lvalue. */
1917 default:
1918 return ref;
1920 case ERROR_MARK:
1921 return error_mark_node;
1924 TREE_TYPE (result) = TREE_TYPE (ref);
1925 TREE_READONLY (result) = TREE_READONLY (ref);
1926 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
1927 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
1928 TREE_RAISES (result) = TREE_RAISES (ref);
1930 return result;
1933 /* Subroutine of stabilize_reference; this is called for subtrees of
1934 references. Any expression with side-effects must be put in a SAVE_EXPR
1935 to ensure that it is only evaluated once.
1937 We don't put SAVE_EXPR nodes around everything, because assigning very
1938 simple expressions to temporaries causes us to miss good opportunities
1939 for optimizations. Among other things, the opportunity to fold in the
1940 addition of a constant into an addressing mode often gets lost, e.g.
1941 "y[i+1] += x;". In general, we take the approach that we should not make
1942 an assignment unless we are forced into it - i.e., that any non-side effect
1943 operator should be allowed, and that cse should take care of coalescing
1944 multiple utterances of the same expression should that prove fruitful. */
1946 static tree
1947 stabilize_reference_1 (e)
1948 tree e;
1950 register tree result;
1951 register int length;
1952 register enum tree_code code = TREE_CODE (e);
1954 /* We cannot ignore const expressions because it might be a reference
1955 to a const array but whose index contains side-effects. But we can
1956 ignore things that are actual constant or that already have been
1957 handled by this function. */
1959 if (TREE_CONSTANT (e) || code == SAVE_EXPR)
1960 return e;
1962 switch (TREE_CODE_CLASS (code))
1964 case 'x':
1965 case 't':
1966 case 'd':
1967 case 'b':
1968 case '<':
1969 case 's':
1970 case 'e':
1971 case 'r':
1972 /* If the expression has side-effects, then encase it in a SAVE_EXPR
1973 so that it will only be evaluated once. */
1974 /* The reference (r) and comparison (<) classes could be handled as
1975 below, but it is generally faster to only evaluate them once. */
1976 if (TREE_SIDE_EFFECTS (e))
1977 return save_expr (e);
1978 return e;
1980 case 'c':
1981 /* Constants need no processing. In fact, we should never reach
1982 here. */
1983 return e;
1985 case '2':
1986 /* Division is slow and tends to be compiled with jumps,
1987 especially the division by powers of 2 that is often
1988 found inside of an array reference. So do it just once. */
1989 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
1990 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
1991 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
1992 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
1993 return save_expr (e);
1994 /* Recursively stabilize each operand. */
1995 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
1996 stabilize_reference_1 (TREE_OPERAND (e, 1)));
1997 break;
1999 case '1':
2000 /* Recursively stabilize each operand. */
2001 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
2002 break;
2005 TREE_TYPE (result) = TREE_TYPE (e);
2006 TREE_READONLY (result) = TREE_READONLY (e);
2007 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
2008 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
2009 TREE_RAISES (result) = TREE_RAISES (e);
2011 return result;
2014 /* Low-level constructors for expressions. */
2016 /* Build an expression of code CODE, data type TYPE,
2017 and operands as specified by the arguments ARG1 and following arguments.
2018 Expressions and reference nodes can be created this way.
2019 Constants, decls, types and misc nodes cannot be. */
2021 tree
2022 build (va_alist)
2023 va_dcl
2025 va_list p;
2026 enum tree_code code;
2027 register tree t;
2028 register int length;
2029 register int i;
2031 va_start (p);
2033 code = va_arg (p, enum tree_code);
2034 t = make_node (code);
2035 length = tree_code_length[(int) code];
2036 TREE_TYPE (t) = va_arg (p, tree);
2038 if (length == 2)
2040 /* This is equivalent to the loop below, but faster. */
2041 register tree arg0 = va_arg (p, tree);
2042 register tree arg1 = va_arg (p, tree);
2043 TREE_OPERAND (t, 0) = arg0;
2044 TREE_OPERAND (t, 1) = arg1;
2045 if ((arg0 && TREE_SIDE_EFFECTS (arg0))
2046 || (arg1 && TREE_SIDE_EFFECTS (arg1)))
2047 TREE_SIDE_EFFECTS (t) = 1;
2048 TREE_RAISES (t)
2049 = (arg0 && TREE_RAISES (arg0)) || (arg1 && TREE_RAISES (arg1));
2051 else if (length == 1)
2053 register tree arg0 = va_arg (p, tree);
2055 /* Call build1 for this! */
2056 if (TREE_CODE_CLASS (code) != 's')
2057 abort ();
2058 TREE_OPERAND (t, 0) = arg0;
2059 if (arg0 && TREE_SIDE_EFFECTS (arg0))
2060 TREE_SIDE_EFFECTS (t) = 1;
2061 TREE_RAISES (t) = (arg0 && TREE_RAISES (arg0));
2063 else
2065 for (i = 0; i < length; i++)
2067 register tree operand = va_arg (p, tree);
2068 TREE_OPERAND (t, i) = operand;
2069 if (operand)
2071 if (TREE_SIDE_EFFECTS (operand))
2072 TREE_SIDE_EFFECTS (t) = 1;
2073 if (TREE_RAISES (operand))
2074 TREE_RAISES (t) = 1;
2078 va_end (p);
2079 return t;
2082 /* Same as above, but only builds for unary operators.
2083 Saves lions share of calls to `build'; cuts down use
2084 of varargs, which is expensive for RISC machines. */
2085 tree
2086 build1 (code, type, node)
2087 enum tree_code code;
2088 tree type;
2089 tree node;
2091 register struct obstack *obstack = current_obstack;
2092 register int i, length;
2093 register tree_node_kind kind;
2094 register tree t;
2096 #ifdef GATHER_STATISTICS
2097 if (TREE_CODE_CLASS (code) == 'r')
2098 kind = r_kind;
2099 else
2100 kind = e_kind;
2101 #endif
2103 obstack = expression_obstack;
2104 length = sizeof (struct tree_exp);
2106 t = (tree) obstack_alloc (obstack, length);
2108 #ifdef GATHER_STATISTICS
2109 tree_node_counts[(int)kind]++;
2110 tree_node_sizes[(int)kind] += length;
2111 #endif
2113 for (i = (length / sizeof (int)) - 1; i >= 0; i--)
2114 ((int *) t)[i] = 0;
2116 TREE_TYPE (t) = type;
2117 TREE_SET_CODE (t, code);
2119 if (obstack == &permanent_obstack)
2120 TREE_PERMANENT (t) = 1;
2122 TREE_OPERAND (t, 0) = node;
2123 if (node)
2125 if (TREE_SIDE_EFFECTS (node))
2126 TREE_SIDE_EFFECTS (t) = 1;
2127 if (TREE_RAISES (node))
2128 TREE_RAISES (t) = 1;
2131 return t;
2134 /* Similar except don't specify the TREE_TYPE
2135 and leave the TREE_SIDE_EFFECTS as 0.
2136 It is permissible for arguments to be null,
2137 or even garbage if their values do not matter. */
2139 tree
2140 build_nt (va_alist)
2141 va_dcl
2143 va_list p;
2144 register enum tree_code code;
2145 register tree t;
2146 register int length;
2147 register int i;
2149 va_start (p);
2151 code = va_arg (p, enum tree_code);
2152 t = make_node (code);
2153 length = tree_code_length[(int) code];
2155 for (i = 0; i < length; i++)
2156 TREE_OPERAND (t, i) = va_arg (p, tree);
2158 va_end (p);
2159 return t;
2162 /* Similar to `build_nt', except we build
2163 on the temp_decl_obstack, regardless. */
2165 tree
2166 build_parse_node (va_alist)
2167 va_dcl
2169 register struct obstack *ambient_obstack = expression_obstack;
2170 va_list p;
2171 register enum tree_code code;
2172 register tree t;
2173 register int length;
2174 register int i;
2176 expression_obstack = &temp_decl_obstack;
2178 va_start (p);
2180 code = va_arg (p, enum tree_code);
2181 t = make_node (code);
2182 length = tree_code_length[(int) code];
2184 for (i = 0; i < length; i++)
2185 TREE_OPERAND (t, i) = va_arg (p, tree);
2187 va_end (p);
2188 expression_obstack = ambient_obstack;
2189 return t;
2192 #if 0
2193 /* Commented out because this wants to be done very
2194 differently. See cp-lex.c. */
2195 tree
2196 build_op_identifier (op1, op2)
2197 tree op1, op2;
2199 register tree t = make_node (OP_IDENTIFIER);
2200 TREE_PURPOSE (t) = op1;
2201 TREE_VALUE (t) = op2;
2202 return t;
2204 #endif
2206 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
2207 We do NOT enter this node in any sort of symbol table.
2209 layout_decl is used to set up the decl's storage layout.
2210 Other slots are initialized to 0 or null pointers. */
2212 tree
2213 build_decl (code, name, type)
2214 enum tree_code code;
2215 tree name, type;
2217 register tree t;
2219 t = make_node (code);
2221 /* if (type == error_mark_node)
2222 type = integer_type_node; */
2223 /* That is not done, deliberately, so that having error_mark_node
2224 as the type can suppress useless errors in the use of this variable. */
2226 DECL_NAME (t) = name;
2227 DECL_ASSEMBLER_NAME (t) = name;
2228 TREE_TYPE (t) = type;
2230 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
2231 layout_decl (t, 0);
2232 else if (code == FUNCTION_DECL)
2233 DECL_MODE (t) = FUNCTION_MODE;
2235 return t;
2238 /* BLOCK nodes are used to represent the structure of binding contours
2239 and declarations, once those contours have been exited and their contents
2240 compiled. This information is used for outputting debugging info. */
2242 tree
2243 build_block (vars, tags, subblocks, supercontext, chain)
2244 tree vars, tags, subblocks, supercontext, chain;
2246 register tree block = make_node (BLOCK);
2247 BLOCK_VARS (block) = vars;
2248 BLOCK_TYPE_TAGS (block) = tags;
2249 BLOCK_SUBBLOCKS (block) = subblocks;
2250 BLOCK_SUPERCONTEXT (block) = supercontext;
2251 BLOCK_CHAIN (block) = chain;
2252 return block;
2255 /* Return a type like TYPE except that its TYPE_READONLY is CONSTP
2256 and its TYPE_VOLATILE is VOLATILEP.
2258 Such variant types already made are recorded so that duplicates
2259 are not made.
2261 A variant types should never be used as the type of an expression.
2262 Always copy the variant information into the TREE_READONLY
2263 and TREE_THIS_VOLATILE of the expression, and then give the expression
2264 as its type the "main variant", the variant whose TYPE_READONLY
2265 and TYPE_VOLATILE are zero. Use TYPE_MAIN_VARIANT to find the
2266 main variant. */
2268 tree
2269 build_type_variant (type, constp, volatilep)
2270 tree type;
2271 int constp, volatilep;
2273 register tree t, m = TYPE_MAIN_VARIANT (type);
2274 register struct obstack *ambient_obstack = current_obstack;
2276 /* Treat any nonzero argument as 1. */
2277 constp = !!constp;
2278 volatilep = !!volatilep;
2280 /* If not generating auxiliary info, search the chain of variants to see
2281 if there is already one there just like the one we need to have. If so,
2282 use that existing one.
2284 We don't do this in the case where we are generating aux info because
2285 in that case we want each typedef names to get it's own distinct type
2286 node, even if the type of this new typedef is the same as some other
2287 (existing) type. */
2289 if (!flag_gen_aux_info)
2290 for (t = m; t; t = TYPE_NEXT_VARIANT (t))
2291 if (constp == TYPE_READONLY (t) && volatilep == TYPE_VOLATILE (t))
2292 return t;
2294 /* We need a new one. */
2296 current_obstack = TYPE_OBSTACK (type);
2297 t = copy_node (type);
2298 current_obstack = ambient_obstack;
2300 TYPE_READONLY (t) = constp;
2301 TYPE_VOLATILE (t) = volatilep;
2302 TYPE_POINTER_TO (t) = 0;
2303 TYPE_REFERENCE_TO (t) = 0;
2305 /* Add this type to the chain of variants of TYPE. */
2306 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
2307 TYPE_NEXT_VARIANT (m) = t;
2309 return t;
2312 /* Give TYPE a new main variant: NEW_MAIN.
2313 This is the right thing to do only when something else
2314 about TYPE is modified in place. */
2316 tree
2317 change_main_variant (type, new_main)
2318 tree type, new_main;
2320 tree t;
2321 tree omain = TYPE_MAIN_VARIANT (type);
2323 /* Remove TYPE from the TYPE_NEXT_VARIANT chain of its main variant. */
2324 if (TYPE_NEXT_VARIANT (omain) == type)
2325 TYPE_NEXT_VARIANT (omain) = TYPE_NEXT_VARIANT (type);
2326 else
2327 for (t = TYPE_NEXT_VARIANT (omain); t && TYPE_NEXT_VARIANT (t);
2328 t = TYPE_NEXT_VARIANT (t))
2329 if (TYPE_NEXT_VARIANT (t) == type)
2331 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (type);
2332 break;
2335 TYPE_MAIN_VARIANT (type) = new_main;
2336 TYPE_NEXT_VARIANT (type) = TYPE_NEXT_VARIANT (new_main);
2337 TYPE_NEXT_VARIANT (new_main) = type;
2340 /* Create a new variant of TYPE, equivalent but distinct.
2341 This is so the caller can modify it. */
2343 tree
2344 build_type_copy (type)
2345 tree type;
2347 register tree t, m = TYPE_MAIN_VARIANT (type);
2348 register struct obstack *ambient_obstack = current_obstack;
2350 current_obstack = TYPE_OBSTACK (type);
2351 t = copy_node (type);
2352 current_obstack = ambient_obstack;
2354 TYPE_POINTER_TO (t) = 0;
2355 TYPE_REFERENCE_TO (t) = 0;
2357 /* Add this type to the chain of variants of TYPE. */
2358 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
2359 TYPE_NEXT_VARIANT (m) = t;
2361 return t;
2364 /* Hashing of types so that we don't make duplicates.
2365 The entry point is `type_hash_canon'. */
2367 /* Each hash table slot is a bucket containing a chain
2368 of these structures. */
2370 struct type_hash
2372 struct type_hash *next; /* Next structure in the bucket. */
2373 int hashcode; /* Hash code of this type. */
2374 tree type; /* The type recorded here. */
2377 /* Now here is the hash table. When recording a type, it is added
2378 to the slot whose index is the hash code mod the table size.
2379 Note that the hash table is used for several kinds of types
2380 (function types, array types and array index range types, for now).
2381 While all these live in the same table, they are completely independent,
2382 and the hash code is computed differently for each of these. */
2384 #define TYPE_HASH_SIZE 59
2385 struct type_hash *type_hash_table[TYPE_HASH_SIZE];
2387 /* Here is how primitive or already-canonicalized types' hash
2388 codes are made. */
2389 #define TYPE_HASH(TYPE) ((HOST_WIDE_INT) (TYPE) & 0777777)
2391 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
2392 with types in the TREE_VALUE slots), by adding the hash codes
2393 of the individual types. */
2396 type_hash_list (list)
2397 tree list;
2399 register int hashcode;
2400 register tree tail;
2401 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
2402 hashcode += TYPE_HASH (TREE_VALUE (tail));
2403 return hashcode;
2406 /* Look in the type hash table for a type isomorphic to TYPE.
2407 If one is found, return it. Otherwise return 0. */
2409 tree
2410 type_hash_lookup (hashcode, type)
2411 int hashcode;
2412 tree type;
2414 register struct type_hash *h;
2415 for (h = type_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
2416 if (h->hashcode == hashcode
2417 && TREE_CODE (h->type) == TREE_CODE (type)
2418 && TREE_TYPE (h->type) == TREE_TYPE (type)
2419 && (TYPE_MAX_VALUE (h->type) == TYPE_MAX_VALUE (type)
2420 || tree_int_cst_equal (TYPE_MAX_VALUE (h->type),
2421 TYPE_MAX_VALUE (type)))
2422 && (TYPE_MIN_VALUE (h->type) == TYPE_MIN_VALUE (type)
2423 || tree_int_cst_equal (TYPE_MIN_VALUE (h->type),
2424 TYPE_MIN_VALUE (type)))
2425 && (TYPE_DOMAIN (h->type) == TYPE_DOMAIN (type)
2426 || (TYPE_DOMAIN (h->type)
2427 && TREE_CODE (TYPE_DOMAIN (h->type)) == TREE_LIST
2428 && TYPE_DOMAIN (type)
2429 && TREE_CODE (TYPE_DOMAIN (type)) == TREE_LIST
2430 && type_list_equal (TYPE_DOMAIN (h->type), TYPE_DOMAIN (type)))))
2431 return h->type;
2432 return 0;
2435 /* Add an entry to the type-hash-table
2436 for a type TYPE whose hash code is HASHCODE. */
2438 void
2439 type_hash_add (hashcode, type)
2440 int hashcode;
2441 tree type;
2443 register struct type_hash *h;
2445 h = (struct type_hash *) oballoc (sizeof (struct type_hash));
2446 h->hashcode = hashcode;
2447 h->type = type;
2448 h->next = type_hash_table[hashcode % TYPE_HASH_SIZE];
2449 type_hash_table[hashcode % TYPE_HASH_SIZE] = h;
2452 /* Given TYPE, and HASHCODE its hash code, return the canonical
2453 object for an identical type if one already exists.
2454 Otherwise, return TYPE, and record it as the canonical object
2455 if it is a permanent object.
2457 To use this function, first create a type of the sort you want.
2458 Then compute its hash code from the fields of the type that
2459 make it different from other similar types.
2460 Then call this function and use the value.
2461 This function frees the type you pass in if it is a duplicate. */
2463 /* Set to 1 to debug without canonicalization. Never set by program. */
2464 int debug_no_type_hash = 0;
2466 tree
2467 type_hash_canon (hashcode, type)
2468 int hashcode;
2469 tree type;
2471 tree t1;
2473 if (debug_no_type_hash)
2474 return type;
2476 t1 = type_hash_lookup (hashcode, type);
2477 if (t1 != 0)
2479 struct obstack *o
2480 = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
2481 obstack_free (o, type);
2482 #ifdef GATHER_STATISTICS
2483 tree_node_counts[(int)t_kind]--;
2484 tree_node_sizes[(int)t_kind] -= sizeof (struct tree_type);
2485 #endif
2486 return t1;
2489 /* If this is a new type, record it for later reuse. */
2490 if (current_obstack == &permanent_obstack)
2491 type_hash_add (hashcode, type);
2493 return type;
2496 /* Given two lists of types
2497 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
2498 return 1 if the lists contain the same types in the same order.
2499 Also, the TREE_PURPOSEs must match. */
2502 type_list_equal (l1, l2)
2503 tree l1, l2;
2505 register tree t1, t2;
2506 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2508 if (TREE_VALUE (t1) != TREE_VALUE (t2))
2509 return 0;
2510 if (TREE_PURPOSE (t1) != TREE_PURPOSE (t2))
2512 int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
2513 if (cmp < 0)
2514 abort ();
2515 if (cmp == 0)
2516 return 0;
2520 return t1 == t2;
2523 /* Nonzero if integer constants T1 and T2
2524 represent the same constant value. */
2527 tree_int_cst_equal (t1, t2)
2528 tree t1, t2;
2530 if (t1 == t2)
2531 return 1;
2532 if (t1 == 0 || t2 == 0)
2533 return 0;
2534 if (TREE_CODE (t1) == INTEGER_CST
2535 && TREE_CODE (t2) == INTEGER_CST
2536 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
2537 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
2538 return 1;
2539 return 0;
2542 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
2543 The precise way of comparison depends on their data type. */
2546 tree_int_cst_lt (t1, t2)
2547 tree t1, t2;
2549 if (t1 == t2)
2550 return 0;
2552 if (!TREE_UNSIGNED (TREE_TYPE (t1)))
2553 return INT_CST_LT (t1, t2);
2554 return INT_CST_LT_UNSIGNED (t1, t2);
2557 /* Compare two constructor-element-type constants. */
2559 simple_cst_list_equal (l1, l2)
2560 tree l1, l2;
2562 while (l1 != NULL_TREE && l2 != NULL_TREE)
2564 int cmp = simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2));
2565 if (cmp < 0)
2566 abort ();
2567 if (cmp == 0)
2568 return 0;
2569 l1 = TREE_CHAIN (l1);
2570 l2 = TREE_CHAIN (l2);
2572 return (l1 == l2);
2575 /* Return truthvalue of whether T1 is the same tree structure as T2.
2576 Return 1 if they are the same.
2577 Return 0 if they are understandably different.
2578 Return -1 if either contains tree structure not understood by
2579 this function. */
2582 simple_cst_equal (t1, t2)
2583 tree t1, t2;
2585 register enum tree_code code1, code2;
2586 int cmp;
2588 if (t1 == t2)
2589 return 1;
2590 if (t1 == 0 || t2 == 0)
2591 return 0;
2593 code1 = TREE_CODE (t1);
2594 code2 = TREE_CODE (t2);
2596 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
2597 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR || code2 == NON_LVALUE_EXPR)
2598 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2599 else
2600 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
2601 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
2602 || code2 == NON_LVALUE_EXPR)
2603 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
2605 if (code1 != code2)
2606 return 0;
2608 switch (code1)
2610 case INTEGER_CST:
2611 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
2612 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
2614 case REAL_CST:
2615 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2617 case STRING_CST:
2618 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2619 && !bcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2620 TREE_STRING_LENGTH (t1));
2622 case CONSTRUCTOR:
2623 abort ();
2625 case SAVE_EXPR:
2626 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2628 case CALL_EXPR:
2629 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2630 if (cmp <= 0)
2631 return cmp;
2632 return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2634 case TARGET_EXPR:
2635 /* Special case: if either target is an unallocated VAR_DECL,
2636 it means that it's going to be unified with whatever the
2637 TARGET_EXPR is really supposed to initialize, so treat it
2638 as being equivalent to anything. */
2639 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
2640 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
2641 && DECL_RTL (TREE_OPERAND (t1, 0)) == 0)
2642 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
2643 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
2644 && DECL_RTL (TREE_OPERAND (t2, 0)) == 0))
2645 cmp = 1;
2646 else
2647 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2648 if (cmp <= 0)
2649 return cmp;
2650 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2652 case WITH_CLEANUP_EXPR:
2653 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2654 if (cmp <= 0)
2655 return cmp;
2656 return simple_cst_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t1, 2));
2658 case COMPONENT_REF:
2659 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
2660 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2661 return 0;
2663 case VAR_DECL:
2664 case PARM_DECL:
2665 case CONST_DECL:
2666 case FUNCTION_DECL:
2667 return 0;
2670 /* This general rule works for most tree codes.
2671 All exceptions should be handled above. */
2673 switch (TREE_CODE_CLASS (code1))
2675 int i;
2676 case '1':
2677 case '2':
2678 case '<':
2679 case 'e':
2680 case 'r':
2681 case 's':
2682 cmp = 1;
2683 for (i=0; i<tree_code_length[(int) code1]; ++i)
2685 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
2686 if (cmp <= 0)
2687 return cmp;
2689 return cmp;
2692 return -1;
2695 /* Constructors for pointer, array and function types.
2696 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
2697 constructed by language-dependent code, not here.) */
2699 /* Construct, lay out and return the type of pointers to TO_TYPE.
2700 If such a type has already been constructed, reuse it. */
2702 tree
2703 build_pointer_type (to_type)
2704 tree to_type;
2706 register tree t = TYPE_POINTER_TO (to_type);
2708 /* First, if we already have a type for pointers to TO_TYPE, use it. */
2710 if (t)
2711 return t;
2713 /* We need a new one. Put this in the same obstack as TO_TYPE. */
2714 push_obstacks (TYPE_OBSTACK (to_type), TYPE_OBSTACK (to_type));
2715 t = make_node (POINTER_TYPE);
2716 pop_obstacks ();
2718 TREE_TYPE (t) = to_type;
2720 /* Record this type as the pointer to TO_TYPE. */
2721 TYPE_POINTER_TO (to_type) = t;
2723 /* Lay out the type. This function has many callers that are concerned
2724 with expression-construction, and this simplifies them all.
2725 Also, it guarantees the TYPE_SIZE is in the same obstack as the type. */
2726 layout_type (t);
2728 return t;
2731 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
2732 MAXVAL should be the maximum value in the domain
2733 (one less than the length of the array). */
2735 tree
2736 build_index_type (maxval)
2737 tree maxval;
2739 register tree itype = make_node (INTEGER_TYPE);
2740 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
2741 TYPE_MIN_VALUE (itype) = build_int_2 (0, 0);
2742 TREE_TYPE (TYPE_MIN_VALUE (itype)) = sizetype;
2743 TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
2744 TYPE_MODE (itype) = TYPE_MODE (sizetype);
2745 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
2746 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
2747 if (TREE_CODE (maxval) == INTEGER_CST)
2749 int maxint = (int) TREE_INT_CST_LOW (maxval);
2750 /* If the domain should be empty, make sure the maxval
2751 remains -1 and is not spoiled by truncation. */
2752 if (INT_CST_LT (maxval, integer_zero_node))
2754 TYPE_MAX_VALUE (itype) = build_int_2 (-1, -1);
2755 TREE_TYPE (TYPE_MAX_VALUE (itype)) = sizetype;
2757 return type_hash_canon (maxint < 0 ? ~maxint : maxint, itype);
2759 else
2760 return itype;
2763 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
2764 ENUMERAL_TYPE, BOOLEAN_TYPE, or CHAR_TYPE), with
2765 low bound LOWVAL and high bound HIGHVAL.
2766 if TYPE==NULL_TREE, sizetype is used. */
2768 tree
2769 build_range_type (type, lowval, highval)
2770 tree type, lowval, highval;
2772 register tree itype = make_node (INTEGER_TYPE);
2773 TREE_TYPE (itype) = type;
2774 if (type == NULL_TREE)
2775 type = sizetype;
2776 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
2777 TYPE_MIN_VALUE (itype) = convert (type, lowval);
2778 TYPE_MAX_VALUE (itype) = convert (type, highval);
2779 TYPE_MODE (itype) = TYPE_MODE (type);
2780 TYPE_SIZE (itype) = TYPE_SIZE (type);
2781 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
2782 if ((TREE_CODE (lowval) == INTEGER_CST)
2783 && (TREE_CODE (highval) == INTEGER_CST))
2785 HOST_WIDE_INT highint = TREE_INT_CST_LOW (highval);
2786 HOST_WIDE_INT lowint = TREE_INT_CST_LOW (lowval);
2787 int maxint = (int) (highint - lowint);
2788 return type_hash_canon (maxint < 0 ? ~maxint : maxint, itype);
2790 else
2791 return itype;
2794 /* Just like build_index_type, but takes lowval and highval instead
2795 of just highval (maxval). */
2797 tree
2798 build_index_2_type (lowval,highval)
2799 tree lowval, highval;
2801 return build_range_type (NULL_TREE, lowval, highval);
2804 /* Return nonzero iff ITYPE1 and ITYPE2 are equal (in the LISP sense).
2805 Needed because when index types are not hashed, equal index types
2806 built at different times appear distinct, even though structurally,
2807 they are not. */
2810 index_type_equal (itype1, itype2)
2811 tree itype1, itype2;
2813 if (TREE_CODE (itype1) != TREE_CODE (itype2))
2814 return 0;
2815 if (TREE_CODE (itype1) == INTEGER_TYPE)
2817 if (TYPE_PRECISION (itype1) != TYPE_PRECISION (itype2)
2818 || TYPE_MODE (itype1) != TYPE_MODE (itype2)
2819 || ! simple_cst_equal (TYPE_SIZE (itype1), TYPE_SIZE (itype2))
2820 || TYPE_ALIGN (itype1) != TYPE_ALIGN (itype2))
2821 return 0;
2822 if (simple_cst_equal (TYPE_MIN_VALUE (itype1), TYPE_MIN_VALUE (itype2))
2823 && simple_cst_equal (TYPE_MAX_VALUE (itype1), TYPE_MAX_VALUE (itype2)))
2824 return 1;
2826 return 0;
2829 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
2830 and number of elements specified by the range of values of INDEX_TYPE.
2831 If such a type has already been constructed, reuse it. */
2833 tree
2834 build_array_type (elt_type, index_type)
2835 tree elt_type, index_type;
2837 register tree t;
2838 int hashcode;
2840 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
2842 error ("arrays of functions are not meaningful");
2843 elt_type = integer_type_node;
2846 /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
2847 build_pointer_type (elt_type);
2849 /* Allocate the array after the pointer type,
2850 in case we free it in type_hash_canon. */
2851 t = make_node (ARRAY_TYPE);
2852 TREE_TYPE (t) = elt_type;
2853 TYPE_DOMAIN (t) = index_type;
2855 if (index_type == 0)
2857 return t;
2860 hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
2861 t = type_hash_canon (hashcode, t);
2863 #if 0 /* This led to crashes, because it could put a temporary node
2864 on the TYPE_NEXT_VARIANT chain of a permanent one. */
2865 /* The main variant of an array type should always
2866 be an array whose element type is the main variant. */
2867 if (elt_type != TYPE_MAIN_VARIANT (elt_type))
2868 change_main_variant (t, build_array_type (TYPE_MAIN_VARIANT (elt_type),
2869 index_type));
2870 #endif
2872 if (TYPE_SIZE (t) == 0)
2873 layout_type (t);
2874 return t;
2877 /* Construct, lay out and return
2878 the type of functions returning type VALUE_TYPE
2879 given arguments of types ARG_TYPES.
2880 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
2881 are data type nodes for the arguments of the function.
2882 If such a type has already been constructed, reuse it. */
2884 tree
2885 build_function_type (value_type, arg_types)
2886 tree value_type, arg_types;
2888 register tree t;
2889 int hashcode;
2891 if (TREE_CODE (value_type) == FUNCTION_TYPE)
2893 error ("function return type cannot be function");
2894 value_type = integer_type_node;
2897 /* Make a node of the sort we want. */
2898 t = make_node (FUNCTION_TYPE);
2899 TREE_TYPE (t) = value_type;
2900 TYPE_ARG_TYPES (t) = arg_types;
2902 /* If we already have such a type, use the old one and free this one. */
2903 hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
2904 t = type_hash_canon (hashcode, t);
2906 if (TYPE_SIZE (t) == 0)
2907 layout_type (t);
2908 return t;
2911 /* Build the node for the type of references-to-TO_TYPE. */
2913 tree
2914 build_reference_type (to_type)
2915 tree to_type;
2917 register tree t = TYPE_REFERENCE_TO (to_type);
2918 register struct obstack *ambient_obstack = current_obstack;
2919 register struct obstack *ambient_saveable_obstack = saveable_obstack;
2921 /* First, if we already have a type for pointers to TO_TYPE, use it. */
2923 if (t)
2924 return t;
2926 /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
2927 if (TREE_PERMANENT (to_type))
2929 current_obstack = &permanent_obstack;
2930 saveable_obstack = &permanent_obstack;
2933 t = make_node (REFERENCE_TYPE);
2934 TREE_TYPE (t) = to_type;
2936 /* Record this type as the pointer to TO_TYPE. */
2937 TYPE_REFERENCE_TO (to_type) = t;
2939 layout_type (t);
2941 current_obstack = ambient_obstack;
2942 saveable_obstack = ambient_saveable_obstack;
2943 return t;
2946 /* Construct, lay out and return the type of methods belonging to class
2947 BASETYPE and whose arguments and values are described by TYPE.
2948 If that type exists already, reuse it.
2949 TYPE must be a FUNCTION_TYPE node. */
2951 tree
2952 build_method_type (basetype, type)
2953 tree basetype, type;
2955 register tree t;
2956 int hashcode;
2958 /* Make a node of the sort we want. */
2959 t = make_node (METHOD_TYPE);
2961 if (TREE_CODE (type) != FUNCTION_TYPE)
2962 abort ();
2964 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
2965 TREE_TYPE (t) = TREE_TYPE (type);
2967 /* The actual arglist for this function includes a "hidden" argument
2968 which is "this". Put it into the list of argument types. */
2970 TYPE_ARG_TYPES (t)
2971 = tree_cons (NULL_TREE,
2972 build_pointer_type (basetype), TYPE_ARG_TYPES (type));
2974 /* If we already have such a type, use the old one and free this one. */
2975 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
2976 t = type_hash_canon (hashcode, t);
2978 if (TYPE_SIZE (t) == 0)
2979 layout_type (t);
2981 return t;
2984 /* Construct, lay out and return the type of offsets to a value
2985 of type TYPE, within an object of type BASETYPE.
2986 If a suitable offset type exists already, reuse it. */
2988 tree
2989 build_offset_type (basetype, type)
2990 tree basetype, type;
2992 register tree t;
2993 int hashcode;
2995 /* Make a node of the sort we want. */
2996 t = make_node (OFFSET_TYPE);
2998 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
2999 TREE_TYPE (t) = type;
3001 /* If we already have such a type, use the old one and free this one. */
3002 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
3003 t = type_hash_canon (hashcode, t);
3005 if (TYPE_SIZE (t) == 0)
3006 layout_type (t);
3008 return t;
3011 /* Create a complex type whose components are COMPONENT_TYPE. */
3013 tree
3014 build_complex_type (component_type)
3015 tree component_type;
3017 register tree t;
3018 int hashcode;
3020 /* Make a node of the sort we want. */
3021 t = make_node (COMPLEX_TYPE);
3023 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
3024 TYPE_VOLATILE (t) = TYPE_VOLATILE (component_type);
3025 TYPE_READONLY (t) = TYPE_READONLY (component_type);
3027 /* If we already have such a type, use the old one and free this one. */
3028 hashcode = TYPE_HASH (component_type);
3029 t = type_hash_canon (hashcode, t);
3031 if (TYPE_SIZE (t) == 0)
3032 layout_type (t);
3034 return t;
3037 /* Return OP, stripped of any conversions to wider types as much as is safe.
3038 Converting the value back to OP's type makes a value equivalent to OP.
3040 If FOR_TYPE is nonzero, we return a value which, if converted to
3041 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
3043 If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
3044 narrowest type that can hold the value, even if they don't exactly fit.
3045 Otherwise, bit-field references are changed to a narrower type
3046 only if they can be fetched directly from memory in that type.
3048 OP must have integer, real or enumeral type. Pointers are not allowed!
3050 There are some cases where the obvious value we could return
3051 would regenerate to OP if converted to OP's type,
3052 but would not extend like OP to wider types.
3053 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
3054 For example, if OP is (unsigned short)(signed char)-1,
3055 we avoid returning (signed char)-1 if FOR_TYPE is int,
3056 even though extending that to an unsigned short would regenerate OP,
3057 since the result of extending (signed char)-1 to (int)
3058 is different from (int) OP. */
3060 tree
3061 get_unwidened (op, for_type)
3062 register tree op;
3063 tree for_type;
3065 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
3066 /* TYPE_PRECISION is safe in place of type_precision since
3067 pointer types are not allowed. */
3068 register tree type = TREE_TYPE (op);
3069 register unsigned final_prec
3070 = TYPE_PRECISION (for_type != 0 ? for_type : type);
3071 register int uns
3072 = (for_type != 0 && for_type != type
3073 && final_prec > TYPE_PRECISION (type)
3074 && TREE_UNSIGNED (type));
3075 register tree win = op;
3077 while (TREE_CODE (op) == NOP_EXPR)
3079 register int bitschange
3080 = TYPE_PRECISION (TREE_TYPE (op))
3081 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
3083 /* Truncations are many-one so cannot be removed.
3084 Unless we are later going to truncate down even farther. */
3085 if (bitschange < 0
3086 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
3087 break;
3089 /* See what's inside this conversion. If we decide to strip it,
3090 we will set WIN. */
3091 op = TREE_OPERAND (op, 0);
3093 /* If we have not stripped any zero-extensions (uns is 0),
3094 we can strip any kind of extension.
3095 If we have previously stripped a zero-extension,
3096 only zero-extensions can safely be stripped.
3097 Any extension can be stripped if the bits it would produce
3098 are all going to be discarded later by truncating to FOR_TYPE. */
3100 if (bitschange > 0)
3102 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
3103 win = op;
3104 /* TREE_UNSIGNED says whether this is a zero-extension.
3105 Let's avoid computing it if it does not affect WIN
3106 and if UNS will not be needed again. */
3107 if ((uns || TREE_CODE (op) == NOP_EXPR)
3108 && TREE_UNSIGNED (TREE_TYPE (op)))
3110 uns = 1;
3111 win = op;
3116 if (TREE_CODE (op) == COMPONENT_REF
3117 /* Since type_for_size always gives an integer type. */
3118 && TREE_CODE (type) != REAL_TYPE)
3120 unsigned innerprec = TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)));
3121 type = type_for_size (innerprec, TREE_UNSIGNED (TREE_OPERAND (op, 1)));
3123 /* We can get this structure field in the narrowest type it fits in.
3124 If FOR_TYPE is 0, do this only for a field that matches the
3125 narrower type exactly and is aligned for it
3126 The resulting extension to its nominal type (a fullword type)
3127 must fit the same conditions as for other extensions. */
3129 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
3130 && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
3131 && (! uns || final_prec <= innerprec
3132 || TREE_UNSIGNED (TREE_OPERAND (op, 1)))
3133 && type != 0)
3135 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
3136 TREE_OPERAND (op, 1));
3137 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
3138 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
3139 TREE_RAISES (win) = TREE_RAISES (op);
3142 return win;
3145 /* Return OP or a simpler expression for a narrower value
3146 which can be sign-extended or zero-extended to give back OP.
3147 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
3148 or 0 if the value should be sign-extended. */
3150 tree
3151 get_narrower (op, unsignedp_ptr)
3152 register tree op;
3153 int *unsignedp_ptr;
3155 register int uns = 0;
3156 int first = 1;
3157 register tree win = op;
3159 while (TREE_CODE (op) == NOP_EXPR)
3161 register int bitschange
3162 = TYPE_PRECISION (TREE_TYPE (op))
3163 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
3165 /* Truncations are many-one so cannot be removed. */
3166 if (bitschange < 0)
3167 break;
3169 /* See what's inside this conversion. If we decide to strip it,
3170 we will set WIN. */
3171 op = TREE_OPERAND (op, 0);
3173 if (bitschange > 0)
3175 /* An extension: the outermost one can be stripped,
3176 but remember whether it is zero or sign extension. */
3177 if (first)
3178 uns = TREE_UNSIGNED (TREE_TYPE (op));
3179 /* Otherwise, if a sign extension has been stripped,
3180 only sign extensions can now be stripped;
3181 if a zero extension has been stripped, only zero-extensions. */
3182 else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
3183 break;
3184 first = 0;
3186 else /* bitschange == 0 */
3188 /* A change in nominal type can always be stripped, but we must
3189 preserve the unsignedness. */
3190 if (first)
3191 uns = TREE_UNSIGNED (TREE_TYPE (op));
3192 first = 0;
3195 win = op;
3198 if (TREE_CODE (op) == COMPONENT_REF
3199 /* Since type_for_size always gives an integer type. */
3200 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE)
3202 unsigned innerprec = TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)));
3203 tree type = type_for_size (innerprec, TREE_UNSIGNED (op));
3205 /* We can get this structure field in a narrower type that fits it,
3206 but the resulting extension to its nominal type (a fullword type)
3207 must satisfy the same conditions as for other extensions.
3209 Do this only for fields that are aligned (not bit-fields),
3210 because when bit-field insns will be used there is no
3211 advantage in doing this. */
3213 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
3214 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
3215 && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
3216 && type != 0)
3218 if (first)
3219 uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
3220 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
3221 TREE_OPERAND (op, 1));
3222 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
3223 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
3224 TREE_RAISES (win) = TREE_RAISES (op);
3227 *unsignedp_ptr = uns;
3228 return win;
3231 /* Return the precision of a type, for arithmetic purposes.
3232 Supports all types on which arithmetic is possible
3233 (including pointer types).
3234 It's not clear yet what will be right for complex types. */
3237 type_precision (type)
3238 register tree type;
3240 return ((TREE_CODE (type) == INTEGER_TYPE
3241 || TREE_CODE (type) == ENUMERAL_TYPE
3242 || TREE_CODE (type) == REAL_TYPE)
3243 ? TYPE_PRECISION (type) : POINTER_SIZE);
3246 /* Nonzero if integer constant C has a value that is permissible
3247 for type TYPE (an INTEGER_TYPE). */
3250 int_fits_type_p (c, type)
3251 tree c, type;
3253 if (TREE_UNSIGNED (type))
3254 return (!INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c)
3255 && !INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type))
3256 && (TREE_INT_CST_HIGH (c) >= 0 || TREE_UNSIGNED (TREE_TYPE (c))));
3257 else
3258 return (!INT_CST_LT (TYPE_MAX_VALUE (type), c)
3259 && !INT_CST_LT (c, TYPE_MIN_VALUE (type))
3260 && (TREE_INT_CST_HIGH (c) >= 0 || !TREE_UNSIGNED (TREE_TYPE (c))));
3263 /* Return the innermost context enclosing DECL that is
3264 a FUNCTION_DECL, or zero if none. */
3266 tree
3267 decl_function_context (decl)
3268 tree decl;
3270 tree context;
3272 if (TREE_CODE (decl) == ERROR_MARK)
3273 return 0;
3275 if (TREE_CODE (decl) == SAVE_EXPR)
3276 context = SAVE_EXPR_CONTEXT (decl);
3277 else
3278 context = DECL_CONTEXT (decl);
3280 while (context && TREE_CODE (context) != FUNCTION_DECL)
3282 if (TREE_CODE (context) == RECORD_TYPE
3283 || TREE_CODE (context) == UNION_TYPE)
3284 context = TYPE_CONTEXT (context);
3285 else if (TREE_CODE (context) == TYPE_DECL)
3286 context = DECL_CONTEXT (context);
3287 else if (TREE_CODE (context) == BLOCK)
3288 context = BLOCK_SUPERCONTEXT (context);
3289 else
3290 /* Unhandled CONTEXT !? */
3291 abort ();
3294 return context;
3297 /* Return the innermost context enclosing DECL that is
3298 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
3299 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
3301 tree
3302 decl_type_context (decl)
3303 tree decl;
3305 tree context = DECL_CONTEXT (decl);
3307 while (context)
3309 if (TREE_CODE (context) == RECORD_TYPE
3310 || TREE_CODE (context) == UNION_TYPE
3311 || TREE_CODE (context) == QUAL_UNION_TYPE)
3312 return context;
3313 if (TREE_CODE (context) == TYPE_DECL
3314 || TREE_CODE (context) == FUNCTION_DECL)
3315 context = DECL_CONTEXT (context);
3316 else if (TREE_CODE (context) == BLOCK)
3317 context = BLOCK_SUPERCONTEXT (context);
3318 else
3319 /* Unhandled CONTEXT!? */
3320 abort ();
3322 return NULL_TREE;
3325 void
3326 print_obstack_statistics (str, o)
3327 char *str;
3328 struct obstack *o;
3330 struct _obstack_chunk *chunk = o->chunk;
3331 int n_chunks = 0;
3332 int n_alloc = 0;
3334 while (chunk)
3336 n_chunks += 1;
3337 n_alloc += chunk->limit - &chunk->contents[0];
3338 chunk = chunk->prev;
3340 fprintf (stderr, "obstack %s: %d bytes, %d chunks\n",
3341 str, n_alloc, n_chunks);
3343 void
3344 dump_tree_statistics ()
3346 int i;
3347 int total_nodes, total_bytes;
3349 fprintf (stderr, "\n??? tree nodes created\n\n");
3350 #ifdef GATHER_STATISTICS
3351 fprintf (stderr, "Kind Nodes Bytes\n");
3352 fprintf (stderr, "-------------------------------------\n");
3353 total_nodes = total_bytes = 0;
3354 for (i = 0; i < (int) all_kinds; i++)
3356 fprintf (stderr, "%-20s %6d %9d\n", tree_node_kind_names[i],
3357 tree_node_counts[i], tree_node_sizes[i]);
3358 total_nodes += tree_node_counts[i];
3359 total_bytes += tree_node_sizes[i];
3361 fprintf (stderr, "%-20s %9d\n", "identifier names", id_string_size);
3362 fprintf (stderr, "-------------------------------------\n");
3363 fprintf (stderr, "%-20s %6d %9d\n", "Total", total_nodes, total_bytes);
3364 fprintf (stderr, "-------------------------------------\n");
3365 #else
3366 fprintf (stderr, "(No per-node statistics)\n");
3367 #endif
3368 print_lang_statistics ();
3371 #define FILE_FUNCTION_PREFIX_LEN 9
3373 #ifndef NO_DOLLAR_IN_LABEL
3374 #define FILE_FUNCTION_FORMAT "_GLOBAL_$D$%s"
3375 #else /* NO_DOLLAR_IN_LABEL */
3376 #ifndef NO_DOT_IN_LABEL
3377 #define FILE_FUNCTION_FORMAT "_GLOBAL_.D.%s"
3378 #else /* NO_DOT_IN_LABEL */
3379 #define FILE_FUNCTION_FORMAT "__GLOBAL_D_%s"
3380 #endif /* NO_DOT_IN_LABEL */
3381 #endif /* NO_DOLLAR_IN_LABEL */
3383 extern char * first_global_object_name;
3385 /* If KIND=='I', return a suitable global initializer (constructor) name.
3386 If KIND=='D', return a suitable global clean-up (destructor) name. */
3388 tree
3389 get_file_function_name (kind)
3390 int kind;
3392 char *buf;
3393 register char *p;
3395 if (first_global_object_name)
3396 p = first_global_object_name;
3397 else if (main_input_filename)
3398 p = main_input_filename;
3399 else
3400 p = input_filename;
3402 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p));
3404 /* Set up the name of the file-level functions we may need. */
3405 /* Use a global object (which is already required to be unique over
3406 the program) rather than the file name (which imposes extra
3407 constraints). -- Raeburn@MIT.EDU, 10 Jan 1990. */
3408 sprintf (buf, FILE_FUNCTION_FORMAT, p);
3410 /* Don't need to pull wierd characters out of global names. */
3411 if (p != first_global_object_name)
3413 for (p = buf+11; *p; p++)
3414 if (! ((*p >= '0' && *p <= '9')
3415 #if 0 /* we always want labels, which are valid C++ identifiers (+ `$') */
3416 #ifndef ASM_IDENTIFY_GCC /* this is required if `.' is invalid -- k. raeburn */
3417 || *p == '.'
3418 #endif
3419 #endif
3420 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
3421 || *p == '$'
3422 #endif
3423 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
3424 || *p == '.'
3425 #endif
3426 || (*p >= 'A' && *p <= 'Z')
3427 || (*p >= 'a' && *p <= 'z')))
3428 *p = '_';
3431 buf[FILE_FUNCTION_PREFIX_LEN] = kind;
3433 return get_identifier (buf);