Annotation of gcc/stor-layout.c, revision 1.1.1.2

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.