OSDN Git Service

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