GCC Wikia
Advertisement

このページを編集する際は,編集に関する方針に従ってください.[]

概要[]

引数[]

実装[]

1577 /* Calculate the mode, size, and alignment for TYPE.
1578    For an array type, calculate the element separation as well.
1579    Record TYPE on the chain of permanent or temporary types
1580    so that dbxout will find out about it.
1581 
1582    TYPE_SIZE of a type is nonzero if the type has been laid out already.
1583    layout_type does nothing on such a type.
1584 
1585    If the type is incomplete, its TYPE_SIZE remains zero.  */
1586 
1587 void
1588 layout_type (tree type)
1589 {
1590   gcc_assert (type);
1591 
1592   if (type == error_mark_node)
1593     return;
1594 
1595   /* Do nothing if type has been laid out before.  */
1596   if (TYPE_SIZE (type))
1597     return;
1598 
1599   switch (TREE_CODE (type))
1600     {
1601     case LANG_TYPE:
1602       /* This kind of type is the responsibility
1603          of the language-specific code.  */
1604       gcc_unreachable ();
1605 
1606     case BOOLEAN_TYPE:  /* Used for Java, Pascal, and Chill.  */
1607       if (TYPE_PRECISION (type) == 0)
1608         TYPE_PRECISION (type) = 1; /* default to one byte/boolean.  */
1609 
1610       /* ... fall through ...  */
1611 
1612     case INTEGER_TYPE:
1613     case ENUMERAL_TYPE:
1614     case CHAR_TYPE:
1615       if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
1616           && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
1617         TYPE_UNSIGNED (type) = 1;
1618 
1619       TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
1620                                                  MODE_INT);
1621       TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1622       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1623       break;
1624 
1625     case REAL_TYPE:
1626       TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
1627       TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1628       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1629       break;
1630 
1631     case COMPLEX_TYPE:
1632       TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TREE_TYPE (type));
1633       TYPE_MODE (type)
1634         = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
1635                          (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE
1636                           ? MODE_COMPLEX_FLOAT : MODE_COMPLEX_INT),
1637                          0);
1638       TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1639       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1640       break;
1641 
1642     case VECTOR_TYPE:
1643       {
1644         int nunits = TYPE_VECTOR_SUBPARTS (type);
1645         tree nunits_tree = build_int_cst (NULL_TREE, nunits);
1646         tree innertype = TREE_TYPE (type);
1647 
1648         gcc_assert (!(nunits & (nunits - 1)));
1649 
1650         /* Find an appropriate mode for the vector type.  */
1651         if (TYPE_MODE (type) == VOIDmode)
1652           {
1653             enum machine_mode innermode = TYPE_MODE (innertype);
1654             enum machine_mode mode;
1655 
1656             /* First, look for a supported vector type.  */
1657             if (GET_MODE_CLASS (innermode) == MODE_FLOAT)
1658               mode = MIN_MODE_VECTOR_FLOAT;
1659             else
1660               mode = MIN_MODE_VECTOR_INT;
1661 
1662             for (; mode != VOIDmode ; mode = GET_MODE_WIDER_MODE (mode))
1663               if (GET_MODE_NUNITS (mode) == nunits
1664                   && GET_MODE_INNER (mode) == innermode
1665                   && targetm.vector_mode_supported_p (mode))
1666                 break;
1667 
1668             /* For integers, try mapping it to a same-sized scalar mode.  */
1669             if (mode == VOIDmode
1670                 && GET_MODE_CLASS (innermode) == MODE_INT)
1671               mode = mode_for_size (nunits * GET_MODE_BITSIZE (innermode),
1672                                     MODE_INT, 0);
1673 
1674             if (mode == VOIDmode || !have_regs_of_mode[mode])
1675               TYPE_MODE (type) = BLKmode;
1676             else
1677               TYPE_MODE (type) = mode;
1678           }
1679 
1680         TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TREE_TYPE (type));
1681         TYPE_SIZE_UNIT (type) = int_const_binop (MULT_EXPR,
1682                                                  TYPE_SIZE_UNIT (innertype),
1683                                                  nunits_tree, 0);
1684         TYPE_SIZE (type) = int_const_binop (MULT_EXPR, TYPE_SIZE (innertype),
1685                                             nunits_tree, 0);
1686 
1687         /* Always naturally align vectors.  This prevents ABI changes
1688            depending on whether or not native vector modes are supported.  */
1689         TYPE_ALIGN (type) = tree_low_cst (TYPE_SIZE (type), 0);
1690         break;
1691       }
1692 
1693     case VOID_TYPE:
1694       /* This is an incomplete type and so doesn't have a size.  */
1695       TYPE_ALIGN (type) = 1;
1696       TYPE_USER_ALIGN (type) = 0;
1697       TYPE_MODE (type) = VOIDmode;
1698       break;
1699 
1700     case OFFSET_TYPE:
1701       TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
1702       TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
1703       /* A pointer might be MODE_PARTIAL_INT,
1704          but ptrdiff_t must be integral.  */
1705       TYPE_MODE (type) = mode_for_size (POINTER_SIZE, MODE_INT, 0);
1706       break;
1707 
1708     case FUNCTION_TYPE:
1709     case METHOD_TYPE:
1710       /* It's hard to see what the mode and size of a function ought to
1711          be, but we do know the alignment is FUNCTION_BOUNDARY, so
1712          make it consistent with that.  */
1713       TYPE_MODE (type) = mode_for_size (FUNCTION_BOUNDARY, MODE_INT, 0);
1714       TYPE_SIZE (type) = bitsize_int (FUNCTION_BOUNDARY);
1715       TYPE_SIZE_UNIT (type) = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1716       break;
1717 
1718     case POINTER_TYPE:
1719     case REFERENCE_TYPE:
1720       {
1721 
1722         enum machine_mode mode = ((TREE_CODE (type) == REFERENCE_TYPE
1723                                    && reference_types_internal)
1724                                   ? Pmode : TYPE_MODE (type));
1725 
1726         int nbits = GET_MODE_BITSIZE (mode);
1727 
1728         TYPE_SIZE (type) = bitsize_int (nbits);
1729         TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (mode));
1730         TYPE_UNSIGNED (type) = 1;
1731         TYPE_PRECISION (type) = nbits;
1732       }
1733       break;
1734 
1735     case ARRAY_TYPE:
1736       {
1737         tree index = TYPE_DOMAIN (type);
1738         tree element = TREE_TYPE (type);
1739 
1740         build_pointer_type (element);
1741 
1742         /* We need to know both bounds in order to compute the size.  */
1743         if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
1744             && TYPE_SIZE (element))
1745           {
1746             tree ub = TYPE_MAX_VALUE (index);
1747             tree lb = TYPE_MIN_VALUE (index);
1748             tree length;
1749             tree element_size;
1750 
1751             /* The initial subtraction should happen in the original type so
1752                that (possible) negative values are handled appropriately.  */
1753             length = size_binop (PLUS_EXPR, size_one_node,
1754                                  fold_convert (sizetype,
1755                                                fold_build2 (MINUS_EXPR,
1756                                                             TREE_TYPE (lb),
1757                                                             ub, lb)));
1758 
1759             /* Special handling for arrays of bits (for Chill).  */
1760             element_size = TYPE_SIZE (element);
1761             if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element)
1762                 && (integer_zerop (TYPE_MAX_VALUE (element))
1763                     || integer_onep (TYPE_MAX_VALUE (element)))
1764                 && host_integerp (TYPE_MIN_VALUE (element), 1))
1765               {
1766                 HOST_WIDE_INT maxvalue
1767                   = tree_low_cst (TYPE_MAX_VALUE (element), 1);
1768                 HOST_WIDE_INT minvalue
1769                   = tree_low_cst (TYPE_MIN_VALUE (element), 1);
1770 
1771                 if (maxvalue - minvalue == 1
1772                     && (maxvalue == 1 || maxvalue == 0))
1773                   element_size = integer_one_node;
1774               }
1775 
1776             /* If neither bound is a constant and sizetype is signed, make
1777                sure the size is never negative.  We should really do this
1778                if *either* bound is non-constant, but this is the best
1779                compromise between C and Ada.  */
1780             if (!TYPE_UNSIGNED (sizetype)
1781                 && TREE_CODE (TYPE_MIN_VALUE (index)) != INTEGER_CST
1782                 && TREE_CODE (TYPE_MAX_VALUE (index)) != INTEGER_CST)
1783               length = size_binop (MAX_EXPR, length, size_zero_node);
1784 
1785             TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size,
1786                                            fold_convert (bitsizetype, 
1787                                                          length));
1788 
1789             /* If we know the size of the element, calculate the total
1790                size directly, rather than do some division thing below.
1791                This optimization helps Fortran assumed-size arrays
1792                (where the size of the array is determined at runtime)
1793                substantially.
1794                Note that we can't do this in the case where the size of
1795                the elements is one bit since TYPE_SIZE_UNIT cannot be
1796                set correctly in that case.  */
1797             if (TYPE_SIZE_UNIT (element) != 0 && ! integer_onep (element_size))
1798               TYPE_SIZE_UNIT (type)
1799                 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
1800           }
1801 
1802         /* Now round the alignment and size,
1803            using machine-dependent criteria if any.  */
1804 
1805 #ifdef ROUND_TYPE_ALIGN
1806         TYPE_ALIGN (type)
1807           = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
1808 #else
1809         TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
1810 #endif
1811         TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (element);
1812         TYPE_MODE (type) = BLKmode;
1813         if (TYPE_SIZE (type) != 0
1814 #ifdef MEMBER_TYPE_FORCES_BLK
1815             && ! MEMBER_TYPE_FORCES_BLK (type, VOIDmode)
1816 #endif
1817             /* BLKmode elements force BLKmode aggregate;
1818                else extract/store fields may lose.  */
1819             && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
1820                 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
1821           {
1822             /* One-element arrays get the component type's mode.  */
1823             if (simple_cst_equal (TYPE_SIZE (type),
1824                                   TYPE_SIZE (TREE_TYPE (type))))
1825               TYPE_MODE (type) = TYPE_MODE (TREE_TYPE (type));
1826             else
1827               TYPE_MODE (type)
1828                 = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1829 
1830             if (TYPE_MODE (type) != BLKmode
1831                 && STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
1832                 && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (TYPE_MODE (type))
1833                 && TYPE_MODE (type) != BLKmode)
1834               {
1835                 TYPE_NO_FORCE_BLK (type) = 1;
1836                 TYPE_MODE (type) = BLKmode;
1837               }
1838           }
1839         /* When the element size is constant, check that it is at least as
1840            large as the element alignment.  */
1841         if (TYPE_SIZE_UNIT (element)
1842             && TREE_CODE (TYPE_SIZE_UNIT (element)) == INTEGER_CST
1843             /* If TYPE_SIZE_UNIT overflowed, then it is certainly larger than
1844                TYPE_ALIGN_UNIT.  */
1845             && !TREE_CONSTANT_OVERFLOW (TYPE_SIZE_UNIT (element))
1846             && !integer_zerop (TYPE_SIZE_UNIT (element))
1847             && compare_tree_int (TYPE_SIZE_UNIT (element),
1848                                  TYPE_ALIGN_UNIT (element)) < 0)
1849           error ("alignment of array elements is greater than element size");
1850         break;
1851       }
1852 
1853     case RECORD_TYPE:
1854     case UNION_TYPE:
1855     case QUAL_UNION_TYPE:
1856       {
1857         tree field;
1858         record_layout_info rli;
1859 
1860         /* Initialize the layout information.  */
1861         rli = start_record_layout (type);
1862 
1863         /* If this is a QUAL_UNION_TYPE, we want to process the fields
1864            in the reverse order in building the COND_EXPR that denotes
1865            its size.  We reverse them again later.  */
1866         if (TREE_CODE (type) == QUAL_UNION_TYPE)
1867           TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1868 
1869         /* Place all the fields.  */
1870         for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1871           place_field (rli, field);
1872 
1873         if (TREE_CODE (type) == QUAL_UNION_TYPE)
1874           TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1875 
1876         if (lang_adjust_rli)
1877           (*lang_adjust_rli) (rli);
1878 
1879         /* Finish laying out the record.  */
1880         finish_record_layout (rli, /*free_p=*/true);
1881       }
1882       break;
1883 
1884     default:
1885       gcc_unreachable ();
1886     }
1887 
1888   /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE.  For
1889      records and unions, finish_record_layout already called this
1890      function.  */
1891   if (TREE_CODE (type) != RECORD_TYPE
1892       && TREE_CODE (type) != UNION_TYPE
1893       && TREE_CODE (type) != QUAL_UNION_TYPE)
1894     finalize_type_size (type);
1895 
1896   /* If an alias set has been set for this aggregate when it was incomplete,
1897      force it into alias set 0.
1898      This is too conservative, but we cannot call record_component_aliases
1899      here because some frontends still change the aggregates after
1900      layout_type.  */
1901   if (AGGREGATE_TYPE_P (type) && TYPE_ALIAS_SET_KNOWN_P (type))
1902     TYPE_ALIAS_SET (type) = 0;
1903 }



リンク元

Advertisement