No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / usr.bin / g++ / cc1plus / tree.c
blobfccdf2b866e89364a114705533adebaf7129b9b9
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 1988 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 1, 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 <stdio.h>
37 #include "tree.h"
38 #include "obstack.h"
39 #include "gvarargs.h"
40 #include "flags.h"
42 #define obstack_chunk_alloc xmalloc
43 #define obstack_chunk_free free
45 extern int xmalloc ();
46 extern void free ();
48 /* Tree nodes of permanent duration are allocated in this obstack.
49 They are the identifier nodes, and everything outside of
50 the bodies and parameters of function definitions. */
52 struct obstack permanent_obstack;
54 /* The initial RTL, and all ..._TYPE nodes, in a function
55 are allocated in this obstack. Usually they are freed at the
56 end of the function, but if the function is inline they are saved. */
58 struct obstack maybepermanent_obstack;
60 /* The contents of the current function definition are allocated
61 in this obstack, and all are freed at the end of the function. */
63 struct obstack temporary_obstack;
65 /* The tree nodes of an expression are allocated
66 in this obstack, and all are freed at the end of the expression. */
68 struct obstack momentary_obstack;
70 /* The tree nodes of a declarator are allocated
71 in this obstack, and all are freed when the declarator
72 has been parsed. */
74 static struct obstack temp_decl_obstack;
76 /* This points at either permanent_obstack or maybepermanent_obstack. */
78 struct obstack *saveable_obstack;
80 /* This is same as saveable_obstack during parse and expansion phase;
81 it points to temporary_obstack during optimization.
82 This is the obstack to be used for creating rtl objects. */
84 struct obstack *rtl_obstack;
86 /* This points at either permanent_obstack or temporary_obstack. */
88 struct obstack *current_obstack;
90 /* This points at either permanent_obstack or temporary_obstack
91 or momentary_obstack. */
93 struct obstack *expression_obstack;
95 /* Addresses of first objects in some obstacks.
96 This is for freeing their entire contents. */
97 char *maybepermanent_firstobj;
98 char *temporary_firstobj;
99 char *momentary_firstobj;
100 char *temp_decl_firstobj;
102 /* Nonzero means all ..._TYPE nodes should be allocated permanently. */
104 int all_types_permanent;
106 /* Stack of places to restore the momentary obstack back to. */
108 struct momentary_level
110 /* Pointer back to previous such level. */
111 struct momentary_level *prev;
112 /* First object allocated within this level. */
113 char *base;
114 /* Value of expression_obstack saved at entry to this level. */
115 struct obstack *obstack;
118 struct momentary_level *momentary_stack;
120 /* Table indexed by tree code giving a string containing a character
121 classifying the tree code. Possibilities are
122 t, d, s, c, r and e. See tree.def for details. */
124 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
126 char *standard_tree_code_type[] = {
127 #include "tree.def"
129 #undef DEFTREECODE
131 /* Table indexed by tree code giving number of expression
132 operands beyond the fixed part of the node structure.
133 Not used for types or decls. */
135 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
137 int standard_tree_code_length[] = {
138 #include "tree.def"
140 #undef DEFTREECODE
142 /* Names of tree components.
143 Used for printing out the tree and error messages. */
144 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
146 char *standard_tree_code_name[] = {
147 #include "tree.def"
149 #undef DEFTREECODE
151 /* Table indexed by tree code giving a string containing a character
152 classifying the tree code. Possibilities are
153 t, d, s, c, r and e. See tree.def for details. */
155 char **tree_code_type;
157 /* Table indexed by tree code giving number of expression
158 operands beyond the fixed part of the node structure.
159 Not used for types or decls. */
161 int *tree_code_length;
163 /* Table indexed by tree code giving name of tree code, as a string. */
165 char **tree_code_name;
167 /* Counter for assigning unique ids to all tree nodes. */
169 int tree_node_counter = 0;
171 /* Statistics-gathering stuff. */
172 typedef enum
174 d_kind, t_kind, s_kind, r_kind, e_kind, c_kind,
175 id_kind, op_id_kind, perm_list_kind, temp_list_kind,
176 x_kind, lang_decl, lang_type, all_kinds
177 } tree_node_kind;
178 int tree_node_kinds[(int)all_kinds];
179 int tree_node_sizes[(int)all_kinds];
180 int id_string_size = 0;
181 char *tree_node_kind_names[] = { "decls", "types", "stmts", "refs", "exprs", "constants",
182 "identifiers", "op_identifiers", "perm_tree_lists", "temp_tree_lists",
183 "random kinds", "lang_decl kinds", "lang_type kinds" };
185 /* Hash table for uniquizing IDENTIFIER_NODEs by name. */
187 #define MAX_HASH_TABLE 1009
188 static tree hash_table[MAX_HASH_TABLE]; /* id hash buckets */
190 /* 0 while creating built-in identifiers. */
191 static int do_identifier_warnings;
193 /* Init data for node creation, at the beginning of compilation. */
195 void
196 init_tree ()
198 obstack_init (&permanent_obstack);
200 obstack_init (&temporary_obstack);
201 temporary_firstobj = (char *) obstack_alloc (&temporary_obstack, 0);
202 obstack_init (&momentary_obstack);
203 momentary_firstobj = (char *) obstack_alloc (&momentary_obstack, 0);
204 obstack_init (&maybepermanent_obstack);
205 maybepermanent_firstobj
206 = (char *) obstack_alloc (&maybepermanent_obstack, 0);
207 obstack_init (&temp_decl_obstack);
208 temp_decl_firstobj = (char *) obstack_alloc (&temp_decl_obstack, 0);
210 current_obstack = &permanent_obstack;
211 expression_obstack = &permanent_obstack;
212 rtl_obstack = saveable_obstack = &permanent_obstack;
213 tree_node_counter = 1;
214 /* bzero (hash_table, sizeof hash_table); */
216 tree_code_type = (char **) xmalloc (sizeof (standard_tree_code_type));
217 tree_code_length = (int *) xmalloc (sizeof (standard_tree_code_length));
218 tree_code_name = (char **) xmalloc (sizeof (standard_tree_code_name));
219 bcopy (standard_tree_code_type, tree_code_type,
220 sizeof (standard_tree_code_type));
221 bcopy (standard_tree_code_length, tree_code_length,
222 sizeof (standard_tree_code_length));
223 bcopy (standard_tree_code_name, tree_code_name,
224 sizeof (standard_tree_code_name));
226 #if 0
227 /* Save all variables describing the current status into the structure *P.
228 This is used before starting a nested function. */
230 void
231 save_tree_status (p)
232 struct function *p;
234 p->all_types_permanent = all_types_permanent;
235 p->momentary_stack = momentary_stack;
236 p->maybepermanent_firstobj = maybepermanent_firstobj;
237 p->temporary_firstobj = temporary_firstobj;
238 p->momentary_firstobj = momentary_firstobj;
239 p->current_obstack = current_obstack;
240 p->expression_obstack = expression_obstack;
241 p->saveable_obstack = saveable_obstack;
242 p->rtl_obstack = rtl_obstack;
244 current_obstack = &permanent_obstack;
245 expression_obstack = &permanent_obstack;
246 rtl_obstack = saveable_obstack = &permanent_obstack;
248 maybepermanent_firstobj = (char *) obstack_finish (&maybepermanent_obstack);
249 temporary_firstobj = (char *) obstack_finish (&temporary_obstack);
250 momentary_firstobj = (char *) obstack_finish (&momentary_obstack);
253 /* Restore all variables describing the current status from the structure *P.
254 This is used after a nested function. */
256 void
257 restore_tree_status (p)
258 struct function *p;
260 all_types_permanent = p->all_types_permanent;
261 momentary_stack = p->momentary_stack;
263 obstack_free (&maybepermanent_obstack, maybepermanent_firstobj);
264 obstack_free (&temporary_obstack, temporary_firstobj);
265 obstack_free (&momentary_obstack, momentary_firstobj);
267 maybepermanent_firstobj = p->maybepermanent_firstobj;
268 temporary_firstobj = p->temporary_firstobj;
269 momentary_firstobj = p->momentary_firstobj;
270 current_obstack = p->current_obstack;
271 expression_obstack = p->expression_obstack;
272 saveable_obstack = p->saveable_obstack;
273 rtl_obstack = p->rtl_obstack;
275 #endif
277 /* Start allocating on the temporary (per function) obstack.
278 This is done in start_function before parsing the function body,
279 and before each initialization at top level, and to go back
280 to temporary allocation after doing end_temporary_allocation. */
282 void
283 temporary_allocation ()
285 current_obstack = &temporary_obstack;
286 expression_obstack = &temporary_obstack;
287 rtl_obstack = saveable_obstack = &maybepermanent_obstack;
288 momentary_stack = 0;
291 /* Start allocating on the permanent obstack but don't
292 free the temporary data. After calling this, call
293 `permanent_allocation' to fully resume permanent allocation status. */
295 void
296 end_temporary_allocation ()
298 current_obstack = &permanent_obstack;
299 expression_obstack = &permanent_obstack;
300 rtl_obstack = saveable_obstack = &permanent_obstack;
303 /* Resume allocating on the temporary obstack, undoing
304 effects of `end_temporary_allocation'. */
306 void
307 resume_temporary_allocation ()
309 current_obstack = &temporary_obstack;
310 expression_obstack = &temporary_obstack;
311 rtl_obstack = saveable_obstack = &maybepermanent_obstack;
314 /* Nonzero if temporary allocation is currently in effect.
315 Zero if currently doing permanent allocation. */
318 allocation_temporary_p ()
320 return current_obstack == &temporary_obstack;
323 /* Go back to allocating on the permanent obstack
324 and free everything in the temporary obstack.
325 This is done in finish_function after fully compiling a function. */
327 void
328 permanent_allocation ()
330 /* Free up previous temporary obstack data */
331 obstack_free (&temporary_obstack, temporary_firstobj);
332 obstack_free (&momentary_obstack, momentary_firstobj);
333 obstack_free (&maybepermanent_obstack, maybepermanent_firstobj);
334 obstack_free (&temp_decl_obstack, temp_decl_firstobj);
336 current_obstack = &permanent_obstack;
337 expression_obstack = &permanent_obstack;
338 rtl_obstack = saveable_obstack = &permanent_obstack;
341 /* Save permanently everything on the maybepermanent_obstack. */
343 void
344 preserve_data ()
346 maybepermanent_firstobj
347 = (char *) obstack_alloc (&maybepermanent_obstack, 0);
350 void
351 preserve_initializer ()
353 temporary_firstobj
354 = (char *) obstack_alloc (&temporary_obstack, 0);
355 momentary_firstobj
356 = (char *) obstack_alloc (&momentary_obstack, 0);
357 maybepermanent_firstobj
358 = (char *) obstack_alloc (&maybepermanent_obstack, 0);
361 /* Allocate SIZE bytes in the current obstack
362 and return a pointer to them.
363 In practice the current obstack is always the temporary one. */
365 char *
366 oballoc (size)
367 int size;
369 return (char *) obstack_alloc (current_obstack, size);
372 /* Free the object PTR in the current obstack
373 as well as everything allocated since PTR.
374 In practice the current obstack is always the temporary one. */
376 void
377 obfree (ptr)
378 char *ptr;
380 obstack_free (current_obstack, ptr);
383 /* Allocate SIZE bytes in the permanent obstack
384 and return a pointer to them. */
386 char *
387 permalloc (size)
388 long size;
390 return (char *) obstack_alloc (&permanent_obstack, size);
393 /* Allocate SIZE bytes in the saveable obstack
394 and return a pointer to them. */
396 char *
397 savealloc (size)
398 int size;
400 return (char *) obstack_alloc (saveable_obstack, size);
403 /* Start a level of momentary allocation.
404 In C, each compound statement has its own level
405 and that level is freed at the end of each statement.
406 All expression nodes are allocated in the momentary allocation level. */
408 void
409 push_momentary ()
411 struct momentary_level *tem
412 = (struct momentary_level *) obstack_alloc (&momentary_obstack,
413 sizeof (struct momentary_level));
414 tem->prev = momentary_stack;
415 tem->base = (char *) obstack_base (&momentary_obstack);
416 tem->obstack = expression_obstack;
417 momentary_stack = tem;
418 expression_obstack = &momentary_obstack;
421 /* Free all the storage in the current momentary-allocation level.
422 In C, this happens at the end of each statement. */
424 void
425 clear_momentary ()
427 obstack_free (&momentary_obstack, momentary_stack->base);
430 /* Discard a level of momentary allocation.
431 In C, this happens at the end of each compound statement.
432 Restore the status of expression node allocation
433 that was in effect before this level was created. */
435 void
436 pop_momentary ()
438 struct momentary_level *tem = momentary_stack;
439 momentary_stack = tem->prev;
440 obstack_free (&momentary_obstack, tem);
441 expression_obstack = tem->obstack;
444 /* Call when starting to parse a declaration:
445 make expressions in the declaration last the length of the function.
446 Returns an argument that should be passed to resume_momentary later. */
449 suspend_momentary ()
451 register int tem = expression_obstack == &momentary_obstack;
452 expression_obstack = saveable_obstack;
453 return tem;
456 /* Call when finished parsing a declaration:
457 restore the treatment of node-allocation that was
458 in effect before the suspension.
459 YES should be the value previously returned by suspend_momentary. */
461 void
462 resume_momentary (yes)
463 int yes;
465 if (yes)
466 expression_obstack = &momentary_obstack;
469 /* Return a newly allocated node of code CODE.
470 Initialize the node's unique id and its TREE_PERMANENT flag.
471 For decl and type nodes, some other fields are initialized.
472 The rest of the node is initialized to zero.
474 Achoo! I got a code in the node. */
476 tree
477 make_node (code)
478 enum tree_code code;
480 register tree t;
481 register int type = *tree_code_type[(int) code];
482 register int length;
483 register struct obstack *obstack = current_obstack;
484 register int i;
485 register tree_node_kind kind;
487 switch (type)
489 case 'd': /* A decl node */
490 length = sizeof (struct tree_decl)
491 + tree_code_length[(int) code] * sizeof (char *);
492 kind = d_kind;
493 /* All decls in an inline function need to be saved. */
494 if (obstack != &permanent_obstack)
495 obstack = saveable_obstack;
496 /* PARM_DECLs always go on the saveable_obstack, not permanent
497 even though we may make them before the function turns
498 on temporary allocation. */
499 else if (code == PARM_DECL)
500 obstack = &maybepermanent_obstack;
501 break;
503 case 't': /* a type node */
504 length = sizeof (struct tree_type);
505 kind = t_kind;
506 /* All data types are put where we can preserve them if nec. */
507 if (obstack != &permanent_obstack)
508 obstack = all_types_permanent ? &permanent_obstack : saveable_obstack;
509 break;
511 case 's': /* a stmt node */
512 length = sizeof (struct tree_common)
513 + 2 * sizeof (int)
514 + tree_code_length[(int) code] * sizeof (char *);
515 kind = s_kind;
516 /* All stmts are put where we can preserve them if nec. */
517 if (obstack != &permanent_obstack)
518 obstack = saveable_obstack;
519 break;
521 case 'r': /* a reference */
522 obstack = expression_obstack;
523 length = sizeof (struct tree_exp)
524 + (tree_code_length[(int) code] - 1) * sizeof (char *);
525 kind = r_kind;
526 break;
528 case 'e': /* an expression */
529 obstack = expression_obstack;
530 length = sizeof (struct tree_exp)
531 + (tree_code_length[(int) code] - 1) * sizeof (char *);
532 kind = e_kind;
533 break;
535 case 'c': /* a constant */
536 obstack = expression_obstack;
537 /* We can't use tree_code_length for this, since the number of words
538 is machine-dependent due to varying alignment of `double'. */
539 if (code == REAL_CST)
541 length = sizeof (struct tree_real_cst);
542 kind = c_kind;
543 break;
545 length = sizeof (struct tree_common)
546 + tree_code_length[(int) code] * sizeof (char *);
547 kind = c_kind;
548 break;
550 case 'x': /* something random, like an identifier. */
551 length = sizeof (struct tree_common)
552 + tree_code_length[(int) code] * sizeof (char *);
553 /* Identifier nodes are always permanent since they are
554 unique in a compiler run. */
555 if (code == IDENTIFIER_NODE)
557 kind = id_kind;
558 obstack = &permanent_obstack;
560 else if (code == OP_IDENTIFIER)
561 kind = op_id_kind;
562 else
563 kind = x_kind;
566 t = (tree) obstack_alloc (obstack, length);
568 #ifdef GATHER_STATISTICS
569 tree_node_kinds[(int)kind]++;
570 tree_node_sizes[(int)kind] += length;
571 #endif
573 TREE_UID (t) = tree_node_counter++;
574 TREE_TYPE (t) = 0;
575 TREE_CHAIN (t) = 0;
576 for (i = (length / sizeof (int)) - 1;
577 i >= sizeof (struct tree_common) / sizeof (int) - 1;
578 i--)
579 ((int *) t)[i] = 0;
581 TREE_SET_CODE (t, code);
582 if (obstack == &permanent_obstack)
583 TREE_PERMANENT (t) = 1;
585 if (type == 'd')
587 extern int lineno;
589 DECL_ALIGN (t) = 1;
590 DECL_SIZE_UNIT (t) = 1;
591 DECL_VOFFSET_UNIT (t) = 1;
592 if (code == PARM_DECL)
593 DECL_CONTEXT (t) = current_function_decl;
594 else
596 DECL_SOURCE_LINE (t) = lineno;
597 DECL_SOURCE_FILE (t)
598 = (input_filename) ? input_filename : "<built-in>";
602 if (type == 't')
604 TYPE_ALIGN (t) = 1;
605 TYPE_SIZE_UNIT (t) = 1;
606 TYPE_MAIN_VARIANT (t) = t;
609 if (type == 'c')
611 TREE_LITERAL (t) = 1;
614 return t;
617 /* Return a new node with the same contents as NODE
618 except that its TREE_CHAIN is zero and it has a fresh uid. */
620 tree
621 copy_node (node)
622 tree node;
624 register tree t;
625 register enum tree_code code = TREE_CODE (node);
626 register int length;
627 register int i;
629 switch (*tree_code_type[(int) code])
631 case 'd': /* A decl node */
632 length = sizeof (struct tree_decl)
633 + tree_code_length[(int) code] * sizeof (char *);
634 break;
636 case 't': /* a type node */
637 length = sizeof (struct tree_type);
638 break;
640 case 's':
641 length = sizeof (struct tree_common)
642 + 2 * sizeof (int)
643 + tree_code_length[(int) code] * sizeof (char *);
644 break;
646 case 'r': /* a reference */
647 case 'e': /* a expression */
648 length = sizeof (struct tree_exp)
649 + (tree_code_length[(int) code] - 1) * sizeof (char *);
650 break;
652 case 'c': /* a constant */
653 /* We can't use tree_code_length for this, since the number of words
654 is machine-dependent due to varying alignment of `double'. */
655 if (code == REAL_CST)
657 length = sizeof (struct tree_real_cst);
658 break;
661 case 'x': /* something random, like an identifier. */
662 length = sizeof (struct tree_common)
663 + tree_code_length[(int) code] * sizeof (char *);
664 if (code == TREE_VEC)
665 length += (TREE_VEC_LENGTH (node) - 1) * sizeof (char *);
668 t = (tree) obstack_alloc (current_obstack, length);
670 for (i = ((length + sizeof (int) - 1) / sizeof (int)) - 1;
671 i >= 0;
672 i--)
673 ((int *) t)[i] = ((int *) node)[i];
675 TREE_UID (t) = tree_node_counter++;
676 TREE_CHAIN (t) = 0;
678 TREE_PERMANENT (t) = (current_obstack == &permanent_obstack);
680 return t;
683 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
684 For example, this can copy a list made of TREE_LIST nodes. */
686 tree
687 copy_list (list)
688 tree list;
690 tree head;
691 register tree prev, next;
693 if (list == 0)
694 return 0;
696 head = prev = copy_node (list);
697 next = TREE_CHAIN (list);
698 while (next)
700 TREE_CHAIN (prev) = copy_node (next);
701 prev = TREE_CHAIN (prev);
702 next = TREE_CHAIN (next);
704 return head;
707 #define HASHBITS 30
709 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
710 If an identifier with that name has previously been referred to,
711 the same node is returned this time. */
713 tree
714 get_identifier (text)
715 register char *text;
717 register int hi;
718 register int i;
719 register tree idp;
720 register int len, hash_len;
722 /* Compute length of text in len. */
723 for (len = 0; text[len]; len++);
725 /* Decide how much of that length to hash on */
726 hash_len = len;
727 if (warn_id_clash && len > id_clash_len)
728 hash_len = id_clash_len;
730 /* Compute hash code */
731 hi = 17 * (unsigned)(text[0]) + len;
732 for (i = 1; i < hash_len; i += 2)
733 hi = ((hi * 613) + (unsigned)(text[i]));
735 hi &= (1 << HASHBITS) - 1;
736 hi %= MAX_HASH_TABLE;
738 /* Search table for identifier */
739 for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
740 if (IDENTIFIER_LENGTH (idp) == len
741 && IDENTIFIER_POINTER (idp)[0] == text[0]
742 && !bcmp (IDENTIFIER_POINTER (idp), text, len))
743 return idp; /* <-- return if found */
745 /* Not found; optionally warn about a similar identifier */
746 if (warn_id_clash && do_identifier_warnings && len > id_clash_len)
747 for (idp = hash_table[hi]; idp; idp = TREE_CHAIN (idp))
748 if (!strncmp (IDENTIFIER_POINTER (idp), text, id_clash_len))
750 warning ("`%s' and `%s' identical in first n characters",
751 IDENTIFIER_POINTER (idp), text);
752 break;
755 if (tree_code_length[(int) IDENTIFIER_NODE] < 0)
756 abort (); /* set_identifier_size hasn't been called. */
758 /* Not found, create one, add to chain */
759 idp = make_node (IDENTIFIER_NODE);
760 IDENTIFIER_LENGTH (idp) = len;
761 id_string_size += len;
763 IDENTIFIER_POINTER (idp) = obstack_copy0 (&permanent_obstack, text, len);
765 TREE_CHAIN (idp) = hash_table[hi];
766 hash_table[hi] = idp;
767 return idp; /* <-- return if created */
770 /* Enable warnings on similar identifiers (if requested).
771 Done after the built-in identifiers are created. */
773 void
774 start_identifier_warnings ()
776 do_identifier_warnings = 1;
779 /* Record the size of an identifier node for the language in use.
780 SIZE is the total size in bytes.
781 This is called by the language-specific files. This must be
782 called before allocating any identifiers. */
784 void
785 set_identifier_size (size)
786 int size;
788 tree_code_length[(int) IDENTIFIER_NODE]
789 = (size - sizeof (struct tree_common)) / sizeof (tree);
792 /* Return a newly constructed INTEGER_CST node whose constant value
793 is specified by the two ints LOW and HI.
794 The TREE_TYPE is set to `int'. */
796 tree
797 build_int_2 (low, hi)
798 int low, hi;
800 register tree t = make_node (INTEGER_CST);
801 TREE_INT_CST_LOW (t) = low;
802 TREE_INT_CST_HIGH (t) = hi;
803 TREE_TYPE (t) = integer_type_node;
804 return t;
807 /* Return a new REAL_CST node whose type is TYPE and value is D. */
809 tree
810 build_real (type, d)
811 tree type;
812 REAL_VALUE_TYPE d;
814 tree v;
816 /* Check for valid float value for this type on this target machine;
817 if not, can print error message and store a valid value in D. */
818 #ifdef CHECK_FLOAT_VALUE
819 CHECK_FLOAT_VALUE (TYPE_MODE (type), d);
820 #endif
822 v = make_node (REAL_CST);
823 TREE_TYPE (v) = type;
824 TREE_REAL_CST (v) = d;
825 return v;
828 /* Return a new REAL_CST node whose type is TYPE
829 and whose value is the integer value of the INTEGER_CST node I. */
831 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
833 REAL_VALUE_TYPE
834 real_value_from_int_cst (i)
835 tree i;
837 REAL_VALUE_TYPE d;
838 #ifdef REAL_ARITHMETIC
839 REAL_VALUE_FROM_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i));
840 #else /* not REAL_ARITHMETIC */
841 if (TREE_INT_CST_HIGH (i) < 0)
843 d = (double) (~ TREE_INT_CST_HIGH (i));
844 d *= ((double) (1 << (HOST_BITS_PER_INT / 2))
845 * (double) (1 << (HOST_BITS_PER_INT / 2)));
846 d += (double) (unsigned) (~ TREE_INT_CST_LOW (i));
847 d = (- d - 1.0);
849 else
851 d = (double) TREE_INT_CST_HIGH (i);
852 d *= ((double) (1 << (HOST_BITS_PER_INT / 2))
853 * (double) (1 << (HOST_BITS_PER_INT / 2)));
854 d += (double) (unsigned) TREE_INT_CST_LOW (i);
856 #endif /* not REAL_ARITHMETIC */
857 return d;
860 /* This function can't be implemented if we can't do arithmetic
861 on the float representation. */
863 tree
864 build_real_from_int_cst (type, i)
865 tree type;
866 tree i;
868 tree v;
869 REAL_VALUE_TYPE d;
871 v = make_node (REAL_CST);
872 TREE_TYPE (v) = type;
874 d = real_value_from_int_cst (i);
875 /* Check for valid float value for this type on this target machine;
876 if not, can print error message and store a valid value in D. */
877 #ifdef CHECK_FLOAT_VALUE
878 CHECK_FLOAT_VALUE (TYPE_MODE (type), d);
879 #endif
881 TREE_REAL_CST (v) = d;
882 return v;
885 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
887 /* Return a newly constructed STRING_CST node whose value is
888 the LEN characters at STR.
889 The TREE_TYPE is not initialized. */
891 tree
892 build_string (len, str)
893 int len;
894 char *str;
896 register tree s = make_node (STRING_CST);
897 TREE_STRING_LENGTH (s) = len;
898 TREE_STRING_POINTER (s) = obstack_copy0 (saveable_obstack, str, len);
899 return s;
902 /* Return a newly constructed COMPLEX_CST node whose value is
903 specified by the real and imaginary parts REAL and IMAG.
904 Both REAL and IMAG should be constant nodes.
905 The TREE_TYPE is not initialized. */
907 tree
908 build_complex (real, imag)
909 tree real, imag;
911 register tree t = make_node (COMPLEX_CST);
912 TREE_REALPART (t) = real;
913 TREE_IMAGPART (t) = imag;
914 return t;
917 /* Build a newly constructed TREE_VEC node of length LEN. */
918 tree
919 make_tree_vec (len)
920 int len;
922 register tree t;
923 register int length = (len-1) * sizeof (tree) + sizeof (struct tree_vec);
924 register struct obstack *obstack = current_obstack;
925 register int i;
927 #ifdef GATHER_STATISTICS
928 tree_node_kinds[(int)x_kind]++;
929 tree_node_sizes[(int)x_kind] += length;
930 #endif
932 t = (tree) obstack_alloc (obstack, length);
934 TREE_UID (t) = tree_node_counter++;
935 TREE_TYPE (t) = 0;
936 TREE_CHAIN (t) = 0;
937 for (i = (length / sizeof (int)) - 1;
938 i >= sizeof (struct tree_common) / sizeof (int) - 1;
939 i--)
940 ((int *) t)[i] = 0;
941 TREE_SET_CODE (t, TREE_VEC);
942 TREE_VEC_LENGTH (t) = len;
943 if (obstack == &permanent_obstack)
944 TREE_PERMANENT (t) = 1;
946 return t;
949 /* Return 1 if EXPR is the integer constant zero. */
952 integer_zerop (expr)
953 tree expr;
955 return (TREE_CODE (expr) == INTEGER_CST
956 && TREE_INT_CST_LOW (expr) == 0
957 && TREE_INT_CST_HIGH (expr) == 0);
960 /* Return 1 if EXPR is the integer constant one. */
963 integer_onep (expr)
964 tree expr;
966 return (TREE_CODE (expr) == INTEGER_CST
967 && TREE_INT_CST_LOW (expr) == 1
968 && TREE_INT_CST_HIGH (expr) == 0);
971 /* Return 1 if EXPR is an integer containing all 1's
972 in as much precision as it contains. */
975 integer_all_onesp (expr)
976 tree expr;
978 register int prec;
979 register int uns;
981 if (TREE_CODE (expr) != INTEGER_CST)
982 return 0;
984 uns = TREE_UNSIGNED (TREE_TYPE (expr));
985 if (!uns)
986 return TREE_INT_CST_LOW (expr) == -1 && TREE_INT_CST_HIGH (expr) == -1;
988 prec = TYPE_PRECISION (TREE_TYPE (expr));
989 if (prec >= HOST_BITS_PER_INT)
990 return TREE_INT_CST_LOW (expr) == -1
991 && TREE_INT_CST_HIGH (expr) == (1 << (prec - HOST_BITS_PER_INT)) - 1;
992 else
993 return TREE_INT_CST_LOW (expr) == (1 << prec) - 1;
996 /* Return list element whose TREE_VALUE is ELEM.
997 Return 0 if ELEM is not in LIST. */
998 tree
999 value_member (elem, list)
1000 tree elem, list;
1002 while (list)
1004 if (elem == TREE_VALUE (list))
1005 return list;
1006 list = TREE_CHAIN (list);
1008 return NULL_TREE;
1011 /* Return list element whose TREE_PURPOSE is ELEM.
1012 Return 0 if ELEM is not in LIST. */
1013 tree
1014 purpose_member (elem, list)
1015 tree elem, list;
1017 while (list)
1019 if (elem == TREE_PURPOSE (list))
1020 return list;
1021 list = TREE_CHAIN (list);
1023 return NULL_TREE;
1026 /* Return the length of a chain of nodes chained through TREE_CHAIN.
1027 We expect a null pointer to mark the end of the chain.
1028 This is the Lisp primitive `length'. */
1031 list_length (t)
1032 tree t;
1034 register tree tail;
1035 register int len = 0;
1037 for (tail = t; tail; tail = TREE_CHAIN (tail))
1038 len++;
1040 return len;
1043 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
1044 by modifying the last node in chain 1 to point to chain 2.
1045 This is the Lisp primitive `nconc'. */
1047 tree
1048 chainon (op1, op2)
1049 tree op1, op2;
1051 tree t;
1053 if (op1)
1055 for (t = op1; TREE_CHAIN (t); t = TREE_CHAIN (t))
1056 if (t == op2) abort (); /* Circularity being created */
1057 TREE_CHAIN (t) = op2;
1058 return op1;
1060 else return op2;
1063 /* Return a newly created TREE_LIST node whose
1064 purpose and value fields are PARM and VALUE. */
1066 tree
1067 build_tree_list (parm, value)
1068 tree parm, value;
1070 #if 0
1071 register tree t = make_node (TREE_LIST);
1072 #else
1073 register tree t;
1074 register struct obstack *obstack = current_obstack;
1075 register int i;
1077 t = (tree) obstack_alloc (obstack, sizeof (struct tree_list));
1078 TREE_UID (t) = tree_node_counter++;
1079 TREE_TYPE (t) = 0;
1080 TREE_CHAIN (t) = 0;
1081 ((int *) t)[3] = 0;
1083 TREE_SET_CODE (t, TREE_LIST);
1084 if (obstack == &permanent_obstack)
1086 TREE_PERMANENT (t) = 1;
1087 #ifdef GATHER_STATISTICS
1088 tree_node_kinds[(int)perm_list_kind]++;
1089 tree_node_sizes[(int)perm_list_kind] += sizeof (struct tree_list);
1090 #endif
1092 else
1094 #ifdef GATHER_STATISTICS
1095 tree_node_kinds[(int)temp_list_kind]++;
1096 tree_node_sizes[(int)temp_list_kind] += sizeof (struct tree_list);
1097 #endif
1099 #endif
1101 TREE_PURPOSE (t) = parm;
1102 TREE_VALUE (t) = value;
1103 return t;
1106 /* Similar, but build on the temp_decl_obstack. */
1107 tree
1108 build_decl_list (parm, value)
1109 tree parm, value;
1111 register tree node;
1112 register struct obstack *ambient_obstack = current_obstack;
1113 current_obstack = &temp_decl_obstack;
1114 node = build_tree_list (parm, value);
1115 current_obstack = ambient_obstack;
1116 return node;
1119 /* Return a newly created TREE_LIST node whose
1120 purpose and value fields are PARM and VALUE
1121 and whose TREE_CHAIN is CHAIN. */
1123 tree
1124 tree_cons (purpose, value, chain)
1125 tree purpose, value, chain;
1127 #if 0
1128 register tree t = make_node (TREE_LIST);
1129 #else
1130 register tree t;
1131 register struct obstack *obstack = current_obstack;
1132 register int i;
1134 t = (tree) obstack_alloc (obstack, sizeof (struct tree_list));
1135 TREE_UID (t) = tree_node_counter++;
1136 TREE_TYPE (t) = 0;
1137 ((int *) t)[3] = 0;
1139 TREE_SET_CODE (t, TREE_LIST);
1140 if (obstack == &permanent_obstack)
1142 TREE_PERMANENT (t) = 1;
1143 #ifdef GATHER_STATISTICS
1144 tree_node_kinds[(int)perm_list_kind]++;
1145 tree_node_sizes[(int)perm_list_kind] += sizeof (struct tree_list);
1146 #endif
1148 else
1150 #ifdef GATHER_STATISTICS
1151 tree_node_kinds[(int)temp_list_kind]++;
1152 tree_node_sizes[(int)temp_list_kind] += sizeof (struct tree_list);
1153 #endif
1155 #endif
1157 TREE_CHAIN (t) = chain;
1158 TREE_PURPOSE (t) = purpose;
1159 TREE_VALUE (t) = value;
1160 return t;
1163 /* Similar, but build on the temp_decl_obstack. */
1164 tree
1165 decl_tree_cons (purpose, value, chain)
1166 tree purpose, value, chain;
1168 register tree node;
1169 register struct obstack *ambient_obstack = current_obstack;
1170 current_obstack = &temp_decl_obstack;
1172 node = tree_cons (purpose, value, chain);
1173 current_obstack = ambient_obstack;
1174 return node;
1177 /* Same as `tree_cons' but make a permanent object. */
1179 tree
1180 perm_tree_cons (purpose, value, chain)
1181 tree purpose, value, chain;
1183 register tree node;
1184 register struct obstack *ambient_obstack = current_obstack;
1185 current_obstack = &permanent_obstack;
1187 node = tree_cons (purpose, value, chain);
1188 current_obstack = ambient_obstack;
1189 return node;
1192 /* Same as `tree_cons', but make this node temporary, regardless. */
1194 tree
1195 temp_tree_cons (purpose, value, chain)
1196 tree purpose, value, chain;
1198 register tree node;
1199 register struct obstack *ambient_obstack = current_obstack;
1200 current_obstack = &temporary_obstack;
1202 node = tree_cons (purpose, value, chain);
1203 current_obstack = ambient_obstack;
1204 return node;
1207 /* Same as `tree_cons', but save this node if the function's RTL is saved. */
1209 tree
1210 saveable_tree_cons (purpose, value, chain)
1211 tree purpose, value, chain;
1213 register tree node;
1214 register struct obstack *ambient_obstack = current_obstack;
1215 current_obstack = saveable_obstack;
1217 node = tree_cons (purpose, value, chain);
1218 current_obstack = ambient_obstack;
1219 return node;
1222 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
1224 tree
1225 tree_last (chain)
1226 register tree chain;
1228 register tree next;
1229 if (chain)
1230 while (next = TREE_CHAIN (chain))
1231 chain = next;
1232 return chain;
1235 /* Reverse the order of elements in the chain T,
1236 and return the new head of the chain (old last element). */
1238 tree
1239 nreverse (t)
1240 tree t;
1242 register tree prev = 0, decl, next;
1243 for (decl = t; decl; decl = next)
1245 next = TREE_CHAIN (decl);
1246 TREE_CHAIN (decl) = prev;
1247 prev = decl;
1249 return prev;
1252 /* Given a chain CHAIN of tree nodes,
1253 construct and return a list of those nodes. */
1255 tree
1256 listify (chain)
1257 tree chain;
1259 tree result = NULL_TREE;
1260 tree in_tail = chain;
1261 tree out_tail = NULL_TREE;
1263 while (in_tail)
1265 tree next = tree_cons (NULL_TREE, in_tail, NULL_TREE);
1266 if (out_tail)
1267 TREE_CHAIN (out_tail) = next;
1268 else
1269 result = next;
1270 out_tail = next;
1271 in_tail = TREE_CHAIN (in_tail);
1274 return result;
1277 /* Return the size nominally occupied by an object of type TYPE
1278 when it resides in memory. The value is measured in units of bytes,
1279 and its data type is that normally used for type sizes
1280 (which is the first type created by make_signed_type or
1281 make_unsigned_type). */
1283 tree
1284 size_in_bytes (type)
1285 tree type;
1287 if (type == error_mark_node)
1288 return integer_zero_node;
1289 type = TYPE_MAIN_VARIANT (type);
1290 if (TYPE_SIZE (type) == 0)
1292 incomplete_type_error (0, type);
1293 return integer_zero_node;
1295 return convert_units (TYPE_SIZE (type), TYPE_SIZE_UNIT (type),
1296 BITS_PER_UNIT);
1299 /* Return the size of TYPE (in bytes) as an integer,
1300 or return -1 if the size can vary. */
1303 int_size_in_bytes (type)
1304 tree type;
1306 int size;
1307 if (type == error_mark_node)
1308 return 0;
1309 type = TYPE_MAIN_VARIANT (type);
1310 if (TYPE_SIZE (type) == 0)
1311 return -1;
1312 if (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1313 return -1;
1314 size = TREE_INT_CST_LOW (TYPE_SIZE (type)) * TYPE_SIZE_UNIT (type);
1315 return (size + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1318 /* Return, as an INTEGER_CST node, the number of elements for
1319 TYPE (which is an ARRAY_TYPE). */
1321 tree
1322 array_type_nelts (type)
1323 tree type;
1325 tree index_type = TYPE_DOMAIN (type);
1326 if (index_type == NULL_TREE)
1328 incomplete_type_error (NULL_TREE, type);
1329 return error_mark_node;
1331 return (tree_int_cst_equal (TYPE_MIN_VALUE (index_type), integer_zero_node)
1332 ? TYPE_MAX_VALUE (index_type)
1333 : fold (build (MINUS_EXPR, integer_type_node,
1334 TYPE_MAX_VALUE (index_type),
1335 TYPE_MIN_VALUE (index_type))));
1338 /* Return nonzero if arg is static -- a reference to an object in
1339 static storage. This is not the same as the C meaning of `static'. */
1342 staticp (arg)
1343 tree arg;
1345 register enum tree_code code = TREE_CODE (arg);
1347 if ((code == VAR_DECL || code == FUNCTION_DECL || code == CONSTRUCTOR)
1348 && (TREE_STATIC (arg) || TREE_EXTERNAL (arg)))
1349 return 1;
1351 if (code == STRING_CST)
1352 return 1;
1354 if (code == COMPONENT_REF)
1355 return (DECL_VOFFSET (TREE_OPERAND (arg, 1)) == 0
1356 && staticp (TREE_OPERAND (arg, 0)));
1358 if (code == INDIRECT_REF)
1359 return TREE_LITERAL (TREE_OPERAND (arg, 0));
1361 if (code == ARRAY_REF)
1363 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
1364 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
1365 return staticp (TREE_OPERAND (arg, 0));
1368 return 0;
1371 /* This should be applied to any node which may be used in more than one place,
1372 but must be evaluated only once. Normally, the code generator would
1373 reevaluate the node each time; this forces it to compute it once and save
1374 the result. This is done by encapsulating the node in a SAVE_EXPR. */
1376 tree
1377 save_expr (expr)
1378 tree expr;
1380 register tree t = fold (expr);
1382 /* If the tree evaluates to a constant, then we don't want to hide that
1383 fact (i.e. this allows further folding, and direct checks for constants).
1384 Since it is no problem to reevaluate literals, we just return the
1385 literal node. */
1387 if (TREE_LITERAL (t) || TREE_READONLY (t) || TREE_CODE (t) == SAVE_EXPR)
1388 return t;
1390 return build (SAVE_EXPR, TREE_TYPE (expr), t, NULL);
1393 /* Stabilize a reference so that we can use it any number of times
1394 without causing its operands to be evaluated more than once.
1395 Returns the stabilized reference.
1397 Also allows conversion expressions whose operands are references.
1398 Any other kind of expression is returned unchanged. */
1400 tree
1401 stabilize_reference (ref)
1402 tree ref;
1404 register tree result;
1405 register enum tree_code code = TREE_CODE (ref);
1407 switch (code)
1409 case VAR_DECL:
1410 case PARM_DECL:
1411 case RESULT_DECL:
1412 result = ref;
1413 break;
1415 case NOP_EXPR:
1416 case CONVERT_EXPR:
1417 case FLOAT_EXPR:
1418 case FIX_TRUNC_EXPR:
1419 case FIX_FLOOR_EXPR:
1420 case FIX_ROUND_EXPR:
1421 case FIX_CEIL_EXPR:
1422 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
1423 break;
1425 case INDIRECT_REF:
1426 result = build_nt (INDIRECT_REF, save_expr (TREE_OPERAND (ref, 0)));
1427 break;
1429 case COMPONENT_REF:
1430 result = build_nt (COMPONENT_REF,
1431 stabilize_reference (TREE_OPERAND (ref, 0)),
1432 TREE_OPERAND (ref, 1));
1433 break;
1435 case ARRAY_REF:
1436 result = build_nt (ARRAY_REF, stabilize_reference (TREE_OPERAND (ref, 0)),
1437 save_expr (TREE_OPERAND (ref, 1)));
1438 break;
1440 /* If arg isn't a kind of lvalue we recognize, make no change.
1441 Caller should recognize the error for an invalid lvalue. */
1442 default:
1443 return ref;
1445 case ERROR_MARK:
1446 return error_mark_node;
1449 TREE_TYPE (result) = TREE_TYPE (ref);
1450 TREE_READONLY (result) = TREE_READONLY (ref);
1451 TREE_VOLATILE (result) = TREE_VOLATILE (ref);
1452 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
1453 TREE_RAISES (result) = TREE_RAISES (ref);
1455 return result;
1458 /* Low-level constructors for expressions. */
1460 /* Build an expression of code CODE, data type TYPE,
1461 and operands as specified by the arguments ARG1 and following arguments.
1462 Expressions and reference nodes can be created this way.
1463 Constants, decls, types and misc nodes cannot be. */
1465 tree
1466 build (va_alist)
1467 va_dcl
1469 register va_list p;
1470 enum tree_code code;
1471 register tree t;
1472 register int length;
1473 register int i;
1475 va_start (p);
1477 code = va_arg (p, enum tree_code);
1478 t = make_node (code);
1479 length = tree_code_length[(int) code];
1480 TREE_TYPE (t) = va_arg (p, tree);
1482 if (length == 2)
1484 /* This is equivalent to the loop below, but faster. */
1485 register tree arg0 = va_arg (p, tree);
1486 register tree arg1 = va_arg (p, tree);
1487 TREE_OPERAND (t, 0) = arg0;
1488 TREE_OPERAND (t, 1) = arg1;
1489 TREE_VOLATILE (t)
1490 = (arg0 && TREE_VOLATILE (arg0)) || (arg1 && TREE_VOLATILE (arg1));
1491 TREE_RAISES (t)
1492 = (arg0 && TREE_RAISES (arg0)) || (arg1 && TREE_RAISES (arg1));
1494 else
1496 for (i = 0; i < length; i++)
1498 register tree operand = va_arg (p, tree);
1499 TREE_OPERAND (t, i) = operand;
1500 if (operand)
1502 if (TREE_VOLATILE (operand))
1503 TREE_VOLATILE (t) = 1;
1504 if (TREE_RAISES (operand))
1505 TREE_RAISES (t) = 1;
1509 va_end (p);
1510 return t;
1513 /* Same as above, but only builds for unary operators.
1514 Saves lions share of calls to `build'; cuts down use
1515 of varargs, which is expensive for RISC machines. */
1516 tree
1517 build1 (code, type, node)
1518 enum tree_code code;
1519 tree type;
1520 tree node;
1522 register struct obstack *obstack = current_obstack;
1523 register int i, length;
1524 register tree_node_kind kind;
1525 register tree t;
1527 if (*tree_code_type[(int) code] == 'r')
1528 kind = r_kind;
1529 else if (*tree_code_type[(int) code] == 'e')
1530 kind = e_kind;
1531 else
1532 abort ();
1534 obstack = expression_obstack;
1535 length = sizeof (struct tree_exp);
1537 t = (tree) obstack_alloc (obstack, length);
1539 #ifdef GATHER_STATISTICS
1540 tree_node_kinds[(int)kind]++;
1541 tree_node_sizes[(int)kind] += length;
1542 #endif
1544 TREE_UID (t) = tree_node_counter++;
1545 TREE_TYPE (t) = type;
1546 TREE_CHAIN (t) = 0;
1548 for (i = (length / sizeof (int)) - 2;
1549 i >= sizeof (struct tree_common) / sizeof (int) - 1;
1550 i--)
1551 ((int *) t)[i] = 0;
1552 TREE_SET_CODE (t, code);
1554 if (obstack == &permanent_obstack)
1555 TREE_PERMANENT (t) = 1;
1557 TREE_OPERAND (t, 0) = node;
1558 if (node)
1560 if (TREE_VOLATILE (node))
1561 TREE_VOLATILE (t) = 1;
1562 if (TREE_RAISES (node))
1563 TREE_RAISES (t) = 1;
1565 return t;
1568 /* Similar except don't specify the TREE_TYPE
1569 and leave the TREE_VOLATILE as 0.
1570 It is permissible for arguments to be null,
1571 or even garbage if their values do not matter. */
1573 tree
1574 build_nt (va_alist)
1575 va_dcl
1577 register va_list p;
1578 register enum tree_code code;
1579 register tree t;
1580 register int length;
1581 register int i;
1583 va_start (p);
1585 code = va_arg (p, enum tree_code);
1586 t = make_node (code);
1587 length = tree_code_length[(int) code];
1589 for (i = 0; i < length; i++)
1590 TREE_OPERAND (t, i) = va_arg (p, tree);
1592 va_end (p);
1593 return t;
1596 /* Similar to `build_nt', except we build
1597 on the temp_decl_obstack, regardless. */
1599 tree
1600 build_parse_node (va_alist)
1601 va_dcl
1603 register struct obstack *ambient_obstack = expression_obstack;
1604 register va_list p;
1605 register enum tree_code code;
1606 register tree t;
1607 register int length;
1608 register int i;
1610 expression_obstack = &temp_decl_obstack;
1612 va_start (p);
1614 code = va_arg (p, enum tree_code);
1615 t = make_node (code);
1616 length = tree_code_length[(int) code];
1618 for (i = 0; i < length; i++)
1619 TREE_OPERAND (t, i) = va_arg (p, tree);
1621 va_end (p);
1622 expression_obstack = ambient_obstack;
1623 return t;
1626 #if 0
1627 /* Commented out because this wants to be done very
1628 differently. See cplus-lex.c. */
1629 tree
1630 build_op_identifier (op1, op2)
1631 tree op1, op2;
1633 register tree t = make_node (OP_IDENTIFIER);
1634 TREE_PURPOSE (t) = op1;
1635 TREE_VALUE (t) = op2;
1636 return t;
1638 #endif
1640 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
1641 We do NOT enter this node in any sort of symbol table.
1643 layout_decl is used to set up the decl's storage layout.
1644 Other slots are initialized to 0 or null pointers. */
1646 tree
1647 build_decl (code, name, type)
1648 enum tree_code code;
1649 tree name, type;
1651 register tree t;
1653 t = make_node (code);
1655 /* if (type == error_mark_node)
1656 type = integer_type_node; */
1657 /* That is not done, deliberately, so that having error_mark_node
1658 as the type can suppress useless errors in the use of this variable. */
1660 DECL_NAME (t) = name;
1661 if (name)
1663 #if 0
1664 DECL_PRINT_NAME (t) = IDENTIFIER_POINTER (name);
1665 #endif
1666 if (code != PARM_DECL)
1667 DECL_ASSEMBLER_NAME (t) = IDENTIFIER_POINTER (name);
1669 TREE_TYPE (t) = type;
1671 /* A freshly built node has these properties anyway. */
1672 #if 0
1673 DECL_ARGUMENTS (t) = NULL_TREE;
1674 DECL_INITIAL (t) = NULL_TREE;
1675 #endif /* 0 */
1677 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
1678 layout_decl (t, 0);
1679 else if (code == FUNCTION_DECL)
1680 DECL_MODE (t) = FUNCTION_MODE;
1682 return t;
1685 /* Low-level constructors for statements.
1686 These constructors all expect source file name and line number
1687 as arguments, as well as enough arguments to fill in the data
1688 in the statement node. */
1690 tree
1691 build_goto (filename, line, label)
1692 char *filename;
1693 int line;
1694 tree label;
1696 register tree t = make_node (GOTO_STMT);
1697 STMT_SOURCE_FILE (t) = filename;
1698 STMT_SOURCE_LINE (t) = line;
1699 STMT_BODY (t) = label;
1700 return t;
1703 tree
1704 build_return (filename, line, arg)
1705 char *filename;
1706 int line;
1707 tree arg;
1709 register tree t = make_node (RETURN_STMT);
1711 STMT_SOURCE_FILE (t) = filename;
1712 STMT_SOURCE_LINE (t) = line;
1713 STMT_BODY (t) = arg;
1714 return t;
1717 tree
1718 build_expr_stmt (filename, line, expr)
1719 char *filename;
1720 int line;
1721 tree expr;
1723 register tree t = make_node (EXPR_STMT);
1725 STMT_SOURCE_FILE (t) = filename;
1726 STMT_SOURCE_LINE (t) = line;
1727 STMT_BODY (t) = expr;
1728 return t;
1731 tree
1732 build_if (filename, line, cond, thenclause, elseclause)
1733 char *filename;
1734 int line;
1735 tree cond, thenclause, elseclause;
1737 register tree t = make_node (IF_STMT);
1739 STMT_SOURCE_FILE (t) = filename;
1740 STMT_SOURCE_LINE (t) = line;
1741 STMT_COND (t) = cond;
1742 STMT_THEN (t) = thenclause;
1743 STMT_ELSE (t) = elseclause;
1744 return t;
1747 tree
1748 build_exit (filename, line, cond)
1749 char *filename;
1750 int line;
1751 tree cond;
1753 register tree t = make_node (EXIT_STMT);
1754 STMT_SOURCE_FILE (t) = filename;
1755 STMT_SOURCE_LINE (t) = line;
1756 STMT_BODY (t) = cond;
1757 return t;
1760 tree
1761 build_asm_stmt (filename, line, asmcode)
1762 char *filename;
1763 int line;
1764 tree asmcode;
1766 register tree t = make_node (ASM_STMT);
1767 STMT_SOURCE_FILE (t) = filename;
1768 STMT_SOURCE_LINE (t) = line;
1769 STMT_BODY (t) = asmcode;
1770 return t;
1773 tree
1774 build_case (filename, line, object, cases)
1775 char *filename;
1776 int line;
1777 tree object, cases;
1779 register tree t = make_node (CASE_STMT);
1780 STMT_SOURCE_FILE (t) = filename;
1781 STMT_SOURCE_LINE (t) = line;
1782 STMT_CASE_INDEX (t) = object;
1783 STMT_CASE_LIST (t) = cases;
1784 return t;
1787 tree
1788 build_compound (filename, line, body)
1789 char *filename;
1790 int line;
1791 tree body;
1793 register tree t = make_node (COMPOUND_STMT);
1794 STMT_SOURCE_FILE (t) = filename;
1795 STMT_SOURCE_LINE (t) = line;
1796 STMT_BODY (t) = body;
1797 return t;
1800 tree
1801 build_loop (filename, line, vars, cond, body)
1802 char *filename;
1803 int line;
1804 tree vars, cond, body;
1806 register tree t = make_node (LOOP_STMT);
1807 STMT_SOURCE_FILE (t) = filename;
1808 STMT_SOURCE_LINE (t) = line;
1809 STMT_LOOP_VARS (t) = vars;
1810 STMT_LOOP_COND (t) = cond;
1811 STMT_LOOP_BODY (t) = body;
1812 return t;
1815 /* LET_STMT nodes are used to represent the structure of binding contours
1816 and declarations, once those contours have been exited and their contents
1817 compiled. This information is used for outputting debugging info. */
1819 tree
1820 build_let (filename, line, vars, subblocks, supercontext, tags)
1821 char *filename;
1822 int line;
1823 tree vars, subblocks, supercontext, tags;
1825 register tree t = make_node (LET_STMT);
1826 STMT_SOURCE_FILE (t) = filename;
1827 STMT_SOURCE_LINE (t) = line;
1828 STMT_VARS (t) = vars;
1829 STMT_SUBBLOCKS (t) = subblocks;
1830 STMT_SUPERCONTEXT (t) = supercontext;
1831 STMT_BIND_SIZE (t) = 0;
1832 STMT_TYPE_TAGS (t) = tags;
1833 return t;
1836 /* Return a type like TYPE except that its TREE_READONLY is CONSTP
1837 and its TREE_VOLATILE is VOLATILEP.
1839 Such variant types already made are recorded so that duplicates
1840 are not made.
1842 A variant types should never be used as the type of an expression.
1843 Always copy the variant information into the TREE_READONLY
1844 and TREE_VOLATILE of the expression, and then give the expression
1845 as its type the "main variant", the variant whose TREE_READONLY
1846 and TREE_VOLATILE are zero. Use TYPE_MAIN_VARIANT to find the
1847 main variant. */
1849 tree
1850 build_type_variant (type, constp, volatilep)
1851 tree type;
1852 int constp, volatilep;
1854 register tree t, m = TYPE_MAIN_VARIANT (type);
1855 register struct obstack *ambient_obstack = current_obstack;
1857 /* Treat any nonzero argument as 1. */
1858 constp = !!constp;
1859 volatilep = !!volatilep;
1861 /* First search the chain variants for one that is what we want. */
1863 for (t = m; t; t = TYPE_NEXT_VARIANT (t))
1864 if (constp == TREE_READONLY (t)
1865 && volatilep == TREE_VOLATILE (t))
1866 return t;
1868 /* We need a new one. */
1869 current_obstack
1870 = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
1872 t = copy_node (type);
1873 TREE_READONLY (t) = constp;
1874 TREE_VOLATILE (t) = volatilep;
1875 TYPE_POINTER_TO (t) = 0;
1876 TYPE_REFERENCE_TO (t) = 0;
1878 /* Add this type to the chain of variants of TYPE. */
1879 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
1880 TYPE_NEXT_VARIANT (m) = t;
1882 current_obstack = ambient_obstack;
1883 return t;
1886 /* Hashing of types so that we don't make duplicates.
1887 The entry point is `type_hash_canon'. */
1889 /* Each hash table slot is a bucket containing a chain
1890 of these structures. */
1892 struct type_hash
1894 struct type_hash *next; /* Next structure in the bucket. */
1895 int hashcode; /* Hash code of this type. */
1896 tree type; /* The type recorded here. */
1899 /* Now here is the hash table. When recording a type, it is added
1900 to the slot whose index is the hash code mod the table size.
1901 Note that the hash table is used for several kinds of types
1902 (function types, array types and array index range types, for now).
1903 While all these live in the same table, they are completely independent,
1904 and the hash code is computed differently for each of these. */
1906 #define TYPE_HASH_SIZE 59
1907 struct type_hash *type_hash_table[TYPE_HASH_SIZE];
1909 /* Here is how primitive or already-canonicalized types' hash
1910 codes are made. */
1911 #define TYPE_HASH(TYPE) TREE_UID (TYPE)
1913 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
1914 with types in the TREE_VALUE slots), by adding the hash codes
1915 of the individual types. */
1918 type_hash_list (list)
1919 tree list;
1921 register int hashcode;
1922 register tree tail;
1923 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
1924 hashcode += TYPE_HASH (TREE_VALUE (tail));
1925 return hashcode;
1928 /* Look in the type hash table for a type isomorphic to TYPE.
1929 If one is found, return it. Otherwise return 0. */
1931 tree
1932 type_hash_lookup (hashcode, type)
1933 int hashcode;
1934 tree type;
1936 register struct type_hash *h;
1937 for (h = type_hash_table[hashcode % TYPE_HASH_SIZE]; h; h = h->next)
1938 if (h->hashcode == hashcode
1939 && TREE_CODE (h->type) == TREE_CODE (type)
1940 && TREE_TYPE (h->type) == TREE_TYPE (type)
1941 && (TYPE_MAX_VALUE (h->type) == TYPE_MAX_VALUE (type)
1942 || tree_int_cst_equal (TYPE_MAX_VALUE (h->type),
1943 TYPE_MAX_VALUE (type)))
1944 && (TYPE_MIN_VALUE (h->type) == TYPE_MIN_VALUE (type)
1945 || tree_int_cst_equal (TYPE_MIN_VALUE (h->type),
1946 TYPE_MIN_VALUE (type)))
1947 && (TYPE_DOMAIN (h->type) == TYPE_DOMAIN (type)
1948 || (TREE_CODE (TYPE_DOMAIN (h->type)) == TREE_LIST
1949 && TREE_CODE (TYPE_DOMAIN (type)) == TREE_LIST
1950 && type_list_equal (TYPE_DOMAIN (h->type), TYPE_DOMAIN (type)))))
1951 return h->type;
1952 return 0;
1955 /* Add an entry to the type-hash-table
1956 for a type TYPE whose hash code is HASHCODE. */
1958 void
1959 type_hash_add (hashcode, type)
1960 int hashcode;
1961 tree type;
1963 register struct type_hash *h;
1965 h = (struct type_hash *) oballoc (sizeof (struct type_hash));
1966 h->hashcode = hashcode;
1967 h->type = type;
1968 h->next = type_hash_table[hashcode % TYPE_HASH_SIZE];
1969 type_hash_table[hashcode % TYPE_HASH_SIZE] = h;
1972 /* Given TYPE, and HASHCODE its hash code, return the canonical
1973 object for an identical type if one already exists.
1974 Otherwise, return TYPE, and record it as the canonical object
1975 if it is a permanent object.
1977 To use this function, first create a type of the sort you want.
1978 Then compute its hash code from the fields of the type that
1979 make it different from other similar types.
1980 Then call this function and use the value.
1981 This function frees the type you pass in if it is a duplicate. */
1983 /* Set to 1 to debug without canonicalization. Never set by program. */
1984 int debug_no_type_hash = 0;
1986 tree
1987 type_hash_canon (hashcode, type)
1988 int hashcode;
1989 tree type;
1991 tree t1;
1993 if (debug_no_type_hash)
1994 return type;
1996 t1 = type_hash_lookup (hashcode, type);
1997 if (t1 != 0)
1999 struct obstack *o
2000 = TREE_PERMANENT (type) ? &permanent_obstack : saveable_obstack;
2001 obstack_free (o, type);
2002 #ifdef GATHER_STATISTICS
2003 tree_node_kinds[(int)t_kind]--;
2004 tree_node_sizes[(int)t_kind] -= sizeof (struct tree_type);
2005 #endif
2006 return t1;
2009 /* If this is a new type, record it for later reuse. */
2010 if (current_obstack == &permanent_obstack)
2011 type_hash_add (hashcode, type);
2013 return type;
2016 /* Given two lists of types
2017 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
2018 return 1 if the lists contain the same types in the same order.
2019 Also, the TREE_PURPOSEs must match. */
2022 type_list_equal (l1, l2)
2023 tree l1, l2;
2025 register tree t1, t2;
2026 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
2028 if (TREE_VALUE (t1) != TREE_VALUE (t2))
2029 return 0;
2030 if (TREE_PURPOSE (t1) != TREE_PURPOSE (t2))
2032 int cmp = simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2));
2033 if (cmp < 0)
2034 abort ();
2035 if (cmp == 0)
2036 return 0;
2040 return t1 == t2;
2043 /* Nonzero if integer constants T1 and T2
2044 represent the same constant value. */
2047 tree_int_cst_equal (t1, t2)
2048 tree t1, t2;
2050 if (t1 == t2)
2051 return 1;
2052 if (t1 == 0 || t2 == 0)
2053 return 0;
2054 if (TREE_CODE (t1) == INTEGER_CST
2055 && TREE_CODE (t2) == INTEGER_CST
2056 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
2057 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
2058 return 1;
2059 return 0;
2062 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
2063 The precise way of comparison depends on their data type. */
2066 tree_int_cst_lt (t1, t2)
2067 tree t1, t2;
2069 if (t1 == t2)
2070 return 0;
2072 if (!TREE_UNSIGNED (TREE_TYPE (t1)))
2073 return INT_CST_LT (t1, t2);
2074 return INT_CST_LT_UNSIGNED (t1, t2);
2077 /* Compare two constructor-element-type constants. */
2079 simple_cst_list_equal (l1, l2)
2080 tree l1, l2;
2082 while (l1 != NULL_TREE && l2 != NULL_TREE)
2084 int cmp = simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2));
2085 if (cmp < 0)
2086 abort ();
2087 if (cmp == 0)
2088 return 0;
2089 l1 = TREE_CHAIN (l1);
2090 l2 = TREE_CHAIN (l2);
2092 return (l1 == l2);
2095 /* Return truthvalue of whether T1 is the same tree structure as T2.
2096 Return 1 if they are the same.
2097 Return 0 if they are understandably different.
2098 Return -1 if either contains tree structure not understood by
2099 this function. */
2101 simple_cst_equal (t1, t2)
2102 tree t1, t2;
2104 register enum tree_code code1, code2;
2105 int cmp;
2107 if (t1 == t2)
2108 return 1;
2109 if (t1 == 0 || t2 == 0)
2110 return 0;
2112 code1 = TREE_CODE (t1);
2113 code2 = TREE_CODE (t2);
2115 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR)
2116 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR)
2117 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2118 else
2119 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
2120 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR)
2121 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
2123 if (code1 != code2)
2124 return 0;
2126 switch (code1)
2128 case INTEGER_CST:
2129 return TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
2130 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2);
2132 case REAL_CST:
2133 return REAL_VALUES_EQUAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2135 case STRING_CST:
2136 return TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2137 && !bcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2138 TREE_STRING_LENGTH (t1));
2140 case CONSTRUCTOR:
2141 abort ();
2143 case SAVE_EXPR:
2144 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2146 case NEW_EXPR:
2147 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2149 case CALL_EXPR:
2150 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2151 if (cmp <= 0)
2152 return cmp;
2153 return simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2155 case COMPONENT_REF:
2156 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
2157 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2158 return 0;
2160 case VAR_DECL:
2161 case PARM_DECL:
2162 case CONST_DECL:
2163 case FUNCTION_DECL:
2164 return 0;
2166 case PLUS_EXPR:
2167 case MINUS_EXPR:
2168 case MULT_EXPR:
2169 case TRUNC_DIV_EXPR:
2170 case TRUNC_MOD_EXPR:
2171 case LSHIFT_EXPR:
2172 case RSHIFT_EXPR:
2173 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2174 if (cmp <= 0)
2175 return cmp;
2176 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
2178 case NEGATE_EXPR:
2179 case ADDR_EXPR:
2180 case REFERENCE_EXPR:
2181 case INDIRECT_REF:
2182 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2184 default:
2185 return lang_simple_cst_equal (t1, t2);
2189 /* Constructors for pointer, array and function types.
2190 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
2191 constructed by language-dependent code, not here.) */
2193 /* Construct, lay out and return the type of pointers to TO_TYPE.
2194 If such a type has already been constructed, reuse it. */
2196 tree
2197 build_pointer_type (to_type)
2198 tree to_type;
2200 register tree t = TYPE_POINTER_TO (to_type);
2201 register struct obstack *ambient_obstack = current_obstack;
2202 register struct obstack *ambient_saveable_obstack = saveable_obstack;
2204 /* First, if we already have a type for pointers to TO_TYPE, use it. */
2206 if (t)
2207 return t;
2209 /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
2210 if (TREE_PERMANENT (to_type))
2212 current_obstack = &permanent_obstack;
2213 saveable_obstack = &permanent_obstack;
2216 t = make_node (POINTER_TYPE);
2217 TREE_TYPE (t) = to_type;
2219 /* Record this type as the pointer to TO_TYPE. */
2220 TYPE_POINTER_TO (to_type) = t;
2222 /* Lay out the type. This function has many callers that are concerned
2223 with expression-construction, and this simplifies them all.
2224 Also, it guarantees the TYPE_SIZE is permanent if the type is. */
2225 layout_type (t);
2227 current_obstack = ambient_obstack;
2228 saveable_obstack = ambient_saveable_obstack;
2229 return t;
2232 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
2233 MAXVAL should be the maximum value in the domain
2234 (one less than the length of the array). */
2236 tree
2237 build_index_type (maxval)
2238 tree maxval;
2240 register tree itype = make_node (INTEGER_TYPE);
2241 int maxint = TREE_INT_CST_LOW (maxval);
2242 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
2243 TYPE_MIN_VALUE (itype) = build_int_2 (0, 0);
2244 TREE_TYPE (TYPE_MIN_VALUE (itype)) = sizetype;
2245 TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
2246 TYPE_MODE (itype) = SImode;
2247 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
2248 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
2249 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
2250 return type_hash_canon (maxint > 0 ? maxint : - maxint, itype);
2253 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
2254 and number of elements specified by the range of values of INDEX_TYPE.
2255 If such a type has already been constructed, reuse it. */
2257 tree
2258 build_array_type (elt_type, index_type)
2259 tree elt_type, index_type;
2261 register tree t = make_node (ARRAY_TYPE);
2262 int hashcode;
2264 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
2266 error ("arrays of functions are not meaningful");
2267 elt_type = integer_type_node;
2270 TREE_TYPE (t) = elt_type;
2271 TYPE_DOMAIN (t) = index_type;
2273 /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
2274 build_pointer_type (elt_type);
2276 if (index_type == 0)
2277 return t;
2279 hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
2280 t = type_hash_canon (hashcode, t);
2282 if (TYPE_SIZE (t) == 0)
2283 layout_type (t);
2284 return t;
2287 /* Construct, lay out and return
2288 the type of functions returning type VALUE_TYPE
2289 given arguments of types ARG_TYPES.
2290 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
2291 are data type nodes for the arguments of the function.
2292 If such a type has already been constructed, reuse it. */
2294 tree
2295 build_function_type (value_type, arg_types)
2296 tree value_type, arg_types;
2298 register tree t;
2299 int hashcode;
2301 if (TREE_CODE (value_type) == FUNCTION_TYPE
2302 || TREE_CODE (value_type) == ARRAY_TYPE)
2304 error ("function return type cannot be function or array");
2305 value_type = integer_type_node;
2308 /* Make a node of the sort we want. */
2309 t = make_node (FUNCTION_TYPE);
2310 TREE_TYPE (t) = value_type;
2311 TYPE_ARG_TYPES (t) = arg_types;
2313 /* If we already have such a type, use the old one and free this one. */
2314 hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
2315 t = type_hash_canon (hashcode, t);
2317 if (TYPE_SIZE (t) == 0)
2318 layout_type (t);
2319 return t;
2322 /* Build the node for the type of references-to-TO_TYPE. */
2324 tree
2325 build_reference_type (to_type)
2326 tree to_type;
2328 register tree t = TYPE_REFERENCE_TO (to_type);
2329 register struct obstack *ambient_obstack = current_obstack;
2330 register struct obstack *ambient_saveable_obstack = saveable_obstack;
2332 /* First, if we already have a type for pointers to TO_TYPE, use it. */
2334 if (t)
2335 return t;
2337 /* We need a new one. If TO_TYPE is permanent, make this permanent too. */
2338 if (TREE_PERMANENT (to_type))
2340 current_obstack = &permanent_obstack;
2341 saveable_obstack = &permanent_obstack;
2344 t = make_node (REFERENCE_TYPE);
2345 TREE_TYPE (t) = to_type;
2347 /* Record this type as the pointer to TO_TYPE. */
2348 TYPE_REFERENCE_TO (to_type) = t;
2350 layout_type (t);
2352 current_obstack = ambient_obstack;
2353 saveable_obstack = ambient_saveable_obstack;
2354 return t;
2357 /* Construct, lay out and return the type of methods belonging to class
2358 BASETYPE and whose arguments and values are described by TYPE.
2359 If that type exists already, reuse it.
2360 TYPE must be a FUNCTION_TYPE node. */
2362 tree
2363 build_method_type (basetype, type)
2364 tree basetype, type;
2366 register tree t;
2367 int hashcode;
2369 /* Make a node of the sort we want. */
2370 t = make_node (METHOD_TYPE);
2372 if (TREE_CODE (type) != FUNCTION_TYPE)
2373 abort ();
2375 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
2376 TREE_TYPE (t) = TREE_TYPE (type);
2378 /* The actual arglist for this function includes a "hidden" argument
2379 which is "this". Put it into the list of argument types. */
2381 TYPE_ARG_TYPES (t)
2382 = tree_cons (NULL, build_pointer_type (basetype), TYPE_ARG_TYPES (type));
2384 /* If we already have such a type, use the old one and free this one. */
2385 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
2386 t = type_hash_canon (hashcode, t);
2388 if (TYPE_SIZE (t) == 0)
2389 layout_type (t);
2391 return t;
2394 /* Construct, lay out and return the type of methods belonging to class
2395 BASETYPE and whose arguments and values are described by TYPE.
2396 If that type exists already, reuse it.
2397 TYPE must be a FUNCTION_TYPE node. */
2399 tree
2400 build_offset_type (basetype, type)
2401 tree basetype, type;
2403 register tree t;
2404 int hashcode;
2406 /* Make a node of the sort we want. */
2407 t = make_node (OFFSET_TYPE);
2409 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
2410 TREE_TYPE (t) = type;
2412 /* If we already have such a type, use the old one and free this one. */
2413 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
2414 t = type_hash_canon (hashcode, t);
2416 if (TYPE_SIZE (t) == 0)
2417 layout_type (t);
2419 return t;
2422 /* Return the innermost context enclosing FNDECL that is
2423 a RECORD_TYPE or UNION_TYPE, or zero if none.
2424 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
2426 tree
2427 decl_type_context (fndecl)
2428 tree fndecl;
2430 tree context = DECL_CONTEXT (fndecl);
2432 while (context)
2434 if (TREE_CODE (context) == RECORD_TYPE
2435 || TREE_CODE (context) == UNION_TYPE)
2436 return context;
2437 if (TREE_CODE (context) == TYPE_DECL
2438 || TREE_CODE (context) == FUNCTION_DECL)
2439 context = DECL_CONTEXT (context);
2440 else if (TREE_CODE (context) == LET_STMT)
2441 context = STMT_SUPERCONTEXT (context);
2442 else
2443 /* Unhandled CONTEXT!? */
2444 abort ();
2446 return NULL_TREE;
2449 /* Return OP, stripped of any conversions to wider types as much as is safe.
2450 Converting the value back to OP's type makes a value equivalent to OP.
2452 If FOR_TYPE is nonzero, we return a value which, if converted to
2453 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
2455 If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
2456 narrowest type that can hold the value, even if they don't exactly fit.
2457 Otherwise, bit-field references are changed to a narrower type
2458 only if they can be fetched directly from memory in that type.
2460 OP must have integer, real or enumeral type. Pointers are not allowed!
2462 There are some cases where the obvious value we could return
2463 would regenerate to OP if converted to OP's type,
2464 but would not extend like OP to wider types.
2465 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
2466 For example, if OP is (unsigned short)(signed char)-1,
2467 we avoid returning (signed char)-1 if FOR_TYPE is int,
2468 even though extending that to an unsigned short would regenerate OP,
2469 since the result of extending (signed char)-1 to (int)
2470 is different from (int) OP. */
2472 tree
2473 get_unwidened (op, for_type)
2474 register tree op;
2475 tree for_type;
2477 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
2478 /* TYPE_PRECISION is safe in place of type_precision since
2479 pointer types are not allowed. */
2480 register tree type = TREE_TYPE (op);
2481 register int final_prec = TYPE_PRECISION (for_type != 0 ? for_type : type);
2482 register int uns
2483 = (for_type != 0 && for_type != type
2484 && final_prec > TYPE_PRECISION (type)
2485 && TREE_UNSIGNED (type));
2486 register tree win = op;
2488 while (TREE_CODE (op) == NOP_EXPR)
2490 register int bitschange
2491 = TYPE_PRECISION (TREE_TYPE (op))
2492 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
2494 /* Truncations are many-one so cannot be removed.
2495 Unless we are later going to truncate down even farther. */
2496 if (bitschange < 0
2497 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
2498 break;
2500 /* See what's inside this conversion. If we decide to strip it,
2501 we will set WIN. */
2502 op = TREE_OPERAND (op, 0);
2504 /* If we have not stripped any zero-extensions (uns is 0),
2505 we can strip any kind of extension.
2506 If we have previously stripped a zero-extension,
2507 only zero-extensions can safely be stripped.
2508 Any extension can be stripped if the bits it would produce
2509 are all going to be discarded later by truncating to FOR_TYPE. */
2511 if (bitschange > 0)
2513 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
2514 win = op;
2515 /* TREE_UNSIGNED says whether this is a zero-extension.
2516 Let's avoid computing it if it does not affect WIN
2517 and if UNS will not be needed again. */
2518 if ((uns || TREE_CODE (op) == NOP_EXPR)
2519 && TREE_UNSIGNED (TREE_TYPE (op)))
2521 uns = 1;
2522 win = op;
2527 if (TREE_CODE (op) == COMPONENT_REF
2528 /* Since type_for_size always gives an integer type. */
2529 && TREE_CODE (type) != REAL_TYPE)
2531 int innerprec = (TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)))
2532 * DECL_SIZE_UNIT (TREE_OPERAND (op, 1)));
2533 type = type_for_size (innerprec, TREE_UNSIGNED (TREE_OPERAND (op, 1)));
2535 /* We can get this structure field in the narrowest type it fits in.
2536 If FOR_TYPE is 0, do this only for a field that matches the
2537 narrower type exactly and is aligned for it (i.e. mode isn't BI).
2538 The resulting extension to its nominal type (a fullword type)
2539 must fit the same conditions as for other extensions. */
2541 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
2542 && (for_type || DECL_MODE (TREE_OPERAND (op, 1)) != BImode)
2543 && (! uns || final_prec <= innerprec
2544 || TREE_UNSIGNED (TREE_OPERAND (op, 1)))
2545 && type != 0)
2547 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
2548 TREE_OPERAND (op, 1));
2549 TREE_VOLATILE (win) = TREE_VOLATILE (op);
2550 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
2551 TREE_RAISES (win) = TREE_RAISES (op);
2554 return win;
2557 /* Return OP or a simpler expression for a narrower value
2558 which can be sign-extended or zero-extended to give back OP.
2559 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
2560 or 0 if the value should be sign-extended. */
2562 tree
2563 get_narrower (op, unsignedp_ptr)
2564 register tree op;
2565 int *unsignedp_ptr;
2567 register int uns = 0;
2568 int first = 1;
2569 register tree win = op;
2571 while (TREE_CODE (op) == NOP_EXPR)
2573 register int bitschange
2574 = TYPE_PRECISION (TREE_TYPE (op))
2575 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
2577 /* Truncations are many-one so cannot be removed. */
2578 if (bitschange < 0)
2579 break;
2581 /* See what's inside this conversion. If we decide to strip it,
2582 we will set WIN. */
2583 op = TREE_OPERAND (op, 0);
2585 if (bitschange > 0)
2587 /* An extension: the outermost one can be stripped,
2588 but remember whether it is zero or sign extension. */
2589 if (first)
2590 uns = TREE_UNSIGNED (TREE_TYPE (op));
2591 /* Otherwise, if a sign extension has been stripped,
2592 only sign extensions can now be stripped;
2593 if a zero extension has been stripped, only zero-extensions. */
2594 else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
2595 break;
2596 first = 0;
2598 /* A change in nominal type can always be stripped. */
2600 win = op;
2603 if (TREE_CODE (op) == COMPONENT_REF
2604 /* Since type_for_size always gives an integer type. */
2605 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE)
2607 int innerprec = (TREE_INT_CST_LOW (DECL_SIZE (TREE_OPERAND (op, 1)))
2608 * DECL_SIZE_UNIT (TREE_OPERAND (op, 1)));
2609 tree type = type_for_size (innerprec, TREE_UNSIGNED (op));
2611 /* We can get this structure field in a narrower type that fits it,
2612 but the resulting extension to its nominal type (a fullword type)
2613 must satisfy the same conditions as for other extensions.
2615 Do this only for fields that are aligned (not BImode),
2616 because when bit-field insns will be used there is no
2617 advantage in doing this. */
2619 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
2620 && DECL_MODE (TREE_OPERAND (op, 1)) != BImode
2621 && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
2622 && type != 0)
2624 if (first)
2625 uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
2626 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
2627 TREE_OPERAND (op, 1));
2628 TREE_VOLATILE (win) = TREE_VOLATILE (op);
2629 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
2630 TREE_RAISES (win) = TREE_RAISES (op);
2633 *unsignedp_ptr = uns;
2634 return win;
2637 /* Return the precision of a type, for arithmetic purposes.
2638 Supports all types on which arithmetic is possible
2639 (including pointer types).
2640 It's not clear yet what will be right for complex types. */
2643 type_precision (type)
2644 register tree type;
2646 return ((TREE_CODE (type) == INTEGER_TYPE
2647 || TREE_CODE (type) == ENUMERAL_TYPE
2648 || TREE_CODE (type) == REAL_TYPE)
2649 ? TYPE_PRECISION (type) : POINTER_SIZE);
2652 /* Nonzero if integer constant C has a value that is permissible
2653 for type TYPE (an INTEGER_TYPE). */
2656 int_fits_type_p (c, type)
2657 tree c, type;
2659 if (TREE_UNSIGNED (type))
2660 return (!INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c)
2661 && !INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type)));
2662 else
2663 return (!INT_CST_LT (TYPE_MAX_VALUE (type), c)
2664 && !INT_CST_LT (c, TYPE_MIN_VALUE (type)));
2667 void
2668 print_obstack_statistics (str, o)
2669 char *str;
2670 struct obstack *o;
2672 struct _obstack_chunk *chunk = o->chunk;
2673 int n_chunks = 0;
2674 int n_alloc = 0;
2676 while (chunk)
2678 n_chunks += 1;
2679 n_alloc += chunk->limit - &chunk->contents[0];
2680 chunk = chunk->prev;
2682 fprintf (stderr, "obstack %s: %d bytes, %d chunks\n",
2683 str, n_alloc, n_chunks);
2686 void
2687 dump_tree_statistics ()
2689 int i;
2690 int total_nodes, total_bytes;
2691 extern struct obstack class_obstack;
2693 fprintf (stderr, "\n%d tree nodes created\n\n", tree_node_counter);
2694 #ifdef GATHER_STATISTICS
2695 fprintf (stderr, "Kind Nodes Bytes\n");
2696 fprintf (stderr, "-------------------------------------\n");
2697 total_nodes = total_bytes = 0;
2698 for (i = 0; i < (int) all_kinds; i++)
2700 fprintf (stderr, "%-20s %6d %9d\n", tree_node_kind_names[i],
2701 tree_node_kinds[i], tree_node_sizes[i]);
2702 total_nodes += tree_node_kinds[i];
2703 total_bytes += tree_node_sizes[i];
2705 fprintf (stderr, "%-20s %9d\n", "identifier names", id_string_size);
2706 fprintf (stderr, "-------------------------------------\n");
2707 fprintf (stderr, "%-20s %6d %9d\n", "Total", total_nodes, total_bytes);
2708 fprintf (stderr, "-------------------------------------\n");
2709 #else
2710 fprintf (stderr, "(No per-node statistics)\n");
2711 #endif
2712 print_obstack_statistics ("class_obstack", &class_obstack);
2713 print_obstack_statistics ("permanent_obstack", &permanent_obstack);
2714 print_obstack_statistics ("maybepermanent_obstack", &maybepermanent_obstack);
2715 print_search_statistics ();
2716 print_class_statistics ();