1 /* C-compiler utilities for types and variables storage layout
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1996, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
34 /* Set to one when set_sizetype has been called. */
35 static int sizetype_set
;
37 /* List of types created before set_sizetype has been called. We do not
38 make this a GGC root since we want these nodes to be reclaimed. */
39 static tree early_type_list
;
41 /* Data type for the expressions representing sizes of data types.
42 It is the first integer type laid out. */
43 tree sizetype_tab
[(int) TYPE_KIND_LAST
];
45 /* If nonzero, this is an upper limit on alignment of structure fields.
46 The value is measured in bits. */
47 unsigned int maximum_field_alignment
;
49 /* If non-zero, the alignment of a bitstring or (power-)set value, in bits.
50 May be overridden by front-ends. */
51 unsigned int set_alignment
= 0;
53 /* Nonzero if all REFERENCE_TYPEs are internal and hence should be
54 allocated in Pmode, not ptr_mode. Set only by internal_reference_types
55 called only by a front end. */
56 static int reference_types_internal
= 0;
58 static void finalize_record_size
PARAMS ((record_layout_info
));
59 static void finalize_type_size
PARAMS ((tree
));
60 static void place_union_field
PARAMS ((record_layout_info
, tree
));
61 extern void debug_rli
PARAMS ((record_layout_info
));
63 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
65 static tree pending_sizes
;
67 /* Nonzero means cannot safely call expand_expr now,
68 so put variable sizes onto `pending_sizes' instead. */
70 int immediate_size_expand
;
72 /* Show that REFERENCE_TYPES are internal and should be Pmode. Called only
76 internal_reference_types ()
78 reference_types_internal
= 1;
81 /* Get a list of all the objects put on the pending sizes list. */
86 tree chain
= pending_sizes
;
89 /* Put each SAVE_EXPR into the current function. */
90 for (t
= chain
; t
; t
= TREE_CHAIN (t
))
91 SAVE_EXPR_CONTEXT (TREE_VALUE (t
)) = current_function_decl
;
97 /* Return non-zero if EXPR is present on the pending sizes list. */
100 is_pending_size (expr
)
105 for (t
= pending_sizes
; t
; t
= TREE_CHAIN (t
))
106 if (TREE_VALUE (t
) == expr
)
111 /* Add EXPR to the pending sizes list. */
114 put_pending_size (expr
)
117 if (TREE_CODE (expr
) == SAVE_EXPR
)
118 pending_sizes
= tree_cons (NULL_TREE
, expr
, pending_sizes
);
121 /* Put a chain of objects into the pending sizes list, which must be
125 put_pending_sizes (chain
)
131 pending_sizes
= chain
;
134 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
135 to serve as the actual size-expression for a type or decl. */
141 /* If the language-processor is to take responsibility for variable-sized
142 items (e.g., languages which have elaboration procedures like Ada),
143 just return SIZE unchanged. Likewise for self-referential sizes. */
144 if (TREE_CONSTANT (size
)
145 || global_bindings_p () < 0 || contains_placeholder_p (size
))
148 size
= save_expr (size
);
150 /* If an array with a variable number of elements is declared, and
151 the elements require destruction, we will emit a cleanup for the
152 array. That cleanup is run both on normal exit from the block
153 and in the exception-handler for the block. Normally, when code
154 is used in both ordinary code and in an exception handler it is
155 `unsaved', i.e., all SAVE_EXPRs are recalculated. However, we do
156 not wish to do that here; the array-size is the same in both
158 if (TREE_CODE (size
) == SAVE_EXPR
)
159 SAVE_EXPR_PERSISTENT_P (size
) = 1;
161 if (global_bindings_p ())
163 if (TREE_CONSTANT (size
))
164 error ("type size can't be explicitly evaluated");
166 error ("variable-size type declared outside of any function");
168 return size_one_node
;
171 if (immediate_size_expand
)
172 /* NULL_RTX is not defined; neither is the rtx type.
173 Also, we would like to pass const0_rtx here, but don't have it. */
174 expand_expr (size
, expand_expr (integer_zero_node
, NULL_RTX
, VOIDmode
, 0),
176 else if (cfun
!= 0 && cfun
->x_dont_save_pending_sizes_p
)
177 /* The front-end doesn't want us to keep a list of the expressions
178 that determine sizes for variable size objects. */
181 put_pending_size (size
);
186 #ifndef MAX_FIXED_MODE_SIZE
187 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
190 /* Return the machine mode to use for a nonscalar of SIZE bits.
191 The mode must be in class CLASS, and have exactly that many bits.
192 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
196 mode_for_size (size
, class, limit
)
198 enum mode_class
class;
201 register enum machine_mode mode
;
203 if (limit
&& size
> MAX_FIXED_MODE_SIZE
)
206 /* Get the first mode which has this size, in the specified class. */
207 for (mode
= GET_CLASS_NARROWEST_MODE (class); mode
!= VOIDmode
;
208 mode
= GET_MODE_WIDER_MODE (mode
))
209 if (GET_MODE_BITSIZE (mode
) == size
)
215 /* Similar, except passed a tree node. */
218 mode_for_size_tree (size
, class, limit
)
220 enum mode_class
class;
223 if (TREE_CODE (size
) != INTEGER_CST
224 /* What we really want to say here is that the size can fit in a
225 host integer, but we know there's no way we'd find a mode for
226 this many bits, so there's no point in doing the precise test. */
227 || compare_tree_int (size
, 1000) > 0)
230 return mode_for_size (TREE_INT_CST_LOW (size
), class, limit
);
233 /* Similar, but never return BLKmode; return the narrowest mode that
234 contains at least the requested number of bits. */
237 smallest_mode_for_size (size
, class)
239 enum mode_class
class;
241 register enum machine_mode mode
;
243 /* Get the first mode which has at least this size, in the
245 for (mode
= GET_CLASS_NARROWEST_MODE (class); mode
!= VOIDmode
;
246 mode
= GET_MODE_WIDER_MODE (mode
))
247 if (GET_MODE_BITSIZE (mode
) >= size
)
253 /* Find an integer mode of the exact same size, or BLKmode on failure. */
256 int_mode_for_mode (mode
)
257 enum machine_mode mode
;
259 switch (GET_MODE_CLASS (mode
))
262 case MODE_PARTIAL_INT
:
265 case MODE_COMPLEX_INT
:
266 case MODE_COMPLEX_FLOAT
:
268 case MODE_VECTOR_INT
:
269 case MODE_VECTOR_FLOAT
:
270 mode
= mode_for_size (GET_MODE_BITSIZE (mode
), MODE_INT
, 0);
277 /* ... fall through ... */
287 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
288 This can only be applied to objects of a sizetype. */
291 round_up (value
, divisor
)
295 tree arg
= size_int_type (divisor
, TREE_TYPE (value
));
297 return size_binop (MULT_EXPR
, size_binop (CEIL_DIV_EXPR
, value
, arg
), arg
);
300 /* Likewise, but round down. */
303 round_down (value
, divisor
)
307 tree arg
= size_int_type (divisor
, TREE_TYPE (value
));
309 return size_binop (MULT_EXPR
, size_binop (FLOOR_DIV_EXPR
, value
, arg
), arg
);
312 /* Set the size, mode and alignment of a ..._DECL node.
313 TYPE_DECL does need this for C++.
314 Note that LABEL_DECL and CONST_DECL nodes do not need this,
315 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
316 Don't call layout_decl for them.
318 KNOWN_ALIGN is the amount of alignment we can assume this
319 decl has with no special effort. It is relevant only for FIELD_DECLs
320 and depends on the previous fields.
321 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
322 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
323 the record will be aligned to suit. */
326 layout_decl (decl
, known_align
)
328 unsigned int known_align
;
330 register tree type
= TREE_TYPE (decl
);
331 register enum tree_code code
= TREE_CODE (decl
);
333 if (code
== CONST_DECL
)
335 else if (code
!= VAR_DECL
&& code
!= PARM_DECL
&& code
!= RESULT_DECL
336 && code
!= TYPE_DECL
&& code
!= FIELD_DECL
)
339 if (type
== error_mark_node
)
340 type
= void_type_node
;
342 /* Usually the size and mode come from the data type without change,
343 however, the front-end may set the explicit width of the field, so its
344 size may not be the same as the size of its type. This happens with
345 bitfields, of course (an `int' bitfield may be only 2 bits, say), but it
346 also happens with other fields. For example, the C++ front-end creates
347 zero-sized fields corresponding to empty base classes, and depends on
348 layout_type setting DECL_FIELD_BITPOS correctly for the field. Set the
349 size in bytes from the size in bits. If we have already set the mode,
350 don't set it again since we can be called twice for FIELD_DECLs. */
352 TREE_UNSIGNED (decl
) = TREE_UNSIGNED (type
);
353 if (DECL_MODE (decl
) == VOIDmode
)
354 DECL_MODE (decl
) = TYPE_MODE (type
);
356 if (DECL_SIZE (decl
) == 0)
358 DECL_SIZE (decl
) = TYPE_SIZE (type
);
359 DECL_SIZE_UNIT (decl
) = TYPE_SIZE_UNIT (type
);
362 DECL_SIZE_UNIT (decl
)
363 = convert (sizetype
, size_binop (CEIL_DIV_EXPR
, DECL_SIZE (decl
),
366 /* Force alignment required for the data type.
367 But if the decl itself wants greater alignment, don't override that.
368 Likewise, if the decl is packed, don't override it. */
369 if (! (code
== FIELD_DECL
&& DECL_BIT_FIELD (decl
))
370 && (DECL_ALIGN (decl
) == 0
371 || (! (code
== FIELD_DECL
&& DECL_PACKED (decl
))
372 && TYPE_ALIGN (type
) > DECL_ALIGN (decl
))))
374 DECL_ALIGN (decl
) = TYPE_ALIGN (type
);
375 DECL_USER_ALIGN (decl
) = 0;
378 /* For fields, set the bit field type and update the alignment. */
379 if (code
== FIELD_DECL
)
381 DECL_BIT_FIELD_TYPE (decl
) = DECL_BIT_FIELD (decl
) ? type
: 0;
382 if (maximum_field_alignment
!= 0)
383 DECL_ALIGN (decl
) = MIN (DECL_ALIGN (decl
), maximum_field_alignment
);
384 else if (DECL_PACKED (decl
))
386 DECL_ALIGN (decl
) = MIN (DECL_ALIGN (decl
), BITS_PER_UNIT
);
387 DECL_USER_ALIGN (decl
) = 0;
391 /* See if we can use an ordinary integer mode for a bit-field.
392 Conditions are: a fixed size that is correct for another mode
393 and occupying a complete byte or bytes on proper boundary. */
394 if (code
== FIELD_DECL
&& DECL_BIT_FIELD (decl
)
395 && TYPE_SIZE (type
) != 0
396 && TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
397 && GET_MODE_CLASS (TYPE_MODE (type
)) == MODE_INT
)
399 register enum machine_mode xmode
400 = mode_for_size_tree (DECL_SIZE (decl
), MODE_INT
, 1);
402 if (xmode
!= BLKmode
&& known_align
>= GET_MODE_ALIGNMENT (xmode
))
404 DECL_ALIGN (decl
) = MAX (GET_MODE_ALIGNMENT (xmode
),
406 DECL_MODE (decl
) = xmode
;
407 DECL_BIT_FIELD (decl
) = 0;
411 /* Turn off DECL_BIT_FIELD if we won't need it set. */
412 if (code
== FIELD_DECL
&& DECL_BIT_FIELD (decl
)
413 && TYPE_MODE (type
) == BLKmode
&& DECL_MODE (decl
) == BLKmode
414 && known_align
>= TYPE_ALIGN (type
)
415 && DECL_ALIGN (decl
) >= TYPE_ALIGN (type
)
416 && DECL_SIZE_UNIT (decl
) != 0)
417 DECL_BIT_FIELD (decl
) = 0;
419 /* Evaluate nonconstant size only once, either now or as soon as safe. */
420 if (DECL_SIZE (decl
) != 0 && TREE_CODE (DECL_SIZE (decl
)) != INTEGER_CST
)
421 DECL_SIZE (decl
) = variable_size (DECL_SIZE (decl
));
422 if (DECL_SIZE_UNIT (decl
) != 0
423 && TREE_CODE (DECL_SIZE_UNIT (decl
)) != INTEGER_CST
)
424 DECL_SIZE_UNIT (decl
) = variable_size (DECL_SIZE_UNIT (decl
));
426 /* If requested, warn about definitions of large data objects. */
428 && (code
== VAR_DECL
|| code
== PARM_DECL
)
429 && ! DECL_EXTERNAL (decl
))
431 tree size
= DECL_SIZE_UNIT (decl
);
433 if (size
!= 0 && TREE_CODE (size
) == INTEGER_CST
434 && compare_tree_int (size
, larger_than_size
) > 0)
436 unsigned int size_as_int
= TREE_INT_CST_LOW (size
);
438 if (compare_tree_int (size
, size_as_int
) == 0)
439 warning_with_decl (decl
, "size of `%s' is %d bytes", size_as_int
);
441 warning_with_decl (decl
, "size of `%s' is larger than %d bytes",
447 /* Hook for a front-end function that can modify the record layout as needed
448 immediately before it is finalized. */
450 void (*lang_adjust_rli
) PARAMS ((record_layout_info
)) = 0;
453 set_lang_adjust_rli (f
)
454 void (*f
) PARAMS ((record_layout_info
));
459 /* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
460 QUAL_UNION_TYPE. Return a pointer to a struct record_layout_info which
461 is to be passed to all other layout functions for this record. It is the
462 responsibility of the caller to call `free' for the storage returned.
463 Note that garbage collection is not permitted until we finish laying
467 start_record_layout (t
)
470 record_layout_info rli
471 = (record_layout_info
) xmalloc (sizeof (struct record_layout_info_s
));
475 /* If the type has a minimum specified alignment (via an attribute
476 declaration, for example) use it -- otherwise, start with a
477 one-byte alignment. */
478 rli
->record_align
= MAX (BITS_PER_UNIT
, TYPE_ALIGN (t
));
479 rli
->unpacked_align
= rli
->unpadded_align
= rli
->record_align
;
480 rli
->offset_align
= MAX (rli
->record_align
, BIGGEST_ALIGNMENT
);
482 #ifdef STRUCTURE_SIZE_BOUNDARY
483 /* Packed structures don't need to have minimum size. */
484 if (! TYPE_PACKED (t
))
485 rli
->record_align
= MAX (rli
->record_align
, STRUCTURE_SIZE_BOUNDARY
);
488 rli
->offset
= size_zero_node
;
489 rli
->bitpos
= bitsize_zero_node
;
490 rli
->pending_statics
= 0;
491 rli
->packed_maybe_necessary
= 0;
496 /* These four routines perform computations that convert between
497 the offset/bitpos forms and byte and bit offsets. */
500 bit_from_pos (offset
, bitpos
)
503 return size_binop (PLUS_EXPR
, bitpos
,
504 size_binop (MULT_EXPR
, convert (bitsizetype
, offset
),
509 byte_from_pos (offset
, bitpos
)
512 return size_binop (PLUS_EXPR
, offset
,
514 size_binop (TRUNC_DIV_EXPR
, bitpos
,
515 bitsize_unit_node
)));
519 pos_from_byte (poffset
, pbitpos
, off_align
, pos
)
520 tree
*poffset
, *pbitpos
;
521 unsigned int off_align
;
525 = size_binop (MULT_EXPR
,
527 size_binop (FLOOR_DIV_EXPR
, pos
,
528 bitsize_int (off_align
530 size_int (off_align
/ BITS_PER_UNIT
));
531 *pbitpos
= size_binop (MULT_EXPR
,
532 size_binop (FLOOR_MOD_EXPR
, pos
,
533 bitsize_int (off_align
/ BITS_PER_UNIT
)),
538 pos_from_bit (poffset
, pbitpos
, off_align
, pos
)
539 tree
*poffset
, *pbitpos
;
540 unsigned int off_align
;
543 *poffset
= size_binop (MULT_EXPR
,
545 size_binop (FLOOR_DIV_EXPR
, pos
,
546 bitsize_int (off_align
))),
547 size_int (off_align
/ BITS_PER_UNIT
));
548 *pbitpos
= size_binop (FLOOR_MOD_EXPR
, pos
, bitsize_int (off_align
));
551 /* Given a pointer to bit and byte offsets and an offset alignment,
552 normalize the offsets so they are within the alignment. */
555 normalize_offset (poffset
, pbitpos
, off_align
)
556 tree
*poffset
, *pbitpos
;
557 unsigned int off_align
;
559 /* If the bit position is now larger than it should be, adjust it
561 if (compare_tree_int (*pbitpos
, off_align
) >= 0)
563 tree extra_aligns
= size_binop (FLOOR_DIV_EXPR
, *pbitpos
,
564 bitsize_int (off_align
));
567 = size_binop (PLUS_EXPR
, *poffset
,
568 size_binop (MULT_EXPR
, convert (sizetype
, extra_aligns
),
569 size_int (off_align
/ BITS_PER_UNIT
)));
572 = size_binop (FLOOR_MOD_EXPR
, *pbitpos
, bitsize_int (off_align
));
576 /* Print debugging information about the information in RLI. */
580 record_layout_info rli
;
582 print_node_brief (stderr
, "type", rli
->t
, 0);
583 print_node_brief (stderr
, "\noffset", rli
->offset
, 0);
584 print_node_brief (stderr
, " bitpos", rli
->bitpos
, 0);
586 fprintf (stderr
, "\naligns: rec = %u, unpack = %u, unpad = %u, off = %u\n",
587 rli
->record_align
, rli
->unpacked_align
, rli
->unpadded_align
,
589 if (rli
->packed_maybe_necessary
)
590 fprintf (stderr
, "packed may be necessary\n");
592 if (rli
->pending_statics
)
594 fprintf (stderr
, "pending statics:\n");
595 debug_tree (rli
->pending_statics
);
599 /* Given an RLI with a possibly-incremented BITPOS, adjust OFFSET and
600 BITPOS if necessary to keep BITPOS below OFFSET_ALIGN. */
604 record_layout_info rli
;
606 normalize_offset (&rli
->offset
, &rli
->bitpos
, rli
->offset_align
);
609 /* Returns the size in bytes allocated so far. */
612 rli_size_unit_so_far (rli
)
613 record_layout_info rli
;
615 return byte_from_pos (rli
->offset
, rli
->bitpos
);
618 /* Returns the size in bits allocated so far. */
621 rli_size_so_far (rli
)
622 record_layout_info rli
;
624 return bit_from_pos (rli
->offset
, rli
->bitpos
);
627 /* Called from place_field to handle unions. */
630 place_union_field (rli
, field
)
631 record_layout_info rli
;
634 unsigned int desired_align
;
636 layout_decl (field
, 0);
638 DECL_FIELD_OFFSET (field
) = size_zero_node
;
639 DECL_FIELD_BIT_OFFSET (field
) = bitsize_zero_node
;
640 SET_DECL_OFFSET_ALIGN (field
, BIGGEST_ALIGNMENT
);
642 desired_align
= DECL_ALIGN (field
);
644 #ifdef BIGGEST_FIELD_ALIGNMENT
645 /* Some targets (i.e. i386) limit union field alignment
646 to a lower boundary than alignment of variables unless
647 it was overridden by attribute aligned. */
648 if (! DECL_USER_ALIGN (field
))
650 MIN (desired_align
, (unsigned) BIGGEST_FIELD_ALIGNMENT
);
653 /* Union must be at least as aligned as any field requires. */
654 rli
->record_align
= MAX (rli
->record_align
, desired_align
);
655 rli
->unpadded_align
= MAX (rli
->unpadded_align
, desired_align
);
657 #ifdef PCC_BITFIELD_TYPE_MATTERS
658 /* On the m88000, a bit field of declare type `int' forces the
659 entire union to have `int' alignment. */
660 if (PCC_BITFIELD_TYPE_MATTERS
&& DECL_BIT_FIELD_TYPE (field
))
662 rli
->record_align
= MAX (rli
->record_align
,
663 TYPE_ALIGN (TREE_TYPE (field
)));
664 rli
->unpadded_align
= MAX (rli
->unpadded_align
,
665 TYPE_ALIGN (TREE_TYPE (field
)));
669 /* We assume the union's size will be a multiple of a byte so we don't
670 bother with BITPOS. */
671 if (TREE_CODE (rli
->t
) == UNION_TYPE
)
672 rli
->offset
= size_binop (MAX_EXPR
, rli
->offset
, DECL_SIZE_UNIT (field
));
673 else if (TREE_CODE (rli
->t
) == QUAL_UNION_TYPE
)
674 rli
->offset
= fold (build (COND_EXPR
, sizetype
,
675 DECL_QUALIFIER (field
),
676 DECL_SIZE_UNIT (field
), rli
->offset
));
679 /* RLI contains information about the layout of a RECORD_TYPE. FIELD
680 is a FIELD_DECL to be added after those fields already present in
681 T. (FIELD is not actually added to the TYPE_FIELDS list here;
682 callers that desire that behavior must manually perform that step.) */
685 place_field (rli
, field
)
686 record_layout_info rli
;
689 /* The alignment required for FIELD. */
690 unsigned int desired_align
;
691 /* The alignment FIELD would have if we just dropped it into the
692 record as it presently stands. */
693 unsigned int known_align
;
694 unsigned int actual_align
;
695 unsigned int user_align
;
696 /* The type of this field. */
697 tree type
= TREE_TYPE (field
);
699 if (TREE_CODE (field
) == ERROR_MARK
|| TREE_CODE (type
) == ERROR_MARK
)
702 /* If FIELD is static, then treat it like a separate variable, not
703 really like a structure field. If it is a FUNCTION_DECL, it's a
704 method. In both cases, all we do is lay out the decl, and we do
705 it *after* the record is laid out. */
706 if (TREE_CODE (field
) == VAR_DECL
)
708 rli
->pending_statics
= tree_cons (NULL_TREE
, field
,
709 rli
->pending_statics
);
713 /* Enumerators and enum types which are local to this class need not
714 be laid out. Likewise for initialized constant fields. */
715 else if (TREE_CODE (field
) != FIELD_DECL
)
718 /* Unions are laid out very differently than records, so split
719 that code off to another function. */
720 else if (TREE_CODE (rli
->t
) != RECORD_TYPE
)
722 place_union_field (rli
, field
);
726 /* Work out the known alignment so far. Note that A & (-A) is the
727 value of the least-significant bit in A that is one. */
728 if (! integer_zerop (rli
->bitpos
))
729 known_align
= (tree_low_cst (rli
->bitpos
, 1)
730 & - tree_low_cst (rli
->bitpos
, 1));
731 else if (integer_zerop (rli
->offset
))
732 known_align
= BIGGEST_ALIGNMENT
;
733 else if (host_integerp (rli
->offset
, 1))
734 known_align
= (BITS_PER_UNIT
735 * (tree_low_cst (rli
->offset
, 1)
736 & - tree_low_cst (rli
->offset
, 1)));
738 known_align
= rli
->offset_align
;
740 /* Lay out the field so we know what alignment it needs. For a
741 packed field, use the alignment as specified, disregarding what
742 the type would want. */
743 desired_align
= DECL_ALIGN (field
);
744 user_align
= DECL_USER_ALIGN (field
);
745 layout_decl (field
, known_align
);
746 if (! DECL_PACKED (field
))
748 desired_align
= DECL_ALIGN (field
);
749 user_align
= DECL_USER_ALIGN (field
);
752 /* Some targets (i.e. i386, VMS) limit struct field alignment
753 to a lower boundary than alignment of variables unless
754 it was overridden by attribute aligned. */
755 #ifdef BIGGEST_FIELD_ALIGNMENT
758 = MIN (desired_align
, (unsigned) BIGGEST_FIELD_ALIGNMENT
);
761 #ifdef ADJUST_FIELD_ALIGN
762 desired_align
= ADJUST_FIELD_ALIGN (field
, desired_align
);
765 /* Record must have at least as much alignment as any field.
766 Otherwise, the alignment of the field within the record is
768 #ifdef PCC_BITFIELD_TYPE_MATTERS
769 if (PCC_BITFIELD_TYPE_MATTERS
&& type
!= error_mark_node
770 && DECL_BIT_FIELD_TYPE (field
)
771 && ! integer_zerop (TYPE_SIZE (type
)))
773 /* For these machines, a zero-length field does not
774 affect the alignment of the structure as a whole.
775 It does, however, affect the alignment of the next field
776 within the structure. */
777 if (! integer_zerop (DECL_SIZE (field
)))
778 rli
->record_align
= MAX (rli
->record_align
, desired_align
);
779 else if (! DECL_PACKED (field
))
780 desired_align
= TYPE_ALIGN (type
);
782 /* A named bit field of declared type `int'
783 forces the entire structure to have `int' alignment. */
784 if (DECL_NAME (field
) != 0)
786 unsigned int type_align
= TYPE_ALIGN (type
);
788 if (maximum_field_alignment
!= 0)
789 type_align
= MIN (type_align
, maximum_field_alignment
);
790 else if (DECL_PACKED (field
))
791 type_align
= MIN (type_align
, BITS_PER_UNIT
);
793 rli
->record_align
= MAX (rli
->record_align
, type_align
);
794 rli
->unpadded_align
= MAX (rli
->unpadded_align
, DECL_ALIGN (field
));
796 rli
->unpacked_align
= MAX (rli
->unpacked_align
, TYPE_ALIGN (type
));
802 rli
->record_align
= MAX (rli
->record_align
, desired_align
);
803 rli
->unpacked_align
= MAX (rli
->unpacked_align
, TYPE_ALIGN (type
));
804 rli
->unpadded_align
= MAX (rli
->unpadded_align
, DECL_ALIGN (field
));
807 if (warn_packed
&& DECL_PACKED (field
))
809 if (known_align
> TYPE_ALIGN (type
))
811 if (TYPE_ALIGN (type
) > desired_align
)
813 if (STRICT_ALIGNMENT
)
814 warning_with_decl (field
, "packed attribute causes inefficient alignment for `%s'");
816 warning_with_decl (field
, "packed attribute is unnecessary for `%s'");
820 rli
->packed_maybe_necessary
= 1;
823 /* Does this field automatically have alignment it needs by virtue
824 of the fields that precede it and the record's own alignment? */
825 if (known_align
< desired_align
)
827 /* No, we need to skip space before this field.
828 Bump the cumulative size to multiple of field alignment. */
831 warning_with_decl (field
, "padding struct to align `%s'");
833 /* If the alignment is still within offset_align, just align
835 if (desired_align
< rli
->offset_align
)
836 rli
->bitpos
= round_up (rli
->bitpos
, desired_align
);
839 /* First adjust OFFSET by the partial bits, then align. */
841 = size_binop (PLUS_EXPR
, rli
->offset
,
843 size_binop (CEIL_DIV_EXPR
, rli
->bitpos
,
844 bitsize_unit_node
)));
845 rli
->bitpos
= bitsize_zero_node
;
847 rli
->offset
= round_up (rli
->offset
, desired_align
/ BITS_PER_UNIT
);
850 if (! TREE_CONSTANT (rli
->offset
))
851 rli
->offset_align
= desired_align
;
855 /* Handle compatibility with PCC. Note that if the record has any
856 variable-sized fields, we need not worry about compatibility. */
857 #ifdef PCC_BITFIELD_TYPE_MATTERS
858 if (PCC_BITFIELD_TYPE_MATTERS
859 && TREE_CODE (field
) == FIELD_DECL
860 && type
!= error_mark_node
861 && DECL_BIT_FIELD (field
)
862 && ! DECL_PACKED (field
)
863 && maximum_field_alignment
== 0
864 && ! integer_zerop (DECL_SIZE (field
))
865 && host_integerp (DECL_SIZE (field
), 1)
866 && host_integerp (rli
->offset
, 1)
867 && host_integerp (TYPE_SIZE (type
), 1))
869 unsigned int type_align
= TYPE_ALIGN (type
);
870 tree dsize
= DECL_SIZE (field
);
871 HOST_WIDE_INT field_size
= tree_low_cst (dsize
, 1);
872 HOST_WIDE_INT offset
= tree_low_cst (rli
->offset
, 0);
873 HOST_WIDE_INT bit_offset
= tree_low_cst (rli
->bitpos
, 0);
875 /* A bit field may not span more units of alignment of its type
876 than its type itself. Advance to next boundary if necessary. */
877 if ((((offset
* BITS_PER_UNIT
+ bit_offset
+ field_size
+
880 - (offset
* BITS_PER_UNIT
+ bit_offset
) / type_align
)
881 > tree_low_cst (TYPE_SIZE (type
), 1) / type_align
)
882 rli
->bitpos
= round_up (rli
->bitpos
, type_align
);
886 #ifdef BITFIELD_NBYTES_LIMITED
887 if (BITFIELD_NBYTES_LIMITED
888 && TREE_CODE (field
) == FIELD_DECL
889 && type
!= error_mark_node
890 && DECL_BIT_FIELD_TYPE (field
)
891 && ! DECL_PACKED (field
)
892 && ! integer_zerop (DECL_SIZE (field
))
893 && host_integerp (DECL_SIZE (field
), 1)
894 && host_integerp (rli
->offset
, 1)
895 && host_integerp (TYPE_SIZE (type
), 1))
897 unsigned int type_align
= TYPE_ALIGN (type
);
898 tree dsize
= DECL_SIZE (field
);
899 HOST_WIDE_INT field_size
= tree_low_cst (dsize
, 1);
900 HOST_WIDE_INT offset
= tree_low_cst (rli
->offset
, 0);
901 HOST_WIDE_INT bit_offset
= tree_low_cst (rli
->bitpos
, 0);
903 if (maximum_field_alignment
!= 0)
904 type_align
= MIN (type_align
, maximum_field_alignment
);
905 /* ??? This test is opposite the test in the containing if
906 statement, so this code is unreachable currently. */
907 else if (DECL_PACKED (field
))
908 type_align
= MIN (type_align
, BITS_PER_UNIT
);
910 /* A bit field may not span the unit of alignment of its type.
911 Advance to next boundary if necessary. */
912 /* ??? This code should match the code above for the
913 PCC_BITFIELD_TYPE_MATTERS case. */
914 if ((offset
* BITS_PER_UNIT
+ bit_offset
) / type_align
915 != ((offset
* BITS_PER_UNIT
+ bit_offset
+ field_size
- 1)
917 rli
->bitpos
= round_up (rli
->bitpos
, type_align
);
921 /* Offset so far becomes the position of this field after normalizing. */
923 DECL_FIELD_OFFSET (field
) = rli
->offset
;
924 DECL_FIELD_BIT_OFFSET (field
) = rli
->bitpos
;
925 SET_DECL_OFFSET_ALIGN (field
, rli
->offset_align
);
927 /* If this field ended up more aligned than we thought it would be (we
928 approximate this by seeing if its position changed), lay out the field
929 again; perhaps we can use an integral mode for it now. */
930 if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field
)))
931 actual_align
= (tree_low_cst (DECL_FIELD_BIT_OFFSET (field
), 1)
932 & - tree_low_cst (DECL_FIELD_BIT_OFFSET (field
), 1));
933 else if (integer_zerop (DECL_FIELD_OFFSET (field
)))
934 actual_align
= BIGGEST_ALIGNMENT
;
935 else if (host_integerp (DECL_FIELD_OFFSET (field
), 1))
936 actual_align
= (BITS_PER_UNIT
937 * (tree_low_cst (DECL_FIELD_OFFSET (field
), 1)
938 & - tree_low_cst (DECL_FIELD_OFFSET (field
), 1)));
940 actual_align
= DECL_OFFSET_ALIGN (field
);
942 if (known_align
!= actual_align
)
943 layout_decl (field
, actual_align
);
945 /* Now add size of this field to the size of the record. If the size is
946 not constant, treat the field as being a multiple of bytes and just
947 adjust the offset, resetting the bit position. Otherwise, apportion the
948 size amongst the bit position and offset. First handle the case of an
949 unspecified size, which can happen when we have an invalid nested struct
950 definition, such as struct j { struct j { int i; } }. The error message
951 is printed in finish_struct. */
952 if (DECL_SIZE (field
) == 0)
954 else if (TREE_CODE (DECL_SIZE_UNIT (field
)) != INTEGER_CST
955 || TREE_CONSTANT_OVERFLOW (DECL_SIZE_UNIT (field
)))
958 = size_binop (PLUS_EXPR
, rli
->offset
,
960 size_binop (CEIL_DIV_EXPR
, rli
->bitpos
,
961 bitsize_unit_node
)));
963 = size_binop (PLUS_EXPR
, rli
->offset
, DECL_SIZE_UNIT (field
));
964 rli
->bitpos
= bitsize_zero_node
;
965 rli
->offset_align
= MIN (rli
->offset_align
, DECL_ALIGN (field
));
969 rli
->bitpos
= size_binop (PLUS_EXPR
, rli
->bitpos
, DECL_SIZE (field
));
974 /* Assuming that all the fields have been laid out, this function uses
975 RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
976 inidicated by RLI. */
979 finalize_record_size (rli
)
980 record_layout_info rli
;
982 tree unpadded_size
, unpadded_size_unit
;
984 /* Now we want just byte and bit offsets, so set the offset alignment
985 to be a byte and then normalize. */
986 rli
->offset_align
= BITS_PER_UNIT
;
989 /* Determine the desired alignment. */
990 #ifdef ROUND_TYPE_ALIGN
991 TYPE_ALIGN (rli
->t
) = ROUND_TYPE_ALIGN (rli
->t
, TYPE_ALIGN (rli
->t
),
994 TYPE_ALIGN (rli
->t
) = MAX (TYPE_ALIGN (rli
->t
), rli
->record_align
);
996 TYPE_USER_ALIGN (rli
->t
) = 0;
998 /* Compute the size so far. Be sure to allow for extra bits in the
999 size in bytes. We have guaranteed above that it will be no more
1000 than a single byte. */
1001 unpadded_size
= rli_size_so_far (rli
);
1002 unpadded_size_unit
= rli_size_unit_so_far (rli
);
1003 if (! integer_zerop (rli
->bitpos
))
1005 = size_binop (PLUS_EXPR
, unpadded_size_unit
, size_one_node
);
1007 /* Record the un-rounded size in the binfo node. But first we check
1008 the size of TYPE_BINFO to make sure that BINFO_SIZE is available. */
1009 if (TYPE_BINFO (rli
->t
) && TREE_VEC_LENGTH (TYPE_BINFO (rli
->t
)) > 6)
1011 TYPE_BINFO_SIZE (rli
->t
) = unpadded_size
;
1012 TYPE_BINFO_SIZE_UNIT (rli
->t
) = unpadded_size_unit
;
1015 /* Round the size up to be a multiple of the required alignment */
1016 #ifdef ROUND_TYPE_SIZE
1017 TYPE_SIZE (rli
->t
) = ROUND_TYPE_SIZE (rli
->t
, unpadded_size
,
1018 TYPE_ALIGN (rli
->t
));
1019 TYPE_SIZE_UNIT (rli
->t
)
1020 = ROUND_TYPE_SIZE_UNIT (rli
->t
, unpadded_size_unit
,
1021 TYPE_ALIGN (rli
->t
) / BITS_PER_UNIT
);
1023 TYPE_SIZE (rli
->t
) = round_up (unpadded_size
, TYPE_ALIGN (rli
->t
));
1024 TYPE_SIZE_UNIT (rli
->t
) = round_up (unpadded_size_unit
,
1025 TYPE_ALIGN (rli
->t
) / BITS_PER_UNIT
);
1028 if (warn_padded
&& TREE_CONSTANT (unpadded_size
)
1029 && simple_cst_equal (unpadded_size
, TYPE_SIZE (rli
->t
)) == 0)
1030 warning ("padding struct size to alignment boundary");
1032 if (warn_packed
&& TREE_CODE (rli
->t
) == RECORD_TYPE
1033 && TYPE_PACKED (rli
->t
) && ! rli
->packed_maybe_necessary
1034 && TREE_CONSTANT (unpadded_size
))
1038 #ifdef ROUND_TYPE_ALIGN
1040 = ROUND_TYPE_ALIGN (rli
->t
, TYPE_ALIGN (rli
->t
), rli
->unpacked_align
);
1042 rli
->unpacked_align
= MAX (TYPE_ALIGN (rli
->t
), rli
->unpacked_align
);
1045 #ifdef ROUND_TYPE_SIZE
1046 unpacked_size
= ROUND_TYPE_SIZE (rli
->t
, TYPE_SIZE (rli
->t
),
1047 rli
->unpacked_align
);
1049 unpacked_size
= round_up (TYPE_SIZE (rli
->t
), rli
->unpacked_align
);
1052 if (simple_cst_equal (unpacked_size
, TYPE_SIZE (rli
->t
)))
1054 TYPE_PACKED (rli
->t
) = 0;
1056 if (TYPE_NAME (rli
->t
))
1060 if (TREE_CODE (TYPE_NAME (rli
->t
)) == IDENTIFIER_NODE
)
1061 name
= IDENTIFIER_POINTER (TYPE_NAME (rli
->t
));
1063 name
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (rli
->t
)));
1065 if (STRICT_ALIGNMENT
)
1066 warning ("packed attribute causes inefficient alignment for `%s'", name
);
1068 warning ("packed attribute is unnecessary for `%s'", name
);
1072 if (STRICT_ALIGNMENT
)
1073 warning ("packed attribute causes inefficient alignment");
1075 warning ("packed attribute is unnecessary");
1081 /* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE). */
1084 compute_record_mode (type
)
1088 enum machine_mode mode
= VOIDmode
;
1090 /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
1091 However, if possible, we use a mode that fits in a register
1092 instead, in order to allow for better optimization down the
1094 TYPE_MODE (type
) = BLKmode
;
1096 if (! host_integerp (TYPE_SIZE (type
), 1))
1099 /* A record which has any BLKmode members must itself be
1100 BLKmode; it can't go in a register. Unless the member is
1101 BLKmode only because it isn't aligned. */
1102 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1104 unsigned HOST_WIDE_INT bitpos
;
1106 if (TREE_CODE (field
) != FIELD_DECL
)
1109 if (TREE_CODE (TREE_TYPE (field
)) == ERROR_MARK
1110 || (TYPE_MODE (TREE_TYPE (field
)) == BLKmode
1111 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field
)))
1112 || ! host_integerp (bit_position (field
), 1)
1113 || ! host_integerp (DECL_SIZE (field
), 1))
1116 bitpos
= int_bit_position (field
);
1118 /* Must be BLKmode if any field crosses a word boundary,
1119 since extract_bit_field can't handle that in registers. */
1120 if (bitpos
/ BITS_PER_WORD
1121 != ((tree_low_cst (DECL_SIZE (field
), 1) + bitpos
- 1)
1123 /* But there is no problem if the field is entire words. */
1124 && tree_low_cst (DECL_SIZE (field
), 1) % BITS_PER_WORD
!= 0)
1127 /* If this field is the whole struct, remember its mode so
1128 that, say, we can put a double in a class into a DF
1129 register instead of forcing it to live in the stack. */
1130 if (simple_cst_equal (TYPE_SIZE (type
), DECL_SIZE (field
)))
1131 mode
= DECL_MODE (field
);
1133 #ifdef MEMBER_TYPE_FORCES_BLK
1134 /* With some targets, eg. c4x, it is sub-optimal
1135 to access an aligned BLKmode structure as a scalar. */
1136 if (mode
== VOIDmode
&& MEMBER_TYPE_FORCES_BLK (field
))
1138 #endif /* MEMBER_TYPE_FORCES_BLK */
1141 /* If we only have one real field; use its mode. This only applies to
1142 RECORD_TYPE. This does not apply to unions. */
1143 if (TREE_CODE (type
) == RECORD_TYPE
&& mode
!= VOIDmode
)
1144 TYPE_MODE (type
) = mode
;
1146 TYPE_MODE (type
) = mode_for_size_tree (TYPE_SIZE (type
), MODE_INT
, 1);
1148 /* If structure's known alignment is less than what the scalar
1149 mode would need, and it matters, then stick with BLKmode. */
1150 if (TYPE_MODE (type
) != BLKmode
1152 && ! (TYPE_ALIGN (type
) >= BIGGEST_ALIGNMENT
1153 || TYPE_ALIGN (type
) >= GET_MODE_ALIGNMENT (TYPE_MODE (type
))))
1155 /* If this is the only reason this type is BLKmode, then
1156 don't force containing types to be BLKmode. */
1157 TYPE_NO_FORCE_BLK (type
) = 1;
1158 TYPE_MODE (type
) = BLKmode
;
1162 /* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
1166 finalize_type_size (type
)
1169 /* Normally, use the alignment corresponding to the mode chosen.
1170 However, where strict alignment is not required, avoid
1171 over-aligning structures, since most compilers do not do this
1174 if (TYPE_MODE (type
) != BLKmode
&& TYPE_MODE (type
) != VOIDmode
1175 && (STRICT_ALIGNMENT
1176 || (TREE_CODE (type
) != RECORD_TYPE
&& TREE_CODE (type
) != UNION_TYPE
1177 && TREE_CODE (type
) != QUAL_UNION_TYPE
1178 && TREE_CODE (type
) != ARRAY_TYPE
)))
1180 TYPE_ALIGN (type
) = GET_MODE_ALIGNMENT (TYPE_MODE (type
));
1181 TYPE_USER_ALIGN (type
) = 0;
1184 /* Do machine-dependent extra alignment. */
1185 #ifdef ROUND_TYPE_ALIGN
1187 = ROUND_TYPE_ALIGN (type
, TYPE_ALIGN (type
), BITS_PER_UNIT
);
1190 /* If we failed to find a simple way to calculate the unit size
1191 of the type, find it by division. */
1192 if (TYPE_SIZE_UNIT (type
) == 0 && TYPE_SIZE (type
) != 0)
1193 /* TYPE_SIZE (type) is computed in bitsizetype. After the division, the
1194 result will fit in sizetype. We will get more efficient code using
1195 sizetype, so we force a conversion. */
1196 TYPE_SIZE_UNIT (type
)
1197 = convert (sizetype
,
1198 size_binop (FLOOR_DIV_EXPR
, TYPE_SIZE (type
),
1199 bitsize_unit_node
));
1201 if (TYPE_SIZE (type
) != 0)
1203 #ifdef ROUND_TYPE_SIZE
1205 = ROUND_TYPE_SIZE (type
, TYPE_SIZE (type
), TYPE_ALIGN (type
));
1206 TYPE_SIZE_UNIT (type
)
1207 = ROUND_TYPE_SIZE_UNIT (type
, TYPE_SIZE_UNIT (type
),
1208 TYPE_ALIGN (type
) / BITS_PER_UNIT
);
1210 TYPE_SIZE (type
) = round_up (TYPE_SIZE (type
), TYPE_ALIGN (type
));
1211 TYPE_SIZE_UNIT (type
)
1212 = round_up (TYPE_SIZE_UNIT (type
), TYPE_ALIGN (type
) / BITS_PER_UNIT
);
1216 /* Evaluate nonconstant sizes only once, either now or as soon as safe. */
1217 if (TYPE_SIZE (type
) != 0 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
1218 TYPE_SIZE (type
) = variable_size (TYPE_SIZE (type
));
1219 if (TYPE_SIZE_UNIT (type
) != 0
1220 && TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
)
1221 TYPE_SIZE_UNIT (type
) = variable_size (TYPE_SIZE_UNIT (type
));
1223 /* Also layout any other variants of the type. */
1224 if (TYPE_NEXT_VARIANT (type
)
1225 || type
!= TYPE_MAIN_VARIANT (type
))
1228 /* Record layout info of this variant. */
1229 tree size
= TYPE_SIZE (type
);
1230 tree size_unit
= TYPE_SIZE_UNIT (type
);
1231 unsigned int align
= TYPE_ALIGN (type
);
1232 unsigned int user_align
= TYPE_USER_ALIGN (type
);
1233 enum machine_mode mode
= TYPE_MODE (type
);
1235 /* Copy it into all variants. */
1236 for (variant
= TYPE_MAIN_VARIANT (type
);
1238 variant
= TYPE_NEXT_VARIANT (variant
))
1240 TYPE_SIZE (variant
) = size
;
1241 TYPE_SIZE_UNIT (variant
) = size_unit
;
1242 TYPE_ALIGN (variant
) = align
;
1243 TYPE_USER_ALIGN (variant
) = user_align
;
1244 TYPE_MODE (variant
) = mode
;
1249 /* Do all of the work required to layout the type indicated by RLI,
1250 once the fields have been laid out. This function will call `free'
1254 finish_record_layout (rli
)
1255 record_layout_info rli
;
1257 /* Compute the final size. */
1258 finalize_record_size (rli
);
1260 /* Compute the TYPE_MODE for the record. */
1261 compute_record_mode (rli
->t
);
1263 /* Perform any last tweaks to the TYPE_SIZE, etc. */
1264 finalize_type_size (rli
->t
);
1266 /* Lay out any static members. This is done now because their type
1267 may use the record's type. */
1268 while (rli
->pending_statics
)
1270 layout_decl (TREE_VALUE (rli
->pending_statics
), 0);
1271 rli
->pending_statics
= TREE_CHAIN (rli
->pending_statics
);
1278 /* Calculate the mode, size, and alignment for TYPE.
1279 For an array type, calculate the element separation as well.
1280 Record TYPE on the chain of permanent or temporary types
1281 so that dbxout will find out about it.
1283 TYPE_SIZE of a type is nonzero if the type has been laid out already.
1284 layout_type does nothing on such a type.
1286 If the type is incomplete, its TYPE_SIZE remains zero. */
1295 /* Do nothing if type has been laid out before. */
1296 if (TYPE_SIZE (type
))
1299 switch (TREE_CODE (type
))
1302 /* This kind of type is the responsibility
1303 of the language-specific code. */
1306 case BOOLEAN_TYPE
: /* Used for Java, Pascal, and Chill. */
1307 if (TYPE_PRECISION (type
) == 0)
1308 TYPE_PRECISION (type
) = 1; /* default to one byte/boolean. */
1310 /* ... fall through ... */
1315 if (TREE_CODE (TYPE_MIN_VALUE (type
)) == INTEGER_CST
1316 && tree_int_cst_sgn (TYPE_MIN_VALUE (type
)) >= 0)
1317 TREE_UNSIGNED (type
) = 1;
1319 TYPE_MODE (type
) = smallest_mode_for_size (TYPE_PRECISION (type
),
1321 TYPE_SIZE (type
) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
1322 TYPE_SIZE_UNIT (type
) = size_int (GET_MODE_SIZE (TYPE_MODE (type
)));
1326 TYPE_MODE (type
) = mode_for_size (TYPE_PRECISION (type
), MODE_FLOAT
, 0);
1327 TYPE_SIZE (type
) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
1328 TYPE_SIZE_UNIT (type
) = size_int (GET_MODE_SIZE (TYPE_MODE (type
)));
1332 TREE_UNSIGNED (type
) = TREE_UNSIGNED (TREE_TYPE (type
));
1334 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type
)),
1335 (TREE_CODE (TREE_TYPE (type
)) == INTEGER_TYPE
1336 ? MODE_COMPLEX_INT
: MODE_COMPLEX_FLOAT
),
1338 TYPE_SIZE (type
) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
1339 TYPE_SIZE_UNIT (type
) = size_int (GET_MODE_SIZE (TYPE_MODE (type
)));
1346 subtype
= TREE_TYPE (type
);
1347 TREE_UNSIGNED (type
) = TREE_UNSIGNED (subtype
);
1348 TYPE_SIZE (type
) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
1349 TYPE_SIZE_UNIT (type
) = size_int (GET_MODE_SIZE (TYPE_MODE (type
)));
1354 /* This is an incomplete type and so doesn't have a size. */
1355 TYPE_ALIGN (type
) = 1;
1356 TYPE_USER_ALIGN (type
) = 0;
1357 TYPE_MODE (type
) = VOIDmode
;
1361 TYPE_SIZE (type
) = bitsize_int (POINTER_SIZE
);
1362 TYPE_SIZE_UNIT (type
) = size_int (POINTER_SIZE
/ BITS_PER_UNIT
);
1363 /* A pointer might be MODE_PARTIAL_INT,
1364 but ptrdiff_t must be integral. */
1365 TYPE_MODE (type
) = mode_for_size (POINTER_SIZE
, MODE_INT
, 0);
1370 TYPE_MODE (type
) = mode_for_size (2 * POINTER_SIZE
, MODE_INT
, 0);
1371 TYPE_SIZE (type
) = bitsize_int (2 * POINTER_SIZE
);
1372 TYPE_SIZE_UNIT (type
) = size_int ((2 * POINTER_SIZE
) / BITS_PER_UNIT
);
1376 case REFERENCE_TYPE
:
1378 int nbits
= ((TREE_CODE (type
) == REFERENCE_TYPE
1379 && reference_types_internal
)
1380 ? GET_MODE_BITSIZE (Pmode
) : POINTER_SIZE
);
1382 TYPE_MODE (type
) = nbits
== POINTER_SIZE
? ptr_mode
: Pmode
;
1383 TYPE_SIZE (type
) = bitsize_int (nbits
);
1384 TYPE_SIZE_UNIT (type
) = size_int (nbits
/ BITS_PER_UNIT
);
1385 TREE_UNSIGNED (type
) = 1;
1386 TYPE_PRECISION (type
) = nbits
;
1392 register tree index
= TYPE_DOMAIN (type
);
1393 register tree element
= TREE_TYPE (type
);
1395 build_pointer_type (element
);
1397 /* We need to know both bounds in order to compute the size. */
1398 if (index
&& TYPE_MAX_VALUE (index
) && TYPE_MIN_VALUE (index
)
1399 && TYPE_SIZE (element
))
1401 tree ub
= TYPE_MAX_VALUE (index
);
1402 tree lb
= TYPE_MIN_VALUE (index
);
1406 /* The initial subtraction should happen in the original type so
1407 that (possible) negative values are handled appropriately. */
1408 length
= size_binop (PLUS_EXPR
, size_one_node
,
1410 fold (build (MINUS_EXPR
,
1414 /* Special handling for arrays of bits (for Chill). */
1415 element_size
= TYPE_SIZE (element
);
1416 if (TYPE_PACKED (type
) && INTEGRAL_TYPE_P (element
)
1417 && (integer_zerop (TYPE_MAX_VALUE (element
))
1418 || integer_onep (TYPE_MAX_VALUE (element
)))
1419 && host_integerp (TYPE_MIN_VALUE (element
), 1))
1421 HOST_WIDE_INT maxvalue
1422 = tree_low_cst (TYPE_MAX_VALUE (element
), 1);
1423 HOST_WIDE_INT minvalue
1424 = tree_low_cst (TYPE_MIN_VALUE (element
), 1);
1426 if (maxvalue
- minvalue
== 1
1427 && (maxvalue
== 1 || maxvalue
== 0))
1428 element_size
= integer_one_node
;
1431 TYPE_SIZE (type
) = size_binop (MULT_EXPR
, element_size
,
1432 convert (bitsizetype
, length
));
1434 /* If we know the size of the element, calculate the total
1435 size directly, rather than do some division thing below.
1436 This optimization helps Fortran assumed-size arrays
1437 (where the size of the array is determined at runtime)
1439 Note that we can't do this in the case where the size of
1440 the elements is one bit since TYPE_SIZE_UNIT cannot be
1441 set correctly in that case. */
1442 if (TYPE_SIZE_UNIT (element
) != 0 && ! integer_onep (element_size
))
1443 TYPE_SIZE_UNIT (type
)
1444 = size_binop (MULT_EXPR
, TYPE_SIZE_UNIT (element
), length
);
1446 /* Complain if the user has requested an array too large to
1448 if (TREE_CODE (TYPE_SIZE (type
)) == INTEGER_CST
1449 && TREE_OVERFLOW (TYPE_SIZE (type
)))
1451 error ("requested array too large for target");
1453 /* Avoid crashing later. */
1454 TYPE_SIZE (type
) = element_size
;
1455 if (TYPE_SIZE_UNIT (type
))
1456 TYPE_SIZE_UNIT (type
) = TYPE_SIZE_UNIT (element
);
1460 /* Now round the alignment and size,
1461 using machine-dependent criteria if any. */
1463 #ifdef ROUND_TYPE_ALIGN
1465 = ROUND_TYPE_ALIGN (type
, TYPE_ALIGN (element
), BITS_PER_UNIT
);
1467 TYPE_ALIGN (type
) = MAX (TYPE_ALIGN (element
), BITS_PER_UNIT
);
1470 #ifdef ROUND_TYPE_SIZE
1471 if (TYPE_SIZE (type
) != 0)
1474 = ROUND_TYPE_SIZE (type
, TYPE_SIZE (type
), TYPE_ALIGN (type
));
1476 /* If the rounding changed the size of the type, remove any
1477 pre-calculated TYPE_SIZE_UNIT. */
1478 if (simple_cst_equal (TYPE_SIZE (type
), tmp
) != 1)
1479 TYPE_SIZE_UNIT (type
) = NULL
;
1481 TYPE_SIZE (type
) = tmp
;
1485 TYPE_MODE (type
) = BLKmode
;
1486 if (TYPE_SIZE (type
) != 0
1487 #ifdef MEMBER_TYPE_FORCES_BLK
1488 && ! MEMBER_TYPE_FORCES_BLK (type
)
1490 /* BLKmode elements force BLKmode aggregate;
1491 else extract/store fields may lose. */
1492 && (TYPE_MODE (TREE_TYPE (type
)) != BLKmode
1493 || TYPE_NO_FORCE_BLK (TREE_TYPE (type
))))
1496 = mode_for_size_tree (TYPE_SIZE (type
), MODE_INT
, 1);
1498 if (TYPE_MODE (type
) != BLKmode
1499 && STRICT_ALIGNMENT
&& TYPE_ALIGN (type
) < BIGGEST_ALIGNMENT
1500 && TYPE_ALIGN (type
) < GET_MODE_ALIGNMENT (TYPE_MODE (type
))
1501 && TYPE_MODE (type
) != BLKmode
)
1503 TYPE_NO_FORCE_BLK (type
) = 1;
1504 TYPE_MODE (type
) = BLKmode
;
1512 case QUAL_UNION_TYPE
:
1515 record_layout_info rli
;
1517 /* Initialize the layout information. */
1518 rli
= start_record_layout (type
);
1520 /* If this is a QUAL_UNION_TYPE, we want to process the fields
1521 in the reverse order in building the COND_EXPR that denotes
1522 its size. We reverse them again later. */
1523 if (TREE_CODE (type
) == QUAL_UNION_TYPE
)
1524 TYPE_FIELDS (type
) = nreverse (TYPE_FIELDS (type
));
1526 /* Place all the fields. */
1527 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1528 place_field (rli
, field
);
1530 if (TREE_CODE (type
) == QUAL_UNION_TYPE
)
1531 TYPE_FIELDS (type
) = nreverse (TYPE_FIELDS (type
));
1533 if (lang_adjust_rli
)
1534 (*lang_adjust_rli
) (rli
);
1536 /* Finish laying out the record. */
1537 finish_record_layout (rli
);
1541 case SET_TYPE
: /* Used by Chill and Pascal. */
1542 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))) != INTEGER_CST
1543 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type
))) != INTEGER_CST
)
1547 #ifndef SET_WORD_SIZE
1548 #define SET_WORD_SIZE BITS_PER_WORD
1550 unsigned int alignment
1551 = set_alignment
? set_alignment
: SET_WORD_SIZE
;
1553 = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
1554 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type
))) + 1);
1556 = ((size_in_bits
+ alignment
- 1) / alignment
) * alignment
;
1558 if (rounded_size
> (int) alignment
)
1559 TYPE_MODE (type
) = BLKmode
;
1561 TYPE_MODE (type
) = mode_for_size (alignment
, MODE_INT
, 1);
1563 TYPE_SIZE (type
) = bitsize_int (rounded_size
);
1564 TYPE_SIZE_UNIT (type
) = size_int (rounded_size
/ BITS_PER_UNIT
);
1565 TYPE_ALIGN (type
) = alignment
;
1566 TYPE_USER_ALIGN (type
) = 0;
1567 TYPE_PRECISION (type
) = size_in_bits
;
1572 /* The size may vary in different languages, so the language front end
1573 should fill in the size. */
1574 TYPE_ALIGN (type
) = BIGGEST_ALIGNMENT
;
1575 TYPE_USER_ALIGN (type
) = 0;
1576 TYPE_MODE (type
) = BLKmode
;
1583 /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE. For
1584 records and unions, finish_record_layout already called this
1586 if (TREE_CODE (type
) != RECORD_TYPE
1587 && TREE_CODE (type
) != UNION_TYPE
1588 && TREE_CODE (type
) != QUAL_UNION_TYPE
)
1589 finalize_type_size (type
);
1591 /* If this type is created before sizetype has been permanently set,
1592 record it so set_sizetype can fix it up. */
1594 early_type_list
= tree_cons (NULL_TREE
, type
, early_type_list
);
1596 /* If an alias set has been set for this aggregate when it was incomplete,
1597 force it into alias set 0.
1598 This is too conservative, but we cannot call record_component_aliases
1599 here because some frontends still change the aggregates after
1601 if (AGGREGATE_TYPE_P (type
) && TYPE_ALIAS_SET_KNOWN_P (type
))
1602 TYPE_ALIAS_SET (type
) = 0;
1605 /* Create and return a type for signed integers of PRECISION bits. */
1608 make_signed_type (precision
)
1611 register tree type
= make_node (INTEGER_TYPE
);
1613 TYPE_PRECISION (type
) = precision
;
1615 fixup_signed_type (type
);
1619 /* Create and return a type for unsigned integers of PRECISION bits. */
1622 make_unsigned_type (precision
)
1625 register tree type
= make_node (INTEGER_TYPE
);
1627 TYPE_PRECISION (type
) = precision
;
1629 fixup_unsigned_type (type
);
1633 /* Initialize sizetype and bitsizetype to a reasonable and temporary
1634 value to enable integer types to be created. */
1637 initialize_sizetypes ()
1639 tree t
= make_node (INTEGER_TYPE
);
1641 /* Set this so we do something reasonable for the build_int_2 calls
1643 integer_type_node
= t
;
1645 TYPE_MODE (t
) = SImode
;
1646 TYPE_ALIGN (t
) = GET_MODE_ALIGNMENT (SImode
);
1647 TYPE_USER_ALIGN (t
) = 0;
1648 TYPE_SIZE (t
) = build_int_2 (GET_MODE_BITSIZE (SImode
), 0);
1649 TYPE_SIZE_UNIT (t
) = build_int_2 (GET_MODE_SIZE (SImode
), 0);
1650 TREE_UNSIGNED (t
) = 1;
1651 TYPE_PRECISION (t
) = GET_MODE_BITSIZE (SImode
);
1652 TYPE_MIN_VALUE (t
) = build_int_2 (0, 0);
1653 TYPE_IS_SIZETYPE (t
) = 1;
1655 /* 1000 avoids problems with possible overflow and is certainly
1656 larger than any size value we'd want to be storing. */
1657 TYPE_MAX_VALUE (t
) = build_int_2 (1000, 0);
1659 /* These two must be different nodes because of the caching done in
1662 bitsizetype
= copy_node (t
);
1663 integer_type_node
= 0;
1666 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1667 Also update the type of any standard type's sizes made so far. */
1673 int oprecision
= TYPE_PRECISION (type
);
1674 /* The *bitsizetype types use a precision that avoids overflows when
1675 calculating signed sizes / offsets in bits. However, when
1676 cross-compiling from a 32 bit to a 64 bit host, we are limited to 64 bit
1678 int precision
= MIN (oprecision
+ BITS_PER_UNIT_LOG
+ 1,
1679 2 * HOST_BITS_PER_WIDE_INT
);
1686 /* Make copies of nodes since we'll be setting TYPE_IS_SIZETYPE. */
1687 sizetype
= copy_node (type
);
1688 TYPE_DOMAIN (sizetype
) = type
;
1689 TYPE_IS_SIZETYPE (sizetype
) = 1;
1690 bitsizetype
= make_node (INTEGER_TYPE
);
1691 TYPE_NAME (bitsizetype
) = TYPE_NAME (type
);
1692 TYPE_PRECISION (bitsizetype
) = precision
;
1693 TYPE_IS_SIZETYPE (bitsizetype
) = 1;
1695 if (TREE_UNSIGNED (type
))
1696 fixup_unsigned_type (bitsizetype
);
1698 fixup_signed_type (bitsizetype
);
1700 layout_type (bitsizetype
);
1702 if (TREE_UNSIGNED (type
))
1704 usizetype
= sizetype
;
1705 ubitsizetype
= bitsizetype
;
1706 ssizetype
= copy_node (make_signed_type (oprecision
));
1707 sbitsizetype
= copy_node (make_signed_type (precision
));
1711 ssizetype
= sizetype
;
1712 sbitsizetype
= bitsizetype
;
1713 usizetype
= copy_node (make_unsigned_type (oprecision
));
1714 ubitsizetype
= copy_node (make_unsigned_type (precision
));
1717 TYPE_NAME (bitsizetype
) = get_identifier ("bit_size_type");
1719 /* Show is a sizetype, is a main type, and has no pointers to it. */
1720 for (i
= 0; i
< ARRAY_SIZE (sizetype_tab
); i
++)
1722 TYPE_IS_SIZETYPE (sizetype_tab
[i
]) = 1;
1723 TYPE_MAIN_VARIANT (sizetype_tab
[i
]) = sizetype_tab
[i
];
1724 TYPE_NEXT_VARIANT (sizetype_tab
[i
]) = 0;
1725 TYPE_POINTER_TO (sizetype_tab
[i
]) = 0;
1726 TYPE_REFERENCE_TO (sizetype_tab
[i
]) = 0;
1729 ggc_add_tree_root ((tree
*) &sizetype_tab
,
1730 sizeof sizetype_tab
/ sizeof (tree
));
1732 /* Go down each of the types we already made and set the proper type
1733 for the sizes in them. */
1734 for (t
= early_type_list
; t
!= 0; t
= TREE_CHAIN (t
))
1736 if (TREE_CODE (TREE_VALUE (t
)) != INTEGER_TYPE
)
1739 TREE_TYPE (TYPE_SIZE (TREE_VALUE (t
))) = bitsizetype
;
1740 TREE_TYPE (TYPE_SIZE_UNIT (TREE_VALUE (t
))) = sizetype
;
1743 early_type_list
= 0;
1747 /* Set the extreme values of TYPE based on its precision in bits,
1748 then lay it out. Used when make_signed_type won't do
1749 because the tree code is not INTEGER_TYPE.
1750 E.g. for Pascal, when the -fsigned-char option is given. */
1753 fixup_signed_type (type
)
1756 register int precision
= TYPE_PRECISION (type
);
1758 TYPE_MIN_VALUE (type
)
1759 = build_int_2 ((precision
- HOST_BITS_PER_WIDE_INT
> 0
1760 ? 0 : (HOST_WIDE_INT
) (-1) << (precision
- 1)),
1761 (((HOST_WIDE_INT
) (-1)
1762 << (precision
- HOST_BITS_PER_WIDE_INT
- 1 > 0
1763 ? precision
- HOST_BITS_PER_WIDE_INT
- 1
1765 TYPE_MAX_VALUE (type
)
1766 = build_int_2 ((precision
- HOST_BITS_PER_WIDE_INT
> 0
1767 ? -1 : ((HOST_WIDE_INT
) 1 << (precision
- 1)) - 1),
1768 (precision
- HOST_BITS_PER_WIDE_INT
- 1 > 0
1769 ? (((HOST_WIDE_INT
) 1
1770 << (precision
- HOST_BITS_PER_WIDE_INT
- 1))) - 1
1773 TREE_TYPE (TYPE_MIN_VALUE (type
)) = type
;
1774 TREE_TYPE (TYPE_MAX_VALUE (type
)) = type
;
1776 /* Lay out the type: set its alignment, size, etc. */
1780 /* Set the extreme values of TYPE based on its precision in bits,
1781 then lay it out. This is used both in `make_unsigned_type'
1782 and for enumeral types. */
1785 fixup_unsigned_type (type
)
1788 register int precision
= TYPE_PRECISION (type
);
1790 TYPE_MIN_VALUE (type
) = build_int_2 (0, 0);
1791 TYPE_MAX_VALUE (type
)
1792 = build_int_2 (precision
- HOST_BITS_PER_WIDE_INT
>= 0
1793 ? -1 : ((HOST_WIDE_INT
) 1 << precision
) - 1,
1794 precision
- HOST_BITS_PER_WIDE_INT
> 0
1795 ? ((unsigned HOST_WIDE_INT
) ~0
1796 >> (HOST_BITS_PER_WIDE_INT
1797 - (precision
- HOST_BITS_PER_WIDE_INT
)))
1799 TREE_TYPE (TYPE_MIN_VALUE (type
)) = type
;
1800 TREE_TYPE (TYPE_MAX_VALUE (type
)) = type
;
1802 /* Lay out the type: set its alignment, size, etc. */
1806 /* Find the best machine mode to use when referencing a bit field of length
1807 BITSIZE bits starting at BITPOS.
1809 The underlying object is known to be aligned to a boundary of ALIGN bits.
1810 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1811 larger than LARGEST_MODE (usually SImode).
1813 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1814 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1815 mode meeting these conditions.
1817 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1818 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1819 all the conditions. */
1822 get_best_mode (bitsize
, bitpos
, align
, largest_mode
, volatilep
)
1823 int bitsize
, bitpos
;
1825 enum machine_mode largest_mode
;
1828 enum machine_mode mode
;
1829 unsigned int unit
= 0;
1831 /* Find the narrowest integer mode that contains the bit field. */
1832 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); mode
!= VOIDmode
;
1833 mode
= GET_MODE_WIDER_MODE (mode
))
1835 unit
= GET_MODE_BITSIZE (mode
);
1836 if ((bitpos
% unit
) + bitsize
<= unit
)
1840 if (mode
== VOIDmode
1841 /* It is tempting to omit the following line
1842 if STRICT_ALIGNMENT is true.
1843 But that is incorrect, since if the bitfield uses part of 3 bytes
1844 and we use a 4-byte mode, we could get a spurious segv
1845 if the extra 4th byte is past the end of memory.
1846 (Though at least one Unix compiler ignores this problem:
1847 that on the Sequent 386 machine. */
1848 || MIN (unit
, BIGGEST_ALIGNMENT
) > align
1849 || (largest_mode
!= VOIDmode
&& unit
> GET_MODE_BITSIZE (largest_mode
)))
1852 if (SLOW_BYTE_ACCESS
&& ! volatilep
)
1854 enum machine_mode wide_mode
= VOIDmode
, tmode
;
1856 for (tmode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); tmode
!= VOIDmode
;
1857 tmode
= GET_MODE_WIDER_MODE (tmode
))
1859 unit
= GET_MODE_BITSIZE (tmode
);
1860 if (bitpos
/ unit
== (bitpos
+ bitsize
- 1) / unit
1861 && unit
<= BITS_PER_WORD
1862 && unit
<= MIN (align
, BIGGEST_ALIGNMENT
)
1863 && (largest_mode
== VOIDmode
1864 || unit
<= GET_MODE_BITSIZE (largest_mode
)))
1868 if (wide_mode
!= VOIDmode
)
1875 /* Return the alignment of MODE. This will be bounded by 1 and
1876 BIGGEST_ALIGNMENT. */
1879 get_mode_alignment (mode
)
1880 enum machine_mode mode
;
1882 unsigned int alignment
= GET_MODE_UNIT_SIZE (mode
);
1884 /* Extract the LSB of the size. */
1885 alignment
= alignment
& -alignment
;
1886 alignment
*= BITS_PER_UNIT
;
1888 alignment
= MIN (BIGGEST_ALIGNMENT
, MAX (1, alignment
));
1892 /* This function is run once to initialize stor-layout.c. */
1895 init_stor_layout_once ()
1897 ggc_add_tree_root (&pending_sizes
, 1);