Use target specific, language specific object files feature to allow build
[official-gcc.git] / gcc / tree.c
blobdcacdaf1d397943f0100c18ed766a7abe7f9a1b6
1 /* Language-independent node constructors for parse phase of GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* This file contains the low level primitives for operating on tree nodes,
23 including allocation, list operations, interning of identifiers,
24 construction of data type nodes and statement nodes,
25 and construction of type conversion nodes. It also contains
26 tables index by tree code that describe how to take apart
27 nodes of that code.
29 It is intended to be language-independent, but occasionally
30 calls language-dependent routines defined (for C) in typecheck.c.
32 The low-level allocation routines oballoc and permalloc
33 are used also for allocating many other kinds of objects
34 by all passes of the compiler. */
36 #include "config.h"
37 #include "system.h"
38 #include "flags.h"
39 #include "tree.h"
40 #include "tm_p.h"
41 #include "function.h"
42 #include "obstack.h"
43 #include "toplev.h"
44 #include "ggc.h"
45 #include "hashtab.h"
46 #include "output.h"
47 #include "defaults.h"
49 #define obstack_chunk_alloc xmalloc
50 #define obstack_chunk_free free
51 /* obstack.[ch] explicitly declined to prototype this. */
52 extern int _obstack_allocated_p PARAMS ((struct obstack *h, PTR obj));
54 static void unsave_expr_now_r PARAMS ((tree));
56 /* Objects allocated on this obstack last forever. */
58 struct obstack permanent_obstack;
60 /* Table indexed by tree code giving a string containing a character
61 classifying the tree code. Possibilities are
62 t, d, s, c, r, <, 1, 2 and e. See tree.def for details. */
64 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) TYPE,
66 char tree_code_type[MAX_TREE_CODES] = {
67 #include "tree.def"
69 #undef DEFTREECODE
71 /* Table indexed by tree code giving number of expression
72 operands beyond the fixed part of the node structure.
73 Not used for types or decls. */
75 #define DEFTREECODE(SYM, NAME, TYPE, LENGTH) LENGTH,
77 int tree_code_length[MAX_TREE_CODES] = {
78 #include "tree.def"
80 #undef DEFTREECODE
82 /* Names of tree components.
83 Used for printing out the tree and error messages. */
84 #define DEFTREECODE(SYM, NAME, TYPE, LEN) NAME,
86 const char *tree_code_name[MAX_TREE_CODES] = {
87 #include "tree.def"
89 #undef DEFTREECODE
91 /* Statistics-gathering stuff. */
92 typedef enum
94 d_kind,
95 t_kind,
96 b_kind,
97 s_kind,
98 r_kind,
99 e_kind,
100 c_kind,
101 id_kind,
102 op_id_kind,
103 perm_list_kind,
104 temp_list_kind,
105 vec_kind,
106 x_kind,
107 lang_decl,
108 lang_type,
109 all_kinds
110 } tree_node_kind;
112 int tree_node_counts[(int) all_kinds];
113 int tree_node_sizes[(int) all_kinds];
114 int id_string_size = 0;
116 static const char * const tree_node_kind_names[] = {
117 "decls",
118 "types",
119 "blocks",
120 "stmts",
121 "refs",
122 "exprs",
123 "constants",
124 "identifiers",
125 "op_identifiers",
126 "perm_tree_lists",
127 "temp_tree_lists",
128 "vecs",
129 "random kinds",
130 "lang_decl kinds",
131 "lang_type kinds"
134 /* Unique id for next decl created. */
135 static int next_decl_uid;
136 /* Unique id for next type created. */
137 static int next_type_uid = 1;
139 /* Here is how primitive or already-canonicalized types' hash
140 codes are made. */
141 #define TYPE_HASH(TYPE) ((unsigned long) (TYPE) & 0777777)
143 /* Since we cannot rehash a type after it is in the table, we have to
144 keep the hash code. */
146 struct type_hash
148 unsigned long hash;
149 tree type;
152 /* Initial size of the hash table (rounded to next prime). */
153 #define TYPE_HASH_INITIAL_SIZE 1000
155 /* Now here is the hash table. When recording a type, it is added to
156 the slot whose index is the hash code. Note that the hash table is
157 used for several kinds of types (function types, array types and
158 array index range types, for now). While all these live in the
159 same table, they are completely independent, and the hash code is
160 computed differently for each of these. */
162 htab_t type_hash_table;
164 static void build_real_from_int_cst_1 PARAMS ((PTR));
165 static void set_type_quals PARAMS ((tree, int));
166 static void append_random_chars PARAMS ((char *));
167 static void mark_type_hash PARAMS ((void *));
168 static int type_hash_eq PARAMS ((const void*, const void*));
169 static unsigned int type_hash_hash PARAMS ((const void*));
170 static void print_type_hash_statistics PARAMS((void));
171 static int mark_hash_entry PARAMS((void **, void *));
172 static void finish_vector_type PARAMS((tree));
174 /* If non-null, these are language-specific helper functions for
175 unsave_expr_now. If present, LANG_UNSAVE is called before its
176 argument (an UNSAVE_EXPR) is to be unsaved, and all other
177 processing in unsave_expr_now is aborted. LANG_UNSAVE_EXPR_NOW is
178 called from unsave_expr_1 for language-specific tree codes. */
179 void (*lang_unsave) PARAMS ((tree *));
180 void (*lang_unsave_expr_now) PARAMS ((tree));
182 /* If non-null, these are language-specific helper functions for
183 unsafe_for_reeval. Return negative to not handle some tree. */
184 int (*lang_unsafe_for_reeval) PARAMS ((tree));
186 tree global_trees[TI_MAX];
187 tree integer_types[itk_none];
189 /* Init the principal obstacks. */
191 void
192 init_obstacks ()
194 gcc_obstack_init (&permanent_obstack);
196 /* Initialize the hash table of types. */
197 type_hash_table = htab_create (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
198 type_hash_eq, 0);
199 ggc_add_root (&type_hash_table, 1, sizeof type_hash_table, mark_type_hash);
200 ggc_add_tree_root (global_trees, TI_MAX);
201 ggc_add_tree_root (integer_types, itk_none);
204 void
205 gcc_obstack_init (obstack)
206 struct obstack *obstack;
208 /* Let particular systems override the size of a chunk. */
209 #ifndef OBSTACK_CHUNK_SIZE
210 #define OBSTACK_CHUNK_SIZE 0
211 #endif
212 /* Let them override the alloc and free routines too. */
213 #ifndef OBSTACK_CHUNK_ALLOC
214 #define OBSTACK_CHUNK_ALLOC xmalloc
215 #endif
216 #ifndef OBSTACK_CHUNK_FREE
217 #define OBSTACK_CHUNK_FREE free
218 #endif
219 _obstack_begin (obstack, OBSTACK_CHUNK_SIZE, 0,
220 (void *(*) PARAMS ((long))) OBSTACK_CHUNK_ALLOC,
221 (void (*) PARAMS ((void *))) OBSTACK_CHUNK_FREE);
225 /* Allocate SIZE bytes in the permanent obstack
226 and return a pointer to them. */
228 char *
229 permalloc (size)
230 int size;
232 return (char *) obstack_alloc (&permanent_obstack, size);
235 /* Allocate NELEM items of SIZE bytes in the permanent obstack
236 and return a pointer to them. The storage is cleared before
237 returning the value. */
239 char *
240 perm_calloc (nelem, size)
241 int nelem;
242 long size;
244 char *rval = (char *) obstack_alloc (&permanent_obstack, nelem * size);
245 memset (rval, 0, nelem * size);
246 return rval;
249 /* Compute the number of bytes occupied by 'node'. This routine only
250 looks at TREE_CODE and, if the code is TREE_VEC, TREE_VEC_LENGTH. */
251 size_t
252 tree_size (node)
253 tree node;
255 enum tree_code code = TREE_CODE (node);
257 switch (TREE_CODE_CLASS (code))
259 case 'd': /* A decl node */
260 return sizeof (struct tree_decl);
262 case 't': /* a type node */
263 return sizeof (struct tree_type);
265 case 'b': /* a lexical block node */
266 return sizeof (struct tree_block);
268 case 'r': /* a reference */
269 case 'e': /* an expression */
270 case 's': /* an expression with side effects */
271 case '<': /* a comparison expression */
272 case '1': /* a unary arithmetic expression */
273 case '2': /* a binary arithmetic expression */
274 return (sizeof (struct tree_exp)
275 + (TREE_CODE_LENGTH (code) - 1) * sizeof (char *));
277 case 'c': /* a constant */
278 /* We can't use TREE_CODE_LENGTH for INTEGER_CST, since the number of
279 words is machine-dependent due to varying length of HOST_WIDE_INT,
280 which might be wider than a pointer (e.g., long long). Similarly
281 for REAL_CST, since the number of words is machine-dependent due
282 to varying size and alignment of `double'. */
283 if (code == INTEGER_CST)
284 return sizeof (struct tree_int_cst);
285 else if (code == REAL_CST)
286 return sizeof (struct tree_real_cst);
287 else
288 return (sizeof (struct tree_common)
289 + TREE_CODE_LENGTH (code) * sizeof (char *));
291 case 'x': /* something random, like an identifier. */
293 size_t length;
294 length = (sizeof (struct tree_common)
295 + TREE_CODE_LENGTH (code) * sizeof (char *));
296 if (code == TREE_VEC)
297 length += (TREE_VEC_LENGTH (node) - 1) * sizeof (char *);
298 return length;
301 default:
302 abort ();
306 /* Return a newly allocated node of code CODE.
307 For decl and type nodes, some other fields are initialized.
308 The rest of the node is initialized to zero.
310 Achoo! I got a code in the node. */
312 tree
313 make_node (code)
314 enum tree_code code;
316 register tree t;
317 register int type = TREE_CODE_CLASS (code);
318 register size_t length;
319 #ifdef GATHER_STATISTICS
320 register tree_node_kind kind;
321 #endif
322 struct tree_common ttmp;
324 /* We can't allocate a TREE_VEC without knowing how many elements
325 it will have. */
326 if (code == TREE_VEC)
327 abort ();
329 TREE_SET_CODE ((tree)&ttmp, code);
330 length = tree_size ((tree)&ttmp);
332 #ifdef GATHER_STATISTICS
333 switch (type)
335 case 'd': /* A decl node */
336 kind = d_kind;
337 break;
339 case 't': /* a type node */
340 kind = t_kind;
341 break;
343 case 'b': /* a lexical block */
344 kind = b_kind;
345 break;
347 case 's': /* an expression with side effects */
348 kind = s_kind;
349 break;
351 case 'r': /* a reference */
352 kind = r_kind;
353 break;
355 case 'e': /* an expression */
356 case '<': /* a comparison expression */
357 case '1': /* a unary arithmetic expression */
358 case '2': /* a binary arithmetic expression */
359 kind = e_kind;
360 break;
362 case 'c': /* a constant */
363 kind = c_kind;
364 break;
366 case 'x': /* something random, like an identifier. */
367 if (code == IDENTIFIER_NODE)
368 kind = id_kind;
369 else if (code == OP_IDENTIFIER)
370 kind = op_id_kind;
371 else if (code == TREE_VEC)
372 kind = vec_kind;
373 else
374 kind = x_kind;
375 break;
377 default:
378 abort ();
381 tree_node_counts[(int) kind]++;
382 tree_node_sizes[(int) kind] += length;
383 #endif
385 t = ggc_alloc_tree (length);
387 memset ((PTR) t, 0, length);
389 TREE_SET_CODE (t, code);
391 switch (type)
393 case 's':
394 TREE_SIDE_EFFECTS (t) = 1;
395 TREE_TYPE (t) = void_type_node;
396 break;
398 case 'd':
399 if (code != FUNCTION_DECL)
400 DECL_ALIGN (t) = 1;
401 DECL_USER_ALIGN (t) = 0;
402 DECL_IN_SYSTEM_HEADER (t) = in_system_header;
403 DECL_SOURCE_LINE (t) = lineno;
404 DECL_SOURCE_FILE (t) =
405 (input_filename) ? input_filename : "<built-in>";
406 DECL_UID (t) = next_decl_uid++;
407 /* Note that we have not yet computed the alias set for this
408 declaration. */
409 DECL_POINTER_ALIAS_SET (t) = -1;
410 break;
412 case 't':
413 TYPE_UID (t) = next_type_uid++;
414 TYPE_ALIGN (t) = char_type_node ? TYPE_ALIGN (char_type_node) : 0;
415 TYPE_USER_ALIGN (t) = 0;
416 TYPE_MAIN_VARIANT (t) = t;
417 TYPE_ATTRIBUTES (t) = NULL_TREE;
418 #ifdef SET_DEFAULT_TYPE_ATTRIBUTES
419 SET_DEFAULT_TYPE_ATTRIBUTES (t);
420 #endif
421 /* Note that we have not yet computed the alias set for this
422 type. */
423 TYPE_ALIAS_SET (t) = -1;
424 break;
426 case 'c':
427 TREE_CONSTANT (t) = 1;
428 break;
430 case 'e':
431 switch (code)
433 case INIT_EXPR:
434 case MODIFY_EXPR:
435 case VA_ARG_EXPR:
436 case RTL_EXPR:
437 case PREDECREMENT_EXPR:
438 case PREINCREMENT_EXPR:
439 case POSTDECREMENT_EXPR:
440 case POSTINCREMENT_EXPR:
441 /* All of these have side-effects, no matter what their
442 operands are. */
443 TREE_SIDE_EFFECTS (t) = 1;
444 break;
446 default:
447 break;
449 break;
452 return t;
455 /* A front-end can reset this to an appropriate function if types need
456 special handling. */
458 tree (*make_lang_type_fn) PARAMS ((enum tree_code)) = make_node;
460 /* Return a new type (with the indicated CODE), doing whatever
461 language-specific processing is required. */
463 tree
464 make_lang_type (code)
465 enum tree_code code;
467 return (*make_lang_type_fn) (code);
470 /* Return a new node with the same contents as NODE except that its
471 TREE_CHAIN is zero and it has a fresh uid. Unlike make_node, this
472 function always performs the allocation on the CURRENT_OBSTACK;
473 it's up to the caller to pick the right obstack before calling this
474 function. */
476 tree
477 copy_node (node)
478 tree node;
480 register tree t;
481 register enum tree_code code = TREE_CODE (node);
482 register size_t length;
484 length = tree_size (node);
485 t = ggc_alloc_tree (length);
486 memcpy (t, node, length);
488 TREE_CHAIN (t) = 0;
489 TREE_ASM_WRITTEN (t) = 0;
491 if (TREE_CODE_CLASS (code) == 'd')
492 DECL_UID (t) = next_decl_uid++;
493 else if (TREE_CODE_CLASS (code) == 't')
495 TYPE_UID (t) = next_type_uid++;
496 /* The following is so that the debug code for
497 the copy is different from the original type.
498 The two statements usually duplicate each other
499 (because they clear fields of the same union),
500 but the optimizer should catch that. */
501 TYPE_SYMTAB_POINTER (t) = 0;
502 TYPE_SYMTAB_ADDRESS (t) = 0;
505 return t;
508 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
509 For example, this can copy a list made of TREE_LIST nodes. */
511 tree
512 copy_list (list)
513 tree list;
515 tree head;
516 register tree prev, next;
518 if (list == 0)
519 return 0;
521 head = prev = copy_node (list);
522 next = TREE_CHAIN (list);
523 while (next)
525 TREE_CHAIN (prev) = copy_node (next);
526 prev = TREE_CHAIN (prev);
527 next = TREE_CHAIN (next);
529 return head;
533 /* Return a newly constructed INTEGER_CST node whose constant value
534 is specified by the two ints LOW and HI.
535 The TREE_TYPE is set to `int'.
537 This function should be used via the `build_int_2' macro. */
539 tree
540 build_int_2_wide (low, hi)
541 unsigned HOST_WIDE_INT low;
542 HOST_WIDE_INT hi;
544 register tree t = make_node (INTEGER_CST);
546 TREE_INT_CST_LOW (t) = low;
547 TREE_INT_CST_HIGH (t) = hi;
548 TREE_TYPE (t) = integer_type_node;
549 return t;
552 /* Return a new REAL_CST node whose type is TYPE and value is D. */
554 tree
555 build_real (type, d)
556 tree type;
557 REAL_VALUE_TYPE d;
559 tree v;
560 int overflow = 0;
562 /* Check for valid float value for this type on this target machine;
563 if not, can print error message and store a valid value in D. */
564 #ifdef CHECK_FLOAT_VALUE
565 CHECK_FLOAT_VALUE (TYPE_MODE (type), d, overflow);
566 #endif
568 v = make_node (REAL_CST);
569 TREE_TYPE (v) = type;
570 TREE_REAL_CST (v) = d;
571 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
572 return v;
575 /* Return a new REAL_CST node whose type is TYPE
576 and whose value is the integer value of the INTEGER_CST node I. */
578 #if !defined (REAL_IS_NOT_DOUBLE) || defined (REAL_ARITHMETIC)
580 REAL_VALUE_TYPE
581 real_value_from_int_cst (type, i)
582 tree type ATTRIBUTE_UNUSED, i;
584 REAL_VALUE_TYPE d;
586 #ifdef REAL_ARITHMETIC
587 /* Clear all bits of the real value type so that we can later do
588 bitwise comparisons to see if two values are the same. */
589 memset ((char *) &d, 0, sizeof d);
591 if (! TREE_UNSIGNED (TREE_TYPE (i)))
592 REAL_VALUE_FROM_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
593 TYPE_MODE (type));
594 else
595 REAL_VALUE_FROM_UNSIGNED_INT (d, TREE_INT_CST_LOW (i),
596 TREE_INT_CST_HIGH (i), TYPE_MODE (type));
597 #else /* not REAL_ARITHMETIC */
598 /* Some 386 compilers mishandle unsigned int to float conversions,
599 so introduce a temporary variable E to avoid those bugs. */
600 if (TREE_INT_CST_HIGH (i) < 0 && ! TREE_UNSIGNED (TREE_TYPE (i)))
602 REAL_VALUE_TYPE e;
604 d = (double) (~TREE_INT_CST_HIGH (i));
605 e = ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
606 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
607 d *= e;
608 e = (double) (~TREE_INT_CST_LOW (i));
609 d += e;
610 d = (- d - 1.0);
612 else
614 REAL_VALUE_TYPE e;
616 d = (double) (unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (i);
617 e = ((double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2))
618 * (double) ((HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)));
619 d *= e;
620 e = (double) TREE_INT_CST_LOW (i);
621 d += e;
623 #endif /* not REAL_ARITHMETIC */
624 return d;
627 /* Args to pass to and from build_real_from_int_cst_1. */
629 struct brfic_args
631 tree type; /* Input: type to conver to. */
632 tree i; /* Input: operand to convert. */
633 REAL_VALUE_TYPE d; /* Output: floating point value. */
636 /* Convert an integer to a floating point value while protected by a floating
637 point exception handler. */
639 static void
640 build_real_from_int_cst_1 (data)
641 PTR data;
643 struct brfic_args *args = (struct brfic_args *) data;
645 #ifdef REAL_ARITHMETIC
646 args->d = real_value_from_int_cst (args->type, args->i);
647 #else
648 args->d
649 = REAL_VALUE_TRUNCATE (TYPE_MODE (args->type),
650 real_value_from_int_cst (args->type, args->i));
651 #endif
654 /* Given a tree representing an integer constant I, return a tree
655 representing the same value as a floating-point constant of type TYPE.
656 We cannot perform this operation if there is no way of doing arithmetic
657 on floating-point values. */
659 tree
660 build_real_from_int_cst (type, i)
661 tree type;
662 tree i;
664 tree v;
665 int overflow = TREE_OVERFLOW (i);
666 REAL_VALUE_TYPE d;
667 struct brfic_args args;
669 v = make_node (REAL_CST);
670 TREE_TYPE (v) = type;
672 /* Setup input for build_real_from_int_cst_1() */
673 args.type = type;
674 args.i = i;
676 if (do_float_handler (build_real_from_int_cst_1, (PTR) &args))
677 /* Receive output from build_real_from_int_cst_1() */
678 d = args.d;
679 else
681 /* We got an exception from build_real_from_int_cst_1() */
682 d = dconst0;
683 overflow = 1;
686 /* Check for valid float value for this type on this target machine. */
688 #ifdef CHECK_FLOAT_VALUE
689 CHECK_FLOAT_VALUE (TYPE_MODE (type), d, overflow);
690 #endif
692 TREE_REAL_CST (v) = d;
693 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
694 return v;
697 #endif /* not REAL_IS_NOT_DOUBLE, or REAL_ARITHMETIC */
699 /* Return a newly constructed STRING_CST node whose value is
700 the LEN characters at STR.
701 The TREE_TYPE is not initialized. */
703 tree
704 build_string (len, str)
705 int len;
706 const char *str;
708 register tree s = make_node (STRING_CST);
710 TREE_STRING_LENGTH (s) = len;
711 TREE_STRING_POINTER (s) = ggc_alloc_string (str, len);
713 return s;
716 /* Return a newly constructed COMPLEX_CST node whose value is
717 specified by the real and imaginary parts REAL and IMAG.
718 Both REAL and IMAG should be constant nodes. TYPE, if specified,
719 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
721 tree
722 build_complex (type, real, imag)
723 tree type;
724 tree real, imag;
726 register tree t = make_node (COMPLEX_CST);
728 TREE_REALPART (t) = real;
729 TREE_IMAGPART (t) = imag;
730 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
731 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
732 TREE_CONSTANT_OVERFLOW (t)
733 = TREE_CONSTANT_OVERFLOW (real) | TREE_CONSTANT_OVERFLOW (imag);
734 return t;
737 /* Build a newly constructed TREE_VEC node of length LEN. */
739 tree
740 make_tree_vec (len)
741 int len;
743 register tree t;
744 register int length = (len-1) * sizeof (tree) + sizeof (struct tree_vec);
746 #ifdef GATHER_STATISTICS
747 tree_node_counts[(int)vec_kind]++;
748 tree_node_sizes[(int)vec_kind] += length;
749 #endif
751 t = ggc_alloc_tree (length);
753 memset ((PTR) t, 0, length);
754 TREE_SET_CODE (t, TREE_VEC);
755 TREE_VEC_LENGTH (t) = len;
757 return t;
760 /* Return 1 if EXPR is the integer constant zero or a complex constant
761 of zero. */
764 integer_zerop (expr)
765 tree expr;
767 STRIP_NOPS (expr);
769 return ((TREE_CODE (expr) == INTEGER_CST
770 && ! TREE_CONSTANT_OVERFLOW (expr)
771 && TREE_INT_CST_LOW (expr) == 0
772 && TREE_INT_CST_HIGH (expr) == 0)
773 || (TREE_CODE (expr) == COMPLEX_CST
774 && integer_zerop (TREE_REALPART (expr))
775 && integer_zerop (TREE_IMAGPART (expr))));
778 /* Return 1 if EXPR is the integer constant one or the corresponding
779 complex constant. */
782 integer_onep (expr)
783 tree expr;
785 STRIP_NOPS (expr);
787 return ((TREE_CODE (expr) == INTEGER_CST
788 && ! TREE_CONSTANT_OVERFLOW (expr)
789 && TREE_INT_CST_LOW (expr) == 1
790 && TREE_INT_CST_HIGH (expr) == 0)
791 || (TREE_CODE (expr) == COMPLEX_CST
792 && integer_onep (TREE_REALPART (expr))
793 && integer_zerop (TREE_IMAGPART (expr))));
796 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
797 it contains. Likewise for the corresponding complex constant. */
800 integer_all_onesp (expr)
801 tree expr;
803 register int prec;
804 register int uns;
806 STRIP_NOPS (expr);
808 if (TREE_CODE (expr) == COMPLEX_CST
809 && integer_all_onesp (TREE_REALPART (expr))
810 && integer_zerop (TREE_IMAGPART (expr)))
811 return 1;
813 else if (TREE_CODE (expr) != INTEGER_CST
814 || TREE_CONSTANT_OVERFLOW (expr))
815 return 0;
817 uns = TREE_UNSIGNED (TREE_TYPE (expr));
818 if (!uns)
819 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
820 && TREE_INT_CST_HIGH (expr) == -1);
822 /* Note that using TYPE_PRECISION here is wrong. We care about the
823 actual bits, not the (arbitrary) range of the type. */
824 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)));
825 if (prec >= HOST_BITS_PER_WIDE_INT)
827 HOST_WIDE_INT high_value;
828 int shift_amount;
830 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
832 if (shift_amount > HOST_BITS_PER_WIDE_INT)
833 /* Can not handle precisions greater than twice the host int size. */
834 abort ();
835 else if (shift_amount == HOST_BITS_PER_WIDE_INT)
836 /* Shifting by the host word size is undefined according to the ANSI
837 standard, so we must handle this as a special case. */
838 high_value = -1;
839 else
840 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
842 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
843 && TREE_INT_CST_HIGH (expr) == high_value);
845 else
846 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
849 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
850 one bit on). */
853 integer_pow2p (expr)
854 tree expr;
856 int prec;
857 HOST_WIDE_INT high, low;
859 STRIP_NOPS (expr);
861 if (TREE_CODE (expr) == COMPLEX_CST
862 && integer_pow2p (TREE_REALPART (expr))
863 && integer_zerop (TREE_IMAGPART (expr)))
864 return 1;
866 if (TREE_CODE (expr) != INTEGER_CST || TREE_CONSTANT_OVERFLOW (expr))
867 return 0;
869 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
870 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
871 high = TREE_INT_CST_HIGH (expr);
872 low = TREE_INT_CST_LOW (expr);
874 /* First clear all bits that are beyond the type's precision in case
875 we've been sign extended. */
877 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
879 else if (prec > HOST_BITS_PER_WIDE_INT)
880 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
881 else
883 high = 0;
884 if (prec < HOST_BITS_PER_WIDE_INT)
885 low &= ~((HOST_WIDE_INT) (-1) << prec);
888 if (high == 0 && low == 0)
889 return 0;
891 return ((high == 0 && (low & (low - 1)) == 0)
892 || (low == 0 && (high & (high - 1)) == 0));
895 /* Return the power of two represented by a tree node known to be a
896 power of two. */
899 tree_log2 (expr)
900 tree expr;
902 int prec;
903 HOST_WIDE_INT high, low;
905 STRIP_NOPS (expr);
907 if (TREE_CODE (expr) == COMPLEX_CST)
908 return tree_log2 (TREE_REALPART (expr));
910 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
911 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
913 high = TREE_INT_CST_HIGH (expr);
914 low = TREE_INT_CST_LOW (expr);
916 /* First clear all bits that are beyond the type's precision in case
917 we've been sign extended. */
919 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
921 else if (prec > HOST_BITS_PER_WIDE_INT)
922 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
923 else
925 high = 0;
926 if (prec < HOST_BITS_PER_WIDE_INT)
927 low &= ~((HOST_WIDE_INT) (-1) << prec);
930 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
931 : exact_log2 (low));
934 /* Similar, but return the largest integer Y such that 2 ** Y is less
935 than or equal to EXPR. */
938 tree_floor_log2 (expr)
939 tree expr;
941 int prec;
942 HOST_WIDE_INT high, low;
944 STRIP_NOPS (expr);
946 if (TREE_CODE (expr) == COMPLEX_CST)
947 return tree_log2 (TREE_REALPART (expr));
949 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
950 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
952 high = TREE_INT_CST_HIGH (expr);
953 low = TREE_INT_CST_LOW (expr);
955 /* First clear all bits that are beyond the type's precision in case
956 we've been sign extended. Ignore if type's precision hasn't been set
957 since what we are doing is setting it. */
959 if (prec == 2 * HOST_BITS_PER_WIDE_INT || prec == 0)
961 else if (prec > HOST_BITS_PER_WIDE_INT)
962 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
963 else
965 high = 0;
966 if (prec < HOST_BITS_PER_WIDE_INT)
967 low &= ~((HOST_WIDE_INT) (-1) << prec);
970 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
971 : floor_log2 (low));
974 /* Return 1 if EXPR is the real constant zero. */
977 real_zerop (expr)
978 tree expr;
980 STRIP_NOPS (expr);
982 return ((TREE_CODE (expr) == REAL_CST
983 && ! TREE_CONSTANT_OVERFLOW (expr)
984 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0))
985 || (TREE_CODE (expr) == COMPLEX_CST
986 && real_zerop (TREE_REALPART (expr))
987 && real_zerop (TREE_IMAGPART (expr))));
990 /* Return 1 if EXPR is the real constant one in real or complex form. */
993 real_onep (expr)
994 tree expr;
996 STRIP_NOPS (expr);
998 return ((TREE_CODE (expr) == REAL_CST
999 && ! TREE_CONSTANT_OVERFLOW (expr)
1000 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1))
1001 || (TREE_CODE (expr) == COMPLEX_CST
1002 && real_onep (TREE_REALPART (expr))
1003 && real_zerop (TREE_IMAGPART (expr))));
1006 /* Return 1 if EXPR is the real constant two. */
1009 real_twop (expr)
1010 tree expr;
1012 STRIP_NOPS (expr);
1014 return ((TREE_CODE (expr) == REAL_CST
1015 && ! TREE_CONSTANT_OVERFLOW (expr)
1016 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2))
1017 || (TREE_CODE (expr) == COMPLEX_CST
1018 && real_twop (TREE_REALPART (expr))
1019 && real_zerop (TREE_IMAGPART (expr))));
1022 /* Nonzero if EXP is a constant or a cast of a constant. */
1025 really_constant_p (exp)
1026 tree exp;
1028 /* This is not quite the same as STRIP_NOPS. It does more. */
1029 while (TREE_CODE (exp) == NOP_EXPR
1030 || TREE_CODE (exp) == CONVERT_EXPR
1031 || TREE_CODE (exp) == NON_LVALUE_EXPR)
1032 exp = TREE_OPERAND (exp, 0);
1033 return TREE_CONSTANT (exp);
1036 /* Return first list element whose TREE_VALUE is ELEM.
1037 Return 0 if ELEM is not in LIST. */
1039 tree
1040 value_member (elem, list)
1041 tree elem, list;
1043 while (list)
1045 if (elem == TREE_VALUE (list))
1046 return list;
1047 list = TREE_CHAIN (list);
1049 return NULL_TREE;
1052 /* Return first list element whose TREE_PURPOSE is ELEM.
1053 Return 0 if ELEM is not in LIST. */
1055 tree
1056 purpose_member (elem, list)
1057 tree elem, list;
1059 while (list)
1061 if (elem == TREE_PURPOSE (list))
1062 return list;
1063 list = TREE_CHAIN (list);
1065 return NULL_TREE;
1068 /* Return first list element whose BINFO_TYPE is ELEM.
1069 Return 0 if ELEM is not in LIST. */
1071 tree
1072 binfo_member (elem, list)
1073 tree elem, list;
1075 while (list)
1077 if (elem == BINFO_TYPE (list))
1078 return list;
1079 list = TREE_CHAIN (list);
1081 return NULL_TREE;
1084 /* Return nonzero if ELEM is part of the chain CHAIN. */
1087 chain_member (elem, chain)
1088 tree elem, chain;
1090 while (chain)
1092 if (elem == chain)
1093 return 1;
1094 chain = TREE_CHAIN (chain);
1097 return 0;
1100 /* Return nonzero if ELEM is equal to TREE_VALUE (CHAIN) for any piece of
1101 chain CHAIN. This and the next function are currently unused, but
1102 are retained for completeness. */
1105 chain_member_value (elem, chain)
1106 tree elem, chain;
1108 while (chain)
1110 if (elem == TREE_VALUE (chain))
1111 return 1;
1112 chain = TREE_CHAIN (chain);
1115 return 0;
1118 /* Return nonzero if ELEM is equal to TREE_PURPOSE (CHAIN)
1119 for any piece of chain CHAIN. */
1122 chain_member_purpose (elem, chain)
1123 tree elem, chain;
1125 while (chain)
1127 if (elem == TREE_PURPOSE (chain))
1128 return 1;
1129 chain = TREE_CHAIN (chain);
1132 return 0;
1135 /* Return the length of a chain of nodes chained through TREE_CHAIN.
1136 We expect a null pointer to mark the end of the chain.
1137 This is the Lisp primitive `length'. */
1140 list_length (t)
1141 tree t;
1143 register tree tail;
1144 register int len = 0;
1146 for (tail = t; tail; tail = TREE_CHAIN (tail))
1147 len++;
1149 return len;
1152 /* Returns the number of FIELD_DECLs in TYPE. */
1155 fields_length (type)
1156 tree type;
1158 tree t = TYPE_FIELDS (type);
1159 int count = 0;
1161 for (; t; t = TREE_CHAIN (t))
1162 if (TREE_CODE (t) == FIELD_DECL)
1163 ++count;
1165 return count;
1168 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
1169 by modifying the last node in chain 1 to point to chain 2.
1170 This is the Lisp primitive `nconc'. */
1172 tree
1173 chainon (op1, op2)
1174 tree op1, op2;
1177 if (op1)
1179 register tree t1;
1180 #ifdef ENABLE_TREE_CHECKING
1181 register tree t2;
1182 #endif
1184 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
1186 TREE_CHAIN (t1) = op2;
1187 #ifdef ENABLE_TREE_CHECKING
1188 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
1189 if (t2 == t1)
1190 abort (); /* Circularity created. */
1191 #endif
1192 return op1;
1194 else
1195 return op2;
1198 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
1200 tree
1201 tree_last (chain)
1202 register tree chain;
1204 register tree next;
1205 if (chain)
1206 while ((next = TREE_CHAIN (chain)))
1207 chain = next;
1208 return chain;
1211 /* Reverse the order of elements in the chain T,
1212 and return the new head of the chain (old last element). */
1214 tree
1215 nreverse (t)
1216 tree t;
1218 register tree prev = 0, decl, next;
1219 for (decl = t; decl; decl = next)
1221 next = TREE_CHAIN (decl);
1222 TREE_CHAIN (decl) = prev;
1223 prev = decl;
1225 return prev;
1228 /* Given a chain CHAIN of tree nodes,
1229 construct and return a list of those nodes. */
1231 tree
1232 listify (chain)
1233 tree chain;
1235 tree result = NULL_TREE;
1236 tree in_tail = chain;
1237 tree out_tail = NULL_TREE;
1239 while (in_tail)
1241 tree next = tree_cons (NULL_TREE, in_tail, NULL_TREE);
1242 if (out_tail)
1243 TREE_CHAIN (out_tail) = next;
1244 else
1245 result = next;
1246 out_tail = next;
1247 in_tail = TREE_CHAIN (in_tail);
1250 return result;
1253 /* Return a newly created TREE_LIST node whose
1254 purpose and value fields are PARM and VALUE. */
1256 tree
1257 build_tree_list (parm, value)
1258 tree parm, value;
1260 register tree t = make_node (TREE_LIST);
1261 TREE_PURPOSE (t) = parm;
1262 TREE_VALUE (t) = value;
1263 return t;
1266 /* Return a newly created TREE_LIST node whose
1267 purpose and value fields are PARM and VALUE
1268 and whose TREE_CHAIN is CHAIN. */
1270 tree
1271 tree_cons (purpose, value, chain)
1272 tree purpose, value, chain;
1274 register tree node;
1276 node = ggc_alloc_tree (sizeof (struct tree_list));
1278 memset (node, 0, sizeof (struct tree_common));
1280 #ifdef GATHER_STATISTICS
1281 tree_node_counts[(int) x_kind]++;
1282 tree_node_sizes[(int) x_kind] += sizeof (struct tree_list);
1283 #endif
1285 TREE_SET_CODE (node, TREE_LIST);
1286 TREE_CHAIN (node) = chain;
1287 TREE_PURPOSE (node) = purpose;
1288 TREE_VALUE (node) = value;
1289 return node;
1293 /* Return the size nominally occupied by an object of type TYPE
1294 when it resides in memory. The value is measured in units of bytes,
1295 and its data type is that normally used for type sizes
1296 (which is the first type created by make_signed_type or
1297 make_unsigned_type). */
1299 tree
1300 size_in_bytes (type)
1301 tree type;
1303 tree t;
1305 if (type == error_mark_node)
1306 return integer_zero_node;
1308 type = TYPE_MAIN_VARIANT (type);
1309 t = TYPE_SIZE_UNIT (type);
1311 if (t == 0)
1313 incomplete_type_error (NULL_TREE, type);
1314 return size_zero_node;
1317 if (TREE_CODE (t) == INTEGER_CST)
1318 force_fit_type (t, 0);
1320 return t;
1323 /* Return the size of TYPE (in bytes) as a wide integer
1324 or return -1 if the size can vary or is larger than an integer. */
1326 HOST_WIDE_INT
1327 int_size_in_bytes (type)
1328 tree type;
1330 tree t;
1332 if (type == error_mark_node)
1333 return 0;
1335 type = TYPE_MAIN_VARIANT (type);
1336 t = TYPE_SIZE_UNIT (type);
1337 if (t == 0
1338 || TREE_CODE (t) != INTEGER_CST
1339 || TREE_OVERFLOW (t)
1340 || TREE_INT_CST_HIGH (t) != 0
1341 /* If the result would appear negative, it's too big to represent. */
1342 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
1343 return -1;
1345 return TREE_INT_CST_LOW (t);
1348 /* Return the bit position of FIELD, in bits from the start of the record.
1349 This is a tree of type bitsizetype. */
1351 tree
1352 bit_position (field)
1353 tree field;
1356 return bit_from_pos (DECL_FIELD_OFFSET (field),
1357 DECL_FIELD_BIT_OFFSET (field));
1360 /* Likewise, but return as an integer. Abort if it cannot be represented
1361 in that way (since it could be a signed value, we don't have the option
1362 of returning -1 like int_size_in_byte can. */
1364 HOST_WIDE_INT
1365 int_bit_position (field)
1366 tree field;
1368 return tree_low_cst (bit_position (field), 0);
1371 /* Return the byte position of FIELD, in bytes from the start of the record.
1372 This is a tree of type sizetype. */
1374 tree
1375 byte_position (field)
1376 tree field;
1378 return byte_from_pos (DECL_FIELD_OFFSET (field),
1379 DECL_FIELD_BIT_OFFSET (field));
1382 /* Likewise, but return as an integer. Abort if it cannot be represented
1383 in that way (since it could be a signed value, we don't have the option
1384 of returning -1 like int_size_in_byte can. */
1386 HOST_WIDE_INT
1387 int_byte_position (field)
1388 tree field;
1390 return tree_low_cst (byte_position (field), 0);
1393 /* Return the strictest alignment, in bits, that T is known to have. */
1395 unsigned int
1396 expr_align (t)
1397 tree t;
1399 unsigned int align0, align1;
1401 switch (TREE_CODE (t))
1403 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
1404 /* If we have conversions, we know that the alignment of the
1405 object must meet each of the alignments of the types. */
1406 align0 = expr_align (TREE_OPERAND (t, 0));
1407 align1 = TYPE_ALIGN (TREE_TYPE (t));
1408 return MAX (align0, align1);
1410 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
1411 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
1412 case WITH_RECORD_EXPR: case CLEANUP_POINT_EXPR: case UNSAVE_EXPR:
1413 /* These don't change the alignment of an object. */
1414 return expr_align (TREE_OPERAND (t, 0));
1416 case COND_EXPR:
1417 /* The best we can do is say that the alignment is the least aligned
1418 of the two arms. */
1419 align0 = expr_align (TREE_OPERAND (t, 1));
1420 align1 = expr_align (TREE_OPERAND (t, 2));
1421 return MIN (align0, align1);
1423 case LABEL_DECL: case CONST_DECL:
1424 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
1425 if (DECL_ALIGN (t) != 0)
1426 return DECL_ALIGN (t);
1427 break;
1429 case FUNCTION_DECL:
1430 return FUNCTION_BOUNDARY;
1432 default:
1433 break;
1436 /* Otherwise take the alignment from that of the type. */
1437 return TYPE_ALIGN (TREE_TYPE (t));
1440 /* Return, as a tree node, the number of elements for TYPE (which is an
1441 ARRAY_TYPE) minus one. This counts only elements of the top array. */
1443 tree
1444 array_type_nelts (type)
1445 tree type;
1447 tree index_type, min, max;
1449 /* If they did it with unspecified bounds, then we should have already
1450 given an error about it before we got here. */
1451 if (! TYPE_DOMAIN (type))
1452 return error_mark_node;
1454 index_type = TYPE_DOMAIN (type);
1455 min = TYPE_MIN_VALUE (index_type);
1456 max = TYPE_MAX_VALUE (index_type);
1458 return (integer_zerop (min)
1459 ? max
1460 : fold (build (MINUS_EXPR, TREE_TYPE (max), max, min)));
1463 /* Return nonzero if arg is static -- a reference to an object in
1464 static storage. This is not the same as the C meaning of `static'. */
1467 staticp (arg)
1468 tree arg;
1470 switch (TREE_CODE (arg))
1472 case FUNCTION_DECL:
1473 /* Nested functions aren't static, since taking their address
1474 involves a trampoline. */
1475 return (decl_function_context (arg) == 0 || DECL_NO_STATIC_CHAIN (arg))
1476 && ! DECL_NON_ADDR_CONST_P (arg);
1478 case VAR_DECL:
1479 return (TREE_STATIC (arg) || DECL_EXTERNAL (arg))
1480 && ! DECL_NON_ADDR_CONST_P (arg);
1482 case CONSTRUCTOR:
1483 return TREE_STATIC (arg);
1485 case LABEL_DECL:
1486 case STRING_CST:
1487 return 1;
1489 /* If we are referencing a bitfield, we can't evaluate an
1490 ADDR_EXPR at compile time and so it isn't a constant. */
1491 case COMPONENT_REF:
1492 return (! DECL_BIT_FIELD (TREE_OPERAND (arg, 1))
1493 && staticp (TREE_OPERAND (arg, 0)));
1495 case BIT_FIELD_REF:
1496 return 0;
1498 #if 0
1499 /* This case is technically correct, but results in setting
1500 TREE_CONSTANT on ADDR_EXPRs that cannot be evaluated at
1501 compile time. */
1502 case INDIRECT_REF:
1503 return TREE_CONSTANT (TREE_OPERAND (arg, 0));
1504 #endif
1506 case ARRAY_REF:
1507 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
1508 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
1509 return staticp (TREE_OPERAND (arg, 0));
1511 default:
1512 return 0;
1516 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
1517 Do this to any expression which may be used in more than one place,
1518 but must be evaluated only once.
1520 Normally, expand_expr would reevaluate the expression each time.
1521 Calling save_expr produces something that is evaluated and recorded
1522 the first time expand_expr is called on it. Subsequent calls to
1523 expand_expr just reuse the recorded value.
1525 The call to expand_expr that generates code that actually computes
1526 the value is the first call *at compile time*. Subsequent calls
1527 *at compile time* generate code to use the saved value.
1528 This produces correct result provided that *at run time* control
1529 always flows through the insns made by the first expand_expr
1530 before reaching the other places where the save_expr was evaluated.
1531 You, the caller of save_expr, must make sure this is so.
1533 Constants, and certain read-only nodes, are returned with no
1534 SAVE_EXPR because that is safe. Expressions containing placeholders
1535 are not touched; see tree.def for an explanation of what these
1536 are used for. */
1538 tree
1539 save_expr (expr)
1540 tree expr;
1542 register tree t = fold (expr);
1544 /* We don't care about whether this can be used as an lvalue in this
1545 context. */
1546 while (TREE_CODE (t) == NON_LVALUE_EXPR)
1547 t = TREE_OPERAND (t, 0);
1549 /* If the tree evaluates to a constant, then we don't want to hide that
1550 fact (i.e. this allows further folding, and direct checks for constants).
1551 However, a read-only object that has side effects cannot be bypassed.
1552 Since it is no problem to reevaluate literals, we just return the
1553 literal node. */
1555 if (TREE_CONSTANT (t) || (TREE_READONLY (t) && ! TREE_SIDE_EFFECTS (t))
1556 || TREE_CODE (t) == SAVE_EXPR || TREE_CODE (t) == ERROR_MARK)
1557 return t;
1559 /* If T contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
1560 it means that the size or offset of some field of an object depends on
1561 the value within another field.
1563 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
1564 and some variable since it would then need to be both evaluated once and
1565 evaluated more than once. Front-ends must assure this case cannot
1566 happen by surrounding any such subexpressions in their own SAVE_EXPR
1567 and forcing evaluation at the proper time. */
1568 if (contains_placeholder_p (t))
1569 return t;
1571 t = build (SAVE_EXPR, TREE_TYPE (expr), t, current_function_decl, NULL_TREE);
1573 /* This expression might be placed ahead of a jump to ensure that the
1574 value was computed on both sides of the jump. So make sure it isn't
1575 eliminated as dead. */
1576 TREE_SIDE_EFFECTS (t) = 1;
1577 TREE_READONLY (t) = 1;
1578 return t;
1581 /* Arrange for an expression to be expanded multiple independent
1582 times. This is useful for cleanup actions, as the backend can
1583 expand them multiple times in different places. */
1585 tree
1586 unsave_expr (expr)
1587 tree expr;
1589 tree t;
1591 /* If this is already protected, no sense in protecting it again. */
1592 if (TREE_CODE (expr) == UNSAVE_EXPR)
1593 return expr;
1595 t = build1 (UNSAVE_EXPR, TREE_TYPE (expr), expr);
1596 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (expr);
1597 return t;
1600 /* Returns the index of the first non-tree operand for CODE, or the number
1601 of operands if all are trees. */
1604 first_rtl_op (code)
1605 enum tree_code code;
1607 switch (code)
1609 case SAVE_EXPR:
1610 return 2;
1611 case GOTO_SUBROUTINE_EXPR:
1612 case RTL_EXPR:
1613 return 0;
1614 case WITH_CLEANUP_EXPR:
1615 /* Should be defined to be 2. */
1616 return 1;
1617 case METHOD_CALL_EXPR:
1618 return 3;
1619 default:
1620 return TREE_CODE_LENGTH (code);
1624 /* Perform any modifications to EXPR required when it is unsaved. Does
1625 not recurse into EXPR's subtrees. */
1627 void
1628 unsave_expr_1 (expr)
1629 tree expr;
1631 switch (TREE_CODE (expr))
1633 case SAVE_EXPR:
1634 if (! SAVE_EXPR_PERSISTENT_P (expr))
1635 SAVE_EXPR_RTL (expr) = 0;
1636 break;
1638 case TARGET_EXPR:
1639 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
1640 It's OK for this to happen if it was part of a subtree that
1641 isn't immediately expanded, such as operand 2 of another
1642 TARGET_EXPR. */
1643 if (TREE_OPERAND (expr, 1))
1644 break;
1646 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
1647 TREE_OPERAND (expr, 3) = NULL_TREE;
1648 break;
1650 case RTL_EXPR:
1651 /* I don't yet know how to emit a sequence multiple times. */
1652 if (RTL_EXPR_SEQUENCE (expr) != 0)
1653 abort ();
1654 break;
1656 default:
1657 if (lang_unsave_expr_now != 0)
1658 (*lang_unsave_expr_now) (expr);
1659 break;
1663 /* Helper function for unsave_expr_now. */
1665 static void
1666 unsave_expr_now_r (expr)
1667 tree expr;
1669 enum tree_code code;
1671 /* There's nothing to do for NULL_TREE. */
1672 if (expr == 0)
1673 return;
1675 unsave_expr_1 (expr);
1677 code = TREE_CODE (expr);
1678 switch (TREE_CODE_CLASS (code))
1680 case 'c': /* a constant */
1681 case 't': /* a type node */
1682 case 'd': /* A decl node */
1683 case 'b': /* A block node */
1684 break;
1686 case 'x': /* miscellaneous: e.g., identifier, TREE_LIST or ERROR_MARK. */
1687 if (code == TREE_LIST)
1689 unsave_expr_now_r (TREE_VALUE (expr));
1690 unsave_expr_now_r (TREE_CHAIN (expr));
1692 break;
1694 case 'e': /* an expression */
1695 case 'r': /* a reference */
1696 case 's': /* an expression with side effects */
1697 case '<': /* a comparison expression */
1698 case '2': /* a binary arithmetic expression */
1699 case '1': /* a unary arithmetic expression */
1701 int i;
1703 for (i = first_rtl_op (code) - 1; i >= 0; i--)
1704 unsave_expr_now_r (TREE_OPERAND (expr, i));
1706 break;
1708 default:
1709 abort ();
1713 /* Modify a tree in place so that all the evaluate only once things
1714 are cleared out. Return the EXPR given. */
1716 tree
1717 unsave_expr_now (expr)
1718 tree expr;
1720 if (lang_unsave!= 0)
1721 (*lang_unsave) (&expr);
1722 else
1723 unsave_expr_now_r (expr);
1725 return expr;
1728 /* Return 0 if it is safe to evaluate EXPR multiple times,
1729 return 1 if it is safe if EXPR is unsaved afterward, or
1730 return 2 if it is completely unsafe.
1732 This assumes that CALL_EXPRs and TARGET_EXPRs are never replicated in
1733 an expression tree, so that it safe to unsave them and the surrounding
1734 context will be correct.
1736 SAVE_EXPRs basically *only* appear replicated in an expression tree,
1737 occasionally across the whole of a function. It is therefore only
1738 safe to unsave a SAVE_EXPR if you know that all occurrences appear
1739 below the UNSAVE_EXPR.
1741 RTL_EXPRs consume their rtl during evaluation. It is therefore
1742 never possible to unsave them. */
1745 unsafe_for_reeval (expr)
1746 tree expr;
1748 int unsafeness = 0;
1749 enum tree_code code;
1750 int i, tmp;
1751 tree exp;
1752 int first_rtl;
1754 if (expr == NULL_TREE)
1755 return 1;
1757 code = TREE_CODE (expr);
1758 first_rtl = first_rtl_op (code);
1760 switch (code)
1762 case SAVE_EXPR:
1763 case RTL_EXPR:
1764 return 2;
1766 case TREE_LIST:
1767 for (exp = expr; exp != 0; exp = TREE_CHAIN (exp))
1769 tmp = unsafe_for_reeval (TREE_VALUE (exp));
1770 unsafeness = MAX (tmp, unsafeness);
1773 return unsafeness;
1775 case CALL_EXPR:
1776 tmp = unsafe_for_reeval (TREE_OPERAND (expr, 1));
1777 return MAX (tmp, 1);
1779 case TARGET_EXPR:
1780 unsafeness = 1;
1781 break;
1783 default:
1784 if (lang_unsafe_for_reeval != 0)
1786 tmp = (*lang_unsafe_for_reeval) (expr);
1787 if (tmp >= 0)
1788 return tmp;
1790 break;
1793 switch (TREE_CODE_CLASS (code))
1795 case 'c': /* a constant */
1796 case 't': /* a type node */
1797 case 'x': /* something random, like an identifier or an ERROR_MARK. */
1798 case 'd': /* A decl node */
1799 case 'b': /* A block node */
1800 return 0;
1802 case 'e': /* an expression */
1803 case 'r': /* a reference */
1804 case 's': /* an expression with side effects */
1805 case '<': /* a comparison expression */
1806 case '2': /* a binary arithmetic expression */
1807 case '1': /* a unary arithmetic expression */
1808 for (i = first_rtl - 1; i >= 0; i--)
1810 tmp = unsafe_for_reeval (TREE_OPERAND (expr, i));
1811 unsafeness = MAX (tmp, unsafeness);
1814 return unsafeness;
1816 default:
1817 return 2;
1821 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
1822 or offset that depends on a field within a record. */
1825 contains_placeholder_p (exp)
1826 tree exp;
1828 register enum tree_code code;
1829 int result;
1831 if (!exp)
1832 return 0;
1834 /* If we have a WITH_RECORD_EXPR, it "cancels" any PLACEHOLDER_EXPR
1835 in it since it is supplying a value for it. */
1836 code = TREE_CODE (exp);
1837 if (code == WITH_RECORD_EXPR)
1838 return 0;
1839 else if (code == PLACEHOLDER_EXPR)
1840 return 1;
1842 switch (TREE_CODE_CLASS (code))
1844 case 'r':
1845 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
1846 position computations since they will be converted into a
1847 WITH_RECORD_EXPR involving the reference, which will assume
1848 here will be valid. */
1849 return contains_placeholder_p (TREE_OPERAND (exp, 0));
1851 case 'x':
1852 if (code == TREE_LIST)
1853 return (contains_placeholder_p (TREE_VALUE (exp))
1854 || (TREE_CHAIN (exp) != 0
1855 && contains_placeholder_p (TREE_CHAIN (exp))));
1856 break;
1858 case '1':
1859 case '2': case '<':
1860 case 'e':
1861 switch (code)
1863 case COMPOUND_EXPR:
1864 /* Ignoring the first operand isn't quite right, but works best. */
1865 return contains_placeholder_p (TREE_OPERAND (exp, 1));
1867 case RTL_EXPR:
1868 case CONSTRUCTOR:
1869 return 0;
1871 case COND_EXPR:
1872 return (contains_placeholder_p (TREE_OPERAND (exp, 0))
1873 || contains_placeholder_p (TREE_OPERAND (exp, 1))
1874 || contains_placeholder_p (TREE_OPERAND (exp, 2)));
1876 case SAVE_EXPR:
1877 /* If we already know this doesn't have a placeholder, don't
1878 check again. */
1879 if (SAVE_EXPR_NOPLACEHOLDER (exp) || SAVE_EXPR_RTL (exp) != 0)
1880 return 0;
1882 SAVE_EXPR_NOPLACEHOLDER (exp) = 1;
1883 result = contains_placeholder_p (TREE_OPERAND (exp, 0));
1884 if (result)
1885 SAVE_EXPR_NOPLACEHOLDER (exp) = 0;
1887 return result;
1889 case CALL_EXPR:
1890 return (TREE_OPERAND (exp, 1) != 0
1891 && contains_placeholder_p (TREE_OPERAND (exp, 1)));
1893 default:
1894 break;
1897 switch (TREE_CODE_LENGTH (code))
1899 case 1:
1900 return contains_placeholder_p (TREE_OPERAND (exp, 0));
1901 case 2:
1902 return (contains_placeholder_p (TREE_OPERAND (exp, 0))
1903 || contains_placeholder_p (TREE_OPERAND (exp, 1)));
1904 default:
1905 return 0;
1908 default:
1909 return 0;
1911 return 0;
1914 /* Return 1 if EXP contains any expressions that produce cleanups for an
1915 outer scope to deal with. Used by fold. */
1918 has_cleanups (exp)
1919 tree exp;
1921 int i, nops, cmp;
1923 if (! TREE_SIDE_EFFECTS (exp))
1924 return 0;
1926 switch (TREE_CODE (exp))
1928 case TARGET_EXPR:
1929 case GOTO_SUBROUTINE_EXPR:
1930 case WITH_CLEANUP_EXPR:
1931 return 1;
1933 case CLEANUP_POINT_EXPR:
1934 return 0;
1936 case CALL_EXPR:
1937 for (exp = TREE_OPERAND (exp, 1); exp; exp = TREE_CHAIN (exp))
1939 cmp = has_cleanups (TREE_VALUE (exp));
1940 if (cmp)
1941 return cmp;
1943 return 0;
1945 default:
1946 break;
1949 /* This general rule works for most tree codes. All exceptions should be
1950 handled above. If this is a language-specific tree code, we can't
1951 trust what might be in the operand, so say we don't know
1952 the situation. */
1953 if ((int) TREE_CODE (exp) >= (int) LAST_AND_UNUSED_TREE_CODE)
1954 return -1;
1956 nops = first_rtl_op (TREE_CODE (exp));
1957 for (i = 0; i < nops; i++)
1958 if (TREE_OPERAND (exp, i) != 0)
1960 int type = TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (exp, i)));
1961 if (type == 'e' || type == '<' || type == '1' || type == '2'
1962 || type == 'r' || type == 's')
1964 cmp = has_cleanups (TREE_OPERAND (exp, i));
1965 if (cmp)
1966 return cmp;
1970 return 0;
1973 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
1974 return a tree with all occurrences of references to F in a
1975 PLACEHOLDER_EXPR replaced by R. Note that we assume here that EXP
1976 contains only arithmetic expressions or a CALL_EXPR with a
1977 PLACEHOLDER_EXPR occurring only in its arglist. */
1979 tree
1980 substitute_in_expr (exp, f, r)
1981 tree exp;
1982 tree f;
1983 tree r;
1985 enum tree_code code = TREE_CODE (exp);
1986 tree op0, op1, op2;
1987 tree new;
1988 tree inner;
1990 switch (TREE_CODE_CLASS (code))
1992 case 'c':
1993 case 'd':
1994 return exp;
1996 case 'x':
1997 if (code == PLACEHOLDER_EXPR)
1998 return exp;
1999 else if (code == TREE_LIST)
2001 op0 = (TREE_CHAIN (exp) == 0
2002 ? 0 : substitute_in_expr (TREE_CHAIN (exp), f, r));
2003 op1 = substitute_in_expr (TREE_VALUE (exp), f, r);
2004 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
2005 return exp;
2007 return tree_cons (TREE_PURPOSE (exp), op1, op0);
2010 abort ();
2012 case '1':
2013 case '2':
2014 case '<':
2015 case 'e':
2016 switch (TREE_CODE_LENGTH (code))
2018 case 1:
2019 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2020 if (op0 == TREE_OPERAND (exp, 0))
2021 return exp;
2023 if (code == NON_LVALUE_EXPR)
2024 return op0;
2026 new = fold (build1 (code, TREE_TYPE (exp), op0));
2027 break;
2029 case 2:
2030 /* An RTL_EXPR cannot contain a PLACEHOLDER_EXPR; a CONSTRUCTOR
2031 could, but we don't support it. */
2032 if (code == RTL_EXPR)
2033 return exp;
2034 else if (code == CONSTRUCTOR)
2035 abort ();
2037 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2038 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
2039 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
2040 return exp;
2042 new = fold (build (code, TREE_TYPE (exp), op0, op1));
2043 break;
2045 case 3:
2046 /* It cannot be that anything inside a SAVE_EXPR contains a
2047 PLACEHOLDER_EXPR. */
2048 if (code == SAVE_EXPR)
2049 return exp;
2051 else if (code == CALL_EXPR)
2053 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
2054 if (op1 == TREE_OPERAND (exp, 1))
2055 return exp;
2057 return build (code, TREE_TYPE (exp),
2058 TREE_OPERAND (exp, 0), op1, NULL_TREE);
2061 else if (code != COND_EXPR)
2062 abort ();
2064 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2065 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
2066 op2 = substitute_in_expr (TREE_OPERAND (exp, 2), f, r);
2067 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2068 && op2 == TREE_OPERAND (exp, 2))
2069 return exp;
2071 new = fold (build (code, TREE_TYPE (exp), op0, op1, op2));
2072 break;
2074 default:
2075 abort ();
2078 break;
2080 case 'r':
2081 switch (code)
2083 case COMPONENT_REF:
2084 /* If this expression is getting a value from a PLACEHOLDER_EXPR
2085 and it is the right field, replace it with R. */
2086 for (inner = TREE_OPERAND (exp, 0);
2087 TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
2088 inner = TREE_OPERAND (inner, 0))
2090 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2091 && TREE_OPERAND (exp, 1) == f)
2092 return r;
2094 /* If this expression hasn't been completed let, leave it
2095 alone. */
2096 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
2097 && TREE_TYPE (inner) == 0)
2098 return exp;
2100 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2101 if (op0 == TREE_OPERAND (exp, 0))
2102 return exp;
2104 new = fold (build (code, TREE_TYPE (exp), op0,
2105 TREE_OPERAND (exp, 1)));
2106 break;
2108 case BIT_FIELD_REF:
2109 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2110 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
2111 op2 = substitute_in_expr (TREE_OPERAND (exp, 2), f, r);
2112 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2113 && op2 == TREE_OPERAND (exp, 2))
2114 return exp;
2116 new = fold (build (code, TREE_TYPE (exp), op0, op1, op2));
2117 break;
2119 case INDIRECT_REF:
2120 case BUFFER_REF:
2121 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2122 if (op0 == TREE_OPERAND (exp, 0))
2123 return exp;
2125 new = fold (build1 (code, TREE_TYPE (exp), op0));
2126 break;
2128 default:
2129 abort ();
2131 break;
2133 default:
2134 abort ();
2137 TREE_READONLY (new) = TREE_READONLY (exp);
2138 return new;
2141 /* Stabilize a reference so that we can use it any number of times
2142 without causing its operands to be evaluated more than once.
2143 Returns the stabilized reference. This works by means of save_expr,
2144 so see the caveats in the comments about save_expr.
2146 Also allows conversion expressions whose operands are references.
2147 Any other kind of expression is returned unchanged. */
2149 tree
2150 stabilize_reference (ref)
2151 tree ref;
2153 register tree result;
2154 register enum tree_code code = TREE_CODE (ref);
2156 switch (code)
2158 case VAR_DECL:
2159 case PARM_DECL:
2160 case RESULT_DECL:
2161 /* No action is needed in this case. */
2162 return ref;
2164 case NOP_EXPR:
2165 case CONVERT_EXPR:
2166 case FLOAT_EXPR:
2167 case FIX_TRUNC_EXPR:
2168 case FIX_FLOOR_EXPR:
2169 case FIX_ROUND_EXPR:
2170 case FIX_CEIL_EXPR:
2171 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2172 break;
2174 case INDIRECT_REF:
2175 result = build_nt (INDIRECT_REF,
2176 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2177 break;
2179 case COMPONENT_REF:
2180 result = build_nt (COMPONENT_REF,
2181 stabilize_reference (TREE_OPERAND (ref, 0)),
2182 TREE_OPERAND (ref, 1));
2183 break;
2185 case BIT_FIELD_REF:
2186 result = build_nt (BIT_FIELD_REF,
2187 stabilize_reference (TREE_OPERAND (ref, 0)),
2188 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2189 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2190 break;
2192 case ARRAY_REF:
2193 result = build_nt (ARRAY_REF,
2194 stabilize_reference (TREE_OPERAND (ref, 0)),
2195 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2196 break;
2198 case COMPOUND_EXPR:
2199 /* We cannot wrap the first expression in a SAVE_EXPR, as then
2200 it wouldn't be ignored. This matters when dealing with
2201 volatiles. */
2202 return stabilize_reference_1 (ref);
2204 case RTL_EXPR:
2205 result = build1 (INDIRECT_REF, TREE_TYPE (ref),
2206 save_expr (build1 (ADDR_EXPR,
2207 build_pointer_type (TREE_TYPE (ref)),
2208 ref)));
2209 break;
2211 /* If arg isn't a kind of lvalue we recognize, make no change.
2212 Caller should recognize the error for an invalid lvalue. */
2213 default:
2214 return ref;
2216 case ERROR_MARK:
2217 return error_mark_node;
2220 TREE_TYPE (result) = TREE_TYPE (ref);
2221 TREE_READONLY (result) = TREE_READONLY (ref);
2222 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
2223 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
2225 return result;
2228 /* Subroutine of stabilize_reference; this is called for subtrees of
2229 references. Any expression with side-effects must be put in a SAVE_EXPR
2230 to ensure that it is only evaluated once.
2232 We don't put SAVE_EXPR nodes around everything, because assigning very
2233 simple expressions to temporaries causes us to miss good opportunities
2234 for optimizations. Among other things, the opportunity to fold in the
2235 addition of a constant into an addressing mode often gets lost, e.g.
2236 "y[i+1] += x;". In general, we take the approach that we should not make
2237 an assignment unless we are forced into it - i.e., that any non-side effect
2238 operator should be allowed, and that cse should take care of coalescing
2239 multiple utterances of the same expression should that prove fruitful. */
2241 tree
2242 stabilize_reference_1 (e)
2243 tree e;
2245 register tree result;
2246 register enum tree_code code = TREE_CODE (e);
2248 /* We cannot ignore const expressions because it might be a reference
2249 to a const array but whose index contains side-effects. But we can
2250 ignore things that are actual constant or that already have been
2251 handled by this function. */
2253 if (TREE_CONSTANT (e) || code == SAVE_EXPR)
2254 return e;
2256 switch (TREE_CODE_CLASS (code))
2258 case 'x':
2259 case 't':
2260 case 'd':
2261 case 'b':
2262 case '<':
2263 case 's':
2264 case 'e':
2265 case 'r':
2266 /* If the expression has side-effects, then encase it in a SAVE_EXPR
2267 so that it will only be evaluated once. */
2268 /* The reference (r) and comparison (<) classes could be handled as
2269 below, but it is generally faster to only evaluate them once. */
2270 if (TREE_SIDE_EFFECTS (e))
2271 return save_expr (e);
2272 return e;
2274 case 'c':
2275 /* Constants need no processing. In fact, we should never reach
2276 here. */
2277 return e;
2279 case '2':
2280 /* Division is slow and tends to be compiled with jumps,
2281 especially the division by powers of 2 that is often
2282 found inside of an array reference. So do it just once. */
2283 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
2284 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
2285 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
2286 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
2287 return save_expr (e);
2288 /* Recursively stabilize each operand. */
2289 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
2290 stabilize_reference_1 (TREE_OPERAND (e, 1)));
2291 break;
2293 case '1':
2294 /* Recursively stabilize each operand. */
2295 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
2296 break;
2298 default:
2299 abort ();
2302 TREE_TYPE (result) = TREE_TYPE (e);
2303 TREE_READONLY (result) = TREE_READONLY (e);
2304 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
2305 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
2307 return result;
2310 /* Low-level constructors for expressions. */
2312 /* Build an expression of code CODE, data type TYPE,
2313 and operands as specified by the arguments ARG1 and following arguments.
2314 Expressions and reference nodes can be created this way.
2315 Constants, decls, types and misc nodes cannot be. */
2317 tree
2318 build VPARAMS ((enum tree_code code, tree tt, ...))
2320 #ifndef ANSI_PROTOTYPES
2321 enum tree_code code;
2322 tree tt;
2323 #endif
2324 va_list p;
2325 register tree t;
2326 register int length;
2327 register int i;
2328 int fro;
2330 VA_START (p, tt);
2332 #ifndef ANSI_PROTOTYPES
2333 code = va_arg (p, enum tree_code);
2334 tt = va_arg (p, tree);
2335 #endif
2337 t = make_node (code);
2338 length = TREE_CODE_LENGTH (code);
2339 TREE_TYPE (t) = tt;
2341 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
2342 result based on those same flags for the arguments. But if the
2343 arguments aren't really even `tree' expressions, we shouldn't be trying
2344 to do this. */
2345 fro = first_rtl_op (code);
2347 if (length == 2)
2349 /* This is equivalent to the loop below, but faster. */
2350 register tree arg0 = va_arg (p, tree);
2351 register tree arg1 = va_arg (p, tree);
2353 TREE_OPERAND (t, 0) = arg0;
2354 TREE_OPERAND (t, 1) = arg1;
2355 TREE_READONLY (t) = 1;
2356 if (arg0 && fro > 0)
2358 if (TREE_SIDE_EFFECTS (arg0))
2359 TREE_SIDE_EFFECTS (t) = 1;
2360 if (!TREE_READONLY (arg0))
2361 TREE_READONLY (t) = 0;
2364 if (arg1 && fro > 1)
2366 if (TREE_SIDE_EFFECTS (arg1))
2367 TREE_SIDE_EFFECTS (t) = 1;
2368 if (!TREE_READONLY (arg1))
2369 TREE_READONLY (t) = 0;
2372 else if (length == 1)
2374 register tree arg0 = va_arg (p, tree);
2376 /* The only one-operand cases we handle here are those with side-effects.
2377 Others are handled with build1. So don't bother checked if the
2378 arg has side-effects since we'll already have set it.
2380 ??? This really should use build1 too. */
2381 if (TREE_CODE_CLASS (code) != 's')
2382 abort ();
2383 TREE_OPERAND (t, 0) = arg0;
2385 else
2387 for (i = 0; i < length; i++)
2389 register tree operand = va_arg (p, tree);
2391 TREE_OPERAND (t, i) = operand;
2392 if (operand && fro > i)
2394 if (TREE_SIDE_EFFECTS (operand))
2395 TREE_SIDE_EFFECTS (t) = 1;
2399 va_end (p);
2400 return t;
2403 /* Same as above, but only builds for unary operators.
2404 Saves lions share of calls to `build'; cuts down use
2405 of varargs, which is expensive for RISC machines. */
2407 tree
2408 build1 (code, type, node)
2409 enum tree_code code;
2410 tree type;
2411 tree node;
2413 register int length;
2414 #ifdef GATHER_STATISTICS
2415 register tree_node_kind kind;
2416 #endif
2417 register tree t;
2419 #ifdef GATHER_STATISTICS
2420 if (TREE_CODE_CLASS (code) == 'r')
2421 kind = r_kind;
2422 else
2423 kind = e_kind;
2424 #endif
2426 length = sizeof (struct tree_exp);
2428 t = ggc_alloc_tree (length);
2430 memset ((PTR) t, 0, sizeof (struct tree_common));
2432 #ifdef GATHER_STATISTICS
2433 tree_node_counts[(int) kind]++;
2434 tree_node_sizes[(int) kind] += length;
2435 #endif
2437 TREE_SET_CODE (t, code);
2439 TREE_TYPE (t) = type;
2440 TREE_COMPLEXITY (t) = 0;
2441 TREE_OPERAND (t, 0) = node;
2442 if (node && first_rtl_op (code) != 0)
2444 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
2445 TREE_READONLY (t) = TREE_READONLY (node);
2448 switch (code)
2450 case INIT_EXPR:
2451 case MODIFY_EXPR:
2452 case VA_ARG_EXPR:
2453 case RTL_EXPR:
2454 case PREDECREMENT_EXPR:
2455 case PREINCREMENT_EXPR:
2456 case POSTDECREMENT_EXPR:
2457 case POSTINCREMENT_EXPR:
2458 /* All of these have side-effects, no matter what their
2459 operands are. */
2460 TREE_SIDE_EFFECTS (t) = 1;
2461 TREE_READONLY (t) = 0;
2462 break;
2464 default:
2465 break;
2468 return t;
2471 /* Similar except don't specify the TREE_TYPE
2472 and leave the TREE_SIDE_EFFECTS as 0.
2473 It is permissible for arguments to be null,
2474 or even garbage if their values do not matter. */
2476 tree
2477 build_nt VPARAMS ((enum tree_code code, ...))
2479 #ifndef ANSI_PROTOTYPES
2480 enum tree_code code;
2481 #endif
2482 va_list p;
2483 register tree t;
2484 register int length;
2485 register int i;
2487 VA_START (p, code);
2489 #ifndef ANSI_PROTOTYPES
2490 code = va_arg (p, enum tree_code);
2491 #endif
2493 t = make_node (code);
2494 length = TREE_CODE_LENGTH (code);
2496 for (i = 0; i < length; i++)
2497 TREE_OPERAND (t, i) = va_arg (p, tree);
2499 va_end (p);
2500 return t;
2503 /* Similar to `build_nt', except we build
2504 on the temp_decl_obstack, regardless. */
2506 tree
2507 build_parse_node VPARAMS ((enum tree_code code, ...))
2509 #ifndef ANSI_PROTOTYPES
2510 enum tree_code code;
2511 #endif
2512 va_list p;
2513 register tree t;
2514 register int length;
2515 register int i;
2517 VA_START (p, code);
2519 #ifndef ANSI_PROTOTYPES
2520 code = va_arg (p, enum tree_code);
2521 #endif
2523 t = make_node (code);
2524 length = TREE_CODE_LENGTH (code);
2526 for (i = 0; i < length; i++)
2527 TREE_OPERAND (t, i) = va_arg (p, tree);
2529 va_end (p);
2530 return t;
2533 #if 0
2534 /* Commented out because this wants to be done very
2535 differently. See cp-lex.c. */
2536 tree
2537 build_op_identifier (op1, op2)
2538 tree op1, op2;
2540 register tree t = make_node (OP_IDENTIFIER);
2541 TREE_PURPOSE (t) = op1;
2542 TREE_VALUE (t) = op2;
2543 return t;
2545 #endif
2547 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
2548 We do NOT enter this node in any sort of symbol table.
2550 layout_decl is used to set up the decl's storage layout.
2551 Other slots are initialized to 0 or null pointers. */
2553 tree
2554 build_decl (code, name, type)
2555 enum tree_code code;
2556 tree name, type;
2558 register tree t;
2560 t = make_node (code);
2562 /* if (type == error_mark_node)
2563 type = integer_type_node; */
2564 /* That is not done, deliberately, so that having error_mark_node
2565 as the type can suppress useless errors in the use of this variable. */
2567 DECL_NAME (t) = name;
2568 DECL_ASSEMBLER_NAME (t) = name;
2569 TREE_TYPE (t) = type;
2571 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
2572 layout_decl (t, 0);
2573 else if (code == FUNCTION_DECL)
2574 DECL_MODE (t) = FUNCTION_MODE;
2576 return t;
2579 /* BLOCK nodes are used to represent the structure of binding contours
2580 and declarations, once those contours have been exited and their contents
2581 compiled. This information is used for outputting debugging info. */
2583 tree
2584 build_block (vars, tags, subblocks, supercontext, chain)
2585 tree vars, tags ATTRIBUTE_UNUSED, subblocks, supercontext, chain;
2587 register tree block = make_node (BLOCK);
2589 BLOCK_VARS (block) = vars;
2590 BLOCK_SUBBLOCKS (block) = subblocks;
2591 BLOCK_SUPERCONTEXT (block) = supercontext;
2592 BLOCK_CHAIN (block) = chain;
2593 return block;
2596 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
2597 location where an expression or an identifier were encountered. It
2598 is necessary for languages where the frontend parser will handle
2599 recursively more than one file (Java is one of them). */
2601 tree
2602 build_expr_wfl (node, file, line, col)
2603 tree node;
2604 const char *file;
2605 int line, col;
2607 static const char *last_file = 0;
2608 static tree last_filenode = NULL_TREE;
2609 register tree wfl = make_node (EXPR_WITH_FILE_LOCATION);
2611 EXPR_WFL_NODE (wfl) = node;
2612 EXPR_WFL_SET_LINECOL (wfl, line, col);
2613 if (file != last_file)
2615 last_file = file;
2616 last_filenode = file ? get_identifier (file) : NULL_TREE;
2619 EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
2620 if (node)
2622 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
2623 TREE_TYPE (wfl) = TREE_TYPE (node);
2626 return wfl;
2629 /* Return a declaration like DDECL except that its DECL_MACHINE_ATTRIBUTE
2630 is ATTRIBUTE. */
2632 tree
2633 build_decl_attribute_variant (ddecl, attribute)
2634 tree ddecl, attribute;
2636 DECL_MACHINE_ATTRIBUTES (ddecl) = attribute;
2637 return ddecl;
2640 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
2641 is ATTRIBUTE.
2643 Record such modified types already made so we don't make duplicates. */
2645 tree
2646 build_type_attribute_variant (ttype, attribute)
2647 tree ttype, attribute;
2649 if ( ! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
2651 unsigned int hashcode;
2652 tree ntype;
2654 ntype = copy_node (ttype);
2656 TYPE_POINTER_TO (ntype) = 0;
2657 TYPE_REFERENCE_TO (ntype) = 0;
2658 TYPE_ATTRIBUTES (ntype) = attribute;
2660 /* Create a new main variant of TYPE. */
2661 TYPE_MAIN_VARIANT (ntype) = ntype;
2662 TYPE_NEXT_VARIANT (ntype) = 0;
2663 set_type_quals (ntype, TYPE_UNQUALIFIED);
2665 hashcode = (TYPE_HASH (TREE_CODE (ntype))
2666 + TYPE_HASH (TREE_TYPE (ntype))
2667 + attribute_hash_list (attribute));
2669 switch (TREE_CODE (ntype))
2671 case FUNCTION_TYPE:
2672 hashcode += TYPE_HASH (TYPE_ARG_TYPES (ntype));
2673 break;
2674 case ARRAY_TYPE:
2675 hashcode += TYPE_HASH (TYPE_DOMAIN (ntype));
2676 break;
2677 case INTEGER_TYPE:
2678 hashcode += TYPE_HASH (TYPE_MAX_VALUE (ntype));
2679 break;
2680 case REAL_TYPE:
2681 hashcode += TYPE_HASH (TYPE_PRECISION (ntype));
2682 break;
2683 default:
2684 break;
2687 ntype = type_hash_canon (hashcode, ntype);
2688 ttype = build_qualified_type (ntype, TYPE_QUALS (ttype));
2691 return ttype;
2694 /* Return a 1 if ATTR_NAME and ATTR_ARGS is valid for either declaration DECL
2695 or type TYPE and 0 otherwise. Validity is determined the configuration
2696 macros VALID_MACHINE_DECL_ATTRIBUTE and VALID_MACHINE_TYPE_ATTRIBUTE. */
2699 valid_machine_attribute (attr_name, attr_args, decl, type)
2700 tree attr_name;
2701 tree attr_args ATTRIBUTE_UNUSED;
2702 tree decl ATTRIBUTE_UNUSED;
2703 tree type ATTRIBUTE_UNUSED;
2705 int validated = 0;
2706 #ifdef VALID_MACHINE_DECL_ATTRIBUTE
2707 tree decl_attr_list = decl != 0 ? DECL_MACHINE_ATTRIBUTES (decl) : 0;
2708 #endif
2709 #ifdef VALID_MACHINE_TYPE_ATTRIBUTE
2710 tree type_attr_list = TYPE_ATTRIBUTES (type);
2711 #endif
2713 if (TREE_CODE (attr_name) != IDENTIFIER_NODE)
2714 abort ();
2716 #ifdef VALID_MACHINE_DECL_ATTRIBUTE
2717 if (decl != 0
2718 && VALID_MACHINE_DECL_ATTRIBUTE (decl, decl_attr_list, attr_name,
2719 attr_args))
2721 tree attr = lookup_attribute (IDENTIFIER_POINTER (attr_name),
2722 decl_attr_list);
2724 if (attr != NULL_TREE)
2726 /* Override existing arguments. Declarations are unique so we can
2727 modify this in place. */
2728 TREE_VALUE (attr) = attr_args;
2730 else
2732 decl_attr_list = tree_cons (attr_name, attr_args, decl_attr_list);
2733 decl = build_decl_attribute_variant (decl, decl_attr_list);
2736 validated = 1;
2738 #endif
2740 #ifdef VALID_MACHINE_TYPE_ATTRIBUTE
2741 if (validated)
2742 /* Don't apply the attribute to both the decl and the type. */
2744 else if (VALID_MACHINE_TYPE_ATTRIBUTE (type, type_attr_list, attr_name,
2745 attr_args))
2747 tree attr = lookup_attribute (IDENTIFIER_POINTER (attr_name),
2748 type_attr_list);
2750 if (attr != NULL_TREE)
2752 /* Override existing arguments.
2753 ??? This currently works since attribute arguments are not
2754 included in `attribute_hash_list'. Something more complicated
2755 may be needed in the future. */
2756 TREE_VALUE (attr) = attr_args;
2758 else
2760 /* If this is part of a declaration, create a type variant,
2761 otherwise, this is part of a type definition, so add it
2762 to the base type. */
2763 type_attr_list = tree_cons (attr_name, attr_args, type_attr_list);
2764 if (decl != 0)
2765 type = build_type_attribute_variant (type, type_attr_list);
2766 else
2767 TYPE_ATTRIBUTES (type) = type_attr_list;
2770 if (decl != 0)
2771 TREE_TYPE (decl) = type;
2773 validated = 1;
2776 /* Handle putting a type attribute on pointer-to-function-type by putting
2777 the attribute on the function type. */
2778 else if (POINTER_TYPE_P (type)
2779 && TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
2780 && VALID_MACHINE_TYPE_ATTRIBUTE (TREE_TYPE (type), type_attr_list,
2781 attr_name, attr_args))
2783 tree inner_type = TREE_TYPE (type);
2784 tree inner_attr_list = TYPE_ATTRIBUTES (inner_type);
2785 tree attr = lookup_attribute (IDENTIFIER_POINTER (attr_name),
2786 type_attr_list);
2788 if (attr != NULL_TREE)
2789 TREE_VALUE (attr) = attr_args;
2790 else
2792 inner_attr_list = tree_cons (attr_name, attr_args, inner_attr_list);
2793 inner_type = build_type_attribute_variant (inner_type,
2794 inner_attr_list);
2797 if (decl != 0)
2798 TREE_TYPE (decl) = build_pointer_type (inner_type);
2799 else
2801 /* Clear TYPE_POINTER_TO for the old inner type, since
2802 `type' won't be pointing to it anymore. */
2803 TYPE_POINTER_TO (TREE_TYPE (type)) = NULL_TREE;
2804 TREE_TYPE (type) = inner_type;
2807 validated = 1;
2809 #endif
2811 return validated;
2814 /* Return non-zero if IDENT is a valid name for attribute ATTR,
2815 or zero if not.
2817 We try both `text' and `__text__', ATTR may be either one. */
2818 /* ??? It might be a reasonable simplification to require ATTR to be only
2819 `text'. One might then also require attribute lists to be stored in
2820 their canonicalized form. */
2823 is_attribute_p (attr, ident)
2824 const char *attr;
2825 tree ident;
2827 int ident_len, attr_len;
2828 const char *p;
2830 if (TREE_CODE (ident) != IDENTIFIER_NODE)
2831 return 0;
2833 if (strcmp (attr, IDENTIFIER_POINTER (ident)) == 0)
2834 return 1;
2836 p = IDENTIFIER_POINTER (ident);
2837 ident_len = strlen (p);
2838 attr_len = strlen (attr);
2840 /* If ATTR is `__text__', IDENT must be `text'; and vice versa. */
2841 if (attr[0] == '_')
2843 if (attr[1] != '_'
2844 || attr[attr_len - 2] != '_'
2845 || attr[attr_len - 1] != '_')
2846 abort ();
2847 if (ident_len == attr_len - 4
2848 && strncmp (attr + 2, p, attr_len - 4) == 0)
2849 return 1;
2851 else
2853 if (ident_len == attr_len + 4
2854 && p[0] == '_' && p[1] == '_'
2855 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
2856 && strncmp (attr, p + 2, attr_len) == 0)
2857 return 1;
2860 return 0;
2863 /* Given an attribute name and a list of attributes, return a pointer to the
2864 attribute's list element if the attribute is part of the list, or NULL_TREE
2865 if not found. */
2867 tree
2868 lookup_attribute (attr_name, list)
2869 const char *attr_name;
2870 tree list;
2872 tree l;
2874 for (l = list; l; l = TREE_CHAIN (l))
2876 if (TREE_CODE (TREE_PURPOSE (l)) != IDENTIFIER_NODE)
2877 abort ();
2878 if (is_attribute_p (attr_name, TREE_PURPOSE (l)))
2879 return l;
2882 return NULL_TREE;
2885 /* Return an attribute list that is the union of a1 and a2. */
2887 tree
2888 merge_attributes (a1, a2)
2889 register tree a1, a2;
2891 tree attributes;
2893 /* Either one unset? Take the set one. */
2895 if ((attributes = a1) == 0)
2896 attributes = a2;
2898 /* One that completely contains the other? Take it. */
2900 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
2902 if (attribute_list_contained (a2, a1))
2903 attributes = a2;
2904 else
2906 /* Pick the longest list, and hang on the other list. */
2907 /* ??? For the moment we punt on the issue of attrs with args. */
2909 if (list_length (a1) < list_length (a2))
2910 attributes = a2, a2 = a1;
2912 for (; a2 != 0; a2 = TREE_CHAIN (a2))
2913 if (lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
2914 attributes) == NULL_TREE)
2916 a1 = copy_node (a2);
2917 TREE_CHAIN (a1) = attributes;
2918 attributes = a1;
2922 return attributes;
2925 /* Given types T1 and T2, merge their attributes and return
2926 the result. */
2928 tree
2929 merge_machine_type_attributes (t1, t2)
2930 tree t1, t2;
2932 #ifdef MERGE_MACHINE_TYPE_ATTRIBUTES
2933 return MERGE_MACHINE_TYPE_ATTRIBUTES (t1, t2);
2934 #else
2935 return merge_attributes (TYPE_ATTRIBUTES (t1),
2936 TYPE_ATTRIBUTES (t2));
2937 #endif
2940 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
2941 the result. */
2943 tree
2944 merge_machine_decl_attributes (olddecl, newdecl)
2945 tree olddecl, newdecl;
2947 #ifdef MERGE_MACHINE_DECL_ATTRIBUTES
2948 return MERGE_MACHINE_DECL_ATTRIBUTES (olddecl, newdecl);
2949 #else
2950 return merge_attributes (DECL_MACHINE_ATTRIBUTES (olddecl),
2951 DECL_MACHINE_ATTRIBUTES (newdecl));
2952 #endif
2955 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
2956 of the various TYPE_QUAL values. */
2958 static void
2959 set_type_quals (type, type_quals)
2960 tree type;
2961 int type_quals;
2963 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
2964 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
2965 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
2968 /* Given a type node TYPE and a TYPE_QUALIFIER_SET, return a type for
2969 the same kind of data as TYPE describes. Variants point to the
2970 "main variant" (which has no qualifiers set) via TYPE_MAIN_VARIANT,
2971 and it points to a chain of other variants so that duplicate
2972 variants are never made. Only main variants should ever appear as
2973 types of expressions. */
2975 tree
2976 build_qualified_type (type, type_quals)
2977 tree type;
2978 int type_quals;
2980 register tree t;
2982 /* Search the chain of variants to see if there is already one there just
2983 like the one we need to have. If so, use that existing one. We must
2984 preserve the TYPE_NAME, since there is code that depends on this. */
2986 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2987 if (TYPE_QUALS (t) == type_quals && TYPE_NAME (t) == TYPE_NAME (type))
2988 return t;
2990 /* We need a new one. */
2991 t = build_type_copy (type);
2992 set_type_quals (t, type_quals);
2993 return t;
2996 /* Create a new variant of TYPE, equivalent but distinct.
2997 This is so the caller can modify it. */
2999 tree
3000 build_type_copy (type)
3001 tree type;
3003 register tree t, m = TYPE_MAIN_VARIANT (type);
3005 t = copy_node (type);
3007 TYPE_POINTER_TO (t) = 0;
3008 TYPE_REFERENCE_TO (t) = 0;
3010 /* Add this type to the chain of variants of TYPE. */
3011 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
3012 TYPE_NEXT_VARIANT (m) = t;
3014 return t;
3017 /* Hashing of types so that we don't make duplicates.
3018 The entry point is `type_hash_canon'. */
3020 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
3021 with types in the TREE_VALUE slots), by adding the hash codes
3022 of the individual types. */
3024 unsigned int
3025 type_hash_list (list)
3026 tree list;
3028 unsigned int hashcode;
3029 register tree tail;
3031 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
3032 hashcode += TYPE_HASH (TREE_VALUE (tail));
3034 return hashcode;
3037 /* These are the Hashtable callback functions. */
3039 /* Returns true if the types are equal. */
3041 static int
3042 type_hash_eq (va, vb)
3043 const void *va;
3044 const void *vb;
3046 const struct type_hash *a = va, *b = vb;
3047 if (a->hash == b->hash
3048 && TREE_CODE (a->type) == TREE_CODE (b->type)
3049 && TREE_TYPE (a->type) == TREE_TYPE (b->type)
3050 && attribute_list_equal (TYPE_ATTRIBUTES (a->type),
3051 TYPE_ATTRIBUTES (b->type))
3052 && TYPE_ALIGN (a->type) == TYPE_ALIGN (b->type)
3053 && (TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
3054 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
3055 TYPE_MAX_VALUE (b->type)))
3056 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
3057 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
3058 TYPE_MIN_VALUE (b->type)))
3059 /* Note that TYPE_DOMAIN is TYPE_ARG_TYPES for FUNCTION_TYPE. */
3060 && (TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type)
3061 || (TYPE_DOMAIN (a->type)
3062 && TREE_CODE (TYPE_DOMAIN (a->type)) == TREE_LIST
3063 && TYPE_DOMAIN (b->type)
3064 && TREE_CODE (TYPE_DOMAIN (b->type)) == TREE_LIST
3065 && type_list_equal (TYPE_DOMAIN (a->type),
3066 TYPE_DOMAIN (b->type)))))
3067 return 1;
3068 return 0;
3071 /* Return the cached hash value. */
3073 static unsigned int
3074 type_hash_hash (item)
3075 const void *item;
3077 return ((const struct type_hash *) item)->hash;
3080 /* Look in the type hash table for a type isomorphic to TYPE.
3081 If one is found, return it. Otherwise return 0. */
3083 tree
3084 type_hash_lookup (hashcode, type)
3085 unsigned int hashcode;
3086 tree type;
3088 struct type_hash *h, in;
3090 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
3091 must call that routine before comparing TYPE_ALIGNs. */
3092 layout_type (type);
3094 in.hash = hashcode;
3095 in.type = type;
3097 h = htab_find_with_hash (type_hash_table, &in, hashcode);
3098 if (h)
3099 return h->type;
3100 return NULL_TREE;
3103 /* Add an entry to the type-hash-table
3104 for a type TYPE whose hash code is HASHCODE. */
3106 void
3107 type_hash_add (hashcode, type)
3108 unsigned int hashcode;
3109 tree type;
3111 struct type_hash *h;
3112 void **loc;
3114 h = (struct type_hash *) permalloc (sizeof (struct type_hash));
3115 h->hash = hashcode;
3116 h->type = type;
3117 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
3118 *(struct type_hash **) loc = h;
3121 /* Given TYPE, and HASHCODE its hash code, return the canonical
3122 object for an identical type if one already exists.
3123 Otherwise, return TYPE, and record it as the canonical object
3124 if it is a permanent object.
3126 To use this function, first create a type of the sort you want.
3127 Then compute its hash code from the fields of the type that
3128 make it different from other similar types.
3129 Then call this function and use the value.
3130 This function frees the type you pass in if it is a duplicate. */
3132 /* Set to 1 to debug without canonicalization. Never set by program. */
3133 int debug_no_type_hash = 0;
3135 tree
3136 type_hash_canon (hashcode, type)
3137 unsigned int hashcode;
3138 tree type;
3140 tree t1;
3142 if (debug_no_type_hash)
3143 return type;
3145 t1 = type_hash_lookup (hashcode, type);
3146 if (t1 != 0)
3148 #ifdef GATHER_STATISTICS
3149 tree_node_counts[(int) t_kind]--;
3150 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
3151 #endif
3152 return t1;
3155 /* If this is a permanent type, record it for later reuse. */
3156 type_hash_add (hashcode, type);
3158 return type;
3161 /* Callback function for htab_traverse. */
3163 static int
3164 mark_hash_entry (entry, param)
3165 void **entry;
3166 void *param ATTRIBUTE_UNUSED;
3168 struct type_hash *p = *(struct type_hash **) entry;
3170 ggc_mark_tree (p->type);
3172 /* Continue scan. */
3173 return 1;
3176 /* Mark ARG (which is really a htab_t *) for GC. */
3178 static void
3179 mark_type_hash (arg)
3180 void *arg;
3182 htab_t t = *(htab_t *) arg;
3184 htab_traverse (t, mark_hash_entry, 0);
3187 static void
3188 print_type_hash_statistics ()
3190 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
3191 (long) htab_size (type_hash_table),
3192 (long) htab_elements (type_hash_table),
3193 htab_collisions (type_hash_table));
3196 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
3197 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
3198 by adding the hash codes of the individual attributes. */
3200 unsigned int
3201 attribute_hash_list (list)
3202 tree list;
3204 unsigned int hashcode;
3205 register tree tail;
3207 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
3208 /* ??? Do we want to add in TREE_VALUE too? */
3209 hashcode += TYPE_HASH (TREE_PURPOSE (tail));
3210 return hashcode;
3213 /* Given two lists of attributes, return true if list l2 is
3214 equivalent to l1. */
3217 attribute_list_equal (l1, l2)
3218 tree l1, l2;
3220 return attribute_list_contained (l1, l2)
3221 && attribute_list_contained (l2, l1);
3224 /* Given two lists of attributes, return true if list L2 is
3225 completely contained within L1. */
3226 /* ??? This would be faster if attribute names were stored in a canonicalized
3227 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
3228 must be used to show these elements are equivalent (which they are). */
3229 /* ??? It's not clear that attributes with arguments will always be handled
3230 correctly. */
3233 attribute_list_contained (l1, l2)
3234 tree l1, l2;
3236 register tree t1, t2;
3238 /* First check the obvious, maybe the lists are identical. */
3239 if (l1 == l2)
3240 return 1;
3242 /* Maybe the lists are similar. */
3243 for (t1 = l1, t2 = l2;
3244 t1 != 0 && t2 != 0
3245 && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
3246 && TREE_VALUE (t1) == TREE_VALUE (t2);
3247 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
3249 /* Maybe the lists are equal. */
3250 if (t1 == 0 && t2 == 0)
3251 return 1;
3253 for (; t2 != 0; t2 = TREE_CHAIN (t2))
3255 tree attr
3256 = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)), l1);
3258 if (attr == 0)
3259 return 0;
3261 if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) != 1)
3262 return 0;
3265 return 1;
3268 /* Given two lists of types
3269 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
3270 return 1 if the lists contain the same types in the same order.
3271 Also, the TREE_PURPOSEs must match. */
3274 type_list_equal (l1, l2)
3275 tree l1, l2;
3277 register tree t1, t2;
3279 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
3280 if (TREE_VALUE (t1) != TREE_VALUE (t2)
3281 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
3282 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
3283 && (TREE_TYPE (TREE_PURPOSE (t1))
3284 == TREE_TYPE (TREE_PURPOSE (t2))))))
3285 return 0;
3287 return t1 == t2;
3290 /* Nonzero if integer constants T1 and T2
3291 represent the same constant value. */
3294 tree_int_cst_equal (t1, t2)
3295 tree t1, t2;
3297 if (t1 == t2)
3298 return 1;
3300 if (t1 == 0 || t2 == 0)
3301 return 0;
3303 if (TREE_CODE (t1) == INTEGER_CST
3304 && TREE_CODE (t2) == INTEGER_CST
3305 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3306 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
3307 return 1;
3309 return 0;
3312 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
3313 The precise way of comparison depends on their data type. */
3316 tree_int_cst_lt (t1, t2)
3317 tree t1, t2;
3319 if (t1 == t2)
3320 return 0;
3322 if (! TREE_UNSIGNED (TREE_TYPE (t1)))
3323 return INT_CST_LT (t1, t2);
3325 return INT_CST_LT_UNSIGNED (t1, t2);
3328 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
3331 tree_int_cst_compare (t1, t2)
3332 tree t1;
3333 tree t2;
3335 if (tree_int_cst_lt (t1, t2))
3336 return -1;
3337 else if (tree_int_cst_lt (t2, t1))
3338 return 1;
3339 else
3340 return 0;
3343 /* Return 1 if T is an INTEGER_CST that can be represented in a single
3344 HOST_WIDE_INT value. If POS is nonzero, the result must be positive. */
3347 host_integerp (t, pos)
3348 tree t;
3349 int pos;
3351 return (TREE_CODE (t) == INTEGER_CST
3352 && ! TREE_OVERFLOW (t)
3353 && ((TREE_INT_CST_HIGH (t) == 0
3354 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
3355 || (! pos && TREE_INT_CST_HIGH (t) == -1
3356 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
3357 || (! pos && TREE_INT_CST_HIGH (t) == 0
3358 && TREE_UNSIGNED (TREE_TYPE (t)))));
3361 /* Return the HOST_WIDE_INT least significant bits of T if it is an
3362 INTEGER_CST and there is no overflow. POS is nonzero if the result must
3363 be positive. Abort if we cannot satisfy the above conditions. */
3365 HOST_WIDE_INT
3366 tree_low_cst (t, pos)
3367 tree t;
3368 int pos;
3370 if (host_integerp (t, pos))
3371 return TREE_INT_CST_LOW (t);
3372 else
3373 abort ();
3376 /* Return the most significant bit of the integer constant T. */
3379 tree_int_cst_msb (t)
3380 tree t;
3382 register int prec;
3383 HOST_WIDE_INT h;
3384 unsigned HOST_WIDE_INT l;
3386 /* Note that using TYPE_PRECISION here is wrong. We care about the
3387 actual bits, not the (arbitrary) range of the type. */
3388 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
3389 rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
3390 2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
3391 return (l & 1) == 1;
3394 /* Return an indication of the sign of the integer constant T.
3395 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
3396 Note that -1 will never be returned it T's type is unsigned. */
3399 tree_int_cst_sgn (t)
3400 tree t;
3402 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
3403 return 0;
3404 else if (TREE_UNSIGNED (TREE_TYPE (t)))
3405 return 1;
3406 else if (TREE_INT_CST_HIGH (t) < 0)
3407 return -1;
3408 else
3409 return 1;
3412 /* Compare two constructor-element-type constants. Return 1 if the lists
3413 are known to be equal; otherwise return 0. */
3416 simple_cst_list_equal (l1, l2)
3417 tree l1, l2;
3419 while (l1 != NULL_TREE && l2 != NULL_TREE)
3421 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
3422 return 0;
3424 l1 = TREE_CHAIN (l1);
3425 l2 = TREE_CHAIN (l2);
3428 return l1 == l2;
3431 /* Return truthvalue of whether T1 is the same tree structure as T2.
3432 Return 1 if they are the same.
3433 Return 0 if they are understandably different.
3434 Return -1 if either contains tree structure not understood by
3435 this function. */
3438 simple_cst_equal (t1, t2)
3439 tree t1, t2;
3441 register enum tree_code code1, code2;
3442 int cmp;
3443 int i;
3445 if (t1 == t2)
3446 return 1;
3447 if (t1 == 0 || t2 == 0)
3448 return 0;
3450 code1 = TREE_CODE (t1);
3451 code2 = TREE_CODE (t2);
3453 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
3455 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3456 || code2 == NON_LVALUE_EXPR)
3457 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3458 else
3459 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
3462 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3463 || code2 == NON_LVALUE_EXPR)
3464 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
3466 if (code1 != code2)
3467 return 0;
3469 switch (code1)
3471 case INTEGER_CST:
3472 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3473 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
3475 case REAL_CST:
3476 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3478 case STRING_CST:
3479 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3480 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3481 TREE_STRING_LENGTH (t1)));
3483 case CONSTRUCTOR:
3484 if (CONSTRUCTOR_ELTS (t1) == CONSTRUCTOR_ELTS (t2))
3485 return 1;
3486 else
3487 abort ();
3489 case SAVE_EXPR:
3490 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3492 case CALL_EXPR:
3493 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3494 if (cmp <= 0)
3495 return cmp;
3496 return
3497 simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3499 case TARGET_EXPR:
3500 /* Special case: if either target is an unallocated VAR_DECL,
3501 it means that it's going to be unified with whatever the
3502 TARGET_EXPR is really supposed to initialize, so treat it
3503 as being equivalent to anything. */
3504 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
3505 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
3506 && DECL_RTL (TREE_OPERAND (t1, 0)) == 0)
3507 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
3508 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
3509 && DECL_RTL (TREE_OPERAND (t2, 0)) == 0))
3510 cmp = 1;
3511 else
3512 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3514 if (cmp <= 0)
3515 return cmp;
3517 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3519 case WITH_CLEANUP_EXPR:
3520 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3521 if (cmp <= 0)
3522 return cmp;
3524 return simple_cst_equal (TREE_OPERAND (t1, 2), TREE_OPERAND (t1, 2));
3526 case COMPONENT_REF:
3527 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
3528 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3530 return 0;
3532 case VAR_DECL:
3533 case PARM_DECL:
3534 case CONST_DECL:
3535 case FUNCTION_DECL:
3536 return 0;
3538 default:
3539 break;
3542 /* This general rule works for most tree codes. All exceptions should be
3543 handled above. If this is a language-specific tree code, we can't
3544 trust what might be in the operand, so say we don't know
3545 the situation. */
3546 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
3547 return -1;
3549 switch (TREE_CODE_CLASS (code1))
3551 case '1':
3552 case '2':
3553 case '<':
3554 case 'e':
3555 case 'r':
3556 case 's':
3557 cmp = 1;
3558 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
3560 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
3561 if (cmp <= 0)
3562 return cmp;
3565 return cmp;
3567 default:
3568 return -1;
3572 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
3573 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
3574 than U, respectively. */
3577 compare_tree_int (t, u)
3578 tree t;
3579 unsigned int u;
3581 if (tree_int_cst_sgn (t) < 0)
3582 return -1;
3583 else if (TREE_INT_CST_HIGH (t) != 0)
3584 return 1;
3585 else if (TREE_INT_CST_LOW (t) == u)
3586 return 0;
3587 else if (TREE_INT_CST_LOW (t) < u)
3588 return -1;
3589 else
3590 return 1;
3593 /* Constructors for pointer, array and function types.
3594 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
3595 constructed by language-dependent code, not here.) */
3597 /* Construct, lay out and return the type of pointers to TO_TYPE.
3598 If such a type has already been constructed, reuse it. */
3600 tree
3601 build_pointer_type (to_type)
3602 tree to_type;
3604 register tree t = TYPE_POINTER_TO (to_type);
3606 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3608 if (t != 0)
3609 return t;
3611 /* We need a new one. */
3612 t = make_node (POINTER_TYPE);
3614 TREE_TYPE (t) = to_type;
3616 /* Record this type as the pointer to TO_TYPE. */
3617 TYPE_POINTER_TO (to_type) = t;
3619 /* Lay out the type. This function has many callers that are concerned
3620 with expression-construction, and this simplifies them all.
3621 Also, it guarantees the TYPE_SIZE is in the same obstack as the type. */
3622 layout_type (t);
3624 return t;
3627 /* Build the node for the type of references-to-TO_TYPE. */
3629 tree
3630 build_reference_type (to_type)
3631 tree to_type;
3633 register tree t = TYPE_REFERENCE_TO (to_type);
3635 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3637 if (t)
3638 return t;
3640 /* We need a new one. */
3641 t = make_node (REFERENCE_TYPE);
3643 TREE_TYPE (t) = to_type;
3645 /* Record this type as the pointer to TO_TYPE. */
3646 TYPE_REFERENCE_TO (to_type) = t;
3648 layout_type (t);
3650 return t;
3653 /* Build a type that is compatible with t but has no cv quals anywhere
3654 in its type, thus
3656 const char *const *const * -> char ***. */
3658 tree
3659 build_type_no_quals (t)
3660 tree t;
3662 switch (TREE_CODE (t))
3664 case POINTER_TYPE:
3665 return build_pointer_type (build_type_no_quals (TREE_TYPE (t)));
3666 case REFERENCE_TYPE:
3667 return build_reference_type (build_type_no_quals (TREE_TYPE (t)));
3668 default:
3669 return TYPE_MAIN_VARIANT (t);
3673 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
3674 MAXVAL should be the maximum value in the domain
3675 (one less than the length of the array).
3677 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
3678 We don't enforce this limit, that is up to caller (e.g. language front end).
3679 The limit exists because the result is a signed type and we don't handle
3680 sizes that use more than one HOST_WIDE_INT. */
3682 tree
3683 build_index_type (maxval)
3684 tree maxval;
3686 register tree itype = make_node (INTEGER_TYPE);
3688 TREE_TYPE (itype) = sizetype;
3689 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
3690 TYPE_MIN_VALUE (itype) = size_zero_node;
3691 TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
3692 TYPE_MODE (itype) = TYPE_MODE (sizetype);
3693 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
3694 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
3695 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
3696 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
3698 if (host_integerp (maxval, 1))
3699 return type_hash_canon (tree_low_cst (maxval, 1), itype);
3700 else
3701 return itype;
3704 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
3705 ENUMERAL_TYPE, BOOLEAN_TYPE, or CHAR_TYPE), with
3706 low bound LOWVAL and high bound HIGHVAL.
3707 if TYPE==NULL_TREE, sizetype is used. */
3709 tree
3710 build_range_type (type, lowval, highval)
3711 tree type, lowval, highval;
3713 register tree itype = make_node (INTEGER_TYPE);
3715 TREE_TYPE (itype) = type;
3716 if (type == NULL_TREE)
3717 type = sizetype;
3719 TYPE_MIN_VALUE (itype) = convert (type, lowval);
3720 TYPE_MAX_VALUE (itype) = highval ? convert (type, highval) : NULL;
3722 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
3723 TYPE_MODE (itype) = TYPE_MODE (type);
3724 TYPE_SIZE (itype) = TYPE_SIZE (type);
3725 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
3726 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
3727 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
3729 if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
3730 return type_hash_canon (tree_low_cst (highval, 0)
3731 - tree_low_cst (lowval, 0),
3732 itype);
3733 else
3734 return itype;
3737 /* Just like build_index_type, but takes lowval and highval instead
3738 of just highval (maxval). */
3740 tree
3741 build_index_2_type (lowval,highval)
3742 tree lowval, highval;
3744 return build_range_type (sizetype, lowval, highval);
3747 /* Return nonzero iff ITYPE1 and ITYPE2 are equal (in the LISP sense).
3748 Needed because when index types are not hashed, equal index types
3749 built at different times appear distinct, even though structurally,
3750 they are not. */
3753 index_type_equal (itype1, itype2)
3754 tree itype1, itype2;
3756 if (TREE_CODE (itype1) != TREE_CODE (itype2))
3757 return 0;
3759 if (TREE_CODE (itype1) == INTEGER_TYPE)
3761 if (TYPE_PRECISION (itype1) != TYPE_PRECISION (itype2)
3762 || TYPE_MODE (itype1) != TYPE_MODE (itype2)
3763 || simple_cst_equal (TYPE_SIZE (itype1), TYPE_SIZE (itype2)) != 1
3764 || TYPE_ALIGN (itype1) != TYPE_ALIGN (itype2))
3765 return 0;
3767 if (1 == simple_cst_equal (TYPE_MIN_VALUE (itype1),
3768 TYPE_MIN_VALUE (itype2))
3769 && 1 == simple_cst_equal (TYPE_MAX_VALUE (itype1),
3770 TYPE_MAX_VALUE (itype2)))
3771 return 1;
3774 return 0;
3777 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
3778 and number of elements specified by the range of values of INDEX_TYPE.
3779 If such a type has already been constructed, reuse it. */
3781 tree
3782 build_array_type (elt_type, index_type)
3783 tree elt_type, index_type;
3785 register tree t;
3786 unsigned int hashcode;
3788 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
3790 error ("arrays of functions are not meaningful");
3791 elt_type = integer_type_node;
3794 /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
3795 build_pointer_type (elt_type);
3797 /* Allocate the array after the pointer type,
3798 in case we free it in type_hash_canon. */
3799 t = make_node (ARRAY_TYPE);
3800 TREE_TYPE (t) = elt_type;
3801 TYPE_DOMAIN (t) = index_type;
3803 if (index_type == 0)
3805 return t;
3808 hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
3809 t = type_hash_canon (hashcode, t);
3811 if (!COMPLETE_TYPE_P (t))
3812 layout_type (t);
3813 return t;
3816 /* Return the TYPE of the elements comprising
3817 the innermost dimension of ARRAY. */
3819 tree
3820 get_inner_array_type (array)
3821 tree array;
3823 tree type = TREE_TYPE (array);
3825 while (TREE_CODE (type) == ARRAY_TYPE)
3826 type = TREE_TYPE (type);
3828 return type;
3831 /* Construct, lay out and return
3832 the type of functions returning type VALUE_TYPE
3833 given arguments of types ARG_TYPES.
3834 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
3835 are data type nodes for the arguments of the function.
3836 If such a type has already been constructed, reuse it. */
3838 tree
3839 build_function_type (value_type, arg_types)
3840 tree value_type, arg_types;
3842 register tree t;
3843 unsigned int hashcode;
3845 if (TREE_CODE (value_type) == FUNCTION_TYPE)
3847 error ("function return type cannot be function");
3848 value_type = integer_type_node;
3851 /* Make a node of the sort we want. */
3852 t = make_node (FUNCTION_TYPE);
3853 TREE_TYPE (t) = value_type;
3854 TYPE_ARG_TYPES (t) = arg_types;
3856 /* If we already have such a type, use the old one and free this one. */
3857 hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
3858 t = type_hash_canon (hashcode, t);
3860 if (!COMPLETE_TYPE_P (t))
3861 layout_type (t);
3862 return t;
3865 /* Construct, lay out and return the type of methods belonging to class
3866 BASETYPE and whose arguments and values are described by TYPE.
3867 If that type exists already, reuse it.
3868 TYPE must be a FUNCTION_TYPE node. */
3870 tree
3871 build_method_type (basetype, type)
3872 tree basetype, type;
3874 register tree t;
3875 unsigned int hashcode;
3877 /* Make a node of the sort we want. */
3878 t = make_node (METHOD_TYPE);
3880 if (TREE_CODE (type) != FUNCTION_TYPE)
3881 abort ();
3883 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3884 TREE_TYPE (t) = TREE_TYPE (type);
3886 /* The actual arglist for this function includes a "hidden" argument
3887 which is "this". Put it into the list of argument types. */
3889 TYPE_ARG_TYPES (t)
3890 = tree_cons (NULL_TREE,
3891 build_pointer_type (basetype), TYPE_ARG_TYPES (type));
3893 /* If we already have such a type, use the old one and free this one. */
3894 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
3895 t = type_hash_canon (hashcode, t);
3897 if (!COMPLETE_TYPE_P (t))
3898 layout_type (t);
3900 return t;
3903 /* Construct, lay out and return the type of offsets to a value
3904 of type TYPE, within an object of type BASETYPE.
3905 If a suitable offset type exists already, reuse it. */
3907 tree
3908 build_offset_type (basetype, type)
3909 tree basetype, type;
3911 register tree t;
3912 unsigned int hashcode;
3914 /* Make a node of the sort we want. */
3915 t = make_node (OFFSET_TYPE);
3917 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3918 TREE_TYPE (t) = type;
3920 /* If we already have such a type, use the old one and free this one. */
3921 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
3922 t = type_hash_canon (hashcode, t);
3924 if (!COMPLETE_TYPE_P (t))
3925 layout_type (t);
3927 return t;
3930 /* Create a complex type whose components are COMPONENT_TYPE. */
3932 tree
3933 build_complex_type (component_type)
3934 tree component_type;
3936 register tree t;
3937 unsigned int hashcode;
3939 /* Make a node of the sort we want. */
3940 t = make_node (COMPLEX_TYPE);
3942 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
3943 set_type_quals (t, TYPE_QUALS (component_type));
3945 /* If we already have such a type, use the old one and free this one. */
3946 hashcode = TYPE_HASH (component_type);
3947 t = type_hash_canon (hashcode, t);
3949 if (!COMPLETE_TYPE_P (t))
3950 layout_type (t);
3952 /* If we are writing Dwarf2 output we need to create a name,
3953 since complex is a fundamental type. */
3954 if (write_symbols == DWARF2_DEBUG && ! TYPE_NAME (t))
3956 const char *name;
3957 if (component_type == char_type_node)
3958 name = "complex char";
3959 else if (component_type == signed_char_type_node)
3960 name = "complex signed char";
3961 else if (component_type == unsigned_char_type_node)
3962 name = "complex unsigned char";
3963 else if (component_type == short_integer_type_node)
3964 name = "complex short int";
3965 else if (component_type == short_unsigned_type_node)
3966 name = "complex short unsigned int";
3967 else if (component_type == integer_type_node)
3968 name = "complex int";
3969 else if (component_type == unsigned_type_node)
3970 name = "complex unsigned int";
3971 else if (component_type == long_integer_type_node)
3972 name = "complex long int";
3973 else if (component_type == long_unsigned_type_node)
3974 name = "complex long unsigned int";
3975 else if (component_type == long_long_integer_type_node)
3976 name = "complex long long int";
3977 else if (component_type == long_long_unsigned_type_node)
3978 name = "complex long long unsigned int";
3979 else
3980 name = 0;
3982 if (name != 0)
3983 TYPE_NAME (t) = get_identifier (name);
3986 return t;
3989 /* Return OP, stripped of any conversions to wider types as much as is safe.
3990 Converting the value back to OP's type makes a value equivalent to OP.
3992 If FOR_TYPE is nonzero, we return a value which, if converted to
3993 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
3995 If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
3996 narrowest type that can hold the value, even if they don't exactly fit.
3997 Otherwise, bit-field references are changed to a narrower type
3998 only if they can be fetched directly from memory in that type.
4000 OP must have integer, real or enumeral type. Pointers are not allowed!
4002 There are some cases where the obvious value we could return
4003 would regenerate to OP if converted to OP's type,
4004 but would not extend like OP to wider types.
4005 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
4006 For example, if OP is (unsigned short)(signed char)-1,
4007 we avoid returning (signed char)-1 if FOR_TYPE is int,
4008 even though extending that to an unsigned short would regenerate OP,
4009 since the result of extending (signed char)-1 to (int)
4010 is different from (int) OP. */
4012 tree
4013 get_unwidened (op, for_type)
4014 register tree op;
4015 tree for_type;
4017 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
4018 register tree type = TREE_TYPE (op);
4019 register unsigned final_prec
4020 = TYPE_PRECISION (for_type != 0 ? for_type : type);
4021 register int uns
4022 = (for_type != 0 && for_type != type
4023 && final_prec > TYPE_PRECISION (type)
4024 && TREE_UNSIGNED (type));
4025 register tree win = op;
4027 while (TREE_CODE (op) == NOP_EXPR)
4029 register int bitschange
4030 = TYPE_PRECISION (TREE_TYPE (op))
4031 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
4033 /* Truncations are many-one so cannot be removed.
4034 Unless we are later going to truncate down even farther. */
4035 if (bitschange < 0
4036 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
4037 break;
4039 /* See what's inside this conversion. If we decide to strip it,
4040 we will set WIN. */
4041 op = TREE_OPERAND (op, 0);
4043 /* If we have not stripped any zero-extensions (uns is 0),
4044 we can strip any kind of extension.
4045 If we have previously stripped a zero-extension,
4046 only zero-extensions can safely be stripped.
4047 Any extension can be stripped if the bits it would produce
4048 are all going to be discarded later by truncating to FOR_TYPE. */
4050 if (bitschange > 0)
4052 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
4053 win = op;
4054 /* TREE_UNSIGNED says whether this is a zero-extension.
4055 Let's avoid computing it if it does not affect WIN
4056 and if UNS will not be needed again. */
4057 if ((uns || TREE_CODE (op) == NOP_EXPR)
4058 && TREE_UNSIGNED (TREE_TYPE (op)))
4060 uns = 1;
4061 win = op;
4066 if (TREE_CODE (op) == COMPONENT_REF
4067 /* Since type_for_size always gives an integer type. */
4068 && TREE_CODE (type) != REAL_TYPE
4069 /* Don't crash if field not laid out yet. */
4070 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
4071 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
4073 unsigned int innerprec
4074 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4076 type = type_for_size (innerprec, TREE_UNSIGNED (TREE_OPERAND (op, 1)));
4078 /* We can get this structure field in the narrowest type it fits in.
4079 If FOR_TYPE is 0, do this only for a field that matches the
4080 narrower type exactly and is aligned for it
4081 The resulting extension to its nominal type (a fullword type)
4082 must fit the same conditions as for other extensions. */
4084 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
4085 && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
4086 && (! uns || final_prec <= innerprec
4087 || TREE_UNSIGNED (TREE_OPERAND (op, 1)))
4088 && type != 0)
4090 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4091 TREE_OPERAND (op, 1));
4092 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4093 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4097 return win;
4100 /* Return OP or a simpler expression for a narrower value
4101 which can be sign-extended or zero-extended to give back OP.
4102 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
4103 or 0 if the value should be sign-extended. */
4105 tree
4106 get_narrower (op, unsignedp_ptr)
4107 register tree op;
4108 int *unsignedp_ptr;
4110 register int uns = 0;
4111 int first = 1;
4112 register tree win = op;
4114 while (TREE_CODE (op) == NOP_EXPR)
4116 register int bitschange
4117 = (TYPE_PRECISION (TREE_TYPE (op))
4118 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
4120 /* Truncations are many-one so cannot be removed. */
4121 if (bitschange < 0)
4122 break;
4124 /* See what's inside this conversion. If we decide to strip it,
4125 we will set WIN. */
4126 op = TREE_OPERAND (op, 0);
4128 if (bitschange > 0)
4130 /* An extension: the outermost one can be stripped,
4131 but remember whether it is zero or sign extension. */
4132 if (first)
4133 uns = TREE_UNSIGNED (TREE_TYPE (op));
4134 /* Otherwise, if a sign extension has been stripped,
4135 only sign extensions can now be stripped;
4136 if a zero extension has been stripped, only zero-extensions. */
4137 else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
4138 break;
4139 first = 0;
4141 else /* bitschange == 0 */
4143 /* A change in nominal type can always be stripped, but we must
4144 preserve the unsignedness. */
4145 if (first)
4146 uns = TREE_UNSIGNED (TREE_TYPE (op));
4147 first = 0;
4150 win = op;
4153 if (TREE_CODE (op) == COMPONENT_REF
4154 /* Since type_for_size always gives an integer type. */
4155 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
4156 /* Ensure field is laid out already. */
4157 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0)
4159 unsigned HOST_WIDE_INT innerprec
4160 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4161 tree type = type_for_size (innerprec, TREE_UNSIGNED (op));
4163 /* We can get this structure field in a narrower type that fits it,
4164 but the resulting extension to its nominal type (a fullword type)
4165 must satisfy the same conditions as for other extensions.
4167 Do this only for fields that are aligned (not bit-fields),
4168 because when bit-field insns will be used there is no
4169 advantage in doing this. */
4171 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
4172 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
4173 && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
4174 && type != 0)
4176 if (first)
4177 uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
4178 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4179 TREE_OPERAND (op, 1));
4180 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4181 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4184 *unsignedp_ptr = uns;
4185 return win;
4188 /* Nonzero if integer constant C has a value that is permissible
4189 for type TYPE (an INTEGER_TYPE). */
4192 int_fits_type_p (c, type)
4193 tree c, type;
4195 /* If the bounds of the type are integers, we can check ourselves.
4196 Otherwise,. use force_fit_type, which checks against the precision. */
4197 if (TYPE_MAX_VALUE (type) != NULL_TREE
4198 && TYPE_MIN_VALUE (type) != NULL_TREE
4199 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST
4200 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
4202 if (TREE_UNSIGNED (type))
4203 return (! INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c)
4204 && ! INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type))
4205 /* Negative ints never fit unsigned types. */
4206 && ! (TREE_INT_CST_HIGH (c) < 0
4207 && ! TREE_UNSIGNED (TREE_TYPE (c))));
4208 else
4209 return (! INT_CST_LT (TYPE_MAX_VALUE (type), c)
4210 && ! INT_CST_LT (c, TYPE_MIN_VALUE (type))
4211 /* Unsigned ints with top bit set never fit signed types. */
4212 && ! (TREE_INT_CST_HIGH (c) < 0
4213 && TREE_UNSIGNED (TREE_TYPE (c))));
4215 else
4217 c = copy_node (c);
4218 TREE_TYPE (c) = type;
4219 return !force_fit_type (c, 0);
4223 /* Given a DECL or TYPE, return the scope in which it was declared, or
4224 NULL_TREE if there is no containing scope. */
4226 tree
4227 get_containing_scope (t)
4228 tree t;
4230 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
4233 /* Return the innermost context enclosing DECL that is
4234 a FUNCTION_DECL, or zero if none. */
4236 tree
4237 decl_function_context (decl)
4238 tree decl;
4240 tree context;
4242 if (TREE_CODE (decl) == ERROR_MARK)
4243 return 0;
4245 if (TREE_CODE (decl) == SAVE_EXPR)
4246 context = SAVE_EXPR_CONTEXT (decl);
4248 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
4249 where we look up the function at runtime. Such functions always take
4250 a first argument of type 'pointer to real context'.
4252 C++ should really be fixed to use DECL_CONTEXT for the real context,
4253 and use something else for the "virtual context". */
4254 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
4255 context
4256 = TYPE_MAIN_VARIANT
4257 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4258 else
4259 context = DECL_CONTEXT (decl);
4261 while (context && TREE_CODE (context) != FUNCTION_DECL)
4263 if (TREE_CODE (context) == BLOCK)
4264 context = BLOCK_SUPERCONTEXT (context);
4265 else
4266 context = get_containing_scope (context);
4269 return context;
4272 /* Return the innermost context enclosing DECL that is
4273 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
4274 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
4276 tree
4277 decl_type_context (decl)
4278 tree decl;
4280 tree context = DECL_CONTEXT (decl);
4282 while (context)
4284 if (TREE_CODE (context) == RECORD_TYPE
4285 || TREE_CODE (context) == UNION_TYPE
4286 || TREE_CODE (context) == QUAL_UNION_TYPE)
4287 return context;
4289 if (TREE_CODE (context) == TYPE_DECL
4290 || TREE_CODE (context) == FUNCTION_DECL)
4291 context = DECL_CONTEXT (context);
4293 else if (TREE_CODE (context) == BLOCK)
4294 context = BLOCK_SUPERCONTEXT (context);
4296 else
4297 /* Unhandled CONTEXT!? */
4298 abort ();
4300 return NULL_TREE;
4303 /* CALL is a CALL_EXPR. Return the declaration for the function
4304 called, or NULL_TREE if the called function cannot be
4305 determined. */
4307 tree
4308 get_callee_fndecl (call)
4309 tree call;
4311 tree addr;
4313 /* It's invalid to call this function with anything but a
4314 CALL_EXPR. */
4315 if (TREE_CODE (call) != CALL_EXPR)
4316 abort ();
4318 /* The first operand to the CALL is the address of the function
4319 called. */
4320 addr = TREE_OPERAND (call, 0);
4322 STRIP_NOPS (addr);
4324 /* If this is a readonly function pointer, extract its initial value. */
4325 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
4326 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
4327 && DECL_INITIAL (addr))
4328 addr = DECL_INITIAL (addr);
4330 /* If the address is just `&f' for some function `f', then we know
4331 that `f' is being called. */
4332 if (TREE_CODE (addr) == ADDR_EXPR
4333 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
4334 return TREE_OPERAND (addr, 0);
4336 /* We couldn't figure out what was being called. */
4337 return NULL_TREE;
4340 /* Print debugging information about the obstack O, named STR. */
4342 void
4343 print_obstack_statistics (str, o)
4344 const char *str;
4345 struct obstack *o;
4347 struct _obstack_chunk *chunk = o->chunk;
4348 int n_chunks = 1;
4349 int n_alloc = 0;
4351 n_alloc += o->next_free - chunk->contents;
4352 chunk = chunk->prev;
4353 while (chunk)
4355 n_chunks += 1;
4356 n_alloc += chunk->limit - &chunk->contents[0];
4357 chunk = chunk->prev;
4359 fprintf (stderr, "obstack %s: %u bytes, %d chunks\n",
4360 str, n_alloc, n_chunks);
4363 /* Print debugging information about tree nodes generated during the compile,
4364 and any language-specific information. */
4366 void
4367 dump_tree_statistics ()
4369 #ifdef GATHER_STATISTICS
4370 int i;
4371 int total_nodes, total_bytes;
4372 #endif
4374 fprintf (stderr, "\n??? tree nodes created\n\n");
4375 #ifdef GATHER_STATISTICS
4376 fprintf (stderr, "Kind Nodes Bytes\n");
4377 fprintf (stderr, "-------------------------------------\n");
4378 total_nodes = total_bytes = 0;
4379 for (i = 0; i < (int) all_kinds; i++)
4381 fprintf (stderr, "%-20s %6d %9d\n", tree_node_kind_names[i],
4382 tree_node_counts[i], tree_node_sizes[i]);
4383 total_nodes += tree_node_counts[i];
4384 total_bytes += tree_node_sizes[i];
4386 fprintf (stderr, "%-20s %9d\n", "identifier names", id_string_size);
4387 fprintf (stderr, "-------------------------------------\n");
4388 fprintf (stderr, "%-20s %6d %9d\n", "Total", total_nodes, total_bytes);
4389 fprintf (stderr, "-------------------------------------\n");
4390 #else
4391 fprintf (stderr, "(No per-node statistics)\n");
4392 #endif
4393 print_obstack_statistics ("permanent_obstack", &permanent_obstack);
4394 print_type_hash_statistics ();
4395 print_lang_statistics ();
4398 #define FILE_FUNCTION_PREFIX_LEN 9
4400 #ifndef NO_DOLLAR_IN_LABEL
4401 #define FILE_FUNCTION_FORMAT "_GLOBAL_$%s$%s"
4402 #else /* NO_DOLLAR_IN_LABEL */
4403 #ifndef NO_DOT_IN_LABEL
4404 #define FILE_FUNCTION_FORMAT "_GLOBAL_.%s.%s"
4405 #else /* NO_DOT_IN_LABEL */
4406 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
4407 #endif /* NO_DOT_IN_LABEL */
4408 #endif /* NO_DOLLAR_IN_LABEL */
4410 /* Appends 6 random characters to TEMPLATE to (hopefully) avoid name
4411 clashes in cases where we can't reliably choose a unique name.
4413 Derived from mkstemp.c in libiberty. */
4415 static void
4416 append_random_chars (template)
4417 char *template;
4419 static const char letters[]
4420 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
4421 static unsigned HOST_WIDE_INT value;
4422 unsigned HOST_WIDE_INT v;
4424 #ifdef HAVE_GETTIMEOFDAY
4425 struct timeval tv;
4426 #endif
4428 template += strlen (template);
4430 #ifdef HAVE_GETTIMEOFDAY
4431 /* Get some more or less random data. */
4432 gettimeofday (&tv, NULL);
4433 value += ((unsigned HOST_WIDE_INT) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
4434 #else
4435 value += getpid ();
4436 #endif
4438 v = value;
4440 /* Fill in the random bits. */
4441 template[0] = letters[v % 62];
4442 v /= 62;
4443 template[1] = letters[v % 62];
4444 v /= 62;
4445 template[2] = letters[v % 62];
4446 v /= 62;
4447 template[3] = letters[v % 62];
4448 v /= 62;
4449 template[4] = letters[v % 62];
4450 v /= 62;
4451 template[5] = letters[v % 62];
4453 template[6] = '\0';
4456 /* P is a string that will be used in a symbol. Mask out any characters
4457 that are not valid in that context. */
4459 void
4460 clean_symbol_name (p)
4461 char *p;
4463 for (; *p; p++)
4464 if (! (ISDIGIT(*p)
4465 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
4466 || *p == '$'
4467 #endif
4468 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
4469 || *p == '.'
4470 #endif
4471 || ISUPPER (*p)
4472 || ISLOWER (*p)))
4473 *p = '_';
4476 /* Generate a name for a function unique to this translation unit.
4477 TYPE is some string to identify the purpose of this function to the
4478 linker or collect2. */
4480 tree
4481 get_file_function_name_long (type)
4482 const char *type;
4484 char *buf;
4485 const char *p;
4486 char *q;
4488 if (first_global_object_name)
4489 p = first_global_object_name;
4490 else
4492 /* We don't have anything that we know to be unique to this translation
4493 unit, so use what we do have and throw in some randomness. */
4495 const char *name = weak_global_object_name;
4496 const char *file = main_input_filename;
4498 if (! name)
4499 name = "";
4500 if (! file)
4501 file = input_filename;
4503 q = (char *) alloca (7 + strlen (name) + strlen (file));
4505 sprintf (q, "%s%s", name, file);
4506 append_random_chars (q);
4507 p = q;
4510 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
4511 + strlen (type));
4513 /* Set up the name of the file-level functions we may need.
4514 Use a global object (which is already required to be unique over
4515 the program) rather than the file name (which imposes extra
4516 constraints). */
4517 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
4519 /* Don't need to pull weird characters out of global names. */
4520 if (p != first_global_object_name)
4521 clean_symbol_name (buf + 11);
4523 return get_identifier (buf);
4526 /* If KIND=='I', return a suitable global initializer (constructor) name.
4527 If KIND=='D', return a suitable global clean-up (destructor) name. */
4529 tree
4530 get_file_function_name (kind)
4531 int kind;
4533 char p[2];
4535 p[0] = kind;
4536 p[1] = 0;
4538 return get_file_function_name_long (p);
4541 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4542 The result is placed in BUFFER (which has length BIT_SIZE),
4543 with one bit in each char ('\000' or '\001').
4545 If the constructor is constant, NULL_TREE is returned.
4546 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4548 tree
4549 get_set_constructor_bits (init, buffer, bit_size)
4550 tree init;
4551 char *buffer;
4552 int bit_size;
4554 int i;
4555 tree vals;
4556 HOST_WIDE_INT domain_min
4557 = TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (init))));
4558 tree non_const_bits = NULL_TREE;
4559 for (i = 0; i < bit_size; i++)
4560 buffer[i] = 0;
4562 for (vals = TREE_OPERAND (init, 1);
4563 vals != NULL_TREE; vals = TREE_CHAIN (vals))
4565 if (TREE_CODE (TREE_VALUE (vals)) != INTEGER_CST
4566 || (TREE_PURPOSE (vals) != NULL_TREE
4567 && TREE_CODE (TREE_PURPOSE (vals)) != INTEGER_CST))
4568 non_const_bits
4569 = tree_cons (TREE_PURPOSE (vals), TREE_VALUE (vals), non_const_bits);
4570 else if (TREE_PURPOSE (vals) != NULL_TREE)
4572 /* Set a range of bits to ones. */
4573 HOST_WIDE_INT lo_index
4574 = TREE_INT_CST_LOW (TREE_PURPOSE (vals)) - domain_min;
4575 HOST_WIDE_INT hi_index
4576 = TREE_INT_CST_LOW (TREE_VALUE (vals)) - domain_min;
4578 if (lo_index < 0 || lo_index >= bit_size
4579 || hi_index < 0 || hi_index >= bit_size)
4580 abort ();
4581 for (; lo_index <= hi_index; lo_index++)
4582 buffer[lo_index] = 1;
4584 else
4586 /* Set a single bit to one. */
4587 HOST_WIDE_INT index
4588 = TREE_INT_CST_LOW (TREE_VALUE (vals)) - domain_min;
4589 if (index < 0 || index >= bit_size)
4591 error ("invalid initializer for bit string");
4592 return NULL_TREE;
4594 buffer[index] = 1;
4597 return non_const_bits;
4600 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4601 The result is placed in BUFFER (which is an array of bytes).
4602 If the constructor is constant, NULL_TREE is returned.
4603 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4605 tree
4606 get_set_constructor_bytes (init, buffer, wd_size)
4607 tree init;
4608 unsigned char *buffer;
4609 int wd_size;
4611 int i;
4612 int set_word_size = BITS_PER_UNIT;
4613 int bit_size = wd_size * set_word_size;
4614 int bit_pos = 0;
4615 unsigned char *bytep = buffer;
4616 char *bit_buffer = (char *) alloca (bit_size);
4617 tree non_const_bits = get_set_constructor_bits (init, bit_buffer, bit_size);
4619 for (i = 0; i < wd_size; i++)
4620 buffer[i] = 0;
4622 for (i = 0; i < bit_size; i++)
4624 if (bit_buffer[i])
4626 if (BYTES_BIG_ENDIAN)
4627 *bytep |= (1 << (set_word_size - 1 - bit_pos));
4628 else
4629 *bytep |= 1 << bit_pos;
4631 bit_pos++;
4632 if (bit_pos >= set_word_size)
4633 bit_pos = 0, bytep++;
4635 return non_const_bits;
4638 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4639 /* Complain that the tree code of NODE does not match the expected CODE.
4640 FILE, LINE, and FUNCTION are of the caller. */
4642 void
4643 tree_check_failed (node, code, file, line, function)
4644 const tree node;
4645 enum tree_code code;
4646 const char *file;
4647 int line;
4648 const char *function;
4650 error ("Tree check: expected %s, have %s",
4651 tree_code_name[code], tree_code_name[TREE_CODE (node)]);
4652 fancy_abort (file, line, function);
4655 /* Similar to above, except that we check for a class of tree
4656 code, given in CL. */
4658 void
4659 tree_class_check_failed (node, cl, file, line, function)
4660 const tree node;
4661 int cl;
4662 const char *file;
4663 int line;
4664 const char *function;
4666 error ("Tree check: expected class '%c', have '%c' (%s)",
4667 cl, TREE_CODE_CLASS (TREE_CODE (node)),
4668 tree_code_name[TREE_CODE (node)]);
4669 fancy_abort (file, line, function);
4672 #endif /* ENABLE_TREE_CHECKING */
4674 /* For a new vector type node T, build the information necessary for
4675 debuggint output. */
4677 static void
4678 finish_vector_type (t)
4679 tree t;
4681 layout_type (t);
4684 tree index = build_int_2 (TYPE_VECTOR_SUBPARTS (t) - 1, 0);
4685 tree array = build_array_type (TREE_TYPE (t),
4686 build_index_type (index));
4687 tree rt = make_node (RECORD_TYPE);
4689 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
4690 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
4691 layout_type (rt);
4692 TYPE_DEBUG_REPRESENTATION_TYPE (t) = rt;
4693 /* In dwarfout.c, type lookup uses TYPE_UID numbers. We want to output
4694 the representation type, and we want to find that die when looking up
4695 the vector type. This is most easily achieved by making the TYPE_UID
4696 numbers equal. */
4697 TYPE_UID (rt) = TYPE_UID (t);
4701 /* Create nodes for all integer types (and error_mark_node) using the sizes
4702 of C datatypes. The caller should call set_sizetype soon after calling
4703 this function to select one of the types as sizetype. */
4705 void
4706 build_common_tree_nodes (signed_char)
4707 int signed_char;
4709 error_mark_node = make_node (ERROR_MARK);
4710 TREE_TYPE (error_mark_node) = error_mark_node;
4712 initialize_sizetypes ();
4714 /* Define both `signed char' and `unsigned char'. */
4715 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
4716 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
4718 /* Define `char', which is like either `signed char' or `unsigned char'
4719 but not the same as either. */
4720 char_type_node
4721 = (signed_char
4722 ? make_signed_type (CHAR_TYPE_SIZE)
4723 : make_unsigned_type (CHAR_TYPE_SIZE));
4725 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
4726 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
4727 integer_type_node = make_signed_type (INT_TYPE_SIZE);
4728 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
4729 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
4730 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
4731 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
4732 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
4734 intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
4735 intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
4736 intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
4737 intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
4738 #if HOST_BITS_PER_WIDE_INT >= 64
4739 intTI_type_node = make_signed_type (GET_MODE_BITSIZE (TImode));
4740 #endif
4742 unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
4743 unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
4744 unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
4745 unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
4746 #if HOST_BITS_PER_WIDE_INT >= 64
4747 unsigned_intTI_type_node = make_unsigned_type (GET_MODE_BITSIZE (TImode));
4748 #endif
4751 /* Call this function after calling build_common_tree_nodes and set_sizetype.
4752 It will create several other common tree nodes. */
4754 void
4755 build_common_tree_nodes_2 (short_double)
4756 int short_double;
4758 /* Define these next since types below may used them. */
4759 integer_zero_node = build_int_2 (0, 0);
4760 integer_one_node = build_int_2 (1, 0);
4762 size_zero_node = size_int (0);
4763 size_one_node = size_int (1);
4764 bitsize_zero_node = bitsize_int (0);
4765 bitsize_one_node = bitsize_int (1);
4766 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
4768 void_type_node = make_node (VOID_TYPE);
4769 layout_type (void_type_node);
4771 /* We are not going to have real types in C with less than byte alignment,
4772 so we might as well not have any types that claim to have it. */
4773 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
4774 TYPE_USER_ALIGN (void_type_node) = 0;
4776 null_pointer_node = build_int_2 (0, 0);
4777 TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
4778 layout_type (TREE_TYPE (null_pointer_node));
4780 ptr_type_node = build_pointer_type (void_type_node);
4781 const_ptr_type_node
4782 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
4784 float_type_node = make_node (REAL_TYPE);
4785 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
4786 layout_type (float_type_node);
4788 double_type_node = make_node (REAL_TYPE);
4789 if (short_double)
4790 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
4791 else
4792 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
4793 layout_type (double_type_node);
4795 long_double_type_node = make_node (REAL_TYPE);
4796 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
4797 layout_type (long_double_type_node);
4799 complex_integer_type_node = make_node (COMPLEX_TYPE);
4800 TREE_TYPE (complex_integer_type_node) = integer_type_node;
4801 layout_type (complex_integer_type_node);
4803 complex_float_type_node = make_node (COMPLEX_TYPE);
4804 TREE_TYPE (complex_float_type_node) = float_type_node;
4805 layout_type (complex_float_type_node);
4807 complex_double_type_node = make_node (COMPLEX_TYPE);
4808 TREE_TYPE (complex_double_type_node) = double_type_node;
4809 layout_type (complex_double_type_node);
4811 complex_long_double_type_node = make_node (COMPLEX_TYPE);
4812 TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
4813 layout_type (complex_long_double_type_node);
4815 #ifdef BUILD_VA_LIST_TYPE
4816 BUILD_VA_LIST_TYPE (va_list_type_node);
4817 #else
4818 va_list_type_node = build_type_copy (ptr_type_node);
4819 #endif
4821 V4SF_type_node = make_node (VECTOR_TYPE);
4822 TREE_TYPE (V4SF_type_node) = float_type_node;
4823 TYPE_MODE (V4SF_type_node) = V4SFmode;
4824 finish_vector_type (V4SF_type_node);
4826 V4SI_type_node = make_node (VECTOR_TYPE);
4827 TREE_TYPE (V4SI_type_node) = intSI_type_node;
4828 TYPE_MODE (V4SI_type_node) = V4SImode;
4829 finish_vector_type (V4SI_type_node);
4831 V2SI_type_node = make_node (VECTOR_TYPE);
4832 TREE_TYPE (V2SI_type_node) = intSI_type_node;
4833 TYPE_MODE (V2SI_type_node) = V2SImode;
4834 finish_vector_type (V2SI_type_node);
4836 V4HI_type_node = make_node (VECTOR_TYPE);
4837 TREE_TYPE (V4HI_type_node) = intHI_type_node;
4838 TYPE_MODE (V4HI_type_node) = V4HImode;
4839 finish_vector_type (V4HI_type_node);
4841 V8QI_type_node = make_node (VECTOR_TYPE);
4842 TREE_TYPE (V8QI_type_node) = intQI_type_node;
4843 TYPE_MODE (V8QI_type_node) = V8QImode;
4844 finish_vector_type (V8QI_type_node);