OSDN Git Service

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