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, 2002 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
35 /* Set to one when set_sizetype has been called. */
36 static int sizetype_set;
38 /* List of types created before set_sizetype has been called. We do not
39 make this a GGC root since we want these nodes to be reclaimed. */
40 static tree early_type_list;
42 /* Data type for the expressions representing sizes of data types.
43 It is the first integer type laid out. */
44 tree sizetype_tab[(int) TYPE_KIND_LAST];
46 /* If nonzero, this is an upper limit on alignment of structure fields.
47 The value is measured in bits. */
48 unsigned int maximum_field_alignment;
50 /* If non-zero, the alignment of a bitstring or (power-)set value, in bits.
51 May be overridden by front-ends. */
52 unsigned int set_alignment = 0;
54 /* Nonzero if all REFERENCE_TYPEs are internal and hence should be
55 allocated in Pmode, not ptr_mode. Set only by internal_reference_types
56 called only by a front end. */
57 static int reference_types_internal = 0;
59 static void finalize_record_size PARAMS ((record_layout_info));
60 static void finalize_type_size PARAMS ((tree));
61 static void place_union_field PARAMS ((record_layout_info, tree));
62 extern void debug_rli PARAMS ((record_layout_info));
64 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
66 static tree pending_sizes;
68 /* Nonzero means cannot safely call expand_expr now,
69 so put variable sizes onto `pending_sizes' instead. */
71 int immediate_size_expand;
73 /* Show that REFERENCE_TYPES are internal and should be Pmode. Called only
77 internal_reference_types ()
79 reference_types_internal = 1;
82 /* Get a list of all the objects put on the pending sizes list. */
87 tree chain = pending_sizes;
90 /* Put each SAVE_EXPR into the current function. */
91 for (t = chain; t; t = TREE_CHAIN (t))
92 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
98 /* Return non-zero if EXPR is present on the pending sizes list. */
101 is_pending_size (expr)
106 for (t = pending_sizes; t; t = TREE_CHAIN (t))
107 if (TREE_VALUE (t) == expr)
112 /* Add EXPR to the pending sizes list. */
115 put_pending_size (expr)
118 /* Strip any simple arithmetic from EXPR to see if it has an underlying
120 while (TREE_CODE_CLASS (TREE_CODE (expr)) == '1'
121 || (TREE_CODE_CLASS (TREE_CODE (expr)) == '2'
122 && TREE_CONSTANT (TREE_OPERAND (expr, 1))))
123 expr = TREE_OPERAND (expr, 0);
125 if (TREE_CODE (expr) == SAVE_EXPR)
126 pending_sizes = tree_cons (NULL_TREE, expr, pending_sizes);
129 /* Put a chain of objects into the pending sizes list, which must be
133 put_pending_sizes (chain)
139 pending_sizes = chain;
142 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
143 to serve as the actual size-expression for a type or decl. */
149 /* If the language-processor is to take responsibility for variable-sized
150 items (e.g., languages which have elaboration procedures like Ada),
151 just return SIZE unchanged. Likewise for self-referential sizes and
153 if (TREE_CONSTANT (size)
154 || global_bindings_p () < 0 || contains_placeholder_p (size))
157 size = save_expr (size);
159 /* If an array with a variable number of elements is declared, and
160 the elements require destruction, we will emit a cleanup for the
161 array. That cleanup is run both on normal exit from the block
162 and in the exception-handler for the block. Normally, when code
163 is used in both ordinary code and in an exception handler it is
164 `unsaved', i.e., all SAVE_EXPRs are recalculated. However, we do
165 not wish to do that here; the array-size is the same in both
167 if (TREE_CODE (size) == SAVE_EXPR)
168 SAVE_EXPR_PERSISTENT_P (size) = 1;
170 if (global_bindings_p ())
172 if (TREE_CONSTANT (size))
173 error ("type size can't be explicitly evaluated");
175 error ("variable-size type declared outside of any function");
177 return size_one_node;
180 if (immediate_size_expand)
181 /* NULL_RTX is not defined; neither is the rtx type.
182 Also, we would like to pass const0_rtx here, but don't have it. */
183 expand_expr (size, expand_expr (integer_zero_node, NULL_RTX, VOIDmode, 0),
185 else if (cfun != 0 && cfun->x_dont_save_pending_sizes_p)
186 /* The front-end doesn't want us to keep a list of the expressions
187 that determine sizes for variable size objects. */
190 put_pending_size (size);
195 #ifndef MAX_FIXED_MODE_SIZE
196 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
199 /* Return the machine mode to use for a nonscalar of SIZE bits.
200 The mode must be in class CLASS, and have exactly that many bits.
201 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
205 mode_for_size (size, class, limit)
207 enum mode_class class;
210 enum machine_mode mode;
212 if (limit && size > MAX_FIXED_MODE_SIZE)
215 /* Get the first mode which has this size, in the specified class. */
216 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
217 mode = GET_MODE_WIDER_MODE (mode))
218 if (GET_MODE_BITSIZE (mode) == size)
224 /* Similar, except passed a tree node. */
227 mode_for_size_tree (size, class, limit)
229 enum mode_class class;
232 if (TREE_CODE (size) != INTEGER_CST
233 /* What we really want to say here is that the size can fit in a
234 host integer, but we know there's no way we'd find a mode for
235 this many bits, so there's no point in doing the precise test. */
236 || compare_tree_int (size, 1000) > 0)
239 return mode_for_size (TREE_INT_CST_LOW (size), class, limit);
242 /* Similar, but never return BLKmode; return the narrowest mode that
243 contains at least the requested number of bits. */
246 smallest_mode_for_size (size, class)
248 enum mode_class class;
250 enum machine_mode mode;
252 /* Get the first mode which has at least this size, in the
254 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
255 mode = GET_MODE_WIDER_MODE (mode))
256 if (GET_MODE_BITSIZE (mode) >= size)
262 /* Find an integer mode of the exact same size, or BLKmode on failure. */
265 int_mode_for_mode (mode)
266 enum machine_mode mode;
268 switch (GET_MODE_CLASS (mode))
271 case MODE_PARTIAL_INT:
274 case MODE_COMPLEX_INT:
275 case MODE_COMPLEX_FLOAT:
277 case MODE_VECTOR_INT:
278 case MODE_VECTOR_FLOAT:
279 mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
286 /* ... fall through ... */
296 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
297 This can only be applied to objects of a sizetype. */
300 round_up (value, divisor)
304 tree arg = size_int_type (divisor, TREE_TYPE (value));
306 return size_binop (MULT_EXPR, size_binop (CEIL_DIV_EXPR, value, arg), arg);
309 /* Likewise, but round down. */
312 round_down (value, divisor)
316 tree arg = size_int_type (divisor, TREE_TYPE (value));
318 return size_binop (MULT_EXPR, size_binop (FLOOR_DIV_EXPR, value, arg), arg);
321 /* Set the size, mode and alignment of a ..._DECL node.
322 TYPE_DECL does need this for C++.
323 Note that LABEL_DECL and CONST_DECL nodes do not need this,
324 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
325 Don't call layout_decl for them.
327 KNOWN_ALIGN is the amount of alignment we can assume this
328 decl has with no special effort. It is relevant only for FIELD_DECLs
329 and depends on the previous fields.
330 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
331 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
332 the record will be aligned to suit. */
335 layout_decl (decl, known_align)
337 unsigned int known_align;
339 tree type = TREE_TYPE (decl);
340 enum tree_code code = TREE_CODE (decl);
342 if (code == CONST_DECL)
344 else if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
345 && code != TYPE_DECL && code != FIELD_DECL)
348 if (type == error_mark_node)
349 type = void_type_node;
351 /* Usually the size and mode come from the data type without change,
352 however, the front-end may set the explicit width of the field, so its
353 size may not be the same as the size of its type. This happens with
354 bitfields, of course (an `int' bitfield may be only 2 bits, say), but it
355 also happens with other fields. For example, the C++ front-end creates
356 zero-sized fields corresponding to empty base classes, and depends on
357 layout_type setting DECL_FIELD_BITPOS correctly for the field. Set the
358 size in bytes from the size in bits. If we have already set the mode,
359 don't set it again since we can be called twice for FIELD_DECLs. */
361 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
362 if (DECL_MODE (decl) == VOIDmode)
363 DECL_MODE (decl) = TYPE_MODE (type);
365 if (DECL_SIZE (decl) == 0)
367 DECL_SIZE (decl) = TYPE_SIZE (type);
368 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
371 DECL_SIZE_UNIT (decl)
372 = convert (sizetype, size_binop (CEIL_DIV_EXPR, DECL_SIZE (decl),
375 /* Force alignment required for the data type.
376 But if the decl itself wants greater alignment, don't override that.
377 Likewise, if the decl is packed, don't override it. */
378 if (! (code == FIELD_DECL && DECL_BIT_FIELD (decl))
379 && (DECL_ALIGN (decl) == 0
380 || (! (code == FIELD_DECL && DECL_PACKED (decl))
381 && TYPE_ALIGN (type) > DECL_ALIGN (decl))))
383 DECL_ALIGN (decl) = TYPE_ALIGN (type);
384 DECL_USER_ALIGN (decl) = 0;
387 /* For fields, set the bit field type and update the alignment. */
388 if (code == FIELD_DECL)
390 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
391 if (maximum_field_alignment != 0)
392 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
394 /* If the field is of variable size, we can't misalign it since we
395 have no way to make a temporary to align the result. But this
396 isn't an issue if the decl is not addressable. Likewise if it
397 is of unknown size. */
398 else if (DECL_PACKED (decl)
399 && (DECL_NONADDRESSABLE_P (decl)
400 || DECL_SIZE_UNIT (decl) == 0
401 || TREE_CODE (DECL_SIZE_UNIT (decl)) == INTEGER_CST))
403 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
404 DECL_USER_ALIGN (decl) = 0;
408 /* See if we can use an ordinary integer mode for a bit-field.
409 Conditions are: a fixed size that is correct for another mode
410 and occupying a complete byte or bytes on proper boundary. */
411 if (code == FIELD_DECL && DECL_BIT_FIELD (decl)
412 && TYPE_SIZE (type) != 0
413 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
414 && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
416 enum machine_mode xmode
417 = mode_for_size_tree (DECL_SIZE (decl), MODE_INT, 1);
419 if (xmode != BLKmode && known_align >= GET_MODE_ALIGNMENT (xmode))
421 DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
423 DECL_MODE (decl) = xmode;
424 DECL_BIT_FIELD (decl) = 0;
428 /* Turn off DECL_BIT_FIELD if we won't need it set. */
429 if (code == FIELD_DECL && DECL_BIT_FIELD (decl)
430 && TYPE_MODE (type) == BLKmode && DECL_MODE (decl) == BLKmode
431 && known_align >= TYPE_ALIGN (type)
432 && DECL_ALIGN (decl) >= TYPE_ALIGN (type)
433 && DECL_SIZE_UNIT (decl) != 0)
434 DECL_BIT_FIELD (decl) = 0;
436 /* Evaluate nonconstant size only once, either now or as soon as safe. */
437 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
438 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
439 if (DECL_SIZE_UNIT (decl) != 0
440 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST)
441 DECL_SIZE_UNIT (decl) = variable_size (DECL_SIZE_UNIT (decl));
443 /* If requested, warn about definitions of large data objects. */
445 && (code == VAR_DECL || code == PARM_DECL)
446 && ! DECL_EXTERNAL (decl))
448 tree size = DECL_SIZE_UNIT (decl);
450 if (size != 0 && TREE_CODE (size) == INTEGER_CST
451 && compare_tree_int (size, larger_than_size) > 0)
453 unsigned int size_as_int = TREE_INT_CST_LOW (size);
455 if (compare_tree_int (size, size_as_int) == 0)
456 warning_with_decl (decl, "size of `%s' is %d bytes", size_as_int);
458 warning_with_decl (decl, "size of `%s' is larger than %d bytes",
464 /* Hook for a front-end function that can modify the record layout as needed
465 immediately before it is finalized. */
467 void (*lang_adjust_rli) PARAMS ((record_layout_info)) = 0;
470 set_lang_adjust_rli (f)
471 void (*f) PARAMS ((record_layout_info));
476 /* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
477 QUAL_UNION_TYPE. Return a pointer to a struct record_layout_info which
478 is to be passed to all other layout functions for this record. It is the
479 responsibility of the caller to call `free' for the storage returned.
480 Note that garbage collection is not permitted until we finish laying
484 start_record_layout (t)
487 record_layout_info rli
488 = (record_layout_info) xmalloc (sizeof (struct record_layout_info_s));
492 /* If the type has a minimum specified alignment (via an attribute
493 declaration, for example) use it -- otherwise, start with a
494 one-byte alignment. */
495 rli->record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (t));
496 rli->unpacked_align = rli->unpadded_align = rli->record_align;
497 rli->offset_align = MAX (rli->record_align, BIGGEST_ALIGNMENT);
499 #ifdef STRUCTURE_SIZE_BOUNDARY
500 /* Packed structures don't need to have minimum size. */
501 if (! TYPE_PACKED (t))
502 rli->record_align = MAX (rli->record_align, STRUCTURE_SIZE_BOUNDARY);
505 rli->offset = size_zero_node;
506 rli->bitpos = bitsize_zero_node;
508 rli->pending_statics = 0;
509 rli->packed_maybe_necessary = 0;
514 /* These four routines perform computations that convert between
515 the offset/bitpos forms and byte and bit offsets. */
518 bit_from_pos (offset, bitpos)
521 return size_binop (PLUS_EXPR, bitpos,
522 size_binop (MULT_EXPR, convert (bitsizetype, offset),
527 byte_from_pos (offset, bitpos)
530 return size_binop (PLUS_EXPR, offset,
532 size_binop (TRUNC_DIV_EXPR, bitpos,
533 bitsize_unit_node)));
537 pos_from_byte (poffset, pbitpos, off_align, pos)
538 tree *poffset, *pbitpos;
539 unsigned int off_align;
543 = size_binop (MULT_EXPR,
545 size_binop (FLOOR_DIV_EXPR, pos,
546 bitsize_int (off_align
548 size_int (off_align / BITS_PER_UNIT));
549 *pbitpos = size_binop (MULT_EXPR,
550 size_binop (FLOOR_MOD_EXPR, pos,
551 bitsize_int (off_align / BITS_PER_UNIT)),
556 pos_from_bit (poffset, pbitpos, off_align, pos)
557 tree *poffset, *pbitpos;
558 unsigned int off_align;
561 *poffset = size_binop (MULT_EXPR,
563 size_binop (FLOOR_DIV_EXPR, pos,
564 bitsize_int (off_align))),
565 size_int (off_align / BITS_PER_UNIT));
566 *pbitpos = size_binop (FLOOR_MOD_EXPR, pos, bitsize_int (off_align));
569 /* Given a pointer to bit and byte offsets and an offset alignment,
570 normalize the offsets so they are within the alignment. */
573 normalize_offset (poffset, pbitpos, off_align)
574 tree *poffset, *pbitpos;
575 unsigned int off_align;
577 /* If the bit position is now larger than it should be, adjust it
579 if (compare_tree_int (*pbitpos, off_align) >= 0)
581 tree extra_aligns = size_binop (FLOOR_DIV_EXPR, *pbitpos,
582 bitsize_int (off_align));
585 = size_binop (PLUS_EXPR, *poffset,
586 size_binop (MULT_EXPR, convert (sizetype, extra_aligns),
587 size_int (off_align / BITS_PER_UNIT)));
590 = size_binop (FLOOR_MOD_EXPR, *pbitpos, bitsize_int (off_align));
594 /* Print debugging information about the information in RLI. */
598 record_layout_info rli;
600 print_node_brief (stderr, "type", rli->t, 0);
601 print_node_brief (stderr, "\noffset", rli->offset, 0);
602 print_node_brief (stderr, " bitpos", rli->bitpos, 0);
604 fprintf (stderr, "\naligns: rec = %u, unpack = %u, unpad = %u, off = %u\n",
605 rli->record_align, rli->unpacked_align, rli->unpadded_align,
607 if (rli->packed_maybe_necessary)
608 fprintf (stderr, "packed may be necessary\n");
610 if (rli->pending_statics)
612 fprintf (stderr, "pending statics:\n");
613 debug_tree (rli->pending_statics);
617 /* Given an RLI with a possibly-incremented BITPOS, adjust OFFSET and
618 BITPOS if necessary to keep BITPOS below OFFSET_ALIGN. */
622 record_layout_info rli;
624 normalize_offset (&rli->offset, &rli->bitpos, rli->offset_align);
627 /* Returns the size in bytes allocated so far. */
630 rli_size_unit_so_far (rli)
631 record_layout_info rli;
633 return byte_from_pos (rli->offset, rli->bitpos);
636 /* Returns the size in bits allocated so far. */
639 rli_size_so_far (rli)
640 record_layout_info rli;
642 return bit_from_pos (rli->offset, rli->bitpos);
645 /* Called from place_field to handle unions. */
648 place_union_field (rli, field)
649 record_layout_info rli;
652 unsigned int desired_align;
654 layout_decl (field, 0);
656 DECL_FIELD_OFFSET (field) = size_zero_node;
657 DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
658 SET_DECL_OFFSET_ALIGN (field, BIGGEST_ALIGNMENT);
660 desired_align = DECL_ALIGN (field);
662 #ifdef BIGGEST_FIELD_ALIGNMENT
663 /* Some targets (i.e. i386) limit union field alignment
664 to a lower boundary than alignment of variables unless
665 it was overridden by attribute aligned. */
666 if (! DECL_USER_ALIGN (field))
668 MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
671 #ifdef ADJUST_FIELD_ALIGN
672 if (! DECL_USER_ALIGN (field))
673 desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
676 TYPE_USER_ALIGN (rli->t) |= DECL_USER_ALIGN (field);
678 /* Union must be at least as aligned as any field requires. */
679 rli->record_align = MAX (rli->record_align, desired_align);
680 rli->unpadded_align = MAX (rli->unpadded_align, desired_align);
682 #ifdef PCC_BITFIELD_TYPE_MATTERS
683 /* On the m88000, a bit field of declare type `int' forces the
684 entire union to have `int' alignment. */
685 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
687 unsigned int type_align = TYPE_ALIGN (TREE_TYPE (field));
689 #ifdef ADJUST_FIELD_ALIGN
690 if (! TYPE_USER_ALIGN (TREE_TYPE (field)))
691 type_align = ADJUST_FIELD_ALIGN (field, type_align);
693 rli->record_align = MAX (rli->record_align, type_align);
694 rli->unpadded_align = MAX (rli->unpadded_align, type_align);
695 TYPE_USER_ALIGN (rli->t) |= TYPE_USER_ALIGN (TREE_TYPE (field));
699 /* We assume the union's size will be a multiple of a byte so we don't
700 bother with BITPOS. */
701 if (TREE_CODE (rli->t) == UNION_TYPE)
702 rli->offset = size_binop (MAX_EXPR, rli->offset, DECL_SIZE_UNIT (field));
703 else if (TREE_CODE (rli->t) == QUAL_UNION_TYPE)
704 rli->offset = fold (build (COND_EXPR, sizetype,
705 DECL_QUALIFIER (field),
706 DECL_SIZE_UNIT (field), rli->offset));
709 /* RLI contains information about the layout of a RECORD_TYPE. FIELD
710 is a FIELD_DECL to be added after those fields already present in
711 T. (FIELD is not actually added to the TYPE_FIELDS list here;
712 callers that desire that behavior must manually perform that step.) */
715 place_field (rli, field)
716 record_layout_info rli;
719 /* The alignment required for FIELD. */
720 unsigned int desired_align;
721 /* The alignment FIELD would have if we just dropped it into the
722 record as it presently stands. */
723 unsigned int known_align;
724 unsigned int actual_align;
725 unsigned int user_align;
726 /* The type of this field. */
727 tree type = TREE_TYPE (field);
729 if (TREE_CODE (field) == ERROR_MARK || TREE_CODE (type) == ERROR_MARK)
732 /* If FIELD is static, then treat it like a separate variable, not
733 really like a structure field. If it is a FUNCTION_DECL, it's a
734 method. In both cases, all we do is lay out the decl, and we do
735 it *after* the record is laid out. */
736 if (TREE_CODE (field) == VAR_DECL)
738 rli->pending_statics = tree_cons (NULL_TREE, field,
739 rli->pending_statics);
743 /* Enumerators and enum types which are local to this class need not
744 be laid out. Likewise for initialized constant fields. */
745 else if (TREE_CODE (field) != FIELD_DECL)
748 /* Unions are laid out very differently than records, so split
749 that code off to another function. */
750 else if (TREE_CODE (rli->t) != RECORD_TYPE)
752 place_union_field (rli, field);
756 /* Work out the known alignment so far. Note that A & (-A) is the
757 value of the least-significant bit in A that is one. */
758 if (! integer_zerop (rli->bitpos))
759 known_align = (tree_low_cst (rli->bitpos, 1)
760 & - tree_low_cst (rli->bitpos, 1));
761 else if (integer_zerop (rli->offset))
762 known_align = BIGGEST_ALIGNMENT;
763 else if (host_integerp (rli->offset, 1))
764 known_align = (BITS_PER_UNIT
765 * (tree_low_cst (rli->offset, 1)
766 & - tree_low_cst (rli->offset, 1)));
768 known_align = rli->offset_align;
770 /* Lay out the field so we know what alignment it needs. For a
771 packed field, use the alignment as specified, disregarding what
772 the type would want. */
773 desired_align = DECL_ALIGN (field);
774 user_align = DECL_USER_ALIGN (field);
775 layout_decl (field, known_align);
776 if (! DECL_PACKED (field))
778 desired_align = DECL_ALIGN (field);
779 user_align = DECL_USER_ALIGN (field);
782 /* Some targets (i.e. i386, VMS) limit struct field alignment
783 to a lower boundary than alignment of variables unless
784 it was overridden by attribute aligned. */
785 #ifdef BIGGEST_FIELD_ALIGNMENT
788 = MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
791 #ifdef ADJUST_FIELD_ALIGN
793 desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
796 /* Record must have at least as much alignment as any field.
797 Otherwise, the alignment of the field within the record is
799 if ((* targetm.ms_bitfield_layout_p) (rli->t)
800 && type != error_mark_node
801 && DECL_BIT_FIELD_TYPE (field)
802 && ! integer_zerop (TYPE_SIZE (type))
803 && integer_zerop (DECL_SIZE (field)))
806 && DECL_BIT_FIELD_TYPE (rli->prev_field)
807 && ! integer_zerop (DECL_SIZE (rli->prev_field)))
809 rli->record_align = MAX (rli->record_align, desired_align);
810 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
816 #ifdef PCC_BITFIELD_TYPE_MATTERS
817 if (PCC_BITFIELD_TYPE_MATTERS && type != error_mark_node
818 && ! (* targetm.ms_bitfield_layout_p) (rli->t)
819 && DECL_BIT_FIELD_TYPE (field)
820 && ! integer_zerop (TYPE_SIZE (type)))
822 /* For these machines, a zero-length field does not
823 affect the alignment of the structure as a whole.
824 It does, however, affect the alignment of the next field
825 within the structure. */
826 if (! integer_zerop (DECL_SIZE (field)))
827 rli->record_align = MAX (rli->record_align, desired_align);
828 else if (! DECL_PACKED (field))
829 desired_align = TYPE_ALIGN (type);
831 /* A named bit field of declared type `int'
832 forces the entire structure to have `int' alignment. */
833 if (DECL_NAME (field) != 0)
835 unsigned int type_align = TYPE_ALIGN (type);
837 #ifdef ADJUST_FIELD_ALIGN
838 if (! TYPE_USER_ALIGN (type))
839 type_align = ADJUST_FIELD_ALIGN (field, type_align);
842 if (maximum_field_alignment != 0)
843 type_align = MIN (type_align, maximum_field_alignment);
844 else if (DECL_PACKED (field))
845 type_align = MIN (type_align, BITS_PER_UNIT);
847 rli->record_align = MAX (rli->record_align, type_align);
848 rli->unpadded_align = MAX (rli->unpadded_align, DECL_ALIGN (field));
850 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
851 user_align |= TYPE_USER_ALIGN (type);
857 rli->record_align = MAX (rli->record_align, desired_align);
858 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
859 rli->unpadded_align = MAX (rli->unpadded_align, DECL_ALIGN (field));
862 if (warn_packed && DECL_PACKED (field))
864 if (known_align > TYPE_ALIGN (type))
866 if (TYPE_ALIGN (type) > desired_align)
868 if (STRICT_ALIGNMENT)
869 warning_with_decl (field, "packed attribute causes inefficient alignment for `%s'");
871 warning_with_decl (field, "packed attribute is unnecessary for `%s'");
875 rli->packed_maybe_necessary = 1;
878 /* Does this field automatically have alignment it needs by virtue
879 of the fields that precede it and the record's own alignment? */
880 if (known_align < desired_align)
882 /* No, we need to skip space before this field.
883 Bump the cumulative size to multiple of field alignment. */
886 warning_with_decl (field, "padding struct to align `%s'");
888 /* If the alignment is still within offset_align, just align
890 if (desired_align < rli->offset_align)
891 rli->bitpos = round_up (rli->bitpos, desired_align);
894 /* First adjust OFFSET by the partial bits, then align. */
896 = size_binop (PLUS_EXPR, rli->offset,
898 size_binop (CEIL_DIV_EXPR, rli->bitpos,
899 bitsize_unit_node)));
900 rli->bitpos = bitsize_zero_node;
902 rli->offset = round_up (rli->offset, desired_align / BITS_PER_UNIT);
905 if (! TREE_CONSTANT (rli->offset))
906 rli->offset_align = desired_align;
910 /* Handle compatibility with PCC. Note that if the record has any
911 variable-sized fields, we need not worry about compatibility. */
912 #ifdef PCC_BITFIELD_TYPE_MATTERS
913 if (PCC_BITFIELD_TYPE_MATTERS
914 && ! (* targetm.ms_bitfield_layout_p) (rli->t)
915 && TREE_CODE (field) == FIELD_DECL
916 && type != error_mark_node
917 && DECL_BIT_FIELD (field)
918 && ! DECL_PACKED (field)
919 && maximum_field_alignment == 0
920 && ! integer_zerop (DECL_SIZE (field))
921 && host_integerp (DECL_SIZE (field), 1)
922 && host_integerp (rli->offset, 1)
923 && host_integerp (TYPE_SIZE (type), 1))
925 unsigned int type_align = TYPE_ALIGN (type);
926 tree dsize = DECL_SIZE (field);
927 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
928 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
929 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
931 #ifdef ADJUST_FIELD_ALIGN
932 if (! TYPE_USER_ALIGN (type))
933 type_align = ADJUST_FIELD_ALIGN (field, type_align);
936 /* A bit field may not span more units of alignment of its type
937 than its type itself. Advance to next boundary if necessary. */
938 if ((((offset * BITS_PER_UNIT + bit_offset + field_size +
941 - (offset * BITS_PER_UNIT + bit_offset) / type_align)
942 > tree_low_cst (TYPE_SIZE (type), 1) / type_align)
943 rli->bitpos = round_up (rli->bitpos, type_align);
945 user_align |= TYPE_USER_ALIGN (type);
949 #ifdef BITFIELD_NBYTES_LIMITED
950 if (BITFIELD_NBYTES_LIMITED
951 && ! (* targetm.ms_bitfield_layout_p) (rli->t)
952 && TREE_CODE (field) == FIELD_DECL
953 && type != error_mark_node
954 && DECL_BIT_FIELD_TYPE (field)
955 && ! DECL_PACKED (field)
956 && ! integer_zerop (DECL_SIZE (field))
957 && host_integerp (DECL_SIZE (field), 1)
958 && host_integerp (rli->offset, 1)
959 && host_integerp (TYPE_SIZE (type), 1))
961 unsigned int type_align = TYPE_ALIGN (type);
962 tree dsize = DECL_SIZE (field);
963 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
964 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
965 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
967 #ifdef ADJUST_FIELD_ALIGN
968 if (! TYPE_USER_ALIGN (type))
969 type_align = ADJUST_FIELD_ALIGN (field, type_align);
972 if (maximum_field_alignment != 0)
973 type_align = MIN (type_align, maximum_field_alignment);
974 /* ??? This test is opposite the test in the containing if
975 statement, so this code is unreachable currently. */
976 else if (DECL_PACKED (field))
977 type_align = MIN (type_align, BITS_PER_UNIT);
979 /* A bit field may not span the unit of alignment of its type.
980 Advance to next boundary if necessary. */
981 /* ??? This code should match the code above for the
982 PCC_BITFIELD_TYPE_MATTERS case. */
983 if ((offset * BITS_PER_UNIT + bit_offset) / type_align
984 != ((offset * BITS_PER_UNIT + bit_offset + field_size - 1)
986 rli->bitpos = round_up (rli->bitpos, type_align);
988 user_align |= TYPE_USER_ALIGN (type);
992 /* See the docs for TARGET_MS_BITFIELD_LAYOUT_P for details. */
993 if ((* targetm.ms_bitfield_layout_p) (rli->t)
994 && TREE_CODE (field) == FIELD_DECL
995 && type != error_mark_node
996 && ! DECL_PACKED (field)
999 && host_integerp (DECL_SIZE (field), 1)
1000 && DECL_SIZE (rli->prev_field)
1001 && host_integerp (DECL_SIZE (rli->prev_field), 1)
1002 && host_integerp (rli->offset, 1)
1003 && host_integerp (TYPE_SIZE (type), 1)
1004 && host_integerp (TYPE_SIZE (TREE_TYPE (rli->prev_field)), 1)
1005 && ((DECL_BIT_FIELD_TYPE (rli->prev_field)
1006 && ! integer_zerop (DECL_SIZE (rli->prev_field)))
1007 || (DECL_BIT_FIELD_TYPE (field)
1008 && ! integer_zerop (DECL_SIZE (field))))
1009 && (! simple_cst_equal (TYPE_SIZE (type),
1010 TYPE_SIZE (TREE_TYPE (rli->prev_field)))
1011 /* If the previous field was a zero-sized bit-field, either
1012 it was ignored, in which case we must ensure the proper
1013 alignment of this field here, or it already forced the
1014 alignment of this field, in which case forcing the
1015 alignment again is harmless. So, do it in both cases. */
1016 || (DECL_BIT_FIELD_TYPE (rli->prev_field)
1017 && integer_zerop (DECL_SIZE (rli->prev_field)))))
1019 unsigned int type_align = TYPE_ALIGN (type);
1022 && DECL_BIT_FIELD_TYPE (rli->prev_field)
1023 /* If the previous bit-field is zero-sized, we've already
1024 accounted for its alignment needs (or ignored it, if
1025 appropriate) while placing it. */
1026 && ! integer_zerop (DECL_SIZE (rli->prev_field)))
1027 type_align = MAX (type_align,
1028 TYPE_ALIGN (TREE_TYPE (rli->prev_field)));
1030 if (maximum_field_alignment != 0)
1031 type_align = MIN (type_align, maximum_field_alignment);
1033 rli->bitpos = round_up (rli->bitpos, type_align);
1036 /* Offset so far becomes the position of this field after normalizing. */
1037 normalize_rli (rli);
1038 DECL_FIELD_OFFSET (field) = rli->offset;
1039 DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
1040 SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
1042 TYPE_USER_ALIGN (rli->t) |= user_align;
1044 /* If this field ended up more aligned than we thought it would be (we
1045 approximate this by seeing if its position changed), lay out the field
1046 again; perhaps we can use an integral mode for it now. */
1047 if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field)))
1048 actual_align = (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1049 & - tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1));
1050 else if (integer_zerop (DECL_FIELD_OFFSET (field)))
1051 actual_align = BIGGEST_ALIGNMENT;
1052 else if (host_integerp (DECL_FIELD_OFFSET (field), 1))
1053 actual_align = (BITS_PER_UNIT
1054 * (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
1055 & - tree_low_cst (DECL_FIELD_OFFSET (field), 1)));
1057 actual_align = DECL_OFFSET_ALIGN (field);
1059 if (known_align != actual_align)
1060 layout_decl (field, actual_align);
1062 rli->prev_field = field;
1064 /* Now add size of this field to the size of the record. If the size is
1065 not constant, treat the field as being a multiple of bytes and just
1066 adjust the offset, resetting the bit position. Otherwise, apportion the
1067 size amongst the bit position and offset. First handle the case of an
1068 unspecified size, which can happen when we have an invalid nested struct
1069 definition, such as struct j { struct j { int i; } }. The error message
1070 is printed in finish_struct. */
1071 if (DECL_SIZE (field) == 0)
1073 else if (TREE_CODE (DECL_SIZE_UNIT (field)) != INTEGER_CST
1074 || TREE_CONSTANT_OVERFLOW (DECL_SIZE_UNIT (field)))
1077 = size_binop (PLUS_EXPR, rli->offset,
1079 size_binop (CEIL_DIV_EXPR, rli->bitpos,
1080 bitsize_unit_node)));
1082 = size_binop (PLUS_EXPR, rli->offset, DECL_SIZE_UNIT (field));
1083 rli->bitpos = bitsize_zero_node;
1084 rli->offset_align = MIN (rli->offset_align, DECL_ALIGN (field));
1088 rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
1089 normalize_rli (rli);
1093 /* Assuming that all the fields have been laid out, this function uses
1094 RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
1095 inidicated by RLI. */
1098 finalize_record_size (rli)
1099 record_layout_info rli;
1101 tree unpadded_size, unpadded_size_unit;
1103 /* Now we want just byte and bit offsets, so set the offset alignment
1104 to be a byte and then normalize. */
1105 rli->offset_align = BITS_PER_UNIT;
1106 normalize_rli (rli);
1108 /* Determine the desired alignment. */
1109 #ifdef ROUND_TYPE_ALIGN
1110 TYPE_ALIGN (rli->t) = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t),
1113 TYPE_ALIGN (rli->t) = MAX (TYPE_ALIGN (rli->t), rli->record_align);
1116 /* Compute the size so far. Be sure to allow for extra bits in the
1117 size in bytes. We have guaranteed above that it will be no more
1118 than a single byte. */
1119 unpadded_size = rli_size_so_far (rli);
1120 unpadded_size_unit = rli_size_unit_so_far (rli);
1121 if (! integer_zerop (rli->bitpos))
1123 = size_binop (PLUS_EXPR, unpadded_size_unit, size_one_node);
1125 /* Record the un-rounded size in the binfo node. But first we check
1126 the size of TYPE_BINFO to make sure that BINFO_SIZE is available. */
1127 if (TYPE_BINFO (rli->t) && TREE_VEC_LENGTH (TYPE_BINFO (rli->t)) > 6)
1129 TYPE_BINFO_SIZE (rli->t) = unpadded_size;
1130 TYPE_BINFO_SIZE_UNIT (rli->t) = unpadded_size_unit;
1133 /* Round the size up to be a multiple of the required alignment */
1134 #ifdef ROUND_TYPE_SIZE
1135 TYPE_SIZE (rli->t) = ROUND_TYPE_SIZE (rli->t, unpadded_size,
1136 TYPE_ALIGN (rli->t));
1137 TYPE_SIZE_UNIT (rli->t)
1138 = ROUND_TYPE_SIZE_UNIT (rli->t, unpadded_size_unit,
1139 TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
1141 TYPE_SIZE (rli->t) = round_up (unpadded_size, TYPE_ALIGN (rli->t));
1142 TYPE_SIZE_UNIT (rli->t) = round_up (unpadded_size_unit,
1143 TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
1146 if (warn_padded && TREE_CONSTANT (unpadded_size)
1147 && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0)
1148 warning ("padding struct size to alignment boundary");
1150 if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
1151 && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
1152 && TREE_CONSTANT (unpadded_size))
1156 #ifdef ROUND_TYPE_ALIGN
1158 = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t), rli->unpacked_align);
1160 rli->unpacked_align = MAX (TYPE_ALIGN (rli->t), rli->unpacked_align);
1163 #ifdef ROUND_TYPE_SIZE
1164 unpacked_size = ROUND_TYPE_SIZE (rli->t, TYPE_SIZE (rli->t),
1165 rli->unpacked_align);
1167 unpacked_size = round_up (TYPE_SIZE (rli->t), rli->unpacked_align);
1170 if (simple_cst_equal (unpacked_size, TYPE_SIZE (rli->t)))
1172 TYPE_PACKED (rli->t) = 0;
1174 if (TYPE_NAME (rli->t))
1178 if (TREE_CODE (TYPE_NAME (rli->t)) == IDENTIFIER_NODE)
1179 name = IDENTIFIER_POINTER (TYPE_NAME (rli->t));
1181 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (rli->t)));
1183 if (STRICT_ALIGNMENT)
1184 warning ("packed attribute causes inefficient alignment for `%s'", name);
1186 warning ("packed attribute is unnecessary for `%s'", name);
1190 if (STRICT_ALIGNMENT)
1191 warning ("packed attribute causes inefficient alignment");
1193 warning ("packed attribute is unnecessary");
1199 /* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE). */
1202 compute_record_mode (type)
1206 enum machine_mode mode = VOIDmode;
1208 /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
1209 However, if possible, we use a mode that fits in a register
1210 instead, in order to allow for better optimization down the
1212 TYPE_MODE (type) = BLKmode;
1214 if (! host_integerp (TYPE_SIZE (type), 1))
1217 /* A record which has any BLKmode members must itself be
1218 BLKmode; it can't go in a register. Unless the member is
1219 BLKmode only because it isn't aligned. */
1220 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1222 unsigned HOST_WIDE_INT bitpos;
1224 if (TREE_CODE (field) != FIELD_DECL)
1227 if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK
1228 || (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1229 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1230 || ! host_integerp (bit_position (field), 1)
1231 || DECL_SIZE (field) == 0
1232 || ! host_integerp (DECL_SIZE (field), 1))
1235 bitpos = int_bit_position (field);
1237 /* Must be BLKmode if any field crosses a word boundary,
1238 since extract_bit_field can't handle that in registers. */
1239 if (bitpos / BITS_PER_WORD
1240 != ((tree_low_cst (DECL_SIZE (field), 1) + bitpos - 1)
1242 /* But there is no problem if the field is entire words. */
1243 && tree_low_cst (DECL_SIZE (field), 1) % BITS_PER_WORD != 0)
1246 /* If this field is the whole struct, remember its mode so
1247 that, say, we can put a double in a class into a DF
1248 register instead of forcing it to live in the stack. */
1249 if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
1250 mode = DECL_MODE (field);
1252 #ifdef MEMBER_TYPE_FORCES_BLK
1253 /* With some targets, eg. c4x, it is sub-optimal
1254 to access an aligned BLKmode structure as a scalar. */
1256 /* On ia64-*-hpux we need to ensure that we don't change the
1257 mode of a structure containing a single field or else we
1258 will pass it incorrectly. Since a structure with a single
1259 field causes mode to get set above we can't allow the
1260 check for mode == VOIDmode in this case. Perhaps
1261 MEMBER_TYPE_FORCES_BLK should be extended to include mode
1262 as an argument and the check could be put in there for c4x. */
1264 if ((mode == VOIDmode || FUNCTION_ARG_REG_LITTLE_ENDIAN)
1265 && MEMBER_TYPE_FORCES_BLK (field))
1267 #endif /* MEMBER_TYPE_FORCES_BLK */
1270 /* If we only have one real field; use its mode. This only applies to
1271 RECORD_TYPE. This does not apply to unions. */
1272 if (TREE_CODE (type) == RECORD_TYPE && mode != VOIDmode)
1273 TYPE_MODE (type) = mode;
1275 TYPE_MODE (type) = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1277 /* If structure's known alignment is less than what the scalar
1278 mode would need, and it matters, then stick with BLKmode. */
1279 if (TYPE_MODE (type) != BLKmode
1281 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1282 || TYPE_ALIGN (type) >= GET_MODE_ALIGNMENT (TYPE_MODE (type))))
1284 /* If this is the only reason this type is BLKmode, then
1285 don't force containing types to be BLKmode. */
1286 TYPE_NO_FORCE_BLK (type) = 1;
1287 TYPE_MODE (type) = BLKmode;
1291 /* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
1295 finalize_type_size (type)
1298 /* Normally, use the alignment corresponding to the mode chosen.
1299 However, where strict alignment is not required, avoid
1300 over-aligning structures, since most compilers do not do this
1303 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1304 && (STRICT_ALIGNMENT
1305 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1306 && TREE_CODE (type) != QUAL_UNION_TYPE
1307 && TREE_CODE (type) != ARRAY_TYPE)))
1309 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1310 TYPE_USER_ALIGN (type) = 0;
1313 /* Do machine-dependent extra alignment. */
1314 #ifdef ROUND_TYPE_ALIGN
1316 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1319 /* If we failed to find a simple way to calculate the unit size
1320 of the type, find it by division. */
1321 if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1322 /* TYPE_SIZE (type) is computed in bitsizetype. After the division, the
1323 result will fit in sizetype. We will get more efficient code using
1324 sizetype, so we force a conversion. */
1325 TYPE_SIZE_UNIT (type)
1326 = convert (sizetype,
1327 size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1328 bitsize_unit_node));
1330 if (TYPE_SIZE (type) != 0)
1332 #ifdef ROUND_TYPE_SIZE
1334 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1335 TYPE_SIZE_UNIT (type)
1336 = ROUND_TYPE_SIZE_UNIT (type, TYPE_SIZE_UNIT (type),
1337 TYPE_ALIGN (type) / BITS_PER_UNIT);
1339 TYPE_SIZE (type) = round_up (TYPE_SIZE (type), TYPE_ALIGN (type));
1340 TYPE_SIZE_UNIT (type)
1341 = round_up (TYPE_SIZE_UNIT (type), TYPE_ALIGN (type) / BITS_PER_UNIT);
1345 /* Evaluate nonconstant sizes only once, either now or as soon as safe. */
1346 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1347 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1348 if (TYPE_SIZE_UNIT (type) != 0
1349 && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1350 TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1352 /* Also layout any other variants of the type. */
1353 if (TYPE_NEXT_VARIANT (type)
1354 || type != TYPE_MAIN_VARIANT (type))
1357 /* Record layout info of this variant. */
1358 tree size = TYPE_SIZE (type);
1359 tree size_unit = TYPE_SIZE_UNIT (type);
1360 unsigned int align = TYPE_ALIGN (type);
1361 unsigned int user_align = TYPE_USER_ALIGN (type);
1362 enum machine_mode mode = TYPE_MODE (type);
1364 /* Copy it into all variants. */
1365 for (variant = TYPE_MAIN_VARIANT (type);
1367 variant = TYPE_NEXT_VARIANT (variant))
1369 TYPE_SIZE (variant) = size;
1370 TYPE_SIZE_UNIT (variant) = size_unit;
1371 TYPE_ALIGN (variant) = align;
1372 TYPE_USER_ALIGN (variant) = user_align;
1373 TYPE_MODE (variant) = mode;
1378 /* Do all of the work required to layout the type indicated by RLI,
1379 once the fields have been laid out. This function will call `free'
1383 finish_record_layout (rli)
1384 record_layout_info rli;
1386 /* Compute the final size. */
1387 finalize_record_size (rli);
1389 /* Compute the TYPE_MODE for the record. */
1390 compute_record_mode (rli->t);
1392 /* Perform any last tweaks to the TYPE_SIZE, etc. */
1393 finalize_type_size (rli->t);
1395 /* Lay out any static members. This is done now because their type
1396 may use the record's type. */
1397 while (rli->pending_statics)
1399 layout_decl (TREE_VALUE (rli->pending_statics), 0);
1400 rli->pending_statics = TREE_CHAIN (rli->pending_statics);
1407 /* Calculate the mode, size, and alignment for TYPE.
1408 For an array type, calculate the element separation as well.
1409 Record TYPE on the chain of permanent or temporary types
1410 so that dbxout will find out about it.
1412 TYPE_SIZE of a type is nonzero if the type has been laid out already.
1413 layout_type does nothing on such a type.
1415 If the type is incomplete, its TYPE_SIZE remains zero. */
1424 /* Do nothing if type has been laid out before. */
1425 if (TYPE_SIZE (type))
1428 switch (TREE_CODE (type))
1431 /* This kind of type is the responsibility
1432 of the language-specific code. */
1435 case BOOLEAN_TYPE: /* Used for Java, Pascal, and Chill. */
1436 if (TYPE_PRECISION (type) == 0)
1437 TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
1439 /* ... fall through ... */
1444 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
1445 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
1446 TREE_UNSIGNED (type) = 1;
1448 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
1450 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1451 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1455 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
1456 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1457 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1461 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
1463 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
1464 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
1465 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
1467 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1468 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1475 subtype = TREE_TYPE (type);
1476 TREE_UNSIGNED (type) = TREE_UNSIGNED (subtype);
1477 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1478 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1483 /* This is an incomplete type and so doesn't have a size. */
1484 TYPE_ALIGN (type) = 1;
1485 TYPE_USER_ALIGN (type) = 0;
1486 TYPE_MODE (type) = VOIDmode;
1490 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
1491 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
1492 /* A pointer might be MODE_PARTIAL_INT,
1493 but ptrdiff_t must be integral. */
1494 TYPE_MODE (type) = mode_for_size (POINTER_SIZE, MODE_INT, 0);
1499 TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
1500 TYPE_SIZE (type) = bitsize_int (2 * POINTER_SIZE);
1501 TYPE_SIZE_UNIT (type) = size_int ((2 * POINTER_SIZE) / BITS_PER_UNIT);
1505 case REFERENCE_TYPE:
1507 int nbits = ((TREE_CODE (type) == REFERENCE_TYPE
1508 && reference_types_internal)
1509 ? GET_MODE_BITSIZE (Pmode) : POINTER_SIZE);
1511 TYPE_MODE (type) = nbits == POINTER_SIZE ? ptr_mode : Pmode;
1512 TYPE_SIZE (type) = bitsize_int (nbits);
1513 TYPE_SIZE_UNIT (type) = size_int (nbits / BITS_PER_UNIT);
1514 TREE_UNSIGNED (type) = 1;
1515 TYPE_PRECISION (type) = nbits;
1521 tree index = TYPE_DOMAIN (type);
1522 tree element = TREE_TYPE (type);
1524 build_pointer_type (element);
1526 /* We need to know both bounds in order to compute the size. */
1527 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
1528 && TYPE_SIZE (element))
1530 tree ub = TYPE_MAX_VALUE (index);
1531 tree lb = TYPE_MIN_VALUE (index);
1535 /* The initial subtraction should happen in the original type so
1536 that (possible) negative values are handled appropriately. */
1537 length = size_binop (PLUS_EXPR, size_one_node,
1539 fold (build (MINUS_EXPR,
1543 /* Special handling for arrays of bits (for Chill). */
1544 element_size = TYPE_SIZE (element);
1545 if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element)
1546 && (integer_zerop (TYPE_MAX_VALUE (element))
1547 || integer_onep (TYPE_MAX_VALUE (element)))
1548 && host_integerp (TYPE_MIN_VALUE (element), 1))
1550 HOST_WIDE_INT maxvalue
1551 = tree_low_cst (TYPE_MAX_VALUE (element), 1);
1552 HOST_WIDE_INT minvalue
1553 = tree_low_cst (TYPE_MIN_VALUE (element), 1);
1555 if (maxvalue - minvalue == 1
1556 && (maxvalue == 1 || maxvalue == 0))
1557 element_size = integer_one_node;
1560 TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size,
1561 convert (bitsizetype, length));
1563 /* If we know the size of the element, calculate the total
1564 size directly, rather than do some division thing below.
1565 This optimization helps Fortran assumed-size arrays
1566 (where the size of the array is determined at runtime)
1568 Note that we can't do this in the case where the size of
1569 the elements is one bit since TYPE_SIZE_UNIT cannot be
1570 set correctly in that case. */
1571 if (TYPE_SIZE_UNIT (element) != 0 && ! integer_onep (element_size))
1572 TYPE_SIZE_UNIT (type)
1573 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
1576 /* Now round the alignment and size,
1577 using machine-dependent criteria if any. */
1579 #ifdef ROUND_TYPE_ALIGN
1581 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
1583 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
1585 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (element);
1587 #ifdef ROUND_TYPE_SIZE
1588 if (TYPE_SIZE (type) != 0)
1591 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1593 /* If the rounding changed the size of the type, remove any
1594 pre-calculated TYPE_SIZE_UNIT. */
1595 if (simple_cst_equal (TYPE_SIZE (type), tmp) != 1)
1596 TYPE_SIZE_UNIT (type) = NULL;
1598 TYPE_SIZE (type) = tmp;
1602 TYPE_MODE (type) = BLKmode;
1603 if (TYPE_SIZE (type) != 0
1604 #ifdef MEMBER_TYPE_FORCES_BLK
1605 && ! MEMBER_TYPE_FORCES_BLK (type)
1607 /* BLKmode elements force BLKmode aggregate;
1608 else extract/store fields may lose. */
1609 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
1610 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
1612 /* One-element arrays get the component type's mode. */
1613 if (simple_cst_equal (TYPE_SIZE (type),
1614 TYPE_SIZE (TREE_TYPE (type))))
1615 TYPE_MODE (type) = TYPE_MODE (TREE_TYPE (type));
1618 = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1620 if (TYPE_MODE (type) != BLKmode
1621 && STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
1622 && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (TYPE_MODE (type))
1623 && TYPE_MODE (type) != BLKmode)
1625 TYPE_NO_FORCE_BLK (type) = 1;
1626 TYPE_MODE (type) = BLKmode;
1634 case QUAL_UNION_TYPE:
1637 record_layout_info rli;
1639 /* Initialize the layout information. */
1640 rli = start_record_layout (type);
1642 /* If this is a QUAL_UNION_TYPE, we want to process the fields
1643 in the reverse order in building the COND_EXPR that denotes
1644 its size. We reverse them again later. */
1645 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1646 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1648 /* Place all the fields. */
1649 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1650 place_field (rli, field);
1652 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1653 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1655 if (lang_adjust_rli)
1656 (*lang_adjust_rli) (rli);
1658 /* Finish laying out the record. */
1659 finish_record_layout (rli);
1663 case SET_TYPE: /* Used by Chill and Pascal. */
1664 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1665 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1669 #ifndef SET_WORD_SIZE
1670 #define SET_WORD_SIZE BITS_PER_WORD
1672 unsigned int alignment
1673 = set_alignment ? set_alignment : SET_WORD_SIZE;
1675 = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1676 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
1678 = ((size_in_bits + alignment - 1) / alignment) * alignment;
1680 if (rounded_size > (int) alignment)
1681 TYPE_MODE (type) = BLKmode;
1683 TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1685 TYPE_SIZE (type) = bitsize_int (rounded_size);
1686 TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1687 TYPE_ALIGN (type) = alignment;
1688 TYPE_USER_ALIGN (type) = 0;
1689 TYPE_PRECISION (type) = size_in_bits;
1694 /* The size may vary in different languages, so the language front end
1695 should fill in the size. */
1696 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1697 TYPE_USER_ALIGN (type) = 0;
1698 TYPE_MODE (type) = BLKmode;
1705 /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE. For
1706 records and unions, finish_record_layout already called this
1708 if (TREE_CODE (type) != RECORD_TYPE
1709 && TREE_CODE (type) != UNION_TYPE
1710 && TREE_CODE (type) != QUAL_UNION_TYPE)
1711 finalize_type_size (type);
1713 /* If this type is created before sizetype has been permanently set,
1714 record it so set_sizetype can fix it up. */
1716 early_type_list = tree_cons (NULL_TREE, type, early_type_list);
1718 /* If an alias set has been set for this aggregate when it was incomplete,
1719 force it into alias set 0.
1720 This is too conservative, but we cannot call record_component_aliases
1721 here because some frontends still change the aggregates after
1723 if (AGGREGATE_TYPE_P (type) && TYPE_ALIAS_SET_KNOWN_P (type))
1724 TYPE_ALIAS_SET (type) = 0;
1727 /* Create and return a type for signed integers of PRECISION bits. */
1730 make_signed_type (precision)
1733 tree type = make_node (INTEGER_TYPE);
1735 TYPE_PRECISION (type) = precision;
1737 fixup_signed_type (type);
1741 /* Create and return a type for unsigned integers of PRECISION bits. */
1744 make_unsigned_type (precision)
1747 tree type = make_node (INTEGER_TYPE);
1749 TYPE_PRECISION (type) = precision;
1751 fixup_unsigned_type (type);
1755 /* Initialize sizetype and bitsizetype to a reasonable and temporary
1756 value to enable integer types to be created. */
1759 initialize_sizetypes ()
1761 tree t = make_node (INTEGER_TYPE);
1763 /* Set this so we do something reasonable for the build_int_2 calls
1765 integer_type_node = t;
1767 TYPE_MODE (t) = SImode;
1768 TYPE_ALIGN (t) = GET_MODE_ALIGNMENT (SImode);
1769 TYPE_USER_ALIGN (t) = 0;
1770 TYPE_SIZE (t) = build_int_2 (GET_MODE_BITSIZE (SImode), 0);
1771 TYPE_SIZE_UNIT (t) = build_int_2 (GET_MODE_SIZE (SImode), 0);
1772 TREE_UNSIGNED (t) = 1;
1773 TYPE_PRECISION (t) = GET_MODE_BITSIZE (SImode);
1774 TYPE_MIN_VALUE (t) = build_int_2 (0, 0);
1775 TYPE_IS_SIZETYPE (t) = 1;
1777 /* 1000 avoids problems with possible overflow and is certainly
1778 larger than any size value we'd want to be storing. */
1779 TYPE_MAX_VALUE (t) = build_int_2 (1000, 0);
1781 /* These two must be different nodes because of the caching done in
1784 bitsizetype = copy_node (t);
1785 integer_type_node = 0;
1788 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1789 Also update the type of any standard type's sizes made so far. */
1795 int oprecision = TYPE_PRECISION (type);
1796 /* The *bitsizetype types use a precision that avoids overflows when
1797 calculating signed sizes / offsets in bits. However, when
1798 cross-compiling from a 32 bit to a 64 bit host, we are limited to 64 bit
1800 int precision = MIN (oprecision + BITS_PER_UNIT_LOG + 1,
1801 2 * HOST_BITS_PER_WIDE_INT);
1808 /* Make copies of nodes since we'll be setting TYPE_IS_SIZETYPE. */
1809 sizetype = copy_node (type);
1810 TYPE_DOMAIN (sizetype) = type;
1811 TYPE_IS_SIZETYPE (sizetype) = 1;
1812 bitsizetype = make_node (INTEGER_TYPE);
1813 TYPE_NAME (bitsizetype) = TYPE_NAME (type);
1814 TYPE_PRECISION (bitsizetype) = precision;
1815 TYPE_IS_SIZETYPE (bitsizetype) = 1;
1817 if (TREE_UNSIGNED (type))
1818 fixup_unsigned_type (bitsizetype);
1820 fixup_signed_type (bitsizetype);
1822 layout_type (bitsizetype);
1824 if (TREE_UNSIGNED (type))
1826 usizetype = sizetype;
1827 ubitsizetype = bitsizetype;
1828 ssizetype = copy_node (make_signed_type (oprecision));
1829 sbitsizetype = copy_node (make_signed_type (precision));
1833 ssizetype = sizetype;
1834 sbitsizetype = bitsizetype;
1835 usizetype = copy_node (make_unsigned_type (oprecision));
1836 ubitsizetype = copy_node (make_unsigned_type (precision));
1839 TYPE_NAME (bitsizetype) = get_identifier ("bit_size_type");
1841 /* Show is a sizetype, is a main type, and has no pointers to it. */
1842 for (i = 0; i < ARRAY_SIZE (sizetype_tab); i++)
1844 TYPE_IS_SIZETYPE (sizetype_tab[i]) = 1;
1845 TYPE_MAIN_VARIANT (sizetype_tab[i]) = sizetype_tab[i];
1846 TYPE_NEXT_VARIANT (sizetype_tab[i]) = 0;
1847 TYPE_POINTER_TO (sizetype_tab[i]) = 0;
1848 TYPE_REFERENCE_TO (sizetype_tab[i]) = 0;
1851 ggc_add_tree_root ((tree *) &sizetype_tab,
1852 sizeof sizetype_tab / sizeof (tree));
1854 /* Go down each of the types we already made and set the proper type
1855 for the sizes in them. */
1856 for (t = early_type_list; t != 0; t = TREE_CHAIN (t))
1858 if (TREE_CODE (TREE_VALUE (t)) != INTEGER_TYPE)
1861 TREE_TYPE (TYPE_SIZE (TREE_VALUE (t))) = bitsizetype;
1862 TREE_TYPE (TYPE_SIZE_UNIT (TREE_VALUE (t))) = sizetype;
1865 early_type_list = 0;
1869 /* Set the extreme values of TYPE based on its precision in bits,
1870 then lay it out. Used when make_signed_type won't do
1871 because the tree code is not INTEGER_TYPE.
1872 E.g. for Pascal, when the -fsigned-char option is given. */
1875 fixup_signed_type (type)
1878 int precision = TYPE_PRECISION (type);
1880 /* We can not represent properly constants greater then
1881 2 * HOST_BITS_PER_WIDE_INT, still we need the types
1882 as they are used by i386 vector extensions and friends. */
1883 if (precision > HOST_BITS_PER_WIDE_INT * 2)
1884 precision = HOST_BITS_PER_WIDE_INT * 2;
1886 TYPE_MIN_VALUE (type)
1887 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1888 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1889 (((HOST_WIDE_INT) (-1)
1890 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1891 ? precision - HOST_BITS_PER_WIDE_INT - 1
1893 TYPE_MAX_VALUE (type)
1894 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1895 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1896 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1897 ? (((HOST_WIDE_INT) 1
1898 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1901 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1902 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1904 /* Lay out the type: set its alignment, size, etc. */
1908 /* Set the extreme values of TYPE based on its precision in bits,
1909 then lay it out. This is used both in `make_unsigned_type'
1910 and for enumeral types. */
1913 fixup_unsigned_type (type)
1916 int precision = TYPE_PRECISION (type);
1918 /* We can not represent properly constants greater then
1919 2 * HOST_BITS_PER_WIDE_INT, still we need the types
1920 as they are used by i386 vector extensions and friends. */
1921 if (precision > HOST_BITS_PER_WIDE_INT * 2)
1922 precision = HOST_BITS_PER_WIDE_INT * 2;
1924 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1925 TYPE_MAX_VALUE (type)
1926 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1927 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1928 precision - HOST_BITS_PER_WIDE_INT > 0
1929 ? ((unsigned HOST_WIDE_INT) ~0
1930 >> (HOST_BITS_PER_WIDE_INT
1931 - (precision - HOST_BITS_PER_WIDE_INT)))
1933 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1934 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1936 /* Lay out the type: set its alignment, size, etc. */
1940 /* Find the best machine mode to use when referencing a bit field of length
1941 BITSIZE bits starting at BITPOS.
1943 The underlying object is known to be aligned to a boundary of ALIGN bits.
1944 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1945 larger than LARGEST_MODE (usually SImode).
1947 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1948 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1949 mode meeting these conditions.
1951 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1952 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1953 all the conditions. */
1956 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1957 int bitsize, bitpos;
1959 enum machine_mode largest_mode;
1962 enum machine_mode mode;
1963 unsigned int unit = 0;
1965 /* Find the narrowest integer mode that contains the bit field. */
1966 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1967 mode = GET_MODE_WIDER_MODE (mode))
1969 unit = GET_MODE_BITSIZE (mode);
1970 if ((bitpos % unit) + bitsize <= unit)
1974 if (mode == VOIDmode
1975 /* It is tempting to omit the following line
1976 if STRICT_ALIGNMENT is true.
1977 But that is incorrect, since if the bitfield uses part of 3 bytes
1978 and we use a 4-byte mode, we could get a spurious segv
1979 if the extra 4th byte is past the end of memory.
1980 (Though at least one Unix compiler ignores this problem:
1981 that on the Sequent 386 machine. */
1982 || MIN (unit, BIGGEST_ALIGNMENT) > align
1983 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1986 if (SLOW_BYTE_ACCESS && ! volatilep)
1988 enum machine_mode wide_mode = VOIDmode, tmode;
1990 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1991 tmode = GET_MODE_WIDER_MODE (tmode))
1993 unit = GET_MODE_BITSIZE (tmode);
1994 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1995 && unit <= BITS_PER_WORD
1996 && unit <= MIN (align, BIGGEST_ALIGNMENT)
1997 && (largest_mode == VOIDmode
1998 || unit <= GET_MODE_BITSIZE (largest_mode)))
2002 if (wide_mode != VOIDmode)
2009 /* This function is run once to initialize stor-layout.c. */
2012 init_stor_layout_once ()
2014 ggc_add_tree_root (&pending_sizes, 1);