OSDN Git Service

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