OSDN Git Service

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