OSDN Git Service

* c-decl.c (struct language_function): Renamed from struct c_function.
[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                   || TREE_CODE (TREE_TYPE (field)) == ERROR_MARK)
941                 continue;
942
943               if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
944                   && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
945                 goto record_lose;
946
947               if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
948                 goto record_lose;
949
950               bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
951
952               /* Must be BLKmode if any field crosses a word boundary,
953                  since extract_bit_field can't handle that in registers.  */
954               if (bitpos / BITS_PER_WORD
955                   != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
956                       / BITS_PER_WORD)
957                   /* But there is no problem if the field is entire words.  */
958                   && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD != 0)
959                 goto record_lose;
960
961               /* If this field is the whole struct, remember its mode so
962                  that, say, we can put a double in a class into a DF
963                  register instead of forcing it to live in the stack.  */
964               if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
965                 mode = DECL_MODE (field);
966             }
967
968           if (mode != VOIDmode)
969             /* We only have one real field; use its mode.  */
970             TYPE_MODE (type) = mode;
971           else
972             TYPE_MODE (type)
973               = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
974                                MODE_INT, 1);
975
976           /* If structure's known alignment is less than
977              what the scalar mode would need, and it matters,
978              then stick with BLKmode.  */
979           if (STRICT_ALIGNMENT
980               && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
981                     || ((int)TYPE_ALIGN (type)
982                         >= TREE_INT_CST_LOW (TYPE_SIZE (type)))))
983             {
984               if (TYPE_MODE (type) != BLKmode)
985                 /* If this is the only reason this type is BLKmode,
986                    then don't force containing types to be BLKmode.  */
987                 TYPE_NO_FORCE_BLK (type) = 1;
988               TYPE_MODE (type) = BLKmode;
989             }
990
991         record_lose: ;
992         }
993
994       /* Lay out any static members.  This is done now
995          because their type may use the record's type.  */
996       while (pending_statics)
997         {
998           layout_decl (TREE_VALUE (pending_statics), 0);
999           pending_statics = TREE_CHAIN (pending_statics);
1000         }
1001       break;
1002
1003     case UNION_TYPE:
1004     case QUAL_UNION_TYPE:
1005       layout_union (type);
1006       TYPE_MODE (type) = BLKmode;
1007       if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
1008           /* If structure's known alignment is less than
1009              what the scalar mode would need, and it matters,
1010              then stick with BLKmode.  */
1011           && (! STRICT_ALIGNMENT
1012               || TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1013               || (int)TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type))))
1014         {
1015           tree field;
1016           /* A union which has any BLKmode members must itself be BLKmode;
1017              it can't go in a register.
1018              Unless the member is BLKmode only because it isn't aligned.  */
1019           for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1020             {
1021               if (TREE_CODE (field) != FIELD_DECL)
1022                 continue;
1023
1024               if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1025                   && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1026                 goto union_lose;
1027             }
1028
1029           TYPE_MODE (type)
1030             = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
1031                              MODE_INT, 1);
1032
1033         union_lose: ;
1034         }
1035       break;
1036
1037     case SET_TYPE:  /* Used by Chill and Pascal. */
1038       if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1039           || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1040         abort();
1041       else
1042         {
1043 #ifndef SET_WORD_SIZE
1044 #define SET_WORD_SIZE BITS_PER_WORD
1045 #endif
1046           int alignment = set_alignment ? set_alignment : SET_WORD_SIZE;
1047           int size_in_bits
1048             = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1049                - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
1050           int rounded_size
1051             = ((size_in_bits + alignment - 1) / alignment) * alignment;
1052           if (rounded_size > alignment)
1053             TYPE_MODE (type) = BLKmode;
1054           else
1055             TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1056           TYPE_SIZE (type) = bitsize_int (rounded_size, 0L);
1057           TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1058           TYPE_ALIGN (type) = alignment;
1059           TYPE_PRECISION (type) = size_in_bits;
1060         }
1061       break;
1062
1063     case FILE_TYPE:
1064       /* The size may vary in different languages, so the language front end
1065          should fill in the size.  */
1066       TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1067       TYPE_MODE  (type) = BLKmode;
1068       break;
1069
1070     default:
1071       abort ();
1072     } /* end switch */
1073
1074   /* Normally, use the alignment corresponding to the mode chosen.
1075      However, where strict alignment is not required, avoid
1076      over-aligning structures, since most compilers do not do this
1077      alignment.  */
1078
1079   if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1080       && (STRICT_ALIGNMENT
1081           || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1082               && TREE_CODE (type) != QUAL_UNION_TYPE
1083               && TREE_CODE (type) != ARRAY_TYPE)))
1084     TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1085
1086   /* Do machine-dependent extra alignment.  */
1087 #ifdef ROUND_TYPE_ALIGN
1088   TYPE_ALIGN (type)
1089     = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1090 #endif
1091
1092 #ifdef ROUND_TYPE_SIZE
1093   if (TYPE_SIZE (type) != 0)
1094     TYPE_SIZE (type)
1095       = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1096 #endif
1097
1098   /* Evaluate nonconstant size only once, either now or as soon as safe.  */
1099   if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1100     TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1101
1102   /* If we failed to find a simple way to calculate the unit size
1103      of the type above, find it by division.  */
1104   if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1105     {
1106       TYPE_SIZE_UNIT (type) = size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1107                                           size_int (BITS_PER_UNIT));
1108     }
1109
1110   /* Once again evaluate only once, either now or as soon as safe.  */
1111   if (TYPE_SIZE_UNIT (type) != 0
1112       && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1113     TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1114
1115   /* Also layout any other variants of the type.  */
1116   if (TYPE_NEXT_VARIANT (type)
1117       || type != TYPE_MAIN_VARIANT (type))
1118     {
1119       tree variant;
1120       /* Record layout info of this variant.  */
1121       tree size = TYPE_SIZE (type);
1122       tree size_unit = TYPE_SIZE_UNIT (type);
1123       int align = TYPE_ALIGN (type);
1124       enum machine_mode mode = TYPE_MODE (type);
1125
1126       /* Copy it into all variants.  */
1127       for (variant = TYPE_MAIN_VARIANT (type);
1128            variant;
1129            variant = TYPE_NEXT_VARIANT (variant))
1130         {
1131           TYPE_SIZE (variant) = size;
1132           TYPE_SIZE_UNIT (variant) = size_unit;
1133           TYPE_ALIGN (variant) = align;
1134           TYPE_MODE (variant) = mode;
1135         }
1136     }
1137         
1138   pop_obstacks ();
1139   resume_momentary (old);
1140 }
1141 \f
1142 /* Create and return a type for signed integers of PRECISION bits.  */
1143
1144 tree
1145 make_signed_type (precision)
1146      int precision;
1147 {
1148   register tree type = make_node (INTEGER_TYPE);
1149
1150   TYPE_PRECISION (type) = precision;
1151
1152   /* Create the extreme values based on the number of bits.  */
1153
1154   TYPE_MIN_VALUE (type)
1155     = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1156                     ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1157                    (((HOST_WIDE_INT) (-1)
1158                      << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1159                          ? precision - HOST_BITS_PER_WIDE_INT - 1
1160                          : 0))));
1161   TYPE_MAX_VALUE (type)
1162     = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1163                     ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1164                    (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1165                     ? (((HOST_WIDE_INT) 1
1166                         << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1167                     : 0));
1168
1169   /* Give this type's extreme values this type as their type.  */
1170
1171   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1172   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1173
1174   /* The first type made with this or `make_unsigned_type'
1175      is the type for size values.  */
1176
1177   if (sizetype == 0)
1178     set_sizetype (type);
1179
1180   /* Lay out the type: set its alignment, size, etc.  */
1181
1182   layout_type (type);
1183
1184   return type;
1185 }
1186
1187 /* Create and return a type for unsigned integers of PRECISION bits.  */
1188
1189 tree
1190 make_unsigned_type (precision)
1191      int precision;
1192 {
1193   register tree type = make_node (INTEGER_TYPE);
1194
1195   TYPE_PRECISION (type) = precision;
1196
1197   /* The first type made with this or `make_signed_type'
1198      is the type for size values.  */
1199
1200   if (sizetype == 0)
1201     {
1202       TREE_UNSIGNED (type) = 1;
1203       set_sizetype (type);
1204     }
1205
1206   fixup_unsigned_type (type);
1207   return type;
1208 }
1209
1210 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1211    Also update the type of any standard type's sizes made so far.  */
1212
1213 void
1214 set_sizetype (type)
1215      tree type;
1216 {
1217   int oprecision = TYPE_PRECISION (type), precision;
1218
1219   sizetype = type;
1220
1221   /* The *bitsizetype types use a precision that avoids overflows when
1222      calculating signed sizes / offsets in bits.
1223
1224      We are allocating bitsizetype once and change it in place when
1225      we decide later that we want to change it.  This way, we avoid the
1226      hassle of changing all the TYPE_SIZE (TREE_TYPE (sometype))
1227      individually in each front end.  */
1228   if (! bitsizetype)
1229     bitsizetype = make_node (INTEGER_TYPE);
1230   if (TYPE_NAME (sizetype) && ! TYPE_NAME (bitsizetype))
1231     TYPE_NAME (bitsizetype) = TYPE_NAME (sizetype);
1232
1233   precision = oprecision + BITS_PER_UNIT_LOG + 1;
1234   /* However, when cross-compiling from a 32 bit to a 64 bit host,
1235      we are limited to 64 bit precision.  */
1236   if (precision > 2 * HOST_BITS_PER_WIDE_INT)
1237     precision = 2 * HOST_BITS_PER_WIDE_INT;
1238   TYPE_PRECISION (bitsizetype) = precision;
1239   if (TREE_UNSIGNED (type))
1240     fixup_unsigned_type (bitsizetype);
1241   else
1242     fixup_signed_type (bitsizetype);
1243   layout_type (bitsizetype);
1244
1245   if (TREE_UNSIGNED (type))
1246     {
1247       usizetype = sizetype;
1248       ubitsizetype = bitsizetype;
1249       ssizetype = make_signed_type (oprecision);
1250       sbitsizetype = make_signed_type (precision);
1251     }
1252   else
1253     {
1254       ssizetype = sizetype;
1255       sbitsizetype = bitsizetype;
1256       usizetype = make_unsigned_type (oprecision);
1257       ubitsizetype = make_unsigned_type (precision);
1258     }
1259 }
1260
1261 /* Set the extreme values of TYPE based on its precision in bits,
1262    then lay it out.  Used when make_signed_type won't do
1263    because the tree code is not INTEGER_TYPE.
1264    E.g. for Pascal, when the -fsigned-char option is given.  */
1265
1266 void
1267 fixup_signed_type (type)
1268      tree type;
1269 {
1270   register int precision = TYPE_PRECISION (type);
1271
1272   TYPE_MIN_VALUE (type)
1273     = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1274                     ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1275                    (((HOST_WIDE_INT) (-1)
1276                      << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1277                          ? precision - HOST_BITS_PER_WIDE_INT - 1
1278                          : 0))));
1279   TYPE_MAX_VALUE (type)
1280     = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1281                     ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1282                    (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1283                     ? (((HOST_WIDE_INT) 1
1284                         << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1285                     : 0));
1286
1287   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1288   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1289
1290   /* Lay out the type: set its alignment, size, etc.  */
1291
1292   layout_type (type);
1293 }
1294
1295 /* Set the extreme values of TYPE based on its precision in bits,
1296    then lay it out.  This is used both in `make_unsigned_type'
1297    and for enumeral types.  */
1298
1299 void
1300 fixup_unsigned_type (type)
1301      tree type;
1302 {
1303   register int precision = TYPE_PRECISION (type);
1304
1305   TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1306   TYPE_MAX_VALUE (type)
1307     = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1308                    ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1309                    precision - HOST_BITS_PER_WIDE_INT > 0
1310                    ? ((unsigned HOST_WIDE_INT) ~0
1311                       >> (HOST_BITS_PER_WIDE_INT
1312                           - (precision - HOST_BITS_PER_WIDE_INT)))
1313                    : 0);
1314   TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1315   TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1316
1317   /* Lay out the type: set its alignment, size, etc.  */
1318
1319   layout_type (type);
1320 }
1321 \f
1322 /* Find the best machine mode to use when referencing a bit field of length
1323    BITSIZE bits starting at BITPOS.
1324
1325    The underlying object is known to be aligned to a boundary of ALIGN bits.
1326    If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1327    larger than LARGEST_MODE (usually SImode).
1328
1329    If no mode meets all these conditions, we return VOIDmode.  Otherwise, if
1330    VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1331    mode meeting these conditions.
1332
1333    Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1334    the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1335    all the conditions.  */
1336
1337 enum machine_mode
1338 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1339      int bitsize, bitpos;
1340      int align;
1341      enum machine_mode largest_mode;
1342      int volatilep;
1343 {
1344   enum machine_mode mode;
1345   int unit = 0;
1346
1347   /* Find the narrowest integer mode that contains the bit field.  */
1348   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1349        mode = GET_MODE_WIDER_MODE (mode))
1350     {
1351       unit = GET_MODE_BITSIZE (mode);
1352       if ((bitpos % unit) + bitsize <= unit)
1353         break;
1354     }
1355
1356   if (mode == VOIDmode
1357       /* It is tempting to omit the following line
1358          if STRICT_ALIGNMENT is true.
1359          But that is incorrect, since if the bitfield uses part of 3 bytes
1360          and we use a 4-byte mode, we could get a spurious segv
1361          if the extra 4th byte is past the end of memory.
1362          (Though at least one Unix compiler ignores this problem:
1363          that on the Sequent 386 machine.  */
1364       || MIN (unit, BIGGEST_ALIGNMENT) > align
1365       || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1366     return VOIDmode;
1367
1368   if (SLOW_BYTE_ACCESS && ! volatilep)
1369     {
1370       enum machine_mode wide_mode = VOIDmode, tmode;
1371
1372       for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1373            tmode = GET_MODE_WIDER_MODE (tmode))
1374         {
1375           unit = GET_MODE_BITSIZE (tmode);
1376           if (bitpos / unit == (bitpos + bitsize - 1) / unit
1377               && unit <= BITS_PER_WORD
1378               && unit <= MIN (align, BIGGEST_ALIGNMENT)
1379               && (largest_mode == VOIDmode
1380                   || unit <= GET_MODE_BITSIZE (largest_mode)))
1381             wide_mode = tmode;
1382         }
1383
1384       if (wide_mode != VOIDmode)
1385         return wide_mode;
1386     }
1387
1388   return mode;
1389 }