1 /* C-compiler utilities for types and variables storage layout
2 Copyright (C) 1987, 1988, 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
27 #define CEIL(x,y) (((x) + (y) - 1) / (y))
29 /* Data type for the expressions representing sizes of data types.
30 It is the first integer type laid out.
35 /* An integer constant with value 0 whose type is sizetype. */
39 /* An integer constant with value 1 whose type is sizetype. */
43 /* If nonzero, this is an upper limit on alignment of structure fields.
44 The value is measured in bits. */
45 int maximum_field_alignment;
47 #define GET_MODE_ALIGNMENT(MODE) \
48 MIN (BIGGEST_ALIGNMENT, \
49 MAX (1, (GET_MODE_UNIT_SIZE (MODE) * BITS_PER_UNIT)))
51 static enum machine_mode smallest_mode_for_size PROTO((unsigned int,
53 static tree layout_record PROTO((tree));
54 static void layout_union PROTO((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;
78 /* Given a size SIZE that isn't constant, return a SAVE_EXPR
79 to serve as the actual size-expression for a type or decl. */
85 /* If the language-processor is to take responsibility for variable-sized
86 items (e.g., languages which have elaboration procedures like Ada),
87 just return SIZE unchanged. */
88 if (global_bindings_p () < 0)
91 size = save_expr (size);
93 if (global_bindings_p ())
95 if (TREE_CONSTANT (size))
96 error ("type size can't be explicitly evaluated");
98 error ("variable-size type declared outside of any function");
103 if (immediate_size_expand)
104 /* NULL_RTX is not defined; neither is the rtx type.
105 Also, we would like to pass const0_rtx here, but don't have it. */
106 expand_expr (size, expand_expr (integer_zero_node, NULL_PTR, VOIDmode, 0),
109 pending_sizes = tree_cons (NULL_TREE, size, pending_sizes);
114 #ifndef MAX_FIXED_MODE_SIZE
115 #define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode)
118 /* Return the machine mode to use for a nonscalar of SIZE bits.
119 The mode must be in class CLASS, and have exactly that many bits.
120 If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not
124 mode_for_size (size, class, limit)
126 enum mode_class class;
129 register enum machine_mode mode;
131 if (limit && size > MAX_FIXED_MODE_SIZE)
134 /* Get the first mode which has this size, in the specified class. */
135 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
136 mode = GET_MODE_WIDER_MODE (mode))
137 if (GET_MODE_BITSIZE (mode) == size)
143 /* Similar, but never return BLKmode; return the narrowest mode that
144 contains at least the requested number of bits. */
146 static enum machine_mode
147 smallest_mode_for_size (size, class)
149 enum mode_class class;
151 register enum machine_mode mode;
153 /* Get the first mode which has at least this size, in the
155 for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode;
156 mode = GET_MODE_WIDER_MODE (mode))
157 if (GET_MODE_BITSIZE (mode) >= size)
163 /* Return the value of VALUE, rounded up to a multiple of DIVISOR. */
166 round_up (value, divisor)
170 return size_binop (MULT_EXPR,
171 size_binop (CEIL_DIV_EXPR, value, size_int (divisor)),
175 /* Set the size, mode and alignment of a ..._DECL node.
176 TYPE_DECL does need this for C++.
177 Note that LABEL_DECL and CONST_DECL nodes do not need this,
178 and FUNCTION_DECL nodes have them set up in a special (and simple) way.
179 Don't call layout_decl for them.
181 KNOWN_ALIGN is the amount of alignment we can assume this
182 decl has with no special effort. It is relevant only for FIELD_DECLs
183 and depends on the previous fields.
184 All that matters about KNOWN_ALIGN is which powers of 2 divide it.
185 If KNOWN_ALIGN is 0, it means, "as much alignment as you like":
186 the record will be aligned to suit. */
189 layout_decl (decl, known_align)
191 unsigned known_align;
193 register tree type = TREE_TYPE (decl);
194 register enum tree_code code = TREE_CODE (decl);
195 int spec_size = DECL_FIELD_SIZE (decl);
197 if (code == CONST_DECL)
200 if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL
201 && code != FIELD_DECL && code != TYPE_DECL)
204 if (type == error_mark_node)
206 type = void_type_node;
210 /* Usually the size and mode come from the data type without change. */
212 DECL_MODE (decl) = TYPE_MODE (type);
213 TREE_UNSIGNED (decl) = TREE_UNSIGNED (type);
214 if (DECL_SIZE (decl) == 0)
215 DECL_SIZE (decl) = TYPE_SIZE (type);
217 if (code == FIELD_DECL && DECL_BIT_FIELD (decl))
219 /* This is a bit-field. We don't know how to handle
220 them except for integers and enums, and front end should
221 never generate them otherwise. */
223 if (! (TREE_CODE (type) == INTEGER_TYPE
224 || TREE_CODE (type) == ENUMERAL_TYPE))
227 if (spec_size == 0 && DECL_NAME (decl) != 0)
230 /* Size is specified number of bits. */
231 DECL_SIZE (decl) = size_int (spec_size);
233 /* Force alignment required for the data type.
234 But if the decl itself wants greater alignment, don't override that.
235 Likewise, if the decl is packed, don't override it. */
236 else if (DECL_ALIGN (decl) == 0
237 || (! DECL_PACKED (decl) && TYPE_ALIGN (type) > DECL_ALIGN (decl)))
238 DECL_ALIGN (decl) = TYPE_ALIGN (type);
240 /* See if we can use an ordinary integer mode for a bit-field. */
241 /* Conditions are: a fixed size that is correct for another mode
242 and occupying a complete byte or bytes on proper boundary. */
243 if (code == FIELD_DECL)
245 DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0;
246 if (maximum_field_alignment != 0)
247 DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), maximum_field_alignment);
250 if (DECL_BIT_FIELD (decl)
251 && TYPE_SIZE (type) != 0
252 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
254 register enum machine_mode xmode
255 = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1);
258 && known_align % GET_MODE_ALIGNMENT (xmode) == 0)
260 DECL_ALIGN (decl) = MAX (GET_MODE_ALIGNMENT (xmode),
262 DECL_MODE (decl) = xmode;
263 DECL_SIZE (decl) = size_int (GET_MODE_BITSIZE (xmode));
264 /* This no longer needs to be accessed as a bit field. */
265 DECL_BIT_FIELD (decl) = 0;
269 /* Evaluate nonconstant size only once, either now or as soon as safe. */
270 if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST)
271 DECL_SIZE (decl) = variable_size (DECL_SIZE (decl));
274 /* Lay out a RECORD_TYPE type (a C struct).
275 This means laying out the fields, determining their positions,
276 and computing the overall size and required alignment of the record.
277 Note that if you set the TYPE_ALIGN before calling this
278 then the struct is aligned to at least that boundary.
280 If the type has basetypes, you must call layout_basetypes
281 before calling this function.
283 The return value is a list of static members of the record.
284 They still need to be laid out. */
291 #ifdef STRUCTURE_SIZE_BOUNDARY
292 unsigned record_align = MAX (STRUCTURE_SIZE_BOUNDARY, TYPE_ALIGN (rec));
294 unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec));
296 /* These must be laid out *after* the record is. */
297 tree pending_statics = NULL_TREE;
298 /* Record size so far is CONST_SIZE + VAR_SIZE bits,
299 where CONST_SIZE is an integer
300 and VAR_SIZE is a tree expression.
301 If VAR_SIZE is null, the size is just CONST_SIZE.
302 Naturally we try to avoid using VAR_SIZE. */
303 register int const_size = 0;
304 register tree var_size = 0;
305 /* Once we start using VAR_SIZE, this is the maximum alignment
306 that we know VAR_SIZE has. */
307 register int var_align = BITS_PER_UNIT;
310 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
312 register int known_align = var_size ? var_align : const_size;
313 register int desired_align;
315 /* If FIELD is static, then treat it like a separate variable,
316 not really like a structure field.
317 If it is a FUNCTION_DECL, it's a method.
318 In both cases, all we do is lay out the decl,
319 and we do it *after* the record is laid out. */
321 if (TREE_STATIC (field))
323 pending_statics = tree_cons (NULL_TREE, field, pending_statics);
326 /* Enumerators and enum types which are local to this class need not
327 be laid out. Likewise for initialized constant fields. */
328 if (TREE_CODE (field) != FIELD_DECL)
331 /* Lay out the field so we know what alignment it needs.
332 For a packed field, use the alignment as specified,
333 disregarding what the type would want. */
334 if (DECL_PACKED (field))
335 desired_align = DECL_ALIGN (field);
336 layout_decl (field, known_align);
337 if (! DECL_PACKED (field))
338 desired_align = DECL_ALIGN (field);
339 /* Some targets (i.e. VMS) limit struct field alignment
340 to a lower boundary than alignment of variables. */
341 #ifdef BIGGEST_FIELD_ALIGNMENT
342 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
345 /* Record must have at least as much alignment as any field.
346 Otherwise, the alignment of the field within the record
349 #ifndef PCC_BITFIELD_TYPE_MATTERS
350 record_align = MAX (record_align, desired_align);
352 if (PCC_BITFIELD_TYPE_MATTERS && TREE_TYPE (field) != error_mark_node
353 && DECL_BIT_FIELD_TYPE (field)
354 && ! integer_zerop (TYPE_SIZE (TREE_TYPE (field))))
356 /* For these machines, a zero-length field does not
357 affect the alignment of the structure as a whole.
358 It does, however, affect the alignment of the next field
359 within the structure. */
360 if (! integer_zerop (DECL_SIZE (field)))
361 record_align = MAX (record_align, desired_align);
362 else if (! DECL_PACKED (field))
363 desired_align = TYPE_ALIGN (TREE_TYPE (field));
364 /* A named bit field of declared type `int'
365 forces the entire structure to have `int' alignment. */
366 if (DECL_NAME (field) != 0)
368 int type_align = TYPE_ALIGN (TREE_TYPE (field));
369 if (maximum_field_alignment != 0)
370 type_align = MIN (type_align, maximum_field_alignment);
372 record_align = MAX (record_align, type_align);
376 record_align = MAX (record_align, desired_align);
379 /* Does this field automatically have alignment it needs
380 by virtue of the fields that precede it and the record's
383 if (const_size % desired_align != 0
384 || (var_align % desired_align != 0
387 /* No, we need to skip space before this field.
388 Bump the cumulative size to multiple of field alignment. */
391 || var_align % desired_align == 0)
393 = CEIL (const_size, desired_align) * desired_align;
397 var_size = size_binop (PLUS_EXPR, var_size,
398 size_int (const_size));
400 var_size = round_up (var_size, desired_align);
401 var_align = MIN (var_align, desired_align);
405 #ifdef PCC_BITFIELD_TYPE_MATTERS
406 if (PCC_BITFIELD_TYPE_MATTERS
407 && TREE_CODE (field) == FIELD_DECL
408 && TREE_TYPE (field) != error_mark_node
409 && DECL_BIT_FIELD_TYPE (field)
410 && !DECL_PACKED (field)
411 /* If #pragma pack is in effect, turn off this feature. */
412 && maximum_field_alignment == 0
413 && !integer_zerop (DECL_SIZE (field)))
415 int type_align = TYPE_ALIGN (TREE_TYPE (field));
416 register tree dsize = DECL_SIZE (field);
417 int field_size = TREE_INT_CST_LOW (dsize);
419 /* A bit field may not span the unit of alignment of its type.
420 Advance to next boundary if necessary. */
421 /* ??? There is some uncertainty here as to what
422 should be done if type_align is less than the width of the type.
423 That can happen because the width exceeds BIGGEST_ALIGNMENT
424 or because it exceeds maximum_field_alignment. */
425 if (const_size / type_align
426 != (const_size + field_size - 1) / type_align)
427 const_size = CEIL (const_size, type_align) * type_align;
431 /* No existing machine description uses this parameter.
432 So I have made it in this aspect identical to PCC_BITFIELD_TYPE_MATTERS. */
433 #ifdef BITFIELD_NBYTES_LIMITED
434 if (BITFIELD_NBYTES_LIMITED
435 && TREE_CODE (field) == FIELD_DECL
436 && TREE_TYPE (field) != error_mark_node
437 && DECL_BIT_FIELD_TYPE (field)
438 && !DECL_PACKED (field)
439 && !integer_zerop (DECL_SIZE (field)))
441 int type_align = TYPE_ALIGN (TREE_TYPE (field));
442 register tree dsize = DECL_SIZE (field);
443 int field_size = TREE_INT_CST_LOW (dsize);
445 if (maximum_field_alignment != 0)
446 type_align = MIN (type_align, maximum_field_alignment);
448 /* A bit field may not span the unit of alignment of its type.
449 Advance to next boundary if necessary. */
450 if (const_size / type_align
451 != (const_size + field_size - 1) / type_align)
452 const_size = CEIL (const_size, type_align) * type_align;
456 /* Size so far becomes the position of this field. */
458 if (var_size && const_size)
459 DECL_FIELD_BITPOS (field)
460 = size_binop (PLUS_EXPR, var_size, size_int (const_size));
462 DECL_FIELD_BITPOS (field) = var_size;
465 DECL_FIELD_BITPOS (field) = size_int (const_size);
467 /* If this field ended up more aligned than we thought it
468 would be (we approximate this by seeing if its position
469 changed), lay out the field again; perhaps we can use an
470 integral mode for it now. */
471 if (known_align != const_size)
472 layout_decl (field, const_size);
475 /* Now add size of this field to the size of the record. */
478 register tree dsize = DECL_SIZE (field);
480 /* This can happen when we have an invalid nested struct definition,
481 such as struct j { struct j { int i; } }. The error message is
482 printed in finish_struct. */
485 else if (TREE_CODE (dsize) == INTEGER_CST
486 && TREE_INT_CST_HIGH (dsize) == 0
487 && TREE_INT_CST_LOW (dsize) + const_size > const_size)
488 /* Use const_size if there's no overflow. */
489 const_size += TREE_INT_CST_LOW (dsize);
495 var_size = size_binop (PLUS_EXPR, var_size, dsize);
500 /* Work out the total size and alignment of the record
501 as one expression and store in the record type.
502 Round it up to a multiple of the record's alignment. */
506 TYPE_SIZE (rec) = size_int (const_size);
512 = size_binop (PLUS_EXPR, var_size, size_int (const_size));
513 TYPE_SIZE (rec) = var_size;
516 /* Determine the desired alignment. */
517 #ifdef ROUND_TYPE_ALIGN
518 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), record_align);
520 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), record_align);
523 #ifdef ROUND_TYPE_SIZE
524 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
526 /* Round the size up to be a multiple of the required alignment */
527 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
530 return pending_statics;
533 /* Lay out a UNION_TYPE or QUAL_UNION_TYPE type.
534 Lay out all the fields, set their positions to zero,
535 and compute the size and alignment of the union (maximum of any field).
536 Note that if you set the TYPE_ALIGN before calling this
537 then the union align is aligned to at least that boundary. */
544 #ifdef STRUCTURE_SIZE_BOUNDARY
545 unsigned union_align = STRUCTURE_SIZE_BOUNDARY;
547 unsigned union_align = BITS_PER_UNIT;
550 /* The size of the union, based on the fields scanned so far,
551 is max (CONST_SIZE, VAR_SIZE).
552 VAR_SIZE may be null; then CONST_SIZE by itself is the size. */
553 register int const_size = 0;
554 register tree var_size = 0;
556 /* If this is a QUAL_UNION_TYPE, we want to process the fields in
557 the reverse order in building the COND_EXPR that denotes its
558 size. We reverse them again later. */
559 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
560 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
562 for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field))
564 /* Enums which are local to this class need not be laid out. */
565 if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL)
568 layout_decl (field, 0);
569 DECL_FIELD_BITPOS (field) = size_int (0);
571 /* Union must be at least as aligned as any field requires. */
573 union_align = MAX (union_align, DECL_ALIGN (field));
575 #ifdef PCC_BITFIELD_TYPE_MATTERS
576 /* On the m88000, a bit field of declare type `int'
577 forces the entire union to have `int' alignment. */
578 if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field))
579 union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field)));
582 if (TREE_CODE (rec) == UNION_TYPE)
584 /* Set union_size to max (decl_size, union_size).
585 There are more and less general ways to do this.
586 Use only CONST_SIZE unless forced to use VAR_SIZE. */
588 if (TREE_CODE (DECL_SIZE (field)) == INTEGER_CST)
590 = MAX (const_size, TREE_INT_CST_LOW (DECL_SIZE (field)));
591 else if (var_size == 0)
592 var_size = DECL_SIZE (field);
594 var_size = size_binop (MAX_EXPR, var_size, DECL_SIZE (field));
596 else if (TREE_CODE (rec) == QUAL_UNION_TYPE)
597 var_size = fold (build (COND_EXPR, sizetype, DECL_QUALIFIER (field),
599 var_size ? var_size : integer_zero_node));
602 if (TREE_CODE (rec) == QUAL_UNION_TYPE)
603 TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec));
605 /* Determine the ultimate size of the union (in bytes). */
606 if (NULL == var_size)
607 TYPE_SIZE (rec) = size_int (CEIL (const_size, BITS_PER_UNIT)
609 else if (const_size == 0)
610 TYPE_SIZE (rec) = var_size;
612 TYPE_SIZE (rec) = size_binop (MAX_EXPR, var_size,
613 round_up (size_int (const_size),
616 /* Determine the desired alignment. */
617 #ifdef ROUND_TYPE_ALIGN
618 TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), union_align);
620 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align);
623 #ifdef ROUND_TYPE_SIZE
624 TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec));
626 /* Round the size up to be a multiple of the required alignment */
627 TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec));
631 /* Calculate the mode, size, and alignment for TYPE.
632 For an array type, calculate the element separation as well.
633 Record TYPE on the chain of permanent or temporary types
634 so that dbxout will find out about it.
636 TYPE_SIZE of a type is nonzero if the type has been laid out already.
637 layout_type does nothing on such a type.
639 If the type is incomplete, its TYPE_SIZE remains zero. */
646 tree pending_statics;
651 /* Do nothing if type has been laid out before. */
652 if (TYPE_SIZE (type))
655 /* Make sure all nodes we allocate are not momentary;
656 they must last past the current statement. */
657 old = suspend_momentary ();
659 /* Put all our nodes into the same obstack as the type. Also,
660 make expressions saveable (this is a no-op for permanent types). */
662 push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type));
663 saveable_allocation ();
665 switch (TREE_CODE (type))
668 /* This kind of type is the responsibility
669 of the languge-specific code. */
674 if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST
675 && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0)
676 TREE_UNSIGNED (type) = 1;
678 TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type),
680 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
684 TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0);
685 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
689 TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type));
691 = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)),
692 (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE
693 ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT),
695 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
699 TYPE_SIZE (type) = size_zero_node;
700 TYPE_ALIGN (type) = 1;
701 TYPE_MODE (type) = VOIDmode;
705 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (Pmode));
706 TYPE_MODE (type) = Pmode;
711 TYPE_MODE (type) = mode_for_size (2 * GET_MODE_BITSIZE (Pmode),
713 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
718 TYPE_MODE (type) = Pmode;
719 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
720 TREE_UNSIGNED (type) = 1;
721 TYPE_PRECISION (type) = GET_MODE_BITSIZE (TYPE_MODE (type));
726 register tree index = TYPE_DOMAIN (type);
727 register tree element = TREE_TYPE (type);
729 build_pointer_type (element);
731 /* We need to know both bounds in order to compute the size. */
732 if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index)
733 && TYPE_SIZE (element))
736 = size_binop (PLUS_EXPR, size_one_node,
737 size_binop (MINUS_EXPR, TYPE_MAX_VALUE (index),
738 TYPE_MIN_VALUE (index)));
740 TYPE_SIZE (type) = size_binop (MULT_EXPR, length,
741 TYPE_SIZE (element));
744 /* Now round the alignment and size,
745 using machine-dependent criteria if any. */
747 #ifdef ROUND_TYPE_ALIGN
749 = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT);
751 TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT);
754 #ifdef ROUND_TYPE_SIZE
755 if (TYPE_SIZE (type) != 0)
757 = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type));
760 TYPE_MODE (type) = BLKmode;
761 if (TYPE_SIZE (type) != 0
762 && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
763 /* BLKmode elements force BLKmode aggregate;
764 else extract/store fields may lose. */
765 && (TYPE_MODE (TREE_TYPE (type)) != BLKmode
766 || TYPE_NO_FORCE_BLK (TREE_TYPE (type))))
769 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
772 if (STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT
773 && TYPE_ALIGN (type) < TREE_INT_CST_LOW (TYPE_SIZE (type))
774 && TYPE_MODE (type) != BLKmode)
776 TYPE_NO_FORCE_BLK (type) = 1;
777 TYPE_MODE (type) = BLKmode;
784 pending_statics = layout_record (type);
785 TYPE_MODE (type) = BLKmode;
786 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST)
789 /* A record which has any BLKmode members must itself be BLKmode;
790 it can't go in a register.
791 Unless the member is BLKmode only because it isn't aligned. */
792 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
796 if (TREE_CODE (field) != FIELD_DECL)
799 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
800 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
803 if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST)
806 bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field));
808 /* Must be BLKmode if any field crosses a word boundary,
809 since extract_bit_field can't handle that in registers. */
810 if (bitpos / BITS_PER_WORD
811 != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1)
813 /* But there is no problem if the field is entire words. */
814 && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD == 0)
819 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
822 /* If structure's known alignment is less than
823 what the scalar mode would need, and it matters,
824 then stick with BLKmode. */
826 && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
827 || (TYPE_ALIGN (type)
828 >= TREE_INT_CST_LOW (TYPE_SIZE (type)))))
830 if (TYPE_MODE (type) != BLKmode)
831 /* If this is the only reason this type is BLKmode,
832 then don't force containing types to be BLKmode. */
833 TYPE_NO_FORCE_BLK (type) = 1;
834 TYPE_MODE (type) = BLKmode;
840 /* Lay out any static members. This is done now
841 because their type may use the record's type. */
842 while (pending_statics)
844 layout_decl (TREE_VALUE (pending_statics), 0);
845 pending_statics = TREE_CHAIN (pending_statics);
850 case QUAL_UNION_TYPE:
852 TYPE_MODE (type) = BLKmode;
853 if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST
854 /* If structure's known alignment is less than
855 what the scalar mode would need, and it matters,
856 then stick with BLKmode. */
857 && (! STRICT_ALIGNMENT
858 || TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT
859 || TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type))))
862 /* A union which has any BLKmode members must itself be BLKmode;
863 it can't go in a register.
864 Unless the member is BLKmode only because it isn't aligned. */
865 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
867 if (TREE_CODE (field) != FIELD_DECL)
870 if (TYPE_MODE (TREE_TYPE (field)) == BLKmode
871 && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field)))
876 = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)),
884 case BOOLEAN_TYPE: /* store one byte/boolean for now. */
885 TYPE_MODE (type) = QImode;
886 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
887 TYPE_PRECISION (type) = 1;
888 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
892 TYPE_MODE (type) = QImode;
893 TYPE_SIZE (type) = size_int (GET_MODE_BITSIZE (TYPE_MODE (type)));
894 TYPE_PRECISION (type) = GET_MODE_BITSIZE (TYPE_MODE (type));
895 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
899 /* The size may vary in different languages, so the language front end
900 should fill in the size. */
901 TYPE_ALIGN (type) = BIGGEST_ALIGNMENT;
902 TYPE_MODE (type) = BLKmode;
909 /* Normally, use the alignment corresponding to the mode chosen.
910 However, where strict alignment is not required, avoid
911 over-aligning structures, since most compilers do not do this
914 if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode
916 || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
917 && TREE_CODE (type) != QUAL_UNION_TYPE
918 && TREE_CODE (type) != ARRAY_TYPE)))
919 TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type));
921 /* Evaluate nonconstant size only once, either now or as soon as safe. */
922 if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST)
923 TYPE_SIZE (type) = variable_size (TYPE_SIZE (type));
925 /* Also layout any other variants of the type. */
926 if (TYPE_NEXT_VARIANT (type)
927 || type != TYPE_MAIN_VARIANT (type))
930 /* Record layout info of this variant. */
931 tree size = TYPE_SIZE (type);
932 int align = TYPE_ALIGN (type);
933 enum machine_mode mode = TYPE_MODE (type);
935 /* Copy it into all variants. */
936 for (variant = TYPE_MAIN_VARIANT (type);
938 variant = TYPE_NEXT_VARIANT (variant))
940 TYPE_SIZE (variant) = size;
941 TYPE_ALIGN (variant) = align;
942 TYPE_MODE (variant) = mode;
947 resume_momentary (old);
950 /* Create and return a type for signed integers of PRECISION bits. */
953 make_signed_type (precision)
956 register tree type = make_node (INTEGER_TYPE);
958 TYPE_PRECISION (type) = precision;
960 /* Create the extreme values based on the number of bits. */
962 TYPE_MIN_VALUE (type)
963 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
964 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
965 (((HOST_WIDE_INT) (-1)
966 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
967 ? precision - HOST_BITS_PER_WIDE_INT - 1
969 TYPE_MAX_VALUE (type)
970 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
971 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
972 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
973 ? (((HOST_WIDE_INT) 1
974 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
977 /* Give this type's extreme values this type as their type. */
979 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
980 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
982 /* The first type made with this or `make_unsigned_type'
983 is the type for size values. */
990 /* Lay out the type: set its alignment, size, etc. */
997 /* Create and return a type for unsigned integers of PRECISION bits. */
1000 make_unsigned_type (precision)
1003 register tree type = make_node (INTEGER_TYPE);
1005 TYPE_PRECISION (type) = precision;
1007 /* The first type made with this or `make_signed_type'
1008 is the type for size values. */
1015 fixup_unsigned_type (type);
1019 /* Set the extreme values of TYPE based on its precision in bits,
1020 then lay it out. Used when make_signed_type won't do
1021 because the tree code is not INTEGER_TYPE.
1022 E.g. for Pascal, when the -fsigned-char option is given. */
1025 fixup_signed_type (type)
1028 register int precision = TYPE_PRECISION (type);
1030 TYPE_MIN_VALUE (type)
1031 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1032 ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)),
1033 (((HOST_WIDE_INT) (-1)
1034 << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1035 ? precision - HOST_BITS_PER_WIDE_INT - 1
1037 TYPE_MAX_VALUE (type)
1038 = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0
1039 ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1),
1040 (precision - HOST_BITS_PER_WIDE_INT - 1 > 0
1041 ? (((HOST_WIDE_INT) 1
1042 << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1
1045 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1046 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1048 /* Lay out the type: set its alignment, size, etc. */
1053 /* Set the extreme values of TYPE based on its precision in bits,
1054 then lay it out. This is used both in `make_unsigned_type'
1055 and for enumeral types. */
1058 fixup_unsigned_type (type)
1061 register int precision = TYPE_PRECISION (type);
1063 TYPE_MIN_VALUE (type) = build_int_2 (0, 0);
1064 TYPE_MAX_VALUE (type)
1065 = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0
1066 ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1,
1067 precision - HOST_BITS_PER_WIDE_INT > 0
1068 ? ((unsigned HOST_WIDE_INT) ~0
1069 >> (HOST_BITS_PER_WIDE_INT
1070 - (precision - HOST_BITS_PER_WIDE_INT)))
1072 TREE_TYPE (TYPE_MIN_VALUE (type)) = type;
1073 TREE_TYPE (TYPE_MAX_VALUE (type)) = type;
1075 /* Lay out the type: set its alignment, size, etc. */
1080 /* Find the best machine mode to use when referencing a bit field of length
1081 BITSIZE bits starting at BITPOS.
1083 The underlying object is known to be aligned to a boundary of ALIGN bits.
1084 If LARGEST_MODE is not VOIDmode, it means that we should not use a mode
1085 larger than LARGEST_MODE (usually SImode).
1087 If no mode meets all these conditions, we return VOIDmode. Otherwise, if
1088 VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest
1089 mode meeting these conditions.
1091 Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return
1092 the largest mode (but a mode no wider than UNITS_PER_WORD) that meets
1093 all the conditions. */
1096 get_best_mode (bitsize, bitpos, align, largest_mode, volatilep)
1097 int bitsize, bitpos;
1099 enum machine_mode largest_mode;
1102 enum machine_mode mode;
1105 /* Find the narrowest integer mode that contains the bit field. */
1106 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1107 mode = GET_MODE_WIDER_MODE (mode))
1109 unit = GET_MODE_BITSIZE (mode);
1110 if (bitpos / unit == (bitpos + bitsize - 1) / unit)
1114 if (mode == MAX_MACHINE_MODE
1115 /* It is tempting to omit the following line
1116 if STRICT_ALIGNMENT is true.
1117 But that is incorrect, since if the bitfield uses part of 3 bytes
1118 and we use a 4-byte mode, we could get a spurious segv
1119 if the extra 4th byte is past the end of memory.
1120 (Though at least one Unix compiler ignores this problem:
1121 that on the Sequent 386 machine. */
1122 || MIN (unit, BIGGEST_ALIGNMENT) > align
1123 || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode)))
1126 if (SLOW_BYTE_ACCESS && ! volatilep)
1128 enum machine_mode wide_mode = VOIDmode, tmode;
1130 for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode;
1131 tmode = GET_MODE_WIDER_MODE (tmode))
1133 unit = GET_MODE_BITSIZE (tmode);
1134 if (bitpos / unit == (bitpos + bitsize - 1) / unit
1135 && unit <= BITS_PER_WORD
1136 && unit <= MIN (align, BIGGEST_ALIGNMENT)
1137 && (largest_mode == VOIDmode
1138 || unit <= GET_MODE_BITSIZE (largest_mode)))
1142 if (wide_mode != VOIDmode)
1149 /* Save all variables describing the current status into the structure *P.
1150 This is used before starting a nested function. */
1153 save_storage_status (p)
1156 #if 0 /* Need not save, since always 0 and non0 (resp.) within a function. */
1157 p->pending_sizes = pending_sizes;
1158 p->immediate_size_expand = immediate_size_expand;
1162 /* Restore all variables describing the current status from the structure *P.
1163 This is used after a nested function. */
1166 restore_storage_status (p)
1170 pending_sizes = p->pending_sizes;
1171 immediate_size_expand = p->immediate_size_expand;