OSDN Git Service

Add prototypes for layout_record and layout_union.
[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 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20
21 #include "config.h"
22 #include <stdio.h>
23
24 #include "tree.h"
25 #include "function.h"
26
27 #define CEIL(x,y) (((x) + (y) - 1) / (y))
28
29 /* Data type for the expressions representing sizes of data types.
30    It is the first integer type laid out.
31    In C, this is int.  */
32
33 tree sizetype;
34
35 /* An integer constant with value 0 whose type is sizetype.  */
36
37 tree size_zero_node;
38
39 /* An integer constant with value 1 whose type is sizetype.  */
40
41 tree size_one_node;
42
43 /* If nonzero, this is an upper limit on alignment of structure fields.
44    The value is measured in bits.  */
45 int maximum_field_alignment;
46
47 #define GET_MODE_ALIGNMENT(MODE)   \
48   MIN (BIGGEST_ALIGNMENT,          \
49        MAX (1, (GET_MODE_UNIT_SIZE (MODE) * BITS_PER_UNIT)))
50
51 static enum machine_mode smallest_mode_for_size  PROTO((unsigned int,
52                                                         enum mode_class));
53 static tree layout_record       PROTO((tree));
54 static void layout_union        PROTO((tree));
55 \f
56 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded.  */
57
58 static tree pending_sizes;
59
60 /* Nonzero means cannot safely call expand_expr now,
61    so put variable sizes onto `pending_sizes' instead.  */
62
63 int immediate_size_expand;
64
65 tree
66 get_pending_sizes ()
67 {
68   tree chain = pending_sizes;
69   tree t;
70
71   /* Put each SAVE_EXPR into the current function.  */
72   for (t = chain; t; t = TREE_CHAIN (t))
73     SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
74   pending_sizes = 0;
75   return chain;
76 }
77
78 /* Given a size SIZE that isn't constant, return a SAVE_EXPR
79    to serve as the actual size-expression for a type or decl.  */
80
81 tree
82 variable_size (size)
83      tree size;
84 {
85   size = save_expr (size);
86
87   /* If the language-processor is to take responsibility for variable-sized
88      items (e.g., languages which have elaboration procedures like Ada),
89      just return SIZE unchanged.  */
90   if (global_bindings_p () < 0)
91     return size;
92
93   else if (global_bindings_p ())
94     {
95       if (TREE_CONSTANT (size))
96         error ("type size can't be explicitly evaluated");
97       else
98         error ("variable-size type declared outside of any function");
99
100       return size_int (1);
101     }
102
103   if (immediate_size_expand)
104     /* NULL_RTX is not defined; neither is the rtx type. 
105        Also, we would like to pass const0_rtx here, but don't have it.  */
106     expand_expr (size, expand_expr (integer_zero_node, NULL_PTR, VOIDmode, 0),
107                  VOIDmode, 0);
108   else
109     pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
110
111   return size;
112 }
113 \f
114 #ifndef MAX_FIXED_MODE_SIZE
115 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
116 #endif
117
118 /* Return the machine mode to use for a nonscalar of SIZE bits.
119    The mode must be in class CLASS, and have exactly that many bits.
120    If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
121    be used.  */
122
123 enum machine_mode
124 mode_for_size (size, class, limit)
125      unsigned int size;
126      enum mode_class class;
127      int limit;
128 {
129   register enum machine_mode mode;
130
131   if (limit && size > MAX_FIXED_MODE_SIZE)
132     return BLKmode;
133
134   /* Get the first mode which has this size, in the specified class.  */
135   for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
136        mode = GET_MODE_WIDER_MODE (mode))
137     if (GET_MODE_BITSIZE (mode) == size)
138       return mode;
139
140   return BLKmode;
141 }
142
143 /* Similar, but never return BLKmode; return the narrowest mode that
144    contains at least the requested number of bits.  */
145
146 static enum machine_mode
147 smallest_mode_for_size (size, class)
148      unsigned int size;
149      enum mode_class class;
150 {
151   register enum machine_mode mode;
152
153   /* Get the first mode which has at least this size, in the
154      specified class.  */
155   for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
156        mode = GET_MODE_WIDER_MODE (mode))
157     if (GET_MODE_BITSIZE (mode) >= size)
158       return mode;
159
160   abort ();
161 }
162
163 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.  */
164
165 tree
166 round_up (value, divisor)
167      tree value;
168      int divisor;
169 {
170   return size_binop (MULT_EXPR,
171                      size_binop (CEIL_DIV_EXPR, value, size_int (divisor)),
172                      size_int (divisor));
173 }
174 \f
175 /* Set the size, mode and alignment of a ..._DECL node.
176    TYPE_DECL does need this for C++.
177    Note that LABEL_DECL and CONST_DECL nodes do not need this,
178    and FUNCTION_DECL nodes have them set up in a special (and simple) way.
179    Don't call layout_decl for them.
180
181    KNOWN_ALIGN is the amount of alignment we can assume this
182    decl has with no special effort.  It is relevant only for FIELD_DECLs
183    and depends on the previous fields.
184    All that matters about KNOWN_ALIGN is which powers of 2 divide it.
185    If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
186    the record will be aligned to suit.  */
187
188 void
189 layout_decl (decl, known_align)
190      tree decl;
191      unsigned known_align;
192 {
193   register tree type = TREE_TYPE (decl);
194   register enum tree_code code = TREE_CODE (decl);
195   int spec_size = DECL_FIELD_SIZE (decl);
196
197   if (code == CONST_DECL)
198     return;
199
200   if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
201       && code != FIELD_DECL && code != TYPE_DECL)
202     abort ();
203
204   if (type == error_mark_node)
205     {
206       type = void_type_node;
207       spec_size = 0;
208     }
209
210   /* Usually the size and mode come from the data type without change.  */
211
212   DECL_MODE (decl) = TYPE_MODE (type);
213   TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
214   if (DECL_SIZE (decl) == 0)
215     DECL_SIZE (decl) = TYPE_SIZE (type);
216
217   if (code == FIELD_DECL && DECL_BIT_FIELD (decl))
218     {
219       /* This is a bit-field.  We don't know how to handle
220          them except for integers and enums, and front end should
221          never generate them otherwise.  */
222
223       if (! (TREE_CODE (type) == INTEGER_TYPE
224              || TREE_CODE (type) == ENUMERAL_TYPE))
225         abort ();
226
227       if (spec_size == 0 && DECL_NAME (decl) != 0)
228         abort ();
229
230       /* Size is specified number of bits.  */
231       DECL_SIZE (decl) = size_int (spec_size);
232     }
233   /* Force alignment required for the data type.
234      But if the decl itself wants greater alignment, don't override that.
235      Likewise, if the decl is packed, don't override it.  */
236   else if (DECL_ALIGN (decl) == 0
237            || (! DECL_PACKED (decl) &&  TYPE_ALIGN (type) > DECL_ALIGN (decl)))
238     DECL_ALIGN (decl) = TYPE_ALIGN (type);
239
240   /* See if we can use an ordinary integer mode for a bit-field.  */
241   /* Conditions are: a fixed size that is correct for another mode
242      and occupying a complete byte or bytes on proper boundary.  */
243   if (code == FIELD_DECL)
244     {
245       DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
246       if (maximum_field_alignment != 0)
247         DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
248     }
249
250   if (DECL_BIT_FIELD (decl)
251       && TYPE_SIZE (type) != 0
252       && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
253     {
254       register enum machine_mode xmode
255         = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1);
256
257       if (xmode != BLKmode
258           && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
259         {
260           DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
261                                    DECL_ALIGN (decl));
262           DECL_MODE (decl) = xmode;
263           DECL_SIZE (decl) = size_int (GET_MODE_BITSIZE (xmode));
264           /* This no longer needs to be accessed as a bit field.  */
265           DECL_BIT_FIELD (decl) = 0;
266         }
267     }
268
269   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
270   if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
271     DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
272 }
273 \f
274 /* Lay out a RECORD_TYPE type (a C struct).
275    This means laying out the fields, determining their positions,
276    and computing the overall size and required alignment of the record.
277    Note that if you set the TYPE_ALIGN before calling this
278    then the struct is aligned to at least that boundary.
279
280    If the type has basetypes, you must call layout_basetypes
281    before calling this function.
282
283    The return value is a list of static members of the record.
284    They still need to be laid out.  */
285
286 static tree
287 layout_record (rec)
288      tree rec;
289 {
290   register tree field;
291 #ifdef STRUCTURE_SIZE_BOUNDARY
292   unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
293 #else
294   unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
295 #endif
296   /* These must be laid out *after* the record is.  */
297   tree pending_statics = NULL_TREE;
298   /* Record size so far is CONST_SIZE + VAR_SIZE bits,
299      where CONST_SIZE is an integer
300      and VAR_SIZE is a tree expression.
301      If VAR_SIZE is null, the size is just CONST_SIZE.
302      Naturally we try to avoid using VAR_SIZE.  */
303   register int const_size = 0;
304   register tree var_size = 0;
305   /* Once we start using VAR_SIZE, this is the maximum alignment
306      that we know VAR_SIZE has.  */
307   register int var_align = BITS_PER_UNIT;
308
309
310   for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
311     {
312       register int desired_align;
313
314       /* If FIELD is static, then treat it like a separate variable,
315          not really like a structure field.
316          If it is a FUNCTION_DECL, it's a method.
317          In both cases, all we do is lay out the decl,
318          and we do it *after* the record is laid out.  */
319
320       if (TREE_STATIC (field))
321         {
322           pending_statics = tree_cons (NULL_TREE, field, pending_statics);
323           continue;
324         }
325       /* Enumerators and enum types which are local to this class need not
326          be laid out.  Likewise for initialized constant fields.  */
327       if (TREE_CODE (field) != FIELD_DECL)
328         continue;
329
330       /* Lay out the field so we know what alignment it needs.
331          For KNOWN_ALIGN, pass the number of bits from start of record
332          or some divisor of it.  */
333
334       /* For a packed field, use the alignment as specified,
335          disregarding what the type would want.  */
336       if (DECL_PACKED (field))
337         desired_align = DECL_ALIGN (field);
338       layout_decl (field, var_size ? var_align : const_size);
339       if (! DECL_PACKED (field))
340         desired_align = DECL_ALIGN (field);
341       /* Some targets (i.e. VMS) limit struct field alignment
342          to a lower boundary than alignment of variables.  */
343 #ifdef BIGGEST_FIELD_ALIGNMENT
344       desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
345 #endif
346
347       /* Record must have at least as much alignment as any field.
348          Otherwise, the alignment of the field within the record
349          is meaningless.  */
350
351 #ifndef PCC_BITFIELD_TYPE_MATTERS
352       record_align = MAX (record_align, desired_align);
353 #else
354       if (PCC_BITFIELD_TYPE_MATTERS && TREE_TYPE (field) != error_mark_node
355           && DECL_BIT_FIELD_TYPE (field)
356           && ! integer_zerop (TYPE_SIZE (TREE_TYPE (field))))
357         {
358           /* For these machines, a zero-length field does not
359              affect the alignment of the structure as a whole.
360              It does, however, affect the alignment of the next field
361              within the structure.  */
362           if (! integer_zerop (DECL_SIZE (field)))
363             record_align = MAX (record_align, desired_align);
364           else if (! DECL_PACKED (field))
365             desired_align = TYPE_ALIGN (TREE_TYPE (field));
366           /* A named bit field of declared type `int'
367              forces the entire structure to have `int' alignment.  */
368           if (DECL_NAME (field) != 0)
369             {
370               int type_align = TYPE_ALIGN (TREE_TYPE (field));
371               if (maximum_field_alignment != 0)
372                 type_align = MIN (type_align, maximum_field_alignment);
373
374               record_align = MAX (record_align, type_align);
375             }
376         }
377       else
378         record_align = MAX (record_align, desired_align);
379 #endif
380
381       /* Does this field automatically have alignment it needs
382          by virtue of the fields that precede it and the record's
383          own alignment?  */
384
385       if (const_size % desired_align != 0
386           || (var_align % desired_align != 0
387               && var_size != 0))
388         {
389           /* No, we need to skip space before this field.
390              Bump the cumulative size to multiple of field alignment.  */
391
392           if (var_size == 0
393               || var_align % desired_align == 0)
394             const_size
395               = CEIL (const_size, desired_align) * desired_align;
396           else
397             {
398               if (const_size > 0)
399                 var_size = size_binop (PLUS_EXPR, var_size,
400                                        size_int (const_size));
401               const_size = 0;
402               var_size = round_up (var_size, desired_align);
403               var_align = MIN (var_align, desired_align);
404             }
405         }
406
407 #ifdef PCC_BITFIELD_TYPE_MATTERS
408       if (PCC_BITFIELD_TYPE_MATTERS
409           && TREE_CODE (field) == FIELD_DECL
410           && TREE_TYPE (field) != error_mark_node
411           && DECL_BIT_FIELD_TYPE (field)
412           && !DECL_PACKED (field)
413           && !integer_zerop (DECL_SIZE (field)))
414         {
415           int type_align = TYPE_ALIGN (TREE_TYPE (field));
416           register tree dsize = DECL_SIZE (field);
417           int field_size = TREE_INT_CST_LOW (dsize);
418
419           if (maximum_field_alignment != 0)
420             type_align = MIN (type_align, maximum_field_alignment);
421
422           /* A bit field may not span the unit of alignment of its type.
423              Advance to next boundary if necessary.  */
424           /* ??? There is some uncertainty here as to what
425              should be done if type_align is less than the width of the type.
426              That can happen because the width exceeds BIGGEST_ALIGNMENT
427              or because it exceeds maximum_field_alignment.  */
428           if (const_size / type_align
429               != (const_size + field_size - 1) / type_align)
430             const_size = CEIL (const_size, type_align) * type_align;
431         }
432 #endif
433
434 /* No existing machine description uses this parameter.
435    So I have made it in this aspect identical to PCC_BITFIELD_TYPE_MATTERS.  */
436 #ifdef BITFIELD_NBYTES_LIMITED
437       if (BITFIELD_NBYTES_LIMITED
438           && TREE_CODE (field) == FIELD_DECL
439           && TREE_TYPE (field) != error_mark_node
440           && DECL_BIT_FIELD_TYPE (field)
441           && !DECL_PACKED (field)
442           && !integer_zerop (DECL_SIZE (field)))
443         {
444           int type_align = TYPE_ALIGN (TREE_TYPE (field));
445           register tree dsize = DECL_SIZE (field);
446           int field_size = TREE_INT_CST_LOW (dsize);
447
448           if (maximum_field_alignment != 0)
449             type_align = MIN (type_align, maximum_field_alignment);
450
451           /* A bit field may not span the unit of alignment of its type.
452              Advance to next boundary if necessary.  */
453           if (const_size / type_align
454               != (const_size + field_size - 1) / type_align)
455             const_size = CEIL (const_size, type_align) * type_align;
456         }
457 #endif
458
459       /* Size so far becomes the position of this field.  */
460
461       if (var_size && const_size)
462         DECL_FIELD_BITPOS (field)
463           = size_binop (PLUS_EXPR, var_size, size_int (const_size));
464       else if (var_size)
465         DECL_FIELD_BITPOS (field) = var_size;
466       else
467         DECL_FIELD_BITPOS (field) = size_int (const_size);
468
469       /* If this field is an anonymous union,
470          give each union-member the same position as the union has.
471
472          ??? This is a real kludge because it makes the structure
473          of the types look strange.  This feature is only used by
474          C++, which should have build_component_ref build two
475          COMPONENT_REF operations, one for the union and one for
476          the inner field.  We set the offset of this field to zero
477          so that either the old or the correct method will work.
478          Setting DECL_FIELD_CONTEXT is wrong unless the inner fields are
479          moved into the type of this field, but nothing seems to break
480          by doing this.  This kludge should be removed after 2.4.  */
481
482       if (DECL_NAME (field) == 0
483           && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
484         {
485           tree uelt = TYPE_FIELDS (TREE_TYPE (field));
486           for (; uelt; uelt = TREE_CHAIN (uelt))
487             {
488               DECL_FIELD_CONTEXT (uelt) = DECL_FIELD_CONTEXT (field);
489               DECL_FIELD_BITPOS (uelt) = DECL_FIELD_BITPOS (field);
490             }
491
492           DECL_FIELD_BITPOS (field) = integer_zero_node;
493         }
494
495       /* Now add size of this field to the size of the record.  */
496
497       {
498         register tree dsize = DECL_SIZE (field);
499
500         /* This can happen when we have an invalid nested struct definition,
501            such as struct j { struct j { int i; } }.  The error message is
502            printed in finish_struct.  */
503         if (dsize == 0)
504           /* Do nothing.  */;
505         else if (TREE_CODE (dsize) == INTEGER_CST
506                  && TREE_INT_CST_HIGH (dsize) == 0
507                  && TREE_INT_CST_LOW (dsize) + const_size > const_size)
508           /* Use const_size if there's no overflow.  */
509           const_size += TREE_INT_CST_LOW (dsize);
510         else
511           {
512             if (var_size == 0)
513               var_size = dsize;
514             else
515               var_size = size_binop (PLUS_EXPR, var_size, dsize);
516           }
517       }
518     }
519
520   /* Work out the total size and alignment of the record
521      as one expression and store in the record type.
522      Round it up to a multiple of the record's alignment.  */
523
524   if (var_size == 0)
525     {
526       TYPE_SIZE (rec) = size_int (const_size);
527     }
528   else
529     {
530       if (const_size)
531         var_size
532           = size_binop (PLUS_EXPR, var_size, size_int (const_size));
533       TYPE_SIZE (rec) = var_size;
534     }
535
536   /* Determine the desired alignment.  */
537 #ifdef ROUND_TYPE_ALIGN
538   TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), record_align);
539 #else
540   TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), record_align);
541 #endif
542
543 #ifdef ROUND_TYPE_SIZE
544   TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
545 #else
546   /* Round the size up to be a multiple of the required alignment */
547   TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
548 #endif
549
550   return pending_statics;
551 }
552 \f
553 /* Lay out a UNION_TYPE or QUAL_UNION_TYPE type.
554    Lay out all the fields, set their positions to zero,
555    and compute the size and alignment of the union (maximum of any field).
556    Note that if you set the TYPE_ALIGN before calling this
557    then the union align is aligned to at least that boundary.  */
558
559 static void
560 layout_union (rec)
561      tree rec;
562 {
563   register tree field;
564 #ifdef STRUCTURE_SIZE_BOUNDARY
565   unsigned union_align = STRUCTURE_SIZE_BOUNDARY;
566 #else
567   unsigned union_align = BITS_PER_UNIT;
568 #endif
569
570   /* The size of the union, based on the fields scanned so far,
571      is max (CONST_SIZE, VAR_SIZE).
572      VAR_SIZE may be null; then CONST_SIZE by itself is the size.  */
573   register int const_size = 0;
574   register tree var_size = 0;
575
576   /* If this is a QUAL_UNION_TYPE, we want to process the fields in
577      the reverse order in building the COND_EXPR that denotes its
578      size.  We reverse them again later.  */
579   if (TREE_CODE (rec) == QUAL_UNION_TYPE)
580     TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
581
582   for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
583     {
584       /* Enums which are local to this class need not be laid out.  */
585       if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
586         continue;
587
588       layout_decl (field, 0);
589       DECL_FIELD_BITPOS (field) = size_int (0);
590
591       /* Union must be at least as aligned as any field requires.  */
592
593       union_align = MAX (union_align, DECL_ALIGN (field));
594
595 #ifdef PCC_BITFIELD_TYPE_MATTERS
596       /* On the m88000, a bit field of declare type `int'
597          forces the entire union to have `int' alignment.  */
598       if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
599         union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
600 #endif
601
602       if (TREE_CODE (rec) == UNION_TYPE)
603         {
604           /* Set union_size to max (decl_size, union_size).
605              There are more and less general ways to do this.
606              Use only CONST_SIZE unless forced to use VAR_SIZE.  */
607
608           if (TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
609             const_size
610               = MAX (const_size, TREE_INT_CST_LOW (DECL_SIZE (field)));
611           else if (var_size == 0)
612             var_size = DECL_SIZE (field);
613           else
614             var_size = size_binop (MAX_EXPR, var_size, DECL_SIZE (field));
615         }
616       else if (TREE_CODE (rec) == QUAL_UNION_TYPE)
617         var_size = fold (build (COND_EXPR, sizetype, DECL_QUALIFIER (field),
618                                 DECL_SIZE (field),
619                                 var_size ? var_size : integer_zero_node));
620       }
621
622   if (TREE_CODE (rec) == QUAL_UNION_TYPE)
623     TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
624
625   /* Determine the ultimate size of the union (in bytes).  */
626   if (NULL == var_size)
627     TYPE_SIZE (rec) = size_int (CEIL (const_size, BITS_PER_UNIT)
628                                 * BITS_PER_UNIT);
629   else if (const_size == 0)
630     TYPE_SIZE (rec) = var_size;
631   else
632     TYPE_SIZE (rec) = size_binop (MAX_EXPR, var_size,
633                                   round_up (size_int (const_size),
634                                             BITS_PER_UNIT));
635
636   /* Determine the desired alignment.  */
637 #ifdef ROUND_TYPE_ALIGN
638   TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), union_align);
639 #else
640   TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
641 #endif
642
643 #ifdef ROUND_TYPE_SIZE
644   TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
645 #else
646   /* Round the size up to be a multiple of the required alignment */
647   TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
648 #endif
649 }
650 \f
651 /* Calculate the mode, size, and alignment for TYPE.
652    For an array type, calculate the element separation as well.
653    Record TYPE on the chain of permanent or temporary types
654    so that dbxout will find out about it.
655
656    TYPE_SIZE of a type is nonzero if the type has been laid out already.
657    layout_type does nothing on such a type.
658
659    If the type is incomplete, its TYPE_SIZE remains zero.  */
660
661 void
662 layout_type (type)
663      tree type;
664 {
665   int old;
666   tree pending_statics;
667
668   if (type == 0)
669     abort ();
670
671   /* Do nothing if type has been laid out before.  */
672   if (TYPE_SIZE (type))
673     return;
674
675   /* Make sure all nodes we allocate are not momentary;
676      they must last past the current statement.  */
677   old = suspend_momentary ();
678
679   /* If we are processing a permanent type, make nodes permanent.
680      If processing a temporary type, make it saveable, since the
681      type node itself is.  This is important if the function is inline,
682      since its decls will get copied later.  */
683   push_obstacks_nochange ();
684   if (allocation_temporary_p ())
685     {
686       if (TREE_PERMANENT (type))
687         end_temporary_allocation ();
688       else
689         saveable_allocation ();
690     }
691
692   switch (TREE_CODE (type))
693     {
694     case LANG_TYPE:
695       /* This kind of type is the responsibility
696          of the languge-specific code.  */
697       abort ();
698
699     case INTEGER_TYPE:
700     case ENUMERAL_TYPE:
701       if (TREE_INT_CST_HIGH (TYPE_MIN_VALUE (type)) >= 0)
702         TREE_UNSIGNED (type) = 1;
703
704       TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
705                                                  MODE_INT);
706       TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
707       break;
708
709     case REAL_TYPE:
710       TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
711       TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
712       break;
713
714     case COMPLEX_TYPE:
715       TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
716       TYPE_MODE (type)
717         = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
718                          (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
719                           ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
720                          0);
721       TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
722       break;
723
724     case VOID_TYPE:
725       TYPE_SIZE (type) = size_zero_node;
726       TYPE_ALIGN (type) = 1;
727       TYPE_MODE (type) = VOIDmode;
728       break;
729
730     case OFFSET_TYPE:
731       TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (Pmode));
732       TYPE_MODE (type) = Pmode;
733       break;
734
735     case FUNCTION_TYPE:
736     case METHOD_TYPE:
737       TYPE_MODE (type) = mode_for_size (2 * GET_MODE_BITSIZE (Pmode),
738                                         MODE_INT, 0);
739       TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
740       break;
741
742     case POINTER_TYPE:
743     case REFERENCE_TYPE:
744       TYPE_MODE (type) = Pmode;
745       TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
746       TREE_UNSIGNED (type) = 1;
747       TYPE_PRECISION (type) = GET_MODE_BITSIZE (TYPE_MODE (type));
748       break;
749
750     case ARRAY_TYPE:
751       {
752         register tree index = TYPE_DOMAIN (type);
753         register tree element = TREE_TYPE (type);
754
755         build_pointer_type (element);
756
757         /* We need to know both bounds in order to compute the size.  */
758         if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
759             && TYPE_SIZE (element))
760           {
761             tree length
762               = size_binop (PLUS_EXPR, size_one_node,
763                             size_binop (MINUS_EXPR, TYPE_MAX_VALUE (index),
764                                         TYPE_MIN_VALUE (index)));
765
766             TYPE_SIZE (type) = size_binop (MULT_EXPR, length,
767                                            TYPE_SIZE (element));
768           }
769
770         /* Now round the alignment and size,
771            using machine-dependent criteria if any.  */
772
773 #ifdef ROUND_TYPE_ALIGN
774         TYPE_ALIGN (type)
775           = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
776 #else
777         TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
778 #endif
779
780 #ifdef ROUND_TYPE_SIZE
781         if (TYPE_SIZE (type) != 0)
782           TYPE_SIZE (type)
783             = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
784 #endif
785
786         TYPE_MODE (type) = BLKmode;
787         if (TYPE_SIZE (type) != 0
788             && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
789             /* BLKmode elements force BLKmode aggregate;
790                else extract/store fields may lose.  */
791             && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
792                 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
793           {
794             TYPE_MODE (type)
795               = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
796                                MODE_INT, 1);
797
798             if (STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
799                 && TYPE_ALIGN (type) < TREE_INT_CST_LOW (TYPE_SIZE (type))
800                 && TYPE_MODE (type) != BLKmode)
801               {
802                 TYPE_NO_FORCE_BLK (type) = 1;
803                 TYPE_MODE (type) = BLKmode;
804               }
805           }
806         break;
807       }
808
809     case RECORD_TYPE:
810       pending_statics = layout_record (type);
811       TYPE_MODE (type) = BLKmode;
812       if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
813         {
814           tree field;
815           /* A record which has any BLKmode members must itself be BLKmode;
816              it can't go in a register.
817              Unless the member is BLKmode only because it isn't aligned.  */
818           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
819             {
820               int bitpos;
821
822               if (TREE_CODE (field) != FIELD_DECL)
823                 continue;
824
825               if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
826                   && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
827                 goto record_lose;
828
829               if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
830                 goto record_lose;
831
832               bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
833
834               /* Must be BLKmode if any field crosses a word boundary,
835                  since extract_bit_field can't handle that in registers.  */
836               if (bitpos / BITS_PER_WORD
837                   != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
838                       / BITS_PER_WORD)
839                   /* But there is no problem if the field is entire words.  */
840                   && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD == 0)
841                 goto record_lose;
842             }
843
844           TYPE_MODE (type)
845             = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
846                              MODE_INT, 1);
847
848           /* If structure's known alignment is less than
849              what the scalar mode would need, and it matters,
850              then stick with BLKmode.  */
851           if (STRICT_ALIGNMENT
852               && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
853                     || (TYPE_ALIGN (type)
854                         >= TREE_INT_CST_LOW (TYPE_SIZE (type)))))
855             {
856               if (TYPE_MODE (type) != BLKmode)
857                 /* If this is the only reason this type is BLKmode,
858                    then don't force containing types to be BLKmode.  */
859                 TYPE_NO_FORCE_BLK (type) = 1;
860               TYPE_MODE (type) = BLKmode;
861             }
862
863         record_lose: ;
864         }
865
866       /* Lay out any static members.  This is done now
867          because their type may use the record's type.  */
868       while (pending_statics)
869         {
870           layout_decl (TREE_VALUE (pending_statics), 0);
871           pending_statics = TREE_CHAIN (pending_statics);
872         }
873       break;
874
875     case UNION_TYPE:
876     case QUAL_UNION_TYPE:
877       layout_union (type);
878       TYPE_MODE (type) = BLKmode;
879       if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
880           /* If structure's known alignment is less than
881              what the scalar mode would need, and it matters,
882              then stick with BLKmode.  */
883           && (! STRICT_ALIGNMENT
884               || TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
885               || TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type))))
886         {
887           tree field;
888           /* A union which has any BLKmode members must itself be BLKmode;
889              it can't go in a register.
890              Unless the member is BLKmode only because it isn't aligned.  */
891           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
892             {
893               if (TREE_CODE (field) != FIELD_DECL)
894                 continue;
895
896               if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
897                   && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
898                 goto union_lose;
899             }
900
901           TYPE_MODE (type)
902             = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
903                              MODE_INT, 1);
904
905         union_lose: ;
906         }
907       break;
908
909     /* Pascal types */
910     case BOOLEAN_TYPE:           /* store one byte/boolean for now. */
911       TYPE_MODE (type) = QImode;
912       TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
913       TYPE_PRECISION (type) = 1;
914       TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
915       break;
916
917     case CHAR_TYPE:
918       TYPE_MODE (type) = QImode;
919       TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
920       TYPE_PRECISION (type) = GET_MODE_BITSIZE (TYPE_MODE (type));
921       TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
922       break;
923
924     case FILE_TYPE:
925       /* The size may vary in different languages, so the language front end
926          should fill in the size.  */
927       TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
928       TYPE_MODE  (type) = BLKmode;
929       break;
930
931     default:
932       abort ();
933     } /* end switch */
934
935   /* Normally, use the alignment corresponding to the mode chosen.
936      However, where strict alignment is not required, avoid
937      over-aligning structures, since most compilers do not do this
938      alignment.  */
939
940   if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
941       && (STRICT_ALIGNMENT
942           || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
943               && TREE_CODE (type) != QUAL_UNION_TYPE
944               && TREE_CODE (type) != ARRAY_TYPE)))
945     TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
946
947   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
948   if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
949     TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
950
951   /* Also layout any other variants of the type.  */
952   if (TYPE_NEXT_VARIANT (type)
953       || type != TYPE_MAIN_VARIANT (type))
954     {
955       tree variant;
956       /* Record layout info of this variant.  */
957       tree size = TYPE_SIZE (type);
958       int align = TYPE_ALIGN (type);
959       enum machine_mode mode = TYPE_MODE (type);
960
961       /* Copy it into all variants.  */
962       for (variant = TYPE_MAIN_VARIANT (type);
963            variant;
964            variant = TYPE_NEXT_VARIANT (variant))
965         {
966           TYPE_SIZE (variant) = size;
967           TYPE_ALIGN (variant) = align;
968           TYPE_MODE (variant) = mode;
969         }
970     }
971         
972   pop_obstacks ();
973   resume_momentary (old);
974 }
975 \f
976 /* Create and return a type for signed integers of PRECISION bits.  */
977
978 tree
979 make_signed_type (precision)
980      int precision;
981 {
982   register tree type = make_node (INTEGER_TYPE);
983
984   TYPE_PRECISION (type) = precision;
985
986   /* Create the extreme values based on the number of bits.  */
987
988   TYPE_MIN_VALUE (type)
989     = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
990                     ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
991                    (((HOST_WIDE_INT) (-1)
992                      << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
993                          ? precision - HOST_BITS_PER_WIDE_INT - 1
994                          : 0))));
995   TYPE_MAX_VALUE (type)
996     = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
997                     ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
998                    (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
999                     ? (((HOST_WIDE_INT) 1
1000                         << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1001                     : 0));
1002
1003   /* Give this type's extreme values this type as their type.  */
1004
1005   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1006   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1007
1008   /* The first type made with this or `make_unsigned_type'
1009      is the type for size values.  */
1010
1011   if (sizetype == 0)
1012     {
1013       sizetype = type;
1014     }
1015
1016   /* Lay out the type: set its alignment, size, etc.  */
1017
1018   layout_type (type);
1019
1020   return type;
1021 }
1022
1023 /* Create and return a type for unsigned integers of PRECISION bits.  */
1024
1025 tree
1026 make_unsigned_type (precision)
1027      int precision;
1028 {
1029   register tree type = make_node (INTEGER_TYPE);
1030
1031   TYPE_PRECISION (type) = precision;
1032
1033   /* The first type made with this or `make_signed_type'
1034      is the type for size values.  */
1035
1036   if (sizetype == 0)
1037     {
1038       sizetype = type;
1039     }
1040
1041   fixup_unsigned_type (type);
1042   return type;
1043 }
1044
1045 /* Set the extreme values of TYPE based on its precision in bits,
1046    then lay it out.  Used when make_signed_type won't do
1047    because the tree code is not INTEGER_TYPE.
1048    E.g. for Pascal, when the -fsigned-char option is given.  */
1049
1050 void
1051 fixup_signed_type (type)
1052      tree type;
1053 {
1054   register int precision = TYPE_PRECISION (type);
1055
1056   TYPE_MIN_VALUE (type)
1057     = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1058                     ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1059                    (((HOST_WIDE_INT) (-1)
1060                      << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1061                          ? precision - HOST_BITS_PER_WIDE_INT - 1
1062                          : 0))));
1063   TYPE_MAX_VALUE (type)
1064     = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1065                     ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1066                    (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1067                     ? (((HOST_WIDE_INT) 1
1068                         << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1069                     : 0));
1070
1071   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1072   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1073
1074   /* Lay out the type: set its alignment, size, etc.  */
1075
1076   layout_type (type);
1077 }
1078
1079 /* Set the extreme values of TYPE based on its precision in bits,
1080    then lay it out.  This is used both in `make_unsigned_type'
1081    and for enumeral types.  */
1082
1083 void
1084 fixup_unsigned_type (type)
1085      tree type;
1086 {
1087   register int precision = TYPE_PRECISION (type);
1088
1089   TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1090   TYPE_MAX_VALUE (type)
1091     = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1092                    ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1093                    precision - HOST_BITS_PER_WIDE_INT > 0
1094                    ? ((unsigned HOST_WIDE_INT) ~0
1095                       >> (HOST_BITS_PER_WIDE_INT
1096                           - (precision - HOST_BITS_PER_WIDE_INT)))
1097                    : 0);
1098   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1099   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1100
1101   /* Lay out the type: set its alignment, size, etc.  */
1102
1103   layout_type (type);
1104 }
1105 \f
1106 /* Find the best machine mode to use when referencing a bit field of length
1107    BITSIZE bits starting at BITPOS.
1108
1109    The underlying object is known to be aligned to a boundary of ALIGN bits.
1110    If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1111    larger than LARGEST_MODE (usually SImode).
1112
1113    If no mode meets all these conditions, we return VOIDmode.  Otherwise, if
1114    VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1115    mode meeting these conditions.
1116
1117    Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1118    the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1119    all the conditions.  */
1120
1121 enum machine_mode
1122 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1123      int bitsize, bitpos;
1124      int align;
1125      enum machine_mode largest_mode;
1126      int volatilep;
1127 {
1128   enum machine_mode mode;
1129   int unit;
1130
1131   /* Find the narrowest integer mode that contains the bit field.  */
1132   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1133        mode = GET_MODE_WIDER_MODE (mode))
1134     {
1135       unit = GET_MODE_BITSIZE (mode);
1136       if (bitpos / unit == (bitpos + bitsize - 1) / unit)
1137         break;
1138     }
1139
1140   if (mode == MAX_MACHINE_MODE
1141       /* It is tempting to omit the following line
1142          if STRICT_ALIGNMENT is true.
1143          But that is incorrect, since if the bitfield uses part of 3 bytes
1144          and we use a 4-byte mode, we could get a spurious segv
1145          if the extra 4th byte is past the end of memory.
1146          (Though at least one Unix compiler ignores this problem:
1147          that on the Sequent 386 machine.  */
1148       || MIN (unit, BIGGEST_ALIGNMENT) > align
1149       || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1150     return VOIDmode;
1151
1152   if (SLOW_BYTE_ACCESS && ! volatilep)
1153     {
1154       enum machine_mode wide_mode = VOIDmode, tmode;
1155
1156       for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1157            tmode = GET_MODE_WIDER_MODE (tmode))
1158         {
1159           unit = GET_MODE_BITSIZE (tmode);
1160           if (bitpos / unit == (bitpos + bitsize - 1) / unit
1161               && unit <= BITS_PER_WORD
1162               && unit <= MIN (align, BIGGEST_ALIGNMENT)
1163               && (largest_mode == VOIDmode
1164                   || unit <= GET_MODE_BITSIZE (largest_mode)))
1165             wide_mode = tmode;
1166         }
1167
1168       if (wide_mode != VOIDmode)
1169         return wide_mode;
1170     }
1171
1172   return mode;
1173 }
1174 \f
1175 /* Save all variables describing the current status into the structure *P.
1176    This is used before starting a nested function.  */
1177
1178 void
1179 save_storage_status (p)
1180      struct function *p;
1181 {
1182 #if 0  /* Need not save, since always 0 and non0 (resp.) within a function.  */
1183   p->pending_sizes = pending_sizes;
1184   p->immediate_size_expand = immediate_size_expand;
1185 #endif /* 0 */
1186 }
1187
1188 /* Restore all variables describing the current status from the structure *P.
1189    This is used after a nested function.  */
1190
1191 void
1192 restore_storage_status (p)
1193      struct function *p;
1194 {
1195 #if 0
1196   pending_sizes = p->pending_sizes;
1197   immediate_size_expand = p->immediate_size_expand;
1198 #endif /* 0 */
1199 }