Initial revision
[official-gcc.git] / gcc / stor-layout.c
bloba3c4f5c265a49a7fa1fbf1dc8d1c7f69037b68a8
1 /* C-compiler utilities for types and variables storage layout
2 Copyright (C) 1987, 1988, 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GNU CC.
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.
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.
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. */
21 #include "config.h"
22 #include <stdio.h>
24 #include "tree.h"
25 #include "function.h"
27 #define CEIL(x,y) (((x) + (y) - 1) / (y))
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. */
33 tree sizetype;
35 /* An integer constant with value 0 whose type is sizetype. */
37 tree size_zero_node;
39 /* An integer constant with value 1 whose type is sizetype. */
41 tree size_one_node;
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;
47 /* If non-zero, the alignment of a bitsting or (power-)set value, in bits.
48 May be overridden by front-ends. */
49 int set_alignment = 0;
51 #define GET_MODE_ALIGNMENT(MODE) \
52 MIN (BIGGEST_ALIGNMENT, \
53 MAX (1, (GET_MODE_UNIT_SIZE (MODE) * BITS_PER_UNIT)))
55 static enum machine_mode smallest_mode_for_size PROTO((unsigned int,
56 enum mode_class));
57 static tree layout_record PROTO((tree));
58 static void layout_union PROTO((tree));
60 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
62 static tree pending_sizes;
64 /* Nonzero means cannot safely call expand_expr now,
65 so put variable sizes onto `pending_sizes' instead. */
67 int immediate_size_expand;
69 tree
70 get_pending_sizes ()
72 tree chain = pending_sizes;
73 tree t;
75 /* Put each SAVE_EXPR into the current function. */
76 for (t = chain; t; t = TREE_CHAIN (t))
77 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
78 pending_sizes = 0;
79 return chain;
82 /* Given a size SIZE that isn't constant, return a SAVE_EXPR
83 to serve as the actual size-expression for a type or decl. */
85 tree
86 variable_size (size)
87 tree size;
89 /* If the language-processor is to take responsibility for variable-sized
90 items (e.g., languages which have elaboration procedures like Ada),
91 just return SIZE unchanged. Likewise for self-referential sizes. */
92 if (global_bindings_p () < 0 || contains_placeholder_p (size))
93 return size;
95 size = save_expr (size);
97 if (global_bindings_p ())
99 if (TREE_CONSTANT (size))
100 error ("type size can't be explicitly evaluated");
101 else
102 error ("variable-size type declared outside of any function");
104 return size_int (1);
107 if (immediate_size_expand)
108 /* NULL_RTX is not defined; neither is the rtx type.
109 Also, we would like to pass const0_rtx here, but don't have it. */
110 expand_expr (size, expand_expr (integer_zero_node, NULL_PTR, VOIDmode, 0),
111 VOIDmode, 0);
112 else
113 pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
115 return size;
118 #ifndef MAX_FIXED_MODE_SIZE
119 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
120 #endif
122 /* Return the machine mode to use for a nonscalar of SIZE bits.
123 The mode must be in class CLASS, and have exactly that many bits.
124 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
125 be used. */
127 enum machine_mode
128 mode_for_size (size, class, limit)
129 unsigned int size;
130 enum mode_class class;
131 int limit;
133 register enum machine_mode mode;
135 if (limit && size > MAX_FIXED_MODE_SIZE)
136 return BLKmode;
138 /* Get the first mode which has this size, in the specified class. */
139 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
140 mode = GET_MODE_WIDER_MODE (mode))
141 if (GET_MODE_BITSIZE (mode) == size)
142 return mode;
144 return BLKmode;
147 /* Similar, but never return BLKmode; return the narrowest mode that
148 contains at least the requested number of bits. */
150 static enum machine_mode
151 smallest_mode_for_size (size, class)
152 unsigned int size;
153 enum mode_class class;
155 register enum machine_mode mode;
157 /* Get the first mode which has at least this size, in the
158 specified class. */
159 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
160 mode = GET_MODE_WIDER_MODE (mode))
161 if (GET_MODE_BITSIZE (mode) >= size)
162 return mode;
164 abort ();
167 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
169 tree
170 round_up (value, divisor)
171 tree value;
172 int divisor;
174 return size_binop (MULT_EXPR,
175 size_binop (CEIL_DIV_EXPR, value, size_int (divisor)),
176 size_int (divisor));
179 /* Set the size, mode and alignment of a ..._DECL node.
180 TYPE_DECL does need this for C++.
181 Note that LABEL_DECL and CONST_DECL nodes do not need this,
182 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
183 Don't call layout_decl for them.
185 KNOWN_ALIGN is the amount of alignment we can assume this
186 decl has with no special effort. It is relevant only for FIELD_DECLs
187 and depends on the previous fields.
188 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
189 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
190 the record will be aligned to suit. */
192 void
193 layout_decl (decl, known_align)
194 tree decl;
195 unsigned known_align;
197 register tree type = TREE_TYPE (decl);
198 register enum tree_code code = TREE_CODE (decl);
199 int spec_size = DECL_FIELD_SIZE (decl);
201 if (code == CONST_DECL)
202 return;
204 if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
205 && code != FIELD_DECL && code != TYPE_DECL)
206 abort ();
208 if (type == error_mark_node)
210 type = void_type_node;
211 spec_size = 0;
214 /* Usually the size and mode come from the data type without change. */
216 DECL_MODE (decl) = TYPE_MODE (type);
217 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
218 if (DECL_SIZE (decl) == 0)
219 DECL_SIZE (decl) = TYPE_SIZE (type);
221 if (code == FIELD_DECL && DECL_BIT_FIELD (decl))
223 if (spec_size == 0 && DECL_NAME (decl) != 0)
224 abort ();
226 /* Size is specified number of bits. */
227 DECL_SIZE (decl) = size_int (spec_size);
229 /* Force alignment required for the data type.
230 But if the decl itself wants greater alignment, don't override that.
231 Likewise, if the decl is packed, don't override it. */
232 else if (DECL_ALIGN (decl) == 0
233 || (! DECL_PACKED (decl) && TYPE_ALIGN (type) > DECL_ALIGN (decl)))
234 DECL_ALIGN (decl) = TYPE_ALIGN (type);
236 /* See if we can use an ordinary integer mode for a bit-field. */
237 /* Conditions are: a fixed size that is correct for another mode
238 and occupying a complete byte or bytes on proper boundary. */
239 if (code == FIELD_DECL)
241 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
242 if (maximum_field_alignment != 0)
243 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
246 if (DECL_BIT_FIELD (decl)
247 && TYPE_SIZE (type) != 0
248 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
250 register enum machine_mode xmode
251 = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1);
253 if (xmode != BLKmode
254 && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
256 DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
257 DECL_ALIGN (decl));
258 DECL_MODE (decl) = xmode;
259 DECL_SIZE (decl) = size_int (GET_MODE_BITSIZE (xmode));
260 /* This no longer needs to be accessed as a bit field. */
261 DECL_BIT_FIELD (decl) = 0;
265 /* Evaluate nonconstant size only once, either now or as soon as safe. */
266 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
267 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
270 /* Lay out a RECORD_TYPE type (a C struct).
271 This means laying out the fields, determining their positions,
272 and computing the overall size and required alignment of the record.
273 Note that if you set the TYPE_ALIGN before calling this
274 then the struct is aligned to at least that boundary.
276 If the type has basetypes, you must call layout_basetypes
277 before calling this function.
279 The return value is a list of static members of the record.
280 They still need to be laid out. */
282 static tree
283 layout_record (rec)
284 tree rec;
286 register tree field;
287 #ifdef STRUCTURE_SIZE_BOUNDARY
288 unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
289 #else
290 unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
291 #endif
292 /* These must be laid out *after* the record is. */
293 tree pending_statics = NULL_TREE;
294 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
295 where CONST_SIZE is an integer
296 and VAR_SIZE is a tree expression.
297 If VAR_SIZE is null, the size is just CONST_SIZE.
298 Naturally we try to avoid using VAR_SIZE. */
299 register int const_size = 0;
300 register tree var_size = 0;
301 /* Once we start using VAR_SIZE, this is the maximum alignment
302 that we know VAR_SIZE has. */
303 register int var_align = BITS_PER_UNIT;
306 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
308 register int known_align = var_size ? var_align : const_size;
309 register int desired_align;
311 /* If FIELD is static, then treat it like a separate variable,
312 not really like a structure field.
313 If it is a FUNCTION_DECL, it's a method.
314 In both cases, all we do is lay out the decl,
315 and we do it *after* the record is laid out. */
317 if (TREE_STATIC (field))
319 pending_statics = tree_cons (NULL_TREE, field, pending_statics);
320 continue;
322 /* Enumerators and enum types which are local to this class need not
323 be laid out. Likewise for initialized constant fields. */
324 if (TREE_CODE (field) != FIELD_DECL)
325 continue;
327 /* Lay out the field so we know what alignment it needs.
328 For a packed field, use the alignment as specified,
329 disregarding what the type would want. */
330 if (DECL_PACKED (field))
331 desired_align = DECL_ALIGN (field);
332 layout_decl (field, known_align);
333 if (! DECL_PACKED (field))
334 desired_align = DECL_ALIGN (field);
335 /* Some targets (i.e. VMS) limit struct field alignment
336 to a lower boundary than alignment of variables. */
337 #ifdef BIGGEST_FIELD_ALIGNMENT
338 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
339 #endif
341 /* Record must have at least as much alignment as any field.
342 Otherwise, the alignment of the field within the record
343 is meaningless. */
345 #ifndef PCC_BITFIELD_TYPE_MATTERS
346 record_align = MAX (record_align, desired_align);
347 #else
348 if (PCC_BITFIELD_TYPE_MATTERS && TREE_TYPE (field) != error_mark_node
349 && DECL_BIT_FIELD_TYPE (field)
350 && ! integer_zerop (TYPE_SIZE (TREE_TYPE (field))))
352 /* For these machines, a zero-length field does not
353 affect the alignment of the structure as a whole.
354 It does, however, affect the alignment of the next field
355 within the structure. */
356 if (! integer_zerop (DECL_SIZE (field)))
357 record_align = MAX (record_align, desired_align);
358 else if (! DECL_PACKED (field))
359 desired_align = TYPE_ALIGN (TREE_TYPE (field));
360 /* A named bit field of declared type `int'
361 forces the entire structure to have `int' alignment. */
362 if (DECL_NAME (field) != 0)
364 int type_align = TYPE_ALIGN (TREE_TYPE (field));
365 if (maximum_field_alignment != 0)
366 type_align = MIN (type_align, maximum_field_alignment);
368 record_align = MAX (record_align, type_align);
371 else
372 record_align = MAX (record_align, desired_align);
373 #endif
375 /* Does this field automatically have alignment it needs
376 by virtue of the fields that precede it and the record's
377 own alignment? */
379 if (const_size % desired_align != 0
380 || (var_align % desired_align != 0
381 && var_size != 0))
383 /* No, we need to skip space before this field.
384 Bump the cumulative size to multiple of field alignment. */
386 if (var_size == 0
387 || var_align % desired_align == 0)
388 const_size
389 = CEIL (const_size, desired_align) * desired_align;
390 else
392 if (const_size > 0)
393 var_size = size_binop (PLUS_EXPR, var_size,
394 size_int (const_size));
395 const_size = 0;
396 var_size = round_up (var_size, desired_align);
397 var_align = MIN (var_align, desired_align);
401 #ifdef PCC_BITFIELD_TYPE_MATTERS
402 if (PCC_BITFIELD_TYPE_MATTERS
403 && TREE_CODE (field) == FIELD_DECL
404 && TREE_TYPE (field) != error_mark_node
405 && DECL_BIT_FIELD_TYPE (field)
406 && !DECL_PACKED (field)
407 /* If #pragma pack is in effect, turn off this feature. */
408 && maximum_field_alignment == 0
409 && !integer_zerop (DECL_SIZE (field)))
411 int type_align = TYPE_ALIGN (TREE_TYPE (field));
412 register tree dsize = DECL_SIZE (field);
413 int field_size = TREE_INT_CST_LOW (dsize);
415 /* A bit field may not span the unit of alignment of its type.
416 Advance to next boundary if necessary. */
417 /* ??? There is some uncertainty here as to what
418 should be done if type_align is less than the width of the type.
419 That can happen because the width exceeds BIGGEST_ALIGNMENT
420 or because it exceeds maximum_field_alignment. */
421 if (const_size / type_align
422 != (const_size + field_size - 1) / type_align)
423 const_size = CEIL (const_size, type_align) * type_align;
425 #endif
427 /* No existing machine description uses this parameter.
428 So I have made it in this aspect identical to PCC_BITFIELD_TYPE_MATTERS. */
429 #ifdef BITFIELD_NBYTES_LIMITED
430 if (BITFIELD_NBYTES_LIMITED
431 && TREE_CODE (field) == FIELD_DECL
432 && TREE_TYPE (field) != error_mark_node
433 && DECL_BIT_FIELD_TYPE (field)
434 && !DECL_PACKED (field)
435 && !integer_zerop (DECL_SIZE (field)))
437 int type_align = TYPE_ALIGN (TREE_TYPE (field));
438 register tree dsize = DECL_SIZE (field);
439 int field_size = TREE_INT_CST_LOW (dsize);
441 if (maximum_field_alignment != 0)
442 type_align = MIN (type_align, maximum_field_alignment);
444 /* A bit field may not span the unit of alignment of its type.
445 Advance to next boundary if necessary. */
446 if (const_size / type_align
447 != (const_size + field_size - 1) / type_align)
448 const_size = CEIL (const_size, type_align) * type_align;
450 #endif
452 /* Size so far becomes the position of this field. */
454 if (var_size && const_size)
455 DECL_FIELD_BITPOS (field)
456 = size_binop (PLUS_EXPR, var_size, size_int (const_size));
457 else if (var_size)
458 DECL_FIELD_BITPOS (field) = var_size;
459 else
461 DECL_FIELD_BITPOS (field) = size_int (const_size);
463 /* If this field ended up more aligned than we thought it
464 would be (we approximate this by seeing if its position
465 changed), lay out the field again; perhaps we can use an
466 integral mode for it now. */
467 if (known_align != const_size)
468 layout_decl (field, const_size);
471 /* Now add size of this field to the size of the record. */
474 register tree dsize = DECL_SIZE (field);
476 /* This can happen when we have an invalid nested struct definition,
477 such as struct j { struct j { int i; } }. The error message is
478 printed in finish_struct. */
479 if (dsize == 0)
480 /* Do nothing. */;
481 else if (TREE_CODE (dsize) == INTEGER_CST
482 && TREE_INT_CST_HIGH (dsize) == 0
483 && TREE_INT_CST_LOW (dsize) + const_size > const_size)
484 /* Use const_size if there's no overflow. */
485 const_size += TREE_INT_CST_LOW (dsize);
486 else
488 if (var_size == 0)
489 var_size = dsize;
490 else
491 var_size = size_binop (PLUS_EXPR, var_size, dsize);
496 /* Work out the total size and alignment of the record
497 as one expression and store in the record type.
498 Round it up to a multiple of the record's alignment. */
500 if (var_size == 0)
502 TYPE_SIZE (rec) = size_int (const_size);
504 else
506 if (const_size)
507 var_size
508 = size_binop (PLUS_EXPR, var_size, size_int (const_size));
509 TYPE_SIZE (rec) = var_size;
512 /* Determine the desired alignment. */
513 #ifdef ROUND_TYPE_ALIGN
514 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), record_align);
515 #else
516 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), record_align);
517 #endif
519 #ifdef ROUND_TYPE_SIZE
520 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
521 #else
522 /* Round the size up to be a multiple of the required alignment */
523 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
524 #endif
526 return pending_statics;
529 /* Lay out a UNION_TYPE or QUAL_UNION_TYPE type.
530 Lay out all the fields, set their positions to zero,
531 and compute the size and alignment of the union (maximum of any field).
532 Note that if you set the TYPE_ALIGN before calling this
533 then the union align is aligned to at least that boundary. */
535 static void
536 layout_union (rec)
537 tree rec;
539 register tree field;
540 #ifdef STRUCTURE_SIZE_BOUNDARY
541 unsigned union_align = STRUCTURE_SIZE_BOUNDARY;
542 #else
543 unsigned union_align = BITS_PER_UNIT;
544 #endif
546 /* The size of the union, based on the fields scanned so far,
547 is max (CONST_SIZE, VAR_SIZE).
548 VAR_SIZE may be null; then CONST_SIZE by itself is the size. */
549 register int const_size = 0;
550 register tree var_size = 0;
552 /* If this is a QUAL_UNION_TYPE, we want to process the fields in
553 the reverse order in building the COND_EXPR that denotes its
554 size. We reverse them again later. */
555 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
556 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
558 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
560 /* Enums which are local to this class need not be laid out. */
561 if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
562 continue;
564 layout_decl (field, 0);
565 DECL_FIELD_BITPOS (field) = size_int (0);
567 /* Union must be at least as aligned as any field requires. */
569 union_align = MAX (union_align, DECL_ALIGN (field));
571 #ifdef PCC_BITFIELD_TYPE_MATTERS
572 /* On the m88000, a bit field of declare type `int'
573 forces the entire union to have `int' alignment. */
574 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
575 union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
576 #endif
578 if (TREE_CODE (rec) == UNION_TYPE)
580 /* Set union_size to max (decl_size, union_size).
581 There are more and less general ways to do this.
582 Use only CONST_SIZE unless forced to use VAR_SIZE. */
584 if (TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
585 const_size
586 = MAX (const_size, TREE_INT_CST_LOW (DECL_SIZE (field)));
587 else if (var_size == 0)
588 var_size = DECL_SIZE (field);
589 else
590 var_size = size_binop (MAX_EXPR, var_size, DECL_SIZE (field));
592 else if (TREE_CODE (rec) == QUAL_UNION_TYPE)
593 var_size = fold (build (COND_EXPR, sizetype, DECL_QUALIFIER (field),
594 DECL_SIZE (field),
595 var_size ? var_size : integer_zero_node));
598 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
599 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
601 /* Determine the ultimate size of the union (in bytes). */
602 if (NULL == var_size)
603 TYPE_SIZE (rec) = size_int (CEIL (const_size, BITS_PER_UNIT)
604 * BITS_PER_UNIT);
605 else if (const_size == 0)
606 TYPE_SIZE (rec) = var_size;
607 else
608 TYPE_SIZE (rec) = size_binop (MAX_EXPR, var_size,
609 round_up (size_int (const_size),
610 BITS_PER_UNIT));
612 /* Determine the desired alignment. */
613 #ifdef ROUND_TYPE_ALIGN
614 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), union_align);
615 #else
616 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
617 #endif
619 #ifdef ROUND_TYPE_SIZE
620 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
621 #else
622 /* Round the size up to be a multiple of the required alignment */
623 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
624 #endif
627 /* Calculate the mode, size, and alignment for TYPE.
628 For an array type, calculate the element separation as well.
629 Record TYPE on the chain of permanent or temporary types
630 so that dbxout will find out about it.
632 TYPE_SIZE of a type is nonzero if the type has been laid out already.
633 layout_type does nothing on such a type.
635 If the type is incomplete, its TYPE_SIZE remains zero. */
637 void
638 layout_type (type)
639 tree type;
641 int old;
642 tree pending_statics;
644 if (type == 0)
645 abort ();
647 /* Do nothing if type has been laid out before. */
648 if (TYPE_SIZE (type))
649 return;
651 /* Make sure all nodes we allocate are not momentary;
652 they must last past the current statement. */
653 old = suspend_momentary ();
655 /* Put all our nodes into the same obstack as the type. Also,
656 make expressions saveable (this is a no-op for permanent types). */
658 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
659 saveable_allocation ();
661 switch (TREE_CODE (type))
663 case LANG_TYPE:
664 /* This kind of type is the responsibility
665 of the languge-specific code. */
666 abort ();
668 case INTEGER_TYPE:
669 case ENUMERAL_TYPE:
670 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
671 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
672 TREE_UNSIGNED (type) = 1;
674 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
675 MODE_INT);
676 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
677 break;
679 case REAL_TYPE:
680 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
681 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
682 break;
684 case COMPLEX_TYPE:
685 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
686 TYPE_MODE (type)
687 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
688 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
689 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
691 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
692 break;
694 case VOID_TYPE:
695 TYPE_SIZE (type) = size_zero_node;
696 TYPE_ALIGN (type) = 1;
697 TYPE_MODE (type) = VOIDmode;
698 break;
700 case OFFSET_TYPE:
701 TYPE_SIZE (type) = size_int (POINTER_SIZE);
702 TYPE_MODE (type) = ptr_mode;
703 break;
705 case FUNCTION_TYPE:
706 case METHOD_TYPE:
707 TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
708 TYPE_SIZE (type) = size_int (2 * POINTER_SIZE);
709 break;
711 case POINTER_TYPE:
712 case REFERENCE_TYPE:
713 TYPE_MODE (type) = ptr_mode;
714 TYPE_SIZE (type) = size_int (POINTER_SIZE);
715 TREE_UNSIGNED (type) = 1;
716 TYPE_PRECISION (type) = POINTER_SIZE;
717 break;
719 case ARRAY_TYPE:
721 register tree index = TYPE_DOMAIN (type);
722 register tree element = TREE_TYPE (type);
724 build_pointer_type (element);
726 /* We need to know both bounds in order to compute the size. */
727 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
728 && TYPE_SIZE (element))
730 tree length
731 = size_binop (PLUS_EXPR, size_one_node,
732 size_binop (MINUS_EXPR, TYPE_MAX_VALUE (index),
733 TYPE_MIN_VALUE (index)));
735 TYPE_SIZE (type) = size_binop (MULT_EXPR, length,
736 TYPE_SIZE (element));
739 /* Now round the alignment and size,
740 using machine-dependent criteria if any. */
742 #ifdef ROUND_TYPE_ALIGN
743 TYPE_ALIGN (type)
744 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
745 #else
746 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
747 #endif
749 #ifdef ROUND_TYPE_SIZE
750 if (TYPE_SIZE (type) != 0)
751 TYPE_SIZE (type)
752 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
753 #endif
755 TYPE_MODE (type) = BLKmode;
756 if (TYPE_SIZE (type) != 0
757 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
758 /* BLKmode elements force BLKmode aggregate;
759 else extract/store fields may lose. */
760 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
761 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
763 TYPE_MODE (type)
764 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
765 MODE_INT, 1);
767 if (STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
768 && TYPE_ALIGN (type) < TREE_INT_CST_LOW (TYPE_SIZE (type))
769 && TYPE_MODE (type) != BLKmode)
771 TYPE_NO_FORCE_BLK (type) = 1;
772 TYPE_MODE (type) = BLKmode;
775 break;
778 case RECORD_TYPE:
779 pending_statics = layout_record (type);
780 TYPE_MODE (type) = BLKmode;
781 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
783 tree field;
784 /* A record which has any BLKmode members must itself be BLKmode;
785 it can't go in a register.
786 Unless the member is BLKmode only because it isn't aligned. */
787 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
789 int bitpos;
791 if (TREE_CODE (field) != FIELD_DECL)
792 continue;
794 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
795 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
796 goto record_lose;
798 if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
799 goto record_lose;
801 bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
803 /* Must be BLKmode if any field crosses a word boundary,
804 since extract_bit_field can't handle that in registers. */
805 if (bitpos / BITS_PER_WORD
806 != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
807 / BITS_PER_WORD)
808 /* But there is no problem if the field is entire words. */
809 && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD == 0)
810 goto record_lose;
813 TYPE_MODE (type)
814 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
815 MODE_INT, 1);
817 /* If structure's known alignment is less than
818 what the scalar mode would need, and it matters,
819 then stick with BLKmode. */
820 if (STRICT_ALIGNMENT
821 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
822 || (TYPE_ALIGN (type)
823 >= TREE_INT_CST_LOW (TYPE_SIZE (type)))))
825 if (TYPE_MODE (type) != BLKmode)
826 /* If this is the only reason this type is BLKmode,
827 then don't force containing types to be BLKmode. */
828 TYPE_NO_FORCE_BLK (type) = 1;
829 TYPE_MODE (type) = BLKmode;
832 record_lose: ;
835 /* Lay out any static members. This is done now
836 because their type may use the record's type. */
837 while (pending_statics)
839 layout_decl (TREE_VALUE (pending_statics), 0);
840 pending_statics = TREE_CHAIN (pending_statics);
842 break;
844 case UNION_TYPE:
845 case QUAL_UNION_TYPE:
846 layout_union (type);
847 TYPE_MODE (type) = BLKmode;
848 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
849 /* If structure's known alignment is less than
850 what the scalar mode would need, and it matters,
851 then stick with BLKmode. */
852 && (! STRICT_ALIGNMENT
853 || TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
854 || TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type))))
856 tree field;
857 /* A union which has any BLKmode members must itself be BLKmode;
858 it can't go in a register.
859 Unless the member is BLKmode only because it isn't aligned. */
860 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
862 if (TREE_CODE (field) != FIELD_DECL)
863 continue;
865 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
866 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
867 goto union_lose;
870 TYPE_MODE (type)
871 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
872 MODE_INT, 1);
874 union_lose: ;
876 break;
878 /* Pascal and Chill types */
879 case BOOLEAN_TYPE: /* store one byte/boolean for now. */
880 TYPE_MODE (type) = QImode;
881 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
882 TYPE_PRECISION (type) = 1;
883 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
884 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
885 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
886 TREE_UNSIGNED (type) = 1;
887 break;
889 case CHAR_TYPE:
890 TYPE_MODE (type) = QImode;
891 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
892 TYPE_PRECISION (type) = GET_MODE_BITSIZE (TYPE_MODE (type));
893 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
894 break;
896 case SET_TYPE:
897 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
898 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
899 abort();
900 else
902 #ifndef SET_WORD_SIZE
903 #define SET_WORD_SIZE BITS_PER_WORD
904 #endif
905 int alignment = set_alignment ? set_alignment : SET_WORD_SIZE;
906 int size_in_bits =
907 TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
908 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1;
909 int rounded_size
910 = ((size_in_bits + alignment - 1) / alignment) * alignment;
911 if (rounded_size > alignment)
912 TYPE_MODE (type) = BLKmode;
913 else
914 TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
915 TYPE_SIZE (type) = size_int (rounded_size);
916 TYPE_ALIGN (type) = alignment;
917 TYPE_PRECISION (type) = size_in_bits;
919 break;
921 case FILE_TYPE:
922 /* The size may vary in different languages, so the language front end
923 should fill in the size. */
924 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
925 TYPE_MODE (type) = BLKmode;
926 break;
928 default:
929 abort ();
930 } /* end switch */
932 /* Normally, use the alignment corresponding to the mode chosen.
933 However, where strict alignment is not required, avoid
934 over-aligning structures, since most compilers do not do this
935 alignment. */
937 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
938 && (STRICT_ALIGNMENT
939 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
940 && TREE_CODE (type) != QUAL_UNION_TYPE
941 && TREE_CODE (type) != ARRAY_TYPE)))
942 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
944 /* Evaluate nonconstant size only once, either now or as soon as safe. */
945 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
946 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
948 /* Also layout any other variants of the type. */
949 if (TYPE_NEXT_VARIANT (type)
950 || type != TYPE_MAIN_VARIANT (type))
952 tree variant;
953 /* Record layout info of this variant. */
954 tree size = TYPE_SIZE (type);
955 int align = TYPE_ALIGN (type);
956 enum machine_mode mode = TYPE_MODE (type);
958 /* Copy it into all variants. */
959 for (variant = TYPE_MAIN_VARIANT (type);
960 variant;
961 variant = TYPE_NEXT_VARIANT (variant))
963 TYPE_SIZE (variant) = size;
964 TYPE_ALIGN (variant) = align;
965 TYPE_MODE (variant) = mode;
969 pop_obstacks ();
970 resume_momentary (old);
973 /* Create and return a type for signed integers of PRECISION bits. */
975 tree
976 make_signed_type (precision)
977 int precision;
979 register tree type = make_node (INTEGER_TYPE);
981 TYPE_PRECISION (type) = precision;
983 /* Create the extreme values based on the number of bits. */
985 TYPE_MIN_VALUE (type)
986 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
987 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
988 (((HOST_WIDE_INT) (-1)
989 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
990 ? precision - HOST_BITS_PER_WIDE_INT - 1
991 : 0))));
992 TYPE_MAX_VALUE (type)
993 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
994 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
995 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
996 ? (((HOST_WIDE_INT) 1
997 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
998 : 0));
1000 /* Give this type's extreme values this type as their type. */
1002 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1003 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1005 /* The first type made with this or `make_unsigned_type'
1006 is the type for size values. */
1008 if (sizetype == 0)
1010 sizetype = type;
1013 /* Lay out the type: set its alignment, size, etc. */
1015 layout_type (type);
1017 return type;
1020 /* Create and return a type for unsigned integers of PRECISION bits. */
1022 tree
1023 make_unsigned_type (precision)
1024 int precision;
1026 register tree type = make_node (INTEGER_TYPE);
1028 TYPE_PRECISION (type) = precision;
1030 /* The first type made with this or `make_signed_type'
1031 is the type for size values. */
1033 if (sizetype == 0)
1035 sizetype = type;
1038 fixup_unsigned_type (type);
1039 return type;
1042 /* Set the extreme values of TYPE based on its precision in bits,
1043 then lay it out. Used when make_signed_type won't do
1044 because the tree code is not INTEGER_TYPE.
1045 E.g. for Pascal, when the -fsigned-char option is given. */
1047 void
1048 fixup_signed_type (type)
1049 tree type;
1051 register int precision = TYPE_PRECISION (type);
1053 TYPE_MIN_VALUE (type)
1054 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1055 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1056 (((HOST_WIDE_INT) (-1)
1057 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1058 ? precision - HOST_BITS_PER_WIDE_INT - 1
1059 : 0))));
1060 TYPE_MAX_VALUE (type)
1061 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1062 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1063 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1064 ? (((HOST_WIDE_INT) 1
1065 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1066 : 0));
1068 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1069 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1071 /* Lay out the type: set its alignment, size, etc. */
1073 layout_type (type);
1076 /* Set the extreme values of TYPE based on its precision in bits,
1077 then lay it out. This is used both in `make_unsigned_type'
1078 and for enumeral types. */
1080 void
1081 fixup_unsigned_type (type)
1082 tree type;
1084 register int precision = TYPE_PRECISION (type);
1086 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1087 TYPE_MAX_VALUE (type)
1088 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1089 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1090 precision - HOST_BITS_PER_WIDE_INT > 0
1091 ? ((unsigned HOST_WIDE_INT) ~0
1092 >> (HOST_BITS_PER_WIDE_INT
1093 - (precision - HOST_BITS_PER_WIDE_INT)))
1094 : 0);
1095 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1096 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1098 /* Lay out the type: set its alignment, size, etc. */
1100 layout_type (type);
1103 /* Find the best machine mode to use when referencing a bit field of length
1104 BITSIZE bits starting at BITPOS.
1106 The underlying object is known to be aligned to a boundary of ALIGN bits.
1107 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1108 larger than LARGEST_MODE (usually SImode).
1110 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1111 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1112 mode meeting these conditions.
1114 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1115 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1116 all the conditions. */
1118 enum machine_mode
1119 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1120 int bitsize, bitpos;
1121 int align;
1122 enum machine_mode largest_mode;
1123 int volatilep;
1125 enum machine_mode mode;
1126 int unit;
1128 /* Find the narrowest integer mode that contains the bit field. */
1129 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1130 mode = GET_MODE_WIDER_MODE (mode))
1132 unit = GET_MODE_BITSIZE (mode);
1133 if (bitpos / unit == (bitpos + bitsize - 1) / unit)
1134 break;
1137 if (mode == MAX_MACHINE_MODE
1138 /* It is tempting to omit the following line
1139 if STRICT_ALIGNMENT is true.
1140 But that is incorrect, since if the bitfield uses part of 3 bytes
1141 and we use a 4-byte mode, we could get a spurious segv
1142 if the extra 4th byte is past the end of memory.
1143 (Though at least one Unix compiler ignores this problem:
1144 that on the Sequent 386 machine. */
1145 || MIN (unit, BIGGEST_ALIGNMENT) > align
1146 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1147 return VOIDmode;
1149 if (SLOW_BYTE_ACCESS && ! volatilep)
1151 enum machine_mode wide_mode = VOIDmode, tmode;
1153 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1154 tmode = GET_MODE_WIDER_MODE (tmode))
1156 unit = GET_MODE_BITSIZE (tmode);
1157 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1158 && unit <= BITS_PER_WORD
1159 && unit <= MIN (align, BIGGEST_ALIGNMENT)
1160 && (largest_mode == VOIDmode
1161 || unit <= GET_MODE_BITSIZE (largest_mode)))
1162 wide_mode = tmode;
1165 if (wide_mode != VOIDmode)
1166 return wide_mode;
1169 return mode;
1172 /* Save all variables describing the current status into the structure *P.
1173 This is used before starting a nested function. */
1175 void
1176 save_storage_status (p)
1177 struct function *p;
1179 #if 0 /* Need not save, since always 0 and non0 (resp.) within a function. */
1180 p->pending_sizes = pending_sizes;
1181 p->immediate_size_expand = immediate_size_expand;
1182 #endif /* 0 */
1185 /* Restore all variables describing the current status from the structure *P.
1186 This is used after a nested function. */
1188 void
1189 restore_storage_status (p)
1190 struct function *p;
1192 #if 0
1193 pending_sizes = p->pending_sizes;
1194 immediate_size_expand = p->immediate_size_expand;
1195 #endif /* 0 */