OSDN Git Service

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