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 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
34 /* Set to one when set_sizetype has been called. */
35 static int sizetype_set;
37 /* List of types created before set_sizetype has been called. We do not
38 make this a GGC root since we want these nodes to be reclaimed. */
39 static tree early_type_list;
41 /* Data type for the expressions representing sizes of data types.
42 It is the first integer type laid out. */
43 tree sizetype_tab[(int) TYPE_KIND_LAST];
45 /* If nonzero, this is an upper limit on alignment of structure fields.
46 The value is measured in bits. */
47 unsigned int maximum_field_alignment;
49 /* If non-zero, the alignment of a bitstring or (power-)set value, in bits.
50 May be overridden by front-ends. */
51 unsigned int set_alignment = 0;
53 static tree layout_record PARAMS ((tree));
54 static void layout_union PARAMS ((tree));
56 /* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */
58 static tree pending_sizes;
60 /* Nonzero means cannot safely call expand_expr now,
61 so put variable sizes onto `pending_sizes' instead. */
63 int immediate_size_expand;
68 tree chain = pending_sizes;
71 /* Put each SAVE_EXPR into the current function. */
72 for (t = chain; t; t = TREE_CHAIN (t))
73 SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl;
80 put_pending_sizes (chain)
86 pending_sizes = chain;
89 /* Given a size SIZE that may not be a constant, return a SAVE_EXPR
90 to serve as the actual size-expression for a type or decl. */
96 /* If the language-processor is to take responsibility for variable-sized
97 items (e.g., languages which have elaboration procedures like Ada),
98 just return SIZE unchanged. Likewise for self-referential sizes. */
99 if (TREE_CONSTANT (size)
100 || global_bindings_p () < 0 || contains_placeholder_p (size))
103 size = save_expr (size);
105 /* If an array with a variable number of elements is declared, and
106 the elements require destruction, we will emit a cleanup for the
107 array. That cleanup is run both on normal exit from the block
108 and in the exception-handler for the block. Normally, when code
109 is used in both ordinary code and in an exception handler it is
110 `unsaved', i.e., all SAVE_EXPRs are recalculated. However, we do
111 not wish to do that here; the array-size is the same in both
113 if (TREE_CODE (size) == SAVE_EXPR)
114 SAVE_EXPR_PERSISTENT_P (size) = 1;
116 if (global_bindings_p ())
118 if (TREE_CONSTANT (size))
119 error ("type size can't be explicitly evaluated");
121 error ("variable-size type declared outside of any function");
123 return size_one_node;
126 if (immediate_size_expand)
127 /* NULL_RTX is not defined; neither is the rtx type.
128 Also, we would like to pass const0_rtx here, but don't have it. */
129 expand_expr (size, expand_expr (integer_zero_node, NULL_PTR, VOIDmode, 0),
132 && cfun->x_dont_save_pending_sizes_p)
133 /* The front-end doesn't want us to keep a list of the expressions
134 that determine sizes for variable size objects. */
137 pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
142 #ifndef MAX_FIXED_MODE_SIZE
143 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
146 /* Return the machine mode to use for a nonscalar of SIZE bits.
147 The mode must be in class CLASS, and have exactly that many bits.
148 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
152 mode_for_size (size, class, limit)
154 enum mode_class class;
157 register enum machine_mode mode;
159 if (limit && size > (unsigned int)(MAX_FIXED_MODE_SIZE))
162 /* Get the first mode which has this size, in the specified class. */
163 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
164 mode = GET_MODE_WIDER_MODE (mode))
165 if ((unsigned int)GET_MODE_BITSIZE (mode) == size)
171 /* Similar, but never return BLKmode; return the narrowest mode that
172 contains at least the requested number of bits. */
175 smallest_mode_for_size (size, class)
177 enum mode_class class;
179 register enum machine_mode mode;
181 /* Get the first mode which has at least this size, in the
183 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
184 mode = GET_MODE_WIDER_MODE (mode))
185 if ((unsigned int)GET_MODE_BITSIZE (mode) >= size)
191 /* Find an integer mode of the exact same size, or BLKmode on failure. */
194 int_mode_for_mode (mode)
195 enum machine_mode mode;
197 switch (GET_MODE_CLASS (mode))
200 case MODE_PARTIAL_INT:
203 case MODE_COMPLEX_INT:
204 case MODE_COMPLEX_FLOAT:
206 mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0);
213 /* ... fall through ... */
223 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
224 This can only be applied to objects of a sizetype. */
227 round_up (value, divisor)
231 tree arg = size_int_type (divisor, TREE_TYPE (value));
233 return size_binop (MULT_EXPR, size_binop (CEIL_DIV_EXPR, value, arg), arg);
236 /* Likewise, but round down. */
239 round_down (value, divisor)
243 tree arg = size_int_type (divisor, TREE_TYPE (value));
245 return size_binop (MULT_EXPR, size_binop (FLOOR_DIV_EXPR, value, arg), arg);
248 /* Set the size, mode and alignment of a ..._DECL node.
249 TYPE_DECL does need this for C++.
250 Note that LABEL_DECL and CONST_DECL nodes do not need this,
251 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
252 Don't call layout_decl for them.
254 KNOWN_ALIGN is the amount of alignment we can assume this
255 decl has with no special effort. It is relevant only for FIELD_DECLs
256 and depends on the previous fields.
257 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
258 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
259 the record will be aligned to suit. */
262 layout_decl (decl, known_align)
264 unsigned int known_align;
266 register tree type = TREE_TYPE (decl);
267 register enum tree_code code = TREE_CODE (decl);
269 if (code == CONST_DECL)
271 else if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
272 && code != TYPE_DECL && code != FIELD_DECL)
275 if (type == error_mark_node)
276 type = void_type_node;
278 /* Usually the size and mode come from the data type without change. */
279 DECL_MODE (decl) = TYPE_MODE (type);
280 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
281 if (DECL_SIZE (decl) == 0)
283 DECL_SIZE (decl) = TYPE_SIZE (type);
284 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (type);
286 else if (code == FIELD_DECL)
288 HOST_WIDE_INT spec_size;
290 /* The front-end may set the explicit width of the field, so its
291 size may not be the same as the size of its type. This happens
292 with bitfields, of course (an `int' bitfield may be only 2 bits,
293 say), but it also happens with other fields. For example, the
294 C++ front-end creates zero-sized fields corresponding to empty
295 base classes, and depends on layout_type setting
296 DECL_FIELD_BITPOS correctly for the field. */
297 if (integer_zerop (DECL_SIZE (decl))
298 && DECL_NAME (decl) != NULL_TREE)
301 /* Size is specified in number of bits. */
302 spec_size = TREE_INT_CST_LOW (DECL_SIZE (decl));
303 if (spec_size % BITS_PER_UNIT == 0)
304 DECL_SIZE_UNIT (decl) = size_int (spec_size / BITS_PER_UNIT);
306 DECL_SIZE_UNIT (decl) = 0;
309 /* Force alignment required for the data type.
310 But if the decl itself wants greater alignment, don't override that.
311 Likewise, if the decl is packed, don't override it. */
312 if (!(code == FIELD_DECL && DECL_BIT_FIELD (decl))
313 && (DECL_ALIGN (decl) == 0
314 || (! DECL_PACKED (decl) && TYPE_ALIGN (type) > DECL_ALIGN (decl))))
315 DECL_ALIGN (decl) = TYPE_ALIGN (type);
317 /* See if we can use an ordinary integer mode for a bit-field.
318 Conditions are: a fixed size that is correct for another mode
319 and occupying a complete byte or bytes on proper boundary. */
320 if (code == FIELD_DECL)
322 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
323 if (maximum_field_alignment != 0)
324 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
325 else if (DECL_PACKED (decl))
326 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT);
329 if (DECL_BIT_FIELD (decl)
330 && TYPE_SIZE (type) != 0
331 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
332 && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT)
334 register enum machine_mode xmode
335 = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1);
338 && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
340 DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
342 DECL_MODE (decl) = xmode;
343 DECL_SIZE (decl) = bitsize_int (GET_MODE_BITSIZE (xmode));
344 DECL_SIZE_UNIT (decl) = size_int (GET_MODE_SIZE (xmode));
345 /* This no longer needs to be accessed as a bit field. */
346 DECL_BIT_FIELD (decl) = 0;
350 /* Turn off DECL_BIT_FIELD if we won't need it set. */
351 if (DECL_BIT_FIELD (decl) && TYPE_MODE (type) == BLKmode
352 && known_align % TYPE_ALIGN (type) == 0
353 && DECL_SIZE_UNIT (decl) != 0
354 && DECL_ALIGN (decl) >= TYPE_ALIGN (type))
355 DECL_BIT_FIELD (decl) = 0;
357 /* Evaluate nonconstant size only once, either now or as soon as safe. */
358 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
359 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
360 if (DECL_SIZE_UNIT (decl) != 0
361 && TREE_CODE (DECL_SIZE_UNIT (decl)) != INTEGER_CST)
362 DECL_SIZE_UNIT (decl) = variable_size (DECL_SIZE_UNIT (decl));
364 /* If requested, warn about definitions of large data objects. */
366 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == PARM_DECL)
367 && ! DECL_EXTERNAL (decl))
369 tree size = DECL_SIZE_UNIT (decl);
371 if (size != 0 && TREE_CODE (size) == INTEGER_CST
372 && (TREE_INT_CST_HIGH (size) != 0
373 || TREE_INT_CST_LOW (size) > larger_than_size))
375 int size_as_int = TREE_INT_CST_LOW (size);
377 if (size_as_int == TREE_INT_CST_LOW (size)
378 && TREE_INT_CST_HIGH (size) == 0)
379 warning_with_decl (decl, "size of `%s' is %d bytes", size_as_int);
381 warning_with_decl (decl, "size of `%s' is larger than %d bytes",
387 /* Lay out a RECORD_TYPE type (a C struct).
388 This means laying out the fields, determining their positions,
389 and computing the overall size and required alignment of the record.
390 Note that if you set the TYPE_ALIGN before calling this
391 then the struct is aligned to at least that boundary.
393 If the type has basetypes, you must call layout_basetypes
394 before calling this function.
396 The return value is a list of static members of the record.
397 They still need to be laid out. */
404 unsigned int record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
405 unsigned int unpacked_align = record_align;
406 /* These must be laid out *after* the record is. */
407 tree pending_statics = NULL_TREE;
408 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
409 where CONST_SIZE is an integer
410 and VAR_SIZE is a tree expression.
411 If VAR_SIZE is null, the size is just CONST_SIZE.
412 Naturally we try to avoid using VAR_SIZE. */
413 HOST_WIDE_INT const_size = 0;
415 /* Once we start using VAR_SIZE, this is the maximum alignment
416 that we know VAR_SIZE has. */
417 unsigned int var_align = BITS_PER_UNIT;
418 int packed_maybe_necessary = 0;
420 #ifdef STRUCTURE_SIZE_BOUNDARY
421 /* Packed structures don't need to have minimum size. */
422 if (! TYPE_PACKED (rec))
423 record_align = MAX (record_align, STRUCTURE_SIZE_BOUNDARY);
426 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
428 unsigned int known_align = var_size ? var_align : const_size;
429 unsigned int desired_align = 0;
430 tree type = TREE_TYPE (field);
432 /* If FIELD is static, then treat it like a separate variable,
433 not really like a structure field.
434 If it is a FUNCTION_DECL, it's a method.
435 In both cases, all we do is lay out the decl,
436 and we do it *after* the record is laid out. */
438 if (TREE_CODE (field) == VAR_DECL)
440 pending_statics = tree_cons (NULL_TREE, field, pending_statics);
444 /* Enumerators and enum types which are local to this class need not
445 be laid out. Likewise for initialized constant fields. */
446 if (TREE_CODE (field) != FIELD_DECL)
449 /* Lay out the field so we know what alignment it needs.
450 For a packed field, use the alignment as specified,
451 disregarding what the type would want. */
452 if (DECL_PACKED (field))
453 desired_align = DECL_ALIGN (field);
454 layout_decl (field, known_align);
455 if (! DECL_PACKED (field))
456 desired_align = DECL_ALIGN (field);
457 /* Some targets (i.e. VMS) limit struct field alignment
458 to a lower boundary than alignment of variables. */
459 #ifdef BIGGEST_FIELD_ALIGNMENT
460 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
462 #ifdef ADJUST_FIELD_ALIGN
463 desired_align = ADJUST_FIELD_ALIGN (field, desired_align);
466 /* Record must have at least as much alignment as any field.
467 Otherwise, the alignment of the field within the record
470 #ifdef PCC_BITFIELD_TYPE_MATTERS
471 if (PCC_BITFIELD_TYPE_MATTERS && type != error_mark_node
472 && DECL_BIT_FIELD_TYPE (field)
473 && ! integer_zerop (TYPE_SIZE (type)))
475 /* For these machines, a zero-length field does not
476 affect the alignment of the structure as a whole.
477 It does, however, affect the alignment of the next field
478 within the structure. */
479 if (! integer_zerop (DECL_SIZE (field)))
480 record_align = MAX (record_align, desired_align);
481 else if (! DECL_PACKED (field))
482 desired_align = TYPE_ALIGN (type);
483 /* A named bit field of declared type `int'
484 forces the entire structure to have `int' alignment. */
485 if (DECL_NAME (field) != 0)
487 unsigned int type_align = TYPE_ALIGN (type);
489 if (maximum_field_alignment != 0)
490 type_align = MIN (type_align, maximum_field_alignment);
491 else if (DECL_PACKED (field))
492 type_align = MIN (type_align, BITS_PER_UNIT);
494 record_align = MAX (record_align, type_align);
496 unpacked_align = MAX (unpacked_align, TYPE_ALIGN (type));
502 record_align = MAX (record_align, desired_align);
504 unpacked_align = MAX (unpacked_align, TYPE_ALIGN (type));
507 if (warn_packed && DECL_PACKED (field))
509 if (const_size % TYPE_ALIGN (type) == 0
510 || (var_align % TYPE_ALIGN (type) == 0 && var_size != NULL_TREE))
512 if (TYPE_ALIGN (type) > desired_align)
514 if (STRICT_ALIGNMENT)
515 warning_with_decl (field, "packed attribute causes inefficient alignment for `%s'");
517 warning_with_decl (field, "packed attribute is unnecessary for `%s'");
521 packed_maybe_necessary = 1;
524 /* Does this field automatically have alignment it needs
525 by virtue of the fields that precede it and the record's
528 if (const_size % desired_align != 0
529 || (var_align % desired_align != 0 && var_size != NULL_TREE))
531 /* No, we need to skip space before this field.
532 Bump the cumulative size to multiple of field alignment. */
535 warning_with_decl (field, "padding struct to align `%s'");
537 if (var_size == NULL_TREE || var_align % desired_align == 0)
539 = CEIL (const_size, desired_align) * desired_align;
543 var_size = size_binop (PLUS_EXPR, var_size,
544 bitsize_int (const_size));
546 var_size = round_up (var_size, desired_align);
547 var_align = MIN (var_align, desired_align);
551 #ifdef PCC_BITFIELD_TYPE_MATTERS
552 if (PCC_BITFIELD_TYPE_MATTERS
553 && TREE_CODE (field) == FIELD_DECL
554 && type != error_mark_node
555 && DECL_BIT_FIELD_TYPE (field)
556 && !DECL_PACKED (field)
557 && maximum_field_alignment == 0
558 && !integer_zerop (DECL_SIZE (field)))
560 unsigned int type_align = TYPE_ALIGN (type);
561 register tree dsize = DECL_SIZE (field);
562 int field_size = TREE_INT_CST_LOW (dsize);
564 /* A bit field may not span more units of alignment of its type
565 than its type itself. Advance to next boundary if necessary. */
566 if (((const_size + field_size + type_align - 1) / type_align
567 - const_size / type_align)
568 > TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (field))) / type_align)
569 const_size = CEIL (const_size, type_align) * type_align;
573 /* No existing machine description uses this parameter.
574 So I have made it in this aspect identical to PCC_BITFIELD_TYPE_MATTERS. */
575 #ifdef BITFIELD_NBYTES_LIMITED
576 if (BITFIELD_NBYTES_LIMITED
577 && TREE_CODE (field) == FIELD_DECL
578 && type != error_mark_node
579 && DECL_BIT_FIELD_TYPE (field)
580 && !DECL_PACKED (field)
581 && !integer_zerop (DECL_SIZE (field)))
583 unsigned int type_align = TYPE_ALIGN (type);
584 register tree dsize = DECL_SIZE (field);
585 int field_size = TREE_INT_CST_LOW (dsize);
587 if (maximum_field_alignment != 0)
588 type_align = MIN (type_align, maximum_field_alignment);
589 /* ??? This test is opposite the test in the containing if
590 statement, so this code is unreachable currently. */
591 else if (DECL_PACKED (field))
592 type_align = MIN (type_align, BITS_PER_UNIT);
594 /* A bit field may not span the unit of alignment of its type.
595 Advance to next boundary if necessary. */
596 /* ??? This code should match the code above for the
597 PCC_BITFIELD_TYPE_MATTERS case. */
598 if (const_size / type_align
599 != (const_size + field_size - 1) / type_align)
600 const_size = CEIL (const_size, type_align) * type_align;
604 /* Size so far becomes the position of this field. */
606 if (var_size && const_size)
607 DECL_FIELD_BITPOS (field)
608 = size_binop (PLUS_EXPR, var_size, bitsize_int (const_size));
610 DECL_FIELD_BITPOS (field) = var_size;
613 DECL_FIELD_BITPOS (field) = bitsize_int (const_size);
615 /* If this field ended up more aligned than we thought it
616 would be (we approximate this by seeing if its position
617 changed), lay out the field again; perhaps we can use an
618 integral mode for it now. */
619 if (known_align != const_size)
620 layout_decl (field, const_size);
623 /* Now add size of this field to the size of the record. */
626 register tree dsize = DECL_SIZE (field);
628 /* This can happen when we have an invalid nested struct definition,
629 such as struct j { struct j { int i; } }. The error message is
630 printed in finish_struct. */
633 else if (TREE_CODE (dsize) == INTEGER_CST
634 && ! TREE_CONSTANT_OVERFLOW (dsize)
635 && TREE_INT_CST_HIGH (dsize) == 0
636 && TREE_INT_CST_LOW (dsize) + const_size >= const_size)
637 /* Use const_size if there's no overflow. */
638 const_size += TREE_INT_CST_LOW (dsize);
641 if (var_size == NULL_TREE)
644 var_size = size_binop (PLUS_EXPR, var_size, dsize);
649 /* Work out the total size and alignment of the record
650 as one expression and store in the record type.
651 Round it up to a multiple of the record's alignment. */
653 if (var_size == NULL_TREE)
654 TYPE_SIZE (rec) = bitsize_int (const_size);
658 var_size = size_binop (PLUS_EXPR, var_size, bitsize_int (const_size));
660 TYPE_SIZE (rec) = var_size;
663 /* Determine the desired alignment. */
664 #ifdef ROUND_TYPE_ALIGN
665 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), record_align);
667 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), record_align);
670 /* Record the un-rounded size in the binfo node. But first we check
671 the size of TYPE_BINFO to make sure that BINFO_SIZE is available. */
672 if (TYPE_BINFO (rec) && TREE_VEC_LENGTH (TYPE_BINFO (rec)) > 6)
674 TYPE_BINFO_SIZE (rec) = TYPE_SIZE (rec);
675 TYPE_BINFO_SIZE_UNIT (rec)
677 size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (rec),
678 bitsize_int (BITS_PER_UNIT)));
682 tree unpadded_size = TYPE_SIZE (rec);
684 #ifdef ROUND_TYPE_SIZE
685 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
687 /* Round the size up to be a multiple of the required alignment */
688 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
691 if (warn_padded && var_size == NULL_TREE
692 && simple_cst_equal (unpadded_size, TYPE_SIZE (rec)) == 0)
693 warning ("padding struct size to alignment boundary");
696 if (warn_packed && TYPE_PACKED (rec) && !packed_maybe_necessary
697 && var_size == NULL_TREE)
701 TYPE_PACKED (rec) = 0;
702 #ifdef ROUND_TYPE_ALIGN
704 = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), unpacked_align);
706 unpacked_align = MAX (TYPE_ALIGN (rec), unpacked_align);
708 #ifdef ROUND_TYPE_SIZE
709 unpacked_size = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), unpacked_align);
711 unpacked_size = round_up (TYPE_SIZE (rec), unpacked_align);
714 if (simple_cst_equal (unpacked_size, TYPE_SIZE (rec)))
720 if (TREE_CODE (TYPE_NAME (rec)) == IDENTIFIER_NODE)
721 name = IDENTIFIER_POINTER (TYPE_NAME (rec));
723 name = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (rec)));
724 if (STRICT_ALIGNMENT)
725 warning ("packed attribute causes inefficient alignment for `%s'", name);
727 warning ("packed attribute is unnecessary for `%s'", name);
731 if (STRICT_ALIGNMENT)
732 warning ("packed attribute causes inefficient alignment");
734 warning ("packed attribute is unnecessary");
737 TYPE_PACKED (rec) = 1;
740 return pending_statics;
743 /* Lay out a UNION_TYPE or QUAL_UNION_TYPE type.
744 Lay out all the fields, set their positions to zero,
745 and compute the size and alignment of the union (maximum of any field).
746 Note that if you set the TYPE_ALIGN before calling this
747 then the union align is aligned to at least that boundary. */
754 unsigned int union_align = BITS_PER_UNIT;
756 /* The size of the union, based on the fields scanned so far,
757 is max (CONST_SIZE, VAR_SIZE).
758 VAR_SIZE may be null; then CONST_SIZE by itself is the size. */
759 register HOST_WIDE_INT const_size = 0;
760 register tree var_size = 0;
762 #ifdef STRUCTURE_SIZE_BOUNDARY
763 /* Packed structures don't need to have minimum size. */
764 if (! TYPE_PACKED (rec))
765 union_align = STRUCTURE_SIZE_BOUNDARY;
768 /* If this is a QUAL_UNION_TYPE, we want to process the fields in
769 the reverse order in building the COND_EXPR that denotes its
770 size. We reverse them again later. */
771 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
772 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
774 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
778 /* Enums which are local to this class need not be laid out. */
779 if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
782 layout_decl (field, 0);
783 DECL_FIELD_BITPOS (field) = bitsize_int (0);
785 /* Union must be at least as aligned as any field requires. */
787 union_align = MAX (union_align, DECL_ALIGN (field));
789 #ifdef PCC_BITFIELD_TYPE_MATTERS
790 /* On the m88000, a bit field of declare type `int'
791 forces the entire union to have `int' alignment. */
792 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
793 union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
796 dsize = DECL_SIZE (field);
797 if (TREE_CODE (rec) == UNION_TYPE)
799 /* Set union_size to max (decl_size, union_size).
800 There are more and less general ways to do this.
801 Use only CONST_SIZE unless forced to use VAR_SIZE. */
803 if (TREE_CODE (dsize) == INTEGER_CST
804 && ! TREE_CONSTANT_OVERFLOW (dsize)
805 && TREE_INT_CST_HIGH (dsize) == 0)
807 = MAX (const_size, TREE_INT_CST_LOW (dsize));
808 else if (var_size == 0)
811 var_size = size_binop (MAX_EXPR, var_size, dsize);
813 else if (TREE_CODE (rec) == QUAL_UNION_TYPE)
814 var_size = fold (build (COND_EXPR, bitsizetype, DECL_QUALIFIER (field),
816 var_size ? var_size : bitsize_int (0)));
819 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
820 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
822 /* Determine the ultimate size of the union (in bytes). */
823 if (NULL == var_size)
825 = bitsize_int (CEIL (const_size, BITS_PER_UNIT) * BITS_PER_UNIT);
827 else if (const_size == 0)
828 TYPE_SIZE (rec) = var_size;
830 TYPE_SIZE (rec) = size_binop (MAX_EXPR, var_size,
831 round_up (bitsize_int (const_size),
834 /* Determine the desired alignment. */
835 #ifdef ROUND_TYPE_ALIGN
836 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), union_align);
838 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
841 #ifdef ROUND_TYPE_SIZE
842 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
844 /* Round the size up to be a multiple of the required alignment */
845 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
849 /* Calculate the mode, size, and alignment for TYPE.
850 For an array type, calculate the element separation as well.
851 Record TYPE on the chain of permanent or temporary types
852 so that dbxout will find out about it.
854 TYPE_SIZE of a type is nonzero if the type has been laid out already.
855 layout_type does nothing on such a type.
857 If the type is incomplete, its TYPE_SIZE remains zero. */
864 tree pending_statics;
869 /* Do nothing if type has been laid out before. */
870 if (TYPE_SIZE (type))
873 /* Make sure all nodes we allocate are not momentary;
874 they must last past the current statement. */
875 old = suspend_momentary ();
877 /* Put all our nodes into the same obstack as the type. Also,
878 make expressions saveable (this is a no-op for permanent types). */
880 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
881 saveable_allocation ();
883 switch (TREE_CODE (type))
886 /* This kind of type is the responsibility
887 of the language-specific code. */
890 case BOOLEAN_TYPE: /* Used for Java, Pascal, and Chill. */
891 if (TYPE_PRECISION (type) == 0)
892 TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */
894 /* ... fall through ... */
899 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
900 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
901 TREE_UNSIGNED (type) = 1;
903 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
905 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
906 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
910 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
911 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
912 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
916 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
918 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
919 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
920 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
922 TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
923 TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type)));
927 TYPE_SIZE (type) = size_zero_node;
928 TYPE_SIZE_UNIT (type) = size_zero_node;
929 TYPE_ALIGN (type) = 1;
930 TYPE_MODE (type) = VOIDmode;
934 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
935 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
936 TYPE_MODE (type) = ptr_mode;
941 TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0);
942 TYPE_SIZE (type) = bitsize_int (2 * POINTER_SIZE);
943 TYPE_SIZE_UNIT (type) = size_int ((2 * POINTER_SIZE) / BITS_PER_UNIT);
948 TYPE_MODE (type) = ptr_mode;
949 TYPE_SIZE (type) = bitsize_int (POINTER_SIZE);
950 TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT);
951 TREE_UNSIGNED (type) = 1;
952 TYPE_PRECISION (type) = POINTER_SIZE;
957 register tree index = TYPE_DOMAIN (type);
958 register tree element = TREE_TYPE (type);
960 build_pointer_type (element);
962 /* We need to know both bounds in order to compute the size. */
963 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
964 && TYPE_SIZE (element))
966 tree ub = TYPE_MAX_VALUE (index);
967 tree lb = TYPE_MIN_VALUE (index);
971 /* If UB is max (lb - 1, x), remove the MAX_EXPR since the
972 test for negative below covers it. */
973 if (TREE_CODE (ub) == MAX_EXPR
974 && TREE_CODE (TREE_OPERAND (ub, 0)) == MINUS_EXPR
975 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 0), 1))
976 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 0), 0),
978 ub = TREE_OPERAND (ub, 1);
979 else if (TREE_CODE (ub) == MAX_EXPR
980 && TREE_CODE (TREE_OPERAND (ub, 1)) == MINUS_EXPR
981 && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 1), 1))
982 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 1),
985 ub = TREE_OPERAND (ub, 0);
987 /* The initial subtraction should happen in the original type so
988 that (possible) negative values are handled appropriately. */
989 length = size_binop (PLUS_EXPR, size_one_node,
991 fold (build (MINUS_EXPR,
995 /* If neither bound is a constant and sizetype is signed, make
996 sure the size is never negative. We should really do this
997 if *either* bound is non-constant, but this is the best
998 compromise between C and Ada. */
999 if (! TREE_UNSIGNED (sizetype)
1000 && TREE_CODE (TYPE_MIN_VALUE (index)) != INTEGER_CST
1001 && TREE_CODE (TYPE_MAX_VALUE (index)) != INTEGER_CST)
1002 length = size_binop (MAX_EXPR, length, size_zero_node);
1004 /* Special handling for arrays of bits (for Chill). */
1005 element_size = TYPE_SIZE (element);
1006 if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element))
1008 HOST_WIDE_INT maxvalue
1009 = TREE_INT_CST_LOW (TYPE_MAX_VALUE (element));
1010 HOST_WIDE_INT minvalue
1011 = TREE_INT_CST_LOW (TYPE_MIN_VALUE (element));
1013 if (maxvalue - minvalue == 1
1014 && (maxvalue == 1 || maxvalue == 0))
1015 element_size = integer_one_node;
1018 TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size,
1019 convert (bitsizetype, length));
1021 /* If we know the size of the element, calculate the total
1022 size directly, rather than do some division thing below.
1023 This optimization helps Fortran assumed-size arrays
1024 (where the size of the array is determined at runtime)
1026 Note that we can't do this in the case where the size of
1027 the elements is one bit since TYPE_SIZE_UNIT cannot be
1028 set correctly in that case. */
1029 if (TYPE_SIZE_UNIT (element) != 0 && ! integer_onep (element_size))
1030 TYPE_SIZE_UNIT (type)
1031 = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length);
1034 /* Now round the alignment and size,
1035 using machine-dependent criteria if any. */
1037 #ifdef ROUND_TYPE_ALIGN
1039 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
1041 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
1044 #ifdef ROUND_TYPE_SIZE
1045 if (TYPE_SIZE (type) != 0)
1048 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1050 /* If the rounding changed the size of the type, remove any
1051 pre-calculated TYPE_SIZE_UNIT. */
1052 if (simple_cst_equal (TYPE_SIZE (type), tmp) != 1)
1053 TYPE_SIZE_UNIT (type) = NULL;
1055 TYPE_SIZE (type) = tmp;
1059 TYPE_MODE (type) = BLKmode;
1060 if (TYPE_SIZE (type) != 0
1061 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
1062 /* BLKmode elements force BLKmode aggregate;
1063 else extract/store fields may lose. */
1064 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
1065 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
1068 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
1071 if (STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
1072 && ((int) TYPE_ALIGN (type)
1073 < TREE_INT_CST_LOW (TYPE_SIZE (type)))
1074 && TYPE_MODE (type) != BLKmode)
1076 TYPE_NO_FORCE_BLK (type) = 1;
1077 TYPE_MODE (type) = BLKmode;
1084 pending_statics = layout_record (type);
1085 TYPE_MODE (type) = BLKmode;
1086 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
1089 enum machine_mode mode = VOIDmode;
1091 /* A record which has any BLKmode members must itself be BLKmode;
1092 it can't go in a register.
1093 Unless the member is BLKmode only because it isn't aligned. */
1094 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1098 if (TREE_CODE (field) != FIELD_DECL
1099 || TREE_CODE (TREE_TYPE (field)) == ERROR_MARK)
1102 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1103 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1106 if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
1109 bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
1111 /* Must be BLKmode if any field crosses a word boundary,
1112 since extract_bit_field can't handle that in registers. */
1113 if (bitpos / BITS_PER_WORD
1114 != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
1116 /* But there is no problem if the field is entire words. */
1117 && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD != 0)
1120 /* If this field is the whole struct, remember its mode so
1121 that, say, we can put a double in a class into a DF
1122 register instead of forcing it to live in the stack. */
1123 if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field)))
1124 mode = DECL_MODE (field);
1126 #ifdef STRUCT_FORCE_BLK
1127 /* With some targets, eg. c4x, it is sub-optimal
1128 to access an aligned BLKmode structure as a scalar. */
1129 if (mode == VOIDmode && STRUCT_FORCE_BLK (field))
1131 #endif /* STRUCT_FORCE_BLK */
1134 if (mode != VOIDmode)
1135 /* We only have one real field; use its mode. */
1136 TYPE_MODE (type) = mode;
1139 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
1142 /* If structure's known alignment is less than
1143 what the scalar mode would need, and it matters,
1144 then stick with BLKmode. */
1145 if (STRICT_ALIGNMENT
1146 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1147 || ((int) TYPE_ALIGN (type)
1148 >= TREE_INT_CST_LOW (TYPE_SIZE (type)))))
1150 if (TYPE_MODE (type) != BLKmode)
1151 /* If this is the only reason this type is BLKmode,
1152 then don't force containing types to be BLKmode. */
1153 TYPE_NO_FORCE_BLK (type) = 1;
1154 TYPE_MODE (type) = BLKmode;
1160 /* Lay out any static members. This is done now
1161 because their type may use the record's type. */
1162 while (pending_statics)
1164 layout_decl (TREE_VALUE (pending_statics), 0);
1165 pending_statics = TREE_CHAIN (pending_statics);
1170 case QUAL_UNION_TYPE:
1171 layout_union (type);
1172 TYPE_MODE (type) = BLKmode;
1173 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
1174 /* If structure's known alignment is less than
1175 what the scalar mode would need, and it matters,
1176 then stick with BLKmode. */
1177 && (! STRICT_ALIGNMENT
1178 || TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
1179 || ((int) TYPE_ALIGN (type)
1180 >= TREE_INT_CST_LOW (TYPE_SIZE (type)))))
1184 /* A union which has any BLKmode members must itself be BLKmode;
1185 it can't go in a register.
1186 Unless the member is BLKmode only because it isn't aligned. */
1187 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1189 if (TREE_CODE (field) != FIELD_DECL)
1192 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
1193 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
1198 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
1205 case SET_TYPE: /* Used by Chill and Pascal. */
1206 if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST
1207 || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST)
1211 #ifndef SET_WORD_SIZE
1212 #define SET_WORD_SIZE BITS_PER_WORD
1214 unsigned int alignment
1215 = set_alignment ? set_alignment : SET_WORD_SIZE;
1217 = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type)))
1218 - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1);
1220 = ((size_in_bits + alignment - 1) / alignment) * alignment;
1222 if (rounded_size > (int) alignment)
1223 TYPE_MODE (type) = BLKmode;
1225 TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1);
1227 TYPE_SIZE (type) = bitsize_int (rounded_size);
1228 TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT);
1229 TYPE_ALIGN (type) = alignment;
1230 TYPE_PRECISION (type) = size_in_bits;
1235 /* The size may vary in different languages, so the language front end
1236 should fill in the size. */
1237 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
1238 TYPE_MODE (type) = BLKmode;
1245 /* Normally, use the alignment corresponding to the mode chosen.
1246 However, where strict alignment is not required, avoid
1247 over-aligning structures, since most compilers do not do this
1250 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
1251 && (STRICT_ALIGNMENT
1252 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
1253 && TREE_CODE (type) != QUAL_UNION_TYPE
1254 && TREE_CODE (type) != ARRAY_TYPE)))
1255 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
1257 /* Do machine-dependent extra alignment. */
1258 #ifdef ROUND_TYPE_ALIGN
1260 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT);
1263 #ifdef ROUND_TYPE_SIZE
1264 if (TYPE_SIZE (type) != 0)
1266 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
1269 /* Evaluate nonconstant size only once, either now or as soon as safe. */
1270 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
1271 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
1273 /* If we failed to find a simple way to calculate the unit size
1274 of the type above, find it by division. */
1275 if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0)
1276 /* TYPE_SIZE (type) is computed in bitsizetype. After the division, the
1277 result will fit in sizetype. We will get more efficient code using
1278 sizetype, so we force a conversion. */
1279 TYPE_SIZE_UNIT (type)
1280 = convert (sizetype,
1281 size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type),
1282 bitsize_int (BITS_PER_UNIT)));
1284 /* Once again evaluate only once, either now or as soon as safe. */
1285 if (TYPE_SIZE_UNIT (type) != 0
1286 && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST)
1287 TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type));
1289 /* Also layout any other variants of the type. */
1290 if (TYPE_NEXT_VARIANT (type)
1291 || type != TYPE_MAIN_VARIANT (type))
1294 /* Record layout info of this variant. */
1295 tree size = TYPE_SIZE (type);
1296 tree size_unit = TYPE_SIZE_UNIT (type);
1297 unsigned int align = TYPE_ALIGN (type);
1298 enum machine_mode mode = TYPE_MODE (type);
1300 /* Copy it into all variants. */
1301 for (variant = TYPE_MAIN_VARIANT (type);
1303 variant = TYPE_NEXT_VARIANT (variant))
1305 TYPE_SIZE (variant) = size;
1306 TYPE_SIZE_UNIT (variant) = size_unit;
1307 TYPE_ALIGN (variant) = align;
1308 TYPE_MODE (variant) = mode;
1313 resume_momentary (old);
1315 /* If this type is created before sizetype has been permanently set,
1316 record it so set_sizetype can fix it up. */
1319 TREE_CHAIN (type) = early_type_list;
1320 early_type_list = type;
1324 /* Create and return a type for signed integers of PRECISION bits. */
1327 make_signed_type (precision)
1330 register tree type = make_node (INTEGER_TYPE);
1332 TYPE_PRECISION (type) = precision;
1334 fixup_signed_type (type);
1338 /* Create and return a type for unsigned integers of PRECISION bits. */
1341 make_unsigned_type (precision)
1344 register tree type = make_node (INTEGER_TYPE);
1346 TYPE_PRECISION (type) = precision;
1348 fixup_unsigned_type (type);
1352 /* Initialize sizetype and bitsizetype to a reasonable and temporary
1353 value to enable integer types to be created. */
1356 initialize_sizetypes ()
1358 tree t = make_node (INTEGER_TYPE);
1360 /* Set this so we do something reasonable for the build_int_2 calls
1362 integer_type_node = t;
1364 TYPE_MODE (t) = SImode;
1365 TYPE_ALIGN (t) = GET_MODE_ALIGNMENT (SImode);
1366 TYPE_SIZE (t) = build_int_2 (GET_MODE_BITSIZE (SImode), 0);
1367 TYPE_SIZE_UNIT (t) = build_int_2 (GET_MODE_SIZE (SImode), 0);
1368 TREE_UNSIGNED (t) = 1;
1369 TYPE_PRECISION (t) = GET_MODE_BITSIZE (SImode);
1370 TYPE_MIN_VALUE (t) = build_int_2 (0, 0);
1372 /* 1000 avoids problems with possible overflow and is certainly
1373 larger than any size value we'd want to be storing. */
1374 TYPE_MAX_VALUE (t) = build_int_2 (1000, 0);
1376 /* These two must be different nodes because of the caching done in
1379 bitsizetype = copy_node (t);
1380 integer_type_node = 0;
1383 /* Set sizetype to TYPE, and initialize *sizetype accordingly.
1384 Also update the type of any standard type's sizes made so far. */
1390 int oprecision = TYPE_PRECISION (type);
1391 /* The *bitsizetype types use a precision that avoids overflows when
1392 calculating signed sizes / offsets in bits. However, when
1393 cross-compiling from a 32 bit to a 64 bit host, we are limited to 64 bit
1395 int precision = MIN (oprecision + BITS_PER_UNIT_LOG + 1,
1396 2 * HOST_BITS_PER_WIDE_INT);
1403 /* Make copies of nodes since we'll be setting TYPE_IS_SIZETYPE. */
1404 sizetype = copy_node (type);
1405 bitsizetype = make_node (INTEGER_TYPE);
1406 TYPE_NAME (bitsizetype) = TYPE_NAME (type);
1407 TYPE_PRECISION (bitsizetype) = precision;
1409 if (TREE_UNSIGNED (type))
1410 fixup_unsigned_type (bitsizetype);
1412 fixup_signed_type (bitsizetype);
1414 layout_type (bitsizetype);
1416 if (TREE_UNSIGNED (type))
1418 usizetype = sizetype;
1419 ubitsizetype = bitsizetype;
1420 ssizetype = copy_node (make_signed_type (oprecision));
1421 sbitsizetype = copy_node (make_signed_type (precision));
1425 ssizetype = sizetype;
1426 sbitsizetype = bitsizetype;
1427 usizetype = copy_node (make_unsigned_type (oprecision));
1428 ubitsizetype = copy_node (make_unsigned_type (precision));
1431 TYPE_NAME (bitsizetype) = get_identifier ("bit_size_type");
1433 for (i = 0; i < sizeof sizetype_tab / sizeof sizetype_tab[0]; i++)
1434 TYPE_IS_SIZETYPE (sizetype_tab[i]) = 1;
1436 ggc_add_tree_root ((tree *) &sizetype_tab,
1437 sizeof sizetype_tab / sizeof (tree));
1439 /* Go down each of the types we already made and set the proper type
1440 for the sizes in them. */
1441 for (t = early_type_list; t != 0; t = next)
1443 next = TREE_CHAIN (t);
1446 if (TREE_CODE (t) != INTEGER_TYPE)
1449 TREE_TYPE (TYPE_SIZE (t)) = bitsizetype;
1450 TREE_TYPE (TYPE_SIZE_UNIT (t)) = sizetype;
1453 early_type_list = 0;
1457 /* Set the extreme values of TYPE based on its precision in bits,
1458 then lay it out. Used when make_signed_type won't do
1459 because the tree code is not INTEGER_TYPE.
1460 E.g. for Pascal, when the -fsigned-char option is given. */
1463 fixup_signed_type (type)
1466 register int precision = TYPE_PRECISION (type);
1468 TYPE_MIN_VALUE (type)
1469 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1470 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1471 (((HOST_WIDE_INT) (-1)
1472 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1473 ? precision - HOST_BITS_PER_WIDE_INT - 1
1475 TYPE_MAX_VALUE (type)
1476 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1477 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1478 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1479 ? (((HOST_WIDE_INT) 1
1480 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1483 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1484 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1486 /* Lay out the type: set its alignment, size, etc. */
1490 /* Set the extreme values of TYPE based on its precision in bits,
1491 then lay it out. This is used both in `make_unsigned_type'
1492 and for enumeral types. */
1495 fixup_unsigned_type (type)
1498 register int precision = TYPE_PRECISION (type);
1500 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1501 TYPE_MAX_VALUE (type)
1502 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1503 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1504 precision - HOST_BITS_PER_WIDE_INT > 0
1505 ? ((unsigned HOST_WIDE_INT) ~0
1506 >> (HOST_BITS_PER_WIDE_INT
1507 - (precision - HOST_BITS_PER_WIDE_INT)))
1509 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1510 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1512 /* Lay out the type: set its alignment, size, etc. */
1516 /* Find the best machine mode to use when referencing a bit field of length
1517 BITSIZE bits starting at BITPOS.
1519 The underlying object is known to be aligned to a boundary of ALIGN bits.
1520 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1521 larger than LARGEST_MODE (usually SImode).
1523 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1524 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1525 mode meeting these conditions.
1527 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1528 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1529 all the conditions. */
1532 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1533 int bitsize, bitpos;
1535 enum machine_mode largest_mode;
1538 enum machine_mode mode;
1541 /* Find the narrowest integer mode that contains the bit field. */
1542 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1543 mode = GET_MODE_WIDER_MODE (mode))
1545 unit = GET_MODE_BITSIZE (mode);
1546 if ((bitpos % unit) + bitsize <= unit)
1550 if (mode == VOIDmode
1551 /* It is tempting to omit the following line
1552 if STRICT_ALIGNMENT is true.
1553 But that is incorrect, since if the bitfield uses part of 3 bytes
1554 and we use a 4-byte mode, we could get a spurious segv
1555 if the extra 4th byte is past the end of memory.
1556 (Though at least one Unix compiler ignores this problem:
1557 that on the Sequent 386 machine. */
1558 || MIN (unit, BIGGEST_ALIGNMENT) > (int) align
1559 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1562 if (SLOW_BYTE_ACCESS && ! volatilep)
1564 enum machine_mode wide_mode = VOIDmode, tmode;
1566 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1567 tmode = GET_MODE_WIDER_MODE (tmode))
1569 unit = GET_MODE_BITSIZE (tmode);
1570 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1571 && unit <= BITS_PER_WORD
1572 && unit <= (int) MIN (align, BIGGEST_ALIGNMENT)
1573 && (largest_mode == VOIDmode
1574 || unit <= GET_MODE_BITSIZE (largest_mode)))
1578 if (wide_mode != VOIDmode)
1585 /* Return the alignment of MODE. This will be bounded by 1 and
1586 BIGGEST_ALIGNMENT. */
1589 get_mode_alignment (mode)
1590 enum machine_mode mode;
1592 unsigned alignment = GET_MODE_UNIT_SIZE (mode);
1594 /* Extract the LSB of the size. */
1595 alignment = alignment & -alignment;
1597 alignment *= BITS_PER_UNIT;
1599 alignment = MIN (BIGGEST_ALIGNMENT, MAX (1, alignment));
1603 /* This function is run once to initialize stor-layout.c. */
1606 init_stor_layout_once ()
1608 ggc_add_tree_root (&pending_sizes, 1);