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 GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
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_PTR
, 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
) = TYPE_USER_ALIGN (type
);
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 /* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
448 QUAL_UNION_TYPE. Return a pointer to a struct record_layout_info which
449 is to be passed to all other layout functions for this record. It is the
450 responsibility of the caller to call `free' for the storage returned.
451 Note that garbage collection is not permitted until we finish laying
455 start_record_layout (t
)
458 record_layout_info rli
459 = (record_layout_info
) xmalloc (sizeof (struct record_layout_info_s
));
463 /* If the type has a minimum specified alignment (via an attribute
464 declaration, for example) use it -- otherwise, start with a
465 one-byte alignment. */
466 rli
->record_align
= MAX (BITS_PER_UNIT
, TYPE_ALIGN (t
));
467 rli
->unpacked_align
= rli
->record_align
;
468 rli
->offset_align
= MAX (rli
->record_align
, BIGGEST_ALIGNMENT
);
470 #ifdef STRUCTURE_SIZE_BOUNDARY
471 /* Packed structures don't need to have minimum size. */
472 if (! TYPE_PACKED (t
))
473 rli
->record_align
= MAX (rli
->record_align
, STRUCTURE_SIZE_BOUNDARY
);
476 rli
->offset
= size_zero_node
;
477 rli
->bitpos
= bitsize_zero_node
;
478 rli
->pending_statics
= 0;
479 rli
->packed_maybe_necessary
= 0;
484 /* These four routines perform computations that convert between
485 the offset/bitpos forms and byte and bit offsets. */
488 bit_from_pos (offset
, bitpos
)
491 return size_binop (PLUS_EXPR
, bitpos
,
492 size_binop (MULT_EXPR
, convert (bitsizetype
, offset
),
497 byte_from_pos (offset
, bitpos
)
500 return size_binop (PLUS_EXPR
, offset
,
502 size_binop (TRUNC_DIV_EXPR
, bitpos
,
503 bitsize_unit_node
)));
507 pos_from_byte (poffset
, pbitpos
, off_align
, pos
)
508 tree
*poffset
, *pbitpos
;
509 unsigned int off_align
;
513 = size_binop (MULT_EXPR
,
515 size_binop (FLOOR_DIV_EXPR
, pos
,
516 bitsize_int (off_align
518 size_int (off_align
/ BITS_PER_UNIT
));
519 *pbitpos
= size_binop (MULT_EXPR
,
520 size_binop (FLOOR_MOD_EXPR
, pos
,
521 bitsize_int (off_align
/ BITS_PER_UNIT
)),
526 pos_from_bit (poffset
, pbitpos
, off_align
, pos
)
527 tree
*poffset
, *pbitpos
;
528 unsigned int off_align
;
531 *poffset
= size_binop (MULT_EXPR
,
533 size_binop (FLOOR_DIV_EXPR
, pos
,
534 bitsize_int (off_align
))),
535 size_int (off_align
/ BITS_PER_UNIT
));
536 *pbitpos
= size_binop (FLOOR_MOD_EXPR
, pos
, bitsize_int (off_align
));
539 /* Given a pointer to bit and byte offsets and an offset alignment,
540 normalize the offsets so they are within the alignment. */
543 normalize_offset (poffset
, pbitpos
, off_align
)
544 tree
*poffset
, *pbitpos
;
545 unsigned int off_align
;
547 /* If the bit position is now larger than it should be, adjust it
549 if (compare_tree_int (*pbitpos
, off_align
) >= 0)
551 tree extra_aligns
= size_binop (FLOOR_DIV_EXPR
, *pbitpos
,
552 bitsize_int (off_align
));
555 = size_binop (PLUS_EXPR
, *poffset
,
556 size_binop (MULT_EXPR
, convert (sizetype
, extra_aligns
),
557 size_int (off_align
/ BITS_PER_UNIT
)));
560 = size_binop (FLOOR_MOD_EXPR
, *pbitpos
, bitsize_int (off_align
));
564 /* Print debugging information about the information in RLI. */
568 record_layout_info rli
;
570 print_node_brief (stderr
, "type", rli
->t
, 0);
571 print_node_brief (stderr
, "\noffset", rli
->offset
, 0);
572 print_node_brief (stderr
, " bitpos", rli
->bitpos
, 0);
574 fprintf (stderr
, "\nrec_align = %u, unpack_align = %u, off_align = %u\n",
575 rli
->record_align
, rli
->unpacked_align
, rli
->offset_align
);
576 if (rli
->packed_maybe_necessary
)
577 fprintf (stderr
, "packed may be necessary\n");
579 if (rli
->pending_statics
)
581 fprintf (stderr
, "pending statics:\n");
582 debug_tree (rli
->pending_statics
);
586 /* Given an RLI with a possibly-incremented BITPOS, adjust OFFSET and
587 BITPOS if necessary to keep BITPOS below OFFSET_ALIGN. */
591 record_layout_info rli
;
593 normalize_offset (&rli
->offset
, &rli
->bitpos
, rli
->offset_align
);
596 /* Returns the size in bytes allocated so far. */
599 rli_size_unit_so_far (rli
)
600 record_layout_info rli
;
602 return byte_from_pos (rli
->offset
, rli
->bitpos
);
605 /* Returns the size in bits allocated so far. */
608 rli_size_so_far (rli
)
609 record_layout_info rli
;
611 return bit_from_pos (rli
->offset
, rli
->bitpos
);
614 /* Called from place_field to handle unions. */
617 place_union_field (rli
, field
)
618 record_layout_info rli
;
621 unsigned int desired_align
;
623 layout_decl (field
, 0);
625 DECL_FIELD_OFFSET (field
) = size_zero_node
;
626 DECL_FIELD_BIT_OFFSET (field
) = bitsize_zero_node
;
627 SET_DECL_OFFSET_ALIGN (field
, BIGGEST_ALIGNMENT
);
629 desired_align
= DECL_ALIGN (field
);
631 #ifdef BIGGEST_FIELD_ALIGNMENT
632 /* Some targets (i.e. i386) limit union field alignment
633 to a lower boundary than alignment of variables unless
634 it was overridden by attribute aligned. */
635 if (! DECL_USER_ALIGN (field
))
637 MIN (desired_align
, (unsigned) BIGGEST_FIELD_ALIGNMENT
);
640 /* Union must be at least as aligned as any field requires. */
641 rli
->record_align
= MAX (rli
->record_align
, desired_align
);
643 #ifdef PCC_BITFIELD_TYPE_MATTERS
644 /* On the m88000, a bit field of declare type `int' forces the
645 entire union to have `int' alignment. */
646 if (PCC_BITFIELD_TYPE_MATTERS
&& DECL_BIT_FIELD_TYPE (field
))
647 rli
->record_align
= MAX (rli
->record_align
,
648 TYPE_ALIGN (TREE_TYPE (field
)));
651 /* We assume the union's size will be a multiple of a byte so we don't
652 bother with BITPOS. */
653 if (TREE_CODE (rli
->t
) == UNION_TYPE
)
654 rli
->offset
= size_binop (MAX_EXPR
, rli
->offset
, DECL_SIZE_UNIT (field
));
655 else if (TREE_CODE (rli
->t
) == QUAL_UNION_TYPE
)
656 rli
->offset
= fold (build (COND_EXPR
, sizetype
,
657 DECL_QUALIFIER (field
),
658 DECL_SIZE_UNIT (field
), rli
->offset
));
661 /* RLI contains information about the layout of a RECORD_TYPE. FIELD
662 is a FIELD_DECL to be added after those fields already present in
663 T. (FIELD is not actually added to the TYPE_FIELDS list here;
664 callers that desire that behavior must manually perform that step.) */
667 place_field (rli
, field
)
668 record_layout_info rli
;
671 /* The alignment required for FIELD. */
672 unsigned int desired_align
;
673 /* The alignment FIELD would have if we just dropped it into the
674 record as it presently stands. */
675 unsigned int known_align
;
676 unsigned int actual_align
;
677 unsigned int user_align
;
678 /* The type of this field. */
679 tree type
= TREE_TYPE (field
);
681 if (TREE_CODE (field
) == ERROR_MARK
|| TREE_CODE (type
) == ERROR_MARK
)
684 /* If FIELD is static, then treat it like a separate variable, not
685 really like a structure field. If it is a FUNCTION_DECL, it's a
686 method. In both cases, all we do is lay out the decl, and we do
687 it *after* the record is laid out. */
688 if (TREE_CODE (field
) == VAR_DECL
)
690 rli
->pending_statics
= tree_cons (NULL_TREE
, field
,
691 rli
->pending_statics
);
695 /* Enumerators and enum types which are local to this class need not
696 be laid out. Likewise for initialized constant fields. */
697 else if (TREE_CODE (field
) != FIELD_DECL
)
700 /* Unions are laid out very differently than records, so split
701 that code off to another function. */
702 else if (TREE_CODE (rli
->t
) != RECORD_TYPE
)
704 place_union_field (rli
, field
);
708 /* Work out the known alignment so far. Note that A & (-A) is the
709 value of the least-significant bit in A that is one. */
710 if (! integer_zerop (rli
->bitpos
))
711 known_align
= (tree_low_cst (rli
->bitpos
, 1)
712 & - tree_low_cst (rli
->bitpos
, 1));
713 else if (integer_zerop (rli
->offset
))
714 known_align
= BIGGEST_ALIGNMENT
;
715 else if (host_integerp (rli
->offset
, 1))
716 known_align
= (BITS_PER_UNIT
717 * (tree_low_cst (rli
->offset
, 1)
718 & - tree_low_cst (rli
->offset
, 1)));
720 known_align
= rli
->offset_align
;
722 /* Lay out the field so we know what alignment it needs. For a
723 packed field, use the alignment as specified, disregarding what
724 the type would want. */
725 desired_align
= DECL_ALIGN (field
);
726 user_align
= DECL_USER_ALIGN (field
);
727 layout_decl (field
, known_align
);
728 if (! DECL_PACKED (field
))
730 desired_align
= DECL_ALIGN (field
);
731 user_align
= DECL_USER_ALIGN (field
);
734 /* Some targets (i.e. i386, VMS) limit struct field alignment
735 to a lower boundary than alignment of variables unless
736 it was overridden by attribute aligned. */
737 #ifdef BIGGEST_FIELD_ALIGNMENT
740 = MIN (desired_align
, (unsigned) BIGGEST_FIELD_ALIGNMENT
);
743 #ifdef ADJUST_FIELD_ALIGN
744 desired_align
= ADJUST_FIELD_ALIGN (field
, desired_align
);
747 /* Record must have at least as much alignment as any field.
748 Otherwise, the alignment of the field within the record is
750 #ifdef PCC_BITFIELD_TYPE_MATTERS
751 if (PCC_BITFIELD_TYPE_MATTERS
&& type
!= error_mark_node
752 && DECL_BIT_FIELD_TYPE (field
)
753 && ! integer_zerop (TYPE_SIZE (type
)))
755 /* For these machines, a zero-length field does not
756 affect the alignment of the structure as a whole.
757 It does, however, affect the alignment of the next field
758 within the structure. */
759 if (! integer_zerop (DECL_SIZE (field
)))
760 rli
->record_align
= MAX (rli
->record_align
, desired_align
);
761 else if (! DECL_PACKED (field
))
762 desired_align
= TYPE_ALIGN (type
);
764 /* A named bit field of declared type `int'
765 forces the entire structure to have `int' alignment. */
766 if (DECL_NAME (field
) != 0)
768 unsigned int type_align
= TYPE_ALIGN (type
);
770 if (maximum_field_alignment
!= 0)
771 type_align
= MIN (type_align
, maximum_field_alignment
);
772 else if (DECL_PACKED (field
))
773 type_align
= MIN (type_align
, BITS_PER_UNIT
);
775 rli
->record_align
= MAX (rli
->record_align
, type_align
);
777 rli
->unpacked_align
= MAX (rli
->unpacked_align
,
784 rli
->record_align
= MAX (rli
->record_align
, desired_align
);
785 rli
->unpacked_align
= MAX (rli
->unpacked_align
, TYPE_ALIGN (type
));
788 if (warn_packed
&& DECL_PACKED (field
))
790 if (known_align
> TYPE_ALIGN (type
))
792 if (TYPE_ALIGN (type
) > desired_align
)
794 if (STRICT_ALIGNMENT
)
795 warning_with_decl (field
, "packed attribute causes inefficient alignment for `%s'");
797 warning_with_decl (field
, "packed attribute is unnecessary for `%s'");
801 rli
->packed_maybe_necessary
= 1;
804 /* Does this field automatically have alignment it needs by virtue
805 of the fields that precede it and the record's own alignment? */
806 if (known_align
< desired_align
)
808 /* No, we need to skip space before this field.
809 Bump the cumulative size to multiple of field alignment. */
812 warning_with_decl (field
, "padding struct to align `%s'");
814 /* If the alignment is still within offset_align, just align
816 if (desired_align
< rli
->offset_align
)
817 rli
->bitpos
= round_up (rli
->bitpos
, desired_align
);
820 /* First adjust OFFSET by the partial bits, then align. */
822 = size_binop (PLUS_EXPR
, rli
->offset
,
824 size_binop (CEIL_DIV_EXPR
, rli
->bitpos
,
825 bitsize_unit_node
)));
826 rli
->bitpos
= bitsize_zero_node
;
828 rli
->offset
= round_up (rli
->offset
, desired_align
/ BITS_PER_UNIT
);
831 if (! TREE_CONSTANT (rli
->offset
))
832 rli
->offset_align
= desired_align
;
836 /* Handle compatibility with PCC. Note that if the record has any
837 variable-sized fields, we need not worry about compatibility. */
838 #ifdef PCC_BITFIELD_TYPE_MATTERS
839 if (PCC_BITFIELD_TYPE_MATTERS
840 && TREE_CODE (field
) == FIELD_DECL
841 && type
!= error_mark_node
842 && DECL_BIT_FIELD (field
)
843 && ! DECL_PACKED (field
)
844 && maximum_field_alignment
== 0
845 && ! integer_zerop (DECL_SIZE (field
))
846 && host_integerp (DECL_SIZE (field
), 1)
847 && host_integerp (rli
->offset
, 1)
848 && host_integerp (TYPE_SIZE (type
), 1))
850 unsigned int type_align
= TYPE_ALIGN (type
);
851 tree dsize
= DECL_SIZE (field
);
852 HOST_WIDE_INT field_size
= tree_low_cst (dsize
, 1);
853 HOST_WIDE_INT offset
= tree_low_cst (rli
->offset
, 0);
854 HOST_WIDE_INT bit_offset
= tree_low_cst (rli
->bitpos
, 0);
856 /* A bit field may not span more units of alignment of its type
857 than its type itself. Advance to next boundary if necessary. */
858 if ((((offset
* BITS_PER_UNIT
+ bit_offset
+ field_size
+
861 - (offset
* BITS_PER_UNIT
+ bit_offset
) / type_align
)
862 > tree_low_cst (TYPE_SIZE (type
), 1) / type_align
)
863 rli
->bitpos
= round_up (rli
->bitpos
, type_align
);
867 #ifdef BITFIELD_NBYTES_LIMITED
868 if (BITFIELD_NBYTES_LIMITED
869 && TREE_CODE (field
) == FIELD_DECL
870 && type
!= error_mark_node
871 && DECL_BIT_FIELD_TYPE (field
)
872 && ! DECL_PACKED (field
)
873 && ! integer_zerop (DECL_SIZE (field
))
874 && host_integerp (DECL_SIZE (field
), 1)
875 && host_integerp (rli
->offset
, 1)
876 && host_integerp (TYPE_SIZE (type
), 1))
878 unsigned int type_align
= TYPE_ALIGN (type
);
879 tree dsize
= DECL_SIZE (field
);
880 HOST_WIDE_INT field_size
= tree_low_cst (dsize
, 1);
881 HOST_WIDE_INT offset
= tree_low_cst (rli
->offset
, 0);
882 HOST_WIDE_INT bit_offset
= tree_low_cst (rli
->bitpos
, 0);
884 if (maximum_field_alignment
!= 0)
885 type_align
= MIN (type_align
, maximum_field_alignment
);
886 /* ??? This test is opposite the test in the containing if
887 statement, so this code is unreachable currently. */
888 else if (DECL_PACKED (field
))
889 type_align
= MIN (type_align
, BITS_PER_UNIT
);
891 /* A bit field may not span the unit of alignment of its type.
892 Advance to next boundary if necessary. */
893 /* ??? This code should match the code above for the
894 PCC_BITFIELD_TYPE_MATTERS case. */
895 if ((offset
* BITS_PER_UNIT
+ bit_offset
) / type_align
896 != ((offset
* BITS_PER_UNIT
+ bit_offset
+ field_size
- 1)
898 rli
->bitpos
= round_up (rli
->bitpos
, type_align
);
902 /* Offset so far becomes the position of this field after normalizing. */
904 DECL_FIELD_OFFSET (field
) = rli
->offset
;
905 DECL_FIELD_BIT_OFFSET (field
) = rli
->bitpos
;
906 SET_DECL_OFFSET_ALIGN (field
, rli
->offset_align
);
908 /* If this field ended up more aligned than we thought it would be (we
909 approximate this by seeing if its position changed), lay out the field
910 again; perhaps we can use an integral mode for it now. */
911 if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field
)))
912 actual_align
= (tree_low_cst (DECL_FIELD_BIT_OFFSET (field
), 1)
913 & - tree_low_cst (DECL_FIELD_BIT_OFFSET (field
), 1));
914 else if (integer_zerop (DECL_FIELD_OFFSET (field
)))
915 actual_align
= BIGGEST_ALIGNMENT
;
916 else if (host_integerp (DECL_FIELD_OFFSET (field
), 1))
917 actual_align
= (BITS_PER_UNIT
918 * (tree_low_cst (DECL_FIELD_OFFSET (field
), 1)
919 & - tree_low_cst (DECL_FIELD_OFFSET (field
), 1)));
921 actual_align
= DECL_OFFSET_ALIGN (field
);
923 if (known_align
!= actual_align
)
924 layout_decl (field
, actual_align
);
926 /* Now add size of this field to the size of the record. If the size is
927 not constant, treat the field as being a multiple of bytes and just
928 adjust the offset, resetting the bit position. Otherwise, apportion the
929 size amongst the bit position and offset. First handle the case of an
930 unspecified size, which can happen when we have an invalid nested struct
931 definition, such as struct j { struct j { int i; } }. The error message
932 is printed in finish_struct. */
933 if (DECL_SIZE (field
) == 0)
935 else if (TREE_CODE (DECL_SIZE_UNIT (field
)) != INTEGER_CST
936 || TREE_CONSTANT_OVERFLOW (DECL_SIZE_UNIT (field
)))
939 = size_binop (PLUS_EXPR
, rli
->offset
,
941 size_binop (CEIL_DIV_EXPR
, rli
->bitpos
,
942 bitsize_unit_node
)));
944 = size_binop (PLUS_EXPR
, rli
->offset
, DECL_SIZE_UNIT (field
));
945 rli
->bitpos
= bitsize_zero_node
;
946 rli
->offset_align
= MIN (rli
->offset_align
, DECL_ALIGN (field
));
950 rli
->bitpos
= size_binop (PLUS_EXPR
, rli
->bitpos
, DECL_SIZE (field
));
955 /* Assuming that all the fields have been laid out, this function uses
956 RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
957 inidicated by RLI. */
960 finalize_record_size (rli
)
961 record_layout_info rli
;
963 tree unpadded_size
, unpadded_size_unit
;
965 /* Now we want just byte and bit offsets, so set the offset alignment
966 to be a byte and then normalize. */
967 rli
->offset_align
= BITS_PER_UNIT
;
970 /* Determine the desired alignment. */
971 #ifdef ROUND_TYPE_ALIGN
972 TYPE_ALIGN (rli
->t
) = ROUND_TYPE_ALIGN (rli
->t
, TYPE_ALIGN (rli
->t
),
975 TYPE_ALIGN (rli
->t
) = MAX (TYPE_ALIGN (rli
->t
), rli
->record_align
);
977 TYPE_USER_ALIGN (rli
->t
) = 1;
979 /* Compute the size so far. Be sure to allow for extra bits in the
980 size in bytes. We have guaranteed above that it will be no more
981 than a single byte. */
982 unpadded_size
= rli_size_so_far (rli
);
983 unpadded_size_unit
= rli_size_unit_so_far (rli
);
984 if (! integer_zerop (rli
->bitpos
))
986 = size_binop (PLUS_EXPR
, unpadded_size_unit
, size_one_node
);
988 /* Record the un-rounded size in the binfo node. But first we check
989 the size of TYPE_BINFO to make sure that BINFO_SIZE is available. */
990 if (TYPE_BINFO (rli
->t
) && TREE_VEC_LENGTH (TYPE_BINFO (rli
->t
)) > 6)
992 TYPE_BINFO_SIZE (rli
->t
) = unpadded_size
;
993 TYPE_BINFO_SIZE_UNIT (rli
->t
) = unpadded_size_unit
;
996 /* Round the size up to be a multiple of the required alignment */
997 #ifdef ROUND_TYPE_SIZE
998 TYPE_SIZE (rli
->t
) = ROUND_TYPE_SIZE (rli
->t
, unpadded_size
,
999 TYPE_ALIGN (rli
->t
));
1000 TYPE_SIZE_UNIT (rli
->t
)
1001 = ROUND_TYPE_SIZE_UNIT (rli
->t
, unpadded_size_unit
,
1002 TYPE_ALIGN (rli
->t
) / BITS_PER_UNIT
);
1004 TYPE_SIZE (rli
->t
) = round_up (unpadded_size
, TYPE_ALIGN (rli
->t
));
1005 TYPE_SIZE_UNIT (rli
->t
) = round_up (unpadded_size_unit
,
1006 TYPE_ALIGN (rli
->t
) / BITS_PER_UNIT
);
1009 if (warn_padded
&& TREE_CONSTANT (unpadded_size
)
1010 && simple_cst_equal (unpadded_size
, TYPE_SIZE (rli
->t
)) == 0)
1011 warning ("padding struct size to alignment boundary");
1013 if (warn_packed
&& TREE_CODE (rli
->t
) == RECORD_TYPE
1014 && TYPE_PACKED (rli
->t
) && ! rli
->packed_maybe_necessary
1015 && TREE_CONSTANT (unpadded_size
))
1019 #ifdef ROUND_TYPE_ALIGN
1021 = ROUND_TYPE_ALIGN (rli
->t
, TYPE_ALIGN (rli
->t
), rli
->unpacked_align
);
1023 rli
->unpacked_align
= MAX (TYPE_ALIGN (rli
->t
), rli
->unpacked_align
);
1026 #ifdef ROUND_TYPE_SIZE
1027 unpacked_size
= ROUND_TYPE_SIZE (rli
->t
, TYPE_SIZE (rli
->t
),
1028 rli
->unpacked_align
);
1030 unpacked_size
= round_up (TYPE_SIZE (rli
->t
), rli
->unpacked_align
);
1033 if (simple_cst_equal (unpacked_size
, TYPE_SIZE (rli
->t
)))
1035 TYPE_PACKED (rli
->t
) = 0;
1037 if (TYPE_NAME (rli
->t
))
1041 if (TREE_CODE (TYPE_NAME (rli
->t
)) == IDENTIFIER_NODE
)
1042 name
= IDENTIFIER_POINTER (TYPE_NAME (rli
->t
));
1044 name
= IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (rli
->t
)));
1046 if (STRICT_ALIGNMENT
)
1047 warning ("packed attribute causes inefficient alignment for `%s'", name
);
1049 warning ("packed attribute is unnecessary for `%s'", name
);
1053 if (STRICT_ALIGNMENT
)
1054 warning ("packed attribute causes inefficient alignment");
1056 warning ("packed attribute is unnecessary");
1062 /* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE). */
1065 compute_record_mode (type
)
1069 enum machine_mode mode
= VOIDmode
;
1071 /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
1072 However, if possible, we use a mode that fits in a register
1073 instead, in order to allow for better optimization down the
1075 TYPE_MODE (type
) = BLKmode
;
1077 if (! host_integerp (TYPE_SIZE (type
), 1))
1080 /* A record which has any BLKmode members must itself be
1081 BLKmode; it can't go in a register. Unless the member is
1082 BLKmode only because it isn't aligned. */
1083 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1085 unsigned HOST_WIDE_INT bitpos
;
1087 if (TREE_CODE (field
) != FIELD_DECL
)
1090 if (TREE_CODE (TREE_TYPE (field
)) == ERROR_MARK
1091 || (TYPE_MODE (TREE_TYPE (field
)) == BLKmode
1092 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field
)))
1093 || ! host_integerp (bit_position (field
), 1)
1094 || ! host_integerp (DECL_SIZE (field
), 1))
1097 bitpos
= int_bit_position (field
);
1099 /* Must be BLKmode if any field crosses a word boundary,
1100 since extract_bit_field can't handle that in registers. */
1101 if (bitpos
/ BITS_PER_WORD
1102 != ((tree_low_cst (DECL_SIZE (field
), 1) + bitpos
- 1)
1104 /* But there is no problem if the field is entire words. */
1105 && tree_low_cst (DECL_SIZE (field
), 1) % BITS_PER_WORD
!= 0)
1108 /* If this field is the whole struct, remember its mode so
1109 that, say, we can put a double in a class into a DF
1110 register instead of forcing it to live in the stack. */
1111 if (simple_cst_equal (TYPE_SIZE (type
), DECL_SIZE (field
)))
1112 mode
= DECL_MODE (field
);
1114 #ifdef MEMBER_TYPE_FORCES_BLK
1115 /* With some targets, eg. c4x, it is sub-optimal
1116 to access an aligned BLKmode structure as a scalar. */
1117 if (mode
== VOIDmode
&& MEMBER_TYPE_FORCES_BLK (field
))
1119 #endif /* MEMBER_TYPE_FORCES_BLK */
1122 /* If we only have one real field; use its mode. This only applies to
1123 RECORD_TYPE. This does not apply to unions. */
1124 if (TREE_CODE (type
) == RECORD_TYPE
&& mode
!= VOIDmode
)
1125 TYPE_MODE (type
) = mode
;
1127 TYPE_MODE (type
) = mode_for_size_tree (TYPE_SIZE (type
), MODE_INT
, 1);
1129 /* If structure's known alignment is less than what the scalar
1130 mode would need, and it matters, then stick with BLKmode. */
1131 if (TYPE_MODE (type
) != BLKmode
1133 && ! (TYPE_ALIGN (type
) >= BIGGEST_ALIGNMENT
1134 || TYPE_ALIGN (type
) >= GET_MODE_ALIGNMENT (TYPE_MODE (type
))))
1136 /* If this is the only reason this type is BLKmode, then
1137 don't force containing types to be BLKmode. */
1138 TYPE_NO_FORCE_BLK (type
) = 1;
1139 TYPE_MODE (type
) = BLKmode
;
1143 /* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
1147 finalize_type_size (type
)
1150 /* Normally, use the alignment corresponding to the mode chosen.
1151 However, where strict alignment is not required, avoid
1152 over-aligning structures, since most compilers do not do this
1155 if (TYPE_MODE (type
) != BLKmode
&& TYPE_MODE (type
) != VOIDmode
1156 && (STRICT_ALIGNMENT
1157 || (TREE_CODE (type
) != RECORD_TYPE
&& TREE_CODE (type
) != UNION_TYPE
1158 && TREE_CODE (type
) != QUAL_UNION_TYPE
1159 && TREE_CODE (type
) != ARRAY_TYPE
)))
1161 TYPE_ALIGN (type
) = GET_MODE_ALIGNMENT (TYPE_MODE (type
));
1162 TYPE_USER_ALIGN (type
) = 0;
1165 /* Do machine-dependent extra alignment. */
1166 #ifdef ROUND_TYPE_ALIGN
1168 = ROUND_TYPE_ALIGN (type
, TYPE_ALIGN (type
), BITS_PER_UNIT
);
1171 /* If we failed to find a simple way to calculate the unit size
1172 of the type, find it by division. */
1173 if (TYPE_SIZE_UNIT (type
) == 0 && TYPE_SIZE (type
) != 0)
1174 /* TYPE_SIZE (type) is computed in bitsizetype. After the division, the
1175 result will fit in sizetype. We will get more efficient code using
1176 sizetype, so we force a conversion. */
1177 TYPE_SIZE_UNIT (type
)
1178 = convert (sizetype
,
1179 size_binop (FLOOR_DIV_EXPR
, TYPE_SIZE (type
),
1180 bitsize_unit_node
));
1182 if (TYPE_SIZE (type
) != 0)
1184 #ifdef ROUND_TYPE_SIZE
1186 = ROUND_TYPE_SIZE (type
, TYPE_SIZE (type
), TYPE_ALIGN (type
));
1187 TYPE_SIZE_UNIT (type
)
1188 = ROUND_TYPE_SIZE_UNIT (type
, TYPE_SIZE_UNIT (type
),
1189 TYPE_ALIGN (type
) / BITS_PER_UNIT
);
1191 TYPE_SIZE (type
) = round_up (TYPE_SIZE (type
), TYPE_ALIGN (type
));
1192 TYPE_SIZE_UNIT (type
)
1193 = round_up (TYPE_SIZE_UNIT (type
), TYPE_ALIGN (type
) / BITS_PER_UNIT
);
1197 /* Evaluate nonconstant sizes only once, either now or as soon as safe. */
1198 if (TYPE_SIZE (type
) != 0 && TREE_CODE (TYPE_SIZE (type
)) != INTEGER_CST
)
1199 TYPE_SIZE (type
) = variable_size (TYPE_SIZE (type
));
1200 if (TYPE_SIZE_UNIT (type
) != 0
1201 && TREE_CODE (TYPE_SIZE_UNIT (type
)) != INTEGER_CST
)
1202 TYPE_SIZE_UNIT (type
) = variable_size (TYPE_SIZE_UNIT (type
));
1204 /* Also layout any other variants of the type. */
1205 if (TYPE_NEXT_VARIANT (type
)
1206 || type
!= TYPE_MAIN_VARIANT (type
))
1209 /* Record layout info of this variant. */
1210 tree size
= TYPE_SIZE (type
);
1211 tree size_unit
= TYPE_SIZE_UNIT (type
);
1212 unsigned int align
= TYPE_ALIGN (type
);
1213 unsigned int user_align
= TYPE_USER_ALIGN (type
);
1214 enum machine_mode mode
= TYPE_MODE (type
);
1216 /* Copy it into all variants. */
1217 for (variant
= TYPE_MAIN_VARIANT (type
);
1219 variant
= TYPE_NEXT_VARIANT (variant
))
1221 TYPE_SIZE (variant
) = size
;
1222 TYPE_SIZE_UNIT (variant
) = size_unit
;
1223 TYPE_ALIGN (variant
) = align
;
1224 TYPE_USER_ALIGN (variant
) = user_align
;
1225 TYPE_MODE (variant
) = mode
;
1230 /* Do all of the work required to layout the type indicated by RLI,
1231 once the fields have been laid out. This function will call `free'
1235 finish_record_layout (rli
)
1236 record_layout_info rli
;
1238 /* Compute the final size. */
1239 finalize_record_size (rli
);
1241 /* Compute the TYPE_MODE for the record. */
1242 compute_record_mode (rli
->t
);
1244 /* Perform any last tweaks to the TYPE_SIZE, etc. */
1245 finalize_type_size (rli
->t
);
1247 /* Lay out any static members. This is done now because their type
1248 may use the record's type. */
1249 while (rli
->pending_statics
)
1251 layout_decl (TREE_VALUE (rli
->pending_statics
), 0);
1252 rli
->pending_statics
= TREE_CHAIN (rli
->pending_statics
);
1259 /* Calculate the mode, size, and alignment for TYPE.
1260 For an array type, calculate the element separation as well.
1261 Record TYPE on the chain of permanent or temporary types
1262 so that dbxout will find out about it.
1264 TYPE_SIZE of a type is nonzero if the type has been laid out already.
1265 layout_type does nothing on such a type.
1267 If the type is incomplete, its TYPE_SIZE remains zero. */
1276 /* Do nothing if type has been laid out before. */
1277 if (TYPE_SIZE (type
))
1280 switch (TREE_CODE (type
))
1283 /* This kind of type is the responsibility
1284 of the language-specific code. */
1287 case BOOLEAN_TYPE
: /* Used for Java, Pascal, and Chill. */
1288 if (TYPE_PRECISION (type
) == 0)
1289 TYPE_PRECISION (type
) = 1; /* default to one byte/boolean. */
1291 /* ... fall through ... */
1296 if (TREE_CODE (TYPE_MIN_VALUE (type
)) == INTEGER_CST
1297 && tree_int_cst_sgn (TYPE_MIN_VALUE (type
)) >= 0)
1298 TREE_UNSIGNED (type
) = 1;
1300 TYPE_MODE (type
) = smallest_mode_for_size (TYPE_PRECISION (type
),
1302 TYPE_SIZE (type
) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
1303 TYPE_SIZE_UNIT (type
) = size_int (GET_MODE_SIZE (TYPE_MODE (type
)));
1307 TYPE_MODE (type
) = mode_for_size (TYPE_PRECISION (type
), MODE_FLOAT
, 0);
1308 TYPE_SIZE (type
) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
1309 TYPE_SIZE_UNIT (type
) = size_int (GET_MODE_SIZE (TYPE_MODE (type
)));
1313 TREE_UNSIGNED (type
) = TREE_UNSIGNED (TREE_TYPE (type
));
1315 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type
)),
1316 (TREE_CODE (TREE_TYPE (type
)) == INTEGER_TYPE
1317 ? MODE_COMPLEX_INT
: MODE_COMPLEX_FLOAT
),
1319 TYPE_SIZE (type
) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
1320 TYPE_SIZE_UNIT (type
) = size_int (GET_MODE_SIZE (TYPE_MODE (type
)));
1327 subtype
= TREE_TYPE (type
);
1328 TREE_UNSIGNED (type
) = TREE_UNSIGNED (subtype
);
1329 TYPE_SIZE (type
) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type
)));
1330 TYPE_SIZE_UNIT (type
) = size_int (GET_MODE_SIZE (TYPE_MODE (type
)));
1335 /* This is an incomplete type and so doesn't have a size. */
1336 TYPE_ALIGN (type
) = 1;
1337 TYPE_USER_ALIGN (type
) = 0;
1338 TYPE_MODE (type
) = VOIDmode
;
1342 TYPE_SIZE (type
) = bitsize_int (POINTER_SIZE
);
1343 TYPE_SIZE_UNIT (type
) = size_int (POINTER_SIZE
/ BITS_PER_UNIT
);
1344 /* A pointer might be MODE_PARTIAL_INT,
1345 but ptrdiff_t must be integral. */
1346 TYPE_MODE (type
) = mode_for_size (POINTER_SIZE
, MODE_INT
, 0);
1351 TYPE_MODE (type
) = mode_for_size (2 * POINTER_SIZE
, MODE_INT
, 0);
1352 TYPE_SIZE (type
) = bitsize_int (2 * POINTER_SIZE
);
1353 TYPE_SIZE_UNIT (type
) = size_int ((2 * POINTER_SIZE
) / BITS_PER_UNIT
);
1357 case REFERENCE_TYPE
:
1359 int nbits
= ((TREE_CODE (type
) == REFERENCE_TYPE
1360 && reference_types_internal
)
1361 ? GET_MODE_BITSIZE (Pmode
) : POINTER_SIZE
);
1363 TYPE_MODE (type
) = nbits
== POINTER_SIZE
? ptr_mode
: Pmode
;
1364 TYPE_SIZE (type
) = bitsize_int (nbits
);
1365 TYPE_SIZE_UNIT (type
) = size_int (nbits
/ BITS_PER_UNIT
);
1366 TREE_UNSIGNED (type
) = 1;
1367 TYPE_PRECISION (type
) = nbits
;
1373 register tree index
= TYPE_DOMAIN (type
);
1374 register tree element
= TREE_TYPE (type
);
1376 build_pointer_type (element
);
1378 /* We need to know both bounds in order to compute the size. */
1379 if (index
&& TYPE_MAX_VALUE (index
) && TYPE_MIN_VALUE (index
)
1380 && TYPE_SIZE (element
))
1382 tree ub
= TYPE_MAX_VALUE (index
);
1383 tree lb
= TYPE_MIN_VALUE (index
);
1387 /* The initial subtraction should happen in the original type so
1388 that (possible) negative values are handled appropriately. */
1389 length
= size_binop (PLUS_EXPR
, size_one_node
,
1391 fold (build (MINUS_EXPR
,
1395 /* Special handling for arrays of bits (for Chill). */
1396 element_size
= TYPE_SIZE (element
);
1397 if (TYPE_PACKED (type
) && INTEGRAL_TYPE_P (element
)
1398 && (integer_zerop (TYPE_MAX_VALUE (element
))
1399 || integer_onep (TYPE_MAX_VALUE (element
)))
1400 && host_integerp (TYPE_MIN_VALUE (element
), 1))
1402 HOST_WIDE_INT maxvalue
1403 = tree_low_cst (TYPE_MAX_VALUE (element
), 1);
1404 HOST_WIDE_INT minvalue
1405 = tree_low_cst (TYPE_MIN_VALUE (element
), 1);
1407 if (maxvalue
- minvalue
== 1
1408 && (maxvalue
== 1 || maxvalue
== 0))
1409 element_size
= integer_one_node
;
1412 TYPE_SIZE (type
) = size_binop (MULT_EXPR
, element_size
,
1413 convert (bitsizetype
, length
));
1415 /* If we know the size of the element, calculate the total
1416 size directly, rather than do some division thing below.
1417 This optimization helps Fortran assumed-size arrays
1418 (where the size of the array is determined at runtime)
1420 Note that we can't do this in the case where the size of
1421 the elements is one bit since TYPE_SIZE_UNIT cannot be
1422 set correctly in that case. */
1423 if (TYPE_SIZE_UNIT (element
) != 0 && ! integer_onep (element_size
))
1424 TYPE_SIZE_UNIT (type
)
1425 = size_binop (MULT_EXPR
, TYPE_SIZE_UNIT (element
), length
);
1428 /* Now round the alignment and size,
1429 using machine-dependent criteria if any. */
1431 #ifdef ROUND_TYPE_ALIGN
1433 = ROUND_TYPE_ALIGN (type
, TYPE_ALIGN (element
), BITS_PER_UNIT
);
1435 TYPE_ALIGN (type
) = MAX (TYPE_ALIGN (element
), BITS_PER_UNIT
);
1438 #ifdef ROUND_TYPE_SIZE
1439 if (TYPE_SIZE (type
) != 0)
1442 = ROUND_TYPE_SIZE (type
, TYPE_SIZE (type
), TYPE_ALIGN (type
));
1444 /* If the rounding changed the size of the type, remove any
1445 pre-calculated TYPE_SIZE_UNIT. */
1446 if (simple_cst_equal (TYPE_SIZE (type
), tmp
) != 1)
1447 TYPE_SIZE_UNIT (type
) = NULL
;
1449 TYPE_SIZE (type
) = tmp
;
1453 TYPE_MODE (type
) = BLKmode
;
1454 if (TYPE_SIZE (type
) != 0
1455 #ifdef MEMBER_TYPE_FORCES_BLK
1456 && ! MEMBER_TYPE_FORCES_BLK (type
)
1458 /* BLKmode elements force BLKmode aggregate;
1459 else extract/store fields may lose. */
1460 && (TYPE_MODE (TREE_TYPE (type
)) != BLKmode
1461 || TYPE_NO_FORCE_BLK (TREE_TYPE (type
))))
1464 = mode_for_size_tree (TYPE_SIZE (type
), MODE_INT
, 1);
1466 if (TYPE_MODE (type
) != BLKmode
1467 && STRICT_ALIGNMENT
&& TYPE_ALIGN (type
) < BIGGEST_ALIGNMENT
1468 && TYPE_ALIGN (type
) < GET_MODE_ALIGNMENT (TYPE_MODE (type
))
1469 && TYPE_MODE (type
) != BLKmode
)
1471 TYPE_NO_FORCE_BLK (type
) = 1;
1472 TYPE_MODE (type
) = BLKmode
;
1480 case QUAL_UNION_TYPE
:
1483 record_layout_info rli
;
1485 /* Initialize the layout information. */
1486 rli
= start_record_layout (type
);
1488 /* If this is a QUAL_UNION_TYPE, we want to process the fields
1489 in the reverse order in building the COND_EXPR that denotes
1490 its size. We reverse them again later. */
1491 if (TREE_CODE (type
) == QUAL_UNION_TYPE
)
1492 TYPE_FIELDS (type
) = nreverse (TYPE_FIELDS (type
));
1494 /* Place all the fields. */
1495 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
1496 place_field (rli
, field
);
1498 if (TREE_CODE (type
) == QUAL_UNION_TYPE
)
1499 TYPE_FIELDS (type
) = nreverse (TYPE_FIELDS (type
));
1501 /* Finish laying out the record. */
1502 finish_record_layout (rli
);
1506 case SET_TYPE
: /* Used by Chill and Pascal. */
1507 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type
))) != INTEGER_CST
1508 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type
))) != INTEGER_CST
)
1512 #ifndef SET_WORD_SIZE
1513 #define SET_WORD_SIZE BITS_PER_WORD
1515 unsigned int alignment
1516 = set_alignment
? set_alignment
: SET_WORD_SIZE
;
1518 = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type
)))
1519 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type
))) + 1);
1521 = ((size_in_bits
+ alignment
- 1) / alignment
) * alignment
;
1523 if (rounded_size
> (int) alignment
)
1524 TYPE_MODE (type
) = BLKmode
;
1526 TYPE_MODE (type
) = mode_for_size (alignment
, MODE_INT
, 1);
1528 TYPE_SIZE (type
) = bitsize_int (rounded_size
);
1529 TYPE_SIZE_UNIT (type
) = size_int (rounded_size
/ BITS_PER_UNIT
);
1530 TYPE_ALIGN (type
) = alignment
;
1531 TYPE_USER_ALIGN (type
) = 0;
1532 TYPE_PRECISION (type
) = size_in_bits
;
1537 /* The size may vary in different languages, so the language front end
1538 should fill in the size. */
1539 TYPE_ALIGN (type
) = BIGGEST_ALIGNMENT
;
1540 TYPE_USER_ALIGN (type
) = 0;
1541 TYPE_MODE (type
) = BLKmode
;
1548 /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE. For
1549 records and unions, finish_record_layout already called this
1551 if (TREE_CODE (type
) != RECORD_TYPE
1552 && TREE_CODE (type
) != UNION_TYPE
1553 && TREE_CODE (type
) != QUAL_UNION_TYPE
)
1554 finalize_type_size (type
);
1556 /* If this type is created before sizetype has been permanently set,
1557 record it so set_sizetype can fix it up. */
1559 early_type_list
= tree_cons (NULL_TREE
, type
, early_type_list
);
1561 /* If an alias set has been set for this aggregate when it was incomplete,
1562 force it into alias set 0.
1563 This is too conservative, but we cannot call record_component_aliases
1564 here because some frontends still change the aggregates after
1566 if (AGGREGATE_TYPE_P (type
) && TYPE_ALIAS_SET_KNOWN_P (type
))
1567 TYPE_ALIAS_SET (type
) = 0;
1570 /* Create and return a type for signed integers of PRECISION bits. */
1573 make_signed_type (precision
)
1576 register tree type
= make_node (INTEGER_TYPE
);
1578 TYPE_PRECISION (type
) = precision
;
1580 fixup_signed_type (type
);
1584 /* Create and return a type for unsigned integers of PRECISION bits. */
1587 make_unsigned_type (precision
)
1590 register tree type
= make_node (INTEGER_TYPE
);
1592 TYPE_PRECISION (type
) = precision
;
1594 fixup_unsigned_type (type
);
1598 /* Initialize sizetype and bitsizetype to a reasonable and temporary
1599 value to enable integer types to be created. */
1602 initialize_sizetypes ()
1604 tree t
= make_node (INTEGER_TYPE
);
1606 /* Set this so we do something reasonable for the build_int_2 calls
1608 integer_type_node
= t
;
1610 TYPE_MODE (t
) = SImode
;
1611 TYPE_ALIGN (t
) = GET_MODE_ALIGNMENT (SImode
);
1612 TYPE_USER_ALIGN (t
) = 0;
1613 TYPE_SIZE (t
) = build_int_2 (GET_MODE_BITSIZE (SImode
), 0);
1614 TYPE_SIZE_UNIT (t
) = build_int_2 (GET_MODE_SIZE (SImode
), 0);
1615 TREE_UNSIGNED (t
) = 1;
1616 TYPE_PRECISION (t
) = GET_MODE_BITSIZE (SImode
);
1617 TYPE_MIN_VALUE (t
) = build_int_2 (0, 0);
1618 TYPE_IS_SIZETYPE (t
) = 1;
1620 /* 1000 avoids problems with possible overflow and is certainly
1621 larger than any size value we'd want to be storing. */
1622 TYPE_MAX_VALUE (t
) = build_int_2 (1000, 0);
1624 /* These two must be different nodes because of the caching done in
1627 bitsizetype
= copy_node (t
);
1628 integer_type_node
= 0;
1631 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1632 Also update the type of any standard type's sizes made so far. */
1638 int oprecision
= TYPE_PRECISION (type
);
1639 /* The *bitsizetype types use a precision that avoids overflows when
1640 calculating signed sizes / offsets in bits. However, when
1641 cross-compiling from a 32 bit to a 64 bit host, we are limited to 64 bit
1643 int precision
= MIN (oprecision
+ BITS_PER_UNIT_LOG
+ 1,
1644 2 * HOST_BITS_PER_WIDE_INT
);
1651 /* Make copies of nodes since we'll be setting TYPE_IS_SIZETYPE. */
1652 sizetype
= copy_node (type
);
1653 TYPE_DOMAIN (sizetype
) = type
;
1654 TYPE_IS_SIZETYPE (sizetype
) = 1;
1655 bitsizetype
= make_node (INTEGER_TYPE
);
1656 TYPE_NAME (bitsizetype
) = TYPE_NAME (type
);
1657 TYPE_PRECISION (bitsizetype
) = precision
;
1658 TYPE_IS_SIZETYPE (bitsizetype
) = 1;
1660 if (TREE_UNSIGNED (type
))
1661 fixup_unsigned_type (bitsizetype
);
1663 fixup_signed_type (bitsizetype
);
1665 layout_type (bitsizetype
);
1667 if (TREE_UNSIGNED (type
))
1669 usizetype
= sizetype
;
1670 ubitsizetype
= bitsizetype
;
1671 ssizetype
= copy_node (make_signed_type (oprecision
));
1672 sbitsizetype
= copy_node (make_signed_type (precision
));
1676 ssizetype
= sizetype
;
1677 sbitsizetype
= bitsizetype
;
1678 usizetype
= copy_node (make_unsigned_type (oprecision
));
1679 ubitsizetype
= copy_node (make_unsigned_type (precision
));
1682 TYPE_NAME (bitsizetype
) = get_identifier ("bit_size_type");
1684 /* Show is a sizetype, is a main type, and has no pointers to it. */
1685 for (i
= 0; i
< ARRAY_SIZE (sizetype_tab
); i
++)
1687 TYPE_IS_SIZETYPE (sizetype_tab
[i
]) = 1;
1688 TYPE_MAIN_VARIANT (sizetype_tab
[i
]) = sizetype_tab
[i
];
1689 TYPE_NEXT_VARIANT (sizetype_tab
[i
]) = 0;
1690 TYPE_POINTER_TO (sizetype_tab
[i
]) = 0;
1691 TYPE_REFERENCE_TO (sizetype_tab
[i
]) = 0;
1694 ggc_add_tree_root ((tree
*) &sizetype_tab
,
1695 sizeof sizetype_tab
/ sizeof (tree
));
1697 /* Go down each of the types we already made and set the proper type
1698 for the sizes in them. */
1699 for (t
= early_type_list
; t
!= 0; t
= TREE_CHAIN (t
))
1701 if (TREE_CODE (TREE_VALUE (t
)) != INTEGER_TYPE
)
1704 TREE_TYPE (TYPE_SIZE (TREE_VALUE (t
))) = bitsizetype
;
1705 TREE_TYPE (TYPE_SIZE_UNIT (TREE_VALUE (t
))) = sizetype
;
1708 early_type_list
= 0;
1712 /* Set the extreme values of TYPE based on its precision in bits,
1713 then lay it out. Used when make_signed_type won't do
1714 because the tree code is not INTEGER_TYPE.
1715 E.g. for Pascal, when the -fsigned-char option is given. */
1718 fixup_signed_type (type
)
1721 register int precision
= TYPE_PRECISION (type
);
1723 TYPE_MIN_VALUE (type
)
1724 = build_int_2 ((precision
- HOST_BITS_PER_WIDE_INT
> 0
1725 ? 0 : (HOST_WIDE_INT
) (-1) << (precision
- 1)),
1726 (((HOST_WIDE_INT
) (-1)
1727 << (precision
- HOST_BITS_PER_WIDE_INT
- 1 > 0
1728 ? precision
- HOST_BITS_PER_WIDE_INT
- 1
1730 TYPE_MAX_VALUE (type
)
1731 = build_int_2 ((precision
- HOST_BITS_PER_WIDE_INT
> 0
1732 ? -1 : ((HOST_WIDE_INT
) 1 << (precision
- 1)) - 1),
1733 (precision
- HOST_BITS_PER_WIDE_INT
- 1 > 0
1734 ? (((HOST_WIDE_INT
) 1
1735 << (precision
- HOST_BITS_PER_WIDE_INT
- 1))) - 1
1738 TREE_TYPE (TYPE_MIN_VALUE (type
)) = type
;
1739 TREE_TYPE (TYPE_MAX_VALUE (type
)) = type
;
1741 /* Lay out the type: set its alignment, size, etc. */
1745 /* Set the extreme values of TYPE based on its precision in bits,
1746 then lay it out. This is used both in `make_unsigned_type'
1747 and for enumeral types. */
1750 fixup_unsigned_type (type
)
1753 register int precision
= TYPE_PRECISION (type
);
1755 TYPE_MIN_VALUE (type
) = build_int_2 (0, 0);
1756 TYPE_MAX_VALUE (type
)
1757 = build_int_2 (precision
- HOST_BITS_PER_WIDE_INT
>= 0
1758 ? -1 : ((HOST_WIDE_INT
) 1 << precision
) - 1,
1759 precision
- HOST_BITS_PER_WIDE_INT
> 0
1760 ? ((unsigned HOST_WIDE_INT
) ~0
1761 >> (HOST_BITS_PER_WIDE_INT
1762 - (precision
- HOST_BITS_PER_WIDE_INT
)))
1764 TREE_TYPE (TYPE_MIN_VALUE (type
)) = type
;
1765 TREE_TYPE (TYPE_MAX_VALUE (type
)) = type
;
1767 /* Lay out the type: set its alignment, size, etc. */
1771 /* Find the best machine mode to use when referencing a bit field of length
1772 BITSIZE bits starting at BITPOS.
1774 The underlying object is known to be aligned to a boundary of ALIGN bits.
1775 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1776 larger than LARGEST_MODE (usually SImode).
1778 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1779 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1780 mode meeting these conditions.
1782 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1783 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1784 all the conditions. */
1787 get_best_mode (bitsize
, bitpos
, align
, largest_mode
, volatilep
)
1788 int bitsize
, bitpos
;
1790 enum machine_mode largest_mode
;
1793 enum machine_mode mode
;
1794 unsigned int unit
= 0;
1796 /* Find the narrowest integer mode that contains the bit field. */
1797 for (mode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); mode
!= VOIDmode
;
1798 mode
= GET_MODE_WIDER_MODE (mode
))
1800 unit
= GET_MODE_BITSIZE (mode
);
1801 if ((bitpos
% unit
) + bitsize
<= unit
)
1805 if (mode
== VOIDmode
1806 /* It is tempting to omit the following line
1807 if STRICT_ALIGNMENT is true.
1808 But that is incorrect, since if the bitfield uses part of 3 bytes
1809 and we use a 4-byte mode, we could get a spurious segv
1810 if the extra 4th byte is past the end of memory.
1811 (Though at least one Unix compiler ignores this problem:
1812 that on the Sequent 386 machine. */
1813 || MIN (unit
, BIGGEST_ALIGNMENT
) > align
1814 || (largest_mode
!= VOIDmode
&& unit
> GET_MODE_BITSIZE (largest_mode
)))
1817 if (SLOW_BYTE_ACCESS
&& ! volatilep
)
1819 enum machine_mode wide_mode
= VOIDmode
, tmode
;
1821 for (tmode
= GET_CLASS_NARROWEST_MODE (MODE_INT
); tmode
!= VOIDmode
;
1822 tmode
= GET_MODE_WIDER_MODE (tmode
))
1824 unit
= GET_MODE_BITSIZE (tmode
);
1825 if (bitpos
/ unit
== (bitpos
+ bitsize
- 1) / unit
1826 && unit
<= BITS_PER_WORD
1827 && unit
<= MIN (align
, BIGGEST_ALIGNMENT
)
1828 && (largest_mode
== VOIDmode
1829 || unit
<= GET_MODE_BITSIZE (largest_mode
)))
1833 if (wide_mode
!= VOIDmode
)
1840 /* Return the alignment of MODE. This will be bounded by 1 and
1841 BIGGEST_ALIGNMENT. */
1844 get_mode_alignment (mode
)
1845 enum machine_mode mode
;
1847 unsigned int alignment
= GET_MODE_UNIT_SIZE (mode
) * BITS_PER_UNIT
;
1849 /* Extract the LSB of the size. */
1850 alignment
= alignment
& -alignment
;
1852 alignment
= MIN (BIGGEST_ALIGNMENT
, MAX (1, alignment
));
1856 /* This function is run once to initialize stor-layout.c. */
1859 init_stor_layout_once ()
1861 ggc_add_tree_root (&pending_sizes
, 1);