OSDN Git Service

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