OSDN Git Service

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