OSDN Git Service

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