OSDN Git Service

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