home *** CD-ROM | disk | FTP | other *** search
/ Education Sampler 1992 [NeXTSTEP] / Education_1992_Sampler.iso / NeXT / GnuSource / cc-61.0.1 / cc / stor-layout.c < prev    next >
C/C++ Source or Header  |  1991-06-04  |  32KB  |  1,064 lines

  1. /* C-compiler utilities for types and variables storage layout
  2.    Copyright (C) 1987, 1988 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include "config.h"
  22. #include <stdio.h>
  23.  
  24. #include "tree.h"
  25. #include "function.h"
  26.  
  27. #define MAX(x,y) ((x) > (y) ? (x) : (y))
  28. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  29. #define CEIL(x,y) (((x) + (y) - 1) / (y))
  30.  
  31. /* Data type for the expressions representing sizes of data types.
  32.    It is the first integer type laid out.
  33.    In C, this is int.  */
  34.  
  35. tree sizetype;
  36.  
  37. /* An integer constant with value 0 whose type is sizetype.  */
  38.  
  39. tree size_zero_node;
  40.  
  41. /* An integer constant with value 1 whose type is sizetype.  */
  42.  
  43. tree size_one_node;
  44.  
  45. #define GET_MODE_ALIGNMENT(MODE)   \
  46.   MIN (BIGGEST_ALIGNMENT,        \
  47.        MAX (1, (GET_MODE_UNIT_SIZE (MODE) * BITS_PER_UNIT)))
  48.  
  49. /* Chain of all permanent types we have allocated since last
  50.    call to get_permanent_types.  */
  51.  
  52. tree permanent_type_chain;
  53.  
  54. /* Chain of all temporary types we have allocated in this function.  */
  55.  
  56. tree temporary_type_chain;
  57.  
  58. /* When the chains is not null, these point at the last
  59.    types on the two chains.  These help us tell whether a type
  60.    is already on a chain.  */
  61. tree permanent_type_end;
  62. tree temporary_type_end;
  63.  
  64. /* Put the newly-made type T
  65.    on either permanent_type_chain or temporary_type_chain.
  66.    Types that are const or volatile variants of other types
  67.    are not put on any chain, since in the gdb symbol segment
  68.    we do not make those distinctions.
  69.  
  70.    If T is already on the chain, we do nothing.  */
  71.  
  72. void
  73. chain_type (t)
  74.      tree t;
  75. {
  76.   if (TYPE_MAIN_VARIANT (t) != t)
  77.     return;
  78.   if (TREE_CHAIN (t) != 0)
  79.     return;
  80.   if (TREE_PERMANENT (t))
  81.     {
  82.       /* If T is on the chain at the end, don't chain it to itself!  */
  83.       if (t == permanent_type_end)
  84.     return;
  85.       /* Add T to the end of the chain.  */
  86.       if (permanent_type_chain == 0)
  87.     permanent_type_chain = t;
  88.       else
  89.     TREE_CHAIN (permanent_type_end) = t;
  90.       permanent_type_end = t;
  91.     }
  92.   else
  93.     {
  94.       if (t == temporary_type_end)
  95.     return;
  96.       if (temporary_type_chain == 0)
  97.     temporary_type_chain = t;
  98.       else
  99.     TREE_CHAIN (temporary_type_end) = t;
  100.       temporary_type_end = t;
  101.     }
  102. }
  103.  
  104. /* Get a chain of all permanent types made since this function
  105.    was last called.  */
  106.  
  107. tree
  108. get_permanent_types ()
  109. {
  110.   register tree tem = permanent_type_chain;
  111.   permanent_type_chain = 0;
  112.   permanent_type_end = 0;
  113.   return tem;
  114. }
  115.  
  116. /* Get a chain of all temporary types made since this function
  117.    was last called.  */
  118.  
  119. tree
  120. get_temporary_types ()
  121. {
  122.   register tree tem = temporary_type_chain;
  123.   temporary_type_chain = 0;
  124.   temporary_type_end = 0;
  125.   return tem;
  126. }
  127.  
  128. /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded.  */
  129.  
  130. static tree pending_sizes;
  131.  
  132. /* Nonzero means cannot safely call expand_expr now,
  133.    so put variable sizes onto `pending_sizes' instead.  */
  134.  
  135. int immediate_size_expand;
  136.  
  137. tree
  138. get_pending_sizes ()
  139. {
  140.   tree chain = pending_sizes;
  141.   pending_sizes = 0;
  142.   return chain;
  143. }
  144.  
  145. /* Given a size SIZE that isn't constant, return a SAVE_EXPR
  146.    to serve as the actual size-expression for a type or decl.  */
  147.  
  148. static tree
  149. variable_size (size)
  150.      tree size;
  151. {
  152.   size = save_expr (size);
  153.  
  154.   if (global_bindings_p ())
  155.     {
  156.       error ("variable-size type declared outside of any function");
  157.       return size_int (1);
  158.     }
  159.  
  160.   if (immediate_size_expand)
  161.     expand_expr (size, 0, VOIDmode, 0);
  162.   else
  163.     pending_sizes = tree_cons (0, size, pending_sizes);
  164.  
  165.   return size;
  166. }
  167.  
  168. #ifndef MAX_FIXED_MODE_SIZE
  169. #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
  170. #endif
  171.  
  172. /* Return the machine mode to use for a nonscalar of SIZE bits.
  173.    The mode must be in class CLASS, and have exactly that many bits.
  174.    If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
  175.    be used.  */
  176.  
  177. enum machine_mode
  178. mode_for_size (size, class, limit)
  179.      unsigned int size;
  180.      enum mode_class class;
  181.      int limit;
  182. {
  183.   register enum machine_mode mode;
  184.  
  185.   if (limit && size > MAX_FIXED_MODE_SIZE)
  186.     return BLKmode;
  187.  
  188.   /* Get the last mode which has this size, in the specified class.  */
  189.   for (mode = VOIDmode; (int) mode < (int) MAX_MACHINE_MODE;
  190.        mode = (enum machine_mode) ((int) mode + 1))
  191.     if (GET_MODE_BITSIZE (mode) == size
  192.     && GET_MODE_CLASS (mode) == class)
  193.       return mode;
  194.  
  195.   return BLKmode;
  196. }
  197.  
  198. /* Return the value of VALUE, rounded up to a multiple of DIVISOR.  */
  199.  
  200. tree
  201. round_up (value, divisor)
  202.      tree value;
  203.      int divisor;
  204. {
  205.   return size_binop (MULT_EXPR,
  206.              size_binop (CEIL_DIV_EXPR, value, size_int (divisor)),
  207.              size_int (divisor));
  208. }
  209.  
  210. /* Set the size, mode and alignment of a ..._DECL node.
  211.    TYPE_DECL does need this for C++.
  212.    Note that LABEL_DECL and CONST_DECL nodes do not need this,
  213.    and FUNCTION_DECL nodes have them set up in a special (and simple) way.
  214.    Don't call layout_decl for them.
  215.  
  216.    KNOWN_ALIGN is the amount of alignment we can assume this
  217.    decl has with no special effort.  It is relevant only for FIELD_DECLs
  218.    and depends on the previous fields.
  219.    All that matters about KNOWN_ALIGN is which powers of 2 divide it.
  220.    If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
  221.    the record will be aligned to suit.  */
  222.  
  223. void
  224. layout_decl (decl, known_align)
  225.      tree decl;
  226.      unsigned known_align;
  227. {
  228.   register tree type = TREE_TYPE (decl);
  229.   register enum tree_code code = TREE_CODE (decl);
  230.   int spec_size = DECL_FRAME_SIZE (decl);
  231.  
  232.   if (code == CONST_DECL)
  233.     return;
  234.  
  235.   if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
  236.       && code != FIELD_DECL && code != TYPE_DECL)
  237.     abort ();
  238.  
  239.   if (type == error_mark_node)
  240.     {
  241.       type = void_type_node;
  242.       spec_size = 0;
  243.     }
  244.  
  245.   /* Usually the size and mode come from the data type without change.  */
  246.  
  247.   DECL_MODE (decl) = TYPE_MODE (type);
  248.   DECL_SIZE (decl) = TYPE_SIZE (type);
  249.   TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
  250.  
  251.   if (code == FIELD_DECL && DECL_BIT_FIELD (decl))
  252.     {
  253.       /* This is a bit-field.  We don't know how to handle
  254.      them except for integers and enums, and front end should
  255.      never generate them otherwise.  */
  256.  
  257.       if (! (TREE_CODE (type) == INTEGER_TYPE
  258.          || TREE_CODE (type) == ENUMERAL_TYPE))
  259.     abort ();
  260.  
  261.       if (spec_size == 0 && DECL_NAME (decl) != 0)
  262.     abort ();
  263.  
  264.       /* Size is specified number of bits.  */
  265.       DECL_SIZE (decl) = size_int (spec_size);
  266.     }
  267.   /* Force alignment required for the data type.
  268.      But if the decl itself wants greater alignment, don't override that.  */
  269.   else if (TYPE_ALIGN (type) > DECL_ALIGN (decl))
  270.     DECL_ALIGN (decl) = TYPE_ALIGN (type);
  271.  
  272.   /* See if we can use an ordinary integer mode for a bit-field.  */
  273.   /* Conditions are: a fixed size that is correct for another mode
  274.      and occupying a complete byte or bytes on proper boundary.  */
  275.   if (code == FIELD_DECL)
  276.     DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
  277.   if (DECL_BIT_FIELD (decl)
  278.       && TYPE_SIZE (type) != 0
  279.       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
  280.     {
  281.       register enum machine_mode xmode
  282.     = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1);
  283.  
  284.       if (xmode != BLKmode
  285.       && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
  286.     {
  287.       DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
  288.                    DECL_ALIGN (decl));
  289.       DECL_MODE (decl) = xmode;
  290.       DECL_SIZE (decl) = size_int (GET_MODE_BITSIZE (xmode));
  291.       /* This no longer needs to be accessed as a bit field.  */
  292.       DECL_BIT_FIELD (decl) = 0;
  293.     }
  294.     }
  295.  
  296.   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
  297.   if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
  298.     DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
  299. }
  300.  
  301. /* Lay out a RECORD_TYPE type (a C struct).
  302.    This means laying out the fields, determining their positions,
  303.    and computing the overall size and required alignment of the record.
  304.    Note that if you set the TYPE_ALIGN before calling this
  305.    then the struct is aligned to at least that boundary.
  306.  
  307.    If the type has basetypes, you must call layout_basetypes
  308.    before calling this function.  */
  309.  
  310. static void
  311. layout_record (rec)
  312.      tree rec;
  313. {
  314.   register tree field;
  315. #ifdef STRUCTURE_SIZE_BOUNDARY
  316.   int record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
  317. #else
  318.   int record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
  319. #endif
  320.   /* These must be laid out *after* the record is.  */
  321.   tree pending_statics = NULL_TREE;
  322.   /* Record size so far is CONST_SIZE + VAR_SIZE bits,
  323.      where CONST_SIZE is an integer
  324.      and VAR_SIZE is a tree expression.
  325.      If VAR_SIZE is null, the size is just CONST_SIZE.
  326.      Naturally we try to avoid using VAR_SIZE.  */
  327.   register int const_size = 0;
  328.   register tree var_size = 0;
  329.   /* Once we start using VAR_SIZE, this is the maximum alignment
  330.      that we know VAR_SIZE has.  */
  331.   register int var_align = BITS_PER_UNIT;
  332.  
  333.  
  334.   for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
  335.     {
  336.       register int desired_align;
  337.  
  338.       /* If FIELD is static, then treat it like a separate variable,
  339.      not really like a structure field.
  340.      If it is a FUNCTION_DECL, it's a method.
  341.      In both cases, all we do is lay out the decl,
  342.      and we do it *after* the record is laid out.  */
  343.  
  344.       if (TREE_STATIC (field))
  345.     {
  346.       pending_statics = tree_cons (NULL, field, pending_statics);
  347.       continue;
  348.     }
  349.       /* Enumerators and enum types which are local to this class need not
  350.      be laid out.  Likewise for initialized constant fields.  */
  351.       if (TREE_CODE (field) != FIELD_DECL)
  352.     continue;
  353.  
  354.       /* Lay out the field so we know what alignment it needs.
  355.      For KNOWN_ALIGN, pass the number of bits from start of record
  356.      or some divisor of it.  */
  357.  
  358.       layout_decl (field, var_size ? var_align : const_size);
  359.       desired_align = DECL_ALIGN (field);
  360.       /* Some targets (i.e. VMS) limit struct field alignment
  361.      to a lower boundary than alignment of variables.  */
  362. #ifdef BIGGEST_FIELD_ALIGNMENT
  363.       desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
  364. #endif
  365.  
  366.       /* Record must have at least as much alignment as any field.
  367.      Otherwise, the alignment of the field within the record
  368.      is meaningless.  */
  369.  
  370. #ifndef PCC_BITFIELD_TYPE_MATTERS
  371.       record_align = MAX (record_align, desired_align);
  372. #else
  373.       if (PCC_BITFIELD_TYPE_MATTERS && TREE_TYPE (field) != error_mark_node
  374.       && ! integer_zerop (TYPE_SIZE (TREE_TYPE (field))))
  375.     {
  376.       /* For these machines, a zero-length field does not
  377.          affect the alignment of the structure as a whole.
  378.          It does, however, affect the alignment of the next field
  379.          within the structure.  */
  380.       if (! integer_zerop (DECL_SIZE (field)))
  381.         record_align = MAX (record_align, desired_align);
  382.       else
  383.         desired_align = TYPE_ALIGN (TREE_TYPE (field));
  384.       /* A named bit field of declared type `int'
  385.          forces the entire structure to have `int' alignment.  */
  386.       if (DECL_NAME (field) != 0)
  387.         record_align = MAX (record_align, TYPE_ALIGN (TREE_TYPE (field)));
  388.     }
  389.       else
  390.     record_align = MAX (record_align, desired_align);
  391. #endif
  392.  
  393.       /* Does this field automatically have alignment it needs
  394.      by virtue of the fields that precede it and the record's
  395.      own alignment?  */
  396.  
  397.       if (const_size % desired_align != 0
  398.       || (var_align % desired_align != 0
  399.           && var_size != 0))
  400.     {
  401.       /* No, we need to skip space before this field.
  402.          Bump the cumulative size to multiple of field alignment.  */
  403.  
  404.       if (var_size == 0
  405.           || var_align % desired_align == 0)
  406.         const_size
  407.           = CEIL (const_size, desired_align) * desired_align;
  408.       else
  409.         {
  410.           if (const_size > 0)
  411.         var_size = size_binop (PLUS_EXPR, var_size, const_size);
  412.           const_size = 0;
  413.           var_size = round_up (var_size, desired_align);
  414.           var_align = MIN (var_align, desired_align);
  415.         }
  416.     }
  417.  
  418. #ifdef PCC_BITFIELD_TYPE_MATTERS
  419.       if (PCC_BITFIELD_TYPE_MATTERS
  420.       && TREE_CODE (field) == FIELD_DECL
  421.       && TREE_TYPE (field) != error_mark_node
  422.       && !integer_zerop (DECL_SIZE (field)))
  423.     {
  424.       int type_align = TYPE_ALIGN (TREE_TYPE (field));
  425.       register tree dsize = DECL_SIZE (field);
  426.       int field_size = TREE_INT_CST_LOW (dsize);
  427.  
  428.       /* A bit field may not span the unit of alignment of its type.
  429.          Advance to next boundary if necessary.  */
  430.       if (const_size / type_align
  431.           != (const_size + field_size - 1) / type_align)
  432.         const_size = CEIL (const_size, type_align) * type_align;
  433.     }
  434. #endif
  435.  
  436. #ifdef BITFIELD_NBYTES_LIMITED
  437.       if (BITFIELD_NBYTES_LIMITED && TREE_CODE (field) == FIELD_DECL
  438.       && DECL_BIT_FIELD (field)
  439.       && TREE_TYPE (field) != error_mark_node)
  440.     {
  441.       int type_size = int_size_in_bytes (TREE_TYPE (field));
  442.       int type_align = TYPE_ALIGN (TREE_TYPE (field));
  443.       register tree dsize = DECL_SIZE (field);
  444.       int field_size = TREE_INT_CST_LOW (dsize);
  445.  
  446.       /* A bit field may not span the unit of alignment of its type.
  447.          Advance to next boundary if necessary.  */
  448. #if 0  /* this code was checking for requiring more bytes than the
  449.       size of the type.  That doesn't match the comment.
  450.       However, it is not certain whether the comment or the
  451.       code was correct.  */
  452.       if (((const_size + field_size - 1) / BITS_PER_UNIT
  453.            - const_size / BITS_PER_UNIT)
  454.           > type_size)
  455. #endif
  456.       if ((const_size + field_size - 1) / type_align
  457.           != const_size / type_align)
  458.         const_size = CEIL (const_size, type_align) * type_align;
  459.     }
  460. #endif
  461.  
  462.       /* Size so far becomes the position of this field.  */
  463.  
  464.       if (var_size && const_size)
  465.     DECL_FIELD_BITPOS (field)
  466.       = size_binop (PLUS_EXPR, var_size, size_int (const_size));
  467.       else if (var_size)
  468.     DECL_FIELD_BITPOS (field) = var_size;
  469.       else
  470.     DECL_FIELD_BITPOS (field) = size_int (const_size);
  471.  
  472.       /* If this field is an anonymous union,
  473.      give each union-member the same position as the union has.  */
  474.  
  475.       if (DECL_NAME (field) == 0
  476.       && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
  477.     {
  478.       tree uelt = TYPE_FIELDS (TREE_TYPE (field));
  479.       for (; uelt; uelt = TREE_CHAIN (uelt))
  480.         {
  481.           DECL_FIELD_CONTEXT (uelt) = DECL_FIELD_CONTEXT (field);
  482.           DECL_FIELD_BITPOS (uelt) = DECL_FIELD_BITPOS (field);
  483.         }
  484.     }
  485.  
  486.       /* Now add size of this field to the size of the record.  */
  487.  
  488.       {
  489.         register tree dsize = DECL_SIZE (field);
  490.  
  491.     if (TREE_CODE (dsize) == INTEGER_CST)
  492.       const_size += TREE_INT_CST_LOW (dsize);
  493.     else
  494.       {
  495.         if (var_size == 0)
  496.           var_size = dsize;
  497.         else
  498.           var_size = size_binop (PLUS_EXPR, var_size, dsize);
  499.       }
  500.       }
  501.     }
  502.  
  503.   /* Work out the total size and alignment of the record
  504.      as one expression and store in the record type.
  505.      Round it up to a multiple of the record's alignment.  */
  506.  
  507.   if (var_size == 0)
  508.     {
  509.       TYPE_SIZE (rec) = size_int (const_size);
  510.     }
  511.   else
  512.     {
  513.       if (const_size)
  514.     var_size
  515.       = size_binop (PLUS_EXPR, var_size, size_int (const_size));
  516.       TYPE_SIZE (rec) = var_size;
  517.     }
  518.  
  519.   /* Determine the desired alignment.  */
  520. #ifdef ROUND_TYPE_ALIGN
  521.   TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), record_align);
  522. #else
  523.   TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), record_align);
  524. #endif
  525.  
  526. #ifdef ROUND_TYPE_SIZE
  527.   TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
  528. #else
  529.   /* Round the size up to be a multiple of the required alignment */
  530.   TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
  531. #endif
  532.  
  533.   /* Lay out any static members.  This is done now
  534.      because their type may use the record's type.  */
  535.  
  536.   for (field = pending_statics; field; field = TREE_CHAIN (field))
  537.     layout_decl (TREE_VALUE (field), 0);
  538. }
  539.  
  540. /* Lay out a UNION_TYPE type.
  541.    Lay out all the fields, set their positions to zero,
  542.    and compute the size and alignment of the union (maximum of any field).
  543.    Note that if you set the TYPE_ALIGN before calling this
  544.    then the union align is aligned to at least that boundary.  */
  545.  
  546. static void
  547. layout_union (rec)
  548.      tree rec;
  549. {
  550.   register tree field;
  551. #ifdef STRUCTURE_SIZE_BOUNDARY
  552.   int union_align = STRUCTURE_SIZE_BOUNDARY;
  553. #else
  554.   int union_align = BITS_PER_UNIT;
  555. #endif
  556.  
  557.   /* The size of the union, based on the fields scanned so far,
  558.      is max (CONST_SIZE, VAR_SIZE).
  559.      VAR_SIZE may be null; then CONST_SIZE by itself is the size.  */
  560.   register int const_size = 0;
  561.   register tree var_size = 0;
  562.  
  563.   for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
  564.     {
  565.       /* Enums which are local to this class need not be laid out.  */
  566.       if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
  567.     continue;
  568.  
  569.       layout_decl (field, 0);
  570.       DECL_FIELD_BITPOS (field) = size_int (0);
  571.  
  572.       /* Union must be at least as aligned as any field requires.  */
  573.  
  574.       union_align = MAX (union_align, DECL_ALIGN (field));
  575.  
  576. #ifdef PCC_BITFIELD_TYPE_MATTERS
  577.       /* On the m88000, a bit field of declare type `int'
  578.      forces the entire union to have `int' alignment.  */
  579.       if (PCC_BITFIELD_TYPE_MATTERS) 
  580.     union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
  581. #endif
  582.  
  583.       /* Set union_size to max (decl_size, union_size).
  584.      There are more and less general ways to do this.
  585.      Use only CONST_SIZE unless forced to use VAR_SIZE.  */
  586.  
  587.       if (TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
  588.     const_size = MAX (const_size, TREE_INT_CST_LOW (DECL_SIZE (field)));
  589.       else if (var_size == 0)
  590.     var_size = DECL_SIZE (field);
  591.       else
  592.     var_size = size_binop (MAX_EXPR, var_size, DECL_SIZE (field));
  593.     }
  594.  
  595.   /* Determine the ultimate size of the union (in bytes).  */
  596.   if (NULL == var_size)
  597.     TYPE_SIZE (rec) = size_int (CEIL (const_size, BITS_PER_UNIT)
  598.                 * BITS_PER_UNIT);
  599.   else if (const_size == 0)
  600.     TYPE_SIZE (rec) = var_size;
  601.   else
  602.     TYPE_SIZE (rec) = size_binop (MAX_EXPR, var_size,
  603.                   round_up (size_int (const_size),
  604.                         BITS_PER_UNIT));
  605.  
  606.   /* Determine the desired alignment.  */
  607. #ifdef ROUND_TYPE_ALIGN
  608.   TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), union_align);
  609. #else
  610.   TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
  611. #endif
  612.  
  613. #ifdef ROUND_TYPE_SIZE
  614.   TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
  615. #else
  616.   /* Round the size up to be a multiple of the required alignment */
  617.   TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
  618. #endif
  619. }
  620.  
  621. /* Calculate the mode, size, and alignment for TYPE.
  622.    For an array type, calculate the element separation as well.
  623.    Record TYPE on the chain of permanent or temporary types
  624.    so that dbxout will find out about it.
  625.  
  626.    TYPE_SIZE of a type is nonzero if the type has been laid out already.
  627.    layout_type does nothing on such a type.
  628.  
  629.    If the type is incomplete, its TYPE_SIZE remains zero.  */
  630.  
  631. void
  632. layout_type (type)
  633.      tree type;
  634. {
  635.   int old;
  636.   int temporary = 0;
  637.  
  638.   if (type == 0)
  639.     abort ();
  640.  
  641.   /* Do nothing if type has been laid out before.  */
  642.   if (TYPE_SIZE (type))
  643.     return;
  644.  
  645.   /* Make sure all nodes we allocate are not momentary;
  646.      they must last past the current statement.  */
  647.   old  = suspend_momentary ();
  648.   if (TREE_PERMANENT (type) && allocation_temporary_p ())
  649.     {
  650.       temporary = 1;
  651.       end_temporary_allocation ();
  652.     }
  653.  
  654.   chain_type (type);
  655.  
  656.   switch (TREE_CODE (type))
  657.     {
  658.     case LANG_TYPE:
  659.       /* This kind of type is the responsibility
  660.      of the languge-specific code.  */
  661.       abort ();
  662.  
  663.     case INTEGER_TYPE:
  664.     case ENUMERAL_TYPE:
  665.       if (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (type)) >= 0)
  666.     TREE_UNSIGNED (type) = 1;
  667.  
  668.       TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_INT, 1);
  669.       TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
  670.       break;
  671.  
  672.     case REAL_TYPE:
  673.       TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
  674.       TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
  675.       break;
  676.  
  677.     case COMPLEX_TYPE:
  678.       TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
  679.       TYPE_MODE (type)
  680.     = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
  681.              (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
  682.               ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
  683.              0);
  684.       TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
  685.       break;
  686.  
  687.     case VOID_TYPE:
  688.       TYPE_SIZE (type) = size_zero_node;
  689.       TYPE_ALIGN (type) = 1;
  690.       TYPE_MODE (type) = VOIDmode;
  691.       break;
  692.  
  693.     case FUNCTION_TYPE:
  694.     case METHOD_TYPE:
  695.       TYPE_MODE (type) = mode_for_size (2 * GET_MODE_BITSIZE (Pmode),
  696.                     MODE_INT, 0);
  697.       TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
  698.       break;
  699.  
  700.     case POINTER_TYPE:
  701.     case REFERENCE_TYPE:
  702.       TYPE_MODE (type) = Pmode;
  703.       TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
  704.       TREE_UNSIGNED (type) = 1;
  705.       TYPE_PRECISION (type) = GET_MODE_BITSIZE (TYPE_MODE (type));
  706.       break;
  707.  
  708.     case ARRAY_TYPE:
  709.       {
  710.     register tree index = TYPE_DOMAIN (type);
  711.     register tree element = TREE_TYPE (type);
  712.  
  713.     build_pointer_type (element);
  714.  
  715.     /* We need to know both bounds in order to compute the size.  */
  716.     if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
  717.         && TYPE_SIZE (element))
  718.       {
  719.         tree length
  720.           = size_binop (PLUS_EXPR, size_one_node,
  721.                 size_binop (MINUS_EXPR, TYPE_MAX_VALUE (index),
  722.                     TYPE_MIN_VALUE (index)));
  723.  
  724.         TYPE_SIZE (type) = size_binop (MULT_EXPR, length,
  725.                        TYPE_SIZE (element));
  726.       }
  727.  
  728.     /* Now round the alignment and size,
  729.        using machine-dependent criteria if any.  */
  730.  
  731. #ifdef ROUND_TYPE_ALIGN
  732.     TYPE_ALIGN (type)
  733.       = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
  734. #else
  735.     TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
  736. #endif
  737.  
  738. #ifdef ROUND_TYPE_SIZE
  739.     if (TYPE_SIZE (type) != 0)
  740.       TYPE_SIZE (type)
  741.         = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
  742. #endif
  743.  
  744.     TYPE_MODE (type) = BLKmode;
  745.     if (TYPE_SIZE (type) != 0
  746.         && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
  747.         /* BLKmode elements force BLKmode aggregate;
  748.            else extract/store fields may lose.  */
  749.         && TYPE_MODE (TREE_TYPE (type)) != BLKmode
  750.         /* Following conjunct used to be only if STRICT_ALIGNMENT,
  751.            but that was a bug.  It caused type char[2]
  752.            to have mode HImode but alignment of 16.
  753.            This lead to weirdness in the layout of structures.  */
  754.         && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
  755.         || TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type)))
  756.         )
  757.       {
  758.         TYPE_MODE (type)
  759.           = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
  760.                    MODE_INT, 1);
  761.       }
  762.     break;
  763.       }
  764.  
  765.     case RECORD_TYPE:
  766.       layout_record (type);
  767.       TYPE_MODE (type) = BLKmode;
  768.       if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
  769.     {
  770.       tree field;
  771.       /* A record which has any BLKmode members must itself be BLKmode;
  772.          it can't go in a register.
  773.          Unless the member is BLKmode only because it isn't aligned.  */
  774.       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  775.         {
  776.           int bitpos;
  777.  
  778.           if (TREE_CODE (field) != FIELD_DECL)
  779.         continue;
  780.  
  781.           if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
  782.           && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
  783.         goto record_lose;
  784.  
  785.           if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
  786.         goto record_lose;
  787.  
  788.           bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
  789.  
  790.           /* Must be BLKmode if any field crosses a word boundary,
  791.          since extract_bit_field can't handle that in registers.  */
  792.           if (bitpos / BITS_PER_WORD
  793.           != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
  794.               / BITS_PER_WORD)
  795.           /* But there is no problem if the field is entire words.  */
  796.           && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD == 0)
  797.         goto record_lose;
  798.         }
  799.  
  800.       TYPE_MODE (type)
  801.         = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
  802.                  MODE_INT, 1);
  803.  
  804.       /* If structure's known alignment is less than
  805.          what the scalar mode would need, and it matters,
  806.          then stick with BLKmode.  */
  807. #ifdef STRICT_ALIGNMENT
  808.       if (! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
  809.          || TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type))))
  810.         {
  811.           if (TYPE_MODE (type) != BLKmode)
  812.         /* If this is the only reason this type is BLKmode,
  813.            then don't force containing types to be BLKmode.  */
  814.         TYPE_NO_FORCE_BLK (type) = 1;
  815.           TYPE_MODE (type) = BLKmode;
  816.         }
  817. #endif
  818.     record_lose: ;
  819.     }
  820.       break;
  821.  
  822.     case UNION_TYPE:
  823.       layout_union (type);
  824.       TYPE_MODE (type) = BLKmode;
  825.       if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
  826.       /* If structure's known alignment is less than
  827.          what the scalar mode would need, and it matters,
  828.          then stick with BLKmode.  */
  829. #ifdef STRICT_ALIGNMENT
  830.       && (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
  831.           || TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type)))
  832. #endif
  833.       )
  834.     {
  835.       tree field;
  836.       /* A union which has any BLKmode members must itself be BLKmode;
  837.          it can't go in a register.
  838.          Unless the member is BLKmode only because it isn't aligned.  */
  839.       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
  840.         {
  841.           if (TREE_CODE (field) != FIELD_DECL)
  842.         continue;
  843.  
  844.           if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
  845.           && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
  846.         goto union_lose;
  847.         }
  848.  
  849.       TYPE_MODE (type)
  850.         = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
  851.                  MODE_INT, 1);
  852.  
  853.     union_lose: ;
  854.     }
  855.       break;
  856.  
  857.     default:
  858.       abort ();
  859.     } /* end switch */
  860.  
  861.   if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode)
  862.     TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
  863.  
  864.   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
  865.   if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
  866.     TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
  867.  
  868.   /* Also layout any other variants of the type.  */
  869.   if (TYPE_NEXT_VARIANT (type)
  870.       || type != TYPE_MAIN_VARIANT (type))
  871.     {
  872.       tree variant;
  873.       /* Record layout info of this variant.  */
  874.       tree size = TYPE_SIZE (type);
  875.       int align = TYPE_ALIGN (type);
  876.       enum machine_mode mode = TYPE_MODE (type);
  877.  
  878.       /* Copy it into all variants.  */
  879.       for (variant = TYPE_MAIN_VARIANT (type);
  880.        variant;
  881.        variant = TYPE_NEXT_VARIANT (variant))
  882.     {
  883.       TYPE_SIZE (variant) = size;
  884.       TYPE_ALIGN (variant) = align;
  885.       TYPE_MODE (variant) = mode;
  886.     }
  887.     }
  888.     
  889.   if (temporary)
  890.     resume_temporary_allocation ();
  891.   resume_momentary (old);
  892. }
  893.  
  894. /* Create and return a type for signed integers of PRECISION bits.  */
  895.  
  896. tree
  897. make_signed_type (precision)
  898.      int precision;
  899. {
  900.   register tree type = make_node (INTEGER_TYPE);
  901.  
  902.   TYPE_PRECISION (type) = precision;
  903.  
  904.   /* Create the extreme values based on the number of bits.  */
  905.  
  906.   TYPE_MIN_VALUE (type)
  907.     = build_int_2 ((precision-HOST_BITS_PER_INT > 0 ? 0 : (-1)<<(precision-1)),
  908.            (-1)<<(precision-HOST_BITS_PER_INT-1 > 0
  909.               ? precision-HOST_BITS_PER_INT-1
  910.               : 0));
  911.   TYPE_MAX_VALUE (type)
  912.     = build_int_2 ((precision-HOST_BITS_PER_INT > 0 ? -1 : (1<<(precision-1))-1),
  913.            (precision-HOST_BITS_PER_INT-1 > 0
  914.             ? (1<<(precision-HOST_BITS_PER_INT-1))-1
  915.             : 0));
  916.  
  917.   /* Give this type's extreme values this type as their type.  */
  918.  
  919.   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
  920.   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
  921.  
  922.   /* The first type made with this or `make_unsigned_type'
  923.      is the type for size values.  */
  924.  
  925.   if (sizetype == 0)
  926.     {
  927.       sizetype = type;
  928.     }
  929.  
  930.   /* Lay out the type: set its alignment, size, etc.  */
  931.  
  932.   layout_type (type);
  933.  
  934.   return type;
  935. }
  936.  
  937. /* Create and return a type for unsigned integers of PRECISION bits.  */
  938.  
  939. tree
  940. make_unsigned_type (precision)
  941.      int precision;
  942. {
  943.   register tree type = make_node (INTEGER_TYPE);
  944.  
  945.   TYPE_PRECISION (type) = precision;
  946.  
  947.   /* The first type made with this or `make_signed_type'
  948.      is the type for size values.  */
  949.  
  950.   if (sizetype == 0)
  951.     {
  952.       sizetype = type;
  953.     }
  954.  
  955.   fixup_unsigned_type (type);
  956.   return type;
  957. }
  958.  
  959. /* Set the extreme values of TYPE based on its precision in bits,
  960.    the lay it out.  This is used both in `make_unsigned_type'
  961.    and for enumeral types.  */
  962.  
  963. void
  964. fixup_unsigned_type (type)
  965.      tree type;
  966. {
  967.   register int precision = TYPE_PRECISION (type);
  968.  
  969.   TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
  970.   TYPE_MAX_VALUE (type)
  971.     = build_int_2 (precision-HOST_BITS_PER_INT >= 0 ? -1 : (1<<precision)-1,
  972.            precision-HOST_BITS_PER_INT > 0
  973.            ? ((unsigned) ~0
  974.               >> (HOST_BITS_PER_INT - (precision - HOST_BITS_PER_INT)))
  975.            : 0);
  976.   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
  977.   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
  978.  
  979.   /* Lay out the type: set its alignment, size, etc.  */
  980.  
  981.   layout_type (type);
  982. }
  983.  
  984. /* Find the best machine mode to use when referencing a bit field of length
  985.    BITSIZE bits starting at BITPOS.
  986.  
  987.    The underlying object is known to be aligned to a boundary of ALIGN bits
  988.    and we should not use a mode larger than LARGEST_MODE (usually SImode).
  989.  
  990.    If no mode meets all these conditions, we return VOIDmode.  Otherwise,
  991.    unless SLOW_BYTE_ACCESS is true, we return the smallest mode meeting
  992.    these conditions.
  993.  
  994.    If SLOW_BYTE_ACCESS is true and SImode meets all the conditions, it is
  995.    returned instead.  */
  996.  
  997. enum machine_mode
  998. get_best_mode (bitsize, bitpos, align, largest_mode)
  999.      int bitsize, bitpos;
  1000.      int align;
  1001.      enum machine_mode largest_mode;
  1002. {
  1003.   enum machine_mode mode;
  1004.   int unit;
  1005.  
  1006.   /* Find the smallest mode that contains the bit field.  */
  1007.   for (mode = QImode; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
  1008.     {
  1009.       unit = GET_MODE_BITSIZE (mode);
  1010.       if (bitpos / unit == (bitpos + bitsize - 1) / unit)
  1011.     break;
  1012.     }
  1013.  
  1014.   if (mode == VOIDmode
  1015.       || unit > MIN (align, BIGGEST_ALIGNMENT)
  1016.       || unit > GET_MODE_BITSIZE (largest_mode))
  1017.     return VOIDmode;
  1018.  
  1019.   if (SLOW_BYTE_ACCESS && BITS_PER_WORD <= MIN (align, BIGGEST_ALIGNMENT)
  1020.       && BITS_PER_WORD <= GET_MODE_BITSIZE (largest_mode))
  1021.     return SImode;
  1022.  
  1023.   return mode;
  1024. }
  1025.  
  1026. /* Save all variables describing the current status into the structure *P.
  1027.    This is used before starting a nested function.  */
  1028.  
  1029. void
  1030. save_storage_status (p)
  1031.      struct function *p;
  1032. {
  1033.   p->permanent_type_chain = permanent_type_chain;
  1034.   p->temporary_type_chain = temporary_type_chain;
  1035.   p->permanent_type_end = permanent_type_end;
  1036.   p->temporary_type_end = temporary_type_end;
  1037. #if 0  /* Need not save, since always 0 and non0 (resp.) within a function.  */
  1038.   p->pending_sizes = pending_sizes;
  1039.   p->immediate_size_expand = immediate_size_expand;
  1040. #endif /* 0 */
  1041.  
  1042.   permanent_type_chain = 0;
  1043.   temporary_type_chain = 0;
  1044.   permanent_type_end = 0;
  1045.   temporary_type_end = 0;
  1046. }
  1047.  
  1048. /* Restore all variables describing the current status from the structure *P.
  1049.    This is used after a nested function.  */
  1050.  
  1051. void
  1052. restore_storage_status (p)
  1053.      struct function *p;
  1054. {
  1055.   permanent_type_chain = p->permanent_type_chain;
  1056.   temporary_type_chain = p->temporary_type_chain;
  1057.   permanent_type_end = p->permanent_type_end;
  1058.   temporary_type_end = p->temporary_type_end;
  1059. #if 0
  1060.   pending_sizes = p->pending_sizes;
  1061.   immediate_size_expand = p->immediate_size_expand;
  1062. #endif /* 0 */
  1063. }
  1064.