OSDN Git Service

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