home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 7 / FreshFishVol7.bin / bbs / gnu / gcc-2.3.3-src.lha / GNU / src / amiga / gcc-2.3.3 / stor-layout.c < prev    next >
C/C++ Source or Header  |  1994-02-06  |  35KB  |  1,126 lines

  1. /* C-compiler utilities for types and variables storage layout
  2.    Copyright (C) 1987, 1988, 1992 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 CEIL(x,y) (((x) + (y) - 1) / (y))
  28.  
  29. /* Data type for the expressions representing sizes of data types.
  30.    It is the first integer type laid out.
  31.    In C, this is int.  */
  32.  
  33. tree sizetype;
  34.  
  35. /* An integer constant with value 0 whose type is sizetype.  */
  36.  
  37. tree size_zero_node;
  38.  
  39. /* An integer constant with value 1 whose type is sizetype.  */
  40.  
  41. tree size_one_node;
  42.  
  43. /* If nonzero, this is an upper limit on alignment of structure fields.
  44.    The value is measured in bits.  */
  45. int maximum_field_alignment;
  46.  
  47. #define GET_MODE_ALIGNMENT(MODE)   \
  48.   MIN (BIGGEST_ALIGNMENT,        \
  49.        MAX (1, (GET_MODE_UNIT_SIZE (MODE) * BITS_PER_UNIT)))
  50.  
  51. /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded.  */
  52.  
  53. static tree pending_sizes;
  54.  
  55. /* Nonzero means cannot safely call expand_expr now,
  56.    so put variable sizes onto `pending_sizes' instead.  */
  57.  
  58. int immediate_size_expand;
  59.  
  60. tree
  61. get_pending_sizes ()
  62. {
  63.   tree chain = pending_sizes;
  64.   tree t;
  65.  
  66.   /* Put each SAVE_EXPR into the current function.  */
  67.   for (t = chain; t; t = TREE_CHAIN (t))
  68.     SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
  69.   pending_sizes = 0;
  70.   return chain;
  71. }
  72.  
  73. /* Given a size SIZE that isn't constant, return a SAVE_EXPR
  74.    to serve as the actual size-expression for a type or decl.  */
  75.  
  76. tree
  77. variable_size (size)
  78.      tree size;
  79. {
  80.   size = save_expr (size);
  81.  
  82.   if (global_bindings_p ())
  83.     {
  84.       error ("variable-size type declared outside of any function");
  85.       return size_int (1);
  86.     }
  87.  
  88.   if (immediate_size_expand)
  89.     expand_expr (size, NULL_PTR, VOIDmode, 0);
  90.   else
  91.     pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
  92.  
  93.   return size;
  94. }
  95.  
  96. #ifndef MAX_FIXED_MODE_SIZE
  97. #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
  98. #endif
  99.  
  100. /* Return the machine mode to use for a nonscalar of SIZE bits.
  101.    The mode must be in class CLASS, and have exactly that many bits.
  102.    If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
  103.    be used.  */
  104.  
  105. enum machine_mode
  106. mode_for_size (size, class, limit)
  107.      unsigned int size;
  108.      enum mode_class class;
  109.      int limit;
  110. {
  111.   register enum machine_mode mode;
  112.  
  113.   if (limit && size > MAX_FIXED_MODE_SIZE)
  114.     return BLKmode;
  115.  
  116.   /* Get the last mode which has this size, in the specified class.  */
  117.   for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
  118.        mode = GET_MODE_WIDER_MODE (mode))
  119.     if (GET_MODE_BITSIZE (mode) == size)
  120.       return mode;
  121.  
  122.   return BLKmode;
  123. }
  124.  
  125. /* Return the value of VALUE, rounded up to a multiple of DIVISOR.  */
  126.  
  127. tree
  128. round_up (value, divisor)
  129.      tree value;
  130.      int divisor;
  131. {
  132.   return size_binop (MULT_EXPR,
  133.              size_binop (CEIL_DIV_EXPR, value, size_int (divisor)),
  134.              size_int (divisor));
  135. }
  136.  
  137. /* Set the size, mode and alignment of a ..._DECL node.
  138.    TYPE_DECL does need this for C++.
  139.    Note that LABEL_DECL and CONST_DECL nodes do not need this,
  140.    and FUNCTION_DECL nodes have them set up in a special (and simple) way.
  141.    Don't call layout_decl for them.
  142.  
  143.    KNOWN_ALIGN is the amount of alignment we can assume this
  144.    decl has with no special effort.  It is relevant only for FIELD_DECLs
  145.    and depends on the previous fields.
  146.    All that matters about KNOWN_ALIGN is which powers of 2 divide it.
  147.    If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
  148.    the record will be aligned to suit.  */
  149.  
  150. void
  151. layout_decl (decl, known_align)
  152.      tree decl;
  153.      unsigned known_align;
  154. {
  155.   register tree type = TREE_TYPE (decl);
  156.   register enum tree_code code = TREE_CODE (decl);
  157.   int spec_size = DECL_FIELD_SIZE (decl);
  158.  
  159.   if (code == CONST_DECL)
  160.     return;
  161.  
  162.   if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
  163.       && code != FIELD_DECL && code != TYPE_DECL)
  164.     abort ();
  165.  
  166.   if (type == error_mark_node)
  167.     {
  168.       type = void_type_node;
  169.       spec_size = 0;
  170.     }
  171.  
  172.   /* Usually the size and mode come from the data type without change.  */
  173.  
  174.   DECL_MODE (decl) = TYPE_MODE (type);
  175.   DECL_SIZE (decl) = TYPE_SIZE (type);
  176.   TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
  177.  
  178.   if (code == FIELD_DECL && DECL_BIT_FIELD (decl))
  179.     {
  180.       /* This is a bit-field.  We don't know how to handle
  181.      them except for integers and enums, and front end should
  182.      never generate them otherwise.  */
  183.  
  184.       if (! (TREE_CODE (type) == INTEGER_TYPE
  185.          || TREE_CODE (type) == ENUMERAL_TYPE))
  186.     abort ();
  187.  
  188.       if (spec_size == 0 && DECL_NAME (decl) != 0)
  189.     abort ();
  190.  
  191.       /* Size is specified number of bits.  */
  192.       DECL_SIZE (decl) = size_int (spec_size);
  193.     }
  194.   /* Force alignment required for the data type.
  195.      But if the decl itself wants greater alignment, don't override that.
  196.      Likewise, if the decl is packed, don't override it.  */
  197.   else if (DECL_ALIGN (decl) == 0
  198.        || (! DECL_PACKED (decl) &&  TYPE_ALIGN (type) > DECL_ALIGN (decl)))
  199.     DECL_ALIGN (decl) = TYPE_ALIGN (type);
  200.  
  201.   /* See if we can use an ordinary integer mode for a bit-field.  */
  202.   /* Conditions are: a fixed size that is correct for another mode
  203.      and occupying a complete byte or bytes on proper boundary.  */
  204.   if (code == FIELD_DECL)
  205.     {
  206.       DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
  207.       if (maximum_field_alignment != 0)
  208.     DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
  209.     }
  210.  
  211.   if (DECL_BIT_FIELD (decl)
  212.       && TYPE_SIZE (type) != 0
  213.       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
  214.     {
  215.       register enum machine_mode xmode
  216.     = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1);
  217.  
  218.       if (xmode != BLKmode
  219.       && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
  220.     {
  221.       DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
  222.                    DECL_ALIGN (decl));
  223.       DECL_MODE (decl) = xmode;
  224.       DECL_SIZE (decl) = size_int (GET_MODE_BITSIZE (xmode));
  225.       /* This no longer needs to be accessed as a bit field.  */
  226.       DECL_BIT_FIELD (decl) = 0;
  227.     }
  228.     }
  229.  
  230.   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
  231.   if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
  232.     DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
  233. }
  234.  
  235. /* Lay out a RECORD_TYPE type (a C struct).
  236.    This means laying out the fields, determining their positions,
  237.    and computing the overall size and required alignment of the record.
  238.    Note that if you set the TYPE_ALIGN before calling this
  239.    then the struct is aligned to at least that boundary.
  240.  
  241.    If the type has basetypes, you must call layout_basetypes
  242.    before calling this function.
  243.  
  244.    The return value is a list of static members of the record.
  245.    They still need to be laid out.  */
  246.  
  247. static tree
  248. layout_record (rec)
  249.      tree rec;
  250. {
  251.   register tree field;
  252. #ifdef STRUCTURE_SIZE_BOUNDARY
  253.   unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
  254. #else
  255.   unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
  256. #endif
  257.   /* These must be laid out *after* the record is.  */
  258.   tree pending_statics = NULL_TREE;
  259.   /* Record size so far is CONST_SIZE + VAR_SIZE bits,
  260.      where CONST_SIZE is an integer
  261.      and VAR_SIZE is a tree expression.
  262.      If VAR_SIZE is null, the size is just CONST_SIZE.
  263.      Naturally we try to avoid using VAR_SIZE.  */
  264.   register int const_size = 0;
  265.   register tree var_size = 0;
  266.   /* Once we start using VAR_SIZE, this is the maximum alignment
  267.      that we know VAR_SIZE has.  */