(in m32rx patch): Replace "." with "@." when preceeded by a capital letter
[official-gcc.git] / gcc / stor-layout.c
blob22dd86b709ef14f632b84830f715e72b5e9e8a45
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
10 version.
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
15 for more details.
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
20 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 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 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 tree type = TREE_TYPE (decl);
331 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) = 0;
378 /* For fields, set the bit field type and update the alignment. */
379 if (code == FIELD_DECL)
381 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
382 if (maximum_field_alignment != 0)
383 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
384 else if (DECL_PACKED (decl))
386 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
387 DECL_USER_ALIGN (decl) = 0;
391 /* See if we can use an ordinary integer mode for a bit-field.
392 Conditions are: a fixed size that is correct for another mode
393 and occupying a complete byte or bytes on proper boundary. */
394 if (code == FIELD_DECL && DECL_BIT_FIELD (decl)
395 && TYPE_SIZE (type) != 0
396 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
397 && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
399 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 /* Hook for a front-end function that can modify the record layout as needed
448 immediately before it is finalized. */
450 void (*lang_adjust_rli) PARAMS ((record_layout_info)) = 0;
452 void
453 set_lang_adjust_rli (f)
454 void (*f) PARAMS ((record_layout_info));
456 lang_adjust_rli = f;
459 /* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
460 QUAL_UNION_TYPE. Return a pointer to a struct record_layout_info which
461 is to be passed to all other layout functions for this record. It is the
462 responsibility of the caller to call `free' for the storage returned.
463 Note that garbage collection is not permitted until we finish laying
464 out the record. */
466 record_layout_info
467 start_record_layout (t)
468 tree t;
470 record_layout_info rli
471 = (record_layout_info) xmalloc (sizeof (struct record_layout_info_s));
473 rli->t = t;
475 /* If the type has a minimum specified alignment (via an attribute
476 declaration, for example) use it -- otherwise, start with a
477 one-byte alignment. */
478 rli->record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (t));
479 rli->unpacked_align = rli->unpadded_align = rli->record_align;
480 rli->offset_align = MAX (rli->record_align, BIGGEST_ALIGNMENT);
482 #ifdef STRUCTURE_SIZE_BOUNDARY
483 /* Packed structures don't need to have minimum size. */
484 if (! TYPE_PACKED (t))
485 rli->record_align = MAX (rli->record_align, STRUCTURE_SIZE_BOUNDARY);
486 #endif
488 rli->offset = size_zero_node;
489 rli->bitpos = bitsize_zero_node;
490 rli->pending_statics = 0;
491 rli->packed_maybe_necessary = 0;
493 return rli;
496 /* These four routines perform computations that convert between
497 the offset/bitpos forms and byte and bit offsets. */
499 tree
500 bit_from_pos (offset, bitpos)
501 tree offset, bitpos;
503 return size_binop (PLUS_EXPR, bitpos,
504 size_binop (MULT_EXPR, convert (bitsizetype, offset),
505 bitsize_unit_node));
508 tree
509 byte_from_pos (offset, bitpos)
510 tree offset, bitpos;
512 return size_binop (PLUS_EXPR, offset,
513 convert (sizetype,
514 size_binop (TRUNC_DIV_EXPR, bitpos,
515 bitsize_unit_node)));
518 void
519 pos_from_byte (poffset, pbitpos, off_align, pos)
520 tree *poffset, *pbitpos;
521 unsigned int off_align;
522 tree pos;
524 *poffset
525 = size_binop (MULT_EXPR,
526 convert (sizetype,
527 size_binop (FLOOR_DIV_EXPR, pos,
528 bitsize_int (off_align
529 / BITS_PER_UNIT))),
530 size_int (off_align / BITS_PER_UNIT));
531 *pbitpos = size_binop (MULT_EXPR,
532 size_binop (FLOOR_MOD_EXPR, pos,
533 bitsize_int (off_align / BITS_PER_UNIT)),
534 bitsize_unit_node);
537 void
538 pos_from_bit (poffset, pbitpos, off_align, pos)
539 tree *poffset, *pbitpos;
540 unsigned int off_align;
541 tree pos;
543 *poffset = size_binop (MULT_EXPR,
544 convert (sizetype,
545 size_binop (FLOOR_DIV_EXPR, pos,
546 bitsize_int (off_align))),
547 size_int (off_align / BITS_PER_UNIT));
548 *pbitpos = size_binop (FLOOR_MOD_EXPR, pos, bitsize_int (off_align));
551 /* Given a pointer to bit and byte offsets and an offset alignment,
552 normalize the offsets so they are within the alignment. */
554 void
555 normalize_offset (poffset, pbitpos, off_align)
556 tree *poffset, *pbitpos;
557 unsigned int off_align;
559 /* If the bit position is now larger than it should be, adjust it
560 downwards. */
561 if (compare_tree_int (*pbitpos, off_align) >= 0)
563 tree extra_aligns = size_binop (FLOOR_DIV_EXPR, *pbitpos,
564 bitsize_int (off_align));
566 *poffset
567 = size_binop (PLUS_EXPR, *poffset,
568 size_binop (MULT_EXPR, convert (sizetype, extra_aligns),
569 size_int (off_align / BITS_PER_UNIT)));
571 *pbitpos
572 = size_binop (FLOOR_MOD_EXPR, *pbitpos, bitsize_int (off_align));
576 /* Print debugging information about the information in RLI. */
578 void
579 debug_rli (rli)
580 record_layout_info rli;
582 print_node_brief (stderr, "type", rli->t, 0);
583 print_node_brief (stderr, "\noffset", rli->offset, 0);
584 print_node_brief (stderr, " bitpos", rli->bitpos, 0);
586 fprintf (stderr, "\naligns: rec = %u, unpack = %u, unpad = %u, off = %u\n",
587 rli->record_align, rli->unpacked_align, rli->unpadded_align,
588 rli->offset_align);
589 if (rli->packed_maybe_necessary)
590 fprintf (stderr, "packed may be necessary\n");
592 if (rli->pending_statics)
594 fprintf (stderr, "pending statics:\n");
595 debug_tree (rli->pending_statics);
599 /* Given an RLI with a possibly-incremented BITPOS, adjust OFFSET and
600 BITPOS if necessary to keep BITPOS below OFFSET_ALIGN. */
602 void
603 normalize_rli (rli)
604 record_layout_info rli;
606 normalize_offset (&rli->offset, &rli->bitpos, rli->offset_align);
609 /* Returns the size in bytes allocated so far. */
611 tree
612 rli_size_unit_so_far (rli)
613 record_layout_info rli;
615 return byte_from_pos (rli->offset, rli->bitpos);
618 /* Returns the size in bits allocated so far. */
620 tree
621 rli_size_so_far (rli)
622 record_layout_info rli;
624 return bit_from_pos (rli->offset, rli->bitpos);
627 /* Called from place_field to handle unions. */
629 static void
630 place_union_field (rli, field)
631 record_layout_info rli;
632 tree field;
634 unsigned int desired_align;
636 layout_decl (field, 0);
638 DECL_FIELD_OFFSET (field) = size_zero_node;
639 DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
640 SET_DECL_OFFSET_ALIGN (field, BIGGEST_ALIGNMENT);
642 desired_align = DECL_ALIGN (field);
644 #ifdef BIGGEST_FIELD_ALIGNMENT
645 /* Some targets (i.e. i386) limit union field alignment
646 to a lower boundary than alignment of variables unless
647 it was overridden by attribute aligned. */
648 if (! DECL_USER_ALIGN (field))
649 desired_align =
650 MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
651 #endif
653 TYPE_USER_ALIGN (rli->t) |= DECL_USER_ALIGN (field);
655 /* Union must be at least as aligned as any field requires. */
656 rli->record_align = MAX (rli->record_align, desired_align);
657 rli->unpadded_align = MAX (rli->unpadded_align, desired_align);
659 #ifdef PCC_BITFIELD_TYPE_MATTERS
660 /* On the m88000, a bit field of declare type `int' forces the
661 entire union to have `int' alignment. */
662 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
664 rli->record_align = MAX (rli->record_align,
665 TYPE_ALIGN (TREE_TYPE (field)));
666 rli->unpadded_align = MAX (rli->unpadded_align,
667 TYPE_ALIGN (TREE_TYPE (field)));
669 #endif
671 /* We assume the union's size will be a multiple of a byte so we don't
672 bother with BITPOS. */
673 if (TREE_CODE (rli->t) == UNION_TYPE)
674 rli->offset = size_binop (MAX_EXPR, rli->offset, DECL_SIZE_UNIT (field));
675 else if (TREE_CODE (rli->t) == QUAL_UNION_TYPE)
676 rli->offset = fold (build (COND_EXPR, sizetype,
677 DECL_QUALIFIER (field),
678 DECL_SIZE_UNIT (field), rli->offset));
681 /* RLI contains information about the layout of a RECORD_TYPE. FIELD
682 is a FIELD_DECL to be added after those fields already present in
683 T. (FIELD is not actually added to the TYPE_FIELDS list here;
684 callers that desire that behavior must manually perform that step.) */
686 void
687 place_field (rli, field)
688 record_layout_info rli;
689 tree field;
691 /* The alignment required for FIELD. */
692 unsigned int desired_align;
693 /* The alignment FIELD would have if we just dropped it into the
694 record as it presently stands. */
695 unsigned int known_align;
696 unsigned int actual_align;
697 unsigned int user_align;
698 /* The type of this field. */
699 tree type = TREE_TYPE (field);
701 if (TREE_CODE (field) == ERROR_MARK || TREE_CODE (type) == ERROR_MARK)
702 return;
704 /* If FIELD is static, then treat it like a separate variable, not
705 really like a structure field. If it is a FUNCTION_DECL, it's a
706 method. In both cases, all we do is lay out the decl, and we do
707 it *after* the record is laid out. */
708 if (TREE_CODE (field) == VAR_DECL)
710 rli->pending_statics = tree_cons (NULL_TREE, field,
711 rli->pending_statics);
712 return;
715 /* Enumerators and enum types which are local to this class need not
716 be laid out. Likewise for initialized constant fields. */
717 else if (TREE_CODE (field) != FIELD_DECL)
718 return;
720 /* Unions are laid out very differently than records, so split
721 that code off to another function. */
722 else if (TREE_CODE (rli->t) != RECORD_TYPE)
724 place_union_field (rli, field);
725 return;
728 /* Work out the known alignment so far. Note that A & (-A) is the
729 value of the least-significant bit in A that is one. */
730 if (! integer_zerop (rli->bitpos))
731 known_align = (tree_low_cst (rli->bitpos, 1)
732 & - tree_low_cst (rli->bitpos, 1));
733 else if (integer_zerop (rli->offset))
734 known_align = BIGGEST_ALIGNMENT;
735 else if (host_integerp (rli->offset, 1))
736 known_align = (BITS_PER_UNIT
737 * (tree_low_cst (rli->offset, 1)
738 & - tree_low_cst (rli->offset, 1)));
739 else
740 known_align = rli->offset_align;
742 /* Lay out the field so we know what alignment it needs. For a
743 packed field, use the alignment as specified, disregarding what
744 the type would want. */
745 desired_align = DECL_ALIGN (field);
746 user_align = DECL_USER_ALIGN (field);
747 layout_decl (field, known_align);
748 if (! DECL_PACKED (field))
750 desired_align = DECL_ALIGN (field);
751 user_align = DECL_USER_ALIGN (field);
754 /* Some targets (i.e. i386, VMS) limit struct field alignment
755 to a lower boundary than alignment of variables unless
756 it was overridden by attribute aligned. */
757 #ifdef BIGGEST_FIELD_ALIGNMENT
758 if (! user_align)
759 desired_align
760 = MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
761 #endif
763 #ifdef ADJUST_FIELD_ALIGN
764 desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
765 #endif
767 /* Record must have at least as much alignment as any field.
768 Otherwise, the alignment of the field within the record is
769 meaningless. */
770 #ifdef PCC_BITFIELD_TYPE_MATTERS
771 if (PCC_BITFIELD_TYPE_MATTERS && type != error_mark_node
772 && DECL_BIT_FIELD_TYPE (field)
773 && ! integer_zerop (TYPE_SIZE (type)))
775 /* For these machines, a zero-length field does not
776 affect the alignment of the structure as a whole.
777 It does, however, affect the alignment of the next field
778 within the structure. */
779 if (! integer_zerop (DECL_SIZE (field)))
780 rli->record_align = MAX (rli->record_align, desired_align);
781 else if (! DECL_PACKED (field))
782 desired_align = TYPE_ALIGN (type);
784 /* A named bit field of declared type `int'
785 forces the entire structure to have `int' alignment. */
786 if (DECL_NAME (field) != 0)
788 unsigned int type_align = TYPE_ALIGN (type);
790 if (maximum_field_alignment != 0)
791 type_align = MIN (type_align, maximum_field_alignment);
792 else if (DECL_PACKED (field))
793 type_align = MIN (type_align, BITS_PER_UNIT);
795 rli->record_align = MAX (rli->record_align, type_align);
796 rli->unpadded_align = MAX (rli->unpadded_align, DECL_ALIGN (field));
797 if (warn_packed)
798 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
801 else
802 #endif
804 rli->record_align = MAX (rli->record_align, desired_align);
805 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
806 rli->unpadded_align = MAX (rli->unpadded_align, DECL_ALIGN (field));
809 if (warn_packed && DECL_PACKED (field))
811 if (known_align > TYPE_ALIGN (type))
813 if (TYPE_ALIGN (type) > desired_align)
815 if (STRICT_ALIGNMENT)
816 warning_with_decl (field, "packed attribute causes inefficient alignment for `%s'");
817 else
818 warning_with_decl (field, "packed attribute is unnecessary for `%s'");
821 else
822 rli->packed_maybe_necessary = 1;
825 /* Does this field automatically have alignment it needs by virtue
826 of the fields that precede it and the record's own alignment? */
827 if (known_align < desired_align)
829 /* No, we need to skip space before this field.
830 Bump the cumulative size to multiple of field alignment. */
832 if (warn_padded)
833 warning_with_decl (field, "padding struct to align `%s'");
835 /* If the alignment is still within offset_align, just align
836 the bit position. */
837 if (desired_align < rli->offset_align)
838 rli->bitpos = round_up (rli->bitpos, desired_align);
839 else
841 /* First adjust OFFSET by the partial bits, then align. */
842 rli->offset
843 = size_binop (PLUS_EXPR, rli->offset,
844 convert (sizetype,
845 size_binop (CEIL_DIV_EXPR, rli->bitpos,
846 bitsize_unit_node)));
847 rli->bitpos = bitsize_zero_node;
849 rli->offset = round_up (rli->offset, desired_align / BITS_PER_UNIT);
852 if (! TREE_CONSTANT (rli->offset))
853 rli->offset_align = desired_align;
857 /* Handle compatibility with PCC. Note that if the record has any
858 variable-sized fields, we need not worry about compatibility. */
859 #ifdef PCC_BITFIELD_TYPE_MATTERS
860 if (PCC_BITFIELD_TYPE_MATTERS
861 && TREE_CODE (field) == FIELD_DECL
862 && type != error_mark_node
863 && DECL_BIT_FIELD (field)
864 && ! DECL_PACKED (field)
865 && maximum_field_alignment == 0
866 && ! integer_zerop (DECL_SIZE (field))
867 && host_integerp (DECL_SIZE (field), 1)
868 && host_integerp (rli->offset, 1)
869 && host_integerp (TYPE_SIZE (type), 1))
871 unsigned int type_align = TYPE_ALIGN (type);
872 tree dsize = DECL_SIZE (field);
873 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
874 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
875 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
877 /* A bit field may not span more units of alignment of its type
878 than its type itself. Advance to next boundary if necessary. */
879 if ((((offset * BITS_PER_UNIT + bit_offset + field_size +
880 type_align - 1)
881 / type_align)
882 - (offset * BITS_PER_UNIT + bit_offset) / type_align)
883 > tree_low_cst (TYPE_SIZE (type), 1) / type_align)
884 rli->bitpos = round_up (rli->bitpos, type_align);
886 #endif
888 #ifdef BITFIELD_NBYTES_LIMITED
889 if (BITFIELD_NBYTES_LIMITED
890 && TREE_CODE (field) == FIELD_DECL
891 && type != error_mark_node
892 && DECL_BIT_FIELD_TYPE (field)
893 && ! DECL_PACKED (field)
894 && ! integer_zerop (DECL_SIZE (field))
895 && host_integerp (DECL_SIZE (field), 1)
896 && host_integerp (rli->offset, 1)
897 && host_integerp (TYPE_SIZE (type), 1))
899 unsigned int type_align = TYPE_ALIGN (type);
900 tree dsize = DECL_SIZE (field);
901 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
902 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
903 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
905 if (maximum_field_alignment != 0)
906 type_align = MIN (type_align, maximum_field_alignment);
907 /* ??? This test is opposite the test in the containing if
908 statement, so this code is unreachable currently. */
909 else if (DECL_PACKED (field))
910 type_align = MIN (type_align, BITS_PER_UNIT);
912 /* A bit field may not span the unit of alignment of its type.
913 Advance to next boundary if necessary. */
914 /* ??? This code should match the code above for the
915 PCC_BITFIELD_TYPE_MATTERS case. */
916 if ((offset * BITS_PER_UNIT + bit_offset) / type_align
917 != ((offset * BITS_PER_UNIT + bit_offset + field_size - 1)
918 / type_align))
919 rli->bitpos = round_up (rli->bitpos, type_align);
921 #endif
923 /* Offset so far becomes the position of this field after normalizing. */
924 normalize_rli (rli);
925 DECL_FIELD_OFFSET (field) = rli->offset;
926 DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
927 SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
929 TYPE_USER_ALIGN (rli->t) |= user_align;
931 /* If this field ended up more aligned than we thought it would be (we
932 approximate this by seeing if its position changed), lay out the field
933 again; perhaps we can use an integral mode for it now. */
934 if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field)))
935 actual_align = (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
936 & - tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1));
937 else if (integer_zerop (DECL_FIELD_OFFSET (field)))
938 actual_align = BIGGEST_ALIGNMENT;
939 else if (host_integerp (DECL_FIELD_OFFSET (field), 1))
940 actual_align = (BITS_PER_UNIT
941 * (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
942 & - tree_low_cst (DECL_FIELD_OFFSET (field), 1)));
943 else
944 actual_align = DECL_OFFSET_ALIGN (field);
946 if (known_align != actual_align)
947 layout_decl (field, actual_align);
949 /* Now add size of this field to the size of the record. If the size is
950 not constant, treat the field as being a multiple of bytes and just
951 adjust the offset, resetting the bit position. Otherwise, apportion the
952 size amongst the bit position and offset. First handle the case of an
953 unspecified size, which can happen when we have an invalid nested struct
954 definition, such as struct j { struct j { int i; } }. The error message
955 is printed in finish_struct. */
956 if (DECL_SIZE (field) == 0)
957 /* Do nothing. */;
958 else if (TREE_CODE (DECL_SIZE_UNIT (field)) != INTEGER_CST
959 || TREE_CONSTANT_OVERFLOW (DECL_SIZE_UNIT (field)))
961 rli->offset
962 = size_binop (PLUS_EXPR, rli->offset,
963 convert (sizetype,
964 size_binop (CEIL_DIV_EXPR, rli->bitpos,
965 bitsize_unit_node)));
966 rli->offset
967 = size_binop (PLUS_EXPR, rli->offset, DECL_SIZE_UNIT (field));
968 rli->bitpos = bitsize_zero_node;
969 rli->offset_align = MIN (rli->offset_align, DECL_ALIGN (field));
971 else
973 rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
974 normalize_rli (rli);
978 /* Assuming that all the fields have been laid out, this function uses
979 RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
980 inidicated by RLI. */
982 static void
983 finalize_record_size (rli)
984 record_layout_info rli;
986 tree unpadded_size, unpadded_size_unit;
988 /* Now we want just byte and bit offsets, so set the offset alignment
989 to be a byte and then normalize. */
990 rli->offset_align = BITS_PER_UNIT;
991 normalize_rli (rli);
993 /* Determine the desired alignment. */
994 #ifdef ROUND_TYPE_ALIGN
995 TYPE_ALIGN (rli->t) = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t),
996 rli->record_align);
997 #else
998 TYPE_ALIGN (rli->t) = MAX (TYPE_ALIGN (rli->t), rli->record_align);
999 #endif
1001 /* Compute the size so far. Be sure to allow for extra bits in the
1002 size in bytes. We have guaranteed above that it will be no more
1003 than a single byte. */
1004 unpadded_size = rli_size_so_far (rli);
1005 unpadded_size_unit = rli_size_unit_so_far (rli);
1006 if (! integer_zerop (rli->bitpos))
1007 unpadded_size_unit
1008 = size_binop (PLUS_EXPR, unpadded_size_unit, size_one_node);
1010 /* Record the un-rounded size in the binfo node. But first we check
1011 the size of TYPE_BINFO to make sure that BINFO_SIZE is available. */
1012 if (TYPE_BINFO (rli->t) && TREE_VEC_LENGTH (TYPE_BINFO (rli->t)) > 6)
1014 TYPE_BINFO_SIZE (rli->t) = unpadded_size;
1015 TYPE_BINFO_SIZE_UNIT (rli->t) = unpadded_size_unit;
1018 /* Round the size up to be a multiple of the required alignment */
1019 #ifdef ROUND_TYPE_SIZE
1020 TYPE_SIZE (rli->t) = ROUND_TYPE_SIZE (rli->t, unpadded_size,
1021 TYPE_ALIGN (rli->t));
1022 TYPE_SIZE_UNIT (rli->t)
1023 = ROUND_TYPE_SIZE_UNIT (rli->t, unpadded_size_unit,
1024 TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
1025 #else
1026 TYPE_SIZE (rli->t) = round_up (unpadded_size, TYPE_ALIGN (rli->t));
1027 TYPE_SIZE_UNIT (rli->t) = round_up (unpadded_size_unit,
1028 TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
1029 #endif
1031 if (warn_padded && TREE_CONSTANT (unpadded_size)
1032 && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0)
1033 warning ("padding struct size to alignment boundary");
1035 if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
1036 && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
1037 && TREE_CONSTANT (unpadded_size))
1039 tree unpacked_size;
1041 #ifdef ROUND_TYPE_ALIGN
1042 rli->unpacked_align
1043 = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t), rli->unpacked_align);
1044 #else
1045 rli->unpacked_align = MAX (TYPE_ALIGN (rli->t), rli->unpacked_align);
1046 #endif
1048 #ifdef ROUND_TYPE_SIZE
1049 unpacked_size = ROUND_TYPE_SIZE (rli->t, TYPE_SIZE (rli->t),
1050 rli->unpacked_align);
1051 #else
1052 unpacked_size = round_up (TYPE_SIZE (rli->t), rli->unpacked_align);
1053 #endif
1055 if (simple_cst_equal (unpacked_size, TYPE_SIZE (rli->t)))
1057 TYPE_PACKED (rli->t) = 0;
1059 if (TYPE_NAME (rli->t))
1061 const char *name;
1063 if (TREE_CODE (TYPE_NAME (rli->t)) == IDENTIFIER_NODE)
1064 name = IDENTIFIER_POINTER (TYPE_NAME (rli->t));
1065 else
1066 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (rli->t)));
1068 if (STRICT_ALIGNMENT)
1069 warning ("packed attribute causes inefficient alignment for `%s'", name);
1070 else
1071 warning ("packed attribute is unnecessary for `%s'", name);
1073 else
1075 if (STRICT_ALIGNMENT)
1076 warning ("packed attribute causes inefficient alignment");
1077 else
1078 warning ("packed attribute is unnecessary");
1084 /* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE). */
1086 void
1087 compute_record_mode (type)
1088 tree type;
1090 tree field;
1091 enum machine_mode mode = VOIDmode;
1093 /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
1094 However, if possible, we use a mode that fits in a register
1095 instead, in order to allow for better optimization down the
1096 line. */
1097 TYPE_MODE (type) = BLKmode;
1099 if (! host_integerp (TYPE_SIZE (type), 1))
1100 return;
1102 /* A record which has any BLKmode members must itself be
1103 BLKmode; it can't go in a register. Unless the member is
1104 BLKmode only because it isn't aligned. */
1105 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1107 unsigned HOST_WIDE_INT bitpos;
1109 if (TREE_CODE (field) != FIELD_DECL)
1110 continue;
1112 if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK
1113 || (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1114 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1115 || ! host_integerp (bit_position (field), 1)
1116 || DECL_SIZE (field) == 0
1117 || ! host_integerp (DECL_SIZE (field), 1))
1118 return;
1120 bitpos = int_bit_position (field);
1122 /* Must be BLKmode if any field crosses a word boundary,
1123 since extract_bit_field can't handle that in registers. */
1124 if (bitpos / BITS_PER_WORD
1125 != ((tree_low_cst (DECL_SIZE (field), 1) + bitpos - 1)
1126 / BITS_PER_WORD)
1127 /* But there is no problem if the field is entire words. */
1128 && tree_low_cst (DECL_SIZE (field), 1) % BITS_PER_WORD != 0)
1129 return;
1131 /* If this field is the whole struct, remember its mode so
1132 that, say, we can put a double in a class into a DF
1133 register instead of forcing it to live in the stack. */
1134 if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
1135 mode = DECL_MODE (field);
1137 #ifdef MEMBER_TYPE_FORCES_BLK
1138 /* With some targets, eg. c4x, it is sub-optimal
1139 to access an aligned BLKmode structure as a scalar. */
1140 if (mode == VOIDmode && MEMBER_TYPE_FORCES_BLK (field))
1141 return;
1142 #endif /* MEMBER_TYPE_FORCES_BLK */
1145 /* If we only have one real field; use its mode. This only applies to
1146 RECORD_TYPE. This does not apply to unions. */
1147 if (TREE_CODE (type) == RECORD_TYPE && mode != VOIDmode)
1148 TYPE_MODE (type) = mode;
1149 else
1150 TYPE_MODE (type) = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1152 /* If structure's known alignment is less than what the scalar
1153 mode would need, and it matters, then stick with BLKmode. */
1154 if (TYPE_MODE (type) != BLKmode
1155 && STRICT_ALIGNMENT
1156 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1157 || TYPE_ALIGN (type) >= GET_MODE_ALIGNMENT (TYPE_MODE (type))))
1159 /* If this is the only reason this type is BLKmode, then
1160 don't force containing types to be BLKmode. */
1161 TYPE_NO_FORCE_BLK (type) = 1;
1162 TYPE_MODE (type) = BLKmode;
1166 /* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
1167 out. */
1169 static void
1170 finalize_type_size (type)
1171 tree type;
1173 /* Normally, use the alignment corresponding to the mode chosen.
1174 However, where strict alignment is not required, avoid
1175 over-aligning structures, since most compilers do not do this
1176 alignment. */
1178 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1179 && (STRICT_ALIGNMENT
1180 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1181 && TREE_CODE (type) != QUAL_UNION_TYPE
1182 && TREE_CODE (type) != ARRAY_TYPE)))
1184 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1185 TYPE_USER_ALIGN (type) = 0;
1188 /* Do machine-dependent extra alignment. */
1189 #ifdef ROUND_TYPE_ALIGN
1190 TYPE_ALIGN (type)
1191 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1192 #endif
1194 /* If we failed to find a simple way to calculate the unit size
1195 of the type, find it by division. */
1196 if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1197 /* TYPE_SIZE (type) is computed in bitsizetype. After the division, the
1198 result will fit in sizetype. We will get more efficient code using
1199 sizetype, so we force a conversion. */
1200 TYPE_SIZE_UNIT (type)
1201 = convert (sizetype,
1202 size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1203 bitsize_unit_node));
1205 if (TYPE_SIZE (type) != 0)
1207 #ifdef ROUND_TYPE_SIZE
1208 TYPE_SIZE (type)
1209 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1210 TYPE_SIZE_UNIT (type)
1211 = ROUND_TYPE_SIZE_UNIT (type, TYPE_SIZE_UNIT (type),
1212 TYPE_ALIGN (type) / BITS_PER_UNIT);
1213 #else
1214 TYPE_SIZE (type) = round_up (TYPE_SIZE (type), TYPE_ALIGN (type));
1215 TYPE_SIZE_UNIT (type)
1216 = round_up (TYPE_SIZE_UNIT (type), TYPE_ALIGN (type) / BITS_PER_UNIT);
1217 #endif
1220 /* Evaluate nonconstant sizes only once, either now or as soon as safe. */
1221 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1222 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1223 if (TYPE_SIZE_UNIT (type) != 0
1224 && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1225 TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1227 /* Also layout any other variants of the type. */
1228 if (TYPE_NEXT_VARIANT (type)
1229 || type != TYPE_MAIN_VARIANT (type))
1231 tree variant;
1232 /* Record layout info of this variant. */
1233 tree size = TYPE_SIZE (type);
1234 tree size_unit = TYPE_SIZE_UNIT (type);
1235 unsigned int align = TYPE_ALIGN (type);
1236 unsigned int user_align = TYPE_USER_ALIGN (type);
1237 enum machine_mode mode = TYPE_MODE (type);
1239 /* Copy it into all variants. */
1240 for (variant = TYPE_MAIN_VARIANT (type);
1241 variant != 0;
1242 variant = TYPE_NEXT_VARIANT (variant))
1244 TYPE_SIZE (variant) = size;
1245 TYPE_SIZE_UNIT (variant) = size_unit;
1246 TYPE_ALIGN (variant) = align;
1247 TYPE_USER_ALIGN (variant) = user_align;
1248 TYPE_MODE (variant) = mode;
1253 /* Do all of the work required to layout the type indicated by RLI,
1254 once the fields have been laid out. This function will call `free'
1255 for RLI. */
1257 void
1258 finish_record_layout (rli)
1259 record_layout_info rli;
1261 /* Compute the final size. */
1262 finalize_record_size (rli);
1264 /* Compute the TYPE_MODE for the record. */
1265 compute_record_mode (rli->t);
1267 /* Perform any last tweaks to the TYPE_SIZE, etc. */
1268 finalize_type_size (rli->t);
1270 /* Lay out any static members. This is done now because their type
1271 may use the record's type. */
1272 while (rli->pending_statics)
1274 layout_decl (TREE_VALUE (rli->pending_statics), 0);
1275 rli->pending_statics = TREE_CHAIN (rli->pending_statics);
1278 /* Clean up. */
1279 free (rli);
1282 /* Calculate the mode, size, and alignment for TYPE.
1283 For an array type, calculate the element separation as well.
1284 Record TYPE on the chain of permanent or temporary types
1285 so that dbxout will find out about it.
1287 TYPE_SIZE of a type is nonzero if the type has been laid out already.
1288 layout_type does nothing on such a type.
1290 If the type is incomplete, its TYPE_SIZE remains zero. */
1292 void
1293 layout_type (type)
1294 tree type;
1296 if (type == 0)
1297 abort ();
1299 /* Do nothing if type has been laid out before. */
1300 if (TYPE_SIZE (type))
1301 return;
1303 switch (TREE_CODE (type))
1305 case LANG_TYPE:
1306 /* This kind of type is the responsibility
1307 of the language-specific code. */
1308 abort ();
1310 case BOOLEAN_TYPE: /* Used for Java, Pascal, and Chill. */
1311 if (TYPE_PRECISION (type) == 0)
1312 TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
1314 /* ... fall through ... */
1316 case INTEGER_TYPE:
1317 case ENUMERAL_TYPE:
1318 case CHAR_TYPE:
1319 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
1320 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
1321 TREE_UNSIGNED (type) = 1;
1323 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
1324 MODE_INT);
1325 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1326 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1327 break;
1329 case REAL_TYPE:
1330 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
1331 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1332 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1333 break;
1335 case COMPLEX_TYPE:
1336 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
1337 TYPE_MODE (type)
1338 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
1339 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
1340 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
1342 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1343 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1344 break;
1346 case VECTOR_TYPE:
1348 tree subtype;
1350 subtype = TREE_TYPE (type);
1351 TREE_UNSIGNED (type) = TREE_UNSIGNED (subtype);
1352 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1353 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1355 break;
1357 case VOID_TYPE:
1358 /* This is an incomplete type and so doesn't have a size. */
1359 TYPE_ALIGN (type) = 1;
1360 TYPE_USER_ALIGN (type) = 0;
1361 TYPE_MODE (type) = VOIDmode;
1362 break;
1364 case OFFSET_TYPE:
1365 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
1366 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
1367 /* A pointer might be MODE_PARTIAL_INT,
1368 but ptrdiff_t must be integral. */
1369 TYPE_MODE (type) = mode_for_size (POINTER_SIZE, MODE_INT, 0);
1370 break;
1372 case FUNCTION_TYPE:
1373 case METHOD_TYPE:
1374 TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
1375 TYPE_SIZE (type) = bitsize_int (2 * POINTER_SIZE);
1376 TYPE_SIZE_UNIT (type) = size_int ((2 * POINTER_SIZE) / BITS_PER_UNIT);
1377 break;
1379 case POINTER_TYPE:
1380 case REFERENCE_TYPE:
1382 int nbits = ((TREE_CODE (type) == REFERENCE_TYPE
1383 && reference_types_internal)
1384 ? GET_MODE_BITSIZE (Pmode) : POINTER_SIZE);
1386 TYPE_MODE (type) = nbits == POINTER_SIZE ? ptr_mode : Pmode;
1387 TYPE_SIZE (type) = bitsize_int (nbits);
1388 TYPE_SIZE_UNIT (type) = size_int (nbits / BITS_PER_UNIT);
1389 TREE_UNSIGNED (type) = 1;
1390 TYPE_PRECISION (type) = nbits;
1392 break;
1394 case ARRAY_TYPE:
1396 tree index = TYPE_DOMAIN (type);
1397 tree element = TREE_TYPE (type);
1399 build_pointer_type (element);
1401 /* We need to know both bounds in order to compute the size. */
1402 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
1403 && TYPE_SIZE (element))
1405 tree ub = TYPE_MAX_VALUE (index);
1406 tree lb = TYPE_MIN_VALUE (index);
1407 tree length;
1408 tree element_size;
1410 /* The initial subtraction should happen in the original type so
1411 that (possible) negative values are handled appropriately. */
1412 length = size_binop (PLUS_EXPR, size_one_node,
1413 convert (sizetype,
1414 fold (build (MINUS_EXPR,
1415 TREE_TYPE (lb),
1416 ub, lb))));
1418 /* Special handling for arrays of bits (for Chill). */
1419 element_size = TYPE_SIZE (element);
1420 if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element)
1421 && (integer_zerop (TYPE_MAX_VALUE (element))
1422 || integer_onep (TYPE_MAX_VALUE (element)))
1423 && host_integerp (TYPE_MIN_VALUE (element), 1))
1425 HOST_WIDE_INT maxvalue
1426 = tree_low_cst (TYPE_MAX_VALUE (element), 1);
1427 HOST_WIDE_INT minvalue
1428 = tree_low_cst (TYPE_MIN_VALUE (element), 1);
1430 if (maxvalue - minvalue == 1
1431 && (maxvalue == 1 || maxvalue == 0))
1432 element_size = integer_one_node;
1435 TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size,
1436 convert (bitsizetype, length));
1438 /* If we know the size of the element, calculate the total
1439 size directly, rather than do some division thing below.
1440 This optimization helps Fortran assumed-size arrays
1441 (where the size of the array is determined at runtime)
1442 substantially.
1443 Note that we can't do this in the case where the size of
1444 the elements is one bit since TYPE_SIZE_UNIT cannot be
1445 set correctly in that case. */
1446 if (TYPE_SIZE_UNIT (element) != 0 && ! integer_onep (element_size))
1447 TYPE_SIZE_UNIT (type)
1448 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
1451 /* Now round the alignment and size,
1452 using machine-dependent criteria if any. */
1454 #ifdef ROUND_TYPE_ALIGN
1455 TYPE_ALIGN (type)
1456 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
1457 #else
1458 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
1459 #endif
1460 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (element);
1462 #ifdef ROUND_TYPE_SIZE
1463 if (TYPE_SIZE (type) != 0)
1465 tree tmp
1466 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1468 /* If the rounding changed the size of the type, remove any
1469 pre-calculated TYPE_SIZE_UNIT. */
1470 if (simple_cst_equal (TYPE_SIZE (type), tmp) != 1)
1471 TYPE_SIZE_UNIT (type) = NULL;
1473 TYPE_SIZE (type) = tmp;
1475 #endif
1477 TYPE_MODE (type) = BLKmode;
1478 if (TYPE_SIZE (type) != 0
1479 #ifdef MEMBER_TYPE_FORCES_BLK
1480 && ! MEMBER_TYPE_FORCES_BLK (type)
1481 #endif
1482 /* BLKmode elements force BLKmode aggregate;
1483 else extract/store fields may lose. */
1484 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
1485 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
1487 TYPE_MODE (type)
1488 = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1490 if (TYPE_MODE (type) != BLKmode
1491 && STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
1492 && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (TYPE_MODE (type))
1493 && TYPE_MODE (type) != BLKmode)
1495 TYPE_NO_FORCE_BLK (type) = 1;
1496 TYPE_MODE (type) = BLKmode;
1499 break;
1502 case RECORD_TYPE:
1503 case UNION_TYPE:
1504 case QUAL_UNION_TYPE:
1506 tree field;
1507 record_layout_info rli;
1509 /* Initialize the layout information. */
1510 rli = start_record_layout (type);
1512 /* If this is a QUAL_UNION_TYPE, we want to process the fields
1513 in the reverse order in building the COND_EXPR that denotes
1514 its size. We reverse them again later. */
1515 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1516 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1518 /* Place all the fields. */
1519 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1520 place_field (rli, field);
1522 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1523 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1525 if (lang_adjust_rli)
1526 (*lang_adjust_rli) (rli);
1528 /* Finish laying out the record. */
1529 finish_record_layout (rli);
1531 break;
1533 case SET_TYPE: /* Used by Chill and Pascal. */
1534 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1535 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1536 abort();
1537 else
1539 #ifndef SET_WORD_SIZE
1540 #define SET_WORD_SIZE BITS_PER_WORD
1541 #endif
1542 unsigned int alignment
1543 = set_alignment ? set_alignment : SET_WORD_SIZE;
1544 int size_in_bits
1545 = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1546 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
1547 int rounded_size
1548 = ((size_in_bits + alignment - 1) / alignment) * alignment;
1550 if (rounded_size > (int) alignment)
1551 TYPE_MODE (type) = BLKmode;
1552 else
1553 TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1555 TYPE_SIZE (type) = bitsize_int (rounded_size);
1556 TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1557 TYPE_ALIGN (type) = alignment;
1558 TYPE_USER_ALIGN (type) = 0;
1559 TYPE_PRECISION (type) = size_in_bits;
1561 break;
1563 case FILE_TYPE:
1564 /* The size may vary in different languages, so the language front end
1565 should fill in the size. */
1566 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1567 TYPE_USER_ALIGN (type) = 0;
1568 TYPE_MODE (type) = BLKmode;
1569 break;
1571 default:
1572 abort ();
1575 /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE. For
1576 records and unions, finish_record_layout already called this
1577 function. */
1578 if (TREE_CODE (type) != RECORD_TYPE
1579 && TREE_CODE (type) != UNION_TYPE
1580 && TREE_CODE (type) != QUAL_UNION_TYPE)
1581 finalize_type_size (type);
1583 /* If this type is created before sizetype has been permanently set,
1584 record it so set_sizetype can fix it up. */
1585 if (! sizetype_set)
1586 early_type_list = tree_cons (NULL_TREE, type, early_type_list);
1588 /* If an alias set has been set for this aggregate when it was incomplete,
1589 force it into alias set 0.
1590 This is too conservative, but we cannot call record_component_aliases
1591 here because some frontends still change the aggregates after
1592 layout_type. */
1593 if (AGGREGATE_TYPE_P (type) && TYPE_ALIAS_SET_KNOWN_P (type))
1594 TYPE_ALIAS_SET (type) = 0;
1597 /* Create and return a type for signed integers of PRECISION bits. */
1599 tree
1600 make_signed_type (precision)
1601 int precision;
1603 tree type = make_node (INTEGER_TYPE);
1605 TYPE_PRECISION (type) = precision;
1607 fixup_signed_type (type);
1608 return type;
1611 /* Create and return a type for unsigned integers of PRECISION bits. */
1613 tree
1614 make_unsigned_type (precision)
1615 int precision;
1617 tree type = make_node (INTEGER_TYPE);
1619 TYPE_PRECISION (type) = precision;
1621 fixup_unsigned_type (type);
1622 return type;
1625 /* Initialize sizetype and bitsizetype to a reasonable and temporary
1626 value to enable integer types to be created. */
1628 void
1629 initialize_sizetypes ()
1631 tree t = make_node (INTEGER_TYPE);
1633 /* Set this so we do something reasonable for the build_int_2 calls
1634 below. */
1635 integer_type_node = t;
1637 TYPE_MODE (t) = SImode;
1638 TYPE_ALIGN (t) = GET_MODE_ALIGNMENT (SImode);
1639 TYPE_USER_ALIGN (t) = 0;
1640 TYPE_SIZE (t) = build_int_2 (GET_MODE_BITSIZE (SImode), 0);
1641 TYPE_SIZE_UNIT (t) = build_int_2 (GET_MODE_SIZE (SImode), 0);
1642 TREE_UNSIGNED (t) = 1;
1643 TYPE_PRECISION (t) = GET_MODE_BITSIZE (SImode);
1644 TYPE_MIN_VALUE (t) = build_int_2 (0, 0);
1645 TYPE_IS_SIZETYPE (t) = 1;
1647 /* 1000 avoids problems with possible overflow and is certainly
1648 larger than any size value we'd want to be storing. */
1649 TYPE_MAX_VALUE (t) = build_int_2 (1000, 0);
1651 /* These two must be different nodes because of the caching done in
1652 size_int_wide. */
1653 sizetype = t;
1654 bitsizetype = copy_node (t);
1655 integer_type_node = 0;
1658 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1659 Also update the type of any standard type's sizes made so far. */
1661 void
1662 set_sizetype (type)
1663 tree type;
1665 int oprecision = TYPE_PRECISION (type);
1666 /* The *bitsizetype types use a precision that avoids overflows when
1667 calculating signed sizes / offsets in bits. However, when
1668 cross-compiling from a 32 bit to a 64 bit host, we are limited to 64 bit
1669 precision. */
1670 int precision = MIN (oprecision + BITS_PER_UNIT_LOG + 1,
1671 2 * HOST_BITS_PER_WIDE_INT);
1672 unsigned int i;
1673 tree t;
1675 if (sizetype_set)
1676 abort ();
1678 /* Make copies of nodes since we'll be setting TYPE_IS_SIZETYPE. */
1679 sizetype = copy_node (type);
1680 TYPE_DOMAIN (sizetype) = type;
1681 TYPE_IS_SIZETYPE (sizetype) = 1;
1682 bitsizetype = make_node (INTEGER_TYPE);
1683 TYPE_NAME (bitsizetype) = TYPE_NAME (type);
1684 TYPE_PRECISION (bitsizetype) = precision;
1685 TYPE_IS_SIZETYPE (bitsizetype) = 1;
1687 if (TREE_UNSIGNED (type))
1688 fixup_unsigned_type (bitsizetype);
1689 else
1690 fixup_signed_type (bitsizetype);
1692 layout_type (bitsizetype);
1694 if (TREE_UNSIGNED (type))
1696 usizetype = sizetype;
1697 ubitsizetype = bitsizetype;
1698 ssizetype = copy_node (make_signed_type (oprecision));
1699 sbitsizetype = copy_node (make_signed_type (precision));
1701 else
1703 ssizetype = sizetype;
1704 sbitsizetype = bitsizetype;
1705 usizetype = copy_node (make_unsigned_type (oprecision));
1706 ubitsizetype = copy_node (make_unsigned_type (precision));
1709 TYPE_NAME (bitsizetype) = get_identifier ("bit_size_type");
1711 /* Show is a sizetype, is a main type, and has no pointers to it. */
1712 for (i = 0; i < ARRAY_SIZE (sizetype_tab); i++)
1714 TYPE_IS_SIZETYPE (sizetype_tab[i]) = 1;
1715 TYPE_MAIN_VARIANT (sizetype_tab[i]) = sizetype_tab[i];
1716 TYPE_NEXT_VARIANT (sizetype_tab[i]) = 0;
1717 TYPE_POINTER_TO (sizetype_tab[i]) = 0;
1718 TYPE_REFERENCE_TO (sizetype_tab[i]) = 0;
1721 ggc_add_tree_root ((tree *) &sizetype_tab,
1722 sizeof sizetype_tab / sizeof (tree));
1724 /* Go down each of the types we already made and set the proper type
1725 for the sizes in them. */
1726 for (t = early_type_list; t != 0; t = TREE_CHAIN (t))
1728 if (TREE_CODE (TREE_VALUE (t)) != INTEGER_TYPE)
1729 abort ();
1731 TREE_TYPE (TYPE_SIZE (TREE_VALUE (t))) = bitsizetype;
1732 TREE_TYPE (TYPE_SIZE_UNIT (TREE_VALUE (t))) = sizetype;
1735 early_type_list = 0;
1736 sizetype_set = 1;
1739 /* Set the extreme values of TYPE based on its precision in bits,
1740 then lay it out. Used when make_signed_type won't do
1741 because the tree code is not INTEGER_TYPE.
1742 E.g. for Pascal, when the -fsigned-char option is given. */
1744 void
1745 fixup_signed_type (type)
1746 tree type;
1748 int precision = TYPE_PRECISION (type);
1750 TYPE_MIN_VALUE (type)
1751 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1752 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1753 (((HOST_WIDE_INT) (-1)
1754 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1755 ? precision - HOST_BITS_PER_WIDE_INT - 1
1756 : 0))));
1757 TYPE_MAX_VALUE (type)
1758 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1759 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1760 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1761 ? (((HOST_WIDE_INT) 1
1762 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1763 : 0));
1765 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1766 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1768 /* Lay out the type: set its alignment, size, etc. */
1769 layout_type (type);
1772 /* Set the extreme values of TYPE based on its precision in bits,
1773 then lay it out. This is used both in `make_unsigned_type'
1774 and for enumeral types. */
1776 void
1777 fixup_unsigned_type (type)
1778 tree type;
1780 int precision = TYPE_PRECISION (type);
1782 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1783 TYPE_MAX_VALUE (type)
1784 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1785 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1786 precision - HOST_BITS_PER_WIDE_INT > 0
1787 ? ((unsigned HOST_WIDE_INT) ~0
1788 >> (HOST_BITS_PER_WIDE_INT
1789 - (precision - HOST_BITS_PER_WIDE_INT)))
1790 : 0);
1791 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1792 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1794 /* Lay out the type: set its alignment, size, etc. */
1795 layout_type (type);
1798 /* Find the best machine mode to use when referencing a bit field of length
1799 BITSIZE bits starting at BITPOS.
1801 The underlying object is known to be aligned to a boundary of ALIGN bits.
1802 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1803 larger than LARGEST_MODE (usually SImode).
1805 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1806 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1807 mode meeting these conditions.
1809 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1810 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1811 all the conditions. */
1813 enum machine_mode
1814 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1815 int bitsize, bitpos;
1816 unsigned int align;
1817 enum machine_mode largest_mode;
1818 int volatilep;
1820 enum machine_mode mode;
1821 unsigned int unit = 0;
1823 /* Find the narrowest integer mode that contains the bit field. */
1824 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1825 mode = GET_MODE_WIDER_MODE (mode))
1827 unit = GET_MODE_BITSIZE (mode);
1828 if ((bitpos % unit) + bitsize <= unit)
1829 break;
1832 if (mode == VOIDmode
1833 /* It is tempting to omit the following line
1834 if STRICT_ALIGNMENT is true.
1835 But that is incorrect, since if the bitfield uses part of 3 bytes
1836 and we use a 4-byte mode, we could get a spurious segv
1837 if the extra 4th byte is past the end of memory.
1838 (Though at least one Unix compiler ignores this problem:
1839 that on the Sequent 386 machine. */
1840 || MIN (unit, BIGGEST_ALIGNMENT) > align
1841 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1842 return VOIDmode;
1844 if (SLOW_BYTE_ACCESS && ! volatilep)
1846 enum machine_mode wide_mode = VOIDmode, tmode;
1848 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1849 tmode = GET_MODE_WIDER_MODE (tmode))
1851 unit = GET_MODE_BITSIZE (tmode);
1852 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1853 && unit <= BITS_PER_WORD
1854 && unit <= MIN (align, BIGGEST_ALIGNMENT)
1855 && (largest_mode == VOIDmode
1856 || unit <= GET_MODE_BITSIZE (largest_mode)))
1857 wide_mode = tmode;
1860 if (wide_mode != VOIDmode)
1861 return wide_mode;
1864 return mode;
1867 /* This function is run once to initialize stor-layout.c. */
1869 void
1870 init_stor_layout_once ()
1872 ggc_add_tree_root (&pending_sizes, 1);