OSDN Git Service

cf97159b5c5f5ca8386b316eb6eadadb33040914
[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, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "flags.h"
31 #include "function.h"
32 #include "expr.h"
33 #include "toplev.h"
34 #include "ggc.h"
35 #include "target.h"
36 #include "langhooks.h"
37
38 /* Set to one when set_sizetype has been called.  */
39 static int sizetype_set;
40
41 /* List of types created before set_sizetype has been called.  We do not
42    make this a GGC root since we want these nodes to be reclaimed.  */
43 static tree early_type_list;
44
45 /* Data type for the expressions representing sizes of data types.
46    It is the first integer type laid out.  */
47 tree sizetype_tab[(int) TYPE_KIND_LAST];
48
49 /* If nonzero, this is an upper limit on alignment of structure fields.
50    The value is measured in bits.  */
51 unsigned int maximum_field_alignment;
52
53 /* If nonzero, the alignment of a bitstring or (power-)set value, in bits.
54    May be overridden by front-ends.  */
55 unsigned int set_alignment = 0;
56
57 /* Nonzero if all REFERENCE_TYPEs are internal and hence should be
58    allocated in Pmode, not ptr_mode.   Set only by internal_reference_types
59    called only by a front end.  */
60 static int reference_types_internal = 0;
61
62 static void finalize_record_size (record_layout_info);
63 static void finalize_type_size (tree);
64 static void place_union_field (record_layout_info, tree);
65 #if defined (PCC_BITFIELD_TYPE_MATTERS) || defined (BITFIELD_NBYTES_LIMITED)
66 static int excess_unit_span (HOST_WIDE_INT, HOST_WIDE_INT, HOST_WIDE_INT,
67                              HOST_WIDE_INT, tree);
68 #endif
69 extern void debug_rli (record_layout_info);
70 \f
71 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded.  */
72
73 static GTY(()) tree pending_sizes;
74
75 /* Show that REFERENCE_TYPES are internal and should be Pmode.  Called only
76    by front end.  */
77
78 void
79 internal_reference_types (void)
80 {
81   reference_types_internal = 1;
82 }
83
84 /* Get a list of all the objects put on the pending sizes list.  */
85
86 tree
87 get_pending_sizes (void)
88 {
89   tree chain = pending_sizes;
90
91   pending_sizes = 0;
92   return chain;
93 }
94
95 /* Add EXPR to the pending sizes list.  */
96
97 void
98 put_pending_size (tree expr)
99 {
100   /* Strip any simple arithmetic from EXPR to see if it has an underlying
101      SAVE_EXPR.  */
102   expr = skip_simple_arithmetic (expr);
103
104   if (TREE_CODE (expr) == SAVE_EXPR)
105     pending_sizes = tree_cons (NULL_TREE, expr, pending_sizes);
106 }
107
108 /* Put a chain of objects into the pending sizes list, which must be
109    empty.  */
110
111 void
112 put_pending_sizes (tree chain)
113 {
114   if (pending_sizes)
115     abort ();
116
117   pending_sizes = chain;
118 }
119
120 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
121    to serve as the actual size-expression for a type or decl.  */
122
123 tree
124 variable_size (tree size)
125 {
126   tree save;
127
128   /* If the language-processor is to take responsibility for variable-sized
129      items (e.g., languages which have elaboration procedures like Ada),
130      just return SIZE unchanged.  Likewise for self-referential sizes and
131      constant sizes.  */
132   if (TREE_CONSTANT (size)
133       || lang_hooks.decls.global_bindings_p () < 0
134       || CONTAINS_PLACEHOLDER_P (size))
135     return size;
136
137   size = save_expr (size);
138
139   /* If an array with a variable number of elements is declared, and
140      the elements require destruction, we will emit a cleanup for the
141      array.  That cleanup is run both on normal exit from the block
142      and in the exception-handler for the block.  Normally, when code
143      is used in both ordinary code and in an exception handler it is
144      `unsaved', i.e., all SAVE_EXPRs are recalculated.  However, we do
145      not wish to do that here; the array-size is the same in both
146      places.  */
147   save = skip_simple_arithmetic (size);
148
149   if (cfun && cfun->x_dont_save_pending_sizes_p)
150     /* The front-end doesn't want us to keep a list of the expressions
151        that determine sizes for variable size objects.  Trust it.  */
152     return size;
153
154   if (lang_hooks.decls.global_bindings_p ())
155     {
156       if (TREE_CONSTANT (size))
157         error ("type size can't be explicitly evaluated");
158       else
159         error ("variable-size type declared outside of any function");
160
161       return size_one_node;
162     }
163
164   put_pending_size (save);
165
166   return size;
167 }
168 \f
169 #ifndef MAX_FIXED_MODE_SIZE
170 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
171 #endif
172
173 /* Return the machine mode to use for a nonscalar of SIZE bits.  The
174    mode must be in class CLASS, and have exactly that many value bits;
175    it may have padding as well.  If LIMIT is nonzero, modes of wider
176    than MAX_FIXED_MODE_SIZE will not be used.  */
177
178 enum machine_mode
179 mode_for_size (unsigned int size, enum mode_class class, int limit)
180 {
181   enum machine_mode mode;
182
183   if (limit && size > MAX_FIXED_MODE_SIZE)
184     return BLKmode;
185
186   /* Get the first mode which has this size, in the specified class.  */
187   for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
188        mode = GET_MODE_WIDER_MODE (mode))
189     if (GET_MODE_PRECISION (mode) == size)
190       return mode;
191
192   return BLKmode;
193 }
194
195 /* Similar, except passed a tree node.  */
196
197 enum machine_mode
198 mode_for_size_tree (tree size, enum mode_class class, int limit)
199 {
200   if (TREE_CODE (size) != INTEGER_CST
201       || TREE_OVERFLOW (size)
202       /* What we really want to say here is that the size can fit in a
203          host integer, but we know there's no way we'd find a mode for
204          this many bits, so there's no point in doing the precise test.  */
205       || compare_tree_int (size, 1000) > 0)
206     return BLKmode;
207   else
208     return mode_for_size (tree_low_cst (size, 1), class, limit);
209 }
210
211 /* Similar, but never return BLKmode; return the narrowest mode that
212    contains at least the requested number of value bits.  */
213
214 enum machine_mode
215 smallest_mode_for_size (unsigned int size, enum mode_class class)
216 {
217   enum machine_mode mode;
218
219   /* Get the first mode which has at least this size, in the
220      specified class.  */
221   for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
222        mode = GET_MODE_WIDER_MODE (mode))
223     if (GET_MODE_PRECISION (mode) >= size)
224       return mode;
225
226   abort ();
227 }
228
229 /* Find an integer mode of the exact same size, or BLKmode on failure.  */
230
231 enum machine_mode
232 int_mode_for_mode (enum machine_mode mode)
233 {
234   switch (GET_MODE_CLASS (mode))
235     {
236     case MODE_INT:
237     case MODE_PARTIAL_INT:
238       break;
239
240     case MODE_COMPLEX_INT:
241     case MODE_COMPLEX_FLOAT:
242     case MODE_FLOAT:
243     case MODE_VECTOR_INT:
244     case MODE_VECTOR_FLOAT:
245       mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
246       break;
247
248     case MODE_RANDOM:
249       if (mode == BLKmode)
250         break;
251
252       /* ... fall through ...  */
253
254     case MODE_CC:
255     default:
256       abort ();
257     }
258
259   return mode;
260 }
261
262 /* Return the alignment of MODE. This will be bounded by 1 and
263    BIGGEST_ALIGNMENT.  */
264
265 unsigned int
266 get_mode_alignment (enum machine_mode mode)
267 {
268   return MIN (BIGGEST_ALIGNMENT, MAX (1, mode_base_align[mode]*BITS_PER_UNIT));
269 }
270
271 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
272    This can only be applied to objects of a sizetype.  */
273
274 tree
275 round_up (tree value, int divisor)
276 {
277   tree arg = size_int_type (divisor, TREE_TYPE (value));
278
279   return size_binop (MULT_EXPR, size_binop (CEIL_DIV_EXPR, value, arg), arg);
280 }
281
282 /* Likewise, but round down.  */
283
284 tree
285 round_down (tree value, int divisor)
286 {
287   tree arg = size_int_type (divisor, TREE_TYPE (value));
288
289   return size_binop (MULT_EXPR, size_binop (FLOOR_DIV_EXPR, value, arg), arg);
290 }
291 \f
292 /* Subroutine of layout_decl: Force alignment required for the data type.
293    But if the decl itself wants greater alignment, don't override that.  */
294
295 static inline void
296 do_type_align (tree type, tree decl)
297 {
298   if (TYPE_ALIGN (type) > DECL_ALIGN (decl))
299     {
300       DECL_ALIGN (decl) = TYPE_ALIGN (type);
301       if (TREE_CODE (decl) == FIELD_DECL)
302         DECL_USER_ALIGN (decl) = TYPE_USER_ALIGN (type);
303     }
304 }
305
306 /* Set the size, mode and alignment of a ..._DECL node.
307    TYPE_DECL does need this for C++.
308    Note that LABEL_DECL and CONST_DECL nodes do not need this,
309    and FUNCTION_DECL nodes have them set up in a special (and simple) way.
310    Don't call layout_decl for them.
311
312    KNOWN_ALIGN is the amount of alignment we can assume this
313    decl has with no special effort.  It is relevant only for FIELD_DECLs
314    and depends on the previous fields.
315    All that matters about KNOWN_ALIGN is which powers of 2 divide it.
316    If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
317    the record will be aligned to suit.  */
318
319 void
320 layout_decl (tree decl, unsigned int known_align)
321 {
322   tree type = TREE_TYPE (decl);
323   enum tree_code code = TREE_CODE (decl);
324   rtx rtl = NULL_RTX;
325
326   if (code == CONST_DECL)
327     return;
328   else if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
329            && code != TYPE_DECL && code != FIELD_DECL)
330     abort ();
331
332   rtl = DECL_RTL_IF_SET (decl);
333
334   if (type == error_mark_node)
335     type = void_type_node;
336
337   /* Usually the size and mode come from the data type without change,
338      however, the front-end may set the explicit width of the field, so its
339      size may not be the same as the size of its type.  This happens with
340      bitfields, of course (an `int' bitfield may be only 2 bits, say), but it
341      also happens with other fields.  For example, the C++ front-end creates
342      zero-sized fields corresponding to empty base classes, and depends on
343      layout_type setting DECL_FIELD_BITPOS correctly for the field.  Set the
344      size in bytes from the size in bits.  If we have already set the mode,
345      don't set it again since we can be called twice for FIELD_DECLs.  */
346
347   DECL_UNSIGNED (decl) = TYPE_UNSIGNED (type);
348   if (DECL_MODE (decl) == VOIDmode)
349     DECL_MODE (decl) = TYPE_MODE (type);
350
351   if (DECL_SIZE (decl) == 0)
352     {
353       DECL_SIZE (decl) = TYPE_SIZE (type);
354       DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
355     }
356   else if (DECL_SIZE_UNIT (decl) == 0)
357     DECL_SIZE_UNIT (decl)
358       = convert (sizetype, size_binop (CEIL_DIV_EXPR, DECL_SIZE (decl),
359                                        bitsize_unit_node));
360
361   if (code != FIELD_DECL)
362     /* For non-fields, update the alignment from the type.  */
363     do_type_align (type, decl);
364   else
365     /* For fields, it's a bit more complicated...  */
366     {
367       bool old_user_align = DECL_USER_ALIGN (decl);
368
369       if (DECL_BIT_FIELD (decl))
370         {
371           DECL_BIT_FIELD_TYPE (decl) = type;
372
373           /* A zero-length bit-field affects the alignment of the next
374              field.  */
375           if (integer_zerop (DECL_SIZE (decl))
376               && ! DECL_PACKED (decl)
377               && ! targetm.ms_bitfield_layout_p (DECL_FIELD_CONTEXT (decl)))
378             {
379 #ifdef PCC_BITFIELD_TYPE_MATTERS
380               if (PCC_BITFIELD_TYPE_MATTERS)
381                 do_type_align (type, decl);
382               else
383 #endif
384                 {
385 #ifdef EMPTY_FIELD_BOUNDARY
386                   if (EMPTY_FIELD_BOUNDARY > DECL_ALIGN (decl))
387                     {
388                       DECL_ALIGN (decl) = EMPTY_FIELD_BOUNDARY;
389                       DECL_USER_ALIGN (decl) = 0;
390                     }
391 #endif
392                 }
393             }
394
395           /* See if we can use an ordinary integer mode for a bit-field.
396              Conditions are: a fixed size that is correct for another mode
397              and occupying a complete byte or bytes on proper boundary.  */
398           if (TYPE_SIZE (type) != 0
399               && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
400               && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
401             {
402               enum machine_mode xmode
403                 = mode_for_size_tree (DECL_SIZE (decl), MODE_INT, 1);
404
405               if (xmode != BLKmode 
406                   && (known_align == 0
407                       || known_align >= GET_MODE_ALIGNMENT (xmode)))
408                 {
409                   DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
410                                            DECL_ALIGN (decl));
411                   DECL_MODE (decl) = xmode;
412                   DECL_BIT_FIELD (decl) = 0;
413                 }
414             }
415
416           /* Turn off DECL_BIT_FIELD if we won't need it set.  */
417           if (TYPE_MODE (type) == BLKmode && DECL_MODE (decl) == BLKmode
418               && known_align >= TYPE_ALIGN (type)
419               && DECL_ALIGN (decl) >= TYPE_ALIGN (type))
420             DECL_BIT_FIELD (decl) = 0;
421         }
422       else if (DECL_PACKED (decl) && DECL_USER_ALIGN (decl))
423         /* Don't touch DECL_ALIGN.  For other packed fields, go ahead and
424            round up; we'll reduce it again below.  We want packing to
425            supersede USER_ALIGN inherited from the type, but defer to
426            alignment explicitly specified on the field decl.  */;
427       else
428         do_type_align (type, decl);
429
430       /* If the field is of variable size, we can't misalign it since we
431          have no way to make a temporary to align the result.  But this
432          isn't an issue if the decl is not addressable.  Likewise if it
433          is of unknown size.
434
435          Note that do_type_align may set DECL_USER_ALIGN, so we need to
436          check old_user_align instead.  */
437       if (DECL_PACKED (decl)
438           && !old_user_align
439           && (DECL_NONADDRESSABLE_P (decl)
440               || DECL_SIZE_UNIT (decl) == 0
441               || TREE_CODE (DECL_SIZE_UNIT (decl)) == INTEGER_CST))
442         DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
443
444       if (! DECL_USER_ALIGN (decl) && ! DECL_PACKED (decl))
445         {
446           /* Some targets (i.e. i386, VMS) limit struct field alignment
447              to a lower boundary than alignment of variables unless
448              it was overridden by attribute aligned.  */
449 #ifdef BIGGEST_FIELD_ALIGNMENT
450           DECL_ALIGN (decl)
451             = MIN (DECL_ALIGN (decl), (unsigned) BIGGEST_FIELD_ALIGNMENT);
452 #endif
453 #ifdef ADJUST_FIELD_ALIGN
454           DECL_ALIGN (decl) = ADJUST_FIELD_ALIGN (decl, DECL_ALIGN (decl));
455 #endif
456         }
457
458       /* Should this be controlled by DECL_USER_ALIGN, too?  */
459       if (maximum_field_alignment != 0)
460         DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
461     }
462
463   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
464   if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
465     DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
466   if (DECL_SIZE_UNIT (decl) != 0
467       && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST)
468     DECL_SIZE_UNIT (decl) = variable_size (DECL_SIZE_UNIT (decl));
469
470   /* If requested, warn about definitions of large data objects.  */
471   if (warn_larger_than
472       && (code == VAR_DECL || code == PARM_DECL)
473       && ! DECL_EXTERNAL (decl))
474     {
475       tree size = DECL_SIZE_UNIT (decl);
476
477       if (size != 0 && TREE_CODE (size) == INTEGER_CST
478           && compare_tree_int (size, larger_than_size) > 0)
479         {
480           int size_as_int = TREE_INT_CST_LOW (size);
481
482           if (compare_tree_int (size, size_as_int) == 0)
483             warning ("%Jsize of '%D' is %d bytes", decl, decl, size_as_int);
484           else
485             warning ("%Jsize of '%D' is larger than %d bytes",
486                      decl, decl, larger_than_size);
487         }
488     }
489
490   /* If the RTL was already set, update its mode and mem attributes.  */
491   if (rtl)
492     {
493       PUT_MODE (rtl, DECL_MODE (decl));
494       SET_DECL_RTL (decl, 0);
495       set_mem_attributes (rtl, decl, 1);
496       SET_DECL_RTL (decl, rtl);
497     }
498 }
499 \f
500 /* Hook for a front-end function that can modify the record layout as needed
501    immediately before it is finalized.  */
502
503 void (*lang_adjust_rli) (record_layout_info) = 0;
504
505 void
506 set_lang_adjust_rli (void (*f) (record_layout_info))
507 {
508   lang_adjust_rli = f;
509 }
510
511 /* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
512    QUAL_UNION_TYPE.  Return a pointer to a struct record_layout_info which
513    is to be passed to all other layout functions for this record.  It is the
514    responsibility of the caller to call `free' for the storage returned.
515    Note that garbage collection is not permitted until we finish laying
516    out the record.  */
517
518 record_layout_info
519 start_record_layout (tree t)
520 {
521   record_layout_info rli = xmalloc (sizeof (struct record_layout_info_s));
522
523   rli->t = t;
524
525   /* If the type has a minimum specified alignment (via an attribute
526      declaration, for example) use it -- otherwise, start with a
527      one-byte alignment.  */
528   rli->record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (t));
529   rli->unpacked_align = rli->record_align;
530   rli->offset_align = MAX (rli->record_align, BIGGEST_ALIGNMENT);
531
532 #ifdef STRUCTURE_SIZE_BOUNDARY
533   /* Packed structures don't need to have minimum size.  */
534   if (! TYPE_PACKED (t))
535     rli->record_align = MAX (rli->record_align, (unsigned) STRUCTURE_SIZE_BOUNDARY);
536 #endif
537
538   rli->offset = size_zero_node;
539   rli->bitpos = bitsize_zero_node;
540   rli->prev_field = 0;
541   rli->pending_statics = 0;
542   rli->packed_maybe_necessary = 0;
543
544   return rli;
545 }
546
547 /* These four routines perform computations that convert between
548    the offset/bitpos forms and byte and bit offsets.  */
549
550 tree
551 bit_from_pos (tree offset, tree bitpos)
552 {
553   return size_binop (PLUS_EXPR, bitpos,
554                      size_binop (MULT_EXPR, convert (bitsizetype, offset),
555                                  bitsize_unit_node));
556 }
557
558 tree
559 byte_from_pos (tree offset, tree bitpos)
560 {
561   return size_binop (PLUS_EXPR, offset,
562                      convert (sizetype,
563                               size_binop (TRUNC_DIV_EXPR, bitpos,
564                                           bitsize_unit_node)));
565 }
566
567 void
568 pos_from_bit (tree *poffset, tree *pbitpos, unsigned int off_align,
569               tree pos)
570 {
571   *poffset = size_binop (MULT_EXPR,
572                          convert (sizetype,
573                                   size_binop (FLOOR_DIV_EXPR, pos,
574                                               bitsize_int (off_align))),
575                          size_int (off_align / BITS_PER_UNIT));
576   *pbitpos = size_binop (FLOOR_MOD_EXPR, pos, bitsize_int (off_align));
577 }
578
579 /* Given a pointer to bit and byte offsets and an offset alignment,
580    normalize the offsets so they are within the alignment.  */
581
582 void
583 normalize_offset (tree *poffset, tree *pbitpos, unsigned int off_align)
584 {
585   /* If the bit position is now larger than it should be, adjust it
586      downwards.  */
587   if (compare_tree_int (*pbitpos, off_align) >= 0)
588     {
589       tree extra_aligns = size_binop (FLOOR_DIV_EXPR, *pbitpos,
590                                       bitsize_int (off_align));
591
592       *poffset
593         = size_binop (PLUS_EXPR, *poffset,
594                       size_binop (MULT_EXPR, convert (sizetype, extra_aligns),
595                                   size_int (off_align / BITS_PER_UNIT)));
596
597       *pbitpos
598         = size_binop (FLOOR_MOD_EXPR, *pbitpos, bitsize_int (off_align));
599     }
600 }
601
602 /* Print debugging information about the information in RLI.  */
603
604 void
605 debug_rli (record_layout_info rli)
606 {
607   print_node_brief (stderr, "type", rli->t, 0);
608   print_node_brief (stderr, "\noffset", rli->offset, 0);
609   print_node_brief (stderr, " bitpos", rli->bitpos, 0);
610
611   fprintf (stderr, "\naligns: rec = %u, unpack = %u, off = %u\n",
612            rli->record_align, rli->unpacked_align,
613            rli->offset_align);
614   if (rli->packed_maybe_necessary)
615     fprintf (stderr, "packed may be necessary\n");
616
617   if (rli->pending_statics)
618     {
619       fprintf (stderr, "pending statics:\n");
620       debug_tree (rli->pending_statics);
621     }
622 }
623
624 /* Given an RLI with a possibly-incremented BITPOS, adjust OFFSET and
625    BITPOS if necessary to keep BITPOS below OFFSET_ALIGN.  */
626
627 void
628 normalize_rli (record_layout_info rli)
629 {
630   normalize_offset (&rli->offset, &rli->bitpos, rli->offset_align);
631 }
632
633 /* Returns the size in bytes allocated so far.  */
634
635 tree
636 rli_size_unit_so_far (record_layout_info rli)
637 {
638   return byte_from_pos (rli->offset, rli->bitpos);
639 }
640
641 /* Returns the size in bits allocated so far.  */
642
643 tree
644 rli_size_so_far (record_layout_info rli)
645 {
646   return bit_from_pos (rli->offset, rli->bitpos);
647 }
648
649 /* FIELD is about to be added to RLI->T.  The alignment (in bits) of
650    the next available location is given by KNOWN_ALIGN.  Update the
651    variable alignment fields in RLI, and return the alignment to give
652    the FIELD.  */
653
654 unsigned int
655 update_alignment_for_field (record_layout_info rli, tree field,
656                             unsigned int known_align)
657 {
658   /* The alignment required for FIELD.  */
659   unsigned int desired_align;
660   /* The type of this field.  */
661   tree type = TREE_TYPE (field);
662   /* True if the field was explicitly aligned by the user.  */
663   bool user_align;
664   bool is_bitfield;
665
666   /* Lay out the field so we know what alignment it needs.  */
667   layout_decl (field, known_align);
668   desired_align = DECL_ALIGN (field);
669   user_align = DECL_USER_ALIGN (field);
670
671   is_bitfield = (type != error_mark_node
672                  && DECL_BIT_FIELD_TYPE (field)
673                  && ! integer_zerop (TYPE_SIZE (type)));
674
675   /* Record must have at least as much alignment as any field.
676      Otherwise, the alignment of the field within the record is
677      meaningless.  */
678   if (is_bitfield && targetm.ms_bitfield_layout_p (rli->t))
679     {
680       /* Here, the alignment of the underlying type of a bitfield can
681          affect the alignment of a record; even a zero-sized field
682          can do this.  The alignment should be to the alignment of
683          the type, except that for zero-size bitfields this only
684          applies if there was an immediately prior, nonzero-size
685          bitfield.  (That's the way it is, experimentally.) */
686       if (! integer_zerop (DECL_SIZE (field))
687           ? ! DECL_PACKED (field)
688           : (rli->prev_field
689              && DECL_BIT_FIELD_TYPE (rli->prev_field)
690              && ! integer_zerop (DECL_SIZE (rli->prev_field))))
691         {
692           unsigned int type_align = TYPE_ALIGN (type);
693           type_align = MAX (type_align, desired_align);
694           if (maximum_field_alignment != 0)
695             type_align = MIN (type_align, maximum_field_alignment);
696           rli->record_align = MAX (rli->record_align, type_align);
697           rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
698         }
699     }
700 #ifdef PCC_BITFIELD_TYPE_MATTERS
701   else if (is_bitfield && PCC_BITFIELD_TYPE_MATTERS)
702     {
703       /* Named bit-fields cause the entire structure to have the
704          alignment implied by their type.  Some targets also apply the same
705          rules to unnamed bitfields.  */
706       if (DECL_NAME (field) != 0
707           || targetm.align_anon_bitfield ())
708         {
709           unsigned int type_align = TYPE_ALIGN (type);
710
711 #ifdef ADJUST_FIELD_ALIGN
712           if (! TYPE_USER_ALIGN (type))
713             type_align = ADJUST_FIELD_ALIGN (field, type_align);
714 #endif
715
716           if (maximum_field_alignment != 0)
717             type_align = MIN (type_align, maximum_field_alignment);
718           else if (DECL_PACKED (field))
719             type_align = MIN (type_align, BITS_PER_UNIT);
720
721           /* The alignment of the record is increased to the maximum
722              of the current alignment, the alignment indicated on the
723              field (i.e., the alignment specified by an __aligned__
724              attribute), and the alignment indicated by the type of
725              the field.  */
726           rli->record_align = MAX (rli->record_align, desired_align);
727           rli->record_align = MAX (rli->record_align, type_align);
728
729           if (warn_packed)
730             rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
731           user_align |= TYPE_USER_ALIGN (type);
732         }
733     }
734 #endif
735   else
736     {
737       rli->record_align = MAX (rli->record_align, desired_align);
738       rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
739     }
740
741   TYPE_USER_ALIGN (rli->t) |= user_align;
742
743   return desired_align;
744 }
745
746 /* Called from place_field to handle unions.  */
747
748 static void
749 place_union_field (record_layout_info rli, tree field)
750 {
751   update_alignment_for_field (rli, field, /*known_align=*/0);
752
753   DECL_FIELD_OFFSET (field) = size_zero_node;
754   DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
755   SET_DECL_OFFSET_ALIGN (field, BIGGEST_ALIGNMENT);
756
757   /* We assume the union's size will be a multiple of a byte so we don't
758      bother with BITPOS.  */
759   if (TREE_CODE (rli->t) == UNION_TYPE)
760     rli->offset = size_binop (MAX_EXPR, rli->offset, DECL_SIZE_UNIT (field));
761   else if (TREE_CODE (rli->t) == QUAL_UNION_TYPE)
762     rli->offset = fold (build (COND_EXPR, sizetype,
763                                DECL_QUALIFIER (field),
764                                DECL_SIZE_UNIT (field), rli->offset));
765 }
766
767 #if defined (PCC_BITFIELD_TYPE_MATTERS) || defined (BITFIELD_NBYTES_LIMITED)
768 /* A bitfield of SIZE with a required access alignment of ALIGN is allocated
769    at BYTE_OFFSET / BIT_OFFSET.  Return nonzero if the field would span more
770    units of alignment than the underlying TYPE.  */
771 static int
772 excess_unit_span (HOST_WIDE_INT byte_offset, HOST_WIDE_INT bit_offset,
773                   HOST_WIDE_INT size, HOST_WIDE_INT align, tree type)
774 {
775   /* Note that the calculation of OFFSET might overflow; we calculate it so
776      that we still get the right result as long as ALIGN is a power of two.  */
777   unsigned HOST_WIDE_INT offset = byte_offset * BITS_PER_UNIT + bit_offset;
778
779   offset = offset % align;
780   return ((offset + size + align - 1) / align
781           > ((unsigned HOST_WIDE_INT) tree_low_cst (TYPE_SIZE (type), 1)
782              / align));
783 }
784 #endif
785
786 /* RLI contains information about the layout of a RECORD_TYPE.  FIELD
787    is a FIELD_DECL to be added after those fields already present in
788    T.  (FIELD is not actually added to the TYPE_FIELDS list here;
789    callers that desire that behavior must manually perform that step.)  */
790
791 void
792 place_field (record_layout_info rli, tree field)
793 {
794   /* The alignment required for FIELD.  */
795   unsigned int desired_align;
796   /* The alignment FIELD would have if we just dropped it into the
797      record as it presently stands.  */
798   unsigned int known_align;
799   unsigned int actual_align;
800   /* The type of this field.  */
801   tree type = TREE_TYPE (field);
802
803   if (TREE_CODE (field) == ERROR_MARK || TREE_CODE (type) == ERROR_MARK)
804       return;
805
806   /* If FIELD is static, then treat it like a separate variable, not
807      really like a structure field.  If it is a FUNCTION_DECL, it's a
808      method.  In both cases, all we do is lay out the decl, and we do
809      it *after* the record is laid out.  */
810   if (TREE_CODE (field) == VAR_DECL)
811     {
812       rli->pending_statics = tree_cons (NULL_TREE, field,
813                                         rli->pending_statics);
814       return;
815     }
816
817   /* Enumerators and enum types which are local to this class need not
818      be laid out.  Likewise for initialized constant fields.  */
819   else if (TREE_CODE (field) != FIELD_DECL)
820     return;
821
822   /* Unions are laid out very differently than records, so split
823      that code off to another function.  */
824   else if (TREE_CODE (rli->t) != RECORD_TYPE)
825     {
826       place_union_field (rli, field);
827       return;
828     }
829
830   /* Work out the known alignment so far.  Note that A & (-A) is the
831      value of the least-significant bit in A that is one.  */
832   if (! integer_zerop (rli->bitpos))
833     known_align = (tree_low_cst (rli->bitpos, 1)
834                    & - tree_low_cst (rli->bitpos, 1));
835   else if (integer_zerop (rli->offset))
836     known_align = BIGGEST_ALIGNMENT;
837   else if (host_integerp (rli->offset, 1))
838     known_align = (BITS_PER_UNIT
839                    * (tree_low_cst (rli->offset, 1)
840                       & - tree_low_cst (rli->offset, 1)));
841   else
842     known_align = rli->offset_align;
843
844   desired_align = update_alignment_for_field (rli, field, known_align);
845
846   if (warn_packed && DECL_PACKED (field))
847     {
848       if (known_align >= TYPE_ALIGN (type))
849         {
850           if (TYPE_ALIGN (type) > desired_align)
851             {
852               if (STRICT_ALIGNMENT)
853                 warning ("%Jpacked attribute causes inefficient alignment "
854                          "for '%D'", field, field);
855               else
856                 warning ("%Jpacked attribute is unnecessary for '%D'",
857                          field, field);
858             }
859         }
860       else
861         rli->packed_maybe_necessary = 1;
862     }
863
864   /* Does this field automatically have alignment it needs by virtue
865      of the fields that precede it and the record's own alignment?  */
866   if (known_align < desired_align)
867     {
868       /* No, we need to skip space before this field.
869          Bump the cumulative size to multiple of field alignment.  */
870
871       if (warn_padded)
872         warning ("%Jpadding struct to align '%D'", field, field);
873
874       /* If the alignment is still within offset_align, just align
875          the bit position.  */
876       if (desired_align < rli->offset_align)
877         rli->bitpos = round_up (rli->bitpos, desired_align);
878       else
879         {
880           /* First adjust OFFSET by the partial bits, then align.  */
881           rli->offset
882             = size_binop (PLUS_EXPR, rli->offset,
883                           convert (sizetype,
884                                    size_binop (CEIL_DIV_EXPR, rli->bitpos,
885                                                bitsize_unit_node)));
886           rli->bitpos = bitsize_zero_node;
887
888           rli->offset = round_up (rli->offset, desired_align / BITS_PER_UNIT);
889         }
890
891       if (! TREE_CONSTANT (rli->offset))
892         rli->offset_align = desired_align;
893
894     }
895
896   /* Handle compatibility with PCC.  Note that if the record has any
897      variable-sized fields, we need not worry about compatibility.  */
898 #ifdef PCC_BITFIELD_TYPE_MATTERS
899   if (PCC_BITFIELD_TYPE_MATTERS
900       && ! targetm.ms_bitfield_layout_p (rli->t)
901       && TREE_CODE (field) == FIELD_DECL
902       && type != error_mark_node
903       && DECL_BIT_FIELD (field)
904       && ! DECL_PACKED (field)
905       && maximum_field_alignment == 0
906       && ! integer_zerop (DECL_SIZE (field))
907       && host_integerp (DECL_SIZE (field), 1)
908       && host_integerp (rli->offset, 1)
909       && host_integerp (TYPE_SIZE (type), 1))
910     {
911       unsigned int type_align = TYPE_ALIGN (type);
912       tree dsize = DECL_SIZE (field);
913       HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
914       HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
915       HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
916
917 #ifdef ADJUST_FIELD_ALIGN
918       if (! TYPE_USER_ALIGN (type))
919         type_align = ADJUST_FIELD_ALIGN (field, type_align);
920 #endif
921
922       /* A bit field may not span more units of alignment of its type
923          than its type itself.  Advance to next boundary if necessary.  */
924       if (excess_unit_span (offset, bit_offset, field_size, type_align, type))
925         rli->bitpos = round_up (rli->bitpos, type_align);
926
927       TYPE_USER_ALIGN (rli->t) |= TYPE_USER_ALIGN (type);
928     }
929 #endif
930
931 #ifdef BITFIELD_NBYTES_LIMITED
932   if (BITFIELD_NBYTES_LIMITED
933       && ! targetm.ms_bitfield_layout_p (rli->t)
934       && TREE_CODE (field) == FIELD_DECL
935       && type != error_mark_node
936       && DECL_BIT_FIELD_TYPE (field)
937       && ! DECL_PACKED (field)
938       && ! integer_zerop (DECL_SIZE (field))
939       && host_integerp (DECL_SIZE (field), 1)
940       && host_integerp (rli->offset, 1)
941       && host_integerp (TYPE_SIZE (type), 1))
942     {
943       unsigned int type_align = TYPE_ALIGN (type);
944       tree dsize = DECL_SIZE (field);
945       HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
946       HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
947       HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
948
949 #ifdef ADJUST_FIELD_ALIGN
950       if (! TYPE_USER_ALIGN (type))
951         type_align = ADJUST_FIELD_ALIGN (field, type_align);
952 #endif
953
954       if (maximum_field_alignment != 0)
955         type_align = MIN (type_align, maximum_field_alignment);
956       /* ??? This test is opposite the test in the containing if
957          statement, so this code is unreachable currently.  */
958       else if (DECL_PACKED (field))
959         type_align = MIN (type_align, BITS_PER_UNIT);
960
961       /* A bit field may not span the unit of alignment of its type.
962          Advance to next boundary if necessary.  */
963       if (excess_unit_span (offset, bit_offset, field_size, type_align, type))
964         rli->bitpos = round_up (rli->bitpos, type_align);
965
966       TYPE_USER_ALIGN (rli->t) |= TYPE_USER_ALIGN (type);
967     }
968 #endif
969
970   /* See the docs for TARGET_MS_BITFIELD_LAYOUT_P for details.
971      A subtlety:
972         When a bit field is inserted into a packed record, the whole
973         size of the underlying type is used by one or more same-size
974         adjacent bitfields.  (That is, if its long:3, 32 bits is
975         used in the record, and any additional adjacent long bitfields are
976         packed into the same chunk of 32 bits. However, if the size
977         changes, a new field of that size is allocated.)  In an unpacked
978         record, this is the same as using alignment, but not equivalent
979         when packing.
980
981      Note: for compatibility, we use the type size, not the type alignment
982      to determine alignment, since that matches the documentation */
983
984   if (targetm.ms_bitfield_layout_p (rli->t)
985        && ((DECL_BIT_FIELD_TYPE (field) && ! DECL_PACKED (field))
986           || (rli->prev_field && ! DECL_PACKED (rli->prev_field))))
987     {
988       /* At this point, either the prior or current are bitfields,
989          (possibly both), and we're dealing with MS packing.  */
990       tree prev_saved = rli->prev_field;
991
992       /* Is the prior field a bitfield?  If so, handle "runs" of same
993          type size fields.  */
994       if (rli->prev_field /* necessarily a bitfield if it exists.  */)
995         {
996           /* If both are bitfields, nonzero, and the same size, this is
997              the middle of a run.  Zero declared size fields are special
998              and handled as "end of run". (Note: it's nonzero declared
999              size, but equal type sizes!) (Since we know that both
1000              the current and previous fields are bitfields by the
1001              time we check it, DECL_SIZE must be present for both.) */
1002           if (DECL_BIT_FIELD_TYPE (field)
1003               && !integer_zerop (DECL_SIZE (field))
1004               && !integer_zerop (DECL_SIZE (rli->prev_field))
1005               && host_integerp (DECL_SIZE (rli->prev_field), 0)
1006               && host_integerp (TYPE_SIZE (type), 0)
1007               && simple_cst_equal (TYPE_SIZE (type),
1008                                    TYPE_SIZE (TREE_TYPE (rli->prev_field))))
1009             {
1010               /* We're in the middle of a run of equal type size fields; make
1011                  sure we realign if we run out of bits.  (Not decl size,
1012                  type size!) */
1013               HOST_WIDE_INT bitsize = tree_low_cst (DECL_SIZE (field), 0);
1014
1015               if (rli->remaining_in_alignment < bitsize)
1016                 {
1017                   /* out of bits; bump up to next 'word'.  */
1018                   rli->offset = DECL_FIELD_OFFSET (rli->prev_field);
1019                   rli->bitpos
1020                     = size_binop (PLUS_EXPR, TYPE_SIZE (type),
1021                                   DECL_FIELD_BIT_OFFSET (rli->prev_field));
1022                   rli->prev_field = field;
1023                   rli->remaining_in_alignment
1024                     = tree_low_cst (TYPE_SIZE (type), 0);
1025                 }
1026
1027               rli->remaining_in_alignment -= bitsize;
1028             }
1029           else
1030             {
1031               /* End of a run: if leaving a run of bitfields of the same type
1032                  size, we have to "use up" the rest of the bits of the type
1033                  size.
1034
1035                  Compute the new position as the sum of the size for the prior
1036                  type and where we first started working on that type.
1037                  Note: since the beginning of the field was aligned then
1038                  of course the end will be too.  No round needed.  */
1039
1040               if (!integer_zerop (DECL_SIZE (rli->prev_field)))
1041                 {
1042                   tree type_size = TYPE_SIZE (TREE_TYPE (rli->prev_field));
1043
1044                   rli->bitpos
1045                     = size_binop (PLUS_EXPR, type_size,
1046                                   DECL_FIELD_BIT_OFFSET (rli->prev_field));
1047                 }
1048               else
1049                 /* We "use up" size zero fields; the code below should behave
1050                    as if the prior field was not a bitfield.  */
1051                 prev_saved = NULL;
1052
1053               /* Cause a new bitfield to be captured, either this time (if
1054                  currently a bitfield) or next time we see one.  */
1055               if (!DECL_BIT_FIELD_TYPE(field)
1056                  || integer_zerop (DECL_SIZE (field)))
1057                 rli->prev_field = NULL;
1058             }
1059
1060           normalize_rli (rli);
1061         }
1062
1063       /* If we're starting a new run of same size type bitfields
1064          (or a run of non-bitfields), set up the "first of the run"
1065          fields.
1066
1067          That is, if the current field is not a bitfield, or if there
1068          was a prior bitfield the type sizes differ, or if there wasn't
1069          a prior bitfield the size of the current field is nonzero.
1070
1071          Note: we must be sure to test ONLY the type size if there was
1072          a prior bitfield and ONLY for the current field being zero if
1073          there wasn't.  */
1074
1075       if (!DECL_BIT_FIELD_TYPE (field)
1076           || ( prev_saved != NULL
1077                ? !simple_cst_equal (TYPE_SIZE (type),
1078                                     TYPE_SIZE (TREE_TYPE (prev_saved)))
1079               : !integer_zerop (DECL_SIZE (field)) ))
1080         {
1081           /* Never smaller than a byte for compatibility.  */
1082           unsigned int type_align = BITS_PER_UNIT;
1083
1084           /* (When not a bitfield), we could be seeing a flex array (with
1085              no DECL_SIZE).  Since we won't be using remaining_in_alignment
1086              until we see a bitfield (and come by here again) we just skip
1087              calculating it.  */
1088           if (DECL_SIZE (field) != NULL
1089               && host_integerp (TYPE_SIZE (TREE_TYPE (field)), 0)
1090               && host_integerp (DECL_SIZE (field), 0))
1091             rli->remaining_in_alignment
1092               = tree_low_cst (TYPE_SIZE (TREE_TYPE(field)), 0)
1093                 - tree_low_cst (DECL_SIZE (field), 0);
1094
1095           /* Now align (conventionally) for the new type.  */
1096           if (!DECL_PACKED(field))
1097             type_align = MAX(TYPE_ALIGN (type), type_align);
1098
1099           if (prev_saved
1100               && DECL_BIT_FIELD_TYPE (prev_saved)
1101               /* If the previous bit-field is zero-sized, we've already
1102                  accounted for its alignment needs (or ignored it, if
1103                  appropriate) while placing it.  */
1104               && ! integer_zerop (DECL_SIZE (prev_saved)))
1105             type_align = MAX (type_align,
1106                               TYPE_ALIGN (TREE_TYPE (prev_saved)));
1107
1108           if (maximum_field_alignment != 0)
1109             type_align = MIN (type_align, maximum_field_alignment);
1110
1111           rli->bitpos = round_up (rli->bitpos, type_align);
1112
1113           /* If we really aligned, don't allow subsequent bitfields
1114              to undo that.  */
1115           rli->prev_field = NULL;
1116         }
1117     }
1118
1119   /* Offset so far becomes the position of this field after normalizing.  */
1120   normalize_rli (rli);
1121   DECL_FIELD_OFFSET (field) = rli->offset;
1122   DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
1123   SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
1124
1125   /* If this field ended up more aligned than we thought it would be (we
1126      approximate this by seeing if its position changed), lay out the field
1127      again; perhaps we can use an integral mode for it now.  */
1128   if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field)))
1129     actual_align = (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1130                     & - tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1));
1131   else if (integer_zerop (DECL_FIELD_OFFSET (field)))
1132     actual_align = BIGGEST_ALIGNMENT;
1133   else if (host_integerp (DECL_FIELD_OFFSET (field), 1))
1134     actual_align = (BITS_PER_UNIT
1135                    * (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
1136                       & - tree_low_cst (DECL_FIELD_OFFSET (field), 1)));
1137   else
1138     actual_align = DECL_OFFSET_ALIGN (field);
1139
1140   if (known_align != actual_align)
1141     layout_decl (field, actual_align);
1142
1143   /* Only the MS bitfields use this.  */
1144   if (rli->prev_field == NULL && DECL_BIT_FIELD_TYPE(field))
1145       rli->prev_field = field;
1146
1147   /* Now add size of this field to the size of the record.  If the size is
1148      not constant, treat the field as being a multiple of bytes and just
1149      adjust the offset, resetting the bit position.  Otherwise, apportion the
1150      size amongst the bit position and offset.  First handle the case of an
1151      unspecified size, which can happen when we have an invalid nested struct
1152      definition, such as struct j { struct j { int i; } }.  The error message
1153      is printed in finish_struct.  */
1154   if (DECL_SIZE (field) == 0)
1155     /* Do nothing.  */;
1156   else if (TREE_CODE (DECL_SIZE_UNIT (field)) != INTEGER_CST
1157            || TREE_CONSTANT_OVERFLOW (DECL_SIZE_UNIT (field)))
1158     {
1159       rli->offset
1160         = size_binop (PLUS_EXPR, rli->offset,
1161                       convert (sizetype,
1162                                size_binop (CEIL_DIV_EXPR, rli->bitpos,
1163                                            bitsize_unit_node)));
1164       rli->offset
1165         = size_binop (PLUS_EXPR, rli->offset, DECL_SIZE_UNIT (field));
1166       rli->bitpos = bitsize_zero_node;
1167       rli->offset_align = MIN (rli->offset_align, desired_align);
1168     }
1169   else
1170     {
1171       rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
1172       normalize_rli (rli);
1173     }
1174 }
1175
1176 /* Assuming that all the fields have been laid out, this function uses
1177    RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
1178    indicated by RLI.  */
1179
1180 static void
1181 finalize_record_size (record_layout_info rli)
1182 {
1183   tree unpadded_size, unpadded_size_unit;
1184
1185   /* Now we want just byte and bit offsets, so set the offset alignment
1186      to be a byte and then normalize.  */
1187   rli->offset_align = BITS_PER_UNIT;
1188   normalize_rli (rli);
1189
1190   /* Determine the desired alignment.  */
1191 #ifdef ROUND_TYPE_ALIGN
1192   TYPE_ALIGN (rli->t) = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t),
1193                                           rli->record_align);
1194 #else
1195   TYPE_ALIGN (rli->t) = MAX (TYPE_ALIGN (rli->t), rli->record_align);
1196 #endif
1197
1198   /* Compute the size so far.  Be sure to allow for extra bits in the
1199      size in bytes.  We have guaranteed above that it will be no more
1200      than a single byte.  */
1201   unpadded_size = rli_size_so_far (rli);
1202   unpadded_size_unit = rli_size_unit_so_far (rli);
1203   if (! integer_zerop (rli->bitpos))
1204     unpadded_size_unit
1205       = size_binop (PLUS_EXPR, unpadded_size_unit, size_one_node);
1206
1207   /* Round the size up to be a multiple of the required alignment.  */
1208   TYPE_SIZE (rli->t) = round_up (unpadded_size, TYPE_ALIGN (rli->t));
1209   TYPE_SIZE_UNIT (rli->t) = round_up (unpadded_size_unit,
1210                                       TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
1211
1212   if (warn_padded && TREE_CONSTANT (unpadded_size)
1213       && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0)
1214     warning ("padding struct size to alignment boundary");
1215
1216   if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
1217       && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
1218       && TREE_CONSTANT (unpadded_size))
1219     {
1220       tree unpacked_size;
1221
1222 #ifdef ROUND_TYPE_ALIGN
1223       rli->unpacked_align
1224         = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t), rli->unpacked_align);
1225 #else
1226       rli->unpacked_align = MAX (TYPE_ALIGN (rli->t), rli->unpacked_align);
1227 #endif
1228
1229       unpacked_size = round_up (TYPE_SIZE (rli->t), rli->unpacked_align);
1230       if (simple_cst_equal (unpacked_size, TYPE_SIZE (rli->t)))
1231         {
1232           TYPE_PACKED (rli->t) = 0;
1233
1234           if (TYPE_NAME (rli->t))
1235             {
1236               const char *name;
1237
1238               if (TREE_CODE (TYPE_NAME (rli->t)) == IDENTIFIER_NODE)
1239                 name = IDENTIFIER_POINTER (TYPE_NAME (rli->t));
1240               else
1241                 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (rli->t)));
1242
1243               if (STRICT_ALIGNMENT)
1244                 warning ("packed attribute causes inefficient alignment for `%s'", name);
1245               else
1246                 warning ("packed attribute is unnecessary for `%s'", name);
1247             }
1248           else
1249             {
1250               if (STRICT_ALIGNMENT)
1251                 warning ("packed attribute causes inefficient alignment");
1252               else
1253                 warning ("packed attribute is unnecessary");
1254             }
1255         }
1256     }
1257 }
1258
1259 /* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE).  */
1260
1261 void
1262 compute_record_mode (tree type)
1263 {
1264   tree field;
1265   enum machine_mode mode = VOIDmode;
1266
1267   /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
1268      However, if possible, we use a mode that fits in a register
1269      instead, in order to allow for better optimization down the
1270      line.  */
1271   TYPE_MODE (type) = BLKmode;
1272
1273   if (! host_integerp (TYPE_SIZE (type), 1))
1274     return;
1275
1276   /* A record which has any BLKmode members must itself be
1277      BLKmode; it can't go in a register.  Unless the member is
1278      BLKmode only because it isn't aligned.  */
1279   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1280     {
1281       if (TREE_CODE (field) != FIELD_DECL)
1282         continue;
1283
1284       if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK
1285           || (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1286               && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field))
1287               && !(TYPE_SIZE (TREE_TYPE (field)) != 0
1288                    && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))))
1289           || ! host_integerp (bit_position (field), 1)
1290           || DECL_SIZE (field) == 0
1291           || ! host_integerp (DECL_SIZE (field), 1))
1292         return;
1293
1294       /* If this field is the whole struct, remember its mode so
1295          that, say, we can put a double in a class into a DF
1296          register instead of forcing it to live in the stack.  */
1297       if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
1298         mode = DECL_MODE (field);
1299
1300 #ifdef MEMBER_TYPE_FORCES_BLK
1301       /* With some targets, eg. c4x, it is sub-optimal
1302          to access an aligned BLKmode structure as a scalar.  */
1303
1304       if (MEMBER_TYPE_FORCES_BLK (field, mode))
1305         return;
1306 #endif /* MEMBER_TYPE_FORCES_BLK  */
1307     }
1308
1309   /* If we only have one real field; use its mode.  This only applies to
1310      RECORD_TYPE.  This does not apply to unions.  */
1311   if (TREE_CODE (type) == RECORD_TYPE && mode != VOIDmode)
1312     TYPE_MODE (type) = mode;
1313   else
1314     TYPE_MODE (type) = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1315
1316   /* If structure's known alignment is less than what the scalar
1317      mode would need, and it matters, then stick with BLKmode.  */
1318   if (TYPE_MODE (type) != BLKmode
1319       && STRICT_ALIGNMENT
1320       && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1321             || TYPE_ALIGN (type) >= GET_MODE_ALIGNMENT (TYPE_MODE (type))))
1322     {
1323       /* If this is the only reason this type is BLKmode, then
1324          don't force containing types to be BLKmode.  */
1325       TYPE_NO_FORCE_BLK (type) = 1;
1326       TYPE_MODE (type) = BLKmode;
1327     }
1328 }
1329
1330 /* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
1331    out.  */
1332
1333 static void
1334 finalize_type_size (tree type)
1335 {
1336   /* Normally, use the alignment corresponding to the mode chosen.
1337      However, where strict alignment is not required, avoid
1338      over-aligning structures, since most compilers do not do this
1339      alignment.  */
1340
1341   if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1342       && (STRICT_ALIGNMENT
1343           || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1344               && TREE_CODE (type) != QUAL_UNION_TYPE
1345               && TREE_CODE (type) != ARRAY_TYPE)))
1346     {
1347       TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1348       TYPE_USER_ALIGN (type) = 0;
1349     }
1350
1351   /* Do machine-dependent extra alignment.  */
1352 #ifdef ROUND_TYPE_ALIGN
1353   TYPE_ALIGN (type)
1354     = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1355 #endif
1356
1357   /* If we failed to find a simple way to calculate the unit size
1358      of the type, find it by division.  */
1359   if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1360     /* TYPE_SIZE (type) is computed in bitsizetype.  After the division, the
1361        result will fit in sizetype.  We will get more efficient code using
1362        sizetype, so we force a conversion.  */
1363     TYPE_SIZE_UNIT (type)
1364       = convert (sizetype,
1365                  size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1366                              bitsize_unit_node));
1367
1368   if (TYPE_SIZE (type) != 0)
1369     {
1370       TYPE_SIZE (type) = round_up (TYPE_SIZE (type), TYPE_ALIGN (type));
1371       TYPE_SIZE_UNIT (type)
1372         = round_up (TYPE_SIZE_UNIT (type), TYPE_ALIGN (type) / BITS_PER_UNIT);
1373     }
1374
1375   /* Evaluate nonconstant sizes only once, either now or as soon as safe.  */
1376   if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1377     TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1378   if (TYPE_SIZE_UNIT (type) != 0
1379       && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1380     TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1381
1382   /* Also layout any other variants of the type.  */
1383   if (TYPE_NEXT_VARIANT (type)
1384       || type != TYPE_MAIN_VARIANT (type))
1385     {
1386       tree variant;
1387       /* Record layout info of this variant.  */
1388       tree size = TYPE_SIZE (type);
1389       tree size_unit = TYPE_SIZE_UNIT (type);
1390       unsigned int align = TYPE_ALIGN (type);
1391       unsigned int user_align = TYPE_USER_ALIGN (type);
1392       enum machine_mode mode = TYPE_MODE (type);
1393
1394       /* Copy it into all variants.  */
1395       for (variant = TYPE_MAIN_VARIANT (type);
1396            variant != 0;
1397            variant = TYPE_NEXT_VARIANT (variant))
1398         {
1399           TYPE_SIZE (variant) = size;
1400           TYPE_SIZE_UNIT (variant) = size_unit;
1401           TYPE_ALIGN (variant) = align;
1402           TYPE_USER_ALIGN (variant) = user_align;
1403           TYPE_MODE (variant) = mode;
1404         }
1405     }
1406 }
1407
1408 /* Do all of the work required to layout the type indicated by RLI,
1409    once the fields have been laid out.  This function will call `free'
1410    for RLI, unless FREE_P is false.  Passing a value other than false
1411    for FREE_P is bad practice; this option only exists to support the
1412    G++ 3.2 ABI.  */
1413
1414 void
1415 finish_record_layout (record_layout_info rli, int free_p)
1416 {
1417   /* Compute the final size.  */
1418   finalize_record_size (rli);
1419
1420   /* Compute the TYPE_MODE for the record.  */
1421   compute_record_mode (rli->t);
1422
1423   /* Perform any last tweaks to the TYPE_SIZE, etc.  */
1424   finalize_type_size (rli->t);
1425
1426   /* Lay out any static members.  This is done now because their type
1427      may use the record's type.  */
1428   while (rli->pending_statics)
1429     {
1430       layout_decl (TREE_VALUE (rli->pending_statics), 0);
1431       rli->pending_statics = TREE_CHAIN (rli->pending_statics);
1432     }
1433
1434   /* Clean up.  */
1435   if (free_p)
1436     free (rli);
1437 }
1438 \f
1439
1440 /* Finish processing a builtin RECORD_TYPE type TYPE.  It's name is
1441    NAME, its fields are chained in reverse on FIELDS.
1442
1443    If ALIGN_TYPE is non-null, it is given the same alignment as
1444    ALIGN_TYPE.  */
1445
1446 void
1447 finish_builtin_struct (tree type, const char *name, tree fields,
1448                        tree align_type)
1449 {
1450   tree tail, next;
1451
1452   for (tail = NULL_TREE; fields; tail = fields, fields = next)
1453     {
1454       DECL_FIELD_CONTEXT (fields) = type;
1455       next = TREE_CHAIN (fields);
1456       TREE_CHAIN (fields) = tail;
1457     }
1458   TYPE_FIELDS (type) = tail;
1459
1460   if (align_type)
1461     {
1462       TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
1463       TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (align_type);
1464     }
1465
1466   layout_type (type);
1467 #if 0 /* not yet, should get fixed properly later */
1468   TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
1469 #else
1470   TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
1471 #endif
1472   TYPE_STUB_DECL (type) = TYPE_NAME (type);
1473   layout_decl (TYPE_NAME (type), 0);
1474 }
1475
1476 /* Calculate the mode, size, and alignment for TYPE.
1477    For an array type, calculate the element separation as well.
1478    Record TYPE on the chain of permanent or temporary types
1479    so that dbxout will find out about it.
1480
1481    TYPE_SIZE of a type is nonzero if the type has been laid out already.
1482    layout_type does nothing on such a type.
1483
1484    If the type is incomplete, its TYPE_SIZE remains zero.  */
1485
1486 void
1487 layout_type (tree type)
1488 {
1489   if (type == 0)
1490     abort ();
1491
1492   if (type == error_mark_node)
1493     return;
1494
1495   /* Do nothing if type has been laid out before.  */
1496   if (TYPE_SIZE (type))
1497     return;
1498
1499   switch (TREE_CODE (type))
1500     {
1501     case LANG_TYPE:
1502       /* This kind of type is the responsibility
1503          of the language-specific code.  */
1504       abort ();
1505
1506     case BOOLEAN_TYPE:  /* Used for Java, Pascal, and Chill.  */
1507       if (TYPE_PRECISION (type) == 0)
1508         TYPE_PRECISION (type) = 1; /* default to one byte/boolean.  */
1509
1510       /* ... fall through ...  */
1511
1512     case INTEGER_TYPE:
1513     case ENUMERAL_TYPE:
1514     case CHAR_TYPE:
1515       if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
1516           && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
1517         TYPE_UNSIGNED (type) = 1;
1518
1519       TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
1520                                                  MODE_INT);
1521       TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1522       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1523       break;
1524
1525     case REAL_TYPE:
1526       TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
1527       TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1528       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1529       break;
1530
1531     case COMPLEX_TYPE:
1532       TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TREE_TYPE (type));
1533       TYPE_MODE (type)
1534         = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
1535                          (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE
1536                           ? MODE_COMPLEX_FLOAT : MODE_COMPLEX_INT),
1537                          0);
1538       TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1539       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1540       break;
1541
1542     case VECTOR_TYPE:
1543       TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TREE_TYPE (type));
1544       TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1545       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1546       break;
1547
1548     case VOID_TYPE:
1549       /* This is an incomplete type and so doesn't have a size.  */
1550       TYPE_ALIGN (type) = 1;
1551       TYPE_USER_ALIGN (type) = 0;
1552       TYPE_MODE (type) = VOIDmode;
1553       break;
1554
1555     case OFFSET_TYPE:
1556       TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
1557       TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
1558       /* A pointer might be MODE_PARTIAL_INT,
1559          but ptrdiff_t must be integral.  */
1560       TYPE_MODE (type) = mode_for_size (POINTER_SIZE, MODE_INT, 0);
1561       break;
1562
1563     case FUNCTION_TYPE:
1564     case METHOD_TYPE:
1565       /* It's hard to see what the mode and size of a function ought to
1566          be, but we do know the alignment is FUNCTION_BOUNDARY, so
1567          make it consistent with that.  */
1568       TYPE_MODE (type) = mode_for_size (FUNCTION_BOUNDARY, MODE_INT, 0);
1569       TYPE_SIZE (type) = bitsize_int (FUNCTION_BOUNDARY);
1570       TYPE_SIZE_UNIT (type) = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1571       break;
1572
1573     case POINTER_TYPE:
1574     case REFERENCE_TYPE:
1575       {
1576
1577         enum machine_mode mode = ((TREE_CODE (type) == REFERENCE_TYPE
1578                                    && reference_types_internal)
1579                                   ? Pmode : TYPE_MODE (type));
1580
1581         int nbits = GET_MODE_BITSIZE (mode);
1582
1583         TYPE_SIZE (type) = bitsize_int (nbits);
1584         TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (mode));
1585         TYPE_UNSIGNED (type) = 1;
1586         TYPE_PRECISION (type) = nbits;
1587       }
1588       break;
1589
1590     case ARRAY_TYPE:
1591       {
1592         tree index = TYPE_DOMAIN (type);
1593         tree element = TREE_TYPE (type);
1594
1595         build_pointer_type (element);
1596
1597         /* We need to know both bounds in order to compute the size.  */
1598         if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
1599             && TYPE_SIZE (element))
1600           {
1601             tree ub = TYPE_MAX_VALUE (index);
1602             tree lb = TYPE_MIN_VALUE (index);
1603             tree length;
1604             tree element_size;
1605
1606             /* The initial subtraction should happen in the original type so
1607                that (possible) negative values are handled appropriately.  */
1608             length = size_binop (PLUS_EXPR, size_one_node,
1609                                  convert (sizetype,
1610                                           fold (build (MINUS_EXPR,
1611                                                        TREE_TYPE (lb),
1612                                                        ub, lb))));
1613
1614             /* Special handling for arrays of bits (for Chill).  */
1615             element_size = TYPE_SIZE (element);
1616             if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element)
1617                 && (integer_zerop (TYPE_MAX_VALUE (element))
1618                     || integer_onep (TYPE_MAX_VALUE (element)))
1619                 && host_integerp (TYPE_MIN_VALUE (element), 1))
1620               {
1621                 HOST_WIDE_INT maxvalue
1622                   = tree_low_cst (TYPE_MAX_VALUE (element), 1);
1623                 HOST_WIDE_INT minvalue
1624                   = tree_low_cst (TYPE_MIN_VALUE (element), 1);
1625
1626                 if (maxvalue - minvalue == 1
1627                     && (maxvalue == 1 || maxvalue == 0))
1628                   element_size = integer_one_node;
1629               }
1630
1631             /* If neither bound is a constant and sizetype is signed, make
1632                sure the size is never negative.  We should really do this
1633                if *either* bound is non-constant, but this is the best
1634                compromise between C and Ada.  */
1635             if (!TYPE_UNSIGNED (sizetype)
1636                 && TREE_CODE (TYPE_MIN_VALUE (index)) != INTEGER_CST
1637                 && TREE_CODE (TYPE_MAX_VALUE (index)) != INTEGER_CST)
1638               length = size_binop (MAX_EXPR, length, size_zero_node);
1639
1640             TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size,
1641                                            convert (bitsizetype, length));
1642
1643             /* If we know the size of the element, calculate the total
1644                size directly, rather than do some division thing below.
1645                This optimization helps Fortran assumed-size arrays
1646                (where the size of the array is determined at runtime)
1647                substantially.
1648                Note that we can't do this in the case where the size of
1649                the elements is one bit since TYPE_SIZE_UNIT cannot be
1650                set correctly in that case.  */
1651             if (TYPE_SIZE_UNIT (element) != 0 && ! integer_onep (element_size))
1652               TYPE_SIZE_UNIT (type)
1653                 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
1654           }
1655
1656         /* Now round the alignment and size,
1657            using machine-dependent criteria if any.  */
1658
1659 #ifdef ROUND_TYPE_ALIGN
1660         TYPE_ALIGN (type)
1661           = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
1662 #else
1663         TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
1664 #endif
1665         TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (element);
1666         TYPE_MODE (type) = BLKmode;
1667         if (TYPE_SIZE (type) != 0
1668 #ifdef MEMBER_TYPE_FORCES_BLK
1669             && ! MEMBER_TYPE_FORCES_BLK (type, VOIDmode)
1670 #endif
1671             /* BLKmode elements force BLKmode aggregate;
1672                else extract/store fields may lose.  */
1673             && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
1674                 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
1675           {
1676             /* One-element arrays get the component type's mode.  */
1677             if (simple_cst_equal (TYPE_SIZE (type),
1678                                   TYPE_SIZE (TREE_TYPE (type))))
1679               TYPE_MODE (type) = TYPE_MODE (TREE_TYPE (type));
1680             else
1681               TYPE_MODE (type)
1682                 = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1683
1684             if (TYPE_MODE (type) != BLKmode
1685                 && STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
1686                 && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (TYPE_MODE (type))
1687                 && TYPE_MODE (type) != BLKmode)
1688               {
1689                 TYPE_NO_FORCE_BLK (type) = 1;
1690                 TYPE_MODE (type) = BLKmode;
1691               }
1692           }
1693         break;
1694       }
1695
1696     case RECORD_TYPE:
1697     case UNION_TYPE:
1698     case QUAL_UNION_TYPE:
1699       {
1700         tree field;
1701         record_layout_info rli;
1702
1703         /* Initialize the layout information.  */
1704         rli = start_record_layout (type);
1705
1706         /* If this is a QUAL_UNION_TYPE, we want to process the fields
1707            in the reverse order in building the COND_EXPR that denotes
1708            its size.  We reverse them again later.  */
1709         if (TREE_CODE (type) == QUAL_UNION_TYPE)
1710           TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1711
1712         /* Place all the fields.  */
1713         for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1714           place_field (rli, field);
1715
1716         if (TREE_CODE (type) == QUAL_UNION_TYPE)
1717           TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1718
1719         if (lang_adjust_rli)
1720           (*lang_adjust_rli) (rli);
1721
1722         /* Finish laying out the record.  */
1723         finish_record_layout (rli, /*free_p=*/true);
1724       }
1725       break;
1726
1727     case SET_TYPE:  /* Used by Chill and Pascal.  */
1728       if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1729           || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1730         abort ();
1731       else
1732         {
1733 #ifndef SET_WORD_SIZE
1734 #define SET_WORD_SIZE BITS_PER_WORD
1735 #endif
1736           unsigned int alignment
1737             = set_alignment ? set_alignment : SET_WORD_SIZE;
1738           HOST_WIDE_INT size_in_bits
1739             = (tree_low_cst (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), 0)
1740                - tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0) + 1);
1741           HOST_WIDE_INT rounded_size
1742             = ((size_in_bits + alignment - 1) / alignment) * alignment;
1743
1744           if (rounded_size > (int) alignment)
1745             TYPE_MODE (type) = BLKmode;
1746           else
1747             TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1748
1749           TYPE_SIZE (type) = bitsize_int (rounded_size);
1750           TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1751           TYPE_ALIGN (type) = alignment;
1752           TYPE_USER_ALIGN (type) = 0;
1753           TYPE_PRECISION (type) = size_in_bits;
1754         }
1755       break;
1756
1757     case FILE_TYPE:
1758       /* The size may vary in different languages, so the language front end
1759          should fill in the size.  */
1760       TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1761       TYPE_USER_ALIGN (type) = 0;
1762       TYPE_MODE  (type) = BLKmode;
1763       break;
1764
1765     default:
1766       abort ();
1767     }
1768
1769   /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE.  For
1770      records and unions, finish_record_layout already called this
1771      function.  */
1772   if (TREE_CODE (type) != RECORD_TYPE
1773       && TREE_CODE (type) != UNION_TYPE
1774       && TREE_CODE (type) != QUAL_UNION_TYPE)
1775     finalize_type_size (type);
1776
1777   /* If this type is created before sizetype has been permanently set,
1778      record it so set_sizetype can fix it up.  */
1779   if (! sizetype_set)
1780     early_type_list = tree_cons (NULL_TREE, type, early_type_list);
1781
1782   /* If an alias set has been set for this aggregate when it was incomplete,
1783      force it into alias set 0.
1784      This is too conservative, but we cannot call record_component_aliases
1785      here because some frontends still change the aggregates after
1786      layout_type.  */
1787   if (AGGREGATE_TYPE_P (type) && TYPE_ALIAS_SET_KNOWN_P (type))
1788     TYPE_ALIAS_SET (type) = 0;
1789 }
1790 \f
1791 /* Create and return a type for signed integers of PRECISION bits.  */
1792
1793 tree
1794 make_signed_type (int precision)
1795 {
1796   tree type = make_node (INTEGER_TYPE);
1797
1798   TYPE_PRECISION (type) = precision;
1799
1800   fixup_signed_type (type);
1801   return type;
1802 }
1803
1804 /* Create and return a type for unsigned integers of PRECISION bits.  */
1805
1806 tree
1807 make_unsigned_type (int precision)
1808 {
1809   tree type = make_node (INTEGER_TYPE);
1810
1811   TYPE_PRECISION (type) = precision;
1812
1813   fixup_unsigned_type (type);
1814   return type;
1815 }
1816 \f
1817 /* Initialize sizetype and bitsizetype to a reasonable and temporary
1818    value to enable integer types to be created.  */
1819
1820 void
1821 initialize_sizetypes (void)
1822 {
1823   tree t = make_node (INTEGER_TYPE);
1824
1825   /* Set this so we do something reasonable for the build_int_2 calls
1826      below.  */
1827   integer_type_node = t;
1828
1829   TYPE_MODE (t) = SImode;
1830   TYPE_ALIGN (t) = GET_MODE_ALIGNMENT (SImode);
1831   TYPE_USER_ALIGN (t) = 0;
1832   TYPE_SIZE (t) = build_int_2 (GET_MODE_BITSIZE (SImode), 0);
1833   TYPE_SIZE_UNIT (t) = build_int_2 (GET_MODE_SIZE (SImode), 0);
1834   TYPE_UNSIGNED (t) = 1;
1835   TYPE_PRECISION (t) = GET_MODE_BITSIZE (SImode);
1836   TYPE_MIN_VALUE (t) = build_int_2 (0, 0);
1837   TYPE_IS_SIZETYPE (t) = 1;
1838
1839   /* 1000 avoids problems with possible overflow and is certainly
1840      larger than any size value we'd want to be storing.  */
1841   TYPE_MAX_VALUE (t) = build_int_2 (1000, 0);
1842
1843   /* These two must be different nodes because of the caching done in
1844      size_int_wide.  */
1845   sizetype = t;
1846   bitsizetype = copy_node (t);
1847   integer_type_node = 0;
1848 }
1849
1850 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1851    Also update the type of any standard type's sizes made so far.  */
1852
1853 void
1854 set_sizetype (tree type)
1855 {
1856   int oprecision = TYPE_PRECISION (type);
1857   /* The *bitsizetype types use a precision that avoids overflows when
1858      calculating signed sizes / offsets in bits.  However, when
1859      cross-compiling from a 32 bit to a 64 bit host, we are limited to 64 bit
1860      precision.  */
1861   int precision = MIN (oprecision + BITS_PER_UNIT_LOG + 1,
1862                        2 * HOST_BITS_PER_WIDE_INT);
1863   unsigned int i;
1864   tree t;
1865
1866   if (sizetype_set)
1867     abort ();
1868
1869   /* Make copies of nodes since we'll be setting TYPE_IS_SIZETYPE.  */
1870   sizetype = copy_node (type);
1871   TYPE_ORIG_SIZE_TYPE (sizetype) = type;
1872   TYPE_IS_SIZETYPE (sizetype) = 1;
1873   bitsizetype = make_node (INTEGER_TYPE);
1874   TYPE_NAME (bitsizetype) = TYPE_NAME (type);
1875   TYPE_PRECISION (bitsizetype) = precision;
1876   TYPE_IS_SIZETYPE (bitsizetype) = 1;
1877
1878   if (TYPE_UNSIGNED (type))
1879     fixup_unsigned_type (bitsizetype);
1880   else
1881     fixup_signed_type (bitsizetype);
1882
1883   layout_type (bitsizetype);
1884
1885   if (TYPE_UNSIGNED (type))
1886     {
1887       usizetype = sizetype;
1888       ubitsizetype = bitsizetype;
1889       ssizetype = copy_node (make_signed_type (oprecision));
1890       sbitsizetype = copy_node (make_signed_type (precision));
1891     }
1892   else
1893     {
1894       ssizetype = sizetype;
1895       sbitsizetype = bitsizetype;
1896       usizetype = copy_node (make_unsigned_type (oprecision));
1897       ubitsizetype = copy_node (make_unsigned_type (precision));
1898     }
1899
1900   TYPE_NAME (bitsizetype) = get_identifier ("bit_size_type");
1901
1902   /* Show is a sizetype, is a main type, and has no pointers to it.  */
1903   for (i = 0; i < ARRAY_SIZE (sizetype_tab); i++)
1904     {
1905       TYPE_IS_SIZETYPE (sizetype_tab[i]) = 1;
1906       TYPE_MAIN_VARIANT (sizetype_tab[i]) = sizetype_tab[i];
1907       TYPE_NEXT_VARIANT (sizetype_tab[i]) = 0;
1908       TYPE_POINTER_TO (sizetype_tab[i]) = 0;
1909       TYPE_REFERENCE_TO (sizetype_tab[i]) = 0;
1910     }
1911
1912   /* Go down each of the types we already made and set the proper type
1913      for the sizes in them.  */
1914   for (t = early_type_list; t != 0; t = TREE_CHAIN (t))
1915     {
1916       if (TREE_CODE (TREE_VALUE (t)) != INTEGER_TYPE
1917           && TREE_CODE (TREE_VALUE (t)) != BOOLEAN_TYPE)
1918         abort ();
1919
1920       TREE_TYPE (TYPE_SIZE (TREE_VALUE (t))) = bitsizetype;
1921       TREE_TYPE (TYPE_SIZE_UNIT (TREE_VALUE (t))) = sizetype;
1922     }
1923
1924   early_type_list = 0;
1925   sizetype_set = 1;
1926 }
1927 \f
1928 /* TYPE is an integral type, i.e., an INTEGRAL_TYPE, ENUMERAL_TYPE,
1929    BOOLEAN_TYPE, or CHAR_TYPE.  Set TYPE_MIN_VALUE and TYPE_MAX_VALUE
1930    for TYPE, based on the PRECISION and whether or not the TYPE
1931    IS_UNSIGNED.  PRECISION need not correspond to a width supported
1932    natively by the hardware; for example, on a machine with 8-bit,
1933    16-bit, and 32-bit register modes, PRECISION might be 7, 23, or
1934    61.  */
1935
1936 void
1937 set_min_and_max_values_for_integral_type (tree type,
1938                                           int precision,
1939                                           bool is_unsigned)
1940 {
1941   tree min_value;
1942   tree max_value;
1943
1944   if (is_unsigned)
1945     {
1946       min_value = build_int_2 (0, 0);
1947       max_value 
1948         = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1949                        ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1950                        precision - HOST_BITS_PER_WIDE_INT > 0
1951                        ? ((unsigned HOST_WIDE_INT) ~0
1952                           >> (HOST_BITS_PER_WIDE_INT
1953                               - (precision - HOST_BITS_PER_WIDE_INT)))
1954                        : 0);
1955     }
1956   else
1957     {
1958       min_value 
1959         = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1960                         ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1961                        (((HOST_WIDE_INT) (-1)
1962                          << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1963                              ? precision - HOST_BITS_PER_WIDE_INT - 1
1964                              : 0))));    
1965       max_value
1966         = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1967                         ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1968                        (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1969                         ? (((HOST_WIDE_INT) 1
1970                             << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1971                         : 0));
1972     }
1973
1974   TREE_TYPE (min_value) = type;
1975   TREE_TYPE (max_value) = type;
1976   TYPE_MIN_VALUE (type) = min_value;
1977   TYPE_MAX_VALUE (type) = max_value;
1978 }
1979
1980 /* Set the extreme values of TYPE based on its precision in bits,
1981    then lay it out.  Used when make_signed_type won't do
1982    because the tree code is not INTEGER_TYPE.
1983    E.g. for Pascal, when the -fsigned-char option is given.  */
1984
1985 void
1986 fixup_signed_type (tree type)
1987 {
1988   int precision = TYPE_PRECISION (type);
1989
1990   /* We can not represent properly constants greater then
1991      2 * HOST_BITS_PER_WIDE_INT, still we need the types
1992      as they are used by i386 vector extensions and friends.  */
1993   if (precision > HOST_BITS_PER_WIDE_INT * 2)
1994     precision = HOST_BITS_PER_WIDE_INT * 2;
1995
1996   set_min_and_max_values_for_integral_type (type, precision, 
1997                                             /*is_unsigned=*/false);
1998
1999   /* Lay out the type: set its alignment, size, etc.  */
2000   layout_type (type);
2001 }
2002
2003 /* Set the extreme values of TYPE based on its precision in bits,
2004    then lay it out.  This is used both in `make_unsigned_type'
2005    and for enumeral types.  */
2006
2007 void
2008 fixup_unsigned_type (tree type)
2009 {
2010   int precision = TYPE_PRECISION (type);
2011
2012   /* We can not represent properly constants greater then
2013      2 * HOST_BITS_PER_WIDE_INT, still we need the types
2014      as they are used by i386 vector extensions and friends.  */
2015   if (precision > HOST_BITS_PER_WIDE_INT * 2)
2016     precision = HOST_BITS_PER_WIDE_INT * 2;
2017
2018   set_min_and_max_values_for_integral_type (type, precision, 
2019                                             /*is_unsigned=*/true);
2020
2021   /* Lay out the type: set its alignment, size, etc.  */
2022   layout_type (type);
2023 }
2024 \f
2025 /* Find the best machine mode to use when referencing a bit field of length
2026    BITSIZE bits starting at BITPOS.
2027
2028    The underlying object is known to be aligned to a boundary of ALIGN bits.
2029    If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
2030    larger than LARGEST_MODE (usually SImode).
2031
2032    If no mode meets all these conditions, we return VOIDmode.  Otherwise, if
2033    VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
2034    mode meeting these conditions.
2035
2036    Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
2037    the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
2038    all the conditions.  */
2039
2040 enum machine_mode
2041 get_best_mode (int bitsize, int bitpos, unsigned int align,
2042                enum machine_mode largest_mode, int volatilep)
2043 {
2044   enum machine_mode mode;
2045   unsigned int unit = 0;
2046
2047   /* Find the narrowest integer mode that contains the bit field.  */
2048   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2049        mode = GET_MODE_WIDER_MODE (mode))
2050     {
2051       unit = GET_MODE_BITSIZE (mode);
2052       if ((bitpos % unit) + bitsize <= unit)
2053         break;
2054     }
2055
2056   if (mode == VOIDmode
2057       /* It is tempting to omit the following line
2058          if STRICT_ALIGNMENT is true.
2059          But that is incorrect, since if the bitfield uses part of 3 bytes
2060          and we use a 4-byte mode, we could get a spurious segv
2061          if the extra 4th byte is past the end of memory.
2062          (Though at least one Unix compiler ignores this problem:
2063          that on the Sequent 386 machine.  */
2064       || MIN (unit, BIGGEST_ALIGNMENT) > align
2065       || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
2066     return VOIDmode;
2067
2068   if (SLOW_BYTE_ACCESS && ! volatilep)
2069     {
2070       enum machine_mode wide_mode = VOIDmode, tmode;
2071
2072       for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
2073            tmode = GET_MODE_WIDER_MODE (tmode))
2074         {
2075           unit = GET_MODE_BITSIZE (tmode);
2076           if (bitpos / unit == (bitpos + bitsize - 1) / unit
2077               && unit <= BITS_PER_WORD
2078               && unit <= MIN (align, BIGGEST_ALIGNMENT)
2079               && (largest_mode == VOIDmode
2080                   || unit <= GET_MODE_BITSIZE (largest_mode)))
2081             wide_mode = tmode;
2082         }
2083
2084       if (wide_mode != VOIDmode)
2085         return wide_mode;
2086     }
2087
2088   return mode;
2089 }
2090
2091 /* Gets minimal and maximal values for MODE (signed or unsigned depending on
2092    SIGN).  The returned constants are made to be usable in TARGET_MODE.  */
2093
2094 void
2095 get_mode_bounds (enum machine_mode mode, int sign,
2096                  enum machine_mode target_mode,
2097                  rtx *mmin, rtx *mmax)
2098 {
2099   unsigned size = GET_MODE_BITSIZE (mode);
2100   unsigned HOST_WIDE_INT min_val, max_val;
2101
2102   if (size > HOST_BITS_PER_WIDE_INT)
2103     abort ();
2104
2105   if (sign)
2106     {
2107       min_val = -((unsigned HOST_WIDE_INT) 1 << (size - 1));
2108       max_val = ((unsigned HOST_WIDE_INT) 1 << (size - 1)) - 1;
2109     }
2110   else
2111     {
2112       min_val = 0;
2113       max_val = ((unsigned HOST_WIDE_INT) 1 << (size - 1) << 1) - 1;
2114     }
2115
2116   *mmin = GEN_INT (trunc_int_for_mode (min_val, target_mode));
2117   *mmax = GEN_INT (trunc_int_for_mode (max_val, target_mode));
2118 }
2119
2120 #include "gt-stor-layout.h"