1 /* C-compiler utilities for types and variables storage layout
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1996, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
34 /* Set to one when set_sizetype has been called. */
35 static int sizetype_set;
37 /* List of types created before set_sizetype has been called. We do not
38 make this a GGC root since we want these nodes to be reclaimed. */
39 static tree early_type_list;
41 /* Data type for the expressions representing sizes of data types.
42 It is the first integer type laid out. */
43 tree sizetype_tab[(int) TYPE_KIND_LAST];
45 /* If nonzero, this is an upper limit on alignment of structure fields.
46 The value is measured in bits. */
47 unsigned int maximum_field_alignment;
49 /* If non-zero, the alignment of a bitstring or (power-)set value, in bits.
50 May be overridden by front-ends. */
51 unsigned int set_alignment = 0;
53 /* Nonzero if all REFERENCE_TYPEs are internal and hence should be
54 allocated in Pmode, not ptr_mode. Set only by internal_reference_types
55 called only by a front end. */
56 static int reference_types_internal = 0;
58 static void finalize_record_size PARAMS ((record_layout_info));
59 static void finalize_type_size PARAMS ((tree));
60 static void place_union_field PARAMS ((record_layout_info, tree));
61 extern void debug_rli PARAMS ((record_layout_info));
63 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
65 static tree pending_sizes;
67 /* Nonzero means cannot safely call expand_expr now,
68 so put variable sizes onto `pending_sizes' instead. */
70 int immediate_size_expand;
72 /* Show that REFERENCE_TYPES are internal and should be Pmode. Called only
76 internal_reference_types ()
78 reference_types_internal = 1;
81 /* Get a list of all the objects put on the pending sizes list. */
86 tree chain = pending_sizes;
89 /* Put each SAVE_EXPR into the current function. */
90 for (t = chain; t; t = TREE_CHAIN (t))
91 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
97 /* Return non-zero if EXPR is present on the pending sizes list. */
100 is_pending_size (expr)
105 for (t = pending_sizes; t; t = TREE_CHAIN (t))
106 if (TREE_VALUE (t) == expr)
111 /* Add EXPR to the pending sizes list. */
114 put_pending_size (expr)
117 /* Strip any simple arithmetic from EXPR to see if it has an underlying
119 while (TREE_CODE_CLASS (TREE_CODE (expr)) == '1'
120 || (TREE_CODE_CLASS (TREE_CODE (expr)) == '2'
121 && TREE_CONSTANT (TREE_OPERAND (expr, 1))))
122 expr = TREE_OPERAND (expr, 0);
124 if (TREE_CODE (expr) == SAVE_EXPR)
125 pending_sizes = tree_cons (NULL_TREE, expr, pending_sizes);
128 /* Put a chain of objects into the pending sizes list, which must be
132 put_pending_sizes (chain)
138 pending_sizes = chain;
141 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
142 to serve as the actual size-expression for a type or decl. */
148 /* If the language-processor is to take responsibility for variable-sized
149 items (e.g., languages which have elaboration procedures like Ada),
150 just return SIZE unchanged. Likewise for self-referential sizes and
152 if (TREE_CONSTANT (size)
153 || global_bindings_p () < 0 || contains_placeholder_p (size))
156 size = save_expr (size);
158 /* If an array with a variable number of elements is declared, and
159 the elements require destruction, we will emit a cleanup for the
160 array. That cleanup is run both on normal exit from the block
161 and in the exception-handler for the block. Normally, when code
162 is used in both ordinary code and in an exception handler it is
163 `unsaved', i.e., all SAVE_EXPRs are recalculated. However, we do
164 not wish to do that here; the array-size is the same in both
166 if (TREE_CODE (size) == SAVE_EXPR)
167 SAVE_EXPR_PERSISTENT_P (size) = 1;
169 if (global_bindings_p ())
171 if (TREE_CONSTANT (size))
172 error ("type size can't be explicitly evaluated");
174 error ("variable-size type declared outside of any function");
176 return size_one_node;
179 if (immediate_size_expand)
180 /* NULL_RTX is not defined; neither is the rtx type.
181 Also, we would like to pass const0_rtx here, but don't have it. */
182 expand_expr (size, expand_expr (integer_zero_node, NULL_RTX, VOIDmode, 0),
184 else if (cfun != 0 && cfun->x_dont_save_pending_sizes_p)
185 /* The front-end doesn't want us to keep a list of the expressions
186 that determine sizes for variable size objects. */
189 put_pending_size (size);
194 #ifndef MAX_FIXED_MODE_SIZE
195 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
198 /* Return the machine mode to use for a nonscalar of SIZE bits.
199 The mode must be in class CLASS, and have exactly that many bits.
200 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
204 mode_for_size (size, class, limit)
206 enum mode_class class;
209 enum machine_mode mode;
211 if (limit && size > MAX_FIXED_MODE_SIZE)
214 /* Get the first mode which has this size, in the specified class. */
215 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
216 mode = GET_MODE_WIDER_MODE (mode))
217 if (GET_MODE_BITSIZE (mode) == size)
223 /* Similar, except passed a tree node. */
226 mode_for_size_tree (size, class, limit)
228 enum mode_class class;
231 if (TREE_CODE (size) != INTEGER_CST
232 /* What we really want to say here is that the size can fit in a
233 host integer, but we know there's no way we'd find a mode for
234 this many bits, so there's no point in doing the precise test. */
235 || compare_tree_int (size, 1000) > 0)
238 return mode_for_size (TREE_INT_CST_LOW (size), class, limit);
241 /* Similar, but never return BLKmode; return the narrowest mode that
242 contains at least the requested number of bits. */
245 smallest_mode_for_size (size, class)
247 enum mode_class class;
249 enum machine_mode mode;
251 /* Get the first mode which has at least this size, in the
253 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
254 mode = GET_MODE_WIDER_MODE (mode))
255 if (GET_MODE_BITSIZE (mode) >= size)
261 /* Find an integer mode of the exact same size, or BLKmode on failure. */
264 int_mode_for_mode (mode)
265 enum machine_mode mode;
267 switch (GET_MODE_CLASS (mode))
270 case MODE_PARTIAL_INT:
273 case MODE_COMPLEX_INT:
274 case MODE_COMPLEX_FLOAT:
276 case MODE_VECTOR_INT:
277 case MODE_VECTOR_FLOAT:
278 mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
285 /* ... fall through ... */
295 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
296 This can only be applied to objects of a sizetype. */
299 round_up (value, divisor)
303 tree arg = size_int_type (divisor, TREE_TYPE (value));
305 return size_binop (MULT_EXPR, size_binop (CEIL_DIV_EXPR, value, arg), arg);
308 /* Likewise, but round down. */
311 round_down (value, divisor)
315 tree arg = size_int_type (divisor, TREE_TYPE (value));
317 return size_binop (MULT_EXPR, size_binop (FLOOR_DIV_EXPR, value, arg), arg);
320 /* Set the size, mode and alignment of a ..._DECL node.
321 TYPE_DECL does need this for C++.
322 Note that LABEL_DECL and CONST_DECL nodes do not need this,
323 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
324 Don't call layout_decl for them.
326 KNOWN_ALIGN is the amount of alignment we can assume this
327 decl has with no special effort. It is relevant only for FIELD_DECLs
328 and depends on the previous fields.
329 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
330 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
331 the record will be aligned to suit. */
334 layout_decl (decl, known_align)
336 unsigned int known_align;
338 tree type = TREE_TYPE (decl);
339 enum tree_code code = TREE_CODE (decl);
341 if (code == CONST_DECL)
343 else if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
344 && code != TYPE_DECL && code != FIELD_DECL)
347 if (type == error_mark_node)
348 type = void_type_node;
350 /* Usually the size and mode come from the data type without change,
351 however, the front-end may set the explicit width of the field, so its
352 size may not be the same as the size of its type. This happens with
353 bitfields, of course (an `int' bitfield may be only 2 bits, say), but it
354 also happens with other fields. For example, the C++ front-end creates
355 zero-sized fields corresponding to empty base classes, and depends on
356 layout_type setting DECL_FIELD_BITPOS correctly for the field. Set the
357 size in bytes from the size in bits. If we have already set the mode,
358 don't set it again since we can be called twice for FIELD_DECLs. */
360 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
361 if (DECL_MODE (decl) == VOIDmode)
362 DECL_MODE (decl) = TYPE_MODE (type);
364 if (DECL_SIZE (decl) == 0)
366 DECL_SIZE (decl) = TYPE_SIZE (type);
367 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
370 DECL_SIZE_UNIT (decl)
371 = convert (sizetype, size_binop (CEIL_DIV_EXPR, DECL_SIZE (decl),
374 /* Force alignment required for the data type.
375 But if the decl itself wants greater alignment, don't override that.
376 Likewise, if the decl is packed, don't override it. */
377 if (! (code == FIELD_DECL && DECL_BIT_FIELD (decl))
378 && (DECL_ALIGN (decl) == 0
379 || (! (code == FIELD_DECL && DECL_PACKED (decl))
380 && TYPE_ALIGN (type) > DECL_ALIGN (decl))))
382 DECL_ALIGN (decl) = TYPE_ALIGN (type);
383 DECL_USER_ALIGN (decl) = 0;
386 /* For fields, set the bit field type and update the alignment. */
387 if (code == FIELD_DECL)
389 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
390 if (maximum_field_alignment != 0)
391 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
392 else if (DECL_PACKED (decl))
394 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
395 DECL_USER_ALIGN (decl) = 0;
399 /* See if we can use an ordinary integer mode for a bit-field.
400 Conditions are: a fixed size that is correct for another mode
401 and occupying a complete byte or bytes on proper boundary. */
402 if (code == FIELD_DECL && DECL_BIT_FIELD (decl)
403 && TYPE_SIZE (type) != 0
404 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
405 && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
407 enum machine_mode xmode
408 = mode_for_size_tree (DECL_SIZE (decl), MODE_INT, 1);
410 if (xmode != BLKmode && known_align >= GET_MODE_ALIGNMENT (xmode))
412 DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
414 DECL_MODE (decl) = xmode;
415 DECL_BIT_FIELD (decl) = 0;
419 /* Turn off DECL_BIT_FIELD if we won't need it set. */
420 if (code == FIELD_DECL && DECL_BIT_FIELD (decl)
421 && TYPE_MODE (type) == BLKmode && DECL_MODE (decl) == BLKmode
422 && known_align >= TYPE_ALIGN (type)
423 && DECL_ALIGN (decl) >= TYPE_ALIGN (type)
424 && DECL_SIZE_UNIT (decl) != 0)
425 DECL_BIT_FIELD (decl) = 0;
427 /* Evaluate nonconstant size only once, either now or as soon as safe. */
428 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
429 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
430 if (DECL_SIZE_UNIT (decl) != 0
431 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST)
432 DECL_SIZE_UNIT (decl) = variable_size (DECL_SIZE_UNIT (decl));
434 /* If requested, warn about definitions of large data objects. */
436 && (code == VAR_DECL || code == PARM_DECL)
437 && ! DECL_EXTERNAL (decl))
439 tree size = DECL_SIZE_UNIT (decl);
441 if (size != 0 && TREE_CODE (size) == INTEGER_CST
442 && compare_tree_int (size, larger_than_size) > 0)
444 unsigned int size_as_int = TREE_INT_CST_LOW (size);
446 if (compare_tree_int (size, size_as_int) == 0)
447 warning_with_decl (decl, "size of `%s' is %d bytes", size_as_int);
449 warning_with_decl (decl, "size of `%s' is larger than %d bytes",
455 /* Hook for a front-end function that can modify the record layout as needed
456 immediately before it is finalized. */
458 void (*lang_adjust_rli) PARAMS ((record_layout_info)) = 0;
461 set_lang_adjust_rli (f)
462 void (*f) PARAMS ((record_layout_info));
467 /* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
468 QUAL_UNION_TYPE. Return a pointer to a struct record_layout_info which
469 is to be passed to all other layout functions for this record. It is the
470 responsibility of the caller to call `free' for the storage returned.
471 Note that garbage collection is not permitted until we finish laying
475 start_record_layout (t)
478 record_layout_info rli
479 = (record_layout_info) xmalloc (sizeof (struct record_layout_info_s));
483 /* If the type has a minimum specified alignment (via an attribute
484 declaration, for example) use it -- otherwise, start with a
485 one-byte alignment. */
486 rli->record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (t));
487 rli->unpacked_align = rli->unpadded_align = rli->record_align;
488 rli->offset_align = MAX (rli->record_align, BIGGEST_ALIGNMENT);
490 #ifdef STRUCTURE_SIZE_BOUNDARY
491 /* Packed structures don't need to have minimum size. */
492 if (! TYPE_PACKED (t))
493 rli->record_align = MAX (rli->record_align, STRUCTURE_SIZE_BOUNDARY);
496 rli->offset = size_zero_node;
497 rli->bitpos = bitsize_zero_node;
498 rli->pending_statics = 0;
499 rli->packed_maybe_necessary = 0;
504 /* These four routines perform computations that convert between
505 the offset/bitpos forms and byte and bit offsets. */
508 bit_from_pos (offset, bitpos)
511 return size_binop (PLUS_EXPR, bitpos,
512 size_binop (MULT_EXPR, convert (bitsizetype, offset),
517 byte_from_pos (offset, bitpos)
520 return size_binop (PLUS_EXPR, offset,
522 size_binop (TRUNC_DIV_EXPR, bitpos,
523 bitsize_unit_node)));
527 pos_from_byte (poffset, pbitpos, off_align, pos)
528 tree *poffset, *pbitpos;
529 unsigned int off_align;
533 = size_binop (MULT_EXPR,
535 size_binop (FLOOR_DIV_EXPR, pos,
536 bitsize_int (off_align
538 size_int (off_align / BITS_PER_UNIT));
539 *pbitpos = size_binop (MULT_EXPR,
540 size_binop (FLOOR_MOD_EXPR, pos,
541 bitsize_int (off_align / BITS_PER_UNIT)),
546 pos_from_bit (poffset, pbitpos, off_align, pos)
547 tree *poffset, *pbitpos;
548 unsigned int off_align;
551 *poffset = size_binop (MULT_EXPR,
553 size_binop (FLOOR_DIV_EXPR, pos,
554 bitsize_int (off_align))),
555 size_int (off_align / BITS_PER_UNIT));
556 *pbitpos = size_binop (FLOOR_MOD_EXPR, pos, bitsize_int (off_align));
559 /* Given a pointer to bit and byte offsets and an offset alignment,
560 normalize the offsets so they are within the alignment. */
563 normalize_offset (poffset, pbitpos, off_align)
564 tree *poffset, *pbitpos;
565 unsigned int off_align;
567 /* If the bit position is now larger than it should be, adjust it
569 if (compare_tree_int (*pbitpos, off_align) >= 0)
571 tree extra_aligns = size_binop (FLOOR_DIV_EXPR, *pbitpos,
572 bitsize_int (off_align));
575 = size_binop (PLUS_EXPR, *poffset,
576 size_binop (MULT_EXPR, convert (sizetype, extra_aligns),
577 size_int (off_align / BITS_PER_UNIT)));
580 = size_binop (FLOOR_MOD_EXPR, *pbitpos, bitsize_int (off_align));
584 /* Print debugging information about the information in RLI. */
588 record_layout_info rli;
590 print_node_brief (stderr, "type", rli->t, 0);
591 print_node_brief (stderr, "\noffset", rli->offset, 0);
592 print_node_brief (stderr, " bitpos", rli->bitpos, 0);
594 fprintf (stderr, "\naligns: rec = %u, unpack = %u, unpad = %u, off = %u\n",
595 rli->record_align, rli->unpacked_align, rli->unpadded_align,
597 if (rli->packed_maybe_necessary)
598 fprintf (stderr, "packed may be necessary\n");
600 if (rli->pending_statics)
602 fprintf (stderr, "pending statics:\n");
603 debug_tree (rli->pending_statics);
607 /* Given an RLI with a possibly-incremented BITPOS, adjust OFFSET and
608 BITPOS if necessary to keep BITPOS below OFFSET_ALIGN. */
612 record_layout_info rli;
614 normalize_offset (&rli->offset, &rli->bitpos, rli->offset_align);
617 /* Returns the size in bytes allocated so far. */
620 rli_size_unit_so_far (rli)
621 record_layout_info rli;
623 return byte_from_pos (rli->offset, rli->bitpos);
626 /* Returns the size in bits allocated so far. */
629 rli_size_so_far (rli)
630 record_layout_info rli;
632 return bit_from_pos (rli->offset, rli->bitpos);
635 /* Called from place_field to handle unions. */
638 place_union_field (rli, field)
639 record_layout_info rli;
642 unsigned int desired_align;
644 layout_decl (field, 0);
646 DECL_FIELD_OFFSET (field) = size_zero_node;
647 DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
648 SET_DECL_OFFSET_ALIGN (field, BIGGEST_ALIGNMENT);
650 desired_align = DECL_ALIGN (field);
652 #ifdef BIGGEST_FIELD_ALIGNMENT
653 /* Some targets (i.e. i386) limit union field alignment
654 to a lower boundary than alignment of variables unless
655 it was overridden by attribute aligned. */
656 if (! DECL_USER_ALIGN (field))
658 MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
661 #ifdef ADJUST_FIELD_ALIGN
662 desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
665 TYPE_USER_ALIGN (rli->t) |= DECL_USER_ALIGN (field);
667 /* Union must be at least as aligned as any field requires. */
668 rli->record_align = MAX (rli->record_align, desired_align);
669 rli->unpadded_align = MAX (rli->unpadded_align, desired_align);
671 #ifdef PCC_BITFIELD_TYPE_MATTERS
672 /* On the m88000, a bit field of declare type `int' forces the
673 entire union to have `int' alignment. */
674 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
676 rli->record_align = MAX (rli->record_align,
677 TYPE_ALIGN (TREE_TYPE (field)));
678 rli->unpadded_align = MAX (rli->unpadded_align,
679 TYPE_ALIGN (TREE_TYPE (field)));
683 /* We assume the union's size will be a multiple of a byte so we don't
684 bother with BITPOS. */
685 if (TREE_CODE (rli->t) == UNION_TYPE)
686 rli->offset = size_binop (MAX_EXPR, rli->offset, DECL_SIZE_UNIT (field));
687 else if (TREE_CODE (rli->t) == QUAL_UNION_TYPE)
688 rli->offset = fold (build (COND_EXPR, sizetype,
689 DECL_QUALIFIER (field),
690 DECL_SIZE_UNIT (field), rli->offset));
693 /* RLI contains information about the layout of a RECORD_TYPE. FIELD
694 is a FIELD_DECL to be added after those fields already present in
695 T. (FIELD is not actually added to the TYPE_FIELDS list here;
696 callers that desire that behavior must manually perform that step.) */
699 place_field (rli, field)
700 record_layout_info rli;
703 /* The alignment required for FIELD. */
704 unsigned int desired_align;
705 /* The alignment FIELD would have if we just dropped it into the
706 record as it presently stands. */
707 unsigned int known_align;
708 unsigned int actual_align;
709 unsigned int user_align;
710 /* The type of this field. */
711 tree type = TREE_TYPE (field);
713 if (TREE_CODE (field) == ERROR_MARK || TREE_CODE (type) == ERROR_MARK)
716 /* If FIELD is static, then treat it like a separate variable, not
717 really like a structure field. If it is a FUNCTION_DECL, it's a
718 method. In both cases, all we do is lay out the decl, and we do
719 it *after* the record is laid out. */
720 if (TREE_CODE (field) == VAR_DECL)
722 rli->pending_statics = tree_cons (NULL_TREE, field,
723 rli->pending_statics);
727 /* Enumerators and enum types which are local to this class need not
728 be laid out. Likewise for initialized constant fields. */
729 else if (TREE_CODE (field) != FIELD_DECL)
732 /* Unions are laid out very differently than records, so split
733 that code off to another function. */
734 else if (TREE_CODE (rli->t) != RECORD_TYPE)
736 place_union_field (rli, field);
740 /* Work out the known alignment so far. Note that A & (-A) is the
741 value of the least-significant bit in A that is one. */
742 if (! integer_zerop (rli->bitpos))
743 known_align = (tree_low_cst (rli->bitpos, 1)
744 & - tree_low_cst (rli->bitpos, 1));
745 else if (integer_zerop (rli->offset))
746 known_align = BIGGEST_ALIGNMENT;
747 else if (host_integerp (rli->offset, 1))
748 known_align = (BITS_PER_UNIT
749 * (tree_low_cst (rli->offset, 1)
750 & - tree_low_cst (rli->offset, 1)));
752 known_align = rli->offset_align;
754 /* Lay out the field so we know what alignment it needs. For a
755 packed field, use the alignment as specified, disregarding what
756 the type would want. */
757 desired_align = DECL_ALIGN (field);
758 user_align = DECL_USER_ALIGN (field);
759 layout_decl (field, known_align);
760 if (! DECL_PACKED (field))
762 desired_align = DECL_ALIGN (field);
763 user_align = DECL_USER_ALIGN (field);
766 /* Some targets (i.e. i386, VMS) limit struct field alignment
767 to a lower boundary than alignment of variables unless
768 it was overridden by attribute aligned. */
769 #ifdef BIGGEST_FIELD_ALIGNMENT
772 = MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
775 #ifdef ADJUST_FIELD_ALIGN
776 desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
779 /* Record must have at least as much alignment as any field.
780 Otherwise, the alignment of the field within the record is
782 #ifdef PCC_BITFIELD_TYPE_MATTERS
783 if (PCC_BITFIELD_TYPE_MATTERS && type != error_mark_node
784 && DECL_BIT_FIELD_TYPE (field)
785 && ! integer_zerop (TYPE_SIZE (type)))
787 /* For these machines, a zero-length field does not
788 affect the alignment of the structure as a whole.
789 It does, however, affect the alignment of the next field
790 within the structure. */
791 if (! integer_zerop (DECL_SIZE (field)))
792 rli->record_align = MAX (rli->record_align, desired_align);
793 else if (! DECL_PACKED (field))
794 desired_align = TYPE_ALIGN (type);
796 /* A named bit field of declared type `int'
797 forces the entire structure to have `int' alignment. */
798 if (DECL_NAME (field) != 0)
800 unsigned int type_align = TYPE_ALIGN (type);
802 if (maximum_field_alignment != 0)
803 type_align = MIN (type_align, maximum_field_alignment);
804 else if (DECL_PACKED (field))
805 type_align = MIN (type_align, BITS_PER_UNIT);
807 rli->record_align = MAX (rli->record_align, type_align);
808 rli->unpadded_align = MAX (rli->unpadded_align, DECL_ALIGN (field));
810 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
816 rli->record_align = MAX (rli->record_align, desired_align);
817 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
818 rli->unpadded_align = MAX (rli->unpadded_align, DECL_ALIGN (field));
821 if (warn_packed && DECL_PACKED (field))
823 if (known_align > TYPE_ALIGN (type))
825 if (TYPE_ALIGN (type) > desired_align)
827 if (STRICT_ALIGNMENT)
828 warning_with_decl (field, "packed attribute causes inefficient alignment for `%s'");
830 warning_with_decl (field, "packed attribute is unnecessary for `%s'");
834 rli->packed_maybe_necessary = 1;
837 /* Does this field automatically have alignment it needs by virtue
838 of the fields that precede it and the record's own alignment? */
839 if (known_align < desired_align)
841 /* No, we need to skip space before this field.
842 Bump the cumulative size to multiple of field alignment. */
845 warning_with_decl (field, "padding struct to align `%s'");
847 /* If the alignment is still within offset_align, just align
849 if (desired_align < rli->offset_align)
850 rli->bitpos = round_up (rli->bitpos, desired_align);
853 /* First adjust OFFSET by the partial bits, then align. */
855 = size_binop (PLUS_EXPR, rli->offset,
857 size_binop (CEIL_DIV_EXPR, rli->bitpos,
858 bitsize_unit_node)));
859 rli->bitpos = bitsize_zero_node;
861 rli->offset = round_up (rli->offset, desired_align / BITS_PER_UNIT);
864 if (! TREE_CONSTANT (rli->offset))
865 rli->offset_align = desired_align;
869 /* Handle compatibility with PCC. Note that if the record has any
870 variable-sized fields, we need not worry about compatibility. */
871 #ifdef PCC_BITFIELD_TYPE_MATTERS
872 if (PCC_BITFIELD_TYPE_MATTERS
873 && TREE_CODE (field) == FIELD_DECL
874 && type != error_mark_node
875 && DECL_BIT_FIELD (field)
876 && ! DECL_PACKED (field)
877 && maximum_field_alignment == 0
878 && ! integer_zerop (DECL_SIZE (field))
879 && host_integerp (DECL_SIZE (field), 1)
880 && host_integerp (rli->offset, 1)
881 && host_integerp (TYPE_SIZE (type), 1))
883 unsigned int type_align = TYPE_ALIGN (type);
884 tree dsize = DECL_SIZE (field);
885 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
886 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
887 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
889 /* A bit field may not span more units of alignment of its type
890 than its type itself. Advance to next boundary if necessary. */
891 if ((((offset * BITS_PER_UNIT + bit_offset + field_size +
894 - (offset * BITS_PER_UNIT + bit_offset) / type_align)
895 > tree_low_cst (TYPE_SIZE (type), 1) / type_align)
896 rli->bitpos = round_up (rli->bitpos, type_align);
900 #ifdef BITFIELD_NBYTES_LIMITED
901 if (BITFIELD_NBYTES_LIMITED
902 && TREE_CODE (field) == FIELD_DECL
903 && type != error_mark_node
904 && DECL_BIT_FIELD_TYPE (field)
905 && ! DECL_PACKED (field)
906 && ! integer_zerop (DECL_SIZE (field))
907 && host_integerp (DECL_SIZE (field), 1)
908 && host_integerp (rli->offset, 1)
909 && host_integerp (TYPE_SIZE (type), 1))
911 unsigned int type_align = TYPE_ALIGN (type);
912 tree dsize = DECL_SIZE (field);
913 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
914 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
915 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
917 if (maximum_field_alignment != 0)
918 type_align = MIN (type_align, maximum_field_alignment);
919 /* ??? This test is opposite the test in the containing if
920 statement, so this code is unreachable currently. */
921 else if (DECL_PACKED (field))
922 type_align = MIN (type_align, BITS_PER_UNIT);
924 /* A bit field may not span the unit of alignment of its type.
925 Advance to next boundary if necessary. */
926 /* ??? This code should match the code above for the
927 PCC_BITFIELD_TYPE_MATTERS case. */
928 if ((offset * BITS_PER_UNIT + bit_offset) / type_align
929 != ((offset * BITS_PER_UNIT + bit_offset + field_size - 1)
931 rli->bitpos = round_up (rli->bitpos, type_align);
935 /* Offset so far becomes the position of this field after normalizing. */
937 DECL_FIELD_OFFSET (field) = rli->offset;
938 DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
939 SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
941 TYPE_USER_ALIGN (rli->t) |= user_align;
943 /* If this field ended up more aligned than we thought it would be (we
944 approximate this by seeing if its position changed), lay out the field
945 again; perhaps we can use an integral mode for it now. */
946 if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field)))
947 actual_align = (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
948 & - tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1));
949 else if (integer_zerop (DECL_FIELD_OFFSET (field)))
950 actual_align = BIGGEST_ALIGNMENT;
951 else if (host_integerp (DECL_FIELD_OFFSET (field), 1))
952 actual_align = (BITS_PER_UNIT
953 * (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
954 & - tree_low_cst (DECL_FIELD_OFFSET (field), 1)));
956 actual_align = DECL_OFFSET_ALIGN (field);
958 if (known_align != actual_align)
959 layout_decl (field, actual_align);
961 /* Now add size of this field to the size of the record. If the size is
962 not constant, treat the field as being a multiple of bytes and just
963 adjust the offset, resetting the bit position. Otherwise, apportion the
964 size amongst the bit position and offset. First handle the case of an
965 unspecified size, which can happen when we have an invalid nested struct
966 definition, such as struct j { struct j { int i; } }. The error message
967 is printed in finish_struct. */
968 if (DECL_SIZE (field) == 0)
970 else if (TREE_CODE (DECL_SIZE_UNIT (field)) != INTEGER_CST
971 || TREE_CONSTANT_OVERFLOW (DECL_SIZE_UNIT (field)))
974 = size_binop (PLUS_EXPR, rli->offset,
976 size_binop (CEIL_DIV_EXPR, rli->bitpos,
977 bitsize_unit_node)));
979 = size_binop (PLUS_EXPR, rli->offset, DECL_SIZE_UNIT (field));
980 rli->bitpos = bitsize_zero_node;
981 rli->offset_align = MIN (rli->offset_align, DECL_ALIGN (field));
985 rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
990 /* Assuming that all the fields have been laid out, this function uses
991 RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
992 inidicated by RLI. */
995 finalize_record_size (rli)
996 record_layout_info rli;
998 tree unpadded_size, unpadded_size_unit;
1000 /* Now we want just byte and bit offsets, so set the offset alignment
1001 to be a byte and then normalize. */
1002 rli->offset_align = BITS_PER_UNIT;
1003 normalize_rli (rli);
1005 /* Determine the desired alignment. */
1006 #ifdef ROUND_TYPE_ALIGN
1007 TYPE_ALIGN (rli->t) = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t),
1010 TYPE_ALIGN (rli->t) = MAX (TYPE_ALIGN (rli->t), rli->record_align);
1013 /* Compute the size so far. Be sure to allow for extra bits in the
1014 size in bytes. We have guaranteed above that it will be no more
1015 than a single byte. */
1016 unpadded_size = rli_size_so_far (rli);
1017 unpadded_size_unit = rli_size_unit_so_far (rli);
1018 if (! integer_zerop (rli->bitpos))
1020 = size_binop (PLUS_EXPR, unpadded_size_unit, size_one_node);
1022 /* Record the un-rounded size in the binfo node. But first we check
1023 the size of TYPE_BINFO to make sure that BINFO_SIZE is available. */
1024 if (TYPE_BINFO (rli->t) && TREE_VEC_LENGTH (TYPE_BINFO (rli->t)) > 6)
1026 TYPE_BINFO_SIZE (rli->t) = unpadded_size;
1027 TYPE_BINFO_SIZE_UNIT (rli->t) = unpadded_size_unit;
1030 /* Round the size up to be a multiple of the required alignment */
1031 #ifdef ROUND_TYPE_SIZE
1032 TYPE_SIZE (rli->t) = ROUND_TYPE_SIZE (rli->t, unpadded_size,
1033 TYPE_ALIGN (rli->t));
1034 TYPE_SIZE_UNIT (rli->t)
1035 = ROUND_TYPE_SIZE_UNIT (rli->t, unpadded_size_unit,
1036 TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
1038 TYPE_SIZE (rli->t) = round_up (unpadded_size, TYPE_ALIGN (rli->t));
1039 TYPE_SIZE_UNIT (rli->t) = round_up (unpadded_size_unit,
1040 TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
1043 if (warn_padded && TREE_CONSTANT (unpadded_size)
1044 && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0)
1045 warning ("padding struct size to alignment boundary");
1047 if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
1048 && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
1049 && TREE_CONSTANT (unpadded_size))
1053 #ifdef ROUND_TYPE_ALIGN
1055 = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t), rli->unpacked_align);
1057 rli->unpacked_align = MAX (TYPE_ALIGN (rli->t), rli->unpacked_align);
1060 #ifdef ROUND_TYPE_SIZE
1061 unpacked_size = ROUND_TYPE_SIZE (rli->t, TYPE_SIZE (rli->t),
1062 rli->unpacked_align);
1064 unpacked_size = round_up (TYPE_SIZE (rli->t), rli->unpacked_align);
1067 if (simple_cst_equal (unpacked_size, TYPE_SIZE (rli->t)))
1069 TYPE_PACKED (rli->t) = 0;
1071 if (TYPE_NAME (rli->t))
1075 if (TREE_CODE (TYPE_NAME (rli->t)) == IDENTIFIER_NODE)
1076 name = IDENTIFIER_POINTER (TYPE_NAME (rli->t));
1078 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (rli->t)));
1080 if (STRICT_ALIGNMENT)
1081 warning ("packed attribute causes inefficient alignment for `%s'", name);
1083 warning ("packed attribute is unnecessary for `%s'", name);
1087 if (STRICT_ALIGNMENT)
1088 warning ("packed attribute causes inefficient alignment");
1090 warning ("packed attribute is unnecessary");
1096 /* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE). */
1099 compute_record_mode (type)
1103 enum machine_mode mode = VOIDmode;
1105 /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
1106 However, if possible, we use a mode that fits in a register
1107 instead, in order to allow for better optimization down the
1109 TYPE_MODE (type) = BLKmode;
1111 if (! host_integerp (TYPE_SIZE (type), 1))
1114 /* A record which has any BLKmode members must itself be
1115 BLKmode; it can't go in a register. Unless the member is
1116 BLKmode only because it isn't aligned. */
1117 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1119 unsigned HOST_WIDE_INT bitpos;
1121 if (TREE_CODE (field) != FIELD_DECL)
1124 if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK
1125 || (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1126 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1127 || ! host_integerp (bit_position (field), 1)
1128 || DECL_SIZE (field) == 0
1129 || ! host_integerp (DECL_SIZE (field), 1))
1132 bitpos = int_bit_position (field);
1134 /* Must be BLKmode if any field crosses a word boundary,
1135 since extract_bit_field can't handle that in registers. */
1136 if (bitpos / BITS_PER_WORD
1137 != ((tree_low_cst (DECL_SIZE (field), 1) + bitpos - 1)
1139 /* But there is no problem if the field is entire words. */
1140 && tree_low_cst (DECL_SIZE (field), 1) % BITS_PER_WORD != 0)
1143 /* If this field is the whole struct, remember its mode so
1144 that, say, we can put a double in a class into a DF
1145 register instead of forcing it to live in the stack. */
1146 if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
1147 mode = DECL_MODE (field);
1149 #ifdef MEMBER_TYPE_FORCES_BLK
1150 /* With some targets, eg. c4x, it is sub-optimal
1151 to access an aligned BLKmode structure as a scalar. */
1152 if (mode == VOIDmode && MEMBER_TYPE_FORCES_BLK (field))
1154 #endif /* MEMBER_TYPE_FORCES_BLK */
1157 /* If we only have one real field; use its mode. This only applies to
1158 RECORD_TYPE. This does not apply to unions. */
1159 if (TREE_CODE (type) == RECORD_TYPE && mode != VOIDmode)
1160 TYPE_MODE (type) = mode;
1162 TYPE_MODE (type) = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1164 /* If structure's known alignment is less than what the scalar
1165 mode would need, and it matters, then stick with BLKmode. */
1166 if (TYPE_MODE (type) != BLKmode
1168 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1169 || TYPE_ALIGN (type) >= GET_MODE_ALIGNMENT (TYPE_MODE (type))))
1171 /* If this is the only reason this type is BLKmode, then
1172 don't force containing types to be BLKmode. */
1173 TYPE_NO_FORCE_BLK (type) = 1;
1174 TYPE_MODE (type) = BLKmode;
1178 /* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
1182 finalize_type_size (type)
1185 /* Normally, use the alignment corresponding to the mode chosen.
1186 However, where strict alignment is not required, avoid
1187 over-aligning structures, since most compilers do not do this
1190 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1191 && (STRICT_ALIGNMENT
1192 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1193 && TREE_CODE (type) != QUAL_UNION_TYPE
1194 && TREE_CODE (type) != ARRAY_TYPE)))
1196 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1197 TYPE_USER_ALIGN (type) = 0;
1200 /* Do machine-dependent extra alignment. */
1201 #ifdef ROUND_TYPE_ALIGN
1203 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1206 /* If we failed to find a simple way to calculate the unit size
1207 of the type, find it by division. */
1208 if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1209 /* TYPE_SIZE (type) is computed in bitsizetype. After the division, the
1210 result will fit in sizetype. We will get more efficient code using
1211 sizetype, so we force a conversion. */
1212 TYPE_SIZE_UNIT (type)
1213 = convert (sizetype,
1214 size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1215 bitsize_unit_node));
1217 if (TYPE_SIZE (type) != 0)
1219 #ifdef ROUND_TYPE_SIZE
1221 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1222 TYPE_SIZE_UNIT (type)
1223 = ROUND_TYPE_SIZE_UNIT (type, TYPE_SIZE_UNIT (type),
1224 TYPE_ALIGN (type) / BITS_PER_UNIT);
1226 TYPE_SIZE (type) = round_up (TYPE_SIZE (type), TYPE_ALIGN (type));
1227 TYPE_SIZE_UNIT (type)
1228 = round_up (TYPE_SIZE_UNIT (type), TYPE_ALIGN (type) / BITS_PER_UNIT);
1232 /* Evaluate nonconstant sizes only once, either now or as soon as safe. */
1233 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1234 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1235 if (TYPE_SIZE_UNIT (type) != 0
1236 && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1237 TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1239 /* Also layout any other variants of the type. */
1240 if (TYPE_NEXT_VARIANT (type)
1241 || type != TYPE_MAIN_VARIANT (type))
1244 /* Record layout info of this variant. */
1245 tree size = TYPE_SIZE (type);
1246 tree size_unit = TYPE_SIZE_UNIT (type);
1247 unsigned int align = TYPE_ALIGN (type);
1248 unsigned int user_align = TYPE_USER_ALIGN (type);
1249 enum machine_mode mode = TYPE_MODE (type);
1251 /* Copy it into all variants. */
1252 for (variant = TYPE_MAIN_VARIANT (type);
1254 variant = TYPE_NEXT_VARIANT (variant))
1256 TYPE_SIZE (variant) = size;
1257 TYPE_SIZE_UNIT (variant) = size_unit;
1258 TYPE_ALIGN (variant) = align;
1259 TYPE_USER_ALIGN (variant) = user_align;
1260 TYPE_MODE (variant) = mode;
1265 /* Do all of the work required to layout the type indicated by RLI,
1266 once the fields have been laid out. This function will call `free'
1270 finish_record_layout (rli)
1271 record_layout_info rli;
1273 /* Compute the final size. */
1274 finalize_record_size (rli);
1276 /* Compute the TYPE_MODE for the record. */
1277 compute_record_mode (rli->t);
1279 /* Perform any last tweaks to the TYPE_SIZE, etc. */
1280 finalize_type_size (rli->t);
1282 /* Lay out any static members. This is done now because their type
1283 may use the record's type. */
1284 while (rli->pending_statics)
1286 layout_decl (TREE_VALUE (rli->pending_statics), 0);
1287 rli->pending_statics = TREE_CHAIN (rli->pending_statics);
1294 /* Calculate the mode, size, and alignment for TYPE.
1295 For an array type, calculate the element separation as well.
1296 Record TYPE on the chain of permanent or temporary types
1297 so that dbxout will find out about it.
1299 TYPE_SIZE of a type is nonzero if the type has been laid out already.
1300 layout_type does nothing on such a type.
1302 If the type is incomplete, its TYPE_SIZE remains zero. */
1311 /* Do nothing if type has been laid out before. */
1312 if (TYPE_SIZE (type))
1315 switch (TREE_CODE (type))
1318 /* This kind of type is the responsibility
1319 of the language-specific code. */
1322 case BOOLEAN_TYPE: /* Used for Java, Pascal, and Chill. */
1323 if (TYPE_PRECISION (type) == 0)
1324 TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
1326 /* ... fall through ... */
1331 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
1332 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
1333 TREE_UNSIGNED (type) = 1;
1335 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
1337 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1338 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1342 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
1343 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1344 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1348 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
1350 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
1351 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
1352 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
1354 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1355 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1362 subtype = TREE_TYPE (type);
1363 TREE_UNSIGNED (type) = TREE_UNSIGNED (subtype);
1364 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1365 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1370 /* This is an incomplete type and so doesn't have a size. */
1371 TYPE_ALIGN (type) = 1;
1372 TYPE_USER_ALIGN (type) = 0;
1373 TYPE_MODE (type) = VOIDmode;
1377 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
1378 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
1379 /* A pointer might be MODE_PARTIAL_INT,
1380 but ptrdiff_t must be integral. */
1381 TYPE_MODE (type) = mode_for_size (POINTER_SIZE, MODE_INT, 0);
1386 TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
1387 TYPE_SIZE (type) = bitsize_int (2 * POINTER_SIZE);
1388 TYPE_SIZE_UNIT (type) = size_int ((2 * POINTER_SIZE) / BITS_PER_UNIT);
1392 case REFERENCE_TYPE:
1394 int nbits = ((TREE_CODE (type) == REFERENCE_TYPE
1395 && reference_types_internal)
1396 ? GET_MODE_BITSIZE (Pmode) : POINTER_SIZE);
1398 TYPE_MODE (type) = nbits == POINTER_SIZE ? ptr_mode : Pmode;
1399 TYPE_SIZE (type) = bitsize_int (nbits);
1400 TYPE_SIZE_UNIT (type) = size_int (nbits / BITS_PER_UNIT);
1401 TREE_UNSIGNED (type) = 1;
1402 TYPE_PRECISION (type) = nbits;
1408 tree index = TYPE_DOMAIN (type);
1409 tree element = TREE_TYPE (type);
1411 build_pointer_type (element);
1413 /* We need to know both bounds in order to compute the size. */
1414 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
1415 && TYPE_SIZE (element))
1417 tree ub = TYPE_MAX_VALUE (index);
1418 tree lb = TYPE_MIN_VALUE (index);
1422 /* The initial subtraction should happen in the original type so
1423 that (possible) negative values are handled appropriately. */
1424 length = size_binop (PLUS_EXPR, size_one_node,
1426 fold (build (MINUS_EXPR,
1430 /* Special handling for arrays of bits (for Chill). */
1431 element_size = TYPE_SIZE (element);
1432 if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element)
1433 && (integer_zerop (TYPE_MAX_VALUE (element))
1434 || integer_onep (TYPE_MAX_VALUE (element)))
1435 && host_integerp (TYPE_MIN_VALUE (element), 1))
1437 HOST_WIDE_INT maxvalue
1438 = tree_low_cst (TYPE_MAX_VALUE (element), 1);
1439 HOST_WIDE_INT minvalue
1440 = tree_low_cst (TYPE_MIN_VALUE (element), 1);
1442 if (maxvalue - minvalue == 1
1443 && (maxvalue == 1 || maxvalue == 0))
1444 element_size = integer_one_node;
1447 TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size,
1448 convert (bitsizetype, length));
1450 /* If we know the size of the element, calculate the total
1451 size directly, rather than do some division thing below.
1452 This optimization helps Fortran assumed-size arrays
1453 (where the size of the array is determined at runtime)
1455 Note that we can't do this in the case where the size of
1456 the elements is one bit since TYPE_SIZE_UNIT cannot be
1457 set correctly in that case. */
1458 if (TYPE_SIZE_UNIT (element) != 0 && ! integer_onep (element_size))
1459 TYPE_SIZE_UNIT (type)
1460 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
1463 /* Now round the alignment and size,
1464 using machine-dependent criteria if any. */
1466 #ifdef ROUND_TYPE_ALIGN
1468 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
1470 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
1472 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (element);
1474 #ifdef ROUND_TYPE_SIZE
1475 if (TYPE_SIZE (type) != 0)
1478 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1480 /* If the rounding changed the size of the type, remove any
1481 pre-calculated TYPE_SIZE_UNIT. */
1482 if (simple_cst_equal (TYPE_SIZE (type), tmp) != 1)
1483 TYPE_SIZE_UNIT (type) = NULL;
1485 TYPE_SIZE (type) = tmp;
1489 TYPE_MODE (type) = BLKmode;
1490 if (TYPE_SIZE (type) != 0
1491 #ifdef MEMBER_TYPE_FORCES_BLK
1492 && ! MEMBER_TYPE_FORCES_BLK (type)
1494 /* BLKmode elements force BLKmode aggregate;
1495 else extract/store fields may lose. */
1496 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
1497 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
1500 = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1502 if (TYPE_MODE (type) != BLKmode
1503 && STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
1504 && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (TYPE_MODE (type))
1505 && TYPE_MODE (type) != BLKmode)
1507 TYPE_NO_FORCE_BLK (type) = 1;
1508 TYPE_MODE (type) = BLKmode;
1516 case QUAL_UNION_TYPE:
1519 record_layout_info rli;
1521 /* Initialize the layout information. */
1522 rli = start_record_layout (type);
1524 /* If this is a QUAL_UNION_TYPE, we want to process the fields
1525 in the reverse order in building the COND_EXPR that denotes
1526 its size. We reverse them again later. */
1527 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1528 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1530 /* Place all the fields. */
1531 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1532 place_field (rli, field);
1534 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1535 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1537 if (lang_adjust_rli)
1538 (*lang_adjust_rli) (rli);
1540 /* Finish laying out the record. */
1541 finish_record_layout (rli);
1545 case SET_TYPE: /* Used by Chill and Pascal. */
1546 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1547 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1551 #ifndef SET_WORD_SIZE
1552 #define SET_WORD_SIZE BITS_PER_WORD
1554 unsigned int alignment
1555 = set_alignment ? set_alignment : SET_WORD_SIZE;
1557 = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1558 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
1560 = ((size_in_bits + alignment - 1) / alignment) * alignment;
1562 if (rounded_size > (int) alignment)
1563 TYPE_MODE (type) = BLKmode;
1565 TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1567 TYPE_SIZE (type) = bitsize_int (rounded_size);
1568 TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1569 TYPE_ALIGN (type) = alignment;
1570 TYPE_USER_ALIGN (type) = 0;
1571 TYPE_PRECISION (type) = size_in_bits;
1576 /* The size may vary in different languages, so the language front end
1577 should fill in the size. */
1578 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1579 TYPE_USER_ALIGN (type) = 0;
1580 TYPE_MODE (type) = BLKmode;
1587 /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE. For
1588 records and unions, finish_record_layout already called this
1590 if (TREE_CODE (type) != RECORD_TYPE
1591 && TREE_CODE (type) != UNION_TYPE
1592 && TREE_CODE (type) != QUAL_UNION_TYPE)
1593 finalize_type_size (type);
1595 /* If this type is created before sizetype has been permanently set,
1596 record it so set_sizetype can fix it up. */
1598 early_type_list = tree_cons (NULL_TREE, type, early_type_list);
1600 /* If an alias set has been set for this aggregate when it was incomplete,
1601 force it into alias set 0.
1602 This is too conservative, but we cannot call record_component_aliases
1603 here because some frontends still change the aggregates after
1605 if (AGGREGATE_TYPE_P (type) && TYPE_ALIAS_SET_KNOWN_P (type))
1606 TYPE_ALIAS_SET (type) = 0;
1609 /* Create and return a type for signed integers of PRECISION bits. */
1612 make_signed_type (precision)
1615 tree type = make_node (INTEGER_TYPE);
1617 TYPE_PRECISION (type) = precision;
1619 fixup_signed_type (type);
1623 /* Create and return a type for unsigned integers of PRECISION bits. */
1626 make_unsigned_type (precision)
1629 tree type = make_node (INTEGER_TYPE);
1631 TYPE_PRECISION (type) = precision;
1633 fixup_unsigned_type (type);
1637 /* Initialize sizetype and bitsizetype to a reasonable and temporary
1638 value to enable integer types to be created. */
1641 initialize_sizetypes ()
1643 tree t = make_node (INTEGER_TYPE);
1645 /* Set this so we do something reasonable for the build_int_2 calls
1647 integer_type_node = t;
1649 TYPE_MODE (t) = SImode;
1650 TYPE_ALIGN (t) = GET_MODE_ALIGNMENT (SImode);
1651 TYPE_USER_ALIGN (t) = 0;
1652 TYPE_SIZE (t) = build_int_2 (GET_MODE_BITSIZE (SImode), 0);
1653 TYPE_SIZE_UNIT (t) = build_int_2 (GET_MODE_SIZE (SImode), 0);
1654 TREE_UNSIGNED (t) = 1;
1655 TYPE_PRECISION (t) = GET_MODE_BITSIZE (SImode);
1656 TYPE_MIN_VALUE (t) = build_int_2 (0, 0);
1657 TYPE_IS_SIZETYPE (t) = 1;
1659 /* 1000 avoids problems with possible overflow and is certainly
1660 larger than any size value we'd want to be storing. */
1661 TYPE_MAX_VALUE (t) = build_int_2 (1000, 0);
1663 /* These two must be different nodes because of the caching done in
1666 bitsizetype = copy_node (t);
1667 integer_type_node = 0;
1670 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1671 Also update the type of any standard type's sizes made so far. */
1677 int oprecision = TYPE_PRECISION (type);
1678 /* The *bitsizetype types use a precision that avoids overflows when
1679 calculating signed sizes / offsets in bits. However, when
1680 cross-compiling from a 32 bit to a 64 bit host, we are limited to 64 bit
1682 int precision = MIN (oprecision + BITS_PER_UNIT_LOG + 1,
1683 2 * HOST_BITS_PER_WIDE_INT);
1690 /* Make copies of nodes since we'll be setting TYPE_IS_SIZETYPE. */
1691 sizetype = copy_node (type);
1692 TYPE_DOMAIN (sizetype) = type;
1693 TYPE_IS_SIZETYPE (sizetype) = 1;
1694 bitsizetype = make_node (INTEGER_TYPE);
1695 TYPE_NAME (bitsizetype) = TYPE_NAME (type);
1696 TYPE_PRECISION (bitsizetype) = precision;
1697 TYPE_IS_SIZETYPE (bitsizetype) = 1;
1699 if (TREE_UNSIGNED (type))
1700 fixup_unsigned_type (bitsizetype);
1702 fixup_signed_type (bitsizetype);
1704 layout_type (bitsizetype);
1706 if (TREE_UNSIGNED (type))
1708 usizetype = sizetype;
1709 ubitsizetype = bitsizetype;
1710 ssizetype = copy_node (make_signed_type (oprecision));
1711 sbitsizetype = copy_node (make_signed_type (precision));
1715 ssizetype = sizetype;
1716 sbitsizetype = bitsizetype;
1717 usizetype = copy_node (make_unsigned_type (oprecision));
1718 ubitsizetype = copy_node (make_unsigned_type (precision));
1721 TYPE_NAME (bitsizetype) = get_identifier ("bit_size_type");
1723 /* Show is a sizetype, is a main type, and has no pointers to it. */
1724 for (i = 0; i < ARRAY_SIZE (sizetype_tab); i++)
1726 TYPE_IS_SIZETYPE (sizetype_tab[i]) = 1;
1727 TYPE_MAIN_VARIANT (sizetype_tab[i]) = sizetype_tab[i];
1728 TYPE_NEXT_VARIANT (sizetype_tab[i]) = 0;
1729 TYPE_POINTER_TO (sizetype_tab[i]) = 0;
1730 TYPE_REFERENCE_TO (sizetype_tab[i]) = 0;
1733 ggc_add_tree_root ((tree *) &sizetype_tab,
1734 sizeof sizetype_tab / sizeof (tree));
1736 /* Go down each of the types we already made and set the proper type
1737 for the sizes in them. */
1738 for (t = early_type_list; t != 0; t = TREE_CHAIN (t))
1740 if (TREE_CODE (TREE_VALUE (t)) != INTEGER_TYPE)
1743 TREE_TYPE (TYPE_SIZE (TREE_VALUE (t))) = bitsizetype;
1744 TREE_TYPE (TYPE_SIZE_UNIT (TREE_VALUE (t))) = sizetype;
1747 early_type_list = 0;
1751 /* Set the extreme values of TYPE based on its precision in bits,
1752 then lay it out. Used when make_signed_type won't do
1753 because the tree code is not INTEGER_TYPE.
1754 E.g. for Pascal, when the -fsigned-char option is given. */
1757 fixup_signed_type (type)
1760 int precision = TYPE_PRECISION (type);
1762 /* We can not represent properly constants greater then
1763 2 * HOST_BITS_PER_WIDE_INT, still we need the types
1764 as they are used by i386 vector extensions and friends. */
1765 if (precision > HOST_BITS_PER_WIDE_INT * 2)
1766 precision = HOST_BITS_PER_WIDE_INT * 2;
1768 TYPE_MIN_VALUE (type)
1769 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1770 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1771 (((HOST_WIDE_INT) (-1)
1772 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1773 ? precision - HOST_BITS_PER_WIDE_INT - 1
1775 TYPE_MAX_VALUE (type)
1776 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1777 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1778 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1779 ? (((HOST_WIDE_INT) 1
1780 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1783 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1784 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1786 /* Lay out the type: set its alignment, size, etc. */
1790 /* Set the extreme values of TYPE based on its precision in bits,
1791 then lay it out. This is used both in `make_unsigned_type'
1792 and for enumeral types. */
1795 fixup_unsigned_type (type)
1798 int precision = TYPE_PRECISION (type);
1800 /* We can not represent properly constants greater then
1801 2 * HOST_BITS_PER_WIDE_INT, still we need the types
1802 as they are used by i386 vector extensions and friends. */
1803 if (precision > HOST_BITS_PER_WIDE_INT * 2)
1804 precision = HOST_BITS_PER_WIDE_INT * 2;
1806 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1807 TYPE_MAX_VALUE (type)
1808 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1809 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1810 precision - HOST_BITS_PER_WIDE_INT > 0
1811 ? ((unsigned HOST_WIDE_INT) ~0
1812 >> (HOST_BITS_PER_WIDE_INT
1813 - (precision - HOST_BITS_PER_WIDE_INT)))
1815 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1816 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1818 /* Lay out the type: set its alignment, size, etc. */
1822 /* Find the best machine mode to use when referencing a bit field of length
1823 BITSIZE bits starting at BITPOS.
1825 The underlying object is known to be aligned to a boundary of ALIGN bits.
1826 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1827 larger than LARGEST_MODE (usually SImode).
1829 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1830 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1831 mode meeting these conditions.
1833 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1834 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1835 all the conditions. */
1838 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1839 int bitsize, bitpos;
1841 enum machine_mode largest_mode;
1844 enum machine_mode mode;
1845 unsigned int unit = 0;
1847 /* Find the narrowest integer mode that contains the bit field. */
1848 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1849 mode = GET_MODE_WIDER_MODE (mode))
1851 unit = GET_MODE_BITSIZE (mode);
1852 if ((bitpos % unit) + bitsize <= unit)
1856 if (mode == VOIDmode
1857 /* It is tempting to omit the following line
1858 if STRICT_ALIGNMENT is true.
1859 But that is incorrect, since if the bitfield uses part of 3 bytes
1860 and we use a 4-byte mode, we could get a spurious segv
1861 if the extra 4th byte is past the end of memory.
1862 (Though at least one Unix compiler ignores this problem:
1863 that on the Sequent 386 machine. */
1864 || MIN (unit, BIGGEST_ALIGNMENT) > align
1865 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1868 if (SLOW_BYTE_ACCESS && ! volatilep)
1870 enum machine_mode wide_mode = VOIDmode, tmode;
1872 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1873 tmode = GET_MODE_WIDER_MODE (tmode))
1875 unit = GET_MODE_BITSIZE (tmode);
1876 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1877 && unit <= BITS_PER_WORD
1878 && unit <= MIN (align, BIGGEST_ALIGNMENT)
1879 && (largest_mode == VOIDmode
1880 || unit <= GET_MODE_BITSIZE (largest_mode)))
1884 if (wide_mode != VOIDmode)
1891 /* This function is run once to initialize stor-layout.c. */
1894 init_stor_layout_once ()
1896 ggc_add_tree_root (&pending_sizes, 1);