OSDN Git Service

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