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