OSDN Git Service

* system.h (CEIL): Define.
[pf3gnuchains/gcc-fork.git] / gcc / stor-layout.c
1 /* C-compiler utilities for types and variables storage layout
2    Copyright (C) 1987, 88, 92-98, 1999 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21
22 #include "config.h"
23 #include "system.h"
24 #include "tree.h"
25 #include "rtl.h"
26 #include "tm_p.h"
27 #include "flags.h"
28 #include "function.h"
29 #include "expr.h"
30 #include "toplev.h"
31 #include "ggc.h"
32
33 /* Data type for the expressions representing sizes of data types.
34    It is the first integer type laid out.  */
35
36 struct sizetype_tab sizetype_tab;
37
38 /* If nonzero, this is an upper limit on alignment of structure fields.
39    The value is measured in bits.  */
40 int maximum_field_alignment;
41
42 /* If non-zero, the alignment of a bitstring or (power-)set value, in bits.
43    May be overridden by front-ends.  */
44 int set_alignment = 0;
45
46 static tree layout_record       PROTO((tree));
47 static void layout_union        PROTO((tree));
48 \f
49 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded.  */
50
51 static tree pending_sizes;
52
53 /* Nonzero means cannot safely call expand_expr now,
54    so put variable sizes onto `pending_sizes' instead.  */
55
56 int immediate_size_expand;
57
58 tree
59 get_pending_sizes ()
60 {
61   tree chain = pending_sizes;
62   tree t;
63
64   /* Put each SAVE_EXPR into the current function.  */
65   for (t = chain; t; t = TREE_CHAIN (t))
66     SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
67
68   pending_sizes = 0;
69   return chain;
70 }
71
72 void
73 put_pending_sizes (chain)
74      tree chain;
75 {
76   if (pending_sizes)
77     abort ();
78
79   pending_sizes = chain;
80 }
81
82 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
83    to serve as the actual size-expression for a type or decl.  */
84
85 tree
86 variable_size (size)
87      tree size;
88 {
89   /* If the language-processor is to take responsibility for variable-sized
90      items (e.g., languages which have elaboration procedures like Ada),
91      just return SIZE unchanged.  Likewise for self-referential sizes.  */
92   if (TREE_CONSTANT (size)
93       || global_bindings_p () < 0 || contains_placeholder_p (size))
94     return size;
95
96   size = save_expr (size);
97
98   /* If an array with a variable number of elements is declared, and
99      the elements require destruction, we will emit a cleanup for the
100      array.  That cleanup is run both on normal exit from the block
101      and in the exception-handler for the block.  Normally, when code
102      is used in both ordinary code and in an exception handler it is
103      `unsaved', i.e., all SAVE_EXPRs are recalculated.  However, we do
104      not wish to do that here; the array-size is the same in both
105      places.  */
106   if (TREE_CODE (size) == SAVE_EXPR)
107     SAVE_EXPR_PERSISTENT_P (size) = 1;
108
109   if (global_bindings_p ())
110     {
111       if (TREE_CONSTANT (size))
112         error ("type size can't be explicitly evaluated");
113       else
114         error ("variable-size type declared outside of any function");
115
116       return size_int (1);
117     }
118
119   if (immediate_size_expand)
120     /* NULL_RTX is not defined; neither is the rtx type. 
121        Also, we would like to pass const0_rtx here, but don't have it.  */
122     expand_expr (size, expand_expr (integer_zero_node, NULL_PTR, VOIDmode, 0),
123                  VOIDmode, 0);
124   else if (cfun != 0
125            && cfun->x_dont_save_pending_sizes_p)
126     /* The front-end doesn't want us to keep a list of the expressions
127        that determine sizes for variable size objects.  */
128     ;
129   else
130     pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
131
132   return size;
133 }
134 \f
135 #ifndef MAX_FIXED_MODE_SIZE
136 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
137 #endif
138
139 /* Return the machine mode to use for a nonscalar of SIZE bits.
140    The mode must be in class CLASS, and have exactly that many bits.
141    If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
142    be used.  */
143
144 enum machine_mode
145 mode_for_size (size, class, limit)
146      unsigned int size;
147      enum mode_class class;
148      int limit;
149 {
150   register enum machine_mode mode;
151
152   if (limit && size > (unsigned int)(MAX_FIXED_MODE_SIZE))
153     return BLKmode;
154
155   /* Get the first mode which has this size, in the specified class.  */
156   for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
157        mode = GET_MODE_WIDER_MODE (mode))
158     if ((unsigned int)GET_MODE_BITSIZE (mode) == size)
159       return mode;
160
161   return BLKmode;
162 }
163
164 /* Similar, but never return BLKmode; return the narrowest mode that
165    contains at least the requested number of bits.  */
166
167 enum machine_mode
168 smallest_mode_for_size (size, class)
169      unsigned int size;
170      enum mode_class class;
171 {
172   register enum machine_mode mode;
173
174   /* Get the first mode which has at least this size, in the
175      specified class.  */
176   for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
177        mode = GET_MODE_WIDER_MODE (mode))
178     if ((unsigned int)GET_MODE_BITSIZE (mode) >= size)
179       return mode;
180
181   abort ();
182 }
183
184 /* Find an integer mode of the exact same size, or BLKmode on failure.  */
185
186 enum machine_mode
187 int_mode_for_mode (mode)
188      enum machine_mode mode;
189 {
190   switch (GET_MODE_CLASS (mode))
191     {
192     case MODE_INT:
193     case MODE_PARTIAL_INT:
194       break;
195
196     case MODE_COMPLEX_INT:
197     case MODE_COMPLEX_FLOAT:
198     case MODE_FLOAT:
199       mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
200       break;
201
202     case MODE_RANDOM:
203       if (mode == BLKmode)
204         break;
205
206       /* ... fall through ... */
207
208     case MODE_CC:
209     default:
210       abort();
211     }
212
213   return mode;
214 }
215
216 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.  */
217
218 tree
219 round_up (value, divisor)
220      tree value;
221      int divisor;
222 {
223   return size_binop (MULT_EXPR,
224                      size_binop (CEIL_DIV_EXPR, value, size_int (divisor)),
225                      size_int (divisor));
226 }
227 \f
228 /* Set the size, mode and alignment of a ..._DECL node.
229    TYPE_DECL does need this for C++.
230    Note that LABEL_DECL and CONST_DECL nodes do not need this,
231    and FUNCTION_DECL nodes have them set up in a special (and simple) way.
232    Don't call layout_decl for them.
233
234    KNOWN_ALIGN is the amount of alignment we can assume this
235    decl has with no special effort.  It is relevant only for FIELD_DECLs
236    and depends on the previous fields.
237    All that matters about KNOWN_ALIGN is which powers of 2 divide it.
238    If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
239    the record will be aligned to suit.  */
240
241 void
242 layout_decl (decl, known_align)
243      tree decl;
244      unsigned known_align;
245 {
246   register tree type = TREE_TYPE (decl);
247   register enum tree_code code = TREE_CODE (decl);
248   int spec_size = DECL_FIELD_SIZE (decl);
249
250   if (code == CONST_DECL)
251     return;
252
253   if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
254       && code != FIELD_DECL && code != TYPE_DECL)
255     abort ();
256
257   if (type == error_mark_node)
258     {
259       type = void_type_node;
260       spec_size = 0;
261     }
262
263   /* Usually the size and mode come from the data type without change.  */
264
265   DECL_MODE (decl) = TYPE_MODE (type);
266   TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
267   if (DECL_SIZE (decl) == 0)
268     DECL_SIZE (decl) = TYPE_SIZE (type);
269
270   if (code == FIELD_DECL && DECL_BIT_FIELD (decl))
271     {
272       if (spec_size == 0 && DECL_NAME (decl) != 0)
273         abort ();
274
275       /* Size is specified in number of bits.  */
276       DECL_SIZE (decl) = bitsize_int (spec_size, 0);
277     }
278   /* Force alignment required for the data type.
279      But if the decl itself wants greater alignment, don't override that.
280      Likewise, if the decl is packed, don't override it.  */
281   else if (DECL_ALIGN (decl) == 0
282            || (! DECL_PACKED (decl) &&  TYPE_ALIGN (type) > DECL_ALIGN (decl)))
283     DECL_ALIGN (decl) = TYPE_ALIGN (type);
284
285   /* See if we can use an ordinary integer mode for a bit-field. 
286      Conditions are: a fixed size that is correct for another mode
287      and occupying a complete byte or bytes on proper boundary.  */
288   if (code == FIELD_DECL)
289     {
290       DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
291       if (maximum_field_alignment != 0)
292         DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl),
293                                  (unsigned)maximum_field_alignment);
294       else if (DECL_PACKED (decl))
295         DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
296     }
297
298   if (DECL_BIT_FIELD (decl)
299       && TYPE_SIZE (type) != 0
300       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
301       && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
302     {
303       register enum machine_mode xmode
304         = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1);
305
306       if (xmode != BLKmode
307           && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
308         {
309           DECL_ALIGN (decl) = MAX ((unsigned) GET_MODE_ALIGNMENT (xmode),
310                                    DECL_ALIGN (decl));
311           DECL_MODE (decl) = xmode;
312           DECL_SIZE (decl) = bitsize_int (GET_MODE_BITSIZE (xmode), 0);
313           /* This no longer needs to be accessed as a bit field.  */
314           DECL_BIT_FIELD (decl) = 0;
315         }
316     }
317
318   /* Turn off DECL_BIT_FIELD if we won't need it set.  */
319   if (DECL_BIT_FIELD (decl) && TYPE_MODE (type) == BLKmode
320       && known_align % TYPE_ALIGN (type) == 0
321       && DECL_SIZE (decl) != 0
322       && (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST
323           || (TREE_INT_CST_LOW (DECL_SIZE (decl)) % BITS_PER_UNIT) == 0)
324       && DECL_ALIGN (decl) >= TYPE_ALIGN (type))
325     DECL_BIT_FIELD (decl) = 0;
326
327   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
328   if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
329     DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
330 }
331 \f
332 /* Lay out a RECORD_TYPE type (a C struct).
333    This means laying out the fields, determining their positions,
334    and computing the overall size and required alignment of the record.
335    Note that if you set the TYPE_ALIGN before calling this
336    then the struct is aligned to at least that boundary.
337
338    If the type has basetypes, you must call layout_basetypes
339    before calling this function.
340
341    The return value is a list of static members of the record.
342    They still need to be laid out.  */
343
344 static tree
345 layout_record (rec)
346      tree rec;
347 {
348   register tree field;
349   unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
350   unsigned unpacked_align = record_align;
351   /* These must be laid out *after* the record is.  */
352   tree pending_statics = NULL_TREE;
353   /* Record size so far is CONST_SIZE + VAR_SIZE bits,
354      where CONST_SIZE is an integer
355      and VAR_SIZE is a tree expression.
356      If VAR_SIZE is null, the size is just CONST_SIZE.
357      Naturally we try to avoid using VAR_SIZE.  */
358   register HOST_WIDE_INT const_size = 0;
359   register tree var_size = 0;
360   /* Once we start using VAR_SIZE, this is the maximum alignment
361      that we know VAR_SIZE has.  */
362   register int var_align = BITS_PER_UNIT;
363   int packed_maybe_necessary = 0;
364
365 #ifdef STRUCTURE_SIZE_BOUNDARY
366   /* Packed structures don't need to have minimum size.  */
367   if (! TYPE_PACKED (rec))
368     record_align = MAX (record_align, STRUCTURE_SIZE_BOUNDARY);
369 #endif
370
371   for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
372     {
373       register int known_align = var_size ? var_align : const_size;
374       register int desired_align = 0;
375       tree type = TREE_TYPE (field);
376
377       /* If FIELD is static, then treat it like a separate variable,
378          not really like a structure field.
379          If it is a FUNCTION_DECL, it's a method.
380          In both cases, all we do is lay out the decl,
381          and we do it *after* the record is laid out.  */
382
383       if (TREE_CODE (field) == VAR_DECL)
384         {
385           pending_statics = tree_cons (NULL_TREE, field, pending_statics);
386           continue;
387         }
388
389       /* Enumerators and enum types which are local to this class need not
390          be laid out.  Likewise for initialized constant fields.  */
391       if (TREE_CODE (field) != FIELD_DECL)
392         continue;
393
394       /* Lay out the field so we know what alignment it needs.
395          For a packed field, use the alignment as specified,
396          disregarding what the type would want.  */
397       if (DECL_PACKED (field))
398         desired_align = DECL_ALIGN (field);
399       layout_decl (field, known_align);
400       if (! DECL_PACKED (field))
401         desired_align = DECL_ALIGN (field);
402       /* Some targets (i.e. VMS) limit struct field alignment
403          to a lower boundary than alignment of variables.  */
404 #ifdef BIGGEST_FIELD_ALIGNMENT
405       desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
406 #endif
407 #ifdef ADJUST_FIELD_ALIGN
408       desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
409 #endif
410
411       /* Record must have at least as much alignment as any field.
412          Otherwise, the alignment of the field within the record
413          is meaningless.  */
414
415 #ifdef PCC_BITFIELD_TYPE_MATTERS
416       if (PCC_BITFIELD_TYPE_MATTERS && type != error_mark_node
417           && DECL_BIT_FIELD_TYPE (field)
418           && ! integer_zerop (TYPE_SIZE (type)))
419         {
420           /* For these machines, a zero-length field does not
421              affect the alignment of the structure as a whole.
422              It does, however, affect the alignment of the next field
423              within the structure.  */
424           if (! integer_zerop (DECL_SIZE (field)))
425             record_align = MAX ((int)record_align, desired_align);
426           else if (! DECL_PACKED (field))
427             desired_align = TYPE_ALIGN (type);
428           /* A named bit field of declared type `int'
429              forces the entire structure to have `int' alignment.  */
430           if (DECL_NAME (field) != 0)
431             {
432               int type_align = TYPE_ALIGN (type);
433               if (maximum_field_alignment != 0)
434                 type_align = MIN (type_align, maximum_field_alignment);
435               else if (DECL_PACKED (field))
436                 type_align = MIN (type_align, BITS_PER_UNIT);
437
438               record_align = MAX ((int) record_align, type_align);
439               if (warn_packed)
440                 unpacked_align = MAX (unpacked_align, TYPE_ALIGN (type));
441             }
442         }
443       else
444 #endif
445         {
446           record_align = MAX ((int) record_align, desired_align);
447           if (warn_packed)
448             unpacked_align = MAX (unpacked_align, TYPE_ALIGN (type));
449         }
450
451       if (warn_packed && DECL_PACKED (field))
452         {
453           if (const_size % TYPE_ALIGN (type) == 0
454               || (var_align % TYPE_ALIGN (type) == 0
455                   && var_size != NULL_TREE))
456             {
457               if (TYPE_ALIGN (type) > desired_align)
458                 {
459                   if (STRICT_ALIGNMENT)
460                     warning_with_decl (field, "packed attribute causes inefficient alignment for `%s'");
461                   else
462                     warning_with_decl (field, "packed attribute is unnecessary for `%s'");
463                 }
464             }
465           else
466             packed_maybe_necessary = 1;
467         }
468
469       /* Does this field automatically have alignment it needs
470          by virtue of the fields that precede it and the record's
471          own alignment?  */
472
473       if (const_size % desired_align != 0
474           || (var_align % desired_align != 0
475               && var_size != NULL_TREE))
476         {
477           /* No, we need to skip space before this field.
478              Bump the cumulative size to multiple of field alignment.  */
479
480           if (warn_padded)
481             warning_with_decl (field, "padding struct to align `%s'");
482
483           if (var_size == NULL_TREE
484               || var_align % desired_align == 0)
485             const_size
486               = CEIL (const_size, desired_align) * desired_align;
487           else
488             {
489               if (const_size > 0)
490                 var_size = size_binop (PLUS_EXPR, var_size,
491                                        bitsize_int (const_size, 0L));
492               const_size = 0;
493               var_size = round_up (var_size, desired_align);
494               var_align = MIN (var_align, desired_align);
495             }
496         }
497
498 #ifdef PCC_BITFIELD_TYPE_MATTERS
499       if (PCC_BITFIELD_TYPE_MATTERS
500           && TREE_CODE (field) == FIELD_DECL
501           && type != error_mark_node
502           && DECL_BIT_FIELD_TYPE (field)
503           && !DECL_PACKED (field)
504           && maximum_field_alignment == 0
505           && !integer_zerop (DECL_SIZE (field)))
506         {
507           int type_align = TYPE_ALIGN (type);
508           register tree dsize = DECL_SIZE (field);
509           int field_size = TREE_INT_CST_LOW (dsize);
510
511           /* A bit field may not span more units of alignment of its type
512              than its type itself.  Advance to next boundary if necessary.  */
513           if (((const_size + field_size + type_align - 1) / type_align
514                - const_size / type_align)
515               > TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (field))) / type_align)
516             const_size = CEIL (const_size, type_align) * type_align;
517         }
518 #endif
519
520 /* No existing machine description uses this parameter.
521    So I have made it in this aspect identical to PCC_BITFIELD_TYPE_MATTERS.  */
522 #ifdef BITFIELD_NBYTES_LIMITED
523       if (BITFIELD_NBYTES_LIMITED
524           && TREE_CODE (field) == FIELD_DECL
525           && type != error_mark_node
526           && DECL_BIT_FIELD_TYPE (field)
527           && !DECL_PACKED (field)
528           && !integer_zerop (DECL_SIZE (field)))
529         {
530           int type_align = TYPE_ALIGN (type);
531           register tree dsize = DECL_SIZE (field);
532           int field_size = TREE_INT_CST_LOW (dsize);
533
534           if (maximum_field_alignment != 0)
535             type_align = MIN (type_align, maximum_field_alignment);
536           /* ??? This test is opposite the test in the containing if
537              statement, so this code is unreachable currently.  */
538           else if (DECL_PACKED (field))
539             type_align = MIN (type_align, BITS_PER_UNIT);
540
541           /* A bit field may not span the unit of alignment of its type.
542              Advance to next boundary if necessary.  */
543           /* ??? This code should match the code above for the
544              PCC_BITFIELD_TYPE_MATTERS case.  */
545           if (const_size / type_align
546               != (const_size + field_size - 1) / type_align)
547             const_size = CEIL (const_size, type_align) * type_align;
548         }
549 #endif
550
551       /* Size so far becomes the position of this field.  */
552
553       if (var_size && const_size)
554         DECL_FIELD_BITPOS (field)
555           = size_binop (PLUS_EXPR, var_size, bitsize_int (const_size, 0L));
556       else if (var_size)
557         DECL_FIELD_BITPOS (field) = var_size;
558       else
559         {
560           DECL_FIELD_BITPOS (field) = bitsize_int (const_size, 0L);
561
562           /* If this field ended up more aligned than we thought it
563              would be (we approximate this by seeing if its position
564              changed), lay out the field again; perhaps we can use an
565              integral mode for it now.  */
566           if (known_align != const_size)
567             layout_decl (field, const_size);
568         }
569
570       /* Now add size of this field to the size of the record.  */
571
572       {
573         register tree dsize = DECL_SIZE (field);
574
575         /* This can happen when we have an invalid nested struct definition,
576            such as struct j { struct j { int i; } }.  The error message is
577            printed in finish_struct.  */
578         if (dsize == 0)
579           /* Do nothing.  */;
580         else if (TREE_CODE (dsize) == INTEGER_CST
581                  && ! TREE_CONSTANT_OVERFLOW (dsize)
582                  && TREE_INT_CST_HIGH (dsize) == 0
583                  && TREE_INT_CST_LOW (dsize) + const_size >= const_size)
584           /* Use const_size if there's no overflow.  */
585           const_size += TREE_INT_CST_LOW (dsize);
586         else
587           {
588             if (var_size == NULL_TREE)
589               var_size = dsize;
590             else
591               var_size = size_binop (PLUS_EXPR, var_size, dsize);
592           }
593       }
594     }
595
596   /* Work out the total size and alignment of the record
597      as one expression and store in the record type.
598      Round it up to a multiple of the record's alignment.  */
599
600   if (var_size == NULL_TREE)
601     {
602       TYPE_SIZE (rec) = bitsize_int (const_size, 0L);
603     }
604   else
605     {
606       if (const_size)
607         var_size
608           = size_binop (PLUS_EXPR, var_size, bitsize_int (const_size, 0L));
609       TYPE_SIZE (rec) = var_size;
610     }
611
612   /* Determine the desired alignment.  */
613 #ifdef ROUND_TYPE_ALIGN
614   TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), record_align);
615 #else
616   TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), record_align);
617 #endif
618
619   /* Record the un-rounded size in the binfo node.  But first we check
620      the size of TYPE_BINFO to make sure that BINFO_SIZE is available.  */
621   if (TYPE_BINFO (rec) && TREE_VEC_LENGTH (TYPE_BINFO (rec)) > 6)
622     TYPE_BINFO_SIZE (rec) = TYPE_SIZE (rec);
623   
624   {
625     tree unpadded_size = TYPE_SIZE (rec);
626 #ifdef ROUND_TYPE_SIZE
627     TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
628 #else
629     /* Round the size up to be a multiple of the required alignment */
630     TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
631 #endif
632     if (warn_padded && var_size == NULL_TREE
633         && simple_cst_equal (unpadded_size, TYPE_SIZE (rec)) == 0)
634       warning ("padding struct size to alignment boundary");
635   }
636   
637   if (warn_packed && TYPE_PACKED (rec) && !packed_maybe_necessary
638       && var_size == NULL_TREE)
639     {
640       tree unpacked_size;
641       TYPE_PACKED (rec) = 0;
642 #ifdef ROUND_TYPE_ALIGN
643       unpacked_align
644         = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), unpacked_align);
645 #else
646       unpacked_align = MAX (TYPE_ALIGN (rec), unpacked_align);
647 #endif
648 #ifdef ROUND_TYPE_SIZE
649       unpacked_size = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), unpacked_align);
650 #else
651       unpacked_size = round_up (TYPE_SIZE (rec), unpacked_align);
652 #endif
653       if (simple_cst_equal (unpacked_size, TYPE_SIZE (rec)))
654         {
655           if (TYPE_NAME (rec))
656             {
657               char *name;
658               if (TREE_CODE (TYPE_NAME (rec)) == IDENTIFIER_NODE)
659                 name = IDENTIFIER_POINTER (TYPE_NAME (rec));
660               else
661                 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (rec)));
662               if (STRICT_ALIGNMENT)
663                 warning ("packed attribute causes inefficient alignment for `%s'", name);
664               else
665                 warning ("packed attribute is unnecessary for `%s'", name);
666             }
667           else
668             {
669               if (STRICT_ALIGNMENT)
670                 warning ("packed attribute causes inefficient alignment");
671               else
672                 warning ("packed attribute is unnecessary");
673             }
674         }
675       TYPE_PACKED (rec) = 1;
676     }
677
678   return pending_statics;
679 }
680 \f
681 /* Lay out a UNION_TYPE or QUAL_UNION_TYPE type.
682    Lay out all the fields, set their positions to zero,
683    and compute the size and alignment of the union (maximum of any field).
684    Note that if you set the TYPE_ALIGN before calling this
685    then the union align is aligned to at least that boundary.  */
686
687 static void
688 layout_union (rec)
689      tree rec;
690 {
691   register tree field;
692   unsigned union_align = BITS_PER_UNIT;
693
694   /* The size of the union, based on the fields scanned so far,
695      is max (CONST_SIZE, VAR_SIZE).
696      VAR_SIZE may be null; then CONST_SIZE by itself is the size.  */
697   register HOST_WIDE_INT const_size = 0;
698   register tree var_size = 0;
699
700 #ifdef STRUCTURE_SIZE_BOUNDARY
701   /* Packed structures don't need to have minimum size.  */
702   if (! TYPE_PACKED (rec))
703     union_align = STRUCTURE_SIZE_BOUNDARY;
704 #endif
705
706   /* If this is a QUAL_UNION_TYPE, we want to process the fields in
707      the reverse order in building the COND_EXPR that denotes its
708      size.  We reverse them again later.  */
709   if (TREE_CODE (rec) == QUAL_UNION_TYPE)
710     TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
711
712   for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
713     {
714       tree dsize;
715       
716       /* Enums which are local to this class need not be laid out.  */
717       if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
718         continue;
719
720       layout_decl (field, 0);
721       DECL_FIELD_BITPOS (field) = bitsize_int (0L, 0L);
722
723       /* Union must be at least as aligned as any field requires.  */
724
725       union_align = MAX (union_align, DECL_ALIGN (field));
726
727 #ifdef PCC_BITFIELD_TYPE_MATTERS
728       /* On the m88000, a bit field of declare type `int'
729          forces the entire union to have `int' alignment.  */
730       if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
731         union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
732 #endif
733
734       dsize = DECL_SIZE (field);
735       if (TREE_CODE (rec) == UNION_TYPE)
736         {
737           /* Set union_size to max (decl_size, union_size).
738              There are more and less general ways to do this.
739              Use only CONST_SIZE unless forced to use VAR_SIZE.  */
740
741           if (TREE_CODE (dsize) == INTEGER_CST
742               && ! TREE_CONSTANT_OVERFLOW (dsize)
743               && TREE_INT_CST_HIGH (dsize) == 0)
744             const_size
745               = MAX (const_size, TREE_INT_CST_LOW (dsize));
746           else if (var_size == 0)
747             var_size = dsize;
748           else
749             var_size = size_binop (MAX_EXPR, var_size, dsize);
750         }
751       else if (TREE_CODE (rec) == QUAL_UNION_TYPE)
752         var_size = fold (build (COND_EXPR, sizetype, DECL_QUALIFIER (field),
753                                 DECL_SIZE (field),
754                                 var_size ? var_size : bitsize_int (0L, 0L)));
755       }
756
757   if (TREE_CODE (rec) == QUAL_UNION_TYPE)
758     TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
759
760   /* Determine the ultimate size of the union (in bytes).  */
761   if (NULL == var_size)
762     TYPE_SIZE (rec) = bitsize_int (CEIL (const_size, BITS_PER_UNIT)
763                                    * BITS_PER_UNIT, 0L);
764   else if (const_size == 0)
765     TYPE_SIZE (rec) = var_size;
766   else
767     TYPE_SIZE (rec) = size_binop (MAX_EXPR, var_size,
768                                   round_up (bitsize_int (const_size, 0L),
769                                             BITS_PER_UNIT));
770
771   /* Determine the desired alignment.  */
772 #ifdef ROUND_TYPE_ALIGN
773   TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), union_align);
774 #else
775   TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
776 #endif
777
778 #ifdef ROUND_TYPE_SIZE
779   TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
780 #else
781   /* Round the size up to be a multiple of the required alignment */
782   TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
783 #endif
784 }
785 \f
786 /* Calculate the mode, size, and alignment for TYPE.
787    For an array type, calculate the element separation as well.
788    Record TYPE on the chain of permanent or temporary types
789    so that dbxout will find out about it.
790
791    TYPE_SIZE of a type is nonzero if the type has been laid out already.
792    layout_type does nothing on such a type.
793
794    If the type is incomplete, its TYPE_SIZE remains zero.  */
795
796 void
797 layout_type (type)
798      tree type;
799 {
800   int old;
801   tree pending_statics;
802
803   if (type == 0)
804     abort ();
805
806   /* Do nothing if type has been laid out before.  */
807   if (TYPE_SIZE (type))
808     return;
809
810   /* Make sure all nodes we allocate are not momentary;
811      they must last past the current statement.  */
812   old = suspend_momentary ();
813
814   /* Put all our nodes into the same obstack as the type.  Also,
815      make expressions saveable (this is a no-op for permanent types).  */
816
817   push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
818   saveable_allocation ();
819
820   switch (TREE_CODE (type))
821     {
822     case LANG_TYPE:
823       /* This kind of type is the responsibility
824          of the language-specific code.  */
825       abort ();
826
827     case BOOLEAN_TYPE:  /* Used for Java, Pascal, and Chill. */
828       if (TYPE_PRECISION (type) == 0)
829         TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
830
831       /* ... fall through ... */
832
833     case INTEGER_TYPE:
834     case ENUMERAL_TYPE:
835     case CHAR_TYPE:
836       if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
837           && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
838         TREE_UNSIGNED (type) = 1;
839
840       TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
841                                                  MODE_INT);
842       TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
843       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
844       break;
845
846     case REAL_TYPE:
847       TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
848       TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
849       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
850       break;
851
852     case COMPLEX_TYPE:
853       TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
854       TYPE_MODE (type)
855         = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
856                          (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
857                           ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
858                          0);
859       TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L);
860       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
861       break;
862
863     case VOID_TYPE:
864       TYPE_SIZE (type) = size_zero_node;
865       TYPE_SIZE_UNIT (type) = size_zero_node;
866       TYPE_ALIGN (type) = 1;
867       TYPE_MODE (type) = VOIDmode;
868       break;
869
870     case OFFSET_TYPE:
871       TYPE_SIZE (type) = bitsize_int (POINTER_SIZE, 0L);
872       TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
873       TYPE_MODE (type) = ptr_mode;
874       break;
875
876     case FUNCTION_TYPE:
877     case METHOD_TYPE:
878       TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
879       TYPE_SIZE (type) = bitsize_int (2 * POINTER_SIZE, 0);
880       TYPE_SIZE_UNIT (type) = size_int ((2 * POINTER_SIZE) / BITS_PER_UNIT);
881       break;
882
883     case POINTER_TYPE:
884     case REFERENCE_TYPE:
885       TYPE_MODE (type) = ptr_mode;
886       TYPE_SIZE (type) = bitsize_int (POINTER_SIZE, 0L);
887       TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
888       TREE_UNSIGNED (type) = 1;
889       TYPE_PRECISION (type) = POINTER_SIZE;
890       break;
891
892     case ARRAY_TYPE:
893       {
894         register tree index = TYPE_DOMAIN (type);
895         register tree element = TREE_TYPE (type);
896
897         build_pointer_type (element);
898
899         /* We need to know both bounds in order to compute the size.  */
900         if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
901             && TYPE_SIZE (element))
902           {
903             tree ub = TYPE_MAX_VALUE (index);
904             tree lb = TYPE_MIN_VALUE (index);
905             tree length;
906             tree element_size;
907
908             /* If UB is max (lb - 1, x), remove the MAX_EXPR since the
909                test for negative below covers it.  */
910             if (TREE_CODE (ub) == MAX_EXPR
911                 && TREE_CODE (TREE_OPERAND (ub, 0)) == MINUS_EXPR
912                 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 0), 1))
913                 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 0), 0),
914                                     lb, 0))
915               ub = TREE_OPERAND (ub, 1);
916             else if (TREE_CODE (ub) == MAX_EXPR
917                      && TREE_CODE (TREE_OPERAND (ub, 1)) == MINUS_EXPR
918                      && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 1), 1))
919                      && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 1),
920                                                        0),
921                                          lb, 0))
922               ub = TREE_OPERAND (ub, 0);
923
924             /* The initial subtraction should happen in the original type so
925                that (possible) negative values are handled appropriately.  */
926             length = size_binop (PLUS_EXPR, size_one_node,
927                                  fold (build (MINUS_EXPR, TREE_TYPE (lb),
928                                               ub, lb)));
929
930             /* If neither bound is a constant and sizetype is signed, make
931                sure the size is never negative.  We should really do this
932                if *either* bound is non-constant, but this is the best
933                compromise between C and Ada.  */
934             if (! TREE_UNSIGNED (sizetype)
935                 && TREE_CODE (TYPE_MIN_VALUE (index)) != INTEGER_CST
936                 && TREE_CODE (TYPE_MAX_VALUE (index)) != INTEGER_CST)
937               length = size_binop (MAX_EXPR, length, size_zero_node);
938
939             /* Special handling for arrays of bits (for Chill).  */
940             element_size = TYPE_SIZE (element);
941             if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element))
942               {
943                 HOST_WIDE_INT maxvalue
944                   = TREE_INT_CST_LOW (TYPE_MAX_VALUE (element));
945                 HOST_WIDE_INT minvalue
946                   = TREE_INT_CST_LOW (TYPE_MIN_VALUE (element));
947
948                 if (maxvalue - minvalue == 1
949                     && (maxvalue == 1 || maxvalue == 0))
950                   element_size = integer_one_node;
951               }
952
953             TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size, length);
954
955             /* If we know the size of the element, calculate the total
956                size directly, rather than do some division thing below.
957                This optimization helps Fortran assumed-size arrays
958                (where the size of the array is determined at runtime)
959                substantially.
960                Note that we can't do this in the case where the size of
961                the elements is one bit since TYPE_SIZE_UNIT cannot be
962                set correctly in that case.  */
963             if (TYPE_SIZE_UNIT (element) != 0
964                 && element_size != integer_one_node)
965               TYPE_SIZE_UNIT (type)
966                 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
967           }
968
969         /* Now round the alignment and size,
970            using machine-dependent criteria if any.  */
971
972 #ifdef ROUND_TYPE_ALIGN
973         TYPE_ALIGN (type)
974           = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
975 #else
976         TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
977 #endif
978
979 #ifdef ROUND_TYPE_SIZE
980         if (TYPE_SIZE (type) != 0)
981           {
982             tree tmp
983               = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
984
985             /* If the rounding changed the size of the type, remove any
986                pre-calculated TYPE_SIZE_UNIT.  */
987             if (simple_cst_equal (TYPE_SIZE (type), tmp) != 1)
988               TYPE_SIZE_UNIT (type) = NULL;
989
990             TYPE_SIZE (type) = tmp;
991           }
992 #endif
993
994         TYPE_MODE (type) = BLKmode;
995         if (TYPE_SIZE (type) != 0
996             && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
997             /* BLKmode elements force BLKmode aggregate;
998                else extract/store fields may lose.  */
999             && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
1000                 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
1001           {
1002             TYPE_MODE (type)
1003               = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
1004                                MODE_INT, 1);
1005
1006             if (STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
1007                 && ((int) TYPE_ALIGN (type)
1008                     < TREE_INT_CST_LOW (TYPE_SIZE (type)))
1009                 && TYPE_MODE (type) != BLKmode)
1010               {
1011                 TYPE_NO_FORCE_BLK (type) = 1;
1012                 TYPE_MODE (type) = BLKmode;
1013               }
1014           }
1015         break;
1016       }
1017
1018     case RECORD_TYPE:
1019       pending_statics = layout_record (type);
1020       TYPE_MODE (type) = BLKmode;
1021       if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
1022         {
1023           tree field;
1024           enum machine_mode mode = VOIDmode;
1025
1026           /* A record which has any BLKmode members must itself be BLKmode;
1027              it can't go in a register.
1028              Unless the member is BLKmode only because it isn't aligned.  */
1029           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1030             {
1031               int bitpos;
1032
1033               if (TREE_CODE (field) != FIELD_DECL
1034                   || TREE_CODE (TREE_TYPE (field)) == ERROR_MARK)
1035                 continue;
1036
1037               if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1038                   && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1039                 goto record_lose;
1040
1041               if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
1042                 goto record_lose;
1043
1044               bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
1045
1046               /* Must be BLKmode if any field crosses a word boundary,
1047                  since extract_bit_field can't handle that in registers.  */
1048               if (bitpos / BITS_PER_WORD
1049                   != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
1050                       / BITS_PER_WORD)
1051                   /* But there is no problem if the field is entire words.  */
1052                   && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD != 0)
1053                 goto record_lose;
1054
1055               /* If this field is the whole struct, remember its mode so
1056                  that, say, we can put a double in a class into a DF
1057                  register instead of forcing it to live in the stack.  */
1058               if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
1059                 mode = DECL_MODE (field);
1060             }
1061
1062           if (mode != VOIDmode)
1063             /* We only have one real field; use its mode.  */
1064             TYPE_MODE (type) = mode;
1065           else
1066             TYPE_MODE (type)
1067               = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
1068                                MODE_INT, 1);
1069
1070           /* If structure's known alignment is less than
1071              what the scalar mode would need, and it matters,
1072              then stick with BLKmode.  */
1073           if (STRICT_ALIGNMENT
1074               && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1075                     || ((int) TYPE_ALIGN (type)
1076                         >= TREE_INT_CST_LOW (TYPE_SIZE (type)))))
1077             {
1078               if (TYPE_MODE (type) != BLKmode)
1079                 /* If this is the only reason this type is BLKmode,
1080                    then don't force containing types to be BLKmode.  */
1081                 TYPE_NO_FORCE_BLK (type) = 1;
1082               TYPE_MODE (type) = BLKmode;
1083             }
1084
1085         record_lose: ;
1086         }
1087
1088       /* Lay out any static members.  This is done now
1089          because their type may use the record's type.  */
1090       while (pending_statics)
1091         {
1092           layout_decl (TREE_VALUE (pending_statics), 0);
1093           pending_statics = TREE_CHAIN (pending_statics);
1094         }
1095       break;
1096
1097     case UNION_TYPE:
1098     case QUAL_UNION_TYPE:
1099       layout_union (type);
1100       TYPE_MODE (type) = BLKmode;
1101       if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
1102           /* If structure's known alignment is less than
1103              what the scalar mode would need, and it matters,
1104              then stick with BLKmode.  */
1105           && (! STRICT_ALIGNMENT
1106               || TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1107               || (int)TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type))))
1108         {
1109           tree field;
1110           /* A union which has any BLKmode members must itself be BLKmode;
1111              it can't go in a register.
1112              Unless the member is BLKmode only because it isn't aligned.  */
1113           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1114             {
1115               if (TREE_CODE (field) != FIELD_DECL)
1116                 continue;
1117
1118               if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1119                   && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1120                 goto union_lose;
1121             }
1122
1123           TYPE_MODE (type)
1124             = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
1125                              MODE_INT, 1);
1126
1127         union_lose: ;
1128         }
1129       break;
1130
1131     case SET_TYPE:  /* Used by Chill and Pascal. */
1132       if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1133           || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1134         abort();
1135       else
1136         {
1137 #ifndef SET_WORD_SIZE
1138 #define SET_WORD_SIZE BITS_PER_WORD
1139 #endif
1140           int alignment = set_alignment ? set_alignment : SET_WORD_SIZE;
1141           int size_in_bits
1142             = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1143                - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
1144           int rounded_size
1145             = ((size_in_bits + alignment - 1) / alignment) * alignment;
1146           if (rounded_size > alignment)
1147             TYPE_MODE (type) = BLKmode;
1148           else
1149             TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1150           TYPE_SIZE (type) = bitsize_int (rounded_size, 0L);
1151           TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1152           TYPE_ALIGN (type) = alignment;
1153           TYPE_PRECISION (type) = size_in_bits;
1154         }
1155       break;
1156
1157     case FILE_TYPE:
1158       /* The size may vary in different languages, so the language front end
1159          should fill in the size.  */
1160       TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1161       TYPE_MODE  (type) = BLKmode;
1162       break;
1163
1164     default:
1165       abort ();
1166     } /* end switch */
1167
1168   /* Normally, use the alignment corresponding to the mode chosen.
1169      However, where strict alignment is not required, avoid
1170      over-aligning structures, since most compilers do not do this
1171      alignment.  */
1172
1173   if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1174       && (STRICT_ALIGNMENT
1175           || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1176               && TREE_CODE (type) != QUAL_UNION_TYPE
1177               && TREE_CODE (type) != ARRAY_TYPE)))
1178     TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1179
1180   /* Do machine-dependent extra alignment.  */
1181 #ifdef ROUND_TYPE_ALIGN
1182   TYPE_ALIGN (type)
1183     = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1184 #endif
1185
1186 #ifdef ROUND_TYPE_SIZE
1187   if (TYPE_SIZE (type) != 0)
1188     TYPE_SIZE (type)
1189       = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1190 #endif
1191
1192   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
1193   if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1194     TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1195
1196   /* If we failed to find a simple way to calculate the unit size
1197      of the type above, find it by division.  */
1198   if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1199     /* TYPE_SIZE (type) is computed in bitsizetype.  After the division, the
1200        result will fit in sizetype.  We will get more efficient code using
1201        sizetype, so we force a conversion.  */
1202     TYPE_SIZE_UNIT (type)
1203       = convert (sizetype,
1204                  size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1205                              size_int (BITS_PER_UNIT)));
1206
1207   /* Once again evaluate only once, either now or as soon as safe.  */
1208   if (TYPE_SIZE_UNIT (type) != 0
1209       && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1210     TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1211
1212   /* Also layout any other variants of the type.  */
1213   if (TYPE_NEXT_VARIANT (type)
1214       || type != TYPE_MAIN_VARIANT (type))
1215     {
1216       tree variant;
1217       /* Record layout info of this variant.  */
1218       tree size = TYPE_SIZE (type);
1219       tree size_unit = TYPE_SIZE_UNIT (type);
1220       int align = TYPE_ALIGN (type);
1221       enum machine_mode mode = TYPE_MODE (type);
1222
1223       /* Copy it into all variants.  */
1224       for (variant = TYPE_MAIN_VARIANT (type);
1225            variant != 0;
1226            variant = TYPE_NEXT_VARIANT (variant))
1227         {
1228           TYPE_SIZE (variant) = size;
1229           TYPE_SIZE_UNIT (variant) = size_unit;
1230           TYPE_ALIGN (variant) = align;
1231           TYPE_MODE (variant) = mode;
1232         }
1233     }
1234         
1235   pop_obstacks ();
1236   resume_momentary (old);
1237 }
1238 \f
1239 /* Create and return a type for signed integers of PRECISION bits.  */
1240
1241 tree
1242 make_signed_type (precision)
1243      int precision;
1244 {
1245   register tree type = make_node (INTEGER_TYPE);
1246
1247   TYPE_PRECISION (type) = precision;
1248
1249   /* Create the extreme values based on the number of bits.  */
1250
1251   TYPE_MIN_VALUE (type)
1252     = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1253                     ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1254                    (((HOST_WIDE_INT) (-1)
1255                      << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1256                          ? precision - HOST_BITS_PER_WIDE_INT - 1
1257                          : 0))));
1258   TYPE_MAX_VALUE (type)
1259     = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1260                     ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1261                    (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1262                     ? (((HOST_WIDE_INT) 1
1263                         << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1264                     : 0));
1265
1266   /* Give this type's extreme values this type as their type.  */
1267
1268   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1269   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1270
1271   /* The first type made with this or `make_unsigned_type'
1272      is the type for size values.  */
1273   if (sizetype == 0)
1274     set_sizetype (type);
1275
1276   /* Lay out the type: set its alignment, size, etc.  */
1277
1278   layout_type (type);
1279   return type;
1280 }
1281
1282 /* Create and return a type for unsigned integers of PRECISION bits.  */
1283
1284 tree
1285 make_unsigned_type (precision)
1286      int precision;
1287 {
1288   register tree type = make_node (INTEGER_TYPE);
1289
1290   TYPE_PRECISION (type) = precision;
1291
1292   /* The first type made with this or `make_signed_type'
1293      is the type for size values.  */
1294
1295   if (sizetype == 0)
1296     {
1297       TREE_UNSIGNED (type) = 1;
1298       set_sizetype (type);
1299     }
1300
1301   fixup_unsigned_type (type);
1302   return type;
1303 }
1304
1305 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1306    Also update the type of any standard type's sizes made so far.  */
1307
1308 void
1309 set_sizetype (type)
1310      tree type;
1311 {
1312   int oprecision = TYPE_PRECISION (type);
1313   /* The *bitsizetype types use a precision that avoids overflows when
1314      calculating signed sizes / offsets in bits.  However, when
1315      cross-compiling from a 32 bit to a 64 bit host, we are limited to 64 bit
1316      precision.  */
1317   int precision = MIN (oprecision + BITS_PER_UNIT_LOG + 1,
1318                        2 * HOST_BITS_PER_WIDE_INT);
1319
1320   sizetype = type;
1321   bitsizetype = make_node (INTEGER_TYPE);
1322   TYPE_NAME (bitsizetype) = TYPE_NAME (type);
1323   TYPE_PRECISION (bitsizetype) = precision;
1324
1325   if (TREE_UNSIGNED (type))
1326     fixup_unsigned_type (bitsizetype);
1327   else
1328     fixup_signed_type (bitsizetype);
1329
1330   layout_type (bitsizetype);
1331
1332   if (TREE_UNSIGNED (type))
1333     {
1334       usizetype = sizetype;
1335       ubitsizetype = bitsizetype;
1336       ssizetype = make_signed_type (oprecision);
1337       sbitsizetype = make_signed_type (precision);
1338     }
1339   else
1340     {
1341       ssizetype = sizetype;
1342       sbitsizetype = bitsizetype;
1343       usizetype = make_unsigned_type (oprecision);
1344       ubitsizetype = make_unsigned_type (precision);
1345     }
1346   TYPE_NAME (bitsizetype) = TYPE_NAME (sizetype);
1347
1348   ggc_add_tree_root ((tree *) &sizetype_tab,
1349                      sizeof sizetype_tab / sizeof (tree));
1350 }
1351
1352 /* Set the extreme values of TYPE based on its precision in bits,
1353    then lay it out.  Used when make_signed_type won't do
1354    because the tree code is not INTEGER_TYPE.
1355    E.g. for Pascal, when the -fsigned-char option is given.  */
1356
1357 void
1358 fixup_signed_type (type)
1359      tree type;
1360 {
1361   register int precision = TYPE_PRECISION (type);
1362
1363   TYPE_MIN_VALUE (type)
1364     = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1365                     ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1366                    (((HOST_WIDE_INT) (-1)
1367                      << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1368                          ? precision - HOST_BITS_PER_WIDE_INT - 1
1369                          : 0))));
1370   TYPE_MAX_VALUE (type)
1371     = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1372                     ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1373                    (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1374                     ? (((HOST_WIDE_INT) 1
1375                         << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1376                     : 0));
1377
1378   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1379   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1380
1381   /* Lay out the type: set its alignment, size, etc.  */
1382
1383   layout_type (type);
1384 }
1385
1386 /* Set the extreme values of TYPE based on its precision in bits,
1387    then lay it out.  This is used both in `make_unsigned_type'
1388    and for enumeral types.  */
1389
1390 void
1391 fixup_unsigned_type (type)
1392      tree type;
1393 {
1394   register int precision = TYPE_PRECISION (type);
1395
1396   TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1397   TYPE_MAX_VALUE (type)
1398     = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1399                    ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1400                    precision - HOST_BITS_PER_WIDE_INT > 0
1401                    ? ((unsigned HOST_WIDE_INT) ~0
1402                       >> (HOST_BITS_PER_WIDE_INT
1403                           - (precision - HOST_BITS_PER_WIDE_INT)))
1404                    : 0);
1405   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1406   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1407
1408   /* Lay out the type: set its alignment, size, etc.  */
1409
1410   layout_type (type);
1411 }
1412 \f
1413 /* Find the best machine mode to use when referencing a bit field of length
1414    BITSIZE bits starting at BITPOS.
1415
1416    The underlying object is known to be aligned to a boundary of ALIGN bits.
1417    If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1418    larger than LARGEST_MODE (usually SImode).
1419
1420    If no mode meets all these conditions, we return VOIDmode.  Otherwise, if
1421    VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1422    mode meeting these conditions.
1423
1424    Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1425    the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1426    all the conditions.  */
1427
1428 enum machine_mode
1429 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1430      int bitsize, bitpos;
1431      int align;
1432      enum machine_mode largest_mode;
1433      int volatilep;
1434 {
1435   enum machine_mode mode;
1436   int unit = 0;
1437
1438   /* Find the narrowest integer mode that contains the bit field.  */
1439   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1440        mode = GET_MODE_WIDER_MODE (mode))
1441     {
1442       unit = GET_MODE_BITSIZE (mode);
1443       if ((bitpos % unit) + bitsize <= unit)
1444         break;
1445     }
1446
1447   if (mode == VOIDmode
1448       /* It is tempting to omit the following line
1449          if STRICT_ALIGNMENT is true.
1450          But that is incorrect, since if the bitfield uses part of 3 bytes
1451          and we use a 4-byte mode, we could get a spurious segv
1452          if the extra 4th byte is past the end of memory.
1453          (Though at least one Unix compiler ignores this problem:
1454          that on the Sequent 386 machine.  */
1455       || MIN (unit, BIGGEST_ALIGNMENT) > align
1456       || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1457     return VOIDmode;
1458
1459   if (SLOW_BYTE_ACCESS && ! volatilep)
1460     {
1461       enum machine_mode wide_mode = VOIDmode, tmode;
1462
1463       for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1464            tmode = GET_MODE_WIDER_MODE (tmode))
1465         {
1466           unit = GET_MODE_BITSIZE (tmode);
1467           if (bitpos / unit == (bitpos + bitsize - 1) / unit
1468               && unit <= BITS_PER_WORD
1469               && unit <= MIN (align, BIGGEST_ALIGNMENT)
1470               && (largest_mode == VOIDmode
1471                   || unit <= GET_MODE_BITSIZE (largest_mode)))
1472             wide_mode = tmode;
1473         }
1474
1475       if (wide_mode != VOIDmode)
1476         return wide_mode;
1477     }
1478
1479   return mode;
1480 }
1481
1482 /* This function is run once to initialize stor-layout.c.  */
1483
1484 void
1485 init_stor_layout_once ()
1486 {
1487   ggc_add_tree_root (&pending_sizes, 1);
1488 }