Daily bump.
[official-gcc.git] / gcc / stor-layout.c
blobcb55d901669cf144f8b16dd8162548e8ad40bd05
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 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"
33 #include "target.h"
35 /* Set to one when set_sizetype has been called. */
36 static int sizetype_set;
38 /* List of types created before set_sizetype has been called. We do not
39 make this a GGC root since we want these nodes to be reclaimed. */
40 static tree early_type_list;
42 /* Data type for the expressions representing sizes of data types.
43 It is the first integer type laid out. */
44 tree sizetype_tab[(int) TYPE_KIND_LAST];
46 /* If nonzero, this is an upper limit on alignment of structure fields.
47 The value is measured in bits. */
48 unsigned int maximum_field_alignment;
50 /* If non-zero, the alignment of a bitstring or (power-)set value, in bits.
51 May be overridden by front-ends. */
52 unsigned int set_alignment = 0;
54 /* Nonzero if all REFERENCE_TYPEs are internal and hence should be
55 allocated in Pmode, not ptr_mode. Set only by internal_reference_types
56 called only by a front end. */
57 static int reference_types_internal = 0;
59 static void finalize_record_size PARAMS ((record_layout_info));
60 static void finalize_type_size PARAMS ((tree));
61 static void place_union_field PARAMS ((record_layout_info, tree));
62 extern void debug_rli PARAMS ((record_layout_info));
64 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
66 static tree pending_sizes;
68 /* Nonzero means cannot safely call expand_expr now,
69 so put variable sizes onto `pending_sizes' instead. */
71 int immediate_size_expand;
73 /* Show that REFERENCE_TYPES are internal and should be Pmode. Called only
74 by front end. */
76 void
77 internal_reference_types ()
79 reference_types_internal = 1;
82 /* Get a list of all the objects put on the pending sizes list. */
84 tree
85 get_pending_sizes ()
87 tree chain = pending_sizes;
88 tree t;
90 /* Put each SAVE_EXPR into the current function. */
91 for (t = chain; t; t = TREE_CHAIN (t))
92 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
94 pending_sizes = 0;
95 return chain;
98 /* Return non-zero if EXPR is present on the pending sizes list. */
101 is_pending_size (expr)
102 tree expr;
104 tree t;
106 for (t = pending_sizes; t; t = TREE_CHAIN (t))
107 if (TREE_VALUE (t) == expr)
108 return 1;
109 return 0;
112 /* Add EXPR to the pending sizes list. */
114 void
115 put_pending_size (expr)
116 tree expr;
118 /* Strip any simple arithmetic from EXPR to see if it has an underlying
119 SAVE_EXPR. */
120 while (TREE_CODE_CLASS (TREE_CODE (expr)) == '1'
121 || (TREE_CODE_CLASS (TREE_CODE (expr)) == '2'
122 && TREE_CONSTANT (TREE_OPERAND (expr, 1))))
123 expr = TREE_OPERAND (expr, 0);
125 if (TREE_CODE (expr) == SAVE_EXPR)
126 pending_sizes = tree_cons (NULL_TREE, expr, pending_sizes);
129 /* Put a chain of objects into the pending sizes list, which must be
130 empty. */
132 void
133 put_pending_sizes (chain)
134 tree chain;
136 if (pending_sizes)
137 abort ();
139 pending_sizes = chain;
142 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
143 to serve as the actual size-expression for a type or decl. */
145 tree
146 variable_size (size)
147 tree size;
149 /* If the language-processor is to take responsibility for variable-sized
150 items (e.g., languages which have elaboration procedures like Ada),
151 just return SIZE unchanged. Likewise for self-referential sizes and
152 constant sizes. */
153 if (TREE_CONSTANT (size)
154 || global_bindings_p () < 0 || contains_placeholder_p (size))
155 return size;
157 size = save_expr (size);
159 /* If an array with a variable number of elements is declared, and
160 the elements require destruction, we will emit a cleanup for the
161 array. That cleanup is run both on normal exit from the block
162 and in the exception-handler for the block. Normally, when code
163 is used in both ordinary code and in an exception handler it is
164 `unsaved', i.e., all SAVE_EXPRs are recalculated. However, we do
165 not wish to do that here; the array-size is the same in both
166 places. */
167 if (TREE_CODE (size) == SAVE_EXPR)
168 SAVE_EXPR_PERSISTENT_P (size) = 1;
170 if (global_bindings_p ())
172 if (TREE_CONSTANT (size))
173 error ("type size can't be explicitly evaluated");
174 else
175 error ("variable-size type declared outside of any function");
177 return size_one_node;
180 if (immediate_size_expand)
181 /* NULL_RTX is not defined; neither is the rtx type.
182 Also, we would like to pass const0_rtx here, but don't have it. */
183 expand_expr (size, expand_expr (integer_zero_node, NULL_RTX, VOIDmode, 0),
184 VOIDmode, 0);
185 else if (cfun != 0 && cfun->x_dont_save_pending_sizes_p)
186 /* The front-end doesn't want us to keep a list of the expressions
187 that determine sizes for variable size objects. */
189 else
190 put_pending_size (size);
192 return size;
195 #ifndef MAX_FIXED_MODE_SIZE
196 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
197 #endif
199 /* Return the machine mode to use for a nonscalar of SIZE bits.
200 The mode must be in class CLASS, and have exactly that many bits.
201 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
202 be used. */
204 enum machine_mode
205 mode_for_size (size, class, limit)
206 unsigned int size;
207 enum mode_class class;
208 int limit;
210 enum machine_mode mode;
212 if (limit && size > MAX_FIXED_MODE_SIZE)
213 return BLKmode;
215 /* Get the first mode which has this size, in the specified class. */
216 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
217 mode = GET_MODE_WIDER_MODE (mode))
218 if (GET_MODE_BITSIZE (mode) == size)
219 return mode;
221 return BLKmode;
224 /* Similar, except passed a tree node. */
226 enum machine_mode
227 mode_for_size_tree (size, class, limit)
228 tree size;
229 enum mode_class class;
230 int limit;
232 if (TREE_CODE (size) != INTEGER_CST
233 /* What we really want to say here is that the size can fit in a
234 host integer, but we know there's no way we'd find a mode for
235 this many bits, so there's no point in doing the precise test. */
236 || compare_tree_int (size, 1000) > 0)
237 return BLKmode;
238 else
239 return mode_for_size (TREE_INT_CST_LOW (size), class, limit);
242 /* Similar, but never return BLKmode; return the narrowest mode that
243 contains at least the requested number of bits. */
245 enum machine_mode
246 smallest_mode_for_size (size, class)
247 unsigned int size;
248 enum mode_class class;
250 enum machine_mode mode;
252 /* Get the first mode which has at least this size, in the
253 specified class. */
254 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
255 mode = GET_MODE_WIDER_MODE (mode))
256 if (GET_MODE_BITSIZE (mode) >= size)
257 return mode;
259 abort ();
262 /* Find an integer mode of the exact same size, or BLKmode on failure. */
264 enum machine_mode
265 int_mode_for_mode (mode)
266 enum machine_mode mode;
268 switch (GET_MODE_CLASS (mode))
270 case MODE_INT:
271 case MODE_PARTIAL_INT:
272 break;
274 case MODE_COMPLEX_INT:
275 case MODE_COMPLEX_FLOAT:
276 case MODE_FLOAT:
277 case MODE_VECTOR_INT:
278 case MODE_VECTOR_FLOAT:
279 mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
280 break;
282 case MODE_RANDOM:
283 if (mode == BLKmode)
284 break;
286 /* ... fall through ... */
288 case MODE_CC:
289 default:
290 abort ();
293 return mode;
296 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
297 This can only be applied to objects of a sizetype. */
299 tree
300 round_up (value, divisor)
301 tree value;
302 int divisor;
304 tree arg = size_int_type (divisor, TREE_TYPE (value));
306 return size_binop (MULT_EXPR, size_binop (CEIL_DIV_EXPR, value, arg), arg);
309 /* Likewise, but round down. */
311 tree
312 round_down (value, divisor)
313 tree value;
314 int divisor;
316 tree arg = size_int_type (divisor, TREE_TYPE (value));
318 return size_binop (MULT_EXPR, size_binop (FLOOR_DIV_EXPR, value, arg), arg);
321 /* Set the size, mode and alignment of a ..._DECL node.
322 TYPE_DECL does need this for C++.
323 Note that LABEL_DECL and CONST_DECL nodes do not need this,
324 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
325 Don't call layout_decl for them.
327 KNOWN_ALIGN is the amount of alignment we can assume this
328 decl has with no special effort. It is relevant only for FIELD_DECLs
329 and depends on the previous fields.
330 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
331 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
332 the record will be aligned to suit. */
334 void
335 layout_decl (decl, known_align)
336 tree decl;
337 unsigned int known_align;
339 tree type = TREE_TYPE (decl);
340 enum tree_code code = TREE_CODE (decl);
342 if (code == CONST_DECL)
343 return;
344 else if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
345 && code != TYPE_DECL && code != FIELD_DECL)
346 abort ();
348 if (type == error_mark_node)
349 type = void_type_node;
351 /* Usually the size and mode come from the data type without change,
352 however, the front-end may set the explicit width of the field, so its
353 size may not be the same as the size of its type. This happens with
354 bitfields, of course (an `int' bitfield may be only 2 bits, say), but it
355 also happens with other fields. For example, the C++ front-end creates
356 zero-sized fields corresponding to empty base classes, and depends on
357 layout_type setting DECL_FIELD_BITPOS correctly for the field. Set the
358 size in bytes from the size in bits. If we have already set the mode,
359 don't set it again since we can be called twice for FIELD_DECLs. */
361 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
362 if (DECL_MODE (decl) == VOIDmode)
363 DECL_MODE (decl) = TYPE_MODE (type);
365 if (DECL_SIZE (decl) == 0)
367 DECL_SIZE (decl) = TYPE_SIZE (type);
368 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
370 else
371 DECL_SIZE_UNIT (decl)
372 = convert (sizetype, size_binop (CEIL_DIV_EXPR, DECL_SIZE (decl),
373 bitsize_unit_node));
375 /* Force alignment required for the data type.
376 But if the decl itself wants greater alignment, don't override that.
377 Likewise, if the decl is packed, don't override it. */
378 if (! (code == FIELD_DECL && DECL_BIT_FIELD (decl))
379 && (DECL_ALIGN (decl) == 0
380 || (! (code == FIELD_DECL && DECL_PACKED (decl))
381 && TYPE_ALIGN (type) > DECL_ALIGN (decl))))
383 DECL_ALIGN (decl) = TYPE_ALIGN (type);
384 DECL_USER_ALIGN (decl) = 0;
387 /* For fields, set the bit field type and update the alignment. */
388 if (code == FIELD_DECL)
390 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
391 if (maximum_field_alignment != 0)
392 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
394 /* If the field is of variable size, we can't misalign it since we
395 have no way to make a temporary to align the result. But this
396 isn't an issue if the decl is not addressable. Likewise if it
397 is of unknown size. */
398 else if (DECL_PACKED (decl)
399 && (DECL_NONADDRESSABLE_P (decl)
400 || DECL_SIZE_UNIT (decl) == 0
401 || TREE_CODE (DECL_SIZE_UNIT (decl)) == INTEGER_CST))
403 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
404 DECL_USER_ALIGN (decl) = 0;
408 /* See if we can use an ordinary integer mode for a bit-field.
409 Conditions are: a fixed size that is correct for another mode
410 and occupying a complete byte or bytes on proper boundary. */
411 if (code == FIELD_DECL && DECL_BIT_FIELD (decl)
412 && TYPE_SIZE (type) != 0
413 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
414 && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
416 enum machine_mode xmode
417 = mode_for_size_tree (DECL_SIZE (decl), MODE_INT, 1);
419 if (xmode != BLKmode && known_align >= GET_MODE_ALIGNMENT (xmode))
421 DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
422 DECL_ALIGN (decl));
423 DECL_MODE (decl) = xmode;
424 DECL_BIT_FIELD (decl) = 0;
428 /* Turn off DECL_BIT_FIELD if we won't need it set. */
429 if (code == FIELD_DECL && DECL_BIT_FIELD (decl)
430 && TYPE_MODE (type) == BLKmode && DECL_MODE (decl) == BLKmode
431 && known_align >= TYPE_ALIGN (type)
432 && DECL_ALIGN (decl) >= TYPE_ALIGN (type)
433 && DECL_SIZE_UNIT (decl) != 0)
434 DECL_BIT_FIELD (decl) = 0;
436 /* Evaluate nonconstant size only once, either now or as soon as safe. */
437 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
438 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
439 if (DECL_SIZE_UNIT (decl) != 0
440 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST)
441 DECL_SIZE_UNIT (decl) = variable_size (DECL_SIZE_UNIT (decl));
443 /* If requested, warn about definitions of large data objects. */
444 if (warn_larger_than
445 && (code == VAR_DECL || code == PARM_DECL)
446 && ! DECL_EXTERNAL (decl))
448 tree size = DECL_SIZE_UNIT (decl);
450 if (size != 0 && TREE_CODE (size) == INTEGER_CST
451 && compare_tree_int (size, larger_than_size) > 0)
453 unsigned int size_as_int = TREE_INT_CST_LOW (size);
455 if (compare_tree_int (size, size_as_int) == 0)
456 warning_with_decl (decl, "size of `%s' is %d bytes", size_as_int);
457 else
458 warning_with_decl (decl, "size of `%s' is larger than %d bytes",
459 larger_than_size);
464 /* Hook for a front-end function that can modify the record layout as needed
465 immediately before it is finalized. */
467 void (*lang_adjust_rli) PARAMS ((record_layout_info)) = 0;
469 void
470 set_lang_adjust_rli (f)
471 void (*f) PARAMS ((record_layout_info));
473 lang_adjust_rli = f;
476 /* Begin laying out type T, which may be a RECORD_TYPE, UNION_TYPE, or
477 QUAL_UNION_TYPE. Return a pointer to a struct record_layout_info which
478 is to be passed to all other layout functions for this record. It is the
479 responsibility of the caller to call `free' for the storage returned.
480 Note that garbage collection is not permitted until we finish laying
481 out the record. */
483 record_layout_info
484 start_record_layout (t)
485 tree t;
487 record_layout_info rli
488 = (record_layout_info) xmalloc (sizeof (struct record_layout_info_s));
490 rli->t = t;
492 /* If the type has a minimum specified alignment (via an attribute
493 declaration, for example) use it -- otherwise, start with a
494 one-byte alignment. */
495 rli->record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (t));
496 rli->unpacked_align = rli->unpadded_align = rli->record_align;
497 rli->offset_align = MAX (rli->record_align, BIGGEST_ALIGNMENT);
499 #ifdef STRUCTURE_SIZE_BOUNDARY
500 /* Packed structures don't need to have minimum size. */
501 if (! TYPE_PACKED (t))
502 rli->record_align = MAX (rli->record_align, STRUCTURE_SIZE_BOUNDARY);
503 #endif
505 rli->offset = size_zero_node;
506 rli->bitpos = bitsize_zero_node;
507 rli->prev_field = 0;
508 rli->pending_statics = 0;
509 rli->packed_maybe_necessary = 0;
511 return rli;
514 /* These four routines perform computations that convert between
515 the offset/bitpos forms and byte and bit offsets. */
517 tree
518 bit_from_pos (offset, bitpos)
519 tree offset, bitpos;
521 return size_binop (PLUS_EXPR, bitpos,
522 size_binop (MULT_EXPR, convert (bitsizetype, offset),
523 bitsize_unit_node));
526 tree
527 byte_from_pos (offset, bitpos)
528 tree offset, bitpos;
530 return size_binop (PLUS_EXPR, offset,
531 convert (sizetype,
532 size_binop (TRUNC_DIV_EXPR, bitpos,
533 bitsize_unit_node)));
536 void
537 pos_from_byte (poffset, pbitpos, off_align, pos)
538 tree *poffset, *pbitpos;
539 unsigned int off_align;
540 tree pos;
542 *poffset
543 = size_binop (MULT_EXPR,
544 convert (sizetype,
545 size_binop (FLOOR_DIV_EXPR, pos,
546 bitsize_int (off_align
547 / BITS_PER_UNIT))),
548 size_int (off_align / BITS_PER_UNIT));
549 *pbitpos = size_binop (MULT_EXPR,
550 size_binop (FLOOR_MOD_EXPR, pos,
551 bitsize_int (off_align / BITS_PER_UNIT)),
552 bitsize_unit_node);
555 void
556 pos_from_bit (poffset, pbitpos, off_align, pos)
557 tree *poffset, *pbitpos;
558 unsigned int off_align;
559 tree pos;
561 *poffset = size_binop (MULT_EXPR,
562 convert (sizetype,
563 size_binop (FLOOR_DIV_EXPR, pos,
564 bitsize_int (off_align))),
565 size_int (off_align / BITS_PER_UNIT));
566 *pbitpos = size_binop (FLOOR_MOD_EXPR, pos, bitsize_int (off_align));
569 /* Given a pointer to bit and byte offsets and an offset alignment,
570 normalize the offsets so they are within the alignment. */
572 void
573 normalize_offset (poffset, pbitpos, off_align)
574 tree *poffset, *pbitpos;
575 unsigned int off_align;
577 /* If the bit position is now larger than it should be, adjust it
578 downwards. */
579 if (compare_tree_int (*pbitpos, off_align) >= 0)
581 tree extra_aligns = size_binop (FLOOR_DIV_EXPR, *pbitpos,
582 bitsize_int (off_align));
584 *poffset
585 = size_binop (PLUS_EXPR, *poffset,
586 size_binop (MULT_EXPR, convert (sizetype, extra_aligns),
587 size_int (off_align / BITS_PER_UNIT)));
589 *pbitpos
590 = size_binop (FLOOR_MOD_EXPR, *pbitpos, bitsize_int (off_align));
594 /* Print debugging information about the information in RLI. */
596 void
597 debug_rli (rli)
598 record_layout_info rli;
600 print_node_brief (stderr, "type", rli->t, 0);
601 print_node_brief (stderr, "\noffset", rli->offset, 0);
602 print_node_brief (stderr, " bitpos", rli->bitpos, 0);
604 fprintf (stderr, "\naligns: rec = %u, unpack = %u, unpad = %u, off = %u\n",
605 rli->record_align, rli->unpacked_align, rli->unpadded_align,
606 rli->offset_align);
607 if (rli->packed_maybe_necessary)
608 fprintf (stderr, "packed may be necessary\n");
610 if (rli->pending_statics)
612 fprintf (stderr, "pending statics:\n");
613 debug_tree (rli->pending_statics);
617 /* Given an RLI with a possibly-incremented BITPOS, adjust OFFSET and
618 BITPOS if necessary to keep BITPOS below OFFSET_ALIGN. */
620 void
621 normalize_rli (rli)
622 record_layout_info rli;
624 normalize_offset (&rli->offset, &rli->bitpos, rli->offset_align);
627 /* Returns the size in bytes allocated so far. */
629 tree
630 rli_size_unit_so_far (rli)
631 record_layout_info rli;
633 return byte_from_pos (rli->offset, rli->bitpos);
636 /* Returns the size in bits allocated so far. */
638 tree
639 rli_size_so_far (rli)
640 record_layout_info rli;
642 return bit_from_pos (rli->offset, rli->bitpos);
645 /* Called from place_field to handle unions. */
647 static void
648 place_union_field (rli, field)
649 record_layout_info rli;
650 tree field;
652 unsigned int desired_align;
654 layout_decl (field, 0);
656 DECL_FIELD_OFFSET (field) = size_zero_node;
657 DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
658 SET_DECL_OFFSET_ALIGN (field, BIGGEST_ALIGNMENT);
660 desired_align = DECL_ALIGN (field);
662 #ifdef BIGGEST_FIELD_ALIGNMENT
663 /* Some targets (i.e. i386) limit union field alignment
664 to a lower boundary than alignment of variables unless
665 it was overridden by attribute aligned. */
666 if (! DECL_USER_ALIGN (field))
667 desired_align =
668 MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
669 #endif
671 #ifdef ADJUST_FIELD_ALIGN
672 desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
673 #endif
675 TYPE_USER_ALIGN (rli->t) |= DECL_USER_ALIGN (field);
677 /* Union must be at least as aligned as any field requires. */
678 rli->record_align = MAX (rli->record_align, desired_align);
679 rli->unpadded_align = MAX (rli->unpadded_align, desired_align);
681 #ifdef PCC_BITFIELD_TYPE_MATTERS
682 /* On the m88000, a bit field of declare type `int' forces the
683 entire union to have `int' alignment. */
684 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
686 rli->record_align = MAX (rli->record_align,
687 TYPE_ALIGN (TREE_TYPE (field)));
688 rli->unpadded_align = MAX (rli->unpadded_align,
689 TYPE_ALIGN (TREE_TYPE (field)));
691 #endif
693 /* We assume the union's size will be a multiple of a byte so we don't
694 bother with BITPOS. */
695 if (TREE_CODE (rli->t) == UNION_TYPE)
696 rli->offset = size_binop (MAX_EXPR, rli->offset, DECL_SIZE_UNIT (field));
697 else if (TREE_CODE (rli->t) == QUAL_UNION_TYPE)
698 rli->offset = fold (build (COND_EXPR, sizetype,
699 DECL_QUALIFIER (field),
700 DECL_SIZE_UNIT (field), rli->offset));
703 /* RLI contains information about the layout of a RECORD_TYPE. FIELD
704 is a FIELD_DECL to be added after those fields already present in
705 T. (FIELD is not actually added to the TYPE_FIELDS list here;
706 callers that desire that behavior must manually perform that step.) */
708 void
709 place_field (rli, field)
710 record_layout_info rli;
711 tree field;
713 /* The alignment required for FIELD. */
714 unsigned int desired_align;
715 /* The alignment FIELD would have if we just dropped it into the
716 record as it presently stands. */
717 unsigned int known_align;
718 unsigned int actual_align;
719 unsigned int user_align;
720 /* The type of this field. */
721 tree type = TREE_TYPE (field);
723 if (TREE_CODE (field) == ERROR_MARK || TREE_CODE (type) == ERROR_MARK)
724 return;
726 /* If FIELD is static, then treat it like a separate variable, not
727 really like a structure field. If it is a FUNCTION_DECL, it's a
728 method. In both cases, all we do is lay out the decl, and we do
729 it *after* the record is laid out. */
730 if (TREE_CODE (field) == VAR_DECL)
732 rli->pending_statics = tree_cons (NULL_TREE, field,
733 rli->pending_statics);
734 return;
737 /* Enumerators and enum types which are local to this class need not
738 be laid out. Likewise for initialized constant fields. */
739 else if (TREE_CODE (field) != FIELD_DECL)
740 return;
742 /* Unions are laid out very differently than records, so split
743 that code off to another function. */
744 else if (TREE_CODE (rli->t) != RECORD_TYPE)
746 place_union_field (rli, field);
747 return;
750 /* Work out the known alignment so far. Note that A & (-A) is the
751 value of the least-significant bit in A that is one. */
752 if (! integer_zerop (rli->bitpos))
753 known_align = (tree_low_cst (rli->bitpos, 1)
754 & - tree_low_cst (rli->bitpos, 1));
755 else if (integer_zerop (rli->offset))
756 known_align = BIGGEST_ALIGNMENT;
757 else if (host_integerp (rli->offset, 1))
758 known_align = (BITS_PER_UNIT
759 * (tree_low_cst (rli->offset, 1)
760 & - tree_low_cst (rli->offset, 1)));
761 else
762 known_align = rli->offset_align;
764 /* Lay out the field so we know what alignment it needs. For a
765 packed field, use the alignment as specified, disregarding what
766 the type would want. */
767 desired_align = DECL_ALIGN (field);
768 user_align = DECL_USER_ALIGN (field);
769 layout_decl (field, known_align);
770 if (! DECL_PACKED (field))
772 desired_align = DECL_ALIGN (field);
773 user_align = DECL_USER_ALIGN (field);
776 /* Some targets (i.e. i386, VMS) limit struct field alignment
777 to a lower boundary than alignment of variables unless
778 it was overridden by attribute aligned. */
779 #ifdef BIGGEST_FIELD_ALIGNMENT
780 if (! user_align)
781 desired_align
782 = MIN (desired_align, (unsigned) BIGGEST_FIELD_ALIGNMENT);
783 #endif
785 #ifdef ADJUST_FIELD_ALIGN
786 desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
787 #endif
789 /* Record must have at least as much alignment as any field.
790 Otherwise, the alignment of the field within the record is
791 meaningless. */
792 if ((* targetm.ms_bitfield_layout_p) (rli->t)
793 && type != error_mark_node
794 && DECL_BIT_FIELD_TYPE (field)
795 && ! integer_zerop (TYPE_SIZE (type)))
797 /* Here, the alignment of the underlying type of a bitfield can
798 affect the alignment of a record; even a zero-sized field
799 can do this. The alignment should be to the alignment of
800 the type, except that for zero-size bitfields this only
801 applies if there was an immediately prior, non-zero-size
802 bitfield. (That's the way it is, experimentally.) */
803 if (! integer_zerop (DECL_SIZE (field))
804 || (rli->prev_field
805 && DECL_BIT_FIELD_TYPE (rli->prev_field)
806 && ! integer_zerop (DECL_SIZE (rli->prev_field))))
808 rli->record_align = MAX (rli->record_align, TYPE_ALIGN(type));
809 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
811 else
812 desired_align = 1;
814 else
815 #ifdef PCC_BITFIELD_TYPE_MATTERS
816 if (PCC_BITFIELD_TYPE_MATTERS && type != error_mark_node
817 && ! (* targetm.ms_bitfield_layout_p) (rli->t)
818 && DECL_BIT_FIELD_TYPE (field)
819 && ! integer_zerop (TYPE_SIZE (type)))
821 /* For these machines, a zero-length field does not
822 affect the alignment of the structure as a whole.
823 It does, however, affect the alignment of the next field
824 within the structure. */
825 if (! integer_zerop (DECL_SIZE (field)))
826 rli->record_align = MAX (rli->record_align, desired_align);
827 else if (! DECL_PACKED (field))
828 desired_align = TYPE_ALIGN (type);
830 /* A named bit field of declared type `int'
831 forces the entire structure to have `int' alignment. */
832 if (DECL_NAME (field) != 0)
834 unsigned int type_align = TYPE_ALIGN (type);
836 if (maximum_field_alignment != 0)
837 type_align = MIN (type_align, maximum_field_alignment);
838 else if (DECL_PACKED (field))
839 type_align = MIN (type_align, BITS_PER_UNIT);
841 rli->record_align = MAX (rli->record_align, type_align);
842 rli->unpadded_align = MAX (rli->unpadded_align, DECL_ALIGN (field));
843 if (warn_packed)
844 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
847 else
848 #endif
850 rli->record_align = MAX (rli->record_align, desired_align);
851 rli->unpacked_align = MAX (rli->unpacked_align, TYPE_ALIGN (type));
852 rli->unpadded_align = MAX (rli->unpadded_align, DECL_ALIGN (field));
855 if (warn_packed && DECL_PACKED (field))
857 if (known_align > TYPE_ALIGN (type))
859 if (TYPE_ALIGN (type) > desired_align)
861 if (STRICT_ALIGNMENT)
862 warning_with_decl (field, "packed attribute causes inefficient alignment for `%s'");
863 else
864 warning_with_decl (field, "packed attribute is unnecessary for `%s'");
867 else
868 rli->packed_maybe_necessary = 1;
871 /* Does this field automatically have alignment it needs by virtue
872 of the fields that precede it and the record's own alignment? */
873 if (known_align < desired_align)
875 /* No, we need to skip space before this field.
876 Bump the cumulative size to multiple of field alignment. */
878 if (warn_padded)
879 warning_with_decl (field, "padding struct to align `%s'");
881 /* If the alignment is still within offset_align, just align
882 the bit position. */
883 if (desired_align < rli->offset_align)
884 rli->bitpos = round_up (rli->bitpos, desired_align);
885 else
887 /* First adjust OFFSET by the partial bits, then align. */
888 rli->offset
889 = size_binop (PLUS_EXPR, rli->offset,
890 convert (sizetype,
891 size_binop (CEIL_DIV_EXPR, rli->bitpos,
892 bitsize_unit_node)));
893 rli->bitpos = bitsize_zero_node;
895 rli->offset = round_up (rli->offset, desired_align / BITS_PER_UNIT);
898 if (! TREE_CONSTANT (rli->offset))
899 rli->offset_align = desired_align;
903 /* Handle compatibility with PCC. Note that if the record has any
904 variable-sized fields, we need not worry about compatibility. */
905 #ifdef PCC_BITFIELD_TYPE_MATTERS
906 if (PCC_BITFIELD_TYPE_MATTERS
907 && ! (* targetm.ms_bitfield_layout_p) (rli->t)
908 && TREE_CODE (field) == FIELD_DECL
909 && type != error_mark_node
910 && DECL_BIT_FIELD (field)
911 && ! DECL_PACKED (field)
912 && maximum_field_alignment == 0
913 && ! integer_zerop (DECL_SIZE (field))
914 && host_integerp (DECL_SIZE (field), 1)
915 && host_integerp (rli->offset, 1)
916 && host_integerp (TYPE_SIZE (type), 1))
918 unsigned int type_align = TYPE_ALIGN (type);
919 tree dsize = DECL_SIZE (field);
920 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
921 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
922 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
924 /* A bit field may not span more units of alignment of its type
925 than its type itself. Advance to next boundary if necessary. */
926 if ((((offset * BITS_PER_UNIT + bit_offset + field_size +
927 type_align - 1)
928 / type_align)
929 - (offset * BITS_PER_UNIT + bit_offset) / type_align)
930 > tree_low_cst (TYPE_SIZE (type), 1) / type_align)
931 rli->bitpos = round_up (rli->bitpos, type_align);
933 #endif
935 #ifdef BITFIELD_NBYTES_LIMITED
936 if (BITFIELD_NBYTES_LIMITED
937 && ! (* targetm.ms_bitfield_layout_p) (rli->t)
938 && TREE_CODE (field) == FIELD_DECL
939 && type != error_mark_node
940 && DECL_BIT_FIELD_TYPE (field)
941 && ! DECL_PACKED (field)
942 && ! integer_zerop (DECL_SIZE (field))
943 && host_integerp (DECL_SIZE (field), 1)
944 && host_integerp (rli->offset, 1)
945 && host_integerp (TYPE_SIZE (type), 1))
947 unsigned int type_align = TYPE_ALIGN (type);
948 tree dsize = DECL_SIZE (field);
949 HOST_WIDE_INT field_size = tree_low_cst (dsize, 1);
950 HOST_WIDE_INT offset = tree_low_cst (rli->offset, 0);
951 HOST_WIDE_INT bit_offset = tree_low_cst (rli->bitpos, 0);
953 if (maximum_field_alignment != 0)
954 type_align = MIN (type_align, maximum_field_alignment);
955 /* ??? This test is opposite the test in the containing if
956 statement, so this code is unreachable currently. */
957 else if (DECL_PACKED (field))
958 type_align = MIN (type_align, BITS_PER_UNIT);
960 /* A bit field may not span the unit of alignment of its type.
961 Advance to next boundary if necessary. */
962 /* ??? This code should match the code above for the
963 PCC_BITFIELD_TYPE_MATTERS case. */
964 if ((offset * BITS_PER_UNIT + bit_offset) / type_align
965 != ((offset * BITS_PER_UNIT + bit_offset + field_size - 1)
966 / type_align))
967 rli->bitpos = round_up (rli->bitpos, type_align);
969 #endif
971 /* See the docs for TARGET_MS_BITFIELD_LAYOUT_P for details. */
972 if ((* targetm.ms_bitfield_layout_p) (rli->t)
973 && TREE_CODE (field) == FIELD_DECL
974 && type != error_mark_node
975 && ! DECL_PACKED (field)
976 && rli->prev_field
977 && DECL_SIZE (field)
978 && host_integerp (DECL_SIZE (field), 1)
979 && DECL_SIZE (rli->prev_field)
980 && host_integerp (DECL_SIZE (rli->prev_field), 1)
981 && host_integerp (rli->offset, 1)
982 && host_integerp (TYPE_SIZE (type), 1)
983 && host_integerp (TYPE_SIZE (TREE_TYPE (rli->prev_field)), 1)
984 && ((DECL_BIT_FIELD_TYPE (rli->prev_field)
985 && ! integer_zerop (DECL_SIZE (rli->prev_field)))
986 || (DECL_BIT_FIELD_TYPE (field)
987 && ! integer_zerop (DECL_SIZE (field))))
988 && (! simple_cst_equal (TYPE_SIZE (type),
989 TYPE_SIZE (TREE_TYPE (rli->prev_field)))
990 /* If the previous field was a zero-sized bit-field, either
991 it was ignored, in which case we must ensure the proper
992 alignment of this field here, or it already forced the
993 alignment of this field, in which case forcing the
994 alignment again is harmless. So, do it in both cases. */
995 || (DECL_BIT_FIELD_TYPE (rli->prev_field)
996 && integer_zerop (DECL_SIZE (rli->prev_field)))))
998 unsigned int type_align = TYPE_ALIGN (type);
1000 if (rli->prev_field
1001 && DECL_BIT_FIELD_TYPE (rli->prev_field)
1002 /* If the previous bit-field is zero-sized, we've already
1003 accounted for its alignment needs (or ignored it, if
1004 appropriate) while placing it. */
1005 && ! integer_zerop (DECL_SIZE (rli->prev_field)))
1006 type_align = MAX (type_align,
1007 TYPE_ALIGN (TREE_TYPE (rli->prev_field)));
1009 if (maximum_field_alignment != 0)
1010 type_align = MIN (type_align, maximum_field_alignment);
1012 rli->bitpos = round_up (rli->bitpos, type_align);
1015 /* Offset so far becomes the position of this field after normalizing. */
1016 normalize_rli (rli);
1017 DECL_FIELD_OFFSET (field) = rli->offset;
1018 DECL_FIELD_BIT_OFFSET (field) = rli->bitpos;
1019 SET_DECL_OFFSET_ALIGN (field, rli->offset_align);
1021 TYPE_USER_ALIGN (rli->t) |= user_align;
1023 /* If this field ended up more aligned than we thought it would be (we
1024 approximate this by seeing if its position changed), lay out the field
1025 again; perhaps we can use an integral mode for it now. */
1026 if (! integer_zerop (DECL_FIELD_BIT_OFFSET (field)))
1027 actual_align = (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
1028 & - tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1));
1029 else if (integer_zerop (DECL_FIELD_OFFSET (field)))
1030 actual_align = BIGGEST_ALIGNMENT;
1031 else if (host_integerp (DECL_FIELD_OFFSET (field), 1))
1032 actual_align = (BITS_PER_UNIT
1033 * (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
1034 & - tree_low_cst (DECL_FIELD_OFFSET (field), 1)));
1035 else
1036 actual_align = DECL_OFFSET_ALIGN (field);
1038 if (known_align != actual_align)
1039 layout_decl (field, actual_align);
1041 rli->prev_field = field;
1043 /* Now add size of this field to the size of the record. If the size is
1044 not constant, treat the field as being a multiple of bytes and just
1045 adjust the offset, resetting the bit position. Otherwise, apportion the
1046 size amongst the bit position and offset. First handle the case of an
1047 unspecified size, which can happen when we have an invalid nested struct
1048 definition, such as struct j { struct j { int i; } }. The error message
1049 is printed in finish_struct. */
1050 if (DECL_SIZE (field) == 0)
1051 /* Do nothing. */;
1052 else if (TREE_CODE (DECL_SIZE_UNIT (field)) != INTEGER_CST
1053 || TREE_CONSTANT_OVERFLOW (DECL_SIZE_UNIT (field)))
1055 rli->offset
1056 = size_binop (PLUS_EXPR, rli->offset,
1057 convert (sizetype,
1058 size_binop (CEIL_DIV_EXPR, rli->bitpos,
1059 bitsize_unit_node)));
1060 rli->offset
1061 = size_binop (PLUS_EXPR, rli->offset, DECL_SIZE_UNIT (field));
1062 rli->bitpos = bitsize_zero_node;
1063 rli->offset_align = MIN (rli->offset_align, DECL_ALIGN (field));
1065 else
1067 rli->bitpos = size_binop (PLUS_EXPR, rli->bitpos, DECL_SIZE (field));
1068 normalize_rli (rli);
1072 /* Assuming that all the fields have been laid out, this function uses
1073 RLI to compute the final TYPE_SIZE, TYPE_ALIGN, etc. for the type
1074 inidicated by RLI. */
1076 static void
1077 finalize_record_size (rli)
1078 record_layout_info rli;
1080 tree unpadded_size, unpadded_size_unit;
1082 /* Now we want just byte and bit offsets, so set the offset alignment
1083 to be a byte and then normalize. */
1084 rli->offset_align = BITS_PER_UNIT;
1085 normalize_rli (rli);
1087 /* Determine the desired alignment. */
1088 #ifdef ROUND_TYPE_ALIGN
1089 TYPE_ALIGN (rli->t) = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t),
1090 rli->record_align);
1091 #else
1092 TYPE_ALIGN (rli->t) = MAX (TYPE_ALIGN (rli->t), rli->record_align);
1093 #endif
1095 /* Compute the size so far. Be sure to allow for extra bits in the
1096 size in bytes. We have guaranteed above that it will be no more
1097 than a single byte. */
1098 unpadded_size = rli_size_so_far (rli);
1099 unpadded_size_unit = rli_size_unit_so_far (rli);
1100 if (! integer_zerop (rli->bitpos))
1101 unpadded_size_unit
1102 = size_binop (PLUS_EXPR, unpadded_size_unit, size_one_node);
1104 /* Record the un-rounded size in the binfo node. But first we check
1105 the size of TYPE_BINFO to make sure that BINFO_SIZE is available. */
1106 if (TYPE_BINFO (rli->t) && TREE_VEC_LENGTH (TYPE_BINFO (rli->t)) > 6)
1108 TYPE_BINFO_SIZE (rli->t) = unpadded_size;
1109 TYPE_BINFO_SIZE_UNIT (rli->t) = unpadded_size_unit;
1112 /* Round the size up to be a multiple of the required alignment */
1113 #ifdef ROUND_TYPE_SIZE
1114 TYPE_SIZE (rli->t) = ROUND_TYPE_SIZE (rli->t, unpadded_size,
1115 TYPE_ALIGN (rli->t));
1116 TYPE_SIZE_UNIT (rli->t)
1117 = ROUND_TYPE_SIZE_UNIT (rli->t, unpadded_size_unit,
1118 TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
1119 #else
1120 TYPE_SIZE (rli->t) = round_up (unpadded_size, TYPE_ALIGN (rli->t));
1121 TYPE_SIZE_UNIT (rli->t) = round_up (unpadded_size_unit,
1122 TYPE_ALIGN (rli->t) / BITS_PER_UNIT);
1123 #endif
1125 if (warn_padded && TREE_CONSTANT (unpadded_size)
1126 && simple_cst_equal (unpadded_size, TYPE_SIZE (rli->t)) == 0)
1127 warning ("padding struct size to alignment boundary");
1129 if (warn_packed && TREE_CODE (rli->t) == RECORD_TYPE
1130 && TYPE_PACKED (rli->t) && ! rli->packed_maybe_necessary
1131 && TREE_CONSTANT (unpadded_size))
1133 tree unpacked_size;
1135 #ifdef ROUND_TYPE_ALIGN
1136 rli->unpacked_align
1137 = ROUND_TYPE_ALIGN (rli->t, TYPE_ALIGN (rli->t), rli->unpacked_align);
1138 #else
1139 rli->unpacked_align = MAX (TYPE_ALIGN (rli->t), rli->unpacked_align);
1140 #endif
1142 #ifdef ROUND_TYPE_SIZE
1143 unpacked_size = ROUND_TYPE_SIZE (rli->t, TYPE_SIZE (rli->t),
1144 rli->unpacked_align);
1145 #else
1146 unpacked_size = round_up (TYPE_SIZE (rli->t), rli->unpacked_align);
1147 #endif
1149 if (simple_cst_equal (unpacked_size, TYPE_SIZE (rli->t)))
1151 TYPE_PACKED (rli->t) = 0;
1153 if (TYPE_NAME (rli->t))
1155 const char *name;
1157 if (TREE_CODE (TYPE_NAME (rli->t)) == IDENTIFIER_NODE)
1158 name = IDENTIFIER_POINTER (TYPE_NAME (rli->t));
1159 else
1160 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (rli->t)));
1162 if (STRICT_ALIGNMENT)
1163 warning ("packed attribute causes inefficient alignment for `%s'", name);
1164 else
1165 warning ("packed attribute is unnecessary for `%s'", name);
1167 else
1169 if (STRICT_ALIGNMENT)
1170 warning ("packed attribute causes inefficient alignment");
1171 else
1172 warning ("packed attribute is unnecessary");
1178 /* Compute the TYPE_MODE for the TYPE (which is a RECORD_TYPE). */
1180 void
1181 compute_record_mode (type)
1182 tree type;
1184 tree field;
1185 enum machine_mode mode = VOIDmode;
1187 /* Most RECORD_TYPEs have BLKmode, so we start off assuming that.
1188 However, if possible, we use a mode that fits in a register
1189 instead, in order to allow for better optimization down the
1190 line. */
1191 TYPE_MODE (type) = BLKmode;
1193 if (! host_integerp (TYPE_SIZE (type), 1))
1194 return;
1196 /* A record which has any BLKmode members must itself be
1197 BLKmode; it can't go in a register. Unless the member is
1198 BLKmode only because it isn't aligned. */
1199 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1201 unsigned HOST_WIDE_INT bitpos;
1203 if (TREE_CODE (field) != FIELD_DECL)
1204 continue;
1206 if (TREE_CODE (TREE_TYPE (field)) == ERROR_MARK
1207 || (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1208 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1209 || ! host_integerp (bit_position (field), 1)
1210 || DECL_SIZE (field) == 0
1211 || ! host_integerp (DECL_SIZE (field), 1))
1212 return;
1214 bitpos = int_bit_position (field);
1216 /* Must be BLKmode if any field crosses a word boundary,
1217 since extract_bit_field can't handle that in registers. */
1218 if (bitpos / BITS_PER_WORD
1219 != ((tree_low_cst (DECL_SIZE (field), 1) + bitpos - 1)
1220 / BITS_PER_WORD)
1221 /* But there is no problem if the field is entire words. */
1222 && tree_low_cst (DECL_SIZE (field), 1) % BITS_PER_WORD != 0)
1223 return;
1225 /* If this field is the whole struct, remember its mode so
1226 that, say, we can put a double in a class into a DF
1227 register instead of forcing it to live in the stack. */
1228 if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
1229 mode = DECL_MODE (field);
1231 #ifdef MEMBER_TYPE_FORCES_BLK
1232 /* With some targets, eg. c4x, it is sub-optimal
1233 to access an aligned BLKmode structure as a scalar. */
1235 /* On ia64-*-hpux we need to ensure that we don't change the
1236 mode of a structure containing a single field or else we
1237 will pass it incorrectly. Since a structure with a single
1238 field causes mode to get set above we can't allow the
1239 check for mode == VOIDmode in this case. Perhaps
1240 MEMBER_TYPE_FORCES_BLK should be extended to include mode
1241 as an argument and the check could be put in there for c4x. */
1243 if ((mode == VOIDmode || FUNCTION_ARG_REG_LITTLE_ENDIAN)
1244 && MEMBER_TYPE_FORCES_BLK (field))
1245 return;
1246 #endif /* MEMBER_TYPE_FORCES_BLK */
1249 /* If we only have one real field; use its mode. This only applies to
1250 RECORD_TYPE. This does not apply to unions. */
1251 if (TREE_CODE (type) == RECORD_TYPE && mode != VOIDmode)
1252 TYPE_MODE (type) = mode;
1253 else
1254 TYPE_MODE (type) = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1256 /* If structure's known alignment is less than what the scalar
1257 mode would need, and it matters, then stick with BLKmode. */
1258 if (TYPE_MODE (type) != BLKmode
1259 && STRICT_ALIGNMENT
1260 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1261 || TYPE_ALIGN (type) >= GET_MODE_ALIGNMENT (TYPE_MODE (type))))
1263 /* If this is the only reason this type is BLKmode, then
1264 don't force containing types to be BLKmode. */
1265 TYPE_NO_FORCE_BLK (type) = 1;
1266 TYPE_MODE (type) = BLKmode;
1270 /* Compute TYPE_SIZE and TYPE_ALIGN for TYPE, once it has been laid
1271 out. */
1273 static void
1274 finalize_type_size (type)
1275 tree type;
1277 /* Normally, use the alignment corresponding to the mode chosen.
1278 However, where strict alignment is not required, avoid
1279 over-aligning structures, since most compilers do not do this
1280 alignment. */
1282 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1283 && (STRICT_ALIGNMENT
1284 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1285 && TREE_CODE (type) != QUAL_UNION_TYPE
1286 && TREE_CODE (type) != ARRAY_TYPE)))
1288 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1289 TYPE_USER_ALIGN (type) = 0;
1292 /* Do machine-dependent extra alignment. */
1293 #ifdef ROUND_TYPE_ALIGN
1294 TYPE_ALIGN (type)
1295 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1296 #endif
1298 /* If we failed to find a simple way to calculate the unit size
1299 of the type, find it by division. */
1300 if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1301 /* TYPE_SIZE (type) is computed in bitsizetype. After the division, the
1302 result will fit in sizetype. We will get more efficient code using
1303 sizetype, so we force a conversion. */
1304 TYPE_SIZE_UNIT (type)
1305 = convert (sizetype,
1306 size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1307 bitsize_unit_node));
1309 if (TYPE_SIZE (type) != 0)
1311 #ifdef ROUND_TYPE_SIZE
1312 TYPE_SIZE (type)
1313 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1314 TYPE_SIZE_UNIT (type)
1315 = ROUND_TYPE_SIZE_UNIT (type, TYPE_SIZE_UNIT (type),
1316 TYPE_ALIGN (type) / BITS_PER_UNIT);
1317 #else
1318 TYPE_SIZE (type) = round_up (TYPE_SIZE (type), TYPE_ALIGN (type));
1319 TYPE_SIZE_UNIT (type)
1320 = round_up (TYPE_SIZE_UNIT (type), TYPE_ALIGN (type) / BITS_PER_UNIT);
1321 #endif
1324 /* Evaluate nonconstant sizes only once, either now or as soon as safe. */
1325 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1326 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1327 if (TYPE_SIZE_UNIT (type) != 0
1328 && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1329 TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1331 /* Also layout any other variants of the type. */
1332 if (TYPE_NEXT_VARIANT (type)
1333 || type != TYPE_MAIN_VARIANT (type))
1335 tree variant;
1336 /* Record layout info of this variant. */
1337 tree size = TYPE_SIZE (type);
1338 tree size_unit = TYPE_SIZE_UNIT (type);
1339 unsigned int align = TYPE_ALIGN (type);
1340 unsigned int user_align = TYPE_USER_ALIGN (type);
1341 enum machine_mode mode = TYPE_MODE (type);
1343 /* Copy it into all variants. */
1344 for (variant = TYPE_MAIN_VARIANT (type);
1345 variant != 0;
1346 variant = TYPE_NEXT_VARIANT (variant))
1348 TYPE_SIZE (variant) = size;
1349 TYPE_SIZE_UNIT (variant) = size_unit;
1350 TYPE_ALIGN (variant) = align;
1351 TYPE_USER_ALIGN (variant) = user_align;
1352 TYPE_MODE (variant) = mode;
1357 /* Do all of the work required to layout the type indicated by RLI,
1358 once the fields have been laid out. This function will call `free'
1359 for RLI. */
1361 void
1362 finish_record_layout (rli)
1363 record_layout_info rli;
1365 /* Compute the final size. */
1366 finalize_record_size (rli);
1368 /* Compute the TYPE_MODE for the record. */
1369 compute_record_mode (rli->t);
1371 /* Perform any last tweaks to the TYPE_SIZE, etc. */
1372 finalize_type_size (rli->t);
1374 /* Lay out any static members. This is done now because their type
1375 may use the record's type. */
1376 while (rli->pending_statics)
1378 layout_decl (TREE_VALUE (rli->pending_statics), 0);
1379 rli->pending_statics = TREE_CHAIN (rli->pending_statics);
1382 /* Clean up. */
1383 free (rli);
1386 /* Calculate the mode, size, and alignment for TYPE.
1387 For an array type, calculate the element separation as well.
1388 Record TYPE on the chain of permanent or temporary types
1389 so that dbxout will find out about it.
1391 TYPE_SIZE of a type is nonzero if the type has been laid out already.
1392 layout_type does nothing on such a type.
1394 If the type is incomplete, its TYPE_SIZE remains zero. */
1396 void
1397 layout_type (type)
1398 tree type;
1400 if (type == 0)
1401 abort ();
1403 /* Do nothing if type has been laid out before. */
1404 if (TYPE_SIZE (type))
1405 return;
1407 switch (TREE_CODE (type))
1409 case LANG_TYPE:
1410 /* This kind of type is the responsibility
1411 of the language-specific code. */
1412 abort ();
1414 case BOOLEAN_TYPE: /* Used for Java, Pascal, and Chill. */
1415 if (TYPE_PRECISION (type) == 0)
1416 TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
1418 /* ... fall through ... */
1420 case INTEGER_TYPE:
1421 case ENUMERAL_TYPE:
1422 case CHAR_TYPE:
1423 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
1424 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
1425 TREE_UNSIGNED (type) = 1;
1427 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
1428 MODE_INT);
1429 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1430 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1431 break;
1433 case REAL_TYPE:
1434 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
1435 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1436 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1437 break;
1439 case COMPLEX_TYPE:
1440 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
1441 TYPE_MODE (type)
1442 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
1443 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
1444 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
1446 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1447 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1448 break;
1450 case VECTOR_TYPE:
1452 tree subtype;
1454 subtype = TREE_TYPE (type);
1455 TREE_UNSIGNED (type) = TREE_UNSIGNED (subtype);
1456 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
1457 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
1459 break;
1461 case VOID_TYPE:
1462 /* This is an incomplete type and so doesn't have a size. */
1463 TYPE_ALIGN (type) = 1;
1464 TYPE_USER_ALIGN (type) = 0;
1465 TYPE_MODE (type) = VOIDmode;
1466 break;
1468 case OFFSET_TYPE:
1469 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
1470 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
1471 /* A pointer might be MODE_PARTIAL_INT,
1472 but ptrdiff_t must be integral. */
1473 TYPE_MODE (type) = mode_for_size (POINTER_SIZE, MODE_INT, 0);
1474 break;
1476 case FUNCTION_TYPE:
1477 case METHOD_TYPE:
1478 TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
1479 TYPE_SIZE (type) = bitsize_int (2 * POINTER_SIZE);
1480 TYPE_SIZE_UNIT (type) = size_int ((2 * POINTER_SIZE) / BITS_PER_UNIT);
1481 break;
1483 case POINTER_TYPE:
1484 case REFERENCE_TYPE:
1486 int nbits = ((TREE_CODE (type) == REFERENCE_TYPE
1487 && reference_types_internal)
1488 ? GET_MODE_BITSIZE (Pmode) : POINTER_SIZE);
1490 TYPE_MODE (type) = nbits == POINTER_SIZE ? ptr_mode : Pmode;
1491 TYPE_SIZE (type) = bitsize_int (nbits);
1492 TYPE_SIZE_UNIT (type) = size_int (nbits / BITS_PER_UNIT);
1493 TREE_UNSIGNED (type) = 1;
1494 TYPE_PRECISION (type) = nbits;
1496 break;
1498 case ARRAY_TYPE:
1500 tree index = TYPE_DOMAIN (type);
1501 tree element = TREE_TYPE (type);
1503 build_pointer_type (element);
1505 /* We need to know both bounds in order to compute the size. */
1506 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
1507 && TYPE_SIZE (element))
1509 tree ub = TYPE_MAX_VALUE (index);
1510 tree lb = TYPE_MIN_VALUE (index);
1511 tree length;
1512 tree element_size;
1514 /* The initial subtraction should happen in the original type so
1515 that (possible) negative values are handled appropriately. */
1516 length = size_binop (PLUS_EXPR, size_one_node,
1517 convert (sizetype,
1518 fold (build (MINUS_EXPR,
1519 TREE_TYPE (lb),
1520 ub, lb))));
1522 /* Special handling for arrays of bits (for Chill). */
1523 element_size = TYPE_SIZE (element);
1524 if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element)
1525 && (integer_zerop (TYPE_MAX_VALUE (element))
1526 || integer_onep (TYPE_MAX_VALUE (element)))
1527 && host_integerp (TYPE_MIN_VALUE (element), 1))
1529 HOST_WIDE_INT maxvalue
1530 = tree_low_cst (TYPE_MAX_VALUE (element), 1);
1531 HOST_WIDE_INT minvalue
1532 = tree_low_cst (TYPE_MIN_VALUE (element), 1);
1534 if (maxvalue - minvalue == 1
1535 && (maxvalue == 1 || maxvalue == 0))
1536 element_size = integer_one_node;
1539 TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size,
1540 convert (bitsizetype, length));
1542 /* If we know the size of the element, calculate the total
1543 size directly, rather than do some division thing below.
1544 This optimization helps Fortran assumed-size arrays
1545 (where the size of the array is determined at runtime)
1546 substantially.
1547 Note that we can't do this in the case where the size of
1548 the elements is one bit since TYPE_SIZE_UNIT cannot be
1549 set correctly in that case. */
1550 if (TYPE_SIZE_UNIT (element) != 0 && ! integer_onep (element_size))
1551 TYPE_SIZE_UNIT (type)
1552 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
1555 /* Now round the alignment and size,
1556 using machine-dependent criteria if any. */
1558 #ifdef ROUND_TYPE_ALIGN
1559 TYPE_ALIGN (type)
1560 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
1561 #else
1562 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
1563 #endif
1564 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (element);
1566 #ifdef ROUND_TYPE_SIZE
1567 if (TYPE_SIZE (type) != 0)
1569 tree tmp
1570 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1572 /* If the rounding changed the size of the type, remove any
1573 pre-calculated TYPE_SIZE_UNIT. */
1574 if (simple_cst_equal (TYPE_SIZE (type), tmp) != 1)
1575 TYPE_SIZE_UNIT (type) = NULL;
1577 TYPE_SIZE (type) = tmp;
1579 #endif
1581 TYPE_MODE (type) = BLKmode;
1582 if (TYPE_SIZE (type) != 0
1583 #ifdef MEMBER_TYPE_FORCES_BLK
1584 && ! MEMBER_TYPE_FORCES_BLK (type)
1585 #endif
1586 /* BLKmode elements force BLKmode aggregate;
1587 else extract/store fields may lose. */
1588 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
1589 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
1591 /* One-element arrays get the component type's mode. */
1592 if (simple_cst_equal (TYPE_SIZE (type),
1593 TYPE_SIZE (TREE_TYPE (type))))
1594 TYPE_MODE (type) = TYPE_MODE (TREE_TYPE (type));
1595 else
1596 TYPE_MODE (type)
1597 = mode_for_size_tree (TYPE_SIZE (type), MODE_INT, 1);
1599 if (TYPE_MODE (type) != BLKmode
1600 && STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
1601 && TYPE_ALIGN (type) < GET_MODE_ALIGNMENT (TYPE_MODE (type))
1602 && TYPE_MODE (type) != BLKmode)
1604 TYPE_NO_FORCE_BLK (type) = 1;
1605 TYPE_MODE (type) = BLKmode;
1608 break;
1611 case RECORD_TYPE:
1612 case UNION_TYPE:
1613 case QUAL_UNION_TYPE:
1615 tree field;
1616 record_layout_info rli;
1618 /* Initialize the layout information. */
1619 rli = start_record_layout (type);
1621 /* If this is a QUAL_UNION_TYPE, we want to process the fields
1622 in the reverse order in building the COND_EXPR that denotes
1623 its size. We reverse them again later. */
1624 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1625 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1627 /* Place all the fields. */
1628 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1629 place_field (rli, field);
1631 if (TREE_CODE (type) == QUAL_UNION_TYPE)
1632 TYPE_FIELDS (type) = nreverse (TYPE_FIELDS (type));
1634 if (lang_adjust_rli)
1635 (*lang_adjust_rli) (rli);
1637 /* Finish laying out the record. */
1638 finish_record_layout (rli);
1640 break;
1642 case SET_TYPE: /* Used by Chill and Pascal. */
1643 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1644 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1645 abort ();
1646 else
1648 #ifndef SET_WORD_SIZE
1649 #define SET_WORD_SIZE BITS_PER_WORD
1650 #endif
1651 unsigned int alignment
1652 = set_alignment ? set_alignment : SET_WORD_SIZE;
1653 int size_in_bits
1654 = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1655 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
1656 int rounded_size
1657 = ((size_in_bits + alignment - 1) / alignment) * alignment;
1659 if (rounded_size > (int) alignment)
1660 TYPE_MODE (type) = BLKmode;
1661 else
1662 TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1664 TYPE_SIZE (type) = bitsize_int (rounded_size);
1665 TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1666 TYPE_ALIGN (type) = alignment;
1667 TYPE_USER_ALIGN (type) = 0;
1668 TYPE_PRECISION (type) = size_in_bits;
1670 break;
1672 case FILE_TYPE:
1673 /* The size may vary in different languages, so the language front end
1674 should fill in the size. */
1675 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1676 TYPE_USER_ALIGN (type) = 0;
1677 TYPE_MODE (type) = BLKmode;
1678 break;
1680 default:
1681 abort ();
1684 /* Compute the final TYPE_SIZE, TYPE_ALIGN, etc. for TYPE. For
1685 records and unions, finish_record_layout already called this
1686 function. */
1687 if (TREE_CODE (type) != RECORD_TYPE
1688 && TREE_CODE (type) != UNION_TYPE
1689 && TREE_CODE (type) != QUAL_UNION_TYPE)
1690 finalize_type_size (type);
1692 /* If this type is created before sizetype has been permanently set,
1693 record it so set_sizetype can fix it up. */
1694 if (! sizetype_set)
1695 early_type_list = tree_cons (NULL_TREE, type, early_type_list);
1697 /* If an alias set has been set for this aggregate when it was incomplete,
1698 force it into alias set 0.
1699 This is too conservative, but we cannot call record_component_aliases
1700 here because some frontends still change the aggregates after
1701 layout_type. */
1702 if (AGGREGATE_TYPE_P (type) && TYPE_ALIAS_SET_KNOWN_P (type))
1703 TYPE_ALIAS_SET (type) = 0;
1706 /* Create and return a type for signed integers of PRECISION bits. */
1708 tree
1709 make_signed_type (precision)
1710 int precision;
1712 tree type = make_node (INTEGER_TYPE);
1714 TYPE_PRECISION (type) = precision;
1716 fixup_signed_type (type);
1717 return type;
1720 /* Create and return a type for unsigned integers of PRECISION bits. */
1722 tree
1723 make_unsigned_type (precision)
1724 int precision;
1726 tree type = make_node (INTEGER_TYPE);
1728 TYPE_PRECISION (type) = precision;
1730 fixup_unsigned_type (type);
1731 return type;
1734 /* Initialize sizetype and bitsizetype to a reasonable and temporary
1735 value to enable integer types to be created. */
1737 void
1738 initialize_sizetypes ()
1740 tree t = make_node (INTEGER_TYPE);
1742 /* Set this so we do something reasonable for the build_int_2 calls
1743 below. */
1744 integer_type_node = t;
1746 TYPE_MODE (t) = SImode;
1747 TYPE_ALIGN (t) = GET_MODE_ALIGNMENT (SImode);
1748 TYPE_USER_ALIGN (t) = 0;
1749 TYPE_SIZE (t) = build_int_2 (GET_MODE_BITSIZE (SImode), 0);
1750 TYPE_SIZE_UNIT (t) = build_int_2 (GET_MODE_SIZE (SImode), 0);
1751 TREE_UNSIGNED (t) = 1;
1752 TYPE_PRECISION (t) = GET_MODE_BITSIZE (SImode);
1753 TYPE_MIN_VALUE (t) = build_int_2 (0, 0);
1754 TYPE_IS_SIZETYPE (t) = 1;
1756 /* 1000 avoids problems with possible overflow and is certainly
1757 larger than any size value we'd want to be storing. */
1758 TYPE_MAX_VALUE (t) = build_int_2 (1000, 0);
1760 /* These two must be different nodes because of the caching done in
1761 size_int_wide. */
1762 sizetype = t;
1763 bitsizetype = copy_node (t);
1764 integer_type_node = 0;
1767 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1768 Also update the type of any standard type's sizes made so far. */
1770 void
1771 set_sizetype (type)
1772 tree type;
1774 int oprecision = TYPE_PRECISION (type);
1775 /* The *bitsizetype types use a precision that avoids overflows when
1776 calculating signed sizes / offsets in bits. However, when
1777 cross-compiling from a 32 bit to a 64 bit host, we are limited to 64 bit
1778 precision. */
1779 int precision = MIN (oprecision + BITS_PER_UNIT_LOG + 1,
1780 2 * HOST_BITS_PER_WIDE_INT);
1781 unsigned int i;
1782 tree t;
1784 if (sizetype_set)
1785 abort ();
1787 /* Make copies of nodes since we'll be setting TYPE_IS_SIZETYPE. */
1788 sizetype = copy_node (type);
1789 TYPE_DOMAIN (sizetype) = type;
1790 TYPE_IS_SIZETYPE (sizetype) = 1;
1791 bitsizetype = make_node (INTEGER_TYPE);
1792 TYPE_NAME (bitsizetype) = TYPE_NAME (type);
1793 TYPE_PRECISION (bitsizetype) = precision;
1794 TYPE_IS_SIZETYPE (bitsizetype) = 1;
1796 if (TREE_UNSIGNED (type))
1797 fixup_unsigned_type (bitsizetype);
1798 else
1799 fixup_signed_type (bitsizetype);
1801 layout_type (bitsizetype);
1803 if (TREE_UNSIGNED (type))
1805 usizetype = sizetype;
1806 ubitsizetype = bitsizetype;
1807 ssizetype = copy_node (make_signed_type (oprecision));
1808 sbitsizetype = copy_node (make_signed_type (precision));
1810 else
1812 ssizetype = sizetype;
1813 sbitsizetype = bitsizetype;
1814 usizetype = copy_node (make_unsigned_type (oprecision));
1815 ubitsizetype = copy_node (make_unsigned_type (precision));
1818 TYPE_NAME (bitsizetype) = get_identifier ("bit_size_type");
1820 /* Show is a sizetype, is a main type, and has no pointers to it. */
1821 for (i = 0; i < ARRAY_SIZE (sizetype_tab); i++)
1823 TYPE_IS_SIZETYPE (sizetype_tab[i]) = 1;
1824 TYPE_MAIN_VARIANT (sizetype_tab[i]) = sizetype_tab[i];
1825 TYPE_NEXT_VARIANT (sizetype_tab[i]) = 0;
1826 TYPE_POINTER_TO (sizetype_tab[i]) = 0;
1827 TYPE_REFERENCE_TO (sizetype_tab[i]) = 0;
1830 ggc_add_tree_root ((tree *) &sizetype_tab,
1831 sizeof sizetype_tab / sizeof (tree));
1833 /* Go down each of the types we already made and set the proper type
1834 for the sizes in them. */
1835 for (t = early_type_list; t != 0; t = TREE_CHAIN (t))
1837 if (TREE_CODE (TREE_VALUE (t)) != INTEGER_TYPE)
1838 abort ();
1840 TREE_TYPE (TYPE_SIZE (TREE_VALUE (t))) = bitsizetype;
1841 TREE_TYPE (TYPE_SIZE_UNIT (TREE_VALUE (t))) = sizetype;
1844 early_type_list = 0;
1845 sizetype_set = 1;
1848 /* Set the extreme values of TYPE based on its precision in bits,
1849 then lay it out. Used when make_signed_type won't do
1850 because the tree code is not INTEGER_TYPE.
1851 E.g. for Pascal, when the -fsigned-char option is given. */
1853 void
1854 fixup_signed_type (type)
1855 tree type;
1857 int precision = TYPE_PRECISION (type);
1859 /* We can not represent properly constants greater then
1860 2 * HOST_BITS_PER_WIDE_INT, still we need the types
1861 as they are used by i386 vector extensions and friends. */
1862 if (precision > HOST_BITS_PER_WIDE_INT * 2)
1863 precision = HOST_BITS_PER_WIDE_INT * 2;
1865 TYPE_MIN_VALUE (type)
1866 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1867 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1868 (((HOST_WIDE_INT) (-1)
1869 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1870 ? precision - HOST_BITS_PER_WIDE_INT - 1
1871 : 0))));
1872 TYPE_MAX_VALUE (type)
1873 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1874 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1875 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1876 ? (((HOST_WIDE_INT) 1
1877 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1878 : 0));
1880 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1881 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1883 /* Lay out the type: set its alignment, size, etc. */
1884 layout_type (type);
1887 /* Set the extreme values of TYPE based on its precision in bits,
1888 then lay it out. This is used both in `make_unsigned_type'
1889 and for enumeral types. */
1891 void
1892 fixup_unsigned_type (type)
1893 tree type;
1895 int precision = TYPE_PRECISION (type);
1897 /* We can not represent properly constants greater then
1898 2 * HOST_BITS_PER_WIDE_INT, still we need the types
1899 as they are used by i386 vector extensions and friends. */
1900 if (precision > HOST_BITS_PER_WIDE_INT * 2)
1901 precision = HOST_BITS_PER_WIDE_INT * 2;
1903 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1904 TYPE_MAX_VALUE (type)
1905 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1906 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1907 precision - HOST_BITS_PER_WIDE_INT > 0
1908 ? ((unsigned HOST_WIDE_INT) ~0
1909 >> (HOST_BITS_PER_WIDE_INT
1910 - (precision - HOST_BITS_PER_WIDE_INT)))
1911 : 0);
1912 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1913 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1915 /* Lay out the type: set its alignment, size, etc. */
1916 layout_type (type);
1919 /* Find the best machine mode to use when referencing a bit field of length
1920 BITSIZE bits starting at BITPOS.
1922 The underlying object is known to be aligned to a boundary of ALIGN bits.
1923 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1924 larger than LARGEST_MODE (usually SImode).
1926 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1927 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1928 mode meeting these conditions.
1930 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1931 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1932 all the conditions. */
1934 enum machine_mode
1935 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1936 int bitsize, bitpos;
1937 unsigned int align;
1938 enum machine_mode largest_mode;
1939 int volatilep;
1941 enum machine_mode mode;
1942 unsigned int unit = 0;
1944 /* Find the narrowest integer mode that contains the bit field. */
1945 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1946 mode = GET_MODE_WIDER_MODE (mode))
1948 unit = GET_MODE_BITSIZE (mode);
1949 if ((bitpos % unit) + bitsize <= unit)
1950 break;
1953 if (mode == VOIDmode
1954 /* It is tempting to omit the following line
1955 if STRICT_ALIGNMENT is true.
1956 But that is incorrect, since if the bitfield uses part of 3 bytes
1957 and we use a 4-byte mode, we could get a spurious segv
1958 if the extra 4th byte is past the end of memory.
1959 (Though at least one Unix compiler ignores this problem:
1960 that on the Sequent 386 machine. */
1961 || MIN (unit, BIGGEST_ALIGNMENT) > align
1962 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1963 return VOIDmode;
1965 if (SLOW_BYTE_ACCESS && ! volatilep)
1967 enum machine_mode wide_mode = VOIDmode, tmode;
1969 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1970 tmode = GET_MODE_WIDER_MODE (tmode))
1972 unit = GET_MODE_BITSIZE (tmode);
1973 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1974 && unit <= BITS_PER_WORD
1975 && unit <= MIN (align, BIGGEST_ALIGNMENT)
1976 && (largest_mode == VOIDmode
1977 || unit <= GET_MODE_BITSIZE (largest_mode)))
1978 wide_mode = tmode;
1981 if (wide_mode != VOIDmode)
1982 return wide_mode;
1985 return mode;
1988 /* This function is run once to initialize stor-layout.c. */
1990 void
1991 init_stor_layout_once ()
1993 ggc_add_tree_root (&pending_sizes, 1);