2000-05-02 Jeff Sturm <jsturm@one-point.com>
[official-gcc.git] / gcc / stor-layout.c
bloba278eb11e54a186168abd7a405443185d41ee6f4
1 /* C-compiler utilities for types and variables storage layout
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1996, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "flags.h"
29 #include "function.h"
30 #include "expr.h"
31 #include "toplev.h"
32 #include "ggc.h"
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
73 by front end. */
75 void
76 internal_reference_types ()
78 reference_types_internal = 1;
81 /* Get a list of all the objects put on the pending sizes list. */
83 tree
84 get_pending_sizes ()
86 tree chain = pending_sizes;
87 tree t;
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;
93 pending_sizes = 0;
94 return chain;
97 /* Return non-zero if EXPR is present on the pending sizes list. */
99 int
100 is_pending_size (expr)
101 tree expr;
103 tree t;
105 for (t = pending_sizes; t; t = TREE_CHAIN (t))
106 if (TREE_VALUE (t) == expr)
107 return 1;
108 return 0;
111 /* Add EXPR to the pending sizes list. */
113 void
114 put_pending_size (expr)
115 tree expr;
117 if (TREE_CODE (expr) == SAVE_EXPR)
118 pending_sizes = tree_cons (NULL_TREE, expr, pending_sizes);
121 /* Put a chain of objects into the pending sizes list, which must be
122 empty. */
124 void
125 put_pending_sizes (chain)
126 tree chain;
128 if (pending_sizes)
129 abort ();
131 pending_sizes = chain;
134 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
135 to serve as the actual size-expression for a type or decl. */
137 tree
138 variable_size (size)
139 tree size;
141 /* If the language-processor is to take responsibility for variable-sized
142 items (e.g., languages which have elaboration procedures like Ada),
143 just return SIZE unchanged. Likewise for self-referential sizes. */
144 if (TREE_CONSTANT (size)
145 || global_bindings_p () < 0 || contains_placeholder_p (size))
146 return size;
148 size = save_expr (size);
150 /* If an array with a variable number of elements is declared, and
151 the elements require destruction, we will emit a cleanup for the
152 array. That cleanup is run both on normal exit from the block
153 and in the exception-handler for the block. Normally, when code
154 is used in both ordinary code and in an exception handler it is
155 `unsaved', i.e., all SAVE_EXPRs are recalculated. However, we do
156 not wish to do that here; the array-size is the same in both
157 places. */
158 if (TREE_CODE (size) == SAVE_EXPR)
159 SAVE_EXPR_PERSISTENT_P (size) = 1;
161 if (global_bindings_p ())
163 if (TREE_CONSTANT (size))
164 error ("type size can't be explicitly evaluated");
165 else
166 error ("variable-size type declared outside of any function");
168 return size_one_node;
171 if (immediate_size_expand)
172 /* NULL_RTX is not defined; neither is the rtx type.
173 Also, we would like to pass const0_rtx here, but don't have it. */
174 expand_expr (size, expand_expr (integer_zero_node, NULL_RTX, VOIDmode, 0),
175 VOIDmode, 0);
176 else if (cfun != 0 && cfun->x_dont_save_pending_sizes_p)
177 /* The front-end doesn't want us to keep a list of the expressions
178 that determine sizes for variable size objects. */
180 else
181 put_pending_size (size);
183 return size;
186 #ifndef MAX_FIXED_MODE_SIZE
187 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
188 #endif
190 /* Return the machine mode to use for a nonscalar of SIZE bits.
191 The mode must be in class CLASS, and have exactly that many bits.
192 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
193 be used. */
195 enum machine_mode
196 mode_for_size (size, class, limit)
197 unsigned int size;
198 enum mode_class class;
199 int limit;
201 register enum machine_mode mode;
203 if (limit && size > MAX_FIXED_MODE_SIZE)
204 return BLKmode;
206 /* Get the first mode which has this size, in the specified class. */
207 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
208 mode = GET_MODE_WIDER_MODE (mode))
209 if (GET_MODE_BITSIZE (mode) == size)
210 return mode;
212 return BLKmode;
215 /* Similar, except passed a tree node. */
217 enum machine_mode
218 mode_for_size_tree (size, class, limit)
219 tree size;
220 enum mode_class class;
221 int limit;
223 if (TREE_CODE (size) != INTEGER_CST
224 /* What we really want to say here is that the size can fit in a
225 host integer, but we know there's no way we'd find a mode for
226 this many bits, so there's no point in doing the precise test. */
227 || compare_tree_int (size, 1000) > 0)
228 return BLKmode;
229 else
230 return mode_for_size (TREE_INT_CST_LOW (size), class, limit);
233 /* Similar, but never return BLKmode; return the narrowest mode that
234 contains at least the requested number of bits. */
236 enum machine_mode
237 smallest_mode_for_size (size, class)
238 unsigned int size;
239 enum mode_class class;
241 register enum machine_mode mode;
243 /* Get the first mode which has at least this size, in the
244 specified class. */
245 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
246 mode = GET_MODE_WIDER_MODE (mode))
247 if (GET_MODE_BITSIZE (mode) >= size)
248 return mode;
250 abort ();
253 /* Find an integer mode of the exact same size, or BLKmode on failure. */
255 enum machine_mode
256 int_mode_for_mode (mode)
257 enum machine_mode mode;
259 switch (GET_MODE_CLASS (mode))
261 case MODE_INT:
262 case MODE_PARTIAL_INT:
263 break;
265 case MODE_COMPLEX_INT:
266 case MODE_COMPLEX_FLOAT:
267 case MODE_FLOAT:
268 case MODE_VECTOR_INT:
269 case MODE_VECTOR_FLOAT:
270 mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
271 break;
273 case MODE_RANDOM:
274 if (mode == BLKmode)
275 break;
277 /* ... fall through ... */
279 case MODE_CC:
280 default:
281 abort ();
284 return mode;
287 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
288 This can only be applied to objects of a sizetype. */
290 tree
291 round_up (value, divisor)
292 tree value;
293 int divisor;
295 tree arg = size_int_type (divisor, TREE_TYPE (value));
297 return size_binop (MULT_EXPR, size_binop (CEIL_DIV_EXPR, value, arg), arg);
300 /* Likewise, but round down. */
302 tree
303 round_down (value, divisor)
304 tree value;
305 int divisor;
307 tree arg = size_int_type (divisor, TREE_TYPE (value));
309 return size_binop (MULT_EXPR, size_binop (FLOOR_DIV_EXPR, value, arg), arg);
312 /* Set the size, mode and alignment of a ..._DECL node.
313 TYPE_DECL does need this for C++.
314 Note that LABEL_DECL and CONST_DECL nodes do not need this,
315 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
316 Don't call layout_decl for them.
318 KNOWN_ALIGN is the amount of alignment we can assume this
319 decl has with no special effort. It is relevant only for FIELD_DECLs
320 and depends on the previous fields.
321 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
322 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
323 the record will be aligned to suit. */
325 void
326 layout_decl (decl, known_align)
327 tree decl;
328 unsigned int known_align;
330 register tree type = TREE_TYPE (decl);
331 register enum tree_code code = TREE_CODE (decl);
333 if (code == CONST_DECL)
334 return;
335 else if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
336 && code != TYPE_DECL && code != FIELD_DECL)
337 abort ();
339 if (type == error_mark_node)
340 type = void_type_node;
342 /* Usually the size and mode come from the data type without change,
343 however, the front-end may set the explicit width of the field, so its
344 size may not be the same as the size of its type. This happens with
345 bitfields, of course (an `int' bitfield may be only 2 bits, say), but it
346 also happens with other fields. For example, the C++ front-end creates
347 zero-sized fields corresponding to empty base classes, and depends on
348 layout_type setting DECL_FIELD_BITPOS correctly for the field. Set the
349 size in bytes from the size in bits. If we have already set the mode,
350 don't set it again since we can be called twice for FIELD_DECLs. */
352 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
353 if (DECL_MODE (decl) == VOIDmode)
354 DECL_MODE (decl) = TYPE_MODE (type);
356 if (DECL_SIZE (decl) == 0)
358 DECL_SIZE (decl) = TYPE_SIZE (type);
359 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
361 else
362 DECL_SIZE_UNIT (decl)
363 = convert (sizetype, size_binop (CEIL_DIV_EXPR, DECL_SIZE (decl),
364 bitsize_unit_node));
366 /* Force alignment required for the data type.
367 But if the decl itself wants greater alignment, don't override that.
368 Likewise, if the decl is packed, don't override it. */
369 if (! (code == FIELD_DECL && DECL_BIT_FIELD (decl))
370 && (DECL_ALIGN (decl) == 0
371 || (! (code == FIELD_DECL && DECL_PACKED (decl))
372 && TYPE_ALIGN (type) > DECL_ALIGN (decl))))
374 DECL_ALIGN (decl) = TYPE_ALIGN (type);
375 DECL_USER_ALIGN (decl) = TYPE_USER_ALIGN (type);
378 /* For fields, set the bit field type and update the alignment. */
379 if (code == FIELD_DECL)
381 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
382 if (maximum_field_alignment != 0)
383 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
384 else if (DECL_PACKED (decl))
386 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
387 DECL_USER_ALIGN (decl) = 0;
391 /* See if we can use an ordinary integer mode for a bit-field.
392 Conditions are: a fixed size that is correct for another mode
393 and occupying a complete byte or bytes on proper boundary. */
394 if (code == FIELD_DECL && DECL_BIT_FIELD (decl)
395 && TYPE_SIZE (type) != 0
396 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
397 && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
399 register enum machine_mode xmode
400 = mode_for_size_tree (DECL_SIZE (decl), MODE_INT, 1);
402 if (xmode != BLKmode && known_align >= GET_MODE_ALIGNMENT (xmode))
404 DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
405 DECL_ALIGN (decl));
406 DECL_MODE (decl) = xmode;
407 DECL_BIT_FIELD (decl) = 0;
411 /* Turn off DECL_BIT_FIELD if we won't need it set. */
412 if (code == FIELD_DECL && DECL_BIT_FIELD (decl)
413 && TYPE_MODE (type) == BLKmode && DECL_MODE (decl) == BLKmode
414 && known_align >= TYPE_ALIGN (type)
415 && DECL_ALIGN (decl) >= TYPE_ALIGN (type)
416 && DECL_SIZE_UNIT (decl) != 0)
417 DECL_BIT_FIELD (decl) = 0;
419 /* Evaluate nonconstant size only once, either now or as soon as safe. */
420 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
421 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
422 if (DECL_SIZE_UNIT (decl) != 0
423 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST)
424 DECL_SIZE_UNIT (decl) = variable_size (DECL_SIZE_UNIT (decl));
426 /* If requested, warn about definitions of large data objects. */
427 if (warn_larger_than
428 && (code == VAR_DECL || code == PARM_DECL)
429 && ! DECL_EXTERNAL (decl))
431 tree size = DECL_SIZE_UNIT (decl);
433 if (size != 0 && TREE_CODE (size) == INTEGER_CST
434 && compare_tree_int (size, larger_than_size) > 0)
436 unsigned int size_as_int = TREE_INT_CST_LOW (size);
438 if (compare_tree_int (size, size_as_int) == 0)
439 warning_with_decl (decl, "size of `%s' is %d bytes", size_as_int);
440 else
441 warning_with_decl (decl, "size of `%s' is larger than %d bytes",
442 larger_than_size);
447 /* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
448 QUAL_UNION_TYPE. Return a pointer to a struct record_layout_info which
449 is to be passed to all other layout functions for this record. It is the
450 responsibility of the caller to call `free' for the storage returned.
451 Note that garbage collection is not permitted until we finish laying
452 out the record. */
454 record_layout_info
455 start_record_layout (t)
456 tree t;
458 record_layout_info rli
459 = (record_layout_info) xmalloc (sizeof (struct record_layout_info_s));
461 rli->t = t;
463 /* If the type has a minimum specified alignment (via an attribute
464 declaration, for example) use it -- otherwise, start with a
465 one-byte alignment. */
466 rli->record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (t));
467 rli->unpacked_align = rli->record_align;
468 rli->offset_align = MAX (rli->record_align, BIGGEST_ALIGNMENT);
470 #ifdef STRUCTURE_SIZE_BOUNDARY
471 /* Packed structures don't need to have minimum size. */
472 if (! TYPE_PACKED (t))
473 rli->record_align = MAX (rli->record_align, STRUCTURE_SIZE_BOUNDARY);
474 #endif
476 rli->offset = size_zero_node;
477 rli->bitpos = bitsize_zero_node;
478 rli->pending_statics = 0;
479 rli->packed_maybe_necessary = 0;
481 return rli;
484 /* These four routines perform computations that convert between
485 the offset/bitpos forms and byte and bit offsets. */
487 tree
488 bit_from_pos (offset, bitpos)
489 tree offset, bitpos;
491 return size_binop (PLUS_EXPR, bitpos,
492 size_binop (MULT_EXPR, convert (bitsizetype, offset),
493 bitsize_unit_node));
496 tree
497 byte_from_pos (offset, bitpos)
498 tree offset, bitpos;
500 return size_binop (PLUS_EXPR, offset,
501 convert (sizetype,
502 size_binop (TRUNC_DIV_EXPR, bitpos,
503 bitsize_unit_node)));
506 void
507 pos_from_byte (poffset, pbitpos, off_align, pos)
508 tree *poffset, *pbitpos;
509 unsigned int off_align;
510 tree pos;
512 *poffset
513 = size_binop (MULT_EXPR,
514 convert (sizetype,
515 size_binop (FLOOR_DIV_EXPR, pos,
516 bitsize_int (off_align
517 / BITS_PER_UNIT))),
518 size_int (off_align / BITS_PER_UNIT));
519 *pbitpos = size_binop (MULT_EXPR,
520 size_binop (FLOOR_MOD_EXPR, pos,
521 bitsize_int (off_align / BITS_PER_UNIT)),
522 bitsize_unit_node);
525 void
526 pos_from_bit (poffset, pbitpos, off_align, pos)
527 tree *poffset, *pbitpos;
528 unsigned int off_align;
529 tree pos;
531 *poffset = size_binop (MULT_EXPR,
532 convert (sizetype,
533 size_binop (FLOOR_DIV_EXPR, pos,
534 bitsize_int (off_align))),
535 size_int (off_align / BITS_PER_UNIT));
536 *pbitpos = size_binop (FLOOR_MOD_EXPR, pos, bitsize_int (off_align));
539 /* Given a pointer to bit and byte offsets and an offset alignment,
540 normalize the offsets so they are within the alignment. */
542 void
543 normalize_offset (poffset, pbitpos, off_align)
544 tree *poffset, *pbitpos;
545 unsigned int off_align;
547 /* If the bit position is now larger than it should be, adjust it
548 downwards. */
549 if (compare_tree_int (*pbitpos, off_align) >= 0)
551 tree extra_aligns = size_binop (FLOOR_DIV_EXPR, *pbitpos,
552 bitsize_int (off_align));
554 *poffset
555 = size_binop (PLUS_EXPR, *poffset,
556 size_binop (MULT_EXPR, convert (sizetype, extra_aligns),
557 size_int (off_align / BITS_PER_UNIT)));
559 *pbitpos
560 = size_binop (FLOOR_MOD_EXPR, *pbitpos, bitsize_int (off_align));
564 /* Print debugging information about the information in RLI. */
566 void
567 debug_rli (rli)
568 record_layout_info rli;
570 print_node_brief (stderr, "type", rli->t, 0);
571 print_node_brief (stderr, "\noffset", rli->offset, 0);
572 print_node_brief (stderr, " bitpos", rli->bitpos, 0);
574 fprintf (stderr, "\nrec_align = %u, unpack_align = %u, off_align = %u\n",
575 rli->record_align, rli->unpacked_align, rli->offset_align);
576 if (rli->packed_maybe_necessary)
577 fprintf (stderr, "packed may be necessary\n");
579 if (rli->pending_statics)
581 fprintf (stderr, "pending statics:\n");
582 debug_tree (rli->pending_statics);
586 /* Given an RLI with a possibly-incremented BITPOS, adjust OFFSET and
587 BITPOS if necessary to keep BITPOS below OFFSET_ALIGN. */
589 void
590 normalize_rli (rli)
591 record_layout_info rli;
593 normalize_offset (&rli->offset, &rli->bitpos, rli->offset_align);
596 /* Returns the size in bytes allocated so far. */
598 tree
599 rli_size_unit_so_far (rli)
600 record_layout_info rli;
602 return byte_from_pos (rli->offset, rli->bitpos);
605 /* Returns the size in bits allocated so far. */
607 tree
608 rli_size_so_far (rli)
609 record_layout_info rli;
611 return bit_from_pos (rli->offset, rli->bitpos);
614 /* Called from place_field to handle unions. */
616 static void
617 place_union_field (rli, field)
618 record_layout_info rli;
619 tree field;
621 unsigned int desired_align;
623 layout_decl (field, 0);
625 DECL_FIELD_OFFSET (field) = size_zero_node;
626 DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
627 SET_DECL_OFFSET_ALIGN (field, BIGGEST_ALIGNMENT);
629 desired_align = DECL_ALIGN (field);
631 #ifdef BIGGEST_FIELD_ALIGNMENT
632 /* Some targets (i.e. i386) limit union field alignment
633 to a lower boundary than alignment of variables unless
634 it was overridden by attribute aligned. */
635 if (! DECL_USER_ALIGN (field))
636 desired_align =
637 MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
638 #endif
640 /* Union must be at least as aligned as any field requires. */
641 rli->record_align = MAX (rli->record_align, desired_align);
643 #ifdef PCC_BITFIELD_TYPE_MATTERS
644 /* On the m88000, a bit field of declare type `int' forces the
645 entire union to have `int' alignment. */
646 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
647 rli->record_align = MAX (rli->record_align,
648 TYPE_ALIGN (TREE_TYPE (field)));
649 #endif
651 /* We assume the union's size will be a multiple of a byte so we don't
652 bother with BITPOS. */
653 if (TREE_CODE (rli->t) == UNION_TYPE)
654 rli->offset = size_binop (MAX_EXPR, rli->offset, DECL_SIZE_UNIT (field));
655 else if (TREE_CODE (rli->t) == QUAL_UNION_TYPE)
656 rli->offset = fold (build (COND_EXPR, sizetype,
657 DECL_QUALIFIER (field),
658 DECL_SIZE_UNIT (field), rli->offset));
661 /* RLI contains information about the layout of a RECORD_TYPE. FIELD
662 is a FIELD_DECL to be added after those fields already present in
663 T. (FIELD is not actually added to the TYPE_FIELDS list here;
664 callers that desire that behavior must manually perform that step.) */
666 void
667 place_field (rli, field)
668 record_layout_info rli;
669 tree field;
671 /* The alignment required for FIELD. */
672 unsigned int desired_align;
673 /* The alignment FIELD would have if we just dropped it into the
674 record as it presently stands. */
675 unsigned int known_align;
676 unsigned int actual_align;
677 unsigned int user_align;
678 /* The type of this field. */
679 tree type = TREE_TYPE (field);
681 if (TREE_CODE (field) == ERROR_MARK || TREE_CODE (type) == ERROR_MARK)
682 return;
684 /* If FIELD is static, then treat it like a separate variable, not
685 really like a structure field. If it is a FUNCTION_DECL, it's a
686 method. In both cases, all we do is lay out the decl, and we do
687 it *after* the record is laid out. */
688 if (TREE_CODE (field) == VAR_DECL)
690 rli->pending_statics = tree_cons (NULL_TREE, field,
691 rli->pending_statics);
692 return;
695 /* Enumerators and enum types which are local to this class need not
696 be laid out. Likewise for initialized constant fields. */
697 else if (TREE_CODE (field) != FIELD_DECL)
698 return;
700 /* Unions are laid out very differently than records, so split
701 that code off to another function. */
702 else if (TREE_CODE (rli->t) != RECORD_TYPE)
704 place_union_field (rli, field);
705 return;
708 /* Work out the known alignment so far. Note that A & (-A) is the
709 value of the least-significant bit in A that is one. */
710 if (! integer_zerop (rli->bitpos))
711 known_align = (tree_low_cst (rli->bitpos, 1)
712 & - tree_low_cst (rli->bitpos, 1));
713 else if (integer_zerop (rli->offset))
714 known_align = BIGGEST_ALIGNMENT;
715 else if (host_integerp (rli->offset, 1))
716 known_align = (BITS_PER_UNIT
717 * (tree_low_cst (rli->offset, 1)
718 & - tree_low_cst (rli->offset, 1)));
719 else
720 known_align = rli->offset_align;
722 /* Lay out the field so we know what alignment it needs. For a
723 packed field, use the alignment as specified, disregarding what
724 the type would want. */
725 desired_align = DECL_ALIGN (field);
726 user_align = DECL_USER_ALIGN (field);
727 layout_decl (field, known_align);
728 if (! DECL_PACKED (field))
730 desired_align = DECL_ALIGN (field);
731 user_align = DECL_USER_ALIGN (field);
734 /* Some targets (i.e. i386, VMS) limit struct field alignment
735 to a lower boundary than alignment of variables unless
736 it was overridden by attribute aligned. */
737 #ifdef BIGGEST_FIELD_ALIGNMENT
738 if (! user_align)
739 desired_align
740 = MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
741 #endif
743 #ifdef ADJUST_FIELD_ALIGN
744 desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
745 #endif
747 /* Record must have at least as much alignment as any field.
748 Otherwise, the alignment of the field within the record is
749 meaningless. */
750 #ifdef PCC_BITFIELD_TYPE_MATTERS
751 if (PCC_BITFIELD_TYPE_MATTERS && type != error_mark_node
752 && DECL_BIT_FIELD_TYPE (field)
753 && ! integer_zerop (TYPE_SIZE (type)))
755 /* For these machines, a zero-length field does not
756 affect the alignment of the structure as a whole.
757 It does, however, affect the alignment of the next field
758 within the structure. */
759 if (! integer_zerop (DECL_SIZE (field)))
760 rli->record_align = MAX (rli->record_align, desired_align);
761 else if (! DECL_PACKED (field))
762 desired_align = TYPE_ALIGN (type);
764 /* A named bit field of declared type `int'
765 forces the entire structure to have `int' alignment. */
766 if (DECL_NAME (field) != 0)
768 unsigned int type_align = TYPE_ALIGN (type);
770 if (maximum_field_alignment != 0)
771 type_align = MIN (type_align, maximum_field_alignment);
772 else if (DECL_PACKED (field))
773 type_align = MIN (type_align, BITS_PER_UNIT);
775 rli->record_align = MAX (rli->record_align, type_align);
776 if (warn_packed)
777 rli->unpacked_align = MAX (rli->unpacked_align,
778 TYPE_ALIGN (type));
781 else
782 #endif
784 rli->record_align = MAX (rli->record_align, desired_align);
785 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
788 if (warn_packed && DECL_PACKED (field))
790 if (known_align > TYPE_ALIGN (type))
792 if (TYPE_ALIGN (type) > desired_align)
794 if (STRICT_ALIGNMENT)
795 warning_with_decl (field, "packed attribute causes inefficient alignment for `%s'");
796 else
797 warning_with_decl (field, "packed attribute is unnecessary for `%s'");
800 else
801 rli->packed_maybe_necessary = 1;
804 /* Does this field automatically have alignment it needs by virtue
805 of the fields that precede it and the record's own alignment? */
806 if (known_align < desired_align)
808 /* No, we need to skip space before this field.
809 Bump the cumulative size to multiple of field alignment. */
811 if (warn_padded)
812 warning_with_decl (field, "padding struct to align `%s'");
814 /* If the alignment is still within offset_align, just align
815 the bit position. */
816 if (desired_align < rli->offset_align)
817 rli->bitpos = round_up (rli->bitpos, desired_align);
818 else
820 /* First adjust OFFSET by the partial bits, then align. */
821 rli->offset
822 = size_binop (PLUS_EXPR, rli->offset,
823 convert (sizetype,
824 size_binop (CEIL_DIV_EXPR, rli->bitpos,
825 bitsize_unit_node)));
826 rli->bitpos = bitsize_zero_node;
828 rli->offset = round_up (rli->offset, desired_align / BITS_PER_UNIT);
831 if (! TREE_CONSTANT (rli->offset))
832 rli->offset_align = desired_align;
836 /* Handle compatibility with PCC. Note that if the record has any
837 variable-sized fields, we need not worry about compatibility. */
838 #ifdef PCC_BITFIELD_TYPE_MATTERS
839 if (PCC_BITFIELD_TYPE_MATTERS
840 && TREE_CODE (field) == FIELD_DECL
841 && type != error_mark_node
842 && DECL_BIT_FIELD (field)
843 && ! DECL_PACKED (field)
844 && maximum_field_alignment == 0
845 && ! integer_zerop (DECL_SIZE (field))
846 && host_integerp (DECL_SIZE (field), 1)
847 && host_integerp (rli->offset, 1)
848 && host_integerp (TYPE_SIZE (type), 1))
850 unsigned int type_align = TYPE_ALIGN (type);
851 tree dsize = DECL_SIZE (field);
852 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
853 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
854 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
856 /* A bit field may not span more units of alignment of its type
857 than its type itself. Advance to next boundary if necessary. */
858 if ((((offset * BITS_PER_UNIT + bit_offset + field_size +
859 type_align - 1)
860 / type_align)
861 - (offset * BITS_PER_UNIT + bit_offset) / type_align)
862 > tree_low_cst (TYPE_SIZE (type), 1) / type_align)
863 rli->bitpos = round_up (rli->bitpos, type_align);
865 #endif
867 #ifdef BITFIELD_NBYTES_LIMITED
868 if (BITFIELD_NBYTES_LIMITED
869 && TREE_CODE (field) == FIELD_DECL
870 && type != error_mark_node
871 && DECL_BIT_FIELD_TYPE (field)
872 && ! DECL_PACKED (field)
873 && ! integer_zerop (DECL_SIZE (field))
874 && host_integerp (DECL_SIZE (field), 1)
875 && host_integerp (rli->offset, 1)
876 && host_integerp (TYPE_SIZE (type), 1))
878 unsigned int type_align = TYPE_ALIGN (type);
879 tree dsize = DECL_SIZE (field);
880 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
881 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
882 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
884 if (maximum_field_alignment != 0)
885 type_align = MIN (type_align, maximum_field_alignment);
886 /* ??? This test is opposite the test in the containing if
887 statement, so this code is unreachable currently. */
888 else if (DECL_PACKED (field))
889 type_align = MIN (type_align, BITS_PER_UNIT);
891 /* A bit field may not span the unit of alignment of its type.
892 Advance to next boundary if necessary. */
893 /* ??? This code should match the code above for the
894 PCC_BITFIELD_TYPE_MATTERS case. */
895 if ((offset * BITS_PER_UNIT + bit_offset) / type_align
896 != ((offset * BITS_PER_UNIT + bit_offset + field_size - 1)
897 / type_align))
898 rli->bitpos = round_up (rli->bitpos, type_align);
900 #endif
902 /* Offset so far becomes the position of this field after normalizing. */
903 normalize_rli (rli);
904 DECL_FIELD_OFFSET (field) = rli->offset;
905 DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
906 SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
908 /* If this field ended up more aligned than we thought it would be (we
909 approximate this by seeing if its position changed), lay out the field
910 again; perhaps we can use an integral mode for it now. */
911 if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field)))
912 actual_align = (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
913 & - tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1));
914 else if (integer_zerop (DECL_FIELD_OFFSET (field)))
915 actual_align = BIGGEST_ALIGNMENT;
916 else if (host_integerp (DECL_FIELD_OFFSET (field), 1))
917 actual_align = (BITS_PER_UNIT
918 * (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
919 & - tree_low_cst (DECL_FIELD_OFFSET (field), 1)));
920 else
921 actual_align = DECL_OFFSET_ALIGN (field);
923 if (known_align != actual_align)
924 layout_decl (field, actual_align);
926 /* Now add size of this field to the size of the record. If the size is
927 not constant, treat the field as being a multiple of bytes and just
928 adjust the offset, resetting the bit position. Otherwise, apportion the
929 size amongst the bit position and offset. First handle the case of an
930 unspecified size, which can happen when we have an invalid nested struct
931 definition, such as struct j { struct j { int i; } }. The error message
932 is printed in finish_struct. */
933 if (DECL_SIZE (field) == 0)
934 /* Do nothing. */;
935 else if (TREE_CODE (DECL_SIZE_UNIT (field)) != INTEGER_CST
936 || TREE_CONSTANT_OVERFLOW (DECL_SIZE_UNIT (field)))
938 rli->offset
939 = size_binop (PLUS_EXPR, rli->offset,
940 convert (sizetype,
941 size_binop (CEIL_DIV_EXPR, rli->bitpos,
942 bitsize_unit_node)));
943 rli->offset
944 = size_binop (PLUS_EXPR, rli->offset, DECL_SIZE_UNIT (field));
945 rli->bitpos = bitsize_zero_node;
946 rli->offset_align = MIN (rli->offset_align, DECL_ALIGN (field));
948 else
950 rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
951 normalize_rli (rli);
955 /* Assuming that all the fields have been laid out, this function uses
956 RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
957 inidicated by RLI. */
959 static void
960 finalize_record_size (rli)
961 record_layout_info rli;
963 tree unpadded_size, unpadded_size_unit;
965 /* Now we want just byte and bit offsets, so set the offset alignment
966 to be a byte and then normalize. */
967 rli->offset_align = BITS_PER_UNIT;
968 normalize_rli (rli);
970 /* Determine the desired alignment. */
971 #ifdef ROUND_TYPE_ALIGN
972 TYPE_ALIGN (rli->t) = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t),
973 rli->record_align);
974 #else
975 TYPE_ALIGN (rli->t) = MAX (TYPE_ALIGN (rli->t), rli->record_align);
976 #endif
977 TYPE_USER_ALIGN (rli->t) = 1;
979 /* Compute the size so far. Be sure to allow for extra bits in the
980 size in bytes. We have guaranteed above that it will be no more
981 than a single byte. */
982 unpadded_size = rli_size_so_far (rli);
983 unpadded_size_unit = rli_size_unit_so_far (rli);
984 if (! integer_zerop (rli->bitpos))
985 unpadded_size_unit
986 = size_binop (PLUS_EXPR, unpadded_size_unit, size_one_node);
988 /* Record the un-rounded size in the binfo node. But first we check
989 the size of TYPE_BINFO to make sure that BINFO_SIZE is available. */
990 if (TYPE_BINFO (rli->t) && TREE_VEC_LENGTH (TYPE_BINFO (rli->t)) > 6)
992 TYPE_BINFO_SIZE (rli->t) = unpadded_size;
993 TYPE_BINFO_SIZE_UNIT (rli->t) = unpadded_size_unit;
996 /* Round the size up to be a multiple of the required alignment */
997 #ifdef ROUND_TYPE_SIZE
998 TYPE_SIZE (rli->t) = ROUND_TYPE_SIZE (rli->t, unpadded_size,
999 TYPE_ALIGN (rli->t));
1000 TYPE_SIZE_UNIT (rli->t)
1001 = ROUND_TYPE_SIZE_UNIT (rli->t, unpadded_size_unit,
1002 TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
1003 #else
1004 TYPE_SIZE (rli->t) = round_up (unpadded_size, TYPE_ALIGN (rli->t));
1005 TYPE_SIZE_UNIT (rli->t) = round_up (unpadded_size_unit,
1006 TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
1007 #endif
1009 if (warn_padded && TREE_CONSTANT (unpadded_size)
1010 && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0)
1011 warning ("padding struct size to alignment boundary");
1013 if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
1014 && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
1015 && TREE_CONSTANT (unpadded_size))
1017 tree unpacked_size;
1019 #ifdef ROUND_TYPE_ALIGN
1020 rli->unpacked_align
1021 = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t), rli->unpacked_align);
1022 #else
1023 rli->unpacked_align = MAX (TYPE_ALIGN (rli->t), rli->unpacked_align);
1024 #endif
1026 #ifdef ROUND_TYPE_SIZE
1027 unpacked_size = ROUND_TYPE_SIZE (rli->t, TYPE_SIZE (rli->t),
1028 rli->unpacked_align);
1029 #else
1030 unpacked_size = round_up (TYPE_SIZE (rli->t), rli->unpacked_align);
1031 #endif
1033 if (simple_cst_equal (unpacked_size, TYPE_SIZE (rli->t)))
1035 TYPE_PACKED (rli->t) = 0;
1037 if (TYPE_NAME (rli->t))
1039 const char *name;
1041 if (TREE_CODE (TYPE_NAME (rli->t)) == IDENTIFIER_NODE)
1042 name = IDENTIFIER_POINTER (TYPE_NAME (rli->t));
1043 else
1044 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (rli->t)));
1046 if (STRICT_ALIGNMENT)
1047 warning ("packed attribute causes inefficient alignment for `%s'", name);
1048 else
1049 warning ("packed attribute is unnecessary for `%s'", name);
1051 else
1053 if (STRICT_ALIGNMENT)
1054 warning ("packed attribute causes inefficient alignment");
1055 else
1056 warning ("packed attribute is unnecessary");
1062 /* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE). */
1064 void
1065 compute_record_mode (type)
1066 tree type;
1068 tree field;
1069 enum machine_mode mode = VOIDmode;
1071 /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
1072 However, if possible, we use a mode that fits in a register
1073 instead, in order to allow for better optimization down the
1074 line. */
1075 TYPE_MODE (type) = BLKmode;
1077 if (! host_integerp (TYPE_SIZE (type), 1))
1078 return;
1080 /* A record which has any BLKmode members must itself be
1081 BLKmode; it can't go in a register. Unless the member is
1082 BLKmode only because it isn't aligned. */
1083 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1085 unsigned HOST_WIDE_INT bitpos;
1087 if (TREE_CODE (field) != FIELD_DECL)
1088 continue;
1090 if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK
1091 || (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1092 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1093 || ! host_integerp (bit_position (field), 1)
1094 || ! host_integerp (DECL_SIZE (field), 1))
1095 return;
1097 bitpos = int_bit_position (field);
1099 /* Must be BLKmode if any field crosses a word boundary,
1100 since extract_bit_field can't handle that in registers. */
1101 if (bitpos / BITS_PER_WORD
1102 != ((tree_low_cst (DECL_SIZE (field), 1) + bitpos - 1)
1103 / BITS_PER_WORD)
1104 /* But there is no problem if the field is entire words. */
1105 && tree_low_cst (DECL_SIZE (field), 1) % BITS_PER_WORD != 0)
1106 return;
1108 /* If this field is the whole struct, remember its mode so
1109 that, say, we can put a double in a class into a DF
1110 register instead of forcing it to live in the stack. */
1111 if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
1112 mode = DECL_MODE (field);
1114 #ifdef MEMBER_TYPE_FORCES_BLK
1115 /* With some targets, eg. c4x, it is sub-optimal
1116 to access an aligned BLKmode structure as a scalar. */
1117 if (mode == VOIDmode && MEMBER_TYPE_FORCES_BLK (field))
1118 return;
1119 #endif /* MEMBER_TYPE_FORCES_BLK */
1122 /* If we only have one real field; use its mode. This only applies to
1123 RECORD_TYPE. This does not apply to unions. */
1124 if (TREE_CODE (type) == RECORD_TYPE && mode != VOIDmode)
1125 TYPE_MODE (type) = mode;
1126 else
1127 TYPE_MODE (type) = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1129 /* If structure's known alignment is less than what the scalar
1130 mode would need, and it matters, then stick with BLKmode. */
1131 if (TYPE_MODE (type) != BLKmode
1132 && STRICT_ALIGNMENT
1133 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1134 || TYPE_ALIGN (type) >= GET_MODE_ALIGNMENT (TYPE_MODE (type))))
1136 /* If this is the only reason this type is BLKmode, then
1137 don't force containing types to be BLKmode. */
1138 TYPE_NO_FORCE_BLK (type) = 1;
1139 TYPE_MODE (type) = BLKmode;
1143 /* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
1144 out. */
1146 static void
1147 finalize_type_size (type)
1148 tree type;
1150 /* Normally, use the alignment corresponding to the mode chosen.
1151 However, where strict alignment is not required, avoid
1152 over-aligning structures, since most compilers do not do this
1153 alignment. */
1155 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1156 && (STRICT_ALIGNMENT
1157 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1158 && TREE_CODE (type) != QUAL_UNION_TYPE
1159 && TREE_CODE (type) != ARRAY_TYPE)))
1161 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1162 TYPE_USER_ALIGN (type) = 0;
1165 /* Do machine-dependent extra alignment. */
1166 #ifdef ROUND_TYPE_ALIGN
1167 TYPE_ALIGN (type)
1168 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1169 #endif
1171 /* If we failed to find a simple way to calculate the unit size
1172 of the type, find it by division. */
1173 if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1174 /* TYPE_SIZE (type) is computed in bitsizetype. After the division, the
1175 result will fit in sizetype. We will get more efficient code using
1176 sizetype, so we force a conversion. */
1177 TYPE_SIZE_UNIT (type)
1178 = convert (sizetype,
1179 size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1180 bitsize_unit_node));
1182 if (TYPE_SIZE (type) != 0)
1184 #ifdef ROUND_TYPE_SIZE
1185 TYPE_SIZE (type)
1186 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1187 TYPE_SIZE_UNIT (type)
1188 = ROUND_TYPE_SIZE_UNIT (type, TYPE_SIZE_UNIT (type),
1189 TYPE_ALIGN (type) / BITS_PER_UNIT);
1190 #else
1191 TYPE_SIZE (type) = round_up (TYPE_SIZE (type), TYPE_ALIGN (type));
1192 TYPE_SIZE_UNIT (type)
1193 = round_up (TYPE_SIZE_UNIT (type), TYPE_ALIGN (type) / BITS_PER_UNIT);
1194 #endif
1197 /* Evaluate nonconstant sizes only once, either now or as soon as safe. */
1198 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1199 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1200 if (TYPE_SIZE_UNIT (type) != 0
1201 && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1202 TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1204 /* Also layout any other variants of the type. */
1205 if (TYPE_NEXT_VARIANT (type)
1206 || type != TYPE_MAIN_VARIANT (type))
1208 tree variant;
1209 /* Record layout info of this variant. */
1210 tree size = TYPE_SIZE (type);
1211 tree size_unit = TYPE_SIZE_UNIT (type);
1212 unsigned int align = TYPE_ALIGN (type);
1213 unsigned int user_align = TYPE_USER_ALIGN (type);
1214 enum machine_mode mode = TYPE_MODE (type);
1216 /* Copy it into all variants. */
1217 for (variant = TYPE_MAIN_VARIANT (type);
1218 variant != 0;
1219 variant = TYPE_NEXT_VARIANT (variant))
1221 TYPE_SIZE (variant) = size;
1222 TYPE_SIZE_UNIT (variant) = size_unit;
1223 TYPE_ALIGN (variant) = align;
1224 TYPE_USER_ALIGN (variant) = user_align;
1225 TYPE_MODE (variant) = mode;
1230 /* Do all of the work required to layout the type indicated by RLI,
1231 once the fields have been laid out. This function will call `free'
1232 for RLI. */
1234 void
1235 finish_record_layout (rli)
1236 record_layout_info rli;
1238 /* Compute the final size. */
1239 finalize_record_size (rli);
1241 /* Compute the TYPE_MODE for the record. */
1242 compute_record_mode (rli->t);
1244 /* Perform any last tweaks to the TYPE_SIZE, etc. */
1245 finalize_type_size (rli->t);
1247 /* Lay out any static members. This is done now because their type
1248 may use the record's type. */
1249 while (rli->pending_statics)
1251 layout_decl (TREE_VALUE (rli->pending_statics), 0);
1252 rli->pending_statics = TREE_CHAIN (rli->pending_statics);
1255 /* Clean up. */
1256 free (rli);
1259 /* Calculate the mode, size, and alignment for TYPE.
1260 For an array type, calculate the element separation as well.
1261 Record TYPE on the chain of permanent or temporary types
1262 so that dbxout will find out about it.
1264 TYPE_SIZE of a type is nonzero if the type has been laid out already.
1265 layout_type does nothing on such a type.
1267 If the type is incomplete, its TYPE_SIZE remains zero. */
1269 void
1270 layout_type (type)
1271 tree type;
1273 if (type == 0)
1274 abort ();
1276 /* Do nothing if type has been laid out before. */
1277 if (TYPE_SIZE (type))
1278 return;
1280 switch (TREE_CODE (type))
1282 case LANG_TYPE:
1283 /* This kind of type is the responsibility
1284 of the language-specific code. */
1285 abort ();
1287 case BOOLEAN_TYPE: /* Used for Java, Pascal, and Chill. */
1288 if (TYPE_PRECISION (type) == 0)
1289 TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
1291 /* ... fall through ... */
1293 case INTEGER_TYPE:
1294 case ENUMERAL_TYPE:
1295 case CHAR_TYPE:
1296 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
1297 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
1298 TREE_UNSIGNED (type) = 1;
1300 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
1301 MODE_INT);
1302 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1303 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1304 break;
1306 case REAL_TYPE:
1307 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
1308 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1309 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1310 break;
1312 case COMPLEX_TYPE:
1313 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
1314 TYPE_MODE (type)
1315 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
1316 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
1317 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
1319 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1320 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1321 break;
1323 case VECTOR_TYPE:
1325 tree subtype;
1327 subtype = TREE_TYPE (type);
1328 TREE_UNSIGNED (type) = TREE_UNSIGNED (subtype);
1329 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1330 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1332 break;
1334 case VOID_TYPE:
1335 /* This is an incomplete type and so doesn't have a size. */
1336 TYPE_ALIGN (type) = 1;
1337 TYPE_USER_ALIGN (type) = 0;
1338 TYPE_MODE (type) = VOIDmode;
1339 break;
1341 case OFFSET_TYPE:
1342 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
1343 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
1344 /* A pointer might be MODE_PARTIAL_INT,
1345 but ptrdiff_t must be integral. */
1346 TYPE_MODE (type) = mode_for_size (POINTER_SIZE, MODE_INT, 0);
1347 break;
1349 case FUNCTION_TYPE:
1350 case METHOD_TYPE:
1351 TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
1352 TYPE_SIZE (type) = bitsize_int (2 * POINTER_SIZE);
1353 TYPE_SIZE_UNIT (type) = size_int ((2 * POINTER_SIZE) / BITS_PER_UNIT);
1354 break;
1356 case POINTER_TYPE:
1357 case REFERENCE_TYPE:
1359 int nbits = ((TREE_CODE (type) == REFERENCE_TYPE
1360 && reference_types_internal)
1361 ? GET_MODE_BITSIZE (Pmode) : POINTER_SIZE);
1363 TYPE_MODE (type) = nbits == POINTER_SIZE ? ptr_mode : Pmode;
1364 TYPE_SIZE (type) = bitsize_int (nbits);
1365 TYPE_SIZE_UNIT (type) = size_int (nbits / BITS_PER_UNIT);
1366 TREE_UNSIGNED (type) = 1;
1367 TYPE_PRECISION (type) = nbits;
1369 break;
1371 case ARRAY_TYPE:
1373 register tree index = TYPE_DOMAIN (type);
1374 register tree element = TREE_TYPE (type);
1376 build_pointer_type (element);
1378 /* We need to know both bounds in order to compute the size. */
1379 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
1380 && TYPE_SIZE (element))
1382 tree ub = TYPE_MAX_VALUE (index);
1383 tree lb = TYPE_MIN_VALUE (index);
1384 tree length;
1385 tree element_size;
1387 /* The initial subtraction should happen in the original type so
1388 that (possible) negative values are handled appropriately. */
1389 length = size_binop (PLUS_EXPR, size_one_node,
1390 convert (sizetype,
1391 fold (build (MINUS_EXPR,
1392 TREE_TYPE (lb),
1393 ub, lb))));
1395 /* Special handling for arrays of bits (for Chill). */
1396 element_size = TYPE_SIZE (element);
1397 if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element)
1398 && (integer_zerop (TYPE_MAX_VALUE (element))
1399 || integer_onep (TYPE_MAX_VALUE (element)))
1400 && host_integerp (TYPE_MIN_VALUE (element), 1))
1402 HOST_WIDE_INT maxvalue
1403 = tree_low_cst (TYPE_MAX_VALUE (element), 1);
1404 HOST_WIDE_INT minvalue
1405 = tree_low_cst (TYPE_MIN_VALUE (element), 1);
1407 if (maxvalue - minvalue == 1
1408 && (maxvalue == 1 || maxvalue == 0))
1409 element_size = integer_one_node;
1412 TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size,
1413 convert (bitsizetype, length));
1415 /* If we know the size of the element, calculate the total
1416 size directly, rather than do some division thing below.
1417 This optimization helps Fortran assumed-size arrays
1418 (where the size of the array is determined at runtime)
1419 substantially.
1420 Note that we can't do this in the case where the size of
1421 the elements is one bit since TYPE_SIZE_UNIT cannot be
1422 set correctly in that case. */
1423 if (TYPE_SIZE_UNIT (element) != 0 && ! integer_onep (element_size))
1424 TYPE_SIZE_UNIT (type)
1425 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
1428 /* Now round the alignment and size,
1429 using machine-dependent criteria if any. */
1431 #ifdef ROUND_TYPE_ALIGN
1432 TYPE_ALIGN (type)
1433 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
1434 #else
1435 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
1436 #endif
1438 #ifdef ROUND_TYPE_SIZE
1439 if (TYPE_SIZE (type) != 0)
1441 tree tmp
1442 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1444 /* If the rounding changed the size of the type, remove any
1445 pre-calculated TYPE_SIZE_UNIT. */
1446 if (simple_cst_equal (TYPE_SIZE (type), tmp) != 1)
1447 TYPE_SIZE_UNIT (type) = NULL;
1449 TYPE_SIZE (type) = tmp;
1451 #endif
1453 TYPE_MODE (type) = BLKmode;
1454 if (TYPE_SIZE (type) != 0
1455 #ifdef MEMBER_TYPE_FORCES_BLK
1456 && ! MEMBER_TYPE_FORCES_BLK (type)
1457 #endif
1458 /* BLKmode elements force BLKmode aggregate;
1459 else extract/store fields may lose. */
1460 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
1461 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
1463 TYPE_MODE (type)
1464 = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1466 if (TYPE_MODE (type) != BLKmode
1467 && STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
1468 && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (TYPE_MODE (type))
1469 && TYPE_MODE (type) != BLKmode)
1471 TYPE_NO_FORCE_BLK (type) = 1;
1472 TYPE_MODE (type) = BLKmode;
1475 break;
1478 case RECORD_TYPE:
1479 case UNION_TYPE:
1480 case QUAL_UNION_TYPE:
1482 tree field;
1483 record_layout_info rli;
1485 /* Initialize the layout information. */
1486 rli = start_record_layout (type);
1488 /* If this is a QUAL_UNION_TYPE, we want to process the fields
1489 in the reverse order in building the COND_EXPR that denotes
1490 its size. We reverse them again later. */
1491 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1492 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1494 /* Place all the fields. */
1495 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1496 place_field (rli, field);
1498 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1499 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1501 /* Finish laying out the record. */
1502 finish_record_layout (rli);
1504 break;
1506 case SET_TYPE: /* Used by Chill and Pascal. */
1507 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1508 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1509 abort();
1510 else
1512 #ifndef SET_WORD_SIZE
1513 #define SET_WORD_SIZE BITS_PER_WORD
1514 #endif
1515 unsigned int alignment
1516 = set_alignment ? set_alignment : SET_WORD_SIZE;
1517 int size_in_bits
1518 = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1519 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
1520 int rounded_size
1521 = ((size_in_bits + alignment - 1) / alignment) * alignment;
1523 if (rounded_size > (int) alignment)
1524 TYPE_MODE (type) = BLKmode;
1525 else
1526 TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1528 TYPE_SIZE (type) = bitsize_int (rounded_size);
1529 TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1530 TYPE_ALIGN (type) = alignment;
1531 TYPE_USER_ALIGN (type) = 0;
1532 TYPE_PRECISION (type) = size_in_bits;
1534 break;
1536 case FILE_TYPE:
1537 /* The size may vary in different languages, so the language front end
1538 should fill in the size. */
1539 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1540 TYPE_USER_ALIGN (type) = 0;
1541 TYPE_MODE (type) = BLKmode;
1542 break;
1544 default:
1545 abort ();
1548 /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE. For
1549 records and unions, finish_record_layout already called this
1550 function. */
1551 if (TREE_CODE (type) != RECORD_TYPE
1552 && TREE_CODE (type) != UNION_TYPE
1553 && TREE_CODE (type) != QUAL_UNION_TYPE)
1554 finalize_type_size (type);
1556 /* If this type is created before sizetype has been permanently set,
1557 record it so set_sizetype can fix it up. */
1558 if (! sizetype_set)
1559 early_type_list = tree_cons (NULL_TREE, type, early_type_list);
1561 /* If an alias set has been set for this aggregate when it was incomplete,
1562 force it into alias set 0.
1563 This is too conservative, but we cannot call record_component_aliases
1564 here because some frontends still change the aggregates after
1565 layout_type. */
1566 if (AGGREGATE_TYPE_P (type) && TYPE_ALIAS_SET_KNOWN_P (type))
1567 TYPE_ALIAS_SET (type) = 0;
1570 /* Create and return a type for signed integers of PRECISION bits. */
1572 tree
1573 make_signed_type (precision)
1574 int precision;
1576 register tree type = make_node (INTEGER_TYPE);
1578 TYPE_PRECISION (type) = precision;
1580 fixup_signed_type (type);
1581 return type;
1584 /* Create and return a type for unsigned integers of PRECISION bits. */
1586 tree
1587 make_unsigned_type (precision)
1588 int precision;
1590 register tree type = make_node (INTEGER_TYPE);
1592 TYPE_PRECISION (type) = precision;
1594 fixup_unsigned_type (type);
1595 return type;
1598 /* Initialize sizetype and bitsizetype to a reasonable and temporary
1599 value to enable integer types to be created. */
1601 void
1602 initialize_sizetypes ()
1604 tree t = make_node (INTEGER_TYPE);
1606 /* Set this so we do something reasonable for the build_int_2 calls
1607 below. */
1608 integer_type_node = t;
1610 TYPE_MODE (t) = SImode;
1611 TYPE_ALIGN (t) = GET_MODE_ALIGNMENT (SImode);
1612 TYPE_USER_ALIGN (t) = 0;
1613 TYPE_SIZE (t) = build_int_2 (GET_MODE_BITSIZE (SImode), 0);
1614 TYPE_SIZE_UNIT (t) = build_int_2 (GET_MODE_SIZE (SImode), 0);
1615 TREE_UNSIGNED (t) = 1;
1616 TYPE_PRECISION (t) = GET_MODE_BITSIZE (SImode);
1617 TYPE_MIN_VALUE (t) = build_int_2 (0, 0);
1618 TYPE_IS_SIZETYPE (t) = 1;
1620 /* 1000 avoids problems with possible overflow and is certainly
1621 larger than any size value we'd want to be storing. */
1622 TYPE_MAX_VALUE (t) = build_int_2 (1000, 0);
1624 /* These two must be different nodes because of the caching done in
1625 size_int_wide. */
1626 sizetype = t;
1627 bitsizetype = copy_node (t);
1628 integer_type_node = 0;
1631 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1632 Also update the type of any standard type's sizes made so far. */
1634 void
1635 set_sizetype (type)
1636 tree type;
1638 int oprecision = TYPE_PRECISION (type);
1639 /* The *bitsizetype types use a precision that avoids overflows when
1640 calculating signed sizes / offsets in bits. However, when
1641 cross-compiling from a 32 bit to a 64 bit host, we are limited to 64 bit
1642 precision. */
1643 int precision = MIN (oprecision + BITS_PER_UNIT_LOG + 1,
1644 2 * HOST_BITS_PER_WIDE_INT);
1645 unsigned int i;
1646 tree t;
1648 if (sizetype_set)
1649 abort ();
1651 /* Make copies of nodes since we'll be setting TYPE_IS_SIZETYPE. */
1652 sizetype = copy_node (type);
1653 TYPE_DOMAIN (sizetype) = type;
1654 TYPE_IS_SIZETYPE (sizetype) = 1;
1655 bitsizetype = make_node (INTEGER_TYPE);
1656 TYPE_NAME (bitsizetype) = TYPE_NAME (type);
1657 TYPE_PRECISION (bitsizetype) = precision;
1658 TYPE_IS_SIZETYPE (bitsizetype) = 1;
1660 if (TREE_UNSIGNED (type))
1661 fixup_unsigned_type (bitsizetype);
1662 else
1663 fixup_signed_type (bitsizetype);
1665 layout_type (bitsizetype);
1667 if (TREE_UNSIGNED (type))
1669 usizetype = sizetype;
1670 ubitsizetype = bitsizetype;
1671 ssizetype = copy_node (make_signed_type (oprecision));
1672 sbitsizetype = copy_node (make_signed_type (precision));
1674 else
1676 ssizetype = sizetype;
1677 sbitsizetype = bitsizetype;
1678 usizetype = copy_node (make_unsigned_type (oprecision));
1679 ubitsizetype = copy_node (make_unsigned_type (precision));
1682 TYPE_NAME (bitsizetype) = get_identifier ("bit_size_type");
1684 /* Show is a sizetype, is a main type, and has no pointers to it. */
1685 for (i = 0; i < ARRAY_SIZE (sizetype_tab); i++)
1687 TYPE_IS_SIZETYPE (sizetype_tab[i]) = 1;
1688 TYPE_MAIN_VARIANT (sizetype_tab[i]) = sizetype_tab[i];
1689 TYPE_NEXT_VARIANT (sizetype_tab[i]) = 0;
1690 TYPE_POINTER_TO (sizetype_tab[i]) = 0;
1691 TYPE_REFERENCE_TO (sizetype_tab[i]) = 0;
1694 ggc_add_tree_root ((tree *) &sizetype_tab,
1695 sizeof sizetype_tab / sizeof (tree));
1697 /* Go down each of the types we already made and set the proper type
1698 for the sizes in them. */
1699 for (t = early_type_list; t != 0; t = TREE_CHAIN (t))
1701 if (TREE_CODE (TREE_VALUE (t)) != INTEGER_TYPE)
1702 abort ();
1704 TREE_TYPE (TYPE_SIZE (TREE_VALUE (t))) = bitsizetype;
1705 TREE_TYPE (TYPE_SIZE_UNIT (TREE_VALUE (t))) = sizetype;
1708 early_type_list = 0;
1709 sizetype_set = 1;
1712 /* Set the extreme values of TYPE based on its precision in bits,
1713 then lay it out. Used when make_signed_type won't do
1714 because the tree code is not INTEGER_TYPE.
1715 E.g. for Pascal, when the -fsigned-char option is given. */
1717 void
1718 fixup_signed_type (type)
1719 tree type;
1721 register int precision = TYPE_PRECISION (type);
1723 TYPE_MIN_VALUE (type)
1724 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1725 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1726 (((HOST_WIDE_INT) (-1)
1727 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1728 ? precision - HOST_BITS_PER_WIDE_INT - 1
1729 : 0))));
1730 TYPE_MAX_VALUE (type)
1731 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1732 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1733 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1734 ? (((HOST_WIDE_INT) 1
1735 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1736 : 0));
1738 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1739 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1741 /* Lay out the type: set its alignment, size, etc. */
1742 layout_type (type);
1745 /* Set the extreme values of TYPE based on its precision in bits,
1746 then lay it out. This is used both in `make_unsigned_type'
1747 and for enumeral types. */
1749 void
1750 fixup_unsigned_type (type)
1751 tree type;
1753 register int precision = TYPE_PRECISION (type);
1755 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1756 TYPE_MAX_VALUE (type)
1757 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1758 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1759 precision - HOST_BITS_PER_WIDE_INT > 0
1760 ? ((unsigned HOST_WIDE_INT) ~0
1761 >> (HOST_BITS_PER_WIDE_INT
1762 - (precision - HOST_BITS_PER_WIDE_INT)))
1763 : 0);
1764 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1765 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1767 /* Lay out the type: set its alignment, size, etc. */
1768 layout_type (type);
1771 /* Find the best machine mode to use when referencing a bit field of length
1772 BITSIZE bits starting at BITPOS.
1774 The underlying object is known to be aligned to a boundary of ALIGN bits.
1775 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1776 larger than LARGEST_MODE (usually SImode).
1778 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1779 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1780 mode meeting these conditions.
1782 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1783 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1784 all the conditions. */
1786 enum machine_mode
1787 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1788 int bitsize, bitpos;
1789 unsigned int align;
1790 enum machine_mode largest_mode;
1791 int volatilep;
1793 enum machine_mode mode;
1794 unsigned int unit = 0;
1796 /* Find the narrowest integer mode that contains the bit field. */
1797 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1798 mode = GET_MODE_WIDER_MODE (mode))
1800 unit = GET_MODE_BITSIZE (mode);
1801 if ((bitpos % unit) + bitsize <= unit)
1802 break;
1805 if (mode == VOIDmode
1806 /* It is tempting to omit the following line
1807 if STRICT_ALIGNMENT is true.
1808 But that is incorrect, since if the bitfield uses part of 3 bytes
1809 and we use a 4-byte mode, we could get a spurious segv
1810 if the extra 4th byte is past the end of memory.
1811 (Though at least one Unix compiler ignores this problem:
1812 that on the Sequent 386 machine. */
1813 || MIN (unit, BIGGEST_ALIGNMENT) > align
1814 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1815 return VOIDmode;
1817 if (SLOW_BYTE_ACCESS && ! volatilep)
1819 enum machine_mode wide_mode = VOIDmode, tmode;
1821 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1822 tmode = GET_MODE_WIDER_MODE (tmode))
1824 unit = GET_MODE_BITSIZE (tmode);
1825 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1826 && unit <= BITS_PER_WORD
1827 && unit <= MIN (align, BIGGEST_ALIGNMENT)
1828 && (largest_mode == VOIDmode
1829 || unit <= GET_MODE_BITSIZE (largest_mode)))
1830 wide_mode = tmode;
1833 if (wide_mode != VOIDmode)
1834 return wide_mode;
1837 return mode;
1840 /* Return the alignment of MODE. This will be bounded by 1 and
1841 BIGGEST_ALIGNMENT. */
1843 unsigned int
1844 get_mode_alignment (mode)
1845 enum machine_mode mode;
1847 unsigned int alignment = GET_MODE_UNIT_SIZE (mode) * BITS_PER_UNIT;
1849 /* Extract the LSB of the size. */
1850 alignment = alignment & -alignment;
1852 alignment = MIN (BIGGEST_ALIGNMENT, MAX (1, alignment));
1853 return alignment;
1856 /* This function is run once to initialize stor-layout.c. */
1858 void
1859 init_stor_layout_once ()
1861 ggc_add_tree_root (&pending_sizes, 1);