2004-08-23 Eric Christopher <echristo@redhat.com>
[official-gcc.git] / gcc / stor-layout.c
blobacbba5535475aa916e903afcf993c21fa2b4bb33
1 /* C-compiler utilities for types and variables storage layout
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1996, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 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 "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "flags.h"
31 #include "function.h"
32 #include "expr.h"
33 #include "toplev.h"
34 #include "ggc.h"
35 #include "target.h"
36 #include "langhooks.h"
37 #include "regs.h"
38 #include "params.h"
40 /* Set to one when set_sizetype has been called. */
41 static int sizetype_set;
43 /* List of types created before set_sizetype has been called. We do not
44 make this a GGC root since we want these nodes to be reclaimed. */
45 static tree early_type_list;
47 /* Data type for the expressions representing sizes of data types.
48 It is the first integer type laid out. */
49 tree sizetype_tab[(int) TYPE_KIND_LAST];
51 /* If nonzero, this is an upper limit on alignment of structure fields.
52 The value is measured in bits. */
53 unsigned int maximum_field_alignment;
55 /* If nonzero, the alignment of a bitstring or (power-)set value, in bits.
56 May be overridden by front-ends. */
57 unsigned int set_alignment = 0;
59 /* Nonzero if all REFERENCE_TYPEs are internal and hence should be
60 allocated in Pmode, not ptr_mode. Set only by internal_reference_types
61 called only by a front end. */
62 static int reference_types_internal = 0;
64 static void finalize_record_size (record_layout_info);
65 static void finalize_type_size (tree);
66 static void place_union_field (record_layout_info, tree);
67 #if defined (PCC_BITFIELD_TYPE_MATTERS) || defined (BITFIELD_NBYTES_LIMITED)
68 static int excess_unit_span (HOST_WIDE_INT, HOST_WIDE_INT, HOST_WIDE_INT,
69 HOST_WIDE_INT, tree);
70 #endif
71 extern void debug_rli (record_layout_info);
73 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
75 static GTY(()) tree pending_sizes;
77 /* Show that REFERENCE_TYPES are internal and should be Pmode. Called only
78 by front end. */
80 void
81 internal_reference_types (void)
83 reference_types_internal = 1;
86 /* Get a list of all the objects put on the pending sizes list. */
88 tree
89 get_pending_sizes (void)
91 tree chain = pending_sizes;
93 pending_sizes = 0;
94 return chain;
97 /* Add EXPR to the pending sizes list. */
99 void
100 put_pending_size (tree expr)
102 /* Strip any simple arithmetic from EXPR to see if it has an underlying
103 SAVE_EXPR. */
104 expr = skip_simple_arithmetic (expr);
106 if (TREE_CODE (expr) == SAVE_EXPR)
107 pending_sizes = tree_cons (NULL_TREE, expr, pending_sizes);
110 /* Put a chain of objects into the pending sizes list, which must be
111 empty. */
113 void
114 put_pending_sizes (tree chain)
116 if (pending_sizes)
117 abort ();
119 pending_sizes = chain;
122 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
123 to serve as the actual size-expression for a type or decl. */
125 tree
126 variable_size (tree size)
128 tree save;
130 /* If the language-processor is to take responsibility for variable-sized
131 items (e.g., languages which have elaboration procedures like Ada),
132 just return SIZE unchanged. Likewise for self-referential sizes and
133 constant sizes. */
134 if (TREE_CONSTANT (size)
135 || lang_hooks.decls.global_bindings_p () < 0
136 || CONTAINS_PLACEHOLDER_P (size))
137 return size;
139 size = save_expr (size);
141 /* If an array with a variable number of elements is declared, and
142 the elements require destruction, we will emit a cleanup for the
143 array. That cleanup is run both on normal exit from the block
144 and in the exception-handler for the block. Normally, when code
145 is used in both ordinary code and in an exception handler it is
146 `unsaved', i.e., all SAVE_EXPRs are recalculated. However, we do
147 not wish to do that here; the array-size is the same in both
148 places. */
149 save = skip_simple_arithmetic (size);
151 if (cfun && cfun->x_dont_save_pending_sizes_p)
152 /* The front-end doesn't want us to keep a list of the expressions
153 that determine sizes for variable size objects. Trust it. */
154 return size;
156 if (lang_hooks.decls.global_bindings_p ())
158 if (TREE_CONSTANT (size))
159 error ("type size can't be explicitly evaluated");
160 else
161 error ("variable-size type declared outside of any function");
163 return size_one_node;
166 put_pending_size (save);
168 return size;
171 #ifndef MAX_FIXED_MODE_SIZE
172 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
173 #endif
175 /* Return the machine mode to use for a nonscalar of SIZE bits. The
176 mode must be in class CLASS, and have exactly that many value bits;
177 it may have padding as well. If LIMIT is nonzero, modes of wider
178 than MAX_FIXED_MODE_SIZE will not be used. */
180 enum machine_mode
181 mode_for_size (unsigned int size, enum mode_class class, int limit)
183 enum machine_mode mode;
185 if (limit && size > MAX_FIXED_MODE_SIZE)
186 return BLKmode;
188 /* Get the first mode which has this size, in the specified class. */
189 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
190 mode = GET_MODE_WIDER_MODE (mode))
191 if (GET_MODE_PRECISION (mode) == size)
192 return mode;
194 return BLKmode;
197 /* Similar, except passed a tree node. */
199 enum machine_mode
200 mode_for_size_tree (tree size, enum mode_class class, int limit)
202 if (TREE_CODE (size) != INTEGER_CST
203 || TREE_OVERFLOW (size)
204 /* What we really want to say here is that the size can fit in a
205 host integer, but we know there's no way we'd find a mode for
206 this many bits, so there's no point in doing the precise test. */
207 || compare_tree_int (size, 1000) > 0)
208 return BLKmode;
209 else
210 return mode_for_size (tree_low_cst (size, 1), class, limit);
213 /* Similar, but never return BLKmode; return the narrowest mode that
214 contains at least the requested number of value bits. */
216 enum machine_mode
217 smallest_mode_for_size (unsigned int size, enum mode_class class)
219 enum machine_mode mode;
221 /* Get the first mode which has at least this size, in the
222 specified class. */
223 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
224 mode = GET_MODE_WIDER_MODE (mode))
225 if (GET_MODE_PRECISION (mode) >= size)
226 return mode;
228 abort ();
231 /* Find an integer mode of the exact same size, or BLKmode on failure. */
233 enum machine_mode
234 int_mode_for_mode (enum machine_mode mode)
236 switch (GET_MODE_CLASS (mode))
238 case MODE_INT:
239 case MODE_PARTIAL_INT:
240 break;
242 case MODE_COMPLEX_INT:
243 case MODE_COMPLEX_FLOAT:
244 case MODE_FLOAT:
245 case MODE_VECTOR_INT:
246 case MODE_VECTOR_FLOAT:
247 mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
248 break;
250 case MODE_RANDOM:
251 if (mode == BLKmode)
252 break;
254 /* ... fall through ... */
256 case MODE_CC:
257 default:
258 abort ();
261 return mode;
264 /* Return the alignment of MODE. This will be bounded by 1 and
265 BIGGEST_ALIGNMENT. */
267 unsigned int
268 get_mode_alignment (enum machine_mode mode)
270 return MIN (BIGGEST_ALIGNMENT, MAX (1, mode_base_align[mode]*BITS_PER_UNIT));
274 /* Subroutine of layout_decl: Force alignment required for the data type.
275 But if the decl itself wants greater alignment, don't override that. */
277 static inline void
278 do_type_align (tree type, tree decl)
280 if (TYPE_ALIGN (type) > DECL_ALIGN (decl))
282 DECL_ALIGN (decl) = TYPE_ALIGN (type);
283 if (TREE_CODE (decl) == FIELD_DECL)
284 DECL_USER_ALIGN (decl) = TYPE_USER_ALIGN (type);
288 /* Set the size, mode and alignment of a ..._DECL node.
289 TYPE_DECL does need this for C++.
290 Note that LABEL_DECL and CONST_DECL nodes do not need this,
291 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
292 Don't call layout_decl for them.
294 KNOWN_ALIGN is the amount of alignment we can assume this
295 decl has with no special effort. It is relevant only for FIELD_DECLs
296 and depends on the previous fields.
297 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
298 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
299 the record will be aligned to suit. */
301 void
302 layout_decl (tree decl, unsigned int known_align)
304 tree type = TREE_TYPE (decl);
305 enum tree_code code = TREE_CODE (decl);
306 rtx rtl = NULL_RTX;
308 if (code == CONST_DECL)
309 return;
310 else if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
311 && code != TYPE_DECL && code != FIELD_DECL)
312 abort ();
314 rtl = DECL_RTL_IF_SET (decl);
316 if (type == error_mark_node)
317 type = void_type_node;
319 /* Usually the size and mode come from the data type without change,
320 however, the front-end may set the explicit width of the field, so its
321 size may not be the same as the size of its type. This happens with
322 bitfields, of course (an `int' bitfield may be only 2 bits, say), but it
323 also happens with other fields. For example, the C++ front-end creates
324 zero-sized fields corresponding to empty base classes, and depends on
325 layout_type setting DECL_FIELD_BITPOS correctly for the field. Set the
326 size in bytes from the size in bits. If we have already set the mode,
327 don't set it again since we can be called twice for FIELD_DECLs. */
329 DECL_UNSIGNED (decl) = TYPE_UNSIGNED (type);
330 if (DECL_MODE (decl) == VOIDmode)
331 DECL_MODE (decl) = TYPE_MODE (type);
333 if (DECL_SIZE (decl) == 0)
335 DECL_SIZE (decl) = TYPE_SIZE (type);
336 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
338 else if (DECL_SIZE_UNIT (decl) == 0)
339 DECL_SIZE_UNIT (decl)
340 = convert (sizetype, size_binop (CEIL_DIV_EXPR, DECL_SIZE (decl),
341 bitsize_unit_node));
343 if (code != FIELD_DECL)
344 /* For non-fields, update the alignment from the type. */
345 do_type_align (type, decl);
346 else
347 /* For fields, it's a bit more complicated... */
349 bool old_user_align = DECL_USER_ALIGN (decl);
351 if (DECL_BIT_FIELD (decl))
353 DECL_BIT_FIELD_TYPE (decl) = type;
355 /* A zero-length bit-field affects the alignment of the next
356 field. */
357 if (integer_zerop (DECL_SIZE (decl))
358 && ! DECL_PACKED (decl)
359 && ! targetm.ms_bitfield_layout_p (DECL_FIELD_CONTEXT (decl)))
361 #ifdef PCC_BITFIELD_TYPE_MATTERS
362 if (PCC_BITFIELD_TYPE_MATTERS)
363 do_type_align (type, decl);
364 else
365 #endif
367 #ifdef EMPTY_FIELD_BOUNDARY
368 if (EMPTY_FIELD_BOUNDARY > DECL_ALIGN (decl))
370 DECL_ALIGN (decl) = EMPTY_FIELD_BOUNDARY;
371 DECL_USER_ALIGN (decl) = 0;
373 #endif
377 /* See if we can use an ordinary integer mode for a bit-field.
378 Conditions are: a fixed size that is correct for another mode
379 and occupying a complete byte or bytes on proper boundary. */
380 if (TYPE_SIZE (type) != 0
381 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
382 && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
384 enum machine_mode xmode
385 = mode_for_size_tree (DECL_SIZE (decl), MODE_INT, 1);
387 if (xmode != BLKmode
388 && (known_align == 0
389 || known_align >= GET_MODE_ALIGNMENT (xmode)))
391 DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
392 DECL_ALIGN (decl));
393 DECL_MODE (decl) = xmode;
394 DECL_BIT_FIELD (decl) = 0;
398 /* Turn off DECL_BIT_FIELD if we won't need it set. */
399 if (TYPE_MODE (type) == BLKmode && DECL_MODE (decl) == BLKmode
400 && known_align >= TYPE_ALIGN (type)
401 && DECL_ALIGN (decl) >= TYPE_ALIGN (type))
402 DECL_BIT_FIELD (decl) = 0;
404 else if (DECL_PACKED (decl) && DECL_USER_ALIGN (decl))
405 /* Don't touch DECL_ALIGN. For other packed fields, go ahead and
406 round up; we'll reduce it again below. We want packing to
407 supersede USER_ALIGN inherited from the type, but defer to
408 alignment explicitly specified on the field decl. */;
409 else
410 do_type_align (type, decl);
412 /* If the field is of variable size, we can't misalign it since we
413 have no way to make a temporary to align the result. But this
414 isn't an issue if the decl is not addressable. Likewise if it
415 is of unknown size.
417 Note that do_type_align may set DECL_USER_ALIGN, so we need to
418 check old_user_align instead. */
419 if (DECL_PACKED (decl)
420 && !old_user_align
421 && (DECL_NONADDRESSABLE_P (decl)
422 || DECL_SIZE_UNIT (decl) == 0
423 || TREE_CODE (DECL_SIZE_UNIT (decl)) == INTEGER_CST))
424 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
426 if (! DECL_USER_ALIGN (decl) && ! DECL_PACKED (decl))
428 /* Some targets (i.e. i386, VMS) limit struct field alignment
429 to a lower boundary than alignment of variables unless
430 it was overridden by attribute aligned. */
431 #ifdef BIGGEST_FIELD_ALIGNMENT
432 DECL_ALIGN (decl)
433 = MIN (DECL_ALIGN (decl), (unsigned) BIGGEST_FIELD_ALIGNMENT);
434 #endif
435 #ifdef ADJUST_FIELD_ALIGN
436 DECL_ALIGN (decl) = ADJUST_FIELD_ALIGN (decl, DECL_ALIGN (decl));
437 #endif
440 /* Should this be controlled by DECL_USER_ALIGN, too? */
441 if (maximum_field_alignment != 0)
442 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
445 /* Evaluate nonconstant size only once, either now or as soon as safe. */
446 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
447 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
448 if (DECL_SIZE_UNIT (decl) != 0
449 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST)
450 DECL_SIZE_UNIT (decl) = variable_size (DECL_SIZE_UNIT (decl));
452 /* If requested, warn about definitions of large data objects. */
453 if (warn_larger_than
454 && (code == VAR_DECL || code == PARM_DECL)
455 && ! DECL_EXTERNAL (decl))
457 tree size = DECL_SIZE_UNIT (decl);
459 if (size != 0 && TREE_CODE (size) == INTEGER_CST
460 && compare_tree_int (size, larger_than_size) > 0)
462 int size_as_int = TREE_INT_CST_LOW (size);
464 if (compare_tree_int (size, size_as_int) == 0)
465 warning ("%Jsize of '%D' is %d bytes", decl, decl, size_as_int);
466 else
467 warning ("%Jsize of '%D' is larger than %d bytes",
468 decl, decl, larger_than_size);
472 /* If the RTL was already set, update its mode and mem attributes. */
473 if (rtl)
475 PUT_MODE (rtl, DECL_MODE (decl));
476 SET_DECL_RTL (decl, 0);
477 set_mem_attributes (rtl, decl, 1);
478 SET_DECL_RTL (decl, rtl);
482 /* Given a VAR_DECL, PARM_DECL or RESULT_DECL, clears the results of
483 a previous call to layout_decl and calls it again. */
485 void
486 relayout_decl (tree decl)
488 DECL_SIZE (decl) = DECL_SIZE_UNIT (decl) = 0;
489 DECL_MODE (decl) = VOIDmode;
490 DECL_ALIGN (decl) = 0;
491 SET_DECL_RTL (decl, 0);
493 layout_decl (decl, 0);
496 /* Hook for a front-end function that can modify the record layout as needed
497 immediately before it is finalized. */
499 void (*lang_adjust_rli) (record_layout_info) = 0;
501 void
502 set_lang_adjust_rli (void (*f) (record_layout_info))
504 lang_adjust_rli = f;
507 /* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
508 QUAL_UNION_TYPE. Return a pointer to a struct record_layout_info which
509 is to be passed to all other layout functions for this record. It is the
510 responsibility of the caller to call `free' for the storage returned.
511 Note that garbage collection is not permitted until we finish laying
512 out the record. */
514 record_layout_info
515 start_record_layout (tree t)
517 record_layout_info rli = xmalloc (sizeof (struct record_layout_info_s));
519 rli->t = t;
521 /* If the type has a minimum specified alignment (via an attribute
522 declaration, for example) use it -- otherwise, start with a
523 one-byte alignment. */
524 rli->record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (t));
525 rli->unpacked_align = rli->record_align;
526 rli->offset_align = MAX (rli->record_align, BIGGEST_ALIGNMENT);
528 #ifdef STRUCTURE_SIZE_BOUNDARY
529 /* Packed structures don't need to have minimum size. */
530 if (! TYPE_PACKED (t))
531 rli->record_align = MAX (rli->record_align, (unsigned) STRUCTURE_SIZE_BOUNDARY);
532 #endif
534 rli->offset = size_zero_node;
535 rli->bitpos = bitsize_zero_node;
536 rli->prev_field = 0;
537 rli->pending_statics = 0;
538 rli->packed_maybe_necessary = 0;
540 return rli;
543 /* These four routines perform computations that convert between
544 the offset/bitpos forms and byte and bit offsets. */
546 tree
547 bit_from_pos (tree offset, tree bitpos)
549 return size_binop (PLUS_EXPR, bitpos,
550 size_binop (MULT_EXPR, convert (bitsizetype, offset),
551 bitsize_unit_node));
554 tree
555 byte_from_pos (tree offset, tree bitpos)
557 return size_binop (PLUS_EXPR, offset,
558 convert (sizetype,
559 size_binop (TRUNC_DIV_EXPR, bitpos,
560 bitsize_unit_node)));
563 void
564 pos_from_bit (tree *poffset, tree *pbitpos, unsigned int off_align,
565 tree pos)
567 *poffset = size_binop (MULT_EXPR,
568 convert (sizetype,
569 size_binop (FLOOR_DIV_EXPR, pos,
570 bitsize_int (off_align))),
571 size_int (off_align / BITS_PER_UNIT));
572 *pbitpos = size_binop (FLOOR_MOD_EXPR, pos, bitsize_int (off_align));
575 /* Given a pointer to bit and byte offsets and an offset alignment,
576 normalize the offsets so they are within the alignment. */
578 void
579 normalize_offset (tree *poffset, tree *pbitpos, unsigned int off_align)
581 /* If the bit position is now larger than it should be, adjust it
582 downwards. */
583 if (compare_tree_int (*pbitpos, off_align) >= 0)
585 tree extra_aligns = size_binop (FLOOR_DIV_EXPR, *pbitpos,
586 bitsize_int (off_align));
588 *poffset
589 = size_binop (PLUS_EXPR, *poffset,
590 size_binop (MULT_EXPR, convert (sizetype, extra_aligns),
591 size_int (off_align / BITS_PER_UNIT)));
593 *pbitpos
594 = size_binop (FLOOR_MOD_EXPR, *pbitpos, bitsize_int (off_align));
598 /* Print debugging information about the information in RLI. */
600 void
601 debug_rli (record_layout_info rli)
603 print_node_brief (stderr, "type", rli->t, 0);
604 print_node_brief (stderr, "\noffset", rli->offset, 0);
605 print_node_brief (stderr, " bitpos", rli->bitpos, 0);
607 fprintf (stderr, "\naligns: rec = %u, unpack = %u, off = %u\n",
608 rli->record_align, rli->unpacked_align,
609 rli->offset_align);
610 if (rli->packed_maybe_necessary)
611 fprintf (stderr, "packed may be necessary\n");
613 if (rli->pending_statics)
615 fprintf (stderr, "pending statics:\n");
616 debug_tree (rli->pending_statics);
620 /* Given an RLI with a possibly-incremented BITPOS, adjust OFFSET and
621 BITPOS if necessary to keep BITPOS below OFFSET_ALIGN. */
623 void
624 normalize_rli (record_layout_info rli)
626 normalize_offset (&rli->offset, &rli->bitpos, rli->offset_align);
629 /* Returns the size in bytes allocated so far. */
631 tree
632 rli_size_unit_so_far (record_layout_info rli)
634 return byte_from_pos (rli->offset, rli->bitpos);
637 /* Returns the size in bits allocated so far. */
639 tree
640 rli_size_so_far (record_layout_info rli)
642 return bit_from_pos (rli->offset, rli->bitpos);
645 /* FIELD is about to be added to RLI->T. The alignment (in bits) of
646 the next available location is given by KNOWN_ALIGN. Update the
647 variable alignment fields in RLI, and return the alignment to give
648 the FIELD. */
650 unsigned int
651 update_alignment_for_field (record_layout_info rli, tree field,
652 unsigned int known_align)
654 /* The alignment required for FIELD. */
655 unsigned int desired_align;
656 /* The type of this field. */
657 tree type = TREE_TYPE (field);
658 /* True if the field was explicitly aligned by the user. */
659 bool user_align;
660 bool is_bitfield;
662 /* Lay out the field so we know what alignment it needs. */
663 layout_decl (field, known_align);
664 desired_align = DECL_ALIGN (field);
665 user_align = DECL_USER_ALIGN (field);
667 is_bitfield = (type != error_mark_node
668 && DECL_BIT_FIELD_TYPE (field)
669 && ! integer_zerop (TYPE_SIZE (type)));
671 /* Record must have at least as much alignment as any field.
672 Otherwise, the alignment of the field within the record is
673 meaningless. */
674 if (is_bitfield && targetm.ms_bitfield_layout_p (rli->t))
676 /* Here, the alignment of the underlying type of a bitfield can
677 affect the alignment of a record; even a zero-sized field
678 can do this. The alignment should be to the alignment of
679 the type, except that for zero-size bitfields this only
680 applies if there was an immediately prior, nonzero-size
681 bitfield. (That's the way it is, experimentally.) */
682 if (! integer_zerop (DECL_SIZE (field))
683 ? ! DECL_PACKED (field)
684 : (rli->prev_field
685 && DECL_BIT_FIELD_TYPE (rli->prev_field)
686 && ! integer_zerop (DECL_SIZE (rli->prev_field))))
688 unsigned int type_align = TYPE_ALIGN (type);
689 type_align = MAX (type_align, desired_align);
690 if (maximum_field_alignment != 0)
691 type_align = MIN (type_align, maximum_field_alignment);
692 rli->record_align = MAX (rli->record_align, type_align);
693 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
696 #ifdef PCC_BITFIELD_TYPE_MATTERS
697 else if (is_bitfield && PCC_BITFIELD_TYPE_MATTERS)
699 /* Named bit-fields cause the entire structure to have the
700 alignment implied by their type. Some targets also apply the same
701 rules to unnamed bitfields. */
702 if (DECL_NAME (field) != 0
703 || targetm.align_anon_bitfield ())
705 unsigned int type_align = TYPE_ALIGN (type);
707 #ifdef ADJUST_FIELD_ALIGN
708 if (! TYPE_USER_ALIGN (type))
709 type_align = ADJUST_FIELD_ALIGN (field, type_align);
710 #endif
712 if (maximum_field_alignment != 0)
713 type_align = MIN (type_align, maximum_field_alignment);
714 else if (DECL_PACKED (field))
715 type_align = MIN (type_align, BITS_PER_UNIT);
717 /* The alignment of the record is increased to the maximum
718 of the current alignment, the alignment indicated on the
719 field (i.e., the alignment specified by an __aligned__
720 attribute), and the alignment indicated by the type of
721 the field. */
722 rli->record_align = MAX (rli->record_align, desired_align);
723 rli->record_align = MAX (rli->record_align, type_align);
725 if (warn_packed)
726 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
727 user_align |= TYPE_USER_ALIGN (type);
730 #endif
731 else
733 rli->record_align = MAX (rli->record_align, desired_align);
734 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
737 TYPE_USER_ALIGN (rli->t) |= user_align;
739 return desired_align;
742 /* Called from place_field to handle unions. */
744 static void
745 place_union_field (record_layout_info rli, tree field)
747 update_alignment_for_field (rli, field, /*known_align=*/0);
749 DECL_FIELD_OFFSET (field) = size_zero_node;
750 DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
751 SET_DECL_OFFSET_ALIGN (field, BIGGEST_ALIGNMENT);
753 /* We assume the union's size will be a multiple of a byte so we don't
754 bother with BITPOS. */
755 if (TREE_CODE (rli->t) == UNION_TYPE)
756 rli->offset = size_binop (MAX_EXPR, rli->offset, DECL_SIZE_UNIT (field));
757 else if (TREE_CODE (rli->t) == QUAL_UNION_TYPE)
758 rli->offset = fold (build3 (COND_EXPR, sizetype,
759 DECL_QUALIFIER (field),
760 DECL_SIZE_UNIT (field), rli->offset));
763 #if defined (PCC_BITFIELD_TYPE_MATTERS) || defined (BITFIELD_NBYTES_LIMITED)
764 /* A bitfield of SIZE with a required access alignment of ALIGN is allocated
765 at BYTE_OFFSET / BIT_OFFSET. Return nonzero if the field would span more
766 units of alignment than the underlying TYPE. */
767 static int
768 excess_unit_span (HOST_WIDE_INT byte_offset, HOST_WIDE_INT bit_offset,
769 HOST_WIDE_INT size, HOST_WIDE_INT align, tree type)
771 /* Note that the calculation of OFFSET might overflow; we calculate it so
772 that we still get the right result as long as ALIGN is a power of two. */
773 unsigned HOST_WIDE_INT offset = byte_offset * BITS_PER_UNIT + bit_offset;
775 offset = offset % align;
776 return ((offset + size + align - 1) / align
777 > ((unsigned HOST_WIDE_INT) tree_low_cst (TYPE_SIZE (type), 1)
778 / align));
780 #endif
782 /* RLI contains information about the layout of a RECORD_TYPE. FIELD
783 is a FIELD_DECL to be added after those fields already present in
784 T. (FIELD is not actually added to the TYPE_FIELDS list here;
785 callers that desire that behavior must manually perform that step.) */
787 void
788 place_field (record_layout_info rli, tree field)
790 /* The alignment required for FIELD. */
791 unsigned int desired_align;
792 /* The alignment FIELD would have if we just dropped it into the
793 record as it presently stands. */
794 unsigned int known_align;
795 unsigned int actual_align;
796 /* The type of this field. */
797 tree type = TREE_TYPE (field);
799 if (TREE_CODE (field) == ERROR_MARK || TREE_CODE (type) == ERROR_MARK)
800 return;
802 /* If FIELD is static, then treat it like a separate variable, not
803 really like a structure field. If it is a FUNCTION_DECL, it's a
804 method. In both cases, all we do is lay out the decl, and we do
805 it *after* the record is laid out. */
806 if (TREE_CODE (field) == VAR_DECL)
808 rli->pending_statics = tree_cons (NULL_TREE, field,
809 rli->pending_statics);
810 return;
813 /* Enumerators and enum types which are local to this class need not
814 be laid out. Likewise for initialized constant fields. */
815 else if (TREE_CODE (field) != FIELD_DECL)
816 return;
818 /* Unions are laid out very differently than records, so split
819 that code off to another function. */
820 else if (TREE_CODE (rli->t) != RECORD_TYPE)
822 place_union_field (rli, field);
823 return;
826 /* Work out the known alignment so far. Note that A & (-A) is the
827 value of the least-significant bit in A that is one. */
828 if (! integer_zerop (rli->bitpos))
829 known_align = (tree_low_cst (rli->bitpos, 1)
830 & - tree_low_cst (rli->bitpos, 1));
831 else if (integer_zerop (rli->offset))
832 known_align = BIGGEST_ALIGNMENT;
833 else if (host_integerp (rli->offset, 1))
834 known_align = (BITS_PER_UNIT
835 * (tree_low_cst (rli->offset, 1)
836 & - tree_low_cst (rli->offset, 1)));
837 else
838 known_align = rli->offset_align;
840 desired_align = update_alignment_for_field (rli, field, known_align);
842 if (warn_packed && DECL_PACKED (field))
844 if (known_align >= TYPE_ALIGN (type))
846 if (TYPE_ALIGN (type) > desired_align)
848 if (STRICT_ALIGNMENT)
849 warning ("%Jpacked attribute causes inefficient alignment "
850 "for '%D'", field, field);
851 else
852 warning ("%Jpacked attribute is unnecessary for '%D'",
853 field, field);
856 else
857 rli->packed_maybe_necessary = 1;
860 /* Does this field automatically have alignment it needs by virtue
861 of the fields that precede it and the record's own alignment? */
862 if (known_align < desired_align)
864 /* No, we need to skip space before this field.
865 Bump the cumulative size to multiple of field alignment. */
867 if (warn_padded)
868 warning ("%Jpadding struct to align '%D'", field, field);
870 /* If the alignment is still within offset_align, just align
871 the bit position. */
872 if (desired_align < rli->offset_align)
873 rli->bitpos = round_up (rli->bitpos, desired_align);
874 else
876 /* First adjust OFFSET by the partial bits, then align. */
877 rli->offset
878 = size_binop (PLUS_EXPR, rli->offset,
879 convert (sizetype,
880 size_binop (CEIL_DIV_EXPR, rli->bitpos,
881 bitsize_unit_node)));
882 rli->bitpos = bitsize_zero_node;
884 rli->offset = round_up (rli->offset, desired_align / BITS_PER_UNIT);
887 if (! TREE_CONSTANT (rli->offset))
888 rli->offset_align = desired_align;
892 /* Handle compatibility with PCC. Note that if the record has any
893 variable-sized fields, we need not worry about compatibility. */
894 #ifdef PCC_BITFIELD_TYPE_MATTERS
895 if (PCC_BITFIELD_TYPE_MATTERS
896 && ! targetm.ms_bitfield_layout_p (rli->t)
897 && TREE_CODE (field) == FIELD_DECL
898 && type != error_mark_node
899 && DECL_BIT_FIELD (field)
900 && ! DECL_PACKED (field)
901 && maximum_field_alignment == 0
902 && ! integer_zerop (DECL_SIZE (field))
903 && host_integerp (DECL_SIZE (field), 1)
904 && host_integerp (rli->offset, 1)
905 && host_integerp (TYPE_SIZE (type), 1))
907 unsigned int type_align = TYPE_ALIGN (type);
908 tree dsize = DECL_SIZE (field);
909 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
910 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
911 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
913 #ifdef ADJUST_FIELD_ALIGN
914 if (! TYPE_USER_ALIGN (type))
915 type_align = ADJUST_FIELD_ALIGN (field, type_align);
916 #endif
918 /* A bit field may not span more units of alignment of its type
919 than its type itself. Advance to next boundary if necessary. */
920 if (excess_unit_span (offset, bit_offset, field_size, type_align, type))
921 rli->bitpos = round_up (rli->bitpos, type_align);
923 TYPE_USER_ALIGN (rli->t) |= TYPE_USER_ALIGN (type);
925 #endif
927 #ifdef BITFIELD_NBYTES_LIMITED
928 if (BITFIELD_NBYTES_LIMITED
929 && ! targetm.ms_bitfield_layout_p (rli->t)
930 && TREE_CODE (field) == FIELD_DECL
931 && type != error_mark_node
932 && DECL_BIT_FIELD_TYPE (field)
933 && ! DECL_PACKED (field)
934 && ! integer_zerop (DECL_SIZE (field))
935 && host_integerp (DECL_SIZE (field), 1)
936 && host_integerp (rli->offset, 1)
937 && host_integerp (TYPE_SIZE (type), 1))
939 unsigned int type_align = TYPE_ALIGN (type);
940 tree dsize = DECL_SIZE (field);
941 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
942 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
943 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
945 #ifdef ADJUST_FIELD_ALIGN
946 if (! TYPE_USER_ALIGN (type))
947 type_align = ADJUST_FIELD_ALIGN (field, type_align);
948 #endif
950 if (maximum_field_alignment != 0)
951 type_align = MIN (type_align, maximum_field_alignment);
952 /* ??? This test is opposite the test in the containing if
953 statement, so this code is unreachable currently. */
954 else if (DECL_PACKED (field))
955 type_align = MIN (type_align, BITS_PER_UNIT);
957 /* A bit field may not span the unit of alignment of its type.
958 Advance to next boundary if necessary. */
959 if (excess_unit_span (offset, bit_offset, field_size, type_align, type))
960 rli->bitpos = round_up (rli->bitpos, type_align);
962 TYPE_USER_ALIGN (rli->t) |= TYPE_USER_ALIGN (type);
964 #endif
966 /* See the docs for TARGET_MS_BITFIELD_LAYOUT_P for details.
967 A subtlety:
968 When a bit field is inserted into a packed record, the whole
969 size of the underlying type is used by one or more same-size
970 adjacent bitfields. (That is, if its long:3, 32 bits is
971 used in the record, and any additional adjacent long bitfields are
972 packed into the same chunk of 32 bits. However, if the size
973 changes, a new field of that size is allocated.) In an unpacked
974 record, this is the same as using alignment, but not equivalent
975 when packing.
977 Note: for compatibility, we use the type size, not the type alignment
978 to determine alignment, since that matches the documentation */
980 if (targetm.ms_bitfield_layout_p (rli->t)
981 && ((DECL_BIT_FIELD_TYPE (field) && ! DECL_PACKED (field))
982 || (rli->prev_field && ! DECL_PACKED (rli->prev_field))))
984 /* At this point, either the prior or current are bitfields,
985 (possibly both), and we're dealing with MS packing. */
986 tree prev_saved = rli->prev_field;
988 /* Is the prior field a bitfield? If so, handle "runs" of same
989 type size fields. */
990 if (rli->prev_field /* necessarily a bitfield if it exists. */)
992 /* If both are bitfields, nonzero, and the same size, this is
993 the middle of a run. Zero declared size fields are special
994 and handled as "end of run". (Note: it's nonzero declared
995 size, but equal type sizes!) (Since we know that both
996 the current and previous fields are bitfields by the
997 time we check it, DECL_SIZE must be present for both.) */
998 if (DECL_BIT_FIELD_TYPE (field)
999 && !integer_zerop (DECL_SIZE (field))
1000 && !integer_zerop (DECL_SIZE (rli->prev_field))
1001 && host_integerp (DECL_SIZE (rli->prev_field), 0)
1002 && host_integerp (TYPE_SIZE (type), 0)
1003 && simple_cst_equal (TYPE_SIZE (type),
1004 TYPE_SIZE (TREE_TYPE (rli->prev_field))))
1006 /* We're in the middle of a run of equal type size fields; make
1007 sure we realign if we run out of bits. (Not decl size,
1008 type size!) */
1009 HOST_WIDE_INT bitsize = tree_low_cst (DECL_SIZE (field), 0);
1011 if (rli->remaining_in_alignment < bitsize)
1013 /* out of bits; bump up to next 'word'. */
1014 rli->offset = DECL_FIELD_OFFSET (rli->prev_field);
1015 rli->bitpos
1016 = size_binop (PLUS_EXPR, TYPE_SIZE (type),
1017 DECL_FIELD_BIT_OFFSET (rli->prev_field));
1018 rli->prev_field = field;
1019 rli->remaining_in_alignment
1020 = tree_low_cst (TYPE_SIZE (type), 0);
1023 rli->remaining_in_alignment -= bitsize;
1025 else
1027 /* End of a run: if leaving a run of bitfields of the same type
1028 size, we have to "use up" the rest of the bits of the type
1029 size.
1031 Compute the new position as the sum of the size for the prior
1032 type and where we first started working on that type.
1033 Note: since the beginning of the field was aligned then
1034 of course the end will be too. No round needed. */
1036 if (!integer_zerop (DECL_SIZE (rli->prev_field)))
1038 tree type_size = TYPE_SIZE (TREE_TYPE (rli->prev_field));
1040 rli->bitpos
1041 = size_binop (PLUS_EXPR, type_size,
1042 DECL_FIELD_BIT_OFFSET (rli->prev_field));
1044 else
1045 /* We "use up" size zero fields; the code below should behave
1046 as if the prior field was not a bitfield. */
1047 prev_saved = NULL;
1049 /* Cause a new bitfield to be captured, either this time (if
1050 currently a bitfield) or next time we see one. */
1051 if (!DECL_BIT_FIELD_TYPE(field)
1052 || integer_zerop (DECL_SIZE (field)))
1053 rli->prev_field = NULL;
1056 normalize_rli (rli);
1059 /* If we're starting a new run of same size type bitfields
1060 (or a run of non-bitfields), set up the "first of the run"
1061 fields.
1063 That is, if the current field is not a bitfield, or if there
1064 was a prior bitfield the type sizes differ, or if there wasn't
1065 a prior bitfield the size of the current field is nonzero.
1067 Note: we must be sure to test ONLY the type size if there was
1068 a prior bitfield and ONLY for the current field being zero if
1069 there wasn't. */
1071 if (!DECL_BIT_FIELD_TYPE (field)
1072 || ( prev_saved != NULL
1073 ? !simple_cst_equal (TYPE_SIZE (type),
1074 TYPE_SIZE (TREE_TYPE (prev_saved)))
1075 : !integer_zerop (DECL_SIZE (field)) ))
1077 /* Never smaller than a byte for compatibility. */
1078 unsigned int type_align = BITS_PER_UNIT;
1080 /* (When not a bitfield), we could be seeing a flex array (with
1081 no DECL_SIZE). Since we won't be using remaining_in_alignment
1082 until we see a bitfield (and come by here again) we just skip
1083 calculating it. */
1084 if (DECL_SIZE (field) != NULL
1085 && host_integerp (TYPE_SIZE (TREE_TYPE (field)), 0)
1086 && host_integerp (DECL_SIZE (field), 0))
1087 rli->remaining_in_alignment
1088 = tree_low_cst (TYPE_SIZE (TREE_TYPE(field)), 0)
1089 - tree_low_cst (DECL_SIZE (field), 0);
1091 /* Now align (conventionally) for the new type. */
1092 if (!DECL_PACKED(field))
1093 type_align = MAX(TYPE_ALIGN (type), type_align);
1095 if (prev_saved
1096 && DECL_BIT_FIELD_TYPE (prev_saved)
1097 /* If the previous bit-field is zero-sized, we've already
1098 accounted for its alignment needs (or ignored it, if
1099 appropriate) while placing it. */
1100 && ! integer_zerop (DECL_SIZE (prev_saved)))
1101 type_align = MAX (type_align,
1102 TYPE_ALIGN (TREE_TYPE (prev_saved)));
1104 if (maximum_field_alignment != 0)
1105 type_align = MIN (type_align, maximum_field_alignment);
1107 rli->bitpos = round_up (rli->bitpos, type_align);
1109 /* If we really aligned, don't allow subsequent bitfields
1110 to undo that. */
1111 rli->prev_field = NULL;
1115 /* Offset so far becomes the position of this field after normalizing. */
1116 normalize_rli (rli);
1117 DECL_FIELD_OFFSET (field) = rli->offset;
1118 DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
1119 SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
1121 /* If this field ended up more aligned than we thought it would be (we
1122 approximate this by seeing if its position changed), lay out the field
1123 again; perhaps we can use an integral mode for it now. */
1124 if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field)))
1125 actual_align = (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1126 & - tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1));
1127 else if (integer_zerop (DECL_FIELD_OFFSET (field)))
1128 actual_align = BIGGEST_ALIGNMENT;
1129 else if (host_integerp (DECL_FIELD_OFFSET (field), 1))
1130 actual_align = (BITS_PER_UNIT
1131 * (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
1132 & - tree_low_cst (DECL_FIELD_OFFSET (field), 1)));
1133 else
1134 actual_align = DECL_OFFSET_ALIGN (field);
1136 if (known_align != actual_align)
1137 layout_decl (field, actual_align);
1139 /* Only the MS bitfields use this. */
1140 if (rli->prev_field == NULL && DECL_BIT_FIELD_TYPE(field))
1141 rli->prev_field = field;
1143 /* Now add size of this field to the size of the record. If the size is
1144 not constant, treat the field as being a multiple of bytes and just
1145 adjust the offset, resetting the bit position. Otherwise, apportion the
1146 size amongst the bit position and offset. First handle the case of an
1147 unspecified size, which can happen when we have an invalid nested struct
1148 definition, such as struct j { struct j { int i; } }. The error message
1149 is printed in finish_struct. */
1150 if (DECL_SIZE (field) == 0)
1151 /* Do nothing. */;
1152 else if (TREE_CODE (DECL_SIZE_UNIT (field)) != INTEGER_CST
1153 || TREE_CONSTANT_OVERFLOW (DECL_SIZE_UNIT (field)))
1155 rli->offset
1156 = size_binop (PLUS_EXPR, rli->offset,
1157 convert (sizetype,
1158 size_binop (CEIL_DIV_EXPR, rli->bitpos,
1159 bitsize_unit_node)));
1160 rli->offset
1161 = size_binop (PLUS_EXPR, rli->offset, DECL_SIZE_UNIT (field));
1162 rli->bitpos = bitsize_zero_node;
1163 rli->offset_align = MIN (rli->offset_align, desired_align);
1165 else
1167 rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
1168 normalize_rli (rli);
1172 /* Assuming that all the fields have been laid out, this function uses
1173 RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
1174 indicated by RLI. */
1176 static void
1177 finalize_record_size (record_layout_info rli)
1179 tree unpadded_size, unpadded_size_unit;
1181 /* Now we want just byte and bit offsets, so set the offset alignment
1182 to be a byte and then normalize. */
1183 rli->offset_align = BITS_PER_UNIT;
1184 normalize_rli (rli);
1186 /* Determine the desired alignment. */
1187 #ifdef ROUND_TYPE_ALIGN
1188 TYPE_ALIGN (rli->t) = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t),
1189 rli->record_align);
1190 #else
1191 TYPE_ALIGN (rli->t) = MAX (TYPE_ALIGN (rli->t), rli->record_align);
1192 #endif
1194 /* Compute the size so far. Be sure to allow for extra bits in the
1195 size in bytes. We have guaranteed above that it will be no more
1196 than a single byte. */
1197 unpadded_size = rli_size_so_far (rli);
1198 unpadded_size_unit = rli_size_unit_so_far (rli);
1199 if (! integer_zerop (rli->bitpos))
1200 unpadded_size_unit
1201 = size_binop (PLUS_EXPR, unpadded_size_unit, size_one_node);
1203 /* Round the size up to be a multiple of the required alignment. */
1204 TYPE_SIZE (rli->t) = round_up (unpadded_size, TYPE_ALIGN (rli->t));
1205 TYPE_SIZE_UNIT (rli->t) = round_up (unpadded_size_unit,
1206 TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
1208 if (warn_padded && TREE_CONSTANT (unpadded_size)
1209 && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0)
1210 warning ("padding struct size to alignment boundary");
1212 if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
1213 && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
1214 && TREE_CONSTANT (unpadded_size))
1216 tree unpacked_size;
1218 #ifdef ROUND_TYPE_ALIGN
1219 rli->unpacked_align
1220 = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t), rli->unpacked_align);
1221 #else
1222 rli->unpacked_align = MAX (TYPE_ALIGN (rli->t), rli->unpacked_align);
1223 #endif
1225 unpacked_size = round_up (TYPE_SIZE (rli->t), rli->unpacked_align);
1226 if (simple_cst_equal (unpacked_size, TYPE_SIZE (rli->t)))
1228 TYPE_PACKED (rli->t) = 0;
1230 if (TYPE_NAME (rli->t))
1232 const char *name;
1234 if (TREE_CODE (TYPE_NAME (rli->t)) == IDENTIFIER_NODE)
1235 name = IDENTIFIER_POINTER (TYPE_NAME (rli->t));
1236 else
1237 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (rli->t)));
1239 if (STRICT_ALIGNMENT)
1240 warning ("packed attribute causes inefficient alignment for `%s'", name);
1241 else
1242 warning ("packed attribute is unnecessary for `%s'", name);
1244 else
1246 if (STRICT_ALIGNMENT)
1247 warning ("packed attribute causes inefficient alignment");
1248 else
1249 warning ("packed attribute is unnecessary");
1255 /* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE). */
1257 void
1258 compute_record_mode (tree type)
1260 tree field;
1261 enum machine_mode mode = VOIDmode;
1263 /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
1264 However, if possible, we use a mode that fits in a register
1265 instead, in order to allow for better optimization down the
1266 line. */
1267 TYPE_MODE (type) = BLKmode;
1269 if (! host_integerp (TYPE_SIZE (type), 1))
1270 return;
1272 /* A record which has any BLKmode members must itself be
1273 BLKmode; it can't go in a register. Unless the member is
1274 BLKmode only because it isn't aligned. */
1275 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1277 if (TREE_CODE (field) != FIELD_DECL)
1278 continue;
1280 if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK
1281 || (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1282 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field))
1283 && !(TYPE_SIZE (TREE_TYPE (field)) != 0
1284 && integer_zerop (TYPE_SIZE (TREE_TYPE (field)))))
1285 || ! host_integerp (bit_position (field), 1)
1286 || DECL_SIZE (field) == 0
1287 || ! host_integerp (DECL_SIZE (field), 1))
1288 return;
1290 /* If this field is the whole struct, remember its mode so
1291 that, say, we can put a double in a class into a DF
1292 register instead of forcing it to live in the stack. */
1293 if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
1294 mode = DECL_MODE (field);
1296 #ifdef MEMBER_TYPE_FORCES_BLK
1297 /* With some targets, eg. c4x, it is sub-optimal
1298 to access an aligned BLKmode structure as a scalar. */
1300 if (MEMBER_TYPE_FORCES_BLK (field, mode))
1301 return;
1302 #endif /* MEMBER_TYPE_FORCES_BLK */
1305 /* If we only have one real field; use its mode. This only applies to
1306 RECORD_TYPE. This does not apply to unions. */
1307 if (TREE_CODE (type) == RECORD_TYPE && mode != VOIDmode)
1308 TYPE_MODE (type) = mode;
1309 else
1310 TYPE_MODE (type) = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1312 /* If structure's known alignment is less than what the scalar
1313 mode would need, and it matters, then stick with BLKmode. */
1314 if (TYPE_MODE (type) != BLKmode
1315 && STRICT_ALIGNMENT
1316 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1317 || TYPE_ALIGN (type) >= GET_MODE_ALIGNMENT (TYPE_MODE (type))))
1319 /* If this is the only reason this type is BLKmode, then
1320 don't force containing types to be BLKmode. */
1321 TYPE_NO_FORCE_BLK (type) = 1;
1322 TYPE_MODE (type) = BLKmode;
1326 /* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
1327 out. */
1329 static void
1330 finalize_type_size (tree type)
1332 /* Normally, use the alignment corresponding to the mode chosen.
1333 However, where strict alignment is not required, avoid
1334 over-aligning structures, since most compilers do not do this
1335 alignment. */
1337 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1338 && (STRICT_ALIGNMENT
1339 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1340 && TREE_CODE (type) != QUAL_UNION_TYPE
1341 && TREE_CODE (type) != ARRAY_TYPE)))
1343 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1344 TYPE_USER_ALIGN (type) = 0;
1347 /* Do machine-dependent extra alignment. */
1348 #ifdef ROUND_TYPE_ALIGN
1349 TYPE_ALIGN (type)
1350 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1351 #endif
1353 /* If we failed to find a simple way to calculate the unit size
1354 of the type, find it by division. */
1355 if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1356 /* TYPE_SIZE (type) is computed in bitsizetype. After the division, the
1357 result will fit in sizetype. We will get more efficient code using
1358 sizetype, so we force a conversion. */
1359 TYPE_SIZE_UNIT (type)
1360 = convert (sizetype,
1361 size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1362 bitsize_unit_node));
1364 if (TYPE_SIZE (type) != 0)
1366 TYPE_SIZE (type) = round_up (TYPE_SIZE (type), TYPE_ALIGN (type));
1367 TYPE_SIZE_UNIT (type)
1368 = round_up (TYPE_SIZE_UNIT (type), TYPE_ALIGN (type) / BITS_PER_UNIT);
1371 /* Evaluate nonconstant sizes only once, either now or as soon as safe. */
1372 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1373 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1374 if (TYPE_SIZE_UNIT (type) != 0
1375 && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1376 TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1378 /* Also layout any other variants of the type. */
1379 if (TYPE_NEXT_VARIANT (type)
1380 || type != TYPE_MAIN_VARIANT (type))
1382 tree variant;
1383 /* Record layout info of this variant. */
1384 tree size = TYPE_SIZE (type);
1385 tree size_unit = TYPE_SIZE_UNIT (type);
1386 unsigned int align = TYPE_ALIGN (type);
1387 unsigned int user_align = TYPE_USER_ALIGN (type);
1388 enum machine_mode mode = TYPE_MODE (type);
1390 /* Copy it into all variants. */
1391 for (variant = TYPE_MAIN_VARIANT (type);
1392 variant != 0;
1393 variant = TYPE_NEXT_VARIANT (variant))
1395 TYPE_SIZE (variant) = size;
1396 TYPE_SIZE_UNIT (variant) = size_unit;
1397 TYPE_ALIGN (variant) = align;
1398 TYPE_USER_ALIGN (variant) = user_align;
1399 TYPE_MODE (variant) = mode;
1404 /* Do all of the work required to layout the type indicated by RLI,
1405 once the fields have been laid out. This function will call `free'
1406 for RLI, unless FREE_P is false. Passing a value other than false
1407 for FREE_P is bad practice; this option only exists to support the
1408 G++ 3.2 ABI. */
1410 void
1411 finish_record_layout (record_layout_info rli, int free_p)
1413 /* Compute the final size. */
1414 finalize_record_size (rli);
1416 /* Compute the TYPE_MODE for the record. */
1417 compute_record_mode (rli->t);
1419 /* Perform any last tweaks to the TYPE_SIZE, etc. */
1420 finalize_type_size (rli->t);
1422 /* Lay out any static members. This is done now because their type
1423 may use the record's type. */
1424 while (rli->pending_statics)
1426 layout_decl (TREE_VALUE (rli->pending_statics), 0);
1427 rli->pending_statics = TREE_CHAIN (rli->pending_statics);
1430 /* Clean up. */
1431 if (free_p)
1432 free (rli);
1436 /* Finish processing a builtin RECORD_TYPE type TYPE. It's name is
1437 NAME, its fields are chained in reverse on FIELDS.
1439 If ALIGN_TYPE is non-null, it is given the same alignment as
1440 ALIGN_TYPE. */
1442 void
1443 finish_builtin_struct (tree type, const char *name, tree fields,
1444 tree align_type)
1446 tree tail, next;
1448 for (tail = NULL_TREE; fields; tail = fields, fields = next)
1450 DECL_FIELD_CONTEXT (fields) = type;
1451 next = TREE_CHAIN (fields);
1452 TREE_CHAIN (fields) = tail;
1454 TYPE_FIELDS (type) = tail;
1456 if (align_type)
1458 TYPE_ALIGN (type) = TYPE_ALIGN (align_type);
1459 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (align_type);
1462 layout_type (type);
1463 #if 0 /* not yet, should get fixed properly later */
1464 TYPE_NAME (type) = make_type_decl (get_identifier (name), type);
1465 #else
1466 TYPE_NAME (type) = build_decl (TYPE_DECL, get_identifier (name), type);
1467 #endif
1468 TYPE_STUB_DECL (type) = TYPE_NAME (type);
1469 layout_decl (TYPE_NAME (type), 0);
1472 /* Calculate the mode, size, and alignment for TYPE.
1473 For an array type, calculate the element separation as well.
1474 Record TYPE on the chain of permanent or temporary types
1475 so that dbxout will find out about it.
1477 TYPE_SIZE of a type is nonzero if the type has been laid out already.
1478 layout_type does nothing on such a type.
1480 If the type is incomplete, its TYPE_SIZE remains zero. */
1482 void
1483 layout_type (tree type)
1485 if (type == 0)
1486 abort ();
1488 if (type == error_mark_node)
1489 return;
1491 /* Do nothing if type has been laid out before. */
1492 if (TYPE_SIZE (type))
1493 return;
1495 switch (TREE_CODE (type))
1497 case LANG_TYPE:
1498 /* This kind of type is the responsibility
1499 of the language-specific code. */
1500 abort ();
1502 case BOOLEAN_TYPE: /* Used for Java, Pascal, and Chill. */
1503 if (TYPE_PRECISION (type) == 0)
1504 TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
1506 /* ... fall through ... */
1508 case INTEGER_TYPE:
1509 case ENUMERAL_TYPE:
1510 case CHAR_TYPE:
1511 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
1512 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
1513 TYPE_UNSIGNED (type) = 1;
1515 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
1516 MODE_INT);
1517 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1518 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1519 break;
1521 case REAL_TYPE:
1522 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
1523 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1524 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1525 break;
1527 case COMPLEX_TYPE:
1528 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TREE_TYPE (type));
1529 TYPE_MODE (type)
1530 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
1531 (TREE_CODE (TREE_TYPE (type)) == REAL_TYPE
1532 ? MODE_COMPLEX_FLOAT : MODE_COMPLEX_INT),
1534 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1535 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1536 break;
1538 case VECTOR_TYPE:
1540 int nunits = TYPE_VECTOR_SUBPARTS (type);
1541 tree nunits_tree = build_int_cst (NULL_TREE, nunits, 0);
1542 tree innertype = TREE_TYPE (type);
1544 if (nunits & (nunits - 1))
1545 abort ();
1547 /* Find an appropriate mode for the vector type. */
1548 if (TYPE_MODE (type) == VOIDmode)
1550 enum machine_mode innermode = TYPE_MODE (innertype);
1551 enum machine_mode mode;
1553 /* First, look for a supported vector type. */
1554 if (GET_MODE_CLASS (innermode) == MODE_FLOAT)
1555 mode = MIN_MODE_VECTOR_FLOAT;
1556 else
1557 mode = MIN_MODE_VECTOR_INT;
1559 for (; mode != VOIDmode ; mode = GET_MODE_WIDER_MODE (mode))
1560 if (GET_MODE_NUNITS (mode) == nunits
1561 && GET_MODE_INNER (mode) == innermode
1562 && targetm.vector_mode_supported_p (mode))
1563 break;
1565 /* For integers, try mapping it to a same-sized scalar mode. */
1566 if (mode == VOIDmode
1567 && GET_MODE_CLASS (innermode) == MODE_INT)
1568 mode = mode_for_size (nunits * GET_MODE_BITSIZE (innermode),
1569 MODE_INT, 0);
1571 if (mode == VOIDmode || !have_regs_of_mode[mode])
1572 TYPE_MODE (type) = BLKmode;
1573 else
1574 TYPE_MODE (type) = mode;
1577 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TREE_TYPE (type));
1578 TYPE_SIZE_UNIT (type) = int_const_binop (MULT_EXPR,
1579 TYPE_SIZE_UNIT (innertype),
1580 nunits_tree, 0);
1581 TYPE_SIZE (type) = int_const_binop (MULT_EXPR, TYPE_SIZE (innertype),
1582 nunits_tree, 0);
1583 break;
1586 case VOID_TYPE:
1587 /* This is an incomplete type and so doesn't have a size. */
1588 TYPE_ALIGN (type) = 1;
1589 TYPE_USER_ALIGN (type) = 0;
1590 TYPE_MODE (type) = VOIDmode;
1591 break;
1593 case OFFSET_TYPE:
1594 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
1595 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
1596 /* A pointer might be MODE_PARTIAL_INT,
1597 but ptrdiff_t must be integral. */
1598 TYPE_MODE (type) = mode_for_size (POINTER_SIZE, MODE_INT, 0);
1599 break;
1601 case FUNCTION_TYPE:
1602 case METHOD_TYPE:
1603 /* It's hard to see what the mode and size of a function ought to
1604 be, but we do know the alignment is FUNCTION_BOUNDARY, so
1605 make it consistent with that. */
1606 TYPE_MODE (type) = mode_for_size (FUNCTION_BOUNDARY, MODE_INT, 0);
1607 TYPE_SIZE (type) = bitsize_int (FUNCTION_BOUNDARY);
1608 TYPE_SIZE_UNIT (type) = size_int (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1609 break;
1611 case POINTER_TYPE:
1612 case REFERENCE_TYPE:
1615 enum machine_mode mode = ((TREE_CODE (type) == REFERENCE_TYPE
1616 && reference_types_internal)
1617 ? Pmode : TYPE_MODE (type));
1619 int nbits = GET_MODE_BITSIZE (mode);
1621 TYPE_SIZE (type) = bitsize_int (nbits);
1622 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (mode));
1623 TYPE_UNSIGNED (type) = 1;
1624 TYPE_PRECISION (type) = nbits;
1626 break;
1628 case ARRAY_TYPE:
1630 tree index = TYPE_DOMAIN (type);
1631 tree element = TREE_TYPE (type);
1633 build_pointer_type (element);
1635 /* We need to know both bounds in order to compute the size. */
1636 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
1637 && TYPE_SIZE (element))
1639 tree ub = TYPE_MAX_VALUE (index);
1640 tree lb = TYPE_MIN_VALUE (index);
1641 tree length;
1642 tree element_size;
1644 /* The initial subtraction should happen in the original type so
1645 that (possible) negative values are handled appropriately. */
1646 length = size_binop (PLUS_EXPR, size_one_node,
1647 convert (sizetype,
1648 fold (build2 (MINUS_EXPR,
1649 TREE_TYPE (lb),
1650 ub, lb))));
1652 /* Special handling for arrays of bits (for Chill). */
1653 element_size = TYPE_SIZE (element);
1654 if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element)
1655 && (integer_zerop (TYPE_MAX_VALUE (element))
1656 || integer_onep (TYPE_MAX_VALUE (element)))
1657 && host_integerp (TYPE_MIN_VALUE (element), 1))
1659 HOST_WIDE_INT maxvalue
1660 = tree_low_cst (TYPE_MAX_VALUE (element), 1);
1661 HOST_WIDE_INT minvalue
1662 = tree_low_cst (TYPE_MIN_VALUE (element), 1);
1664 if (maxvalue - minvalue == 1
1665 && (maxvalue == 1 || maxvalue == 0))
1666 element_size = integer_one_node;
1669 /* If neither bound is a constant and sizetype is signed, make
1670 sure the size is never negative. We should really do this
1671 if *either* bound is non-constant, but this is the best
1672 compromise between C and Ada. */
1673 if (!TYPE_UNSIGNED (sizetype)
1674 && TREE_CODE (TYPE_MIN_VALUE (index)) != INTEGER_CST
1675 && TREE_CODE (TYPE_MAX_VALUE (index)) != INTEGER_CST)
1676 length = size_binop (MAX_EXPR, length, size_zero_node);
1678 TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size,
1679 convert (bitsizetype, length));
1681 /* If we know the size of the element, calculate the total
1682 size directly, rather than do some division thing below.
1683 This optimization helps Fortran assumed-size arrays
1684 (where the size of the array is determined at runtime)
1685 substantially.
1686 Note that we can't do this in the case where the size of
1687 the elements is one bit since TYPE_SIZE_UNIT cannot be
1688 set correctly in that case. */
1689 if (TYPE_SIZE_UNIT (element) != 0 && ! integer_onep (element_size))
1690 TYPE_SIZE_UNIT (type)
1691 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
1694 /* Now round the alignment and size,
1695 using machine-dependent criteria if any. */
1697 #ifdef ROUND_TYPE_ALIGN
1698 TYPE_ALIGN (type)
1699 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
1700 #else
1701 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
1702 #endif
1703 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (element);
1704 TYPE_MODE (type) = BLKmode;
1705 if (TYPE_SIZE (type) != 0
1706 #ifdef MEMBER_TYPE_FORCES_BLK
1707 && ! MEMBER_TYPE_FORCES_BLK (type, VOIDmode)
1708 #endif
1709 /* BLKmode elements force BLKmode aggregate;
1710 else extract/store fields may lose. */
1711 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
1712 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
1714 /* One-element arrays get the component type's mode. */
1715 if (simple_cst_equal (TYPE_SIZE (type),
1716 TYPE_SIZE (TREE_TYPE (type))))
1717 TYPE_MODE (type) = TYPE_MODE (TREE_TYPE (type));
1718 else
1719 TYPE_MODE (type)
1720 = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1722 if (TYPE_MODE (type) != BLKmode
1723 && STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
1724 && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (TYPE_MODE (type))
1725 && TYPE_MODE (type) != BLKmode)
1727 TYPE_NO_FORCE_BLK (type) = 1;
1728 TYPE_MODE (type) = BLKmode;
1731 break;
1734 case RECORD_TYPE:
1735 case UNION_TYPE:
1736 case QUAL_UNION_TYPE:
1738 tree field;
1739 record_layout_info rli;
1741 /* Initialize the layout information. */
1742 rli = start_record_layout (type);
1744 /* If this is a QUAL_UNION_TYPE, we want to process the fields
1745 in the reverse order in building the COND_EXPR that denotes
1746 its size. We reverse them again later. */
1747 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1748 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1750 /* Place all the fields. */
1751 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1752 place_field (rli, field);
1754 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1755 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1757 if (lang_adjust_rli)
1758 (*lang_adjust_rli) (rli);
1760 /* Finish laying out the record. */
1761 finish_record_layout (rli, /*free_p=*/true);
1763 break;
1765 case SET_TYPE: /* Used by Chill and Pascal. */
1766 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1767 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1768 abort ();
1769 else
1771 #ifndef SET_WORD_SIZE
1772 #define SET_WORD_SIZE BITS_PER_WORD
1773 #endif
1774 unsigned int alignment
1775 = set_alignment ? set_alignment : SET_WORD_SIZE;
1776 HOST_WIDE_INT size_in_bits
1777 = (tree_low_cst (TYPE_MAX_VALUE (TYPE_DOMAIN (type)), 0)
1778 - tree_low_cst (TYPE_MIN_VALUE (TYPE_DOMAIN (type)), 0) + 1);
1779 HOST_WIDE_INT rounded_size
1780 = ((size_in_bits + alignment - 1) / alignment) * alignment;
1782 if (rounded_size > (int) alignment)
1783 TYPE_MODE (type) = BLKmode;
1784 else
1785 TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1787 TYPE_SIZE (type) = bitsize_int (rounded_size);
1788 TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1789 TYPE_ALIGN (type) = alignment;
1790 TYPE_USER_ALIGN (type) = 0;
1791 TYPE_PRECISION (type) = size_in_bits;
1793 break;
1795 case FILE_TYPE:
1796 /* The size may vary in different languages, so the language front end
1797 should fill in the size. */
1798 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1799 TYPE_USER_ALIGN (type) = 0;
1800 TYPE_MODE (type) = BLKmode;
1801 break;
1803 default:
1804 abort ();
1807 /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE. For
1808 records and unions, finish_record_layout already called this
1809 function. */
1810 if (TREE_CODE (type) != RECORD_TYPE
1811 && TREE_CODE (type) != UNION_TYPE
1812 && TREE_CODE (type) != QUAL_UNION_TYPE)
1813 finalize_type_size (type);
1815 /* If this type is created before sizetype has been permanently set,
1816 record it so set_sizetype can fix it up. */
1817 if (! sizetype_set)
1818 early_type_list = tree_cons (NULL_TREE, type, early_type_list);
1820 /* If an alias set has been set for this aggregate when it was incomplete,
1821 force it into alias set 0.
1822 This is too conservative, but we cannot call record_component_aliases
1823 here because some frontends still change the aggregates after
1824 layout_type. */
1825 if (AGGREGATE_TYPE_P (type) && TYPE_ALIAS_SET_KNOWN_P (type))
1826 TYPE_ALIAS_SET (type) = 0;
1829 /* Create and return a type for signed integers of PRECISION bits. */
1831 tree
1832 make_signed_type (int precision)
1834 tree type = make_node (INTEGER_TYPE);
1836 TYPE_PRECISION (type) = precision;
1838 fixup_signed_type (type);
1839 return type;
1842 /* Create and return a type for unsigned integers of PRECISION bits. */
1844 tree
1845 make_unsigned_type (int precision)
1847 tree type = make_node (INTEGER_TYPE);
1849 TYPE_PRECISION (type) = precision;
1851 fixup_unsigned_type (type);
1852 return type;
1855 /* Initialize sizetype and bitsizetype to a reasonable and temporary
1856 value to enable integer types to be created. */
1858 void
1859 initialize_sizetypes (void)
1861 tree t = make_node (INTEGER_TYPE);
1863 TYPE_MODE (t) = SImode;
1864 TYPE_ALIGN (t) = GET_MODE_ALIGNMENT (SImode);
1865 TYPE_USER_ALIGN (t) = 0;
1866 TYPE_IS_SIZETYPE (t) = 1;
1867 TYPE_SIZE (t) = build_int_cst (t, GET_MODE_BITSIZE (SImode), 0);
1868 TYPE_SIZE_UNIT (t) = build_int_cst (t, GET_MODE_SIZE (SImode), 0);
1869 TYPE_UNSIGNED (t) = 1;
1870 TYPE_PRECISION (t) = GET_MODE_BITSIZE (SImode);
1871 TYPE_MIN_VALUE (t) = build_int_cst (t, 0, 0);
1873 /* 1000 avoids problems with possible overflow and is certainly
1874 larger than any size value we'd want to be storing. */
1875 TYPE_MAX_VALUE (t) = build_int_cst (t, 1000, 0);
1877 /* These two must be different nodes because of the caching done in
1878 size_int_wide. */
1879 sizetype = t;
1880 bitsizetype = copy_node (t);
1881 TYPE_CACHED_VALUES (bitsizetype) = NULL_TREE;
1882 TYPE_CACHED_VALUES_P (bitsizetype) = 0;
1885 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1886 Also update the type of any standard type's sizes made so far. */
1888 void
1889 set_sizetype (tree type)
1891 int oprecision = TYPE_PRECISION (type);
1892 /* The *bitsizetype types use a precision that avoids overflows when
1893 calculating signed sizes / offsets in bits. However, when
1894 cross-compiling from a 32 bit to a 64 bit host, we are limited to 64 bit
1895 precision. */
1896 int precision = MIN (oprecision + BITS_PER_UNIT_LOG + 1,
1897 2 * HOST_BITS_PER_WIDE_INT);
1898 unsigned int i;
1899 tree t;
1901 if (sizetype_set)
1902 abort ();
1904 /* Make copies of nodes since we'll be setting TYPE_IS_SIZETYPE. */
1905 sizetype = copy_node (type);
1906 TYPE_CACHED_VALUES (sizetype) = make_tree_vec (INTEGER_SHARE_LIMIT);
1907 TYPE_CACHED_VALUES_P (sizetype) = 1;
1908 TREE_TYPE (TYPE_CACHED_VALUES (sizetype)) = type;
1909 TYPE_IS_SIZETYPE (sizetype) = 1;
1910 bitsizetype = make_node (INTEGER_TYPE);
1911 TYPE_NAME (bitsizetype) = TYPE_NAME (type);
1912 TYPE_PRECISION (bitsizetype) = precision;
1913 TYPE_IS_SIZETYPE (bitsizetype) = 1;
1915 if (TYPE_UNSIGNED (type))
1916 fixup_unsigned_type (bitsizetype);
1917 else
1918 fixup_signed_type (bitsizetype);
1920 layout_type (bitsizetype);
1922 if (TYPE_UNSIGNED (type))
1924 ssizetype = copy_node (make_signed_type (oprecision));
1925 sbitsizetype = copy_node (make_signed_type (precision));
1927 else
1929 ssizetype = sizetype;
1930 sbitsizetype = bitsizetype;
1933 TYPE_NAME (bitsizetype) = get_identifier ("bit_size_type");
1935 /* Show is a sizetype, is a main type, and has no pointers to it. */
1936 for (i = 0; i < ARRAY_SIZE (sizetype_tab); i++)
1938 TYPE_IS_SIZETYPE (sizetype_tab[i]) = 1;
1939 TYPE_MAIN_VARIANT (sizetype_tab[i]) = sizetype_tab[i];
1940 TYPE_NEXT_VARIANT (sizetype_tab[i]) = 0;
1941 TYPE_POINTER_TO (sizetype_tab[i]) = 0;
1942 TYPE_REFERENCE_TO (sizetype_tab[i]) = 0;
1945 /* Go down each of the types we already made and set the proper type
1946 for the sizes in them. */
1947 for (t = early_type_list; t != 0; t = TREE_CHAIN (t))
1949 if (TREE_CODE (TREE_VALUE (t)) != INTEGER_TYPE
1950 && TREE_CODE (TREE_VALUE (t)) != BOOLEAN_TYPE)
1951 abort ();
1953 TREE_TYPE (TYPE_SIZE (TREE_VALUE (t))) = bitsizetype;
1954 TREE_TYPE (TYPE_SIZE_UNIT (TREE_VALUE (t))) = sizetype;
1957 early_type_list = 0;
1958 sizetype_set = 1;
1961 /* TYPE is an integral type, i.e., an INTEGRAL_TYPE, ENUMERAL_TYPE,
1962 BOOLEAN_TYPE, or CHAR_TYPE. Set TYPE_MIN_VALUE and TYPE_MAX_VALUE
1963 for TYPE, based on the PRECISION and whether or not the TYPE
1964 IS_UNSIGNED. PRECISION need not correspond to a width supported
1965 natively by the hardware; for example, on a machine with 8-bit,
1966 16-bit, and 32-bit register modes, PRECISION might be 7, 23, or
1967 61. */
1969 void
1970 set_min_and_max_values_for_integral_type (tree type,
1971 int precision,
1972 bool is_unsigned)
1974 tree min_value;
1975 tree max_value;
1977 if (is_unsigned)
1979 min_value = build_int_cst (type, 0, 0);
1980 max_value
1981 = build_int_cst (type, precision - HOST_BITS_PER_WIDE_INT >= 0
1982 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1983 precision - HOST_BITS_PER_WIDE_INT > 0
1984 ? ((unsigned HOST_WIDE_INT) ~0
1985 >> (HOST_BITS_PER_WIDE_INT
1986 - (precision - HOST_BITS_PER_WIDE_INT)))
1987 : 0);
1989 else
1991 min_value
1992 = build_int_cst (type,
1993 (precision - HOST_BITS_PER_WIDE_INT > 0
1994 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1995 (((HOST_WIDE_INT) (-1)
1996 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1997 ? precision - HOST_BITS_PER_WIDE_INT - 1
1998 : 0))));
1999 max_value
2000 = build_int_cst (type,
2001 (precision - HOST_BITS_PER_WIDE_INT > 0
2002 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
2003 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
2004 ? (((HOST_WIDE_INT) 1
2005 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
2006 : 0));
2009 TYPE_MIN_VALUE (type) = min_value;
2010 TYPE_MAX_VALUE (type) = max_value;
2013 /* Set the extreme values of TYPE based on its precision in bits,
2014 then lay it out. Used when make_signed_type won't do
2015 because the tree code is not INTEGER_TYPE.
2016 E.g. for Pascal, when the -fsigned-char option is given. */
2018 void
2019 fixup_signed_type (tree type)
2021 int precision = TYPE_PRECISION (type);
2023 /* We can not represent properly constants greater then
2024 2 * HOST_BITS_PER_WIDE_INT, still we need the types
2025 as they are used by i386 vector extensions and friends. */
2026 if (precision > HOST_BITS_PER_WIDE_INT * 2)
2027 precision = HOST_BITS_PER_WIDE_INT * 2;
2029 set_min_and_max_values_for_integral_type (type, precision,
2030 /*is_unsigned=*/false);
2032 /* Lay out the type: set its alignment, size, etc. */
2033 layout_type (type);
2036 /* Set the extreme values of TYPE based on its precision in bits,
2037 then lay it out. This is used both in `make_unsigned_type'
2038 and for enumeral types. */
2040 void
2041 fixup_unsigned_type (tree type)
2043 int precision = TYPE_PRECISION (type);
2045 /* We can not represent properly constants greater then
2046 2 * HOST_BITS_PER_WIDE_INT, still we need the types
2047 as they are used by i386 vector extensions and friends. */
2048 if (precision > HOST_BITS_PER_WIDE_INT * 2)
2049 precision = HOST_BITS_PER_WIDE_INT * 2;
2051 TYPE_UNSIGNED (type) = 1;
2053 set_min_and_max_values_for_integral_type (type, precision,
2054 /*is_unsigned=*/true);
2056 /* Lay out the type: set its alignment, size, etc. */
2057 layout_type (type);
2060 /* Find the best machine mode to use when referencing a bit field of length
2061 BITSIZE bits starting at BITPOS.
2063 The underlying object is known to be aligned to a boundary of ALIGN bits.
2064 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
2065 larger than LARGEST_MODE (usually SImode).
2067 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
2068 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
2069 mode meeting these conditions.
2071 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
2072 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
2073 all the conditions. */
2075 enum machine_mode
2076 get_best_mode (int bitsize, int bitpos, unsigned int align,
2077 enum machine_mode largest_mode, int volatilep)
2079 enum machine_mode mode;
2080 unsigned int unit = 0;
2082 /* Find the narrowest integer mode that contains the bit field. */
2083 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2084 mode = GET_MODE_WIDER_MODE (mode))
2086 unit = GET_MODE_BITSIZE (mode);
2087 if ((bitpos % unit) + bitsize <= unit)
2088 break;
2091 if (mode == VOIDmode
2092 /* It is tempting to omit the following line
2093 if STRICT_ALIGNMENT is true.
2094 But that is incorrect, since if the bitfield uses part of 3 bytes
2095 and we use a 4-byte mode, we could get a spurious segv
2096 if the extra 4th byte is past the end of memory.
2097 (Though at least one Unix compiler ignores this problem:
2098 that on the Sequent 386 machine. */
2099 || MIN (unit, BIGGEST_ALIGNMENT) > align
2100 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
2101 return VOIDmode;
2103 if (SLOW_BYTE_ACCESS && ! volatilep)
2105 enum machine_mode wide_mode = VOIDmode, tmode;
2107 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
2108 tmode = GET_MODE_WIDER_MODE (tmode))
2110 unit = GET_MODE_BITSIZE (tmode);
2111 if (bitpos / unit == (bitpos + bitsize - 1) / unit
2112 && unit <= BITS_PER_WORD
2113 && unit <= MIN (align, BIGGEST_ALIGNMENT)
2114 && (largest_mode == VOIDmode
2115 || unit <= GET_MODE_BITSIZE (largest_mode)))
2116 wide_mode = tmode;
2119 if (wide_mode != VOIDmode)
2120 return wide_mode;
2123 return mode;
2126 /* Gets minimal and maximal values for MODE (signed or unsigned depending on
2127 SIGN). The returned constants are made to be usable in TARGET_MODE. */
2129 void
2130 get_mode_bounds (enum machine_mode mode, int sign,
2131 enum machine_mode target_mode,
2132 rtx *mmin, rtx *mmax)
2134 unsigned size = GET_MODE_BITSIZE (mode);
2135 unsigned HOST_WIDE_INT min_val, max_val;
2137 if (size > HOST_BITS_PER_WIDE_INT)
2138 abort ();
2140 if (sign)
2142 min_val = -((unsigned HOST_WIDE_INT) 1 << (size - 1));
2143 max_val = ((unsigned HOST_WIDE_INT) 1 << (size - 1)) - 1;
2145 else
2147 min_val = 0;
2148 max_val = ((unsigned HOST_WIDE_INT) 1 << (size - 1) << 1) - 1;
2151 *mmin = GEN_INT (trunc_int_for_mode (min_val, target_mode));
2152 *mmax = GEN_INT (trunc_int_for_mode (max_val, target_mode));
2155 #include "gt-stor-layout.h"