OSDN Git Service

* crontab, doc_exclude, update_branch_version, update_version,
[pf3gnuchains/gcc-fork.git] / gcc / stor-layout.c
index eadb973..152eff1 100644 (file)
@@ -1,6 +1,6 @@
 /* C-compiler utilities for types and variables storage layout
    Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1996, 1998,
-   1999, 2000 Free Software Foundation, Inc.
+   1999, 2000, 2001 Free Software Foundation, Inc.
 
 This file is part of GNU CC.
 
@@ -50,6 +50,11 @@ unsigned int maximum_field_alignment;
    May be overridden by front-ends.  */
 unsigned int set_alignment = 0;
 
+/* Nonzero if all REFERENCE_TYPEs are internal and hence should be
+   allocated in Pmode, not ptr_mode.   Set only by internal_reference_types
+   called only by a front end.  */
+static int reference_types_internal = 0;
+
 static void finalize_record_size       PARAMS ((record_layout_info));
 static void finalize_type_size         PARAMS ((tree));
 static void place_union_field          PARAMS ((record_layout_info, tree));
@@ -64,6 +69,15 @@ static tree pending_sizes;
 
 int immediate_size_expand;
 
+/* Show that REFERENCE_TYPES are internal and should be Pmode.  Called only
+   by front end.  */
+
+void
+internal_reference_types ()
+{
+  reference_types_internal = 1;
+}
+
 /* Get a list of all the objects put on the pending sizes list.  */
 
 tree
@@ -80,6 +94,30 @@ get_pending_sizes ()
   return chain;
 }
 
+/* Return non-zero if EXPR is present on the pending sizes list.  */
+
+int
+is_pending_size (expr)
+     tree expr;
+{
+  tree t;
+
+  for (t = pending_sizes; t; t = TREE_CHAIN (t))
+    if (TREE_VALUE (t) == expr)
+      return 1;
+  return 0;
+}
+
+/* Add EXPR to the pending sizes list.  */
+
+void
+put_pending_size (expr)
+     tree expr;
+{
+  if (TREE_CODE (expr) == SAVE_EXPR)
+    pending_sizes = tree_cons (NULL_TREE, expr, pending_sizes);
+}
+
 /* Put a chain of objects into the pending sizes list, which must be
    empty.  */
 
@@ -140,7 +178,7 @@ variable_size (size)
        that determine sizes for variable size objects.  */
     ;
   else
-    pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
+    put_pending_size (size);
 
   return size;
 }
@@ -227,6 +265,8 @@ int_mode_for_mode (mode)
     case MODE_COMPLEX_INT:
     case MODE_COMPLEX_FLOAT:
     case MODE_FLOAT:
+    case MODE_VECTOR_INT:
+    case MODE_VECTOR_FLOAT:
       mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
       break;
 
@@ -330,7 +370,10 @@ layout_decl (decl, known_align)
       && (DECL_ALIGN (decl) == 0
          || (! (code == FIELD_DECL && DECL_PACKED (decl))
              && TYPE_ALIGN (type) > DECL_ALIGN (decl))))
-    DECL_ALIGN (decl) = TYPE_ALIGN (type);
+    {        
+      DECL_ALIGN (decl) = TYPE_ALIGN (type);
+      DECL_USER_ALIGN (decl) = TYPE_USER_ALIGN (type);
+    }
 
   /* For fields, set the bit field type and update the alignment.  */
   if (code == FIELD_DECL)
@@ -339,7 +382,10 @@ layout_decl (decl, known_align)
       if (maximum_field_alignment != 0)
        DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
       else if (DECL_PACKED (decl))
-       DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
+       {
+         DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
+         DECL_USER_ALIGN (decl) = 0;
+       }
     }
 
   /* See if we can use an ordinary integer mode for a bit-field. 
@@ -410,7 +456,7 @@ start_record_layout (t)
      tree t;
 {
   record_layout_info rli 
-    = (record_layout_info) xmalloc (sizeof (struct record_layout_info));
+    = (record_layout_info) xmalloc (sizeof (struct record_layout_info_s));
 
   rli->t = t;
 
@@ -572,14 +618,27 @@ place_union_field (rli, field)
      record_layout_info rli;
      tree field;
 {
+  unsigned int desired_align;
+
   layout_decl (field, 0);
   
   DECL_FIELD_OFFSET (field) = size_zero_node;
   DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
-  DECL_OFFSET_ALIGN (field) = BIGGEST_ALIGNMENT;
+  SET_DECL_OFFSET_ALIGN (field, BIGGEST_ALIGNMENT);
+
+  desired_align = DECL_ALIGN (field);
+
+#ifdef BIGGEST_FIELD_ALIGNMENT
+  /* Some targets (i.e. i386) limit union field alignment
+     to a lower boundary than alignment of variables unless
+     it was overridden by attribute aligned.  */
+  if (! DECL_USER_ALIGN (field))
+    desired_align =
+      MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
+#endif
 
   /* Union must be at least as aligned as any field requires.  */
-  rli->record_align = MAX (rli->record_align, DECL_ALIGN (field));
+  rli->record_align = MAX (rli->record_align, desired_align);
 
 #ifdef PCC_BITFIELD_TYPE_MATTERS
   /* On the m88000, a bit field of declare type `int' forces the
@@ -615,9 +674,13 @@ place_field (rli, field)
      record as it presently stands.  */
   unsigned int known_align;
   unsigned int actual_align;
+  unsigned int user_align;
   /* The type of this field.  */
   tree type = TREE_TYPE (field);
  
+  if (TREE_CODE (field) == ERROR_MARK || TREE_CODE (type) == ERROR_MARK)
+      return;
+
   /* If FIELD is static, then treat it like a separate variable, not
      really like a structure field.  If it is a FUNCTION_DECL, it's a
      method.  In both cases, all we do is lay out the decl, and we do
@@ -660,15 +723,23 @@ place_field (rli, field)
      packed field, use the alignment as specified, disregarding what
      the type would want.  */
   desired_align = DECL_ALIGN (field);
+  user_align = DECL_USER_ALIGN (field);
   layout_decl (field, known_align);
   if (! DECL_PACKED (field))
-    desired_align = DECL_ALIGN (field);
+    {
+      desired_align = DECL_ALIGN (field);
+      user_align = DECL_USER_ALIGN (field);
+    }
 
-  /* Some targets (i.e. VMS) limit struct field alignment
-     to a lower boundary than alignment of variables.  */
+  /* Some targets (i.e. i386, VMS) limit struct field alignment
+     to a lower boundary than alignment of variables unless
+     it was overridden by attribute aligned.  */
 #ifdef BIGGEST_FIELD_ALIGNMENT
-  desired_align = MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
+  if (! user_align)
+    desired_align
+      = MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
 #endif
+
 #ifdef ADJUST_FIELD_ALIGN
   desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
 #endif
@@ -801,7 +872,7 @@ place_field (rli, field)
       && ! DECL_PACKED (field)
       && ! integer_zerop (DECL_SIZE (field))
       && host_integerp (DECL_SIZE (field), 1)
-      && host_integerp (rli->size, 1)
+      && host_integerp (rli->offset, 1)
       && host_integerp (TYPE_SIZE (type), 1))
     {
       unsigned int type_align = TYPE_ALIGN (type);
@@ -832,7 +903,7 @@ place_field (rli, field)
   normalize_rli (rli);
   DECL_FIELD_OFFSET (field) = rli->offset;
   DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
-  DECL_OFFSET_ALIGN (field) = rli->offset_align;
+  SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
 
   /* If this field ended up more aligned than we thought it would be (we
      approximate this by seeing if its position changed), lay out the field
@@ -861,7 +932,8 @@ place_field (rli, field)
      is printed in finish_struct.  */
   if (DECL_SIZE (field) == 0)
     /* Do nothing.  */;
-  else if (! TREE_CONSTANT (DECL_SIZE_UNIT (field)))
+  else if (TREE_CODE (DECL_SIZE_UNIT (field)) != INTEGER_CST
+          || TREE_CONSTANT_OVERFLOW (DECL_SIZE_UNIT (field)))
     {
       rli->offset
        = size_binop (PLUS_EXPR, rli->offset,
@@ -902,6 +974,7 @@ finalize_record_size (rli)
 #else
   TYPE_ALIGN (rli->t) = MAX (TYPE_ALIGN (rli->t), rli->record_align);
 #endif
+  TYPE_USER_ALIGN (rli->t) = 1;
 
   /* Compute the size so far.  Be sure to allow for extra bits in the
      size in bytes.  We have guaranteed above that it will be no more
@@ -925,7 +998,7 @@ finalize_record_size (rli)
   TYPE_SIZE (rli->t) = ROUND_TYPE_SIZE (rli->t, unpadded_size,
                                        TYPE_ALIGN (rli->t));
   TYPE_SIZE_UNIT (rli->t)
-    = ROUND_TYPE_SIZE_UNIT (rli->t, unpaded_size_unit,
+    = ROUND_TYPE_SIZE_UNIT (rli->t, unpadded_size_unit,
                            TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
 #else
   TYPE_SIZE (rli->t) = round_up (unpadded_size, TYPE_ALIGN (rli->t));
@@ -963,7 +1036,7 @@ finalize_record_size (rli)
 
          if (TYPE_NAME (rli->t))
            {
-             char *name;
+             const char *name;
 
              if (TREE_CODE (TYPE_NAME (rli->t)) == IDENTIFIER_NODE)
                name = IDENTIFIER_POINTER (TYPE_NAME (rli->t));
@@ -1026,7 +1099,7 @@ compute_record_mode (type)
       /* Must be BLKmode if any field crosses a word boundary,
         since extract_bit_field can't handle that in registers.  */
       if (bitpos / BITS_PER_WORD
-         != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
+         != ((tree_low_cst (DECL_SIZE (field), 1) + bitpos - 1)
              / BITS_PER_WORD)
          /* But there is no problem if the field is entire words.  */
          && tree_low_cst (DECL_SIZE (field), 1) % BITS_PER_WORD != 0)
@@ -1035,19 +1108,20 @@ compute_record_mode (type)
       /* If this field is the whole struct, remember its mode so
         that, say, we can put a double in a class into a DF
         register instead of forcing it to live in the stack.  */
-      if (field == TYPE_FIELDS (type) && TREE_CHAIN (field) == 0)
+      if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
        mode = DECL_MODE (field);
 
-#ifdef STRUCT_FORCE_BLK
+#ifdef MEMBER_TYPE_FORCES_BLK
       /* With some targets, eg. c4x, it is sub-optimal
         to access an aligned BLKmode structure as a scalar.  */
-      if (mode == VOIDmode && STRUCT_FORCE_BLK (field))
+      if (mode == VOIDmode && MEMBER_TYPE_FORCES_BLK (field))
        return;
-#endif /* STRUCT_FORCE_BLK  */
+#endif /* MEMBER_TYPE_FORCES_BLK  */
     }
 
-  if (mode != VOIDmode)
-    /* We only have one real field; use its mode.  */
+  /* If we only have one real field; use its mode.  This only applies to
+     RECORD_TYPE.  This does not apply to unions.  */
+  if (TREE_CODE (type) == RECORD_TYPE && mode != VOIDmode)
     TYPE_MODE (type) = mode;
   else
     TYPE_MODE (type) = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
@@ -1083,7 +1157,10 @@ finalize_type_size (type)
          || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
              && TREE_CODE (type) != QUAL_UNION_TYPE
              && TREE_CODE (type) != ARRAY_TYPE)))
-    TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
+    {
+      TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
+      TYPE_USER_ALIGN (type) = 0;
+    }
 
   /* Do machine-dependent extra alignment.  */
 #ifdef ROUND_TYPE_ALIGN
@@ -1133,6 +1210,7 @@ finalize_type_size (type)
       tree size = TYPE_SIZE (type);
       tree size_unit = TYPE_SIZE_UNIT (type);
       unsigned int align = TYPE_ALIGN (type);
+      unsigned int user_align = TYPE_USER_ALIGN (type);
       enum machine_mode mode = TYPE_MODE (type);
 
       /* Copy it into all variants.  */
@@ -1143,6 +1221,7 @@ finalize_type_size (type)
          TYPE_SIZE (variant) = size;
          TYPE_SIZE_UNIT (variant) = size_unit;
          TYPE_ALIGN (variant) = align;
+         TYPE_USER_ALIGN (variant) = user_align;
          TYPE_MODE (variant) = mode;
        }
     }
@@ -1162,6 +1241,9 @@ finish_record_layout (rli)
   /* Compute the TYPE_MODE for the record.  */
   compute_record_mode (rli->t);
 
+  /* Perform any last tweaks to the TYPE_SIZE, etc.  */
+  finalize_type_size (rli->t);
+
   /* Lay out any static members.  This is done now because their type
      may use the record's type.  */
   while (rli->pending_statics)
@@ -1170,9 +1252,6 @@ finish_record_layout (rli)
       rli->pending_statics = TREE_CHAIN (rli->pending_statics);
     }
 
-  /* Perform any last tweaks to the TYPE_SIZE, etc.  */
-  finalize_type_size (rli->t);
-
   /* Clean up.  */
   free (rli);
 }
@@ -1191,8 +1270,6 @@ void
 layout_type (type)
      tree type;
 {
-  int old;
-
   if (type == 0)
     abort ();
 
@@ -1200,16 +1277,6 @@ layout_type (type)
   if (TYPE_SIZE (type))
     return;
 
-  /* Make sure all nodes we allocate are not momentary; they must last
-     past the current statement.  */
-  old = suspend_momentary ();
-
-  /* Put all our nodes into the same obstack as the type.  Also,
-     make expressions saveable (this is a no-op for permanent types).  */
-
-  push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
-  saveable_allocation ();
-
   switch (TREE_CODE (type))
     {
     case LANG_TYPE:
@@ -1253,9 +1320,21 @@ layout_type (type)
       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
       break;
 
+    case VECTOR_TYPE:
+      {
+       tree subtype;
+
+       subtype = TREE_TYPE (type);
+       TREE_UNSIGNED (type) = TREE_UNSIGNED (subtype);
+       TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
+       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
+      }
+      break;
+
     case VOID_TYPE:
       /* This is an incomplete type and so doesn't have a size.  */
       TYPE_ALIGN (type) = 1;
+      TYPE_USER_ALIGN (type) = 0;
       TYPE_MODE (type) = VOIDmode;
       break;
 
@@ -1274,11 +1353,17 @@ layout_type (type)
 
     case POINTER_TYPE:
     case REFERENCE_TYPE:
-      TYPE_MODE (type) = ptr_mode;
-      TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
-      TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
-      TREE_UNSIGNED (type) = 1;
-      TYPE_PRECISION (type) = POINTER_SIZE;
+      {
+       int nbits = ((TREE_CODE (type) == REFERENCE_TYPE
+                     && reference_types_internal)
+                    ? GET_MODE_BITSIZE (Pmode) : POINTER_SIZE);
+
+       TYPE_MODE (type) = nbits == POINTER_SIZE ? ptr_mode : Pmode;
+       TYPE_SIZE (type) = bitsize_int (nbits);
+       TYPE_SIZE_UNIT (type) = size_int (nbits / BITS_PER_UNIT);
+       TREE_UNSIGNED (type) = 1;
+       TYPE_PRECISION (type) = nbits;
+      }
       break;
 
     case ARRAY_TYPE:
@@ -1297,22 +1382,6 @@ layout_type (type)
            tree length;
            tree element_size;
 
-           /* If UB is max (lb - 1, x), remove the MAX_EXPR since the
-              test for negative below covers it.  */
-           if (TREE_CODE (ub) == MAX_EXPR
-               && TREE_CODE (TREE_OPERAND (ub, 0)) == MINUS_EXPR
-               && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 0), 1))
-               && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 0), 0),
-                                   lb, 0))
-             ub = TREE_OPERAND (ub, 1);
-           else if (TREE_CODE (ub) == MAX_EXPR
-                    && TREE_CODE (TREE_OPERAND (ub, 1)) == MINUS_EXPR
-                    && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 1), 1))
-                    && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 1),
-                                                      0),
-                                        lb, 0))
-             ub = TREE_OPERAND (ub, 0);
-
            /* The initial subtraction should happen in the original type so
               that (possible) negative values are handled appropriately.  */
            length = size_binop (PLUS_EXPR, size_one_node,
@@ -1321,23 +1390,17 @@ layout_type (type)
                                                       TREE_TYPE (lb),
                                                       ub, lb))));
 
-           /* If neither bound is a constant and sizetype is signed, make
-              sure the size is never negative.  We should really do this
-              if *either* bound is non-constant, but this is the best
-              compromise between C and Ada.  */
-           if (! TREE_UNSIGNED (sizetype)
-               && TREE_CODE (TYPE_MIN_VALUE (index)) != INTEGER_CST
-               && TREE_CODE (TYPE_MAX_VALUE (index)) != INTEGER_CST)
-             length = size_binop (MAX_EXPR, length, size_zero_node);
-
            /* Special handling for arrays of bits (for Chill).  */
            element_size = TYPE_SIZE (element);
-           if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element))
+           if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element)
+               && (integer_zerop (TYPE_MAX_VALUE (element))
+                   || integer_onep (TYPE_MAX_VALUE (element)))
+               && host_integerp (TYPE_MIN_VALUE (element), 1))
              {
                HOST_WIDE_INT maxvalue
-                 = TREE_INT_CST_LOW (TYPE_MAX_VALUE (element));
+                 = tree_low_cst (TYPE_MAX_VALUE (element), 1);
                HOST_WIDE_INT minvalue
-                 = TREE_INT_CST_LOW (TYPE_MIN_VALUE (element));
+                 = tree_low_cst (TYPE_MIN_VALUE (element), 1);
 
                if (maxvalue - minvalue == 1
                    && (maxvalue == 1 || maxvalue == 0))
@@ -1387,6 +1450,9 @@ layout_type (type)
 
        TYPE_MODE (type) = BLKmode;
        if (TYPE_SIZE (type) != 0
+#ifdef MEMBER_TYPE_FORCES_BLK
+           && ! MEMBER_TYPE_FORCES_BLK (type)
+#endif
            /* BLKmode elements force BLKmode aggregate;
               else extract/store fields may lose.  */
            && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
@@ -1460,6 +1526,7 @@ layout_type (type)
          TYPE_SIZE (type) = bitsize_int (rounded_size);
          TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
          TYPE_ALIGN (type) = alignment;
+         TYPE_USER_ALIGN (type) = 0;
          TYPE_PRECISION (type) = size_in_bits;
        }
       break;
@@ -1468,6 +1535,7 @@ layout_type (type)
       /* The size may vary in different languages, so the language front end
         should fill in the size.  */
       TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
+      TYPE_USER_ALIGN (type) = 0;
       TYPE_MODE  (type) = BLKmode;
       break;
 
@@ -1483,13 +1551,18 @@ layout_type (type)
       && TREE_CODE (type) != QUAL_UNION_TYPE)
     finalize_type_size (type);
 
-  pop_obstacks ();
-  resume_momentary (old);
-
   /* If this type is created before sizetype has been permanently set,
      record it so set_sizetype can fix it up.  */
   if (! sizetype_set)
     early_type_list = tree_cons (NULL_TREE, type, early_type_list);
+
+  /* If an alias set has been set for this aggregate when it was incomplete,
+     force it into alias set 0.
+     This is too conservative, but we cannot call record_component_aliases
+     here because some frontends still change the aggregates after
+     layout_type.  */
+  if (AGGREGATE_TYPE_P (type) && TYPE_ALIAS_SET_KNOWN_P (type))
+    TYPE_ALIAS_SET (type) = 0;
 }
 \f
 /* Create and return a type for signed integers of PRECISION bits.  */
@@ -1534,6 +1607,7 @@ initialize_sizetypes ()
 
   TYPE_MODE (t) = SImode;
   TYPE_ALIGN (t) = GET_MODE_ALIGNMENT (SImode);
+  TYPE_USER_ALIGN (t) = 0;
   TYPE_SIZE (t) = build_int_2 (GET_MODE_BITSIZE (SImode), 0);
   TYPE_SIZE_UNIT (t) = build_int_2 (GET_MODE_SIZE (SImode), 0);
   TREE_UNSIGNED (t) = 1;
@@ -1606,7 +1680,7 @@ set_sizetype (type)
   TYPE_NAME (bitsizetype) = get_identifier ("bit_size_type");
 
   /* Show is a sizetype, is a main type, and has no pointers to it.  */
-  for (i = 0; i < sizeof sizetype_tab / sizeof sizetype_tab[0]; i++)
+  for (i = 0; i < ARRAY_SIZE (sizetype_tab); i++)
     {
       TYPE_IS_SIZETYPE (sizetype_tab[i]) = 1;
       TYPE_MAIN_VARIANT (sizetype_tab[i]) = sizetype_tab[i];