OSDN Git Service

* stor-layout.c (layout_decl): Revert change to handling of alignment
[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 /* Hook for a front-end function that can modify the record layout as needed
448    immediately before it is finalized.  */
449
450 void (*lang_adjust_rli) PARAMS ((record_layout_info)) = 0;
451
452 void
453 set_lang_adjust_rli (f)
454      void (*f) PARAMS ((record_layout_info));
455 {
456   lang_adjust_rli = f;
457 }
458
459 /* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
460    QUAL_UNION_TYPE.  Return a pointer to a struct record_layout_info which
461    is to be passed to all other layout functions for this record.  It is the
462    responsibility of the caller to call `free' for the storage returned. 
463    Note that garbage collection is not permitted until we finish laying
464    out the record.  */
465
466 record_layout_info
467 start_record_layout (t)
468      tree t;
469 {
470   record_layout_info rli 
471     = (record_layout_info) xmalloc (sizeof (struct record_layout_info_s));
472
473   rli->t = t;
474
475   /* If the type has a minimum specified alignment (via an attribute
476      declaration, for example) use it -- otherwise, start with a
477      one-byte alignment.  */
478   rli->record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (t));
479   rli->unpacked_align = rli->unpadded_align = rli->record_align;
480   rli->offset_align = MAX (rli->record_align, BIGGEST_ALIGNMENT);
481
482 #ifdef STRUCTURE_SIZE_BOUNDARY
483   /* Packed structures don't need to have minimum size.  */
484   if (! TYPE_PACKED (t))
485     rli->record_align = MAX (rli->record_align, STRUCTURE_SIZE_BOUNDARY);
486 #endif
487
488   rli->offset = size_zero_node;
489   rli->bitpos = bitsize_zero_node;
490   rli->pending_statics = 0;
491   rli->packed_maybe_necessary = 0;
492
493   return rli;
494 }
495
496 /* These four routines perform computations that convert between
497    the offset/bitpos forms and byte and bit offsets.  */
498
499 tree
500 bit_from_pos (offset, bitpos)
501      tree offset, bitpos;
502 {
503   return size_binop (PLUS_EXPR, bitpos,
504                      size_binop (MULT_EXPR, convert (bitsizetype, offset),
505                                  bitsize_unit_node));
506 }
507
508 tree
509 byte_from_pos (offset, bitpos)
510      tree offset, bitpos;
511 {
512   return size_binop (PLUS_EXPR, offset,
513                      convert (sizetype,
514                               size_binop (TRUNC_DIV_EXPR, bitpos,
515                                           bitsize_unit_node)));
516 }
517
518 void
519 pos_from_byte (poffset, pbitpos, off_align, pos)
520      tree *poffset, *pbitpos;
521      unsigned int off_align;
522      tree pos;
523 {
524   *poffset
525     = size_binop (MULT_EXPR,
526                   convert (sizetype,
527                            size_binop (FLOOR_DIV_EXPR, pos,
528                                        bitsize_int (off_align
529                                                     / BITS_PER_UNIT))),
530                   size_int (off_align / BITS_PER_UNIT));
531   *pbitpos = size_binop (MULT_EXPR,
532                          size_binop (FLOOR_MOD_EXPR, pos,
533                                      bitsize_int (off_align / BITS_PER_UNIT)),
534                          bitsize_unit_node);
535 }
536
537 void
538 pos_from_bit (poffset, pbitpos, off_align, pos)
539      tree *poffset, *pbitpos;
540      unsigned int off_align;
541      tree pos;
542 {
543   *poffset = size_binop (MULT_EXPR,
544                          convert (sizetype,
545                                   size_binop (FLOOR_DIV_EXPR, pos,
546                                               bitsize_int (off_align))),
547                          size_int (off_align / BITS_PER_UNIT));
548   *pbitpos = size_binop (FLOOR_MOD_EXPR, pos, bitsize_int (off_align));
549 }
550
551 /* Given a pointer to bit and byte offsets and an offset alignment,
552    normalize the offsets so they are within the alignment.  */
553
554 void
555 normalize_offset (poffset, pbitpos, off_align)
556      tree *poffset, *pbitpos;
557      unsigned int off_align;
558 {
559   /* If the bit position is now larger than it should be, adjust it
560      downwards.  */
561   if (compare_tree_int (*pbitpos, off_align) >= 0)
562     {
563       tree extra_aligns = size_binop (FLOOR_DIV_EXPR, *pbitpos,
564                                       bitsize_int (off_align));
565
566       *poffset
567         = size_binop (PLUS_EXPR, *poffset,
568                       size_binop (MULT_EXPR, convert (sizetype, extra_aligns),
569                                   size_int (off_align / BITS_PER_UNIT)));
570                                 
571       *pbitpos
572         = size_binop (FLOOR_MOD_EXPR, *pbitpos, bitsize_int (off_align));
573     }
574 }
575
576 /* Print debugging information about the information in RLI.  */
577
578 void
579 debug_rli (rli)
580      record_layout_info rli;
581 {
582   print_node_brief (stderr, "type", rli->t, 0);
583   print_node_brief (stderr, "\noffset", rli->offset, 0);
584   print_node_brief (stderr, " bitpos", rli->bitpos, 0);
585
586   fprintf (stderr, "\naligns: rec = %u, unpack = %u, unpad = %u, off = %u\n",
587            rli->record_align, rli->unpacked_align, rli->unpadded_align,
588            rli->offset_align);
589   if (rli->packed_maybe_necessary)
590     fprintf (stderr, "packed may be necessary\n");
591
592   if (rli->pending_statics)
593     {
594       fprintf (stderr, "pending statics:\n");
595       debug_tree (rli->pending_statics);
596     }
597 }
598
599 /* Given an RLI with a possibly-incremented BITPOS, adjust OFFSET and
600    BITPOS if necessary to keep BITPOS below OFFSET_ALIGN.  */
601
602 void
603 normalize_rli (rli)
604      record_layout_info rli;
605 {
606   normalize_offset (&rli->offset, &rli->bitpos, rli->offset_align);
607 }
608
609 /* Returns the size in bytes allocated so far.  */
610
611 tree
612 rli_size_unit_so_far (rli)
613      record_layout_info rli;
614 {
615   return byte_from_pos (rli->offset, rli->bitpos);
616 }
617
618 /* Returns the size in bits allocated so far.  */
619
620 tree
621 rli_size_so_far (rli)
622      record_layout_info rli;
623 {
624   return bit_from_pos (rli->offset, rli->bitpos);
625 }
626
627 /* Called from place_field to handle unions.  */
628
629 static void
630 place_union_field (rli, field)
631      record_layout_info rli;
632      tree field;
633 {
634   unsigned int desired_align;
635
636   layout_decl (field, 0);
637   
638   DECL_FIELD_OFFSET (field) = size_zero_node;
639   DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
640   SET_DECL_OFFSET_ALIGN (field, BIGGEST_ALIGNMENT);
641
642   desired_align = DECL_ALIGN (field);
643
644 #ifdef BIGGEST_FIELD_ALIGNMENT
645   /* Some targets (i.e. i386) limit union field alignment
646      to a lower boundary than alignment of variables unless
647      it was overridden by attribute aligned.  */
648   if (! DECL_USER_ALIGN (field))
649     desired_align =
650       MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
651 #endif
652
653   /* Union must be at least as aligned as any field requires.  */
654   rli->record_align = MAX (rli->record_align, desired_align);
655   rli->unpadded_align = MAX (rli->unpadded_align, desired_align);
656
657 #ifdef PCC_BITFIELD_TYPE_MATTERS
658   /* On the m88000, a bit field of declare type `int' forces the
659      entire union to have `int' alignment.  */
660   if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
661     {
662       rli->record_align = MAX (rli->record_align, 
663                                TYPE_ALIGN (TREE_TYPE (field)));
664       rli->unpadded_align = MAX (rli->unpadded_align,
665                                  TYPE_ALIGN (TREE_TYPE (field)));
666     }
667 #endif
668
669   /* We assume the union's size will be a multiple of a byte so we don't
670      bother with BITPOS.  */
671   if (TREE_CODE (rli->t) == UNION_TYPE)
672     rli->offset = size_binop (MAX_EXPR, rli->offset, DECL_SIZE_UNIT (field));
673   else if (TREE_CODE (rli->t) == QUAL_UNION_TYPE)
674     rli->offset = fold (build (COND_EXPR, sizetype, 
675                                DECL_QUALIFIER (field),
676                                DECL_SIZE_UNIT (field), rli->offset));
677 }
678
679 /* RLI contains information about the layout of a RECORD_TYPE.  FIELD
680    is a FIELD_DECL to be added after those fields already present in
681    T.  (FIELD is not actually added to the TYPE_FIELDS list here;
682    callers that desire that behavior must manually perform that step.)  */
683
684 void
685 place_field (rli, field)
686      record_layout_info rli;
687      tree field;
688 {
689   /* The alignment required for FIELD.  */
690   unsigned int desired_align;
691   /* The alignment FIELD would have if we just dropped it into the
692      record as it presently stands.  */
693   unsigned int known_align;
694   unsigned int actual_align;
695   unsigned int user_align;
696   /* The type of this field.  */
697   tree type = TREE_TYPE (field);
698  
699   if (TREE_CODE (field) == ERROR_MARK || TREE_CODE (type) == ERROR_MARK)
700       return;
701
702   /* If FIELD is static, then treat it like a separate variable, not
703      really like a structure field.  If it is a FUNCTION_DECL, it's a
704      method.  In both cases, all we do is lay out the decl, and we do
705      it *after* the record is laid out.  */
706   if (TREE_CODE (field) == VAR_DECL)
707     {
708       rli->pending_statics = tree_cons (NULL_TREE, field,
709                                         rli->pending_statics);
710       return;
711     }
712
713   /* Enumerators and enum types which are local to this class need not
714      be laid out.  Likewise for initialized constant fields.  */
715   else if (TREE_CODE (field) != FIELD_DECL)
716     return;
717
718   /* Unions are laid out very differently than records, so split
719      that code off to another function.  */
720   else if (TREE_CODE (rli->t) != RECORD_TYPE)
721     {
722       place_union_field (rli, field);
723       return;
724     }
725
726   /* Work out the known alignment so far.  Note that A & (-A) is the
727      value of the least-significant bit in A that is one.  */
728   if (! integer_zerop (rli->bitpos))
729     known_align = (tree_low_cst (rli->bitpos, 1)
730                    & - tree_low_cst (rli->bitpos, 1));
731   else if (integer_zerop (rli->offset))
732     known_align = BIGGEST_ALIGNMENT;
733   else if (host_integerp (rli->offset, 1))
734     known_align = (BITS_PER_UNIT
735                    * (tree_low_cst (rli->offset, 1)
736                       & - tree_low_cst (rli->offset, 1)));
737   else
738     known_align = rli->offset_align;
739
740   /* Lay out the field so we know what alignment it needs.  For a
741      packed field, use the alignment as specified, disregarding what
742      the type would want.  */
743   desired_align = DECL_ALIGN (field);
744   user_align = DECL_USER_ALIGN (field);
745   layout_decl (field, known_align);
746   if (! DECL_PACKED (field))
747     {
748       desired_align = DECL_ALIGN (field);
749       user_align = DECL_USER_ALIGN (field);
750     }
751
752   /* Some targets (i.e. i386, VMS) limit struct field alignment
753      to a lower boundary than alignment of variables unless
754      it was overridden by attribute aligned.  */
755 #ifdef BIGGEST_FIELD_ALIGNMENT
756   if (! user_align)
757     desired_align
758       = MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
759 #endif
760
761 #ifdef ADJUST_FIELD_ALIGN
762   desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
763 #endif
764
765   /* Record must have at least as much alignment as any field.
766      Otherwise, the alignment of the field within the record is
767      meaningless.  */
768 #ifdef PCC_BITFIELD_TYPE_MATTERS
769   if (PCC_BITFIELD_TYPE_MATTERS && type != error_mark_node
770       && DECL_BIT_FIELD_TYPE (field)
771       && ! integer_zerop (TYPE_SIZE (type)))
772     {
773       /* For these machines, a zero-length field does not
774          affect the alignment of the structure as a whole.
775          It does, however, affect the alignment of the next field
776          within the structure.  */
777       if (! integer_zerop (DECL_SIZE (field)))
778         rli->record_align = MAX (rli->record_align, desired_align);
779       else if (! DECL_PACKED (field))
780         desired_align = TYPE_ALIGN (type);
781
782       /* A named bit field of declared type `int'
783          forces the entire structure to have `int' alignment.  */
784       if (DECL_NAME (field) != 0)
785         {
786           unsigned int type_align = TYPE_ALIGN (type);
787
788           if (maximum_field_alignment != 0)
789             type_align = MIN (type_align, maximum_field_alignment);
790           else if (DECL_PACKED (field))
791             type_align = MIN (type_align, BITS_PER_UNIT);
792
793           rli->record_align = MAX (rli->record_align, type_align);
794           rli->unpadded_align = MAX (rli->unpadded_align, DECL_ALIGN (field));
795           if (warn_packed)
796             rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
797         }
798     }
799   else
800 #endif
801     {
802       rli->record_align = MAX (rli->record_align, desired_align);
803       rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
804       rli->unpadded_align = MAX (rli->unpadded_align, DECL_ALIGN (field));
805     }
806
807   if (warn_packed && DECL_PACKED (field))
808     {
809       if (known_align > TYPE_ALIGN (type))
810         {
811           if (TYPE_ALIGN (type) > desired_align)
812             {
813               if (STRICT_ALIGNMENT)
814                 warning_with_decl (field, "packed attribute causes inefficient alignment for `%s'");
815               else
816                 warning_with_decl (field, "packed attribute is unnecessary for `%s'");
817             }
818         }
819       else
820         rli->packed_maybe_necessary = 1;
821     }
822
823   /* Does this field automatically have alignment it needs by virtue
824      of the fields that precede it and the record's own alignment?  */
825   if (known_align < desired_align)
826     {
827       /* No, we need to skip space before this field.
828          Bump the cumulative size to multiple of field alignment.  */
829
830       if (warn_padded)
831         warning_with_decl (field, "padding struct to align `%s'");
832
833       /* If the alignment is still within offset_align, just align
834          the bit position.  */
835       if (desired_align < rli->offset_align)
836         rli->bitpos = round_up (rli->bitpos, desired_align);
837       else
838         {
839           /* First adjust OFFSET by the partial bits, then align.  */
840           rli->offset
841             = size_binop (PLUS_EXPR, rli->offset,
842                           convert (sizetype,
843                                    size_binop (CEIL_DIV_EXPR, rli->bitpos,
844                                                bitsize_unit_node)));
845           rli->bitpos = bitsize_zero_node;
846
847           rli->offset = round_up (rli->offset, desired_align / BITS_PER_UNIT);
848         }
849
850       if (! TREE_CONSTANT (rli->offset))
851         rli->offset_align = desired_align;
852
853     }
854
855   /* Handle compatibility with PCC.  Note that if the record has any
856      variable-sized fields, we need not worry about compatibility.  */
857 #ifdef PCC_BITFIELD_TYPE_MATTERS
858   if (PCC_BITFIELD_TYPE_MATTERS
859       && TREE_CODE (field) == FIELD_DECL
860       && type != error_mark_node
861       && DECL_BIT_FIELD (field)
862       && ! DECL_PACKED (field)
863       && maximum_field_alignment == 0
864       && ! integer_zerop (DECL_SIZE (field))
865       && host_integerp (DECL_SIZE (field), 1)
866       && host_integerp (rli->offset, 1)
867       && host_integerp (TYPE_SIZE (type), 1))
868     {
869       unsigned int type_align = TYPE_ALIGN (type);
870       tree dsize = DECL_SIZE (field);
871       HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
872       HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
873       HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
874
875       /* A bit field may not span more units of alignment of its type
876          than its type itself.  Advance to next boundary if necessary.  */
877       if ((((offset * BITS_PER_UNIT + bit_offset + field_size +
878              type_align - 1)
879             / type_align)
880            - (offset * BITS_PER_UNIT + bit_offset) / type_align)
881           > tree_low_cst (TYPE_SIZE (type), 1) / type_align)
882         rli->bitpos = round_up (rli->bitpos, type_align);
883     }
884 #endif
885
886 #ifdef BITFIELD_NBYTES_LIMITED
887   if (BITFIELD_NBYTES_LIMITED
888       && TREE_CODE (field) == FIELD_DECL
889       && type != error_mark_node
890       && DECL_BIT_FIELD_TYPE (field)
891       && ! DECL_PACKED (field)
892       && ! integer_zerop (DECL_SIZE (field))
893       && host_integerp (DECL_SIZE (field), 1)
894       && host_integerp (rli->offset, 1)
895       && host_integerp (TYPE_SIZE (type), 1))
896     {
897       unsigned int type_align = TYPE_ALIGN (type);
898       tree dsize = DECL_SIZE (field);
899       HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
900       HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
901       HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
902
903       if (maximum_field_alignment != 0)
904         type_align = MIN (type_align, maximum_field_alignment);
905       /* ??? This test is opposite the test in the containing if
906          statement, so this code is unreachable currently.  */
907       else if (DECL_PACKED (field))
908         type_align = MIN (type_align, BITS_PER_UNIT);
909
910       /* A bit field may not span the unit of alignment of its type.
911          Advance to next boundary if necessary.  */
912       /* ??? This code should match the code above for the
913          PCC_BITFIELD_TYPE_MATTERS case.  */
914       if ((offset * BITS_PER_UNIT + bit_offset) / type_align
915           != ((offset * BITS_PER_UNIT + bit_offset + field_size - 1)
916               / type_align))
917         rli->bitpos = round_up (rli->bitpos, type_align);
918     }
919 #endif
920
921   /* Offset so far becomes the position of this field after normalizing.  */
922   normalize_rli (rli);
923   DECL_FIELD_OFFSET (field) = rli->offset;
924   DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
925   SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
926
927   /* If this field ended up more aligned than we thought it would be (we
928      approximate this by seeing if its position changed), lay out the field
929      again; perhaps we can use an integral mode for it now.  */
930   if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field)))
931     actual_align = (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
932                     & - tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1));
933   else if (integer_zerop (DECL_FIELD_OFFSET (field)))
934     actual_align = BIGGEST_ALIGNMENT;
935   else if (host_integerp (DECL_FIELD_OFFSET (field), 1))
936     actual_align = (BITS_PER_UNIT
937                    * (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
938                       & - tree_low_cst (DECL_FIELD_OFFSET (field), 1)));
939   else
940     actual_align = DECL_OFFSET_ALIGN (field);
941
942   if (known_align != actual_align)
943     layout_decl (field, actual_align);
944
945   /* Now add size of this field to the size of the record.  If the size is
946      not constant, treat the field as being a multiple of bytes and just
947      adjust the offset, resetting the bit position.  Otherwise, apportion the
948      size amongst the bit position and offset.  First handle the case of an
949      unspecified size, which can happen when we have an invalid nested struct
950      definition, such as struct j { struct j { int i; } }.  The error message
951      is printed in finish_struct.  */
952   if (DECL_SIZE (field) == 0)
953     /* Do nothing.  */;
954   else if (TREE_CODE (DECL_SIZE_UNIT (field)) != INTEGER_CST
955            || TREE_CONSTANT_OVERFLOW (DECL_SIZE_UNIT (field)))
956     {
957       rli->offset
958         = size_binop (PLUS_EXPR, rli->offset,
959                       convert (sizetype,
960                                size_binop (CEIL_DIV_EXPR, rli->bitpos,
961                                            bitsize_unit_node)));
962       rli->offset
963         = size_binop (PLUS_EXPR, rli->offset, DECL_SIZE_UNIT (field));
964       rli->bitpos = bitsize_zero_node;
965       rli->offset_align = MIN (rli->offset_align, DECL_ALIGN (field));
966     }
967   else
968     {
969       rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
970       normalize_rli (rli);
971     }
972 }
973
974 /* Assuming that all the fields have been laid out, this function uses
975    RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
976    inidicated by RLI.  */
977
978 static void
979 finalize_record_size (rli)
980      record_layout_info rli;
981 {
982   tree unpadded_size, unpadded_size_unit;
983
984   /* Now we want just byte and bit offsets, so set the offset alignment
985      to be a byte and then normalize.  */
986   rli->offset_align = BITS_PER_UNIT;
987   normalize_rli (rli);
988
989   /* Determine the desired alignment.  */
990 #ifdef ROUND_TYPE_ALIGN
991   TYPE_ALIGN (rli->t) = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t),
992                                           rli->record_align);
993 #else
994   TYPE_ALIGN (rli->t) = MAX (TYPE_ALIGN (rli->t), rli->record_align);
995 #endif
996   TYPE_USER_ALIGN (rli->t) = 1;
997
998   /* Compute the size so far.  Be sure to allow for extra bits in the
999      size in bytes.  We have guaranteed above that it will be no more
1000      than a single byte.  */
1001   unpadded_size = rli_size_so_far (rli);
1002   unpadded_size_unit = rli_size_unit_so_far (rli);
1003   if (! integer_zerop (rli->bitpos))
1004     unpadded_size_unit
1005       = size_binop (PLUS_EXPR, unpadded_size_unit, size_one_node);
1006
1007   /* Record the un-rounded size in the binfo node.  But first we check
1008      the size of TYPE_BINFO to make sure that BINFO_SIZE is available.  */
1009   if (TYPE_BINFO (rli->t) && TREE_VEC_LENGTH (TYPE_BINFO (rli->t)) > 6)
1010     {
1011       TYPE_BINFO_SIZE (rli->t) = unpadded_size;
1012       TYPE_BINFO_SIZE_UNIT (rli->t) = unpadded_size_unit;
1013     }
1014
1015     /* Round the size up to be a multiple of the required alignment */
1016 #ifdef ROUND_TYPE_SIZE
1017   TYPE_SIZE (rli->t) = ROUND_TYPE_SIZE (rli->t, unpadded_size,
1018                                         TYPE_ALIGN (rli->t));
1019   TYPE_SIZE_UNIT (rli->t)
1020     = ROUND_TYPE_SIZE_UNIT (rli->t, unpadded_size_unit,
1021                             TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
1022 #else
1023   TYPE_SIZE (rli->t) = round_up (unpadded_size, TYPE_ALIGN (rli->t));
1024   TYPE_SIZE_UNIT (rli->t) = round_up (unpadded_size_unit,
1025                                       TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
1026 #endif
1027
1028   if (warn_padded && TREE_CONSTANT (unpadded_size)
1029       && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0)
1030     warning ("padding struct size to alignment boundary");
1031   
1032   if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
1033       && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
1034       && TREE_CONSTANT (unpadded_size))
1035     {
1036       tree unpacked_size;
1037
1038 #ifdef ROUND_TYPE_ALIGN
1039       rli->unpacked_align
1040         = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t), rli->unpacked_align);
1041 #else
1042       rli->unpacked_align = MAX (TYPE_ALIGN (rli->t), rli->unpacked_align);
1043 #endif
1044
1045 #ifdef ROUND_TYPE_SIZE
1046       unpacked_size = ROUND_TYPE_SIZE (rli->t, TYPE_SIZE (rli->t),
1047                                        rli->unpacked_align);
1048 #else
1049       unpacked_size = round_up (TYPE_SIZE (rli->t), rli->unpacked_align);
1050 #endif
1051
1052       if (simple_cst_equal (unpacked_size, TYPE_SIZE (rli->t)))
1053         {
1054           TYPE_PACKED (rli->t) = 0;
1055
1056           if (TYPE_NAME (rli->t))
1057             {
1058               const char *name;
1059
1060               if (TREE_CODE (TYPE_NAME (rli->t)) == IDENTIFIER_NODE)
1061                 name = IDENTIFIER_POINTER (TYPE_NAME (rli->t));
1062               else
1063                 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (rli->t)));
1064
1065               if (STRICT_ALIGNMENT)
1066                 warning ("packed attribute causes inefficient alignment for `%s'", name);
1067               else
1068                 warning ("packed attribute is unnecessary for `%s'", name);
1069             }
1070           else
1071             {
1072               if (STRICT_ALIGNMENT)
1073                 warning ("packed attribute causes inefficient alignment");
1074               else
1075                 warning ("packed attribute is unnecessary");
1076             }
1077         }
1078     }
1079 }
1080
1081 /* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE).  */
1082
1083 void
1084 compute_record_mode (type)
1085      tree type;
1086 {
1087   tree field;
1088   enum machine_mode mode = VOIDmode;
1089
1090   /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
1091      However, if possible, we use a mode that fits in a register
1092      instead, in order to allow for better optimization down the
1093      line.  */
1094   TYPE_MODE (type) = BLKmode;
1095
1096   if (! host_integerp (TYPE_SIZE (type), 1))
1097     return;
1098
1099   /* A record which has any BLKmode members must itself be
1100      BLKmode; it can't go in a register.  Unless the member is
1101      BLKmode only because it isn't aligned.  */
1102   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1103     {
1104       unsigned HOST_WIDE_INT bitpos;
1105
1106       if (TREE_CODE (field) != FIELD_DECL)
1107         continue;
1108
1109       if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK
1110           || (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1111               && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1112           || ! host_integerp (bit_position (field), 1)
1113           || ! host_integerp (DECL_SIZE (field), 1))
1114         return;
1115
1116       bitpos = int_bit_position (field);
1117           
1118       /* Must be BLKmode if any field crosses a word boundary,
1119          since extract_bit_field can't handle that in registers.  */
1120       if (bitpos / BITS_PER_WORD
1121           != ((tree_low_cst (DECL_SIZE (field), 1) + bitpos - 1)
1122               / BITS_PER_WORD)
1123           /* But there is no problem if the field is entire words.  */
1124           && tree_low_cst (DECL_SIZE (field), 1) % BITS_PER_WORD != 0)
1125         return;
1126
1127       /* If this field is the whole struct, remember its mode so
1128          that, say, we can put a double in a class into a DF
1129          register instead of forcing it to live in the stack.  */
1130       if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
1131         mode = DECL_MODE (field);
1132
1133 #ifdef MEMBER_TYPE_FORCES_BLK
1134       /* With some targets, eg. c4x, it is sub-optimal
1135          to access an aligned BLKmode structure as a scalar.  */
1136       if (mode == VOIDmode && MEMBER_TYPE_FORCES_BLK (field))
1137         return;
1138 #endif /* MEMBER_TYPE_FORCES_BLK  */
1139     }
1140
1141   /* If we only have one real field; use its mode.  This only applies to
1142      RECORD_TYPE.  This does not apply to unions.  */
1143   if (TREE_CODE (type) == RECORD_TYPE && mode != VOIDmode)
1144     TYPE_MODE (type) = mode;
1145   else
1146     TYPE_MODE (type) = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1147
1148   /* If structure's known alignment is less than what the scalar
1149      mode would need, and it matters, then stick with BLKmode.  */
1150   if (TYPE_MODE (type) != BLKmode
1151       && STRICT_ALIGNMENT
1152       && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1153             || TYPE_ALIGN (type) >= GET_MODE_ALIGNMENT (TYPE_MODE (type))))
1154     {
1155       /* If this is the only reason this type is BLKmode, then
1156          don't force containing types to be BLKmode.  */
1157       TYPE_NO_FORCE_BLK (type) = 1;
1158       TYPE_MODE (type) = BLKmode;
1159     }
1160 }
1161
1162 /* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
1163    out.  */
1164
1165 static void
1166 finalize_type_size (type)
1167      tree type;
1168 {
1169   /* Normally, use the alignment corresponding to the mode chosen.
1170      However, where strict alignment is not required, avoid
1171      over-aligning structures, since most compilers do not do this
1172      alignment.  */
1173
1174   if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1175       && (STRICT_ALIGNMENT
1176           || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1177               && TREE_CODE (type) != QUAL_UNION_TYPE
1178               && TREE_CODE (type) != ARRAY_TYPE)))
1179     {
1180       TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1181       TYPE_USER_ALIGN (type) = 0;
1182     }
1183
1184   /* Do machine-dependent extra alignment.  */
1185 #ifdef ROUND_TYPE_ALIGN
1186   TYPE_ALIGN (type)
1187     = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1188 #endif
1189
1190   /* If we failed to find a simple way to calculate the unit size
1191      of the type, find it by division.  */
1192   if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1193     /* TYPE_SIZE (type) is computed in bitsizetype.  After the division, the
1194        result will fit in sizetype.  We will get more efficient code using
1195        sizetype, so we force a conversion.  */
1196     TYPE_SIZE_UNIT (type)
1197       = convert (sizetype,
1198                  size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1199                              bitsize_unit_node));
1200
1201   if (TYPE_SIZE (type) != 0)
1202     {
1203 #ifdef ROUND_TYPE_SIZE
1204       TYPE_SIZE (type)
1205         = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1206       TYPE_SIZE_UNIT (type)
1207         = ROUND_TYPE_SIZE_UNIT (type, TYPE_SIZE_UNIT (type),
1208                                 TYPE_ALIGN (type) / BITS_PER_UNIT);
1209 #else
1210       TYPE_SIZE (type) = round_up (TYPE_SIZE (type), TYPE_ALIGN (type));
1211       TYPE_SIZE_UNIT (type)
1212         = round_up (TYPE_SIZE_UNIT (type), TYPE_ALIGN (type) / BITS_PER_UNIT);
1213 #endif
1214     }
1215
1216   /* Evaluate nonconstant sizes only once, either now or as soon as safe.  */
1217   if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1218     TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1219   if (TYPE_SIZE_UNIT (type) != 0
1220       && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1221     TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1222
1223   /* Also layout any other variants of the type.  */
1224   if (TYPE_NEXT_VARIANT (type)
1225       || type != TYPE_MAIN_VARIANT (type))
1226     {
1227       tree variant;
1228       /* Record layout info of this variant.  */
1229       tree size = TYPE_SIZE (type);
1230       tree size_unit = TYPE_SIZE_UNIT (type);
1231       unsigned int align = TYPE_ALIGN (type);
1232       unsigned int user_align = TYPE_USER_ALIGN (type);
1233       enum machine_mode mode = TYPE_MODE (type);
1234
1235       /* Copy it into all variants.  */
1236       for (variant = TYPE_MAIN_VARIANT (type);
1237            variant != 0;
1238            variant = TYPE_NEXT_VARIANT (variant))
1239         {
1240           TYPE_SIZE (variant) = size;
1241           TYPE_SIZE_UNIT (variant) = size_unit;
1242           TYPE_ALIGN (variant) = align;
1243           TYPE_USER_ALIGN (variant) = user_align;
1244           TYPE_MODE (variant) = mode;
1245         }
1246     }
1247 }
1248
1249 /* Do all of the work required to layout the type indicated by RLI,
1250    once the fields have been laid out.  This function will call `free'
1251    for RLI.  */
1252
1253 void
1254 finish_record_layout (rli)
1255      record_layout_info rli;
1256 {
1257   /* Compute the final size.  */
1258   finalize_record_size (rli);
1259
1260   /* Compute the TYPE_MODE for the record.  */
1261   compute_record_mode (rli->t);
1262
1263   /* Perform any last tweaks to the TYPE_SIZE, etc.  */
1264   finalize_type_size (rli->t);
1265
1266   /* Lay out any static members.  This is done now because their type
1267      may use the record's type.  */
1268   while (rli->pending_statics)
1269     {
1270       layout_decl (TREE_VALUE (rli->pending_statics), 0);
1271       rli->pending_statics = TREE_CHAIN (rli->pending_statics);
1272     }
1273
1274   /* Clean up.  */
1275   free (rli);
1276 }
1277 \f
1278 /* Calculate the mode, size, and alignment for TYPE.
1279    For an array type, calculate the element separation as well.
1280    Record TYPE on the chain of permanent or temporary types
1281    so that dbxout will find out about it.
1282
1283    TYPE_SIZE of a type is nonzero if the type has been laid out already.
1284    layout_type does nothing on such a type.
1285
1286    If the type is incomplete, its TYPE_SIZE remains zero.  */
1287
1288 void
1289 layout_type (type)
1290      tree type;
1291 {
1292   if (type == 0)
1293     abort ();
1294
1295   /* Do nothing if type has been laid out before.  */
1296   if (TYPE_SIZE (type))
1297     return;
1298
1299   switch (TREE_CODE (type))
1300     {
1301     case LANG_TYPE:
1302       /* This kind of type is the responsibility
1303          of the language-specific code.  */
1304       abort ();
1305
1306     case BOOLEAN_TYPE:  /* Used for Java, Pascal, and Chill. */
1307       if (TYPE_PRECISION (type) == 0)
1308         TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
1309
1310       /* ... fall through ... */
1311
1312     case INTEGER_TYPE:
1313     case ENUMERAL_TYPE:
1314     case CHAR_TYPE:
1315       if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
1316           && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
1317         TREE_UNSIGNED (type) = 1;
1318
1319       TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
1320                                                  MODE_INT);
1321       TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1322       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1323       break;
1324
1325     case REAL_TYPE:
1326       TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
1327       TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1328       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1329       break;
1330
1331     case COMPLEX_TYPE:
1332       TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
1333       TYPE_MODE (type)
1334         = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
1335                          (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
1336                           ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
1337                          0);
1338       TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1339       TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1340       break;
1341
1342     case VECTOR_TYPE:
1343       {
1344         tree subtype;
1345
1346         subtype = TREE_TYPE (type);
1347         TREE_UNSIGNED (type) = TREE_UNSIGNED (subtype);
1348         TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1349         TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1350       }
1351       break;
1352
1353     case VOID_TYPE:
1354       /* This is an incomplete type and so doesn't have a size.  */
1355       TYPE_ALIGN (type) = 1;
1356       TYPE_USER_ALIGN (type) = 0;
1357       TYPE_MODE (type) = VOIDmode;
1358       break;
1359
1360     case OFFSET_TYPE:
1361       TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
1362       TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
1363       /* A pointer might be MODE_PARTIAL_INT,
1364          but ptrdiff_t must be integral.  */
1365       TYPE_MODE (type) = mode_for_size (POINTER_SIZE, MODE_INT, 0);
1366       break;
1367
1368     case FUNCTION_TYPE:
1369     case METHOD_TYPE:
1370       TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
1371       TYPE_SIZE (type) = bitsize_int (2 * POINTER_SIZE);
1372       TYPE_SIZE_UNIT (type) = size_int ((2 * POINTER_SIZE) / BITS_PER_UNIT);
1373       break;
1374
1375     case POINTER_TYPE:
1376     case REFERENCE_TYPE:
1377       {
1378         int nbits = ((TREE_CODE (type) == REFERENCE_TYPE
1379                       && reference_types_internal)
1380                      ? GET_MODE_BITSIZE (Pmode) : POINTER_SIZE);
1381
1382         TYPE_MODE (type) = nbits == POINTER_SIZE ? ptr_mode : Pmode;
1383         TYPE_SIZE (type) = bitsize_int (nbits);
1384         TYPE_SIZE_UNIT (type) = size_int (nbits / BITS_PER_UNIT);
1385         TREE_UNSIGNED (type) = 1;
1386         TYPE_PRECISION (type) = nbits;
1387       }
1388       break;
1389
1390     case ARRAY_TYPE:
1391       {
1392         register tree index = TYPE_DOMAIN (type);
1393         register tree element = TREE_TYPE (type);
1394
1395         build_pointer_type (element);
1396
1397         /* We need to know both bounds in order to compute the size.  */
1398         if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
1399             && TYPE_SIZE (element))
1400           {
1401             tree ub = TYPE_MAX_VALUE (index);
1402             tree lb = TYPE_MIN_VALUE (index);
1403             tree length;
1404             tree element_size;
1405
1406             /* The initial subtraction should happen in the original type so
1407                that (possible) negative values are handled appropriately.  */
1408             length = size_binop (PLUS_EXPR, size_one_node,
1409                                  convert (sizetype,
1410                                           fold (build (MINUS_EXPR,
1411                                                        TREE_TYPE (lb),
1412                                                        ub, lb))));
1413
1414             /* Special handling for arrays of bits (for Chill).  */
1415             element_size = TYPE_SIZE (element);
1416             if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element)
1417                 && (integer_zerop (TYPE_MAX_VALUE (element))
1418                     || integer_onep (TYPE_MAX_VALUE (element)))
1419                 && host_integerp (TYPE_MIN_VALUE (element), 1))
1420               {
1421                 HOST_WIDE_INT maxvalue
1422                   = tree_low_cst (TYPE_MAX_VALUE (element), 1);
1423                 HOST_WIDE_INT minvalue
1424                   = tree_low_cst (TYPE_MIN_VALUE (element), 1);
1425
1426                 if (maxvalue - minvalue == 1
1427                     && (maxvalue == 1 || maxvalue == 0))
1428                   element_size = integer_one_node;
1429               }
1430
1431             TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size,
1432                                            convert (bitsizetype, length));
1433
1434             /* If we know the size of the element, calculate the total
1435                size directly, rather than do some division thing below.
1436                This optimization helps Fortran assumed-size arrays
1437                (where the size of the array is determined at runtime)
1438                substantially.
1439                Note that we can't do this in the case where the size of
1440                the elements is one bit since TYPE_SIZE_UNIT cannot be
1441                set correctly in that case.  */
1442             if (TYPE_SIZE_UNIT (element) != 0 && ! integer_onep (element_size))
1443               TYPE_SIZE_UNIT (type)
1444                 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
1445           }
1446
1447         /* Now round the alignment and size,
1448            using machine-dependent criteria if any.  */
1449
1450 #ifdef ROUND_TYPE_ALIGN
1451         TYPE_ALIGN (type)
1452           = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
1453 #else
1454         TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
1455 #endif
1456
1457 #ifdef ROUND_TYPE_SIZE
1458         if (TYPE_SIZE (type) != 0)
1459           {
1460             tree tmp
1461               = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1462
1463             /* If the rounding changed the size of the type, remove any
1464                pre-calculated TYPE_SIZE_UNIT.  */
1465             if (simple_cst_equal (TYPE_SIZE (type), tmp) != 1)
1466               TYPE_SIZE_UNIT (type) = NULL;
1467
1468             TYPE_SIZE (type) = tmp;
1469           }
1470 #endif
1471
1472         TYPE_MODE (type) = BLKmode;
1473         if (TYPE_SIZE (type) != 0
1474 #ifdef MEMBER_TYPE_FORCES_BLK
1475             && ! MEMBER_TYPE_FORCES_BLK (type)
1476 #endif
1477             /* BLKmode elements force BLKmode aggregate;
1478                else extract/store fields may lose.  */
1479             && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
1480                 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
1481           {
1482             TYPE_MODE (type)
1483               = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1484
1485             if (TYPE_MODE (type) != BLKmode
1486                 && STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
1487                 && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (TYPE_MODE (type))
1488                 && TYPE_MODE (type) != BLKmode)
1489               {
1490                 TYPE_NO_FORCE_BLK (type) = 1;
1491                 TYPE_MODE (type) = BLKmode;
1492               }
1493           }
1494         break;
1495       }
1496
1497     case RECORD_TYPE:
1498     case UNION_TYPE:
1499     case QUAL_UNION_TYPE:
1500       {
1501         tree field;
1502         record_layout_info rli;
1503
1504         /* Initialize the layout information.  */
1505         rli = start_record_layout (type);
1506
1507         /* If this is a QUAL_UNION_TYPE, we want to process the fields
1508            in the reverse order in building the COND_EXPR that denotes
1509            its size.  We reverse them again later.  */
1510         if (TREE_CODE (type) == QUAL_UNION_TYPE)
1511           TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1512
1513         /* Place all the fields.  */
1514         for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1515           place_field (rli, field);
1516
1517         if (TREE_CODE (type) == QUAL_UNION_TYPE)
1518           TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1519
1520         if (lang_adjust_rli)
1521           (*lang_adjust_rli) (rli);
1522
1523         /* Finish laying out the record.  */
1524         finish_record_layout (rli);
1525       }
1526       break;
1527
1528     case SET_TYPE:  /* Used by Chill and Pascal. */
1529       if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1530           || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1531         abort();
1532       else
1533         {
1534 #ifndef SET_WORD_SIZE
1535 #define SET_WORD_SIZE BITS_PER_WORD
1536 #endif
1537           unsigned int alignment
1538             = set_alignment ? set_alignment : SET_WORD_SIZE;
1539           int size_in_bits
1540             = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1541                - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
1542           int rounded_size
1543             = ((size_in_bits + alignment - 1) / alignment) * alignment;
1544
1545           if (rounded_size > (int) alignment)
1546             TYPE_MODE (type) = BLKmode;
1547           else
1548             TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1549
1550           TYPE_SIZE (type) = bitsize_int (rounded_size);
1551           TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1552           TYPE_ALIGN (type) = alignment;
1553           TYPE_USER_ALIGN (type) = 0;
1554           TYPE_PRECISION (type) = size_in_bits;
1555         }
1556       break;
1557
1558     case FILE_TYPE:
1559       /* The size may vary in different languages, so the language front end
1560          should fill in the size.  */
1561       TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1562       TYPE_USER_ALIGN (type) = 0;
1563       TYPE_MODE  (type) = BLKmode;
1564       break;
1565
1566     default:
1567       abort ();
1568     }
1569
1570   /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE.  For
1571      records and unions, finish_record_layout already called this
1572      function.  */
1573   if (TREE_CODE (type) != RECORD_TYPE 
1574       && TREE_CODE (type) != UNION_TYPE
1575       && TREE_CODE (type) != QUAL_UNION_TYPE)
1576     finalize_type_size (type);
1577
1578   /* If this type is created before sizetype has been permanently set,
1579      record it so set_sizetype can fix it up.  */
1580   if (! sizetype_set)
1581     early_type_list = tree_cons (NULL_TREE, type, early_type_list);
1582
1583   /* If an alias set has been set for this aggregate when it was incomplete,
1584      force it into alias set 0.
1585      This is too conservative, but we cannot call record_component_aliases
1586      here because some frontends still change the aggregates after
1587      layout_type.  */
1588   if (AGGREGATE_TYPE_P (type) && TYPE_ALIAS_SET_KNOWN_P (type))
1589     TYPE_ALIAS_SET (type) = 0;
1590 }
1591 \f
1592 /* Create and return a type for signed integers of PRECISION bits.  */
1593
1594 tree
1595 make_signed_type (precision)
1596      int precision;
1597 {
1598   register tree type = make_node (INTEGER_TYPE);
1599
1600   TYPE_PRECISION (type) = precision;
1601
1602   fixup_signed_type (type);
1603   return type;
1604 }
1605
1606 /* Create and return a type for unsigned integers of PRECISION bits.  */
1607
1608 tree
1609 make_unsigned_type (precision)
1610      int precision;
1611 {
1612   register tree type = make_node (INTEGER_TYPE);
1613
1614   TYPE_PRECISION (type) = precision;
1615
1616   fixup_unsigned_type (type);
1617   return type;
1618 }
1619 \f
1620 /* Initialize sizetype and bitsizetype to a reasonable and temporary
1621    value to enable integer types to be created.  */
1622
1623 void
1624 initialize_sizetypes ()
1625 {
1626   tree t = make_node (INTEGER_TYPE);
1627
1628   /* Set this so we do something reasonable for the build_int_2 calls
1629      below.  */
1630   integer_type_node = t;
1631
1632   TYPE_MODE (t) = SImode;
1633   TYPE_ALIGN (t) = GET_MODE_ALIGNMENT (SImode);
1634   TYPE_USER_ALIGN (t) = 0;
1635   TYPE_SIZE (t) = build_int_2 (GET_MODE_BITSIZE (SImode), 0);
1636   TYPE_SIZE_UNIT (t) = build_int_2 (GET_MODE_SIZE (SImode), 0);
1637   TREE_UNSIGNED (t) = 1;
1638   TYPE_PRECISION (t) = GET_MODE_BITSIZE (SImode);
1639   TYPE_MIN_VALUE (t) = build_int_2 (0, 0);
1640   TYPE_IS_SIZETYPE (t) = 1;
1641
1642   /* 1000 avoids problems with possible overflow and is certainly
1643      larger than any size value we'd want to be storing.  */
1644   TYPE_MAX_VALUE (t) = build_int_2 (1000, 0);
1645
1646   /* These two must be different nodes because of the caching done in
1647      size_int_wide.  */
1648   sizetype = t;
1649   bitsizetype = copy_node (t);
1650   integer_type_node = 0;
1651 }
1652
1653 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1654    Also update the type of any standard type's sizes made so far.  */
1655
1656 void
1657 set_sizetype (type)
1658      tree type;
1659 {
1660   int oprecision = TYPE_PRECISION (type);
1661   /* The *bitsizetype types use a precision that avoids overflows when
1662      calculating signed sizes / offsets in bits.  However, when
1663      cross-compiling from a 32 bit to a 64 bit host, we are limited to 64 bit
1664      precision.  */
1665   int precision = MIN (oprecision + BITS_PER_UNIT_LOG + 1,
1666                        2 * HOST_BITS_PER_WIDE_INT);
1667   unsigned int i;
1668   tree t;
1669
1670   if (sizetype_set)
1671     abort ();
1672
1673   /* Make copies of nodes since we'll be setting TYPE_IS_SIZETYPE.  */
1674   sizetype = copy_node (type);
1675   TYPE_DOMAIN (sizetype) = type;
1676   TYPE_IS_SIZETYPE (sizetype) = 1;
1677   bitsizetype = make_node (INTEGER_TYPE);
1678   TYPE_NAME (bitsizetype) = TYPE_NAME (type);
1679   TYPE_PRECISION (bitsizetype) = precision;
1680   TYPE_IS_SIZETYPE (bitsizetype) = 1;
1681
1682   if (TREE_UNSIGNED (type))
1683     fixup_unsigned_type (bitsizetype);
1684   else
1685     fixup_signed_type (bitsizetype);
1686
1687   layout_type (bitsizetype);
1688
1689   if (TREE_UNSIGNED (type))
1690     {
1691       usizetype = sizetype;
1692       ubitsizetype = bitsizetype;
1693       ssizetype = copy_node (make_signed_type (oprecision));
1694       sbitsizetype = copy_node (make_signed_type (precision));
1695     }
1696   else
1697     {
1698       ssizetype = sizetype;
1699       sbitsizetype = bitsizetype;
1700       usizetype = copy_node (make_unsigned_type (oprecision));
1701       ubitsizetype = copy_node (make_unsigned_type (precision));
1702     }
1703
1704   TYPE_NAME (bitsizetype) = get_identifier ("bit_size_type");
1705
1706   /* Show is a sizetype, is a main type, and has no pointers to it.  */
1707   for (i = 0; i < ARRAY_SIZE (sizetype_tab); i++)
1708     {
1709       TYPE_IS_SIZETYPE (sizetype_tab[i]) = 1;
1710       TYPE_MAIN_VARIANT (sizetype_tab[i]) = sizetype_tab[i];
1711       TYPE_NEXT_VARIANT (sizetype_tab[i]) = 0;
1712       TYPE_POINTER_TO (sizetype_tab[i]) = 0;
1713       TYPE_REFERENCE_TO (sizetype_tab[i]) = 0;
1714     }
1715
1716   ggc_add_tree_root ((tree *) &sizetype_tab,
1717                      sizeof sizetype_tab / sizeof (tree));
1718
1719   /* Go down each of the types we already made and set the proper type
1720      for the sizes in them.  */
1721   for (t = early_type_list; t != 0; t = TREE_CHAIN (t))
1722     {
1723       if (TREE_CODE (TREE_VALUE (t)) != INTEGER_TYPE)
1724         abort ();
1725
1726       TREE_TYPE (TYPE_SIZE (TREE_VALUE (t))) = bitsizetype;
1727       TREE_TYPE (TYPE_SIZE_UNIT (TREE_VALUE (t))) = sizetype;
1728     }
1729
1730   early_type_list = 0;
1731   sizetype_set = 1;
1732 }
1733 \f
1734 /* Set the extreme values of TYPE based on its precision in bits,
1735    then lay it out.  Used when make_signed_type won't do
1736    because the tree code is not INTEGER_TYPE.
1737    E.g. for Pascal, when the -fsigned-char option is given.  */
1738
1739 void
1740 fixup_signed_type (type)
1741      tree type;
1742 {
1743   register int precision = TYPE_PRECISION (type);
1744
1745   TYPE_MIN_VALUE (type)
1746     = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1747                     ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1748                    (((HOST_WIDE_INT) (-1)
1749                      << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1750                          ? precision - HOST_BITS_PER_WIDE_INT - 1
1751                          : 0))));
1752   TYPE_MAX_VALUE (type)
1753     = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1754                     ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1755                    (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1756                     ? (((HOST_WIDE_INT) 1
1757                         << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1758                     : 0));
1759
1760   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1761   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1762
1763   /* Lay out the type: set its alignment, size, etc.  */
1764   layout_type (type);
1765 }
1766
1767 /* Set the extreme values of TYPE based on its precision in bits,
1768    then lay it out.  This is used both in `make_unsigned_type'
1769    and for enumeral types.  */
1770
1771 void
1772 fixup_unsigned_type (type)
1773      tree type;
1774 {
1775   register int precision = TYPE_PRECISION (type);
1776
1777   TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1778   TYPE_MAX_VALUE (type)
1779     = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1780                    ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1781                    precision - HOST_BITS_PER_WIDE_INT > 0
1782                    ? ((unsigned HOST_WIDE_INT) ~0
1783                       >> (HOST_BITS_PER_WIDE_INT
1784                           - (precision - HOST_BITS_PER_WIDE_INT)))
1785                    : 0);
1786   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1787   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1788
1789   /* Lay out the type: set its alignment, size, etc.  */
1790   layout_type (type);
1791 }
1792 \f
1793 /* Find the best machine mode to use when referencing a bit field of length
1794    BITSIZE bits starting at BITPOS.
1795
1796    The underlying object is known to be aligned to a boundary of ALIGN bits.
1797    If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1798    larger than LARGEST_MODE (usually SImode).
1799
1800    If no mode meets all these conditions, we return VOIDmode.  Otherwise, if
1801    VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1802    mode meeting these conditions.
1803
1804    Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1805    the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1806    all the conditions.  */
1807
1808 enum machine_mode
1809 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1810      int bitsize, bitpos;
1811      unsigned int align;
1812      enum machine_mode largest_mode;
1813      int volatilep;
1814 {
1815   enum machine_mode mode;
1816   unsigned int unit = 0;
1817
1818   /* Find the narrowest integer mode that contains the bit field.  */
1819   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1820        mode = GET_MODE_WIDER_MODE (mode))
1821     {
1822       unit = GET_MODE_BITSIZE (mode);
1823       if ((bitpos % unit) + bitsize <= unit)
1824         break;
1825     }
1826
1827   if (mode == VOIDmode
1828       /* It is tempting to omit the following line
1829          if STRICT_ALIGNMENT is true.
1830          But that is incorrect, since if the bitfield uses part of 3 bytes
1831          and we use a 4-byte mode, we could get a spurious segv
1832          if the extra 4th byte is past the end of memory.
1833          (Though at least one Unix compiler ignores this problem:
1834          that on the Sequent 386 machine.  */
1835       || MIN (unit, BIGGEST_ALIGNMENT) > align
1836       || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1837     return VOIDmode;
1838
1839   if (SLOW_BYTE_ACCESS && ! volatilep)
1840     {
1841       enum machine_mode wide_mode = VOIDmode, tmode;
1842
1843       for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1844            tmode = GET_MODE_WIDER_MODE (tmode))
1845         {
1846           unit = GET_MODE_BITSIZE (tmode);
1847           if (bitpos / unit == (bitpos + bitsize - 1) / unit
1848               && unit <= BITS_PER_WORD
1849               && unit <= MIN (align, BIGGEST_ALIGNMENT)
1850               && (largest_mode == VOIDmode
1851                   || unit <= GET_MODE_BITSIZE (largest_mode)))
1852             wide_mode = tmode;
1853         }
1854
1855       if (wide_mode != VOIDmode)
1856         return wide_mode;
1857     }
1858
1859   return mode;
1860 }
1861
1862 /* Return the alignment of MODE. This will be bounded by 1 and
1863    BIGGEST_ALIGNMENT.  */
1864
1865 unsigned int
1866 get_mode_alignment (mode)
1867      enum machine_mode mode;
1868 {
1869   unsigned int alignment = GET_MODE_UNIT_SIZE (mode) * BITS_PER_UNIT;
1870   
1871   /* Extract the LSB of the size.  */
1872   alignment = alignment & -alignment;
1873
1874   alignment = MIN (BIGGEST_ALIGNMENT, MAX (1, alignment));
1875   return alignment;
1876 }
1877
1878 /* This function is run once to initialize stor-layout.c.  */
1879
1880 void
1881 init_stor_layout_once ()
1882 {
1883   ggc_add_tree_root (&pending_sizes, 1);
1884 }