OSDN Git Service

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