OSDN Git Service

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