OSDN Git Service

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