2002-08-22 Paolo Carlini <pcarlini@unitus.it>
[official-gcc.git] / gcc / tree.c
blob1a5910e985dbe557c4b8114bb52c822acbd7aa30
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, 2001, 2002 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 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 #include "config.h"
33 #include "system.h"
34 #include "flags.h"
35 #include "tree.h"
36 #include "real.h"
37 #include "tm_p.h"
38 #include "function.h"
39 #include "obstack.h"
40 #include "toplev.h"
41 #include "ggc.h"
42 #include "hashtab.h"
43 #include "output.h"
44 #include "target.h"
45 #include "langhooks.h"
47 /* obstack.[ch] explicitly declined to prototype this. */
48 extern int _obstack_allocated_p PARAMS ((struct obstack *h, PTR obj));
50 #ifdef GATHER_STATISTICS
51 /* Statistics-gathering stuff. */
52 typedef enum
54 d_kind,
55 t_kind,
56 b_kind,
57 s_kind,
58 r_kind,
59 e_kind,
60 c_kind,
61 id_kind,
62 perm_list_kind,
63 temp_list_kind,
64 vec_kind,
65 x_kind,
66 lang_decl,
67 lang_type,
68 all_kinds
69 } tree_node_kind;
71 int tree_node_counts[(int) all_kinds];
72 int tree_node_sizes[(int) all_kinds];
74 static const char * const tree_node_kind_names[] = {
75 "decls",
76 "types",
77 "blocks",
78 "stmts",
79 "refs",
80 "exprs",
81 "constants",
82 "identifiers",
83 "perm_tree_lists",
84 "temp_tree_lists",
85 "vecs",
86 "random kinds",
87 "lang_decl kinds",
88 "lang_type kinds"
90 #endif /* GATHER_STATISTICS */
92 /* Unique id for next decl created. */
93 static int next_decl_uid;
94 /* Unique id for next type created. */
95 static int next_type_uid = 1;
97 /* Since we cannot rehash a type after it is in the table, we have to
98 keep the hash code. */
100 struct type_hash GTY(())
102 unsigned long hash;
103 tree type;
106 /* Initial size of the hash table (rounded to next prime). */
107 #define TYPE_HASH_INITIAL_SIZE 1000
109 /* Now here is the hash table. When recording a type, it is added to
110 the slot whose index is the hash code. Note that the hash table is
111 used for several kinds of types (function types, array types and
112 array index range types, for now). While all these live in the
113 same table, they are completely independent, and the hash code is
114 computed differently for each of these. */
116 static GTY ((if_marked ("type_hash_marked_p"), param_is (struct type_hash)))
117 htab_t type_hash_table;
119 static void set_type_quals PARAMS ((tree, int));
120 static void append_random_chars PARAMS ((char *));
121 static int type_hash_eq PARAMS ((const void *, const void *));
122 static unsigned int type_hash_hash PARAMS ((const void *));
123 static void print_type_hash_statistics PARAMS((void));
124 static void finish_vector_type PARAMS((tree));
125 static tree make_vector PARAMS ((enum machine_mode, tree, int));
126 static int type_hash_marked_p PARAMS ((const void *));
128 tree global_trees[TI_MAX];
129 tree integer_types[itk_none];
131 /* Init tree.c. */
133 void
134 init_ttree ()
136 /* Initialize the hash table of types. */
137 type_hash_table = htab_create (TYPE_HASH_INITIAL_SIZE, type_hash_hash,
138 type_hash_eq, 0);
142 /* The name of the object as the assembler will see it (but before any
143 translations made by ASM_OUTPUT_LABELREF). Often this is the same
144 as DECL_NAME. It is an IDENTIFIER_NODE. */
145 tree
146 decl_assembler_name (decl)
147 tree decl;
149 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
150 (*lang_hooks.set_decl_assembler_name) (decl);
151 return DECL_CHECK (decl)->decl.assembler_name;
154 /* Compute the number of bytes occupied by 'node'. This routine only
155 looks at TREE_CODE and, if the code is TREE_VEC, TREE_VEC_LENGTH. */
156 size_t
157 tree_size (node)
158 tree node;
160 enum tree_code code = TREE_CODE (node);
162 switch (TREE_CODE_CLASS (code))
164 case 'd': /* A decl node */
165 return sizeof (struct tree_decl);
167 case 't': /* a type node */
168 return sizeof (struct tree_type);
170 case 'b': /* a lexical block node */
171 return sizeof (struct tree_block);
173 case 'r': /* a reference */
174 case 'e': /* an expression */
175 case 's': /* an expression with side effects */
176 case '<': /* a comparison expression */
177 case '1': /* a unary arithmetic expression */
178 case '2': /* a binary arithmetic expression */
179 return (sizeof (struct tree_exp)
180 + (TREE_CODE_LENGTH (code) - 1) * sizeof (char *));
182 case 'c': /* a constant */
183 /* We can't use TREE_CODE_LENGTH for INTEGER_CST, since the number of
184 words is machine-dependent due to varying length of HOST_WIDE_INT,
185 which might be wider than a pointer (e.g., long long). Similarly
186 for REAL_CST, since the number of words is machine-dependent due
187 to varying size and alignment of `double'. */
188 if (code == INTEGER_CST)
189 return sizeof (struct tree_int_cst);
190 else if (code == REAL_CST)
191 return sizeof (struct tree_real_cst);
192 else
193 return (sizeof (struct tree_common)
194 + TREE_CODE_LENGTH (code) * sizeof (char *));
196 case 'x': /* something random, like an identifier. */
198 size_t length;
199 length = (sizeof (struct tree_common)
200 + TREE_CODE_LENGTH (code) * sizeof (char *));
201 if (code == TREE_VEC)
202 length += (TREE_VEC_LENGTH (node) - 1) * sizeof (char *);
203 return length;
206 default:
207 abort ();
211 /* Return a newly allocated node of code CODE.
212 For decl and type nodes, some other fields are initialized.
213 The rest of the node is initialized to zero.
215 Achoo! I got a code in the node. */
217 tree
218 make_node (code)
219 enum tree_code code;
221 tree t;
222 int type = TREE_CODE_CLASS (code);
223 size_t length;
224 #ifdef GATHER_STATISTICS
225 tree_node_kind kind;
226 #endif
227 struct tree_common ttmp;
229 /* We can't allocate a TREE_VEC without knowing how many elements
230 it will have. */
231 if (code == TREE_VEC)
232 abort ();
234 TREE_SET_CODE ((tree)&ttmp, code);
235 length = tree_size ((tree)&ttmp);
237 #ifdef GATHER_STATISTICS
238 switch (type)
240 case 'd': /* A decl node */
241 kind = d_kind;
242 break;
244 case 't': /* a type node */
245 kind = t_kind;
246 break;
248 case 'b': /* a lexical block */
249 kind = b_kind;
250 break;
252 case 's': /* an expression with side effects */
253 kind = s_kind;
254 break;
256 case 'r': /* a reference */
257 kind = r_kind;
258 break;
260 case 'e': /* an expression */
261 case '<': /* a comparison expression */
262 case '1': /* a unary arithmetic expression */
263 case '2': /* a binary arithmetic expression */
264 kind = e_kind;
265 break;
267 case 'c': /* a constant */
268 kind = c_kind;
269 break;
271 case 'x': /* something random, like an identifier. */
272 if (code == IDENTIFIER_NODE)
273 kind = id_kind;
274 else if (code == TREE_VEC)
275 kind = vec_kind;
276 else
277 kind = x_kind;
278 break;
280 default:
281 abort ();
284 tree_node_counts[(int) kind]++;
285 tree_node_sizes[(int) kind] += length;
286 #endif
288 t = ggc_alloc_tree (length);
290 memset ((PTR) t, 0, length);
292 TREE_SET_CODE (t, code);
294 switch (type)
296 case 's':
297 TREE_SIDE_EFFECTS (t) = 1;
298 TREE_TYPE (t) = void_type_node;
299 break;
301 case 'd':
302 if (code != FUNCTION_DECL)
303 DECL_ALIGN (t) = 1;
304 DECL_USER_ALIGN (t) = 0;
305 DECL_IN_SYSTEM_HEADER (t) = in_system_header;
306 DECL_SOURCE_LINE (t) = lineno;
307 DECL_SOURCE_FILE (t) =
308 (input_filename) ? input_filename : "<built-in>";
309 DECL_UID (t) = next_decl_uid++;
311 /* We have not yet computed the alias set for this declaration. */
312 DECL_POINTER_ALIAS_SET (t) = -1;
313 break;
315 case 't':
316 TYPE_UID (t) = next_type_uid++;
317 TYPE_ALIGN (t) = char_type_node ? TYPE_ALIGN (char_type_node) : 0;
318 TYPE_USER_ALIGN (t) = 0;
319 TYPE_MAIN_VARIANT (t) = t;
321 /* Default to no attributes for type, but let target change that. */
322 TYPE_ATTRIBUTES (t) = NULL_TREE;
323 (*targetm.set_default_type_attributes) (t);
325 /* We have not yet computed the alias set for this type. */
326 TYPE_ALIAS_SET (t) = -1;
327 break;
329 case 'c':
330 TREE_CONSTANT (t) = 1;
331 break;
333 case 'e':
334 switch (code)
336 case INIT_EXPR:
337 case MODIFY_EXPR:
338 case VA_ARG_EXPR:
339 case RTL_EXPR:
340 case PREDECREMENT_EXPR:
341 case PREINCREMENT_EXPR:
342 case POSTDECREMENT_EXPR:
343 case POSTINCREMENT_EXPR:
344 /* All of these have side-effects, no matter what their
345 operands are. */
346 TREE_SIDE_EFFECTS (t) = 1;
347 break;
349 default:
350 break;
352 break;
355 return t;
358 /* Return a new node with the same contents as NODE except that its
359 TREE_CHAIN is zero and it has a fresh uid. */
361 tree
362 copy_node (node)
363 tree node;
365 tree t;
366 enum tree_code code = TREE_CODE (node);
367 size_t length;
369 length = tree_size (node);
370 t = ggc_alloc_tree (length);
371 memcpy (t, node, length);
373 TREE_CHAIN (t) = 0;
374 TREE_ASM_WRITTEN (t) = 0;
376 if (TREE_CODE_CLASS (code) == 'd')
377 DECL_UID (t) = next_decl_uid++;
378 else if (TREE_CODE_CLASS (code) == 't')
380 TYPE_UID (t) = next_type_uid++;
381 /* The following is so that the debug code for
382 the copy is different from the original type.
383 The two statements usually duplicate each other
384 (because they clear fields of the same union),
385 but the optimizer should catch that. */
386 TYPE_SYMTAB_POINTER (t) = 0;
387 TYPE_SYMTAB_ADDRESS (t) = 0;
390 return t;
393 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
394 For example, this can copy a list made of TREE_LIST nodes. */
396 tree
397 copy_list (list)
398 tree list;
400 tree head;
401 tree prev, next;
403 if (list == 0)
404 return 0;
406 head = prev = copy_node (list);
407 next = TREE_CHAIN (list);
408 while (next)
410 TREE_CHAIN (prev) = copy_node (next);
411 prev = TREE_CHAIN (prev);
412 next = TREE_CHAIN (next);
414 return head;
418 /* Return a newly constructed INTEGER_CST node whose constant value
419 is specified by the two ints LOW and HI.
420 The TREE_TYPE is set to `int'.
422 This function should be used via the `build_int_2' macro. */
424 tree
425 build_int_2_wide (low, hi)
426 unsigned HOST_WIDE_INT low;
427 HOST_WIDE_INT hi;
429 tree t = make_node (INTEGER_CST);
431 TREE_INT_CST_LOW (t) = low;
432 TREE_INT_CST_HIGH (t) = hi;
433 TREE_TYPE (t) = integer_type_node;
434 return t;
437 /* Return a new VECTOR_CST node whose type is TYPE and whose values
438 are in a list pointed by VALS. */
440 tree
441 build_vector (type, vals)
442 tree type, vals;
444 tree v = make_node (VECTOR_CST);
445 int over1 = 0, over2 = 0;
446 tree link;
448 TREE_VECTOR_CST_ELTS (v) = vals;
449 TREE_TYPE (v) = type;
451 /* Iterate through elements and check for overflow. */
452 for (link = vals; link; link = TREE_CHAIN (link))
454 tree value = TREE_VALUE (link);
456 over1 |= TREE_OVERFLOW (value);
457 over2 |= TREE_CONSTANT_OVERFLOW (value);
460 TREE_OVERFLOW (v) = over1;
461 TREE_CONSTANT_OVERFLOW (v) = over2;
463 return v;
466 /* Return a new REAL_CST node whose type is TYPE and value is D. */
468 tree
469 build_real (type, d)
470 tree type;
471 REAL_VALUE_TYPE d;
473 tree v;
474 REAL_VALUE_TYPE *dp;
475 int overflow = 0;
477 /* Check for valid float value for this type on this target machine;
478 if not, can print error message and store a valid value in D. */
479 #ifdef CHECK_FLOAT_VALUE
480 CHECK_FLOAT_VALUE (TYPE_MODE (type), d, overflow);
481 #endif
483 v = make_node (REAL_CST);
484 dp = ggc_alloc (sizeof (REAL_VALUE_TYPE));
485 memcpy (dp, &d, sizeof (REAL_VALUE_TYPE));
487 TREE_TYPE (v) = type;
488 TREE_REAL_CST_PTR (v) = dp;
489 TREE_OVERFLOW (v) = TREE_CONSTANT_OVERFLOW (v) = overflow;
490 return v;
493 /* Return a new REAL_CST node whose type is TYPE
494 and whose value is the integer value of the INTEGER_CST node I. */
496 REAL_VALUE_TYPE
497 real_value_from_int_cst (type, i)
498 tree type ATTRIBUTE_UNUSED, i;
500 REAL_VALUE_TYPE d;
502 /* Clear all bits of the real value type so that we can later do
503 bitwise comparisons to see if two values are the same. */
504 memset ((char *) &d, 0, sizeof d);
506 if (! TREE_UNSIGNED (TREE_TYPE (i)))
507 REAL_VALUE_FROM_INT (d, TREE_INT_CST_LOW (i), TREE_INT_CST_HIGH (i),
508 TYPE_MODE (type));
509 else
510 REAL_VALUE_FROM_UNSIGNED_INT (d, TREE_INT_CST_LOW (i),
511 TREE_INT_CST_HIGH (i), TYPE_MODE (type));
512 return d;
515 /* Given a tree representing an integer constant I, return a tree
516 representing the same value as a floating-point constant of type TYPE. */
518 tree
519 build_real_from_int_cst (type, i)
520 tree type;
521 tree i;
523 tree v;
524 int overflow = TREE_OVERFLOW (i);
526 v = build_real (type, real_value_from_int_cst (type, i));
528 TREE_OVERFLOW (v) |= overflow;
529 TREE_CONSTANT_OVERFLOW (v) |= overflow;
530 return v;
533 /* Return a newly constructed STRING_CST node whose value is
534 the LEN characters at STR.
535 The TREE_TYPE is not initialized. */
537 tree
538 build_string (len, str)
539 int len;
540 const char *str;
542 tree s = make_node (STRING_CST);
544 TREE_STRING_LENGTH (s) = len;
545 TREE_STRING_POINTER (s) = ggc_alloc_string (str, len);
547 return s;
550 /* Return a newly constructed COMPLEX_CST node whose value is
551 specified by the real and imaginary parts REAL and IMAG.
552 Both REAL and IMAG should be constant nodes. TYPE, if specified,
553 will be the type of the COMPLEX_CST; otherwise a new type will be made. */
555 tree
556 build_complex (type, real, imag)
557 tree type;
558 tree real, imag;
560 tree t = make_node (COMPLEX_CST);
562 TREE_REALPART (t) = real;
563 TREE_IMAGPART (t) = imag;
564 TREE_TYPE (t) = type ? type : build_complex_type (TREE_TYPE (real));
565 TREE_OVERFLOW (t) = TREE_OVERFLOW (real) | TREE_OVERFLOW (imag);
566 TREE_CONSTANT_OVERFLOW (t)
567 = TREE_CONSTANT_OVERFLOW (real) | TREE_CONSTANT_OVERFLOW (imag);
568 return t;
571 /* Build a newly constructed TREE_VEC node of length LEN. */
573 tree
574 make_tree_vec (len)
575 int len;
577 tree t;
578 int length = (len - 1) * sizeof (tree) + sizeof (struct tree_vec);
580 #ifdef GATHER_STATISTICS
581 tree_node_counts[(int) vec_kind]++;
582 tree_node_sizes[(int) vec_kind] += length;
583 #endif
585 t = ggc_alloc_tree (length);
587 memset ((PTR) t, 0, length);
588 TREE_SET_CODE (t, TREE_VEC);
589 TREE_VEC_LENGTH (t) = len;
591 return t;
594 /* Return 1 if EXPR is the integer constant zero or a complex constant
595 of zero. */
598 integer_zerop (expr)
599 tree expr;
601 STRIP_NOPS (expr);
603 return ((TREE_CODE (expr) == INTEGER_CST
604 && ! TREE_CONSTANT_OVERFLOW (expr)
605 && TREE_INT_CST_LOW (expr) == 0
606 && TREE_INT_CST_HIGH (expr) == 0)
607 || (TREE_CODE (expr) == COMPLEX_CST
608 && integer_zerop (TREE_REALPART (expr))
609 && integer_zerop (TREE_IMAGPART (expr))));
612 /* Return 1 if EXPR is the integer constant one or the corresponding
613 complex constant. */
616 integer_onep (expr)
617 tree expr;
619 STRIP_NOPS (expr);
621 return ((TREE_CODE (expr) == INTEGER_CST
622 && ! TREE_CONSTANT_OVERFLOW (expr)
623 && TREE_INT_CST_LOW (expr) == 1
624 && TREE_INT_CST_HIGH (expr) == 0)
625 || (TREE_CODE (expr) == COMPLEX_CST
626 && integer_onep (TREE_REALPART (expr))
627 && integer_zerop (TREE_IMAGPART (expr))));
630 /* Return 1 if EXPR is an integer containing all 1's in as much precision as
631 it contains. Likewise for the corresponding complex constant. */
634 integer_all_onesp (expr)
635 tree expr;
637 int prec;
638 int uns;
640 STRIP_NOPS (expr);
642 if (TREE_CODE (expr) == COMPLEX_CST
643 && integer_all_onesp (TREE_REALPART (expr))
644 && integer_zerop (TREE_IMAGPART (expr)))
645 return 1;
647 else if (TREE_CODE (expr) != INTEGER_CST
648 || TREE_CONSTANT_OVERFLOW (expr))
649 return 0;
651 uns = TREE_UNSIGNED (TREE_TYPE (expr));
652 if (!uns)
653 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
654 && TREE_INT_CST_HIGH (expr) == -1);
656 /* Note that using TYPE_PRECISION here is wrong. We care about the
657 actual bits, not the (arbitrary) range of the type. */
658 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (expr)));
659 if (prec >= HOST_BITS_PER_WIDE_INT)
661 HOST_WIDE_INT high_value;
662 int shift_amount;
664 shift_amount = prec - HOST_BITS_PER_WIDE_INT;
666 if (shift_amount > HOST_BITS_PER_WIDE_INT)
667 /* Can not handle precisions greater than twice the host int size. */
668 abort ();
669 else if (shift_amount == HOST_BITS_PER_WIDE_INT)
670 /* Shifting by the host word size is undefined according to the ANSI
671 standard, so we must handle this as a special case. */
672 high_value = -1;
673 else
674 high_value = ((HOST_WIDE_INT) 1 << shift_amount) - 1;
676 return (TREE_INT_CST_LOW (expr) == ~(unsigned HOST_WIDE_INT) 0
677 && TREE_INT_CST_HIGH (expr) == high_value);
679 else
680 return TREE_INT_CST_LOW (expr) == ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
683 /* Return 1 if EXPR is an integer constant that is a power of 2 (i.e., has only
684 one bit on). */
687 integer_pow2p (expr)
688 tree expr;
690 int prec;
691 HOST_WIDE_INT high, low;
693 STRIP_NOPS (expr);
695 if (TREE_CODE (expr) == COMPLEX_CST
696 && integer_pow2p (TREE_REALPART (expr))
697 && integer_zerop (TREE_IMAGPART (expr)))
698 return 1;
700 if (TREE_CODE (expr) != INTEGER_CST || TREE_CONSTANT_OVERFLOW (expr))
701 return 0;
703 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
704 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
705 high = TREE_INT_CST_HIGH (expr);
706 low = TREE_INT_CST_LOW (expr);
708 /* First clear all bits that are beyond the type's precision in case
709 we've been sign extended. */
711 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
713 else if (prec > HOST_BITS_PER_WIDE_INT)
714 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
715 else
717 high = 0;
718 if (prec < HOST_BITS_PER_WIDE_INT)
719 low &= ~((HOST_WIDE_INT) (-1) << prec);
722 if (high == 0 && low == 0)
723 return 0;
725 return ((high == 0 && (low & (low - 1)) == 0)
726 || (low == 0 && (high & (high - 1)) == 0));
729 /* Return the power of two represented by a tree node known to be a
730 power of two. */
733 tree_log2 (expr)
734 tree expr;
736 int prec;
737 HOST_WIDE_INT high, low;
739 STRIP_NOPS (expr);
741 if (TREE_CODE (expr) == COMPLEX_CST)
742 return tree_log2 (TREE_REALPART (expr));
744 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
745 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
747 high = TREE_INT_CST_HIGH (expr);
748 low = TREE_INT_CST_LOW (expr);
750 /* First clear all bits that are beyond the type's precision in case
751 we've been sign extended. */
753 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
755 else if (prec > HOST_BITS_PER_WIDE_INT)
756 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
757 else
759 high = 0;
760 if (prec < HOST_BITS_PER_WIDE_INT)
761 low &= ~((HOST_WIDE_INT) (-1) << prec);
764 return (high != 0 ? HOST_BITS_PER_WIDE_INT + exact_log2 (high)
765 : exact_log2 (low));
768 /* Similar, but return the largest integer Y such that 2 ** Y is less
769 than or equal to EXPR. */
772 tree_floor_log2 (expr)
773 tree expr;
775 int prec;
776 HOST_WIDE_INT high, low;
778 STRIP_NOPS (expr);
780 if (TREE_CODE (expr) == COMPLEX_CST)
781 return tree_log2 (TREE_REALPART (expr));
783 prec = (POINTER_TYPE_P (TREE_TYPE (expr))
784 ? POINTER_SIZE : TYPE_PRECISION (TREE_TYPE (expr)));
786 high = TREE_INT_CST_HIGH (expr);
787 low = TREE_INT_CST_LOW (expr);
789 /* First clear all bits that are beyond the type's precision in case
790 we've been sign extended. Ignore if type's precision hasn't been set
791 since what we are doing is setting it. */
793 if (prec == 2 * HOST_BITS_PER_WIDE_INT || prec == 0)
795 else if (prec > HOST_BITS_PER_WIDE_INT)
796 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
797 else
799 high = 0;
800 if (prec < HOST_BITS_PER_WIDE_INT)
801 low &= ~((HOST_WIDE_INT) (-1) << prec);
804 return (high != 0 ? HOST_BITS_PER_WIDE_INT + floor_log2 (high)
805 : floor_log2 (low));
808 /* Return 1 if EXPR is the real constant zero. */
811 real_zerop (expr)
812 tree expr;
814 STRIP_NOPS (expr);
816 return ((TREE_CODE (expr) == REAL_CST
817 && ! TREE_CONSTANT_OVERFLOW (expr)
818 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst0))
819 || (TREE_CODE (expr) == COMPLEX_CST
820 && real_zerop (TREE_REALPART (expr))
821 && real_zerop (TREE_IMAGPART (expr))));
824 /* Return 1 if EXPR is the real constant one in real or complex form. */
827 real_onep (expr)
828 tree expr;
830 STRIP_NOPS (expr);
832 return ((TREE_CODE (expr) == REAL_CST
833 && ! TREE_CONSTANT_OVERFLOW (expr)
834 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst1))
835 || (TREE_CODE (expr) == COMPLEX_CST
836 && real_onep (TREE_REALPART (expr))
837 && real_zerop (TREE_IMAGPART (expr))));
840 /* Return 1 if EXPR is the real constant two. */
843 real_twop (expr)
844 tree expr;
846 STRIP_NOPS (expr);
848 return ((TREE_CODE (expr) == REAL_CST
849 && ! TREE_CONSTANT_OVERFLOW (expr)
850 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconst2))
851 || (TREE_CODE (expr) == COMPLEX_CST
852 && real_twop (TREE_REALPART (expr))
853 && real_zerop (TREE_IMAGPART (expr))));
856 /* Return 1 if EXPR is the real constant minus one. */
859 real_minus_onep (expr)
860 tree expr;
862 STRIP_NOPS (expr);
864 return ((TREE_CODE (expr) == REAL_CST
865 && ! TREE_CONSTANT_OVERFLOW (expr)
866 && REAL_VALUES_EQUAL (TREE_REAL_CST (expr), dconstm1))
867 || (TREE_CODE (expr) == COMPLEX_CST
868 && real_minus_onep (TREE_REALPART (expr))
869 && real_zerop (TREE_IMAGPART (expr))));
872 /* Nonzero if EXP is a constant or a cast of a constant. */
875 really_constant_p (exp)
876 tree exp;
878 /* This is not quite the same as STRIP_NOPS. It does more. */
879 while (TREE_CODE (exp) == NOP_EXPR
880 || TREE_CODE (exp) == CONVERT_EXPR
881 || TREE_CODE (exp) == NON_LVALUE_EXPR)
882 exp = TREE_OPERAND (exp, 0);
883 return TREE_CONSTANT (exp);
886 /* Return first list element whose TREE_VALUE is ELEM.
887 Return 0 if ELEM is not in LIST. */
889 tree
890 value_member (elem, list)
891 tree elem, list;
893 while (list)
895 if (elem == TREE_VALUE (list))
896 return list;
897 list = TREE_CHAIN (list);
899 return NULL_TREE;
902 /* Return first list element whose TREE_PURPOSE is ELEM.
903 Return 0 if ELEM is not in LIST. */
905 tree
906 purpose_member (elem, list)
907 tree elem, list;
909 while (list)
911 if (elem == TREE_PURPOSE (list))
912 return list;
913 list = TREE_CHAIN (list);
915 return NULL_TREE;
918 /* Return first list element whose BINFO_TYPE is ELEM.
919 Return 0 if ELEM is not in LIST. */
921 tree
922 binfo_member (elem, list)
923 tree elem, list;
925 while (list)
927 if (elem == BINFO_TYPE (list))
928 return list;
929 list = TREE_CHAIN (list);
931 return NULL_TREE;
934 /* Return nonzero if ELEM is part of the chain CHAIN. */
937 chain_member (elem, chain)
938 tree elem, chain;
940 while (chain)
942 if (elem == chain)
943 return 1;
944 chain = TREE_CHAIN (chain);
947 return 0;
950 /* Return nonzero if ELEM is equal to TREE_VALUE (CHAIN) for any piece of
951 chain CHAIN. This and the next function are currently unused, but
952 are retained for completeness. */
955 chain_member_value (elem, chain)
956 tree elem, chain;
958 while (chain)
960 if (elem == TREE_VALUE (chain))
961 return 1;
962 chain = TREE_CHAIN (chain);
965 return 0;
968 /* Return nonzero if ELEM is equal to TREE_PURPOSE (CHAIN)
969 for any piece of chain CHAIN. */
972 chain_member_purpose (elem, chain)
973 tree elem, chain;
975 while (chain)
977 if (elem == TREE_PURPOSE (chain))
978 return 1;
979 chain = TREE_CHAIN (chain);
982 return 0;
985 /* Return the length of a chain of nodes chained through TREE_CHAIN.
986 We expect a null pointer to mark the end of the chain.
987 This is the Lisp primitive `length'. */
990 list_length (t)
991 tree t;
993 tree tail;
994 int len = 0;
996 for (tail = t; tail; tail = TREE_CHAIN (tail))
997 len++;
999 return len;
1002 /* Returns the number of FIELD_DECLs in TYPE. */
1005 fields_length (type)
1006 tree type;
1008 tree t = TYPE_FIELDS (type);
1009 int count = 0;
1011 for (; t; t = TREE_CHAIN (t))
1012 if (TREE_CODE (t) == FIELD_DECL)
1013 ++count;
1015 return count;
1018 /* Concatenate two chains of nodes (chained through TREE_CHAIN)
1019 by modifying the last node in chain 1 to point to chain 2.
1020 This is the Lisp primitive `nconc'. */
1022 tree
1023 chainon (op1, op2)
1024 tree op1, op2;
1027 if (op1)
1029 tree t1;
1030 #ifdef ENABLE_TREE_CHECKING
1031 tree t2;
1032 #endif
1034 for (t1 = op1; TREE_CHAIN (t1); t1 = TREE_CHAIN (t1))
1036 TREE_CHAIN (t1) = op2;
1037 #ifdef ENABLE_TREE_CHECKING
1038 for (t2 = op2; t2; t2 = TREE_CHAIN (t2))
1039 if (t2 == t1)
1040 abort (); /* Circularity created. */
1041 #endif
1042 return op1;
1044 else
1045 return op2;
1048 /* Return the last node in a chain of nodes (chained through TREE_CHAIN). */
1050 tree
1051 tree_last (chain)
1052 tree chain;
1054 tree next;
1055 if (chain)
1056 while ((next = TREE_CHAIN (chain)))
1057 chain = next;
1058 return chain;
1061 /* Reverse the order of elements in the chain T,
1062 and return the new head of the chain (old last element). */
1064 tree
1065 nreverse (t)
1066 tree t;
1068 tree prev = 0, decl, next;
1069 for (decl = t; decl; decl = next)
1071 next = TREE_CHAIN (decl);
1072 TREE_CHAIN (decl) = prev;
1073 prev = decl;
1075 return prev;
1078 /* Given a chain CHAIN of tree nodes,
1079 construct and return a list of those nodes. */
1081 tree
1082 listify (chain)
1083 tree chain;
1085 tree result = NULL_TREE;
1086 tree in_tail = chain;
1087 tree out_tail = NULL_TREE;
1089 while (in_tail)
1091 tree next = tree_cons (NULL_TREE, in_tail, NULL_TREE);
1092 if (out_tail)
1093 TREE_CHAIN (out_tail) = next;
1094 else
1095 result = next;
1096 out_tail = next;
1097 in_tail = TREE_CHAIN (in_tail);
1100 return result;
1103 /* Return a newly created TREE_LIST node whose
1104 purpose and value fields are PARM and VALUE. */
1106 tree
1107 build_tree_list (parm, value)
1108 tree parm, value;
1110 tree t = make_node (TREE_LIST);
1111 TREE_PURPOSE (t) = parm;
1112 TREE_VALUE (t) = value;
1113 return t;
1116 /* Return a newly created TREE_LIST node whose
1117 purpose and value fields are PARM and VALUE
1118 and whose TREE_CHAIN is CHAIN. */
1120 tree
1121 tree_cons (purpose, value, chain)
1122 tree purpose, value, chain;
1124 tree node;
1126 node = ggc_alloc_tree (sizeof (struct tree_list));
1128 memset (node, 0, sizeof (struct tree_common));
1130 #ifdef GATHER_STATISTICS
1131 tree_node_counts[(int) x_kind]++;
1132 tree_node_sizes[(int) x_kind] += sizeof (struct tree_list);
1133 #endif
1135 TREE_SET_CODE (node, TREE_LIST);
1136 TREE_CHAIN (node) = chain;
1137 TREE_PURPOSE (node) = purpose;
1138 TREE_VALUE (node) = value;
1139 return node;
1143 /* Return the size nominally occupied by an object of type TYPE
1144 when it resides in memory. The value is measured in units of bytes,
1145 and its data type is that normally used for type sizes
1146 (which is the first type created by make_signed_type or
1147 make_unsigned_type). */
1149 tree
1150 size_in_bytes (type)
1151 tree type;
1153 tree t;
1155 if (type == error_mark_node)
1156 return integer_zero_node;
1158 type = TYPE_MAIN_VARIANT (type);
1159 t = TYPE_SIZE_UNIT (type);
1161 if (t == 0)
1163 (*lang_hooks.types.incomplete_type_error) (NULL_TREE, type);
1164 return size_zero_node;
1167 if (TREE_CODE (t) == INTEGER_CST)
1168 force_fit_type (t, 0);
1170 return t;
1173 /* Return the size of TYPE (in bytes) as a wide integer
1174 or return -1 if the size can vary or is larger than an integer. */
1176 HOST_WIDE_INT
1177 int_size_in_bytes (type)
1178 tree type;
1180 tree t;
1182 if (type == error_mark_node)
1183 return 0;
1185 type = TYPE_MAIN_VARIANT (type);
1186 t = TYPE_SIZE_UNIT (type);
1187 if (t == 0
1188 || TREE_CODE (t) != INTEGER_CST
1189 || TREE_OVERFLOW (t)
1190 || TREE_INT_CST_HIGH (t) != 0
1191 /* If the result would appear negative, it's too big to represent. */
1192 || (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0)
1193 return -1;
1195 return TREE_INT_CST_LOW (t);
1198 /* Return the bit position of FIELD, in bits from the start of the record.
1199 This is a tree of type bitsizetype. */
1201 tree
1202 bit_position (field)
1203 tree field;
1206 return bit_from_pos (DECL_FIELD_OFFSET (field),
1207 DECL_FIELD_BIT_OFFSET (field));
1210 /* Likewise, but return as an integer. Abort if it cannot be represented
1211 in that way (since it could be a signed value, we don't have the option
1212 of returning -1 like int_size_in_byte can. */
1214 HOST_WIDE_INT
1215 int_bit_position (field)
1216 tree field;
1218 return tree_low_cst (bit_position (field), 0);
1221 /* Return the byte position of FIELD, in bytes from the start of the record.
1222 This is a tree of type sizetype. */
1224 tree
1225 byte_position (field)
1226 tree field;
1228 return byte_from_pos (DECL_FIELD_OFFSET (field),
1229 DECL_FIELD_BIT_OFFSET (field));
1232 /* Likewise, but return as an integer. Abort if it cannot be represented
1233 in that way (since it could be a signed value, we don't have the option
1234 of returning -1 like int_size_in_byte can. */
1236 HOST_WIDE_INT
1237 int_byte_position (field)
1238 tree field;
1240 return tree_low_cst (byte_position (field), 0);
1243 /* Return the strictest alignment, in bits, that T is known to have. */
1245 unsigned int
1246 expr_align (t)
1247 tree t;
1249 unsigned int align0, align1;
1251 switch (TREE_CODE (t))
1253 case NOP_EXPR: case CONVERT_EXPR: case NON_LVALUE_EXPR:
1254 /* If we have conversions, we know that the alignment of the
1255 object must meet each of the alignments of the types. */
1256 align0 = expr_align (TREE_OPERAND (t, 0));
1257 align1 = TYPE_ALIGN (TREE_TYPE (t));
1258 return MAX (align0, align1);
1260 case SAVE_EXPR: case COMPOUND_EXPR: case MODIFY_EXPR:
1261 case INIT_EXPR: case TARGET_EXPR: case WITH_CLEANUP_EXPR:
1262 case WITH_RECORD_EXPR: case CLEANUP_POINT_EXPR: case UNSAVE_EXPR:
1263 /* These don't change the alignment of an object. */
1264 return expr_align (TREE_OPERAND (t, 0));
1266 case COND_EXPR:
1267 /* The best we can do is say that the alignment is the least aligned
1268 of the two arms. */
1269 align0 = expr_align (TREE_OPERAND (t, 1));
1270 align1 = expr_align (TREE_OPERAND (t, 2));
1271 return MIN (align0, align1);
1273 case LABEL_DECL: case CONST_DECL:
1274 case VAR_DECL: case PARM_DECL: case RESULT_DECL:
1275 if (DECL_ALIGN (t) != 0)
1276 return DECL_ALIGN (t);
1277 break;
1279 case FUNCTION_DECL:
1280 return FUNCTION_BOUNDARY;
1282 default:
1283 break;
1286 /* Otherwise take the alignment from that of the type. */
1287 return TYPE_ALIGN (TREE_TYPE (t));
1290 /* Return, as a tree node, the number of elements for TYPE (which is an
1291 ARRAY_TYPE) minus one. This counts only elements of the top array. */
1293 tree
1294 array_type_nelts (type)
1295 tree type;
1297 tree index_type, min, max;
1299 /* If they did it with unspecified bounds, then we should have already
1300 given an error about it before we got here. */
1301 if (! TYPE_DOMAIN (type))
1302 return error_mark_node;
1304 index_type = TYPE_DOMAIN (type);
1305 min = TYPE_MIN_VALUE (index_type);
1306 max = TYPE_MAX_VALUE (index_type);
1308 return (integer_zerop (min)
1309 ? max
1310 : fold (build (MINUS_EXPR, TREE_TYPE (max), max, min)));
1313 /* Return nonzero if arg is static -- a reference to an object in
1314 static storage. This is not the same as the C meaning of `static'. */
1317 staticp (arg)
1318 tree arg;
1320 switch (TREE_CODE (arg))
1322 case FUNCTION_DECL:
1323 /* Nested functions aren't static, since taking their address
1324 involves a trampoline. */
1325 return ((decl_function_context (arg) == 0 || DECL_NO_STATIC_CHAIN (arg))
1326 && ! DECL_NON_ADDR_CONST_P (arg));
1328 case VAR_DECL:
1329 return ((TREE_STATIC (arg) || DECL_EXTERNAL (arg))
1330 && ! DECL_THREAD_LOCAL (arg)
1331 && ! DECL_NON_ADDR_CONST_P (arg));
1333 case CONSTRUCTOR:
1334 return TREE_STATIC (arg);
1336 case LABEL_DECL:
1337 case STRING_CST:
1338 return 1;
1340 /* If we are referencing a bitfield, we can't evaluate an
1341 ADDR_EXPR at compile time and so it isn't a constant. */
1342 case COMPONENT_REF:
1343 return (! DECL_BIT_FIELD (TREE_OPERAND (arg, 1))
1344 && staticp (TREE_OPERAND (arg, 0)));
1346 case BIT_FIELD_REF:
1347 return 0;
1349 #if 0
1350 /* This case is technically correct, but results in setting
1351 TREE_CONSTANT on ADDR_EXPRs that cannot be evaluated at
1352 compile time. */
1353 case INDIRECT_REF:
1354 return TREE_CONSTANT (TREE_OPERAND (arg, 0));
1355 #endif
1357 case ARRAY_REF:
1358 case ARRAY_RANGE_REF:
1359 if (TREE_CODE (TYPE_SIZE (TREE_TYPE (arg))) == INTEGER_CST
1360 && TREE_CODE (TREE_OPERAND (arg, 1)) == INTEGER_CST)
1361 return staticp (TREE_OPERAND (arg, 0));
1363 default:
1364 if ((unsigned int) TREE_CODE (arg)
1365 >= (unsigned int) LAST_AND_UNUSED_TREE_CODE)
1366 return (*lang_hooks.staticp) (arg);
1367 else
1368 return 0;
1372 /* Wrap a SAVE_EXPR around EXPR, if appropriate.
1373 Do this to any expression which may be used in more than one place,
1374 but must be evaluated only once.
1376 Normally, expand_expr would reevaluate the expression each time.
1377 Calling save_expr produces something that is evaluated and recorded
1378 the first time expand_expr is called on it. Subsequent calls to
1379 expand_expr just reuse the recorded value.
1381 The call to expand_expr that generates code that actually computes
1382 the value is the first call *at compile time*. Subsequent calls
1383 *at compile time* generate code to use the saved value.
1384 This produces correct result provided that *at run time* control
1385 always flows through the insns made by the first expand_expr
1386 before reaching the other places where the save_expr was evaluated.
1387 You, the caller of save_expr, must make sure this is so.
1389 Constants, and certain read-only nodes, are returned with no
1390 SAVE_EXPR because that is safe. Expressions containing placeholders
1391 are not touched; see tree.def for an explanation of what these
1392 are used for. */
1394 tree
1395 save_expr (expr)
1396 tree expr;
1398 tree t = fold (expr);
1399 tree inner;
1401 /* We don't care about whether this can be used as an lvalue in this
1402 context. */
1403 while (TREE_CODE (t) == NON_LVALUE_EXPR)
1404 t = TREE_OPERAND (t, 0);
1406 /* If we have simple operations applied to a SAVE_EXPR or to a SAVE_EXPR and
1407 a constant, it will be more efficient to not make another SAVE_EXPR since
1408 it will allow better simplification and GCSE will be able to merge the
1409 computations if they actualy occur. */
1410 for (inner = t;
1411 (TREE_CODE_CLASS (TREE_CODE (inner)) == '1'
1412 || (TREE_CODE_CLASS (TREE_CODE (inner)) == '2'
1413 && TREE_CONSTANT (TREE_OPERAND (inner, 1))));
1414 inner = TREE_OPERAND (inner, 0))
1417 /* If the tree evaluates to a constant, then we don't want to hide that
1418 fact (i.e. this allows further folding, and direct checks for constants).
1419 However, a read-only object that has side effects cannot be bypassed.
1420 Since it is no problem to reevaluate literals, we just return the
1421 literal node. */
1422 if (TREE_CONSTANT (inner)
1423 || (TREE_READONLY (inner) && ! TREE_SIDE_EFFECTS (inner))
1424 || TREE_CODE (inner) == SAVE_EXPR || TREE_CODE (inner) == ERROR_MARK)
1425 return t;
1427 /* If T contains a PLACEHOLDER_EXPR, we must evaluate it each time, since
1428 it means that the size or offset of some field of an object depends on
1429 the value within another field.
1431 Note that it must not be the case that T contains both a PLACEHOLDER_EXPR
1432 and some variable since it would then need to be both evaluated once and
1433 evaluated more than once. Front-ends must assure this case cannot
1434 happen by surrounding any such subexpressions in their own SAVE_EXPR
1435 and forcing evaluation at the proper time. */
1436 if (contains_placeholder_p (t))
1437 return t;
1439 t = build (SAVE_EXPR, TREE_TYPE (expr), t, current_function_decl, NULL_TREE);
1441 /* This expression might be placed ahead of a jump to ensure that the
1442 value was computed on both sides of the jump. So make sure it isn't
1443 eliminated as dead. */
1444 TREE_SIDE_EFFECTS (t) = 1;
1445 TREE_READONLY (t) = 1;
1446 return t;
1449 /* Arrange for an expression to be expanded multiple independent
1450 times. This is useful for cleanup actions, as the backend can
1451 expand them multiple times in different places. */
1453 tree
1454 unsave_expr (expr)
1455 tree expr;
1457 tree t;
1459 /* If this is already protected, no sense in protecting it again. */
1460 if (TREE_CODE (expr) == UNSAVE_EXPR)
1461 return expr;
1463 t = build1 (UNSAVE_EXPR, TREE_TYPE (expr), expr);
1464 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (expr);
1465 return t;
1468 /* Returns the index of the first non-tree operand for CODE, or the number
1469 of operands if all are trees. */
1472 first_rtl_op (code)
1473 enum tree_code code;
1475 switch (code)
1477 case SAVE_EXPR:
1478 return 2;
1479 case GOTO_SUBROUTINE_EXPR:
1480 case RTL_EXPR:
1481 return 0;
1482 case WITH_CLEANUP_EXPR:
1483 return 2;
1484 case METHOD_CALL_EXPR:
1485 return 3;
1486 default:
1487 return TREE_CODE_LENGTH (code);
1491 /* Return which tree structure is used by T. */
1493 enum tree_node_structure_enum
1494 tree_node_structure (t)
1495 tree t;
1497 enum tree_code code = TREE_CODE (t);
1499 switch (TREE_CODE_CLASS (code))
1501 case 'd': return TS_DECL;
1502 case 't': return TS_TYPE;
1503 case 'b': return TS_BLOCK;
1504 case 'r': case '<': case '1': case '2': case 'e': case 's':
1505 return TS_EXP;
1506 default: /* 'c' and 'x' */
1507 break;
1509 switch (code)
1511 /* 'c' cases. */
1512 case INTEGER_CST: return TS_INT_CST;
1513 case REAL_CST: return TS_REAL_CST;
1514 case COMPLEX_CST: return TS_COMPLEX;
1515 case VECTOR_CST: return TS_VECTOR;
1516 case STRING_CST: return TS_STRING;
1517 /* 'x' cases. */
1518 case ERROR_MARK: return TS_COMMON;
1519 case IDENTIFIER_NODE: return TS_IDENTIFIER;
1520 case TREE_LIST: return TS_LIST;
1521 case TREE_VEC: return TS_VEC;
1522 case PLACEHOLDER_EXPR: return TS_COMMON;
1524 default:
1525 abort ();
1529 /* Perform any modifications to EXPR required when it is unsaved. Does
1530 not recurse into EXPR's subtrees. */
1532 void
1533 unsave_expr_1 (expr)
1534 tree expr;
1536 switch (TREE_CODE (expr))
1538 case SAVE_EXPR:
1539 if (! SAVE_EXPR_PERSISTENT_P (expr))
1540 SAVE_EXPR_RTL (expr) = 0;
1541 break;
1543 case TARGET_EXPR:
1544 /* Don't mess with a TARGET_EXPR that hasn't been expanded.
1545 It's OK for this to happen if it was part of a subtree that
1546 isn't immediately expanded, such as operand 2 of another
1547 TARGET_EXPR. */
1548 if (TREE_OPERAND (expr, 1))
1549 break;
1551 TREE_OPERAND (expr, 1) = TREE_OPERAND (expr, 3);
1552 TREE_OPERAND (expr, 3) = NULL_TREE;
1553 break;
1555 case RTL_EXPR:
1556 /* I don't yet know how to emit a sequence multiple times. */
1557 if (RTL_EXPR_SEQUENCE (expr) != 0)
1558 abort ();
1559 break;
1561 default:
1562 break;
1566 /* Default lang hook for "unsave_expr_now". */
1568 tree
1569 lhd_unsave_expr_now (expr)
1570 tree expr;
1572 enum tree_code code;
1574 /* There's nothing to do for NULL_TREE. */
1575 if (expr == 0)
1576 return expr;
1578 unsave_expr_1 (expr);
1580 code = TREE_CODE (expr);
1581 switch (TREE_CODE_CLASS (code))
1583 case 'c': /* a constant */
1584 case 't': /* a type node */
1585 case 'd': /* A decl node */
1586 case 'b': /* A block node */
1587 break;
1589 case 'x': /* miscellaneous: e.g., identifier, TREE_LIST or ERROR_MARK. */
1590 if (code == TREE_LIST)
1592 lhd_unsave_expr_now (TREE_VALUE (expr));
1593 lhd_unsave_expr_now (TREE_CHAIN (expr));
1595 break;
1597 case 'e': /* an expression */
1598 case 'r': /* a reference */
1599 case 's': /* an expression with side effects */
1600 case '<': /* a comparison expression */
1601 case '2': /* a binary arithmetic expression */
1602 case '1': /* a unary arithmetic expression */
1604 int i;
1606 for (i = first_rtl_op (code) - 1; i >= 0; i--)
1607 lhd_unsave_expr_now (TREE_OPERAND (expr, i));
1609 break;
1611 default:
1612 abort ();
1615 return expr;
1618 /* Return 0 if it is safe to evaluate EXPR multiple times,
1619 return 1 if it is safe if EXPR is unsaved afterward, or
1620 return 2 if it is completely unsafe.
1622 This assumes that CALL_EXPRs and TARGET_EXPRs are never replicated in
1623 an expression tree, so that it safe to unsave them and the surrounding
1624 context will be correct.
1626 SAVE_EXPRs basically *only* appear replicated in an expression tree,
1627 occasionally across the whole of a function. It is therefore only
1628 safe to unsave a SAVE_EXPR if you know that all occurrences appear
1629 below the UNSAVE_EXPR.
1631 RTL_EXPRs consume their rtl during evaluation. It is therefore
1632 never possible to unsave them. */
1635 unsafe_for_reeval (expr)
1636 tree expr;
1638 int unsafeness = 0;
1639 enum tree_code code;
1640 int i, tmp;
1641 tree exp;
1642 int first_rtl;
1644 if (expr == NULL_TREE)
1645 return 1;
1647 code = TREE_CODE (expr);
1648 first_rtl = first_rtl_op (code);
1650 switch (code)
1652 case SAVE_EXPR:
1653 case RTL_EXPR:
1654 return 2;
1656 case TREE_LIST:
1657 for (exp = expr; exp != 0; exp = TREE_CHAIN (exp))
1659 tmp = unsafe_for_reeval (TREE_VALUE (exp));
1660 unsafeness = MAX (tmp, unsafeness);
1663 return unsafeness;
1665 case CALL_EXPR:
1666 tmp = unsafe_for_reeval (TREE_OPERAND (expr, 1));
1667 return MAX (tmp, 1);
1669 case TARGET_EXPR:
1670 unsafeness = 1;
1671 break;
1673 default:
1674 tmp = (*lang_hooks.unsafe_for_reeval) (expr);
1675 if (tmp >= 0)
1676 return tmp;
1677 break;
1680 switch (TREE_CODE_CLASS (code))
1682 case 'c': /* a constant */
1683 case 't': /* a type node */
1684 case 'x': /* something random, like an identifier or an ERROR_MARK. */
1685 case 'd': /* A decl node */
1686 case 'b': /* A block node */
1687 return 0;
1689 case 'e': /* an expression */
1690 case 'r': /* a reference */
1691 case 's': /* an expression with side effects */
1692 case '<': /* a comparison expression */
1693 case '2': /* a binary arithmetic expression */
1694 case '1': /* a unary arithmetic expression */
1695 for (i = first_rtl - 1; i >= 0; i--)
1697 tmp = unsafe_for_reeval (TREE_OPERAND (expr, i));
1698 unsafeness = MAX (tmp, unsafeness);
1701 return unsafeness;
1703 default:
1704 return 2;
1708 /* Return 1 if EXP contains a PLACEHOLDER_EXPR; i.e., if it represents a size
1709 or offset that depends on a field within a record. */
1712 contains_placeholder_p (exp)
1713 tree exp;
1715 enum tree_code code;
1716 int result;
1718 if (!exp)
1719 return 0;
1721 /* If we have a WITH_RECORD_EXPR, it "cancels" any PLACEHOLDER_EXPR
1722 in it since it is supplying a value for it. */
1723 code = TREE_CODE (exp);
1724 if (code == WITH_RECORD_EXPR)
1725 return 0;
1726 else if (code == PLACEHOLDER_EXPR)
1727 return 1;
1729 switch (TREE_CODE_CLASS (code))
1731 case 'r':
1732 /* Don't look at any PLACEHOLDER_EXPRs that might be in index or bit
1733 position computations since they will be converted into a
1734 WITH_RECORD_EXPR involving the reference, which will assume
1735 here will be valid. */
1736 return contains_placeholder_p (TREE_OPERAND (exp, 0));
1738 case 'x':
1739 if (code == TREE_LIST)
1740 return (contains_placeholder_p (TREE_VALUE (exp))
1741 || (TREE_CHAIN (exp) != 0
1742 && contains_placeholder_p (TREE_CHAIN (exp))));
1743 break;
1745 case '1':
1746 case '2': case '<':
1747 case 'e':
1748 switch (code)
1750 case COMPOUND_EXPR:
1751 /* Ignoring the first operand isn't quite right, but works best. */
1752 return contains_placeholder_p (TREE_OPERAND (exp, 1));
1754 case RTL_EXPR:
1755 case CONSTRUCTOR:
1756 return 0;
1758 case COND_EXPR:
1759 return (contains_placeholder_p (TREE_OPERAND (exp, 0))
1760 || contains_placeholder_p (TREE_OPERAND (exp, 1))
1761 || contains_placeholder_p (TREE_OPERAND (exp, 2)));
1763 case SAVE_EXPR:
1764 /* If we already know this doesn't have a placeholder, don't
1765 check again. */
1766 if (SAVE_EXPR_NOPLACEHOLDER (exp) || SAVE_EXPR_RTL (exp) != 0)
1767 return 0;
1769 SAVE_EXPR_NOPLACEHOLDER (exp) = 1;
1770 result = contains_placeholder_p (TREE_OPERAND (exp, 0));
1771 if (result)
1772 SAVE_EXPR_NOPLACEHOLDER (exp) = 0;
1774 return result;
1776 case CALL_EXPR:
1777 return (TREE_OPERAND (exp, 1) != 0
1778 && contains_placeholder_p (TREE_OPERAND (exp, 1)));
1780 default:
1781 break;
1784 switch (TREE_CODE_LENGTH (code))
1786 case 1:
1787 return contains_placeholder_p (TREE_OPERAND (exp, 0));
1788 case 2:
1789 return (contains_placeholder_p (TREE_OPERAND (exp, 0))
1790 || contains_placeholder_p (TREE_OPERAND (exp, 1)));
1791 default:
1792 return 0;
1795 default:
1796 return 0;
1798 return 0;
1801 /* Return 1 if EXP contains any expressions that produce cleanups for an
1802 outer scope to deal with. Used by fold. */
1805 has_cleanups (exp)
1806 tree exp;
1808 int i, nops, cmp;
1810 if (! TREE_SIDE_EFFECTS (exp))
1811 return 0;
1813 switch (TREE_CODE (exp))
1815 case TARGET_EXPR:
1816 case GOTO_SUBROUTINE_EXPR:
1817 case WITH_CLEANUP_EXPR:
1818 return 1;
1820 case CLEANUP_POINT_EXPR:
1821 return 0;
1823 case CALL_EXPR:
1824 for (exp = TREE_OPERAND (exp, 1); exp; exp = TREE_CHAIN (exp))
1826 cmp = has_cleanups (TREE_VALUE (exp));
1827 if (cmp)
1828 return cmp;
1830 return 0;
1832 default:
1833 break;
1836 /* This general rule works for most tree codes. All exceptions should be
1837 handled above. If this is a language-specific tree code, we can't
1838 trust what might be in the operand, so say we don't know
1839 the situation. */
1840 if ((int) TREE_CODE (exp) >= (int) LAST_AND_UNUSED_TREE_CODE)
1841 return -1;
1843 nops = first_rtl_op (TREE_CODE (exp));
1844 for (i = 0; i < nops; i++)
1845 if (TREE_OPERAND (exp, i) != 0)
1847 int type = TREE_CODE_CLASS (TREE_CODE (TREE_OPERAND (exp, i)));
1848 if (type == 'e' || type == '<' || type == '1' || type == '2'
1849 || type == 'r' || type == 's')
1851 cmp = has_cleanups (TREE_OPERAND (exp, i));
1852 if (cmp)
1853 return cmp;
1857 return 0;
1860 /* Given a tree EXP, a FIELD_DECL F, and a replacement value R,
1861 return a tree with all occurrences of references to F in a
1862 PLACEHOLDER_EXPR replaced by R. Note that we assume here that EXP
1863 contains only arithmetic expressions or a CALL_EXPR with a
1864 PLACEHOLDER_EXPR occurring only in its arglist. */
1866 tree
1867 substitute_in_expr (exp, f, r)
1868 tree exp;
1869 tree f;
1870 tree r;
1872 enum tree_code code = TREE_CODE (exp);
1873 tree op0, op1, op2;
1874 tree new;
1875 tree inner;
1877 switch (TREE_CODE_CLASS (code))
1879 case 'c':
1880 case 'd':
1881 return exp;
1883 case 'x':
1884 if (code == PLACEHOLDER_EXPR)
1885 return exp;
1886 else if (code == TREE_LIST)
1888 op0 = (TREE_CHAIN (exp) == 0
1889 ? 0 : substitute_in_expr (TREE_CHAIN (exp), f, r));
1890 op1 = substitute_in_expr (TREE_VALUE (exp), f, r);
1891 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
1892 return exp;
1894 return tree_cons (TREE_PURPOSE (exp), op1, op0);
1897 abort ();
1899 case '1':
1900 case '2':
1901 case '<':
1902 case 'e':
1903 switch (TREE_CODE_LENGTH (code))
1905 case 1:
1906 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
1907 if (op0 == TREE_OPERAND (exp, 0))
1908 return exp;
1910 if (code == NON_LVALUE_EXPR)
1911 return op0;
1913 new = fold (build1 (code, TREE_TYPE (exp), op0));
1914 break;
1916 case 2:
1917 /* An RTL_EXPR cannot contain a PLACEHOLDER_EXPR; a CONSTRUCTOR
1918 could, but we don't support it. */
1919 if (code == RTL_EXPR)
1920 return exp;
1921 else if (code == CONSTRUCTOR)
1922 abort ();
1924 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
1925 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
1926 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
1927 return exp;
1929 new = fold (build (code, TREE_TYPE (exp), op0, op1));
1930 break;
1932 case 3:
1933 /* It cannot be that anything inside a SAVE_EXPR contains a
1934 PLACEHOLDER_EXPR. */
1935 if (code == SAVE_EXPR)
1936 return exp;
1938 else if (code == CALL_EXPR)
1940 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
1941 if (op1 == TREE_OPERAND (exp, 1))
1942 return exp;
1944 return build (code, TREE_TYPE (exp),
1945 TREE_OPERAND (exp, 0), op1, NULL_TREE);
1948 else if (code != COND_EXPR)
1949 abort ();
1951 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
1952 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
1953 op2 = substitute_in_expr (TREE_OPERAND (exp, 2), f, r);
1954 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
1955 && op2 == TREE_OPERAND (exp, 2))
1956 return exp;
1958 new = fold (build (code, TREE_TYPE (exp), op0, op1, op2));
1959 break;
1961 default:
1962 abort ();
1965 break;
1967 case 'r':
1968 switch (code)
1970 case COMPONENT_REF:
1971 /* If this expression is getting a value from a PLACEHOLDER_EXPR
1972 and it is the right field, replace it with R. */
1973 for (inner = TREE_OPERAND (exp, 0);
1974 TREE_CODE_CLASS (TREE_CODE (inner)) == 'r';
1975 inner = TREE_OPERAND (inner, 0))
1977 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
1978 && TREE_OPERAND (exp, 1) == f)
1979 return r;
1981 /* If this expression hasn't been completed let, leave it
1982 alone. */
1983 if (TREE_CODE (inner) == PLACEHOLDER_EXPR
1984 && TREE_TYPE (inner) == 0)
1985 return exp;
1987 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
1988 if (op0 == TREE_OPERAND (exp, 0))
1989 return exp;
1991 new = fold (build (code, TREE_TYPE (exp), op0,
1992 TREE_OPERAND (exp, 1)));
1993 break;
1995 case BIT_FIELD_REF:
1996 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
1997 op1 = substitute_in_expr (TREE_OPERAND (exp, 1), f, r);
1998 op2 = substitute_in_expr (TREE_OPERAND (exp, 2), f, r);
1999 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
2000 && op2 == TREE_OPERAND (exp, 2))
2001 return exp;
2003 new = fold (build (code, TREE_TYPE (exp), op0, op1, op2));
2004 break;
2006 case INDIRECT_REF:
2007 case BUFFER_REF:
2008 op0 = substitute_in_expr (TREE_OPERAND (exp, 0), f, r);
2009 if (op0 == TREE_OPERAND (exp, 0))
2010 return exp;
2012 new = fold (build1 (code, TREE_TYPE (exp), op0));
2013 break;
2015 default:
2016 abort ();
2018 break;
2020 default:
2021 abort ();
2024 TREE_READONLY (new) = TREE_READONLY (exp);
2025 return new;
2028 /* Stabilize a reference so that we can use it any number of times
2029 without causing its operands to be evaluated more than once.
2030 Returns the stabilized reference. This works by means of save_expr,
2031 so see the caveats in the comments about save_expr.
2033 Also allows conversion expressions whose operands are references.
2034 Any other kind of expression is returned unchanged. */
2036 tree
2037 stabilize_reference (ref)
2038 tree ref;
2040 tree result;
2041 enum tree_code code = TREE_CODE (ref);
2043 switch (code)
2045 case VAR_DECL:
2046 case PARM_DECL:
2047 case RESULT_DECL:
2048 /* No action is needed in this case. */
2049 return ref;
2051 case NOP_EXPR:
2052 case CONVERT_EXPR:
2053 case FLOAT_EXPR:
2054 case FIX_TRUNC_EXPR:
2055 case FIX_FLOOR_EXPR:
2056 case FIX_ROUND_EXPR:
2057 case FIX_CEIL_EXPR:
2058 result = build_nt (code, stabilize_reference (TREE_OPERAND (ref, 0)));
2059 break;
2061 case INDIRECT_REF:
2062 result = build_nt (INDIRECT_REF,
2063 stabilize_reference_1 (TREE_OPERAND (ref, 0)));
2064 break;
2066 case COMPONENT_REF:
2067 result = build_nt (COMPONENT_REF,
2068 stabilize_reference (TREE_OPERAND (ref, 0)),
2069 TREE_OPERAND (ref, 1));
2070 break;
2072 case BIT_FIELD_REF:
2073 result = build_nt (BIT_FIELD_REF,
2074 stabilize_reference (TREE_OPERAND (ref, 0)),
2075 stabilize_reference_1 (TREE_OPERAND (ref, 1)),
2076 stabilize_reference_1 (TREE_OPERAND (ref, 2)));
2077 break;
2079 case ARRAY_REF:
2080 result = build_nt (ARRAY_REF,
2081 stabilize_reference (TREE_OPERAND (ref, 0)),
2082 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2083 break;
2085 case ARRAY_RANGE_REF:
2086 result = build_nt (ARRAY_RANGE_REF,
2087 stabilize_reference (TREE_OPERAND (ref, 0)),
2088 stabilize_reference_1 (TREE_OPERAND (ref, 1)));
2089 break;
2091 case COMPOUND_EXPR:
2092 /* We cannot wrap the first expression in a SAVE_EXPR, as then
2093 it wouldn't be ignored. This matters when dealing with
2094 volatiles. */
2095 return stabilize_reference_1 (ref);
2097 case RTL_EXPR:
2098 result = build1 (INDIRECT_REF, TREE_TYPE (ref),
2099 save_expr (build1 (ADDR_EXPR,
2100 build_pointer_type (TREE_TYPE (ref)),
2101 ref)));
2102 break;
2104 /* If arg isn't a kind of lvalue we recognize, make no change.
2105 Caller should recognize the error for an invalid lvalue. */
2106 default:
2107 return ref;
2109 case ERROR_MARK:
2110 return error_mark_node;
2113 TREE_TYPE (result) = TREE_TYPE (ref);
2114 TREE_READONLY (result) = TREE_READONLY (ref);
2115 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (ref);
2116 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (ref);
2118 return result;
2121 /* Subroutine of stabilize_reference; this is called for subtrees of
2122 references. Any expression with side-effects must be put in a SAVE_EXPR
2123 to ensure that it is only evaluated once.
2125 We don't put SAVE_EXPR nodes around everything, because assigning very
2126 simple expressions to temporaries causes us to miss good opportunities
2127 for optimizations. Among other things, the opportunity to fold in the
2128 addition of a constant into an addressing mode often gets lost, e.g.
2129 "y[i+1] += x;". In general, we take the approach that we should not make
2130 an assignment unless we are forced into it - i.e., that any non-side effect
2131 operator should be allowed, and that cse should take care of coalescing
2132 multiple utterances of the same expression should that prove fruitful. */
2134 tree
2135 stabilize_reference_1 (e)
2136 tree e;
2138 tree result;
2139 enum tree_code code = TREE_CODE (e);
2141 /* We cannot ignore const expressions because it might be a reference
2142 to a const array but whose index contains side-effects. But we can
2143 ignore things that are actual constant or that already have been
2144 handled by this function. */
2146 if (TREE_CONSTANT (e) || code == SAVE_EXPR)
2147 return e;
2149 switch (TREE_CODE_CLASS (code))
2151 case 'x':
2152 case 't':
2153 case 'd':
2154 case 'b':
2155 case '<':
2156 case 's':
2157 case 'e':
2158 case 'r':
2159 /* If the expression has side-effects, then encase it in a SAVE_EXPR
2160 so that it will only be evaluated once. */
2161 /* The reference (r) and comparison (<) classes could be handled as
2162 below, but it is generally faster to only evaluate them once. */
2163 if (TREE_SIDE_EFFECTS (e))
2164 return save_expr (e);
2165 return e;
2167 case 'c':
2168 /* Constants need no processing. In fact, we should never reach
2169 here. */
2170 return e;
2172 case '2':
2173 /* Division is slow and tends to be compiled with jumps,
2174 especially the division by powers of 2 that is often
2175 found inside of an array reference. So do it just once. */
2176 if (code == TRUNC_DIV_EXPR || code == TRUNC_MOD_EXPR
2177 || code == FLOOR_DIV_EXPR || code == FLOOR_MOD_EXPR
2178 || code == CEIL_DIV_EXPR || code == CEIL_MOD_EXPR
2179 || code == ROUND_DIV_EXPR || code == ROUND_MOD_EXPR)
2180 return save_expr (e);
2181 /* Recursively stabilize each operand. */
2182 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)),
2183 stabilize_reference_1 (TREE_OPERAND (e, 1)));
2184 break;
2186 case '1':
2187 /* Recursively stabilize each operand. */
2188 result = build_nt (code, stabilize_reference_1 (TREE_OPERAND (e, 0)));
2189 break;
2191 default:
2192 abort ();
2195 TREE_TYPE (result) = TREE_TYPE (e);
2196 TREE_READONLY (result) = TREE_READONLY (e);
2197 TREE_SIDE_EFFECTS (result) = TREE_SIDE_EFFECTS (e);
2198 TREE_THIS_VOLATILE (result) = TREE_THIS_VOLATILE (e);
2200 return result;
2203 /* Low-level constructors for expressions. */
2205 /* Build an expression of code CODE, data type TYPE,
2206 and operands as specified by the arguments ARG1 and following arguments.
2207 Expressions and reference nodes can be created this way.
2208 Constants, decls, types and misc nodes cannot be. */
2210 tree
2211 build VPARAMS ((enum tree_code code, tree tt, ...))
2213 tree t;
2214 int length;
2215 int i;
2216 int fro;
2217 int constant;
2219 VA_OPEN (p, tt);
2220 VA_FIXEDARG (p, enum tree_code, code);
2221 VA_FIXEDARG (p, tree, tt);
2223 t = make_node (code);
2224 length = TREE_CODE_LENGTH (code);
2225 TREE_TYPE (t) = tt;
2227 /* Below, we automatically set TREE_SIDE_EFFECTS and TREE_READONLY for the
2228 result based on those same flags for the arguments. But if the
2229 arguments aren't really even `tree' expressions, we shouldn't be trying
2230 to do this. */
2231 fro = first_rtl_op (code);
2233 /* Expressions without side effects may be constant if their
2234 arguments are as well. */
2235 constant = (TREE_CODE_CLASS (code) == '<'
2236 || TREE_CODE_CLASS (code) == '1'
2237 || TREE_CODE_CLASS (code) == '2'
2238 || TREE_CODE_CLASS (code) == 'c');
2240 if (length == 2)
2242 /* This is equivalent to the loop below, but faster. */
2243 tree arg0 = va_arg (p, tree);
2244 tree arg1 = va_arg (p, tree);
2246 TREE_OPERAND (t, 0) = arg0;
2247 TREE_OPERAND (t, 1) = arg1;
2248 TREE_READONLY (t) = 1;
2249 if (arg0 && fro > 0)
2251 if (TREE_SIDE_EFFECTS (arg0))
2252 TREE_SIDE_EFFECTS (t) = 1;
2253 if (!TREE_READONLY (arg0))
2254 TREE_READONLY (t) = 0;
2255 if (!TREE_CONSTANT (arg0))
2256 constant = 0;
2259 if (arg1 && fro > 1)
2261 if (TREE_SIDE_EFFECTS (arg1))
2262 TREE_SIDE_EFFECTS (t) = 1;
2263 if (!TREE_READONLY (arg1))
2264 TREE_READONLY (t) = 0;
2265 if (!TREE_CONSTANT (arg1))
2266 constant = 0;
2269 else if (length == 1)
2271 tree arg0 = va_arg (p, tree);
2273 /* The only one-operand cases we handle here are those with side-effects.
2274 Others are handled with build1. So don't bother checked if the
2275 arg has side-effects since we'll already have set it.
2277 ??? This really should use build1 too. */
2278 if (TREE_CODE_CLASS (code) != 's')
2279 abort ();
2280 TREE_OPERAND (t, 0) = arg0;
2282 else
2284 for (i = 0; i < length; i++)
2286 tree operand = va_arg (p, tree);
2288 TREE_OPERAND (t, i) = operand;
2289 if (operand && fro > i)
2291 if (TREE_SIDE_EFFECTS (operand))
2292 TREE_SIDE_EFFECTS (t) = 1;
2293 if (!TREE_CONSTANT (operand))
2294 constant = 0;
2298 VA_CLOSE (p);
2300 TREE_CONSTANT (t) = constant;
2301 return t;
2304 /* Same as above, but only builds for unary operators.
2305 Saves lions share of calls to `build'; cuts down use
2306 of varargs, which is expensive for RISC machines. */
2308 tree
2309 build1 (code, type, node)
2310 enum tree_code code;
2311 tree type;
2312 tree node;
2314 int length;
2315 #ifdef GATHER_STATISTICS
2316 tree_node_kind kind;
2317 #endif
2318 tree t;
2320 #ifdef GATHER_STATISTICS
2321 if (TREE_CODE_CLASS (code) == 'r')
2322 kind = r_kind;
2323 else
2324 kind = e_kind;
2325 #endif
2327 #ifdef ENABLE_CHECKING
2328 if (TREE_CODE_CLASS (code) == '2'
2329 || TREE_CODE_CLASS (code) == '<'
2330 || TREE_CODE_LENGTH (code) != 1)
2331 abort ();
2332 #endif /* ENABLE_CHECKING */
2334 length = sizeof (struct tree_exp);
2336 t = ggc_alloc_tree (length);
2338 memset ((PTR) t, 0, sizeof (struct tree_common));
2340 #ifdef GATHER_STATISTICS
2341 tree_node_counts[(int) kind]++;
2342 tree_node_sizes[(int) kind] += length;
2343 #endif
2345 TREE_SET_CODE (t, code);
2347 TREE_TYPE (t) = type;
2348 TREE_COMPLEXITY (t) = 0;
2349 TREE_OPERAND (t, 0) = node;
2350 if (node && first_rtl_op (code) != 0)
2352 TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (node);
2353 TREE_READONLY (t) = TREE_READONLY (node);
2356 switch (code)
2358 case INIT_EXPR:
2359 case MODIFY_EXPR:
2360 case VA_ARG_EXPR:
2361 case RTL_EXPR:
2362 case PREDECREMENT_EXPR:
2363 case PREINCREMENT_EXPR:
2364 case POSTDECREMENT_EXPR:
2365 case POSTINCREMENT_EXPR:
2366 /* All of these have side-effects, no matter what their
2367 operands are. */
2368 TREE_SIDE_EFFECTS (t) = 1;
2369 TREE_READONLY (t) = 0;
2370 break;
2372 case INDIRECT_REF:
2373 /* Whether a dereference is readonly has nothing to do with whether
2374 its operand is readonly. */
2375 TREE_READONLY (t) = 0;
2376 break;
2378 default:
2379 if (TREE_CODE_CLASS (code) == '1' && node && TREE_CONSTANT (node))
2380 TREE_CONSTANT (t) = 1;
2381 break;
2384 return t;
2387 /* Similar except don't specify the TREE_TYPE
2388 and leave the TREE_SIDE_EFFECTS as 0.
2389 It is permissible for arguments to be null,
2390 or even garbage if their values do not matter. */
2392 tree
2393 build_nt VPARAMS ((enum tree_code code, ...))
2395 tree t;
2396 int length;
2397 int i;
2399 VA_OPEN (p, code);
2400 VA_FIXEDARG (p, enum tree_code, code);
2402 t = make_node (code);
2403 length = TREE_CODE_LENGTH (code);
2405 for (i = 0; i < length; i++)
2406 TREE_OPERAND (t, i) = va_arg (p, tree);
2408 VA_CLOSE (p);
2409 return t;
2412 /* Create a DECL_... node of code CODE, name NAME and data type TYPE.
2413 We do NOT enter this node in any sort of symbol table.
2415 layout_decl is used to set up the decl's storage layout.
2416 Other slots are initialized to 0 or null pointers. */
2418 tree
2419 build_decl (code, name, type)
2420 enum tree_code code;
2421 tree name, type;
2423 tree t;
2425 t = make_node (code);
2427 /* if (type == error_mark_node)
2428 type = integer_type_node; */
2429 /* That is not done, deliberately, so that having error_mark_node
2430 as the type can suppress useless errors in the use of this variable. */
2432 DECL_NAME (t) = name;
2433 TREE_TYPE (t) = type;
2435 if (code == VAR_DECL || code == PARM_DECL || code == RESULT_DECL)
2436 layout_decl (t, 0);
2437 else if (code == FUNCTION_DECL)
2438 DECL_MODE (t) = FUNCTION_MODE;
2440 return t;
2443 /* BLOCK nodes are used to represent the structure of binding contours
2444 and declarations, once those contours have been exited and their contents
2445 compiled. This information is used for outputting debugging info. */
2447 tree
2448 build_block (vars, tags, subblocks, supercontext, chain)
2449 tree vars, tags ATTRIBUTE_UNUSED, subblocks, supercontext, chain;
2451 tree block = make_node (BLOCK);
2453 BLOCK_VARS (block) = vars;
2454 BLOCK_SUBBLOCKS (block) = subblocks;
2455 BLOCK_SUPERCONTEXT (block) = supercontext;
2456 BLOCK_CHAIN (block) = chain;
2457 return block;
2460 /* EXPR_WITH_FILE_LOCATION are used to keep track of the exact
2461 location where an expression or an identifier were encountered. It
2462 is necessary for languages where the frontend parser will handle
2463 recursively more than one file (Java is one of them). */
2465 tree
2466 build_expr_wfl (node, file, line, col)
2467 tree node;
2468 const char *file;
2469 int line, col;
2471 static const char *last_file = 0;
2472 static tree last_filenode = NULL_TREE;
2473 tree wfl = make_node (EXPR_WITH_FILE_LOCATION);
2475 EXPR_WFL_NODE (wfl) = node;
2476 EXPR_WFL_SET_LINECOL (wfl, line, col);
2477 if (file != last_file)
2479 last_file = file;
2480 last_filenode = file ? get_identifier (file) : NULL_TREE;
2483 EXPR_WFL_FILENAME_NODE (wfl) = last_filenode;
2484 if (node)
2486 TREE_SIDE_EFFECTS (wfl) = TREE_SIDE_EFFECTS (node);
2487 TREE_TYPE (wfl) = TREE_TYPE (node);
2490 return wfl;
2493 /* Return a declaration like DDECL except that its DECL_ATTRIBUTES
2494 is ATTRIBUTE. */
2496 tree
2497 build_decl_attribute_variant (ddecl, attribute)
2498 tree ddecl, attribute;
2500 DECL_ATTRIBUTES (ddecl) = attribute;
2501 return ddecl;
2504 /* Return a type like TTYPE except that its TYPE_ATTRIBUTE
2505 is ATTRIBUTE.
2507 Record such modified types already made so we don't make duplicates. */
2509 tree
2510 build_type_attribute_variant (ttype, attribute)
2511 tree ttype, attribute;
2513 if (! attribute_list_equal (TYPE_ATTRIBUTES (ttype), attribute))
2515 unsigned int hashcode;
2516 tree ntype;
2518 ntype = copy_node (ttype);
2520 TYPE_POINTER_TO (ntype) = 0;
2521 TYPE_REFERENCE_TO (ntype) = 0;
2522 TYPE_ATTRIBUTES (ntype) = attribute;
2524 /* Create a new main variant of TYPE. */
2525 TYPE_MAIN_VARIANT (ntype) = ntype;
2526 TYPE_NEXT_VARIANT (ntype) = 0;
2527 set_type_quals (ntype, TYPE_UNQUALIFIED);
2529 hashcode = (TYPE_HASH (TREE_CODE (ntype))
2530 + TYPE_HASH (TREE_TYPE (ntype))
2531 + attribute_hash_list (attribute));
2533 switch (TREE_CODE (ntype))
2535 case FUNCTION_TYPE:
2536 hashcode += TYPE_HASH (TYPE_ARG_TYPES (ntype));
2537 break;
2538 case ARRAY_TYPE:
2539 hashcode += TYPE_HASH (TYPE_DOMAIN (ntype));
2540 break;
2541 case INTEGER_TYPE:
2542 hashcode += TYPE_HASH (TYPE_MAX_VALUE (ntype));
2543 break;
2544 case REAL_TYPE:
2545 hashcode += TYPE_HASH (TYPE_PRECISION (ntype));
2546 break;
2547 default:
2548 break;
2551 ntype = type_hash_canon (hashcode, ntype);
2552 ttype = build_qualified_type (ntype, TYPE_QUALS (ttype));
2555 return ttype;
2558 /* Default value of targetm.comp_type_attributes that always returns 1. */
2561 default_comp_type_attributes (type1, type2)
2562 tree type1 ATTRIBUTE_UNUSED;
2563 tree type2 ATTRIBUTE_UNUSED;
2565 return 1;
2568 /* Default version of targetm.set_default_type_attributes that always does
2569 nothing. */
2571 void
2572 default_set_default_type_attributes (type)
2573 tree type ATTRIBUTE_UNUSED;
2577 /* Default version of targetm.insert_attributes that always does nothing. */
2578 void
2579 default_insert_attributes (decl, attr_ptr)
2580 tree decl ATTRIBUTE_UNUSED;
2581 tree *attr_ptr ATTRIBUTE_UNUSED;
2585 /* Default value of targetm.function_attribute_inlinable_p that always
2586 returns false. */
2587 bool
2588 default_function_attribute_inlinable_p (fndecl)
2589 tree fndecl ATTRIBUTE_UNUSED;
2591 /* By default, functions with machine attributes cannot be inlined. */
2592 return false;
2595 /* Default value of targetm.ms_bitfield_layout_p that always returns
2596 false. */
2597 bool
2598 default_ms_bitfield_layout_p (record)
2599 tree record ATTRIBUTE_UNUSED;
2601 /* By default, GCC does not use the MS VC++ bitfield layout rules. */
2602 return false;
2605 /* Return non-zero if IDENT is a valid name for attribute ATTR,
2606 or zero if not.
2608 We try both `text' and `__text__', ATTR may be either one. */
2609 /* ??? It might be a reasonable simplification to require ATTR to be only
2610 `text'. One might then also require attribute lists to be stored in
2611 their canonicalized form. */
2614 is_attribute_p (attr, ident)
2615 const char *attr;
2616 tree ident;
2618 int ident_len, attr_len;
2619 const char *p;
2621 if (TREE_CODE (ident) != IDENTIFIER_NODE)
2622 return 0;
2624 if (strcmp (attr, IDENTIFIER_POINTER (ident)) == 0)
2625 return 1;
2627 p = IDENTIFIER_POINTER (ident);
2628 ident_len = strlen (p);
2629 attr_len = strlen (attr);
2631 /* If ATTR is `__text__', IDENT must be `text'; and vice versa. */
2632 if (attr[0] == '_')
2634 if (attr[1] != '_'
2635 || attr[attr_len - 2] != '_'
2636 || attr[attr_len - 1] != '_')
2637 abort ();
2638 if (ident_len == attr_len - 4
2639 && strncmp (attr + 2, p, attr_len - 4) == 0)
2640 return 1;
2642 else
2644 if (ident_len == attr_len + 4
2645 && p[0] == '_' && p[1] == '_'
2646 && p[ident_len - 2] == '_' && p[ident_len - 1] == '_'
2647 && strncmp (attr, p + 2, attr_len) == 0)
2648 return 1;
2651 return 0;
2654 /* Given an attribute name and a list of attributes, return a pointer to the
2655 attribute's list element if the attribute is part of the list, or NULL_TREE
2656 if not found. If the attribute appears more than once, this only
2657 returns the first occurrence; the TREE_CHAIN of the return value should
2658 be passed back in if further occurrences are wanted. */
2660 tree
2661 lookup_attribute (attr_name, list)
2662 const char *attr_name;
2663 tree list;
2665 tree l;
2667 for (l = list; l; l = TREE_CHAIN (l))
2669 if (TREE_CODE (TREE_PURPOSE (l)) != IDENTIFIER_NODE)
2670 abort ();
2671 if (is_attribute_p (attr_name, TREE_PURPOSE (l)))
2672 return l;
2675 return NULL_TREE;
2678 /* Return an attribute list that is the union of a1 and a2. */
2680 tree
2681 merge_attributes (a1, a2)
2682 tree a1, a2;
2684 tree attributes;
2686 /* Either one unset? Take the set one. */
2688 if ((attributes = a1) == 0)
2689 attributes = a2;
2691 /* One that completely contains the other? Take it. */
2693 else if (a2 != 0 && ! attribute_list_contained (a1, a2))
2695 if (attribute_list_contained (a2, a1))
2696 attributes = a2;
2697 else
2699 /* Pick the longest list, and hang on the other list. */
2701 if (list_length (a1) < list_length (a2))
2702 attributes = a2, a2 = a1;
2704 for (; a2 != 0; a2 = TREE_CHAIN (a2))
2706 tree a;
2707 for (a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
2708 attributes);
2709 a != NULL_TREE;
2710 a = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (a2)),
2711 TREE_CHAIN (a)))
2713 if (simple_cst_equal (TREE_VALUE (a), TREE_VALUE (a2)) == 1)
2714 break;
2716 if (a == NULL_TREE)
2718 a1 = copy_node (a2);
2719 TREE_CHAIN (a1) = attributes;
2720 attributes = a1;
2725 return attributes;
2728 /* Given types T1 and T2, merge their attributes and return
2729 the result. */
2731 tree
2732 merge_type_attributes (t1, t2)
2733 tree t1, t2;
2735 return merge_attributes (TYPE_ATTRIBUTES (t1),
2736 TYPE_ATTRIBUTES (t2));
2739 /* Given decls OLDDECL and NEWDECL, merge their attributes and return
2740 the result. */
2742 tree
2743 merge_decl_attributes (olddecl, newdecl)
2744 tree olddecl, newdecl;
2746 return merge_attributes (DECL_ATTRIBUTES (olddecl),
2747 DECL_ATTRIBUTES (newdecl));
2750 #ifdef TARGET_DLLIMPORT_DECL_ATTRIBUTES
2752 /* Specialization of merge_decl_attributes for various Windows targets.
2754 This handles the following situation:
2756 __declspec (dllimport) int foo;
2757 int foo;
2759 The second instance of `foo' nullifies the dllimport. */
2761 tree
2762 merge_dllimport_decl_attributes (old, new)
2763 tree old;
2764 tree new;
2766 tree a;
2767 int delete_dllimport_p;
2769 old = DECL_ATTRIBUTES (old);
2770 new = DECL_ATTRIBUTES (new);
2772 /* What we need to do here is remove from `old' dllimport if it doesn't
2773 appear in `new'. dllimport behaves like extern: if a declaration is
2774 marked dllimport and a definition appears later, then the object
2775 is not dllimport'd. */
2776 if (lookup_attribute ("dllimport", old) != NULL_TREE
2777 && lookup_attribute ("dllimport", new) == NULL_TREE)
2778 delete_dllimport_p = 1;
2779 else
2780 delete_dllimport_p = 0;
2782 a = merge_attributes (old, new);
2784 if (delete_dllimport_p)
2786 tree prev, t;
2788 /* Scan the list for dllimport and delete it. */
2789 for (prev = NULL_TREE, t = a; t; prev = t, t = TREE_CHAIN (t))
2791 if (is_attribute_p ("dllimport", TREE_PURPOSE (t)))
2793 if (prev == NULL_TREE)
2794 a = TREE_CHAIN (a);
2795 else
2796 TREE_CHAIN (prev) = TREE_CHAIN (t);
2797 break;
2802 return a;
2805 #endif /* TARGET_DLLIMPORT_DECL_ATTRIBUTES */
2807 /* Set the type qualifiers for TYPE to TYPE_QUALS, which is a bitmask
2808 of the various TYPE_QUAL values. */
2810 static void
2811 set_type_quals (type, type_quals)
2812 tree type;
2813 int type_quals;
2815 TYPE_READONLY (type) = (type_quals & TYPE_QUAL_CONST) != 0;
2816 TYPE_VOLATILE (type) = (type_quals & TYPE_QUAL_VOLATILE) != 0;
2817 TYPE_RESTRICT (type) = (type_quals & TYPE_QUAL_RESTRICT) != 0;
2820 /* Return a version of the TYPE, qualified as indicated by the
2821 TYPE_QUALS, if one exists. If no qualified version exists yet,
2822 return NULL_TREE. */
2824 tree
2825 get_qualified_type (type, type_quals)
2826 tree type;
2827 int type_quals;
2829 tree t;
2831 /* Search the chain of variants to see if there is already one there just
2832 like the one we need to have. If so, use that existing one. We must
2833 preserve the TYPE_NAME, since there is code that depends on this. */
2834 for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
2835 if (TYPE_QUALS (t) == type_quals && TYPE_NAME (t) == TYPE_NAME (type)
2836 && TYPE_CONTEXT (t) == TYPE_CONTEXT (type))
2837 return t;
2839 return NULL_TREE;
2842 /* Like get_qualified_type, but creates the type if it does not
2843 exist. This function never returns NULL_TREE. */
2845 tree
2846 build_qualified_type (type, type_quals)
2847 tree type;
2848 int type_quals;
2850 tree t;
2852 /* See if we already have the appropriate qualified variant. */
2853 t = get_qualified_type (type, type_quals);
2855 /* If not, build it. */
2856 if (!t)
2858 t = build_type_copy (type);
2859 set_type_quals (t, type_quals);
2862 return t;
2865 /* Create a new variant of TYPE, equivalent but distinct.
2866 This is so the caller can modify it. */
2868 tree
2869 build_type_copy (type)
2870 tree type;
2872 tree t, m = TYPE_MAIN_VARIANT (type);
2874 t = copy_node (type);
2876 TYPE_POINTER_TO (t) = 0;
2877 TYPE_REFERENCE_TO (t) = 0;
2879 /* Add this type to the chain of variants of TYPE. */
2880 TYPE_NEXT_VARIANT (t) = TYPE_NEXT_VARIANT (m);
2881 TYPE_NEXT_VARIANT (m) = t;
2883 return t;
2886 /* Hashing of types so that we don't make duplicates.
2887 The entry point is `type_hash_canon'. */
2889 /* Compute a hash code for a list of types (chain of TREE_LIST nodes
2890 with types in the TREE_VALUE slots), by adding the hash codes
2891 of the individual types. */
2893 unsigned int
2894 type_hash_list (list)
2895 tree list;
2897 unsigned int hashcode;
2898 tree tail;
2900 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
2901 hashcode += TYPE_HASH (TREE_VALUE (tail));
2903 return hashcode;
2906 /* These are the Hashtable callback functions. */
2908 /* Returns true if the types are equal. */
2910 static int
2911 type_hash_eq (va, vb)
2912 const void *va;
2913 const void *vb;
2915 const struct type_hash *a = va, *b = vb;
2916 if (a->hash == b->hash
2917 && TREE_CODE (a->type) == TREE_CODE (b->type)
2918 && TREE_TYPE (a->type) == TREE_TYPE (b->type)
2919 && attribute_list_equal (TYPE_ATTRIBUTES (a->type),
2920 TYPE_ATTRIBUTES (b->type))
2921 && TYPE_ALIGN (a->type) == TYPE_ALIGN (b->type)
2922 && (TYPE_MAX_VALUE (a->type) == TYPE_MAX_VALUE (b->type)
2923 || tree_int_cst_equal (TYPE_MAX_VALUE (a->type),
2924 TYPE_MAX_VALUE (b->type)))
2925 && (TYPE_MIN_VALUE (a->type) == TYPE_MIN_VALUE (b->type)
2926 || tree_int_cst_equal (TYPE_MIN_VALUE (a->type),
2927 TYPE_MIN_VALUE (b->type)))
2928 /* Note that TYPE_DOMAIN is TYPE_ARG_TYPES for FUNCTION_TYPE. */
2929 && (TYPE_DOMAIN (a->type) == TYPE_DOMAIN (b->type)
2930 || (TYPE_DOMAIN (a->type)
2931 && TREE_CODE (TYPE_DOMAIN (a->type)) == TREE_LIST
2932 && TYPE_DOMAIN (b->type)
2933 && TREE_CODE (TYPE_DOMAIN (b->type)) == TREE_LIST
2934 && type_list_equal (TYPE_DOMAIN (a->type),
2935 TYPE_DOMAIN (b->type)))))
2936 return 1;
2937 return 0;
2940 /* Return the cached hash value. */
2942 static unsigned int
2943 type_hash_hash (item)
2944 const void *item;
2946 return ((const struct type_hash *) item)->hash;
2949 /* Look in the type hash table for a type isomorphic to TYPE.
2950 If one is found, return it. Otherwise return 0. */
2952 tree
2953 type_hash_lookup (hashcode, type)
2954 unsigned int hashcode;
2955 tree type;
2957 struct type_hash *h, in;
2959 /* The TYPE_ALIGN field of a type is set by layout_type(), so we
2960 must call that routine before comparing TYPE_ALIGNs. */
2961 layout_type (type);
2963 in.hash = hashcode;
2964 in.type = type;
2966 h = htab_find_with_hash (type_hash_table, &in, hashcode);
2967 if (h)
2968 return h->type;
2969 return NULL_TREE;
2972 /* Add an entry to the type-hash-table
2973 for a type TYPE whose hash code is HASHCODE. */
2975 void
2976 type_hash_add (hashcode, type)
2977 unsigned int hashcode;
2978 tree type;
2980 struct type_hash *h;
2981 void **loc;
2983 h = (struct type_hash *) ggc_alloc (sizeof (struct type_hash));
2984 h->hash = hashcode;
2985 h->type = type;
2986 loc = htab_find_slot_with_hash (type_hash_table, h, hashcode, INSERT);
2987 *(struct type_hash **) loc = h;
2990 /* Given TYPE, and HASHCODE its hash code, return the canonical
2991 object for an identical type if one already exists.
2992 Otherwise, return TYPE, and record it as the canonical object
2993 if it is a permanent object.
2995 To use this function, first create a type of the sort you want.
2996 Then compute its hash code from the fields of the type that
2997 make it different from other similar types.
2998 Then call this function and use the value.
2999 This function frees the type you pass in if it is a duplicate. */
3001 /* Set to 1 to debug without canonicalization. Never set by program. */
3002 int debug_no_type_hash = 0;
3004 tree
3005 type_hash_canon (hashcode, type)
3006 unsigned int hashcode;
3007 tree type;
3009 tree t1;
3011 if (debug_no_type_hash)
3012 return type;
3014 /* See if the type is in the hash table already. If so, return it.
3015 Otherwise, add the type. */
3016 t1 = type_hash_lookup (hashcode, type);
3017 if (t1 != 0)
3019 #ifdef GATHER_STATISTICS
3020 tree_node_counts[(int) t_kind]--;
3021 tree_node_sizes[(int) t_kind] -= sizeof (struct tree_type);
3022 #endif
3023 return t1;
3025 else
3027 type_hash_add (hashcode, type);
3028 return type;
3032 /* See if the data pointed to by the type hash table is marked. We consider
3033 it marked if the type is marked or if a debug type number or symbol
3034 table entry has been made for the type. This reduces the amount of
3035 debugging output and eliminates that dependency of the debug output on
3036 the number of garbage collections. */
3038 static int
3039 type_hash_marked_p (p)
3040 const void *p;
3042 tree type = ((struct type_hash *) p)->type;
3044 return ggc_marked_p (type) || TYPE_SYMTAB_POINTER (type);
3047 static void
3048 print_type_hash_statistics ()
3050 fprintf (stderr, "Type hash: size %ld, %ld elements, %f collisions\n",
3051 (long) htab_size (type_hash_table),
3052 (long) htab_elements (type_hash_table),
3053 htab_collisions (type_hash_table));
3056 /* Compute a hash code for a list of attributes (chain of TREE_LIST nodes
3057 with names in the TREE_PURPOSE slots and args in the TREE_VALUE slots),
3058 by adding the hash codes of the individual attributes. */
3060 unsigned int
3061 attribute_hash_list (list)
3062 tree list;
3064 unsigned int hashcode;
3065 tree tail;
3067 for (hashcode = 0, tail = list; tail; tail = TREE_CHAIN (tail))
3068 /* ??? Do we want to add in TREE_VALUE too? */
3069 hashcode += TYPE_HASH (TREE_PURPOSE (tail));
3070 return hashcode;
3073 /* Given two lists of attributes, return true if list l2 is
3074 equivalent to l1. */
3077 attribute_list_equal (l1, l2)
3078 tree l1, l2;
3080 return attribute_list_contained (l1, l2)
3081 && attribute_list_contained (l2, l1);
3084 /* Given two lists of attributes, return true if list L2 is
3085 completely contained within L1. */
3086 /* ??? This would be faster if attribute names were stored in a canonicalized
3087 form. Otherwise, if L1 uses `foo' and L2 uses `__foo__', the long method
3088 must be used to show these elements are equivalent (which they are). */
3089 /* ??? It's not clear that attributes with arguments will always be handled
3090 correctly. */
3093 attribute_list_contained (l1, l2)
3094 tree l1, l2;
3096 tree t1, t2;
3098 /* First check the obvious, maybe the lists are identical. */
3099 if (l1 == l2)
3100 return 1;
3102 /* Maybe the lists are similar. */
3103 for (t1 = l1, t2 = l2;
3104 t1 != 0 && t2 != 0
3105 && TREE_PURPOSE (t1) == TREE_PURPOSE (t2)
3106 && TREE_VALUE (t1) == TREE_VALUE (t2);
3107 t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2));
3109 /* Maybe the lists are equal. */
3110 if (t1 == 0 && t2 == 0)
3111 return 1;
3113 for (; t2 != 0; t2 = TREE_CHAIN (t2))
3115 tree attr;
3116 for (attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)), l1);
3117 attr != NULL_TREE;
3118 attr = lookup_attribute (IDENTIFIER_POINTER (TREE_PURPOSE (t2)),
3119 TREE_CHAIN (attr)))
3121 if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) == 1)
3122 break;
3125 if (attr == 0)
3126 return 0;
3128 if (simple_cst_equal (TREE_VALUE (t2), TREE_VALUE (attr)) != 1)
3129 return 0;
3132 return 1;
3135 /* Given two lists of types
3136 (chains of TREE_LIST nodes with types in the TREE_VALUE slots)
3137 return 1 if the lists contain the same types in the same order.
3138 Also, the TREE_PURPOSEs must match. */
3141 type_list_equal (l1, l2)
3142 tree l1, l2;
3144 tree t1, t2;
3146 for (t1 = l1, t2 = l2; t1 && t2; t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
3147 if (TREE_VALUE (t1) != TREE_VALUE (t2)
3148 || (TREE_PURPOSE (t1) != TREE_PURPOSE (t2)
3149 && ! (1 == simple_cst_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2))
3150 && (TREE_TYPE (TREE_PURPOSE (t1))
3151 == TREE_TYPE (TREE_PURPOSE (t2))))))
3152 return 0;
3154 return t1 == t2;
3157 /* Returns the number of arguments to the FUNCTION_TYPE or METHOD_TYPE
3158 given by TYPE. If the argument list accepts variable arguments,
3159 then this function counts only the ordinary arguments. */
3162 type_num_arguments (type)
3163 tree type;
3165 int i = 0;
3166 tree t;
3168 for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
3169 /* If the function does not take a variable number of arguments,
3170 the last element in the list will have type `void'. */
3171 if (VOID_TYPE_P (TREE_VALUE (t)))
3172 break;
3173 else
3174 ++i;
3176 return i;
3179 /* Nonzero if integer constants T1 and T2
3180 represent the same constant value. */
3183 tree_int_cst_equal (t1, t2)
3184 tree t1, t2;
3186 if (t1 == t2)
3187 return 1;
3189 if (t1 == 0 || t2 == 0)
3190 return 0;
3192 if (TREE_CODE (t1) == INTEGER_CST
3193 && TREE_CODE (t2) == INTEGER_CST
3194 && TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3195 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2))
3196 return 1;
3198 return 0;
3201 /* Nonzero if integer constants T1 and T2 represent values that satisfy <.
3202 The precise way of comparison depends on their data type. */
3205 tree_int_cst_lt (t1, t2)
3206 tree t1, t2;
3208 if (t1 == t2)
3209 return 0;
3211 if (TREE_UNSIGNED (TREE_TYPE (t1)) != TREE_UNSIGNED (TREE_TYPE (t2)))
3213 int t1_sgn = tree_int_cst_sgn (t1);
3214 int t2_sgn = tree_int_cst_sgn (t2);
3216 if (t1_sgn < t2_sgn)
3217 return 1;
3218 else if (t1_sgn > t2_sgn)
3219 return 0;
3220 /* Otherwise, both are non-negative, so we compare them as
3221 unsigned just in case one of them would overflow a signed
3222 type. */
3224 else if (! TREE_UNSIGNED (TREE_TYPE (t1)))
3225 return INT_CST_LT (t1, t2);
3227 return INT_CST_LT_UNSIGNED (t1, t2);
3230 /* Returns -1 if T1 < T2, 0 if T1 == T2, and 1 if T1 > T2. */
3233 tree_int_cst_compare (t1, t2)
3234 tree t1;
3235 tree t2;
3237 if (tree_int_cst_lt (t1, t2))
3238 return -1;
3239 else if (tree_int_cst_lt (t2, t1))
3240 return 1;
3241 else
3242 return 0;
3245 /* Return 1 if T is an INTEGER_CST that can be manipulated efficiently on
3246 the host. If POS is zero, the value can be represented in a single
3247 HOST_WIDE_INT. If POS is nonzero, the value must be positive and can
3248 be represented in a single unsigned HOST_WIDE_INT. */
3251 host_integerp (t, pos)
3252 tree t;
3253 int pos;
3255 return (TREE_CODE (t) == INTEGER_CST
3256 && ! TREE_OVERFLOW (t)
3257 && ((TREE_INT_CST_HIGH (t) == 0
3258 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) >= 0)
3259 || (! pos && TREE_INT_CST_HIGH (t) == -1
3260 && (HOST_WIDE_INT) TREE_INT_CST_LOW (t) < 0
3261 && ! TREE_UNSIGNED (TREE_TYPE (t)))
3262 || (pos && TREE_INT_CST_HIGH (t) == 0)));
3265 /* Return the HOST_WIDE_INT least significant bits of T if it is an
3266 INTEGER_CST and there is no overflow. POS is nonzero if the result must
3267 be positive. Abort if we cannot satisfy the above conditions. */
3269 HOST_WIDE_INT
3270 tree_low_cst (t, pos)
3271 tree t;
3272 int pos;
3274 if (host_integerp (t, pos))
3275 return TREE_INT_CST_LOW (t);
3276 else
3277 abort ();
3280 /* Return the most significant bit of the integer constant T. */
3283 tree_int_cst_msb (t)
3284 tree t;
3286 int prec;
3287 HOST_WIDE_INT h;
3288 unsigned HOST_WIDE_INT l;
3290 /* Note that using TYPE_PRECISION here is wrong. We care about the
3291 actual bits, not the (arbitrary) range of the type. */
3292 prec = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (t))) - 1;
3293 rshift_double (TREE_INT_CST_LOW (t), TREE_INT_CST_HIGH (t), prec,
3294 2 * HOST_BITS_PER_WIDE_INT, &l, &h, 0);
3295 return (l & 1) == 1;
3298 /* Return an indication of the sign of the integer constant T.
3299 The return value is -1 if T < 0, 0 if T == 0, and 1 if T > 0.
3300 Note that -1 will never be returned it T's type is unsigned. */
3303 tree_int_cst_sgn (t)
3304 tree t;
3306 if (TREE_INT_CST_LOW (t) == 0 && TREE_INT_CST_HIGH (t) == 0)
3307 return 0;
3308 else if (TREE_UNSIGNED (TREE_TYPE (t)))
3309 return 1;
3310 else if (TREE_INT_CST_HIGH (t) < 0)
3311 return -1;
3312 else
3313 return 1;
3316 /* Compare two constructor-element-type constants. Return 1 if the lists
3317 are known to be equal; otherwise return 0. */
3320 simple_cst_list_equal (l1, l2)
3321 tree l1, l2;
3323 while (l1 != NULL_TREE && l2 != NULL_TREE)
3325 if (simple_cst_equal (TREE_VALUE (l1), TREE_VALUE (l2)) != 1)
3326 return 0;
3328 l1 = TREE_CHAIN (l1);
3329 l2 = TREE_CHAIN (l2);
3332 return l1 == l2;
3335 /* Return truthvalue of whether T1 is the same tree structure as T2.
3336 Return 1 if they are the same.
3337 Return 0 if they are understandably different.
3338 Return -1 if either contains tree structure not understood by
3339 this function. */
3342 simple_cst_equal (t1, t2)
3343 tree t1, t2;
3345 enum tree_code code1, code2;
3346 int cmp;
3347 int i;
3349 if (t1 == t2)
3350 return 1;
3351 if (t1 == 0 || t2 == 0)
3352 return 0;
3354 code1 = TREE_CODE (t1);
3355 code2 = TREE_CODE (t2);
3357 if (code1 == NOP_EXPR || code1 == CONVERT_EXPR || code1 == NON_LVALUE_EXPR)
3359 if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3360 || code2 == NON_LVALUE_EXPR)
3361 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3362 else
3363 return simple_cst_equal (TREE_OPERAND (t1, 0), t2);
3366 else if (code2 == NOP_EXPR || code2 == CONVERT_EXPR
3367 || code2 == NON_LVALUE_EXPR)
3368 return simple_cst_equal (t1, TREE_OPERAND (t2, 0));
3370 if (code1 != code2)
3371 return 0;
3373 switch (code1)
3375 case INTEGER_CST:
3376 return (TREE_INT_CST_LOW (t1) == TREE_INT_CST_LOW (t2)
3377 && TREE_INT_CST_HIGH (t1) == TREE_INT_CST_HIGH (t2));
3379 case REAL_CST:
3380 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3382 case STRING_CST:
3383 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3384 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3385 TREE_STRING_LENGTH (t1)));
3387 case CONSTRUCTOR:
3388 if (CONSTRUCTOR_ELTS (t1) == CONSTRUCTOR_ELTS (t2))
3389 return 1;
3390 else
3391 abort ();
3393 case SAVE_EXPR:
3394 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3396 case CALL_EXPR:
3397 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3398 if (cmp <= 0)
3399 return cmp;
3400 return
3401 simple_cst_list_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3403 case TARGET_EXPR:
3404 /* Special case: if either target is an unallocated VAR_DECL,
3405 it means that it's going to be unified with whatever the
3406 TARGET_EXPR is really supposed to initialize, so treat it
3407 as being equivalent to anything. */
3408 if ((TREE_CODE (TREE_OPERAND (t1, 0)) == VAR_DECL
3409 && DECL_NAME (TREE_OPERAND (t1, 0)) == NULL_TREE
3410 && !DECL_RTL_SET_P (TREE_OPERAND (t1, 0)))
3411 || (TREE_CODE (TREE_OPERAND (t2, 0)) == VAR_DECL
3412 && DECL_NAME (TREE_OPERAND (t2, 0)) == NULL_TREE
3413 && !DECL_RTL_SET_P (TREE_OPERAND (t2, 0))))
3414 cmp = 1;
3415 else
3416 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3418 if (cmp <= 0)
3419 return cmp;
3421 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1));
3423 case WITH_CLEANUP_EXPR:
3424 cmp = simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3425 if (cmp <= 0)
3426 return cmp;
3428 return simple_cst_equal (TREE_OPERAND (t1, 1), TREE_OPERAND (t1, 1));
3430 case COMPONENT_REF:
3431 if (TREE_OPERAND (t1, 1) == TREE_OPERAND (t2, 1))
3432 return simple_cst_equal (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3434 return 0;
3436 case VAR_DECL:
3437 case PARM_DECL:
3438 case CONST_DECL:
3439 case FUNCTION_DECL:
3440 return 0;
3442 default:
3443 break;
3446 /* This general rule works for most tree codes. All exceptions should be
3447 handled above. If this is a language-specific tree code, we can't
3448 trust what might be in the operand, so say we don't know
3449 the situation. */
3450 if ((int) code1 >= (int) LAST_AND_UNUSED_TREE_CODE)
3451 return -1;
3453 switch (TREE_CODE_CLASS (code1))
3455 case '1':
3456 case '2':
3457 case '<':
3458 case 'e':
3459 case 'r':
3460 case 's':
3461 cmp = 1;
3462 for (i = 0; i < TREE_CODE_LENGTH (code1); i++)
3464 cmp = simple_cst_equal (TREE_OPERAND (t1, i), TREE_OPERAND (t2, i));
3465 if (cmp <= 0)
3466 return cmp;
3469 return cmp;
3471 default:
3472 return -1;
3476 /* Compare the value of T, an INTEGER_CST, with U, an unsigned integer value.
3477 Return -1, 0, or 1 if the value of T is less than, equal to, or greater
3478 than U, respectively. */
3481 compare_tree_int (t, u)
3482 tree t;
3483 unsigned HOST_WIDE_INT u;
3485 if (tree_int_cst_sgn (t) < 0)
3486 return -1;
3487 else if (TREE_INT_CST_HIGH (t) != 0)
3488 return 1;
3489 else if (TREE_INT_CST_LOW (t) == u)
3490 return 0;
3491 else if (TREE_INT_CST_LOW (t) < u)
3492 return -1;
3493 else
3494 return 1;
3497 /* Constructors for pointer, array and function types.
3498 (RECORD_TYPE, UNION_TYPE and ENUMERAL_TYPE nodes are
3499 constructed by language-dependent code, not here.) */
3501 /* Construct, lay out and return the type of pointers to TO_TYPE.
3502 If such a type has already been constructed, reuse it. */
3504 tree
3505 build_pointer_type (to_type)
3506 tree to_type;
3508 tree t = TYPE_POINTER_TO (to_type);
3510 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3512 if (t != 0)
3513 return t;
3515 /* We need a new one. */
3516 t = make_node (POINTER_TYPE);
3518 TREE_TYPE (t) = to_type;
3520 /* Record this type as the pointer to TO_TYPE. */
3521 TYPE_POINTER_TO (to_type) = t;
3523 /* Lay out the type. This function has many callers that are concerned
3524 with expression-construction, and this simplifies them all.
3525 Also, it guarantees the TYPE_SIZE is in the same obstack as the type. */
3526 layout_type (t);
3528 return t;
3531 /* Build the node for the type of references-to-TO_TYPE. */
3533 tree
3534 build_reference_type (to_type)
3535 tree to_type;
3537 tree t = TYPE_REFERENCE_TO (to_type);
3539 /* First, if we already have a type for pointers to TO_TYPE, use it. */
3541 if (t)
3542 return t;
3544 /* We need a new one. */
3545 t = make_node (REFERENCE_TYPE);
3547 TREE_TYPE (t) = to_type;
3549 /* Record this type as the pointer to TO_TYPE. */
3550 TYPE_REFERENCE_TO (to_type) = t;
3552 layout_type (t);
3554 return t;
3557 /* Build a type that is compatible with t but has no cv quals anywhere
3558 in its type, thus
3560 const char *const *const * -> char ***. */
3562 tree
3563 build_type_no_quals (t)
3564 tree t;
3566 switch (TREE_CODE (t))
3568 case POINTER_TYPE:
3569 return build_pointer_type (build_type_no_quals (TREE_TYPE (t)));
3570 case REFERENCE_TYPE:
3571 return build_reference_type (build_type_no_quals (TREE_TYPE (t)));
3572 default:
3573 return TYPE_MAIN_VARIANT (t);
3577 /* Create a type of integers to be the TYPE_DOMAIN of an ARRAY_TYPE.
3578 MAXVAL should be the maximum value in the domain
3579 (one less than the length of the array).
3581 The maximum value that MAXVAL can have is INT_MAX for a HOST_WIDE_INT.
3582 We don't enforce this limit, that is up to caller (e.g. language front end).
3583 The limit exists because the result is a signed type and we don't handle
3584 sizes that use more than one HOST_WIDE_INT. */
3586 tree
3587 build_index_type (maxval)
3588 tree maxval;
3590 tree itype = make_node (INTEGER_TYPE);
3592 TREE_TYPE (itype) = sizetype;
3593 TYPE_PRECISION (itype) = TYPE_PRECISION (sizetype);
3594 TYPE_MIN_VALUE (itype) = size_zero_node;
3595 TYPE_MAX_VALUE (itype) = convert (sizetype, maxval);
3596 TYPE_MODE (itype) = TYPE_MODE (sizetype);
3597 TYPE_SIZE (itype) = TYPE_SIZE (sizetype);
3598 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (sizetype);
3599 TYPE_ALIGN (itype) = TYPE_ALIGN (sizetype);
3600 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (sizetype);
3602 if (host_integerp (maxval, 1))
3603 return type_hash_canon (tree_low_cst (maxval, 1), itype);
3604 else
3605 return itype;
3608 /* Create a range of some discrete type TYPE (an INTEGER_TYPE,
3609 ENUMERAL_TYPE, BOOLEAN_TYPE, or CHAR_TYPE), with
3610 low bound LOWVAL and high bound HIGHVAL.
3611 if TYPE==NULL_TREE, sizetype is used. */
3613 tree
3614 build_range_type (type, lowval, highval)
3615 tree type, lowval, highval;
3617 tree itype = make_node (INTEGER_TYPE);
3619 TREE_TYPE (itype) = type;
3620 if (type == NULL_TREE)
3621 type = sizetype;
3623 TYPE_MIN_VALUE (itype) = convert (type, lowval);
3624 TYPE_MAX_VALUE (itype) = highval ? convert (type, highval) : NULL;
3626 TYPE_PRECISION (itype) = TYPE_PRECISION (type);
3627 TYPE_MODE (itype) = TYPE_MODE (type);
3628 TYPE_SIZE (itype) = TYPE_SIZE (type);
3629 TYPE_SIZE_UNIT (itype) = TYPE_SIZE_UNIT (type);
3630 TYPE_ALIGN (itype) = TYPE_ALIGN (type);
3631 TYPE_USER_ALIGN (itype) = TYPE_USER_ALIGN (type);
3633 if (host_integerp (lowval, 0) && highval != 0 && host_integerp (highval, 0))
3634 return type_hash_canon (tree_low_cst (highval, 0)
3635 - tree_low_cst (lowval, 0),
3636 itype);
3637 else
3638 return itype;
3641 /* Just like build_index_type, but takes lowval and highval instead
3642 of just highval (maxval). */
3644 tree
3645 build_index_2_type (lowval, highval)
3646 tree lowval, highval;
3648 return build_range_type (sizetype, lowval, highval);
3651 /* Return nonzero iff ITYPE1 and ITYPE2 are equal (in the LISP sense).
3652 Needed because when index types are not hashed, equal index types
3653 built at different times appear distinct, even though structurally,
3654 they are not. */
3657 index_type_equal (itype1, itype2)
3658 tree itype1, itype2;
3660 if (TREE_CODE (itype1) != TREE_CODE (itype2))
3661 return 0;
3663 if (TREE_CODE (itype1) == INTEGER_TYPE)
3665 if (TYPE_PRECISION (itype1) != TYPE_PRECISION (itype2)
3666 || TYPE_MODE (itype1) != TYPE_MODE (itype2)
3667 || simple_cst_equal (TYPE_SIZE (itype1), TYPE_SIZE (itype2)) != 1
3668 || TYPE_ALIGN (itype1) != TYPE_ALIGN (itype2))
3669 return 0;
3671 if (1 == simple_cst_equal (TYPE_MIN_VALUE (itype1),
3672 TYPE_MIN_VALUE (itype2))
3673 && 1 == simple_cst_equal (TYPE_MAX_VALUE (itype1),
3674 TYPE_MAX_VALUE (itype2)))
3675 return 1;
3678 return 0;
3681 /* Construct, lay out and return the type of arrays of elements with ELT_TYPE
3682 and number of elements specified by the range of values of INDEX_TYPE.
3683 If such a type has already been constructed, reuse it. */
3685 tree
3686 build_array_type (elt_type, index_type)
3687 tree elt_type, index_type;
3689 tree t;
3690 unsigned int hashcode;
3692 if (TREE_CODE (elt_type) == FUNCTION_TYPE)
3694 error ("arrays of functions are not meaningful");
3695 elt_type = integer_type_node;
3698 /* Make sure TYPE_POINTER_TO (elt_type) is filled in. */
3699 build_pointer_type (elt_type);
3701 /* Allocate the array after the pointer type,
3702 in case we free it in type_hash_canon. */
3703 t = make_node (ARRAY_TYPE);
3704 TREE_TYPE (t) = elt_type;
3705 TYPE_DOMAIN (t) = index_type;
3707 if (index_type == 0)
3709 return t;
3712 hashcode = TYPE_HASH (elt_type) + TYPE_HASH (index_type);
3713 t = type_hash_canon (hashcode, t);
3715 if (!COMPLETE_TYPE_P (t))
3716 layout_type (t);
3717 return t;
3720 /* Return the TYPE of the elements comprising
3721 the innermost dimension of ARRAY. */
3723 tree
3724 get_inner_array_type (array)
3725 tree array;
3727 tree type = TREE_TYPE (array);
3729 while (TREE_CODE (type) == ARRAY_TYPE)
3730 type = TREE_TYPE (type);
3732 return type;
3735 /* Construct, lay out and return
3736 the type of functions returning type VALUE_TYPE
3737 given arguments of types ARG_TYPES.
3738 ARG_TYPES is a chain of TREE_LIST nodes whose TREE_VALUEs
3739 are data type nodes for the arguments of the function.
3740 If such a type has already been constructed, reuse it. */
3742 tree
3743 build_function_type (value_type, arg_types)
3744 tree value_type, arg_types;
3746 tree t;
3747 unsigned int hashcode;
3749 if (TREE_CODE (value_type) == FUNCTION_TYPE)
3751 error ("function return type cannot be function");
3752 value_type = integer_type_node;
3755 /* Make a node of the sort we want. */
3756 t = make_node (FUNCTION_TYPE);
3757 TREE_TYPE (t) = value_type;
3758 TYPE_ARG_TYPES (t) = arg_types;
3760 /* If we already have such a type, use the old one and free this one. */
3761 hashcode = TYPE_HASH (value_type) + type_hash_list (arg_types);
3762 t = type_hash_canon (hashcode, t);
3764 if (!COMPLETE_TYPE_P (t))
3765 layout_type (t);
3766 return t;
3769 /* Build a function type. The RETURN_TYPE is the type retured by the
3770 function. If additional arguments are provided, they are
3771 additional argument types. The list of argument types must always
3772 be terminated by NULL_TREE. */
3774 tree
3775 build_function_type_list VPARAMS ((tree return_type, ...))
3777 tree t, args, last;
3779 VA_OPEN (p, return_type);
3780 VA_FIXEDARG (p, tree, return_type);
3782 t = va_arg (p, tree);
3783 for (args = NULL_TREE; t != NULL_TREE; t = va_arg (p, tree))
3784 args = tree_cons (NULL_TREE, t, args);
3786 last = args;
3787 args = nreverse (args);
3788 TREE_CHAIN (last) = void_list_node;
3789 args = build_function_type (return_type, args);
3791 VA_CLOSE (p);
3792 return args;
3795 /* Construct, lay out and return the type of methods belonging to class
3796 BASETYPE and whose arguments and values are described by TYPE.
3797 If that type exists already, reuse it.
3798 TYPE must be a FUNCTION_TYPE node. */
3800 tree
3801 build_method_type (basetype, type)
3802 tree basetype, type;
3804 tree t;
3805 unsigned int hashcode;
3807 /* Make a node of the sort we want. */
3808 t = make_node (METHOD_TYPE);
3810 if (TREE_CODE (type) != FUNCTION_TYPE)
3811 abort ();
3813 TYPE_METHOD_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3814 TREE_TYPE (t) = TREE_TYPE (type);
3816 /* The actual arglist for this function includes a "hidden" argument
3817 which is "this". Put it into the list of argument types. */
3819 TYPE_ARG_TYPES (t)
3820 = tree_cons (NULL_TREE,
3821 build_pointer_type (basetype), TYPE_ARG_TYPES (type));
3823 /* If we already have such a type, use the old one and free this one. */
3824 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
3825 t = type_hash_canon (hashcode, t);
3827 if (!COMPLETE_TYPE_P (t))
3828 layout_type (t);
3830 return t;
3833 /* Construct, lay out and return the type of offsets to a value
3834 of type TYPE, within an object of type BASETYPE.
3835 If a suitable offset type exists already, reuse it. */
3837 tree
3838 build_offset_type (basetype, type)
3839 tree basetype, type;
3841 tree t;
3842 unsigned int hashcode;
3844 /* Make a node of the sort we want. */
3845 t = make_node (OFFSET_TYPE);
3847 TYPE_OFFSET_BASETYPE (t) = TYPE_MAIN_VARIANT (basetype);
3848 TREE_TYPE (t) = type;
3850 /* If we already have such a type, use the old one and free this one. */
3851 hashcode = TYPE_HASH (basetype) + TYPE_HASH (type);
3852 t = type_hash_canon (hashcode, t);
3854 if (!COMPLETE_TYPE_P (t))
3855 layout_type (t);
3857 return t;
3860 /* Create a complex type whose components are COMPONENT_TYPE. */
3862 tree
3863 build_complex_type (component_type)
3864 tree component_type;
3866 tree t;
3867 unsigned int hashcode;
3869 /* Make a node of the sort we want. */
3870 t = make_node (COMPLEX_TYPE);
3872 TREE_TYPE (t) = TYPE_MAIN_VARIANT (component_type);
3873 set_type_quals (t, TYPE_QUALS (component_type));
3875 /* If we already have such a type, use the old one and free this one. */
3876 hashcode = TYPE_HASH (component_type);
3877 t = type_hash_canon (hashcode, t);
3879 if (!COMPLETE_TYPE_P (t))
3880 layout_type (t);
3882 /* If we are writing Dwarf2 output we need to create a name,
3883 since complex is a fundamental type. */
3884 if ((write_symbols == DWARF2_DEBUG || write_symbols == VMS_AND_DWARF2_DEBUG)
3885 && ! TYPE_NAME (t))
3887 const char *name;
3888 if (component_type == char_type_node)
3889 name = "complex char";
3890 else if (component_type == signed_char_type_node)
3891 name = "complex signed char";
3892 else if (component_type == unsigned_char_type_node)
3893 name = "complex unsigned char";
3894 else if (component_type == short_integer_type_node)
3895 name = "complex short int";
3896 else if (component_type == short_unsigned_type_node)
3897 name = "complex short unsigned int";
3898 else if (component_type == integer_type_node)
3899 name = "complex int";
3900 else if (component_type == unsigned_type_node)
3901 name = "complex unsigned int";
3902 else if (component_type == long_integer_type_node)
3903 name = "complex long int";
3904 else if (component_type == long_unsigned_type_node)
3905 name = "complex long unsigned int";
3906 else if (component_type == long_long_integer_type_node)
3907 name = "complex long long int";
3908 else if (component_type == long_long_unsigned_type_node)
3909 name = "complex long long unsigned int";
3910 else
3911 name = 0;
3913 if (name != 0)
3914 TYPE_NAME (t) = get_identifier (name);
3917 return t;
3920 /* Return OP, stripped of any conversions to wider types as much as is safe.
3921 Converting the value back to OP's type makes a value equivalent to OP.
3923 If FOR_TYPE is nonzero, we return a value which, if converted to
3924 type FOR_TYPE, would be equivalent to converting OP to type FOR_TYPE.
3926 If FOR_TYPE is nonzero, unaligned bit-field references may be changed to the
3927 narrowest type that can hold the value, even if they don't exactly fit.
3928 Otherwise, bit-field references are changed to a narrower type
3929 only if they can be fetched directly from memory in that type.
3931 OP must have integer, real or enumeral type. Pointers are not allowed!
3933 There are some cases where the obvious value we could return
3934 would regenerate to OP if converted to OP's type,
3935 but would not extend like OP to wider types.
3936 If FOR_TYPE indicates such extension is contemplated, we eschew such values.
3937 For example, if OP is (unsigned short)(signed char)-1,
3938 we avoid returning (signed char)-1 if FOR_TYPE is int,
3939 even though extending that to an unsigned short would regenerate OP,
3940 since the result of extending (signed char)-1 to (int)
3941 is different from (int) OP. */
3943 tree
3944 get_unwidened (op, for_type)
3945 tree op;
3946 tree for_type;
3948 /* Set UNS initially if converting OP to FOR_TYPE is a zero-extension. */
3949 tree type = TREE_TYPE (op);
3950 unsigned final_prec
3951 = TYPE_PRECISION (for_type != 0 ? for_type : type);
3952 int uns
3953 = (for_type != 0 && for_type != type
3954 && final_prec > TYPE_PRECISION (type)
3955 && TREE_UNSIGNED (type));
3956 tree win = op;
3958 while (TREE_CODE (op) == NOP_EXPR)
3960 int bitschange
3961 = TYPE_PRECISION (TREE_TYPE (op))
3962 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0)));
3964 /* Truncations are many-one so cannot be removed.
3965 Unless we are later going to truncate down even farther. */
3966 if (bitschange < 0
3967 && final_prec > TYPE_PRECISION (TREE_TYPE (op)))
3968 break;
3970 /* See what's inside this conversion. If we decide to strip it,
3971 we will set WIN. */
3972 op = TREE_OPERAND (op, 0);
3974 /* If we have not stripped any zero-extensions (uns is 0),
3975 we can strip any kind of extension.
3976 If we have previously stripped a zero-extension,
3977 only zero-extensions can safely be stripped.
3978 Any extension can be stripped if the bits it would produce
3979 are all going to be discarded later by truncating to FOR_TYPE. */
3981 if (bitschange > 0)
3983 if (! uns || final_prec <= TYPE_PRECISION (TREE_TYPE (op)))
3984 win = op;
3985 /* TREE_UNSIGNED says whether this is a zero-extension.
3986 Let's avoid computing it if it does not affect WIN
3987 and if UNS will not be needed again. */
3988 if ((uns || TREE_CODE (op) == NOP_EXPR)
3989 && TREE_UNSIGNED (TREE_TYPE (op)))
3991 uns = 1;
3992 win = op;
3997 if (TREE_CODE (op) == COMPONENT_REF
3998 /* Since type_for_size always gives an integer type. */
3999 && TREE_CODE (type) != REAL_TYPE
4000 /* Don't crash if field not laid out yet. */
4001 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0
4002 && host_integerp (DECL_SIZE (TREE_OPERAND (op, 1)), 1))
4004 unsigned int innerprec
4005 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4006 int unsignedp = TREE_UNSIGNED (TREE_OPERAND (op, 1));
4007 type = (*lang_hooks.types.type_for_size) (innerprec, unsignedp);
4009 /* We can get this structure field in the narrowest type it fits in.
4010 If FOR_TYPE is 0, do this only for a field that matches the
4011 narrower type exactly and is aligned for it
4012 The resulting extension to its nominal type (a fullword type)
4013 must fit the same conditions as for other extensions. */
4015 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
4016 && (for_type || ! DECL_BIT_FIELD (TREE_OPERAND (op, 1)))
4017 && (! uns || final_prec <= innerprec || unsignedp)
4018 && type != 0)
4020 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4021 TREE_OPERAND (op, 1));
4022 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4023 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4027 return win;
4030 /* Return OP or a simpler expression for a narrower value
4031 which can be sign-extended or zero-extended to give back OP.
4032 Store in *UNSIGNEDP_PTR either 1 if the value should be zero-extended
4033 or 0 if the value should be sign-extended. */
4035 tree
4036 get_narrower (op, unsignedp_ptr)
4037 tree op;
4038 int *unsignedp_ptr;
4040 int uns = 0;
4041 int first = 1;
4042 tree win = op;
4044 while (TREE_CODE (op) == NOP_EXPR)
4046 int bitschange
4047 = (TYPE_PRECISION (TREE_TYPE (op))
4048 - TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op, 0))));
4050 /* Truncations are many-one so cannot be removed. */
4051 if (bitschange < 0)
4052 break;
4054 /* See what's inside this conversion. If we decide to strip it,
4055 we will set WIN. */
4056 op = TREE_OPERAND (op, 0);
4058 if (bitschange > 0)
4060 /* An extension: the outermost one can be stripped,
4061 but remember whether it is zero or sign extension. */
4062 if (first)
4063 uns = TREE_UNSIGNED (TREE_TYPE (op));
4064 /* Otherwise, if a sign extension has been stripped,
4065 only sign extensions can now be stripped;
4066 if a zero extension has been stripped, only zero-extensions. */
4067 else if (uns != TREE_UNSIGNED (TREE_TYPE (op)))
4068 break;
4069 first = 0;
4071 else /* bitschange == 0 */
4073 /* A change in nominal type can always be stripped, but we must
4074 preserve the unsignedness. */
4075 if (first)
4076 uns = TREE_UNSIGNED (TREE_TYPE (op));
4077 first = 0;
4080 win = op;
4083 if (TREE_CODE (op) == COMPONENT_REF
4084 /* Since type_for_size always gives an integer type. */
4085 && TREE_CODE (TREE_TYPE (op)) != REAL_TYPE
4086 /* Ensure field is laid out already. */
4087 && DECL_SIZE (TREE_OPERAND (op, 1)) != 0)
4089 unsigned HOST_WIDE_INT innerprec
4090 = tree_low_cst (DECL_SIZE (TREE_OPERAND (op, 1)), 1);
4091 tree type = (*lang_hooks.types.type_for_size) (innerprec,
4092 TREE_UNSIGNED (op));
4094 /* We can get this structure field in a narrower type that fits it,
4095 but the resulting extension to its nominal type (a fullword type)
4096 must satisfy the same conditions as for other extensions.
4098 Do this only for fields that are aligned (not bit-fields),
4099 because when bit-field insns will be used there is no
4100 advantage in doing this. */
4102 if (innerprec < TYPE_PRECISION (TREE_TYPE (op))
4103 && ! DECL_BIT_FIELD (TREE_OPERAND (op, 1))
4104 && (first || uns == TREE_UNSIGNED (TREE_OPERAND (op, 1)))
4105 && type != 0)
4107 if (first)
4108 uns = TREE_UNSIGNED (TREE_OPERAND (op, 1));
4109 win = build (COMPONENT_REF, type, TREE_OPERAND (op, 0),
4110 TREE_OPERAND (op, 1));
4111 TREE_SIDE_EFFECTS (win) = TREE_SIDE_EFFECTS (op);
4112 TREE_THIS_VOLATILE (win) = TREE_THIS_VOLATILE (op);
4115 *unsignedp_ptr = uns;
4116 return win;
4119 /* Nonzero if integer constant C has a value that is permissible
4120 for type TYPE (an INTEGER_TYPE). */
4123 int_fits_type_p (c, type)
4124 tree c, type;
4126 /* If the bounds of the type are integers, we can check ourselves.
4127 If not, but this type is a subtype, try checking against that.
4128 Otherwise, use force_fit_type, which checks against the precision. */
4129 if (TYPE_MAX_VALUE (type) != NULL_TREE
4130 && TYPE_MIN_VALUE (type) != NULL_TREE
4131 && TREE_CODE (TYPE_MAX_VALUE (type)) == INTEGER_CST
4132 && TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST)
4134 if (TREE_UNSIGNED (type))
4135 return (! INT_CST_LT_UNSIGNED (TYPE_MAX_VALUE (type), c)
4136 && ! INT_CST_LT_UNSIGNED (c, TYPE_MIN_VALUE (type))
4137 /* Negative ints never fit unsigned types. */
4138 && ! (TREE_INT_CST_HIGH (c) < 0
4139 && ! TREE_UNSIGNED (TREE_TYPE (c))));
4140 else
4141 return (! INT_CST_LT (TYPE_MAX_VALUE (type), c)
4142 && ! INT_CST_LT (c, TYPE_MIN_VALUE (type))
4143 /* Unsigned ints with top bit set never fit signed types. */
4144 && ! (TREE_INT_CST_HIGH (c) < 0
4145 && TREE_UNSIGNED (TREE_TYPE (c))));
4147 else if (TREE_CODE (type) == INTEGER_TYPE && TREE_TYPE (type) != 0)
4148 return int_fits_type_p (c, TREE_TYPE (type));
4149 else
4151 c = copy_node (c);
4152 TREE_TYPE (c) = type;
4153 return !force_fit_type (c, 0);
4157 /* Given a DECL or TYPE, return the scope in which it was declared, or
4158 NULL_TREE if there is no containing scope. */
4160 tree
4161 get_containing_scope (t)
4162 tree t;
4164 return (TYPE_P (t) ? TYPE_CONTEXT (t) : DECL_CONTEXT (t));
4167 /* Return the innermost context enclosing DECL that is
4168 a FUNCTION_DECL, or zero if none. */
4170 tree
4171 decl_function_context (decl)
4172 tree decl;
4174 tree context;
4176 if (TREE_CODE (decl) == ERROR_MARK)
4177 return 0;
4179 if (TREE_CODE (decl) == SAVE_EXPR)
4180 context = SAVE_EXPR_CONTEXT (decl);
4182 /* C++ virtual functions use DECL_CONTEXT for the class of the vtable
4183 where we look up the function at runtime. Such functions always take
4184 a first argument of type 'pointer to real context'.
4186 C++ should really be fixed to use DECL_CONTEXT for the real context,
4187 and use something else for the "virtual context". */
4188 else if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VINDEX (decl))
4189 context
4190 = TYPE_MAIN_VARIANT
4191 (TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (decl)))));
4192 else
4193 context = DECL_CONTEXT (decl);
4195 while (context && TREE_CODE (context) != FUNCTION_DECL)
4197 if (TREE_CODE (context) == BLOCK)
4198 context = BLOCK_SUPERCONTEXT (context);
4199 else
4200 context = get_containing_scope (context);
4203 return context;
4206 /* Return the innermost context enclosing DECL that is
4207 a RECORD_TYPE, UNION_TYPE or QUAL_UNION_TYPE, or zero if none.
4208 TYPE_DECLs and FUNCTION_DECLs are transparent to this function. */
4210 tree
4211 decl_type_context (decl)
4212 tree decl;
4214 tree context = DECL_CONTEXT (decl);
4216 while (context)
4218 if (TREE_CODE (context) == NAMESPACE_DECL)
4219 return NULL_TREE;
4221 if (TREE_CODE (context) == RECORD_TYPE
4222 || TREE_CODE (context) == UNION_TYPE
4223 || TREE_CODE (context) == QUAL_UNION_TYPE)
4224 return context;
4226 if (TREE_CODE (context) == TYPE_DECL
4227 || TREE_CODE (context) == FUNCTION_DECL)
4228 context = DECL_CONTEXT (context);
4230 else if (TREE_CODE (context) == BLOCK)
4231 context = BLOCK_SUPERCONTEXT (context);
4233 else
4234 /* Unhandled CONTEXT!? */
4235 abort ();
4237 return NULL_TREE;
4240 /* CALL is a CALL_EXPR. Return the declaration for the function
4241 called, or NULL_TREE if the called function cannot be
4242 determined. */
4244 tree
4245 get_callee_fndecl (call)
4246 tree call;
4248 tree addr;
4250 /* It's invalid to call this function with anything but a
4251 CALL_EXPR. */
4252 if (TREE_CODE (call) != CALL_EXPR)
4253 abort ();
4255 /* The first operand to the CALL is the address of the function
4256 called. */
4257 addr = TREE_OPERAND (call, 0);
4259 STRIP_NOPS (addr);
4261 /* If this is a readonly function pointer, extract its initial value. */
4262 if (DECL_P (addr) && TREE_CODE (addr) != FUNCTION_DECL
4263 && TREE_READONLY (addr) && ! TREE_THIS_VOLATILE (addr)
4264 && DECL_INITIAL (addr))
4265 addr = DECL_INITIAL (addr);
4267 /* If the address is just `&f' for some function `f', then we know
4268 that `f' is being called. */
4269 if (TREE_CODE (addr) == ADDR_EXPR
4270 && TREE_CODE (TREE_OPERAND (addr, 0)) == FUNCTION_DECL)
4271 return TREE_OPERAND (addr, 0);
4273 /* We couldn't figure out what was being called. */
4274 return NULL_TREE;
4277 /* Print debugging information about the obstack O, named STR. */
4279 void
4280 print_obstack_statistics (str, o)
4281 const char *str;
4282 struct obstack *o;
4284 struct _obstack_chunk *chunk = o->chunk;
4285 int n_chunks = 1;
4286 int n_alloc = 0;
4288 n_alloc += o->next_free - chunk->contents;
4289 chunk = chunk->prev;
4290 while (chunk)
4292 n_chunks += 1;
4293 n_alloc += chunk->limit - &chunk->contents[0];
4294 chunk = chunk->prev;
4296 fprintf (stderr, "obstack %s: %u bytes, %d chunks\n",
4297 str, n_alloc, n_chunks);
4300 /* Print debugging information about tree nodes generated during the compile,
4301 and any language-specific information. */
4303 void
4304 dump_tree_statistics ()
4306 #ifdef GATHER_STATISTICS
4307 int i;
4308 int total_nodes, total_bytes;
4309 #endif
4311 fprintf (stderr, "\n??? tree nodes created\n\n");
4312 #ifdef GATHER_STATISTICS
4313 fprintf (stderr, "Kind Nodes Bytes\n");
4314 fprintf (stderr, "-------------------------------------\n");
4315 total_nodes = total_bytes = 0;
4316 for (i = 0; i < (int) all_kinds; i++)
4318 fprintf (stderr, "%-20s %6d %9d\n", tree_node_kind_names[i],
4319 tree_node_counts[i], tree_node_sizes[i]);
4320 total_nodes += tree_node_counts[i];
4321 total_bytes += tree_node_sizes[i];
4323 fprintf (stderr, "-------------------------------------\n");
4324 fprintf (stderr, "%-20s %6d %9d\n", "Total", total_nodes, total_bytes);
4325 fprintf (stderr, "-------------------------------------\n");
4326 #else
4327 fprintf (stderr, "(No per-node statistics)\n");
4328 #endif
4329 print_type_hash_statistics ();
4330 (*lang_hooks.print_statistics) ();
4333 #define FILE_FUNCTION_FORMAT "_GLOBAL__%s_%s"
4335 /* Appends 6 random characters to TEMPLATE to (hopefully) avoid name
4336 clashes in cases where we can't reliably choose a unique name.
4338 Derived from mkstemp.c in libiberty. */
4340 static void
4341 append_random_chars (template)
4342 char *template;
4344 static const char letters[]
4345 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
4346 static unsigned HOST_WIDE_INT value;
4347 unsigned HOST_WIDE_INT v;
4349 if (! value)
4351 struct stat st;
4353 /* VALUE should be unique for each file and must not change between
4354 compiles since this can cause bootstrap comparison errors. */
4356 if (stat (main_input_filename, &st) < 0)
4358 /* This can happen when preprocessed text is shipped between
4359 machines, e.g. with bug reports. Assume that uniqueness
4360 isn't actually an issue. */
4361 value = 1;
4363 else
4365 /* In VMS, ino is an array, so we have to use both values. We
4366 conditionalize that. */
4367 #ifdef VMS
4368 #define INO_TO_INT(INO) ((int) (INO)[1] << 16 ^ (int) (INO)[2])
4369 #else
4370 #define INO_TO_INT(INO) INO
4371 #endif
4372 value = st.st_dev ^ INO_TO_INT (st.st_ino) ^ st.st_mtime;
4376 template += strlen (template);
4378 v = value;
4380 /* Fill in the random bits. */
4381 template[0] = letters[v % 62];
4382 v /= 62;
4383 template[1] = letters[v % 62];
4384 v /= 62;
4385 template[2] = letters[v % 62];
4386 v /= 62;
4387 template[3] = letters[v % 62];
4388 v /= 62;
4389 template[4] = letters[v % 62];
4390 v /= 62;
4391 template[5] = letters[v % 62];
4393 template[6] = '\0';
4396 /* P is a string that will be used in a symbol. Mask out any characters
4397 that are not valid in that context. */
4399 void
4400 clean_symbol_name (p)
4401 char *p;
4403 for (; *p; p++)
4404 if (! (ISALNUM (*p)
4405 #ifndef NO_DOLLAR_IN_LABEL /* this for `$'; unlikely, but... -- kr */
4406 || *p == '$'
4407 #endif
4408 #ifndef NO_DOT_IN_LABEL /* this for `.'; unlikely, but... */
4409 || *p == '.'
4410 #endif
4412 *p = '_';
4415 /* Generate a name for a function unique to this translation unit.
4416 TYPE is some string to identify the purpose of this function to the
4417 linker or collect2. */
4419 tree
4420 get_file_function_name_long (type)
4421 const char *type;
4423 char *buf;
4424 const char *p;
4425 char *q;
4427 if (first_global_object_name)
4428 p = first_global_object_name;
4429 else
4431 /* We don't have anything that we know to be unique to this translation
4432 unit, so use what we do have and throw in some randomness. */
4434 const char *name = weak_global_object_name;
4435 const char *file = main_input_filename;
4437 if (! name)
4438 name = "";
4439 if (! file)
4440 file = input_filename;
4442 q = (char *) alloca (7 + strlen (name) + strlen (file));
4444 sprintf (q, "%s%s", name, file);
4445 append_random_chars (q);
4446 p = q;
4449 buf = (char *) alloca (sizeof (FILE_FUNCTION_FORMAT) + strlen (p)
4450 + strlen (type));
4452 /* Set up the name of the file-level functions we may need.
4453 Use a global object (which is already required to be unique over
4454 the program) rather than the file name (which imposes extra
4455 constraints). */
4456 sprintf (buf, FILE_FUNCTION_FORMAT, type, p);
4458 /* Don't need to pull weird characters out of global names. */
4459 if (p != first_global_object_name)
4460 clean_symbol_name (buf + 11);
4462 return get_identifier (buf);
4465 /* If KIND=='I', return a suitable global initializer (constructor) name.
4466 If KIND=='D', return a suitable global clean-up (destructor) name. */
4468 tree
4469 get_file_function_name (kind)
4470 int kind;
4472 char p[2];
4474 p[0] = kind;
4475 p[1] = 0;
4477 return get_file_function_name_long (p);
4480 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4481 The result is placed in BUFFER (which has length BIT_SIZE),
4482 with one bit in each char ('\000' or '\001').
4484 If the constructor is constant, NULL_TREE is returned.
4485 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4487 tree
4488 get_set_constructor_bits (init, buffer, bit_size)
4489 tree init;
4490 char *buffer;
4491 int bit_size;
4493 int i;
4494 tree vals;
4495 HOST_WIDE_INT domain_min
4496 = tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (init))), 0);
4497 tree non_const_bits = NULL_TREE;
4499 for (i = 0; i < bit_size; i++)
4500 buffer[i] = 0;
4502 for (vals = TREE_OPERAND (init, 1);
4503 vals != NULL_TREE; vals = TREE_CHAIN (vals))
4505 if (!host_integerp (TREE_VALUE (vals), 0)
4506 || (TREE_PURPOSE (vals) != NULL_TREE
4507 && !host_integerp (TREE_PURPOSE (vals), 0)))
4508 non_const_bits
4509 = tree_cons (TREE_PURPOSE (vals), TREE_VALUE (vals), non_const_bits);
4510 else if (TREE_PURPOSE (vals) != NULL_TREE)
4512 /* Set a range of bits to ones. */
4513 HOST_WIDE_INT lo_index
4514 = tree_low_cst (TREE_PURPOSE (vals), 0) - domain_min;
4515 HOST_WIDE_INT hi_index
4516 = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
4518 if (lo_index < 0 || lo_index >= bit_size
4519 || hi_index < 0 || hi_index >= bit_size)
4520 abort ();
4521 for (; lo_index <= hi_index; lo_index++)
4522 buffer[lo_index] = 1;
4524 else
4526 /* Set a single bit to one. */
4527 HOST_WIDE_INT index
4528 = tree_low_cst (TREE_VALUE (vals), 0) - domain_min;
4529 if (index < 0 || index >= bit_size)
4531 error ("invalid initializer for bit string");
4532 return NULL_TREE;
4534 buffer[index] = 1;
4537 return non_const_bits;
4540 /* Expand (the constant part of) a SET_TYPE CONSTRUCTOR node.
4541 The result is placed in BUFFER (which is an array of bytes).
4542 If the constructor is constant, NULL_TREE is returned.
4543 Otherwise, a TREE_LIST of the non-constant elements is emitted. */
4545 tree
4546 get_set_constructor_bytes (init, buffer, wd_size)
4547 tree init;
4548 unsigned char *buffer;
4549 int wd_size;
4551 int i;
4552 int set_word_size = BITS_PER_UNIT;
4553 int bit_size = wd_size * set_word_size;
4554 int bit_pos = 0;
4555 unsigned char *bytep = buffer;
4556 char *bit_buffer = (char *) alloca (bit_size);
4557 tree non_const_bits = get_set_constructor_bits (init, bit_buffer, bit_size);
4559 for (i = 0; i < wd_size; i++)
4560 buffer[i] = 0;
4562 for (i = 0; i < bit_size; i++)
4564 if (bit_buffer[i])
4566 if (BYTES_BIG_ENDIAN)
4567 *bytep |= (1 << (set_word_size - 1 - bit_pos));
4568 else
4569 *bytep |= 1 << bit_pos;
4571 bit_pos++;
4572 if (bit_pos >= set_word_size)
4573 bit_pos = 0, bytep++;
4575 return non_const_bits;
4578 #if defined ENABLE_TREE_CHECKING && (GCC_VERSION >= 2007)
4579 /* Complain that the tree code of NODE does not match the expected CODE.
4580 FILE, LINE, and FUNCTION are of the caller. */
4582 void
4583 tree_check_failed (node, code, file, line, function)
4584 const tree node;
4585 enum tree_code code;
4586 const char *file;
4587 int line;
4588 const char *function;
4590 internal_error ("tree check: expected %s, have %s in %s, at %s:%d",
4591 tree_code_name[code], tree_code_name[TREE_CODE (node)],
4592 function, trim_filename (file), line);
4595 /* Similar to above, except that we check for a class of tree
4596 code, given in CL. */
4598 void
4599 tree_class_check_failed (node, cl, file, line, function)
4600 const tree node;
4601 int cl;
4602 const char *file;
4603 int line;
4604 const char *function;
4606 internal_error
4607 ("tree check: expected class '%c', have '%c' (%s) in %s, at %s:%d",
4608 cl, TREE_CODE_CLASS (TREE_CODE (node)),
4609 tree_code_name[TREE_CODE (node)], function, trim_filename (file), line);
4612 #endif /* ENABLE_TREE_CHECKING */
4614 /* For a new vector type node T, build the information necessary for
4615 debugging output. */
4617 static void
4618 finish_vector_type (t)
4619 tree t;
4621 layout_type (t);
4624 tree index = build_int_2 (TYPE_VECTOR_SUBPARTS (t) - 1, 0);
4625 tree array = build_array_type (TREE_TYPE (t),
4626 build_index_type (index));
4627 tree rt = make_node (RECORD_TYPE);
4629 TYPE_FIELDS (rt) = build_decl (FIELD_DECL, get_identifier ("f"), array);
4630 DECL_CONTEXT (TYPE_FIELDS (rt)) = rt;
4631 layout_type (rt);
4632 TYPE_DEBUG_REPRESENTATION_TYPE (t) = rt;
4633 /* In dwarfout.c, type lookup uses TYPE_UID numbers. We want to output
4634 the representation type, and we want to find that die when looking up
4635 the vector type. This is most easily achieved by making the TYPE_UID
4636 numbers equal. */
4637 TYPE_UID (rt) = TYPE_UID (t);
4641 /* Create nodes for all integer types (and error_mark_node) using the sizes
4642 of C datatypes. The caller should call set_sizetype soon after calling
4643 this function to select one of the types as sizetype. */
4645 void
4646 build_common_tree_nodes (signed_char)
4647 int signed_char;
4649 error_mark_node = make_node (ERROR_MARK);
4650 TREE_TYPE (error_mark_node) = error_mark_node;
4652 initialize_sizetypes ();
4654 /* Define both `signed char' and `unsigned char'. */
4655 signed_char_type_node = make_signed_type (CHAR_TYPE_SIZE);
4656 unsigned_char_type_node = make_unsigned_type (CHAR_TYPE_SIZE);
4658 /* Define `char', which is like either `signed char' or `unsigned char'
4659 but not the same as either. */
4660 char_type_node
4661 = (signed_char
4662 ? make_signed_type (CHAR_TYPE_SIZE)
4663 : make_unsigned_type (CHAR_TYPE_SIZE));
4665 short_integer_type_node = make_signed_type (SHORT_TYPE_SIZE);
4666 short_unsigned_type_node = make_unsigned_type (SHORT_TYPE_SIZE);
4667 integer_type_node = make_signed_type (INT_TYPE_SIZE);
4668 unsigned_type_node = make_unsigned_type (INT_TYPE_SIZE);
4669 long_integer_type_node = make_signed_type (LONG_TYPE_SIZE);
4670 long_unsigned_type_node = make_unsigned_type (LONG_TYPE_SIZE);
4671 long_long_integer_type_node = make_signed_type (LONG_LONG_TYPE_SIZE);
4672 long_long_unsigned_type_node = make_unsigned_type (LONG_LONG_TYPE_SIZE);
4674 intQI_type_node = make_signed_type (GET_MODE_BITSIZE (QImode));
4675 intHI_type_node = make_signed_type (GET_MODE_BITSIZE (HImode));
4676 intSI_type_node = make_signed_type (GET_MODE_BITSIZE (SImode));
4677 intDI_type_node = make_signed_type (GET_MODE_BITSIZE (DImode));
4678 intTI_type_node = make_signed_type (GET_MODE_BITSIZE (TImode));
4680 unsigned_intQI_type_node = make_unsigned_type (GET_MODE_BITSIZE (QImode));
4681 unsigned_intHI_type_node = make_unsigned_type (GET_MODE_BITSIZE (HImode));
4682 unsigned_intSI_type_node = make_unsigned_type (GET_MODE_BITSIZE (SImode));
4683 unsigned_intDI_type_node = make_unsigned_type (GET_MODE_BITSIZE (DImode));
4684 unsigned_intTI_type_node = make_unsigned_type (GET_MODE_BITSIZE (TImode));
4687 /* Call this function after calling build_common_tree_nodes and set_sizetype.
4688 It will create several other common tree nodes. */
4690 void
4691 build_common_tree_nodes_2 (short_double)
4692 int short_double;
4694 /* Define these next since types below may used them. */
4695 integer_zero_node = build_int_2 (0, 0);
4696 integer_one_node = build_int_2 (1, 0);
4697 integer_minus_one_node = build_int_2 (-1, -1);
4699 size_zero_node = size_int (0);
4700 size_one_node = size_int (1);
4701 bitsize_zero_node = bitsize_int (0);
4702 bitsize_one_node = bitsize_int (1);
4703 bitsize_unit_node = bitsize_int (BITS_PER_UNIT);
4705 void_type_node = make_node (VOID_TYPE);
4706 layout_type (void_type_node);
4708 /* We are not going to have real types in C with less than byte alignment,
4709 so we might as well not have any types that claim to have it. */
4710 TYPE_ALIGN (void_type_node) = BITS_PER_UNIT;
4711 TYPE_USER_ALIGN (void_type_node) = 0;
4713 null_pointer_node = build_int_2 (0, 0);
4714 TREE_TYPE (null_pointer_node) = build_pointer_type (void_type_node);
4715 layout_type (TREE_TYPE (null_pointer_node));
4717 ptr_type_node = build_pointer_type (void_type_node);
4718 const_ptr_type_node
4719 = build_pointer_type (build_type_variant (void_type_node, 1, 0));
4721 float_type_node = make_node (REAL_TYPE);
4722 TYPE_PRECISION (float_type_node) = FLOAT_TYPE_SIZE;
4723 layout_type (float_type_node);
4725 double_type_node = make_node (REAL_TYPE);
4726 if (short_double)
4727 TYPE_PRECISION (double_type_node) = FLOAT_TYPE_SIZE;
4728 else
4729 TYPE_PRECISION (double_type_node) = DOUBLE_TYPE_SIZE;
4730 layout_type (double_type_node);
4732 long_double_type_node = make_node (REAL_TYPE);
4733 TYPE_PRECISION (long_double_type_node) = LONG_DOUBLE_TYPE_SIZE;
4734 layout_type (long_double_type_node);
4736 complex_integer_type_node = make_node (COMPLEX_TYPE);
4737 TREE_TYPE (complex_integer_type_node) = integer_type_node;
4738 layout_type (complex_integer_type_node);
4740 complex_float_type_node = make_node (COMPLEX_TYPE);
4741 TREE_TYPE (complex_float_type_node) = float_type_node;
4742 layout_type (complex_float_type_node);
4744 complex_double_type_node = make_node (COMPLEX_TYPE);
4745 TREE_TYPE (complex_double_type_node) = double_type_node;
4746 layout_type (complex_double_type_node);
4748 complex_long_double_type_node = make_node (COMPLEX_TYPE);
4749 TREE_TYPE (complex_long_double_type_node) = long_double_type_node;
4750 layout_type (complex_long_double_type_node);
4753 tree t;
4754 BUILD_VA_LIST_TYPE (t);
4756 /* Many back-ends define record types without seting TYPE_NAME.
4757 If we copied the record type here, we'd keep the original
4758 record type without a name. This breaks name mangling. So,
4759 don't copy record types and let c_common_nodes_and_builtins()
4760 declare the type to be __builtin_va_list. */
4761 if (TREE_CODE (t) != RECORD_TYPE)
4762 t = build_type_copy (t);
4764 va_list_type_node = t;
4767 unsigned_V4SI_type_node
4768 = make_vector (V4SImode, unsigned_intSI_type_node, 1);
4769 unsigned_V2SI_type_node
4770 = make_vector (V2SImode, unsigned_intSI_type_node, 1);
4771 unsigned_V2DI_type_node
4772 = make_vector (V2DImode, unsigned_intDI_type_node, 1);
4773 unsigned_V4HI_type_node
4774 = make_vector (V4HImode, unsigned_intHI_type_node, 1);
4775 unsigned_V8QI_type_node
4776 = make_vector (V8QImode, unsigned_intQI_type_node, 1);
4777 unsigned_V8HI_type_node
4778 = make_vector (V8HImode, unsigned_intHI_type_node, 1);
4779 unsigned_V16QI_type_node
4780 = make_vector (V16QImode, unsigned_intQI_type_node, 1);
4782 V16SF_type_node = make_vector (V16SFmode, float_type_node, 0);
4783 V4SF_type_node = make_vector (V4SFmode, float_type_node, 0);
4784 V4SI_type_node = make_vector (V4SImode, intSI_type_node, 0);
4785 V2SI_type_node = make_vector (V2SImode, intSI_type_node, 0);
4786 V2DI_type_node = make_vector (V2DImode, intDI_type_node, 0);
4787 V4HI_type_node = make_vector (V4HImode, intHI_type_node, 0);
4788 V8QI_type_node = make_vector (V8QImode, intQI_type_node, 0);
4789 V8HI_type_node = make_vector (V8HImode, intHI_type_node, 0);
4790 V2SF_type_node = make_vector (V2SFmode, float_type_node, 0);
4791 V2DF_type_node = make_vector (V2DFmode, double_type_node, 0);
4792 V16QI_type_node = make_vector (V16QImode, intQI_type_node, 0);
4795 /* Returns a vector tree node given a vector mode, the inner type, and
4796 the signness. */
4798 static tree
4799 make_vector (mode, innertype, unsignedp)
4800 enum machine_mode mode;
4801 tree innertype;
4802 int unsignedp;
4804 tree t;
4806 t = make_node (VECTOR_TYPE);
4807 TREE_TYPE (t) = innertype;
4808 TYPE_MODE (t) = mode;
4809 TREE_UNSIGNED (TREE_TYPE (t)) = unsignedp;
4810 finish_vector_type (t);
4812 return t;
4815 /* Given an initializer INIT, return TRUE if INIT is zero or some
4816 aggregate of zeros. Otherwise return FALSE. */
4818 bool
4819 initializer_zerop (init)
4820 tree init;
4822 STRIP_NOPS (init);
4824 switch (TREE_CODE (init))
4826 case INTEGER_CST:
4827 return integer_zerop (init);
4828 case REAL_CST:
4829 return real_zerop (init)
4830 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (init));
4831 case COMPLEX_CST:
4832 return integer_zerop (init)
4833 || (real_zerop (init)
4834 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_REALPART (init)))
4835 && ! REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (TREE_IMAGPART (init))));
4836 case CONSTRUCTOR:
4838 if (AGGREGATE_TYPE_P (TREE_TYPE (init)))
4840 tree aggr_init = TREE_OPERAND (init, 1);
4842 while (aggr_init)
4844 if (! initializer_zerop (TREE_VALUE (aggr_init)))
4845 return false;
4846 aggr_init = TREE_CHAIN (aggr_init);
4848 return true;
4850 return false;
4852 default:
4853 return false;
4857 #include "gt-tree.h"