OSDN Git Service

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