OSDN Git Service

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