OSDN Git Service

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