OSDN Git Service

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