* Makefile.in (rtlanal.o): Depend on $(TM_P_H).
[official-gcc.git] / gcc / stor-layout.c
blob9de3233821af1abf41af30afd94cec0540ecdb1f
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 /* Union must be at least as aligned as any field requires. */
654 rli->record_align = MAX (rli->record_align, desired_align);
655 rli->unpadded_align = MAX (rli->unpadded_align, desired_align);
657 #ifdef PCC_BITFIELD_TYPE_MATTERS
658 /* On the m88000, a bit field of declare type `int' forces the
659 entire union to have `int' alignment. */
660 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
662 rli->record_align = MAX (rli->record_align,
663 TYPE_ALIGN (TREE_TYPE (field)));
664 rli->unpadded_align = MAX (rli->unpadded_align,
665 TYPE_ALIGN (TREE_TYPE (field)));
667 #endif
669 /* We assume the union's size will be a multiple of a byte so we don't
670 bother with BITPOS. */
671 if (TREE_CODE (rli->t) == UNION_TYPE)
672 rli->offset = size_binop (MAX_EXPR, rli->offset, DECL_SIZE_UNIT (field));
673 else if (TREE_CODE (rli->t) == QUAL_UNION_TYPE)
674 rli->offset = fold (build (COND_EXPR, sizetype,
675 DECL_QUALIFIER (field),
676 DECL_SIZE_UNIT (field), rli->offset));
679 /* RLI contains information about the layout of a RECORD_TYPE. FIELD
680 is a FIELD_DECL to be added after those fields already present in
681 T. (FIELD is not actually added to the TYPE_FIELDS list here;
682 callers that desire that behavior must manually perform that step.) */
684 void
685 place_field (rli, field)
686 record_layout_info rli;
687 tree field;
689 /* The alignment required for FIELD. */
690 unsigned int desired_align;
691 /* The alignment FIELD would have if we just dropped it into the
692 record as it presently stands. */
693 unsigned int known_align;
694 unsigned int actual_align;
695 unsigned int user_align;
696 /* The type of this field. */
697 tree type = TREE_TYPE (field);
699 if (TREE_CODE (field) == ERROR_MARK || TREE_CODE (type) == ERROR_MARK)
700 return;
702 /* If FIELD is static, then treat it like a separate variable, not
703 really like a structure field. If it is a FUNCTION_DECL, it's a
704 method. In both cases, all we do is lay out the decl, and we do
705 it *after* the record is laid out. */
706 if (TREE_CODE (field) == VAR_DECL)
708 rli->pending_statics = tree_cons (NULL_TREE, field,
709 rli->pending_statics);
710 return;
713 /* Enumerators and enum types which are local to this class need not
714 be laid out. Likewise for initialized constant fields. */
715 else if (TREE_CODE (field) != FIELD_DECL)
716 return;
718 /* Unions are laid out very differently than records, so split
719 that code off to another function. */
720 else if (TREE_CODE (rli->t) != RECORD_TYPE)
722 place_union_field (rli, field);
723 return;
726 /* Work out the known alignment so far. Note that A & (-A) is the
727 value of the least-significant bit in A that is one. */
728 if (! integer_zerop (rli->bitpos))
729 known_align = (tree_low_cst (rli->bitpos, 1)
730 & - tree_low_cst (rli->bitpos, 1));
731 else if (integer_zerop (rli->offset))
732 known_align = BIGGEST_ALIGNMENT;
733 else if (host_integerp (rli->offset, 1))
734 known_align = (BITS_PER_UNIT
735 * (tree_low_cst (rli->offset, 1)
736 & - tree_low_cst (rli->offset, 1)));
737 else
738 known_align = rli->offset_align;
740 /* Lay out the field so we know what alignment it needs. For a
741 packed field, use the alignment as specified, disregarding what
742 the type would want. */
743 desired_align = DECL_ALIGN (field);
744 user_align = DECL_USER_ALIGN (field);
745 layout_decl (field, known_align);
746 if (! DECL_PACKED (field))
748 desired_align = DECL_ALIGN (field);
749 user_align = DECL_USER_ALIGN (field);
752 /* Some targets (i.e. i386, VMS) limit struct field alignment
753 to a lower boundary than alignment of variables unless
754 it was overridden by attribute aligned. */
755 #ifdef BIGGEST_FIELD_ALIGNMENT
756 if (! user_align)
757 desired_align
758 = MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
759 #endif
761 #ifdef ADJUST_FIELD_ALIGN
762 desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
763 #endif
765 /* Record must have at least as much alignment as any field.
766 Otherwise, the alignment of the field within the record is
767 meaningless. */
768 #ifdef PCC_BITFIELD_TYPE_MATTERS
769 if (PCC_BITFIELD_TYPE_MATTERS && type != error_mark_node
770 && DECL_BIT_FIELD_TYPE (field)
771 && ! integer_zerop (TYPE_SIZE (type)))
773 /* For these machines, a zero-length field does not
774 affect the alignment of the structure as a whole.
775 It does, however, affect the alignment of the next field
776 within the structure. */
777 if (! integer_zerop (DECL_SIZE (field)))
778 rli->record_align = MAX (rli->record_align, desired_align);
779 else if (! DECL_PACKED (field))
780 desired_align = TYPE_ALIGN (type);
782 /* A named bit field of declared type `int'
783 forces the entire structure to have `int' alignment. */
784 if (DECL_NAME (field) != 0)
786 unsigned int type_align = TYPE_ALIGN (type);
788 if (maximum_field_alignment != 0)
789 type_align = MIN (type_align, maximum_field_alignment);
790 else if (DECL_PACKED (field))
791 type_align = MIN (type_align, BITS_PER_UNIT);
793 rli->record_align = MAX (rli->record_align, type_align);
794 rli->unpadded_align = MAX (rli->unpadded_align, DECL_ALIGN (field));
795 if (warn_packed)
796 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
799 else
800 #endif
802 rli->record_align = MAX (rli->record_align, desired_align);
803 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
804 rli->unpadded_align = MAX (rli->unpadded_align, DECL_ALIGN (field));
807 if (warn_packed && DECL_PACKED (field))
809 if (known_align > TYPE_ALIGN (type))
811 if (TYPE_ALIGN (type) > desired_align)
813 if (STRICT_ALIGNMENT)
814 warning_with_decl (field, "packed attribute causes inefficient alignment for `%s'");
815 else
816 warning_with_decl (field, "packed attribute is unnecessary for `%s'");
819 else
820 rli->packed_maybe_necessary = 1;
823 /* Does this field automatically have alignment it needs by virtue
824 of the fields that precede it and the record's own alignment? */
825 if (known_align < desired_align)
827 /* No, we need to skip space before this field.
828 Bump the cumulative size to multiple of field alignment. */
830 if (warn_padded)
831 warning_with_decl (field, "padding struct to align `%s'");
833 /* If the alignment is still within offset_align, just align
834 the bit position. */
835 if (desired_align < rli->offset_align)
836 rli->bitpos = round_up (rli->bitpos, desired_align);
837 else
839 /* First adjust OFFSET by the partial bits, then align. */
840 rli->offset
841 = size_binop (PLUS_EXPR, rli->offset,
842 convert (sizetype,
843 size_binop (CEIL_DIV_EXPR, rli->bitpos,
844 bitsize_unit_node)));
845 rli->bitpos = bitsize_zero_node;
847 rli->offset = round_up (rli->offset, desired_align / BITS_PER_UNIT);
850 if (! TREE_CONSTANT (rli->offset))
851 rli->offset_align = desired_align;
855 /* Handle compatibility with PCC. Note that if the record has any
856 variable-sized fields, we need not worry about compatibility. */
857 #ifdef PCC_BITFIELD_TYPE_MATTERS
858 if (PCC_BITFIELD_TYPE_MATTERS
859 && TREE_CODE (field) == FIELD_DECL
860 && type != error_mark_node
861 && DECL_BIT_FIELD (field)
862 && ! DECL_PACKED (field)
863 && maximum_field_alignment == 0
864 && ! integer_zerop (DECL_SIZE (field))
865 && host_integerp (DECL_SIZE (field), 1)
866 && host_integerp (rli->offset, 1)
867 && host_integerp (TYPE_SIZE (type), 1))
869 unsigned int type_align = TYPE_ALIGN (type);
870 tree dsize = DECL_SIZE (field);
871 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
872 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
873 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
875 /* A bit field may not span more units of alignment of its type
876 than its type itself. Advance to next boundary if necessary. */
877 if ((((offset * BITS_PER_UNIT + bit_offset + field_size +
878 type_align - 1)
879 / type_align)
880 - (offset * BITS_PER_UNIT + bit_offset) / type_align)
881 > tree_low_cst (TYPE_SIZE (type), 1) / type_align)
882 rli->bitpos = round_up (rli->bitpos, type_align);
884 #endif
886 #ifdef BITFIELD_NBYTES_LIMITED
887 if (BITFIELD_NBYTES_LIMITED
888 && TREE_CODE (field) == FIELD_DECL
889 && type != error_mark_node
890 && DECL_BIT_FIELD_TYPE (field)
891 && ! DECL_PACKED (field)
892 && ! integer_zerop (DECL_SIZE (field))
893 && host_integerp (DECL_SIZE (field), 1)
894 && host_integerp (rli->offset, 1)
895 && host_integerp (TYPE_SIZE (type), 1))
897 unsigned int type_align = TYPE_ALIGN (type);
898 tree dsize = DECL_SIZE (field);
899 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
900 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
901 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
903 if (maximum_field_alignment != 0)
904 type_align = MIN (type_align, maximum_field_alignment);
905 /* ??? This test is opposite the test in the containing if
906 statement, so this code is unreachable currently. */
907 else if (DECL_PACKED (field))
908 type_align = MIN (type_align, BITS_PER_UNIT);
910 /* A bit field may not span the unit of alignment of its type.
911 Advance to next boundary if necessary. */
912 /* ??? This code should match the code above for the
913 PCC_BITFIELD_TYPE_MATTERS case. */
914 if ((offset * BITS_PER_UNIT + bit_offset) / type_align
915 != ((offset * BITS_PER_UNIT + bit_offset + field_size - 1)
916 / type_align))
917 rli->bitpos = round_up (rli->bitpos, type_align);
919 #endif
921 /* Offset so far becomes the position of this field after normalizing. */
922 normalize_rli (rli);
923 DECL_FIELD_OFFSET (field) = rli->offset;
924 DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
925 SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
927 /* If this field ended up more aligned than we thought it would be (we
928 approximate this by seeing if its position changed), lay out the field
929 again; perhaps we can use an integral mode for it now. */
930 if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field)))
931 actual_align = (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
932 & - tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1));
933 else if (integer_zerop (DECL_FIELD_OFFSET (field)))
934 actual_align = BIGGEST_ALIGNMENT;
935 else if (host_integerp (DECL_FIELD_OFFSET (field), 1))
936 actual_align = (BITS_PER_UNIT
937 * (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
938 & - tree_low_cst (DECL_FIELD_OFFSET (field), 1)));
939 else
940 actual_align = DECL_OFFSET_ALIGN (field);
942 if (known_align != actual_align)
943 layout_decl (field, actual_align);
945 /* Now add size of this field to the size of the record. If the size is
946 not constant, treat the field as being a multiple of bytes and just
947 adjust the offset, resetting the bit position. Otherwise, apportion the
948 size amongst the bit position and offset. First handle the case of an
949 unspecified size, which can happen when we have an invalid nested struct
950 definition, such as struct j { struct j { int i; } }. The error message
951 is printed in finish_struct. */
952 if (DECL_SIZE (field) == 0)
953 /* Do nothing. */;
954 else if (TREE_CODE (DECL_SIZE_UNIT (field)) != INTEGER_CST
955 || TREE_CONSTANT_OVERFLOW (DECL_SIZE_UNIT (field)))
957 rli->offset
958 = size_binop (PLUS_EXPR, rli->offset,
959 convert (sizetype,
960 size_binop (CEIL_DIV_EXPR, rli->bitpos,
961 bitsize_unit_node)));
962 rli->offset
963 = size_binop (PLUS_EXPR, rli->offset, DECL_SIZE_UNIT (field));
964 rli->bitpos = bitsize_zero_node;
965 rli->offset_align = MIN (rli->offset_align, DECL_ALIGN (field));
967 else
969 rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
970 normalize_rli (rli);
974 /* Assuming that all the fields have been laid out, this function uses
975 RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
976 inidicated by RLI. */
978 static void
979 finalize_record_size (rli)
980 record_layout_info rli;
982 tree unpadded_size, unpadded_size_unit;
984 /* Now we want just byte and bit offsets, so set the offset alignment
985 to be a byte and then normalize. */
986 rli->offset_align = BITS_PER_UNIT;
987 normalize_rli (rli);
989 /* Determine the desired alignment. */
990 #ifdef ROUND_TYPE_ALIGN
991 TYPE_ALIGN (rli->t) = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t),
992 rli->record_align);
993 #else
994 TYPE_ALIGN (rli->t) = MAX (TYPE_ALIGN (rli->t), rli->record_align);
995 #endif
996 TYPE_USER_ALIGN (rli->t) = 0;
998 /* Compute the size so far. Be sure to allow for extra bits in the
999 size in bytes. We have guaranteed above that it will be no more
1000 than a single byte. */
1001 unpadded_size = rli_size_so_far (rli);
1002 unpadded_size_unit = rli_size_unit_so_far (rli);
1003 if (! integer_zerop (rli->bitpos))
1004 unpadded_size_unit
1005 = size_binop (PLUS_EXPR, unpadded_size_unit, size_one_node);
1007 /* Record the un-rounded size in the binfo node. But first we check
1008 the size of TYPE_BINFO to make sure that BINFO_SIZE is available. */
1009 if (TYPE_BINFO (rli->t) && TREE_VEC_LENGTH (TYPE_BINFO (rli->t)) > 6)
1011 TYPE_BINFO_SIZE (rli->t) = unpadded_size;
1012 TYPE_BINFO_SIZE_UNIT (rli->t) = unpadded_size_unit;
1015 /* Round the size up to be a multiple of the required alignment */
1016 #ifdef ROUND_TYPE_SIZE
1017 TYPE_SIZE (rli->t) = ROUND_TYPE_SIZE (rli->t, unpadded_size,
1018 TYPE_ALIGN (rli->t));
1019 TYPE_SIZE_UNIT (rli->t)
1020 = ROUND_TYPE_SIZE_UNIT (rli->t, unpadded_size_unit,
1021 TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
1022 #else
1023 TYPE_SIZE (rli->t) = round_up (unpadded_size, TYPE_ALIGN (rli->t));
1024 TYPE_SIZE_UNIT (rli->t) = round_up (unpadded_size_unit,
1025 TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
1026 #endif
1028 if (warn_padded && TREE_CONSTANT (unpadded_size)
1029 && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0)
1030 warning ("padding struct size to alignment boundary");
1032 if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
1033 && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
1034 && TREE_CONSTANT (unpadded_size))
1036 tree unpacked_size;
1038 #ifdef ROUND_TYPE_ALIGN
1039 rli->unpacked_align
1040 = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t), rli->unpacked_align);
1041 #else
1042 rli->unpacked_align = MAX (TYPE_ALIGN (rli->t), rli->unpacked_align);
1043 #endif
1045 #ifdef ROUND_TYPE_SIZE
1046 unpacked_size = ROUND_TYPE_SIZE (rli->t, TYPE_SIZE (rli->t),
1047 rli->unpacked_align);
1048 #else
1049 unpacked_size = round_up (TYPE_SIZE (rli->t), rli->unpacked_align);
1050 #endif
1052 if (simple_cst_equal (unpacked_size, TYPE_SIZE (rli->t)))
1054 TYPE_PACKED (rli->t) = 0;
1056 if (TYPE_NAME (rli->t))
1058 const char *name;
1060 if (TREE_CODE (TYPE_NAME (rli->t)) == IDENTIFIER_NODE)
1061 name = IDENTIFIER_POINTER (TYPE_NAME (rli->t));
1062 else
1063 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (rli->t)));
1065 if (STRICT_ALIGNMENT)
1066 warning ("packed attribute causes inefficient alignment for `%s'", name);
1067 else
1068 warning ("packed attribute is unnecessary for `%s'", name);
1070 else
1072 if (STRICT_ALIGNMENT)
1073 warning ("packed attribute causes inefficient alignment");
1074 else
1075 warning ("packed attribute is unnecessary");
1081 /* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE). */
1083 void
1084 compute_record_mode (type)
1085 tree type;
1087 tree field;
1088 enum machine_mode mode = VOIDmode;
1090 /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
1091 However, if possible, we use a mode that fits in a register
1092 instead, in order to allow for better optimization down the
1093 line. */
1094 TYPE_MODE (type) = BLKmode;
1096 if (! host_integerp (TYPE_SIZE (type), 1))
1097 return;
1099 /* A record which has any BLKmode members must itself be
1100 BLKmode; it can't go in a register. Unless the member is
1101 BLKmode only because it isn't aligned. */
1102 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1104 unsigned HOST_WIDE_INT bitpos;
1106 if (TREE_CODE (field) != FIELD_DECL)
1107 continue;
1109 if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK
1110 || (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1111 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1112 || ! host_integerp (bit_position (field), 1)
1113 || DECL_SIZE (field) == 0
1114 || ! host_integerp (DECL_SIZE (field), 1))
1115 return;
1117 bitpos = int_bit_position (field);
1119 /* Must be BLKmode if any field crosses a word boundary,
1120 since extract_bit_field can't handle that in registers. */
1121 if (bitpos / BITS_PER_WORD
1122 != ((tree_low_cst (DECL_SIZE (field), 1) + bitpos - 1)
1123 / BITS_PER_WORD)
1124 /* But there is no problem if the field is entire words. */
1125 && tree_low_cst (DECL_SIZE (field), 1) % BITS_PER_WORD != 0)
1126 return;
1128 /* If this field is the whole struct, remember its mode so
1129 that, say, we can put a double in a class into a DF
1130 register instead of forcing it to live in the stack. */
1131 if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
1132 mode = DECL_MODE (field);
1134 #ifdef MEMBER_TYPE_FORCES_BLK
1135 /* With some targets, eg. c4x, it is sub-optimal
1136 to access an aligned BLKmode structure as a scalar. */
1137 if (mode == VOIDmode && MEMBER_TYPE_FORCES_BLK (field))
1138 return;
1139 #endif /* MEMBER_TYPE_FORCES_BLK */
1142 /* If we only have one real field; use its mode. This only applies to
1143 RECORD_TYPE. This does not apply to unions. */
1144 if (TREE_CODE (type) == RECORD_TYPE && mode != VOIDmode)
1145 TYPE_MODE (type) = mode;
1146 else
1147 TYPE_MODE (type) = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1149 /* If structure's known alignment is less than what the scalar
1150 mode would need, and it matters, then stick with BLKmode. */
1151 if (TYPE_MODE (type) != BLKmode
1152 && STRICT_ALIGNMENT
1153 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1154 || TYPE_ALIGN (type) >= GET_MODE_ALIGNMENT (TYPE_MODE (type))))
1156 /* If this is the only reason this type is BLKmode, then
1157 don't force containing types to be BLKmode. */
1158 TYPE_NO_FORCE_BLK (type) = 1;
1159 TYPE_MODE (type) = BLKmode;
1163 /* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
1164 out. */
1166 static void
1167 finalize_type_size (type)
1168 tree type;
1170 /* Normally, use the alignment corresponding to the mode chosen.
1171 However, where strict alignment is not required, avoid
1172 over-aligning structures, since most compilers do not do this
1173 alignment. */
1175 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1176 && (STRICT_ALIGNMENT
1177 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1178 && TREE_CODE (type) != QUAL_UNION_TYPE
1179 && TREE_CODE (type) != ARRAY_TYPE)))
1181 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1182 TYPE_USER_ALIGN (type) = 0;
1185 /* Do machine-dependent extra alignment. */
1186 #ifdef ROUND_TYPE_ALIGN
1187 TYPE_ALIGN (type)
1188 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1189 #endif
1191 /* If we failed to find a simple way to calculate the unit size
1192 of the type, find it by division. */
1193 if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1194 /* TYPE_SIZE (type) is computed in bitsizetype. After the division, the
1195 result will fit in sizetype. We will get more efficient code using
1196 sizetype, so we force a conversion. */
1197 TYPE_SIZE_UNIT (type)
1198 = convert (sizetype,
1199 size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1200 bitsize_unit_node));
1202 if (TYPE_SIZE (type) != 0)
1204 #ifdef ROUND_TYPE_SIZE
1205 TYPE_SIZE (type)
1206 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1207 TYPE_SIZE_UNIT (type)
1208 = ROUND_TYPE_SIZE_UNIT (type, TYPE_SIZE_UNIT (type),
1209 TYPE_ALIGN (type) / BITS_PER_UNIT);
1210 #else
1211 TYPE_SIZE (type) = round_up (TYPE_SIZE (type), TYPE_ALIGN (type));
1212 TYPE_SIZE_UNIT (type)
1213 = round_up (TYPE_SIZE_UNIT (type), TYPE_ALIGN (type) / BITS_PER_UNIT);
1214 #endif
1217 /* Evaluate nonconstant sizes only once, either now or as soon as safe. */
1218 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1219 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1220 if (TYPE_SIZE_UNIT (type) != 0
1221 && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1222 TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1224 /* Also layout any other variants of the type. */
1225 if (TYPE_NEXT_VARIANT (type)
1226 || type != TYPE_MAIN_VARIANT (type))
1228 tree variant;
1229 /* Record layout info of this variant. */
1230 tree size = TYPE_SIZE (type);
1231 tree size_unit = TYPE_SIZE_UNIT (type);
1232 unsigned int align = TYPE_ALIGN (type);
1233 unsigned int user_align = TYPE_USER_ALIGN (type);
1234 enum machine_mode mode = TYPE_MODE (type);
1236 /* Copy it into all variants. */
1237 for (variant = TYPE_MAIN_VARIANT (type);
1238 variant != 0;
1239 variant = TYPE_NEXT_VARIANT (variant))
1241 TYPE_SIZE (variant) = size;
1242 TYPE_SIZE_UNIT (variant) = size_unit;
1243 TYPE_ALIGN (variant) = align;
1244 TYPE_USER_ALIGN (variant) = user_align;
1245 TYPE_MODE (variant) = mode;
1250 /* Do all of the work required to layout the type indicated by RLI,
1251 once the fields have been laid out. This function will call `free'
1252 for RLI. */
1254 void
1255 finish_record_layout (rli)
1256 record_layout_info rli;
1258 /* Compute the final size. */
1259 finalize_record_size (rli);
1261 /* Compute the TYPE_MODE for the record. */
1262 compute_record_mode (rli->t);
1264 /* Perform any last tweaks to the TYPE_SIZE, etc. */
1265 finalize_type_size (rli->t);
1267 /* Lay out any static members. This is done now because their type
1268 may use the record's type. */
1269 while (rli->pending_statics)
1271 layout_decl (TREE_VALUE (rli->pending_statics), 0);
1272 rli->pending_statics = TREE_CHAIN (rli->pending_statics);
1275 /* Clean up. */
1276 free (rli);
1279 /* Calculate the mode, size, and alignment for TYPE.
1280 For an array type, calculate the element separation as well.
1281 Record TYPE on the chain of permanent or temporary types
1282 so that dbxout will find out about it.
1284 TYPE_SIZE of a type is nonzero if the type has been laid out already.
1285 layout_type does nothing on such a type.
1287 If the type is incomplete, its TYPE_SIZE remains zero. */
1289 void
1290 layout_type (type)
1291 tree type;
1293 if (type == 0)
1294 abort ();
1296 /* Do nothing if type has been laid out before. */
1297 if (TYPE_SIZE (type))
1298 return;
1300 switch (TREE_CODE (type))
1302 case LANG_TYPE:
1303 /* This kind of type is the responsibility
1304 of the language-specific code. */
1305 abort ();
1307 case BOOLEAN_TYPE: /* Used for Java, Pascal, and Chill. */
1308 if (TYPE_PRECISION (type) == 0)
1309 TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
1311 /* ... fall through ... */
1313 case INTEGER_TYPE:
1314 case ENUMERAL_TYPE:
1315 case CHAR_TYPE:
1316 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
1317 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
1318 TREE_UNSIGNED (type) = 1;
1320 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
1321 MODE_INT);
1322 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1323 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1324 break;
1326 case REAL_TYPE:
1327 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
1328 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1329 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1330 break;
1332 case COMPLEX_TYPE:
1333 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
1334 TYPE_MODE (type)
1335 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
1336 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
1337 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
1339 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1340 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1341 break;
1343 case VECTOR_TYPE:
1345 tree subtype;
1347 subtype = TREE_TYPE (type);
1348 TREE_UNSIGNED (type) = TREE_UNSIGNED (subtype);
1349 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1350 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1352 break;
1354 case VOID_TYPE:
1355 /* This is an incomplete type and so doesn't have a size. */
1356 TYPE_ALIGN (type) = 1;
1357 TYPE_USER_ALIGN (type) = 0;
1358 TYPE_MODE (type) = VOIDmode;
1359 break;
1361 case OFFSET_TYPE:
1362 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
1363 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
1364 /* A pointer might be MODE_PARTIAL_INT,
1365 but ptrdiff_t must be integral. */
1366 TYPE_MODE (type) = mode_for_size (POINTER_SIZE, MODE_INT, 0);
1367 break;
1369 case FUNCTION_TYPE:
1370 case METHOD_TYPE:
1371 TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
1372 TYPE_SIZE (type) = bitsize_int (2 * POINTER_SIZE);
1373 TYPE_SIZE_UNIT (type) = size_int ((2 * POINTER_SIZE) / BITS_PER_UNIT);
1374 break;
1376 case POINTER_TYPE:
1377 case REFERENCE_TYPE:
1379 int nbits = ((TREE_CODE (type) == REFERENCE_TYPE
1380 && reference_types_internal)
1381 ? GET_MODE_BITSIZE (Pmode) : POINTER_SIZE);
1383 TYPE_MODE (type) = nbits == POINTER_SIZE ? ptr_mode : Pmode;
1384 TYPE_SIZE (type) = bitsize_int (nbits);
1385 TYPE_SIZE_UNIT (type) = size_int (nbits / BITS_PER_UNIT);
1386 TREE_UNSIGNED (type) = 1;
1387 TYPE_PRECISION (type) = nbits;
1389 break;
1391 case ARRAY_TYPE:
1393 tree index = TYPE_DOMAIN (type);
1394 tree element = TREE_TYPE (type);
1396 build_pointer_type (element);
1398 /* We need to know both bounds in order to compute the size. */
1399 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
1400 && TYPE_SIZE (element))
1402 tree ub = TYPE_MAX_VALUE (index);
1403 tree lb = TYPE_MIN_VALUE (index);
1404 tree length;
1405 tree element_size;
1407 /* The initial subtraction should happen in the original type so
1408 that (possible) negative values are handled appropriately. */
1409 length = size_binop (PLUS_EXPR, size_one_node,
1410 convert (sizetype,
1411 fold (build (MINUS_EXPR,
1412 TREE_TYPE (lb),
1413 ub, lb))));
1415 /* Special handling for arrays of bits (for Chill). */
1416 element_size = TYPE_SIZE (element);
1417 if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element)
1418 && (integer_zerop (TYPE_MAX_VALUE (element))
1419 || integer_onep (TYPE_MAX_VALUE (element)))
1420 && host_integerp (TYPE_MIN_VALUE (element), 1))
1422 HOST_WIDE_INT maxvalue
1423 = tree_low_cst (TYPE_MAX_VALUE (element), 1);
1424 HOST_WIDE_INT minvalue
1425 = tree_low_cst (TYPE_MIN_VALUE (element), 1);
1427 if (maxvalue - minvalue == 1
1428 && (maxvalue == 1 || maxvalue == 0))
1429 element_size = integer_one_node;
1432 TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size,
1433 convert (bitsizetype, length));
1435 /* If we know the size of the element, calculate the total
1436 size directly, rather than do some division thing below.
1437 This optimization helps Fortran assumed-size arrays
1438 (where the size of the array is determined at runtime)
1439 substantially.
1440 Note that we can't do this in the case where the size of
1441 the elements is one bit since TYPE_SIZE_UNIT cannot be
1442 set correctly in that case. */
1443 if (TYPE_SIZE_UNIT (element) != 0 && ! integer_onep (element_size))
1444 TYPE_SIZE_UNIT (type)
1445 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
1448 /* Now round the alignment and size,
1449 using machine-dependent criteria if any. */
1451 #ifdef ROUND_TYPE_ALIGN
1452 TYPE_ALIGN (type)
1453 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
1454 #else
1455 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
1456 #endif
1458 #ifdef ROUND_TYPE_SIZE
1459 if (TYPE_SIZE (type) != 0)
1461 tree tmp
1462 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1464 /* If the rounding changed the size of the type, remove any
1465 pre-calculated TYPE_SIZE_UNIT. */
1466 if (simple_cst_equal (TYPE_SIZE (type), tmp) != 1)
1467 TYPE_SIZE_UNIT (type) = NULL;
1469 TYPE_SIZE (type) = tmp;
1471 #endif
1473 TYPE_MODE (type) = BLKmode;
1474 if (TYPE_SIZE (type) != 0
1475 #ifdef MEMBER_TYPE_FORCES_BLK
1476 && ! MEMBER_TYPE_FORCES_BLK (type)
1477 #endif
1478 /* BLKmode elements force BLKmode aggregate;
1479 else extract/store fields may lose. */
1480 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
1481 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
1483 TYPE_MODE (type)
1484 = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1486 if (TYPE_MODE (type) != BLKmode
1487 && STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
1488 && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (TYPE_MODE (type))
1489 && TYPE_MODE (type) != BLKmode)
1491 TYPE_NO_FORCE_BLK (type) = 1;
1492 TYPE_MODE (type) = BLKmode;
1495 break;
1498 case RECORD_TYPE:
1499 case UNION_TYPE:
1500 case QUAL_UNION_TYPE:
1502 tree field;
1503 record_layout_info rli;
1505 /* Initialize the layout information. */
1506 rli = start_record_layout (type);
1508 /* If this is a QUAL_UNION_TYPE, we want to process the fields
1509 in the reverse order in building the COND_EXPR that denotes
1510 its size. We reverse them again later. */
1511 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1512 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1514 /* Place all the fields. */
1515 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1516 place_field (rli, field);
1518 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1519 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1521 if (lang_adjust_rli)
1522 (*lang_adjust_rli) (rli);
1524 /* Finish laying out the record. */
1525 finish_record_layout (rli);
1527 break;
1529 case SET_TYPE: /* Used by Chill and Pascal. */
1530 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1531 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1532 abort();
1533 else
1535 #ifndef SET_WORD_SIZE
1536 #define SET_WORD_SIZE BITS_PER_WORD
1537 #endif
1538 unsigned int alignment
1539 = set_alignment ? set_alignment : SET_WORD_SIZE;
1540 int size_in_bits
1541 = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1542 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
1543 int rounded_size
1544 = ((size_in_bits + alignment - 1) / alignment) * alignment;
1546 if (rounded_size > (int) alignment)
1547 TYPE_MODE (type) = BLKmode;
1548 else
1549 TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1551 TYPE_SIZE (type) = bitsize_int (rounded_size);
1552 TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1553 TYPE_ALIGN (type) = alignment;
1554 TYPE_USER_ALIGN (type) = 0;
1555 TYPE_PRECISION (type) = size_in_bits;
1557 break;
1559 case FILE_TYPE:
1560 /* The size may vary in different languages, so the language front end
1561 should fill in the size. */
1562 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1563 TYPE_USER_ALIGN (type) = 0;
1564 TYPE_MODE (type) = BLKmode;
1565 break;
1567 default:
1568 abort ();
1571 /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE. For
1572 records and unions, finish_record_layout already called this
1573 function. */
1574 if (TREE_CODE (type) != RECORD_TYPE
1575 && TREE_CODE (type) != UNION_TYPE
1576 && TREE_CODE (type) != QUAL_UNION_TYPE)
1577 finalize_type_size (type);
1579 /* If this type is created before sizetype has been permanently set,
1580 record it so set_sizetype can fix it up. */
1581 if (! sizetype_set)
1582 early_type_list = tree_cons (NULL_TREE, type, early_type_list);
1584 /* If an alias set has been set for this aggregate when it was incomplete,
1585 force it into alias set 0.
1586 This is too conservative, but we cannot call record_component_aliases
1587 here because some frontends still change the aggregates after
1588 layout_type. */
1589 if (AGGREGATE_TYPE_P (type) && TYPE_ALIAS_SET_KNOWN_P (type))
1590 TYPE_ALIAS_SET (type) = 0;
1593 /* Create and return a type for signed integers of PRECISION bits. */
1595 tree
1596 make_signed_type (precision)
1597 int precision;
1599 tree type = make_node (INTEGER_TYPE);
1601 TYPE_PRECISION (type) = precision;
1603 fixup_signed_type (type);
1604 return type;
1607 /* Create and return a type for unsigned integers of PRECISION bits. */
1609 tree
1610 make_unsigned_type (precision)
1611 int precision;
1613 tree type = make_node (INTEGER_TYPE);
1615 TYPE_PRECISION (type) = precision;
1617 fixup_unsigned_type (type);
1618 return type;
1621 /* Initialize sizetype and bitsizetype to a reasonable and temporary
1622 value to enable integer types to be created. */
1624 void
1625 initialize_sizetypes ()
1627 tree t = make_node (INTEGER_TYPE);
1629 /* Set this so we do something reasonable for the build_int_2 calls
1630 below. */
1631 integer_type_node = t;
1633 TYPE_MODE (t) = SImode;
1634 TYPE_ALIGN (t) = GET_MODE_ALIGNMENT (SImode);
1635 TYPE_USER_ALIGN (t) = 0;
1636 TYPE_SIZE (t) = build_int_2 (GET_MODE_BITSIZE (SImode), 0);
1637 TYPE_SIZE_UNIT (t) = build_int_2 (GET_MODE_SIZE (SImode), 0);
1638 TREE_UNSIGNED (t) = 1;
1639 TYPE_PRECISION (t) = GET_MODE_BITSIZE (SImode);
1640 TYPE_MIN_VALUE (t) = build_int_2 (0, 0);
1641 TYPE_IS_SIZETYPE (t) = 1;
1643 /* 1000 avoids problems with possible overflow and is certainly
1644 larger than any size value we'd want to be storing. */
1645 TYPE_MAX_VALUE (t) = build_int_2 (1000, 0);
1647 /* These two must be different nodes because of the caching done in
1648 size_int_wide. */
1649 sizetype = t;
1650 bitsizetype = copy_node (t);
1651 integer_type_node = 0;
1654 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1655 Also update the type of any standard type's sizes made so far. */
1657 void
1658 set_sizetype (type)
1659 tree type;
1661 int oprecision = TYPE_PRECISION (type);
1662 /* The *bitsizetype types use a precision that avoids overflows when
1663 calculating signed sizes / offsets in bits. However, when
1664 cross-compiling from a 32 bit to a 64 bit host, we are limited to 64 bit
1665 precision. */
1666 int precision = MIN (oprecision + BITS_PER_UNIT_LOG + 1,
1667 2 * HOST_BITS_PER_WIDE_INT);
1668 unsigned int i;
1669 tree t;
1671 if (sizetype_set)
1672 abort ();
1674 /* Make copies of nodes since we'll be setting TYPE_IS_SIZETYPE. */
1675 sizetype = copy_node (type);
1676 TYPE_DOMAIN (sizetype) = type;
1677 TYPE_IS_SIZETYPE (sizetype) = 1;
1678 bitsizetype = make_node (INTEGER_TYPE);
1679 TYPE_NAME (bitsizetype) = TYPE_NAME (type);
1680 TYPE_PRECISION (bitsizetype) = precision;
1681 TYPE_IS_SIZETYPE (bitsizetype) = 1;
1683 if (TREE_UNSIGNED (type))
1684 fixup_unsigned_type (bitsizetype);
1685 else
1686 fixup_signed_type (bitsizetype);
1688 layout_type (bitsizetype);
1690 if (TREE_UNSIGNED (type))
1692 usizetype = sizetype;
1693 ubitsizetype = bitsizetype;
1694 ssizetype = copy_node (make_signed_type (oprecision));
1695 sbitsizetype = copy_node (make_signed_type (precision));
1697 else
1699 ssizetype = sizetype;
1700 sbitsizetype = bitsizetype;
1701 usizetype = copy_node (make_unsigned_type (oprecision));
1702 ubitsizetype = copy_node (make_unsigned_type (precision));
1705 TYPE_NAME (bitsizetype) = get_identifier ("bit_size_type");
1707 /* Show is a sizetype, is a main type, and has no pointers to it. */
1708 for (i = 0; i < ARRAY_SIZE (sizetype_tab); i++)
1710 TYPE_IS_SIZETYPE (sizetype_tab[i]) = 1;
1711 TYPE_MAIN_VARIANT (sizetype_tab[i]) = sizetype_tab[i];
1712 TYPE_NEXT_VARIANT (sizetype_tab[i]) = 0;
1713 TYPE_POINTER_TO (sizetype_tab[i]) = 0;
1714 TYPE_REFERENCE_TO (sizetype_tab[i]) = 0;
1717 ggc_add_tree_root ((tree *) &sizetype_tab,
1718 sizeof sizetype_tab / sizeof (tree));
1720 /* Go down each of the types we already made and set the proper type
1721 for the sizes in them. */
1722 for (t = early_type_list; t != 0; t = TREE_CHAIN (t))
1724 if (TREE_CODE (TREE_VALUE (t)) != INTEGER_TYPE)
1725 abort ();
1727 TREE_TYPE (TYPE_SIZE (TREE_VALUE (t))) = bitsizetype;
1728 TREE_TYPE (TYPE_SIZE_UNIT (TREE_VALUE (t))) = sizetype;
1731 early_type_list = 0;
1732 sizetype_set = 1;
1735 /* Set the extreme values of TYPE based on its precision in bits,
1736 then lay it out. Used when make_signed_type won't do
1737 because the tree code is not INTEGER_TYPE.
1738 E.g. for Pascal, when the -fsigned-char option is given. */
1740 void
1741 fixup_signed_type (type)
1742 tree type;
1744 int precision = TYPE_PRECISION (type);
1746 TYPE_MIN_VALUE (type)
1747 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1748 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1749 (((HOST_WIDE_INT) (-1)
1750 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1751 ? precision - HOST_BITS_PER_WIDE_INT - 1
1752 : 0))));
1753 TYPE_MAX_VALUE (type)
1754 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1755 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1756 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1757 ? (((HOST_WIDE_INT) 1
1758 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1759 : 0));
1761 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1762 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1764 /* Lay out the type: set its alignment, size, etc. */
1765 layout_type (type);
1768 /* Set the extreme values of TYPE based on its precision in bits,
1769 then lay it out. This is used both in `make_unsigned_type'
1770 and for enumeral types. */
1772 void
1773 fixup_unsigned_type (type)
1774 tree type;
1776 int precision = TYPE_PRECISION (type);
1778 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1779 TYPE_MAX_VALUE (type)
1780 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1781 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1782 precision - HOST_BITS_PER_WIDE_INT > 0
1783 ? ((unsigned HOST_WIDE_INT) ~0
1784 >> (HOST_BITS_PER_WIDE_INT
1785 - (precision - HOST_BITS_PER_WIDE_INT)))
1786 : 0);
1787 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1788 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1790 /* Lay out the type: set its alignment, size, etc. */
1791 layout_type (type);
1794 /* Find the best machine mode to use when referencing a bit field of length
1795 BITSIZE bits starting at BITPOS.
1797 The underlying object is known to be aligned to a boundary of ALIGN bits.
1798 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1799 larger than LARGEST_MODE (usually SImode).
1801 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1802 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1803 mode meeting these conditions.
1805 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1806 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1807 all the conditions. */
1809 enum machine_mode
1810 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1811 int bitsize, bitpos;
1812 unsigned int align;
1813 enum machine_mode largest_mode;
1814 int volatilep;
1816 enum machine_mode mode;
1817 unsigned int unit = 0;
1819 /* Find the narrowest integer mode that contains the bit field. */
1820 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1821 mode = GET_MODE_WIDER_MODE (mode))
1823 unit = GET_MODE_BITSIZE (mode);
1824 if ((bitpos % unit) + bitsize <= unit)
1825 break;
1828 if (mode == VOIDmode
1829 /* It is tempting to omit the following line
1830 if STRICT_ALIGNMENT is true.
1831 But that is incorrect, since if the bitfield uses part of 3 bytes
1832 and we use a 4-byte mode, we could get a spurious segv
1833 if the extra 4th byte is past the end of memory.
1834 (Though at least one Unix compiler ignores this problem:
1835 that on the Sequent 386 machine. */
1836 || MIN (unit, BIGGEST_ALIGNMENT) > align
1837 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1838 return VOIDmode;
1840 if (SLOW_BYTE_ACCESS && ! volatilep)
1842 enum machine_mode wide_mode = VOIDmode, tmode;
1844 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1845 tmode = GET_MODE_WIDER_MODE (tmode))
1847 unit = GET_MODE_BITSIZE (tmode);
1848 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1849 && unit <= BITS_PER_WORD
1850 && unit <= MIN (align, BIGGEST_ALIGNMENT)
1851 && (largest_mode == VOIDmode
1852 || unit <= GET_MODE_BITSIZE (largest_mode)))
1853 wide_mode = tmode;
1856 if (wide_mode != VOIDmode)
1857 return wide_mode;
1860 return mode;
1863 /* Return the alignment of MODE. This will be bounded by 1 and
1864 BIGGEST_ALIGNMENT. */
1866 unsigned int
1867 get_mode_alignment (mode)
1868 enum machine_mode mode;
1870 unsigned int alignment = GET_MODE_UNIT_SIZE (mode);
1872 /* Extract the LSB of the size. */
1873 alignment = alignment & -alignment;
1874 alignment *= BITS_PER_UNIT;
1876 alignment = MIN (BIGGEST_ALIGNMENT, MAX (1, alignment));
1877 return alignment;
1880 /* This function is run once to initialize stor-layout.c. */
1882 void
1883 init_stor_layout_once ()
1885 ggc_add_tree_root (&pending_sizes, 1);